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 9560063497SArun 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 { 11223e964c2SLino Sanfilippo if (atomic_dec_and_test(&mark->refcnt)) { 11323e964c2SLino Sanfilippo if (mark->group) 11423e964c2SLino Sanfilippo fsnotify_put_group(mark->group); 1155444e298SEric Paris mark->free_mark(mark); 1165444e298SEric Paris } 11723e964c2SLino Sanfilippo } 1185444e298SEric Paris 1195444e298SEric Paris /* 1205444e298SEric Paris * Any time a mark is getting freed we end up here. 1215444e298SEric Paris * The caller had better be holding a reference to this mark so we don't actually 1225444e298SEric Paris * do the final put under the mark->lock 1235444e298SEric Paris */ 1245444e298SEric Paris void fsnotify_destroy_mark(struct fsnotify_mark *mark) 1255444e298SEric Paris { 1265444e298SEric Paris struct fsnotify_group *group; 1270d48b7f0SEric Paris struct inode *inode = NULL; 1285444e298SEric Paris 1295444e298SEric Paris spin_lock(&mark->lock); 130*104d06f0SLino Sanfilippo /* dont get the group from a mark that is not alive yet */ 131*104d06f0SLino Sanfilippo if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { 132*104d06f0SLino Sanfilippo spin_unlock(&mark->lock); 133*104d06f0SLino Sanfilippo return; 134*104d06f0SLino Sanfilippo } 13523e964c2SLino Sanfilippo fsnotify_get_group(mark->group); 1365444e298SEric Paris group = mark->group; 137*104d06f0SLino Sanfilippo spin_unlock(&mark->lock); 138*104d06f0SLino Sanfilippo 139*104d06f0SLino Sanfilippo spin_lock(&group->mark_lock); 140*104d06f0SLino Sanfilippo spin_lock(&mark->lock); 1415444e298SEric Paris 142700307a2SEric Paris /* something else already called this function on this mark */ 143700307a2SEric Paris if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { 1445444e298SEric Paris spin_unlock(&mark->lock); 145*104d06f0SLino Sanfilippo spin_unlock(&group->mark_lock); 14623e964c2SLino Sanfilippo goto put_group; 1475444e298SEric Paris } 1485444e298SEric Paris 149700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 150700307a2SEric Paris 1510d48b7f0SEric Paris if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) { 1520d48b7f0SEric Paris inode = mark->i.inode; 153b31d397eSEric Paris fsnotify_destroy_inode_mark(mark); 1540d48b7f0SEric Paris } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT) 1550d48b7f0SEric Paris fsnotify_destroy_vfsmount_mark(mark); 1565444e298SEric Paris else 1575444e298SEric Paris BUG(); 1585444e298SEric Paris 1595444e298SEric Paris list_del_init(&mark->g_list); 1605444e298SEric Paris 1615444e298SEric Paris spin_unlock(&mark->lock); 162*104d06f0SLino Sanfilippo spin_unlock(&group->mark_lock); 1635444e298SEric Paris 16475c1be48SEric Paris spin_lock(&destroy_lock); 16575c1be48SEric Paris list_add(&mark->destroy_list, &destroy_list); 16675c1be48SEric Paris spin_unlock(&destroy_lock); 16775c1be48SEric Paris wake_up(&destroy_waitq); 16875c1be48SEric Paris 1695444e298SEric Paris /* 1705444e298SEric Paris * Some groups like to know that marks are being freed. This is a 1715444e298SEric Paris * callback to the group function to let it know that this mark 1725444e298SEric Paris * is being freed. 1735444e298SEric Paris */ 1745444e298SEric Paris if (group->ops->freeing_mark) 1755444e298SEric Paris group->ops->freeing_mark(mark, group); 1765444e298SEric Paris 1775444e298SEric Paris /* 1785444e298SEric Paris * __fsnotify_update_child_dentry_flags(inode); 1795444e298SEric Paris * 1805444e298SEric Paris * I really want to call that, but we can't, we have no idea if the inode 1815444e298SEric Paris * still exists the second we drop the mark->lock. 1825444e298SEric Paris * 1835444e298SEric Paris * The next time an event arrive to this inode from one of it's children 1845444e298SEric Paris * __fsnotify_parent will see that the inode doesn't care about it's 1855444e298SEric Paris * children and will update all of these flags then. So really this 1865444e298SEric Paris * is just a lazy update (and could be a perf win...) 1875444e298SEric Paris */ 1885444e298SEric Paris 18990b1e7a5SEric Paris if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) 1905444e298SEric Paris iput(inode); 1915444e298SEric Paris /* 192fed47485SMiklos Szeredi * We don't necessarily have a ref on mark from caller so the above iput 193fed47485SMiklos Szeredi * may have already destroyed it. Don't touch from now on. 194fed47485SMiklos Szeredi */ 195fed47485SMiklos Szeredi 19623e964c2SLino Sanfilippo atomic_dec(&group->num_marks); 19723e964c2SLino Sanfilippo 19823e964c2SLino Sanfilippo put_group: 19923e964c2SLino Sanfilippo fsnotify_put_group(group); 2005444e298SEric Paris } 2015444e298SEric Paris 20290b1e7a5SEric Paris void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask) 20390b1e7a5SEric Paris { 20490b1e7a5SEric Paris assert_spin_locked(&mark->lock); 20590b1e7a5SEric Paris 20690b1e7a5SEric Paris mark->mask = mask; 20790b1e7a5SEric Paris 20890b1e7a5SEric Paris if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) 20990b1e7a5SEric Paris fsnotify_set_inode_mark_mask_locked(mark, mask); 21090b1e7a5SEric Paris } 21190b1e7a5SEric Paris 21233af5e32SEric Paris void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask) 21333af5e32SEric Paris { 21433af5e32SEric Paris assert_spin_locked(&mark->lock); 21533af5e32SEric Paris 21633af5e32SEric Paris mark->ignored_mask = mask; 21733af5e32SEric Paris } 21890b1e7a5SEric Paris 2195444e298SEric Paris /* 2205444e298SEric Paris * Attach an initialized mark to a given group and fs object. 2215444e298SEric Paris * These marks may be used for the fsnotify backend to determine which 2225444e298SEric Paris * event types should be delivered to which group. 2235444e298SEric Paris */ 2245444e298SEric Paris int fsnotify_add_mark(struct fsnotify_mark *mark, 2255444e298SEric Paris struct fsnotify_group *group, struct inode *inode, 2265444e298SEric Paris struct vfsmount *mnt, int allow_dups) 2275444e298SEric Paris { 2285444e298SEric Paris int ret = 0; 2295444e298SEric Paris 2305444e298SEric Paris BUG_ON(inode && mnt); 2315444e298SEric Paris BUG_ON(!inode && !mnt); 2325444e298SEric Paris 2335444e298SEric Paris /* 2345444e298SEric Paris * LOCKING ORDER!!!! 2355444e298SEric Paris * group->mark_lock 236*104d06f0SLino Sanfilippo * mark->lock 2375444e298SEric Paris * inode->i_lock 2385444e298SEric Paris */ 2395444e298SEric Paris spin_lock(&group->mark_lock); 2405444e298SEric Paris 241*104d06f0SLino Sanfilippo spin_lock(&mark->lock); 242700307a2SEric Paris mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE; 243700307a2SEric Paris 24423e964c2SLino Sanfilippo fsnotify_get_group(group); 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 26290b1e7a5SEric Paris /* this will pin the object if appropriate */ 26390b1e7a5SEric Paris fsnotify_set_mark_mask_locked(mark, mark->mask); 2645444e298SEric Paris spin_unlock(&mark->lock); 2655444e298SEric Paris 266*104d06f0SLino Sanfilippo spin_unlock(&group->mark_lock); 267*104d06f0SLino Sanfilippo 2685444e298SEric Paris if (inode) 2695444e298SEric Paris __fsnotify_update_child_dentry_flags(inode); 2705444e298SEric Paris 2715444e298SEric Paris return ret; 2725444e298SEric Paris err: 273700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 2745444e298SEric Paris list_del_init(&mark->g_list); 27523e964c2SLino Sanfilippo fsnotify_put_group(group); 27675c1be48SEric Paris mark->group = NULL; 2775444e298SEric Paris atomic_dec(&group->num_marks); 2785444e298SEric Paris 2795444e298SEric Paris spin_unlock(&mark->lock); 280*104d06f0SLino Sanfilippo spin_unlock(&group->mark_lock); 2815444e298SEric Paris 28275c1be48SEric Paris spin_lock(&destroy_lock); 28375c1be48SEric Paris list_add(&mark->destroy_list, &destroy_list); 28475c1be48SEric Paris spin_unlock(&destroy_lock); 28575c1be48SEric Paris wake_up(&destroy_waitq); 28675c1be48SEric 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; 32823e964c2SLino Sanfilippo if (old->group) 32923e964c2SLino Sanfilippo fsnotify_get_group(old->group); 3305444e298SEric Paris new->group = old->group; 3315444e298SEric Paris new->mask = old->mask; 3325444e298SEric Paris new->free_mark = old->free_mark; 3335444e298SEric Paris } 3345444e298SEric Paris 3355444e298SEric Paris /* 3365444e298SEric Paris * Nothing fancy, just initialize lists and locks and counters. 3375444e298SEric Paris */ 3385444e298SEric Paris void fsnotify_init_mark(struct fsnotify_mark *mark, 3395444e298SEric Paris void (*free_mark)(struct fsnotify_mark *mark)) 3405444e298SEric Paris { 341ba643f04SEric Paris memset(mark, 0, sizeof(*mark)); 3425444e298SEric Paris spin_lock_init(&mark->lock); 3435444e298SEric Paris atomic_set(&mark->refcnt, 1); 3445444e298SEric Paris mark->free_mark = free_mark; 3455444e298SEric Paris } 34675c1be48SEric Paris 34775c1be48SEric Paris static int fsnotify_mark_destroy(void *ignored) 34875c1be48SEric Paris { 34975c1be48SEric Paris struct fsnotify_mark *mark, *next; 35075c1be48SEric Paris LIST_HEAD(private_destroy_list); 35175c1be48SEric Paris 35275c1be48SEric Paris for (;;) { 35375c1be48SEric Paris spin_lock(&destroy_lock); 3548778abb9SAndreas Gruenbacher /* exchange the list head */ 3558778abb9SAndreas Gruenbacher list_replace_init(&destroy_list, &private_destroy_list); 35675c1be48SEric Paris spin_unlock(&destroy_lock); 35775c1be48SEric Paris 35875c1be48SEric Paris synchronize_srcu(&fsnotify_mark_srcu); 35975c1be48SEric Paris 36075c1be48SEric Paris list_for_each_entry_safe(mark, next, &private_destroy_list, destroy_list) { 36175c1be48SEric Paris list_del_init(&mark->destroy_list); 36275c1be48SEric Paris fsnotify_put_mark(mark); 36375c1be48SEric Paris } 36475c1be48SEric Paris 36575c1be48SEric Paris wait_event_interruptible(destroy_waitq, !list_empty(&destroy_list)); 36675c1be48SEric Paris } 36775c1be48SEric Paris 36875c1be48SEric Paris return 0; 36975c1be48SEric Paris } 37075c1be48SEric Paris 37175c1be48SEric Paris static int __init fsnotify_mark_init(void) 37275c1be48SEric Paris { 37375c1be48SEric Paris struct task_struct *thread; 37475c1be48SEric Paris 37575c1be48SEric Paris thread = kthread_run(fsnotify_mark_destroy, NULL, 37675c1be48SEric Paris "fsnotify_mark"); 37775c1be48SEric Paris if (IS_ERR(thread)) 37875c1be48SEric Paris panic("unable to start fsnotify mark destruction thread."); 37975c1be48SEric Paris 38075c1be48SEric Paris return 0; 38175c1be48SEric Paris } 38275c1be48SEric Paris device_initcall(fsnotify_mark_init); 383