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