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_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);
1075 * xdr_reserve_space_vec - Reserves a large amount of buffer space for sending
1076 * @xdr: pointer to xdr_stream
1077 * @vec: pointer to a kvec array
1078 * @nbytes: number of bytes to reserve
1080 * Reserves enough buffer space to encode 'nbytes' of data and stores the
1081 * pointers in 'vec'. The size argument passed to xdr_reserve_space() is
1082 * determined based on the number of bytes remaining in the current page to
1083 * avoid invalidating iov_base pointers when xdr_commit_encode() is called.
1085 int xdr_reserve_space_vec(struct xdr_stream *xdr, struct kvec *vec, size_t nbytes)
1092 * svcrdma requires every READ payload to start somewhere
1095 if (xdr->iov == xdr->buf->head) {
1101 thislen = xdr->buf->page_len % PAGE_SIZE;
1102 thislen = min_t(size_t, nbytes, PAGE_SIZE - thislen);
1104 p = xdr_reserve_space(xdr, thislen);
1108 vec[v].iov_base = p;
1109 vec[v].iov_len = thislen;
1116 EXPORT_SYMBOL_GPL(xdr_reserve_space_vec);
1119 * xdr_truncate_encode - truncate an encode buffer
1120 * @xdr: pointer to xdr_stream
1121 * @len: new length of buffer
1123 * Truncates the xdr stream, so that xdr->buf->len == len,
1124 * and xdr->p points at offset len from the start of the buffer, and
1125 * head, tail, and page lengths are adjusted to correspond.
1127 * If this means moving xdr->p to a different buffer, we assume that
1128 * the end pointer should be set to the end of the current page,
1129 * except in the case of the head buffer when we assume the head
1130 * buffer's current length represents the end of the available buffer.
1132 * This is *not* safe to use on a buffer that already has inlined page
1133 * cache pages (as in a zero-copy server read reply), except for the
1134 * simple case of truncating from one position in the tail to another.
1137 void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
1139 struct xdr_buf *buf = xdr->buf;
1140 struct kvec *head = buf->head;
1141 struct kvec *tail = buf->tail;
1145 if (len > buf->len) {
1149 xdr_commit_encode(xdr);
1151 fraglen = min_t(int, buf->len - len, tail->iov_len);
1152 tail->iov_len -= fraglen;
1153 buf->len -= fraglen;
1154 if (tail->iov_len) {
1155 xdr->p = tail->iov_base + tail->iov_len;
1156 WARN_ON_ONCE(!xdr->end);
1157 WARN_ON_ONCE(!xdr->iov);
1160 WARN_ON_ONCE(fraglen);
1161 fraglen = min_t(int, buf->len - len, buf->page_len);
1162 buf->page_len -= fraglen;
1163 buf->len -= fraglen;
1165 new = buf->page_base + buf->page_len;
1167 xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
1169 if (buf->page_len) {
1170 xdr->p = page_address(*xdr->page_ptr);
1171 xdr->end = (void *)xdr->p + PAGE_SIZE;
1172 xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
1173 WARN_ON_ONCE(xdr->iov);
1177 xdr->end = head->iov_base + head->iov_len;
1178 /* (otherwise assume xdr->end is already set) */
1180 head->iov_len = len;
1182 xdr->p = head->iov_base + head->iov_len;
1183 xdr->iov = buf->head;
1185 EXPORT_SYMBOL(xdr_truncate_encode);
1188 * xdr_truncate_decode - Truncate a decoding stream
1189 * @xdr: pointer to struct xdr_stream
1190 * @len: Number of bytes to remove
1193 void xdr_truncate_decode(struct xdr_stream *xdr, size_t len)
1195 unsigned int nbytes = xdr_align_size(len);
1197 xdr->buf->len -= nbytes;
1198 xdr->nwords -= XDR_QUADLEN(nbytes);
1200 EXPORT_SYMBOL_GPL(xdr_truncate_decode);
1203 * xdr_restrict_buflen - decrease available buffer space
1204 * @xdr: pointer to xdr_stream
1205 * @newbuflen: new maximum number of bytes available
1207 * Adjust our idea of how much space is available in the buffer.
1208 * If we've already used too much space in the buffer, returns -1.
1209 * If the available space is already smaller than newbuflen, returns 0
1210 * and does nothing. Otherwise, adjusts xdr->buf->buflen to newbuflen
1211 * and ensures xdr->end is set at most offset newbuflen from the start
1214 int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
1216 struct xdr_buf *buf = xdr->buf;
1217 int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
1218 int end_offset = buf->len + left_in_this_buf;
1220 if (newbuflen < 0 || newbuflen < buf->len)
1222 if (newbuflen > buf->buflen)
1224 if (newbuflen < end_offset)
1225 xdr->end = (void *)xdr->end + newbuflen - end_offset;
1226 buf->buflen = newbuflen;
1229 EXPORT_SYMBOL(xdr_restrict_buflen);
1232 * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
1233 * @xdr: pointer to xdr_stream
1234 * @pages: array of pages to insert
1235 * @base: starting offset of first data byte in @pages
1236 * @len: number of data bytes in @pages to insert
1238 * After the @pages are added, the tail iovec is instantiated pointing to
1239 * end of the head buffer, and the stream is set up to encode subsequent
1240 * items into the tail.
1242 void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
1245 struct xdr_buf *buf = xdr->buf;
1246 struct kvec *tail = buf->tail;
1249 buf->page_base = base;
1250 buf->page_len = len;
1252 tail->iov_base = xdr->p;
1257 unsigned int pad = 4 - (len & 3);
1259 BUG_ON(xdr->p >= xdr->end);
1260 tail->iov_base = (char *)xdr->p + (len & 3);
1261 tail->iov_len += pad;
1268 EXPORT_SYMBOL_GPL(xdr_write_pages);
1270 static unsigned int xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
1271 unsigned int base, unsigned int len)
1273 if (len > iov->iov_len)
1275 if (unlikely(base > len))
1277 xdr->p = (__be32*)(iov->iov_base + base);
1278 xdr->end = (__be32*)(iov->iov_base + len);
1280 xdr->page_ptr = NULL;
1284 static unsigned int xdr_set_tail_base(struct xdr_stream *xdr,
1285 unsigned int base, unsigned int len)
1287 struct xdr_buf *buf = xdr->buf;
1289 xdr_stream_set_pos(xdr, base + buf->page_len + buf->head->iov_len);
1290 return xdr_set_iov(xdr, buf->tail, base, len);
1293 static unsigned int xdr_set_page_base(struct xdr_stream *xdr,
1294 unsigned int base, unsigned int len)
1297 unsigned int maxlen;
1302 maxlen = xdr->buf->page_len;
1310 xdr_stream_page_set_pos(xdr, base);
1311 base += xdr->buf->page_base;
1313 pgnr = base >> PAGE_SHIFT;
1314 xdr->page_ptr = &xdr->buf->pages[pgnr];
1315 kaddr = page_address(*xdr->page_ptr);
1317 pgoff = base & ~PAGE_MASK;
1318 xdr->p = (__be32*)(kaddr + pgoff);
1320 pgend = pgoff + len;
1321 if (pgend > PAGE_SIZE)
1323 xdr->end = (__be32*)(kaddr + pgend);
1328 static void xdr_set_page(struct xdr_stream *xdr, unsigned int base,
1331 if (xdr_set_page_base(xdr, base, len) == 0) {
1332 base -= xdr->buf->page_len;
1333 xdr_set_tail_base(xdr, base, len);
1337 static void xdr_set_next_page(struct xdr_stream *xdr)
1339 unsigned int newbase;
1341 newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
1342 newbase -= xdr->buf->page_base;
1343 if (newbase < xdr->buf->page_len)
1344 xdr_set_page_base(xdr, newbase, xdr_stream_remaining(xdr));
1346 xdr_set_tail_base(xdr, 0, xdr_stream_remaining(xdr));
1349 static bool xdr_set_next_buffer(struct xdr_stream *xdr)
1351 if (xdr->page_ptr != NULL)
1352 xdr_set_next_page(xdr);
1353 else if (xdr->iov == xdr->buf->head)
1354 xdr_set_page(xdr, 0, xdr_stream_remaining(xdr));
1355 return xdr->p != xdr->end;
1359 * xdr_init_decode - Initialize an xdr_stream for decoding data.
1360 * @xdr: pointer to xdr_stream struct
1361 * @buf: pointer to XDR buffer from which to decode data
1362 * @p: current pointer inside XDR buffer
1363 * @rqst: pointer to controlling rpc_rqst, for debugging
1365 void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
1366 struct rpc_rqst *rqst)
1369 xdr_reset_scratch_buffer(xdr);
1370 xdr->nwords = XDR_QUADLEN(buf->len);
1371 if (xdr_set_iov(xdr, buf->head, 0, buf->len) == 0 &&
1372 xdr_set_page_base(xdr, 0, buf->len) == 0)
1373 xdr_set_iov(xdr, buf->tail, 0, buf->len);
1374 if (p != NULL && p > xdr->p && xdr->end >= p) {
1375 xdr->nwords -= p - xdr->p;
1380 EXPORT_SYMBOL_GPL(xdr_init_decode);
1383 * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages
1384 * @xdr: pointer to xdr_stream struct
1385 * @buf: pointer to XDR buffer from which to decode data
1386 * @pages: list of pages to decode into
1387 * @len: length in bytes of buffer in pages
1389 void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
1390 struct page **pages, unsigned int len)
1392 memset(buf, 0, sizeof(*buf));
1394 buf->page_len = len;
1397 xdr_init_decode(xdr, buf, NULL, NULL);
1399 EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
1401 static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1403 unsigned int nwords = XDR_QUADLEN(nbytes);
1405 __be32 *q = p + nwords;
1407 if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
1410 xdr->nwords -= nwords;
1414 static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
1417 char *cpdest = xdr->scratch.iov_base;
1418 size_t cplen = (char *)xdr->end - (char *)xdr->p;
1420 if (nbytes > xdr->scratch.iov_len)
1422 p = __xdr_inline_decode(xdr, cplen);
1425 memcpy(cpdest, p, cplen);
1426 if (!xdr_set_next_buffer(xdr))
1430 p = __xdr_inline_decode(xdr, nbytes);
1433 memcpy(cpdest, p, nbytes);
1434 return xdr->scratch.iov_base;
1436 trace_rpc_xdr_overflow(xdr, nbytes);
1441 * xdr_inline_decode - Retrieve XDR data to decode
1442 * @xdr: pointer to xdr_stream struct
1443 * @nbytes: number of bytes of data to decode
1445 * Check if the input buffer is long enough to enable us to decode
1446 * 'nbytes' more bytes of data starting at the current position.
1447 * If so return the current pointer, then update the current
1450 __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1454 if (unlikely(nbytes == 0))
1456 if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1458 p = __xdr_inline_decode(xdr, nbytes);
1461 return xdr_copy_to_scratch(xdr, nbytes);
1463 trace_rpc_xdr_overflow(xdr, nbytes);
1466 EXPORT_SYMBOL_GPL(xdr_inline_decode);
1468 static void xdr_realign_pages(struct xdr_stream *xdr)
1470 struct xdr_buf *buf = xdr->buf;
1471 struct kvec *iov = buf->head;
1472 unsigned int cur = xdr_stream_pos(xdr);
1473 unsigned int copied;
1475 /* Realign pages to current pointer position */
1476 if (iov->iov_len > cur) {
1477 copied = xdr_shrink_bufhead(buf, cur);
1478 trace_rpc_xdr_alignment(xdr, cur, copied);
1479 xdr_set_page(xdr, 0, buf->page_len);
1483 static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
1485 struct xdr_buf *buf = xdr->buf;
1486 unsigned int nwords = XDR_QUADLEN(len);
1487 unsigned int copied;
1489 if (xdr->nwords == 0)
1492 xdr_realign_pages(xdr);
1493 if (nwords > xdr->nwords) {
1494 nwords = xdr->nwords;
1497 if (buf->page_len <= len)
1498 len = buf->page_len;
1499 else if (nwords < xdr->nwords) {
1500 /* Truncate page data and move it into the tail */
1501 copied = xdr_shrink_pagelen(buf, len);
1502 trace_rpc_xdr_alignment(xdr, len, copied);
1508 * xdr_read_pages - align page-based XDR data to current pointer position
1509 * @xdr: pointer to xdr_stream struct
1510 * @len: number of bytes of page data
1512 * Moves data beyond the current pointer position from the XDR head[] buffer
1513 * into the page list. Any data that lies beyond current position + @len
1514 * bytes is moved into the XDR tail[]. The xdr_stream current position is
1515 * then advanced past that data to align to the next XDR object in the tail.
1517 * Returns the number of XDR encoded bytes now contained in the pages
1519 unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
1521 unsigned int nwords = XDR_QUADLEN(len);
1522 unsigned int base, end, pglen;
1524 pglen = xdr_align_pages(xdr, nwords << 2);
1528 base = (nwords << 2) - pglen;
1529 end = xdr_stream_remaining(xdr) - pglen;
1531 xdr_set_tail_base(xdr, base, end);
1532 return len <= pglen ? len : pglen;
1534 EXPORT_SYMBOL_GPL(xdr_read_pages);
1537 * xdr_set_pagelen - Sets the length of the XDR pages
1538 * @xdr: pointer to xdr_stream struct
1539 * @len: new length of the XDR page data
1541 * Either grows or shrinks the length of the xdr pages by setting pagelen to
1542 * @len bytes. When shrinking, any extra data is moved into buf->tail, whereas
1543 * when growing any data beyond the current pointer is moved into the tail.
1545 * Returns True if the operation was successful, and False otherwise.
1547 void xdr_set_pagelen(struct xdr_stream *xdr, unsigned int len)
1549 struct xdr_buf *buf = xdr->buf;
1550 size_t remaining = xdr_stream_remaining(xdr);
1553 if (len < buf->page_len) {
1554 base = buf->page_len - len;
1555 xdr_shrink_pagelen(buf, len);
1557 xdr_buf_head_shift_right(buf, xdr_stream_pos(xdr),
1558 buf->page_len, remaining);
1559 if (len > buf->page_len)
1560 xdr_buf_try_expand(buf, len - buf->page_len);
1562 xdr_set_tail_base(xdr, base, remaining);
1564 EXPORT_SYMBOL_GPL(xdr_set_pagelen);
1567 * xdr_enter_page - decode data from the XDR page
1568 * @xdr: pointer to xdr_stream struct
1569 * @len: number of bytes of page data
1571 * Moves data beyond the current pointer position from the XDR head[] buffer
1572 * into the page list. Any data that lies beyond current position + "len"
1573 * bytes is moved into the XDR tail[]. The current pointer is then
1574 * repositioned at the beginning of the first XDR page.
1576 void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
1578 len = xdr_align_pages(xdr, len);
1580 * Position current pointer at beginning of tail, and
1581 * set remaining message length.
1584 xdr_set_page_base(xdr, 0, len);
1586 EXPORT_SYMBOL_GPL(xdr_enter_page);
1588 static const struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
1590 void xdr_buf_from_iov(const struct kvec *iov, struct xdr_buf *buf)
1592 buf->head[0] = *iov;
1593 buf->tail[0] = empty_iov;
1595 buf->buflen = buf->len = iov->iov_len;
1597 EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
1600 * xdr_buf_subsegment - set subbuf to a portion of buf
1601 * @buf: an xdr buffer
1602 * @subbuf: the result buffer
1603 * @base: beginning of range in bytes
1604 * @len: length of range in bytes
1606 * sets @subbuf to an xdr buffer representing the portion of @buf of
1607 * length @len starting at offset @base.
1609 * @buf and @subbuf may be pointers to the same struct xdr_buf.
1611 * Returns -1 if base or length are out of bounds.
1613 int xdr_buf_subsegment(const struct xdr_buf *buf, struct xdr_buf *subbuf,
1614 unsigned int base, unsigned int len)
1616 subbuf->buflen = subbuf->len = len;
1617 if (base < buf->head[0].iov_len) {
1618 subbuf->head[0].iov_base = buf->head[0].iov_base + base;
1619 subbuf->head[0].iov_len = min_t(unsigned int, len,
1620 buf->head[0].iov_len - base);
1621 len -= subbuf->head[0].iov_len;
1624 base -= buf->head[0].iov_len;
1625 subbuf->head[0].iov_base = buf->head[0].iov_base;
1626 subbuf->head[0].iov_len = 0;
1629 if (base < buf->page_len) {
1630 subbuf->page_len = min(buf->page_len - base, len);
1631 base += buf->page_base;
1632 subbuf->page_base = base & ~PAGE_MASK;
1633 subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
1634 len -= subbuf->page_len;
1637 base -= buf->page_len;
1638 subbuf->pages = buf->pages;
1639 subbuf->page_base = 0;
1640 subbuf->page_len = 0;
1643 if (base < buf->tail[0].iov_len) {
1644 subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
1645 subbuf->tail[0].iov_len = min_t(unsigned int, len,
1646 buf->tail[0].iov_len - base);
1647 len -= subbuf->tail[0].iov_len;
1650 base -= buf->tail[0].iov_len;
1651 subbuf->tail[0].iov_base = buf->tail[0].iov_base;
1652 subbuf->tail[0].iov_len = 0;
1659 EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
1662 * xdr_stream_subsegment - set @subbuf to a portion of @xdr
1663 * @xdr: an xdr_stream set up for decoding
1664 * @subbuf: the result buffer
1665 * @nbytes: length of @xdr to extract, in bytes
1667 * Sets up @subbuf to represent a portion of @xdr. The portion
1668 * starts at the current offset in @xdr, and extends for a length
1669 * of @nbytes. If this is successful, @xdr is advanced to the next
1670 * XDR data item following that portion.
1673 * %true: @subbuf has been initialized, and @xdr has been advanced.
1674 * %false: a bounds error has occurred
1676 bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
1677 unsigned int nbytes)
1679 unsigned int start = xdr_stream_pos(xdr);
1680 unsigned int remaining, len;
1682 /* Extract @subbuf and bounds-check the fn arguments */
1683 if (xdr_buf_subsegment(xdr->buf, subbuf, start, nbytes))
1686 /* Advance @xdr by @nbytes */
1687 for (remaining = nbytes; remaining;) {
1688 if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1691 len = (char *)xdr->end - (char *)xdr->p;
1692 if (remaining <= len) {
1693 xdr->p = (__be32 *)((char *)xdr->p +
1694 (remaining + xdr_pad_size(nbytes)));
1698 xdr->p = (__be32 *)((char *)xdr->p + len);
1703 xdr_stream_set_pos(xdr, start + nbytes);
1706 EXPORT_SYMBOL_GPL(xdr_stream_subsegment);
1709 * xdr_stream_move_subsegment - Move part of a stream to another position
1710 * @xdr: the source xdr_stream
1711 * @offset: the source offset of the segment
1712 * @target: the target offset of the segment
1713 * @length: the number of bytes to move
1715 * Moves @length bytes from @offset to @target in the xdr_stream, overwriting
1716 * anything in its space. Returns the number of bytes in the segment.
1718 unsigned int xdr_stream_move_subsegment(struct xdr_stream *xdr, unsigned int offset,
1719 unsigned int target, unsigned int length)
1724 if (offset < target) {
1725 shift = target - offset;
1726 if (xdr_buf_subsegment(xdr->buf, &buf, offset, shift + length) < 0)
1728 xdr_buf_head_shift_right(&buf, 0, length, shift);
1729 } else if (offset > target) {
1730 shift = offset - target;
1731 if (xdr_buf_subsegment(xdr->buf, &buf, target, shift + length) < 0)
1733 xdr_buf_head_shift_left(&buf, shift, length, shift);
1737 EXPORT_SYMBOL_GPL(xdr_stream_move_subsegment);
1740 * xdr_stream_zero - zero out a portion of an xdr_stream
1741 * @xdr: an xdr_stream to zero out
1742 * @offset: the starting point in the stream
1743 * @length: the number of bytes to zero
1745 unsigned int xdr_stream_zero(struct xdr_stream *xdr, unsigned int offset,
1746 unsigned int length)
1750 if (xdr_buf_subsegment(xdr->buf, &buf, offset, length) < 0)
1752 if (buf.head[0].iov_len)
1753 xdr_buf_iov_zero(buf.head, 0, buf.head[0].iov_len);
1754 if (buf.page_len > 0)
1755 xdr_buf_pages_zero(&buf, 0, buf.page_len);
1756 if (buf.tail[0].iov_len)
1757 xdr_buf_iov_zero(buf.tail, 0, buf.tail[0].iov_len);
1760 EXPORT_SYMBOL_GPL(xdr_stream_zero);
1763 * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
1764 * @buf: buf to be trimmed
1765 * @len: number of bytes to reduce "buf" by
1767 * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
1768 * that it's possible that we'll trim less than that amount if the xdr_buf is
1769 * too small, or if (for instance) it's all in the head and the parser has
1770 * already read too far into it.
1772 void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
1775 unsigned int trim = len;
1777 if (buf->tail[0].iov_len) {
1778 cur = min_t(size_t, buf->tail[0].iov_len, trim);
1779 buf->tail[0].iov_len -= cur;
1785 if (buf->page_len) {
1786 cur = min_t(unsigned int, buf->page_len, trim);
1787 buf->page_len -= cur;
1793 if (buf->head[0].iov_len) {
1794 cur = min_t(size_t, buf->head[0].iov_len, trim);
1795 buf->head[0].iov_len -= cur;
1799 buf->len -= (len - trim);
1801 EXPORT_SYMBOL_GPL(xdr_buf_trim);
1803 static void __read_bytes_from_xdr_buf(const struct xdr_buf *subbuf,
1804 void *obj, unsigned int len)
1806 unsigned int this_len;
1808 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1809 memcpy(obj, subbuf->head[0].iov_base, this_len);
1812 this_len = min_t(unsigned int, len, subbuf->page_len);
1813 _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
1816 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1817 memcpy(obj, subbuf->tail[0].iov_base, this_len);
1820 /* obj is assumed to point to allocated memory of size at least len: */
1821 int read_bytes_from_xdr_buf(const struct xdr_buf *buf, unsigned int base,
1822 void *obj, unsigned int len)
1824 struct xdr_buf subbuf;
1827 status = xdr_buf_subsegment(buf, &subbuf, base, len);
1830 __read_bytes_from_xdr_buf(&subbuf, obj, len);
1833 EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
1835 static void __write_bytes_to_xdr_buf(const struct xdr_buf *subbuf,
1836 void *obj, unsigned int len)
1838 unsigned int this_len;
1840 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1841 memcpy(subbuf->head[0].iov_base, obj, this_len);
1844 this_len = min_t(unsigned int, len, subbuf->page_len);
1845 _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
1848 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1849 memcpy(subbuf->tail[0].iov_base, obj, this_len);
1852 /* obj is assumed to point to allocated memory of size at least len: */
1853 int write_bytes_to_xdr_buf(const struct xdr_buf *buf, unsigned int base,
1854 void *obj, unsigned int len)
1856 struct xdr_buf subbuf;
1859 status = xdr_buf_subsegment(buf, &subbuf, base, len);
1862 __write_bytes_to_xdr_buf(&subbuf, obj, len);
1865 EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
1867 int xdr_decode_word(const struct xdr_buf *buf, unsigned int base, u32 *obj)
1872 status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
1875 *obj = be32_to_cpu(raw);
1878 EXPORT_SYMBOL_GPL(xdr_decode_word);
1880 int xdr_encode_word(const struct xdr_buf *buf, unsigned int base, u32 obj)
1882 __be32 raw = cpu_to_be32(obj);
1884 return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
1886 EXPORT_SYMBOL_GPL(xdr_encode_word);
1888 /* Returns 0 on success, or else a negative error code. */
1889 static int xdr_xcode_array2(const struct xdr_buf *buf, unsigned int base,
1890 struct xdr_array2_desc *desc, int encode)
1892 char *elem = NULL, *c;
1893 unsigned int copied = 0, todo, avail_here;
1894 struct page **ppages = NULL;
1898 if (xdr_encode_word(buf, base, desc->array_len) != 0)
1901 if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
1902 desc->array_len > desc->array_maxlen ||
1903 (unsigned long) base + 4 + desc->array_len *
1904 desc->elem_size > buf->len)
1912 todo = desc->array_len * desc->elem_size;
1915 if (todo && base < buf->head->iov_len) {
1916 c = buf->head->iov_base + base;
1917 avail_here = min_t(unsigned int, todo,
1918 buf->head->iov_len - base);
1921 while (avail_here >= desc->elem_size) {
1922 err = desc->xcode(desc, c);
1925 c += desc->elem_size;
1926 avail_here -= desc->elem_size;
1930 elem = kmalloc(desc->elem_size, GFP_KERNEL);
1936 err = desc->xcode(desc, elem);
1939 memcpy(c, elem, avail_here);
1941 memcpy(elem, c, avail_here);
1942 copied = avail_here;
1944 base = buf->head->iov_len; /* align to start of pages */
1947 /* process pages array */
1948 base -= buf->head->iov_len;
1949 if (todo && base < buf->page_len) {
1950 unsigned int avail_page;
1952 avail_here = min(todo, buf->page_len - base);
1955 base += buf->page_base;
1956 ppages = buf->pages + (base >> PAGE_SHIFT);
1958 avail_page = min_t(unsigned int, PAGE_SIZE - base,
1960 c = kmap(*ppages) + base;
1962 while (avail_here) {
1963 avail_here -= avail_page;
1964 if (copied || avail_page < desc->elem_size) {
1965 unsigned int l = min(avail_page,
1966 desc->elem_size - copied);
1968 elem = kmalloc(desc->elem_size,
1976 err = desc->xcode(desc, elem);
1980 memcpy(c, elem + copied, l);
1982 if (copied == desc->elem_size)
1985 memcpy(elem + copied, c, l);
1987 if (copied == desc->elem_size) {
1988 err = desc->xcode(desc, elem);
1997 while (avail_page >= desc->elem_size) {
1998 err = desc->xcode(desc, c);
2001 c += desc->elem_size;
2002 avail_page -= desc->elem_size;
2005 unsigned int l = min(avail_page,
2006 desc->elem_size - copied);
2008 elem = kmalloc(desc->elem_size,
2016 err = desc->xcode(desc, elem);
2020 memcpy(c, elem + copied, l);
2022 if (copied == desc->elem_size)
2025 memcpy(elem + copied, c, l);
2027 if (copied == desc->elem_size) {
2028 err = desc->xcode(desc, elem);
2041 avail_page = min(avail_here,
2042 (unsigned int) PAGE_SIZE);
2044 base = buf->page_len; /* align to start of tail */
2048 base -= buf->page_len;
2050 c = buf->tail->iov_base + base;
2052 unsigned int l = desc->elem_size - copied;
2055 memcpy(c, elem + copied, l);
2057 memcpy(elem + copied, c, l);
2058 err = desc->xcode(desc, elem);
2066 err = desc->xcode(desc, c);
2069 c += desc->elem_size;
2070 todo -= desc->elem_size;
2082 int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base,
2083 struct xdr_array2_desc *desc)
2085 if (base >= buf->len)
2088 return xdr_xcode_array2(buf, base, desc, 0);
2090 EXPORT_SYMBOL_GPL(xdr_decode_array2);
2092 int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base,
2093 struct xdr_array2_desc *desc)
2095 if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
2096 buf->head->iov_len + buf->page_len + buf->tail->iov_len)
2099 return xdr_xcode_array2(buf, base, desc, 1);
2101 EXPORT_SYMBOL_GPL(xdr_encode_array2);
2103 int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset,
2105 int (*actor)(struct scatterlist *, void *), void *data)
2108 unsigned int page_len, thislen, page_offset;
2109 struct scatterlist sg[1];
2111 sg_init_table(sg, 1);
2113 if (offset >= buf->head[0].iov_len) {
2114 offset -= buf->head[0].iov_len;
2116 thislen = buf->head[0].iov_len - offset;
2119 sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
2120 ret = actor(sg, data);
2129 if (offset >= buf->page_len) {
2130 offset -= buf->page_len;
2132 page_len = buf->page_len - offset;
2136 page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
2137 i = (offset + buf->page_base) >> PAGE_SHIFT;
2138 thislen = PAGE_SIZE - page_offset;
2140 if (thislen > page_len)
2142 sg_set_page(sg, buf->pages[i], thislen, page_offset);
2143 ret = actor(sg, data);
2146 page_len -= thislen;
2149 thislen = PAGE_SIZE;
2150 } while (page_len != 0);
2155 if (offset < buf->tail[0].iov_len) {
2156 thislen = buf->tail[0].iov_len - offset;
2159 sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
2160 ret = actor(sg, data);
2168 EXPORT_SYMBOL_GPL(xdr_process_buf);
2171 * xdr_stream_decode_opaque - Decode variable length opaque
2172 * @xdr: pointer to xdr_stream
2173 * @ptr: location to store opaque data
2174 * @size: size of storage buffer @ptr
2177 * On success, returns size of object stored in *@ptr
2178 * %-EBADMSG on XDR buffer overflow
2179 * %-EMSGSIZE on overflow of storage buffer @ptr
2181 ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, size_t size)
2186 ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
2189 memcpy(ptr, p, ret);
2192 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque);
2195 * xdr_stream_decode_opaque_dup - Decode and duplicate variable length opaque
2196 * @xdr: pointer to xdr_stream
2197 * @ptr: location to store pointer to opaque data
2198 * @maxlen: maximum acceptable object size
2199 * @gfp_flags: GFP mask to use
2202 * On success, returns size of object stored in *@ptr
2203 * %-EBADMSG on XDR buffer overflow
2204 * %-EMSGSIZE if the size of the object would exceed @maxlen
2205 * %-ENOMEM on memory allocation failure
2207 ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
2208 size_t maxlen, gfp_t gfp_flags)
2213 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2215 *ptr = kmemdup(p, ret, gfp_flags);
2223 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_dup);
2226 * xdr_stream_decode_string - Decode variable length string
2227 * @xdr: pointer to xdr_stream
2228 * @str: location to store string
2229 * @size: size of storage buffer @str
2232 * On success, returns length of NUL-terminated string stored in *@str
2233 * %-EBADMSG on XDR buffer overflow
2234 * %-EMSGSIZE on overflow of storage buffer @str
2236 ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, size_t size)
2241 ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
2243 memcpy(str, p, ret);
2250 EXPORT_SYMBOL_GPL(xdr_stream_decode_string);
2253 * xdr_stream_decode_string_dup - Decode and duplicate variable length string
2254 * @xdr: pointer to xdr_stream
2255 * @str: location to store pointer to string
2256 * @maxlen: maximum acceptable string length
2257 * @gfp_flags: GFP mask to use
2260 * On success, returns length of NUL-terminated string stored in *@ptr
2261 * %-EBADMSG on XDR buffer overflow
2262 * %-EMSGSIZE if the size of the string would exceed @maxlen
2263 * %-ENOMEM on memory allocation failure
2265 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
2266 size_t maxlen, gfp_t gfp_flags)
2271 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2273 char *s = kmemdup_nul(p, ret, gfp_flags);
2283 EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);
2286 * xdr_stream_decode_opaque_auth - Decode struct opaque_auth (RFC5531 S8.2)
2287 * @xdr: pointer to xdr_stream
2288 * @flavor: location to store decoded flavor
2289 * @body: location to store decode body
2290 * @body_len: location to store length of decoded body
2293 * On success, returns the number of buffer bytes consumed
2294 * %-EBADMSG on XDR buffer overflow
2295 * %-EMSGSIZE if the decoded size of the body field exceeds 400 octets
2297 ssize_t xdr_stream_decode_opaque_auth(struct xdr_stream *xdr, u32 *flavor,
2298 void **body, unsigned int *body_len)
2302 len = xdr_stream_decode_u32(xdr, flavor);
2303 if (unlikely(len < 0))
2305 ret = xdr_stream_decode_opaque_inline(xdr, body, RPC_MAX_AUTH_SIZE);
2306 if (unlikely(ret < 0))
2311 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_auth);
2314 * xdr_stream_encode_opaque_auth - Encode struct opaque_auth (RFC5531 S8.2)
2315 * @xdr: pointer to xdr_stream
2316 * @flavor: verifier flavor to encode
2317 * @body: content of body to encode
2318 * @body_len: length of body to encode
2321 * On success, returns length in bytes of XDR buffer consumed
2322 * %-EBADMSG on XDR buffer overflow
2323 * %-EMSGSIZE if the size of @body exceeds 400 octets
2325 ssize_t xdr_stream_encode_opaque_auth(struct xdr_stream *xdr, u32 flavor,
2326 void *body, unsigned int body_len)
2330 if (unlikely(body_len > RPC_MAX_AUTH_SIZE))
2332 len = xdr_stream_encode_u32(xdr, flavor);
2333 if (unlikely(len < 0))
2335 ret = xdr_stream_encode_opaque(xdr, body, body_len);
2336 if (unlikely(ret < 0))
2340 EXPORT_SYMBOL_GPL(xdr_stream_encode_opaque_auth);