Re: [patch 7/13] Encode and decode arbitrary XDR arrays

2005-02-17 Thread Adrian Bunk
On Tue, Feb 15, 2005 at 02:17:18PM -0500, Trond Myklebust wrote:
> 
> net/sunrpc/xdr.c:1024:3: warning: mixing declarations and code
>...
> Please don't use these gcc extensions in the kernel.

Just for the record:
This is not a gcc extension - this is C99 but not supported by
gcc 2.95 (which is a supported compiler for kernel 2.6).

> Cheers,
>   Trond

cu
Adrian

-- 

   "Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
   "Only a promise," Lao Er said.
   Pearl S. Buck - Dragon Seed

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [patch 7/13] Encode and decode arbitrary XDR arrays

2005-02-17 Thread Adrian Bunk
On Tue, Feb 15, 2005 at 02:17:18PM -0500, Trond Myklebust wrote:
 
 net/sunrpc/xdr.c:1024:3: warning: mixing declarations and code
...
 Please don't use these gcc extensions in the kernel.

Just for the record:
This is not a gcc extension - this is C99 but not supported by
gcc 2.95 (which is a supported compiler for kernel 2.6).

 Cheers,
   Trond

cu
Adrian

-- 

   Is there not promise of rain? Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
   Only a promise, Lao Er said.
   Pearl S. Buck - Dragon Seed

-
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [patch 7/13] Encode and decode arbitrary XDR arrays

2005-02-16 Thread Andreas Gruenbacher
On Tue, 2005-02-15 at 20:17, Trond Myklebust wrote:
> lau den 22.01.2005 Klokka 21:34 (+0100) skreiv Andreas Gruenbacher:
> > vanlig tekstdokument vedlegg (patches.suse)
> > Add xdr_encode_array2 and xdr_decode_array2 functions for encoding
> > end decoding arrays with arbitrary entries, such as acl entries. The
> > goal here is to do this without allocating a contiguous temporary
> > buffer.
> 
> net/sunrpc/xdr.c:1024:3: warning: mixing declarations and code
> net/sunrpc/xdr.c:967:16: warning: bad constant expression
> 
> Please don't use these gcc extensions in the kernel.

Andrew has anready fixed the "mixing declarations and code" thing. The
attached patch kmallocs the buffer if needed. This uglifies the code
quite a bit though...

