nfsm_subs.h revision 1.4
1/*	$OpenBSD: nfsm_subs.h,v 1.4 1996/03/31 13:16:09 mickey 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. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the University of
22 *	California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *	@(#)nfsm_subs.h	8.2 (Berkeley) 3/30/95
40 */
41
42
43#ifndef _NFS_NFSM_SUBS_H_
44#define _NFS_NFSM_SUBS_H_
45
46
47/*
48 * These macros do strange and peculiar things to mbuf chains for
49 * the assistance of the nfs code. To attempt to use them for any
50 * other purpose will be dangerous. (they make weird assumptions)
51 */
52
53/*
54 * First define what the actual subs. return
55 */
56
57#define	M_HASCL(m)	((m)->m_flags & M_EXT)
58#define	NFSMINOFF(m) \
59		if (M_HASCL(m)) \
60			(m)->m_data = (m)->m_ext.ext_buf; \
61		else if ((m)->m_flags & M_PKTHDR) \
62			(m)->m_data = (m)->m_pktdat; \
63		else \
64			(m)->m_data = (m)->m_dat
65#define	NFSMADV(m, s)	(m)->m_data += (s)
66#define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES: \
67				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
68
69/*
70 * Now for the macros that do the simple stuff and call the functions
71 * for the hard stuff.
72 * These macros use several vars. declared in nfsm_reqhead and these
73 * vars. must not be used elsewhere unless you are careful not to corrupt
74 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
75 * that may be used so long as the value is not expected to retained
76 * after a macro.
77 * I know, this is kind of dorkey, but it makes the actual op functions
78 * fairly clean and deals with the mess caused by the xdr discriminating
79 * unions.
80 */
81
82#define	nfsm_build(a,c,s) \
83		{ if ((s) > M_TRAILINGSPACE(mb)) { \
84			MGET(mb2, M_WAIT, MT_DATA); \
85			if ((s) > MLEN) \
86				panic("build > MLEN"); \
87			mb->m_next = mb2; \
88			mb = mb2; \
89			mb->m_len = 0; \
90			bpos = mtod(mb, caddr_t); \
91		} \
92		(a) = (c)(bpos); \
93		mb->m_len += (s); \
94		bpos += (s); }
95
96#define	nfsm_dissect(a, c, s) \
97		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
98		if (t1 >= (s)) { \
99			(a) = (c)(dpos); \
100			dpos += (s); \
101		} else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
102			error = t1; \
103			m_freem(mrep); \
104			goto nfsmout; \
105		} else { \
106			(a) = (c)cp2; \
107		} }
108
109#define nfsm_fhtom(v, v3) \
110	      { if (v3) { \
111			t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
112			if (t2 <= M_TRAILINGSPACE(mb)) { \
113				nfsm_build(tl, u_int32_t *, t2); \
114				*tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
115				*(tl + ((t2>>2) - 2)) = 0; \
116				bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
117					VTONFS(v)->n_fhsize); \
118			} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
119				(caddr_t)VTONFS(v)->n_fhp, \
120				  VTONFS(v)->n_fhsize)) != 0) { \
121				error = t2; \
122				m_freem(mreq); \
123				goto nfsmout; \
124			} \
125		} else { \
126			nfsm_build(cp, caddr_t, NFSX_V2FH); \
127			bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
128		} }
129
130#define nfsm_srvfhtom(f, v3) \
131		{ if (v3) { \
132			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FH); \
133			*tl++ = txdr_unsigned(NFSX_V3FH); \
134			bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
135		} else { \
136			nfsm_build(cp, caddr_t, NFSX_V2FH); \
137			bcopy((caddr_t)(f), cp, NFSX_V2FH); \
138		} }
139
140#define nfsm_srvpostop_fh(f) \
141		{ nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
142		*tl++ = nfs_true; \
143		*tl++ = txdr_unsigned(NFSX_V3FH); \
144		bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
145		}
146
147#define nfsm_mtofh(d, v, v3, f) \
148		{ struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
149		if (v3) { \
150			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
151			(f) = fxdr_unsigned(int, *tl); \
152		} else \
153			(f) = 1; \
154		if (f) { \
155			nfsm_getfh(ttfhp, ttfhsize, (v3)); \
156			if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
157				&ttnp)) != 0) { \
158				error = t1; \
159				m_freem(mrep); \
160				goto nfsmout; \
161			} \
162			(v) = NFSTOV(ttnp); \
163		} \
164		if (v3) { \
165			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
166			if (f) \
167				(f) = fxdr_unsigned(int, *tl); \
168			else if (fxdr_unsigned(int, *tl)) \
169				nfsm_adv(NFSX_V3FATTR); \
170		} \
171		if (f) \
172			nfsm_loadattr((v), (struct vattr *)0); \
173		}
174
175#define nfsm_getfh(f, s, v3) \
176		{ if (v3) { \
177			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
178			if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
179				(s) > NFSX_V3FHMAX) { \
180				m_freem(mrep); \
181				error = EBADRPC; \
182				goto nfsmout; \
183			} \
184		} else \
185			(s) = NFSX_V2FH; \
186		nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
187
188#define	nfsm_loadattr(v, a) \
189		{ struct vnode *ttvp = (v); \
190		if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a))) != 0) { \
191			error = t1; \
192			m_freem(mrep); \
193			goto nfsmout; \
194		} \
195		(v) = ttvp; }
196
197#define	nfsm_postop_attr(v, f) \
198		{ struct vnode *ttvp = (v); \
199		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
200		if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
201			if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
202				(struct vattr *)0)) != 0) { \
203				error = t1; \
204				(f) = 0; \
205				m_freem(mrep); \
206				goto nfsmout; \
207			} \
208			(v) = ttvp; \
209		} }
210
211/* Used as (f) for nfsm_wcc_data() */
212#define NFSV3_WCCRATTR	0
213#define NFSV3_WCCCHK	1
214
215#define	nfsm_wcc_data(v, f) \
216		{ int ttattrf, ttretf = 0; \
217		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
218		if (*tl == nfs_true) { \
219			nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
220			if (f) \
221				ttretf = (VTONFS(v)->n_mtime == \
222					fxdr_unsigned(u_int32_t, *(tl + 2))); \
223		} \
224		nfsm_postop_attr((v), ttattrf); \
225		if (f) { \
226			(f) = ttretf; \
227		} else { \
228			(f) = ttattrf; \
229		} }
230
231#define nfsm_v3sattr(s, a) \
232		{ (s)->sa_modetrue = nfs_true; \
233		(s)->sa_mode = vtonfsv3_mode((a)->va_mode); \
234		(s)->sa_uidfalse = nfs_false; \
235		(s)->sa_gidfalse = nfs_false; \
236		(s)->sa_sizefalse = nfs_false; \
237		(s)->sa_atimetype = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
238		txdr_nfsv3time(&(a)->va_atime, &(s)->sa_atime); \
239		(s)->sa_mtimetype = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
240		txdr_nfsv3time(&(a)->va_mtime, &(s)->sa_mtime); \
241		}
242
243#define	nfsm_strsiz(s,m) \
244		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
245		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
246			m_freem(mrep); \
247			error = EBADRPC; \
248			goto nfsmout; \
249		} }
250
251#define	nfsm_srvstrsiz(s,m) \
252		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
253		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
254			error = EBADRPC; \
255			nfsm_reply(0); \
256		} }
257
258#define	nfsm_srvnamesiz(s) \
259		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
260		if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
261			error = NFSERR_NAMETOL; \
262		if ((s) <= 0) \
263			error = EBADRPC; \
264		if (error) \
265			nfsm_reply(0); \
266		}
267
268#define nfsm_mtouio(p,s) \
269		if ((s) > 0 && \
270		   (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
271			error = t1; \
272			m_freem(mrep); \
273			goto nfsmout; \
274		}
275
276#define nfsm_uiotom(p,s) \
277		if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
278			error = t1; \
279			m_freem(mreq); \
280			goto nfsmout; \
281		}
282
283#define	nfsm_reqhead(v,a,s) \
284		mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
285
286#define nfsm_reqdone	m_freem(mrep); \
287		nfsmout:
288
289#define nfsm_rndup(a)	(((a)+3)&(~0x3))
290
291#define	nfsm_request(v, t, p, c)	\
292		if ((error = nfs_request((v), mreq, (t), (p), \
293		   (c), &mrep, &md, &dpos)) != 0) { \
294			if (error & NFSERR_RETERR) \
295				error &= ~NFSERR_RETERR; \
296			else \
297				goto nfsmout; \
298		}
299
300#define	nfsm_strtom(a,s,m) \
301		if ((s) > (m)) { \
302			m_freem(mreq); \
303			error = ENAMETOOLONG; \
304			goto nfsmout; \
305		} \
306		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
307		if (t2 <= M_TRAILINGSPACE(mb)) { \
308			nfsm_build(tl,u_int32_t *,t2); \
309			*tl++ = txdr_unsigned(s); \
310			*(tl+((t2>>2)-2)) = 0; \
311			bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
312		} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
313			error = t2; \
314			m_freem(mreq); \
315			goto nfsmout; \
316		}
317
318#define	nfsm_srvdone \
319		nfsmout: \
320		return(error)
321
322#define	nfsm_reply(s) \
323		{ \
324		nfsd->nd_repstat = error; \
325		if (error && !(nfsd->nd_flag & ND_NFSV3)) \
326		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
327			mrq, &mb, &bpos); \
328		else \
329		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
330			mrq, &mb, &bpos); \
331		if (mrep != NULL) { \
332			m_freem(mrep); \
333			mrep = NULL; \
334		} \
335		mreq = *mrq; \
336		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
337			error == EBADRPC)) \
338			return(0); \
339		}
340
341#define	nfsm_writereply(s, v3) \
342		{ \
343		nfsd->nd_repstat = error; \
344		if (error && !(v3)) \
345		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
346			&mreq, &mb, &bpos); \
347		else \
348		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
349			&mreq, &mb, &bpos); \
350		}
351
352#define	nfsm_adv(s) \
353		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
354		if (t1 >= (s)) { \
355			dpos += (s); \
356		} else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
357			error = t1; \
358			m_freem(mrep); \
359			goto nfsmout; \
360		} }
361
362#define nfsm_srvmtofh(f) \
363		{ if (nfsd->nd_flag & ND_NFSV3) { \
364			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
365			if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
366				error = EBADRPC; \
367				nfsm_reply(0); \
368			} \
369		} \
370		nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
371		bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
372		if ((nfsd->nd_flag & ND_NFSV3) == 0) \
373			nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
374		}
375
376#define	nfsm_clget \
377		if (bp >= be) { \
378			if (mp == mb) \
379				mp->m_len += bp-bpos; \
380			MGET(mp, M_WAIT, MT_DATA); \
381			MCLGET(mp, M_WAIT); \
382			mp->m_len = NFSMSIZ(mp); \
383			mp2->m_next = mp; \
384			mp2 = mp; \
385			bp = mtod(mp, caddr_t); \
386			be = bp+mp->m_len; \
387		} \
388		tl = (u_int32_t *)bp
389
390#define	nfsm_srvfillattr(a, f) \
391		nfsm_srvfattr(nfsd, (a), (f))
392
393#define nfsm_srvwcc_data(br, b, ar, a) \
394		nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
395
396#define nfsm_srvpostop_attr(r, a) \
397		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
398
399#define nfsm_srvsattr(a) \
400		{ nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
401		if (*tl == nfs_true) { \
402			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
403			(a)->va_mode = nfstov_mode(*tl); \
404		} \
405		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
406		if (*tl == nfs_true) { \
407			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
408			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
409		} \
410		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
411		if (*tl == nfs_true) { \
412			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
413			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
414		} \
415		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
416		if (*tl == nfs_true) { \
417			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
418			fxdr_hyper(tl, &(a)->va_size); \
419		} \
420		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
421		switch (fxdr_unsigned(int, *tl)) { \
422		case NFSV3SATTRTIME_TOCLIENT: \
423			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
424			fxdr_nfsv3time(tl, &(a)->va_atime); \
425			break; \
426		case NFSV3SATTRTIME_TOSERVER: \
427			(a)->va_atime.tv_sec = time.tv_sec; \
428			(a)->va_atime.tv_nsec = time.tv_usec * 1000; \
429			break; \
430		}; \
431		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
432		switch (fxdr_unsigned(int, *tl)) { \
433		case NFSV3SATTRTIME_TOCLIENT: \
434			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
435			fxdr_nfsv3time(tl, &(a)->va_mtime); \
436			break; \
437		case NFSV3SATTRTIME_TOSERVER: \
438			(a)->va_mtime.tv_sec = time.tv_sec; \
439			(a)->va_mtime.tv_nsec = time.tv_usec * 1000; \
440			break; \
441		}; }
442
443#endif
444