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