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 struct kmem_cache *fsnotify_mark_connector_cachep;
88 static DEFINE_SPINLOCK(destroy_lock);
89 static LIST_HEAD(destroy_list);
91 static void fsnotify_mark_destroy_workfn(struct work_struct *work);
92 static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
94 void fsnotify_get_mark(struct fsnotify_mark *mark)
96 atomic_inc(&mark->refcnt);
99 void fsnotify_put_mark(struct fsnotify_mark *mark)
101 if (atomic_dec_and_test(&mark->refcnt)) {
103 fsnotify_put_group(mark->group);
104 mark->free_mark(mark);
108 /* Calculate mask of events for a list of marks */
109 u32 fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
112 struct fsnotify_mark *mark;
117 hlist_for_each_entry(mark, &conn->list, 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->connector->flags & FSNOTIFY_OBJ_TYPE_INODE)
146 inode = fsnotify_destroy_inode_mark(mark);
147 else if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT)
148 fsnotify_destroy_vfsmount_mark(mark);
152 * Note that we didn't update flags telling whether inode cares about
153 * what's happening with children. We update these flags from
154 * __fsnotify_parent() lazily when next event happens on one of our
158 list_del_init(&mark->g_list);
160 spin_unlock(&mark->lock);
165 atomic_dec(&group->num_marks);
169 * Prepare mark for freeing and add it to the list of marks prepared for
170 * freeing. The actual freeing must happen after SRCU period ends and the
171 * caller is responsible for this.
173 * The function returns true if the mark was added to the list of marks for
174 * freeing. The function returns false if someone else has already called
175 * __fsnotify_free_mark() for the mark.
177 static bool __fsnotify_free_mark(struct fsnotify_mark *mark)
179 struct fsnotify_group *group = mark->group;
181 spin_lock(&mark->lock);
182 /* something else already called this function on this mark */
183 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
184 spin_unlock(&mark->lock);
187 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
188 spin_unlock(&mark->lock);
191 * Some groups like to know that marks are being freed. This is a
192 * callback to the group function to let it know that this mark
195 if (group->ops->freeing_mark)
196 group->ops->freeing_mark(mark, group);
198 spin_lock(&destroy_lock);
199 list_add(&mark->g_list, &destroy_list);
200 spin_unlock(&destroy_lock);
206 * Free fsnotify mark. The freeing is actually happening from a workqueue which
207 * first waits for srcu period end. Caller must have a reference to the mark
208 * or be protected by fsnotify_mark_srcu.
210 void fsnotify_free_mark(struct fsnotify_mark *mark)
212 if (__fsnotify_free_mark(mark)) {
213 queue_delayed_work(system_unbound_wq, &reaper_work,
214 FSNOTIFY_REAPER_DELAY);
218 void fsnotify_destroy_mark(struct fsnotify_mark *mark,
219 struct fsnotify_group *group)
221 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
222 fsnotify_detach_mark(mark);
223 mutex_unlock(&group->mark_mutex);
224 fsnotify_free_mark(mark);
227 void fsnotify_destroy_marks(struct fsnotify_mark_connector *conn,
230 struct fsnotify_mark *mark;
237 * We have to be careful since we can race with e.g.
238 * fsnotify_clear_marks_by_group() and once we drop 'lock',
239 * mark can get removed from the obj_list and destroyed. But
240 * we are holding mark reference so mark cannot be freed and
241 * calling fsnotify_destroy_mark() more than once is fine.
244 if (hlist_empty(&conn->list)) {
248 mark = hlist_entry(conn->list.first, struct fsnotify_mark,
251 * We don't update i_fsnotify_mask / mnt_fsnotify_mask here
252 * since inode / mount is going away anyway. So just remove
253 * mark from the list.
255 hlist_del_init_rcu(&mark->obj_list);
256 fsnotify_get_mark(mark);
258 fsnotify_destroy_mark(mark, mark->group);
259 fsnotify_put_mark(mark);
263 void fsnotify_connector_free(struct fsnotify_mark_connector **connp)
266 kmem_cache_free(fsnotify_mark_connector_cachep, *connp);
271 void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
273 assert_spin_locked(&mark->lock);
278 void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
280 assert_spin_locked(&mark->lock);
282 mark->ignored_mask = mask;
286 * Sorting function for lists of fsnotify marks.
288 * Fanotify supports different notification classes (reflected as priority of
289 * notification group). Events shall be passed to notification groups in
290 * decreasing priority order. To achieve this marks in notification lists for
291 * inodes and vfsmounts are sorted so that priorities of corresponding groups
294 * Furthermore correct handling of the ignore mask requires processing inode
295 * and vfsmount marks of each group together. Using the group address as
296 * further sort criterion provides a unique sorting order and thus we can
297 * merge inode and vfsmount lists of marks in linear time and find groups
298 * present in both lists.
300 * A return value of 1 signifies that b has priority over a.
301 * A return value of 0 signifies that the two marks have to be handled together.
302 * A return value of -1 signifies that a has priority over b.
304 int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
312 if (a->priority < b->priority)
314 if (a->priority > b->priority)
321 static int fsnotify_attach_connector_to_object(
322 struct fsnotify_mark_connector **connp,
324 struct vfsmount *mnt)
326 struct fsnotify_mark_connector *conn;
328 conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_ATOMIC);
331 INIT_HLIST_HEAD(&conn->list);
333 conn->flags = FSNOTIFY_OBJ_TYPE_INODE;
336 conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
340 * Make sure 'conn' initialization is visible. Matches
341 * lockless_dereference() in fsnotify().
350 * Add mark into proper place in given list of marks. These marks may be used
351 * for the fsnotify backend to determine which event types should be delivered
352 * to which group and for which inodes. These marks are ordered according to
353 * priority, highest number first, and then by the group's location in memory.
355 int fsnotify_add_mark_list(struct fsnotify_mark_connector **connp,
356 struct fsnotify_mark *mark, struct inode *inode,
357 struct vfsmount *mnt, int allow_dups)
359 struct fsnotify_mark *lmark, *last = NULL;
360 struct fsnotify_mark_connector *conn;
365 err = fsnotify_attach_connector_to_object(connp, inode, mnt);
371 /* is mark the first mark? */
372 if (hlist_empty(&conn->list)) {
373 hlist_add_head_rcu(&mark->obj_list, &conn->list);
379 /* should mark be in the middle of the current list? */
380 hlist_for_each_entry(lmark, &conn->list, obj_list) {
383 if ((lmark->group == mark->group) && !allow_dups)
386 cmp = fsnotify_compare_groups(lmark->group, mark->group);
388 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
393 BUG_ON(last == NULL);
394 /* mark should be the last entry. last is the current last entry */
395 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
397 mark->connector = conn;
402 * Attach an initialized mark to a given group and fs object.
403 * These marks may be used for the fsnotify backend to determine which
404 * event types should be delivered to which group.
406 int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
407 struct fsnotify_group *group, struct inode *inode,
408 struct vfsmount *mnt, int allow_dups)
412 BUG_ON(inode && mnt);
413 BUG_ON(!inode && !mnt);
414 BUG_ON(!mutex_is_locked(&group->mark_mutex));
422 spin_lock(&mark->lock);
423 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
425 fsnotify_get_group(group);
427 list_add(&mark->g_list, &group->marks_list);
428 atomic_inc(&group->num_marks);
429 fsnotify_get_mark(mark); /* for i_list and g_list */
432 ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups);
436 ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups);
442 spin_unlock(&mark->lock);
445 __fsnotify_update_child_dentry_flags(inode);
449 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
450 list_del_init(&mark->g_list);
451 fsnotify_put_group(group);
453 atomic_dec(&group->num_marks);
455 spin_unlock(&mark->lock);
457 spin_lock(&destroy_lock);
458 list_add(&mark->g_list, &destroy_list);
459 spin_unlock(&destroy_lock);
460 queue_delayed_work(system_unbound_wq, &reaper_work,
461 FSNOTIFY_REAPER_DELAY);
466 int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
467 struct inode *inode, struct vfsmount *mnt, int allow_dups)
470 mutex_lock(&group->mark_mutex);
471 ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
472 mutex_unlock(&group->mark_mutex);
477 * Given a list of marks, find the mark associated with given group. If found
478 * take a reference to that mark and return it, else return NULL.
480 struct fsnotify_mark *fsnotify_find_mark(struct fsnotify_mark_connector *conn,
481 struct fsnotify_group *group)
483 struct fsnotify_mark *mark;
488 hlist_for_each_entry(mark, &conn->list, obj_list) {
489 if (mark->group == group) {
490 fsnotify_get_mark(mark);
498 * clear any marks in a group in which mark->flags & flags is true
500 void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
503 struct fsnotify_mark *lmark, *mark;
507 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
508 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
509 * to_free list so we have to use mark_mutex even when accessing that
510 * list. And freeing mark requires us to drop mark_mutex. So we can
511 * reliably free only the first mark in the list. That's why we first
512 * move marks to free to to_free list in one go and then free marks in
513 * to_free list one by one.
515 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
516 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
517 if (mark->connector->flags & flags)
518 list_move(&mark->g_list, &to_free);
520 mutex_unlock(&group->mark_mutex);
523 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
524 if (list_empty(&to_free)) {
525 mutex_unlock(&group->mark_mutex);
528 mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
529 fsnotify_get_mark(mark);
530 fsnotify_detach_mark(mark);
531 mutex_unlock(&group->mark_mutex);
532 fsnotify_free_mark(mark);
533 fsnotify_put_mark(mark);
538 * Given a group, prepare for freeing all the marks associated with that group.
539 * The marks are attached to the list of marks prepared for destruction, the
540 * caller is responsible for freeing marks in that list after SRCU period has
543 void fsnotify_detach_group_marks(struct fsnotify_group *group)
545 struct fsnotify_mark *mark;
548 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
549 if (list_empty(&group->marks_list)) {
550 mutex_unlock(&group->mark_mutex);
553 mark = list_first_entry(&group->marks_list,
554 struct fsnotify_mark, g_list);
555 fsnotify_get_mark(mark);
556 fsnotify_detach_mark(mark);
557 mutex_unlock(&group->mark_mutex);
558 __fsnotify_free_mark(mark);
559 fsnotify_put_mark(mark);
564 * Nothing fancy, just initialize lists and locks and counters.
566 void fsnotify_init_mark(struct fsnotify_mark *mark,
567 void (*free_mark)(struct fsnotify_mark *mark))
569 memset(mark, 0, sizeof(*mark));
570 spin_lock_init(&mark->lock);
571 atomic_set(&mark->refcnt, 1);
572 mark->free_mark = free_mark;
576 * Destroy all marks in destroy_list, waits for SRCU period to finish before
577 * actually freeing marks.
579 void fsnotify_mark_destroy_list(void)
581 struct fsnotify_mark *mark, *next;
582 struct list_head private_destroy_list;
584 spin_lock(&destroy_lock);
585 /* exchange the list head */
586 list_replace_init(&destroy_list, &private_destroy_list);
587 spin_unlock(&destroy_lock);
589 synchronize_srcu(&fsnotify_mark_srcu);
591 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
592 list_del_init(&mark->g_list);
593 fsnotify_put_mark(mark);
597 static void fsnotify_mark_destroy_workfn(struct work_struct *work)
599 fsnotify_mark_destroy_list();