nfsm_subs.h revision 1.31
1/*	$OpenBSD: nfsm_subs.h,v 1.31 2008/06/15 04:03:40 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 = 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) \
174		{ int ttattrf, ttretf = 0; \
175		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
176		if (*tl == nfs_true) { \
177			nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
178			if (f) \
179				ttretf = (VTONFS(v)->n_mtime == \
180					fxdr_unsigned(u_int32_t, *(tl + 2))); \
181		} \
182		nfsm_postop_attr((v), ttattrf); \
183		if (f) { \
184			(f) = ttretf; \
185		} else { \
186			(f) = ttattrf; \
187		} }
188
189#define	nfsm_strsiz(s,m) \
190		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
191		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
192			m_freem(mrep); \
193			error = EBADRPC; \
194			goto nfsmout; \
195		} }
196
197#define	nfsm_srvstrsiz(s,m) \
198		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
199		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
200			error = EBADRPC; \
201			nfsm_reply(0); \
202		} }
203
204#define	nfsm_srvnamesiz(s) \
205		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
206		if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
207			error = NFSERR_NAMETOL; \
208		if ((s) <= 0) \
209			error = EBADRPC; \
210		if (error) \
211			nfsm_reply(0); \
212		}
213
214#define nfsm_mtouio(p,s) \
215		if ((s) > 0 && \
216		   (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
217			error = t1; \
218			m_freem(mrep); \
219			goto nfsmout; \
220		}
221
222#define nfsm_rndup(a)	(((a)+3)&(~0x3))
223
224#define	nfsm_request(v, t, p, c)	\
225		if ((error = nfs_request((v), mreq, (t), (p), \
226		   (c), &mrep, &md, &dpos)) != 0) { \
227			if (error & NFSERR_RETERR) \
228				error &= ~NFSERR_RETERR; \
229			else \
230				goto nfsmout; \
231		}
232
233#define	nfsm_strtom(a,s,m) \
234		if ((s) > (m)) { \
235			m_freem(mreq); \
236			error = ENAMETOOLONG; \
237			goto nfsmout; \
238		} \
239		nfsm_strtombuf(&mb, (a), (s))
240
241#define	nfsm_reply(s) \
242		{ \
243		nfsd->nd_repstat = error; \
244		if (error && !(nfsd->nd_flag & ND_NFSV3)) \
245		   (void) nfs_rephead(0, nfsd, slp, error, \
246			mrq, &mb); \
247		else \
248		   (void) nfs_rephead((s), nfsd, slp, error, \
249			mrq, &mb); \
250		if (mrep != NULL) { \
251			m_freem(mrep); \
252			mrep = NULL; \
253		} \
254		mreq = *mrq; \
255		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
256			error == EBADRPC)) \
257			return(0); \
258		}
259
260#define	nfsm_writereply(s, v3) \
261		{ \
262		nfsd->nd_repstat = error; \
263		if (error && !(v3)) \
264		   (void) nfs_rephead(0, nfsd, slp, error, \
265			&mreq, &mb); \
266		else \
267		   (void) nfs_rephead((s), nfsd, slp, error, \
268			&mreq, &mb); \
269		}
270
271#define	nfsm_adv(s) \
272		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
273		if (t1 >= (s)) { \
274			dpos += (s); \
275		} else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
276			error = t1; \
277			m_freem(mrep); \
278			goto nfsmout; \
279		} }
280
281#define nfsm_srvmtofh(f) \
282		{ if (nfsd->nd_flag & ND_NFSV3) { \
283			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
284			if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
285				error = EBADRPC; \
286				nfsm_reply(0); \
287			} \
288		} \
289		nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
290		bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
291		if ((nfsd->nd_flag & ND_NFSV3) == 0) \
292			nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
293		}
294
295#define nfsm_srvsattr(a) \
296		{ nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
297		if (*tl == nfs_true) { \
298			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
299			(a)->va_mode = nfstov_mode(*tl); \
300		} \
301		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
302		if (*tl == nfs_true) { \
303			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
304			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
305		} \
306		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
307		if (*tl == nfs_true) { \
308			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
309			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
310		} \
311		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
312		if (*tl == nfs_true) { \
313			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
314			(a)->va_size = fxdr_hyper(tl); \
315		} \
316		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
317		switch (fxdr_unsigned(int, *tl)) { \
318		case NFSV3SATTRTIME_TOCLIENT: \
319			(a)->va_vaflags &= ~VA_UTIMES_NULL; \
320			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
321			fxdr_nfsv3time(tl, &(a)->va_atime); \
322			break; \
323		case NFSV3SATTRTIME_TOSERVER: \
324			getnanotime(&(a)->va_atime); \
325			break; \
326		}; \
327		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
328		switch (fxdr_unsigned(int, *tl)) { \
329		case NFSV3SATTRTIME_TOCLIENT: \
330			(a)->va_vaflags &= ~VA_UTIMES_NULL; \
331			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
332			fxdr_nfsv3time(tl, &(a)->va_mtime); \
333			break; \
334		case NFSV3SATTRTIME_TOSERVER: \
335			getnanotime(&(a)->va_mtime); \
336			break; \
337		}; }
338
339#endif
340