xref: /csrg-svn/sys/nfs/nfs_subs.c (revision 44513)
138420Smckusick /*
238420Smckusick  * Copyright (c) 1989 The Regents of the University of California.
338420Smckusick  * All rights reserved.
438420Smckusick  *
538420Smckusick  * This code is derived from software contributed to Berkeley by
638420Smckusick  * Rick Macklem at The University of Guelph.
738420Smckusick  *
8*44513Sbostic  * %sccs.include.redist.c%
938420Smckusick  *
10*44513Sbostic  *	@(#)nfs_subs.c	7.28 (Berkeley) 06/28/90
1138420Smckusick  */
1238420Smckusick 
1338420Smckusick /*
1438420Smckusick  * These functions support the macros and help fiddle mbuf chains for
1538420Smckusick  * the nfs op functions. They do things like create the rpc header and
1638420Smckusick  * copy data between mbuf chains and uio lists.
1738420Smckusick  */
1838420Smckusick #include "param.h"
1939345Smckusick #include "user.h"
2039345Smckusick #include "proc.h"
2140136Smckusick #include "systm.h"
2240136Smckusick #include "kernel.h"
2338420Smckusick #include "mount.h"
2438420Smckusick #include "file.h"
2538420Smckusick #include "vnode.h"
2640136Smckusick #include "mbuf.h"
2740136Smckusick #include "errno.h"
2838884Smacklem #include "map.h"
2938420Smckusick #include "rpcv2.h"
3038420Smckusick #include "nfsv2.h"
3138420Smckusick #include "nfsnode.h"
3238420Smckusick #include "nfs.h"
3338884Smacklem #include "nfsiom.h"
3438420Smckusick #include "xdr_subs.h"
3538420Smckusick #include "nfsm_subs.h"
3638420Smckusick 
3738420Smckusick #define TRUE	1
3838420Smckusick #define	FALSE	0
3938420Smckusick 
4038420Smckusick /*
4138420Smckusick  * Data items converted to xdr at startup, since they are constant
4238420Smckusick  * This is kinda hokey, but may save a little time doing byte swaps
4338420Smckusick  */
4438420Smckusick u_long nfs_procids[NFS_NPROCS];
4538420Smckusick u_long nfs_xdrneg1;
4638420Smckusick u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied,
4738420Smckusick 	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
4838420Smckusick u_long nfs_vers, nfs_prog, nfs_true, nfs_false;
4938420Smckusick /* And other global data */
5038420Smckusick static u_long *rpc_uidp = (u_long *)0;
5138420Smckusick static u_long nfs_xid = 1;
5238420Smckusick static char *rpc_unixauth;
5338420Smckusick extern long hostid;
5442244Smckusick enum vtype ntov_type[7] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON };
5541902Smckusick extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
5638884Smacklem extern struct map nfsmap[NFS_MSIZ];
5741902Smckusick extern struct nfsreq nfsreqh;
5838420Smckusick 
5938420Smckusick /* Function ret types */
6038420Smckusick static char *nfs_unixauth();
6138420Smckusick 
6238420Smckusick /*
6338737Smckusick  * Maximum number of groups passed through to NFS server.
6441902Smckusick  * According to RFC1057 it should be 16.
6538737Smckusick  * For release 3.X systems, the maximum value is 8.
6638737Smckusick  * For release 4.X systems, the maximum value is 10.
6738737Smckusick  */
6838737Smckusick int numgrps = 8;
6938737Smckusick 
7038737Smckusick /*
7138420Smckusick  * Create the header for an rpc request packet
7238420Smckusick  * The function nfs_unixauth() creates a unix style authorization string
7338420Smckusick  * and returns a ptr to it.
7438420Smckusick  * The hsiz is the size of the rest of the nfs request header.
7538420Smckusick  * (just used to decide if a cluster is a good idea)
7639494Smckusick  * nb: Note that the prog, vers and procid args are already in xdr byte order
7738420Smckusick  */
7839494Smckusick struct mbuf *nfsm_reqh(prog, vers, procid, cred, hsiz, bpos, mb, retxid)
7938420Smckusick 	u_long prog;
8038420Smckusick 	u_long vers;
8139494Smckusick 	u_long procid;
8238420Smckusick 	struct ucred *cred;
8338420Smckusick 	int hsiz;
8438420Smckusick 	caddr_t *bpos;
8538420Smckusick 	struct mbuf **mb;
8638420Smckusick 	u_long *retxid;
8738420Smckusick {
8838420Smckusick 	register struct mbuf *mreq, *m;
8938420Smckusick 	register u_long *p;
9038420Smckusick 	struct mbuf *m1;
9138420Smckusick 	char *ap;
9238420Smckusick 	int asiz, siz;
9338420Smckusick 
9438420Smckusick 	NFSMGETHDR(mreq);
9538737Smckusick 	asiz = (((cred->cr_ngroups > numgrps) ? numgrps : cred->cr_ngroups)<<2);
9638425Smckusick #ifdef FILLINHOST
9738420Smckusick 	asiz += nfsm_rndup(hostnamelen)+(9*NFSX_UNSIGNED);
9838425Smckusick #else
9938425Smckusick 	asiz += 9*NFSX_UNSIGNED;
10038425Smckusick #endif
10138420Smckusick 
10238420Smckusick 	/* If we need a lot, alloc a cluster ?? */
10338420Smckusick 	if ((asiz+hsiz+RPC_SIZ) > MHLEN)
10441902Smckusick 		MCLGET(mreq, M_WAIT);
10538420Smckusick 	mreq->m_len = NFSMSIZ(mreq);
10638420Smckusick 	siz = mreq->m_len;
10738420Smckusick 	m1 = mreq;
10838420Smckusick 	/*
10938420Smckusick 	 * Alloc enough mbufs
11038420Smckusick 	 * We do it now to avoid all sleeps after the call to nfs_unixauth()
11138420Smckusick 	 */
11238420Smckusick 	while ((asiz+RPC_SIZ) > siz) {
11338420Smckusick 		MGET(m, M_WAIT, MT_DATA);
11438420Smckusick 		m1->m_next = m;
11538420Smckusick 		m->m_len = MLEN;
11638420Smckusick 		siz += MLEN;
11738420Smckusick 		m1 = m;
11838420Smckusick 	}
11938420Smckusick 	p = mtod(mreq, u_long *);
12038420Smckusick 	*p++ = *retxid = txdr_unsigned(++nfs_xid);
12138420Smckusick 	*p++ = rpc_call;
12238420Smckusick 	*p++ = rpc_vers;
12338420Smckusick 	*p++ = prog;
12438420Smckusick 	*p++ = vers;
12539494Smckusick 	*p++ = procid;
12638420Smckusick 
12738420Smckusick 	/* Now we can call nfs_unixauth() and copy it in */
12838420Smckusick 	ap = nfs_unixauth(cred);
12938420Smckusick 	m = mreq;
13038420Smckusick 	siz = m->m_len-RPC_SIZ;
13138420Smckusick 	if (asiz <= siz) {
13238420Smckusick 		bcopy(ap, (caddr_t)p, asiz);
13338420Smckusick 		m->m_len = asiz+RPC_SIZ;
13438420Smckusick 	} else {
13538420Smckusick 		bcopy(ap, (caddr_t)p, siz);
13638420Smckusick 		ap += siz;
13738420Smckusick 		asiz -= siz;
13838420Smckusick 		while (asiz > 0) {
13938420Smckusick 			siz = (asiz > MLEN) ? MLEN : asiz;
14038420Smckusick 			m = m->m_next;
14138420Smckusick 			bcopy(ap, mtod(m, caddr_t), siz);
14238420Smckusick 			m->m_len = siz;
14338420Smckusick 			asiz -= siz;
14438420Smckusick 			ap += siz;
14538420Smckusick 		}
14638420Smckusick 	}
14738420Smckusick 
14838420Smckusick 	/* Finally, return values */
14938420Smckusick 	*mb = m;
15038420Smckusick 	*bpos = mtod(m, caddr_t)+m->m_len;
15138420Smckusick 	return (mreq);
15238420Smckusick }
15338420Smckusick 
15438420Smckusick /*
15538420Smckusick  * copies mbuf chain to the uio scatter/gather list
15638420Smckusick  */
15738420Smckusick nfsm_mbuftouio(mrep, uiop, siz, dpos)
15838420Smckusick 	struct mbuf **mrep;
15943353Smckusick 	register struct uio *uiop;
16038420Smckusick 	int siz;
16138420Smckusick 	caddr_t *dpos;
16238420Smckusick {
16343353Smckusick 	register char *mbufcp, *uiocp;
16438420Smckusick 	register int xfer, left, len;
16538420Smckusick 	register struct mbuf *mp;
16638420Smckusick 	long uiosiz, rem;
16741902Smckusick 	int error = 0;
16838420Smckusick 
16938420Smckusick 	mp = *mrep;
17038420Smckusick 	mbufcp = *dpos;
17138420Smckusick 	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
17238420Smckusick 	rem = nfsm_rndup(siz)-siz;
17338420Smckusick 	while (siz > 0) {
17438420Smckusick 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
17541902Smckusick 			return (EFBIG);
17638420Smckusick 		left = uiop->uio_iov->iov_len;
17738420Smckusick 		uiocp = uiop->uio_iov->iov_base;
17838420Smckusick 		if (left > siz)
17938420Smckusick 			left = siz;
18038420Smckusick 		uiosiz = left;
18138420Smckusick 		while (left > 0) {
18238420Smckusick 			while (len == 0) {
18338420Smckusick 				mp = mp->m_next;
18438420Smckusick 				if (mp == NULL)
18538420Smckusick 					return (EBADRPC);
18638420Smckusick 				mbufcp = mtod(mp, caddr_t);
18738420Smckusick 				len = mp->m_len;
18838420Smckusick 			}
18938420Smckusick 			xfer = (left > len) ? len : left;
19038420Smckusick #ifdef notdef
19138420Smckusick 			/* Not Yet.. */
19238420Smckusick 			if (uiop->uio_iov->iov_op != NULL)
19338420Smckusick 				(*(uiop->uio_iov->iov_op))
19438420Smckusick 				(mbufcp, uiocp, xfer);
19538420Smckusick 			else
19638420Smckusick #endif
19738420Smckusick 			if (uiop->uio_segflg == UIO_SYSSPACE)
19838420Smckusick 				bcopy(mbufcp, uiocp, xfer);
19938420Smckusick 			else
20038420Smckusick 				copyout(mbufcp, uiocp, xfer);
20138420Smckusick 			left -= xfer;
20238420Smckusick 			len -= xfer;
20338420Smckusick 			mbufcp += xfer;
20438420Smckusick 			uiocp += xfer;
20539585Smckusick 			uiop->uio_offset += xfer;
20638420Smckusick 			uiop->uio_resid -= xfer;
20738420Smckusick 		}
20838420Smckusick 		if (uiop->uio_iov->iov_len <= siz) {
20938420Smckusick 			uiop->uio_iovcnt--;
21038420Smckusick 			uiop->uio_iov++;
21138420Smckusick 		} else {
21238420Smckusick 			uiop->uio_iov->iov_base += uiosiz;
21338420Smckusick 			uiop->uio_iov->iov_len -= uiosiz;
21438420Smckusick 		}
21538420Smckusick 		siz -= uiosiz;
21638420Smckusick 	}
21738420Smckusick 	*dpos = mbufcp;
21838420Smckusick 	*mrep = mp;
21941902Smckusick 	if (rem > 0) {
22041902Smckusick 		if (len < rem)
22141902Smckusick 			error = nfs_adv(mrep, dpos, rem, len);
22241902Smckusick 		else
22341902Smckusick 			*dpos += rem;
22441902Smckusick 	}
22541902Smckusick 	return (error);
22638420Smckusick }
22738420Smckusick 
22838420Smckusick /*
22938420Smckusick  * copies a uio scatter/gather list to an mbuf chain...
23038420Smckusick  */
23138420Smckusick nfsm_uiotombuf(uiop, mq, siz, bpos)
23238420Smckusick 	register struct uio *uiop;
23338420Smckusick 	struct mbuf **mq;
23438420Smckusick 	int siz;
23538420Smckusick 	caddr_t *bpos;
23638420Smckusick {
23743353Smckusick 	register char *uiocp;
23843353Smckusick 	register struct mbuf *mp, *mp2;
23943353Smckusick 	register int xfer, left, len;
24043353Smckusick 	int uiosiz, clflg, rem;
24143353Smckusick 	char *cp;
24238420Smckusick 
24338420Smckusick 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
24438420Smckusick 		clflg = 1;
24538420Smckusick 	else
24638420Smckusick 		clflg = 0;
24738420Smckusick 	rem = nfsm_rndup(siz)-siz;
24838420Smckusick 	mp2 = *mq;
24938420Smckusick 	while (siz > 0) {
25038420Smckusick 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
25141902Smckusick 			return (EINVAL);
25238420Smckusick 		left = uiop->uio_iov->iov_len;
25338420Smckusick 		uiocp = uiop->uio_iov->iov_base;
25438420Smckusick 		if (left > siz)
25538420Smckusick 			left = siz;
25638420Smckusick 		uiosiz = left;
25738420Smckusick 		while (left > 0) {
25838420Smckusick 			MGET(mp, M_WAIT, MT_DATA);
25938420Smckusick 			if (clflg)
26041902Smckusick 				MCLGET(mp, M_WAIT);
26138420Smckusick 			mp->m_len = NFSMSIZ(mp);
26238420Smckusick 			mp2->m_next = mp;
26338420Smckusick 			mp2 = mp;
26438420Smckusick 			xfer = (left > mp->m_len) ? mp->m_len : left;
26538420Smckusick #ifdef notdef
26638420Smckusick 			/* Not Yet.. */
26738420Smckusick 			if (uiop->uio_iov->iov_op != NULL)
26838420Smckusick 				(*(uiop->uio_iov->iov_op))
26938420Smckusick 				(uiocp, mtod(mp, caddr_t), xfer);
27038420Smckusick 			else
27138420Smckusick #endif
27238420Smckusick 			if (uiop->uio_segflg == UIO_SYSSPACE)
27338420Smckusick 				bcopy(uiocp, mtod(mp, caddr_t), xfer);
27438420Smckusick 			else
27538420Smckusick 				copyin(uiocp, mtod(mp, caddr_t), xfer);
27638420Smckusick 			len = mp->m_len;
27738420Smckusick 			mp->m_len = xfer;
27838420Smckusick 			left -= xfer;
27938420Smckusick 			uiocp += xfer;
28039585Smckusick 			uiop->uio_offset += xfer;
28138420Smckusick 			uiop->uio_resid -= xfer;
28238420Smckusick 		}
28338420Smckusick 		if (uiop->uio_iov->iov_len <= siz) {
28438420Smckusick 			uiop->uio_iovcnt--;
28538420Smckusick 			uiop->uio_iov++;
28638420Smckusick 		} else {
28738420Smckusick 			uiop->uio_iov->iov_base += uiosiz;
28838420Smckusick 			uiop->uio_iov->iov_len -= uiosiz;
28938420Smckusick 		}
29038420Smckusick 		siz -= uiosiz;
29138420Smckusick 	}
29238420Smckusick 	if (rem > 0) {
29338420Smckusick 		if (rem > (len-mp->m_len)) {
29438420Smckusick 			MGET(mp, M_WAIT, MT_DATA);
29538420Smckusick 			mp->m_len = 0;
29638420Smckusick 			mp2->m_next = mp;
29738420Smckusick 		}
29838420Smckusick 		cp = mtod(mp, caddr_t)+mp->m_len;
29938420Smckusick 		for (left = 0; left < rem; left++)
30038420Smckusick 			*cp++ = '\0';
30138420Smckusick 		mp->m_len += rem;
30238420Smckusick 		*bpos = cp;
30338420Smckusick 	} else
30438420Smckusick 		*bpos = mtod(mp, caddr_t)+mp->m_len;
30538420Smckusick 	*mq = mp;
30641902Smckusick 	return (0);
30738420Smckusick }
30838420Smckusick 
30938420Smckusick /*
31038420Smckusick  * Help break down an mbuf chain by setting the first siz bytes contiguous
31138420Smckusick  * pointed to by returned val.
31238420Smckusick  * If Updateflg == True we can overwrite the first part of the mbuf data
31338420Smckusick  * This is used by the macros nfsm_disect and nfsm_disecton for tough
31438420Smckusick  * cases. (The macros use the vars. dpos and dpos2)
31538420Smckusick  */
31638420Smckusick nfsm_disct(mdp, dposp, siz, left, updateflg, cp2)
31738420Smckusick 	struct mbuf **mdp;
31838420Smckusick 	caddr_t *dposp;
31938420Smckusick 	int siz;
32038420Smckusick 	int left;
32138420Smckusick 	int updateflg;
32238420Smckusick 	caddr_t *cp2;
32338420Smckusick {
32438420Smckusick 	register struct mbuf *mp, *mp2;
32538420Smckusick 	register int siz2, xfer;
32638420Smckusick 	register caddr_t p;
32738420Smckusick 
32838420Smckusick 	mp = *mdp;
32938420Smckusick 	while (left == 0) {
33038420Smckusick 		*mdp = mp = mp->m_next;
33138420Smckusick 		if (mp == NULL)
33241902Smckusick 			return (EBADRPC);
33338420Smckusick 		left = mp->m_len;
33438420Smckusick 		*dposp = mtod(mp, caddr_t);
33538420Smckusick 	}
33638420Smckusick 	if (left >= siz) {
33738420Smckusick 		*cp2 = *dposp;
33838420Smckusick 		*dposp += siz;
33938420Smckusick 	} else if (mp->m_next == NULL) {
34041902Smckusick 		return (EBADRPC);
34141902Smckusick 	} else if (siz > MHLEN) {
34238420Smckusick 		panic("nfs S too big");
34338420Smckusick 	} else {
34438420Smckusick 		/* Iff update, you can overwrite, else must alloc new mbuf */
34538420Smckusick 		if (updateflg) {
34638420Smckusick 			NFSMINOFF(mp);
34738420Smckusick 		} else {
34838420Smckusick 			MGET(mp2, M_WAIT, MT_DATA);
34938420Smckusick 			mp2->m_next = mp->m_next;
35038420Smckusick 			mp->m_next = mp2;
35138420Smckusick 			mp->m_len -= left;
35238420Smckusick 			mp = mp2;
35338420Smckusick 		}
35438420Smckusick 		*cp2 = p = mtod(mp, caddr_t);
35538420Smckusick 		bcopy(*dposp, p, left);		/* Copy what was left */
35638420Smckusick 		siz2 = siz-left;
35738420Smckusick 		p += left;
35838420Smckusick 		mp2 = mp->m_next;
35941902Smckusick 		/* Loop around copying up the siz2 bytes */
36038420Smckusick 		while (siz2 > 0) {
36138420Smckusick 			if (mp2 == NULL)
36238420Smckusick 				return (EBADRPC);
36338420Smckusick 			xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
36441902Smckusick 			if (xfer > 0) {
36541902Smckusick 				bcopy(mtod(mp2, caddr_t), p, xfer);
36641902Smckusick 				NFSMADV(mp2, xfer);
36741902Smckusick 				mp2->m_len -= xfer;
36841902Smckusick 				p += xfer;
36941902Smckusick 				siz2 -= xfer;
37041902Smckusick 			}
37138420Smckusick 			if (siz2 > 0)
37238420Smckusick 				mp2 = mp2->m_next;
37338420Smckusick 		}
37438420Smckusick 		mp->m_len = siz;
37538420Smckusick 		*mdp = mp2;
37638420Smckusick 		*dposp = mtod(mp2, caddr_t);
37738420Smckusick 	}
37839494Smckusick 	return (0);
37938420Smckusick }
38038420Smckusick 
38138420Smckusick /*
38241902Smckusick  * Advance the position in the mbuf chain.
38338420Smckusick  */
38438420Smckusick nfs_adv(mdp, dposp, offs, left)
38538420Smckusick 	struct mbuf **mdp;
38638420Smckusick 	caddr_t *dposp;
38738420Smckusick 	int offs;
38838420Smckusick 	int left;
38938420Smckusick {
39038420Smckusick 	register struct mbuf *m;
39138420Smckusick 	register int s;
39238420Smckusick 
39338420Smckusick 	m = *mdp;
39438420Smckusick 	s = left;
39538420Smckusick 	while (s < offs) {
39638420Smckusick 		offs -= s;
39738420Smckusick 		m = m->m_next;
39838420Smckusick 		if (m == NULL)
39941902Smckusick 			return (EBADRPC);
40038420Smckusick 		s = m->m_len;
40138420Smckusick 	}
40238420Smckusick 	*mdp = m;
40338420Smckusick 	*dposp = mtod(m, caddr_t)+offs;
40441902Smckusick 	return (0);
40538420Smckusick }
40638420Smckusick 
40738420Smckusick /*
40838420Smckusick  * Copy a string into mbufs for the hard cases...
40938420Smckusick  */
41038420Smckusick nfsm_strtmbuf(mb, bpos, cp, siz)
41138420Smckusick 	struct mbuf **mb;
41238420Smckusick 	char **bpos;
41338420Smckusick 	char *cp;
41438420Smckusick 	long siz;
41538420Smckusick {
41638420Smckusick 	register struct mbuf *m1, *m2;
41738420Smckusick 	long left, xfer, len, tlen;
41838420Smckusick 	u_long *p;
41938420Smckusick 	int putsize;
42038420Smckusick 
42138420Smckusick 	putsize = 1;
42238420Smckusick 	m2 = *mb;
42338420Smckusick 	left = NFSMSIZ(m2)-m2->m_len;
42438420Smckusick 	if (left > 0) {
42538420Smckusick 		p = ((u_long *)(*bpos));
42638420Smckusick 		*p++ = txdr_unsigned(siz);
42738420Smckusick 		putsize = 0;
42838420Smckusick 		left -= NFSX_UNSIGNED;
42938420Smckusick 		m2->m_len += NFSX_UNSIGNED;
43038420Smckusick 		if (left > 0) {
43138420Smckusick 			bcopy(cp, (caddr_t) p, left);
43238420Smckusick 			siz -= left;
43338420Smckusick 			cp += left;
43438420Smckusick 			m2->m_len += left;
43538420Smckusick 			left = 0;
43638420Smckusick 		}
43738420Smckusick 	}
43838420Smckusick 	/* Loop arround adding mbufs */
43938420Smckusick 	while (siz > 0) {
44038420Smckusick 		MGET(m1, M_WAIT, MT_DATA);
44138420Smckusick 		if (siz > MLEN)
44241902Smckusick 			MCLGET(m1, M_WAIT);
44338420Smckusick 		m1->m_len = NFSMSIZ(m1);
44438420Smckusick 		m2->m_next = m1;
44538420Smckusick 		m2 = m1;
44638420Smckusick 		p = mtod(m1, u_long *);
44738420Smckusick 		tlen = 0;
44838420Smckusick 		if (putsize) {
44938420Smckusick 			*p++ = txdr_unsigned(siz);
45038420Smckusick 			m1->m_len -= NFSX_UNSIGNED;
45138420Smckusick 			tlen = NFSX_UNSIGNED;
45238420Smckusick 			putsize = 0;
45338420Smckusick 		}
45438420Smckusick 		if (siz < m1->m_len) {
45538420Smckusick 			len = nfsm_rndup(siz);
45638420Smckusick 			xfer = siz;
45738420Smckusick 			if (xfer < len)
45838420Smckusick 				*(p+(xfer>>2)) = 0;
45938420Smckusick 		} else {
46038420Smckusick 			xfer = len = m1->m_len;
46138420Smckusick 		}
46238420Smckusick 		bcopy(cp, (caddr_t) p, xfer);
46338420Smckusick 		m1->m_len = len+tlen;
46438420Smckusick 		siz -= xfer;
46538420Smckusick 		cp += xfer;
46638420Smckusick 	}
46738420Smckusick 	*mb = m1;
46838420Smckusick 	*bpos = mtod(m1, caddr_t)+m1->m_len;
46941902Smckusick 	return (0);
47038420Smckusick }
47138420Smckusick 
47238420Smckusick /*
47338420Smckusick  * Called once to initialize data structures...
47438420Smckusick  */
47539444Smckusick nfs_init()
47638420Smckusick {
47738420Smckusick 	register int i;
47838420Smckusick 
47938420Smckusick 	rpc_vers = txdr_unsigned(RPC_VER2);
48038420Smckusick 	rpc_call = txdr_unsigned(RPC_CALL);
48138420Smckusick 	rpc_reply = txdr_unsigned(RPC_REPLY);
48238420Smckusick 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
48338420Smckusick 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
48438420Smckusick 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
48538420Smckusick 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
48638420Smckusick 	nfs_vers = txdr_unsigned(NFS_VER2);
48738420Smckusick 	nfs_prog = txdr_unsigned(NFS_PROG);
48838420Smckusick 	nfs_true = txdr_unsigned(TRUE);
48938420Smckusick 	nfs_false = txdr_unsigned(FALSE);
49038420Smckusick 	/* Loop thru nfs procids */
49138420Smckusick 	for (i = 0; i < NFS_NPROCS; i++)
49238420Smckusick 		nfs_procids[i] = txdr_unsigned(i);
49339345Smckusick 	/* Ensure async daemons disabled */
49441902Smckusick 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
49539345Smckusick 		nfs_iodwant[i] = (struct proc *)0;
49638420Smckusick 	nfs_xdrneg1 = txdr_unsigned(-1);
49738420Smckusick 	nfs_nhinit();			/* Init the nfsnode table */
49839755Smckusick 	nfsrv_initcache();		/* Init the server request cache */
49938884Smacklem 	rminit(nfsmap, (long)NFS_MAPREG, (long)1, "nfs mapreg", NFS_MSIZ);
50041902Smckusick 
50141902Smckusick 	/*
50241902Smckusick 	 * Initialize reply list and start timer
50341902Smckusick 	 */
50441902Smckusick 	nfsreqh.r_prev = nfsreqh.r_next = &nfsreqh;
50538420Smckusick 	nfs_timer();
50638420Smckusick }
50738420Smckusick 
50838420Smckusick /*
50938420Smckusick  * Fill in the rest of the rpc_unixauth and return it
51038420Smckusick  */
51138420Smckusick static char *nfs_unixauth(cr)
51238420Smckusick 	register struct ucred *cr;
51338420Smckusick {
51438420Smckusick 	register u_long *p;
51538420Smckusick 	register int i;
51638420Smckusick 	int ngr;
51738420Smckusick 
51838420Smckusick 	/* Maybe someday there should be a cache of AUTH_SHORT's */
51938420Smckusick 	if ((p = rpc_uidp) == NULL) {
52038425Smckusick #ifdef FILLINHOST
52138420Smckusick 		i = nfsm_rndup(hostnamelen)+(19*NFSX_UNSIGNED);
52238425Smckusick #else
52338425Smckusick 		i = 19*NFSX_UNSIGNED;
52438425Smckusick #endif
52538420Smckusick 		MALLOC(p, u_long *, i, M_TEMP, M_WAITOK);
52638420Smckusick 		bzero((caddr_t)p, i);
52738420Smckusick 		rpc_unixauth = (caddr_t)p;
52838420Smckusick 		*p++ = txdr_unsigned(RPCAUTH_UNIX);
52938420Smckusick 		p++;	/* Fill in size later */
53038420Smckusick 		*p++ = hostid;
53138425Smckusick #ifdef FILLINHOST
53238420Smckusick 		*p++ = txdr_unsigned(hostnamelen);
53338420Smckusick 		i = nfsm_rndup(hostnamelen);
53438420Smckusick 		bcopy(hostname, (caddr_t)p, hostnamelen);
53538420Smckusick 		p += (i>>2);
53638425Smckusick #else
53738425Smckusick 		*p++ = 0;
53838425Smckusick #endif
53938420Smckusick 		rpc_uidp = p;
54038420Smckusick 	}
54138420Smckusick 	*p++ = txdr_unsigned(cr->cr_uid);
54238420Smckusick 	*p++ = txdr_unsigned(cr->cr_groups[0]);
54338737Smckusick 	ngr = (cr->cr_ngroups > numgrps) ? numgrps : cr->cr_ngroups;
54438420Smckusick 	*p++ = txdr_unsigned(ngr);
54538420Smckusick 	for (i = 0; i < ngr; i++)
54638420Smckusick 		*p++ = txdr_unsigned(cr->cr_groups[i]);
54738420Smckusick 	/* And add the AUTH_NULL */
54838420Smckusick 	*p++ = 0;
54938420Smckusick 	*p = 0;
55038420Smckusick 	i = (((caddr_t)p)-rpc_unixauth)-12;
55138420Smckusick 	p = (u_long *)(rpc_unixauth+4);
55238420Smckusick 	*p = txdr_unsigned(i);
55341902Smckusick 	return (rpc_unixauth);
55438420Smckusick }
55538420Smckusick 
55638420Smckusick /*
55738420Smckusick  * Attribute cache routines.
55838420Smckusick  * nfs_loadattrcache() - loads or updates the cache contents from attributes
55938420Smckusick  *	that are on the mbuf list
56038420Smckusick  * nfs_getattrcache() - returns valid attributes if found in cache, returns
56138420Smckusick  *	error otherwise
56238420Smckusick  */
56338420Smckusick 
56438420Smckusick /*
56539444Smckusick  * Load the attribute cache (that lives in the nfsnode entry) with
56638420Smckusick  * the values on the mbuf list and
56738420Smckusick  * Iff vap not NULL
56838420Smckusick  *    copy the attributes to *vaper
56938420Smckusick  */
57039457Smckusick nfs_loadattrcache(vpp, mdp, dposp, vaper)
57139457Smckusick 	struct vnode **vpp;
57238420Smckusick 	struct mbuf **mdp;
57338420Smckusick 	caddr_t *dposp;
57438420Smckusick 	struct vattr *vaper;
57538420Smckusick {
57639457Smckusick 	register struct vnode *vp = *vpp;
57738420Smckusick 	register struct vattr *vap;
57838884Smacklem 	register struct nfsv2_fattr *fp;
57939444Smckusick 	extern struct vnodeops spec_nfsv2nodeops;
58039457Smckusick 	register struct nfsnode *np;
58139494Smckusick 	register long t1;
58239494Smckusick 	caddr_t dpos, cp2;
58339494Smckusick 	int error = 0;
58439494Smckusick 	struct mbuf *md;
58539444Smckusick 	enum vtype type;
58642244Smckusick 	long rdev;
58739444Smckusick 	struct timeval mtime;
58839444Smckusick 	struct vnode *nvp;
58938420Smckusick 
59038420Smckusick 	md = *mdp;
59138420Smckusick 	dpos = *dposp;
59238420Smckusick 	t1 = (mtod(md, caddr_t)+md->m_len)-dpos;
59338420Smckusick 	if (error = nfsm_disct(&md, &dpos, NFSX_FATTR, t1, TRUE, &cp2))
59438420Smckusick 		return (error);
59538884Smacklem 	fp = (struct nfsv2_fattr *)cp2;
59639444Smckusick 	type = nfstov_type(fp->fa_type);
59742244Smckusick 	rdev = fxdr_unsigned(long, fp->fa_rdev);
59839444Smckusick 	fxdr_time(&fp->fa_mtime, &mtime);
59939444Smckusick 	/*
60039444Smckusick 	 * If v_type == VNON it is a new node, so fill in the v_type,
60139444Smckusick 	 * n_mtime fields. Check to see if it represents a special
60239444Smckusick 	 * device, and if so, check for a possible alias. Once the
60339444Smckusick 	 * correct vnode has been obtained, fill in the rest of the
60439444Smckusick 	 * information.
60539444Smckusick 	 */
60638420Smckusick 	np = VTONFS(vp);
60739444Smckusick 	if (vp->v_type == VNON) {
60842244Smckusick 		if (type == VCHR && rdev == 0xffffffff)
60942244Smckusick 			vp->v_type = type = VFIFO;
61042244Smckusick 		else
61142244Smckusick 			vp->v_type = type;
61240295Smckusick 		if (vp->v_type == VFIFO) {
61340295Smckusick #ifdef FIFO
61440295Smckusick 			extern struct vnodeops fifo_nfsv2nodeops;
61540295Smckusick 			vp->v_op = &fifo_nfsv2nodeops;
61640295Smckusick #else
61740295Smckusick 			return (EOPNOTSUPP);
61840295Smckusick #endif /* FIFO */
61940295Smckusick 		}
62039444Smckusick 		if (vp->v_type == VCHR || vp->v_type == VBLK) {
62139444Smckusick 			vp->v_op = &spec_nfsv2nodeops;
62242244Smckusick 			if (nvp = checkalias(vp, (dev_t)rdev, vp->v_mount)) {
62339444Smckusick 				/*
62439444Smckusick 				 * Reinitialize aliased node.
62539444Smckusick 				 */
62639444Smckusick 				np = VTONFS(nvp);
62739444Smckusick 				np->n_vnode = nvp;
62839907Smckusick 				np->n_flag = 0;
62939907Smckusick 				nfs_lock(nvp);
63039444Smckusick 				bcopy((caddr_t)&VTONFS(vp)->n_fh,
63139444Smckusick 					(caddr_t)&np->n_fh, NFSX_FH);
63239444Smckusick 				insque(np, nfs_hash(&np->n_fh));
63339444Smckusick 				np->n_attrstamp = 0;
63439444Smckusick 				np->n_sillyrename = (struct sillyrename *)0;
63539444Smckusick 				/*
63639457Smckusick 				 * Discard unneeded vnode and update actual one
63739444Smckusick 				 */
63839444Smckusick 				vput(vp);
63941902Smckusick 				*vpp = nvp;
64039444Smckusick 			}
64139444Smckusick 		}
64239444Smckusick 		np->n_mtime = mtime.tv_sec;
64339444Smckusick 	}
64438420Smckusick 	vap = &np->n_vattr;
64539444Smckusick 	vap->va_type = type;
64638884Smacklem 	vap->va_mode = nfstov_mode(fp->fa_mode);
64738884Smacklem 	vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
64838884Smacklem 	vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
64938884Smacklem 	vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
65038884Smacklem 	vap->va_size = fxdr_unsigned(u_long, fp->fa_size);
65138884Smacklem 	if ((np->n_flag & NMODIFIED) == 0 || vap->va_size > np->n_size)
65238884Smacklem 		np->n_size = vap->va_size;
65340642Smckusick 	vap->va_size_rsv = 0;
65438884Smacklem 	vap->va_blocksize = fxdr_unsigned(long, fp->fa_blocksize);
65542244Smckusick 	vap->va_rdev = (dev_t)rdev;
65642244Smckusick 	vap->va_bytes = fxdr_unsigned(long, fp->fa_blocks) * NFS_FABLKSIZE;
65740642Smckusick 	vap->va_bytes_rsv = 0;
65842878Smckusick 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
65938884Smacklem 	vap->va_fileid = fxdr_unsigned(long, fp->fa_fileid);
66039755Smckusick 	vap->va_atime.tv_sec = fxdr_unsigned(long, fp->fa_atime.tv_sec);
66139755Smckusick 	vap->va_atime.tv_usec = 0;
66239755Smckusick 	vap->va_flags = fxdr_unsigned(u_long, fp->fa_atime.tv_usec);
66339444Smckusick 	vap->va_mtime = mtime;
66439755Smckusick 	vap->va_ctime.tv_sec = fxdr_unsigned(long, fp->fa_ctime.tv_sec);
66539755Smckusick 	vap->va_ctime.tv_usec = 0;
66639755Smckusick 	vap->va_gen = fxdr_unsigned(u_long, fp->fa_ctime.tv_usec);
66738420Smckusick 	np->n_attrstamp = time.tv_sec;
66838420Smckusick 	*dposp = dpos;
66938420Smckusick 	*mdp = md;
67038884Smacklem 	if (vaper != NULL) {
67138420Smckusick 		bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
67238884Smacklem 		if ((np->n_flag & NMODIFIED) && (np->n_size > vap->va_size))
67338884Smacklem 			vaper->va_size = np->n_size;
67438884Smacklem 	}
67538420Smckusick 	return (0);
67638420Smckusick }
67738420Smckusick 
67838420Smckusick /*
67938420Smckusick  * Check the time stamp
68038420Smckusick  * If the cache is valid, copy contents to *vap and return 0
68138420Smckusick  * otherwise return an error
68238420Smckusick  */
68338420Smckusick nfs_getattrcache(vp, vap)
68438420Smckusick 	register struct vnode *vp;
68538420Smckusick 	struct vattr *vap;
68638420Smckusick {
68738420Smckusick 	register struct nfsnode *np;
68838420Smckusick 
68938420Smckusick 	np = VTONFS(vp);
69038420Smckusick 	if ((time.tv_sec-np->n_attrstamp) < NFS_ATTRTIMEO) {
69138420Smckusick 		nfsstats.attrcache_hits++;
69238420Smckusick 		bcopy((caddr_t)&np->n_vattr,(caddr_t)vap,sizeof(struct vattr));
69339361Smckusick 		if ((np->n_flag & NMODIFIED) == 0)
69439361Smckusick 			np->n_size = vap->va_size;
69539361Smckusick 		else if (np->n_size > vap->va_size)
69638884Smacklem 			vap->va_size = np->n_size;
69738420Smckusick 		return (0);
69838420Smckusick 	} else {
69938420Smckusick 		nfsstats.attrcache_misses++;
70038420Smckusick 		return (ENOENT);
70138420Smckusick 	}
70238420Smckusick }
70338420Smckusick 
70438420Smckusick /*
70541902Smckusick  * Set up nameidata for a namei() call and do it
70638420Smckusick  */
70738420Smckusick nfs_namei(ndp, fhp, len, mdp, dposp)
70838420Smckusick 	register struct nameidata *ndp;
70938420Smckusick 	fhandle_t *fhp;
71038420Smckusick 	int len;
71138420Smckusick 	struct mbuf **mdp;
71238420Smckusick 	caddr_t *dposp;
71338420Smckusick {
71438420Smckusick 	register int i, rem;
71538420Smckusick 	register struct mbuf *md;
71638420Smckusick 	register char *cp;
71741902Smckusick 	struct vnode *dp;
71838420Smckusick 	int flag;
71941902Smckusick 	int error;
72038420Smckusick 
72142244Smckusick 	if ((ndp->ni_nameiop & HASBUF) == 0) {
72242244Smckusick 		flag = ndp->ni_nameiop & OPFLAG;
72342244Smckusick 		/*
72442244Smckusick 		 * Copy the name from the mbuf list to the d_name field of ndp
72542244Smckusick 		 * and set the various ndp fields appropriately.
72642244Smckusick 		 */
72742244Smckusick 		cp = *dposp;
72842244Smckusick 		md = *mdp;
72942244Smckusick 		rem = mtod(md, caddr_t)+md->m_len-cp;
73042244Smckusick 		ndp->ni_hash = 0;
73142244Smckusick 		for (i = 0; i < len;) {
73242244Smckusick 			while (rem == 0) {
73342244Smckusick 				md = md->m_next;
73442244Smckusick 				if (md == NULL)
73542244Smckusick 					return (EBADRPC);
73642244Smckusick 				cp = mtod(md, caddr_t);
73742244Smckusick 				rem = md->m_len;
73842244Smckusick 			}
73942244Smckusick 			if (*cp == '\0' || *cp == '/')
74042244Smckusick 				return (EINVAL);
74142244Smckusick 			if (*cp & 0200)
74242244Smckusick 				if ((*cp&0377) == ('/'|0200) || flag != DELETE)
74342244Smckusick 					return (EINVAL);
74442244Smckusick 			ndp->ni_dent.d_name[i++] = *cp;
74542244Smckusick 			ndp->ni_hash += (unsigned char)*cp * i;
74642244Smckusick 			cp++;
74742244Smckusick 			rem--;
74838420Smckusick 		}
74942244Smckusick 		*mdp = md;
75042244Smckusick 		*dposp = cp;
75142244Smckusick 		len = nfsm_rndup(len)-len;
75242244Smckusick 		if (len > 0) {
75342244Smckusick 			if (rem < len) {
75442244Smckusick 				if (error = nfs_adv(mdp, dposp, len, rem))
75542244Smckusick 					return (error);
75642244Smckusick 			} else
75742244Smckusick 				*dposp += len;
75842244Smckusick 		}
75942244Smckusick 	} else
76042244Smckusick 		i = len;
76138420Smckusick 	ndp->ni_namelen = i;
76238420Smckusick 	ndp->ni_dent.d_namlen = i;
76338420Smckusick 	ndp->ni_dent.d_name[i] = '\0';
76442244Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
76538425Smckusick 	ndp->ni_pathlen = 1;
76641902Smckusick 	ndp->ni_pnbuf = ndp->ni_dirp = ndp->ni_ptr = &ndp->ni_dent.d_name[0];
76738420Smckusick 	ndp->ni_next = &ndp->ni_dent.d_name[i];
76841902Smckusick 	ndp->ni_nameiop |= (NOCROSSMOUNT | REMOTE | HASBUF);
76938420Smckusick 
77041902Smckusick 	if (error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cred))
77138420Smckusick 		return (error);
77238425Smckusick 	if (dp->v_type != VDIR) {
77341902Smckusick 		vrele(dp);
77438425Smckusick 		return (ENOTDIR);
77538425Smckusick 	}
77639345Smckusick 	/*
77739345Smckusick 	 * Must set current directory here to avoid confusion in namei()
77839345Smckusick 	 * called from rename()
77939345Smckusick 	 */
78038425Smckusick 	ndp->ni_cdir = dp;
78141398Smckusick 	ndp->ni_rdir = NULLVP;
78238420Smckusick 
78338420Smckusick 	/*
78441902Smckusick 	 * And call namei() to do the real work
78538420Smckusick 	 */
78641902Smckusick 	error = namei(ndp);
78741902Smckusick 	vrele(dp);
78838420Smckusick 	return (error);
78938420Smckusick }
79038420Smckusick 
79138420Smckusick /*
79238420Smckusick  * A fiddled version of m_adj() that ensures null fill to a long
79338420Smckusick  * boundary and only trims off the back end
79438420Smckusick  */
79538420Smckusick nfsm_adj(mp, len, nul)
79638420Smckusick 	struct mbuf *mp;
79738420Smckusick 	register int len;
79838420Smckusick 	int nul;
79938420Smckusick {
80038420Smckusick 	register struct mbuf *m;
80138420Smckusick 	register int count, i;
80238420Smckusick 	register char *cp;
80338420Smckusick 
80438420Smckusick 	/*
80538420Smckusick 	 * Trim from tail.  Scan the mbuf chain,
80638420Smckusick 	 * calculating its length and finding the last mbuf.
80738420Smckusick 	 * If the adjustment only affects this mbuf, then just
80838420Smckusick 	 * adjust and return.  Otherwise, rescan and truncate
80938420Smckusick 	 * after the remaining size.
81038420Smckusick 	 */
81138420Smckusick 	count = 0;
81238420Smckusick 	m = mp;
81338420Smckusick 	for (;;) {
81438420Smckusick 		count += m->m_len;
81538420Smckusick 		if (m->m_next == (struct mbuf *)0)
81638420Smckusick 			break;
81738420Smckusick 		m = m->m_next;
81838420Smckusick 	}
81938579Smckusick 	if (m->m_len > len) {
82038420Smckusick 		m->m_len -= len;
82138420Smckusick 		if (nul > 0) {
82238420Smckusick 			cp = mtod(m, caddr_t)+m->m_len-nul;
82338420Smckusick 			for (i = 0; i < nul; i++)
82438420Smckusick 				*cp++ = '\0';
82538420Smckusick 		}
82638420Smckusick 		return;
82738420Smckusick 	}
82838420Smckusick 	count -= len;
82938420Smckusick 	if (count < 0)
83038420Smckusick 		count = 0;
83138420Smckusick 	/*
83238420Smckusick 	 * Correct length for chain is "count".
83338420Smckusick 	 * Find the mbuf with last data, adjust its length,
83438420Smckusick 	 * and toss data from remaining mbufs on chain.
83538420Smckusick 	 */
83638420Smckusick 	for (m = mp; m; m = m->m_next) {
83738420Smckusick 		if (m->m_len >= count) {
83838420Smckusick 			m->m_len = count;
83938420Smckusick 			if (nul > 0) {
84038420Smckusick 				cp = mtod(m, caddr_t)+m->m_len-nul;
84138420Smckusick 				for (i = 0; i < nul; i++)
84238420Smckusick 					*cp++ = '\0';
84338420Smckusick 			}
84438420Smckusick 			break;
84538420Smckusick 		}
84638420Smckusick 		count -= m->m_len;
84738420Smckusick 	}
84838420Smckusick 	while (m = m->m_next)
84938420Smckusick 		m->m_len = 0;
85038420Smckusick }
85138420Smckusick 
85238420Smckusick /*
85338420Smckusick  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
85438420Smckusick  * 	- look up fsid in mount list (if not found ret error)
85538420Smckusick  *	- check that it is exported
85638420Smckusick  *	- get vp by calling VFS_FHTOVP() macro
85738420Smckusick  *	- if not lockflag unlock it with VOP_UNLOCK()
85841902Smckusick  *	- if cred->cr_uid == 0 set it to m_exroot
85938420Smckusick  */
86038420Smckusick nfsrv_fhtovp(fhp, lockflag, vpp, cred)
86138420Smckusick 	fhandle_t *fhp;
86238420Smckusick 	int lockflag;
86338420Smckusick 	struct vnode **vpp;
86438420Smckusick 	struct ucred *cred;
86538420Smckusick {
86638420Smckusick 	register struct mount *mp;
86738420Smckusick 
86838420Smckusick 	if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
86938420Smckusick 		return (ESTALE);
87041398Smckusick 	if ((mp->mnt_flag & MNT_EXPORTED) == 0)
87138420Smckusick 		return (EACCES);
87238420Smckusick 	if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
87338420Smckusick 		return (ESTALE);
87438420Smckusick 	if (cred->cr_uid == 0)
87541398Smckusick 		cred->cr_uid = mp->mnt_exroot;
87638420Smckusick 	if (!lockflag)
87738420Smckusick 		VOP_UNLOCK(*vpp);
87838420Smckusick 	return (0);
87938420Smckusick }
880