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