blob: 3bbef7f5f8260179de529471cadb1a970e0d0fb0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/xdr.c
3 *
4 * Generic XDR support.
5 *
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 */
8
Chuck Levera246b012005-08-11 16:25:23 -04009#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/string.h>
13#include <linux/kernel.h>
14#include <linux/pagemap.h>
15#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/sunrpc/xdr.h>
17#include <linux/sunrpc/msg_prot.h>
18
19/*
20 * XDR functions for basic NFS types
21 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070022__be32 *
23xdr_encode_netobj(__be32 *p, const struct xdr_netobj *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 unsigned int quadlen = XDR_QUADLEN(obj->len);
26
27 p[quadlen] = 0; /* zero trailing bytes */
Benny Halevy9f162d22009-08-14 17:18:44 +030028 *p++ = cpu_to_be32(obj->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 memcpy(p, obj->data, obj->len);
30 return p + XDR_QUADLEN(obj->len);
31}
Trond Myklebust468039e2008-12-23 15:21:31 -050032EXPORT_SYMBOL_GPL(xdr_encode_netobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070034__be32 *
35xdr_decode_netobj(__be32 *p, struct xdr_netobj *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 unsigned int len;
38
Benny Halevy98866b52009-08-14 17:18:49 +030039 if ((len = be32_to_cpu(*p++)) > XDR_MAX_NETOBJ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 return NULL;
41 obj->len = len;
42 obj->data = (u8 *) p;
43 return p + XDR_QUADLEN(len);
44}
Trond Myklebust468039e2008-12-23 15:21:31 -050045EXPORT_SYMBOL_GPL(xdr_decode_netobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/**
48 * xdr_encode_opaque_fixed - Encode fixed length opaque data
Pavel Pisa4dc3b162005-05-01 08:59:25 -070049 * @p: pointer to current position in XDR buffer.
50 * @ptr: pointer to data to encode (or NULL)
51 * @nbytes: size of data.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
53 * Copy the array of data of length nbytes at ptr to the XDR buffer
54 * at position p, then align to the next 32-bit boundary by padding
55 * with zero bytes (see RFC1832).
56 * Note: if ptr is NULL, only the padding is performed.
57 *
58 * Returns the updated current XDR buffer position
59 *
60 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070061__be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 if (likely(nbytes != 0)) {
64 unsigned int quadlen = XDR_QUADLEN(nbytes);
65 unsigned int padding = (quadlen << 2) - nbytes;
66
67 if (ptr != NULL)
68 memcpy(p, ptr, nbytes);
69 if (padding != 0)
70 memset((char *)p + nbytes, 0, padding);
71 p += quadlen;
72 }
73 return p;
74}
Trond Myklebust468039e2008-12-23 15:21:31 -050075EXPORT_SYMBOL_GPL(xdr_encode_opaque_fixed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/**
78 * xdr_encode_opaque - Encode variable length opaque data
Pavel Pisa4dc3b162005-05-01 08:59:25 -070079 * @p: pointer to current position in XDR buffer.
80 * @ptr: pointer to data to encode (or NULL)
81 * @nbytes: size of data.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
83 * Returns the updated current XDR buffer position
84 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070085__be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Benny Halevy9f162d22009-08-14 17:18:44 +030087 *p++ = cpu_to_be32(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return xdr_encode_opaque_fixed(p, ptr, nbytes);
89}
Trond Myklebust468039e2008-12-23 15:21:31 -050090EXPORT_SYMBOL_GPL(xdr_encode_opaque);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070092__be32 *
93xdr_encode_string(__be32 *p, const char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 return xdr_encode_array(p, string, strlen(string));
96}
Trond Myklebust468039e2008-12-23 15:21:31 -050097EXPORT_SYMBOL_GPL(xdr_encode_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070099__be32 *
Chuck Levere5cff482007-11-01 16:56:47 -0400100xdr_decode_string_inplace(__be32 *p, char **sp,
101 unsigned int *lenp, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Chuck Levere5cff482007-11-01 16:56:47 -0400103 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Benny Halevy98866b52009-08-14 17:18:49 +0300105 len = be32_to_cpu(*p++);
Chuck Levere5cff482007-11-01 16:56:47 -0400106 if (len > maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return NULL;
108 *lenp = len;
109 *sp = (char *) p;
110 return p + XDR_QUADLEN(len);
111}
Trond Myklebust468039e2008-12-23 15:21:31 -0500112EXPORT_SYMBOL_GPL(xdr_decode_string_inplace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114void
115xdr_encode_pages(struct xdr_buf *xdr, struct page **pages, unsigned int base,
116 unsigned int len)
117{
118 struct kvec *tail = xdr->tail;
119 u32 *p;
120
121 xdr->pages = pages;
122 xdr->page_base = base;
123 xdr->page_len = len;
124
125 p = (u32 *)xdr->head[0].iov_base + XDR_QUADLEN(xdr->head[0].iov_len);
126 tail->iov_base = p;
127 tail->iov_len = 0;
128
129 if (len & 3) {
130 unsigned int pad = 4 - (len & 3);
131
132 *p = 0;
133 tail->iov_base = (char *)p + (len & 3);
134 tail->iov_len = pad;
135 len += pad;
136 }
137 xdr->buflen += len;
138 xdr->len += len;
139}
Trond Myklebust468039e2008-12-23 15:21:31 -0500140EXPORT_SYMBOL_GPL(xdr_encode_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142void
143xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
144 struct page **pages, unsigned int base, unsigned int len)
145{
146 struct kvec *head = xdr->head;
147 struct kvec *tail = xdr->tail;
148 char *buf = (char *)head->iov_base;
149 unsigned int buflen = head->iov_len;
150
151 head->iov_len = offset;
152
153 xdr->pages = pages;
154 xdr->page_base = base;
155 xdr->page_len = len;
156
157 tail->iov_base = buf + offset;
158 tail->iov_len = buflen - offset;
159
160 xdr->buflen += len;
161}
Trond Myklebust468039e2008-12-23 15:21:31 -0500162EXPORT_SYMBOL_GPL(xdr_inline_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
165 * Helper routines for doing 'memmove' like operations on a struct xdr_buf
166 *
167 * _shift_data_right_pages
168 * @pages: vector of pages containing both the source and dest memory area.
169 * @pgto_base: page vector address of destination
170 * @pgfrom_base: page vector address of source
171 * @len: number of bytes to copy
172 *
173 * Note: the addresses pgto_base and pgfrom_base are both calculated in
174 * the same way:
175 * if a memory area starts at byte 'base' in page 'pages[i]',
176 * then its address is given as (i << PAGE_CACHE_SHIFT) + base
177 * Also note: pgfrom_base must be < pgto_base, but the memory areas
178 * they point to may overlap.
179 */
180static void
181_shift_data_right_pages(struct page **pages, size_t pgto_base,
182 size_t pgfrom_base, size_t len)
183{
184 struct page **pgfrom, **pgto;
185 char *vfrom, *vto;
186 size_t copy;
187
188 BUG_ON(pgto_base <= pgfrom_base);
189
190 pgto_base += len;
191 pgfrom_base += len;
192
193 pgto = pages + (pgto_base >> PAGE_CACHE_SHIFT);
194 pgfrom = pages + (pgfrom_base >> PAGE_CACHE_SHIFT);
195
196 pgto_base &= ~PAGE_CACHE_MASK;
197 pgfrom_base &= ~PAGE_CACHE_MASK;
198
199 do {
200 /* Are any pointers crossing a page boundary? */
201 if (pgto_base == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 pgto_base = PAGE_CACHE_SIZE;
203 pgto--;
204 }
205 if (pgfrom_base == 0) {
206 pgfrom_base = PAGE_CACHE_SIZE;
207 pgfrom--;
208 }
209
210 copy = len;
211 if (copy > pgto_base)
212 copy = pgto_base;
213 if (copy > pgfrom_base)
214 copy = pgfrom_base;
215 pgto_base -= copy;
216 pgfrom_base -= copy;
217
218 vto = kmap_atomic(*pgto, KM_USER0);
219 vfrom = kmap_atomic(*pgfrom, KM_USER1);
220 memmove(vto + pgto_base, vfrom + pgfrom_base, copy);
Trond Myklebustbce34812006-07-05 13:17:12 -0400221 flush_dcache_page(*pgto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 kunmap_atomic(vfrom, KM_USER1);
223 kunmap_atomic(vto, KM_USER0);
224
225 } while ((len -= copy) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228/*
229 * _copy_to_pages
230 * @pages: array of pages
231 * @pgbase: page vector address of destination
232 * @p: pointer to source data
233 * @len: length
234 *
235 * Copies data from an arbitrary memory location into an array of pages
236 * The copy is assumed to be non-overlapping.
237 */
238static void
239_copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
240{
241 struct page **pgto;
242 char *vto;
243 size_t copy;
244
245 pgto = pages + (pgbase >> PAGE_CACHE_SHIFT);
246 pgbase &= ~PAGE_CACHE_MASK;
247
Trond Myklebustdaeba892008-03-31 17:02:02 -0400248 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 copy = PAGE_CACHE_SIZE - pgbase;
250 if (copy > len)
251 copy = len;
252
253 vto = kmap_atomic(*pgto, KM_USER0);
254 memcpy(vto + pgbase, p, copy);
255 kunmap_atomic(vto, KM_USER0);
256
Trond Myklebustdaeba892008-03-31 17:02:02 -0400257 len -= copy;
258 if (len == 0)
259 break;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 pgbase += copy;
262 if (pgbase == PAGE_CACHE_SIZE) {
263 flush_dcache_page(*pgto);
264 pgbase = 0;
265 pgto++;
266 }
267 p += copy;
Trond Myklebustdaeba892008-03-31 17:02:02 -0400268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 flush_dcache_page(*pgto);
270}
271
272/*
273 * _copy_from_pages
274 * @p: pointer to destination
275 * @pages: array of pages
276 * @pgbase: offset of source data
277 * @len: length
278 *
279 * Copies data into an arbitrary memory location from an array of pages
280 * The copy is assumed to be non-overlapping.
281 */
282static void
283_copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
284{
285 struct page **pgfrom;
286 char *vfrom;
287 size_t copy;
288
289 pgfrom = pages + (pgbase >> PAGE_CACHE_SHIFT);
290 pgbase &= ~PAGE_CACHE_MASK;
291
292 do {
293 copy = PAGE_CACHE_SIZE - pgbase;
294 if (copy > len)
295 copy = len;
296
297 vfrom = kmap_atomic(*pgfrom, KM_USER0);
298 memcpy(p, vfrom + pgbase, copy);
299 kunmap_atomic(vfrom, KM_USER0);
300
301 pgbase += copy;
302 if (pgbase == PAGE_CACHE_SIZE) {
303 pgbase = 0;
304 pgfrom++;
305 }
306 p += copy;
307
308 } while ((len -= copy) != 0);
309}
310
311/*
312 * xdr_shrink_bufhead
313 * @buf: xdr_buf
314 * @len: bytes to remove from buf->head[0]
315 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800316 * Shrinks XDR buffer's header kvec buf->head[0] by
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 * 'len' bytes. The extra data is not lost, but is instead
318 * moved into the inlined pages and/or the tail.
319 */
320static void
321xdr_shrink_bufhead(struct xdr_buf *buf, size_t len)
322{
323 struct kvec *head, *tail;
324 size_t copy, offs;
325 unsigned int pglen = buf->page_len;
326
327 tail = buf->tail;
328 head = buf->head;
329 BUG_ON (len > head->iov_len);
330
331 /* Shift the tail first */
332 if (tail->iov_len != 0) {
333 if (tail->iov_len > len) {
334 copy = tail->iov_len - len;
335 memmove((char *)tail->iov_base + len,
336 tail->iov_base, copy);
337 }
338 /* Copy from the inlined pages into the tail */
339 copy = len;
340 if (copy > pglen)
341 copy = pglen;
342 offs = len - copy;
343 if (offs >= tail->iov_len)
344 copy = 0;
345 else if (copy > tail->iov_len - offs)
346 copy = tail->iov_len - offs;
347 if (copy != 0)
348 _copy_from_pages((char *)tail->iov_base + offs,
349 buf->pages,
350 buf->page_base + pglen + offs - len,
351 copy);
352 /* Do we also need to copy data from the head into the tail ? */
353 if (len > pglen) {
354 offs = copy = len - pglen;
355 if (copy > tail->iov_len)
356 copy = tail->iov_len;
357 memcpy(tail->iov_base,
358 (char *)head->iov_base +
359 head->iov_len - offs,
360 copy);
361 }
362 }
363 /* Now handle pages */
364 if (pglen != 0) {
365 if (pglen > len)
366 _shift_data_right_pages(buf->pages,
367 buf->page_base + len,
368 buf->page_base,
369 pglen - len);
370 copy = len;
371 if (len > pglen)
372 copy = pglen;
373 _copy_to_pages(buf->pages, buf->page_base,
374 (char *)head->iov_base + head->iov_len - len,
375 copy);
376 }
377 head->iov_len -= len;
378 buf->buflen -= len;
379 /* Have we truncated the message? */
380 if (buf->len > buf->buflen)
381 buf->len = buf->buflen;
382}
383
384/*
385 * xdr_shrink_pagelen
386 * @buf: xdr_buf
387 * @len: bytes to remove from buf->pages
388 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800389 * Shrinks XDR buffer's page array buf->pages by
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 * 'len' bytes. The extra data is not lost, but is instead
391 * moved into the tail.
392 */
393static void
394xdr_shrink_pagelen(struct xdr_buf *buf, size_t len)
395{
396 struct kvec *tail;
397 size_t copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 unsigned int pglen = buf->page_len;
Trond Myklebustcf187c22010-08-29 12:13:16 -0400399 unsigned int tailbuf_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 tail = buf->tail;
402 BUG_ON (len > pglen);
403
Trond Myklebustcf187c22010-08-29 12:13:16 -0400404 tailbuf_len = buf->buflen - buf->head->iov_len - buf->page_len;
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /* Shift the tail first */
Trond Myklebustcf187c22010-08-29 12:13:16 -0400407 if (tailbuf_len != 0) {
408 unsigned int free_space = tailbuf_len - tail->iov_len;
409
410 if (len < free_space)
411 free_space = len;
412 tail->iov_len += free_space;
413
Benny Halevy42d6d8a2010-08-29 12:13:15 -0400414 copy = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (tail->iov_len > len) {
Benny Halevy0fe62a352010-08-29 12:13:15 -0400416 char *p = (char *)tail->iov_base + len;
Benny Halevy2e29ebb2010-08-29 12:13:15 -0400417 memmove(p, tail->iov_base, tail->iov_len - len);
Benny Halevy42d6d8a2010-08-29 12:13:15 -0400418 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 copy = tail->iov_len;
Benny Halevy42d6d8a2010-08-29 12:13:15 -0400420 /* Copy from the inlined pages into the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 _copy_from_pages((char *)tail->iov_base,
422 buf->pages, buf->page_base + pglen - len,
423 copy);
424 }
425 buf->page_len -= len;
426 buf->buflen -= len;
427 /* Have we truncated the message? */
428 if (buf->len > buf->buflen)
429 buf->len = buf->buflen;
430}
431
432void
433xdr_shift_buf(struct xdr_buf *buf, size_t len)
434{
435 xdr_shrink_bufhead(buf, len);
436}
Trond Myklebust468039e2008-12-23 15:21:31 -0500437EXPORT_SYMBOL_GPL(xdr_shift_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439/**
440 * xdr_init_encode - Initialize a struct xdr_stream for sending data.
441 * @xdr: pointer to xdr_stream struct
442 * @buf: pointer to XDR buffer in which to encode data
443 * @p: current pointer inside XDR buffer
444 *
445 * Note: at the moment the RPC client only passes the length of our
446 * scratch buffer in the xdr_buf's header kvec. Previously this
447 * meant we needed to call xdr_adjust_iovec() after encoding the
448 * data. With the new scheme, the xdr_stream manages the details
449 * of the buffer length, and takes care of adjusting the kvec
450 * length for us.
451 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700452void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct kvec *iov = buf->head;
Trond Myklebust334ccfd2005-06-22 17:16:19 +0000455 int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Trond Myklebust334ccfd2005-06-22 17:16:19 +0000457 BUG_ON(scratch_len < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 xdr->buf = buf;
459 xdr->iov = iov;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700460 xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
461 xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
Trond Myklebust334ccfd2005-06-22 17:16:19 +0000462 BUG_ON(iov->iov_len > scratch_len);
463
464 if (p != xdr->p && p != NULL) {
465 size_t len;
466
467 BUG_ON(p < xdr->p || p > xdr->end);
468 len = (char *)p - (char *)xdr->p;
469 xdr->p = p;
470 buf->len += len;
471 iov->iov_len += len;
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
Trond Myklebust468039e2008-12-23 15:21:31 -0500474EXPORT_SYMBOL_GPL(xdr_init_encode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476/**
477 * xdr_reserve_space - Reserve buffer space for sending
478 * @xdr: pointer to xdr_stream
479 * @nbytes: number of bytes to reserve
480 *
481 * Checks that we have enough buffer space to encode 'nbytes' more
482 * bytes of data. If so, update the total xdr_buf length, and
483 * adjust the length of the current kvec.
484 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700485__be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700487 __be32 *p = xdr->p;
488 __be32 *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* align nbytes on the next 32-bit boundary */
491 nbytes += 3;
492 nbytes &= ~3;
493 q = p + (nbytes >> 2);
494 if (unlikely(q > xdr->end || q < p))
495 return NULL;
496 xdr->p = q;
497 xdr->iov->iov_len += nbytes;
498 xdr->buf->len += nbytes;
499 return p;
500}
Trond Myklebust468039e2008-12-23 15:21:31 -0500501EXPORT_SYMBOL_GPL(xdr_reserve_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503/**
504 * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
505 * @xdr: pointer to xdr_stream
506 * @pages: list of pages
507 * @base: offset of first byte
508 * @len: length of data in bytes
509 *
510 */
511void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
512 unsigned int len)
513{
514 struct xdr_buf *buf = xdr->buf;
515 struct kvec *iov = buf->tail;
516 buf->pages = pages;
517 buf->page_base = base;
518 buf->page_len = len;
519
520 iov->iov_base = (char *)xdr->p;
521 iov->iov_len = 0;
522 xdr->iov = iov;
523
524 if (len & 3) {
525 unsigned int pad = 4 - (len & 3);
526
527 BUG_ON(xdr->p >= xdr->end);
528 iov->iov_base = (char *)xdr->p + (len & 3);
529 iov->iov_len += pad;
530 len += pad;
531 *xdr->p++ = 0;
532 }
533 buf->buflen += len;
534 buf->len += len;
535}
Trond Myklebust468039e2008-12-23 15:21:31 -0500536EXPORT_SYMBOL_GPL(xdr_write_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538/**
539 * xdr_init_decode - Initialize an xdr_stream for decoding data.
540 * @xdr: pointer to xdr_stream struct
541 * @buf: pointer to XDR buffer from which to decode data
542 * @p: current pointer inside XDR buffer
543 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700544void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct kvec *iov = buf->head;
547 unsigned int len = iov->iov_len;
548
549 if (len > buf->len)
550 len = buf->len;
551 xdr->buf = buf;
552 xdr->iov = iov;
553 xdr->p = p;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700554 xdr->end = (__be32 *)((char *)iov->iov_base + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
Trond Myklebust468039e2008-12-23 15:21:31 -0500556EXPORT_SYMBOL_GPL(xdr_init_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558/**
559 * xdr_inline_decode - Retrieve non-page XDR data to decode
560 * @xdr: pointer to xdr_stream struct
561 * @nbytes: number of bytes of data to decode
562 *
563 * Check if the input buffer is long enough to enable us to decode
564 * 'nbytes' more bytes of data starting at the current position.
565 * If so return the current pointer, then update the current
566 * pointer position.
567 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700568__be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700570 __be32 *p = xdr->p;
571 __be32 *q = p + XDR_QUADLEN(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (unlikely(q > xdr->end || q < p))
574 return NULL;
575 xdr->p = q;
576 return p;
577}
Trond Myklebust468039e2008-12-23 15:21:31 -0500578EXPORT_SYMBOL_GPL(xdr_inline_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580/**
581 * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
582 * @xdr: pointer to xdr_stream struct
583 * @len: number of bytes of page data
584 *
585 * Moves data beyond the current pointer position from the XDR head[] buffer
586 * into the page list. Any data that lies beyond current position + "len"
Trond Myklebust8b23ea72006-06-09 09:34:21 -0400587 * bytes is moved into the XDR tail[].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
589void xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
590{
591 struct xdr_buf *buf = xdr->buf;
592 struct kvec *iov;
593 ssize_t shift;
594 unsigned int end;
595 int padding;
596
597 /* Realign pages to current pointer position */
598 iov = buf->head;
599 shift = iov->iov_len + (char *)iov->iov_base - (char *)xdr->p;
600 if (shift > 0)
601 xdr_shrink_bufhead(buf, shift);
602
603 /* Truncate page data and move it into the tail */
604 if (buf->page_len > len)
605 xdr_shrink_pagelen(buf, buf->page_len - len);
606 padding = (XDR_QUADLEN(len) << 2) - len;
607 xdr->iov = iov = buf->tail;
608 /* Compute remaining message length. */
609 end = iov->iov_len;
610 shift = buf->buflen - buf->len;
611 if (shift < end)
612 end -= shift;
613 else if (shift > 0)
614 end = 0;
615 /*
616 * Position current pointer at beginning of tail, and
617 * set remaining message length.
618 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700619 xdr->p = (__be32 *)((char *)iov->iov_base + padding);
620 xdr->end = (__be32 *)((char *)iov->iov_base + end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
Trond Myklebust468039e2008-12-23 15:21:31 -0500622EXPORT_SYMBOL_GPL(xdr_read_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Trond Myklebust8b23ea72006-06-09 09:34:21 -0400624/**
625 * xdr_enter_page - decode data from the XDR page
626 * @xdr: pointer to xdr_stream struct
627 * @len: number of bytes of page data
628 *
629 * Moves data beyond the current pointer position from the XDR head[] buffer
630 * into the page list. Any data that lies beyond current position + "len"
631 * bytes is moved into the XDR tail[]. The current pointer is then
632 * repositioned at the beginning of the first XDR page.
633 */
634void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
635{
636 char * kaddr = page_address(xdr->buf->pages[0]);
637 xdr_read_pages(xdr, len);
638 /*
639 * Position current pointer at beginning of tail, and
640 * set remaining message length.
641 */
642 if (len > PAGE_CACHE_SIZE - xdr->buf->page_base)
643 len = PAGE_CACHE_SIZE - xdr->buf->page_base;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700644 xdr->p = (__be32 *)(kaddr + xdr->buf->page_base);
645 xdr->end = (__be32 *)((char *)xdr->p + len);
Trond Myklebust8b23ea72006-06-09 09:34:21 -0400646}
Trond Myklebust468039e2008-12-23 15:21:31 -0500647EXPORT_SYMBOL_GPL(xdr_enter_page);
Trond Myklebust8b23ea72006-06-09 09:34:21 -0400648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
650
651void
652xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
653{
654 buf->head[0] = *iov;
655 buf->tail[0] = empty_iov;
656 buf->page_len = 0;
657 buf->buflen = buf->len = iov->iov_len;
658}
Trond Myklebust468039e2008-12-23 15:21:31 -0500659EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661/* Sets subbuf to the portion of buf of length len beginning base bytes
662 * from the start of buf. Returns -1 if base of length are out of bounds. */
663int
664xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
Trond Myklebust1e789572006-08-31 15:09:19 -0400665 unsigned int base, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 subbuf->buflen = subbuf->len = len;
Trond Myklebust1e789572006-08-31 15:09:19 -0400668 if (base < buf->head[0].iov_len) {
669 subbuf->head[0].iov_base = buf->head[0].iov_base + base;
670 subbuf->head[0].iov_len = min_t(unsigned int, len,
671 buf->head[0].iov_len - base);
672 len -= subbuf->head[0].iov_len;
673 base = 0;
674 } else {
675 subbuf->head[0].iov_base = NULL;
676 subbuf->head[0].iov_len = 0;
677 base -= buf->head[0].iov_len;
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 if (base < buf->page_len) {
Trond Myklebust1e789572006-08-31 15:09:19 -0400681 subbuf->page_len = min(buf->page_len - base, len);
682 base += buf->page_base;
683 subbuf->page_base = base & ~PAGE_CACHE_MASK;
684 subbuf->pages = &buf->pages[base >> PAGE_CACHE_SHIFT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 len -= subbuf->page_len;
686 base = 0;
687 } else {
688 base -= buf->page_len;
689 subbuf->page_len = 0;
690 }
691
Trond Myklebust1e789572006-08-31 15:09:19 -0400692 if (base < buf->tail[0].iov_len) {
693 subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
694 subbuf->tail[0].iov_len = min_t(unsigned int, len,
695 buf->tail[0].iov_len - base);
696 len -= subbuf->tail[0].iov_len;
697 base = 0;
698 } else {
699 subbuf->tail[0].iov_base = NULL;
700 subbuf->tail[0].iov_len = 0;
701 base -= buf->tail[0].iov_len;
702 }
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (base || len)
705 return -1;
706 return 0;
707}
Trond Myklebust468039e2008-12-23 15:21:31 -0500708EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Trond Myklebust4e3e43a2006-10-17 13:47:24 -0400710static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Trond Myklebust1e789572006-08-31 15:09:19 -0400712 unsigned int this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Trond Myklebust4e3e43a2006-10-17 13:47:24 -0400714 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
715 memcpy(obj, subbuf->head[0].iov_base, this_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 len -= this_len;
717 obj += this_len;
Trond Myklebust4e3e43a2006-10-17 13:47:24 -0400718 this_len = min_t(unsigned int, len, subbuf->page_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (this_len)
Trond Myklebust4e3e43a2006-10-17 13:47:24 -0400720 _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 len -= this_len;
722 obj += this_len;
Trond Myklebust4e3e43a2006-10-17 13:47:24 -0400723 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
724 memcpy(obj, subbuf->tail[0].iov_base, this_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000727/* obj is assumed to point to allocated memory of size at least len: */
Trond Myklebust4e3e43a2006-10-17 13:47:24 -0400728int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000729{
730 struct xdr_buf subbuf;
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000731 int status;
732
733 status = xdr_buf_subsegment(buf, &subbuf, base, len);
Trond Myklebust4e3e43a2006-10-17 13:47:24 -0400734 if (status != 0)
735 return status;
736 __read_bytes_from_xdr_buf(&subbuf, obj, len);
737 return 0;
738}
Trond Myklebust468039e2008-12-23 15:21:31 -0500739EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
Trond Myklebust4e3e43a2006-10-17 13:47:24 -0400740
741static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
742{
743 unsigned int this_len;
744
745 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
746 memcpy(subbuf->head[0].iov_base, obj, this_len);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000747 len -= this_len;
748 obj += this_len;
Trond Myklebust4e3e43a2006-10-17 13:47:24 -0400749 this_len = min_t(unsigned int, len, subbuf->page_len);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000750 if (this_len)
Trond Myklebust4e3e43a2006-10-17 13:47:24 -0400751 _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000752 len -= this_len;
753 obj += this_len;
Trond Myklebust4e3e43a2006-10-17 13:47:24 -0400754 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
755 memcpy(subbuf->tail[0].iov_base, obj, this_len);
756}
757
758/* obj is assumed to point to allocated memory of size at least len: */
759int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
760{
761 struct xdr_buf subbuf;
762 int status;
763
764 status = xdr_buf_subsegment(buf, &subbuf, base, len);
765 if (status != 0)
766 return status;
767 __write_bytes_to_xdr_buf(&subbuf, obj, len);
768 return 0;
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000769}
Kevin Coffmanc43abae2010-03-17 13:02:58 -0400770EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000771
772int
Trond Myklebust1e789572006-08-31 15:09:19 -0400773xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700775 __be32 raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int status;
777
778 status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
779 if (status)
780 return status;
Benny Halevy98866b52009-08-14 17:18:49 +0300781 *obj = be32_to_cpu(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
783}
Trond Myklebust468039e2008-12-23 15:21:31 -0500784EXPORT_SYMBOL_GPL(xdr_decode_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000786int
Trond Myklebust1e789572006-08-31 15:09:19 -0400787xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000788{
Benny Halevy9f162d22009-08-14 17:18:44 +0300789 __be32 raw = cpu_to_be32(obj);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000790
791 return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
792}
Trond Myklebust468039e2008-12-23 15:21:31 -0500793EXPORT_SYMBOL_GPL(xdr_encode_word);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795/* If the netobj starting offset bytes from the start of xdr_buf is contained
796 * entirely in the head or the tail, set object to point to it; otherwise
797 * try to find space for it at the end of the tail, copy it there, and
798 * set obj to point to it. */
Trond Myklebustbee57c92006-10-09 22:08:22 -0400799int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Trond Myklebustbee57c92006-10-09 22:08:22 -0400801 struct xdr_buf subbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000803 if (xdr_decode_word(buf, offset, &obj->len))
Trond Myklebustbee57c92006-10-09 22:08:22 -0400804 return -EFAULT;
805 if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
806 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Trond Myklebustbee57c92006-10-09 22:08:22 -0400808 /* Is the obj contained entirely in the head? */
809 obj->data = subbuf.head[0].iov_base;
810 if (subbuf.head[0].iov_len == obj->len)
811 return 0;
812 /* ..or is the obj contained entirely in the tail? */
813 obj->data = subbuf.tail[0].iov_base;
814 if (subbuf.tail[0].iov_len == obj->len)
815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Trond Myklebustbee57c92006-10-09 22:08:22 -0400817 /* use end of tail as storage for obj:
818 * (We don't copy to the beginning because then we'd have
819 * to worry about doing a potentially overlapping copy.
820 * This assumes the object is at most half the length of the
821 * tail.) */
822 if (obj->len > buf->buflen - buf->len)
823 return -ENOMEM;
824 if (buf->tail[0].iov_len != 0)
825 obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
826 else
827 obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
828 __read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
Trond Myklebust468039e2008-12-23 15:21:31 -0500831EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000832
833/* Returns 0 on success, or else a negative error code. */
834static int
835xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
836 struct xdr_array2_desc *desc, int encode)
837{
838 char *elem = NULL, *c;
839 unsigned int copied = 0, todo, avail_here;
840 struct page **ppages = NULL;
841 int err;
842
843 if (encode) {
844 if (xdr_encode_word(buf, base, desc->array_len) != 0)
845 return -EINVAL;
846 } else {
847 if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
Trond Myklebust58fcb8d2005-08-10 18:15:12 -0400848 desc->array_len > desc->array_maxlen ||
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000849 (unsigned long) base + 4 + desc->array_len *
850 desc->elem_size > buf->len)
851 return -EINVAL;
852 }
853 base += 4;
854
855 if (!desc->xcode)
856 return 0;
857
858 todo = desc->array_len * desc->elem_size;
859
860 /* process head */
861 if (todo && base < buf->head->iov_len) {
862 c = buf->head->iov_base + base;
863 avail_here = min_t(unsigned int, todo,
864 buf->head->iov_len - base);
865 todo -= avail_here;
866
867 while (avail_here >= desc->elem_size) {
868 err = desc->xcode(desc, c);
869 if (err)
870 goto out;
871 c += desc->elem_size;
872 avail_here -= desc->elem_size;
873 }
874 if (avail_here) {
875 if (!elem) {
876 elem = kmalloc(desc->elem_size, GFP_KERNEL);
877 err = -ENOMEM;
878 if (!elem)
879 goto out;
880 }
881 if (encode) {
882 err = desc->xcode(desc, elem);
883 if (err)
884 goto out;
885 memcpy(c, elem, avail_here);
886 } else
887 memcpy(elem, c, avail_here);
888 copied = avail_here;
889 }
890 base = buf->head->iov_len; /* align to start of pages */
891 }
892
893 /* process pages array */
894 base -= buf->head->iov_len;
895 if (todo && base < buf->page_len) {
896 unsigned int avail_page;
897
898 avail_here = min(todo, buf->page_len - base);
899 todo -= avail_here;
900
901 base += buf->page_base;
902 ppages = buf->pages + (base >> PAGE_CACHE_SHIFT);
903 base &= ~PAGE_CACHE_MASK;
904 avail_page = min_t(unsigned int, PAGE_CACHE_SIZE - base,
905 avail_here);
906 c = kmap(*ppages) + base;
907
908 while (avail_here) {
909 avail_here -= avail_page;
910 if (copied || avail_page < desc->elem_size) {
911 unsigned int l = min(avail_page,
912 desc->elem_size - copied);
913 if (!elem) {
914 elem = kmalloc(desc->elem_size,
915 GFP_KERNEL);
916 err = -ENOMEM;
917 if (!elem)
918 goto out;
919 }
920 if (encode) {
921 if (!copied) {
922 err = desc->xcode(desc, elem);
923 if (err)
924 goto out;
925 }
926 memcpy(c, elem + copied, l);
927 copied += l;
928 if (copied == desc->elem_size)
929 copied = 0;
930 } else {
931 memcpy(elem + copied, c, l);
932 copied += l;
933 if (copied == desc->elem_size) {
934 err = desc->xcode(desc, elem);
935 if (err)
936 goto out;
937 copied = 0;
938 }
939 }
940 avail_page -= l;
941 c += l;
942 }
943 while (avail_page >= desc->elem_size) {
944 err = desc->xcode(desc, c);
945 if (err)
946 goto out;
947 c += desc->elem_size;
948 avail_page -= desc->elem_size;
949 }
950 if (avail_page) {
951 unsigned int l = min(avail_page,
952 desc->elem_size - copied);
953 if (!elem) {
954 elem = kmalloc(desc->elem_size,
955 GFP_KERNEL);
956 err = -ENOMEM;
957 if (!elem)
958 goto out;
959 }
960 if (encode) {
961 if (!copied) {
962 err = desc->xcode(desc, elem);
963 if (err)
964 goto out;
965 }
966 memcpy(c, elem + copied, l);
967 copied += l;
968 if (copied == desc->elem_size)
969 copied = 0;
970 } else {
971 memcpy(elem + copied, c, l);
972 copied += l;
973 if (copied == desc->elem_size) {
974 err = desc->xcode(desc, elem);
975 if (err)
976 goto out;
977 copied = 0;
978 }
979 }
980 }
981 if (avail_here) {
982 kunmap(*ppages);
983 ppages++;
984 c = kmap(*ppages);
985 }
986
987 avail_page = min(avail_here,
988 (unsigned int) PAGE_CACHE_SIZE);
989 }
990 base = buf->page_len; /* align to start of tail */
991 }
992
993 /* process tail */
994 base -= buf->page_len;
995 if (todo) {
996 c = buf->tail->iov_base + base;
997 if (copied) {
998 unsigned int l = desc->elem_size - copied;
999
1000 if (encode)
1001 memcpy(c, elem + copied, l);
1002 else {
1003 memcpy(elem + copied, c, l);
1004 err = desc->xcode(desc, elem);
1005 if (err)
1006 goto out;
1007 }
1008 todo -= l;
1009 c += l;
1010 }
1011 while (todo) {
1012 err = desc->xcode(desc, c);
1013 if (err)
1014 goto out;
1015 c += desc->elem_size;
1016 todo -= desc->elem_size;
1017 }
1018 }
1019 err = 0;
1020
1021out:
Jesper Juhla51482b2005-11-08 09:41:34 -08001022 kfree(elem);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001023 if (ppages)
1024 kunmap(*ppages);
1025 return err;
1026}
1027
1028int
1029xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
1030 struct xdr_array2_desc *desc)
1031{
1032 if (base >= buf->len)
1033 return -EINVAL;
1034
1035 return xdr_xcode_array2(buf, base, desc, 0);
1036}
Trond Myklebust468039e2008-12-23 15:21:31 -05001037EXPORT_SYMBOL_GPL(xdr_decode_array2);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +00001038
1039int
1040xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
1041 struct xdr_array2_desc *desc)
1042{
1043 if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
1044 buf->head->iov_len + buf->page_len + buf->tail->iov_len)
1045 return -EINVAL;
1046
1047 return xdr_xcode_array2(buf, base, desc, 1);
1048}
Trond Myklebust468039e2008-12-23 15:21:31 -05001049EXPORT_SYMBOL_GPL(xdr_encode_array2);
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001050
1051int
1052xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001053 int (*actor)(struct scatterlist *, void *), void *data)
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001054{
1055 int i, ret = 0;
1056 unsigned page_len, thislen, page_offset;
1057 struct scatterlist sg[1];
1058
Herbert Xu68e3f5d2007-10-27 00:52:07 -07001059 sg_init_table(sg, 1);
1060
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001061 if (offset >= buf->head[0].iov_len) {
1062 offset -= buf->head[0].iov_len;
1063 } else {
1064 thislen = buf->head[0].iov_len - offset;
1065 if (thislen > len)
1066 thislen = len;
1067 sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
1068 ret = actor(sg, data);
1069 if (ret)
1070 goto out;
1071 offset = 0;
1072 len -= thislen;
1073 }
1074 if (len == 0)
1075 goto out;
1076
1077 if (offset >= buf->page_len) {
1078 offset -= buf->page_len;
1079 } else {
1080 page_len = buf->page_len - offset;
1081 if (page_len > len)
1082 page_len = len;
1083 len -= page_len;
1084 page_offset = (offset + buf->page_base) & (PAGE_CACHE_SIZE - 1);
1085 i = (offset + buf->page_base) >> PAGE_CACHE_SHIFT;
1086 thislen = PAGE_CACHE_SIZE - page_offset;
1087 do {
1088 if (thislen > page_len)
1089 thislen = page_len;
Jens Axboe642f149032007-10-24 11:20:47 +02001090 sg_set_page(sg, buf->pages[i], thislen, page_offset);
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001091 ret = actor(sg, data);
1092 if (ret)
1093 goto out;
1094 page_len -= thislen;
1095 i++;
1096 page_offset = 0;
1097 thislen = PAGE_CACHE_SIZE;
1098 } while (page_len != 0);
1099 offset = 0;
1100 }
1101 if (len == 0)
1102 goto out;
1103 if (offset < buf->tail[0].iov_len) {
1104 thislen = buf->tail[0].iov_len - offset;
1105 if (thislen > len)
1106 thislen = len;
1107 sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
1108 ret = actor(sg, data);
1109 len -= thislen;
1110 }
1111 if (len != 0)
1112 ret = -EINVAL;
1113out:
1114 return ret;
1115}
Trond Myklebust468039e2008-12-23 15:21:31 -05001116EXPORT_SYMBOL_GPL(xdr_process_buf);
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -05001117