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