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