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