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