xref: /openbmc/linux/net/sunrpc/xdr.c (revision aaf9128a)
1 /*
2  * linux/net/sunrpc/xdr.c
3  *
4  * Generic XDR support.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/types.h>
12 #include <linux/string.h>
13 #include <linux/kernel.h>
14 #include <linux/pagemap.h>
15 #include <linux/errno.h>
16 #include <linux/sunrpc/xdr.h>
17 #include <linux/sunrpc/msg_prot.h>
18 #include <linux/bvec.h>
19 
20 /*
21  * XDR functions for basic NFS types
22  */
23 __be32 *
24 xdr_encode_netobj(__be32 *p, const struct xdr_netobj *obj)
25 {
26 	unsigned int	quadlen = XDR_QUADLEN(obj->len);
27 
28 	p[quadlen] = 0;		/* zero trailing bytes */
29 	*p++ = cpu_to_be32(obj->len);
30 	memcpy(p, obj->data, obj->len);
31 	return p + XDR_QUADLEN(obj->len);
32 }
33 EXPORT_SYMBOL_GPL(xdr_encode_netobj);
34 
35 __be32 *
36 xdr_decode_netobj(__be32 *p, struct xdr_netobj *obj)
37 {
38 	unsigned int	len;
39 
40 	if ((len = be32_to_cpu(*p++)) > XDR_MAX_NETOBJ)
41 		return NULL;
42 	obj->len  = len;
43 	obj->data = (u8 *) p;
44 	return p + XDR_QUADLEN(len);
45 }
46 EXPORT_SYMBOL_GPL(xdr_decode_netobj);
47 
48 /**
49  * xdr_encode_opaque_fixed - Encode fixed length opaque data
50  * @p: pointer to current position in XDR buffer.
51  * @ptr: pointer to data to encode (or NULL)
52  * @nbytes: size of data.
53  *
54  * Copy the array of data of length nbytes at ptr to the XDR buffer
55  * at position p, then align to the next 32-bit boundary by padding
56  * with zero bytes (see RFC1832).
57  * Note: if ptr is NULL, only the padding is performed.
58  *
59  * Returns the updated current XDR buffer position
60  *
61  */
62 __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int nbytes)
63 {
64 	if (likely(nbytes != 0)) {
65 		unsigned int quadlen = XDR_QUADLEN(nbytes);
66 		unsigned int padding = (quadlen << 2) - nbytes;
67 
68 		if (ptr != NULL)
69 			memcpy(p, ptr, nbytes);
70 		if (padding != 0)
71 			memset((char *)p + nbytes, 0, padding);
72 		p += quadlen;
73 	}
74 	return p;
75 }
76 EXPORT_SYMBOL_GPL(xdr_encode_opaque_fixed);
77 
78 /**
79  * xdr_encode_opaque - Encode variable length opaque data
80  * @p: pointer to current position in XDR buffer.
81  * @ptr: pointer to data to encode (or NULL)
82  * @nbytes: size of data.
83  *
84  * Returns the updated current XDR buffer position
85  */
86 __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int nbytes)
87 {
88 	*p++ = cpu_to_be32(nbytes);
89 	return xdr_encode_opaque_fixed(p, ptr, nbytes);
90 }
91 EXPORT_SYMBOL_GPL(xdr_encode_opaque);
92 
93 __be32 *
94 xdr_encode_string(__be32 *p, const char *string)
95 {
96 	return xdr_encode_array(p, string, strlen(string));
97 }
98 EXPORT_SYMBOL_GPL(xdr_encode_string);
99 
100 __be32 *
101 xdr_decode_string_inplace(__be32 *p, char **sp,
102 			  unsigned int *lenp, unsigned int maxlen)
103 {
104 	u32 len;
105 
106 	len = be32_to_cpu(*p++);
107 	if (len > maxlen)
108 		return NULL;
109 	*lenp = len;
110 	*sp = (char *) p;
111 	return p + XDR_QUADLEN(len);
112 }
113 EXPORT_SYMBOL_GPL(xdr_decode_string_inplace);
114 
115 /**
116  * xdr_terminate_string - '\0'-terminate a string residing in an xdr_buf
117  * @buf: XDR buffer where string resides
118  * @len: length of string, in bytes
119  *
120  */
121 void
122 xdr_terminate_string(struct xdr_buf *buf, const u32 len)
123 {
124 	char *kaddr;
125 
126 	kaddr = kmap_atomic(buf->pages[0]);
127 	kaddr[buf->page_base + len] = '\0';
128 	kunmap_atomic(kaddr);
129 }
130 EXPORT_SYMBOL_GPL(xdr_terminate_string);
131 
132 size_t
133 xdr_buf_pagecount(struct xdr_buf *buf)
134 {
135 	if (!buf->page_len)
136 		return 0;
137 	return (buf->page_base + buf->page_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
138 }
139 
140 int
141 xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp)
142 {
143 	size_t i, n = xdr_buf_pagecount(buf);
144 
145 	if (n != 0 && buf->bvec == NULL) {
146 		buf->bvec = kmalloc_array(n, sizeof(buf->bvec[0]), gfp);
147 		if (!buf->bvec)
148 			return -ENOMEM;
149 		for (i = 0; i < n; i++) {
150 			buf->bvec[i].bv_page = buf->pages[i];
151 			buf->bvec[i].bv_len = PAGE_SIZE;
152 			buf->bvec[i].bv_offset = 0;
153 		}
154 	}
155 	return 0;
156 }
157 
158 void
159 xdr_free_bvec(struct xdr_buf *buf)
160 {
161 	kfree(buf->bvec);
162 	buf->bvec = NULL;
163 }
164 
165 void
166 xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
167 		 struct page **pages, unsigned int base, unsigned int len)
168 {
169 	struct kvec *head = xdr->head;
170 	struct kvec *tail = xdr->tail;
171 	char *buf = (char *)head->iov_base;
172 	unsigned int buflen = head->iov_len;
173 
174 	head->iov_len  = offset;
175 
176 	xdr->pages = pages;
177 	xdr->page_base = base;
178 	xdr->page_len = len;
179 
180 	tail->iov_base = buf + offset;
181 	tail->iov_len = buflen - offset;
182 
183 	xdr->buflen += len;
184 }
185 EXPORT_SYMBOL_GPL(xdr_inline_pages);
186 
187 /*
188  * Helper routines for doing 'memmove' like operations on a struct xdr_buf
189  */
190 
191 /**
192  * _shift_data_right_pages
193  * @pages: vector of pages containing both the source and dest memory area.
194  * @pgto_base: page vector address of destination
195  * @pgfrom_base: page vector address of source
196  * @len: number of bytes to copy
197  *
198  * Note: the addresses pgto_base and pgfrom_base are both calculated in
199  *       the same way:
200  *            if a memory area starts at byte 'base' in page 'pages[i]',
201  *            then its address is given as (i << PAGE_SHIFT) + base
202  * Also note: pgfrom_base must be < pgto_base, but the memory areas
203  * 	they point to may overlap.
204  */
205 static void
206 _shift_data_right_pages(struct page **pages, size_t pgto_base,
207 		size_t pgfrom_base, size_t len)
208 {
209 	struct page **pgfrom, **pgto;
210 	char *vfrom, *vto;
211 	size_t copy;
212 
213 	BUG_ON(pgto_base <= pgfrom_base);
214 
215 	pgto_base += len;
216 	pgfrom_base += len;
217 
218 	pgto = pages + (pgto_base >> PAGE_SHIFT);
219 	pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
220 
221 	pgto_base &= ~PAGE_MASK;
222 	pgfrom_base &= ~PAGE_MASK;
223 
224 	do {
225 		/* Are any pointers crossing a page boundary? */
226 		if (pgto_base == 0) {
227 			pgto_base = PAGE_SIZE;
228 			pgto--;
229 		}
230 		if (pgfrom_base == 0) {
231 			pgfrom_base = PAGE_SIZE;
232 			pgfrom--;
233 		}
234 
235 		copy = len;
236 		if (copy > pgto_base)
237 			copy = pgto_base;
238 		if (copy > pgfrom_base)
239 			copy = pgfrom_base;
240 		pgto_base -= copy;
241 		pgfrom_base -= copy;
242 
243 		vto = kmap_atomic(*pgto);
244 		if (*pgto != *pgfrom) {
245 			vfrom = kmap_atomic(*pgfrom);
246 			memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
247 			kunmap_atomic(vfrom);
248 		} else
249 			memmove(vto + pgto_base, vto + pgfrom_base, copy);
250 		flush_dcache_page(*pgto);
251 		kunmap_atomic(vto);
252 
253 	} while ((len -= copy) != 0);
254 }
255 
256 /**
257  * _copy_to_pages
258  * @pages: array of pages
259  * @pgbase: page vector address of destination
260  * @p: pointer to source data
261  * @len: length
262  *
263  * Copies data from an arbitrary memory location into an array of pages
264  * The copy is assumed to be non-overlapping.
265  */
266 static void
267 _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
268 {
269 	struct page **pgto;
270 	char *vto;
271 	size_t copy;
272 
273 	pgto = pages + (pgbase >> PAGE_SHIFT);
274 	pgbase &= ~PAGE_MASK;
275 
276 	for (;;) {
277 		copy = PAGE_SIZE - pgbase;
278 		if (copy > len)
279 			copy = len;
280 
281 		vto = kmap_atomic(*pgto);
282 		memcpy(vto + pgbase, p, copy);
283 		kunmap_atomic(vto);
284 
285 		len -= copy;
286 		if (len == 0)
287 			break;
288 
289 		pgbase += copy;
290 		if (pgbase == PAGE_SIZE) {
291 			flush_dcache_page(*pgto);
292 			pgbase = 0;
293 			pgto++;
294 		}
295 		p += copy;
296 	}
297 	flush_dcache_page(*pgto);
298 }
299 
300 /**
301  * _copy_from_pages
302  * @p: pointer to destination
303  * @pages: array of pages
304  * @pgbase: offset of source data
305  * @len: length
306  *
307  * Copies data into an arbitrary memory location from an array of pages
308  * The copy is assumed to be non-overlapping.
309  */
310 void
311 _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
312 {
313 	struct page **pgfrom;
314 	char *vfrom;
315 	size_t copy;
316 
317 	pgfrom = pages + (pgbase >> PAGE_SHIFT);
318 	pgbase &= ~PAGE_MASK;
319 
320 	do {
321 		copy = PAGE_SIZE - pgbase;
322 		if (copy > len)
323 			copy = len;
324 
325 		vfrom = kmap_atomic(*pgfrom);
326 		memcpy(p, vfrom + pgbase, copy);
327 		kunmap_atomic(vfrom);
328 
329 		pgbase += copy;
330 		if (pgbase == PAGE_SIZE) {
331 			pgbase = 0;
332 			pgfrom++;
333 		}
334 		p += copy;
335 
336 	} while ((len -= copy) != 0);
337 }
338 EXPORT_SYMBOL_GPL(_copy_from_pages);
339 
340 /**
341  * xdr_shrink_bufhead
342  * @buf: xdr_buf
343  * @len: bytes to remove from buf->head[0]
344  *
345  * Shrinks XDR buffer's header kvec buf->head[0] by
346  * 'len' bytes. The extra data is not lost, but is instead
347  * moved into the inlined pages and/or the tail.
348  */
349 static void
350 xdr_shrink_bufhead(struct xdr_buf *buf, size_t len)
351 {
352 	struct kvec *head, *tail;
353 	size_t copy, offs;
354 	unsigned int pglen = buf->page_len;
355 
356 	tail = buf->tail;
357 	head = buf->head;
358 
359 	WARN_ON_ONCE(len > head->iov_len);
360 	if (len > head->iov_len)
361 		len = head->iov_len;
362 
363 	/* Shift the tail first */
364 	if (tail->iov_len != 0) {
365 		if (tail->iov_len > len) {
366 			copy = tail->iov_len - len;
367 			memmove((char *)tail->iov_base + len,
368 					tail->iov_base, copy);
369 		}
370 		/* Copy from the inlined pages into the tail */
371 		copy = len;
372 		if (copy > pglen)
373 			copy = pglen;
374 		offs = len - copy;
375 		if (offs >= tail->iov_len)
376 			copy = 0;
377 		else if (copy > tail->iov_len - offs)
378 			copy = tail->iov_len - offs;
379 		if (copy != 0)
380 			_copy_from_pages((char *)tail->iov_base + offs,
381 					buf->pages,
382 					buf->page_base + pglen + offs - len,
383 					copy);
384 		/* Do we also need to copy data from the head into the tail ? */
385 		if (len > pglen) {
386 			offs = copy = len - pglen;
387 			if (copy > tail->iov_len)
388 				copy = tail->iov_len;
389 			memcpy(tail->iov_base,
390 					(char *)head->iov_base +
391 					head->iov_len - offs,
392 					copy);
393 		}
394 	}
395 	/* Now handle pages */
396 	if (pglen != 0) {
397 		if (pglen > len)
398 			_shift_data_right_pages(buf->pages,
399 					buf->page_base + len,
400 					buf->page_base,
401 					pglen - len);
402 		copy = len;
403 		if (len > pglen)
404 			copy = pglen;
405 		_copy_to_pages(buf->pages, buf->page_base,
406 				(char *)head->iov_base + head->iov_len - len,
407 				copy);
408 	}
409 	head->iov_len -= len;
410 	buf->buflen -= len;
411 	/* Have we truncated the message? */
412 	if (buf->len > buf->buflen)
413 		buf->len = buf->buflen;
414 }
415 
416 /**
417  * xdr_shrink_pagelen
418  * @buf: xdr_buf
419  * @len: bytes to remove from buf->pages
420  *
421  * Shrinks XDR buffer's page array buf->pages by
422  * 'len' bytes. The extra data is not lost, but is instead
423  * moved into the tail.
424  */
425 static void
426 xdr_shrink_pagelen(struct xdr_buf *buf, size_t len)
427 {
428 	struct kvec *tail;
429 	size_t copy;
430 	unsigned int pglen = buf->page_len;
431 	unsigned int tailbuf_len;
432 
433 	tail = buf->tail;
434 	BUG_ON (len > pglen);
435 
436 	tailbuf_len = buf->buflen - buf->head->iov_len - buf->page_len;
437 
438 	/* Shift the tail first */
439 	if (tailbuf_len != 0) {
440 		unsigned int free_space = tailbuf_len - tail->iov_len;
441 
442 		if (len < free_space)
443 			free_space = len;
444 		tail->iov_len += free_space;
445 
446 		copy = len;
447 		if (tail->iov_len > len) {
448 			char *p = (char *)tail->iov_base + len;
449 			memmove(p, tail->iov_base, tail->iov_len - len);
450 		} else
451 			copy = tail->iov_len;
452 		/* Copy from the inlined pages into the tail */
453 		_copy_from_pages((char *)tail->iov_base,
454 				buf->pages, buf->page_base + pglen - len,
455 				copy);
456 	}
457 	buf->page_len -= len;
458 	buf->buflen -= len;
459 	/* Have we truncated the message? */
460 	if (buf->len > buf->buflen)
461 		buf->len = buf->buflen;
462 }
463 
464 void
465 xdr_shift_buf(struct xdr_buf *buf, size_t len)
466 {
467 	xdr_shrink_bufhead(buf, len);
468 }
469 EXPORT_SYMBOL_GPL(xdr_shift_buf);
470 
471 /**
472  * xdr_stream_pos - Return the current offset from the start of the xdr_stream
473  * @xdr: pointer to struct xdr_stream
474  */
475 unsigned int xdr_stream_pos(const struct xdr_stream *xdr)
476 {
477 	return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2;
478 }
479 EXPORT_SYMBOL_GPL(xdr_stream_pos);
480 
481 /**
482  * xdr_init_encode - Initialize a struct xdr_stream for sending data.
483  * @xdr: pointer to xdr_stream struct
484  * @buf: pointer to XDR buffer in which to encode data
485  * @p: current pointer inside XDR buffer
486  *
487  * Note: at the moment the RPC client only passes the length of our
488  *	 scratch buffer in the xdr_buf's header kvec. Previously this
489  *	 meant we needed to call xdr_adjust_iovec() after encoding the
490  *	 data. With the new scheme, the xdr_stream manages the details
491  *	 of the buffer length, and takes care of adjusting the kvec
492  *	 length for us.
493  */
494 void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
495 {
496 	struct kvec *iov = buf->head;
497 	int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
498 
499 	xdr_set_scratch_buffer(xdr, NULL, 0);
500 	BUG_ON(scratch_len < 0);
501 	xdr->buf = buf;
502 	xdr->iov = iov;
503 	xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
504 	xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
505 	BUG_ON(iov->iov_len > scratch_len);
506 
507 	if (p != xdr->p && p != NULL) {
508 		size_t len;
509 
510 		BUG_ON(p < xdr->p || p > xdr->end);
511 		len = (char *)p - (char *)xdr->p;
512 		xdr->p = p;
513 		buf->len += len;
514 		iov->iov_len += len;
515 	}
516 }
517 EXPORT_SYMBOL_GPL(xdr_init_encode);
518 
519 /**
520  * xdr_commit_encode - Ensure all data is written to buffer
521  * @xdr: pointer to xdr_stream
522  *
523  * We handle encoding across page boundaries by giving the caller a
524  * temporary location to write to, then later copying the data into
525  * place; xdr_commit_encode does that copying.
526  *
527  * Normally the caller doesn't need to call this directly, as the
528  * following xdr_reserve_space will do it.  But an explicit call may be
529  * required at the end of encoding, or any other time when the xdr_buf
530  * data might be read.
531  */
532 void xdr_commit_encode(struct xdr_stream *xdr)
533 {
534 	int shift = xdr->scratch.iov_len;
535 	void *page;
536 
537 	if (shift == 0)
538 		return;
539 	page = page_address(*xdr->page_ptr);
540 	memcpy(xdr->scratch.iov_base, page, shift);
541 	memmove(page, page + shift, (void *)xdr->p - page);
542 	xdr->scratch.iov_len = 0;
543 }
544 EXPORT_SYMBOL_GPL(xdr_commit_encode);
545 
546 static __be32 *xdr_get_next_encode_buffer(struct xdr_stream *xdr,
547 		size_t nbytes)
548 {
549 	__be32 *p;
550 	int space_left;
551 	int frag1bytes, frag2bytes;
552 
553 	if (nbytes > PAGE_SIZE)
554 		return NULL; /* Bigger buffers require special handling */
555 	if (xdr->buf->len + nbytes > xdr->buf->buflen)
556 		return NULL; /* Sorry, we're totally out of space */
557 	frag1bytes = (xdr->end - xdr->p) << 2;
558 	frag2bytes = nbytes - frag1bytes;
559 	if (xdr->iov)
560 		xdr->iov->iov_len += frag1bytes;
561 	else
562 		xdr->buf->page_len += frag1bytes;
563 	xdr->page_ptr++;
564 	xdr->iov = NULL;
565 	/*
566 	 * If the last encode didn't end exactly on a page boundary, the
567 	 * next one will straddle boundaries.  Encode into the next
568 	 * page, then copy it back later in xdr_commit_encode.  We use
569 	 * the "scratch" iov to track any temporarily unused fragment of
570 	 * space at the end of the previous buffer:
571 	 */
572 	xdr->scratch.iov_base = xdr->p;
573 	xdr->scratch.iov_len = frag1bytes;
574 	p = page_address(*xdr->page_ptr);
575 	/*
576 	 * Note this is where the next encode will start after we've
577 	 * shifted this one back:
578 	 */
579 	xdr->p = (void *)p + frag2bytes;
580 	space_left = xdr->buf->buflen - xdr->buf->len;
581 	xdr->end = (void *)p + min_t(int, space_left, PAGE_SIZE);
582 	xdr->buf->page_len += frag2bytes;
583 	xdr->buf->len += nbytes;
584 	return p;
585 }
586 
587 /**
588  * xdr_reserve_space - Reserve buffer space for sending
589  * @xdr: pointer to xdr_stream
590  * @nbytes: number of bytes to reserve
591  *
592  * Checks that we have enough buffer space to encode 'nbytes' more
593  * bytes of data. If so, update the total xdr_buf length, and
594  * adjust the length of the current kvec.
595  */
596 __be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
597 {
598 	__be32 *p = xdr->p;
599 	__be32 *q;
600 
601 	xdr_commit_encode(xdr);
602 	/* align nbytes on the next 32-bit boundary */
603 	nbytes += 3;
604 	nbytes &= ~3;
605 	q = p + (nbytes >> 2);
606 	if (unlikely(q > xdr->end || q < p))
607 		return xdr_get_next_encode_buffer(xdr, nbytes);
608 	xdr->p = q;
609 	if (xdr->iov)
610 		xdr->iov->iov_len += nbytes;
611 	else
612 		xdr->buf->page_len += nbytes;
613 	xdr->buf->len += nbytes;
614 	return p;
615 }
616 EXPORT_SYMBOL_GPL(xdr_reserve_space);
617 
618 /**
619  * xdr_truncate_encode - truncate an encode buffer
620  * @xdr: pointer to xdr_stream
621  * @len: new length of buffer
622  *
623  * Truncates the xdr stream, so that xdr->buf->len == len,
624  * and xdr->p points at offset len from the start of the buffer, and
625  * head, tail, and page lengths are adjusted to correspond.
626  *
627  * If this means moving xdr->p to a different buffer, we assume that
628  * that the end pointer should be set to the end of the current page,
629  * except in the case of the head buffer when we assume the head
630  * buffer's current length represents the end of the available buffer.
631  *
632  * This is *not* safe to use on a buffer that already has inlined page
633  * cache pages (as in a zero-copy server read reply), except for the
634  * simple case of truncating from one position in the tail to another.
635  *
636  */
637 void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
638 {
639 	struct xdr_buf *buf = xdr->buf;
640 	struct kvec *head = buf->head;
641 	struct kvec *tail = buf->tail;
642 	int fraglen;
643 	int new;
644 
645 	if (len > buf->len) {
646 		WARN_ON_ONCE(1);
647 		return;
648 	}
649 	xdr_commit_encode(xdr);
650 
651 	fraglen = min_t(int, buf->len - len, tail->iov_len);
652 	tail->iov_len -= fraglen;
653 	buf->len -= fraglen;
654 	if (tail->iov_len) {
655 		xdr->p = tail->iov_base + tail->iov_len;
656 		WARN_ON_ONCE(!xdr->end);
657 		WARN_ON_ONCE(!xdr->iov);
658 		return;
659 	}
660 	WARN_ON_ONCE(fraglen);
661 	fraglen = min_t(int, buf->len - len, buf->page_len);
662 	buf->page_len -= fraglen;
663 	buf->len -= fraglen;
664 
665 	new = buf->page_base + buf->page_len;
666 
667 	xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
668 
669 	if (buf->page_len) {
670 		xdr->p = page_address(*xdr->page_ptr);
671 		xdr->end = (void *)xdr->p + PAGE_SIZE;
672 		xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
673 		WARN_ON_ONCE(xdr->iov);
674 		return;
675 	}
676 	if (fraglen)
677 		xdr->end = head->iov_base + head->iov_len;
678 	/* (otherwise assume xdr->end is already set) */
679 	xdr->page_ptr--;
680 	head->iov_len = len;
681 	buf->len = len;
682 	xdr->p = head->iov_base + head->iov_len;
683 	xdr->iov = buf->head;
684 }
685 EXPORT_SYMBOL(xdr_truncate_encode);
686 
687 /**
688  * xdr_restrict_buflen - decrease available buffer space
689  * @xdr: pointer to xdr_stream
690  * @newbuflen: new maximum number of bytes available
691  *
692  * Adjust our idea of how much space is available in the buffer.
693  * If we've already used too much space in the buffer, returns -1.
694  * If the available space is already smaller than newbuflen, returns 0
695  * and does nothing.  Otherwise, adjusts xdr->buf->buflen to newbuflen
696  * and ensures xdr->end is set at most offset newbuflen from the start
697  * of the buffer.
698  */
699 int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
700 {
701 	struct xdr_buf *buf = xdr->buf;
702 	int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
703 	int end_offset = buf->len + left_in_this_buf;
704 
705 	if (newbuflen < 0 || newbuflen < buf->len)
706 		return -1;
707 	if (newbuflen > buf->buflen)
708 		return 0;
709 	if (newbuflen < end_offset)
710 		xdr->end = (void *)xdr->end + newbuflen - end_offset;
711 	buf->buflen = newbuflen;
712 	return 0;
713 }
714 EXPORT_SYMBOL(xdr_restrict_buflen);
715 
716 /**
717  * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
718  * @xdr: pointer to xdr_stream
719  * @pages: list of pages
720  * @base: offset of first byte
721  * @len: length of data in bytes
722  *
723  */
724 void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
725 		 unsigned int len)
726 {
727 	struct xdr_buf *buf = xdr->buf;
728 	struct kvec *iov = buf->tail;
729 	buf->pages = pages;
730 	buf->page_base = base;
731 	buf->page_len = len;
732 
733 	iov->iov_base = (char *)xdr->p;
734 	iov->iov_len  = 0;
735 	xdr->iov = iov;
736 
737 	if (len & 3) {
738 		unsigned int pad = 4 - (len & 3);
739 
740 		BUG_ON(xdr->p >= xdr->end);
741 		iov->iov_base = (char *)xdr->p + (len & 3);
742 		iov->iov_len  += pad;
743 		len += pad;
744 		*xdr->p++ = 0;
745 	}
746 	buf->buflen += len;
747 	buf->len += len;
748 }
749 EXPORT_SYMBOL_GPL(xdr_write_pages);
750 
751 static void xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
752 		unsigned int len)
753 {
754 	if (len > iov->iov_len)
755 		len = iov->iov_len;
756 	xdr->p = (__be32*)iov->iov_base;
757 	xdr->end = (__be32*)(iov->iov_base + len);
758 	xdr->iov = iov;
759 	xdr->page_ptr = NULL;
760 }
761 
762 static int xdr_set_page_base(struct xdr_stream *xdr,
763 		unsigned int base, unsigned int len)
764 {
765 	unsigned int pgnr;
766 	unsigned int maxlen;
767 	unsigned int pgoff;
768 	unsigned int pgend;
769 	void *kaddr;
770 
771 	maxlen = xdr->buf->page_len;
772 	if (base >= maxlen)
773 		return -EINVAL;
774 	maxlen -= base;
775 	if (len > maxlen)
776 		len = maxlen;
777 
778 	base += xdr->buf->page_base;
779 
780 	pgnr = base >> PAGE_SHIFT;
781 	xdr->page_ptr = &xdr->buf->pages[pgnr];
782 	kaddr = page_address(*xdr->page_ptr);
783 
784 	pgoff = base & ~PAGE_MASK;
785 	xdr->p = (__be32*)(kaddr + pgoff);
786 
787 	pgend = pgoff + len;
788 	if (pgend > PAGE_SIZE)
789 		pgend = PAGE_SIZE;
790 	xdr->end = (__be32*)(kaddr + pgend);
791 	xdr->iov = NULL;
792 	return 0;
793 }
794 
795 static void xdr_set_next_page(struct xdr_stream *xdr)
796 {
797 	unsigned int newbase;
798 
799 	newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
800 	newbase -= xdr->buf->page_base;
801 
802 	if (xdr_set_page_base(xdr, newbase, PAGE_SIZE) < 0)
803 		xdr_set_iov(xdr, xdr->buf->tail, xdr->nwords << 2);
804 }
805 
806 static bool xdr_set_next_buffer(struct xdr_stream *xdr)
807 {
808 	if (xdr->page_ptr != NULL)
809 		xdr_set_next_page(xdr);
810 	else if (xdr->iov == xdr->buf->head) {
811 		if (xdr_set_page_base(xdr, 0, PAGE_SIZE) < 0)
812 			xdr_set_iov(xdr, xdr->buf->tail, xdr->nwords << 2);
813 	}
814 	return xdr->p != xdr->end;
815 }
816 
817 /**
818  * xdr_init_decode - Initialize an xdr_stream for decoding data.
819  * @xdr: pointer to xdr_stream struct
820  * @buf: pointer to XDR buffer from which to decode data
821  * @p: current pointer inside XDR buffer
822  */
823 void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
824 {
825 	xdr->buf = buf;
826 	xdr->scratch.iov_base = NULL;
827 	xdr->scratch.iov_len = 0;
828 	xdr->nwords = XDR_QUADLEN(buf->len);
829 	if (buf->head[0].iov_len != 0)
830 		xdr_set_iov(xdr, buf->head, buf->len);
831 	else if (buf->page_len != 0)
832 		xdr_set_page_base(xdr, 0, buf->len);
833 	else
834 		xdr_set_iov(xdr, buf->head, buf->len);
835 	if (p != NULL && p > xdr->p && xdr->end >= p) {
836 		xdr->nwords -= p - xdr->p;
837 		xdr->p = p;
838 	}
839 }
840 EXPORT_SYMBOL_GPL(xdr_init_decode);
841 
842 /**
843  * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages
844  * @xdr: pointer to xdr_stream struct
845  * @buf: pointer to XDR buffer from which to decode data
846  * @pages: list of pages to decode into
847  * @len: length in bytes of buffer in pages
848  */
849 void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
850 			   struct page **pages, unsigned int len)
851 {
852 	memset(buf, 0, sizeof(*buf));
853 	buf->pages =  pages;
854 	buf->page_len =  len;
855 	buf->buflen =  len;
856 	buf->len = len;
857 	xdr_init_decode(xdr, buf, NULL);
858 }
859 EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
860 
861 static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
862 {
863 	unsigned int nwords = XDR_QUADLEN(nbytes);
864 	__be32 *p = xdr->p;
865 	__be32 *q = p + nwords;
866 
867 	if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
868 		return NULL;
869 	xdr->p = q;
870 	xdr->nwords -= nwords;
871 	return p;
872 }
873 
874 /**
875  * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
876  * @xdr: pointer to xdr_stream struct
877  * @buf: pointer to an empty buffer
878  * @buflen: size of 'buf'
879  *
880  * The scratch buffer is used when decoding from an array of pages.
881  * If an xdr_inline_decode() call spans across page boundaries, then
882  * we copy the data into the scratch buffer in order to allow linear
883  * access.
884  */
885 void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
886 {
887 	xdr->scratch.iov_base = buf;
888 	xdr->scratch.iov_len = buflen;
889 }
890 EXPORT_SYMBOL_GPL(xdr_set_scratch_buffer);
891 
892 static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
893 {
894 	__be32 *p;
895 	char *cpdest = xdr->scratch.iov_base;
896 	size_t cplen = (char *)xdr->end - (char *)xdr->p;
897 
898 	if (nbytes > xdr->scratch.iov_len)
899 		return NULL;
900 	p = __xdr_inline_decode(xdr, cplen);
901 	if (p == NULL)
902 		return NULL;
903 	memcpy(cpdest, p, cplen);
904 	cpdest += cplen;
905 	nbytes -= cplen;
906 	if (!xdr_set_next_buffer(xdr))
907 		return NULL;
908 	p = __xdr_inline_decode(xdr, nbytes);
909 	if (p == NULL)
910 		return NULL;
911 	memcpy(cpdest, p, nbytes);
912 	return xdr->scratch.iov_base;
913 }
914 
915 /**
916  * xdr_inline_decode - Retrieve XDR data to decode
917  * @xdr: pointer to xdr_stream struct
918  * @nbytes: number of bytes of data to decode
919  *
920  * Check if the input buffer is long enough to enable us to decode
921  * 'nbytes' more bytes of data starting at the current position.
922  * If so return the current pointer, then update the current
923  * pointer position.
924  */
925 __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
926 {
927 	__be32 *p;
928 
929 	if (nbytes == 0)
930 		return xdr->p;
931 	if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
932 		return NULL;
933 	p = __xdr_inline_decode(xdr, nbytes);
934 	if (p != NULL)
935 		return p;
936 	return xdr_copy_to_scratch(xdr, nbytes);
937 }
938 EXPORT_SYMBOL_GPL(xdr_inline_decode);
939 
940 static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
941 {
942 	struct xdr_buf *buf = xdr->buf;
943 	struct kvec *iov;
944 	unsigned int nwords = XDR_QUADLEN(len);
945 	unsigned int cur = xdr_stream_pos(xdr);
946 
947 	if (xdr->nwords == 0)
948 		return 0;
949 	/* Realign pages to current pointer position */
950 	iov  = buf->head;
951 	if (iov->iov_len > cur) {
952 		xdr_shrink_bufhead(buf, iov->iov_len - cur);
953 		xdr->nwords = XDR_QUADLEN(buf->len - cur);
954 	}
955 
956 	if (nwords > xdr->nwords) {
957 		nwords = xdr->nwords;
958 		len = nwords << 2;
959 	}
960 	if (buf->page_len <= len)
961 		len = buf->page_len;
962 	else if (nwords < xdr->nwords) {
963 		/* Truncate page data and move it into the tail */
964 		xdr_shrink_pagelen(buf, buf->page_len - len);
965 		xdr->nwords = XDR_QUADLEN(buf->len - cur);
966 	}
967 	return len;
968 }
969 
970 /**
971  * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
972  * @xdr: pointer to xdr_stream struct
973  * @len: number of bytes of page data
974  *
975  * Moves data beyond the current pointer position from the XDR head[] buffer
976  * into the page list. Any data that lies beyond current position + "len"
977  * bytes is moved into the XDR tail[].
978  *
979  * Returns the number of XDR encoded bytes now contained in the pages
980  */
981 unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
982 {
983 	struct xdr_buf *buf = xdr->buf;
984 	struct kvec *iov;
985 	unsigned int nwords;
986 	unsigned int end;
987 	unsigned int padding;
988 
989 	len = xdr_align_pages(xdr, len);
990 	if (len == 0)
991 		return 0;
992 	nwords = XDR_QUADLEN(len);
993 	padding = (nwords << 2) - len;
994 	xdr->iov = iov = buf->tail;
995 	/* Compute remaining message length.  */
996 	end = ((xdr->nwords - nwords) << 2) + padding;
997 	if (end > iov->iov_len)
998 		end = iov->iov_len;
999 
1000 	/*
1001 	 * Position current pointer at beginning of tail, and
1002 	 * set remaining message length.
1003 	 */
1004 	xdr->p = (__be32 *)((char *)iov->iov_base + padding);
1005 	xdr->end = (__be32 *)((char *)iov->iov_base + end);
1006 	xdr->page_ptr = NULL;
1007 	xdr->nwords = XDR_QUADLEN(end - padding);
1008 	return len;
1009 }
1010 EXPORT_SYMBOL_GPL(xdr_read_pages);
1011 
1012 /**
1013  * xdr_enter_page - decode data from the XDR page
1014  * @xdr: pointer to xdr_stream struct
1015  * @len: number of bytes of page data
1016  *
1017  * Moves data beyond the current pointer position from the XDR head[] buffer
1018  * into the page list. Any data that lies beyond current position + "len"
1019  * bytes is moved into the XDR tail[]. The current pointer is then
1020  * repositioned at the beginning of the first XDR page.
1021  */
1022 void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
1023 {
1024 	len = xdr_align_pages(xdr, len);
1025 	/*
1026 	 * Position current pointer at beginning of tail, and
1027 	 * set remaining message length.
1028 	 */
1029 	if (len != 0)
1030 		xdr_set_page_base(xdr, 0, len);
1031 }
1032 EXPORT_SYMBOL_GPL(xdr_enter_page);
1033 
1034 static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
1035 
1036 void
1037 xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
1038 {
1039 	buf->head[0] = *iov;
1040 	buf->tail[0] = empty_iov;
1041 	buf->page_len = 0;
1042 	buf->buflen = buf->len = iov->iov_len;
1043 }
1044 EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
1045 
1046 /**
1047  * xdr_buf_subsegment - set subbuf to a portion of buf
1048  * @buf: an xdr buffer
1049  * @subbuf: the result buffer
1050  * @base: beginning of range in bytes
1051  * @len: length of range in bytes
1052  *
1053  * sets @subbuf to an xdr buffer representing the portion of @buf of
1054  * length @len starting at offset @base.
1055  *
1056  * @buf and @subbuf may be pointers to the same struct xdr_buf.
1057  *
1058  * Returns -1 if base of length are out of bounds.
1059  */
1060 int
1061 xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
1062 			unsigned int base, unsigned int len)
1063 {
1064 	subbuf->buflen = subbuf->len = len;
1065 	if (base < buf->head[0].iov_len) {
1066 		subbuf->head[0].iov_base = buf->head[0].iov_base + base;
1067 		subbuf->head[0].iov_len = min_t(unsigned int, len,
1068 						buf->head[0].iov_len - base);
1069 		len -= subbuf->head[0].iov_len;
1070 		base = 0;
1071 	} else {
1072 		base -= buf->head[0].iov_len;
1073 		subbuf->head[0].iov_len = 0;
1074 	}
1075 
1076 	if (base < buf->page_len) {
1077 		subbuf->page_len = min(buf->page_len - base, len);
1078 		base += buf->page_base;
1079 		subbuf->page_base = base & ~PAGE_MASK;
1080 		subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
1081 		len -= subbuf->page_len;
1082 		base = 0;
1083 	} else {
1084 		base -= buf->page_len;
1085 		subbuf->page_len = 0;
1086 	}
1087 
1088 	if (base < buf->tail[0].iov_len) {
1089 		subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
1090 		subbuf->tail[0].iov_len = min_t(unsigned int, len,
1091 						buf->tail[0].iov_len - base);
1092 		len -= subbuf->tail[0].iov_len;
1093 		base = 0;
1094 	} else {
1095 		base -= buf->tail[0].iov_len;
1096 		subbuf->tail[0].iov_len = 0;
1097 	}
1098 
1099 	if (base || len)
1100 		return -1;
1101 	return 0;
1102 }
1103 EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
1104 
1105 /**
1106  * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
1107  * @buf: buf to be trimmed
1108  * @len: number of bytes to reduce "buf" by
1109  *
1110  * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
1111  * that it's possible that we'll trim less than that amount if the xdr_buf is
1112  * too small, or if (for instance) it's all in the head and the parser has
1113  * already read too far into it.
1114  */
1115 void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
1116 {
1117 	size_t cur;
1118 	unsigned int trim = len;
1119 
1120 	if (buf->tail[0].iov_len) {
1121 		cur = min_t(size_t, buf->tail[0].iov_len, trim);
1122 		buf->tail[0].iov_len -= cur;
1123 		trim -= cur;
1124 		if (!trim)
1125 			goto fix_len;
1126 	}
1127 
1128 	if (buf->page_len) {
1129 		cur = min_t(unsigned int, buf->page_len, trim);
1130 		buf->page_len -= cur;
1131 		trim -= cur;
1132 		if (!trim)
1133 			goto fix_len;
1134 	}
1135 
1136 	if (buf->head[0].iov_len) {
1137 		cur = min_t(size_t, buf->head[0].iov_len, trim);
1138 		buf->head[0].iov_len -= cur;
1139 		trim -= cur;
1140 	}
1141 fix_len:
1142 	buf->len -= (len - trim);
1143 }
1144 EXPORT_SYMBOL_GPL(xdr_buf_trim);
1145 
1146 static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
1147 {
1148 	unsigned int this_len;
1149 
1150 	this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1151 	memcpy(obj, subbuf->head[0].iov_base, this_len);
1152 	len -= this_len;
1153 	obj += this_len;
1154 	this_len = min_t(unsigned int, len, subbuf->page_len);
1155 	if (this_len)
1156 		_copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
1157 	len -= this_len;
1158 	obj += this_len;
1159 	this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1160 	memcpy(obj, subbuf->tail[0].iov_base, this_len);
1161 }
1162 
1163 /* obj is assumed to point to allocated memory of size at least len: */
1164 int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
1165 {
1166 	struct xdr_buf subbuf;
1167 	int status;
1168 
1169 	status = xdr_buf_subsegment(buf, &subbuf, base, len);
1170 	if (status != 0)
1171 		return status;
1172 	__read_bytes_from_xdr_buf(&subbuf, obj, len);
1173 	return 0;
1174 }
1175 EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
1176 
1177 static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
1178 {
1179 	unsigned int this_len;
1180 
1181 	this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1182 	memcpy(subbuf->head[0].iov_base, obj, this_len);
1183 	len -= this_len;
1184 	obj += this_len;
1185 	this_len = min_t(unsigned int, len, subbuf->page_len);
1186 	if (this_len)
1187 		_copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
1188 	len -= this_len;
1189 	obj += this_len;
1190 	this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1191 	memcpy(subbuf->tail[0].iov_base, obj, this_len);
1192 }
1193 
1194 /* obj is assumed to point to allocated memory of size at least len: */
1195 int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
1196 {
1197 	struct xdr_buf subbuf;
1198 	int status;
1199 
1200 	status = xdr_buf_subsegment(buf, &subbuf, base, len);
1201 	if (status != 0)
1202 		return status;
1203 	__write_bytes_to_xdr_buf(&subbuf, obj, len);
1204 	return 0;
1205 }
1206 EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
1207 
1208 int
1209 xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
1210 {
1211 	__be32	raw;
1212 	int	status;
1213 
1214 	status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
1215 	if (status)
1216 		return status;
1217 	*obj = be32_to_cpu(raw);
1218 	return 0;
1219 }
1220 EXPORT_SYMBOL_GPL(xdr_decode_word);
1221 
1222 int
1223 xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
1224 {
1225 	__be32	raw = cpu_to_be32(obj);
1226 
1227 	return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
1228 }
1229 EXPORT_SYMBOL_GPL(xdr_encode_word);
1230 
1231 /* If the netobj starting offset bytes from the start of xdr_buf is contained
1232  * entirely in the head or the tail, set object to point to it; otherwise
1233  * try to find space for it at the end of the tail, copy it there, and
1234  * set obj to point to it. */
1235 int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
1236 {
1237 	struct xdr_buf subbuf;
1238 
1239 	if (xdr_decode_word(buf, offset, &obj->len))
1240 		return -EFAULT;
1241 	if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
1242 		return -EFAULT;
1243 
1244 	/* Is the obj contained entirely in the head? */
1245 	obj->data = subbuf.head[0].iov_base;
1246 	if (subbuf.head[0].iov_len == obj->len)
1247 		return 0;
1248 	/* ..or is the obj contained entirely in the tail? */
1249 	obj->data = subbuf.tail[0].iov_base;
1250 	if (subbuf.tail[0].iov_len == obj->len)
1251 		return 0;
1252 
1253 	/* use end of tail as storage for obj:
1254 	 * (We don't copy to the beginning because then we'd have
1255 	 * to worry about doing a potentially overlapping copy.
1256 	 * This assumes the object is at most half the length of the
1257 	 * tail.) */
1258 	if (obj->len > buf->buflen - buf->len)
1259 		return -ENOMEM;
1260 	if (buf->tail[0].iov_len != 0)
1261 		obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
1262 	else
1263 		obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
1264 	__read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
1265 	return 0;
1266 }
1267 EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
1268 
1269 /* Returns 0 on success, or else a negative error code. */
1270 static int
1271 xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
1272 		 struct xdr_array2_desc *desc, int encode)
1273 {
1274 	char *elem = NULL, *c;
1275 	unsigned int copied = 0, todo, avail_here;
1276 	struct page **ppages = NULL;
1277 	int err;
1278 
1279 	if (encode) {
1280 		if (xdr_encode_word(buf, base, desc->array_len) != 0)
1281 			return -EINVAL;
1282 	} else {
1283 		if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
1284 		    desc->array_len > desc->array_maxlen ||
1285 		    (unsigned long) base + 4 + desc->array_len *
1286 				    desc->elem_size > buf->len)
1287 			return -EINVAL;
1288 	}
1289 	base += 4;
1290 
1291 	if (!desc->xcode)
1292 		return 0;
1293 
1294 	todo = desc->array_len * desc->elem_size;
1295 
1296 	/* process head */
1297 	if (todo && base < buf->head->iov_len) {
1298 		c = buf->head->iov_base + base;
1299 		avail_here = min_t(unsigned int, todo,
1300 				   buf->head->iov_len - base);
1301 		todo -= avail_here;
1302 
1303 		while (avail_here >= desc->elem_size) {
1304 			err = desc->xcode(desc, c);
1305 			if (err)
1306 				goto out;
1307 			c += desc->elem_size;
1308 			avail_here -= desc->elem_size;
1309 		}
1310 		if (avail_here) {
1311 			if (!elem) {
1312 				elem = kmalloc(desc->elem_size, GFP_KERNEL);
1313 				err = -ENOMEM;
1314 				if (!elem)
1315 					goto out;
1316 			}
1317 			if (encode) {
1318 				err = desc->xcode(desc, elem);
1319 				if (err)
1320 					goto out;
1321 				memcpy(c, elem, avail_here);
1322 			} else
1323 				memcpy(elem, c, avail_here);
1324 			copied = avail_here;
1325 		}
1326 		base = buf->head->iov_len;  /* align to start of pages */
1327 	}
1328 
1329 	/* process pages array */
1330 	base -= buf->head->iov_len;
1331 	if (todo && base < buf->page_len) {
1332 		unsigned int avail_page;
1333 
1334 		avail_here = min(todo, buf->page_len - base);
1335 		todo -= avail_here;
1336 
1337 		base += buf->page_base;
1338 		ppages = buf->pages + (base >> PAGE_SHIFT);
1339 		base &= ~PAGE_MASK;
1340 		avail_page = min_t(unsigned int, PAGE_SIZE - base,
1341 					avail_here);
1342 		c = kmap(*ppages) + base;
1343 
1344 		while (avail_here) {
1345 			avail_here -= avail_page;
1346 			if (copied || avail_page < desc->elem_size) {
1347 				unsigned int l = min(avail_page,
1348 					desc->elem_size - copied);
1349 				if (!elem) {
1350 					elem = kmalloc(desc->elem_size,
1351 						       GFP_KERNEL);
1352 					err = -ENOMEM;
1353 					if (!elem)
1354 						goto out;
1355 				}
1356 				if (encode) {
1357 					if (!copied) {
1358 						err = desc->xcode(desc, elem);
1359 						if (err)
1360 							goto out;
1361 					}
1362 					memcpy(c, elem + copied, l);
1363 					copied += l;
1364 					if (copied == desc->elem_size)
1365 						copied = 0;
1366 				} else {
1367 					memcpy(elem + copied, c, l);
1368 					copied += l;
1369 					if (copied == desc->elem_size) {
1370 						err = desc->xcode(desc, elem);
1371 						if (err)
1372 							goto out;
1373 						copied = 0;
1374 					}
1375 				}
1376 				avail_page -= l;
1377 				c += l;
1378 			}
1379 			while (avail_page >= desc->elem_size) {
1380 				err = desc->xcode(desc, c);
1381 				if (err)
1382 					goto out;
1383 				c += desc->elem_size;
1384 				avail_page -= desc->elem_size;
1385 			}
1386 			if (avail_page) {
1387 				unsigned int l = min(avail_page,
1388 					    desc->elem_size - copied);
1389 				if (!elem) {
1390 					elem = kmalloc(desc->elem_size,
1391 						       GFP_KERNEL);
1392 					err = -ENOMEM;
1393 					if (!elem)
1394 						goto out;
1395 				}
1396 				if (encode) {
1397 					if (!copied) {
1398 						err = desc->xcode(desc, elem);
1399 						if (err)
1400 							goto out;
1401 					}
1402 					memcpy(c, elem + copied, l);
1403 					copied += l;
1404 					if (copied == desc->elem_size)
1405 						copied = 0;
1406 				} else {
1407 					memcpy(elem + copied, c, l);
1408 					copied += l;
1409 					if (copied == desc->elem_size) {
1410 						err = desc->xcode(desc, elem);
1411 						if (err)
1412 							goto out;
1413 						copied = 0;
1414 					}
1415 				}
1416 			}
1417 			if (avail_here) {
1418 				kunmap(*ppages);
1419 				ppages++;
1420 				c = kmap(*ppages);
1421 			}
1422 
1423 			avail_page = min(avail_here,
1424 				 (unsigned int) PAGE_SIZE);
1425 		}
1426 		base = buf->page_len;  /* align to start of tail */
1427 	}
1428 
1429 	/* process tail */
1430 	base -= buf->page_len;
1431 	if (todo) {
1432 		c = buf->tail->iov_base + base;
1433 		if (copied) {
1434 			unsigned int l = desc->elem_size - copied;
1435 
1436 			if (encode)
1437 				memcpy(c, elem + copied, l);
1438 			else {
1439 				memcpy(elem + copied, c, l);
1440 				err = desc->xcode(desc, elem);
1441 				if (err)
1442 					goto out;
1443 			}
1444 			todo -= l;
1445 			c += l;
1446 		}
1447 		while (todo) {
1448 			err = desc->xcode(desc, c);
1449 			if (err)
1450 				goto out;
1451 			c += desc->elem_size;
1452 			todo -= desc->elem_size;
1453 		}
1454 	}
1455 	err = 0;
1456 
1457 out:
1458 	kfree(elem);
1459 	if (ppages)
1460 		kunmap(*ppages);
1461 	return err;
1462 }
1463 
1464 int
1465 xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
1466 		  struct xdr_array2_desc *desc)
1467 {
1468 	if (base >= buf->len)
1469 		return -EINVAL;
1470 
1471 	return xdr_xcode_array2(buf, base, desc, 0);
1472 }
1473 EXPORT_SYMBOL_GPL(xdr_decode_array2);
1474 
1475 int
1476 xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
1477 		  struct xdr_array2_desc *desc)
1478 {
1479 	if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
1480 	    buf->head->iov_len + buf->page_len + buf->tail->iov_len)
1481 		return -EINVAL;
1482 
1483 	return xdr_xcode_array2(buf, base, desc, 1);
1484 }
1485 EXPORT_SYMBOL_GPL(xdr_encode_array2);
1486 
1487 int
1488 xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
1489 		int (*actor)(struct scatterlist *, void *), void *data)
1490 {
1491 	int i, ret = 0;
1492 	unsigned int page_len, thislen, page_offset;
1493 	struct scatterlist      sg[1];
1494 
1495 	sg_init_table(sg, 1);
1496 
1497 	if (offset >= buf->head[0].iov_len) {
1498 		offset -= buf->head[0].iov_len;
1499 	} else {
1500 		thislen = buf->head[0].iov_len - offset;
1501 		if (thislen > len)
1502 			thislen = len;
1503 		sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
1504 		ret = actor(sg, data);
1505 		if (ret)
1506 			goto out;
1507 		offset = 0;
1508 		len -= thislen;
1509 	}
1510 	if (len == 0)
1511 		goto out;
1512 
1513 	if (offset >= buf->page_len) {
1514 		offset -= buf->page_len;
1515 	} else {
1516 		page_len = buf->page_len - offset;
1517 		if (page_len > len)
1518 			page_len = len;
1519 		len -= page_len;
1520 		page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
1521 		i = (offset + buf->page_base) >> PAGE_SHIFT;
1522 		thislen = PAGE_SIZE - page_offset;
1523 		do {
1524 			if (thislen > page_len)
1525 				thislen = page_len;
1526 			sg_set_page(sg, buf->pages[i], thislen, page_offset);
1527 			ret = actor(sg, data);
1528 			if (ret)
1529 				goto out;
1530 			page_len -= thislen;
1531 			i++;
1532 			page_offset = 0;
1533 			thislen = PAGE_SIZE;
1534 		} while (page_len != 0);
1535 		offset = 0;
1536 	}
1537 	if (len == 0)
1538 		goto out;
1539 	if (offset < buf->tail[0].iov_len) {
1540 		thislen = buf->tail[0].iov_len - offset;
1541 		if (thislen > len)
1542 			thislen = len;
1543 		sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
1544 		ret = actor(sg, data);
1545 		len -= thislen;
1546 	}
1547 	if (len != 0)
1548 		ret = -EINVAL;
1549 out:
1550 	return ret;
1551 }
1552 EXPORT_SYMBOL_GPL(xdr_process_buf);
1553 
1554 /**
1555  * xdr_stream_decode_opaque - Decode variable length opaque
1556  * @xdr: pointer to xdr_stream
1557  * @ptr: location to store opaque data
1558  * @size: size of storage buffer @ptr
1559  *
1560  * Return values:
1561  *   On success, returns size of object stored in *@ptr
1562  *   %-EBADMSG on XDR buffer overflow
1563  *   %-EMSGSIZE on overflow of storage buffer @ptr
1564  */
1565 ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, size_t size)
1566 {
1567 	ssize_t ret;
1568 	void *p;
1569 
1570 	ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
1571 	if (ret <= 0)
1572 		return ret;
1573 	memcpy(ptr, p, ret);
1574 	return ret;
1575 }
1576 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque);
1577 
1578 /**
1579  * xdr_stream_decode_opaque_dup - Decode and duplicate variable length opaque
1580  * @xdr: pointer to xdr_stream
1581  * @ptr: location to store pointer to opaque data
1582  * @maxlen: maximum acceptable object size
1583  * @gfp_flags: GFP mask to use
1584  *
1585  * Return values:
1586  *   On success, returns size of object stored in *@ptr
1587  *   %-EBADMSG on XDR buffer overflow
1588  *   %-EMSGSIZE if the size of the object would exceed @maxlen
1589  *   %-ENOMEM on memory allocation failure
1590  */
1591 ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
1592 		size_t maxlen, gfp_t gfp_flags)
1593 {
1594 	ssize_t ret;
1595 	void *p;
1596 
1597 	ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
1598 	if (ret > 0) {
1599 		*ptr = kmemdup(p, ret, gfp_flags);
1600 		if (*ptr != NULL)
1601 			return ret;
1602 		ret = -ENOMEM;
1603 	}
1604 	*ptr = NULL;
1605 	return ret;
1606 }
1607 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_dup);
1608 
1609 /**
1610  * xdr_stream_decode_string - Decode variable length string
1611  * @xdr: pointer to xdr_stream
1612  * @str: location to store string
1613  * @size: size of storage buffer @str
1614  *
1615  * Return values:
1616  *   On success, returns length of NUL-terminated string stored in *@str
1617  *   %-EBADMSG on XDR buffer overflow
1618  *   %-EMSGSIZE on overflow of storage buffer @str
1619  */
1620 ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, size_t size)
1621 {
1622 	ssize_t ret;
1623 	void *p;
1624 
1625 	ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
1626 	if (ret > 0) {
1627 		memcpy(str, p, ret);
1628 		str[ret] = '\0';
1629 		return strlen(str);
1630 	}
1631 	*str = '\0';
1632 	return ret;
1633 }
1634 EXPORT_SYMBOL_GPL(xdr_stream_decode_string);
1635 
1636 /**
1637  * xdr_stream_decode_string_dup - Decode and duplicate variable length string
1638  * @xdr: pointer to xdr_stream
1639  * @str: location to store pointer to string
1640  * @maxlen: maximum acceptable string length
1641  * @gfp_flags: GFP mask to use
1642  *
1643  * Return values:
1644  *   On success, returns length of NUL-terminated string stored in *@ptr
1645  *   %-EBADMSG on XDR buffer overflow
1646  *   %-EMSGSIZE if the size of the string would exceed @maxlen
1647  *   %-ENOMEM on memory allocation failure
1648  */
1649 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
1650 		size_t maxlen, gfp_t gfp_flags)
1651 {
1652 	void *p;
1653 	ssize_t ret;
1654 
1655 	ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
1656 	if (ret > 0) {
1657 		char *s = kmalloc(ret + 1, gfp_flags);
1658 		if (s != NULL) {
1659 			memcpy(s, p, ret);
1660 			s[ret] = '\0';
1661 			*str = s;
1662 			return strlen(s);
1663 		}
1664 		ret = -ENOMEM;
1665 	}
1666 	*str = NULL;
1667 	return ret;
1668 }
1669 EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);
1670