15444e298SEric Paris /* 25444e298SEric Paris * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> 35444e298SEric Paris * 45444e298SEric Paris * This program is free software; you can redistribute it and/or modify 55444e298SEric Paris * it under the terms of the GNU General Public License as published by 65444e298SEric Paris * the Free Software Foundation; either version 2, or (at your option) 75444e298SEric Paris * any later version. 85444e298SEric Paris * 95444e298SEric Paris * This program is distributed in the hope that it will be useful, 105444e298SEric Paris * but WITHOUT ANY WARRANTY; without even the implied warranty of 115444e298SEric Paris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 125444e298SEric Paris * GNU General Public License for more details. 135444e298SEric Paris * 145444e298SEric Paris * You should have received a copy of the GNU General Public License 155444e298SEric Paris * along with this program; see the file COPYING. If not, write to 165444e298SEric Paris * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 175444e298SEric Paris */ 185444e298SEric Paris 195444e298SEric Paris /* 205444e298SEric Paris * fsnotify inode mark locking/lifetime/and refcnting 215444e298SEric Paris * 225444e298SEric Paris * REFCNT: 239756b918SLino Sanfilippo * The group->recnt and mark->refcnt tell how many "things" in the kernel 249756b918SLino Sanfilippo * currently are referencing the objects. Both kind of objects typically will 259756b918SLino Sanfilippo * live inside the kernel with a refcnt of 2, one for its creation and one for 269756b918SLino Sanfilippo * the reference a group and a mark hold to each other. 279756b918SLino Sanfilippo * If you are holding the appropriate locks, you can take a reference and the 289756b918SLino Sanfilippo * object itself is guaranteed to survive until the reference is dropped. 295444e298SEric Paris * 305444e298SEric Paris * LOCKING: 319756b918SLino Sanfilippo * There are 3 locks involved with fsnotify inode marks and they MUST be taken 329756b918SLino Sanfilippo * in order as follows: 335444e298SEric Paris * 349756b918SLino Sanfilippo * group->mark_mutex 355444e298SEric Paris * mark->lock 365444e298SEric Paris * inode->i_lock 375444e298SEric Paris * 389756b918SLino Sanfilippo * group->mark_mutex protects the marks_list anchored inside a given group and 399756b918SLino Sanfilippo * each mark is hooked via the g_list. It also protects the groups private 409756b918SLino Sanfilippo * data (i.e group limits). 419756b918SLino Sanfilippo 429756b918SLino Sanfilippo * mark->lock protects the marks attributes like its masks and flags. 439756b918SLino Sanfilippo * Furthermore it protects the access to a reference of the group that the mark 449756b918SLino Sanfilippo * is assigned to as well as the access to a reference of the inode/vfsmount 459756b918SLino Sanfilippo * that is being watched by the mark. 465444e298SEric Paris * 475444e298SEric Paris * inode->i_lock protects the i_fsnotify_marks list anchored inside a 485444e298SEric Paris * given inode and each mark is hooked via the i_list. (and sorta the 495444e298SEric Paris * free_i_list) 505444e298SEric Paris * 515444e298SEric Paris * 525444e298SEric Paris * LIFETIME: 535444e298SEric Paris * Inode marks survive between when they are added to an inode and when their 545444e298SEric Paris * refcnt==0. 555444e298SEric Paris * 565444e298SEric Paris * The inode mark can be cleared for a number of different reasons including: 575444e298SEric Paris * - The inode is unlinked for the last time. (fsnotify_inode_remove) 585444e298SEric Paris * - The inode is being evicted from cache. (fsnotify_inode_delete) 595444e298SEric Paris * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes) 605444e298SEric Paris * - Something explicitly requests that it be removed. (fsnotify_destroy_mark) 615444e298SEric Paris * - The fsnotify_group associated with the mark is going away and all such marks 625444e298SEric Paris * need to be cleaned up. (fsnotify_clear_marks_by_group) 635444e298SEric Paris * 645444e298SEric Paris * Worst case we are given an inode and need to clean up all the marks on that 655444e298SEric Paris * inode. We take i_lock and walk the i_fsnotify_marks safely. For each 665444e298SEric Paris * mark on the list we take a reference (so the mark can't disappear under us). 675444e298SEric Paris * We remove that mark form the inode's list of marks and we add this mark to a 689756b918SLino Sanfilippo * private list anchored on the stack using i_free_list; we walk i_free_list 699756b918SLino Sanfilippo * and before we destroy the mark we make sure that we dont race with a 709756b918SLino Sanfilippo * concurrent destroy_group by getting a ref to the marks group and taking the 719756b918SLino Sanfilippo * groups mutex. 729756b918SLino Sanfilippo 735444e298SEric Paris * Very similarly for freeing by group, except we use free_g_list. 745444e298SEric Paris * 755444e298SEric Paris * This has the very interesting property of being able to run concurrently with 765444e298SEric Paris * any (or all) other directions. 775444e298SEric Paris */ 785444e298SEric Paris 795444e298SEric Paris #include <linux/fs.h> 805444e298SEric Paris #include <linux/init.h> 815444e298SEric Paris #include <linux/kernel.h> 8275c1be48SEric Paris #include <linux/kthread.h> 835444e298SEric Paris #include <linux/module.h> 845444e298SEric Paris #include <linux/mutex.h> 855444e298SEric Paris #include <linux/slab.h> 865444e298SEric Paris #include <linux/spinlock.h> 8775c1be48SEric Paris #include <linux/srcu.h> 885444e298SEric Paris 8960063497SArun Sharma #include <linux/atomic.h> 905444e298SEric Paris 915444e298SEric Paris #include <linux/fsnotify_backend.h> 925444e298SEric Paris #include "fsnotify.h" 935444e298SEric Paris 940918f1c3SJeff Layton #define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */ 950918f1c3SJeff Layton 9675c1be48SEric Paris struct srcu_struct fsnotify_mark_srcu; 9713d34ac6SJeff Layton static DEFINE_SPINLOCK(destroy_lock); 9813d34ac6SJeff Layton static LIST_HEAD(destroy_list); 990918f1c3SJeff Layton 100*35e48176SJan Kara static void fsnotify_mark_destroy_workfn(struct work_struct *work); 101*35e48176SJan Kara static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn); 10275c1be48SEric Paris 1035444e298SEric Paris void fsnotify_get_mark(struct fsnotify_mark *mark) 1045444e298SEric Paris { 1055444e298SEric Paris atomic_inc(&mark->refcnt); 1065444e298SEric Paris } 1075444e298SEric Paris 1085444e298SEric Paris void fsnotify_put_mark(struct fsnotify_mark *mark) 1095444e298SEric Paris { 11023e964c2SLino Sanfilippo if (atomic_dec_and_test(&mark->refcnt)) { 11123e964c2SLino Sanfilippo if (mark->group) 11223e964c2SLino Sanfilippo fsnotify_put_group(mark->group); 1135444e298SEric Paris mark->free_mark(mark); 1145444e298SEric Paris } 11523e964c2SLino Sanfilippo } 1165444e298SEric Paris 1170809ab69SJan Kara /* Calculate mask of events for a list of marks */ 1180809ab69SJan Kara u32 fsnotify_recalc_mask(struct hlist_head *head) 1190809ab69SJan Kara { 1200809ab69SJan Kara u32 new_mask = 0; 1210809ab69SJan Kara struct fsnotify_mark *mark; 1220809ab69SJan Kara 1230809ab69SJan Kara hlist_for_each_entry(mark, head, obj_list) 1240809ab69SJan Kara new_mask |= mark->mask; 1250809ab69SJan Kara return new_mask; 1260809ab69SJan Kara } 1270809ab69SJan Kara 1285444e298SEric Paris /* 1294712e722SJan Kara * Remove mark from inode / vfsmount list, group list, drop inode reference 1304712e722SJan Kara * if we got one. 1314712e722SJan Kara * 1324712e722SJan Kara * Must be called with group->mark_mutex held. 1335444e298SEric Paris */ 1344712e722SJan Kara void fsnotify_detach_mark(struct fsnotify_mark *mark) 1355444e298SEric Paris { 1360d48b7f0SEric Paris struct inode *inode = NULL; 1374712e722SJan Kara struct fsnotify_group *group = mark->group; 1385444e298SEric Paris 139d5a335b8SLino Sanfilippo BUG_ON(!mutex_is_locked(&group->mark_mutex)); 140d5a335b8SLino Sanfilippo 141104d06f0SLino Sanfilippo spin_lock(&mark->lock); 1425444e298SEric Paris 143700307a2SEric Paris /* something else already called this function on this mark */ 1444712e722SJan Kara if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { 1455444e298SEric Paris spin_unlock(&mark->lock); 146e2a29943SLino Sanfilippo return; 1475444e298SEric Paris } 1485444e298SEric Paris 1494712e722SJan Kara mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED; 150700307a2SEric Paris 1510d48b7f0SEric Paris if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) { 1520809ab69SJan Kara inode = mark->inode; 153b31d397eSEric Paris fsnotify_destroy_inode_mark(mark); 1540d48b7f0SEric Paris } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT) 1550d48b7f0SEric Paris fsnotify_destroy_vfsmount_mark(mark); 1565444e298SEric Paris else 1575444e298SEric Paris BUG(); 1584712e722SJan Kara /* 1594712e722SJan Kara * Note that we didn't update flags telling whether inode cares about 1604712e722SJan Kara * what's happening with children. We update these flags from 1614712e722SJan Kara * __fsnotify_parent() lazily when next event happens on one of our 1624712e722SJan Kara * children. 1634712e722SJan Kara */ 1645444e298SEric Paris 1655444e298SEric Paris list_del_init(&mark->g_list); 166d725e66cSLinus Torvalds 1675444e298SEric Paris spin_unlock(&mark->lock); 168d5a335b8SLino Sanfilippo 1696960b0d9SLino Sanfilippo if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) 1706960b0d9SLino Sanfilippo iput(inode); 1714712e722SJan Kara 1724712e722SJan Kara atomic_dec(&group->num_marks); 1734712e722SJan Kara } 1744712e722SJan Kara 1754712e722SJan Kara /* 176*35e48176SJan Kara * Prepare mark for freeing and add it to the list of marks prepared for 177*35e48176SJan Kara * freeing. The actual freeing must happen after SRCU period ends and the 178*35e48176SJan Kara * caller is responsible for this. 179*35e48176SJan Kara * 180*35e48176SJan Kara * The function returns true if the mark was added to the list of marks for 181*35e48176SJan Kara * freeing. The function returns false if someone else has already called 182*35e48176SJan Kara * __fsnotify_free_mark() for the mark. 1834712e722SJan Kara */ 184*35e48176SJan Kara static bool __fsnotify_free_mark(struct fsnotify_mark *mark) 1854712e722SJan Kara { 1864712e722SJan Kara struct fsnotify_group *group = mark->group; 1874712e722SJan Kara 1884712e722SJan Kara spin_lock(&mark->lock); 1894712e722SJan Kara /* something else already called this function on this mark */ 1904712e722SJan Kara if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { 1914712e722SJan Kara spin_unlock(&mark->lock); 192*35e48176SJan Kara return false; 1934712e722SJan Kara } 1944712e722SJan Kara mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 1954712e722SJan Kara spin_unlock(&mark->lock); 1965444e298SEric Paris 197d725e66cSLinus Torvalds /* 198d725e66cSLinus Torvalds * Some groups like to know that marks are being freed. This is a 199d725e66cSLinus Torvalds * callback to the group function to let it know that this mark 200d725e66cSLinus Torvalds * is being freed. 201d725e66cSLinus Torvalds */ 202d725e66cSLinus Torvalds if (group->ops->freeing_mark) 203d725e66cSLinus Torvalds group->ops->freeing_mark(mark, group); 204*35e48176SJan Kara 205*35e48176SJan Kara spin_lock(&destroy_lock); 206*35e48176SJan Kara list_add(&mark->g_list, &destroy_list); 207*35e48176SJan Kara spin_unlock(&destroy_lock); 208*35e48176SJan Kara 209*35e48176SJan Kara return true; 210*35e48176SJan Kara } 211*35e48176SJan Kara 212*35e48176SJan Kara /* 213*35e48176SJan Kara * Free fsnotify mark. The freeing is actually happening from a workqueue which 214*35e48176SJan Kara * first waits for srcu period end. Caller must have a reference to the mark 215*35e48176SJan Kara * or be protected by fsnotify_mark_srcu. 216*35e48176SJan Kara */ 217*35e48176SJan Kara void fsnotify_free_mark(struct fsnotify_mark *mark) 218*35e48176SJan Kara { 219*35e48176SJan Kara if (__fsnotify_free_mark(mark)) { 220*35e48176SJan Kara queue_delayed_work(system_unbound_wq, &reaper_work, 221*35e48176SJan Kara FSNOTIFY_REAPER_DELAY); 222*35e48176SJan Kara } 223d5a335b8SLino Sanfilippo } 224d5a335b8SLino Sanfilippo 225d5a335b8SLino Sanfilippo void fsnotify_destroy_mark(struct fsnotify_mark *mark, 226d5a335b8SLino Sanfilippo struct fsnotify_group *group) 227d5a335b8SLino Sanfilippo { 2286960b0d9SLino Sanfilippo mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 2294712e722SJan Kara fsnotify_detach_mark(mark); 230d5a335b8SLino Sanfilippo mutex_unlock(&group->mark_mutex); 2314712e722SJan Kara fsnotify_free_mark(mark); 2325444e298SEric Paris } 2335444e298SEric Paris 234925d1132SJan Kara void fsnotify_destroy_marks(struct hlist_head *head, spinlock_t *lock) 2350809ab69SJan Kara { 236925d1132SJan Kara struct fsnotify_mark *mark; 2370809ab69SJan Kara 238925d1132SJan Kara while (1) { 239925d1132SJan Kara /* 240925d1132SJan Kara * We have to be careful since we can race with e.g. 241925d1132SJan Kara * fsnotify_clear_marks_by_group() and once we drop 'lock', 242925d1132SJan Kara * mark can get removed from the obj_list and destroyed. But 243925d1132SJan Kara * we are holding mark reference so mark cannot be freed and 244925d1132SJan Kara * calling fsnotify_destroy_mark() more than once is fine. 245925d1132SJan Kara */ 246925d1132SJan Kara spin_lock(lock); 247925d1132SJan Kara if (hlist_empty(head)) { 248925d1132SJan Kara spin_unlock(lock); 249925d1132SJan Kara break; 250925d1132SJan Kara } 251925d1132SJan Kara mark = hlist_entry(head->first, struct fsnotify_mark, obj_list); 252925d1132SJan Kara /* 253925d1132SJan Kara * We don't update i_fsnotify_mask / mnt_fsnotify_mask here 254925d1132SJan Kara * since inode / mount is going away anyway. So just remove 255925d1132SJan Kara * mark from the list. 256925d1132SJan Kara */ 257925d1132SJan Kara hlist_del_init_rcu(&mark->obj_list); 258925d1132SJan Kara fsnotify_get_mark(mark); 259925d1132SJan Kara spin_unlock(lock); 260925d1132SJan Kara fsnotify_destroy_mark(mark, mark->group); 2610809ab69SJan Kara fsnotify_put_mark(mark); 2620809ab69SJan Kara } 2630809ab69SJan Kara } 2640809ab69SJan Kara 26590b1e7a5SEric Paris void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask) 26690b1e7a5SEric Paris { 26790b1e7a5SEric Paris assert_spin_locked(&mark->lock); 26890b1e7a5SEric Paris 26990b1e7a5SEric Paris mark->mask = mask; 27090b1e7a5SEric Paris 27190b1e7a5SEric Paris if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) 27290b1e7a5SEric Paris fsnotify_set_inode_mark_mask_locked(mark, mask); 27390b1e7a5SEric Paris } 27490b1e7a5SEric Paris 27533af5e32SEric Paris void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask) 27633af5e32SEric Paris { 27733af5e32SEric Paris assert_spin_locked(&mark->lock); 27833af5e32SEric Paris 27933af5e32SEric Paris mark->ignored_mask = mask; 28033af5e32SEric Paris } 28190b1e7a5SEric Paris 2825444e298SEric Paris /* 2838edc6e16SJan Kara * Sorting function for lists of fsnotify marks. 2848edc6e16SJan Kara * 2858edc6e16SJan Kara * Fanotify supports different notification classes (reflected as priority of 2868edc6e16SJan Kara * notification group). Events shall be passed to notification groups in 2878edc6e16SJan Kara * decreasing priority order. To achieve this marks in notification lists for 2888edc6e16SJan Kara * inodes and vfsmounts are sorted so that priorities of corresponding groups 2898edc6e16SJan Kara * are descending. 2908edc6e16SJan Kara * 2918edc6e16SJan Kara * Furthermore correct handling of the ignore mask requires processing inode 2928edc6e16SJan Kara * and vfsmount marks of each group together. Using the group address as 2938edc6e16SJan Kara * further sort criterion provides a unique sorting order and thus we can 2948edc6e16SJan Kara * merge inode and vfsmount lists of marks in linear time and find groups 2958edc6e16SJan Kara * present in both lists. 2968edc6e16SJan Kara * 2978edc6e16SJan Kara * A return value of 1 signifies that b has priority over a. 2988edc6e16SJan Kara * A return value of 0 signifies that the two marks have to be handled together. 2998edc6e16SJan Kara * A return value of -1 signifies that a has priority over b. 3008edc6e16SJan Kara */ 3018edc6e16SJan Kara int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b) 3028edc6e16SJan Kara { 3038edc6e16SJan Kara if (a == b) 3048edc6e16SJan Kara return 0; 3058edc6e16SJan Kara if (!a) 3068edc6e16SJan Kara return 1; 3078edc6e16SJan Kara if (!b) 3088edc6e16SJan Kara return -1; 3098edc6e16SJan Kara if (a->priority < b->priority) 3108edc6e16SJan Kara return 1; 3118edc6e16SJan Kara if (a->priority > b->priority) 3128edc6e16SJan Kara return -1; 3138edc6e16SJan Kara if (a < b) 3148edc6e16SJan Kara return 1; 3158edc6e16SJan Kara return -1; 3168edc6e16SJan Kara } 3178edc6e16SJan Kara 3180809ab69SJan Kara /* Add mark into proper place in given list of marks */ 3190809ab69SJan Kara int fsnotify_add_mark_list(struct hlist_head *head, struct fsnotify_mark *mark, 3200809ab69SJan Kara int allow_dups) 3210809ab69SJan Kara { 3220809ab69SJan Kara struct fsnotify_mark *lmark, *last = NULL; 3230809ab69SJan Kara int cmp; 3240809ab69SJan Kara 3250809ab69SJan Kara /* is mark the first mark? */ 3260809ab69SJan Kara if (hlist_empty(head)) { 3270809ab69SJan Kara hlist_add_head_rcu(&mark->obj_list, head); 3280809ab69SJan Kara return 0; 3290809ab69SJan Kara } 3300809ab69SJan Kara 3310809ab69SJan Kara /* should mark be in the middle of the current list? */ 3320809ab69SJan Kara hlist_for_each_entry(lmark, head, obj_list) { 3330809ab69SJan Kara last = lmark; 3340809ab69SJan Kara 3350809ab69SJan Kara if ((lmark->group == mark->group) && !allow_dups) 3360809ab69SJan Kara return -EEXIST; 3370809ab69SJan Kara 3380809ab69SJan Kara cmp = fsnotify_compare_groups(lmark->group, mark->group); 3390809ab69SJan Kara if (cmp >= 0) { 3400809ab69SJan Kara hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list); 3410809ab69SJan Kara return 0; 3420809ab69SJan Kara } 3430809ab69SJan Kara } 3440809ab69SJan Kara 3450809ab69SJan Kara BUG_ON(last == NULL); 3460809ab69SJan Kara /* mark should be the last entry. last is the current last entry */ 3470809ab69SJan Kara hlist_add_behind_rcu(&mark->obj_list, &last->obj_list); 3480809ab69SJan Kara return 0; 3490809ab69SJan Kara } 3500809ab69SJan Kara 3518edc6e16SJan Kara /* 3525444e298SEric Paris * Attach an initialized mark to a given group and fs object. 3535444e298SEric Paris * These marks may be used for the fsnotify backend to determine which 3545444e298SEric Paris * event types should be delivered to which group. 3555444e298SEric Paris */ 356d5a335b8SLino Sanfilippo int fsnotify_add_mark_locked(struct fsnotify_mark *mark, 3575444e298SEric Paris struct fsnotify_group *group, struct inode *inode, 3585444e298SEric Paris struct vfsmount *mnt, int allow_dups) 3595444e298SEric Paris { 3605444e298SEric Paris int ret = 0; 3615444e298SEric Paris 3625444e298SEric Paris BUG_ON(inode && mnt); 3635444e298SEric Paris BUG_ON(!inode && !mnt); 364d5a335b8SLino Sanfilippo BUG_ON(!mutex_is_locked(&group->mark_mutex)); 3655444e298SEric Paris 3665444e298SEric Paris /* 3675444e298SEric Paris * LOCKING ORDER!!!! 368986ab098SLino Sanfilippo * group->mark_mutex 369104d06f0SLino Sanfilippo * mark->lock 3705444e298SEric Paris * inode->i_lock 3715444e298SEric Paris */ 372104d06f0SLino Sanfilippo spin_lock(&mark->lock); 3734712e722SJan Kara mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED; 374700307a2SEric Paris 37523e964c2SLino Sanfilippo fsnotify_get_group(group); 3765444e298SEric Paris mark->group = group; 3775444e298SEric Paris list_add(&mark->g_list, &group->marks_list); 3785444e298SEric Paris atomic_inc(&group->num_marks); 3795444e298SEric Paris fsnotify_get_mark(mark); /* for i_list and g_list */ 3805444e298SEric Paris 3815444e298SEric Paris if (inode) { 3825444e298SEric Paris ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups); 3835444e298SEric Paris if (ret) 3845444e298SEric Paris goto err; 3850d48b7f0SEric Paris } else if (mnt) { 3860d48b7f0SEric Paris ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups); 3870d48b7f0SEric Paris if (ret) 3880d48b7f0SEric Paris goto err; 3895444e298SEric Paris } else { 3905444e298SEric Paris BUG(); 3915444e298SEric Paris } 3925444e298SEric Paris 39390b1e7a5SEric Paris /* this will pin the object if appropriate */ 39490b1e7a5SEric Paris fsnotify_set_mark_mask_locked(mark, mark->mask); 3955444e298SEric Paris spin_unlock(&mark->lock); 3965444e298SEric Paris 3975444e298SEric Paris if (inode) 3985444e298SEric Paris __fsnotify_update_child_dentry_flags(inode); 3995444e298SEric Paris 4005444e298SEric Paris return ret; 4015444e298SEric Paris err: 402700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 4035444e298SEric Paris list_del_init(&mark->g_list); 40423e964c2SLino Sanfilippo fsnotify_put_group(group); 40575c1be48SEric Paris mark->group = NULL; 4065444e298SEric Paris atomic_dec(&group->num_marks); 4075444e298SEric Paris 4085444e298SEric Paris spin_unlock(&mark->lock); 4095444e298SEric Paris 41013d34ac6SJeff Layton spin_lock(&destroy_lock); 41113d34ac6SJeff Layton list_add(&mark->g_list, &destroy_list); 41213d34ac6SJeff Layton spin_unlock(&destroy_lock); 4130918f1c3SJeff Layton queue_delayed_work(system_unbound_wq, &reaper_work, 4140918f1c3SJeff Layton FSNOTIFY_REAPER_DELAY); 41513d34ac6SJeff Layton 4165444e298SEric Paris return ret; 4175444e298SEric Paris } 4185444e298SEric Paris 419d5a335b8SLino Sanfilippo int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group, 420d5a335b8SLino Sanfilippo struct inode *inode, struct vfsmount *mnt, int allow_dups) 421d5a335b8SLino Sanfilippo { 422d5a335b8SLino Sanfilippo int ret; 423d5a335b8SLino Sanfilippo mutex_lock(&group->mark_mutex); 424d5a335b8SLino Sanfilippo ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups); 425d5a335b8SLino Sanfilippo mutex_unlock(&group->mark_mutex); 426d5a335b8SLino Sanfilippo return ret; 427d5a335b8SLino Sanfilippo } 428d5a335b8SLino Sanfilippo 4295444e298SEric Paris /* 4300809ab69SJan Kara * Given a list of marks, find the mark associated with given group. If found 4310809ab69SJan Kara * take a reference to that mark and return it, else return NULL. 4320809ab69SJan Kara */ 4330809ab69SJan Kara struct fsnotify_mark *fsnotify_find_mark(struct hlist_head *head, 4340809ab69SJan Kara struct fsnotify_group *group) 4350809ab69SJan Kara { 4360809ab69SJan Kara struct fsnotify_mark *mark; 4370809ab69SJan Kara 4380809ab69SJan Kara hlist_for_each_entry(mark, head, obj_list) { 4390809ab69SJan Kara if (mark->group == group) { 4400809ab69SJan Kara fsnotify_get_mark(mark); 4410809ab69SJan Kara return mark; 4420809ab69SJan Kara } 4430809ab69SJan Kara } 4440809ab69SJan Kara return NULL; 4450809ab69SJan Kara } 4460809ab69SJan Kara 4470809ab69SJan Kara /* 448d725e66cSLinus Torvalds * clear any marks in a group in which mark->flags & flags is true 4495444e298SEric Paris */ 4504d92604cSEric Paris void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group, 4514d92604cSEric Paris unsigned int flags) 4525444e298SEric Paris { 4535444e298SEric Paris struct fsnotify_mark *lmark, *mark; 4548f2f3eb5SJan Kara LIST_HEAD(to_free); 4555444e298SEric Paris 4568f2f3eb5SJan Kara /* 4578f2f3eb5SJan Kara * We have to be really careful here. Anytime we drop mark_mutex, e.g. 4588f2f3eb5SJan Kara * fsnotify_clear_marks_by_inode() can come and free marks. Even in our 4598f2f3eb5SJan Kara * to_free list so we have to use mark_mutex even when accessing that 4608f2f3eb5SJan Kara * list. And freeing mark requires us to drop mark_mutex. So we can 4618f2f3eb5SJan Kara * reliably free only the first mark in the list. That's why we first 4628f2f3eb5SJan Kara * move marks to free to to_free list in one go and then free marks in 4638f2f3eb5SJan Kara * to_free list one by one. 4648f2f3eb5SJan Kara */ 4656960b0d9SLino Sanfilippo mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 4665444e298SEric Paris list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { 4678f2f3eb5SJan Kara if (mark->flags & flags) 4688f2f3eb5SJan Kara list_move(&mark->g_list, &to_free); 4694d92604cSEric Paris } 470986ab098SLino Sanfilippo mutex_unlock(&group->mark_mutex); 4718f2f3eb5SJan Kara 4728f2f3eb5SJan Kara while (1) { 4738f2f3eb5SJan Kara mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 4748f2f3eb5SJan Kara if (list_empty(&to_free)) { 4758f2f3eb5SJan Kara mutex_unlock(&group->mark_mutex); 4768f2f3eb5SJan Kara break; 4778f2f3eb5SJan Kara } 4788f2f3eb5SJan Kara mark = list_first_entry(&to_free, struct fsnotify_mark, g_list); 4798f2f3eb5SJan Kara fsnotify_get_mark(mark); 4804712e722SJan Kara fsnotify_detach_mark(mark); 4818f2f3eb5SJan Kara mutex_unlock(&group->mark_mutex); 4824712e722SJan Kara fsnotify_free_mark(mark); 4838f2f3eb5SJan Kara fsnotify_put_mark(mark); 4848f2f3eb5SJan Kara } 4855444e298SEric Paris } 4865444e298SEric Paris 4874d92604cSEric Paris /* 488*35e48176SJan Kara * Given a group, prepare for freeing all the marks associated with that group. 489*35e48176SJan Kara * The marks are attached to the list of marks prepared for destruction, the 490*35e48176SJan Kara * caller is responsible for freeing marks in that list after SRCU period has 491*35e48176SJan Kara * ended. 4924d92604cSEric Paris */ 493*35e48176SJan Kara void fsnotify_detach_group_marks(struct fsnotify_group *group) 4944d92604cSEric Paris { 495*35e48176SJan Kara struct fsnotify_mark *mark; 496*35e48176SJan Kara 497*35e48176SJan Kara while (1) { 498*35e48176SJan Kara mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 499*35e48176SJan Kara if (list_empty(&group->marks_list)) { 500*35e48176SJan Kara mutex_unlock(&group->mark_mutex); 501*35e48176SJan Kara break; 502*35e48176SJan Kara } 503*35e48176SJan Kara mark = list_first_entry(&group->marks_list, 504*35e48176SJan Kara struct fsnotify_mark, g_list); 505*35e48176SJan Kara fsnotify_get_mark(mark); 506*35e48176SJan Kara fsnotify_detach_mark(mark); 507*35e48176SJan Kara mutex_unlock(&group->mark_mutex); 508*35e48176SJan Kara __fsnotify_free_mark(mark); 509*35e48176SJan Kara fsnotify_put_mark(mark); 510*35e48176SJan Kara } 5114d92604cSEric Paris } 5124d92604cSEric Paris 5135444e298SEric Paris void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old) 5145444e298SEric Paris { 5155444e298SEric Paris assert_spin_locked(&old->lock); 5160809ab69SJan Kara new->inode = old->inode; 5170809ab69SJan Kara new->mnt = old->mnt; 51823e964c2SLino Sanfilippo if (old->group) 51923e964c2SLino Sanfilippo fsnotify_get_group(old->group); 5205444e298SEric Paris new->group = old->group; 5215444e298SEric Paris new->mask = old->mask; 5225444e298SEric Paris new->free_mark = old->free_mark; 5235444e298SEric Paris } 5245444e298SEric Paris 5255444e298SEric Paris /* 5265444e298SEric Paris * Nothing fancy, just initialize lists and locks and counters. 5275444e298SEric Paris */ 5285444e298SEric Paris void fsnotify_init_mark(struct fsnotify_mark *mark, 5295444e298SEric Paris void (*free_mark)(struct fsnotify_mark *mark)) 5305444e298SEric Paris { 531ba643f04SEric Paris memset(mark, 0, sizeof(*mark)); 5325444e298SEric Paris spin_lock_init(&mark->lock); 5335444e298SEric Paris atomic_set(&mark->refcnt, 1); 5345444e298SEric Paris mark->free_mark = free_mark; 5355444e298SEric Paris } 53613d34ac6SJeff Layton 537*35e48176SJan Kara /* 538*35e48176SJan Kara * Destroy all marks in destroy_list, waits for SRCU period to finish before 539*35e48176SJan Kara * actually freeing marks. 540*35e48176SJan Kara */ 541*35e48176SJan Kara void fsnotify_mark_destroy_list(void) 54213d34ac6SJeff Layton { 54313d34ac6SJeff Layton struct fsnotify_mark *mark, *next; 54413d34ac6SJeff Layton struct list_head private_destroy_list; 54513d34ac6SJeff Layton 54613d34ac6SJeff Layton spin_lock(&destroy_lock); 54713d34ac6SJeff Layton /* exchange the list head */ 54813d34ac6SJeff Layton list_replace_init(&destroy_list, &private_destroy_list); 54913d34ac6SJeff Layton spin_unlock(&destroy_lock); 55013d34ac6SJeff Layton 55113d34ac6SJeff Layton synchronize_srcu(&fsnotify_mark_srcu); 55213d34ac6SJeff Layton 55313d34ac6SJeff Layton list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) { 55413d34ac6SJeff Layton list_del_init(&mark->g_list); 55513d34ac6SJeff Layton fsnotify_put_mark(mark); 55613d34ac6SJeff Layton } 55713d34ac6SJeff Layton } 558*35e48176SJan Kara 559*35e48176SJan Kara static void fsnotify_mark_destroy_workfn(struct work_struct *work) 560*35e48176SJan Kara { 561*35e48176SJan Kara fsnotify_mark_destroy_list(); 562*35e48176SJan Kara } 563