mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andreas Gruenbacher <agruen@suse.de>
To: linux-kernel@vger.kernel.org, Neil Brown <neilb@cse.unsw.edu.au>,
	Trond Myklebust <trond.myklebust@fys.uio.no>
Cc: Olaf Kirch <okir@suse.de>,
	"Andries E. Brouwer" <Andries.Brouwer@cwi.nl>,
	Buck Huppmann <buchk@pobox.com>, Andrew Morton <akpm@osdl.org>
Subject: [patch 7/13] Encode and decode arbitrary XDR arrays
Date: Sat, 22 Jan 2005 21:34:07 +0100	[thread overview]
Message-ID: <20050122203619.570180000@blunzn.suse.de> (raw)
In-Reply-To: <20050122203326.402087000@blunzn.suse.de>

[-- Attachment #1: patches.suse/sunrpc-xdr-arrays --]
[-- Type: text/plain, Size: 8972 bytes --]

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 <agruen@suse.de>
Acked-by: Olaf Kirch <okir@suse.de>

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;
+	unsigned int copied = 0, todo, avail_here;
+	struct page **ppages = NULL;
+	int err = 0;
+
+	if (encode) {
+		if (xdr_encode_word(buf, base, desc->array_len) != 0)
+			return -EINVAL;
+	} else {
+		if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
+		    (unsigned long) base + 4 + desc->array_len *
+				    desc->elem_size > buf->len)
+			return -EINVAL;
+	}
+	base += 4;
+
+	if (!desc->xcode)
+		return 0;
+
+	todo = desc->array_len * desc->elem_size;
+	
+	/* process head */
+	if (todo && base < buf->head->iov_len) {
+		c = buf->head->iov_base + base;
+		avail_here = min_t(unsigned int, todo,
+				   buf->head->iov_len - base);
+		todo -= avail_here;
+
+		while (avail_here >= desc->elem_size) {
+			err = desc->xcode(desc, c);
+			if (err)
+				goto out;
+			c += desc->elem_size;
+			avail_here -= desc->elem_size;
+		}
+		if (avail_here) {
+			if (encode) {
+				err = desc->xcode(desc, elem);
+				if (err)
+					goto out;
+				memcpy(c, elem, avail_here);
+			} else
+				memcpy(elem, c, avail_here);
+			copied = avail_here;
+		}
+		base = buf->head->iov_len;  /* align to start of pages */
+	}
+
+	/* process pages array */
+	base -= buf->head->iov_len;
+	if (todo && base < buf->page_len) {
+		avail_here = min(todo, buf->page_len - base);
+		todo -= avail_here;
+
+		base += buf->page_base;
+		ppages = buf->pages + (base >> PAGE_CACHE_SHIFT);
+		base &= ~PAGE_CACHE_MASK;
+		unsigned int avail_page = min_t(unsigned int,
+			PAGE_CACHE_SIZE - base, avail_here);
+		c = kmap(*ppages) + base;
+
+		while (avail_here) {
+			avail_here -= avail_page;
+			if (copied || avail_page < desc->elem_size) {
+				unsigned int l = min(avail_page,
+					desc->elem_size - copied);
+				if (encode) {
+					if (!copied) {
+						err = desc->xcode(desc, elem);
+						if (err)
+							goto out;
+					}
+					memcpy(c, elem + copied, l);
+					copied += l;
+					if (copied == desc->elem_size)
+						copied = 0;
+				} else {
+					memcpy(elem + copied, c, l);
+					copied += l;
+					if (copied == desc->elem_size) {
+						err = desc->xcode(desc, elem);
+						if (err)
+							goto out;
+						copied = 0;
+					}
+				}
+				avail_page -= l;
+				c += l;
+			}
+			while (avail_page >= desc->elem_size) {
+				err = desc->xcode(desc, c);
+				if (err)
+					goto out;
+				c += desc->elem_size;
+				avail_page -= desc->elem_size;
+			}
+			if (avail_page) {
+				unsigned int l = min(avail_page,
+					    desc->elem_size - copied);
+				if (encode) {
+					if (!copied) {
+						err = desc->xcode(desc, elem);
+						if (err)
+							goto out;
+					}
+					memcpy(c, elem + copied, l);
+					copied += l;
+					if (copied == desc->elem_size)
+						copied = 0;
+				} else {
+					memcpy(elem + copied, c, l);
+					copied += l;
+					if (copied == desc->elem_size) {
+						err = desc->xcode(desc, elem);
+						if (err)
+							goto out;
+						copied = 0;
+					}
+				}
+			}
+			if (avail_here) {
+				kunmap(*ppages);
+				ppages++;
+				c = kmap(*ppages);
+			}
+
+			avail_page = min(avail_here,
+				 (unsigned int) PAGE_CACHE_SIZE);
+		}
+		base = buf->page_len;  /* align to start of tail */
+	}
+
+	/* process tail */
+	base -= buf->page_len;
+	if (todo) {
+		c = buf->tail->iov_base + base;
+		if (copied) {
+			unsigned int l = desc->elem_size - copied;
+
+			if (encode)
+				memcpy(c, elem + copied, l);
+			else {
+				memcpy(elem + copied, c, l);
+				err = desc->xcode(desc, elem);
+				if (err)
+					goto out;
+			}
+			todo -= l;
+			c += l;
+		}
+		while (todo) {
+			err = desc->xcode(desc, c);
+			if (err)
+				goto out;
+			c += desc->elem_size;
+			todo -= desc->elem_size;
+		}
+	}
+	
+out:
+	if (ppages)
+		kunmap(*ppages);
+	return err;
+}
+
+int
+xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
+		  struct xdr_array2_desc *desc)
+{
+	if (base >= buf->len)
+		return -EINVAL;
+
+	return xdr_xcode_array2(buf, base, desc, 0);
+}
+
+int
+xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
+		  struct xdr_array2_desc *desc)
+{
+	if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
+	    buf->head->iov_len + buf->page_len + buf->tail->iov_len)
+		return -EINVAL;
+
+	return xdr_xcode_array2(buf, base, desc, 1);
+}

