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