2 * linux/net/sunrpc/xdr.c
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/socket.h>
11 #include <linux/string.h>
12 #include <linux/kernel.h>
13 #include <linux/pagemap.h>
14 #include <linux/errno.h>
16 #include <linux/net.h>
18 #include <linux/sunrpc/xdr.h>
19 #include <linux/sunrpc/msg_prot.h>
22 * XDR functions for basic NFS types
25 xdr_encode_netobj(u32
*p
, const struct xdr_netobj
*obj
)
27 unsigned int quadlen
= XDR_QUADLEN(obj
->len
);
29 p
[quadlen
] = 0; /* zero trailing bytes */
30 *p
++ = htonl(obj
->len
);
31 memcpy(p
, obj
->data
, obj
->len
);
32 return p
+ XDR_QUADLEN(obj
->len
);
36 xdr_decode_netobj(u32
*p
, struct xdr_netobj
*obj
)
40 if ((len
= ntohl(*p
++)) > XDR_MAX_NETOBJ
)
44 return p
+ XDR_QUADLEN(len
);
48 * xdr_encode_opaque_fixed - Encode fixed length opaque data
49 * @p: pointer to current position in XDR buffer.
50 * @ptr: pointer to data to encode (or NULL)
51 * @nbytes: size of data.
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.
58 * Returns the updated current XDR buffer position
61 u32
*xdr_encode_opaque_fixed(u32
*p
, const void *ptr
, unsigned int nbytes
)
63 if (likely(nbytes
!= 0)) {
64 unsigned int quadlen
= XDR_QUADLEN(nbytes
);
65 unsigned int padding
= (quadlen
<< 2) - nbytes
;
68 memcpy(p
, ptr
, nbytes
);
70 memset((char *)p
+ nbytes
, 0, padding
);
75 EXPORT_SYMBOL(xdr_encode_opaque_fixed
);
78 * xdr_encode_opaque - Encode variable length opaque data
79 * @p: pointer to current position in XDR buffer.
80 * @ptr: pointer to data to encode (or NULL)
81 * @nbytes: size of data.
83 * Returns the updated current XDR buffer position
85 u32
*xdr_encode_opaque(u32
*p
, const void *ptr
, unsigned int nbytes
)
88 return xdr_encode_opaque_fixed(p
, ptr
, nbytes
);
90 EXPORT_SYMBOL(xdr_encode_opaque
);
93 xdr_encode_string(u32
*p
, const char *string
)
95 return xdr_encode_array(p
, string
, strlen(string
));
99 xdr_decode_string(u32
*p
, char **sp
, int *lenp
, int maxlen
)
104 if ((len
= ntohl(*p
++)) > maxlen
)
108 if ((len
% 4) != 0) {
111 string
= (char *) (p
- 1);
112 memmove(string
, p
, len
);
116 return p
+ XDR_QUADLEN(len
);
120 xdr_decode_string_inplace(u32
*p
, char **sp
, int *lenp
, int maxlen
)
124 if ((len
= ntohl(*p
++)) > maxlen
)
128 return p
+ XDR_QUADLEN(len
);
132 xdr_encode_pages(struct xdr_buf
*xdr
, struct page
**pages
, unsigned int base
,
135 struct kvec
*tail
= xdr
->tail
;
139 xdr
->page_base
= base
;
142 p
= (u32
*)xdr
->head
[0].iov_base
+ XDR_QUADLEN(xdr
->head
[0].iov_len
);
147 unsigned int pad
= 4 - (len
& 3);
150 tail
->iov_base
= (char *)p
+ (len
& 3);
159 xdr_inline_pages(struct xdr_buf
*xdr
, unsigned int offset
,
160 struct page
**pages
, unsigned int base
, unsigned int len
)
162 struct kvec
*head
= xdr
->head
;
163 struct kvec
*tail
= xdr
->tail
;
164 char *buf
= (char *)head
->iov_base
;
165 unsigned int buflen
= head
->iov_len
;
167 head
->iov_len
= offset
;
170 xdr
->page_base
= base
;
173 tail
->iov_base
= buf
+ offset
;
174 tail
->iov_len
= buflen
- offset
;
180 xdr_partial_copy_from_skb(struct xdr_buf
*xdr
, unsigned int base
,
182 skb_read_actor_t copy_actor
)
184 struct page
**ppage
= xdr
->pages
;
185 unsigned int len
, pglen
= xdr
->page_len
;
189 len
= xdr
->head
[0].iov_len
;
192 ret
= copy_actor(desc
, (char *)xdr
->head
[0].iov_base
+ base
, len
);
194 if (ret
!= len
|| !desc
->count
)
206 if (base
|| xdr
->page_base
) {
208 base
+= xdr
->page_base
;
209 ppage
+= base
>> PAGE_CACHE_SHIFT
;
210 base
&= ~PAGE_CACHE_MASK
;
215 /* ACL likes to be lazy in allocating pages - ACLs
216 * are small by default but can get huge. */
217 if (unlikely(*ppage
== NULL
)) {
218 *ppage
= alloc_page(GFP_ATOMIC
);
219 if (unlikely(*ppage
== NULL
)) {
226 len
= PAGE_CACHE_SIZE
;
227 kaddr
= kmap_atomic(*ppage
, KM_SKB_SUNRPC_DATA
);
232 ret
= copy_actor(desc
, kaddr
+ base
, len
);
237 ret
= copy_actor(desc
, kaddr
, len
);
239 flush_dcache_page(*ppage
);
240 kunmap_atomic(kaddr
, KM_SKB_SUNRPC_DATA
);
242 if (ret
!= len
|| !desc
->count
)
245 } while ((pglen
-= len
) != 0);
247 len
= xdr
->tail
[0].iov_len
;
249 copied
+= copy_actor(desc
, (char *)xdr
->tail
[0].iov_base
+ base
, len
- base
);
256 xdr_sendpages(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
,
257 struct xdr_buf
*xdr
, unsigned int base
, int msgflags
)
259 struct page
**ppage
= xdr
->pages
;
260 unsigned int len
, pglen
= xdr
->page_len
;
262 ssize_t (*sendpage
)(struct socket
*, struct page
*, int, size_t, int);
264 len
= xdr
->head
[0].iov_len
;
265 if (base
< len
|| (addr
!= NULL
&& base
== 0)) {
267 .iov_base
= xdr
->head
[0].iov_base
+ base
,
268 .iov_len
= len
- base
,
270 struct msghdr msg
= {
272 .msg_namelen
= addrlen
,
273 .msg_flags
= msgflags
,
276 msg
.msg_flags
|= MSG_MORE
;
278 if (iov
.iov_len
!= 0)
279 err
= kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
281 err
= kernel_sendmsg(sock
, &msg
, NULL
, 0, 0);
286 if (err
!= iov
.iov_len
)
298 if (base
|| xdr
->page_base
) {
300 base
+= xdr
->page_base
;
301 ppage
+= base
>> PAGE_CACHE_SHIFT
;
302 base
&= ~PAGE_CACHE_MASK
;
305 sendpage
= sock
->ops
->sendpage
? : sock_no_sendpage
;
307 int flags
= msgflags
;
309 len
= PAGE_CACHE_SIZE
;
315 if (pglen
!= len
|| xdr
->tail
[0].iov_len
!= 0)
318 /* Hmm... We might be dealing with highmem pages */
319 if (PageHighMem(*ppage
))
320 sendpage
= sock_no_sendpage
;
321 err
= sendpage(sock
, *ppage
, base
, len
, flags
);
330 } while ((pglen
-= len
) != 0);
332 len
= xdr
->tail
[0].iov_len
;
335 .iov_base
= xdr
->tail
[0].iov_base
+ base
,
336 .iov_len
= len
- base
,
338 struct msghdr msg
= {
339 .msg_flags
= msgflags
,
341 err
= kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
353 * Helper routines for doing 'memmove' like operations on a struct xdr_buf
355 * _shift_data_right_pages
356 * @pages: vector of pages containing both the source and dest memory area.
357 * @pgto_base: page vector address of destination
358 * @pgfrom_base: page vector address of source
359 * @len: number of bytes to copy
361 * Note: the addresses pgto_base and pgfrom_base are both calculated in
363 * if a memory area starts at byte 'base' in page 'pages[i]',
364 * then its address is given as (i << PAGE_CACHE_SHIFT) + base
365 * Also note: pgfrom_base must be < pgto_base, but the memory areas
366 * they point to may overlap.
369 _shift_data_right_pages(struct page
**pages
, size_t pgto_base
,
370 size_t pgfrom_base
, size_t len
)
372 struct page
**pgfrom
, **pgto
;
376 BUG_ON(pgto_base
<= pgfrom_base
);
381 pgto
= pages
+ (pgto_base
>> PAGE_CACHE_SHIFT
);
382 pgfrom
= pages
+ (pgfrom_base
>> PAGE_CACHE_SHIFT
);
384 pgto_base
&= ~PAGE_CACHE_MASK
;
385 pgfrom_base
&= ~PAGE_CACHE_MASK
;
388 /* Are any pointers crossing a page boundary? */
389 if (pgto_base
== 0) {
390 flush_dcache_page(*pgto
);
391 pgto_base
= PAGE_CACHE_SIZE
;
394 if (pgfrom_base
== 0) {
395 pgfrom_base
= PAGE_CACHE_SIZE
;
400 if (copy
> pgto_base
)
402 if (copy
> pgfrom_base
)
407 vto
= kmap_atomic(*pgto
, KM_USER0
);
408 vfrom
= kmap_atomic(*pgfrom
, KM_USER1
);
409 memmove(vto
+ pgto_base
, vfrom
+ pgfrom_base
, copy
);
410 kunmap_atomic(vfrom
, KM_USER1
);
411 kunmap_atomic(vto
, KM_USER0
);
413 } while ((len
-= copy
) != 0);
414 flush_dcache_page(*pgto
);
419 * @pages: array of pages
420 * @pgbase: page vector address of destination
421 * @p: pointer to source data
424 * Copies data from an arbitrary memory location into an array of pages
425 * The copy is assumed to be non-overlapping.
428 _copy_to_pages(struct page
**pages
, size_t pgbase
, const char *p
, size_t len
)
434 pgto
= pages
+ (pgbase
>> PAGE_CACHE_SHIFT
);
435 pgbase
&= ~PAGE_CACHE_MASK
;
438 copy
= PAGE_CACHE_SIZE
- pgbase
;
442 vto
= kmap_atomic(*pgto
, KM_USER0
);
443 memcpy(vto
+ pgbase
, p
, copy
);
444 kunmap_atomic(vto
, KM_USER0
);
447 if (pgbase
== PAGE_CACHE_SIZE
) {
448 flush_dcache_page(*pgto
);
454 } while ((len
-= copy
) != 0);
455 flush_dcache_page(*pgto
);
460 * @p: pointer to destination
461 * @pages: array of pages
462 * @pgbase: offset of source data
465 * Copies data into an arbitrary memory location from an array of pages
466 * The copy is assumed to be non-overlapping.
469 _copy_from_pages(char *p
, struct page
**pages
, size_t pgbase
, size_t len
)
471 struct page
**pgfrom
;
475 pgfrom
= pages
+ (pgbase
>> PAGE_CACHE_SHIFT
);
476 pgbase
&= ~PAGE_CACHE_MASK
;
479 copy
= PAGE_CACHE_SIZE
- pgbase
;
483 vfrom
= kmap_atomic(*pgfrom
, KM_USER0
);
484 memcpy(p
, vfrom
+ pgbase
, copy
);
485 kunmap_atomic(vfrom
, KM_USER0
);
488 if (pgbase
== PAGE_CACHE_SIZE
) {
494 } while ((len
-= copy
) != 0);
500 * @len: bytes to remove from buf->head[0]
502 * Shrinks XDR buffer's header kvec buf->head[0] by
503 * 'len' bytes. The extra data is not lost, but is instead
504 * moved into the inlined pages and/or the tail.
507 xdr_shrink_bufhead(struct xdr_buf
*buf
, size_t len
)
509 struct kvec
*head
, *tail
;
511 unsigned int pglen
= buf
->page_len
;
515 BUG_ON (len
> head
->iov_len
);
517 /* Shift the tail first */
518 if (tail
->iov_len
!= 0) {
519 if (tail
->iov_len
> len
) {
520 copy
= tail
->iov_len
- len
;
521 memmove((char *)tail
->iov_base
+ len
,
522 tail
->iov_base
, copy
);
524 /* Copy from the inlined pages into the tail */
529 if (offs
>= tail
->iov_len
)
531 else if (copy
> tail
->iov_len
- offs
)
532 copy
= tail
->iov_len
- offs
;
534 _copy_from_pages((char *)tail
->iov_base
+ offs
,
536 buf
->page_base
+ pglen
+ offs
- len
,
538 /* Do we also need to copy data from the head into the tail ? */
540 offs
= copy
= len
- pglen
;
541 if (copy
> tail
->iov_len
)
542 copy
= tail
->iov_len
;
543 memcpy(tail
->iov_base
,
544 (char *)head
->iov_base
+
545 head
->iov_len
- offs
,
549 /* Now handle pages */
552 _shift_data_right_pages(buf
->pages
,
553 buf
->page_base
+ len
,
559 _copy_to_pages(buf
->pages
, buf
->page_base
,
560 (char *)head
->iov_base
+ head
->iov_len
- len
,
563 head
->iov_len
-= len
;
565 /* Have we truncated the message? */
566 if (buf
->len
> buf
->buflen
)
567 buf
->len
= buf
->buflen
;
573 * @len: bytes to remove from buf->pages
575 * Shrinks XDR buffer's page array buf->pages by
576 * 'len' bytes. The extra data is not lost, but is instead
577 * moved into the tail.
580 xdr_shrink_pagelen(struct xdr_buf
*buf
, size_t len
)
585 unsigned int pglen
= buf
->page_len
;
588 BUG_ON (len
> pglen
);
590 /* Shift the tail first */
591 if (tail
->iov_len
!= 0) {
592 p
= (char *)tail
->iov_base
+ len
;
593 if (tail
->iov_len
> len
) {
594 copy
= tail
->iov_len
- len
;
595 memmove(p
, tail
->iov_base
, copy
);
598 /* Copy from the inlined pages into the tail */
600 if (copy
> tail
->iov_len
)
601 copy
= tail
->iov_len
;
602 _copy_from_pages((char *)tail
->iov_base
,
603 buf
->pages
, buf
->page_base
+ pglen
- len
,
606 buf
->page_len
-= len
;
608 /* Have we truncated the message? */
609 if (buf
->len
> buf
->buflen
)
610 buf
->len
= buf
->buflen
;
614 xdr_shift_buf(struct xdr_buf
*buf
, size_t len
)
616 xdr_shrink_bufhead(buf
, len
);
620 * xdr_init_encode - Initialize a struct xdr_stream for sending data.
621 * @xdr: pointer to xdr_stream struct
622 * @buf: pointer to XDR buffer in which to encode data
623 * @p: current pointer inside XDR buffer
625 * Note: at the moment the RPC client only passes the length of our
626 * scratch buffer in the xdr_buf's header kvec. Previously this
627 * meant we needed to call xdr_adjust_iovec() after encoding the
628 * data. With the new scheme, the xdr_stream manages the details
629 * of the buffer length, and takes care of adjusting the kvec
632 void xdr_init_encode(struct xdr_stream
*xdr
, struct xdr_buf
*buf
, uint32_t *p
)
634 struct kvec
*iov
= buf
->head
;
635 int scratch_len
= buf
->buflen
- buf
->page_len
- buf
->tail
[0].iov_len
;
637 BUG_ON(scratch_len
< 0);
640 xdr
->p
= (uint32_t *)((char *)iov
->iov_base
+ iov
->iov_len
);
641 xdr
->end
= (uint32_t *)((char *)iov
->iov_base
+ scratch_len
);
642 BUG_ON(iov
->iov_len
> scratch_len
);
644 if (p
!= xdr
->p
&& p
!= NULL
) {
647 BUG_ON(p
< xdr
->p
|| p
> xdr
->end
);
648 len
= (char *)p
- (char *)xdr
->p
;
654 EXPORT_SYMBOL(xdr_init_encode
);
657 * xdr_reserve_space - Reserve buffer space for sending
658 * @xdr: pointer to xdr_stream
659 * @nbytes: number of bytes to reserve
661 * Checks that we have enough buffer space to encode 'nbytes' more
662 * bytes of data. If so, update the total xdr_buf length, and
663 * adjust the length of the current kvec.
665 uint32_t * xdr_reserve_space(struct xdr_stream
*xdr
, size_t nbytes
)
667 uint32_t *p
= xdr
->p
;
670 /* align nbytes on the next 32-bit boundary */
673 q
= p
+ (nbytes
>> 2);
674 if (unlikely(q
> xdr
->end
|| q
< p
))
677 xdr
->iov
->iov_len
+= nbytes
;
678 xdr
->buf
->len
+= nbytes
;
681 EXPORT_SYMBOL(xdr_reserve_space
);
684 * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
685 * @xdr: pointer to xdr_stream
686 * @pages: list of pages
687 * @base: offset of first byte
688 * @len: length of data in bytes
691 void xdr_write_pages(struct xdr_stream
*xdr
, struct page
**pages
, unsigned int base
,
694 struct xdr_buf
*buf
= xdr
->buf
;
695 struct kvec
*iov
= buf
->tail
;
697 buf
->page_base
= base
;
700 iov
->iov_base
= (char *)xdr
->p
;
705 unsigned int pad
= 4 - (len
& 3);
707 BUG_ON(xdr
->p
>= xdr
->end
);
708 iov
->iov_base
= (char *)xdr
->p
+ (len
& 3);
716 EXPORT_SYMBOL(xdr_write_pages
);
719 * xdr_init_decode - Initialize an xdr_stream for decoding data.
720 * @xdr: pointer to xdr_stream struct
721 * @buf: pointer to XDR buffer from which to decode data
722 * @p: current pointer inside XDR buffer
724 void xdr_init_decode(struct xdr_stream
*xdr
, struct xdr_buf
*buf
, uint32_t *p
)
726 struct kvec
*iov
= buf
->head
;
727 unsigned int len
= iov
->iov_len
;
734 xdr
->end
= (uint32_t *)((char *)iov
->iov_base
+ len
);
736 EXPORT_SYMBOL(xdr_init_decode
);
739 * xdr_inline_decode - Retrieve non-page XDR data to decode
740 * @xdr: pointer to xdr_stream struct
741 * @nbytes: number of bytes of data to decode
743 * Check if the input buffer is long enough to enable us to decode
744 * 'nbytes' more bytes of data starting at the current position.
745 * If so return the current pointer, then update the current
748 uint32_t * xdr_inline_decode(struct xdr_stream
*xdr
, size_t nbytes
)
750 uint32_t *p
= xdr
->p
;
751 uint32_t *q
= p
+ XDR_QUADLEN(nbytes
);
753 if (unlikely(q
> xdr
->end
|| q
< p
))
758 EXPORT_SYMBOL(xdr_inline_decode
);
761 * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
762 * @xdr: pointer to xdr_stream struct
763 * @len: number of bytes of page data
765 * Moves data beyond the current pointer position from the XDR head[] buffer
766 * into the page list. Any data that lies beyond current position + "len"
767 * bytes is moved into the XDR tail[]. The current pointer is then
768 * repositioned at the beginning of the XDR tail.
770 void xdr_read_pages(struct xdr_stream
*xdr
, unsigned int len
)
772 struct xdr_buf
*buf
= xdr
->buf
;
778 /* Realign pages to current pointer position */
780 shift
= iov
->iov_len
+ (char *)iov
->iov_base
- (char *)xdr
->p
;
782 xdr_shrink_bufhead(buf
, shift
);
784 /* Truncate page data and move it into the tail */
785 if (buf
->page_len
> len
)
786 xdr_shrink_pagelen(buf
, buf
->page_len
- len
);
787 padding
= (XDR_QUADLEN(len
) << 2) - len
;
788 xdr
->iov
= iov
= buf
->tail
;
789 /* Compute remaining message length. */
791 shift
= buf
->buflen
- buf
->len
;
797 * Position current pointer at beginning of tail, and
798 * set remaining message length.
800 xdr
->p
= (uint32_t *)((char *)iov
->iov_base
+ padding
);
801 xdr
->end
= (uint32_t *)((char *)iov
->iov_base
+ end
);
803 EXPORT_SYMBOL(xdr_read_pages
);
805 static struct kvec empty_iov
= {.iov_base
= NULL
, .iov_len
= 0};
808 xdr_buf_from_iov(struct kvec
*iov
, struct xdr_buf
*buf
)
811 buf
->tail
[0] = empty_iov
;
813 buf
->buflen
= buf
->len
= iov
->iov_len
;
816 /* Sets subiov to the intersection of iov with the buffer of length len
817 * starting base bytes after iov. Indicates empty intersection by setting
818 * length of subiov to zero. Decrements len by length of subiov, sets base
819 * to zero (or decrements it by length of iov if subiov is empty). */
821 iov_subsegment(struct kvec
*iov
, struct kvec
*subiov
, int *base
, int *len
)
823 if (*base
> iov
->iov_len
) {
824 subiov
->iov_base
= NULL
;
826 *base
-= iov
->iov_len
;
828 subiov
->iov_base
= iov
->iov_base
+ *base
;
829 subiov
->iov_len
= min(*len
, (int)iov
->iov_len
- *base
);
832 *len
-= subiov
->iov_len
;
835 /* Sets subbuf to the portion of buf of length len beginning base bytes
836 * from the start of buf. Returns -1 if base of length are out of bounds. */
838 xdr_buf_subsegment(struct xdr_buf
*buf
, struct xdr_buf
*subbuf
,
843 subbuf
->buflen
= subbuf
->len
= len
;
844 iov_subsegment(buf
->head
, subbuf
->head
, &base
, &len
);
846 if (base
< buf
->page_len
) {
847 i
= (base
+ buf
->page_base
) >> PAGE_CACHE_SHIFT
;
848 subbuf
->pages
= &buf
->pages
[i
];
849 subbuf
->page_base
= (base
+ buf
->page_base
) & ~PAGE_CACHE_MASK
;
850 subbuf
->page_len
= min((int)buf
->page_len
- base
, len
);
851 len
-= subbuf
->page_len
;
854 base
-= buf
->page_len
;
855 subbuf
->page_len
= 0;
858 iov_subsegment(buf
->tail
, subbuf
->tail
, &base
, &len
);
864 /* obj is assumed to point to allocated memory of size at least len: */
866 read_bytes_from_xdr_buf(struct xdr_buf
*buf
, int base
, void *obj
, int len
)
868 struct xdr_buf subbuf
;
872 status
= xdr_buf_subsegment(buf
, &subbuf
, base
, len
);
875 this_len
= min(len
, (int)subbuf
.head
[0].iov_len
);
876 memcpy(obj
, subbuf
.head
[0].iov_base
, this_len
);
879 this_len
= min(len
, (int)subbuf
.page_len
);
881 _copy_from_pages(obj
, subbuf
.pages
, subbuf
.page_base
, this_len
);
884 this_len
= min(len
, (int)subbuf
.tail
[0].iov_len
);
885 memcpy(obj
, subbuf
.tail
[0].iov_base
, this_len
);
890 /* obj is assumed to point to allocated memory of size at least len: */
892 write_bytes_to_xdr_buf(struct xdr_buf
*buf
, int base
, void *obj
, int len
)
894 struct xdr_buf subbuf
;
898 status
= xdr_buf_subsegment(buf
, &subbuf
, base
, len
);
901 this_len
= min(len
, (int)subbuf
.head
[0].iov_len
);
902 memcpy(subbuf
.head
[0].iov_base
, obj
, this_len
);
905 this_len
= min(len
, (int)subbuf
.page_len
);
907 _copy_to_pages(subbuf
.pages
, subbuf
.page_base
, obj
, this_len
);
910 this_len
= min(len
, (int)subbuf
.tail
[0].iov_len
);
911 memcpy(subbuf
.tail
[0].iov_base
, obj
, this_len
);
917 xdr_decode_word(struct xdr_buf
*buf
, int base
, u32
*obj
)
922 status
= read_bytes_from_xdr_buf(buf
, base
, &raw
, sizeof(*obj
));
930 xdr_encode_word(struct xdr_buf
*buf
, int base
, u32 obj
)
932 u32 raw
= htonl(obj
);
934 return write_bytes_to_xdr_buf(buf
, base
, &raw
, sizeof(obj
));
937 /* If the netobj starting offset bytes from the start of xdr_buf is contained
938 * entirely in the head or the tail, set object to point to it; otherwise
939 * try to find space for it at the end of the tail, copy it there, and
940 * set obj to point to it. */
942 xdr_buf_read_netobj(struct xdr_buf
*buf
, struct xdr_netobj
*obj
, int offset
)
944 u32 tail_offset
= buf
->head
[0].iov_len
+ buf
->page_len
;
947 if (xdr_decode_word(buf
, offset
, &obj
->len
))
949 obj_end_offset
= offset
+ 4 + obj
->len
;
951 if (obj_end_offset
<= buf
->head
[0].iov_len
) {
952 /* The obj is contained entirely in the head: */
953 obj
->data
= buf
->head
[0].iov_base
+ offset
+ 4;
954 } else if (offset
+ 4 >= tail_offset
) {
955 if (obj_end_offset
- tail_offset
956 > buf
->tail
[0].iov_len
)
958 /* The obj is contained entirely in the tail: */
959 obj
->data
= buf
->tail
[0].iov_base
960 + offset
- tail_offset
+ 4;
962 /* use end of tail as storage for obj:
963 * (We don't copy to the beginning because then we'd have
964 * to worry about doing a potentially overlapping copy.
965 * This assumes the object is at most half the length of the
967 if (obj
->len
> buf
->tail
[0].iov_len
)
969 obj
->data
= buf
->tail
[0].iov_base
+ buf
->tail
[0].iov_len
-
971 if (read_bytes_from_xdr_buf(buf
, offset
+ 4,
972 obj
->data
, obj
->len
))
981 /* Returns 0 on success, or else a negative error code. */
983 xdr_xcode_array2(struct xdr_buf
*buf
, unsigned int base
,
984 struct xdr_array2_desc
*desc
, int encode
)
986 char *elem
= NULL
, *c
;
987 unsigned int copied
= 0, todo
, avail_here
;
988 struct page
**ppages
= NULL
;
992 if (xdr_encode_word(buf
, base
, desc
->array_len
) != 0)
995 if (xdr_decode_word(buf
, base
, &desc
->array_len
) != 0 ||
996 desc
->array_len
> desc
->array_maxlen
||
997 (unsigned long) base
+ 4 + desc
->array_len
*
998 desc
->elem_size
> buf
->len
)
1006 todo
= desc
->array_len
* desc
->elem_size
;
1009 if (todo
&& base
< buf
->head
->iov_len
) {
1010 c
= buf
->head
->iov_base
+ base
;
1011 avail_here
= min_t(unsigned int, todo
,
1012 buf
->head
->iov_len
- base
);
1015 while (avail_here
>= desc
->elem_size
) {
1016 err
= desc
->xcode(desc
, c
);
1019 c
+= desc
->elem_size
;
1020 avail_here
-= desc
->elem_size
;
1024 elem
= kmalloc(desc
->elem_size
, GFP_KERNEL
);
1030 err
= desc
->xcode(desc
, elem
);
1033 memcpy(c
, elem
, avail_here
);
1035 memcpy(elem
, c
, avail_here
);
1036 copied
= avail_here
;
1038 base
= buf
->head
->iov_len
; /* align to start of pages */
1041 /* process pages array */
1042 base
-= buf
->head
->iov_len
;
1043 if (todo
&& base
< buf
->page_len
) {
1044 unsigned int avail_page
;
1046 avail_here
= min(todo
, buf
->page_len
- base
);
1049 base
+= buf
->page_base
;
1050 ppages
= buf
->pages
+ (base
>> PAGE_CACHE_SHIFT
);
1051 base
&= ~PAGE_CACHE_MASK
;
1052 avail_page
= min_t(unsigned int, PAGE_CACHE_SIZE
- base
,
1054 c
= kmap(*ppages
) + base
;
1056 while (avail_here
) {
1057 avail_here
-= avail_page
;
1058 if (copied
|| avail_page
< desc
->elem_size
) {
1059 unsigned int l
= min(avail_page
,
1060 desc
->elem_size
- copied
);
1062 elem
= kmalloc(desc
->elem_size
,
1070 err
= desc
->xcode(desc
, elem
);
1074 memcpy(c
, elem
+ copied
, l
);
1076 if (copied
== desc
->elem_size
)
1079 memcpy(elem
+ copied
, c
, l
);
1081 if (copied
== desc
->elem_size
) {
1082 err
= desc
->xcode(desc
, elem
);
1091 while (avail_page
>= desc
->elem_size
) {
1092 err
= desc
->xcode(desc
, c
);
1095 c
+= desc
->elem_size
;
1096 avail_page
-= desc
->elem_size
;
1099 unsigned int l
= min(avail_page
,
1100 desc
->elem_size
- copied
);
1102 elem
= kmalloc(desc
->elem_size
,
1110 err
= desc
->xcode(desc
, elem
);
1114 memcpy(c
, elem
+ copied
, l
);
1116 if (copied
== desc
->elem_size
)
1119 memcpy(elem
+ copied
, c
, l
);
1121 if (copied
== desc
->elem_size
) {
1122 err
= desc
->xcode(desc
, elem
);
1135 avail_page
= min(avail_here
,
1136 (unsigned int) PAGE_CACHE_SIZE
);
1138 base
= buf
->page_len
; /* align to start of tail */
1142 base
-= buf
->page_len
;
1144 c
= buf
->tail
->iov_base
+ base
;
1146 unsigned int l
= desc
->elem_size
- copied
;
1149 memcpy(c
, elem
+ copied
, l
);
1151 memcpy(elem
+ copied
, c
, l
);
1152 err
= desc
->xcode(desc
, elem
);
1160 err
= desc
->xcode(desc
, c
);
1163 c
+= desc
->elem_size
;
1164 todo
-= desc
->elem_size
;
1178 xdr_decode_array2(struct xdr_buf
*buf
, unsigned int base
,
1179 struct xdr_array2_desc
*desc
)
1181 if (base
>= buf
->len
)
1184 return xdr_xcode_array2(buf
, base
, desc
, 0);
1188 xdr_encode_array2(struct xdr_buf
*buf
, unsigned int base
,
1189 struct xdr_array2_desc
*desc
)
1191 if ((unsigned long) base
+ 4 + desc
->array_len
* desc
->elem_size
>
1192 buf
->head
->iov_len
+ buf
->page_len
+ buf
->tail
->iov_len
)
1195 return xdr_xcode_array2(buf
, base
, desc
, 1);