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