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