Path: blob/main/sys/contrib/openzfs/include/os/linux/spl/rpc/xdr.h
48775 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Copyright (c) 2008 Sun Microsystems, Inc.3* Written by Ricardo Correia <[email protected]>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 it8* under the terms of the GNU General Public License as published by the9* Free Software Foundation; either version 2 of the License, or (at your10* option) any later version.11*12* The SPL is distributed in the hope that it will be useful, but WITHOUT13* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or14* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License15* for more details.16*17* You should have received a copy of the GNU General Public License along18* with the SPL. If not, see <http://www.gnu.org/licenses/>.19*/2021#ifndef _SPL_RPC_XDR_H22#define _SPL_RPC_XDR_H2324#include <sys/types.h>2526/*27* XDR enums and types.28*/29enum xdr_op {30XDR_ENCODE,31XDR_DECODE32};3334struct xdr_ops;3536typedef struct {37const struct xdr_ops *x_ops;38/* Let caller know xdrmem_create() succeeds */39caddr_t x_addr; /* Current buffer addr */40caddr_t x_addr_end; /* End of the buffer */41enum xdr_op x_op; /* Stream direction */42} XDR;4344typedef bool_t (*xdrproc_t)(XDR *xdrs, void *ptr);4546struct xdr_ops {47bool_t (*xdr_control)(XDR *, int, void *);4849bool_t (*xdr_char)(XDR *, char *);50bool_t (*xdr_u_short)(XDR *, unsigned short *);51bool_t (*xdr_u_int)(XDR *, unsigned *);52bool_t (*xdr_u_longlong_t)(XDR *, u_longlong_t *);5354bool_t (*xdr_opaque)(XDR *, caddr_t, const uint_t);55bool_t (*xdr_string)(XDR *, char **, const uint_t);56bool_t (*xdr_array)(XDR *, caddr_t *, uint_t *, const uint_t,57const uint_t, const xdrproc_t);58};5960/*61* XDR control operator.62*/63#define XDR_GET_BYTES_AVAIL 16465struct xdr_bytesrec {66bool_t xc_is_last_record;67size_t xc_num_avail;68};6970/*71* XDR functions.72*/73void xdrmem_create(XDR *xdrs, const caddr_t addr, const uint_t size,74const enum xdr_op op);7576#define xdr_control(xdrs, req, info) \77(xdrs)->x_ops->xdr_control((xdrs), (req), (info))7879/*80* For precaution, the following are defined as static inlines instead of macros81* to get some amount of type safety.82*83* Also, macros wouldn't work in the case where typecasting is done, because it84* must be possible to reference the functions' addresses by these names.85*/86static inline bool_t xdr_char(XDR *xdrs, char *cp)87{88return (xdrs->x_ops->xdr_char(xdrs, cp));89}9091static inline bool_t xdr_u_short(XDR *xdrs, unsigned short *usp)92{93return (xdrs->x_ops->xdr_u_short(xdrs, usp));94}9596static inline bool_t xdr_short(XDR *xdrs, short *sp)97{98BUILD_BUG_ON(sizeof (short) != 2);99return (xdrs->x_ops->xdr_u_short(xdrs, (unsigned short *) sp));100}101102static inline bool_t xdr_u_int(XDR *xdrs, unsigned *up)103{104return (xdrs->x_ops->xdr_u_int(xdrs, up));105}106107static inline bool_t xdr_int(XDR *xdrs, int *ip)108{109BUILD_BUG_ON(sizeof (int) != 4);110return (xdrs->x_ops->xdr_u_int(xdrs, (unsigned *)ip));111}112113static inline bool_t xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)114{115return (xdrs->x_ops->xdr_u_longlong_t(xdrs, ullp));116}117118static inline bool_t xdr_longlong_t(XDR *xdrs, longlong_t *llp)119{120BUILD_BUG_ON(sizeof (longlong_t) != 8);121return (xdrs->x_ops->xdr_u_longlong_t(xdrs, (u_longlong_t *)llp));122}123124/*125* Fixed-length opaque data.126*/127static inline bool_t xdr_opaque(XDR *xdrs, caddr_t cp, const uint_t cnt)128{129return (xdrs->x_ops->xdr_opaque(xdrs, cp, cnt));130}131132/*133* Variable-length string.134* The *sp buffer must have (maxsize + 1) bytes.135*/136static inline bool_t xdr_string(XDR *xdrs, char **sp, const uint_t maxsize)137{138return (xdrs->x_ops->xdr_string(xdrs, sp, maxsize));139}140141/*142* Variable-length arrays.143*/144static inline bool_t xdr_array(XDR *xdrs, caddr_t *arrp, uint_t *sizep,145const uint_t maxsize, const uint_t elsize, const xdrproc_t elproc)146{147return xdrs->x_ops->xdr_array(xdrs, arrp, sizep, maxsize, elsize,148elproc);149}150151#endif /* SPL_RPC_XDR_H */152153154