xref: /openbmc/linux/net/sunrpc/xdr.c (revision 8e7a49e0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/net/sunrpc/xdr.c
4  *
5  * Generic XDR support.
6  *
7  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
8  */
9 
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>
21 
22 static void _copy_to_pages(struct page **, size_t, const char *, size_t);
23 
24 
25 /*
26  * XDR functions for basic NFS types
27  */
28 __be32 *
29 xdr_encode_netobj(__be32 *p, const struct xdr_netobj *obj)
30 {
31 	unsigned int	quadlen = XDR_QUADLEN(obj->len);
32 
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);
37 }
38 EXPORT_SYMBOL_GPL(xdr_encode_netobj);
39 
40 __be32 *
41 xdr_decode_netobj(__be32 *p, struct xdr_netobj *obj)
42 {
43 	unsigned int	len;
44 
45 	if ((len = be32_to_cpu(*p++)) > XDR_MAX_NETOBJ)
46 		return NULL;
47 	obj->len  = len;
48 	obj->data = (u8 *) p;
49 	return p + XDR_QUADLEN(len);
50 }
51 EXPORT_SYMBOL_GPL(xdr_decode_netobj);
52 
53 /**
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.
58  *
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.
63  *
64  * Returns the updated current XDR buffer position
65  *
66  */
67 __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int nbytes)
68 {
69 	if (likely(nbytes != 0)) {
70 		unsigned int quadlen = XDR_QUADLEN(nbytes);
71 		unsigned int padding = (quadlen << 2) - nbytes;
72 
73 		if (ptr != NULL)
74 			memcpy(p, ptr, nbytes);
75 		if (padding != 0)
76 			memset((char *)p + nbytes, 0, padding);
77 		p += quadlen;
78 	}
79 	return p;
80 }
81 EXPORT_SYMBOL_GPL(xdr_encode_opaque_fixed);
82 
83 /**
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.
88  *
89  * Returns the updated current XDR buffer position
90  */
91 __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int nbytes)
92 {
93 	*p++ = cpu_to_be32(nbytes);
94 	return xdr_encode_opaque_fixed(p, ptr, nbytes);
95 }
96 EXPORT_SYMBOL_GPL(xdr_encode_opaque);
97 
98 __be32 *
99 xdr_encode_string(__be32 *p, const char *string)
100 {
101 	return xdr_encode_array(p, string, strlen(string));
102 }
103 EXPORT_SYMBOL_GPL(xdr_encode_string);
104 
105 __be32 *
106 xdr_decode_string_inplace(__be32 *p, char **sp,
107 			  unsigned int *lenp, unsigned int maxlen)
108 {
109 	u32 len;
110 
111 	len = be32_to_cpu(*p++);
112 	if (len > maxlen)
113 		return NULL;
114 	*lenp = len;
115 	*sp = (char *) p;
116 	return p + XDR_QUADLEN(len);
117 }
118 EXPORT_SYMBOL_GPL(xdr_decode_string_inplace);
119 
120 /**
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
124  *
125  */
126 void xdr_terminate_string(const struct xdr_buf *buf, const u32 len)
127 {
128 	char *kaddr;
129 
130 	kaddr = kmap_atomic(buf->pages[0]);
131 	kaddr[buf->page_base + len] = '\0';
132 	kunmap_atomic(kaddr);
133 }
134 EXPORT_SYMBOL_GPL(xdr_terminate_string);
135 
136 size_t xdr_buf_pagecount(const struct xdr_buf *buf)
137 {
138 	if (!buf->page_len)
139 		return 0;
140 	return (buf->page_base + buf->page_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
141 }
142 
143 int
144 xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp)
145 {
146 	size_t i, n = xdr_buf_pagecount(buf);
147 
148 	if (n != 0 && buf->bvec == NULL) {
149 		buf->bvec = kmalloc_array(n, sizeof(buf->bvec[0]), gfp);
150 		if (!buf->bvec)
151 			return -ENOMEM;
152 		for (i = 0; i < n; i++) {
153 			bvec_set_page(&buf->bvec[i], buf->pages[i], PAGE_SIZE,
154 				      0);
155 		}
156 	}
157 	return 0;
158 }
159 
160 void
161 xdr_free_bvec(struct xdr_buf *buf)
162 {
163 	kfree(buf->bvec);
164 	buf->bvec = NULL;
165 }
166 
167 /**
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
174  *
175  */
176 void
177 xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
178 		 struct page **pages, unsigned int base, unsigned int len)
179 {
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;
184 
185 	head->iov_len  = offset;
186 
187 	xdr->pages = pages;
188 	xdr->page_base = base;
189 	xdr->page_len = len;
190 
191 	tail->iov_base = buf + offset;
192 	tail->iov_len = buflen - offset;
193 	xdr->buflen += len;
194 }
195 EXPORT_SYMBOL_GPL(xdr_inline_pages);
196 
197 /*
198  * Helper routines for doing 'memmove' like operations on a struct xdr_buf
199  */
200 
201 /**
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
207  *
208  * Note: the addresses pgto_base and pgfrom_base are both calculated in
209  *       the same way:
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.
214  */
215 static void
216 _shift_data_left_pages(struct page **pages, size_t pgto_base,
217 			size_t pgfrom_base, size_t len)
218 {
219 	struct page **pgfrom, **pgto;
220 	char *vfrom, *vto;
221 	size_t copy;
222 
223 	BUG_ON(pgfrom_base <= pgto_base);
224 
225 	if (!len)
226 		return;
227 
228 	pgto = pages + (pgto_base >> PAGE_SHIFT);
229 	pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
230 
231 	pgto_base &= ~PAGE_MASK;
232 	pgfrom_base &= ~PAGE_MASK;
233 
234 	do {
235 		if (pgto_base >= PAGE_SIZE) {
236 			pgto_base = 0;
237 			pgto++;
238 		}
239 		if (pgfrom_base >= PAGE_SIZE){
240 			pgfrom_base = 0;
241 			pgfrom++;
242 		}
243 
244 		copy = len;
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;
249 
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);
255 		} else
256 			memmove(vto + pgto_base, vto + pgfrom_base, copy);
257 		flush_dcache_page(*pgto);
258 		kunmap_atomic(vto);
259 
260 		pgto_base += copy;
261 		pgfrom_base += copy;
262 
263 	} while ((len -= copy) != 0);
264 }
265 
266 /**
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
272  *
273  * Note: the addresses pgto_base and pgfrom_base are both calculated in
274  *       the same way:
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.
279  */
280 static void
281 _shift_data_right_pages(struct page **pages, size_t pgto_base,
282 		size_t pgfrom_base, size_t len)
283 {
284 	struct page **pgfrom, **pgto;
285 	char *vfrom, *vto;
286 	size_t copy;
287 
288 	BUG_ON(pgto_base <= pgfrom_base);
289 
290 	if (!len)
291 		return;
292 
293 	pgto_base += len;
294 	pgfrom_base += len;
295 
296 	pgto = pages + (pgto_base >> PAGE_SHIFT);
297 	pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
298 
299 	pgto_base &= ~PAGE_MASK;
300 	pgfrom_base &= ~PAGE_MASK;
301 
302 	do {
303 		/* Are any pointers crossing a page boundary? */
304 		if (pgto_base == 0) {
305 			pgto_base = PAGE_SIZE;
306 			pgto--;
307 		}
308 		if (pgfrom_base == 0) {
309 			pgfrom_base = PAGE_SIZE;
310 			pgfrom--;
311 		}
312 
313 		copy = len;
314 		if (copy > pgto_base)
315 			copy = pgto_base;
316 		if (copy > pgfrom_base)
317 			copy = pgfrom_base;
318 		pgto_base -= copy;
319 		pgfrom_base -= copy;
320 
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);
326 		} else
327 			memmove(vto + pgto_base, vto + pgfrom_base, copy);
328 		flush_dcache_page(*pgto);
329 		kunmap_atomic(vto);
330 
331 	} while ((len -= copy) != 0);
332 }
333 
334 /**
335  * _copy_to_pages
336  * @pages: array of pages
337  * @pgbase: page vector address of destination
338  * @p: pointer to source data
339  * @len: length
340  *
341  * Copies data from an arbitrary memory location into an array of pages
342  * The copy is assumed to be non-overlapping.
343  */
344 static void
345 _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
346 {
347 	struct page **pgto;
348 	char *vto;
349 	size_t copy;
350 
351 	if (!len)
352 		return;
353 
354 	pgto = pages + (pgbase >> PAGE_SHIFT);
355 	pgbase &= ~PAGE_MASK;
356 
357 	for (;;) {
358 		copy = PAGE_SIZE - pgbase;
359 		if (copy > len)
360 			copy = len;
361 
362 		vto = kmap_atomic(*pgto);
363 		memcpy(vto + pgbase, p, copy);
364 		kunmap_atomic(vto);
365 
366 		len -= copy;
367 		if (len == 0)
368 			break;
369 
370 		pgbase += copy;
371 		if (pgbase == PAGE_SIZE) {
372 			flush_dcache_page(*pgto);
373 			pgbase = 0;
374 			pgto++;
375 		}
376 		p += copy;
377 	}
378 	flush_dcache_page(*pgto);
379 }
380 
381 /**
382  * _copy_from_pages
383  * @p: pointer to destination
384  * @pages: array of pages
385  * @pgbase: offset of source data
386  * @len: length
387  *
388  * Copies data into an arbitrary memory location from an array of pages
389  * The copy is assumed to be non-overlapping.
390  */
391 void
392 _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
393 {
394 	struct page **pgfrom;
395 	char *vfrom;
396 	size_t copy;
397 
398 	if (!len)
399 		return;
400 
401 	pgfrom = pages + (pgbase >> PAGE_SHIFT);
402 	pgbase &= ~PAGE_MASK;
403 
404 	do {
405 		copy = PAGE_SIZE - pgbase;
406 		if (copy > len)
407 			copy = len;
408 
409 		vfrom = kmap_atomic(*pgfrom);
410 		memcpy(p, vfrom + pgbase, copy);
411 		kunmap_atomic(vfrom);
412 
413 		pgbase += copy;
414 		if (pgbase == PAGE_SIZE) {
415 			pgbase = 0;
416 			pgfrom++;
417 		}
418 		p += copy;
419 
420 	} while ((len -= copy) != 0);
421 }
422 EXPORT_SYMBOL_GPL(_copy_from_pages);
423 
424 static void xdr_buf_iov_zero(const struct kvec *iov, unsigned int base,
425 			     unsigned int len)
426 {
427 	if (base >= iov->iov_len)
428 		return;
429 	if (len > iov->iov_len - base)
430 		len = iov->iov_len - base;
431 	memset(iov->iov_base + base, 0, len);
432 }
433 
434 /**
435  * xdr_buf_pages_zero
436  * @buf: xdr_buf
437  * @pgbase: beginning offset
438  * @len: length
439  */
440 static void xdr_buf_pages_zero(const struct xdr_buf *buf, unsigned int pgbase,
441 			       unsigned int len)
442 {
443 	struct page **pages = buf->pages;
444 	struct page **page;
445 	char *vpage;
446 	unsigned int zero;
447 
448 	if (!len)
449 		return;
450 	if (pgbase >= buf->page_len) {
451 		xdr_buf_iov_zero(buf->tail, pgbase - buf->page_len, len);
452 		return;
453 	}
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;
457 	}
458 
459 	pgbase += buf->page_base;
460 
461 	page = pages + (pgbase >> PAGE_SHIFT);
462 	pgbase &= ~PAGE_MASK;
463 
464 	do {
465 		zero = PAGE_SIZE - pgbase;
466 		if (zero > len)
467 			zero = len;
468 
469 		vpage = kmap_atomic(*page);
470 		memset(vpage + pgbase, 0, zero);
471 		kunmap_atomic(vpage);
472 
473 		flush_dcache_page(*page);
474 		pgbase = 0;
475 		page++;
476 
477 	} while ((len -= zero) != 0);
478 }
479 
480 static unsigned int xdr_buf_pages_fill_sparse(const struct xdr_buf *buf,
481 					      unsigned int buflen, gfp_t gfp)
482 {
483 	unsigned int i, npages, pagelen;
484 
485 	if (!(buf->flags & XDRBUF_SPARSE_PAGES))
486 		return buflen;
487 	if (buflen <= buf->head->iov_len)
488 		return buflen;
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++) {
494 		if (!buf->pages[i])
495 			continue;
496 		buf->pages[i] = alloc_page(gfp);
497 		if (likely(buf->pages[i]))
498 			continue;
499 		buflen -= pagelen;
500 		pagelen = i << PAGE_SHIFT;
501 		if (pagelen > buf->page_base)
502 			buflen += pagelen - buf->page_base;
503 		break;
504 	}
505 	return buflen;
506 }
507 
508 static void xdr_buf_try_expand(struct xdr_buf *buf, unsigned int len)
509 {
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;
514 
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,
518 						   GFP_KERNEL);
519 		free_space = newlen - buf->len;
520 		buf->len = newlen;
521 		len -= free_space;
522 		if (!len)
523 			return;
524 	}
525 
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;
531 	}
532 }
533 
534 static void xdr_buf_tail_copy_right(const struct xdr_buf *buf,
535 				    unsigned int base, unsigned int len,
536 				    unsigned int shift)
537 {
538 	const struct kvec *tail = buf->tail;
539 	unsigned int to = base + shift;
540 
541 	if (to >= tail->iov_len)
542 		return;
543 	if (len + to > tail->iov_len)
544 		len = tail->iov_len - to;
545 	memmove(tail->iov_base + to, tail->iov_base + base, len);
546 }
547 
548 static void xdr_buf_pages_copy_right(const struct xdr_buf *buf,
549 				     unsigned int base, unsigned int len,
550 				     unsigned int shift)
551 {
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;
556 
557 	if (base >= buf->page_len)
558 		return;
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)
564 			talen = len;
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;
569 		talen = len - pglen;
570 		if (talen > tail->iov_len)
571 			talen = tail->iov_len;
572 	} else
573 		pglen = len;
574 
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);
579 }
580 
581 static void xdr_buf_head_copy_right(const struct xdr_buf *buf,
582 				    unsigned int base, unsigned int len,
583 				    unsigned int shift)
584 {
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;
590 
591 	if (base >= head->iov_len)
592 		return;
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;
597 		talen = len;
598 	} else if (to >= head->iov_len) {
599 		pgto = to - head->iov_len;
600 		pglen = len;
601 		if (pgto + pglen > buf->page_len) {
602 			talen = pgto + pglen - buf->page_len;
603 			pglen -= talen;
604 		}
605 	} else {
606 		pglen = len - to;
607 		if (pglen > buf->page_len) {
608 			talen = pglen - buf->page_len;
609 			pglen = buf->page_len;
610 		}
611 	}
612 
613 	len -= talen;
614 	base += 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);
618 
619 	len -= pglen;
620 	base -= pglen;
621 	_copy_to_pages(buf->pages, buf->page_base + pgto, head->iov_base + base,
622 		       pglen);
623 
624 	base -= len;
625 	memmove(head->iov_base + to, head->iov_base + base, len);
626 }
627 
628 static void xdr_buf_tail_shift_right(const struct xdr_buf *buf,
629 				     unsigned int base, unsigned int len,
630 				     unsigned int shift)
631 {
632 	const struct kvec *tail = buf->tail;
633 
634 	if (base >= tail->iov_len || !shift || !len)
635 		return;
636 	xdr_buf_tail_copy_right(buf, base, len, shift);
637 }
638 
639 static void xdr_buf_pages_shift_right(const struct xdr_buf *buf,
640 				      unsigned int base, unsigned int len,
641 				      unsigned int shift)
642 {
643 	if (!shift || !len)
644 		return;
645 	if (base >= buf->page_len) {
646 		xdr_buf_tail_shift_right(buf, base - buf->page_len, len, shift);
647 		return;
648 	}
649 	if (base + len > buf->page_len)
650 		xdr_buf_tail_shift_right(buf, 0, base + len - buf->page_len,
651 					 shift);
652 	xdr_buf_pages_copy_right(buf, base, len, shift);
653 }
654 
655 static void xdr_buf_head_shift_right(const struct xdr_buf *buf,
656 				     unsigned int base, unsigned int len,
657 				     unsigned int shift)
658 {
659 	const struct kvec *head = buf->head;
660 
661 	if (!shift)
662 		return;
663 	if (base >= head->iov_len) {
664 		xdr_buf_pages_shift_right(buf, head->iov_len - base, len,
665 					  shift);
666 		return;
667 	}
668 	if (base + len > head->iov_len)
669 		xdr_buf_pages_shift_right(buf, 0, base + len - head->iov_len,
670 					  shift);
671 	xdr_buf_head_copy_right(buf, base, len, shift);
672 }
673 
674 static void xdr_buf_tail_copy_left(const struct xdr_buf *buf, unsigned int base,
675 				   unsigned int len, unsigned int shift)
676 {
677 	const struct kvec *tail = buf->tail;
678 
679 	if (base >= tail->iov_len)
680 		return;
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;
686 		unsigned int hdto =
687 			head->iov_len + buf->page_len + base - shift;
688 		unsigned int hdlen = len;
689 
690 		if (WARN_ONCE(shift > head->iov_len + buf->page_len + base,
691 			      "SUNRPC: Misaligned data.\n"))
692 			return;
693 		if (hdto + hdlen > head->iov_len)
694 			hdlen = head->iov_len - hdto;
695 		memcpy(head->iov_base + hdto, tail->iov_base + base, hdlen);
696 		base += hdlen;
697 		len -= hdlen;
698 		if (!len)
699 			return;
700 	}
701 	/* Shift data into pages */
702 	if (shift > base) {
703 		unsigned int pgto = buf->page_len + base - shift;
704 		unsigned int pglen = len;
705 
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);
710 		base += pglen;
711 		len -= pglen;
712 		if (!len)
713 			return;
714 	}
715 	memmove(tail->iov_base + base - shift, tail->iov_base + base, len);
716 }
717 
718 static void xdr_buf_pages_copy_left(const struct xdr_buf *buf,
719 				    unsigned int base, unsigned int len,
720 				    unsigned int shift)
721 {
722 	unsigned int pgto;
723 
724 	if (base >= buf->page_len)
725 		return;
726 	if (len > buf->page_len - base)
727 		len = buf->page_len - base;
728 	/* Shift data into head */
729 	if (shift > base) {
730 		const struct kvec *head = buf->head;
731 		unsigned int hdto = head->iov_len + base - shift;
732 		unsigned int hdlen = len;
733 
734 		if (WARN_ONCE(shift > head->iov_len + base,
735 			      "SUNRPC: Misaligned data.\n"))
736 			return;
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);
741 		base += hdlen;
742 		len -= hdlen;
743 		if (!len)
744 			return;
745 	}
746 	pgto = base - shift;
747 	_shift_data_left_pages(buf->pages, buf->page_base + pgto,
748 			       buf->page_base + base, len);
749 }
750 
751 static void xdr_buf_tail_shift_left(const struct xdr_buf *buf,
752 				    unsigned int base, unsigned int len,
753 				    unsigned int shift)
754 {
755 	if (!shift || !len)
756 		return;
757 	xdr_buf_tail_copy_left(buf, base, len, shift);
758 }
759 
760 static void xdr_buf_pages_shift_left(const struct xdr_buf *buf,
761 				     unsigned int base, unsigned int len,
762 				     unsigned int shift)
763 {
764 	if (!shift || !len)
765 		return;
766 	if (base >= buf->page_len) {
767 		xdr_buf_tail_shift_left(buf, base - buf->page_len, len, shift);
768 		return;
769 	}
770 	xdr_buf_pages_copy_left(buf, base, len, shift);
771 	len += base;
772 	if (len <= buf->page_len)
773 		return;
774 	xdr_buf_tail_copy_left(buf, 0, len - buf->page_len, shift);
775 }
776 
777 static void xdr_buf_head_shift_left(const struct xdr_buf *buf,
778 				    unsigned int base, unsigned int len,
779 				    unsigned int shift)
780 {
781 	const struct kvec *head = buf->head;
782 	unsigned int bytes;
783 
784 	if (!shift || !len)
785 		return;
786 
787 	if (shift > base) {
788 		bytes = (shift - base);
789 		if (bytes >= len)
790 			return;
791 		base += bytes;
792 		len -= bytes;
793 	}
794 
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);
799 		base += bytes;
800 		len -= bytes;
801 	}
802 	xdr_buf_pages_shift_left(buf, base - head->iov_len, len, shift);
803 }
804 
805 /**
806  * xdr_shrink_bufhead
807  * @buf: xdr_buf
808  * @len: new length of buf->head[0]
809  *
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.
813  */
814 static unsigned int xdr_shrink_bufhead(struct xdr_buf *buf, unsigned int len)
815 {
816 	struct kvec *head = buf->head;
817 	unsigned int shift, buflen = max(buf->len, len);
818 
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;
823 	}
824 	if (len >= head->iov_len)
825 		return 0;
826 	shift = head->iov_len - len;
827 	xdr_buf_try_expand(buf, shift);
828 	xdr_buf_head_shift_right(buf, len, buflen - len, shift);
829 	head->iov_len = len;
830 	buf->buflen -= shift;
831 	buf->len -= shift;
832 	return shift;
833 }
834 
835 /**
836  * xdr_shrink_pagelen - shrinks buf->pages to @len bytes
837  * @buf: xdr_buf
838  * @len: new page buffer length
839  *
840  * The extra data is not lost, but is instead moved into buf->tail.
841  * Returns the actual number of bytes moved.
842  */
843 static unsigned int xdr_shrink_pagelen(struct xdr_buf *buf, unsigned int len)
844 {
845 	unsigned int shift, buflen = buf->len - buf->head->iov_len;
846 
847 	WARN_ON_ONCE(len > buf->page_len);
848 	if (buf->head->iov_len >= buf->len || len > buflen)
849 		buflen = len;
850 	if (buf->page_len > buflen) {
851 		buf->buflen -= buf->page_len - buflen;
852 		buf->page_len = buflen;
853 	}
854 	if (len >= buf->page_len)
855 		return 0;
856 	shift = buf->page_len - len;
857 	xdr_buf_try_expand(buf, shift);
858 	xdr_buf_pages_shift_right(buf, len, buflen - len, shift);
859 	buf->page_len = len;
860 	buf->len -= shift;
861 	buf->buflen -= shift;
862 	return shift;
863 }
864 
865 /**
866  * xdr_stream_pos - Return the current offset from the start of the xdr_stream
867  * @xdr: pointer to struct xdr_stream
868  */
869 unsigned int xdr_stream_pos(const struct xdr_stream *xdr)
870 {
871 	return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2;
872 }
873 EXPORT_SYMBOL_GPL(xdr_stream_pos);
874 
875 static void xdr_stream_set_pos(struct xdr_stream *xdr, unsigned int pos)
876 {
877 	unsigned int blen = xdr->buf->len;
878 
879 	xdr->nwords = blen > pos ? XDR_QUADLEN(blen) - XDR_QUADLEN(pos) : 0;
880 }
881 
882 static void xdr_stream_page_set_pos(struct xdr_stream *xdr, unsigned int pos)
883 {
884 	xdr_stream_set_pos(xdr, pos + xdr->buf->head[0].iov_len);
885 }
886 
887 /**
888  * xdr_page_pos - Return the current offset from the start of the xdr pages
889  * @xdr: pointer to struct xdr_stream
890  */
891 unsigned int xdr_page_pos(const struct xdr_stream *xdr)
892 {
893 	unsigned int pos = xdr_stream_pos(xdr);
894 
895 	WARN_ON(pos < xdr->buf->head[0].iov_len);
896 	return pos - xdr->buf->head[0].iov_len;
897 }
898 EXPORT_SYMBOL_GPL(xdr_page_pos);
899 
900 /**
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
906  *
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
912  *	 length for us.
913  */
914 void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
915 		     struct rpc_rqst *rqst)
916 {
917 	struct kvec *iov = buf->head;
918 	int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
919 
920 	xdr_reset_scratch_buffer(xdr);
921 	BUG_ON(scratch_len < 0);
922 	xdr->buf = buf;
923 	xdr->iov = iov;
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);
927 
928 	if (p != xdr->p && p != NULL) {
929 		size_t len;
930 
931 		BUG_ON(p < xdr->p || p > xdr->end);
932 		len = (char *)p - (char *)xdr->p;
933 		xdr->p = p;
934 		buf->len += len;
935 		iov->iov_len += len;
936 	}
937 	xdr->rqst = rqst;
938 }
939 EXPORT_SYMBOL_GPL(xdr_init_encode);
940 
941 /**
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
947  *
948  */
949 void xdr_init_encode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
950 			   struct page **pages, struct rpc_rqst *rqst)
951 {
952 	xdr_reset_scratch_buffer(xdr);
953 
954 	xdr->buf = buf;
955 	xdr->page_ptr = pages;
956 	xdr->iov = NULL;
957 	xdr->p = page_address(*pages);
958 	xdr->end = (void *)xdr->p + min_t(u32, buf->buflen, PAGE_SIZE);
959 	xdr->rqst = rqst;
960 }
961 EXPORT_SYMBOL_GPL(xdr_init_encode_pages);
962 
963 /**
964  * __xdr_commit_encode - Ensure all data is written to buffer
965  * @xdr: pointer to xdr_stream
966  *
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.
970  *
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.
975  */
976 void __xdr_commit_encode(struct xdr_stream *xdr)
977 {
978 	size_t shift = xdr->scratch.iov_len;
979 	void *page;
980 
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);
985 }
986 EXPORT_SYMBOL_GPL(__xdr_commit_encode);
987 
988 /*
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.
992  */
993 static noinline __be32 *xdr_get_next_encode_buffer(struct xdr_stream *xdr,
994 						   size_t nbytes)
995 {
996 	int space_left;
997 	int frag1bytes, frag2bytes;
998 	void *p;
999 
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;
1006 	if (xdr->iov)
1007 		xdr->iov->iov_len += frag1bytes;
1008 	else
1009 		xdr->buf->page_len += frag1bytes;
1010 	xdr->page_ptr++;
1011 	xdr->iov = NULL;
1012 
1013 	/*
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:
1019 	 */
1020 	xdr_set_scratch_buffer(xdr, xdr->p, frag1bytes);
1021 
1022 	/*
1023 	 * xdr->p is where the next encode will start after
1024 	 * xdr_commit_encode() has shifted this one back:
1025 	 */
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;
1031 	else
1032 		xdr->end = p + space_left - frag1bytes;
1033 
1034 	xdr->buf->page_len += frag2bytes;
1035 	xdr->buf->len += nbytes;
1036 	return p;
1037 out_overflow:
1038 	trace_rpc_xdr_overflow(xdr, nbytes);
1039 	return NULL;
1040 }
1041 
1042 /**
1043  * xdr_reserve_space - Reserve buffer space for sending
1044  * @xdr: pointer to xdr_stream
1045  * @nbytes: number of bytes to reserve
1046  *
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.
1050  */
1051 __be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
1052 {
1053 	__be32 *p = xdr->p;
1054 	__be32 *q;
1055 
1056 	xdr_commit_encode(xdr);
1057 	/* align nbytes on the next 32-bit boundary */
1058 	nbytes += 3;
1059 	nbytes &= ~3;
1060 	q = p + (nbytes >> 2);
1061 	if (unlikely(q > xdr->end || q < p))
1062 		return xdr_get_next_encode_buffer(xdr, nbytes);
1063 	xdr->p = q;
1064 	if (xdr->iov)
1065 		xdr->iov->iov_len += nbytes;
1066 	else
1067 		xdr->buf->page_len += nbytes;
1068 	xdr->buf->len += nbytes;
1069 	return p;
1070 }
1071 EXPORT_SYMBOL_GPL(xdr_reserve_space);
1072 
1073 
1074 /**
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
1079  *
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.
1084  */
1085 int xdr_reserve_space_vec(struct xdr_stream *xdr, struct kvec *vec, size_t nbytes)
1086 {
1087 	int thislen;
1088 	int v = 0;
1089 	__be32 *p;
1090 
1091 	/*
1092 	 * svcrdma requires every READ payload to start somewhere
1093 	 * in xdr->pages.
1094 	 */
1095 	if (xdr->iov == xdr->buf->head) {
1096 		xdr->iov = NULL;
1097 		xdr->end = xdr->p;
1098 	}
1099 
1100 	while (nbytes) {
1101 		thislen = xdr->buf->page_len % PAGE_SIZE;
1102 		thislen = min_t(size_t, nbytes, PAGE_SIZE - thislen);
1103 
1104 		p = xdr_reserve_space(xdr, thislen);
1105 		if (!p)
1106 			return -EIO;
1107 
1108 		vec[v].iov_base = p;
1109 		vec[v].iov_len = thislen;
1110 		v++;
1111 		nbytes -= thislen;
1112 	}
1113 
1114 	return v;
1115 }
1116 EXPORT_SYMBOL_GPL(xdr_reserve_space_vec);
1117 
1118 /**
1119  * xdr_truncate_encode - truncate an encode buffer
1120  * @xdr: pointer to xdr_stream
1121  * @len: new length of buffer
1122  *
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.
1126  *
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.
1131  *
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.
1135  *
1136  */
1137 void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
1138 {
1139 	struct xdr_buf *buf = xdr->buf;
1140 	struct kvec *head = buf->head;
1141 	struct kvec *tail = buf->tail;
1142 	int fraglen;
1143 	int new;
1144 
1145 	if (len > buf->len) {
1146 		WARN_ON_ONCE(1);
1147 		return;
1148 	}
1149 	xdr_commit_encode(xdr);
1150 
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);
1158 		return;
1159 	}
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;
1164 
1165 	new = buf->page_base + buf->page_len;
1166 
1167 	xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
1168 
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);
1174 		return;
1175 	}
1176 	if (fraglen)
1177 		xdr->end = head->iov_base + head->iov_len;
1178 	/* (otherwise assume xdr->end is already set) */
1179 	xdr->page_ptr--;
1180 	head->iov_len = len;
1181 	buf->len = len;
1182 	xdr->p = head->iov_base + head->iov_len;
1183 	xdr->iov = buf->head;
1184 }
1185 EXPORT_SYMBOL(xdr_truncate_encode);
1186 
1187 /**
1188  * xdr_truncate_decode - Truncate a decoding stream
1189  * @xdr: pointer to struct xdr_stream
1190  * @len: Number of bytes to remove
1191  *
1192  */
1193 void xdr_truncate_decode(struct xdr_stream *xdr, size_t len)
1194 {
1195 	unsigned int nbytes = xdr_align_size(len);
1196 
1197 	xdr->buf->len -= nbytes;
1198 	xdr->nwords -= XDR_QUADLEN(nbytes);
1199 }
1200 EXPORT_SYMBOL_GPL(xdr_truncate_decode);
1201 
1202 /**
1203  * xdr_restrict_buflen - decrease available buffer space
1204  * @xdr: pointer to xdr_stream
1205  * @newbuflen: new maximum number of bytes available
1206  *
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
1212  * of the buffer.
1213  */
1214 int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
1215 {
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;
1219 
1220 	if (newbuflen < 0 || newbuflen < buf->len)
1221 		return -1;
1222 	if (newbuflen > buf->buflen)
1223 		return 0;
1224 	if (newbuflen < end_offset)
1225 		xdr->end = (void *)xdr->end + newbuflen - end_offset;
1226 	buf->buflen = newbuflen;
1227 	return 0;
1228 }
1229 EXPORT_SYMBOL(xdr_restrict_buflen);
1230 
1231 /**
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
1237  *
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.
1241  */
1242 void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
1243 		 unsigned int len)
1244 {
1245 	struct xdr_buf *buf = xdr->buf;
1246 	struct kvec *tail = buf->tail;
1247 
1248 	buf->pages = pages;
1249 	buf->page_base = base;
1250 	buf->page_len = len;
1251 
1252 	tail->iov_base = xdr->p;
1253 	tail->iov_len = 0;
1254 	xdr->iov = tail;
1255 
1256 	if (len & 3) {
1257 		unsigned int pad = 4 - (len & 3);
1258 
1259 		BUG_ON(xdr->p >= xdr->end);
1260 		tail->iov_base = (char *)xdr->p + (len & 3);
1261 		tail->iov_len += pad;
1262 		len += pad;
1263 		*xdr->p++ = 0;
1264 	}
1265 	buf->buflen += len;
1266 	buf->len += len;
1267 }
1268 EXPORT_SYMBOL_GPL(xdr_write_pages);
1269 
1270 static unsigned int xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
1271 				unsigned int base, unsigned int len)
1272 {
1273 	if (len > iov->iov_len)
1274 		len = iov->iov_len;
1275 	if (unlikely(base > len))
1276 		base = len;
1277 	xdr->p = (__be32*)(iov->iov_base + base);
1278 	xdr->end = (__be32*)(iov->iov_base + len);
1279 	xdr->iov = iov;
1280 	xdr->page_ptr = NULL;
1281 	return len - base;
1282 }
1283 
1284 static unsigned int xdr_set_tail_base(struct xdr_stream *xdr,
1285 				      unsigned int base, unsigned int len)
1286 {
1287 	struct xdr_buf *buf = xdr->buf;
1288 
1289 	xdr_stream_set_pos(xdr, base + buf->page_len + buf->head->iov_len);
1290 	return xdr_set_iov(xdr, buf->tail, base, len);
1291 }
1292 
1293 static unsigned int xdr_set_page_base(struct xdr_stream *xdr,
1294 				      unsigned int base, unsigned int len)
1295 {
1296 	unsigned int pgnr;
1297 	unsigned int maxlen;
1298 	unsigned int pgoff;
1299 	unsigned int pgend;
1300 	void *kaddr;
1301 
1302 	maxlen = xdr->buf->page_len;
1303 	if (base >= maxlen)
1304 		return 0;
1305 	else
1306 		maxlen -= base;
1307 	if (len > maxlen)
1308 		len = maxlen;
1309 
1310 	xdr_stream_page_set_pos(xdr, base);
1311 	base += xdr->buf->page_base;
1312 
1313 	pgnr = base >> PAGE_SHIFT;
1314 	xdr->page_ptr = &xdr->buf->pages[pgnr];
1315 	kaddr = page_address(*xdr->page_ptr);
1316 
1317 	pgoff = base & ~PAGE_MASK;
1318 	xdr->p = (__be32*)(kaddr + pgoff);
1319 
1320 	pgend = pgoff + len;
1321 	if (pgend > PAGE_SIZE)
1322 		pgend = PAGE_SIZE;
1323 	xdr->end = (__be32*)(kaddr + pgend);
1324 	xdr->iov = NULL;
1325 	return len;
1326 }
1327 
1328 static void xdr_set_page(struct xdr_stream *xdr, unsigned int base,
1329 			 unsigned int len)
1330 {
1331 	if (xdr_set_page_base(xdr, base, len) == 0) {
1332 		base -= xdr->buf->page_len;
1333 		xdr_set_tail_base(xdr, base, len);
1334 	}
1335 }
1336 
1337 static void xdr_set_next_page(struct xdr_stream *xdr)
1338 {
1339 	unsigned int newbase;
1340 
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));
1345 	else
1346 		xdr_set_tail_base(xdr, 0, xdr_stream_remaining(xdr));
1347 }
1348 
1349 static bool xdr_set_next_buffer(struct xdr_stream *xdr)
1350 {
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;
1356 }
1357 
1358 /**
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
1364  */
1365 void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
1366 		     struct rpc_rqst *rqst)
1367 {
1368 	xdr->buf = buf;
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;
1376 		xdr->p = p;
1377 	}
1378 	xdr->rqst = rqst;
1379 }
1380 EXPORT_SYMBOL_GPL(xdr_init_decode);
1381 
1382 /**
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
1388  */
1389 void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
1390 			   struct page **pages, unsigned int len)
1391 {
1392 	memset(buf, 0, sizeof(*buf));
1393 	buf->pages =  pages;
1394 	buf->page_len =  len;
1395 	buf->buflen =  len;
1396 	buf->len = len;
1397 	xdr_init_decode(xdr, buf, NULL, NULL);
1398 }
1399 EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
1400 
1401 static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1402 {
1403 	unsigned int nwords = XDR_QUADLEN(nbytes);
1404 	__be32 *p = xdr->p;
1405 	__be32 *q = p + nwords;
1406 
1407 	if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
1408 		return NULL;
1409 	xdr->p = q;
1410 	xdr->nwords -= nwords;
1411 	return p;
1412 }
1413 
1414 static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
1415 {
1416 	__be32 *p;
1417 	char *cpdest = xdr->scratch.iov_base;
1418 	size_t cplen = (char *)xdr->end - (char *)xdr->p;
1419 
1420 	if (nbytes > xdr->scratch.iov_len)
1421 		goto out_overflow;
1422 	p = __xdr_inline_decode(xdr, cplen);
1423 	if (p == NULL)
1424 		return NULL;
1425 	memcpy(cpdest, p, cplen);
1426 	if (!xdr_set_next_buffer(xdr))
1427 		goto out_overflow;
1428 	cpdest += cplen;
1429 	nbytes -= cplen;
1430 	p = __xdr_inline_decode(xdr, nbytes);
1431 	if (p == NULL)
1432 		return NULL;
1433 	memcpy(cpdest, p, nbytes);
1434 	return xdr->scratch.iov_base;
1435 out_overflow:
1436 	trace_rpc_xdr_overflow(xdr, nbytes);
1437 	return NULL;
1438 }
1439 
1440 /**
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
1444  *
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
1448  * pointer position.
1449  */
1450 __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1451 {
1452 	__be32 *p;
1453 
1454 	if (unlikely(nbytes == 0))
1455 		return xdr->p;
1456 	if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1457 		goto out_overflow;
1458 	p = __xdr_inline_decode(xdr, nbytes);
1459 	if (p != NULL)
1460 		return p;
1461 	return xdr_copy_to_scratch(xdr, nbytes);
1462 out_overflow:
1463 	trace_rpc_xdr_overflow(xdr, nbytes);
1464 	return NULL;
1465 }
1466 EXPORT_SYMBOL_GPL(xdr_inline_decode);
1467 
1468 static void xdr_realign_pages(struct xdr_stream *xdr)
1469 {
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;
1474 
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);
1480 	}
1481 }
1482 
1483 static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
1484 {
1485 	struct xdr_buf *buf = xdr->buf;
1486 	unsigned int nwords = XDR_QUADLEN(len);
1487 	unsigned int copied;
1488 
1489 	if (xdr->nwords == 0)
1490 		return 0;
1491 
1492 	xdr_realign_pages(xdr);
1493 	if (nwords > xdr->nwords) {
1494 		nwords = xdr->nwords;
1495 		len = nwords << 2;
1496 	}
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);
1503 	}
1504 	return len;
1505 }
1506 
1507 /**
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
1511  *
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.
1516  *
1517  * Returns the number of XDR encoded bytes now contained in the pages
1518  */
1519 unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
1520 {
1521 	unsigned int nwords = XDR_QUADLEN(len);
1522 	unsigned int base, end, pglen;
1523 
1524 	pglen = xdr_align_pages(xdr, nwords << 2);
1525 	if (pglen == 0)
1526 		return 0;
1527 
1528 	base = (nwords << 2) - pglen;
1529 	end = xdr_stream_remaining(xdr) - pglen;
1530 
1531 	xdr_set_tail_base(xdr, base, end);
1532 	return len <= pglen ? len : pglen;
1533 }
1534 EXPORT_SYMBOL_GPL(xdr_read_pages);
1535 
1536 /**
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
1540  *
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.
1544  *
1545  * Returns True if the operation was successful, and False otherwise.
1546  */
1547 void xdr_set_pagelen(struct xdr_stream *xdr, unsigned int len)
1548 {
1549 	struct xdr_buf *buf = xdr->buf;
1550 	size_t remaining = xdr_stream_remaining(xdr);
1551 	size_t base = 0;
1552 
1553 	if (len < buf->page_len) {
1554 		base = buf->page_len - len;
1555 		xdr_shrink_pagelen(buf, len);
1556 	} else {
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);
1561 	}
1562 	xdr_set_tail_base(xdr, base, remaining);
1563 }
1564 EXPORT_SYMBOL_GPL(xdr_set_pagelen);
1565 
1566 /**
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
1570  *
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.
1575  */
1576 void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
1577 {
1578 	len = xdr_align_pages(xdr, len);
1579 	/*
1580 	 * Position current pointer at beginning of tail, and
1581 	 * set remaining message length.
1582 	 */
1583 	if (len != 0)
1584 		xdr_set_page_base(xdr, 0, len);
1585 }
1586 EXPORT_SYMBOL_GPL(xdr_enter_page);
1587 
1588 static const struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
1589 
1590 void xdr_buf_from_iov(const struct kvec *iov, struct xdr_buf *buf)
1591 {
1592 	buf->head[0] = *iov;
1593 	buf->tail[0] = empty_iov;
1594 	buf->page_len = 0;
1595 	buf->buflen = buf->len = iov->iov_len;
1596 }
1597 EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
1598 
1599 /**
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
1605  *
1606  * sets @subbuf to an xdr buffer representing the portion of @buf of
1607  * length @len starting at offset @base.
1608  *
1609  * @buf and @subbuf may be pointers to the same struct xdr_buf.
1610  *
1611  * Returns -1 if base or length are out of bounds.
1612  */
1613 int xdr_buf_subsegment(const struct xdr_buf *buf, struct xdr_buf *subbuf,
1614 		       unsigned int base, unsigned int len)
1615 {
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;
1622 		base = 0;
1623 	} else {
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;
1627 	}
1628 
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;
1635 		base = 0;
1636 	} else {
1637 		base -= buf->page_len;
1638 		subbuf->pages = buf->pages;
1639 		subbuf->page_base = 0;
1640 		subbuf->page_len = 0;
1641 	}
1642 
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;
1648 		base = 0;
1649 	} else {
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;
1653 	}
1654 
1655 	if (base || len)
1656 		return -1;
1657 	return 0;
1658 }
1659 EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
1660 
1661 /**
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
1666  *
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.
1671  *
1672  * Return values:
1673  *   %true: @subbuf has been initialized, and @xdr has been advanced.
1674  *   %false: a bounds error has occurred
1675  */
1676 bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
1677 			   unsigned int nbytes)
1678 {
1679 	unsigned int start = xdr_stream_pos(xdr);
1680 	unsigned int remaining, len;
1681 
1682 	/* Extract @subbuf and bounds-check the fn arguments */
1683 	if (xdr_buf_subsegment(xdr->buf, subbuf, start, nbytes))
1684 		return false;
1685 
1686 	/* Advance @xdr by @nbytes */
1687 	for (remaining = nbytes; remaining;) {
1688 		if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1689 			return false;
1690 
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)));
1695 			break;
1696 		}
1697 
1698 		xdr->p = (__be32 *)((char *)xdr->p + len);
1699 		xdr->end = xdr->p;
1700 		remaining -= len;
1701 	}
1702 
1703 	xdr_stream_set_pos(xdr, start + nbytes);
1704 	return true;
1705 }
1706 EXPORT_SYMBOL_GPL(xdr_stream_subsegment);
1707 
1708 /**
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
1714  *
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.
1717  */
1718 unsigned int xdr_stream_move_subsegment(struct xdr_stream *xdr, unsigned int offset,
1719 					unsigned int target, unsigned int length)
1720 {
1721 	struct xdr_buf buf;
1722 	unsigned int shift;
1723 
1724 	if (offset < target) {
1725 		shift = target - offset;
1726 		if (xdr_buf_subsegment(xdr->buf, &buf, offset, shift + length) < 0)
1727 			return 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)
1732 			return 0;
1733 		xdr_buf_head_shift_left(&buf, shift, length, shift);
1734 	}
1735 	return length;
1736 }
1737 EXPORT_SYMBOL_GPL(xdr_stream_move_subsegment);
1738 
1739 /**
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
1744  */
1745 unsigned int xdr_stream_zero(struct xdr_stream *xdr, unsigned int offset,
1746 			     unsigned int length)
1747 {
1748 	struct xdr_buf buf;
1749 
1750 	if (xdr_buf_subsegment(xdr->buf, &buf, offset, length) < 0)
1751 		return 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);
1758 	return length;
1759 }
1760 EXPORT_SYMBOL_GPL(xdr_stream_zero);
1761 
1762 /**
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
1766  *
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.
1771  */
1772 void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
1773 {
1774 	size_t cur;
1775 	unsigned int trim = len;
1776 
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;
1780 		trim -= cur;
1781 		if (!trim)
1782 			goto fix_len;
1783 	}
1784 
1785 	if (buf->page_len) {
1786 		cur = min_t(unsigned int, buf->page_len, trim);
1787 		buf->page_len -= cur;
1788 		trim -= cur;
1789 		if (!trim)
1790 			goto fix_len;
1791 	}
1792 
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;
1796 		trim -= cur;
1797 	}
1798 fix_len:
1799 	buf->len -= (len - trim);
1800 }
1801 EXPORT_SYMBOL_GPL(xdr_buf_trim);
1802 
1803 static void __read_bytes_from_xdr_buf(const struct xdr_buf *subbuf,
1804 				      void *obj, unsigned int len)
1805 {
1806 	unsigned int this_len;
1807 
1808 	this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1809 	memcpy(obj, subbuf->head[0].iov_base, this_len);
1810 	len -= this_len;
1811 	obj += 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);
1814 	len -= this_len;
1815 	obj += 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);
1818 }
1819 
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)
1823 {
1824 	struct xdr_buf subbuf;
1825 	int status;
1826 
1827 	status = xdr_buf_subsegment(buf, &subbuf, base, len);
1828 	if (status != 0)
1829 		return status;
1830 	__read_bytes_from_xdr_buf(&subbuf, obj, len);
1831 	return 0;
1832 }
1833 EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
1834 
1835 static void __write_bytes_to_xdr_buf(const struct xdr_buf *subbuf,
1836 				     void *obj, unsigned int len)
1837 {
1838 	unsigned int this_len;
1839 
1840 	this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1841 	memcpy(subbuf->head[0].iov_base, obj, this_len);
1842 	len -= this_len;
1843 	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);
1846 	len -= this_len;
1847 	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);
1850 }
1851 
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)
1855 {
1856 	struct xdr_buf subbuf;
1857 	int status;
1858 
1859 	status = xdr_buf_subsegment(buf, &subbuf, base, len);
1860 	if (status != 0)
1861 		return status;
1862 	__write_bytes_to_xdr_buf(&subbuf, obj, len);
1863 	return 0;
1864 }
1865 EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
1866 
1867 int xdr_decode_word(const struct xdr_buf *buf, unsigned int base, u32 *obj)
1868 {
1869 	__be32	raw;
1870 	int	status;
1871 
1872 	status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
1873 	if (status)
1874 		return status;
1875 	*obj = be32_to_cpu(raw);
1876 	return 0;
1877 }
1878 EXPORT_SYMBOL_GPL(xdr_decode_word);
1879 
1880 int xdr_encode_word(const struct xdr_buf *buf, unsigned int base, u32 obj)
1881 {
1882 	__be32	raw = cpu_to_be32(obj);
1883 
1884 	return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
1885 }
1886 EXPORT_SYMBOL_GPL(xdr_encode_word);
1887 
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)
1891 {
1892 	char *elem = NULL, *c;
1893 	unsigned int copied = 0, todo, avail_here;
1894 	struct page **ppages = NULL;
1895 	int err;
1896 
1897 	if (encode) {
1898 		if (xdr_encode_word(buf, base, desc->array_len) != 0)
1899 			return -EINVAL;
1900 	} else {
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)
1905 			return -EINVAL;
1906 	}
1907 	base += 4;
1908 
1909 	if (!desc->xcode)
1910 		return 0;
1911 
1912 	todo = desc->array_len * desc->elem_size;
1913 
1914 	/* process head */
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);
1919 		todo -= avail_here;
1920 
1921 		while (avail_here >= desc->elem_size) {
1922 			err = desc->xcode(desc, c);
1923 			if (err)
1924 				goto out;
1925 			c += desc->elem_size;
1926 			avail_here -= desc->elem_size;
1927 		}
1928 		if (avail_here) {
1929 			if (!elem) {
1930 				elem = kmalloc(desc->elem_size, GFP_KERNEL);
1931 				err = -ENOMEM;
1932 				if (!elem)
1933 					goto out;
1934 			}
1935 			if (encode) {
1936 				err = desc->xcode(desc, elem);
1937 				if (err)
1938 					goto out;
1939 				memcpy(c, elem, avail_here);
1940 			} else
1941 				memcpy(elem, c, avail_here);
1942 			copied = avail_here;
1943 		}
1944 		base = buf->head->iov_len;  /* align to start of pages */
1945 	}
1946 
1947 	/* process pages array */
1948 	base -= buf->head->iov_len;
1949 	if (todo && base < buf->page_len) {
1950 		unsigned int avail_page;
1951 
1952 		avail_here = min(todo, buf->page_len - base);
1953 		todo -= avail_here;
1954 
1955 		base += buf->page_base;
1956 		ppages = buf->pages + (base >> PAGE_SHIFT);
1957 		base &= ~PAGE_MASK;
1958 		avail_page = min_t(unsigned int, PAGE_SIZE - base,
1959 					avail_here);
1960 		c = kmap(*ppages) + base;
1961 
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);
1967 				if (!elem) {
1968 					elem = kmalloc(desc->elem_size,
1969 						       GFP_KERNEL);
1970 					err = -ENOMEM;
1971 					if (!elem)
1972 						goto out;
1973 				}
1974 				if (encode) {
1975 					if (!copied) {
1976 						err = desc->xcode(desc, elem);
1977 						if (err)
1978 							goto out;
1979 					}
1980 					memcpy(c, elem + copied, l);
1981 					copied += l;
1982 					if (copied == desc->elem_size)
1983 						copied = 0;
1984 				} else {
1985 					memcpy(elem + copied, c, l);
1986 					copied += l;
1987 					if (copied == desc->elem_size) {
1988 						err = desc->xcode(desc, elem);
1989 						if (err)
1990 							goto out;
1991 						copied = 0;
1992 					}
1993 				}
1994 				avail_page -= l;
1995 				c += l;
1996 			}
1997 			while (avail_page >= desc->elem_size) {
1998 				err = desc->xcode(desc, c);
1999 				if (err)
2000 					goto out;
2001 				c += desc->elem_size;
2002 				avail_page -= desc->elem_size;
2003 			}
2004 			if (avail_page) {
2005 				unsigned int l = min(avail_page,
2006 					    desc->elem_size - copied);
2007 				if (!elem) {
2008 					elem = kmalloc(desc->elem_size,
2009 						       GFP_KERNEL);
2010 					err = -ENOMEM;
2011 					if (!elem)
2012 						goto out;
2013 				}
2014 				if (encode) {
2015 					if (!copied) {
2016 						err = desc->xcode(desc, elem);
2017 						if (err)
2018 							goto out;
2019 					}
2020 					memcpy(c, elem + copied, l);
2021 					copied += l;
2022 					if (copied == desc->elem_size)
2023 						copied = 0;
2024 				} else {
2025 					memcpy(elem + copied, c, l);
2026 					copied += l;
2027 					if (copied == desc->elem_size) {
2028 						err = desc->xcode(desc, elem);
2029 						if (err)
2030 							goto out;
2031 						copied = 0;
2032 					}
2033 				}
2034 			}
2035 			if (avail_here) {
2036 				kunmap(*ppages);
2037 				ppages++;
2038 				c = kmap(*ppages);
2039 			}
2040 
2041 			avail_page = min(avail_here,
2042 				 (unsigned int) PAGE_SIZE);
2043 		}
2044 		base = buf->page_len;  /* align to start of tail */
2045 	}
2046 
2047 	/* process tail */
2048 	base -= buf->page_len;
2049 	if (todo) {
2050 		c = buf->tail->iov_base + base;
2051 		if (copied) {
2052 			unsigned int l = desc->elem_size - copied;
2053 
2054 			if (encode)
2055 				memcpy(c, elem + copied, l);
2056 			else {
2057 				memcpy(elem + copied, c, l);
2058 				err = desc->xcode(desc, elem);
2059 				if (err)
2060 					goto out;
2061 			}
2062 			todo -= l;
2063 			c += l;
2064 		}
2065 		while (todo) {
2066 			err = desc->xcode(desc, c);
2067 			if (err)
2068 				goto out;
2069 			c += desc->elem_size;
2070 			todo -= desc->elem_size;
2071 		}
2072 	}
2073 	err = 0;
2074 
2075 out:
2076 	kfree(elem);
2077 	if (ppages)
2078 		kunmap(*ppages);
2079 	return err;
2080 }
2081 
2082 int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base,
2083 		      struct xdr_array2_desc *desc)
2084 {
2085 	if (base >= buf->len)
2086 		return -EINVAL;
2087 
2088 	return xdr_xcode_array2(buf, base, desc, 0);
2089 }
2090 EXPORT_SYMBOL_GPL(xdr_decode_array2);
2091 
2092 int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base,
2093 		      struct xdr_array2_desc *desc)
2094 {
2095 	if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
2096 	    buf->head->iov_len + buf->page_len + buf->tail->iov_len)
2097 		return -EINVAL;
2098 
2099 	return xdr_xcode_array2(buf, base, desc, 1);
2100 }
2101 EXPORT_SYMBOL_GPL(xdr_encode_array2);
2102 
2103 int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset,
2104 		    unsigned int len,
2105 		    int (*actor)(struct scatterlist *, void *), void *data)
2106 {
2107 	int i, ret = 0;
2108 	unsigned int page_len, thislen, page_offset;
2109 	struct scatterlist      sg[1];
2110 
2111 	sg_init_table(sg, 1);
2112 
2113 	if (offset >= buf->head[0].iov_len) {
2114 		offset -= buf->head[0].iov_len;
2115 	} else {
2116 		thislen = buf->head[0].iov_len - offset;
2117 		if (thislen > len)
2118 			thislen = len;
2119 		sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
2120 		ret = actor(sg, data);
2121 		if (ret)
2122 			goto out;
2123 		offset = 0;
2124 		len -= thislen;
2125 	}
2126 	if (len == 0)
2127 		goto out;
2128 
2129 	if (offset >= buf->page_len) {
2130 		offset -= buf->page_len;
2131 	} else {
2132 		page_len = buf->page_len - offset;
2133 		if (page_len > len)
2134 			page_len = len;
2135 		len -= page_len;
2136 		page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
2137 		i = (offset + buf->page_base) >> PAGE_SHIFT;
2138 		thislen = PAGE_SIZE - page_offset;
2139 		do {
2140 			if (thislen > page_len)
2141 				thislen = page_len;
2142 			sg_set_page(sg, buf->pages[i], thislen, page_offset);
2143 			ret = actor(sg, data);
2144 			if (ret)
2145 				goto out;
2146 			page_len -= thislen;
2147 			i++;
2148 			page_offset = 0;
2149 			thislen = PAGE_SIZE;
2150 		} while (page_len != 0);
2151 		offset = 0;
2152 	}
2153 	if (len == 0)
2154 		goto out;
2155 	if (offset < buf->tail[0].iov_len) {
2156 		thislen = buf->tail[0].iov_len - offset;
2157 		if (thislen > len)
2158 			thislen = len;
2159 		sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
2160 		ret = actor(sg, data);
2161 		len -= thislen;
2162 	}
2163 	if (len != 0)
2164 		ret = -EINVAL;
2165 out:
2166 	return ret;
2167 }
2168 EXPORT_SYMBOL_GPL(xdr_process_buf);
2169 
2170 /**
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
2175  *
2176  * Return values:
2177  *   On success, returns size of object stored in *@ptr
2178  *   %-EBADMSG on XDR buffer overflow
2179  *   %-EMSGSIZE on overflow of storage buffer @ptr
2180  */
2181 ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, size_t size)
2182 {
2183 	ssize_t ret;
2184 	void *p;
2185 
2186 	ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
2187 	if (ret <= 0)
2188 		return ret;
2189 	memcpy(ptr, p, ret);
2190 	return ret;
2191 }
2192 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque);
2193 
2194 /**
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
2200  *
2201  * Return values:
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
2206  */
2207 ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
2208 		size_t maxlen, gfp_t gfp_flags)
2209 {
2210 	ssize_t ret;
2211 	void *p;
2212 
2213 	ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2214 	if (ret > 0) {
2215 		*ptr = kmemdup(p, ret, gfp_flags);
2216 		if (*ptr != NULL)
2217 			return ret;
2218 		ret = -ENOMEM;
2219 	}
2220 	*ptr = NULL;
2221 	return ret;
2222 }
2223 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_dup);
2224 
2225 /**
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
2230  *
2231  * Return values:
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
2235  */
2236 ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, size_t size)
2237 {
2238 	ssize_t ret;
2239 	void *p;
2240 
2241 	ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
2242 	if (ret > 0) {
2243 		memcpy(str, p, ret);
2244 		str[ret] = '\0';
2245 		return strlen(str);
2246 	}
2247 	*str = '\0';
2248 	return ret;
2249 }
2250 EXPORT_SYMBOL_GPL(xdr_stream_decode_string);
2251 
2252 /**
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
2258  *
2259  * Return values:
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
2264  */
2265 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
2266 		size_t maxlen, gfp_t gfp_flags)
2267 {
2268 	void *p;
2269 	ssize_t ret;
2270 
2271 	ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2272 	if (ret > 0) {
2273 		char *s = kmemdup_nul(p, ret, gfp_flags);
2274 		if (s != NULL) {
2275 			*str = s;
2276 			return strlen(s);
2277 		}
2278 		ret = -ENOMEM;
2279 	}
2280 	*str = NULL;
2281 	return ret;
2282 }
2283 EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);
2284 
2285 /**
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
2291  *
2292  * Return values:
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
2296  */
2297 ssize_t xdr_stream_decode_opaque_auth(struct xdr_stream *xdr, u32 *flavor,
2298 				      void **body, unsigned int *body_len)
2299 {
2300 	ssize_t ret, len;
2301 
2302 	len = xdr_stream_decode_u32(xdr, flavor);
2303 	if (unlikely(len < 0))
2304 		return len;
2305 	ret = xdr_stream_decode_opaque_inline(xdr, body, RPC_MAX_AUTH_SIZE);
2306 	if (unlikely(ret < 0))
2307 		return ret;
2308 	*body_len = ret;
2309 	return len + ret;
2310 }
2311 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_auth);
2312 
2313 /**
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
2319  *
2320  * Return values:
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
2324  */
2325 ssize_t xdr_stream_encode_opaque_auth(struct xdr_stream *xdr, u32 flavor,
2326 				      void *body, unsigned int body_len)
2327 {
2328 	ssize_t ret, len;
2329 
2330 	if (unlikely(body_len > RPC_MAX_AUTH_SIZE))
2331 		return -EMSGSIZE;
2332 	len = xdr_stream_encode_u32(xdr, flavor);
2333 	if (unlikely(len < 0))
2334 		return len;
2335 	ret = xdr_stream_encode_opaque(xdr, body, body_len);
2336 	if (unlikely(ret < 0))
2337 		return ret;
2338 	return len + ret;
2339 }
2340 EXPORT_SYMBOL_GPL(xdr_stream_encode_opaque_auth);
2341