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 2725985edcSLucas De Marchi * and the object itself is guaranteed 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> 8875c1be48SEric 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> 9375c1be48SEric Paris #include <linux/srcu.h> 945444e298SEric Paris 95*60063497SArun Sharma #include <linux/atomic.h> 965444e298SEric Paris 975444e298SEric Paris #include <linux/fsnotify_backend.h> 985444e298SEric Paris #include "fsnotify.h" 995444e298SEric Paris 10075c1be48SEric Paris struct srcu_struct fsnotify_mark_srcu; 10175c1be48SEric Paris static DEFINE_SPINLOCK(destroy_lock); 10275c1be48SEric Paris static LIST_HEAD(destroy_list); 10375c1be48SEric Paris static DECLARE_WAIT_QUEUE_HEAD(destroy_waitq); 10475c1be48SEric Paris 1055444e298SEric Paris void fsnotify_get_mark(struct fsnotify_mark *mark) 1065444e298SEric Paris { 1075444e298SEric Paris atomic_inc(&mark->refcnt); 1085444e298SEric Paris } 1095444e298SEric Paris 1105444e298SEric Paris void fsnotify_put_mark(struct fsnotify_mark *mark) 1115444e298SEric Paris { 1125444e298SEric Paris if (atomic_dec_and_test(&mark->refcnt)) 1135444e298SEric Paris mark->free_mark(mark); 1145444e298SEric Paris } 1155444e298SEric Paris 1165444e298SEric Paris /* 1175444e298SEric Paris * Any time a mark is getting freed we end up here. 1185444e298SEric Paris * The caller had better be holding a reference to this mark so we don't actually 1195444e298SEric Paris * do the final put under the mark->lock 1205444e298SEric Paris */ 1215444e298SEric Paris void fsnotify_destroy_mark(struct fsnotify_mark *mark) 1225444e298SEric Paris { 1235444e298SEric Paris struct fsnotify_group *group; 1240d48b7f0SEric Paris struct inode *inode = NULL; 1255444e298SEric Paris 1265444e298SEric Paris spin_lock(&mark->lock); 1275444e298SEric Paris 1285444e298SEric Paris group = mark->group; 1295444e298SEric Paris 130700307a2SEric Paris /* something else already called this function on this mark */ 131700307a2SEric Paris if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { 1325444e298SEric Paris spin_unlock(&mark->lock); 1335444e298SEric Paris return; 1345444e298SEric Paris } 1355444e298SEric Paris 136700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 137700307a2SEric Paris 1385444e298SEric Paris /* 1 from caller and 1 for being on i_list/g_list */ 1395444e298SEric Paris BUG_ON(atomic_read(&mark->refcnt) < 2); 1405444e298SEric Paris 1415444e298SEric Paris spin_lock(&group->mark_lock); 1425444e298SEric Paris 1430d48b7f0SEric Paris if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) { 1440d48b7f0SEric Paris inode = mark->i.inode; 145b31d397eSEric Paris fsnotify_destroy_inode_mark(mark); 1460d48b7f0SEric Paris } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT) 1470d48b7f0SEric Paris fsnotify_destroy_vfsmount_mark(mark); 1485444e298SEric Paris else 1495444e298SEric Paris BUG(); 1505444e298SEric Paris 1515444e298SEric Paris list_del_init(&mark->g_list); 1525444e298SEric Paris 1535444e298SEric Paris spin_unlock(&group->mark_lock); 1545444e298SEric Paris spin_unlock(&mark->lock); 1555444e298SEric Paris 15675c1be48SEric Paris spin_lock(&destroy_lock); 15775c1be48SEric Paris list_add(&mark->destroy_list, &destroy_list); 15875c1be48SEric Paris spin_unlock(&destroy_lock); 15975c1be48SEric Paris wake_up(&destroy_waitq); 16075c1be48SEric Paris 1615444e298SEric Paris /* 1625444e298SEric Paris * Some groups like to know that marks are being freed. This is a 1635444e298SEric Paris * callback to the group function to let it know that this mark 1645444e298SEric Paris * is being freed. 1655444e298SEric Paris */ 1665444e298SEric Paris if (group->ops->freeing_mark) 1675444e298SEric Paris group->ops->freeing_mark(mark, group); 1685444e298SEric Paris 1695444e298SEric Paris /* 1705444e298SEric Paris * __fsnotify_update_child_dentry_flags(inode); 1715444e298SEric Paris * 1725444e298SEric Paris * I really want to call that, but we can't, we have no idea if the inode 1735444e298SEric Paris * still exists the second we drop the mark->lock. 1745444e298SEric Paris * 1755444e298SEric Paris * The next time an event arrive to this inode from one of it's children 1765444e298SEric Paris * __fsnotify_parent will see that the inode doesn't care about it's 1775444e298SEric Paris * children and will update all of these flags then. So really this 1785444e298SEric Paris * is just a lazy update (and could be a perf win...) 1795444e298SEric Paris */ 1805444e298SEric Paris 18190b1e7a5SEric Paris if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) 1825444e298SEric Paris iput(inode); 1835444e298SEric Paris 1845444e298SEric Paris /* 1855444e298SEric Paris * it's possible that this group tried to destroy itself, but this 1865444e298SEric Paris * this mark was simultaneously being freed by inode. If that's the 1875444e298SEric Paris * case, we finish freeing the group here. 1885444e298SEric Paris */ 1895444e298SEric Paris if (unlikely(atomic_dec_and_test(&group->num_marks))) 1905444e298SEric Paris fsnotify_final_destroy_group(group); 1915444e298SEric Paris } 1925444e298SEric Paris 19390b1e7a5SEric Paris void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask) 19490b1e7a5SEric Paris { 19590b1e7a5SEric Paris assert_spin_locked(&mark->lock); 19690b1e7a5SEric Paris 19790b1e7a5SEric Paris mark->mask = mask; 19890b1e7a5SEric Paris 19990b1e7a5SEric Paris if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) 20090b1e7a5SEric Paris fsnotify_set_inode_mark_mask_locked(mark, mask); 20190b1e7a5SEric Paris } 20290b1e7a5SEric Paris 20333af5e32SEric Paris void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask) 20433af5e32SEric Paris { 20533af5e32SEric Paris assert_spin_locked(&mark->lock); 20633af5e32SEric Paris 20733af5e32SEric Paris mark->ignored_mask = mask; 20833af5e32SEric Paris } 20990b1e7a5SEric Paris 2105444e298SEric Paris /* 2115444e298SEric Paris * Attach an initialized mark to a given group and fs object. 2125444e298SEric Paris * These marks may be used for the fsnotify backend to determine which 2135444e298SEric Paris * event types should be delivered to which group. 2145444e298SEric Paris */ 2155444e298SEric Paris int fsnotify_add_mark(struct fsnotify_mark *mark, 2165444e298SEric Paris struct fsnotify_group *group, struct inode *inode, 2175444e298SEric Paris struct vfsmount *mnt, int allow_dups) 2185444e298SEric Paris { 2195444e298SEric Paris int ret = 0; 2205444e298SEric Paris 2215444e298SEric Paris BUG_ON(inode && mnt); 2225444e298SEric Paris BUG_ON(!inode && !mnt); 2235444e298SEric Paris 2245444e298SEric Paris /* 2255444e298SEric Paris * LOCKING ORDER!!!! 2265444e298SEric Paris * mark->lock 2275444e298SEric Paris * group->mark_lock 2285444e298SEric Paris * inode->i_lock 2295444e298SEric Paris */ 2305444e298SEric Paris spin_lock(&mark->lock); 2315444e298SEric Paris spin_lock(&group->mark_lock); 2325444e298SEric Paris 233700307a2SEric Paris mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE; 234700307a2SEric Paris 2355444e298SEric Paris mark->group = group; 2365444e298SEric Paris list_add(&mark->g_list, &group->marks_list); 2375444e298SEric Paris atomic_inc(&group->num_marks); 2385444e298SEric Paris fsnotify_get_mark(mark); /* for i_list and g_list */ 2395444e298SEric Paris 2405444e298SEric Paris if (inode) { 2415444e298SEric Paris ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups); 2425444e298SEric Paris if (ret) 2435444e298SEric Paris goto err; 2440d48b7f0SEric Paris } else if (mnt) { 2450d48b7f0SEric Paris ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups); 2460d48b7f0SEric Paris if (ret) 2470d48b7f0SEric Paris goto err; 2485444e298SEric Paris } else { 2495444e298SEric Paris BUG(); 2505444e298SEric Paris } 2515444e298SEric Paris 2525444e298SEric Paris spin_unlock(&group->mark_lock); 25390b1e7a5SEric Paris 25490b1e7a5SEric Paris /* this will pin the object if appropriate */ 25590b1e7a5SEric Paris fsnotify_set_mark_mask_locked(mark, mark->mask); 25690b1e7a5SEric Paris 2575444e298SEric Paris spin_unlock(&mark->lock); 2585444e298SEric Paris 2595444e298SEric Paris if (inode) 2605444e298SEric Paris __fsnotify_update_child_dentry_flags(inode); 2615444e298SEric Paris 2625444e298SEric Paris return ret; 2635444e298SEric Paris err: 264700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 2655444e298SEric Paris list_del_init(&mark->g_list); 26675c1be48SEric Paris mark->group = NULL; 2675444e298SEric Paris atomic_dec(&group->num_marks); 2685444e298SEric Paris 2695444e298SEric Paris spin_unlock(&group->mark_lock); 2705444e298SEric Paris spin_unlock(&mark->lock); 2715444e298SEric Paris 27275c1be48SEric Paris spin_lock(&destroy_lock); 27375c1be48SEric Paris list_add(&mark->destroy_list, &destroy_list); 27475c1be48SEric Paris spin_unlock(&destroy_lock); 27575c1be48SEric Paris wake_up(&destroy_waitq); 27675c1be48SEric Paris 2775444e298SEric Paris return ret; 2785444e298SEric Paris } 2795444e298SEric Paris 2805444e298SEric Paris /* 2814d92604cSEric Paris * clear any marks in a group in which mark->flags & flags is true 2825444e298SEric Paris */ 2834d92604cSEric Paris void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group, 2844d92604cSEric Paris unsigned int flags) 2855444e298SEric Paris { 2865444e298SEric Paris struct fsnotify_mark *lmark, *mark; 2875444e298SEric Paris LIST_HEAD(free_list); 2885444e298SEric Paris 2895444e298SEric Paris spin_lock(&group->mark_lock); 2905444e298SEric Paris list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { 2914d92604cSEric Paris if (mark->flags & flags) { 2925444e298SEric Paris list_add(&mark->free_g_list, &free_list); 2935444e298SEric Paris list_del_init(&mark->g_list); 2945444e298SEric Paris fsnotify_get_mark(mark); 2955444e298SEric Paris } 2964d92604cSEric Paris } 2975444e298SEric Paris spin_unlock(&group->mark_lock); 2985444e298SEric Paris 2995444e298SEric Paris list_for_each_entry_safe(mark, lmark, &free_list, free_g_list) { 3005444e298SEric Paris fsnotify_destroy_mark(mark); 3015444e298SEric Paris fsnotify_put_mark(mark); 3025444e298SEric Paris } 3035444e298SEric Paris } 3045444e298SEric Paris 3054d92604cSEric Paris /* 3064d92604cSEric Paris * Given a group, destroy all of the marks associated with that group. 3074d92604cSEric Paris */ 3084d92604cSEric Paris void fsnotify_clear_marks_by_group(struct fsnotify_group *group) 3094d92604cSEric Paris { 3104d92604cSEric Paris fsnotify_clear_marks_by_group_flags(group, (unsigned int)-1); 3114d92604cSEric Paris } 3124d92604cSEric Paris 3135444e298SEric Paris void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old) 3145444e298SEric Paris { 3155444e298SEric Paris assert_spin_locked(&old->lock); 3165444e298SEric Paris new->i.inode = old->i.inode; 3175444e298SEric Paris new->m.mnt = old->m.mnt; 3185444e298SEric Paris new->group = old->group; 3195444e298SEric Paris new->mask = old->mask; 3205444e298SEric Paris new->free_mark = old->free_mark; 3215444e298SEric Paris } 3225444e298SEric Paris 3235444e298SEric Paris /* 3245444e298SEric Paris * Nothing fancy, just initialize lists and locks and counters. 3255444e298SEric Paris */ 3265444e298SEric Paris void fsnotify_init_mark(struct fsnotify_mark *mark, 3275444e298SEric Paris void (*free_mark)(struct fsnotify_mark *mark)) 3285444e298SEric Paris { 329ba643f04SEric Paris memset(mark, 0, sizeof(*mark)); 3305444e298SEric Paris spin_lock_init(&mark->lock); 3315444e298SEric Paris atomic_set(&mark->refcnt, 1); 3325444e298SEric Paris mark->free_mark = free_mark; 3335444e298SEric Paris } 33475c1be48SEric Paris 33575c1be48SEric Paris static int fsnotify_mark_destroy(void *ignored) 33675c1be48SEric Paris { 33775c1be48SEric Paris struct fsnotify_mark *mark, *next; 33875c1be48SEric Paris LIST_HEAD(private_destroy_list); 33975c1be48SEric Paris 34075c1be48SEric Paris for (;;) { 34175c1be48SEric Paris spin_lock(&destroy_lock); 3428778abb9SAndreas Gruenbacher /* exchange the list head */ 3438778abb9SAndreas Gruenbacher list_replace_init(&destroy_list, &private_destroy_list); 34475c1be48SEric Paris spin_unlock(&destroy_lock); 34575c1be48SEric Paris 34675c1be48SEric Paris synchronize_srcu(&fsnotify_mark_srcu); 34775c1be48SEric Paris 34875c1be48SEric Paris list_for_each_entry_safe(mark, next, &private_destroy_list, destroy_list) { 34975c1be48SEric Paris list_del_init(&mark->destroy_list); 35075c1be48SEric Paris fsnotify_put_mark(mark); 35175c1be48SEric Paris } 35275c1be48SEric Paris 35375c1be48SEric Paris wait_event_interruptible(destroy_waitq, !list_empty(&destroy_list)); 35475c1be48SEric Paris } 35575c1be48SEric Paris 35675c1be48SEric Paris return 0; 35775c1be48SEric Paris } 35875c1be48SEric Paris 35975c1be48SEric Paris static int __init fsnotify_mark_init(void) 36075c1be48SEric Paris { 36175c1be48SEric Paris struct task_struct *thread; 36275c1be48SEric Paris 36375c1be48SEric Paris thread = kthread_run(fsnotify_mark_destroy, NULL, 36475c1be48SEric Paris "fsnotify_mark"); 36575c1be48SEric Paris if (IS_ERR(thread)) 36675c1be48SEric Paris panic("unable to start fsnotify mark destruction thread."); 36775c1be48SEric Paris 36875c1be48SEric Paris return 0; 36975c1be48SEric Paris } 37075c1be48SEric Paris device_initcall(fsnotify_mark_init); 371