2 * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <linux/list.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/srcu.h>
23 #include <linux/rculist.h>
24 #include <linux/wait.h>
26 #include <linux/fsnotify_backend.h>
29 #include <asm/atomic.h>
31 /* protects writes to fsnotify_groups and fsnotify_mask */
32 static DEFINE_MUTEX(fsnotify_grp_mutex);
33 /* protects reads while running the fsnotify_groups list */
34 struct srcu_struct fsnotify_grp_srcu;
35 /* all groups registered to receive filesystem notifications */
36 LIST_HEAD(fsnotify_groups);
37 /* bitwise OR of all events (FS_*) interesting to some group on this system */
41 * When a new group registers or changes it's set of interesting events
42 * this function updates the fsnotify_mask to contain all interesting events
44 void fsnotify_recalc_global_mask(void)
46 struct fsnotify_group *group;
50 idx = srcu_read_lock(&fsnotify_grp_srcu);
51 list_for_each_entry_rcu(group, &fsnotify_groups, group_list)
53 srcu_read_unlock(&fsnotify_grp_srcu, idx);
58 * Take a reference to a group so things found under the fsnotify_grp_mutex
59 * can't get freed under us
61 static void fsnotify_get_group(struct fsnotify_group *group)
63 atomic_inc(&group->refcnt);
67 * Final freeing of a group
69 static void fsnotify_destroy_group(struct fsnotify_group *group)
71 if (group->ops->free_group_priv)
72 group->ops->free_group_priv(group);
78 * Remove this group from the global list of groups that will get events
79 * this can be done even if there are still references and things still using
80 * this group. This just stops the group from getting new events.
82 static void __fsnotify_evict_group(struct fsnotify_group *group)
84 BUG_ON(!mutex_is_locked(&fsnotify_grp_mutex));
86 if (group->on_group_list)
87 list_del_rcu(&group->group_list);
88 group->on_group_list = 0;
92 * Called when a group is no longer interested in getting events. This can be
93 * used if a group is misbehaving or if for some reason a group should no longer
94 * get any filesystem events.
96 void fsnotify_evict_group(struct fsnotify_group *group)
98 mutex_lock(&fsnotify_grp_mutex);
99 __fsnotify_evict_group(group);
100 mutex_unlock(&fsnotify_grp_mutex);
104 * Drop a reference to a group. Free it if it's through.
106 void fsnotify_put_group(struct fsnotify_group *group)
108 if (!atomic_dec_and_mutex_lock(&group->refcnt, &fsnotify_grp_mutex))
112 * OK, now we know that there's no other users *and* we hold mutex,
113 * so no new references will appear
115 __fsnotify_evict_group(group);
118 * now it's off the list, so the only thing we might care about is
121 mutex_unlock(&fsnotify_grp_mutex);
122 synchronize_srcu(&fsnotify_grp_srcu);
124 /* and now it is really dead. _Nothing_ could be seeing it */
125 fsnotify_recalc_global_mask();
126 fsnotify_destroy_group(group);
130 * Simply run the fsnotify_groups list and find a group which matches
131 * the given parameters. If a group is found we take a reference to that
134 static struct fsnotify_group *fsnotify_find_group(unsigned int group_num, __u32 mask,
135 const struct fsnotify_ops *ops)
137 struct fsnotify_group *group_iter;
138 struct fsnotify_group *group = NULL;
140 BUG_ON(!mutex_is_locked(&fsnotify_grp_mutex));
142 list_for_each_entry_rcu(group_iter, &fsnotify_groups, group_list) {
143 if (group_iter->group_num == group_num) {
144 if ((group_iter->mask == mask) &&
145 (group_iter->ops == ops)) {
146 fsnotify_get_group(group_iter);
149 group = ERR_PTR(-EEXIST);
156 * Either finds an existing group which matches the group_num, mask, and ops or
157 * creates a new group and adds it to the global group list. In either case we
158 * take a reference for the group returned.
160 struct fsnotify_group *fsnotify_obtain_group(unsigned int group_num, __u32 mask,
161 const struct fsnotify_ops *ops)
163 struct fsnotify_group *group, *tgroup;
165 /* very low use, simpler locking if we just always alloc */
166 group = kmalloc(sizeof(struct fsnotify_group), GFP_KERNEL);
168 return ERR_PTR(-ENOMEM);
170 atomic_set(&group->refcnt, 1);
172 group->on_group_list = 0;
173 group->group_num = group_num;
178 mutex_lock(&fsnotify_grp_mutex);
179 tgroup = fsnotify_find_group(group_num, mask, ops);
181 /* group already exists */
182 mutex_unlock(&fsnotify_grp_mutex);
183 /* destroy the new one we made */
184 fsnotify_put_group(group);
188 /* group not found, add a new one */
189 list_add_rcu(&group->group_list, &fsnotify_groups);
190 group->on_group_list = 1;
192 mutex_unlock(&fsnotify_grp_mutex);
195 fsnotify_recalc_global_mask();