Home | History | Annotate | Line # | Download | only in nfs
nfs_serv.c revision 1.40
      1 /*	$NetBSD: nfs_serv.c,v 1.40 1998/02/05 08:00:21 mrg 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.7 (Berkeley) 5/14/95
     39  */
     40 
     41 /*
     42  * nfs version 2 and 3 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  *	For Version 3, nfsm_reply() does not return for the error case, since
     58  *	most version 3 rpcs return more than the status for error cases.
     59  */
     60 
     61 #include <sys/param.h>
     62 #include <sys/systm.h>
     63 #include <sys/proc.h>
     64 #include <sys/file.h>
     65 #include <sys/namei.h>
     66 #include <sys/vnode.h>
     67 #include <sys/mount.h>
     68 #include <sys/socket.h>
     69 #include <sys/socketvar.h>
     70 #include <sys/mbuf.h>
     71 #include <sys/dirent.h>
     72 #include <sys/stat.h>
     73 #include <sys/kernel.h>
     74 #include <ufs/ufs/dir.h>
     75 
     76 #include <vm/vm.h>
     77 
     78 #if defined(UVM)
     79 #include <uvm/uvm_extern.h>
     80 #endif
     81 
     82 #include <nfs/nfsproto.h>
     83 #include <nfs/rpcv2.h>
     84 #include <nfs/nfs.h>
     85 #include <nfs/xdr_subs.h>
     86 #include <nfs/nfsm_subs.h>
     87 #include <nfs/nqnfs.h>
     88 #include <nfs/nfs_var.h>
     89 
     90 /* Global vars */
     91 extern u_int32_t nfs_xdrneg1;
     92 extern u_int32_t nfs_false, nfs_true;
     93 extern enum vtype nv3tov_type[8];
     94 extern struct nfsstats nfsstats;
     95 extern nfstype nfsv2_type[9];
     96 extern nfstype nfsv3_type[9];
     97 extern struct nfs_public nfs_pub;
     98 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
     99 
    100 /*
    101  * nfs v3 access service
    102  */
    103 int
    104 nfsrv3_access(nfsd, slp, procp, mrq)
    105 	struct nfsrv_descript *nfsd;
    106 	struct nfssvc_sock *slp;
    107 	struct proc *procp;
    108 	struct mbuf **mrq;
    109 {
    110 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    111 	struct mbuf *nam = nfsd->nd_nam;
    112 	caddr_t dpos = nfsd->nd_dpos;
    113 	struct ucred *cred = &nfsd->nd_cr;
    114 	struct vnode *vp;
    115 	nfsfh_t nfh;
    116 	fhandle_t *fhp;
    117 	register u_int32_t *tl;
    118 	register int32_t t1;
    119 	caddr_t bpos;
    120 	int error = 0, rdonly, cache = 0, getret;
    121 	char *cp2;
    122 	struct mbuf *mb, *mreq, *mb2;
    123 	struct vattr va;
    124 	u_long inmode, testmode, outmode;
    125 	u_quad_t frev;
    126 
    127 	fhp = &nfh.fh_generic;
    128 	nfsm_srvmtofh(fhp);
    129 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    130 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
    131 	    (nfsd->nd_flag & ND_KERBAUTH), FALSE);
    132 	if (error) {
    133 		nfsm_reply(NFSX_UNSIGNED);
    134 		nfsm_srvpostop_attr(1, (struct vattr *)0);
    135 		return (0);
    136 	}
    137 	inmode = fxdr_unsigned(u_int32_t, *tl);
    138 	outmode = 0;
    139 	if ((inmode & NFSV3ACCESS_READ) &&
    140 	    nfsrv_access(vp, VREAD, cred, rdonly, procp, 0) == 0)
    141 		outmode |= NFSV3ACCESS_READ;
    142 	if (vp->v_type != VDIR) {
    143 		testmode = inmode & (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
    144 		if (testmode &&
    145 		    nfsrv_access(vp, VWRITE, cred, rdonly, procp, 0) == 0)
    146 			outmode |= testmode;
    147 		if ((inmode & NFSV3ACCESS_EXECUTE) &&
    148 		    nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0) == 0)
    149 			outmode |= NFSV3ACCESS_EXECUTE;
    150 	} else {
    151 		testmode = inmode & (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
    152 		    NFSV3ACCESS_DELETE);
    153 		if (testmode &&
    154 		    nfsrv_access(vp, VWRITE, cred, rdonly, procp, 0) == 0)
    155 			outmode |= testmode;
    156 		if ((inmode & NFSV3ACCESS_LOOKUP) &&
    157 		    nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0) == 0)
    158 			outmode |= NFSV3ACCESS_LOOKUP;
    159 	}
    160 	getret = VOP_GETATTR(vp, &va, cred, procp);
    161 	vput(vp);
    162 	nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
    163 	nfsm_srvpostop_attr(getret, &va);
    164 	nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    165 	*tl = txdr_unsigned(outmode);
    166 	nfsm_srvdone;
    167 }
    168 
    169 /*
    170  * nfs getattr service
    171  */
    172 int
    173 nfsrv_getattr(nfsd, slp, procp, mrq)
    174 	struct nfsrv_descript *nfsd;
    175 	struct nfssvc_sock *slp;
    176 	struct proc *procp;
    177 	struct mbuf **mrq;
    178 {
    179 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    180 	struct mbuf *nam = nfsd->nd_nam;
    181 	caddr_t dpos = nfsd->nd_dpos;
    182 	struct ucred *cred = &nfsd->nd_cr;
    183 	register struct nfs_fattr *fp;
    184 	struct vattr va;
    185 	struct vnode *vp;
    186 	nfsfh_t nfh;
    187 	fhandle_t *fhp;
    188 	register u_int32_t *tl;
    189 	register int32_t t1;
    190 	caddr_t bpos;
    191 	int error = 0, rdonly, cache;
    192 	char *cp2;
    193 	struct mbuf *mb, *mb2, *mreq;
    194 	u_quad_t frev;
    195 
    196 	fhp = &nfh.fh_generic;
    197 	nfsm_srvmtofh(fhp);
    198 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
    199 	    (nfsd->nd_flag & ND_KERBAUTH), FALSE);
    200 	if (error) {
    201 		nfsm_reply(0);
    202 		return (0);
    203 	}
    204 	nqsrv_getl(vp, ND_READ);
    205 	error = VOP_GETATTR(vp, &va, cred, procp);
    206 	vput(vp);
    207 	nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
    208 	if (error)
    209 		return (0);
    210 	nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
    211 	nfsm_srvfillattr(&va, fp);
    212 	nfsm_srvdone;
    213 }
    214 
    215 /*
    216  * nfs setattr service
    217  */
    218 int
    219 nfsrv_setattr(nfsd, slp, procp, mrq)
    220 	struct nfsrv_descript *nfsd;
    221 	struct nfssvc_sock *slp;
    222 	struct proc *procp;
    223 	struct mbuf **mrq;
    224 {
    225 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    226 	struct mbuf *nam = nfsd->nd_nam;
    227 	caddr_t dpos = nfsd->nd_dpos;
    228 	struct ucred *cred = &nfsd->nd_cr;
    229 	struct vattr va, preat;
    230 	register struct nfsv2_sattr *sp;
    231 	register struct nfs_fattr *fp;
    232 	struct vnode *vp;
    233 	nfsfh_t nfh;
    234 	fhandle_t *fhp;
    235 	register u_int32_t *tl;
    236 	register int32_t t1;
    237 	caddr_t bpos;
    238 	int error = 0, rdonly, cache, preat_ret = 1, postat_ret = 1;
    239 	int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
    240 	char *cp2;
    241 	struct mbuf *mb, *mb2, *mreq;
    242 	u_quad_t frev;
    243 	struct timespec guard;
    244 
    245 	fhp = &nfh.fh_generic;
    246 	nfsm_srvmtofh(fhp);
    247 	VATTR_NULL(&va);
    248 	if (v3) {
    249 		nfsm_srvsattr(&va);
    250 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    251 		gcheck = fxdr_unsigned(int, *tl);
    252 		if (gcheck) {
    253 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    254 			fxdr_nfsv3time(tl, &guard);
    255 		}
    256 	} else {
    257 		nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
    258 		/*
    259 		 * Nah nah nah nah na nah
    260 		 * There is a bug in the Sun client that puts 0xffff in the mode
    261 		 * field of sattr when it should put in 0xffffffff. The u_short
    262 		 * doesn't sign extend.
    263 		 * --> check the low order 2 bytes for 0xffff
    264 		 */
    265 		if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
    266 			va.va_mode = nfstov_mode(sp->sa_mode);
    267 		if (sp->sa_uid != nfs_xdrneg1)
    268 			va.va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
    269 		if (sp->sa_gid != nfs_xdrneg1)
    270 			va.va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
    271 		if (sp->sa_size != nfs_xdrneg1)
    272 			va.va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
    273 		if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
    274 #ifdef notyet
    275 			fxdr_nfsv2time(&sp->sa_atime, &va.va_atime);
    276 #else
    277 			va.va_atime.tv_sec =
    278 				fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
    279 			va.va_atime.tv_nsec = 0;
    280 #endif
    281 		}
    282 		if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
    283 			fxdr_nfsv2time(&sp->sa_mtime, &va.va_mtime);
    284 
    285 	}
    286 
    287 	/*
    288 	 * Now that we have all the fields, lets do it.
    289 	 */
    290 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
    291 	    (nfsd->nd_flag & ND_KERBAUTH), FALSE);
    292 	if (error) {
    293 		nfsm_reply(2 * NFSX_UNSIGNED);
    294 		nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
    295 		return (0);
    296 	}
    297 	nqsrv_getl(vp, ND_WRITE);
    298 	if (v3) {
    299 		error = preat_ret = VOP_GETATTR(vp, &preat, cred, procp);
    300 		if (!error && gcheck &&
    301 			(preat.va_ctime.tv_sec != guard.tv_sec ||
    302 			 preat.va_ctime.tv_nsec != guard.tv_nsec))
    303 			error = NFSERR_NOT_SYNC;
    304 		if (error) {
    305 			vput(vp);
    306 			nfsm_reply(NFSX_WCCDATA(v3));
    307 			nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
    308 			return (0);
    309 		}
    310 	}
    311 
    312 	/*
    313 	 * If the size is being changed write acces is required, otherwise
    314 	 * just check for a read only file system.
    315 	 */
    316 	if (va.va_size == ((u_quad_t)((quad_t) -1))) {
    317 		if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
    318 			error = EROFS;
    319 			goto out;
    320 		}
    321 	} else {
    322 		if (vp->v_type == VDIR) {
    323 			error = EISDIR;
    324 			goto out;
    325 		} else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
    326 			procp, 0)) != 0)
    327 			goto out;
    328 	}
    329 	error = VOP_SETATTR(vp, &va, cred, procp);
    330 	postat_ret = VOP_GETATTR(vp, &va, cred, procp);
    331 	if (!error)
    332 		error = postat_ret;
    333 out:
    334 	vput(vp);
    335 	nfsm_reply(NFSX_WCCORFATTR(v3));
    336 	if (v3) {
    337 		nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
    338 		return (0);
    339 	} else {
    340 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
    341 		nfsm_srvfillattr(&va, fp);
    342 	}
    343 	nfsm_srvdone;
    344 }
    345 
    346 /*
    347  * nfs lookup rpc
    348  */
    349 int
    350 nfsrv_lookup(nfsd, slp, procp, mrq)
    351 	struct nfsrv_descript *nfsd;
    352 	struct nfssvc_sock *slp;
    353 	struct proc *procp;
    354 	struct mbuf **mrq;
    355 {
    356 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    357 	struct mbuf *nam = nfsd->nd_nam;
    358 	caddr_t dpos = nfsd->nd_dpos;
    359 	struct ucred *cred = &nfsd->nd_cr;
    360 	register struct nfs_fattr *fp;
    361 	struct nameidata nd, ind, *ndp = &nd;
    362 	struct vnode *vp, *dirp;
    363 	nfsfh_t nfh;
    364 	fhandle_t *fhp;
    365 	register caddr_t cp;
    366 	register u_int32_t *tl;
    367 	register int32_t t1;
    368 	caddr_t bpos;
    369 	int error = 0, cache, len, dirattr_ret = 1;
    370 	int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
    371 	char *cp2;
    372 	struct mbuf *mb, *mb2, *mreq;
    373 	struct vattr va, dirattr;
    374 	u_quad_t frev;
    375 
    376 	fhp = &nfh.fh_generic;
    377 	nfsm_srvmtofh(fhp);
    378 	nfsm_srvnamesiz(len);
    379 
    380 	pubflag = nfs_ispublicfh(fhp);
    381 
    382 	nd.ni_cnd.cn_cred = cred;
    383 	nd.ni_cnd.cn_nameiop = LOOKUP;
    384 	nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
    385 	error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
    386 		&dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
    387 
    388 	if (!error && pubflag) {
    389 		if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
    390 			/*
    391 			 * Setup call to lookup() to see if we can find
    392 			 * the index file. Arguably, this doesn't belong
    393 			 * in a kernel.. Ugh.
    394 			 */
    395 			ind = nd;
    396 			VOP_UNLOCK(nd.ni_vp);
    397 			ind.ni_pathlen = strlen(nfs_pub.np_index);
    398 			ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
    399 			    nfs_pub.np_index;
    400 			ind.ni_startdir = nd.ni_vp;
    401 			VREF(ind.ni_startdir);
    402 			error = lookup(&ind);
    403 			if (!error) {
    404 				/*
    405 				 * Found an index file. Get rid of
    406 				 * the old references.
    407 				 */
    408 				if (dirp)
    409 					vrele(dirp);
    410 				dirp = nd.ni_vp;
    411 				vrele(nd.ni_startdir);
    412 				ndp = &ind;
    413 			} else
    414 				error = 0;
    415 		}
    416 		/*
    417 		 * If the public filehandle was used, check that this lookup
    418 		 * didn't result in a filehandle outside the publicly exported
    419 		 * filesystem.
    420 		 */
    421 
    422 		if (!error && ndp->ni_vp->v_mount != nfs_pub.np_mount) {
    423 			vput(nd.ni_vp);
    424 			error = EPERM;
    425 		}
    426 	}
    427 
    428 	if (dirp) {
    429 		if (v3)
    430 			dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
    431 				procp);
    432 		vrele(dirp);
    433 	}
    434 
    435 	if (error) {
    436 		nfsm_reply(NFSX_POSTOPATTR(v3));
    437 		nfsm_srvpostop_attr(dirattr_ret, &dirattr);
    438 		return (0);
    439 	}
    440 
    441 	nqsrv_getl(ndp->ni_startdir, ND_READ);
    442 	vrele(ndp->ni_startdir);
    443 	FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    444 	vp = ndp->ni_vp;
    445 	bzero((caddr_t)fhp, sizeof(nfh));
    446 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
    447 	error = VFS_VPTOFH(vp, &fhp->fh_fid);
    448 	if (!error)
    449 		error = VOP_GETATTR(vp, &va, cred, procp);
    450 	vput(vp);
    451 	nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
    452 	if (error) {
    453 		nfsm_srvpostop_attr(dirattr_ret, &dirattr);
    454 		return (0);
    455 	}
    456 	nfsm_srvfhtom(fhp, v3);
    457 	if (v3) {
    458 		nfsm_srvpostop_attr(0, &va);
    459 		nfsm_srvpostop_attr(dirattr_ret, &dirattr);
    460 	} else {
    461 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
    462 		nfsm_srvfillattr(&va, fp);
    463 	}
    464 	nfsm_srvdone;
    465 }
    466 
    467 /*
    468  * nfs readlink service
    469  */
    470 int
    471 nfsrv_readlink(nfsd, slp, procp, mrq)
    472 	struct nfsrv_descript *nfsd;
    473 	struct nfssvc_sock *slp;
    474 	struct proc *procp;
    475 	struct mbuf **mrq;
    476 {
    477 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    478 	struct mbuf *nam = nfsd->nd_nam;
    479 	caddr_t dpos = nfsd->nd_dpos;
    480 	struct ucred *cred = &nfsd->nd_cr;
    481 	struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
    482 	register struct iovec *ivp = iv;
    483 	register struct mbuf *mp;
    484 	register u_int32_t *tl;
    485 	register int32_t t1;
    486 	caddr_t bpos;
    487 	int error = 0, rdonly, cache, i, tlen, len, getret;
    488 	int v3 = (nfsd->nd_flag & ND_NFSV3);
    489 	char *cp2;
    490 	struct mbuf *mb, *mb2, *mp2 = NULL, *mp3 = NULL, *mreq;
    491 	struct vnode *vp;
    492 	struct vattr attr;
    493 	nfsfh_t nfh;
    494 	fhandle_t *fhp;
    495 	struct uio io, *uiop = &io;
    496 	u_quad_t frev;
    497 
    498 	fhp = &nfh.fh_generic;
    499 	nfsm_srvmtofh(fhp);
    500 	len = 0;
    501 	i = 0;
    502 	while (len < NFS_MAXPATHLEN) {
    503 		MGET(mp, M_WAIT, MT_DATA);
    504 		MCLGET(mp, M_WAIT);
    505 		mp->m_len = NFSMSIZ(mp);
    506 		if (len == 0)
    507 			mp3 = mp2 = mp;
    508 		else {
    509 			mp2->m_next = mp;
    510 			mp2 = mp;
    511 		}
    512 		if ((len+mp->m_len) > NFS_MAXPATHLEN) {
    513 			mp->m_len = NFS_MAXPATHLEN-len;
    514 			len = NFS_MAXPATHLEN;
    515 		} else
    516 			len += mp->m_len;
    517 		ivp->iov_base = mtod(mp, caddr_t);
    518 		ivp->iov_len = mp->m_len;
    519 		i++;
    520 		ivp++;
    521 	}
    522 	uiop->uio_iov = iv;
    523 	uiop->uio_iovcnt = i;
    524 	uiop->uio_offset = 0;
    525 	uiop->uio_resid = len;
    526 	uiop->uio_rw = UIO_READ;
    527 	uiop->uio_segflg = UIO_SYSSPACE;
    528 	uiop->uio_procp = (struct proc *)0;
    529 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
    530 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
    531 	if (error) {
    532 		m_freem(mp3);
    533 		nfsm_reply(2 * NFSX_UNSIGNED);
    534 		nfsm_srvpostop_attr(1, (struct vattr *)0);
    535 		return (0);
    536 	}
    537 	if (vp->v_type != VLNK) {
    538 		if (v3)
    539 			error = EINVAL;
    540 		else
    541 			error = ENXIO;
    542 		goto out;
    543 	}
    544 	nqsrv_getl(vp, ND_READ);
    545 	error = VOP_READLINK(vp, uiop, cred);
    546 out:
    547 	getret = VOP_GETATTR(vp, &attr, cred, procp);
    548 	vput(vp);
    549 	if (error)
    550 		m_freem(mp3);
    551 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
    552 	if (v3) {
    553 		nfsm_srvpostop_attr(getret, &attr);
    554 		if (error)
    555 			return (0);
    556 	}
    557 	if (uiop->uio_resid > 0) {
    558 		len -= uiop->uio_resid;
    559 		tlen = nfsm_rndup(len);
    560 		nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
    561 	}
    562 	nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    563 	*tl = txdr_unsigned(len);
    564 	mb->m_next = mp3;
    565 	nfsm_srvdone;
    566 }
    567 
    568 /*
    569  * nfs read service
    570  */
    571 int
    572 nfsrv_read(nfsd, slp, procp, mrq)
    573 	struct nfsrv_descript *nfsd;
    574 	struct nfssvc_sock *slp;
    575 	struct proc *procp;
    576 	struct mbuf **mrq;
    577 {
    578 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    579 	struct mbuf *nam = nfsd->nd_nam;
    580 	caddr_t dpos = nfsd->nd_dpos;
    581 	struct ucred *cred = &nfsd->nd_cr;
    582 	register struct iovec *iv;
    583 	struct iovec *iv2;
    584 	register struct mbuf *m;
    585 	register struct nfs_fattr *fp;
    586 	register u_int32_t *tl;
    587 	register int32_t t1;
    588 	register int i;
    589 	caddr_t bpos;
    590 	int error = 0, rdonly, cache, cnt, len, left, siz, tlen, getret;
    591 	int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
    592 	char *cp2;
    593 	struct mbuf *mb, *mb2, *mreq;
    594 	struct mbuf *m2;
    595 	struct vnode *vp;
    596 	nfsfh_t nfh;
    597 	fhandle_t *fhp;
    598 	struct uio io, *uiop = &io;
    599 	struct vattr va;
    600 	off_t off;
    601 	u_quad_t frev;
    602 
    603 	fhp = &nfh.fh_generic;
    604 	nfsm_srvmtofh(fhp);
    605 	if (v3) {
    606 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    607 		fxdr_hyper(tl, &off);
    608 	} else {
    609 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    610 		off = (off_t)fxdr_unsigned(u_int32_t, *tl);
    611 	}
    612 	nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
    613 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
    614 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
    615 	if (error) {
    616 		nfsm_reply(2 * NFSX_UNSIGNED);
    617 		nfsm_srvpostop_attr(1, (struct vattr *)0);
    618 		return (0);
    619 	}
    620 	if (vp->v_type != VREG) {
    621 		if (v3)
    622 			error = EINVAL;
    623 		else
    624 			error = (vp->v_type == VDIR) ? EISDIR : EACCES;
    625 	}
    626 	if (!error) {
    627 	    nqsrv_getl(vp, ND_READ);
    628 	    if ((error = nfsrv_access(vp, VREAD, cred, rdonly, procp, 1)) != 0)
    629 		error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 1);
    630 	}
    631 	getret = VOP_GETATTR(vp, &va, cred, procp);
    632 	if (!error)
    633 		error = getret;
    634 	if (error) {
    635 		vput(vp);
    636 		nfsm_reply(NFSX_POSTOPATTR(v3));
    637 		nfsm_srvpostop_attr(getret, &va);
    638 		return (0);
    639 	}
    640 	if (off >= va.va_size)
    641 		cnt = 0;
    642 	else if ((off + reqlen) > va.va_size)
    643 		cnt = nfsm_rndup(va.va_size - off);
    644 	else
    645 		cnt = reqlen;
    646 	nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
    647 	if (v3) {
    648 		nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
    649 		*tl++ = nfs_true;
    650 		fp = (struct nfs_fattr *)tl;
    651 		tl += (NFSX_V3FATTR / sizeof (u_int32_t));
    652 	} else {
    653 		nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
    654 		fp = (struct nfs_fattr *)tl;
    655 		tl += (NFSX_V2FATTR / sizeof (u_int32_t));
    656 	}
    657 	len = left = cnt;
    658 	if (cnt > 0) {
    659 		/*
    660 		 * Generate the mbuf list with the uio_iov ref. to it.
    661 		 */
    662 		i = 0;
    663 		m = m2 = mb;
    664 		while (left > 0) {
    665 			siz = min(M_TRAILINGSPACE(m), left);
    666 			if (siz > 0) {
    667 				left -= siz;
    668 				i++;
    669 			}
    670 			if (left > 0) {
    671 				MGET(m, M_WAIT, MT_DATA);
    672 				MCLGET(m, M_WAIT);
    673 				m->m_len = 0;
    674 				m2->m_next = m;
    675 				m2 = m;
    676 			}
    677 		}
    678 		MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
    679 		       M_TEMP, M_WAITOK);
    680 		uiop->uio_iov = iv2 = iv;
    681 		m = mb;
    682 		left = cnt;
    683 		i = 0;
    684 		while (left > 0) {
    685 			if (m == NULL)
    686 				panic("nfsrv_read iov");
    687 			siz = min(M_TRAILINGSPACE(m), left);
    688 			if (siz > 0) {
    689 				iv->iov_base = mtod(m, caddr_t) + m->m_len;
    690 				iv->iov_len = siz;
    691 				m->m_len += siz;
    692 				left -= siz;
    693 				iv++;
    694 				i++;
    695 			}
    696 			m = m->m_next;
    697 		}
    698 		uiop->uio_iovcnt = i;
    699 		uiop->uio_offset = off;
    700 		uiop->uio_resid = cnt;
    701 		uiop->uio_rw = UIO_READ;
    702 		uiop->uio_segflg = UIO_SYSSPACE;
    703 		error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
    704 		off = uiop->uio_offset;
    705 		FREE((caddr_t)iv2, M_TEMP);
    706 		if (error || (getret = VOP_GETATTR(vp, &va, cred, procp)) != 0){
    707 			if (!error)
    708 				error = getret;
    709 			m_freem(mreq);
    710 			vput(vp);
    711 			nfsm_reply(NFSX_POSTOPATTR(v3));
    712 			nfsm_srvpostop_attr(getret, &va);
    713 			return (0);
    714 		}
    715 	} else
    716 		uiop->uio_resid = 0;
    717 	vput(vp);
    718 	nfsm_srvfillattr(&va, fp);
    719 	len -= uiop->uio_resid;
    720 	tlen = nfsm_rndup(len);
    721 	if (cnt != tlen || tlen != len)
    722 		nfsm_adj(mb, cnt - tlen, tlen - len);
    723 	if (v3) {
    724 		*tl++ = txdr_unsigned(len);
    725 		if (len < reqlen)
    726 			*tl++ = nfs_true;
    727 		else
    728 			*tl++ = nfs_false;
    729 	}
    730 	*tl = txdr_unsigned(len);
    731 	nfsm_srvdone;
    732 }
    733 
    734 /*
    735  * nfs write service
    736  */
    737 int
    738 nfsrv_write(nfsd, slp, procp, mrq)
    739 	struct nfsrv_descript *nfsd;
    740 	struct nfssvc_sock *slp;
    741 	struct proc *procp;
    742 	struct mbuf **mrq;
    743 {
    744 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    745 	struct mbuf *nam = nfsd->nd_nam;
    746 	caddr_t dpos = nfsd->nd_dpos;
    747 	struct ucred *cred = &nfsd->nd_cr;
    748 	register struct iovec *ivp;
    749 	register int i, cnt;
    750 	register struct mbuf *mp;
    751 	register struct nfs_fattr *fp;
    752 	struct iovec *iv;
    753 	struct vattr va, forat;
    754 	register u_int32_t *tl;
    755 	register int32_t t1;
    756 	caddr_t bpos;
    757 	int error = 0, rdonly, cache, len, forat_ret = 1;
    758 	int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
    759 	int stable = NFSV3WRITE_FILESYNC;
    760 	int v3 = (nfsd->nd_flag & ND_NFSV3);
    761 	char *cp2;
    762 	struct mbuf *mb, *mb2, *mreq;
    763 	struct vnode *vp;
    764 	nfsfh_t nfh;
    765 	fhandle_t *fhp;
    766 	struct uio io, *uiop = &io;
    767 	off_t off;
    768 	u_quad_t frev;
    769 
    770 	if (mrep == NULL) {
    771 		*mrq = NULL;
    772 		return (0);
    773 	}
    774 	fhp = &nfh.fh_generic;
    775 	nfsm_srvmtofh(fhp);
    776 	if (v3) {
    777 		nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
    778 		fxdr_hyper(tl, &off);
    779 		tl += 3;
    780 		stable = fxdr_unsigned(int, *tl++);
    781 	} else {
    782 		nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
    783 		off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
    784 		tl += 2;
    785 	}
    786 	retlen = len = fxdr_unsigned(int32_t, *tl);
    787 	cnt = i = 0;
    788 
    789 	/*
    790 	 * For NFS Version 2, it is not obvious what a write of zero length
    791 	 * should do, but I might as well be consistent with Version 3,
    792 	 * which is to return ok so long as there are no permission problems.
    793 	 */
    794 	if (len > 0) {
    795 	    zeroing = 1;
    796 	    mp = mrep;
    797 	    while (mp) {
    798 		if (mp == md) {
    799 			zeroing = 0;
    800 			adjust = dpos - mtod(mp, caddr_t);
    801 			mp->m_len -= adjust;
    802 			if (mp->m_len > 0 && adjust > 0)
    803 				NFSMADV(mp, adjust);
    804 		}
    805 		if (zeroing)
    806 			mp->m_len = 0;
    807 		else if (mp->m_len > 0) {
    808 			i += mp->m_len;
    809 			if (i > len) {
    810 				mp->m_len -= (i - len);
    811 				zeroing	= 1;
    812 			}
    813 			if (mp->m_len > 0)
    814 				cnt++;
    815 		}
    816 		mp = mp->m_next;
    817 	    }
    818 	}
    819 	if (len > NFS_MAXDATA || len < 0 || i < len) {
    820 		error = EIO;
    821 		nfsm_reply(2 * NFSX_UNSIGNED);
    822 		nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
    823 		return (0);
    824 	}
    825 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
    826 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
    827 	if (error) {
    828 		nfsm_reply(2 * NFSX_UNSIGNED);
    829 		nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
    830 		return (0);
    831 	}
    832 	if (v3)
    833 		forat_ret = VOP_GETATTR(vp, &forat, cred, procp);
    834 	if (vp->v_type != VREG) {
    835 		if (v3)
    836 			error = EINVAL;
    837 		else
    838 			error = (vp->v_type == VDIR) ? EISDIR : EACCES;
    839 	}
    840 	if (!error) {
    841 		nqsrv_getl(vp, ND_WRITE);
    842 		error = nfsrv_access(vp, VWRITE, cred, rdonly, procp, 1);
    843 	}
    844 	if (error) {
    845 		vput(vp);
    846 		nfsm_reply(NFSX_WCCDATA(v3));
    847 		nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
    848 		return (0);
    849 	}
    850 
    851 	if (len > 0) {
    852 	    MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
    853 		M_WAITOK);
    854 	    uiop->uio_iov = iv = ivp;
    855 	    uiop->uio_iovcnt = cnt;
    856 	    mp = mrep;
    857 	    while (mp) {
    858 		if (mp->m_len > 0) {
    859 			ivp->iov_base = mtod(mp, caddr_t);
    860 			ivp->iov_len = mp->m_len;
    861 			ivp++;
    862 		}
    863 		mp = mp->m_next;
    864 	    }
    865 
    866 	    /*
    867 	     * XXX
    868 	     * The IO_METASYNC flag indicates that all metadata (and not just
    869 	     * enough to ensure data integrity) mus be written to stable storage
    870 	     * synchronously.
    871 	     * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
    872 	     */
    873 	    if (stable == NFSV3WRITE_UNSTABLE)
    874 		ioflags = IO_NODELOCKED;
    875 	    else if (stable == NFSV3WRITE_DATASYNC)
    876 		ioflags = (IO_SYNC | IO_NODELOCKED);
    877 	    else
    878 		ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
    879 	    uiop->uio_resid = len;
    880 	    uiop->uio_rw = UIO_WRITE;
    881 	    uiop->uio_segflg = UIO_SYSSPACE;
    882 	    uiop->uio_procp = (struct proc *)0;
    883 	    uiop->uio_offset = off;
    884 	    error = VOP_WRITE(vp, uiop, ioflags, cred);
    885 	    nfsstats.srvvop_writes++;
    886 	    FREE((caddr_t)iv, M_TEMP);
    887 	}
    888 	aftat_ret = VOP_GETATTR(vp, &va, cred, procp);
    889 	vput(vp);
    890 	if (!error)
    891 		error = aftat_ret;
    892 	nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
    893 		2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
    894 	if (v3) {
    895 		nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
    896 		if (error)
    897 			return (0);
    898 		nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
    899 		*tl++ = txdr_unsigned(retlen);
    900 		if (stable == NFSV3WRITE_UNSTABLE)
    901 			*tl++ = txdr_unsigned(stable);
    902 		else
    903 			*tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
    904 		/*
    905 		 * Actually, there is no need to txdr these fields,
    906 		 * but it may make the values more human readable,
    907 		 * for debugging purposes.
    908 		 */
    909 		*tl++ = txdr_unsigned(boottime.tv_sec);
    910 		*tl = txdr_unsigned(boottime.tv_usec);
    911 	} else {
    912 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
    913 		nfsm_srvfillattr(&va, fp);
    914 	}
    915 	nfsm_srvdone;
    916 }
    917 
    918 /*
    919  * NFS write service with write gathering support. Called when
    920  * nfsrvw_procrastinate > 0.
    921  * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
    922  * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
    923  * Jan. 1994.
    924  */
    925 int
    926 nfsrv_writegather(ndp, slp, procp, mrq)
    927 	struct nfsrv_descript **ndp;
    928 	struct nfssvc_sock *slp;
    929 	struct proc *procp;
    930 	struct mbuf **mrq;
    931 {
    932 	register struct iovec *ivp;
    933 	register struct mbuf *mp;
    934 	register struct nfsrv_descript *wp, *nfsd, *owp, *swp;
    935 	register struct nfs_fattr *fp;
    936 	register int i = 0;
    937 	struct iovec *iov;
    938 	struct nfsrvw_delayhash *wpp;
    939 	struct ucred *cred;
    940 	struct vattr va, forat;
    941 	register u_int32_t *tl;
    942 	register int32_t t1;
    943 	caddr_t bpos, dpos;
    944 	int error = 0, rdonly, cache, len = 0, forat_ret = 1;
    945 	int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
    946 	char *cp2;
    947 	struct mbuf *mb, *mb2, *mreq, *mrep, *md;
    948 	struct vnode *vp;
    949 	struct uio io, *uiop = &io;
    950 	u_quad_t frev, cur_usec;
    951 
    952 	*mrq = NULL;
    953 	if (*ndp) {
    954 	    nfsd = *ndp;
    955 	    *ndp = NULL;
    956 	    mrep = nfsd->nd_mrep;
    957 	    md = nfsd->nd_md;
    958 	    dpos = nfsd->nd_dpos;
    959 	    cred = &nfsd->nd_cr;
    960 	    v3 = (nfsd->nd_flag & ND_NFSV3);
    961 	    LIST_INIT(&nfsd->nd_coalesce);
    962 	    nfsd->nd_mreq = NULL;
    963 	    nfsd->nd_stable = NFSV3WRITE_FILESYNC;
    964 	    cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec;
    965 	    nfsd->nd_time = cur_usec + nfsrvw_procrastinate;
    966 
    967 	    /*
    968 	     * Now, get the write header..
    969 	     */
    970 	    nfsm_srvmtofh(&nfsd->nd_fh);
    971 	    if (v3) {
    972 		nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
    973 		fxdr_hyper(tl, &nfsd->nd_off);
    974 		tl += 3;
    975 		nfsd->nd_stable = fxdr_unsigned(int, *tl++);
    976 	    } else {
    977 		nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
    978 		nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
    979 		tl += 2;
    980 	    }
    981 	    len = fxdr_unsigned(int32_t, *tl);
    982 	    nfsd->nd_len = len;
    983 	    nfsd->nd_eoff = nfsd->nd_off + len;
    984 
    985 	    /*
    986 	     * Trim the header out of the mbuf list and trim off any trailing
    987 	     * junk so that the mbuf list has only the write data.
    988 	     */
    989 	    zeroing = 1;
    990 	    i = 0;
    991 	    mp = mrep;
    992 	    while (mp) {
    993 		if (mp == md) {
    994 		    zeroing = 0;
    995 		    adjust = dpos - mtod(mp, caddr_t);
    996 		    mp->m_len -= adjust;
    997 		    if (mp->m_len > 0 && adjust > 0)
    998 			NFSMADV(mp, adjust);
    999 		}
   1000 		if (zeroing)
   1001 		    mp->m_len = 0;
   1002 		else {
   1003 		    i += mp->m_len;
   1004 		    if (i > len) {
   1005 			mp->m_len -= (i - len);
   1006 			zeroing = 1;
   1007 		    }
   1008 		}
   1009 		mp = mp->m_next;
   1010 	    }
   1011 	    if (len > NFS_MAXDATA || len < 0  || i < len) {
   1012 nfsmout:
   1013 		m_freem(mrep);
   1014 		error = EIO;
   1015 		nfsm_writereply(2 * NFSX_UNSIGNED, v3);
   1016 		if (v3)
   1017 		    nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
   1018 		nfsd->nd_mreq = mreq;
   1019 		nfsd->nd_mrep = NULL;
   1020 		nfsd->nd_time = 0;
   1021 	    }
   1022 
   1023 	    /*
   1024 	     * Add this entry to the hash and time queues.
   1025 	     */
   1026 	    s = splsoftclock();
   1027 	    owp = NULL;
   1028 	    wp = slp->ns_tq.lh_first;
   1029 	    while (wp && wp->nd_time < nfsd->nd_time) {
   1030 		owp = wp;
   1031 		wp = wp->nd_tq.le_next;
   1032 	    }
   1033 	    if (owp) {
   1034 		LIST_INSERT_AFTER(owp, nfsd, nd_tq);
   1035 	    } else {
   1036 		LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
   1037 	    }
   1038 	    if (nfsd->nd_mrep) {
   1039 		wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
   1040 		owp = NULL;
   1041 		wp = wpp->lh_first;
   1042 		while (wp &&
   1043 		    bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
   1044 		    owp = wp;
   1045 		    wp = wp->nd_hash.le_next;
   1046 		}
   1047 		while (wp && wp->nd_off < nfsd->nd_off &&
   1048 		    !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
   1049 		    owp = wp;
   1050 		    wp = wp->nd_hash.le_next;
   1051 		}
   1052 		if (owp) {
   1053 		    LIST_INSERT_AFTER(owp, nfsd, nd_hash);
   1054 
   1055 		    /*
   1056 		     * Search the hash list for overlapping entries and
   1057 		     * coalesce.
   1058 		     */
   1059 		    for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
   1060 			wp = nfsd->nd_hash.le_next;
   1061 			if (NFSW_SAMECRED(owp, nfsd))
   1062 			    nfsrvw_coalesce(owp, nfsd);
   1063 		    }
   1064 		} else {
   1065 		    LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
   1066 		}
   1067 	    }
   1068 	    splx(s);
   1069 	}
   1070 
   1071 	/*
   1072 	 * Now, do VOP_WRITE()s for any one(s) that need to be done now
   1073 	 * and generate the associated reply mbuf list(s).
   1074 	 */
   1075 loop1:
   1076 	cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec;
   1077 	s = splsoftclock();
   1078 	for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
   1079 		owp = nfsd->nd_tq.le_next;
   1080 		if (nfsd->nd_time > cur_usec)
   1081 		    break;
   1082 		if (nfsd->nd_mreq)
   1083 		    continue;
   1084 		LIST_REMOVE(nfsd, nd_tq);
   1085 		LIST_REMOVE(nfsd, nd_hash);
   1086 		splx(s);
   1087 		mrep = nfsd->nd_mrep;
   1088 		nfsd->nd_mrep = NULL;
   1089 		cred = &nfsd->nd_cr;
   1090 		v3 = (nfsd->nd_flag & ND_NFSV3);
   1091 		forat_ret = aftat_ret = 1;
   1092 		error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
   1093 		    nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH),
   1094 		    FALSE);
   1095 		if (!error) {
   1096 		    if (v3)
   1097 			forat_ret = VOP_GETATTR(vp, &forat, cred, procp);
   1098 		    if (vp->v_type != VREG) {
   1099 			if (v3)
   1100 			    error = EINVAL;
   1101 			else
   1102 			    error = (vp->v_type == VDIR) ? EISDIR : EACCES;
   1103 		    }
   1104 		} else
   1105 		    vp = NULL;
   1106 		if (!error) {
   1107 		    nqsrv_getl(vp, ND_WRITE);
   1108 		    error = nfsrv_access(vp, VWRITE, cred, rdonly, procp, 1);
   1109 		}
   1110 
   1111 		if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
   1112 		    ioflags = IO_NODELOCKED;
   1113 		else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
   1114 		    ioflags = (IO_SYNC | IO_NODELOCKED);
   1115 		else
   1116 		    ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
   1117 		uiop->uio_rw = UIO_WRITE;
   1118 		uiop->uio_segflg = UIO_SYSSPACE;
   1119 		uiop->uio_procp = (struct proc *)0;
   1120 		uiop->uio_offset = nfsd->nd_off;
   1121 		uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
   1122 		if (uiop->uio_resid > 0) {
   1123 		    mp = mrep;
   1124 		    i = 0;
   1125 		    while (mp) {
   1126 			if (mp->m_len > 0)
   1127 			    i++;
   1128 			mp = mp->m_next;
   1129 		    }
   1130 		    uiop->uio_iovcnt = i;
   1131 		    MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
   1132 			M_TEMP, M_WAITOK);
   1133 		    uiop->uio_iov = ivp = iov;
   1134 		    mp = mrep;
   1135 		    while (mp) {
   1136 			if (mp->m_len > 0) {
   1137 			    ivp->iov_base = mtod(mp, caddr_t);
   1138 			    ivp->iov_len = mp->m_len;
   1139 			    ivp++;
   1140 			}
   1141 			mp = mp->m_next;
   1142 		    }
   1143 		    if (!error) {
   1144 			error = VOP_WRITE(vp, uiop, ioflags, cred);
   1145 			nfsstats.srvvop_writes++;
   1146 		    }
   1147 		    FREE((caddr_t)iov, M_TEMP);
   1148 		}
   1149 		m_freem(mrep);
   1150 		if (vp) {
   1151 		    aftat_ret = VOP_GETATTR(vp, &va, cred, procp);
   1152 		    vput(vp);
   1153 		}
   1154 
   1155 		/*
   1156 		 * Loop around generating replies for all write rpcs that have
   1157 		 * now been completed.
   1158 		 */
   1159 		swp = nfsd;
   1160 		do {
   1161 		    if (error) {
   1162 			nfsm_writereply(NFSX_WCCDATA(v3), v3);
   1163 			if (v3) {
   1164 			    nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
   1165 			}
   1166 		    } else {
   1167 			nfsm_writereply(NFSX_PREOPATTR(v3) +
   1168 			    NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
   1169 			    NFSX_WRITEVERF(v3), v3);
   1170 			if (v3) {
   1171 			    nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
   1172 			    nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
   1173 			    *tl++ = txdr_unsigned(nfsd->nd_len);
   1174 			    *tl++ = txdr_unsigned(swp->nd_stable);
   1175 			    /*
   1176 			     * Actually, there is no need to txdr these fields,
   1177 			     * but it may make the values more human readable,
   1178 			     * for debugging purposes.
   1179 			     */
   1180 			    *tl++ = txdr_unsigned(boottime.tv_sec);
   1181 			    *tl = txdr_unsigned(boottime.tv_usec);
   1182 			} else {
   1183 			    nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
   1184 			    nfsm_srvfillattr(&va, fp);
   1185 			}
   1186 		    }
   1187 		    nfsd->nd_mreq = mreq;
   1188 		    if (nfsd->nd_mrep)
   1189 			panic("nfsrv_write: nd_mrep not free");
   1190 
   1191 		    /*
   1192 		     * Done. Put it at the head of the timer queue so that
   1193 		     * the final phase can return the reply.
   1194 		     */
   1195 		    s = splsoftclock();
   1196 		    if (nfsd != swp) {
   1197 			nfsd->nd_time = 0;
   1198 			LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
   1199 		    }
   1200 		    nfsd = swp->nd_coalesce.lh_first;
   1201 		    if (nfsd) {
   1202 			LIST_REMOVE(nfsd, nd_tq);
   1203 		    }
   1204 		    splx(s);
   1205 		} while (nfsd);
   1206 		s = splsoftclock();
   1207 		swp->nd_time = 0;
   1208 		LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
   1209 		splx(s);
   1210 		goto loop1;
   1211 	}
   1212 	splx(s);
   1213 
   1214 	/*
   1215 	 * Search for a reply to return.
   1216 	 */
   1217 	s = splsoftclock();
   1218 	for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
   1219 		if (nfsd->nd_mreq) {
   1220 		    LIST_REMOVE(nfsd, nd_tq);
   1221 		    *mrq = nfsd->nd_mreq;
   1222 		    *ndp = nfsd;
   1223 		    break;
   1224 		}
   1225 	splx(s);
   1226 	return (0);
   1227 }
   1228 
   1229 /*
   1230  * Coalesce the write request nfsd into owp. To do this we must:
   1231  * - remove nfsd from the queues
   1232  * - merge nfsd->nd_mrep into owp->nd_mrep
   1233  * - update the nd_eoff and nd_stable for owp
   1234  * - put nfsd on owp's nd_coalesce list
   1235  * NB: Must be called at splsoftclock().
   1236  */
   1237 void
   1238 nfsrvw_coalesce(owp, nfsd)
   1239         register struct nfsrv_descript *owp;
   1240         register struct nfsrv_descript *nfsd;
   1241 {
   1242         register int overlap;
   1243         register struct mbuf *mp;
   1244 
   1245         LIST_REMOVE(nfsd, nd_hash);
   1246         LIST_REMOVE(nfsd, nd_tq);
   1247         if (owp->nd_eoff < nfsd->nd_eoff) {
   1248             overlap = owp->nd_eoff - nfsd->nd_off;
   1249             if (overlap < 0)
   1250                 panic("nfsrv_coalesce: bad off");
   1251             if (overlap > 0)
   1252                 m_adj(nfsd->nd_mrep, overlap);
   1253             mp = owp->nd_mrep;
   1254             while (mp->m_next)
   1255                 mp = mp->m_next;
   1256             mp->m_next = nfsd->nd_mrep;
   1257             owp->nd_eoff = nfsd->nd_eoff;
   1258         } else
   1259             m_freem(nfsd->nd_mrep);
   1260         nfsd->nd_mrep = NULL;
   1261         if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
   1262             owp->nd_stable = NFSV3WRITE_FILESYNC;
   1263         else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
   1264             owp->nd_stable == NFSV3WRITE_UNSTABLE)
   1265             owp->nd_stable = NFSV3WRITE_DATASYNC;
   1266         LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
   1267  	/*
   1268  	 * nfsd might hold coalesce elements! Move them to owp.
   1269  	 * Otherwise, requests may be lost and clients will be stuck.
   1270  	 */
   1271  	if (nfsd->nd_coalesce.lh_first)
   1272  	{
   1273  		register struct nfsrv_descript *m;
   1274 
   1275  		while ((m = nfsd->nd_coalesce.lh_first))
   1276  		{
   1277  			LIST_REMOVE(m, nd_tq);
   1278  			LIST_INSERT_HEAD(&owp->nd_coalesce, m, nd_tq);
   1279  		}
   1280  	}
   1281 }
   1282 
   1283 /*
   1284  * nfs create service
   1285  * now does a truncate to 0 length via. setattr if it already exists
   1286  */
   1287 int
   1288 nfsrv_create(nfsd, slp, procp, mrq)
   1289 	struct nfsrv_descript *nfsd;
   1290 	struct nfssvc_sock *slp;
   1291 	struct proc *procp;
   1292 	struct mbuf **mrq;
   1293 {
   1294 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   1295 	struct mbuf *nam = nfsd->nd_nam;
   1296 	caddr_t dpos = nfsd->nd_dpos;
   1297 	struct ucred *cred = &nfsd->nd_cr;
   1298 	register struct nfs_fattr *fp;
   1299 	struct vattr va, dirfor, diraft;
   1300 	register struct nfsv2_sattr *sp;
   1301 	register u_int32_t *tl;
   1302 	struct nameidata nd;
   1303 	register caddr_t cp;
   1304 	register int32_t t1;
   1305 	caddr_t bpos;
   1306 	int error = 0, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
   1307 	int rdev = 0;
   1308 	int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
   1309 	char *cp2;
   1310 	struct mbuf *mb, *mb2, *mreq;
   1311 	struct vnode *vp = NULL, *dirp = NULL;
   1312 	nfsfh_t nfh;
   1313 	fhandle_t *fhp;
   1314 	u_quad_t frev, tempsize;
   1315 	u_char cverf[NFSX_V3CREATEVERF];
   1316 
   1317 	nd.ni_cnd.cn_nameiop = 0;
   1318 	fhp = &nfh.fh_generic;
   1319 	nfsm_srvmtofh(fhp);
   1320 	nfsm_srvnamesiz(len);
   1321 	nd.ni_cnd.cn_cred = cred;
   1322 	nd.ni_cnd.cn_nameiop = CREATE;
   1323 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
   1324 	error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
   1325 		&dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   1326 	if (dirp) {
   1327 		if (v3)
   1328 			dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
   1329 				procp);
   1330 		else {
   1331 			vrele(dirp);
   1332 			dirp = (struct vnode *)0;
   1333 		}
   1334 	}
   1335 	if (error) {
   1336 		nfsm_reply(NFSX_WCCDATA(v3));
   1337 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   1338 		if (dirp)
   1339 			vrele(dirp);
   1340 		return (0);
   1341 	}
   1342 	VATTR_NULL(&va);
   1343 	if (v3) {
   1344 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1345 		how = fxdr_unsigned(int, *tl);
   1346 		switch (how) {
   1347 		case NFSV3CREATE_GUARDED:
   1348 			if (nd.ni_vp) {
   1349 				error = EEXIST;
   1350 				break;
   1351 			}
   1352 		case NFSV3CREATE_UNCHECKED:
   1353 			nfsm_srvsattr(&va);
   1354 			break;
   1355 		case NFSV3CREATE_EXCLUSIVE:
   1356 			nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
   1357 			bcopy(cp, cverf, NFSX_V3CREATEVERF);
   1358 			exclusive_flag = 1;
   1359 			if (nd.ni_vp == NULL)
   1360 				va.va_mode = 0;
   1361 			break;
   1362 		};
   1363 		va.va_type = VREG;
   1364 	} else {
   1365 		nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1366 		va.va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
   1367 		if (va.va_type == VNON)
   1368 			va.va_type = VREG;
   1369 		va.va_mode = nfstov_mode(sp->sa_mode);
   1370 		switch (va.va_type) {
   1371 		case VREG:
   1372 			tsize = fxdr_unsigned(int32_t, sp->sa_size);
   1373 			if (tsize != -1)
   1374 				va.va_size = (u_quad_t)tsize;
   1375 			break;
   1376 		case VCHR:
   1377 		case VBLK:
   1378 		case VFIFO:
   1379 			rdev = fxdr_unsigned(int32_t, sp->sa_size);
   1380 			break;
   1381 		default:
   1382 			break;
   1383 		};
   1384 	}
   1385 
   1386 	/*
   1387 	 * Iff doesn't exist, create it
   1388 	 * otherwise just truncate to 0 length
   1389 	 *   should I set the mode too ??
   1390 	 */
   1391 	if (nd.ni_vp == NULL) {
   1392 		if (va.va_type == VREG || va.va_type == VSOCK) {
   1393 			vrele(nd.ni_startdir);
   1394 			nqsrv_getl(nd.ni_dvp, ND_WRITE);
   1395 			error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
   1396 			if (!error) {
   1397 				FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1398 				if (exclusive_flag) {
   1399 					exclusive_flag = 0;
   1400 					VATTR_NULL(&va);
   1401 					bcopy(cverf, (caddr_t)&va.va_atime,
   1402 						NFSX_V3CREATEVERF);
   1403 					error = VOP_SETATTR(nd.ni_vp, &va, cred,
   1404 						procp);
   1405 				}
   1406 			}
   1407 		} else if (va.va_type == VCHR || va.va_type == VBLK ||
   1408 			va.va_type == VFIFO) {
   1409 			if (va.va_type == VCHR && rdev == 0xffffffff)
   1410 				va.va_type = VFIFO;
   1411 			error = suser(cred, (u_short *)0);
   1412 			if (error) {
   1413 				vrele(nd.ni_startdir);
   1414 				free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1415 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1416 				vput(nd.ni_dvp);
   1417 				nfsm_reply(0);
   1418 				return (error);
   1419 			} else
   1420 				va.va_rdev = (dev_t)rdev;
   1421 			nqsrv_getl(nd.ni_dvp, ND_WRITE);
   1422 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd,
   1423 			    &va);
   1424 			if (error) {
   1425 				vrele(nd.ni_startdir);
   1426 				nfsm_reply(0);
   1427 			}
   1428 			nd.ni_cnd.cn_nameiop = LOOKUP;
   1429 			nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART);
   1430 			nd.ni_cnd.cn_proc = procp;
   1431 			nd.ni_cnd.cn_cred = cred;
   1432 			if ((error = lookup(&nd)) != 0) {
   1433 				free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1434 				nfsm_reply(0);
   1435 			}
   1436 			FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1437 			if (nd.ni_cnd.cn_flags & ISSYMLINK) {
   1438 				vrele(nd.ni_dvp);
   1439 				vput(nd.ni_vp);
   1440 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1441 				error = EINVAL;
   1442 				nfsm_reply(0);
   1443 			}
   1444 		} else {
   1445 			vrele(nd.ni_startdir);
   1446 			free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1447 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1448 			vput(nd.ni_dvp);
   1449 			error = ENXIO;
   1450 		}
   1451 		vp = nd.ni_vp;
   1452 	} else {
   1453 		vrele(nd.ni_startdir);
   1454 		free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1455 		vp = nd.ni_vp;
   1456 		if (nd.ni_dvp == vp)
   1457 			vrele(nd.ni_dvp);
   1458 		else
   1459 			vput(nd.ni_dvp);
   1460 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1461 		if (va.va_size != -1) {
   1462 			error = nfsrv_access(vp, VWRITE, cred,
   1463 			    (nd.ni_cnd.cn_flags & RDONLY), procp, 0);
   1464 			if (!error) {
   1465 				nqsrv_getl(vp, ND_WRITE);
   1466 				tempsize = va.va_size;
   1467 				VATTR_NULL(&va);
   1468 				va.va_size = tempsize;
   1469 				error = VOP_SETATTR(vp, &va, cred,
   1470 					 procp);
   1471 			}
   1472 			if (error)
   1473 				vput(vp);
   1474 		}
   1475 	}
   1476 	if (!error) {
   1477 		bzero((caddr_t)fhp, sizeof(nfh));
   1478 		fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
   1479 		error = VFS_VPTOFH(vp, &fhp->fh_fid);
   1480 		if (!error)
   1481 			error = VOP_GETATTR(vp, &va, cred, procp);
   1482 		vput(vp);
   1483 	}
   1484 	if (v3) {
   1485 		if (exclusive_flag && !error &&
   1486 			bcmp(cverf, (caddr_t)&va.va_atime, NFSX_V3CREATEVERF))
   1487 			error = EEXIST;
   1488 		diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
   1489 		vrele(dirp);
   1490 	}
   1491 	nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
   1492 	if (v3) {
   1493 		if (!error) {
   1494 			nfsm_srvpostop_fh(fhp);
   1495 			nfsm_srvpostop_attr(0, &va);
   1496 		}
   1497 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   1498 	} else {
   1499 		nfsm_srvfhtom(fhp, v3);
   1500 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
   1501 		nfsm_srvfillattr(&va, fp);
   1502 	}
   1503 	return (0);
   1504 nfsmout:
   1505 	if (dirp)
   1506 		vrele(dirp);
   1507 	if (nd.ni_cnd.cn_nameiop) {
   1508 		vrele(nd.ni_startdir);
   1509 		free((caddr_t)nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1510 	}
   1511 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1512 	if (nd.ni_dvp == nd.ni_vp)
   1513 		vrele(nd.ni_dvp);
   1514 	else
   1515 		vput(nd.ni_dvp);
   1516 	if (nd.ni_vp)
   1517 		vput(nd.ni_vp);
   1518 	return (error);
   1519 }
   1520 
   1521 /*
   1522  * nfs v3 mknod service
   1523  */
   1524 int
   1525 nfsrv_mknod(nfsd, slp, procp, mrq)
   1526 	struct nfsrv_descript *nfsd;
   1527 	struct nfssvc_sock *slp;
   1528 	struct proc *procp;
   1529 	struct mbuf **mrq;
   1530 {
   1531 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   1532 	struct mbuf *nam = nfsd->nd_nam;
   1533 	caddr_t dpos = nfsd->nd_dpos;
   1534 	struct ucred *cred = &nfsd->nd_cr;
   1535 	struct vattr va, dirfor, diraft;
   1536 	register u_int32_t *tl;
   1537 	struct nameidata nd;
   1538 	register int32_t t1;
   1539 	caddr_t bpos;
   1540 	int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
   1541 	u_int32_t major, minor;
   1542 	enum vtype vtyp;
   1543 	char *cp2;
   1544 	struct mbuf *mb, *mb2, *mreq;
   1545 	struct vnode *vp, *dirp = (struct vnode *)0;
   1546 	nfsfh_t nfh;
   1547 	fhandle_t *fhp;
   1548 	u_quad_t frev;
   1549 
   1550 	nd.ni_cnd.cn_nameiop = 0;
   1551 	fhp = &nfh.fh_generic;
   1552 	nfsm_srvmtofh(fhp);
   1553 	nfsm_srvnamesiz(len);
   1554 	nd.ni_cnd.cn_cred = cred;
   1555 	nd.ni_cnd.cn_nameiop = CREATE;
   1556 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
   1557 	error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
   1558 		&dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   1559 	if (dirp)
   1560 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
   1561 	if (error) {
   1562 		nfsm_reply(NFSX_WCCDATA(1));
   1563 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   1564 		if (dirp)
   1565 			vrele(dirp);
   1566 		return (0);
   1567 	}
   1568 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1569 	vtyp = nfsv3tov_type(*tl);
   1570 	if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
   1571 		vrele(nd.ni_startdir);
   1572 		free((caddr_t)nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1573 		error = NFSERR_BADTYPE;
   1574 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1575 		vput(nd.ni_dvp);
   1576 		goto out;
   1577 	}
   1578 	VATTR_NULL(&va);
   1579 	nfsm_srvsattr(&va);
   1580 	if (vtyp == VCHR || vtyp == VBLK) {
   1581 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1582 		major = fxdr_unsigned(u_int32_t, *tl++);
   1583 		minor = fxdr_unsigned(u_int32_t, *tl);
   1584 		va.va_rdev = makedev(major, minor);
   1585 	}
   1586 
   1587 	/*
   1588 	 * Iff doesn't exist, create it.
   1589 	 */
   1590 	if (nd.ni_vp) {
   1591 		vrele(nd.ni_startdir);
   1592 		free((caddr_t)nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1593 		error = EEXIST;
   1594 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1595 		vput(nd.ni_dvp);
   1596 		goto out;
   1597 	}
   1598 	va.va_type = vtyp;
   1599 	if (vtyp == VSOCK) {
   1600 		vrele(nd.ni_startdir);
   1601 		nqsrv_getl(nd.ni_dvp, ND_WRITE);
   1602 		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
   1603 		if (!error)
   1604 			FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1605 	} else {
   1606 		error = suser(cred, (u_short *)0);
   1607 		if (error) {
   1608 			vrele(nd.ni_startdir);
   1609 			free((caddr_t)nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1610 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1611 			vput(nd.ni_dvp);
   1612 			goto out;
   1613 		}
   1614 		nqsrv_getl(nd.ni_dvp, ND_WRITE);
   1615 		error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
   1616 		if (error) {
   1617 			vrele(nd.ni_startdir);
   1618 			goto out;
   1619 		}
   1620 		nd.ni_cnd.cn_nameiop = LOOKUP;
   1621 		nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART);
   1622 		nd.ni_cnd.cn_proc = procp;
   1623 		nd.ni_cnd.cn_cred = procp->p_ucred;
   1624 		error = lookup(&nd);
   1625 		FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1626 		if (error)
   1627 			goto out;
   1628 		if (nd.ni_cnd.cn_flags & ISSYMLINK) {
   1629 			vrele(nd.ni_dvp);
   1630 			vput(nd.ni_vp);
   1631 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1632 			error = EINVAL;
   1633 		}
   1634 	}
   1635 out:
   1636 	vp = nd.ni_vp;
   1637 	if (!error) {
   1638 		bzero((caddr_t)fhp, sizeof(nfh));
   1639 		fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
   1640 		error = VFS_VPTOFH(vp, &fhp->fh_fid);
   1641 		if (!error)
   1642 			error = VOP_GETATTR(vp, &va, cred, procp);
   1643 		vput(vp);
   1644 	}
   1645 	diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
   1646 	vrele(dirp);
   1647 	nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
   1648 	if (!error) {
   1649 		nfsm_srvpostop_fh(fhp);
   1650 		nfsm_srvpostop_attr(0, &va);
   1651 	}
   1652 	nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   1653 	return (0);
   1654 nfsmout:
   1655 	if (dirp)
   1656 		vrele(dirp);
   1657 	if (nd.ni_cnd.cn_nameiop) {
   1658 		vrele(nd.ni_startdir);
   1659 		free((caddr_t)nd.ni_cnd.cn_pnbuf, M_NAMEI);
   1660 	}
   1661 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1662 	if (nd.ni_dvp == nd.ni_vp)
   1663 		vrele(nd.ni_dvp);
   1664 	else
   1665 		vput(nd.ni_dvp);
   1666 	if (nd.ni_vp)
   1667 		vput(nd.ni_vp);
   1668 	return (error);
   1669 }
   1670 
   1671 /*
   1672  * nfs remove service
   1673  */
   1674 int
   1675 nfsrv_remove(nfsd, slp, procp, mrq)
   1676 	struct nfsrv_descript *nfsd;
   1677 	struct nfssvc_sock *slp;
   1678 	struct proc *procp;
   1679 	struct mbuf **mrq;
   1680 {
   1681 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   1682 	struct mbuf *nam = nfsd->nd_nam;
   1683 	caddr_t dpos = nfsd->nd_dpos;
   1684 	struct ucred *cred = &nfsd->nd_cr;
   1685 	struct nameidata nd;
   1686 	register u_int32_t *tl;
   1687 	register int32_t t1;
   1688 	caddr_t bpos;
   1689 	int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
   1690 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   1691 	char *cp2;
   1692 	struct mbuf *mb, *mreq;
   1693 	struct vnode *vp, *dirp;
   1694 	struct vattr dirfor, diraft;
   1695 	nfsfh_t nfh;
   1696 	fhandle_t *fhp;
   1697 	u_quad_t frev;
   1698 
   1699 #ifndef nolint
   1700 	vp = (struct vnode *)0;
   1701 #endif
   1702 	fhp = &nfh.fh_generic;
   1703 	nfsm_srvmtofh(fhp);
   1704 	nfsm_srvnamesiz(len);
   1705 	nd.ni_cnd.cn_cred = cred;
   1706 	nd.ni_cnd.cn_nameiop = DELETE;
   1707 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
   1708 	error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
   1709 		&dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   1710 	if (dirp) {
   1711 		if (v3)
   1712 			dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
   1713 				procp);
   1714 		else
   1715 			vrele(dirp);
   1716 	}
   1717 	if (!error) {
   1718 		vp = nd.ni_vp;
   1719 		if (vp->v_type == VDIR &&
   1720 		    (error = suser(cred, (u_short *)0)) != 0)
   1721 			goto out;
   1722 		/*
   1723 		 * The root of a mounted filesystem cannot be deleted.
   1724 		 */
   1725 		if (vp->v_flag & VROOT) {
   1726 			error = EBUSY;
   1727 			goto out;
   1728 		}
   1729 out:
   1730 		if (!error) {
   1731 #if defined(UVM)
   1732 			(void)uvm_vnp_uncache(vp);
   1733 #else
   1734 			(void)vnode_pager_uncache(vp);
   1735 #endif
   1736 			nqsrv_getl(nd.ni_dvp, ND_WRITE);
   1737 			nqsrv_getl(vp, ND_WRITE);
   1738 			error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1739 		} else {
   1740 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1741 			if (nd.ni_dvp == vp)
   1742 				vrele(nd.ni_dvp);
   1743 			else
   1744 				vput(nd.ni_dvp);
   1745 			vput(vp);
   1746 		}
   1747 	}
   1748 	if (dirp && v3) {
   1749 		diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
   1750 		vrele(dirp);
   1751 	}
   1752 	nfsm_reply(NFSX_WCCDATA(v3));
   1753 	if (v3) {
   1754 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   1755 		return (0);
   1756 	}
   1757 	nfsm_srvdone;
   1758 }
   1759 
   1760 /*
   1761  * nfs rename service
   1762  */
   1763 int
   1764 nfsrv_rename(nfsd, slp, procp, mrq)
   1765 	struct nfsrv_descript *nfsd;
   1766 	struct nfssvc_sock *slp;
   1767 	struct proc *procp;
   1768 	struct mbuf **mrq;
   1769 {
   1770 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   1771 	struct mbuf *nam = nfsd->nd_nam;
   1772 	caddr_t dpos = nfsd->nd_dpos;
   1773 	struct ucred *cred = &nfsd->nd_cr;
   1774 	register u_int32_t *tl;
   1775 	register int32_t t1;
   1776 	caddr_t bpos;
   1777 	int error = 0, cache, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
   1778 	int tdirfor_ret = 1, tdiraft_ret = 1;
   1779 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   1780 	char *cp2;
   1781 	struct mbuf *mb, *mreq;
   1782 	struct nameidata fromnd, tond;
   1783 	struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
   1784 	struct vnode *tdirp = (struct vnode *)0;
   1785 	struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
   1786 	nfsfh_t fnfh, tnfh;
   1787 	fhandle_t *ffhp, *tfhp;
   1788 	u_quad_t frev;
   1789 	uid_t saved_uid;
   1790 
   1791 #ifndef nolint
   1792 	fvp = (struct vnode *)0;
   1793 #endif
   1794 	ffhp = &fnfh.fh_generic;
   1795 	tfhp = &tnfh.fh_generic;
   1796 	fromnd.ni_cnd.cn_nameiop = 0;
   1797 	tond.ni_cnd.cn_nameiop = 0;
   1798 	nfsm_srvmtofh(ffhp);
   1799 	nfsm_srvnamesiz(len);
   1800 	/*
   1801 	 * Remember our original uid so that we can reset cr_uid before
   1802 	 * the second nfs_namei() call, in case it is remapped.
   1803 	 */
   1804 	saved_uid = cred->cr_uid;
   1805 	fromnd.ni_cnd.cn_cred = cred;
   1806 	fromnd.ni_cnd.cn_nameiop = DELETE;
   1807 	fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
   1808 	error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
   1809 		&dpos, &fdirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   1810 	if (fdirp) {
   1811 		if (v3)
   1812 			fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred,
   1813 				procp);
   1814 		else {
   1815 			vrele(fdirp);
   1816 			fdirp = (struct vnode *)0;
   1817 		}
   1818 	}
   1819 	if (error) {
   1820 		nfsm_reply(2 * NFSX_WCCDATA(v3));
   1821 		nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
   1822 		nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
   1823 		if (fdirp)
   1824 			vrele(fdirp);
   1825 		return (0);
   1826 	}
   1827 	fvp = fromnd.ni_vp;
   1828 	nfsm_srvmtofh(tfhp);
   1829 	nfsm_strsiz(len2, NFS_MAXNAMLEN);
   1830 	cred->cr_uid = saved_uid;
   1831 	tond.ni_cnd.cn_cred = cred;
   1832 	tond.ni_cnd.cn_nameiop = RENAME;
   1833 	tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
   1834 	error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
   1835 		&dpos, &tdirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   1836 	if (tdirp) {
   1837 		if (v3)
   1838 			tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred,
   1839 				procp);
   1840 		else {
   1841 			vrele(tdirp);
   1842 			tdirp = (struct vnode *)0;
   1843 		}
   1844 	}
   1845 	if (error) {
   1846 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1847 		vrele(fromnd.ni_dvp);
   1848 		vrele(fvp);
   1849 		goto out1;
   1850 	}
   1851 	tdvp = tond.ni_dvp;
   1852 	tvp = tond.ni_vp;
   1853 	if (tvp != NULL) {
   1854 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   1855 			if (v3)
   1856 				error = EEXIST;
   1857 			else
   1858 				error = EISDIR;
   1859 			goto out;
   1860 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   1861 			if (v3)
   1862 				error = EEXIST;
   1863 			else
   1864 				error = ENOTDIR;
   1865 			goto out;
   1866 		}
   1867 		if (tvp->v_type == VDIR && tvp->v_mountedhere) {
   1868 			if (v3)
   1869 				error = EXDEV;
   1870 			else
   1871 				error = ENOTEMPTY;
   1872 			goto out;
   1873 		}
   1874 	}
   1875 	if (fvp->v_type == VDIR && fvp->v_mountedhere) {
   1876 		if (v3)
   1877 			error = EXDEV;
   1878 		else
   1879 			error = ENOTEMPTY;
   1880 		goto out;
   1881 	}
   1882 	if (fvp->v_mount != tdvp->v_mount) {
   1883 		if (v3)
   1884 			error = EXDEV;
   1885 		else
   1886 			error = ENOTEMPTY;
   1887 		goto out;
   1888 	}
   1889 	if (fvp == tdvp)
   1890 		if (v3)
   1891 			error = EINVAL;
   1892 		else
   1893 			error = ENOTEMPTY;
   1894 	/*
   1895 	 * If source is the same as the destination (that is the
   1896 	 * same vnode with the same name in the same directory),
   1897 	 * then there is nothing to do.
   1898 	 */
   1899 	if (fvp == tvp && fromnd.ni_dvp == tdvp &&
   1900 	    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   1901 	    !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
   1902 	      fromnd.ni_cnd.cn_namelen))
   1903 		error = -1;
   1904 out:
   1905 	if (!error) {
   1906 		nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
   1907 		nqsrv_getl(tdvp, ND_WRITE);
   1908 		if (tvp) {
   1909 #if defined(UVM)
   1910 			(void)uvm_vnp_uncache(tvp);
   1911 #else
   1912 			(void)vnode_pager_uncache(tvp);
   1913 #endif
   1914 			nqsrv_getl(tvp, ND_WRITE);
   1915 		}
   1916 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   1917 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   1918 	} else {
   1919 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   1920 		if (tdvp == tvp)
   1921 			vrele(tdvp);
   1922 		else
   1923 			vput(tdvp);
   1924 		if (tvp)
   1925 			vput(tvp);
   1926 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1927 		vrele(fromnd.ni_dvp);
   1928 		vrele(fvp);
   1929 		if (error == -1)
   1930 			error = 0;
   1931 	}
   1932 	vrele(tond.ni_startdir);
   1933 	FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   1934 out1:
   1935 	if (fdirp) {
   1936 		fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, procp);
   1937 		vrele(fdirp);
   1938 	}
   1939 	if (tdirp) {
   1940 		tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, procp);
   1941 		vrele(tdirp);
   1942 	}
   1943 	vrele(fromnd.ni_startdir);
   1944 	FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   1945 	nfsm_reply(2 * NFSX_WCCDATA(v3));
   1946 	if (v3) {
   1947 		nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
   1948 		nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
   1949 	}
   1950 	return (0);
   1951 
   1952 nfsmout:
   1953 	if (fdirp)
   1954 		vrele(fdirp);
   1955 	if (tdirp)
   1956 		vrele(tdirp);
   1957 	if (tond.ni_cnd.cn_nameiop) {
   1958 		vrele(tond.ni_startdir);
   1959 		FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   1960 	}
   1961 	if (fromnd.ni_cnd.cn_nameiop) {
   1962 		vrele(fromnd.ni_startdir);
   1963 		FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   1964 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1965 		vrele(fromnd.ni_dvp);
   1966 		vrele(fvp);
   1967 	}
   1968 	return (error);
   1969 }
   1970 
   1971 /*
   1972  * nfs link service
   1973  */
   1974 int
   1975 nfsrv_link(nfsd, slp, procp, mrq)
   1976 	struct nfsrv_descript *nfsd;
   1977 	struct nfssvc_sock *slp;
   1978 	struct proc *procp;
   1979 	struct mbuf **mrq;
   1980 {
   1981 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   1982 	struct mbuf *nam = nfsd->nd_nam;
   1983 	caddr_t dpos = nfsd->nd_dpos;
   1984 	struct ucred *cred = &nfsd->nd_cr;
   1985 	struct nameidata nd;
   1986 	register u_int32_t *tl;
   1987 	register int32_t t1;
   1988 	caddr_t bpos;
   1989 	int error = 0, rdonly, cache, len, dirfor_ret = 1, diraft_ret = 1;
   1990 	int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
   1991 	char *cp2;
   1992 	struct mbuf *mb, *mreq;
   1993 	struct vnode *vp, *xp, *dirp = (struct vnode *)0;
   1994 	struct vattr dirfor, diraft, at;
   1995 	nfsfh_t nfh, dnfh;
   1996 	fhandle_t *fhp, *dfhp;
   1997 	u_quad_t frev;
   1998 
   1999 	fhp = &nfh.fh_generic;
   2000 	dfhp = &dnfh.fh_generic;
   2001 	nfsm_srvmtofh(fhp);
   2002 	nfsm_srvmtofh(dfhp);
   2003 	nfsm_srvnamesiz(len);
   2004 	error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam,
   2005 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   2006 	if (error) {
   2007 		nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
   2008 		nfsm_srvpostop_attr(getret, &at);
   2009 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2010 		return (0);
   2011 	}
   2012 	if (vp->v_type == VDIR && (error = suser(cred, (u_short *)0)) != 0)
   2013 		goto out1;
   2014 	nd.ni_cnd.cn_cred = cred;
   2015 	nd.ni_cnd.cn_nameiop = CREATE;
   2016 	nd.ni_cnd.cn_flags = LOCKPARENT;
   2017 	error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
   2018 		&dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   2019 	if (dirp) {
   2020 		if (v3)
   2021 			dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
   2022 				procp);
   2023 		else {
   2024 			vrele(dirp);
   2025 			dirp = (struct vnode *)0;
   2026 		}
   2027 	}
   2028 	if (error)
   2029 		goto out1;
   2030 	xp = nd.ni_vp;
   2031 	if (xp != NULL) {
   2032 		error = EEXIST;
   2033 		goto out;
   2034 	}
   2035 	xp = nd.ni_dvp;
   2036 	if (vp->v_mount != xp->v_mount)
   2037 		error = EXDEV;
   2038 out:
   2039 	if (!error) {
   2040 		nqsrv_getl(vp, ND_WRITE);
   2041 		nqsrv_getl(xp, ND_WRITE);
   2042 		error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   2043 	} else {
   2044 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2045 		if (nd.ni_dvp == nd.ni_vp)
   2046 			vrele(nd.ni_dvp);
   2047 		else
   2048 			vput(nd.ni_dvp);
   2049 		if (nd.ni_vp)
   2050 			vrele(nd.ni_vp);
   2051 	}
   2052 out1:
   2053 	if (v3)
   2054 		getret = VOP_GETATTR(vp, &at, cred, procp);
   2055 	if (dirp) {
   2056 		diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
   2057 		vrele(dirp);
   2058 	}
   2059 	vrele(vp);
   2060 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
   2061 	if (v3) {
   2062 		nfsm_srvpostop_attr(getret, &at);
   2063 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2064 		return (0);
   2065 	}
   2066 	nfsm_srvdone;
   2067 }
   2068 
   2069 /*
   2070  * nfs symbolic link service
   2071  */
   2072 int
   2073 nfsrv_symlink(nfsd, slp, procp, mrq)
   2074 	struct nfsrv_descript *nfsd;
   2075 	struct nfssvc_sock *slp;
   2076 	struct proc *procp;
   2077 	struct mbuf **mrq;
   2078 {
   2079 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   2080 	struct mbuf *nam = nfsd->nd_nam;
   2081 	caddr_t dpos = nfsd->nd_dpos;
   2082 	struct ucred *cred = &nfsd->nd_cr;
   2083 	struct vattr va, dirfor, diraft;
   2084 	struct nameidata nd;
   2085 	register u_int32_t *tl;
   2086 	register int32_t t1;
   2087 	struct nfsv2_sattr *sp;
   2088 	char *bpos, *pathcp = NULL, *cp2;
   2089 	struct uio io;
   2090 	struct iovec iv;
   2091 	int error = 0, cache, len, len2, dirfor_ret = 1, diraft_ret = 1;
   2092 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   2093 	struct mbuf *mb, *mreq, *mb2;
   2094 	struct vnode *dirp = (struct vnode *)0;
   2095 	nfsfh_t nfh;
   2096 	fhandle_t *fhp;
   2097 	u_quad_t frev;
   2098 
   2099 	nd.ni_cnd.cn_nameiop = 0;
   2100 	fhp = &nfh.fh_generic;
   2101 	nfsm_srvmtofh(fhp);
   2102 	nfsm_srvnamesiz(len);
   2103 	nd.ni_cnd.cn_cred = cred;
   2104 	nd.ni_cnd.cn_nameiop = CREATE;
   2105 	nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
   2106 	error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
   2107 		&dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   2108 	if (dirp) {
   2109 		if (v3)
   2110 			dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
   2111 				procp);
   2112 		else {
   2113 			vrele(dirp);
   2114 			dirp = (struct vnode *)0;
   2115 		}
   2116 	}
   2117 	if (error)
   2118 		goto out;
   2119 	VATTR_NULL(&va);
   2120 	if (v3)
   2121 		nfsm_srvsattr(&va);
   2122 	nfsm_strsiz(len2, NFS_MAXPATHLEN);
   2123 	MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
   2124 	iv.iov_base = pathcp;
   2125 	iv.iov_len = len2;
   2126 	io.uio_resid = len2;
   2127 	io.uio_offset = 0;
   2128 	io.uio_iov = &iv;
   2129 	io.uio_iovcnt = 1;
   2130 	io.uio_segflg = UIO_SYSSPACE;
   2131 	io.uio_rw = UIO_READ;
   2132 	io.uio_procp = (struct proc *)0;
   2133 	nfsm_mtouio(&io, len2);
   2134 	if (!v3) {
   2135 		nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   2136 		va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
   2137 	}
   2138 	*(pathcp + len2) = '\0';
   2139 	if (nd.ni_vp) {
   2140 		vrele(nd.ni_startdir);
   2141 		free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
   2142 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2143 		if (nd.ni_dvp == nd.ni_vp)
   2144 			vrele(nd.ni_dvp);
   2145 		else
   2146 			vput(nd.ni_dvp);
   2147 		vrele(nd.ni_vp);
   2148 		error = EEXIST;
   2149 		goto out;
   2150 	}
   2151 	nqsrv_getl(nd.ni_dvp, ND_WRITE);
   2152 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp);
   2153 	if (error)
   2154 		vrele(nd.ni_startdir);
   2155 	else {
   2156 	    if (v3) {
   2157 		nd.ni_cnd.cn_nameiop = LOOKUP;
   2158 		nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART | FOLLOW);
   2159 		nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
   2160 		nd.ni_cnd.cn_proc = procp;
   2161 		nd.ni_cnd.cn_cred = cred;
   2162 		error = lookup(&nd);
   2163 		if (!error) {
   2164 			bzero((caddr_t)fhp, sizeof(nfh));
   2165 			fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
   2166 			error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
   2167 			if (!error)
   2168 				error = VOP_GETATTR(nd.ni_vp, &va, cred,
   2169 					procp);
   2170 			vput(nd.ni_vp);
   2171 		}
   2172 	    } else
   2173 		vrele(nd.ni_startdir);
   2174 	    FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
   2175 	}
   2176 out:
   2177 	if (pathcp)
   2178 		FREE(pathcp, M_TEMP);
   2179 	if (dirp) {
   2180 		diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
   2181 		vrele(dirp);
   2182 	}
   2183 	nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
   2184 	if (v3) {
   2185 		if (!error) {
   2186 			nfsm_srvpostop_fh(fhp);
   2187 			nfsm_srvpostop_attr(0, &va);
   2188 		}
   2189 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2190 	}
   2191 	return (0);
   2192 nfsmout:
   2193 	if (nd.ni_cnd.cn_nameiop) {
   2194 		vrele(nd.ni_startdir);
   2195 		free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
   2196 	}
   2197 	if (dirp)
   2198 		vrele(dirp);
   2199 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2200 	if (nd.ni_dvp == nd.ni_vp)
   2201 		vrele(nd.ni_dvp);
   2202 	else
   2203 		vput(nd.ni_dvp);
   2204 	if (nd.ni_vp)
   2205 		vrele(nd.ni_vp);
   2206 	if (pathcp)
   2207 		FREE(pathcp, M_TEMP);
   2208 	return (error);
   2209 }
   2210 
   2211 /*
   2212  * nfs mkdir service
   2213  */
   2214 int
   2215 nfsrv_mkdir(nfsd, slp, procp, mrq)
   2216 	struct nfsrv_descript *nfsd;
   2217 	struct nfssvc_sock *slp;
   2218 	struct proc *procp;
   2219 	struct mbuf **mrq;
   2220 {
   2221 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   2222 	struct mbuf *nam = nfsd->nd_nam;
   2223 	caddr_t dpos = nfsd->nd_dpos;
   2224 	struct ucred *cred = &nfsd->nd_cr;
   2225 	struct vattr va, dirfor, diraft;
   2226 	register struct nfs_fattr *fp;
   2227 	struct nameidata nd;
   2228 	register caddr_t cp;
   2229 	register u_int32_t *tl;
   2230 	register int32_t t1;
   2231 	caddr_t bpos;
   2232 	int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
   2233 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   2234 	char *cp2;
   2235 	struct mbuf *mb, *mb2, *mreq;
   2236 	struct vnode *vp, *dirp = (struct vnode *)0;
   2237 	nfsfh_t nfh;
   2238 	fhandle_t *fhp;
   2239 	u_quad_t frev;
   2240 
   2241 	fhp = &nfh.fh_generic;
   2242 	nfsm_srvmtofh(fhp);
   2243 	nfsm_srvnamesiz(len);
   2244 	nd.ni_cnd.cn_cred = cred;
   2245 	nd.ni_cnd.cn_nameiop = CREATE;
   2246 	nd.ni_cnd.cn_flags = LOCKPARENT;
   2247 	error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
   2248 		&dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   2249 	if (dirp) {
   2250 		if (v3)
   2251 			dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
   2252 				procp);
   2253 		else {
   2254 			vrele(dirp);
   2255 			dirp = (struct vnode *)0;
   2256 		}
   2257 	}
   2258 	if (error) {
   2259 		nfsm_reply(NFSX_WCCDATA(v3));
   2260 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2261 		if (dirp)
   2262 			vrele(dirp);
   2263 		return (0);
   2264 	}
   2265 	VATTR_NULL(&va);
   2266 	if (v3) {
   2267 		nfsm_srvsattr(&va);
   2268 	} else {
   2269 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2270 		va.va_mode = nfstov_mode(*tl++);
   2271 	}
   2272 	va.va_type = VDIR;
   2273 	vp = nd.ni_vp;
   2274 	if (vp != NULL) {
   2275 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2276 		if (nd.ni_dvp == vp)
   2277 			vrele(nd.ni_dvp);
   2278 		else
   2279 			vput(nd.ni_dvp);
   2280 		vrele(vp);
   2281 		error = EEXIST;
   2282 		goto out;
   2283 	}
   2284 	nqsrv_getl(nd.ni_dvp, ND_WRITE);
   2285 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
   2286 	if (!error) {
   2287 		vp = nd.ni_vp;
   2288 		bzero((caddr_t)fhp, sizeof(nfh));
   2289 		fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
   2290 		error = VFS_VPTOFH(vp, &fhp->fh_fid);
   2291 		if (!error)
   2292 			error = VOP_GETATTR(vp, &va, cred, procp);
   2293 		vput(vp);
   2294 	}
   2295 out:
   2296 	if (dirp) {
   2297 		diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
   2298 		vrele(dirp);
   2299 	}
   2300 	nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
   2301 	if (v3) {
   2302 		if (!error) {
   2303 			nfsm_srvpostop_fh(fhp);
   2304 			nfsm_srvpostop_attr(0, &va);
   2305 		}
   2306 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2307 	} else {
   2308 		nfsm_srvfhtom(fhp, v3);
   2309 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
   2310 		nfsm_srvfillattr(&va, fp);
   2311 	}
   2312 	return (0);
   2313 nfsmout:
   2314 	if (dirp)
   2315 		vrele(dirp);
   2316 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2317 	if (nd.ni_dvp == nd.ni_vp)
   2318 		vrele(nd.ni_dvp);
   2319 	else
   2320 		vput(nd.ni_dvp);
   2321 	if (nd.ni_vp)
   2322 		vrele(nd.ni_vp);
   2323 	return (error);
   2324 }
   2325 
   2326 /*
   2327  * nfs rmdir service
   2328  */
   2329 int
   2330 nfsrv_rmdir(nfsd, slp, procp, mrq)
   2331 	struct nfsrv_descript *nfsd;
   2332 	struct nfssvc_sock *slp;
   2333 	struct proc *procp;
   2334 	struct mbuf **mrq;
   2335 {
   2336 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   2337 	struct mbuf *nam = nfsd->nd_nam;
   2338 	caddr_t dpos = nfsd->nd_dpos;
   2339 	struct ucred *cred = &nfsd->nd_cr;
   2340 	register u_int32_t *tl;
   2341 	register int32_t t1;
   2342 	caddr_t bpos;
   2343 	int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
   2344 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   2345 	char *cp2;
   2346 	struct mbuf *mb, *mreq;
   2347 	struct vnode *vp, *dirp = (struct vnode *)0;
   2348 	struct vattr dirfor, diraft;
   2349 	nfsfh_t nfh;
   2350 	fhandle_t *fhp;
   2351 	struct nameidata nd;
   2352 	u_quad_t frev;
   2353 
   2354 	fhp = &nfh.fh_generic;
   2355 	nfsm_srvmtofh(fhp);
   2356 	nfsm_srvnamesiz(len);
   2357 	nd.ni_cnd.cn_cred = cred;
   2358 	nd.ni_cnd.cn_nameiop = DELETE;
   2359 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
   2360 	error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
   2361 		&dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   2362 	if (dirp) {
   2363 		if (v3)
   2364 			dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
   2365 				procp);
   2366 		else {
   2367 			vrele(dirp);
   2368 			dirp = (struct vnode *)0;
   2369 		}
   2370 	}
   2371 	if (error) {
   2372 		nfsm_reply(NFSX_WCCDATA(v3));
   2373 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2374 		if (dirp)
   2375 			vrele(dirp);
   2376 		return (0);
   2377 	}
   2378 	vp = nd.ni_vp;
   2379 	if (vp->v_type != VDIR) {
   2380 		error = ENOTDIR;
   2381 		goto out;
   2382 	}
   2383 	/*
   2384 	 * No rmdir "." please.
   2385 	 */
   2386 	if (nd.ni_dvp == vp) {
   2387 		error = EINVAL;
   2388 		goto out;
   2389 	}
   2390 	/*
   2391 	 * The root of a mounted filesystem cannot be deleted.
   2392 	 */
   2393 	if (vp->v_flag & VROOT)
   2394 		error = EBUSY;
   2395 out:
   2396 	if (!error) {
   2397 		nqsrv_getl(nd.ni_dvp, ND_WRITE);
   2398 		nqsrv_getl(vp, ND_WRITE);
   2399 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   2400 	} else {
   2401 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2402 		if (nd.ni_dvp == nd.ni_vp)
   2403 			vrele(nd.ni_dvp);
   2404 		else
   2405 			vput(nd.ni_dvp);
   2406 		vput(vp);
   2407 	}
   2408 	if (dirp) {
   2409 		diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
   2410 		vrele(dirp);
   2411 	}
   2412 	nfsm_reply(NFSX_WCCDATA(v3));
   2413 	if (v3) {
   2414 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2415 		return (0);
   2416 	}
   2417 	nfsm_srvdone;
   2418 }
   2419 
   2420 /*
   2421  * nfs readdir service
   2422  * - mallocs what it thinks is enough to read
   2423  *	count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
   2424  * - calls VOP_READDIR()
   2425  * - loops around building the reply
   2426  *	if the output generated exceeds count break out of loop
   2427  *	The nfsm_clget macro is used here so that the reply will be packed
   2428  *	tightly in mbuf clusters.
   2429  * - it only knows that it has encountered eof when the VOP_READDIR()
   2430  *	reads nothing
   2431  * - as such one readdir rpc will return eof false although you are there
   2432  *	and then the next will return eof
   2433  * - it trims out records with d_fileno == 0
   2434  *	this doesn't matter for Unix clients, but they might confuse clients
   2435  *	for other os'.
   2436  * - it trims out records with d_type == DT_WHT
   2437  *	these cannot be seen through NFS (unless we extend the protocol)
   2438  * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
   2439  *	than requested, but this may not apply to all filesystems. For
   2440  *	example, client NFS does not { although it is never remote mounted
   2441  *	anyhow }
   2442  *     The alternate call nfsrv_readdirplus() does lookups as well.
   2443  * PS: The NFS protocol spec. does not clarify what the "count" byte
   2444  *	argument is a count of.. just name strings and file id's or the
   2445  *	entire reply rpc or ...
   2446  *	I tried just file name and id sizes and it confused the Sun client,
   2447  *	so I am using the full rpc size now. The "paranoia.." comment refers
   2448  *	to including the status longwords that are not a part of the dir.
   2449  *	"entry" structures, but are in the rpc.
   2450  */
   2451 struct flrep {
   2452 	nfsuint64 fl_off;
   2453 	u_int32_t fl_postopok;
   2454 	u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
   2455 	u_int32_t fl_fhok;
   2456 	u_int32_t fl_fhsize;
   2457 	u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
   2458 };
   2459 
   2460 int
   2461 nfsrv_readdir(nfsd, slp, procp, mrq)
   2462 	struct nfsrv_descript *nfsd;
   2463 	struct nfssvc_sock *slp;
   2464 	struct proc *procp;
   2465 	struct mbuf **mrq;
   2466 {
   2467 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   2468 	struct mbuf *nam = nfsd->nd_nam;
   2469 	caddr_t dpos = nfsd->nd_dpos;
   2470 	struct ucred *cred = &nfsd->nd_cr;
   2471 	register char *bp, *be;
   2472 	register struct mbuf *mp;
   2473 	register struct dirent *dp;
   2474 	register caddr_t cp;
   2475 	register u_int32_t *tl;
   2476 	register int32_t t1;
   2477 	caddr_t bpos;
   2478 	struct mbuf *mb, *mb2, *mreq, *mp2;
   2479 	char *cpos, *cend, *cp2, *rbuf;
   2480 	struct vnode *vp;
   2481 	struct vattr at;
   2482 	nfsfh_t nfh;
   2483 	fhandle_t *fhp;
   2484 	struct uio io;
   2485 	struct iovec iv;
   2486 	int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
   2487 	int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
   2488 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   2489 	u_quad_t frev, off, toff, verf;
   2490 	off_t *cookies = NULL, *cookiep;
   2491 	nfsuint64 jar;
   2492 
   2493 	fhp = &nfh.fh_generic;
   2494 	nfsm_srvmtofh(fhp);
   2495 	if (v3) {
   2496 		nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
   2497 		fxdr_hyper(tl, &toff);
   2498 		tl += 2;
   2499 		fxdr_hyper(tl, &verf);
   2500 		tl += 2;
   2501 	} else {
   2502 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2503 		toff = fxdr_unsigned(u_quad_t, *tl++);
   2504 	}
   2505 	off = toff;
   2506 	cnt = fxdr_unsigned(int, *tl);
   2507 	siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
   2508 	xfer = NFS_SRVMAXDATA(nfsd);
   2509 	if (siz > xfer)
   2510 		siz = xfer;
   2511 	fullsiz = siz;
   2512 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
   2513 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   2514 	if (!error && vp->v_type != VDIR) {
   2515 		error = ENOTDIR;
   2516 		vput(vp);
   2517 	}
   2518 	if (error) {
   2519 		nfsm_reply(NFSX_UNSIGNED);
   2520 		nfsm_srvpostop_attr(getret, &at);
   2521 		return (0);
   2522 	}
   2523 	nqsrv_getl(vp, ND_READ);
   2524 	if (v3) {
   2525 		error = getret = VOP_GETATTR(vp, &at, cred, procp);
   2526 #ifdef NFS3_STRICTVERF
   2527 		/*
   2528 		 * XXX This check is too strict for Solaris 2.5 clients.
   2529 		 */
   2530 		if (!error && toff && verf != at.va_filerev)
   2531 			error = NFSERR_BAD_COOKIE;
   2532 #endif
   2533 	}
   2534 	if (!error)
   2535 		error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
   2536 	if (error) {
   2537 		vput(vp);
   2538 		nfsm_reply(NFSX_POSTOPATTR(v3));
   2539 		nfsm_srvpostop_attr(getret, &at);
   2540 		return (0);
   2541 	}
   2542 #ifdef Lite2_integrated
   2543 	VOP_UNLOCK(vp, 0, procp);
   2544 #else
   2545 	VOP_UNLOCK(vp);
   2546 #endif
   2547 	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
   2548 	ncookies = siz / (5 * NFSX_UNSIGNED); /*7 for V3, but it's an est. so*/
   2549 	MALLOC(cookies, off_t *, ncookies * sizeof (off_t), M_TEMP,
   2550 		M_WAITOK);
   2551 again:
   2552 	iv.iov_base = rbuf;
   2553 	iv.iov_len = fullsiz;
   2554 	io.uio_iov = &iv;
   2555 	io.uio_iovcnt = 1;
   2556 	io.uio_offset = (off_t)off;
   2557 	io.uio_resid = fullsiz;
   2558 	io.uio_segflg = UIO_SYSSPACE;
   2559 	io.uio_rw = UIO_READ;
   2560 	io.uio_procp = (struct proc *)0;
   2561 	eofflag = 0;
   2562 #ifdef Lite2_integrated
   2563 	VOP_LOCK(vp, 0, procp);
   2564 #else
   2565 	VOP_LOCK(vp);
   2566 #endif
   2567 
   2568 	error = VOP_READDIR(vp, &io, cred, &eofflag, cookies, ncookies);
   2569 
   2570 	off = (off_t)io.uio_offset;
   2571 	if (!cookies && !error)
   2572 		error = NFSERR_PERM;
   2573 	if (v3) {
   2574 		getret = VOP_GETATTR(vp, &at, cred, procp);
   2575 		if (!error)
   2576 			error = getret;
   2577 	}
   2578 
   2579 #ifdef Lite2_integrated
   2580 	VOP_UNLOCK(vp, 0, procp);
   2581 #else
   2582 	VOP_UNLOCK(vp);
   2583 #endif
   2584 	if (error) {
   2585 		vrele(vp);
   2586 		free((caddr_t)rbuf, M_TEMP);
   2587 		if (cookies)
   2588 			free((caddr_t)cookies, M_TEMP);
   2589 		nfsm_reply(NFSX_POSTOPATTR(v3));
   2590 		nfsm_srvpostop_attr(getret, &at);
   2591 		return (0);
   2592 	}
   2593 	if (io.uio_resid) {
   2594 		siz -= io.uio_resid;
   2595 
   2596 		/*
   2597 		 * If nothing read, return eof
   2598 		 * rpc reply
   2599 		 */
   2600 		if (siz == 0) {
   2601 			vrele(vp);
   2602 			nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
   2603 				2 * NFSX_UNSIGNED);
   2604 			if (v3) {
   2605 				nfsm_srvpostop_attr(getret, &at);
   2606 				nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
   2607 				txdr_hyper(&at.va_filerev, tl);
   2608 				tl += 2;
   2609 			} else
   2610 				nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2611 			*tl++ = nfs_false;
   2612 			*tl = nfs_true;
   2613 			FREE((caddr_t)rbuf, M_TEMP);
   2614 			FREE((caddr_t)cookies, M_TEMP);
   2615 			return (0);
   2616 		}
   2617 	}
   2618 
   2619 	/*
   2620 	 * Check for degenerate cases of nothing useful read.
   2621 	 * If so go try again
   2622 	 */
   2623 	cpos = rbuf;
   2624 	cend = rbuf + siz;
   2625 	dp = (struct dirent *)cpos;
   2626 	cookiep = cookies;
   2627 
   2628 	while (cpos < cend && ncookies > 0 &&
   2629 		(dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
   2630 		cpos += dp->d_reclen;
   2631 		dp = (struct dirent *)cpos;
   2632 		cookiep++;
   2633 		ncookies--;
   2634 	}
   2635 	if (cpos >= cend || ncookies == 0) {
   2636 		toff = off;
   2637 		siz = fullsiz;
   2638 		goto again;
   2639 	}
   2640 
   2641 	len = 3 * NFSX_UNSIGNED;	/* paranoia, probably can be 0 */
   2642 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
   2643 	if (v3) {
   2644 		nfsm_srvpostop_attr(getret, &at);
   2645 		nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2646 		txdr_hyper(&at.va_filerev, tl);
   2647 	}
   2648 	mp = mp2 = mb;
   2649 	bp = bpos;
   2650 	be = bp + M_TRAILINGSPACE(mp);
   2651 
   2652 	/* Loop through the records and build reply */
   2653 	while (cpos < cend && ncookies > 0) {
   2654 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
   2655 			nlen = dp->d_namlen;
   2656 			rem = nfsm_rndup(nlen)-nlen;
   2657 			len += (4 * NFSX_UNSIGNED + nlen + rem);
   2658 			if (v3)
   2659 				len += 2 * NFSX_UNSIGNED;
   2660 			if (len > cnt) {
   2661 				eofflag = 0;
   2662 				break;
   2663 			}
   2664 			/*
   2665 			 * Build the directory record xdr from
   2666 			 * the dirent entry.
   2667 			 */
   2668 			nfsm_clget;
   2669 			*tl = nfs_true;
   2670 			bp += NFSX_UNSIGNED;
   2671 			if (v3) {
   2672 				nfsm_clget;
   2673 				*tl = 0;
   2674 				bp += NFSX_UNSIGNED;
   2675 			}
   2676 			nfsm_clget;
   2677 			*tl = txdr_unsigned(dp->d_fileno);
   2678 			bp += NFSX_UNSIGNED;
   2679 			nfsm_clget;
   2680 			*tl = txdr_unsigned(nlen);
   2681 			bp += NFSX_UNSIGNED;
   2682 
   2683 			/* And loop around copying the name */
   2684 			xfer = nlen;
   2685 			cp = dp->d_name;
   2686 			while (xfer > 0) {
   2687 				nfsm_clget;
   2688 				if ((bp+xfer) > be)
   2689 					tsiz = be-bp;
   2690 				else
   2691 					tsiz = xfer;
   2692 				bcopy(cp, bp, tsiz);
   2693 				bp += tsiz;
   2694 				xfer -= tsiz;
   2695 				if (xfer > 0)
   2696 					cp += tsiz;
   2697 			}
   2698 			/* And null pad to an int32_t boundary */
   2699 			for (i = 0; i < rem; i++)
   2700 				*bp++ = '\0';
   2701 			nfsm_clget;
   2702 
   2703 			/* Finish off the record */
   2704 			txdr_hyper(cookiep, &jar);
   2705 			if (v3) {
   2706 				*tl = jar.nfsuquad[0];
   2707 				bp += NFSX_UNSIGNED;
   2708 				nfsm_clget;
   2709 			}
   2710 			*tl = jar.nfsuquad[1];
   2711 			bp += NFSX_UNSIGNED;
   2712 		}
   2713 		cpos += dp->d_reclen;
   2714 		dp = (struct dirent *)cpos;
   2715 		cookiep++;
   2716 		ncookies--;
   2717 	}
   2718 	vrele(vp);
   2719 	nfsm_clget;
   2720 	*tl = nfs_false;
   2721 	bp += NFSX_UNSIGNED;
   2722 	nfsm_clget;
   2723 	if (eofflag)
   2724 		*tl = nfs_true;
   2725 	else
   2726 		*tl = nfs_false;
   2727 	bp += NFSX_UNSIGNED;
   2728 	if (mp != mb) {
   2729 		if (bp < be)
   2730 			mp->m_len = bp - mtod(mp, caddr_t);
   2731 	} else
   2732 		mp->m_len += bp - bpos;
   2733 	FREE((caddr_t)rbuf, M_TEMP);
   2734 	FREE((caddr_t)cookies, M_TEMP);
   2735 	nfsm_srvdone;
   2736 }
   2737 
   2738 int
   2739 nfsrv_readdirplus(nfsd, slp, procp, mrq)
   2740 	struct nfsrv_descript *nfsd;
   2741 	struct nfssvc_sock *slp;
   2742 	struct proc *procp;
   2743 	struct mbuf **mrq;
   2744 {
   2745 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   2746 	struct mbuf *nam = nfsd->nd_nam;
   2747 	caddr_t dpos = nfsd->nd_dpos;
   2748 	struct ucred *cred = &nfsd->nd_cr;
   2749 	register char *bp, *be;
   2750 	register struct mbuf *mp;
   2751 	register struct dirent *dp;
   2752 	register caddr_t cp;
   2753 	register u_int32_t *tl;
   2754 	register int32_t t1;
   2755 	caddr_t bpos;
   2756 	struct mbuf *mb, *mb2, *mreq, *mp2;
   2757 	char *cpos, *cend, *cp2, *rbuf;
   2758 	struct vnode *vp, *nvp;
   2759 	struct flrep fl;
   2760 	nfsfh_t nfh;
   2761 	fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
   2762 	struct uio io;
   2763 	struct iovec iv;
   2764 	struct vattr va, at, *vap = &va;
   2765 	struct nfs_fattr *fp;
   2766 	int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
   2767 	int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
   2768 	u_quad_t frev, off, toff, verf;
   2769 	off_t *cookies = NULL, *cookiep;
   2770 
   2771 	fhp = &nfh.fh_generic;
   2772 	nfsm_srvmtofh(fhp);
   2773 	nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
   2774 	fxdr_hyper(tl, &toff);
   2775 	tl += 2;
   2776 	fxdr_hyper(tl, &verf);
   2777 	tl += 2;
   2778 	siz = fxdr_unsigned(int, *tl++);
   2779 	cnt = fxdr_unsigned(int, *tl);
   2780 	off = toff;
   2781 	siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
   2782 	xfer = NFS_SRVMAXDATA(nfsd);
   2783 	if (siz > xfer)
   2784 		siz = xfer;
   2785 	fullsiz = siz;
   2786 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
   2787 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   2788 	if (!error && vp->v_type != VDIR) {
   2789 		error = ENOTDIR;
   2790 		vput(vp);
   2791 	}
   2792 	if (error) {
   2793 		nfsm_reply(NFSX_UNSIGNED);
   2794 		nfsm_srvpostop_attr(getret, &at);
   2795 		return (0);
   2796 	}
   2797 	error = getret = VOP_GETATTR(vp, &at, cred, procp);
   2798 #ifdef NFS3_STRICTVERF
   2799 	/*
   2800 	 * XXX This check is too strict for Solaris 2.5 clients.
   2801 	 */
   2802 	if (!error && toff && verf != at.va_filerev)
   2803 		error = NFSERR_BAD_COOKIE;
   2804 #endif
   2805 	if (!error) {
   2806 		nqsrv_getl(vp, ND_READ);
   2807 		error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
   2808 	}
   2809 	if (error) {
   2810 		vput(vp);
   2811 		nfsm_reply(NFSX_V3POSTOPATTR);
   2812 		nfsm_srvpostop_attr(getret, &at);
   2813 		return (0);
   2814 	}
   2815 #ifdef Lite2_integrated
   2816 	VOP_UNLOCK(vp, 0, procp);
   2817 #else
   2818 	VOP_UNLOCK(vp);
   2819 #endif
   2820 
   2821 	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
   2822 	ncookies = siz / (7 * NFSX_UNSIGNED);
   2823 	MALLOC(cookies, off_t *, ncookies * sizeof (off_t), M_TEMP,
   2824 		M_WAITOK);
   2825 again:
   2826 	iv.iov_base = rbuf;
   2827 	iv.iov_len = fullsiz;
   2828 	io.uio_iov = &iv;
   2829 	io.uio_iovcnt = 1;
   2830 	io.uio_offset = (off_t)off;
   2831 	io.uio_resid = fullsiz;
   2832 	io.uio_segflg = UIO_SYSSPACE;
   2833 	io.uio_rw = UIO_READ;
   2834 	io.uio_procp = (struct proc *)0;
   2835 	eofflag = 0;
   2836 
   2837 #ifdef Lite2_integrated
   2838 	VOP_LOCK(vp, 0, procp);
   2839 #else
   2840 	VOP_LOCK(vp);
   2841 #endif
   2842 	error = VOP_READDIR(vp, &io, cred, &eofflag, cookies, ncookies);
   2843 
   2844 	off = (u_quad_t)io.uio_offset;
   2845 	getret = VOP_GETATTR(vp, &at, cred, procp);
   2846 
   2847 #ifdef Lite2_integrated
   2848 	VOP_UNLOCK(vp, 0, procp);
   2849 #else
   2850 	VOP_UNLOCK(vp);
   2851 #endif
   2852 
   2853 	/*
   2854 	 * If the VGET operation doesn't work for this filesystem,
   2855 	 * we can't support readdirplus. Returning NOTSUPP should
   2856 	 * make clients fall back to plain readdir.
   2857 	 * There's no need to check for VPTOFH as well, we wouldn't
   2858 	 * even be here otherwise.
   2859 	 */
   2860 	if (!getret) {
   2861 		if ((getret = VFS_VGET(vp->v_mount, at.va_fileid, &nvp)))
   2862 			getret = (getret == EOPNOTSUPP) ?
   2863 				NFSERR_NOTSUPP : NFSERR_IO;
   2864 		else
   2865 			vput(nvp);
   2866 	}
   2867 
   2868 	if (!cookies && !error)
   2869 		error = NFSERR_PERM;
   2870 	if (!error)
   2871 		error = getret;
   2872 	if (error) {
   2873 		vrele(vp);
   2874 		if (cookies)
   2875 			free((caddr_t)cookies, M_TEMP);
   2876 		free((caddr_t)rbuf, M_TEMP);
   2877 		nfsm_reply(NFSX_V3POSTOPATTR);
   2878 		nfsm_srvpostop_attr(getret, &at);
   2879 		return (0);
   2880 	}
   2881 	if (io.uio_resid) {
   2882 		siz -= io.uio_resid;
   2883 
   2884 		/*
   2885 		 * If nothing read, return eof
   2886 		 * rpc reply
   2887 		 */
   2888 		if (siz == 0) {
   2889 			vrele(vp);
   2890 			nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
   2891 				2 * NFSX_UNSIGNED);
   2892 			nfsm_srvpostop_attr(getret, &at);
   2893 			nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
   2894 			txdr_hyper(&at.va_filerev, tl);
   2895 			tl += 2;
   2896 			*tl++ = nfs_false;
   2897 			*tl = nfs_true;
   2898 			FREE((caddr_t)cookies, M_TEMP);
   2899 			FREE((caddr_t)rbuf, M_TEMP);
   2900 			return (0);
   2901 		}
   2902 	}
   2903 
   2904 	/*
   2905 	 * Check for degenerate cases of nothing useful read.
   2906 	 * If so go try again
   2907 	 */
   2908 	cpos = rbuf;
   2909 	cend = rbuf + siz;
   2910 	dp = (struct dirent *)cpos;
   2911 	cookiep = cookies;
   2912 
   2913 	while (cpos < cend && ncookies > 0 &&
   2914 		(dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
   2915 		cpos += dp->d_reclen;
   2916 		dp = (struct dirent *)cpos;
   2917 		cookiep++;
   2918 		ncookies--;
   2919 	}
   2920 	if (cpos >= cend || ncookies == 0) {
   2921 		toff = off;
   2922 		siz = fullsiz;
   2923 		goto again;
   2924 	}
   2925 
   2926 	dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
   2927 	nfsm_reply(cnt);
   2928 	nfsm_srvpostop_attr(getret, &at);
   2929 	nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2930 	txdr_hyper(&at.va_filerev, tl);
   2931 	mp = mp2 = mb;
   2932 	bp = bpos;
   2933 	be = bp + M_TRAILINGSPACE(mp);
   2934 
   2935 	/* Loop through the records and build reply */
   2936 	while (cpos < cend && ncookies > 0) {
   2937 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
   2938 			nlen = dp->d_namlen;
   2939 			rem = nfsm_rndup(nlen)-nlen;
   2940 
   2941 			/*
   2942 			 * For readdir_and_lookup get the vnode using
   2943 			 * the file number.
   2944 			 */
   2945 			if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
   2946 				goto invalid;
   2947 			bzero((caddr_t)nfhp, NFSX_V3FH);
   2948 			nfhp->fh_fsid =
   2949 				nvp->v_mount->mnt_stat.f_fsid;
   2950 			if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
   2951 				vput(nvp);
   2952 				goto invalid;
   2953 			}
   2954 			if (VOP_GETATTR(nvp, vap, cred, procp)) {
   2955 				vput(nvp);
   2956 				goto invalid;
   2957 			}
   2958 			vput(nvp);
   2959 
   2960 			/*
   2961 			 * If either the dircount or maxcount will be
   2962 			 * exceeded, get out now. Both of these lengths
   2963 			 * are calculated conservatively, including all
   2964 			 * XDR overheads.
   2965 			 */
   2966 			len += (7 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
   2967 				NFSX_V3POSTOPATTR);
   2968 			dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
   2969 			if (len > cnt || dirlen > fullsiz) {
   2970 				eofflag = 0;
   2971 				break;
   2972 			}
   2973 
   2974 			/*
   2975 			 * Build the directory record xdr from
   2976 			 * the dirent entry.
   2977 			 */
   2978 			fp = (struct nfs_fattr *)&fl.fl_fattr;
   2979 			nfsm_srvfillattr(vap, fp);
   2980 			fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
   2981 			fl.fl_fhok = nfs_true;
   2982 			fl.fl_postopok = nfs_true;
   2983 			txdr_hyper(cookiep, fl.fl_off.nfsuquad);
   2984 
   2985 			nfsm_clget;
   2986 			*tl = nfs_true;
   2987 			bp += NFSX_UNSIGNED;
   2988 			nfsm_clget;
   2989 			*tl = 0;
   2990 			bp += NFSX_UNSIGNED;
   2991 			nfsm_clget;
   2992 			*tl = txdr_unsigned(dp->d_fileno);
   2993 			bp += NFSX_UNSIGNED;
   2994 			nfsm_clget;
   2995 			*tl = txdr_unsigned(nlen);
   2996 			bp += NFSX_UNSIGNED;
   2997 
   2998 			/* And loop around copying the name */
   2999 			xfer = nlen;
   3000 			cp = dp->d_name;
   3001 			while (xfer > 0) {
   3002 				nfsm_clget;
   3003 				if ((bp + xfer) > be)
   3004 					tsiz = be - bp;
   3005 				else
   3006 					tsiz = xfer;
   3007 				bcopy(cp, bp, tsiz);
   3008 				bp += tsiz;
   3009 				xfer -= tsiz;
   3010 				if (xfer > 0)
   3011 					cp += tsiz;
   3012 			}
   3013 			/* And null pad to an int32_t boundary */
   3014 			for (i = 0; i < rem; i++)
   3015 				*bp++ = '\0';
   3016 
   3017 			/*
   3018 			 * Now copy the flrep structure out.
   3019 			 */
   3020 			xfer = sizeof (struct flrep);
   3021 			cp = (caddr_t)&fl;
   3022 			while (xfer > 0) {
   3023 				nfsm_clget;
   3024 				if ((bp + xfer) > be)
   3025 					tsiz = be - bp;
   3026 				else
   3027 					tsiz = xfer;
   3028 				bcopy(cp, bp, tsiz);
   3029 				bp += tsiz;
   3030 				xfer -= tsiz;
   3031 				if (xfer > 0)
   3032 					cp += tsiz;
   3033 			}
   3034 		}
   3035 invalid:
   3036 		cpos += dp->d_reclen;
   3037 		dp = (struct dirent *)cpos;
   3038 		cookiep++;
   3039 		ncookies--;
   3040 	}
   3041 	vrele(vp);
   3042 	nfsm_clget;
   3043 	*tl = nfs_false;
   3044 	bp += NFSX_UNSIGNED;
   3045 	nfsm_clget;
   3046 	if (eofflag)
   3047 		*tl = nfs_true;
   3048 	else
   3049 		*tl = nfs_false;
   3050 	bp += NFSX_UNSIGNED;
   3051 	if (mp != mb) {
   3052 		if (bp < be)
   3053 			mp->m_len = bp - mtod(mp, caddr_t);
   3054 	} else
   3055 		mp->m_len += bp - bpos;
   3056 	FREE((caddr_t)cookies, M_TEMP);
   3057 	FREE((caddr_t)rbuf, M_TEMP);
   3058 	nfsm_srvdone;
   3059 }
   3060 
   3061 /*
   3062  * nfs commit service
   3063  */
   3064 int
   3065 nfsrv_commit(nfsd, slp, procp, mrq)
   3066 	struct nfsrv_descript *nfsd;
   3067 	struct nfssvc_sock *slp;
   3068 	struct proc *procp;
   3069 	struct mbuf **mrq;
   3070 {
   3071 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   3072 	struct mbuf *nam = nfsd->nd_nam;
   3073 	caddr_t dpos = nfsd->nd_dpos;
   3074 	struct ucred *cred = &nfsd->nd_cr;
   3075 	struct vattr bfor, aft;
   3076 	struct vnode *vp;
   3077 	nfsfh_t nfh;
   3078 	fhandle_t *fhp;
   3079 	register u_int32_t *tl;
   3080 	register int32_t t1;
   3081 	caddr_t bpos;
   3082 	int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
   3083 	char *cp2;
   3084 	struct mbuf *mb, *mb2, *mreq;
   3085 	u_quad_t frev, off;
   3086 
   3087 #ifndef nolint
   3088 	cache = 0;
   3089 #endif
   3090 	fhp = &nfh.fh_generic;
   3091 	nfsm_srvmtofh(fhp);
   3092 	nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   3093 
   3094 	/*
   3095 	 * XXX At this time VOP_FSYNC() does not accept offset and byte
   3096 	 * count parameters, so these arguments are useless (someday maybe).
   3097 	 */
   3098 	fxdr_hyper(tl, &off);
   3099 	tl += 2;
   3100 	cnt = fxdr_unsigned(int, *tl);
   3101 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
   3102 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   3103 	if (error) {
   3104 		nfsm_reply(2 * NFSX_UNSIGNED);
   3105 		nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
   3106 		return (0);
   3107 	}
   3108 	for_ret = VOP_GETATTR(vp, &bfor, cred, procp);
   3109 	error = VOP_FSYNC(vp, cred, MNT_WAIT, procp);
   3110 	aft_ret = VOP_GETATTR(vp, &aft, cred, procp);
   3111 	vput(vp);
   3112 	nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
   3113 	nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
   3114 	if (!error) {
   3115 		nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
   3116 		*tl++ = txdr_unsigned(boottime.tv_sec);
   3117 		*tl = txdr_unsigned(boottime.tv_usec);
   3118 	} else
   3119 		return (0);
   3120 	nfsm_srvdone;
   3121 }
   3122 
   3123 /*
   3124  * nfs statfs service
   3125  */
   3126 int
   3127 nfsrv_statfs(nfsd, slp, procp, mrq)
   3128 	struct nfsrv_descript *nfsd;
   3129 	struct nfssvc_sock *slp;
   3130 	struct proc *procp;
   3131 	struct mbuf **mrq;
   3132 {
   3133 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   3134 	struct mbuf *nam = nfsd->nd_nam;
   3135 	caddr_t dpos = nfsd->nd_dpos;
   3136 	struct ucred *cred = &nfsd->nd_cr;
   3137 	register struct statfs *sf;
   3138 	register struct nfs_statfs *sfp;
   3139 	register u_int32_t *tl;
   3140 	register int32_t t1;
   3141 	caddr_t bpos;
   3142 	int error = 0, rdonly, cache, getret = 1;
   3143 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   3144 	char *cp2;
   3145 	struct mbuf *mb, *mb2, *mreq;
   3146 	struct vnode *vp;
   3147 	struct vattr at;
   3148 	nfsfh_t nfh;
   3149 	fhandle_t *fhp;
   3150 	struct statfs statfs;
   3151 	u_quad_t frev, tval;
   3152 
   3153 #ifndef nolint
   3154 	cache = 0;
   3155 #endif
   3156 	fhp = &nfh.fh_generic;
   3157 	nfsm_srvmtofh(fhp);
   3158 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
   3159 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   3160 	if (error) {
   3161 		nfsm_reply(NFSX_UNSIGNED);
   3162 		nfsm_srvpostop_attr(getret, &at);
   3163 		return (0);
   3164 	}
   3165 	sf = &statfs;
   3166 	error = VFS_STATFS(vp->v_mount, sf, procp);
   3167 	getret = VOP_GETATTR(vp, &at, cred, procp);
   3168 	vput(vp);
   3169 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
   3170 	if (v3)
   3171 		nfsm_srvpostop_attr(getret, &at);
   3172 	if (error)
   3173 		return (0);
   3174 	nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
   3175 	if (v3) {
   3176 		tval = (u_quad_t)sf->f_blocks;
   3177 		tval *= (u_quad_t)sf->f_bsize;
   3178 		txdr_hyper(&tval, &sfp->sf_tbytes);
   3179 		tval = (u_quad_t)sf->f_bfree;
   3180 		tval *= (u_quad_t)sf->f_bsize;
   3181 		txdr_hyper(&tval, &sfp->sf_fbytes);
   3182 		tval = (u_quad_t)sf->f_bavail;
   3183 		tval *= (u_quad_t)sf->f_bsize;
   3184 		txdr_hyper(&tval, &sfp->sf_abytes);
   3185 		sfp->sf_tfiles.nfsuquad[0] = 0;
   3186 		sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
   3187 		sfp->sf_ffiles.nfsuquad[0] = 0;
   3188 		sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
   3189 		sfp->sf_afiles.nfsuquad[0] = 0;
   3190 		sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
   3191 		sfp->sf_invarsec = 0;
   3192 	} else {
   3193 		sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
   3194 		sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
   3195 		sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
   3196 		sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
   3197 		sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
   3198 	}
   3199 	nfsm_srvdone;
   3200 }
   3201 
   3202 /*
   3203  * nfs fsinfo service
   3204  */
   3205 int
   3206 nfsrv_fsinfo(nfsd, slp, procp, mrq)
   3207 	struct nfsrv_descript *nfsd;
   3208 	struct nfssvc_sock *slp;
   3209 	struct proc *procp;
   3210 	struct mbuf **mrq;
   3211 {
   3212 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   3213 	struct mbuf *nam = nfsd->nd_nam;
   3214 	caddr_t dpos = nfsd->nd_dpos;
   3215 	struct ucred *cred = &nfsd->nd_cr;
   3216 	register u_int32_t *tl;
   3217 	register struct nfsv3_fsinfo *sip;
   3218 	register int32_t t1;
   3219 	caddr_t bpos;
   3220 	int error = 0, rdonly, cache, getret = 1, pref;
   3221 	char *cp2;
   3222 	struct mbuf *mb, *mb2, *mreq;
   3223 	struct vnode *vp;
   3224 	struct vattr at;
   3225 	nfsfh_t nfh;
   3226 	fhandle_t *fhp;
   3227 	u_quad_t frev, maxfsize;
   3228 	struct statfs sb;
   3229 
   3230 #ifndef nolint
   3231 	cache = 0;
   3232 #endif
   3233 	fhp = &nfh.fh_generic;
   3234 	nfsm_srvmtofh(fhp);
   3235 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
   3236 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   3237 	if (error) {
   3238 		nfsm_reply(NFSX_UNSIGNED);
   3239 		nfsm_srvpostop_attr(getret, &at);
   3240 		return (0);
   3241 	}
   3242 
   3243 	/* XXX Try to make a guess on the max file size. */
   3244 	VFS_STATFS(vp->v_mount, &sb, (struct proc *)0);
   3245 	maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
   3246 
   3247 	getret = VOP_GETATTR(vp, &at, cred, procp);
   3248 	vput(vp);
   3249 	nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
   3250 	nfsm_srvpostop_attr(getret, &at);
   3251 	nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
   3252 
   3253 	/*
   3254 	 * XXX
   3255 	 * There should be file system VFS OP(s) to get this information.
   3256 	 * For now, assume ufs.
   3257 	 */
   3258 	if (slp->ns_so->so_type == SOCK_DGRAM)
   3259 		pref = NFS_MAXDGRAMDATA;
   3260 	else
   3261 		pref = NFS_MAXDATA;
   3262 	sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
   3263 	sip->fs_rtpref = txdr_unsigned(pref);
   3264 	sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
   3265 	sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
   3266 	sip->fs_wtpref = txdr_unsigned(pref);
   3267 	sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
   3268 	sip->fs_dtpref = txdr_unsigned(pref);
   3269 	txdr_hyper(&maxfsize, &sip->fs_maxfilesize);
   3270 	sip->fs_timedelta.nfsv3_sec = 0;
   3271 	sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
   3272 	sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
   3273 		NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
   3274 		NFSV3FSINFO_CANSETTIME);
   3275 	nfsm_srvdone;
   3276 }
   3277 
   3278 /*
   3279  * nfs pathconf service
   3280  */
   3281 int
   3282 nfsrv_pathconf(nfsd, slp, procp, mrq)
   3283 	struct nfsrv_descript *nfsd;
   3284 	struct nfssvc_sock *slp;
   3285 	struct proc *procp;
   3286 	struct mbuf **mrq;
   3287 {
   3288 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   3289 	struct mbuf *nam = nfsd->nd_nam;
   3290 	caddr_t dpos = nfsd->nd_dpos;
   3291 	struct ucred *cred = &nfsd->nd_cr;
   3292 	register u_int32_t *tl;
   3293 	register struct nfsv3_pathconf *pc;
   3294 	register int32_t t1;
   3295 	caddr_t bpos;
   3296 	int error = 0, rdonly, cache, getret = 1;
   3297 	register_t linkmax, namemax, chownres, notrunc;
   3298 	char *cp2;
   3299 	struct mbuf *mb, *mb2, *mreq;
   3300 	struct vnode *vp;
   3301 	struct vattr at;
   3302 	nfsfh_t nfh;
   3303 	fhandle_t *fhp;
   3304 	u_quad_t frev;
   3305 
   3306 #ifndef nolint
   3307 	cache = 0;
   3308 #endif
   3309 	fhp = &nfh.fh_generic;
   3310 	nfsm_srvmtofh(fhp);
   3311 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
   3312 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
   3313 	if (error) {
   3314 		nfsm_reply(NFSX_UNSIGNED);
   3315 		nfsm_srvpostop_attr(getret, &at);
   3316 		return (0);
   3317 	}
   3318 	error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
   3319 	if (!error)
   3320 		error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
   3321 	if (!error)
   3322 		error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
   3323 	if (!error)
   3324 		error = VOP_PATHCONF(vp, _PC_NO_TRUNC, &notrunc);
   3325 	getret = VOP_GETATTR(vp, &at, cred, procp);
   3326 	vput(vp);
   3327 	nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
   3328 	nfsm_srvpostop_attr(getret, &at);
   3329 	if (error)
   3330 		return (0);
   3331 	nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
   3332 
   3333 	pc->pc_linkmax = txdr_unsigned(linkmax);
   3334 	pc->pc_namemax = txdr_unsigned(namemax);
   3335 	pc->pc_notrunc = txdr_unsigned(notrunc);
   3336 	pc->pc_chownrestricted = txdr_unsigned(chownres);
   3337 
   3338 	/*
   3339 	 * These should probably be supported by VOP_PATHCONF(), but
   3340 	 * until msdosfs is exportable (why would you want to?), the
   3341 	 * Unix defaults should be ok.
   3342 	 */
   3343 	pc->pc_caseinsensitive = nfs_false;
   3344 	pc->pc_casepreserving = nfs_true;
   3345 	nfsm_srvdone;
   3346 }
   3347 
   3348 /*
   3349  * Null operation, used by clients to ping server
   3350  */
   3351 /* ARGSUSED */
   3352 int
   3353 nfsrv_null(nfsd, slp, procp, mrq)
   3354 	struct nfsrv_descript *nfsd;
   3355 	struct nfssvc_sock *slp;
   3356 	struct proc *procp;
   3357 	struct mbuf **mrq;
   3358 {
   3359 	struct mbuf *mrep = nfsd->nd_mrep;
   3360 	caddr_t bpos;
   3361 	int error = NFSERR_RETVOID, cache = 0;
   3362 	struct mbuf *mb, *mreq;
   3363 	u_quad_t frev;
   3364 
   3365 	nfsm_reply(0);
   3366 	return (0);
   3367 }
   3368 
   3369 /*
   3370  * No operation, used for obsolete procedures
   3371  */
   3372 /* ARGSUSED */
   3373 int
   3374 nfsrv_noop(nfsd, slp, procp, mrq)
   3375 	struct nfsrv_descript *nfsd;
   3376 	struct nfssvc_sock *slp;
   3377 	struct proc *procp;
   3378 	struct mbuf **mrq;
   3379 {
   3380 	struct mbuf *mrep = nfsd->nd_mrep;
   3381 	caddr_t bpos;
   3382 	int error, cache = 0;
   3383 	struct mbuf *mb, *mreq;
   3384 	u_quad_t frev;
   3385 
   3386 	if (nfsd->nd_repstat)
   3387 		error = nfsd->nd_repstat;
   3388 	else
   3389 		error = EPROCUNAVAIL;
   3390 	nfsm_reply(0);
   3391 	return (0);
   3392 }
   3393 
   3394 /*
   3395  * Perform access checking for vnodes obtained from file handles that would
   3396  * refer to files already opened by a Unix client. You cannot just use
   3397  * vn_writechk() and VOP_ACCESS() for two reasons.
   3398  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
   3399  * 2 - The owner is to be given access irrespective of mode bits for some
   3400  *     operations, so that processes that chmod after opening a file don't
   3401  *     break. I don't like this because it opens a security hole, but since
   3402  *     the nfs server opens a security hole the size of a barn door anyhow,
   3403  *     what the heck.
   3404  *
   3405  * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
   3406  * will return EPERM instead of EACCESS. EPERM is always an error.
   3407  */
   3408 int
   3409 nfsrv_access(vp, flags, cred, rdonly, p, override)
   3410 	register struct vnode *vp;
   3411 	int flags;
   3412 	register struct ucred *cred;
   3413 	int rdonly;
   3414 	struct proc *p;
   3415 {
   3416 	struct vattr vattr;
   3417 	int error;
   3418 	if (flags & VWRITE) {
   3419 		/* Just vn_writechk() changed to check rdonly */
   3420 		/*
   3421 		 * Disallow write attempts on read-only file systems;
   3422 		 * unless the file is a socket or a block or character
   3423 		 * device resident on the file system.
   3424 		 */
   3425 		if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
   3426 			switch (vp->v_type) {
   3427 			case VREG:
   3428 			case VDIR:
   3429 			case VLNK:
   3430 				return (EROFS);
   3431 			default:
   3432 				break;
   3433 			}
   3434 		}
   3435 		/*
   3436 		 * If there's shared text associated with
   3437 		 * the inode, try to free it up once.  If
   3438 		 * we fail, we can't allow writing.
   3439 		 */
   3440 #if defined(UVM)
   3441 		if ((vp->v_flag & VTEXT) && !uvm_vnp_uncache(vp))
   3442 			return (ETXTBSY);
   3443 #else
   3444 		if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
   3445 			return (ETXTBSY);
   3446 #endif
   3447 	}
   3448 	error = VOP_GETATTR(vp, &vattr, cred, p);
   3449 	if (error)
   3450 		return (error);
   3451 	error = VOP_ACCESS(vp, flags, cred, p);
   3452 	/*
   3453 	 * Allow certain operations for the owner (reads and writes
   3454 	 * on files that are already open).
   3455 	 */
   3456 	if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
   3457 		error = 0;
   3458 	return error;
   3459 }
   3460