nfsm_subs.h revision 1.23
1/*	$OpenBSD: nfsm_subs.h,v 1.23 2007/12/13 18:32:55 blambert Exp $	*/
2/*	$NetBSD: nfsm_subs.h,v 1.10 1996/03/20 21:59:56 fvdl Exp $	*/
3
4/*
5 * Copyright (c) 1989, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Rick Macklem at The University of Guelph.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	@(#)nfsm_subs.h	8.2 (Berkeley) 3/30/95
36 */
37
38
39#ifndef _NFS_NFSM_SUBS_H_
40#define _NFS_NFSM_SUBS_H_
41
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
53#define	M_HASCL(m)	((m)->m_flags & M_EXT)
54#define	NFSMADV(m, s)	(m)->m_data += (s)
55#define	NFSMSIZ(m)	((M_HASCL(m)) ? (m)->m_ext.ext_size : \
56				(((m)->m_flags & M_PKTHDR) ? MHLEN : MLEN))
57
58/*
59 * Now for the macros that do the simple stuff and call the functions
60 * for the hard stuff.
61 * These macros use several vars. declared in nfsm_reqhead and these
62 * vars. must not be used elsewhere unless you are careful not to corrupt
63 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
64 * that may be used so long as the value is not expected to retained
65 * after a macro.
66 * I know, this is kind of dorkey, but it makes the actual op functions
67 * fairly clean and deals with the mess caused by the xdr discriminating
68 * unions.
69 */
70
71#define	nfsm_build(a,c,s) \
72		{ if ((s) > M_TRAILINGSPACE(mb)) { \
73			MGET(mb2, M_WAIT, MT_DATA); \
74			if ((s) > MLEN) \
75				panic("build > MLEN"); \
76			mb->m_next = mb2; \
77			mb = mb2; \
78			mb->m_len = 0; \
79			bpos = mtod(mb, caddr_t); \
80		} \
81		(a) = (c)(bpos); \
82		mb->m_len += (s); \
83		bpos += (s); }
84
85#define	nfsm_dissect(a, c, s) \
86		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
87		if (t1 >= (s)) { \
88			(a) = (c)(dpos); \
89			dpos += (s); \
90		} else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
91			error = t1; \
92			m_freem(mrep); \
93			goto nfsmout; \
94		} else { \
95			(a) = (c)cp2; \
96		} }
97
98#define nfsm_fhtom(v, v3) \
99	      { if (v3) { \
100			t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
101			if (t2 <= M_TRAILINGSPACE(mb)) { \
102				nfsm_build(tl, u_int32_t *, t2); \
103				*tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
104				*(tl + ((t2>>2) - 2)) = 0; \
105				bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
106					VTONFS(v)->n_fhsize); \
107			} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
108				(caddr_t)VTONFS(v)->n_fhp, \
109				  VTONFS(v)->n_fhsize)) != 0) { \
110				error = t2; \
111				m_freem(mreq); \
112				goto nfsmout; \
113			} \
114		} else { \
115			nfsm_build(cp, caddr_t, NFSX_V2FH); \
116			bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
117		} }
118
119#define nfsm_srvfhtom(f, v3) \
120		{ if (v3) { \
121			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FH); \
122			*tl++ = txdr_unsigned(NFSX_V3FH); \
123			bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
124		} else { \
125			nfsm_build(cp, caddr_t, NFSX_V2FH); \
126			bcopy((caddr_t)(f), cp, NFSX_V2FH); \
127		} }
128
129#define nfsm_srvpostop_fh(f) \
130		{ nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
131		*tl++ = nfs_true; \
132		*tl++ = txdr_unsigned(NFSX_V3FH); \
133		bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
134		}
135
136#define nfsm_mtofh(d, v, v3, f) \
137		{ struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
138		if (v3) { \
139			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
140			(f) = fxdr_unsigned(int, *tl); \
141		} else \
142			(f) = 1; \
143		if (f) { \
144			nfsm_getfh(ttfhp, ttfhsize, (v3)); \
145			if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
146				&ttnp)) != 0) { \
147				error = t1; \
148				m_freem(mrep); \
149				goto nfsmout; \
150			} \
151			(v) = NFSTOV(ttnp); \
152		} \
153		if (v3) { \
154			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
155			if (f) \
156				(f) = fxdr_unsigned(int, *tl); \
157			else if (fxdr_unsigned(int, *tl)) \
158				nfsm_adv(NFSX_V3FATTR); \
159		} \
160		if (f) \
161			nfsm_loadattr((v), (struct vattr *)0); \
162		}
163
164#define nfsm_getfh(f, s, v3) \
165		{ if (v3) { \
166			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
167			if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
168				(s) > NFSX_V3FHMAX) { \
169				m_freem(mrep); \
170				error = EBADRPC; \
171				goto nfsmout; \
172			} \
173		} else \
174			(s) = NFSX_V2FH; \
175		nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
176
177#define	nfsm_loadattr(v, a) \
178		{ struct vnode *ttvp = (v); \
179		if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a))) != 0) { \
180			error = t1; \
181			m_freem(mrep); \
182			goto nfsmout; \
183		} \
184		(v) = ttvp; }
185
186#define	nfsm_postop_attr(v, f) \
187		{ struct vnode *ttvp = (v); \
188		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
189		if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
190			if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
191				(struct vattr *)0)) != 0) { \
192				error = t1; \
193				(f) = 0; \
194				m_freem(mrep); \
195				goto nfsmout; \
196			} \
197			(v) = ttvp; \
198		} }
199
200/* Used as (f) for nfsm_wcc_data() */
201#define NFSV3_WCCRATTR	0
202#define NFSV3_WCCCHK	1
203
204#define	nfsm_wcc_data(v, f) \
205		{ int ttattrf, ttretf = 0; \
206		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
207		if (*tl == nfs_true) { \
208			nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
209			if (f) \
210				ttretf = (VTONFS(v)->n_mtime == \
211					fxdr_unsigned(u_int32_t, *(tl + 2))); \
212		} \
213		nfsm_postop_attr((v), ttattrf); \
214		if (f) { \
215			(f) = ttretf; \
216		} else { \
217			(f) = ttattrf; \
218		} }
219
220#define	nfsm_strsiz(s,m) \
221		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
222		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
223			m_freem(mrep); \
224			error = EBADRPC; \
225			goto nfsmout; \
226		} }
227
228#define	nfsm_srvstrsiz(s,m) \
229		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
230		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
231			error = EBADRPC; \
232			nfsm_reply(0); \
233		} }
234
235#define	nfsm_srvnamesiz(s) \
236		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
237		if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
238			error = NFSERR_NAMETOL; \
239		if ((s) <= 0) \
240			error = EBADRPC; \
241		if (error) \
242			nfsm_reply(0); \
243		}
244
245#define nfsm_mtouio(p,s) \
246		if ((s) > 0 && \
247		   (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
248			error = t1; \
249			m_freem(mrep); \
250			goto nfsmout; \
251		}
252
253#define	nfsm_reqhead(v,a,s) \
254		mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
255
256#define nfsm_rndup(a)	(((a)+3)&(~0x3))
257
258#define	nfsm_request(v, t, p, c)	\
259		if ((error = nfs_request((v), mreq, (t), (p), \
260		   (c), &mrep, &md, &dpos)) != 0) { \
261			if (error & NFSERR_RETERR) \
262				error &= ~NFSERR_RETERR; \
263			else \
264				goto nfsmout; \
265		}
266
267#define	nfsm_strtom(a,s,m) \
268		if ((s) > (m)) { \
269			m_freem(mreq); \
270			error = ENAMETOOLONG; \
271			goto nfsmout; \
272		} \
273		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
274		if (t2 <= M_TRAILINGSPACE(mb)) { \
275			nfsm_build(tl,u_int32_t *,t2); \
276			*tl++ = txdr_unsigned(s); \
277			*(tl+((t2>>2)-2)) = 0; \
278			bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
279		} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
280			error = t2; \
281			m_freem(mreq); \
282			goto nfsmout; \
283		}
284
285#define	nfsm_reply(s) \
286		{ \
287		nfsd->nd_repstat = error; \
288		if (error && !(nfsd->nd_flag & ND_NFSV3)) \
289		   (void) nfs_rephead(0, nfsd, slp, error, \
290			mrq, &mb, &bpos); \
291		else \
292		   (void) nfs_rephead((s), nfsd, slp, error, \
293			mrq, &mb, &bpos); \
294		if (mrep != NULL) { \
295			m_freem(mrep); \
296			mrep = NULL; \
297		} \
298		mreq = *mrq; \
299		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
300			error == EBADRPC)) \
301			return(0); \
302		}
303
304#define	nfsm_writereply(s, v3) \
305		{ \
306		nfsd->nd_repstat = error; \
307		if (error && !(v3)) \
308		   (void) nfs_rephead(0, nfsd, slp, error, \
309			&mreq, &mb, &bpos); \
310		else \
311		   (void) nfs_rephead((s), nfsd, slp, error, \
312			&mreq, &mb, &bpos); \
313		}
314
315#define	nfsm_adv(s) \
316		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
317		if (t1 >= (s)) { \
318			dpos += (s); \
319		} else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
320			error = t1; \
321			m_freem(mrep); \
322			goto nfsmout; \
323		} }
324
325#define nfsm_srvmtofh(f) \
326		{ if (nfsd->nd_flag & ND_NFSV3) { \
327			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
328			if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
329				error = EBADRPC; \
330				nfsm_reply(0); \
331			} \
332		} \
333		nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
334		bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
335		if ((nfsd->nd_flag & ND_NFSV3) == 0) \
336			nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
337		}
338
339#define	nfsm_clget \
340		if (bp >= be) { \
341			if (mp == mb) \
342				mp->m_len += bp-bpos; \
343			MGET(mp, M_WAIT, MT_DATA); \
344			MCLGET(mp, M_WAIT); \
345			mp->m_len = NFSMSIZ(mp); \
346			mp2->m_next = mp; \
347			mp2 = mp; \
348			bp = mtod(mp, caddr_t); \
349			be = bp+mp->m_len; \
350		} \
351		tl = (u_int32_t *)bp
352
353#define nfsm_srvpostop_attr(r, a) \
354		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
355
356#define nfsm_srvsattr(a) \
357		{ nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
358		if (*tl == nfs_true) { \
359			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
360			(a)->va_mode = nfstov_mode(*tl); \
361		} \
362		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
363		if (*tl == nfs_true) { \
364			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
365			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
366		} \
367		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
368		if (*tl == nfs_true) { \
369			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
370			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
371		} \
372		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
373		if (*tl == nfs_true) { \
374			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
375			(a)->va_size = fxdr_hyper(tl); \
376		} \
377		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
378		switch (fxdr_unsigned(int, *tl)) { \
379		case NFSV3SATTRTIME_TOCLIENT: \
380			(a)->va_vaflags &= ~VA_UTIMES_NULL; \
381			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
382			fxdr_nfsv3time(tl, &(a)->va_atime); \
383			break; \
384		case NFSV3SATTRTIME_TOSERVER: \
385			getnanotime(&(a)->va_atime); \
386			break; \
387		}; \
388		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
389		switch (fxdr_unsigned(int, *tl)) { \
390		case NFSV3SATTRTIME_TOCLIENT: \
391			(a)->va_vaflags &= ~VA_UTIMES_NULL; \
392			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
393			fxdr_nfsv3time(tl, &(a)->va_mtime); \
394			break; \
395		case NFSV3SATTRTIME_TOSERVER: \
396			getnanotime(&(a)->va_mtime); \
397			break; \
398		}; }
399
400#endif
401