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
54 * refcnt==0. Marks are also protected by fsnotify_mark_srcu.
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 * This has the very interesting property of being able to run concurrently with
65 * any (or all) other directions.
69 #include <linux/init.h>
70 #include <linux/kernel.h>
71 #include <linux/kthread.h>
72 #include <linux/module.h>
73 #include <linux/mutex.h>
74 #include <linux/slab.h>
75 #include <linux/spinlock.h>
76 #include <linux/srcu.h>
78 #include <linux/atomic.h>
80 #include <linux/fsnotify_backend.h>
83 #define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */
85 struct srcu_struct fsnotify_mark_srcu;
86 static DEFINE_SPINLOCK(destroy_lock);
87 static LIST_HEAD(destroy_list);
89 static void fsnotify_mark_destroy_workfn(struct work_struct *work);
90 static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
92 void fsnotify_get_mark(struct fsnotify_mark *mark)
94 atomic_inc(&mark->refcnt);
97 void fsnotify_put_mark(struct fsnotify_mark *mark)
99 if (atomic_dec_and_test(&mark->refcnt)) {
101 fsnotify_put_group(mark->group);
102 mark->free_mark(mark);
106 /* Calculate mask of events for a list of marks */
107 u32 fsnotify_recalc_mask(struct hlist_head *head)
110 struct fsnotify_mark *mark;
112 hlist_for_each_entry(mark, head, obj_list)
113 new_mask |= mark->mask;
118 * Remove mark from inode / vfsmount list, group list, drop inode reference
121 * Must be called with group->mark_mutex held.
123 void fsnotify_detach_mark(struct fsnotify_mark *mark)
125 struct inode *inode = NULL;
126 struct fsnotify_group *group = mark->group;
128 BUG_ON(!mutex_is_locked(&group->mark_mutex));
130 spin_lock(&mark->lock);
132 /* something else already called this function on this mark */
133 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
134 spin_unlock(&mark->lock);
138 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
140 if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
142 fsnotify_destroy_inode_mark(mark);
143 } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT)
144 fsnotify_destroy_vfsmount_mark(mark);
148 * Note that we didn't update flags telling whether inode cares about
149 * what's happening with children. We update these flags from
150 * __fsnotify_parent() lazily when next event happens on one of our
154 list_del_init(&mark->g_list);
156 spin_unlock(&mark->lock);
158 if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED))
161 atomic_dec(&group->num_marks);
165 * Prepare mark for freeing and add it to the list of marks prepared for
166 * freeing. The actual freeing must happen after SRCU period ends and the
167 * caller is responsible for this.
169 * The function returns true if the mark was added to the list of marks for
170 * freeing. The function returns false if someone else has already called
171 * __fsnotify_free_mark() for the mark.
173 static bool __fsnotify_free_mark(struct fsnotify_mark *mark)
175 struct fsnotify_group *group = mark->group;
177 spin_lock(&mark->lock);
178 /* something else already called this function on this mark */
179 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
180 spin_unlock(&mark->lock);
183 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
184 spin_unlock(&mark->lock);
187 * Some groups like to know that marks are being freed. This is a
188 * callback to the group function to let it know that this mark
191 if (group->ops->freeing_mark)
192 group->ops->freeing_mark(mark, group);
194 spin_lock(&destroy_lock);
195 list_add(&mark->g_list, &destroy_list);
196 spin_unlock(&destroy_lock);
202 * Free fsnotify mark. The freeing is actually happening from a workqueue which
203 * first waits for srcu period end. Caller must have a reference to the mark
204 * or be protected by fsnotify_mark_srcu.
206 void fsnotify_free_mark(struct fsnotify_mark *mark)
208 if (__fsnotify_free_mark(mark)) {
209 queue_delayed_work(system_unbound_wq, &reaper_work,
210 FSNOTIFY_REAPER_DELAY);
214 void fsnotify_destroy_mark(struct fsnotify_mark *mark,
215 struct fsnotify_group *group)
217 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
218 fsnotify_detach_mark(mark);
219 mutex_unlock(&group->mark_mutex);
220 fsnotify_free_mark(mark);
223 void fsnotify_destroy_marks(struct hlist_head *head, spinlock_t *lock)
225 struct fsnotify_mark *mark;
229 * We have to be careful since we can race with e.g.
230 * fsnotify_clear_marks_by_group() and once we drop 'lock',
231 * mark can get removed from the obj_list and destroyed. But
232 * we are holding mark reference so mark cannot be freed and
233 * calling fsnotify_destroy_mark() more than once is fine.
236 if (hlist_empty(head)) {
240 mark = hlist_entry(head->first, struct fsnotify_mark, obj_list);
242 * We don't update i_fsnotify_mask / mnt_fsnotify_mask here
243 * since inode / mount is going away anyway. So just remove
244 * mark from the list.
246 hlist_del_init_rcu(&mark->obj_list);
247 fsnotify_get_mark(mark);
249 fsnotify_destroy_mark(mark, mark->group);
250 fsnotify_put_mark(mark);
254 void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
256 assert_spin_locked(&mark->lock);
260 if (mark->flags & FSNOTIFY_MARK_FLAG_INODE)
261 fsnotify_set_inode_mark_mask_locked(mark, mask);
264 void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
266 assert_spin_locked(&mark->lock);
268 mark->ignored_mask = mask;
272 * Sorting function for lists of fsnotify marks.
274 * Fanotify supports different notification classes (reflected as priority of
275 * notification group). Events shall be passed to notification groups in
276 * decreasing priority order. To achieve this marks in notification lists for
277 * inodes and vfsmounts are sorted so that priorities of corresponding groups
280 * Furthermore correct handling of the ignore mask requires processing inode
281 * and vfsmount marks of each group together. Using the group address as
282 * further sort criterion provides a unique sorting order and thus we can
283 * merge inode and vfsmount lists of marks in linear time and find groups
284 * present in both lists.
286 * A return value of 1 signifies that b has priority over a.
287 * A return value of 0 signifies that the two marks have to be handled together.
288 * A return value of -1 signifies that a has priority over b.
290 int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
298 if (a->priority < b->priority)
300 if (a->priority > b->priority)
307 /* Add mark into proper place in given list of marks */
308 int fsnotify_add_mark_list(struct hlist_head *head, struct fsnotify_mark *mark,
311 struct fsnotify_mark *lmark, *last = NULL;
314 /* is mark the first mark? */
315 if (hlist_empty(head)) {
316 hlist_add_head_rcu(&mark->obj_list, head);
320 /* should mark be in the middle of the current list? */
321 hlist_for_each_entry(lmark, head, obj_list) {
324 if ((lmark->group == mark->group) && !allow_dups)
327 cmp = fsnotify_compare_groups(lmark->group, mark->group);
329 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
334 BUG_ON(last == NULL);
335 /* mark should be the last entry. last is the current last entry */
336 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
341 * Attach an initialized mark to a given group and fs object.
342 * These marks may be used for the fsnotify backend to determine which
343 * event types should be delivered to which group.
345 int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
346 struct fsnotify_group *group, struct inode *inode,
347 struct vfsmount *mnt, int allow_dups)
351 BUG_ON(inode && mnt);
352 BUG_ON(!inode && !mnt);
353 BUG_ON(!mutex_is_locked(&group->mark_mutex));
361 spin_lock(&mark->lock);
362 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
364 fsnotify_get_group(group);
366 list_add(&mark->g_list, &group->marks_list);
367 atomic_inc(&group->num_marks);
368 fsnotify_get_mark(mark); /* for i_list and g_list */
371 ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups);
375 ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups);
382 /* this will pin the object if appropriate */
383 fsnotify_set_mark_mask_locked(mark, mark->mask);
384 spin_unlock(&mark->lock);
387 __fsnotify_update_child_dentry_flags(inode);
391 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
392 list_del_init(&mark->g_list);
393 fsnotify_put_group(group);
395 atomic_dec(&group->num_marks);
397 spin_unlock(&mark->lock);
399 spin_lock(&destroy_lock);
400 list_add(&mark->g_list, &destroy_list);
401 spin_unlock(&destroy_lock);
402 queue_delayed_work(system_unbound_wq, &reaper_work,
403 FSNOTIFY_REAPER_DELAY);
408 int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
409 struct inode *inode, struct vfsmount *mnt, int allow_dups)
412 mutex_lock(&group->mark_mutex);
413 ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
414 mutex_unlock(&group->mark_mutex);
419 * Given a list of marks, find the mark associated with given group. If found
420 * take a reference to that mark and return it, else return NULL.
422 struct fsnotify_mark *fsnotify_find_mark(struct hlist_head *head,
423 struct fsnotify_group *group)
425 struct fsnotify_mark *mark;
427 hlist_for_each_entry(mark, head, obj_list) {
428 if (mark->group == group) {
429 fsnotify_get_mark(mark);
437 * clear any marks in a group in which mark->flags & flags is true
439 void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
442 struct fsnotify_mark *lmark, *mark;
446 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
447 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
448 * to_free list so we have to use mark_mutex even when accessing that
449 * list. And freeing mark requires us to drop mark_mutex. So we can
450 * reliably free only the first mark in the list. That's why we first
451 * move marks to free to to_free list in one go and then free marks in
452 * to_free list one by one.
454 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
455 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
456 if (mark->flags & flags)
457 list_move(&mark->g_list, &to_free);
459 mutex_unlock(&group->mark_mutex);
462 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
463 if (list_empty(&to_free)) {
464 mutex_unlock(&group->mark_mutex);
467 mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
468 fsnotify_get_mark(mark);
469 fsnotify_detach_mark(mark);
470 mutex_unlock(&group->mark_mutex);
471 fsnotify_free_mark(mark);
472 fsnotify_put_mark(mark);
477 * Given a group, prepare for freeing all the marks associated with that group.
478 * The marks are attached to the list of marks prepared for destruction, the
479 * caller is responsible for freeing marks in that list after SRCU period has
482 void fsnotify_detach_group_marks(struct fsnotify_group *group)
484 struct fsnotify_mark *mark;
487 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
488 if (list_empty(&group->marks_list)) {
489 mutex_unlock(&group->mark_mutex);
492 mark = list_first_entry(&group->marks_list,
493 struct fsnotify_mark, g_list);
494 fsnotify_get_mark(mark);
495 fsnotify_detach_mark(mark);
496 mutex_unlock(&group->mark_mutex);
497 __fsnotify_free_mark(mark);
498 fsnotify_put_mark(mark);
503 * Nothing fancy, just initialize lists and locks and counters.
505 void fsnotify_init_mark(struct fsnotify_mark *mark,
506 void (*free_mark)(struct fsnotify_mark *mark))
508 memset(mark, 0, sizeof(*mark));
509 spin_lock_init(&mark->lock);
510 atomic_set(&mark->refcnt, 1);
511 mark->free_mark = free_mark;
515 * Destroy all marks in destroy_list, waits for SRCU period to finish before
516 * actually freeing marks.
518 void fsnotify_mark_destroy_list(void)
520 struct fsnotify_mark *mark, *next;
521 struct list_head private_destroy_list;
523 spin_lock(&destroy_lock);
524 /* exchange the list head */
525 list_replace_init(&destroy_list, &private_destroy_list);
526 spin_unlock(&destroy_lock);
528 synchronize_srcu(&fsnotify_mark_srcu);
530 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
531 list_del_init(&mark->g_list);
532 fsnotify_put_mark(mark);
536 static void fsnotify_mark_destroy_workfn(struct work_struct *work)
538 fsnotify_mark_destroy_list();