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