Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.181
      1  1.181   hannken /*	$NetBSD: genfs_vnops.c,v 1.181 2010/06/24 13:03:16 hannken 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.181   hannken __KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.181 2010/06/24 13:03:16 hannken 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.1   mycroft #include <sys/namei.h>
     68    1.1   mycroft #include <sys/vnode.h>
     69   1.13  wrstuden #include <sys/fcntl.h>
     70  1.135      yamt #include <sys/kmem.h>
     71    1.3   mycroft #include <sys/poll.h>
     72   1.37       chs #include <sys/mman.h>
     73   1.66  jdolecek #include <sys/file.h>
     74  1.125      elad #include <sys/kauth.h>
     75  1.169      elad #include <sys/stat.h>
     76    1.1   mycroft 
     77    1.1   mycroft #include <miscfs/genfs/genfs.h>
     78   1.37       chs #include <miscfs/genfs/genfs_node.h>
     79    1.6      fvdl #include <miscfs/specfs/specdev.h>
     80    1.1   mycroft 
     81   1.21       chs #include <uvm/uvm.h>
     82   1.21       chs #include <uvm/uvm_pager.h>
     83   1.21       chs 
     84   1.70  christos static void filt_genfsdetach(struct knote *);
     85   1.70  christos static int filt_genfsread(struct knote *, long);
     86   1.70  christos static int filt_genfsvnode(struct knote *, long);
     87   1.70  christos 
     88    1.1   mycroft int
     89   1.53     enami genfs_poll(void *v)
     90    1.1   mycroft {
     91    1.3   mycroft 	struct vop_poll_args /* {
     92    1.1   mycroft 		struct vnode *a_vp;
     93    1.3   mycroft 		int a_events;
     94  1.116  christos 		struct lwp *a_l;
     95    1.1   mycroft 	} */ *ap = v;
     96    1.1   mycroft 
     97    1.3   mycroft 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
     98    1.1   mycroft }
     99    1.1   mycroft 
    100    1.1   mycroft int
    101   1.53     enami genfs_seek(void *v)
    102    1.4    kleink {
    103    1.4    kleink 	struct vop_seek_args /* {
    104    1.4    kleink 		struct vnode *a_vp;
    105    1.4    kleink 		off_t a_oldoff;
    106    1.4    kleink 		off_t a_newoff;
    107  1.125      elad 		kauth_cred_t cred;
    108    1.4    kleink 	} */ *ap = v;
    109    1.4    kleink 
    110    1.4    kleink 	if (ap->a_newoff < 0)
    111    1.4    kleink 		return (EINVAL);
    112    1.4    kleink 
    113    1.4    kleink 	return (0);
    114    1.4    kleink }
    115    1.4    kleink 
    116    1.4    kleink int
    117   1.53     enami genfs_abortop(void *v)
    118    1.1   mycroft {
    119    1.1   mycroft 	struct vop_abortop_args /* {
    120    1.1   mycroft 		struct vnode *a_dvp;
    121    1.1   mycroft 		struct componentname *a_cnp;
    122    1.1   mycroft 	} */ *ap = v;
    123   1.53     enami 
    124    1.1   mycroft 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    125   1.19   thorpej 		PNBUF_PUT(ap->a_cnp->cn_pnbuf);
    126    1.1   mycroft 	return (0);
    127   1.13  wrstuden }
    128   1.13  wrstuden 
    129   1.13  wrstuden int
    130   1.53     enami genfs_fcntl(void *v)
    131   1.13  wrstuden {
    132   1.13  wrstuden 	struct vop_fcntl_args /* {
    133   1.13  wrstuden 		struct vnode *a_vp;
    134   1.13  wrstuden 		u_int a_command;
    135  1.150  christos 		void *a_data;
    136   1.13  wrstuden 		int a_fflag;
    137  1.125      elad 		kauth_cred_t a_cred;
    138  1.116  christos 		struct lwp *a_l;
    139   1.13  wrstuden 	} */ *ap = v;
    140   1.13  wrstuden 
    141   1.13  wrstuden 	if (ap->a_command == F_SETFL)
    142   1.13  wrstuden 		return (0);
    143   1.13  wrstuden 	else
    144   1.13  wrstuden 		return (EOPNOTSUPP);
    145    1.1   mycroft }
    146    1.1   mycroft 
    147    1.1   mycroft /*ARGSUSED*/
    148    1.1   mycroft int
    149  1.138  christos genfs_badop(void *v)
    150    1.1   mycroft {
    151    1.1   mycroft 
    152    1.1   mycroft 	panic("genfs: bad op");
    153    1.1   mycroft }
    154    1.1   mycroft 
    155    1.1   mycroft /*ARGSUSED*/
    156    1.1   mycroft int
    157  1.138  christos genfs_nullop(void *v)
    158    1.1   mycroft {
    159    1.1   mycroft 
    160    1.1   mycroft 	return (0);
    161   1.10    kleink }
    162   1.10    kleink 
    163   1.10    kleink /*ARGSUSED*/
    164   1.10    kleink int
    165  1.138  christos genfs_einval(void *v)
    166   1.10    kleink {
    167   1.10    kleink 
    168   1.10    kleink 	return (EINVAL);
    169    1.1   mycroft }
    170    1.1   mycroft 
    171   1.12  wrstuden /*
    172   1.74  jdolecek  * Called when an fs doesn't support a particular vop.
    173  1.177     pooka  * This takes care to vrele, vput, or vunlock passed in vnodes
    174  1.177     pooka  * and calls VOP_ABORTOP for a componentname (in non-rename VOP).
    175   1.12  wrstuden  */
    176   1.12  wrstuden int
    177   1.75  jdolecek genfs_eopnotsupp(void *v)
    178   1.12  wrstuden {
    179   1.12  wrstuden 	struct vop_generic_args /*
    180   1.12  wrstuden 		struct vnodeop_desc *a_desc;
    181   1.53     enami 		/ * other random data follows, presumably * /
    182   1.12  wrstuden 	} */ *ap = v;
    183   1.12  wrstuden 	struct vnodeop_desc *desc = ap->a_desc;
    184   1.74  jdolecek 	struct vnode *vp, *vp_last = NULL;
    185  1.177     pooka 	int flags, i, j, offset_cnp, offset_vp;
    186  1.177     pooka 
    187  1.177     pooka 	KASSERT(desc->vdesc_offset != VOP_LOOKUP_DESCOFFSET);
    188  1.177     pooka 	KASSERT(desc->vdesc_offset != VOP_ABORTOP_DESCOFFSET);
    189  1.177     pooka 
    190  1.177     pooka 	/*
    191  1.177     pooka 	 * Free componentname that lookup potentially SAVENAMEd.
    192  1.177     pooka 	 *
    193  1.177     pooka 	 * As is logical, componentnames for VOP_RENAME are handled by
    194  1.177     pooka 	 * the caller of VOP_RENAME.  Yay, rename!
    195  1.177     pooka 	 */
    196  1.177     pooka 	if (desc->vdesc_offset != VOP_RENAME_DESCOFFSET &&
    197  1.177     pooka 	    (offset_vp = desc->vdesc_vp_offsets[0]) != VDESC_NO_OFFSET &&
    198  1.177     pooka 	    (offset_cnp = desc->vdesc_componentname_offset) != VDESC_NO_OFFSET){
    199  1.177     pooka 		struct componentname *cnp;
    200  1.177     pooka 		struct vnode *dvp;
    201  1.177     pooka 
    202  1.177     pooka 		dvp = *VOPARG_OFFSETTO(struct vnode **, offset_vp, ap);
    203  1.177     pooka 		cnp = *VOPARG_OFFSETTO(struct componentname **, offset_cnp, ap);
    204  1.177     pooka 
    205  1.177     pooka 		VOP_ABORTOP(dvp, cnp);
    206  1.177     pooka 	}
    207   1.12  wrstuden 
    208   1.12  wrstuden 	flags = desc->vdesc_flags;
    209   1.12  wrstuden 	for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
    210  1.177     pooka 		if ((offset_vp = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
    211   1.12  wrstuden 			break;	/* stop at end of list */
    212   1.12  wrstuden 		if ((j = flags & VDESC_VP0_WILLPUT)) {
    213  1.177     pooka 			vp = *VOPARG_OFFSETTO(struct vnode **, offset_vp, ap);
    214   1.74  jdolecek 
    215   1.74  jdolecek 			/* Skip if NULL */
    216   1.74  jdolecek 			if (!vp)
    217   1.74  jdolecek 				continue;
    218   1.74  jdolecek 
    219   1.12  wrstuden 			switch (j) {
    220   1.12  wrstuden 			case VDESC_VP0_WILLPUT:
    221   1.74  jdolecek 				/* Check for dvp == vp cases */
    222   1.74  jdolecek 				if (vp == vp_last)
    223   1.74  jdolecek 					vrele(vp);
    224   1.74  jdolecek 				else {
    225   1.74  jdolecek 					vput(vp);
    226   1.74  jdolecek 					vp_last = vp;
    227   1.74  jdolecek 				}
    228   1.12  wrstuden 				break;
    229   1.12  wrstuden 			case VDESC_VP0_WILLUNLOCK:
    230  1.181   hannken 				VOP_UNLOCK(vp);
    231   1.12  wrstuden 				break;
    232   1.12  wrstuden 			case VDESC_VP0_WILLRELE:
    233   1.12  wrstuden 				vrele(vp);
    234   1.12  wrstuden 				break;
    235   1.12  wrstuden 			}
    236   1.12  wrstuden 		}
    237   1.12  wrstuden 	}
    238   1.12  wrstuden 
    239   1.12  wrstuden 	return (EOPNOTSUPP);
    240   1.12  wrstuden }
    241   1.12  wrstuden 
    242    1.1   mycroft /*ARGSUSED*/
    243    1.1   mycroft int
    244  1.138  christos genfs_ebadf(void *v)
    245    1.1   mycroft {
    246    1.1   mycroft 
    247    1.1   mycroft 	return (EBADF);
    248    1.9  matthias }
    249    1.9  matthias 
    250    1.9  matthias /* ARGSUSED */
    251    1.9  matthias int
    252  1.138  christos genfs_enoioctl(void *v)
    253    1.9  matthias {
    254    1.9  matthias 
    255   1.51    atatat 	return (EPASSTHROUGH);
    256    1.6      fvdl }
    257    1.6      fvdl 
    258    1.6      fvdl 
    259    1.6      fvdl /*
    260   1.15      fvdl  * Eliminate all activity associated with the requested vnode
    261    1.6      fvdl  * and with all vnodes aliased to the requested vnode.
    262    1.6      fvdl  */
    263    1.6      fvdl int
    264   1.53     enami genfs_revoke(void *v)
    265    1.6      fvdl {
    266    1.6      fvdl 	struct vop_revoke_args /* {
    267    1.6      fvdl 		struct vnode *a_vp;
    268    1.6      fvdl 		int a_flags;
    269    1.6      fvdl 	} */ *ap = v;
    270    1.6      fvdl 
    271    1.6      fvdl #ifdef DIAGNOSTIC
    272    1.6      fvdl 	if ((ap->a_flags & REVOKEALL) == 0)
    273    1.6      fvdl 		panic("genfs_revoke: not revokeall");
    274    1.6      fvdl #endif
    275  1.161        ad 	vrevoke(ap->a_vp);
    276    1.6      fvdl 	return (0);
    277    1.6      fvdl }
    278    1.6      fvdl 
    279    1.6      fvdl /*
    280   1.12  wrstuden  * Lock the node.
    281    1.6      fvdl  */
    282    1.6      fvdl int
    283   1.53     enami genfs_lock(void *v)
    284    1.6      fvdl {
    285    1.6      fvdl 	struct vop_lock_args /* {
    286    1.6      fvdl 		struct vnode *a_vp;
    287    1.6      fvdl 		int a_flags;
    288    1.6      fvdl 	} */ *ap = v;
    289    1.6      fvdl 	struct vnode *vp = ap->a_vp;
    290  1.163        ad 	int flags = ap->a_flags;
    291    1.6      fvdl 
    292  1.178   hannken 	return (vlockmgr(&vp->v_lock, flags));
    293    1.6      fvdl }
    294    1.6      fvdl 
    295    1.6      fvdl /*
    296   1.12  wrstuden  * Unlock the node.
    297    1.6      fvdl  */
    298    1.6      fvdl int
    299   1.53     enami genfs_unlock(void *v)
    300    1.6      fvdl {
    301    1.6      fvdl 	struct vop_unlock_args /* {
    302    1.6      fvdl 		struct vnode *a_vp;
    303    1.6      fvdl 	} */ *ap = v;
    304    1.6      fvdl 	struct vnode *vp = ap->a_vp;
    305    1.6      fvdl 
    306  1.178   hannken 	return (vlockmgr(&vp->v_lock, LK_RELEASE));
    307    1.6      fvdl }
    308    1.6      fvdl 
    309    1.6      fvdl /*
    310   1.12  wrstuden  * Return whether or not the node is locked.
    311    1.6      fvdl  */
    312    1.6      fvdl int
    313   1.53     enami genfs_islocked(void *v)
    314    1.6      fvdl {
    315    1.6      fvdl 	struct vop_islocked_args /* {
    316    1.6      fvdl 		struct vnode *a_vp;
    317    1.6      fvdl 	} */ *ap = v;
    318    1.6      fvdl 	struct vnode *vp = ap->a_vp;
    319    1.6      fvdl 
    320  1.178   hannken 	return (vlockstatus(&vp->v_lock));
    321   1.12  wrstuden }
    322   1.12  wrstuden 
    323   1.12  wrstuden /*
    324   1.12  wrstuden  * Stubs to use when there is no locking to be done on the underlying object.
    325   1.12  wrstuden  */
    326   1.12  wrstuden int
    327   1.53     enami genfs_nolock(void *v)
    328   1.12  wrstuden {
    329   1.12  wrstuden 
    330   1.12  wrstuden 	return (0);
    331   1.12  wrstuden }
    332   1.12  wrstuden 
    333   1.12  wrstuden int
    334  1.138  christos genfs_nounlock(void *v)
    335   1.12  wrstuden {
    336   1.53     enami 
    337   1.12  wrstuden 	return (0);
    338   1.12  wrstuden }
    339   1.12  wrstuden 
    340   1.12  wrstuden int
    341  1.138  christos genfs_noislocked(void *v)
    342   1.12  wrstuden {
    343   1.53     enami 
    344   1.12  wrstuden 	return (0);
    345    1.8   thorpej }
    346    1.8   thorpej 
    347   1.34       chs int
    348  1.138  christos genfs_mmap(void *v)
    349   1.34       chs {
    350   1.53     enami 
    351   1.53     enami 	return (0);
    352   1.21       chs }
    353   1.21       chs 
    354  1.168     pooka /*
    355  1.168     pooka  * VOP_PUTPAGES() for vnodes which never have pages.
    356  1.168     pooka  */
    357  1.168     pooka 
    358  1.168     pooka int
    359  1.168     pooka genfs_null_putpages(void *v)
    360  1.168     pooka {
    361  1.168     pooka 	struct vop_putpages_args /* {
    362  1.168     pooka 		struct vnode *a_vp;
    363  1.168     pooka 		voff_t a_offlo;
    364  1.168     pooka 		voff_t a_offhi;
    365  1.168     pooka 		int a_flags;
    366  1.168     pooka 	} */ *ap = v;
    367  1.168     pooka 	struct vnode *vp = ap->a_vp;
    368  1.168     pooka 
    369  1.168     pooka 	KASSERT(vp->v_uobj.uo_npages == 0);
    370  1.168     pooka 	mutex_exit(&vp->v_interlock);
    371  1.168     pooka 	return (0);
    372  1.168     pooka }
    373  1.168     pooka 
    374   1.37       chs void
    375   1.98      yamt genfs_node_init(struct vnode *vp, const struct genfs_ops *ops)
    376   1.37       chs {
    377   1.37       chs 	struct genfs_node *gp = VTOG(vp);
    378   1.37       chs 
    379  1.146        ad 	rw_init(&gp->g_glock);
    380   1.37       chs 	gp->g_op = ops;
    381   1.37       chs }
    382   1.37       chs 
    383   1.37       chs void
    384  1.147        ad genfs_node_destroy(struct vnode *vp)
    385  1.147        ad {
    386  1.147        ad 	struct genfs_node *gp = VTOG(vp);
    387  1.147        ad 
    388  1.147        ad 	rw_destroy(&gp->g_glock);
    389  1.147        ad }
    390  1.147        ad 
    391  1.147        ad void
    392  1.138  christos genfs_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
    393   1.21       chs {
    394   1.21       chs 	int bsize;
    395   1.21       chs 
    396   1.37       chs 	bsize = 1 << vp->v_mount->mnt_fs_bshift;
    397   1.37       chs 	*eobp = (size + bsize - 1) & ~(bsize - 1);
    398   1.43       chs }
    399   1.43       chs 
    400   1.66  jdolecek static void
    401   1.66  jdolecek filt_genfsdetach(struct knote *kn)
    402   1.66  jdolecek {
    403   1.66  jdolecek 	struct vnode *vp = (struct vnode *)kn->kn_hook;
    404   1.66  jdolecek 
    405  1.164        ad 	mutex_enter(&vp->v_interlock);
    406   1.66  jdolecek 	SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
    407  1.164        ad 	mutex_exit(&vp->v_interlock);
    408   1.66  jdolecek }
    409   1.66  jdolecek 
    410   1.66  jdolecek static int
    411   1.66  jdolecek filt_genfsread(struct knote *kn, long hint)
    412   1.66  jdolecek {
    413   1.66  jdolecek 	struct vnode *vp = (struct vnode *)kn->kn_hook;
    414  1.164        ad 	int rv;
    415   1.66  jdolecek 
    416   1.66  jdolecek 	/*
    417   1.66  jdolecek 	 * filesystem is gone, so set the EOF flag and schedule
    418   1.66  jdolecek 	 * the knote for deletion.
    419   1.66  jdolecek 	 */
    420  1.164        ad 	switch (hint) {
    421  1.164        ad 	case NOTE_REVOKE:
    422  1.164        ad 		KASSERT(mutex_owned(&vp->v_interlock));
    423   1.66  jdolecek 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
    424   1.66  jdolecek 		return (1);
    425  1.164        ad 	case 0:
    426  1.164        ad 		mutex_enter(&vp->v_interlock);
    427  1.165        ad 		kn->kn_data = vp->v_size - ((file_t *)kn->kn_obj)->f_offset;
    428  1.164        ad 		rv = (kn->kn_data != 0);
    429  1.164        ad 		mutex_exit(&vp->v_interlock);
    430  1.164        ad 		return rv;
    431  1.164        ad 	default:
    432  1.164        ad 		KASSERT(mutex_owned(&vp->v_interlock));
    433  1.165        ad 		kn->kn_data = vp->v_size - ((file_t *)kn->kn_obj)->f_offset;
    434  1.164        ad 		return (kn->kn_data != 0);
    435   1.66  jdolecek 	}
    436   1.66  jdolecek }
    437   1.66  jdolecek 
    438   1.66  jdolecek static int
    439   1.66  jdolecek filt_genfsvnode(struct knote *kn, long hint)
    440   1.66  jdolecek {
    441  1.164        ad 	struct vnode *vp = (struct vnode *)kn->kn_hook;
    442  1.164        ad 	int fflags;
    443   1.66  jdolecek 
    444  1.164        ad 	switch (hint) {
    445  1.164        ad 	case NOTE_REVOKE:
    446  1.164        ad 		KASSERT(mutex_owned(&vp->v_interlock));
    447   1.66  jdolecek 		kn->kn_flags |= EV_EOF;
    448  1.164        ad 		if ((kn->kn_sfflags & hint) != 0)
    449  1.164        ad 			kn->kn_fflags |= hint;
    450   1.66  jdolecek 		return (1);
    451  1.164        ad 	case 0:
    452  1.164        ad 		mutex_enter(&vp->v_interlock);
    453  1.164        ad 		fflags = kn->kn_fflags;
    454  1.164        ad 		mutex_exit(&vp->v_interlock);
    455  1.164        ad 		break;
    456  1.164        ad 	default:
    457  1.164        ad 		KASSERT(mutex_owned(&vp->v_interlock));
    458  1.164        ad 		if ((kn->kn_sfflags & hint) != 0)
    459  1.164        ad 			kn->kn_fflags |= hint;
    460  1.164        ad 		fflags = kn->kn_fflags;
    461  1.164        ad 		break;
    462   1.66  jdolecek 	}
    463  1.164        ad 
    464  1.165        ad 	return (fflags != 0);
    465   1.66  jdolecek }
    466   1.66  jdolecek 
    467   1.96     perry static const struct filterops genfsread_filtops =
    468   1.66  jdolecek 	{ 1, NULL, filt_genfsdetach, filt_genfsread };
    469   1.96     perry static const struct filterops genfsvnode_filtops =
    470   1.66  jdolecek 	{ 1, NULL, filt_genfsdetach, filt_genfsvnode };
    471   1.66  jdolecek 
    472   1.66  jdolecek int
    473   1.66  jdolecek genfs_kqfilter(void *v)
    474   1.66  jdolecek {
    475   1.66  jdolecek 	struct vop_kqfilter_args /* {
    476   1.66  jdolecek 		struct vnode	*a_vp;
    477   1.66  jdolecek 		struct knote	*a_kn;
    478   1.66  jdolecek 	} */ *ap = v;
    479   1.66  jdolecek 	struct vnode *vp;
    480   1.66  jdolecek 	struct knote *kn;
    481   1.66  jdolecek 
    482   1.66  jdolecek 	vp = ap->a_vp;
    483   1.66  jdolecek 	kn = ap->a_kn;
    484   1.66  jdolecek 	switch (kn->kn_filter) {
    485   1.66  jdolecek 	case EVFILT_READ:
    486   1.66  jdolecek 		kn->kn_fop = &genfsread_filtops;
    487   1.66  jdolecek 		break;
    488   1.66  jdolecek 	case EVFILT_VNODE:
    489   1.66  jdolecek 		kn->kn_fop = &genfsvnode_filtops;
    490   1.66  jdolecek 		break;
    491   1.66  jdolecek 	default:
    492  1.159     pooka 		return (EINVAL);
    493   1.66  jdolecek 	}
    494   1.66  jdolecek 
    495   1.66  jdolecek 	kn->kn_hook = vp;
    496   1.66  jdolecek 
    497  1.164        ad 	mutex_enter(&vp->v_interlock);
    498   1.66  jdolecek 	SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
    499  1.164        ad 	mutex_exit(&vp->v_interlock);
    500   1.66  jdolecek 
    501   1.66  jdolecek 	return (0);
    502    1.1   mycroft }
    503  1.136      yamt 
    504  1.136      yamt void
    505  1.136      yamt genfs_node_wrlock(struct vnode *vp)
    506  1.136      yamt {
    507  1.136      yamt 	struct genfs_node *gp = VTOG(vp);
    508  1.136      yamt 
    509  1.146        ad 	rw_enter(&gp->g_glock, RW_WRITER);
    510  1.136      yamt }
    511  1.136      yamt 
    512  1.136      yamt void
    513  1.136      yamt genfs_node_rdlock(struct vnode *vp)
    514  1.136      yamt {
    515  1.136      yamt 	struct genfs_node *gp = VTOG(vp);
    516  1.136      yamt 
    517  1.146        ad 	rw_enter(&gp->g_glock, RW_READER);
    518  1.136      yamt }
    519  1.136      yamt 
    520  1.176  uebayasi int
    521  1.175  uebayasi genfs_node_rdtrylock(struct vnode *vp)
    522  1.175  uebayasi {
    523  1.175  uebayasi 	struct genfs_node *gp = VTOG(vp);
    524  1.175  uebayasi 
    525  1.176  uebayasi 	return rw_tryenter(&gp->g_glock, RW_READER);
    526  1.175  uebayasi }
    527  1.175  uebayasi 
    528  1.175  uebayasi void
    529  1.136      yamt genfs_node_unlock(struct vnode *vp)
    530  1.136      yamt {
    531  1.136      yamt 	struct genfs_node *gp = VTOG(vp);
    532  1.136      yamt 
    533  1.146        ad 	rw_exit(&gp->g_glock);
    534  1.136      yamt }
    535  1.169      elad 
    536  1.169      elad /*
    537  1.172      elad  * Do the usual access checking.
    538  1.172      elad  * file_mode, uid and gid are from the vnode in question,
    539  1.172      elad  * while acc_mode and cred are from the VOP_ACCESS parameter list
    540  1.172      elad  */
    541  1.172      elad int
    542  1.172      elad genfs_can_access(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
    543  1.172      elad     mode_t acc_mode, kauth_cred_t cred)
    544  1.172      elad {
    545  1.172      elad 	mode_t mask;
    546  1.172      elad 	int error, ismember;
    547  1.172      elad 
    548  1.172      elad 	/*
    549  1.172      elad 	 * Super-user always gets read/write access, but execute access depends
    550  1.172      elad 	 * on at least one execute bit being set.
    551  1.172      elad 	 */
    552  1.172      elad 	if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0) {
    553  1.172      elad 		if ((acc_mode & VEXEC) && type != VDIR &&
    554  1.172      elad 		    (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
    555  1.172      elad 			return (EACCES);
    556  1.172      elad 		return (0);
    557  1.172      elad 	}
    558  1.172      elad 
    559  1.172      elad 	mask = 0;
    560  1.172      elad 
    561  1.172      elad 	/* Otherwise, check the owner. */
    562  1.172      elad 	if (kauth_cred_geteuid(cred) == uid) {
    563  1.172      elad 		if (acc_mode & VEXEC)
    564  1.172      elad 			mask |= S_IXUSR;
    565  1.172      elad 		if (acc_mode & VREAD)
    566  1.172      elad 			mask |= S_IRUSR;
    567  1.172      elad 		if (acc_mode & VWRITE)
    568  1.172      elad 			mask |= S_IWUSR;
    569  1.172      elad 		return ((file_mode & mask) == mask ? 0 : EACCES);
    570  1.172      elad 	}
    571  1.172      elad 
    572  1.172      elad 	/* Otherwise, check the groups. */
    573  1.172      elad 	error = kauth_cred_ismember_gid(cred, gid, &ismember);
    574  1.172      elad 	if (error)
    575  1.172      elad 		return (error);
    576  1.172      elad 	if (kauth_cred_getegid(cred) == gid || ismember) {
    577  1.172      elad 		if (acc_mode & VEXEC)
    578  1.172      elad 			mask |= S_IXGRP;
    579  1.172      elad 		if (acc_mode & VREAD)
    580  1.172      elad 			mask |= S_IRGRP;
    581  1.172      elad 		if (acc_mode & VWRITE)
    582  1.172      elad 			mask |= S_IWGRP;
    583  1.172      elad 		return ((file_mode & mask) == mask ? 0 : EACCES);
    584  1.172      elad 	}
    585  1.172      elad 
    586  1.172      elad 	/* Otherwise, check everyone else. */
    587  1.172      elad 	if (acc_mode & VEXEC)
    588  1.172      elad 		mask |= S_IXOTH;
    589  1.172      elad 	if (acc_mode & VREAD)
    590  1.172      elad 		mask |= S_IROTH;
    591  1.172      elad 	if (acc_mode & VWRITE)
    592  1.172      elad 		mask |= S_IWOTH;
    593  1.172      elad 	return ((file_mode & mask) == mask ? 0 : EACCES);
    594  1.172      elad }
    595  1.172      elad 
    596  1.172      elad /*
    597  1.169      elad  * Common routine to check if chmod() is allowed.
    598  1.169      elad  *
    599  1.169      elad  * Policy:
    600  1.169      elad  *   - You must be root, or
    601  1.169      elad  *   - You must own the file, and
    602  1.169      elad  *     - You must not set the "sticky" bit (meaningless, see chmod(2))
    603  1.169      elad  *     - You must be a member of the group if you're trying to set the
    604  1.169      elad  *       SGIDf bit
    605  1.169      elad  *
    606  1.169      elad  * cred - credentials of the invoker
    607  1.169      elad  * vp - vnode of the file-system object
    608  1.169      elad  * cur_uid, cur_gid - current uid/gid of the file-system object
    609  1.169      elad  * new_mode - new mode for the file-system object
    610  1.169      elad  *
    611  1.169      elad  * Returns 0 if the change is allowed, or an error value otherwise.
    612  1.169      elad  */
    613  1.169      elad int
    614  1.169      elad genfs_can_chmod(vnode_t *vp, kauth_cred_t cred, uid_t cur_uid,
    615  1.169      elad     gid_t cur_gid, mode_t new_mode)
    616  1.169      elad {
    617  1.169      elad 	int error;
    618  1.169      elad 
    619  1.169      elad 	/* Superuser can always change mode. */
    620  1.169      elad 	error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    621  1.169      elad 	    NULL);
    622  1.169      elad 	if (!error)
    623  1.169      elad 		return (0);
    624  1.169      elad 
    625  1.169      elad 	/* Otherwise, user must own the file. */
    626  1.169      elad 	if (kauth_cred_geteuid(cred) != cur_uid)
    627  1.169      elad 		return (EPERM);
    628  1.169      elad 
    629  1.169      elad 	/*
    630  1.169      elad 	 * Non-root users can't set the sticky bit on files.
    631  1.169      elad 	 */
    632  1.169      elad 	if ((vp->v_type != VDIR) && (new_mode & S_ISTXT))
    633  1.169      elad 		return (EFTYPE);
    634  1.169      elad 
    635  1.169      elad 	/*
    636  1.169      elad 	 * If the invoker is trying to set the SGID bit on the file,
    637  1.169      elad 	 * check group membership.
    638  1.169      elad 	 */
    639  1.169      elad 	if (new_mode & S_ISGID) {
    640  1.169      elad 		int ismember;
    641  1.169      elad 
    642  1.169      elad 		error = kauth_cred_ismember_gid(cred, cur_gid,
    643  1.169      elad 		    &ismember);
    644  1.169      elad 		if (error || !ismember)
    645  1.169      elad 			return (EPERM);
    646  1.169      elad 	}
    647  1.169      elad 
    648  1.169      elad 	return (0);
    649  1.169      elad }
    650  1.169      elad 
    651  1.169      elad /*
    652  1.169      elad  * Common routine to check if chown() is allowed.
    653  1.169      elad  *
    654  1.169      elad  * Policy:
    655  1.169      elad  *   - You must be root, or
    656  1.169      elad  *   - You must own the file, and
    657  1.169      elad  *     - You must not try to change ownership, and
    658  1.169      elad  *     - You must be member of the new group
    659  1.169      elad  *
    660  1.169      elad  * cred - credentials of the invoker
    661  1.169      elad  * cur_uid, cur_gid - current uid/gid of the file-system object
    662  1.169      elad  * new_uid, new_gid - target uid/gid of the file-system object
    663  1.169      elad  *
    664  1.169      elad  * Returns 0 if the change is allowed, or an error value otherwise.
    665  1.169      elad  */
    666  1.169      elad int
    667  1.169      elad genfs_can_chown(vnode_t *vp, kauth_cred_t cred, uid_t cur_uid,
    668  1.169      elad     gid_t cur_gid, uid_t new_uid, gid_t new_gid)
    669  1.169      elad {
    670  1.169      elad 	int error, ismember;
    671  1.169      elad 
    672  1.169      elad 	/*
    673  1.169      elad 	 * You can only change ownership of a file if:
    674  1.169      elad 	 * You are the superuser, or...
    675  1.169      elad 	 */
    676  1.169      elad 	error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    677  1.169      elad 	    NULL);
    678  1.169      elad 	if (!error)
    679  1.169      elad 		return (0);
    680  1.169      elad 
    681  1.169      elad 	/*
    682  1.169      elad 	 * You own the file and...
    683  1.169      elad 	 */
    684  1.169      elad 	if (kauth_cred_geteuid(cred) == cur_uid) {
    685  1.169      elad 		/*
    686  1.169      elad 		 * You don't try to change ownership, and...
    687  1.169      elad 		 */
    688  1.169      elad 		if (new_uid != cur_uid)
    689  1.169      elad 			return (EPERM);
    690  1.169      elad 
    691  1.169      elad 		/*
    692  1.169      elad 		 * You don't try to change group (no-op), or...
    693  1.169      elad 		 */
    694  1.169      elad 		if (new_gid == cur_gid)
    695  1.169      elad 			return (0);
    696  1.169      elad 
    697  1.169      elad 		/*
    698  1.169      elad 		 * Your effective gid is the new gid, or...
    699  1.169      elad 		 */
    700  1.169      elad 		if (kauth_cred_getegid(cred) == new_gid)
    701  1.169      elad 			return (0);
    702  1.169      elad 
    703  1.169      elad 		/*
    704  1.169      elad 		 * The new gid is one you're a member of.
    705  1.169      elad 		 */
    706  1.169      elad 		ismember = 0;
    707  1.169      elad 		error = kauth_cred_ismember_gid(cred, new_gid,
    708  1.169      elad 		    &ismember);
    709  1.174       roy 		if (!error && ismember)
    710  1.174       roy 			return (0);
    711  1.169      elad 	}
    712  1.169      elad 
    713  1.173     pooka 	return (EPERM);
    714  1.169      elad }
    715  1.169      elad 
    716  1.170      elad /*
    717  1.170      elad  * Common routine to check if the device can be mounted.
    718  1.170      elad  *
    719  1.170      elad  * devvp - the locked vnode of the device
    720  1.170      elad  * cred - credentials of the invoker
    721  1.170      elad  * accessmode - the accessmode (VREAD, VWRITE)
    722  1.170      elad  *
    723  1.170      elad  * Returns 0 if the mount is allowed, or an error value otherwise.
    724  1.170      elad  */
    725  1.170      elad int
    726  1.170      elad genfs_can_mount(vnode_t *devvp, mode_t accessmode, kauth_cred_t cred)
    727  1.170      elad {
    728  1.170      elad 	int error;
    729  1.170      elad 
    730  1.170      elad 	/* Always allow for root. */
    731  1.170      elad 	error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL);
    732  1.170      elad 	if (!error)
    733  1.170      elad 		return (0);
    734  1.170      elad 
    735  1.170      elad 	error = VOP_ACCESS(devvp, accessmode, cred);
    736  1.170      elad 
    737  1.170      elad 	return (error);
    738  1.170      elad }
    739  1.171      elad 
    740  1.171      elad int
    741  1.171      elad genfs_can_chtimes(vnode_t *vp, u_int vaflags, uid_t owner_uid,
    742  1.171      elad     kauth_cred_t cred)
    743  1.171      elad {
    744  1.171      elad 	int error;
    745  1.171      elad 
    746  1.171      elad 	/* Must be root, or... */
    747  1.171      elad 	error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL);
    748  1.171      elad 	if (!error)
    749  1.171      elad 		return (0);
    750  1.171      elad 
    751  1.171      elad 	/* must be owner, or... */
    752  1.171      elad 	if (kauth_cred_geteuid(cred) == owner_uid)
    753  1.171      elad 		return (0);
    754  1.171      elad 
    755  1.171      elad 	/* set the times to the current time, and... */
    756  1.171      elad 	if ((vaflags & VA_UTIMES_NULL) == 0)
    757  1.171      elad 		return (EPERM);
    758  1.171      elad 
    759  1.171      elad 	/* have write access. */
    760  1.171      elad 	error = VOP_ACCESS(vp, VWRITE, cred);
    761  1.171      elad 	if (error)
    762  1.171      elad 		return (error);
    763  1.171      elad 
    764  1.171      elad 	return (0);
    765  1.171      elad }
    766  1.171      elad 
    767