xref: /csrg-svn/sys/nfs/nfs_subs.c (revision 39907)
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*39907Smckusick  *	@(#)nfs_subs.c	7.16 (Berkeley) 01/15/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 "strings.h"
2938420Smckusick #include "param.h"
3039444Smckusick #include "systm.h"
3139345Smckusick #include "user.h"
3239345Smckusick #include "proc.h"
3338420Smckusick #include "mount.h"
3438425Smckusick #include "../ufs/dir.h"
3538420Smckusick #include "time.h"
3638420Smckusick #include "errno.h"
3738420Smckusick #include "kernel.h"
3838420Smckusick #include "malloc.h"
3938420Smckusick #include "mbuf.h"
4038420Smckusick #include "file.h"
4138420Smckusick #include "vnode.h"
4238420Smckusick #include "uio.h"
4338420Smckusick #include "namei.h"
4438420Smckusick #include "ucred.h"
4538884Smacklem #include "map.h"
4638420Smckusick #include "rpcv2.h"
4738420Smckusick #include "nfsv2.h"
4838420Smckusick #include "nfsnode.h"
4938420Smckusick #include "nfs.h"
5038884Smacklem #include "nfsiom.h"
5138420Smckusick #include "xdr_subs.h"
5238420Smckusick #include "nfsm_subs.h"
5338420Smckusick 
5438420Smckusick #define TRUE	1
5538420Smckusick #define	FALSE	0
5638420Smckusick 
5738420Smckusick /*
5838420Smckusick  * Data items converted to xdr at startup, since they are constant
5938420Smckusick  * This is kinda hokey, but may save a little time doing byte swaps
6038420Smckusick  */
6138420Smckusick u_long nfs_procids[NFS_NPROCS];
6238420Smckusick u_long nfs_xdrneg1;
6338420Smckusick u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied,
6438420Smckusick 	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
6538420Smckusick u_long nfs_vers, nfs_prog, nfs_true, nfs_false;
6638420Smckusick /* And other global data */
6738420Smckusick static u_long *rpc_uidp = (u_long *)0;
6838420Smckusick static u_long nfs_xid = 1;
6938420Smckusick static char *rpc_unixauth;
7038420Smckusick extern long hostid;
7138420Smckusick extern enum vtype v_type[NFLNK+1];
7239345Smckusick extern struct proc *nfs_iodwant[MAX_ASYNCDAEMON];
7338884Smacklem extern struct map nfsmap[NFS_MSIZ];
7438420Smckusick 
7538420Smckusick /* Function ret types */
7638420Smckusick static char *nfs_unixauth();
7738420Smckusick 
7838420Smckusick /*
7938737Smckusick  * Maximum number of groups passed through to NFS server.
8038737Smckusick  * For release 3.X systems, the maximum value is 8.
8138737Smckusick  * For release 4.X systems, the maximum value is 10.
8238737Smckusick  */
8338737Smckusick int numgrps = 8;
8438737Smckusick 
8538737Smckusick /*
8638420Smckusick  * Create the header for an rpc request packet
8738420Smckusick  * The function nfs_unixauth() creates a unix style authorization string
8838420Smckusick  * and returns a ptr to it.
8938420Smckusick  * The hsiz is the size of the rest of the nfs request header.
9038420Smckusick  * (just used to decide if a cluster is a good idea)
9139494Smckusick  * nb: Note that the prog, vers and procid args are already in xdr byte order
9238420Smckusick  */
9339494Smckusick struct mbuf *nfsm_reqh(prog, vers, procid, cred, hsiz, bpos, mb, retxid)
9438420Smckusick 	u_long prog;
9538420Smckusick 	u_long vers;
9639494Smckusick 	u_long procid;
9738420Smckusick 	struct ucred *cred;
9838420Smckusick 	int hsiz;
9938420Smckusick 	caddr_t *bpos;
10038420Smckusick 	struct mbuf **mb;
10138420Smckusick 	u_long *retxid;
10238420Smckusick {
10338420Smckusick 	register struct mbuf *mreq, *m;
10438420Smckusick 	register u_long *p;
10538420Smckusick 	struct mbuf *m1;
10638420Smckusick 	char *ap;
10738420Smckusick 	int asiz, siz;
10838420Smckusick 
10938420Smckusick 	NFSMGETHDR(mreq);
11038737Smckusick 	asiz = (((cred->cr_ngroups > numgrps) ? numgrps : cred->cr_ngroups)<<2);
11138425Smckusick #ifdef FILLINHOST
11238420Smckusick 	asiz += nfsm_rndup(hostnamelen)+(9*NFSX_UNSIGNED);
11338425Smckusick #else
11438425Smckusick 	asiz += 9*NFSX_UNSIGNED;
11538425Smckusick #endif
11638420Smckusick 
11738420Smckusick 	/* If we need a lot, alloc a cluster ?? */
11838420Smckusick 	if ((asiz+hsiz+RPC_SIZ) > MHLEN)
11938420Smckusick 		NFSMCLGET(mreq, M_WAIT);
12038420Smckusick 	mreq->m_len = NFSMSIZ(mreq);
12138420Smckusick 	siz = mreq->m_len;
12238420Smckusick 	m1 = mreq;
12338420Smckusick 	/*
12438420Smckusick 	 * Alloc enough mbufs
12538420Smckusick 	 * We do it now to avoid all sleeps after the call to nfs_unixauth()
12638420Smckusick 	 */
12738420Smckusick 	while ((asiz+RPC_SIZ) > siz) {
12838420Smckusick 		MGET(m, M_WAIT, MT_DATA);
12938420Smckusick 		m1->m_next = m;
13038420Smckusick 		m->m_len = MLEN;
13138420Smckusick 		siz += MLEN;
13238420Smckusick 		m1 = m;
13338420Smckusick 	}
13438420Smckusick 	p = mtod(mreq, u_long *);
13538420Smckusick 	*p++ = *retxid = txdr_unsigned(++nfs_xid);
13638420Smckusick 	*p++ = rpc_call;
13738420Smckusick 	*p++ = rpc_vers;
13838420Smckusick 	*p++ = prog;
13938420Smckusick 	*p++ = vers;
14039494Smckusick 	*p++ = procid;
14138420Smckusick 
14238420Smckusick 	/* Now we can call nfs_unixauth() and copy it in */
14338420Smckusick 	ap = nfs_unixauth(cred);
14438420Smckusick 	m = mreq;
14538420Smckusick 	siz = m->m_len-RPC_SIZ;
14638420Smckusick 	if (asiz <= siz) {
14738420Smckusick 		bcopy(ap, (caddr_t)p, asiz);
14838420Smckusick 		m->m_len = asiz+RPC_SIZ;
14938420Smckusick 	} else {
15038420Smckusick 		bcopy(ap, (caddr_t)p, siz);
15138420Smckusick 		ap += siz;
15238420Smckusick 		asiz -= siz;
15338420Smckusick 		while (asiz > 0) {
15438420Smckusick 			siz = (asiz > MLEN) ? MLEN : asiz;
15538420Smckusick 			m = m->m_next;
15638420Smckusick 			bcopy(ap, mtod(m, caddr_t), siz);
15738420Smckusick 			m->m_len = siz;
15838420Smckusick 			asiz -= siz;
15938420Smckusick 			ap += siz;
16038420Smckusick 		}
16138420Smckusick 	}
16238420Smckusick 
16338420Smckusick 	/* Finally, return values */
16438420Smckusick 	*mb = m;
16538420Smckusick 	*bpos = mtod(m, caddr_t)+m->m_len;
16638420Smckusick 	return (mreq);
16738420Smckusick }
16838420Smckusick 
16938420Smckusick /*
17038420Smckusick  * copies mbuf chain to the uio scatter/gather list
17138420Smckusick  */
17238420Smckusick nfsm_mbuftouio(mrep, uiop, siz, dpos)
17338420Smckusick 	struct mbuf **mrep;
17438420Smckusick 	struct uio *uiop;
17538420Smckusick 	int siz;
17638420Smckusick 	caddr_t *dpos;
17738420Smckusick {
17838420Smckusick 	register int xfer, left, len;
17938420Smckusick 	register struct mbuf *mp;
18038420Smckusick 	register char *mbufcp, *uiocp;
18138420Smckusick 	long uiosiz, rem;
18238420Smckusick 
18338420Smckusick 	mp = *mrep;
18438420Smckusick 	mbufcp = *dpos;
18538420Smckusick 	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
18638420Smckusick 	rem = nfsm_rndup(siz)-siz;
18738420Smckusick 	while (siz > 0) {
18838420Smckusick 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
18938420Smckusick 			return(EFBIG);
19038420Smckusick 		left = uiop->uio_iov->iov_len;
19138420Smckusick 		uiocp = uiop->uio_iov->iov_base;
19238420Smckusick 		if (left > siz)
19338420Smckusick 			left = siz;
19438420Smckusick 		uiosiz = left;
19538420Smckusick 		while (left > 0) {
19638420Smckusick 			while (len == 0) {
19738420Smckusick 				mp = mp->m_next;
19838420Smckusick 				if (mp == NULL)
19938420Smckusick 					return (EBADRPC);
20038420Smckusick 				mbufcp = mtod(mp, caddr_t);
20138420Smckusick 				len = mp->m_len;
20238420Smckusick 			}
20338420Smckusick 			xfer = (left > len) ? len : left;
20438420Smckusick #ifdef notdef
20538420Smckusick 			/* Not Yet.. */
20638420Smckusick 			if (uiop->uio_iov->iov_op != NULL)
20738420Smckusick 				(*(uiop->uio_iov->iov_op))
20838420Smckusick 				(mbufcp, uiocp, xfer);
20938420Smckusick 			else
21038420Smckusick #endif
21138420Smckusick 			if (uiop->uio_segflg == UIO_SYSSPACE)
21238420Smckusick 				bcopy(mbufcp, uiocp, xfer);
21338420Smckusick 			else
21438420Smckusick 				copyout(mbufcp, uiocp, xfer);
21538420Smckusick 			left -= xfer;
21638420Smckusick 			len -= xfer;
21738420Smckusick 			mbufcp += xfer;
21838420Smckusick 			uiocp += xfer;
21939585Smckusick 			uiop->uio_offset += xfer;
22038420Smckusick 			uiop->uio_resid -= xfer;
22138420Smckusick 		}
22238420Smckusick 		if (uiop->uio_iov->iov_len <= siz) {
22338420Smckusick 			uiop->uio_iovcnt--;
22438420Smckusick 			uiop->uio_iov++;
22538420Smckusick 		} else {
22638420Smckusick 			uiop->uio_iov->iov_base += uiosiz;
22738420Smckusick 			uiop->uio_iov->iov_len -= uiosiz;
22838420Smckusick 		}
22938420Smckusick 		siz -= uiosiz;
23038420Smckusick 	}
23138420Smckusick 	if (rem > 0)
23238420Smckusick 		mbufcp += rem;
23338420Smckusick 	*dpos = mbufcp;
23438420Smckusick 	*mrep = mp;
23538420Smckusick 	return(0);
23638420Smckusick }
23738420Smckusick 
23838420Smckusick /*
23938420Smckusick  * copies a uio scatter/gather list to an mbuf chain...
24038420Smckusick  */
24138420Smckusick nfsm_uiotombuf(uiop, mq, siz, bpos)
24238420Smckusick 	register struct uio *uiop;
24338420Smckusick 	struct mbuf **mq;
24438420Smckusick 	int siz;
24538420Smckusick 	caddr_t *bpos;
24638420Smckusick {
24738420Smckusick 	register struct mbuf *mp;
24838420Smckusick 	struct mbuf *mp2;
24939494Smckusick 	long xfer, left, uiosiz;
25038420Smckusick 	int clflg;
25138420Smckusick 	int rem, len;
25238420Smckusick 	char *cp, *uiocp;
25338420Smckusick 
25438420Smckusick 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
25538420Smckusick 		clflg = 1;
25638420Smckusick 	else
25738420Smckusick 		clflg = 0;
25838420Smckusick 	rem = nfsm_rndup(siz)-siz;
25938420Smckusick 	mp2 = *mq;
26038420Smckusick 	while (siz > 0) {
26138420Smckusick 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
26238420Smckusick 			return(EINVAL);
26338420Smckusick 		left = uiop->uio_iov->iov_len;
26438420Smckusick 		uiocp = uiop->uio_iov->iov_base;
26538420Smckusick 		if (left > siz)
26638420Smckusick 			left = siz;
26738420Smckusick 		uiosiz = left;
26838420Smckusick 		while (left > 0) {
26938420Smckusick 			MGET(mp, M_WAIT, MT_DATA);
27038420Smckusick 			if (clflg)
27138420Smckusick 				NFSMCLGET(mp, M_WAIT);
27238420Smckusick 			mp->m_len = NFSMSIZ(mp);
27338420Smckusick 			mp2->m_next = mp;
27438420Smckusick 			mp2 = mp;
27538420Smckusick 			xfer = (left > mp->m_len) ? mp->m_len : left;
27638420Smckusick #ifdef notdef
27738420Smckusick 			/* Not Yet.. */
27838420Smckusick 			if (uiop->uio_iov->iov_op != NULL)
27938420Smckusick 				(*(uiop->uio_iov->iov_op))
28038420Smckusick 				(uiocp, mtod(mp, caddr_t), xfer);
28138420Smckusick 			else
28238420Smckusick #endif
28338420Smckusick 			if (uiop->uio_segflg == UIO_SYSSPACE)
28438420Smckusick 				bcopy(uiocp, mtod(mp, caddr_t), xfer);
28538420Smckusick 			else
28638420Smckusick 				copyin(uiocp, mtod(mp, caddr_t), xfer);
28738420Smckusick 			len = mp->m_len;
28838420Smckusick 			mp->m_len = xfer;
28938420Smckusick 			left -= xfer;
29038420Smckusick 			uiocp += xfer;
29139585Smckusick 			uiop->uio_offset += xfer;
29238420Smckusick 			uiop->uio_resid -= xfer;
29338420Smckusick 		}
29438420Smckusick 		if (uiop->uio_iov->iov_len <= siz) {
29538420Smckusick 			uiop->uio_iovcnt--;
29638420Smckusick 			uiop->uio_iov++;
29738420Smckusick 		} else {
29838420Smckusick 			uiop->uio_iov->iov_base += uiosiz;
29938420Smckusick 			uiop->uio_iov->iov_len -= uiosiz;
30038420Smckusick 		}
30138420Smckusick 		siz -= uiosiz;
30238420Smckusick 	}
30338420Smckusick 	if (rem > 0) {
30438420Smckusick 		if (rem > (len-mp->m_len)) {
30538420Smckusick 			MGET(mp, M_WAIT, MT_DATA);
30638420Smckusick 			mp->m_len = 0;
30738420Smckusick 			mp2->m_next = mp;
30838420Smckusick 		}
30938420Smckusick 		cp = mtod(mp, caddr_t)+mp->m_len;
31038420Smckusick 		for (left = 0; left < rem; left++)
31138420Smckusick 			*cp++ = '\0';
31238420Smckusick 		mp->m_len += rem;
31338420Smckusick 		*bpos = cp;
31438420Smckusick 	} else
31538420Smckusick 		*bpos = mtod(mp, caddr_t)+mp->m_len;
31638420Smckusick 	*mq = mp;
31738420Smckusick 	return(0);
31838420Smckusick }
31938420Smckusick 
32038420Smckusick /*
32138420Smckusick  * Help break down an mbuf chain by setting the first siz bytes contiguous
32238420Smckusick  * pointed to by returned val.
32338420Smckusick  * If Updateflg == True we can overwrite the first part of the mbuf data
32438420Smckusick  * This is used by the macros nfsm_disect and nfsm_disecton for tough
32538420Smckusick  * cases. (The macros use the vars. dpos and dpos2)
32638420Smckusick  */
32738420Smckusick nfsm_disct(mdp, dposp, siz, left, updateflg, cp2)
32838420Smckusick 	struct mbuf **mdp;
32938420Smckusick 	caddr_t *dposp;
33038420Smckusick 	int siz;
33138420Smckusick 	int left;
33238420Smckusick 	int updateflg;
33338420Smckusick 	caddr_t *cp2;
33438420Smckusick {
33538420Smckusick 	register struct mbuf *mp, *mp2;
33638420Smckusick 	register int siz2, xfer;
33738420Smckusick 	register caddr_t p;
33838420Smckusick 
33938420Smckusick 	mp = *mdp;
34038420Smckusick 	while (left == 0) {
34138420Smckusick 		*mdp = mp = mp->m_next;
34238420Smckusick 		if (mp == NULL)
34338420Smckusick 			return(EBADRPC);
34438420Smckusick 		left = mp->m_len;
34538420Smckusick 		*dposp = mtod(mp, caddr_t);
34638420Smckusick 	}
34738420Smckusick 	if (left >= siz) {
34838420Smckusick 		*cp2 = *dposp;
34938420Smckusick 		*dposp += siz;
35038420Smckusick 		return(0);
35138420Smckusick 	} else if (mp->m_next == NULL) {
35238420Smckusick 		return(EBADRPC);
35338420Smckusick 	} else if (siz > MCLBYTES) {
35438420Smckusick 		panic("nfs S too big");
35538420Smckusick 	} else {
35638420Smckusick 		/* Iff update, you can overwrite, else must alloc new mbuf */
35738420Smckusick 		if (updateflg) {
35838420Smckusick 			NFSMINOFF(mp);
35938420Smckusick 		} else {
36038420Smckusick 			MGET(mp2, M_WAIT, MT_DATA);
36138420Smckusick 			mp2->m_next = mp->m_next;
36238420Smckusick 			mp->m_next = mp2;
36338420Smckusick 			mp->m_len -= left;
36438420Smckusick 			mp = mp2;
36538420Smckusick 		}
36638420Smckusick 		/* Alloc cluster iff we need it */
36738420Smckusick 		if (!M_HASCL(mp) && siz > NFSMSIZ(mp)) {
36838420Smckusick 			NFSMCLGET(mp, M_WAIT);
36938420Smckusick 			if (!M_HASCL(mp))
37038420Smckusick 				return(ENOBUFS);
37138420Smckusick 		}
37238420Smckusick 		*cp2 = p = mtod(mp, caddr_t);
37338420Smckusick 		bcopy(*dposp, p, left);		/* Copy what was left */
37438420Smckusick 		siz2 = siz-left;
37538420Smckusick 		p += left;
37638420Smckusick 		mp2 = mp->m_next;
37738420Smckusick 		/* Loop arround copying up the siz2 bytes */
37838420Smckusick 		while (siz2 > 0) {
37938420Smckusick 			if (mp2 == NULL)
38038420Smckusick 				return (EBADRPC);
38138420Smckusick 			xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
38238420Smckusick 			bcopy(mtod(mp2, caddr_t), p, xfer);
38338420Smckusick 			NFSMADV(mp2, xfer);
38438420Smckusick 			mp2->m_len -= xfer;
38538420Smckusick 			siz2 -= xfer;
38638420Smckusick 			if (siz2 > 0)
38738420Smckusick 				mp2 = mp2->m_next;
38838420Smckusick 		}
38938420Smckusick 		mp->m_len = siz;
39038420Smckusick 		*mdp = mp2;
39138420Smckusick 		*dposp = mtod(mp2, caddr_t);
39238420Smckusick 	}
39339494Smckusick 	return (0);
39438420Smckusick }
39538420Smckusick 
39638420Smckusick /*
39738420Smckusick  * Advance the position in the mbuf chain with/without freeing mbufs
39838420Smckusick  */
39938420Smckusick nfs_adv(mdp, dposp, offs, left)
40038420Smckusick 	struct mbuf **mdp;
40138420Smckusick 	caddr_t *dposp;
40238420Smckusick 	int offs;
40338420Smckusick 	int left;
40438420Smckusick {
40538420Smckusick 	register struct mbuf *m;
40638420Smckusick 	register int s;
40738420Smckusick 
40838420Smckusick 	m = *mdp;
40938420Smckusick 	s = left;
41038420Smckusick 	while (s < offs) {
41138420Smckusick 		offs -= s;
41238420Smckusick 		m = m->m_next;
41338420Smckusick 		if (m == NULL)
41438420Smckusick 			return(EBADRPC);
41538420Smckusick 		s = m->m_len;
41638420Smckusick 	}
41738420Smckusick 	*mdp = m;
41838420Smckusick 	*dposp = mtod(m, caddr_t)+offs;
41938420Smckusick 	return(0);
42038420Smckusick }
42138420Smckusick 
42238420Smckusick /*
42338420Smckusick  * Copy a string into mbufs for the hard cases...
42438420Smckusick  */
42538420Smckusick nfsm_strtmbuf(mb, bpos, cp, siz)
42638420Smckusick 	struct mbuf **mb;
42738420Smckusick 	char **bpos;
42838420Smckusick 	char *cp;
42938420Smckusick 	long siz;
43038420Smckusick {
43138420Smckusick 	register struct mbuf *m1, *m2;
43238420Smckusick 	long left, xfer, len, tlen;
43338420Smckusick 	u_long *p;
43438420Smckusick 	int putsize;
43538420Smckusick 
43638420Smckusick 	putsize = 1;
43738420Smckusick 	m2 = *mb;
43838420Smckusick 	left = NFSMSIZ(m2)-m2->m_len;
43938420Smckusick 	if (left > 0) {
44038420Smckusick 		p = ((u_long *)(*bpos));
44138420Smckusick 		*p++ = txdr_unsigned(siz);
44238420Smckusick 		putsize = 0;
44338420Smckusick 		left -= NFSX_UNSIGNED;
44438420Smckusick 		m2->m_len += NFSX_UNSIGNED;
44538420Smckusick 		if (left > 0) {
44638420Smckusick 			bcopy(cp, (caddr_t) p, left);
44738420Smckusick 			siz -= left;
44838420Smckusick 			cp += left;
44938420Smckusick 			m2->m_len += left;
45038420Smckusick 			left = 0;
45138420Smckusick 		}
45238420Smckusick 	}
45338420Smckusick 	/* Loop arround adding mbufs */
45438420Smckusick 	while (siz > 0) {
45538420Smckusick 		MGET(m1, M_WAIT, MT_DATA);
45638420Smckusick 		if (siz > MLEN)
45738420Smckusick 			NFSMCLGET(m1, M_WAIT);
45838420Smckusick 		m1->m_len = NFSMSIZ(m1);
45938420Smckusick 		m2->m_next = m1;
46038420Smckusick 		m2 = m1;
46138420Smckusick 		p = mtod(m1, u_long *);
46238420Smckusick 		tlen = 0;
46338420Smckusick 		if (putsize) {
46438420Smckusick 			*p++ = txdr_unsigned(siz);
46538420Smckusick 			m1->m_len -= NFSX_UNSIGNED;
46638420Smckusick 			tlen = NFSX_UNSIGNED;
46738420Smckusick 			putsize = 0;
46838420Smckusick 		}
46938420Smckusick 		if (siz < m1->m_len) {
47038420Smckusick 			len = nfsm_rndup(siz);
47138420Smckusick 			xfer = siz;
47238420Smckusick 			if (xfer < len)
47338420Smckusick 				*(p+(xfer>>2)) = 0;
47438420Smckusick 		} else {
47538420Smckusick 			xfer = len = m1->m_len;
47638420Smckusick 		}
47738420Smckusick 		bcopy(cp, (caddr_t) p, xfer);
47838420Smckusick 		m1->m_len = len+tlen;
47938420Smckusick 		siz -= xfer;
48038420Smckusick 		cp += xfer;
48138420Smckusick 	}
48238420Smckusick 	*mb = m1;
48338420Smckusick 	*bpos = mtod(m1, caddr_t)+m1->m_len;
48438420Smckusick 	return(0);
48538420Smckusick }
48638420Smckusick 
48738420Smckusick /*
48838420Smckusick  * Called once to initialize data structures...
48938420Smckusick  */
49039444Smckusick nfs_init()
49138420Smckusick {
49238420Smckusick 	register int i;
49338420Smckusick 
49438420Smckusick 	rpc_vers = txdr_unsigned(RPC_VER2);
49538420Smckusick 	rpc_call = txdr_unsigned(RPC_CALL);
49638420Smckusick 	rpc_reply = txdr_unsigned(RPC_REPLY);
49738420Smckusick 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
49838420Smckusick 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
49938420Smckusick 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
50038420Smckusick 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
50138420Smckusick 	nfs_vers = txdr_unsigned(NFS_VER2);
50238420Smckusick 	nfs_prog = txdr_unsigned(NFS_PROG);
50338420Smckusick 	nfs_true = txdr_unsigned(TRUE);
50438420Smckusick 	nfs_false = txdr_unsigned(FALSE);
50538420Smckusick 	/* Loop thru nfs procids */
50638420Smckusick 	for (i = 0; i < NFS_NPROCS; i++)
50738420Smckusick 		nfs_procids[i] = txdr_unsigned(i);
50839345Smckusick 	/* Ensure async daemons disabled */
50939345Smckusick 	for (i = 0; i < MAX_ASYNCDAEMON; i++)
51039345Smckusick 		nfs_iodwant[i] = (struct proc *)0;
51138420Smckusick 	v_type[0] = VNON;
51238420Smckusick 	v_type[1] = VREG;
51338420Smckusick 	v_type[2] = VDIR;
51438420Smckusick 	v_type[3] = VBLK;
51538420Smckusick 	v_type[4] = VCHR;
51638420Smckusick 	v_type[5] = VLNK;
51738420Smckusick 	nfs_xdrneg1 = txdr_unsigned(-1);
51838420Smckusick 	nfs_nhinit();			/* Init the nfsnode table */
51939755Smckusick 	nfsrv_initcache();		/* Init the server request cache */
52038884Smacklem 	rminit(nfsmap, (long)NFS_MAPREG, (long)1, "nfs mapreg", NFS_MSIZ);
52138420Smckusick 	/* And start timer */
52238420Smckusick 	nfs_timer();
52338420Smckusick }
52438420Smckusick 
52538420Smckusick /*
52638420Smckusick  * Fill in the rest of the rpc_unixauth and return it
52738420Smckusick  */
52838420Smckusick static char *nfs_unixauth(cr)
52938420Smckusick 	register struct ucred *cr;
53038420Smckusick {
53138420Smckusick 	register u_long *p;
53238420Smckusick 	register int i;
53338420Smckusick 	int ngr;
53438420Smckusick 
53538420Smckusick 	/* Maybe someday there should be a cache of AUTH_SHORT's */
53638420Smckusick 	if ((p = rpc_uidp) == NULL) {
53738425Smckusick #ifdef FILLINHOST
53838420Smckusick 		i = nfsm_rndup(hostnamelen)+(19*NFSX_UNSIGNED);
53938425Smckusick #else
54038425Smckusick 		i = 19*NFSX_UNSIGNED;
54138425Smckusick #endif
54238420Smckusick 		MALLOC(p, u_long *, i, M_TEMP, M_WAITOK);
54338420Smckusick 		bzero((caddr_t)p, i);
54438420Smckusick 		rpc_unixauth = (caddr_t)p;
54538420Smckusick 		*p++ = txdr_unsigned(RPCAUTH_UNIX);
54638420Smckusick 		p++;	/* Fill in size later */
54738420Smckusick 		*p++ = hostid;
54838425Smckusick #ifdef FILLINHOST
54938420Smckusick 		*p++ = txdr_unsigned(hostnamelen);
55038420Smckusick 		i = nfsm_rndup(hostnamelen);
55138420Smckusick 		bcopy(hostname, (caddr_t)p, hostnamelen);
55238420Smckusick 		p += (i>>2);
55338425Smckusick #else
55438425Smckusick 		*p++ = 0;
55538425Smckusick #endif
55638420Smckusick 		rpc_uidp = p;
55738420Smckusick 	}
55838420Smckusick 	*p++ = txdr_unsigned(cr->cr_uid);
55938420Smckusick 	*p++ = txdr_unsigned(cr->cr_groups[0]);
56038737Smckusick 	ngr = (cr->cr_ngroups > numgrps) ? numgrps : cr->cr_ngroups;
56138420Smckusick 	*p++ = txdr_unsigned(ngr);
56238420Smckusick 	for (i = 0; i < ngr; i++)
56338420Smckusick 		*p++ = txdr_unsigned(cr->cr_groups[i]);
56438420Smckusick 	/* And add the AUTH_NULL */
56538420Smckusick 	*p++ = 0;
56638420Smckusick 	*p = 0;
56738420Smckusick 	i = (((caddr_t)p)-rpc_unixauth)-12;
56838420Smckusick 	p = (u_long *)(rpc_unixauth+4);
56938420Smckusick 	*p = txdr_unsigned(i);
57038420Smckusick 	return(rpc_unixauth);
57138420Smckusick }
57238420Smckusick 
57338420Smckusick /*
57438420Smckusick  * Attribute cache routines.
57538420Smckusick  * nfs_loadattrcache() - loads or updates the cache contents from attributes
57638420Smckusick  *	that are on the mbuf list
57738420Smckusick  * nfs_getattrcache() - returns valid attributes if found in cache, returns
57838420Smckusick  *	error otherwise
57938420Smckusick  */
58038420Smckusick 
58138420Smckusick /*
58239444Smckusick  * Load the attribute cache (that lives in the nfsnode entry) with
58338420Smckusick  * the values on the mbuf list and
58438420Smckusick  * Iff vap not NULL
58538420Smckusick  *    copy the attributes to *vaper
58638420Smckusick  */
58739457Smckusick nfs_loadattrcache(vpp, mdp, dposp, vaper)
58839457Smckusick 	struct vnode **vpp;
58938420Smckusick 	struct mbuf **mdp;
59038420Smckusick 	caddr_t *dposp;
59138420Smckusick 	struct vattr *vaper;
59238420Smckusick {
59339457Smckusick 	register struct vnode *vp = *vpp;
59438420Smckusick 	register struct vattr *vap;
59538884Smacklem 	register struct nfsv2_fattr *fp;
59639444Smckusick 	extern struct vnodeops spec_nfsv2nodeops;
59739457Smckusick 	register struct nfsnode *np;
59839494Smckusick 	register long t1;
59939494Smckusick 	caddr_t dpos, cp2;
60039494Smckusick 	int error = 0;
60139494Smckusick 	struct mbuf *md;
60239444Smckusick 	enum vtype type;
60339444Smckusick 	dev_t rdev;
60439444Smckusick 	struct timeval mtime;
60539444Smckusick 	struct vnode *nvp;
60638420Smckusick 
60738420Smckusick 	md = *mdp;
60838420Smckusick 	dpos = *dposp;
60938420Smckusick 	t1 = (mtod(md, caddr_t)+md->m_len)-dpos;
61038420Smckusick 	if (error = nfsm_disct(&md, &dpos, NFSX_FATTR, t1, TRUE, &cp2))
61138420Smckusick 		return (error);
61238884Smacklem 	fp = (struct nfsv2_fattr *)cp2;
61339444Smckusick 	type = nfstov_type(fp->fa_type);
61439444Smckusick 	rdev = fxdr_unsigned(dev_t, fp->fa_rdev);
61539444Smckusick 	fxdr_time(&fp->fa_mtime, &mtime);
61639444Smckusick 	/*
61739444Smckusick 	 * If v_type == VNON it is a new node, so fill in the v_type,
61839444Smckusick 	 * n_mtime fields. Check to see if it represents a special
61939444Smckusick 	 * device, and if so, check for a possible alias. Once the
62039444Smckusick 	 * correct vnode has been obtained, fill in the rest of the
62139444Smckusick 	 * information.
62239444Smckusick 	 */
62338420Smckusick 	np = VTONFS(vp);
62439444Smckusick 	if (vp->v_type == VNON) {
62539444Smckusick 		vp->v_type = type;
62639444Smckusick 		if (vp->v_type == VCHR || vp->v_type == VBLK) {
62739444Smckusick 			vp->v_op = &spec_nfsv2nodeops;
62839618Smckusick 			if (nvp = checkalias(vp, rdev, vp->v_mount)) {
62939444Smckusick 				/*
63039444Smckusick 				 * Reinitialize aliased node.
63139444Smckusick 				 */
63239444Smckusick 				np = VTONFS(nvp);
63339444Smckusick 				np->n_vnode = nvp;
634*39907Smckusick 				np->n_flag = 0;
635*39907Smckusick 				nfs_lock(nvp);
63639444Smckusick 				bcopy((caddr_t)&VTONFS(vp)->n_fh,
63739444Smckusick 					(caddr_t)&np->n_fh, NFSX_FH);
63839444Smckusick 				insque(np, nfs_hash(&np->n_fh));
63939444Smckusick 				np->n_attrstamp = 0;
64039444Smckusick 				np->n_sillyrename = (struct sillyrename *)0;
64139444Smckusick 				/*
64239457Smckusick 				 * Discard unneeded vnode and update actual one
64339444Smckusick 				 */
64439444Smckusick 				vput(vp);
64539457Smckusick 				*vpp = nvp;
64639444Smckusick 			}
64739444Smckusick 		}
64839444Smckusick 		np->n_mtime = mtime.tv_sec;
64939444Smckusick 	}
65038420Smckusick 	vap = &np->n_vattr;
65139444Smckusick 	vap->va_type = type;
65238884Smacklem 	vap->va_mode = nfstov_mode(fp->fa_mode);
65338884Smacklem 	vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
65438884Smacklem 	vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
65538884Smacklem 	vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
65638884Smacklem 	vap->va_size = fxdr_unsigned(u_long, fp->fa_size);
65738884Smacklem 	if ((np->n_flag & NMODIFIED) == 0 || vap->va_size > np->n_size)
65838884Smacklem 		np->n_size = vap->va_size;
65938420Smckusick 	vap->va_size1 = 0;		/* OR -1 ?? */
66038884Smacklem 	vap->va_blocksize = fxdr_unsigned(long, fp->fa_blocksize);
66139444Smckusick 	vap->va_rdev = rdev;
66238884Smacklem 	vap->va_bytes = fxdr_unsigned(long, fp->fa_blocks) * vap->va_blocksize;
66338884Smacklem 	vap->va_bytes1 = 0;
66439755Smckusick 	vap->va_fsid = vp->v_mount->m_fsid.val[0];
66538884Smacklem 	vap->va_fileid = fxdr_unsigned(long, fp->fa_fileid);
66639755Smckusick 	vap->va_atime.tv_sec = fxdr_unsigned(long, fp->fa_atime.tv_sec);
66739755Smckusick 	vap->va_atime.tv_usec = 0;
66839755Smckusick 	vap->va_flags = fxdr_unsigned(u_long, fp->fa_atime.tv_usec);
66939444Smckusick 	vap->va_mtime = mtime;
67039755Smckusick 	vap->va_ctime.tv_sec = fxdr_unsigned(long, fp->fa_ctime.tv_sec);
67139755Smckusick 	vap->va_ctime.tv_usec = 0;
67239755Smckusick 	vap->va_gen = fxdr_unsigned(u_long, fp->fa_ctime.tv_usec);
67338420Smckusick 	np->n_attrstamp = time.tv_sec;
67438420Smckusick 	*dposp = dpos;
67538420Smckusick 	*mdp = md;
67638884Smacklem 	if (vaper != NULL) {
67738420Smckusick 		bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
67838884Smacklem 		if ((np->n_flag & NMODIFIED) && (np->n_size > vap->va_size))
67938884Smacklem 			vaper->va_size = np->n_size;
68038884Smacklem 	}
68138420Smckusick 	return (0);
68238420Smckusick }
68338420Smckusick 
68438420Smckusick /*
68538420Smckusick  * Check the time stamp
68638420Smckusick  * If the cache is valid, copy contents to *vap and return 0
68738420Smckusick  * otherwise return an error
68838420Smckusick  */
68938420Smckusick nfs_getattrcache(vp, vap)
69038420Smckusick 	register struct vnode *vp;
69138420Smckusick 	struct vattr *vap;
69238420Smckusick {
69338420Smckusick 	register struct nfsnode *np;
69438420Smckusick 
69538420Smckusick 	np = VTONFS(vp);
69638420Smckusick 	if ((time.tv_sec-np->n_attrstamp) < NFS_ATTRTIMEO) {
69738420Smckusick 		nfsstats.attrcache_hits++;
69838420Smckusick 		bcopy((caddr_t)&np->n_vattr,(caddr_t)vap,sizeof(struct vattr));
69939361Smckusick 		if ((np->n_flag & NMODIFIED) == 0)
70039361Smckusick 			np->n_size = vap->va_size;
70139361Smckusick 		else if (np->n_size > vap->va_size)
70238884Smacklem 			vap->va_size = np->n_size;
70338420Smckusick 		return (0);
70438420Smckusick 	} else {
70538420Smckusick 		nfsstats.attrcache_misses++;
70638420Smckusick 		return (ENOENT);
70738420Smckusick 	}
70838420Smckusick }
70938420Smckusick 
71038420Smckusick /*
71138420Smckusick  * nfs_namei - a liitle like namei(), but for one element only
71238420Smckusick  *	essentially look up file handle, fill in ndp and call VOP_LOOKUP()
71338420Smckusick  */
71438420Smckusick nfs_namei(ndp, fhp, len, mdp, dposp)
71538420Smckusick 	register struct nameidata *ndp;
71638420Smckusick 	fhandle_t *fhp;
71738420Smckusick 	int len;
71838420Smckusick 	struct mbuf **mdp;
71938420Smckusick 	caddr_t *dposp;
72038420Smckusick {
72138420Smckusick 	register int i, rem;
72238420Smckusick 	register struct mbuf *md;
72338420Smckusick 	register char *cp;
72438420Smckusick 	struct vnode *dp = (struct vnode *)0;
72538420Smckusick 	int flag;
72638420Smckusick 	int docache;
72738420Smckusick 	int wantparent;
72838420Smckusick 	int lockparent;
72938420Smckusick 	int error = 0;
73038420Smckusick 
73138420Smckusick 	ndp->ni_vp = ndp->ni_dvp = (struct vnode *)0;
73238420Smckusick 	flag = ndp->ni_nameiop & OPFLAG;
73338420Smckusick 	wantparent = ndp->ni_nameiop & (LOCKPARENT | WANTPARENT);
73438420Smckusick 	lockparent = ndp->ni_nameiop & LOCKPARENT;
73538420Smckusick 	docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE;
73638420Smckusick 	if (flag == DELETE || wantparent)
73738420Smckusick 		docache = 0;
73838420Smckusick 
73938420Smckusick 	/* Fill in the nameidata and call lookup */
74038420Smckusick 	cp = *dposp;
74138420Smckusick 	md = *mdp;
74238420Smckusick 	rem = mtod(md, caddr_t)+md->m_len-cp;
74338420Smckusick 	ndp->ni_hash = 0;
74438420Smckusick 	for (i = 0; i < len;) {
74538420Smckusick 		if (rem == 0) {
74638420Smckusick 			md = md->m_next;
74738420Smckusick 			if (md == NULL)
74838420Smckusick 				return (EBADRPC);
74938420Smckusick 			cp = mtod(md, caddr_t);
75038420Smckusick 			rem = md->m_len;
75138420Smckusick 		}
75238420Smckusick 		if (*cp == '\0' || *cp == '/')
75338420Smckusick 			return (EINVAL);
75438420Smckusick 		if (*cp & 0200)
75538420Smckusick 			if ((*cp&0377) == ('/'|0200) || flag != DELETE)
75638420Smckusick 				return (EINVAL);
75738420Smckusick 		ndp->ni_dent.d_name[i++] = *cp;
75838420Smckusick 		ndp->ni_hash += (unsigned char)*cp * i;
75938420Smckusick 		cp++;
76038420Smckusick 		rem--;
76138420Smckusick 	}
76238420Smckusick 	*mdp = md;
76338420Smckusick 	len = nfsm_rndup(len)-len;
76438420Smckusick 	if (len > 0)
76538420Smckusick 		*dposp = cp+len;
76638420Smckusick 	else
76738420Smckusick 		*dposp = cp;
76838420Smckusick 	ndp->ni_namelen = i;
76938420Smckusick 	ndp->ni_dent.d_namlen = i;
77038420Smckusick 	ndp->ni_dent.d_name[i] = '\0';
77138425Smckusick 	ndp->ni_pathlen = 1;
77238420Smckusick 	ndp->ni_dirp = ndp->ni_ptr = &ndp->ni_dent.d_name[0];
77338420Smckusick 	ndp->ni_next = &ndp->ni_dent.d_name[i];
77438420Smckusick 	ndp->ni_loopcnt = 0;	/* Not actually used for now */
77538420Smckusick 	ndp->ni_endoff = 0;
77638420Smckusick 	if (docache)
77738420Smckusick 		ndp->ni_makeentry = 1;
77838420Smckusick 	else
77938420Smckusick 		ndp->ni_makeentry = 0;
78038420Smckusick 	ndp->ni_isdotdot = (i == 2 &&
78138420Smckusick 		ndp->ni_dent.d_name[1] == '.' && ndp->ni_dent.d_name[0] == '.');
78238420Smckusick 
78338420Smckusick 	if (error = nfsrv_fhtovp(fhp, TRUE, &dp, ndp->ni_cred))
78438420Smckusick 		return (error);
78538425Smckusick 	if (dp->v_type != VDIR) {
78638425Smckusick 		vput(dp);
78738425Smckusick 		return (ENOTDIR);
78838425Smckusick 	}
78939345Smckusick 	/*
79039345Smckusick 	 * Must set current directory here to avoid confusion in namei()
79139345Smckusick 	 * called from rename()
79239345Smckusick 	 */
79338425Smckusick 	ndp->ni_cdir = dp;
79438425Smckusick 	ndp->ni_rdir = (struct vnode *)0;
79538420Smckusick 
79638420Smckusick 	/*
79738425Smckusick 	 * Handle "..":
79838425Smckusick 	 * If this vnode is the root of the mounted
79938425Smckusick 	 *    file system, then ignore it so can't get out
80038420Smckusick 	 */
80138425Smckusick 	if (ndp->ni_isdotdot && (dp->v_flag & VROOT)) {
80238425Smckusick 		ndp->ni_dvp = dp;
80338425Smckusick 		ndp->ni_vp = dp;
80438425Smckusick 		VREF(dp);
80538425Smckusick 		goto nextname;
80638420Smckusick 	}
80738420Smckusick 
80838420Smckusick 	/*
80938420Smckusick 	 * We now have a segment name to search for, and a directory to search.
81038420Smckusick 	 */
81138420Smckusick 	if (error = VOP_LOOKUP(dp, ndp)) {
81238420Smckusick 		if (ndp->ni_vp != NULL)
81338420Smckusick 			panic("leaf should be empty");
81438420Smckusick 		/*
81538420Smckusick 		 * If creating and at end of pathname, then can consider
81638420Smckusick 		 * allowing file to be created.
81738420Smckusick 		 */
81838420Smckusick 		if (ndp->ni_dvp->v_mount->m_flag & (M_RDONLY | M_EXRDONLY))
81938420Smckusick 			error = EROFS;
82038420Smckusick 		if (flag == LOOKUP || flag == DELETE || error != ENOENT)
82138420Smckusick 			goto bad;
82238420Smckusick 		/*
82338420Smckusick 		 * We return with ni_vp NULL to indicate that the entry
82438420Smckusick 		 * doesn't currently exist, leaving a pointer to the
82538420Smckusick 		 * (possibly locked) directory inode in ndp->ni_dvp.
82638420Smckusick 		 */
82738420Smckusick 		return (0);	/* should this be ENOENT? */
82838420Smckusick 	}
82938420Smckusick 
83038420Smckusick 	dp = ndp->ni_vp;
83138420Smckusick 
83238420Smckusick nextname:
83338425Smckusick 	ndp->ni_ptr = ndp->ni_next;
83438420Smckusick 	/*
83538425Smckusick 	 * Check for read-only file systems
83638420Smckusick 	 */
83738421Smckusick 	if (flag == DELETE || flag == RENAME) {
83838421Smckusick 		/*
83938421Smckusick 		 * Disallow directory write attempts on read-only
84038421Smckusick 		 * file systems.
84138421Smckusick 		 */
84238421Smckusick 		if ((dp->v_mount->m_flag & (M_RDONLY|M_EXRDONLY)) ||
84338425Smckusick 		    (wantparent && (ndp->ni_dvp->v_mount->m_flag & (M_RDONLY|M_EXRDONLY)))) {
84438421Smckusick 			error = EROFS;
84538421Smckusick 			goto bad2;
84638421Smckusick 		}
84738421Smckusick 	}
84838420Smckusick 
84938420Smckusick 	if (!wantparent)
85038420Smckusick 		vrele(ndp->ni_dvp);
85138420Smckusick 
85238420Smckusick 	if ((ndp->ni_nameiop & LOCKLEAF) == 0)
85338420Smckusick 		VOP_UNLOCK(dp);
85438420Smckusick 	return (0);
85538420Smckusick 
85638420Smckusick bad2:
85738420Smckusick 	if (lockparent)
85838420Smckusick 		VOP_UNLOCK(ndp->ni_dvp);
85938420Smckusick 	vrele(ndp->ni_dvp);
86038420Smckusick bad:
86138420Smckusick 	vput(dp);
86238420Smckusick 	ndp->ni_vp = NULL;
86338420Smckusick 	return (error);
86438420Smckusick }
86538420Smckusick 
86638420Smckusick /*
86738420Smckusick  * A fiddled version of m_adj() that ensures null fill to a long
86838420Smckusick  * boundary and only trims off the back end
86938420Smckusick  */
87038420Smckusick nfsm_adj(mp, len, nul)
87138420Smckusick 	struct mbuf *mp;
87238420Smckusick 	register int len;
87338420Smckusick 	int nul;
87438420Smckusick {
87538420Smckusick 	register struct mbuf *m;
87638420Smckusick 	register int count, i;
87738420Smckusick 	register char *cp;
87838420Smckusick 
87938420Smckusick 	/*
88038420Smckusick 	 * Trim from tail.  Scan the mbuf chain,
88138420Smckusick 	 * calculating its length and finding the last mbuf.
88238420Smckusick 	 * If the adjustment only affects this mbuf, then just
88338420Smckusick 	 * adjust and return.  Otherwise, rescan and truncate
88438420Smckusick 	 * after the remaining size.
88538420Smckusick 	 */
88638420Smckusick 	count = 0;
88738420Smckusick 	m = mp;
88838420Smckusick 	for (;;) {
88938420Smckusick 		count += m->m_len;
89038420Smckusick 		if (m->m_next == (struct mbuf *)0)
89138420Smckusick 			break;
89238420Smckusick 		m = m->m_next;
89338420Smckusick 	}
89438579Smckusick 	if (m->m_len > len) {
89538420Smckusick 		m->m_len -= len;
89638420Smckusick 		if (nul > 0) {
89738420Smckusick 			cp = mtod(m, caddr_t)+m->m_len-nul;
89838420Smckusick 			for (i = 0; i < nul; i++)
89938420Smckusick 				*cp++ = '\0';
90038420Smckusick 		}
90138420Smckusick 		return;
90238420Smckusick 	}
90338420Smckusick 	count -= len;
90438420Smckusick 	if (count < 0)
90538420Smckusick 		count = 0;
90638420Smckusick 	/*
90738420Smckusick 	 * Correct length for chain is "count".
90838420Smckusick 	 * Find the mbuf with last data, adjust its length,
90938420Smckusick 	 * and toss data from remaining mbufs on chain.
91038420Smckusick 	 */
91138420Smckusick 	for (m = mp; m; m = m->m_next) {
91238420Smckusick 		if (m->m_len >= count) {
91338420Smckusick 			m->m_len = count;
91438420Smckusick 			if (nul > 0) {
91538420Smckusick 				cp = mtod(m, caddr_t)+m->m_len-nul;
91638420Smckusick 				for (i = 0; i < nul; i++)
91738420Smckusick 					*cp++ = '\0';
91838420Smckusick 			}
91938420Smckusick 			break;
92038420Smckusick 		}
92138420Smckusick 		count -= m->m_len;
92238420Smckusick 	}
92338420Smckusick 	while (m = m->m_next)
92438420Smckusick 		m->m_len = 0;
92538420Smckusick }
92638420Smckusick 
92738420Smckusick /*
92838420Smckusick  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
92938420Smckusick  * 	- look up fsid in mount list (if not found ret error)
93038420Smckusick  *	- check that it is exported
93138420Smckusick  *	- get vp by calling VFS_FHTOVP() macro
93238420Smckusick  *	- if not lockflag unlock it with VOP_UNLOCK()
93338420Smckusick  *	- if cred->cr_uid == 0 set it to m_exroot
93438420Smckusick  */
93538420Smckusick nfsrv_fhtovp(fhp, lockflag, vpp, cred)
93638420Smckusick 	fhandle_t *fhp;
93738420Smckusick 	int lockflag;
93838420Smckusick 	struct vnode **vpp;
93938420Smckusick 	struct ucred *cred;
94038420Smckusick {
94138420Smckusick 	register struct mount *mp;
94238420Smckusick 
94338420Smckusick 	if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
94438420Smckusick 		return (ESTALE);
94538420Smckusick 	if ((mp->m_flag & M_EXPORTED) == 0)
94638420Smckusick 		return (EACCES);
94738420Smckusick 	if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
94838420Smckusick 		return (ESTALE);
94938420Smckusick 	if (cred->cr_uid == 0)
95038420Smckusick 		cred->cr_uid = mp->m_exroot;
95138420Smckusick 	if (!lockflag)
95238420Smckusick 		VOP_UNLOCK(*vpp);
95338420Smckusick 	return (0);
95438420Smckusick }
955