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