xref: /linux-6.15/include/linux/sunrpc/xdr.h (revision b1cd3d82)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * XDR standard data types and function declarations
4  *
5  * Copyright (C) 1995-1997 Olaf Kirch <[email protected]>
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 
19 struct bio_vec;
20 struct rpc_rqst;
21 
22 /*
23  * Buffer adjustment
24  */
25 #define XDR_QUADLEN(l)		(((l) + 3) >> 2)
26 
27 /*
28  * Generic opaque `network object.'
29  */
30 #define XDR_MAX_NETOBJ		1024
31 struct xdr_netobj {
32 	unsigned int		len;
33 	u8 *			data;
34 };
35 
36 /*
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
40  * pages.
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.
44  *
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.
48  */
49 struct xdr_buf {
50 	struct kvec	head[1],	/* RPC header + non-page data */
51 			tail[1];	/* Appended after page data */
52 
53 	struct bio_vec	*bvec;
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 */
61 
62 	unsigned int	buflen,		/* Total length of storage buffer */
63 			len;		/* Length of XDR encoded message */
64 };
65 
66 static inline void
67 xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
68 {
69 	buf->head[0].iov_base = start;
70 	buf->head[0].iov_len = len;
71 	buf->tail[0].iov_len = 0;
72 	buf->pages = NULL;
73 	buf->page_len = 0;
74 	buf->flags = 0;
75 	buf->len = 0;
76 	buf->buflen = len;
77 }
78 
79 /*
80  * pre-xdr'ed macros.
81  */
82 
83 #define	xdr_zero	cpu_to_be32(0)
84 #define	xdr_one		cpu_to_be32(1)
85 #define	xdr_two		cpu_to_be32(2)
86 
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)
91 
92 #define	rpc_call	cpu_to_be32(RPC_CALL)
93 #define	rpc_reply	cpu_to_be32(RPC_REPLY)
94 
95 #define	rpc_msg_accepted	cpu_to_be32(RPC_MSG_ACCEPTED)
96 
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)
104 
105 #define	rpc_mismatch		cpu_to_be32(RPC_MISMATCH)
106 #define	rpc_auth_error		cpu_to_be32(RPC_AUTH_ERROR)
107 
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)
116 
117 /*
118  * Miscellaneous XDR helper functions
119  */
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 *);
127 
128 void	xdr_inline_pages(struct xdr_buf *, unsigned int,
129 			 struct page **, unsigned int, unsigned int);
130 void	xdr_terminate_string(const struct xdr_buf *, const u32);
131 size_t	xdr_buf_pagecount(const 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);
134 
135 static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len)
136 {
137 	return xdr_encode_opaque(p, s, len);
138 }
139 
140 /*
141  * Decode 64bit quantities (NFSv3 support)
142  */
143 static inline __be32 *
144 xdr_encode_hyper(__be32 *p, __u64 val)
145 {
146 	put_unaligned_be64(val, p);
147 	return p + 2;
148 }
149 
150 static inline __be32 *
151 xdr_decode_hyper(__be32 *p, __u64 *valp)
152 {
153 	*valp = get_unaligned_be64(p);
154 	return p + 2;
155 }
156 
157 static inline __be32 *
158 xdr_decode_opaque_fixed(__be32 *p, void *ptr, unsigned int len)
159 {
160 	memcpy(ptr, p, len);
161 	return p + XDR_QUADLEN(len);
162 }
163 
164 static inline void xdr_netobj_dup(struct xdr_netobj *dst,
165 				  struct xdr_netobj *src, gfp_t gfp_mask)
166 {
167 	dst->data = kmemdup(src->data, src->len, gfp_mask);
168 	dst->len = src->len;
169 }
170 
171 /*
172  * Adjust kvec to reflect end of xdr'ed data (RPC client XDR)
173  */
174 static inline int
175 xdr_adjust_iovec(struct kvec *iov, __be32 *p)
176 {
177 	return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base);
178 }
179 
180 /*
181  * XDR buffer helper functions
182  */
183 extern void xdr_shift_buf(struct xdr_buf *, size_t);
184 extern void xdr_buf_from_iov(const struct kvec *, struct xdr_buf *);
185 extern int xdr_buf_subsegment(const 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(const struct xdr_buf *, unsigned int, void *, unsigned int);
188 extern int write_bytes_to_xdr_buf(const struct xdr_buf *, unsigned int, void *, unsigned int);
189 
190 extern int xdr_encode_word(const struct xdr_buf *, unsigned int, u32);
191 extern int xdr_decode_word(const struct xdr_buf *, unsigned int, u32 *);
192 
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;
200 };
201 
202 extern int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base,
203 			     struct xdr_array2_desc *desc);
204 extern int xdr_encode_array2(const 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,
207 			     size_t len);
208 
209 /*
210  * Provide some simple tools for XDR buffer overflow-checking etc.
211  */
212 struct xdr_stream {
213 	__be32 *p;		/* start of available buffer */
214 	struct xdr_buf *buf;	/* XDR buffer to read/write */
215 
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 */
221 
222 	struct rpc_rqst *rqst;	/* For debugging */
223 };
224 
225 /*
226  * These are the xdr_stream style generic XDR encode and decode functions.
227  */
228 typedef void	(*kxdreproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
229 		const void *obj);
230 typedef int	(*kxdrdproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
231 		void *obj);
232 
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,
237 		size_t nbytes);
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 __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes);
250 extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
251 extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
252 extern int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data);
253 extern unsigned int xdr_align_data(struct xdr_stream *, unsigned int offset, unsigned int length);
254 extern unsigned int xdr_expand_hole(struct xdr_stream *, unsigned int offset, unsigned int length);
255 extern bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
256 				  unsigned int len);
257 
258 /**
259  * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
260  * @xdr: pointer to xdr_stream struct
261  * @buf: pointer to an empty buffer
262  * @buflen: size of 'buf'
263  *
264  * The scratch buffer is used when decoding from an array of pages.
265  * If an xdr_inline_decode() call spans across page boundaries, then
266  * we copy the data into the scratch buffer in order to allow linear
267  * access.
268  */
269 static inline void
270 xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
271 {
272 	xdr->scratch.iov_base = buf;
273 	xdr->scratch.iov_len = buflen;
274 }
275 
276 /**
277  * xdr_set_scratch_page - Attach a scratch buffer for decoding data
278  * @xdr: pointer to xdr_stream struct
279  * @page: an anonymous page
280  *
281  * See xdr_set_scratch_buffer().
282  */
283 static inline void
284 xdr_set_scratch_page(struct xdr_stream *xdr, struct page *page)
285 {
286 	xdr_set_scratch_buffer(xdr, page_address(page), PAGE_SIZE);
287 }
288 
289 /**
290  * xdr_reset_scratch_buffer - Clear scratch buffer information
291  * @xdr: pointer to xdr_stream struct
292  *
293  * See xdr_set_scratch_buffer().
294  */
295 static inline void
296 xdr_reset_scratch_buffer(struct xdr_stream *xdr)
297 {
298 	xdr_set_scratch_buffer(xdr, NULL, 0);
299 }
300 
301 /**
302  * xdr_stream_remaining - Return the number of bytes remaining in the stream
303  * @xdr: pointer to struct xdr_stream
304  *
305  * Return value:
306  *   Number of bytes remaining in @xdr before xdr->end
307  */
308 static inline size_t
309 xdr_stream_remaining(const struct xdr_stream *xdr)
310 {
311 	return xdr->nwords << 2;
312 }
313 
314 ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr,
315 		size_t size);
316 ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
317 		size_t maxlen, gfp_t gfp_flags);
318 ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str,
319 		size_t size);
320 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
321 		size_t maxlen, gfp_t gfp_flags);
322 /**
323  * xdr_align_size - Calculate padded size of an object
324  * @n: Size of an object being XDR encoded (in bytes)
325  *
326  * Return value:
327  *   Size (in bytes) of the object including xdr padding
328  */
329 static inline size_t
330 xdr_align_size(size_t n)
331 {
332 	const size_t mask = sizeof(__u32) - 1;
333 
334 	return (n + mask) & ~mask;
335 }
336 
337 /**
338  * xdr_pad_size - Calculate size of an object's pad
339  * @n: Size of an object being XDR encoded (in bytes)
340  *
341  * This implementation avoids the need for conditional
342  * branches or modulo division.
343  *
344  * Return value:
345  *   Size (in bytes) of the needed XDR pad
346  */
347 static inline size_t xdr_pad_size(size_t n)
348 {
349 	return xdr_align_size(n) - n;
350 }
351 
352 /**
353  * xdr_stream_encode_item_present - Encode a "present" list item
354  * @xdr: pointer to xdr_stream
355  *
356  * Return values:
357  *   On success, returns length in bytes of XDR buffer consumed
358  *   %-EMSGSIZE on XDR buffer overflow
359  */
360 static inline ssize_t xdr_stream_encode_item_present(struct xdr_stream *xdr)
361 {
362 	const size_t len = sizeof(__be32);
363 	__be32 *p = xdr_reserve_space(xdr, len);
364 
365 	if (unlikely(!p))
366 		return -EMSGSIZE;
367 	*p = xdr_one;
368 	return len;
369 }
370 
371 /**
372  * xdr_stream_encode_item_absent - Encode a "not present" list item
373  * @xdr: pointer to xdr_stream
374  *
375  * Return values:
376  *   On success, returns length in bytes of XDR buffer consumed
377  *   %-EMSGSIZE on XDR buffer overflow
378  */
379 static inline int xdr_stream_encode_item_absent(struct xdr_stream *xdr)
380 {
381 	const size_t len = sizeof(__be32);
382 	__be32 *p = xdr_reserve_space(xdr, len);
383 
384 	if (unlikely(!p))
385 		return -EMSGSIZE;
386 	*p = xdr_zero;
387 	return len;
388 }
389 
390 /**
391  * xdr_stream_encode_u32 - Encode a 32-bit integer
392  * @xdr: pointer to xdr_stream
393  * @n: integer to encode
394  *
395  * Return values:
396  *   On success, returns length in bytes of XDR buffer consumed
397  *   %-EMSGSIZE on XDR buffer overflow
398  */
399 static inline ssize_t
400 xdr_stream_encode_u32(struct xdr_stream *xdr, __u32 n)
401 {
402 	const size_t len = sizeof(n);
403 	__be32 *p = xdr_reserve_space(xdr, len);
404 
405 	if (unlikely(!p))
406 		return -EMSGSIZE;
407 	*p = cpu_to_be32(n);
408 	return len;
409 }
410 
411 /**
412  * xdr_stream_encode_u64 - Encode a 64-bit integer
413  * @xdr: pointer to xdr_stream
414  * @n: 64-bit integer to encode
415  *
416  * Return values:
417  *   On success, returns length in bytes of XDR buffer consumed
418  *   %-EMSGSIZE on XDR buffer overflow
419  */
420 static inline ssize_t
421 xdr_stream_encode_u64(struct xdr_stream *xdr, __u64 n)
422 {
423 	const size_t len = sizeof(n);
424 	__be32 *p = xdr_reserve_space(xdr, len);
425 
426 	if (unlikely(!p))
427 		return -EMSGSIZE;
428 	xdr_encode_hyper(p, n);
429 	return len;
430 }
431 
432 /**
433  * xdr_stream_encode_opaque_inline - Encode opaque xdr data
434  * @xdr: pointer to xdr_stream
435  * @ptr: pointer to void pointer
436  * @len: size of object
437  *
438  * Return values:
439  *   On success, returns length in bytes of XDR buffer consumed
440  *   %-EMSGSIZE on XDR buffer overflow
441  */
442 static inline ssize_t
443 xdr_stream_encode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t len)
444 {
445 	size_t count = sizeof(__u32) + xdr_align_size(len);
446 	__be32 *p = xdr_reserve_space(xdr, count);
447 
448 	if (unlikely(!p)) {
449 		*ptr = NULL;
450 		return -EMSGSIZE;
451 	}
452 	xdr_encode_opaque(p, NULL, len);
453 	*ptr = ++p;
454 	return count;
455 }
456 
457 /**
458  * xdr_stream_encode_opaque_fixed - Encode fixed length opaque xdr data
459  * @xdr: pointer to xdr_stream
460  * @ptr: pointer to opaque data object
461  * @len: size of object pointed to by @ptr
462  *
463  * Return values:
464  *   On success, returns length in bytes of XDR buffer consumed
465  *   %-EMSGSIZE on XDR buffer overflow
466  */
467 static inline ssize_t
468 xdr_stream_encode_opaque_fixed(struct xdr_stream *xdr, const void *ptr, size_t len)
469 {
470 	__be32 *p = xdr_reserve_space(xdr, len);
471 
472 	if (unlikely(!p))
473 		return -EMSGSIZE;
474 	xdr_encode_opaque_fixed(p, ptr, len);
475 	return xdr_align_size(len);
476 }
477 
478 /**
479  * xdr_stream_encode_opaque - Encode variable length opaque xdr data
480  * @xdr: pointer to xdr_stream
481  * @ptr: pointer to opaque data object
482  * @len: size of object pointed to by @ptr
483  *
484  * Return values:
485  *   On success, returns length in bytes of XDR buffer consumed
486  *   %-EMSGSIZE on XDR buffer overflow
487  */
488 static inline ssize_t
489 xdr_stream_encode_opaque(struct xdr_stream *xdr, const void *ptr, size_t len)
490 {
491 	size_t count = sizeof(__u32) + xdr_align_size(len);
492 	__be32 *p = xdr_reserve_space(xdr, count);
493 
494 	if (unlikely(!p))
495 		return -EMSGSIZE;
496 	xdr_encode_opaque(p, ptr, len);
497 	return count;
498 }
499 
500 /**
501  * xdr_stream_encode_uint32_array - Encode variable length array of integers
502  * @xdr: pointer to xdr_stream
503  * @array: array of integers
504  * @array_size: number of elements in @array
505  *
506  * Return values:
507  *   On success, returns length in bytes of XDR buffer consumed
508  *   %-EMSGSIZE on XDR buffer overflow
509  */
510 static inline ssize_t
511 xdr_stream_encode_uint32_array(struct xdr_stream *xdr,
512 		const __u32 *array, size_t array_size)
513 {
514 	ssize_t ret = (array_size+1) * sizeof(__u32);
515 	__be32 *p = xdr_reserve_space(xdr, ret);
516 
517 	if (unlikely(!p))
518 		return -EMSGSIZE;
519 	*p++ = cpu_to_be32(array_size);
520 	for (; array_size > 0; p++, array++, array_size--)
521 		*p = cpu_to_be32p(array);
522 	return ret;
523 }
524 
525 /**
526  * xdr_item_is_absent - symbolically handle XDR discriminators
527  * @p: pointer to undecoded discriminator
528  *
529  * Return values:
530  *   %true if the following XDR item is absent
531  *   %false if the following XDR item is present
532  */
533 static inline bool xdr_item_is_absent(const __be32 *p)
534 {
535 	return *p == xdr_zero;
536 }
537 
538 /**
539  * xdr_item_is_present - symbolically handle XDR discriminators
540  * @p: pointer to undecoded discriminator
541  *
542  * Return values:
543  *   %true if the following XDR item is present
544  *   %false if the following XDR item is absent
545  */
546 static inline bool xdr_item_is_present(const __be32 *p)
547 {
548 	return *p != xdr_zero;
549 }
550 
551 /**
552  * xdr_stream_decode_bool - Decode a boolean
553  * @xdr: pointer to xdr_stream
554  * @ptr: pointer to a u32 in which to store the result
555  *
556  * Return values:
557  *   %0 on success
558  *   %-EBADMSG on XDR buffer overflow
559  */
560 static inline ssize_t
561 xdr_stream_decode_bool(struct xdr_stream *xdr, __u32 *ptr)
562 {
563 	const size_t count = sizeof(*ptr);
564 	__be32 *p = xdr_inline_decode(xdr, count);
565 
566 	if (unlikely(!p))
567 		return -EBADMSG;
568 	*ptr = (*p != xdr_zero);
569 	return 0;
570 }
571 
572 /**
573  * xdr_stream_decode_u32 - Decode a 32-bit integer
574  * @xdr: pointer to xdr_stream
575  * @ptr: location to store integer
576  *
577  * Return values:
578  *   %0 on success
579  *   %-EBADMSG on XDR buffer overflow
580  */
581 static inline ssize_t
582 xdr_stream_decode_u32(struct xdr_stream *xdr, __u32 *ptr)
583 {
584 	const size_t count = sizeof(*ptr);
585 	__be32 *p = xdr_inline_decode(xdr, count);
586 
587 	if (unlikely(!p))
588 		return -EBADMSG;
589 	*ptr = be32_to_cpup(p);
590 	return 0;
591 }
592 
593 /**
594  * xdr_stream_decode_u64 - Decode a 64-bit integer
595  * @xdr: pointer to xdr_stream
596  * @ptr: location to store 64-bit integer
597  *
598  * Return values:
599  *   %0 on success
600  *   %-EBADMSG on XDR buffer overflow
601  */
602 static inline ssize_t
603 xdr_stream_decode_u64(struct xdr_stream *xdr, __u64 *ptr)
604 {
605 	const size_t count = sizeof(*ptr);
606 	__be32 *p = xdr_inline_decode(xdr, count);
607 
608 	if (unlikely(!p))
609 		return -EBADMSG;
610 	xdr_decode_hyper(p, ptr);
611 	return 0;
612 }
613 
614 /**
615  * xdr_stream_decode_opaque_fixed - Decode fixed length opaque xdr data
616  * @xdr: pointer to xdr_stream
617  * @ptr: location to store data
618  * @len: size of buffer pointed to by @ptr
619  *
620  * Return values:
621  *   On success, returns size of object stored in @ptr
622  *   %-EBADMSG on XDR buffer overflow
623  */
624 static inline ssize_t
625 xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len)
626 {
627 	__be32 *p = xdr_inline_decode(xdr, len);
628 
629 	if (unlikely(!p))
630 		return -EBADMSG;
631 	xdr_decode_opaque_fixed(p, ptr, len);
632 	return len;
633 }
634 
635 /**
636  * xdr_stream_decode_opaque_inline - Decode variable length opaque xdr data
637  * @xdr: pointer to xdr_stream
638  * @ptr: location to store pointer to opaque data
639  * @maxlen: maximum acceptable object size
640  *
641  * Note: the pointer stored in @ptr cannot be assumed valid after the XDR
642  * buffer has been destroyed, or even after calling xdr_inline_decode()
643  * on @xdr. It is therefore expected that the object it points to should
644  * be processed immediately.
645  *
646  * Return values:
647  *   On success, returns size of object stored in *@ptr
648  *   %-EBADMSG on XDR buffer overflow
649  *   %-EMSGSIZE if the size of the object would exceed @maxlen
650  */
651 static inline ssize_t
652 xdr_stream_decode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t maxlen)
653 {
654 	__be32 *p;
655 	__u32 len;
656 
657 	*ptr = NULL;
658 	if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
659 		return -EBADMSG;
660 	if (len != 0) {
661 		p = xdr_inline_decode(xdr, len);
662 		if (unlikely(!p))
663 			return -EBADMSG;
664 		if (unlikely(len > maxlen))
665 			return -EMSGSIZE;
666 		*ptr = p;
667 	}
668 	return len;
669 }
670 
671 /**
672  * xdr_stream_decode_uint32_array - Decode variable length array of integers
673  * @xdr: pointer to xdr_stream
674  * @array: location to store the integer array or NULL
675  * @array_size: number of elements to store
676  *
677  * Return values:
678  *   On success, returns number of elements stored in @array
679  *   %-EBADMSG on XDR buffer overflow
680  *   %-EMSGSIZE if the size of the array exceeds @array_size
681  */
682 static inline ssize_t
683 xdr_stream_decode_uint32_array(struct xdr_stream *xdr,
684 		__u32 *array, size_t array_size)
685 {
686 	__be32 *p;
687 	__u32 len;
688 	ssize_t retval;
689 
690 	if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
691 		return -EBADMSG;
692 	p = xdr_inline_decode(xdr, len * sizeof(*p));
693 	if (unlikely(!p))
694 		return -EBADMSG;
695 	if (array == NULL)
696 		return len;
697 	if (len <= array_size) {
698 		if (len < array_size)
699 			memset(array+len, 0, (array_size-len)*sizeof(*array));
700 		array_size = len;
701 		retval = len;
702 	} else
703 		retval = -EMSGSIZE;
704 	for (; array_size > 0; p++, array++, array_size--)
705 		*array = be32_to_cpup(p);
706 	return retval;
707 }
708 
709 #endif /* _SUNRPC_XDR_H_ */
710