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
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
xdr_buf_init(struct xdr_buf * buf,void * start,size_t len)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(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);
134
xdr_encode_array(__be32 * p,const void * s,unsigned int len)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 *
xdr_encode_hyper(__be32 * p,__u64 val)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 *
xdr_decode_hyper(__be32 * p,__u64 * valp)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 *
xdr_decode_opaque_fixed(__be32 * p,void * ptr,unsigned int len)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
xdr_netobj_dup(struct xdr_netobj * dst,struct xdr_netobj * src,gfp_t gfp_mask)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
xdr_adjust_iovec(struct kvec * iov,__be32 * p)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(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);
189
190 extern int xdr_encode_word(struct xdr_buf *, unsigned int, u32);
191 extern int xdr_decode_word(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(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,
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 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);
256
257 /**
258 * xdr_stream_remaining - Return the number of bytes remaining in the stream
259 * @xdr: pointer to struct xdr_stream
260 *
261 * Return value:
262 * Number of bytes remaining in @xdr before xdr->end
263 */
264 static inline size_t
xdr_stream_remaining(const struct xdr_stream * xdr)265 xdr_stream_remaining(const struct xdr_stream *xdr)
266 {
267 return xdr->nwords << 2;
268 }
269
270 ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr,
271 size_t size);
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,
275 size_t size);
276 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
277 size_t maxlen, gfp_t gfp_flags);
278 /**
279 * xdr_align_size - Calculate padded size of an object
280 * @n: Size of an object being XDR encoded (in bytes)
281 *
282 * Return value:
283 * Size (in bytes) of the object including xdr padding
284 */
285 static inline size_t
xdr_align_size(size_t n)286 xdr_align_size(size_t n)
287 {
288 const size_t mask = sizeof(__u32) - 1;
289
290 return (n + mask) & ~mask;
291 }
292
293 /**
294 * xdr_pad_size - Calculate size of an object's pad
295 * @n: Size of an object being XDR encoded (in bytes)
296 *
297 * This implementation avoids the need for conditional
298 * branches or modulo division.
299 *
300 * Return value:
301 * Size (in bytes) of the needed XDR pad
302 */
xdr_pad_size(size_t n)303 static inline size_t xdr_pad_size(size_t n)
304 {
305 return xdr_align_size(n) - n;
306 }
307
308 /**
309 * xdr_stream_encode_item_present - Encode a "present" list item
310 * @xdr: pointer to xdr_stream
311 *
312 * Return values:
313 * On success, returns length in bytes of XDR buffer consumed
314 * %-EMSGSIZE on XDR buffer overflow
315 */
xdr_stream_encode_item_present(struct xdr_stream * xdr)316 static inline ssize_t xdr_stream_encode_item_present(struct xdr_stream *xdr)
317 {
318 const size_t len = sizeof(__be32);
319 __be32 *p = xdr_reserve_space(xdr, len);
320
321 if (unlikely(!p))
322 return -EMSGSIZE;
323 *p = xdr_one;
324 return len;
325 }
326
327 /**
328 * xdr_stream_encode_item_absent - Encode a "not present" list item
329 * @xdr: pointer to xdr_stream
330 *
331 * Return values:
332 * On success, returns length in bytes of XDR buffer consumed
333 * %-EMSGSIZE on XDR buffer overflow
334 */
xdr_stream_encode_item_absent(struct xdr_stream * xdr)335 static inline int xdr_stream_encode_item_absent(struct xdr_stream *xdr)
336 {
337 const size_t len = sizeof(__be32);
338 __be32 *p = xdr_reserve_space(xdr, len);
339
340 if (unlikely(!p))
341 return -EMSGSIZE;
342 *p = xdr_zero;
343 return len;
344 }
345
346 /**
347 * xdr_stream_encode_u32 - Encode a 32-bit integer
348 * @xdr: pointer to xdr_stream
349 * @n: integer to encode
350 *
351 * Return values:
352 * On success, returns length in bytes of XDR buffer consumed
353 * %-EMSGSIZE on XDR buffer overflow
354 */
355 static inline ssize_t
xdr_stream_encode_u32(struct xdr_stream * xdr,__u32 n)356 xdr_stream_encode_u32(struct xdr_stream *xdr, __u32 n)
357 {
358 const size_t len = sizeof(n);
359 __be32 *p = xdr_reserve_space(xdr, len);
360
361 if (unlikely(!p))
362 return -EMSGSIZE;
363 *p = cpu_to_be32(n);
364 return len;
365 }
366
367 /**
368 * xdr_stream_encode_u64 - Encode a 64-bit integer
369 * @xdr: pointer to xdr_stream
370 * @n: 64-bit integer to encode
371 *
372 * Return values:
373 * On success, returns length in bytes of XDR buffer consumed
374 * %-EMSGSIZE on XDR buffer overflow
375 */
376 static inline ssize_t
xdr_stream_encode_u64(struct xdr_stream * xdr,__u64 n)377 xdr_stream_encode_u64(struct xdr_stream *xdr, __u64 n)
378 {
379 const size_t len = sizeof(n);
380 __be32 *p = xdr_reserve_space(xdr, len);
381
382 if (unlikely(!p))
383 return -EMSGSIZE;
384 xdr_encode_hyper(p, n);
385 return len;
386 }
387
388 /**
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
393 *
394 * Return values:
395 * On success, returns length in bytes of XDR buffer consumed
396 * %-EMSGSIZE on XDR buffer overflow
397 */
398 static inline ssize_t
xdr_stream_encode_opaque_inline(struct xdr_stream * xdr,void ** ptr,size_t len)399 xdr_stream_encode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t len)
400 {
401 size_t count = sizeof(__u32) + xdr_align_size(len);
402 __be32 *p = xdr_reserve_space(xdr, count);
403
404 if (unlikely(!p)) {
405 *ptr = NULL;
406 return -EMSGSIZE;
407 }
408 xdr_encode_opaque(p, NULL, len);
409 *ptr = ++p;
410 return count;
411 }
412
413 /**
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
418 *
419 * Return values:
420 * On success, returns length in bytes of XDR buffer consumed
421 * %-EMSGSIZE on XDR buffer overflow
422 */
423 static inline ssize_t
xdr_stream_encode_opaque_fixed(struct xdr_stream * xdr,const void * ptr,size_t len)424 xdr_stream_encode_opaque_fixed(struct xdr_stream *xdr, const void *ptr, size_t len)
425 {
426 __be32 *p = xdr_reserve_space(xdr, len);
427
428 if (unlikely(!p))
429 return -EMSGSIZE;
430 xdr_encode_opaque_fixed(p, ptr, len);
431 return xdr_align_size(len);
432 }
433
434 /**
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
439 *
440 * Return values:
441 * On success, returns length in bytes of XDR buffer consumed
442 * %-EMSGSIZE on XDR buffer overflow
443 */
444 static inline ssize_t
xdr_stream_encode_opaque(struct xdr_stream * xdr,const void * ptr,size_t len)445 xdr_stream_encode_opaque(struct xdr_stream *xdr, const void *ptr, size_t len)
446 {
447 size_t count = sizeof(__u32) + xdr_align_size(len);
448 __be32 *p = xdr_reserve_space(xdr, count);
449
450 if (unlikely(!p))
451 return -EMSGSIZE;
452 xdr_encode_opaque(p, ptr, len);
453 return count;
454 }
455
456 /**
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
461 *
462 * Return values:
463 * On success, returns length in bytes of XDR buffer consumed
464 * %-EMSGSIZE on XDR buffer overflow
465 */
466 static inline ssize_t
xdr_stream_encode_uint32_array(struct xdr_stream * xdr,const __u32 * array,size_t array_size)467 xdr_stream_encode_uint32_array(struct xdr_stream *xdr,
468 const __u32 *array, size_t array_size)
469 {
470 ssize_t ret = (array_size+1) * sizeof(__u32);
471 __be32 *p = xdr_reserve_space(xdr, ret);
472
473 if (unlikely(!p))
474 return -EMSGSIZE;
475 *p++ = cpu_to_be32(array_size);
476 for (; array_size > 0; p++, array++, array_size--)
477 *p = cpu_to_be32p(array);
478 return ret;
479 }
480
481 /**
482 * xdr_item_is_absent - symbolically handle XDR discriminators
483 * @p: pointer to undecoded discriminator
484 *
485 * Return values:
486 * %true if the following XDR item is absent
487 * %false if the following XDR item is present
488 */
xdr_item_is_absent(const __be32 * p)489 static inline bool xdr_item_is_absent(const __be32 *p)
490 {
491 return *p == xdr_zero;
492 }
493
494 /**
495 * xdr_item_is_present - symbolically handle XDR discriminators
496 * @p: pointer to undecoded discriminator
497 *
498 * Return values:
499 * %true if the following XDR item is present
500 * %false if the following XDR item is absent
501 */
xdr_item_is_present(const __be32 * p)502 static inline bool xdr_item_is_present(const __be32 *p)
503 {
504 return *p != xdr_zero;
505 }
506
507 /**
508 * xdr_stream_decode_u32 - Decode a 32-bit integer
509 * @xdr: pointer to xdr_stream
510 * @ptr: location to store integer
511 *
512 * Return values:
513 * %0 on success
514 * %-EBADMSG on XDR buffer overflow
515 */
516 static inline ssize_t
xdr_stream_decode_u32(struct xdr_stream * xdr,__u32 * ptr)517 xdr_stream_decode_u32(struct xdr_stream *xdr, __u32 *ptr)
518 {
519 const size_t count = sizeof(*ptr);
520 __be32 *p = xdr_inline_decode(xdr, count);
521
522 if (unlikely(!p))
523 return -EBADMSG;
524 *ptr = be32_to_cpup(p);
525 return 0;
526 }
527
528 /**
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
533 *
534 * Return values:
535 * On success, returns size of object stored in @ptr
536 * %-EBADMSG on XDR buffer overflow
537 */
538 static inline ssize_t
xdr_stream_decode_opaque_fixed(struct xdr_stream * xdr,void * ptr,size_t len)539 xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len)
540 {
541 __be32 *p = xdr_inline_decode(xdr, len);
542
543 if (unlikely(!p))
544 return -EBADMSG;
545 xdr_decode_opaque_fixed(p, ptr, len);
546 return len;
547 }
548
549 /**
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
554 *
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.
559 *
560 * Return values:
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
564 */
565 static inline ssize_t
xdr_stream_decode_opaque_inline(struct xdr_stream * xdr,void ** ptr,size_t maxlen)566 xdr_stream_decode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t maxlen)
567 {
568 __be32 *p;
569 __u32 len;
570
571 *ptr = NULL;
572 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
573 return -EBADMSG;
574 if (len != 0) {
575 p = xdr_inline_decode(xdr, len);
576 if (unlikely(!p))
577 return -EBADMSG;
578 if (unlikely(len > maxlen))
579 return -EMSGSIZE;
580 *ptr = p;
581 }
582 return len;
583 }
584
585 /**
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
590 *
591 * Return values:
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
595 */
596 static inline ssize_t
xdr_stream_decode_uint32_array(struct xdr_stream * xdr,__u32 * array,size_t array_size)597 xdr_stream_decode_uint32_array(struct xdr_stream *xdr,
598 __u32 *array, size_t array_size)
599 {
600 __be32 *p;
601 __u32 len;
602 ssize_t retval;
603
604 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
605 return -EBADMSG;
606 p = xdr_inline_decode(xdr, len * sizeof(*p));
607 if (unlikely(!p))
608 return -EBADMSG;
609 if (array == NULL)
610 return len;
611 if (len <= array_size) {
612 if (len < array_size)
613 memset(array+len, 0, (array_size-len)*sizeof(*array));
614 array_size = len;
615 retval = len;
616 } else
617 retval = -EMSGSIZE;
618 for (; array_size > 0; p++, array++, array_size--)
619 *array = be32_to_cpup(p);
620 return retval;
621 }
622
623 #endif /* _SUNRPC_XDR_H_ */
624