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