1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/xdr.c
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_inline_pages - Prepare receive buffer for a large reply
169 * @xdr: xdr_buf into which reply will be placed
170 * @offset: expected offset where data payload will start, in bytes
171 * @pages: vector of struct page pointers
172 * @base: offset in first page where receive should start, in bytes
173 * @len: expected size of the upper layer data payload, in bytes
177 xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
178 struct page **pages, unsigned int base, unsigned int len)
180 struct kvec *head = xdr->head;
181 struct kvec *tail = xdr->tail;
182 char *buf = (char *)head->iov_base;
183 unsigned int buflen = head->iov_len;
185 head->iov_len = offset;
188 xdr->page_base = base;
191 tail->iov_base = buf + offset;
192 tail->iov_len = buflen - offset;
195 EXPORT_SYMBOL_GPL(xdr_inline_pages);
198 * Helper routines for doing 'memmove' like operations on a struct xdr_buf
202 * _shift_data_left_pages
203 * @pages: vector of pages containing both the source and dest memory area.
204 * @pgto_base: page vector address of destination
205 * @pgfrom_base: page vector address of source
206 * @len: number of bytes to copy
208 * Note: the addresses pgto_base and pgfrom_base are both calculated in
210 * if a memory area starts at byte 'base' in page 'pages[i]',
211 * then its address is given as (i << PAGE_CACHE_SHIFT) + base
212 * Alse note: pgto_base must be < pgfrom_base, but the memory areas
213 * they point to may overlap.
216 _shift_data_left_pages(struct page **pages, size_t pgto_base,
217 size_t pgfrom_base, size_t len)
219 struct page **pgfrom, **pgto;
223 BUG_ON(pgfrom_base <= pgto_base);
228 pgto = pages + (pgto_base >> PAGE_SHIFT);
229 pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
231 pgto_base &= ~PAGE_MASK;
232 pgfrom_base &= ~PAGE_MASK;
235 if (pgto_base >= PAGE_SIZE) {
239 if (pgfrom_base >= PAGE_SIZE){
245 if (copy > (PAGE_SIZE - pgto_base))
246 copy = PAGE_SIZE - pgto_base;
247 if (copy > (PAGE_SIZE - pgfrom_base))
248 copy = PAGE_SIZE - pgfrom_base;
250 vto = kmap_atomic(*pgto);
251 if (*pgto != *pgfrom) {
252 vfrom = kmap_atomic(*pgfrom);
253 memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
254 kunmap_atomic(vfrom);
256 memmove(vto + pgto_base, vto + pgfrom_base, copy);
257 flush_dcache_page(*pgto);
263 } while ((len -= copy) != 0);
267 * _shift_data_right_pages
268 * @pages: vector of pages containing both the source and dest memory area.
269 * @pgto_base: page vector address of destination
270 * @pgfrom_base: page vector address of source
271 * @len: number of bytes to copy
273 * Note: the addresses pgto_base and pgfrom_base are both calculated in
275 * if a memory area starts at byte 'base' in page 'pages[i]',
276 * then its address is given as (i << PAGE_SHIFT) + base
277 * Also note: pgfrom_base must be < pgto_base, but the memory areas
278 * they point to may overlap.
281 _shift_data_right_pages(struct page **pages, size_t pgto_base,
282 size_t pgfrom_base, size_t len)
284 struct page **pgfrom, **pgto;
288 BUG_ON(pgto_base <= pgfrom_base);
296 pgto = pages + (pgto_base >> PAGE_SHIFT);
297 pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
299 pgto_base &= ~PAGE_MASK;
300 pgfrom_base &= ~PAGE_MASK;
303 /* Are any pointers crossing a page boundary? */
304 if (pgto_base == 0) {
305 pgto_base = PAGE_SIZE;
308 if (pgfrom_base == 0) {
309 pgfrom_base = PAGE_SIZE;
314 if (copy > pgto_base)
316 if (copy > pgfrom_base)
321 vto = kmap_atomic(*pgto);
322 if (*pgto != *pgfrom) {
323 vfrom = kmap_atomic(*pgfrom);
324 memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
325 kunmap_atomic(vfrom);
327 memmove(vto + pgto_base, vto + pgfrom_base, copy);
328 flush_dcache_page(*pgto);
331 } while ((len -= copy) != 0);
336 * @pages: array of pages
337 * @pgbase: page vector address of destination
338 * @p: pointer to source data
341 * Copies data from an arbitrary memory location into an array of pages
342 * The copy is assumed to be non-overlapping.
345 _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
354 pgto = pages + (pgbase >> PAGE_SHIFT);
355 pgbase &= ~PAGE_MASK;
358 copy = PAGE_SIZE - pgbase;
362 vto = kmap_atomic(*pgto);
363 memcpy(vto + pgbase, p, copy);
371 if (pgbase == PAGE_SIZE) {
372 flush_dcache_page(*pgto);
378 flush_dcache_page(*pgto);
383 * @p: pointer to destination
384 * @pages: array of pages
385 * @pgbase: offset of source data
388 * Copies data into an arbitrary memory location from an array of pages
389 * The copy is assumed to be non-overlapping.
392 _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
394 struct page **pgfrom;
401 pgfrom = pages + (pgbase >> PAGE_SHIFT);
402 pgbase &= ~PAGE_MASK;
405 copy = PAGE_SIZE - pgbase;
409 vfrom = kmap_atomic(*pgfrom);
410 memcpy(p, vfrom + pgbase, copy);
411 kunmap_atomic(vfrom);
414 if (pgbase == PAGE_SIZE) {
420 } while ((len -= copy) != 0);
422 EXPORT_SYMBOL_GPL(_copy_from_pages);
424 static void xdr_buf_iov_zero(const struct kvec *iov, unsigned int base,
427 if (base >= iov->iov_len)
429 if (len > iov->iov_len - base)
430 len = iov->iov_len - base;
431 memset(iov->iov_base + base, 0, len);
437 * @pgbase: beginning offset
440 static void xdr_buf_pages_zero(const struct xdr_buf *buf, unsigned int pgbase,
443 struct page **pages = buf->pages;
450 if (pgbase >= buf->page_len) {
451 xdr_buf_iov_zero(buf->tail, pgbase - buf->page_len, len);
454 if (pgbase + len > buf->page_len) {
455 xdr_buf_iov_zero(buf->tail, 0, pgbase + len - buf->page_len);
456 len = buf->page_len - pgbase;
459 pgbase += buf->page_base;
461 page = pages + (pgbase >> PAGE_SHIFT);
462 pgbase &= ~PAGE_MASK;
465 zero = PAGE_SIZE - pgbase;
469 vpage = kmap_atomic(*page);
470 memset(vpage + pgbase, 0, zero);
471 kunmap_atomic(vpage);
473 flush_dcache_page(*page);
477 } while ((len -= zero) != 0);
480 static unsigned int xdr_buf_pages_fill_sparse(const struct xdr_buf *buf,
481 unsigned int buflen, gfp_t gfp)
483 unsigned int i, npages, pagelen;
485 if (!(buf->flags & XDRBUF_SPARSE_PAGES))
487 if (buflen <= buf->head->iov_len)
489 pagelen = buflen - buf->head->iov_len;
490 if (pagelen > buf->page_len)
491 pagelen = buf->page_len;
492 npages = (pagelen + buf->page_base + PAGE_SIZE - 1) >> PAGE_SHIFT;
493 for (i = 0; i < npages; i++) {
496 buf->pages[i] = alloc_page(gfp);
497 if (likely(buf->pages[i]))
500 pagelen = i << PAGE_SHIFT;
501 if (pagelen > buf->page_base)
502 buflen += pagelen - buf->page_base;
508 static void xdr_buf_try_expand(struct xdr_buf *buf, unsigned int len)
510 struct kvec *head = buf->head;
511 struct kvec *tail = buf->tail;
512 unsigned int sum = head->iov_len + buf->page_len + tail->iov_len;
513 unsigned int free_space, newlen;
515 if (sum > buf->len) {
516 free_space = min_t(unsigned int, sum - buf->len, len);
517 newlen = xdr_buf_pages_fill_sparse(buf, buf->len + free_space,
519 free_space = newlen - buf->len;
526 if (buf->buflen > sum) {
527 /* Expand the tail buffer */
528 free_space = min_t(unsigned int, buf->buflen - sum, len);
529 tail->iov_len += free_space;
530 buf->len += free_space;
534 static void xdr_buf_tail_copy_right(const struct xdr_buf *buf,
535 unsigned int base, unsigned int len,
538 const struct kvec *tail = buf->tail;
539 unsigned int to = base + shift;
541 if (to >= tail->iov_len)
543 if (len + to > tail->iov_len)
544 len = tail->iov_len - to;
545 memmove(tail->iov_base + to, tail->iov_base + base, len);
548 static void xdr_buf_pages_copy_right(const struct xdr_buf *buf,
549 unsigned int base, unsigned int len,
552 const struct kvec *tail = buf->tail;
553 unsigned int to = base + shift;
554 unsigned int pglen = 0;
555 unsigned int talen = 0, tato = 0;
557 if (base >= buf->page_len)
559 if (len > buf->page_len - base)
560 len = buf->page_len - base;
561 if (to >= buf->page_len) {
562 tato = to - buf->page_len;
563 if (tail->iov_len >= len + tato)
565 else if (tail->iov_len > tato)
566 talen = tail->iov_len - tato;
567 } else if (len + to >= buf->page_len) {
568 pglen = buf->page_len - to;
570 if (talen > tail->iov_len)
571 talen = tail->iov_len;
575 _copy_from_pages(tail->iov_base + tato, buf->pages,
576 buf->page_base + base + pglen, talen);
577 _shift_data_right_pages(buf->pages, buf->page_base + to,
578 buf->page_base + base, pglen);
581 static void xdr_buf_head_copy_right(const struct xdr_buf *buf,
582 unsigned int base, unsigned int len,
585 const struct kvec *head = buf->head;
586 const struct kvec *tail = buf->tail;
587 unsigned int to = base + shift;
588 unsigned int pglen = 0, pgto = 0;
589 unsigned int talen = 0, tato = 0;
591 if (base >= head->iov_len)
593 if (len > head->iov_len - base)
594 len = head->iov_len - base;
595 if (to >= buf->page_len + head->iov_len) {
596 tato = to - buf->page_len - head->iov_len;
598 } else if (to >= head->iov_len) {
599 pgto = to - head->iov_len;
601 if (pgto + pglen > buf->page_len) {
602 talen = pgto + pglen - buf->page_len;
607 if (pglen > buf->page_len) {
608 talen = pglen - buf->page_len;
609 pglen = buf->page_len;
615 if (talen + tato > tail->iov_len)
616 talen = tail->iov_len > tato ? tail->iov_len - tato : 0;
617 memcpy(tail->iov_base + tato, head->iov_base + base, talen);
621 _copy_to_pages(buf->pages, buf->page_base + pgto, head->iov_base + base,
625 memmove(head->iov_base + to, head->iov_base + base, len);
628 static void xdr_buf_tail_shift_right(const struct xdr_buf *buf,
629 unsigned int base, unsigned int len,
632 const struct kvec *tail = buf->tail;
634 if (base >= tail->iov_len || !shift || !len)
636 xdr_buf_tail_copy_right(buf, base, len, shift);
639 static void xdr_buf_pages_shift_right(const struct xdr_buf *buf,
640 unsigned int base, unsigned int len,
645 if (base >= buf->page_len) {
646 xdr_buf_tail_shift_right(buf, base - buf->page_len, len, shift);
649 if (base + len > buf->page_len)
650 xdr_buf_tail_shift_right(buf, 0, base + len - buf->page_len,
652 xdr_buf_pages_copy_right(buf, base, len, shift);
655 static void xdr_buf_head_shift_right(const struct xdr_buf *buf,
656 unsigned int base, unsigned int len,
659 const struct kvec *head = buf->head;
663 if (base >= head->iov_len) {
664 xdr_buf_pages_shift_right(buf, head->iov_len - base, len,
668 if (base + len > head->iov_len)
669 xdr_buf_pages_shift_right(buf, 0, base + len - head->iov_len,
671 xdr_buf_head_copy_right(buf, base, len, shift);
674 static void xdr_buf_tail_copy_left(const struct xdr_buf *buf, unsigned int base,
675 unsigned int len, unsigned int shift)
677 const struct kvec *tail = buf->tail;
679 if (base >= tail->iov_len)
681 if (len > tail->iov_len - base)
682 len = tail->iov_len - base;
683 /* Shift data into head */
684 if (shift > buf->page_len + base) {
685 const struct kvec *head = buf->head;
687 head->iov_len + buf->page_len + base - shift;
688 unsigned int hdlen = len;
690 if (WARN_ONCE(shift > head->iov_len + buf->page_len + base,
691 "SUNRPC: Misaligned data.\n"))
693 if (hdto + hdlen > head->iov_len)
694 hdlen = head->iov_len - hdto;
695 memcpy(head->iov_base + hdto, tail->iov_base + base, hdlen);
701 /* Shift data into pages */
703 unsigned int pgto = buf->page_len + base - shift;
704 unsigned int pglen = len;
706 if (pgto + pglen > buf->page_len)
707 pglen = buf->page_len - pgto;
708 _copy_to_pages(buf->pages, buf->page_base + pgto,
709 tail->iov_base + base, pglen);
715 memmove(tail->iov_base + base - shift, tail->iov_base + base, len);
718 static void xdr_buf_pages_copy_left(const struct xdr_buf *buf,
719 unsigned int base, unsigned int len,
724 if (base >= buf->page_len)
726 if (len > buf->page_len - base)
727 len = buf->page_len - base;
728 /* Shift data into head */
730 const struct kvec *head = buf->head;
731 unsigned int hdto = head->iov_len + base - shift;
732 unsigned int hdlen = len;
734 if (WARN_ONCE(shift > head->iov_len + base,
735 "SUNRPC: Misaligned data.\n"))
737 if (hdto + hdlen > head->iov_len)
738 hdlen = head->iov_len - hdto;
739 _copy_from_pages(head->iov_base + hdto, buf->pages,
740 buf->page_base + base, hdlen);
747 _shift_data_left_pages(buf->pages, buf->page_base + pgto,
748 buf->page_base + base, len);
751 static void xdr_buf_tail_shift_left(const struct xdr_buf *buf,
752 unsigned int base, unsigned int len,
757 xdr_buf_tail_copy_left(buf, base, len, shift);
760 static void xdr_buf_pages_shift_left(const struct xdr_buf *buf,
761 unsigned int base, unsigned int len,
766 if (base >= buf->page_len) {
767 xdr_buf_tail_shift_left(buf, base - buf->page_len, len, shift);
770 xdr_buf_pages_copy_left(buf, base, len, shift);
772 if (len <= buf->page_len)
774 xdr_buf_tail_copy_left(buf, 0, len - buf->page_len, shift);
777 static void xdr_buf_head_shift_left(const struct xdr_buf *buf,
778 unsigned int base, unsigned int len,
781 const struct kvec *head = buf->head;
788 bytes = (shift - base);
795 if (base < head->iov_len) {
796 bytes = min_t(unsigned int, len, head->iov_len - base);
797 memmove(head->iov_base + (base - shift),
798 head->iov_base + base, bytes);
802 xdr_buf_pages_shift_left(buf, base - head->iov_len, len, shift);
808 * @len: new length of buf->head[0]
810 * Shrinks XDR buffer's header kvec buf->head[0], setting it to
811 * 'len' bytes. The extra data is not lost, but is instead
812 * moved into the inlined pages and/or the tail.
814 static unsigned int xdr_shrink_bufhead(struct xdr_buf *buf, unsigned int len)
816 struct kvec *head = buf->head;
817 unsigned int shift, buflen = max(buf->len, len);
819 WARN_ON_ONCE(len > head->iov_len);
820 if (head->iov_len > buflen) {
821 buf->buflen -= head->iov_len - buflen;
822 head->iov_len = buflen;
824 if (len >= head->iov_len)
826 shift = head->iov_len - len;
827 xdr_buf_try_expand(buf, shift);
828 xdr_buf_head_shift_right(buf, len, buflen - len, shift);
830 buf->buflen -= shift;
836 * xdr_shrink_pagelen - shrinks buf->pages to @len bytes
838 * @len: new page buffer length
840 * The extra data is not lost, but is instead moved into buf->tail.
841 * Returns the actual number of bytes moved.
843 static unsigned int xdr_shrink_pagelen(struct xdr_buf *buf, unsigned int len)
845 unsigned int shift, buflen = buf->len - buf->head->iov_len;
847 WARN_ON_ONCE(len > buf->page_len);
848 if (buf->head->iov_len >= buf->len || len > buflen)
850 if (buf->page_len > buflen) {
851 buf->buflen -= buf->page_len - buflen;
852 buf->page_len = buflen;
854 if (len >= buf->page_len)
856 shift = buf->page_len - len;
857 xdr_buf_try_expand(buf, shift);
858 xdr_buf_pages_shift_right(buf, len, buflen - len, shift);
861 buf->buflen -= shift;
866 * xdr_stream_pos - Return the current offset from the start of the xdr_stream
867 * @xdr: pointer to struct xdr_stream
869 unsigned int xdr_stream_pos(const struct xdr_stream *xdr)
871 return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2;
873 EXPORT_SYMBOL_GPL(xdr_stream_pos);
875 static void xdr_stream_set_pos(struct xdr_stream *xdr, unsigned int pos)
877 unsigned int blen = xdr->buf->len;
879 xdr->nwords = blen > pos ? XDR_QUADLEN(blen) - XDR_QUADLEN(pos) : 0;
882 static void xdr_stream_page_set_pos(struct xdr_stream *xdr, unsigned int pos)
884 xdr_stream_set_pos(xdr, pos + xdr->buf->head[0].iov_len);
888 * xdr_page_pos - Return the current offset from the start of the xdr pages
889 * @xdr: pointer to struct xdr_stream
891 unsigned int xdr_page_pos(const struct xdr_stream *xdr)
893 unsigned int pos = xdr_stream_pos(xdr);
895 WARN_ON(pos < xdr->buf->head[0].iov_len);
896 return pos - xdr->buf->head[0].iov_len;
898 EXPORT_SYMBOL_GPL(xdr_page_pos);
901 * xdr_init_encode - Initialize a struct xdr_stream for sending data.
902 * @xdr: pointer to xdr_stream struct
903 * @buf: pointer to XDR buffer in which to encode data
904 * @p: current pointer inside XDR buffer
905 * @rqst: pointer to controlling rpc_rqst, for debugging
907 * Note: at the moment the RPC client only passes the length of our
908 * scratch buffer in the xdr_buf's header kvec. Previously this
909 * meant we needed to call xdr_adjust_iovec() after encoding the
910 * data. With the new scheme, the xdr_stream manages the details
911 * of the buffer length, and takes care of adjusting the kvec
914 void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
915 struct rpc_rqst *rqst)
917 struct kvec *iov = buf->head;
918 int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
920 xdr_reset_scratch_buffer(xdr);
921 BUG_ON(scratch_len < 0);
924 xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
925 xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
926 BUG_ON(iov->iov_len > scratch_len);
928 if (p != xdr->p && p != NULL) {
931 BUG_ON(p < xdr->p || p > xdr->end);
932 len = (char *)p - (char *)xdr->p;
939 EXPORT_SYMBOL_GPL(xdr_init_encode);
942 * xdr_init_encode_pages - Initialize an xdr_stream for encoding into pages
943 * @xdr: pointer to xdr_stream struct
944 * @buf: pointer to XDR buffer into which to encode data
945 * @pages: list of pages to decode into
946 * @rqst: pointer to controlling rpc_rqst, for debugging
949 void xdr_init_encode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
950 struct page **pages, struct rpc_rqst *rqst)
952 xdr_reset_scratch_buffer(xdr);
955 xdr->page_ptr = pages;
957 xdr->p = page_address(*pages);
958 xdr->end = (void *)xdr->p + min_t(u32, buf->buflen, PAGE_SIZE);
961 EXPORT_SYMBOL_GPL(xdr_init_encode_pages);
964 * __xdr_commit_encode - Ensure all data is written to buffer
965 * @xdr: pointer to xdr_stream
967 * We handle encoding across page boundaries by giving the caller a
968 * temporary location to write to, then later copying the data into
969 * place; xdr_commit_encode does that copying.
971 * Normally the caller doesn't need to call this directly, as the
972 * following xdr_reserve_space will do it. But an explicit call may be
973 * required at the end of encoding, or any other time when the xdr_buf
974 * data might be read.
976 void __xdr_commit_encode(struct xdr_stream *xdr)
978 size_t shift = xdr->scratch.iov_len;
981 page = page_address(*xdr->page_ptr);
982 memcpy(xdr->scratch.iov_base, page, shift);
983 memmove(page, page + shift, (void *)xdr->p - page);
984 xdr_reset_scratch_buffer(xdr);
986 EXPORT_SYMBOL_GPL(__xdr_commit_encode);
989 * The buffer space to be reserved crosses the boundary between
990 * xdr->buf->head and xdr->buf->pages, or between two pages
991 * in xdr->buf->pages.
993 static noinline __be32 *xdr_get_next_encode_buffer(struct xdr_stream *xdr,
997 int frag1bytes, frag2bytes;
1000 if (nbytes > PAGE_SIZE)
1001 goto out_overflow; /* Bigger buffers require special handling */
1002 if (xdr->buf->len + nbytes > xdr->buf->buflen)
1003 goto out_overflow; /* Sorry, we're totally out of space */
1004 frag1bytes = (xdr->end - xdr->p) << 2;
1005 frag2bytes = nbytes - frag1bytes;
1007 xdr->iov->iov_len += frag1bytes;
1009 xdr->buf->page_len += frag1bytes;
1014 * If the last encode didn't end exactly on a page boundary, the
1015 * next one will straddle boundaries. Encode into the next
1016 * page, then copy it back later in xdr_commit_encode. We use
1017 * the "scratch" iov to track any temporarily unused fragment of
1018 * space at the end of the previous buffer:
1020 xdr_set_scratch_buffer(xdr, xdr->p, frag1bytes);
1023 * xdr->p is where the next encode will start after
1024 * xdr_commit_encode() has shifted this one back:
1026 p = page_address(*xdr->page_ptr);
1027 xdr->p = p + frag2bytes;
1028 space_left = xdr->buf->buflen - xdr->buf->len;
1029 if (space_left - frag1bytes >= PAGE_SIZE)
1030 xdr->end = p + PAGE_SIZE;
1032 xdr->end = p + space_left - frag1bytes;
1034 xdr->buf->page_len += frag2bytes;
1035 xdr->buf->len += nbytes;
1038 trace_rpc_xdr_overflow(xdr, nbytes);
1043 * xdr_reserve_space - Reserve buffer space for sending
1044 * @xdr: pointer to xdr_stream
1045 * @nbytes: number of bytes to reserve
1047 * Checks that we have enough buffer space to encode 'nbytes' more
1048 * bytes of data. If so, update the total xdr_buf length, and
1049 * adjust the length of the current kvec.
1051 __be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
1056 xdr_commit_encode(xdr);
1057 /* align nbytes on the next 32-bit boundary */
1060 q = p + (nbytes >> 2);
1061 if (unlikely(q > xdr->end || q < p))
1062 return xdr_get_next_encode_buffer(xdr, nbytes);
1065 xdr->iov->iov_len += nbytes;
1067 xdr->buf->page_len += nbytes;
1068 xdr->buf->len += nbytes;
1071 EXPORT_SYMBOL_GPL(xdr_reserve_space);
1074 * xdr_reserve_space_vec - Reserves a large amount of buffer space for sending
1075 * @xdr: pointer to xdr_stream
1076 * @nbytes: number of bytes to reserve
1078 * The size argument passed to xdr_reserve_space() is determined based
1079 * on the number of bytes remaining in the current page to avoid
1080 * invalidating iov_base pointers when xdr_commit_encode() is called.
1084 * %-EMSGSIZE: not enough space is available in @xdr
1086 int xdr_reserve_space_vec(struct xdr_stream *xdr, size_t nbytes)
1092 * svcrdma requires every READ payload to start somewhere
1095 if (xdr->iov == xdr->buf->head) {
1100 /* XXX: Let's find a way to make this more efficient */
1102 thislen = xdr->buf->page_len % PAGE_SIZE;
1103 thislen = min_t(size_t, nbytes, PAGE_SIZE - thislen);
1105 p = xdr_reserve_space(xdr, thislen);
1114 EXPORT_SYMBOL_GPL(xdr_reserve_space_vec);
1117 * xdr_truncate_encode - truncate an encode buffer
1118 * @xdr: pointer to xdr_stream
1119 * @len: new length of buffer
1121 * Truncates the xdr stream, so that xdr->buf->len == len,
1122 * and xdr->p points at offset len from the start of the buffer, and
1123 * head, tail, and page lengths are adjusted to correspond.
1125 * If this means moving xdr->p to a different buffer, we assume that
1126 * the end pointer should be set to the end of the current page,
1127 * except in the case of the head buffer when we assume the head
1128 * buffer's current length represents the end of the available buffer.
1130 * This is *not* safe to use on a buffer that already has inlined page
1131 * cache pages (as in a zero-copy server read reply), except for the
1132 * simple case of truncating from one position in the tail to another.
1135 void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
1137 struct xdr_buf *buf = xdr->buf;
1138 struct kvec *head = buf->head;
1139 struct kvec *tail = buf->tail;
1143 if (len > buf->len) {
1147 xdr_commit_encode(xdr);
1149 fraglen = min_t(int, buf->len - len, tail->iov_len);
1150 tail->iov_len -= fraglen;
1151 buf->len -= fraglen;
1152 if (tail->iov_len) {
1153 xdr->p = tail->iov_base + tail->iov_len;
1154 WARN_ON_ONCE(!xdr->end);
1155 WARN_ON_ONCE(!xdr->iov);
1158 WARN_ON_ONCE(fraglen);
1159 fraglen = min_t(int, buf->len - len, buf->page_len);
1160 buf->page_len -= fraglen;
1161 buf->len -= fraglen;
1163 new = buf->page_base + buf->page_len;
1165 xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
1167 if (buf->page_len) {
1168 xdr->p = page_address(*xdr->page_ptr);
1169 xdr->end = (void *)xdr->p + PAGE_SIZE;
1170 xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
1171 WARN_ON_ONCE(xdr->iov);
1175 xdr->end = head->iov_base + head->iov_len;
1176 /* (otherwise assume xdr->end is already set) */
1178 head->iov_len = len;
1180 xdr->p = head->iov_base + head->iov_len;
1181 xdr->iov = buf->head;
1183 EXPORT_SYMBOL(xdr_truncate_encode);
1186 * xdr_truncate_decode - Truncate a decoding stream
1187 * @xdr: pointer to struct xdr_stream
1188 * @len: Number of bytes to remove
1191 void xdr_truncate_decode(struct xdr_stream *xdr, size_t len)
1193 unsigned int nbytes = xdr_align_size(len);
1195 xdr->buf->len -= nbytes;
1196 xdr->nwords -= XDR_QUADLEN(nbytes);
1198 EXPORT_SYMBOL_GPL(xdr_truncate_decode);
1201 * xdr_restrict_buflen - decrease available buffer space
1202 * @xdr: pointer to xdr_stream
1203 * @newbuflen: new maximum number of bytes available
1205 * Adjust our idea of how much space is available in the buffer.
1206 * If we've already used too much space in the buffer, returns -1.
1207 * If the available space is already smaller than newbuflen, returns 0
1208 * and does nothing. Otherwise, adjusts xdr->buf->buflen to newbuflen
1209 * and ensures xdr->end is set at most offset newbuflen from the start
1212 int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
1214 struct xdr_buf *buf = xdr->buf;
1215 int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
1216 int end_offset = buf->len + left_in_this_buf;
1218 if (newbuflen < 0 || newbuflen < buf->len)
1220 if (newbuflen > buf->buflen)
1222 if (newbuflen < end_offset)
1223 xdr->end = (void *)xdr->end + newbuflen - end_offset;
1224 buf->buflen = newbuflen;
1227 EXPORT_SYMBOL(xdr_restrict_buflen);
1230 * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
1231 * @xdr: pointer to xdr_stream
1232 * @pages: array of pages to insert
1233 * @base: starting offset of first data byte in @pages
1234 * @len: number of data bytes in @pages to insert
1236 * After the @pages are added, the tail iovec is instantiated pointing to
1237 * end of the head buffer, and the stream is set up to encode subsequent
1238 * items into the tail.
1240 void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
1243 struct xdr_buf *buf = xdr->buf;
1244 struct kvec *tail = buf->tail;
1247 buf->page_base = base;
1248 buf->page_len = len;
1250 tail->iov_base = xdr->p;
1255 unsigned int pad = 4 - (len & 3);
1257 BUG_ON(xdr->p >= xdr->end);
1258 tail->iov_base = (char *)xdr->p + (len & 3);
1259 tail->iov_len += pad;
1266 EXPORT_SYMBOL_GPL(xdr_write_pages);
1268 static unsigned int xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
1269 unsigned int base, unsigned int len)
1271 if (len > iov->iov_len)
1273 if (unlikely(base > len))
1275 xdr->p = (__be32*)(iov->iov_base + base);
1276 xdr->end = (__be32*)(iov->iov_base + len);
1278 xdr->page_ptr = NULL;
1282 static unsigned int xdr_set_tail_base(struct xdr_stream *xdr,
1283 unsigned int base, unsigned int len)
1285 struct xdr_buf *buf = xdr->buf;
1287 xdr_stream_set_pos(xdr, base + buf->page_len + buf->head->iov_len);
1288 return xdr_set_iov(xdr, buf->tail, base, len);
1291 static unsigned int xdr_set_page_base(struct xdr_stream *xdr,
1292 unsigned int base, unsigned int len)
1295 unsigned int maxlen;
1300 maxlen = xdr->buf->page_len;
1308 xdr_stream_page_set_pos(xdr, base);
1309 base += xdr->buf->page_base;
1311 pgnr = base >> PAGE_SHIFT;
1312 xdr->page_ptr = &xdr->buf->pages[pgnr];
1313 kaddr = page_address(*xdr->page_ptr);
1315 pgoff = base & ~PAGE_MASK;
1316 xdr->p = (__be32*)(kaddr + pgoff);
1318 pgend = pgoff + len;
1319 if (pgend > PAGE_SIZE)
1321 xdr->end = (__be32*)(kaddr + pgend);
1326 static void xdr_set_page(struct xdr_stream *xdr, unsigned int base,
1329 if (xdr_set_page_base(xdr, base, len) == 0) {
1330 base -= xdr->buf->page_len;
1331 xdr_set_tail_base(xdr, base, len);
1335 static void xdr_set_next_page(struct xdr_stream *xdr)
1337 unsigned int newbase;
1339 newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
1340 newbase -= xdr->buf->page_base;
1341 if (newbase < xdr->buf->page_len)
1342 xdr_set_page_base(xdr, newbase, xdr_stream_remaining(xdr));
1344 xdr_set_tail_base(xdr, 0, xdr_stream_remaining(xdr));
1347 static bool xdr_set_next_buffer(struct xdr_stream *xdr)
1349 if (xdr->page_ptr != NULL)
1350 xdr_set_next_page(xdr);
1351 else if (xdr->iov == xdr->buf->head)
1352 xdr_set_page(xdr, 0, xdr_stream_remaining(xdr));
1353 return xdr->p != xdr->end;
1357 * xdr_init_decode - Initialize an xdr_stream for decoding data.
1358 * @xdr: pointer to xdr_stream struct
1359 * @buf: pointer to XDR buffer from which to decode data
1360 * @p: current pointer inside XDR buffer
1361 * @rqst: pointer to controlling rpc_rqst, for debugging
1363 void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
1364 struct rpc_rqst *rqst)
1367 xdr_reset_scratch_buffer(xdr);
1368 xdr->nwords = XDR_QUADLEN(buf->len);
1369 if (xdr_set_iov(xdr, buf->head, 0, buf->len) == 0 &&
1370 xdr_set_page_base(xdr, 0, buf->len) == 0)
1371 xdr_set_iov(xdr, buf->tail, 0, buf->len);
1372 if (p != NULL && p > xdr->p && xdr->end >= p) {
1373 xdr->nwords -= p - xdr->p;
1378 EXPORT_SYMBOL_GPL(xdr_init_decode);
1381 * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages
1382 * @xdr: pointer to xdr_stream struct
1383 * @buf: pointer to XDR buffer from which to decode data
1384 * @pages: list of pages to decode into
1385 * @len: length in bytes of buffer in pages
1387 void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
1388 struct page **pages, unsigned int len)
1390 memset(buf, 0, sizeof(*buf));
1392 buf->page_len = len;
1395 xdr_init_decode(xdr, buf, NULL, NULL);
1397 EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
1399 static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1401 unsigned int nwords = XDR_QUADLEN(nbytes);
1403 __be32 *q = p + nwords;
1405 if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
1408 xdr->nwords -= nwords;
1412 static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
1415 char *cpdest = xdr->scratch.iov_base;
1416 size_t cplen = (char *)xdr->end - (char *)xdr->p;
1418 if (nbytes > xdr->scratch.iov_len)
1420 p = __xdr_inline_decode(xdr, cplen);
1423 memcpy(cpdest, p, cplen);
1424 if (!xdr_set_next_buffer(xdr))
1428 p = __xdr_inline_decode(xdr, nbytes);
1431 memcpy(cpdest, p, nbytes);
1432 return xdr->scratch.iov_base;
1434 trace_rpc_xdr_overflow(xdr, nbytes);
1439 * xdr_inline_decode - Retrieve XDR data to decode
1440 * @xdr: pointer to xdr_stream struct
1441 * @nbytes: number of bytes of data to decode
1443 * Check if the input buffer is long enough to enable us to decode
1444 * 'nbytes' more bytes of data starting at the current position.
1445 * If so return the current pointer, then update the current
1448 __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1452 if (unlikely(nbytes == 0))
1454 if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1456 p = __xdr_inline_decode(xdr, nbytes);
1459 return xdr_copy_to_scratch(xdr, nbytes);
1461 trace_rpc_xdr_overflow(xdr, nbytes);
1464 EXPORT_SYMBOL_GPL(xdr_inline_decode);
1466 static void xdr_realign_pages(struct xdr_stream *xdr)
1468 struct xdr_buf *buf = xdr->buf;
1469 struct kvec *iov = buf->head;
1470 unsigned int cur = xdr_stream_pos(xdr);
1471 unsigned int copied;
1473 /* Realign pages to current pointer position */
1474 if (iov->iov_len > cur) {
1475 copied = xdr_shrink_bufhead(buf, cur);
1476 trace_rpc_xdr_alignment(xdr, cur, copied);
1477 xdr_set_page(xdr, 0, buf->page_len);
1481 static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
1483 struct xdr_buf *buf = xdr->buf;
1484 unsigned int nwords = XDR_QUADLEN(len);
1485 unsigned int copied;
1487 if (xdr->nwords == 0)
1490 xdr_realign_pages(xdr);
1491 if (nwords > xdr->nwords) {
1492 nwords = xdr->nwords;
1495 if (buf->page_len <= len)
1496 len = buf->page_len;
1497 else if (nwords < xdr->nwords) {
1498 /* Truncate page data and move it into the tail */
1499 copied = xdr_shrink_pagelen(buf, len);
1500 trace_rpc_xdr_alignment(xdr, len, copied);
1506 * xdr_read_pages - align page-based XDR data to current pointer position
1507 * @xdr: pointer to xdr_stream struct
1508 * @len: number of bytes of page data
1510 * Moves data beyond the current pointer position from the XDR head[] buffer
1511 * into the page list. Any data that lies beyond current position + @len
1512 * bytes is moved into the XDR tail[]. The xdr_stream current position is
1513 * then advanced past that data to align to the next XDR object in the tail.
1515 * Returns the number of XDR encoded bytes now contained in the pages
1517 unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
1519 unsigned int nwords = XDR_QUADLEN(len);
1520 unsigned int base, end, pglen;
1522 pglen = xdr_align_pages(xdr, nwords << 2);
1526 base = (nwords << 2) - pglen;
1527 end = xdr_stream_remaining(xdr) - pglen;
1529 xdr_set_tail_base(xdr, base, end);
1530 return len <= pglen ? len : pglen;
1532 EXPORT_SYMBOL_GPL(xdr_read_pages);
1535 * xdr_set_pagelen - Sets the length of the XDR pages
1536 * @xdr: pointer to xdr_stream struct
1537 * @len: new length of the XDR page data
1539 * Either grows or shrinks the length of the xdr pages by setting pagelen to
1540 * @len bytes. When shrinking, any extra data is moved into buf->tail, whereas
1541 * when growing any data beyond the current pointer is moved into the tail.
1543 * Returns True if the operation was successful, and False otherwise.
1545 void xdr_set_pagelen(struct xdr_stream *xdr, unsigned int len)
1547 struct xdr_buf *buf = xdr->buf;
1548 size_t remaining = xdr_stream_remaining(xdr);
1551 if (len < buf->page_len) {
1552 base = buf->page_len - len;
1553 xdr_shrink_pagelen(buf, len);
1555 xdr_buf_head_shift_right(buf, xdr_stream_pos(xdr),
1556 buf->page_len, remaining);
1557 if (len > buf->page_len)
1558 xdr_buf_try_expand(buf, len - buf->page_len);
1560 xdr_set_tail_base(xdr, base, remaining);
1562 EXPORT_SYMBOL_GPL(xdr_set_pagelen);
1565 * xdr_enter_page - decode data from the XDR page
1566 * @xdr: pointer to xdr_stream struct
1567 * @len: number of bytes of page data
1569 * Moves data beyond the current pointer position from the XDR head[] buffer
1570 * into the page list. Any data that lies beyond current position + "len"
1571 * bytes is moved into the XDR tail[]. The current pointer is then
1572 * repositioned at the beginning of the first XDR page.
1574 void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
1576 len = xdr_align_pages(xdr, len);
1578 * Position current pointer at beginning of tail, and
1579 * set remaining message length.
1582 xdr_set_page_base(xdr, 0, len);
1584 EXPORT_SYMBOL_GPL(xdr_enter_page);
1586 static const struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
1588 void xdr_buf_from_iov(const struct kvec *iov, struct xdr_buf *buf)
1590 buf->head[0] = *iov;
1591 buf->tail[0] = empty_iov;
1593 buf->buflen = buf->len = iov->iov_len;
1595 EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
1598 * xdr_buf_subsegment - set subbuf to a portion of buf
1599 * @buf: an xdr buffer
1600 * @subbuf: the result buffer
1601 * @base: beginning of range in bytes
1602 * @len: length of range in bytes
1604 * sets @subbuf to an xdr buffer representing the portion of @buf of
1605 * length @len starting at offset @base.
1607 * @buf and @subbuf may be pointers to the same struct xdr_buf.
1609 * Returns -1 if base or length are out of bounds.
1611 int xdr_buf_subsegment(const struct xdr_buf *buf, struct xdr_buf *subbuf,
1612 unsigned int base, unsigned int len)
1614 subbuf->buflen = subbuf->len = len;
1615 if (base < buf->head[0].iov_len) {
1616 subbuf->head[0].iov_base = buf->head[0].iov_base + base;
1617 subbuf->head[0].iov_len = min_t(unsigned int, len,
1618 buf->head[0].iov_len - base);
1619 len -= subbuf->head[0].iov_len;
1622 base -= buf->head[0].iov_len;
1623 subbuf->head[0].iov_base = buf->head[0].iov_base;
1624 subbuf->head[0].iov_len = 0;
1627 if (base < buf->page_len) {
1628 subbuf->page_len = min(buf->page_len - base, len);
1629 base += buf->page_base;
1630 subbuf->page_base = base & ~PAGE_MASK;
1631 subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
1632 len -= subbuf->page_len;
1635 base -= buf->page_len;
1636 subbuf->pages = buf->pages;
1637 subbuf->page_base = 0;
1638 subbuf->page_len = 0;
1641 if (base < buf->tail[0].iov_len) {
1642 subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
1643 subbuf->tail[0].iov_len = min_t(unsigned int, len,
1644 buf->tail[0].iov_len - base);
1645 len -= subbuf->tail[0].iov_len;
1648 base -= buf->tail[0].iov_len;
1649 subbuf->tail[0].iov_base = buf->tail[0].iov_base;
1650 subbuf->tail[0].iov_len = 0;
1657 EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
1660 * xdr_stream_subsegment - set @subbuf to a portion of @xdr
1661 * @xdr: an xdr_stream set up for decoding
1662 * @subbuf: the result buffer
1663 * @nbytes: length of @xdr to extract, in bytes
1665 * Sets up @subbuf to represent a portion of @xdr. The portion
1666 * starts at the current offset in @xdr, and extends for a length
1667 * of @nbytes. If this is successful, @xdr is advanced to the next
1668 * XDR data item following that portion.
1671 * %true: @subbuf has been initialized, and @xdr has been advanced.
1672 * %false: a bounds error has occurred
1674 bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
1675 unsigned int nbytes)
1677 unsigned int start = xdr_stream_pos(xdr);
1678 unsigned int remaining, len;
1680 /* Extract @subbuf and bounds-check the fn arguments */
1681 if (xdr_buf_subsegment(xdr->buf, subbuf, start, nbytes))
1684 /* Advance @xdr by @nbytes */
1685 for (remaining = nbytes; remaining;) {
1686 if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1689 len = (char *)xdr->end - (char *)xdr->p;
1690 if (remaining <= len) {
1691 xdr->p = (__be32 *)((char *)xdr->p +
1692 (remaining + xdr_pad_size(nbytes)));
1696 xdr->p = (__be32 *)((char *)xdr->p + len);
1701 xdr_stream_set_pos(xdr, start + nbytes);
1704 EXPORT_SYMBOL_GPL(xdr_stream_subsegment);
1707 * xdr_stream_move_subsegment - Move part of a stream to another position
1708 * @xdr: the source xdr_stream
1709 * @offset: the source offset of the segment
1710 * @target: the target offset of the segment
1711 * @length: the number of bytes to move
1713 * Moves @length bytes from @offset to @target in the xdr_stream, overwriting
1714 * anything in its space. Returns the number of bytes in the segment.
1716 unsigned int xdr_stream_move_subsegment(struct xdr_stream *xdr, unsigned int offset,
1717 unsigned int target, unsigned int length)
1722 if (offset < target) {
1723 shift = target - offset;
1724 if (xdr_buf_subsegment(xdr->buf, &buf, offset, shift + length) < 0)
1726 xdr_buf_head_shift_right(&buf, 0, length, shift);
1727 } else if (offset > target) {
1728 shift = offset - target;
1729 if (xdr_buf_subsegment(xdr->buf, &buf, target, shift + length) < 0)
1731 xdr_buf_head_shift_left(&buf, shift, length, shift);
1735 EXPORT_SYMBOL_GPL(xdr_stream_move_subsegment);
1738 * xdr_stream_zero - zero out a portion of an xdr_stream
1739 * @xdr: an xdr_stream to zero out
1740 * @offset: the starting point in the stream
1741 * @length: the number of bytes to zero
1743 unsigned int xdr_stream_zero(struct xdr_stream *xdr, unsigned int offset,
1744 unsigned int length)
1748 if (xdr_buf_subsegment(xdr->buf, &buf, offset, length) < 0)
1750 if (buf.head[0].iov_len)
1751 xdr_buf_iov_zero(buf.head, 0, buf.head[0].iov_len);
1752 if (buf.page_len > 0)
1753 xdr_buf_pages_zero(&buf, 0, buf.page_len);
1754 if (buf.tail[0].iov_len)
1755 xdr_buf_iov_zero(buf.tail, 0, buf.tail[0].iov_len);
1758 EXPORT_SYMBOL_GPL(xdr_stream_zero);
1761 * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
1762 * @buf: buf to be trimmed
1763 * @len: number of bytes to reduce "buf" by
1765 * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
1766 * that it's possible that we'll trim less than that amount if the xdr_buf is
1767 * too small, or if (for instance) it's all in the head and the parser has
1768 * already read too far into it.
1770 void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
1773 unsigned int trim = len;
1775 if (buf->tail[0].iov_len) {
1776 cur = min_t(size_t, buf->tail[0].iov_len, trim);
1777 buf->tail[0].iov_len -= cur;
1783 if (buf->page_len) {
1784 cur = min_t(unsigned int, buf->page_len, trim);
1785 buf->page_len -= cur;
1791 if (buf->head[0].iov_len) {
1792 cur = min_t(size_t, buf->head[0].iov_len, trim);
1793 buf->head[0].iov_len -= cur;
1797 buf->len -= (len - trim);
1799 EXPORT_SYMBOL_GPL(xdr_buf_trim);
1801 static void __read_bytes_from_xdr_buf(const struct xdr_buf *subbuf,
1802 void *obj, unsigned int len)
1804 unsigned int this_len;
1806 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1807 memcpy(obj, subbuf->head[0].iov_base, this_len);
1810 this_len = min_t(unsigned int, len, subbuf->page_len);
1811 _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
1814 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1815 memcpy(obj, subbuf->tail[0].iov_base, this_len);
1818 /* obj is assumed to point to allocated memory of size at least len: */
1819 int read_bytes_from_xdr_buf(const struct xdr_buf *buf, unsigned int base,
1820 void *obj, unsigned int len)
1822 struct xdr_buf subbuf;
1825 status = xdr_buf_subsegment(buf, &subbuf, base, len);
1828 __read_bytes_from_xdr_buf(&subbuf, obj, len);
1831 EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
1833 static void __write_bytes_to_xdr_buf(const struct xdr_buf *subbuf,
1834 void *obj, unsigned int len)
1836 unsigned int this_len;
1838 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1839 memcpy(subbuf->head[0].iov_base, obj, this_len);
1842 this_len = min_t(unsigned int, len, subbuf->page_len);
1843 _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
1846 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1847 memcpy(subbuf->tail[0].iov_base, obj, this_len);
1850 /* obj is assumed to point to allocated memory of size at least len: */
1851 int write_bytes_to_xdr_buf(const struct xdr_buf *buf, unsigned int base,
1852 void *obj, unsigned int len)
1854 struct xdr_buf subbuf;
1857 status = xdr_buf_subsegment(buf, &subbuf, base, len);
1860 __write_bytes_to_xdr_buf(&subbuf, obj, len);
1863 EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
1865 int xdr_decode_word(const struct xdr_buf *buf, unsigned int base, u32 *obj)
1870 status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
1873 *obj = be32_to_cpu(raw);
1876 EXPORT_SYMBOL_GPL(xdr_decode_word);
1878 int xdr_encode_word(const struct xdr_buf *buf, unsigned int base, u32 obj)
1880 __be32 raw = cpu_to_be32(obj);
1882 return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
1884 EXPORT_SYMBOL_GPL(xdr_encode_word);
1886 /* Returns 0 on success, or else a negative error code. */
1887 static int xdr_xcode_array2(const struct xdr_buf *buf, unsigned int base,
1888 struct xdr_array2_desc *desc, int encode)
1890 char *elem = NULL, *c;
1891 unsigned int copied = 0, todo, avail_here;
1892 struct page **ppages = NULL;
1896 if (xdr_encode_word(buf, base, desc->array_len) != 0)
1899 if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
1900 desc->array_len > desc->array_maxlen ||
1901 (unsigned long) base + 4 + desc->array_len *
1902 desc->elem_size > buf->len)
1910 todo = desc->array_len * desc->elem_size;
1913 if (todo && base < buf->head->iov_len) {
1914 c = buf->head->iov_base + base;
1915 avail_here = min_t(unsigned int, todo,
1916 buf->head->iov_len - base);
1919 while (avail_here >= desc->elem_size) {
1920 err = desc->xcode(desc, c);
1923 c += desc->elem_size;
1924 avail_here -= desc->elem_size;
1928 elem = kmalloc(desc->elem_size, GFP_KERNEL);
1934 err = desc->xcode(desc, elem);
1937 memcpy(c, elem, avail_here);
1939 memcpy(elem, c, avail_here);
1940 copied = avail_here;
1942 base = buf->head->iov_len; /* align to start of pages */
1945 /* process pages array */
1946 base -= buf->head->iov_len;
1947 if (todo && base < buf->page_len) {
1948 unsigned int avail_page;
1950 avail_here = min(todo, buf->page_len - base);
1953 base += buf->page_base;
1954 ppages = buf->pages + (base >> PAGE_SHIFT);
1956 avail_page = min_t(unsigned int, PAGE_SIZE - base,
1958 c = kmap(*ppages) + base;
1960 while (avail_here) {
1961 avail_here -= avail_page;
1962 if (copied || avail_page < desc->elem_size) {
1963 unsigned int l = min(avail_page,
1964 desc->elem_size - copied);
1966 elem = kmalloc(desc->elem_size,
1974 err = desc->xcode(desc, elem);
1978 memcpy(c, elem + copied, l);
1980 if (copied == desc->elem_size)
1983 memcpy(elem + copied, c, l);
1985 if (copied == desc->elem_size) {
1986 err = desc->xcode(desc, elem);
1995 while (avail_page >= desc->elem_size) {
1996 err = desc->xcode(desc, c);
1999 c += desc->elem_size;
2000 avail_page -= desc->elem_size;
2003 unsigned int l = min(avail_page,
2004 desc->elem_size - copied);
2006 elem = kmalloc(desc->elem_size,
2014 err = desc->xcode(desc, elem);
2018 memcpy(c, elem + copied, l);
2020 if (copied == desc->elem_size)
2023 memcpy(elem + copied, c, l);
2025 if (copied == desc->elem_size) {
2026 err = desc->xcode(desc, elem);
2039 avail_page = min(avail_here,
2040 (unsigned int) PAGE_SIZE);
2042 base = buf->page_len; /* align to start of tail */
2046 base -= buf->page_len;
2048 c = buf->tail->iov_base + base;
2050 unsigned int l = desc->elem_size - copied;
2053 memcpy(c, elem + copied, l);
2055 memcpy(elem + copied, c, l);
2056 err = desc->xcode(desc, elem);
2064 err = desc->xcode(desc, c);
2067 c += desc->elem_size;
2068 todo -= desc->elem_size;
2080 int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base,
2081 struct xdr_array2_desc *desc)
2083 if (base >= buf->len)
2086 return xdr_xcode_array2(buf, base, desc, 0);
2088 EXPORT_SYMBOL_GPL(xdr_decode_array2);
2090 int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base,
2091 struct xdr_array2_desc *desc)
2093 if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
2094 buf->head->iov_len + buf->page_len + buf->tail->iov_len)
2097 return xdr_xcode_array2(buf, base, desc, 1);
2099 EXPORT_SYMBOL_GPL(xdr_encode_array2);
2101 int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset,
2103 int (*actor)(struct scatterlist *, void *), void *data)
2106 unsigned int page_len, thislen, page_offset;
2107 struct scatterlist sg[1];
2109 sg_init_table(sg, 1);
2111 if (offset >= buf->head[0].iov_len) {
2112 offset -= buf->head[0].iov_len;
2114 thislen = buf->head[0].iov_len - offset;
2117 sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
2118 ret = actor(sg, data);
2127 if (offset >= buf->page_len) {
2128 offset -= buf->page_len;
2130 page_len = buf->page_len - offset;
2134 page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
2135 i = (offset + buf->page_base) >> PAGE_SHIFT;
2136 thislen = PAGE_SIZE - page_offset;
2138 if (thislen > page_len)
2140 sg_set_page(sg, buf->pages[i], thislen, page_offset);
2141 ret = actor(sg, data);
2144 page_len -= thislen;
2147 thislen = PAGE_SIZE;
2148 } while (page_len != 0);
2153 if (offset < buf->tail[0].iov_len) {
2154 thislen = buf->tail[0].iov_len - offset;
2157 sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
2158 ret = actor(sg, data);
2166 EXPORT_SYMBOL_GPL(xdr_process_buf);
2169 * xdr_stream_decode_opaque - Decode variable length opaque
2170 * @xdr: pointer to xdr_stream
2171 * @ptr: location to store opaque data
2172 * @size: size of storage buffer @ptr
2175 * On success, returns size of object stored in *@ptr
2176 * %-EBADMSG on XDR buffer overflow
2177 * %-EMSGSIZE on overflow of storage buffer @ptr
2179 ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, size_t size)
2184 ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
2187 memcpy(ptr, p, ret);
2190 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque);
2193 * xdr_stream_decode_opaque_dup - Decode and duplicate variable length opaque
2194 * @xdr: pointer to xdr_stream
2195 * @ptr: location to store pointer to opaque data
2196 * @maxlen: maximum acceptable object size
2197 * @gfp_flags: GFP mask to use
2200 * On success, returns size of object stored in *@ptr
2201 * %-EBADMSG on XDR buffer overflow
2202 * %-EMSGSIZE if the size of the object would exceed @maxlen
2203 * %-ENOMEM on memory allocation failure
2205 ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
2206 size_t maxlen, gfp_t gfp_flags)
2211 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2213 *ptr = kmemdup(p, ret, gfp_flags);
2221 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_dup);
2224 * xdr_stream_decode_string - Decode variable length string
2225 * @xdr: pointer to xdr_stream
2226 * @str: location to store string
2227 * @size: size of storage buffer @str
2230 * On success, returns length of NUL-terminated string stored in *@str
2231 * %-EBADMSG on XDR buffer overflow
2232 * %-EMSGSIZE on overflow of storage buffer @str
2234 ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, size_t size)
2239 ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
2241 memcpy(str, p, ret);
2248 EXPORT_SYMBOL_GPL(xdr_stream_decode_string);
2251 * xdr_stream_decode_string_dup - Decode and duplicate variable length string
2252 * @xdr: pointer to xdr_stream
2253 * @str: location to store pointer to string
2254 * @maxlen: maximum acceptable string length
2255 * @gfp_flags: GFP mask to use
2258 * On success, returns length of NUL-terminated string stored in *@ptr
2259 * %-EBADMSG on XDR buffer overflow
2260 * %-EMSGSIZE if the size of the string would exceed @maxlen
2261 * %-ENOMEM on memory allocation failure
2263 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
2264 size_t maxlen, gfp_t gfp_flags)
2269 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2271 char *s = kmemdup_nul(p, ret, gfp_flags);
2281 EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);
2284 * xdr_stream_decode_opaque_auth - Decode struct opaque_auth (RFC5531 S8.2)
2285 * @xdr: pointer to xdr_stream
2286 * @flavor: location to store decoded flavor
2287 * @body: location to store decode body
2288 * @body_len: location to store length of decoded body
2291 * On success, returns the number of buffer bytes consumed
2292 * %-EBADMSG on XDR buffer overflow
2293 * %-EMSGSIZE if the decoded size of the body field exceeds 400 octets
2295 ssize_t xdr_stream_decode_opaque_auth(struct xdr_stream *xdr, u32 *flavor,
2296 void **body, unsigned int *body_len)
2300 len = xdr_stream_decode_u32(xdr, flavor);
2301 if (unlikely(len < 0))
2303 ret = xdr_stream_decode_opaque_inline(xdr, body, RPC_MAX_AUTH_SIZE);
2304 if (unlikely(ret < 0))
2309 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_auth);
2312 * xdr_stream_encode_opaque_auth - Encode struct opaque_auth (RFC5531 S8.2)
2313 * @xdr: pointer to xdr_stream
2314 * @flavor: verifier flavor to encode
2315 * @body: content of body to encode
2316 * @body_len: length of body to encode
2319 * On success, returns length in bytes of XDR buffer consumed
2320 * %-EBADMSG on XDR buffer overflow
2321 * %-EMSGSIZE if the size of @body exceeds 400 octets
2323 ssize_t xdr_stream_encode_opaque_auth(struct xdr_stream *xdr, u32 flavor,
2324 void *body, unsigned int body_len)
2328 if (unlikely(body_len > RPC_MAX_AUTH_SIZE))
2330 len = xdr_stream_encode_u32(xdr, flavor);
2331 if (unlikely(len < 0))
2333 ret = xdr_stream_encode_opaque(xdr, body, body_len);
2334 if (unlikely(ret < 0))
2338 EXPORT_SYMBOL_GPL(xdr_stream_encode_opaque_auth);