xref: /csrg-svn/sys/nfs/nfs_subs.c (revision 40136)
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  *
838420Smckusick  * Redistribution and use in source and binary forms are permitted
938420Smckusick  * provided that the above copyright notice and this paragraph are
1038420Smckusick  * duplicated in all such forms and that any documentation,
1138420Smckusick  * advertising materials, and other materials related to such
1238420Smckusick  * distribution and use acknowledge that the software was developed
1338420Smckusick  * by the University of California, Berkeley.  The name of the
1438420Smckusick  * University may not be used to endorse or promote products derived
1538420Smckusick  * from this software without specific prior written permission.
1638420Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1738420Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1838420Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1938420Smckusick  *
20*40136Smckusick  *	@(#)nfs_subs.c	7.17 (Berkeley) 02/17/90
2138420Smckusick  */
2238420Smckusick 
2338420Smckusick /*
2438420Smckusick  * These functions support the macros and help fiddle mbuf chains for
2538420Smckusick  * the nfs op functions. They do things like create the rpc header and
2638420Smckusick  * copy data between mbuf chains and uio lists.
2738420Smckusick  */
2838420Smckusick #include "param.h"
2939345Smckusick #include "user.h"
3039345Smckusick #include "proc.h"
31*40136Smckusick #include "systm.h"
32*40136Smckusick #include "kernel.h"
3338420Smckusick #include "mount.h"
3438420Smckusick #include "file.h"
3538420Smckusick #include "vnode.h"
36*40136Smckusick #include "mbuf.h"
37*40136Smckusick #include "errno.h"
38*40136Smckusick #include "strings.h"
3938884Smacklem #include "map.h"
4038420Smckusick #include "rpcv2.h"
4138420Smckusick #include "nfsv2.h"
4238420Smckusick #include "nfsnode.h"
4338420Smckusick #include "nfs.h"
4438884Smacklem #include "nfsiom.h"
4538420Smckusick #include "xdr_subs.h"
4638420Smckusick #include "nfsm_subs.h"
4738420Smckusick 
4838420Smckusick #define TRUE	1
4938420Smckusick #define	FALSE	0
5038420Smckusick 
5138420Smckusick /*
5238420Smckusick  * Data items converted to xdr at startup, since they are constant
5338420Smckusick  * This is kinda hokey, but may save a little time doing byte swaps
5438420Smckusick  */
5538420Smckusick u_long nfs_procids[NFS_NPROCS];
5638420Smckusick u_long nfs_xdrneg1;
5738420Smckusick u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied,
5838420Smckusick 	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
5938420Smckusick u_long nfs_vers, nfs_prog, nfs_true, nfs_false;
6038420Smckusick /* And other global data */
6138420Smckusick static u_long *rpc_uidp = (u_long *)0;
6238420Smckusick static u_long nfs_xid = 1;
6338420Smckusick static char *rpc_unixauth;
6438420Smckusick extern long hostid;
6538420Smckusick extern enum vtype v_type[NFLNK+1];
6639345Smckusick extern struct proc *nfs_iodwant[MAX_ASYNCDAEMON];
6738884Smacklem extern struct map nfsmap[NFS_MSIZ];
6838420Smckusick 
6938420Smckusick /* Function ret types */
7038420Smckusick static char *nfs_unixauth();
7138420Smckusick 
7238420Smckusick /*
7338737Smckusick  * Maximum number of groups passed through to NFS server.
7438737Smckusick  * For release 3.X systems, the maximum value is 8.
7538737Smckusick  * For release 4.X systems, the maximum value is 10.
7638737Smckusick  */
7738737Smckusick int numgrps = 8;
7838737Smckusick 
7938737Smckusick /*
8038420Smckusick  * Create the header for an rpc request packet
8138420Smckusick  * The function nfs_unixauth() creates a unix style authorization string
8238420Smckusick  * and returns a ptr to it.
8338420Smckusick  * The hsiz is the size of the rest of the nfs request header.
8438420Smckusick  * (just used to decide if a cluster is a good idea)
8539494Smckusick  * nb: Note that the prog, vers and procid args are already in xdr byte order
8638420Smckusick  */
8739494Smckusick struct mbuf *nfsm_reqh(prog, vers, procid, cred, hsiz, bpos, mb, retxid)
8838420Smckusick 	u_long prog;
8938420Smckusick 	u_long vers;
9039494Smckusick 	u_long procid;
9138420Smckusick 	struct ucred *cred;
9238420Smckusick 	int hsiz;
9338420Smckusick 	caddr_t *bpos;
9438420Smckusick 	struct mbuf **mb;
9538420Smckusick 	u_long *retxid;
9638420Smckusick {
9738420Smckusick 	register struct mbuf *mreq, *m;
9838420Smckusick 	register u_long *p;
9938420Smckusick 	struct mbuf *m1;
10038420Smckusick 	char *ap;
10138420Smckusick 	int asiz, siz;
10238420Smckusick 
10338420Smckusick 	NFSMGETHDR(mreq);
10438737Smckusick 	asiz = (((cred->cr_ngroups > numgrps) ? numgrps : cred->cr_ngroups)<<2);
10538425Smckusick #ifdef FILLINHOST
10638420Smckusick 	asiz += nfsm_rndup(hostnamelen)+(9*NFSX_UNSIGNED);
10738425Smckusick #else
10838425Smckusick 	asiz += 9*NFSX_UNSIGNED;
10938425Smckusick #endif
11038420Smckusick 
11138420Smckusick 	/* If we need a lot, alloc a cluster ?? */
11238420Smckusick 	if ((asiz+hsiz+RPC_SIZ) > MHLEN)
11338420Smckusick 		NFSMCLGET(mreq, M_WAIT);
11438420Smckusick 	mreq->m_len = NFSMSIZ(mreq);
11538420Smckusick 	siz = mreq->m_len;
11638420Smckusick 	m1 = mreq;
11738420Smckusick 	/*
11838420Smckusick 	 * Alloc enough mbufs
11938420Smckusick 	 * We do it now to avoid all sleeps after the call to nfs_unixauth()
12038420Smckusick 	 */
12138420Smckusick 	while ((asiz+RPC_SIZ) > siz) {
12238420Smckusick 		MGET(m, M_WAIT, MT_DATA);
12338420Smckusick 		m1->m_next = m;
12438420Smckusick 		m->m_len = MLEN;
12538420Smckusick 		siz += MLEN;
12638420Smckusick 		m1 = m;
12738420Smckusick 	}
12838420Smckusick 	p = mtod(mreq, u_long *);
12938420Smckusick 	*p++ = *retxid = txdr_unsigned(++nfs_xid);
13038420Smckusick 	*p++ = rpc_call;
13138420Smckusick 	*p++ = rpc_vers;
13238420Smckusick 	*p++ = prog;
13338420Smckusick 	*p++ = vers;
13439494Smckusick 	*p++ = procid;
13538420Smckusick 
13638420Smckusick 	/* Now we can call nfs_unixauth() and copy it in */
13738420Smckusick 	ap = nfs_unixauth(cred);
13838420Smckusick 	m = mreq;
13938420Smckusick 	siz = m->m_len-RPC_SIZ;
14038420Smckusick 	if (asiz <= siz) {
14138420Smckusick 		bcopy(ap, (caddr_t)p, asiz);
14238420Smckusick 		m->m_len = asiz+RPC_SIZ;
14338420Smckusick 	} else {
14438420Smckusick 		bcopy(ap, (caddr_t)p, siz);
14538420Smckusick 		ap += siz;
14638420Smckusick 		asiz -= siz;
14738420Smckusick 		while (asiz > 0) {
14838420Smckusick 			siz = (asiz > MLEN) ? MLEN : asiz;
14938420Smckusick 			m = m->m_next;
15038420Smckusick 			bcopy(ap, mtod(m, caddr_t), siz);
15138420Smckusick 			m->m_len = siz;
15238420Smckusick 			asiz -= siz;
15338420Smckusick 			ap += siz;
15438420Smckusick 		}
15538420Smckusick 	}
15638420Smckusick 
15738420Smckusick 	/* Finally, return values */
15838420Smckusick 	*mb = m;
15938420Smckusick 	*bpos = mtod(m, caddr_t)+m->m_len;
16038420Smckusick 	return (mreq);
16138420Smckusick }
16238420Smckusick 
16338420Smckusick /*
16438420Smckusick  * copies mbuf chain to the uio scatter/gather list
16538420Smckusick  */
16638420Smckusick nfsm_mbuftouio(mrep, uiop, siz, dpos)
16738420Smckusick 	struct mbuf **mrep;
16838420Smckusick 	struct uio *uiop;
16938420Smckusick 	int siz;
17038420Smckusick 	caddr_t *dpos;
17138420Smckusick {
17238420Smckusick 	register int xfer, left, len;
17338420Smckusick 	register struct mbuf *mp;
17438420Smckusick 	register char *mbufcp, *uiocp;
17538420Smckusick 	long uiosiz, rem;
17638420Smckusick 
17738420Smckusick 	mp = *mrep;
17838420Smckusick 	mbufcp = *dpos;
17938420Smckusick 	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
18038420Smckusick 	rem = nfsm_rndup(siz)-siz;
18138420Smckusick 	while (siz > 0) {
18238420Smckusick 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
18338420Smckusick 			return(EFBIG);
18438420Smckusick 		left = uiop->uio_iov->iov_len;
18538420Smckusick 		uiocp = uiop->uio_iov->iov_base;
18638420Smckusick 		if (left > siz)
18738420Smckusick 			left = siz;
18838420Smckusick 		uiosiz = left;
18938420Smckusick 		while (left > 0) {
19038420Smckusick 			while (len == 0) {
19138420Smckusick 				mp = mp->m_next;
19238420Smckusick 				if (mp == NULL)
19338420Smckusick 					return (EBADRPC);
19438420Smckusick 				mbufcp = mtod(mp, caddr_t);
19538420Smckusick 				len = mp->m_len;
19638420Smckusick 			}
19738420Smckusick 			xfer = (left > len) ? len : left;
19838420Smckusick #ifdef notdef
19938420Smckusick 			/* Not Yet.. */
20038420Smckusick 			if (uiop->uio_iov->iov_op != NULL)
20138420Smckusick 				(*(uiop->uio_iov->iov_op))
20238420Smckusick 				(mbufcp, uiocp, xfer);
20338420Smckusick 			else
20438420Smckusick #endif
20538420Smckusick 			if (uiop->uio_segflg == UIO_SYSSPACE)
20638420Smckusick 				bcopy(mbufcp, uiocp, xfer);
20738420Smckusick 			else
20838420Smckusick 				copyout(mbufcp, uiocp, xfer);
20938420Smckusick 			left -= xfer;
21038420Smckusick 			len -= xfer;
21138420Smckusick 			mbufcp += xfer;
21238420Smckusick 			uiocp += xfer;
21339585Smckusick 			uiop->uio_offset += xfer;
21438420Smckusick 			uiop->uio_resid -= xfer;
21538420Smckusick 		}
21638420Smckusick 		if (uiop->uio_iov->iov_len <= siz) {
21738420Smckusick 			uiop->uio_iovcnt--;
21838420Smckusick 			uiop->uio_iov++;
21938420Smckusick 		} else {
22038420Smckusick 			uiop->uio_iov->iov_base += uiosiz;
22138420Smckusick 			uiop->uio_iov->iov_len -= uiosiz;
22238420Smckusick 		}
22338420Smckusick 		siz -= uiosiz;
22438420Smckusick 	}
22538420Smckusick 	if (rem > 0)
22638420Smckusick 		mbufcp += rem;
22738420Smckusick 	*dpos = mbufcp;
22838420Smckusick 	*mrep = mp;
22938420Smckusick 	return(0);
23038420Smckusick }
23138420Smckusick 
23238420Smckusick /*
23338420Smckusick  * copies a uio scatter/gather list to an mbuf chain...
23438420Smckusick  */
23538420Smckusick nfsm_uiotombuf(uiop, mq, siz, bpos)
23638420Smckusick 	register struct uio *uiop;
23738420Smckusick 	struct mbuf **mq;
23838420Smckusick 	int siz;
23938420Smckusick 	caddr_t *bpos;
24038420Smckusick {
24138420Smckusick 	register struct mbuf *mp;
24238420Smckusick 	struct mbuf *mp2;
24339494Smckusick 	long xfer, left, uiosiz;
24438420Smckusick 	int clflg;
24538420Smckusick 	int rem, len;
24638420Smckusick 	char *cp, *uiocp;
24738420Smckusick 
24838420Smckusick 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
24938420Smckusick 		clflg = 1;
25038420Smckusick 	else
25138420Smckusick 		clflg = 0;
25238420Smckusick 	rem = nfsm_rndup(siz)-siz;
25338420Smckusick 	mp2 = *mq;
25438420Smckusick 	while (siz > 0) {
25538420Smckusick 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
25638420Smckusick 			return(EINVAL);
25738420Smckusick 		left = uiop->uio_iov->iov_len;
25838420Smckusick 		uiocp = uiop->uio_iov->iov_base;
25938420Smckusick 		if (left > siz)
26038420Smckusick 			left = siz;
26138420Smckusick 		uiosiz = left;
26238420Smckusick 		while (left > 0) {
26338420Smckusick 			MGET(mp, M_WAIT, MT_DATA);
26438420Smckusick 			if (clflg)
26538420Smckusick 				NFSMCLGET(mp, M_WAIT);
26638420Smckusick 			mp->m_len = NFSMSIZ(mp);
26738420Smckusick 			mp2->m_next = mp;
26838420Smckusick 			mp2 = mp;
26938420Smckusick 			xfer = (left > mp->m_len) ? mp->m_len : left;
27038420Smckusick #ifdef notdef
27138420Smckusick 			/* Not Yet.. */
27238420Smckusick 			if (uiop->uio_iov->iov_op != NULL)
27338420Smckusick 				(*(uiop->uio_iov->iov_op))
27438420Smckusick 				(uiocp, mtod(mp, caddr_t), xfer);
27538420Smckusick 			else
27638420Smckusick #endif
27738420Smckusick 			if (uiop->uio_segflg == UIO_SYSSPACE)
27838420Smckusick 				bcopy(uiocp, mtod(mp, caddr_t), xfer);
27938420Smckusick 			else
28038420Smckusick 				copyin(uiocp, mtod(mp, caddr_t), xfer);
28138420Smckusick 			len = mp->m_len;
28238420Smckusick 			mp->m_len = xfer;
28338420Smckusick 			left -= xfer;
28438420Smckusick 			uiocp += xfer;
28539585Smckusick 			uiop->uio_offset += xfer;
28638420Smckusick 			uiop->uio_resid -= xfer;
28738420Smckusick 		}
28838420Smckusick 		if (uiop->uio_iov->iov_len <= siz) {
28938420Smckusick 			uiop->uio_iovcnt--;
29038420Smckusick 			uiop->uio_iov++;
29138420Smckusick 		} else {
29238420Smckusick 			uiop->uio_iov->iov_base += uiosiz;
29338420Smckusick 			uiop->uio_iov->iov_len -= uiosiz;
29438420Smckusick 		}
29538420Smckusick 		siz -= uiosiz;
29638420Smckusick 	}
29738420Smckusick 	if (rem > 0) {
29838420Smckusick 		if (rem > (len-mp->m_len)) {
29938420Smckusick 			MGET(mp, M_WAIT, MT_DATA);
30038420Smckusick 			mp->m_len = 0;
30138420Smckusick 			mp2->m_next = mp;
30238420Smckusick 		}
30338420Smckusick 		cp = mtod(mp, caddr_t)+mp->m_len;
30438420Smckusick 		for (left = 0; left < rem; left++)
30538420Smckusick 			*cp++ = '\0';
30638420Smckusick 		mp->m_len += rem;
30738420Smckusick 		*bpos = cp;
30838420Smckusick 	} else
30938420Smckusick 		*bpos = mtod(mp, caddr_t)+mp->m_len;
31038420Smckusick 	*mq = mp;
31138420Smckusick 	return(0);
31238420Smckusick }
31338420Smckusick 
31438420Smckusick /*
31538420Smckusick  * Help break down an mbuf chain by setting the first siz bytes contiguous
31638420Smckusick  * pointed to by returned val.
31738420Smckusick  * If Updateflg == True we can overwrite the first part of the mbuf data
31838420Smckusick  * This is used by the macros nfsm_disect and nfsm_disecton for tough
31938420Smckusick  * cases. (The macros use the vars. dpos and dpos2)
32038420Smckusick  */
32138420Smckusick nfsm_disct(mdp, dposp, siz, left, updateflg, cp2)
32238420Smckusick 	struct mbuf **mdp;
32338420Smckusick 	caddr_t *dposp;
32438420Smckusick 	int siz;
32538420Smckusick 	int left;
32638420Smckusick 	int updateflg;
32738420Smckusick 	caddr_t *cp2;
32838420Smckusick {
32938420Smckusick 	register struct mbuf *mp, *mp2;
33038420Smckusick 	register int siz2, xfer;
33138420Smckusick 	register caddr_t p;
33238420Smckusick 
33338420Smckusick 	mp = *mdp;
33438420Smckusick 	while (left == 0) {
33538420Smckusick 		*mdp = mp = mp->m_next;
33638420Smckusick 		if (mp == NULL)
33738420Smckusick 			return(EBADRPC);
33838420Smckusick 		left = mp->m_len;
33938420Smckusick 		*dposp = mtod(mp, caddr_t);
34038420Smckusick 	}
34138420Smckusick 	if (left >= siz) {
34238420Smckusick 		*cp2 = *dposp;
34338420Smckusick 		*dposp += siz;
34438420Smckusick 		return(0);
34538420Smckusick 	} else if (mp->m_next == NULL) {
34638420Smckusick 		return(EBADRPC);
34738420Smckusick 	} else if (siz > MCLBYTES) {
34838420Smckusick 		panic("nfs S too big");
34938420Smckusick 	} else {
35038420Smckusick 		/* Iff update, you can overwrite, else must alloc new mbuf */
35138420Smckusick 		if (updateflg) {
35238420Smckusick 			NFSMINOFF(mp);
35338420Smckusick 		} else {
35438420Smckusick 			MGET(mp2, M_WAIT, MT_DATA);
35538420Smckusick 			mp2->m_next = mp->m_next;
35638420Smckusick 			mp->m_next = mp2;
35738420Smckusick 			mp->m_len -= left;
35838420Smckusick 			mp = mp2;
35938420Smckusick 		}
36038420Smckusick 		/* Alloc cluster iff we need it */
36138420Smckusick 		if (!M_HASCL(mp) && siz > NFSMSIZ(mp)) {
36238420Smckusick 			NFSMCLGET(mp, M_WAIT);
36338420Smckusick 			if (!M_HASCL(mp))
36438420Smckusick 				return(ENOBUFS);
36538420Smckusick 		}
36638420Smckusick 		*cp2 = p = mtod(mp, caddr_t);
36738420Smckusick 		bcopy(*dposp, p, left);		/* Copy what was left */
36838420Smckusick 		siz2 = siz-left;
36938420Smckusick 		p += left;
37038420Smckusick 		mp2 = mp->m_next;
37138420Smckusick 		/* Loop arround copying up the siz2 bytes */
37238420Smckusick 		while (siz2 > 0) {
37338420Smckusick 			if (mp2 == NULL)
37438420Smckusick 				return (EBADRPC);
37538420Smckusick 			xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
37638420Smckusick 			bcopy(mtod(mp2, caddr_t), p, xfer);
37738420Smckusick 			NFSMADV(mp2, xfer);
37838420Smckusick 			mp2->m_len -= xfer;
37938420Smckusick 			siz2 -= xfer;
38038420Smckusick 			if (siz2 > 0)
38138420Smckusick 				mp2 = mp2->m_next;
38238420Smckusick 		}
38338420Smckusick 		mp->m_len = siz;
38438420Smckusick 		*mdp = mp2;
38538420Smckusick 		*dposp = mtod(mp2, caddr_t);
38638420Smckusick 	}
38739494Smckusick 	return (0);
38838420Smckusick }
38938420Smckusick 
39038420Smckusick /*
39138420Smckusick  * Advance the position in the mbuf chain with/without freeing mbufs
39238420Smckusick  */
39338420Smckusick nfs_adv(mdp, dposp, offs, left)
39438420Smckusick 	struct mbuf **mdp;
39538420Smckusick 	caddr_t *dposp;
39638420Smckusick 	int offs;
39738420Smckusick 	int left;
39838420Smckusick {
39938420Smckusick 	register struct mbuf *m;
40038420Smckusick 	register int s;
40138420Smckusick 
40238420Smckusick 	m = *mdp;
40338420Smckusick 	s = left;
40438420Smckusick 	while (s < offs) {
40538420Smckusick 		offs -= s;
40638420Smckusick 		m = m->m_next;
40738420Smckusick 		if (m == NULL)
40838420Smckusick 			return(EBADRPC);
40938420Smckusick 		s = m->m_len;
41038420Smckusick 	}
41138420Smckusick 	*mdp = m;
41238420Smckusick 	*dposp = mtod(m, caddr_t)+offs;
41338420Smckusick 	return(0);
41438420Smckusick }
41538420Smckusick 
41638420Smckusick /*
41738420Smckusick  * Copy a string into mbufs for the hard cases...
41838420Smckusick  */
41938420Smckusick nfsm_strtmbuf(mb, bpos, cp, siz)
42038420Smckusick 	struct mbuf **mb;
42138420Smckusick 	char **bpos;
42238420Smckusick 	char *cp;
42338420Smckusick 	long siz;
42438420Smckusick {
42538420Smckusick 	register struct mbuf *m1, *m2;
42638420Smckusick 	long left, xfer, len, tlen;
42738420Smckusick 	u_long *p;
42838420Smckusick 	int putsize;
42938420Smckusick 
43038420Smckusick 	putsize = 1;
43138420Smckusick 	m2 = *mb;
43238420Smckusick 	left = NFSMSIZ(m2)-m2->m_len;
43338420Smckusick 	if (left > 0) {
43438420Smckusick 		p = ((u_long *)(*bpos));
43538420Smckusick 		*p++ = txdr_unsigned(siz);
43638420Smckusick 		putsize = 0;
43738420Smckusick 		left -= NFSX_UNSIGNED;
43838420Smckusick 		m2->m_len += NFSX_UNSIGNED;
43938420Smckusick 		if (left > 0) {
44038420Smckusick 			bcopy(cp, (caddr_t) p, left);
44138420Smckusick 			siz -= left;
44238420Smckusick 			cp += left;
44338420Smckusick 			m2->m_len += left;
44438420Smckusick 			left = 0;
44538420Smckusick 		}
44638420Smckusick 	}
44738420Smckusick 	/* Loop arround adding mbufs */
44838420Smckusick 	while (siz > 0) {
44938420Smckusick 		MGET(m1, M_WAIT, MT_DATA);
45038420Smckusick 		if (siz > MLEN)
45138420Smckusick 			NFSMCLGET(m1, M_WAIT);
45238420Smckusick 		m1->m_len = NFSMSIZ(m1);
45338420Smckusick 		m2->m_next = m1;
45438420Smckusick 		m2 = m1;
45538420Smckusick 		p = mtod(m1, u_long *);
45638420Smckusick 		tlen = 0;
45738420Smckusick 		if (putsize) {
45838420Smckusick 			*p++ = txdr_unsigned(siz);
45938420Smckusick 			m1->m_len -= NFSX_UNSIGNED;
46038420Smckusick 			tlen = NFSX_UNSIGNED;
46138420Smckusick 			putsize = 0;
46238420Smckusick 		}
46338420Smckusick 		if (siz < m1->m_len) {
46438420Smckusick 			len = nfsm_rndup(siz);
46538420Smckusick 			xfer = siz;
46638420Smckusick 			if (xfer < len)
46738420Smckusick 				*(p+(xfer>>2)) = 0;
46838420Smckusick 		} else {
46938420Smckusick 			xfer = len = m1->m_len;
47038420Smckusick 		}
47138420Smckusick 		bcopy(cp, (caddr_t) p, xfer);
47238420Smckusick 		m1->m_len = len+tlen;
47338420Smckusick 		siz -= xfer;
47438420Smckusick 		cp += xfer;
47538420Smckusick 	}
47638420Smckusick 	*mb = m1;
47738420Smckusick 	*bpos = mtod(m1, caddr_t)+m1->m_len;
47838420Smckusick 	return(0);
47938420Smckusick }
48038420Smckusick 
48138420Smckusick /*
48238420Smckusick  * Called once to initialize data structures...
48338420Smckusick  */
48439444Smckusick nfs_init()
48538420Smckusick {
48638420Smckusick 	register int i;
48738420Smckusick 
48838420Smckusick 	rpc_vers = txdr_unsigned(RPC_VER2);
48938420Smckusick 	rpc_call = txdr_unsigned(RPC_CALL);
49038420Smckusick 	rpc_reply = txdr_unsigned(RPC_REPLY);
49138420Smckusick 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
49238420Smckusick 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
49338420Smckusick 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
49438420Smckusick 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
49538420Smckusick 	nfs_vers = txdr_unsigned(NFS_VER2);
49638420Smckusick 	nfs_prog = txdr_unsigned(NFS_PROG);
49738420Smckusick 	nfs_true = txdr_unsigned(TRUE);
49838420Smckusick 	nfs_false = txdr_unsigned(FALSE);
49938420Smckusick 	/* Loop thru nfs procids */
50038420Smckusick 	for (i = 0; i < NFS_NPROCS; i++)
50138420Smckusick 		nfs_procids[i] = txdr_unsigned(i);
50239345Smckusick 	/* Ensure async daemons disabled */
50339345Smckusick 	for (i = 0; i < MAX_ASYNCDAEMON; i++)
50439345Smckusick 		nfs_iodwant[i] = (struct proc *)0;
50538420Smckusick 	v_type[0] = VNON;
50638420Smckusick 	v_type[1] = VREG;
50738420Smckusick 	v_type[2] = VDIR;
50838420Smckusick 	v_type[3] = VBLK;
50938420Smckusick 	v_type[4] = VCHR;
51038420Smckusick 	v_type[5] = VLNK;
51138420Smckusick 	nfs_xdrneg1 = txdr_unsigned(-1);
51238420Smckusick 	nfs_nhinit();			/* Init the nfsnode table */
51339755Smckusick 	nfsrv_initcache();		/* Init the server request cache */
51438884Smacklem 	rminit(nfsmap, (long)NFS_MAPREG, (long)1, "nfs mapreg", NFS_MSIZ);
51538420Smckusick 	/* And start timer */
51638420Smckusick 	nfs_timer();
51738420Smckusick }
51838420Smckusick 
51938420Smckusick /*
52038420Smckusick  * Fill in the rest of the rpc_unixauth and return it
52138420Smckusick  */
52238420Smckusick static char *nfs_unixauth(cr)
52338420Smckusick 	register struct ucred *cr;
52438420Smckusick {
52538420Smckusick 	register u_long *p;
52638420Smckusick 	register int i;
52738420Smckusick 	int ngr;
52838420Smckusick 
52938420Smckusick 	/* Maybe someday there should be a cache of AUTH_SHORT's */
53038420Smckusick 	if ((p = rpc_uidp) == NULL) {
53138425Smckusick #ifdef FILLINHOST
53238420Smckusick 		i = nfsm_rndup(hostnamelen)+(19*NFSX_UNSIGNED);
53338425Smckusick #else
53438425Smckusick 		i = 19*NFSX_UNSIGNED;
53538425Smckusick #endif
53638420Smckusick 		MALLOC(p, u_long *, i, M_TEMP, M_WAITOK);
53738420Smckusick 		bzero((caddr_t)p, i);
53838420Smckusick 		rpc_unixauth = (caddr_t)p;
53938420Smckusick 		*p++ = txdr_unsigned(RPCAUTH_UNIX);
54038420Smckusick 		p++;	/* Fill in size later */
54138420Smckusick 		*p++ = hostid;
54238425Smckusick #ifdef FILLINHOST
54338420Smckusick 		*p++ = txdr_unsigned(hostnamelen);
54438420Smckusick 		i = nfsm_rndup(hostnamelen);
54538420Smckusick 		bcopy(hostname, (caddr_t)p, hostnamelen);
54638420Smckusick 		p += (i>>2);
54738425Smckusick #else
54838425Smckusick 		*p++ = 0;
54938425Smckusick #endif
55038420Smckusick 		rpc_uidp = p;
55138420Smckusick 	}
55238420Smckusick 	*p++ = txdr_unsigned(cr->cr_uid);
55338420Smckusick 	*p++ = txdr_unsigned(cr->cr_groups[0]);
55438737Smckusick 	ngr = (cr->cr_ngroups > numgrps) ? numgrps : cr->cr_ngroups;
55538420Smckusick 	*p++ = txdr_unsigned(ngr);
55638420Smckusick 	for (i = 0; i < ngr; i++)
55738420Smckusick 		*p++ = txdr_unsigned(cr->cr_groups[i]);
55838420Smckusick 	/* And add the AUTH_NULL */
55938420Smckusick 	*p++ = 0;
56038420Smckusick 	*p = 0;
56138420Smckusick 	i = (((caddr_t)p)-rpc_unixauth)-12;
56238420Smckusick 	p = (u_long *)(rpc_unixauth+4);
56338420Smckusick 	*p = txdr_unsigned(i);
56438420Smckusick 	return(rpc_unixauth);
56538420Smckusick }
56638420Smckusick 
56738420Smckusick /*
56838420Smckusick  * Attribute cache routines.
56938420Smckusick  * nfs_loadattrcache() - loads or updates the cache contents from attributes
57038420Smckusick  *	that are on the mbuf list
57138420Smckusick  * nfs_getattrcache() - returns valid attributes if found in cache, returns
57238420Smckusick  *	error otherwise
57338420Smckusick  */
57438420Smckusick 
57538420Smckusick /*
57639444Smckusick  * Load the attribute cache (that lives in the nfsnode entry) with
57738420Smckusick  * the values on the mbuf list and
57838420Smckusick  * Iff vap not NULL
57938420Smckusick  *    copy the attributes to *vaper
58038420Smckusick  */
58139457Smckusick nfs_loadattrcache(vpp, mdp, dposp, vaper)
58239457Smckusick 	struct vnode **vpp;
58338420Smckusick 	struct mbuf **mdp;
58438420Smckusick 	caddr_t *dposp;
58538420Smckusick 	struct vattr *vaper;
58638420Smckusick {
58739457Smckusick 	register struct vnode *vp = *vpp;
58838420Smckusick 	register struct vattr *vap;
58938884Smacklem 	register struct nfsv2_fattr *fp;
59039444Smckusick 	extern struct vnodeops spec_nfsv2nodeops;
59139457Smckusick 	register struct nfsnode *np;
59239494Smckusick 	register long t1;
59339494Smckusick 	caddr_t dpos, cp2;
59439494Smckusick 	int error = 0;
59539494Smckusick 	struct mbuf *md;
59639444Smckusick 	enum vtype type;
59739444Smckusick 	dev_t rdev;
59839444Smckusick 	struct timeval mtime;
59939444Smckusick 	struct vnode *nvp;
60038420Smckusick 
60138420Smckusick 	md = *mdp;
60238420Smckusick 	dpos = *dposp;
60338420Smckusick 	t1 = (mtod(md, caddr_t)+md->m_len)-dpos;
60438420Smckusick 	if (error = nfsm_disct(&md, &dpos, NFSX_FATTR, t1, TRUE, &cp2))
60538420Smckusick 		return (error);
60638884Smacklem 	fp = (struct nfsv2_fattr *)cp2;
60739444Smckusick 	type = nfstov_type(fp->fa_type);
60839444Smckusick 	rdev = fxdr_unsigned(dev_t, fp->fa_rdev);
60939444Smckusick 	fxdr_time(&fp->fa_mtime, &mtime);
61039444Smckusick 	/*
61139444Smckusick 	 * If v_type == VNON it is a new node, so fill in the v_type,
61239444Smckusick 	 * n_mtime fields. Check to see if it represents a special
61339444Smckusick 	 * device, and if so, check for a possible alias. Once the
61439444Smckusick 	 * correct vnode has been obtained, fill in the rest of the
61539444Smckusick 	 * information.
61639444Smckusick 	 */
61738420Smckusick 	np = VTONFS(vp);
61839444Smckusick 	if (vp->v_type == VNON) {
61939444Smckusick 		vp->v_type = type;
62039444Smckusick 		if (vp->v_type == VCHR || vp->v_type == VBLK) {
62139444Smckusick 			vp->v_op = &spec_nfsv2nodeops;
62239618Smckusick 			if (nvp = checkalias(vp, 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);
63939457Smckusick 				*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;
65338420Smckusick 	vap->va_size1 = 0;		/* OR -1 ?? */
65438884Smacklem 	vap->va_blocksize = fxdr_unsigned(long, fp->fa_blocksize);
65539444Smckusick 	vap->va_rdev = rdev;
65638884Smacklem 	vap->va_bytes = fxdr_unsigned(long, fp->fa_blocks) * vap->va_blocksize;
65738884Smacklem 	vap->va_bytes1 = 0;
65839755Smckusick 	vap->va_fsid = vp->v_mount->m_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 /*
70538420Smckusick  * nfs_namei - a liitle like namei(), but for one element only
70638420Smckusick  *	essentially look up file handle, fill in ndp and call VOP_LOOKUP()
70738420Smckusick  */
70838420Smckusick nfs_namei(ndp, fhp, len, mdp, dposp)
70938420Smckusick 	register struct nameidata *ndp;
71038420Smckusick 	fhandle_t *fhp;
71138420Smckusick 	int len;
71238420Smckusick 	struct mbuf **mdp;
71338420Smckusick 	caddr_t *dposp;
71438420Smckusick {
71538420Smckusick 	register int i, rem;
71638420Smckusick 	register struct mbuf *md;
71738420Smckusick 	register char *cp;
71838420Smckusick 	struct vnode *dp = (struct vnode *)0;
71938420Smckusick 	int flag;
72038420Smckusick 	int docache;
72138420Smckusick 	int wantparent;
72238420Smckusick 	int lockparent;
72338420Smckusick 	int error = 0;
72438420Smckusick 
72538420Smckusick 	ndp->ni_vp = ndp->ni_dvp = (struct vnode *)0;
72638420Smckusick 	flag = ndp->ni_nameiop & OPFLAG;
72738420Smckusick 	wantparent = ndp->ni_nameiop & (LOCKPARENT | WANTPARENT);
72838420Smckusick 	lockparent = ndp->ni_nameiop & LOCKPARENT;
72938420Smckusick 	docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE;
73038420Smckusick 	if (flag == DELETE || wantparent)
73138420Smckusick 		docache = 0;
73238420Smckusick 
73338420Smckusick 	/* Fill in the nameidata and call lookup */
73438420Smckusick 	cp = *dposp;
73538420Smckusick 	md = *mdp;
73638420Smckusick 	rem = mtod(md, caddr_t)+md->m_len-cp;
73738420Smckusick 	ndp->ni_hash = 0;
73838420Smckusick 	for (i = 0; i < len;) {
73938420Smckusick 		if (rem == 0) {
74038420Smckusick 			md = md->m_next;
74138420Smckusick 			if (md == NULL)
74238420Smckusick 				return (EBADRPC);
74338420Smckusick 			cp = mtod(md, caddr_t);
74438420Smckusick 			rem = md->m_len;
74538420Smckusick 		}
74638420Smckusick 		if (*cp == '\0' || *cp == '/')
74738420Smckusick 			return (EINVAL);
74838420Smckusick 		if (*cp & 0200)
74938420Smckusick 			if ((*cp&0377) == ('/'|0200) || flag != DELETE)
75038420Smckusick 				return (EINVAL);
75138420Smckusick 		ndp->ni_dent.d_name[i++] = *cp;
75238420Smckusick 		ndp->ni_hash += (unsigned char)*cp * i;
75338420Smckusick 		cp++;
75438420Smckusick 		rem--;
75538420Smckusick 	}
75638420Smckusick 	*mdp = md;
75738420Smckusick 	len = nfsm_rndup(len)-len;
75838420Smckusick 	if (len > 0)
75938420Smckusick 		*dposp = cp+len;
76038420Smckusick 	else
76138420Smckusick 		*dposp = cp;
76238420Smckusick 	ndp->ni_namelen = i;
76338420Smckusick 	ndp->ni_dent.d_namlen = i;
76438420Smckusick 	ndp->ni_dent.d_name[i] = '\0';
76538425Smckusick 	ndp->ni_pathlen = 1;
76638420Smckusick 	ndp->ni_dirp = ndp->ni_ptr = &ndp->ni_dent.d_name[0];
76738420Smckusick 	ndp->ni_next = &ndp->ni_dent.d_name[i];
76838420Smckusick 	ndp->ni_loopcnt = 0;	/* Not actually used for now */
76938420Smckusick 	ndp->ni_endoff = 0;
77038420Smckusick 	if (docache)
77138420Smckusick 		ndp->ni_makeentry = 1;
77238420Smckusick 	else
77338420Smckusick 		ndp->ni_makeentry = 0;
77438420Smckusick 	ndp->ni_isdotdot = (i == 2 &&
77538420Smckusick 		ndp->ni_dent.d_name[1] == '.' && ndp->ni_dent.d_name[0] == '.');
77638420Smckusick 
77738420Smckusick 	if (error = nfsrv_fhtovp(fhp, TRUE, &dp, ndp->ni_cred))
77838420Smckusick 		return (error);
77938425Smckusick 	if (dp->v_type != VDIR) {
78038425Smckusick 		vput(dp);
78138425Smckusick 		return (ENOTDIR);
78238425Smckusick 	}
78339345Smckusick 	/*
78439345Smckusick 	 * Must set current directory here to avoid confusion in namei()
78539345Smckusick 	 * called from rename()
78639345Smckusick 	 */
78738425Smckusick 	ndp->ni_cdir = dp;
78838425Smckusick 	ndp->ni_rdir = (struct vnode *)0;
78938420Smckusick 
79038420Smckusick 	/*
79138425Smckusick 	 * Handle "..":
79238425Smckusick 	 * If this vnode is the root of the mounted
79338425Smckusick 	 *    file system, then ignore it so can't get out
79438420Smckusick 	 */
79538425Smckusick 	if (ndp->ni_isdotdot && (dp->v_flag & VROOT)) {
79638425Smckusick 		ndp->ni_dvp = dp;
79738425Smckusick 		ndp->ni_vp = dp;
79838425Smckusick 		VREF(dp);
79938425Smckusick 		goto nextname;
80038420Smckusick 	}
80138420Smckusick 
80238420Smckusick 	/*
80338420Smckusick 	 * We now have a segment name to search for, and a directory to search.
80438420Smckusick 	 */
80538420Smckusick 	if (error = VOP_LOOKUP(dp, ndp)) {
80638420Smckusick 		if (ndp->ni_vp != NULL)
80738420Smckusick 			panic("leaf should be empty");
80838420Smckusick 		/*
80938420Smckusick 		 * If creating and at end of pathname, then can consider
81038420Smckusick 		 * allowing file to be created.
81138420Smckusick 		 */
81238420Smckusick 		if (ndp->ni_dvp->v_mount->m_flag & (M_RDONLY | M_EXRDONLY))
81338420Smckusick 			error = EROFS;
81438420Smckusick 		if (flag == LOOKUP || flag == DELETE || error != ENOENT)
81538420Smckusick 			goto bad;
81638420Smckusick 		/*
81738420Smckusick 		 * We return with ni_vp NULL to indicate that the entry
81838420Smckusick 		 * doesn't currently exist, leaving a pointer to the
81938420Smckusick 		 * (possibly locked) directory inode in ndp->ni_dvp.
82038420Smckusick 		 */
82138420Smckusick 		return (0);	/* should this be ENOENT? */
82238420Smckusick 	}
82338420Smckusick 
82438420Smckusick 	dp = ndp->ni_vp;
82538420Smckusick 
82638420Smckusick nextname:
82738425Smckusick 	ndp->ni_ptr = ndp->ni_next;
82838420Smckusick 	/*
82938425Smckusick 	 * Check for read-only file systems
83038420Smckusick 	 */
83138421Smckusick 	if (flag == DELETE || flag == RENAME) {
83238421Smckusick 		/*
83338421Smckusick 		 * Disallow directory write attempts on read-only
83438421Smckusick 		 * file systems.
83538421Smckusick 		 */
83638421Smckusick 		if ((dp->v_mount->m_flag & (M_RDONLY|M_EXRDONLY)) ||
83738425Smckusick 		    (wantparent && (ndp->ni_dvp->v_mount->m_flag & (M_RDONLY|M_EXRDONLY)))) {
83838421Smckusick 			error = EROFS;
83938421Smckusick 			goto bad2;
84038421Smckusick 		}
84138421Smckusick 	}
84238420Smckusick 
84338420Smckusick 	if (!wantparent)
84438420Smckusick 		vrele(ndp->ni_dvp);
84538420Smckusick 
84638420Smckusick 	if ((ndp->ni_nameiop & LOCKLEAF) == 0)
84738420Smckusick 		VOP_UNLOCK(dp);
84838420Smckusick 	return (0);
84938420Smckusick 
85038420Smckusick bad2:
85138420Smckusick 	if (lockparent)
85238420Smckusick 		VOP_UNLOCK(ndp->ni_dvp);
85338420Smckusick 	vrele(ndp->ni_dvp);
85438420Smckusick bad:
85538420Smckusick 	vput(dp);
85638420Smckusick 	ndp->ni_vp = NULL;
85738420Smckusick 	return (error);
85838420Smckusick }
85938420Smckusick 
86038420Smckusick /*
86138420Smckusick  * A fiddled version of m_adj() that ensures null fill to a long
86238420Smckusick  * boundary and only trims off the back end
86338420Smckusick  */
86438420Smckusick nfsm_adj(mp, len, nul)
86538420Smckusick 	struct mbuf *mp;
86638420Smckusick 	register int len;
86738420Smckusick 	int nul;
86838420Smckusick {
86938420Smckusick 	register struct mbuf *m;
87038420Smckusick 	register int count, i;
87138420Smckusick 	register char *cp;
87238420Smckusick 
87338420Smckusick 	/*
87438420Smckusick 	 * Trim from tail.  Scan the mbuf chain,
87538420Smckusick 	 * calculating its length and finding the last mbuf.
87638420Smckusick 	 * If the adjustment only affects this mbuf, then just
87738420Smckusick 	 * adjust and return.  Otherwise, rescan and truncate
87838420Smckusick 	 * after the remaining size.
87938420Smckusick 	 */
88038420Smckusick 	count = 0;
88138420Smckusick 	m = mp;
88238420Smckusick 	for (;;) {
88338420Smckusick 		count += m->m_len;
88438420Smckusick 		if (m->m_next == (struct mbuf *)0)
88538420Smckusick 			break;
88638420Smckusick 		m = m->m_next;
88738420Smckusick 	}
88838579Smckusick 	if (m->m_len > len) {
88938420Smckusick 		m->m_len -= len;
89038420Smckusick 		if (nul > 0) {
89138420Smckusick 			cp = mtod(m, caddr_t)+m->m_len-nul;
89238420Smckusick 			for (i = 0; i < nul; i++)
89338420Smckusick 				*cp++ = '\0';
89438420Smckusick 		}
89538420Smckusick 		return;
89638420Smckusick 	}
89738420Smckusick 	count -= len;
89838420Smckusick 	if (count < 0)
89938420Smckusick 		count = 0;
90038420Smckusick 	/*
90138420Smckusick 	 * Correct length for chain is "count".
90238420Smckusick 	 * Find the mbuf with last data, adjust its length,
90338420Smckusick 	 * and toss data from remaining mbufs on chain.
90438420Smckusick 	 */
90538420Smckusick 	for (m = mp; m; m = m->m_next) {
90638420Smckusick 		if (m->m_len >= count) {
90738420Smckusick 			m->m_len = count;
90838420Smckusick 			if (nul > 0) {
90938420Smckusick 				cp = mtod(m, caddr_t)+m->m_len-nul;
91038420Smckusick 				for (i = 0; i < nul; i++)
91138420Smckusick 					*cp++ = '\0';
91238420Smckusick 			}
91338420Smckusick 			break;
91438420Smckusick 		}
91538420Smckusick 		count -= m->m_len;
91638420Smckusick 	}
91738420Smckusick 	while (m = m->m_next)
91838420Smckusick 		m->m_len = 0;
91938420Smckusick }
92038420Smckusick 
92138420Smckusick /*
92238420Smckusick  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
92338420Smckusick  * 	- look up fsid in mount list (if not found ret error)
92438420Smckusick  *	- check that it is exported
92538420Smckusick  *	- get vp by calling VFS_FHTOVP() macro
92638420Smckusick  *	- if not lockflag unlock it with VOP_UNLOCK()
92738420Smckusick  *	- if cred->cr_uid == 0 set it to m_exroot
92838420Smckusick  */
92938420Smckusick nfsrv_fhtovp(fhp, lockflag, vpp, cred)
93038420Smckusick 	fhandle_t *fhp;
93138420Smckusick 	int lockflag;
93238420Smckusick 	struct vnode **vpp;
93338420Smckusick 	struct ucred *cred;
93438420Smckusick {
93538420Smckusick 	register struct mount *mp;
93638420Smckusick 
93738420Smckusick 	if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
93838420Smckusick 		return (ESTALE);
93938420Smckusick 	if ((mp->m_flag & M_EXPORTED) == 0)
94038420Smckusick 		return (EACCES);
94138420Smckusick 	if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
94238420Smckusick 		return (ESTALE);
94338420Smckusick 	if (cred->cr_uid == 0)
94438420Smckusick 		cred->cr_uid = mp->m_exroot;
94538420Smckusick 	if (!lockflag)
94638420Smckusick 		VOP_UNLOCK(*vpp);
94738420Smckusick 	return (0);
94838420Smckusick }
949