Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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_buf_from_iov(const struct kvec *, struct xdr_buf *);
192extern int xdr_buf_subsegment(const struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
193extern void xdr_buf_trim(struct xdr_buf *, unsigned int);
194extern int read_bytes_from_xdr_buf(const struct xdr_buf *, unsigned int, void *, unsigned int);
195extern int write_bytes_to_xdr_buf(const struct xdr_buf *, unsigned int, void *, unsigned int);
196
197extern int xdr_encode_word(const struct xdr_buf *, unsigned int, u32);
198extern int xdr_decode_word(const struct xdr_buf *, unsigned int, u32 *);
199
200struct xdr_array2_desc;
201typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
202struct xdr_array2_desc {
203 unsigned int elem_size;
204 unsigned int array_len;
205 unsigned int array_maxlen;
206 xdr_xcode_elem_t xcode;
207};
208
209extern int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base,
210 struct xdr_array2_desc *desc);
211extern int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base,
212 struct xdr_array2_desc *desc);
213extern void _copy_from_pages(char *p, struct page **pages, size_t pgbase,
214 size_t len);
215
216/*
217 * Provide some simple tools for XDR buffer overflow-checking etc.
218 */
219struct xdr_stream {
220 __be32 *p; /* start of available buffer */
221 struct xdr_buf *buf; /* XDR buffer to read/write */
222
223 __be32 *end; /* end of available buffer space */
224 struct kvec *iov; /* pointer to the current kvec */
225 struct kvec scratch; /* Scratch buffer */
226 struct page **page_ptr; /* pointer to the current page */
227 unsigned int nwords; /* Remaining decode buffer length */
228
229 struct rpc_rqst *rqst; /* For debugging */
230};
231
232/*
233 * These are the xdr_stream style generic XDR encode and decode functions.
234 */
235typedef void (*kxdreproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
236 const void *obj);
237typedef int (*kxdrdproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
238 void *obj);
239
240extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf,
241 __be32 *p, struct rpc_rqst *rqst);
242extern void xdr_init_encode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
243 struct page **pages, struct rpc_rqst *rqst);
244extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes);
245extern int xdr_reserve_space_vec(struct xdr_stream *xdr, 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 void xdr_truncate_decode(struct xdr_stream *xdr, size_t len);
249extern int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen);
250extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages,
251 unsigned int base, unsigned int len);
252extern unsigned int xdr_stream_pos(const struct xdr_stream *xdr);
253extern unsigned int xdr_page_pos(const struct xdr_stream *xdr);
254extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf,
255 __be32 *p, struct rpc_rqst *rqst);
256extern void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
257 struct page **pages, unsigned int len);
258extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes);
259extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
260extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
261extern int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data);
262extern void xdr_set_pagelen(struct xdr_stream *, unsigned int len);
263extern bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
264 unsigned int len);
265extern unsigned int xdr_stream_move_subsegment(struct xdr_stream *xdr, unsigned int offset,
266 unsigned int target, unsigned int length);
267extern unsigned int xdr_stream_zero(struct xdr_stream *xdr, unsigned int offset,
268 unsigned int length);
269
270/**
271 * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
272 * @xdr: pointer to xdr_stream struct
273 * @buf: pointer to an empty buffer
274 * @buflen: size of 'buf'
275 *
276 * The scratch buffer is used when decoding from an array of pages.
277 * If an xdr_inline_decode() call spans across page boundaries, then
278 * we copy the data into the scratch buffer in order to allow linear
279 * access.
280 */
281static inline void
282xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
283{
284 xdr->scratch.iov_base = buf;
285 xdr->scratch.iov_len = buflen;
286}
287
288/**
289 * xdr_set_scratch_page - Attach a scratch buffer for decoding data
290 * @xdr: pointer to xdr_stream struct
291 * @page: an anonymous page
292 *
293 * See xdr_set_scratch_buffer().
294 */
295static inline void
296xdr_set_scratch_page(struct xdr_stream *xdr, struct page *page)
297{
298 xdr_set_scratch_buffer(xdr, page_address(page), PAGE_SIZE);
299}
300
301/**
302 * xdr_reset_scratch_buffer - Clear scratch buffer information
303 * @xdr: pointer to xdr_stream struct
304 *
305 * See xdr_set_scratch_buffer().
306 */
307static inline void
308xdr_reset_scratch_buffer(struct xdr_stream *xdr)
309{
310 xdr_set_scratch_buffer(xdr, NULL, 0);
311}
312
313/**
314 * xdr_commit_encode - Ensure all data is written to xdr->buf
315 * @xdr: pointer to xdr_stream
316 *
317 * Handle encoding across page boundaries by giving the caller a
318 * temporary location to write to, then later copying the data into
319 * place. __xdr_commit_encode() does that copying.
320 */
321static inline void xdr_commit_encode(struct xdr_stream *xdr)
322{
323 if (unlikely(xdr->scratch.iov_len))
324 __xdr_commit_encode(xdr);
325}
326
327/**
328 * xdr_stream_remaining - Return the number of bytes remaining in the stream
329 * @xdr: pointer to struct xdr_stream
330 *
331 * Return value:
332 * Number of bytes remaining in @xdr before xdr->end
333 */
334static inline size_t
335xdr_stream_remaining(const struct xdr_stream *xdr)
336{
337 return xdr->nwords << 2;
338}
339
340ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr,
341 size_t size);
342ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
343 size_t maxlen, gfp_t gfp_flags);
344ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str,
345 size_t size);
346ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
347 size_t maxlen, gfp_t gfp_flags);
348ssize_t xdr_stream_decode_opaque_auth(struct xdr_stream *xdr, u32 *flavor,
349 void **body, unsigned int *body_len);
350ssize_t xdr_stream_encode_opaque_auth(struct xdr_stream *xdr, u32 flavor,
351 void *body, unsigned int body_len);
352
353/**
354 * xdr_align_size - Calculate padded size of an object
355 * @n: Size of an object being XDR encoded (in bytes)
356 *
357 * Return value:
358 * Size (in bytes) of the object including xdr padding
359 */
360static inline size_t
361xdr_align_size(size_t n)
362{
363 const size_t mask = XDR_UNIT - 1;
364
365 return (n + mask) & ~mask;
366}
367
368/**
369 * xdr_pad_size - Calculate size of an object's pad
370 * @n: Size of an object being XDR encoded (in bytes)
371 *
372 * This implementation avoids the need for conditional
373 * branches or modulo division.
374 *
375 * Return value:
376 * Size (in bytes) of the needed XDR pad
377 */
378static inline size_t xdr_pad_size(size_t n)
379{
380 return xdr_align_size(n) - n;
381}
382
383/**
384 * xdr_stream_encode_item_present - Encode a "present" list item
385 * @xdr: pointer to xdr_stream
386 *
387 * Return values:
388 * On success, returns length in bytes of XDR buffer consumed
389 * %-EMSGSIZE on XDR buffer overflow
390 */
391static inline ssize_t xdr_stream_encode_item_present(struct xdr_stream *xdr)
392{
393 const size_t len = XDR_UNIT;
394 __be32 *p = xdr_reserve_space(xdr, len);
395
396 if (unlikely(!p))
397 return -EMSGSIZE;
398 *p = xdr_one;
399 return len;
400}
401
402/**
403 * xdr_stream_encode_item_absent - Encode a "not present" list item
404 * @xdr: pointer to xdr_stream
405 *
406 * Return values:
407 * On success, returns length in bytes of XDR buffer consumed
408 * %-EMSGSIZE on XDR buffer overflow
409 */
410static inline int xdr_stream_encode_item_absent(struct xdr_stream *xdr)
411{
412 const size_t len = XDR_UNIT;
413 __be32 *p = xdr_reserve_space(xdr, len);
414
415 if (unlikely(!p))
416 return -EMSGSIZE;
417 *p = xdr_zero;
418 return len;
419}
420
421/**
422 * xdr_encode_bool - Encode a boolean item
423 * @p: address in a buffer into which to encode
424 * @n: boolean value to encode
425 *
426 * Return value:
427 * Address of item following the encoded boolean
428 */
429static inline __be32 *xdr_encode_bool(__be32 *p, u32 n)
430{
431 *p++ = n ? xdr_one : xdr_zero;
432 return p;
433}
434
435/**
436 * xdr_stream_encode_bool - Encode a boolean item
437 * @xdr: pointer to xdr_stream
438 * @n: boolean value to encode
439 *
440 * Return values:
441 * On success, returns length in bytes of XDR buffer consumed
442 * %-EMSGSIZE on XDR buffer overflow
443 */
444static inline int xdr_stream_encode_bool(struct xdr_stream *xdr, __u32 n)
445{
446 const size_t len = XDR_UNIT;
447 __be32 *p = xdr_reserve_space(xdr, len);
448
449 if (unlikely(!p))
450 return -EMSGSIZE;
451 xdr_encode_bool(p, n);
452 return len;
453}
454
455/**
456 * xdr_stream_encode_u32 - Encode a 32-bit integer
457 * @xdr: pointer to xdr_stream
458 * @n: integer to encode
459 *
460 * Return values:
461 * On success, returns length in bytes of XDR buffer consumed
462 * %-EMSGSIZE on XDR buffer overflow
463 */
464static inline ssize_t
465xdr_stream_encode_u32(struct xdr_stream *xdr, __u32 n)
466{
467 const size_t len = sizeof(n);
468 __be32 *p = xdr_reserve_space(xdr, len);
469
470 if (unlikely(!p))
471 return -EMSGSIZE;
472 *p = cpu_to_be32(n);
473 return len;
474}
475
476/**
477 * xdr_stream_encode_be32 - Encode a big-endian 32-bit integer
478 * @xdr: pointer to xdr_stream
479 * @n: integer to encode
480 *
481 * Return values:
482 * On success, returns length in bytes of XDR buffer consumed
483 * %-EMSGSIZE on XDR buffer overflow
484 */
485static inline ssize_t
486xdr_stream_encode_be32(struct xdr_stream *xdr, __be32 n)
487{
488 const size_t len = sizeof(n);
489 __be32 *p = xdr_reserve_space(xdr, len);
490
491 if (unlikely(!p))
492 return -EMSGSIZE;
493 *p = n;
494 return len;
495}
496
497/**
498 * xdr_stream_encode_u64 - Encode a 64-bit integer
499 * @xdr: pointer to xdr_stream
500 * @n: 64-bit integer to encode
501 *
502 * Return values:
503 * On success, returns length in bytes of XDR buffer consumed
504 * %-EMSGSIZE on XDR buffer overflow
505 */
506static inline ssize_t
507xdr_stream_encode_u64(struct xdr_stream *xdr, __u64 n)
508{
509 const size_t len = sizeof(n);
510 __be32 *p = xdr_reserve_space(xdr, len);
511
512 if (unlikely(!p))
513 return -EMSGSIZE;
514 xdr_encode_hyper(p, n);
515 return len;
516}
517
518/**
519 * xdr_stream_encode_opaque_inline - Encode opaque xdr data
520 * @xdr: pointer to xdr_stream
521 * @ptr: pointer to void pointer
522 * @len: size of object
523 *
524 * Return values:
525 * On success, returns length in bytes of XDR buffer consumed
526 * %-EMSGSIZE on XDR buffer overflow
527 */
528static inline ssize_t
529xdr_stream_encode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t len)
530{
531 size_t count = sizeof(__u32) + xdr_align_size(len);
532 __be32 *p = xdr_reserve_space(xdr, count);
533
534 if (unlikely(!p)) {
535 *ptr = NULL;
536 return -EMSGSIZE;
537 }
538 xdr_encode_opaque(p, NULL, len);
539 *ptr = ++p;
540 return count;
541}
542
543/**
544 * xdr_stream_encode_opaque_fixed - Encode fixed length opaque xdr data
545 * @xdr: pointer to xdr_stream
546 * @ptr: pointer to opaque data object
547 * @len: size of object pointed to by @ptr
548 *
549 * Return values:
550 * On success, returns length in bytes of XDR buffer consumed
551 * %-EMSGSIZE on XDR buffer overflow
552 */
553static inline ssize_t
554xdr_stream_encode_opaque_fixed(struct xdr_stream *xdr, const void *ptr, size_t len)
555{
556 __be32 *p = xdr_reserve_space(xdr, len);
557
558 if (unlikely(!p))
559 return -EMSGSIZE;
560 xdr_encode_opaque_fixed(p, ptr, len);
561 return xdr_align_size(len);
562}
563
564/**
565 * xdr_stream_encode_opaque - Encode variable length opaque xdr data
566 * @xdr: pointer to xdr_stream
567 * @ptr: pointer to opaque data object
568 * @len: size of object pointed to by @ptr
569 *
570 * Return values:
571 * On success, returns length in bytes of XDR buffer consumed
572 * %-EMSGSIZE on XDR buffer overflow
573 */
574static inline ssize_t
575xdr_stream_encode_opaque(struct xdr_stream *xdr, const void *ptr, size_t len)
576{
577 size_t count = sizeof(__u32) + xdr_align_size(len);
578 __be32 *p = xdr_reserve_space(xdr, count);
579
580 if (unlikely(!p))
581 return -EMSGSIZE;
582 xdr_encode_opaque(p, ptr, len);
583 return count;
584}
585
586/**
587 * xdr_stream_encode_uint32_array - Encode variable length array of integers
588 * @xdr: pointer to xdr_stream
589 * @array: array of integers
590 * @array_size: number of elements in @array
591 *
592 * Return values:
593 * On success, returns length in bytes of XDR buffer consumed
594 * %-EMSGSIZE on XDR buffer overflow
595 */
596static inline ssize_t
597xdr_stream_encode_uint32_array(struct xdr_stream *xdr,
598 const __u32 *array, size_t array_size)
599{
600 ssize_t ret = (array_size+1) * sizeof(__u32);
601 __be32 *p = xdr_reserve_space(xdr, ret);
602
603 if (unlikely(!p))
604 return -EMSGSIZE;
605 *p++ = cpu_to_be32(array_size);
606 for (; array_size > 0; p++, array++, array_size--)
607 *p = cpu_to_be32p(array);
608 return ret;
609}
610
611/**
612 * xdr_item_is_absent - symbolically handle XDR discriminators
613 * @p: pointer to undecoded discriminator
614 *
615 * Return values:
616 * %true if the following XDR item is absent
617 * %false if the following XDR item is present
618 */
619static inline bool xdr_item_is_absent(const __be32 *p)
620{
621 return *p == xdr_zero;
622}
623
624/**
625 * xdr_item_is_present - symbolically handle XDR discriminators
626 * @p: pointer to undecoded discriminator
627 *
628 * Return values:
629 * %true if the following XDR item is present
630 * %false if the following XDR item is absent
631 */
632static inline bool xdr_item_is_present(const __be32 *p)
633{
634 return *p != xdr_zero;
635}
636
637/**
638 * xdr_stream_decode_bool - Decode a boolean
639 * @xdr: pointer to xdr_stream
640 * @ptr: pointer to a u32 in which to store the result
641 *
642 * Return values:
643 * %0 on success
644 * %-EBADMSG on XDR buffer overflow
645 */
646static inline ssize_t
647xdr_stream_decode_bool(struct xdr_stream *xdr, __u32 *ptr)
648{
649 const size_t count = sizeof(*ptr);
650 __be32 *p = xdr_inline_decode(xdr, count);
651
652 if (unlikely(!p))
653 return -EBADMSG;
654 *ptr = (*p != xdr_zero);
655 return 0;
656}
657
658/**
659 * xdr_stream_decode_u32 - Decode a 32-bit integer
660 * @xdr: pointer to xdr_stream
661 * @ptr: location to store integer
662 *
663 * Return values:
664 * %0 on success
665 * %-EBADMSG on XDR buffer overflow
666 */
667static inline ssize_t
668xdr_stream_decode_u32(struct xdr_stream *xdr, __u32 *ptr)
669{
670 const size_t count = sizeof(*ptr);
671 __be32 *p = xdr_inline_decode(xdr, count);
672
673 if (unlikely(!p))
674 return -EBADMSG;
675 *ptr = be32_to_cpup(p);
676 return 0;
677}
678
679/**
680 * xdr_stream_decode_u64 - Decode a 64-bit integer
681 * @xdr: pointer to xdr_stream
682 * @ptr: location to store 64-bit integer
683 *
684 * Return values:
685 * %0 on success
686 * %-EBADMSG on XDR buffer overflow
687 */
688static inline ssize_t
689xdr_stream_decode_u64(struct xdr_stream *xdr, __u64 *ptr)
690{
691 const size_t count = sizeof(*ptr);
692 __be32 *p = xdr_inline_decode(xdr, count);
693
694 if (unlikely(!p))
695 return -EBADMSG;
696 xdr_decode_hyper(p, ptr);
697 return 0;
698}
699
700/**
701 * xdr_stream_decode_opaque_fixed - Decode fixed length opaque xdr data
702 * @xdr: pointer to xdr_stream
703 * @ptr: location to store data
704 * @len: size of buffer pointed to by @ptr
705 *
706 * Return values:
707 * On success, returns size of object stored in @ptr
708 * %-EBADMSG on XDR buffer overflow
709 */
710static inline ssize_t
711xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len)
712{
713 __be32 *p = xdr_inline_decode(xdr, len);
714
715 if (unlikely(!p))
716 return -EBADMSG;
717 xdr_decode_opaque_fixed(p, ptr, len);
718 return len;
719}
720
721/**
722 * xdr_stream_decode_opaque_inline - Decode variable length opaque xdr data
723 * @xdr: pointer to xdr_stream
724 * @ptr: location to store pointer to opaque data
725 * @maxlen: maximum acceptable object size
726 *
727 * Note: the pointer stored in @ptr cannot be assumed valid after the XDR
728 * buffer has been destroyed, or even after calling xdr_inline_decode()
729 * on @xdr. It is therefore expected that the object it points to should
730 * be processed immediately.
731 *
732 * Return values:
733 * On success, returns size of object stored in *@ptr
734 * %-EBADMSG on XDR buffer overflow
735 * %-EMSGSIZE if the size of the object would exceed @maxlen
736 */
737static inline ssize_t
738xdr_stream_decode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t maxlen)
739{
740 __be32 *p;
741 __u32 len;
742
743 *ptr = NULL;
744 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
745 return -EBADMSG;
746 if (len != 0) {
747 p = xdr_inline_decode(xdr, len);
748 if (unlikely(!p))
749 return -EBADMSG;
750 if (unlikely(len > maxlen))
751 return -EMSGSIZE;
752 *ptr = p;
753 }
754 return len;
755}
756
757/**
758 * xdr_stream_decode_uint32_array - Decode variable length array of integers
759 * @xdr: pointer to xdr_stream
760 * @array: location to store the integer array or NULL
761 * @array_size: number of elements to store
762 *
763 * Return values:
764 * On success, returns number of elements stored in @array
765 * %-EBADMSG on XDR buffer overflow
766 * %-EMSGSIZE if the size of the array exceeds @array_size
767 */
768static inline ssize_t
769xdr_stream_decode_uint32_array(struct xdr_stream *xdr,
770 __u32 *array, size_t array_size)
771{
772 __be32 *p;
773 __u32 len;
774 ssize_t retval;
775
776 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
777 return -EBADMSG;
778 if (len > SIZE_MAX / sizeof(*p))
779 return -EBADMSG;
780 p = xdr_inline_decode(xdr, len * sizeof(*p));
781 if (unlikely(!p))
782 return -EBADMSG;
783 if (array == NULL)
784 return len;
785 if (len <= array_size) {
786 if (len < array_size)
787 memset(array+len, 0, (array_size-len)*sizeof(*array));
788 array_size = len;
789 retval = len;
790 } else
791 retval = -EMSGSIZE;
792 for (; array_size > 0; p++, array++, array_size--)
793 *array = be32_to_cpup(p);
794 return retval;
795}
796
797#endif /* _SUNRPC_XDR_H_ */