xref: /netbsd-src/sys/nfs/nfsm_subs.h (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * 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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	from: @(#)nfsm_subs.h	7.11 (Berkeley) 4/16/91
37  *	$Id: nfsm_subs.h,v 1.3 1993/06/03 01:12:46 cgd Exp $
38  */
39 
40 #ifndef _NFS_NFSM_SUBS_H_
41 #define _NFS_NFSM_SUBS_H_
42 
43 /*
44  * These macros do strange and peculiar things to mbuf chains for
45  * the assistance of the nfs code. To attempt to use them for any
46  * other purpose will be dangerous. (they make weird assumptions)
47  */
48 
49 /*
50  * First define what the actual subs. return
51  */
52 extern struct mbuf *nfsm_reqh();
53 
54 #define	M_HASCL(m)	((m)->m_flags & M_EXT)
55 #define	NFSMGETHDR(m) \
56 		MGETHDR(m, M_WAIT, MT_DATA); \
57 		(m)->m_pkthdr.len = 0; \
58 		(m)->m_pkthdr.rcvif = (struct ifnet *)0
59 #define	NFSMINOFF(m) \
60 		if (M_HASCL(m)) \
61 			(m)->m_data = (m)->m_ext.ext_buf; \
62 		else \
63 			(m)->m_data = (m)->m_dat
64 #define	NFSMADV(m, s)	(m)->m_data += (s)
65 #define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES: \
66 				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
67 
68 /*
69  * Now for the macros that do the simple stuff and call the functions
70  * for the hard stuff.
71  * These macros use several vars. declared in nfsm_reqhead and these
72  * vars. must not be used elsewhere unless you are careful not to corrupt
73  * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
74  * that may be used so long as the value is not expected to retained
75  * after a macro.
76  * I know, this is kind of dorkey, but it makes the actual op functions
77  * fairly clean and deals with the mess caused by the xdr discriminating
78  * unions.
79  */
80 
81 #ifndef lint
82 #define	nfsm_build(a,c,s) \
83 		t1 = NFSMSIZ(mb); \
84 		if ((s) > (t1-mb->m_len)) { \
85 			MGET(mb2, M_WAIT, MT_DATA); \
86 			if ((s) > MLEN) \
87 				panic("build > MLEN"); \
88 			mb->m_next = mb2; \
89 			mb = mb2; \
90 			mb->m_len = 0; \
91 			bpos = mtod(mb, caddr_t); \
92 		} \
93 		(a) = (c)(bpos); \
94 		mb->m_len += (s); \
95 		bpos += (s)
96 #else /* lint */
97 #define	nfsm_build(a,c,s) \
98 		t1 = NFSMSIZ(mb); \
99 		if ((s) > (t1-mb->m_len)) { \
100 			MGET(mb2, M_WAIT, MT_DATA); \
101 			mb->m_next = mb2; \
102 			mb = mb2; \
103 			mb->m_len = 0; \
104 			bpos = mtod(mb, caddr_t); \
105 		} \
106 		(a) = (c)(bpos); \
107 		mb->m_len += (s); \
108 		bpos += (s)
109 #endif /* lint */
110 
111 #define	nfsm_disect(a,c,s) \
112 		t1 = mtod(md, caddr_t)+md->m_len-dpos; \
113 		if (t1 >= (s)) { \
114 			(a) = (c)(dpos); \
115 			dpos += (s); \
116 		} else if (error = nfsm_disct(&md, &dpos, (s), t1, TRUE, &cp2)) { \
117 			m_freem(mrep); \
118 			goto nfsmout; \
119 		} else { \
120 			(a) = (c)cp2; \
121 		}
122 
123 #define	nfsm_disecton(a,c,s) \
124 		t1 = mtod(md, caddr_t)+md->m_len-dpos; \
125 		if (t1 >= (s)) { \
126 			(a) = (c)(dpos); \
127 			dpos += (s); \
128 		} else if (error = nfsm_disct(&md, &dpos, (s), t1, FALSE, &cp2)) { \
129 			m_freem(mrep); \
130 			goto nfsmout; \
131 		} else { \
132 			(a) = (c)cp2; \
133 		}
134 
135 #define nfsm_fhtom(v) \
136 		nfsm_build(cp,caddr_t,NFSX_FH); \
137 		bcopy((caddr_t)&(VTONFS(v)->n_fh), cp, NFSX_FH)
138 
139 #define nfsm_srvfhtom(f) \
140 		nfsm_build(cp,caddr_t,NFSX_FH); \
141 		bcopy((caddr_t)(f), cp, NFSX_FH)
142 
143 #define nfsm_mtofh(d,v) \
144 		{ struct nfsnode *np; nfsv2fh_t *fhp; \
145 		nfsm_disect(fhp,nfsv2fh_t *,NFSX_FH); \
146 		if (error = nfs_nget((d)->v_mount, fhp, &np)) { \
147 			m_freem(mrep); \
148 			goto nfsmout; \
149 		} \
150 		(v) = NFSTOV(np); \
151 		nfsm_loadattr(v, (struct vattr *)0); \
152 		}
153 
154 #define	nfsm_loadattr(v,a) \
155 		{ struct vnode *tvp = (v); \
156 		if (error = nfs_loadattrcache(&tvp, &md, &dpos, (a))) { \
157 			m_freem(mrep); \
158 			goto nfsmout; \
159 		} \
160 		(v) = tvp; }
161 
162 #define	nfsm_strsiz(s,m) \
163 		nfsm_disect(tl,u_long *,NFSX_UNSIGNED); \
164 		if (((s) = fxdr_unsigned(long,*tl)) > (m)) { \
165 			m_freem(mrep); \
166 			error = EBADRPC; \
167 			goto nfsmout; \
168 		}
169 
170 #define	nfsm_srvstrsiz(s,m) \
171 		nfsm_disect(tl,u_long *,NFSX_UNSIGNED); \
172 		if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \
173 			error = EBADRPC; \
174 			nfsm_reply(0); \
175 		}
176 
177 #define nfsm_mtouio(p,s) \
178 		if ((s) > 0 && \
179 		   (error = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
180 			m_freem(mrep); \
181 			goto nfsmout; \
182 		}
183 
184 #define nfsm_uiotom(p,s) \
185 		if (error = nfsm_uiotombuf((p),&mb,(s),&bpos)) { \
186 			m_freem(mreq); \
187 			goto nfsmout; \
188 		}
189 
190 #define	nfsm_reqhead(a,c,s) \
191 		if ((mreq = nfsm_reqh(nfs_prog,nfs_vers,(a),(c),(s),&bpos,&mb,&xid)) == NULL) { \
192 			error = ENOBUFS; \
193 			goto nfsmout; \
194 		}
195 
196 #define nfsm_reqdone	m_freem(mrep); \
197 		nfsmout:
198 
199 #define nfsm_rndup(a)	(((a)+3)&(~0x3))
200 
201 #define	nfsm_request(v, t, p, h)	\
202 		if (error = nfs_request((v), mreq, xid, (t), (p), (h), \
203 		   (v)->v_mount, &mrep, &md, &dpos)) \
204 			goto nfsmout
205 
206 #define	nfsm_strtom(a,s,m) \
207 		if ((s) > (m)) { \
208 			m_freem(mreq); \
209 			error = ENAMETOOLONG; \
210 			goto nfsmout; \
211 		} \
212 		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
213 		if(t2<=(NFSMSIZ(mb)-mb->m_len)){ \
214 			nfsm_build(tl,u_long *,t2); \
215 			*tl++ = txdr_unsigned(s); \
216 			*(tl+((t2>>2)-2)) = 0; \
217 			bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
218 		} else if (error = nfsm_strtmbuf(&mb, &bpos, (a), (s))) { \
219 			m_freem(mreq); \
220 			goto nfsmout; \
221 		}
222 
223 #define	nfsm_srvdone \
224 		nfsmout: \
225 		return(error)
226 
227 #ifndef lint
228 #define	nfsm_reply(s) \
229 		{ \
230 		*repstat = error; \
231 		if (error) \
232 			nfs_rephead(0, xid, error, mrq, &mb, &bpos); \
233 		else \
234 			nfs_rephead((s), xid, error, mrq, &mb, &bpos); \
235 		m_freem(mrep); \
236 		mreq = *mrq; \
237 		if (error) \
238 			return(0); \
239 		}
240 #else	/* lint */
241 #define	nfsm_reply(s) \
242 		{ \
243 		*repstat = error; \
244 		if (error) \
245 			nfs_rephead(0, xid, error, mrq, &mb, &bpos); \
246 		else \
247 			nfs_rephead((s), xid, error, mrq, &mb, &bpos); \
248 		m_freem(mrep); \
249 		mreq = *mrq; \
250 		mrep = mreq; \
251 		if (error) \
252 			return(0); \
253 		}
254 #endif	/* lint */
255 
256 #define	nfsm_adv(s) \
257 		t1 = mtod(md, caddr_t)+md->m_len-dpos; \
258 		if (t1 >= (s)) { \
259 			dpos += (s); \
260 		} else if (error = nfs_adv(&md, &dpos, (s), t1)) { \
261 			m_freem(mrep); \
262 			goto nfsmout; \
263 		}
264 
265 #define nfsm_srvmtofh(f) \
266 		nfsm_disecton(tl, u_long *, NFSX_FH); \
267 		bcopy((caddr_t)tl, (caddr_t)f, NFSX_FH)
268 
269 #define	nfsm_clget \
270 		if (bp >= be) { \
271 			MGET(mp, M_WAIT, MT_DATA); \
272 			MCLGET(mp, M_WAIT); \
273 			mp->m_len = NFSMSIZ(mp); \
274 			if (mp3 == NULL) \
275 				mp3 = mp2 = mp; \
276 			else { \
277 				mp2->m_next = mp; \
278 				mp2 = mp; \
279 			} \
280 			bp = mtod(mp, caddr_t); \
281 			be = bp+mp->m_len; \
282 		} \
283 		tl = (u_long *)bp
284 
285 #if 0 /* va_atime.tv_usec is not unused (jfw@ksr.com 6/2/93) */
286 #define	nfsm_srvfillattr \
287 	fp->fa_type = vtonfs_type(vap->va_type); \
288 	fp->fa_mode = vtonfs_mode(vap->va_type, vap->va_mode); \
289 	fp->fa_nlink = txdr_unsigned(vap->va_nlink); \
290 	fp->fa_uid = txdr_unsigned(vap->va_uid); \
291 	fp->fa_gid = txdr_unsigned(vap->va_gid); \
292 	fp->fa_size = txdr_unsigned(vap->va_size); \
293 	fp->fa_blocksize = txdr_unsigned(vap->va_blocksize); \
294 	if (vap->va_type == VFIFO) \
295 		fp->fa_rdev = 0xffffffff; \
296 	else \
297 		fp->fa_rdev = txdr_unsigned(vap->va_rdev); \
298 	fp->fa_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); \
299 	fp->fa_fsid = txdr_unsigned(vap->va_fsid); \
300 	fp->fa_fileid = txdr_unsigned(vap->va_fileid); \
301 	fp->fa_atime.tv_sec = txdr_unsigned(vap->va_atime.tv_sec); \
302 	fp->fa_atime.tv_usec = txdr_unsigned(vap->va_flags); \
303 	txdr_time(&vap->va_mtime, &fp->fa_mtime); \
304 	fp->fa_ctime.tv_sec = txdr_unsigned(vap->va_ctime.tv_sec); \
305 	fp->fa_ctime.tv_usec = txdr_unsigned(vap->va_gen)
306 #else /* don't try to extend the poor NFS protocol */
307 #define	nfsm_srvfillattr \
308 	fp->fa_type = vtonfs_type(vap->va_type); \
309 	fp->fa_mode = vtonfs_mode(vap->va_type, vap->va_mode); \
310 	fp->fa_nlink = txdr_unsigned(vap->va_nlink); \
311 	fp->fa_uid = txdr_unsigned(vap->va_uid); \
312 	fp->fa_gid = txdr_unsigned(vap->va_gid); \
313 	fp->fa_size = txdr_unsigned(vap->va_size); \
314 	fp->fa_blocksize = txdr_unsigned(vap->va_blocksize); \
315 	if (vap->va_type == VFIFO) \
316 		fp->fa_rdev = 0xffffffff; \
317 	else \
318 		fp->fa_rdev = txdr_unsigned(vap->va_rdev); \
319 	fp->fa_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); \
320 	fp->fa_fsid = txdr_unsigned(vap->va_fsid); \
321 	fp->fa_fileid = txdr_unsigned(vap->va_fileid); \
322 	txdr_time(&vap->va_atime, &fp->fa_atime); \
323 	txdr_time(&vap->va_mtime, &fp->fa_mtime); \
324         txdr_time(&vap->va_ctime, &fp->fa_ctime);
325 #endif
326 
327 #endif /* !_NFS_NFSM_SUBS_H_ */
328