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: 235444e298SEric Paris * The mark->refcnt tells how many "things" in the kernel currently are 245444e298SEric Paris * referencing this object. The object typically will live inside the kernel 255444e298SEric Paris * with a refcnt of 2, one for each list it is on (i_list, g_list). Any task 265444e298SEric Paris * which can find this object holding the appropriete locks, can take a reference 275444e298SEric Paris * and the object itself is guarenteed to survive until the reference is dropped. 285444e298SEric Paris * 295444e298SEric Paris * LOCKING: 305444e298SEric Paris * There are 3 spinlocks involved with fsnotify inode marks and they MUST 315444e298SEric Paris * be taken in order as follows: 325444e298SEric Paris * 335444e298SEric Paris * mark->lock 345444e298SEric Paris * group->mark_lock 355444e298SEric Paris * inode->i_lock 365444e298SEric Paris * 375444e298SEric Paris * mark->lock protects 2 things, mark->group and mark->inode. You must hold 385444e298SEric Paris * that lock to dereference either of these things (they could be NULL even with 395444e298SEric Paris * the lock) 405444e298SEric Paris * 415444e298SEric Paris * group->mark_lock protects the marks_list anchored inside a given group 425444e298SEric Paris * and each mark is hooked via the g_list. It also sorta protects the 435444e298SEric Paris * free_g_list, which when used is anchored by a private list on the stack of the 445444e298SEric Paris * task which held the group->mark_lock. 455444e298SEric Paris * 465444e298SEric Paris * inode->i_lock protects the i_fsnotify_marks list anchored inside a 475444e298SEric Paris * given inode and each mark is hooked via the i_list. (and sorta the 485444e298SEric Paris * free_i_list) 495444e298SEric Paris * 505444e298SEric Paris * 515444e298SEric Paris * LIFETIME: 525444e298SEric Paris * Inode marks survive between when they are added to an inode and when their 535444e298SEric Paris * refcnt==0. 545444e298SEric Paris * 555444e298SEric Paris * The inode mark can be cleared for a number of different reasons including: 565444e298SEric Paris * - The inode is unlinked for the last time. (fsnotify_inode_remove) 575444e298SEric Paris * - The inode is being evicted from cache. (fsnotify_inode_delete) 585444e298SEric Paris * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes) 595444e298SEric Paris * - Something explicitly requests that it be removed. (fsnotify_destroy_mark) 605444e298SEric Paris * - The fsnotify_group associated with the mark is going away and all such marks 615444e298SEric Paris * need to be cleaned up. (fsnotify_clear_marks_by_group) 625444e298SEric Paris * 635444e298SEric Paris * Worst case we are given an inode and need to clean up all the marks on that 645444e298SEric Paris * inode. We take i_lock and walk the i_fsnotify_marks safely. For each 655444e298SEric Paris * mark on the list we take a reference (so the mark can't disappear under us). 665444e298SEric Paris * We remove that mark form the inode's list of marks and we add this mark to a 675444e298SEric Paris * private list anchored on the stack using i_free_list; At this point we no 685444e298SEric Paris * longer fear anything finding the mark using the inode's list of marks. 695444e298SEric Paris * 705444e298SEric Paris * We can safely and locklessly run the private list on the stack of everything 715444e298SEric Paris * we just unattached from the original inode. For each mark on the private list 725444e298SEric Paris * we grab the mark-> and can thus dereference mark->group and mark->inode. If 735444e298SEric Paris * we see the group and inode are not NULL we take those locks. Now holding all 745444e298SEric Paris * 3 locks we can completely remove the mark from other tasks finding it in the 755444e298SEric Paris * future. Remember, 10 things might already be referencing this mark, but they 765444e298SEric Paris * better be holding a ref. We drop our reference we took before we unhooked it 775444e298SEric Paris * from the inode. When the ref hits 0 we can free the mark. 785444e298SEric Paris * 795444e298SEric Paris * Very similarly for freeing by group, except we use free_g_list. 805444e298SEric Paris * 815444e298SEric Paris * This has the very interesting property of being able to run concurrently with 825444e298SEric Paris * any (or all) other directions. 835444e298SEric Paris */ 845444e298SEric Paris 855444e298SEric Paris #include <linux/fs.h> 865444e298SEric Paris #include <linux/init.h> 875444e298SEric Paris #include <linux/kernel.h> 88*75c1be48SEric Paris #include <linux/kthread.h> 895444e298SEric Paris #include <linux/module.h> 905444e298SEric Paris #include <linux/mutex.h> 915444e298SEric Paris #include <linux/slab.h> 925444e298SEric Paris #include <linux/spinlock.h> 93*75c1be48SEric Paris #include <linux/srcu.h> 945444e298SEric Paris #include <linux/writeback.h> /* for inode_lock */ 955444e298SEric Paris 965444e298SEric Paris #include <asm/atomic.h> 975444e298SEric Paris 985444e298SEric Paris #include <linux/fsnotify_backend.h> 995444e298SEric Paris #include "fsnotify.h" 1005444e298SEric Paris 101*75c1be48SEric Paris struct srcu_struct fsnotify_mark_srcu; 102*75c1be48SEric Paris static DEFINE_SPINLOCK(destroy_lock); 103*75c1be48SEric Paris static LIST_HEAD(destroy_list); 104*75c1be48SEric Paris static DECLARE_WAIT_QUEUE_HEAD(destroy_waitq); 105*75c1be48SEric Paris 1065444e298SEric Paris void fsnotify_get_mark(struct fsnotify_mark *mark) 1075444e298SEric Paris { 1085444e298SEric Paris atomic_inc(&mark->refcnt); 1095444e298SEric Paris } 1105444e298SEric Paris 1115444e298SEric Paris void fsnotify_put_mark(struct fsnotify_mark *mark) 1125444e298SEric Paris { 1135444e298SEric Paris if (atomic_dec_and_test(&mark->refcnt)) 1145444e298SEric Paris mark->free_mark(mark); 1155444e298SEric Paris } 1165444e298SEric Paris 1175444e298SEric Paris /* 1185444e298SEric Paris * Any time a mark is getting freed we end up here. 1195444e298SEric Paris * The caller had better be holding a reference to this mark so we don't actually 1205444e298SEric Paris * do the final put under the mark->lock 1215444e298SEric Paris */ 1225444e298SEric Paris void fsnotify_destroy_mark(struct fsnotify_mark *mark) 1235444e298SEric Paris { 1245444e298SEric Paris struct fsnotify_group *group; 1250d48b7f0SEric Paris struct inode *inode = NULL; 1265444e298SEric Paris 1275444e298SEric Paris spin_lock(&mark->lock); 1285444e298SEric Paris 1295444e298SEric Paris group = mark->group; 1305444e298SEric Paris 131700307a2SEric Paris /* something else already called this function on this mark */ 132700307a2SEric Paris if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { 1335444e298SEric Paris spin_unlock(&mark->lock); 1345444e298SEric Paris return; 1355444e298SEric Paris } 1365444e298SEric Paris 137700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 138700307a2SEric Paris 1395444e298SEric Paris /* 1 from caller and 1 for being on i_list/g_list */ 1405444e298SEric Paris BUG_ON(atomic_read(&mark->refcnt) < 2); 1415444e298SEric Paris 1425444e298SEric Paris spin_lock(&group->mark_lock); 1435444e298SEric Paris 1440d48b7f0SEric Paris if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) { 1450d48b7f0SEric Paris inode = mark->i.inode; 146b31d397eSEric Paris fsnotify_destroy_inode_mark(mark); 1470d48b7f0SEric Paris } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT) 1480d48b7f0SEric Paris fsnotify_destroy_vfsmount_mark(mark); 1495444e298SEric Paris else 1505444e298SEric Paris BUG(); 1515444e298SEric Paris 1525444e298SEric Paris list_del_init(&mark->g_list); 1535444e298SEric Paris 1545444e298SEric Paris spin_unlock(&group->mark_lock); 1555444e298SEric Paris spin_unlock(&mark->lock); 1565444e298SEric Paris 157*75c1be48SEric Paris spin_lock(&destroy_lock); 158*75c1be48SEric Paris list_add(&mark->destroy_list, &destroy_list); 159*75c1be48SEric Paris spin_unlock(&destroy_lock); 160*75c1be48SEric Paris wake_up(&destroy_waitq); 161*75c1be48SEric Paris 1625444e298SEric Paris /* 1635444e298SEric Paris * Some groups like to know that marks are being freed. This is a 1645444e298SEric Paris * callback to the group function to let it know that this mark 1655444e298SEric Paris * is being freed. 1665444e298SEric Paris */ 1675444e298SEric Paris if (group->ops->freeing_mark) 1685444e298SEric Paris group->ops->freeing_mark(mark, group); 1695444e298SEric Paris 1705444e298SEric Paris /* 1715444e298SEric Paris * __fsnotify_update_child_dentry_flags(inode); 1725444e298SEric Paris * 1735444e298SEric Paris * I really want to call that, but we can't, we have no idea if the inode 1745444e298SEric Paris * still exists the second we drop the mark->lock. 1755444e298SEric Paris * 1765444e298SEric Paris * The next time an event arrive to this inode from one of it's children 1775444e298SEric Paris * __fsnotify_parent will see that the inode doesn't care about it's 1785444e298SEric Paris * children and will update all of these flags then. So really this 1795444e298SEric Paris * is just a lazy update (and could be a perf win...) 1805444e298SEric Paris */ 1815444e298SEric Paris 18290b1e7a5SEric Paris if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) 1835444e298SEric Paris iput(inode); 1845444e298SEric Paris 1855444e298SEric Paris /* 1865444e298SEric Paris * it's possible that this group tried to destroy itself, but this 1875444e298SEric Paris * this mark was simultaneously being freed by inode. If that's the 1885444e298SEric Paris * case, we finish freeing the group here. 1895444e298SEric Paris */ 1905444e298SEric Paris if (unlikely(atomic_dec_and_test(&group->num_marks))) 1915444e298SEric Paris fsnotify_final_destroy_group(group); 1925444e298SEric Paris } 1935444e298SEric Paris 19490b1e7a5SEric Paris void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask) 19590b1e7a5SEric Paris { 19690b1e7a5SEric Paris assert_spin_locked(&mark->lock); 19790b1e7a5SEric Paris 19890b1e7a5SEric Paris mark->mask = mask; 19990b1e7a5SEric Paris 20090b1e7a5SEric Paris if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) 20190b1e7a5SEric Paris fsnotify_set_inode_mark_mask_locked(mark, mask); 20290b1e7a5SEric Paris } 20390b1e7a5SEric Paris 20433af5e32SEric Paris void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask) 20533af5e32SEric Paris { 20633af5e32SEric Paris assert_spin_locked(&mark->lock); 20733af5e32SEric Paris 20833af5e32SEric Paris mark->ignored_mask = mask; 20933af5e32SEric Paris } 21090b1e7a5SEric Paris 2115444e298SEric Paris /* 2125444e298SEric Paris * Attach an initialized mark to a given group and fs object. 2135444e298SEric Paris * These marks may be used for the fsnotify backend to determine which 2145444e298SEric Paris * event types should be delivered to which group. 2155444e298SEric Paris */ 2165444e298SEric Paris int fsnotify_add_mark(struct fsnotify_mark *mark, 2175444e298SEric Paris struct fsnotify_group *group, struct inode *inode, 2185444e298SEric Paris struct vfsmount *mnt, int allow_dups) 2195444e298SEric Paris { 2205444e298SEric Paris int ret = 0; 2215444e298SEric Paris 2225444e298SEric Paris BUG_ON(inode && mnt); 2235444e298SEric Paris BUG_ON(!inode && !mnt); 2245444e298SEric Paris 2255444e298SEric Paris /* 2265444e298SEric Paris * if this group isn't being testing for inode type events we need 2275444e298SEric Paris * to start testing 2285444e298SEric Paris */ 2295444e298SEric Paris if (inode && unlikely(list_empty(&group->inode_group_list))) 2305444e298SEric Paris fsnotify_add_inode_group(group); 2315444e298SEric Paris else if (mnt && unlikely(list_empty(&group->vfsmount_group_list))) 2325444e298SEric Paris fsnotify_add_vfsmount_group(group); 2335444e298SEric Paris 2345444e298SEric Paris /* 2355444e298SEric Paris * LOCKING ORDER!!!! 2365444e298SEric Paris * mark->lock 2375444e298SEric Paris * group->mark_lock 2385444e298SEric Paris * inode->i_lock 2395444e298SEric Paris */ 2405444e298SEric Paris spin_lock(&mark->lock); 2415444e298SEric Paris spin_lock(&group->mark_lock); 2425444e298SEric Paris 243700307a2SEric Paris mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE; 244700307a2SEric Paris 2455444e298SEric Paris mark->group = group; 2465444e298SEric Paris list_add(&mark->g_list, &group->marks_list); 2475444e298SEric Paris atomic_inc(&group->num_marks); 2485444e298SEric Paris fsnotify_get_mark(mark); /* for i_list and g_list */ 2495444e298SEric Paris 2505444e298SEric Paris if (inode) { 2515444e298SEric Paris ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups); 2525444e298SEric Paris if (ret) 2535444e298SEric Paris goto err; 2540d48b7f0SEric Paris } else if (mnt) { 2550d48b7f0SEric Paris ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups); 2560d48b7f0SEric Paris if (ret) 2570d48b7f0SEric Paris goto err; 2585444e298SEric Paris } else { 2595444e298SEric Paris BUG(); 2605444e298SEric Paris } 2615444e298SEric Paris 2625444e298SEric Paris spin_unlock(&group->mark_lock); 26390b1e7a5SEric Paris 26490b1e7a5SEric Paris /* this will pin the object if appropriate */ 26590b1e7a5SEric Paris fsnotify_set_mark_mask_locked(mark, mark->mask); 26690b1e7a5SEric Paris 2675444e298SEric Paris spin_unlock(&mark->lock); 2685444e298SEric Paris 2695444e298SEric Paris if (inode) 2705444e298SEric Paris __fsnotify_update_child_dentry_flags(inode); 2715444e298SEric Paris 2725444e298SEric Paris return ret; 2735444e298SEric Paris err: 274700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 2755444e298SEric Paris list_del_init(&mark->g_list); 276*75c1be48SEric Paris mark->group = NULL; 2775444e298SEric Paris atomic_dec(&group->num_marks); 2785444e298SEric Paris 2795444e298SEric Paris spin_unlock(&group->mark_lock); 2805444e298SEric Paris spin_unlock(&mark->lock); 2815444e298SEric Paris 282*75c1be48SEric Paris spin_lock(&destroy_lock); 283*75c1be48SEric Paris list_add(&mark->destroy_list, &destroy_list); 284*75c1be48SEric Paris spin_unlock(&destroy_lock); 285*75c1be48SEric Paris wake_up(&destroy_waitq); 286*75c1be48SEric Paris 2875444e298SEric Paris return ret; 2885444e298SEric Paris } 2895444e298SEric Paris 2905444e298SEric Paris /* 2914d92604cSEric Paris * clear any marks in a group in which mark->flags & flags is true 2925444e298SEric Paris */ 2934d92604cSEric Paris void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group, 2944d92604cSEric Paris unsigned int flags) 2955444e298SEric Paris { 2965444e298SEric Paris struct fsnotify_mark *lmark, *mark; 2975444e298SEric Paris LIST_HEAD(free_list); 2985444e298SEric Paris 2995444e298SEric Paris spin_lock(&group->mark_lock); 3005444e298SEric Paris list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { 3014d92604cSEric Paris if (mark->flags & flags) { 3025444e298SEric Paris list_add(&mark->free_g_list, &free_list); 3035444e298SEric Paris list_del_init(&mark->g_list); 3045444e298SEric Paris fsnotify_get_mark(mark); 3055444e298SEric Paris } 3064d92604cSEric Paris } 3075444e298SEric Paris spin_unlock(&group->mark_lock); 3085444e298SEric Paris 3095444e298SEric Paris list_for_each_entry_safe(mark, lmark, &free_list, free_g_list) { 3105444e298SEric Paris fsnotify_destroy_mark(mark); 3115444e298SEric Paris fsnotify_put_mark(mark); 3125444e298SEric Paris } 3135444e298SEric Paris } 3145444e298SEric Paris 3154d92604cSEric Paris /* 3164d92604cSEric Paris * Given a group, destroy all of the marks associated with that group. 3174d92604cSEric Paris */ 3184d92604cSEric Paris void fsnotify_clear_marks_by_group(struct fsnotify_group *group) 3194d92604cSEric Paris { 3204d92604cSEric Paris fsnotify_clear_marks_by_group_flags(group, (unsigned int)-1); 3214d92604cSEric Paris } 3224d92604cSEric Paris 3235444e298SEric Paris void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old) 3245444e298SEric Paris { 3255444e298SEric Paris assert_spin_locked(&old->lock); 3265444e298SEric Paris new->i.inode = old->i.inode; 3275444e298SEric Paris new->m.mnt = old->m.mnt; 3285444e298SEric Paris new->group = old->group; 3295444e298SEric Paris new->mask = old->mask; 3305444e298SEric Paris new->free_mark = old->free_mark; 3315444e298SEric Paris } 3325444e298SEric Paris 3335444e298SEric Paris /* 3345444e298SEric Paris * Nothing fancy, just initialize lists and locks and counters. 3355444e298SEric Paris */ 3365444e298SEric Paris void fsnotify_init_mark(struct fsnotify_mark *mark, 3375444e298SEric Paris void (*free_mark)(struct fsnotify_mark *mark)) 3385444e298SEric Paris { 339ba643f04SEric Paris memset(mark, 0, sizeof(*mark)); 3405444e298SEric Paris spin_lock_init(&mark->lock); 3415444e298SEric Paris atomic_set(&mark->refcnt, 1); 3425444e298SEric Paris mark->free_mark = free_mark; 3435444e298SEric Paris } 344*75c1be48SEric Paris 345*75c1be48SEric Paris static int fsnotify_mark_destroy(void *ignored) 346*75c1be48SEric Paris { 347*75c1be48SEric Paris struct fsnotify_mark *mark, *next; 348*75c1be48SEric Paris LIST_HEAD(private_destroy_list); 349*75c1be48SEric Paris 350*75c1be48SEric Paris for (;;) { 351*75c1be48SEric Paris spin_lock(&destroy_lock); 352*75c1be48SEric Paris list_for_each_entry_safe(mark, next, &destroy_list, destroy_list) { 353*75c1be48SEric Paris list_del(&mark->destroy_list); 354*75c1be48SEric Paris list_add(&mark->destroy_list, &private_destroy_list); 355*75c1be48SEric Paris } 356*75c1be48SEric Paris spin_unlock(&destroy_lock); 357*75c1be48SEric Paris 358*75c1be48SEric Paris synchronize_srcu(&fsnotify_mark_srcu); 359*75c1be48SEric Paris 360*75c1be48SEric Paris list_for_each_entry_safe(mark, next, &private_destroy_list, destroy_list) { 361*75c1be48SEric Paris list_del_init(&mark->destroy_list); 362*75c1be48SEric Paris fsnotify_put_mark(mark); 363*75c1be48SEric Paris } 364*75c1be48SEric Paris 365*75c1be48SEric Paris wait_event_interruptible(destroy_waitq, !list_empty(&destroy_list)); 366*75c1be48SEric Paris } 367*75c1be48SEric Paris 368*75c1be48SEric Paris return 0; 369*75c1be48SEric Paris } 370*75c1be48SEric Paris 371*75c1be48SEric Paris static int __init fsnotify_mark_init(void) 372*75c1be48SEric Paris { 373*75c1be48SEric Paris struct task_struct *thread; 374*75c1be48SEric Paris 375*75c1be48SEric Paris thread = kthread_run(fsnotify_mark_destroy, NULL, 376*75c1be48SEric Paris "fsnotify_mark"); 377*75c1be48SEric Paris if (IS_ERR(thread)) 378*75c1be48SEric Paris panic("unable to start fsnotify mark destruction thread."); 379*75c1be48SEric Paris 380*75c1be48SEric Paris return 0; 381*75c1be48SEric Paris } 382*75c1be48SEric Paris device_initcall(fsnotify_mark_init); 383