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