xref: /csrg-svn/sys/nfs/nfs_subs.c (revision 41398)
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*41398Smckusick  *	@(#)nfs_subs.c	7.22 (Berkeley) 05/04/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"
3140136Smckusick #include "systm.h"
3240136Smckusick #include "kernel.h"
3338420Smckusick #include "mount.h"
3438420Smckusick #include "file.h"
3538420Smckusick #include "vnode.h"
3640136Smckusick #include "mbuf.h"
3740136Smckusick #include "errno.h"
3838884Smacklem #include "map.h"
3938420Smckusick #include "rpcv2.h"
4038420Smckusick #include "nfsv2.h"
4138420Smckusick #include "nfsnode.h"
4238420Smckusick #include "nfs.h"
4338884Smacklem #include "nfsiom.h"
4438420Smckusick #include "xdr_subs.h"
4538420Smckusick #include "nfsm_subs.h"
4638420Smckusick 
4738420Smckusick #define TRUE	1
4838420Smckusick #define	FALSE	0
4938420Smckusick 
5038420Smckusick /*
5138420Smckusick  * Data items converted to xdr at startup, since they are constant
5238420Smckusick  * This is kinda hokey, but may save a little time doing byte swaps
5338420Smckusick  */
5438420Smckusick u_long nfs_procids[NFS_NPROCS];
5538420Smckusick u_long nfs_xdrneg1;
5638420Smckusick u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied,
5738420Smckusick 	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
5838420Smckusick u_long nfs_vers, nfs_prog, nfs_true, nfs_false;
5938420Smckusick /* And other global data */
6038420Smckusick static u_long *rpc_uidp = (u_long *)0;
6138420Smckusick static u_long nfs_xid = 1;
6238420Smckusick static char *rpc_unixauth;
6338420Smckusick extern long hostid;
6438420Smckusick extern enum vtype v_type[NFLNK+1];
6539345Smckusick extern struct proc *nfs_iodwant[MAX_ASYNCDAEMON];
6638884Smacklem extern struct map nfsmap[NFS_MSIZ];
6738420Smckusick 
6838420Smckusick /* Function ret types */
6938420Smckusick static char *nfs_unixauth();
7038420Smckusick 
7138420Smckusick /*
7238737Smckusick  * Maximum number of groups passed through to NFS server.
7338737Smckusick  * For release 3.X systems, the maximum value is 8.
7438737Smckusick  * For release 4.X systems, the maximum value is 10.
7538737Smckusick  */
7638737Smckusick int numgrps = 8;
7738737Smckusick 
7838737Smckusick /*
7938420Smckusick  * Create the header for an rpc request packet
8038420Smckusick  * The function nfs_unixauth() creates a unix style authorization string
8138420Smckusick  * and returns a ptr to it.
8238420Smckusick  * The hsiz is the size of the rest of the nfs request header.
8338420Smckusick  * (just used to decide if a cluster is a good idea)
8439494Smckusick  * nb: Note that the prog, vers and procid args are already in xdr byte order
8538420Smckusick  */
8639494Smckusick struct mbuf *nfsm_reqh(prog, vers, procid, cred, hsiz, bpos, mb, retxid)
8738420Smckusick 	u_long prog;
8838420Smckusick 	u_long vers;
8939494Smckusick 	u_long procid;
9038420Smckusick 	struct ucred *cred;
9138420Smckusick 	int hsiz;
9238420Smckusick 	caddr_t *bpos;
9338420Smckusick 	struct mbuf **mb;
9438420Smckusick 	u_long *retxid;
9538420Smckusick {
9638420Smckusick 	register struct mbuf *mreq, *m;
9738420Smckusick 	register u_long *p;
9838420Smckusick 	struct mbuf *m1;
9938420Smckusick 	char *ap;
10038420Smckusick 	int asiz, siz;
10138420Smckusick 
10238420Smckusick 	NFSMGETHDR(mreq);
10338737Smckusick 	asiz = (((cred->cr_ngroups > numgrps) ? numgrps : cred->cr_ngroups)<<2);
10438425Smckusick #ifdef FILLINHOST
10538420Smckusick 	asiz += nfsm_rndup(hostnamelen)+(9*NFSX_UNSIGNED);
10638425Smckusick #else
10738425Smckusick 	asiz += 9*NFSX_UNSIGNED;
10838425Smckusick #endif
10938420Smckusick 
11038420Smckusick 	/* If we need a lot, alloc a cluster ?? */
11138420Smckusick 	if ((asiz+hsiz+RPC_SIZ) > MHLEN)
11238420Smckusick 		NFSMCLGET(mreq, M_WAIT);
11338420Smckusick 	mreq->m_len = NFSMSIZ(mreq);
11438420Smckusick 	siz = mreq->m_len;
11538420Smckusick 	m1 = mreq;
11638420Smckusick 	/*
11738420Smckusick 	 * Alloc enough mbufs
11838420Smckusick 	 * We do it now to avoid all sleeps after the call to nfs_unixauth()
11938420Smckusick 	 */
12038420Smckusick 	while ((asiz+RPC_SIZ) > siz) {
12138420Smckusick 		MGET(m, M_WAIT, MT_DATA);
12238420Smckusick 		m1->m_next = m;
12338420Smckusick 		m->m_len = MLEN;
12438420Smckusick 		siz += MLEN;
12538420Smckusick 		m1 = m;
12638420Smckusick 	}
12738420Smckusick 	p = mtod(mreq, u_long *);
12838420Smckusick 	*p++ = *retxid = txdr_unsigned(++nfs_xid);
12938420Smckusick 	*p++ = rpc_call;
13038420Smckusick 	*p++ = rpc_vers;
13138420Smckusick 	*p++ = prog;
13238420Smckusick 	*p++ = vers;
13339494Smckusick 	*p++ = procid;
13438420Smckusick 
13538420Smckusick 	/* Now we can call nfs_unixauth() and copy it in */
13638420Smckusick 	ap = nfs_unixauth(cred);
13738420Smckusick 	m = mreq;
13838420Smckusick 	siz = m->m_len-RPC_SIZ;
13938420Smckusick 	if (asiz <= siz) {
14038420Smckusick 		bcopy(ap, (caddr_t)p, asiz);
14138420Smckusick 		m->m_len = asiz+RPC_SIZ;
14238420Smckusick 	} else {
14338420Smckusick 		bcopy(ap, (caddr_t)p, siz);
14438420Smckusick 		ap += siz;
14538420Smckusick 		asiz -= siz;
14638420Smckusick 		while (asiz > 0) {
14738420Smckusick 			siz = (asiz > MLEN) ? MLEN : asiz;
14838420Smckusick 			m = m->m_next;
14938420Smckusick 			bcopy(ap, mtod(m, caddr_t), siz);
15038420Smckusick 			m->m_len = siz;
15138420Smckusick 			asiz -= siz;
15238420Smckusick 			ap += siz;
15338420Smckusick 		}
15438420Smckusick 	}
15538420Smckusick 
15638420Smckusick 	/* Finally, return values */
15738420Smckusick 	*mb = m;
15838420Smckusick 	*bpos = mtod(m, caddr_t)+m->m_len;
15938420Smckusick 	return (mreq);
16038420Smckusick }
16138420Smckusick 
16238420Smckusick /*
16338420Smckusick  * copies mbuf chain to the uio scatter/gather list
16438420Smckusick  */
16538420Smckusick nfsm_mbuftouio(mrep, uiop, siz, dpos)
16638420Smckusick 	struct mbuf **mrep;
16738420Smckusick 	struct uio *uiop;
16838420Smckusick 	int siz;
16938420Smckusick 	caddr_t *dpos;
17038420Smckusick {
17138420Smckusick 	register int xfer, left, len;
17238420Smckusick 	register struct mbuf *mp;
17338420Smckusick 	register char *mbufcp, *uiocp;
17438420Smckusick 	long uiosiz, rem;
17538420Smckusick 
17638420Smckusick 	mp = *mrep;
17738420Smckusick 	mbufcp = *dpos;
17838420Smckusick 	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
17938420Smckusick 	rem = nfsm_rndup(siz)-siz;
18038420Smckusick 	while (siz > 0) {
18138420Smckusick 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
18238420Smckusick 			return(EFBIG);
18338420Smckusick 		left = uiop->uio_iov->iov_len;
18438420Smckusick 		uiocp = uiop->uio_iov->iov_base;
18538420Smckusick 		if (left > siz)
18638420Smckusick 			left = siz;
18738420Smckusick 		uiosiz = left;
18838420Smckusick 		while (left > 0) {
18938420Smckusick 			while (len == 0) {
19038420Smckusick 				mp = mp->m_next;
19138420Smckusick 				if (mp == NULL)
19238420Smckusick 					return (EBADRPC);
19338420Smckusick 				mbufcp = mtod(mp, caddr_t);
19438420Smckusick 				len = mp->m_len;
19538420Smckusick 			}
19638420Smckusick 			xfer = (left > len) ? len : left;
19738420Smckusick #ifdef notdef
19838420Smckusick 			/* Not Yet.. */
19938420Smckusick 			if (uiop->uio_iov->iov_op != NULL)
20038420Smckusick 				(*(uiop->uio_iov->iov_op))
20138420Smckusick 				(mbufcp, uiocp, xfer);
20238420Smckusick 			else
20338420Smckusick #endif
20438420Smckusick 			if (uiop->uio_segflg == UIO_SYSSPACE)
20538420Smckusick 				bcopy(mbufcp, uiocp, xfer);
20638420Smckusick 			else
20738420Smckusick 				copyout(mbufcp, uiocp, xfer);
20838420Smckusick 			left -= xfer;
20938420Smckusick 			len -= xfer;
21038420Smckusick 			mbufcp += xfer;
21138420Smckusick 			uiocp += xfer;
21239585Smckusick 			uiop->uio_offset += xfer;
21338420Smckusick 			uiop->uio_resid -= xfer;
21438420Smckusick 		}
21538420Smckusick 		if (uiop->uio_iov->iov_len <= siz) {
21638420Smckusick 			uiop->uio_iovcnt--;
21738420Smckusick 			uiop->uio_iov++;
21838420Smckusick 		} else {
21938420Smckusick 			uiop->uio_iov->iov_base += uiosiz;
22038420Smckusick 			uiop->uio_iov->iov_len -= uiosiz;
22138420Smckusick 		}
22238420Smckusick 		siz -= uiosiz;
22338420Smckusick 	}
22438420Smckusick 	if (rem > 0)
22538420Smckusick 		mbufcp += rem;
22638420Smckusick 	*dpos = mbufcp;
22738420Smckusick 	*mrep = mp;
22838420Smckusick 	return(0);
22938420Smckusick }
23038420Smckusick 
23138420Smckusick /*
23238420Smckusick  * copies a uio scatter/gather list to an mbuf chain...
23338420Smckusick  */
23438420Smckusick nfsm_uiotombuf(uiop, mq, siz, bpos)
23538420Smckusick 	register struct uio *uiop;
23638420Smckusick 	struct mbuf **mq;
23738420Smckusick 	int siz;
23838420Smckusick 	caddr_t *bpos;
23938420Smckusick {
24038420Smckusick 	register struct mbuf *mp;
24138420Smckusick 	struct mbuf *mp2;
24239494Smckusick 	long xfer, left, uiosiz;
24338420Smckusick 	int clflg;
24438420Smckusick 	int rem, len;
24538420Smckusick 	char *cp, *uiocp;
24638420Smckusick 
24738420Smckusick 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
24838420Smckusick 		clflg = 1;
24938420Smckusick 	else
25038420Smckusick 		clflg = 0;
25138420Smckusick 	rem = nfsm_rndup(siz)-siz;
25238420Smckusick 	mp2 = *mq;
25338420Smckusick 	while (siz > 0) {
25438420Smckusick 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
25538420Smckusick 			return(EINVAL);
25638420Smckusick 		left = uiop->uio_iov->iov_len;
25738420Smckusick 		uiocp = uiop->uio_iov->iov_base;
25838420Smckusick 		if (left > siz)
25938420Smckusick 			left = siz;
26038420Smckusick 		uiosiz = left;
26138420Smckusick 		while (left > 0) {
26238420Smckusick 			MGET(mp, M_WAIT, MT_DATA);
26338420Smckusick 			if (clflg)
26438420Smckusick 				NFSMCLGET(mp, M_WAIT);
26538420Smckusick 			mp->m_len = NFSMSIZ(mp);
26638420Smckusick 			mp2->m_next = mp;
26738420Smckusick 			mp2 = mp;
26838420Smckusick 			xfer = (left > mp->m_len) ? mp->m_len : left;
26938420Smckusick #ifdef notdef
27038420Smckusick 			/* Not Yet.. */
27138420Smckusick 			if (uiop->uio_iov->iov_op != NULL)
27238420Smckusick 				(*(uiop->uio_iov->iov_op))
27338420Smckusick 				(uiocp, mtod(mp, caddr_t), xfer);
27438420Smckusick 			else
27538420Smckusick #endif
27638420Smckusick 			if (uiop->uio_segflg == UIO_SYSSPACE)
27738420Smckusick 				bcopy(uiocp, mtod(mp, caddr_t), xfer);
27838420Smckusick 			else
27938420Smckusick 				copyin(uiocp, mtod(mp, caddr_t), xfer);
28038420Smckusick 			len = mp->m_len;
28138420Smckusick 			mp->m_len = xfer;
28238420Smckusick 			left -= xfer;
28338420Smckusick 			uiocp += xfer;
28439585Smckusick 			uiop->uio_offset += xfer;
28538420Smckusick 			uiop->uio_resid -= xfer;
28638420Smckusick 		}
28738420Smckusick 		if (uiop->uio_iov->iov_len <= siz) {
28838420Smckusick 			uiop->uio_iovcnt--;
28938420Smckusick 			uiop->uio_iov++;
29038420Smckusick 		} else {
29138420Smckusick 			uiop->uio_iov->iov_base += uiosiz;
29238420Smckusick 			uiop->uio_iov->iov_len -= uiosiz;
29338420Smckusick 		}
29438420Smckusick 		siz -= uiosiz;
29538420Smckusick 	}
29638420Smckusick 	if (rem > 0) {
29738420Smckusick 		if (rem > (len-mp->m_len)) {
29838420Smckusick 			MGET(mp, M_WAIT, MT_DATA);
29938420Smckusick 			mp->m_len = 0;
30038420Smckusick 			mp2->m_next = mp;
30138420Smckusick 		}
30238420Smckusick 		cp = mtod(mp, caddr_t)+mp->m_len;
30338420Smckusick 		for (left = 0; left < rem; left++)
30438420Smckusick 			*cp++ = '\0';
30538420Smckusick 		mp->m_len += rem;
30638420Smckusick 		*bpos = cp;
30738420Smckusick 	} else
30838420Smckusick 		*bpos = mtod(mp, caddr_t)+mp->m_len;
30938420Smckusick 	*mq = mp;
31038420Smckusick 	return(0);
31138420Smckusick }
31238420Smckusick 
31338420Smckusick /*
31438420Smckusick  * Help break down an mbuf chain by setting the first siz bytes contiguous
31538420Smckusick  * pointed to by returned val.
31638420Smckusick  * If Updateflg == True we can overwrite the first part of the mbuf data
31738420Smckusick  * This is used by the macros nfsm_disect and nfsm_disecton for tough
31838420Smckusick  * cases. (The macros use the vars. dpos and dpos2)
31938420Smckusick  */
32038420Smckusick nfsm_disct(mdp, dposp, siz, left, updateflg, cp2)
32138420Smckusick 	struct mbuf **mdp;
32238420Smckusick 	caddr_t *dposp;
32338420Smckusick 	int siz;
32438420Smckusick 	int left;
32538420Smckusick 	int updateflg;
32638420Smckusick 	caddr_t *cp2;
32738420Smckusick {
32838420Smckusick 	register struct mbuf *mp, *mp2;
32938420Smckusick 	register int siz2, xfer;
33038420Smckusick 	register caddr_t p;
33138420Smckusick 
33238420Smckusick 	mp = *mdp;
33338420Smckusick 	while (left == 0) {
33438420Smckusick 		*mdp = mp = mp->m_next;
33538420Smckusick 		if (mp == NULL)
33638420Smckusick 			return(EBADRPC);
33738420Smckusick 		left = mp->m_len;
33838420Smckusick 		*dposp = mtod(mp, caddr_t);
33938420Smckusick 	}
34038420Smckusick 	if (left >= siz) {
34138420Smckusick 		*cp2 = *dposp;
34238420Smckusick 		*dposp += siz;
34338420Smckusick 		return(0);
34438420Smckusick 	} else if (mp->m_next == NULL) {
34538420Smckusick 		return(EBADRPC);
34638420Smckusick 	} else if (siz > MCLBYTES) {
34738420Smckusick 		panic("nfs S too big");
34838420Smckusick 	} else {
34938420Smckusick 		/* Iff update, you can overwrite, else must alloc new mbuf */
35038420Smckusick 		if (updateflg) {
35138420Smckusick 			NFSMINOFF(mp);
35238420Smckusick 		} else {
35338420Smckusick 			MGET(mp2, M_WAIT, MT_DATA);
35438420Smckusick 			mp2->m_next = mp->m_next;
35538420Smckusick 			mp->m_next = mp2;
35638420Smckusick 			mp->m_len -= left;
35738420Smckusick 			mp = mp2;
35838420Smckusick 		}
35938420Smckusick 		/* Alloc cluster iff we need it */
36038420Smckusick 		if (!M_HASCL(mp) && siz > NFSMSIZ(mp)) {
36138420Smckusick 			NFSMCLGET(mp, M_WAIT);
36238420Smckusick 			if (!M_HASCL(mp))
36338420Smckusick 				return(ENOBUFS);
36438420Smckusick 		}
36538420Smckusick 		*cp2 = p = mtod(mp, caddr_t);
36638420Smckusick 		bcopy(*dposp, p, left);		/* Copy what was left */
36738420Smckusick 		siz2 = siz-left;
36838420Smckusick 		p += left;
36938420Smckusick 		mp2 = mp->m_next;
37038420Smckusick 		/* Loop arround copying up the siz2 bytes */
37138420Smckusick 		while (siz2 > 0) {
37238420Smckusick 			if (mp2 == NULL)
37338420Smckusick 				return (EBADRPC);
37438420Smckusick 			xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
37538420Smckusick 			bcopy(mtod(mp2, caddr_t), p, xfer);
37638420Smckusick 			NFSMADV(mp2, xfer);
37738420Smckusick 			mp2->m_len -= xfer;
37838420Smckusick 			siz2 -= xfer;
37938420Smckusick 			if (siz2 > 0)
38038420Smckusick 				mp2 = mp2->m_next;
38138420Smckusick 		}
38238420Smckusick 		mp->m_len = siz;
38338420Smckusick 		*mdp = mp2;
38438420Smckusick 		*dposp = mtod(mp2, caddr_t);
38538420Smckusick 	}
38639494Smckusick 	return (0);
38738420Smckusick }
38838420Smckusick 
38938420Smckusick /*
39038420Smckusick  * Advance the position in the mbuf chain with/without freeing mbufs
39138420Smckusick  */
39238420Smckusick nfs_adv(mdp, dposp, offs, left)
39338420Smckusick 	struct mbuf **mdp;
39438420Smckusick 	caddr_t *dposp;
39538420Smckusick 	int offs;
39638420Smckusick 	int left;
39738420Smckusick {
39838420Smckusick 	register struct mbuf *m;
39938420Smckusick 	register int s;
40038420Smckusick 
40138420Smckusick 	m = *mdp;
40238420Smckusick 	s = left;
40338420Smckusick 	while (s < offs) {
40438420Smckusick 		offs -= s;
40538420Smckusick 		m = m->m_next;
40638420Smckusick 		if (m == NULL)
40738420Smckusick 			return(EBADRPC);
40838420Smckusick 		s = m->m_len;
40938420Smckusick 	}
41038420Smckusick 	*mdp = m;
41138420Smckusick 	*dposp = mtod(m, caddr_t)+offs;
41238420Smckusick 	return(0);
41338420Smckusick }
41438420Smckusick 
41538420Smckusick /*
41638420Smckusick  * Copy a string into mbufs for the hard cases...
41738420Smckusick  */
41838420Smckusick nfsm_strtmbuf(mb, bpos, cp, siz)
41938420Smckusick 	struct mbuf **mb;
42038420Smckusick 	char **bpos;
42138420Smckusick 	char *cp;
42238420Smckusick 	long siz;
42338420Smckusick {
42438420Smckusick 	register struct mbuf *m1, *m2;
42538420Smckusick 	long left, xfer, len, tlen;
42638420Smckusick 	u_long *p;
42738420Smckusick 	int putsize;
42838420Smckusick 
42938420Smckusick 	putsize = 1;
43038420Smckusick 	m2 = *mb;
43138420Smckusick 	left = NFSMSIZ(m2)-m2->m_len;
43238420Smckusick 	if (left > 0) {
43338420Smckusick 		p = ((u_long *)(*bpos));
43438420Smckusick 		*p++ = txdr_unsigned(siz);
43538420Smckusick 		putsize = 0;
43638420Smckusick 		left -= NFSX_UNSIGNED;
43738420Smckusick 		m2->m_len += NFSX_UNSIGNED;
43838420Smckusick 		if (left > 0) {
43938420Smckusick 			bcopy(cp, (caddr_t) p, left);
44038420Smckusick 			siz -= left;
44138420Smckusick 			cp += left;
44238420Smckusick 			m2->m_len += left;
44338420Smckusick 			left = 0;
44438420Smckusick 		}
44538420Smckusick 	}
44638420Smckusick 	/* Loop arround adding mbufs */
44738420Smckusick 	while (siz > 0) {
44838420Smckusick 		MGET(m1, M_WAIT, MT_DATA);
44938420Smckusick 		if (siz > MLEN)
45038420Smckusick 			NFSMCLGET(m1, M_WAIT);
45138420Smckusick 		m1->m_len = NFSMSIZ(m1);
45238420Smckusick 		m2->m_next = m1;
45338420Smckusick 		m2 = m1;
45438420Smckusick 		p = mtod(m1, u_long *);
45538420Smckusick 		tlen = 0;
45638420Smckusick 		if (putsize) {
45738420Smckusick 			*p++ = txdr_unsigned(siz);
45838420Smckusick 			m1->m_len -= NFSX_UNSIGNED;
45938420Smckusick 			tlen = NFSX_UNSIGNED;
46038420Smckusick 			putsize = 0;
46138420Smckusick 		}
46238420Smckusick 		if (siz < m1->m_len) {
46338420Smckusick 			len = nfsm_rndup(siz);
46438420Smckusick 			xfer = siz;
46538420Smckusick 			if (xfer < len)
46638420Smckusick 				*(p+(xfer>>2)) = 0;
46738420Smckusick 		} else {
46838420Smckusick 			xfer = len = m1->m_len;
46938420Smckusick 		}
47038420Smckusick 		bcopy(cp, (caddr_t) p, xfer);
47138420Smckusick 		m1->m_len = len+tlen;
47238420Smckusick 		siz -= xfer;
47338420Smckusick 		cp += xfer;
47438420Smckusick 	}
47538420Smckusick 	*mb = m1;
47638420Smckusick 	*bpos = mtod(m1, caddr_t)+m1->m_len;
47738420Smckusick 	return(0);
47838420Smckusick }
47938420Smckusick 
48038420Smckusick /*
48138420Smckusick  * Called once to initialize data structures...
48238420Smckusick  */
48339444Smckusick nfs_init()
48438420Smckusick {
48538420Smckusick 	register int i;
48638420Smckusick 
48738420Smckusick 	rpc_vers = txdr_unsigned(RPC_VER2);
48838420Smckusick 	rpc_call = txdr_unsigned(RPC_CALL);
48938420Smckusick 	rpc_reply = txdr_unsigned(RPC_REPLY);
49038420Smckusick 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
49138420Smckusick 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
49238420Smckusick 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
49338420Smckusick 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
49438420Smckusick 	nfs_vers = txdr_unsigned(NFS_VER2);
49538420Smckusick 	nfs_prog = txdr_unsigned(NFS_PROG);
49638420Smckusick 	nfs_true = txdr_unsigned(TRUE);
49738420Smckusick 	nfs_false = txdr_unsigned(FALSE);
49838420Smckusick 	/* Loop thru nfs procids */
49938420Smckusick 	for (i = 0; i < NFS_NPROCS; i++)
50038420Smckusick 		nfs_procids[i] = txdr_unsigned(i);
50139345Smckusick 	/* Ensure async daemons disabled */
50239345Smckusick 	for (i = 0; i < MAX_ASYNCDAEMON; i++)
50339345Smckusick 		nfs_iodwant[i] = (struct proc *)0;
50438420Smckusick 	v_type[0] = VNON;
50538420Smckusick 	v_type[1] = VREG;
50638420Smckusick 	v_type[2] = VDIR;
50738420Smckusick 	v_type[3] = VBLK;
50838420Smckusick 	v_type[4] = VCHR;
50938420Smckusick 	v_type[5] = VLNK;
51038420Smckusick 	nfs_xdrneg1 = txdr_unsigned(-1);
51138420Smckusick 	nfs_nhinit();			/* Init the nfsnode table */
51239755Smckusick 	nfsrv_initcache();		/* Init the server request cache */
51338884Smacklem 	rminit(nfsmap, (long)NFS_MAPREG, (long)1, "nfs mapreg", NFS_MSIZ);
51438420Smckusick 	/* And start timer */
51538420Smckusick 	nfs_timer();
51638420Smckusick }
51738420Smckusick 
51838420Smckusick /*
51938420Smckusick  * Fill in the rest of the rpc_unixauth and return it
52038420Smckusick  */
52138420Smckusick static char *nfs_unixauth(cr)
52238420Smckusick 	register struct ucred *cr;
52338420Smckusick {
52438420Smckusick 	register u_long *p;
52538420Smckusick 	register int i;
52638420Smckusick 	int ngr;
52738420Smckusick 
52838420Smckusick 	/* Maybe someday there should be a cache of AUTH_SHORT's */
52938420Smckusick 	if ((p = rpc_uidp) == NULL) {
53038425Smckusick #ifdef FILLINHOST
53138420Smckusick 		i = nfsm_rndup(hostnamelen)+(19*NFSX_UNSIGNED);
53238425Smckusick #else
53338425Smckusick 		i = 19*NFSX_UNSIGNED;
53438425Smckusick #endif
53538420Smckusick 		MALLOC(p, u_long *, i, M_TEMP, M_WAITOK);
53638420Smckusick 		bzero((caddr_t)p, i);
53738420Smckusick 		rpc_unixauth = (caddr_t)p;
53838420Smckusick 		*p++ = txdr_unsigned(RPCAUTH_UNIX);
53938420Smckusick 		p++;	/* Fill in size later */
54038420Smckusick 		*p++ = hostid;
54138425Smckusick #ifdef FILLINHOST
54238420Smckusick 		*p++ = txdr_unsigned(hostnamelen);
54338420Smckusick 		i = nfsm_rndup(hostnamelen);
54438420Smckusick 		bcopy(hostname, (caddr_t)p, hostnamelen);
54538420Smckusick 		p += (i>>2);
54638425Smckusick #else
54738425Smckusick 		*p++ = 0;
54838425Smckusick #endif
54938420Smckusick 		rpc_uidp = p;
55038420Smckusick 	}
55138420Smckusick 	*p++ = txdr_unsigned(cr->cr_uid);
55238420Smckusick 	*p++ = txdr_unsigned(cr->cr_groups[0]);
55338737Smckusick 	ngr = (cr->cr_ngroups > numgrps) ? numgrps : cr->cr_ngroups;
55438420Smckusick 	*p++ = txdr_unsigned(ngr);
55538420Smckusick 	for (i = 0; i < ngr; i++)
55638420Smckusick 		*p++ = txdr_unsigned(cr->cr_groups[i]);
55738420Smckusick 	/* And add the AUTH_NULL */
55838420Smckusick 	*p++ = 0;
55938420Smckusick 	*p = 0;
56038420Smckusick 	i = (((caddr_t)p)-rpc_unixauth)-12;
56138420Smckusick 	p = (u_long *)(rpc_unixauth+4);
56238420Smckusick 	*p = txdr_unsigned(i);
56338420Smckusick 	return(rpc_unixauth);
56438420Smckusick }
56538420Smckusick 
56638420Smckusick /*
56738420Smckusick  * Attribute cache routines.
56838420Smckusick  * nfs_loadattrcache() - loads or updates the cache contents from attributes
56938420Smckusick  *	that are on the mbuf list
57038420Smckusick  * nfs_getattrcache() - returns valid attributes if found in cache, returns
57138420Smckusick  *	error otherwise
57238420Smckusick  */
57338420Smckusick 
57438420Smckusick /*
57539444Smckusick  * Load the attribute cache (that lives in the nfsnode entry) with
57638420Smckusick  * the values on the mbuf list and
57738420Smckusick  * Iff vap not NULL
57838420Smckusick  *    copy the attributes to *vaper
57938420Smckusick  */
58039457Smckusick nfs_loadattrcache(vpp, mdp, dposp, vaper)
58139457Smckusick 	struct vnode **vpp;
58238420Smckusick 	struct mbuf **mdp;
58338420Smckusick 	caddr_t *dposp;
58438420Smckusick 	struct vattr *vaper;
58538420Smckusick {
58639457Smckusick 	register struct vnode *vp = *vpp;
58738420Smckusick 	register struct vattr *vap;
58838884Smacklem 	register struct nfsv2_fattr *fp;
58939444Smckusick 	extern struct vnodeops spec_nfsv2nodeops;
59039457Smckusick 	register struct nfsnode *np;
59139494Smckusick 	register long t1;
59239494Smckusick 	caddr_t dpos, cp2;
59339494Smckusick 	int error = 0;
59439494Smckusick 	struct mbuf *md;
59539444Smckusick 	enum vtype type;
59639444Smckusick 	dev_t rdev;
59739444Smckusick 	struct timeval mtime;
59839444Smckusick 	struct vnode *nvp;
59938420Smckusick 
60038420Smckusick 	md = *mdp;
60138420Smckusick 	dpos = *dposp;
60238420Smckusick 	t1 = (mtod(md, caddr_t)+md->m_len)-dpos;
60338420Smckusick 	if (error = nfsm_disct(&md, &dpos, NFSX_FATTR, t1, TRUE, &cp2))
60438420Smckusick 		return (error);
60538884Smacklem 	fp = (struct nfsv2_fattr *)cp2;
60639444Smckusick 	type = nfstov_type(fp->fa_type);
60739444Smckusick 	rdev = fxdr_unsigned(dev_t, fp->fa_rdev);
60839444Smckusick 	fxdr_time(&fp->fa_mtime, &mtime);
60939444Smckusick 	/*
61039444Smckusick 	 * If v_type == VNON it is a new node, so fill in the v_type,
61139444Smckusick 	 * n_mtime fields. Check to see if it represents a special
61239444Smckusick 	 * device, and if so, check for a possible alias. Once the
61339444Smckusick 	 * correct vnode has been obtained, fill in the rest of the
61439444Smckusick 	 * information.
61539444Smckusick 	 */
61638420Smckusick 	np = VTONFS(vp);
61739444Smckusick 	if (vp->v_type == VNON) {
61839444Smckusick 		vp->v_type = type;
61940295Smckusick 		if (vp->v_type == VFIFO) {
62040295Smckusick #ifdef FIFO
62140295Smckusick 			extern struct vnodeops fifo_nfsv2nodeops;
62240295Smckusick 			vp->v_op = &fifo_nfsv2nodeops;
62340295Smckusick #else
62440295Smckusick 			return (EOPNOTSUPP);
62540295Smckusick #endif /* FIFO */
62640295Smckusick 		}
62739444Smckusick 		if (vp->v_type == VCHR || vp->v_type == VBLK) {
62839444Smckusick 			vp->v_op = &spec_nfsv2nodeops;
62939618Smckusick 			if (nvp = checkalias(vp, rdev, vp->v_mount)) {
63039444Smckusick 				/*
63139444Smckusick 				 * Reinitialize aliased node.
63239444Smckusick 				 */
63339444Smckusick 				np = VTONFS(nvp);
63439444Smckusick 				np->n_vnode = nvp;
63539907Smckusick 				np->n_flag = 0;
63639907Smckusick 				nfs_lock(nvp);
63739444Smckusick 				bcopy((caddr_t)&VTONFS(vp)->n_fh,
63839444Smckusick 					(caddr_t)&np->n_fh, NFSX_FH);
63939444Smckusick 				insque(np, nfs_hash(&np->n_fh));
64039444Smckusick 				np->n_attrstamp = 0;
64139444Smckusick 				np->n_sillyrename = (struct sillyrename *)0;
64239444Smckusick 				/*
64339457Smckusick 				 * Discard unneeded vnode and update actual one
64439444Smckusick 				 */
64539444Smckusick 				vput(vp);
64640295Smckusick 				*vpp = vp = nvp;
64739444Smckusick 			}
64839444Smckusick 		}
64939444Smckusick 		np->n_mtime = mtime.tv_sec;
65039444Smckusick 	}
65138420Smckusick 	vap = &np->n_vattr;
65239444Smckusick 	vap->va_type = type;
65338884Smacklem 	vap->va_mode = nfstov_mode(fp->fa_mode);
65438884Smacklem 	vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
65538884Smacklem 	vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
65638884Smacklem 	vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
65738884Smacklem 	vap->va_size = fxdr_unsigned(u_long, fp->fa_size);
65838884Smacklem 	if ((np->n_flag & NMODIFIED) == 0 || vap->va_size > np->n_size)
65938884Smacklem 		np->n_size = vap->va_size;
66040642Smckusick 	vap->va_size_rsv = 0;
66138884Smacklem 	vap->va_blocksize = fxdr_unsigned(long, fp->fa_blocksize);
66239444Smckusick 	vap->va_rdev = rdev;
66338884Smacklem 	vap->va_bytes = fxdr_unsigned(long, fp->fa_blocks) * vap->va_blocksize;
66440642Smckusick 	vap->va_bytes_rsv = 0;
665*41398Smckusick 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
66638884Smacklem 	vap->va_fileid = fxdr_unsigned(long, fp->fa_fileid);
66739755Smckusick 	vap->va_atime.tv_sec = fxdr_unsigned(long, fp->fa_atime.tv_sec);
66839755Smckusick 	vap->va_atime.tv_usec = 0;
66939755Smckusick 	vap->va_flags = fxdr_unsigned(u_long, fp->fa_atime.tv_usec);
67039444Smckusick 	vap->va_mtime = mtime;
67139755Smckusick 	vap->va_ctime.tv_sec = fxdr_unsigned(long, fp->fa_ctime.tv_sec);
67239755Smckusick 	vap->va_ctime.tv_usec = 0;
67339755Smckusick 	vap->va_gen = fxdr_unsigned(u_long, fp->fa_ctime.tv_usec);
67438420Smckusick 	np->n_attrstamp = time.tv_sec;
67538420Smckusick 	*dposp = dpos;
67638420Smckusick 	*mdp = md;
67738884Smacklem 	if (vaper != NULL) {
67838420Smckusick 		bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
67938884Smacklem 		if ((np->n_flag & NMODIFIED) && (np->n_size > vap->va_size))
68038884Smacklem 			vaper->va_size = np->n_size;
68138884Smacklem 	}
68238420Smckusick 	return (0);
68338420Smckusick }
68438420Smckusick 
68538420Smckusick /*
68638420Smckusick  * Check the time stamp
68738420Smckusick  * If the cache is valid, copy contents to *vap and return 0
68838420Smckusick  * otherwise return an error
68938420Smckusick  */
69038420Smckusick nfs_getattrcache(vp, vap)
69138420Smckusick 	register struct vnode *vp;
69238420Smckusick 	struct vattr *vap;
69338420Smckusick {
69438420Smckusick 	register struct nfsnode *np;
69538420Smckusick 
69638420Smckusick 	np = VTONFS(vp);
69738420Smckusick 	if ((time.tv_sec-np->n_attrstamp) < NFS_ATTRTIMEO) {
69838420Smckusick 		nfsstats.attrcache_hits++;
69938420Smckusick 		bcopy((caddr_t)&np->n_vattr,(caddr_t)vap,sizeof(struct vattr));
70039361Smckusick 		if ((np->n_flag & NMODIFIED) == 0)
70139361Smckusick 			np->n_size = vap->va_size;
70239361Smckusick 		else if (np->n_size > vap->va_size)
70338884Smacklem 			vap->va_size = np->n_size;
70438420Smckusick 		return (0);
70538420Smckusick 	} else {
70638420Smckusick 		nfsstats.attrcache_misses++;
70738420Smckusick 		return (ENOENT);
70838420Smckusick 	}
70938420Smckusick }
71038420Smckusick 
71138420Smckusick /*
71238420Smckusick  * nfs_namei - a liitle like namei(), but for one element only
71338420Smckusick  *	essentially look up file handle, fill in ndp and call VOP_LOOKUP()
71438420Smckusick  */
71538420Smckusick nfs_namei(ndp, fhp, len, mdp, dposp)
71638420Smckusick 	register struct nameidata *ndp;
71738420Smckusick 	fhandle_t *fhp;
71838420Smckusick 	int len;
71938420Smckusick 	struct mbuf **mdp;
72038420Smckusick 	caddr_t *dposp;
72138420Smckusick {
72238420Smckusick 	register int i, rem;
72338420Smckusick 	register struct mbuf *md;
72438420Smckusick 	register char *cp;
725*41398Smckusick 	struct vnode *dp = NULLVP;
72638420Smckusick 	int flag;
72738420Smckusick 	int docache;
72838420Smckusick 	int wantparent;
72938420Smckusick 	int lockparent;
73038420Smckusick 	int error = 0;
73138420Smckusick 
732*41398Smckusick 	ndp->ni_vp = ndp->ni_dvp = NULLVP;
73338420Smckusick 	flag = ndp->ni_nameiop & OPFLAG;
73438420Smckusick 	wantparent = ndp->ni_nameiop & (LOCKPARENT | WANTPARENT);
73538420Smckusick 	lockparent = ndp->ni_nameiop & LOCKPARENT;
73638420Smckusick 	docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE;
73738420Smckusick 	if (flag == DELETE || wantparent)
73838420Smckusick 		docache = 0;
73938420Smckusick 
74038420Smckusick 	/* Fill in the nameidata and call lookup */
74138420Smckusick 	cp = *dposp;
74238420Smckusick 	md = *mdp;
74338420Smckusick 	rem = mtod(md, caddr_t)+md->m_len-cp;
74438420Smckusick 	ndp->ni_hash = 0;
74538420Smckusick 	for (i = 0; i < len;) {
74638420Smckusick 		if (rem == 0) {
74738420Smckusick 			md = md->m_next;
74838420Smckusick 			if (md == NULL)
74938420Smckusick 				return (EBADRPC);
75038420Smckusick 			cp = mtod(md, caddr_t);
75138420Smckusick 			rem = md->m_len;
75238420Smckusick 		}
75338420Smckusick 		if (*cp == '\0' || *cp == '/')
75438420Smckusick 			return (EINVAL);
75538420Smckusick 		if (*cp & 0200)
75638420Smckusick 			if ((*cp&0377) == ('/'|0200) || flag != DELETE)
75738420Smckusick 				return (EINVAL);
75838420Smckusick 		ndp->ni_dent.d_name[i++] = *cp;
75938420Smckusick 		ndp->ni_hash += (unsigned char)*cp * i;
76038420Smckusick 		cp++;
76138420Smckusick 		rem--;
76238420Smckusick 	}
76338420Smckusick 	*mdp = md;
76438420Smckusick 	len = nfsm_rndup(len)-len;
76538420Smckusick 	if (len > 0)
76638420Smckusick 		*dposp = cp+len;
76738420Smckusick 	else
76838420Smckusick 		*dposp = cp;
76938420Smckusick 	ndp->ni_namelen = i;
77038420Smckusick 	ndp->ni_dent.d_namlen = i;
77138420Smckusick 	ndp->ni_dent.d_name[i] = '\0';
77238425Smckusick 	ndp->ni_pathlen = 1;
77338420Smckusick 	ndp->ni_dirp = ndp->ni_ptr = &ndp->ni_dent.d_name[0];
77438420Smckusick 	ndp->ni_next = &ndp->ni_dent.d_name[i];
77538420Smckusick 	ndp->ni_loopcnt = 0;	/* Not actually used for now */
77638420Smckusick 	ndp->ni_endoff = 0;
77738420Smckusick 	if (docache)
77838420Smckusick 		ndp->ni_makeentry = 1;
77938420Smckusick 	else
78038420Smckusick 		ndp->ni_makeentry = 0;
78138420Smckusick 	ndp->ni_isdotdot = (i == 2 &&
78238420Smckusick 		ndp->ni_dent.d_name[1] == '.' && ndp->ni_dent.d_name[0] == '.');
78338420Smckusick 
78438420Smckusick 	if (error = nfsrv_fhtovp(fhp, TRUE, &dp, ndp->ni_cred))
78538420Smckusick 		return (error);
78638425Smckusick 	if (dp->v_type != VDIR) {
78738425Smckusick 		vput(dp);
78838425Smckusick 		return (ENOTDIR);
78938425Smckusick 	}
79039345Smckusick 	/*
79139345Smckusick 	 * Must set current directory here to avoid confusion in namei()
79239345Smckusick 	 * called from rename()
79339345Smckusick 	 */
79438425Smckusick 	ndp->ni_cdir = dp;
795*41398Smckusick 	ndp->ni_rdir = NULLVP;
79638420Smckusick 
79738420Smckusick 	/*
79838425Smckusick 	 * Handle "..":
79938425Smckusick 	 * If this vnode is the root of the mounted
80038425Smckusick 	 *    file system, then ignore it so can't get out
80138420Smckusick 	 */
80238425Smckusick 	if (ndp->ni_isdotdot && (dp->v_flag & VROOT)) {
80338425Smckusick 		ndp->ni_dvp = dp;
80438425Smckusick 		ndp->ni_vp = dp;
80538425Smckusick 		VREF(dp);
80638425Smckusick 		goto nextname;
80738420Smckusick 	}
80838420Smckusick 
80938420Smckusick 	/*
81038420Smckusick 	 * We now have a segment name to search for, and a directory to search.
81138420Smckusick 	 */
81238420Smckusick 	if (error = VOP_LOOKUP(dp, ndp)) {
81338420Smckusick 		if (ndp->ni_vp != NULL)
81438420Smckusick 			panic("leaf should be empty");
81538420Smckusick 		/*
81638420Smckusick 		 * If creating and at end of pathname, then can consider
81738420Smckusick 		 * allowing file to be created.
81838420Smckusick 		 */
819*41398Smckusick 		if (ndp->ni_dvp->v_mount->mnt_flag & (MNT_RDONLY|MNT_EXRDONLY))
82038420Smckusick 			error = EROFS;
82138420Smckusick 		if (flag == LOOKUP || flag == DELETE || error != ENOENT)
82238420Smckusick 			goto bad;
82338420Smckusick 		/*
82438420Smckusick 		 * We return with ni_vp NULL to indicate that the entry
82538420Smckusick 		 * doesn't currently exist, leaving a pointer to the
82638420Smckusick 		 * (possibly locked) directory inode in ndp->ni_dvp.
82738420Smckusick 		 */
82838420Smckusick 		return (0);	/* should this be ENOENT? */
82938420Smckusick 	}
83038420Smckusick 
83138420Smckusick 	dp = ndp->ni_vp;
83238420Smckusick 
83338420Smckusick nextname:
83438425Smckusick 	ndp->ni_ptr = ndp->ni_next;
83538420Smckusick 	/*
83638425Smckusick 	 * Check for read-only file systems
83738420Smckusick 	 */
83838421Smckusick 	if (flag == DELETE || flag == RENAME) {
83938421Smckusick 		/*
84038421Smckusick 		 * Disallow directory write attempts on read-only
84138421Smckusick 		 * file systems.
84238421Smckusick 		 */
843*41398Smckusick 		if ((dp->v_mount->mnt_flag & (MNT_RDONLY|MNT_EXRDONLY)) ||
844*41398Smckusick 		    (wantparent &&
845*41398Smckusick 		    (ndp->ni_dvp->v_mount->mnt_flag &
846*41398Smckusick 		     (MNT_RDONLY|MNT_EXRDONLY)))) {
84738421Smckusick 			error = EROFS;
84838421Smckusick 			goto bad2;
84938421Smckusick 		}
85038421Smckusick 	}
85138420Smckusick 
85238420Smckusick 	if (!wantparent)
85338420Smckusick 		vrele(ndp->ni_dvp);
85438420Smckusick 
85538420Smckusick 	if ((ndp->ni_nameiop & LOCKLEAF) == 0)
85638420Smckusick 		VOP_UNLOCK(dp);
85738420Smckusick 	return (0);
85838420Smckusick 
85938420Smckusick bad2:
86038420Smckusick 	if (lockparent)
86138420Smckusick 		VOP_UNLOCK(ndp->ni_dvp);
86238420Smckusick 	vrele(ndp->ni_dvp);
86338420Smckusick bad:
86438420Smckusick 	vput(dp);
86538420Smckusick 	ndp->ni_vp = NULL;
86638420Smckusick 	return (error);
86738420Smckusick }
86838420Smckusick 
86938420Smckusick /*
87038420Smckusick  * A fiddled version of m_adj() that ensures null fill to a long
87138420Smckusick  * boundary and only trims off the back end
87238420Smckusick  */
87338420Smckusick nfsm_adj(mp, len, nul)
87438420Smckusick 	struct mbuf *mp;
87538420Smckusick 	register int len;
87638420Smckusick 	int nul;
87738420Smckusick {
87838420Smckusick 	register struct mbuf *m;
87938420Smckusick 	register int count, i;
88038420Smckusick 	register char *cp;
88138420Smckusick 
88238420Smckusick 	/*
88338420Smckusick 	 * Trim from tail.  Scan the mbuf chain,
88438420Smckusick 	 * calculating its length and finding the last mbuf.
88538420Smckusick 	 * If the adjustment only affects this mbuf, then just
88638420Smckusick 	 * adjust and return.  Otherwise, rescan and truncate
88738420Smckusick 	 * after the remaining size.
88838420Smckusick 	 */
88938420Smckusick 	count = 0;
89038420Smckusick 	m = mp;
89138420Smckusick 	for (;;) {
89238420Smckusick 		count += m->m_len;
89338420Smckusick 		if (m->m_next == (struct mbuf *)0)
89438420Smckusick 			break;
89538420Smckusick 		m = m->m_next;
89638420Smckusick 	}
89738579Smckusick 	if (m->m_len > len) {
89838420Smckusick 		m->m_len -= len;
89938420Smckusick 		if (nul > 0) {
90038420Smckusick 			cp = mtod(m, caddr_t)+m->m_len-nul;
90138420Smckusick 			for (i = 0; i < nul; i++)
90238420Smckusick 				*cp++ = '\0';
90338420Smckusick 		}
90438420Smckusick 		return;
90538420Smckusick 	}
90638420Smckusick 	count -= len;
90738420Smckusick 	if (count < 0)
90838420Smckusick 		count = 0;
90938420Smckusick 	/*
91038420Smckusick 	 * Correct length for chain is "count".
91138420Smckusick 	 * Find the mbuf with last data, adjust its length,
91238420Smckusick 	 * and toss data from remaining mbufs on chain.
91338420Smckusick 	 */
91438420Smckusick 	for (m = mp; m; m = m->m_next) {
91538420Smckusick 		if (m->m_len >= count) {
91638420Smckusick 			m->m_len = count;
91738420Smckusick 			if (nul > 0) {
91838420Smckusick 				cp = mtod(m, caddr_t)+m->m_len-nul;
91938420Smckusick 				for (i = 0; i < nul; i++)
92038420Smckusick 					*cp++ = '\0';
92138420Smckusick 			}
92238420Smckusick 			break;
92338420Smckusick 		}
92438420Smckusick 		count -= m->m_len;
92538420Smckusick 	}
92638420Smckusick 	while (m = m->m_next)
92738420Smckusick 		m->m_len = 0;
92838420Smckusick }
92938420Smckusick 
93038420Smckusick /*
93138420Smckusick  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
93238420Smckusick  * 	- look up fsid in mount list (if not found ret error)
93338420Smckusick  *	- check that it is exported
93438420Smckusick  *	- get vp by calling VFS_FHTOVP() macro
93538420Smckusick  *	- if not lockflag unlock it with VOP_UNLOCK()
936*41398Smckusick  *	- if cred->cr_uid == 0 set it to mnt_exroot
93738420Smckusick  */
93838420Smckusick nfsrv_fhtovp(fhp, lockflag, vpp, cred)
93938420Smckusick 	fhandle_t *fhp;
94038420Smckusick 	int lockflag;
94138420Smckusick 	struct vnode **vpp;
94238420Smckusick 	struct ucred *cred;
94338420Smckusick {
94438420Smckusick 	register struct mount *mp;
94538420Smckusick 
94638420Smckusick 	if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
94738420Smckusick 		return (ESTALE);
948*41398Smckusick 	if ((mp->mnt_flag & MNT_EXPORTED) == 0)
94938420Smckusick 		return (EACCES);
95038420Smckusick 	if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
95138420Smckusick 		return (ESTALE);
95238420Smckusick 	if (cred->cr_uid == 0)
953*41398Smckusick 		cred->cr_uid = mp->mnt_exroot;
95438420Smckusick 	if (!lockflag)
95538420Smckusick 		VOP_UNLOCK(*vpp);
95638420Smckusick 	return (0);
95738420Smckusick }
958