Home | History | Annotate | Line # | Download | only in nfs
nfs_serv.c revision 1.4
      1 /*
      2  * Copyright (c) 1989 The Regents of the University of California.
      3  * 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  *	from: @(#)nfs_serv.c	7.40 (Berkeley) 5/15/91
     37  *	$Id: nfs_serv.c,v 1.4 1993/07/16 00:50:30 cgd Exp $
     38  */
     39 
     40 /*
     41  * nfs version 2 server calls to vnode ops
     42  * - these routines generally have 3 phases
     43  *   1 - break down and validate rpc request in mbuf list
     44  *   2 - do the vnode ops for the request
     45  *       (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
     46  *   3 - build the rpc reply in an mbuf list
     47  *   nb:
     48  *	- do not mix the phases, since the nfsm_?? macros can return failures
     49  *	  on a bad rpc or similar and do not do any vrele() or vput()'s
     50  *
     51  *      - the nfsm_reply() macro generates an nfs rpc reply with the nfs
     52  *	error number iff error != 0 whereas
     53  *	returning an error from the server function implies a fatal error
     54  *	such as a badly constructed rpc request that should be dropped without
     55  *	a reply.
     56  */
     57 
     58 #include "param.h"
     59 #include "proc.h"
     60 #include "file.h"
     61 #include "namei.h"
     62 #include "vnode.h"
     63 #include "mount.h"
     64 #include "mbuf.h"
     65 
     66 #include "../ufs/quota.h"
     67 #include "../ufs/inode.h"
     68 #include "../ufs/dir.h"
     69 
     70 #include "nfsv2.h"
     71 #include "nfs.h"
     72 #include "xdr_subs.h"
     73 #include "nfsm_subs.h"
     74 
     75 /* Defs */
     76 #define	TRUE	1
     77 #define	FALSE	0
     78 
     79 /* Global vars */
     80 extern u_long nfs_procids[NFS_NPROCS];
     81 extern u_long nfs_xdrneg1;
     82 extern u_long nfs_false, nfs_true;
     83 nfstype nfs_type[9]={ NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
     84 		      NFCHR, NFNON };
     85 
     86 int	nfsrv_null(),
     87 	nfsrv_getattr(),
     88 	nfsrv_setattr(),
     89 	nfsrv_lookup(),
     90 	nfsrv_readlink(),
     91 	nfsrv_read(),
     92 	nfsrv_write(),
     93 	nfsrv_create(),
     94 	nfsrv_remove(),
     95 	nfsrv_rename(),
     96 	nfsrv_link(),
     97 	nfsrv_symlink(),
     98 	nfsrv_mkdir(),
     99 	nfsrv_rmdir(),
    100 	nfsrv_readdir(),
    101 	nfsrv_statfs(),
    102 	nfsrv_noop();
    103 
    104 int (*nfsrv_procs[NFS_NPROCS])() = {
    105 	nfsrv_null,
    106 	nfsrv_getattr,
    107 	nfsrv_setattr,
    108 	nfsrv_noop,
    109 	nfsrv_lookup,
    110 	nfsrv_readlink,
    111 	nfsrv_read,
    112 	nfsrv_noop,
    113 	nfsrv_write,
    114 	nfsrv_create,
    115 	nfsrv_remove,
    116 	nfsrv_rename,
    117 	nfsrv_link,
    118 	nfsrv_symlink,
    119 	nfsrv_mkdir,
    120 	nfsrv_rmdir,
    121 	nfsrv_readdir,
    122 	nfsrv_statfs,
    123 };
    124 /*
    125  * nfs getattr service
    126  */
    127 nfsrv_getattr(mrep, md, dpos, cred, xid, mrq, repstat, p)
    128 	struct mbuf **mrq;
    129 	struct mbuf *mrep, *md;
    130 	caddr_t dpos;
    131 	struct ucred *cred;
    132 	u_long xid;
    133 	int *repstat;
    134 	struct proc *p;
    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;
    146 	char *cp2;
    147 	struct mbuf *mb, *mb2, *mreq;
    148 
    149 	fhp = &nfh.fh_generic;
    150 	nfsm_srvmtofh(fhp);
    151 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred))
    152 		nfsm_reply(0);
    153 	error = VOP_GETATTR(vp, vap, cred, p);
    154 	vput(vp);
    155 	nfsm_reply(NFSX_FATTR);
    156 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
    157 	nfsm_srvfillattr;
    158 	nfsm_srvdone;
    159 }
    160 
    161 /*
    162  * nfs setattr service
    163  */
    164 nfsrv_setattr(mrep, md, dpos, cred, xid, mrq, repstat, p)
    165 	struct mbuf **mrq;
    166 	struct mbuf *mrep, *md;
    167 	caddr_t dpos;
    168 	struct ucred *cred;
    169 	u_long xid;
    170 	int *repstat;
    171 	struct proc *p;
    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;
    184 	char *cp2;
    185 	struct mbuf *mb, *mb2, *mreq;
    186 
    187 	fhp = &nfh.fh_generic;
    188 	nfsm_srvmtofh(fhp);
    189 	nfsm_disect(sp, struct nfsv2_sattr *, NFSX_SATTR);
    190 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred))
    191 		nfsm_reply(0);
    192 	if (error = nfsrv_access(vp, VWRITE, cred, p))
    193 		goto out;
    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 (sp->sa_size != nfs_xdrneg1)
    209 		vap->va_size = fxdr_unsigned(u_long, sp->sa_size);
    210 	/*
    211 	 * The usec field of sa_atime is overloaded with the va_flags field
    212 	 * for 4.4BSD clients. Hopefully other clients always set both the
    213 	 * sec and usec fields to -1 when not setting the atime.
    214 	 */
    215 	if (sp->sa_atime.tv_sec != nfs_xdrneg1) {
    216 		vap->va_atime.tv_sec = fxdr_unsigned(long, sp->sa_atime.tv_sec);
    217 		vap->va_atime.tv_usec = 0;
    218 	}
    219 	if (sp->sa_atime.tv_usec != nfs_xdrneg1)
    220 		vap->va_flags = fxdr_unsigned(u_long, sp->sa_atime.tv_usec);
    221 	if (sp->sa_mtime.tv_sec != nfs_xdrneg1)
    222 		fxdr_time(&sp->sa_mtime, &vap->va_mtime);
    223 	if (error = VOP_SETATTR(vp, vap, cred, p)) {
    224 		vput(vp);
    225 		nfsm_reply(0);
    226 	}
    227 	error = VOP_GETATTR(vp, vap, cred, p);
    228 out:
    229 	vput(vp);
    230 	nfsm_reply(NFSX_FATTR);
    231 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
    232 	nfsm_srvfillattr;
    233 	nfsm_srvdone;
    234 }
    235 
    236 /*
    237  * nfs lookup rpc
    238  */
    239 nfsrv_lookup(mrep, md, dpos, cred, xid, mrq, repstat, p)
    240 	struct mbuf **mrq;
    241 	struct mbuf *mrep, *md;
    242 	caddr_t dpos;
    243 	struct ucred *cred;
    244 	u_long xid;
    245 	int *repstat;
    246 	struct proc *p;
    247 {
    248 	register struct nfsv2_fattr *fp;
    249 	struct nameidata nd;
    250 	struct vnode *vp;
    251 	nfsv2fh_t nfh;
    252 	fhandle_t *fhp;
    253 	register caddr_t cp;
    254 	register u_long *tl;
    255 	register long t1;
    256 	caddr_t bpos;
    257 	int error = 0;
    258 	char *cp2;
    259 	struct mbuf *mb, *mb2, *mreq;
    260 	long len;
    261 	struct vattr va, *vap = &va;
    262 
    263 	fhp = &nfh.fh_generic;
    264 	nfsm_srvmtofh(fhp);
    265 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    266 	nd.ni_cred = cred;
    267 	nd.ni_nameiop = LOOKUP | LOCKLEAF;
    268 	if (error = nfs_namei(&nd, fhp, len, &md, &dpos, p))
    269 		nfsm_reply(0);
    270 	vp = nd.ni_vp;
    271 	bzero((caddr_t)fhp, sizeof(nfh));
    272 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
    273 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
    274 		vput(vp);
    275 		nfsm_reply(0);
    276 	}
    277 	error = VOP_GETATTR(vp, vap, cred, p);
    278 	vput(vp);
    279 	nfsm_reply(NFSX_FH+NFSX_FATTR);
    280 	nfsm_srvfhtom(fhp);
    281 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
    282 	nfsm_srvfillattr;
    283 	nfsm_srvdone;
    284 }
    285 
    286 /*
    287  * nfs readlink service
    288  */
    289 nfsrv_readlink(mrep, md, dpos, cred, xid, mrq, repstat, p)
    290 	struct mbuf **mrq;
    291 	struct mbuf *mrep, *md;
    292 	caddr_t dpos;
    293 	struct ucred *cred;
    294 	u_long xid;
    295 	int *repstat;
    296 	struct proc *p;
    297 {
    298 	struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
    299 	register struct iovec *ivp = iv;
    300 	register struct mbuf *mp;
    301 	register u_long *tl;
    302 	register long t1;
    303 	caddr_t bpos;
    304 	int error = 0;
    305 	char *cp2;
    306 	struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
    307 	struct vnode *vp;
    308 	nfsv2fh_t nfh;
    309 	fhandle_t *fhp;
    310 	struct uio io, *uiop = &io;
    311 	int i, tlen, len;
    312 
    313 	fhp = &nfh.fh_generic;
    314 	nfsm_srvmtofh(fhp);
    315 	len = 0;
    316 	i = 0;
    317 	while (len < NFS_MAXPATHLEN) {
    318 		MGET(mp, M_WAIT, MT_DATA);
    319 		MCLGET(mp, M_WAIT);
    320 		mp->m_len = NFSMSIZ(mp);
    321 		if (len == 0)
    322 			mp3 = mp2 = mp;
    323 		else {
    324 			mp2->m_next = mp;
    325 			mp2 = mp;
    326 		}
    327 		if ((len+mp->m_len) > NFS_MAXPATHLEN) {
    328 			mp->m_len = NFS_MAXPATHLEN-len;
    329 			len = NFS_MAXPATHLEN;
    330 		} else
    331 			len += mp->m_len;
    332 		ivp->iov_base = mtod(mp, caddr_t);
    333 		ivp->iov_len = mp->m_len;
    334 		i++;
    335 		ivp++;
    336 	}
    337 	uiop->uio_iov = iv;
    338 	uiop->uio_iovcnt = i;
    339 	uiop->uio_offset = 0;
    340 	uiop->uio_resid = len;
    341 	uiop->uio_rw = UIO_READ;
    342 	uiop->uio_segflg = UIO_SYSSPACE;
    343 	uiop->uio_procp = (struct proc *)0;
    344 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred)) {
    345 		m_freem(mp3);
    346 		nfsm_reply(0);
    347 	}
    348 	if (vp->v_type != VLNK) {
    349 		error = EINVAL;
    350 		goto out;
    351 	}
    352 	error = VOP_READLINK(vp, uiop, cred);
    353 out:
    354 	vput(vp);
    355 	if (error)
    356 		m_freem(mp3);
    357 	nfsm_reply(NFSX_UNSIGNED);
    358 	if (uiop->uio_resid > 0) {
    359 		len -= uiop->uio_resid;
    360 		tlen = nfsm_rndup(len);
    361 		nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
    362 	}
    363 	nfsm_build(tl, u_long *, NFSX_UNSIGNED);
    364 	*tl = txdr_unsigned(len);
    365 	mb->m_next = mp3;
    366 	nfsm_srvdone;
    367 }
    368 
    369 /*
    370  * nfs read service
    371  */
    372 nfsrv_read(mrep, md, dpos, cred, xid, mrq, repstat, p)
    373 	struct mbuf **mrq;
    374 	struct mbuf *mrep, *md;
    375 	caddr_t dpos;
    376 	struct ucred *cred;
    377 	u_long xid;
    378 	int *repstat;
    379 	struct proc *p;
    380 {
    381 	register struct iovec *iv;
    382 	struct iovec *iv2;
    383 	register struct mbuf *m;
    384 	register struct nfsv2_fattr *fp;
    385 	register u_long *tl;
    386 	register long t1;
    387 	caddr_t bpos;
    388 	int error = 0;
    389 	char *cp2;
    390 	struct mbuf *mb, *mb2, *mreq;
    391 	struct mbuf *m2, *m3;
    392 	struct vnode *vp;
    393 	nfsv2fh_t nfh;
    394 	fhandle_t *fhp;
    395 	struct uio io, *uiop = &io;
    396 	struct vattr va, *vap = &va;
    397 	int i, cnt, len, left, siz, tlen;
    398 	off_t off;
    399 
    400 	fhp = &nfh.fh_generic;
    401 	nfsm_srvmtofh(fhp);
    402 	nfsm_disect(tl, u_long *, NFSX_UNSIGNED);
    403 	off = fxdr_unsigned(off_t, *tl);
    404 	nfsm_srvstrsiz(cnt, NFS_MAXDATA);
    405 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred))
    406 		nfsm_reply(0);
    407 	if ((error = nfsrv_access(vp, VREAD, cred, p)) &&
    408 		(error = nfsrv_access(vp, VEXEC, cred, p))) {
    409 		vput(vp);
    410 		nfsm_reply(0);
    411 	}
    412 	len = left = cnt;
    413 	/*
    414 	 * Generate the mbuf list with the uio_iov ref. to it.
    415 	 */
    416 	i = 0;
    417 	m3 = (struct mbuf *)0;
    418 #ifdef lint
    419 	m2 = (struct mbuf *)0;
    420 #endif /* lint */
    421 	MALLOC(iv, struct iovec *,
    422 	       ((NFS_MAXDATA+MLEN-1)/MLEN) * sizeof (struct iovec), M_TEMP,
    423 	       M_WAITOK);
    424 	iv2 = iv;
    425 	while (left > 0) {
    426 		MGET(m, M_WAIT, MT_DATA);
    427 		if (left > MINCLSIZE)
    428 			MCLGET(m, M_WAIT);
    429 		m->m_len = 0;
    430 		siz = min(M_TRAILINGSPACE(m), left);
    431 		m->m_len = siz;
    432 		iv->iov_base = mtod(m, caddr_t);
    433 		iv->iov_len = siz;
    434 		iv++;
    435 		i++;
    436 		left -= siz;
    437 		if (m3) {
    438 			m2->m_next = m;
    439 			m2 = m;
    440 		} else
    441 			m3 = m2 = m;
    442 	}
    443 	uiop->uio_iov = iv2;
    444 	uiop->uio_iovcnt = i;
    445 	uiop->uio_offset = off;
    446 	uiop->uio_resid = cnt;
    447 	uiop->uio_rw = UIO_READ;
    448 	uiop->uio_segflg = UIO_SYSSPACE;
    449 	uiop->uio_procp = (struct proc *)0;
    450 	error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
    451 	off = uiop->uio_offset;
    452 	FREE((caddr_t)iv2, M_TEMP);
    453 	if (error) {
    454 		m_freem(m3);
    455 		vput(vp);
    456 		nfsm_reply(0);
    457 	}
    458 	if (error = VOP_GETATTR(vp, vap, cred, p))
    459 		m_freem(m3);
    460 	vput(vp);
    461 	nfsm_reply(NFSX_FATTR+NFSX_UNSIGNED);
    462 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
    463 	nfsm_srvfillattr;
    464 	len -= uiop->uio_resid;
    465 	if (len > 0) {
    466 		tlen = nfsm_rndup(len);
    467 		if (cnt != tlen || tlen != len)
    468 			nfsm_adj(m3, cnt-tlen, tlen-len);
    469 	} else {
    470 		m_freem(m3);
    471 		m3 = (struct mbuf *)0;
    472 	}
    473 	nfsm_build(tl, u_long *, NFSX_UNSIGNED);
    474 	*tl = txdr_unsigned(len);
    475 	mb->m_next = m3;
    476 	nfsm_srvdone;
    477 }
    478 
    479 /*
    480  * nfs write service
    481  */
    482 nfsrv_write(mrep, md, dpos, cred, xid, mrq, repstat, p)
    483 	struct mbuf *mrep, *md, **mrq;
    484 	caddr_t dpos;
    485 	struct ucred *cred;
    486 	u_long xid;
    487 	int *repstat;
    488 	struct proc *p;
    489 {
    490 	register struct iovec *ivp;
    491 	register struct mbuf *mp;
    492 	register struct nfsv2_fattr *fp;
    493 	struct iovec iv[NFS_MAXIOVEC];
    494 	struct vattr va;
    495 	register struct vattr *vap = &va;
    496 	register u_long *tl;
    497 	register long t1;
    498 	caddr_t bpos;
    499 	int error = 0;
    500 	char *cp2;
    501 	struct mbuf *mb, *mb2, *mreq;
    502 	struct vnode *vp;
    503 	nfsv2fh_t nfh;
    504 	fhandle_t *fhp;
    505 	struct uio io, *uiop = &io;
    506 	off_t off;
    507 	long siz, len, xfer;
    508 
    509 	fhp = &nfh.fh_generic;
    510 	nfsm_srvmtofh(fhp);
    511 	nfsm_disect(tl, u_long *, 4*NFSX_UNSIGNED);
    512 	off = fxdr_unsigned(off_t, *++tl);
    513 	tl += 2;
    514 	len = fxdr_unsigned(long, *tl);
    515 	if (len > NFS_MAXDATA || len <= 0) {
    516 		error = EBADRPC;
    517 		nfsm_reply(0);
    518 	}
    519 	if (dpos == (mtod(md, caddr_t)+md->m_len)) {
    520 		mp = md->m_next;
    521 		if (mp == NULL) {
    522 			error = EBADRPC;
    523 			nfsm_reply(0);
    524 		}
    525 	} else {
    526 		mp = md;
    527 		siz = dpos-mtod(mp, caddr_t);
    528 		mp->m_len -= siz;
    529 		NFSMADV(mp, siz);
    530 	}
    531 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred))
    532 		nfsm_reply(0);
    533 	if (error = nfsrv_access(vp, VWRITE, cred, p)) {
    534 		vput(vp);
    535 		nfsm_reply(0);
    536 	}
    537 	uiop->uio_resid = 0;
    538 	uiop->uio_rw = UIO_WRITE;
    539 	uiop->uio_segflg = UIO_SYSSPACE;
    540 	uiop->uio_procp = (struct proc *)0;
    541 	/*
    542 	 * Do up to NFS_MAXIOVEC mbufs of write each iteration of the
    543 	 * loop until done.
    544 	 */
    545 	while (len > 0 && uiop->uio_resid == 0) {
    546 		ivp = iv;
    547 		siz = 0;
    548 		uiop->uio_iov = ivp;
    549 		uiop->uio_iovcnt = 0;
    550 		uiop->uio_offset = off;
    551 		while (len > 0 && uiop->uio_iovcnt < NFS_MAXIOVEC && mp != NULL) {
    552 			ivp->iov_base = mtod(mp, caddr_t);
    553 			if (len < mp->m_len)
    554 				ivp->iov_len = xfer = len;
    555 			else
    556 				ivp->iov_len = xfer = mp->m_len;
    557 #ifdef notdef
    558 			/* Not Yet .. */
    559 			if (M_HASCL(mp) && (((u_long)ivp->iov_base) & CLOFSET) == 0)
    560 				ivp->iov_op = NULL;	/* what should it be ?? */
    561 			else
    562 				ivp->iov_op = NULL;
    563 #endif
    564 			uiop->uio_iovcnt++;
    565 			ivp++;
    566 			len -= xfer;
    567 			siz += xfer;
    568 			mp = mp->m_next;
    569 		}
    570 		if (len > 0 && mp == NULL) {
    571 			error = EBADRPC;
    572 			vput(vp);
    573 			nfsm_reply(0);
    574 		}
    575 		uiop->uio_resid = siz;
    576 		if (error = VOP_WRITE(vp, uiop, IO_SYNC | IO_NODELOCKED,
    577 			cred)) {
    578 			vput(vp);
    579 			nfsm_reply(0);
    580 		}
    581 		off = uiop->uio_offset;
    582 	}
    583 	error = VOP_GETATTR(vp, vap, cred, p);
    584 	vput(vp);
    585 	nfsm_reply(NFSX_FATTR);
    586 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
    587 	nfsm_srvfillattr;
    588 	nfsm_srvdone;
    589 }
    590 
    591 /*
    592  * nfs create service
    593  * if it already exists, just set length		* 28 Aug 92*
    594  * do NOT truncate unconditionally !
    595  */
    596 nfsrv_create(mrep, md, dpos, cred, xid, mrq, repstat, p)
    597 	struct mbuf *mrep, *md, **mrq;
    598 	caddr_t dpos;
    599 	struct ucred *cred;
    600 	u_long xid;
    601 	int *repstat;
    602 	struct proc *p;
    603 {
    604 	register struct nfsv2_fattr *fp;
    605 	struct vattr va;
    606 	register struct vattr *vap = &va;
    607 	struct nameidata nd;
    608 	register caddr_t cp;
    609 	register u_long *tl;
    610 	register long t1;
    611 	caddr_t bpos;
    612 	long rdev;
    613 	int error = 0;
    614 	char *cp2;
    615 	struct mbuf *mb, *mb2, *mreq;
    616 	struct vnode *vp;
    617 	nfsv2fh_t nfh;
    618 	fhandle_t *fhp;
    619 	long len;
    620 
    621 	nd.ni_nameiop = 0;
    622 	fhp = &nfh.fh_generic;
    623 	nfsm_srvmtofh(fhp);
    624 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    625 	nd.ni_cred = cred;
    626 	nd.ni_nameiop = CREATE | LOCKPARENT | LOCKLEAF | SAVESTART;
    627 	if (error = nfs_namei(&nd, fhp, len, &md, &dpos, p))
    628 		nfsm_reply(0);
    629 	VATTR_NULL(vap);
    630 	nfsm_disect(tl, u_long *, NFSX_SATTR);
    631 	/*
    632 	 * If it doesn't exist, create it		* 28 Aug 92*
    633 	 * otherwise just set length from attributes
    634 	 *   should I set the mode too ??
    635 	 */
    636 	if (nd.ni_vp == NULL) {
    637 		vap->va_type = IFTOVT(fxdr_unsigned(u_long, *tl));
    638 		if (vap->va_type == VNON)
    639 			vap->va_type = VREG;
    640 		vap->va_mode = nfstov_mode(*tl);
    641 		rdev = fxdr_unsigned(long, *(tl+3));
    642 		if (vap->va_type == VREG || vap->va_type == VSOCK) {
    643 			vrele(nd.ni_startdir);
    644 			if (error = VOP_CREATE(&nd, vap, p))
    645 				nfsm_reply(0);
    646 			FREE(nd.ni_pnbuf, M_NAMEI);
    647 		} else if (vap->va_type == VCHR || vap->va_type == VBLK ||
    648 			vap->va_type == VFIFO) {
    649 			if (vap->va_type == VCHR && rdev == 0xffffffff)
    650 				vap->va_type = VFIFO;
    651 			if (vap->va_type == VFIFO) {
    652 #ifndef FIFO
    653 				VOP_ABORTOP(&nd);
    654 				vput(nd.ni_dvp);
    655 				error = ENXIO;
    656 				goto out;
    657 #endif /* FIFO */
    658 			} else if (error = suser(cred, (short *)0)) {
    659 				VOP_ABORTOP(&nd);
    660 				vput(nd.ni_dvp);
    661 				goto out;
    662 			} else
    663 				vap->va_rdev = (dev_t)rdev;
    664 			if (error = VOP_MKNOD(&nd, vap, cred, p)) {
    665 				vrele(nd.ni_startdir);
    666 				nfsm_reply(0);
    667 			}
    668 			nd.ni_nameiop &= ~(OPMASK | LOCKPARENT | SAVESTART);
    669 			nd.ni_nameiop |= LOOKUP;
    670 			if (error = lookup(&nd, p)) {
    671 				free(nd.ni_pnbuf, M_NAMEI);
    672 				nfsm_reply(0);
    673 			}
    674 			FREE(nd.ni_pnbuf, M_NAMEI);
    675 			if (nd.ni_more) {
    676 				vrele(nd.ni_dvp);
    677 				vput(nd.ni_vp);
    678 				VOP_ABORTOP(&nd);
    679 				error = EINVAL;
    680 				nfsm_reply(0);
    681 			}
    682 		} else {
    683 			VOP_ABORTOP(&nd);
    684 			vput(nd.ni_dvp);
    685 			error = ENXIO;
    686 			goto out;
    687 		}
    688 		vp = nd.ni_vp;
    689 	} else {
    690 		vrele(nd.ni_startdir);
    691 		free(nd.ni_pnbuf, M_NAMEI);
    692 		vp = nd.ni_vp;
    693 		if (nd.ni_dvp == vp)
    694 			vrele(nd.ni_dvp);
    695 		else
    696 			vput(nd.ni_dvp);
    697 		VOP_ABORTOP(&nd);
    698 		vap->va_size = fxdr_unsigned(long, *(tl+3));	/* 28 Aug 92*/
    699 /* 08 Sep 92*/	if (vap->va_size != -1 && (error = VOP_SETATTR(vp, vap, cred, p))) {
    700 			vput(vp);
    701 			nfsm_reply(0);
    702 		}
    703 	}
    704 	bzero((caddr_t)fhp, sizeof(nfh));
    705 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
    706 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
    707 		vput(vp);
    708 		nfsm_reply(0);
    709 	}
    710 	error = VOP_GETATTR(vp, vap, cred, p);
    711 	vput(vp);
    712 	nfsm_reply(NFSX_FH+NFSX_FATTR);
    713 	nfsm_srvfhtom(fhp);
    714 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
    715 	nfsm_srvfillattr;
    716 	return (error);
    717 nfsmout:
    718 	if (nd.ni_nameiop)
    719 		vrele(nd.ni_startdir);
    720 	VOP_ABORTOP(&nd);
    721 	if (nd.ni_dvp == nd.ni_vp)
    722 		vrele(nd.ni_dvp);
    723 	else
    724 		vput(nd.ni_dvp);
    725 	if (nd.ni_vp)
    726 		vput(nd.ni_vp);
    727 	return (error);
    728 
    729 out:
    730 	vrele(nd.ni_startdir);
    731 	free(nd.ni_pnbuf, M_NAMEI);
    732 	nfsm_reply(0);
    733 }
    734 
    735 /*
    736  * nfs remove service
    737  */
    738 nfsrv_remove(mrep, md, dpos, cred, xid, mrq, repstat, p)
    739 	struct mbuf *mrep, *md, **mrq;
    740 	caddr_t dpos;
    741 	struct ucred *cred;
    742 	u_long xid;
    743 	int *repstat;
    744 	struct proc *p;
    745 {
    746 	struct nameidata nd;
    747 	register u_long *tl;
    748 	register long t1;
    749 	caddr_t bpos;
    750 	int error = 0;
    751 	char *cp2;
    752 	struct mbuf *mb, *mreq;
    753 	struct vnode *vp;
    754 	nfsv2fh_t nfh;
    755 	fhandle_t *fhp;
    756 	long len;
    757 
    758 	fhp = &nfh.fh_generic;
    759 	nfsm_srvmtofh(fhp);
    760 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    761 	nd.ni_cred = cred;
    762 	nd.ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
    763 	if (error = nfs_namei(&nd, fhp, len, &md, &dpos, p))
    764 		nfsm_reply(0);
    765 	vp = nd.ni_vp;
    766 	if (vp->v_type == VDIR &&
    767 		(error = suser(cred, (short *)0)))
    768 		goto out;
    769 	/*
    770 	 * The root of a mounted filesystem cannot be deleted.
    771 	 */
    772 	if (vp->v_flag & VROOT) {
    773 		error = EBUSY;
    774 		goto out;
    775 	}
    776 	if (vp->v_flag & VTEXT)
    777 		(void) vnode_pager_uncache(vp);
    778 out:
    779 	if (!error) {
    780 		error = VOP_REMOVE(&nd, p);
    781 	} else {
    782 		VOP_ABORTOP(&nd);
    783 		if (nd.ni_dvp == vp)
    784 			vrele(nd.ni_dvp);
    785 		else
    786 			vput(nd.ni_dvp);
    787 		vput(vp);
    788 	}
    789 	nfsm_reply(0);
    790 	nfsm_srvdone;
    791 }
    792 
    793 /*
    794  * nfs rename service
    795  */
    796 nfsrv_rename(mrep, md, dpos, cred, xid, mrq, repstat, p)
    797 	struct mbuf *mrep, *md, **mrq;
    798 	caddr_t dpos;
    799 	struct ucred *cred;
    800 	u_long xid;
    801 	int *repstat;
    802 	struct proc *p;
    803 {
    804 	register u_long *tl;
    805 	register long t1;
    806 	caddr_t bpos;
    807 	int error = 0;
    808 	char *cp2;
    809 	struct mbuf *mb, *mreq;
    810 	struct nameidata fromnd, tond;
    811 	struct vnode *fvp, *tvp, *tdvp;
    812 	nfsv2fh_t fnfh, tnfh;
    813 	fhandle_t *ffhp, *tfhp;
    814 	long len, len2;
    815 	int rootflg = 0;
    816 
    817 	ffhp = &fnfh.fh_generic;
    818 	tfhp = &tnfh.fh_generic;
    819 	fromnd.ni_nameiop = 0;
    820 	tond.ni_nameiop = 0;
    821 	nfsm_srvmtofh(ffhp);
    822 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    823 	/*
    824 	 * Remember if we are root so that we can reset cr_uid before
    825 	 * the second nfs_namei() call
    826 	 */
    827 	if (cred->cr_uid == 0)
    828 		rootflg++;
    829 	fromnd.ni_cred = cred;
    830 	fromnd.ni_nameiop = DELETE | WANTPARENT | SAVESTART;
    831 	if (error = nfs_namei(&fromnd, ffhp, len, &md, &dpos, p))
    832 		nfsm_reply(0);
    833 	fvp = fromnd.ni_vp;
    834 	nfsm_srvmtofh(tfhp);
    835 	nfsm_strsiz(len2, NFS_MAXNAMLEN);
    836 	if (rootflg)
    837 		cred->cr_uid = 0;
    838 	tond.ni_cred = cred;
    839 	tond.ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE
    840 		| SAVESTART;
    841 	if (error = nfs_namei(&tond, tfhp, len2, &md, &dpos, p)) {
    842 		VOP_ABORTOP(&fromnd);
    843 		vrele(fromnd.ni_dvp);
    844 		vrele(fvp);
    845 		goto out1;
    846 	}
    847 	tdvp = tond.ni_dvp;
    848 	tvp = tond.ni_vp;
    849 	if (tvp != NULL) {
    850 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
    851 			error = EISDIR;
    852 			goto out;
    853 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
    854 			error = ENOTDIR;
    855 			goto out;
    856 		}
    857 	}
    858 	if (fvp->v_mount != tdvp->v_mount) {
    859 		error = EXDEV;
    860 		goto out;
    861 	}
    862 	if (fvp == tdvp)
    863 		error = EINVAL;
    864 	/*
    865 	 * If source is the same as the destination (that is the
    866 	 * same vnode with the same name in the same directory),
    867 	 * then there is nothing to do.
    868 	 */
    869 	if (fvp == tvp && fromnd.ni_dvp == tdvp &&
    870 	    fromnd.ni_namelen == tond.ni_namelen &&
    871 	    !bcmp(fromnd.ni_ptr, tond.ni_ptr, fromnd.ni_namelen))
    872 		error = -1;
    873 out:
    874 	if (!error) {
    875 		error = VOP_RENAME(&fromnd, &tond, p);
    876 	} else {
    877 		VOP_ABORTOP(&tond);
    878 		if (tdvp == tvp)
    879 			vrele(tdvp);
    880 		else
    881 			vput(tdvp);
    882 		if (tvp)
    883 			vput(tvp);
    884 		VOP_ABORTOP(&fromnd);
    885 		vrele(fromnd.ni_dvp);
    886 		vrele(fvp);
    887 	}
    888 	vrele(tond.ni_startdir);
    889 	FREE(tond.ni_pnbuf, M_NAMEI);
    890 out1:
    891 	vrele(fromnd.ni_startdir);
    892 	FREE(fromnd.ni_pnbuf, M_NAMEI);
    893 	nfsm_reply(0);
    894 	return (error);
    895 
    896 nfsmout:
    897 	if (tond.ni_nameiop) {
    898 		vrele(tond.ni_startdir);
    899 		FREE(tond.ni_pnbuf, M_NAMEI);
    900 	}
    901 	if (fromnd.ni_nameiop) {
    902 		vrele(fromnd.ni_startdir);
    903 		FREE(fromnd.ni_pnbuf, M_NAMEI);
    904 		VOP_ABORTOP(&fromnd);
    905 		vrele(fromnd.ni_dvp);
    906 		vrele(fvp);
    907 	}
    908 	return (error);
    909 }
    910 
    911 /*
    912  * nfs link service
    913  */
    914 nfsrv_link(mrep, md, dpos, cred, xid, mrq, repstat, p)
    915 	struct mbuf *mrep, *md, **mrq;
    916 	caddr_t dpos;
    917 	struct ucred *cred;
    918 	u_long xid;
    919 	int *repstat;
    920 	struct proc *p;
    921 {
    922 	struct nameidata nd;
    923 	register u_long *tl;
    924 	register long t1;
    925 	caddr_t bpos;
    926 	int error = 0;
    927 	char *cp2;
    928 	struct mbuf *mb, *mreq;
    929 	struct vnode *vp, *xp;
    930 	nfsv2fh_t nfh, dnfh;
    931 	fhandle_t *fhp, *dfhp;
    932 	long len;
    933 
    934 	fhp = &nfh.fh_generic;
    935 	dfhp = &dnfh.fh_generic;
    936 	nfsm_srvmtofh(fhp);
    937 	nfsm_srvmtofh(dfhp);
    938 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
    939 	if (error = nfsrv_fhtovp(fhp, FALSE, &vp, cred))
    940 		nfsm_reply(0);
    941 	if (vp->v_type == VDIR && (error = suser(cred, NULL)))
    942 		goto out1;
    943 	nd.ni_cred = cred;
    944 	nd.ni_nameiop = CREATE | LOCKPARENT;
    945 	if (error = nfs_namei(&nd, dfhp, len, &md, &dpos, p))
    946 		goto out1;
    947 	xp = nd.ni_vp;
    948 	if (xp != NULL) {
    949 		error = EEXIST;
    950 		goto out;
    951 	}
    952 	xp = nd.ni_dvp;
    953 	if (vp->v_mount != xp->v_mount)
    954 		error = EXDEV;
    955 out:
    956 	if (!error) {
    957 		error = VOP_LINK(vp, &nd, p);
    958 	} else {
    959 		VOP_ABORTOP(&nd);
    960 		if (nd.ni_dvp == nd.ni_vp)
    961 			vrele(nd.ni_dvp);
    962 		else
    963 			vput(nd.ni_dvp);
    964 		if (nd.ni_vp)
    965 			vrele(nd.ni_vp);
    966 	}
    967 out1:
    968 	vrele(vp);
    969 	nfsm_reply(0);
    970 	nfsm_srvdone;
    971 }
    972 
    973 /*
    974  * nfs symbolic link service
    975  */
    976 nfsrv_symlink(mrep, md, dpos, cred, xid, mrq, repstat, p)
    977 	struct mbuf *mrep, *md, **mrq;
    978 	caddr_t dpos;
    979 	struct ucred *cred;
    980 	u_long xid;
    981 	int *repstat;
    982 	struct proc *p;
    983 {
    984 	struct vattr va;
    985 	struct nameidata nd;
    986 	register struct vattr *vap = &va;
    987 	register u_long *tl;
    988 	register long t1;
    989 	struct nfsv2_sattr *sp;
    990 	caddr_t bpos;
    991 	struct uio io;
    992 	struct iovec iv;
    993 	int error = 0;
    994 	char *pathcp, *cp2;
    995 	struct mbuf *mb, *mreq;
    996 	nfsv2fh_t nfh;
    997 	fhandle_t *fhp;
    998 	long len, len2;
    999 
   1000 	pathcp = (char *)0;
   1001 	fhp = &nfh.fh_generic;
   1002 	nfsm_srvmtofh(fhp);
   1003 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1004 	nd.ni_cred = cred;
   1005 	nd.ni_nameiop = CREATE | LOCKPARENT;
   1006 	if (error = nfs_namei(&nd, fhp, len, &md, &dpos, p))
   1007 		goto out;
   1008 	nfsm_strsiz(len2, NFS_MAXPATHLEN);
   1009 	MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
   1010 	iv.iov_base = pathcp;
   1011 	iv.iov_len = len2;
   1012 	io.uio_resid = len2;
   1013 	io.uio_offset = 0;
   1014 	io.uio_iov = &iv;
   1015 	io.uio_iovcnt = 1;
   1016 	io.uio_segflg = UIO_SYSSPACE;
   1017 	io.uio_rw = UIO_READ;
   1018 	io.uio_procp = (struct proc *)0;
   1019 	nfsm_mtouio(&io, len2);
   1020 	nfsm_disect(sp, struct nfsv2_sattr *, NFSX_SATTR);
   1021 	*(pathcp + len2) = '\0';
   1022 	if (nd.ni_vp) {
   1023 		VOP_ABORTOP(&nd);
   1024 		if (nd.ni_dvp == nd.ni_vp)
   1025 			vrele(nd.ni_dvp);
   1026 		else
   1027 			vput(nd.ni_dvp);
   1028 		vrele(nd.ni_vp);
   1029 		error = EEXIST;
   1030 		goto out;
   1031 	}
   1032 	VATTR_NULL(vap);
   1033 	vap->va_mode = fxdr_unsigned(u_short, sp->sa_mode);
   1034 	error = VOP_SYMLINK(&nd, vap, pathcp, p);
   1035 out:
   1036 	if (pathcp)
   1037 		FREE(pathcp, M_TEMP);
   1038 	nfsm_reply(0);
   1039 	return (error);
   1040 nfsmout:
   1041 	VOP_ABORTOP(&nd);
   1042 	if (nd.ni_dvp == nd.ni_vp)
   1043 		vrele(nd.ni_dvp);
   1044 	else
   1045 		vput(nd.ni_dvp);
   1046 	if (nd.ni_vp)
   1047 		vrele(nd.ni_vp);
   1048 	if (pathcp)
   1049 		FREE(pathcp, M_TEMP);
   1050 	return (error);
   1051 }
   1052 
   1053 /*
   1054  * nfs mkdir service
   1055  */
   1056 nfsrv_mkdir(mrep, md, dpos, cred, xid, mrq, repstat, p)
   1057 	struct mbuf *mrep, *md, **mrq;
   1058 	caddr_t dpos;
   1059 	struct ucred *cred;
   1060 	u_long xid;
   1061 	int *repstat;
   1062 	struct proc *p;
   1063 {
   1064 	struct vattr va;
   1065 	register struct vattr *vap = &va;
   1066 	register struct nfsv2_fattr *fp;
   1067 	struct nameidata nd;
   1068 	register caddr_t cp;
   1069 	register u_long *tl;
   1070 	register long t1;
   1071 	caddr_t bpos;
   1072 	int error = 0;
   1073 	char *cp2;
   1074 	struct mbuf *mb, *mb2, *mreq;
   1075 	struct vnode *vp;
   1076 	nfsv2fh_t nfh;
   1077 	fhandle_t *fhp;
   1078 	long len;
   1079 
   1080 	fhp = &nfh.fh_generic;
   1081 	nfsm_srvmtofh(fhp);
   1082 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1083 	nd.ni_cred = cred;
   1084 	nd.ni_nameiop = CREATE | LOCKPARENT;
   1085 	if (error = nfs_namei(&nd, fhp, len, &md, &dpos, p))
   1086 		nfsm_reply(0);
   1087 	nfsm_disect(tl, u_long *, NFSX_UNSIGNED);
   1088 	VATTR_NULL(vap);
   1089 	vap->va_type = VDIR;
   1090 	vap->va_mode = nfstov_mode(*tl++);
   1091 	vp = nd.ni_vp;
   1092 	if (vp != NULL) {
   1093 		VOP_ABORTOP(&nd);
   1094 		if (nd.ni_dvp == vp)
   1095 			vrele(nd.ni_dvp);
   1096 		else
   1097 			vput(nd.ni_dvp);
   1098 		vrele(vp);
   1099 		error = EEXIST;
   1100 		nfsm_reply(0);
   1101 	}
   1102 	if (error = VOP_MKDIR(&nd, vap, p))
   1103 		nfsm_reply(0);
   1104 	vp = nd.ni_vp;
   1105 	bzero((caddr_t)fhp, sizeof(nfh));
   1106 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
   1107 	if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
   1108 		vput(vp);
   1109 		nfsm_reply(0);
   1110 	}
   1111 	error = VOP_GETATTR(vp, vap, cred, p);
   1112 	vput(vp);
   1113 	nfsm_reply(NFSX_FH+NFSX_FATTR);
   1114 	nfsm_srvfhtom(fhp);
   1115 	nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR);
   1116 	nfsm_srvfillattr;
   1117 	return (error);
   1118 nfsmout:
   1119 	VOP_ABORTOP(&nd);
   1120 	if (nd.ni_dvp == nd.ni_vp)
   1121 		vrele(nd.ni_dvp);
   1122 	else
   1123 		vput(nd.ni_dvp);
   1124 	if (nd.ni_vp)
   1125 		vrele(nd.ni_vp);
   1126 	return (error);
   1127 }
   1128 
   1129 /*
   1130  * nfs rmdir service
   1131  */
   1132 nfsrv_rmdir(mrep, md, dpos, cred, xid, mrq, repstat, p)
   1133 	struct mbuf *mrep, *md, **mrq;
   1134 	caddr_t dpos;
   1135 	struct ucred *cred;
   1136 	u_long xid;
   1137 	int *repstat;
   1138 	struct proc *p;
   1139 {
   1140 	register u_long *tl;
   1141 	register long t1;
   1142 	caddr_t bpos;
   1143 	int error = 0;
   1144 	char *cp2;
   1145 	struct mbuf *mb, *mreq;
   1146 	struct vnode *vp;
   1147 	nfsv2fh_t nfh;
   1148 	fhandle_t *fhp;
   1149 	long len;
   1150 	struct nameidata nd;
   1151 
   1152 	fhp = &nfh.fh_generic;
   1153 	nfsm_srvmtofh(fhp);
   1154 	nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
   1155 	nd.ni_cred = cred;
   1156 	nd.ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
   1157 	if (error = nfs_namei(&nd, fhp, len, &md, &dpos, p))
   1158 		nfsm_reply(0);
   1159 	vp = nd.ni_vp;
   1160 	if (vp->v_type != VDIR) {
   1161 		error = ENOTDIR;
   1162 		goto out;
   1163 	}
   1164 	/*
   1165 	 * No rmdir "." please.
   1166 	 */
   1167 	if (nd.ni_dvp == vp) {
   1168 		error = EINVAL;
   1169 		goto out;
   1170 	}
   1171 	/*
   1172 	 * The root of a mounted filesystem cannot be deleted.
   1173 	 */
   1174 	if (vp->v_flag & VROOT)
   1175 		error = EBUSY;
   1176 out:
   1177 	if (!error) {
   1178 		error = VOP_RMDIR(&nd, p);
   1179 	} else {
   1180 		VOP_ABORTOP(&nd);
   1181 		if (nd.ni_dvp == nd.ni_vp)
   1182 			vrele(nd.ni_dvp);
   1183 		else
   1184 			vput(nd.ni_dvp);
   1185 		vput(vp);
   1186 	}
   1187 	nfsm_reply(0);
   1188 	nfsm_srvdone;
   1189 }
   1190 
   1191 /*
   1192  * nfs readdir service
   1193  * - mallocs what it thinks is enough to read
   1194  *	count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
   1195  * - calls VOP_READDIR()
   1196  * - loops around building the reply
   1197  *	if the output generated exceeds count break out of loop
   1198  *	The nfsm_clget macro is used here so that the reply will be packed
   1199  *	tightly in mbuf clusters.
   1200  * - it only knows that it has encountered eof when the VOP_READDIR()
   1201  *	reads nothing
   1202  * - as such one readdir rpc will return eof false although you are there
   1203  *	and then the next will return eof
   1204  * - it trims out records with d_ino == 0
   1205  *	this doesn't matter for Unix clients, but they might confuse clients
   1206  *	for other os'.
   1207  * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
   1208  *	than requested, but this may not apply to all filesystems. For
   1209  *	example, client NFS does not { although it is never remote mounted
   1210  *	anyhow }
   1211  * PS: The NFS protocol spec. does not clarify what the "count" byte
   1212  *	argument is a count of.. just name strings and file id's or the
   1213  *	entire reply rpc or ...
   1214  *	I tried just file name and id sizes and it confused the Sun client,
   1215  *	so I am using the full rpc size now. The "paranoia.." comment refers
   1216  *	to including the status longwords that are not a part of the dir.
   1217  *	"entry" structures, but are in the rpc.
   1218  */
   1219 nfsrv_readdir(mrep, md, dpos, cred, xid, mrq, repstat, p)
   1220 	struct mbuf **mrq;
   1221 	struct mbuf *mrep, *md;
   1222 	caddr_t dpos;
   1223 	struct ucred *cred;
   1224 	u_long xid;
   1225 	int *repstat;
   1226 	struct proc *p;
   1227 {
   1228 	register char *bp, *be;
   1229 	register struct mbuf *mp;
   1230 	register struct direct *dp;
   1231 	register caddr_t cp;
   1232 	register u_long *tl;
   1233 	register long t1;
   1234 	caddr_t bpos;
   1235 	int error = 0;
   1236 	char *cp2;
   1237 	struct mbuf *mb, *mb2, *mreq;
   1238 	char *cpos, *cend;
   1239 	int len, nlen, rem, xfer, tsiz, i;
   1240 	struct vnode *vp;
   1241 	struct mbuf *mp2, *mp3;
   1242 	nfsv2fh_t nfh;
   1243 	fhandle_t *fhp;
   1244 	struct uio io;
   1245 	struct iovec iv;
   1246 	int siz, cnt, fullsiz, eofflag;
   1247 	u_long on;
   1248 	char *rbuf;
   1249 	off_t off, toff;
   1250 
   1251 	fhp = &nfh.fh_generic;
   1252 	nfsm_srvmtofh(fhp);
   1253 	nfsm_disect(tl, u_long *, 2*NFSX_UNSIGNED);
   1254 	toff = fxdr_unsigned(off_t, *tl++);
   1255 	off = (toff & ~(NFS_DIRBLKSIZ-1));
   1256 	on = (toff & (NFS_DIRBLKSIZ-1));
   1257 	cnt = fxdr_unsigned(int, *tl);
   1258 	siz = ((cnt+NFS_DIRBLKSIZ-1) & ~(NFS_DIRBLKSIZ-1));
   1259 	if (cnt > NFS_MAXREADDIR)
   1260 		siz = NFS_MAXREADDIR;
   1261 	fullsiz = siz;
   1262 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred))
   1263 		nfsm_reply(0);
   1264 	if (error = nfsrv_access(vp, VEXEC, cred, p)) {
   1265 		vput(vp);
   1266 		nfsm_reply(0);
   1267 	}
   1268 	VOP_UNLOCK(vp);
   1269 	MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
   1270 again:
   1271 	iv.iov_base = rbuf;
   1272 	iv.iov_len = fullsiz;
   1273 	io.uio_iov = &iv;
   1274 	io.uio_iovcnt = 1;
   1275 	io.uio_offset = off;
   1276 	io.uio_resid = fullsiz;
   1277 	io.uio_segflg = UIO_SYSSPACE;
   1278 	io.uio_rw = UIO_READ;
   1279 	io.uio_procp = (struct proc *)0;
   1280 	error = VOP_READDIR(vp, &io, cred, &eofflag);
   1281 	off = io.uio_offset;
   1282 	if (error) {
   1283 		vrele(vp);
   1284 		free((caddr_t)rbuf, M_TEMP);
   1285 		nfsm_reply(0);
   1286 	}
   1287 	if (io.uio_resid) {
   1288 		siz -= io.uio_resid;
   1289 
   1290 		/*
   1291 		 * If nothing read, return eof
   1292 		 * rpc reply
   1293 		 */
   1294 		if (siz == 0) {
   1295 			vrele(vp);
   1296 			nfsm_reply(2*NFSX_UNSIGNED);
   1297 			nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
   1298 			*tl++ = nfs_false;
   1299 			*tl = nfs_true;
   1300 			FREE((caddr_t)rbuf, M_TEMP);
   1301 			return (0);
   1302 		}
   1303 	}
   1304 
   1305 	/*
   1306 	 * Check for degenerate cases of nothing useful read.
   1307 	 * If so go try again
   1308 	 */
   1309 	cpos = rbuf + on;
   1310 	cend = rbuf + siz;
   1311 	dp = (struct direct *)cpos;
   1312 	while (cpos < cend && dp->d_ino == 0) {
   1313 		cpos += dp->d_reclen;
   1314 		dp = (struct direct *)cpos;
   1315 	}
   1316 	if (cpos >= cend) {
   1317 		toff = off;
   1318 		siz = fullsiz;
   1319 		on = 0;
   1320 		goto again;
   1321 	}
   1322 
   1323 	cpos = rbuf + on;
   1324 	cend = rbuf + siz;
   1325 	dp = (struct direct *)cpos;
   1326 	vrele(vp);
   1327 	len = 3*NFSX_UNSIGNED;	/* paranoia, probably can be 0 */
   1328 	bp = be = (caddr_t)0;
   1329 	mp3 = (struct mbuf *)0;
   1330 	nfsm_reply(siz);
   1331 
   1332 	/* Loop through the records and build reply */
   1333 	while (cpos < cend) {
   1334 		if (dp->d_ino != 0) {
   1335 			nlen = dp->d_namlen;
   1336 			rem = nfsm_rndup(nlen)-nlen;
   1337 
   1338 			/*
   1339 			 * As noted above, the NFS spec. is not clear about what
   1340 			 * should be included in "count" as totalled up here in
   1341 			 * "len".
   1342 			 */
   1343 			len += (4*NFSX_UNSIGNED+nlen+rem);
   1344 			if (len > cnt) {
   1345 				eofflag = 0;
   1346 				break;
   1347 			}
   1348 
   1349 			/* Build the directory record xdr from the direct entry */
   1350 			nfsm_clget;
   1351 			*tl = nfs_true;
   1352 			bp += NFSX_UNSIGNED;
   1353 			nfsm_clget;
   1354 			*tl = txdr_unsigned(dp->d_ino);
   1355 			bp += NFSX_UNSIGNED;
   1356 			nfsm_clget;
   1357 			*tl = txdr_unsigned(nlen);
   1358 			bp += NFSX_UNSIGNED;
   1359 
   1360 			/* And loop arround copying the name */
   1361 			xfer = nlen;
   1362 			cp = dp->d_name;
   1363 			while (xfer > 0) {
   1364 				nfsm_clget;
   1365 				if ((bp+xfer) > be)
   1366 					tsiz = be-bp;
   1367 				else
   1368 					tsiz = xfer;
   1369 				bcopy(cp, bp, tsiz);
   1370 				bp += tsiz;
   1371 				xfer -= tsiz;
   1372 				if (xfer > 0)
   1373 					cp += tsiz;
   1374 			}
   1375 			/* And null pad to a long boundary */
   1376 			for (i = 0; i < rem; i++)
   1377 				*bp++ = '\0';
   1378 			nfsm_clget;
   1379 
   1380 			/* Finish off the record */
   1381 			toff += dp->d_reclen;
   1382 			*tl = txdr_unsigned(toff);
   1383 			bp += NFSX_UNSIGNED;
   1384 		} else
   1385 			toff += dp->d_reclen;
   1386 		cpos += dp->d_reclen;
   1387 		dp = (struct direct *)cpos;
   1388 	}
   1389 	nfsm_clget;
   1390 	*tl = nfs_false;
   1391 	bp += NFSX_UNSIGNED;
   1392 	nfsm_clget;
   1393 	if (eofflag)
   1394 		*tl = nfs_true;
   1395 	else
   1396 		*tl = nfs_false;
   1397 	bp += NFSX_UNSIGNED;
   1398 	if (bp < be)
   1399 		mp->m_len = bp-mtod(mp, caddr_t);
   1400 	mb->m_next = mp3;
   1401 	FREE(rbuf, M_TEMP);
   1402 	nfsm_srvdone;
   1403 }
   1404 
   1405 /*
   1406  * nfs statfs service
   1407  */
   1408 nfsrv_statfs(mrep, md, dpos, cred, xid, mrq, repstat, p)
   1409 	struct mbuf **mrq;
   1410 	struct mbuf *mrep, *md;
   1411 	caddr_t dpos;
   1412 	struct ucred *cred;
   1413 	u_long xid;
   1414 	int *repstat;
   1415 	struct proc *p;
   1416 {
   1417 	register struct statfs *sf;
   1418 	register struct nfsv2_statfs *sfp;
   1419 	register u_long *tl;
   1420 	register long t1;
   1421 	caddr_t bpos;
   1422 	int error = 0;
   1423 	char *cp2;
   1424 	struct mbuf *mb, *mb2, *mreq;
   1425 	struct vnode *vp;
   1426 	nfsv2fh_t nfh;
   1427 	fhandle_t *fhp;
   1428 	struct statfs statfs;
   1429 
   1430 	fhp = &nfh.fh_generic;
   1431 	nfsm_srvmtofh(fhp);
   1432 	if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred))
   1433 		nfsm_reply(0);
   1434 	sf = &statfs;
   1435 	error = VFS_STATFS(vp->v_mount, sf, p);
   1436 	vput(vp);
   1437 	nfsm_reply(NFSX_STATFS);
   1438 	nfsm_build(sfp, struct nfsv2_statfs *, NFSX_STATFS);
   1439 	sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
   1440 	sfp->sf_bsize = txdr_unsigned(sf->f_fsize);
   1441 	sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
   1442 	sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
   1443 	sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
   1444 	nfsm_srvdone;
   1445 }
   1446 
   1447 /*
   1448  * Null operation, used by clients to ping server
   1449  */
   1450 /* ARGSUSED */
   1451 nfsrv_null(mrep, md, dpos, cred, xid, mrq, repstat, p)
   1452 	struct mbuf **mrq;
   1453 	struct mbuf *mrep, *md;
   1454 	caddr_t dpos;
   1455 	struct ucred *cred;
   1456 	u_long xid;
   1457 	int *repstat;
   1458 	struct proc *p;
   1459 {
   1460 	caddr_t bpos;
   1461 	int error = 0;
   1462 	struct mbuf *mb, *mreq;
   1463 
   1464 	error = VNOVAL;
   1465 	nfsm_reply(0);
   1466 	return (error);
   1467 }
   1468 
   1469 /*
   1470  * No operation, used for obsolete procedures
   1471  */
   1472 /* ARGSUSED */
   1473 nfsrv_noop(mrep, md, dpos, cred, xid, mrq, repstat, p)
   1474 	struct mbuf **mrq;
   1475 	struct mbuf *mrep, *md;
   1476 	caddr_t dpos;
   1477 	struct ucred *cred;
   1478 	u_long xid;
   1479 	int *repstat;
   1480 	struct proc *p;
   1481 {
   1482 	caddr_t bpos;
   1483 	int error;					/* 08 Sep 92*/
   1484 	struct mbuf *mb, *mreq;
   1485 
   1486 	if (*repstat)					/* 08 Sep 92*/
   1487 		error = *repstat;
   1488 	else
   1489 		error = EPROCUNAVAIL;
   1490 	nfsm_reply(0);
   1491 	return (error);
   1492 }
   1493 
   1494 /*
   1495  * Perform access checking for vnodes obtained from file handles that would
   1496  * refer to files already opened by a Unix client. You cannot just use
   1497  * vn_writechk() and VOP_ACCESS() for two reasons.
   1498  * 1 - You must check for MNT_EXRDONLY as well as MNT_RDONLY for the write case
   1499  * 2 - The owner is to be given access irrespective of mode bits so that
   1500  *     processes that chmod after opening a file don't break. I don't like
   1501  *     this because it opens a security hole, but since the nfs server opens
   1502  *     a security hole the size of a barn door anyhow, what the heck.
   1503  */
   1504 nfsrv_access(vp, flags, cred, p)
   1505 	register struct vnode *vp;
   1506 	int flags;
   1507 	register struct ucred *cred;
   1508 	struct proc *p;
   1509 {
   1510 	struct vattr vattr;
   1511 	int error;
   1512 	if (flags & VWRITE) {
   1513 		/* Just vn_writechk() changed to check MNT_EXRDONLY */
   1514 		/*
   1515 		 * Disallow write attempts on read-only file systems;
   1516 		 * unless the file is a socket or a block or character
   1517 		 * device resident on the file system.
   1518 		 */
   1519 		if (vp->v_mount->mnt_flag & (MNT_RDONLY | MNT_EXRDONLY)) {
   1520 			switch (vp->v_type) {
   1521 			case VREG: case VDIR: case VLNK:
   1522 				return (EROFS);
   1523 			}
   1524 		}
   1525 		/*
   1526 		 * If there's shared text associated with
   1527 		 * the inode, try to free it up once.  If
   1528 		 * we fail, we can't allow writing.
   1529 		 */
   1530 		if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
   1531 			return (ETXTBSY);
   1532 	}
   1533 	if (error = VOP_GETATTR(vp, &vattr, cred, p))
   1534 		return (error);
   1535 	if ((error = VOP_ACCESS(vp, flags, cred, p)) &&
   1536 	    cred->cr_uid != vattr.va_uid)
   1537 		return (error);
   1538 	return (0);
   1539 }
   1540