Home | History | Annotate | Line # | Download | only in nfs
nfs_serv.c revision 1.21
      1 /*	$NetBSD: nfs_serv.c,v 1.21 1996/02/09 15:47:11 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)nfs_serv.c	8.4 (Berkeley) 6/4/94
     39  */
     40 
     41 /*
     42  * nfs version 2 server calls to vnode ops
     43  * - these routines generally have 3 phases
     44  *   1 - break down and validate rpc request in mbuf list
     45  *   2 - do the vnode ops for the request
     46  *       (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
     47  *   3 - build the rpc reply in an mbuf list
     48  *   nb:
     49  *	- do not mix the phases, since the nfsm_?? macros can return failures
     50  *	  on a bad rpc or similar and do not do any vrele() or vput()'s
     51  *
     52  *      - the nfsm_reply() macro generates an nfs rpc reply with the nfs
     53  *	error number iff error != 0 whereas
     54  *	returning an error from the server function implies a fatal error
     55  *	such as a badly constructed rpc request that should be dropped without
     56  *	a reply.
     57  */
     58 
     59 #include <sys/param.h>
     60 #include <sys/systm.h>
     61 #include <sys/proc.h>
     62 #include <sys/file.h>
     63 #include <sys/namei.h>
     64 #include <sys/vnode.h>
     65 #include <sys/mount.h>
     66 #include <sys/mbuf.h>
     67 #include <sys/dirent.h>
     68 #include <sys/stat.h>
     69 
     70 #include <vm/vm.h>
     71 
     72 #include <nfs/nfsv2.h>
     73 #include <nfs/rpcv2.h>
     74 #include <nfs/nfs.h>
     75 #include <nfs/xdr_subs.h>
     76 #include <nfs/nfsm_subs.h>
     77 #include <nfs/nqnfs.h>
     78 
     79 /* Defs */
     80 #define	TRUE	1
     81 #define	FALSE	0
     82 
     83 /* Global vars */
     84 extern u_long nfs_procids[NFS_NPROCS];
     85 extern u_int32_t nfs_xdrneg1;
     86 extern u_int32_t nfs_false, nfs_true;
     87 nfstype nfs_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
     88 		      NFCHR, NFNON };
     89 
     90 /*
     91  * nqnfs access service
     92  */
     93 nqnfsrv_access(nfsd, mrep, md, dpos, cred, nam, mrq)
     94 	struct nfsd *nfsd;
     95 	struct mbuf *mrep, *md;
     96 	caddr_t dpos;
     97 	struct ucred *cred;
     98 	struct mbuf *nam, **mrq;
     99 {
    100 	struct vnode *vp;
    101 	nfsv2fh_t nfh;
    102 	fhandle_t *fhp;
    103 	register u_int32_t *tl;
    104 	register int32_t t1;
    105 	caddr_t bpos;
    106 	int error = 0, rdonly, cache, mode = 0;
    107 	char *cp2;
    108 	struct mbuf *mb, *mreq;
    109 	u_quad_t frev;
    110 
    111 	fhp = &nfh.fh_generic;
    112 	nfsm_srvmtofh(fhp);
    113 	nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
    114 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    115 		nfsm_reply(0);
    116 	if (*tl++ == nfs_true)
    117 		mode |= VREAD;
    118 	if (*tl++ == nfs_true)
    119 		mode |= VWRITE;
    120 	if (*tl == nfs_true)
    121 		mode |= VEXEC;
    122 	error = nfsrv_access(vp, mode, cred, rdonly, nfsd->nd_procp);
    123 	vput(vp);
    124 	nfsm_reply(0);
    125 	nfsm_srvdone;
    126 }
    127 
    128 /*
    129  * nfs getattr service
    130  */
    131 nfsrv_getattr(nfsd, mrep, md, dpos, cred, nam, mrq)
    132 	struct nfsd *nfsd;
    133 	struct mbuf *mrep, *md;
    134 	caddr_t dpos;
    135 	struct ucred *cred;
    136 	struct mbuf *nam, **mrq;
    137 {
    138 	register struct nfsv2_fattr *fp;
    139 	struct vattr va;
    140 	struct vnode *vp;
    141 	nfsv2fh_t nfh;
    142 	fhandle_t *fhp;
    143 	register u_int32_t *tl;
    144 	register int32_t t1;
    145 	caddr_t bpos;
    146 	int error = 0, rdonly, cache;
    147 	char *cp2;
    148 	struct mbuf *mb, *mb2, *mreq;
    149 	u_quad_t frev;
    150 
    151 	fhp = &nfh.fh_generic;
    152 	nfsm_srvmtofh(fhp);
    153 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    154 		nfsm_reply(0);
    155 	nqsrv_getl(vp, NQL_READ);
    156 	error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp);
    157 	vput(vp);
    158 	nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    159 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    160 	nfsm_srvfillattr;
    161 	nfsm_srvdone;
    162 }
    163 
    164 /*
    165  * nfs setattr service
    166  */
    167 nfsrv_setattr(nfsd, mrep, md, dpos, cred, nam, mrq)
    168 	struct nfsd *nfsd;
    169 	struct mbuf *mrep, *md;
    170 	caddr_t dpos;
    171 	struct ucred *cred;
    172 	struct mbuf *nam, **mrq;
    173 {
    174 	struct vattr va;
    175 	register struct nfsv2_sattr *sp;
    176 	register struct nfsv2_fattr *fp;
    177 	struct vnode *vp;
    178 	nfsv2fh_t nfh;
    179 	fhandle_t *fhp;
    180 	register u_int32_t *tl;
    181 	register int32_t t1;
    182 	caddr_t bpos;
    183 	int error = 0, rdonly, cache;
    184 	char *cp2;
    185 	struct mbuf *mb, *mb2, *mreq;
    186 	u_quad_t frev, frev2;
    187 
    188 	fhp = &nfh.fh_generic;
    189 	nfsm_srvmtofh(fhp);
    190 	nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_SATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    191 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    192 		nfsm_reply(0);
    193 	nqsrv_getl(vp, NQL_WRITE);
    194 	VATTR_NULL(&va);
    195 	/*
    196 	 * Nah nah nah nah na nah
    197 	 * There is a bug in the Sun client that puts 0xffff in the mode
    198 	 * field of sattr when it should put in 0xffffffff. The u_short
    199 	 * doesn't sign extend.
    200 	 * --> check the low order 2 bytes for 0xffff
    201 	 */
    202 	if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
    203 		va.va_mode = nfstov_mode(sp->sa_mode);
    204 	if (sp->sa_uid != nfs_xdrneg1)
    205 		va.va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
    206 	if (sp->sa_gid != nfs_xdrneg1)
    207 		va.va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
    208 	if (nfsd->nd_nqlflag == NQL_NOVAL) {
    209 		if (sp->sa_nfssize != nfs_xdrneg1)
    210 			va.va_size = fxdr_unsigned(u_quad_t, sp->sa_nfssize);
    211 		if (sp->sa_nfsatime.nfs_sec != nfs_xdrneg1) {
    212 #ifdef notyet
    213 			fxdr_nfstime(&sp->sa_nfsatime, &va.va_atime);
    214 #else
    215 			va.va_atime.tv_sec =
    216 				fxdr_unsigned(int32_t, sp->sa_nfsatime.nfs_sec);
    217 			va.va_atime.tv_nsec = 0;
    218 #endif
    219 		}
    220 		if (sp->sa_nfsmtime.nfs_sec != nfs_xdrneg1)
    221 			fxdr_nfstime(&sp->sa_nfsmtime, &va.va_mtime);
    222 	} else {
    223 		fxdr_hyper(&sp->sa_nqsize, &va.va_size);
    224 		fxdr_nqtime(&sp->sa_nqatime, &va.va_atime);
    225 		fxdr_nqtime(&sp->sa_nqmtime, &va.va_mtime);
    226 		va.va_flags = fxdr_unsigned(u_int32_t, sp->sa_nqflags);
    227 	}
    228 
    229 	/*
    230 	 * If the size is being changed write acces is required, otherwise
    231 	 * just check for a read only file system.
    232 	 */
    233 	if (va.va_size == ((u_quad_t)((quad_t) -1))) {
    234 		if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
    235 			error = EROFS;
    236 			goto out;
    237 		}
    238 	} else {
    239 		if (vp->v_type == VDIR) {
    240 			error = EISDIR;
    241 			goto out;
    242 		} else if (error = nfsrv_access(vp, VWRITE, cred, rdonly,
    243 			nfsd->nd_procp))
    244 			goto out;
    245 	}
    246 	if (error = VOP_SETATTR(vp, &va, cred, nfsd->nd_procp)) {
    247 		vput(vp);
    248 		nfsm_reply(0);
    249 	}
    250 	error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp);
    251 out:
    252 	vput(vp);
    253 	nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL) + 2*NFSX_UNSIGNED);
    254 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    255 	nfsm_srvfillattr;
    256 	if (nfsd->nd_nqlflag != NQL_NOVAL) {
    257 		nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
    258 		txdr_hyper(&frev2, tl);
    259 	}
    260 	nfsm_srvdone;
    261 }
    262 
    263 /*
    264  * nfs lookup rpc
    265  */
    266 nfsrv_lookup(nfsd, mrep, md, dpos, cred, nam, mrq)
    267 	struct nfsd *nfsd;
    268 	struct mbuf *mrep, *md;
    269 	caddr_t dpos;
    270 	struct ucred *cred;
    271 	struct mbuf *nam, **mrq;
    272 {
    273 	register struct nfsv2_fattr *fp;
    274 	struct nameidata nd;
    275 	struct vnode *vp;
    276 	nfsv2fh_t nfh;
    277 	fhandle_t *fhp;
    278 	register caddr_t cp;
    279 	register u_int32_t *tl;
    280 	register int32_t t1;
    281 	caddr_t bpos;
    282 	int error = 0, cache, duration2, cache2, len;
    283 	char *cp2;
    284 	struct mbuf *mb, *mb2, *mreq;
    285 	struct vattr va;
    286 	u_quad_t frev, frev2;
    287 
    288 	fhp = &nfh.fh_generic;
    289 	duration2 = 0;
    290 	if (nfsd->nd_nqlflag != NQL_NOVAL) {
    291 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    292 		duration2 = fxdr_unsigned(int, *tl);
    293 	}
    294 	nfsm_srvmtofh(fhp);
    295 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    296 	nd.ni_cnd.cn_cred = cred;
    297 	nd.ni_cnd.cn_nameiop = LOOKUP;
    298 	nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
    299 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
    300 	    nfsd->nd_procp))
    301 		nfsm_reply(0);
    302 	nqsrv_getl(nd.ni_startdir, NQL_READ);
    303 	vrele(nd.ni_startdir);
    304 	FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    305 	vp = nd.ni_vp;
    306 	bzero((caddr_t)fhp, sizeof(nfh));
    307 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
    308 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
    309 		vput(vp);
    310 		nfsm_reply(0);
    311 	}
    312 	if (duration2)
    313 		(void) nqsrv_getlease(vp, &duration2, NQL_READ, nfsd,
    314 			nam, &cache2, &frev2, cred);
    315 	error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp);
    316 	vput(vp);
    317 	nfsm_reply(NFSX_FH + NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL) + 5*NFSX_UNSIGNED);
    318 	if (nfsd->nd_nqlflag != NQL_NOVAL) {
    319 		if (duration2) {
    320 			nfsm_build(tl, u_int32_t *, 5*NFSX_UNSIGNED);
    321 			*tl++ = txdr_unsigned(NQL_READ);
    322 			*tl++ = txdr_unsigned(cache2);
    323 			*tl++ = txdr_unsigned(duration2);
    324 			txdr_hyper(&frev2, tl);
    325 		} else {
    326 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    327 			*tl = 0;
    328 		}
    329 	}
    330 	nfsm_srvfhtom(fhp);
    331 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    332 	nfsm_srvfillattr;
    333 	nfsm_srvdone;
    334 }
    335 
    336 /*
    337  * nfs readlink service
    338  */
    339 nfsrv_readlink(nfsd, mrep, md, dpos, cred, nam, mrq)
    340 	struct nfsd *nfsd;
    341 	struct mbuf *mrep, *md;
    342 	caddr_t dpos;
    343 	struct ucred *cred;
    344 	struct mbuf *nam, **mrq;
    345 {
    346 	struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
    347 	register struct iovec *ivp = iv;
    348 	register struct mbuf *mp;
    349 	register u_int32_t *tl;
    350 	register int32_t t1;
    351 	caddr_t bpos;
    352 	int error = 0, rdonly, cache, i, tlen, len;
    353 	char *cp2;
    354 	struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
    355 	struct vnode *vp;
    356 	nfsv2fh_t nfh;
    357 	fhandle_t *fhp;
    358 	struct uio io, *uiop = &io;
    359 	u_quad_t frev;
    360 
    361 	fhp = &nfh.fh_generic;
    362 	nfsm_srvmtofh(fhp);
    363 	len = 0;
    364 	i = 0;
    365 	while (len < NFS_MAXPATHLEN) {
    366 		MGET(mp, M_WAIT, MT_DATA);
    367 		MCLGET(mp, M_WAIT);
    368 		mp->m_len = NFSMSIZ(mp);
    369 		if (len == 0)
    370 			mp3 = mp2 = mp;
    371 		else {
    372 			mp2->m_next = mp;
    373 			mp2 = mp;
    374 		}
    375 		if ((len+mp->m_len) > NFS_MAXPATHLEN) {
    376 			mp->m_len = NFS_MAXPATHLEN-len;
    377 			len = NFS_MAXPATHLEN;
    378 		} else
    379 			len += mp->m_len;
    380 		ivp->iov_base = mtod(mp, caddr_t);
    381 		ivp->iov_len = mp->m_len;
    382 		i++;
    383 		ivp++;
    384 	}
    385 	uiop->uio_iov = iv;
    386 	uiop->uio_iovcnt = i;
    387 	uiop->uio_offset = 0;
    388 	uiop->uio_resid = len;
    389 	uiop->uio_rw = UIO_READ;
    390 	uiop->uio_segflg = UIO_SYSSPACE;
    391 	uiop->uio_procp = (struct proc *)0;
    392 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly)) {
    393 		m_freem(mp3);
    394 		nfsm_reply(0);
    395 	}
    396 	if (vp->v_type != VLNK) {
    397 		error = EINVAL;
    398 		goto out;
    399 	}
    400 	nqsrv_getl(vp, NQL_READ);
    401 	error = VOP_READLINK(vp, uiop, cred);
    402 out:
    403 	vput(vp);
    404 	if (error)
    405 		m_freem(mp3);
    406 	nfsm_reply(NFSX_UNSIGNED);
    407 	if (uiop->uio_resid > 0) {
    408 		len -= uiop->uio_resid;
    409 		tlen = nfsm_rndup(len);
    410 		nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
    411 	}
    412 	nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    413 	*tl = txdr_unsigned(len);
    414 	mb->m_next = mp3;
    415 	nfsm_srvdone;
    416 }
    417 
    418 /*
    419  * nfs read service
    420  */
    421 nfsrv_read(nfsd, mrep, md, dpos, cred, nam, mrq)
    422 	struct nfsd *nfsd;
    423 	struct mbuf *mrep, *md;
    424 	caddr_t dpos;
    425 	struct ucred *cred;
    426 	struct mbuf *nam, **mrq;
    427 {
    428 	register struct iovec *iv;
    429 	struct iovec *iv2;
    430 	register struct mbuf *m;
    431 	register struct nfsv2_fattr *fp;
    432 	register u_int32_t *tl;
    433 	register int32_t t1;
    434 	caddr_t bpos;
    435 	int error = 0, rdonly, cache, i, cnt, len, left, siz, tlen;
    436 	char *cp2;
    437 	struct mbuf *mb, *mb2, *mreq;
    438 	struct mbuf *m2;
    439 	struct vnode *vp;
    440 	nfsv2fh_t nfh;
    441 	fhandle_t *fhp;
    442 	struct uio io, *uiop = &io;
    443 	struct vattr va;
    444 	off_t off;
    445 	u_quad_t frev;
    446 
    447 	fhp = &nfh.fh_generic;
    448 	nfsm_srvmtofh(fhp);
    449 	if (nfsd->nd_nqlflag == NQL_NOVAL) {
    450 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    451 		off = (off_t)fxdr_unsigned(u_int32_t, *tl);
    452 	} else {
    453 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    454 		fxdr_hyper(tl, &off);
    455 	}
    456 	nfsm_srvstrsiz(cnt, NFS_MAXDATA);
    457 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    458 		nfsm_reply(0);
    459 	if (vp->v_type != VREG) {
    460 		error = (vp->v_type == VDIR) ? EISDIR : EACCES;
    461 		vput(vp);
    462 		nfsm_reply(0);
    463 	}
    464 	nqsrv_getl(vp, NQL_READ);
    465 	if ((error = nfsrv_access(vp, VREAD, cred, rdonly, nfsd->nd_procp)) &&
    466 	    (error = nfsrv_access(vp, VEXEC, cred, rdonly, nfsd->nd_procp))) {
    467 		vput(vp);
    468 		nfsm_reply(0);
    469 	}
    470 	if (error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp)) {
    471 		vput(vp);
    472 		nfsm_reply(0);
    473 	}
    474 	if (off >= va.va_size)
    475 		cnt = 0;
    476 	else if ((off + cnt) > va.va_size)
    477 		cnt = nfsm_rndup(va.va_size - off);
    478 	nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL)+NFSX_UNSIGNED+nfsm_rndup(cnt));
    479 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    480 	nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    481 	len = left = cnt;
    482 	if (cnt > 0) {
    483 		/*
    484 		 * Generate the mbuf list with the uio_iov ref. to it.
    485 		 */
    486 		i = 0;
    487 		m = m2 = mb;
    488 		MALLOC(iv, struct iovec *,
    489 		       ((NFS_MAXDATA+MLEN-1)/MLEN) * sizeof (struct iovec),
    490 		       M_TEMP, M_WAITOK);
    491 		iv2 = iv;
    492 		while (left > 0) {
    493 			siz = min(M_TRAILINGSPACE(m), left);
    494 			if (siz > 0) {
    495 				m->m_len += siz;
    496 				iv->iov_base = bpos;
    497 				iv->iov_len = siz;
    498 				iv++;
    499 				i++;
    500 				left -= siz;
    501 			}
    502 			if (left > 0) {
    503 				MGET(m, M_WAIT, MT_DATA);
    504 				MCLGET(m, M_WAIT);
    505 				m->m_len = 0;
    506 				m2->m_next = m;
    507 				m2 = m;
    508 				bpos = mtod(m, caddr_t);
    509 			}
    510 		}
    511 		uiop->uio_iov = iv2;
    512 		uiop->uio_iovcnt = i;
    513 		uiop->uio_offset = off;
    514 		uiop->uio_resid = cnt;
    515 		uiop->uio_rw = UIO_READ;
    516 		uiop->uio_segflg = UIO_SYSSPACE;
    517 		error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
    518 		off = uiop->uio_offset;
    519 		FREE((caddr_t)iv2, M_TEMP);
    520 		if (error || (error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp))) {
    521 			m_freem(mreq);
    522 			vput(vp);
    523 			nfsm_reply(0);
    524 		}
    525 	} else
    526 		uiop->uio_resid = 0;
    527 	vput(vp);
    528 	nfsm_srvfillattr;
    529 	len -= uiop->uio_resid;
    530 	tlen = nfsm_rndup(len);
    531 	if (cnt != tlen || tlen != len)
    532 		nfsm_adj(mb, cnt-tlen, tlen-len);
    533 	*tl = txdr_unsigned(len);
    534 	nfsm_srvdone;
    535 }
    536 
    537 /*
    538  * nfs write service
    539  */
    540 nfsrv_write(nfsd, mrep, md, dpos, cred, nam, mrq)
    541 	struct nfsd *nfsd;
    542 	struct mbuf *mrep, *md;
    543 	caddr_t dpos;
    544 	struct ucred *cred;
    545 	struct mbuf *nam, **mrq;
    546 {
    547 	register struct iovec *ivp;
    548 	register struct mbuf *mp;
    549 	register struct nfsv2_fattr *fp;
    550 	struct iovec iv[NFS_MAXIOVEC];
    551 	struct vattr va;
    552 	register u_int32_t *tl;
    553 	register int32_t t1;
    554 	caddr_t bpos;
    555 	int error = 0, rdonly, cache, siz, len, xfer;
    556 	int ioflags = IO_SYNC | IO_NODELOCKED;
    557 	char *cp2;
    558 	struct mbuf *mb, *mb2, *mreq;
    559 	struct vnode *vp;
    560 	nfsv2fh_t nfh;
    561 	fhandle_t *fhp;
    562 	struct uio io, *uiop = &io;
    563 	off_t off;
    564 	u_quad_t frev;
    565 
    566 	fhp = &nfh.fh_generic;
    567 	nfsm_srvmtofh(fhp);
    568 	nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
    569 	if (nfsd->nd_nqlflag == NQL_NOVAL) {
    570 		off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
    571 		tl += 2;
    572 	} else {
    573 		fxdr_hyper(tl, &off);
    574 		tl += 2;
    575 		if (fxdr_unsigned(u_int32_t, *tl++))
    576 			ioflags |= IO_APPEND;
    577 	}
    578 	len = fxdr_unsigned(int32_t, *tl);
    579 	if (len > NFS_MAXDATA || len <= 0) {
    580 		error = EBADRPC;
    581 		nfsm_reply(0);
    582 	}
    583 	if (dpos == (mtod(md, caddr_t)+md->m_len)) {
    584 		mp = md->m_next;
    585 		if (mp == NULL) {
    586 			error = EBADRPC;
    587 			nfsm_reply(0);
    588 		}
    589 	} else {
    590 		mp = md;
    591 		siz = dpos-mtod(mp, caddr_t);
    592 		mp->m_len -= siz;
    593 		NFSMADV(mp, siz);
    594 	}
    595 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    596 		nfsm_reply(0);
    597 	if (vp->v_type != VREG) {
    598 		error = (vp->v_type == VDIR) ? EISDIR : EACCES;
    599 		vput(vp);
    600 		nfsm_reply(0);
    601 	}
    602 	nqsrv_getl(vp, NQL_WRITE);
    603 	if (error = nfsrv_access(vp, VWRITE, cred, rdonly, nfsd->nd_procp)) {
    604 		vput(vp);
    605 		nfsm_reply(0);
    606 	}
    607 	uiop->uio_resid = 0;
    608 	uiop->uio_rw = UIO_WRITE;
    609 	uiop->uio_segflg = UIO_SYSSPACE;
    610 	uiop->uio_procp = (struct proc *)0;
    611 	/*
    612 	 * Do up to NFS_MAXIOVEC mbufs of write each iteration of the
    613 	 * loop until done.
    614 	 */
    615 	while (len > 0 && uiop->uio_resid == 0) {
    616 		ivp = iv;
    617 		siz = 0;
    618 		uiop->uio_iov = ivp;
    619 		uiop->uio_iovcnt = 0;
    620 		uiop->uio_offset = off;
    621 		while (len > 0 && uiop->uio_iovcnt < NFS_MAXIOVEC && mp != NULL) {
    622 			ivp->iov_base = mtod(mp, caddr_t);
    623 			if (len < mp->m_len)
    624 				ivp->iov_len = xfer = len;
    625 			else
    626 				ivp->iov_len = xfer = mp->m_len;
    627 #ifdef notdef
    628 			/* Not Yet .. */
    629 			if (M_HASCL(mp) && (((u_long)ivp->iov_base) & CLOFSET) == 0)
    630 				ivp->iov_op = NULL;	/* what should it be ?? */
    631 			else
    632 				ivp->iov_op = NULL;
    633 #endif
    634 			uiop->uio_iovcnt++;
    635 			ivp++;
    636 			len -= xfer;
    637 			siz += xfer;
    638 			mp = mp->m_next;
    639 		}
    640 		if (len > 0 && mp == NULL) {
    641 			error = EBADRPC;
    642 			vput(vp);
    643 			nfsm_reply(0);
    644 		}
    645 		uiop->uio_resid = siz;
    646 		if (error = VOP_WRITE(vp, uiop, ioflags, cred)) {
    647 			vput(vp);
    648 			nfsm_reply(0);
    649 		}
    650 		off = uiop->uio_offset;
    651 	}
    652 	error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp);
    653 	vput(vp);
    654 	nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    655 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    656 	nfsm_srvfillattr;
    657 	if (nfsd->nd_nqlflag != NQL_NOVAL) {
    658 		nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
    659 		txdr_hyper(&va.va_filerev, tl);
    660 	}
    661 	nfsm_srvdone;
    662 }
    663 
    664 /*
    665  * nfs create service
    666  * now does a truncate to 0 length via. setattr if it already exists
    667  */
    668 nfsrv_create(nfsd, mrep, md, dpos, cred, nam, mrq)
    669 	struct nfsd *nfsd;
    670 	struct mbuf *mrep, *md;
    671 	caddr_t dpos;
    672 	struct ucred *cred;
    673 	struct mbuf *nam, **mrq;
    674 {
    675 	register struct nfsv2_fattr *fp;
    676 	struct vattr va;
    677 	register struct nfsv2_sattr *sp;
    678 	register u_int32_t *tl;
    679 	struct nameidata nd;
    680 	register caddr_t cp;
    681 	register int32_t t1;
    682 	caddr_t bpos;
    683 	int error = 0, rdev, cache, len, tsize;
    684 	char *cp2;
    685 	struct mbuf *mb, *mb2, *mreq;
    686 	struct vnode *vp;
    687 	nfsv2fh_t nfh;
    688 	fhandle_t *fhp;
    689 	u_quad_t frev;
    690 
    691 	nd.ni_cnd.cn_nameiop = 0;
    692 	fhp = &nfh.fh_generic;
    693 	nfsm_srvmtofh(fhp);
    694 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    695 	nd.ni_cnd.cn_cred = cred;
    696 	nd.ni_cnd.cn_nameiop = CREATE;
    697 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
    698 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
    699 	    nfsd->nd_procp))
    700 		nfsm_reply(0);
    701 	VATTR_NULL(&va);
    702 	nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_SATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    703 	/*
    704 	 * Iff doesn't exist, create it
    705 	 * otherwise just truncate to 0 length
    706 	 *   should I set the mode too ??
    707 	 */
    708 	if (nd.ni_vp == NULL) {
    709 		va.va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
    710 		if (va.va_type == VNON)
    711 			va.va_type = VREG;
    712 		va.va_mode = nfstov_mode(sp->sa_mode);
    713 		if (nfsd->nd_nqlflag == NQL_NOVAL)
    714 			rdev = fxdr_unsigned(int32_t, sp->sa_nfssize);
    715 		else
    716 			rdev = fxdr_unsigned(int32_t, sp->sa_nqrdev);
    717 		if (va.va_type == VREG || va.va_type == VSOCK) {
    718 			vrele(nd.ni_startdir);
    719 			nqsrv_getl(nd.ni_dvp, NQL_WRITE);
    720 			if (error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va))
    721 				nfsm_reply(0);
    722 			FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    723 		} else if (va.va_type == VCHR || va.va_type == VBLK ||
    724 			va.va_type == VFIFO) {
    725 			if (va.va_type == VCHR && rdev == 0xffffffff)
    726 				va.va_type = VFIFO;
    727 			if (va.va_type == VFIFO) {
    728 #ifndef FIFO
    729 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    730 				vput(nd.ni_dvp);
    731 				error = ENXIO;
    732 				goto out;
    733 #endif /* FIFO */
    734 			} else if (error = suser(cred, (u_short *)0)) {
    735 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    736 				vput(nd.ni_dvp);
    737 				goto out;
    738 			} else
    739 				va.va_rdev = (dev_t)rdev;
    740 			nqsrv_getl(nd.ni_dvp, NQL_WRITE);
    741 			if (error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va)) {
    742 				vrele(nd.ni_startdir);
    743 				nfsm_reply(0);
    744 			}
    745 			nd.ni_cnd.cn_nameiop = LOOKUP;
    746 			nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART);
    747 			nd.ni_cnd.cn_proc = nfsd->nd_procp;
    748 			nd.ni_cnd.cn_cred = nfsd->nd_procp->p_ucred;
    749 			if (error = lookup(&nd)) {
    750 				free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    751 				nfsm_reply(0);
    752 			}
    753 			FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    754 			if (nd.ni_cnd.cn_flags & ISSYMLINK) {
    755 				vrele(nd.ni_dvp);
    756 				vput(nd.ni_vp);
    757 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    758 				error = EINVAL;
    759 				nfsm_reply(0);
    760 			}
    761 		} else {
    762 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    763 			vput(nd.ni_dvp);
    764 			error = ENXIO;
    765 			goto out;
    766 		}
    767 		vp = nd.ni_vp;
    768 	} else {
    769 		vrele(nd.ni_startdir);
    770 		free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    771 		vp = nd.ni_vp;
    772 		if (nd.ni_dvp == vp)
    773 			vrele(nd.ni_dvp);
    774 		else
    775 			vput(nd.ni_dvp);
    776 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    777 		if (nfsd->nd_nqlflag == NQL_NOVAL) {
    778 			tsize = fxdr_unsigned(int32_t, sp->sa_nfssize);
    779 			if (tsize != -1)
    780 				va.va_size = (u_quad_t)tsize;
    781 			else
    782 				va.va_size = -1;
    783 		} else
    784 			fxdr_hyper(&sp->sa_nqsize, &va.va_size);
    785 		if (va.va_size != -1) {
    786 			if (error = nfsrv_access(vp, VWRITE, cred,
    787 			    (nd.ni_cnd.cn_flags & RDONLY), nfsd->nd_procp)) {
    788 				vput(vp);
    789 				nfsm_reply(0);
    790 			}
    791 			nqsrv_getl(vp, NQL_WRITE);
    792 			if (error = VOP_SETATTR(vp, &va, cred, nfsd->nd_procp)) {
    793 				vput(vp);
    794 				nfsm_reply(0);
    795 			}
    796 		}
    797 	}
    798 	bzero((caddr_t)fhp, sizeof(nfh));
    799 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
    800 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
    801 		vput(vp);
    802 		nfsm_reply(0);
    803 	}
    804 	error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp);
    805 	vput(vp);
    806 	nfsm_reply(NFSX_FH+NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    807 	nfsm_srvfhtom(fhp);
    808 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    809 	nfsm_srvfillattr;
    810 	return (error);
    811 nfsmout:
    812 	if (nd.ni_cnd.cn_nameiop || nd.ni_cnd.cn_flags)
    813 		vrele(nd.ni_startdir);
    814 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    815 	if (nd.ni_dvp == nd.ni_vp)
    816 		vrele(nd.ni_dvp);
    817 	else
    818 		vput(nd.ni_dvp);
    819 	if (nd.ni_vp)
    820 		vput(nd.ni_vp);
    821 	return (error);
    822 
    823 out:
    824 	vrele(nd.ni_startdir);
    825 	free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    826 	nfsm_reply(0);
    827 }
    828 
    829 /*
    830  * nfs remove service
    831  */
    832 nfsrv_remove(nfsd, mrep, md, dpos, cred, nam, mrq)
    833 	struct nfsd *nfsd;
    834 	struct mbuf *mrep, *md;
    835 	caddr_t dpos;
    836 	struct ucred *cred;
    837 	struct mbuf *nam, **mrq;
    838 {
    839 	struct nameidata nd;
    840 	register u_int32_t *tl;
    841 	register int32_t t1;
    842 	caddr_t bpos;
    843 	int error = 0, cache, len;
    844 	char *cp2;
    845 	struct mbuf *mb, *mreq;
    846 	struct vnode *vp;
    847 	nfsv2fh_t nfh;
    848 	fhandle_t *fhp;
    849 	u_quad_t frev;
    850 
    851 	fhp = &nfh.fh_generic;
    852 	nfsm_srvmtofh(fhp);
    853 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    854 	nd.ni_cnd.cn_cred = cred;
    855 	nd.ni_cnd.cn_nameiop = DELETE;
    856 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
    857 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
    858 	    nfsd->nd_procp))
    859 		nfsm_reply(0);
    860 	vp = nd.ni_vp;
    861 	/*
    862 	 * The root of a mounted filesystem cannot be deleted.
    863 	 */
    864 	if (vp->v_flag & VROOT) {
    865 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    866 		if (nd.ni_dvp == vp)
    867 			vrele(nd.ni_dvp);
    868 		else
    869 			vput(nd.ni_dvp);
    870 		vput(vp);
    871 		error = EBUSY;
    872 		goto out;
    873 	}
    874 	if (vp->v_flag & VTEXT)
    875 		(void) vnode_pager_uncache(vp);
    876 	nqsrv_getl(nd.ni_dvp, NQL_WRITE);
    877 	nqsrv_getl(vp, NQL_WRITE);
    878 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
    879 out:
    880 	nfsm_reply(0);
    881 	nfsm_srvdone;
    882 }
    883 
    884 /*
    885  * nfs rename service
    886  */
    887 nfsrv_rename(nfsd, mrep, md, dpos, cred, nam, mrq)
    888 	struct nfsd *nfsd;
    889 	struct mbuf *mrep, *md;
    890 	caddr_t dpos;
    891 	struct ucred *cred;
    892 	struct mbuf *nam, **mrq;
    893 {
    894 	register u_int32_t *tl;
    895 	register int32_t t1;
    896 	caddr_t bpos;
    897 	int error = 0, cache, len, len2;
    898 	char *cp2;
    899 	struct mbuf *mb, *mreq;
    900 	struct nameidata fromnd, tond;
    901 	struct vnode *fvp, *tvp, *tdvp;
    902 	nfsv2fh_t fnfh, tnfh;
    903 	fhandle_t *ffhp, *tfhp;
    904 	u_quad_t frev;
    905 	uid_t saved_uid;
    906 
    907 	ffhp = &fnfh.fh_generic;
    908 	tfhp = &tnfh.fh_generic;
    909 	fromnd.ni_cnd.cn_nameiop = 0;
    910 	tond.ni_cnd.cn_nameiop = 0;
    911 	nfsm_srvmtofh(ffhp);
    912 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    913 	/*
    914 	 * Remember our original uid so that we can reset cr_uid before
    915 	 * the second nfs_namei() call, in case it is remapped.
    916 	 */
    917 	saved_uid = cred->cr_uid;
    918 	fromnd.ni_cnd.cn_cred = cred;
    919 	fromnd.ni_cnd.cn_nameiop = DELETE;
    920 	fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
    921 	if (error = nfs_namei(&fromnd, ffhp, len, nfsd->nd_slp, nam, &md,
    922 	    &dpos, nfsd->nd_procp))
    923 		nfsm_reply(0);
    924 	fvp = fromnd.ni_vp;
    925 	nfsm_srvmtofh(tfhp);
    926 	nfsm_strsiz(len2, NFS_MAXNAMLEN);
    927 	cred->cr_uid = saved_uid;
    928 	tond.ni_cnd.cn_cred = cred;
    929 	tond.ni_cnd.cn_nameiop = RENAME;
    930 	tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
    931 	if (error = nfs_namei(&tond, tfhp, len2, nfsd->nd_slp, nam, &md,
    932 	    &dpos, nfsd->nd_procp)) {
    933 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
    934 		vrele(fromnd.ni_dvp);
    935 		vrele(fvp);
    936 		goto out1;
    937 	}
    938 	tdvp = tond.ni_dvp;
    939 	tvp = tond.ni_vp;
    940 	if (tvp != NULL) {
    941 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
    942 			error = EISDIR;
    943 			goto out;
    944 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
    945 			error = ENOTDIR;
    946 			goto out;
    947 		}
    948 		if (tvp->v_type == VDIR && tvp->v_mountedhere) {
    949 			error = EXDEV;
    950 			goto out;
    951 		}
    952 	}
    953 	if (fvp->v_type == VDIR && fvp->v_mountedhere) {
    954 		error = EBUSY;
    955 		goto out;
    956 	}
    957 	if (fvp->v_mount != tdvp->v_mount) {
    958 		error = EXDEV;
    959 		goto out;
    960 	}
    961 	if (fvp == tdvp)
    962 		error = EINVAL;
    963 	/*
    964 	 * If source is the same as the destination (that is the
    965 	 * same vnode with the same name in the same directory),
    966 	 * then there is nothing to do.
    967 	 */
    968 	if (fvp == tvp && fromnd.ni_dvp == tdvp &&
    969 	    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
    970 	    !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
    971 	      fromnd.ni_cnd.cn_namelen))
    972 		error = -1;
    973 out:
    974 	if (!error) {
    975 		nqsrv_getl(fromnd.ni_dvp, NQL_WRITE);
    976 		nqsrv_getl(tdvp, NQL_WRITE);
    977 		if (tvp)
    978 			nqsrv_getl(tvp, NQL_WRITE);
    979 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
    980 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
    981 	} else {
    982 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
    983 		if (tdvp == tvp)
    984 			vrele(tdvp);
    985 		else
    986 			vput(tdvp);
    987 		if (tvp)
    988 			vput(tvp);
    989 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
    990 		vrele(fromnd.ni_dvp);
    991 		vrele(fvp);
    992 	}
    993 	vrele(tond.ni_startdir);
    994 	FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
    995 out1:
    996 	vrele(fromnd.ni_startdir);
    997 	FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
    998 	nfsm_reply(0);
    999 	return (error);
   1000 
   1001 nfsmout:
   1002 	if (tond.ni_cnd.cn_nameiop || tond.ni_cnd.cn_flags) {
   1003 		vrele(tond.ni_startdir);
   1004 		FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   1005 	}
   1006 	if (fromnd.ni_cnd.cn_nameiop || fromnd.ni_cnd.cn_flags) {
   1007 		vrele(fromnd.ni_startdir);
   1008 		FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   1009 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1010 		vrele(fromnd.ni_dvp);
   1011 		vrele(fvp);
   1012 	}
   1013 	return (error);
   1014 }
   1015 
   1016 /*
   1017  * nfs link service
   1018  */
   1019 nfsrv_link(nfsd, mrep, md, dpos, cred, nam, mrq)
   1020 	struct nfsd *nfsd;
   1021 	struct mbuf *mrep, *md;
   1022 	caddr_t dpos;
   1023 	struct ucred *cred;
   1024 	struct mbuf *nam, **mrq;
   1025 {
   1026 	struct nameidata nd;
   1027 	register u_int32_t *tl;
   1028 	register int32_t t1;
   1029 	caddr_t bpos;
   1030 	int error = 0, rdonly, cache, len;
   1031 	char *cp2;
   1032 	struct mbuf *mb, *mreq;
   1033 	struct vnode *vp;
   1034 	nfsv2fh_t nfh, dnfh;
   1035 	fhandle_t *fhp, *dfhp;
   1036 	u_quad_t frev;
   1037 
   1038 	fhp = &nfh.fh_generic;
   1039 	dfhp = &dnfh.fh_generic;
   1040 	nfsm_srvmtofh(fhp);
   1041 	nfsm_srvmtofh(dfhp);
   1042 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1043 	if (error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
   1044 		nfsm_reply(0);
   1045 	nd.ni_cnd.cn_cred = cred;
   1046 	nd.ni_cnd.cn_nameiop = CREATE;
   1047 	nd.ni_cnd.cn_flags = LOCKPARENT;
   1048 	if (error = nfs_namei(&nd, dfhp, len, nfsd->nd_slp, nam, &md, &dpos,
   1049 	    nfsd->nd_procp))
   1050 		goto out;
   1051 	if (nd.ni_vp) {
   1052 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1053 		if (nd.ni_dvp == nd.ni_vp)
   1054 			vrele(nd.ni_dvp);
   1055 		else
   1056 			vput(nd.ni_dvp);
   1057 		vrele(nd.ni_vp);
   1058 		error = EEXIST;
   1059 		goto out;
   1060 	}
   1061 	nqsrv_getl(nd.ni_dvp, NQL_WRITE);
   1062 	nqsrv_getl(vp, NQL_WRITE);
   1063 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   1064 out:
   1065 	vrele(vp);
   1066 	nfsm_reply(0);
   1067 	nfsm_srvdone;
   1068 }
   1069 
   1070 /*
   1071  * nfs symbolic link service
   1072  */
   1073 nfsrv_symlink(nfsd, mrep, md, dpos, cred, nam, mrq)
   1074 	struct nfsd *nfsd;
   1075 	struct mbuf *mrep, *md;
   1076 	caddr_t dpos;
   1077 	struct ucred *cred;
   1078 	struct mbuf *nam, **mrq;
   1079 {
   1080 	struct vattr va;
   1081 	struct nameidata nd;
   1082 	register u_int32_t *tl;
   1083 	register int32_t t1;
   1084 	struct nfsv2_sattr *sp;
   1085 	caddr_t bpos;
   1086 	struct uio io;
   1087 	struct iovec iv;
   1088 	int error = 0, cache, len, len2;
   1089 	char *pathcp, *cp2;
   1090 	struct mbuf *mb, *mreq;
   1091 	nfsv2fh_t nfh;
   1092 	fhandle_t *fhp;
   1093 	u_quad_t frev;
   1094 
   1095 	pathcp = (char *)0;
   1096 	fhp = &nfh.fh_generic;
   1097 	nfsm_srvmtofh(fhp);
   1098 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1099 	nd.ni_cnd.cn_cred = cred;
   1100 	nd.ni_cnd.cn_nameiop = CREATE;
   1101 	nd.ni_cnd.cn_flags = LOCKPARENT;
   1102 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
   1103 	    nfsd->nd_procp))
   1104 		goto out;
   1105 	nfsm_strsiz(len2, NFS_MAXPATHLEN);
   1106 	MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
   1107 	iv.iov_base = pathcp;
   1108 	iv.iov_len = len2;
   1109 	io.uio_resid = len2;
   1110 	io.uio_offset = 0;
   1111 	io.uio_iov = &iv;
   1112 	io.uio_iovcnt = 1;
   1113 	io.uio_segflg = UIO_SYSSPACE;
   1114 	io.uio_rw = UIO_READ;
   1115 	io.uio_procp = (struct proc *)0;
   1116 	nfsm_mtouio(&io, len2);
   1117 	nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_SATTR(nfsd->nd_nqlflag != NQL_NOVAL));
   1118 	*(pathcp + len2) = '\0';
   1119 	if (nd.ni_vp) {
   1120 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1121 		if (nd.ni_dvp == nd.ni_vp)
   1122 			vrele(nd.ni_dvp);
   1123 		else
   1124 			vput(nd.ni_dvp);
   1125 		vrele(nd.ni_vp);
   1126 		error = EEXIST;
   1127 		goto out;
   1128 	}
   1129 	VATTR_NULL(&va);
   1130 	va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
   1131 	nqsrv_getl(nd.ni_dvp, NQL_WRITE);
   1132 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp);
   1133 out:
   1134 	if (pathcp)
   1135 		FREE(pathcp, M_TEMP);
   1136 	nfsm_reply(0);
   1137 	return (error);
   1138 nfsmout:
   1139 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1140 	if (nd.ni_dvp == nd.ni_vp)
   1141 		vrele(nd.ni_dvp);
   1142 	else
   1143 		vput(nd.ni_dvp);
   1144 	if (nd.ni_vp)
   1145 		vrele(nd.ni_vp);
   1146 	if (pathcp)
   1147 		FREE(pathcp, M_TEMP);
   1148 	return (error);
   1149 }
   1150 
   1151 /*
   1152  * nfs mkdir service
   1153  */
   1154 nfsrv_mkdir(nfsd, mrep, md, dpos, cred, nam, mrq)
   1155 	struct nfsd *nfsd;
   1156 	struct mbuf *mrep, *md;
   1157 	caddr_t dpos;
   1158 	struct ucred *cred;
   1159 	struct mbuf *nam, **mrq;
   1160 {
   1161 	struct vattr va;
   1162 	register struct nfsv2_fattr *fp;
   1163 	struct nameidata nd;
   1164 	register caddr_t cp;
   1165 	register u_int32_t *tl;
   1166 	register int32_t t1;
   1167 	caddr_t bpos;
   1168 	int error = 0, cache, len;
   1169 	char *cp2;
   1170 	struct mbuf *mb, *mb2, *mreq;
   1171 	struct vnode *vp;
   1172 	nfsv2fh_t nfh;
   1173 	fhandle_t *fhp;
   1174 	u_quad_t frev;
   1175 
   1176 	fhp = &nfh.fh_generic;
   1177 	nfsm_srvmtofh(fhp);
   1178 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1179 	nd.ni_cnd.cn_cred = cred;
   1180 	nd.ni_cnd.cn_nameiop = CREATE;
   1181 	nd.ni_cnd.cn_flags = LOCKPARENT;
   1182 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
   1183 	    nfsd->nd_procp))
   1184 		nfsm_reply(0);
   1185 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1186 	VATTR_NULL(&va);
   1187 	va.va_type = VDIR;
   1188 	va.va_mode = nfstov_mode(*tl++);
   1189 	vp = nd.ni_vp;
   1190 	if (vp != NULL) {
   1191 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1192 		if (nd.ni_dvp == vp)
   1193 			vrele(nd.ni_dvp);
   1194 		else
   1195 			vput(nd.ni_dvp);
   1196 		vrele(vp);
   1197 		error = EEXIST;
   1198 		nfsm_reply(0);
   1199 	}
   1200 	nqsrv_getl(nd.ni_dvp, NQL_WRITE);
   1201 	if (error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va))
   1202 		nfsm_reply(0);
   1203 	vp = nd.ni_vp;
   1204 	bzero((caddr_t)fhp, sizeof(nfh));
   1205 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
   1206 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
   1207 		vput(vp);
   1208 		nfsm_reply(0);
   1209 	}
   1210 	error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp);
   1211 	vput(vp);
   1212 	nfsm_reply(NFSX_FH+NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
   1213 	nfsm_srvfhtom(fhp);
   1214 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
   1215 	nfsm_srvfillattr;
   1216 	return (error);
   1217 nfsmout:
   1218 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1219 	if (nd.ni_dvp == nd.ni_vp)
   1220 		vrele(nd.ni_dvp);
   1221 	else
   1222 		vput(nd.ni_dvp);
   1223 	if (nd.ni_vp)
   1224 		vrele(nd.ni_vp);
   1225 	return (error);
   1226 }
   1227 
   1228 /*
   1229  * nfs rmdir service
   1230  */
   1231 nfsrv_rmdir(nfsd, mrep, md, dpos, cred, nam, mrq)
   1232 	struct nfsd *nfsd;
   1233 	struct mbuf *mrep, *md;
   1234 	caddr_t dpos;
   1235 	struct ucred *cred;
   1236 	struct mbuf *nam, **mrq;
   1237 {
   1238 	register u_int32_t *tl;
   1239 	register int32_t t1;
   1240 	caddr_t bpos;
   1241 	int error = 0, cache, len;
   1242 	char *cp2;
   1243 	struct mbuf *mb, *mreq;
   1244 	struct vnode *vp;
   1245 	nfsv2fh_t nfh;
   1246 	fhandle_t *fhp;
   1247 	struct nameidata nd;
   1248 	u_quad_t frev;
   1249 
   1250 	fhp = &nfh.fh_generic;
   1251 	nfsm_srvmtofh(fhp);
   1252 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1253 	nd.ni_cnd.cn_cred = cred;
   1254 	nd.ni_cnd.cn_nameiop = DELETE;
   1255 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
   1256 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
   1257 	    nfsd->nd_procp))
   1258 		nfsm_reply(0);
   1259 	vp = nd.ni_vp;
   1260 	if (vp->v_type != VDIR) {
   1261 		error = ENOTDIR;
   1262 		goto out;
   1263 	}
   1264 	/*
   1265 	 * No rmdir "." please.
   1266 	 */
   1267 	if (nd.ni_dvp == vp) {
   1268 		error = EINVAL;
   1269 		goto out;
   1270 	}
   1271 	/*
   1272 	 * The root of a mounted filesystem cannot be deleted.
   1273 	 */
   1274 	if (vp->v_flag & VROOT)
   1275 		error = EBUSY;
   1276 out:
   1277 	if (!error) {
   1278 		nqsrv_getl(nd.ni_dvp, NQL_WRITE);
   1279 		nqsrv_getl(vp, NQL_WRITE);
   1280 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1281 	} else {
   1282 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1283 		if (nd.ni_dvp == nd.ni_vp)
   1284 			vrele(nd.ni_dvp);
   1285 		else
   1286 			vput(nd.ni_dvp);
   1287 		vput(vp);
   1288 	}
   1289 	nfsm_reply(0);
   1290 	nfsm_srvdone;
   1291 }
   1292 
   1293 /*
   1294  * nfs readdir service
   1295  * - mallocs what it thinks is enough to read
   1296  *	count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
   1297  * - calls VOP_READDIR()
   1298  * - loops around building the reply
   1299  *	if the output generated exceeds count break out of loop
   1300  *	The nfsm_clget macro is used here so that the reply will be packed
   1301  *	tightly in mbuf clusters.
   1302  * - it only knows that it has encountered eof when the VOP_READDIR()
   1303  *	reads nothing
   1304  * - as such one readdir rpc will return eof false although you are there
   1305  *	and then the next will return eof
   1306  * - it trims out records with d_fileno == 0
   1307  *	this doesn't matter for Unix clients, but they might confuse clients
   1308  *	for other os'.
   1309  * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
   1310  *	than requested, but this may not apply to all filesystems. For
   1311  *	example, client NFS does not { although it is never remote mounted
   1312  *	anyhow }
   1313  *     The alternate call nqnfsrv_readdirlook() does lookups as well.
   1314  * PS: The NFS protocol spec. does not clarify what the "count" byte
   1315  *	argument is a count of.. just name strings and file id's or the
   1316  *	entire reply rpc or ...
   1317  *	I tried just file name and id sizes and it confused the Sun client,
   1318  *	so I am using the full rpc size now. The "paranoia.." comment refers
   1319  *	to including the status longwords that are not a part of the dir.
   1320  *	"entry" structures, but are in the rpc.
   1321  */
   1322 struct flrep {
   1323 	u_int32_t fl_cachable;
   1324 	u_int32_t fl_duration;
   1325 	u_int32_t fl_frev[2];
   1326 	nfsv2fh_t fl_nfh;
   1327 	u_int32_t fl_fattr[NFSX_NQFATTR / sizeof (u_int32_t)];
   1328 };
   1329 
   1330 nfsrv_readdir(nfsd, mrep, md, dpos, cred, nam, mrq)
   1331 	struct nfsd *nfsd;
   1332 	struct mbuf *mrep, *md;
   1333 	caddr_t dpos;
   1334 	struct ucred *cred;
   1335 	struct mbuf *nam, **mrq;
   1336 {
   1337 	register char *bp, *be;
   1338 	register struct mbuf *mp;
   1339 	register struct dirent *dp;
   1340 	register caddr_t cp;
   1341 	register u_int32_t *tl;
   1342 	register int32_t t1;
   1343 	caddr_t bpos;
   1344 	struct mbuf *mb, *mb2, *mreq, *mp2;
   1345 	char *cpos, *cend, *cp2, *rbuf;
   1346 	struct vnode *vp;
   1347 	nfsv2fh_t nfh;
   1348 	fhandle_t *fhp;
   1349 	struct uio io;
   1350 	struct iovec iv;
   1351 	int len, nlen, rem, xfer, tsiz, i, error = 0;
   1352 	int siz, cnt, fullsiz, eofflag, rdonly, cache;
   1353 	u_quad_t frev;
   1354 	u_long off, *cookiebuf, *cookie;
   1355 	int ncookies;
   1356 
   1357 	fhp = &nfh.fh_generic;
   1358 	nfsm_srvmtofh(fhp);
   1359 	nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
   1360 	off = fxdr_unsigned(u_int32_t, *tl++);
   1361 	cnt = fxdr_unsigned(int, *tl);
   1362 	siz = ((cnt+NFS_DIRBLKSIZ-1) & ~(NFS_DIRBLKSIZ-1));
   1363 	if (cnt > NFS_MAXREADDIR)
   1364 		siz = NFS_MAXREADDIR;
   1365 	fullsiz = siz;
   1366 	ncookies = siz / 16;	/* Guess at the number of cookies needed. */
   1367 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
   1368 		nfsm_reply(0);
   1369 	nqsrv_getl(vp, NQL_READ);
   1370 	if (error = nfsrv_access(vp, VEXEC, cred, rdonly, nfsd->nd_procp)) {
   1371 		vput(vp);
   1372 		nfsm_reply(0);
   1373 	}
   1374 	VOP_UNLOCK(vp);
   1375 	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
   1376 	MALLOC(cookiebuf, u_long *, ncookies * sizeof(*cookiebuf), M_TEMP,
   1377 	    M_WAITOK);
   1378 again:
   1379 	iv.iov_base = rbuf;
   1380 	iv.iov_len = fullsiz;
   1381 	io.uio_iov = &iv;
   1382 	io.uio_iovcnt = 1;
   1383 	io.uio_offset = (off_t)off;
   1384 	io.uio_resid = fullsiz;
   1385 	io.uio_segflg = UIO_SYSSPACE;
   1386 	io.uio_rw = UIO_READ;
   1387 	io.uio_procp = (struct proc *)0;
   1388 	error = VOP_READDIR(vp, &io, cred, &eofflag, cookiebuf, ncookies);
   1389 	cookie = cookiebuf;
   1390 	off = (off_t)io.uio_offset;
   1391 	if (error) {
   1392 		vrele(vp);
   1393 		free((caddr_t)cookiebuf, M_TEMP);
   1394 		free((caddr_t)rbuf, M_TEMP);
   1395 		nfsm_reply(0);
   1396 	}
   1397 	if (io.uio_resid) {
   1398 		siz -= io.uio_resid;
   1399 
   1400 		/*
   1401 		 * If nothing read, return eof
   1402 		 * rpc reply
   1403 		 */
   1404 		if (siz == 0) {
   1405 			vrele(vp);
   1406 			nfsm_reply(2*NFSX_UNSIGNED);
   1407 			nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
   1408 			*tl++ = nfs_false;
   1409 			*tl = nfs_true;
   1410 			FREE((caddr_t)cookiebuf, M_TEMP);
   1411 			FREE((caddr_t)rbuf, M_TEMP);
   1412 			return (0);
   1413 		}
   1414 	}
   1415 
   1416 	/*
   1417 	 * Check for degenerate cases of nothing useful read.
   1418 	 * If so go try again
   1419 	 */
   1420 	cpos = rbuf;
   1421 	cend = rbuf + siz;
   1422 	while (cpos < cend) {
   1423 		dp = (struct dirent *)cpos;
   1424 		if (dp->d_fileno == 0) {
   1425 			cpos += dp->d_reclen;
   1426 			cookie++;
   1427 		} else
   1428 			break;
   1429 	}
   1430 	if (cpos >= cend) {
   1431 		siz = fullsiz;
   1432 		goto again;
   1433 	}
   1434 
   1435 	len = 3*NFSX_UNSIGNED;	/* paranoia, probably can be 0 */
   1436 	nfsm_reply(siz);
   1437 	mp = mp2 = mb;
   1438 	bp = bpos;
   1439 	be = bp + M_TRAILINGSPACE(mp);
   1440 
   1441 	/* Loop through the records and build reply */
   1442 	while (cpos < cend) {
   1443 		if (dp->d_fileno != 0) {
   1444 			nlen = dp->d_namlen;
   1445 			rem = nfsm_rndup(nlen)-nlen;
   1446 			len += (4*NFSX_UNSIGNED + nlen + rem);
   1447 			if (len > cnt) {
   1448 				eofflag = 0;
   1449 				break;
   1450 			}
   1451 			/*
   1452 			 * Build the directory record xdr from
   1453 			 * the dirent entry.
   1454 			 */
   1455 			nfsm_clget;
   1456 			*tl = nfs_true;
   1457 			bp += NFSX_UNSIGNED;
   1458 			nfsm_clget;
   1459 			*tl = txdr_unsigned(dp->d_fileno);
   1460 			bp += NFSX_UNSIGNED;
   1461 			nfsm_clget;
   1462 			*tl = txdr_unsigned(nlen);
   1463 			bp += NFSX_UNSIGNED;
   1464 
   1465 			/* And loop around copying the name */
   1466 			xfer = nlen;
   1467 			cp = dp->d_name;
   1468 			while (xfer > 0) {
   1469 				nfsm_clget;
   1470 				if ((bp+xfer) > be)
   1471 					tsiz = be-bp;
   1472 				else
   1473 					tsiz = xfer;
   1474 				bcopy(cp, bp, tsiz);
   1475 				bp += tsiz;
   1476 				xfer -= tsiz;
   1477 				if (xfer > 0)
   1478 					cp += tsiz;
   1479 			}
   1480 			/* And null pad to a int32_t boundary */
   1481 			for (i = 0; i < rem; i++)
   1482 				*bp++ = '\0';
   1483 			nfsm_clget;
   1484 
   1485 			/* Finish off the record */
   1486 			*tl = txdr_unsigned(*cookie);
   1487 			bp += NFSX_UNSIGNED;
   1488 		}
   1489 		cpos += dp->d_reclen;
   1490 		dp = (struct dirent *)cpos;
   1491 		cookie++;
   1492 	}
   1493 	vrele(vp);
   1494 	nfsm_clget;
   1495 	*tl = nfs_false;
   1496 	bp += NFSX_UNSIGNED;
   1497 	nfsm_clget;
   1498 	if (eofflag)
   1499 		*tl = nfs_true;
   1500 	else
   1501 		*tl = nfs_false;
   1502 	bp += NFSX_UNSIGNED;
   1503 	if (mp != mb) {
   1504 		if (bp < be)
   1505 			mp->m_len = bp - mtod(mp, caddr_t);
   1506 	} else
   1507 		mp->m_len += bp - bpos;
   1508 	FREE(cookiebuf, M_TEMP);
   1509 	FREE(rbuf, M_TEMP);
   1510 	nfsm_srvdone;
   1511 }
   1512 
   1513 nqnfsrv_readdirlook(nfsd, mrep, md, dpos, cred, nam, mrq)
   1514 	struct nfsd *nfsd;
   1515 	struct mbuf *mrep, *md;
   1516 	caddr_t dpos;
   1517 	struct ucred *cred;
   1518 	struct mbuf *nam, **mrq;
   1519 {
   1520 	register char *bp, *be;
   1521 	register struct mbuf *mp;
   1522 	register struct dirent *dp;
   1523 	register caddr_t cp;
   1524 	register u_int32_t *tl;
   1525 	register int32_t t1;
   1526 	caddr_t bpos;
   1527 	struct mbuf *mb, *mb2, *mreq, *mp2;
   1528 	char *cpos, *cend, *cp2, *rbuf;
   1529 	struct vnode *vp, *nvp;
   1530 	struct flrep fl;
   1531 	nfsv2fh_t nfh;
   1532 	fhandle_t *fhp;
   1533 	struct uio io;
   1534 	struct iovec iv;
   1535 	struct vattr va;
   1536 	struct nfsv2_fattr *fp;
   1537 	int len, nlen, rem, xfer, tsiz, i, error = 0, duration2, cache2;
   1538 	int siz, cnt, fullsiz, eofflag, rdonly, cache;
   1539 	u_quad_t frev, frev2;
   1540 	u_long off, *cookiebuf, *cookie;
   1541 	int ncookies;
   1542 
   1543 	fhp = &nfh.fh_generic;
   1544 	nfsm_srvmtofh(fhp);
   1545 	nfsm_dissect(tl, u_int32_t *, 3*NFSX_UNSIGNED);
   1546 	off = fxdr_unsigned(u_int32_t, *tl++);
   1547 	cnt = fxdr_unsigned(int, *tl++);
   1548 	duration2 = fxdr_unsigned(int, *tl);
   1549 	siz = ((cnt+NFS_DIRBLKSIZ-1) & ~(NFS_DIRBLKSIZ-1));
   1550 	if (cnt > NFS_MAXREADDIR)
   1551 		siz = NFS_MAXREADDIR;
   1552 	fullsiz = siz;
   1553 	ncookies = siz / 16;	/* Guess at the number of cookies needed. */
   1554 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
   1555 		nfsm_reply(0);
   1556 	nqsrv_getl(vp, NQL_READ);
   1557 	if (error = nfsrv_access(vp, VEXEC, cred, rdonly, nfsd->nd_procp)) {
   1558 		vput(vp);
   1559 		nfsm_reply(0);
   1560 	}
   1561 	VOP_UNLOCK(vp);
   1562 	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
   1563 	MALLOC(cookiebuf, u_long *, ncookies * sizeof(*cookiebuf), M_TEMP,
   1564 	    M_WAITOK);
   1565 again:
   1566 	iv.iov_base = rbuf;
   1567 	iv.iov_len = fullsiz;
   1568 	io.uio_iov = &iv;
   1569 	io.uio_iovcnt = 1;
   1570 	io.uio_offset = (off_t)off;
   1571 	io.uio_resid = fullsiz;
   1572 	io.uio_segflg = UIO_SYSSPACE;
   1573 	io.uio_rw = UIO_READ;
   1574 	io.uio_procp = (struct proc *)0;
   1575 	error = VOP_READDIR(vp, &io, cred, &eofflag, cookiebuf, ncookies);
   1576 	cookie = cookiebuf;
   1577 	off = (u_long)io.uio_offset;
   1578 	if (error) {
   1579 		vrele(vp);
   1580 		free((caddr_t)cookiebuf, M_TEMP);
   1581 		free((caddr_t)rbuf, M_TEMP);
   1582 		nfsm_reply(0);
   1583 	}
   1584 	if (io.uio_resid) {
   1585 		siz -= io.uio_resid;
   1586 
   1587 		/*
   1588 		 * If nothing read, return eof
   1589 		 * rpc reply
   1590 		 */
   1591 		if (siz == 0) {
   1592 			vrele(vp);
   1593 			nfsm_reply(2 * NFSX_UNSIGNED);
   1594 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1595 			*tl++ = nfs_false;
   1596 			*tl = nfs_true;
   1597 			FREE((caddr_t)cookiebuf, M_TEMP);
   1598 			FREE((caddr_t)rbuf, M_TEMP);
   1599 			return (0);
   1600 		}
   1601 	}
   1602 
   1603 	/*
   1604 	 * Check for degenerate cases of nothing useful read.
   1605 	 * If so go try again
   1606 	 */
   1607 	cpos = rbuf;
   1608 	cend = rbuf + siz;
   1609 	while (cpos < cend) {
   1610 		dp = (struct dirent *)cpos;
   1611 		if (dp->d_fileno == 0) {
   1612 			cpos += dp->d_reclen;
   1613 			cookie++;
   1614 		} else
   1615 			break;
   1616 	}
   1617 	if (cpos >= cend) {
   1618 		siz = fullsiz;
   1619 		goto again;
   1620 	}
   1621 
   1622 	len = 3 * NFSX_UNSIGNED;	/* paranoia, probably can be 0 */
   1623 	nfsm_reply(siz);
   1624 	mp = mp2 = mb;
   1625 	bp = bpos;
   1626 	be = bp + M_TRAILINGSPACE(mp);
   1627 
   1628 	/* Loop through the records and build reply */
   1629 	while (cpos < cend) {
   1630 		if (dp->d_fileno != 0) {
   1631 			nlen = dp->d_namlen;
   1632 			rem = nfsm_rndup(nlen)-nlen;
   1633 
   1634 			/*
   1635 			 * For readdir_and_lookup get the vnode using
   1636 			 * the file number.
   1637 			 */
   1638 			if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
   1639 				goto invalid;
   1640 			bzero((caddr_t)&fl.fl_nfh, sizeof (nfsv2fh_t));
   1641 			fl.fl_nfh.fh_generic.fh_fsid =
   1642 				nvp->v_mount->mnt_stat.f_fsid;
   1643 			if (VFS_VPTOFH(nvp, &fl.fl_nfh.fh_generic.fh_fid)) {
   1644 				vput(nvp);
   1645 				goto invalid;
   1646 			}
   1647 			if (duration2) {
   1648 				(void) nqsrv_getlease(nvp, &duration2, NQL_READ,
   1649 					nfsd, nam, &cache2, &frev2, cred);
   1650 				fl.fl_duration = txdr_unsigned(duration2);
   1651 				fl.fl_cachable = txdr_unsigned(cache2);
   1652 				txdr_hyper(&frev2, fl.fl_frev);
   1653 			} else
   1654 				fl.fl_duration = 0;
   1655 			if (VOP_GETATTR(nvp, &va, cred, nfsd->nd_procp)) {
   1656 				vput(nvp);
   1657 				goto invalid;
   1658 			}
   1659 			vput(nvp);
   1660 			fp = (struct nfsv2_fattr *)&fl.fl_fattr;
   1661 			nfsm_srvfillattr;
   1662 			len += (4*NFSX_UNSIGNED + nlen + rem + NFSX_FH
   1663 				+ NFSX_NQFATTR);
   1664 			if (len > cnt) {
   1665 				eofflag = 0;
   1666 				break;
   1667 			}
   1668 			/*
   1669 			 * Build the directory record xdr from
   1670 			 * the dirent entry.
   1671 			 */
   1672 			nfsm_clget;
   1673 			*tl = nfs_true;
   1674 			bp += NFSX_UNSIGNED;
   1675 
   1676 			/*
   1677 			 * For readdir_and_lookup copy the stuff out.
   1678 			 */
   1679 			xfer = sizeof (struct flrep);
   1680 			cp = (caddr_t)&fl;
   1681 			while (xfer > 0) {
   1682 				nfsm_clget;
   1683 				if ((bp+xfer) > be)
   1684 					tsiz = be-bp;
   1685 				else
   1686 					tsiz = xfer;
   1687 				bcopy(cp, bp, tsiz);
   1688 				bp += tsiz;
   1689 				xfer -= tsiz;
   1690 				if (xfer > 0)
   1691 					cp += tsiz;
   1692 			}
   1693 			nfsm_clget;
   1694 			*tl = txdr_unsigned(dp->d_fileno);
   1695 			bp += NFSX_UNSIGNED;
   1696 			nfsm_clget;
   1697 			*tl = txdr_unsigned(nlen);
   1698 			bp += NFSX_UNSIGNED;
   1699 
   1700 			/* And loop around copying the name */
   1701 			xfer = nlen;
   1702 			cp = dp->d_name;
   1703 			while (xfer > 0) {
   1704 				nfsm_clget;
   1705 				if ((bp+xfer) > be)
   1706 					tsiz = be-bp;
   1707 				else
   1708 					tsiz = xfer;
   1709 				bcopy(cp, bp, tsiz);
   1710 				bp += tsiz;
   1711 				xfer -= tsiz;
   1712 				if (xfer > 0)
   1713 					cp += tsiz;
   1714 			}
   1715 			/* And null pad to a int32_t boundary */
   1716 			for (i = 0; i < rem; i++)
   1717 				*bp++ = '\0';
   1718 			nfsm_clget;
   1719 
   1720 			/* Finish off the record */
   1721 			*tl = txdr_unsigned(*cookie);
   1722 			bp += NFSX_UNSIGNED;
   1723 		}
   1724 invalid:
   1725 		cpos += dp->d_reclen;
   1726 		dp = (struct dirent *)cpos;
   1727 		cookie++;
   1728 	}
   1729 	vrele(vp);
   1730 	nfsm_clget;
   1731 	*tl = nfs_false;
   1732 	bp += NFSX_UNSIGNED;
   1733 	nfsm_clget;
   1734 	if (eofflag)
   1735 		*tl = nfs_true;
   1736 	else
   1737 		*tl = nfs_false;
   1738 	bp += NFSX_UNSIGNED;
   1739 	if (mp != mb) {
   1740 		if (bp < be)
   1741 			mp->m_len = bp - mtod(mp, caddr_t);
   1742 	} else
   1743 		mp->m_len += bp - bpos;
   1744 	FREE(cookiebuf, M_TEMP);
   1745 	FREE(rbuf, M_TEMP);
   1746 	nfsm_srvdone;
   1747 }
   1748 
   1749 /*
   1750  * nfs statfs service
   1751  */
   1752 nfsrv_statfs(nfsd, mrep, md, dpos, cred, nam, mrq)
   1753 	struct nfsd *nfsd;
   1754 	struct mbuf *mrep, *md;
   1755 	caddr_t dpos;
   1756 	struct ucred *cred;
   1757 	struct mbuf *nam, **mrq;
   1758 {
   1759 	register struct statfs *sf;
   1760 	register struct nfsv2_statfs *sfp;
   1761 	register u_int32_t *tl;
   1762 	register int32_t t1;
   1763 	caddr_t bpos;
   1764 	int error = 0, rdonly, cache, isnq;
   1765 	char *cp2;
   1766 	struct mbuf *mb, *mb2, *mreq;
   1767 	struct vnode *vp;
   1768 	nfsv2fh_t nfh;
   1769 	fhandle_t *fhp;
   1770 	struct statfs statfs;
   1771 	u_quad_t frev;
   1772 
   1773 	fhp = &nfh.fh_generic;
   1774 	isnq = (nfsd->nd_nqlflag != NQL_NOVAL);
   1775 	nfsm_srvmtofh(fhp);
   1776 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
   1777 		nfsm_reply(0);
   1778 	sf = &statfs;
   1779 	error = VFS_STATFS(vp->v_mount, sf, nfsd->nd_procp);
   1780 	vput(vp);
   1781 	nfsm_reply(NFSX_STATFS(isnq));
   1782 	nfsm_build(sfp, struct nfsv2_statfs *, NFSX_STATFS(isnq));
   1783 	sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
   1784 	sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
   1785 	sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
   1786 	sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
   1787 	sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
   1788 	if (isnq) {
   1789 		sfp->sf_files = txdr_unsigned(sf->f_files);
   1790 		sfp->sf_ffree = txdr_unsigned(sf->f_ffree);
   1791 	}
   1792 	nfsm_srvdone;
   1793 }
   1794 
   1795 /*
   1796  * Null operation, used by clients to ping server
   1797  */
   1798 /* ARGSUSED */
   1799 nfsrv_null(nfsd, mrep, md, dpos, cred, nam, mrq)
   1800 	struct nfsd *nfsd;
   1801 	struct mbuf *mrep, *md;
   1802 	caddr_t dpos;
   1803 	struct ucred *cred;
   1804 	struct mbuf *nam, **mrq;
   1805 {
   1806 	caddr_t bpos;
   1807 	int error = VNOVAL, cache;
   1808 	struct mbuf *mb, *mreq;
   1809 	u_quad_t frev;
   1810 
   1811 	nfsm_reply(0);
   1812 	return (error);
   1813 }
   1814 
   1815 /*
   1816  * No operation, used for obsolete procedures
   1817  */
   1818 /* ARGSUSED */
   1819 nfsrv_noop(nfsd, mrep, md, dpos, cred, nam, mrq)
   1820 	struct nfsd *nfsd;
   1821 	struct mbuf *mrep, *md;
   1822 	caddr_t dpos;
   1823 	struct ucred *cred;
   1824 	struct mbuf *nam, **mrq;
   1825 {
   1826 	caddr_t bpos;
   1827 	int error, cache;
   1828 	struct mbuf *mb, *mreq;
   1829 	u_quad_t frev;
   1830 
   1831 	if (nfsd->nd_repstat)
   1832 		error = nfsd->nd_repstat;
   1833 	else
   1834 		error = EPROCUNAVAIL;
   1835 	nfsm_reply(0);
   1836 	return (error);
   1837 }
   1838 
   1839 /*
   1840  * Perform access checking for vnodes obtained from file handles that would
   1841  * refer to files already opened by a Unix client. You cannot just use
   1842  * vn_writechk() and VOP_ACCESS() for two reasons.
   1843  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
   1844  * 2 - The owner is to be given access irrespective of mode bits so that
   1845  *     processes that chmod after opening a file don't break. I don't like
   1846  *     this because it opens a security hole, but since the nfs server opens
   1847  *     a security hole the size of a barn door anyhow, what the heck.
   1848  */
   1849 nfsrv_access(vp, flags, cred, rdonly, p)
   1850 	register struct vnode *vp;
   1851 	int flags;
   1852 	register struct ucred *cred;
   1853 	int rdonly;
   1854 	struct proc *p;
   1855 {
   1856 	struct vattr vattr;
   1857 	int error;
   1858 	if (flags & VWRITE) {
   1859 		/* Just vn_writechk() changed to check rdonly */
   1860 		/*
   1861 		 * Disallow write attempts on read-only file systems;
   1862 		 * unless the file is a socket or a block or character
   1863 		 * device resident on the file system.
   1864 		 */
   1865 		if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
   1866 			switch (vp->v_type) {
   1867 			case VREG: case VDIR: case VLNK:
   1868 				return (EROFS);
   1869 			}
   1870 		}
   1871 		/*
   1872 		 * If there's shared text associated with
   1873 		 * the inode, try to free it up once.  If
   1874 		 * we fail, we can't allow writing.
   1875 		 */
   1876 		if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
   1877 			return (ETXTBSY);
   1878 	}
   1879 	if (error = VOP_GETATTR(vp, &vattr, cred, p))
   1880 		return (error);
   1881 	if ((error = VOP_ACCESS(vp, flags, cred, p)) &&
   1882 	    cred->cr_uid != vattr.va_uid)
   1883 		return (error);
   1884 	return (0);
   1885 }
   1886