2 * linux/net/sunrpc/xdr.c
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/string.h>
12 #include <linux/kernel.h>
13 #include <linux/pagemap.h>
14 #include <linux/errno.h>
15 #include <linux/sunrpc/xdr.h>
16 #include <linux/sunrpc/msg_prot.h>
19 * XDR functions for basic NFS types
22 xdr_encode_netobj(__be32
*p
, const struct xdr_netobj
*obj
)
24 unsigned int quadlen
= XDR_QUADLEN(obj
->len
);
26 p
[quadlen
] = 0; /* zero trailing bytes */
27 *p
++ = htonl(obj
->len
);
28 memcpy(p
, obj
->data
, obj
->len
);
29 return p
+ XDR_QUADLEN(obj
->len
);
33 xdr_decode_netobj(__be32
*p
, struct xdr_netobj
*obj
)
37 if ((len
= ntohl(*p
++)) > XDR_MAX_NETOBJ
)
41 return p
+ XDR_QUADLEN(len
);
45 * xdr_encode_opaque_fixed - Encode fixed length opaque data
46 * @p: pointer to current position in XDR buffer.
47 * @ptr: pointer to data to encode (or NULL)
48 * @nbytes: size of data.
50 * Copy the array of data of length nbytes at ptr to the XDR buffer
51 * at position p, then align to the next 32-bit boundary by padding
52 * with zero bytes (see RFC1832).
53 * Note: if ptr is NULL, only the padding is performed.
55 * Returns the updated current XDR buffer position
58 __be32
*xdr_encode_opaque_fixed(__be32
*p
, const void *ptr
, unsigned int nbytes
)
60 if (likely(nbytes
!= 0)) {
61 unsigned int quadlen
= XDR_QUADLEN(nbytes
);
62 unsigned int padding
= (quadlen
<< 2) - nbytes
;
65 memcpy(p
, ptr
, nbytes
);
67 memset((char *)p
+ nbytes
, 0, padding
);
72 EXPORT_SYMBOL(xdr_encode_opaque_fixed
);
75 * xdr_encode_opaque - Encode variable length opaque data
76 * @p: pointer to current position in XDR buffer.
77 * @ptr: pointer to data to encode (or NULL)
78 * @nbytes: size of data.
80 * Returns the updated current XDR buffer position
82 __be32
*xdr_encode_opaque(__be32
*p
, const void *ptr
, unsigned int nbytes
)
85 return xdr_encode_opaque_fixed(p
, ptr
, nbytes
);
87 EXPORT_SYMBOL(xdr_encode_opaque
);
90 xdr_encode_string(__be32
*p
, const char *string
)
92 return xdr_encode_array(p
, string
, strlen(string
));
96 xdr_decode_string_inplace(__be32
*p
, char **sp
, int *lenp
, int maxlen
)
100 if ((len
= ntohl(*p
++)) > maxlen
)
104 return p
+ XDR_QUADLEN(len
);
108 xdr_encode_pages(struct xdr_buf
*xdr
, struct page
**pages
, unsigned int base
,
111 struct kvec
*tail
= xdr
->tail
;
115 xdr
->page_base
= base
;
118 p
= (u32
*)xdr
->head
[0].iov_base
+ XDR_QUADLEN(xdr
->head
[0].iov_len
);
123 unsigned int pad
= 4 - (len
& 3);
126 tail
->iov_base
= (char *)p
+ (len
& 3);
135 xdr_inline_pages(struct xdr_buf
*xdr
, unsigned int offset
,
136 struct page
**pages
, unsigned int base
, unsigned int len
)
138 struct kvec
*head
= xdr
->head
;
139 struct kvec
*tail
= xdr
->tail
;
140 char *buf
= (char *)head
->iov_base
;
141 unsigned int buflen
= head
->iov_len
;
143 head
->iov_len
= offset
;
146 xdr
->page_base
= base
;
149 tail
->iov_base
= buf
+ offset
;
150 tail
->iov_len
= buflen
- offset
;
157 * Helper routines for doing 'memmove' like operations on a struct xdr_buf
159 * _shift_data_right_pages
160 * @pages: vector of pages containing both the source and dest memory area.
161 * @pgto_base: page vector address of destination
162 * @pgfrom_base: page vector address of source
163 * @len: number of bytes to copy
165 * Note: the addresses pgto_base and pgfrom_base are both calculated in
167 * if a memory area starts at byte 'base' in page 'pages[i]',
168 * then its address is given as (i << PAGE_CACHE_SHIFT) + base
169 * Also note: pgfrom_base must be < pgto_base, but the memory areas
170 * they point to may overlap.
173 _shift_data_right_pages(struct page
**pages
, size_t pgto_base
,
174 size_t pgfrom_base
, size_t len
)
176 struct page
**pgfrom
, **pgto
;
180 BUG_ON(pgto_base
<= pgfrom_base
);
185 pgto
= pages
+ (pgto_base
>> PAGE_CACHE_SHIFT
);
186 pgfrom
= pages
+ (pgfrom_base
>> PAGE_CACHE_SHIFT
);
188 pgto_base
&= ~PAGE_CACHE_MASK
;
189 pgfrom_base
&= ~PAGE_CACHE_MASK
;
192 /* Are any pointers crossing a page boundary? */
193 if (pgto_base
== 0) {
194 pgto_base
= PAGE_CACHE_SIZE
;
197 if (pgfrom_base
== 0) {
198 pgfrom_base
= PAGE_CACHE_SIZE
;
203 if (copy
> pgto_base
)
205 if (copy
> pgfrom_base
)
210 vto
= kmap_atomic(*pgto
, KM_USER0
);
211 vfrom
= kmap_atomic(*pgfrom
, KM_USER1
);
212 memmove(vto
+ pgto_base
, vfrom
+ pgfrom_base
, copy
);
213 flush_dcache_page(*pgto
);
214 kunmap_atomic(vfrom
, KM_USER1
);
215 kunmap_atomic(vto
, KM_USER0
);
217 } while ((len
-= copy
) != 0);
222 * @pages: array of pages
223 * @pgbase: page vector address of destination
224 * @p: pointer to source data
227 * Copies data from an arbitrary memory location into an array of pages
228 * The copy is assumed to be non-overlapping.
231 _copy_to_pages(struct page
**pages
, size_t pgbase
, const char *p
, size_t len
)
237 pgto
= pages
+ (pgbase
>> PAGE_CACHE_SHIFT
);
238 pgbase
&= ~PAGE_CACHE_MASK
;
241 copy
= PAGE_CACHE_SIZE
- pgbase
;
245 vto
= kmap_atomic(*pgto
, KM_USER0
);
246 memcpy(vto
+ pgbase
, p
, copy
);
247 kunmap_atomic(vto
, KM_USER0
);
250 if (pgbase
== PAGE_CACHE_SIZE
) {
251 flush_dcache_page(*pgto
);
257 } while ((len
-= copy
) != 0);
258 flush_dcache_page(*pgto
);
263 * @p: pointer to destination
264 * @pages: array of pages
265 * @pgbase: offset of source data
268 * Copies data into an arbitrary memory location from an array of pages
269 * The copy is assumed to be non-overlapping.
272 _copy_from_pages(char *p
, struct page
**pages
, size_t pgbase
, size_t len
)
274 struct page
**pgfrom
;
278 pgfrom
= pages
+ (pgbase
>> PAGE_CACHE_SHIFT
);
279 pgbase
&= ~PAGE_CACHE_MASK
;
282 copy
= PAGE_CACHE_SIZE
- pgbase
;
286 vfrom
= kmap_atomic(*pgfrom
, KM_USER0
);
287 memcpy(p
, vfrom
+ pgbase
, copy
);
288 kunmap_atomic(vfrom
, KM_USER0
);
291 if (pgbase
== PAGE_CACHE_SIZE
) {
297 } while ((len
-= copy
) != 0);
303 * @len: bytes to remove from buf->head[0]
305 * Shrinks XDR buffer's header kvec buf->head[0] by
306 * 'len' bytes. The extra data is not lost, but is instead
307 * moved into the inlined pages and/or the tail.
310 xdr_shrink_bufhead(struct xdr_buf
*buf
, size_t len
)
312 struct kvec
*head
, *tail
;
314 unsigned int pglen
= buf
->page_len
;
318 BUG_ON (len
> head
->iov_len
);
320 /* Shift the tail first */
321 if (tail
->iov_len
!= 0) {
322 if (tail
->iov_len
> len
) {
323 copy
= tail
->iov_len
- len
;
324 memmove((char *)tail
->iov_base
+ len
,
325 tail
->iov_base
, copy
);
327 /* Copy from the inlined pages into the tail */
332 if (offs
>= tail
->iov_len
)
334 else if (copy
> tail
->iov_len
- offs
)
335 copy
= tail
->iov_len
- offs
;
337 _copy_from_pages((char *)tail
->iov_base
+ offs
,
339 buf
->page_base
+ pglen
+ offs
- len
,
341 /* Do we also need to copy data from the head into the tail ? */
343 offs
= copy
= len
- pglen
;
344 if (copy
> tail
->iov_len
)
345 copy
= tail
->iov_len
;
346 memcpy(tail
->iov_base
,
347 (char *)head
->iov_base
+
348 head
->iov_len
- offs
,
352 /* Now handle pages */
355 _shift_data_right_pages(buf
->pages
,
356 buf
->page_base
+ len
,
362 _copy_to_pages(buf
->pages
, buf
->page_base
,
363 (char *)head
->iov_base
+ head
->iov_len
- len
,
366 head
->iov_len
-= len
;
368 /* Have we truncated the message? */
369 if (buf
->len
> buf
->buflen
)
370 buf
->len
= buf
->buflen
;
376 * @len: bytes to remove from buf->pages
378 * Shrinks XDR buffer's page array buf->pages by
379 * 'len' bytes. The extra data is not lost, but is instead
380 * moved into the tail.
383 xdr_shrink_pagelen(struct xdr_buf
*buf
, size_t len
)
388 unsigned int pglen
= buf
->page_len
;
391 BUG_ON (len
> pglen
);
393 /* Shift the tail first */
394 if (tail
->iov_len
!= 0) {
395 p
= (char *)tail
->iov_base
+ len
;
396 if (tail
->iov_len
> len
) {
397 copy
= tail
->iov_len
- len
;
398 memmove(p
, tail
->iov_base
, copy
);
401 /* Copy from the inlined pages into the tail */
403 if (copy
> tail
->iov_len
)
404 copy
= tail
->iov_len
;
405 _copy_from_pages((char *)tail
->iov_base
,
406 buf
->pages
, buf
->page_base
+ pglen
- len
,
409 buf
->page_len
-= len
;
411 /* Have we truncated the message? */
412 if (buf
->len
> buf
->buflen
)
413 buf
->len
= buf
->buflen
;
417 xdr_shift_buf(struct xdr_buf
*buf
, size_t len
)
419 xdr_shrink_bufhead(buf
, len
);
423 * xdr_init_encode - Initialize a struct xdr_stream for sending data.
424 * @xdr: pointer to xdr_stream struct
425 * @buf: pointer to XDR buffer in which to encode data
426 * @p: current pointer inside XDR buffer
428 * Note: at the moment the RPC client only passes the length of our
429 * scratch buffer in the xdr_buf's header kvec. Previously this
430 * meant we needed to call xdr_adjust_iovec() after encoding the
431 * data. With the new scheme, the xdr_stream manages the details
432 * of the buffer length, and takes care of adjusting the kvec
435 void xdr_init_encode(struct xdr_stream
*xdr
, struct xdr_buf
*buf
, __be32
*p
)
437 struct kvec
*iov
= buf
->head
;
438 int scratch_len
= buf
->buflen
- buf
->page_len
- buf
->tail
[0].iov_len
;
440 BUG_ON(scratch_len
< 0);
443 xdr
->p
= (__be32
*)((char *)iov
->iov_base
+ iov
->iov_len
);
444 xdr
->end
= (__be32
*)((char *)iov
->iov_base
+ scratch_len
);
445 BUG_ON(iov
->iov_len
> scratch_len
);
447 if (p
!= xdr
->p
&& p
!= NULL
) {
450 BUG_ON(p
< xdr
->p
|| p
> xdr
->end
);
451 len
= (char *)p
- (char *)xdr
->p
;
457 EXPORT_SYMBOL(xdr_init_encode
);
460 * xdr_reserve_space - Reserve buffer space for sending
461 * @xdr: pointer to xdr_stream
462 * @nbytes: number of bytes to reserve
464 * Checks that we have enough buffer space to encode 'nbytes' more
465 * bytes of data. If so, update the total xdr_buf length, and
466 * adjust the length of the current kvec.
468 __be32
* xdr_reserve_space(struct xdr_stream
*xdr
, size_t nbytes
)
473 /* align nbytes on the next 32-bit boundary */
476 q
= p
+ (nbytes
>> 2);
477 if (unlikely(q
> xdr
->end
|| q
< p
))
480 xdr
->iov
->iov_len
+= nbytes
;
481 xdr
->buf
->len
+= nbytes
;
484 EXPORT_SYMBOL(xdr_reserve_space
);
487 * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
488 * @xdr: pointer to xdr_stream
489 * @pages: list of pages
490 * @base: offset of first byte
491 * @len: length of data in bytes
494 void xdr_write_pages(struct xdr_stream
*xdr
, struct page
**pages
, unsigned int base
,
497 struct xdr_buf
*buf
= xdr
->buf
;
498 struct kvec
*iov
= buf
->tail
;
500 buf
->page_base
= base
;
503 iov
->iov_base
= (char *)xdr
->p
;
508 unsigned int pad
= 4 - (len
& 3);
510 BUG_ON(xdr
->p
>= xdr
->end
);
511 iov
->iov_base
= (char *)xdr
->p
+ (len
& 3);
519 EXPORT_SYMBOL(xdr_write_pages
);
522 * xdr_init_decode - Initialize an xdr_stream for decoding data.
523 * @xdr: pointer to xdr_stream struct
524 * @buf: pointer to XDR buffer from which to decode data
525 * @p: current pointer inside XDR buffer
527 void xdr_init_decode(struct xdr_stream
*xdr
, struct xdr_buf
*buf
, __be32
*p
)
529 struct kvec
*iov
= buf
->head
;
530 unsigned int len
= iov
->iov_len
;
537 xdr
->end
= (__be32
*)((char *)iov
->iov_base
+ len
);
539 EXPORT_SYMBOL(xdr_init_decode
);
542 * xdr_inline_decode - Retrieve non-page XDR data to decode
543 * @xdr: pointer to xdr_stream struct
544 * @nbytes: number of bytes of data to decode
546 * Check if the input buffer is long enough to enable us to decode
547 * 'nbytes' more bytes of data starting at the current position.
548 * If so return the current pointer, then update the current
551 __be32
* xdr_inline_decode(struct xdr_stream
*xdr
, size_t nbytes
)
554 __be32
*q
= p
+ XDR_QUADLEN(nbytes
);
556 if (unlikely(q
> xdr
->end
|| q
< p
))
561 EXPORT_SYMBOL(xdr_inline_decode
);
564 * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
565 * @xdr: pointer to xdr_stream struct
566 * @len: number of bytes of page data
568 * Moves data beyond the current pointer position from the XDR head[] buffer
569 * into the page list. Any data that lies beyond current position + "len"
570 * bytes is moved into the XDR tail[].
572 void xdr_read_pages(struct xdr_stream
*xdr
, unsigned int len
)
574 struct xdr_buf
*buf
= xdr
->buf
;
580 /* Realign pages to current pointer position */
582 shift
= iov
->iov_len
+ (char *)iov
->iov_base
- (char *)xdr
->p
;
584 xdr_shrink_bufhead(buf
, shift
);
586 /* Truncate page data and move it into the tail */
587 if (buf
->page_len
> len
)
588 xdr_shrink_pagelen(buf
, buf
->page_len
- len
);
589 padding
= (XDR_QUADLEN(len
) << 2) - len
;
590 xdr
->iov
= iov
= buf
->tail
;
591 /* Compute remaining message length. */
593 shift
= buf
->buflen
- buf
->len
;
599 * Position current pointer at beginning of tail, and
600 * set remaining message length.
602 xdr
->p
= (__be32
*)((char *)iov
->iov_base
+ padding
);
603 xdr
->end
= (__be32
*)((char *)iov
->iov_base
+ end
);
605 EXPORT_SYMBOL(xdr_read_pages
);
608 * xdr_enter_page - decode data from the XDR page
609 * @xdr: pointer to xdr_stream struct
610 * @len: number of bytes of page data
612 * Moves data beyond the current pointer position from the XDR head[] buffer
613 * into the page list. Any data that lies beyond current position + "len"
614 * bytes is moved into the XDR tail[]. The current pointer is then
615 * repositioned at the beginning of the first XDR page.
617 void xdr_enter_page(struct xdr_stream
*xdr
, unsigned int len
)
619 char * kaddr
= page_address(xdr
->buf
->pages
[0]);
620 xdr_read_pages(xdr
, len
);
622 * Position current pointer at beginning of tail, and
623 * set remaining message length.
625 if (len
> PAGE_CACHE_SIZE
- xdr
->buf
->page_base
)
626 len
= PAGE_CACHE_SIZE
- xdr
->buf
->page_base
;
627 xdr
->p
= (__be32
*)(kaddr
+ xdr
->buf
->page_base
);
628 xdr
->end
= (__be32
*)((char *)xdr
->p
+ len
);
630 EXPORT_SYMBOL(xdr_enter_page
);
632 static struct kvec empty_iov
= {.iov_base
= NULL
, .iov_len
= 0};
635 xdr_buf_from_iov(struct kvec
*iov
, struct xdr_buf
*buf
)
638 buf
->tail
[0] = empty_iov
;
640 buf
->buflen
= buf
->len
= iov
->iov_len
;
643 /* Sets subbuf to the portion of buf of length len beginning base bytes
644 * from the start of buf. Returns -1 if base of length are out of bounds. */
646 xdr_buf_subsegment(struct xdr_buf
*buf
, struct xdr_buf
*subbuf
,
647 unsigned int base
, unsigned int len
)
649 subbuf
->buflen
= subbuf
->len
= len
;
650 if (base
< buf
->head
[0].iov_len
) {
651 subbuf
->head
[0].iov_base
= buf
->head
[0].iov_base
+ base
;
652 subbuf
->head
[0].iov_len
= min_t(unsigned int, len
,
653 buf
->head
[0].iov_len
- base
);
654 len
-= subbuf
->head
[0].iov_len
;
657 subbuf
->head
[0].iov_base
= NULL
;
658 subbuf
->head
[0].iov_len
= 0;
659 base
-= buf
->head
[0].iov_len
;
662 if (base
< buf
->page_len
) {
663 subbuf
->page_len
= min(buf
->page_len
- base
, len
);
664 base
+= buf
->page_base
;
665 subbuf
->page_base
= base
& ~PAGE_CACHE_MASK
;
666 subbuf
->pages
= &buf
->pages
[base
>> PAGE_CACHE_SHIFT
];
667 len
-= subbuf
->page_len
;
670 base
-= buf
->page_len
;
671 subbuf
->page_len
= 0;
674 if (base
< buf
->tail
[0].iov_len
) {
675 subbuf
->tail
[0].iov_base
= buf
->tail
[0].iov_base
+ base
;
676 subbuf
->tail
[0].iov_len
= min_t(unsigned int, len
,
677 buf
->tail
[0].iov_len
- base
);
678 len
-= subbuf
->tail
[0].iov_len
;
681 subbuf
->tail
[0].iov_base
= NULL
;
682 subbuf
->tail
[0].iov_len
= 0;
683 base
-= buf
->tail
[0].iov_len
;
691 static void __read_bytes_from_xdr_buf(struct xdr_buf
*subbuf
, void *obj
, unsigned int len
)
693 unsigned int this_len
;
695 this_len
= min_t(unsigned int, len
, subbuf
->head
[0].iov_len
);
696 memcpy(obj
, subbuf
->head
[0].iov_base
, this_len
);
699 this_len
= min_t(unsigned int, len
, subbuf
->page_len
);
701 _copy_from_pages(obj
, subbuf
->pages
, subbuf
->page_base
, this_len
);
704 this_len
= min_t(unsigned int, len
, subbuf
->tail
[0].iov_len
);
705 memcpy(obj
, subbuf
->tail
[0].iov_base
, this_len
);
708 /* obj is assumed to point to allocated memory of size at least len: */
709 int read_bytes_from_xdr_buf(struct xdr_buf
*buf
, unsigned int base
, void *obj
, unsigned int len
)
711 struct xdr_buf subbuf
;
714 status
= xdr_buf_subsegment(buf
, &subbuf
, base
, len
);
717 __read_bytes_from_xdr_buf(&subbuf
, obj
, len
);
721 static void __write_bytes_to_xdr_buf(struct xdr_buf
*subbuf
, void *obj
, unsigned int len
)
723 unsigned int this_len
;
725 this_len
= min_t(unsigned int, len
, subbuf
->head
[0].iov_len
);
726 memcpy(subbuf
->head
[0].iov_base
, obj
, this_len
);
729 this_len
= min_t(unsigned int, len
, subbuf
->page_len
);
731 _copy_to_pages(subbuf
->pages
, subbuf
->page_base
, obj
, this_len
);
734 this_len
= min_t(unsigned int, len
, subbuf
->tail
[0].iov_len
);
735 memcpy(subbuf
->tail
[0].iov_base
, obj
, this_len
);
738 /* obj is assumed to point to allocated memory of size at least len: */
739 int write_bytes_to_xdr_buf(struct xdr_buf
*buf
, unsigned int base
, void *obj
, unsigned int len
)
741 struct xdr_buf subbuf
;
744 status
= xdr_buf_subsegment(buf
, &subbuf
, base
, len
);
747 __write_bytes_to_xdr_buf(&subbuf
, obj
, len
);
752 xdr_decode_word(struct xdr_buf
*buf
, unsigned int base
, u32
*obj
)
757 status
= read_bytes_from_xdr_buf(buf
, base
, &raw
, sizeof(*obj
));
765 xdr_encode_word(struct xdr_buf
*buf
, unsigned int base
, u32 obj
)
767 __be32 raw
= htonl(obj
);
769 return write_bytes_to_xdr_buf(buf
, base
, &raw
, sizeof(obj
));
772 /* If the netobj starting offset bytes from the start of xdr_buf is contained
773 * entirely in the head or the tail, set object to point to it; otherwise
774 * try to find space for it at the end of the tail, copy it there, and
775 * set obj to point to it. */
776 int xdr_buf_read_netobj(struct xdr_buf
*buf
, struct xdr_netobj
*obj
, unsigned int offset
)
778 struct xdr_buf subbuf
;
780 if (xdr_decode_word(buf
, offset
, &obj
->len
))
782 if (xdr_buf_subsegment(buf
, &subbuf
, offset
+ 4, obj
->len
))
785 /* Is the obj contained entirely in the head? */
786 obj
->data
= subbuf
.head
[0].iov_base
;
787 if (subbuf
.head
[0].iov_len
== obj
->len
)
789 /* ..or is the obj contained entirely in the tail? */
790 obj
->data
= subbuf
.tail
[0].iov_base
;
791 if (subbuf
.tail
[0].iov_len
== obj
->len
)
794 /* use end of tail as storage for obj:
795 * (We don't copy to the beginning because then we'd have
796 * to worry about doing a potentially overlapping copy.
797 * This assumes the object is at most half the length of the
799 if (obj
->len
> buf
->buflen
- buf
->len
)
801 if (buf
->tail
[0].iov_len
!= 0)
802 obj
->data
= buf
->tail
[0].iov_base
+ buf
->tail
[0].iov_len
;
804 obj
->data
= buf
->head
[0].iov_base
+ buf
->head
[0].iov_len
;
805 __read_bytes_from_xdr_buf(&subbuf
, obj
->data
, obj
->len
);
809 /* Returns 0 on success, or else a negative error code. */
811 xdr_xcode_array2(struct xdr_buf
*buf
, unsigned int base
,
812 struct xdr_array2_desc
*desc
, int encode
)
814 char *elem
= NULL
, *c
;
815 unsigned int copied
= 0, todo
, avail_here
;
816 struct page
**ppages
= NULL
;
820 if (xdr_encode_word(buf
, base
, desc
->array_len
) != 0)
823 if (xdr_decode_word(buf
, base
, &desc
->array_len
) != 0 ||
824 desc
->array_len
> desc
->array_maxlen
||
825 (unsigned long) base
+ 4 + desc
->array_len
*
826 desc
->elem_size
> buf
->len
)
834 todo
= desc
->array_len
* desc
->elem_size
;
837 if (todo
&& base
< buf
->head
->iov_len
) {
838 c
= buf
->head
->iov_base
+ base
;
839 avail_here
= min_t(unsigned int, todo
,
840 buf
->head
->iov_len
- base
);
843 while (avail_here
>= desc
->elem_size
) {
844 err
= desc
->xcode(desc
, c
);
847 c
+= desc
->elem_size
;
848 avail_here
-= desc
->elem_size
;
852 elem
= kmalloc(desc
->elem_size
, GFP_KERNEL
);
858 err
= desc
->xcode(desc
, elem
);
861 memcpy(c
, elem
, avail_here
);
863 memcpy(elem
, c
, avail_here
);
866 base
= buf
->head
->iov_len
; /* align to start of pages */
869 /* process pages array */
870 base
-= buf
->head
->iov_len
;
871 if (todo
&& base
< buf
->page_len
) {
872 unsigned int avail_page
;
874 avail_here
= min(todo
, buf
->page_len
- base
);
877 base
+= buf
->page_base
;
878 ppages
= buf
->pages
+ (base
>> PAGE_CACHE_SHIFT
);
879 base
&= ~PAGE_CACHE_MASK
;
880 avail_page
= min_t(unsigned int, PAGE_CACHE_SIZE
- base
,
882 c
= kmap(*ppages
) + base
;
885 avail_here
-= avail_page
;
886 if (copied
|| avail_page
< desc
->elem_size
) {
887 unsigned int l
= min(avail_page
,
888 desc
->elem_size
- copied
);
890 elem
= kmalloc(desc
->elem_size
,
898 err
= desc
->xcode(desc
, elem
);
902 memcpy(c
, elem
+ copied
, l
);
904 if (copied
== desc
->elem_size
)
907 memcpy(elem
+ copied
, c
, l
);
909 if (copied
== desc
->elem_size
) {
910 err
= desc
->xcode(desc
, elem
);
919 while (avail_page
>= desc
->elem_size
) {
920 err
= desc
->xcode(desc
, c
);
923 c
+= desc
->elem_size
;
924 avail_page
-= desc
->elem_size
;
927 unsigned int l
= min(avail_page
,
928 desc
->elem_size
- copied
);
930 elem
= kmalloc(desc
->elem_size
,
938 err
= desc
->xcode(desc
, elem
);
942 memcpy(c
, elem
+ copied
, l
);
944 if (copied
== desc
->elem_size
)
947 memcpy(elem
+ copied
, c
, l
);
949 if (copied
== desc
->elem_size
) {
950 err
= desc
->xcode(desc
, elem
);
963 avail_page
= min(avail_here
,
964 (unsigned int) PAGE_CACHE_SIZE
);
966 base
= buf
->page_len
; /* align to start of tail */
970 base
-= buf
->page_len
;
972 c
= buf
->tail
->iov_base
+ base
;
974 unsigned int l
= desc
->elem_size
- copied
;
977 memcpy(c
, elem
+ copied
, l
);
979 memcpy(elem
+ copied
, c
, l
);
980 err
= desc
->xcode(desc
, elem
);
988 err
= desc
->xcode(desc
, c
);
991 c
+= desc
->elem_size
;
992 todo
-= desc
->elem_size
;
1005 xdr_decode_array2(struct xdr_buf
*buf
, unsigned int base
,
1006 struct xdr_array2_desc
*desc
)
1008 if (base
>= buf
->len
)
1011 return xdr_xcode_array2(buf
, base
, desc
, 0);
1015 xdr_encode_array2(struct xdr_buf
*buf
, unsigned int base
,
1016 struct xdr_array2_desc
*desc
)
1018 if ((unsigned long) base
+ 4 + desc
->array_len
* desc
->elem_size
>
1019 buf
->head
->iov_len
+ buf
->page_len
+ buf
->tail
->iov_len
)
1022 return xdr_xcode_array2(buf
, base
, desc
, 1);
1026 xdr_process_buf(struct xdr_buf
*buf
, unsigned int offset
, unsigned int len
,
1027 int (*actor
)(struct scatterlist
*, void *), void *data
)
1030 unsigned page_len
, thislen
, page_offset
;
1031 struct scatterlist sg
[1];
1033 if (offset
>= buf
->head
[0].iov_len
) {
1034 offset
-= buf
->head
[0].iov_len
;
1036 thislen
= buf
->head
[0].iov_len
- offset
;
1039 sg_set_buf(sg
, buf
->head
[0].iov_base
+ offset
, thislen
);
1040 ret
= actor(sg
, data
);
1049 if (offset
>= buf
->page_len
) {
1050 offset
-= buf
->page_len
;
1052 page_len
= buf
->page_len
- offset
;
1056 page_offset
= (offset
+ buf
->page_base
) & (PAGE_CACHE_SIZE
- 1);
1057 i
= (offset
+ buf
->page_base
) >> PAGE_CACHE_SHIFT
;
1058 thislen
= PAGE_CACHE_SIZE
- page_offset
;
1060 if (thislen
> page_len
)
1062 sg
->page
= buf
->pages
[i
];
1063 sg
->offset
= page_offset
;
1064 sg
->length
= thislen
;
1065 ret
= actor(sg
, data
);
1068 page_len
-= thislen
;
1071 thislen
= PAGE_CACHE_SIZE
;
1072 } while (page_len
!= 0);
1077 if (offset
< buf
->tail
[0].iov_len
) {
1078 thislen
= buf
->tail
[0].iov_len
- offset
;
1081 sg_set_buf(sg
, buf
->tail
[0].iov_base
+ offset
, thislen
);
1082 ret
= actor(sg
, data
);
1090 EXPORT_SYMBOL(xdr_process_buf
);