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