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