1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/xdr.c
7 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include <linux/kernel.h>
15 #include <linux/pagemap.h>
16 #include <linux/errno.h>
17 #include <linux/sunrpc/xdr.h>
18 #include <linux/sunrpc/msg_prot.h>
19 #include <linux/bvec.h>
20 #include <trace/events/sunrpc.h>
22 static void _copy_to_pages(struct page **, size_t, const char *, size_t);
26 * XDR functions for basic NFS types
29 xdr_encode_netobj(__be32 *p, const struct xdr_netobj *obj)
31 unsigned int quadlen = XDR_QUADLEN(obj->len);
33 p[quadlen] = 0; /* zero trailing bytes */
34 *p++ = cpu_to_be32(obj->len);
35 memcpy(p, obj->data, obj->len);
36 return p + XDR_QUADLEN(obj->len);
38 EXPORT_SYMBOL_GPL(xdr_encode_netobj);
41 xdr_decode_netobj(__be32 *p, struct xdr_netobj *obj)
45 if ((len = be32_to_cpu(*p++)) > XDR_MAX_NETOBJ)
49 return p + XDR_QUADLEN(len);
51 EXPORT_SYMBOL_GPL(xdr_decode_netobj);
54 * xdr_encode_opaque_fixed - Encode fixed length opaque data
55 * @p: pointer to current position in XDR buffer.
56 * @ptr: pointer to data to encode (or NULL)
57 * @nbytes: size of data.
59 * Copy the array of data of length nbytes at ptr to the XDR buffer
60 * at position p, then align to the next 32-bit boundary by padding
61 * with zero bytes (see RFC1832).
62 * Note: if ptr is NULL, only the padding is performed.
64 * Returns the updated current XDR buffer position
67 __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int nbytes)
69 if (likely(nbytes != 0)) {
70 unsigned int quadlen = XDR_QUADLEN(nbytes);
71 unsigned int padding = (quadlen << 2) - nbytes;
74 memcpy(p, ptr, nbytes);
76 memset((char *)p + nbytes, 0, padding);
81 EXPORT_SYMBOL_GPL(xdr_encode_opaque_fixed);
84 * xdr_encode_opaque - Encode variable length opaque data
85 * @p: pointer to current position in XDR buffer.
86 * @ptr: pointer to data to encode (or NULL)
87 * @nbytes: size of data.
89 * Returns the updated current XDR buffer position
91 __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int nbytes)
93 *p++ = cpu_to_be32(nbytes);
94 return xdr_encode_opaque_fixed(p, ptr, nbytes);
96 EXPORT_SYMBOL_GPL(xdr_encode_opaque);
99 xdr_encode_string(__be32 *p, const char *string)
101 return xdr_encode_array(p, string, strlen(string));
103 EXPORT_SYMBOL_GPL(xdr_encode_string);
106 xdr_decode_string_inplace(__be32 *p, char **sp,
107 unsigned int *lenp, unsigned int maxlen)
111 len = be32_to_cpu(*p++);
116 return p + XDR_QUADLEN(len);
118 EXPORT_SYMBOL_GPL(xdr_decode_string_inplace);
121 * xdr_terminate_string - '\0'-terminate a string residing in an xdr_buf
122 * @buf: XDR buffer where string resides
123 * @len: length of string, in bytes
126 void xdr_terminate_string(const struct xdr_buf *buf, const u32 len)
130 kaddr = kmap_atomic(buf->pages[0]);
131 kaddr[buf->page_base + len] = '\0';
132 kunmap_atomic(kaddr);
134 EXPORT_SYMBOL_GPL(xdr_terminate_string);
136 size_t xdr_buf_pagecount(const struct xdr_buf *buf)
140 return (buf->page_base + buf->page_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
144 xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp)
146 size_t i, n = xdr_buf_pagecount(buf);
148 if (n != 0 && buf->bvec == NULL) {
149 buf->bvec = kmalloc_array(n, sizeof(buf->bvec[0]), gfp);
152 for (i = 0; i < n; i++) {
153 bvec_set_page(&buf->bvec[i], buf->pages[i], PAGE_SIZE,
161 xdr_free_bvec(struct xdr_buf *buf)
168 * xdr_buf_to_bvec - Copy components of an xdr_buf into a bio_vec array
169 * @bvec: bio_vec array to populate
170 * @bvec_size: element count of @bio_vec
171 * @xdr: xdr_buf to be copied
173 * Returns the number of entries consumed in @bvec.
175 unsigned int xdr_buf_to_bvec(struct bio_vec *bvec, unsigned int bvec_size,
176 const struct xdr_buf *xdr)
178 const struct kvec *head = xdr->head;
179 const struct kvec *tail = xdr->tail;
180 unsigned int count = 0;
183 bvec_set_virt(bvec++, head->iov_base, head->iov_len);
188 unsigned int offset, len, remaining;
189 struct page **pages = xdr->pages;
191 offset = offset_in_page(xdr->page_base);
192 remaining = xdr->page_len;
193 while (remaining > 0) {
194 len = min_t(unsigned int, remaining,
196 bvec_set_page(bvec++, *pages++, len, offset);
199 if (unlikely(++count > bvec_size))
205 bvec_set_virt(bvec, tail->iov_base, tail->iov_len);
206 if (unlikely(++count > bvec_size))
213 pr_warn_once("%s: bio_vec array overflow\n", __func__);
216 EXPORT_SYMBOL_GPL(xdr_buf_to_bvec);
219 * xdr_inline_pages - Prepare receive buffer for a large reply
220 * @xdr: xdr_buf into which reply will be placed
221 * @offset: expected offset where data payload will start, in bytes
222 * @pages: vector of struct page pointers
223 * @base: offset in first page where receive should start, in bytes
224 * @len: expected size of the upper layer data payload, in bytes
228 xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
229 struct page **pages, unsigned int base, unsigned int len)
231 struct kvec *head = xdr->head;
232 struct kvec *tail = xdr->tail;
233 char *buf = (char *)head->iov_base;
234 unsigned int buflen = head->iov_len;
236 head->iov_len = offset;
239 xdr->page_base = base;
242 tail->iov_base = buf + offset;
243 tail->iov_len = buflen - offset;
246 EXPORT_SYMBOL_GPL(xdr_inline_pages);
249 * Helper routines for doing 'memmove' like operations on a struct xdr_buf
253 * _shift_data_left_pages
254 * @pages: vector of pages containing both the source and dest memory area.
255 * @pgto_base: page vector address of destination
256 * @pgfrom_base: page vector address of source
257 * @len: number of bytes to copy
259 * Note: the addresses pgto_base and pgfrom_base are both calculated in
261 * if a memory area starts at byte 'base' in page 'pages[i]',
262 * then its address is given as (i << PAGE_CACHE_SHIFT) + base
263 * Alse note: pgto_base must be < pgfrom_base, but the memory areas
264 * they point to may overlap.
267 _shift_data_left_pages(struct page **pages, size_t pgto_base,
268 size_t pgfrom_base, size_t len)
270 struct page **pgfrom, **pgto;
274 BUG_ON(pgfrom_base <= pgto_base);
279 pgto = pages + (pgto_base >> PAGE_SHIFT);
280 pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
282 pgto_base &= ~PAGE_MASK;
283 pgfrom_base &= ~PAGE_MASK;
286 if (pgto_base >= PAGE_SIZE) {
290 if (pgfrom_base >= PAGE_SIZE){
296 if (copy > (PAGE_SIZE - pgto_base))
297 copy = PAGE_SIZE - pgto_base;
298 if (copy > (PAGE_SIZE - pgfrom_base))
299 copy = PAGE_SIZE - pgfrom_base;
301 vto = kmap_atomic(*pgto);
302 if (*pgto != *pgfrom) {
303 vfrom = kmap_atomic(*pgfrom);
304 memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
305 kunmap_atomic(vfrom);
307 memmove(vto + pgto_base, vto + pgfrom_base, copy);
308 flush_dcache_page(*pgto);
314 } while ((len -= copy) != 0);
318 * _shift_data_right_pages
319 * @pages: vector of pages containing both the source and dest memory area.
320 * @pgto_base: page vector address of destination
321 * @pgfrom_base: page vector address of source
322 * @len: number of bytes to copy
324 * Note: the addresses pgto_base and pgfrom_base are both calculated in
326 * if a memory area starts at byte 'base' in page 'pages[i]',
327 * then its address is given as (i << PAGE_SHIFT) + base
328 * Also note: pgfrom_base must be < pgto_base, but the memory areas
329 * they point to may overlap.
332 _shift_data_right_pages(struct page **pages, size_t pgto_base,
333 size_t pgfrom_base, size_t len)
335 struct page **pgfrom, **pgto;
339 BUG_ON(pgto_base <= pgfrom_base);
347 pgto = pages + (pgto_base >> PAGE_SHIFT);
348 pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
350 pgto_base &= ~PAGE_MASK;
351 pgfrom_base &= ~PAGE_MASK;
354 /* Are any pointers crossing a page boundary? */
355 if (pgto_base == 0) {
356 pgto_base = PAGE_SIZE;
359 if (pgfrom_base == 0) {
360 pgfrom_base = PAGE_SIZE;
365 if (copy > pgto_base)
367 if (copy > pgfrom_base)
372 vto = kmap_atomic(*pgto);
373 if (*pgto != *pgfrom) {
374 vfrom = kmap_atomic(*pgfrom);
375 memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
376 kunmap_atomic(vfrom);
378 memmove(vto + pgto_base, vto + pgfrom_base, copy);
379 flush_dcache_page(*pgto);
382 } while ((len -= copy) != 0);
387 * @pages: array of pages
388 * @pgbase: page vector address of destination
389 * @p: pointer to source data
392 * Copies data from an arbitrary memory location into an array of pages
393 * The copy is assumed to be non-overlapping.
396 _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
405 pgto = pages + (pgbase >> PAGE_SHIFT);
406 pgbase &= ~PAGE_MASK;
409 copy = PAGE_SIZE - pgbase;
413 vto = kmap_atomic(*pgto);
414 memcpy(vto + pgbase, p, copy);
422 if (pgbase == PAGE_SIZE) {
423 flush_dcache_page(*pgto);
429 flush_dcache_page(*pgto);
434 * @p: pointer to destination
435 * @pages: array of pages
436 * @pgbase: offset of source data
439 * Copies data into an arbitrary memory location from an array of pages
440 * The copy is assumed to be non-overlapping.
443 _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
445 struct page **pgfrom;
452 pgfrom = pages + (pgbase >> PAGE_SHIFT);
453 pgbase &= ~PAGE_MASK;
456 copy = PAGE_SIZE - pgbase;
460 vfrom = kmap_atomic(*pgfrom);
461 memcpy(p, vfrom + pgbase, copy);
462 kunmap_atomic(vfrom);
465 if (pgbase == PAGE_SIZE) {
471 } while ((len -= copy) != 0);
473 EXPORT_SYMBOL_GPL(_copy_from_pages);
475 static void xdr_buf_iov_zero(const struct kvec *iov, unsigned int base,
478 if (base >= iov->iov_len)
480 if (len > iov->iov_len - base)
481 len = iov->iov_len - base;
482 memset(iov->iov_base + base, 0, len);
488 * @pgbase: beginning offset
491 static void xdr_buf_pages_zero(const struct xdr_buf *buf, unsigned int pgbase,
494 struct page **pages = buf->pages;
501 if (pgbase >= buf->page_len) {
502 xdr_buf_iov_zero(buf->tail, pgbase - buf->page_len, len);
505 if (pgbase + len > buf->page_len) {
506 xdr_buf_iov_zero(buf->tail, 0, pgbase + len - buf->page_len);
507 len = buf->page_len - pgbase;
510 pgbase += buf->page_base;
512 page = pages + (pgbase >> PAGE_SHIFT);
513 pgbase &= ~PAGE_MASK;
516 zero = PAGE_SIZE - pgbase;
520 vpage = kmap_atomic(*page);
521 memset(vpage + pgbase, 0, zero);
522 kunmap_atomic(vpage);
524 flush_dcache_page(*page);
528 } while ((len -= zero) != 0);
531 static unsigned int xdr_buf_pages_fill_sparse(const struct xdr_buf *buf,
532 unsigned int buflen, gfp_t gfp)
534 unsigned int i, npages, pagelen;
536 if (!(buf->flags & XDRBUF_SPARSE_PAGES))
538 if (buflen <= buf->head->iov_len)
540 pagelen = buflen - buf->head->iov_len;
541 if (pagelen > buf->page_len)
542 pagelen = buf->page_len;
543 npages = (pagelen + buf->page_base + PAGE_SIZE - 1) >> PAGE_SHIFT;
544 for (i = 0; i < npages; i++) {
547 buf->pages[i] = alloc_page(gfp);
548 if (likely(buf->pages[i]))
551 pagelen = i << PAGE_SHIFT;
552 if (pagelen > buf->page_base)
553 buflen += pagelen - buf->page_base;
559 static void xdr_buf_try_expand(struct xdr_buf *buf, unsigned int len)
561 struct kvec *head = buf->head;
562 struct kvec *tail = buf->tail;
563 unsigned int sum = head->iov_len + buf->page_len + tail->iov_len;
564 unsigned int free_space, newlen;
566 if (sum > buf->len) {
567 free_space = min_t(unsigned int, sum - buf->len, len);
568 newlen = xdr_buf_pages_fill_sparse(buf, buf->len + free_space,
570 free_space = newlen - buf->len;
577 if (buf->buflen > sum) {
578 /* Expand the tail buffer */
579 free_space = min_t(unsigned int, buf->buflen - sum, len);
580 tail->iov_len += free_space;
581 buf->len += free_space;
585 static void xdr_buf_tail_copy_right(const struct xdr_buf *buf,
586 unsigned int base, unsigned int len,
589 const struct kvec *tail = buf->tail;
590 unsigned int to = base + shift;
592 if (to >= tail->iov_len)
594 if (len + to > tail->iov_len)
595 len = tail->iov_len - to;
596 memmove(tail->iov_base + to, tail->iov_base + base, len);
599 static void xdr_buf_pages_copy_right(const struct xdr_buf *buf,
600 unsigned int base, unsigned int len,
603 const struct kvec *tail = buf->tail;
604 unsigned int to = base + shift;
605 unsigned int pglen = 0;
606 unsigned int talen = 0, tato = 0;
608 if (base >= buf->page_len)
610 if (len > buf->page_len - base)
611 len = buf->page_len - base;
612 if (to >= buf->page_len) {
613 tato = to - buf->page_len;
614 if (tail->iov_len >= len + tato)
616 else if (tail->iov_len > tato)
617 talen = tail->iov_len - tato;
618 } else if (len + to >= buf->page_len) {
619 pglen = buf->page_len - to;
621 if (talen > tail->iov_len)
622 talen = tail->iov_len;
626 _copy_from_pages(tail->iov_base + tato, buf->pages,
627 buf->page_base + base + pglen, talen);
628 _shift_data_right_pages(buf->pages, buf->page_base + to,
629 buf->page_base + base, pglen);
632 static void xdr_buf_head_copy_right(const struct xdr_buf *buf,
633 unsigned int base, unsigned int len,
636 const struct kvec *head = buf->head;
637 const struct kvec *tail = buf->tail;
638 unsigned int to = base + shift;
639 unsigned int pglen = 0, pgto = 0;
640 unsigned int talen = 0, tato = 0;
642 if (base >= head->iov_len)
644 if (len > head->iov_len - base)
645 len = head->iov_len - base;
646 if (to >= buf->page_len + head->iov_len) {
647 tato = to - buf->page_len - head->iov_len;
649 } else if (to >= head->iov_len) {
650 pgto = to - head->iov_len;
652 if (pgto + pglen > buf->page_len) {
653 talen = pgto + pglen - buf->page_len;
658 if (pglen > buf->page_len) {
659 talen = pglen - buf->page_len;
660 pglen = buf->page_len;
666 if (talen + tato > tail->iov_len)
667 talen = tail->iov_len > tato ? tail->iov_len - tato : 0;
668 memcpy(tail->iov_base + tato, head->iov_base + base, talen);
672 _copy_to_pages(buf->pages, buf->page_base + pgto, head->iov_base + base,
676 memmove(head->iov_base + to, head->iov_base + base, len);
679 static void xdr_buf_tail_shift_right(const struct xdr_buf *buf,
680 unsigned int base, unsigned int len,
683 const struct kvec *tail = buf->tail;
685 if (base >= tail->iov_len || !shift || !len)
687 xdr_buf_tail_copy_right(buf, base, len, shift);
690 static void xdr_buf_pages_shift_right(const struct xdr_buf *buf,
691 unsigned int base, unsigned int len,
696 if (base >= buf->page_len) {
697 xdr_buf_tail_shift_right(buf, base - buf->page_len, len, shift);
700 if (base + len > buf->page_len)
701 xdr_buf_tail_shift_right(buf, 0, base + len - buf->page_len,
703 xdr_buf_pages_copy_right(buf, base, len, shift);
706 static void xdr_buf_head_shift_right(const struct xdr_buf *buf,
707 unsigned int base, unsigned int len,
710 const struct kvec *head = buf->head;
714 if (base >= head->iov_len) {
715 xdr_buf_pages_shift_right(buf, head->iov_len - base, len,
719 if (base + len > head->iov_len)
720 xdr_buf_pages_shift_right(buf, 0, base + len - head->iov_len,
722 xdr_buf_head_copy_right(buf, base, len, shift);
725 static void xdr_buf_tail_copy_left(const struct xdr_buf *buf, unsigned int base,
726 unsigned int len, unsigned int shift)
728 const struct kvec *tail = buf->tail;
730 if (base >= tail->iov_len)
732 if (len > tail->iov_len - base)
733 len = tail->iov_len - base;
734 /* Shift data into head */
735 if (shift > buf->page_len + base) {
736 const struct kvec *head = buf->head;
738 head->iov_len + buf->page_len + base - shift;
739 unsigned int hdlen = len;
741 if (WARN_ONCE(shift > head->iov_len + buf->page_len + base,
742 "SUNRPC: Misaligned data.\n"))
744 if (hdto + hdlen > head->iov_len)
745 hdlen = head->iov_len - hdto;
746 memcpy(head->iov_base + hdto, tail->iov_base + base, hdlen);
752 /* Shift data into pages */
754 unsigned int pgto = buf->page_len + base - shift;
755 unsigned int pglen = len;
757 if (pgto + pglen > buf->page_len)
758 pglen = buf->page_len - pgto;
759 _copy_to_pages(buf->pages, buf->page_base + pgto,
760 tail->iov_base + base, pglen);
766 memmove(tail->iov_base + base - shift, tail->iov_base + base, len);
769 static void xdr_buf_pages_copy_left(const struct xdr_buf *buf,
770 unsigned int base, unsigned int len,
775 if (base >= buf->page_len)
777 if (len > buf->page_len - base)
778 len = buf->page_len - base;
779 /* Shift data into head */
781 const struct kvec *head = buf->head;
782 unsigned int hdto = head->iov_len + base - shift;
783 unsigned int hdlen = len;
785 if (WARN_ONCE(shift > head->iov_len + base,
786 "SUNRPC: Misaligned data.\n"))
788 if (hdto + hdlen > head->iov_len)
789 hdlen = head->iov_len - hdto;
790 _copy_from_pages(head->iov_base + hdto, buf->pages,
791 buf->page_base + base, hdlen);
798 _shift_data_left_pages(buf->pages, buf->page_base + pgto,
799 buf->page_base + base, len);
802 static void xdr_buf_tail_shift_left(const struct xdr_buf *buf,
803 unsigned int base, unsigned int len,
808 xdr_buf_tail_copy_left(buf, base, len, shift);
811 static void xdr_buf_pages_shift_left(const struct xdr_buf *buf,
812 unsigned int base, unsigned int len,
817 if (base >= buf->page_len) {
818 xdr_buf_tail_shift_left(buf, base - buf->page_len, len, shift);
821 xdr_buf_pages_copy_left(buf, base, len, shift);
823 if (len <= buf->page_len)
825 xdr_buf_tail_copy_left(buf, 0, len - buf->page_len, shift);
828 static void xdr_buf_head_shift_left(const struct xdr_buf *buf,
829 unsigned int base, unsigned int len,
832 const struct kvec *head = buf->head;
839 bytes = (shift - base);
846 if (base < head->iov_len) {
847 bytes = min_t(unsigned int, len, head->iov_len - base);
848 memmove(head->iov_base + (base - shift),
849 head->iov_base + base, bytes);
853 xdr_buf_pages_shift_left(buf, base - head->iov_len, len, shift);
859 * @len: new length of buf->head[0]
861 * Shrinks XDR buffer's header kvec buf->head[0], setting it to
862 * 'len' bytes. The extra data is not lost, but is instead
863 * moved into the inlined pages and/or the tail.
865 static unsigned int xdr_shrink_bufhead(struct xdr_buf *buf, unsigned int len)
867 struct kvec *head = buf->head;
868 unsigned int shift, buflen = max(buf->len, len);
870 WARN_ON_ONCE(len > head->iov_len);
871 if (head->iov_len > buflen) {
872 buf->buflen -= head->iov_len - buflen;
873 head->iov_len = buflen;
875 if (len >= head->iov_len)
877 shift = head->iov_len - len;
878 xdr_buf_try_expand(buf, shift);
879 xdr_buf_head_shift_right(buf, len, buflen - len, shift);
881 buf->buflen -= shift;
887 * xdr_shrink_pagelen - shrinks buf->pages to @len bytes
889 * @len: new page buffer length
891 * The extra data is not lost, but is instead moved into buf->tail.
892 * Returns the actual number of bytes moved.
894 static unsigned int xdr_shrink_pagelen(struct xdr_buf *buf, unsigned int len)
896 unsigned int shift, buflen = buf->len - buf->head->iov_len;
898 WARN_ON_ONCE(len > buf->page_len);
899 if (buf->head->iov_len >= buf->len || len > buflen)
901 if (buf->page_len > buflen) {
902 buf->buflen -= buf->page_len - buflen;
903 buf->page_len = buflen;
905 if (len >= buf->page_len)
907 shift = buf->page_len - len;
908 xdr_buf_try_expand(buf, shift);
909 xdr_buf_pages_shift_right(buf, len, buflen - len, shift);
912 buf->buflen -= shift;
917 * xdr_stream_pos - Return the current offset from the start of the xdr_stream
918 * @xdr: pointer to struct xdr_stream
920 unsigned int xdr_stream_pos(const struct xdr_stream *xdr)
922 return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2;
924 EXPORT_SYMBOL_GPL(xdr_stream_pos);
926 static void xdr_stream_set_pos(struct xdr_stream *xdr, unsigned int pos)
928 unsigned int blen = xdr->buf->len;
930 xdr->nwords = blen > pos ? XDR_QUADLEN(blen) - XDR_QUADLEN(pos) : 0;
933 static void xdr_stream_page_set_pos(struct xdr_stream *xdr, unsigned int pos)
935 xdr_stream_set_pos(xdr, pos + xdr->buf->head[0].iov_len);
939 * xdr_page_pos - Return the current offset from the start of the xdr pages
940 * @xdr: pointer to struct xdr_stream
942 unsigned int xdr_page_pos(const struct xdr_stream *xdr)
944 unsigned int pos = xdr_stream_pos(xdr);
946 WARN_ON(pos < xdr->buf->head[0].iov_len);
947 return pos - xdr->buf->head[0].iov_len;
949 EXPORT_SYMBOL_GPL(xdr_page_pos);
952 * xdr_init_encode - Initialize a struct xdr_stream for sending data.
953 * @xdr: pointer to xdr_stream struct
954 * @buf: pointer to XDR buffer in which to encode data
955 * @p: current pointer inside XDR buffer
956 * @rqst: pointer to controlling rpc_rqst, for debugging
958 * Note: at the moment the RPC client only passes the length of our
959 * scratch buffer in the xdr_buf's header kvec. Previously this
960 * meant we needed to call xdr_adjust_iovec() after encoding the
961 * data. With the new scheme, the xdr_stream manages the details
962 * of the buffer length, and takes care of adjusting the kvec
965 void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
966 struct rpc_rqst *rqst)
968 struct kvec *iov = buf->head;
969 int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
971 xdr_reset_scratch_buffer(xdr);
972 BUG_ON(scratch_len < 0);
975 xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
976 xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
977 BUG_ON(iov->iov_len > scratch_len);
979 if (p != xdr->p && p != NULL) {
982 BUG_ON(p < xdr->p || p > xdr->end);
983 len = (char *)p - (char *)xdr->p;
990 EXPORT_SYMBOL_GPL(xdr_init_encode);
993 * xdr_init_encode_pages - Initialize an xdr_stream for encoding into pages
994 * @xdr: pointer to xdr_stream struct
995 * @buf: pointer to XDR buffer into which to encode data
996 * @pages: list of pages to decode into
997 * @rqst: pointer to controlling rpc_rqst, for debugging
1000 void xdr_init_encode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
1001 struct page **pages, struct rpc_rqst *rqst)
1003 xdr_reset_scratch_buffer(xdr);
1006 xdr->page_ptr = pages;
1008 xdr->p = page_address(*pages);
1009 xdr->end = (void *)xdr->p + min_t(u32, buf->buflen, PAGE_SIZE);
1012 EXPORT_SYMBOL_GPL(xdr_init_encode_pages);
1015 * __xdr_commit_encode - Ensure all data is written to buffer
1016 * @xdr: pointer to xdr_stream
1018 * We handle encoding across page boundaries by giving the caller a
1019 * temporary location to write to, then later copying the data into
1020 * place; xdr_commit_encode does that copying.
1022 * Normally the caller doesn't need to call this directly, as the
1023 * following xdr_reserve_space will do it. But an explicit call may be
1024 * required at the end of encoding, or any other time when the xdr_buf
1025 * data might be read.
1027 void __xdr_commit_encode(struct xdr_stream *xdr)
1029 size_t shift = xdr->scratch.iov_len;
1032 page = page_address(*xdr->page_ptr);
1033 memcpy(xdr->scratch.iov_base, page, shift);
1034 memmove(page, page + shift, (void *)xdr->p - page);
1035 xdr_reset_scratch_buffer(xdr);
1037 EXPORT_SYMBOL_GPL(__xdr_commit_encode);
1040 * The buffer space to be reserved crosses the boundary between
1041 * xdr->buf->head and xdr->buf->pages, or between two pages
1042 * in xdr->buf->pages.
1044 static noinline __be32 *xdr_get_next_encode_buffer(struct xdr_stream *xdr,
1048 int frag1bytes, frag2bytes;
1051 if (nbytes > PAGE_SIZE)
1052 goto out_overflow; /* Bigger buffers require special handling */
1053 if (xdr->buf->len + nbytes > xdr->buf->buflen)
1054 goto out_overflow; /* Sorry, we're totally out of space */
1055 frag1bytes = (xdr->end - xdr->p) << 2;
1056 frag2bytes = nbytes - frag1bytes;
1058 xdr->iov->iov_len += frag1bytes;
1060 xdr->buf->page_len += frag1bytes;
1065 * If the last encode didn't end exactly on a page boundary, the
1066 * next one will straddle boundaries. Encode into the next
1067 * page, then copy it back later in xdr_commit_encode. We use
1068 * the "scratch" iov to track any temporarily unused fragment of
1069 * space at the end of the previous buffer:
1071 xdr_set_scratch_buffer(xdr, xdr->p, frag1bytes);
1074 * xdr->p is where the next encode will start after
1075 * xdr_commit_encode() has shifted this one back:
1077 p = page_address(*xdr->page_ptr);
1078 xdr->p = p + frag2bytes;
1079 space_left = xdr->buf->buflen - xdr->buf->len;
1080 if (space_left - frag1bytes >= PAGE_SIZE)
1081 xdr->end = p + PAGE_SIZE;
1083 xdr->end = p + space_left - frag1bytes;
1085 xdr->buf->page_len += frag2bytes;
1086 xdr->buf->len += nbytes;
1089 trace_rpc_xdr_overflow(xdr, nbytes);
1094 * xdr_reserve_space - Reserve buffer space for sending
1095 * @xdr: pointer to xdr_stream
1096 * @nbytes: number of bytes to reserve
1098 * Checks that we have enough buffer space to encode 'nbytes' more
1099 * bytes of data. If so, update the total xdr_buf length, and
1100 * adjust the length of the current kvec.
1102 * The returned pointer is valid only until the next call to
1103 * xdr_reserve_space() or xdr_commit_encode() on @xdr. The current
1104 * implementation of this API guarantees that space reserved for a
1105 * four-byte data item remains valid until @xdr is destroyed, but
1106 * that might not always be true in the future.
1108 __be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
1113 xdr_commit_encode(xdr);
1114 /* align nbytes on the next 32-bit boundary */
1117 q = p + (nbytes >> 2);
1118 if (unlikely(q > xdr->end || q < p))
1119 return xdr_get_next_encode_buffer(xdr, nbytes);
1122 xdr->iov->iov_len += nbytes;
1124 xdr->buf->page_len += nbytes;
1125 xdr->buf->len += nbytes;
1128 EXPORT_SYMBOL_GPL(xdr_reserve_space);
1131 * xdr_reserve_space_vec - Reserves a large amount of buffer space for sending
1132 * @xdr: pointer to xdr_stream
1133 * @nbytes: number of bytes to reserve
1135 * The size argument passed to xdr_reserve_space() is determined based
1136 * on the number of bytes remaining in the current page to avoid
1137 * invalidating iov_base pointers when xdr_commit_encode() is called.
1141 * %-EMSGSIZE: not enough space is available in @xdr
1143 int xdr_reserve_space_vec(struct xdr_stream *xdr, size_t nbytes)
1149 * svcrdma requires every READ payload to start somewhere
1152 if (xdr->iov == xdr->buf->head) {
1157 /* XXX: Let's find a way to make this more efficient */
1159 thislen = xdr->buf->page_len % PAGE_SIZE;
1160 thislen = min_t(size_t, nbytes, PAGE_SIZE - thislen);
1162 p = xdr_reserve_space(xdr, thislen);
1171 EXPORT_SYMBOL_GPL(xdr_reserve_space_vec);
1174 * xdr_truncate_encode - truncate an encode buffer
1175 * @xdr: pointer to xdr_stream
1176 * @len: new length of buffer
1178 * Truncates the xdr stream, so that xdr->buf->len == len,
1179 * and xdr->p points at offset len from the start of the buffer, and
1180 * head, tail, and page lengths are adjusted to correspond.
1182 * If this means moving xdr->p to a different buffer, we assume that
1183 * the end pointer should be set to the end of the current page,
1184 * except in the case of the head buffer when we assume the head
1185 * buffer's current length represents the end of the available buffer.
1187 * This is *not* safe to use on a buffer that already has inlined page
1188 * cache pages (as in a zero-copy server read reply), except for the
1189 * simple case of truncating from one position in the tail to another.
1192 void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
1194 struct xdr_buf *buf = xdr->buf;
1195 struct kvec *head = buf->head;
1196 struct kvec *tail = buf->tail;
1200 if (len > buf->len) {
1204 xdr_commit_encode(xdr);
1206 fraglen = min_t(int, buf->len - len, tail->iov_len);
1207 tail->iov_len -= fraglen;
1208 buf->len -= fraglen;
1209 if (tail->iov_len) {
1210 xdr->p = tail->iov_base + tail->iov_len;
1211 WARN_ON_ONCE(!xdr->end);
1212 WARN_ON_ONCE(!xdr->iov);
1215 WARN_ON_ONCE(fraglen);
1216 fraglen = min_t(int, buf->len - len, buf->page_len);
1217 buf->page_len -= fraglen;
1218 buf->len -= fraglen;
1220 new = buf->page_base + buf->page_len;
1222 xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
1224 if (buf->page_len) {
1225 xdr->p = page_address(*xdr->page_ptr);
1226 xdr->end = (void *)xdr->p + PAGE_SIZE;
1227 xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
1228 WARN_ON_ONCE(xdr->iov);
1232 xdr->end = head->iov_base + head->iov_len;
1233 /* (otherwise assume xdr->end is already set) */
1235 head->iov_len = len;
1237 xdr->p = head->iov_base + head->iov_len;
1238 xdr->iov = buf->head;
1240 EXPORT_SYMBOL(xdr_truncate_encode);
1243 * xdr_truncate_decode - Truncate a decoding stream
1244 * @xdr: pointer to struct xdr_stream
1245 * @len: Number of bytes to remove
1248 void xdr_truncate_decode(struct xdr_stream *xdr, size_t len)
1250 unsigned int nbytes = xdr_align_size(len);
1252 xdr->buf->len -= nbytes;
1253 xdr->nwords -= XDR_QUADLEN(nbytes);
1255 EXPORT_SYMBOL_GPL(xdr_truncate_decode);
1258 * xdr_restrict_buflen - decrease available buffer space
1259 * @xdr: pointer to xdr_stream
1260 * @newbuflen: new maximum number of bytes available
1262 * Adjust our idea of how much space is available in the buffer.
1263 * If we've already used too much space in the buffer, returns -1.
1264 * If the available space is already smaller than newbuflen, returns 0
1265 * and does nothing. Otherwise, adjusts xdr->buf->buflen to newbuflen
1266 * and ensures xdr->end is set at most offset newbuflen from the start
1269 int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
1271 struct xdr_buf *buf = xdr->buf;
1272 int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
1273 int end_offset = buf->len + left_in_this_buf;
1275 if (newbuflen < 0 || newbuflen < buf->len)
1277 if (newbuflen > buf->buflen)
1279 if (newbuflen < end_offset)
1280 xdr->end = (void *)xdr->end + newbuflen - end_offset;
1281 buf->buflen = newbuflen;
1284 EXPORT_SYMBOL(xdr_restrict_buflen);
1287 * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
1288 * @xdr: pointer to xdr_stream
1289 * @pages: array of pages to insert
1290 * @base: starting offset of first data byte in @pages
1291 * @len: number of data bytes in @pages to insert
1293 * After the @pages are added, the tail iovec is instantiated pointing to
1294 * end of the head buffer, and the stream is set up to encode subsequent
1295 * items into the tail.
1297 void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
1300 struct xdr_buf *buf = xdr->buf;
1301 struct kvec *tail = buf->tail;
1304 buf->page_base = base;
1305 buf->page_len = len;
1307 tail->iov_base = xdr->p;
1312 unsigned int pad = 4 - (len & 3);
1314 BUG_ON(xdr->p >= xdr->end);
1315 tail->iov_base = (char *)xdr->p + (len & 3);
1316 tail->iov_len += pad;
1323 EXPORT_SYMBOL_GPL(xdr_write_pages);
1325 static unsigned int xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
1326 unsigned int base, unsigned int len)
1328 if (len > iov->iov_len)
1330 if (unlikely(base > len))
1332 xdr->p = (__be32*)(iov->iov_base + base);
1333 xdr->end = (__be32*)(iov->iov_base + len);
1335 xdr->page_ptr = NULL;
1339 static unsigned int xdr_set_tail_base(struct xdr_stream *xdr,
1340 unsigned int base, unsigned int len)
1342 struct xdr_buf *buf = xdr->buf;
1344 xdr_stream_set_pos(xdr, base + buf->page_len + buf->head->iov_len);
1345 return xdr_set_iov(xdr, buf->tail, base, len);
1348 static void xdr_stream_unmap_current_page(struct xdr_stream *xdr)
1350 if (xdr->page_kaddr) {
1351 kunmap_local(xdr->page_kaddr);
1352 xdr->page_kaddr = NULL;
1356 static unsigned int xdr_set_page_base(struct xdr_stream *xdr,
1357 unsigned int base, unsigned int len)
1360 unsigned int maxlen;
1365 maxlen = xdr->buf->page_len;
1373 xdr_stream_unmap_current_page(xdr);
1374 xdr_stream_page_set_pos(xdr, base);
1375 base += xdr->buf->page_base;
1377 pgnr = base >> PAGE_SHIFT;
1378 xdr->page_ptr = &xdr->buf->pages[pgnr];
1380 if (PageHighMem(*xdr->page_ptr)) {
1381 xdr->page_kaddr = kmap_local_page(*xdr->page_ptr);
1382 kaddr = xdr->page_kaddr;
1384 kaddr = page_address(*xdr->page_ptr);
1386 pgoff = base & ~PAGE_MASK;
1387 xdr->p = (__be32*)(kaddr + pgoff);
1389 pgend = pgoff + len;
1390 if (pgend > PAGE_SIZE)
1392 xdr->end = (__be32*)(kaddr + pgend);
1397 static void xdr_set_page(struct xdr_stream *xdr, unsigned int base,
1400 if (xdr_set_page_base(xdr, base, len) == 0) {
1401 base -= xdr->buf->page_len;
1402 xdr_set_tail_base(xdr, base, len);
1406 static void xdr_set_next_page(struct xdr_stream *xdr)
1408 unsigned int newbase;
1410 newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
1411 newbase -= xdr->buf->page_base;
1412 if (newbase < xdr->buf->page_len)
1413 xdr_set_page_base(xdr, newbase, xdr_stream_remaining(xdr));
1415 xdr_set_tail_base(xdr, 0, xdr_stream_remaining(xdr));
1418 static bool xdr_set_next_buffer(struct xdr_stream *xdr)
1420 if (xdr->page_ptr != NULL)
1421 xdr_set_next_page(xdr);
1422 else if (xdr->iov == xdr->buf->head)
1423 xdr_set_page(xdr, 0, xdr_stream_remaining(xdr));
1424 return xdr->p != xdr->end;
1428 * xdr_init_decode - Initialize an xdr_stream for decoding data.
1429 * @xdr: pointer to xdr_stream struct
1430 * @buf: pointer to XDR buffer from which to decode data
1431 * @p: current pointer inside XDR buffer
1432 * @rqst: pointer to controlling rpc_rqst, for debugging
1434 void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
1435 struct rpc_rqst *rqst)
1438 xdr->page_kaddr = NULL;
1439 xdr_reset_scratch_buffer(xdr);
1440 xdr->nwords = XDR_QUADLEN(buf->len);
1441 if (xdr_set_iov(xdr, buf->head, 0, buf->len) == 0 &&
1442 xdr_set_page_base(xdr, 0, buf->len) == 0)
1443 xdr_set_iov(xdr, buf->tail, 0, buf->len);
1444 if (p != NULL && p > xdr->p && xdr->end >= p) {
1445 xdr->nwords -= p - xdr->p;
1450 EXPORT_SYMBOL_GPL(xdr_init_decode);
1453 * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages
1454 * @xdr: pointer to xdr_stream struct
1455 * @buf: pointer to XDR buffer from which to decode data
1456 * @pages: list of pages to decode into
1457 * @len: length in bytes of buffer in pages
1459 void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
1460 struct page **pages, unsigned int len)
1462 memset(buf, 0, sizeof(*buf));
1464 buf->page_len = len;
1467 xdr_init_decode(xdr, buf, NULL, NULL);
1469 EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
1472 * xdr_finish_decode - Clean up the xdr_stream after decoding data.
1473 * @xdr: pointer to xdr_stream struct
1475 void xdr_finish_decode(struct xdr_stream *xdr)
1477 xdr_stream_unmap_current_page(xdr);
1479 EXPORT_SYMBOL(xdr_finish_decode);
1481 static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1483 unsigned int nwords = XDR_QUADLEN(nbytes);
1485 __be32 *q = p + nwords;
1487 if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
1490 xdr->nwords -= nwords;
1494 static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
1497 char *cpdest = xdr->scratch.iov_base;
1498 size_t cplen = (char *)xdr->end - (char *)xdr->p;
1500 if (nbytes > xdr->scratch.iov_len)
1502 p = __xdr_inline_decode(xdr, cplen);
1505 memcpy(cpdest, p, cplen);
1506 if (!xdr_set_next_buffer(xdr))
1510 p = __xdr_inline_decode(xdr, nbytes);
1513 memcpy(cpdest, p, nbytes);
1514 return xdr->scratch.iov_base;
1516 trace_rpc_xdr_overflow(xdr, nbytes);
1521 * xdr_inline_decode - Retrieve XDR data to decode
1522 * @xdr: pointer to xdr_stream struct
1523 * @nbytes: number of bytes of data to decode
1525 * Check if the input buffer is long enough to enable us to decode
1526 * 'nbytes' more bytes of data starting at the current position.
1527 * If so return the current pointer, then update the current
1530 __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1534 if (unlikely(nbytes == 0))
1536 if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1538 p = __xdr_inline_decode(xdr, nbytes);
1541 return xdr_copy_to_scratch(xdr, nbytes);
1543 trace_rpc_xdr_overflow(xdr, nbytes);
1546 EXPORT_SYMBOL_GPL(xdr_inline_decode);
1548 static void xdr_realign_pages(struct xdr_stream *xdr)
1550 struct xdr_buf *buf = xdr->buf;
1551 struct kvec *iov = buf->head;
1552 unsigned int cur = xdr_stream_pos(xdr);
1553 unsigned int copied;
1555 /* Realign pages to current pointer position */
1556 if (iov->iov_len > cur) {
1557 copied = xdr_shrink_bufhead(buf, cur);
1558 trace_rpc_xdr_alignment(xdr, cur, copied);
1559 xdr_set_page(xdr, 0, buf->page_len);
1563 static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
1565 struct xdr_buf *buf = xdr->buf;
1566 unsigned int nwords = XDR_QUADLEN(len);
1567 unsigned int copied;
1569 if (xdr->nwords == 0)
1572 xdr_realign_pages(xdr);
1573 if (nwords > xdr->nwords) {
1574 nwords = xdr->nwords;
1577 if (buf->page_len <= len)
1578 len = buf->page_len;
1579 else if (nwords < xdr->nwords) {
1580 /* Truncate page data and move it into the tail */
1581 copied = xdr_shrink_pagelen(buf, len);
1582 trace_rpc_xdr_alignment(xdr, len, copied);
1588 * xdr_read_pages - align page-based XDR data to current pointer position
1589 * @xdr: pointer to xdr_stream struct
1590 * @len: number of bytes of page data
1592 * Moves data beyond the current pointer position from the XDR head[] buffer
1593 * into the page list. Any data that lies beyond current position + @len
1594 * bytes is moved into the XDR tail[]. The xdr_stream current position is
1595 * then advanced past that data to align to the next XDR object in the tail.
1597 * Returns the number of XDR encoded bytes now contained in the pages
1599 unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
1601 unsigned int nwords = XDR_QUADLEN(len);
1602 unsigned int base, end, pglen;
1604 pglen = xdr_align_pages(xdr, nwords << 2);
1608 base = (nwords << 2) - pglen;
1609 end = xdr_stream_remaining(xdr) - pglen;
1611 xdr_set_tail_base(xdr, base, end);
1612 return len <= pglen ? len : pglen;
1614 EXPORT_SYMBOL_GPL(xdr_read_pages);
1617 * xdr_set_pagelen - Sets the length of the XDR pages
1618 * @xdr: pointer to xdr_stream struct
1619 * @len: new length of the XDR page data
1621 * Either grows or shrinks the length of the xdr pages by setting pagelen to
1622 * @len bytes. When shrinking, any extra data is moved into buf->tail, whereas
1623 * when growing any data beyond the current pointer is moved into the tail.
1625 * Returns True if the operation was successful, and False otherwise.
1627 void xdr_set_pagelen(struct xdr_stream *xdr, unsigned int len)
1629 struct xdr_buf *buf = xdr->buf;
1630 size_t remaining = xdr_stream_remaining(xdr);
1633 if (len < buf->page_len) {
1634 base = buf->page_len - len;
1635 xdr_shrink_pagelen(buf, len);
1637 xdr_buf_head_shift_right(buf, xdr_stream_pos(xdr),
1638 buf->page_len, remaining);
1639 if (len > buf->page_len)
1640 xdr_buf_try_expand(buf, len - buf->page_len);
1642 xdr_set_tail_base(xdr, base, remaining);
1644 EXPORT_SYMBOL_GPL(xdr_set_pagelen);
1647 * xdr_enter_page - decode data from the XDR page
1648 * @xdr: pointer to xdr_stream struct
1649 * @len: number of bytes of page data
1651 * Moves data beyond the current pointer position from the XDR head[] buffer
1652 * into the page list. Any data that lies beyond current position + "len"
1653 * bytes is moved into the XDR tail[]. The current pointer is then
1654 * repositioned at the beginning of the first XDR page.
1656 void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
1658 len = xdr_align_pages(xdr, len);
1660 * Position current pointer at beginning of tail, and
1661 * set remaining message length.
1664 xdr_set_page_base(xdr, 0, len);
1666 EXPORT_SYMBOL_GPL(xdr_enter_page);
1668 static const struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
1670 void xdr_buf_from_iov(const struct kvec *iov, struct xdr_buf *buf)
1672 buf->head[0] = *iov;
1673 buf->tail[0] = empty_iov;
1675 buf->buflen = buf->len = iov->iov_len;
1677 EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
1680 * xdr_buf_subsegment - set subbuf to a portion of buf
1681 * @buf: an xdr buffer
1682 * @subbuf: the result buffer
1683 * @base: beginning of range in bytes
1684 * @len: length of range in bytes
1686 * sets @subbuf to an xdr buffer representing the portion of @buf of
1687 * length @len starting at offset @base.
1689 * @buf and @subbuf may be pointers to the same struct xdr_buf.
1691 * Returns -1 if base or length are out of bounds.
1693 int xdr_buf_subsegment(const struct xdr_buf *buf, struct xdr_buf *subbuf,
1694 unsigned int base, unsigned int len)
1696 subbuf->buflen = subbuf->len = len;
1697 if (base < buf->head[0].iov_len) {
1698 subbuf->head[0].iov_base = buf->head[0].iov_base + base;
1699 subbuf->head[0].iov_len = min_t(unsigned int, len,
1700 buf->head[0].iov_len - base);
1701 len -= subbuf->head[0].iov_len;
1704 base -= buf->head[0].iov_len;
1705 subbuf->head[0].iov_base = buf->head[0].iov_base;
1706 subbuf->head[0].iov_len = 0;
1709 if (base < buf->page_len) {
1710 subbuf->page_len = min(buf->page_len - base, len);
1711 base += buf->page_base;
1712 subbuf->page_base = base & ~PAGE_MASK;
1713 subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
1714 len -= subbuf->page_len;
1717 base -= buf->page_len;
1718 subbuf->pages = buf->pages;
1719 subbuf->page_base = 0;
1720 subbuf->page_len = 0;
1723 if (base < buf->tail[0].iov_len) {
1724 subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
1725 subbuf->tail[0].iov_len = min_t(unsigned int, len,
1726 buf->tail[0].iov_len - base);
1727 len -= subbuf->tail[0].iov_len;
1730 base -= buf->tail[0].iov_len;
1731 subbuf->tail[0].iov_base = buf->tail[0].iov_base;
1732 subbuf->tail[0].iov_len = 0;
1739 EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
1742 * xdr_stream_subsegment - set @subbuf to a portion of @xdr
1743 * @xdr: an xdr_stream set up for decoding
1744 * @subbuf: the result buffer
1745 * @nbytes: length of @xdr to extract, in bytes
1747 * Sets up @subbuf to represent a portion of @xdr. The portion
1748 * starts at the current offset in @xdr, and extends for a length
1749 * of @nbytes. If this is successful, @xdr is advanced to the next
1750 * XDR data item following that portion.
1753 * %true: @subbuf has been initialized, and @xdr has been advanced.
1754 * %false: a bounds error has occurred
1756 bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
1757 unsigned int nbytes)
1759 unsigned int start = xdr_stream_pos(xdr);
1760 unsigned int remaining, len;
1762 /* Extract @subbuf and bounds-check the fn arguments */
1763 if (xdr_buf_subsegment(xdr->buf, subbuf, start, nbytes))
1766 /* Advance @xdr by @nbytes */
1767 for (remaining = nbytes; remaining;) {
1768 if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1771 len = (char *)xdr->end - (char *)xdr->p;
1772 if (remaining <= len) {
1773 xdr->p = (__be32 *)((char *)xdr->p +
1774 (remaining + xdr_pad_size(nbytes)));
1778 xdr->p = (__be32 *)((char *)xdr->p + len);
1783 xdr_stream_set_pos(xdr, start + nbytes);
1786 EXPORT_SYMBOL_GPL(xdr_stream_subsegment);
1789 * xdr_stream_move_subsegment - Move part of a stream to another position
1790 * @xdr: the source xdr_stream
1791 * @offset: the source offset of the segment
1792 * @target: the target offset of the segment
1793 * @length: the number of bytes to move
1795 * Moves @length bytes from @offset to @target in the xdr_stream, overwriting
1796 * anything in its space. Returns the number of bytes in the segment.
1798 unsigned int xdr_stream_move_subsegment(struct xdr_stream *xdr, unsigned int offset,
1799 unsigned int target, unsigned int length)
1804 if (offset < target) {
1805 shift = target - offset;
1806 if (xdr_buf_subsegment(xdr->buf, &buf, offset, shift + length) < 0)
1808 xdr_buf_head_shift_right(&buf, 0, length, shift);
1809 } else if (offset > target) {
1810 shift = offset - target;
1811 if (xdr_buf_subsegment(xdr->buf, &buf, target, shift + length) < 0)
1813 xdr_buf_head_shift_left(&buf, shift, length, shift);
1817 EXPORT_SYMBOL_GPL(xdr_stream_move_subsegment);
1820 * xdr_stream_zero - zero out a portion of an xdr_stream
1821 * @xdr: an xdr_stream to zero out
1822 * @offset: the starting point in the stream
1823 * @length: the number of bytes to zero
1825 unsigned int xdr_stream_zero(struct xdr_stream *xdr, unsigned int offset,
1826 unsigned int length)
1830 if (xdr_buf_subsegment(xdr->buf, &buf, offset, length) < 0)
1832 if (buf.head[0].iov_len)
1833 xdr_buf_iov_zero(buf.head, 0, buf.head[0].iov_len);
1834 if (buf.page_len > 0)
1835 xdr_buf_pages_zero(&buf, 0, buf.page_len);
1836 if (buf.tail[0].iov_len)
1837 xdr_buf_iov_zero(buf.tail, 0, buf.tail[0].iov_len);
1840 EXPORT_SYMBOL_GPL(xdr_stream_zero);
1843 * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
1844 * @buf: buf to be trimmed
1845 * @len: number of bytes to reduce "buf" by
1847 * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
1848 * that it's possible that we'll trim less than that amount if the xdr_buf is
1849 * too small, or if (for instance) it's all in the head and the parser has
1850 * already read too far into it.
1852 void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
1855 unsigned int trim = len;
1857 if (buf->tail[0].iov_len) {
1858 cur = min_t(size_t, buf->tail[0].iov_len, trim);
1859 buf->tail[0].iov_len -= cur;
1865 if (buf->page_len) {
1866 cur = min_t(unsigned int, buf->page_len, trim);
1867 buf->page_len -= cur;
1873 if (buf->head[0].iov_len) {
1874 cur = min_t(size_t, buf->head[0].iov_len, trim);
1875 buf->head[0].iov_len -= cur;
1879 buf->len -= (len - trim);
1881 EXPORT_SYMBOL_GPL(xdr_buf_trim);
1883 static void __read_bytes_from_xdr_buf(const struct xdr_buf *subbuf,
1884 void *obj, unsigned int len)
1886 unsigned int this_len;
1888 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1889 memcpy(obj, subbuf->head[0].iov_base, this_len);
1892 this_len = min_t(unsigned int, len, subbuf->page_len);
1893 _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
1896 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1897 memcpy(obj, subbuf->tail[0].iov_base, this_len);
1900 /* obj is assumed to point to allocated memory of size at least len: */
1901 int read_bytes_from_xdr_buf(const struct xdr_buf *buf, unsigned int base,
1902 void *obj, unsigned int len)
1904 struct xdr_buf subbuf;
1907 status = xdr_buf_subsegment(buf, &subbuf, base, len);
1910 __read_bytes_from_xdr_buf(&subbuf, obj, len);
1913 EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
1915 static void __write_bytes_to_xdr_buf(const struct xdr_buf *subbuf,
1916 void *obj, unsigned int len)
1918 unsigned int this_len;
1920 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1921 memcpy(subbuf->head[0].iov_base, obj, this_len);
1924 this_len = min_t(unsigned int, len, subbuf->page_len);
1925 _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
1928 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1929 memcpy(subbuf->tail[0].iov_base, obj, this_len);
1932 /* obj is assumed to point to allocated memory of size at least len: */
1933 int write_bytes_to_xdr_buf(const struct xdr_buf *buf, unsigned int base,
1934 void *obj, unsigned int len)
1936 struct xdr_buf subbuf;
1939 status = xdr_buf_subsegment(buf, &subbuf, base, len);
1942 __write_bytes_to_xdr_buf(&subbuf, obj, len);
1945 EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
1947 int xdr_decode_word(const struct xdr_buf *buf, unsigned int base, u32 *obj)
1952 status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
1955 *obj = be32_to_cpu(raw);
1958 EXPORT_SYMBOL_GPL(xdr_decode_word);
1960 int xdr_encode_word(const struct xdr_buf *buf, unsigned int base, u32 obj)
1962 __be32 raw = cpu_to_be32(obj);
1964 return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
1966 EXPORT_SYMBOL_GPL(xdr_encode_word);
1968 /* Returns 0 on success, or else a negative error code. */
1969 static int xdr_xcode_array2(const struct xdr_buf *buf, unsigned int base,
1970 struct xdr_array2_desc *desc, int encode)
1972 char *elem = NULL, *c;
1973 unsigned int copied = 0, todo, avail_here;
1974 struct page **ppages = NULL;
1978 if (xdr_encode_word(buf, base, desc->array_len) != 0)
1981 if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
1982 desc->array_len > desc->array_maxlen ||
1983 (unsigned long) base + 4 + desc->array_len *
1984 desc->elem_size > buf->len)
1992 todo = desc->array_len * desc->elem_size;
1995 if (todo && base < buf->head->iov_len) {
1996 c = buf->head->iov_base + base;
1997 avail_here = min_t(unsigned int, todo,
1998 buf->head->iov_len - base);
2001 while (avail_here >= desc->elem_size) {
2002 err = desc->xcode(desc, c);
2005 c += desc->elem_size;
2006 avail_here -= desc->elem_size;
2010 elem = kmalloc(desc->elem_size, GFP_KERNEL);
2016 err = desc->xcode(desc, elem);
2019 memcpy(c, elem, avail_here);
2021 memcpy(elem, c, avail_here);
2022 copied = avail_here;
2024 base = buf->head->iov_len; /* align to start of pages */
2027 /* process pages array */
2028 base -= buf->head->iov_len;
2029 if (todo && base < buf->page_len) {
2030 unsigned int avail_page;
2032 avail_here = min(todo, buf->page_len - base);
2035 base += buf->page_base;
2036 ppages = buf->pages + (base >> PAGE_SHIFT);
2038 avail_page = min_t(unsigned int, PAGE_SIZE - base,
2040 c = kmap(*ppages) + base;
2042 while (avail_here) {
2043 avail_here -= avail_page;
2044 if (copied || avail_page < desc->elem_size) {
2045 unsigned int l = min(avail_page,
2046 desc->elem_size - copied);
2048 elem = kmalloc(desc->elem_size,
2056 err = desc->xcode(desc, elem);
2060 memcpy(c, elem + copied, l);
2062 if (copied == desc->elem_size)
2065 memcpy(elem + copied, c, l);
2067 if (copied == desc->elem_size) {
2068 err = desc->xcode(desc, elem);
2077 while (avail_page >= desc->elem_size) {
2078 err = desc->xcode(desc, c);
2081 c += desc->elem_size;
2082 avail_page -= desc->elem_size;
2085 unsigned int l = min(avail_page,
2086 desc->elem_size - copied);
2088 elem = kmalloc(desc->elem_size,
2096 err = desc->xcode(desc, elem);
2100 memcpy(c, elem + copied, l);
2102 if (copied == desc->elem_size)
2105 memcpy(elem + copied, c, l);
2107 if (copied == desc->elem_size) {
2108 err = desc->xcode(desc, elem);
2121 avail_page = min(avail_here,
2122 (unsigned int) PAGE_SIZE);
2124 base = buf->page_len; /* align to start of tail */
2128 base -= buf->page_len;
2130 c = buf->tail->iov_base + base;
2132 unsigned int l = desc->elem_size - copied;
2135 memcpy(c, elem + copied, l);
2137 memcpy(elem + copied, c, l);
2138 err = desc->xcode(desc, elem);
2146 err = desc->xcode(desc, c);
2149 c += desc->elem_size;
2150 todo -= desc->elem_size;
2162 int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base,
2163 struct xdr_array2_desc *desc)
2165 if (base >= buf->len)
2168 return xdr_xcode_array2(buf, base, desc, 0);
2170 EXPORT_SYMBOL_GPL(xdr_decode_array2);
2172 int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base,
2173 struct xdr_array2_desc *desc)
2175 if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
2176 buf->head->iov_len + buf->page_len + buf->tail->iov_len)
2179 return xdr_xcode_array2(buf, base, desc, 1);
2181 EXPORT_SYMBOL_GPL(xdr_encode_array2);
2183 int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset,
2185 int (*actor)(struct scatterlist *, void *), void *data)
2188 unsigned int page_len, thislen, page_offset;
2189 struct scatterlist sg[1];
2191 sg_init_table(sg, 1);
2193 if (offset >= buf->head[0].iov_len) {
2194 offset -= buf->head[0].iov_len;
2196 thislen = buf->head[0].iov_len - offset;
2199 sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
2200 ret = actor(sg, data);
2209 if (offset >= buf->page_len) {
2210 offset -= buf->page_len;
2212 page_len = buf->page_len - offset;
2216 page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
2217 i = (offset + buf->page_base) >> PAGE_SHIFT;
2218 thislen = PAGE_SIZE - page_offset;
2220 if (thislen > page_len)
2222 sg_set_page(sg, buf->pages[i], thislen, page_offset);
2223 ret = actor(sg, data);
2226 page_len -= thislen;
2229 thislen = PAGE_SIZE;
2230 } while (page_len != 0);
2235 if (offset < buf->tail[0].iov_len) {
2236 thislen = buf->tail[0].iov_len - offset;
2239 sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
2240 ret = actor(sg, data);
2248 EXPORT_SYMBOL_GPL(xdr_process_buf);
2251 * xdr_stream_decode_opaque - Decode variable length opaque
2252 * @xdr: pointer to xdr_stream
2253 * @ptr: location to store opaque data
2254 * @size: size of storage buffer @ptr
2257 * On success, returns size of object stored in *@ptr
2258 * %-EBADMSG on XDR buffer overflow
2259 * %-EMSGSIZE on overflow of storage buffer @ptr
2261 ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, size_t size)
2266 ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
2269 memcpy(ptr, p, ret);
2272 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque);
2275 * xdr_stream_decode_opaque_dup - Decode and duplicate variable length opaque
2276 * @xdr: pointer to xdr_stream
2277 * @ptr: location to store pointer to opaque data
2278 * @maxlen: maximum acceptable object size
2279 * @gfp_flags: GFP mask to use
2282 * On success, returns size of object stored in *@ptr
2283 * %-EBADMSG on XDR buffer overflow
2284 * %-EMSGSIZE if the size of the object would exceed @maxlen
2285 * %-ENOMEM on memory allocation failure
2287 ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
2288 size_t maxlen, gfp_t gfp_flags)
2293 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2295 *ptr = kmemdup(p, ret, gfp_flags);
2303 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_dup);
2306 * xdr_stream_decode_string - Decode variable length string
2307 * @xdr: pointer to xdr_stream
2308 * @str: location to store string
2309 * @size: size of storage buffer @str
2312 * On success, returns length of NUL-terminated string stored in *@str
2313 * %-EBADMSG on XDR buffer overflow
2314 * %-EMSGSIZE on overflow of storage buffer @str
2316 ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, size_t size)
2321 ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
2323 memcpy(str, p, ret);
2330 EXPORT_SYMBOL_GPL(xdr_stream_decode_string);
2333 * xdr_stream_decode_string_dup - Decode and duplicate variable length string
2334 * @xdr: pointer to xdr_stream
2335 * @str: location to store pointer to string
2336 * @maxlen: maximum acceptable string length
2337 * @gfp_flags: GFP mask to use
2340 * On success, returns length of NUL-terminated string stored in *@ptr
2341 * %-EBADMSG on XDR buffer overflow
2342 * %-EMSGSIZE if the size of the string would exceed @maxlen
2343 * %-ENOMEM on memory allocation failure
2345 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
2346 size_t maxlen, gfp_t gfp_flags)
2351 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2353 char *s = kmemdup_nul(p, ret, gfp_flags);
2363 EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);
2366 * xdr_stream_decode_opaque_auth - Decode struct opaque_auth (RFC5531 S8.2)
2367 * @xdr: pointer to xdr_stream
2368 * @flavor: location to store decoded flavor
2369 * @body: location to store decode body
2370 * @body_len: location to store length of decoded body
2373 * On success, returns the number of buffer bytes consumed
2374 * %-EBADMSG on XDR buffer overflow
2375 * %-EMSGSIZE if the decoded size of the body field exceeds 400 octets
2377 ssize_t xdr_stream_decode_opaque_auth(struct xdr_stream *xdr, u32 *flavor,
2378 void **body, unsigned int *body_len)
2382 len = xdr_stream_decode_u32(xdr, flavor);
2383 if (unlikely(len < 0))
2385 ret = xdr_stream_decode_opaque_inline(xdr, body, RPC_MAX_AUTH_SIZE);
2386 if (unlikely(ret < 0))
2391 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_auth);
2394 * xdr_stream_encode_opaque_auth - Encode struct opaque_auth (RFC5531 S8.2)
2395 * @xdr: pointer to xdr_stream
2396 * @flavor: verifier flavor to encode
2397 * @body: content of body to encode
2398 * @body_len: length of body to encode
2401 * On success, returns length in bytes of XDR buffer consumed
2402 * %-EBADMSG on XDR buffer overflow
2403 * %-EMSGSIZE if the size of @body exceeds 400 octets
2405 ssize_t xdr_stream_encode_opaque_auth(struct xdr_stream *xdr, u32 flavor,
2406 void *body, unsigned int body_len)
2410 if (unlikely(body_len > RPC_MAX_AUTH_SIZE))
2412 len = xdr_stream_encode_u32(xdr, flavor);
2413 if (unlikely(len < 0))
2415 ret = xdr_stream_encode_opaque(xdr, body, body_len);
2416 if (unlikely(ret < 0))
2420 EXPORT_SYMBOL_GPL(xdr_stream_encode_opaque_auth);