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