xref: /freebsd/sys/nfs/xdr_subs.h (revision 29363fb446372cb3f10bc98664e9767c53fbb457)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes  * Rick Macklem at The University of Guelph.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  */
34df8bae1dSRodney W. Grimes 
3533420ec6SPaul Richards #ifndef _NFS_XDR_SUBS_H_
3633420ec6SPaul Richards #define _NFS_XDR_SUBS_H_
3733420ec6SPaul Richards 
38df8bae1dSRodney W. Grimes /*
39df8bae1dSRodney W. Grimes  * Macros used for conversion to/from xdr representation by nfs...
40df8bae1dSRodney W. Grimes  * These use the MACHINE DEPENDENT routines ntohl, htonl
41df8bae1dSRodney W. Grimes  * As defined by "XDR: External Data Representation Standard" RFC1014
42df8bae1dSRodney W. Grimes  *
43df8bae1dSRodney W. Grimes  * To simplify the implementation, we use ntohl/htonl even on big-endian
44df8bae1dSRodney W. Grimes  * machines, and count on them being `#define'd away.  Some of these
45df8bae1dSRodney W. Grimes  * might be slightly more efficient as quad_t copies on a big-endian,
46df8bae1dSRodney W. Grimes  * but we cannot count on their alignment anyway.
47df8bae1dSRodney W. Grimes  */
48df8bae1dSRodney W. Grimes 
494152886fSPeter Wemm #define	fxdr_unsigned(t, v)	((t)ntohl((int32_t)(v)))
504152886fSPeter Wemm #define	txdr_unsigned(v)	(htonl((int32_t)(v)))
51df8bae1dSRodney W. Grimes 
52ac7cc2e4SPeter Wemm #define	fxdr_nfsv2time(f, t) \
53ac7cc2e4SPeter Wemm do { \
54030e2e9eSNate Williams 	(t)->tv_sec = ntohl(((struct nfsv2_time *)(f))->nfsv2_sec); \
55a62dc406SDoug Rabson 	if (((struct nfsv2_time *)(f))->nfsv2_usec != 0xffffffff) \
56030e2e9eSNate Williams 		(t)->tv_nsec = 1000 * ntohl(((struct nfsv2_time *)(f))->nfsv2_usec); \
57f295740eSDavid Greenman 	else \
58030e2e9eSNate Williams 		(t)->tv_nsec = 0; \
59ac7cc2e4SPeter Wemm } while (0)
60ac7cc2e4SPeter Wemm #define	txdr_nfsv2time(f, t) \
61ac7cc2e4SPeter Wemm do { \
62030e2e9eSNate Williams 	((struct nfsv2_time *)(t))->nfsv2_sec = htonl((f)->tv_sec); \
63030e2e9eSNate Williams 	if ((f)->tv_nsec != -1) \
64030e2e9eSNate Williams 		((struct nfsv2_time *)(t))->nfsv2_usec = htonl((f)->tv_nsec / 1000); \
65f295740eSDavid Greenman 	else \
66a62dc406SDoug Rabson 		((struct nfsv2_time *)(t))->nfsv2_usec = 0xffffffff; \
67ac7cc2e4SPeter Wemm } while (0)
68df8bae1dSRodney W. Grimes 
69ac7cc2e4SPeter Wemm #define	fxdr_nfsv3time(f, t) \
70ac7cc2e4SPeter Wemm do { \
71030e2e9eSNate Williams 	(t)->tv_sec = ntohl(((struct nfsv3_time *)(f))->nfsv3_sec); \
72030e2e9eSNate Williams 	(t)->tv_nsec = ntohl(((struct nfsv3_time *)(f))->nfsv3_nsec); \
73ac7cc2e4SPeter Wemm } while (0)
74ac7cc2e4SPeter Wemm #define	txdr_nfsv3time(f, t) \
75ac7cc2e4SPeter Wemm do { \
76030e2e9eSNate Williams 	((struct nfsv3_time *)(t))->nfsv3_sec = htonl((f)->tv_sec); \
77030e2e9eSNate Williams 	((struct nfsv3_time *)(t))->nfsv3_nsec = htonl((f)->tv_nsec); \
78ac7cc2e4SPeter Wemm } while (0)
79df8bae1dSRodney W. Grimes 
80b903b04cSPeter Wemm #define	fxdr_hyper(f) \
81b903b04cSPeter Wemm 	((((u_quad_t)ntohl(((u_int32_t *)(f))[0])) << 32) | \
82b903b04cSPeter Wemm 	 (u_quad_t)(ntohl(((u_int32_t *)(f))[1])))
83*32fbc5d8SAlan Somers 
84*32fbc5d8SAlan Somers static inline void
txdr_hyper(uint64_t f,uint32_t * t)85*32fbc5d8SAlan Somers txdr_hyper(uint64_t f, uint32_t* t)
86*32fbc5d8SAlan Somers {
87*32fbc5d8SAlan Somers 	t[0] = htonl((u_int32_t)(f >> 32));
88*32fbc5d8SAlan Somers 	t[1] = htonl((u_int32_t)(f & 0xffffffff));
89*32fbc5d8SAlan Somers }
9033420ec6SPaul Richards 
9133420ec6SPaul Richards #endif
92