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