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 *
xdr_encode_netobj(__be32 * p,const struct xdr_netobj * obj)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 *
xdr_decode_netobj(__be32 * p,struct xdr_netobj * obj)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 */
xdr_encode_opaque_fixed(__be32 * p,const void * ptr,unsigned int nbytes)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 */
xdr_encode_opaque(__be32 * p,const void * ptr,unsigned int nbytes)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 *
xdr_encode_string(__be32 * p,const char * string)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 *
xdr_decode_string_inplace(__be32 * p,char ** sp,unsigned int * lenp,unsigned int maxlen)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(struct xdr_buf * buf,const u32 len)127 xdr_terminate_string(struct xdr_buf *buf, const u32 len)
128 {
129 char *kaddr;
130
131 kaddr = kmap_atomic(buf->pages[0]);
132 kaddr[buf->page_base + len] = '\0';
133 kunmap_atomic(kaddr);
134 }
135 EXPORT_SYMBOL_GPL(xdr_terminate_string);
136
137 size_t
xdr_buf_pagecount(struct xdr_buf * buf)138 xdr_buf_pagecount(struct xdr_buf *buf)
139 {
140 if (!buf->page_len)
141 return 0;
142 return (buf->page_base + buf->page_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
143 }
144
145 int
xdr_alloc_bvec(struct xdr_buf * buf,gfp_t gfp)146 xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp)
147 {
148 size_t i, n = xdr_buf_pagecount(buf);
149
150 if (n != 0 && buf->bvec == NULL) {
151 buf->bvec = kmalloc_array(n, sizeof(buf->bvec[0]), gfp);
152 if (!buf->bvec)
153 return -ENOMEM;
154 for (i = 0; i < n; i++) {
155 buf->bvec[i].bv_page = buf->pages[i];
156 buf->bvec[i].bv_len = PAGE_SIZE;
157 buf->bvec[i].bv_offset = 0;
158 }
159 }
160 return 0;
161 }
162
163 void
xdr_free_bvec(struct xdr_buf * buf)164 xdr_free_bvec(struct xdr_buf *buf)
165 {
166 kfree(buf->bvec);
167 buf->bvec = NULL;
168 }
169
170 /**
171 * xdr_inline_pages - Prepare receive buffer for a large reply
172 * @xdr: xdr_buf into which reply will be placed
173 * @offset: expected offset where data payload will start, in bytes
174 * @pages: vector of struct page pointers
175 * @base: offset in first page where receive should start, in bytes
176 * @len: expected size of the upper layer data payload, in bytes
177 *
178 */
179 void
xdr_inline_pages(struct xdr_buf * xdr,unsigned int offset,struct page ** pages,unsigned int base,unsigned int len)180 xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
181 struct page **pages, unsigned int base, unsigned int len)
182 {
183 struct kvec *head = xdr->head;
184 struct kvec *tail = xdr->tail;
185 char *buf = (char *)head->iov_base;
186 unsigned int buflen = head->iov_len;
187
188 head->iov_len = offset;
189
190 xdr->pages = pages;
191 xdr->page_base = base;
192 xdr->page_len = len;
193
194 tail->iov_base = buf + offset;
195 tail->iov_len = buflen - offset;
196 if ((xdr->page_len & 3) == 0)
197 tail->iov_len -= sizeof(__be32);
198
199 xdr->buflen += len;
200 }
201 EXPORT_SYMBOL_GPL(xdr_inline_pages);
202
203 /*
204 * Helper routines for doing 'memmove' like operations on a struct xdr_buf
205 */
206
207 /**
208 * _shift_data_left_pages
209 * @pages: vector of pages containing both the source and dest memory area.
210 * @pgto_base: page vector address of destination
211 * @pgfrom_base: page vector address of source
212 * @len: number of bytes to copy
213 *
214 * Note: the addresses pgto_base and pgfrom_base are both calculated in
215 * the same way:
216 * if a memory area starts at byte 'base' in page 'pages[i]',
217 * then its address is given as (i << PAGE_CACHE_SHIFT) + base
218 * Alse note: pgto_base must be < pgfrom_base, but the memory areas
219 * they point to may overlap.
220 */
221 static void
_shift_data_left_pages(struct page ** pages,size_t pgto_base,size_t pgfrom_base,size_t len)222 _shift_data_left_pages(struct page **pages, size_t pgto_base,
223 size_t pgfrom_base, size_t len)
224 {
225 struct page **pgfrom, **pgto;
226 char *vfrom, *vto;
227 size_t copy;
228
229 BUG_ON(pgfrom_base <= pgto_base);
230
231 pgto = pages + (pgto_base >> PAGE_SHIFT);
232 pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
233
234 pgto_base &= ~PAGE_MASK;
235 pgfrom_base &= ~PAGE_MASK;
236
237 do {
238 if (pgto_base >= PAGE_SIZE) {
239 pgto_base = 0;
240 pgto++;
241 }
242 if (pgfrom_base >= PAGE_SIZE){
243 pgfrom_base = 0;
244 pgfrom++;
245 }
246
247 copy = len;
248 if (copy > (PAGE_SIZE - pgto_base))
249 copy = PAGE_SIZE - pgto_base;
250 if (copy > (PAGE_SIZE - pgfrom_base))
251 copy = PAGE_SIZE - pgfrom_base;
252
253 vto = kmap_atomic(*pgto);
254 if (*pgto != *pgfrom) {
255 vfrom = kmap_atomic(*pgfrom);
256 memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
257 kunmap_atomic(vfrom);
258 } else
259 memmove(vto + pgto_base, vto + pgfrom_base, copy);
260 flush_dcache_page(*pgto);
261 kunmap_atomic(vto);
262
263 pgto_base += copy;
264 pgfrom_base += copy;
265
266 } while ((len -= copy) != 0);
267 }
268
269 static void
_shift_data_left_tail(struct xdr_buf * buf,unsigned int pgto,size_t len)270 _shift_data_left_tail(struct xdr_buf *buf, unsigned int pgto, size_t len)
271 {
272 struct kvec *tail = buf->tail;
273
274 if (len > tail->iov_len)
275 len = tail->iov_len;
276
277 _copy_to_pages(buf->pages,
278 buf->page_base + pgto,
279 (char *)tail->iov_base,
280 len);
281 tail->iov_len -= len;
282
283 if (tail->iov_len > 0)
284 memmove((char *)tail->iov_base,
285 tail->iov_base + len,
286 tail->iov_len);
287 }
288
289 /**
290 * _shift_data_right_pages
291 * @pages: vector of pages containing both the source and dest memory area.
292 * @pgto_base: page vector address of destination
293 * @pgfrom_base: page vector address of source
294 * @len: number of bytes to copy
295 *
296 * Note: the addresses pgto_base and pgfrom_base are both calculated in
297 * the same way:
298 * if a memory area starts at byte 'base' in page 'pages[i]',
299 * then its address is given as (i << PAGE_SHIFT) + base
300 * Also note: pgfrom_base must be < pgto_base, but the memory areas
301 * they point to may overlap.
302 */
303 static void
_shift_data_right_pages(struct page ** pages,size_t pgto_base,size_t pgfrom_base,size_t len)304 _shift_data_right_pages(struct page **pages, size_t pgto_base,
305 size_t pgfrom_base, size_t len)
306 {
307 struct page **pgfrom, **pgto;
308 char *vfrom, *vto;
309 size_t copy;
310
311 BUG_ON(pgto_base <= pgfrom_base);
312
313 pgto_base += len;
314 pgfrom_base += len;
315
316 pgto = pages + (pgto_base >> PAGE_SHIFT);
317 pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
318
319 pgto_base &= ~PAGE_MASK;
320 pgfrom_base &= ~PAGE_MASK;
321
322 do {
323 /* Are any pointers crossing a page boundary? */
324 if (pgto_base == 0) {
325 pgto_base = PAGE_SIZE;
326 pgto--;
327 }
328 if (pgfrom_base == 0) {
329 pgfrom_base = PAGE_SIZE;
330 pgfrom--;
331 }
332
333 copy = len;
334 if (copy > pgto_base)
335 copy = pgto_base;
336 if (copy > pgfrom_base)
337 copy = pgfrom_base;
338 pgto_base -= copy;
339 pgfrom_base -= copy;
340
341 vto = kmap_atomic(*pgto);
342 if (*pgto != *pgfrom) {
343 vfrom = kmap_atomic(*pgfrom);
344 memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
345 kunmap_atomic(vfrom);
346 } else
347 memmove(vto + pgto_base, vto + pgfrom_base, copy);
348 flush_dcache_page(*pgto);
349 kunmap_atomic(vto);
350
351 } while ((len -= copy) != 0);
352 }
353
354 static unsigned int
_shift_data_right_tail(struct xdr_buf * buf,unsigned int pgfrom,size_t len)355 _shift_data_right_tail(struct xdr_buf *buf, unsigned int pgfrom, size_t len)
356 {
357 struct kvec *tail = buf->tail;
358 unsigned int tailbuf_len;
359 unsigned int result = 0;
360 size_t copy;
361
362 tailbuf_len = buf->buflen - buf->head->iov_len - buf->page_len;
363
364 /* Shift the tail first */
365 if (tailbuf_len != 0) {
366 unsigned int free_space = tailbuf_len - tail->iov_len;
367
368 if (len < free_space)
369 free_space = len;
370 if (len > free_space)
371 len = free_space;
372
373 tail->iov_len += free_space;
374 copy = len;
375
376 if (tail->iov_len > len) {
377 char *p = (char *)tail->iov_base + len;
378 memmove(p, tail->iov_base, tail->iov_len - free_space);
379 result += tail->iov_len - free_space;
380 } else
381 copy = tail->iov_len;
382
383 /* Copy from the inlined pages into the tail */
384 _copy_from_pages((char *)tail->iov_base,
385 buf->pages,
386 buf->page_base + pgfrom,
387 copy);
388 result += copy;
389 }
390
391 return result;
392 }
393
394 /**
395 * _copy_to_pages
396 * @pages: array of pages
397 * @pgbase: page vector address of destination
398 * @p: pointer to source data
399 * @len: length
400 *
401 * Copies data from an arbitrary memory location into an array of pages
402 * The copy is assumed to be non-overlapping.
403 */
404 static void
_copy_to_pages(struct page ** pages,size_t pgbase,const char * p,size_t len)405 _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
406 {
407 struct page **pgto;
408 char *vto;
409 size_t copy;
410
411 pgto = pages + (pgbase >> PAGE_SHIFT);
412 pgbase &= ~PAGE_MASK;
413
414 for (;;) {
415 copy = PAGE_SIZE - pgbase;
416 if (copy > len)
417 copy = len;
418
419 vto = kmap_atomic(*pgto);
420 memcpy(vto + pgbase, p, copy);
421 kunmap_atomic(vto);
422
423 len -= copy;
424 if (len == 0)
425 break;
426
427 pgbase += copy;
428 if (pgbase == PAGE_SIZE) {
429 flush_dcache_page(*pgto);
430 pgbase = 0;
431 pgto++;
432 }
433 p += copy;
434 }
435 flush_dcache_page(*pgto);
436 }
437
438 /**
439 * _copy_from_pages
440 * @p: pointer to destination
441 * @pages: array of pages
442 * @pgbase: offset of source data
443 * @len: length
444 *
445 * Copies data into an arbitrary memory location from an array of pages
446 * The copy is assumed to be non-overlapping.
447 */
448 void
_copy_from_pages(char * p,struct page ** pages,size_t pgbase,size_t len)449 _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
450 {
451 struct page **pgfrom;
452 char *vfrom;
453 size_t copy;
454
455 pgfrom = pages + (pgbase >> PAGE_SHIFT);
456 pgbase &= ~PAGE_MASK;
457
458 do {
459 copy = PAGE_SIZE - pgbase;
460 if (copy > len)
461 copy = len;
462
463 vfrom = kmap_atomic(*pgfrom);
464 memcpy(p, vfrom + pgbase, copy);
465 kunmap_atomic(vfrom);
466
467 pgbase += copy;
468 if (pgbase == PAGE_SIZE) {
469 pgbase = 0;
470 pgfrom++;
471 }
472 p += copy;
473
474 } while ((len -= copy) != 0);
475 }
476 EXPORT_SYMBOL_GPL(_copy_from_pages);
477
478 /**
479 * _zero_pages
480 * @pages: array of pages
481 * @pgbase: beginning page vector address
482 * @len: length
483 */
484 static void
_zero_pages(struct page ** pages,size_t pgbase,size_t len)485 _zero_pages(struct page **pages, size_t pgbase, size_t len)
486 {
487 struct page **page;
488 char *vpage;
489 size_t zero;
490
491 page = pages + (pgbase >> PAGE_SHIFT);
492 pgbase &= ~PAGE_MASK;
493
494 do {
495 zero = PAGE_SIZE - pgbase;
496 if (zero > len)
497 zero = len;
498
499 vpage = kmap_atomic(*page);
500 memset(vpage + pgbase, 0, zero);
501 kunmap_atomic(vpage);
502
503 flush_dcache_page(*page);
504 pgbase = 0;
505 page++;
506
507 } while ((len -= zero) != 0);
508 }
509
510 /**
511 * xdr_shrink_bufhead
512 * @buf: xdr_buf
513 * @len: bytes to remove from buf->head[0]
514 *
515 * Shrinks XDR buffer's header kvec buf->head[0] by
516 * 'len' bytes. The extra data is not lost, but is instead
517 * moved into the inlined pages and/or the tail.
518 */
519 static unsigned int
xdr_shrink_bufhead(struct xdr_buf * buf,size_t len)520 xdr_shrink_bufhead(struct xdr_buf *buf, size_t len)
521 {
522 struct kvec *head, *tail;
523 size_t copy, offs;
524 unsigned int pglen = buf->page_len;
525 unsigned int result;
526
527 result = 0;
528 tail = buf->tail;
529 head = buf->head;
530
531 WARN_ON_ONCE(len > head->iov_len);
532 if (len > head->iov_len)
533 len = head->iov_len;
534
535 /* Shift the tail first */
536 if (tail->iov_len != 0) {
537 if (tail->iov_len > len) {
538 copy = tail->iov_len - len;
539 memmove((char *)tail->iov_base + len,
540 tail->iov_base, copy);
541 result += copy;
542 }
543 /* Copy from the inlined pages into the tail */
544 copy = len;
545 if (copy > pglen)
546 copy = pglen;
547 offs = len - copy;
548 if (offs >= tail->iov_len)
549 copy = 0;
550 else if (copy > tail->iov_len - offs)
551 copy = tail->iov_len - offs;
552 if (copy != 0) {
553 _copy_from_pages((char *)tail->iov_base + offs,
554 buf->pages,
555 buf->page_base + pglen + offs - len,
556 copy);
557 result += copy;
558 }
559 /* Do we also need to copy data from the head into the tail ? */
560 if (len > pglen) {
561 offs = copy = len - pglen;
562 if (copy > tail->iov_len)
563 copy = tail->iov_len;
564 memcpy(tail->iov_base,
565 (char *)head->iov_base +
566 head->iov_len - offs,
567 copy);
568 result += copy;
569 }
570 }
571 /* Now handle pages */
572 if (pglen != 0) {
573 if (pglen > len)
574 _shift_data_right_pages(buf->pages,
575 buf->page_base + len,
576 buf->page_base,
577 pglen - len);
578 copy = len;
579 if (len > pglen)
580 copy = pglen;
581 _copy_to_pages(buf->pages, buf->page_base,
582 (char *)head->iov_base + head->iov_len - len,
583 copy);
584 result += copy;
585 }
586 head->iov_len -= len;
587 buf->buflen -= len;
588 /* Have we truncated the message? */
589 if (buf->len > buf->buflen)
590 buf->len = buf->buflen;
591
592 return result;
593 }
594
595 /**
596 * xdr_shrink_pagelen - shrinks buf->pages by up to @len bytes
597 * @buf: xdr_buf
598 * @len: bytes to remove from buf->pages
599 *
600 * The extra data is not lost, but is instead moved into buf->tail.
601 * Returns the actual number of bytes moved.
602 */
603 static unsigned int
xdr_shrink_pagelen(struct xdr_buf * buf,size_t len)604 xdr_shrink_pagelen(struct xdr_buf *buf, size_t len)
605 {
606 unsigned int pglen = buf->page_len;
607 unsigned int result;
608
609 if (len > buf->page_len)
610 len = buf-> page_len;
611
612 result = _shift_data_right_tail(buf, pglen - len, len);
613 buf->page_len -= len;
614 buf->buflen -= len;
615 /* Have we truncated the message? */
616 if (buf->len > buf->buflen)
617 buf->len = buf->buflen;
618
619 return result;
620 }
621
622 void
xdr_shift_buf(struct xdr_buf * buf,size_t len)623 xdr_shift_buf(struct xdr_buf *buf, size_t len)
624 {
625 xdr_shrink_bufhead(buf, len);
626 }
627 EXPORT_SYMBOL_GPL(xdr_shift_buf);
628
629 /**
630 * xdr_stream_pos - Return the current offset from the start of the xdr_stream
631 * @xdr: pointer to struct xdr_stream
632 */
xdr_stream_pos(const struct xdr_stream * xdr)633 unsigned int xdr_stream_pos(const struct xdr_stream *xdr)
634 {
635 return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2;
636 }
637 EXPORT_SYMBOL_GPL(xdr_stream_pos);
638
639 /**
640 * xdr_page_pos - Return the current offset from the start of the xdr pages
641 * @xdr: pointer to struct xdr_stream
642 */
xdr_page_pos(const struct xdr_stream * xdr)643 unsigned int xdr_page_pos(const struct xdr_stream *xdr)
644 {
645 unsigned int pos = xdr_stream_pos(xdr);
646
647 WARN_ON(pos < xdr->buf->head[0].iov_len);
648 return pos - xdr->buf->head[0].iov_len;
649 }
650 EXPORT_SYMBOL_GPL(xdr_page_pos);
651
652 /**
653 * xdr_init_encode - Initialize a struct xdr_stream for sending data.
654 * @xdr: pointer to xdr_stream struct
655 * @buf: pointer to XDR buffer in which to encode data
656 * @p: current pointer inside XDR buffer
657 * @rqst: pointer to controlling rpc_rqst, for debugging
658 *
659 * Note: at the moment the RPC client only passes the length of our
660 * scratch buffer in the xdr_buf's header kvec. Previously this
661 * meant we needed to call xdr_adjust_iovec() after encoding the
662 * data. With the new scheme, the xdr_stream manages the details
663 * of the buffer length, and takes care of adjusting the kvec
664 * length for us.
665 */
xdr_init_encode(struct xdr_stream * xdr,struct xdr_buf * buf,__be32 * p,struct rpc_rqst * rqst)666 void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
667 struct rpc_rqst *rqst)
668 {
669 struct kvec *iov = buf->head;
670 int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
671
672 xdr_set_scratch_buffer(xdr, NULL, 0);
673 BUG_ON(scratch_len < 0);
674 xdr->buf = buf;
675 xdr->iov = iov;
676 xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
677 xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
678 BUG_ON(iov->iov_len > scratch_len);
679
680 if (p != xdr->p && p != NULL) {
681 size_t len;
682
683 BUG_ON(p < xdr->p || p > xdr->end);
684 len = (char *)p - (char *)xdr->p;
685 xdr->p = p;
686 buf->len += len;
687 iov->iov_len += len;
688 }
689 xdr->rqst = rqst;
690 }
691 EXPORT_SYMBOL_GPL(xdr_init_encode);
692
693 /**
694 * xdr_commit_encode - Ensure all data is written to buffer
695 * @xdr: pointer to xdr_stream
696 *
697 * We handle encoding across page boundaries by giving the caller a
698 * temporary location to write to, then later copying the data into
699 * place; xdr_commit_encode does that copying.
700 *
701 * Normally the caller doesn't need to call this directly, as the
702 * following xdr_reserve_space will do it. But an explicit call may be
703 * required at the end of encoding, or any other time when the xdr_buf
704 * data might be read.
705 */
xdr_commit_encode(struct xdr_stream * xdr)706 inline void xdr_commit_encode(struct xdr_stream *xdr)
707 {
708 int shift = xdr->scratch.iov_len;
709 void *page;
710
711 if (shift == 0)
712 return;
713 page = page_address(*xdr->page_ptr);
714 memcpy(xdr->scratch.iov_base, page, shift);
715 memmove(page, page + shift, (void *)xdr->p - page);
716 xdr->scratch.iov_len = 0;
717 }
718 EXPORT_SYMBOL_GPL(xdr_commit_encode);
719
xdr_get_next_encode_buffer(struct xdr_stream * xdr,size_t nbytes)720 static __be32 *xdr_get_next_encode_buffer(struct xdr_stream *xdr,
721 size_t nbytes)
722 {
723 __be32 *p;
724 int space_left;
725 int frag1bytes, frag2bytes;
726
727 if (nbytes > PAGE_SIZE)
728 goto out_overflow; /* Bigger buffers require special handling */
729 if (xdr->buf->len + nbytes > xdr->buf->buflen)
730 goto out_overflow; /* Sorry, we're totally out of space */
731 frag1bytes = (xdr->end - xdr->p) << 2;
732 frag2bytes = nbytes - frag1bytes;
733 if (xdr->iov)
734 xdr->iov->iov_len += frag1bytes;
735 else
736 xdr->buf->page_len += frag1bytes;
737 xdr->page_ptr++;
738 xdr->iov = NULL;
739 /*
740 * If the last encode didn't end exactly on a page boundary, the
741 * next one will straddle boundaries. Encode into the next
742 * page, then copy it back later in xdr_commit_encode. We use
743 * the "scratch" iov to track any temporarily unused fragment of
744 * space at the end of the previous buffer:
745 */
746 xdr->scratch.iov_base = xdr->p;
747 xdr->scratch.iov_len = frag1bytes;
748 p = page_address(*xdr->page_ptr);
749 /*
750 * Note this is where the next encode will start after we've
751 * shifted this one back:
752 */
753 xdr->p = (void *)p + frag2bytes;
754 space_left = xdr->buf->buflen - xdr->buf->len;
755 if (space_left - frag1bytes >= PAGE_SIZE)
756 xdr->end = (void *)p + PAGE_SIZE;
757 else
758 xdr->end = (void *)p + space_left - frag1bytes;
759
760 xdr->buf->page_len += frag2bytes;
761 xdr->buf->len += nbytes;
762 return p;
763 out_overflow:
764 trace_rpc_xdr_overflow(xdr, nbytes);
765 return NULL;
766 }
767
768 /**
769 * xdr_reserve_space - Reserve buffer space for sending
770 * @xdr: pointer to xdr_stream
771 * @nbytes: number of bytes to reserve
772 *
773 * Checks that we have enough buffer space to encode 'nbytes' more
774 * bytes of data. If so, update the total xdr_buf length, and
775 * adjust the length of the current kvec.
776 */
xdr_reserve_space(struct xdr_stream * xdr,size_t nbytes)777 __be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
778 {
779 __be32 *p = xdr->p;
780 __be32 *q;
781
782 xdr_commit_encode(xdr);
783 /* align nbytes on the next 32-bit boundary */
784 nbytes += 3;
785 nbytes &= ~3;
786 q = p + (nbytes >> 2);
787 if (unlikely(q > xdr->end || q < p))
788 return xdr_get_next_encode_buffer(xdr, nbytes);
789 xdr->p = q;
790 if (xdr->iov)
791 xdr->iov->iov_len += nbytes;
792 else
793 xdr->buf->page_len += nbytes;
794 xdr->buf->len += nbytes;
795 return p;
796 }
797 EXPORT_SYMBOL_GPL(xdr_reserve_space);
798
799
800 /**
801 * xdr_reserve_space_vec - Reserves a large amount of buffer space for sending
802 * @xdr: pointer to xdr_stream
803 * @vec: pointer to a kvec array
804 * @nbytes: number of bytes to reserve
805 *
806 * Reserves enough buffer space to encode 'nbytes' of data and stores the
807 * pointers in 'vec'. The size argument passed to xdr_reserve_space() is
808 * determined based on the number of bytes remaining in the current page to
809 * avoid invalidating iov_base pointers when xdr_commit_encode() is called.
810 */
xdr_reserve_space_vec(struct xdr_stream * xdr,struct kvec * vec,size_t nbytes)811 int xdr_reserve_space_vec(struct xdr_stream *xdr, struct kvec *vec, size_t nbytes)
812 {
813 int thislen;
814 int v = 0;
815 __be32 *p;
816
817 /*
818 * svcrdma requires every READ payload to start somewhere
819 * in xdr->pages.
820 */
821 if (xdr->iov == xdr->buf->head) {
822 xdr->iov = NULL;
823 xdr->end = xdr->p;
824 }
825
826 while (nbytes) {
827 thislen = xdr->buf->page_len % PAGE_SIZE;
828 thislen = min_t(size_t, nbytes, PAGE_SIZE - thislen);
829
830 p = xdr_reserve_space(xdr, thislen);
831 if (!p)
832 return -EIO;
833
834 vec[v].iov_base = p;
835 vec[v].iov_len = thislen;
836 v++;
837 nbytes -= thislen;
838 }
839
840 return v;
841 }
842 EXPORT_SYMBOL_GPL(xdr_reserve_space_vec);
843
844 /**
845 * xdr_truncate_encode - truncate an encode buffer
846 * @xdr: pointer to xdr_stream
847 * @len: new length of buffer
848 *
849 * Truncates the xdr stream, so that xdr->buf->len == len,
850 * and xdr->p points at offset len from the start of the buffer, and
851 * head, tail, and page lengths are adjusted to correspond.
852 *
853 * If this means moving xdr->p to a different buffer, we assume that
854 * the end pointer should be set to the end of the current page,
855 * except in the case of the head buffer when we assume the head
856 * buffer's current length represents the end of the available buffer.
857 *
858 * This is *not* safe to use on a buffer that already has inlined page
859 * cache pages (as in a zero-copy server read reply), except for the
860 * simple case of truncating from one position in the tail to another.
861 *
862 */
xdr_truncate_encode(struct xdr_stream * xdr,size_t len)863 void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
864 {
865 struct xdr_buf *buf = xdr->buf;
866 struct kvec *head = buf->head;
867 struct kvec *tail = buf->tail;
868 int fraglen;
869 int new;
870
871 if (len > buf->len) {
872 WARN_ON_ONCE(1);
873 return;
874 }
875 xdr_commit_encode(xdr);
876
877 fraglen = min_t(int, buf->len - len, tail->iov_len);
878 tail->iov_len -= fraglen;
879 buf->len -= fraglen;
880 if (tail->iov_len) {
881 xdr->p = tail->iov_base + tail->iov_len;
882 WARN_ON_ONCE(!xdr->end);
883 WARN_ON_ONCE(!xdr->iov);
884 return;
885 }
886 WARN_ON_ONCE(fraglen);
887 fraglen = min_t(int, buf->len - len, buf->page_len);
888 buf->page_len -= fraglen;
889 buf->len -= fraglen;
890
891 new = buf->page_base + buf->page_len;
892
893 xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
894
895 if (buf->page_len) {
896 xdr->p = page_address(*xdr->page_ptr);
897 xdr->end = (void *)xdr->p + PAGE_SIZE;
898 xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
899 WARN_ON_ONCE(xdr->iov);
900 return;
901 }
902 if (fraglen)
903 xdr->end = head->iov_base + head->iov_len;
904 /* (otherwise assume xdr->end is already set) */
905 xdr->page_ptr--;
906 head->iov_len = len;
907 buf->len = len;
908 xdr->p = head->iov_base + head->iov_len;
909 xdr->iov = buf->head;
910 }
911 EXPORT_SYMBOL(xdr_truncate_encode);
912
913 /**
914 * xdr_restrict_buflen - decrease available buffer space
915 * @xdr: pointer to xdr_stream
916 * @newbuflen: new maximum number of bytes available
917 *
918 * Adjust our idea of how much space is available in the buffer.
919 * If we've already used too much space in the buffer, returns -1.
920 * If the available space is already smaller than newbuflen, returns 0
921 * and does nothing. Otherwise, adjusts xdr->buf->buflen to newbuflen
922 * and ensures xdr->end is set at most offset newbuflen from the start
923 * of the buffer.
924 */
xdr_restrict_buflen(struct xdr_stream * xdr,int newbuflen)925 int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
926 {
927 struct xdr_buf *buf = xdr->buf;
928 int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
929 int end_offset = buf->len + left_in_this_buf;
930
931 if (newbuflen < 0 || newbuflen < buf->len)
932 return -1;
933 if (newbuflen > buf->buflen)
934 return 0;
935 if (newbuflen < end_offset)
936 xdr->end = (void *)xdr->end + newbuflen - end_offset;
937 buf->buflen = newbuflen;
938 return 0;
939 }
940 EXPORT_SYMBOL(xdr_restrict_buflen);
941
942 /**
943 * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
944 * @xdr: pointer to xdr_stream
945 * @pages: list of pages
946 * @base: offset of first byte
947 * @len: length of data in bytes
948 *
949 */
xdr_write_pages(struct xdr_stream * xdr,struct page ** pages,unsigned int base,unsigned int len)950 void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
951 unsigned int len)
952 {
953 struct xdr_buf *buf = xdr->buf;
954 struct kvec *iov = buf->tail;
955 buf->pages = pages;
956 buf->page_base = base;
957 buf->page_len = len;
958
959 iov->iov_base = (char *)xdr->p;
960 iov->iov_len = 0;
961 xdr->iov = iov;
962
963 if (len & 3) {
964 unsigned int pad = 4 - (len & 3);
965
966 BUG_ON(xdr->p >= xdr->end);
967 iov->iov_base = (char *)xdr->p + (len & 3);
968 iov->iov_len += pad;
969 len += pad;
970 *xdr->p++ = 0;
971 }
972 buf->buflen += len;
973 buf->len += len;
974 }
975 EXPORT_SYMBOL_GPL(xdr_write_pages);
976
xdr_set_iov(struct xdr_stream * xdr,struct kvec * iov,unsigned int len)977 static void xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
978 unsigned int len)
979 {
980 if (len > iov->iov_len)
981 len = iov->iov_len;
982 xdr->p = (__be32*)iov->iov_base;
983 xdr->end = (__be32*)(iov->iov_base + len);
984 xdr->iov = iov;
985 xdr->page_ptr = NULL;
986 }
987
xdr_set_page_base(struct xdr_stream * xdr,unsigned int base,unsigned int len)988 static int xdr_set_page_base(struct xdr_stream *xdr,
989 unsigned int base, unsigned int len)
990 {
991 unsigned int pgnr;
992 unsigned int maxlen;
993 unsigned int pgoff;
994 unsigned int pgend;
995 void *kaddr;
996
997 maxlen = xdr->buf->page_len;
998 if (base >= maxlen)
999 return -EINVAL;
1000 maxlen -= base;
1001 if (len > maxlen)
1002 len = maxlen;
1003
1004 base += xdr->buf->page_base;
1005
1006 pgnr = base >> PAGE_SHIFT;
1007 xdr->page_ptr = &xdr->buf->pages[pgnr];
1008 kaddr = page_address(*xdr->page_ptr);
1009
1010 pgoff = base & ~PAGE_MASK;
1011 xdr->p = (__be32*)(kaddr + pgoff);
1012
1013 pgend = pgoff + len;
1014 if (pgend > PAGE_SIZE)
1015 pgend = PAGE_SIZE;
1016 xdr->end = (__be32*)(kaddr + pgend);
1017 xdr->iov = NULL;
1018 return 0;
1019 }
1020
xdr_set_page(struct xdr_stream * xdr,unsigned int base,unsigned int len)1021 static void xdr_set_page(struct xdr_stream *xdr, unsigned int base,
1022 unsigned int len)
1023 {
1024 if (xdr_set_page_base(xdr, base, len) < 0)
1025 xdr_set_iov(xdr, xdr->buf->tail, xdr->nwords << 2);
1026 }
1027
xdr_set_next_page(struct xdr_stream * xdr)1028 static void xdr_set_next_page(struct xdr_stream *xdr)
1029 {
1030 unsigned int newbase;
1031
1032 newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
1033 newbase -= xdr->buf->page_base;
1034
1035 xdr_set_page(xdr, newbase, PAGE_SIZE);
1036 }
1037
xdr_set_next_buffer(struct xdr_stream * xdr)1038 static bool xdr_set_next_buffer(struct xdr_stream *xdr)
1039 {
1040 if (xdr->page_ptr != NULL)
1041 xdr_set_next_page(xdr);
1042 else if (xdr->iov == xdr->buf->head) {
1043 xdr_set_page(xdr, 0, PAGE_SIZE);
1044 }
1045 return xdr->p != xdr->end;
1046 }
1047
1048 /**
1049 * xdr_init_decode - Initialize an xdr_stream for decoding data.
1050 * @xdr: pointer to xdr_stream struct
1051 * @buf: pointer to XDR buffer from which to decode data
1052 * @p: current pointer inside XDR buffer
1053 * @rqst: pointer to controlling rpc_rqst, for debugging
1054 */
xdr_init_decode(struct xdr_stream * xdr,struct xdr_buf * buf,__be32 * p,struct rpc_rqst * rqst)1055 void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
1056 struct rpc_rqst *rqst)
1057 {
1058 xdr->buf = buf;
1059 xdr->scratch.iov_base = NULL;
1060 xdr->scratch.iov_len = 0;
1061 xdr->nwords = XDR_QUADLEN(buf->len);
1062 if (buf->head[0].iov_len != 0)
1063 xdr_set_iov(xdr, buf->head, buf->len);
1064 else if (buf->page_len != 0)
1065 xdr_set_page_base(xdr, 0, buf->len);
1066 else
1067 xdr_set_iov(xdr, buf->head, buf->len);
1068 if (p != NULL && p > xdr->p && xdr->end >= p) {
1069 xdr->nwords -= p - xdr->p;
1070 xdr->p = p;
1071 }
1072 xdr->rqst = rqst;
1073 }
1074 EXPORT_SYMBOL_GPL(xdr_init_decode);
1075
1076 /**
1077 * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages
1078 * @xdr: pointer to xdr_stream struct
1079 * @buf: pointer to XDR buffer from which to decode data
1080 * @pages: list of pages to decode into
1081 * @len: length in bytes of buffer in pages
1082 */
xdr_init_decode_pages(struct xdr_stream * xdr,struct xdr_buf * buf,struct page ** pages,unsigned int len)1083 void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
1084 struct page **pages, unsigned int len)
1085 {
1086 memset(buf, 0, sizeof(*buf));
1087 buf->pages = pages;
1088 buf->page_len = len;
1089 buf->buflen = len;
1090 buf->len = len;
1091 xdr_init_decode(xdr, buf, NULL, NULL);
1092 }
1093 EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
1094
__xdr_inline_decode(struct xdr_stream * xdr,size_t nbytes)1095 static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1096 {
1097 unsigned int nwords = XDR_QUADLEN(nbytes);
1098 __be32 *p = xdr->p;
1099 __be32 *q = p + nwords;
1100
1101 if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
1102 return NULL;
1103 xdr->p = q;
1104 xdr->nwords -= nwords;
1105 return p;
1106 }
1107
1108 /**
1109 * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
1110 * @xdr: pointer to xdr_stream struct
1111 * @buf: pointer to an empty buffer
1112 * @buflen: size of 'buf'
1113 *
1114 * The scratch buffer is used when decoding from an array of pages.
1115 * If an xdr_inline_decode() call spans across page boundaries, then
1116 * we copy the data into the scratch buffer in order to allow linear
1117 * access.
1118 */
xdr_set_scratch_buffer(struct xdr_stream * xdr,void * buf,size_t buflen)1119 void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
1120 {
1121 xdr->scratch.iov_base = buf;
1122 xdr->scratch.iov_len = buflen;
1123 }
1124 EXPORT_SYMBOL_GPL(xdr_set_scratch_buffer);
1125
xdr_copy_to_scratch(struct xdr_stream * xdr,size_t nbytes)1126 static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
1127 {
1128 __be32 *p;
1129 char *cpdest = xdr->scratch.iov_base;
1130 size_t cplen = (char *)xdr->end - (char *)xdr->p;
1131
1132 if (nbytes > xdr->scratch.iov_len)
1133 goto out_overflow;
1134 p = __xdr_inline_decode(xdr, cplen);
1135 if (p == NULL)
1136 return NULL;
1137 memcpy(cpdest, p, cplen);
1138 if (!xdr_set_next_buffer(xdr))
1139 goto out_overflow;
1140 cpdest += cplen;
1141 nbytes -= cplen;
1142 p = __xdr_inline_decode(xdr, nbytes);
1143 if (p == NULL)
1144 return NULL;
1145 memcpy(cpdest, p, nbytes);
1146 return xdr->scratch.iov_base;
1147 out_overflow:
1148 trace_rpc_xdr_overflow(xdr, nbytes);
1149 return NULL;
1150 }
1151
1152 /**
1153 * xdr_inline_decode - Retrieve XDR data to decode
1154 * @xdr: pointer to xdr_stream struct
1155 * @nbytes: number of bytes of data to decode
1156 *
1157 * Check if the input buffer is long enough to enable us to decode
1158 * 'nbytes' more bytes of data starting at the current position.
1159 * If so return the current pointer, then update the current
1160 * pointer position.
1161 */
xdr_inline_decode(struct xdr_stream * xdr,size_t nbytes)1162 __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1163 {
1164 __be32 *p;
1165
1166 if (unlikely(nbytes == 0))
1167 return xdr->p;
1168 if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1169 goto out_overflow;
1170 p = __xdr_inline_decode(xdr, nbytes);
1171 if (p != NULL)
1172 return p;
1173 return xdr_copy_to_scratch(xdr, nbytes);
1174 out_overflow:
1175 trace_rpc_xdr_overflow(xdr, nbytes);
1176 return NULL;
1177 }
1178 EXPORT_SYMBOL_GPL(xdr_inline_decode);
1179
xdr_realign_pages(struct xdr_stream * xdr)1180 static void xdr_realign_pages(struct xdr_stream *xdr)
1181 {
1182 struct xdr_buf *buf = xdr->buf;
1183 struct kvec *iov = buf->head;
1184 unsigned int cur = xdr_stream_pos(xdr);
1185 unsigned int copied, offset;
1186
1187 /* Realign pages to current pointer position */
1188 if (iov->iov_len > cur) {
1189 offset = iov->iov_len - cur;
1190 copied = xdr_shrink_bufhead(buf, offset);
1191 trace_rpc_xdr_alignment(xdr, offset, copied);
1192 xdr->nwords = XDR_QUADLEN(buf->len - cur);
1193 }
1194 }
1195
xdr_align_pages(struct xdr_stream * xdr,unsigned int len)1196 static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
1197 {
1198 struct xdr_buf *buf = xdr->buf;
1199 unsigned int nwords = XDR_QUADLEN(len);
1200 unsigned int cur = xdr_stream_pos(xdr);
1201 unsigned int copied, offset;
1202
1203 if (xdr->nwords == 0)
1204 return 0;
1205
1206 xdr_realign_pages(xdr);
1207 if (nwords > xdr->nwords) {
1208 nwords = xdr->nwords;
1209 len = nwords << 2;
1210 }
1211 if (buf->page_len <= len)
1212 len = buf->page_len;
1213 else if (nwords < xdr->nwords) {
1214 /* Truncate page data and move it into the tail */
1215 offset = buf->page_len - len;
1216 copied = xdr_shrink_pagelen(buf, offset);
1217 trace_rpc_xdr_alignment(xdr, offset, copied);
1218 xdr->nwords = XDR_QUADLEN(buf->len - cur);
1219 }
1220 return len;
1221 }
1222
1223 /**
1224 * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
1225 * @xdr: pointer to xdr_stream struct
1226 * @len: number of bytes of page data
1227 *
1228 * Moves data beyond the current pointer position from the XDR head[] buffer
1229 * into the page list. Any data that lies beyond current position + "len"
1230 * bytes is moved into the XDR tail[].
1231 *
1232 * Returns the number of XDR encoded bytes now contained in the pages
1233 */
xdr_read_pages(struct xdr_stream * xdr,unsigned int len)1234 unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
1235 {
1236 struct xdr_buf *buf = xdr->buf;
1237 struct kvec *iov;
1238 unsigned int nwords;
1239 unsigned int end;
1240 unsigned int padding;
1241
1242 len = xdr_align_pages(xdr, len);
1243 if (len == 0)
1244 return 0;
1245 nwords = XDR_QUADLEN(len);
1246 padding = (nwords << 2) - len;
1247 xdr->iov = iov = buf->tail;
1248 /* Compute remaining message length. */
1249 end = ((xdr->nwords - nwords) << 2) + padding;
1250 if (end > iov->iov_len)
1251 end = iov->iov_len;
1252
1253 /*
1254 * Position current pointer at beginning of tail, and
1255 * set remaining message length.
1256 */
1257 xdr->p = (__be32 *)((char *)iov->iov_base + padding);
1258 xdr->end = (__be32 *)((char *)iov->iov_base + end);
1259 xdr->page_ptr = NULL;
1260 xdr->nwords = XDR_QUADLEN(end - padding);
1261 return len;
1262 }
1263 EXPORT_SYMBOL_GPL(xdr_read_pages);
1264
xdr_align_data(struct xdr_stream * xdr,uint64_t offset,uint32_t length)1265 uint64_t xdr_align_data(struct xdr_stream *xdr, uint64_t offset, uint32_t length)
1266 {
1267 struct xdr_buf *buf = xdr->buf;
1268 unsigned int from, bytes;
1269 unsigned int shift = 0;
1270
1271 if ((offset + length) < offset ||
1272 (offset + length) > buf->page_len)
1273 length = buf->page_len - offset;
1274
1275 xdr_realign_pages(xdr);
1276 from = xdr_page_pos(xdr);
1277 bytes = xdr->nwords << 2;
1278 if (length < bytes)
1279 bytes = length;
1280
1281 /* Move page data to the left */
1282 if (from > offset) {
1283 shift = min_t(unsigned int, bytes, buf->page_len - from);
1284 _shift_data_left_pages(buf->pages,
1285 buf->page_base + offset,
1286 buf->page_base + from,
1287 shift);
1288 bytes -= shift;
1289
1290 /* Move tail data into the pages, if necessary */
1291 if (bytes > 0)
1292 _shift_data_left_tail(buf, offset + shift, bytes);
1293 }
1294
1295 xdr->nwords -= XDR_QUADLEN(length);
1296 xdr_set_page(xdr, from + length, PAGE_SIZE);
1297 return length;
1298 }
1299 EXPORT_SYMBOL_GPL(xdr_align_data);
1300
xdr_expand_hole(struct xdr_stream * xdr,uint64_t offset,uint64_t length)1301 uint64_t xdr_expand_hole(struct xdr_stream *xdr, uint64_t offset, uint64_t length)
1302 {
1303 struct xdr_buf *buf = xdr->buf;
1304 unsigned int bytes;
1305 unsigned int from;
1306 unsigned int truncated = 0;
1307
1308 if ((offset + length) < offset ||
1309 (offset + length) > buf->page_len)
1310 length = buf->page_len - offset;
1311
1312 xdr_realign_pages(xdr);
1313 from = xdr_page_pos(xdr);
1314 bytes = xdr->nwords << 2;
1315
1316 if (offset + length + bytes > buf->page_len) {
1317 unsigned int shift = (offset + length + bytes) - buf->page_len;
1318 unsigned int res = _shift_data_right_tail(buf, from + bytes - shift, shift);
1319 truncated = shift - res;
1320 xdr->nwords -= XDR_QUADLEN(truncated);
1321 bytes -= shift;
1322 }
1323
1324 /* Now move the page data over and zero pages */
1325 if (bytes > 0)
1326 _shift_data_right_pages(buf->pages,
1327 buf->page_base + offset + length,
1328 buf->page_base + from,
1329 bytes);
1330 _zero_pages(buf->pages, buf->page_base + offset, length);
1331
1332 buf->len += length - (from - offset) - truncated;
1333 xdr_set_page(xdr, offset + length, PAGE_SIZE);
1334 return length;
1335 }
1336 EXPORT_SYMBOL_GPL(xdr_expand_hole);
1337
1338 /**
1339 * xdr_enter_page - decode data from the XDR page
1340 * @xdr: pointer to xdr_stream struct
1341 * @len: number of bytes of page data
1342 *
1343 * Moves data beyond the current pointer position from the XDR head[] buffer
1344 * into the page list. Any data that lies beyond current position + "len"
1345 * bytes is moved into the XDR tail[]. The current pointer is then
1346 * repositioned at the beginning of the first XDR page.
1347 */
xdr_enter_page(struct xdr_stream * xdr,unsigned int len)1348 void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
1349 {
1350 len = xdr_align_pages(xdr, len);
1351 /*
1352 * Position current pointer at beginning of tail, and
1353 * set remaining message length.
1354 */
1355 if (len != 0)
1356 xdr_set_page_base(xdr, 0, len);
1357 }
1358 EXPORT_SYMBOL_GPL(xdr_enter_page);
1359
1360 static const struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
1361
1362 void
xdr_buf_from_iov(struct kvec * iov,struct xdr_buf * buf)1363 xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
1364 {
1365 buf->head[0] = *iov;
1366 buf->tail[0] = empty_iov;
1367 buf->page_len = 0;
1368 buf->buflen = buf->len = iov->iov_len;
1369 }
1370 EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
1371
1372 /**
1373 * xdr_buf_subsegment - set subbuf to a portion of buf
1374 * @buf: an xdr buffer
1375 * @subbuf: the result buffer
1376 * @base: beginning of range in bytes
1377 * @len: length of range in bytes
1378 *
1379 * sets @subbuf to an xdr buffer representing the portion of @buf of
1380 * length @len starting at offset @base.
1381 *
1382 * @buf and @subbuf may be pointers to the same struct xdr_buf.
1383 *
1384 * Returns -1 if base of length are out of bounds.
1385 */
1386 int
xdr_buf_subsegment(struct xdr_buf * buf,struct xdr_buf * subbuf,unsigned int base,unsigned int len)1387 xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
1388 unsigned int base, unsigned int len)
1389 {
1390 subbuf->buflen = subbuf->len = len;
1391 if (base < buf->head[0].iov_len) {
1392 subbuf->head[0].iov_base = buf->head[0].iov_base + base;
1393 subbuf->head[0].iov_len = min_t(unsigned int, len,
1394 buf->head[0].iov_len - base);
1395 len -= subbuf->head[0].iov_len;
1396 base = 0;
1397 } else {
1398 base -= buf->head[0].iov_len;
1399 subbuf->head[0].iov_base = buf->head[0].iov_base;
1400 subbuf->head[0].iov_len = 0;
1401 }
1402
1403 if (base < buf->page_len) {
1404 subbuf->page_len = min(buf->page_len - base, len);
1405 base += buf->page_base;
1406 subbuf->page_base = base & ~PAGE_MASK;
1407 subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
1408 len -= subbuf->page_len;
1409 base = 0;
1410 } else {
1411 base -= buf->page_len;
1412 subbuf->pages = buf->pages;
1413 subbuf->page_base = 0;
1414 subbuf->page_len = 0;
1415 }
1416
1417 if (base < buf->tail[0].iov_len) {
1418 subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
1419 subbuf->tail[0].iov_len = min_t(unsigned int, len,
1420 buf->tail[0].iov_len - base);
1421 len -= subbuf->tail[0].iov_len;
1422 base = 0;
1423 } else {
1424 base -= buf->tail[0].iov_len;
1425 subbuf->tail[0].iov_base = buf->tail[0].iov_base;
1426 subbuf->tail[0].iov_len = 0;
1427 }
1428
1429 if (base || len)
1430 return -1;
1431 return 0;
1432 }
1433 EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
1434
1435 /**
1436 * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
1437 * @buf: buf to be trimmed
1438 * @len: number of bytes to reduce "buf" by
1439 *
1440 * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
1441 * that it's possible that we'll trim less than that amount if the xdr_buf is
1442 * too small, or if (for instance) it's all in the head and the parser has
1443 * already read too far into it.
1444 */
xdr_buf_trim(struct xdr_buf * buf,unsigned int len)1445 void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
1446 {
1447 size_t cur;
1448 unsigned int trim = len;
1449
1450 if (buf->tail[0].iov_len) {
1451 cur = min_t(size_t, buf->tail[0].iov_len, trim);
1452 buf->tail[0].iov_len -= cur;
1453 trim -= cur;
1454 if (!trim)
1455 goto fix_len;
1456 }
1457
1458 if (buf->page_len) {
1459 cur = min_t(unsigned int, buf->page_len, trim);
1460 buf->page_len -= cur;
1461 trim -= cur;
1462 if (!trim)
1463 goto fix_len;
1464 }
1465
1466 if (buf->head[0].iov_len) {
1467 cur = min_t(size_t, buf->head[0].iov_len, trim);
1468 buf->head[0].iov_len -= cur;
1469 trim -= cur;
1470 }
1471 fix_len:
1472 buf->len -= (len - trim);
1473 }
1474 EXPORT_SYMBOL_GPL(xdr_buf_trim);
1475
__read_bytes_from_xdr_buf(struct xdr_buf * subbuf,void * obj,unsigned int len)1476 static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
1477 {
1478 unsigned int this_len;
1479
1480 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1481 memcpy(obj, subbuf->head[0].iov_base, this_len);
1482 len -= this_len;
1483 obj += this_len;
1484 this_len = min_t(unsigned int, len, subbuf->page_len);
1485 if (this_len)
1486 _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
1487 len -= this_len;
1488 obj += this_len;
1489 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1490 memcpy(obj, subbuf->tail[0].iov_base, this_len);
1491 }
1492
1493 /* obj is assumed to point to allocated memory of size at least len: */
read_bytes_from_xdr_buf(struct xdr_buf * buf,unsigned int base,void * obj,unsigned int len)1494 int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
1495 {
1496 struct xdr_buf subbuf;
1497 int status;
1498
1499 status = xdr_buf_subsegment(buf, &subbuf, base, len);
1500 if (status != 0)
1501 return status;
1502 __read_bytes_from_xdr_buf(&subbuf, obj, len);
1503 return 0;
1504 }
1505 EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
1506
__write_bytes_to_xdr_buf(struct xdr_buf * subbuf,void * obj,unsigned int len)1507 static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
1508 {
1509 unsigned int this_len;
1510
1511 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
1512 memcpy(subbuf->head[0].iov_base, obj, this_len);
1513 len -= this_len;
1514 obj += this_len;
1515 this_len = min_t(unsigned int, len, subbuf->page_len);
1516 if (this_len)
1517 _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
1518 len -= this_len;
1519 obj += this_len;
1520 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
1521 memcpy(subbuf->tail[0].iov_base, obj, this_len);
1522 }
1523
1524 /* obj is assumed to point to allocated memory of size at least len: */
write_bytes_to_xdr_buf(struct xdr_buf * buf,unsigned int base,void * obj,unsigned int len)1525 int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
1526 {
1527 struct xdr_buf subbuf;
1528 int status;
1529
1530 status = xdr_buf_subsegment(buf, &subbuf, base, len);
1531 if (status != 0)
1532 return status;
1533 __write_bytes_to_xdr_buf(&subbuf, obj, len);
1534 return 0;
1535 }
1536 EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
1537
1538 int
xdr_decode_word(struct xdr_buf * buf,unsigned int base,u32 * obj)1539 xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
1540 {
1541 __be32 raw;
1542 int status;
1543
1544 status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
1545 if (status)
1546 return status;
1547 *obj = be32_to_cpu(raw);
1548 return 0;
1549 }
1550 EXPORT_SYMBOL_GPL(xdr_decode_word);
1551
1552 int
xdr_encode_word(struct xdr_buf * buf,unsigned int base,u32 obj)1553 xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
1554 {
1555 __be32 raw = cpu_to_be32(obj);
1556
1557 return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
1558 }
1559 EXPORT_SYMBOL_GPL(xdr_encode_word);
1560
1561 /* Returns 0 on success, or else a negative error code. */
1562 static int
xdr_xcode_array2(struct xdr_buf * buf,unsigned int base,struct xdr_array2_desc * desc,int encode)1563 xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
1564 struct xdr_array2_desc *desc, int encode)
1565 {
1566 char *elem = NULL, *c;
1567 unsigned int copied = 0, todo, avail_here;
1568 struct page **ppages = NULL;
1569 int err;
1570
1571 if (encode) {
1572 if (xdr_encode_word(buf, base, desc->array_len) != 0)
1573 return -EINVAL;
1574 } else {
1575 if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
1576 desc->array_len > desc->array_maxlen ||
1577 (unsigned long) base + 4 + desc->array_len *
1578 desc->elem_size > buf->len)
1579 return -EINVAL;
1580 }
1581 base += 4;
1582
1583 if (!desc->xcode)
1584 return 0;
1585
1586 todo = desc->array_len * desc->elem_size;
1587
1588 /* process head */
1589 if (todo && base < buf->head->iov_len) {
1590 c = buf->head->iov_base + base;
1591 avail_here = min_t(unsigned int, todo,
1592 buf->head->iov_len - base);
1593 todo -= avail_here;
1594
1595 while (avail_here >= desc->elem_size) {
1596 err = desc->xcode(desc, c);
1597 if (err)
1598 goto out;
1599 c += desc->elem_size;
1600 avail_here -= desc->elem_size;
1601 }
1602 if (avail_here) {
1603 if (!elem) {
1604 elem = kmalloc(desc->elem_size, GFP_KERNEL);
1605 err = -ENOMEM;
1606 if (!elem)
1607 goto out;
1608 }
1609 if (encode) {
1610 err = desc->xcode(desc, elem);
1611 if (err)
1612 goto out;
1613 memcpy(c, elem, avail_here);
1614 } else
1615 memcpy(elem, c, avail_here);
1616 copied = avail_here;
1617 }
1618 base = buf->head->iov_len; /* align to start of pages */
1619 }
1620
1621 /* process pages array */
1622 base -= buf->head->iov_len;
1623 if (todo && base < buf->page_len) {
1624 unsigned int avail_page;
1625
1626 avail_here = min(todo, buf->page_len - base);
1627 todo -= avail_here;
1628
1629 base += buf->page_base;
1630 ppages = buf->pages + (base >> PAGE_SHIFT);
1631 base &= ~PAGE_MASK;
1632 avail_page = min_t(unsigned int, PAGE_SIZE - base,
1633 avail_here);
1634 c = kmap(*ppages) + base;
1635
1636 while (avail_here) {
1637 avail_here -= avail_page;
1638 if (copied || avail_page < desc->elem_size) {
1639 unsigned int l = min(avail_page,
1640 desc->elem_size - copied);
1641 if (!elem) {
1642 elem = kmalloc(desc->elem_size,
1643 GFP_KERNEL);
1644 err = -ENOMEM;
1645 if (!elem)
1646 goto out;
1647 }
1648 if (encode) {
1649 if (!copied) {
1650 err = desc->xcode(desc, elem);
1651 if (err)
1652 goto out;
1653 }
1654 memcpy(c, elem + copied, l);
1655 copied += l;
1656 if (copied == desc->elem_size)
1657 copied = 0;
1658 } else {
1659 memcpy(elem + copied, c, l);
1660 copied += l;
1661 if (copied == desc->elem_size) {
1662 err = desc->xcode(desc, elem);
1663 if (err)
1664 goto out;
1665 copied = 0;
1666 }
1667 }
1668 avail_page -= l;
1669 c += l;
1670 }
1671 while (avail_page >= desc->elem_size) {
1672 err = desc->xcode(desc, c);
1673 if (err)
1674 goto out;
1675 c += desc->elem_size;
1676 avail_page -= desc->elem_size;
1677 }
1678 if (avail_page) {
1679 unsigned int l = min(avail_page,
1680 desc->elem_size - copied);
1681 if (!elem) {
1682 elem = kmalloc(desc->elem_size,
1683 GFP_KERNEL);
1684 err = -ENOMEM;
1685 if (!elem)
1686 goto out;
1687 }
1688 if (encode) {
1689 if (!copied) {
1690 err = desc->xcode(desc, elem);
1691 if (err)
1692 goto out;
1693 }
1694 memcpy(c, elem + copied, l);
1695 copied += l;
1696 if (copied == desc->elem_size)
1697 copied = 0;
1698 } else {
1699 memcpy(elem + copied, c, l);
1700 copied += l;
1701 if (copied == desc->elem_size) {
1702 err = desc->xcode(desc, elem);
1703 if (err)
1704 goto out;
1705 copied = 0;
1706 }
1707 }
1708 }
1709 if (avail_here) {
1710 kunmap(*ppages);
1711 ppages++;
1712 c = kmap(*ppages);
1713 }
1714
1715 avail_page = min(avail_here,
1716 (unsigned int) PAGE_SIZE);
1717 }
1718 base = buf->page_len; /* align to start of tail */
1719 }
1720
1721 /* process tail */
1722 base -= buf->page_len;
1723 if (todo) {
1724 c = buf->tail->iov_base + base;
1725 if (copied) {
1726 unsigned int l = desc->elem_size - copied;
1727
1728 if (encode)
1729 memcpy(c, elem + copied, l);
1730 else {
1731 memcpy(elem + copied, c, l);
1732 err = desc->xcode(desc, elem);
1733 if (err)
1734 goto out;
1735 }
1736 todo -= l;
1737 c += l;
1738 }
1739 while (todo) {
1740 err = desc->xcode(desc, c);
1741 if (err)
1742 goto out;
1743 c += desc->elem_size;
1744 todo -= desc->elem_size;
1745 }
1746 }
1747 err = 0;
1748
1749 out:
1750 kfree(elem);
1751 if (ppages)
1752 kunmap(*ppages);
1753 return err;
1754 }
1755
1756 int
xdr_decode_array2(struct xdr_buf * buf,unsigned int base,struct xdr_array2_desc * desc)1757 xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
1758 struct xdr_array2_desc *desc)
1759 {
1760 if (base >= buf->len)
1761 return -EINVAL;
1762
1763 return xdr_xcode_array2(buf, base, desc, 0);
1764 }
1765 EXPORT_SYMBOL_GPL(xdr_decode_array2);
1766
1767 int
xdr_encode_array2(struct xdr_buf * buf,unsigned int base,struct xdr_array2_desc * desc)1768 xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
1769 struct xdr_array2_desc *desc)
1770 {
1771 if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
1772 buf->head->iov_len + buf->page_len + buf->tail->iov_len)
1773 return -EINVAL;
1774
1775 return xdr_xcode_array2(buf, base, desc, 1);
1776 }
1777 EXPORT_SYMBOL_GPL(xdr_encode_array2);
1778
1779 int
xdr_process_buf(struct xdr_buf * buf,unsigned int offset,unsigned int len,int (* actor)(struct scatterlist *,void *),void * data)1780 xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
1781 int (*actor)(struct scatterlist *, void *), void *data)
1782 {
1783 int i, ret = 0;
1784 unsigned int page_len, thislen, page_offset;
1785 struct scatterlist sg[1];
1786
1787 sg_init_table(sg, 1);
1788
1789 if (offset >= buf->head[0].iov_len) {
1790 offset -= buf->head[0].iov_len;
1791 } else {
1792 thislen = buf->head[0].iov_len - offset;
1793 if (thislen > len)
1794 thislen = len;
1795 sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
1796 ret = actor(sg, data);
1797 if (ret)
1798 goto out;
1799 offset = 0;
1800 len -= thislen;
1801 }
1802 if (len == 0)
1803 goto out;
1804
1805 if (offset >= buf->page_len) {
1806 offset -= buf->page_len;
1807 } else {
1808 page_len = buf->page_len - offset;
1809 if (page_len > len)
1810 page_len = len;
1811 len -= page_len;
1812 page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
1813 i = (offset + buf->page_base) >> PAGE_SHIFT;
1814 thislen = PAGE_SIZE - page_offset;
1815 do {
1816 if (thislen > page_len)
1817 thislen = page_len;
1818 sg_set_page(sg, buf->pages[i], thislen, page_offset);
1819 ret = actor(sg, data);
1820 if (ret)
1821 goto out;
1822 page_len -= thislen;
1823 i++;
1824 page_offset = 0;
1825 thislen = PAGE_SIZE;
1826 } while (page_len != 0);
1827 offset = 0;
1828 }
1829 if (len == 0)
1830 goto out;
1831 if (offset < buf->tail[0].iov_len) {
1832 thislen = buf->tail[0].iov_len - offset;
1833 if (thislen > len)
1834 thislen = len;
1835 sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
1836 ret = actor(sg, data);
1837 len -= thislen;
1838 }
1839 if (len != 0)
1840 ret = -EINVAL;
1841 out:
1842 return ret;
1843 }
1844 EXPORT_SYMBOL_GPL(xdr_process_buf);
1845
1846 /**
1847 * xdr_stream_decode_opaque - Decode variable length opaque
1848 * @xdr: pointer to xdr_stream
1849 * @ptr: location to store opaque data
1850 * @size: size of storage buffer @ptr
1851 *
1852 * Return values:
1853 * On success, returns size of object stored in *@ptr
1854 * %-EBADMSG on XDR buffer overflow
1855 * %-EMSGSIZE on overflow of storage buffer @ptr
1856 */
xdr_stream_decode_opaque(struct xdr_stream * xdr,void * ptr,size_t size)1857 ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, size_t size)
1858 {
1859 ssize_t ret;
1860 void *p;
1861
1862 ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
1863 if (ret <= 0)
1864 return ret;
1865 memcpy(ptr, p, ret);
1866 return ret;
1867 }
1868 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque);
1869
1870 /**
1871 * xdr_stream_decode_opaque_dup - Decode and duplicate variable length opaque
1872 * @xdr: pointer to xdr_stream
1873 * @ptr: location to store pointer to opaque data
1874 * @maxlen: maximum acceptable object size
1875 * @gfp_flags: GFP mask to use
1876 *
1877 * Return values:
1878 * On success, returns size of object stored in *@ptr
1879 * %-EBADMSG on XDR buffer overflow
1880 * %-EMSGSIZE if the size of the object would exceed @maxlen
1881 * %-ENOMEM on memory allocation failure
1882 */
xdr_stream_decode_opaque_dup(struct xdr_stream * xdr,void ** ptr,size_t maxlen,gfp_t gfp_flags)1883 ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
1884 size_t maxlen, gfp_t gfp_flags)
1885 {
1886 ssize_t ret;
1887 void *p;
1888
1889 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
1890 if (ret > 0) {
1891 *ptr = kmemdup(p, ret, gfp_flags);
1892 if (*ptr != NULL)
1893 return ret;
1894 ret = -ENOMEM;
1895 }
1896 *ptr = NULL;
1897 return ret;
1898 }
1899 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_dup);
1900
1901 /**
1902 * xdr_stream_decode_string - Decode variable length string
1903 * @xdr: pointer to xdr_stream
1904 * @str: location to store string
1905 * @size: size of storage buffer @str
1906 *
1907 * Return values:
1908 * On success, returns length of NUL-terminated string stored in *@str
1909 * %-EBADMSG on XDR buffer overflow
1910 * %-EMSGSIZE on overflow of storage buffer @str
1911 */
xdr_stream_decode_string(struct xdr_stream * xdr,char * str,size_t size)1912 ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, size_t size)
1913 {
1914 ssize_t ret;
1915 void *p;
1916
1917 ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
1918 if (ret > 0) {
1919 memcpy(str, p, ret);
1920 str[ret] = '\0';
1921 return strlen(str);
1922 }
1923 *str = '\0';
1924 return ret;
1925 }
1926 EXPORT_SYMBOL_GPL(xdr_stream_decode_string);
1927
1928 /**
1929 * xdr_stream_decode_string_dup - Decode and duplicate variable length string
1930 * @xdr: pointer to xdr_stream
1931 * @str: location to store pointer to string
1932 * @maxlen: maximum acceptable string length
1933 * @gfp_flags: GFP mask to use
1934 *
1935 * Return values:
1936 * On success, returns length of NUL-terminated string stored in *@ptr
1937 * %-EBADMSG on XDR buffer overflow
1938 * %-EMSGSIZE if the size of the string would exceed @maxlen
1939 * %-ENOMEM on memory allocation failure
1940 */
xdr_stream_decode_string_dup(struct xdr_stream * xdr,char ** str,size_t maxlen,gfp_t gfp_flags)1941 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
1942 size_t maxlen, gfp_t gfp_flags)
1943 {
1944 void *p;
1945 ssize_t ret;
1946
1947 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
1948 if (ret > 0) {
1949 char *s = kmalloc(ret + 1, gfp_flags);
1950 if (s != NULL) {
1951 memcpy(s, p, ret);
1952 s[ret] = '\0';
1953 *str = s;
1954 return strlen(s);
1955 }
1956 ret = -ENOMEM;
1957 }
1958 *str = NULL;
1959 return ret;
1960 }
1961 EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);
1962