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