nfsm_subs.h revision 1.24
1/*	$OpenBSD: nfsm_subs.h,v 1.24 2008/01/06 17:38:23 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_dissect(a, c, s) \
72		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
73		if (t1 >= (s)) { \
74			(a) = (c)(dpos); \
75			dpos += (s); \
76		} else if ((t1 = 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#define nfsm_fhtom(v, v3) \
85	      { if (v3) { \
86			t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
87			if (t2 <= M_TRAILINGSPACE(mb)) { \
88				tl = nfsm_build(&mb, t2, &bpos); \
89				*tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
90				*(tl + ((t2>>2) - 2)) = 0; \
91				bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
92					VTONFS(v)->n_fhsize); \
93			} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
94				(caddr_t)VTONFS(v)->n_fhp, \
95				  VTONFS(v)->n_fhsize)) != 0) { \
96				error = t2; \
97				m_freem(mreq); \
98				goto nfsmout; \
99			} \
100		} else { \
101			cp = nfsm_build(&mb, NFSX_V2FH, &bpos); \
102			bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
103		} }
104
105#define nfsm_srvfhtom(f, v3) \
106		{ if (v3) { \
107			tl = nfsm_build(&mb, NFSX_UNSIGNED + NFSX_V3FH,&bpos); \
108			*tl++ = txdr_unsigned(NFSX_V3FH); \
109			bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
110		} else { \
111			cp = nfsm_build(&mb, NFSX_V2FH, &bpos); \
112			bcopy((caddr_t)(f), cp, NFSX_V2FH); \
113		} }
114
115#define nfsm_srvpostop_fh(f) \
116		{ tl = nfsm_build(&mb, 2 * NFSX_UNSIGNED + NFSX_V3FH, &bpos); \
117		*tl++ = nfs_true; \
118		*tl++ = txdr_unsigned(NFSX_V3FH); \
119		bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
120		}
121
122#define nfsm_mtofh(d, v, v3, f) \
123		{ struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
124		if (v3) { \
125			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
126			(f) = fxdr_unsigned(int, *tl); \
127		} else \
128			(f) = 1; \
129		if (f) { \
130			nfsm_getfh(ttfhp, ttfhsize, (v3)); \
131			if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
132				&ttnp)) != 0) { \
133				error = t1; \
134				m_freem(mrep); \
135				goto nfsmout; \
136			} \
137			(v) = NFSTOV(ttnp); \
138		} \
139		if (v3) { \
140			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
141			if (f) \
142				(f) = fxdr_unsigned(int, *tl); \
143			else if (fxdr_unsigned(int, *tl)) \
144				nfsm_adv(NFSX_V3FATTR); \
145		} \
146		if (f) \
147			nfsm_loadattr((v), (struct vattr *)0); \
148		}
149
150#define nfsm_getfh(f, s, v3) \
151		{ if (v3) { \
152			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
153			if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
154				(s) > NFSX_V3FHMAX) { \
155				m_freem(mrep); \
156				error = EBADRPC; \
157				goto nfsmout; \
158			} \
159		} else \
160			(s) = NFSX_V2FH; \
161		nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
162
163#define	nfsm_loadattr(v, a) \
164		{ struct vnode *ttvp = (v); \
165		if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a))) != 0) { \
166			error = t1; \
167			m_freem(mrep); \
168			goto nfsmout; \
169		} \
170		(v) = ttvp; }
171
172#define	nfsm_postop_attr(v, f) \
173		{ struct vnode *ttvp = (v); \
174		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
175		if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
176			if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
177				(struct vattr *)0)) != 0) { \
178				error = t1; \
179				(f) = 0; \
180				m_freem(mrep); \
181				goto nfsmout; \
182			} \
183			(v) = ttvp; \
184		} }
185
186/* Used as (f) for nfsm_wcc_data() */
187#define NFSV3_WCCRATTR	0
188#define NFSV3_WCCCHK	1
189
190#define	nfsm_wcc_data(v, f) \
191		{ int ttattrf, ttretf = 0; \
192		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
193		if (*tl == nfs_true) { \
194			nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
195			if (f) \
196				ttretf = (VTONFS(v)->n_mtime == \
197					fxdr_unsigned(u_int32_t, *(tl + 2))); \
198		} \
199		nfsm_postop_attr((v), ttattrf); \
200		if (f) { \
201			(f) = ttretf; \
202		} else { \
203			(f) = ttattrf; \
204		} }
205
206#define	nfsm_strsiz(s,m) \
207		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
208		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
209			m_freem(mrep); \
210			error = EBADRPC; \
211			goto nfsmout; \
212		} }
213
214#define	nfsm_srvstrsiz(s,m) \
215		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
216		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
217			error = EBADRPC; \
218			nfsm_reply(0); \
219		} }
220
221#define	nfsm_srvnamesiz(s) \
222		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
223		if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
224			error = NFSERR_NAMETOL; \
225		if ((s) <= 0) \
226			error = EBADRPC; \
227		if (error) \
228			nfsm_reply(0); \
229		}
230
231#define nfsm_mtouio(p,s) \
232		if ((s) > 0 && \
233		   (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
234			error = t1; \
235			m_freem(mrep); \
236			goto nfsmout; \
237		}
238
239#define	nfsm_reqhead(v,a,s) \
240		mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
241
242#define nfsm_rndup(a)	(((a)+3)&(~0x3))
243
244#define	nfsm_request(v, t, p, c)	\
245		if ((error = nfs_request((v), mreq, (t), (p), \
246		   (c), &mrep, &md, &dpos)) != 0) { \
247			if (error & NFSERR_RETERR) \
248				error &= ~NFSERR_RETERR; \
249			else \
250				goto nfsmout; \
251		}
252
253#define	nfsm_strtom(a,s,m) \
254		if ((s) > (m)) { \
255			m_freem(mreq); \
256			error = ENAMETOOLONG; \
257			goto nfsmout; \
258		} \
259		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
260		if (t2 <= M_TRAILINGSPACE(mb)) { \
261			tl = nfsm_build(&mb, t2, &bpos); \
262			*tl++ = txdr_unsigned(s); \
263			*(tl+((t2>>2)-2)) = 0; \
264			bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
265		} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
266			error = t2; \
267			m_freem(mreq); \
268			goto nfsmout; \
269		}
270
271#define	nfsm_reply(s) \
272		{ \
273		nfsd->nd_repstat = error; \
274		if (error && !(nfsd->nd_flag & ND_NFSV3)) \
275		   (void) nfs_rephead(0, nfsd, slp, error, \
276			mrq, &mb, &bpos); \
277		else \
278		   (void) nfs_rephead((s), nfsd, slp, error, \
279			mrq, &mb, &bpos); \
280		if (mrep != NULL) { \
281			m_freem(mrep); \
282			mrep = NULL; \
283		} \
284		mreq = *mrq; \
285		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
286			error == EBADRPC)) \
287			return(0); \
288		}
289
290#define	nfsm_writereply(s, v3) \
291		{ \
292		nfsd->nd_repstat = error; \
293		if (error && !(v3)) \
294		   (void) nfs_rephead(0, nfsd, slp, error, \
295			&mreq, &mb, &bpos); \
296		else \
297		   (void) nfs_rephead((s), nfsd, slp, error, \
298			&mreq, &mb, &bpos); \
299		}
300
301#define	nfsm_adv(s) \
302		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
303		if (t1 >= (s)) { \
304			dpos += (s); \
305		} else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
306			error = t1; \
307			m_freem(mrep); \
308			goto nfsmout; \
309		} }
310
311#define nfsm_srvmtofh(f) \
312		{ if (nfsd->nd_flag & ND_NFSV3) { \
313			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
314			if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
315				error = EBADRPC; \
316				nfsm_reply(0); \
317			} \
318		} \
319		nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
320		bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
321		if ((nfsd->nd_flag & ND_NFSV3) == 0) \
322			nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
323		}
324
325#define	nfsm_clget \
326		if (bp >= be) { \
327			if (mp == mb) \
328				mp->m_len += bp-bpos; \
329			MGET(mp, M_WAIT, MT_DATA); \
330			MCLGET(mp, M_WAIT); \
331			mp->m_len = NFSMSIZ(mp); \
332			mp2->m_next = mp; \
333			mp2 = mp; \
334			bp = mtod(mp, caddr_t); \
335			be = bp+mp->m_len; \
336		} \
337		tl = (u_int32_t *)bp
338
339#define nfsm_srvpostop_attr(r, a) \
340		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
341
342#define nfsm_srvsattr(a) \
343		{ nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
344		if (*tl == nfs_true) { \
345			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
346			(a)->va_mode = nfstov_mode(*tl); \
347		} \
348		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
349		if (*tl == nfs_true) { \
350			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
351			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
352		} \
353		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
354		if (*tl == nfs_true) { \
355			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
356			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
357		} \
358		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
359		if (*tl == nfs_true) { \
360			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
361			(a)->va_size = fxdr_hyper(tl); \
362		} \
363		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
364		switch (fxdr_unsigned(int, *tl)) { \
365		case NFSV3SATTRTIME_TOCLIENT: \
366			(a)->va_vaflags &= ~VA_UTIMES_NULL; \
367			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
368			fxdr_nfsv3time(tl, &(a)->va_atime); \
369			break; \
370		case NFSV3SATTRTIME_TOSERVER: \
371			getnanotime(&(a)->va_atime); \
372			break; \
373		}; \
374		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
375		switch (fxdr_unsigned(int, *tl)) { \
376		case NFSV3SATTRTIME_TOCLIENT: \
377			(a)->va_vaflags &= ~VA_UTIMES_NULL; \
378			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
379			fxdr_nfsv3time(tl, &(a)->va_mtime); \
380			break; \
381		case NFSV3SATTRTIME_TOSERVER: \
382			getnanotime(&(a)->va_mtime); \
383			break; \
384		}; }
385
386#endif
387