nfs_common.h revision 10222
194575Sdes/*
294575Sdes * Copyright (c) 1989, 1993
394575Sdes *	The Regents of the University of California.  All rights reserved.
494575Sdes *
594575Sdes * This code is derived from software contributed to Berkeley by
694575Sdes * Rick Macklem at The University of Guelph.
794575Sdes *
894575Sdes * Redistribution and use in source and binary forms, with or without
994575Sdes * modification, are permitted provided that the following conditions
1094575Sdes * are met:
1194575Sdes * 1. Redistributions of source code must retain the above copyright
1294575Sdes *    notice, this list of conditions and the following disclaimer.
1394575Sdes * 2. Redistributions in binary form must reproduce the above copyright
1494575Sdes *    notice, this list of conditions and the following disclaimer in the
1594575Sdes *    documentation and/or other materials provided with the distribution.
1694575Sdes * 3. All advertising materials mentioning features or use of this software
1794575Sdes *    must display the following acknowledgement:
1894575Sdes *	This product includes software developed by the University of
1994575Sdes *	California, Berkeley and its contributors.
2094575Sdes * 4. Neither the name of the University nor the names of its contributors
2194575Sdes *    may be used to endorse or promote products derived from this software
2294575Sdes *    without specific prior written permission.
2394575Sdes *
2494575Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2594575Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2694575Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2794575Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2894575Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2994575Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3094575Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3194575Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3294575Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3394575Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3494575Sdes * SUCH DAMAGE.
3594575Sdes *
3694575Sdes *	@(#)nfsm_subs.h	8.1 (Berkeley) 6/16/93
3794577Sdes * $Id: nfsm_subs.h,v 1.7 1995/06/27 11:06:55 dfr Exp $
3894577Sdes */
3994577Sdes
4094577Sdes#ifndef _NFS_NFSM_SUBS_H_
4194575Sdes#define _NFS_NFSM_SUBS_H_
4294575Sdes
4394577Sdes
4494577Sdes/*
4594575Sdes * These macros do strange and peculiar things to mbuf chains for
4694575Sdes * the assistance of the nfs code. To attempt to use them for any
4794575Sdes * other purpose will be dangerous. (they make weird assumptions)
4894577Sdes */
4994575Sdes
5094575Sdes/*
5194575Sdes * First define what the actual subs. return
5294575Sdes */
5394575Sdesstruct mbuf *nfsm_reqh __P((struct vnode *vp, u_long procid, int hsiz,
5494577Sdes			    caddr_t *bposp));
5594575Sdesstruct mbuf *nfsm_rpchead __P((struct ucred *cr, int nmflag, int procid,
5694577Sdes			       int auth_type, int auth_len, char *auth_str,
5794577Sdes			       int verf_len, char *verf_str,
5894577Sdes			       struct mbuf *mrest, int mrest_len,
5996198Sdes			       struct mbuf **mbp, u_long *xidp));
6096198Sdes
6196198Sdes#define	M_HASCL(m)	((m)->m_flags & M_EXT)
6296198Sdes#define	NFSMINOFF(m) \
6396198Sdes		if (M_HASCL(m)) \
6494575Sdes			(m)->m_data = (m)->m_ext.ext_buf; \
6596198Sdes		else if ((m)->m_flags & M_PKTHDR) \
6694575Sdes			(m)->m_data = (m)->m_pktdat; \
6794577Sdes		else \
6894577Sdes			(m)->m_data = (m)->m_dat
6994575Sdes#define	NFSMADV(m, s)	(m)->m_data += (s)
7094575Sdes#define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES: \
7194575Sdes				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
7294575Sdes
7394575Sdes/*
7494575Sdes * Now for the macros that do the simple stuff and call the functions
7594575Sdes * for the hard stuff.
7694575Sdes * These macros use several vars. declared in nfsm_reqhead and these
7794577Sdes * vars. must not be used elsewhere unless you are careful not to corrupt
7894577Sdes * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
7996198Sdes * that may be used so long as the value is not expected to retained
8094577Sdes * after a macro.
8194577Sdes * I know, this is kind of dorkey, but it makes the actual op functions
8294577Sdes * fairly clean and deals with the mess caused by the xdr discriminating
8394577Sdes * unions.
8494577Sdes */
8594577Sdes
8694577Sdes#define	nfsm_build(a,c,s) \
8796198Sdes		{ if ((s) > M_TRAILINGSPACE(mb)) { \
8894577Sdes			MGET(mb2, M_WAIT, MT_DATA); \
8994577Sdes			if ((s) > MLEN) \
9094577Sdes				panic("build > MLEN"); \
9196198Sdes			mb->m_next = mb2; \
9294577Sdes			mb = mb2; \
9394577Sdes			mb->m_len = 0; \
9494577Sdes			bpos = mtod(mb, caddr_t); \
9594577Sdes		} \
9696198Sdes		(a) = (c)(bpos); \
9794577Sdes		mb->m_len += (s); \
9894577Sdes		bpos += (s); }
9994577Sdes
10096198Sdes#define	nfsm_dissect(a, c, s) \
10194577Sdes		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
10294577Sdes		if (t1 >= (s)) { \
10394577Sdes			(a) = (c)(dpos); \
10496198Sdes			dpos += (s); \
10596198Sdes		} else if (t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) { \
10696198Sdes			error = t1; \
10796198Sdes			m_freem(mrep); \
10896198Sdes			goto nfsmout; \
10996198Sdes		} else { \
11096198Sdes			(a) = (c)cp2; \
11196198Sdes		} }
11296198Sdes
11396198Sdes#define nfsm_fhtom(v, v3) \
11496198Sdes	      { if (v3) { \
11596198Sdes			t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
11696198Sdes			if (t2 <= M_TRAILINGSPACE(mb)) { \
11796198Sdes				nfsm_build(tl, u_long *, t2); \
11896198Sdes				*tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
11996198Sdes				*(tl + ((t2>>2) - 2)) = 0; \
12096198Sdes				bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
12196198Sdes					VTONFS(v)->n_fhsize); \
12296198Sdes			} else if (t2 = nfsm_strtmbuf(&mb, &bpos, \
12394577Sdes				(caddr_t)VTONFS(v)->n_fhp, VTONFS(v)->n_fhsize)) { \
12494577Sdes				error = t2; \
12594577Sdes				m_freem(mreq); \
12694577Sdes				goto nfsmout; \
12794577Sdes			} \
12894577Sdes		} else { \
12994577Sdes			nfsm_build(cp, caddr_t, NFSX_V2FH); \
13094577Sdes			bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
13194577Sdes		} }
13294577Sdes
13394577Sdes#define nfsm_srvfhtom(f, v3) \
13494577Sdes		{ if (v3) { \
13594577Sdes			nfsm_build(tl, u_long *, NFSX_UNSIGNED + NFSX_V3FH); \
13694577Sdes			*tl++ = txdr_unsigned(NFSX_V3FH); \
13794577Sdes			bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
13894577Sdes		} else { \
13994577Sdes			nfsm_build(cp, caddr_t, NFSX_V2FH); \
14094577Sdes			bcopy((caddr_t)(f), cp, NFSX_V2FH); \
14194577Sdes		} }
14294577Sdes
14394577Sdes#define nfsm_srvpostop_fh(f) \
14494577Sdes		{ nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
14594577Sdes		*tl++ = nfs_true; \
14694577Sdes		*tl++ = txdr_unsigned(NFSX_V3FH); \
14794577Sdes		bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
14894577Sdes		}
14994577Sdes
15096198Sdes#define nfsm_mtofh(d, v, v3, f) \
15194577Sdes		{ struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
15294577Sdes		if (v3) { \
15394577Sdes			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
15494577Sdes			(f) = fxdr_unsigned(int, *tl); \
15594577Sdes		} else \
15694577Sdes			(f) = 1; \
15794577Sdes		if (f) { \
15894577Sdes			nfsm_getfh(ttfhp, ttfhsize, (v3)); \
15994577Sdes			if (t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
16094577Sdes				&ttnp)) { \
16194577Sdes				error = t1; \
16294577Sdes				m_freem(mrep); \
16394577Sdes				goto nfsmout; \
16494577Sdes			} \
16594577Sdes			(v) = NFSTOV(ttnp); \
16694577Sdes		} \
16794577Sdes		if (v3) { \
16894577Sdes			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
16994577Sdes			if (f) \
17094577Sdes				(f) = fxdr_unsigned(int, *tl); \
17194577Sdes			else if (fxdr_unsigned(int, *tl)) \
17294577Sdes				nfsm_adv(NFSX_V3FATTR); \
17394577Sdes		} \
17494577Sdes		if (f) \
17594577Sdes			nfsm_loadattr((v), (struct vattr *)0); \
17694577Sdes		}
17794577Sdes
17894577Sdes#define nfsm_getfh(f, s, v3) \
17994577Sdes		{ if (v3) { \
18094577Sdes			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
18194577Sdes			if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
18294577Sdes				(s) > NFSX_V3FHMAX) { \
18394577Sdes				m_freem(mrep); \
18494577Sdes				error = EBADRPC; \
18594577Sdes				goto nfsmout; \
18694577Sdes			} \
18794577Sdes		} else \
18894577Sdes			(s) = NFSX_V2FH; \
18994577Sdes		nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
19094577Sdes
19194577Sdes#define	nfsm_loadattr(v, a) \
19294577Sdes		{ struct vnode *ttvp = (v); \
19394577Sdes		if (t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a))) { \
19494577Sdes			error = t1; \
19594577Sdes			m_freem(mrep); \
19694577Sdes			goto nfsmout; \
19794577Sdes		} \
19894577Sdes		(v) = ttvp; }
19994577Sdes
20094577Sdes#define	nfsm_postop_attr(v, f) \
20195588Sdes		{ struct vnode *ttvp = (v); \
20295588Sdes		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
20395588Sdes		if ((f) = fxdr_unsigned(int, *tl)) { \
20495588Sdes			if (t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
20594577Sdes				(struct vattr *)0)) { \
20694577Sdes				error = t1; \
20794577Sdes				(f) = 0; \
20894577Sdes				m_freem(mrep); \
20994577Sdes				goto nfsmout; \
21094577Sdes			} \
21194577Sdes			(v) = ttvp; \
21294577Sdes		} }
21394577Sdes
21494577Sdes/* Used as (f) for nfsm_wcc_data() */
21594577Sdes#define NFSV3_WCCRATTR	0
21694577Sdes#define NFSV3_WCCCHK	1
21794577Sdes
21894577Sdes#define	nfsm_wcc_data(v, f) \
21994577Sdes		{ int ttattrf, ttretf = 0; \
22094577Sdes		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
22194577Sdes		if (*tl == nfs_true) { \
22294577Sdes			nfsm_dissect(tl, u_long *, 6 * NFSX_UNSIGNED); \
22394577Sdes			if (f) \
22494575Sdes				ttretf = (VTONFS(v)->n_mtime == \
22594575Sdes					fxdr_unsigned(u_long, *(tl + 2))); \
22694575Sdes		} \
22794575Sdes		nfsm_postop_attr((v), ttattrf); \
22894575Sdes		if (f) { \
22994575Sdes			(f) = ttretf; \
23094575Sdes		} else { \
23194575Sdes			(f) = ttattrf; \
23294575Sdes		} }
23396198Sdes
23494575Sdes#define nfsm_v3sattr(s, a, u, g) \
23594575Sdes		{ (s)->sa_modetrue = nfs_true; \
23694575Sdes		(s)->sa_mode = vtonfsv3_mode((a)->va_mode); \
23794575Sdes		(s)->sa_uidtrue = nfs_true; \
23894575Sdes		(s)->sa_uid = txdr_unsigned(u); \
23994575Sdes		(s)->sa_gidtrue = nfs_true; \
24094575Sdes		(s)->sa_gid = txdr_unsigned(g); \
24194575Sdes		(s)->sa_sizefalse = nfs_false; \
24294575Sdes		(s)->sa_atimetype = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
24394575Sdes		txdr_nfsv3time(&(a)->va_atime, &(s)->sa_atime); \
24494575Sdes		(s)->sa_mtimetype = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
24594575Sdes		txdr_nfsv3time(&(a)->va_mtime, &(s)->sa_mtime); \
24694575Sdes		}
24794575Sdes
24894575Sdes#define	nfsm_strsiz(s,m) \
24994575Sdes		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
25094575Sdes		if (((s) = fxdr_unsigned(long,*tl)) > (m)) { \
25194575Sdes			m_freem(mrep); \
25294575Sdes			error = EBADRPC; \
25394575Sdes			goto nfsmout; \
25494575Sdes		} }
25594575Sdes
25694575Sdes#define	nfsm_srvstrsiz(s,m) \
25794575Sdes		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
25894575Sdes		if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \
25994575Sdes			error = EBADRPC; \
26094575Sdes			nfsm_reply(0); \
26194575Sdes		} }
26294575Sdes
26394575Sdes#define	nfsm_srvnamesiz(s) \
26494575Sdes		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
26594575Sdes		if (((s) = fxdr_unsigned(long,*tl)) > NFS_MAXNAMLEN) \
26694575Sdes			error = NFSERR_NAMETOL; \
26794575Sdes		if ((s) <= 0) \
26894575Sdes			error = EBADRPC; \
26994575Sdes		if (error) \
27094575Sdes			nfsm_reply(0); \
27194575Sdes		}
27294575Sdes
27394575Sdes#define nfsm_mtouio(p,s) \
27494575Sdes		if ((s) > 0 && \
27594575Sdes		   (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
27694575Sdes			error = t1; \
27794575Sdes			m_freem(mrep); \
27894575Sdes			goto nfsmout; \
27994575Sdes		}
28094575Sdes
28195588Sdes#define nfsm_uiotom(p,s) \
28295588Sdes		if (t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) { \
28395588Sdes			error = t1; \
28495588Sdes			m_freem(mreq); \
28594575Sdes			goto nfsmout; \
28694575Sdes		}
28794575Sdes
28894575Sdes#define	nfsm_reqhead(v,a,s) \
28994575Sdes		mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
29094575Sdes
29194575Sdes#define nfsm_reqdone	m_freem(mrep); \
29294575Sdes		nfsmout:
29394575Sdes
294#define nfsm_rndup(a)	(((a)+3)&(~0x3))
295
296#define	nfsm_request(v, t, p, c)	\
297		if (error = nfs_request((v), mreq, (t), (p), \
298		   (c), &mrep, &md, &dpos)) { \
299			if (error & NFSERR_RETERR) \
300				error &= ~NFSERR_RETERR; \
301			else \
302				goto nfsmout; \
303		}
304
305#define	nfsm_strtom(a,s,m) \
306		if ((s) > (m)) { \
307			m_freem(mreq); \
308			error = ENAMETOOLONG; \
309			goto nfsmout; \
310		} \
311		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
312		if (t2 <= M_TRAILINGSPACE(mb)) { \
313			nfsm_build(tl,u_long *,t2); \
314			*tl++ = txdr_unsigned(s); \
315			*(tl+((t2>>2)-2)) = 0; \
316			bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
317		} else if (t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) { \
318			error = t2; \
319			m_freem(mreq); \
320			goto nfsmout; \
321		}
322
323#define	nfsm_srvdone \
324		nfsmout: \
325		return(error)
326
327#define	nfsm_reply(s) \
328		{ \
329		nfsd->nd_repstat = error; \
330		if (error && !(nfsd->nd_flag & ND_NFSV3)) \
331		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
332			mrq, &mb, &bpos); \
333		else \
334		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
335			mrq, &mb, &bpos); \
336		m_freem(mrep); \
337		mreq = *mrq; \
338		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
339			error == EBADRPC)) \
340			return(0); \
341		}
342
343#define	nfsm_writereply(s, v3) \
344		{ \
345		nfsd->nd_repstat = error; \
346		if (error && !(v3)) \
347		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
348			&mreq, &mb, &bpos); \
349		else \
350		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
351			&mreq, &mb, &bpos); \
352		}
353
354#define	nfsm_adv(s) \
355		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
356		if (t1 >= (s)) { \
357			dpos += (s); \
358		} else if (t1 = nfs_adv(&md, &dpos, (s), t1)) { \
359			error = t1; \
360			m_freem(mrep); \
361			goto nfsmout; \
362		} }
363
364#define nfsm_srvmtofh(f) \
365		{ if (nfsd->nd_flag & ND_NFSV3) { \
366			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
367			if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
368				error = EBADRPC; \
369				nfsm_reply(0); \
370			} \
371		} \
372		nfsm_dissect(tl, u_long *, NFSX_V3FH); \
373		bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
374		if ((nfsd->nd_flag & ND_NFSV3) == 0) \
375			nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
376		}
377
378#define	nfsm_clget \
379		if (bp >= be) { \
380			if (mp == mb) \
381				mp->m_len += bp-bpos; \
382			MGET(mp, M_WAIT, MT_DATA); \
383			MCLGET(mp, M_WAIT); \
384			mp->m_len = NFSMSIZ(mp); \
385			mp2->m_next = mp; \
386			mp2 = mp; \
387			bp = mtod(mp, caddr_t); \
388			be = bp+mp->m_len; \
389		} \
390		tl = (u_long *)bp
391
392#define	nfsm_srvfillattr(a, f) \
393		nfsm_srvfattr(nfsd, (a), (f))
394
395#define nfsm_srvwcc_data(br, b, ar, a) \
396		nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
397
398#define nfsm_srvpostop_attr(r, a) \
399		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
400
401#define nfsm_srvsattr(a) \
402		{ nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
403		if (*tl == nfs_true) { \
404			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
405			(a)->va_mode = nfstov_mode(*tl); \
406		} \
407		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
408		if (*tl == nfs_true) { \
409			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
410			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
411		} \
412		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
413		if (*tl == nfs_true) { \
414			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
415			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
416		} \
417		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
418		if (*tl == nfs_true) { \
419			nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
420			fxdr_hyper(tl, &(a)->va_size); \
421		} \
422		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
423		switch (fxdr_unsigned(int, *tl)) { \
424		case NFSV3SATTRTIME_TOCLIENT: \
425			nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
426			fxdr_nfsv3time(tl, &(a)->va_atime); \
427			break; \
428		case NFSV3SATTRTIME_TOSERVER: \
429			(a)->va_atime.ts_sec = time.tv_sec; \
430			(a)->va_atime.ts_nsec = time.tv_usec * 1000; \
431			break; \
432		}; \
433		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
434		switch (fxdr_unsigned(int, *tl)) { \
435		case NFSV3SATTRTIME_TOCLIENT: \
436			nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
437			fxdr_nfsv3time(tl, &(a)->va_mtime); \
438			break; \
439		case NFSV3SATTRTIME_TOSERVER: \
440			(a)->va_mtime.ts_sec = time.tv_sec; \
441			(a)->va_mtime.ts_nsec = time.tv_usec * 1000; \
442			break; \
443		}; }
444
445#endif
446