190586523SEric Paris /* 290586523SEric Paris * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> 390586523SEric Paris * 490586523SEric Paris * This program is free software; you can redistribute it and/or modify 590586523SEric Paris * it under the terms of the GNU General Public License as published by 690586523SEric Paris * the Free Software Foundation; either version 2, or (at your option) 790586523SEric Paris * any later version. 890586523SEric Paris * 990586523SEric Paris * This program is distributed in the hope that it will be useful, 1090586523SEric Paris * but WITHOUT ANY WARRANTY; without even the implied warranty of 1190586523SEric Paris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1290586523SEric Paris * GNU General Public License for more details. 1390586523SEric Paris * 1490586523SEric Paris * You should have received a copy of the GNU General Public License 1590586523SEric Paris * along with this program; see the file COPYING. If not, write to 1690586523SEric Paris * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 1790586523SEric Paris */ 1890586523SEric Paris 1990586523SEric Paris #include <linux/dcache.h> 2090586523SEric Paris #include <linux/fs.h> 215a0e3ad6STejun Heo #include <linux/gfp.h> 2290586523SEric Paris #include <linux/init.h> 2390586523SEric Paris #include <linux/module.h> 247131485aSEric Paris #include <linux/mount.h> 2590586523SEric Paris #include <linux/srcu.h> 2690586523SEric Paris 2790586523SEric Paris #include <linux/fsnotify_backend.h> 2890586523SEric Paris #include "fsnotify.h" 2990586523SEric Paris 3090586523SEric Paris /* 313be25f49SEric Paris * Clear all of the marks on an inode when it is being evicted from core 323be25f49SEric Paris */ 333be25f49SEric Paris void __fsnotify_inode_delete(struct inode *inode) 343be25f49SEric Paris { 353be25f49SEric Paris fsnotify_clear_marks_by_inode(inode); 363be25f49SEric Paris } 373be25f49SEric Paris EXPORT_SYMBOL_GPL(__fsnotify_inode_delete); 383be25f49SEric Paris 39ca9c726eSAndreas Gruenbacher void __fsnotify_vfsmount_delete(struct vfsmount *mnt) 40ca9c726eSAndreas Gruenbacher { 41ca9c726eSAndreas Gruenbacher fsnotify_clear_marks_by_mount(mnt); 42ca9c726eSAndreas Gruenbacher } 43ca9c726eSAndreas Gruenbacher 44ebb3b47eSJan Kara /** 45ebb3b47eSJan Kara * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes. 46ebb3b47eSJan Kara * @sb: superblock being unmounted. 47ebb3b47eSJan Kara * 48ebb3b47eSJan Kara * Called during unmount with no locks held, so needs to be safe against 49ebb3b47eSJan Kara * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block. 50ebb3b47eSJan Kara */ 51ebb3b47eSJan Kara void fsnotify_unmount_inodes(struct super_block *sb) 52ebb3b47eSJan Kara { 53ebb3b47eSJan Kara struct inode *inode, *iput_inode = NULL; 54ebb3b47eSJan Kara 55ebb3b47eSJan Kara spin_lock(&sb->s_inode_list_lock); 56ebb3b47eSJan Kara list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 57ebb3b47eSJan Kara /* 58ebb3b47eSJan Kara * We cannot __iget() an inode in state I_FREEING, 59ebb3b47eSJan Kara * I_WILL_FREE, or I_NEW which is fine because by that point 60ebb3b47eSJan Kara * the inode cannot have any associated watches. 61ebb3b47eSJan Kara */ 62ebb3b47eSJan Kara spin_lock(&inode->i_lock); 63ebb3b47eSJan Kara if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) { 64ebb3b47eSJan Kara spin_unlock(&inode->i_lock); 65ebb3b47eSJan Kara continue; 66ebb3b47eSJan Kara } 67ebb3b47eSJan Kara 68ebb3b47eSJan Kara /* 69ebb3b47eSJan Kara * If i_count is zero, the inode cannot have any watches and 701751e8a6SLinus Torvalds * doing an __iget/iput with SB_ACTIVE clear would actually 71ebb3b47eSJan Kara * evict all inodes with zero i_count from icache which is 72ebb3b47eSJan Kara * unnecessarily violent and may in fact be illegal to do. 73ebb3b47eSJan Kara */ 74ebb3b47eSJan Kara if (!atomic_read(&inode->i_count)) { 75ebb3b47eSJan Kara spin_unlock(&inode->i_lock); 76ebb3b47eSJan Kara continue; 77ebb3b47eSJan Kara } 78ebb3b47eSJan Kara 79ebb3b47eSJan Kara __iget(inode); 80ebb3b47eSJan Kara spin_unlock(&inode->i_lock); 81ebb3b47eSJan Kara spin_unlock(&sb->s_inode_list_lock); 82ebb3b47eSJan Kara 83ebb3b47eSJan Kara if (iput_inode) 84ebb3b47eSJan Kara iput(iput_inode); 85ebb3b47eSJan Kara 86ebb3b47eSJan Kara /* for each watch, send FS_UNMOUNT and then remove it */ 87ebb3b47eSJan Kara fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0); 88ebb3b47eSJan Kara 89ebb3b47eSJan Kara fsnotify_inode_delete(inode); 90ebb3b47eSJan Kara 91ebb3b47eSJan Kara iput_inode = inode; 92ebb3b47eSJan Kara 93ebb3b47eSJan Kara spin_lock(&sb->s_inode_list_lock); 94ebb3b47eSJan Kara } 95ebb3b47eSJan Kara spin_unlock(&sb->s_inode_list_lock); 96ebb3b47eSJan Kara 97ebb3b47eSJan Kara if (iput_inode) 98ebb3b47eSJan Kara iput(iput_inode); 99ebb3b47eSJan Kara } 100ebb3b47eSJan Kara 1013be25f49SEric Paris /* 102c28f7e56SEric Paris * Given an inode, first check if we care what happens to our children. Inotify 103c28f7e56SEric Paris * and dnotify both tell their parents about events. If we care about any event 104c28f7e56SEric Paris * on a child we run all of our children and set a dentry flag saying that the 105c28f7e56SEric Paris * parent cares. Thus when an event happens on a child it can quickly tell if 106c28f7e56SEric Paris * if there is a need to find a parent and send the event to the parent. 107c28f7e56SEric Paris */ 108c28f7e56SEric Paris void __fsnotify_update_child_dentry_flags(struct inode *inode) 109c28f7e56SEric Paris { 110c28f7e56SEric Paris struct dentry *alias; 111c28f7e56SEric Paris int watched; 112c28f7e56SEric Paris 113c28f7e56SEric Paris if (!S_ISDIR(inode->i_mode)) 114c28f7e56SEric Paris return; 115c28f7e56SEric Paris 116c28f7e56SEric Paris /* determine if the children should tell inode about their events */ 117c28f7e56SEric Paris watched = fsnotify_inode_watches_children(inode); 118c28f7e56SEric Paris 119873feea0SNick Piggin spin_lock(&inode->i_lock); 120c28f7e56SEric Paris /* run all of the dentries associated with this inode. Since this is a 121c28f7e56SEric Paris * directory, there damn well better only be one item on this list */ 122946e51f2SAl Viro hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 123c28f7e56SEric Paris struct dentry *child; 124c28f7e56SEric Paris 125c28f7e56SEric Paris /* run all of the children of the original inode and fix their 126c28f7e56SEric Paris * d_flags to indicate parental interest (their parent is the 127c28f7e56SEric Paris * original inode) */ 1282fd6b7f5SNick Piggin spin_lock(&alias->d_lock); 129946e51f2SAl Viro list_for_each_entry(child, &alias->d_subdirs, d_child) { 130c28f7e56SEric Paris if (!child->d_inode) 131c28f7e56SEric Paris continue; 132c28f7e56SEric Paris 1332fd6b7f5SNick Piggin spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); 134c28f7e56SEric Paris if (watched) 135c28f7e56SEric Paris child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED; 136c28f7e56SEric Paris else 137c28f7e56SEric Paris child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED; 138c28f7e56SEric Paris spin_unlock(&child->d_lock); 139c28f7e56SEric Paris } 1402fd6b7f5SNick Piggin spin_unlock(&alias->d_lock); 141c28f7e56SEric Paris } 142873feea0SNick Piggin spin_unlock(&inode->i_lock); 143c28f7e56SEric Paris } 144c28f7e56SEric Paris 145c28f7e56SEric Paris /* Notify this dentry's parent about a child's events. */ 14612c7f9dcSAl Viro int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask) 147c28f7e56SEric Paris { 148c28f7e56SEric Paris struct dentry *parent; 149c28f7e56SEric Paris struct inode *p_inode; 15052420392SEric Paris int ret = 0; 151c28f7e56SEric Paris 15272acc854SAndreas Gruenbacher if (!dentry) 1532069601bSLinus Torvalds dentry = path->dentry; 15428c60e37SEric Paris 155c28f7e56SEric Paris if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED)) 15652420392SEric Paris return 0; 157c28f7e56SEric Paris 1584d4eb366SChristoph Hellwig parent = dget_parent(dentry); 159c28f7e56SEric Paris p_inode = parent->d_inode; 160c28f7e56SEric Paris 1614d4eb366SChristoph Hellwig if (unlikely(!fsnotify_inode_watches_children(p_inode))) 1624d4eb366SChristoph Hellwig __fsnotify_update_child_dentry_flags(p_inode); 1634d4eb366SChristoph Hellwig else if (p_inode->i_fsnotify_mask & mask) { 16449d31c2fSAl Viro struct name_snapshot name; 16549d31c2fSAl Viro 166c28f7e56SEric Paris /* we are notifying a parent so come up with the new mask which 167c28f7e56SEric Paris * specifies these are events which came from a child. */ 168c28f7e56SEric Paris mask |= FS_EVENT_ON_CHILD; 169c28f7e56SEric Paris 17049d31c2fSAl Viro take_dentry_name_snapshot(&name, dentry); 1712069601bSLinus Torvalds if (path) 17252420392SEric Paris ret = fsnotify(p_inode, mask, path, FSNOTIFY_EVENT_PATH, 17349d31c2fSAl Viro name.name, 0); 17428c60e37SEric Paris else 17552420392SEric Paris ret = fsnotify(p_inode, mask, dentry->d_inode, FSNOTIFY_EVENT_INODE, 17649d31c2fSAl Viro name.name, 0); 17749d31c2fSAl Viro release_dentry_name_snapshot(&name); 178c28f7e56SEric Paris } 179c28f7e56SEric Paris 180c28f7e56SEric Paris dput(parent); 18152420392SEric Paris 18252420392SEric Paris return ret; 183c28f7e56SEric Paris } 184c28f7e56SEric Paris EXPORT_SYMBOL_GPL(__fsnotify_parent); 185c28f7e56SEric Paris 186fd657170SDan Carpenter static int send_to_group(struct inode *to_tell, 187ce8f76fbSEric Paris struct fsnotify_mark *inode_mark, 188ce8f76fbSEric Paris struct fsnotify_mark *vfsmount_mark, 189e637835eSAl Viro __u32 mask, const void *data, 190613a807fSEric Paris int data_is, u32 cookie, 1919385a84dSJan Kara const unsigned char *file_name, 1929385a84dSJan Kara struct fsnotify_iter_info *iter_info) 1937131485aSEric Paris { 194faa9560aSEric Paris struct fsnotify_group *group = NULL; 19592183a42SAmir Goldstein __u32 test_mask = (mask & ~FS_EVENT_ON_CHILD); 19692183a42SAmir Goldstein __u32 marks_mask = 0; 19792183a42SAmir Goldstein __u32 marks_ignored_mask = 0; 198613a807fSEric Paris 199faa9560aSEric Paris if (unlikely(!inode_mark && !vfsmount_mark)) { 200faa9560aSEric Paris BUG(); 201faa9560aSEric Paris return 0; 202faa9560aSEric Paris } 2035ba08e2eSEric Paris 204ce8f76fbSEric Paris /* clear ignored on inode modification */ 205ce8f76fbSEric Paris if (mask & FS_MODIFY) { 206ce8f76fbSEric Paris if (inode_mark && 207ce8f76fbSEric Paris !(inode_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) 208ce8f76fbSEric Paris inode_mark->ignored_mask = 0; 209ce8f76fbSEric Paris if (vfsmount_mark && 210ce8f76fbSEric Paris !(vfsmount_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) 211ce8f76fbSEric Paris vfsmount_mark->ignored_mask = 0; 212ce8f76fbSEric Paris } 213613a807fSEric Paris 214ce8f76fbSEric Paris /* does the inode mark tell us to do something? */ 215ce8f76fbSEric Paris if (inode_mark) { 216faa9560aSEric Paris group = inode_mark->group; 21792183a42SAmir Goldstein marks_mask |= inode_mark->mask; 21892183a42SAmir Goldstein marks_ignored_mask |= inode_mark->ignored_mask; 219ce8f76fbSEric Paris } 220ce8f76fbSEric Paris 221ce8f76fbSEric Paris /* does the vfsmount_mark tell us to do something? */ 222ce8f76fbSEric Paris if (vfsmount_mark) { 223faa9560aSEric Paris group = vfsmount_mark->group; 22492183a42SAmir Goldstein marks_mask |= vfsmount_mark->mask; 22592183a42SAmir Goldstein marks_ignored_mask |= vfsmount_mark->ignored_mask; 226ce8f76fbSEric Paris } 227ce8f76fbSEric Paris 228fd657170SDan Carpenter pr_debug("%s: group=%p to_tell=%p mask=%x inode_mark=%p" 22992183a42SAmir Goldstein " vfsmount_mark=%p marks_mask=%x marks_ignored_mask=%x" 2307053aee2SJan Kara " data=%p data_is=%d cookie=%d\n", 23192183a42SAmir Goldstein __func__, group, to_tell, mask, inode_mark, vfsmount_mark, 23292183a42SAmir Goldstein marks_mask, marks_ignored_mask, data, 2337053aee2SJan Kara data_is, cookie); 234faa9560aSEric Paris 23592183a42SAmir Goldstein if (!(test_mask & marks_mask & ~marks_ignored_mask)) 236613a807fSEric Paris return 0; 237613a807fSEric Paris 2387053aee2SJan Kara return group->ops->handle_event(group, to_tell, inode_mark, 2397053aee2SJan Kara vfsmount_mark, mask, data, data_is, 2409385a84dSJan Kara file_name, cookie, iter_info); 2417131485aSEric Paris } 2427131485aSEric Paris 2433427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector **connp) 2443427ce71SMiklos Szeredi { 2453427ce71SMiklos Szeredi struct fsnotify_mark_connector *conn; 2463427ce71SMiklos Szeredi struct hlist_node *node = NULL; 2473427ce71SMiklos Szeredi 2483427ce71SMiklos Szeredi conn = srcu_dereference(*connp, &fsnotify_mark_srcu); 2493427ce71SMiklos Szeredi if (conn) 2503427ce71SMiklos Szeredi node = srcu_dereference(conn->list.first, &fsnotify_mark_srcu); 2513427ce71SMiklos Szeredi 2523427ce71SMiklos Szeredi return hlist_entry_safe(node, struct fsnotify_mark, obj_list); 2533427ce71SMiklos Szeredi } 2543427ce71SMiklos Szeredi 2553427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark) 2563427ce71SMiklos Szeredi { 2573427ce71SMiklos Szeredi struct hlist_node *node = NULL; 2583427ce71SMiklos Szeredi 2593427ce71SMiklos Szeredi if (mark) 2603427ce71SMiklos Szeredi node = srcu_dereference(mark->obj_list.next, 2613427ce71SMiklos Szeredi &fsnotify_mark_srcu); 2623427ce71SMiklos Szeredi 2633427ce71SMiklos Szeredi return hlist_entry_safe(node, struct fsnotify_mark, obj_list); 2643427ce71SMiklos Szeredi } 2653427ce71SMiklos Szeredi 266c28f7e56SEric Paris /* 26790586523SEric Paris * This is the main call to fsnotify. The VFS calls into hook specific functions 26890586523SEric Paris * in linux/fsnotify.h. Those functions then in turn call here. Here will call 26990586523SEric Paris * out to all of the registered fsnotify_group. Those groups can then use the 27090586523SEric Paris * notification event in whatever means they feel necessary. 27190586523SEric Paris */ 272e637835eSAl Viro int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_is, 27359b0df21SEric Paris const unsigned char *file_name, u32 cookie) 27490586523SEric Paris { 2753427ce71SMiklos Szeredi struct fsnotify_iter_info iter_info = {}; 276c63181e6SAl Viro struct mount *mnt; 2779385a84dSJan Kara int ret = 0; 278e42e2773SEric Paris /* global tests shouldn't care about events on child only the specific event */ 279e42e2773SEric Paris __u32 test_mask = (mask & ~FS_EVENT_ON_CHILD); 28090586523SEric Paris 2812069601bSLinus Torvalds if (data_is == FSNOTIFY_EVENT_PATH) 282e637835eSAl Viro mnt = real_mount(((const struct path *)data)->mnt); 283613a807fSEric Paris else 284613a807fSEric Paris mnt = NULL; 285613a807fSEric Paris 286613a807fSEric Paris /* 2877c49b861SDave Hansen * Optimization: srcu_read_lock() has a memory barrier which can 2887c49b861SDave Hansen * be expensive. It protects walking the *_fsnotify_marks lists. 2897c49b861SDave Hansen * However, if we do not walk the lists, we do not have to do 2907c49b861SDave Hansen * SRCU because we have no references to any objects and do not 2917c49b861SDave Hansen * need SRCU to keep them "alive". 2927c49b861SDave Hansen */ 2939dd813c1SJan Kara if (!to_tell->i_fsnotify_marks && 2949dd813c1SJan Kara (!mnt || !mnt->mnt_fsnotify_marks)) 2957c49b861SDave Hansen return 0; 2967c49b861SDave Hansen /* 297613a807fSEric Paris * if this is a modify event we may need to clear the ignored masks 298613a807fSEric Paris * otherwise return if neither the inode nor the vfsmount care about 299613a807fSEric Paris * this type of event. 300613a807fSEric Paris */ 301613a807fSEric Paris if (!(mask & FS_MODIFY) && 302613a807fSEric Paris !(test_mask & to_tell->i_fsnotify_mask) && 303613a807fSEric Paris !(mnt && test_mask & mnt->mnt_fsnotify_mask)) 304613a807fSEric Paris return 0; 3053a9fb89fSEric Paris 3069385a84dSJan Kara iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu); 3077131485aSEric Paris 308613a807fSEric Paris if ((mask & FS_MODIFY) || 3099dd813c1SJan Kara (test_mask & to_tell->i_fsnotify_mask)) { 3103427ce71SMiklos Szeredi iter_info.inode_mark = 3113427ce71SMiklos Szeredi fsnotify_first_mark(&to_tell->i_fsnotify_marks); 3129dd813c1SJan Kara } 31375c1be48SEric Paris 31484e1ab4dSEric Paris if (mnt && ((mask & FS_MODIFY) || 31584e1ab4dSEric Paris (test_mask & mnt->mnt_fsnotify_mask))) { 3163427ce71SMiklos Szeredi iter_info.inode_mark = 3173427ce71SMiklos Szeredi fsnotify_first_mark(&to_tell->i_fsnotify_marks); 3183427ce71SMiklos Szeredi iter_info.vfsmount_mark = 3193427ce71SMiklos Szeredi fsnotify_first_mark(&mnt->mnt_fsnotify_marks); 3207131485aSEric Paris } 32175c1be48SEric Paris 3228edc6e16SJan Kara /* 3238edc6e16SJan Kara * We need to merge inode & vfsmount mark lists so that inode mark 3248edc6e16SJan Kara * ignore masks are properly reflected for mount mark notifications. 3258edc6e16SJan Kara * That's why this traversal is so complicated... 3268edc6e16SJan Kara */ 3273427ce71SMiklos Szeredi while (iter_info.inode_mark || iter_info.vfsmount_mark) { 3283427ce71SMiklos Szeredi struct fsnotify_mark *inode_mark = iter_info.inode_mark; 3293427ce71SMiklos Szeredi struct fsnotify_mark *vfsmount_mark = iter_info.vfsmount_mark; 3303427ce71SMiklos Szeredi 3313427ce71SMiklos Szeredi if (inode_mark && vfsmount_mark) { 3323427ce71SMiklos Szeredi int cmp = fsnotify_compare_groups(inode_mark->group, 3333427ce71SMiklos Szeredi vfsmount_mark->group); 3343427ce71SMiklos Szeredi if (cmp > 0) 3358edc6e16SJan Kara inode_mark = NULL; 3363427ce71SMiklos Szeredi else if (cmp < 0) 3378edc6e16SJan Kara vfsmount_mark = NULL; 3388edc6e16SJan Kara } 3399385a84dSJan Kara 3408edc6e16SJan Kara ret = send_to_group(to_tell, inode_mark, vfsmount_mark, mask, 3419385a84dSJan Kara data, data_is, cookie, file_name, 3429385a84dSJan Kara &iter_info); 34384a5b68eSEric Paris 344ff8bcbd0SEric Paris if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS)) 345ff8bcbd0SEric Paris goto out; 346ff8bcbd0SEric Paris 3473427ce71SMiklos Szeredi if (inode_mark) 3483427ce71SMiklos Szeredi iter_info.inode_mark = 3493427ce71SMiklos Szeredi fsnotify_next_mark(iter_info.inode_mark); 3503427ce71SMiklos Szeredi if (vfsmount_mark) 3513427ce71SMiklos Szeredi iter_info.vfsmount_mark = 3523427ce71SMiklos Szeredi fsnotify_next_mark(iter_info.vfsmount_mark); 3537131485aSEric Paris } 354ff8bcbd0SEric Paris ret = 0; 355ff8bcbd0SEric Paris out: 3569385a84dSJan Kara srcu_read_unlock(&fsnotify_mark_srcu, iter_info.srcu_idx); 357c4ec54b4SEric Paris 35898b5c10dSJean-Christophe Dubois return ret; 35990586523SEric Paris } 36090586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify); 36190586523SEric Paris 3629dd813c1SJan Kara extern struct kmem_cache *fsnotify_mark_connector_cachep; 3639dd813c1SJan Kara 36490586523SEric Paris static __init int fsnotify_init(void) 36590586523SEric Paris { 36675c1be48SEric Paris int ret; 36775c1be48SEric Paris 36820dee624SEric Paris BUG_ON(hweight32(ALL_FSNOTIFY_EVENTS) != 23); 36920dee624SEric Paris 37075c1be48SEric Paris ret = init_srcu_struct(&fsnotify_mark_srcu); 37175c1be48SEric Paris if (ret) 37275c1be48SEric Paris panic("initializing fsnotify_mark_srcu"); 37375c1be48SEric Paris 3749dd813c1SJan Kara fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector, 3759dd813c1SJan Kara SLAB_PANIC); 3769dd813c1SJan Kara 37775c1be48SEric Paris return 0; 37890586523SEric Paris } 37975c1be48SEric Paris core_initcall(fsnotify_init); 380