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