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 1080809ab69SJan Kara /* Calculate mask of events for a list of marks */ 1099dd813c1SJan Kara u32 fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) 1100809ab69SJan Kara { 1110809ab69SJan Kara u32 new_mask = 0; 1120809ab69SJan Kara struct fsnotify_mark *mark; 1130809ab69SJan Kara 1149dd813c1SJan Kara if (!conn) 1159dd813c1SJan Kara return 0; 1169dd813c1SJan Kara 1179dd813c1SJan Kara hlist_for_each_entry(mark, &conn->list, obj_list) 1180809ab69SJan Kara new_mask |= mark->mask; 1190809ab69SJan Kara return new_mask; 1200809ab69SJan Kara } 1210809ab69SJan Kara 1225444e298SEric Paris /* 1234712e722SJan Kara * Remove mark from inode / vfsmount list, group list, drop inode reference 1244712e722SJan Kara * if we got one. 1254712e722SJan Kara * 1264712e722SJan Kara * Must be called with group->mark_mutex held. 1275444e298SEric Paris */ 1284712e722SJan Kara void fsnotify_detach_mark(struct fsnotify_mark *mark) 1295444e298SEric Paris { 1300d48b7f0SEric Paris struct inode *inode = NULL; 1314712e722SJan Kara struct fsnotify_group *group = mark->group; 1325444e298SEric Paris 133d5a335b8SLino Sanfilippo BUG_ON(!mutex_is_locked(&group->mark_mutex)); 134d5a335b8SLino Sanfilippo 135104d06f0SLino Sanfilippo spin_lock(&mark->lock); 1365444e298SEric Paris 137700307a2SEric Paris /* something else already called this function on this mark */ 1384712e722SJan Kara if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { 1395444e298SEric Paris spin_unlock(&mark->lock); 140e2a29943SLino Sanfilippo return; 1415444e298SEric Paris } 1425444e298SEric Paris 1434712e722SJan Kara mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED; 144700307a2SEric Paris 145e911d8afSJan Kara if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_INODE) 146e911d8afSJan Kara inode = fsnotify_destroy_inode_mark(mark); 147e911d8afSJan Kara else if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT) 1480d48b7f0SEric Paris fsnotify_destroy_vfsmount_mark(mark); 1495444e298SEric Paris else 1505444e298SEric Paris BUG(); 1514712e722SJan Kara /* 1524712e722SJan Kara * Note that we didn't update flags telling whether inode cares about 1534712e722SJan Kara * what's happening with children. We update these flags from 1544712e722SJan Kara * __fsnotify_parent() lazily when next event happens on one of our 1554712e722SJan Kara * children. 1564712e722SJan Kara */ 1575444e298SEric Paris 1585444e298SEric Paris list_del_init(&mark->g_list); 159d725e66cSLinus Torvalds 1605444e298SEric Paris spin_unlock(&mark->lock); 161d5a335b8SLino Sanfilippo 162e911d8afSJan Kara if (inode) 1636960b0d9SLino Sanfilippo iput(inode); 1644712e722SJan Kara 1654712e722SJan Kara atomic_dec(&group->num_marks); 1664712e722SJan Kara } 1674712e722SJan Kara 1684712e722SJan Kara /* 16935e48176SJan Kara * Prepare mark for freeing and add it to the list of marks prepared for 17035e48176SJan Kara * freeing. The actual freeing must happen after SRCU period ends and the 17135e48176SJan Kara * caller is responsible for this. 17235e48176SJan Kara * 17335e48176SJan Kara * The function returns true if the mark was added to the list of marks for 17435e48176SJan Kara * freeing. The function returns false if someone else has already called 17535e48176SJan Kara * __fsnotify_free_mark() for the mark. 1764712e722SJan Kara */ 17735e48176SJan Kara static bool __fsnotify_free_mark(struct fsnotify_mark *mark) 1784712e722SJan Kara { 1794712e722SJan Kara struct fsnotify_group *group = mark->group; 1804712e722SJan Kara 1814712e722SJan Kara spin_lock(&mark->lock); 1824712e722SJan Kara /* something else already called this function on this mark */ 1834712e722SJan Kara if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { 1844712e722SJan Kara spin_unlock(&mark->lock); 18535e48176SJan Kara return false; 1864712e722SJan Kara } 1874712e722SJan Kara mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 1884712e722SJan Kara spin_unlock(&mark->lock); 1895444e298SEric Paris 190d725e66cSLinus Torvalds /* 191d725e66cSLinus Torvalds * Some groups like to know that marks are being freed. This is a 192d725e66cSLinus Torvalds * callback to the group function to let it know that this mark 193d725e66cSLinus Torvalds * is being freed. 194d725e66cSLinus Torvalds */ 195d725e66cSLinus Torvalds if (group->ops->freeing_mark) 196d725e66cSLinus Torvalds group->ops->freeing_mark(mark, group); 19735e48176SJan Kara 19835e48176SJan Kara spin_lock(&destroy_lock); 19935e48176SJan Kara list_add(&mark->g_list, &destroy_list); 20035e48176SJan Kara spin_unlock(&destroy_lock); 20135e48176SJan Kara 20235e48176SJan Kara return true; 20335e48176SJan Kara } 20435e48176SJan Kara 20535e48176SJan Kara /* 20635e48176SJan Kara * Free fsnotify mark. The freeing is actually happening from a workqueue which 20735e48176SJan Kara * first waits for srcu period end. Caller must have a reference to the mark 20835e48176SJan Kara * or be protected by fsnotify_mark_srcu. 20935e48176SJan Kara */ 21035e48176SJan Kara void fsnotify_free_mark(struct fsnotify_mark *mark) 21135e48176SJan Kara { 21235e48176SJan Kara if (__fsnotify_free_mark(mark)) { 21335e48176SJan Kara queue_delayed_work(system_unbound_wq, &reaper_work, 21435e48176SJan Kara FSNOTIFY_REAPER_DELAY); 21535e48176SJan Kara } 216d5a335b8SLino Sanfilippo } 217d5a335b8SLino Sanfilippo 218d5a335b8SLino Sanfilippo void fsnotify_destroy_mark(struct fsnotify_mark *mark, 219d5a335b8SLino Sanfilippo struct fsnotify_group *group) 220d5a335b8SLino Sanfilippo { 2216960b0d9SLino Sanfilippo mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 2224712e722SJan Kara fsnotify_detach_mark(mark); 223d5a335b8SLino Sanfilippo mutex_unlock(&group->mark_mutex); 2244712e722SJan Kara fsnotify_free_mark(mark); 2255444e298SEric Paris } 2265444e298SEric Paris 2279dd813c1SJan Kara void fsnotify_connector_free(struct fsnotify_mark_connector **connp) 2289dd813c1SJan Kara { 2299dd813c1SJan Kara if (*connp) { 2309dd813c1SJan Kara kmem_cache_free(fsnotify_mark_connector_cachep, *connp); 2319dd813c1SJan Kara *connp = NULL; 2329dd813c1SJan Kara } 2339dd813c1SJan Kara } 2349dd813c1SJan Kara 23590b1e7a5SEric Paris void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask) 23690b1e7a5SEric Paris { 23790b1e7a5SEric Paris assert_spin_locked(&mark->lock); 23890b1e7a5SEric Paris 23990b1e7a5SEric Paris mark->mask = mask; 24090b1e7a5SEric Paris } 24190b1e7a5SEric Paris 24233af5e32SEric Paris void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask) 24333af5e32SEric Paris { 24433af5e32SEric Paris assert_spin_locked(&mark->lock); 24533af5e32SEric Paris 24633af5e32SEric Paris mark->ignored_mask = mask; 24733af5e32SEric Paris } 24890b1e7a5SEric Paris 2495444e298SEric Paris /* 2508edc6e16SJan Kara * Sorting function for lists of fsnotify marks. 2518edc6e16SJan Kara * 2528edc6e16SJan Kara * Fanotify supports different notification classes (reflected as priority of 2538edc6e16SJan Kara * notification group). Events shall be passed to notification groups in 2548edc6e16SJan Kara * decreasing priority order. To achieve this marks in notification lists for 2558edc6e16SJan Kara * inodes and vfsmounts are sorted so that priorities of corresponding groups 2568edc6e16SJan Kara * are descending. 2578edc6e16SJan Kara * 2588edc6e16SJan Kara * Furthermore correct handling of the ignore mask requires processing inode 2598edc6e16SJan Kara * and vfsmount marks of each group together. Using the group address as 2608edc6e16SJan Kara * further sort criterion provides a unique sorting order and thus we can 2618edc6e16SJan Kara * merge inode and vfsmount lists of marks in linear time and find groups 2628edc6e16SJan Kara * present in both lists. 2638edc6e16SJan Kara * 2648edc6e16SJan Kara * A return value of 1 signifies that b has priority over a. 2658edc6e16SJan Kara * A return value of 0 signifies that the two marks have to be handled together. 2668edc6e16SJan Kara * A return value of -1 signifies that a has priority over b. 2678edc6e16SJan Kara */ 2688edc6e16SJan Kara int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b) 2698edc6e16SJan Kara { 2708edc6e16SJan Kara if (a == b) 2718edc6e16SJan Kara return 0; 2728edc6e16SJan Kara if (!a) 2738edc6e16SJan Kara return 1; 2748edc6e16SJan Kara if (!b) 2758edc6e16SJan Kara return -1; 2768edc6e16SJan Kara if (a->priority < b->priority) 2778edc6e16SJan Kara return 1; 2788edc6e16SJan Kara if (a->priority > b->priority) 2798edc6e16SJan Kara return -1; 2808edc6e16SJan Kara if (a < b) 2818edc6e16SJan Kara return 1; 2828edc6e16SJan Kara return -1; 2838edc6e16SJan Kara } 2848edc6e16SJan Kara 2859dd813c1SJan Kara static int fsnotify_attach_connector_to_object( 28686ffe245SJan Kara struct fsnotify_mark_connector **connp, 287755b5bc6SJan Kara spinlock_t *lock, 28886ffe245SJan Kara struct inode *inode, 28986ffe245SJan Kara struct vfsmount *mnt) 2909dd813c1SJan Kara { 2919dd813c1SJan Kara struct fsnotify_mark_connector *conn; 2929dd813c1SJan Kara 293755b5bc6SJan Kara conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL); 2949dd813c1SJan Kara if (!conn) 2959dd813c1SJan Kara return -ENOMEM; 2969dd813c1SJan Kara INIT_HLIST_HEAD(&conn->list); 29786ffe245SJan Kara if (inode) { 29886ffe245SJan Kara conn->flags = FSNOTIFY_OBJ_TYPE_INODE; 29986ffe245SJan Kara conn->inode = inode; 30086ffe245SJan Kara } else { 30186ffe245SJan Kara conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT; 30286ffe245SJan Kara conn->mnt = mnt; 30386ffe245SJan Kara } 3049dd813c1SJan Kara /* 3059dd813c1SJan Kara * Make sure 'conn' initialization is visible. Matches 3069dd813c1SJan Kara * lockless_dereference() in fsnotify(). 3079dd813c1SJan Kara */ 3089dd813c1SJan Kara smp_wmb(); 309755b5bc6SJan Kara spin_lock(lock); 310755b5bc6SJan Kara if (!*connp) 3119dd813c1SJan Kara *connp = conn; 312755b5bc6SJan Kara else 313755b5bc6SJan Kara kmem_cache_free(fsnotify_mark_connector_cachep, conn); 314755b5bc6SJan Kara spin_unlock(lock); 3159dd813c1SJan Kara 3169dd813c1SJan Kara return 0; 3179dd813c1SJan Kara } 3189dd813c1SJan Kara 3199dd813c1SJan Kara /* 3209dd813c1SJan Kara * Add mark into proper place in given list of marks. These marks may be used 3219dd813c1SJan Kara * for the fsnotify backend to determine which event types should be delivered 3229dd813c1SJan Kara * to which group and for which inodes. These marks are ordered according to 3239dd813c1SJan Kara * priority, highest number first, and then by the group's location in memory. 3249dd813c1SJan Kara */ 325755b5bc6SJan Kara static int fsnotify_add_mark_list(struct fsnotify_mark *mark, 326755b5bc6SJan Kara struct inode *inode, struct vfsmount *mnt, 327755b5bc6SJan Kara int allow_dups) 3280809ab69SJan Kara { 3290809ab69SJan Kara struct fsnotify_mark *lmark, *last = NULL; 3309dd813c1SJan Kara struct fsnotify_mark_connector *conn; 331755b5bc6SJan Kara struct fsnotify_mark_connector **connp; 332755b5bc6SJan Kara spinlock_t *lock; 3330809ab69SJan Kara int cmp; 334755b5bc6SJan Kara int err = 0; 335755b5bc6SJan Kara 336755b5bc6SJan Kara if (WARN_ON(!inode && !mnt)) 337755b5bc6SJan Kara return -EINVAL; 338755b5bc6SJan Kara if (inode) { 339755b5bc6SJan Kara connp = &inode->i_fsnotify_marks; 340755b5bc6SJan Kara lock = &inode->i_lock; 341755b5bc6SJan Kara } else { 342755b5bc6SJan Kara connp = &real_mount(mnt)->mnt_fsnotify_marks; 343755b5bc6SJan Kara lock = &mnt->mnt_root->d_lock; 344755b5bc6SJan Kara } 3459dd813c1SJan Kara 3469dd813c1SJan Kara if (!*connp) { 347755b5bc6SJan Kara err = fsnotify_attach_connector_to_object(connp, lock, 348755b5bc6SJan Kara inode, mnt); 3499dd813c1SJan Kara if (err) 3509dd813c1SJan Kara return err; 3519dd813c1SJan Kara } 352755b5bc6SJan Kara spin_lock(&mark->lock); 353755b5bc6SJan Kara spin_lock(lock); 3549dd813c1SJan Kara conn = *connp; 3550809ab69SJan Kara 3560809ab69SJan Kara /* is mark the first mark? */ 3579dd813c1SJan Kara if (hlist_empty(&conn->list)) { 3589dd813c1SJan Kara hlist_add_head_rcu(&mark->obj_list, &conn->list); 359e911d8afSJan Kara if (inode) 360e911d8afSJan Kara __iget(inode); 36186ffe245SJan Kara goto added; 3620809ab69SJan Kara } 3630809ab69SJan Kara 3640809ab69SJan Kara /* should mark be in the middle of the current list? */ 3659dd813c1SJan Kara hlist_for_each_entry(lmark, &conn->list, obj_list) { 3660809ab69SJan Kara last = lmark; 3670809ab69SJan Kara 368755b5bc6SJan Kara if ((lmark->group == mark->group) && !allow_dups) { 369755b5bc6SJan Kara err = -EEXIST; 370755b5bc6SJan Kara goto out_err; 371755b5bc6SJan Kara } 3720809ab69SJan Kara 3730809ab69SJan Kara cmp = fsnotify_compare_groups(lmark->group, mark->group); 3740809ab69SJan Kara if (cmp >= 0) { 3750809ab69SJan Kara hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list); 37686ffe245SJan Kara goto added; 3770809ab69SJan Kara } 3780809ab69SJan Kara } 3790809ab69SJan Kara 3800809ab69SJan Kara BUG_ON(last == NULL); 3810809ab69SJan Kara /* mark should be the last entry. last is the current last entry */ 3820809ab69SJan Kara hlist_add_behind_rcu(&mark->obj_list, &last->obj_list); 38386ffe245SJan Kara added: 38486ffe245SJan Kara mark->connector = conn; 385755b5bc6SJan Kara out_err: 386755b5bc6SJan Kara spin_unlock(lock); 387755b5bc6SJan Kara spin_unlock(&mark->lock); 388755b5bc6SJan Kara return err; 3890809ab69SJan Kara } 3900809ab69SJan Kara 3918edc6e16SJan Kara /* 3925444e298SEric Paris * Attach an initialized mark to a given group and fs object. 3935444e298SEric Paris * These marks may be used for the fsnotify backend to determine which 3945444e298SEric Paris * event types should be delivered to which group. 3955444e298SEric Paris */ 396d5a335b8SLino Sanfilippo int fsnotify_add_mark_locked(struct fsnotify_mark *mark, 3975444e298SEric Paris struct fsnotify_group *group, struct inode *inode, 3985444e298SEric Paris struct vfsmount *mnt, int allow_dups) 3995444e298SEric Paris { 4005444e298SEric Paris int ret = 0; 4015444e298SEric Paris 4025444e298SEric Paris BUG_ON(inode && mnt); 4035444e298SEric Paris BUG_ON(!inode && !mnt); 404d5a335b8SLino Sanfilippo BUG_ON(!mutex_is_locked(&group->mark_mutex)); 4055444e298SEric Paris 4065444e298SEric Paris /* 4075444e298SEric Paris * LOCKING ORDER!!!! 408986ab098SLino Sanfilippo * group->mark_mutex 409104d06f0SLino Sanfilippo * mark->lock 4105444e298SEric Paris * inode->i_lock 4115444e298SEric Paris */ 412104d06f0SLino Sanfilippo spin_lock(&mark->lock); 4134712e722SJan Kara mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED; 414700307a2SEric Paris 41523e964c2SLino Sanfilippo fsnotify_get_group(group); 4165444e298SEric Paris mark->group = group; 4175444e298SEric Paris list_add(&mark->g_list, &group->marks_list); 4185444e298SEric Paris atomic_inc(&group->num_marks); 4195444e298SEric Paris fsnotify_get_mark(mark); /* for i_list and g_list */ 4205444e298SEric Paris spin_unlock(&mark->lock); 4215444e298SEric Paris 422755b5bc6SJan Kara ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups); 423755b5bc6SJan Kara if (ret) 424755b5bc6SJan Kara goto err; 425755b5bc6SJan Kara 4265444e298SEric Paris if (inode) 427755b5bc6SJan Kara fsnotify_recalc_inode_mask(inode); 428755b5bc6SJan Kara else 429755b5bc6SJan Kara fsnotify_recalc_vfsmount_mask(mnt); 4305444e298SEric Paris 4315444e298SEric Paris return ret; 4325444e298SEric Paris err: 433700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 4345444e298SEric Paris list_del_init(&mark->g_list); 43523e964c2SLino Sanfilippo fsnotify_put_group(group); 43675c1be48SEric Paris mark->group = NULL; 4375444e298SEric Paris atomic_dec(&group->num_marks); 4385444e298SEric Paris 4395444e298SEric Paris spin_unlock(&mark->lock); 4405444e298SEric Paris 44113d34ac6SJeff Layton spin_lock(&destroy_lock); 44213d34ac6SJeff Layton list_add(&mark->g_list, &destroy_list); 44313d34ac6SJeff Layton spin_unlock(&destroy_lock); 4440918f1c3SJeff Layton queue_delayed_work(system_unbound_wq, &reaper_work, 4450918f1c3SJeff Layton FSNOTIFY_REAPER_DELAY); 44613d34ac6SJeff Layton 4475444e298SEric Paris return ret; 4485444e298SEric Paris } 4495444e298SEric Paris 450d5a335b8SLino Sanfilippo int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group, 451d5a335b8SLino Sanfilippo struct inode *inode, struct vfsmount *mnt, int allow_dups) 452d5a335b8SLino Sanfilippo { 453d5a335b8SLino Sanfilippo int ret; 454d5a335b8SLino Sanfilippo mutex_lock(&group->mark_mutex); 455d5a335b8SLino Sanfilippo ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups); 456d5a335b8SLino Sanfilippo mutex_unlock(&group->mark_mutex); 457d5a335b8SLino Sanfilippo return ret; 458d5a335b8SLino Sanfilippo } 459d5a335b8SLino Sanfilippo 4605444e298SEric Paris /* 4610809ab69SJan Kara * Given a list of marks, find the mark associated with given group. If found 4620809ab69SJan Kara * take a reference to that mark and return it, else return NULL. 4630809ab69SJan Kara */ 4649dd813c1SJan Kara struct fsnotify_mark *fsnotify_find_mark(struct fsnotify_mark_connector *conn, 4650809ab69SJan Kara struct fsnotify_group *group) 4660809ab69SJan Kara { 4670809ab69SJan Kara struct fsnotify_mark *mark; 4680809ab69SJan Kara 4699dd813c1SJan Kara if (!conn) 4709dd813c1SJan Kara return NULL; 4719dd813c1SJan Kara 4729dd813c1SJan Kara hlist_for_each_entry(mark, &conn->list, obj_list) { 4730809ab69SJan Kara if (mark->group == group) { 4740809ab69SJan Kara fsnotify_get_mark(mark); 4750809ab69SJan Kara return mark; 4760809ab69SJan Kara } 4770809ab69SJan Kara } 4780809ab69SJan Kara return NULL; 4790809ab69SJan Kara } 4800809ab69SJan Kara 4810809ab69SJan Kara /* 482d725e66cSLinus Torvalds * clear any marks in a group in which mark->flags & flags is true 4835444e298SEric Paris */ 4844d92604cSEric Paris void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group, 4854d92604cSEric Paris unsigned int flags) 4865444e298SEric Paris { 4875444e298SEric Paris struct fsnotify_mark *lmark, *mark; 4888f2f3eb5SJan Kara LIST_HEAD(to_free); 4895444e298SEric Paris 4908f2f3eb5SJan Kara /* 4918f2f3eb5SJan Kara * We have to be really careful here. Anytime we drop mark_mutex, e.g. 4928f2f3eb5SJan Kara * fsnotify_clear_marks_by_inode() can come and free marks. Even in our 4938f2f3eb5SJan Kara * to_free list so we have to use mark_mutex even when accessing that 4948f2f3eb5SJan Kara * list. And freeing mark requires us to drop mark_mutex. So we can 4958f2f3eb5SJan Kara * reliably free only the first mark in the list. That's why we first 4968f2f3eb5SJan Kara * move marks to free to to_free list in one go and then free marks in 4978f2f3eb5SJan Kara * to_free list one by one. 4988f2f3eb5SJan Kara */ 4996960b0d9SLino Sanfilippo mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 5005444e298SEric Paris list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { 50186ffe245SJan Kara if (mark->connector->flags & flags) 5028f2f3eb5SJan Kara list_move(&mark->g_list, &to_free); 5034d92604cSEric Paris } 504986ab098SLino Sanfilippo mutex_unlock(&group->mark_mutex); 5058f2f3eb5SJan Kara 5068f2f3eb5SJan Kara while (1) { 5078f2f3eb5SJan Kara mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 5088f2f3eb5SJan Kara if (list_empty(&to_free)) { 5098f2f3eb5SJan Kara mutex_unlock(&group->mark_mutex); 5108f2f3eb5SJan Kara break; 5118f2f3eb5SJan Kara } 5128f2f3eb5SJan Kara mark = list_first_entry(&to_free, struct fsnotify_mark, g_list); 5138f2f3eb5SJan Kara fsnotify_get_mark(mark); 5144712e722SJan Kara fsnotify_detach_mark(mark); 5158f2f3eb5SJan Kara mutex_unlock(&group->mark_mutex); 5164712e722SJan Kara fsnotify_free_mark(mark); 5178f2f3eb5SJan Kara fsnotify_put_mark(mark); 5188f2f3eb5SJan Kara } 5195444e298SEric Paris } 5205444e298SEric Paris 5214d92604cSEric Paris /* 52235e48176SJan Kara * Given a group, prepare for freeing all the marks associated with that group. 52335e48176SJan Kara * The marks are attached to the list of marks prepared for destruction, the 52435e48176SJan Kara * caller is responsible for freeing marks in that list after SRCU period has 52535e48176SJan Kara * ended. 5264d92604cSEric Paris */ 52735e48176SJan Kara void fsnotify_detach_group_marks(struct fsnotify_group *group) 5284d92604cSEric Paris { 52935e48176SJan Kara struct fsnotify_mark *mark; 53035e48176SJan Kara 53135e48176SJan Kara while (1) { 53235e48176SJan Kara mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 53335e48176SJan Kara if (list_empty(&group->marks_list)) { 53435e48176SJan Kara mutex_unlock(&group->mark_mutex); 53535e48176SJan Kara break; 53635e48176SJan Kara } 53735e48176SJan Kara mark = list_first_entry(&group->marks_list, 53835e48176SJan Kara struct fsnotify_mark, g_list); 53935e48176SJan Kara fsnotify_get_mark(mark); 54035e48176SJan Kara fsnotify_detach_mark(mark); 54135e48176SJan Kara mutex_unlock(&group->mark_mutex); 54235e48176SJan Kara __fsnotify_free_mark(mark); 54335e48176SJan Kara fsnotify_put_mark(mark); 54435e48176SJan Kara } 5454d92604cSEric Paris } 5464d92604cSEric Paris 547*0810b4f9SJan Kara void fsnotify_destroy_marks(struct fsnotify_mark_connector *conn, 548*0810b4f9SJan Kara spinlock_t *lock) 549*0810b4f9SJan Kara { 550*0810b4f9SJan Kara struct fsnotify_mark *mark; 551*0810b4f9SJan Kara 552*0810b4f9SJan Kara if (!conn) 553*0810b4f9SJan Kara return; 554*0810b4f9SJan Kara 555*0810b4f9SJan Kara while (1) { 556*0810b4f9SJan Kara /* 557*0810b4f9SJan Kara * We have to be careful since we can race with e.g. 558*0810b4f9SJan Kara * fsnotify_clear_marks_by_group() and once we drop 'lock', 559*0810b4f9SJan Kara * mark can get removed from the obj_list and destroyed. But 560*0810b4f9SJan Kara * we are holding mark reference so mark cannot be freed and 561*0810b4f9SJan Kara * calling fsnotify_destroy_mark() more than once is fine. 562*0810b4f9SJan Kara */ 563*0810b4f9SJan Kara spin_lock(lock); 564*0810b4f9SJan Kara if (hlist_empty(&conn->list)) { 565*0810b4f9SJan Kara spin_unlock(lock); 566*0810b4f9SJan Kara break; 567*0810b4f9SJan Kara } 568*0810b4f9SJan Kara mark = hlist_entry(conn->list.first, struct fsnotify_mark, 569*0810b4f9SJan Kara obj_list); 570*0810b4f9SJan Kara /* 571*0810b4f9SJan Kara * We don't update i_fsnotify_mask / mnt_fsnotify_mask here 572*0810b4f9SJan Kara * since inode / mount is going away anyway. So just remove 573*0810b4f9SJan Kara * mark from the list. 574*0810b4f9SJan Kara */ 575*0810b4f9SJan Kara hlist_del_init_rcu(&mark->obj_list); 576*0810b4f9SJan Kara fsnotify_get_mark(mark); 577*0810b4f9SJan Kara spin_unlock(lock); 578*0810b4f9SJan Kara fsnotify_destroy_mark(mark, mark->group); 579*0810b4f9SJan Kara fsnotify_put_mark(mark); 580*0810b4f9SJan Kara } 581*0810b4f9SJan Kara } 582*0810b4f9SJan Kara 5835444e298SEric Paris /* 5845444e298SEric Paris * Nothing fancy, just initialize lists and locks and counters. 5855444e298SEric Paris */ 5865444e298SEric Paris void fsnotify_init_mark(struct fsnotify_mark *mark, 5875444e298SEric Paris void (*free_mark)(struct fsnotify_mark *mark)) 5885444e298SEric Paris { 589ba643f04SEric Paris memset(mark, 0, sizeof(*mark)); 5905444e298SEric Paris spin_lock_init(&mark->lock); 5915444e298SEric Paris atomic_set(&mark->refcnt, 1); 5925444e298SEric Paris mark->free_mark = free_mark; 5935444e298SEric Paris } 59413d34ac6SJeff Layton 59535e48176SJan Kara /* 59635e48176SJan Kara * Destroy all marks in destroy_list, waits for SRCU period to finish before 59735e48176SJan Kara * actually freeing marks. 59835e48176SJan Kara */ 59935e48176SJan Kara void fsnotify_mark_destroy_list(void) 60013d34ac6SJeff Layton { 60113d34ac6SJeff Layton struct fsnotify_mark *mark, *next; 60213d34ac6SJeff Layton struct list_head private_destroy_list; 60313d34ac6SJeff Layton 60413d34ac6SJeff Layton spin_lock(&destroy_lock); 60513d34ac6SJeff Layton /* exchange the list head */ 60613d34ac6SJeff Layton list_replace_init(&destroy_list, &private_destroy_list); 60713d34ac6SJeff Layton spin_unlock(&destroy_lock); 60813d34ac6SJeff Layton 60913d34ac6SJeff Layton synchronize_srcu(&fsnotify_mark_srcu); 61013d34ac6SJeff Layton 61113d34ac6SJeff Layton list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) { 61213d34ac6SJeff Layton list_del_init(&mark->g_list); 61313d34ac6SJeff Layton fsnotify_put_mark(mark); 61413d34ac6SJeff Layton } 61513d34ac6SJeff Layton } 61635e48176SJan Kara 61735e48176SJan Kara static void fsnotify_mark_destroy_workfn(struct work_struct *work) 61835e48176SJan Kara { 61935e48176SJan Kara fsnotify_mark_destroy_list(); 62035e48176SJan Kara } 621