nfsm_subs.h revision 1.40
1/*	$OpenBSD: nfsm_subs.h,v 1.40 2009/07/30 14:04:28 thib 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 =						\
76		  nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0) {	\
77		error = t1;						\
78		m_freem(mrep);						\
79		goto nfsmout;						\
80	} else {							\
81		(a) = (c)cp2;						\
82	}								\
83}
84
85#define nfsm_srvpostop_fh(f) {						\
86	tl = nfsm_build(&mb, 2 * NFSX_UNSIGNED + NFSX_V3FH);		\
87	*tl++ = nfs_true;						\
88	*tl++ = txdr_unsigned(NFSX_V3FH);				\
89	bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH);			\
90}
91
92#define nfsm_mtofh(d, v, v3, f)	{					\
93	struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize;		\
94	if (v3) {							\
95		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);		\
96		(f) = fxdr_unsigned(int, *tl);				\
97	} else								\
98		(f) = 1;						\
99	if (f) {							\
100		nfsm_getfh(ttfhp, ttfhsize, (v3));			\
101		if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, 	\
102		    &ttnp)) != 0) {					\
103			error = t1;					\
104			m_freem(mrep);					\
105			goto nfsmout;					\
106		}							\
107		(v) = NFSTOV(ttnp);					\
108	}								\
109	if (v3) {							\
110		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);		\
111		if (f)							\
112			(f) = fxdr_unsigned(int, *tl);			\
113		else if (fxdr_unsigned(int, *tl))			\
114			nfsm_adv(NFSX_V3FATTR);				\
115	}								\
116	if (f)								\
117		nfsm_loadattr((v), NULL);				\
118}
119
120#define nfsm_getfh(f, s, v3) {						\
121	if (v3) {							\
122		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);		\
123		if (((s) = fxdr_unsigned(int, *tl)) <= 0 ||		\
124			(s) > NFSX_V3FHMAX) {				\
125			m_freem(mrep);					\
126			error = EBADRPC;				\
127			goto nfsmout;					\
128		}							\
129	} else								\
130		(s) = NFSX_V2FH;					\
131	nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); 			\
132}
133
134#define nfsm_loadattr(v, a) {						\
135	struct vnode *ttvp = (v);					\
136	if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a))) != 0) {	\
137		error = t1;						\
138		m_freem(mrep);						\
139		goto nfsmout;						\
140	}								\
141	(v) = ttvp;							\
142}
143
144#define nfsm_postop_attr(v, f) { if (mrep != NULL) {			\
145	struct vnode *ttvp = (v);					\
146	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);			\
147	if (((f) = fxdr_unsigned(int, *tl)) != 0) {			\
148		if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos,		\
149			NULL)) != 0) {					\
150			error = t1;					\
151			(f) = 0;					\
152			m_freem(mrep);					\
153			goto nfsmout;					\
154		}							\
155		(v) = ttvp;						\
156	}								\
157} }
158
159/* Used as (f) for nfsm_wcc_data() */
160#define NFSV3_WCCRATTR	0
161#define NFSV3_WCCCHK	1
162
163#define nfsm_wcc_data(v, f) do { if (mrep != NULL) {			\
164	struct timespec	 _mtime;					\
165	int		 ttattrf, ttretf = 0;				\
166									\
167	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);			\
168	if (*tl == nfs_true) {						\
169		nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);	\
170		fxdr_nfsv3time(tl + 2, &_mtime);			\
171		if (f) {						\
172			ttretf = timespeccmp(&VTONFS(v)->n_mtime,	\
173			    &_mtime, !=);				\
174		}							\
175	}								\
176	nfsm_postop_attr((v), ttattrf);					\
177	if (f) {							\
178		(f) = ttretf;						\
179	} else {							\
180		(f) = ttattrf;						\
181	}								\
182} } while (0)
183
184#define nfsm_strsiz(s,m) {						\
185	nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED);			\
186	if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) {			\
187		m_freem(mrep);						\
188		error = EBADRPC;					\
189		goto nfsmout;						\
190	}								\
191}
192
193#define nfsm_srvnamesiz(s) {						\
194	nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED);			\
195	if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) 	\
196		error = NFSERR_NAMETOL;					\
197	if ((s) <= 0)							\
198		error = EBADRPC;					\
199	if (error)							\
200		nfsm_reply(0);						\
201}
202
203#define nfsm_mtouio(p,s)						\
204	if ((s) > 0 &&							\
205	    (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) {		\
206		error = t1;						\
207		m_freem(mrep);						\
208		goto nfsmout;						\
209	}
210
211#define nfsm_rndup(a)	(((a)+3)&(~0x3))
212
213#define nfsm_strtom(a,s,m)						\
214	if ((s) > (m)) {						\
215		m_freem(mreq);						\
216		error = ENAMETOOLONG;					\
217		goto nfsmout;						\
218	}								\
219	nfsm_strtombuf(&mb, (a), (s))
220
221#define nfsm_reply(s) {							\
222	nfsd->nd_repstat = error;					\
223	if (error && !(nfsd->nd_flag & ND_NFSV3))			\
224	   (void) nfs_rephead(0, nfsd, slp, error,			\
225		mrq, &mb);						\
226	else								\
227	   (void) nfs_rephead((s), nfsd, slp, error,			\
228		mrq, &mb);						\
229	if (mrep != NULL) {						\
230		m_freem(mrep);						\
231		mrep = NULL;						\
232	}								\
233	mreq = *mrq;							\
234	if (error && (!(nfsd->nd_flag & ND_NFSV3) || error == EBADRPC))	\
235		return(0);						\
236}
237
238#define nfsm_writereply(s, v3) {					\
239	nfsd->nd_repstat = error;					\
240	if (error && !(v3))						\
241	   (void) nfs_rephead(0, nfsd, slp, error, &mreq, &mb);		\
242	else								\
243	   (void) nfs_rephead((s), nfsd, slp, error, &mreq, &mb);	\
244}
245
246#define nfsm_adv(s) {							\
247	t1 = mtod(md, caddr_t)+md->m_len-dpos;				\
248	if (t1 >= (s)) {						\
249		dpos += (s);						\
250	} else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) {		\
251		error = t1;						\
252		m_freem(mrep);						\
253		goto nfsmout;						\
254	}								\
255}
256
257#define nfsm_srvmtofh(f) {						\
258	if (nfsd->nd_flag & ND_NFSV3) {					\
259		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);		\
260		if (fxdr_unsigned(int, *tl) != NFSX_V3FH) {		\
261			error = EBADRPC;				\
262			nfsm_reply(0);					\
263		}							\
264	}								\
265	nfsm_dissect(tl, u_int32_t *, NFSX_V3FH);			\
266	bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH);			\
267	if ((nfsd->nd_flag & ND_NFSV3) == 0)				\
268	nfsm_adv(NFSX_V2FH - NFSX_V3FH);				\
269}
270
271#endif
272