--
Andreas Gruenbacher <agruen@suse.de>
SUSE Labs, SUSE LINUX PRODUCTS GMBH


  parent reply	other threads:[~2005-01-22 20:49 UTC|newest]

Thread overview: 85+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-22 20:34 [patch 0/13] NFSACL protocol extension for NFSv3 Andreas Gruenbacher
2005-01-22 20:34 ` [patch 1/13] Qsort Andreas Gruenbacher
2005-01-22 21:00   ` vlobanov
2005-01-23  2:03     ` Felipe Alfaro Solana
2005-01-23  2:39       ` Andi Kleen
2005-01-23  3:02         ` Jesper Juhl
2005-01-23  4:46           ` Andi Kleen
2005-01-23  5:05             ` Jesper Juhl
2005-01-23 10:37               ` Rafael J. Wysocki
2005-01-24  4:29                 ` Horst von Brand
2005-01-24 15:45               ` Alan Cox
2005-01-24 17:10               ` H. Peter Anvin
2005-01-25  0:43                 ` Horst von Brand
2005-01-25  4:06                   ` Eric St-Laurent
2005-01-24 22:04             ` Mike Waychison
2005-01-25  6:51               ` Andi Kleen
2005-01-25 10:12                 ` Andreas Gruenbacher
2005-01-25 12:00                   ` Andi Kleen
2005-01-25 12:05                     ` Olaf Kirch
2005-01-25 16:52                       ` Trond Myklebust
2005-01-25 16:53                         ` Andreas Gruenbacher
2005-01-25 17:03                           ` Trond Myklebust
2005-01-25 17:16                             ` Andreas Gruenbacher
2005-01-25 17:37                               ` Trond Myklebust
2005-01-25 18:12                                 ` Andreas Gruenbacher
2005-01-25 19:33                                   ` Trond Myklebust
2005-01-25 19:49                                     ` Andreas Gruenbacher
2005-01-23  4:29         ` Matt Mackall
2005-01-24  0:21           ` Nathan Scott
2005-01-24  2:57             ` Matt Mackall
2005-01-24  4:02           ` Horst von Brand
2005-01-24 21:57             ` Matt Mackall
2005-01-23  4:58         ` Felipe Alfaro Solana
2005-01-24 21:20           ` Matt Mackall
2005-01-24 21:50             ` vlobanov
2005-01-23  4:22       ` Matt Mackall
2005-01-23  5:44       ` Willy Tarreau
2005-01-23 21:24     ` Richard Henderson
     [not found]   ` <1106431568.4153.154.camel@laptopd505.fenrus.org>
