xref: /csrg-svn/sys/nfs/nfs_subs.c (revision 45288)
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  *
844513Sbostic  * %sccs.include.redist.c%
938420Smckusick  *
10*45288Smckusick  *	@(#)nfs_subs.c	7.31 (Berkeley) 10/02/90
1138420Smckusick  */
1238420Smckusick 
1338420Smckusick /*
1438420Smckusick  * These functions support the macros and help fiddle mbuf chains for
1538420Smckusick  * the nfs op functions. They do things like create the rpc header and
1638420Smckusick  * copy data between mbuf chains and uio lists.
1738420Smckusick  */
1838420Smckusick #include "param.h"
1939345Smckusick #include "user.h"
2039345Smckusick #include "proc.h"
2140136Smckusick #include "systm.h"
2240136Smckusick #include "kernel.h"
2338420Smckusick #include "mount.h"
2438420Smckusick #include "file.h"
2538420Smckusick #include "vnode.h"
2640136Smckusick #include "mbuf.h"
2740136Smckusick #include "errno.h"
2838884Smacklem #include "map.h"
2938420Smckusick #include "rpcv2.h"
3038420Smckusick #include "nfsv2.h"
3138420Smckusick #include "nfsnode.h"
3238420Smckusick #include "nfs.h"
3338884Smacklem #include "nfsiom.h"
3438420Smckusick #include "xdr_subs.h"
3538420Smckusick #include "nfsm_subs.h"
3645282Smckusick #include "nfscompress.h"
3738420Smckusick 
3838420Smckusick #define TRUE	1
3938420Smckusick #define	FALSE	0
4038420Smckusick 
4138420Smckusick /*
4238420Smckusick  * Data items converted to xdr at startup, since they are constant
4338420Smckusick  * This is kinda hokey, but may save a little time doing byte swaps
4438420Smckusick  */
4538420Smckusick u_long nfs_procids[NFS_NPROCS];
4638420Smckusick u_long nfs_xdrneg1;
4738420Smckusick u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied,
4838420Smckusick 	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
4938420Smckusick u_long nfs_vers, nfs_prog, nfs_true, nfs_false;
5038420Smckusick /* And other global data */
5138420Smckusick static u_long *rpc_uidp = (u_long *)0;
5238420Smckusick static u_long nfs_xid = 1;
5338420Smckusick static char *rpc_unixauth;
5438420Smckusick extern long hostid;
5542244Smckusick enum vtype ntov_type[7] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON };
5641902Smckusick extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
5738884Smacklem extern struct map nfsmap[NFS_MSIZ];
5841902Smckusick extern struct nfsreq nfsreqh;
5938420Smckusick 
6038420Smckusick /* Function ret types */
6138420Smckusick static char *nfs_unixauth();
6238420Smckusick 
6338420Smckusick /*
6438737Smckusick  * Maximum number of groups passed through to NFS server.
6541902Smckusick  * According to RFC1057 it should be 16.
6638737Smckusick  * For release 3.X systems, the maximum value is 8.
6744987Smckusick  * For some other servers, the maximum value is 10.
6838737Smckusick  */
6938737Smckusick int numgrps = 8;
7038737Smckusick 
7138737Smckusick /*
7238420Smckusick  * Create the header for an rpc request packet
7338420Smckusick  * The function nfs_unixauth() creates a unix style authorization string
7438420Smckusick  * and returns a ptr to it.
7538420Smckusick  * The hsiz is the size of the rest of the nfs request header.
7638420Smckusick  * (just used to decide if a cluster is a good idea)
7739494Smckusick  * nb: Note that the prog, vers and procid args are already in xdr byte order
7838420Smckusick  */
7939494Smckusick struct mbuf *nfsm_reqh(prog, vers, procid, cred, hsiz, bpos, mb, retxid)
8038420Smckusick 	u_long prog;
8138420Smckusick 	u_long vers;
8239494Smckusick 	u_long procid;
8338420Smckusick 	struct ucred *cred;
8438420Smckusick 	int hsiz;
8538420Smckusick 	caddr_t *bpos;
8638420Smckusick 	struct mbuf **mb;
8738420Smckusick 	u_long *retxid;
8838420Smckusick {
8938420Smckusick 	register struct mbuf *mreq, *m;
9038420Smckusick 	register u_long *p;
9138420Smckusick 	struct mbuf *m1;
9238420Smckusick 	char *ap;
9338420Smckusick 	int asiz, siz;
9438420Smckusick 
9538420Smckusick 	NFSMGETHDR(mreq);
9644987Smckusick 	asiz = ((((cred->cr_ngroups - 1) > numgrps) ? numgrps :
9744987Smckusick 		  (cred->cr_ngroups - 1)) << 2);
9838425Smckusick #ifdef FILLINHOST
9938420Smckusick 	asiz += nfsm_rndup(hostnamelen)+(9*NFSX_UNSIGNED);
10038425Smckusick #else
10138425Smckusick 	asiz += 9*NFSX_UNSIGNED;
10238425Smckusick #endif
10338420Smckusick 
10438420Smckusick 	/* If we need a lot, alloc a cluster ?? */
10538420Smckusick 	if ((asiz+hsiz+RPC_SIZ) > MHLEN)
10641902Smckusick 		MCLGET(mreq, M_WAIT);
10738420Smckusick 	mreq->m_len = NFSMSIZ(mreq);
10838420Smckusick 	siz = mreq->m_len;
10938420Smckusick 	m1 = mreq;
11038420Smckusick 	/*
11138420Smckusick 	 * Alloc enough mbufs
11238420Smckusick 	 * We do it now to avoid all sleeps after the call to nfs_unixauth()
11338420Smckusick 	 */
11438420Smckusick 	while ((asiz+RPC_SIZ) > siz) {
11538420Smckusick 		MGET(m, M_WAIT, MT_DATA);
11638420Smckusick 		m1->m_next = m;
11738420Smckusick 		m->m_len = MLEN;
11838420Smckusick 		siz += MLEN;
11938420Smckusick 		m1 = m;
12038420Smckusick 	}
12138420Smckusick 	p = mtod(mreq, u_long *);
12238420Smckusick 	*p++ = *retxid = txdr_unsigned(++nfs_xid);
12338420Smckusick 	*p++ = rpc_call;
12438420Smckusick 	*p++ = rpc_vers;
12538420Smckusick 	*p++ = prog;
12638420Smckusick 	*p++ = vers;
12739494Smckusick 	*p++ = procid;
12838420Smckusick 
12938420Smckusick 	/* Now we can call nfs_unixauth() and copy it in */
13038420Smckusick 	ap = nfs_unixauth(cred);
13138420Smckusick 	m = mreq;
13238420Smckusick 	siz = m->m_len-RPC_SIZ;
13338420Smckusick 	if (asiz <= siz) {
13438420Smckusick 		bcopy(ap, (caddr_t)p, asiz);
13538420Smckusick 		m->m_len = asiz+RPC_SIZ;
13638420Smckusick 	} else {
13738420Smckusick 		bcopy(ap, (caddr_t)p, siz);
13838420Smckusick 		ap += siz;
13938420Smckusick 		asiz -= siz;
14038420Smckusick 		while (asiz > 0) {
14138420Smckusick 			siz = (asiz > MLEN) ? MLEN : asiz;
14238420Smckusick 			m = m->m_next;
14338420Smckusick 			bcopy(ap, mtod(m, caddr_t), siz);
14438420Smckusick 			m->m_len = siz;
14538420Smckusick 			asiz -= siz;
14638420Smckusick 			ap += siz;
14738420Smckusick 		}
14838420Smckusick 	}
14938420Smckusick 
15038420Smckusick 	/* Finally, return values */
15138420Smckusick 	*mb = m;
15238420Smckusick 	*bpos = mtod(m, caddr_t)+m->m_len;
15338420Smckusick 	return (mreq);
15438420Smckusick }
15538420Smckusick 
15638420Smckusick /*
15738420Smckusick  * copies mbuf chain to the uio scatter/gather list
15838420Smckusick  */
15938420Smckusick nfsm_mbuftouio(mrep, uiop, siz, dpos)
16038420Smckusick 	struct mbuf **mrep;
16143353Smckusick 	register struct uio *uiop;
16238420Smckusick 	int siz;
16338420Smckusick 	caddr_t *dpos;
16438420Smckusick {
16543353Smckusick 	register char *mbufcp, *uiocp;
16638420Smckusick 	register int xfer, left, len;
16738420Smckusick 	register struct mbuf *mp;
16838420Smckusick 	long uiosiz, rem;
16941902Smckusick 	int error = 0;
17038420Smckusick 
17138420Smckusick 	mp = *mrep;
17238420Smckusick 	mbufcp = *dpos;
17338420Smckusick 	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
17438420Smckusick 	rem = nfsm_rndup(siz)-siz;
17538420Smckusick 	while (siz > 0) {
17638420Smckusick 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
17741902Smckusick 			return (EFBIG);
17838420Smckusick 		left = uiop->uio_iov->iov_len;
17938420Smckusick 		uiocp = uiop->uio_iov->iov_base;
18038420Smckusick 		if (left > siz)
18138420Smckusick 			left = siz;
18238420Smckusick 		uiosiz = left;
18338420Smckusick 		while (left > 0) {
18438420Smckusick 			while (len == 0) {
18538420Smckusick 				mp = mp->m_next;
18638420Smckusick 				if (mp == NULL)
18738420Smckusick 					return (EBADRPC);
18838420Smckusick 				mbufcp = mtod(mp, caddr_t);
18938420Smckusick 				len = mp->m_len;
19038420Smckusick 			}
19138420Smckusick 			xfer = (left > len) ? len : left;
19238420Smckusick #ifdef notdef
19338420Smckusick 			/* Not Yet.. */
19438420Smckusick 			if (uiop->uio_iov->iov_op != NULL)
19538420Smckusick 				(*(uiop->uio_iov->iov_op))
19638420Smckusick 				(mbufcp, uiocp, xfer);
19738420Smckusick 			else
19838420Smckusick #endif
19938420Smckusick 			if (uiop->uio_segflg == UIO_SYSSPACE)
20038420Smckusick 				bcopy(mbufcp, uiocp, xfer);
20138420Smckusick 			else
20238420Smckusick 				copyout(mbufcp, uiocp, xfer);
20338420Smckusick 			left -= xfer;
20438420Smckusick 			len -= xfer;
20538420Smckusick 			mbufcp += xfer;
20638420Smckusick 			uiocp += xfer;
20739585Smckusick 			uiop->uio_offset += xfer;
20838420Smckusick 			uiop->uio_resid -= xfer;
20938420Smckusick 		}
21038420Smckusick 		if (uiop->uio_iov->iov_len <= siz) {
21138420Smckusick 			uiop->uio_iovcnt--;
21238420Smckusick 			uiop->uio_iov++;
21338420Smckusick 		} else {
21438420Smckusick 			uiop->uio_iov->iov_base += uiosiz;
21538420Smckusick 			uiop->uio_iov->iov_len -= uiosiz;
21638420Smckusick 		}
21738420Smckusick 		siz -= uiosiz;
21838420Smckusick 	}
21938420Smckusick 	*dpos = mbufcp;
22038420Smckusick 	*mrep = mp;
22141902Smckusick 	if (rem > 0) {
22241902Smckusick 		if (len < rem)
22341902Smckusick 			error = nfs_adv(mrep, dpos, rem, len);
22441902Smckusick 		else
22541902Smckusick 			*dpos += rem;
22641902Smckusick 	}
22741902Smckusick 	return (error);
22838420Smckusick }
22938420Smckusick 
23038420Smckusick /*
23138420Smckusick  * copies a uio scatter/gather list to an mbuf chain...
23238420Smckusick  */
23338420Smckusick nfsm_uiotombuf(uiop, mq, siz, bpos)
23438420Smckusick 	register struct uio *uiop;
23538420Smckusick 	struct mbuf **mq;
23638420Smckusick 	int siz;
23738420Smckusick 	caddr_t *bpos;
23838420Smckusick {
23943353Smckusick 	register char *uiocp;
24043353Smckusick 	register struct mbuf *mp, *mp2;
24143353Smckusick 	register int xfer, left, len;
24243353Smckusick 	int uiosiz, clflg, rem;
24343353Smckusick 	char *cp;
24438420Smckusick 
24538420Smckusick 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
24638420Smckusick 		clflg = 1;
24738420Smckusick 	else
24838420Smckusick 		clflg = 0;
24938420Smckusick 	rem = nfsm_rndup(siz)-siz;
25038420Smckusick 	mp2 = *mq;
25138420Smckusick 	while (siz > 0) {
25238420Smckusick 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
25341902Smckusick 			return (EINVAL);
25438420Smckusick 		left = uiop->uio_iov->iov_len;
25538420Smckusick 		uiocp = uiop->uio_iov->iov_base;
25638420Smckusick 		if (left > siz)
25738420Smckusick 			left = siz;
25838420Smckusick 		uiosiz = left;
25938420Smckusick 		while (left > 0) {
26038420Smckusick 			MGET(mp, M_WAIT, MT_DATA);
26138420Smckusick 			if (clflg)
26241902Smckusick 				MCLGET(mp, M_WAIT);
26338420Smckusick 			mp->m_len = NFSMSIZ(mp);
26438420Smckusick 			mp2->m_next = mp;
26538420Smckusick 			mp2 = mp;
26638420Smckusick 			xfer = (left > mp->m_len) ? mp->m_len : left;
26738420Smckusick #ifdef notdef
26838420Smckusick 			/* Not Yet.. */
26938420Smckusick 			if (uiop->uio_iov->iov_op != NULL)
27038420Smckusick 				(*(uiop->uio_iov->iov_op))
27138420Smckusick 				(uiocp, mtod(mp, caddr_t), xfer);
27238420Smckusick 			else
27338420Smckusick #endif
27438420Smckusick 			if (uiop->uio_segflg == UIO_SYSSPACE)
27538420Smckusick 				bcopy(uiocp, mtod(mp, caddr_t), xfer);
27638420Smckusick 			else
27738420Smckusick 				copyin(uiocp, mtod(mp, caddr_t), xfer);
27838420Smckusick 			len = mp->m_len;
27938420Smckusick 			mp->m_len = xfer;
28038420Smckusick 			left -= xfer;
28138420Smckusick 			uiocp += xfer;
28239585Smckusick 			uiop->uio_offset += xfer;
28338420Smckusick 			uiop->uio_resid -= xfer;
28438420Smckusick 		}
28538420Smckusick 		if (uiop->uio_iov->iov_len <= siz) {
28638420Smckusick 			uiop->uio_iovcnt--;
28738420Smckusick 			uiop->uio_iov++;
28838420Smckusick 		} else {
28938420Smckusick 			uiop->uio_iov->iov_base += uiosiz;
29038420Smckusick 			uiop->uio_iov->iov_len -= uiosiz;
29138420Smckusick 		}
29238420Smckusick 		siz -= uiosiz;
29338420Smckusick 	}
29438420Smckusick 	if (rem > 0) {
29538420Smckusick 		if (rem > (len-mp->m_len)) {
29638420Smckusick 			MGET(mp, M_WAIT, MT_DATA);
29738420Smckusick 			mp->m_len = 0;
29838420Smckusick 			mp2->m_next = mp;
29938420Smckusick 		}
30038420Smckusick 		cp = mtod(mp, caddr_t)+mp->m_len;
30138420Smckusick 		for (left = 0; left < rem; left++)
30238420Smckusick 			*cp++ = '\0';
30338420Smckusick 		mp->m_len += rem;
30438420Smckusick 		*bpos = cp;
30538420Smckusick 	} else
30638420Smckusick 		*bpos = mtod(mp, caddr_t)+mp->m_len;
30738420Smckusick 	*mq = mp;
30841902Smckusick 	return (0);
30938420Smckusick }
31038420Smckusick 
31138420Smckusick /*
31238420Smckusick  * Help break down an mbuf chain by setting the first siz bytes contiguous
31338420Smckusick  * pointed to by returned val.
31438420Smckusick  * If Updateflg == True we can overwrite the first part of the mbuf data
31538420Smckusick  * This is used by the macros nfsm_disect and nfsm_disecton for tough
31638420Smckusick  * cases. (The macros use the vars. dpos and dpos2)
31738420Smckusick  */
31838420Smckusick nfsm_disct(mdp, dposp, siz, left, updateflg, cp2)
31938420Smckusick 	struct mbuf **mdp;
32038420Smckusick 	caddr_t *dposp;
32138420Smckusick 	int siz;
32238420Smckusick 	int left;
32338420Smckusick 	int updateflg;
32438420Smckusick 	caddr_t *cp2;
32538420Smckusick {
32638420Smckusick 	register struct mbuf *mp, *mp2;
32738420Smckusick 	register int siz2, xfer;
32838420Smckusick 	register caddr_t p;
32938420Smckusick 
33038420Smckusick 	mp = *mdp;
33138420Smckusick 	while (left == 0) {
33238420Smckusick 		*mdp = mp = mp->m_next;
33338420Smckusick 		if (mp == NULL)
33441902Smckusick 			return (EBADRPC);
33538420Smckusick 		left = mp->m_len;
33638420Smckusick 		*dposp = mtod(mp, caddr_t);
33738420Smckusick 	}
33838420Smckusick 	if (left >= siz) {
33938420Smckusick 		*cp2 = *dposp;
34038420Smckusick 		*dposp += siz;
34138420Smckusick 	} else if (mp->m_next == NULL) {
34241902Smckusick 		return (EBADRPC);
34341902Smckusick 	} else if (siz > MHLEN) {
34438420Smckusick 		panic("nfs S too big");
34538420Smckusick 	} else {
34638420Smckusick 		/* Iff update, you can overwrite, else must alloc new mbuf */
34738420Smckusick 		if (updateflg) {
34838420Smckusick 			NFSMINOFF(mp);
34938420Smckusick 		} else {
35038420Smckusick 			MGET(mp2, M_WAIT, MT_DATA);
35138420Smckusick 			mp2->m_next = mp->m_next;
35238420Smckusick 			mp->m_next = mp2;
35338420Smckusick 			mp->m_len -= left;
35438420Smckusick 			mp = mp2;
35538420Smckusick 		}
35638420Smckusick 		*cp2 = p = mtod(mp, caddr_t);
35738420Smckusick 		bcopy(*dposp, p, left);		/* Copy what was left */
35838420Smckusick 		siz2 = siz-left;
35938420Smckusick 		p += left;
36038420Smckusick 		mp2 = mp->m_next;
36141902Smckusick 		/* Loop around copying up the siz2 bytes */
36238420Smckusick 		while (siz2 > 0) {
36338420Smckusick 			if (mp2 == NULL)
36438420Smckusick 				return (EBADRPC);
36538420Smckusick 			xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
36641902Smckusick 			if (xfer > 0) {
36741902Smckusick 				bcopy(mtod(mp2, caddr_t), p, xfer);
36841902Smckusick 				NFSMADV(mp2, xfer);
36941902Smckusick 				mp2->m_len -= xfer;
37041902Smckusick 				p += xfer;
37141902Smckusick 				siz2 -= xfer;
37241902Smckusick 			}
37338420Smckusick 			if (siz2 > 0)
37438420Smckusick 				mp2 = mp2->m_next;
37538420Smckusick 		}
37638420Smckusick 		mp->m_len = siz;
37738420Smckusick 		*mdp = mp2;
37838420Smckusick 		*dposp = mtod(mp2, caddr_t);
37938420Smckusick 	}
38039494Smckusick 	return (0);
38138420Smckusick }
38238420Smckusick 
38338420Smckusick /*
38441902Smckusick  * Advance the position in the mbuf chain.
38538420Smckusick  */
38638420Smckusick nfs_adv(mdp, dposp, offs, left)
38738420Smckusick 	struct mbuf **mdp;
38838420Smckusick 	caddr_t *dposp;
38938420Smckusick 	int offs;
39038420Smckusick 	int left;
39138420Smckusick {
39238420Smckusick 	register struct mbuf *m;
39338420Smckusick 	register int s;
39438420Smckusick 
39538420Smckusick 	m = *mdp;
39638420Smckusick 	s = left;
39738420Smckusick 	while (s < offs) {
39838420Smckusick 		offs -= s;
39938420Smckusick 		m = m->m_next;
40038420Smckusick 		if (m == NULL)
40141902Smckusick 			return (EBADRPC);
40238420Smckusick 		s = m->m_len;
40338420Smckusick 	}
40438420Smckusick 	*mdp = m;
40538420Smckusick 	*dposp = mtod(m, caddr_t)+offs;
40641902Smckusick 	return (0);
40738420Smckusick }
40838420Smckusick 
40938420Smckusick /*
41038420Smckusick  * Copy a string into mbufs for the hard cases...
41138420Smckusick  */
41238420Smckusick nfsm_strtmbuf(mb, bpos, cp, siz)
41338420Smckusick 	struct mbuf **mb;
41438420Smckusick 	char **bpos;
41538420Smckusick 	char *cp;
41638420Smckusick 	long siz;
41738420Smckusick {
41838420Smckusick 	register struct mbuf *m1, *m2;
41938420Smckusick 	long left, xfer, len, tlen;
42038420Smckusick 	u_long *p;
42138420Smckusick 	int putsize;
42238420Smckusick 
42338420Smckusick 	putsize = 1;
42438420Smckusick 	m2 = *mb;
42538420Smckusick 	left = NFSMSIZ(m2)-m2->m_len;
42638420Smckusick 	if (left > 0) {
42738420Smckusick 		p = ((u_long *)(*bpos));
42838420Smckusick 		*p++ = txdr_unsigned(siz);
42938420Smckusick 		putsize = 0;
43038420Smckusick 		left -= NFSX_UNSIGNED;
43138420Smckusick 		m2->m_len += NFSX_UNSIGNED;
43238420Smckusick 		if (left > 0) {
43338420Smckusick 			bcopy(cp, (caddr_t) p, left);
43438420Smckusick 			siz -= left;
43538420Smckusick 			cp += left;
43638420Smckusick 			m2->m_len += left;
43738420Smckusick 			left = 0;
43838420Smckusick 		}
43938420Smckusick 	}
44038420Smckusick 	/* Loop arround adding mbufs */
44138420Smckusick 	while (siz > 0) {
44238420Smckusick 		MGET(m1, M_WAIT, MT_DATA);
44338420Smckusick 		if (siz > MLEN)
44441902Smckusick 			MCLGET(m1, M_WAIT);
44538420Smckusick 		m1->m_len = NFSMSIZ(m1);
44638420Smckusick 		m2->m_next = m1;
44738420Smckusick 		m2 = m1;
44838420Smckusick 		p = mtod(m1, u_long *);
44938420Smckusick 		tlen = 0;
45038420Smckusick 		if (putsize) {
45138420Smckusick 			*p++ = txdr_unsigned(siz);
45238420Smckusick 			m1->m_len -= NFSX_UNSIGNED;
45338420Smckusick 			tlen = NFSX_UNSIGNED;
45438420Smckusick 			putsize = 0;
45538420Smckusick 		}
45638420Smckusick 		if (siz < m1->m_len) {
45738420Smckusick 			len = nfsm_rndup(siz);
45838420Smckusick 			xfer = siz;
45938420Smckusick 			if (xfer < len)
46038420Smckusick 				*(p+(xfer>>2)) = 0;
46138420Smckusick 		} else {
46238420Smckusick 			xfer = len = m1->m_len;
46338420Smckusick 		}
46438420Smckusick 		bcopy(cp, (caddr_t) p, xfer);
46538420Smckusick 		m1->m_len = len+tlen;
46638420Smckusick 		siz -= xfer;
46738420Smckusick 		cp += xfer;
46838420Smckusick 	}
46938420Smckusick 	*mb = m1;
47038420Smckusick 	*bpos = mtod(m1, caddr_t)+m1->m_len;
47141902Smckusick 	return (0);
47238420Smckusick }
47338420Smckusick 
47438420Smckusick /*
47538420Smckusick  * Called once to initialize data structures...
47638420Smckusick  */
47739444Smckusick nfs_init()
47838420Smckusick {
47938420Smckusick 	register int i;
48038420Smckusick 
48138420Smckusick 	rpc_vers = txdr_unsigned(RPC_VER2);
48238420Smckusick 	rpc_call = txdr_unsigned(RPC_CALL);
48338420Smckusick 	rpc_reply = txdr_unsigned(RPC_REPLY);
48438420Smckusick 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
48538420Smckusick 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
48638420Smckusick 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
48738420Smckusick 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
48838420Smckusick 	nfs_vers = txdr_unsigned(NFS_VER2);
48938420Smckusick 	nfs_prog = txdr_unsigned(NFS_PROG);
49038420Smckusick 	nfs_true = txdr_unsigned(TRUE);
49138420Smckusick 	nfs_false = txdr_unsigned(FALSE);
49238420Smckusick 	/* Loop thru nfs procids */
49338420Smckusick 	for (i = 0; i < NFS_NPROCS; i++)
49438420Smckusick 		nfs_procids[i] = txdr_unsigned(i);
49539345Smckusick 	/* Ensure async daemons disabled */
49641902Smckusick 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
49739345Smckusick 		nfs_iodwant[i] = (struct proc *)0;
49838420Smckusick 	nfs_xdrneg1 = txdr_unsigned(-1);
49938420Smckusick 	nfs_nhinit();			/* Init the nfsnode table */
50039755Smckusick 	nfsrv_initcache();		/* Init the server request cache */
50138884Smacklem 	rminit(nfsmap, (long)NFS_MAPREG, (long)1, "nfs mapreg", NFS_MSIZ);
50241902Smckusick 
50341902Smckusick 	/*
50441902Smckusick 	 * Initialize reply list and start timer
50541902Smckusick 	 */
50641902Smckusick 	nfsreqh.r_prev = nfsreqh.r_next = &nfsreqh;
50738420Smckusick 	nfs_timer();
50838420Smckusick }
50938420Smckusick 
51038420Smckusick /*
51138420Smckusick  * Fill in the rest of the rpc_unixauth and return it
51238420Smckusick  */
51338420Smckusick static char *nfs_unixauth(cr)
51438420Smckusick 	register struct ucred *cr;
51538420Smckusick {
51638420Smckusick 	register u_long *p;
51738420Smckusick 	register int i;
51838420Smckusick 	int ngr;
51938420Smckusick 
52038420Smckusick 	/* Maybe someday there should be a cache of AUTH_SHORT's */
52138420Smckusick 	if ((p = rpc_uidp) == NULL) {
52238425Smckusick #ifdef FILLINHOST
52344987Smckusick 		i = nfsm_rndup(hostnamelen)+(25*NFSX_UNSIGNED);
52438425Smckusick #else
52544987Smckusick 		i = 25*NFSX_UNSIGNED;
52638425Smckusick #endif
52738420Smckusick 		MALLOC(p, u_long *, i, M_TEMP, M_WAITOK);
52838420Smckusick 		bzero((caddr_t)p, i);
52938420Smckusick 		rpc_unixauth = (caddr_t)p;
53038420Smckusick 		*p++ = txdr_unsigned(RPCAUTH_UNIX);
53138420Smckusick 		p++;	/* Fill in size later */
53238420Smckusick 		*p++ = hostid;
53338425Smckusick #ifdef FILLINHOST
53438420Smckusick 		*p++ = txdr_unsigned(hostnamelen);
53538420Smckusick 		i = nfsm_rndup(hostnamelen);
53638420Smckusick 		bcopy(hostname, (caddr_t)p, hostnamelen);
53738420Smckusick 		p += (i>>2);
53838425Smckusick #else
53938425Smckusick 		*p++ = 0;
54038425Smckusick #endif
54138420Smckusick 		rpc_uidp = p;
54238420Smckusick 	}
54338420Smckusick 	*p++ = txdr_unsigned(cr->cr_uid);
54438420Smckusick 	*p++ = txdr_unsigned(cr->cr_groups[0]);
54544987Smckusick 	ngr = ((cr->cr_ngroups - 1) > numgrps) ? numgrps : (cr->cr_ngroups - 1);
54638420Smckusick 	*p++ = txdr_unsigned(ngr);
54744987Smckusick 	for (i = 1; i <= ngr; i++)
54838420Smckusick 		*p++ = txdr_unsigned(cr->cr_groups[i]);
54938420Smckusick 	/* And add the AUTH_NULL */
55038420Smckusick 	*p++ = 0;
55138420Smckusick 	*p = 0;
55238420Smckusick 	i = (((caddr_t)p)-rpc_unixauth)-12;
55338420Smckusick 	p = (u_long *)(rpc_unixauth+4);
55438420Smckusick 	*p = txdr_unsigned(i);
55541902Smckusick 	return (rpc_unixauth);
55638420Smckusick }
55738420Smckusick 
55838420Smckusick /*
55938420Smckusick  * Attribute cache routines.
56038420Smckusick  * nfs_loadattrcache() - loads or updates the cache contents from attributes
56138420Smckusick  *	that are on the mbuf list
56238420Smckusick  * nfs_getattrcache() - returns valid attributes if found in cache, returns
56338420Smckusick  *	error otherwise
56438420Smckusick  */
56538420Smckusick 
56638420Smckusick /*
56739444Smckusick  * Load the attribute cache (that lives in the nfsnode entry) with
56838420Smckusick  * the values on the mbuf list and
56938420Smckusick  * Iff vap not NULL
57038420Smckusick  *    copy the attributes to *vaper
57138420Smckusick  */
57239457Smckusick nfs_loadattrcache(vpp, mdp, dposp, vaper)
57339457Smckusick 	struct vnode **vpp;
57438420Smckusick 	struct mbuf **mdp;
57538420Smckusick 	caddr_t *dposp;
57638420Smckusick 	struct vattr *vaper;
57738420Smckusick {
57839457Smckusick 	register struct vnode *vp = *vpp;
57938420Smckusick 	register struct vattr *vap;
58038884Smacklem 	register struct nfsv2_fattr *fp;
58139444Smckusick 	extern struct vnodeops spec_nfsv2nodeops;
58239457Smckusick 	register struct nfsnode *np;
58339494Smckusick 	register long t1;
58439494Smckusick 	caddr_t dpos, cp2;
58539494Smckusick 	int error = 0;
58639494Smckusick 	struct mbuf *md;
58739444Smckusick 	enum vtype type;
58842244Smckusick 	long rdev;
58939444Smckusick 	struct timeval mtime;
59039444Smckusick 	struct vnode *nvp;
59138420Smckusick 
59238420Smckusick 	md = *mdp;
59338420Smckusick 	dpos = *dposp;
59438420Smckusick 	t1 = (mtod(md, caddr_t)+md->m_len)-dpos;
59538420Smckusick 	if (error = nfsm_disct(&md, &dpos, NFSX_FATTR, t1, TRUE, &cp2))
59638420Smckusick 		return (error);
59738884Smacklem 	fp = (struct nfsv2_fattr *)cp2;
59839444Smckusick 	type = nfstov_type(fp->fa_type);
59942244Smckusick 	rdev = fxdr_unsigned(long, fp->fa_rdev);
60039444Smckusick 	fxdr_time(&fp->fa_mtime, &mtime);
60139444Smckusick 	/*
60239444Smckusick 	 * If v_type == VNON it is a new node, so fill in the v_type,
60339444Smckusick 	 * n_mtime fields. Check to see if it represents a special
60439444Smckusick 	 * device, and if so, check for a possible alias. Once the
60539444Smckusick 	 * correct vnode has been obtained, fill in the rest of the
60639444Smckusick 	 * information.
60739444Smckusick 	 */
60838420Smckusick 	np = VTONFS(vp);
60939444Smckusick 	if (vp->v_type == VNON) {
61042244Smckusick 		if (type == VCHR && rdev == 0xffffffff)
61142244Smckusick 			vp->v_type = type = VFIFO;
61242244Smckusick 		else
61342244Smckusick 			vp->v_type = type;
61440295Smckusick 		if (vp->v_type == VFIFO) {
61540295Smckusick #ifdef FIFO
61640295Smckusick 			extern struct vnodeops fifo_nfsv2nodeops;
61740295Smckusick 			vp->v_op = &fifo_nfsv2nodeops;
61840295Smckusick #else
61940295Smckusick 			return (EOPNOTSUPP);
62040295Smckusick #endif /* FIFO */
62140295Smckusick 		}
62239444Smckusick 		if (vp->v_type == VCHR || vp->v_type == VBLK) {
62339444Smckusick 			vp->v_op = &spec_nfsv2nodeops;
62442244Smckusick 			if (nvp = checkalias(vp, (dev_t)rdev, vp->v_mount)) {
62539444Smckusick 				/*
62639444Smckusick 				 * Reinitialize aliased node.
62739444Smckusick 				 */
62839444Smckusick 				np = VTONFS(nvp);
62939444Smckusick 				np->n_vnode = nvp;
63039907Smckusick 				np->n_flag = 0;
63139907Smckusick 				nfs_lock(nvp);
63239444Smckusick 				bcopy((caddr_t)&VTONFS(vp)->n_fh,
63339444Smckusick 					(caddr_t)&np->n_fh, NFSX_FH);
63439444Smckusick 				insque(np, nfs_hash(&np->n_fh));
63539444Smckusick 				np->n_attrstamp = 0;
63639444Smckusick 				np->n_sillyrename = (struct sillyrename *)0;
63739444Smckusick 				/*
63839457Smckusick 				 * Discard unneeded vnode and update actual one
63939444Smckusick 				 */
64039444Smckusick 				vput(vp);
64141902Smckusick 				*vpp = nvp;
64239444Smckusick 			}
64339444Smckusick 		}
64439444Smckusick 		np->n_mtime = mtime.tv_sec;
64539444Smckusick 	}
64638420Smckusick 	vap = &np->n_vattr;
64739444Smckusick 	vap->va_type = type;
64838884Smacklem 	vap->va_mode = nfstov_mode(fp->fa_mode);
64938884Smacklem 	vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
65038884Smacklem 	vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
65138884Smacklem 	vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
65238884Smacklem 	vap->va_size = fxdr_unsigned(u_long, fp->fa_size);
65338884Smacklem 	if ((np->n_flag & NMODIFIED) == 0 || vap->va_size > np->n_size)
65438884Smacklem 		np->n_size = vap->va_size;
65540642Smckusick 	vap->va_size_rsv = 0;
65638884Smacklem 	vap->va_blocksize = fxdr_unsigned(long, fp->fa_blocksize);
65742244Smckusick 	vap->va_rdev = (dev_t)rdev;
65842244Smckusick 	vap->va_bytes = fxdr_unsigned(long, fp->fa_blocks) * NFS_FABLKSIZE;
65940642Smckusick 	vap->va_bytes_rsv = 0;
66042878Smckusick 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
66138884Smacklem 	vap->va_fileid = fxdr_unsigned(long, fp->fa_fileid);
66239755Smckusick 	vap->va_atime.tv_sec = fxdr_unsigned(long, fp->fa_atime.tv_sec);
66339755Smckusick 	vap->va_atime.tv_usec = 0;
66439755Smckusick 	vap->va_flags = fxdr_unsigned(u_long, fp->fa_atime.tv_usec);
66539444Smckusick 	vap->va_mtime = mtime;
66639755Smckusick 	vap->va_ctime.tv_sec = fxdr_unsigned(long, fp->fa_ctime.tv_sec);
66739755Smckusick 	vap->va_ctime.tv_usec = 0;
66839755Smckusick 	vap->va_gen = fxdr_unsigned(u_long, fp->fa_ctime.tv_usec);
66938420Smckusick 	np->n_attrstamp = time.tv_sec;
67038420Smckusick 	*dposp = dpos;
67138420Smckusick 	*mdp = md;
67238884Smacklem 	if (vaper != NULL) {
67338420Smckusick 		bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
67438884Smacklem 		if ((np->n_flag & NMODIFIED) && (np->n_size > vap->va_size))
67538884Smacklem 			vaper->va_size = np->n_size;
67638884Smacklem 	}
67738420Smckusick 	return (0);
67838420Smckusick }
67938420Smckusick 
68038420Smckusick /*
68138420Smckusick  * Check the time stamp
68238420Smckusick  * If the cache is valid, copy contents to *vap and return 0
68338420Smckusick  * otherwise return an error
68438420Smckusick  */
68538420Smckusick nfs_getattrcache(vp, vap)
68638420Smckusick 	register struct vnode *vp;
68738420Smckusick 	struct vattr *vap;
68838420Smckusick {
68938420Smckusick 	register struct nfsnode *np;
69038420Smckusick 
69138420Smckusick 	np = VTONFS(vp);
69238420Smckusick 	if ((time.tv_sec-np->n_attrstamp) < NFS_ATTRTIMEO) {
69338420Smckusick 		nfsstats.attrcache_hits++;
69438420Smckusick 		bcopy((caddr_t)&np->n_vattr,(caddr_t)vap,sizeof(struct vattr));
69539361Smckusick 		if ((np->n_flag & NMODIFIED) == 0)
69639361Smckusick 			np->n_size = vap->va_size;
69739361Smckusick 		else if (np->n_size > vap->va_size)
69838884Smacklem 			vap->va_size = np->n_size;
69938420Smckusick 		return (0);
70038420Smckusick 	} else {
70138420Smckusick 		nfsstats.attrcache_misses++;
70238420Smckusick 		return (ENOENT);
70338420Smckusick 	}
70438420Smckusick }
70538420Smckusick 
70638420Smckusick /*
70741902Smckusick  * Set up nameidata for a namei() call and do it
70838420Smckusick  */
70938420Smckusick nfs_namei(ndp, fhp, len, mdp, dposp)
71038420Smckusick 	register struct nameidata *ndp;
71138420Smckusick 	fhandle_t *fhp;
71238420Smckusick 	int len;
71338420Smckusick 	struct mbuf **mdp;
71438420Smckusick 	caddr_t *dposp;
71538420Smckusick {
71638420Smckusick 	register int i, rem;
71738420Smckusick 	register struct mbuf *md;
71838420Smckusick 	register char *cp;
71941902Smckusick 	struct vnode *dp;
72038420Smckusick 	int flag;
72141902Smckusick 	int error;
72238420Smckusick 
72342244Smckusick 	if ((ndp->ni_nameiop & HASBUF) == 0) {
72442244Smckusick 		flag = ndp->ni_nameiop & OPFLAG;
72542244Smckusick 		/*
72642244Smckusick 		 * Copy the name from the mbuf list to the d_name field of ndp
72742244Smckusick 		 * and set the various ndp fields appropriately.
72842244Smckusick 		 */
72942244Smckusick 		cp = *dposp;
73042244Smckusick 		md = *mdp;
73142244Smckusick 		rem = mtod(md, caddr_t)+md->m_len-cp;
73242244Smckusick 		ndp->ni_hash = 0;
73342244Smckusick 		for (i = 0; i < len;) {
73442244Smckusick 			while (rem == 0) {
73542244Smckusick 				md = md->m_next;
73642244Smckusick 				if (md == NULL)
73742244Smckusick 					return (EBADRPC);
73842244Smckusick 				cp = mtod(md, caddr_t);
73942244Smckusick 				rem = md->m_len;
74042244Smckusick 			}
74142244Smckusick 			if (*cp == '\0' || *cp == '/')
74242244Smckusick 				return (EINVAL);
74342244Smckusick 			if (*cp & 0200)
74442244Smckusick 				if ((*cp&0377) == ('/'|0200) || flag != DELETE)
74542244Smckusick 					return (EINVAL);
74642244Smckusick 			ndp->ni_dent.d_name[i++] = *cp;
74742244Smckusick 			ndp->ni_hash += (unsigned char)*cp * i;
74842244Smckusick 			cp++;
74942244Smckusick 			rem--;
75038420Smckusick 		}
75142244Smckusick 		*mdp = md;
75242244Smckusick 		*dposp = cp;
75342244Smckusick 		len = nfsm_rndup(len)-len;
75442244Smckusick 		if (len > 0) {
75542244Smckusick 			if (rem < len) {
75642244Smckusick 				if (error = nfs_adv(mdp, dposp, len, rem))
75742244Smckusick 					return (error);
75842244Smckusick 			} else
75942244Smckusick 				*dposp += len;
76042244Smckusick 		}
76142244Smckusick 	} else
76242244Smckusick 		i = len;
76338420Smckusick 	ndp->ni_namelen = i;
76438420Smckusick 	ndp->ni_dent.d_namlen = i;
76538420Smckusick 	ndp->ni_dent.d_name[i] = '\0';
76642244Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
76738425Smckusick 	ndp->ni_pathlen = 1;
76841902Smckusick 	ndp->ni_pnbuf = ndp->ni_dirp = ndp->ni_ptr = &ndp->ni_dent.d_name[0];
76938420Smckusick 	ndp->ni_next = &ndp->ni_dent.d_name[i];
77041902Smckusick 	ndp->ni_nameiop |= (NOCROSSMOUNT | REMOTE | HASBUF);
77138420Smckusick 
77241902Smckusick 	if (error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cred))
77338420Smckusick 		return (error);
77438425Smckusick 	if (dp->v_type != VDIR) {
77541902Smckusick 		vrele(dp);
77638425Smckusick 		return (ENOTDIR);
77738425Smckusick 	}
77839345Smckusick 	/*
77939345Smckusick 	 * Must set current directory here to avoid confusion in namei()
78039345Smckusick 	 * called from rename()
78139345Smckusick 	 */
78238425Smckusick 	ndp->ni_cdir = dp;
78341398Smckusick 	ndp->ni_rdir = NULLVP;
78438420Smckusick 
78538420Smckusick 	/*
78641902Smckusick 	 * And call namei() to do the real work
78738420Smckusick 	 */
78841902Smckusick 	error = namei(ndp);
78941902Smckusick 	vrele(dp);
79038420Smckusick 	return (error);
79138420Smckusick }
79238420Smckusick 
79338420Smckusick /*
79438420Smckusick  * A fiddled version of m_adj() that ensures null fill to a long
79538420Smckusick  * boundary and only trims off the back end
79638420Smckusick  */
79738420Smckusick nfsm_adj(mp, len, nul)
79838420Smckusick 	struct mbuf *mp;
79938420Smckusick 	register int len;
80038420Smckusick 	int nul;
80138420Smckusick {
80238420Smckusick 	register struct mbuf *m;
80338420Smckusick 	register int count, i;
80438420Smckusick 	register char *cp;
80538420Smckusick 
80638420Smckusick 	/*
80738420Smckusick 	 * Trim from tail.  Scan the mbuf chain,
80838420Smckusick 	 * calculating its length and finding the last mbuf.
80938420Smckusick 	 * If the adjustment only affects this mbuf, then just
81038420Smckusick 	 * adjust and return.  Otherwise, rescan and truncate
81138420Smckusick 	 * after the remaining size.
81238420Smckusick 	 */
81338420Smckusick 	count = 0;
81438420Smckusick 	m = mp;
81538420Smckusick 	for (;;) {
81638420Smckusick 		count += m->m_len;
81738420Smckusick 		if (m->m_next == (struct mbuf *)0)
81838420Smckusick 			break;
81938420Smckusick 		m = m->m_next;
82038420Smckusick 	}
82138579Smckusick 	if (m->m_len > len) {
82238420Smckusick 		m->m_len -= len;
82338420Smckusick 		if (nul > 0) {
82438420Smckusick 			cp = mtod(m, caddr_t)+m->m_len-nul;
82538420Smckusick 			for (i = 0; i < nul; i++)
82638420Smckusick 				*cp++ = '\0';
82738420Smckusick 		}
82838420Smckusick 		return;
82938420Smckusick 	}
83038420Smckusick 	count -= len;
83138420Smckusick 	if (count < 0)
83238420Smckusick 		count = 0;
83338420Smckusick 	/*
83438420Smckusick 	 * Correct length for chain is "count".
83538420Smckusick 	 * Find the mbuf with last data, adjust its length,
83638420Smckusick 	 * and toss data from remaining mbufs on chain.
83738420Smckusick 	 */
83838420Smckusick 	for (m = mp; m; m = m->m_next) {
83938420Smckusick 		if (m->m_len >= count) {
84038420Smckusick 			m->m_len = count;
84138420Smckusick 			if (nul > 0) {
84238420Smckusick 				cp = mtod(m, caddr_t)+m->m_len-nul;
84338420Smckusick 				for (i = 0; i < nul; i++)
84438420Smckusick 					*cp++ = '\0';
84538420Smckusick 			}
84638420Smckusick 			break;
84738420Smckusick 		}
84838420Smckusick 		count -= m->m_len;
84938420Smckusick 	}
85038420Smckusick 	while (m = m->m_next)
85138420Smckusick 		m->m_len = 0;
85238420Smckusick }
85338420Smckusick 
85438420Smckusick /*
85538420Smckusick  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
85638420Smckusick  * 	- look up fsid in mount list (if not found ret error)
85738420Smckusick  *	- check that it is exported
85838420Smckusick  *	- get vp by calling VFS_FHTOVP() macro
85938420Smckusick  *	- if not lockflag unlock it with VOP_UNLOCK()
86041902Smckusick  *	- if cred->cr_uid == 0 set it to m_exroot
86138420Smckusick  */
86238420Smckusick nfsrv_fhtovp(fhp, lockflag, vpp, cred)
86338420Smckusick 	fhandle_t *fhp;
86438420Smckusick 	int lockflag;
86538420Smckusick 	struct vnode **vpp;
86638420Smckusick 	struct ucred *cred;
86738420Smckusick {
86838420Smckusick 	register struct mount *mp;
86938420Smckusick 
87038420Smckusick 	if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
87138420Smckusick 		return (ESTALE);
87241398Smckusick 	if ((mp->mnt_flag & MNT_EXPORTED) == 0)
87338420Smckusick 		return (EACCES);
87438420Smckusick 	if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
87538420Smckusick 		return (ESTALE);
87638420Smckusick 	if (cred->cr_uid == 0)
87741398Smckusick 		cred->cr_uid = mp->mnt_exroot;
87838420Smckusick 	if (!lockflag)
87938420Smckusick 		VOP_UNLOCK(*vpp);
88038420Smckusick 	return (0);
88138420Smckusick }
88245282Smckusick 
88345282Smckusick /*
88445282Smckusick  * These two functions implement nfs rpc compression.
88545282Smckusick  * The algorithm is a trivial run length encoding of '\0' bytes. The high
88645282Smckusick  * order nibble of hex "e" is or'd with the number of zeroes - 2 in four
88745282Smckusick  * bits. (2 - 17 zeros) Any data byte with a high order nibble of hex "e"
88845282Smckusick  * is byte stuffed.
88945282Smckusick  * The compressed data is padded with 0x0 bytes to an even multiple of
89045282Smckusick  * 4 bytes in length to avoid any weird long pointer alignments.
89145282Smckusick  * If compression/uncompression is unsuccessful, the original mbuf list
89245282Smckusick  * is returned.
89345282Smckusick  * The first four bytes (the XID) are left uncompressed and the fifth
89445282Smckusick  * byte is set to 0x1 for request and 0x2 for reply.
89545282Smckusick  * An uncompressed RPC will always have the fifth byte == 0x0.
89645282Smckusick  */
89745282Smckusick struct mbuf *
89845282Smckusick nfs_compress(m0)
89945282Smckusick 	struct mbuf *m0;
90045282Smckusick {
90145282Smckusick 	register u_char ch, nextch;
90245282Smckusick 	register int i, rlelast;
90345282Smckusick 	register u_char *ip, *op;
90445282Smckusick 	register int ileft, oleft, noteof;
90545282Smckusick 	register struct mbuf *m, *om;
90645282Smckusick 	struct mbuf **mp, *retm;
90745282Smckusick 	int olen, clget;
90845282Smckusick 
90945282Smckusick 	i = rlelast = 0;
91045282Smckusick 	noteof = 1;
91145282Smckusick 	m = m0;
91245282Smckusick 	if (m->m_len < 12)
91345282Smckusick 		return (m0);
91445282Smckusick 	if (m->m_pkthdr.len >= MINCLSIZE)
91545282Smckusick 		clget = 1;
91645282Smckusick 	else
91745282Smckusick 		clget = 0;
91845282Smckusick 	ileft = m->m_len - 9;
91945282Smckusick 	ip = mtod(m, u_char *);
920*45288Smckusick 	MGETHDR(om, M_WAIT, MT_DATA);
92145282Smckusick 	if (clget)
92245282Smckusick 		MCLGET(om, M_WAIT);
92345282Smckusick 	retm = om;
92445282Smckusick 	mp = &om->m_next;
92545282Smckusick 	olen = om->m_len = 5;
92645282Smckusick 	oleft = M_TRAILINGSPACE(om);
92745282Smckusick 	op = mtod(om, u_char *);
928*45288Smckusick 	bcopy(ip, op, sizeof(u_long));
92945282Smckusick 	ip += 7;
93045282Smckusick 	op += 4;
93145282Smckusick 	*op++ = *ip++ + 1;
93245282Smckusick 	nextch = *ip++;
93345282Smckusick 	while (noteof) {
93445282Smckusick 		ch = nextch;
93545282Smckusick 		if (ileft == 0) {
93645282Smckusick 			do {
93745282Smckusick 				m = m->m_next;
93845282Smckusick 			} while (m && m->m_len == 0);
93945282Smckusick 			if (m) {
94045282Smckusick 				ileft = m->m_len;
94145282Smckusick 				ip = mtod(m, u_char *);
94245282Smckusick 			} else {
94345282Smckusick 				noteof = 0;
94445282Smckusick 				nextch = 0x1;
94545282Smckusick 				goto doit;
94645282Smckusick 			}
94745282Smckusick 		}
94845282Smckusick 		nextch = *ip++;
94945282Smckusick 		ileft--;
95045282Smckusick doit:
95145282Smckusick 		if (ch == '\0') {
95245282Smckusick 			if (++i == NFSC_MAX || nextch != '\0') {
95345282Smckusick 				if (i < 2) {
95445282Smckusick 					nfscput('\0');
95545282Smckusick 				} else {
95645282Smckusick 					if (rlelast == i) {
95745282Smckusick 						nfscput('\0');
95845282Smckusick 						i--;
95945282Smckusick 					}
96045282Smckusick 					if (NFSCRLE(i) == (nextch & 0xff)) {
96145282Smckusick 						i--;
96245282Smckusick 						if (i < 2) {
96345282Smckusick 							nfscput('\0');
96445282Smckusick 						} else {
96545282Smckusick 							nfscput(NFSCRLE(i));
96645282Smckusick 						}
96745282Smckusick 						nfscput('\0');
96845282Smckusick 						rlelast = 0;
96945282Smckusick 					} else {
97045282Smckusick 						nfscput(NFSCRLE(i));
97145282Smckusick 						rlelast = i;
97245282Smckusick 					}
97345282Smckusick 				}
97445282Smckusick 				i = 0;
97545282Smckusick 			}
97645282Smckusick 		} else {
97745282Smckusick 			if ((ch & NFSCRL) == NFSCRL) {
97845282Smckusick 				nfscput(ch);
97945282Smckusick 			}
98045282Smckusick 			nfscput(ch);
98145282Smckusick 			i = rlelast = 0;
98245282Smckusick 		}
98345282Smckusick 	}
98445282Smckusick 	if (olen < m0->m_pkthdr.len) {
98545282Smckusick 		m_freem(m0);
98645282Smckusick 		if (i = (olen & 0x3)) {
98745282Smckusick 			i = 4 - i;
98845282Smckusick 			while (i-- > 0)
98945282Smckusick 				nfscput('\0');
99045282Smckusick 		}
99145282Smckusick 		retm->m_pkthdr.len = olen;
99245282Smckusick 		return (retm);
99345282Smckusick 	} else {
99445282Smckusick 		m_freem(retm);
99545282Smckusick 		return (m0);
99645282Smckusick 	}
99745282Smckusick }
99845282Smckusick 
99945282Smckusick struct mbuf *
100045282Smckusick nfs_uncompress(m0)
100145282Smckusick 	struct mbuf *m0;
100245282Smckusick {
100345282Smckusick 	register u_char cp, nextcp, *ip, *op;
100445282Smckusick 	register struct mbuf *m, *om;
100545282Smckusick 	struct mbuf *retm, **mp;
100645282Smckusick 	int i, j, noteof, clget, ileft, oleft, olen;
100745282Smckusick 
100845282Smckusick 	m = m0;
100945282Smckusick 	if (m->m_pkthdr.len < 6)
101045282Smckusick 		return (m0);
101145282Smckusick 	if (m->m_pkthdr.len >= MINCLSIZE)
101245282Smckusick 		clget = 1;
101345282Smckusick 	else
101445282Smckusick 		clget = 0;
1015*45288Smckusick 	MGETHDR(om, M_WAIT, MT_DATA);
101645282Smckusick 	if (clget)
101745282Smckusick 		MCLGET(om, M_WAIT);
101845282Smckusick 	olen = om->m_len = 8;
101945282Smckusick 	oleft = M_TRAILINGSPACE(om);
102045282Smckusick 	op = mtod(om, u_char *);
102145282Smckusick 	retm = om;
102245282Smckusick 	mp = &om->m_next;
102345282Smckusick 	if (m->m_len >= 6) {
102445282Smckusick 		ileft = m->m_len - 6;
102545282Smckusick 		ip = mtod(m, u_char *);
102645282Smckusick 		*((u_long *)op) = *((u_long *)ip);
102745282Smckusick 		bzero(op + 4, 3);
102845282Smckusick 		ip += 4;
102945282Smckusick 		op += 7;
103045282Smckusick 		if (*ip == '\0') {
103145282Smckusick 			m_freem(om);
103245282Smckusick 			return (m0);
103345282Smckusick 		}
103445282Smckusick 		*op++ = *ip++ - 1;
103545282Smckusick 		cp = *ip++;
103645282Smckusick 	} else {
103745282Smckusick 		ileft = m->m_len;
103845282Smckusick 		ip = mtod(m, u_char *);
103945282Smckusick 		nfscget(*op++);
104045282Smckusick 		nfscget(*op++);
104145282Smckusick 		nfscget(*op++);
104245282Smckusick 		nfscget(*op++);
104345282Smckusick 		bzero(op, 3);
104445282Smckusick 		op += 3;
104545282Smckusick 		nfscget(*op);
104645282Smckusick 		if (*op == '\0') {
104745282Smckusick 			m_freem(om);
104845282Smckusick 			return (m0);
104945282Smckusick 		}
105045282Smckusick 		(*op)--;
105145282Smckusick 		op++;
105245282Smckusick 		nfscget(cp);
105345282Smckusick 	}
105445282Smckusick 	noteof = 1;
105545282Smckusick 	while (noteof) {
105645282Smckusick 		if ((cp & NFSCRL) == NFSCRL) {
105745282Smckusick 			nfscget(nextcp);
105845282Smckusick 			if (cp == nextcp) {
105945282Smckusick 				nfscput(cp);
106045282Smckusick 				goto readit;
106145282Smckusick 			} else {
106245282Smckusick 				i = (cp & 0xf) + 2;
106345282Smckusick 				for (j = 0; j < i; j++) {
106445282Smckusick 					nfscput('\0');
106545282Smckusick 				}
106645282Smckusick 				cp = nextcp;
106745282Smckusick 			}
106845282Smckusick 		} else {
106945282Smckusick 			nfscput(cp);
107045282Smckusick readit:
107145282Smckusick 			nfscget(cp);
107245282Smckusick 		}
107345282Smckusick 	}
107445282Smckusick 	m_freem(m0);
107545282Smckusick 	if (i = (olen & 0x3)) {
107645282Smckusick 		olen -= i;
107745282Smckusick 		om->m_len -= i;
107845282Smckusick 	}
107945282Smckusick 	retm->m_pkthdr.len = olen;
108045282Smckusick 	return (retm);
108145282Smckusick }
1082