xref: /linux/net/sunrpc/xdr.c (revision ef815d2cba782e96b9aad9483523d474ed41c62a)
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  * xdr_reserve_space_vec - Reserves a large amount of buffer space for sending
1075  * @xdr: pointer to xdr_stream
1076  * @nbytes: number of bytes to reserve
1077  *
1078  * The size argument passed to xdr_reserve_space() is determined based
1079  * on the number of bytes remaining in the current page to avoid
1080  * invalidating iov_base pointers when xdr_commit_encode() is called.
1081  *
1082  * Return values:
1083  *   %0: success
1084  *   %-EMSGSIZE: not enough space is available in @xdr
1085  */
1086 int xdr_reserve_space_vec(struct xdr_stream *xdr, size_t nbytes)
1087 {
1088 	size_t thislen;
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 	/* XXX: Let's find a way to make this more efficient */
1101 	while (nbytes) {
1102 		thislen = xdr->buf->page_len % PAGE_SIZE;
1103 		thislen = min_t(size_t, nbytes, PAGE_SIZE - thislen);
1104 
1105 		p = xdr_reserve_space(xdr, thislen);
1106 		if (!p)
1107 			return -EMSGSIZE;
1108 
1109 		nbytes -= thislen;
1110 	}
1111 
1112 	return 0;
1113 }
1114 EXPORT_SYMBOL_GPL(xdr_reserve_space_vec);
1115 
1116 /**
1117  * xdr_truncate_encode - truncate an encode buffer
1118  * @xdr: pointer to xdr_stream
1119  * @len: new length of buffer
1120  *
1121  * Truncates the xdr stream, so that xdr->buf->len == len,
1122  * and xdr->p points at offset len from the start of the buffer, and
1123  * head, tail, and page lengths are adjusted to correspond.
1124  *
1125  * If this means moving xdr->p to a different buffer, we assume that
1126  * the end pointer should be set to the end of the current page,
1127  * except in the case of the head buffer when we assume the head
1128  * buffer's current length represents the end of the available buffer.
1129  *
1130  * This is *not* safe to use on a buffer that already has inlined page
1131  * cache pages (as in a zero-copy server read reply), except for the
1132  * simple case of truncating from one position in the tail to another.
1133  *
1134  */
1135 void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
1136 {
1137 	struct xdr_buf *buf = xdr->buf;
1138 	struct kvec *head = buf->head;
1139 	struct kvec *tail = buf->tail;
1140 	int fraglen;
1141 	int new;
1142 
1143 	if (len > buf->len) {
1144 		WARN_ON_ONCE(1);
1145 		return;
1146 	}
1147 	xdr_commit_encode(xdr);
1148 
1149 	fraglen = min_t(int, buf->len - len, tail->iov_len);
1150 	tail->iov_len -= fraglen;
1151 	buf->len -= fraglen;
1152 	if (tail->iov_len) {
1153 		xdr->p = tail->iov_base + tail->iov_len;
1154 		WARN_ON_ONCE(!xdr->end);
1155 		WARN_ON_ONCE(!xdr->iov);
1156 		return;
1157 	}
1158 	WARN_ON_ONCE(fraglen);
1159 	fraglen = min_t(int, buf->len - len, buf->page_len);
1160 	buf->page_len -= fraglen;
1161 	buf->len -= fraglen;
1162 
1163 	new = buf->page_base + buf->page_len;
1164 
1165 	xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
1166 
1167 	if (buf->page_len) {
1168 		xdr->p = page_address(*xdr->page_ptr);
1169 		xdr->end = (void *)xdr->p + PAGE_SIZE;
1170 		xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
1171 		WARN_ON_ONCE(xdr->iov);
1172 		return;
1173 	}
1174 	if (fraglen)
1175 		xdr->end = head->iov_base + head->iov_len;
1176 	/* (otherwise assume xdr->end is already set) */
1177 	xdr->page_ptr--;
1178 	head->iov_len = len;
1179 	buf->len = len;
1180 	xdr->p = head->iov_base + head->iov_len;
1181 	xdr->iov = buf->head;
1182 }
1183 EXPORT_SYMBOL(xdr_truncate_encode);
1184 
1185 /**
1186  * xdr_truncate_decode - Truncate a decoding stream
1187  * @xdr: pointer to struct xdr_stream
1188  * @len: Number of bytes to remove
1189  *
1190  */
1191 void xdr_truncate_decode(struct xdr_stream *xdr, size_t len)
1192 {
1193 	unsigned int nbytes = xdr_align_size(len);
1194 
1195 	xdr->buf->len -= nbytes;
1196 	xdr->nwords -= XDR_QUADLEN(nbytes);
1197 }
1198 EXPORT_SYMBOL_GPL(xdr_truncate_decode);
1199 
1200 /**
1201  * xdr_restrict_buflen - decrease available buffer space
1202  * @xdr: pointer to xdr_stream
1203  * @newbuflen: new maximum number of bytes available
1204  *
1205  * Adjust our idea of how much space is available in the buffer.
1206  * If we've already used too much space in the buffer, returns -1.
1207  * If the available space is already smaller than newbuflen, returns 0
1208  * and does nothing.  Otherwise, adjusts xdr->buf->buflen to newbuflen
1209  * and ensures xdr->end is set at most offset newbuflen from the start
1210  * of the buffer.
1211  */
1212 int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
1213 {
1214 	struct xdr_buf *buf = xdr->buf;
1215 	int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
1216 	int end_offset = buf->len + left_in_this_buf;
1217 
1218 	if (newbuflen < 0 || newbuflen < buf->len)
1219 		return -1;
1220 	if (newbuflen > buf->buflen)
1221 		return 0;
1222 	if (newbuflen < end_offset)
1223 		xdr->end = (void *)xdr->end + newbuflen - end_offset;
1224 	buf->buflen = newbuflen;
1225 	return 0;
1226 }
1227 EXPORT_SYMBOL(xdr_restrict_buflen);
1228 
1229 /**
1230  * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
1231  * @xdr: pointer to xdr_stream
1232  * @pages: array of pages to insert
1233  * @base: starting offset of first data byte in @pages
1234  * @len: number of data bytes in @pages to insert
1235  *
1236  * After the @pages are added, the tail iovec is instantiated pointing to
1237  * end of the head buffer, and the stream is set up to encode subsequent
1238  * items into the tail.
1239  */
1240 void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
1241 		 unsigned int len)
1242 {
1243 	struct xdr_buf *buf = xdr->buf;
1244 	struct kvec *tail = buf->tail;
1245 
1246 	buf->pages = pages;
1247 	buf->page_base = base;
1248 	buf->page_len = len;
1249 
1250 	tail->iov_base = xdr->p;
1251 	tail->iov_len = 0;
1252 	xdr->iov = tail;
1253 
1254 	if (len & 3) {
1255 		unsigned int pad = 4 - (len & 3);
1256 
1257 		BUG_ON(xdr->p >= xdr->end);
1258 		tail->iov_base = (char *)xdr->p + (len & 3);
1259 		tail->iov_len += pad;
1260 		len += pad;
1261 		*xdr->p++ = 0;
1262 	}
1263 	buf->buflen += len;
1264 	buf->len += len;
1265 }
1266 EXPORT_SYMBOL_GPL(xdr_write_pages);
1267 
1268 static unsigned int xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
1269 				unsigned int base, unsigned int len)
1270 {
1271 	if (len > iov->iov_len)
1272 		len = iov->iov_len;
1273 	if (unlikely(base > len))
1274 		base = len;
1275 	xdr->p = (__be32*)(iov->iov_base + base);
1276 	xdr->end = (__be32*)(iov->iov_base + len);
1277 	xdr->iov = iov;
1278 	xdr->page_ptr = NULL;
1279 	return len - base;
1280 }
1281 
1282 static unsigned int xdr_set_tail_base(struct xdr_stream *xdr,
1283 				      unsigned int base, unsigned int len)
1284 {
1285 	struct xdr_buf *buf = xdr->buf;
1286 
1287 	xdr_stream_set_pos(xdr, base + buf->page_len + buf->head->iov_len);
1288 	return xdr_set_iov(xdr, buf->tail, base, len);
1289 }
1290 
1291 static unsigned int xdr_set_page_base(struct xdr_stream *xdr,
1292 				      unsigned int base, unsigned int len)
1293 {
1294 	unsigned int pgnr;
1295 	unsigned int maxlen;
1296 	unsigned int pgoff;
1297 	unsigned int pgend;
1298 	void *kaddr;
1299 
1300 	maxlen = xdr->buf->page_len;
1301 	if (base >= maxlen)
1302 		return 0;
1303 	else
1304 		maxlen -= base;
1305 	if (len > maxlen)
1306 		len = maxlen;
1307 
1308 	xdr_stream_page_set_pos(xdr, base);
1309 	base += xdr->buf->page_base;
1310 
1311 	pgnr = base >> PAGE_SHIFT;
1312 	xdr->page_ptr = &xdr->buf->pages[pgnr];
1313 	kaddr = page_address(*xdr->page_ptr);
1314 
1315 	pgoff = base & ~PAGE_MASK;
1316 	xdr->p = (__be32*)(kaddr + pgoff);
1317 
1318 	pgend = pgoff + len;
1319 	if (pgend > PAGE_SIZE)
1320 		pgend = PAGE_SIZE;
1321 	xdr->end = (__be32*)(kaddr + pgend);
1322 	xdr->iov = NULL;
1323 	return len;
1324 }
1325 
1326 static void xdr_set_page(struct xdr_stream *xdr, unsigned int base,
1327 			 unsigned int len)
1328 {
1329 	if (xdr_set_page_base(xdr, base, len) == 0) {
1330 		base -= xdr->buf->page_len;
1331 		xdr_set_tail_base(xdr, base, len);
1332 	}
1333 }
1334 
1335 static void xdr_set_next_page(struct xdr_stream *xdr)
1336 {
1337 	unsigned int newbase;
1338 
1339 	newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
1340 	newbase -= xdr->buf->page_base;
1341 	if (newbase < xdr->buf->page_len)
1342 		xdr_set_page_base(xdr, newbase, xdr_stream_remaining(xdr));
1343 	else
1344 		xdr_set_tail_base(xdr, 0, xdr_stream_remaining(xdr));
1345 }
1346 
1347 static bool xdr_set_next_buffer(struct xdr_stream *xdr)
1348 {
1349 	if (xdr->page_ptr != NULL)
1350 		xdr_set_next_page(xdr);
1351 	else if (xdr->iov == xdr->buf->head)
1352 		xdr_set_page(xdr, 0, xdr_stream_remaining(xdr));
1353 	return xdr->p != xdr->end;
1354 }
1355 
1356 /**
1357  * xdr_init_decode - Initialize an xdr_stream for decoding data.
1358  * @xdr: pointer to xdr_stream struct
1359  * @buf: pointer to XDR buffer from which to decode data
1360  * @p: current pointer inside XDR buffer
1361  * @rqst: pointer to controlling rpc_rqst, for debugging
1362  */
1363 void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
1364 		     struct rpc_rqst *rqst)
1365 {
1366 	xdr->buf = buf;
1367 	xdr_reset_scratch_buffer(xdr);
1368 	xdr->nwords = XDR_QUADLEN(buf->len);
1369 	if (xdr_set_iov(xdr, buf->head, 0, buf->len) == 0 &&
1370 	    xdr_set_page_base(xdr, 0, buf->len) == 0)
1371 		xdr_set_iov(xdr, buf->tail, 0, buf->len);
1372 	if (p != NULL && p > xdr->p && xdr->end >= p) {
1373 		xdr->nwords -= p - xdr->p;
1374 		xdr->p = p;
1375 	}
1376 	xdr->rqst = rqst;
1377 }
1378 EXPORT_SYMBOL_GPL(xdr_init_decode);
1379 
1380 /**
1381  * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages
1382  * @xdr: pointer to xdr_stream struct
1383  * @buf: pointer to XDR buffer from which to decode data
1384  * @pages: list of pages to decode into
1385  * @len: length in bytes of buffer in pages
1386  */
1387 void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
1388 			   struct page **pages, unsigned int len)
1389 {
1390 	memset(buf, 0, sizeof(*buf));
1391 	buf->pages =  pages;
1392 	buf->page_len =  len;
1393 	buf->buflen =  len;
1394 	buf->len = len;
1395 	xdr_init_decode(xdr, buf, NULL, NULL);
1396 }
1397 EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
1398 
1399 static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1400 {
1401 	unsigned int nwords = XDR_QUADLEN(nbytes);
1402 	__be32 *p = xdr->p;
1403 	__be32 *q = p + nwords;
1404 
1405 	if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
1406 		return NULL;
1407 	xdr->p = q;
1408 	xdr->nwords -= nwords;
1409 	return p;
1410 }
1411 
1412 static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
1413 {
1414 	__be32 *p;
1415 	char *cpdest = xdr->scratch.iov_base;
1416 	size_t cplen = (char *)xdr->end - (char *)xdr->p;
1417 
1418 	if (nbytes > xdr->scratch.iov_len)
1419 		goto out_overflow;
1420 	p = __xdr_inline_decode(xdr, cplen);
1421 	if (p == NULL)
1422 		return NULL;
1423 	memcpy(cpdest, p, cplen);
1424 	if (!xdr_set_next_buffer(xdr))
1425 		goto out_overflow;
1426 	cpdest += cplen;
1427 	nbytes -= cplen;
1428 	p = __xdr_inline_decode(xdr, nbytes);
1429 	if (p == NULL)
1430 		return NULL;
1431 	memcpy(cpdest, p, nbytes);
1432 	return xdr->scratch.iov_base;
1433 out_overflow:
1434 	trace_rpc_xdr_overflow(xdr, nbytes);
1435 	return NULL;
1436 }
1437 
1438 /**
1439  * xdr_inline_decode - Retrieve XDR data to decode
1440  * @xdr: pointer to xdr_stream struct
1441  * @nbytes: number of bytes of data to decode
1442  *
1443  * Check if the input buffer is long enough to enable us to decode
1444  * 'nbytes' more bytes of data starting at the current position.
1445  * If so return the current pointer, then update the current
1446  * pointer position.
1447  */
1448 __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1449 {
1450 	__be32 *p;
1451 
1452 	if (unlikely(nbytes == 0))
1453 		return xdr->p;
1454 	if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1455 		goto out_overflow;
1456 	p = __xdr_inline_decode(xdr, nbytes);
1457 	if (p != NULL)
1458 		return p;
1459 	return xdr_copy_to_scratch(xdr, nbytes);
1460 out_overflow:
1461 	trace_rpc_xdr_overflow(xdr, nbytes);
1462 	return NULL;
1463 }
1464 EXPORT_SYMBOL_GPL(xdr_inline_decode);
1465 
1466 static void xdr_realign_pages(struct xdr_stream *xdr)
1467 {
1468 	struct xdr_buf *buf = xdr->buf;
1469 	struct kvec *iov = buf->head;
1470 	unsigned int cur = xdr_stream_pos(xdr);
1471 	unsigned int copied;
1472 
1473 	/* Realign pages to current pointer position */
1474 	if (iov->iov_len > cur) {
1475 		copied = xdr_shrink_bufhead(buf, cur);
1476 		trace_rpc_xdr_alignment(xdr, cur, copied);
1477 		xdr_set_page(xdr, 0, buf->page_len);
1478 	}
1479 }
1480 
1481 static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
1482 {
1483 	struct xdr_buf *buf = xdr->buf;
1484 	unsigned int nwords = XDR_QUADLEN(len);
1485 	unsigned int copied;
1486 
1487 	if (xdr->nwords == 0)
1488 		return 0;
1489 
1490 	xdr_realign_pages(xdr);
1491 	if (nwords > xdr->nwords) {
1492 		nwords = xdr->nwords;
1493 		len = nwords << 2;
1494 	}
1495 	if (buf->page_len <= len)
1496 		len = buf->page_len;
1497 	else if (nwords < xdr->nwords) {
1498 		/* Truncate page data and move it into the tail */
1499 		copied = xdr_shrink_pagelen(buf, len);
1500 		trace_rpc_xdr_alignment(xdr, len, copied);
1501 	}
1502 	return len;
1503 }
1504 
1505 /**
1506  * xdr_read_pages - align page-based XDR data to current pointer position
1507  * @xdr: pointer to xdr_stream struct
1508  * @len: number of bytes of page data
1509  *
1510  * Moves data beyond the current pointer position from the XDR head[] buffer
1511  * into the page list. Any data that lies beyond current position + @len
1512  * bytes is moved into the XDR tail[]. The xdr_stream current position is
1513  * then advanced past that data to align to the next XDR object in the tail.
1514  *
1515  * Returns the number of XDR encoded bytes now contained in the pages
1516  */
1517 unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
1518 {
1519 	unsigned int nwords = XDR_QUADLEN(len);
1520 	unsigned int base, end, pglen;
1521 
1522 	pglen = xdr_align_pages(xdr, nwords << 2);
1523 	if (pglen == 0)
1524 		return 0;
1525 
1526 	base = (nwords << 2) - pglen;
1527 	end = xdr_stream_remaining(xdr) - pglen;
1528 
1529 	xdr_set_tail_base(xdr, base, end);
1530 	return len <= pglen ? len : pglen;
1531 }
1532 EXPORT_SYMBOL_GPL(xdr_read_pages);
1533 
1534 /**
1535  * xdr_set_pagelen - Sets the length of the XDR pages
1536  * @xdr: pointer to xdr_stream struct
1537  * @len: new length of the XDR page data
1538  *
1539  * Either grows or shrinks the length of the xdr pages by setting pagelen to
1540  * @len bytes. When shrinking, any extra data is moved into buf->tail, whereas
1541  * when growing any data beyond the current pointer is moved into the tail.
1542  *
1543  * Returns True if the operation was successful, and False otherwise.
1544  */
1545 void xdr_set_pagelen(struct xdr_stream *xdr, unsigned int len)
1546 {
1547 	struct xdr_buf *buf = xdr->buf;
1548 	size_t remaining = xdr_stream_remaining(xdr);
1549 	size_t base = 0;
1550 
1551 	if (len < buf->page_len) {
1552 		base = buf->page_len - len;
1553 		xdr_shrink_pagelen(buf, len);
1554 	} else {
1555 		xdr_buf_head_shift_right(buf, xdr_stream_pos(xdr),
1556 					 buf->page_len, remaining);
1557 		if (len > buf->page_len)
1558 			xdr_buf_try_expand(buf, len - buf->page_len);
1559 	}
1560 	xdr_set_tail_base(xdr, base, remaining);
1561 }
1562 EXPORT_SYMBOL_GPL(xdr_set_pagelen);
1563 
1564 /**
1565  * xdr_enter_page - decode data from the XDR page
1566  * @xdr: pointer to xdr_stream struct
1567  * @len: number of bytes of page data
1568  *
1569  * Moves data beyond the current pointer position from the XDR head[] buffer
1570  * into the page list. Any data that lies beyond current position + "len"
1571  * bytes is moved into the XDR tail[]. The current pointer is then
1572  * repositioned at the beginning of the first XDR page.
1573  */
1574 void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
1575 {
1576 	len = xdr_align_pages(xdr, len);
1577 	/*
1578 	 * Position current pointer at beginning of tail, and
1579 	 * set remaining message length.
1580 	 */
1581 	if (len != 0)
1582 		xdr_set_page_base(xdr, 0, len);
1583 }
1584 EXPORT_SYMBOL_GPL(xdr_enter_page);
1585 
1586 static const struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
1587 
1588 void xdr_buf_from_iov(const struct kvec *iov, struct xdr_buf *buf)
1589 {
1590 	buf->head[0] = *iov;
1591 	buf->tail[0] = empty_iov;
1592 	buf->page_len = 0;
1593 	buf->buflen = buf->len = iov->iov_len;
1594 }
1595 EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
1596 
1597 /**
1598  * xdr_buf_subsegment - set subbuf to a portion of buf
1599  * @buf: an xdr buffer
1600  * @subbuf: the result buffer
1601  * @base: beginning of range in bytes
1602  * @len: length of range in bytes
1603  *
1604  * sets @subbuf to an xdr buffer representing the portion of @buf of
1605  * length @len starting at offset @base.
1606  *
1607  * @buf and @subbuf may be pointers to the same struct xdr_buf.
1608  *
1609  * Returns -1 if base or length are out of bounds.
1610  */
1611 int xdr_buf_subsegment(const struct xdr_buf *buf, struct xdr_buf *subbuf,
1612 		       unsigned int base, unsigned int len)
1613 {
1614 	subbuf->buflen = subbuf->len = len;
1615 	if (base < buf->head[0].iov_len) {
1616 		subbuf->head[0].iov_base = buf->head[0].iov_base + base;
1617 		subbuf->head[0].iov_len = min_t(unsigned int, len,
1618 						buf->head[0].iov_len - base);
1619 		len -= subbuf->head[0].iov_len;
1620 		base = 0;
1621 	} else {
1622 		base -= buf->head[0].iov_len;
1623 		subbuf->head[0].iov_base = buf->head[0].iov_base;
1624 		subbuf->head[0].iov_len = 0;
1625 	}
1626 
1627 	if (base < buf->page_len) {
1628 		subbuf->page_len = min(buf->page_len - base, len);
1629 		base += buf->page_base;
1630 		subbuf->page_base = base & ~PAGE_MASK;
1631 		subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
1632 		len -= subbuf->page_len;
1633 		base = 0;
1634 	} else {
1635 		base -= buf->page_len;
1636 		subbuf->pages = buf->pages;
1637 		subbuf->page_base = 0;
1638 		subbuf->page_len = 0;
1639 	}
1640 
1641 	if (base < buf->tail[0].iov_len) {
1642 		subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
1643 		subbuf->tail[0].iov_len = min_t(unsigned int, len,
1644 						buf->tail[0].iov_len - base);
1645 		len -= subbuf->tail[0].iov_len;
1646 		base = 0;
1647 	} else {
1648 		base -= buf->tail[0].iov_len;
1649 		subbuf->tail[0].iov_base = buf->tail[0].iov_base;
1650 		subbuf->tail[0].iov_len = 0;
1651 	}
1652 
1653 	if (base || len)
1654 		return -1;
1655 	return 0;
1656 }
1657 EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
1658 
1659 /**
1660  * xdr_stream_subsegment - set @subbuf to a portion of @xdr
1661  * @xdr: an xdr_stream set up for decoding
1662  * @subbuf: the result buffer
1663  * @nbytes: length of @xdr to extract, in bytes
1664  *
1665  * Sets up @subbuf to represent a portion of @xdr. The portion
1666  * starts at the current offset in @xdr, and extends for a length
1667  * of @nbytes. If this is successful, @xdr is advanced to the next
1668  * XDR data item following that portion.
1669  *
1670  * Return values:
1671  *   %true: @subbuf has been initialized, and @xdr has been advanced.
1672  *   %false: a bounds error has occurred
1673  */
1674 bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
1675 			   unsigned int nbytes)
1676 {
1677 	unsigned int start = xdr_stream_pos(xdr);
1678 	unsigned int remaining, len;
1679 
1680 	/* Extract @subbuf and bounds-check the fn arguments */
1681 	if (xdr_buf_subsegment(xdr->buf, subbuf, start, nbytes))
1682 		return false;
1683 
1684 	/* Advance @xdr by @nbytes */
1685 	for (remaining = nbytes; remaining;) {
1686 		if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1687 			return false;
1688 
1689 		len = (char *)xdr->end - (char *)xdr->p;
1690 		if (remaining <= len) {
1691 			xdr->p = (__be32 *)((char *)xdr->p +
1692 					(remaining + xdr_pad_size(nbytes)));
1693 			break;
1694 		}
1695 
1696 		xdr->p = (__be32 *)((char *)xdr->p + len);
1697 		xdr->end = xdr->p;
1698 		remaining -= len;
1699 	}
1700 
1701 	xdr_stream_set_pos(xdr, start + nbytes);
1702 	return true;
1703 }
1704 EXPORT_SYMBOL_GPL(xdr_stream_subsegment);
1705 
1706 /**
1707  * xdr_stream_move_subsegment - Move part of a stream to another position
1708  * @xdr: the source xdr_stream
1709  * @offset: the source offset of the segment
1710  * @target: the target offset of the segment
1711  * @length: the number of bytes to move
1712  *
1713  * Moves @length bytes from @offset to @target in the xdr_stream, overwriting
1714  * anything in its space. Returns the number of bytes in the segment.
1715  */
1716 unsigned int xdr_stream_move_subsegment(struct xdr_stream *xdr, unsigned int offset,
1717 					unsigned int target, unsigned int length)
1718 {
1719 	struct xdr_buf buf;
1720 	unsigned int shift;
1721 
1722 	if (offset < target) {
1723 		shift = target - offset;
1724 		if (xdr_buf_subsegment(xdr->buf, &buf, offset, shift + length) < 0)
1725 			return 0;
1726 		xdr_buf_head_shift_right(&buf, 0, length, shift);
1727 	} else if (offset > target) {
1728 		shift = offset - target;
1729 		if (xdr_buf_subsegment(xdr->buf, &buf, target, shift + length) < 0)
1730 			return 0;
1731 		xdr_buf_head_shift_left(&buf, shift, length, shift);
1732 	}
1733 	return length;
1734 }
1735 EXPORT_SYMBOL_GPL(xdr_stream_move_subsegment);
1736 
1737 /**
1738  * xdr_stream_zero - zero out a portion of an xdr_stream
1739  * @xdr: an xdr_stream to zero out
1740  * @offset: the starting point in the stream
1741  * @length: the number of bytes to zero
1742  */
1743 unsigned int xdr_stream_zero(struct xdr_stream *xdr, unsigned int offset,
1744 			     unsigned int length)
1745 {
1746 	struct xdr_buf buf;
1747 
1748 	if (xdr_buf_subsegment(xdr->buf, &buf, offset, length) < 0)
1749 		return 0;
1750 	if (buf.head[0].iov_len)
1751 		xdr_buf_iov_zero(buf.head, 0, buf.head[0].iov_len);
1752 	if (buf.page_len > 0)
1753 		xdr_buf_pages_zero(&buf, 0, buf.page_len);
1754 	if (buf.tail[0].iov_len)
1755 		xdr_buf_iov_zero(buf.tail, 0, buf.tail[0].iov_len);
1756 	return length;
1757 }
1758 EXPORT_SYMBOL_GPL(xdr_stream_zero);
1759 
1760 /**
1761  * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
1762  * @buf: buf to be trimmed
1763  * @len: number of bytes to reduce "buf" by
1764  *
1765  * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
1766  * that it's possible that we'll trim less than that amount if the xdr_buf is
1767  * too small, or if (for instance) it's all in the head and the parser has
1768  * already read too far into it.
1769  */
1770 void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
1771 {
1772 	size_t cur;
1773 	unsigned int trim = len;
1774 
1775 	if (buf->tail[0].iov_len) {
1776 		cur = min_t(size_t, buf->tail[0].iov_len, trim);
1777 		buf->tail[0].iov_len -= cur;
1778 		trim -= cur;
1779 		if (!trim)
1780 			goto fix_len;
1781 	}
1782 
1783 	if (buf->page_len) {
1784 		cur = min_t(unsigned int, buf->page_len, trim);
1785 		buf->page_len -= cur;
1786 		trim -= cur;
1787 		if (!trim)
1788 			goto fix_len;
1789 	}
1790 
1791 	if (buf->head[0].iov_len) {
1792 		cur = min_t(size_t, buf->head[0].iov_len, trim);
1793 		buf->head[0].iov_len -= cur;
1794 		trim -= cur;
1795 	}
1796 fix_len:
1797 	buf->len -= (len - trim);
1798 }
1799 EXPORT_SYMBOL_GPL(xdr_buf_trim);
1800 
1801 static void __read_bytes_from_xdr_buf(const struct xdr_buf *subbuf,
1802 				      void *obj, unsigned int len)
1803 {
1804 	unsigned int this_len;
1805 
1806 	this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1807 	memcpy(obj, subbuf->head[0].iov_base, this_len);
1808 	len -= this_len;
1809 	obj += this_len;
1810 	this_len = min_t(unsigned int, len, subbuf->page_len);
1811 	_copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
1812 	len -= this_len;
1813 	obj += this_len;
1814 	this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1815 	memcpy(obj, subbuf->tail[0].iov_base, this_len);
1816 }
1817 
1818 /* obj is assumed to point to allocated memory of size at least len: */
1819 int read_bytes_from_xdr_buf(const struct xdr_buf *buf, unsigned int base,
1820 			    void *obj, unsigned int len)
1821 {
1822 	struct xdr_buf subbuf;
1823 	int status;
1824 
1825 	status = xdr_buf_subsegment(buf, &subbuf, base, len);
1826 	if (status != 0)
1827 		return status;
1828 	__read_bytes_from_xdr_buf(&subbuf, obj, len);
1829 	return 0;
1830 }
1831 EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
1832 
1833 static void __write_bytes_to_xdr_buf(const struct xdr_buf *subbuf,
1834 				     void *obj, unsigned int len)
1835 {
1836 	unsigned int this_len;
1837 
1838 	this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1839 	memcpy(subbuf->head[0].iov_base, obj, this_len);
1840 	len -= this_len;
1841 	obj += this_len;
1842 	this_len = min_t(unsigned int, len, subbuf->page_len);
1843 	_copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
1844 	len -= this_len;
1845 	obj += this_len;
1846 	this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1847 	memcpy(subbuf->tail[0].iov_base, obj, this_len);
1848 }
1849 
1850 /* obj is assumed to point to allocated memory of size at least len: */
1851 int write_bytes_to_xdr_buf(const struct xdr_buf *buf, unsigned int base,
1852 			   void *obj, unsigned int len)
1853 {
1854 	struct xdr_buf subbuf;
1855 	int status;
1856 
1857 	status = xdr_buf_subsegment(buf, &subbuf, base, len);
1858 	if (status != 0)
1859 		return status;
1860 	__write_bytes_to_xdr_buf(&subbuf, obj, len);
1861 	return 0;
1862 }
1863 EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
1864 
1865 int xdr_decode_word(const struct xdr_buf *buf, unsigned int base, u32 *obj)
1866 {
1867 	__be32	raw;
1868 	int	status;
1869 
1870 	status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
1871 	if (status)
1872 		return status;
1873 	*obj = be32_to_cpu(raw);
1874 	return 0;
1875 }
1876 EXPORT_SYMBOL_GPL(xdr_decode_word);
1877 
1878 int xdr_encode_word(const struct xdr_buf *buf, unsigned int base, u32 obj)
1879 {
1880 	__be32	raw = cpu_to_be32(obj);
1881 
1882 	return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
1883 }
1884 EXPORT_SYMBOL_GPL(xdr_encode_word);
1885 
1886 /* Returns 0 on success, or else a negative error code. */
1887 static int xdr_xcode_array2(const struct xdr_buf *buf, unsigned int base,
1888 			    struct xdr_array2_desc *desc, int encode)
1889 {
1890 	char *elem = NULL, *c;
1891 	unsigned int copied = 0, todo, avail_here;
1892 	struct page **ppages = NULL;
1893 	int err;
1894 
1895 	if (encode) {
1896 		if (xdr_encode_word(buf, base, desc->array_len) != 0)
1897 			return -EINVAL;
1898 	} else {
1899 		if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
1900 		    desc->array_len > desc->array_maxlen ||
1901 		    (unsigned long) base + 4 + desc->array_len *
1902 				    desc->elem_size > buf->len)
1903 			return -EINVAL;
1904 	}
1905 	base += 4;
1906 
1907 	if (!desc->xcode)
1908 		return 0;
1909 
1910 	todo = desc->array_len * desc->elem_size;
1911 
1912 	/* process head */
1913 	if (todo && base < buf->head->iov_len) {
1914 		c = buf->head->iov_base + base;
1915 		avail_here = min_t(unsigned int, todo,
1916 				   buf->head->iov_len - base);
1917 		todo -= avail_here;
1918 
1919 		while (avail_here >= desc->elem_size) {
1920 			err = desc->xcode(desc, c);
1921 			if (err)
1922 				goto out;
1923 			c += desc->elem_size;
1924 			avail_here -= desc->elem_size;
1925 		}
1926 		if (avail_here) {
1927 			if (!elem) {
1928 				elem = kmalloc(desc->elem_size, GFP_KERNEL);
1929 				err = -ENOMEM;
1930 				if (!elem)
1931 					goto out;
1932 			}
1933 			if (encode) {
1934 				err = desc->xcode(desc, elem);
1935 				if (err)
1936 					goto out;
1937 				memcpy(c, elem, avail_here);
1938 			} else
1939 				memcpy(elem, c, avail_here);
1940 			copied = avail_here;
1941 		}
1942 		base = buf->head->iov_len;  /* align to start of pages */
1943 	}
1944 
1945 	/* process pages array */
1946 	base -= buf->head->iov_len;
1947 	if (todo && base < buf->page_len) {
1948 		unsigned int avail_page;
1949 
1950 		avail_here = min(todo, buf->page_len - base);
1951 		todo -= avail_here;
1952 
1953 		base += buf->page_base;
1954 		ppages = buf->pages + (base >> PAGE_SHIFT);
1955 		base &= ~PAGE_MASK;
1956 		avail_page = min_t(unsigned int, PAGE_SIZE - base,
1957 					avail_here);
1958 		c = kmap(*ppages) + base;
1959 
1960 		while (avail_here) {
1961 			avail_here -= avail_page;
1962 			if (copied || avail_page < desc->elem_size) {
1963 				unsigned int l = min(avail_page,
1964 					desc->elem_size - copied);
1965 				if (!elem) {
1966 					elem = kmalloc(desc->elem_size,
1967 						       GFP_KERNEL);
1968 					err = -ENOMEM;
1969 					if (!elem)
1970 						goto out;
1971 				}
1972 				if (encode) {
1973 					if (!copied) {
1974 						err = desc->xcode(desc, elem);
1975 						if (err)
1976 							goto out;
1977 					}
1978 					memcpy(c, elem + copied, l);
1979 					copied += l;
1980 					if (copied == desc->elem_size)
1981 						copied = 0;
1982 				} else {
1983 					memcpy(elem + copied, c, l);
1984 					copied += l;
1985 					if (copied == desc->elem_size) {
1986 						err = desc->xcode(desc, elem);
1987 						if (err)
1988 							goto out;
1989 						copied = 0;
1990 					}
1991 				}
1992 				avail_page -= l;
1993 				c += l;
1994 			}
1995 			while (avail_page >= desc->elem_size) {
1996 				err = desc->xcode(desc, c);
1997 				if (err)
1998 					goto out;
1999 				c += desc->elem_size;
2000 				avail_page -= desc->elem_size;
2001 			}
2002 			if (avail_page) {
2003 				unsigned int l = min(avail_page,
2004 					    desc->elem_size - copied);
2005 				if (!elem) {
2006 					elem = kmalloc(desc->elem_size,
2007 						       GFP_KERNEL);
2008 					err = -ENOMEM;
2009 					if (!elem)
2010 						goto out;
2011 				}
2012 				if (encode) {
2013 					if (!copied) {
2014 						err = desc->xcode(desc, elem);
2015 						if (err)
2016 							goto out;
2017 					}
2018 					memcpy(c, elem + copied, l);
2019 					copied += l;
2020 					if (copied == desc->elem_size)
2021 						copied = 0;
2022 				} else {
2023 					memcpy(elem + copied, c, l);
2024 					copied += l;
2025 					if (copied == desc->elem_size) {
2026 						err = desc->xcode(desc, elem);
2027 						if (err)
2028 							goto out;
2029 						copied = 0;
2030 					}
2031 				}
2032 			}
2033 			if (avail_here) {
2034 				kunmap(*ppages);
2035 				ppages++;
2036 				c = kmap(*ppages);
2037 			}
2038 
2039 			avail_page = min(avail_here,
2040 				 (unsigned int) PAGE_SIZE);
2041 		}
2042 		base = buf->page_len;  /* align to start of tail */
2043 	}
2044 
2045 	/* process tail */
2046 	base -= buf->page_len;
2047 	if (todo) {
2048 		c = buf->tail->iov_base + base;
2049 		if (copied) {
2050 			unsigned int l = desc->elem_size - copied;
2051 
2052 			if (encode)
2053 				memcpy(c, elem + copied, l);
2054 			else {
2055 				memcpy(elem + copied, c, l);
2056 				err = desc->xcode(desc, elem);
2057 				if (err)
2058 					goto out;
2059 			}
2060 			todo -= l;
2061 			c += l;
2062 		}
2063 		while (todo) {
2064 			err = desc->xcode(desc, c);
2065 			if (err)
2066 				goto out;
2067 			c += desc->elem_size;
2068 			todo -= desc->elem_size;
2069 		}
2070 	}
2071 	err = 0;
2072 
2073 out:
2074 	kfree(elem);
2075 	if (ppages)
2076 		kunmap(*ppages);
2077 	return err;
2078 }
2079 
2080 int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base,
2081 		      struct xdr_array2_desc *desc)
2082 {
2083 	if (base >= buf->len)
2084 		return -EINVAL;
2085 
2086 	return xdr_xcode_array2(buf, base, desc, 0);
2087 }
2088 EXPORT_SYMBOL_GPL(xdr_decode_array2);
2089 
2090 int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base,
2091 		      struct xdr_array2_desc *desc)
2092 {
2093 	if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
2094 	    buf->head->iov_len + buf->page_len + buf->tail->iov_len)
2095 		return -EINVAL;
2096 
2097 	return xdr_xcode_array2(buf, base, desc, 1);
2098 }
2099 EXPORT_SYMBOL_GPL(xdr_encode_array2);
2100 
2101 int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset,
2102 		    unsigned int len,
2103 		    int (*actor)(struct scatterlist *, void *), void *data)
2104 {
2105 	int i, ret = 0;
2106 	unsigned int page_len, thislen, page_offset;
2107 	struct scatterlist      sg[1];
2108 
2109 	sg_init_table(sg, 1);
2110 
2111 	if (offset >= buf->head[0].iov_len) {
2112 		offset -= buf->head[0].iov_len;
2113 	} else {
2114 		thislen = buf->head[0].iov_len - offset;
2115 		if (thislen > len)
2116 			thislen = len;
2117 		sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
2118 		ret = actor(sg, data);
2119 		if (ret)
2120 			goto out;
2121 		offset = 0;
2122 		len -= thislen;
2123 	}
2124 	if (len == 0)
2125 		goto out;
2126 
2127 	if (offset >= buf->page_len) {
2128 		offset -= buf->page_len;
2129 	} else {
2130 		page_len = buf->page_len - offset;
2131 		if (page_len > len)
2132 			page_len = len;
2133 		len -= page_len;
2134 		page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
2135 		i = (offset + buf->page_base) >> PAGE_SHIFT;
2136 		thislen = PAGE_SIZE - page_offset;
2137 		do {
2138 			if (thislen > page_len)
2139 				thislen = page_len;
2140 			sg_set_page(sg, buf->pages[i], thislen, page_offset);
2141 			ret = actor(sg, data);
2142 			if (ret)
2143 				goto out;
2144 			page_len -= thislen;
2145 			i++;
2146 			page_offset = 0;
2147 			thislen = PAGE_SIZE;
2148 		} while (page_len != 0);
2149 		offset = 0;
2150 	}
2151 	if (len == 0)
2152 		goto out;
2153 	if (offset < buf->tail[0].iov_len) {
2154 		thislen = buf->tail[0].iov_len - offset;
2155 		if (thislen > len)
2156 			thislen = len;
2157 		sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
2158 		ret = actor(sg, data);
2159 		len -= thislen;
2160 	}
2161 	if (len != 0)
2162 		ret = -EINVAL;
2163 out:
2164 	return ret;
2165 }
2166 EXPORT_SYMBOL_GPL(xdr_process_buf);
2167 
2168 /**
2169  * xdr_stream_decode_opaque - Decode variable length opaque
2170  * @xdr: pointer to xdr_stream
2171  * @ptr: location to store opaque data
2172  * @size: size of storage buffer @ptr
2173  *
2174  * Return values:
2175  *   On success, returns size of object stored in *@ptr
2176  *   %-EBADMSG on XDR buffer overflow
2177  *   %-EMSGSIZE on overflow of storage buffer @ptr
2178  */
2179 ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, size_t size)
2180 {
2181 	ssize_t ret;
2182 	void *p;
2183 
2184 	ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
2185 	if (ret <= 0)
2186 		return ret;
2187 	memcpy(ptr, p, ret);
2188 	return ret;
2189 }
2190 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque);
2191 
2192 /**
2193  * xdr_stream_decode_opaque_dup - Decode and duplicate variable length opaque
2194  * @xdr: pointer to xdr_stream
2195  * @ptr: location to store pointer to opaque data
2196  * @maxlen: maximum acceptable object size
2197  * @gfp_flags: GFP mask to use
2198  *
2199  * Return values:
2200  *   On success, returns size of object stored in *@ptr
2201  *   %-EBADMSG on XDR buffer overflow
2202  *   %-EMSGSIZE if the size of the object would exceed @maxlen
2203  *   %-ENOMEM on memory allocation failure
2204  */
2205 ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
2206 		size_t maxlen, gfp_t gfp_flags)
2207 {
2208 	ssize_t ret;
2209 	void *p;
2210 
2211 	ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2212 	if (ret > 0) {
2213 		*ptr = kmemdup(p, ret, gfp_flags);
2214 		if (*ptr != NULL)
2215 			return ret;
2216 		ret = -ENOMEM;
2217 	}
2218 	*ptr = NULL;
2219 	return ret;
2220 }
2221 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_dup);
2222 
2223 /**
2224  * xdr_stream_decode_string - Decode variable length string
2225  * @xdr: pointer to xdr_stream
2226  * @str: location to store string
2227  * @size: size of storage buffer @str
2228  *
2229  * Return values:
2230  *   On success, returns length of NUL-terminated string stored in *@str
2231  *   %-EBADMSG on XDR buffer overflow
2232  *   %-EMSGSIZE on overflow of storage buffer @str
2233  */
2234 ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, size_t size)
2235 {
2236 	ssize_t ret;
2237 	void *p;
2238 
2239 	ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
2240 	if (ret > 0) {
2241 		memcpy(str, p, ret);
2242 		str[ret] = '\0';
2243 		return strlen(str);
2244 	}
2245 	*str = '\0';
2246 	return ret;
2247 }
2248 EXPORT_SYMBOL_GPL(xdr_stream_decode_string);
2249 
2250 /**
2251  * xdr_stream_decode_string_dup - Decode and duplicate variable length string
2252  * @xdr: pointer to xdr_stream
2253  * @str: location to store pointer to string
2254  * @maxlen: maximum acceptable string length
2255  * @gfp_flags: GFP mask to use
2256  *
2257  * Return values:
2258  *   On success, returns length of NUL-terminated string stored in *@ptr
2259  *   %-EBADMSG on XDR buffer overflow
2260  *   %-EMSGSIZE if the size of the string would exceed @maxlen
2261  *   %-ENOMEM on memory allocation failure
2262  */
2263 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
2264 		size_t maxlen, gfp_t gfp_flags)
2265 {
2266 	void *p;
2267 	ssize_t ret;
2268 
2269 	ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2270 	if (ret > 0) {
2271 		char *s = kmemdup_nul(p, ret, gfp_flags);
2272 		if (s != NULL) {
2273 			*str = s;
2274 			return strlen(s);
2275 		}
2276 		ret = -ENOMEM;
2277 	}
2278 	*str = NULL;
2279 	return ret;
2280 }
2281 EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);
2282 
2283 /**
2284  * xdr_stream_decode_opaque_auth - Decode struct opaque_auth (RFC5531 S8.2)
2285  * @xdr: pointer to xdr_stream
2286  * @flavor: location to store decoded flavor
2287  * @body: location to store decode body
2288  * @body_len: location to store length of decoded body
2289  *
2290  * Return values:
2291  *   On success, returns the number of buffer bytes consumed
2292  *   %-EBADMSG on XDR buffer overflow
2293  *   %-EMSGSIZE if the decoded size of the body field exceeds 400 octets
2294  */
2295 ssize_t xdr_stream_decode_opaque_auth(struct xdr_stream *xdr, u32 *flavor,
2296 				      void **body, unsigned int *body_len)
2297 {
2298 	ssize_t ret, len;
2299 
2300 	len = xdr_stream_decode_u32(xdr, flavor);
2301 	if (unlikely(len < 0))
2302 		return len;
2303 	ret = xdr_stream_decode_opaque_inline(xdr, body, RPC_MAX_AUTH_SIZE);
2304 	if (unlikely(ret < 0))
2305 		return ret;
2306 	*body_len = ret;
2307 	return len + ret;
2308 }
2309 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_auth);
2310 
2311 /**
2312  * xdr_stream_encode_opaque_auth - Encode struct opaque_auth (RFC5531 S8.2)
2313  * @xdr: pointer to xdr_stream
2314  * @flavor: verifier flavor to encode
2315  * @body: content of body to encode
2316  * @body_len: length of body to encode
2317  *
2318  * Return values:
2319  *   On success, returns length in bytes of XDR buffer consumed
2320  *   %-EBADMSG on XDR buffer overflow
2321  *   %-EMSGSIZE if the size of @body exceeds 400 octets
2322  */
2323 ssize_t xdr_stream_encode_opaque_auth(struct xdr_stream *xdr, u32 flavor,
2324 				      void *body, unsigned int body_len)
2325 {
2326 	ssize_t ret, len;
2327 
2328 	if (unlikely(body_len > RPC_MAX_AUTH_SIZE))
2329 		return -EMSGSIZE;
2330 	len = xdr_stream_encode_u32(xdr, flavor);
2331 	if (unlikely(len < 0))
2332 		return len;
2333 	ret = xdr_stream_encode_opaque(xdr, body, body_len);
2334 	if (unlikely(ret < 0))
2335 		return ret;
2336 	return len + ret;
2337 }
2338 EXPORT_SYMBOL_GPL(xdr_stream_encode_opaque_auth);
2339