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