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 /**
41 * xdr_encode_opaque_fixed - Encode fixed length opaque data
42 * @p: pointer to current position in XDR buffer.
43 * @ptr: pointer to data to encode (or NULL)
44 * @nbytes: size of data.
45 *
46 * Copy the array of data of length nbytes at ptr to the XDR buffer
47 * at position p, then align to the next 32-bit boundary by padding
48 * with zero bytes (see RFC1832).
49 * Note: if ptr is NULL, only the padding is performed.
50 *
51 * Returns the updated current XDR buffer position
52 *
53 */
xdr_encode_opaque_fixed(__be32 * p,const void * ptr,unsigned int nbytes)54 __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int nbytes)
55 {
56 if (likely(nbytes != 0)) {
57 unsigned int quadlen = XDR_QUADLEN(nbytes);
58 unsigned int padding = (quadlen << 2) - nbytes;
59
60 if (ptr != NULL)
61 memcpy(p, ptr, nbytes);
62 if (padding != 0)
63 memset((char *)p + nbytes, 0, padding);
64 p += quadlen;
65 }
66 return p;
67 }
68 EXPORT_SYMBOL_GPL(xdr_encode_opaque_fixed);
69
70 /**
71 * xdr_encode_opaque - Encode variable length opaque data
72 * @p: pointer to current position in XDR buffer.
73 * @ptr: pointer to data to encode (or NULL)
74 * @nbytes: size of data.
75 *
76 * Returns the updated current XDR buffer position
77 */
xdr_encode_opaque(__be32 * p,const void * ptr,unsigned int nbytes)78 __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int nbytes)
79 {
80 *p++ = cpu_to_be32(nbytes);
81 return xdr_encode_opaque_fixed(p, ptr, nbytes);
82 }
83 EXPORT_SYMBOL_GPL(xdr_encode_opaque);
84
85 __be32 *
xdr_encode_string(__be32 * p,const char * string)86 xdr_encode_string(__be32 *p, const char *string)
87 {
88 return xdr_encode_array(p, string, strlen(string));
89 }
90 EXPORT_SYMBOL_GPL(xdr_encode_string);
91
92 /**
93 * xdr_terminate_string - '\0'-terminate a string residing in an xdr_buf
94 * @buf: XDR buffer where string resides
95 * @len: length of string, in bytes
96 *
97 */
xdr_terminate_string(const struct xdr_buf * buf,const u32 len)98 void xdr_terminate_string(const struct xdr_buf *buf, const u32 len)
99 {
100 char *kaddr;
101
102 kaddr = kmap_atomic(buf->pages[0]);
103 kaddr[buf->page_base + len] = '\0';
104 kunmap_atomic(kaddr);
105 }
106 EXPORT_SYMBOL_GPL(xdr_terminate_string);
107
xdr_buf_pagecount(const struct xdr_buf * buf)108 size_t xdr_buf_pagecount(const struct xdr_buf *buf)
109 {
110 if (!buf->page_len)
111 return 0;
112 return (buf->page_base + buf->page_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
113 }
114
115 int
xdr_alloc_bvec(struct xdr_buf * buf,gfp_t gfp)116 xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp)
117 {
118 size_t i, n = xdr_buf_pagecount(buf);
119
120 if (n != 0 && buf->bvec == NULL) {
121 buf->bvec = kmalloc_objs(buf->bvec[0], n, gfp);
122 if (!buf->bvec)
123 return -ENOMEM;
124 for (i = 0; i < n; i++) {
125 bvec_set_page(&buf->bvec[i], buf->pages[i], PAGE_SIZE,
126 0);
127 }
128 }
129 return 0;
130 }
131
132 void
xdr_free_bvec(struct xdr_buf * buf)133 xdr_free_bvec(struct xdr_buf *buf)
134 {
135 kfree(buf->bvec);
136 buf->bvec = NULL;
137 }
138
139 /**
140 * xdr_buf_to_bvec - Copy components of an xdr_buf into a bio_vec array
141 * @bvec: bio_vec array to populate
142 * @bvec_size: element count of @bvec
143 * @xdr: xdr_buf to be copied
144 *
145 * Returns the number of entries consumed in @bvec on success, or
146 * -ESERVERFAULT when @xdr does not fit within @bvec_size entries.
147 */
xdr_buf_to_bvec(struct bio_vec * bvec,unsigned int bvec_size,const struct xdr_buf * xdr)148 int xdr_buf_to_bvec(struct bio_vec *bvec, unsigned int bvec_size,
149 const struct xdr_buf *xdr)
150 {
151 const struct kvec *head = xdr->head;
152 const struct kvec *tail = xdr->tail;
153 unsigned int count = 0;
154
155 if (head->iov_len) {
156 if (unlikely(count >= bvec_size))
157 goto bvec_overflow;
158 bvec_set_virt(bvec++, head->iov_base, head->iov_len);
159 ++count;
160 }
161
162 if (xdr->page_len) {
163 unsigned int offset, len, remaining;
164 struct page **pages = xdr->pages;
165
166 offset = offset_in_page(xdr->page_base);
167 remaining = xdr->page_len;
168 while (remaining > 0) {
169 len = min_t(unsigned int, remaining,
170 PAGE_SIZE - offset);
171 if (unlikely(count >= bvec_size))
172 goto bvec_overflow;
173 bvec_set_page(bvec++, *pages++, len, offset);
174 remaining -= len;
175 offset = 0;
176 ++count;
177 }
178 }
179
180 if (tail->iov_len) {
181 if (unlikely(count >= bvec_size))
182 goto bvec_overflow;
183 bvec_set_virt(bvec, tail->iov_base, tail->iov_len);
184 ++count;
185 }
186
187 return count;
188
189 bvec_overflow:
190 pr_warn_once("%s: bio_vec array overflow\n", __func__);
191 return -ESERVERFAULT;
192 }
193 EXPORT_SYMBOL_GPL(xdr_buf_to_bvec);
194
195 /**
196 * xdr_buf_to_sg - Populate a scatterlist from an xdr_buf range
197 * @buf: xdr_buf to map
198 * @offset: starting byte offset within @buf
199 * @len: number of bytes to cover
200 * @sg: scatterlist array initialized with sg_init_table()
201 * @nsg: number of entries available in @sg
202 *
203 * @sg is traversed with sg_next(), so callers may pass a list
204 * assembled with sg_chain().
205 *
206 * Return: on success, the number of scatterlist entries used; the
207 * last used entry is marked with sg_mark_end(). On failure, a
208 * negative errno.
209 */
xdr_buf_to_sg(const struct xdr_buf * buf,unsigned int offset,unsigned int len,struct scatterlist * sg,unsigned int nsg)210 int xdr_buf_to_sg(const struct xdr_buf *buf, unsigned int offset,
211 unsigned int len, struct scatterlist *sg, unsigned int nsg)
212 {
213 unsigned int page_len, thislen, page_offset;
214 struct scatterlist *cur = sg, *prev = NULL;
215 int nents = 0;
216 int i;
217
218 if (len == 0)
219 return 0;
220
221 if (offset >= buf->head[0].iov_len) {
222 offset -= buf->head[0].iov_len;
223 } else {
224 thislen = min_t(unsigned int,
225 buf->head[0].iov_len - offset, len);
226 if (nents >= nsg)
227 return -ENOSPC;
228 sg_set_buf(cur, buf->head[0].iov_base + offset,
229 thislen);
230 prev = cur;
231 cur = sg_next(cur);
232 nents++;
233 len -= thislen;
234 offset = 0;
235 }
236 if (len == 0)
237 goto done;
238
239 if (offset >= buf->page_len) {
240 offset -= buf->page_len;
241 } else {
242 page_len = min(buf->page_len - offset, len);
243 len -= page_len;
244 page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
245 i = (offset + buf->page_base) >> PAGE_SHIFT;
246 thislen = PAGE_SIZE - page_offset;
247 do {
248 if (thislen > page_len)
249 thislen = page_len;
250 if (nents >= nsg)
251 return -ENOSPC;
252 sg_set_page(cur, buf->pages[i],
253 thislen, page_offset);
254 prev = cur;
255 cur = sg_next(cur);
256 nents++;
257 page_len -= thislen;
258 i++;
259 page_offset = 0;
260 thislen = PAGE_SIZE;
261 } while (page_len != 0);
262 offset = 0;
263 }
264 if (len == 0)
265 goto done;
266
267 if (offset < buf->tail[0].iov_len) {
268 thislen = min_t(unsigned int,
269 buf->tail[0].iov_len - offset, len);
270 if (nents >= nsg)
271 return -ENOSPC;
272 sg_set_buf(cur, buf->tail[0].iov_base + offset,
273 thislen);
274 prev = cur;
275 nents++;
276 len -= thislen;
277 }
278 if (len != 0)
279 return -EINVAL;
280
281 done:
282 if (prev)
283 sg_mark_end(prev);
284 return nents;
285 }
286 EXPORT_SYMBOL_GPL(xdr_buf_to_sg);
287
288 /*
289 * Count the scatterlist entries needed to cover [offset, offset + len)
290 * within @buf. Mirrors the walk in xdr_buf_to_sg() so the caller can
291 * size an allocation that matches the requested sub-range rather than
292 * the full xdr_buf.
293 */
xdr_buf_sg_nents(const struct xdr_buf * buf,unsigned int offset,unsigned int len)294 static unsigned int xdr_buf_sg_nents(const struct xdr_buf *buf,
295 unsigned int offset, unsigned int len)
296 {
297 unsigned int nsg = 0, thislen, page_offset;
298
299 if (len == 0)
300 return 0;
301
302 if (offset < buf->head[0].iov_len) {
303 thislen = min_t(unsigned int,
304 buf->head[0].iov_len - offset, len);
305 nsg++;
306 len -= thislen;
307 offset = 0;
308 } else {
309 offset -= buf->head[0].iov_len;
310 }
311 if (len == 0)
312 return nsg;
313
314 if (offset < buf->page_len) {
315 thislen = min(buf->page_len - offset, len);
316 page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
317 nsg += DIV_ROUND_UP(page_offset + thislen, PAGE_SIZE);
318 len -= thislen;
319 offset = 0;
320 } else {
321 offset -= buf->page_len;
322 }
323 if (len == 0)
324 return nsg;
325
326 if (offset < buf->tail[0].iov_len)
327 nsg++;
328 return nsg;
329 }
330
331 /**
332 * xdr_buf_to_sg_alloc - Populate a scatterlist for an xdr_buf range
333 * @buf: xdr_buf to map
334 * @offset: starting byte offset within @buf
335 * @len: number of bytes to cover
336 * @sg_head: caller-provided scatterlist array (typically stack-allocated)
337 * @sg_head_nents: number of entries in @sg_head
338 * @sg_overflow: OUT: chained extension, or NULL when @sg_head sufficed
339 * @gfp: memory allocation flags for overflow
340 *
341 * Populates @sg_head directly when the xdr_buf fits. When more
342 * entries are needed, an overflow scatterlist is allocated and
343 * chained from @sg_head so that the result is traversable with
344 * sg_next().
345 *
346 * Return: on success, the number of populated scatterlist entries
347 * (counting only data entries, not chain entries). @sg_head is
348 * the head of the resulting list. Caller must kfree @sg_overflow
349 * when done. On failure, a negative errno.
350 */
xdr_buf_to_sg_alloc(const struct xdr_buf * buf,unsigned int offset,unsigned int len,struct scatterlist * sg_head,unsigned int sg_head_nents,struct scatterlist ** sg_overflow,gfp_t gfp)351 int xdr_buf_to_sg_alloc(const struct xdr_buf *buf, unsigned int offset,
352 unsigned int len, struct scatterlist *sg_head,
353 unsigned int sg_head_nents,
354 struct scatterlist **sg_overflow, gfp_t gfp)
355 {
356 unsigned int nsg;
357 int ret;
358
359 *sg_overflow = NULL;
360 if (len == 0)
361 return 0;
362
363 nsg = xdr_buf_sg_nents(buf, offset, len);
364 if (nsg == 0)
365 return -EINVAL;
366
367 if (nsg <= sg_head_nents) {
368 sg_init_table(sg_head, nsg);
369 } else {
370 /* +1 replaces the slot sg_chain() consumes as the link. */
371 unsigned int overflow_nents = nsg - sg_head_nents + 1;
372 struct scatterlist *overflow;
373
374 overflow = kmalloc_array(overflow_nents, sizeof(*overflow),
375 gfp);
376 if (!overflow)
377 return -ENOMEM;
378
379 sg_init_table(sg_head, sg_head_nents);
380 sg_init_table(overflow, overflow_nents);
381 sg_chain(sg_head, sg_head_nents, overflow);
382 *sg_overflow = overflow;
383 }
384
385 ret = xdr_buf_to_sg(buf, offset, len, sg_head, nsg);
386 if (ret < 0) {
387 kfree(*sg_overflow);
388 *sg_overflow = NULL;
389 }
390 return ret;
391 }
392 EXPORT_SYMBOL_GPL(xdr_buf_to_sg_alloc);
393
394 /**
395 * xdr_inline_pages - Prepare receive buffer for a large reply
396 * @xdr: xdr_buf into which reply will be placed
397 * @offset: expected offset where data payload will start, in bytes
398 * @pages: vector of struct page pointers
399 * @base: offset in first page where receive should start, in bytes
400 * @len: expected size of the upper layer data payload, in bytes
401 *
402 */
403 void
xdr_inline_pages(struct xdr_buf * xdr,unsigned int offset,struct page ** pages,unsigned int base,unsigned int len)404 xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
405 struct page **pages, unsigned int base, unsigned int len)
406 {
407 struct kvec *head = xdr->head;
408 struct kvec *tail = xdr->tail;
409 char *buf = (char *)head->iov_base;
410 unsigned int buflen = head->iov_len;
411
412 head->iov_len = offset;
413
414 xdr->pages = pages;
415 xdr->page_base = base;
416 xdr->page_len = len;
417
418 tail->iov_base = buf + offset;
419 tail->iov_len = buflen - offset;
420 xdr->buflen += len;
421 }
422 EXPORT_SYMBOL_GPL(xdr_inline_pages);
423
424 /*
425 * Helper routines for doing 'memmove' like operations on a struct xdr_buf
426 */
427
428 /**
429 * _shift_data_left_pages
430 * @pages: vector of pages containing both the source and dest memory area.
431 * @pgto_base: page vector address of destination
432 * @pgfrom_base: page vector address of source
433 * @len: number of bytes to copy
434 *
435 * Note: the addresses pgto_base and pgfrom_base are both calculated in
436 * the same way:
437 * if a memory area starts at byte 'base' in page 'pages[i]',
438 * then its address is given as (i << PAGE_CACHE_SHIFT) + base
439 * Alse note: pgto_base must be < pgfrom_base, but the memory areas
440 * they point to may overlap.
441 */
442 static void
_shift_data_left_pages(struct page ** pages,size_t pgto_base,size_t pgfrom_base,size_t len)443 _shift_data_left_pages(struct page **pages, size_t pgto_base,
444 size_t pgfrom_base, size_t len)
445 {
446 struct page **pgfrom, **pgto;
447 char *vfrom, *vto;
448 size_t copy;
449
450 BUG_ON(pgfrom_base <= pgto_base);
451
452 if (!len)
453 return;
454
455 pgto = pages + (pgto_base >> PAGE_SHIFT);
456 pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
457
458 pgto_base &= ~PAGE_MASK;
459 pgfrom_base &= ~PAGE_MASK;
460
461 do {
462 if (pgto_base >= PAGE_SIZE) {
463 pgto_base = 0;
464 pgto++;
465 }
466 if (pgfrom_base >= PAGE_SIZE){
467 pgfrom_base = 0;
468 pgfrom++;
469 }
470
471 copy = len;
472 if (copy > (PAGE_SIZE - pgto_base))
473 copy = PAGE_SIZE - pgto_base;
474 if (copy > (PAGE_SIZE - pgfrom_base))
475 copy = PAGE_SIZE - pgfrom_base;
476
477 vto = kmap_atomic(*pgto);
478 if (*pgto != *pgfrom) {
479 vfrom = kmap_atomic(*pgfrom);
480 memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
481 kunmap_atomic(vfrom);
482 } else
483 memmove(vto + pgto_base, vto + pgfrom_base, copy);
484 flush_dcache_page(*pgto);
485 kunmap_atomic(vto);
486
487 pgto_base += copy;
488 pgfrom_base += copy;
489
490 } while ((len -= copy) != 0);
491 }
492
493 /**
494 * _shift_data_right_pages
495 * @pages: vector of pages containing both the source and dest memory area.
496 * @pgto_base: page vector address of destination
497 * @pgfrom_base: page vector address of source
498 * @len: number of bytes to copy
499 *
500 * Note: the addresses pgto_base and pgfrom_base are both calculated in
501 * the same way:
502 * if a memory area starts at byte 'base' in page 'pages[i]',
503 * then its address is given as (i << PAGE_SHIFT) + base
504 * Also note: pgfrom_base must be < pgto_base, but the memory areas
505 * they point to may overlap.
506 */
507 static void
_shift_data_right_pages(struct page ** pages,size_t pgto_base,size_t pgfrom_base,size_t len)508 _shift_data_right_pages(struct page **pages, size_t pgto_base,
509 size_t pgfrom_base, size_t len)
510 {
511 struct page **pgfrom, **pgto;
512 char *vfrom, *vto;
513 size_t copy;
514
515 BUG_ON(pgto_base <= pgfrom_base);
516
517 if (!len)
518 return;
519
520 pgto_base += len;
521 pgfrom_base += len;
522
523 pgto = pages + (pgto_base >> PAGE_SHIFT);
524 pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
525
526 pgto_base &= ~PAGE_MASK;
527 pgfrom_base &= ~PAGE_MASK;
528
529 do {
530 /* Are any pointers crossing a page boundary? */
531 if (pgto_base == 0) {
532 pgto_base = PAGE_SIZE;
533 pgto--;
534 }
535 if (pgfrom_base == 0) {
536 pgfrom_base = PAGE_SIZE;
537 pgfrom--;
538 }
539
540 copy = len;
541 if (copy > pgto_base)
542 copy = pgto_base;
543 if (copy > pgfrom_base)
544 copy = pgfrom_base;
545 pgto_base -= copy;
546 pgfrom_base -= copy;
547
548 vto = kmap_atomic(*pgto);
549 if (*pgto != *pgfrom) {
550 vfrom = kmap_atomic(*pgfrom);
551 memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
552 kunmap_atomic(vfrom);
553 } else
554 memmove(vto + pgto_base, vto + pgfrom_base, copy);
555 flush_dcache_page(*pgto);
556 kunmap_atomic(vto);
557
558 } while ((len -= copy) != 0);
559 }
560
561 /**
562 * _copy_to_pages
563 * @pages: array of pages
564 * @pgbase: page vector address of destination
565 * @p: pointer to source data
566 * @len: length
567 *
568 * Copies data from an arbitrary memory location into an array of pages
569 * The copy is assumed to be non-overlapping.
570 */
571 static void
_copy_to_pages(struct page ** pages,size_t pgbase,const char * p,size_t len)572 _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
573 {
574 struct page **pgto;
575 char *vto;
576 size_t copy;
577
578 if (!len)
579 return;
580
581 pgto = pages + (pgbase >> PAGE_SHIFT);
582 pgbase &= ~PAGE_MASK;
583
584 for (;;) {
585 copy = PAGE_SIZE - pgbase;
586 if (copy > len)
587 copy = len;
588
589 vto = kmap_atomic(*pgto);
590 memcpy(vto + pgbase, p, copy);
591 kunmap_atomic(vto);
592
593 len -= copy;
594 if (len == 0)
595 break;
596
597 pgbase += copy;
598 if (pgbase == PAGE_SIZE) {
599 flush_dcache_page(*pgto);
600 pgbase = 0;
601 pgto++;
602 }
603 p += copy;
604 }
605 flush_dcache_page(*pgto);
606 }
607
608 /**
609 * _copy_from_pages
610 * @p: pointer to destination
611 * @pages: array of pages
612 * @pgbase: offset of source data
613 * @len: length
614 *
615 * Copies data into an arbitrary memory location from an array of pages
616 * The copy is assumed to be non-overlapping.
617 */
618 void
_copy_from_pages(char * p,struct page ** pages,size_t pgbase,size_t len)619 _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
620 {
621 struct page **pgfrom;
622 char *vfrom;
623 size_t copy;
624
625 if (!len)
626 return;
627
628 pgfrom = pages + (pgbase >> PAGE_SHIFT);
629 pgbase &= ~PAGE_MASK;
630
631 do {
632 copy = PAGE_SIZE - pgbase;
633 if (copy > len)
634 copy = len;
635
636 vfrom = kmap_atomic(*pgfrom);
637 memcpy(p, vfrom + pgbase, copy);
638 kunmap_atomic(vfrom);
639
640 pgbase += copy;
641 if (pgbase == PAGE_SIZE) {
642 pgbase = 0;
643 pgfrom++;
644 }
645 p += copy;
646
647 } while ((len -= copy) != 0);
648 }
649 EXPORT_SYMBOL_GPL(_copy_from_pages);
650
xdr_buf_iov_zero(const struct kvec * iov,unsigned int base,unsigned int len)651 static void xdr_buf_iov_zero(const struct kvec *iov, unsigned int base,
652 unsigned int len)
653 {
654 if (base >= iov->iov_len)
655 return;
656 if (len > iov->iov_len - base)
657 len = iov->iov_len - base;
658 memset(iov->iov_base + base, 0, len);
659 }
660
661 /**
662 * xdr_buf_pages_zero
663 * @buf: xdr_buf
664 * @pgbase: beginning offset
665 * @len: length
666 */
xdr_buf_pages_zero(const struct xdr_buf * buf,unsigned int pgbase,unsigned int len)667 static void xdr_buf_pages_zero(const struct xdr_buf *buf, unsigned int pgbase,
668 unsigned int len)
669 {
670 struct page **pages = buf->pages;
671 struct page **page;
672 char *vpage;
673 unsigned int zero;
674
675 if (!len)
676 return;
677 if (pgbase >= buf->page_len) {
678 xdr_buf_iov_zero(buf->tail, pgbase - buf->page_len, len);
679 return;
680 }
681 if (pgbase + len > buf->page_len) {
682 xdr_buf_iov_zero(buf->tail, 0, pgbase + len - buf->page_len);
683 len = buf->page_len - pgbase;
684 }
685
686 pgbase += buf->page_base;
687
688 page = pages + (pgbase >> PAGE_SHIFT);
689 pgbase &= ~PAGE_MASK;
690
691 do {
692 zero = PAGE_SIZE - pgbase;
693 if (zero > len)
694 zero = len;
695
696 vpage = kmap_atomic(*page);
697 memset(vpage + pgbase, 0, zero);
698 kunmap_atomic(vpage);
699
700 flush_dcache_page(*page);
701 pgbase = 0;
702 page++;
703
704 } while ((len -= zero) != 0);
705 }
706
xdr_buf_pages_fill_sparse(const struct xdr_buf * buf,unsigned int buflen,gfp_t gfp)707 static unsigned int xdr_buf_pages_fill_sparse(const struct xdr_buf *buf,
708 unsigned int buflen, gfp_t gfp)
709 {
710 unsigned int i, npages, pagelen;
711
712 if (!(buf->flags & XDRBUF_SPARSE_PAGES))
713 return buflen;
714 if (buflen <= buf->head->iov_len)
715 return buflen;
716 pagelen = buflen - buf->head->iov_len;
717 if (pagelen > buf->page_len)
718 pagelen = buf->page_len;
719 npages = (pagelen + buf->page_base + PAGE_SIZE - 1) >> PAGE_SHIFT;
720 for (i = 0; i < npages; i++) {
721 if (!buf->pages[i])
722 continue;
723 buf->pages[i] = alloc_page(gfp);
724 if (likely(buf->pages[i]))
725 continue;
726 buflen -= pagelen;
727 pagelen = i << PAGE_SHIFT;
728 if (pagelen > buf->page_base)
729 buflen += pagelen - buf->page_base;
730 break;
731 }
732 return buflen;
733 }
734
xdr_buf_try_expand(struct xdr_buf * buf,unsigned int len)735 static void xdr_buf_try_expand(struct xdr_buf *buf, unsigned int len)
736 {
737 struct kvec *head = buf->head;
738 struct kvec *tail = buf->tail;
739 unsigned int sum = head->iov_len + buf->page_len + tail->iov_len;
740 unsigned int free_space, newlen;
741
742 if (sum > buf->len) {
743 free_space = min_t(unsigned int, sum - buf->len, len);
744 newlen = xdr_buf_pages_fill_sparse(buf, buf->len + free_space,
745 GFP_KERNEL);
746 free_space = newlen - buf->len;
747 buf->len = newlen;
748 len -= free_space;
749 if (!len)
750 return;
751 }
752
753 if (buf->buflen > sum) {
754 /* Expand the tail buffer */
755 free_space = min_t(unsigned int, buf->buflen - sum, len);
756 tail->iov_len += free_space;
757 buf->len += free_space;
758 }
759 }
760
xdr_buf_tail_copy_right(const struct xdr_buf * buf,unsigned int base,unsigned int len,unsigned int shift)761 static void xdr_buf_tail_copy_right(const struct xdr_buf *buf,
762 unsigned int base, unsigned int len,
763 unsigned int shift)
764 {
765 const struct kvec *tail = buf->tail;
766 unsigned int to = base + shift;
767
768 if (to >= tail->iov_len)
769 return;
770 if (len + to > tail->iov_len)
771 len = tail->iov_len - to;
772 memmove(tail->iov_base + to, tail->iov_base + base, len);
773 }
774
xdr_buf_pages_copy_right(const struct xdr_buf * buf,unsigned int base,unsigned int len,unsigned int shift)775 static void xdr_buf_pages_copy_right(const struct xdr_buf *buf,
776 unsigned int base, unsigned int len,
777 unsigned int shift)
778 {
779 const struct kvec *tail = buf->tail;
780 unsigned int to = base + shift;
781 unsigned int pglen = 0;
782 unsigned int talen = 0, tato = 0;
783
784 if (base >= buf->page_len)
785 return;
786 if (len > buf->page_len - base)
787 len = buf->page_len - base;
788 if (to >= buf->page_len) {
789 tato = to - buf->page_len;
790 if (tail->iov_len >= len + tato)
791 talen = len;
792 else if (tail->iov_len > tato)
793 talen = tail->iov_len - tato;
794 } else if (len + to >= buf->page_len) {
795 pglen = buf->page_len - to;
796 talen = len - pglen;
797 if (talen > tail->iov_len)
798 talen = tail->iov_len;
799 } else
800 pglen = len;
801
802 _copy_from_pages(tail->iov_base + tato, buf->pages,
803 buf->page_base + base + pglen, talen);
804 _shift_data_right_pages(buf->pages, buf->page_base + to,
805 buf->page_base + base, pglen);
806 }
807
xdr_buf_head_copy_right(const struct xdr_buf * buf,unsigned int base,unsigned int len,unsigned int shift)808 static void xdr_buf_head_copy_right(const struct xdr_buf *buf,
809 unsigned int base, unsigned int len,
810 unsigned int shift)
811 {
812 const struct kvec *head = buf->head;
813 const struct kvec *tail = buf->tail;
814 unsigned int to = base + shift;
815 unsigned int pglen = 0, pgto = 0;
816 unsigned int talen = 0, tato = 0;
817
818 if (base >= head->iov_len)
819 return;
820 if (len > head->iov_len - base)
821 len = head->iov_len - base;
822 if (to >= buf->page_len + head->iov_len) {
823 tato = to - buf->page_len - head->iov_len;
824 talen = len;
825 } else if (to >= head->iov_len) {
826 pgto = to - head->iov_len;
827 pglen = len;
828 if (pgto + pglen > buf->page_len) {
829 talen = pgto + pglen - buf->page_len;
830 pglen -= talen;
831 }
832 } else {
833 pglen = len - to;
834 if (pglen > buf->page_len) {
835 talen = pglen - buf->page_len;
836 pglen = buf->page_len;
837 }
838 }
839
840 len -= talen;
841 base += len;
842 if (talen + tato > tail->iov_len)
843 talen = tail->iov_len > tato ? tail->iov_len - tato : 0;
844 memcpy(tail->iov_base + tato, head->iov_base + base, talen);
845
846 len -= pglen;
847 base -= pglen;
848 _copy_to_pages(buf->pages, buf->page_base + pgto, head->iov_base + base,
849 pglen);
850
851 base -= len;
852 memmove(head->iov_base + to, head->iov_base + base, len);
853 }
854
xdr_buf_tail_shift_right(const struct xdr_buf * buf,unsigned int base,unsigned int len,unsigned int shift)855 static void xdr_buf_tail_shift_right(const struct xdr_buf *buf,
856 unsigned int base, unsigned int len,
857 unsigned int shift)
858 {
859 const struct kvec *tail = buf->tail;
860
861 if (base >= tail->iov_len || !shift || !len)
862 return;
863 xdr_buf_tail_copy_right(buf, base, len, shift);
864 }
865
xdr_buf_pages_shift_right(const struct xdr_buf * buf,unsigned int base,unsigned int len,unsigned int shift)866 static void xdr_buf_pages_shift_right(const struct xdr_buf *buf,
867 unsigned int base, unsigned int len,
868 unsigned int shift)
869 {
870 if (!shift || !len)
871 return;
872 if (base >= buf->page_len) {
873 xdr_buf_tail_shift_right(buf, base - buf->page_len, len, shift);
874 return;
875 }
876 if (base + len > buf->page_len)
877 xdr_buf_tail_shift_right(buf, 0, base + len - buf->page_len,
878 shift);
879 xdr_buf_pages_copy_right(buf, base, len, shift);
880 }
881
xdr_buf_head_shift_right(const struct xdr_buf * buf,unsigned int base,unsigned int len,unsigned int shift)882 static void xdr_buf_head_shift_right(const struct xdr_buf *buf,
883 unsigned int base, unsigned int len,
884 unsigned int shift)
885 {
886 const struct kvec *head = buf->head;
887
888 if (!shift)
889 return;
890 if (base >= head->iov_len) {
891 xdr_buf_pages_shift_right(buf, head->iov_len - base, len,
892 shift);
893 return;
894 }
895 if (base + len > head->iov_len)
896 xdr_buf_pages_shift_right(buf, 0, base + len - head->iov_len,
897 shift);
898 xdr_buf_head_copy_right(buf, base, len, shift);
899 }
900
xdr_buf_tail_copy_left(const struct xdr_buf * buf,unsigned int base,unsigned int len,unsigned int shift)901 static void xdr_buf_tail_copy_left(const struct xdr_buf *buf, unsigned int base,
902 unsigned int len, unsigned int shift)
903 {
904 const struct kvec *tail = buf->tail;
905
906 if (base >= tail->iov_len)
907 return;
908 if (len > tail->iov_len - base)
909 len = tail->iov_len - base;
910 /* Shift data into head */
911 if (shift > buf->page_len + base) {
912 const struct kvec *head = buf->head;
913 unsigned int hdto =
914 head->iov_len + buf->page_len + base - shift;
915 unsigned int hdlen = len;
916
917 if (WARN_ONCE(shift > head->iov_len + buf->page_len + base,
918 "SUNRPC: Misaligned data.\n"))
919 return;
920 if (hdto + hdlen > head->iov_len)
921 hdlen = head->iov_len - hdto;
922 memcpy(head->iov_base + hdto, tail->iov_base + base, hdlen);
923 base += hdlen;
924 len -= hdlen;
925 if (!len)
926 return;
927 }
928 /* Shift data into pages */
929 if (shift > base) {
930 unsigned int pgto = buf->page_len + base - shift;
931 unsigned int pglen = len;
932
933 if (pgto + pglen > buf->page_len)
934 pglen = buf->page_len - pgto;
935 _copy_to_pages(buf->pages, buf->page_base + pgto,
936 tail->iov_base + base, pglen);
937 base += pglen;
938 len -= pglen;
939 if (!len)
940 return;
941 }
942 memmove(tail->iov_base + base - shift, tail->iov_base + base, len);
943 }
944
xdr_buf_pages_copy_left(const struct xdr_buf * buf,unsigned int base,unsigned int len,unsigned int shift)945 static void xdr_buf_pages_copy_left(const struct xdr_buf *buf,
946 unsigned int base, unsigned int len,
947 unsigned int shift)
948 {
949 unsigned int pgto;
950
951 if (base >= buf->page_len)
952 return;
953 if (len > buf->page_len - base)
954 len = buf->page_len - base;
955 /* Shift data into head */
956 if (shift > base) {
957 const struct kvec *head = buf->head;
958 unsigned int hdto = head->iov_len + base - shift;
959 unsigned int hdlen = len;
960
961 if (WARN_ONCE(shift > head->iov_len + base,
962 "SUNRPC: Misaligned data.\n"))
963 return;
964 if (hdto + hdlen > head->iov_len)
965 hdlen = head->iov_len - hdto;
966 _copy_from_pages(head->iov_base + hdto, buf->pages,
967 buf->page_base + base, hdlen);
968 base += hdlen;
969 len -= hdlen;
970 if (!len)
971 return;
972 }
973 pgto = base - shift;
974 _shift_data_left_pages(buf->pages, buf->page_base + pgto,
975 buf->page_base + base, len);
976 }
977
xdr_buf_tail_shift_left(const struct xdr_buf * buf,unsigned int base,unsigned int len,unsigned int shift)978 static void xdr_buf_tail_shift_left(const struct xdr_buf *buf,
979 unsigned int base, unsigned int len,
980 unsigned int shift)
981 {
982 if (!shift || !len)
983 return;
984 xdr_buf_tail_copy_left(buf, base, len, shift);
985 }
986
xdr_buf_pages_shift_left(const struct xdr_buf * buf,unsigned int base,unsigned int len,unsigned int shift)987 static void xdr_buf_pages_shift_left(const struct xdr_buf *buf,
988 unsigned int base, unsigned int len,
989 unsigned int shift)
990 {
991 if (!shift || !len)
992 return;
993 if (base >= buf->page_len) {
994 xdr_buf_tail_shift_left(buf, base - buf->page_len, len, shift);
995 return;
996 }
997 xdr_buf_pages_copy_left(buf, base, len, shift);
998 len += base;
999 if (len <= buf->page_len)
1000 return;
1001 xdr_buf_tail_copy_left(buf, 0, len - buf->page_len, shift);
1002 }
1003
xdr_buf_head_shift_left(const struct xdr_buf * buf,unsigned int base,unsigned int len,unsigned int shift)1004 static void xdr_buf_head_shift_left(const struct xdr_buf *buf,
1005 unsigned int base, unsigned int len,
1006 unsigned int shift)
1007 {
1008 const struct kvec *head = buf->head;
1009 unsigned int bytes;
1010
1011 if (!shift || !len)
1012 return;
1013
1014 if (shift > base) {
1015 bytes = (shift - base);
1016 if (bytes >= len)
1017 return;
1018 base += bytes;
1019 len -= bytes;
1020 }
1021
1022 if (base < head->iov_len) {
1023 bytes = min_t(unsigned int, len, head->iov_len - base);
1024 memmove(head->iov_base + (base - shift),
1025 head->iov_base + base, bytes);
1026 base += bytes;
1027 len -= bytes;
1028 }
1029 xdr_buf_pages_shift_left(buf, base - head->iov_len, len, shift);
1030 }
1031
1032 /**
1033 * xdr_shrink_bufhead
1034 * @buf: xdr_buf
1035 * @len: new length of buf->head[0]
1036 *
1037 * Shrinks XDR buffer's header kvec buf->head[0], setting it to
1038 * 'len' bytes. The extra data is not lost, but is instead
1039 * moved into the inlined pages and/or the tail.
1040 */
xdr_shrink_bufhead(struct xdr_buf * buf,unsigned int len)1041 static unsigned int xdr_shrink_bufhead(struct xdr_buf *buf, unsigned int len)
1042 {
1043 struct kvec *head = buf->head;
1044 unsigned int shift, buflen = max(buf->len, len);
1045
1046 WARN_ON_ONCE(len > head->iov_len);
1047 if (head->iov_len > buflen) {
1048 buf->buflen -= head->iov_len - buflen;
1049 head->iov_len = buflen;
1050 }
1051 if (len >= head->iov_len)
1052 return 0;
1053 shift = head->iov_len - len;
1054 xdr_buf_try_expand(buf, shift);
1055 xdr_buf_head_shift_right(buf, len, buflen - len, shift);
1056 head->iov_len = len;
1057 buf->buflen -= shift;
1058 buf->len -= shift;
1059 return shift;
1060 }
1061
1062 /**
1063 * xdr_shrink_pagelen - shrinks buf->pages to @len bytes
1064 * @buf: xdr_buf
1065 * @len: new page buffer length
1066 *
1067 * The extra data is not lost, but is instead moved into buf->tail.
1068 * Returns the actual number of bytes moved.
1069 */
xdr_shrink_pagelen(struct xdr_buf * buf,unsigned int len)1070 static unsigned int xdr_shrink_pagelen(struct xdr_buf *buf, unsigned int len)
1071 {
1072 unsigned int shift, buflen = buf->len - buf->head->iov_len;
1073
1074 WARN_ON_ONCE(len > buf->page_len);
1075 if (buf->head->iov_len >= buf->len || len > buflen)
1076 buflen = len;
1077 if (buf->page_len > buflen) {
1078 buf->buflen -= buf->page_len - buflen;
1079 buf->page_len = buflen;
1080 }
1081 if (len >= buf->page_len)
1082 return 0;
1083 shift = buf->page_len - len;
1084 xdr_buf_try_expand(buf, shift);
1085 xdr_buf_pages_shift_right(buf, len, buflen - len, shift);
1086 buf->page_len = len;
1087 buf->len -= shift;
1088 buf->buflen -= shift;
1089 return shift;
1090 }
1091
1092 /**
1093 * xdr_stream_pos - Return the current offset from the start of the xdr_stream
1094 * @xdr: pointer to struct xdr_stream
1095 */
xdr_stream_pos(const struct xdr_stream * xdr)1096 unsigned int xdr_stream_pos(const struct xdr_stream *xdr)
1097 {
1098 return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2;
1099 }
1100 EXPORT_SYMBOL_GPL(xdr_stream_pos);
1101
xdr_stream_set_pos(struct xdr_stream * xdr,unsigned int pos)1102 static void xdr_stream_set_pos(struct xdr_stream *xdr, unsigned int pos)
1103 {
1104 unsigned int blen = xdr->buf->len;
1105
1106 xdr->nwords = blen > pos ? XDR_QUADLEN(blen) - XDR_QUADLEN(pos) : 0;
1107 }
1108
xdr_stream_page_set_pos(struct xdr_stream * xdr,unsigned int pos)1109 static void xdr_stream_page_set_pos(struct xdr_stream *xdr, unsigned int pos)
1110 {
1111 xdr_stream_set_pos(xdr, pos + xdr->buf->head[0].iov_len);
1112 }
1113
1114 /**
1115 * xdr_page_pos - Return the current offset from the start of the xdr pages
1116 * @xdr: pointer to struct xdr_stream
1117 */
xdr_page_pos(const struct xdr_stream * xdr)1118 unsigned int xdr_page_pos(const struct xdr_stream *xdr)
1119 {
1120 unsigned int pos = xdr_stream_pos(xdr);
1121
1122 WARN_ON(pos < xdr->buf->head[0].iov_len);
1123 return pos - xdr->buf->head[0].iov_len;
1124 }
1125 EXPORT_SYMBOL_GPL(xdr_page_pos);
1126
1127 /**
1128 * xdr_init_encode - Initialize a struct xdr_stream for sending data.
1129 * @xdr: pointer to xdr_stream struct
1130 * @buf: pointer to XDR buffer in which to encode data
1131 * @p: current pointer inside XDR buffer
1132 * @rqst: pointer to controlling rpc_rqst, for debugging
1133 *
1134 * Note: at the moment the RPC client only passes the length of our
1135 * scratch buffer in the xdr_buf's header kvec. Previously this
1136 * meant we needed to call xdr_adjust_iovec() after encoding the
1137 * data. With the new scheme, the xdr_stream manages the details
1138 * of the buffer length, and takes care of adjusting the kvec
1139 * length for us.
1140 */
xdr_init_encode(struct xdr_stream * xdr,struct xdr_buf * buf,__be32 * p,struct rpc_rqst * rqst)1141 void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
1142 struct rpc_rqst *rqst)
1143 {
1144 struct kvec *iov = buf->head;
1145 int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
1146
1147 xdr_reset_scratch_buffer(xdr);
1148 BUG_ON(scratch_len < 0);
1149 xdr->buf = buf;
1150 xdr->iov = iov;
1151 xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
1152 xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
1153 BUG_ON(iov->iov_len > scratch_len);
1154
1155 if (p != xdr->p && p != NULL) {
1156 size_t len;
1157
1158 BUG_ON(p < xdr->p || p > xdr->end);
1159 len = (char *)p - (char *)xdr->p;
1160 xdr->p = p;
1161 buf->len += len;
1162 iov->iov_len += len;
1163 }
1164 xdr->rqst = rqst;
1165 }
1166 EXPORT_SYMBOL_GPL(xdr_init_encode);
1167
1168 /**
1169 * xdr_init_encode_pages - Initialize an xdr_stream for encoding into pages
1170 * @xdr: pointer to xdr_stream struct
1171 * @buf: pointer to XDR buffer into which to encode data
1172 *
1173 */
xdr_init_encode_pages(struct xdr_stream * xdr,struct xdr_buf * buf)1174 void xdr_init_encode_pages(struct xdr_stream *xdr, struct xdr_buf *buf)
1175 {
1176 xdr_reset_scratch_buffer(xdr);
1177
1178 xdr->buf = buf;
1179 xdr->page_ptr = buf->pages;
1180 xdr->iov = NULL;
1181 xdr->p = page_address(*xdr->page_ptr);
1182 xdr->end = (void *)xdr->p + min_t(u32, buf->buflen, PAGE_SIZE);
1183 xdr->rqst = NULL;
1184 }
1185 EXPORT_SYMBOL_GPL(xdr_init_encode_pages);
1186
1187 /**
1188 * __xdr_commit_encode - Ensure all data is written to buffer
1189 * @xdr: pointer to xdr_stream
1190 *
1191 * We handle encoding across page boundaries by giving the caller a
1192 * temporary location to write to, then later copying the data into
1193 * place; xdr_commit_encode does that copying.
1194 *
1195 * Normally the caller doesn't need to call this directly, as the
1196 * following xdr_reserve_space will do it. But an explicit call may be
1197 * required at the end of encoding, or any other time when the xdr_buf
1198 * data might be read.
1199 */
__xdr_commit_encode(struct xdr_stream * xdr)1200 void __xdr_commit_encode(struct xdr_stream *xdr)
1201 {
1202 size_t shift = xdr->scratch.iov_len;
1203 void *page;
1204
1205 page = page_address(*xdr->page_ptr);
1206 memcpy(xdr->scratch.iov_base, page, shift);
1207 memmove(page, page + shift, (void *)xdr->p - page);
1208 xdr_reset_scratch_buffer(xdr);
1209 }
1210 EXPORT_SYMBOL_GPL(__xdr_commit_encode);
1211
1212 /*
1213 * The buffer space to be reserved crosses the boundary between
1214 * xdr->buf->head and xdr->buf->pages, or between two pages
1215 * in xdr->buf->pages.
1216 */
xdr_get_next_encode_buffer(struct xdr_stream * xdr,size_t nbytes)1217 static noinline __be32 *xdr_get_next_encode_buffer(struct xdr_stream *xdr,
1218 size_t nbytes)
1219 {
1220 int space_left;
1221 int frag1bytes, frag2bytes;
1222 void *p;
1223
1224 if (nbytes > PAGE_SIZE)
1225 goto out_overflow; /* Bigger buffers require special handling */
1226 if (xdr->buf->len + nbytes > xdr->buf->buflen)
1227 goto out_overflow; /* Sorry, we're totally out of space */
1228 frag1bytes = (xdr->end - xdr->p) << 2;
1229 frag2bytes = nbytes - frag1bytes;
1230 if (xdr->iov)
1231 xdr->iov->iov_len += frag1bytes;
1232 else
1233 xdr->buf->page_len += frag1bytes;
1234 xdr->page_ptr++;
1235 xdr->iov = NULL;
1236
1237 /*
1238 * If the last encode didn't end exactly on a page boundary, the
1239 * next one will straddle boundaries. Encode into the next
1240 * page, then copy it back later in xdr_commit_encode. We use
1241 * the "scratch" iov to track any temporarily unused fragment of
1242 * space at the end of the previous buffer:
1243 */
1244 xdr_set_scratch_buffer(xdr, xdr->p, frag1bytes);
1245
1246 /*
1247 * xdr->p is where the next encode will start after
1248 * xdr_commit_encode() has shifted this one back:
1249 */
1250 p = page_address(*xdr->page_ptr);
1251 xdr->p = p + frag2bytes;
1252 space_left = xdr->buf->buflen - xdr->buf->len;
1253 if (space_left - frag1bytes >= PAGE_SIZE)
1254 xdr->end = p + PAGE_SIZE;
1255 else
1256 xdr->end = p + space_left - frag1bytes;
1257
1258 xdr->buf->page_len += frag2bytes;
1259 xdr->buf->len += nbytes;
1260 return p;
1261 out_overflow:
1262 trace_rpc_xdr_overflow(xdr, nbytes);
1263 return NULL;
1264 }
1265
1266 /**
1267 * xdr_reserve_space - Reserve buffer space for sending
1268 * @xdr: pointer to xdr_stream
1269 * @nbytes: number of bytes to reserve
1270 *
1271 * Checks that we have enough buffer space to encode 'nbytes' more
1272 * bytes of data. If so, update the total xdr_buf length, and
1273 * adjust the length of the current kvec.
1274 *
1275 * The returned pointer is valid only until the next call to
1276 * xdr_reserve_space() or xdr_commit_encode() on @xdr. The current
1277 * implementation of this API guarantees that space reserved for a
1278 * four-byte data item remains valid until @xdr is destroyed, but
1279 * that might not always be true in the future.
1280 */
xdr_reserve_space(struct xdr_stream * xdr,size_t nbytes)1281 __be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
1282 {
1283 __be32 *p = xdr->p;
1284 __be32 *q;
1285
1286 xdr_commit_encode(xdr);
1287 /* align nbytes on the next 32-bit boundary */
1288 nbytes += 3;
1289 nbytes &= ~3;
1290 q = p + (nbytes >> 2);
1291 if (unlikely(q > xdr->end || q < p))
1292 return xdr_get_next_encode_buffer(xdr, nbytes);
1293 xdr->p = q;
1294 if (xdr->iov)
1295 xdr->iov->iov_len += nbytes;
1296 else
1297 xdr->buf->page_len += nbytes;
1298 xdr->buf->len += nbytes;
1299 return p;
1300 }
1301 EXPORT_SYMBOL_GPL(xdr_reserve_space);
1302
1303 /**
1304 * xdr_reserve_space_vec - Reserves a large amount of buffer space for sending
1305 * @xdr: pointer to xdr_stream
1306 * @nbytes: number of bytes to reserve
1307 *
1308 * The size argument passed to xdr_reserve_space() is determined based
1309 * on the number of bytes remaining in the current page to avoid
1310 * invalidating iov_base pointers when xdr_commit_encode() is called.
1311 *
1312 * Return values:
1313 * %0: success
1314 * %-EMSGSIZE: not enough space is available in @xdr
1315 */
xdr_reserve_space_vec(struct xdr_stream * xdr,size_t nbytes)1316 int xdr_reserve_space_vec(struct xdr_stream *xdr, size_t nbytes)
1317 {
1318 size_t thislen;
1319 __be32 *p;
1320
1321 /*
1322 * svcrdma requires every READ payload to start somewhere
1323 * in xdr->pages.
1324 */
1325 if (xdr->iov == xdr->buf->head) {
1326 xdr->iov = NULL;
1327 xdr->end = xdr->p;
1328 }
1329
1330 /* XXX: Let's find a way to make this more efficient */
1331 while (nbytes) {
1332 thislen = xdr->buf->page_len % PAGE_SIZE;
1333 thislen = min_t(size_t, nbytes, PAGE_SIZE - thislen);
1334
1335 p = xdr_reserve_space(xdr, thislen);
1336 if (!p)
1337 return -EMSGSIZE;
1338
1339 nbytes -= thislen;
1340 }
1341
1342 return 0;
1343 }
1344 EXPORT_SYMBOL_GPL(xdr_reserve_space_vec);
1345
1346 /**
1347 * xdr_truncate_encode - truncate an encode buffer
1348 * @xdr: pointer to xdr_stream
1349 * @len: new length of buffer
1350 *
1351 * Truncates the xdr stream, so that xdr->buf->len == len,
1352 * and xdr->p points at offset len from the start of the buffer, and
1353 * head, tail, and page lengths are adjusted to correspond.
1354 *
1355 * If this means moving xdr->p to a different buffer, we assume that
1356 * the end pointer should be set to the end of the current page,
1357 * except in the case of the head buffer when we assume the head
1358 * buffer's current length represents the end of the available buffer.
1359 *
1360 * This is *not* safe to use on a buffer that already has inlined page
1361 * cache pages (as in a zero-copy server read reply), except for the
1362 * simple case of truncating from one position in the tail to another.
1363 *
1364 */
xdr_truncate_encode(struct xdr_stream * xdr,size_t len)1365 void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
1366 {
1367 struct xdr_buf *buf = xdr->buf;
1368 struct kvec *head = buf->head;
1369 struct kvec *tail = buf->tail;
1370 int fraglen;
1371 int new;
1372
1373 if (len > buf->len) {
1374 WARN_ON_ONCE(1);
1375 return;
1376 }
1377 xdr_commit_encode(xdr);
1378
1379 fraglen = min_t(int, buf->len - len, tail->iov_len);
1380 tail->iov_len -= fraglen;
1381 buf->len -= fraglen;
1382 if (tail->iov_len) {
1383 xdr->p = tail->iov_base + tail->iov_len;
1384 WARN_ON_ONCE(!xdr->end);
1385 WARN_ON_ONCE(!xdr->iov);
1386 return;
1387 }
1388 WARN_ON_ONCE(fraglen);
1389 fraglen = min_t(int, buf->len - len, buf->page_len);
1390 buf->page_len -= fraglen;
1391 buf->len -= fraglen;
1392
1393 new = buf->page_base + buf->page_len;
1394
1395 xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
1396
1397 if (buf->page_len) {
1398 xdr->p = page_address(*xdr->page_ptr);
1399 xdr->end = (void *)xdr->p + PAGE_SIZE;
1400 xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
1401 WARN_ON_ONCE(xdr->iov);
1402 return;
1403 }
1404 if (fraglen)
1405 xdr->end = head->iov_base + head->iov_len;
1406 /* (otherwise assume xdr->end is already set) */
1407 xdr->page_ptr--;
1408 head->iov_len = len;
1409 buf->len = len;
1410 xdr->p = head->iov_base + head->iov_len;
1411 xdr->iov = buf->head;
1412 }
1413 EXPORT_SYMBOL(xdr_truncate_encode);
1414
1415 /**
1416 * xdr_truncate_decode - Truncate a decoding stream
1417 * @xdr: pointer to struct xdr_stream
1418 * @len: Number of bytes to remove
1419 *
1420 */
xdr_truncate_decode(struct xdr_stream * xdr,size_t len)1421 void xdr_truncate_decode(struct xdr_stream *xdr, size_t len)
1422 {
1423 unsigned int nbytes = xdr_align_size(len);
1424
1425 xdr->buf->len -= nbytes;
1426 xdr->nwords -= XDR_QUADLEN(nbytes);
1427 }
1428 EXPORT_SYMBOL_GPL(xdr_truncate_decode);
1429
1430 /**
1431 * xdr_restrict_buflen - decrease available buffer space
1432 * @xdr: pointer to xdr_stream
1433 * @newbuflen: new maximum number of bytes available
1434 *
1435 * Adjust our idea of how much space is available in the buffer.
1436 * If we've already used too much space in the buffer, returns -1.
1437 * If the available space is already smaller than newbuflen, returns 0
1438 * and does nothing. Otherwise, adjusts xdr->buf->buflen to newbuflen
1439 * and ensures xdr->end is set at most offset newbuflen from the start
1440 * of the buffer.
1441 */
xdr_restrict_buflen(struct xdr_stream * xdr,int newbuflen)1442 int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
1443 {
1444 struct xdr_buf *buf = xdr->buf;
1445 int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
1446 int end_offset = buf->len + left_in_this_buf;
1447
1448 if (newbuflen < 0 || newbuflen < buf->len)
1449 return -1;
1450 if (newbuflen > buf->buflen)
1451 return 0;
1452 if (newbuflen < end_offset)
1453 xdr->end = (void *)xdr->end + newbuflen - end_offset;
1454 buf->buflen = newbuflen;
1455 return 0;
1456 }
1457 EXPORT_SYMBOL(xdr_restrict_buflen);
1458
1459 /**
1460 * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
1461 * @xdr: pointer to xdr_stream
1462 * @pages: array of pages to insert
1463 * @base: starting offset of first data byte in @pages
1464 * @len: number of data bytes in @pages to insert
1465 *
1466 * After the @pages are added, the tail iovec is instantiated pointing to
1467 * end of the head buffer, and the stream is set up to encode subsequent
1468 * items into the tail.
1469 */
xdr_write_pages(struct xdr_stream * xdr,struct page ** pages,unsigned int base,unsigned int len)1470 void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
1471 unsigned int len)
1472 {
1473 struct xdr_buf *buf = xdr->buf;
1474 struct kvec *tail = buf->tail;
1475
1476 buf->pages = pages;
1477 buf->page_base = base;
1478 buf->page_len = len;
1479
1480 tail->iov_base = xdr->p;
1481 tail->iov_len = 0;
1482 xdr->iov = tail;
1483
1484 if (len & 3) {
1485 unsigned int pad = 4 - (len & 3);
1486
1487 BUG_ON(xdr->p >= xdr->end);
1488 tail->iov_base = (char *)xdr->p + (len & 3);
1489 tail->iov_len += pad;
1490 len += pad;
1491 *xdr->p++ = 0;
1492 }
1493 buf->buflen += len;
1494 buf->len += len;
1495 }
1496 EXPORT_SYMBOL_GPL(xdr_write_pages);
1497
xdr_set_iov(struct xdr_stream * xdr,struct kvec * iov,unsigned int base,unsigned int len)1498 static unsigned int xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
1499 unsigned int base, unsigned int len)
1500 {
1501 if (len > iov->iov_len)
1502 len = iov->iov_len;
1503 if (unlikely(base > len))
1504 base = len;
1505 xdr->p = (__be32*)(iov->iov_base + base);
1506 xdr->end = (__be32*)(iov->iov_base + len);
1507 xdr->iov = iov;
1508 xdr->page_ptr = NULL;
1509 return len - base;
1510 }
1511
xdr_set_tail_base(struct xdr_stream * xdr,unsigned int base,unsigned int len)1512 static unsigned int xdr_set_tail_base(struct xdr_stream *xdr,
1513 unsigned int base, unsigned int len)
1514 {
1515 struct xdr_buf *buf = xdr->buf;
1516
1517 xdr_stream_set_pos(xdr, base + buf->page_len + buf->head->iov_len);
1518 return xdr_set_iov(xdr, buf->tail, base, len);
1519 }
1520
xdr_stream_unmap_current_page(struct xdr_stream * xdr)1521 static void xdr_stream_unmap_current_page(struct xdr_stream *xdr)
1522 {
1523 if (xdr->page_kaddr) {
1524 kunmap_local(xdr->page_kaddr);
1525 xdr->page_kaddr = NULL;
1526 }
1527 }
1528
xdr_set_page_base(struct xdr_stream * xdr,unsigned int base,unsigned int len)1529 static unsigned int xdr_set_page_base(struct xdr_stream *xdr,
1530 unsigned int base, unsigned int len)
1531 {
1532 unsigned int pgnr;
1533 unsigned int maxlen;
1534 unsigned int pgoff;
1535 unsigned int pgend;
1536 void *kaddr;
1537
1538 maxlen = xdr->buf->page_len;
1539 if (base >= maxlen)
1540 return 0;
1541 else
1542 maxlen -= base;
1543 if (len > maxlen)
1544 len = maxlen;
1545
1546 xdr_stream_unmap_current_page(xdr);
1547 xdr_stream_page_set_pos(xdr, base);
1548 base += xdr->buf->page_base;
1549
1550 pgnr = base >> PAGE_SHIFT;
1551 xdr->page_ptr = &xdr->buf->pages[pgnr];
1552
1553 if (PageHighMem(*xdr->page_ptr)) {
1554 xdr->page_kaddr = kmap_local_page(*xdr->page_ptr);
1555 kaddr = xdr->page_kaddr;
1556 } else
1557 kaddr = page_address(*xdr->page_ptr);
1558
1559 pgoff = base & ~PAGE_MASK;
1560 xdr->p = (__be32*)(kaddr + pgoff);
1561
1562 pgend = pgoff + len;
1563 if (pgend > PAGE_SIZE)
1564 pgend = PAGE_SIZE;
1565 xdr->end = (__be32*)(kaddr + pgend);
1566 xdr->iov = NULL;
1567 return len;
1568 }
1569
xdr_set_page(struct xdr_stream * xdr,unsigned int base,unsigned int len)1570 static void xdr_set_page(struct xdr_stream *xdr, unsigned int base,
1571 unsigned int len)
1572 {
1573 if (xdr_set_page_base(xdr, base, len) == 0) {
1574 base -= xdr->buf->page_len;
1575 xdr_set_tail_base(xdr, base, len);
1576 }
1577 }
1578
xdr_set_next_page(struct xdr_stream * xdr)1579 static void xdr_set_next_page(struct xdr_stream *xdr)
1580 {
1581 unsigned int newbase;
1582
1583 newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
1584 newbase -= xdr->buf->page_base;
1585 if (newbase < xdr->buf->page_len)
1586 xdr_set_page_base(xdr, newbase, xdr_stream_remaining(xdr));
1587 else
1588 xdr_set_tail_base(xdr, 0, xdr_stream_remaining(xdr));
1589 }
1590
xdr_set_next_buffer(struct xdr_stream * xdr)1591 static bool xdr_set_next_buffer(struct xdr_stream *xdr)
1592 {
1593 if (xdr->page_ptr != NULL)
1594 xdr_set_next_page(xdr);
1595 else if (xdr->iov == xdr->buf->head)
1596 xdr_set_page(xdr, 0, xdr_stream_remaining(xdr));
1597 return xdr->p != xdr->end;
1598 }
1599
1600 /**
1601 * xdr_init_decode - Initialize an xdr_stream for decoding data.
1602 * @xdr: pointer to xdr_stream struct
1603 * @buf: pointer to XDR buffer from which to decode data
1604 * @p: current pointer inside XDR buffer
1605 * @rqst: pointer to controlling rpc_rqst, for debugging
1606 */
xdr_init_decode(struct xdr_stream * xdr,struct xdr_buf * buf,__be32 * p,struct rpc_rqst * rqst)1607 void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
1608 struct rpc_rqst *rqst)
1609 {
1610 xdr->buf = buf;
1611 xdr->page_kaddr = NULL;
1612 xdr_reset_scratch_buffer(xdr);
1613 xdr->nwords = XDR_QUADLEN(buf->len);
1614 if (xdr_set_iov(xdr, buf->head, 0, buf->len) == 0 &&
1615 xdr_set_page_base(xdr, 0, buf->len) == 0)
1616 xdr_set_iov(xdr, buf->tail, 0, buf->len);
1617 if (p != NULL && p > xdr->p && xdr->end >= p) {
1618 xdr->nwords -= p - xdr->p;
1619 xdr->p = p;
1620 }
1621 xdr->rqst = rqst;
1622 }
1623 EXPORT_SYMBOL_GPL(xdr_init_decode);
1624
1625 /**
1626 * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages
1627 * @xdr: pointer to xdr_stream struct
1628 * @buf: pointer to XDR buffer from which to decode data
1629 * @pages: list of pages to decode into
1630 * @len: length in bytes of buffer in pages
1631 */
xdr_init_decode_pages(struct xdr_stream * xdr,struct xdr_buf * buf,struct page ** pages,unsigned int len)1632 void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
1633 struct page **pages, unsigned int len)
1634 {
1635 memset(buf, 0, sizeof(*buf));
1636 buf->pages = pages;
1637 buf->page_len = len;
1638 buf->buflen = len;
1639 buf->len = len;
1640 xdr_init_decode(xdr, buf, NULL, NULL);
1641 }
1642 EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
1643
1644 /**
1645 * xdr_finish_decode - Clean up the xdr_stream after decoding data.
1646 * @xdr: pointer to xdr_stream struct
1647 */
xdr_finish_decode(struct xdr_stream * xdr)1648 void xdr_finish_decode(struct xdr_stream *xdr)
1649 {
1650 xdr_stream_unmap_current_page(xdr);
1651 }
1652 EXPORT_SYMBOL(xdr_finish_decode);
1653
__xdr_inline_decode(struct xdr_stream * xdr,size_t nbytes)1654 static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1655 {
1656 unsigned int nwords = XDR_QUADLEN(nbytes);
1657 __be32 *p = xdr->p;
1658 __be32 *q = p + nwords;
1659
1660 if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
1661 return NULL;
1662 xdr->p = q;
1663 xdr->nwords -= nwords;
1664 return p;
1665 }
1666
xdr_copy_to_scratch(struct xdr_stream * xdr,size_t nbytes)1667 static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
1668 {
1669 __be32 *p;
1670 char *cpdest = xdr->scratch.iov_base;
1671 size_t cplen = (char *)xdr->end - (char *)xdr->p;
1672
1673 if (nbytes > xdr->scratch.iov_len)
1674 goto out_overflow;
1675 p = __xdr_inline_decode(xdr, cplen);
1676 if (p == NULL)
1677 return NULL;
1678 memcpy(cpdest, p, cplen);
1679 if (!xdr_set_next_buffer(xdr))
1680 goto out_overflow;
1681 cpdest += cplen;
1682 nbytes -= cplen;
1683 p = __xdr_inline_decode(xdr, nbytes);
1684 if (p == NULL)
1685 return NULL;
1686 memcpy(cpdest, p, nbytes);
1687 return xdr->scratch.iov_base;
1688 out_overflow:
1689 trace_rpc_xdr_overflow(xdr, nbytes);
1690 return NULL;
1691 }
1692
1693 /**
1694 * xdr_inline_decode - Retrieve XDR data to decode
1695 * @xdr: pointer to xdr_stream struct
1696 * @nbytes: number of bytes of data to decode
1697 *
1698 * Check if the input buffer is long enough to enable us to decode
1699 * 'nbytes' more bytes of data starting at the current position.
1700 * If so return the current pointer, then update the current
1701 * pointer position.
1702 */
xdr_inline_decode(struct xdr_stream * xdr,size_t nbytes)1703 __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
1704 {
1705 __be32 *p;
1706
1707 if (unlikely(nbytes == 0))
1708 return xdr->p;
1709 if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1710 goto out_overflow;
1711 p = __xdr_inline_decode(xdr, nbytes);
1712 if (p != NULL)
1713 return p;
1714 return xdr_copy_to_scratch(xdr, nbytes);
1715 out_overflow:
1716 trace_rpc_xdr_overflow(xdr, nbytes);
1717 return NULL;
1718 }
1719 EXPORT_SYMBOL_GPL(xdr_inline_decode);
1720
xdr_realign_pages(struct xdr_stream * xdr)1721 static void xdr_realign_pages(struct xdr_stream *xdr)
1722 {
1723 struct xdr_buf *buf = xdr->buf;
1724 struct kvec *iov = buf->head;
1725 unsigned int cur = xdr_stream_pos(xdr);
1726 unsigned int copied;
1727
1728 /* Realign pages to current pointer position */
1729 if (iov->iov_len > cur) {
1730 copied = xdr_shrink_bufhead(buf, cur);
1731 trace_rpc_xdr_alignment(xdr, cur, copied);
1732 xdr_set_page(xdr, 0, buf->page_len);
1733 }
1734 }
1735
xdr_align_pages(struct xdr_stream * xdr,unsigned int len)1736 static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
1737 {
1738 struct xdr_buf *buf = xdr->buf;
1739 unsigned int nwords = XDR_QUADLEN(len);
1740 unsigned int copied;
1741
1742 if (xdr->nwords == 0)
1743 return 0;
1744
1745 xdr_realign_pages(xdr);
1746 if (nwords > xdr->nwords) {
1747 nwords = xdr->nwords;
1748 len = nwords << 2;
1749 }
1750 if (buf->page_len <= len)
1751 len = buf->page_len;
1752 else if (nwords < xdr->nwords) {
1753 /* Truncate page data and move it into the tail */
1754 copied = xdr_shrink_pagelen(buf, len);
1755 trace_rpc_xdr_alignment(xdr, len, copied);
1756 }
1757 return len;
1758 }
1759
1760 /**
1761 * xdr_read_pages - align page-based XDR data to current pointer position
1762 * @xdr: pointer to xdr_stream struct
1763 * @len: number of bytes of page data
1764 *
1765 * Moves data beyond the current pointer position from the XDR head[] buffer
1766 * into the page list. Any data that lies beyond current position + @len
1767 * bytes is moved into the XDR tail[]. The xdr_stream current position is
1768 * then advanced past that data to align to the next XDR object in the tail.
1769 *
1770 * Returns the number of XDR encoded bytes now contained in the pages
1771 */
xdr_read_pages(struct xdr_stream * xdr,unsigned int len)1772 unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
1773 {
1774 unsigned int nwords = XDR_QUADLEN(len);
1775 unsigned int base, end, pglen;
1776
1777 pglen = xdr_align_pages(xdr, nwords << 2);
1778 if (pglen == 0)
1779 return 0;
1780
1781 base = (nwords << 2) - pglen;
1782 end = xdr_stream_remaining(xdr) - pglen;
1783
1784 xdr_set_tail_base(xdr, base, end);
1785 return len <= pglen ? len : pglen;
1786 }
1787 EXPORT_SYMBOL_GPL(xdr_read_pages);
1788
1789 /**
1790 * xdr_set_pagelen - Sets the length of the XDR pages
1791 * @xdr: pointer to xdr_stream struct
1792 * @len: new length of the XDR page data
1793 *
1794 * Either grows or shrinks the length of the xdr pages by setting pagelen to
1795 * @len bytes. When shrinking, any extra data is moved into buf->tail, whereas
1796 * when growing any data beyond the current pointer is moved into the tail.
1797 *
1798 * Returns True if the operation was successful, and False otherwise.
1799 */
xdr_set_pagelen(struct xdr_stream * xdr,unsigned int len)1800 void xdr_set_pagelen(struct xdr_stream *xdr, unsigned int len)
1801 {
1802 struct xdr_buf *buf = xdr->buf;
1803 size_t remaining = xdr_stream_remaining(xdr);
1804 size_t base = 0;
1805
1806 if (len < buf->page_len) {
1807 base = buf->page_len - len;
1808 xdr_shrink_pagelen(buf, len);
1809 } else {
1810 xdr_buf_head_shift_right(buf, xdr_stream_pos(xdr),
1811 buf->page_len, remaining);
1812 if (len > buf->page_len)
1813 xdr_buf_try_expand(buf, len - buf->page_len);
1814 }
1815 xdr_set_tail_base(xdr, base, remaining);
1816 }
1817 EXPORT_SYMBOL_GPL(xdr_set_pagelen);
1818
1819 /**
1820 * xdr_enter_page - decode data from the XDR page
1821 * @xdr: pointer to xdr_stream struct
1822 * @len: number of bytes of page data
1823 *
1824 * Moves data beyond the current pointer position from the XDR head[] buffer
1825 * into the page list. Any data that lies beyond current position + "len"
1826 * bytes is moved into the XDR tail[]. The current pointer is then
1827 * repositioned at the beginning of the first XDR page.
1828 */
xdr_enter_page(struct xdr_stream * xdr,unsigned int len)1829 void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
1830 {
1831 len = xdr_align_pages(xdr, len);
1832 /*
1833 * Position current pointer at beginning of tail, and
1834 * set remaining message length.
1835 */
1836 if (len != 0)
1837 xdr_set_page_base(xdr, 0, len);
1838 }
1839 EXPORT_SYMBOL_GPL(xdr_enter_page);
1840
1841 static const struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
1842
xdr_buf_from_iov(const struct kvec * iov,struct xdr_buf * buf)1843 void xdr_buf_from_iov(const struct kvec *iov, struct xdr_buf *buf)
1844 {
1845 buf->head[0] = *iov;
1846 buf->tail[0] = empty_iov;
1847 buf->page_len = 0;
1848 buf->buflen = buf->len = iov->iov_len;
1849 }
1850 EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
1851
1852 /**
1853 * xdr_buf_subsegment - set subbuf to a portion of buf
1854 * @buf: an xdr buffer
1855 * @subbuf: the result buffer
1856 * @base: beginning of range in bytes
1857 * @len: length of range in bytes
1858 *
1859 * sets @subbuf to an xdr buffer representing the portion of @buf of
1860 * length @len starting at offset @base.
1861 *
1862 * @buf and @subbuf may be pointers to the same struct xdr_buf.
1863 *
1864 * Returns -1 if base or length are out of bounds.
1865 */
xdr_buf_subsegment(const struct xdr_buf * buf,struct xdr_buf * subbuf,unsigned int base,unsigned int len)1866 int xdr_buf_subsegment(const struct xdr_buf *buf, struct xdr_buf *subbuf,
1867 unsigned int base, unsigned int len)
1868 {
1869 subbuf->buflen = subbuf->len = len;
1870 if (base < buf->head[0].iov_len) {
1871 subbuf->head[0].iov_base = buf->head[0].iov_base + base;
1872 subbuf->head[0].iov_len = min_t(unsigned int, len,
1873 buf->head[0].iov_len - base);
1874 len -= subbuf->head[0].iov_len;
1875 base = 0;
1876 } else {
1877 base -= buf->head[0].iov_len;
1878 subbuf->head[0].iov_base = buf->head[0].iov_base;
1879 subbuf->head[0].iov_len = 0;
1880 }
1881
1882 if (base < buf->page_len) {
1883 subbuf->page_len = min(buf->page_len - base, len);
1884 base += buf->page_base;
1885 subbuf->page_base = base & ~PAGE_MASK;
1886 subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
1887 len -= subbuf->page_len;
1888 base = 0;
1889 } else {
1890 base -= buf->page_len;
1891 subbuf->pages = buf->pages;
1892 subbuf->page_base = 0;
1893 subbuf->page_len = 0;
1894 }
1895
1896 if (base < buf->tail[0].iov_len) {
1897 subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
1898 subbuf->tail[0].iov_len = min_t(unsigned int, len,
1899 buf->tail[0].iov_len - base);
1900 len -= subbuf->tail[0].iov_len;
1901 base = 0;
1902 } else {
1903 base -= buf->tail[0].iov_len;
1904 subbuf->tail[0].iov_base = buf->tail[0].iov_base;
1905 subbuf->tail[0].iov_len = 0;
1906 }
1907
1908 if (base || len)
1909 return -1;
1910 return 0;
1911 }
1912 EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
1913
1914 /**
1915 * xdr_stream_subsegment - set @subbuf to a portion of @xdr
1916 * @xdr: an xdr_stream set up for decoding
1917 * @subbuf: the result buffer
1918 * @nbytes: length of @xdr to extract, in bytes
1919 *
1920 * Sets up @subbuf to represent a portion of @xdr. The portion
1921 * starts at the current offset in @xdr, and extends for a length
1922 * of @nbytes. If this is successful, @xdr is advanced to the next
1923 * XDR data item following that portion.
1924 *
1925 * Return values:
1926 * %true: @subbuf has been initialized, and @xdr has been advanced.
1927 * %false: a bounds error has occurred
1928 */
xdr_stream_subsegment(struct xdr_stream * xdr,struct xdr_buf * subbuf,unsigned int nbytes)1929 bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
1930 unsigned int nbytes)
1931 {
1932 unsigned int start = xdr_stream_pos(xdr);
1933 unsigned int remaining, len;
1934
1935 /* Extract @subbuf and bounds-check the fn arguments */
1936 if (xdr_buf_subsegment(xdr->buf, subbuf, start, nbytes))
1937 return false;
1938
1939 /* Advance @xdr by @nbytes */
1940 for (remaining = nbytes; remaining;) {
1941 if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
1942 return false;
1943
1944 len = (char *)xdr->end - (char *)xdr->p;
1945 if (remaining <= len) {
1946 xdr->p = (__be32 *)((char *)xdr->p +
1947 (remaining + xdr_pad_size(nbytes)));
1948 break;
1949 }
1950
1951 xdr->p = (__be32 *)((char *)xdr->p + len);
1952 xdr->end = xdr->p;
1953 remaining -= len;
1954 }
1955
1956 xdr_stream_set_pos(xdr, start + nbytes);
1957 return true;
1958 }
1959 EXPORT_SYMBOL_GPL(xdr_stream_subsegment);
1960
1961 /**
1962 * xdr_stream_move_subsegment - Move part of a stream to another position
1963 * @xdr: the source xdr_stream
1964 * @offset: the source offset of the segment
1965 * @target: the target offset of the segment
1966 * @length: the number of bytes to move
1967 *
1968 * Moves @length bytes from @offset to @target in the xdr_stream, overwriting
1969 * anything in its space. Returns the number of bytes in the segment.
1970 */
xdr_stream_move_subsegment(struct xdr_stream * xdr,unsigned int offset,unsigned int target,unsigned int length)1971 unsigned int xdr_stream_move_subsegment(struct xdr_stream *xdr, unsigned int offset,
1972 unsigned int target, unsigned int length)
1973 {
1974 struct xdr_buf buf;
1975 unsigned int shift;
1976
1977 if (offset < target) {
1978 shift = target - offset;
1979 if (xdr_buf_subsegment(xdr->buf, &buf, offset, shift + length) < 0)
1980 return 0;
1981 xdr_buf_head_shift_right(&buf, 0, length, shift);
1982 } else if (offset > target) {
1983 shift = offset - target;
1984 if (xdr_buf_subsegment(xdr->buf, &buf, target, shift + length) < 0)
1985 return 0;
1986 xdr_buf_head_shift_left(&buf, shift, length, shift);
1987 }
1988 return length;
1989 }
1990 EXPORT_SYMBOL_GPL(xdr_stream_move_subsegment);
1991
1992 /**
1993 * xdr_stream_zero - zero out a portion of an xdr_stream
1994 * @xdr: an xdr_stream to zero out
1995 * @offset: the starting point in the stream
1996 * @length: the number of bytes to zero
1997 */
xdr_stream_zero(struct xdr_stream * xdr,unsigned int offset,unsigned int length)1998 unsigned int xdr_stream_zero(struct xdr_stream *xdr, unsigned int offset,
1999 unsigned int length)
2000 {
2001 struct xdr_buf buf;
2002
2003 if (xdr_buf_subsegment(xdr->buf, &buf, offset, length) < 0)
2004 return 0;
2005 if (buf.head[0].iov_len)
2006 xdr_buf_iov_zero(buf.head, 0, buf.head[0].iov_len);
2007 if (buf.page_len > 0)
2008 xdr_buf_pages_zero(&buf, 0, buf.page_len);
2009 if (buf.tail[0].iov_len)
2010 xdr_buf_iov_zero(buf.tail, 0, buf.tail[0].iov_len);
2011 return length;
2012 }
2013 EXPORT_SYMBOL_GPL(xdr_stream_zero);
2014
2015 /**
2016 * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
2017 * @buf: buf to be trimmed
2018 * @len: number of bytes to reduce "buf" by
2019 *
2020 * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
2021 * that it's possible that we'll trim less than that amount if the xdr_buf is
2022 * too small, or if (for instance) it's all in the head and the parser has
2023 * already read too far into it.
2024 */
xdr_buf_trim(struct xdr_buf * buf,unsigned int len)2025 void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
2026 {
2027 size_t cur;
2028 unsigned int trim = len;
2029
2030 if (buf->tail[0].iov_len) {
2031 cur = min_t(size_t, buf->tail[0].iov_len, trim);
2032 buf->tail[0].iov_len -= cur;
2033 trim -= cur;
2034 if (!trim)
2035 goto fix_len;
2036 }
2037
2038 if (buf->page_len) {
2039 cur = min_t(unsigned int, buf->page_len, trim);
2040 buf->page_len -= cur;
2041 trim -= cur;
2042 if (!trim)
2043 goto fix_len;
2044 }
2045
2046 if (buf->head[0].iov_len) {
2047 cur = min_t(size_t, buf->head[0].iov_len, trim);
2048 buf->head[0].iov_len -= cur;
2049 trim -= cur;
2050 }
2051 fix_len:
2052 buf->len -= min_t(unsigned int, buf->len, len - trim);
2053 }
2054 EXPORT_SYMBOL_GPL(xdr_buf_trim);
2055
__read_bytes_from_xdr_buf(const struct xdr_buf * subbuf,void * obj,unsigned int len)2056 static void __read_bytes_from_xdr_buf(const struct xdr_buf *subbuf,
2057 void *obj, unsigned int len)
2058 {
2059 unsigned int this_len;
2060
2061 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
2062 memcpy(obj, subbuf->head[0].iov_base, this_len);
2063 len -= this_len;
2064 obj += this_len;
2065 this_len = min_t(unsigned int, len, subbuf->page_len);
2066 _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
2067 len -= this_len;
2068 obj += this_len;
2069 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
2070 memcpy(obj, subbuf->tail[0].iov_base, this_len);
2071 }
2072
2073 /* obj is assumed to point to allocated memory of size at least len: */
read_bytes_from_xdr_buf(const struct xdr_buf * buf,unsigned int base,void * obj,unsigned int len)2074 int read_bytes_from_xdr_buf(const struct xdr_buf *buf, unsigned int base,
2075 void *obj, unsigned int len)
2076 {
2077 struct xdr_buf subbuf;
2078 int status;
2079
2080 status = xdr_buf_subsegment(buf, &subbuf, base, len);
2081 if (status != 0)
2082 return status;
2083 __read_bytes_from_xdr_buf(&subbuf, obj, len);
2084 return 0;
2085 }
2086 EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
2087
__write_bytes_to_xdr_buf(const struct xdr_buf * subbuf,void * obj,unsigned int len)2088 static void __write_bytes_to_xdr_buf(const struct xdr_buf *subbuf,
2089 void *obj, unsigned int len)
2090 {
2091 unsigned int this_len;
2092
2093 this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
2094 memcpy(subbuf->head[0].iov_base, obj, this_len);
2095 len -= this_len;
2096 obj += this_len;
2097 this_len = min_t(unsigned int, len, subbuf->page_len);
2098 _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
2099 len -= this_len;
2100 obj += this_len;
2101 this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
2102 memcpy(subbuf->tail[0].iov_base, obj, this_len);
2103 }
2104
2105 /* obj is assumed to point to allocated memory of size at least len: */
write_bytes_to_xdr_buf(const struct xdr_buf * buf,unsigned int base,void * obj,unsigned int len)2106 int write_bytes_to_xdr_buf(const struct xdr_buf *buf, unsigned int base,
2107 void *obj, unsigned int len)
2108 {
2109 struct xdr_buf subbuf;
2110 int status;
2111
2112 status = xdr_buf_subsegment(buf, &subbuf, base, len);
2113 if (status != 0)
2114 return status;
2115 __write_bytes_to_xdr_buf(&subbuf, obj, len);
2116 return 0;
2117 }
2118 EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
2119
xdr_decode_word(const struct xdr_buf * buf,unsigned int base,u32 * obj)2120 int xdr_decode_word(const struct xdr_buf *buf, unsigned int base, u32 *obj)
2121 {
2122 __be32 raw;
2123 int status;
2124
2125 status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
2126 if (status)
2127 return status;
2128 *obj = be32_to_cpu(raw);
2129 return 0;
2130 }
2131 EXPORT_SYMBOL_GPL(xdr_decode_word);
2132
xdr_encode_word(const struct xdr_buf * buf,unsigned int base,u32 obj)2133 int xdr_encode_word(const struct xdr_buf *buf, unsigned int base, u32 obj)
2134 {
2135 __be32 raw = cpu_to_be32(obj);
2136
2137 return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
2138 }
2139 EXPORT_SYMBOL_GPL(xdr_encode_word);
2140
2141 /* Returns 0 on success, or else a negative error code. */
xdr_xcode_array2(const struct xdr_buf * buf,unsigned int base,struct xdr_array2_desc * desc,int encode)2142 static int xdr_xcode_array2(const struct xdr_buf *buf, unsigned int base,
2143 struct xdr_array2_desc *desc, int encode)
2144 {
2145 char *elem = NULL, *c;
2146 unsigned int copied = 0, todo, avail_here;
2147 struct page **ppages = NULL;
2148 int err;
2149
2150 if (encode) {
2151 if (xdr_encode_word(buf, base, desc->array_len) != 0)
2152 return -EINVAL;
2153 } else {
2154 if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
2155 desc->array_len > desc->array_maxlen ||
2156 (unsigned long) base + 4 + desc->array_len *
2157 desc->elem_size > buf->len)
2158 return -EINVAL;
2159 }
2160 base += 4;
2161
2162 if (!desc->xcode)
2163 return 0;
2164
2165 todo = desc->array_len * desc->elem_size;
2166
2167 /* process head */
2168 if (todo && base < buf->head->iov_len) {
2169 c = buf->head->iov_base + base;
2170 avail_here = min_t(unsigned int, todo,
2171 buf->head->iov_len - base);
2172 todo -= avail_here;
2173
2174 while (avail_here >= desc->elem_size) {
2175 err = desc->xcode(desc, c);
2176 if (err)
2177 goto out;
2178 c += desc->elem_size;
2179 avail_here -= desc->elem_size;
2180 }
2181 if (avail_here) {
2182 if (!elem) {
2183 elem = kmalloc(desc->elem_size, GFP_KERNEL);
2184 err = -ENOMEM;
2185 if (!elem)
2186 goto out;
2187 }
2188 if (encode) {
2189 err = desc->xcode(desc, elem);
2190 if (err)
2191 goto out;
2192 memcpy(c, elem, avail_here);
2193 } else
2194 memcpy(elem, c, avail_here);
2195 copied = avail_here;
2196 }
2197 base = buf->head->iov_len; /* align to start of pages */
2198 }
2199
2200 /* process pages array */
2201 base -= buf->head->iov_len;
2202 if (todo && base < buf->page_len) {
2203 unsigned int avail_page;
2204
2205 avail_here = min(todo, buf->page_len - base);
2206 todo -= avail_here;
2207
2208 base += buf->page_base;
2209 ppages = buf->pages + (base >> PAGE_SHIFT);
2210 base &= ~PAGE_MASK;
2211 avail_page = min_t(unsigned int, PAGE_SIZE - base,
2212 avail_here);
2213 c = kmap(*ppages) + base;
2214
2215 while (avail_here) {
2216 avail_here -= avail_page;
2217 if (copied || avail_page < desc->elem_size) {
2218 unsigned int l = min(avail_page,
2219 desc->elem_size - copied);
2220 if (!elem) {
2221 elem = kmalloc(desc->elem_size,
2222 GFP_KERNEL);
2223 err = -ENOMEM;
2224 if (!elem)
2225 goto out;
2226 }
2227 if (encode) {
2228 if (!copied) {
2229 err = desc->xcode(desc, elem);
2230 if (err)
2231 goto out;
2232 }
2233 memcpy(c, elem + copied, l);
2234 copied += l;
2235 if (copied == desc->elem_size)
2236 copied = 0;
2237 } else {
2238 memcpy(elem + copied, c, l);
2239 copied += l;
2240 if (copied == desc->elem_size) {
2241 err = desc->xcode(desc, elem);
2242 if (err)
2243 goto out;
2244 copied = 0;
2245 }
2246 }
2247 avail_page -= l;
2248 c += l;
2249 }
2250 while (avail_page >= desc->elem_size) {
2251 err = desc->xcode(desc, c);
2252 if (err)
2253 goto out;
2254 c += desc->elem_size;
2255 avail_page -= desc->elem_size;
2256 }
2257 if (avail_page) {
2258 unsigned int l = min(avail_page,
2259 desc->elem_size - copied);
2260 if (!elem) {
2261 elem = kmalloc(desc->elem_size,
2262 GFP_KERNEL);
2263 err = -ENOMEM;
2264 if (!elem)
2265 goto out;
2266 }
2267 if (encode) {
2268 if (!copied) {
2269 err = desc->xcode(desc, elem);
2270 if (err)
2271 goto out;
2272 }
2273 memcpy(c, elem + copied, l);
2274 copied += l;
2275 if (copied == desc->elem_size)
2276 copied = 0;
2277 } else {
2278 memcpy(elem + copied, c, l);
2279 copied += l;
2280 if (copied == desc->elem_size) {
2281 err = desc->xcode(desc, elem);
2282 if (err)
2283 goto out;
2284 copied = 0;
2285 }
2286 }
2287 }
2288 if (avail_here) {
2289 kunmap(*ppages);
2290 ppages++;
2291 c = kmap(*ppages);
2292 }
2293
2294 avail_page = min(avail_here,
2295 (unsigned int) PAGE_SIZE);
2296 }
2297 base = buf->page_len; /* align to start of tail */
2298 }
2299
2300 /* process tail */
2301 base -= buf->page_len;
2302 if (todo) {
2303 c = buf->tail->iov_base + base;
2304 if (copied) {
2305 unsigned int l = desc->elem_size - copied;
2306
2307 if (encode)
2308 memcpy(c, elem + copied, l);
2309 else {
2310 memcpy(elem + copied, c, l);
2311 err = desc->xcode(desc, elem);
2312 if (err)
2313 goto out;
2314 }
2315 todo -= l;
2316 c += l;
2317 }
2318 while (todo) {
2319 err = desc->xcode(desc, c);
2320 if (err)
2321 goto out;
2322 c += desc->elem_size;
2323 todo -= desc->elem_size;
2324 }
2325 }
2326 err = 0;
2327
2328 out:
2329 kfree(elem);
2330 if (ppages)
2331 kunmap(*ppages);
2332 return err;
2333 }
2334
xdr_decode_array2(const struct xdr_buf * buf,unsigned int base,struct xdr_array2_desc * desc)2335 int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base,
2336 struct xdr_array2_desc *desc)
2337 {
2338 if (base >= buf->len)
2339 return -EINVAL;
2340
2341 return xdr_xcode_array2(buf, base, desc, 0);
2342 }
2343 EXPORT_SYMBOL_GPL(xdr_decode_array2);
2344
xdr_encode_array2(const struct xdr_buf * buf,unsigned int base,struct xdr_array2_desc * desc)2345 int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base,
2346 struct xdr_array2_desc *desc)
2347 {
2348 if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
2349 buf->head->iov_len + buf->page_len + buf->tail->iov_len)
2350 return -EINVAL;
2351
2352 return xdr_xcode_array2(buf, base, desc, 1);
2353 }
2354 EXPORT_SYMBOL_GPL(xdr_encode_array2);
2355
2356 /**
2357 * xdr_stream_decode_string_dup - Decode and duplicate variable length string
2358 * @xdr: pointer to xdr_stream
2359 * @str: location to store pointer to string
2360 * @maxlen: maximum acceptable string length
2361 * @gfp_flags: GFP mask to use
2362 *
2363 * Return values:
2364 * On success, returns length of NUL-terminated string stored in *@ptr
2365 * %-EBADMSG on XDR buffer overflow
2366 * %-EMSGSIZE if the size of the string would exceed @maxlen
2367 * %-ENOMEM on memory allocation failure
2368 */
xdr_stream_decode_string_dup(struct xdr_stream * xdr,char ** str,size_t maxlen,gfp_t gfp_flags)2369 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
2370 size_t maxlen, gfp_t gfp_flags)
2371 {
2372 void *p;
2373 ssize_t ret;
2374
2375 ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
2376 if (ret > 0) {
2377 char *s = kmemdup_nul(p, ret, gfp_flags);
2378 if (s != NULL) {
2379 *str = s;
2380 return strlen(s);
2381 }
2382 ret = -ENOMEM;
2383 }
2384 *str = NULL;
2385 return ret;
2386 }
2387 EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);
2388
2389 /**
2390 * xdr_stream_decode_opaque_auth - Decode struct opaque_auth (RFC5531 S8.2)
2391 * @xdr: pointer to xdr_stream
2392 * @flavor: location to store decoded flavor
2393 * @body: location to store decode body
2394 * @body_len: location to store length of decoded body
2395 *
2396 * Return values:
2397 * On success, returns the number of buffer bytes consumed
2398 * %-EBADMSG on XDR buffer overflow
2399 * %-EMSGSIZE if the decoded size of the body field exceeds 400 octets
2400 */
xdr_stream_decode_opaque_auth(struct xdr_stream * xdr,u32 * flavor,void ** body,unsigned int * body_len)2401 ssize_t xdr_stream_decode_opaque_auth(struct xdr_stream *xdr, u32 *flavor,
2402 void **body, unsigned int *body_len)
2403 {
2404 ssize_t ret, len;
2405
2406 len = xdr_stream_decode_u32(xdr, flavor);
2407 if (unlikely(len < 0))
2408 return len;
2409 ret = xdr_stream_decode_opaque_inline(xdr, body, RPC_MAX_AUTH_SIZE);
2410 if (unlikely(ret < 0))
2411 return ret;
2412 *body_len = ret;
2413 return len + ret;
2414 }
2415 EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_auth);
2416
2417 /**
2418 * xdr_stream_encode_opaque_auth - Encode struct opaque_auth (RFC5531 S8.2)
2419 * @xdr: pointer to xdr_stream
2420 * @flavor: verifier flavor to encode
2421 * @body: content of body to encode
2422 * @body_len: length of body to encode
2423 *
2424 * Return values:
2425 * On success, returns length in bytes of XDR buffer consumed
2426 * %-EBADMSG on XDR buffer overflow
2427 * %-EMSGSIZE if the size of @body exceeds 400 octets
2428 */
xdr_stream_encode_opaque_auth(struct xdr_stream * xdr,u32 flavor,void * body,unsigned int body_len)2429 ssize_t xdr_stream_encode_opaque_auth(struct xdr_stream *xdr, u32 flavor,
2430 void *body, unsigned int body_len)
2431 {
2432 ssize_t ret, len;
2433
2434 if (unlikely(body_len > RPC_MAX_AUTH_SIZE))
2435 return -EMSGSIZE;
2436 len = xdr_stream_encode_u32(xdr, flavor);
2437 if (unlikely(len < 0))
2438 return len;
2439 ret = xdr_stream_encode_opaque(xdr, body, body_len);
2440 if (unlikely(ret < 0))
2441 return ret;
2442 return len + ret;
2443 }
2444 EXPORT_SYMBOL_GPL(xdr_stream_encode_opaque_auth);
2445