xref: /freebsd/sys/contrib/openzfs/include/os/linux/spl/rpc/xdr.h (revision 2b8331622f0b212cf3bb4fc4914a501e5321d506)
1 /*
2  *  Copyright (c) 2008 Sun Microsystems, Inc.
3  *  Written by Ricardo Correia <Ricardo.M.Correia@Sun.COM>
4  *
5  *  This file is part of the SPL, Solaris Porting Layer.
6  *
7  *  The SPL is free software; you can redistribute it and/or modify it
8  *  under the terms of the GNU General Public License as published by the
9  *  Free Software Foundation; either version 2 of the License, or (at your
10  *  option) any later version.
11  *
12  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
13  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  *  for more details.
16  *
17  *  You should have received a copy of the GNU General Public License along
18  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef _SPL_RPC_XDR_H
22 #define	_SPL_RPC_XDR_H
23 
24 #include <sys/types.h>
25 
26 typedef int bool_t;
27 
28 /*
29  * XDR enums and types.
30  */
31 enum xdr_op {
32 	XDR_ENCODE,
33 	XDR_DECODE
34 };
35 
36 struct xdr_ops;
37 
38 typedef struct {
39 	const struct xdr_ops	*x_ops;
40 	    /* Let caller know xdrmem_create() succeeds */
41 	caddr_t		x_addr;	/* Current buffer addr */
42 	caddr_t		x_addr_end;	/* End of the buffer */
43 	enum xdr_op	x_op;	/* Stream direction */
44 } XDR;
45 
46 typedef bool_t (*xdrproc_t)(XDR *xdrs, void *ptr);
47 
48 struct xdr_ops {
49 	bool_t (*xdr_control)(XDR *, int, void *);
50 
51 	bool_t (*xdr_char)(XDR *, char *);
52 	bool_t (*xdr_u_short)(XDR *, unsigned short *);
53 	bool_t (*xdr_u_int)(XDR *, unsigned *);
54 	bool_t (*xdr_u_longlong_t)(XDR *, u_longlong_t *);
55 
56 	bool_t (*xdr_opaque)(XDR *, caddr_t, const uint_t);
57 	bool_t (*xdr_string)(XDR *, char **, const uint_t);
58 	bool_t (*xdr_array)(XDR *, caddr_t *, uint_t *, const uint_t,
59 	    const uint_t, const xdrproc_t);
60 };
61 
62 /*
63  * XDR control operator.
64  */
65 #define	XDR_GET_BYTES_AVAIL 1
66 
67 struct xdr_bytesrec {
68 	bool_t xc_is_last_record;
69 	size_t xc_num_avail;
70 };
71 
72 /*
73  * XDR functions.
74  */
75 void xdrmem_create(XDR *xdrs, const caddr_t addr, const uint_t size,
76     const enum xdr_op op);
77 
78 #define	xdr_control(xdrs, req, info) \
79 	(xdrs)->x_ops->xdr_control((xdrs), (req), (info))
80 
81 /*
82  * For precaution, the following are defined as static inlines instead of macros
83  * to get some amount of type safety.
84  *
85  * Also, macros wouldn't work in the case where typecasting is done, because it
86  * must be possible to reference the functions' addresses by these names.
87  */
88 static inline bool_t xdr_char(XDR *xdrs, char *cp)
89 {
90 	return (xdrs->x_ops->xdr_char(xdrs, cp));
91 }
92 
93 static inline bool_t xdr_u_short(XDR *xdrs, unsigned short *usp)
94 {
95 	return (xdrs->x_ops->xdr_u_short(xdrs, usp));
96 }
97 
98 static inline bool_t xdr_short(XDR *xdrs, short *sp)
99 {
100 	BUILD_BUG_ON(sizeof (short) != 2);
101 	return (xdrs->x_ops->xdr_u_short(xdrs, (unsigned short *) sp));
102 }
103 
104 static inline bool_t xdr_u_int(XDR *xdrs, unsigned *up)
105 {
106 	return (xdrs->x_ops->xdr_u_int(xdrs, up));
107 }
108 
109 static inline bool_t xdr_int(XDR *xdrs, int *ip)
110 {
111 	BUILD_BUG_ON(sizeof (int) != 4);
112 	return (xdrs->x_ops->xdr_u_int(xdrs, (unsigned *)ip));
113 }
114 
115 static inline bool_t xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
116 {
117 	return (xdrs->x_ops->xdr_u_longlong_t(xdrs, ullp));
118 }
119 
120 static inline bool_t xdr_longlong_t(XDR *xdrs, longlong_t *llp)
121 {
122 	BUILD_BUG_ON(sizeof (longlong_t) != 8);
123 	return (xdrs->x_ops->xdr_u_longlong_t(xdrs, (u_longlong_t *)llp));
124 }
125 
126 /*
127  * Fixed-length opaque data.
128  */
129 static inline bool_t xdr_opaque(XDR *xdrs, caddr_t cp, const uint_t cnt)
130 {
131 	return (xdrs->x_ops->xdr_opaque(xdrs, cp, cnt));
132 }
133 
134 /*
135  * Variable-length string.
136  * The *sp buffer must have (maxsize + 1) bytes.
137  */
138 static inline bool_t xdr_string(XDR *xdrs, char **sp, const uint_t maxsize)
139 {
140 	return (xdrs->x_ops->xdr_string(xdrs, sp, maxsize));
141 }
142 
143 /*
144  * Variable-length arrays.
145  */
146 static inline bool_t xdr_array(XDR *xdrs, caddr_t *arrp, uint_t *sizep,
147     const uint_t maxsize, const uint_t elsize, const xdrproc_t elproc)
148 {
149 	return xdrs->x_ops->xdr_array(xdrs, arrp, sizep, maxsize, elsize,
150 	    elproc);
151 }
152 
153 #endif /* SPL_RPC_XDR_H */
154