at v6.0 770 lines 22 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2/* 3 * XDR standard data types and function declarations 4 * 5 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de> 6 * 7 * Based on: 8 * RFC 4506 "XDR: External Data Representation Standard", May 2006 9 */ 10 11#ifndef _SUNRPC_XDR_H_ 12#define _SUNRPC_XDR_H_ 13 14#include <linux/uio.h> 15#include <asm/byteorder.h> 16#include <asm/unaligned.h> 17#include <linux/scatterlist.h> 18 19struct bio_vec; 20struct rpc_rqst; 21 22/* 23 * Size of an XDR encoding unit in bytes, i.e. 32 bits, 24 * as defined in Section 3 of RFC 4506. All encoded 25 * XDR data items are aligned on a boundary of 32 bits. 26 */ 27#define XDR_UNIT sizeof(__be32) 28 29/* 30 * Buffer adjustment 31 */ 32#define XDR_QUADLEN(l) (((l) + 3) >> 2) 33 34/* 35 * Generic opaque `network object.' 36 */ 37#define XDR_MAX_NETOBJ 1024 38struct xdr_netobj { 39 unsigned int len; 40 u8 * data; 41}; 42 43/* 44 * Basic structure for transmission/reception of a client XDR message. 45 * Features a header (for a linear buffer containing RPC headers 46 * and the data payload for short messages), and then an array of 47 * pages. 48 * The tail iovec allows you to append data after the page array. Its 49 * main interest is for appending padding to the pages in order to 50 * satisfy the int_32-alignment requirements in RFC1832. 51 * 52 * For the future, we might want to string several of these together 53 * in a list if anybody wants to make use of NFSv4 COMPOUND 54 * operations and/or has a need for scatter/gather involving pages. 55 */ 56struct xdr_buf { 57 struct kvec head[1], /* RPC header + non-page data */ 58 tail[1]; /* Appended after page data */ 59 60 struct bio_vec *bvec; 61 struct page ** pages; /* Array of pages */ 62 unsigned int page_base, /* Start of page data */ 63 page_len, /* Length of page data */ 64 flags; /* Flags for data disposition */ 65#define XDRBUF_READ 0x01 /* target of file read */ 66#define XDRBUF_WRITE 0x02 /* source of file write */ 67#define XDRBUF_SPARSE_PAGES 0x04 /* Page array is sparse */ 68 69 unsigned int buflen, /* Total length of storage buffer */ 70 len; /* Length of XDR encoded message */ 71}; 72 73static inline void 74xdr_buf_init(struct xdr_buf *buf, void *start, size_t len) 75{ 76 buf->head[0].iov_base = start; 77 buf->head[0].iov_len = len; 78 buf->tail[0].iov_len = 0; 79 buf->pages = NULL; 80 buf->page_len = 0; 81 buf->flags = 0; 82 buf->len = 0; 83 buf->buflen = len; 84} 85 86/* 87 * pre-xdr'ed macros. 88 */ 89 90#define xdr_zero cpu_to_be32(0) 91#define xdr_one cpu_to_be32(1) 92#define xdr_two cpu_to_be32(2) 93 94#define rpc_auth_null cpu_to_be32(RPC_AUTH_NULL) 95#define rpc_auth_unix cpu_to_be32(RPC_AUTH_UNIX) 96#define rpc_auth_short cpu_to_be32(RPC_AUTH_SHORT) 97#define rpc_auth_gss cpu_to_be32(RPC_AUTH_GSS) 98#define rpc_auth_tls cpu_to_be32(RPC_AUTH_TLS) 99 100#define rpc_call cpu_to_be32(RPC_CALL) 101#define rpc_reply cpu_to_be32(RPC_REPLY) 102 103#define rpc_msg_accepted cpu_to_be32(RPC_MSG_ACCEPTED) 104 105#define rpc_success cpu_to_be32(RPC_SUCCESS) 106#define rpc_prog_unavail cpu_to_be32(RPC_PROG_UNAVAIL) 107#define rpc_prog_mismatch cpu_to_be32(RPC_PROG_MISMATCH) 108#define rpc_proc_unavail cpu_to_be32(RPC_PROC_UNAVAIL) 109#define rpc_garbage_args cpu_to_be32(RPC_GARBAGE_ARGS) 110#define rpc_system_err cpu_to_be32(RPC_SYSTEM_ERR) 111#define rpc_drop_reply cpu_to_be32(RPC_DROP_REPLY) 112 113#define rpc_mismatch cpu_to_be32(RPC_MISMATCH) 114#define rpc_auth_error cpu_to_be32(RPC_AUTH_ERROR) 115 116#define rpc_auth_ok cpu_to_be32(RPC_AUTH_OK) 117#define rpc_autherr_badcred cpu_to_be32(RPC_AUTH_BADCRED) 118#define rpc_autherr_rejectedcred cpu_to_be32(RPC_AUTH_REJECTEDCRED) 119#define rpc_autherr_badverf cpu_to_be32(RPC_AUTH_BADVERF) 120#define rpc_autherr_rejectedverf cpu_to_be32(RPC_AUTH_REJECTEDVERF) 121#define rpc_autherr_tooweak cpu_to_be32(RPC_AUTH_TOOWEAK) 122#define rpcsec_gsserr_credproblem cpu_to_be32(RPCSEC_GSS_CREDPROBLEM) 123#define rpcsec_gsserr_ctxproblem cpu_to_be32(RPCSEC_GSS_CTXPROBLEM) 124 125/* 126 * Miscellaneous XDR helper functions 127 */ 128__be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int len); 129__be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int len); 130__be32 *xdr_encode_string(__be32 *p, const char *s); 131__be32 *xdr_decode_string_inplace(__be32 *p, char **sp, unsigned int *lenp, 132 unsigned int maxlen); 133__be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *); 134__be32 *xdr_decode_netobj(__be32 *p, struct xdr_netobj *); 135 136void xdr_inline_pages(struct xdr_buf *, unsigned int, 137 struct page **, unsigned int, unsigned int); 138void xdr_terminate_string(const struct xdr_buf *, const u32); 139size_t xdr_buf_pagecount(const struct xdr_buf *buf); 140int xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp); 141void xdr_free_bvec(struct xdr_buf *buf); 142 143static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len) 144{ 145 return xdr_encode_opaque(p, s, len); 146} 147 148/* 149 * Decode 64bit quantities (NFSv3 support) 150 */ 151static inline __be32 * 152xdr_encode_hyper(__be32 *p, __u64 val) 153{ 154 put_unaligned_be64(val, p); 155 return p + 2; 156} 157 158static inline __be32 * 159xdr_decode_hyper(__be32 *p, __u64 *valp) 160{ 161 *valp = get_unaligned_be64(p); 162 return p + 2; 163} 164 165static inline __be32 * 166xdr_decode_opaque_fixed(__be32 *p, void *ptr, unsigned int len) 167{ 168 memcpy(ptr, p, len); 169 return p + XDR_QUADLEN(len); 170} 171 172static inline void xdr_netobj_dup(struct xdr_netobj *dst, 173 struct xdr_netobj *src, gfp_t gfp_mask) 174{ 175 dst->data = kmemdup(src->data, src->len, gfp_mask); 176 dst->len = src->len; 177} 178 179/* 180 * Adjust kvec to reflect end of xdr'ed data (RPC client XDR) 181 */ 182static inline int 183xdr_adjust_iovec(struct kvec *iov, __be32 *p) 184{ 185 return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base); 186} 187 188/* 189 * XDR buffer helper functions 190 */ 191extern void xdr_shift_buf(struct xdr_buf *, size_t); 192extern void xdr_buf_from_iov(const struct kvec *, struct xdr_buf *); 193extern int xdr_buf_subsegment(const struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int); 194extern void xdr_buf_trim(struct xdr_buf *, unsigned int); 195extern int read_bytes_from_xdr_buf(const struct xdr_buf *, unsigned int, void *, unsigned int); 196extern int write_bytes_to_xdr_buf(const struct xdr_buf *, unsigned int, void *, unsigned int); 197 198extern int xdr_encode_word(const struct xdr_buf *, unsigned int, u32); 199extern int xdr_decode_word(const struct xdr_buf *, unsigned int, u32 *); 200 201struct xdr_array2_desc; 202typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem); 203struct xdr_array2_desc { 204 unsigned int elem_size; 205 unsigned int array_len; 206 unsigned int array_maxlen; 207 xdr_xcode_elem_t xcode; 208}; 209 210extern int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base, 211 struct xdr_array2_desc *desc); 212extern int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base, 213 struct xdr_array2_desc *desc); 214extern void _copy_from_pages(char *p, struct page **pages, size_t pgbase, 215 size_t len); 216 217/* 218 * Provide some simple tools for XDR buffer overflow-checking etc. 219 */ 220struct xdr_stream { 221 __be32 *p; /* start of available buffer */ 222 struct xdr_buf *buf; /* XDR buffer to read/write */ 223 224 __be32 *end; /* end of available buffer space */ 225 struct kvec *iov; /* pointer to the current kvec */ 226 struct kvec scratch; /* Scratch buffer */ 227 struct page **page_ptr; /* pointer to the current page */ 228 unsigned int nwords; /* Remaining decode buffer length */ 229 230 struct rpc_rqst *rqst; /* For debugging */ 231}; 232 233/* 234 * These are the xdr_stream style generic XDR encode and decode functions. 235 */ 236typedef void (*kxdreproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr, 237 const void *obj); 238typedef int (*kxdrdproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr, 239 void *obj); 240 241extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, 242 __be32 *p, struct rpc_rqst *rqst); 243extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes); 244extern int xdr_reserve_space_vec(struct xdr_stream *xdr, struct kvec *vec, 245 size_t nbytes); 246extern void __xdr_commit_encode(struct xdr_stream *xdr); 247extern void xdr_truncate_encode(struct xdr_stream *xdr, size_t len); 248extern int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen); 249extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, 250 unsigned int base, unsigned int len); 251extern unsigned int xdr_stream_pos(const struct xdr_stream *xdr); 252extern unsigned int xdr_page_pos(const struct xdr_stream *xdr); 253extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, 254 __be32 *p, struct rpc_rqst *rqst); 255extern void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf, 256 struct page **pages, unsigned int len); 257extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes); 258extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len); 259extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len); 260extern int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data); 261extern void xdr_set_pagelen(struct xdr_stream *, unsigned int len); 262extern bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf, 263 unsigned int len); 264extern unsigned int xdr_stream_move_subsegment(struct xdr_stream *xdr, unsigned int offset, 265 unsigned int target, unsigned int length); 266extern unsigned int xdr_stream_zero(struct xdr_stream *xdr, unsigned int offset, 267 unsigned int length); 268 269/** 270 * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data. 271 * @xdr: pointer to xdr_stream struct 272 * @buf: pointer to an empty buffer 273 * @buflen: size of 'buf' 274 * 275 * The scratch buffer is used when decoding from an array of pages. 276 * If an xdr_inline_decode() call spans across page boundaries, then 277 * we copy the data into the scratch buffer in order to allow linear 278 * access. 279 */ 280static inline void 281xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen) 282{ 283 xdr->scratch.iov_base = buf; 284 xdr->scratch.iov_len = buflen; 285} 286 287/** 288 * xdr_set_scratch_page - Attach a scratch buffer for decoding data 289 * @xdr: pointer to xdr_stream struct 290 * @page: an anonymous page 291 * 292 * See xdr_set_scratch_buffer(). 293 */ 294static inline void 295xdr_set_scratch_page(struct xdr_stream *xdr, struct page *page) 296{ 297 xdr_set_scratch_buffer(xdr, page_address(page), PAGE_SIZE); 298} 299 300/** 301 * xdr_reset_scratch_buffer - Clear scratch buffer information 302 * @xdr: pointer to xdr_stream struct 303 * 304 * See xdr_set_scratch_buffer(). 305 */ 306static inline void 307xdr_reset_scratch_buffer(struct xdr_stream *xdr) 308{ 309 xdr_set_scratch_buffer(xdr, NULL, 0); 310} 311 312/** 313 * xdr_commit_encode - Ensure all data is written to xdr->buf 314 * @xdr: pointer to xdr_stream 315 * 316 * Handle encoding across page boundaries by giving the caller a 317 * temporary location to write to, then later copying the data into 318 * place. __xdr_commit_encode() does that copying. 319 */ 320static inline void xdr_commit_encode(struct xdr_stream *xdr) 321{ 322 if (unlikely(xdr->scratch.iov_len)) 323 __xdr_commit_encode(xdr); 324} 325 326/** 327 * xdr_stream_remaining - Return the number of bytes remaining in the stream 328 * @xdr: pointer to struct xdr_stream 329 * 330 * Return value: 331 * Number of bytes remaining in @xdr before xdr->end 332 */ 333static inline size_t 334xdr_stream_remaining(const struct xdr_stream *xdr) 335{ 336 return xdr->nwords << 2; 337} 338 339ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, 340 size_t size); 341ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr, 342 size_t maxlen, gfp_t gfp_flags); 343ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, 344 size_t size); 345ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str, 346 size_t maxlen, gfp_t gfp_flags); 347/** 348 * xdr_align_size - Calculate padded size of an object 349 * @n: Size of an object being XDR encoded (in bytes) 350 * 351 * Return value: 352 * Size (in bytes) of the object including xdr padding 353 */ 354static inline size_t 355xdr_align_size(size_t n) 356{ 357 const size_t mask = XDR_UNIT - 1; 358 359 return (n + mask) & ~mask; 360} 361 362/** 363 * xdr_pad_size - Calculate size of an object's pad 364 * @n: Size of an object being XDR encoded (in bytes) 365 * 366 * This implementation avoids the need for conditional 367 * branches or modulo division. 368 * 369 * Return value: 370 * Size (in bytes) of the needed XDR pad 371 */ 372static inline size_t xdr_pad_size(size_t n) 373{ 374 return xdr_align_size(n) - n; 375} 376 377/** 378 * xdr_stream_encode_item_present - Encode a "present" list item 379 * @xdr: pointer to xdr_stream 380 * 381 * Return values: 382 * On success, returns length in bytes of XDR buffer consumed 383 * %-EMSGSIZE on XDR buffer overflow 384 */ 385static inline ssize_t xdr_stream_encode_item_present(struct xdr_stream *xdr) 386{ 387 const size_t len = XDR_UNIT; 388 __be32 *p = xdr_reserve_space(xdr, len); 389 390 if (unlikely(!p)) 391 return -EMSGSIZE; 392 *p = xdr_one; 393 return len; 394} 395 396/** 397 * xdr_stream_encode_item_absent - Encode a "not present" list item 398 * @xdr: pointer to xdr_stream 399 * 400 * Return values: 401 * On success, returns length in bytes of XDR buffer consumed 402 * %-EMSGSIZE on XDR buffer overflow 403 */ 404static inline int xdr_stream_encode_item_absent(struct xdr_stream *xdr) 405{ 406 const size_t len = XDR_UNIT; 407 __be32 *p = xdr_reserve_space(xdr, len); 408 409 if (unlikely(!p)) 410 return -EMSGSIZE; 411 *p = xdr_zero; 412 return len; 413} 414 415/** 416 * xdr_encode_bool - Encode a boolean item 417 * @p: address in a buffer into which to encode 418 * @n: boolean value to encode 419 * 420 * Return value: 421 * Address of item following the encoded boolean 422 */ 423static inline __be32 *xdr_encode_bool(__be32 *p, u32 n) 424{ 425 *p++ = n ? xdr_one : xdr_zero; 426 return p; 427} 428 429/** 430 * xdr_stream_encode_bool - Encode a boolean item 431 * @xdr: pointer to xdr_stream 432 * @n: boolean value to encode 433 * 434 * Return values: 435 * On success, returns length in bytes of XDR buffer consumed 436 * %-EMSGSIZE on XDR buffer overflow 437 */ 438static inline int xdr_stream_encode_bool(struct xdr_stream *xdr, __u32 n) 439{ 440 const size_t len = XDR_UNIT; 441 __be32 *p = xdr_reserve_space(xdr, len); 442 443 if (unlikely(!p)) 444 return -EMSGSIZE; 445 xdr_encode_bool(p, n); 446 return len; 447} 448 449/** 450 * xdr_stream_encode_u32 - Encode a 32-bit integer 451 * @xdr: pointer to xdr_stream 452 * @n: integer to encode 453 * 454 * Return values: 455 * On success, returns length in bytes of XDR buffer consumed 456 * %-EMSGSIZE on XDR buffer overflow 457 */ 458static inline ssize_t 459xdr_stream_encode_u32(struct xdr_stream *xdr, __u32 n) 460{ 461 const size_t len = sizeof(n); 462 __be32 *p = xdr_reserve_space(xdr, len); 463 464 if (unlikely(!p)) 465 return -EMSGSIZE; 466 *p = cpu_to_be32(n); 467 return len; 468} 469 470/** 471 * xdr_stream_encode_u64 - Encode a 64-bit integer 472 * @xdr: pointer to xdr_stream 473 * @n: 64-bit integer to encode 474 * 475 * Return values: 476 * On success, returns length in bytes of XDR buffer consumed 477 * %-EMSGSIZE on XDR buffer overflow 478 */ 479static inline ssize_t 480xdr_stream_encode_u64(struct xdr_stream *xdr, __u64 n) 481{ 482 const size_t len = sizeof(n); 483 __be32 *p = xdr_reserve_space(xdr, len); 484 485 if (unlikely(!p)) 486 return -EMSGSIZE; 487 xdr_encode_hyper(p, n); 488 return len; 489} 490 491/** 492 * xdr_stream_encode_opaque_inline - Encode opaque xdr data 493 * @xdr: pointer to xdr_stream 494 * @ptr: pointer to void pointer 495 * @len: size of object 496 * 497 * Return values: 498 * On success, returns length in bytes of XDR buffer consumed 499 * %-EMSGSIZE on XDR buffer overflow 500 */ 501static inline ssize_t 502xdr_stream_encode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t len) 503{ 504 size_t count = sizeof(__u32) + xdr_align_size(len); 505 __be32 *p = xdr_reserve_space(xdr, count); 506 507 if (unlikely(!p)) { 508 *ptr = NULL; 509 return -EMSGSIZE; 510 } 511 xdr_encode_opaque(p, NULL, len); 512 *ptr = ++p; 513 return count; 514} 515 516/** 517 * xdr_stream_encode_opaque_fixed - Encode fixed length opaque xdr data 518 * @xdr: pointer to xdr_stream 519 * @ptr: pointer to opaque data object 520 * @len: size of object pointed to by @ptr 521 * 522 * Return values: 523 * On success, returns length in bytes of XDR buffer consumed 524 * %-EMSGSIZE on XDR buffer overflow 525 */ 526static inline ssize_t 527xdr_stream_encode_opaque_fixed(struct xdr_stream *xdr, const void *ptr, size_t len) 528{ 529 __be32 *p = xdr_reserve_space(xdr, len); 530 531 if (unlikely(!p)) 532 return -EMSGSIZE; 533 xdr_encode_opaque_fixed(p, ptr, len); 534 return xdr_align_size(len); 535} 536 537/** 538 * xdr_stream_encode_opaque - Encode variable length opaque xdr data 539 * @xdr: pointer to xdr_stream 540 * @ptr: pointer to opaque data object 541 * @len: size of object pointed to by @ptr 542 * 543 * Return values: 544 * On success, returns length in bytes of XDR buffer consumed 545 * %-EMSGSIZE on XDR buffer overflow 546 */ 547static inline ssize_t 548xdr_stream_encode_opaque(struct xdr_stream *xdr, const void *ptr, size_t len) 549{ 550 size_t count = sizeof(__u32) + xdr_align_size(len); 551 __be32 *p = xdr_reserve_space(xdr, count); 552 553 if (unlikely(!p)) 554 return -EMSGSIZE; 555 xdr_encode_opaque(p, ptr, len); 556 return count; 557} 558 559/** 560 * xdr_stream_encode_uint32_array - Encode variable length array of integers 561 * @xdr: pointer to xdr_stream 562 * @array: array of integers 563 * @array_size: number of elements in @array 564 * 565 * Return values: 566 * On success, returns length in bytes of XDR buffer consumed 567 * %-EMSGSIZE on XDR buffer overflow 568 */ 569static inline ssize_t 570xdr_stream_encode_uint32_array(struct xdr_stream *xdr, 571 const __u32 *array, size_t array_size) 572{ 573 ssize_t ret = (array_size+1) * sizeof(__u32); 574 __be32 *p = xdr_reserve_space(xdr, ret); 575 576 if (unlikely(!p)) 577 return -EMSGSIZE; 578 *p++ = cpu_to_be32(array_size); 579 for (; array_size > 0; p++, array++, array_size--) 580 *p = cpu_to_be32p(array); 581 return ret; 582} 583 584/** 585 * xdr_item_is_absent - symbolically handle XDR discriminators 586 * @p: pointer to undecoded discriminator 587 * 588 * Return values: 589 * %true if the following XDR item is absent 590 * %false if the following XDR item is present 591 */ 592static inline bool xdr_item_is_absent(const __be32 *p) 593{ 594 return *p == xdr_zero; 595} 596 597/** 598 * xdr_item_is_present - symbolically handle XDR discriminators 599 * @p: pointer to undecoded discriminator 600 * 601 * Return values: 602 * %true if the following XDR item is present 603 * %false if the following XDR item is absent 604 */ 605static inline bool xdr_item_is_present(const __be32 *p) 606{ 607 return *p != xdr_zero; 608} 609 610/** 611 * xdr_stream_decode_bool - Decode a boolean 612 * @xdr: pointer to xdr_stream 613 * @ptr: pointer to a u32 in which to store the result 614 * 615 * Return values: 616 * %0 on success 617 * %-EBADMSG on XDR buffer overflow 618 */ 619static inline ssize_t 620xdr_stream_decode_bool(struct xdr_stream *xdr, __u32 *ptr) 621{ 622 const size_t count = sizeof(*ptr); 623 __be32 *p = xdr_inline_decode(xdr, count); 624 625 if (unlikely(!p)) 626 return -EBADMSG; 627 *ptr = (*p != xdr_zero); 628 return 0; 629} 630 631/** 632 * xdr_stream_decode_u32 - Decode a 32-bit integer 633 * @xdr: pointer to xdr_stream 634 * @ptr: location to store integer 635 * 636 * Return values: 637 * %0 on success 638 * %-EBADMSG on XDR buffer overflow 639 */ 640static inline ssize_t 641xdr_stream_decode_u32(struct xdr_stream *xdr, __u32 *ptr) 642{ 643 const size_t count = sizeof(*ptr); 644 __be32 *p = xdr_inline_decode(xdr, count); 645 646 if (unlikely(!p)) 647 return -EBADMSG; 648 *ptr = be32_to_cpup(p); 649 return 0; 650} 651 652/** 653 * xdr_stream_decode_u64 - Decode a 64-bit integer 654 * @xdr: pointer to xdr_stream 655 * @ptr: location to store 64-bit integer 656 * 657 * Return values: 658 * %0 on success 659 * %-EBADMSG on XDR buffer overflow 660 */ 661static inline ssize_t 662xdr_stream_decode_u64(struct xdr_stream *xdr, __u64 *ptr) 663{ 664 const size_t count = sizeof(*ptr); 665 __be32 *p = xdr_inline_decode(xdr, count); 666 667 if (unlikely(!p)) 668 return -EBADMSG; 669 xdr_decode_hyper(p, ptr); 670 return 0; 671} 672 673/** 674 * xdr_stream_decode_opaque_fixed - Decode fixed length opaque xdr data 675 * @xdr: pointer to xdr_stream 676 * @ptr: location to store data 677 * @len: size of buffer pointed to by @ptr 678 * 679 * Return values: 680 * On success, returns size of object stored in @ptr 681 * %-EBADMSG on XDR buffer overflow 682 */ 683static inline ssize_t 684xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len) 685{ 686 __be32 *p = xdr_inline_decode(xdr, len); 687 688 if (unlikely(!p)) 689 return -EBADMSG; 690 xdr_decode_opaque_fixed(p, ptr, len); 691 return len; 692} 693 694/** 695 * xdr_stream_decode_opaque_inline - Decode variable length opaque xdr data 696 * @xdr: pointer to xdr_stream 697 * @ptr: location to store pointer to opaque data 698 * @maxlen: maximum acceptable object size 699 * 700 * Note: the pointer stored in @ptr cannot be assumed valid after the XDR 701 * buffer has been destroyed, or even after calling xdr_inline_decode() 702 * on @xdr. It is therefore expected that the object it points to should 703 * be processed immediately. 704 * 705 * Return values: 706 * On success, returns size of object stored in *@ptr 707 * %-EBADMSG on XDR buffer overflow 708 * %-EMSGSIZE if the size of the object would exceed @maxlen 709 */ 710static inline ssize_t 711xdr_stream_decode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t maxlen) 712{ 713 __be32 *p; 714 __u32 len; 715 716 *ptr = NULL; 717 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0)) 718 return -EBADMSG; 719 if (len != 0) { 720 p = xdr_inline_decode(xdr, len); 721 if (unlikely(!p)) 722 return -EBADMSG; 723 if (unlikely(len > maxlen)) 724 return -EMSGSIZE; 725 *ptr = p; 726 } 727 return len; 728} 729 730/** 731 * xdr_stream_decode_uint32_array - Decode variable length array of integers 732 * @xdr: pointer to xdr_stream 733 * @array: location to store the integer array or NULL 734 * @array_size: number of elements to store 735 * 736 * Return values: 737 * On success, returns number of elements stored in @array 738 * %-EBADMSG on XDR buffer overflow 739 * %-EMSGSIZE if the size of the array exceeds @array_size 740 */ 741static inline ssize_t 742xdr_stream_decode_uint32_array(struct xdr_stream *xdr, 743 __u32 *array, size_t array_size) 744{ 745 __be32 *p; 746 __u32 len; 747 ssize_t retval; 748 749 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0)) 750 return -EBADMSG; 751 if (len > SIZE_MAX / sizeof(*p)) 752 return -EBADMSG; 753 p = xdr_inline_decode(xdr, len * sizeof(*p)); 754 if (unlikely(!p)) 755 return -EBADMSG; 756 if (array == NULL) 757 return len; 758 if (len <= array_size) { 759 if (len < array_size) 760 memset(array+len, 0, (array_size-len)*sizeof(*array)); 761 array_size = len; 762 retval = len; 763 } else 764 retval = -EMSGSIZE; 765 for (; array_size > 0; p++, array++, array_size--) 766 *array = be32_to_cpup(p); 767 return retval; 768} 769 770#endif /* _SUNRPC_XDR_H_ */