1*90586523SEric Paris /* 2*90586523SEric Paris * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> 3*90586523SEric Paris * 4*90586523SEric Paris * This program is free software; you can redistribute it and/or modify 5*90586523SEric Paris * it under the terms of the GNU General Public License as published by 6*90586523SEric Paris * the Free Software Foundation; either version 2, or (at your option) 7*90586523SEric Paris * any later version. 8*90586523SEric Paris * 9*90586523SEric Paris * This program is distributed in the hope that it will be useful, 10*90586523SEric Paris * but WITHOUT ANY WARRANTY; without even the implied warranty of 11*90586523SEric Paris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12*90586523SEric Paris * GNU General Public License for more details. 13*90586523SEric Paris * 14*90586523SEric Paris * You should have received a copy of the GNU General Public License 15*90586523SEric Paris * along with this program; see the file COPYING. If not, write to 16*90586523SEric Paris * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 17*90586523SEric Paris */ 18*90586523SEric Paris 19*90586523SEric Paris #include <linux/list.h> 20*90586523SEric Paris #include <linux/mutex.h> 21*90586523SEric Paris #include <linux/slab.h> 22*90586523SEric Paris #include <linux/srcu.h> 23*90586523SEric Paris #include <linux/rculist.h> 24*90586523SEric Paris #include <linux/wait.h> 25*90586523SEric Paris 26*90586523SEric Paris #include <linux/fsnotify_backend.h> 27*90586523SEric Paris #include "fsnotify.h" 28*90586523SEric Paris 29*90586523SEric Paris #include <asm/atomic.h> 30*90586523SEric Paris 31*90586523SEric Paris /* protects writes to fsnotify_groups and fsnotify_mask */ 32*90586523SEric Paris static DEFINE_MUTEX(fsnotify_grp_mutex); 33*90586523SEric Paris /* protects reads while running the fsnotify_groups list */ 34*90586523SEric Paris struct srcu_struct fsnotify_grp_srcu; 35*90586523SEric Paris /* all groups registered to receive filesystem notifications */ 36*90586523SEric Paris LIST_HEAD(fsnotify_groups); 37*90586523SEric Paris /* bitwise OR of all events (FS_*) interesting to some group on this system */ 38*90586523SEric Paris __u32 fsnotify_mask; 39*90586523SEric Paris 40*90586523SEric Paris /* 41*90586523SEric Paris * When a new group registers or changes it's set of interesting events 42*90586523SEric Paris * this function updates the fsnotify_mask to contain all interesting events 43*90586523SEric Paris */ 44*90586523SEric Paris void fsnotify_recalc_global_mask(void) 45*90586523SEric Paris { 46*90586523SEric Paris struct fsnotify_group *group; 47*90586523SEric Paris __u32 mask = 0; 48*90586523SEric Paris int idx; 49*90586523SEric Paris 50*90586523SEric Paris idx = srcu_read_lock(&fsnotify_grp_srcu); 51*90586523SEric Paris list_for_each_entry_rcu(group, &fsnotify_groups, group_list) 52*90586523SEric Paris mask |= group->mask; 53*90586523SEric Paris srcu_read_unlock(&fsnotify_grp_srcu, idx); 54*90586523SEric Paris fsnotify_mask = mask; 55*90586523SEric Paris } 56*90586523SEric Paris 57*90586523SEric Paris /* 58*90586523SEric Paris * Take a reference to a group so things found under the fsnotify_grp_mutex 59*90586523SEric Paris * can't get freed under us 60*90586523SEric Paris */ 61*90586523SEric Paris static void fsnotify_get_group(struct fsnotify_group *group) 62*90586523SEric Paris { 63*90586523SEric Paris atomic_inc(&group->refcnt); 64*90586523SEric Paris } 65*90586523SEric Paris 66*90586523SEric Paris /* 67*90586523SEric Paris * Final freeing of a group 68*90586523SEric Paris */ 69*90586523SEric Paris static void fsnotify_destroy_group(struct fsnotify_group *group) 70*90586523SEric Paris { 71*90586523SEric Paris if (group->ops->free_group_priv) 72*90586523SEric Paris group->ops->free_group_priv(group); 73*90586523SEric Paris 74*90586523SEric Paris kfree(group); 75*90586523SEric Paris } 76*90586523SEric Paris 77*90586523SEric Paris /* 78*90586523SEric Paris * Remove this group from the global list of groups that will get events 79*90586523SEric Paris * this can be done even if there are still references and things still using 80*90586523SEric Paris * this group. This just stops the group from getting new events. 81*90586523SEric Paris */ 82*90586523SEric Paris static void __fsnotify_evict_group(struct fsnotify_group *group) 83*90586523SEric Paris { 84*90586523SEric Paris BUG_ON(!mutex_is_locked(&fsnotify_grp_mutex)); 85*90586523SEric Paris 86*90586523SEric Paris if (group->on_group_list) 87*90586523SEric Paris list_del_rcu(&group->group_list); 88*90586523SEric Paris group->on_group_list = 0; 89*90586523SEric Paris } 90*90586523SEric Paris 91*90586523SEric Paris /* 92*90586523SEric Paris * Called when a group is no longer interested in getting events. This can be 93*90586523SEric Paris * used if a group is misbehaving or if for some reason a group should no longer 94*90586523SEric Paris * get any filesystem events. 95*90586523SEric Paris */ 96*90586523SEric Paris void fsnotify_evict_group(struct fsnotify_group *group) 97*90586523SEric Paris { 98*90586523SEric Paris mutex_lock(&fsnotify_grp_mutex); 99*90586523SEric Paris __fsnotify_evict_group(group); 100*90586523SEric Paris mutex_unlock(&fsnotify_grp_mutex); 101*90586523SEric Paris } 102*90586523SEric Paris 103*90586523SEric Paris /* 104*90586523SEric Paris * Drop a reference to a group. Free it if it's through. 105*90586523SEric Paris */ 106*90586523SEric Paris void fsnotify_put_group(struct fsnotify_group *group) 107*90586523SEric Paris { 108*90586523SEric Paris if (!atomic_dec_and_mutex_lock(&group->refcnt, &fsnotify_grp_mutex)) 109*90586523SEric Paris return; 110*90586523SEric Paris 111*90586523SEric Paris /* 112*90586523SEric Paris * OK, now we know that there's no other users *and* we hold mutex, 113*90586523SEric Paris * so no new references will appear 114*90586523SEric Paris */ 115*90586523SEric Paris __fsnotify_evict_group(group); 116*90586523SEric Paris 117*90586523SEric Paris /* 118*90586523SEric Paris * now it's off the list, so the only thing we might care about is 119*90586523SEric Paris * srcu access.... 120*90586523SEric Paris */ 121*90586523SEric Paris mutex_unlock(&fsnotify_grp_mutex); 122*90586523SEric Paris synchronize_srcu(&fsnotify_grp_srcu); 123*90586523SEric Paris 124*90586523SEric Paris /* and now it is really dead. _Nothing_ could be seeing it */ 125*90586523SEric Paris fsnotify_recalc_global_mask(); 126*90586523SEric Paris fsnotify_destroy_group(group); 127*90586523SEric Paris } 128*90586523SEric Paris 129*90586523SEric Paris /* 130*90586523SEric Paris * Simply run the fsnotify_groups list and find a group which matches 131*90586523SEric Paris * the given parameters. If a group is found we take a reference to that 132*90586523SEric Paris * group. 133*90586523SEric Paris */ 134*90586523SEric Paris static struct fsnotify_group *fsnotify_find_group(unsigned int group_num, __u32 mask, 135*90586523SEric Paris const struct fsnotify_ops *ops) 136*90586523SEric Paris { 137*90586523SEric Paris struct fsnotify_group *group_iter; 138*90586523SEric Paris struct fsnotify_group *group = NULL; 139*90586523SEric Paris 140*90586523SEric Paris BUG_ON(!mutex_is_locked(&fsnotify_grp_mutex)); 141*90586523SEric Paris 142*90586523SEric Paris list_for_each_entry_rcu(group_iter, &fsnotify_groups, group_list) { 143*90586523SEric Paris if (group_iter->group_num == group_num) { 144*90586523SEric Paris if ((group_iter->mask == mask) && 145*90586523SEric Paris (group_iter->ops == ops)) { 146*90586523SEric Paris fsnotify_get_group(group_iter); 147*90586523SEric Paris group = group_iter; 148*90586523SEric Paris } else 149*90586523SEric Paris group = ERR_PTR(-EEXIST); 150*90586523SEric Paris } 151*90586523SEric Paris } 152*90586523SEric Paris return group; 153*90586523SEric Paris } 154*90586523SEric Paris 155*90586523SEric Paris /* 156*90586523SEric Paris * Either finds an existing group which matches the group_num, mask, and ops or 157*90586523SEric Paris * creates a new group and adds it to the global group list. In either case we 158*90586523SEric Paris * take a reference for the group returned. 159*90586523SEric Paris */ 160*90586523SEric Paris struct fsnotify_group *fsnotify_obtain_group(unsigned int group_num, __u32 mask, 161*90586523SEric Paris const struct fsnotify_ops *ops) 162*90586523SEric Paris { 163*90586523SEric Paris struct fsnotify_group *group, *tgroup; 164*90586523SEric Paris 165*90586523SEric Paris /* very low use, simpler locking if we just always alloc */ 166*90586523SEric Paris group = kmalloc(sizeof(struct fsnotify_group), GFP_KERNEL); 167*90586523SEric Paris if (!group) 168*90586523SEric Paris return ERR_PTR(-ENOMEM); 169*90586523SEric Paris 170*90586523SEric Paris atomic_set(&group->refcnt, 1); 171*90586523SEric Paris 172*90586523SEric Paris group->on_group_list = 0; 173*90586523SEric Paris group->group_num = group_num; 174*90586523SEric Paris group->mask = mask; 175*90586523SEric Paris 176*90586523SEric Paris group->ops = ops; 177*90586523SEric Paris 178*90586523SEric Paris mutex_lock(&fsnotify_grp_mutex); 179*90586523SEric Paris tgroup = fsnotify_find_group(group_num, mask, ops); 180*90586523SEric Paris if (tgroup) { 181*90586523SEric Paris /* group already exists */ 182*90586523SEric Paris mutex_unlock(&fsnotify_grp_mutex); 183*90586523SEric Paris /* destroy the new one we made */ 184*90586523SEric Paris fsnotify_put_group(group); 185*90586523SEric Paris return tgroup; 186*90586523SEric Paris } 187*90586523SEric Paris 188*90586523SEric Paris /* group not found, add a new one */ 189*90586523SEric Paris list_add_rcu(&group->group_list, &fsnotify_groups); 190*90586523SEric Paris group->on_group_list = 1; 191*90586523SEric Paris 192*90586523SEric Paris mutex_unlock(&fsnotify_grp_mutex); 193*90586523SEric Paris 194*90586523SEric Paris if (mask) 195*90586523SEric Paris fsnotify_recalc_global_mask(); 196*90586523SEric Paris 197*90586523SEric Paris return group; 198*90586523SEric Paris } 199