nfsm_subs.h revision 1.36
1/*	$OpenBSD: nfsm_subs.h,v 1.36 2009/06/04 00:31:42 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	NFSMSIZ(m)	((M_HASCL(m)) ? (m)->m_ext.ext_size : \
55				(((m)->m_flags & M_PKTHDR) ? MHLEN : MLEN))
56
57/*
58 * Now for the macros that do the simple stuff and call the functions
59 * for the hard stuff.
60 * These macros use several vars. declared in nfsm_reqhead and these
61 * vars. must not be used elsewhere unless you are careful not to corrupt
62 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
63 * that may be used so long as the value is not expected to retained
64 * after a macro.
65 * I know, this is kind of dorkey, but it makes the actual op functions
66 * fairly clean and deals with the mess caused by the xdr discriminating
67 * unions.
68 */
69
70#define	nfsm_dissect(a, c, s) \
71		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
72		if (t1 >= (s)) { \
73			(a) = (c)(dpos); \
74			dpos += (s); \
75		} else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
76			error = t1; \
77			m_freem(mrep); \
78			goto nfsmout; \
79		} else { \
80			(a) = (c)cp2; \
81		} }
82
83#define nfsm_srvpostop_fh(f) \
84		{ tl = nfsm_build(&mb, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
85		*tl++ = nfs_true; \
86		*tl++ = txdr_unsigned(NFSX_V3FH); \
87		bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
88		}
89
90#define nfsm_mtofh(d, v, v3, f) \
91		{ struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
92		if (v3) { \
93			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
94			(f) = fxdr_unsigned(int, *tl); \
95		} else \
96			(f) = 1; \
97		if (f) { \
98			nfsm_getfh(ttfhp, ttfhsize, (v3)); \
99			if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
100				&ttnp)) != 0) { \
101				error = t1; \
102				m_freem(mrep); \
103				goto nfsmout; \
104			} \
105			(v) = NFSTOV(ttnp); \
106		} \
107		if (v3) { \
108			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
109			if (f) \
110				(f) = fxdr_unsigned(int, *tl); \
111			else if (fxdr_unsigned(int, *tl)) \
112				nfsm_adv(NFSX_V3FATTR); \
113		} \
114		if (f) \
115			nfsm_loadattr((v), (struct vattr *)0); \
116		}
117
118#define nfsm_getfh(f, s, v3) \
119		{ if (v3) { \
120			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
121			if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
122				(s) > NFSX_V3FHMAX) { \
123				m_freem(mrep); \
124				error = EBADRPC; \
125				goto nfsmout; \
126			} \
127		} else \
128			(s) = NFSX_V2FH; \
129		nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
130
131#define	nfsm_loadattr(v, a) \
132		{ struct vnode *ttvp = (v); \
133		if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a))) != 0) { \
134			error = t1; \
135			m_freem(mrep); \
136			goto nfsmout; \
137		} \
138		(v) = ttvp; }
139
140#define	nfsm_postop_attr(v, f) \
141		{ struct vnode *ttvp = (v); \
142		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
143		if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
144			if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
145				(struct vattr *)0)) != 0) { \
146				error = t1; \
147				(f) = 0; \
148				m_freem(mrep); \
149				goto nfsmout; \
150			} \
151			(v) = ttvp; \
152		} }
153
154/* Used as (f) for nfsm_wcc_data() */
155#define NFSV3_WCCRATTR	0
156#define NFSV3_WCCCHK	1
157
158#define	nfsm_wcc_data(v, f) do {					\
159	struct timespec	 _mtime;					\
160	int		 ttattrf, ttretf = 0;				\
161									\
162	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);			\
163	if (*tl == nfs_true) {						\
164		nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);	\
165		fxdr_nfsv3time(tl + 2, &_mtime);			\
166		if (f) {						\
167			ttretf = timespeccmp(&VTONFS(v)->n_mtime,	\
168			    &_mtime, !=);				\
169		}							\
170	}								\
171	nfsm_postop_attr((v), ttattrf);					\
172	if (f) {							\
173		(f) = ttretf;						\
174	} else {							\
175		(f) = ttattrf;						\
176	}								\
177} while (0)
178
179#define	nfsm_strsiz(s,m) \
180		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
181		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
182			m_freem(mrep); \
183			error = EBADRPC; \
184			goto nfsmout; \
185		} }
186
187#define	nfsm_srvnamesiz(s) \
188		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
189		if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
190			error = NFSERR_NAMETOL; \
191		if ((s) <= 0) \
192			error = EBADRPC; \
193		if (error) \
194			nfsm_reply(0); \
195		}
196
197#define nfsm_mtouio(p,s) \
198		if ((s) > 0 && \
199		   (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
200			error = t1; \
201			m_freem(mrep); \
202			goto nfsmout; \
203		}
204
205#define nfsm_rndup(a)	(((a)+3)&(~0x3))
206
207#define	nfsm_strtom(a,s,m) \
208		if ((s) > (m)) { \
209			m_freem(mreq); \
210			error = ENAMETOOLONG; \
211			goto nfsmout; \
212		} \
213		nfsm_strtombuf(&mb, (a), (s))
214
215#define	nfsm_reply(s) \
216		{ \
217		nfsd->nd_repstat = error; \
218		if (error && !(nfsd->nd_flag & ND_NFSV3)) \
219		   (void) nfs_rephead(0, nfsd, slp, error, \
220			mrq, &mb); \
221		else \
222		   (void) nfs_rephead((s), nfsd, slp, error, \
223			mrq, &mb); \
224		if (mrep != NULL) { \
225			m_freem(mrep); \
226			mrep = NULL; \
227		} \
228		mreq = *mrq; \
229		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
230			error == EBADRPC)) \
231			return(0); \
232		}
233
234#define	nfsm_writereply(s, v3) \
235		{ \
236		nfsd->nd_repstat = error; \
237		if (error && !(v3)) \
238		   (void) nfs_rephead(0, nfsd, slp, error, \
239			&mreq, &mb); \
240		else \
241		   (void) nfs_rephead((s), nfsd, slp, error, \
242			&mreq, &mb); \
243		}
244
245#define	nfsm_adv(s) \
246		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
247		if (t1 >= (s)) { \
248			dpos += (s); \
249		} else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
250			error = t1; \
251			m_freem(mrep); \
252			goto nfsmout; \
253		} }
254
255#define nfsm_srvmtofh(f) \
256		{ if (nfsd->nd_flag & ND_NFSV3) { \
257			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
258			if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
259				error = EBADRPC; \
260				nfsm_reply(0); \
261			} \
262		} \
263		nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
264		bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
265		if ((nfsd->nd_flag & ND_NFSV3) == 0) \
266			nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
267		}
268
269#endif
270