Home | History | Annotate | Line # | Download | only in nfs
nfs_subs.c revision 1.8.2.2
      1 /*
      2  * Copyright (c) 1989 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Rick Macklem at The University of Guelph.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	from: @(#)nfs_subs.c	7.41 (Berkeley) 5/15/91
     37  *	$Id: nfs_subs.c,v 1.8.2.2 1993/11/14 22:22:28 mycroft Exp $
     38  */
     39 
     40 /*
     41  * These functions support the macros and help fiddle mbuf chains for
     42  * the nfs op functions. They do things like create the rpc header and
     43  * copy data between mbuf chains and uio lists.
     44  */
     45 #include <sys/param.h>
     46 #include <sys/proc.h>
     47 #include <sys/filedesc.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/mount.h>
     51 #include <sys/file.h>
     52 #include <sys/vnode.h>
     53 #include <sys/namei.h>
     54 #include <sys/mbuf.h>
     55 
     56 #include <ufs/quota.h>
     57 #include <ufs/inode.h>
     58 
     59 #include <nfs/rpcv2.h>
     60 #include <nfs/nfsv2.h>
     61 #include <nfs/nfsnode.h>
     62 #include <nfs/nfs.h>
     63 #include <nfs/nfsiom.h>
     64 #include <nfs/xdr_subs.h>
     65 #include <nfs/nfsm_subs.h>
     66 #include <nfs/nfscompress.h>
     67 
     68 #include <machine/cpu.h>
     69 
     70 #define TRUE	1
     71 #define	FALSE	0
     72 
     73 /*
     74  * Data items converted to xdr at startup, since they are constant
     75  * This is kinda hokey, but may save a little time doing byte swaps
     76  */
     77 u_long nfs_procids[NFS_NPROCS];
     78 u_long nfs_xdrneg1;
     79 u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied,
     80 	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
     81 u_long nfs_vers, nfs_prog, nfs_true, nfs_false;
     82 /* And other global data */
     83 static u_long *rpc_uidp = (u_long *)0;
     84 static u_long nfs_xid = 1;
     85 static char *rpc_unixauth;
     86 extern long hostid;
     87 
     88 extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
     89 extern struct nfsreq nfsreqh;
     90 
     91 /* Function ret types */
     92 static char *nfs_unixauth();
     93 
     94 /*
     95  * Maximum number of groups passed through to NFS server.
     96  * According to RFC1057 it should be 16.
     97  * For release 3.X systems, the maximum value is 8.
     98  * For some other servers, the maximum value is 10.
     99  */
    100 int numgrps = 8;
    101 
    102 /*
    103  * Create the header for an rpc request packet
    104  * The function nfs_unixauth() creates a unix style authorization string
    105  * and returns a ptr to it.
    106  * The hsiz is the size of the rest of the nfs request header.
    107  * (just used to decide if a cluster is a good idea)
    108  * nb: Note that the prog, vers and procid args are already in xdr byte order
    109  */
    110 struct mbuf *nfsm_reqh(prog, vers, procid, cred, hsiz, bpos, mb, retxid)
    111 	u_long prog;
    112 	u_long vers;
    113 	u_long procid;
    114 	struct ucred *cred;
    115 	int hsiz;
    116 	caddr_t *bpos;
    117 	struct mbuf **mb;
    118 	u_long *retxid;
    119 {
    120 	register struct mbuf *mreq, *m;
    121 	register u_long *tl;
    122 	struct mbuf *m1;
    123 	char *ap;
    124 	int asiz, siz;
    125 	static char authnull[4*NFSX_UNSIGNED];
    126 
    127 	NFSMGETHDR(mreq);
    128 	if (cred != NOCRED) {
    129 	    asiz = ((((cred->cr_ngroups - 1) > numgrps) ? numgrps :
    130 		      (cred->cr_ngroups - 1)) << 2);
    131 #ifdef FILLINHOST
    132 	    asiz += nfsm_rndup(hostnamelen)+(9*NFSX_UNSIGNED);
    133 #else
    134 	    asiz += 9*NFSX_UNSIGNED;
    135 #endif
    136 	} else
    137 	    asiz = 4 * NFSX_UNSIGNED;
    138 
    139 	/* If we need a lot, alloc a cluster ?? */
    140 	if ((asiz+hsiz+RPC_SIZ) > MHLEN)
    141 		MCLGET(mreq, M_WAIT);
    142 	mreq->m_len = NFSMSIZ(mreq);
    143 	siz = mreq->m_len;
    144 	m1 = mreq;
    145 	/*
    146 	 * Alloc enough mbufs
    147 	 * We do it now to avoid all sleeps after the call to nfs_unixauth()
    148 	 */
    149 	while ((asiz+RPC_SIZ) > siz) {
    150 		MGET(m, M_WAIT, MT_DATA);
    151 		m1->m_next = m;
    152 		m->m_len = MLEN;
    153 		siz += MLEN;
    154 		m1 = m;
    155 	}
    156 	tl = mtod(mreq, u_long *);
    157 	*tl++ = *retxid = txdr_unsigned(++nfs_xid);
    158 	*tl++ = rpc_call;
    159 	*tl++ = rpc_vers;
    160 	*tl++ = prog;
    161 	*tl++ = vers;
    162 	*tl++ = procid;
    163 
    164 	/* Now we can call nfs_unixauth() and copy it in */
    165 	if (cred != NOCRED)
    166 	    ap = nfs_unixauth(cred);
    167 	else
    168 	    ap = authnull;
    169 	m = mreq;
    170 	siz = m->m_len-RPC_SIZ;
    171 	if (asiz <= siz) {
    172 		bcopy(ap, (caddr_t)tl, asiz);
    173 		m->m_len = asiz+RPC_SIZ;
    174 	} else {
    175 		bcopy(ap, (caddr_t)tl, siz);
    176 		ap += siz;
    177 		asiz -= siz;
    178 		while (asiz > 0) {
    179 			siz = (asiz > MLEN) ? MLEN : asiz;
    180 			m = m->m_next;
    181 			bcopy(ap, mtod(m, caddr_t), siz);
    182 			m->m_len = siz;
    183 			asiz -= siz;
    184 			ap += siz;
    185 		}
    186 	}
    187 
    188 	/* Finally, return values */
    189 	*mb = m;
    190 	*bpos = mtod(m, caddr_t)+m->m_len;
    191 	return (mreq);
    192 }
    193 
    194 /*
    195  * copies mbuf chain to the uio scatter/gather list
    196  */
    197 nfsm_mbuftouio(mrep, uiop, siz, dpos)
    198 	struct mbuf **mrep;
    199 	register struct uio *uiop;
    200 	int siz;
    201 	caddr_t *dpos;
    202 {
    203 	register char *mbufcp, *uiocp;
    204 	register int xfer, left, len;
    205 	register struct mbuf *mp;
    206 	long uiosiz, rem;
    207 	int error = 0;
    208 
    209 	mp = *mrep;
    210 	mbufcp = *dpos;
    211 	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
    212 	rem = nfsm_rndup(siz)-siz;
    213 	while (siz > 0) {
    214 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
    215 			return (EFBIG);
    216 		left = uiop->uio_iov->iov_len;
    217 		uiocp = uiop->uio_iov->iov_base;
    218 		if (left > siz)
    219 			left = siz;
    220 		uiosiz = left;
    221 		while (left > 0) {
    222 			while (len == 0) {
    223 				mp = mp->m_next;
    224 				if (mp == NULL)
    225 					return (EBADRPC);
    226 				mbufcp = mtod(mp, caddr_t);
    227 				len = mp->m_len;
    228 			}
    229 			xfer = (left > len) ? len : left;
    230 #ifdef notdef
    231 			/* Not Yet.. */
    232 			if (uiop->uio_iov->iov_op != NULL)
    233 				(*(uiop->uio_iov->iov_op))
    234 				(mbufcp, uiocp, xfer);
    235 			else
    236 #endif
    237 			if (uiop->uio_segflg == UIO_SYSSPACE)
    238 				bcopy(mbufcp, uiocp, xfer);
    239 			else
    240 				copyout(mbufcp, uiocp, xfer);
    241 			left -= xfer;
    242 			len -= xfer;
    243 			mbufcp += xfer;
    244 			uiocp += xfer;
    245 			uiop->uio_offset += xfer;
    246 			uiop->uio_resid -= xfer;
    247 		}
    248 		if (uiop->uio_iov->iov_len <= siz) {
    249 			uiop->uio_iovcnt--;
    250 			uiop->uio_iov++;
    251 		} else {
    252 			uiop->uio_iov->iov_base += uiosiz;
    253 			uiop->uio_iov->iov_len -= uiosiz;
    254 		}
    255 		siz -= uiosiz;
    256 	}
    257 	*dpos = mbufcp;
    258 	*mrep = mp;
    259 	if (rem > 0) {
    260 		if (len < rem)
    261 			error = nfs_adv(mrep, dpos, rem, len);
    262 		else
    263 			*dpos += rem;
    264 	}
    265 	return (error);
    266 }
    267 
    268 /*
    269  * copies a uio scatter/gather list to an mbuf chain...
    270  */
    271 nfsm_uiotombuf(uiop, mq, siz, bpos)
    272 	register struct uio *uiop;
    273 	struct mbuf **mq;
    274 	int siz;
    275 	caddr_t *bpos;
    276 {
    277 	register char *uiocp;
    278 	register struct mbuf *mp, *mp2;
    279 	register int xfer, left, len;
    280 	int uiosiz, clflg, rem;
    281 	char *cp;
    282 
    283 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
    284 		clflg = 1;
    285 	else
    286 		clflg = 0;
    287 	rem = nfsm_rndup(siz)-siz;
    288 	mp2 = *mq;
    289 	while (siz > 0) {
    290 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
    291 			return (EINVAL);
    292 		left = uiop->uio_iov->iov_len;
    293 		uiocp = uiop->uio_iov->iov_base;
    294 		if (left > siz)
    295 			left = siz;
    296 		uiosiz = left;
    297 		while (left > 0) {
    298 			MGET(mp, M_WAIT, MT_DATA);
    299 			if (clflg)
    300 				MCLGET(mp, M_WAIT);
    301 			mp->m_len = NFSMSIZ(mp);
    302 			mp2->m_next = mp;
    303 			mp2 = mp;
    304 			xfer = (left > mp->m_len) ? mp->m_len : left;
    305 #ifdef notdef
    306 			/* Not Yet.. */
    307 			if (uiop->uio_iov->iov_op != NULL)
    308 				(*(uiop->uio_iov->iov_op))
    309 				(uiocp, mtod(mp, caddr_t), xfer);
    310 			else
    311 #endif
    312 			if (uiop->uio_segflg == UIO_SYSSPACE)
    313 				bcopy(uiocp, mtod(mp, caddr_t), xfer);
    314 			else
    315 				copyin(uiocp, mtod(mp, caddr_t), xfer);
    316 			len = mp->m_len;
    317 			mp->m_len = xfer;
    318 			left -= xfer;
    319 			uiocp += xfer;
    320 			uiop->uio_offset += xfer;
    321 			uiop->uio_resid -= xfer;
    322 		}
    323 		if (uiop->uio_iov->iov_len <= siz) {
    324 			uiop->uio_iovcnt--;
    325 			uiop->uio_iov++;
    326 		} else {
    327 			uiop->uio_iov->iov_base += uiosiz;
    328 			uiop->uio_iov->iov_len -= uiosiz;
    329 		}
    330 		siz -= uiosiz;
    331 	}
    332 	if (rem > 0) {
    333 		if (rem > (len-mp->m_len)) {
    334 			MGET(mp, M_WAIT, MT_DATA);
    335 			mp->m_len = 0;
    336 			mp2->m_next = mp;
    337 		}
    338 		cp = mtod(mp, caddr_t)+mp->m_len;
    339 		for (left = 0; left < rem; left++)
    340 			*cp++ = '\0';
    341 		mp->m_len += rem;
    342 		*bpos = cp;
    343 	} else
    344 		*bpos = mtod(mp, caddr_t)+mp->m_len;
    345 	*mq = mp;
    346 	return (0);
    347 }
    348 
    349 /*
    350  * Help break down an mbuf chain by setting the first siz bytes contiguous
    351  * pointed to by returned val.
    352  * If Updateflg == True we can overwrite the first part of the mbuf data
    353  * This is used by the macros nfsm_disect and nfsm_disecton for tough
    354  * cases. (The macros use the vars. dpos and dpos2)
    355  */
    356 nfsm_disct(mdp, dposp, siz, left, updateflg, cp2)
    357 	struct mbuf **mdp;
    358 	caddr_t *dposp;
    359 	int siz;
    360 	int left;
    361 	int updateflg;
    362 	caddr_t *cp2;
    363 {
    364 	register struct mbuf *mp, *mp2;
    365 	register int siz2, xfer;
    366 	register caddr_t tl;
    367 
    368 	mp = *mdp;
    369 	while (left == 0) {
    370 		*mdp = mp = mp->m_next;
    371 		if (mp == NULL)
    372 			return (EBADRPC);
    373 		left = mp->m_len;
    374 		*dposp = mtod(mp, caddr_t);
    375 	}
    376 	if (left >= siz) {
    377 		*cp2 = *dposp;
    378 		*dposp += siz;
    379 	} else if (mp->m_next == NULL) {
    380 		return (EBADRPC);
    381 	} else if (siz > MHLEN) {
    382 		panic("nfs S too big");
    383 	} else {
    384 		/* Iff update, you can overwrite, else must alloc new mbuf */
    385 		if (updateflg) {
    386 			NFSMINOFF(mp);
    387 		} else {
    388 			MGET(mp2, M_WAIT, MT_DATA);
    389 			mp2->m_next = mp->m_next;
    390 			mp->m_next = mp2;
    391 			mp->m_len -= left;
    392 			mp = mp2;
    393 		}
    394 		*cp2 = tl = mtod(mp, caddr_t);
    395 		bcopy(*dposp, tl, left);		/* Copy what was left */
    396 		siz2 = siz-left;
    397 		tl += left;
    398 		mp2 = mp->m_next;
    399 		/* Loop around copying up the siz2 bytes */
    400 		while (siz2 > 0) {
    401 			if (mp2 == NULL)
    402 				return (EBADRPC);
    403 			xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
    404 			if (xfer > 0) {
    405 				bcopy(mtod(mp2, caddr_t), tl, xfer);
    406 				NFSMADV(mp2, xfer);
    407 				mp2->m_len -= xfer;
    408 				tl += xfer;
    409 				siz2 -= xfer;
    410 			}
    411 			if (siz2 > 0)
    412 				mp2 = mp2->m_next;
    413 		}
    414 		mp->m_len = siz;
    415 		*mdp = mp2;
    416 		*dposp = mtod(mp2, caddr_t);
    417 	}
    418 	return (0);
    419 }
    420 
    421 /*
    422  * Advance the position in the mbuf chain.
    423  */
    424 nfs_adv(mdp, dposp, offs, left)
    425 	struct mbuf **mdp;
    426 	caddr_t *dposp;
    427 	int offs;
    428 	int left;
    429 {
    430 	register struct mbuf *m;
    431 	register int s;
    432 
    433 	m = *mdp;
    434 	s = left;
    435 	while (s < offs) {
    436 		offs -= s;
    437 		m = m->m_next;
    438 		if (m == NULL)
    439 			return (EBADRPC);
    440 		s = m->m_len;
    441 	}
    442 	*mdp = m;
    443 	*dposp = mtod(m, caddr_t)+offs;
    444 	return (0);
    445 }
    446 
    447 /*
    448  * Copy a string into mbufs for the hard cases...
    449  */
    450 nfsm_strtmbuf(mb, bpos, cp, siz)
    451 	struct mbuf **mb;
    452 	char **bpos;
    453 	char *cp;
    454 	long siz;
    455 {
    456 	register struct mbuf *m1, *m2;
    457 	long left, xfer, len, tlen;
    458 	u_long *tl;
    459 	int putsize;
    460 
    461 	putsize = 1;
    462 	m2 = *mb;
    463 	left = NFSMSIZ(m2)-m2->m_len;
    464 	if (left > 0) {
    465 		tl = ((u_long *)(*bpos));
    466 		*tl++ = txdr_unsigned(siz);
    467 		putsize = 0;
    468 		left -= NFSX_UNSIGNED;
    469 		m2->m_len += NFSX_UNSIGNED;
    470 		if (left > 0) {
    471 			bcopy(cp, (caddr_t) tl, left);
    472 			siz -= left;
    473 			cp += left;
    474 			m2->m_len += left;
    475 			left = 0;
    476 		}
    477 	}
    478 	/* Loop arround adding mbufs */
    479 	while (siz > 0) {
    480 		MGET(m1, M_WAIT, MT_DATA);
    481 		if (siz > MLEN)
    482 			MCLGET(m1, M_WAIT);
    483 		m1->m_len = NFSMSIZ(m1);
    484 		m2->m_next = m1;
    485 		m2 = m1;
    486 		tl = mtod(m1, u_long *);
    487 		tlen = 0;
    488 		if (putsize) {
    489 			*tl++ = txdr_unsigned(siz);
    490 			m1->m_len -= NFSX_UNSIGNED;
    491 			tlen = NFSX_UNSIGNED;
    492 			putsize = 0;
    493 		}
    494 		if (siz < m1->m_len) {
    495 			len = nfsm_rndup(siz);
    496 			xfer = siz;
    497 			if (xfer < len)
    498 				*(tl+(xfer>>2)) = 0;
    499 		} else {
    500 			xfer = len = m1->m_len;
    501 		}
    502 		bcopy(cp, (caddr_t) tl, xfer);
    503 		m1->m_len = len+tlen;
    504 		siz -= xfer;
    505 		cp += xfer;
    506 	}
    507 	*mb = m1;
    508 	*bpos = mtod(m1, caddr_t)+m1->m_len;
    509 	return (0);
    510 }
    511 
    512 /*
    513  * Called once to initialize data structures...
    514  */
    515 nfs_init()
    516 {
    517 	register int i;
    518 
    519 	rpc_vers = txdr_unsigned(RPC_VER2);
    520 	rpc_call = txdr_unsigned(RPC_CALL);
    521 	rpc_reply = txdr_unsigned(RPC_REPLY);
    522 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
    523 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
    524 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
    525 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
    526 	nfs_vers = txdr_unsigned(NFS_VER2);
    527 	nfs_prog = txdr_unsigned(NFS_PROG);
    528 	nfs_true = txdr_unsigned(TRUE);
    529 	nfs_false = txdr_unsigned(FALSE);
    530 	/* Loop thru nfs procids */
    531 	for (i = 0; i < NFS_NPROCS; i++)
    532 		nfs_procids[i] = txdr_unsigned(i);
    533 	/* Ensure async daemons disabled */
    534 	nfs_xdrneg1 = txdr_unsigned(-1);
    535 #ifdef NFSCLIENT
    536 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
    537 		nfs_iodwant[i] = (struct proc *)0;
    538 	nfs_nhinit();			/* Init the nfsnode table */
    539 #endif /* NFSCLIENT */
    540 #ifdef NFSSERVER
    541 	nfsrv_initcache();		/* Init the server request cache */
    542 #endif /*NFSSERVER */
    543 	/*
    544 	 * Initialize reply list and start timer
    545 	 */
    546 	nfsreqh.r_prev = nfsreqh.r_next = &nfsreqh;
    547 	nfs_timer();
    548 }
    549 
    550 /*
    551  * Fill in the rest of the rpc_unixauth and return it
    552  */
    553 static char *nfs_unixauth(cr)
    554 	register struct ucred *cr;
    555 {
    556 	register u_long *tl;
    557 	register int i;
    558 	int ngr;
    559 
    560 	/* Maybe someday there should be a cache of AUTH_SHORT's */
    561 	if ((tl = rpc_uidp) == NULL) {
    562 #ifdef FILLINHOST
    563 		i = nfsm_rndup(hostnamelen)+(25*NFSX_UNSIGNED);
    564 #else
    565 		i = 25*NFSX_UNSIGNED;
    566 #endif
    567 		MALLOC(tl, u_long *, i, M_TEMP, M_WAITOK);
    568 		bzero((caddr_t)tl, i);
    569 		rpc_unixauth = (caddr_t)tl;
    570 		*tl++ = txdr_unsigned(RPCAUTH_UNIX);
    571 		tl++;	/* Fill in size later */
    572 		*tl++ = hostid;
    573 #ifdef FILLINHOST
    574 		*tl++ = txdr_unsigned(hostnamelen);
    575 		i = nfsm_rndup(hostnamelen);
    576 		bcopy(hostname, (caddr_t)tl, hostnamelen);
    577 		tl += (i>>2);
    578 #else
    579 		*tl++ = 0;
    580 #endif
    581 		rpc_uidp = tl;
    582 	}
    583 	*tl++ = txdr_unsigned(cr->cr_uid);
    584 	*tl++ = txdr_unsigned(cr->cr_groups[0]);
    585 	ngr = ((cr->cr_ngroups - 1) > numgrps) ? numgrps : (cr->cr_ngroups - 1);
    586 	*tl++ = txdr_unsigned(ngr);
    587 	for (i = 1; i <= ngr; i++)
    588 		*tl++ = txdr_unsigned(cr->cr_groups[i]);
    589 	/* And add the AUTH_NULL */
    590 	*tl++ = 0;
    591 	*tl = 0;
    592 	i = (((caddr_t)tl)-rpc_unixauth)-12;
    593 	tl = (u_long *)(rpc_unixauth+4);
    594 	*tl = txdr_unsigned(i);
    595 	return (rpc_unixauth);
    596 }
    597 
    598 /*
    599  * Set up nameidata for a namei() call and do it
    600  */
    601 nfs_namei(ndp, fhp, len, mdp, dposp, p)
    602 	register struct nameidata *ndp;
    603 	fhandle_t *fhp;
    604 	int len;
    605 	struct mbuf **mdp;
    606 	caddr_t *dposp;
    607 	struct proc *p;
    608 {
    609 	register int i, rem;
    610 	register struct mbuf *md;
    611 	register char *fromcp, *tocp;
    612 	struct vnode *dp;
    613 	int flag;
    614 	int error;
    615 
    616 	flag = ndp->ni_nameiop & OPMASK;
    617 	MALLOC(ndp->ni_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK);
    618 	/*
    619 	 * Copy the name from the mbuf list to ndp->ni_pnbuf
    620 	 * and set the various ndp fields appropriately.
    621 	 */
    622 	fromcp = *dposp;
    623 	tocp = ndp->ni_pnbuf;
    624 	md = *mdp;
    625 	rem = mtod(md, caddr_t) + md->m_len - fromcp;
    626 	for (i = 0; i < len; i++) {
    627 		while (rem == 0) {
    628 			md = md->m_next;
    629 			if (md == NULL) {
    630 				error = EBADRPC;
    631 				goto out;
    632 			}
    633 			fromcp = mtod(md, caddr_t);
    634 			rem = md->m_len;
    635 		}
    636 		if (*fromcp == '\0' || *fromcp == '/') {
    637 			error = EINVAL;
    638 			goto out;
    639 		}
    640 		if (*fromcp & 0200)
    641 			if ((*fromcp&0377) == ('/'|0200) || flag != DELETE) {
    642 				error = EINVAL;
    643 				goto out;
    644 			}
    645 		*tocp++ = *fromcp++;
    646 		rem--;
    647 	}
    648 	*tocp = '\0';
    649 	*mdp = md;
    650 	*dposp = fromcp;
    651 	len = nfsm_rndup(len)-len;
    652 	if (len > 0) {
    653 		if (rem >= len)
    654 			*dposp += len;
    655 		else if (error = nfs_adv(mdp, dposp, len, rem))
    656 			goto out;
    657 	}
    658 	ndp->ni_pathlen = tocp - ndp->ni_pnbuf;
    659 	ndp->ni_ptr = ndp->ni_pnbuf;
    660 	/*
    661 	 * Extract and set starting directory.
    662 	 */
    663 	if (error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cred))
    664 		goto out;
    665 	if (dp->v_type != VDIR) {
    666 		vrele(dp);
    667 		error = ENOTDIR;
    668 		goto out;
    669 	}
    670 	ndp->ni_startdir = dp;
    671 	ndp->ni_nameiop |= (NOCROSSMOUNT | REMOTE);
    672 	/*
    673 	 * And call lookup() to do the real work
    674 	 */
    675 	if (error = lookup(ndp, p))
    676 		goto out;
    677 	/*
    678 	 * Check for encountering a symbolic link
    679 	 */
    680 	if (ndp->ni_more) {
    681 		if ((ndp->ni_nameiop & LOCKPARENT) && ndp->ni_pathlen == 1)
    682 			vput(ndp->ni_dvp);
    683 		else
    684 			vrele(ndp->ni_dvp);
    685 		vput(ndp->ni_vp);
    686 		ndp->ni_vp = NULL;
    687 		error = EINVAL;
    688 		goto out;
    689 	}
    690 	/*
    691 	 * Check for saved name request
    692 	 */
    693 	if (ndp->ni_nameiop & (SAVENAME | SAVESTART)) {
    694 		ndp->ni_nameiop |= HASBUF;
    695 		return (0);
    696 	}
    697 out:
    698 	FREE(ndp->ni_pnbuf, M_NAMEI);
    699 	return (error);
    700 }
    701 
    702 /*
    703  * A fiddled version of m_adj() that ensures null fill to a long
    704  * boundary and only trims off the back end
    705  */
    706 nfsm_adj(mp, len, nul)
    707 	struct mbuf *mp;
    708 	register int len;
    709 	int nul;
    710 {
    711 	register struct mbuf *m;
    712 	register int count, i;
    713 	register char *cp;
    714 
    715 	/*
    716 	 * Trim from tail.  Scan the mbuf chain,
    717 	 * calculating its length and finding the last mbuf.
    718 	 * If the adjustment only affects this mbuf, then just
    719 	 * adjust and return.  Otherwise, rescan and truncate
    720 	 * after the remaining size.
    721 	 */
    722 	count = 0;
    723 	m = mp;
    724 	for (;;) {
    725 		count += m->m_len;
    726 		if (m->m_next == (struct mbuf *)0)
    727 			break;
    728 		m = m->m_next;
    729 	}
    730 	if (m->m_len > len) {
    731 		m->m_len -= len;
    732 		if (nul > 0) {
    733 			cp = mtod(m, caddr_t)+m->m_len-nul;
    734 			for (i = 0; i < nul; i++)
    735 				*cp++ = '\0';
    736 		}
    737 		return;
    738 	}
    739 	count -= len;
    740 	if (count < 0)
    741 		count = 0;
    742 	/*
    743 	 * Correct length for chain is "count".
    744 	 * Find the mbuf with last data, adjust its length,
    745 	 * and toss data from remaining mbufs on chain.
    746 	 */
    747 	for (m = mp; m; m = m->m_next) {
    748 		if (m->m_len >= count) {
    749 			m->m_len = count;
    750 			if (nul > 0) {
    751 				cp = mtod(m, caddr_t)+m->m_len-nul;
    752 				for (i = 0; i < nul; i++)
    753 					*cp++ = '\0';
    754 			}
    755 			break;
    756 		}
    757 		count -= m->m_len;
    758 	}
    759 	while (m = m->m_next)
    760 		m->m_len = 0;
    761 }
    762 
    763 /*
    764  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
    765  * 	- look up fsid in mount list (if not found ret error)
    766  *	- check that it is exported
    767  *	- get vp by calling VFS_FHTOVP() macro
    768  *	- if not lockflag unlock it with VOP_UNLOCK()
    769  *	- if cred->cr_uid == 0 set it to m_exroot
    770  */
    771 nfsrv_fhtovp(fhp, lockflag, vpp, cred)
    772 	fhandle_t *fhp;
    773 	int lockflag;
    774 	struct vnode **vpp;
    775 	struct ucred *cred;
    776 {
    777 	register struct mount *mp;
    778 
    779 	if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
    780 		return (ESTALE);
    781 	if ((mp->mnt_flag & MNT_EXPORTED) == 0)
    782 		return (EACCES);
    783 	if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
    784 		return (ESTALE);
    785 	if (cred->cr_uid == 0)
    786 		cred->cr_uid = mp->mnt_exroot;
    787 	if (!lockflag)
    788 		VOP_UNLOCK(*vpp);
    789 	return (0);
    790 }
    791 
    792 /*
    793  * These two functions implement nfs rpc compression.
    794  * The algorithm is a trivial run length encoding of '\0' bytes. The high
    795  * order nibble of hex "e" is or'd with the number of zeroes - 2 in four
    796  * bits. (2 - 17 zeros) Any data byte with a high order nibble of hex "e"
    797  * is byte stuffed.
    798  * The compressed data is padded with 0x0 bytes to an even multiple of
    799  * 4 bytes in length to avoid any weird long pointer alignments.
    800  * If compression/uncompression is unsuccessful, the original mbuf list
    801  * is returned.
    802  * The first four bytes (the XID) are left uncompressed and the fifth
    803  * byte is set to 0x1 for request and 0x2 for reply.
    804  * An uncompressed RPC will always have the fifth byte == 0x0.
    805  */
    806 struct mbuf *
    807 nfs_compress(m0)
    808 	struct mbuf *m0;
    809 {
    810 	register u_char ch, nextch;
    811 	register int i, rlelast;
    812 	register u_char *ip, *op;
    813 	register int ileft, oleft, noteof;
    814 	register struct mbuf *m, *om;
    815 	struct mbuf **mp, *retm;
    816 	int olen, clget;
    817 
    818 	i = rlelast = 0;
    819 	noteof = 1;
    820 	m = m0;
    821 	if (m->m_len < 12)
    822 		return (m0);
    823 	if (m->m_pkthdr.len >= MINCLSIZE)
    824 		clget = 1;
    825 	else
    826 		clget = 0;
    827 	ileft = m->m_len - 9;
    828 	ip = mtod(m, u_char *);
    829 	MGETHDR(om, M_WAIT, MT_DATA);
    830 	if (clget)
    831 		MCLGET(om, M_WAIT);
    832 	retm = om;
    833 	mp = &om->m_next;
    834 	olen = om->m_len = 5;
    835 	oleft = M_TRAILINGSPACE(om);
    836 	op = mtod(om, u_char *);
    837 	*((u_long *)op) = *((u_long *)ip);
    838 	ip += 7;
    839 	op += 4;
    840 	*op++ = *ip++ + 1;
    841 	nextch = *ip++;
    842 	while (noteof) {
    843 		ch = nextch;
    844 		if (ileft == 0) {
    845 			do {
    846 				m = m->m_next;
    847 			} while (m && m->m_len == 0);
    848 			if (m) {
    849 				ileft = m->m_len;
    850 				ip = mtod(m, u_char *);
    851 			} else {
    852 				noteof = 0;
    853 				nextch = 0x1;
    854 				goto doit;
    855 			}
    856 		}
    857 		nextch = *ip++;
    858 		ileft--;
    859 doit:
    860 		if (ch == '\0') {
    861 			if (++i == NFSC_MAX || nextch != '\0') {
    862 				if (i < 2) {
    863 					nfscput('\0');
    864 				} else {
    865 					if (rlelast == i) {
    866 						nfscput('\0');
    867 						i--;
    868 					}
    869 					if (NFSCRLE(i) == (nextch & 0xff)) {
    870 						i--;
    871 						if (i < 2) {
    872 							nfscput('\0');
    873 						} else {
    874 							nfscput(NFSCRLE(i));
    875 						}
    876 						nfscput('\0');
    877 						rlelast = 0;
    878 					} else {
    879 						nfscput(NFSCRLE(i));
    880 						rlelast = i;
    881 					}
    882 				}
    883 				i = 0;
    884 			}
    885 		} else {
    886 			if ((ch & NFSCRL) == NFSCRL) {
    887 				nfscput(ch);
    888 			}
    889 			nfscput(ch);
    890 			i = rlelast = 0;
    891 		}
    892 	}
    893 	if (olen < m0->m_pkthdr.len) {
    894 		m_freem(m0);
    895 		if (i = (olen & 0x3)) {
    896 			i = 4 - i;
    897 			while (i-- > 0) {
    898 				nfscput('\0');
    899 			}
    900 		}
    901 		retm->m_pkthdr.len = olen;
    902 		retm->m_pkthdr.rcvif = (struct ifnet *)0;
    903 		return (retm);
    904 	} else {
    905 		m_freem(retm);
    906 		return (m0);
    907 	}
    908 }
    909 
    910 struct mbuf *
    911 nfs_uncompress(m0)
    912 	struct mbuf *m0;
    913 {
    914 	register u_char cp, nextcp, *ip, *op;
    915 	register struct mbuf *m, *om;
    916 	struct mbuf *retm, **mp;
    917 	int i, j, noteof, clget, ileft, oleft, olen;
    918 
    919 	m = m0;
    920 	i = 0;
    921 	while (m && i < MINCLSIZE) {
    922 		i += m->m_len;
    923 		m = m->m_next;
    924 	}
    925 	if (i < 6)
    926 		return (m0);
    927 	if (i >= MINCLSIZE)
    928 		clget = 1;
    929 	else
    930 		clget = 0;
    931 	m = m0;
    932 	MGET(om, M_WAIT, MT_DATA);
    933 	if (clget)
    934 		MCLGET(om, M_WAIT);
    935 	olen = om->m_len = 8;
    936 	oleft = M_TRAILINGSPACE(om);
    937 	op = mtod(om, u_char *);
    938 	retm = om;
    939 	mp = &om->m_next;
    940 	if (m->m_len >= 6) {
    941 		ileft = m->m_len - 6;
    942 		ip = mtod(m, u_char *);
    943 		*((u_long *)op) = *((u_long *)ip);
    944 		bzero(op + 4, 3);
    945 		ip += 4;
    946 		op += 7;
    947 		if (*ip == '\0') {
    948 			m_freem(om);
    949 			return (m0);
    950 		}
    951 		*op++ = *ip++ - 1;
    952 		cp = *ip++;
    953 	} else {
    954 		ileft = m->m_len;
    955 		ip = mtod(m, u_char *);
    956 		nfscget(*op++);
    957 		nfscget(*op++);
    958 		nfscget(*op++);
    959 		nfscget(*op++);
    960 		bzero(op, 3);
    961 		op += 3;
    962 		nfscget(*op);
    963 		if (*op == '\0') {
    964 			m_freem(om);
    965 			return (m0);
    966 		}
    967 		(*op)--;
    968 		op++;
    969 		nfscget(cp);
    970 	}
    971 	noteof = 1;
    972 	while (noteof) {
    973 		if ((cp & NFSCRL) == NFSCRL) {
    974 			nfscget(nextcp);
    975 			if (cp == nextcp) {
    976 				nfscput(cp);
    977 				goto readit;
    978 			} else {
    979 				i = (cp & 0xf) + 2;
    980 				for (j = 0; j < i; j++) {
    981 					nfscput('\0');
    982 				}
    983 				cp = nextcp;
    984 			}
    985 		} else {
    986 			nfscput(cp);
    987 readit:
    988 			nfscget(cp);
    989 		}
    990 	}
    991 	m_freem(m0);
    992 	if (i = (olen & 0x3))
    993 		om->m_len -= i;
    994 	return (retm);
    995 }
    996