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 54c1f33073SJan Kara * refcnt==0. Marks are also protected by fsnotify_mark_srcu. 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 * This has the very interesting property of being able to run concurrently with 655444e298SEric Paris * any (or all) other directions. 665444e298SEric Paris */ 675444e298SEric Paris 685444e298SEric Paris #include <linux/fs.h> 695444e298SEric Paris #include <linux/init.h> 705444e298SEric Paris #include <linux/kernel.h> 7175c1be48SEric Paris #include <linux/kthread.h> 725444e298SEric Paris #include <linux/module.h> 735444e298SEric Paris #include <linux/mutex.h> 745444e298SEric Paris #include <linux/slab.h> 755444e298SEric Paris #include <linux/spinlock.h> 7675c1be48SEric Paris #include <linux/srcu.h> 775444e298SEric Paris 7860063497SArun Sharma #include <linux/atomic.h> 795444e298SEric Paris 805444e298SEric Paris #include <linux/fsnotify_backend.h> 815444e298SEric Paris #include "fsnotify.h" 825444e298SEric Paris 830918f1c3SJeff Layton #define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */ 840918f1c3SJeff Layton 8575c1be48SEric Paris struct srcu_struct fsnotify_mark_srcu; 869dd813c1SJan Kara struct kmem_cache *fsnotify_mark_connector_cachep; 879dd813c1SJan Kara 8813d34ac6SJeff Layton static DEFINE_SPINLOCK(destroy_lock); 8913d34ac6SJeff Layton static LIST_HEAD(destroy_list); 900918f1c3SJeff Layton 9135e48176SJan Kara static void fsnotify_mark_destroy_workfn(struct work_struct *work); 9235e48176SJan Kara static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn); 9375c1be48SEric Paris 945444e298SEric Paris void fsnotify_get_mark(struct fsnotify_mark *mark) 955444e298SEric Paris { 965444e298SEric Paris atomic_inc(&mark->refcnt); 975444e298SEric Paris } 985444e298SEric Paris 995444e298SEric Paris void fsnotify_put_mark(struct fsnotify_mark *mark) 1005444e298SEric Paris { 10123e964c2SLino Sanfilippo if (atomic_dec_and_test(&mark->refcnt)) { 10223e964c2SLino Sanfilippo if (mark->group) 10323e964c2SLino Sanfilippo fsnotify_put_group(mark->group); 1045444e298SEric Paris mark->free_mark(mark); 1055444e298SEric Paris } 10623e964c2SLino Sanfilippo } 1075444e298SEric Paris 108a242677bSJan Kara static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) 1090809ab69SJan Kara { 1100809ab69SJan Kara u32 new_mask = 0; 1110809ab69SJan Kara struct fsnotify_mark *mark; 1120809ab69SJan Kara 1139dd813c1SJan Kara hlist_for_each_entry(mark, &conn->list, obj_list) 1140809ab69SJan Kara new_mask |= mark->mask; 115a242677bSJan Kara if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) 116a242677bSJan Kara conn->inode->i_fsnotify_mask = new_mask; 117a242677bSJan Kara else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT) 118a242677bSJan Kara real_mount(conn->mnt)->mnt_fsnotify_mask = new_mask; 119a242677bSJan Kara } 120a242677bSJan Kara 121a242677bSJan Kara /* 122a242677bSJan Kara * Calculate mask of events for a list of marks. The caller must make sure 123a242677bSJan Kara * connector cannot disappear under us (usually by holding a mark->lock or 124a242677bSJan Kara * mark->group->mark_mutex for a mark on this list). 125a242677bSJan Kara */ 126a242677bSJan Kara void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) 127a242677bSJan Kara { 128a242677bSJan Kara if (!conn) 129a242677bSJan Kara return; 130a242677bSJan Kara 131a242677bSJan Kara if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) 132a242677bSJan Kara spin_lock(&conn->inode->i_lock); 133a242677bSJan Kara else 134a242677bSJan Kara spin_lock(&conn->mnt->mnt_root->d_lock); 135a242677bSJan Kara __fsnotify_recalc_mask(conn); 136a242677bSJan Kara if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) { 137a242677bSJan Kara spin_unlock(&conn->inode->i_lock); 138a242677bSJan Kara __fsnotify_update_child_dentry_flags(conn->inode); 139a242677bSJan Kara } else { 140a242677bSJan Kara spin_unlock(&conn->mnt->mnt_root->d_lock); 141a242677bSJan Kara } 1420809ab69SJan Kara } 1430809ab69SJan Kara 144*8212a609SJan Kara static struct inode *fsnotify_detach_from_object(struct fsnotify_mark *mark) 145*8212a609SJan Kara { 146*8212a609SJan Kara struct fsnotify_mark_connector *conn; 147*8212a609SJan Kara struct inode *inode = NULL; 148*8212a609SJan Kara spinlock_t *lock; 149*8212a609SJan Kara 150*8212a609SJan Kara conn = mark->connector; 151*8212a609SJan Kara if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) 152*8212a609SJan Kara lock = &conn->inode->i_lock; 153*8212a609SJan Kara else 154*8212a609SJan Kara lock = &conn->mnt->mnt_root->d_lock; 155*8212a609SJan Kara spin_lock(lock); 156*8212a609SJan Kara hlist_del_init_rcu(&mark->obj_list); 157*8212a609SJan Kara if (hlist_empty(&conn->list)) { 158*8212a609SJan Kara if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) 159*8212a609SJan Kara inode = conn->inode; 160*8212a609SJan Kara } 161*8212a609SJan Kara mark->connector = NULL; 162*8212a609SJan Kara spin_unlock(lock); 163*8212a609SJan Kara fsnotify_recalc_mask(conn); 164*8212a609SJan Kara 165*8212a609SJan Kara return inode; 166*8212a609SJan Kara } 167*8212a609SJan Kara 1685444e298SEric Paris /* 1694712e722SJan Kara * Remove mark from inode / vfsmount list, group list, drop inode reference 1704712e722SJan Kara * if we got one. 1714712e722SJan Kara * 1724712e722SJan Kara * Must be called with group->mark_mutex held. 1735444e298SEric Paris */ 1744712e722SJan Kara void fsnotify_detach_mark(struct fsnotify_mark *mark) 1755444e298SEric Paris { 1760d48b7f0SEric Paris struct inode *inode = NULL; 1774712e722SJan Kara struct fsnotify_group *group = mark->group; 1785444e298SEric Paris 179d5a335b8SLino Sanfilippo BUG_ON(!mutex_is_locked(&group->mark_mutex)); 180d5a335b8SLino Sanfilippo 181104d06f0SLino Sanfilippo spin_lock(&mark->lock); 1825444e298SEric Paris 183700307a2SEric Paris /* something else already called this function on this mark */ 1844712e722SJan Kara if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { 1855444e298SEric Paris spin_unlock(&mark->lock); 186e2a29943SLino Sanfilippo return; 1875444e298SEric Paris } 1885444e298SEric Paris 1894712e722SJan Kara mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED; 190700307a2SEric Paris 191*8212a609SJan Kara inode = fsnotify_detach_from_object(mark); 192*8212a609SJan Kara 1934712e722SJan Kara /* 1944712e722SJan Kara * Note that we didn't update flags telling whether inode cares about 1954712e722SJan Kara * what's happening with children. We update these flags from 1964712e722SJan Kara * __fsnotify_parent() lazily when next event happens on one of our 1974712e722SJan Kara * children. 1984712e722SJan Kara */ 1995444e298SEric Paris 2005444e298SEric Paris list_del_init(&mark->g_list); 201d725e66cSLinus Torvalds 2025444e298SEric Paris spin_unlock(&mark->lock); 203d5a335b8SLino Sanfilippo 204e911d8afSJan Kara if (inode) 2056960b0d9SLino Sanfilippo iput(inode); 2064712e722SJan Kara 2074712e722SJan Kara atomic_dec(&group->num_marks); 2084712e722SJan Kara } 2094712e722SJan Kara 2104712e722SJan Kara /* 21135e48176SJan Kara * Prepare mark for freeing and add it to the list of marks prepared for 21235e48176SJan Kara * freeing. The actual freeing must happen after SRCU period ends and the 21335e48176SJan Kara * caller is responsible for this. 21435e48176SJan Kara * 21535e48176SJan Kara * The function returns true if the mark was added to the list of marks for 21635e48176SJan Kara * freeing. The function returns false if someone else has already called 21735e48176SJan Kara * __fsnotify_free_mark() for the mark. 2184712e722SJan Kara */ 21935e48176SJan Kara static bool __fsnotify_free_mark(struct fsnotify_mark *mark) 2204712e722SJan Kara { 2214712e722SJan Kara struct fsnotify_group *group = mark->group; 2224712e722SJan Kara 2234712e722SJan Kara spin_lock(&mark->lock); 2244712e722SJan Kara /* something else already called this function on this mark */ 2254712e722SJan Kara if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { 2264712e722SJan Kara spin_unlock(&mark->lock); 22735e48176SJan Kara return false; 2284712e722SJan Kara } 2294712e722SJan Kara mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 2304712e722SJan Kara spin_unlock(&mark->lock); 2315444e298SEric Paris 232d725e66cSLinus Torvalds /* 233d725e66cSLinus Torvalds * Some groups like to know that marks are being freed. This is a 234d725e66cSLinus Torvalds * callback to the group function to let it know that this mark 235d725e66cSLinus Torvalds * is being freed. 236d725e66cSLinus Torvalds */ 237d725e66cSLinus Torvalds if (group->ops->freeing_mark) 238d725e66cSLinus Torvalds group->ops->freeing_mark(mark, group); 23935e48176SJan Kara 24035e48176SJan Kara spin_lock(&destroy_lock); 24135e48176SJan Kara list_add(&mark->g_list, &destroy_list); 24235e48176SJan Kara spin_unlock(&destroy_lock); 24335e48176SJan Kara 24435e48176SJan Kara return true; 24535e48176SJan Kara } 24635e48176SJan Kara 24735e48176SJan Kara /* 24835e48176SJan Kara * Free fsnotify mark. The freeing is actually happening from a workqueue which 24935e48176SJan Kara * first waits for srcu period end. Caller must have a reference to the mark 25035e48176SJan Kara * or be protected by fsnotify_mark_srcu. 25135e48176SJan Kara */ 25235e48176SJan Kara void fsnotify_free_mark(struct fsnotify_mark *mark) 25335e48176SJan Kara { 25435e48176SJan Kara if (__fsnotify_free_mark(mark)) { 25535e48176SJan Kara queue_delayed_work(system_unbound_wq, &reaper_work, 25635e48176SJan Kara FSNOTIFY_REAPER_DELAY); 25735e48176SJan Kara } 258d5a335b8SLino Sanfilippo } 259d5a335b8SLino Sanfilippo 260d5a335b8SLino Sanfilippo void fsnotify_destroy_mark(struct fsnotify_mark *mark, 261d5a335b8SLino Sanfilippo struct fsnotify_group *group) 262d5a335b8SLino Sanfilippo { 2636960b0d9SLino Sanfilippo mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 2644712e722SJan Kara fsnotify_detach_mark(mark); 265d5a335b8SLino Sanfilippo mutex_unlock(&group->mark_mutex); 2664712e722SJan Kara fsnotify_free_mark(mark); 2675444e298SEric Paris } 2685444e298SEric Paris 2699dd813c1SJan Kara void fsnotify_connector_free(struct fsnotify_mark_connector **connp) 2709dd813c1SJan Kara { 2719dd813c1SJan Kara if (*connp) { 2729dd813c1SJan Kara kmem_cache_free(fsnotify_mark_connector_cachep, *connp); 2739dd813c1SJan Kara *connp = NULL; 2749dd813c1SJan Kara } 2759dd813c1SJan Kara } 2769dd813c1SJan Kara 27790b1e7a5SEric Paris void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask) 27890b1e7a5SEric Paris { 27990b1e7a5SEric Paris assert_spin_locked(&mark->lock); 28090b1e7a5SEric Paris 28190b1e7a5SEric Paris mark->mask = mask; 28290b1e7a5SEric Paris } 28390b1e7a5SEric Paris 28433af5e32SEric Paris void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask) 28533af5e32SEric Paris { 28633af5e32SEric Paris assert_spin_locked(&mark->lock); 28733af5e32SEric Paris 28833af5e32SEric Paris mark->ignored_mask = mask; 28933af5e32SEric Paris } 29090b1e7a5SEric Paris 2915444e298SEric Paris /* 2928edc6e16SJan Kara * Sorting function for lists of fsnotify marks. 2938edc6e16SJan Kara * 2948edc6e16SJan Kara * Fanotify supports different notification classes (reflected as priority of 2958edc6e16SJan Kara * notification group). Events shall be passed to notification groups in 2968edc6e16SJan Kara * decreasing priority order. To achieve this marks in notification lists for 2978edc6e16SJan Kara * inodes and vfsmounts are sorted so that priorities of corresponding groups 2988edc6e16SJan Kara * are descending. 2998edc6e16SJan Kara * 3008edc6e16SJan Kara * Furthermore correct handling of the ignore mask requires processing inode 3018edc6e16SJan Kara * and vfsmount marks of each group together. Using the group address as 3028edc6e16SJan Kara * further sort criterion provides a unique sorting order and thus we can 3038edc6e16SJan Kara * merge inode and vfsmount lists of marks in linear time and find groups 3048edc6e16SJan Kara * present in both lists. 3058edc6e16SJan Kara * 3068edc6e16SJan Kara * A return value of 1 signifies that b has priority over a. 3078edc6e16SJan Kara * A return value of 0 signifies that the two marks have to be handled together. 3088edc6e16SJan Kara * A return value of -1 signifies that a has priority over b. 3098edc6e16SJan Kara */ 3108edc6e16SJan Kara int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b) 3118edc6e16SJan Kara { 3128edc6e16SJan Kara if (a == b) 3138edc6e16SJan Kara return 0; 3148edc6e16SJan Kara if (!a) 3158edc6e16SJan Kara return 1; 3168edc6e16SJan Kara if (!b) 3178edc6e16SJan Kara return -1; 3188edc6e16SJan Kara if (a->priority < b->priority) 3198edc6e16SJan Kara return 1; 3208edc6e16SJan Kara if (a->priority > b->priority) 3218edc6e16SJan Kara return -1; 3228edc6e16SJan Kara if (a < b) 3238edc6e16SJan Kara return 1; 3248edc6e16SJan Kara return -1; 3258edc6e16SJan Kara } 3268edc6e16SJan Kara 3279dd813c1SJan Kara static int fsnotify_attach_connector_to_object( 32886ffe245SJan Kara struct fsnotify_mark_connector **connp, 329755b5bc6SJan Kara spinlock_t *lock, 33086ffe245SJan Kara struct inode *inode, 33186ffe245SJan Kara struct vfsmount *mnt) 3329dd813c1SJan Kara { 3339dd813c1SJan Kara struct fsnotify_mark_connector *conn; 3349dd813c1SJan Kara 335755b5bc6SJan Kara conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL); 3369dd813c1SJan Kara if (!conn) 3379dd813c1SJan Kara return -ENOMEM; 3389dd813c1SJan Kara INIT_HLIST_HEAD(&conn->list); 33986ffe245SJan Kara if (inode) { 34086ffe245SJan Kara conn->flags = FSNOTIFY_OBJ_TYPE_INODE; 34186ffe245SJan Kara conn->inode = inode; 34286ffe245SJan Kara } else { 34386ffe245SJan Kara conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT; 34486ffe245SJan Kara conn->mnt = mnt; 34586ffe245SJan Kara } 3469dd813c1SJan Kara /* 3479dd813c1SJan Kara * Make sure 'conn' initialization is visible. Matches 3489dd813c1SJan Kara * lockless_dereference() in fsnotify(). 3499dd813c1SJan Kara */ 3509dd813c1SJan Kara smp_wmb(); 351755b5bc6SJan Kara spin_lock(lock); 352755b5bc6SJan Kara if (!*connp) 3539dd813c1SJan Kara *connp = conn; 354755b5bc6SJan Kara else 355755b5bc6SJan Kara kmem_cache_free(fsnotify_mark_connector_cachep, conn); 356755b5bc6SJan Kara spin_unlock(lock); 3579dd813c1SJan Kara 3589dd813c1SJan Kara return 0; 3599dd813c1SJan Kara } 3609dd813c1SJan Kara 3619dd813c1SJan Kara /* 3629dd813c1SJan Kara * Add mark into proper place in given list of marks. These marks may be used 3639dd813c1SJan Kara * for the fsnotify backend to determine which event types should be delivered 3649dd813c1SJan Kara * to which group and for which inodes. These marks are ordered according to 3659dd813c1SJan Kara * priority, highest number first, and then by the group's location in memory. 3669dd813c1SJan Kara */ 367755b5bc6SJan Kara static int fsnotify_add_mark_list(struct fsnotify_mark *mark, 368755b5bc6SJan Kara struct inode *inode, struct vfsmount *mnt, 369755b5bc6SJan Kara int allow_dups) 3700809ab69SJan Kara { 3710809ab69SJan Kara struct fsnotify_mark *lmark, *last = NULL; 3729dd813c1SJan Kara struct fsnotify_mark_connector *conn; 373755b5bc6SJan Kara struct fsnotify_mark_connector **connp; 374755b5bc6SJan Kara spinlock_t *lock; 3750809ab69SJan Kara int cmp; 376755b5bc6SJan Kara int err = 0; 377755b5bc6SJan Kara 378755b5bc6SJan Kara if (WARN_ON(!inode && !mnt)) 379755b5bc6SJan Kara return -EINVAL; 380755b5bc6SJan Kara if (inode) { 381755b5bc6SJan Kara connp = &inode->i_fsnotify_marks; 382755b5bc6SJan Kara lock = &inode->i_lock; 383755b5bc6SJan Kara } else { 384755b5bc6SJan Kara connp = &real_mount(mnt)->mnt_fsnotify_marks; 385755b5bc6SJan Kara lock = &mnt->mnt_root->d_lock; 386755b5bc6SJan Kara } 3879dd813c1SJan Kara 3889dd813c1SJan Kara if (!*connp) { 389755b5bc6SJan Kara err = fsnotify_attach_connector_to_object(connp, lock, 390755b5bc6SJan Kara inode, mnt); 3919dd813c1SJan Kara if (err) 3929dd813c1SJan Kara return err; 3939dd813c1SJan Kara } 394755b5bc6SJan Kara spin_lock(&mark->lock); 395755b5bc6SJan Kara spin_lock(lock); 3969dd813c1SJan Kara conn = *connp; 3970809ab69SJan Kara 3980809ab69SJan Kara /* is mark the first mark? */ 3999dd813c1SJan Kara if (hlist_empty(&conn->list)) { 4009dd813c1SJan Kara hlist_add_head_rcu(&mark->obj_list, &conn->list); 401e911d8afSJan Kara if (inode) 402e911d8afSJan Kara __iget(inode); 40386ffe245SJan Kara goto added; 4040809ab69SJan Kara } 4050809ab69SJan Kara 4060809ab69SJan Kara /* should mark be in the middle of the current list? */ 4079dd813c1SJan Kara hlist_for_each_entry(lmark, &conn->list, obj_list) { 4080809ab69SJan Kara last = lmark; 4090809ab69SJan Kara 410755b5bc6SJan Kara if ((lmark->group == mark->group) && !allow_dups) { 411755b5bc6SJan Kara err = -EEXIST; 412755b5bc6SJan Kara goto out_err; 413755b5bc6SJan Kara } 4140809ab69SJan Kara 4150809ab69SJan Kara cmp = fsnotify_compare_groups(lmark->group, mark->group); 4160809ab69SJan Kara if (cmp >= 0) { 4170809ab69SJan Kara hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list); 41886ffe245SJan Kara goto added; 4190809ab69SJan Kara } 4200809ab69SJan Kara } 4210809ab69SJan Kara 4220809ab69SJan Kara BUG_ON(last == NULL); 4230809ab69SJan Kara /* mark should be the last entry. last is the current last entry */ 4240809ab69SJan Kara hlist_add_behind_rcu(&mark->obj_list, &last->obj_list); 42586ffe245SJan Kara added: 42686ffe245SJan Kara mark->connector = conn; 427755b5bc6SJan Kara out_err: 428755b5bc6SJan Kara spin_unlock(lock); 429755b5bc6SJan Kara spin_unlock(&mark->lock); 430755b5bc6SJan Kara return err; 4310809ab69SJan Kara } 4320809ab69SJan Kara 4338edc6e16SJan Kara /* 4345444e298SEric Paris * Attach an initialized mark to a given group and fs object. 4355444e298SEric Paris * These marks may be used for the fsnotify backend to determine which 4365444e298SEric Paris * event types should be delivered to which group. 4375444e298SEric Paris */ 438d5a335b8SLino Sanfilippo int fsnotify_add_mark_locked(struct fsnotify_mark *mark, 4395444e298SEric Paris struct fsnotify_group *group, struct inode *inode, 4405444e298SEric Paris struct vfsmount *mnt, int allow_dups) 4415444e298SEric Paris { 4425444e298SEric Paris int ret = 0; 4435444e298SEric Paris 4445444e298SEric Paris BUG_ON(inode && mnt); 4455444e298SEric Paris BUG_ON(!inode && !mnt); 446d5a335b8SLino Sanfilippo BUG_ON(!mutex_is_locked(&group->mark_mutex)); 4475444e298SEric Paris 4485444e298SEric Paris /* 4495444e298SEric Paris * LOCKING ORDER!!!! 450986ab098SLino Sanfilippo * group->mark_mutex 451104d06f0SLino Sanfilippo * mark->lock 4525444e298SEric Paris * inode->i_lock 4535444e298SEric Paris */ 454104d06f0SLino Sanfilippo spin_lock(&mark->lock); 4554712e722SJan Kara mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED; 456700307a2SEric Paris 45723e964c2SLino Sanfilippo fsnotify_get_group(group); 4585444e298SEric Paris mark->group = group; 4595444e298SEric Paris list_add(&mark->g_list, &group->marks_list); 4605444e298SEric Paris atomic_inc(&group->num_marks); 4615444e298SEric Paris fsnotify_get_mark(mark); /* for i_list and g_list */ 4625444e298SEric Paris spin_unlock(&mark->lock); 4635444e298SEric Paris 464755b5bc6SJan Kara ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups); 465755b5bc6SJan Kara if (ret) 466755b5bc6SJan Kara goto err; 467755b5bc6SJan Kara 468a242677bSJan Kara if (mark->mask) 469a242677bSJan Kara fsnotify_recalc_mask(mark->connector); 4705444e298SEric Paris 4715444e298SEric Paris return ret; 4725444e298SEric Paris err: 473700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 4745444e298SEric Paris list_del_init(&mark->g_list); 47523e964c2SLino Sanfilippo fsnotify_put_group(group); 47675c1be48SEric Paris mark->group = NULL; 4775444e298SEric Paris atomic_dec(&group->num_marks); 4785444e298SEric Paris 4795444e298SEric Paris spin_unlock(&mark->lock); 4805444e298SEric Paris 48113d34ac6SJeff Layton spin_lock(&destroy_lock); 48213d34ac6SJeff Layton list_add(&mark->g_list, &destroy_list); 48313d34ac6SJeff Layton spin_unlock(&destroy_lock); 4840918f1c3SJeff Layton queue_delayed_work(system_unbound_wq, &reaper_work, 4850918f1c3SJeff Layton FSNOTIFY_REAPER_DELAY); 48613d34ac6SJeff Layton 4875444e298SEric Paris return ret; 4885444e298SEric Paris } 4895444e298SEric Paris 490d5a335b8SLino Sanfilippo int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group, 491d5a335b8SLino Sanfilippo struct inode *inode, struct vfsmount *mnt, int allow_dups) 492d5a335b8SLino Sanfilippo { 493d5a335b8SLino Sanfilippo int ret; 494d5a335b8SLino Sanfilippo mutex_lock(&group->mark_mutex); 495d5a335b8SLino Sanfilippo ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups); 496d5a335b8SLino Sanfilippo mutex_unlock(&group->mark_mutex); 497d5a335b8SLino Sanfilippo return ret; 498d5a335b8SLino Sanfilippo } 499d5a335b8SLino Sanfilippo 5005444e298SEric Paris /* 5010809ab69SJan Kara * Given a list of marks, find the mark associated with given group. If found 5020809ab69SJan Kara * take a reference to that mark and return it, else return NULL. 5030809ab69SJan Kara */ 5049dd813c1SJan Kara struct fsnotify_mark *fsnotify_find_mark(struct fsnotify_mark_connector *conn, 5050809ab69SJan Kara struct fsnotify_group *group) 5060809ab69SJan Kara { 5070809ab69SJan Kara struct fsnotify_mark *mark; 508f06fd987SJan Kara spinlock_t *lock; 5090809ab69SJan Kara 5109dd813c1SJan Kara if (!conn) 5119dd813c1SJan Kara return NULL; 5129dd813c1SJan Kara 513f06fd987SJan Kara if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) 514f06fd987SJan Kara lock = &conn->inode->i_lock; 515f06fd987SJan Kara else 516f06fd987SJan Kara lock = &conn->mnt->mnt_root->d_lock; 517f06fd987SJan Kara spin_lock(lock); 5189dd813c1SJan Kara hlist_for_each_entry(mark, &conn->list, obj_list) { 5190809ab69SJan Kara if (mark->group == group) { 5200809ab69SJan Kara fsnotify_get_mark(mark); 521f06fd987SJan Kara spin_unlock(lock); 5220809ab69SJan Kara return mark; 5230809ab69SJan Kara } 5240809ab69SJan Kara } 525f06fd987SJan Kara spin_unlock(lock); 5260809ab69SJan Kara return NULL; 5270809ab69SJan Kara } 5280809ab69SJan Kara 5290809ab69SJan Kara /* 530d725e66cSLinus Torvalds * clear any marks in a group in which mark->flags & flags is true 5315444e298SEric Paris */ 5324d92604cSEric Paris void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group, 5334d92604cSEric Paris unsigned int flags) 5345444e298SEric Paris { 5355444e298SEric Paris struct fsnotify_mark *lmark, *mark; 5368f2f3eb5SJan Kara LIST_HEAD(to_free); 5375444e298SEric Paris 5388f2f3eb5SJan Kara /* 5398f2f3eb5SJan Kara * We have to be really careful here. Anytime we drop mark_mutex, e.g. 5408f2f3eb5SJan Kara * fsnotify_clear_marks_by_inode() can come and free marks. Even in our 5418f2f3eb5SJan Kara * to_free list so we have to use mark_mutex even when accessing that 5428f2f3eb5SJan Kara * list. And freeing mark requires us to drop mark_mutex. So we can 5438f2f3eb5SJan Kara * reliably free only the first mark in the list. That's why we first 5448f2f3eb5SJan Kara * move marks to free to to_free list in one go and then free marks in 5458f2f3eb5SJan Kara * to_free list one by one. 5468f2f3eb5SJan Kara */ 5476960b0d9SLino Sanfilippo mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 5485444e298SEric Paris list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { 54986ffe245SJan Kara if (mark->connector->flags & flags) 5508f2f3eb5SJan Kara list_move(&mark->g_list, &to_free); 5514d92604cSEric Paris } 552986ab098SLino Sanfilippo mutex_unlock(&group->mark_mutex); 5538f2f3eb5SJan Kara 5548f2f3eb5SJan Kara while (1) { 5558f2f3eb5SJan Kara mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 5568f2f3eb5SJan Kara if (list_empty(&to_free)) { 5578f2f3eb5SJan Kara mutex_unlock(&group->mark_mutex); 5588f2f3eb5SJan Kara break; 5598f2f3eb5SJan Kara } 5608f2f3eb5SJan Kara mark = list_first_entry(&to_free, struct fsnotify_mark, g_list); 5618f2f3eb5SJan Kara fsnotify_get_mark(mark); 5624712e722SJan Kara fsnotify_detach_mark(mark); 5638f2f3eb5SJan Kara mutex_unlock(&group->mark_mutex); 5644712e722SJan Kara fsnotify_free_mark(mark); 5658f2f3eb5SJan Kara fsnotify_put_mark(mark); 5668f2f3eb5SJan Kara } 5675444e298SEric Paris } 5685444e298SEric Paris 5694d92604cSEric Paris /* 57035e48176SJan Kara * Given a group, prepare for freeing all the marks associated with that group. 57135e48176SJan Kara * The marks are attached to the list of marks prepared for destruction, the 57235e48176SJan Kara * caller is responsible for freeing marks in that list after SRCU period has 57335e48176SJan Kara * ended. 5744d92604cSEric Paris */ 57535e48176SJan Kara void fsnotify_detach_group_marks(struct fsnotify_group *group) 5764d92604cSEric Paris { 57735e48176SJan Kara struct fsnotify_mark *mark; 57835e48176SJan Kara 57935e48176SJan Kara while (1) { 58035e48176SJan Kara mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 58135e48176SJan Kara if (list_empty(&group->marks_list)) { 58235e48176SJan Kara mutex_unlock(&group->mark_mutex); 58335e48176SJan Kara break; 58435e48176SJan Kara } 58535e48176SJan Kara mark = list_first_entry(&group->marks_list, 58635e48176SJan Kara struct fsnotify_mark, g_list); 58735e48176SJan Kara fsnotify_get_mark(mark); 58835e48176SJan Kara fsnotify_detach_mark(mark); 58935e48176SJan Kara mutex_unlock(&group->mark_mutex); 59035e48176SJan Kara __fsnotify_free_mark(mark); 59135e48176SJan Kara fsnotify_put_mark(mark); 59235e48176SJan Kara } 5934d92604cSEric Paris } 5944d92604cSEric Paris 595a03e2e4fSJan Kara void fsnotify_destroy_marks(struct fsnotify_mark_connector *conn) 5960810b4f9SJan Kara { 5970810b4f9SJan Kara struct fsnotify_mark *mark; 598a03e2e4fSJan Kara spinlock_t *lock; 5990810b4f9SJan Kara 6000810b4f9SJan Kara if (!conn) 6010810b4f9SJan Kara return; 6020810b4f9SJan Kara 603a03e2e4fSJan Kara if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) 604a03e2e4fSJan Kara lock = &conn->inode->i_lock; 605a03e2e4fSJan Kara else 606a03e2e4fSJan Kara lock = &conn->mnt->mnt_root->d_lock; 607a03e2e4fSJan Kara 6080810b4f9SJan Kara while (1) { 6090810b4f9SJan Kara /* 6100810b4f9SJan Kara * We have to be careful since we can race with e.g. 6110810b4f9SJan Kara * fsnotify_clear_marks_by_group() and once we drop 'lock', 6120810b4f9SJan Kara * mark can get removed from the obj_list and destroyed. But 6130810b4f9SJan Kara * we are holding mark reference so mark cannot be freed and 6140810b4f9SJan Kara * calling fsnotify_destroy_mark() more than once is fine. 6150810b4f9SJan Kara */ 6160810b4f9SJan Kara spin_lock(lock); 6170810b4f9SJan Kara if (hlist_empty(&conn->list)) { 6180810b4f9SJan Kara spin_unlock(lock); 6190810b4f9SJan Kara break; 6200810b4f9SJan Kara } 6210810b4f9SJan Kara mark = hlist_entry(conn->list.first, struct fsnotify_mark, 6220810b4f9SJan Kara obj_list); 6230810b4f9SJan Kara /* 6240810b4f9SJan Kara * We don't update i_fsnotify_mask / mnt_fsnotify_mask here 6250810b4f9SJan Kara * since inode / mount is going away anyway. So just remove 6260810b4f9SJan Kara * mark from the list. 6270810b4f9SJan Kara */ 6280810b4f9SJan Kara hlist_del_init_rcu(&mark->obj_list); 6290810b4f9SJan Kara fsnotify_get_mark(mark); 6300810b4f9SJan Kara spin_unlock(lock); 6310810b4f9SJan Kara fsnotify_destroy_mark(mark, mark->group); 6320810b4f9SJan Kara fsnotify_put_mark(mark); 6330810b4f9SJan Kara } 6340810b4f9SJan Kara } 6350810b4f9SJan Kara 6365444e298SEric Paris /* 6375444e298SEric Paris * Nothing fancy, just initialize lists and locks and counters. 6385444e298SEric Paris */ 6395444e298SEric Paris void fsnotify_init_mark(struct fsnotify_mark *mark, 6405444e298SEric Paris void (*free_mark)(struct fsnotify_mark *mark)) 6415444e298SEric Paris { 642ba643f04SEric Paris memset(mark, 0, sizeof(*mark)); 6435444e298SEric Paris spin_lock_init(&mark->lock); 6445444e298SEric Paris atomic_set(&mark->refcnt, 1); 6455444e298SEric Paris mark->free_mark = free_mark; 6465444e298SEric Paris } 64713d34ac6SJeff Layton 64835e48176SJan Kara /* 64935e48176SJan Kara * Destroy all marks in destroy_list, waits for SRCU period to finish before 65035e48176SJan Kara * actually freeing marks. 65135e48176SJan Kara */ 65235e48176SJan Kara void fsnotify_mark_destroy_list(void) 65313d34ac6SJeff Layton { 65413d34ac6SJeff Layton struct fsnotify_mark *mark, *next; 65513d34ac6SJeff Layton struct list_head private_destroy_list; 65613d34ac6SJeff Layton 65713d34ac6SJeff Layton spin_lock(&destroy_lock); 65813d34ac6SJeff Layton /* exchange the list head */ 65913d34ac6SJeff Layton list_replace_init(&destroy_list, &private_destroy_list); 66013d34ac6SJeff Layton spin_unlock(&destroy_lock); 66113d34ac6SJeff Layton 66213d34ac6SJeff Layton synchronize_srcu(&fsnotify_mark_srcu); 66313d34ac6SJeff Layton 66413d34ac6SJeff Layton list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) { 66513d34ac6SJeff Layton list_del_init(&mark->g_list); 66613d34ac6SJeff Layton fsnotify_put_mark(mark); 66713d34ac6SJeff Layton } 66813d34ac6SJeff Layton } 66935e48176SJan Kara 67035e48176SJan Kara static void fsnotify_mark_destroy_workfn(struct work_struct *work) 67135e48176SJan Kara { 67235e48176SJan Kara fsnotify_mark_destroy_list(); 67335e48176SJan Kara } 674