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> 885444e298SEric Paris #include <linux/module.h> 895444e298SEric Paris #include <linux/mutex.h> 905444e298SEric Paris #include <linux/slab.h> 915444e298SEric Paris #include <linux/spinlock.h> 925444e298SEric Paris #include <linux/writeback.h> /* for inode_lock */ 935444e298SEric Paris 945444e298SEric Paris #include <asm/atomic.h> 955444e298SEric Paris 965444e298SEric Paris #include <linux/fsnotify_backend.h> 975444e298SEric Paris #include "fsnotify.h" 985444e298SEric Paris 995444e298SEric Paris void fsnotify_get_mark(struct fsnotify_mark *mark) 1005444e298SEric Paris { 1015444e298SEric Paris atomic_inc(&mark->refcnt); 1025444e298SEric Paris } 1035444e298SEric Paris 1045444e298SEric Paris void fsnotify_put_mark(struct fsnotify_mark *mark) 1055444e298SEric Paris { 1065444e298SEric Paris if (atomic_dec_and_test(&mark->refcnt)) 1075444e298SEric Paris mark->free_mark(mark); 1085444e298SEric Paris } 1095444e298SEric Paris 1105444e298SEric Paris /* 1115444e298SEric Paris * Any time a mark is getting freed we end up here. 1125444e298SEric Paris * The caller had better be holding a reference to this mark so we don't actually 1135444e298SEric Paris * do the final put under the mark->lock 1145444e298SEric Paris */ 1155444e298SEric Paris void fsnotify_destroy_mark(struct fsnotify_mark *mark) 1165444e298SEric Paris { 1175444e298SEric Paris struct fsnotify_group *group; 1180d48b7f0SEric Paris struct inode *inode = NULL; 1195444e298SEric Paris 1205444e298SEric Paris spin_lock(&mark->lock); 1215444e298SEric Paris 1225444e298SEric Paris group = mark->group; 1235444e298SEric Paris 124*700307a2SEric Paris /* something else already called this function on this mark */ 125*700307a2SEric Paris if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { 1265444e298SEric Paris spin_unlock(&mark->lock); 1275444e298SEric Paris return; 1285444e298SEric Paris } 1295444e298SEric Paris 130*700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 131*700307a2SEric Paris 1325444e298SEric Paris /* 1 from caller and 1 for being on i_list/g_list */ 1335444e298SEric Paris BUG_ON(atomic_read(&mark->refcnt) < 2); 1345444e298SEric Paris 1355444e298SEric Paris spin_lock(&group->mark_lock); 1365444e298SEric Paris 1370d48b7f0SEric Paris if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) { 1380d48b7f0SEric Paris inode = mark->i.inode; 139b31d397eSEric Paris fsnotify_destroy_inode_mark(mark); 1400d48b7f0SEric Paris } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT) 1410d48b7f0SEric Paris fsnotify_destroy_vfsmount_mark(mark); 1425444e298SEric Paris else 1435444e298SEric Paris BUG(); 1445444e298SEric Paris 1455444e298SEric Paris list_del_init(&mark->g_list); 1465444e298SEric Paris 1475444e298SEric Paris fsnotify_put_mark(mark); /* for i_list and g_list */ 1485444e298SEric Paris 1495444e298SEric Paris spin_unlock(&group->mark_lock); 1505444e298SEric Paris spin_unlock(&mark->lock); 1515444e298SEric Paris 1525444e298SEric Paris /* 1535444e298SEric Paris * Some groups like to know that marks are being freed. This is a 1545444e298SEric Paris * callback to the group function to let it know that this mark 1555444e298SEric Paris * is being freed. 1565444e298SEric Paris */ 1575444e298SEric Paris if (group->ops->freeing_mark) 1585444e298SEric Paris group->ops->freeing_mark(mark, group); 1595444e298SEric Paris 1605444e298SEric Paris /* 1615444e298SEric Paris * __fsnotify_update_child_dentry_flags(inode); 1625444e298SEric Paris * 1635444e298SEric Paris * I really want to call that, but we can't, we have no idea if the inode 1645444e298SEric Paris * still exists the second we drop the mark->lock. 1655444e298SEric Paris * 1665444e298SEric Paris * The next time an event arrive to this inode from one of it's children 1675444e298SEric Paris * __fsnotify_parent will see that the inode doesn't care about it's 1685444e298SEric Paris * children and will update all of these flags then. So really this 1695444e298SEric Paris * is just a lazy update (and could be a perf win...) 1705444e298SEric Paris */ 1715444e298SEric Paris 17290b1e7a5SEric Paris if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) 1735444e298SEric Paris iput(inode); 1745444e298SEric Paris 1755444e298SEric Paris /* 1765444e298SEric Paris * it's possible that this group tried to destroy itself, but this 1775444e298SEric Paris * this mark was simultaneously being freed by inode. If that's the 1785444e298SEric Paris * case, we finish freeing the group here. 1795444e298SEric Paris */ 1805444e298SEric Paris if (unlikely(atomic_dec_and_test(&group->num_marks))) 1815444e298SEric Paris fsnotify_final_destroy_group(group); 1825444e298SEric Paris } 1835444e298SEric Paris 18490b1e7a5SEric Paris void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask) 18590b1e7a5SEric Paris { 18690b1e7a5SEric Paris assert_spin_locked(&mark->lock); 18790b1e7a5SEric Paris 18890b1e7a5SEric Paris mark->mask = mask; 18990b1e7a5SEric Paris 19090b1e7a5SEric Paris if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) 19190b1e7a5SEric Paris fsnotify_set_inode_mark_mask_locked(mark, mask); 19290b1e7a5SEric Paris } 19390b1e7a5SEric Paris 19433af5e32SEric Paris void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask) 19533af5e32SEric Paris { 19633af5e32SEric Paris assert_spin_locked(&mark->lock); 19733af5e32SEric Paris 19833af5e32SEric Paris mark->ignored_mask = mask; 19933af5e32SEric Paris } 20090b1e7a5SEric Paris 2015444e298SEric Paris /* 2025444e298SEric Paris * Attach an initialized mark to a given group and fs object. 2035444e298SEric Paris * These marks may be used for the fsnotify backend to determine which 2045444e298SEric Paris * event types should be delivered to which group. 2055444e298SEric Paris */ 2065444e298SEric Paris int fsnotify_add_mark(struct fsnotify_mark *mark, 2075444e298SEric Paris struct fsnotify_group *group, struct inode *inode, 2085444e298SEric Paris struct vfsmount *mnt, int allow_dups) 2095444e298SEric Paris { 2105444e298SEric Paris int ret = 0; 2115444e298SEric Paris 2125444e298SEric Paris BUG_ON(inode && mnt); 2135444e298SEric Paris BUG_ON(!inode && !mnt); 2145444e298SEric Paris 2155444e298SEric Paris /* 2165444e298SEric Paris * if this group isn't being testing for inode type events we need 2175444e298SEric Paris * to start testing 2185444e298SEric Paris */ 2195444e298SEric Paris if (inode && unlikely(list_empty(&group->inode_group_list))) 2205444e298SEric Paris fsnotify_add_inode_group(group); 2215444e298SEric Paris else if (mnt && unlikely(list_empty(&group->vfsmount_group_list))) 2225444e298SEric Paris fsnotify_add_vfsmount_group(group); 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 233*700307a2SEric Paris mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE; 234*700307a2SEric 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: 264*700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 2655444e298SEric Paris list_del_init(&mark->g_list); 2665444e298SEric Paris atomic_dec(&group->num_marks); 2675444e298SEric Paris fsnotify_put_mark(mark); 2685444e298SEric Paris 2695444e298SEric Paris spin_unlock(&group->mark_lock); 2705444e298SEric Paris spin_unlock(&mark->lock); 2715444e298SEric Paris 2725444e298SEric Paris return ret; 2735444e298SEric Paris } 2745444e298SEric Paris 2755444e298SEric Paris /* 2764d92604cSEric Paris * clear any marks in a group in which mark->flags & flags is true 2775444e298SEric Paris */ 2784d92604cSEric Paris void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group, 2794d92604cSEric Paris unsigned int flags) 2805444e298SEric Paris { 2815444e298SEric Paris struct fsnotify_mark *lmark, *mark; 2825444e298SEric Paris LIST_HEAD(free_list); 2835444e298SEric Paris 2845444e298SEric Paris spin_lock(&group->mark_lock); 2855444e298SEric Paris list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { 2864d92604cSEric Paris if (mark->flags & flags) { 2875444e298SEric Paris list_add(&mark->free_g_list, &free_list); 2885444e298SEric Paris list_del_init(&mark->g_list); 2895444e298SEric Paris fsnotify_get_mark(mark); 2905444e298SEric Paris } 2914d92604cSEric Paris } 2925444e298SEric Paris spin_unlock(&group->mark_lock); 2935444e298SEric Paris 2945444e298SEric Paris list_for_each_entry_safe(mark, lmark, &free_list, free_g_list) { 2955444e298SEric Paris fsnotify_destroy_mark(mark); 2965444e298SEric Paris fsnotify_put_mark(mark); 2975444e298SEric Paris } 2985444e298SEric Paris } 2995444e298SEric Paris 3004d92604cSEric Paris /* 3014d92604cSEric Paris * Given a group, destroy all of the marks associated with that group. 3024d92604cSEric Paris */ 3034d92604cSEric Paris void fsnotify_clear_marks_by_group(struct fsnotify_group *group) 3044d92604cSEric Paris { 3054d92604cSEric Paris fsnotify_clear_marks_by_group_flags(group, (unsigned int)-1); 3064d92604cSEric Paris } 3074d92604cSEric Paris 3085444e298SEric Paris void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old) 3095444e298SEric Paris { 3105444e298SEric Paris assert_spin_locked(&old->lock); 3115444e298SEric Paris new->i.inode = old->i.inode; 3125444e298SEric Paris new->m.mnt = old->m.mnt; 3135444e298SEric Paris new->group = old->group; 3145444e298SEric Paris new->mask = old->mask; 3155444e298SEric Paris new->free_mark = old->free_mark; 3165444e298SEric Paris } 3175444e298SEric Paris 3185444e298SEric Paris /* 3195444e298SEric Paris * Nothing fancy, just initialize lists and locks and counters. 3205444e298SEric Paris */ 3215444e298SEric Paris void fsnotify_init_mark(struct fsnotify_mark *mark, 3225444e298SEric Paris void (*free_mark)(struct fsnotify_mark *mark)) 3235444e298SEric Paris { 324ba643f04SEric Paris memset(mark, 0, sizeof(*mark)); 3255444e298SEric Paris spin_lock_init(&mark->lock); 3265444e298SEric Paris atomic_set(&mark->refcnt, 1); 3275444e298SEric Paris mark->free_mark = free_mark; 3285444e298SEric Paris } 329