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