2005-01-22 22:10     ` Andreas Gruenbacher
2005-01-22 23:28   ` Matt Mackall
2005-01-23  0:21     ` Matt Mackall
2005-01-23  5:08     ` Andreas Gruenbacher
2005-01-23  5:32       ` Matt Mackall
2005-01-23 12:22         ` Andreas Gruenbacher
2005-01-23 16:49           ` Matt Mackall
2005-01-24  3:48   ` Horst von Brand
2005-01-24 20:15   ` [PATCH] lib/qsort Matt Mackall
2005-01-24 23:09     ` Andrew Morton
2005-01-24 23:30       ` Matt Mackall
2005-01-25  4:11     ` Matt Mackall
2005-01-22 20:34 ` [patch 2/13] Return -ENOSYS for RPC programs that are unavailable Andreas Gruenbacher
2005-02-15 17:04   ` Trond Myklebust
2005-02-16 15:32     ` Andreas Gruenbacher
2005-01-22 20:34 ` [patch 3/13] Add missing -EOPNOTSUPP => NFS3ERR_NOTSUPP mapping in nfsd Andreas Gruenbacher
2005-01-22 20:34 ` [patch 4/13] Allow multiple programs to listen on the same port Andreas Gruenbacher
2005-01-22 20:34 ` [patch 5/13] Allow multiple programs to share the same transport Andreas Gruenbacher
2005-01-22 20:34 ` [patch 6/13] Lazy RPC receive buffer allocation Andreas Gruenbacher
2005-01-22 20:34 ` Andreas Gruenbacher [this message]
2005-02-15 19:17   ` [patch 7/13] Encode and decode arbitrary XDR arrays Trond Myklebust
2005-02-16 16:08     ` Andreas Gruenbacher
2005-02-17 14:12     ` Adrian Bunk
2005-01-22 20:34 ` [patch 8/13] Add noacl nfs mount option Andreas Gruenbacher
2005-02-15 17:24   ` Trond Myklebust
2005-02-16 16:10     ` Andreas Gruenbacher
2005-01-22 20:34 ` [patch 9/13] Infrastructure and server side of nfsacl Andreas Gruenbacher
2005-01-22 20:34 ` [patch 10/13] Solaris nfsacl workaround Andreas Gruenbacher
2005-02-15 17:29   ` Trond Myklebust
2005-02-15 20:35     ` Olivier Galibert
2005-02-15 22:43       ` Trond Myklebust
2005-02-15 23:02         ` Olivier Galibert
2005-02-15 23:37           ` Trond Myklebust
2005-02-15 23:43             ` Olivier Galibert
2005-02-16 16:17     ` Andreas Gruenbacher
2005-02-16 17:05       ` Trond Myklebust
2005-02-16 17:39         ` Andreas Gruenbacher
2005-01-22 20:34 ` [patch 11/13] Client side of nfsacl Andreas Gruenbacher
2005-02-15 17:49   ` Trond Myklebust
2005-02-22 13:41     ` Andreas Gruenbacher
2005-02-22 14:13       ` Trond Myklebust
2005-01-22 20:34 ` [patch 12/13] ACL umask handling workaround in nfs client Andreas Gruenbacher
2005-01-25  1:20   ` Andreas Gruenbacher
2005-02-15 18:04   ` Trond Myklebust
2005-02-22 16:47     ` Andreas Gruenbacher
2005-02-22 17:43       ` Trond Myklebust
2005-01-22 20:34 ` [patch 13/13] Cache acls on the nfs client side Andreas Gruenbacher

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050122203619.570180000@blunzn.suse.de \
    --to=agruen@suse.de \
    --cc=Andries.Brouwer@cwi.nl \
    --cc=akpm@osdl.org \
    --cc=buchk@pobox.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=neilb@cse.unsw.edu.au \
    --cc=okir@suse.de \
    --cc=trond.myklebust@fys.uio.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®