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