Home | History | Annotate | Line # | Download | only in nfs
nfs_serv.c revision 1.1.1.2
      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.3 (Berkeley) 1/12/94
     37  */
     38 
     39 /*
     40  * nfs version 2 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  */
     56 
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/proc.h>
     60 #include <sys/file.h>
     61 #include <sys/namei.h>
     62 #include <sys/vnode.h>
     63 #include <sys/mount.h>
     64 #include <sys/mbuf.h>
     65 #include <sys/dirent.h>
     66 #include <sys/stat.h>
     67 
     68 #include <vm/vm.h>
     69 
     70 #include <nfs/nfsv2.h>
     71 #include <nfs/rpcv2.h>
     72 #include <nfs/nfs.h>
     73 #include <nfs/xdr_subs.h>
     74 #include <nfs/nfsm_subs.h>
     75 #include <nfs/nqnfs.h>
     76 
     77 /* Defs */
     78 #define	TRUE	1
     79 #define	FALSE	0
     80 
     81 /* Global vars */
     82 extern u_long nfs_procids[NFS_NPROCS];
     83 extern u_long nfs_xdrneg1;
     84 extern u_long nfs_false, nfs_true;
     85 nfstype nfs_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
     86 		      NFCHR, NFNON };
     87 
     88 /*
     89  * nqnfs access service
     90  */
     91 nqnfsrv_access(nfsd, mrep, md, dpos, cred, nam, mrq)
     92 	struct nfsd *nfsd;
     93 	struct mbuf *mrep, *md;
     94 	caddr_t dpos;
     95 	struct ucred *cred;
     96 	struct mbuf *nam, **mrq;
     97 {
     98 	struct vnode *vp;
     99 	nfsv2fh_t nfh;
    100 	fhandle_t *fhp;
    101 	register u_long *tl;
    102 	register long t1;
    103 	caddr_t bpos;
    104 	int error = 0, rdonly, cache, mode = 0;
    105 	char *cp2;
    106 	struct mbuf *mb, *mreq;
    107 	u_quad_t frev;
    108 
    109 	fhp = &nfh.fh_generic;
    110 	nfsm_srvmtofh(fhp);
    111 	nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
    112 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    113 		nfsm_reply(0);
    114 	if (*tl++ == nfs_true)
    115 		mode |= VREAD;
    116 	if (*tl++ == nfs_true)
    117 		mode |= VWRITE;
    118 	if (*tl == nfs_true)
    119 		mode |= VEXEC;
    120 	error = nfsrv_access(vp, mode, cred, rdonly, nfsd->nd_procp);
    121 	vput(vp);
    122 	nfsm_reply(0);
    123 	nfsm_srvdone;
    124 }
    125 
    126 /*
    127  * nfs getattr service
    128  */
    129 nfsrv_getattr(nfsd, mrep, md, dpos, cred, nam, mrq)
    130 	struct nfsd *nfsd;
    131 	struct mbuf *mrep, *md;
    132 	caddr_t dpos;
    133 	struct ucred *cred;
    134 	struct mbuf *nam, **mrq;
    135 {
    136 	register struct nfsv2_fattr *fp;
    137 	struct vattr va;
    138 	register struct vattr *vap = &va;
    139 	struct vnode *vp;
    140 	nfsv2fh_t nfh;
    141 	fhandle_t *fhp;
    142 	register u_long *tl;
    143 	register long t1;
    144 	caddr_t bpos;
    145 	int error = 0, rdonly, cache;
    146 	char *cp2;
    147 	struct mbuf *mb, *mb2, *mreq;
    148 	u_quad_t frev;
    149 
    150 	fhp = &nfh.fh_generic;
    151 	nfsm_srvmtofh(fhp);
    152 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    153 		nfsm_reply(0);
    154 	nqsrv_getl(vp, NQL_READ);
    155 	error = VOP_GETATTR(vp, vap, cred, nfsd->nd_procp);
    156 	vput(vp);
    157 	nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    158 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    159 	nfsm_srvfillattr;
    160 	nfsm_srvdone;
    161 }
    162 
    163 /*
    164  * nfs setattr service
    165  */
    166 nfsrv_setattr(nfsd, mrep, md, dpos, cred, nam, mrq)
    167 	struct nfsd *nfsd;
    168 	struct mbuf *mrep, *md;
    169 	caddr_t dpos;
    170 	struct ucred *cred;
    171 	struct mbuf *nam, **mrq;
    172 {
    173 	struct vattr va;
    174 	register struct vattr *vap = &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_long *tl;
    181 	register long 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(vap);
    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 		vap->va_mode = nfstov_mode(sp->sa_mode);
    204 	if (sp->sa_uid != nfs_xdrneg1)
    205 		vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
    206 	if (sp->sa_gid != nfs_xdrneg1)
    207 		vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
    208 	if (nfsd->nd_nqlflag == NQL_NOVAL) {
    209 		if (sp->sa_nfssize != nfs_xdrneg1)
    210 			vap->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, &vap->va_atime);
    214 #else
    215 			vap->va_atime.ts_sec =
    216 				fxdr_unsigned(long, sp->sa_nfsatime.nfs_sec);
    217 			vap->va_atime.ts_nsec = 0;
    218 #endif
    219 		}
    220 		if (sp->sa_nfsmtime.nfs_sec != nfs_xdrneg1)
    221 			fxdr_nfstime(&sp->sa_nfsmtime, &vap->va_mtime);
    222 	} else {
    223 		fxdr_hyper(&sp->sa_nqsize, &vap->va_size);
    224 		fxdr_nqtime(&sp->sa_nqatime, &vap->va_atime);
    225 		fxdr_nqtime(&sp->sa_nqmtime, &vap->va_mtime);
    226 		vap->va_flags = fxdr_unsigned(u_long, 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 (vap->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, vap, cred, nfsd->nd_procp)) {
    247 		vput(vp);
    248 		nfsm_reply(0);
    249 	}
    250 	error = VOP_GETATTR(vp, vap, 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_long *, 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_long *tl;
    280 	register long 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, *vap = &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_long *, 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, vap, 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_long *, 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_long *, 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_long *tl;
    350 	register long 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_long *, 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_long *tl;
    433 	register long 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, *vap = &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_long *, NFSX_UNSIGNED);
    451 		off = (off_t)fxdr_unsigned(u_long, *tl);
    452 	} else {
    453 		nfsm_dissect(tl, u_long *, 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, vap, cred, nfsd->nd_procp)) {
    471 		vput(vp);
    472 		nfsm_reply(0);
    473 	}
    474 	if (off >= vap->va_size)
    475 		cnt = 0;
    476 	else if ((off + cnt) > vap->va_size)
    477 		cnt = nfsm_rndup(vap->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_long *, 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, vap, 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 struct vattr *vap = &va;
    553 	register u_long *tl;
    554 	register long t1;
    555 	caddr_t bpos;
    556 	int error = 0, rdonly, cache, siz, len, xfer;
    557 	int ioflags = IO_SYNC | IO_NODELOCKED;
    558 	char *cp2;
    559 	struct mbuf *mb, *mb2, *mreq;
    560 	struct vnode *vp;
    561 	nfsv2fh_t nfh;
    562 	fhandle_t *fhp;
    563 	struct uio io, *uiop = &io;
    564 	off_t off;
    565 	u_quad_t frev;
    566 
    567 	fhp = &nfh.fh_generic;
    568 	nfsm_srvmtofh(fhp);
    569 	nfsm_dissect(tl, u_long *, 4 * NFSX_UNSIGNED);
    570 	if (nfsd->nd_nqlflag == NQL_NOVAL) {
    571 		off = (off_t)fxdr_unsigned(u_long, *++tl);
    572 		tl += 2;
    573 	} else {
    574 		fxdr_hyper(tl, &off);
    575 		tl += 2;
    576 		if (fxdr_unsigned(u_long, *tl++))
    577 			ioflags |= IO_APPEND;
    578 	}
    579 	len = fxdr_unsigned(long, *tl);
    580 	if (len > NFS_MAXDATA || len <= 0) {
    581 		error = EBADRPC;
    582 		nfsm_reply(0);
    583 	}
    584 	if (dpos == (mtod(md, caddr_t)+md->m_len)) {
    585 		mp = md->m_next;
    586 		if (mp == NULL) {
    587 			error = EBADRPC;
    588 			nfsm_reply(0);
    589 		}
    590 	} else {
    591 		mp = md;
    592 		siz = dpos-mtod(mp, caddr_t);
    593 		mp->m_len -= siz;
    594 		NFSMADV(mp, siz);
    595 	}
    596 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
    597 		nfsm_reply(0);
    598 	if (vp->v_type != VREG) {
    599 		error = (vp->v_type == VDIR) ? EISDIR : EACCES;
    600 		vput(vp);
    601 		nfsm_reply(0);
    602 	}
    603 	nqsrv_getl(vp, NQL_WRITE);
    604 	if (error = nfsrv_access(vp, VWRITE, cred, rdonly, nfsd->nd_procp)) {
    605 		vput(vp);
    606 		nfsm_reply(0);
    607 	}
    608 	uiop->uio_resid = 0;
    609 	uiop->uio_rw = UIO_WRITE;
    610 	uiop->uio_segflg = UIO_SYSSPACE;
    611 	uiop->uio_procp = (struct proc *)0;
    612 	/*
    613 	 * Do up to NFS_MAXIOVEC mbufs of write each iteration of the
    614 	 * loop until done.
    615 	 */
    616 	while (len > 0 && uiop->uio_resid == 0) {
    617 		ivp = iv;
    618 		siz = 0;
    619 		uiop->uio_iov = ivp;
    620 		uiop->uio_iovcnt = 0;
    621 		uiop->uio_offset = off;
    622 		while (len > 0 && uiop->uio_iovcnt < NFS_MAXIOVEC && mp != NULL) {
    623 			ivp->iov_base = mtod(mp, caddr_t);
    624 			if (len < mp->m_len)
    625 				ivp->iov_len = xfer = len;
    626 			else
    627 				ivp->iov_len = xfer = mp->m_len;
    628 #ifdef notdef
    629 			/* Not Yet .. */
    630 			if (M_HASCL(mp) && (((u_long)ivp->iov_base) & CLOFSET) == 0)
    631 				ivp->iov_op = NULL;	/* what should it be ?? */
    632 			else
    633 				ivp->iov_op = NULL;
    634 #endif
    635 			uiop->uio_iovcnt++;
    636 			ivp++;
    637 			len -= xfer;
    638 			siz += xfer;
    639 			mp = mp->m_next;
    640 		}
    641 		if (len > 0 && mp == NULL) {
    642 			error = EBADRPC;
    643 			vput(vp);
    644 			nfsm_reply(0);
    645 		}
    646 		uiop->uio_resid = siz;
    647 		if (error = VOP_WRITE(vp, uiop, ioflags, cred)) {
    648 			vput(vp);
    649 			nfsm_reply(0);
    650 		}
    651 		off = uiop->uio_offset;
    652 	}
    653 	error = VOP_GETATTR(vp, vap, cred, nfsd->nd_procp);
    654 	vput(vp);
    655 	nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    656 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    657 	nfsm_srvfillattr;
    658 	if (nfsd->nd_nqlflag != NQL_NOVAL) {
    659 		nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
    660 		txdr_hyper(&vap->va_filerev, tl);
    661 	}
    662 	nfsm_srvdone;
    663 }
    664 
    665 /*
    666  * nfs create service
    667  * now does a truncate to 0 length via. setattr if it already exists
    668  */
    669 nfsrv_create(nfsd, mrep, md, dpos, cred, nam, mrq)
    670 	struct nfsd *nfsd;
    671 	struct mbuf *mrep, *md;
    672 	caddr_t dpos;
    673 	struct ucred *cred;
    674 	struct mbuf *nam, **mrq;
    675 {
    676 	register struct nfsv2_fattr *fp;
    677 	struct vattr va;
    678 	register struct vattr *vap = &va;
    679 	register struct nfsv2_sattr *sp;
    680 	register u_long *tl;
    681 	struct nameidata nd;
    682 	register caddr_t cp;
    683 	register long t1;
    684 	caddr_t bpos;
    685 	int error = 0, rdev, cache, len, tsize;
    686 	char *cp2;
    687 	struct mbuf *mb, *mb2, *mreq;
    688 	struct vnode *vp;
    689 	nfsv2fh_t nfh;
    690 	fhandle_t *fhp;
    691 	u_quad_t frev;
    692 
    693 	nd.ni_cnd.cn_nameiop = 0;
    694 	fhp = &nfh.fh_generic;
    695 	nfsm_srvmtofh(fhp);
    696 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    697 	nd.ni_cnd.cn_cred = cred;
    698 	nd.ni_cnd.cn_nameiop = CREATE;
    699 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
    700 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
    701 	    nfsd->nd_procp))
    702 		nfsm_reply(0);
    703 	VATTR_NULL(vap);
    704 	nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_SATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    705 	/*
    706 	 * Iff doesn't exist, create it
    707 	 * otherwise just truncate to 0 length
    708 	 *   should I set the mode too ??
    709 	 */
    710 	if (nd.ni_vp == NULL) {
    711 		vap->va_type = IFTOVT(fxdr_unsigned(u_long, sp->sa_mode));
    712 		if (vap->va_type == VNON)
    713 			vap->va_type = VREG;
    714 		vap->va_mode = nfstov_mode(sp->sa_mode);
    715 		if (nfsd->nd_nqlflag == NQL_NOVAL)
    716 			rdev = fxdr_unsigned(long, sp->sa_nfssize);
    717 		else
    718 			rdev = fxdr_unsigned(long, sp->sa_nqrdev);
    719 		if (vap->va_type == VREG || vap->va_type == VSOCK) {
    720 			vrele(nd.ni_startdir);
    721 			nqsrv_getl(nd.ni_dvp, NQL_WRITE);
    722 			if (error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap))
    723 				nfsm_reply(0);
    724 			FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    725 		} else if (vap->va_type == VCHR || vap->va_type == VBLK ||
    726 			vap->va_type == VFIFO) {
    727 			if (vap->va_type == VCHR && rdev == 0xffffffff)
    728 				vap->va_type = VFIFO;
    729 			if (vap->va_type == VFIFO) {
    730 #ifndef FIFO
    731 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    732 				vput(nd.ni_dvp);
    733 				error = ENXIO;
    734 				goto out;
    735 #endif /* FIFO */
    736 			} else if (error = suser(cred, (u_short *)0)) {
    737 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    738 				vput(nd.ni_dvp);
    739 				goto out;
    740 			} else
    741 				vap->va_rdev = (dev_t)rdev;
    742 			nqsrv_getl(nd.ni_dvp, NQL_WRITE);
    743 			if (error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap)) {
    744 				vrele(nd.ni_startdir);
    745 				nfsm_reply(0);
    746 			}
    747 			nd.ni_cnd.cn_nameiop = LOOKUP;
    748 			nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART);
    749 			nd.ni_cnd.cn_proc = nfsd->nd_procp;
    750 			nd.ni_cnd.cn_cred = nfsd->nd_procp->p_ucred;
    751 			if (error = lookup(&nd)) {
    752 				free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    753 				nfsm_reply(0);
    754 			}
    755 			FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    756 			if (nd.ni_cnd.cn_flags & ISSYMLINK) {
    757 				vrele(nd.ni_dvp);
    758 				vput(nd.ni_vp);
    759 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    760 				error = EINVAL;
    761 				nfsm_reply(0);
    762 			}
    763 		} else {
    764 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    765 			vput(nd.ni_dvp);
    766 			error = ENXIO;
    767 			goto out;
    768 		}
    769 		vp = nd.ni_vp;
    770 	} else {
    771 		vrele(nd.ni_startdir);
    772 		free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    773 		vp = nd.ni_vp;
    774 		if (nd.ni_dvp == vp)
    775 			vrele(nd.ni_dvp);
    776 		else
    777 			vput(nd.ni_dvp);
    778 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    779 		if (nfsd->nd_nqlflag == NQL_NOVAL) {
    780 			tsize = fxdr_unsigned(long, sp->sa_nfssize);
    781 			if (tsize != -1)
    782 				vap->va_size = (u_quad_t)tsize;
    783 			else
    784 				vap->va_size = -1;
    785 		} else
    786 			fxdr_hyper(&sp->sa_nqsize, &vap->va_size);
    787 		if (vap->va_size != -1) {
    788 			if (error = nfsrv_access(vp, VWRITE, cred,
    789 			    (nd.ni_cnd.cn_flags & RDONLY), nfsd->nd_procp)) {
    790 				vput(vp);
    791 				nfsm_reply(0);
    792 			}
    793 			nqsrv_getl(vp, NQL_WRITE);
    794 			if (error = VOP_SETATTR(vp, vap, cred, nfsd->nd_procp)) {
    795 				vput(vp);
    796 				nfsm_reply(0);
    797 			}
    798 		}
    799 	}
    800 	bzero((caddr_t)fhp, sizeof(nfh));
    801 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
    802 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
    803 		vput(vp);
    804 		nfsm_reply(0);
    805 	}
    806 	error = VOP_GETATTR(vp, vap, cred, nfsd->nd_procp);
    807 	vput(vp);
    808 	nfsm_reply(NFSX_FH+NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    809 	nfsm_srvfhtom(fhp);
    810 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
    811 	nfsm_srvfillattr;
    812 	return (error);
    813 nfsmout:
    814 	if (nd.ni_cnd.cn_nameiop || nd.ni_cnd.cn_flags)
    815 		vrele(nd.ni_startdir);
    816 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    817 	if (nd.ni_dvp == nd.ni_vp)
    818 		vrele(nd.ni_dvp);
    819 	else
    820 		vput(nd.ni_dvp);
    821 	if (nd.ni_vp)
    822 		vput(nd.ni_vp);
    823 	return (error);
    824 
    825 out:
    826 	vrele(nd.ni_startdir);
    827 	free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
    828 	nfsm_reply(0);
    829 }
    830 
    831 /*
    832  * nfs remove service
    833  */
    834 nfsrv_remove(nfsd, mrep, md, dpos, cred, nam, mrq)
    835 	struct nfsd *nfsd;
    836 	struct mbuf *mrep, *md;
    837 	caddr_t dpos;
    838 	struct ucred *cred;
    839 	struct mbuf *nam, **mrq;
    840 {
    841 	struct nameidata nd;
    842 	register u_long *tl;
    843 	register long t1;
    844 	caddr_t bpos;
    845 	int error = 0, cache, len;
    846 	char *cp2;
    847 	struct mbuf *mb, *mreq;
    848 	struct vnode *vp;
    849 	nfsv2fh_t nfh;
    850 	fhandle_t *fhp;
    851 	u_quad_t frev;
    852 
    853 	fhp = &nfh.fh_generic;
    854 	nfsm_srvmtofh(fhp);
    855 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    856 	nd.ni_cnd.cn_cred = cred;
    857 	nd.ni_cnd.cn_nameiop = DELETE;
    858 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
    859 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
    860 	    nfsd->nd_procp))
    861 		nfsm_reply(0);
    862 	vp = nd.ni_vp;
    863 	if (vp->v_type == VDIR &&
    864 		(error = suser(cred, (u_short *)0)))
    865 		goto out;
    866 	/*
    867 	 * The root of a mounted filesystem cannot be deleted.
    868 	 */
    869 	if (vp->v_flag & VROOT) {
    870 		error = EBUSY;
    871 		goto out;
    872 	}
    873 	if (vp->v_flag & VTEXT)
    874 		(void) vnode_pager_uncache(vp);
    875 out:
    876 	if (!error) {
    877 		nqsrv_getl(nd.ni_dvp, NQL_WRITE);
    878 		nqsrv_getl(vp, NQL_WRITE);
    879 		error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
    880 	} else {
    881 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    882 		if (nd.ni_dvp == vp)
    883 			vrele(nd.ni_dvp);
    884 		else
    885 			vput(nd.ni_dvp);
    886 		vput(vp);
    887 	}
    888 	nfsm_reply(0);
    889 	nfsm_srvdone;
    890 }
    891 
    892 /*
    893  * nfs rename service
    894  */
    895 nfsrv_rename(nfsd, mrep, md, dpos, cred, nam, mrq)
    896 	struct nfsd *nfsd;
    897 	struct mbuf *mrep, *md;
    898 	caddr_t dpos;
    899 	struct ucred *cred;
    900 	struct mbuf *nam, **mrq;
    901 {
    902 	register u_long *tl;
    903 	register long t1;
    904 	caddr_t bpos;
    905 	int error = 0, cache, len, len2;
    906 	char *cp2;
    907 	struct mbuf *mb, *mreq;
    908 	struct nameidata fromnd, tond;
    909 	struct vnode *fvp, *tvp, *tdvp;
    910 	nfsv2fh_t fnfh, tnfh;
    911 	fhandle_t *ffhp, *tfhp;
    912 	u_quad_t frev;
    913 	uid_t saved_uid;
    914 
    915 	ffhp = &fnfh.fh_generic;
    916 	tfhp = &tnfh.fh_generic;
    917 	fromnd.ni_cnd.cn_nameiop = 0;
    918 	tond.ni_cnd.cn_nameiop = 0;
    919 	nfsm_srvmtofh(ffhp);
    920 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    921 	/*
    922 	 * Remember our original uid so that we can reset cr_uid before
    923 	 * the second nfs_namei() call, in case it is remapped.
    924 	 */
    925 	saved_uid = cred->cr_uid;
    926 	fromnd.ni_cnd.cn_cred = cred;
    927 	fromnd.ni_cnd.cn_nameiop = DELETE;
    928 	fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
    929 	if (error = nfs_namei(&fromnd, ffhp, len, nfsd->nd_slp, nam, &md,
    930 	    &dpos, nfsd->nd_procp))
    931 		nfsm_reply(0);
    932 	fvp = fromnd.ni_vp;
    933 	nfsm_srvmtofh(tfhp);
    934 	nfsm_strsiz(len2, NFS_MAXNAMLEN);
    935 	cred->cr_uid = saved_uid;
    936 	tond.ni_cnd.cn_cred = cred;
    937 	tond.ni_cnd.cn_nameiop = RENAME;
    938 	tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
    939 	if (error = nfs_namei(&tond, tfhp, len2, nfsd->nd_slp, nam, &md,
    940 	    &dpos, nfsd->nd_procp)) {
    941 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
    942 		vrele(fromnd.ni_dvp);
    943 		vrele(fvp);
    944 		goto out1;
    945 	}
    946 	tdvp = tond.ni_dvp;
    947 	tvp = tond.ni_vp;
    948 	if (tvp != NULL) {
    949 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
    950 			error = EISDIR;
    951 			goto out;
    952 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
    953 			error = ENOTDIR;
    954 			goto out;
    955 		}
    956 		if (tvp->v_type == VDIR && tvp->v_mountedhere) {
    957 			error = EXDEV;
    958 			goto out;
    959 		}
    960 	}
    961 	if (fvp->v_type == VDIR && fvp->v_mountedhere) {
    962 		error = EBUSY;
    963 		goto out;
    964 	}
    965 	if (fvp->v_mount != tdvp->v_mount) {
    966 		error = EXDEV;
    967 		goto out;
    968 	}
    969 	if (fvp == tdvp)
    970 		error = EINVAL;
    971 	/*
    972 	 * If source is the same as the destination (that is the
    973 	 * same vnode with the same name in the same directory),
    974 	 * then there is nothing to do.
    975 	 */
    976 	if (fvp == tvp && fromnd.ni_dvp == tdvp &&
    977 	    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
    978 	    !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
    979 	      fromnd.ni_cnd.cn_namelen))
    980 		error = -1;
    981 out:
    982 	if (!error) {
    983 		nqsrv_getl(fromnd.ni_dvp, NQL_WRITE);
    984 		nqsrv_getl(tdvp, NQL_WRITE);
    985 		if (tvp)
    986 			nqsrv_getl(tvp, NQL_WRITE);
    987 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
    988 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
    989 	} else {
    990 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
    991 		if (tdvp == tvp)
    992 			vrele(tdvp);
    993 		else
    994 			vput(tdvp);
    995 		if (tvp)
    996 			vput(tvp);
    997 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
    998 		vrele(fromnd.ni_dvp);
    999 		vrele(fvp);
   1000 	}
   1001 	vrele(tond.ni_startdir);
   1002 	FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   1003 out1:
   1004 	vrele(fromnd.ni_startdir);
   1005 	FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   1006 	nfsm_reply(0);
   1007 	return (error);
   1008 
   1009 nfsmout:
   1010 	if (tond.ni_cnd.cn_nameiop || tond.ni_cnd.cn_flags) {
   1011 		vrele(tond.ni_startdir);
   1012 		FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   1013 	}
   1014 	if (fromnd.ni_cnd.cn_nameiop || fromnd.ni_cnd.cn_flags) {
   1015 		vrele(fromnd.ni_startdir);
   1016 		FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   1017 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1018 		vrele(fromnd.ni_dvp);
   1019 		vrele(fvp);
   1020 	}
   1021 	return (error);
   1022 }
   1023 
   1024 /*
   1025  * nfs link service
   1026  */
   1027 nfsrv_link(nfsd, mrep, md, dpos, cred, nam, mrq)
   1028 	struct nfsd *nfsd;
   1029 	struct mbuf *mrep, *md;
   1030 	caddr_t dpos;
   1031 	struct ucred *cred;
   1032 	struct mbuf *nam, **mrq;
   1033 {
   1034 	struct nameidata nd;
   1035 	register u_long *tl;
   1036 	register long t1;
   1037 	caddr_t bpos;
   1038 	int error = 0, rdonly, cache, len;
   1039 	char *cp2;
   1040 	struct mbuf *mb, *mreq;
   1041 	struct vnode *vp, *xp;
   1042 	nfsv2fh_t nfh, dnfh;
   1043 	fhandle_t *fhp, *dfhp;
   1044 	u_quad_t frev;
   1045 
   1046 	fhp = &nfh.fh_generic;
   1047 	dfhp = &dnfh.fh_generic;
   1048 	nfsm_srvmtofh(fhp);
   1049 	nfsm_srvmtofh(dfhp);
   1050 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1051 	if (error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
   1052 		nfsm_reply(0);
   1053 	if (vp->v_type == VDIR && (error = suser(cred, (u_short *)0)))
   1054 		goto out1;
   1055 	nd.ni_cnd.cn_cred = cred;
   1056 	nd.ni_cnd.cn_nameiop = CREATE;
   1057 	nd.ni_cnd.cn_flags = LOCKPARENT;
   1058 	if (error = nfs_namei(&nd, dfhp, len, nfsd->nd_slp, nam, &md, &dpos,
   1059 	    nfsd->nd_procp))
   1060 		goto out1;
   1061 	xp = nd.ni_vp;
   1062 	if (xp != NULL) {
   1063 		error = EEXIST;
   1064 		goto out;
   1065 	}
   1066 	xp = nd.ni_dvp;
   1067 	if (vp->v_mount != xp->v_mount)
   1068 		error = EXDEV;
   1069 out:
   1070 	if (!error) {
   1071 		nqsrv_getl(vp, NQL_WRITE);
   1072 		nqsrv_getl(xp, NQL_WRITE);
   1073 		error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   1074 	} else {
   1075 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1076 		if (nd.ni_dvp == nd.ni_vp)
   1077 			vrele(nd.ni_dvp);
   1078 		else
   1079 			vput(nd.ni_dvp);
   1080 		if (nd.ni_vp)
   1081 			vrele(nd.ni_vp);
   1082 	}
   1083 out1:
   1084 	vrele(vp);
   1085 	nfsm_reply(0);
   1086 	nfsm_srvdone;
   1087 }
   1088 
   1089 /*
   1090  * nfs symbolic link service
   1091  */
   1092 nfsrv_symlink(nfsd, mrep, md, dpos, cred, nam, mrq)
   1093 	struct nfsd *nfsd;
   1094 	struct mbuf *mrep, *md;
   1095 	caddr_t dpos;
   1096 	struct ucred *cred;
   1097 	struct mbuf *nam, **mrq;
   1098 {
   1099 	struct vattr va;
   1100 	struct nameidata nd;
   1101 	register struct vattr *vap = &va;
   1102 	register u_long *tl;
   1103 	register long t1;
   1104 	struct nfsv2_sattr *sp;
   1105 	caddr_t bpos;
   1106 	struct uio io;
   1107 	struct iovec iv;
   1108 	int error = 0, cache, len, len2;
   1109 	char *pathcp, *cp2;
   1110 	struct mbuf *mb, *mreq;
   1111 	nfsv2fh_t nfh;
   1112 	fhandle_t *fhp;
   1113 	u_quad_t frev;
   1114 
   1115 	pathcp = (char *)0;
   1116 	fhp = &nfh.fh_generic;
   1117 	nfsm_srvmtofh(fhp);
   1118 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1119 	nd.ni_cnd.cn_cred = cred;
   1120 	nd.ni_cnd.cn_nameiop = CREATE;
   1121 	nd.ni_cnd.cn_flags = LOCKPARENT;
   1122 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
   1123 	    nfsd->nd_procp))
   1124 		goto out;
   1125 	nfsm_strsiz(len2, NFS_MAXPATHLEN);
   1126 	MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
   1127 	iv.iov_base = pathcp;
   1128 	iv.iov_len = len2;
   1129 	io.uio_resid = len2;
   1130 	io.uio_offset = 0;
   1131 	io.uio_iov = &iv;
   1132 	io.uio_iovcnt = 1;
   1133 	io.uio_segflg = UIO_SYSSPACE;
   1134 	io.uio_rw = UIO_READ;
   1135 	io.uio_procp = (struct proc *)0;
   1136 	nfsm_mtouio(&io, len2);
   1137 	nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_SATTR(nfsd->nd_nqlflag != NQL_NOVAL));
   1138 	*(pathcp + len2) = '\0';
   1139 	if (nd.ni_vp) {
   1140 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1141 		if (nd.ni_dvp == nd.ni_vp)
   1142 			vrele(nd.ni_dvp);
   1143 		else
   1144 			vput(nd.ni_dvp);
   1145 		vrele(nd.ni_vp);
   1146 		error = EEXIST;
   1147 		goto out;
   1148 	}
   1149 	VATTR_NULL(vap);
   1150 	vap->va_mode = fxdr_unsigned(u_short, sp->sa_mode);
   1151 	nqsrv_getl(nd.ni_dvp, NQL_WRITE);
   1152 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
   1153 out:
   1154 	if (pathcp)
   1155 		FREE(pathcp, M_TEMP);
   1156 	nfsm_reply(0);
   1157 	return (error);
   1158 nfsmout:
   1159 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1160 	if (nd.ni_dvp == nd.ni_vp)
   1161 		vrele(nd.ni_dvp);
   1162 	else
   1163 		vput(nd.ni_dvp);
   1164 	if (nd.ni_vp)
   1165 		vrele(nd.ni_vp);
   1166 	if (pathcp)
   1167 		FREE(pathcp, M_TEMP);
   1168 	return (error);
   1169 }
   1170 
   1171 /*
   1172  * nfs mkdir service
   1173  */
   1174 nfsrv_mkdir(nfsd, mrep, md, dpos, cred, nam, mrq)
   1175 	struct nfsd *nfsd;
   1176 	struct mbuf *mrep, *md;
   1177 	caddr_t dpos;
   1178 	struct ucred *cred;
   1179 	struct mbuf *nam, **mrq;
   1180 {
   1181 	struct vattr va;
   1182 	register struct vattr *vap = &va;
   1183 	register struct nfsv2_fattr *fp;
   1184 	struct nameidata nd;
   1185 	register caddr_t cp;
   1186 	register u_long *tl;
   1187 	register long t1;
   1188 	caddr_t bpos;
   1189 	int error = 0, cache, len;
   1190 	char *cp2;
   1191 	struct mbuf *mb, *mb2, *mreq;
   1192 	struct vnode *vp;
   1193 	nfsv2fh_t nfh;
   1194 	fhandle_t *fhp;
   1195 	u_quad_t frev;
   1196 
   1197 	fhp = &nfh.fh_generic;
   1198 	nfsm_srvmtofh(fhp);
   1199 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1200 	nd.ni_cnd.cn_cred = cred;
   1201 	nd.ni_cnd.cn_nameiop = CREATE;
   1202 	nd.ni_cnd.cn_flags = LOCKPARENT;
   1203 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
   1204 	    nfsd->nd_procp))
   1205 		nfsm_reply(0);
   1206 	nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
   1207 	VATTR_NULL(vap);
   1208 	vap->va_type = VDIR;
   1209 	vap->va_mode = nfstov_mode(*tl++);
   1210 	vp = nd.ni_vp;
   1211 	if (vp != NULL) {
   1212 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1213 		if (nd.ni_dvp == vp)
   1214 			vrele(nd.ni_dvp);
   1215 		else
   1216 			vput(nd.ni_dvp);
   1217 		vrele(vp);
   1218 		error = EEXIST;
   1219 		nfsm_reply(0);
   1220 	}
   1221 	nqsrv_getl(nd.ni_dvp, NQL_WRITE);
   1222 	if (error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap))
   1223 		nfsm_reply(0);
   1224 	vp = nd.ni_vp;
   1225 	bzero((caddr_t)fhp, sizeof(nfh));
   1226 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
   1227 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
   1228 		vput(vp);
   1229 		nfsm_reply(0);
   1230 	}
   1231 	error = VOP_GETATTR(vp, vap, cred, nfsd->nd_procp);
   1232 	vput(vp);
   1233 	nfsm_reply(NFSX_FH+NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
   1234 	nfsm_srvfhtom(fhp);
   1235 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
   1236 	nfsm_srvfillattr;
   1237 	return (error);
   1238 nfsmout:
   1239 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1240 	if (nd.ni_dvp == nd.ni_vp)
   1241 		vrele(nd.ni_dvp);
   1242 	else
   1243 		vput(nd.ni_dvp);
   1244 	if (nd.ni_vp)
   1245 		vrele(nd.ni_vp);
   1246 	return (error);
   1247 }
   1248 
   1249 /*
   1250  * nfs rmdir service
   1251  */
   1252 nfsrv_rmdir(nfsd, mrep, md, dpos, cred, nam, mrq)
   1253 	struct nfsd *nfsd;
   1254 	struct mbuf *mrep, *md;
   1255 	caddr_t dpos;
   1256 	struct ucred *cred;
   1257 	struct mbuf *nam, **mrq;
   1258 {
   1259 	register u_long *tl;
   1260 	register long t1;
   1261 	caddr_t bpos;
   1262 	int error = 0, cache, len;
   1263 	char *cp2;
   1264 	struct mbuf *mb, *mreq;
   1265 	struct vnode *vp;
   1266 	nfsv2fh_t nfh;
   1267 	fhandle_t *fhp;
   1268 	struct nameidata nd;
   1269 	u_quad_t frev;
   1270 
   1271 	fhp = &nfh.fh_generic;
   1272 	nfsm_srvmtofh(fhp);
   1273 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1274 	nd.ni_cnd.cn_cred = cred;
   1275 	nd.ni_cnd.cn_nameiop = DELETE;
   1276 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
   1277 	if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
   1278 	    nfsd->nd_procp))
   1279 		nfsm_reply(0);
   1280 	vp = nd.ni_vp;
   1281 	if (vp->v_type != VDIR) {
   1282 		error = ENOTDIR;
   1283 		goto out;
   1284 	}
   1285 	/*
   1286 	 * No rmdir "." please.
   1287 	 */
   1288 	if (nd.ni_dvp == vp) {
   1289 		error = EINVAL;
   1290 		goto out;
   1291 	}
   1292 	/*
   1293 	 * The root of a mounted filesystem cannot be deleted.
   1294 	 */
   1295 	if (vp->v_flag & VROOT)
   1296 		error = EBUSY;
   1297 out:
   1298 	if (!error) {
   1299 		nqsrv_getl(nd.ni_dvp, NQL_WRITE);
   1300 		nqsrv_getl(vp, NQL_WRITE);
   1301 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1302 	} else {
   1303 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1304 		if (nd.ni_dvp == nd.ni_vp)
   1305 			vrele(nd.ni_dvp);
   1306 		else
   1307 			vput(nd.ni_dvp);
   1308 		vput(vp);
   1309 	}
   1310 	nfsm_reply(0);
   1311 	nfsm_srvdone;
   1312 }
   1313 
   1314 /*
   1315  * nfs readdir service
   1316  * - mallocs what it thinks is enough to read
   1317  *	count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
   1318  * - calls VOP_READDIR()
   1319  * - loops around building the reply
   1320  *	if the output generated exceeds count break out of loop
   1321  *	The nfsm_clget macro is used here so that the reply will be packed
   1322  *	tightly in mbuf clusters.
   1323  * - it only knows that it has encountered eof when the VOP_READDIR()
   1324  *	reads nothing
   1325  * - as such one readdir rpc will return eof false although you are there
   1326  *	and then the next will return eof
   1327  * - it trims out records with d_fileno == 0
   1328  *	this doesn't matter for Unix clients, but they might confuse clients
   1329  *	for other os'.
   1330  * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
   1331  *	than requested, but this may not apply to all filesystems. For
   1332  *	example, client NFS does not { although it is never remote mounted
   1333  *	anyhow }
   1334  *     The alternate call nqnfsrv_readdirlook() does lookups as well.
   1335  * PS: The NFS protocol spec. does not clarify what the "count" byte
   1336  *	argument is a count of.. just name strings and file id's or the
   1337  *	entire reply rpc or ...
   1338  *	I tried just file name and id sizes and it confused the Sun client,
   1339  *	so I am using the full rpc size now. The "paranoia.." comment refers
   1340  *	to including the status longwords that are not a part of the dir.
   1341  *	"entry" structures, but are in the rpc.
   1342  */
   1343 struct flrep {
   1344 	u_long fl_cachable;
   1345 	u_long fl_duration;
   1346 	u_long fl_frev[2];
   1347 	nfsv2fh_t fl_nfh;
   1348 	u_long fl_fattr[NFSX_NQFATTR / sizeof (u_long)];
   1349 };
   1350 
   1351 nfsrv_readdir(nfsd, mrep, md, dpos, cred, nam, mrq)
   1352 	struct nfsd *nfsd;
   1353 	struct mbuf *mrep, *md;
   1354 	caddr_t dpos;
   1355 	struct ucred *cred;
   1356 	struct mbuf *nam, **mrq;
   1357 {
   1358 	register char *bp, *be;
   1359 	register struct mbuf *mp;
   1360 	register struct dirent *dp;
   1361 	register caddr_t cp;
   1362 	register u_long *tl;
   1363 	register long t1;
   1364 	caddr_t bpos;
   1365 	struct mbuf *mb, *mb2, *mreq, *mp2;
   1366 	char *cpos, *cend, *cp2, *rbuf;
   1367 	struct vnode *vp;
   1368 	nfsv2fh_t nfh;
   1369 	fhandle_t *fhp;
   1370 	struct uio io;
   1371 	struct iovec iv;
   1372 	int len, nlen, rem, xfer, tsiz, i, error = 0;
   1373 	int siz, cnt, fullsiz, eofflag, rdonly, cache;
   1374 	u_quad_t frev;
   1375 	u_long on, off, toff;
   1376 
   1377 	fhp = &nfh.fh_generic;
   1378 	nfsm_srvmtofh(fhp);
   1379 	nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
   1380 	toff = fxdr_unsigned(u_long, *tl++);
   1381 	off = (toff & ~(NFS_DIRBLKSIZ-1));
   1382 	on = (toff & (NFS_DIRBLKSIZ-1));
   1383 	cnt = fxdr_unsigned(int, *tl);
   1384 	siz = ((cnt+NFS_DIRBLKSIZ-1) & ~(NFS_DIRBLKSIZ-1));
   1385 	if (cnt > NFS_MAXREADDIR)
   1386 		siz = NFS_MAXREADDIR;
   1387 	fullsiz = siz;
   1388 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
   1389 		nfsm_reply(0);
   1390 	nqsrv_getl(vp, NQL_READ);
   1391 	if (error = nfsrv_access(vp, VEXEC, cred, rdonly, nfsd->nd_procp)) {
   1392 		vput(vp);
   1393 		nfsm_reply(0);
   1394 	}
   1395 	VOP_UNLOCK(vp);
   1396 	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
   1397 again:
   1398 	iv.iov_base = rbuf;
   1399 	iv.iov_len = fullsiz;
   1400 	io.uio_iov = &iv;
   1401 	io.uio_iovcnt = 1;
   1402 	io.uio_offset = (off_t)off;
   1403 	io.uio_resid = fullsiz;
   1404 	io.uio_segflg = UIO_SYSSPACE;
   1405 	io.uio_rw = UIO_READ;
   1406 	io.uio_procp = (struct proc *)0;
   1407 	error = VOP_READDIR(vp, &io, cred);
   1408 	off = (off_t)io.uio_offset;
   1409 	if (error) {
   1410 		vrele(vp);
   1411 		free((caddr_t)rbuf, M_TEMP);
   1412 		nfsm_reply(0);
   1413 	}
   1414 	if (io.uio_resid < fullsiz)
   1415 		eofflag = 0;
   1416 	else
   1417 		eofflag = 1;
   1418 	if (io.uio_resid) {
   1419 		siz -= io.uio_resid;
   1420 
   1421 		/*
   1422 		 * If nothing read, return eof
   1423 		 * rpc reply
   1424 		 */
   1425 		if (siz == 0) {
   1426 			vrele(vp);
   1427 			nfsm_reply(2*NFSX_UNSIGNED);
   1428 			nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
   1429 			*tl++ = nfs_false;
   1430 			*tl = nfs_true;
   1431 			FREE((caddr_t)rbuf, M_TEMP);
   1432 			return (0);
   1433 		}
   1434 	}
   1435 
   1436 	/*
   1437 	 * Check for degenerate cases of nothing useful read.
   1438 	 * If so go try again
   1439 	 */
   1440 	cpos = rbuf + on;
   1441 	cend = rbuf + siz;
   1442 	dp = (struct dirent *)cpos;
   1443 	while (cpos < cend && dp->d_fileno == 0) {
   1444 		cpos += dp->d_reclen;
   1445 		dp = (struct dirent *)cpos;
   1446 	}
   1447 	if (cpos >= cend) {
   1448 		toff = off;
   1449 		siz = fullsiz;
   1450 		on = 0;
   1451 		goto again;
   1452 	}
   1453 
   1454 	cpos = rbuf + on;
   1455 	cend = rbuf + siz;
   1456 	dp = (struct dirent *)cpos;
   1457 	len = 3*NFSX_UNSIGNED;	/* paranoia, probably can be 0 */
   1458 	nfsm_reply(siz);
   1459 	mp = mp2 = mb;
   1460 	bp = bpos;
   1461 	be = bp + M_TRAILINGSPACE(mp);
   1462 
   1463 	/* Loop through the records and build reply */
   1464 	while (cpos < cend) {
   1465 		if (dp->d_fileno != 0) {
   1466 			nlen = dp->d_namlen;
   1467 			rem = nfsm_rndup(nlen)-nlen;
   1468 			len += (4*NFSX_UNSIGNED + nlen + rem);
   1469 			if (len > cnt) {
   1470 				eofflag = 0;
   1471 				break;
   1472 			}
   1473 			/*
   1474 			 * Build the directory record xdr from
   1475 			 * the dirent entry.
   1476 			 */
   1477 			nfsm_clget;
   1478 			*tl = nfs_true;
   1479 			bp += NFSX_UNSIGNED;
   1480 			nfsm_clget;
   1481 			*tl = txdr_unsigned(dp->d_fileno);
   1482 			bp += NFSX_UNSIGNED;
   1483 			nfsm_clget;
   1484 			*tl = txdr_unsigned(nlen);
   1485 			bp += NFSX_UNSIGNED;
   1486 
   1487 			/* And loop around copying the name */
   1488 			xfer = nlen;
   1489 			cp = dp->d_name;
   1490 			while (xfer > 0) {
   1491 				nfsm_clget;
   1492 				if ((bp+xfer) > be)
   1493 					tsiz = be-bp;
   1494 				else
   1495 					tsiz = xfer;
   1496 				bcopy(cp, bp, tsiz);
   1497 				bp += tsiz;
   1498 				xfer -= tsiz;
   1499 				if (xfer > 0)
   1500 					cp += tsiz;
   1501 			}
   1502 			/* And null pad to a long boundary */
   1503 			for (i = 0; i < rem; i++)
   1504 				*bp++ = '\0';
   1505 			nfsm_clget;
   1506 
   1507 			/* Finish off the record */
   1508 			toff += dp->d_reclen;
   1509 			*tl = txdr_unsigned(toff);
   1510 			bp += NFSX_UNSIGNED;
   1511 		} else
   1512 			toff += dp->d_reclen;
   1513 		cpos += dp->d_reclen;
   1514 		dp = (struct dirent *)cpos;
   1515 	}
   1516 	vrele(vp);
   1517 	nfsm_clget;
   1518 	*tl = nfs_false;
   1519 	bp += NFSX_UNSIGNED;
   1520 	nfsm_clget;
   1521 	if (eofflag)
   1522 		*tl = nfs_true;
   1523 	else
   1524 		*tl = nfs_false;
   1525 	bp += NFSX_UNSIGNED;
   1526 	if (mp != mb) {
   1527 		if (bp < be)
   1528 			mp->m_len = bp - mtod(mp, caddr_t);
   1529 	} else
   1530 		mp->m_len += bp - bpos;
   1531 	FREE(rbuf, M_TEMP);
   1532 	nfsm_srvdone;
   1533 }
   1534 
   1535 nqnfsrv_readdirlook(nfsd, mrep, md, dpos, cred, nam, mrq)
   1536 	struct nfsd *nfsd;
   1537 	struct mbuf *mrep, *md;
   1538 	caddr_t dpos;
   1539 	struct ucred *cred;
   1540 	struct mbuf *nam, **mrq;
   1541 {
   1542 	register char *bp, *be;
   1543 	register struct mbuf *mp;
   1544 	register struct dirent *dp;
   1545 	register caddr_t cp;
   1546 	register u_long *tl;
   1547 	register long t1;
   1548 	caddr_t bpos;
   1549 	struct mbuf *mb, *mb2, *mreq, *mp2;
   1550 	char *cpos, *cend, *cp2, *rbuf;
   1551 	struct vnode *vp, *nvp;
   1552 	struct flrep fl;
   1553 	nfsv2fh_t nfh;
   1554 	fhandle_t *fhp;
   1555 	struct uio io;
   1556 	struct iovec iv;
   1557 	struct vattr va, *vap = &va;
   1558 	struct nfsv2_fattr *fp;
   1559 	int len, nlen, rem, xfer, tsiz, i, error = 0, duration2, cache2;
   1560 	int siz, cnt, fullsiz, eofflag, rdonly, cache;
   1561 	u_quad_t frev, frev2;
   1562 	u_long on, off, toff;
   1563 
   1564 	fhp = &nfh.fh_generic;
   1565 	nfsm_srvmtofh(fhp);
   1566 	nfsm_dissect(tl, u_long *, 3*NFSX_UNSIGNED);
   1567 	toff = fxdr_unsigned(u_long, *tl++);
   1568 	off = (toff & ~(NFS_DIRBLKSIZ-1));
   1569 	on = (toff & (NFS_DIRBLKSIZ-1));
   1570 	cnt = fxdr_unsigned(int, *tl++);
   1571 	duration2 = fxdr_unsigned(int, *tl);
   1572 	siz = ((cnt+NFS_DIRBLKSIZ-1) & ~(NFS_DIRBLKSIZ-1));
   1573 	if (cnt > NFS_MAXREADDIR)
   1574 		siz = NFS_MAXREADDIR;
   1575 	fullsiz = siz;
   1576 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
   1577 		nfsm_reply(0);
   1578 	nqsrv_getl(vp, NQL_READ);
   1579 	if (error = nfsrv_access(vp, VEXEC, cred, rdonly, nfsd->nd_procp)) {
   1580 		vput(vp);
   1581 		nfsm_reply(0);
   1582 	}
   1583 	VOP_UNLOCK(vp);
   1584 	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
   1585 again:
   1586 	iv.iov_base = rbuf;
   1587 	iv.iov_len = fullsiz;
   1588 	io.uio_iov = &iv;
   1589 	io.uio_iovcnt = 1;
   1590 	io.uio_offset = (off_t)off;
   1591 	io.uio_resid = fullsiz;
   1592 	io.uio_segflg = UIO_SYSSPACE;
   1593 	io.uio_rw = UIO_READ;
   1594 	io.uio_procp = (struct proc *)0;
   1595 	error = VOP_READDIR(vp, &io, cred);
   1596 	off = (u_long)io.uio_offset;
   1597 	if (error) {
   1598 		vrele(vp);
   1599 		free((caddr_t)rbuf, M_TEMP);
   1600 		nfsm_reply(0);
   1601 	}
   1602 	if (io.uio_resid < fullsiz)
   1603 		eofflag = 0;
   1604 	else
   1605 		eofflag = 1;
   1606 	if (io.uio_resid) {
   1607 		siz -= io.uio_resid;
   1608 
   1609 		/*
   1610 		 * If nothing read, return eof
   1611 		 * rpc reply
   1612 		 */
   1613 		if (siz == 0) {
   1614 			vrele(vp);
   1615 			nfsm_reply(2 * NFSX_UNSIGNED);
   1616 			nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
   1617 			*tl++ = nfs_false;
   1618 			*tl = nfs_true;
   1619 			FREE((caddr_t)rbuf, M_TEMP);
   1620 			return (0);
   1621 		}
   1622 	}
   1623 
   1624 	/*
   1625 	 * Check for degenerate cases of nothing useful read.
   1626 	 * If so go try again
   1627 	 */
   1628 	cpos = rbuf + on;
   1629 	cend = rbuf + siz;
   1630 	dp = (struct dirent *)cpos;
   1631 	while (cpos < cend && dp->d_fileno == 0) {
   1632 		cpos += dp->d_reclen;
   1633 		dp = (struct dirent *)cpos;
   1634 	}
   1635 	if (cpos >= cend) {
   1636 		toff = off;
   1637 		siz = fullsiz;
   1638 		on = 0;
   1639 		goto again;
   1640 	}
   1641 
   1642 	cpos = rbuf + on;
   1643 	cend = rbuf + siz;
   1644 	dp = (struct dirent *)cpos;
   1645 	len = 3 * NFSX_UNSIGNED;	/* paranoia, probably can be 0 */
   1646 	nfsm_reply(siz);
   1647 	mp = mp2 = mb;
   1648 	bp = bpos;
   1649 	be = bp + M_TRAILINGSPACE(mp);
   1650 
   1651 	/* Loop through the records and build reply */
   1652 	while (cpos < cend) {
   1653 		if (dp->d_fileno != 0) {
   1654 			nlen = dp->d_namlen;
   1655 			rem = nfsm_rndup(nlen)-nlen;
   1656 
   1657 			/*
   1658 			 * For readdir_and_lookup get the vnode using
   1659 			 * the file number.
   1660 			 */
   1661 			if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
   1662 				goto invalid;
   1663 			bzero((caddr_t)&fl.fl_nfh, sizeof (nfsv2fh_t));
   1664 			fl.fl_nfh.fh_generic.fh_fsid =
   1665 				nvp->v_mount->mnt_stat.f_fsid;
   1666 			if (VFS_VPTOFH(nvp, &fl.fl_nfh.fh_generic.fh_fid)) {
   1667 				vput(nvp);
   1668 				goto invalid;
   1669 			}
   1670 			if (duration2) {
   1671 				(void) nqsrv_getlease(nvp, &duration2, NQL_READ,
   1672 					nfsd, nam, &cache2, &frev2, cred);
   1673 				fl.fl_duration = txdr_unsigned(duration2);
   1674 				fl.fl_cachable = txdr_unsigned(cache2);
   1675 				txdr_hyper(&frev2, fl.fl_frev);
   1676 			} else
   1677 				fl.fl_duration = 0;
   1678 			if (VOP_GETATTR(nvp, vap, cred, nfsd->nd_procp)) {
   1679 				vput(nvp);
   1680 				goto invalid;
   1681 			}
   1682 			vput(nvp);
   1683 			fp = (struct nfsv2_fattr *)&fl.fl_fattr;
   1684 			nfsm_srvfillattr;
   1685 			len += (4*NFSX_UNSIGNED + nlen + rem + NFSX_FH
   1686 				+ NFSX_NQFATTR);
   1687 			if (len > cnt) {
   1688 				eofflag = 0;
   1689 				break;
   1690 			}
   1691 			/*
   1692 			 * Build the directory record xdr from
   1693 			 * the dirent entry.
   1694 			 */
   1695 			nfsm_clget;
   1696 			*tl = nfs_true;
   1697 			bp += NFSX_UNSIGNED;
   1698 
   1699 			/*
   1700 			 * For readdir_and_lookup copy the stuff out.
   1701 			 */
   1702 			xfer = sizeof (struct flrep);
   1703 			cp = (caddr_t)&fl;
   1704 			while (xfer > 0) {
   1705 				nfsm_clget;
   1706 				if ((bp+xfer) > be)
   1707 					tsiz = be-bp;
   1708 				else
   1709 					tsiz = xfer;
   1710 				bcopy(cp, bp, tsiz);
   1711 				bp += tsiz;
   1712 				xfer -= tsiz;
   1713 				if (xfer > 0)
   1714 					cp += tsiz;
   1715 			}
   1716 			nfsm_clget;
   1717 			*tl = txdr_unsigned(dp->d_fileno);
   1718 			bp += NFSX_UNSIGNED;
   1719 			nfsm_clget;
   1720 			*tl = txdr_unsigned(nlen);
   1721 			bp += NFSX_UNSIGNED;
   1722 
   1723 			/* And loop around copying the name */
   1724 			xfer = nlen;
   1725 			cp = dp->d_name;
   1726 			while (xfer > 0) {
   1727 				nfsm_clget;
   1728 				if ((bp+xfer) > be)
   1729 					tsiz = be-bp;
   1730 				else
   1731 					tsiz = xfer;
   1732 				bcopy(cp, bp, tsiz);
   1733 				bp += tsiz;
   1734 				xfer -= tsiz;
   1735 				if (xfer > 0)
   1736 					cp += tsiz;
   1737 			}
   1738 			/* And null pad to a long boundary */
   1739 			for (i = 0; i < rem; i++)
   1740 				*bp++ = '\0';
   1741 			nfsm_clget;
   1742 
   1743 			/* Finish off the record */
   1744 			toff += dp->d_reclen;
   1745 			*tl = txdr_unsigned(toff);
   1746 			bp += NFSX_UNSIGNED;
   1747 		} else
   1748 invalid:
   1749 			toff += dp->d_reclen;
   1750 		cpos += dp->d_reclen;
   1751 		dp = (struct dirent *)cpos;
   1752 	}
   1753 	vrele(vp);
   1754 	nfsm_clget;
   1755 	*tl = nfs_false;
   1756 	bp += NFSX_UNSIGNED;
   1757 	nfsm_clget;
   1758 	if (eofflag)
   1759 		*tl = nfs_true;
   1760 	else
   1761 		*tl = nfs_false;
   1762 	bp += NFSX_UNSIGNED;
   1763 	if (mp != mb) {
   1764 		if (bp < be)
   1765 			mp->m_len = bp - mtod(mp, caddr_t);
   1766 	} else
   1767 		mp->m_len += bp - bpos;
   1768 	FREE(rbuf, M_TEMP);
   1769 	nfsm_srvdone;
   1770 }
   1771 
   1772 /*
   1773  * nfs statfs service
   1774  */
   1775 nfsrv_statfs(nfsd, mrep, md, dpos, cred, nam, mrq)
   1776 	struct nfsd *nfsd;
   1777 	struct mbuf *mrep, *md;
   1778 	caddr_t dpos;
   1779 	struct ucred *cred;
   1780 	struct mbuf *nam, **mrq;
   1781 {
   1782 	register struct statfs *sf;
   1783 	register struct nfsv2_statfs *sfp;
   1784 	register u_long *tl;
   1785 	register long t1;
   1786 	caddr_t bpos;
   1787 	int error = 0, rdonly, cache, isnq;
   1788 	char *cp2;
   1789 	struct mbuf *mb, *mb2, *mreq;
   1790 	struct vnode *vp;
   1791 	nfsv2fh_t nfh;
   1792 	fhandle_t *fhp;
   1793 	struct statfs statfs;
   1794 	u_quad_t frev;
   1795 
   1796 	fhp = &nfh.fh_generic;
   1797 	isnq = (nfsd->nd_nqlflag != NQL_NOVAL);
   1798 	nfsm_srvmtofh(fhp);
   1799 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
   1800 		nfsm_reply(0);
   1801 	sf = &statfs;
   1802 	error = VFS_STATFS(vp->v_mount, sf, nfsd->nd_procp);
   1803 	vput(vp);
   1804 	nfsm_reply(NFSX_STATFS(isnq));
   1805 	nfsm_build(sfp, struct nfsv2_statfs *, NFSX_STATFS(isnq));
   1806 	sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
   1807 	sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
   1808 	sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
   1809 	sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
   1810 	sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
   1811 	if (isnq) {
   1812 		sfp->sf_files = txdr_unsigned(sf->f_files);
   1813 		sfp->sf_ffree = txdr_unsigned(sf->f_ffree);
   1814 	}
   1815 	nfsm_srvdone;
   1816 }
   1817 
   1818 /*
   1819  * Null operation, used by clients to ping server
   1820  */
   1821 /* ARGSUSED */
   1822 nfsrv_null(nfsd, mrep, md, dpos, cred, nam, mrq)
   1823 	struct nfsd *nfsd;
   1824 	struct mbuf *mrep, *md;
   1825 	caddr_t dpos;
   1826 	struct ucred *cred;
   1827 	struct mbuf *nam, **mrq;
   1828 {
   1829 	caddr_t bpos;
   1830 	int error = VNOVAL, cache;
   1831 	struct mbuf *mb, *mreq;
   1832 	u_quad_t frev;
   1833 
   1834 	nfsm_reply(0);
   1835 	return (error);
   1836 }
   1837 
   1838 /*
   1839  * No operation, used for obsolete procedures
   1840  */
   1841 /* ARGSUSED */
   1842 nfsrv_noop(nfsd, mrep, md, dpos, cred, nam, mrq)
   1843 	struct nfsd *nfsd;
   1844 	struct mbuf *mrep, *md;
   1845 	caddr_t dpos;
   1846 	struct ucred *cred;
   1847 	struct mbuf *nam, **mrq;
   1848 {
   1849 	caddr_t bpos;
   1850 	int error, cache;
   1851 	struct mbuf *mb, *mreq;
   1852 	u_quad_t frev;
   1853 
   1854 	if (nfsd->nd_repstat)
   1855 		error = nfsd->nd_repstat;
   1856 	else
   1857 		error = EPROCUNAVAIL;
   1858 	nfsm_reply(0);
   1859 	return (error);
   1860 }
   1861 
   1862 /*
   1863  * Perform access checking for vnodes obtained from file handles that would
   1864  * refer to files already opened by a Unix client. You cannot just use
   1865  * vn_writechk() and VOP_ACCESS() for two reasons.
   1866  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
   1867  * 2 - The owner is to be given access irrespective of mode bits so that
   1868  *     processes that chmod after opening a file don't break. I don't like
   1869  *     this because it opens a security hole, but since the nfs server opens
   1870  *     a security hole the size of a barn door anyhow, what the heck.
   1871  */
   1872 nfsrv_access(vp, flags, cred, rdonly, p)
   1873 	register struct vnode *vp;
   1874 	int flags;
   1875 	register struct ucred *cred;
   1876 	int rdonly;
   1877 	struct proc *p;
   1878 {
   1879 	struct vattr vattr;
   1880 	int error;
   1881 	if (flags & VWRITE) {
   1882 		/* Just vn_writechk() changed to check rdonly */
   1883 		/*
   1884 		 * Disallow write attempts on read-only file systems;
   1885 		 * unless the file is a socket or a block or character
   1886 		 * device resident on the file system.
   1887 		 */
   1888 		if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
   1889 			switch (vp->v_type) {
   1890 			case VREG: case VDIR: case VLNK:
   1891 				return (EROFS);
   1892 			}
   1893 		}
   1894 		/*
   1895 		 * If there's shared text associated with
   1896 		 * the inode, try to free it up once.  If
   1897 		 * we fail, we can't allow writing.
   1898 		 */
   1899 		if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
   1900 			return (ETXTBSY);
   1901 	}
   1902 	if (error = VOP_GETATTR(vp, &vattr, cred, p))
   1903 		return (error);
   1904 	if ((error = VOP_ACCESS(vp, flags, cred, p)) &&
   1905 	    cred->cr_uid != vattr.va_uid)
   1906 		return (error);
   1907 	return (0);
   1908 }
   1909