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