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