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:
36 * mark->connector->lock
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 * mark->connector->lock protects the list of marks anchored inside an
48 * inode / vfsmount and each mark is hooked via the i_list.
50 * A list of notification marks relating to inode / mnt is contained in
51 * fsnotify_mark_connector. That structure is alive as long as there are any
52 * marks in the list and is also protected by fsnotify_mark_srcu.
55 * Inode marks survive between when they are added to an inode and when their
56 * refcnt==0. Marks are also protected by fsnotify_mark_srcu.
58 * The inode mark can be cleared for a number of different reasons including:
59 * - The inode is unlinked for the last time. (fsnotify_inode_remove)
60 * - The inode is being evicted from cache. (fsnotify_inode_delete)
61 * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes)
62 * - Something explicitly requests that it be removed. (fsnotify_destroy_mark)
63 * - The fsnotify_group associated with the mark is going away and all such marks
64 * need to be cleaned up. (fsnotify_clear_marks_by_group)
66 * This has the very interesting property of being able to run concurrently with
67 * any (or all) other directions.
71 #include <linux/init.h>
72 #include <linux/kernel.h>
73 #include <linux/kthread.h>
74 #include <linux/module.h>
75 #include <linux/mutex.h>
76 #include <linux/slab.h>
77 #include <linux/spinlock.h>
78 #include <linux/srcu.h>
80 #include <linux/atomic.h>
82 #include <linux/fsnotify_backend.h>
85 #define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */
87 struct srcu_struct fsnotify_mark_srcu;
88 struct kmem_cache *fsnotify_mark_connector_cachep;
90 static DEFINE_SPINLOCK(destroy_lock);
91 static LIST_HEAD(destroy_list);
92 static struct fsnotify_mark_connector *connector_destroy_list;
94 static void fsnotify_mark_destroy_workfn(struct work_struct *work);
95 static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
97 static void fsnotify_connector_destroy_workfn(struct work_struct *work);
98 static DECLARE_WORK(connector_reaper_work, fsnotify_connector_destroy_workfn);
100 void fsnotify_get_mark(struct fsnotify_mark *mark)
102 atomic_inc(&mark->refcnt);
105 void fsnotify_put_mark(struct fsnotify_mark *mark)
107 if (atomic_dec_and_test(&mark->refcnt)) {
109 fsnotify_put_group(mark->group);
110 mark->free_mark(mark);
114 static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
117 struct fsnotify_mark *mark;
119 assert_spin_locked(&conn->lock);
120 hlist_for_each_entry(mark, &conn->list, obj_list)
121 new_mask |= mark->mask;
123 if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
124 conn->inode->i_fsnotify_mask = new_mask;
125 else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT)
126 real_mount(conn->mnt)->mnt_fsnotify_mask = new_mask;
130 * Calculate mask of events for a list of marks. The caller must make sure
131 * connector cannot disappear under us (usually by holding a mark->lock or
132 * mark->group->mark_mutex for a mark on this list).
134 void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
139 spin_lock(&conn->lock);
140 __fsnotify_recalc_mask(conn);
141 spin_unlock(&conn->lock);
142 if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
143 __fsnotify_update_child_dentry_flags(conn->inode);
146 /* Free all connectors queued for freeing once SRCU period ends */
147 static void fsnotify_connector_destroy_workfn(struct work_struct *work)
149 struct fsnotify_mark_connector *conn, *free;
151 spin_lock(&destroy_lock);
152 conn = connector_destroy_list;
153 connector_destroy_list = NULL;
154 spin_unlock(&destroy_lock);
156 synchronize_srcu(&fsnotify_mark_srcu);
159 conn = conn->destroy_next;
160 kmem_cache_free(fsnotify_mark_connector_cachep, free);
165 static struct inode *fsnotify_detach_connector_from_object(
166 struct fsnotify_mark_connector *conn)
168 struct inode *inode = NULL;
170 if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) {
172 rcu_assign_pointer(inode->i_fsnotify_marks, NULL);
173 inode->i_fsnotify_mask = 0;
175 conn->flags &= ~FSNOTIFY_OBJ_TYPE_INODE;
176 } else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
177 rcu_assign_pointer(real_mount(conn->mnt)->mnt_fsnotify_marks,
179 real_mount(conn->mnt)->mnt_fsnotify_mask = 0;
181 conn->flags &= ~FSNOTIFY_OBJ_TYPE_VFSMOUNT;
187 static struct inode *fsnotify_detach_from_object(struct fsnotify_mark *mark)
189 struct fsnotify_mark_connector *conn;
190 struct inode *inode = NULL;
191 bool free_conn = false;
193 conn = mark->connector;
194 spin_lock(&conn->lock);
195 hlist_del_init_rcu(&mark->obj_list);
196 if (hlist_empty(&conn->list)) {
197 inode = fsnotify_detach_connector_from_object(conn);
200 __fsnotify_recalc_mask(conn);
202 mark->connector = NULL;
203 spin_unlock(&conn->lock);
206 spin_lock(&destroy_lock);
207 conn->destroy_next = connector_destroy_list;
208 connector_destroy_list = conn;
209 spin_unlock(&destroy_lock);
210 queue_work(system_unbound_wq, &connector_reaper_work);
217 * Remove mark from inode / vfsmount list, group list, drop inode reference
220 * Must be called with group->mark_mutex held.
222 void fsnotify_detach_mark(struct fsnotify_mark *mark)
224 struct inode *inode = NULL;
225 struct fsnotify_group *group = mark->group;
227 BUG_ON(!mutex_is_locked(&group->mark_mutex));
229 spin_lock(&mark->lock);
231 /* something else already called this function on this mark */
232 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
233 spin_unlock(&mark->lock);
237 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
239 inode = fsnotify_detach_from_object(mark);
242 * Note that we didn't update flags telling whether inode cares about
243 * what's happening with children. We update these flags from
244 * __fsnotify_parent() lazily when next event happens on one of our
248 list_del_init(&mark->g_list);
250 spin_unlock(&mark->lock);
255 atomic_dec(&group->num_marks);
259 * Prepare mark for freeing and add it to the list of marks prepared for
260 * freeing. The actual freeing must happen after SRCU period ends and the
261 * caller is responsible for this.
263 * The function returns true if the mark was added to the list of marks for
264 * freeing. The function returns false if someone else has already called
265 * __fsnotify_free_mark() for the mark.
267 static bool __fsnotify_free_mark(struct fsnotify_mark *mark)
269 struct fsnotify_group *group = mark->group;
271 spin_lock(&mark->lock);
272 /* something else already called this function on this mark */
273 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
274 spin_unlock(&mark->lock);
277 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
278 spin_unlock(&mark->lock);
281 * Some groups like to know that marks are being freed. This is a
282 * callback to the group function to let it know that this mark
285 if (group->ops->freeing_mark)
286 group->ops->freeing_mark(mark, group);
288 spin_lock(&destroy_lock);
289 list_add(&mark->g_list, &destroy_list);
290 spin_unlock(&destroy_lock);
296 * Free fsnotify mark. The freeing is actually happening from a workqueue which
297 * first waits for srcu period end. Caller must have a reference to the mark
298 * or be protected by fsnotify_mark_srcu.
300 void fsnotify_free_mark(struct fsnotify_mark *mark)
302 if (__fsnotify_free_mark(mark)) {
303 queue_delayed_work(system_unbound_wq, &reaper_work,
304 FSNOTIFY_REAPER_DELAY);
308 void fsnotify_destroy_mark(struct fsnotify_mark *mark,
309 struct fsnotify_group *group)
311 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
312 fsnotify_detach_mark(mark);
313 mutex_unlock(&group->mark_mutex);
314 fsnotify_free_mark(mark);
317 void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
319 assert_spin_locked(&mark->lock);
324 void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
326 assert_spin_locked(&mark->lock);
328 mark->ignored_mask = mask;
332 * Sorting function for lists of fsnotify marks.
334 * Fanotify supports different notification classes (reflected as priority of
335 * notification group). Events shall be passed to notification groups in
336 * decreasing priority order. To achieve this marks in notification lists for
337 * inodes and vfsmounts are sorted so that priorities of corresponding groups
340 * Furthermore correct handling of the ignore mask requires processing inode
341 * and vfsmount marks of each group together. Using the group address as
342 * further sort criterion provides a unique sorting order and thus we can
343 * merge inode and vfsmount lists of marks in linear time and find groups
344 * present in both lists.
346 * A return value of 1 signifies that b has priority over a.
347 * A return value of 0 signifies that the two marks have to be handled together.
348 * A return value of -1 signifies that a has priority over b.
350 int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
358 if (a->priority < b->priority)
360 if (a->priority > b->priority)
367 static int fsnotify_attach_connector_to_object(
368 struct fsnotify_mark_connector __rcu **connp,
370 struct vfsmount *mnt)
372 struct fsnotify_mark_connector *conn;
374 conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
377 spin_lock_init(&conn->lock);
378 INIT_HLIST_HEAD(&conn->list);
380 conn->flags = FSNOTIFY_OBJ_TYPE_INODE;
381 conn->inode = igrab(inode);
383 conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
387 * cmpxchg() provides the barrier so that readers of *connp can see
388 * only initialized structure
390 if (cmpxchg(connp, NULL, conn)) {
391 /* Someone else created list structure for us */
394 kmem_cache_free(fsnotify_mark_connector_cachep, conn);
401 * Get mark connector, make sure it is alive and return with its lock held.
402 * This is for users that get connector pointer from inode or mount. Users that
403 * hold reference to a mark on the list may directly lock connector->lock as
404 * they are sure list cannot go away under them.
406 static struct fsnotify_mark_connector *fsnotify_grab_connector(
407 struct fsnotify_mark_connector __rcu **connp)
409 struct fsnotify_mark_connector *conn;
412 idx = srcu_read_lock(&fsnotify_mark_srcu);
413 conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
416 spin_lock(&conn->lock);
417 if (!(conn->flags & (FSNOTIFY_OBJ_TYPE_INODE |
418 FSNOTIFY_OBJ_TYPE_VFSMOUNT))) {
419 spin_unlock(&conn->lock);
420 srcu_read_unlock(&fsnotify_mark_srcu, idx);
424 srcu_read_unlock(&fsnotify_mark_srcu, idx);
429 * Add mark into proper place in given list of marks. These marks may be used
430 * for the fsnotify backend to determine which event types should be delivered
431 * to which group and for which inodes. These marks are ordered according to
432 * priority, highest number first, and then by the group's location in memory.
434 static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
435 struct inode *inode, struct vfsmount *mnt,
438 struct fsnotify_mark *lmark, *last = NULL;
439 struct fsnotify_mark_connector *conn;
440 struct fsnotify_mark_connector __rcu **connp;
444 if (WARN_ON(!inode && !mnt))
447 connp = &inode->i_fsnotify_marks;
449 connp = &real_mount(mnt)->mnt_fsnotify_marks;
451 spin_lock(&mark->lock);
452 conn = fsnotify_grab_connector(connp);
454 spin_unlock(&mark->lock);
455 err = fsnotify_attach_connector_to_object(connp, inode, mnt);
461 /* is mark the first mark? */
462 if (hlist_empty(&conn->list)) {
463 hlist_add_head_rcu(&mark->obj_list, &conn->list);
467 /* should mark be in the middle of the current list? */
468 hlist_for_each_entry(lmark, &conn->list, obj_list) {
471 if ((lmark->group == mark->group) && !allow_dups) {
476 cmp = fsnotify_compare_groups(lmark->group, mark->group);
478 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
483 BUG_ON(last == NULL);
484 /* mark should be the last entry. last is the current last entry */
485 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
487 mark->connector = conn;
489 spin_unlock(&conn->lock);
490 spin_unlock(&mark->lock);
495 * Attach an initialized mark to a given group and fs object.
496 * These marks may be used for the fsnotify backend to determine which
497 * event types should be delivered to which group.
499 int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
500 struct fsnotify_group *group, struct inode *inode,
501 struct vfsmount *mnt, int allow_dups)
505 BUG_ON(inode && mnt);
506 BUG_ON(!inode && !mnt);
507 BUG_ON(!mutex_is_locked(&group->mark_mutex));
513 * mark->connector->lock
515 spin_lock(&mark->lock);
516 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
518 fsnotify_get_group(group);
520 list_add(&mark->g_list, &group->marks_list);
521 atomic_inc(&group->num_marks);
522 fsnotify_get_mark(mark); /* for i_list and g_list */
523 spin_unlock(&mark->lock);
525 ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups);
530 fsnotify_recalc_mask(mark->connector);
534 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
535 list_del_init(&mark->g_list);
536 fsnotify_put_group(group);
538 atomic_dec(&group->num_marks);
540 spin_unlock(&mark->lock);
542 spin_lock(&destroy_lock);
543 list_add(&mark->g_list, &destroy_list);
544 spin_unlock(&destroy_lock);
545 queue_delayed_work(system_unbound_wq, &reaper_work,
546 FSNOTIFY_REAPER_DELAY);
551 int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
552 struct inode *inode, struct vfsmount *mnt, int allow_dups)
555 mutex_lock(&group->mark_mutex);
556 ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
557 mutex_unlock(&group->mark_mutex);
562 * Given a list of marks, find the mark associated with given group. If found
563 * take a reference to that mark and return it, else return NULL.
565 struct fsnotify_mark *fsnotify_find_mark(
566 struct fsnotify_mark_connector __rcu **connp,
567 struct fsnotify_group *group)
569 struct fsnotify_mark_connector *conn;
570 struct fsnotify_mark *mark;
572 conn = fsnotify_grab_connector(connp);
576 hlist_for_each_entry(mark, &conn->list, obj_list) {
577 if (mark->group == group) {
578 fsnotify_get_mark(mark);
579 spin_unlock(&conn->lock);
583 spin_unlock(&conn->lock);
588 * clear any marks in a group in which mark->flags & flags is true
590 void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
593 struct fsnotify_mark *lmark, *mark;
597 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
598 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
599 * to_free list so we have to use mark_mutex even when accessing that
600 * list. And freeing mark requires us to drop mark_mutex. So we can
601 * reliably free only the first mark in the list. That's why we first
602 * move marks to free to to_free list in one go and then free marks in
603 * to_free list one by one.
605 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
606 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
607 if (mark->connector->flags & flags)
608 list_move(&mark->g_list, &to_free);
610 mutex_unlock(&group->mark_mutex);
613 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
614 if (list_empty(&to_free)) {
615 mutex_unlock(&group->mark_mutex);
618 mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
619 fsnotify_get_mark(mark);
620 fsnotify_detach_mark(mark);
621 mutex_unlock(&group->mark_mutex);
622 fsnotify_free_mark(mark);
623 fsnotify_put_mark(mark);
628 * Given a group, prepare for freeing all the marks associated with that group.
629 * The marks are attached to the list of marks prepared for destruction, the
630 * caller is responsible for freeing marks in that list after SRCU period has
633 void fsnotify_detach_group_marks(struct fsnotify_group *group)
635 struct fsnotify_mark *mark;
638 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
639 if (list_empty(&group->marks_list)) {
640 mutex_unlock(&group->mark_mutex);
643 mark = list_first_entry(&group->marks_list,
644 struct fsnotify_mark, g_list);
645 fsnotify_get_mark(mark);
646 fsnotify_detach_mark(mark);
647 mutex_unlock(&group->mark_mutex);
648 __fsnotify_free_mark(mark);
649 fsnotify_put_mark(mark);
653 /* Destroy all marks attached to inode / vfsmount */
654 void fsnotify_destroy_marks(struct fsnotify_mark_connector __rcu **connp)
656 struct fsnotify_mark_connector *conn;
657 struct fsnotify_mark *mark;
659 while ((conn = fsnotify_grab_connector(connp))) {
661 * We have to be careful since we can race with e.g.
662 * fsnotify_clear_marks_by_group() and once we drop the list
663 * lock, mark can get removed from the obj_list and destroyed.
664 * But we are holding mark reference so mark cannot be freed
665 * and calling fsnotify_destroy_mark() more than once is fine.
667 mark = hlist_entry(conn->list.first, struct fsnotify_mark,
669 fsnotify_get_mark(mark);
670 spin_unlock(&conn->lock);
671 fsnotify_destroy_mark(mark, mark->group);
672 fsnotify_put_mark(mark);
677 * Nothing fancy, just initialize lists and locks and counters.
679 void fsnotify_init_mark(struct fsnotify_mark *mark,
680 void (*free_mark)(struct fsnotify_mark *mark))
682 memset(mark, 0, sizeof(*mark));
683 spin_lock_init(&mark->lock);
684 atomic_set(&mark->refcnt, 1);
685 mark->free_mark = free_mark;
689 * Destroy all marks in destroy_list, waits for SRCU period to finish before
690 * actually freeing marks.
692 void fsnotify_mark_destroy_list(void)
694 struct fsnotify_mark *mark, *next;
695 struct list_head private_destroy_list;
697 spin_lock(&destroy_lock);
698 /* exchange the list head */
699 list_replace_init(&destroy_list, &private_destroy_list);
700 spin_unlock(&destroy_lock);
702 synchronize_srcu(&fsnotify_mark_srcu);
704 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
705 list_del_init(&mark->g_list);
706 fsnotify_put_mark(mark);
710 static void fsnotify_mark_destroy_workfn(struct work_struct *work)
712 fsnotify_mark_destroy_list();