1 /* SPDX-License-Identifier: GPL-2.0 */
3 * XDR standard data types and function declarations
5 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
8 * RFC 4506 "XDR: External Data Representation Standard", May 2006
11 #ifndef _SUNRPC_XDR_H_
12 #define _SUNRPC_XDR_H_
14 #include <linux/uio.h>
15 #include <asm/byteorder.h>
16 #include <asm/unaligned.h>
17 #include <linux/scatterlist.h>
25 #define XDR_QUADLEN(l) (((l) + 3) >> 2)
28 * Generic opaque `network object.' At the kernel level, this type
29 * is used only by lockd.
31 #define XDR_MAX_NETOBJ 1024
38 * Basic structure for transmission/reception of a client XDR message.
39 * Features a header (for a linear buffer containing RPC headers
40 * and the data payload for short messages), and then an array of
42 * The tail iovec allows you to append data after the page array. Its
43 * main interest is for appending padding to the pages in order to
44 * satisfy the int_32-alignment requirements in RFC1832.
46 * For the future, we might want to string several of these together
47 * in a list if anybody wants to make use of NFSv4 COMPOUND
48 * operations and/or has a need for scatter/gather involving pages.
51 struct kvec head[1], /* RPC header + non-page data */
52 tail[1]; /* Appended after page data */
55 struct page ** pages; /* Array of pages */
56 unsigned int page_base, /* Start of page data */
57 page_len, /* Length of page data */
58 flags; /* Flags for data disposition */
59 #define XDRBUF_READ 0x01 /* target of file read */
60 #define XDRBUF_WRITE 0x02 /* source of file write */
61 #define XDRBUF_SPARSE_PAGES 0x04 /* Page array is sparse */
63 unsigned int buflen, /* Total length of storage buffer */
64 len; /* Length of XDR encoded message */
68 xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
70 buf->head[0].iov_base = start;
71 buf->head[0].iov_len = len;
72 buf->tail[0].iov_len = 0;
84 #define xdr_zero cpu_to_be32(0)
85 #define xdr_one cpu_to_be32(1)
86 #define xdr_two cpu_to_be32(2)
88 #define rpc_auth_null cpu_to_be32(RPC_AUTH_NULL)
89 #define rpc_auth_unix cpu_to_be32(RPC_AUTH_UNIX)
90 #define rpc_auth_short cpu_to_be32(RPC_AUTH_SHORT)
91 #define rpc_auth_gss cpu_to_be32(RPC_AUTH_GSS)
93 #define rpc_call cpu_to_be32(RPC_CALL)
94 #define rpc_reply cpu_to_be32(RPC_REPLY)
96 #define rpc_msg_accepted cpu_to_be32(RPC_MSG_ACCEPTED)
98 #define rpc_success cpu_to_be32(RPC_SUCCESS)
99 #define rpc_prog_unavail cpu_to_be32(RPC_PROG_UNAVAIL)
100 #define rpc_prog_mismatch cpu_to_be32(RPC_PROG_MISMATCH)
101 #define rpc_proc_unavail cpu_to_be32(RPC_PROC_UNAVAIL)
102 #define rpc_garbage_args cpu_to_be32(RPC_GARBAGE_ARGS)
103 #define rpc_system_err cpu_to_be32(RPC_SYSTEM_ERR)
104 #define rpc_drop_reply cpu_to_be32(RPC_DROP_REPLY)
106 #define rpc_mismatch cpu_to_be32(RPC_MISMATCH)
107 #define rpc_auth_error cpu_to_be32(RPC_AUTH_ERROR)
109 #define rpc_auth_ok cpu_to_be32(RPC_AUTH_OK)
110 #define rpc_autherr_badcred cpu_to_be32(RPC_AUTH_BADCRED)
111 #define rpc_autherr_rejectedcred cpu_to_be32(RPC_AUTH_REJECTEDCRED)
112 #define rpc_autherr_badverf cpu_to_be32(RPC_AUTH_BADVERF)
113 #define rpc_autherr_rejectedverf cpu_to_be32(RPC_AUTH_REJECTEDVERF)
114 #define rpc_autherr_tooweak cpu_to_be32(RPC_AUTH_TOOWEAK)
115 #define rpcsec_gsserr_credproblem cpu_to_be32(RPCSEC_GSS_CREDPROBLEM)
116 #define rpcsec_gsserr_ctxproblem cpu_to_be32(RPCSEC_GSS_CTXPROBLEM)
119 * Miscellaneous XDR helper functions
121 __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int len);
122 __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int len);
123 __be32 *xdr_encode_string(__be32 *p, const char *s);
124 __be32 *xdr_decode_string_inplace(__be32 *p, char **sp, unsigned int *lenp,
125 unsigned int maxlen);
126 __be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *);
127 __be32 *xdr_decode_netobj(__be32 *p, struct xdr_netobj *);
129 void xdr_inline_pages(struct xdr_buf *, unsigned int,
130 struct page **, unsigned int, unsigned int);
131 void xdr_terminate_string(struct xdr_buf *, const u32);
132 size_t xdr_buf_pagecount(struct xdr_buf *buf);
133 int xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp);
134 void xdr_free_bvec(struct xdr_buf *buf);
136 static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len)
138 return xdr_encode_opaque(p, s, len);
142 * Decode 64bit quantities (NFSv3 support)
144 static inline __be32 *
145 xdr_encode_hyper(__be32 *p, __u64 val)
147 put_unaligned_be64(val, p);
151 static inline __be32 *
152 xdr_decode_hyper(__be32 *p, __u64 *valp)
154 *valp = get_unaligned_be64(p);
158 static inline __be32 *
159 xdr_decode_opaque_fixed(__be32 *p, void *ptr, unsigned int len)
162 return p + XDR_QUADLEN(len);
165 static inline void xdr_netobj_dup(struct xdr_netobj *dst,
166 struct xdr_netobj *src, gfp_t gfp_mask)
168 dst->data = kmemdup(src->data, src->len, gfp_mask);
173 * Adjust kvec to reflect end of xdr'ed data (RPC client XDR)
176 xdr_adjust_iovec(struct kvec *iov, __be32 *p)
178 return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base);
182 * XDR buffer helper functions
184 extern void xdr_shift_buf(struct xdr_buf *, size_t);
185 extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
186 extern int xdr_buf_subsegment(const struct xdr_buf *buf, struct xdr_buf *subbuf,
187 unsigned int base, unsigned int len);
188 extern void xdr_buf_trim(struct xdr_buf *, unsigned int);
189 extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
190 extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
192 extern int xdr_encode_word(struct xdr_buf *, unsigned int, u32);
193 extern int xdr_decode_word(struct xdr_buf *, unsigned int, u32 *);
195 struct xdr_array2_desc;
196 typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
197 struct xdr_array2_desc {
198 unsigned int elem_size;
199 unsigned int array_len;
200 unsigned int array_maxlen;
201 xdr_xcode_elem_t xcode;
204 extern int xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
205 struct xdr_array2_desc *desc);
206 extern int xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
207 struct xdr_array2_desc *desc);
208 extern void _copy_from_pages(char *p, struct page **pages, size_t pgbase,
212 * Provide some simple tools for XDR buffer overflow-checking etc.
215 __be32 *p; /* start of available buffer */
216 struct xdr_buf *buf; /* XDR buffer to read/write */
218 __be32 *end; /* end of available buffer space */
219 struct kvec *iov; /* pointer to the current kvec */
220 struct kvec scratch; /* Scratch buffer */
221 struct page **page_ptr; /* pointer to the current page */
222 unsigned int nwords; /* Remaining decode buffer length */
224 struct rpc_rqst *rqst; /* For debugging */
228 * These are the xdr_stream style generic XDR encode and decode functions.
230 typedef void (*kxdreproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
232 typedef int (*kxdrdproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
235 extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf,
236 __be32 *p, struct rpc_rqst *rqst);
237 extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes);
238 extern int xdr_reserve_space_vec(struct xdr_stream *xdr, struct kvec *vec,
240 extern void xdr_commit_encode(struct xdr_stream *xdr);
241 extern void xdr_truncate_encode(struct xdr_stream *xdr, size_t len);
242 extern int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen);
243 extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages,
244 unsigned int base, unsigned int len);
245 extern unsigned int xdr_stream_pos(const struct xdr_stream *xdr);
246 extern unsigned int xdr_page_pos(const struct xdr_stream *xdr);
247 extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf,
248 __be32 *p, struct rpc_rqst *rqst);
249 extern void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
250 struct page **pages, unsigned int len);
251 extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes);
252 extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
253 extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
254 extern int xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data);
255 extern uint64_t xdr_align_data(struct xdr_stream *, uint64_t, uint32_t);
256 extern uint64_t xdr_expand_hole(struct xdr_stream *, uint64_t, uint64_t);
257 extern bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
261 * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
262 * @xdr: pointer to xdr_stream struct
263 * @buf: pointer to an empty buffer
264 * @buflen: size of 'buf'
266 * The scratch buffer is used when decoding from an array of pages.
267 * If an xdr_inline_decode() call spans across page boundaries, then
268 * we copy the data into the scratch buffer in order to allow linear
272 xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
274 xdr->scratch.iov_base = buf;
275 xdr->scratch.iov_len = buflen;
279 * xdr_set_scratch_page - Attach a scratch buffer for decoding data
280 * @xdr: pointer to xdr_stream struct
281 * @page: an anonymous page
283 * See xdr_set_scratch_buffer().
286 xdr_set_scratch_page(struct xdr_stream *xdr, struct page *page)
288 xdr_set_scratch_buffer(xdr, page_address(page), PAGE_SIZE);
292 * xdr_reset_scratch_buffer - Clear scratch buffer information
293 * @xdr: pointer to xdr_stream struct
295 * See xdr_set_scratch_buffer().
298 xdr_reset_scratch_buffer(struct xdr_stream *xdr)
300 xdr_set_scratch_buffer(xdr, NULL, 0);
304 * xdr_stream_remaining - Return the number of bytes remaining in the stream
305 * @xdr: pointer to struct xdr_stream
308 * Number of bytes remaining in @xdr before xdr->end
311 xdr_stream_remaining(const struct xdr_stream *xdr)
313 return xdr->nwords << 2;
316 ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr,
318 ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
319 size_t maxlen, gfp_t gfp_flags);
320 ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str,
322 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
323 size_t maxlen, gfp_t gfp_flags);
325 * xdr_align_size - Calculate padded size of an object
326 * @n: Size of an object being XDR encoded (in bytes)
329 * Size (in bytes) of the object including xdr padding
332 xdr_align_size(size_t n)
334 const size_t mask = sizeof(__u32) - 1;
336 return (n + mask) & ~mask;
340 * xdr_pad_size - Calculate size of an object's pad
341 * @n: Size of an object being XDR encoded (in bytes)
343 * This implementation avoids the need for conditional
344 * branches or modulo division.
347 * Size (in bytes) of the needed XDR pad
349 static inline size_t xdr_pad_size(size_t n)
351 return xdr_align_size(n) - n;
355 * xdr_stream_encode_item_present - Encode a "present" list item
356 * @xdr: pointer to xdr_stream
359 * On success, returns length in bytes of XDR buffer consumed
360 * %-EMSGSIZE on XDR buffer overflow
362 static inline ssize_t xdr_stream_encode_item_present(struct xdr_stream *xdr)
364 const size_t len = sizeof(__be32);
365 __be32 *p = xdr_reserve_space(xdr, len);
374 * xdr_stream_encode_item_absent - Encode a "not present" list item
375 * @xdr: pointer to xdr_stream
378 * On success, returns length in bytes of XDR buffer consumed
379 * %-EMSGSIZE on XDR buffer overflow
381 static inline int xdr_stream_encode_item_absent(struct xdr_stream *xdr)
383 const size_t len = sizeof(__be32);
384 __be32 *p = xdr_reserve_space(xdr, len);
393 * xdr_stream_encode_u32 - Encode a 32-bit integer
394 * @xdr: pointer to xdr_stream
395 * @n: integer to encode
398 * On success, returns length in bytes of XDR buffer consumed
399 * %-EMSGSIZE on XDR buffer overflow
401 static inline ssize_t
402 xdr_stream_encode_u32(struct xdr_stream *xdr, __u32 n)
404 const size_t len = sizeof(n);
405 __be32 *p = xdr_reserve_space(xdr, len);
414 * xdr_stream_encode_u64 - Encode a 64-bit integer
415 * @xdr: pointer to xdr_stream
416 * @n: 64-bit integer to encode
419 * On success, returns length in bytes of XDR buffer consumed
420 * %-EMSGSIZE on XDR buffer overflow
422 static inline ssize_t
423 xdr_stream_encode_u64(struct xdr_stream *xdr, __u64 n)
425 const size_t len = sizeof(n);
426 __be32 *p = xdr_reserve_space(xdr, len);
430 xdr_encode_hyper(p, n);
435 * xdr_stream_encode_opaque_inline - Encode opaque xdr data
436 * @xdr: pointer to xdr_stream
437 * @ptr: pointer to void pointer
438 * @len: size of object
441 * On success, returns length in bytes of XDR buffer consumed
442 * %-EMSGSIZE on XDR buffer overflow
444 static inline ssize_t
445 xdr_stream_encode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t len)
447 size_t count = sizeof(__u32) + xdr_align_size(len);
448 __be32 *p = xdr_reserve_space(xdr, count);
454 xdr_encode_opaque(p, NULL, len);
460 * xdr_stream_encode_opaque_fixed - Encode fixed length opaque xdr data
461 * @xdr: pointer to xdr_stream
462 * @ptr: pointer to opaque data object
463 * @len: size of object pointed to by @ptr
466 * On success, returns length in bytes of XDR buffer consumed
467 * %-EMSGSIZE on XDR buffer overflow
469 static inline ssize_t
470 xdr_stream_encode_opaque_fixed(struct xdr_stream *xdr, const void *ptr, size_t len)
472 __be32 *p = xdr_reserve_space(xdr, len);
476 xdr_encode_opaque_fixed(p, ptr, len);
477 return xdr_align_size(len);
481 * xdr_stream_encode_opaque - Encode variable length opaque xdr data
482 * @xdr: pointer to xdr_stream
483 * @ptr: pointer to opaque data object
484 * @len: size of object pointed to by @ptr
487 * On success, returns length in bytes of XDR buffer consumed
488 * %-EMSGSIZE on XDR buffer overflow
490 static inline ssize_t
491 xdr_stream_encode_opaque(struct xdr_stream *xdr, const void *ptr, size_t len)
493 size_t count = sizeof(__u32) + xdr_align_size(len);
494 __be32 *p = xdr_reserve_space(xdr, count);
498 xdr_encode_opaque(p, ptr, len);
503 * xdr_stream_encode_uint32_array - Encode variable length array of integers
504 * @xdr: pointer to xdr_stream
505 * @array: array of integers
506 * @array_size: number of elements in @array
509 * On success, returns length in bytes of XDR buffer consumed
510 * %-EMSGSIZE on XDR buffer overflow
512 static inline ssize_t
513 xdr_stream_encode_uint32_array(struct xdr_stream *xdr,
514 const __u32 *array, size_t array_size)
516 ssize_t ret = (array_size+1) * sizeof(__u32);
517 __be32 *p = xdr_reserve_space(xdr, ret);
521 *p++ = cpu_to_be32(array_size);
522 for (; array_size > 0; p++, array++, array_size--)
523 *p = cpu_to_be32p(array);
528 * xdr_item_is_absent - symbolically handle XDR discriminators
529 * @p: pointer to undecoded discriminator
532 * %true if the following XDR item is absent
533 * %false if the following XDR item is present
535 static inline bool xdr_item_is_absent(const __be32 *p)
537 return *p == xdr_zero;
541 * xdr_item_is_present - symbolically handle XDR discriminators
542 * @p: pointer to undecoded discriminator
545 * %true if the following XDR item is present
546 * %false if the following XDR item is absent
548 static inline bool xdr_item_is_present(const __be32 *p)
550 return *p != xdr_zero;
554 * xdr_stream_decode_bool - Decode a boolean
555 * @xdr: pointer to xdr_stream
556 * @ptr: pointer to a u32 in which to store the result
560 * %-EBADMSG on XDR buffer overflow
562 static inline ssize_t
563 xdr_stream_decode_bool(struct xdr_stream *xdr, __u32 *ptr)
565 const size_t count = sizeof(*ptr);
566 __be32 *p = xdr_inline_decode(xdr, count);
570 *ptr = (*p != xdr_zero);
575 * xdr_stream_decode_u32 - Decode a 32-bit integer
576 * @xdr: pointer to xdr_stream
577 * @ptr: location to store integer
581 * %-EBADMSG on XDR buffer overflow
583 static inline ssize_t
584 xdr_stream_decode_u32(struct xdr_stream *xdr, __u32 *ptr)
586 const size_t count = sizeof(*ptr);
587 __be32 *p = xdr_inline_decode(xdr, count);
591 *ptr = be32_to_cpup(p);
596 * xdr_stream_decode_u64 - Decode a 64-bit integer
597 * @xdr: pointer to xdr_stream
598 * @ptr: location to store 64-bit integer
602 * %-EBADMSG on XDR buffer overflow
604 static inline ssize_t
605 xdr_stream_decode_u64(struct xdr_stream *xdr, __u64 *ptr)
607 const size_t count = sizeof(*ptr);
608 __be32 *p = xdr_inline_decode(xdr, count);
612 xdr_decode_hyper(p, ptr);
617 * xdr_stream_decode_opaque_fixed - Decode fixed length opaque xdr data
618 * @xdr: pointer to xdr_stream
619 * @ptr: location to store data
620 * @len: size of buffer pointed to by @ptr
623 * On success, returns size of object stored in @ptr
624 * %-EBADMSG on XDR buffer overflow
626 static inline ssize_t
627 xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len)
629 __be32 *p = xdr_inline_decode(xdr, len);
633 xdr_decode_opaque_fixed(p, ptr, len);
638 * xdr_stream_decode_opaque_inline - Decode variable length opaque xdr data
639 * @xdr: pointer to xdr_stream
640 * @ptr: location to store pointer to opaque data
641 * @maxlen: maximum acceptable object size
643 * Note: the pointer stored in @ptr cannot be assumed valid after the XDR
644 * buffer has been destroyed, or even after calling xdr_inline_decode()
645 * on @xdr. It is therefore expected that the object it points to should
646 * be processed immediately.
649 * On success, returns size of object stored in *@ptr
650 * %-EBADMSG on XDR buffer overflow
651 * %-EMSGSIZE if the size of the object would exceed @maxlen
653 static inline ssize_t
654 xdr_stream_decode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t maxlen)
660 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
663 p = xdr_inline_decode(xdr, len);
666 if (unlikely(len > maxlen))
674 * xdr_stream_decode_uint32_array - Decode variable length array of integers
675 * @xdr: pointer to xdr_stream
676 * @array: location to store the integer array or NULL
677 * @array_size: number of elements to store
680 * On success, returns number of elements stored in @array
681 * %-EBADMSG on XDR buffer overflow
682 * %-EMSGSIZE if the size of the array exceeds @array_size
684 static inline ssize_t
685 xdr_stream_decode_uint32_array(struct xdr_stream *xdr,
686 __u32 *array, size_t array_size)
692 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
694 p = xdr_inline_decode(xdr, len * sizeof(*p));
699 if (len <= array_size) {
700 if (len < array_size)
701 memset(array+len, 0, (array_size-len)*sizeof(*array));
706 for (; array_size > 0; p++, array++, array_size--)
707 *array = be32_to_cpup(p);
711 #endif /* _SUNRPC_XDR_H_ */