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