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.'
30 #define XDR_MAX_NETOBJ 1024
37 * Basic structure for transmission/reception of a client XDR message.
38 * Features a header (for a linear buffer containing RPC headers
39 * and the data payload for short messages), and then an array of
41 * The tail iovec allows you to append data after the page array. Its
42 * main interest is for appending padding to the pages in order to
43 * satisfy the int_32-alignment requirements in RFC1832.
45 * For the future, we might want to string several of these together
46 * in a list if anybody wants to make use of NFSv4 COMPOUND
47 * operations and/or has a need for scatter/gather involving pages.
50 struct kvec head[1], /* RPC header + non-page data */
51 tail[1]; /* Appended after page data */
54 struct page ** pages; /* Array of pages */
55 unsigned int page_base, /* Start of page data */
56 page_len, /* Length of page data */
57 flags; /* Flags for data disposition */
58 #define XDRBUF_READ 0x01 /* target of file read */
59 #define XDRBUF_WRITE 0x02 /* source of file write */
60 #define XDRBUF_SPARSE_PAGES 0x04 /* Page array is sparse */
62 unsigned int buflen, /* Total length of storage buffer */
63 len; /* Length of XDR encoded message */
67 xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
69 buf->head[0].iov_base = start;
70 buf->head[0].iov_len = len;
71 buf->tail[0].iov_len = 0;
83 #define xdr_zero cpu_to_be32(0)
84 #define xdr_one cpu_to_be32(1)
85 #define xdr_two cpu_to_be32(2)
87 #define rpc_auth_null cpu_to_be32(RPC_AUTH_NULL)
88 #define rpc_auth_unix cpu_to_be32(RPC_AUTH_UNIX)
89 #define rpc_auth_short cpu_to_be32(RPC_AUTH_SHORT)
90 #define rpc_auth_gss cpu_to_be32(RPC_AUTH_GSS)
92 #define rpc_call cpu_to_be32(RPC_CALL)
93 #define rpc_reply cpu_to_be32(RPC_REPLY)
95 #define rpc_msg_accepted cpu_to_be32(RPC_MSG_ACCEPTED)
97 #define rpc_success cpu_to_be32(RPC_SUCCESS)
98 #define rpc_prog_unavail cpu_to_be32(RPC_PROG_UNAVAIL)
99 #define rpc_prog_mismatch cpu_to_be32(RPC_PROG_MISMATCH)
100 #define rpc_proc_unavail cpu_to_be32(RPC_PROC_UNAVAIL)
101 #define rpc_garbage_args cpu_to_be32(RPC_GARBAGE_ARGS)
102 #define rpc_system_err cpu_to_be32(RPC_SYSTEM_ERR)
103 #define rpc_drop_reply cpu_to_be32(RPC_DROP_REPLY)
105 #define rpc_mismatch cpu_to_be32(RPC_MISMATCH)
106 #define rpc_auth_error cpu_to_be32(RPC_AUTH_ERROR)
108 #define rpc_auth_ok cpu_to_be32(RPC_AUTH_OK)
109 #define rpc_autherr_badcred cpu_to_be32(RPC_AUTH_BADCRED)
110 #define rpc_autherr_rejectedcred cpu_to_be32(RPC_AUTH_REJECTEDCRED)
111 #define rpc_autherr_badverf cpu_to_be32(RPC_AUTH_BADVERF)
112 #define rpc_autherr_rejectedverf cpu_to_be32(RPC_AUTH_REJECTEDVERF)
113 #define rpc_autherr_tooweak cpu_to_be32(RPC_AUTH_TOOWEAK)
114 #define rpcsec_gsserr_credproblem cpu_to_be32(RPCSEC_GSS_CREDPROBLEM)
115 #define rpcsec_gsserr_ctxproblem cpu_to_be32(RPCSEC_GSS_CTXPROBLEM)
118 * Miscellaneous XDR helper functions
120 __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int len);
121 __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int len);
122 __be32 *xdr_encode_string(__be32 *p, const char *s);
123 __be32 *xdr_decode_string_inplace(__be32 *p, char **sp, unsigned int *lenp,
124 unsigned int maxlen);
125 __be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *);
126 __be32 *xdr_decode_netobj(__be32 *p, struct xdr_netobj *);
128 void xdr_inline_pages(struct xdr_buf *, unsigned int,
129 struct page **, unsigned int, unsigned int);
130 void xdr_terminate_string(struct xdr_buf *, const u32);
131 size_t xdr_buf_pagecount(struct xdr_buf *buf);
132 int xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp);
133 void xdr_free_bvec(struct xdr_buf *buf);
135 static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len)
137 return xdr_encode_opaque(p, s, len);
141 * Decode 64bit quantities (NFSv3 support)
143 static inline __be32 *
144 xdr_encode_hyper(__be32 *p, __u64 val)
146 put_unaligned_be64(val, p);
150 static inline __be32 *
151 xdr_decode_hyper(__be32 *p, __u64 *valp)
153 *valp = get_unaligned_be64(p);
157 static inline __be32 *
158 xdr_decode_opaque_fixed(__be32 *p, void *ptr, unsigned int len)
161 return p + XDR_QUADLEN(len);
164 static inline void xdr_netobj_dup(struct xdr_netobj *dst,
165 struct xdr_netobj *src, gfp_t gfp_mask)
167 dst->data = kmemdup(src->data, src->len, gfp_mask);
172 * Adjust kvec to reflect end of xdr'ed data (RPC client XDR)
175 xdr_adjust_iovec(struct kvec *iov, __be32 *p)
177 return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base);
181 * XDR buffer helper functions
183 extern void xdr_shift_buf(struct xdr_buf *, size_t);
184 extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
185 extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
186 extern void xdr_buf_trim(struct xdr_buf *, unsigned int);
187 extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
188 extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
190 extern int xdr_encode_word(struct xdr_buf *, unsigned int, u32);
191 extern int xdr_decode_word(struct xdr_buf *, unsigned int, u32 *);
193 struct xdr_array2_desc;
194 typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
195 struct xdr_array2_desc {
196 unsigned int elem_size;
197 unsigned int array_len;
198 unsigned int array_maxlen;
199 xdr_xcode_elem_t xcode;
202 extern int xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
203 struct xdr_array2_desc *desc);
204 extern int xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
205 struct xdr_array2_desc *desc);
206 extern void _copy_from_pages(char *p, struct page **pages, size_t pgbase,
210 * Provide some simple tools for XDR buffer overflow-checking etc.
213 __be32 *p; /* start of available buffer */
214 struct xdr_buf *buf; /* XDR buffer to read/write */
216 __be32 *end; /* end of available buffer space */
217 struct kvec *iov; /* pointer to the current kvec */
218 struct kvec scratch; /* Scratch buffer */
219 struct page **page_ptr; /* pointer to the current page */
220 unsigned int nwords; /* Remaining decode buffer length */
222 struct rpc_rqst *rqst; /* For debugging */
226 * These are the xdr_stream style generic XDR encode and decode functions.
228 typedef void (*kxdreproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
230 typedef int (*kxdrdproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
233 extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf,
234 __be32 *p, struct rpc_rqst *rqst);
235 extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes);
236 extern int xdr_reserve_space_vec(struct xdr_stream *xdr, struct kvec *vec,
238 extern void xdr_commit_encode(struct xdr_stream *xdr);
239 extern void xdr_truncate_encode(struct xdr_stream *xdr, size_t len);
240 extern int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen);
241 extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages,
242 unsigned int base, unsigned int len);
243 extern unsigned int xdr_stream_pos(const struct xdr_stream *xdr);
244 extern unsigned int xdr_page_pos(const struct xdr_stream *xdr);
245 extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf,
246 __be32 *p, struct rpc_rqst *rqst);
247 extern void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
248 struct page **pages, unsigned int len);
249 extern void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen);
250 extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes);
251 extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
252 extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
253 extern int xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data);
254 extern uint64_t xdr_align_data(struct xdr_stream *, uint64_t, uint32_t);
255 extern uint64_t xdr_expand_hole(struct xdr_stream *, uint64_t, uint64_t);
258 * xdr_stream_remaining - Return the number of bytes remaining in the stream
259 * @xdr: pointer to struct xdr_stream
262 * Number of bytes remaining in @xdr before xdr->end
265 xdr_stream_remaining(const struct xdr_stream *xdr)
267 return xdr->nwords << 2;
270 ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr,
272 ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
273 size_t maxlen, gfp_t gfp_flags);
274 ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str,
276 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
277 size_t maxlen, gfp_t gfp_flags);
279 * xdr_align_size - Calculate padded size of an object
280 * @n: Size of an object being XDR encoded (in bytes)
283 * Size (in bytes) of the object including xdr padding
286 xdr_align_size(size_t n)
288 const size_t mask = sizeof(__u32) - 1;
290 return (n + mask) & ~mask;
294 * xdr_pad_size - Calculate size of an object's pad
295 * @n: Size of an object being XDR encoded (in bytes)
297 * This implementation avoids the need for conditional
298 * branches or modulo division.
301 * Size (in bytes) of the needed XDR pad
303 static inline size_t xdr_pad_size(size_t n)
305 return xdr_align_size(n) - n;
309 * xdr_stream_encode_item_present - Encode a "present" list item
310 * @xdr: pointer to xdr_stream
313 * On success, returns length in bytes of XDR buffer consumed
314 * %-EMSGSIZE on XDR buffer overflow
316 static inline ssize_t xdr_stream_encode_item_present(struct xdr_stream *xdr)
318 const size_t len = sizeof(__be32);
319 __be32 *p = xdr_reserve_space(xdr, len);
328 * xdr_stream_encode_item_absent - Encode a "not present" list item
329 * @xdr: pointer to xdr_stream
332 * On success, returns length in bytes of XDR buffer consumed
333 * %-EMSGSIZE on XDR buffer overflow
335 static inline int xdr_stream_encode_item_absent(struct xdr_stream *xdr)
337 const size_t len = sizeof(__be32);
338 __be32 *p = xdr_reserve_space(xdr, len);
347 * xdr_stream_encode_u32 - Encode a 32-bit integer
348 * @xdr: pointer to xdr_stream
349 * @n: integer to encode
352 * On success, returns length in bytes of XDR buffer consumed
353 * %-EMSGSIZE on XDR buffer overflow
355 static inline ssize_t
356 xdr_stream_encode_u32(struct xdr_stream *xdr, __u32 n)
358 const size_t len = sizeof(n);
359 __be32 *p = xdr_reserve_space(xdr, len);
368 * xdr_stream_encode_u64 - Encode a 64-bit integer
369 * @xdr: pointer to xdr_stream
370 * @n: 64-bit integer to encode
373 * On success, returns length in bytes of XDR buffer consumed
374 * %-EMSGSIZE on XDR buffer overflow
376 static inline ssize_t
377 xdr_stream_encode_u64(struct xdr_stream *xdr, __u64 n)
379 const size_t len = sizeof(n);
380 __be32 *p = xdr_reserve_space(xdr, len);
384 xdr_encode_hyper(p, n);
389 * xdr_stream_encode_opaque_inline - Encode opaque xdr data
390 * @xdr: pointer to xdr_stream
391 * @ptr: pointer to void pointer
392 * @len: size of object
395 * On success, returns length in bytes of XDR buffer consumed
396 * %-EMSGSIZE on XDR buffer overflow
398 static inline ssize_t
399 xdr_stream_encode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t len)
401 size_t count = sizeof(__u32) + xdr_align_size(len);
402 __be32 *p = xdr_reserve_space(xdr, count);
408 xdr_encode_opaque(p, NULL, len);
414 * xdr_stream_encode_opaque_fixed - Encode fixed length opaque xdr data
415 * @xdr: pointer to xdr_stream
416 * @ptr: pointer to opaque data object
417 * @len: size of object pointed to by @ptr
420 * On success, returns length in bytes of XDR buffer consumed
421 * %-EMSGSIZE on XDR buffer overflow
423 static inline ssize_t
424 xdr_stream_encode_opaque_fixed(struct xdr_stream *xdr, const void *ptr, size_t len)
426 __be32 *p = xdr_reserve_space(xdr, len);
430 xdr_encode_opaque_fixed(p, ptr, len);
431 return xdr_align_size(len);
435 * xdr_stream_encode_opaque - Encode variable length opaque xdr data
436 * @xdr: pointer to xdr_stream
437 * @ptr: pointer to opaque data object
438 * @len: size of object pointed to by @ptr
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(struct xdr_stream *xdr, const void *ptr, size_t len)
447 size_t count = sizeof(__u32) + xdr_align_size(len);
448 __be32 *p = xdr_reserve_space(xdr, count);
452 xdr_encode_opaque(p, ptr, len);
457 * xdr_stream_encode_uint32_array - Encode variable length array of integers
458 * @xdr: pointer to xdr_stream
459 * @array: array of integers
460 * @array_size: number of elements in @array
463 * On success, returns length in bytes of XDR buffer consumed
464 * %-EMSGSIZE on XDR buffer overflow
466 static inline ssize_t
467 xdr_stream_encode_uint32_array(struct xdr_stream *xdr,
468 const __u32 *array, size_t array_size)
470 ssize_t ret = (array_size+1) * sizeof(__u32);
471 __be32 *p = xdr_reserve_space(xdr, ret);
475 *p++ = cpu_to_be32(array_size);
476 for (; array_size > 0; p++, array++, array_size--)
477 *p = cpu_to_be32p(array);
482 * xdr_item_is_absent - symbolically handle XDR discriminators
483 * @p: pointer to undecoded discriminator
486 * %true if the following XDR item is absent
487 * %false if the following XDR item is present
489 static inline bool xdr_item_is_absent(const __be32 *p)
491 return *p == xdr_zero;
495 * xdr_item_is_present - symbolically handle XDR discriminators
496 * @p: pointer to undecoded discriminator
499 * %true if the following XDR item is present
500 * %false if the following XDR item is absent
502 static inline bool xdr_item_is_present(const __be32 *p)
504 return *p != xdr_zero;
508 * xdr_stream_decode_u32 - Decode a 32-bit integer
509 * @xdr: pointer to xdr_stream
510 * @ptr: location to store integer
514 * %-EBADMSG on XDR buffer overflow
516 static inline ssize_t
517 xdr_stream_decode_u32(struct xdr_stream *xdr, __u32 *ptr)
519 const size_t count = sizeof(*ptr);
520 __be32 *p = xdr_inline_decode(xdr, count);
524 *ptr = be32_to_cpup(p);
529 * xdr_stream_decode_opaque_fixed - Decode fixed length opaque xdr data
530 * @xdr: pointer to xdr_stream
531 * @ptr: location to store data
532 * @len: size of buffer pointed to by @ptr
535 * On success, returns size of object stored in @ptr
536 * %-EBADMSG on XDR buffer overflow
538 static inline ssize_t
539 xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len)
541 __be32 *p = xdr_inline_decode(xdr, len);
545 xdr_decode_opaque_fixed(p, ptr, len);
550 * xdr_stream_decode_opaque_inline - Decode variable length opaque xdr data
551 * @xdr: pointer to xdr_stream
552 * @ptr: location to store pointer to opaque data
553 * @maxlen: maximum acceptable object size
555 * Note: the pointer stored in @ptr cannot be assumed valid after the XDR
556 * buffer has been destroyed, or even after calling xdr_inline_decode()
557 * on @xdr. It is therefore expected that the object it points to should
558 * be processed immediately.
561 * On success, returns size of object stored in *@ptr
562 * %-EBADMSG on XDR buffer overflow
563 * %-EMSGSIZE if the size of the object would exceed @maxlen
565 static inline ssize_t
566 xdr_stream_decode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t maxlen)
572 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
575 p = xdr_inline_decode(xdr, len);
578 if (unlikely(len > maxlen))
586 * xdr_stream_decode_uint32_array - Decode variable length array of integers
587 * @xdr: pointer to xdr_stream
588 * @array: location to store the integer array or NULL
589 * @array_size: number of elements to store
592 * On success, returns number of elements stored in @array
593 * %-EBADMSG on XDR buffer overflow
594 * %-EMSGSIZE if the size of the array exceeds @array_size
596 static inline ssize_t
597 xdr_stream_decode_uint32_array(struct xdr_stream *xdr,
598 __u32 *array, size_t array_size)
604 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
606 if (len > SIZE_MAX / sizeof(*p))
608 p = xdr_inline_decode(xdr, len * sizeof(*p));
613 if (len <= array_size) {
614 if (len < array_size)
615 memset(array+len, 0, (array_size-len)*sizeof(*array));
620 for (; array_size > 0; p++, array++, array_size--)
621 *array = be32_to_cpup(p);
625 #endif /* _SUNRPC_XDR_H_ */