Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.207
      1  1.207  christos /*	$NetBSD: genfs_vnops.c,v 1.207 2020/05/20 17:06:15 christos Exp $	*/
      2  1.164        ad 
      3  1.164        ad /*-
      4  1.164        ad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.164        ad  * All rights reserved.
      6  1.164        ad  *
      7  1.164        ad  * Redistribution and use in source and binary forms, with or without
      8  1.164        ad  * modification, are permitted provided that the following conditions
      9  1.164        ad  * are met:
     10  1.164        ad  * 1. Redistributions of source code must retain the above copyright
     11  1.164        ad  *    notice, this list of conditions and the following disclaimer.
     12  1.164        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.164        ad  *    notice, this list of conditions and the following disclaimer in the
     14  1.164        ad  *    documentation and/or other materials provided with the distribution.
     15  1.164        ad  *
     16  1.164        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.164        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.164        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.164        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.164        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.164        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.164        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.164        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.164        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.164        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.164        ad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.164        ad  */
     28    1.6      fvdl 
     29    1.6      fvdl /*
     30    1.6      fvdl  * Copyright (c) 1982, 1986, 1989, 1993
     31    1.6      fvdl  *	The Regents of the University of California.  All rights reserved.
     32    1.6      fvdl  *
     33    1.6      fvdl  * Redistribution and use in source and binary forms, with or without
     34    1.6      fvdl  * modification, are permitted provided that the following conditions
     35    1.6      fvdl  * are met:
     36    1.6      fvdl  * 1. Redistributions of source code must retain the above copyright
     37    1.6      fvdl  *    notice, this list of conditions and the following disclaimer.
     38    1.6      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     39    1.6      fvdl  *    notice, this list of conditions and the following disclaimer in the
     40    1.6      fvdl  *    documentation and/or other materials provided with the distribution.
     41   1.81       agc  * 3. Neither the name of the University nor the names of its contributors
     42    1.6      fvdl  *    may be used to endorse or promote products derived from this software
     43    1.6      fvdl  *    without specific prior written permission.
     44    1.6      fvdl  *
     45    1.6      fvdl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     46    1.6      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47    1.6      fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48    1.6      fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     49    1.6      fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50    1.6      fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51    1.6      fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52    1.6      fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53    1.6      fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54    1.6      fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55    1.6      fvdl  * SUCH DAMAGE.
     56    1.6      fvdl  *
     57    1.6      fvdl  */
     58   1.40     lukem 
     59   1.40     lukem #include <sys/cdefs.h>
     60  1.207  christos __KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.207 2020/05/20 17:06:15 christos Exp $");
     61    1.8   thorpej 
     62    1.1   mycroft #include <sys/param.h>
     63    1.1   mycroft #include <sys/systm.h>
     64    1.6      fvdl #include <sys/proc.h>
     65    1.1   mycroft #include <sys/kernel.h>
     66    1.1   mycroft #include <sys/mount.h>
     67  1.186   hannken #include <sys/fstrans.h>
     68    1.1   mycroft #include <sys/namei.h>
     69  1.193   hannken #include <sys/vnode_impl.h>
     70   1.13  wrstuden #include <sys/fcntl.h>
     71  1.135      yamt #include <sys/kmem.h>
     72    1.3   mycroft #include <sys/poll.h>
     73   1.37       chs #include <sys/mman.h>
     74   1.66  jdolecek #include <sys/file.h>
     75  1.125      elad #include <sys/kauth.h>
     76  1.169      elad #include <sys/stat.h>
     77  1.204  christos #include <sys/extattr.h>
     78    1.1   mycroft 
     79    1.1   mycroft #include <miscfs/genfs/genfs.h>
     80   1.37       chs #include <miscfs/genfs/genfs_node.h>
     81    1.6      fvdl #include <miscfs/specfs/specdev.h>
     82    1.1   mycroft 
     83   1.21       chs #include <uvm/uvm.h>
     84   1.21       chs #include <uvm/uvm_pager.h>
     85   1.21       chs 
     86   1.70  christos static void filt_genfsdetach(struct knote *);
     87   1.70  christos static int filt_genfsread(struct knote *, long);
     88   1.70  christos static int filt_genfsvnode(struct knote *, long);
     89   1.70  christos 
     90    1.1   mycroft int
     91   1.53     enami genfs_poll(void *v)
     92    1.1   mycroft {
     93    1.3   mycroft 	struct vop_poll_args /* {
     94    1.1   mycroft 		struct vnode *a_vp;
     95    1.3   mycroft 		int a_events;
     96  1.116  christos 		struct lwp *a_l;
     97    1.1   mycroft 	} */ *ap = v;
     98    1.1   mycroft 
     99    1.3   mycroft 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
    100    1.1   mycroft }
    101    1.1   mycroft 
    102    1.1   mycroft int
    103   1.53     enami genfs_seek(void *v)
    104    1.4    kleink {
    105    1.4    kleink 	struct vop_seek_args /* {
    106    1.4    kleink 		struct vnode *a_vp;
    107    1.4    kleink 		off_t a_oldoff;
    108    1.4    kleink 		off_t a_newoff;
    109  1.125      elad 		kauth_cred_t cred;
    110    1.4    kleink 	} */ *ap = v;
    111    1.4    kleink 
    112    1.4    kleink 	if (ap->a_newoff < 0)
    113    1.4    kleink 		return (EINVAL);
    114    1.4    kleink 
    115    1.4    kleink 	return (0);
    116    1.4    kleink }
    117    1.4    kleink 
    118    1.4    kleink int
    119   1.53     enami genfs_abortop(void *v)
    120    1.1   mycroft {
    121    1.1   mycroft 	struct vop_abortop_args /* {
    122    1.1   mycroft 		struct vnode *a_dvp;
    123    1.1   mycroft 		struct componentname *a_cnp;
    124    1.1   mycroft 	} */ *ap = v;
    125   1.53     enami 
    126  1.184  dholland 	(void)ap;
    127  1.184  dholland 
    128    1.1   mycroft 	return (0);
    129   1.13  wrstuden }
    130   1.13  wrstuden 
    131   1.13  wrstuden int
    132   1.53     enami genfs_fcntl(void *v)
    133   1.13  wrstuden {
    134   1.13  wrstuden 	struct vop_fcntl_args /* {
    135   1.13  wrstuden 		struct vnode *a_vp;
    136   1.13  wrstuden 		u_int a_command;
    137  1.150  christos 		void *a_data;
    138   1.13  wrstuden 		int a_fflag;
    139  1.125      elad 		kauth_cred_t a_cred;
    140  1.116  christos 		struct lwp *a_l;
    141   1.13  wrstuden 	} */ *ap = v;
    142   1.13  wrstuden 
    143   1.13  wrstuden 	if (ap->a_command == F_SETFL)
    144   1.13  wrstuden 		return (0);
    145   1.13  wrstuden 	else
    146   1.13  wrstuden 		return (EOPNOTSUPP);
    147    1.1   mycroft }
    148    1.1   mycroft 
    149    1.1   mycroft /*ARGSUSED*/
    150    1.1   mycroft int
    151  1.138  christos genfs_badop(void *v)
    152    1.1   mycroft {
    153    1.1   mycroft 
    154    1.1   mycroft 	panic("genfs: bad op");
    155    1.1   mycroft }
    156    1.1   mycroft 
    157    1.1   mycroft /*ARGSUSED*/
    158    1.1   mycroft int
    159  1.138  christos genfs_nullop(void *v)
    160    1.1   mycroft {
    161    1.1   mycroft 
    162    1.1   mycroft 	return (0);
    163   1.10    kleink }
    164   1.10    kleink 
    165   1.10    kleink /*ARGSUSED*/
    166   1.10    kleink int
    167  1.138  christos genfs_einval(void *v)
    168   1.10    kleink {
    169   1.10    kleink 
    170   1.10    kleink 	return (EINVAL);
    171    1.1   mycroft }
    172    1.1   mycroft 
    173   1.12  wrstuden /*
    174   1.74  jdolecek  * Called when an fs doesn't support a particular vop.
    175  1.177     pooka  * This takes care to vrele, vput, or vunlock passed in vnodes
    176  1.177     pooka  * and calls VOP_ABORTOP for a componentname (in non-rename VOP).
    177   1.12  wrstuden  */
    178   1.12  wrstuden int
    179   1.75  jdolecek genfs_eopnotsupp(void *v)
    180   1.12  wrstuden {
    181   1.12  wrstuden 	struct vop_generic_args /*
    182   1.12  wrstuden 		struct vnodeop_desc *a_desc;
    183   1.53     enami 		/ * other random data follows, presumably * /
    184   1.12  wrstuden 	} */ *ap = v;
    185   1.12  wrstuden 	struct vnodeop_desc *desc = ap->a_desc;
    186   1.74  jdolecek 	struct vnode *vp, *vp_last = NULL;
    187  1.177     pooka 	int flags, i, j, offset_cnp, offset_vp;
    188  1.177     pooka 
    189  1.177     pooka 	KASSERT(desc->vdesc_offset != VOP_LOOKUP_DESCOFFSET);
    190  1.177     pooka 	KASSERT(desc->vdesc_offset != VOP_ABORTOP_DESCOFFSET);
    191  1.177     pooka 
    192  1.177     pooka 	/*
    193  1.185  dholland 	 * Abort any componentname that lookup potentially left state in.
    194  1.177     pooka 	 *
    195  1.177     pooka 	 * As is logical, componentnames for VOP_RENAME are handled by
    196  1.177     pooka 	 * the caller of VOP_RENAME.  Yay, rename!
    197  1.177     pooka 	 */
    198  1.177     pooka 	if (desc->vdesc_offset != VOP_RENAME_DESCOFFSET &&
    199  1.177     pooka 	    (offset_vp = desc->vdesc_vp_offsets[0]) != VDESC_NO_OFFSET &&
    200  1.177     pooka 	    (offset_cnp = desc->vdesc_componentname_offset) != VDESC_NO_OFFSET){
    201  1.177     pooka 		struct componentname *cnp;
    202  1.177     pooka 		struct vnode *dvp;
    203  1.177     pooka 
    204  1.177     pooka 		dvp = *VOPARG_OFFSETTO(struct vnode **, offset_vp, ap);
    205  1.177     pooka 		cnp = *VOPARG_OFFSETTO(struct componentname **, offset_cnp, ap);
    206  1.177     pooka 
    207  1.177     pooka 		VOP_ABORTOP(dvp, cnp);
    208  1.177     pooka 	}
    209   1.12  wrstuden 
    210   1.12  wrstuden 	flags = desc->vdesc_flags;
    211   1.12  wrstuden 	for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
    212  1.177     pooka 		if ((offset_vp = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
    213   1.12  wrstuden 			break;	/* stop at end of list */
    214   1.12  wrstuden 		if ((j = flags & VDESC_VP0_WILLPUT)) {
    215  1.177     pooka 			vp = *VOPARG_OFFSETTO(struct vnode **, offset_vp, ap);
    216   1.74  jdolecek 
    217   1.74  jdolecek 			/* Skip if NULL */
    218   1.74  jdolecek 			if (!vp)
    219   1.74  jdolecek 				continue;
    220   1.74  jdolecek 
    221   1.12  wrstuden 			switch (j) {
    222   1.12  wrstuden 			case VDESC_VP0_WILLPUT:
    223   1.74  jdolecek 				/* Check for dvp == vp cases */
    224   1.74  jdolecek 				if (vp == vp_last)
    225   1.74  jdolecek 					vrele(vp);
    226   1.74  jdolecek 				else {
    227   1.74  jdolecek 					vput(vp);
    228   1.74  jdolecek 					vp_last = vp;
    229   1.74  jdolecek 				}
    230   1.12  wrstuden 				break;
    231   1.12  wrstuden 			case VDESC_VP0_WILLRELE:
    232   1.12  wrstuden 				vrele(vp);
    233   1.12  wrstuden 				break;
    234   1.12  wrstuden 			}
    235   1.12  wrstuden 		}
    236   1.12  wrstuden 	}
    237   1.12  wrstuden 
    238   1.12  wrstuden 	return (EOPNOTSUPP);
    239   1.12  wrstuden }
    240   1.12  wrstuden 
    241    1.1   mycroft /*ARGSUSED*/
    242    1.1   mycroft int
    243  1.138  christos genfs_ebadf(void *v)
    244    1.1   mycroft {
    245    1.1   mycroft 
    246    1.1   mycroft 	return (EBADF);
    247    1.9  matthias }
    248    1.9  matthias 
    249    1.9  matthias /* ARGSUSED */
    250    1.9  matthias int
    251  1.138  christos genfs_enoioctl(void *v)
    252    1.9  matthias {
    253    1.9  matthias 
    254   1.51    atatat 	return (EPASSTHROUGH);
    255    1.6      fvdl }
    256    1.6      fvdl 
    257    1.6      fvdl 
    258    1.6      fvdl /*
    259   1.15      fvdl  * Eliminate all activity associated with the requested vnode
    260    1.6      fvdl  * and with all vnodes aliased to the requested vnode.
    261    1.6      fvdl  */
    262    1.6      fvdl int
    263   1.53     enami genfs_revoke(void *v)
    264    1.6      fvdl {
    265    1.6      fvdl 	struct vop_revoke_args /* {
    266    1.6      fvdl 		struct vnode *a_vp;
    267    1.6      fvdl 		int a_flags;
    268    1.6      fvdl 	} */ *ap = v;
    269    1.6      fvdl 
    270    1.6      fvdl #ifdef DIAGNOSTIC
    271    1.6      fvdl 	if ((ap->a_flags & REVOKEALL) == 0)
    272    1.6      fvdl 		panic("genfs_revoke: not revokeall");
    273    1.6      fvdl #endif
    274  1.161        ad 	vrevoke(ap->a_vp);
    275    1.6      fvdl 	return (0);
    276    1.6      fvdl }
    277    1.6      fvdl 
    278    1.6      fvdl /*
    279  1.190   hannken  * Lock the node (for deadfs).
    280  1.190   hannken  */
    281  1.190   hannken int
    282  1.190   hannken genfs_deadlock(void *v)
    283  1.190   hannken {
    284  1.190   hannken 	struct vop_lock_args /* {
    285  1.190   hannken 		struct vnode *a_vp;
    286  1.190   hannken 		int a_flags;
    287  1.190   hannken 	} */ *ap = v;
    288  1.193   hannken 	vnode_t *vp = ap->a_vp;
    289  1.193   hannken 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    290  1.190   hannken 	int flags = ap->a_flags;
    291  1.190   hannken 	krw_t op;
    292  1.196   hannken 
    293  1.196   hannken 	if (! ISSET(flags, LK_RETRY))
    294  1.196   hannken 		return ENOENT;
    295  1.190   hannken 
    296  1.200        ad 	if (ISSET(flags, LK_DOWNGRADE)) {
    297  1.202        ad 		rw_downgrade(&vip->vi_lock);
    298  1.200        ad 	} else if (ISSET(flags, LK_UPGRADE)) {
    299  1.202        ad 		KASSERT(ISSET(flags, LK_NOWAIT));
    300  1.202        ad 		if (!rw_tryupgrade(&vip->vi_lock)) {
    301  1.202        ad 			return EBUSY;
    302  1.200        ad 		}
    303  1.202        ad 	} else if ((flags & (LK_EXCLUSIVE | LK_SHARED)) != 0) {
    304  1.200        ad 		op = (ISSET(flags, LK_EXCLUSIVE) ? RW_WRITER : RW_READER);
    305  1.200        ad 		if (ISSET(flags, LK_NOWAIT)) {
    306  1.202        ad 			if (!rw_tryenter(&vip->vi_lock, op))
    307  1.200        ad 				return EBUSY;
    308  1.200        ad 		} else {
    309  1.202        ad 			rw_enter(&vip->vi_lock, op);
    310  1.200        ad 		}
    311  1.190   hannken 	}
    312  1.196   hannken 	VSTATE_ASSERT_UNLOCKED(vp, VS_RECLAIMED);
    313  1.190   hannken 	return 0;
    314  1.190   hannken }
    315  1.190   hannken 
    316  1.190   hannken /*
    317  1.190   hannken  * Unlock the node (for deadfs).
    318  1.190   hannken  */
    319  1.190   hannken int
    320  1.190   hannken genfs_deadunlock(void *v)
    321  1.190   hannken {
    322  1.190   hannken 	struct vop_unlock_args /* {
    323  1.190   hannken 		struct vnode *a_vp;
    324  1.190   hannken 	} */ *ap = v;
    325  1.193   hannken 	vnode_t *vp = ap->a_vp;
    326  1.193   hannken 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    327  1.190   hannken 
    328  1.202        ad 	rw_exit(&vip->vi_lock);
    329  1.190   hannken 
    330  1.190   hannken 	return 0;
    331  1.190   hannken }
    332  1.190   hannken 
    333  1.190   hannken /*
    334   1.12  wrstuden  * Lock the node.
    335    1.6      fvdl  */
    336    1.6      fvdl int
    337   1.53     enami genfs_lock(void *v)
    338    1.6      fvdl {
    339    1.6      fvdl 	struct vop_lock_args /* {
    340    1.6      fvdl 		struct vnode *a_vp;
    341    1.6      fvdl 		int a_flags;
    342    1.6      fvdl 	} */ *ap = v;
    343  1.193   hannken 	vnode_t *vp = ap->a_vp;
    344  1.193   hannken 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    345  1.163        ad 	int flags = ap->a_flags;
    346  1.182   hannken 	krw_t op;
    347    1.6      fvdl 
    348  1.200        ad 	if (ISSET(flags, LK_DOWNGRADE)) {
    349  1.202        ad 		rw_downgrade(&vip->vi_lock);
    350  1.200        ad 	} else if (ISSET(flags, LK_UPGRADE)) {
    351  1.202        ad 		KASSERT(ISSET(flags, LK_NOWAIT));
    352  1.202        ad 		if (!rw_tryupgrade(&vip->vi_lock)) {
    353  1.202        ad 			return EBUSY;
    354  1.200        ad 		}
    355  1.202        ad 	} else if ((flags & (LK_EXCLUSIVE | LK_SHARED)) != 0) {
    356  1.200        ad 		op = (ISSET(flags, LK_EXCLUSIVE) ? RW_WRITER : RW_READER);
    357  1.200        ad 		if (ISSET(flags, LK_NOWAIT)) {
    358  1.202        ad 			if (!rw_tryenter(&vip->vi_lock, op))
    359  1.200        ad 				return EBUSY;
    360  1.200        ad 		} else {
    361  1.202        ad 			rw_enter(&vip->vi_lock, op);
    362  1.200        ad 		}
    363  1.186   hannken 	}
    364  1.196   hannken 	VSTATE_ASSERT_UNLOCKED(vp, VS_ACTIVE);
    365  1.196   hannken 	return 0;
    366    1.6      fvdl }
    367    1.6      fvdl 
    368    1.6      fvdl /*
    369   1.12  wrstuden  * Unlock the node.
    370    1.6      fvdl  */
    371    1.6      fvdl int
    372   1.53     enami genfs_unlock(void *v)
    373    1.6      fvdl {
    374    1.6      fvdl 	struct vop_unlock_args /* {
    375    1.6      fvdl 		struct vnode *a_vp;
    376    1.6      fvdl 	} */ *ap = v;
    377  1.193   hannken 	vnode_t *vp = ap->a_vp;
    378  1.193   hannken 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    379    1.6      fvdl 
    380  1.202        ad 	rw_exit(&vip->vi_lock);
    381  1.182   hannken 
    382  1.182   hannken 	return 0;
    383    1.6      fvdl }
    384    1.6      fvdl 
    385    1.6      fvdl /*
    386   1.12  wrstuden  * Return whether or not the node is locked.
    387    1.6      fvdl  */
    388    1.6      fvdl int
    389   1.53     enami genfs_islocked(void *v)
    390    1.6      fvdl {
    391    1.6      fvdl 	struct vop_islocked_args /* {
    392    1.6      fvdl 		struct vnode *a_vp;
    393    1.6      fvdl 	} */ *ap = v;
    394  1.193   hannken 	vnode_t *vp = ap->a_vp;
    395  1.193   hannken 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    396    1.6      fvdl 
    397  1.202        ad 	if (rw_write_held(&vip->vi_lock))
    398  1.182   hannken 		return LK_EXCLUSIVE;
    399  1.182   hannken 
    400  1.202        ad 	if (rw_read_held(&vip->vi_lock))
    401  1.182   hannken 		return LK_SHARED;
    402  1.182   hannken 
    403  1.182   hannken 	return 0;
    404   1.12  wrstuden }
    405   1.12  wrstuden 
    406   1.12  wrstuden /*
    407   1.12  wrstuden  * Stubs to use when there is no locking to be done on the underlying object.
    408   1.12  wrstuden  */
    409   1.12  wrstuden int
    410   1.53     enami genfs_nolock(void *v)
    411   1.12  wrstuden {
    412   1.12  wrstuden 
    413   1.12  wrstuden 	return (0);
    414   1.12  wrstuden }
    415   1.12  wrstuden 
    416   1.12  wrstuden int
    417  1.138  christos genfs_nounlock(void *v)
    418   1.12  wrstuden {
    419   1.53     enami 
    420   1.12  wrstuden 	return (0);
    421   1.12  wrstuden }
    422   1.12  wrstuden 
    423   1.12  wrstuden int
    424  1.138  christos genfs_noislocked(void *v)
    425   1.12  wrstuden {
    426   1.53     enami 
    427   1.12  wrstuden 	return (0);
    428    1.8   thorpej }
    429    1.8   thorpej 
    430   1.34       chs int
    431  1.138  christos genfs_mmap(void *v)
    432   1.34       chs {
    433   1.53     enami 
    434   1.53     enami 	return (0);
    435   1.21       chs }
    436   1.21       chs 
    437  1.168     pooka /*
    438  1.168     pooka  * VOP_PUTPAGES() for vnodes which never have pages.
    439  1.168     pooka  */
    440  1.168     pooka 
    441  1.168     pooka int
    442  1.168     pooka genfs_null_putpages(void *v)
    443  1.168     pooka {
    444  1.168     pooka 	struct vop_putpages_args /* {
    445  1.168     pooka 		struct vnode *a_vp;
    446  1.168     pooka 		voff_t a_offlo;
    447  1.168     pooka 		voff_t a_offhi;
    448  1.168     pooka 		int a_flags;
    449  1.168     pooka 	} */ *ap = v;
    450  1.168     pooka 	struct vnode *vp = ap->a_vp;
    451  1.168     pooka 
    452  1.168     pooka 	KASSERT(vp->v_uobj.uo_npages == 0);
    453  1.201        ad 	rw_exit(vp->v_uobj.vmobjlock);
    454  1.168     pooka 	return (0);
    455  1.168     pooka }
    456  1.168     pooka 
    457   1.37       chs void
    458   1.98      yamt genfs_node_init(struct vnode *vp, const struct genfs_ops *ops)
    459   1.37       chs {
    460   1.37       chs 	struct genfs_node *gp = VTOG(vp);
    461   1.37       chs 
    462  1.146        ad 	rw_init(&gp->g_glock);
    463   1.37       chs 	gp->g_op = ops;
    464   1.37       chs }
    465   1.37       chs 
    466   1.37       chs void
    467  1.147        ad genfs_node_destroy(struct vnode *vp)
    468  1.147        ad {
    469  1.147        ad 	struct genfs_node *gp = VTOG(vp);
    470  1.147        ad 
    471  1.147        ad 	rw_destroy(&gp->g_glock);
    472  1.147        ad }
    473  1.147        ad 
    474  1.147        ad void
    475  1.138  christos genfs_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
    476   1.21       chs {
    477   1.21       chs 	int bsize;
    478   1.21       chs 
    479   1.37       chs 	bsize = 1 << vp->v_mount->mnt_fs_bshift;
    480   1.37       chs 	*eobp = (size + bsize - 1) & ~(bsize - 1);
    481   1.43       chs }
    482   1.43       chs 
    483   1.66  jdolecek static void
    484   1.66  jdolecek filt_genfsdetach(struct knote *kn)
    485   1.66  jdolecek {
    486   1.66  jdolecek 	struct vnode *vp = (struct vnode *)kn->kn_hook;
    487   1.66  jdolecek 
    488  1.187     rmind 	mutex_enter(vp->v_interlock);
    489   1.66  jdolecek 	SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
    490  1.187     rmind 	mutex_exit(vp->v_interlock);
    491   1.66  jdolecek }
    492   1.66  jdolecek 
    493   1.66  jdolecek static int
    494   1.66  jdolecek filt_genfsread(struct knote *kn, long hint)
    495   1.66  jdolecek {
    496   1.66  jdolecek 	struct vnode *vp = (struct vnode *)kn->kn_hook;
    497  1.164        ad 	int rv;
    498   1.66  jdolecek 
    499   1.66  jdolecek 	/*
    500   1.66  jdolecek 	 * filesystem is gone, so set the EOF flag and schedule
    501   1.66  jdolecek 	 * the knote for deletion.
    502   1.66  jdolecek 	 */
    503  1.164        ad 	switch (hint) {
    504  1.164        ad 	case NOTE_REVOKE:
    505  1.187     rmind 		KASSERT(mutex_owned(vp->v_interlock));
    506   1.66  jdolecek 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
    507   1.66  jdolecek 		return (1);
    508  1.164        ad 	case 0:
    509  1.187     rmind 		mutex_enter(vp->v_interlock);
    510  1.165        ad 		kn->kn_data = vp->v_size - ((file_t *)kn->kn_obj)->f_offset;
    511  1.164        ad 		rv = (kn->kn_data != 0);
    512  1.187     rmind 		mutex_exit(vp->v_interlock);
    513  1.164        ad 		return rv;
    514  1.164        ad 	default:
    515  1.187     rmind 		KASSERT(mutex_owned(vp->v_interlock));
    516  1.165        ad 		kn->kn_data = vp->v_size - ((file_t *)kn->kn_obj)->f_offset;
    517  1.164        ad 		return (kn->kn_data != 0);
    518   1.66  jdolecek 	}
    519   1.66  jdolecek }
    520   1.66  jdolecek 
    521   1.66  jdolecek static int
    522  1.198  christos filt_genfswrite(struct knote *kn, long hint)
    523  1.198  christos {
    524  1.198  christos 	struct vnode *vp = (struct vnode *)kn->kn_hook;
    525  1.198  christos 
    526  1.198  christos 	/*
    527  1.198  christos 	 * filesystem is gone, so set the EOF flag and schedule
    528  1.198  christos 	 * the knote for deletion.
    529  1.198  christos 	 */
    530  1.198  christos 	switch (hint) {
    531  1.198  christos 	case NOTE_REVOKE:
    532  1.198  christos 		KASSERT(mutex_owned(vp->v_interlock));
    533  1.198  christos 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
    534  1.198  christos 		return (1);
    535  1.198  christos 	case 0:
    536  1.198  christos 		mutex_enter(vp->v_interlock);
    537  1.198  christos 		kn->kn_data = 0;
    538  1.198  christos 		mutex_exit(vp->v_interlock);
    539  1.198  christos 		return 1;
    540  1.198  christos 	default:
    541  1.198  christos 		KASSERT(mutex_owned(vp->v_interlock));
    542  1.198  christos 		kn->kn_data = 0;
    543  1.198  christos 		return 1;
    544  1.198  christos 	}
    545  1.198  christos }
    546  1.198  christos 
    547  1.198  christos static int
    548   1.66  jdolecek filt_genfsvnode(struct knote *kn, long hint)
    549   1.66  jdolecek {
    550  1.164        ad 	struct vnode *vp = (struct vnode *)kn->kn_hook;
    551  1.164        ad 	int fflags;
    552   1.66  jdolecek 
    553  1.164        ad 	switch (hint) {
    554  1.164        ad 	case NOTE_REVOKE:
    555  1.187     rmind 		KASSERT(mutex_owned(vp->v_interlock));
    556   1.66  jdolecek 		kn->kn_flags |= EV_EOF;
    557  1.164        ad 		if ((kn->kn_sfflags & hint) != 0)
    558  1.164        ad 			kn->kn_fflags |= hint;
    559   1.66  jdolecek 		return (1);
    560  1.164        ad 	case 0:
    561  1.187     rmind 		mutex_enter(vp->v_interlock);
    562  1.164        ad 		fflags = kn->kn_fflags;
    563  1.187     rmind 		mutex_exit(vp->v_interlock);
    564  1.164        ad 		break;
    565  1.164        ad 	default:
    566  1.187     rmind 		KASSERT(mutex_owned(vp->v_interlock));
    567  1.164        ad 		if ((kn->kn_sfflags & hint) != 0)
    568  1.164        ad 			kn->kn_fflags |= hint;
    569  1.164        ad 		fflags = kn->kn_fflags;
    570  1.164        ad 		break;
    571   1.66  jdolecek 	}
    572  1.164        ad 
    573  1.165        ad 	return (fflags != 0);
    574   1.66  jdolecek }
    575   1.66  jdolecek 
    576  1.199      maya static const struct filterops genfsread_filtops = {
    577  1.199      maya 	.f_isfd = 1,
    578  1.199      maya 	.f_attach = NULL,
    579  1.199      maya 	.f_detach = filt_genfsdetach,
    580  1.199      maya 	.f_event = filt_genfsread,
    581  1.199      maya };
    582  1.199      maya 
    583  1.199      maya static const struct filterops genfswrite_filtops = {
    584  1.199      maya 	.f_isfd = 1,
    585  1.199      maya 	.f_attach = NULL,
    586  1.199      maya 	.f_detach = filt_genfsdetach,
    587  1.199      maya 	.f_event = filt_genfswrite,
    588  1.199      maya };
    589  1.199      maya 
    590  1.199      maya static const struct filterops genfsvnode_filtops = {
    591  1.199      maya 	.f_isfd = 1,
    592  1.199      maya 	.f_attach = NULL,
    593  1.199      maya 	.f_detach = filt_genfsdetach,
    594  1.199      maya 	.f_event = filt_genfsvnode,
    595  1.199      maya };
    596   1.66  jdolecek 
    597   1.66  jdolecek int
    598   1.66  jdolecek genfs_kqfilter(void *v)
    599   1.66  jdolecek {
    600   1.66  jdolecek 	struct vop_kqfilter_args /* {
    601   1.66  jdolecek 		struct vnode	*a_vp;
    602   1.66  jdolecek 		struct knote	*a_kn;
    603   1.66  jdolecek 	} */ *ap = v;
    604   1.66  jdolecek 	struct vnode *vp;
    605   1.66  jdolecek 	struct knote *kn;
    606   1.66  jdolecek 
    607   1.66  jdolecek 	vp = ap->a_vp;
    608   1.66  jdolecek 	kn = ap->a_kn;
    609   1.66  jdolecek 	switch (kn->kn_filter) {
    610   1.66  jdolecek 	case EVFILT_READ:
    611   1.66  jdolecek 		kn->kn_fop = &genfsread_filtops;
    612   1.66  jdolecek 		break;
    613  1.198  christos 	case EVFILT_WRITE:
    614  1.198  christos 		kn->kn_fop = &genfswrite_filtops;
    615  1.198  christos 		break;
    616   1.66  jdolecek 	case EVFILT_VNODE:
    617   1.66  jdolecek 		kn->kn_fop = &genfsvnode_filtops;
    618   1.66  jdolecek 		break;
    619   1.66  jdolecek 	default:
    620  1.159     pooka 		return (EINVAL);
    621   1.66  jdolecek 	}
    622   1.66  jdolecek 
    623   1.66  jdolecek 	kn->kn_hook = vp;
    624   1.66  jdolecek 
    625  1.187     rmind 	mutex_enter(vp->v_interlock);
    626   1.66  jdolecek 	SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
    627  1.187     rmind 	mutex_exit(vp->v_interlock);
    628   1.66  jdolecek 
    629   1.66  jdolecek 	return (0);
    630    1.1   mycroft }
    631  1.136      yamt 
    632  1.136      yamt void
    633  1.136      yamt genfs_node_wrlock(struct vnode *vp)
    634  1.136      yamt {
    635  1.136      yamt 	struct genfs_node *gp = VTOG(vp);
    636  1.136      yamt 
    637  1.146        ad 	rw_enter(&gp->g_glock, RW_WRITER);
    638  1.136      yamt }
    639  1.136      yamt 
    640  1.136      yamt void
    641  1.136      yamt genfs_node_rdlock(struct vnode *vp)
    642  1.136      yamt {
    643  1.136      yamt 	struct genfs_node *gp = VTOG(vp);
    644  1.136      yamt 
    645  1.146        ad 	rw_enter(&gp->g_glock, RW_READER);
    646  1.136      yamt }
    647  1.136      yamt 
    648  1.176  uebayasi int
    649  1.175  uebayasi genfs_node_rdtrylock(struct vnode *vp)
    650  1.175  uebayasi {
    651  1.175  uebayasi 	struct genfs_node *gp = VTOG(vp);
    652  1.175  uebayasi 
    653  1.176  uebayasi 	return rw_tryenter(&gp->g_glock, RW_READER);
    654  1.175  uebayasi }
    655  1.175  uebayasi 
    656  1.175  uebayasi void
    657  1.136      yamt genfs_node_unlock(struct vnode *vp)
    658  1.136      yamt {
    659  1.136      yamt 	struct genfs_node *gp = VTOG(vp);
    660  1.136      yamt 
    661  1.146        ad 	rw_exit(&gp->g_glock);
    662  1.136      yamt }
    663  1.169      elad 
    664  1.183       chs int
    665  1.183       chs genfs_node_wrlocked(struct vnode *vp)
    666  1.183       chs {
    667  1.183       chs 	struct genfs_node *gp = VTOG(vp);
    668  1.183       chs 
    669  1.183       chs 	return rw_write_held(&gp->g_glock);
    670  1.183       chs }
    671  1.183       chs 
    672  1.204  christos static int
    673  1.204  christos groupmember(gid_t gid, kauth_cred_t cred)
    674  1.204  christos {
    675  1.204  christos 	int ismember;
    676  1.204  christos 	int error = kauth_cred_ismember_gid(cred, gid, &ismember);
    677  1.204  christos 	if (error)
    678  1.204  christos 		return error;
    679  1.204  christos 	if (kauth_cred_getegid(cred) == gid || ismember)
    680  1.204  christos 		return 0;
    681  1.204  christos 	return -1;
    682  1.204  christos }
    683  1.204  christos 
    684  1.169      elad /*
    685  1.204  christos  * Common filesystem object access control check routine.  Accepts a
    686  1.204  christos  * vnode, cred, uid, gid, mode, acl, requested access mode.
    687  1.204  christos  * Returns 0 on success, or an errno on failure.
    688  1.172      elad  */
    689  1.172      elad int
    690  1.204  christos genfs_can_access(vnode_t *vp, kauth_cred_t cred, uid_t file_uid, gid_t file_gid,
    691  1.204  christos     mode_t file_mode, struct acl *acl, accmode_t accmode)
    692  1.172      elad {
    693  1.204  christos 	accmode_t dac_granted;
    694  1.204  christos 	int error;
    695  1.204  christos 
    696  1.204  christos 	KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0);
    697  1.204  christos 	KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE));
    698  1.206  christos 
    699  1.204  christos 	/*
    700  1.204  christos 	 * Look for a normal, non-privileged way to access the file/directory
    701  1.204  christos 	 * as requested.  If it exists, go with that.
    702  1.204  christos 	 */
    703  1.204  christos 
    704  1.204  christos 	dac_granted = 0;
    705  1.172      elad 
    706  1.204  christos 	/* Check the owner. */
    707  1.204  christos 	if (kauth_cred_geteuid(cred) == file_uid) {
    708  1.204  christos 		dac_granted |= VADMIN;
    709  1.204  christos 		if (file_mode & S_IXUSR)
    710  1.204  christos 			dac_granted |= VEXEC;
    711  1.204  christos 		if (file_mode & S_IRUSR)
    712  1.204  christos 			dac_granted |= VREAD;
    713  1.204  christos 		if (file_mode & S_IWUSR)
    714  1.204  christos 			dac_granted |= (VWRITE | VAPPEND);
    715  1.172      elad 
    716  1.205  christos 		goto privchk;
    717  1.172      elad 	}
    718  1.172      elad 
    719  1.204  christos 	/* Otherwise, check the groups (first match) */
    720  1.172      elad 	/* Otherwise, check the groups. */
    721  1.204  christos 	error = groupmember(file_gid, cred);
    722  1.204  christos 	if (error > 0)
    723  1.204  christos 		return error;
    724  1.204  christos 	if (error == 0) {
    725  1.204  christos 		if (file_mode & S_IXGRP)
    726  1.204  christos 			dac_granted |= VEXEC;
    727  1.204  christos 		if (file_mode & S_IRGRP)
    728  1.204  christos 			dac_granted |= VREAD;
    729  1.204  christos 		if (file_mode & S_IWGRP)
    730  1.204  christos 			dac_granted |= (VWRITE | VAPPEND);
    731  1.204  christos 
    732  1.205  christos 		goto privchk;
    733  1.172      elad 	}
    734  1.172      elad 
    735  1.172      elad 	/* Otherwise, check everyone else. */
    736  1.204  christos 	if (file_mode & S_IXOTH)
    737  1.204  christos 		dac_granted |= VEXEC;
    738  1.204  christos 	if (file_mode & S_IROTH)
    739  1.204  christos 		dac_granted |= VREAD;
    740  1.204  christos 	if (file_mode & S_IWOTH)
    741  1.204  christos 		dac_granted |= (VWRITE | VAPPEND);
    742  1.205  christos 
    743  1.205  christos privchk:
    744  1.205  christos 	if ((accmode & dac_granted) == accmode)
    745  1.205  christos 		return 0;
    746  1.205  christos 
    747  1.205  christos 	return (accmode & VADMIN) ? EPERM : EACCES;
    748  1.204  christos }
    749  1.204  christos 
    750  1.204  christos /*
    751  1.204  christos  * Implement a version of genfs_can_access() that understands POSIX.1e ACL
    752  1.204  christos  * semantics;
    753  1.204  christos  * the access ACL has already been prepared for evaluation by the file system
    754  1.204  christos  * and is passed via 'uid', 'gid', and 'acl'.  Return 0 on success, else an
    755  1.204  christos  * errno value.
    756  1.204  christos  */
    757  1.204  christos int
    758  1.204  christos genfs_can_access_acl_posix1e(vnode_t *vp, kauth_cred_t cred, uid_t file_uid,
    759  1.204  christos     gid_t file_gid, mode_t file_mode, struct acl *acl, accmode_t accmode)
    760  1.204  christos {
    761  1.204  christos 	struct acl_entry *acl_other, *acl_mask;
    762  1.204  christos 	accmode_t dac_granted;
    763  1.204  christos 	accmode_t acl_mask_granted;
    764  1.204  christos 	int group_matched, i;
    765  1.204  christos 	int error;
    766  1.204  christos 
    767  1.204  christos 	KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0);
    768  1.204  christos 	KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE));
    769  1.204  christos 
    770  1.204  christos 	/*
    771  1.204  christos 	 * The owner matches if the effective uid associated with the
    772  1.204  christos 	 * credential matches that of the ACL_USER_OBJ entry.  While we're
    773  1.204  christos 	 * doing the first scan, also cache the location of the ACL_MASK and
    774  1.204  christos 	 * ACL_OTHER entries, preventing some future iterations.
    775  1.204  christos 	 */
    776  1.204  christos 	acl_mask = acl_other = NULL;
    777  1.204  christos 	for (i = 0; i < acl->acl_cnt; i++) {
    778  1.204  christos 		struct acl_entry *ae = &acl->acl_entry[i];
    779  1.204  christos 		switch (ae->ae_tag) {
    780  1.204  christos 		case ACL_USER_OBJ:
    781  1.204  christos 			if (kauth_cred_geteuid(cred) != file_uid)
    782  1.204  christos 				break;
    783  1.204  christos 			dac_granted = 0;
    784  1.204  christos 			dac_granted |= VADMIN;
    785  1.204  christos 			if (ae->ae_perm & ACL_EXECUTE)
    786  1.204  christos 				dac_granted |= VEXEC;
    787  1.204  christos 			if (ae->ae_perm & ACL_READ)
    788  1.204  christos 				dac_granted |= VREAD;
    789  1.204  christos 			if (ae->ae_perm & ACL_WRITE)
    790  1.204  christos 				dac_granted |= (VWRITE | VAPPEND);
    791  1.204  christos 			goto out;
    792  1.204  christos 
    793  1.204  christos 		case ACL_MASK:
    794  1.204  christos 			acl_mask = ae;
    795  1.204  christos 			break;
    796  1.204  christos 
    797  1.204  christos 		case ACL_OTHER:
    798  1.204  christos 			acl_other = ae;
    799  1.204  christos 			break;
    800  1.204  christos 
    801  1.204  christos 		default:
    802  1.204  christos 			break;
    803  1.204  christos 		}
    804  1.204  christos 	}
    805  1.204  christos 
    806  1.204  christos 	/*
    807  1.204  christos 	 * An ACL_OTHER entry should always exist in a valid access ACL.  If
    808  1.204  christos 	 * it doesn't, then generate a serious failure.	 For now, this means
    809  1.204  christos 	 * a debugging message and EPERM, but in the future should probably
    810  1.204  christos 	 * be a panic.
    811  1.204  christos 	 */
    812  1.204  christos 	if (acl_other == NULL) {
    813  1.204  christos 		/*
    814  1.204  christos 		 * XXX This should never happen
    815  1.204  christos 		 */
    816  1.204  christos 		printf("%s: ACL_OTHER missing\n", __func__);
    817  1.204  christos 		return EPERM;
    818  1.204  christos 	}
    819  1.204  christos 
    820  1.204  christos 	/*
    821  1.204  christos 	 * Checks against ACL_USER, ACL_GROUP_OBJ, and ACL_GROUP fields are
    822  1.204  christos 	 * masked by an ACL_MASK entry, if any.	 As such, first identify the
    823  1.204  christos 	 * ACL_MASK field, then iterate through identifying potential user
    824  1.204  christos 	 * matches, then group matches.	 If there is no ACL_MASK, assume that
    825  1.204  christos 	 * the mask allows all requests to succeed.
    826  1.204  christos 	 */
    827  1.204  christos 	if (acl_mask != NULL) {
    828  1.204  christos 		acl_mask_granted = 0;
    829  1.204  christos 		if (acl_mask->ae_perm & ACL_EXECUTE)
    830  1.204  christos 			acl_mask_granted |= VEXEC;
    831  1.204  christos 		if (acl_mask->ae_perm & ACL_READ)
    832  1.204  christos 			acl_mask_granted |= VREAD;
    833  1.204  christos 		if (acl_mask->ae_perm & ACL_WRITE)
    834  1.204  christos 			acl_mask_granted |= (VWRITE | VAPPEND);
    835  1.204  christos 	} else
    836  1.204  christos 		acl_mask_granted = VEXEC | VREAD | VWRITE | VAPPEND;
    837  1.204  christos 
    838  1.204  christos 	/*
    839  1.204  christos 	 * Check ACL_USER ACL entries.	There will either be one or no
    840  1.204  christos 	 * matches; if there is one, we accept or rejected based on the
    841  1.204  christos 	 * match; otherwise, we continue on to groups.
    842  1.204  christos 	 */
    843  1.204  christos 	for (i = 0; i < acl->acl_cnt; i++) {
    844  1.204  christos 		struct acl_entry *ae = &acl->acl_entry[i];
    845  1.204  christos 		switch (ae->ae_tag) {
    846  1.204  christos 		case ACL_USER:
    847  1.204  christos 			if (kauth_cred_geteuid(cred) != ae->ae_id)
    848  1.204  christos 				break;
    849  1.204  christos 			dac_granted = 0;
    850  1.204  christos 			if (ae->ae_perm & ACL_EXECUTE)
    851  1.204  christos 				dac_granted |= VEXEC;
    852  1.204  christos 			if (ae->ae_perm & ACL_READ)
    853  1.204  christos 				dac_granted |= VREAD;
    854  1.204  christos 			if (ae->ae_perm & ACL_WRITE)
    855  1.204  christos 				dac_granted |= (VWRITE | VAPPEND);
    856  1.204  christos 			dac_granted &= acl_mask_granted;
    857  1.204  christos 			goto out;
    858  1.204  christos 		}
    859  1.204  christos 	}
    860  1.204  christos 
    861  1.204  christos 	/*
    862  1.204  christos 	 * Group match is best-match, not first-match, so find a "best"
    863  1.204  christos 	 * match.  Iterate across, testing each potential group match.	Make
    864  1.204  christos 	 * sure we keep track of whether we found a match or not, so that we
    865  1.204  christos 	 * know if we should try again with any available privilege, or if we
    866  1.204  christos 	 * should move on to ACL_OTHER.
    867  1.204  christos 	 */
    868  1.204  christos 	group_matched = 0;
    869  1.204  christos 	for (i = 0; i < acl->acl_cnt; i++) {
    870  1.204  christos 		struct acl_entry *ae = &acl->acl_entry[i];
    871  1.204  christos 		switch (ae->ae_tag) {
    872  1.204  christos 		case ACL_GROUP_OBJ:
    873  1.204  christos 			error = groupmember(file_gid, cred);
    874  1.204  christos 			if (error > 0)
    875  1.204  christos 				return error;
    876  1.204  christos 			if (error)
    877  1.204  christos 				break;
    878  1.204  christos 			dac_granted = 0;
    879  1.204  christos 			if (ae->ae_perm & ACL_EXECUTE)
    880  1.204  christos 				dac_granted |= VEXEC;
    881  1.204  christos 			if (ae->ae_perm & ACL_READ)
    882  1.204  christos 				dac_granted |= VREAD;
    883  1.204  christos 			if (ae->ae_perm & ACL_WRITE)
    884  1.204  christos 				dac_granted |= (VWRITE | VAPPEND);
    885  1.204  christos 			dac_granted  &= acl_mask_granted;
    886  1.204  christos 
    887  1.204  christos 			if ((accmode & dac_granted) == accmode)
    888  1.204  christos 				return 0;
    889  1.204  christos 
    890  1.204  christos 			group_matched = 1;
    891  1.204  christos 			break;
    892  1.204  christos 
    893  1.204  christos 		case ACL_GROUP:
    894  1.204  christos 			error = groupmember(ae->ae_id, cred);
    895  1.204  christos 			if (error > 0)
    896  1.204  christos 				return error;
    897  1.204  christos 			if (error)
    898  1.204  christos 				break;
    899  1.204  christos 			dac_granted = 0;
    900  1.204  christos 			if (ae->ae_perm & ACL_EXECUTE)
    901  1.204  christos 				dac_granted |= VEXEC;
    902  1.204  christos 			if (ae->ae_perm & ACL_READ)
    903  1.204  christos 				dac_granted |= VREAD;
    904  1.204  christos 			if (ae->ae_perm & ACL_WRITE)
    905  1.204  christos 				dac_granted |= (VWRITE | VAPPEND);
    906  1.204  christos 			dac_granted  &= acl_mask_granted;
    907  1.204  christos 
    908  1.204  christos 			if ((accmode & dac_granted) == accmode)
    909  1.204  christos 				return 0;
    910  1.204  christos 
    911  1.204  christos 			group_matched = 1;
    912  1.204  christos 			break;
    913  1.204  christos 
    914  1.204  christos 		default:
    915  1.204  christos 			break;
    916  1.204  christos 		}
    917  1.204  christos 	}
    918  1.204  christos 
    919  1.204  christos 	if (group_matched == 1) {
    920  1.204  christos 		/*
    921  1.204  christos 		 * There was a match, but it did not grant rights via pure
    922  1.204  christos 		 * DAC.	 Try again, this time with privilege.
    923  1.204  christos 		 */
    924  1.204  christos 		for (i = 0; i < acl->acl_cnt; i++) {
    925  1.204  christos 			struct acl_entry *ae = &acl->acl_entry[i];
    926  1.204  christos 			switch (ae->ae_tag) {
    927  1.204  christos 			case ACL_GROUP_OBJ:
    928  1.204  christos 				error = groupmember(file_gid, cred);
    929  1.204  christos 				if (error > 0)
    930  1.204  christos 					return error;
    931  1.204  christos 				if (error)
    932  1.204  christos 					break;
    933  1.204  christos 				dac_granted = 0;
    934  1.204  christos 				if (ae->ae_perm & ACL_EXECUTE)
    935  1.204  christos 					dac_granted |= VEXEC;
    936  1.204  christos 				if (ae->ae_perm & ACL_READ)
    937  1.204  christos 					dac_granted |= VREAD;
    938  1.204  christos 				if (ae->ae_perm & ACL_WRITE)
    939  1.204  christos 					dac_granted |= (VWRITE | VAPPEND);
    940  1.204  christos 				dac_granted &= acl_mask_granted;
    941  1.204  christos 				goto out;
    942  1.204  christos 
    943  1.204  christos 			case ACL_GROUP:
    944  1.204  christos 				error = groupmember(ae->ae_id, cred);
    945  1.204  christos 				if (error > 0)
    946  1.204  christos 					return error;
    947  1.204  christos 				if (error)
    948  1.204  christos 					break;
    949  1.204  christos 				dac_granted = 0;
    950  1.204  christos 				if (ae->ae_perm & ACL_EXECUTE)
    951  1.204  christos 				dac_granted |= VEXEC;
    952  1.204  christos 				if (ae->ae_perm & ACL_READ)
    953  1.204  christos 					dac_granted |= VREAD;
    954  1.204  christos 				if (ae->ae_perm & ACL_WRITE)
    955  1.204  christos 					dac_granted |= (VWRITE | VAPPEND);
    956  1.204  christos 				dac_granted &= acl_mask_granted;
    957  1.204  christos 
    958  1.204  christos 				goto out;
    959  1.204  christos 			default:
    960  1.204  christos 				break;
    961  1.204  christos 			}
    962  1.204  christos 		}
    963  1.204  christos 		/*
    964  1.204  christos 		 * Even with privilege, group membership was not sufficient.
    965  1.204  christos 		 * Return failure.
    966  1.204  christos 		 */
    967  1.204  christos 		dac_granted = 0;
    968  1.204  christos 		goto out;
    969  1.204  christos 	}
    970  1.204  christos 
    971  1.204  christos 	/*
    972  1.204  christos 	 * Fall back on ACL_OTHER.  ACL_MASK is not applied to ACL_OTHER.
    973  1.204  christos 	 */
    974  1.204  christos 	dac_granted = 0;
    975  1.204  christos 	if (acl_other->ae_perm & ACL_EXECUTE)
    976  1.204  christos 		dac_granted |= VEXEC;
    977  1.204  christos 	if (acl_other->ae_perm & ACL_READ)
    978  1.204  christos 		dac_granted |= VREAD;
    979  1.204  christos 	if (acl_other->ae_perm & ACL_WRITE)
    980  1.204  christos 		dac_granted |= (VWRITE | VAPPEND);
    981  1.204  christos 
    982  1.204  christos out:
    983  1.204  christos 	if ((accmode & dac_granted) == accmode)
    984  1.204  christos 		return 0;
    985  1.204  christos 	return (accmode & VADMIN) ? EPERM : EACCES;
    986  1.204  christos }
    987  1.204  christos 
    988  1.204  christos static struct {
    989  1.204  christos 	accmode_t accmode;
    990  1.204  christos 	int mask;
    991  1.204  christos } accmode2mask[] = {
    992  1.204  christos 	{ VREAD, ACL_READ_DATA },
    993  1.204  christos 	{ VWRITE, ACL_WRITE_DATA },
    994  1.204  christos 	{ VAPPEND, ACL_APPEND_DATA },
    995  1.204  christos 	{ VEXEC, ACL_EXECUTE },
    996  1.204  christos 	{ VREAD_NAMED_ATTRS, ACL_READ_NAMED_ATTRS },
    997  1.204  christos 	{ VWRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS },
    998  1.204  christos 	{ VDELETE_CHILD, ACL_DELETE_CHILD },
    999  1.204  christos 	{ VREAD_ATTRIBUTES, ACL_READ_ATTRIBUTES },
   1000  1.204  christos 	{ VWRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES },
   1001  1.204  christos 	{ VDELETE, ACL_DELETE },
   1002  1.204  christos 	{ VREAD_ACL, ACL_READ_ACL },
   1003  1.204  christos 	{ VWRITE_ACL, ACL_WRITE_ACL },
   1004  1.204  christos 	{ VWRITE_OWNER, ACL_WRITE_OWNER },
   1005  1.204  christos 	{ VSYNCHRONIZE, ACL_SYNCHRONIZE },
   1006  1.204  christos 	{ 0, 0 },
   1007  1.204  christos };
   1008  1.204  christos 
   1009  1.204  christos static int
   1010  1.204  christos _access_mask_from_accmode(accmode_t accmode)
   1011  1.204  christos {
   1012  1.204  christos 	int access_mask = 0, i;
   1013  1.204  christos 
   1014  1.204  christos 	for (i = 0; accmode2mask[i].accmode != 0; i++) {
   1015  1.204  christos 		if (accmode & accmode2mask[i].accmode)
   1016  1.204  christos 			access_mask |= accmode2mask[i].mask;
   1017  1.204  christos 	}
   1018  1.204  christos 
   1019  1.204  christos 	/*
   1020  1.204  christos 	 * VAPPEND is just a modifier for VWRITE; if the caller asked
   1021  1.204  christos 	 * for 'VAPPEND | VWRITE', we want to check for ACL_APPEND_DATA only.
   1022  1.204  christos 	 */
   1023  1.204  christos 	if (access_mask & ACL_APPEND_DATA)
   1024  1.204  christos 		access_mask &= ~ACL_WRITE_DATA;
   1025  1.204  christos 
   1026  1.204  christos 	return (access_mask);
   1027  1.204  christos }
   1028  1.204  christos 
   1029  1.204  christos /*
   1030  1.204  christos  * Return 0, iff access is allowed, 1 otherwise.
   1031  1.204  christos  */
   1032  1.204  christos static int
   1033  1.204  christos _acl_denies(const struct acl *aclp, int access_mask, kauth_cred_t cred,
   1034  1.204  christos     int file_uid, int file_gid, int *denied_explicitly)
   1035  1.204  christos {
   1036  1.204  christos 	int i, error;
   1037  1.204  christos 	const struct acl_entry *ae;
   1038  1.204  christos 
   1039  1.204  christos 	if (denied_explicitly != NULL)
   1040  1.204  christos 		*denied_explicitly = 0;
   1041  1.204  christos 
   1042  1.204  christos 	KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES);
   1043  1.204  christos 
   1044  1.204  christos 	for (i = 0; i < aclp->acl_cnt; i++) {
   1045  1.204  christos 		ae = &(aclp->acl_entry[i]);
   1046  1.204  christos 
   1047  1.204  christos 		if (ae->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
   1048  1.204  christos 		    ae->ae_entry_type != ACL_ENTRY_TYPE_DENY)
   1049  1.204  christos 			continue;
   1050  1.204  christos 		if (ae->ae_flags & ACL_ENTRY_INHERIT_ONLY)
   1051  1.204  christos 			continue;
   1052  1.204  christos 		switch (ae->ae_tag) {
   1053  1.204  christos 		case ACL_USER_OBJ:
   1054  1.204  christos 			if (kauth_cred_geteuid(cred) != file_uid)
   1055  1.204  christos 				continue;
   1056  1.204  christos 			break;
   1057  1.204  christos 		case ACL_USER:
   1058  1.204  christos 			if (kauth_cred_geteuid(cred) != ae->ae_id)
   1059  1.204  christos 				continue;
   1060  1.204  christos 			break;
   1061  1.204  christos 		case ACL_GROUP_OBJ:
   1062  1.204  christos 			error = groupmember(file_gid, cred);
   1063  1.204  christos 			if (error > 0)
   1064  1.204  christos 				return error;
   1065  1.204  christos 			if (error != 0)
   1066  1.204  christos 				continue;
   1067  1.204  christos 			break;
   1068  1.204  christos 		case ACL_GROUP:
   1069  1.204  christos 			error = groupmember(ae->ae_id, cred);
   1070  1.204  christos 			if (error > 0)
   1071  1.204  christos 				return error;
   1072  1.204  christos 			if (error != 0)
   1073  1.204  christos 				continue;
   1074  1.204  christos 			break;
   1075  1.204  christos 		default:
   1076  1.204  christos 			KASSERT(ae->ae_tag == ACL_EVERYONE);
   1077  1.204  christos 		}
   1078  1.204  christos 
   1079  1.204  christos 		if (ae->ae_entry_type == ACL_ENTRY_TYPE_DENY) {
   1080  1.204  christos 			if (ae->ae_perm & access_mask) {
   1081  1.204  christos 				if (denied_explicitly != NULL)
   1082  1.204  christos 					*denied_explicitly = 1;
   1083  1.204  christos 				return (1);
   1084  1.204  christos 			}
   1085  1.204  christos 		}
   1086  1.204  christos 
   1087  1.204  christos 		access_mask &= ~(ae->ae_perm);
   1088  1.204  christos 		if (access_mask == 0)
   1089  1.204  christos 			return (0);
   1090  1.204  christos 	}
   1091  1.204  christos 
   1092  1.204  christos 	if (access_mask == 0)
   1093  1.204  christos 		return (0);
   1094  1.204  christos 
   1095  1.204  christos 	return (1);
   1096  1.204  christos }
   1097  1.204  christos 
   1098  1.204  christos int
   1099  1.204  christos genfs_can_access_acl_nfs4(vnode_t *vp, kauth_cred_t cred, uid_t file_uid,
   1100  1.204  christos     gid_t file_gid, mode_t file_mode, struct acl *aclp, accmode_t accmode)
   1101  1.204  christos {
   1102  1.204  christos 	int denied, explicitly_denied, access_mask, is_directory,
   1103  1.204  christos 	    must_be_owner = 0;
   1104  1.204  christos 	file_mode = 0;
   1105  1.204  christos 
   1106  1.204  christos 	KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND |
   1107  1.204  christos 	    VEXPLICIT_DENY | VREAD_NAMED_ATTRS | VWRITE_NAMED_ATTRS |
   1108  1.204  christos 	    VDELETE_CHILD | VREAD_ATTRIBUTES | VWRITE_ATTRIBUTES | VDELETE |
   1109  1.204  christos 	    VREAD_ACL | VWRITE_ACL | VWRITE_OWNER | VSYNCHRONIZE)) == 0);
   1110  1.204  christos 	KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE));
   1111  1.204  christos 
   1112  1.204  christos 	if (accmode & VADMIN)
   1113  1.204  christos 		must_be_owner = 1;
   1114  1.204  christos 
   1115  1.204  christos 	/*
   1116  1.204  christos 	 * Ignore VSYNCHRONIZE permission.
   1117  1.204  christos 	 */
   1118  1.204  christos 	accmode &= ~VSYNCHRONIZE;
   1119  1.204  christos 
   1120  1.204  christos 	access_mask = _access_mask_from_accmode(accmode);
   1121  1.204  christos 
   1122  1.204  christos 	if (vp && vp->v_type == VDIR)
   1123  1.204  christos 		is_directory = 1;
   1124  1.204  christos 	else
   1125  1.204  christos 		is_directory = 0;
   1126  1.204  christos 
   1127  1.204  christos 	/*
   1128  1.204  christos 	 * File owner is always allowed to read and write the ACL
   1129  1.204  christos 	 * and basic attributes.  This is to prevent a situation
   1130  1.204  christos 	 * where user would change ACL in a way that prevents him
   1131  1.204  christos 	 * from undoing the change.
   1132  1.204  christos 	 */
   1133  1.204  christos 	if (kauth_cred_geteuid(cred) == file_uid)
   1134  1.204  christos 		access_mask &= ~(ACL_READ_ACL | ACL_WRITE_ACL |
   1135  1.204  christos 		    ACL_READ_ATTRIBUTES | ACL_WRITE_ATTRIBUTES);
   1136  1.204  christos 
   1137  1.204  christos 	/*
   1138  1.204  christos 	 * Ignore append permission for regular files; use write
   1139  1.204  christos 	 * permission instead.
   1140  1.204  christos 	 */
   1141  1.204  christos 	if (!is_directory && (access_mask & ACL_APPEND_DATA)) {
   1142  1.204  christos 		access_mask &= ~ACL_APPEND_DATA;
   1143  1.204  christos 		access_mask |= ACL_WRITE_DATA;
   1144  1.204  christos 	}
   1145  1.204  christos 
   1146  1.204  christos 	denied = _acl_denies(aclp, access_mask, cred, file_uid, file_gid,
   1147  1.204  christos 	    &explicitly_denied);
   1148  1.204  christos 
   1149  1.204  christos 	if (must_be_owner) {
   1150  1.204  christos 		if (kauth_cred_geteuid(cred) != file_uid)
   1151  1.204  christos 			denied = EPERM;
   1152  1.204  christos 	}
   1153  1.204  christos 
   1154  1.204  christos 	/*
   1155  1.204  christos 	 * For VEXEC, ensure that at least one execute bit is set for
   1156  1.204  christos 	 * non-directories. We have to check the mode here to stay
   1157  1.204  christos 	 * consistent with execve(2). See the test in
   1158  1.204  christos 	 * exec_check_permissions().
   1159  1.204  christos 	 */
   1160  1.204  christos 	__acl_nfs4_sync_mode_from_acl(&file_mode, aclp);
   1161  1.204  christos 	if (!denied && !is_directory && (accmode & VEXEC) &&
   1162  1.204  christos 	    (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
   1163  1.204  christos 		denied = EACCES;
   1164  1.204  christos 
   1165  1.204  christos 	if (!denied)
   1166  1.204  christos 		return (0);
   1167  1.204  christos 
   1168  1.204  christos 	/*
   1169  1.204  christos 	 * Access failed.  Iff it was not denied explicitly and
   1170  1.204  christos 	 * VEXPLICIT_DENY flag was specified, allow access.
   1171  1.204  christos 	 */
   1172  1.204  christos 	if ((accmode & VEXPLICIT_DENY) && explicitly_denied == 0)
   1173  1.204  christos 		return (0);
   1174  1.204  christos 
   1175  1.204  christos 	accmode &= ~VEXPLICIT_DENY;
   1176  1.204  christos 
   1177  1.204  christos 	if (accmode & (VADMIN_PERMS | VDELETE_CHILD | VDELETE))
   1178  1.204  christos 		denied = EPERM;
   1179  1.204  christos 	else
   1180  1.204  christos 		denied = EACCES;
   1181  1.204  christos 
   1182  1.204  christos 	return (denied);
   1183  1.172      elad }
   1184  1.172      elad 
   1185  1.172      elad /*
   1186  1.169      elad  * Common routine to check if chmod() is allowed.
   1187  1.169      elad  *
   1188  1.169      elad  * Policy:
   1189  1.169      elad  *   - You must own the file, and
   1190  1.169      elad  *     - You must not set the "sticky" bit (meaningless, see chmod(2))
   1191  1.169      elad  *     - You must be a member of the group if you're trying to set the
   1192  1.204  christos  *	 SGIDf bit
   1193  1.169      elad  *
   1194  1.204  christos  * vp - vnode of the file-system object
   1195  1.169      elad  * cred - credentials of the invoker
   1196  1.169      elad  * cur_uid, cur_gid - current uid/gid of the file-system object
   1197  1.169      elad  * new_mode - new mode for the file-system object
   1198  1.169      elad  *
   1199  1.169      elad  * Returns 0 if the change is allowed, or an error value otherwise.
   1200  1.169      elad  */
   1201  1.169      elad int
   1202  1.204  christos genfs_can_chmod(vnode_t *vp, kauth_cred_t cred, uid_t cur_uid,
   1203  1.169      elad     gid_t cur_gid, mode_t new_mode)
   1204  1.169      elad {
   1205  1.169      elad 	int error;
   1206  1.169      elad 
   1207  1.204  christos 	/*
   1208  1.204  christos 	 * To modify the permissions on a file, must possess VADMIN
   1209  1.204  christos 	 * for that file.
   1210  1.204  christos 	 */
   1211  1.204  christos 	if ((error = VOP_ACCESSX(vp, VWRITE_ACL, cred)) != 0)
   1212  1.204  christos 		return (error);
   1213  1.169      elad 
   1214  1.169      elad 	/*
   1215  1.188      elad 	 * Unprivileged users can't set the sticky bit on files.
   1216  1.169      elad 	 */
   1217  1.204  christos 	if ((vp->v_type != VDIR) && (new_mode & S_ISTXT))
   1218  1.169      elad 		return (EFTYPE);
   1219  1.169      elad 
   1220  1.169      elad 	/*
   1221  1.169      elad 	 * If the invoker is trying to set the SGID bit on the file,
   1222  1.169      elad 	 * check group membership.
   1223  1.169      elad 	 */
   1224  1.169      elad 	if (new_mode & S_ISGID) {
   1225  1.169      elad 		int ismember;
   1226  1.169      elad 
   1227  1.169      elad 		error = kauth_cred_ismember_gid(cred, cur_gid,
   1228  1.169      elad 		    &ismember);
   1229  1.169      elad 		if (error || !ismember)
   1230  1.169      elad 			return (EPERM);
   1231  1.169      elad 	}
   1232  1.169      elad 
   1233  1.204  christos 	/*
   1234  1.204  christos 	 * Deny setting setuid if we are not the file owner.
   1235  1.204  christos 	 */
   1236  1.204  christos 	if ((new_mode & S_ISUID) && cur_uid != kauth_cred_geteuid(cred))
   1237  1.204  christos 		return (EPERM);
   1238  1.204  christos 
   1239  1.169      elad 	return (0);
   1240  1.169      elad }
   1241  1.169      elad 
   1242  1.169      elad /*
   1243  1.169      elad  * Common routine to check if chown() is allowed.
   1244  1.169      elad  *
   1245  1.169      elad  * Policy:
   1246  1.169      elad  *   - You must own the file, and
   1247  1.169      elad  *     - You must not try to change ownership, and
   1248  1.169      elad  *     - You must be member of the new group
   1249  1.169      elad  *
   1250  1.204  christos  * vp - vnode
   1251  1.169      elad  * cred - credentials of the invoker
   1252  1.169      elad  * cur_uid, cur_gid - current uid/gid of the file-system object
   1253  1.169      elad  * new_uid, new_gid - target uid/gid of the file-system object
   1254  1.169      elad  *
   1255  1.169      elad  * Returns 0 if the change is allowed, or an error value otherwise.
   1256  1.169      elad  */
   1257  1.169      elad int
   1258  1.204  christos genfs_can_chown(vnode_t *vp, kauth_cred_t cred, uid_t cur_uid,
   1259  1.169      elad     gid_t cur_gid, uid_t new_uid, gid_t new_gid)
   1260  1.169      elad {
   1261  1.169      elad 	int error, ismember;
   1262  1.169      elad 
   1263  1.169      elad 	/*
   1264  1.204  christos 	 * To modify the ownership of a file, must possess VADMIN for that
   1265  1.204  christos 	 * file.
   1266  1.204  christos 	 */
   1267  1.204  christos 	if ((error = VOP_ACCESSX(vp, VWRITE_OWNER, cred)) != 0)
   1268  1.204  christos 		return (error);
   1269  1.204  christos 
   1270  1.204  christos 	/*
   1271  1.169      elad 	 * You can only change ownership of a file if:
   1272  1.169      elad 	 * You own the file and...
   1273  1.169      elad 	 */
   1274  1.169      elad 	if (kauth_cred_geteuid(cred) == cur_uid) {
   1275  1.169      elad 		/*
   1276  1.169      elad 		 * You don't try to change ownership, and...
   1277  1.169      elad 		 */
   1278  1.169      elad 		if (new_uid != cur_uid)
   1279  1.169      elad 			return (EPERM);
   1280  1.169      elad 
   1281  1.169      elad 		/*
   1282  1.169      elad 		 * You don't try to change group (no-op), or...
   1283  1.169      elad 		 */
   1284  1.169      elad 		if (new_gid == cur_gid)
   1285  1.169      elad 			return (0);
   1286  1.169      elad 
   1287  1.169      elad 		/*
   1288  1.169      elad 		 * Your effective gid is the new gid, or...
   1289  1.169      elad 		 */
   1290  1.169      elad 		if (kauth_cred_getegid(cred) == new_gid)
   1291  1.169      elad 			return (0);
   1292  1.169      elad 
   1293  1.169      elad 		/*
   1294  1.169      elad 		 * The new gid is one you're a member of.
   1295  1.169      elad 		 */
   1296  1.169      elad 		ismember = 0;
   1297  1.169      elad 		error = kauth_cred_ismember_gid(cred, new_gid,
   1298  1.169      elad 		    &ismember);
   1299  1.174       roy 		if (!error && ismember)
   1300  1.174       roy 			return (0);
   1301  1.169      elad 	}
   1302  1.169      elad 
   1303  1.173     pooka 	return (EPERM);
   1304  1.169      elad }
   1305  1.169      elad 
   1306  1.171      elad int
   1307  1.204  christos genfs_can_chtimes(vnode_t *vp, kauth_cred_t cred, uid_t owner_uid,
   1308  1.204  christos     u_int vaflags)
   1309  1.171      elad {
   1310  1.171      elad 	int error;
   1311  1.204  christos 	/*
   1312  1.204  christos 	 * Grant permission if the caller is the owner of the file, or
   1313  1.204  christos 	 * the super-user, or has ACL_WRITE_ATTRIBUTES permission on
   1314  1.204  christos 	 * on the file.	 If the time pointer is null, then write
   1315  1.204  christos 	 * permission on the file is also sufficient.
   1316  1.204  christos 	 *
   1317  1.204  christos 	 * From NFSv4.1, draft 21, 6.2.1.3.1, Discussion of Mask Attributes:
   1318  1.204  christos 	 * A user having ACL_WRITE_DATA or ACL_WRITE_ATTRIBUTES
   1319  1.204  christos 	 * will be allowed to set the times [..] to the current
   1320  1.204  christos 	 * server time.
   1321  1.204  christos 	 */
   1322  1.204  christos 	if ((error = VOP_ACCESSX(vp, VWRITE_ATTRIBUTES, cred)) != 0)
   1323  1.207  christos 		return (vaflags & VA_UTIMES_NULL) == 0 ? EPERM : EACCES;
   1324  1.171      elad 
   1325  1.188      elad 	/* Must be owner, or... */
   1326  1.171      elad 	if (kauth_cred_geteuid(cred) == owner_uid)
   1327  1.171      elad 		return (0);
   1328  1.171      elad 
   1329  1.171      elad 	/* set the times to the current time, and... */
   1330  1.171      elad 	if ((vaflags & VA_UTIMES_NULL) == 0)
   1331  1.171      elad 		return (EPERM);
   1332  1.171      elad 
   1333  1.171      elad 	/* have write access. */
   1334  1.171      elad 	error = VOP_ACCESS(vp, VWRITE, cred);
   1335  1.171      elad 	if (error)
   1336  1.171      elad 		return (error);
   1337  1.171      elad 
   1338  1.171      elad 	return (0);
   1339  1.171      elad }
   1340  1.171      elad 
   1341  1.188      elad /*
   1342  1.188      elad  * Common routine to check if chflags() is allowed.
   1343  1.188      elad  *
   1344  1.188      elad  * Policy:
   1345  1.188      elad  *   - You must own the file, and
   1346  1.188      elad  *   - You must not change system flags, and
   1347  1.188      elad  *   - You must not change flags on character/block devices.
   1348  1.188      elad  *
   1349  1.204  christos  * vp - vnode
   1350  1.188      elad  * cred - credentials of the invoker
   1351  1.188      elad  * owner_uid - uid of the file-system object
   1352  1.188      elad  * changing_sysflags - true if the invoker wants to change system flags
   1353  1.188      elad  */
   1354  1.188      elad int
   1355  1.204  christos genfs_can_chflags(vnode_t *vp, kauth_cred_t cred,
   1356  1.204  christos      uid_t owner_uid, bool changing_sysflags)
   1357  1.188      elad {
   1358  1.188      elad 
   1359  1.188      elad 	/* The user must own the file. */
   1360  1.188      elad 	if (kauth_cred_geteuid(cred) != owner_uid) {
   1361  1.189     njoly 		return EPERM;
   1362  1.188      elad 	}
   1363  1.188      elad 
   1364  1.188      elad 	if (changing_sysflags) {
   1365  1.188      elad 		return EPERM;
   1366  1.188      elad 	}
   1367  1.188      elad 
   1368  1.188      elad 	/*
   1369  1.188      elad 	 * Unprivileged users cannot change the flags on devices, even if they
   1370  1.188      elad 	 * own them.
   1371  1.188      elad 	 */
   1372  1.204  christos 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
   1373  1.188      elad 		return EPERM;
   1374  1.188      elad 	}
   1375  1.188      elad 
   1376  1.188      elad 	return 0;
   1377  1.188      elad }
   1378  1.188      elad 
   1379  1.188      elad /*
   1380  1.188      elad  * Common "sticky" policy.
   1381  1.188      elad  *
   1382  1.188      elad  * When a directory is "sticky" (as determined by the caller), this
   1383  1.188      elad  * function may help implementing the following policy:
   1384  1.188      elad  * - Renaming a file in it is only possible if the user owns the directory
   1385  1.188      elad  *   or the file being renamed.
   1386  1.188      elad  * - Deleting a file from it is only possible if the user owns the
   1387  1.188      elad  *   directory or the file being deleted.
   1388  1.188      elad  */
   1389  1.188      elad int
   1390  1.204  christos genfs_can_sticky(vnode_t *vp, kauth_cred_t cred, uid_t dir_uid, uid_t file_uid)
   1391  1.188      elad {
   1392  1.188      elad 	if (kauth_cred_geteuid(cred) != dir_uid &&
   1393  1.188      elad 	    kauth_cred_geteuid(cred) != file_uid)
   1394  1.188      elad 		return EPERM;
   1395  1.188      elad 
   1396  1.188      elad 	return 0;
   1397  1.188      elad }
   1398  1.188      elad 
   1399  1.188      elad int
   1400  1.204  christos genfs_can_extattr(vnode_t *vp, kauth_cred_t cred, int accmode,
   1401  1.204  christos     int attrnamespace)
   1402  1.188      elad {
   1403  1.203  christos 	/*
   1404  1.204  christos 	 * Kernel-invoked always succeeds.
   1405  1.203  christos 	 */
   1406  1.204  christos 	if (cred == NOCRED)
   1407  1.204  christos 		return 0;
   1408  1.204  christos 
   1409  1.204  christos 	switch (attrnamespace) {
   1410  1.204  christos 	case EXTATTR_NAMESPACE_SYSTEM:
   1411  1.204  christos 		return kauth_authorize_system(cred, KAUTH_SYSTEM_FS_EXTATTR,
   1412  1.204  christos 		    0, vp->v_mount, NULL, NULL);
   1413  1.204  christos 	case EXTATTR_NAMESPACE_USER:
   1414  1.204  christos 		return VOP_ACCESS(vp, accmode, cred);
   1415  1.204  christos 	default:
   1416  1.204  christos 		return EPERM;
   1417  1.204  christos 	}
   1418  1.204  christos }
   1419  1.204  christos 
   1420  1.204  christos int
   1421  1.204  christos genfs_access(void *v)
   1422  1.204  christos {
   1423  1.204  christos 	struct vop_access_args *ap = v;
   1424  1.204  christos 
   1425  1.204  christos 	KASSERT((ap->a_accmode & ~(VEXEC | VWRITE | VREAD | VADMIN |
   1426  1.204  christos 	    VAPPEND)) == 0);
   1427  1.204  christos 
   1428  1.204  christos 	return VOP_ACCESSX(ap->a_vp, ap->a_accmode, ap->a_cred);
   1429  1.204  christos }
   1430  1.204  christos 
   1431  1.204  christos int
   1432  1.204  christos genfs_accessx(void *v)
   1433  1.204  christos {
   1434  1.204  christos 	struct vop_accessx_args *ap = v;
   1435  1.204  christos 	int error;
   1436  1.204  christos 	accmode_t accmode = ap->a_accmode;
   1437  1.204  christos 	error = vfs_unixify_accmode(&accmode);
   1438  1.204  christos 	if (error != 0)
   1439  1.204  christos 		return error;
   1440  1.204  christos 
   1441  1.204  christos 	if (accmode == 0)
   1442  1.204  christos 		return 0;
   1443  1.188      elad 
   1444  1.204  christos 	return VOP_ACCESS(ap->a_vp, accmode, ap->a_cred);
   1445  1.188      elad }
   1446