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