2 * linux/net/sunrpc/xdr.c
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/socket.h>
11 #include <linux/string.h>
12 #include <linux/kernel.h>
13 #include <linux/pagemap.h>
14 #include <linux/errno.h>
16 #include <linux/net.h>
18 #include <linux/sunrpc/xdr.h>
19 #include <linux/sunrpc/msg_prot.h>
22 * XDR functions for basic NFS types
25 xdr_encode_netobj(u32 *p, const struct xdr_netobj *obj)
27 unsigned int quadlen = XDR_QUADLEN(obj->len);
29 p[quadlen] = 0; /* zero trailing bytes */
30 *p++ = htonl(obj->len);
31 memcpy(p, obj->data, obj->len);
32 return p + XDR_QUADLEN(obj->len);
36 xdr_decode_netobj(u32 *p, struct xdr_netobj *obj)
40 if ((len = ntohl(*p++)) > XDR_MAX_NETOBJ)
44 return p + XDR_QUADLEN(len);
48 * xdr_encode_opaque_fixed - Encode fixed length opaque data
49 * @p: pointer to current position in XDR buffer.
50 * @ptr: pointer to data to encode (or NULL)
51 * @nbytes: size of data.
53 * Copy the array of data of length nbytes at ptr to the XDR buffer
54 * at position p, then align to the next 32-bit boundary by padding
55 * with zero bytes (see RFC1832).
56 * Note: if ptr is NULL, only the padding is performed.
58 * Returns the updated current XDR buffer position
61 u32 *xdr_encode_opaque_fixed(u32 *p, const void *ptr, unsigned int nbytes)
63 if (likely(nbytes != 0)) {
64 unsigned int quadlen = XDR_QUADLEN(nbytes);
65 unsigned int padding = (quadlen << 2) - nbytes;
68 memcpy(p, ptr, nbytes);
70 memset((char *)p + nbytes, 0, padding);
75 EXPORT_SYMBOL(xdr_encode_opaque_fixed);
78 * xdr_encode_opaque - Encode variable length opaque data
79 * @p: pointer to current position in XDR buffer.
80 * @ptr: pointer to data to encode (or NULL)
81 * @nbytes: size of data.
83 * Returns the updated current XDR buffer position
85 u32 *xdr_encode_opaque(u32 *p, const void *ptr, unsigned int nbytes)
88 return xdr_encode_opaque_fixed(p, ptr, nbytes);
90 EXPORT_SYMBOL(xdr_encode_opaque);
93 xdr_encode_string(u32 *p, const char *string)
95 return xdr_encode_array(p, string, strlen(string));
99 xdr_decode_string(u32 *p, char **sp, int *lenp, int maxlen)
104 if ((len = ntohl(*p++)) > maxlen)
108 if ((len % 4) != 0) {
111 string = (char *) (p - 1);
112 memmove(string, p, len);
116 return p + XDR_QUADLEN(len);
120 xdr_decode_string_inplace(u32 *p, char **sp, int *lenp, int maxlen)
124 if ((len = ntohl(*p++)) > maxlen)
128 return p + XDR_QUADLEN(len);
132 xdr_encode_pages(struct xdr_buf *xdr, struct page **pages, unsigned int base,
135 struct kvec *tail = xdr->tail;
139 xdr->page_base = base;
142 p = (u32 *)xdr->head[0].iov_base + XDR_QUADLEN(xdr->head[0].iov_len);
147 unsigned int pad = 4 - (len & 3);
150 tail->iov_base = (char *)p + (len & 3);
159 xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
160 struct page **pages, unsigned int base, unsigned int len)
162 struct kvec *head = xdr->head;
163 struct kvec *tail = xdr->tail;
164 char *buf = (char *)head->iov_base;
165 unsigned int buflen = head->iov_len;
167 head->iov_len = offset;
170 xdr->page_base = base;
173 tail->iov_base = buf + offset;
174 tail->iov_len = buflen - offset;
180 xdr_partial_copy_from_skb(struct xdr_buf *xdr, unsigned int base,
182 skb_read_actor_t copy_actor)
184 struct page **ppage = xdr->pages;
185 unsigned int len, pglen = xdr->page_len;
188 len = xdr->head[0].iov_len;
191 ret = copy_actor(desc, (char *)xdr->head[0].iov_base + base, len);
192 if (ret != len || !desc->count)
204 if (base || xdr->page_base) {
206 base += xdr->page_base;
207 ppage += base >> PAGE_CACHE_SHIFT;
208 base &= ~PAGE_CACHE_MASK;
213 /* ACL likes to be lazy in allocating pages - ACLs
214 * are small by default but can get huge. */
215 if (unlikely(*ppage == NULL)) {
216 *ppage = alloc_page(GFP_ATOMIC);
217 if (unlikely(*ppage == NULL))
221 len = PAGE_CACHE_SIZE;
222 kaddr = kmap_atomic(*ppage, KM_SKB_SUNRPC_DATA);
227 ret = copy_actor(desc, kaddr + base, len);
232 ret = copy_actor(desc, kaddr, len);
234 flush_dcache_page(*ppage);
235 kunmap_atomic(kaddr, KM_SKB_SUNRPC_DATA);
236 if (ret != len || !desc->count)
239 } while ((pglen -= len) != 0);
241 len = xdr->tail[0].iov_len;
243 copy_actor(desc, (char *)xdr->tail[0].iov_base + base, len - base);
250 xdr_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen,
251 struct xdr_buf *xdr, unsigned int base, int msgflags)
253 struct page **ppage = xdr->pages;
254 unsigned int len, pglen = xdr->page_len;
256 ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int);
258 len = xdr->head[0].iov_len;
259 if (base < len || (addr != NULL && base == 0)) {
261 .iov_base = xdr->head[0].iov_base + base,
262 .iov_len = len - base,
264 struct msghdr msg = {
266 .msg_namelen = addrlen,
267 .msg_flags = msgflags,
270 msg.msg_flags |= MSG_MORE;
272 if (iov.iov_len != 0)
273 err = kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
275 err = kernel_sendmsg(sock, &msg, NULL, 0, 0);
280 if (err != iov.iov_len)
292 if (base || xdr->page_base) {
294 base += xdr->page_base;
295 ppage += base >> PAGE_CACHE_SHIFT;
296 base &= ~PAGE_CACHE_MASK;
299 sendpage = sock->ops->sendpage ? : sock_no_sendpage;
301 int flags = msgflags;
303 len = PAGE_CACHE_SIZE;
309 if (pglen != len || xdr->tail[0].iov_len != 0)
312 /* Hmm... We might be dealing with highmem pages */
313 if (PageHighMem(*ppage))
314 sendpage = sock_no_sendpage;
315 err = sendpage(sock, *ppage, base, len, flags);
324 } while ((pglen -= len) != 0);
326 len = xdr->tail[0].iov_len;
329 .iov_base = xdr->tail[0].iov_base + base,
330 .iov_len = len - base,
332 struct msghdr msg = {
333 .msg_flags = msgflags,
335 err = kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
347 * Helper routines for doing 'memmove' like operations on a struct xdr_buf
349 * _shift_data_right_pages
350 * @pages: vector of pages containing both the source and dest memory area.
351 * @pgto_base: page vector address of destination
352 * @pgfrom_base: page vector address of source
353 * @len: number of bytes to copy
355 * Note: the addresses pgto_base and pgfrom_base are both calculated in
357 * if a memory area starts at byte 'base' in page 'pages[i]',
358 * then its address is given as (i << PAGE_CACHE_SHIFT) + base
359 * Also note: pgfrom_base must be < pgto_base, but the memory areas
360 * they point to may overlap.
363 _shift_data_right_pages(struct page **pages, size_t pgto_base,
364 size_t pgfrom_base, size_t len)
366 struct page **pgfrom, **pgto;
370 BUG_ON(pgto_base <= pgfrom_base);
375 pgto = pages + (pgto_base >> PAGE_CACHE_SHIFT);
376 pgfrom = pages + (pgfrom_base >> PAGE_CACHE_SHIFT);
378 pgto_base &= ~PAGE_CACHE_MASK;
379 pgfrom_base &= ~PAGE_CACHE_MASK;
382 /* Are any pointers crossing a page boundary? */
383 if (pgto_base == 0) {
384 flush_dcache_page(*pgto);
385 pgto_base = PAGE_CACHE_SIZE;
388 if (pgfrom_base == 0) {
389 pgfrom_base = PAGE_CACHE_SIZE;
394 if (copy > pgto_base)
396 if (copy > pgfrom_base)
401 vto = kmap_atomic(*pgto, KM_USER0);
402 vfrom = kmap_atomic(*pgfrom, KM_USER1);
403 memmove(vto + pgto_base, vfrom + pgfrom_base, copy);
404 kunmap_atomic(vfrom, KM_USER1);
405 kunmap_atomic(vto, KM_USER0);
407 } while ((len -= copy) != 0);
408 flush_dcache_page(*pgto);
413 * @pages: array of pages
414 * @pgbase: page vector address of destination
415 * @p: pointer to source data
418 * Copies data from an arbitrary memory location into an array of pages
419 * The copy is assumed to be non-overlapping.
422 _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
428 pgto = pages + (pgbase >> PAGE_CACHE_SHIFT);
429 pgbase &= ~PAGE_CACHE_MASK;
432 copy = PAGE_CACHE_SIZE - pgbase;
436 vto = kmap_atomic(*pgto, KM_USER0);
437 memcpy(vto + pgbase, p, copy);
438 kunmap_atomic(vto, KM_USER0);
441 if (pgbase == PAGE_CACHE_SIZE) {
442 flush_dcache_page(*pgto);
448 } while ((len -= copy) != 0);
449 flush_dcache_page(*pgto);
454 * @p: pointer to destination
455 * @pages: array of pages
456 * @pgbase: offset of source data
459 * Copies data into an arbitrary memory location from an array of pages
460 * The copy is assumed to be non-overlapping.
463 _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
465 struct page **pgfrom;
469 pgfrom = pages + (pgbase >> PAGE_CACHE_SHIFT);
470 pgbase &= ~PAGE_CACHE_MASK;
473 copy = PAGE_CACHE_SIZE - pgbase;
477 vfrom = kmap_atomic(*pgfrom, KM_USER0);
478 memcpy(p, vfrom + pgbase, copy);
479 kunmap_atomic(vfrom, KM_USER0);
482 if (pgbase == PAGE_CACHE_SIZE) {
488 } while ((len -= copy) != 0);
494 * @len: bytes to remove from buf->head[0]
496 * Shrinks XDR buffer's header kvec buf->head[0] by
497 * 'len' bytes. The extra data is not lost, but is instead
498 * moved into the inlined pages and/or the tail.
501 xdr_shrink_bufhead(struct xdr_buf *buf, size_t len)
503 struct kvec *head, *tail;
505 unsigned int pglen = buf->page_len;
509 BUG_ON (len > head->iov_len);
511 /* Shift the tail first */
512 if (tail->iov_len != 0) {
513 if (tail->iov_len > len) {
514 copy = tail->iov_len - len;
515 memmove((char *)tail->iov_base + len,
516 tail->iov_base, copy);
518 /* Copy from the inlined pages into the tail */
523 if (offs >= tail->iov_len)
525 else if (copy > tail->iov_len - offs)
526 copy = tail->iov_len - offs;
528 _copy_from_pages((char *)tail->iov_base + offs,
530 buf->page_base + pglen + offs - len,
532 /* Do we also need to copy data from the head into the tail ? */
534 offs = copy = len - pglen;
535 if (copy > tail->iov_len)
536 copy = tail->iov_len;
537 memcpy(tail->iov_base,
538 (char *)head->iov_base +
539 head->iov_len - offs,
543 /* Now handle pages */
546 _shift_data_right_pages(buf->pages,
547 buf->page_base + len,
553 _copy_to_pages(buf->pages, buf->page_base,
554 (char *)head->iov_base + head->iov_len - len,
557 head->iov_len -= len;
559 /* Have we truncated the message? */
560 if (buf->len > buf->buflen)
561 buf->len = buf->buflen;
567 * @len: bytes to remove from buf->pages
569 * Shrinks XDR buffer's page array buf->pages by
570 * 'len' bytes. The extra data is not lost, but is instead
571 * moved into the tail.
574 xdr_shrink_pagelen(struct xdr_buf *buf, size_t len)
579 unsigned int pglen = buf->page_len;
582 BUG_ON (len > pglen);
584 /* Shift the tail first */
585 if (tail->iov_len != 0) {
586 p = (char *)tail->iov_base + len;
587 if (tail->iov_len > len) {
588 copy = tail->iov_len - len;
589 memmove(p, tail->iov_base, copy);
592 /* Copy from the inlined pages into the tail */
594 if (copy > tail->iov_len)
595 copy = tail->iov_len;
596 _copy_from_pages((char *)tail->iov_base,
597 buf->pages, buf->page_base + pglen - len,
600 buf->page_len -= len;
602 /* Have we truncated the message? */
603 if (buf->len > buf->buflen)
604 buf->len = buf->buflen;
608 xdr_shift_buf(struct xdr_buf *buf, size_t len)
610 xdr_shrink_bufhead(buf, len);
614 * xdr_init_encode - Initialize a struct xdr_stream for sending data.
615 * @xdr: pointer to xdr_stream struct
616 * @buf: pointer to XDR buffer in which to encode data
617 * @p: current pointer inside XDR buffer
619 * Note: at the moment the RPC client only passes the length of our
620 * scratch buffer in the xdr_buf's header kvec. Previously this
621 * meant we needed to call xdr_adjust_iovec() after encoding the
622 * data. With the new scheme, the xdr_stream manages the details
623 * of the buffer length, and takes care of adjusting the kvec
626 void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, uint32_t *p)
628 struct kvec *iov = buf->head;
629 int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
631 BUG_ON(scratch_len < 0);
634 xdr->p = (uint32_t *)((char *)iov->iov_base + iov->iov_len);
635 xdr->end = (uint32_t *)((char *)iov->iov_base + scratch_len);
636 BUG_ON(iov->iov_len > scratch_len);
638 if (p != xdr->p && p != NULL) {
641 BUG_ON(p < xdr->p || p > xdr->end);
642 len = (char *)p - (char *)xdr->p;
648 EXPORT_SYMBOL(xdr_init_encode);
651 * xdr_reserve_space - Reserve buffer space for sending
652 * @xdr: pointer to xdr_stream
653 * @nbytes: number of bytes to reserve
655 * Checks that we have enough buffer space to encode 'nbytes' more
656 * bytes of data. If so, update the total xdr_buf length, and
657 * adjust the length of the current kvec.
659 uint32_t * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
661 uint32_t *p = xdr->p;
664 /* align nbytes on the next 32-bit boundary */
667 q = p + (nbytes >> 2);
668 if (unlikely(q > xdr->end || q < p))
671 xdr->iov->iov_len += nbytes;
672 xdr->buf->len += nbytes;
675 EXPORT_SYMBOL(xdr_reserve_space);
678 * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
679 * @xdr: pointer to xdr_stream
680 * @pages: list of pages
681 * @base: offset of first byte
682 * @len: length of data in bytes
685 void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
688 struct xdr_buf *buf = xdr->buf;
689 struct kvec *iov = buf->tail;
691 buf->page_base = base;
694 iov->iov_base = (char *)xdr->p;
699 unsigned int pad = 4 - (len & 3);
701 BUG_ON(xdr->p >= xdr->end);
702 iov->iov_base = (char *)xdr->p + (len & 3);
710 EXPORT_SYMBOL(xdr_write_pages);
713 * xdr_init_decode - Initialize an xdr_stream for decoding data.
714 * @xdr: pointer to xdr_stream struct
715 * @buf: pointer to XDR buffer from which to decode data
716 * @p: current pointer inside XDR buffer
718 void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, uint32_t *p)
720 struct kvec *iov = buf->head;
721 unsigned int len = iov->iov_len;
728 xdr->end = (uint32_t *)((char *)iov->iov_base + len);
730 EXPORT_SYMBOL(xdr_init_decode);
733 * xdr_inline_decode - Retrieve non-page XDR data to decode
734 * @xdr: pointer to xdr_stream struct
735 * @nbytes: number of bytes of data to decode
737 * Check if the input buffer is long enough to enable us to decode
738 * 'nbytes' more bytes of data starting at the current position.
739 * If so return the current pointer, then update the current
742 uint32_t * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
744 uint32_t *p = xdr->p;
745 uint32_t *q = p + XDR_QUADLEN(nbytes);
747 if (unlikely(q > xdr->end || q < p))
752 EXPORT_SYMBOL(xdr_inline_decode);
755 * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
756 * @xdr: pointer to xdr_stream struct
757 * @len: number of bytes of page data
759 * Moves data beyond the current pointer position from the XDR head[] buffer
760 * into the page list. Any data that lies beyond current position + "len"
761 * bytes is moved into the XDR tail[]. The current pointer is then
762 * repositioned at the beginning of the XDR tail.
764 void xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
766 struct xdr_buf *buf = xdr->buf;
772 /* Realign pages to current pointer position */
774 shift = iov->iov_len + (char *)iov->iov_base - (char *)xdr->p;
776 xdr_shrink_bufhead(buf, shift);
778 /* Truncate page data and move it into the tail */
779 if (buf->page_len > len)
780 xdr_shrink_pagelen(buf, buf->page_len - len);
781 padding = (XDR_QUADLEN(len) << 2) - len;
782 xdr->iov = iov = buf->tail;
783 /* Compute remaining message length. */
785 shift = buf->buflen - buf->len;
791 * Position current pointer at beginning of tail, and
792 * set remaining message length.
794 xdr->p = (uint32_t *)((char *)iov->iov_base + padding);
795 xdr->end = (uint32_t *)((char *)iov->iov_base + end);
797 EXPORT_SYMBOL(xdr_read_pages);
799 static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
802 xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
805 buf->tail[0] = empty_iov;
807 buf->buflen = buf->len = iov->iov_len;
810 /* Sets subiov to the intersection of iov with the buffer of length len
811 * starting base bytes after iov. Indicates empty intersection by setting
812 * length of subiov to zero. Decrements len by length of subiov, sets base
813 * to zero (or decrements it by length of iov if subiov is empty). */
815 iov_subsegment(struct kvec *iov, struct kvec *subiov, int *base, int *len)
817 if (*base > iov->iov_len) {
818 subiov->iov_base = NULL;
820 *base -= iov->iov_len;
822 subiov->iov_base = iov->iov_base + *base;
823 subiov->iov_len = min(*len, (int)iov->iov_len - *base);
826 *len -= subiov->iov_len;
829 /* Sets subbuf to the portion of buf of length len beginning base bytes
830 * from the start of buf. Returns -1 if base of length are out of bounds. */
832 xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
837 subbuf->buflen = subbuf->len = len;
838 iov_subsegment(buf->head, subbuf->head, &base, &len);
840 if (base < buf->page_len) {
841 i = (base + buf->page_base) >> PAGE_CACHE_SHIFT;
842 subbuf->pages = &buf->pages[i];
843 subbuf->page_base = (base + buf->page_base) & ~PAGE_CACHE_MASK;
844 subbuf->page_len = min((int)buf->page_len - base, len);
845 len -= subbuf->page_len;
848 base -= buf->page_len;
849 subbuf->page_len = 0;
852 iov_subsegment(buf->tail, subbuf->tail, &base, &len);
858 /* obj is assumed to point to allocated memory of size at least len: */
860 read_bytes_from_xdr_buf(struct xdr_buf *buf, int base, void *obj, int len)
862 struct xdr_buf subbuf;
866 status = xdr_buf_subsegment(buf, &subbuf, base, len);
869 this_len = min(len, (int)subbuf.head[0].iov_len);
870 memcpy(obj, subbuf.head[0].iov_base, this_len);
873 this_len = min(len, (int)subbuf.page_len);
875 _copy_from_pages(obj, subbuf.pages, subbuf.page_base, this_len);
878 this_len = min(len, (int)subbuf.tail[0].iov_len);
879 memcpy(obj, subbuf.tail[0].iov_base, this_len);
885 read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)
890 status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
897 /* If the netobj starting offset bytes from the start of xdr_buf is contained
898 * entirely in the head or the tail, set object to point to it; otherwise
899 * try to find space for it at the end of the tail, copy it there, and
900 * set obj to point to it. */
902 xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, int offset)
904 u32 tail_offset = buf->head[0].iov_len + buf->page_len;
907 if (read_u32_from_xdr_buf(buf, offset, &obj->len))
909 obj_end_offset = offset + 4 + obj->len;
911 if (obj_end_offset <= buf->head[0].iov_len) {
912 /* The obj is contained entirely in the head: */
913 obj->data = buf->head[0].iov_base + offset + 4;
914 } else if (offset + 4 >= tail_offset) {
915 if (obj_end_offset - tail_offset
916 > buf->tail[0].iov_len)
918 /* The obj is contained entirely in the tail: */
919 obj->data = buf->tail[0].iov_base
920 + offset - tail_offset + 4;
922 /* use end of tail as storage for obj:
923 * (We don't copy to the beginning because then we'd have
924 * to worry about doing a potentially overlapping copy.
925 * This assumes the object is at most half the length of the
927 if (obj->len > buf->tail[0].iov_len)
929 obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len -
931 if (read_bytes_from_xdr_buf(buf, offset + 4,
932 obj->data, obj->len))