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.
20 * fsnotify inode mark locking/lifetime/and refcnting
23 * The group->recnt and mark->refcnt tell how many "things" in the kernel
24 * currently are referencing the objects. Both kind of objects typically will
25 * live inside the kernel with a refcnt of 2, one for its creation and one for
26 * the reference a group and a mark hold to each other.
27 * If you are holding the appropriate locks, you can take a reference and the
28 * object itself is guaranteed to survive until the reference is dropped.
31 * There are 3 locks involved with fsnotify inode marks and they MUST be taken
32 * in order as follows:
38 * group->mark_mutex protects the marks_list anchored inside a given group and
39 * each mark is hooked via the g_list. It also protects the groups private
40 * data (i.e group limits).
42 * mark->lock protects the marks attributes like its masks and flags.
43 * Furthermore it protects the access to a reference of the group that the mark
44 * is assigned to as well as the access to a reference of the inode/vfsmount
45 * that is being watched by the mark.
47 * inode->i_lock protects the i_fsnotify_marks list anchored inside a
48 * given inode and each mark is hooked via the i_list. (and sorta the
53 * Inode marks survive between when they are added to an inode and when their
56 * The inode mark can be cleared for a number of different reasons including:
57 * - The inode is unlinked for the last time. (fsnotify_inode_remove)
58 * - The inode is being evicted from cache. (fsnotify_inode_delete)
59 * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes)
60 * - Something explicitly requests that it be removed. (fsnotify_destroy_mark)
61 * - The fsnotify_group associated with the mark is going away and all such marks
62 * need to be cleaned up. (fsnotify_clear_marks_by_group)
64 * Worst case we are given an inode and need to clean up all the marks on that
65 * inode. We take i_lock and walk the i_fsnotify_marks safely. For each
66 * mark on the list we take a reference (so the mark can't disappear under us).
67 * We remove that mark form the inode's list of marks and we add this mark to a
68 * private list anchored on the stack using i_free_list; we walk i_free_list
69 * and before we destroy the mark we make sure that we dont race with a
70 * concurrent destroy_group by getting a ref to the marks group and taking the
73 * Very similarly for freeing by group, except we use free_g_list.
75 * This has the very interesting property of being able to run concurrently with
76 * any (or all) other directions.
80 #include <linux/init.h>
81 #include <linux/kernel.h>
82 #include <linux/kthread.h>
83 #include <linux/module.h>
84 #include <linux/mutex.h>
85 #include <linux/slab.h>
86 #include <linux/spinlock.h>
87 #include <linux/srcu.h>
89 #include <linux/atomic.h>
91 #include <linux/fsnotify_backend.h>
94 struct srcu_struct fsnotify_mark_srcu;
96 void fsnotify_get_mark(struct fsnotify_mark *mark)
98 atomic_inc(&mark->refcnt);
101 void fsnotify_put_mark(struct fsnotify_mark *mark)
103 if (atomic_dec_and_test(&mark->refcnt)) {
105 fsnotify_put_group(mark->group);
106 mark->free_mark(mark);
109 EXPORT_SYMBOL_GPL(fsnotify_put_mark);
111 /* Calculate mask of events for a list of marks */
112 u32 fsnotify_recalc_mask(struct hlist_head *head)
115 struct fsnotify_mark *mark;
117 hlist_for_each_entry(mark, head, obj_list)
118 new_mask |= mark->mask;
123 * Remove mark from inode / vfsmount list, group list, drop inode reference
126 * Must be called with group->mark_mutex held.
128 void fsnotify_detach_mark(struct fsnotify_mark *mark)
130 struct inode *inode = NULL;
131 struct fsnotify_group *group = mark->group;
133 BUG_ON(!mutex_is_locked(&group->mark_mutex));
135 spin_lock(&mark->lock);
137 /* something else already called this function on this mark */
138 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
139 spin_unlock(&mark->lock);
143 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
145 if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
147 fsnotify_destroy_inode_mark(mark);
148 } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT)
149 fsnotify_destroy_vfsmount_mark(mark);
153 * Note that we didn't update flags telling whether inode cares about
154 * what's happening with children. We update these flags from
155 * __fsnotify_parent() lazily when next event happens on one of our
159 list_del_init(&mark->g_list);
161 spin_unlock(&mark->lock);
163 if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED))
166 atomic_dec(&group->num_marks);
170 fsnotify_mark_free_rcu(struct rcu_head *rcu)
172 struct fsnotify_mark *mark;
174 mark = container_of(rcu, struct fsnotify_mark, g_rcu);
175 fsnotify_put_mark(mark);
179 * Free fsnotify mark. The freeing is actually happening from a call_srcu
180 * callback. Caller must have a reference to the mark or be protected by
181 * fsnotify_mark_srcu.
183 void fsnotify_free_mark(struct fsnotify_mark *mark)
185 struct fsnotify_group *group = mark->group;
187 spin_lock(&mark->lock);
188 /* something else already called this function on this mark */
189 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
190 spin_unlock(&mark->lock);
193 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
194 spin_unlock(&mark->lock);
196 call_srcu(&fsnotify_mark_srcu, &mark->g_rcu, fsnotify_mark_free_rcu);
199 * Some groups like to know that marks are being freed. This is a
200 * callback to the group function to let it know that this mark
203 if (group->ops->freeing_mark)
204 group->ops->freeing_mark(mark, group);
207 void fsnotify_destroy_mark(struct fsnotify_mark *mark,
208 struct fsnotify_group *group)
210 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
211 fsnotify_detach_mark(mark);
212 mutex_unlock(&group->mark_mutex);
213 fsnotify_free_mark(mark);
215 EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
217 void fsnotify_destroy_marks(struct hlist_head *head, spinlock_t *lock)
219 struct fsnotify_mark *mark;
223 * We have to be careful since we can race with e.g.
224 * fsnotify_clear_marks_by_group() and once we drop 'lock',
225 * mark can get removed from the obj_list and destroyed. But
226 * we are holding mark reference so mark cannot be freed and
227 * calling fsnotify_destroy_mark() more than once is fine.
230 if (hlist_empty(head)) {
234 mark = hlist_entry(head->first, struct fsnotify_mark, obj_list);
236 * We don't update i_fsnotify_mask / mnt_fsnotify_mask here
237 * since inode / mount is going away anyway. So just remove
238 * mark from the list.
240 hlist_del_init_rcu(&mark->obj_list);
241 fsnotify_get_mark(mark);
243 fsnotify_destroy_mark(mark, mark->group);
244 fsnotify_put_mark(mark);
248 void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
250 assert_spin_locked(&mark->lock);
254 if (mark->flags & FSNOTIFY_MARK_FLAG_INODE)
255 fsnotify_set_inode_mark_mask_locked(mark, mask);
258 void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
260 assert_spin_locked(&mark->lock);
262 mark->ignored_mask = mask;
266 * Sorting function for lists of fsnotify marks.
268 * Fanotify supports different notification classes (reflected as priority of
269 * notification group). Events shall be passed to notification groups in
270 * decreasing priority order. To achieve this marks in notification lists for
271 * inodes and vfsmounts are sorted so that priorities of corresponding groups
274 * Furthermore correct handling of the ignore mask requires processing inode
275 * and vfsmount marks of each group together. Using the group address as
276 * further sort criterion provides a unique sorting order and thus we can
277 * merge inode and vfsmount lists of marks in linear time and find groups
278 * present in both lists.
280 * A return value of 1 signifies that b has priority over a.
281 * A return value of 0 signifies that the two marks have to be handled together.
282 * A return value of -1 signifies that a has priority over b.
284 int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
292 if (a->priority < b->priority)
294 if (a->priority > b->priority)
301 /* Add mark into proper place in given list of marks */
302 int fsnotify_add_mark_list(struct hlist_head *head, struct fsnotify_mark *mark,
305 struct fsnotify_mark *lmark, *last = NULL;
308 /* is mark the first mark? */
309 if (hlist_empty(head)) {
310 hlist_add_head_rcu(&mark->obj_list, head);
314 /* should mark be in the middle of the current list? */
315 hlist_for_each_entry(lmark, head, obj_list) {
318 if ((lmark->group == mark->group) && !allow_dups)
321 cmp = fsnotify_compare_groups(lmark->group, mark->group);
323 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
328 BUG_ON(last == NULL);
329 /* mark should be the last entry. last is the current last entry */
330 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
335 * Attach an initialized mark to a given group and fs object.
336 * These marks may be used for the fsnotify backend to determine which
337 * event types should be delivered to which group.
339 int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
340 struct fsnotify_group *group, struct inode *inode,
341 struct vfsmount *mnt, int allow_dups)
345 BUG_ON(inode && mnt);
346 BUG_ON(!inode && !mnt);
347 BUG_ON(!mutex_is_locked(&group->mark_mutex));
355 spin_lock(&mark->lock);
356 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
358 fsnotify_get_group(group);
360 list_add(&mark->g_list, &group->marks_list);
361 atomic_inc(&group->num_marks);
362 fsnotify_get_mark(mark); /* for i_list and g_list */
365 ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups);
369 ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups);
376 /* this will pin the object if appropriate */
377 fsnotify_set_mark_mask_locked(mark, mark->mask);
378 spin_unlock(&mark->lock);
381 __fsnotify_update_child_dentry_flags(inode);
385 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
386 list_del_init(&mark->g_list);
387 fsnotify_put_group(group);
389 atomic_dec(&group->num_marks);
391 spin_unlock(&mark->lock);
393 call_srcu(&fsnotify_mark_srcu, &mark->g_rcu, fsnotify_mark_free_rcu);
397 int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
398 struct inode *inode, struct vfsmount *mnt, int allow_dups)
401 mutex_lock(&group->mark_mutex);
402 ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
403 mutex_unlock(&group->mark_mutex);
406 EXPORT_SYMBOL_GPL(fsnotify_add_mark);
409 * Given a list of marks, find the mark associated with given group. If found
410 * take a reference to that mark and return it, else return NULL.
412 struct fsnotify_mark *fsnotify_find_mark(struct hlist_head *head,
413 struct fsnotify_group *group)
415 struct fsnotify_mark *mark;
417 hlist_for_each_entry(mark, head, obj_list) {
418 if (mark->group == group) {
419 fsnotify_get_mark(mark);
427 * clear any marks in a group in which mark->flags & flags is true
429 void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
432 struct fsnotify_mark *lmark, *mark;
436 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
437 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
438 * to_free list so we have to use mark_mutex even when accessing that
439 * list. And freeing mark requires us to drop mark_mutex. So we can
440 * reliably free only the first mark in the list. That's why we first
441 * move marks to free to to_free list in one go and then free marks in
442 * to_free list one by one.
444 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
445 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
446 if (mark->flags & flags)
447 list_move(&mark->g_list, &to_free);
449 mutex_unlock(&group->mark_mutex);
452 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
453 if (list_empty(&to_free)) {
454 mutex_unlock(&group->mark_mutex);
457 mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
458 fsnotify_get_mark(mark);
459 fsnotify_detach_mark(mark);
460 mutex_unlock(&group->mark_mutex);
461 fsnotify_free_mark(mark);
462 fsnotify_put_mark(mark);
467 * Given a group, destroy all of the marks associated with that group.
469 void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
471 fsnotify_clear_marks_by_group_flags(group, (unsigned int)-1);
474 void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old)
476 assert_spin_locked(&old->lock);
477 new->inode = old->inode;
480 fsnotify_get_group(old->group);
481 new->group = old->group;
482 new->mask = old->mask;
483 new->free_mark = old->free_mark;
487 * Nothing fancy, just initialize lists and locks and counters.
489 void fsnotify_init_mark(struct fsnotify_mark *mark,
490 void (*free_mark)(struct fsnotify_mark *mark))
492 memset(mark, 0, sizeof(*mark));
493 spin_lock_init(&mark->lock);
494 atomic_set(&mark->refcnt, 1);
495 mark->free_mark = free_mark;
497 EXPORT_SYMBOL_GPL(fsnotify_init_mark);