xref: /netbsd/sys/fs/nfs/common/nfsm_subs.h (revision a2a6649f)
1 /*	$NetBSD: nfsm_subs.h,v 1.4 2016/12/13 22:52:46 pgoyette Exp $	*/
2 /*-
3  * Copyright (c) 1989, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Rick Macklem at The University of Guelph.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * FreeBSD: head/sys/fs/nfs/nfsm_subs.h 276780 2015-01-07 17:22:56Z rwatson
34  * $NetBSD: nfsm_subs.h,v 1.4 2016/12/13 22:52:46 pgoyette Exp $
35  */
36 
37 #ifndef _NFS_NFSM_SUBS_H_
38 #define	_NFS_NFSM_SUBS_H_
39 
40 
41 /*
42  * These macros do strange and peculiar things to mbuf chains for
43  * the assistance of the nfs code. To attempt to use them for any
44  * other purpose will be dangerous. (they make weird assumptions)
45  */
46 
47 #ifndef APPLE
48 /*
49  * First define what the actual subs. return
50  */
51 #define	NFSM_DATAP(m, s)	(m)->m_data += (s)
52 
53 /*
54  * Now for the macros that do the simple stuff and call the functions
55  * for the hard stuff.
56  * They use fields in struct nfsrv_descript to handle the mbuf queues.
57  * Replace most of the macro with an inline function, to minimize
58  * the machine code. The inline functions in lower case can be called
59  * directly, bypassing the macro.
60  */
61 static __inline void *
nfsm_build(struct nfsrv_descript * nd,int siz)62 nfsm_build(struct nfsrv_descript *nd, int siz)
63 {
64 	void *retp;
65 	struct mbuf *mb2;
66 
67 	if (siz > M_TRAILINGSPACE(nd->nd_mb)) {
68 		NFSMCLGET(mb2, M_NOWAIT);
69 		if (siz > MLEN)
70 			panic("build > MLEN");
71 		mbuf_setlen(mb2, 0);
72 		nd->nd_bpos = NFSMTOD(mb2, char *);
73 		nd->nd_mb->m_next = mb2;
74 		nd->nd_mb = mb2;
75 	}
76 	retp = (void *)(nd->nd_bpos);
77 	nd->nd_mb->m_len += siz;
78 	nd->nd_bpos += siz;
79 	return (retp);
80 }
81 
82 #define	NFSM_BUILD(a, c, s)	((a) = (c)nfsm_build(nd, (s)))
83 
84 static __inline void *
nfsm_dissect(struct nfsrv_descript * nd,int siz)85 nfsm_dissect(struct nfsrv_descript *nd, int siz)
86 {
87 	int tt1;
88 	void *retp;
89 
90 	tt1 = NFSMTOD(nd->nd_md, char *) + nd->nd_md->m_len - nd->nd_dpos;
91 	if (tt1 >= siz) {
92 		retp = (void *)nd->nd_dpos;
93 		nd->nd_dpos += siz;
94 	} else {
95 		retp = nfsm_dissct(nd, siz, M_WAITOK);
96 	}
97 	return (retp);
98 }
99 
100 static __inline void *
nfsm_dissect_nonblock(struct nfsrv_descript * nd,int siz)101 nfsm_dissect_nonblock(struct nfsrv_descript *nd, int siz)
102 {
103 	int tt1;
104 	void *retp;
105 
106 	tt1 = NFSMTOD(nd->nd_md, char *) + nd->nd_md->m_len - nd->nd_dpos;
107 	if (tt1 >= siz) {
108 		retp = (void *)nd->nd_dpos;
109 		nd->nd_dpos += siz;
110 	} else {
111 		retp = nfsm_dissct(nd, siz, M_NOWAIT);
112 	}
113 	return (retp);
114 }
115 
116 #define	NFSM_DISSECT(a, c, s) 						\
117 	do {								\
118 		(a) = (c)nfsm_dissect(nd, (s));	 			\
119 		if ((a) == NULL) { 					\
120 			error = EBADRPC; 				\
121 			goto nfsmout; 					\
122 		}							\
123 	} while (0)
124 
125 #define	NFSM_DISSECT_NONBLOCK(a, c, s) 					\
126 	do {								\
127 		(a) = (c)nfsm_dissect_nonblock(nd, (s));		\
128 		if ((a) == NULL) { 					\
129 			error = EBADRPC; 				\
130 			goto nfsmout; 					\
131 		}							\
132 	} while (0)
133 #endif	/* !APPLE */
134 
135 #define	NFSM_STRSIZ(s, m)  						\
136 	do {								\
137 		tl = (u_int32_t *)nfsm_dissect(nd, NFSX_UNSIGNED);	\
138 		if (!tl || ((s) = fxdr_unsigned(int32_t, *tl)) > (m)) { \
139 			error = EBADRPC; 				\
140 			goto nfsmout; 					\
141 		}							\
142 	} while (0)
143 
144 #define	NFSM_RNDUP(a)	(((a)+3)&(~0x3))
145 
146 #endif	/* _NFS_NFSM_SUBS_H_ */
147