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