Cheers,
-- 
Andreas Gruenbacher <[EMAIL PROTECTED]>
SUSE Labs, SUSE LINUX GMBH
Index: linux-2.6.11-rc3/net/sunrpc/xdr.c
===
--- linux-2.6.11-rc3.orig/net/sunrpc/xdr.c
+++ linux-2.6.11-rc3/net/sunrpc/xdr.c
@@ -964,10 +964,10 @@ static int
 xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
 		 struct xdr_array2_desc *desc, int encode)
 {
-	char elem[desc->elem_size], *c;
+	char *elem = NULL, *c;
 	unsigned int copied = 0, todo, avail_here;
 	struct page **ppages = NULL;
-	int err = 0;
+	int err;
 
 	if (encode) {
 		if (xdr_encode_word(buf, base, desc->array_len) != 0)
@@ -1000,6 +1000,12 @@ xdr_xcode_array2(struct xdr_buf *buf, un
 			avail_here -= desc->elem_size;
 		}
 		if (avail_here) {
+			if (!elem) {
+elem = kmalloc(desc->elem_size, GFP_KERNEL);
+err = -ENOMEM;
+if (!elem)
+	goto out;
+			}
 			if (encode) {
 err = desc->xcode(desc, elem);
 if (err)
@@ -1032,6 +1038,13 @@ xdr_xcode_array2(struct xdr_buf *buf, un
 			if (copied || avail_page < desc->elem_size) {
 unsigned int l = min(avail_page,
 	desc->elem_size - copied);
+if (!elem) {
+	elem = kmalloc(desc->elem_size,
+		   GFP_KERNEL);
+	err = -ENOMEM;
+	if (!elem)
+		goto out;
+}
 if (encode) {
 	if (!copied) {
 		err = desc->xcode(desc, elem);
@@ -1065,6 +1078,13 @@ xdr_xcode_array2(struct xdr_buf *buf, un
 			if (avail_page) {
 unsigned int l = min(avail_page,
 	desc->elem_size - copied);
+if (!elem) {
+	elem = kmalloc(desc->elem_size,
+		   GFP_KERNEL);
+	err = -ENOMEM;
+	if (!elem)
+		goto out;
+}
 if (encode) {
 	if (!copied) {
 		err = desc->xcode(desc, elem);
@@ -1124,8 +1144,11 @@ xdr_xcode_array2(struct xdr_buf *buf, un
 			todo -= desc->elem_size;
 		}
 	}
+	err = 0;
 
 out:
+	if (elem)
+		kfree(elem);
 	if (ppages)
 		kunmap(*ppages);
 	return err;


Re: [patch 7/13] Encode and decode arbitrary XDR arrays

2005-02-16 Thread Andreas Gruenbacher
On Tue, 2005-02-15 at 20:17, Trond Myklebust wrote:
 lau den 22.01.2005 Klokka 21:34 (+0100) skreiv Andreas Gruenbacher:
  vanlig tekstdokument vedlegg (patches.suse)
  Add xdr_encode_array2 and xdr_decode_array2 functions for encoding
  end decoding arrays with arbitrary entries, such as acl entries. The
  goal here is to do this without allocating a contiguous temporary
  buffer.
 
 net/sunrpc/xdr.c:1024:3: warning: mixing declarations and code
 net/sunrpc/xdr.c:967:16: warning: bad constant expression
 
 Please don't use these gcc extensions in the kernel.

Andrew has anready fixed the mixing declarations and code thing. The
attached patch kmallocs the buffer if needed. This uglifies the code
quite a bit though...

Cheers,
-- 
Andreas Gruenbacher [EMAIL PROTECTED]
SUSE Labs, SUSE LINUX GMBH
Index: linux-2.6.11-rc3/net/sunrpc/xdr.c
===
--- linux-2.6.11-rc3.orig/net/sunrpc/xdr.c
+++ linux-2.6.11-rc3/net/sunrpc/xdr.c
@@ -964,10 +964,10 @@ static int
 xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
 		 struct xdr_array2_desc *desc, int encode)
 {
-	char elem[desc-elem_size], *c;
+	char *elem = NULL, *c;
 	unsigned int copied = 0, todo, avail_here;
 	struct page **ppages = NULL;
-	int err = 0;
+	int err;
 
 	if (encode) {
 		if (xdr_encode_word(buf, base, desc-array_len) != 0)
@@ -1000,6 +1000,12 @@ xdr_xcode_array2(struct xdr_buf *buf, un
 			avail_here -= desc-elem_size;
 		}
 		if (avail_here) {
+			if (!elem) {
+elem = kmalloc(desc-elem_size, GFP_KERNEL);
+err = -ENOMEM;
+if (!elem)
+	goto out;
+			}
 			if (encode) {
 err = desc-xcode(desc, elem);
 if (err)
@@ -1032,6 +1038,13 @@ xdr_xcode_array2(struct xdr_buf *buf, un
 			if (copied || avail_page  desc-elem_size) {
 unsigned int l = min(avail_page,
 	desc-elem_size - copied);
+if (!elem) {
+	elem = kmalloc(desc-elem_size,
+		   GFP_KERNEL);
+	err = -ENOMEM;
+	if (!elem)
+		goto out;
+}
 if (encode) {
 	if (!copied) {
 		err = desc-xcode(desc, elem);
@@ -1065,6 +1078,13 @@ xdr_xcode_array2(struct xdr_buf *buf, un
 			if (avail_page) {
 unsigned int l = min(avail_page,
 	desc-elem_size - copied);
+if (!elem) {
+	elem = kmalloc(desc-elem_size,
+		   GFP_KERNEL);
+	err = -ENOMEM;
+	if (!elem)
+		goto out;
+}
 if (encode) {
 	if (!copied) {
 		err = desc-xcode(desc, elem);
@@ -1124,8 +1144,11 @@ xdr_xcode_array2(struct xdr_buf *buf, un
 			todo -= desc-elem_size;
 		}
 	}
+	err = 0;
 
 out:
+	if (elem)
+		kfree(elem);
 	if (ppages)
 		kunmap(*ppages);
 	return err;


Re: [patch 7/13] Encode and decode arbitrary XDR arrays

2005-02-15 Thread Trond Myklebust
lau den 22.01.2005 Klokka 21:34 (+0100) skreiv Andreas Gruenbacher:
> vanlig tekstdokument vedlegg (patches.suse)
> Add xdr_encode_array2 and xdr_decode_array2 functions for encoding
> end decoding arrays with arbitrary entries, such as acl entries. The
> goal here is to do this without allocating a contiguous temporary
> buffer.

net/sunrpc/xdr.c:1024:3: warning: mixing declarations and code
net/sunrpc/xdr.c:967:16: warning: bad constant expression

Please don't use these gcc extensions in the kernel.

Cheers,
  Trond

-- 
Trond Myklebust <[EMAIL PROTECTED]>

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [patch 7/13] Encode and decode arbitrary XDR arrays

2005-02-15 Thread Trond Myklebust
lau den 22.01.2005 Klokka 21:34 (+0100) skreiv Andreas Gruenbacher:
 vanlig tekstdokument vedlegg (patches.suse)
 Add xdr_encode_array2 and xdr_decode_array2 functions for encoding
 end decoding arrays with arbitrary entries, such as acl entries. The
 goal here is to do this without allocating a contiguous temporary
 buffer.

net/sunrpc/xdr.c:1024:3: warning: mixing declarations and code
net/sunrpc/xdr.c:967:16: warning: bad constant expression

Please don't use these gcc extensions in the kernel.

Cheers,
  Trond

-- 
Trond Myklebust [EMAIL PROTECTED]

-
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[patch 7/13] Encode and decode arbitrary XDR arrays

2005-01-22 Thread Andreas Gruenbacher
Add xdr_encode_array2 and xdr_decode_array2 functions for encoding
end decoding arrays with arbitrary entries, such as acl entries. The
goal here is to do this without allocating a contiguous temporary
buffer.

Signed-off-by: Andreas Gruenbacher <[EMAIL PROTECTED]>
Acked-by: Olaf Kirch <[EMAIL PROTECTED]>

Index: linux-2.6.11-rc2/include/linux/sunrpc/xdr.h
===
--- linux-2.6.11-rc2.orig/include/linux/sunrpc/xdr.h
+++ linux-2.6.11-rc2/include/linux/sunrpc/xdr.h
@@ -146,7 +146,8 @@ extern void xdr_shift_buf(struct xdr_buf
 extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
 extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, int, int);
 extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, int);
-extern int read_bytes_from_xdr_buf(struct xdr_buf *buf, int base, void *obj, 
int len);
+extern int read_bytes_from_xdr_buf(struct xdr_buf *, int, void *, int);
+extern int write_bytes_to_xdr_buf(struct xdr_buf *, int, void *, int);
 
 /*
  * Helper structure for copying from an sk_buff.
@@ -168,6 +169,22 @@ struct sockaddr;
 extern int xdr_sendpages(struct socket *, struct sockaddr *, int,
struct xdr_buf *, unsigned int, int);
 
+extern int xdr_encode_word(struct xdr_buf *, int, u32);
+extern int xdr_decode_word(struct xdr_buf *, int, u32 *);
+
+struct xdr_array2_desc;
+typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
+struct xdr_array2_desc {
+   unsigned int elem_size;
+   unsigned int array_len;
+   xdr_xcode_elem_t xcode;
+};
+
+extern int xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
+ struct xdr_array2_desc *desc);
+extern int xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
+struct xdr_array2_desc *desc);
+
 /*
  * Provide some simple tools for XDR buffer overflow-checking etc.
  */
Index: linux-2.6.11-rc2/net/sunrpc/sunrpc_syms.c
===
--- linux-2.6.11-rc2.orig/net/sunrpc/sunrpc_syms.c
+++ linux-2.6.11-rc2/net/sunrpc/sunrpc_syms.c
@@ -129,6 +129,10 @@ EXPORT_SYMBOL(xdr_encode_netobj);
 EXPORT_SYMBOL(xdr_encode_pages);
 EXPORT_SYMBOL(xdr_inline_pages);
 EXPORT_SYMBOL(xdr_shift_buf);
+EXPORT_SYMBOL(xdr_encode_word);
+EXPORT_SYMBOL(xdr_decode_word);
+EXPORT_SYMBOL(xdr_encode_array2);
+EXPORT_SYMBOL(xdr_decode_array2);
 EXPORT_SYMBOL(xdr_buf_from_iov);
 EXPORT_SYMBOL(xdr_buf_subsegment);
 EXPORT_SYMBOL(xdr_buf_read_netobj);
Index: linux-2.6.11-rc2/net/sunrpc/xdr.c
===
--- linux-2.6.11-rc2.orig/net/sunrpc/xdr.c
+++ linux-2.6.11-rc2/net/sunrpc/xdr.c
@@ -868,8 +868,34 @@ out:
return status;
 }
 
-static int
-read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)
+/* obj is assumed to point to allocated memory of size at least len: */
+int
+write_bytes_to_xdr_buf(struct xdr_buf *buf, int base, void *obj, int len)
+{
+   struct xdr_buf subbuf;
+   int this_len;
+   int status;
+
+   status = xdr_buf_subsegment(buf, , base, len);
+   if (status)
+   goto out;
+   this_len = min(len, (int)subbuf.head[0].iov_len);
+   memcpy(subbuf.head[0].iov_base, obj, this_len);
+   len -= this_len;
+   obj += this_len;
+   this_len = min(len, (int)subbuf.page_len);
+   if (this_len)
+   _copy_to_pages(subbuf.pages, subbuf.page_base, obj, this_len);
+   len -= this_len;
+   obj += this_len;
+   this_len = min(len, (int)subbuf.tail[0].iov_len);
+   memcpy(subbuf.tail[0].iov_base, obj, this_len);
+out:
+   return status;
+}
+
+int
+xdr_decode_word(struct xdr_buf *buf, int base, u32 *obj)
 {
u32 raw;
int status;
@@ -881,6 +907,14 @@ read_u32_from_xdr_buf(struct xdr_buf *bu
return 0;
 }
 
+int
+xdr_encode_word(struct xdr_buf *buf, int base, u32 obj)
+{
+   u32 raw = htonl(obj);
+
+   return write_bytes_to_xdr_buf(buf, base, , sizeof(obj));
+}
+
 /* If the netobj starting offset bytes from the start of xdr_buf is contained
  * entirely in the head or the tail, set object to point to it; otherwise
  * try to find space for it at the end of the tail, copy it there, and
@@ -891,7 +925,7 @@ xdr_buf_read_netobj(struct xdr_buf *buf,
u32 tail_offset = buf->head[0].iov_len + buf->page_len;
u32 obj_end_offset;
 
-   if (read_u32_from_xdr_buf(buf, offset, >len))
+   if (xdr_decode_word(buf, offset, >len))
goto out;
obj_end_offset = offset + 4 + obj->len;
 
@@ -924,3 +958,194 @@ xdr_buf_read_netobj(struct xdr_buf *buf,
 out:
return -1;
 }
+
+/* Returns 0 on success, or else a negative error code. */
+static int
+xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
+struct xdr_array2_desc *desc, int encode)
+{
+   char elem[desc->elem_size], *c;
+   unsigned 

[patch 7/13] Encode and decode arbitrary XDR arrays

2005-01-22 Thread Andreas Gruenbacher
Add xdr_encode_array2 and xdr_decode_array2 functions for encoding
end decoding arrays with arbitrary entries, such as acl entries. The
goal here is to do this without allocating a contiguous temporary
buffer.

Signed-off-by: Andreas Gruenbacher [EMAIL PROTECTED]
Acked-by: Olaf Kirch [EMAIL PROTECTED]

Index: linux-2.6.11-rc2/include/linux/sunrpc/xdr.h
===
--- linux-2.6.11-rc2.orig/include/linux/sunrpc/xdr.h
+++ linux-2.6.11-rc2/include/linux/sunrpc/xdr.h
@@ -146,7 +146,8 @@ extern void xdr_shift_buf(struct xdr_buf
 extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
 extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, int, int);
 extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, int);
-extern int read_bytes_from_xdr_buf(struct xdr_buf *buf, int base, void *obj, 
int len);
+extern int read_bytes_from_xdr_buf(struct xdr_buf *, int, void *, int);
+extern int write_bytes_to_xdr_buf(struct xdr_buf *, int, void *, int);
 
 /*
  * Helper structure for copying from an sk_buff.
@@ -168,6 +169,22 @@ struct sockaddr;
 extern int xdr_sendpages(struct socket *, struct sockaddr *, int,
struct xdr_buf *, unsigned int, int);
 
+extern int xdr_encode_word(struct xdr_buf *, int, u32);
+extern int xdr_decode_word(struct xdr_buf *, int, u32 *);
+
+struct xdr_array2_desc;
+typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
+struct xdr_array2_desc {
+   unsigned int elem_size;
+   unsigned int array_len;
+   xdr_xcode_elem_t xcode;
+};
+
+extern int xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
+ struct xdr_array2_desc *desc);
+extern int xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
+struct xdr_array2_desc *desc);
+
 /*
  * Provide some simple tools for XDR buffer overflow-checking etc.
  */
Index: linux-2.6.11-rc2/net/sunrpc/sunrpc_syms.c
===
--- linux-2.6.11-rc2.orig/net/sunrpc/sunrpc_syms.c
+++ linux-2.6.11-rc2/net/sunrpc/sunrpc_syms.c
@@ -129,6 +129,10 @@ EXPORT_SYMBOL(xdr_encode_netobj);
 EXPORT_SYMBOL(xdr_encode_pages);
 EXPORT_SYMBOL(xdr_inline_pages);
 EXPORT_SYMBOL(xdr_shift_buf);
+EXPORT_SYMBOL(xdr_encode_word);
+EXPORT_SYMBOL(xdr_decode_word);
+EXPORT_SYMBOL(xdr_encode_array2);
+EXPORT_SYMBOL(xdr_decode_array2);
 EXPORT_SYMBOL(xdr_buf_from_iov);
 EXPORT_SYMBOL(xdr_buf_subsegment);
 EXPORT_SYMBOL(xdr_buf_read_netobj);
Index: linux-2.6.11-rc2/net/sunrpc/xdr.c
===
--- linux-2.6.11-rc2.orig/net/sunrpc/xdr.c
+++ linux-2.6.11-rc2/net/sunrpc/xdr.c
@@ -868,8 +868,34 @@ out:
return status;
 }
 
-static int
-read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)
+/* obj is assumed to point to allocated memory of size at least len: */
+int
+write_bytes_to_xdr_buf(struct xdr_buf *buf, int base, void *obj, int len)
+{
+   struct xdr_buf subbuf;
+   int this_len;
+   int status;
+
+   status = xdr_buf_subsegment(buf, subbuf, base, len);
+   if (status)
+   goto out;
+   this_len = min(len, (int)subbuf.head[0].iov_len);
+   memcpy(subbuf.head[0].iov_base, obj, this_len);
+   len -= this_len;
+   obj += this_len;
+   this_len = min(len, (int)subbuf.page_len);
+   if (this_len)
+   _copy_to_pages(subbuf.pages, subbuf.page_base, obj, this_len);
+   len -= this_len;
+   obj += this_len;
+   this_len = min(len, (int)subbuf.tail[0].iov_len);
+   memcpy(subbuf.tail[0].iov_base, obj, this_len);
+out:
+   return status;
+}
+
+int
+xdr_decode_word(struct xdr_buf *buf, int base, u32 *obj)
 {
u32 raw;
int status;
@@ -881,6 +907,14 @@ read_u32_from_xdr_buf(struct xdr_buf *bu
return 0;
 }
 
+int
+xdr_encode_word(struct xdr_buf *buf, int base, u32 obj)
+{
+   u32 raw = htonl(obj);
+
+   return write_bytes_to_xdr_buf(buf, base, raw, sizeof(obj));
+}
+
 /* If the netobj starting offset bytes from the start of xdr_buf is contained
  * entirely in the head or the tail, set object to point to it; otherwise
  * try to find space for it at the end of the tail, copy it there, and
@@ -891,7 +925,7 @@ xdr_buf_read_netobj(struct xdr_buf *buf,
u32 tail_offset = buf-head[0].iov_len + buf-page_len;
u32 obj_end_offset;
 
-   if (read_u32_from_xdr_buf(buf, offset, obj-len))
+   if (xdr_decode_word(buf, offset, obj-len))
goto out;
obj_end_offset = offset + 4 + obj-len;
 
@@ -924,3 +958,194 @@ xdr_buf_read_netobj(struct xdr_buf *buf,
 out:
return -1;
 }
+
+/* Returns 0 on success, or else a negative error code. */
+static int
+xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
+struct xdr_array2_desc *desc, int encode)
+{
+   char elem[desc-elem_size], *c;
+