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: 23*9756b918SLino Sanfilippo * The group->recnt and mark->refcnt tell how many "things" in the kernel 24*9756b918SLino Sanfilippo * currently are referencing the objects. Both kind of objects typically will 25*9756b918SLino Sanfilippo * live inside the kernel with a refcnt of 2, one for its creation and one for 26*9756b918SLino Sanfilippo * the reference a group and a mark hold to each other. 27*9756b918SLino Sanfilippo * If you are holding the appropriate locks, you can take a reference and the 28*9756b918SLino Sanfilippo * object itself is guaranteed to survive until the reference is dropped. 295444e298SEric Paris * 305444e298SEric Paris * LOCKING: 31*9756b918SLino Sanfilippo * There are 3 locks involved with fsnotify inode marks and they MUST be taken 32*9756b918SLino Sanfilippo * in order as follows: 335444e298SEric Paris * 34*9756b918SLino Sanfilippo * group->mark_mutex 355444e298SEric Paris * mark->lock 365444e298SEric Paris * inode->i_lock 375444e298SEric Paris * 38*9756b918SLino Sanfilippo * group->mark_mutex protects the marks_list anchored inside a given group and 39*9756b918SLino Sanfilippo * each mark is hooked via the g_list. It also protects the groups private 40*9756b918SLino Sanfilippo * data (i.e group limits). 41*9756b918SLino Sanfilippo 42*9756b918SLino Sanfilippo * mark->lock protects the marks attributes like its masks and flags. 43*9756b918SLino Sanfilippo * Furthermore it protects the access to a reference of the group that the mark 44*9756b918SLino Sanfilippo * is assigned to as well as the access to a reference of the inode/vfsmount 45*9756b918SLino 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 545444e298SEric Paris * refcnt==0. 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 * Worst case we are given an inode and need to clean up all the marks on that 655444e298SEric Paris * inode. We take i_lock and walk the i_fsnotify_marks safely. For each 665444e298SEric Paris * mark on the list we take a reference (so the mark can't disappear under us). 675444e298SEric Paris * We remove that mark form the inode's list of marks and we add this mark to a 68*9756b918SLino Sanfilippo * private list anchored on the stack using i_free_list; we walk i_free_list 69*9756b918SLino Sanfilippo * and before we destroy the mark we make sure that we dont race with a 70*9756b918SLino Sanfilippo * concurrent destroy_group by getting a ref to the marks group and taking the 71*9756b918SLino Sanfilippo * groups mutex. 72*9756b918SLino Sanfilippo 735444e298SEric Paris * Very similarly for freeing by group, except we use free_g_list. 745444e298SEric Paris * 755444e298SEric Paris * This has the very interesting property of being able to run concurrently with 765444e298SEric Paris * any (or all) other directions. 775444e298SEric Paris */ 785444e298SEric Paris 795444e298SEric Paris #include <linux/fs.h> 805444e298SEric Paris #include <linux/init.h> 815444e298SEric Paris #include <linux/kernel.h> 8275c1be48SEric Paris #include <linux/kthread.h> 835444e298SEric Paris #include <linux/module.h> 845444e298SEric Paris #include <linux/mutex.h> 855444e298SEric Paris #include <linux/slab.h> 865444e298SEric Paris #include <linux/spinlock.h> 8775c1be48SEric Paris #include <linux/srcu.h> 885444e298SEric Paris 8960063497SArun Sharma #include <linux/atomic.h> 905444e298SEric Paris 915444e298SEric Paris #include <linux/fsnotify_backend.h> 925444e298SEric Paris #include "fsnotify.h" 935444e298SEric Paris 9475c1be48SEric Paris struct srcu_struct fsnotify_mark_srcu; 9575c1be48SEric Paris static DEFINE_SPINLOCK(destroy_lock); 9675c1be48SEric Paris static LIST_HEAD(destroy_list); 9775c1be48SEric Paris static DECLARE_WAIT_QUEUE_HEAD(destroy_waitq); 9875c1be48SEric 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 { 10623e964c2SLino Sanfilippo if (atomic_dec_and_test(&mark->refcnt)) { 10723e964c2SLino Sanfilippo if (mark->group) 10823e964c2SLino Sanfilippo fsnotify_put_group(mark->group); 1095444e298SEric Paris mark->free_mark(mark); 1105444e298SEric Paris } 11123e964c2SLino Sanfilippo } 1125444e298SEric Paris 1135444e298SEric Paris /* 1145444e298SEric Paris * Any time a mark is getting freed we end up here. 1155444e298SEric Paris * The caller had better be holding a reference to this mark so we don't actually 1165444e298SEric Paris * do the final put under the mark->lock 1175444e298SEric Paris */ 118d5a335b8SLino Sanfilippo void fsnotify_destroy_mark_locked(struct fsnotify_mark *mark, 119e2a29943SLino Sanfilippo struct fsnotify_group *group) 1205444e298SEric Paris { 1210d48b7f0SEric Paris struct inode *inode = NULL; 1225444e298SEric Paris 123d5a335b8SLino Sanfilippo BUG_ON(!mutex_is_locked(&group->mark_mutex)); 124d5a335b8SLino Sanfilippo 125104d06f0SLino Sanfilippo spin_lock(&mark->lock); 1265444e298SEric Paris 127700307a2SEric Paris /* something else already called this function on this mark */ 128700307a2SEric Paris if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { 1295444e298SEric Paris spin_unlock(&mark->lock); 130e2a29943SLino Sanfilippo return; 1315444e298SEric Paris } 1325444e298SEric Paris 133700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 134700307a2SEric Paris 1350d48b7f0SEric Paris if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) { 1360d48b7f0SEric Paris inode = mark->i.inode; 137b31d397eSEric Paris fsnotify_destroy_inode_mark(mark); 1380d48b7f0SEric Paris } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT) 1390d48b7f0SEric Paris fsnotify_destroy_vfsmount_mark(mark); 1405444e298SEric Paris else 1415444e298SEric Paris BUG(); 1425444e298SEric Paris 1435444e298SEric Paris list_del_init(&mark->g_list); 1445444e298SEric Paris 1455444e298SEric Paris spin_unlock(&mark->lock); 146d5a335b8SLino Sanfilippo 1476960b0d9SLino Sanfilippo if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) 1486960b0d9SLino Sanfilippo iput(inode); 149d5a335b8SLino Sanfilippo /* release lock temporarily */ 150986ab098SLino Sanfilippo mutex_unlock(&group->mark_mutex); 1515444e298SEric Paris 15275c1be48SEric Paris spin_lock(&destroy_lock); 15375c1be48SEric Paris list_add(&mark->destroy_list, &destroy_list); 15475c1be48SEric Paris spin_unlock(&destroy_lock); 15575c1be48SEric Paris wake_up(&destroy_waitq); 1566960b0d9SLino Sanfilippo /* 1576960b0d9SLino Sanfilippo * We don't necessarily have a ref on mark from caller so the above destroy 1586960b0d9SLino Sanfilippo * may have actually freed it, unless this group provides a 'freeing_mark' 1596960b0d9SLino Sanfilippo * function which must be holding a reference. 1606960b0d9SLino Sanfilippo */ 16175c1be48SEric 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 18223e964c2SLino Sanfilippo atomic_dec(&group->num_marks); 183d5a335b8SLino Sanfilippo 1846960b0d9SLino Sanfilippo mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 185d5a335b8SLino Sanfilippo } 186d5a335b8SLino Sanfilippo 187d5a335b8SLino Sanfilippo void fsnotify_destroy_mark(struct fsnotify_mark *mark, 188d5a335b8SLino Sanfilippo struct fsnotify_group *group) 189d5a335b8SLino Sanfilippo { 1906960b0d9SLino Sanfilippo mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 191d5a335b8SLino Sanfilippo fsnotify_destroy_mark_locked(mark, group); 192d5a335b8SLino Sanfilippo mutex_unlock(&group->mark_mutex); 1935444e298SEric Paris } 1945444e298SEric Paris 19590b1e7a5SEric Paris void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask) 19690b1e7a5SEric Paris { 19790b1e7a5SEric Paris assert_spin_locked(&mark->lock); 19890b1e7a5SEric Paris 19990b1e7a5SEric Paris mark->mask = mask; 20090b1e7a5SEric Paris 20190b1e7a5SEric Paris if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) 20290b1e7a5SEric Paris fsnotify_set_inode_mark_mask_locked(mark, mask); 20390b1e7a5SEric Paris } 20490b1e7a5SEric Paris 20533af5e32SEric Paris void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask) 20633af5e32SEric Paris { 20733af5e32SEric Paris assert_spin_locked(&mark->lock); 20833af5e32SEric Paris 20933af5e32SEric Paris mark->ignored_mask = mask; 21033af5e32SEric Paris } 21190b1e7a5SEric Paris 2125444e298SEric Paris /* 2135444e298SEric Paris * Attach an initialized mark to a given group and fs object. 2145444e298SEric Paris * These marks may be used for the fsnotify backend to determine which 2155444e298SEric Paris * event types should be delivered to which group. 2165444e298SEric Paris */ 217d5a335b8SLino Sanfilippo int fsnotify_add_mark_locked(struct fsnotify_mark *mark, 2185444e298SEric Paris struct fsnotify_group *group, struct inode *inode, 2195444e298SEric Paris struct vfsmount *mnt, int allow_dups) 2205444e298SEric Paris { 2215444e298SEric Paris int ret = 0; 2225444e298SEric Paris 2235444e298SEric Paris BUG_ON(inode && mnt); 2245444e298SEric Paris BUG_ON(!inode && !mnt); 225d5a335b8SLino Sanfilippo BUG_ON(!mutex_is_locked(&group->mark_mutex)); 2265444e298SEric Paris 2275444e298SEric Paris /* 2285444e298SEric Paris * LOCKING ORDER!!!! 229986ab098SLino Sanfilippo * group->mark_mutex 230104d06f0SLino Sanfilippo * mark->lock 2315444e298SEric Paris * inode->i_lock 2325444e298SEric Paris */ 233104d06f0SLino Sanfilippo spin_lock(&mark->lock); 234700307a2SEric Paris mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE; 235700307a2SEric Paris 23623e964c2SLino Sanfilippo fsnotify_get_group(group); 2375444e298SEric Paris mark->group = group; 2385444e298SEric Paris list_add(&mark->g_list, &group->marks_list); 2395444e298SEric Paris atomic_inc(&group->num_marks); 2405444e298SEric Paris fsnotify_get_mark(mark); /* for i_list and g_list */ 2415444e298SEric Paris 2425444e298SEric Paris if (inode) { 2435444e298SEric Paris ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups); 2445444e298SEric Paris if (ret) 2455444e298SEric Paris goto err; 2460d48b7f0SEric Paris } else if (mnt) { 2470d48b7f0SEric Paris ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups); 2480d48b7f0SEric Paris if (ret) 2490d48b7f0SEric Paris goto err; 2505444e298SEric Paris } else { 2515444e298SEric Paris BUG(); 2525444e298SEric Paris } 2535444e298SEric Paris 25490b1e7a5SEric Paris /* this will pin the object if appropriate */ 25590b1e7a5SEric Paris fsnotify_set_mark_mask_locked(mark, mark->mask); 2565444e298SEric Paris spin_unlock(&mark->lock); 2575444e298SEric Paris 2585444e298SEric Paris if (inode) 2595444e298SEric Paris __fsnotify_update_child_dentry_flags(inode); 2605444e298SEric Paris 2615444e298SEric Paris return ret; 2625444e298SEric Paris err: 263700307a2SEric Paris mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 2645444e298SEric Paris list_del_init(&mark->g_list); 26523e964c2SLino Sanfilippo fsnotify_put_group(group); 26675c1be48SEric Paris mark->group = NULL; 2675444e298SEric Paris atomic_dec(&group->num_marks); 2685444e298SEric Paris 2695444e298SEric Paris spin_unlock(&mark->lock); 2705444e298SEric Paris 27175c1be48SEric Paris spin_lock(&destroy_lock); 27275c1be48SEric Paris list_add(&mark->destroy_list, &destroy_list); 27375c1be48SEric Paris spin_unlock(&destroy_lock); 27475c1be48SEric Paris wake_up(&destroy_waitq); 27575c1be48SEric Paris 2765444e298SEric Paris return ret; 2775444e298SEric Paris } 2785444e298SEric Paris 279d5a335b8SLino Sanfilippo int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group, 280d5a335b8SLino Sanfilippo struct inode *inode, struct vfsmount *mnt, int allow_dups) 281d5a335b8SLino Sanfilippo { 282d5a335b8SLino Sanfilippo int ret; 283d5a335b8SLino Sanfilippo mutex_lock(&group->mark_mutex); 284d5a335b8SLino Sanfilippo ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups); 285d5a335b8SLino Sanfilippo mutex_unlock(&group->mark_mutex); 286d5a335b8SLino Sanfilippo return ret; 287d5a335b8SLino Sanfilippo } 288d5a335b8SLino Sanfilippo 2895444e298SEric Paris /* 2904d92604cSEric Paris * clear any marks in a group in which mark->flags & flags is true 2915444e298SEric Paris */ 2924d92604cSEric Paris void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group, 2934d92604cSEric Paris unsigned int flags) 2945444e298SEric Paris { 2955444e298SEric Paris struct fsnotify_mark *lmark, *mark; 2965444e298SEric Paris 2976960b0d9SLino Sanfilippo mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); 2985444e298SEric Paris list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { 2994d92604cSEric Paris if (mark->flags & flags) { 3005444e298SEric Paris fsnotify_get_mark(mark); 30164c20d2aSLino Sanfilippo fsnotify_destroy_mark_locked(mark, group); 30264c20d2aSLino Sanfilippo fsnotify_put_mark(mark); 3035444e298SEric Paris } 3044d92604cSEric Paris } 305986ab098SLino Sanfilippo mutex_unlock(&group->mark_mutex); 3065444e298SEric Paris } 3075444e298SEric Paris 3084d92604cSEric Paris /* 3094d92604cSEric Paris * Given a group, destroy all of the marks associated with that group. 3104d92604cSEric Paris */ 3114d92604cSEric Paris void fsnotify_clear_marks_by_group(struct fsnotify_group *group) 3124d92604cSEric Paris { 3134d92604cSEric Paris fsnotify_clear_marks_by_group_flags(group, (unsigned int)-1); 3144d92604cSEric Paris } 3154d92604cSEric Paris 3165444e298SEric Paris void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old) 3175444e298SEric Paris { 3185444e298SEric Paris assert_spin_locked(&old->lock); 3195444e298SEric Paris new->i.inode = old->i.inode; 3205444e298SEric Paris new->m.mnt = old->m.mnt; 32123e964c2SLino Sanfilippo if (old->group) 32223e964c2SLino Sanfilippo fsnotify_get_group(old->group); 3235444e298SEric Paris new->group = old->group; 3245444e298SEric Paris new->mask = old->mask; 3255444e298SEric Paris new->free_mark = old->free_mark; 3265444e298SEric Paris } 3275444e298SEric Paris 3285444e298SEric Paris /* 3295444e298SEric Paris * Nothing fancy, just initialize lists and locks and counters. 3305444e298SEric Paris */ 3315444e298SEric Paris void fsnotify_init_mark(struct fsnotify_mark *mark, 3325444e298SEric Paris void (*free_mark)(struct fsnotify_mark *mark)) 3335444e298SEric Paris { 334ba643f04SEric Paris memset(mark, 0, sizeof(*mark)); 3355444e298SEric Paris spin_lock_init(&mark->lock); 3365444e298SEric Paris atomic_set(&mark->refcnt, 1); 3375444e298SEric Paris mark->free_mark = free_mark; 3385444e298SEric Paris } 33975c1be48SEric Paris 34075c1be48SEric Paris static int fsnotify_mark_destroy(void *ignored) 34175c1be48SEric Paris { 34275c1be48SEric Paris struct fsnotify_mark *mark, *next; 34375c1be48SEric Paris LIST_HEAD(private_destroy_list); 34475c1be48SEric Paris 34575c1be48SEric Paris for (;;) { 34675c1be48SEric Paris spin_lock(&destroy_lock); 3478778abb9SAndreas Gruenbacher /* exchange the list head */ 3488778abb9SAndreas Gruenbacher list_replace_init(&destroy_list, &private_destroy_list); 34975c1be48SEric Paris spin_unlock(&destroy_lock); 35075c1be48SEric Paris 35175c1be48SEric Paris synchronize_srcu(&fsnotify_mark_srcu); 35275c1be48SEric Paris 35375c1be48SEric Paris list_for_each_entry_safe(mark, next, &private_destroy_list, destroy_list) { 35475c1be48SEric Paris list_del_init(&mark->destroy_list); 35575c1be48SEric Paris fsnotify_put_mark(mark); 35675c1be48SEric Paris } 35775c1be48SEric Paris 35875c1be48SEric Paris wait_event_interruptible(destroy_waitq, !list_empty(&destroy_list)); 35975c1be48SEric Paris } 36075c1be48SEric Paris 36175c1be48SEric Paris return 0; 36275c1be48SEric Paris } 36375c1be48SEric Paris 36475c1be48SEric Paris static int __init fsnotify_mark_init(void) 36575c1be48SEric Paris { 36675c1be48SEric Paris struct task_struct *thread; 36775c1be48SEric Paris 36875c1be48SEric Paris thread = kthread_run(fsnotify_mark_destroy, NULL, 36975c1be48SEric Paris "fsnotify_mark"); 37075c1be48SEric Paris if (IS_ERR(thread)) 37175c1be48SEric Paris panic("unable to start fsnotify mark destruction thread."); 37275c1be48SEric Paris 37375c1be48SEric Paris return 0; 37475c1be48SEric Paris } 37575c1be48SEric Paris device_initcall(fsnotify_mark_init); 376