Home | History | Annotate | Line # | Download | only in nfs
nfs_serv.c revision 1.20
      1 /*	$NetBSD: nfs_serv.c,v 1.20 1996/02/01 00:40:13 jtc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)nfs_serv.c	8.4 (Berkeley) 6/4/94
     39  */
     40 
     41 /*
     42  * nfs version 2 server calls to vnode ops
     43  * - these routines generally have 3 phases
     44  *   1 - break down and validate rpc request in mbuf list
     45  *   2 - do the vnode ops for the request
     46  *       (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
     47  *   3 - build the rpc reply in an mbuf list
     48  *   nb:
     49  *	- do not mix the phases, since the nfsm_?? macros can return failures
     50  *	  on a bad rpc or similar and do not do any vrele() or vput()'s
     51  *
     52  *      - the nfsm_reply() macro generates an nfs rpc reply with the nfs
     53  *	error number iff error != 0 whereas
     54  *	returning an error from the server function implies a fatal error
     55  *	such as a badly constructed rpc request that should be dropped without
     56  *	a reply.
     57  */
     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/mbuf.h>
     67 #include <sys/dirent.h>
     68 #include <sys/stat.h>
     69 
     70 #include <vm/vm.h>
     71 
     72 #include <nfs/nfsv2.h>
     73 #include <nfs/rpcv2.h>
     74 #include <nfs/nfs.h>
     75 #include <nfs/xdr_subs.h>
     76 #include <nfs/nfsm_subs.h>
     77 #include <nfs/nqnfs.h>
     78 
     79 /* Defs */
     80 #define	TRUE	1
     81 #define	FALSE	0
     82 
     83 /* Global vars */
     84 extern u_long nfs_procids[NFS_NPROCS];
     85 extern u_int32_t nfs_xdrneg1;
     86 extern u_int32_t nfs_false, nfs_true;
     87 nfstype nfs_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
     88 		      NFCHR, NFNON };
     89 
     90 /*
     91  * nqnfs access service
     92  */
     93 nqnfsrv_access(nfsd, mrep, md, dpos, cred, nam, mrq)
     94 	struct nfsd *nfsd;
     95 	struct mbuf *mrep, *md;
     96 	caddr_t dpos;
     97 	struct ucred *cred;
     98 	struct mbuf *nam, **mrq;
     99 {
    100 	struct vnode *vp;
    101 	nfsv2fh_t nfh;
    102 	fhandle_t *fhp;
    103 	register u_int32_t *tl;
    104 	register int32_t t1;
    105 	caddr_t bpos;
    106 	int error = 0, rdonly, cache, mode = 0;
    107 	char *cp2;
    108 	struct mbuf *mb, *mreq;
    109 	u_quad_t frev;
    110 
    111 	fhp = &nfh.fh_generic;
    112 	nfsm_srvmtofh(fhp);
    113 	nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
    114 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    115 		nfsm_reply(0);
    116 	if (*tl++ == nfs_true)
    117 		mode |= VREAD;
    118 	if (*tl++ == nfs_true)
    119 		mode |= VWRITE;
    120 	if (*tl == nfs_true)
    121 		mode |= VEXEC;
    122 	error = nfsrv_access(vp, mode, cred, rdonly, nfsd->nd_procp);
    123 	vput(vp);
    124 	nfsm_reply(0);
    125 	nfsm_srvdone;
    126 }
    127 
    128 /*
    129  * nfs getattr service
    130  */
    131 nfsrv_getattr(nfsd, mrep, md, dpos, cred, nam, mrq)
    132 	struct nfsd *nfsd;
    133 	struct mbuf *mrep, *md;
    134 	caddr_t dpos;
    135 	struct ucred *cred;
    136 	struct mbuf *nam, **mrq;
    137 {
    138 	register struct nfsv2_fattr *fp;
    139 	struct vattr va;
    140 	struct vnode *vp;
    141 	nfsv2fh_t nfh;
    142 	fhandle_t *fhp;
    143 	register u_int32_t *tl;
    144 	register int32_t t1;
    145 	caddr_t bpos;
    146 	int error = 0, rdonly, cache;
    147 	char *cp2;
    148 	struct mbuf *mb, *mb2, *mreq;
    149 	u_quad_t frev;
    150 
    151 	fhp = &nfh.fh_generic;
    152 	nfsm_srvmtofh(fhp);
    153 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    154 		nfsm_reply(0);
    155 	nqsrv_getl(vp, NQL_READ);
    156 	error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp);
    157 	vput(vp);
    158 	nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    159 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    160 	nfsm_srvfillattr;
    161 	nfsm_srvdone;
    162 }
    163 
    164 /*
    165  * nfs setattr service
    166  */
    167 nfsrv_setattr(nfsd, mrep, md, dpos, cred, nam, mrq)
    168 	struct nfsd *nfsd;
    169 	struct mbuf *mrep, *md;
    170 	caddr_t dpos;
    171 	struct ucred *cred;
    172 	struct mbuf *nam, **mrq;
    173 {
    174 	struct vattr va;
    175 	register struct nfsv2_sattr *sp;
    176 	register struct nfsv2_fattr *fp;
    177 	struct vnode *vp;
    178 	nfsv2fh_t nfh;
    179 	fhandle_t *fhp;
    180 	register u_int32_t *tl;
    181 	register int32_t t1;
    182 	caddr_t bpos;
    183 	int error = 0, rdonly, cache;
    184 	char *cp2;
    185 	struct mbuf *mb, *mb2, *mreq;
    186 	u_quad_t frev, frev2;
    187 
    188 	fhp = &nfh.fh_generic;
    189 	nfsm_srvmtofh(fhp);
    190 	nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_SATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    191 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    192 		nfsm_reply(0);
    193 	nqsrv_getl(vp, NQL_WRITE);
    194 	VATTR_NULL(&va);
    195 	/*
    196 	 * Nah nah nah nah na nah
    197 	 * There is a bug in the Sun client that puts 0xffff in the mode
    198 	 * field of sattr when it should put in 0xffffffff. The u_short
    199 	 * doesn't sign extend.
    200 	 * --> check the low order 2 bytes for 0xffff
    201 	 */
    202 	if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
    203 		va.va_mode = nfstov_mode(sp->sa_mode);
    204 	if (sp->sa_uid != nfs_xdrneg1)
    205 		va.va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
    206 	if (sp->sa_gid != nfs_xdrneg1)
    207 		va.va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
    208 	if (nfsd->nd_nqlflag == NQL_NOVAL) {
    209 		if (sp->sa_nfssize != nfs_xdrneg1)
    210 			va.va_size = fxdr_unsigned(u_quad_t, sp->sa_nfssize);
    211 		if (sp->sa_nfsatime.nfs_sec != nfs_xdrneg1) {
    212 #ifdef notyet
    213 			fxdr_nfstime(&sp->sa_nfsatime, &va.va_atime);
    214 #else
    215 			va.va_atime.tv_sec =
    216 				fxdr_unsigned(int32_t, sp->sa_nfsatime.nfs_sec);
    217 			va.va_atime.tv_nsec = 0;
    218 #endif
    219 		}
    220 		if (sp->sa_nfsmtime.nfs_sec != nfs_xdrneg1)
    221 			fxdr_nfstime(&sp->sa_nfsmtime, &va.va_mtime);
    222 	} else {
    223 		fxdr_hyper(&sp->sa_nqsize, &va.va_size);
    224 		fxdr_nqtime(&sp->sa_nqatime, &va.va_atime);
    225 		fxdr_nqtime(&sp->sa_nqmtime, &va.va_mtime);
    226 		va.va_flags = fxdr_unsigned(u_int32_t, sp->sa_nqflags);
    227 	}
    228 
    229 	/*
    230 	 * If the size is being changed write acces is required, otherwise
    231 	 * just check for a read only file system.
    232 	 */
    233 	if (va.va_size == ((u_quad_t)((quad_t) -1))) {
    234 		if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
    235 			error = EROFS;
    236 			goto out;
    237 		}
    238 	} else {
    239 		if (vp->v_type == VDIR) {
    240 			error = EISDIR;
    241 			goto out;
    242 		} else if (error = nfsrv_access(vp, VWRITE, cred, rdonly,
    243 			nfsd->nd_procp))
    244 			goto out;
    245 	}
    246 	if (error = VOP_SETATTR(vp, &va, cred, nfsd->nd_procp)) {
    247 		vput(vp);
    248 		nfsm_reply(0);
    249 	}
    250 	error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp);
    251 out:
    252 	vput(vp);
    253 	nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL) + 2*NFSX_UNSIGNED);
    254 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    255 	nfsm_srvfillattr;
    256 	if (nfsd->nd_nqlflag != NQL_NOVAL) {
    257 		nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
    258 		txdr_hyper(&frev2, tl);
    259 	}
    260 	nfsm_srvdone;
    261 }
    262 
    263 /*
    264  * nfs lookup rpc
    265  */
    266 nfsrv_lookup(nfsd, mrep, md, dpos, cred, nam, mrq)
    267 	struct nfsd *nfsd;
    268 	struct mbuf *mrep, *md;
    269 	caddr_t dpos;
    270 	struct ucred *cred;
    271 	struct mbuf *nam, **mrq;
    272 {
    273 	register struct nfsv2_fattr *fp;
    274 	struct nameidata nd;
    275 	struct vnode *vp;
    276 	nfsv2fh_t nfh;
    277 	fhandle_t *fhp;
    278 	register caddr_t cp;
    279 	register u_int32_t *tl;
    280 	register int32_t t1;
    281 	caddr_t bpos;
    282 	int error = 0, cache, duration2, cache2, len;
    283 	char *cp2;
    284 	struct mbuf *mb, *mb2, *mreq;
    285 	struct vattr va;
    286 	u_quad_t frev, frev2;
    287 
    288 	fhp = &nfh.fh_generic;
    289 	duration2 = 0;
    290 	if (nfsd->nd_nqlflag != NQL_NOVAL) {
    291 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    292 		duration2 = fxdr_unsigned(int, *tl);
    293 	}
    294 	nfsm_srvmtofh(fhp);
    295 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    296 	nd.ni_cnd.cn_cred = cred;
    297 	nd.ni_cnd.cn_nameiop = LOOKUP;
    298 	nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
    299 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
    300 	    nfsd->nd_procp))
    301 		nfsm_reply(0);
    302 	nqsrv_getl(nd.ni_startdir, NQL_READ);
    303 	vrele(nd.ni_startdir);
    304 	FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    305 	vp = nd.ni_vp;
    306 	bzero((caddr_t)fhp, sizeof(nfh));
    307 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
    308 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
    309 		vput(vp);
    310 		nfsm_reply(0);
    311 	}
    312 	if (duration2)
    313 		(void) nqsrv_getlease(vp, &duration2, NQL_READ, nfsd,
    314 			nam, &cache2, &frev2, cred);
    315 	error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp);
    316 	vput(vp);
    317 	nfsm_reply(NFSX_FH + NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL) + 5*NFSX_UNSIGNED);
    318 	if (nfsd->nd_nqlflag != NQL_NOVAL) {
    319 		if (duration2) {
    320 			nfsm_build(tl, u_int32_t *, 5*NFSX_UNSIGNED);
    321 			*tl++ = txdr_unsigned(NQL_READ);
    322 			*tl++ = txdr_unsigned(cache2);
    323 			*tl++ = txdr_unsigned(duration2);
    324 			txdr_hyper(&frev2, tl);
    325 		} else {
    326 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    327 			*tl = 0;
    328 		}
    329 	}
    330 	nfsm_srvfhtom(fhp);
    331 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    332 	nfsm_srvfillattr;
    333 	nfsm_srvdone;
    334 }
    335 
    336 /*
    337  * nfs readlink service
    338  */
    339 nfsrv_readlink(nfsd, mrep, md, dpos, cred, nam, mrq)
    340 	struct nfsd *nfsd;
    341 	struct mbuf *mrep, *md;
    342 	caddr_t dpos;
    343 	struct ucred *cred;
    344 	struct mbuf *nam, **mrq;
    345 {
    346 	struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
    347 	register struct iovec *ivp = iv;
    348 	register struct mbuf *mp;
    349 	register u_int32_t *tl;
    350 	register int32_t t1;
    351 	caddr_t bpos;
    352 	int error = 0, rdonly, cache, i, tlen, len;
    353 	char *cp2;
    354 	struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
    355 	struct vnode *vp;
    356 	nfsv2fh_t nfh;
    357 	fhandle_t *fhp;
    358 	struct uio io, *uiop = &io;
    359 	u_quad_t frev;
    360 
    361 	fhp = &nfh.fh_generic;
    362 	nfsm_srvmtofh(fhp);
    363 	len = 0;
    364 	i = 0;
    365 	while (len < NFS_MAXPATHLEN) {
    366 		MGET(mp, M_WAIT, MT_DATA);
    367 		MCLGET(mp, M_WAIT);
    368 		mp->m_len = NFSMSIZ(mp);
    369 		if (len == 0)
    370 			mp3 = mp2 = mp;
    371 		else {
    372 			mp2->m_next = mp;
    373 			mp2 = mp;
    374 		}
    375 		if ((len+mp->m_len) > NFS_MAXPATHLEN) {
    376 			mp->m_len = NFS_MAXPATHLEN-len;
    377 			len = NFS_MAXPATHLEN;
    378 		} else
    379 			len += mp->m_len;
    380 		ivp->iov_base = mtod(mp, caddr_t);
    381 		ivp->iov_len = mp->m_len;
    382 		i++;
    383 		ivp++;
    384 	}
    385 	uiop->uio_iov = iv;
    386 	uiop->uio_iovcnt = i;
    387 	uiop->uio_offset = 0;
    388 	uiop->uio_resid = len;
    389 	uiop->uio_rw = UIO_READ;
    390 	uiop->uio_segflg = UIO_SYSSPACE;
    391 	uiop->uio_procp = (struct proc *)0;
    392 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly)) {
    393 		m_freem(mp3);
    394 		nfsm_reply(0);
    395 	}
    396 	if (vp->v_type != VLNK) {
    397 		error = EINVAL;
    398 		goto out;
    399 	}
    400 	nqsrv_getl(vp, NQL_READ);
    401 	error = VOP_READLINK(vp, uiop, cred);
    402 out:
    403 	vput(vp);
    404 	if (error)
    405 		m_freem(mp3);
    406 	nfsm_reply(NFSX_UNSIGNED);
    407 	if (uiop->uio_resid > 0) {
    408 		len -= uiop->uio_resid;
    409 		tlen = nfsm_rndup(len);
    410 		nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
    411 	}
    412 	nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    413 	*tl = txdr_unsigned(len);
    414 	mb->m_next = mp3;
    415 	nfsm_srvdone;
    416 }
    417 
    418 /*
    419  * nfs read service
    420  */
    421 nfsrv_read(nfsd, mrep, md, dpos, cred, nam, mrq)
    422 	struct nfsd *nfsd;
    423 	struct mbuf *mrep, *md;
    424 	caddr_t dpos;
    425 	struct ucred *cred;
    426 	struct mbuf *nam, **mrq;
    427 {
    428 	register struct iovec *iv;
    429 	struct iovec *iv2;
    430 	register struct mbuf *m;
    431 	register struct nfsv2_fattr *fp;
    432 	register u_int32_t *tl;
    433 	register int32_t t1;
    434 	caddr_t bpos;
    435 	int error = 0, rdonly, cache, i, cnt, len, left, siz, tlen;
    436 	char *cp2;
    437 	struct mbuf *mb, *mb2, *mreq;
    438 	struct mbuf *m2;
    439 	struct vnode *vp;
    440 	nfsv2fh_t nfh;
    441 	fhandle_t *fhp;
    442 	struct uio io, *uiop = &io;
    443 	struct vattr va;
    444 	off_t off;
    445 	u_quad_t frev;
    446 
    447 	fhp = &nfh.fh_generic;
    448 	nfsm_srvmtofh(fhp);
    449 	if (nfsd->nd_nqlflag == NQL_NOVAL) {
    450 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    451 		off = (off_t)fxdr_unsigned(u_int32_t, *tl);
    452 	} else {
    453 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    454 		fxdr_hyper(tl, &off);
    455 	}
    456 	nfsm_srvstrsiz(cnt, NFS_MAXDATA);
    457 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    458 		nfsm_reply(0);
    459 	if (vp->v_type != VREG) {
    460 		error = (vp->v_type == VDIR) ? EISDIR : EACCES;
    461 		vput(vp);
    462 		nfsm_reply(0);
    463 	}
    464 	nqsrv_getl(vp, NQL_READ);
    465 	if ((error = nfsrv_access(vp, VREAD, cred, rdonly, nfsd->nd_procp)) &&
    466 	    (error = nfsrv_access(vp, VEXEC, cred, rdonly, nfsd->nd_procp))) {
    467 		vput(vp);
    468 		nfsm_reply(0);
    469 	}
    470 	if (error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp)) {
    471 		vput(vp);
    472 		nfsm_reply(0);
    473 	}
    474 	if (off >= va.va_size)
    475 		cnt = 0;
    476 	else if ((off + cnt) > va.va_size)
    477 		cnt = nfsm_rndup(va.va_size - off);
    478 	nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL)+NFSX_UNSIGNED+nfsm_rndup(cnt));
    479 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    480 	nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    481 	len = left = cnt;
    482 	if (cnt > 0) {
    483 		/*
    484 		 * Generate the mbuf list with the uio_iov ref. to it.
    485 		 */
    486 		i = 0;
    487 		m = m2 = mb;
    488 		MALLOC(iv, struct iovec *,
    489 		       ((NFS_MAXDATA+MLEN-1)/MLEN) * sizeof (struct iovec),
    490 		       M_TEMP, M_WAITOK);
    491 		iv2 = iv;
    492 		while (left > 0) {
    493 			siz = min(M_TRAILINGSPACE(m), left);
    494 			if (siz > 0) {
    495 				m->m_len += siz;
    496 				iv->iov_base = bpos;
    497 				iv->iov_len = siz;
    498 				iv++;
    499 				i++;
    500 				left -= siz;
    501 			}
    502 			if (left > 0) {
    503 				MGET(m, M_WAIT, MT_DATA);
    504 				MCLGET(m, M_WAIT);
    505 				m->m_len = 0;
    506 				m2->m_next = m;
    507 				m2 = m;
    508 				bpos = mtod(m, caddr_t);
    509 			}
    510 		}
    511 		uiop->uio_iov = iv2;
    512 		uiop->uio_iovcnt = i;
    513 		uiop->uio_offset = off;
    514 		uiop->uio_resid = cnt;
    515 		uiop->uio_rw = UIO_READ;
    516 		uiop->uio_segflg = UIO_SYSSPACE;
    517 		error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
    518 		off = uiop->uio_offset;
    519 		FREE((caddr_t)iv2, M_TEMP);
    520 		if (error || (error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp))) {
    521 			m_freem(mreq);
    522 			vput(vp);
    523 			nfsm_reply(0);
    524 		}
    525 	} else
    526 		uiop->uio_resid = 0;
    527 	vput(vp);
    528 	nfsm_srvfillattr;
    529 	len -= uiop->uio_resid;
    530 	tlen = nfsm_rndup(len);
    531 	if (cnt != tlen || tlen != len)
    532 		nfsm_adj(mb, cnt-tlen, tlen-len);
    533 	*tl = txdr_unsigned(len);
    534 	nfsm_srvdone;
    535 }
    536 
    537 /*
    538  * nfs write service
    539  */
    540 nfsrv_write(nfsd, mrep, md, dpos, cred, nam, mrq)
    541 	struct nfsd *nfsd;
    542 	struct mbuf *mrep, *md;
    543 	caddr_t dpos;
    544 	struct ucred *cred;
    545 	struct mbuf *nam, **mrq;
    546 {
    547 	register struct iovec *ivp;
    548 	register struct mbuf *mp;
    549 	register struct nfsv2_fattr *fp;
    550 	struct iovec iv[NFS_MAXIOVEC];
    551 	struct vattr va;
    552 	register u_int32_t *tl;
    553 	register int32_t t1;
    554 	caddr_t bpos;
    555 	int error = 0, rdonly, cache, siz, len, xfer;
    556 	int ioflags = IO_SYNC | IO_NODELOCKED;
    557 	char *cp2;
    558 	struct mbuf *mb, *mb2, *mreq;
    559 	struct vnode *vp;
    560 	nfsv2fh_t nfh;
    561 	fhandle_t *fhp;
    562 	struct uio io, *uiop = &io;
    563 	off_t off;
    564 	u_quad_t frev;
    565 
    566 	fhp = &nfh.fh_generic;
    567 	nfsm_srvmtofh(fhp);
    568 	nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
    569 	if (nfsd->nd_nqlflag == NQL_NOVAL) {
    570 		off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
    571 		tl += 2;
    572 	} else {
    573 		fxdr_hyper(tl, &off);
    574 		tl += 2;
    575 		if (fxdr_unsigned(u_int32_t, *tl++))
    576 			ioflags |= IO_APPEND;
    577 	}
    578 	len = fxdr_unsigned(int32_t, *tl);
    579 	if (len > NFS_MAXDATA || len <= 0) {
    580 		error = EBADRPC;
    581 		nfsm_reply(0);
    582 	}
    583 	if (dpos == (mtod(md, caddr_t)+md->m_len)) {
    584 		mp = md->m_next;
    585 		if (mp == NULL) {
    586 			error = EBADRPC;
    587 			nfsm_reply(0);
    588 		}
    589 	} else {
    590 		mp = md;
    591 		siz = dpos-mtod(mp, caddr_t);
    592 		mp->m_len -= siz;
    593 		NFSMADV(mp, siz);
    594 	}
    595 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    596 		nfsm_reply(0);
    597 	if (vp->v_type != VREG) {
    598 		error = (vp->v_type == VDIR) ? EISDIR : EACCES;
    599 		vput(vp);
    600 		nfsm_reply(0);
    601 	}
    602 	nqsrv_getl(vp, NQL_WRITE);
    603 	if (error = nfsrv_access(vp, VWRITE, cred, rdonly, nfsd->nd_procp)) {
    604 		vput(vp);
    605 		nfsm_reply(0);
    606 	}
    607 	uiop->uio_resid = 0;
    608 	uiop->uio_rw = UIO_WRITE;
    609 	uiop->uio_segflg = UIO_SYSSPACE;
    610 	uiop->uio_procp = (struct proc *)0;
    611 	/*
    612 	 * Do up to NFS_MAXIOVEC mbufs of write each iteration of the
    613 	 * loop until done.
    614 	 */
    615 	while (len > 0 && uiop->uio_resid == 0) {
    616 		ivp = iv;
    617 		siz = 0;
    618 		uiop->uio_iov = ivp;
    619 		uiop->uio_iovcnt = 0;
    620 		uiop->uio_offset = off;
    621 		while (len > 0 && uiop->uio_iovcnt < NFS_MAXIOVEC && mp != NULL) {
    622 			ivp->iov_base = mtod(mp, caddr_t);
    623 			if (len < mp->m_len)
    624 				ivp->iov_len = xfer = len;
    625 			else
    626 				ivp->iov_len = xfer = mp->m_len;
    627 #ifdef notdef
    628 			/* Not Yet .. */
    629 			if (M_HASCL(mp) && (((u_long)ivp->iov_base) & CLOFSET) == 0)
    630 				ivp->iov_op = NULL;	/* what should it be ?? */
    631 			else
    632 				ivp->iov_op = NULL;
    633 #endif
    634 			uiop->uio_iovcnt++;
    635 			ivp++;
    636 			len -= xfer;
    637 			siz += xfer;
    638 			mp = mp->m_next;
    639 		}
    640 		if (len > 0 && mp == NULL) {
    641 			error = EBADRPC;
    642 			vput(vp);
    643 			nfsm_reply(0);
    644 		}
    645 		uiop->uio_resid = siz;
    646 		if (error = VOP_WRITE(vp, uiop, ioflags, cred)) {
    647 			vput(vp);
    648 			nfsm_reply(0);
    649 		}
    650 		off = uiop->uio_offset;
    651 	}
    652 	error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp);
    653 	vput(vp);
    654 	nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    655 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    656 	nfsm_srvfillattr;
    657 	if (nfsd->nd_nqlflag != NQL_NOVAL) {
    658 		nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
    659 		txdr_hyper(&va.va_filerev, tl);
    660 	}
    661 	nfsm_srvdone;
    662 }
    663 
    664 /*
    665  * nfs create service
    666  * now does a truncate to 0 length via. setattr if it already exists
    667  */
    668 nfsrv_create(nfsd, mrep, md, dpos, cred, nam, mrq)
    669 	struct nfsd *nfsd;
    670 	struct mbuf *mrep, *md;
    671 	caddr_t dpos;
    672 	struct ucred *cred;
    673 	struct mbuf *nam, **mrq;
    674 {
    675 	register struct nfsv2_fattr *fp;
    676 	struct vattr va;
    677 	register struct nfsv2_sattr *sp;
    678 	register u_int32_t *tl;
    679 	struct nameidata nd;
    680 	register caddr_t cp;
    681 	register int32_t t1;
    682 	caddr_t bpos;
    683 	int error = 0, rdev, cache, len, tsize;
    684 	char *cp2;
    685 	struct mbuf *mb, *mb2, *mreq;
    686 	struct vnode *vp;
    687 	nfsv2fh_t nfh;
    688 	fhandle_t *fhp;
    689 	u_quad_t frev;
    690 
    691 	nd.ni_cnd.cn_nameiop = 0;
    692 	fhp = &nfh.fh_generic;
    693 	nfsm_srvmtofh(fhp);
    694 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    695 	nd.ni_cnd.cn_cred = cred;
    696 	nd.ni_cnd.cn_nameiop = CREATE;
    697 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
    698 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
    699 	    nfsd->nd_procp))
    700 		nfsm_reply(0);
    701 	VATTR_NULL(&va);
    702 	nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_SATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    703 	/*
    704 	 * Iff doesn't exist, create it
    705 	 * otherwise just truncate to 0 length
    706 	 *   should I set the mode too ??
    707 	 */
    708 	if (nd.ni_vp == NULL) {
    709 		va.va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
    710 		if (va.va_type == VNON)
    711 			va.va_type = VREG;
    712 		va.va_mode = nfstov_mode(sp->sa_mode);
    713 		if (nfsd->nd_nqlflag == NQL_NOVAL)
    714 			rdev = fxdr_unsigned(int32_t, sp->sa_nfssize);
    715 		else
    716 			rdev = fxdr_unsigned(int32_t, sp->sa_nqrdev);
    717 		if (va.va_type == VREG || va.va_type == VSOCK) {
    718 			vrele(nd.ni_startdir);
    719 			nqsrv_getl(nd.ni_dvp, NQL_WRITE);
    720 			if (error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va))
    721 				nfsm_reply(0);
    722 			FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    723 		} else if (va.va_type == VCHR || va.va_type == VBLK ||
    724 			va.va_type == VFIFO) {
    725 			if (va.va_type == VCHR && rdev == 0xffffffff)
    726 				va.va_type = VFIFO;
    727 			if (va.va_type == VFIFO) {
    728 #ifndef FIFO
    729 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    730 				vput(nd.ni_dvp);
    731 				error = ENXIO;
    732 				goto out;
    733 #endif /* FIFO */
    734 			} else if (error = suser(cred, (u_short *)0)) {
    735 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    736 				vput(nd.ni_dvp);
    737 				goto out;
    738 			} else
    739 				va.va_rdev = (dev_t)rdev;
    740 			nqsrv_getl(nd.ni_dvp, NQL_WRITE);
    741 			if (error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va)) {
    742 				vrele(nd.ni_startdir);
    743 				nfsm_reply(0);
    744 			}
    745 			nd.ni_cnd.cn_nameiop = LOOKUP;
    746 			nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART);
    747 			nd.ni_cnd.cn_proc = nfsd->nd_procp;
    748 			nd.ni_cnd.cn_cred = nfsd->nd_procp->p_ucred;
    749 			if (error = lookup(&nd)) {
    750 				free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    751 				nfsm_reply(0);
    752 			}
    753 			FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    754 			if (nd.ni_cnd.cn_flags & ISSYMLINK) {
    755 				vrele(nd.ni_dvp);
    756 				vput(nd.ni_vp);
    757 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    758 				error = EINVAL;
    759 				nfsm_reply(0);
    760 			}
    761 		} else {
    762 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    763 			vput(nd.ni_dvp);
    764 			error = ENXIO;
    765 			goto out;
    766 		}
    767 		vp = nd.ni_vp;
    768 	} else {
    769 		vrele(nd.ni_startdir);
    770 		free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    771 		vp = nd.ni_vp;
    772 		if (nd.ni_dvp == vp)
    773 			vrele(nd.ni_dvp);
    774 		else
    775 			vput(nd.ni_dvp);
    776 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    777 		if (nfsd->nd_nqlflag == NQL_NOVAL) {
    778 			tsize = fxdr_unsigned(int32_t, sp->sa_nfssize);
    779 			if (tsize != -1)
    780 				va.va_size = (u_quad_t)tsize;
    781 			else
    782 				va.va_size = -1;
    783 		} else
    784 			fxdr_hyper(&sp->sa_nqsize, &va.va_size);
    785 		if (va.va_size != -1) {
    786 			if (error = nfsrv_access(vp, VWRITE, cred,
    787 			    (nd.ni_cnd.cn_flags & RDONLY), nfsd->nd_procp)) {
    788 				vput(vp);
    789 				nfsm_reply(0);
    790 			}
    791 			nqsrv_getl(vp, NQL_WRITE);
    792 			if (error = VOP_SETATTR(vp, &va, cred, nfsd->nd_procp)) {
    793 				vput(vp);
    794 				nfsm_reply(0);
    795 			}
    796 		}
    797 	}
    798 	bzero((caddr_t)fhp, sizeof(nfh));
    799 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
    800 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
    801 		vput(vp);
    802 		nfsm_reply(0);
    803 	}
    804 	error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp);
    805 	vput(vp);
    806 	nfsm_reply(NFSX_FH+NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    807 	nfsm_srvfhtom(fhp);
    808 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    809 	nfsm_srvfillattr;
    810 	return (error);
    811 nfsmout:
    812 	if (nd.ni_cnd.cn_nameiop || nd.ni_cnd.cn_flags)
    813 		vrele(nd.ni_startdir);
    814 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    815 	if (nd.ni_dvp == nd.ni_vp)
    816 		vrele(nd.ni_dvp);
    817 	else
    818 		vput(nd.ni_dvp);
    819 	if (nd.ni_vp)
    820 		vput(nd.ni_vp);
    821 	return (error);
    822 
    823 out:
    824 	vrele(nd.ni_startdir);
    825 	free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    826 	nfsm_reply(0);
    827 }
    828 
    829 /*
    830  * nfs remove service
    831  */
    832 nfsrv_remove(nfsd, mrep, md, dpos, cred, nam, mrq)
    833 	struct nfsd *nfsd;
    834 	struct mbuf *mrep, *md;
    835 	caddr_t dpos;
    836 	struct ucred *cred;
    837 	struct mbuf *nam, **mrq;
    838 {
    839 	struct nameidata nd;
    840 	register u_int32_t *tl;
    841 	register int32_t t1;
    842 	caddr_t bpos;
    843 	int error = 0, cache, len;
    844 	char *cp2;
    845 	struct mbuf *mb, *mreq;
    846 	struct vnode *vp;
    847 	nfsv2fh_t nfh;
    848 	fhandle_t *fhp;
    849 	u_quad_t frev;
    850 
    851 	fhp = &nfh.fh_generic;
    852 	nfsm_srvmtofh(fhp);
    853 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    854 	nd.ni_cnd.cn_cred = cred;
    855 	nd.ni_cnd.cn_nameiop = DELETE;
    856 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
    857 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
    858 	    nfsd->nd_procp))
    859 		nfsm_reply(0);
    860 	vp = nd.ni_vp;
    861 	if (vp->v_type == VDIR &&
    862 		(error = suser(cred, (u_short *)0)))
    863 		goto out;
    864 	/*
    865 	 * The root of a mounted filesystem cannot be deleted.
    866 	 */
    867 	if (vp->v_flag & VROOT) {
    868 		error = EBUSY;
    869 		goto out;
    870 	}
    871 	if (vp->v_flag & VTEXT)
    872 		(void) vnode_pager_uncache(vp);
    873 out:
    874 	if (!error) {
    875 		nqsrv_getl(nd.ni_dvp, NQL_WRITE);
    876 		nqsrv_getl(vp, NQL_WRITE);
    877 		error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
    878 	} else {
    879 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    880 		if (nd.ni_dvp == vp)
    881 			vrele(nd.ni_dvp);
    882 		else
    883 			vput(nd.ni_dvp);
    884 		vput(vp);
    885 	}
    886 	nfsm_reply(0);
    887 	nfsm_srvdone;
    888 }
    889 
    890 /*
    891  * nfs rename service
    892  */
    893 nfsrv_rename(nfsd, mrep, md, dpos, cred, nam, mrq)
    894 	struct nfsd *nfsd;
    895 	struct mbuf *mrep, *md;
    896 	caddr_t dpos;
    897 	struct ucred *cred;
    898 	struct mbuf *nam, **mrq;
    899 {
    900 	register u_int32_t *tl;
    901 	register int32_t t1;
    902 	caddr_t bpos;
    903 	int error = 0, cache, len, len2;
    904 	char *cp2;
    905 	struct mbuf *mb, *mreq;
    906 	struct nameidata fromnd, tond;
    907 	struct vnode *fvp, *tvp, *tdvp;
    908 	nfsv2fh_t fnfh, tnfh;
    909 	fhandle_t *ffhp, *tfhp;
    910 	u_quad_t frev;
    911 	uid_t saved_uid;
    912 
    913 	ffhp = &fnfh.fh_generic;
    914 	tfhp = &tnfh.fh_generic;
    915 	fromnd.ni_cnd.cn_nameiop = 0;
    916 	tond.ni_cnd.cn_nameiop = 0;
    917 	nfsm_srvmtofh(ffhp);
    918 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    919 	/*
    920 	 * Remember our original uid so that we can reset cr_uid before
    921 	 * the second nfs_namei() call, in case it is remapped.
    922 	 */
    923 	saved_uid = cred->cr_uid;
    924 	fromnd.ni_cnd.cn_cred = cred;
    925 	fromnd.ni_cnd.cn_nameiop = DELETE;
    926 	fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
    927 	if (error = nfs_namei(&fromnd, ffhp, len, nfsd->nd_slp, nam, &md,
    928 	    &dpos, nfsd->nd_procp))
    929 		nfsm_reply(0);
    930 	fvp = fromnd.ni_vp;
    931 	nfsm_srvmtofh(tfhp);
    932 	nfsm_strsiz(len2, NFS_MAXNAMLEN);
    933 	cred->cr_uid = saved_uid;
    934 	tond.ni_cnd.cn_cred = cred;
    935 	tond.ni_cnd.cn_nameiop = RENAME;
    936 	tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
    937 	if (error = nfs_namei(&tond, tfhp, len2, nfsd->nd_slp, nam, &md,
    938 	    &dpos, nfsd->nd_procp)) {
    939 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
    940 		vrele(fromnd.ni_dvp);
    941 		vrele(fvp);
    942 		goto out1;
    943 	}
    944 	tdvp = tond.ni_dvp;
    945 	tvp = tond.ni_vp;
    946 	if (tvp != NULL) {
    947 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
    948 			error = EISDIR;
    949 			goto out;
    950 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
    951 			error = ENOTDIR;
    952 			goto out;
    953 		}
    954 		if (tvp->v_type == VDIR && tvp->v_mountedhere) {
    955 			error = EXDEV;
    956 			goto out;
    957 		}
    958 	}
    959 	if (fvp->v_type == VDIR && fvp->v_mountedhere) {
    960 		error = EBUSY;
    961 		goto out;
    962 	}
    963 	if (fvp->v_mount != tdvp->v_mount) {
    964 		error = EXDEV;
    965 		goto out;
    966 	}
    967 	if (fvp == tdvp)
    968 		error = EINVAL;
    969 	/*
    970 	 * If source is the same as the destination (that is the
    971 	 * same vnode with the same name in the same directory),
    972 	 * then there is nothing to do.
    973 	 */
    974 	if (fvp == tvp && fromnd.ni_dvp == tdvp &&
    975 	    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
    976 	    !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
    977 	      fromnd.ni_cnd.cn_namelen))
    978 		error = -1;
    979 out:
    980 	if (!error) {
    981 		nqsrv_getl(fromnd.ni_dvp, NQL_WRITE);
    982 		nqsrv_getl(tdvp, NQL_WRITE);
    983 		if (tvp)
    984 			nqsrv_getl(tvp, NQL_WRITE);
    985 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
    986 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
    987 	} else {
    988 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
    989 		if (tdvp == tvp)
    990 			vrele(tdvp);
    991 		else
    992 			vput(tdvp);
    993 		if (tvp)
    994 			vput(tvp);
    995 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
    996 		vrele(fromnd.ni_dvp);
    997 		vrele(fvp);
    998 	}
    999 	vrele(tond.ni_startdir);
   1000 	FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   1001 out1:
   1002 	vrele(fromnd.ni_startdir);
   1003 	FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   1004 	nfsm_reply(0);
   1005 	return (error);
   1006 
   1007 nfsmout:
   1008 	if (tond.ni_cnd.cn_nameiop || tond.ni_cnd.cn_flags) {
   1009 		vrele(tond.ni_startdir);
   1010 		FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   1011 	}
   1012 	if (fromnd.ni_cnd.cn_nameiop || fromnd.ni_cnd.cn_flags) {
   1013 		vrele(fromnd.ni_startdir);
   1014 		FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   1015 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1016 		vrele(fromnd.ni_dvp);
   1017 		vrele(fvp);
   1018 	}
   1019 	return (error);
   1020 }
   1021 
   1022 /*
   1023  * nfs link service
   1024  */
   1025 nfsrv_link(nfsd, mrep, md, dpos, cred, nam, mrq)
   1026 	struct nfsd *nfsd;
   1027 	struct mbuf *mrep, *md;
   1028 	caddr_t dpos;
   1029 	struct ucred *cred;
   1030 	struct mbuf *nam, **mrq;
   1031 {
   1032 	struct nameidata nd;
   1033 	register u_int32_t *tl;
   1034 	register int32_t t1;
   1035 	caddr_t bpos;
   1036 	int error = 0, rdonly, cache, len;
   1037 	char *cp2;
   1038 	struct mbuf *mb, *mreq;
   1039 	struct vnode *vp, *xp;
   1040 	nfsv2fh_t nfh, dnfh;
   1041 	fhandle_t *fhp, *dfhp;
   1042 	u_quad_t frev;
   1043 
   1044 	fhp = &nfh.fh_generic;
   1045 	dfhp = &dnfh.fh_generic;
   1046 	nfsm_srvmtofh(fhp);
   1047 	nfsm_srvmtofh(dfhp);
   1048 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1049 	if (error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
   1050 		nfsm_reply(0);
   1051 	if (vp->v_type == VDIR && (error = suser(cred, (u_short *)0)))
   1052 		goto out1;
   1053 	nd.ni_cnd.cn_cred = cred;
   1054 	nd.ni_cnd.cn_nameiop = CREATE;
   1055 	nd.ni_cnd.cn_flags = LOCKPARENT;
   1056 	if (error = nfs_namei(&nd, dfhp, len, nfsd->nd_slp, nam, &md, &dpos,
   1057 	    nfsd->nd_procp))
   1058 		goto out1;
   1059 	xp = nd.ni_vp;
   1060 	if (xp != NULL) {
   1061 		error = EEXIST;
   1062 		goto out;
   1063 	}
   1064 	xp = nd.ni_dvp;
   1065 	if (vp->v_mount != xp->v_mount)
   1066 		error = EXDEV;
   1067 out:
   1068 	if (!error) {
   1069 		nqsrv_getl(vp, NQL_WRITE);
   1070 		nqsrv_getl(xp, NQL_WRITE);
   1071 		error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   1072 	} else {
   1073 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1074 		if (nd.ni_dvp == nd.ni_vp)
   1075 			vrele(nd.ni_dvp);
   1076 		else
   1077 			vput(nd.ni_dvp);
   1078 		if (nd.ni_vp)
   1079 			vrele(nd.ni_vp);
   1080 	}
   1081 out1:
   1082 	vrele(vp);
   1083 	nfsm_reply(0);
   1084 	nfsm_srvdone;
   1085 }
   1086 
   1087 /*
   1088  * nfs symbolic link service
   1089  */
   1090 nfsrv_symlink(nfsd, mrep, md, dpos, cred, nam, mrq)
   1091 	struct nfsd *nfsd;
   1092 	struct mbuf *mrep, *md;
   1093 	caddr_t dpos;
   1094 	struct ucred *cred;
   1095 	struct mbuf *nam, **mrq;
   1096 {
   1097 	struct vattr va;
   1098 	struct nameidata nd;
   1099 	register u_int32_t *tl;
   1100 	register int32_t t1;
   1101 	struct nfsv2_sattr *sp;
   1102 	caddr_t bpos;
   1103 	struct uio io;
   1104 	struct iovec iv;
   1105 	int error = 0, cache, len, len2;
   1106 	char *pathcp, *cp2;
   1107 	struct mbuf *mb, *mreq;
   1108 	nfsv2fh_t nfh;
   1109 	fhandle_t *fhp;
   1110 	u_quad_t frev;
   1111 
   1112 	pathcp = (char *)0;
   1113 	fhp = &nfh.fh_generic;
   1114 	nfsm_srvmtofh(fhp);
   1115 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1116 	nd.ni_cnd.cn_cred = cred;
   1117 	nd.ni_cnd.cn_nameiop = CREATE;
   1118 	nd.ni_cnd.cn_flags = LOCKPARENT;
   1119 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
   1120 	    nfsd->nd_procp))
   1121 		goto out;
   1122 	nfsm_strsiz(len2, NFS_MAXPATHLEN);
   1123 	MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
   1124 	iv.iov_base = pathcp;
   1125 	iv.iov_len = len2;
   1126 	io.uio_resid = len2;
   1127 	io.uio_offset = 0;
   1128 	io.uio_iov = &iv;
   1129 	io.uio_iovcnt = 1;
   1130 	io.uio_segflg = UIO_SYSSPACE;
   1131 	io.uio_rw = UIO_READ;
   1132 	io.uio_procp = (struct proc *)0;
   1133 	nfsm_mtouio(&io, len2);
   1134 	nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_SATTR(nfsd->nd_nqlflag != NQL_NOVAL));
   1135 	*(pathcp + len2) = '\0';
   1136 	if (nd.ni_vp) {
   1137 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1138 		if (nd.ni_dvp == nd.ni_vp)
   1139 			vrele(nd.ni_dvp);
   1140 		else
   1141 			vput(nd.ni_dvp);
   1142 		vrele(nd.ni_vp);
   1143 		error = EEXIST;
   1144 		goto out;
   1145 	}
   1146 	VATTR_NULL(&va);
   1147 	va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
   1148 	nqsrv_getl(nd.ni_dvp, NQL_WRITE);
   1149 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp);
   1150 out:
   1151 	if (pathcp)
   1152 		FREE(pathcp, M_TEMP);
   1153 	nfsm_reply(0);
   1154 	return (error);
   1155 nfsmout:
   1156 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1157 	if (nd.ni_dvp == nd.ni_vp)
   1158 		vrele(nd.ni_dvp);
   1159 	else
   1160 		vput(nd.ni_dvp);
   1161 	if (nd.ni_vp)
   1162 		vrele(nd.ni_vp);
   1163 	if (pathcp)
   1164 		FREE(pathcp, M_TEMP);
   1165 	return (error);
   1166 }
   1167 
   1168 /*
   1169  * nfs mkdir service
   1170  */
   1171 nfsrv_mkdir(nfsd, mrep, md, dpos, cred, nam, mrq)
   1172 	struct nfsd *nfsd;
   1173 	struct mbuf *mrep, *md;
   1174 	caddr_t dpos;
   1175 	struct ucred *cred;
   1176 	struct mbuf *nam, **mrq;
   1177 {
   1178 	struct vattr va;
   1179 	register struct nfsv2_fattr *fp;
   1180 	struct nameidata nd;
   1181 	register caddr_t cp;
   1182 	register u_int32_t *tl;
   1183 	register int32_t t1;
   1184 	caddr_t bpos;
   1185 	int error = 0, cache, len;
   1186 	char *cp2;
   1187 	struct mbuf *mb, *mb2, *mreq;
   1188 	struct vnode *vp;
   1189 	nfsv2fh_t nfh;
   1190 	fhandle_t *fhp;
   1191 	u_quad_t frev;
   1192 
   1193 	fhp = &nfh.fh_generic;
   1194 	nfsm_srvmtofh(fhp);
   1195 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1196 	nd.ni_cnd.cn_cred = cred;
   1197 	nd.ni_cnd.cn_nameiop = CREATE;
   1198 	nd.ni_cnd.cn_flags = LOCKPARENT;
   1199 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
   1200 	    nfsd->nd_procp))
   1201 		nfsm_reply(0);
   1202 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1203 	VATTR_NULL(&va);
   1204 	va.va_type = VDIR;
   1205 	va.va_mode = nfstov_mode(*tl++);
   1206 	vp = nd.ni_vp;
   1207 	if (vp != NULL) {
   1208 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1209 		if (nd.ni_dvp == vp)
   1210 			vrele(nd.ni_dvp);
   1211 		else
   1212 			vput(nd.ni_dvp);
   1213 		vrele(vp);
   1214 		error = EEXIST;
   1215 		nfsm_reply(0);
   1216 	}
   1217 	nqsrv_getl(nd.ni_dvp, NQL_WRITE);
   1218 	if (error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va))
   1219 		nfsm_reply(0);
   1220 	vp = nd.ni_vp;
   1221 	bzero((caddr_t)fhp, sizeof(nfh));
   1222 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
   1223 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
   1224 		vput(vp);
   1225 		nfsm_reply(0);
   1226 	}
   1227 	error = VOP_GETATTR(vp, &va, cred, nfsd->nd_procp);
   1228 	vput(vp);
   1229 	nfsm_reply(NFSX_FH+NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
   1230 	nfsm_srvfhtom(fhp);
   1231 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
   1232 	nfsm_srvfillattr;
   1233 	return (error);
   1234 nfsmout:
   1235 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1236 	if (nd.ni_dvp == nd.ni_vp)
   1237 		vrele(nd.ni_dvp);
   1238 	else
   1239 		vput(nd.ni_dvp);
   1240 	if (nd.ni_vp)
   1241 		vrele(nd.ni_vp);
   1242 	return (error);
   1243 }
   1244 
   1245 /*
   1246  * nfs rmdir service
   1247  */
   1248 nfsrv_rmdir(nfsd, mrep, md, dpos, cred, nam, mrq)
   1249 	struct nfsd *nfsd;
   1250 	struct mbuf *mrep, *md;
   1251 	caddr_t dpos;
   1252 	struct ucred *cred;
   1253 	struct mbuf *nam, **mrq;
   1254 {
   1255 	register u_int32_t *tl;
   1256 	register int32_t t1;
   1257 	caddr_t bpos;
   1258 	int error = 0, cache, len;
   1259 	char *cp2;
   1260 	struct mbuf *mb, *mreq;
   1261 	struct vnode *vp;
   1262 	nfsv2fh_t nfh;
   1263 	fhandle_t *fhp;
   1264 	struct nameidata nd;
   1265 	u_quad_t frev;
   1266 
   1267 	fhp = &nfh.fh_generic;
   1268 	nfsm_srvmtofh(fhp);
   1269 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1270 	nd.ni_cnd.cn_cred = cred;
   1271 	nd.ni_cnd.cn_nameiop = DELETE;
   1272 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
   1273 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
   1274 	    nfsd->nd_procp))
   1275 		nfsm_reply(0);
   1276 	vp = nd.ni_vp;
   1277 	if (vp->v_type != VDIR) {
   1278 		error = ENOTDIR;
   1279 		goto out;
   1280 	}
   1281 	/*
   1282 	 * No rmdir "." please.
   1283 	 */
   1284 	if (nd.ni_dvp == vp) {
   1285 		error = EINVAL;
   1286 		goto out;
   1287 	}
   1288 	/*
   1289 	 * The root of a mounted filesystem cannot be deleted.
   1290 	 */
   1291 	if (vp->v_flag & VROOT)
   1292 		error = EBUSY;
   1293 out:
   1294 	if (!error) {
   1295 		nqsrv_getl(nd.ni_dvp, NQL_WRITE);
   1296 		nqsrv_getl(vp, NQL_WRITE);
   1297 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1298 	} else {
   1299 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1300 		if (nd.ni_dvp == nd.ni_vp)
   1301 			vrele(nd.ni_dvp);
   1302 		else
   1303 			vput(nd.ni_dvp);
   1304 		vput(vp);
   1305 	}
   1306 	nfsm_reply(0);
   1307 	nfsm_srvdone;
   1308 }
   1309 
   1310 /*
   1311  * nfs readdir service
   1312  * - mallocs what it thinks is enough to read
   1313  *	count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
   1314  * - calls VOP_READDIR()
   1315  * - loops around building the reply
   1316  *	if the output generated exceeds count break out of loop
   1317  *	The nfsm_clget macro is used here so that the reply will be packed
   1318  *	tightly in mbuf clusters.
   1319  * - it only knows that it has encountered eof when the VOP_READDIR()
   1320  *	reads nothing
   1321  * - as such one readdir rpc will return eof false although you are there
   1322  *	and then the next will return eof
   1323  * - it trims out records with d_fileno == 0
   1324  *	this doesn't matter for Unix clients, but they might confuse clients
   1325  *	for other os'.
   1326  * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
   1327  *	than requested, but this may not apply to all filesystems. For
   1328  *	example, client NFS does not { although it is never remote mounted
   1329  *	anyhow }
   1330  *     The alternate call nqnfsrv_readdirlook() does lookups as well.
   1331  * PS: The NFS protocol spec. does not clarify what the "count" byte
   1332  *	argument is a count of.. just name strings and file id's or the
   1333  *	entire reply rpc or ...
   1334  *	I tried just file name and id sizes and it confused the Sun client,
   1335  *	so I am using the full rpc size now. The "paranoia.." comment refers
   1336  *	to including the status longwords that are not a part of the dir.
   1337  *	"entry" structures, but are in the rpc.
   1338  */
   1339 struct flrep {
   1340 	u_int32_t fl_cachable;
   1341 	u_int32_t fl_duration;
   1342 	u_int32_t fl_frev[2];
   1343 	nfsv2fh_t fl_nfh;
   1344 	u_int32_t fl_fattr[NFSX_NQFATTR / sizeof (u_int32_t)];
   1345 };
   1346 
   1347 nfsrv_readdir(nfsd, mrep, md, dpos, cred, nam, mrq)
   1348 	struct nfsd *nfsd;
   1349 	struct mbuf *mrep, *md;
   1350 	caddr_t dpos;
   1351 	struct ucred *cred;
   1352 	struct mbuf *nam, **mrq;
   1353 {
   1354 	register char *bp, *be;
   1355 	register struct mbuf *mp;
   1356 	register struct dirent *dp;
   1357 	register caddr_t cp;
   1358 	register u_int32_t *tl;
   1359 	register int32_t t1;
   1360 	caddr_t bpos;
   1361 	struct mbuf *mb, *mb2, *mreq, *mp2;
   1362 	char *cpos, *cend, *cp2, *rbuf;
   1363 	struct vnode *vp;
   1364 	nfsv2fh_t nfh;
   1365 	fhandle_t *fhp;
   1366 	struct uio io;
   1367 	struct iovec iv;
   1368 	int len, nlen, rem, xfer, tsiz, i, error = 0;
   1369 	int siz, cnt, fullsiz, eofflag, rdonly, cache;
   1370 	u_quad_t frev;
   1371 	u_long off, *cookiebuf, *cookie;
   1372 	int ncookies;
   1373 
   1374 	fhp = &nfh.fh_generic;
   1375 	nfsm_srvmtofh(fhp);
   1376 	nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
   1377 	off = fxdr_unsigned(u_int32_t, *tl++);
   1378 	cnt = fxdr_unsigned(int, *tl);
   1379 	siz = ((cnt+NFS_DIRBLKSIZ-1) & ~(NFS_DIRBLKSIZ-1));
   1380 	if (cnt > NFS_MAXREADDIR)
   1381 		siz = NFS_MAXREADDIR;
   1382 	fullsiz = siz;
   1383 	ncookies = siz / 16;	/* Guess at the number of cookies needed. */
   1384 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
   1385 		nfsm_reply(0);
   1386 	nqsrv_getl(vp, NQL_READ);
   1387 	if (error = nfsrv_access(vp, VEXEC, cred, rdonly, nfsd->nd_procp)) {
   1388 		vput(vp);
   1389 		nfsm_reply(0);
   1390 	}
   1391 	VOP_UNLOCK(vp);
   1392 	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
   1393 	MALLOC(cookiebuf, u_long *, ncookies * sizeof(*cookiebuf), M_TEMP,
   1394 	    M_WAITOK);
   1395 again:
   1396 	iv.iov_base = rbuf;
   1397 	iv.iov_len = fullsiz;
   1398 	io.uio_iov = &iv;
   1399 	io.uio_iovcnt = 1;
   1400 	io.uio_offset = (off_t)off;
   1401 	io.uio_resid = fullsiz;
   1402 	io.uio_segflg = UIO_SYSSPACE;
   1403 	io.uio_rw = UIO_READ;
   1404 	io.uio_procp = (struct proc *)0;
   1405 	error = VOP_READDIR(vp, &io, cred, &eofflag, cookiebuf, ncookies);
   1406 	cookie = cookiebuf;
   1407 	off = (off_t)io.uio_offset;
   1408 	if (error) {
   1409 		vrele(vp);
   1410 		free((caddr_t)cookiebuf, M_TEMP);
   1411 		free((caddr_t)rbuf, M_TEMP);
   1412 		nfsm_reply(0);
   1413 	}
   1414 	if (io.uio_resid) {
   1415 		siz -= io.uio_resid;
   1416 
   1417 		/*
   1418 		 * If nothing read, return eof
   1419 		 * rpc reply
   1420 		 */
   1421 		if (siz == 0) {
   1422 			vrele(vp);
   1423 			nfsm_reply(2*NFSX_UNSIGNED);
   1424 			nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
   1425 			*tl++ = nfs_false;
   1426 			*tl = nfs_true;
   1427 			FREE((caddr_t)cookiebuf, M_TEMP);
   1428 			FREE((caddr_t)rbuf, M_TEMP);
   1429 			return (0);
   1430 		}
   1431 	}
   1432 
   1433 	/*
   1434 	 * Check for degenerate cases of nothing useful read.
   1435 	 * If so go try again
   1436 	 */
   1437 	cpos = rbuf;
   1438 	cend = rbuf + siz;
   1439 	while (cpos < cend) {
   1440 		dp = (struct dirent *)cpos;
   1441 		if (dp->d_fileno == 0) {
   1442 			cpos += dp->d_reclen;
   1443 			cookie++;
   1444 		} else
   1445 			break;
   1446 	}
   1447 	if (cpos >= cend) {
   1448 		siz = fullsiz;
   1449 		goto again;
   1450 	}
   1451 
   1452 	len = 3*NFSX_UNSIGNED;	/* paranoia, probably can be 0 */
   1453 	nfsm_reply(siz);
   1454 	mp = mp2 = mb;
   1455 	bp = bpos;
   1456 	be = bp + M_TRAILINGSPACE(mp);
   1457 
   1458 	/* Loop through the records and build reply */
   1459 	while (cpos < cend) {
   1460 		if (dp->d_fileno != 0) {
   1461 			nlen = dp->d_namlen;
   1462 			rem = nfsm_rndup(nlen)-nlen;
   1463 			len += (4*NFSX_UNSIGNED + nlen + rem);
   1464 			if (len > cnt) {
   1465 				eofflag = 0;
   1466 				break;
   1467 			}
   1468 			/*
   1469 			 * Build the directory record xdr from
   1470 			 * the dirent entry.
   1471 			 */
   1472 			nfsm_clget;
   1473 			*tl = nfs_true;
   1474 			bp += NFSX_UNSIGNED;
   1475 			nfsm_clget;
   1476 			*tl = txdr_unsigned(dp->d_fileno);
   1477 			bp += NFSX_UNSIGNED;
   1478 			nfsm_clget;
   1479 			*tl = txdr_unsigned(nlen);
   1480 			bp += NFSX_UNSIGNED;
   1481 
   1482 			/* And loop around copying the name */
   1483 			xfer = nlen;
   1484 			cp = dp->d_name;
   1485 			while (xfer > 0) {
   1486 				nfsm_clget;
   1487 				if ((bp+xfer) > be)
   1488 					tsiz = be-bp;
   1489 				else
   1490 					tsiz = xfer;
   1491 				bcopy(cp, bp, tsiz);
   1492 				bp += tsiz;
   1493 				xfer -= tsiz;
   1494 				if (xfer > 0)
   1495 					cp += tsiz;
   1496 			}
   1497 			/* And null pad to a int32_t boundary */
   1498 			for (i = 0; i < rem; i++)
   1499 				*bp++ = '\0';
   1500 			nfsm_clget;
   1501 
   1502 			/* Finish off the record */
   1503 			*tl = txdr_unsigned(*cookie);
   1504 			bp += NFSX_UNSIGNED;
   1505 		}
   1506 		cpos += dp->d_reclen;
   1507 		dp = (struct dirent *)cpos;
   1508 		cookie++;
   1509 	}
   1510 	vrele(vp);
   1511 	nfsm_clget;
   1512 	*tl = nfs_false;
   1513 	bp += NFSX_UNSIGNED;
   1514 	nfsm_clget;
   1515 	if (eofflag)
   1516 		*tl = nfs_true;
   1517 	else
   1518 		*tl = nfs_false;
   1519 	bp += NFSX_UNSIGNED;
   1520 	if (mp != mb) {
   1521 		if (bp < be)
   1522 			mp->m_len = bp - mtod(mp, caddr_t);
   1523 	} else
   1524 		mp->m_len += bp - bpos;
   1525 	FREE(cookiebuf, M_TEMP);
   1526 	FREE(rbuf, M_TEMP);
   1527 	nfsm_srvdone;
   1528 }
   1529 
   1530 nqnfsrv_readdirlook(nfsd, mrep, md, dpos, cred, nam, mrq)
   1531 	struct nfsd *nfsd;
   1532 	struct mbuf *mrep, *md;
   1533 	caddr_t dpos;
   1534 	struct ucred *cred;
   1535 	struct mbuf *nam, **mrq;
   1536 {
   1537 	register char *bp, *be;
   1538 	register struct mbuf *mp;
   1539 	register struct dirent *dp;
   1540 	register caddr_t cp;
   1541 	register u_int32_t *tl;
   1542 	register int32_t t1;
   1543 	caddr_t bpos;
   1544 	struct mbuf *mb, *mb2, *mreq, *mp2;
   1545 	char *cpos, *cend, *cp2, *rbuf;
   1546 	struct vnode *vp, *nvp;
   1547 	struct flrep fl;
   1548 	nfsv2fh_t nfh;
   1549 	fhandle_t *fhp;
   1550 	struct uio io;
   1551 	struct iovec iv;
   1552 	struct vattr va;
   1553 	struct nfsv2_fattr *fp;
   1554 	int len, nlen, rem, xfer, tsiz, i, error = 0, duration2, cache2;
   1555 	int siz, cnt, fullsiz, eofflag, rdonly, cache;
   1556 	u_quad_t frev, frev2;
   1557 	u_long off, *cookiebuf, *cookie;
   1558 	int ncookies;
   1559 
   1560 	fhp = &nfh.fh_generic;
   1561 	nfsm_srvmtofh(fhp);
   1562 	nfsm_dissect(tl, u_int32_t *, 3*NFSX_UNSIGNED);
   1563 	off = fxdr_unsigned(u_int32_t, *tl++);
   1564 	cnt = fxdr_unsigned(int, *tl++);
   1565 	duration2 = fxdr_unsigned(int, *tl);
   1566 	siz = ((cnt+NFS_DIRBLKSIZ-1) & ~(NFS_DIRBLKSIZ-1));
   1567 	if (cnt > NFS_MAXREADDIR)
   1568 		siz = NFS_MAXREADDIR;
   1569 	fullsiz = siz;
   1570 	ncookies = siz / 16;	/* Guess at the number of cookies needed. */
   1571 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
   1572 		nfsm_reply(0);
   1573 	nqsrv_getl(vp, NQL_READ);
   1574 	if (error = nfsrv_access(vp, VEXEC, cred, rdonly, nfsd->nd_procp)) {
   1575 		vput(vp);
   1576 		nfsm_reply(0);
   1577 	}
   1578 	VOP_UNLOCK(vp);
   1579 	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
   1580 	MALLOC(cookiebuf, u_long *, ncookies * sizeof(*cookiebuf), M_TEMP,
   1581 	    M_WAITOK);
   1582 again:
   1583 	iv.iov_base = rbuf;
   1584 	iv.iov_len = fullsiz;
   1585 	io.uio_iov = &iv;
   1586 	io.uio_iovcnt = 1;
   1587 	io.uio_offset = (off_t)off;
   1588 	io.uio_resid = fullsiz;
   1589 	io.uio_segflg = UIO_SYSSPACE;
   1590 	io.uio_rw = UIO_READ;
   1591 	io.uio_procp = (struct proc *)0;
   1592 	error = VOP_READDIR(vp, &io, cred, &eofflag, cookiebuf, ncookies);
   1593 	cookie = cookiebuf;
   1594 	off = (u_long)io.uio_offset;
   1595 	if (error) {
   1596 		vrele(vp);
   1597 		free((caddr_t)cookiebuf, M_TEMP);
   1598 		free((caddr_t)rbuf, M_TEMP);
   1599 		nfsm_reply(0);
   1600 	}
   1601 	if (io.uio_resid) {
   1602 		siz -= io.uio_resid;
   1603 
   1604 		/*
   1605 		 * If nothing read, return eof
   1606 		 * rpc reply
   1607 		 */
   1608 		if (siz == 0) {
   1609 			vrele(vp);
   1610 			nfsm_reply(2 * NFSX_UNSIGNED);
   1611 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1612 			*tl++ = nfs_false;
   1613 			*tl = nfs_true;
   1614 			FREE((caddr_t)cookiebuf, M_TEMP);
   1615 			FREE((caddr_t)rbuf, M_TEMP);
   1616 			return (0);
   1617 		}
   1618 	}
   1619 
   1620 	/*
   1621 	 * Check for degenerate cases of nothing useful read.
   1622 	 * If so go try again
   1623 	 */
   1624 	cpos = rbuf;
   1625 	cend = rbuf + siz;
   1626 	while (cpos < cend) {
   1627 		dp = (struct dirent *)cpos;
   1628 		if (dp->d_fileno == 0) {
   1629 			cpos += dp->d_reclen;
   1630 			cookie++;
   1631 		} else
   1632 			break;
   1633 	}
   1634 	if (cpos >= cend) {
   1635 		siz = fullsiz;
   1636 		goto again;
   1637 	}
   1638 
   1639 	len = 3 * NFSX_UNSIGNED;	/* paranoia, probably can be 0 */
   1640 	nfsm_reply(siz);
   1641 	mp = mp2 = mb;
   1642 	bp = bpos;
   1643 	be = bp + M_TRAILINGSPACE(mp);
   1644 
   1645 	/* Loop through the records and build reply */
   1646 	while (cpos < cend) {
   1647 		if (dp->d_fileno != 0) {
   1648 			nlen = dp->d_namlen;
   1649 			rem = nfsm_rndup(nlen)-nlen;
   1650 
   1651 			/*
   1652 			 * For readdir_and_lookup get the vnode using
   1653 			 * the file number.
   1654 			 */
   1655 			if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
   1656 				goto invalid;
   1657 			bzero((caddr_t)&fl.fl_nfh, sizeof (nfsv2fh_t));
   1658 			fl.fl_nfh.fh_generic.fh_fsid =
   1659 				nvp->v_mount->mnt_stat.f_fsid;
   1660 			if (VFS_VPTOFH(nvp, &fl.fl_nfh.fh_generic.fh_fid)) {
   1661 				vput(nvp);
   1662 				goto invalid;
   1663 			}
   1664 			if (duration2) {
   1665 				(void) nqsrv_getlease(nvp, &duration2, NQL_READ,
   1666 					nfsd, nam, &cache2, &frev2, cred);
   1667 				fl.fl_duration = txdr_unsigned(duration2);
   1668 				fl.fl_cachable = txdr_unsigned(cache2);
   1669 				txdr_hyper(&frev2, fl.fl_frev);
   1670 			} else
   1671 				fl.fl_duration = 0;
   1672 			if (VOP_GETATTR(nvp, &va, cred, nfsd->nd_procp)) {
   1673 				vput(nvp);
   1674 				goto invalid;
   1675 			}
   1676 			vput(nvp);
   1677 			fp = (struct nfsv2_fattr *)&fl.fl_fattr;
   1678 			nfsm_srvfillattr;
   1679 			len += (4*NFSX_UNSIGNED + nlen + rem + NFSX_FH
   1680 				+ NFSX_NQFATTR);
   1681 			if (len > cnt) {
   1682 				eofflag = 0;
   1683 				break;
   1684 			}
   1685 			/*
   1686 			 * Build the directory record xdr from
   1687 			 * the dirent entry.
   1688 			 */
   1689 			nfsm_clget;
   1690 			*tl = nfs_true;
   1691 			bp += NFSX_UNSIGNED;
   1692 
   1693 			/*
   1694 			 * For readdir_and_lookup copy the stuff out.
   1695 			 */
   1696 			xfer = sizeof (struct flrep);
   1697 			cp = (caddr_t)&fl;
   1698 			while (xfer > 0) {
   1699 				nfsm_clget;
   1700 				if ((bp+xfer) > be)
   1701 					tsiz = be-bp;
   1702 				else
   1703 					tsiz = xfer;
   1704 				bcopy(cp, bp, tsiz);
   1705 				bp += tsiz;
   1706 				xfer -= tsiz;
   1707 				if (xfer > 0)
   1708 					cp += tsiz;
   1709 			}
   1710 			nfsm_clget;
   1711 			*tl = txdr_unsigned(dp->d_fileno);
   1712 			bp += NFSX_UNSIGNED;
   1713 			nfsm_clget;
   1714 			*tl = txdr_unsigned(nlen);
   1715 			bp += NFSX_UNSIGNED;
   1716 
   1717 			/* And loop around copying the name */
   1718 			xfer = nlen;
   1719 			cp = dp->d_name;
   1720 			while (xfer > 0) {
   1721 				nfsm_clget;
   1722 				if ((bp+xfer) > be)
   1723 					tsiz = be-bp;
   1724 				else
   1725 					tsiz = xfer;
   1726 				bcopy(cp, bp, tsiz);
   1727 				bp += tsiz;
   1728 				xfer -= tsiz;
   1729 				if (xfer > 0)
   1730 					cp += tsiz;
   1731 			}
   1732 			/* And null pad to a int32_t boundary */
   1733 			for (i = 0; i < rem; i++)
   1734 				*bp++ = '\0';
   1735 			nfsm_clget;
   1736 
   1737 			/* Finish off the record */
   1738 			*tl = txdr_unsigned(*cookie);
   1739 			bp += NFSX_UNSIGNED;
   1740 		}
   1741 invalid:
   1742 		cpos += dp->d_reclen;
   1743 		dp = (struct dirent *)cpos;
   1744 		cookie++;
   1745 	}
   1746 	vrele(vp);
   1747 	nfsm_clget;
   1748 	*tl = nfs_false;
   1749 	bp += NFSX_UNSIGNED;
   1750 	nfsm_clget;
   1751 	if (eofflag)
   1752 		*tl = nfs_true;
   1753 	else
   1754 		*tl = nfs_false;
   1755 	bp += NFSX_UNSIGNED;
   1756 	if (mp != mb) {
   1757 		if (bp < be)
   1758 			mp->m_len = bp - mtod(mp, caddr_t);
   1759 	} else
   1760 		mp->m_len += bp - bpos;
   1761 	FREE(cookiebuf, M_TEMP);
   1762 	FREE(rbuf, M_TEMP);
   1763 	nfsm_srvdone;
   1764 }
   1765 
   1766 /*
   1767  * nfs statfs service
   1768  */
   1769 nfsrv_statfs(nfsd, mrep, md, dpos, cred, nam, mrq)
   1770 	struct nfsd *nfsd;
   1771 	struct mbuf *mrep, *md;
   1772 	caddr_t dpos;
   1773 	struct ucred *cred;
   1774 	struct mbuf *nam, **mrq;
   1775 {
   1776 	register struct statfs *sf;
   1777 	register struct nfsv2_statfs *sfp;
   1778 	register u_int32_t *tl;
   1779 	register int32_t t1;
   1780 	caddr_t bpos;
   1781 	int error = 0, rdonly, cache, isnq;
   1782 	char *cp2;
   1783 	struct mbuf *mb, *mb2, *mreq;
   1784 	struct vnode *vp;
   1785 	nfsv2fh_t nfh;
   1786 	fhandle_t *fhp;
   1787 	struct statfs statfs;
   1788 	u_quad_t frev;
   1789 
   1790 	fhp = &nfh.fh_generic;
   1791 	isnq = (nfsd->nd_nqlflag != NQL_NOVAL);
   1792 	nfsm_srvmtofh(fhp);
   1793 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
   1794 		nfsm_reply(0);
   1795 	sf = &statfs;
   1796 	error = VFS_STATFS(vp->v_mount, sf, nfsd->nd_procp);
   1797 	vput(vp);
   1798 	nfsm_reply(NFSX_STATFS(isnq));
   1799 	nfsm_build(sfp, struct nfsv2_statfs *, NFSX_STATFS(isnq));
   1800 	sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
   1801 	sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
   1802 	sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
   1803 	sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
   1804 	sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
   1805 	if (isnq) {
   1806 		sfp->sf_files = txdr_unsigned(sf->f_files);
   1807 		sfp->sf_ffree = txdr_unsigned(sf->f_ffree);
   1808 	}
   1809 	nfsm_srvdone;
   1810 }
   1811 
   1812 /*
   1813  * Null operation, used by clients to ping server
   1814  */
   1815 /* ARGSUSED */
   1816 nfsrv_null(nfsd, mrep, md, dpos, cred, nam, mrq)
   1817 	struct nfsd *nfsd;
   1818 	struct mbuf *mrep, *md;
   1819 	caddr_t dpos;
   1820 	struct ucred *cred;
   1821 	struct mbuf *nam, **mrq;
   1822 {
   1823 	caddr_t bpos;
   1824 	int error = VNOVAL, cache;
   1825 	struct mbuf *mb, *mreq;
   1826 	u_quad_t frev;
   1827 
   1828 	nfsm_reply(0);
   1829 	return (error);
   1830 }
   1831 
   1832 /*
   1833  * No operation, used for obsolete procedures
   1834  */
   1835 /* ARGSUSED */
   1836 nfsrv_noop(nfsd, mrep, md, dpos, cred, nam, mrq)
   1837 	struct nfsd *nfsd;
   1838 	struct mbuf *mrep, *md;
   1839 	caddr_t dpos;
   1840 	struct ucred *cred;
   1841 	struct mbuf *nam, **mrq;
   1842 {
   1843 	caddr_t bpos;
   1844 	int error, cache;
   1845 	struct mbuf *mb, *mreq;
   1846 	u_quad_t frev;
   1847 
   1848 	if (nfsd->nd_repstat)
   1849 		error = nfsd->nd_repstat;
   1850 	else
   1851 		error = EPROCUNAVAIL;
   1852 	nfsm_reply(0);
   1853 	return (error);
   1854 }
   1855 
   1856 /*
   1857  * Perform access checking for vnodes obtained from file handles that would
   1858  * refer to files already opened by a Unix client. You cannot just use
   1859  * vn_writechk() and VOP_ACCESS() for two reasons.
   1860  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
   1861  * 2 - The owner is to be given access irrespective of mode bits so that
   1862  *     processes that chmod after opening a file don't break. I don't like
   1863  *     this because it opens a security hole, but since the nfs server opens
   1864  *     a security hole the size of a barn door anyhow, what the heck.
   1865  */
   1866 nfsrv_access(vp, flags, cred, rdonly, p)
   1867 	register struct vnode *vp;
   1868 	int flags;
   1869 	register struct ucred *cred;
   1870 	int rdonly;
   1871 	struct proc *p;
   1872 {
   1873 	struct vattr vattr;
   1874 	int error;
   1875 	if (flags & VWRITE) {
   1876 		/* Just vn_writechk() changed to check rdonly */
   1877 		/*
   1878 		 * Disallow write attempts on read-only file systems;
   1879 		 * unless the file is a socket or a block or character
   1880 		 * device resident on the file system.
   1881 		 */
   1882 		if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
   1883 			switch (vp->v_type) {
   1884 			case VREG: case VDIR: case VLNK:
   1885 				return (EROFS);
   1886 			}
   1887 		}
   1888 		/*
   1889 		 * If there's shared text associated with
   1890 		 * the inode, try to free it up once.  If
   1891 		 * we fail, we can't allow writing.
   1892 		 */
   1893 		if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
   1894 			return (ETXTBSY);
   1895 	}
   1896 	if (error = VOP_GETATTR(vp, &vattr, cred, p))
   1897 		return (error);
   1898 	if ((error = VOP_ACCESS(vp, flags, cred, p)) &&
   1899 	    cred->cr_uid != vattr.va_uid)
   1900 		return (error);
   1901 	return (0);
   1902 }
   1903