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 443be25f49SEric Paris /* 45c28f7e56SEric Paris * Given an inode, first check if we care what happens to our children. Inotify 46c28f7e56SEric Paris * and dnotify both tell their parents about events. If we care about any event 47c28f7e56SEric Paris * on a child we run all of our children and set a dentry flag saying that the 48c28f7e56SEric Paris * parent cares. Thus when an event happens on a child it can quickly tell if 49c28f7e56SEric Paris * if there is a need to find a parent and send the event to the parent. 50c28f7e56SEric Paris */ 51c28f7e56SEric Paris void __fsnotify_update_child_dentry_flags(struct inode *inode) 52c28f7e56SEric Paris { 53c28f7e56SEric Paris struct dentry *alias; 54c28f7e56SEric Paris int watched; 55c28f7e56SEric Paris 56c28f7e56SEric Paris if (!S_ISDIR(inode->i_mode)) 57c28f7e56SEric Paris return; 58c28f7e56SEric Paris 59c28f7e56SEric Paris /* determine if the children should tell inode about their events */ 60c28f7e56SEric Paris watched = fsnotify_inode_watches_children(inode); 61c28f7e56SEric Paris 62c28f7e56SEric Paris spin_lock(&dcache_lock); 63c28f7e56SEric Paris /* run all of the dentries associated with this inode. Since this is a 64c28f7e56SEric Paris * directory, there damn well better only be one item on this list */ 65c28f7e56SEric Paris list_for_each_entry(alias, &inode->i_dentry, d_alias) { 66c28f7e56SEric Paris struct dentry *child; 67c28f7e56SEric Paris 68c28f7e56SEric Paris /* run all of the children of the original inode and fix their 69c28f7e56SEric Paris * d_flags to indicate parental interest (their parent is the 70c28f7e56SEric Paris * original inode) */ 71c28f7e56SEric Paris list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) { 72c28f7e56SEric Paris if (!child->d_inode) 73c28f7e56SEric Paris continue; 74c28f7e56SEric Paris 75c28f7e56SEric Paris spin_lock(&child->d_lock); 76c28f7e56SEric Paris if (watched) 77c28f7e56SEric Paris child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED; 78c28f7e56SEric Paris else 79c28f7e56SEric Paris child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED; 80c28f7e56SEric Paris spin_unlock(&child->d_lock); 81c28f7e56SEric Paris } 82c28f7e56SEric Paris } 83c28f7e56SEric Paris spin_unlock(&dcache_lock); 84c28f7e56SEric Paris } 85c28f7e56SEric Paris 86c28f7e56SEric Paris /* Notify this dentry's parent about a child's events. */ 8772acc854SAndreas Gruenbacher void __fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask) 88c28f7e56SEric Paris { 89c28f7e56SEric Paris struct dentry *parent; 90c28f7e56SEric Paris struct inode *p_inode; 91c28f7e56SEric Paris bool send = false; 92c28f7e56SEric Paris bool should_update_children = false; 93c28f7e56SEric Paris 9472acc854SAndreas Gruenbacher if (!dentry) 9572acc854SAndreas Gruenbacher dentry = path->dentry; 9628c60e37SEric Paris 97c28f7e56SEric Paris if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED)) 98c28f7e56SEric Paris return; 99c28f7e56SEric Paris 100c28f7e56SEric Paris spin_lock(&dentry->d_lock); 101c28f7e56SEric Paris parent = dentry->d_parent; 102c28f7e56SEric Paris p_inode = parent->d_inode; 103c28f7e56SEric Paris 104c28f7e56SEric Paris if (fsnotify_inode_watches_children(p_inode)) { 105c28f7e56SEric Paris if (p_inode->i_fsnotify_mask & mask) { 106c28f7e56SEric Paris dget(parent); 107c28f7e56SEric Paris send = true; 108c28f7e56SEric Paris } 109c28f7e56SEric Paris } else { 110c28f7e56SEric Paris /* 111c28f7e56SEric Paris * The parent doesn't care about events on it's children but 112c28f7e56SEric Paris * at least one child thought it did. We need to run all the 113c28f7e56SEric Paris * children and update their d_flags to let them know p_inode 114c28f7e56SEric Paris * doesn't care about them any more. 115c28f7e56SEric Paris */ 116c28f7e56SEric Paris dget(parent); 117c28f7e56SEric Paris should_update_children = true; 118c28f7e56SEric Paris } 119c28f7e56SEric Paris 120c28f7e56SEric Paris spin_unlock(&dentry->d_lock); 121c28f7e56SEric Paris 122c28f7e56SEric Paris if (send) { 123c28f7e56SEric Paris /* we are notifying a parent so come up with the new mask which 124c28f7e56SEric Paris * specifies these are events which came from a child. */ 125c28f7e56SEric Paris mask |= FS_EVENT_ON_CHILD; 126c28f7e56SEric Paris 12772acc854SAndreas Gruenbacher if (path) 12872acc854SAndreas Gruenbacher fsnotify(p_inode, mask, path, FSNOTIFY_EVENT_PATH, 12928c60e37SEric Paris dentry->d_name.name, 0); 13028c60e37SEric Paris else 13162ffe5dfSEric Paris fsnotify(p_inode, mask, dentry->d_inode, FSNOTIFY_EVENT_INODE, 13247882c6fSEric Paris dentry->d_name.name, 0); 133c28f7e56SEric Paris dput(parent); 134c28f7e56SEric Paris } 135c28f7e56SEric Paris 136c28f7e56SEric Paris if (unlikely(should_update_children)) { 137c28f7e56SEric Paris __fsnotify_update_child_dentry_flags(p_inode); 138c28f7e56SEric Paris dput(parent); 139c28f7e56SEric Paris } 140c28f7e56SEric Paris } 141c28f7e56SEric Paris EXPORT_SYMBOL_GPL(__fsnotify_parent); 142c28f7e56SEric Paris 143e8983861SEric Paris void __fsnotify_flush_ignored_mask(struct inode *inode, void *data, int data_is) 144e8983861SEric Paris { 145e8983861SEric Paris struct fsnotify_mark *mark; 146e8983861SEric Paris struct hlist_node *node; 147e8983861SEric Paris 148e8983861SEric Paris if (!hlist_empty(&inode->i_fsnotify_marks)) { 149e8983861SEric Paris spin_lock(&inode->i_lock); 150e8983861SEric Paris hlist_for_each_entry(mark, node, &inode->i_fsnotify_marks, i.i_list) { 151c908370fSEric Paris if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) 152e8983861SEric Paris mark->ignored_mask = 0; 153e8983861SEric Paris } 154e8983861SEric Paris spin_unlock(&inode->i_lock); 155e8983861SEric Paris } 156e8983861SEric Paris 157e8983861SEric Paris if (data_is == FSNOTIFY_EVENT_PATH) { 158e8983861SEric Paris struct vfsmount *mnt; 159e8983861SEric Paris 160e8983861SEric Paris mnt = ((struct path *)data)->mnt; 161e8983861SEric Paris if (mnt && !hlist_empty(&mnt->mnt_fsnotify_marks)) { 162e8983861SEric Paris spin_lock(&mnt->mnt_root->d_lock); 163e8983861SEric Paris hlist_for_each_entry(mark, node, &mnt->mnt_fsnotify_marks, m.m_list) { 164c908370fSEric Paris if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) 165e8983861SEric Paris mark->ignored_mask = 0; 166e8983861SEric Paris } 167e8983861SEric Paris spin_unlock(&mnt->mnt_root->d_lock); 168e8983861SEric Paris } 169e8983861SEric Paris } 170e8983861SEric Paris } 171e8983861SEric Paris 172c4ec54b4SEric Paris static int send_to_group(struct fsnotify_group *group, struct inode *to_tell, 1733a9fb89fSEric Paris struct vfsmount *mnt, __u32 mask, void *data, 17459b0df21SEric Paris int data_is, u32 cookie, const unsigned char *file_name, 1753a9fb89fSEric Paris struct fsnotify_event **event) 1767131485aSEric Paris { 1775ba08e2eSEric Paris pr_debug("%s: group=%p to_tell=%p mnt=%p mask=%x data=%p data_is=%d" 1785ba08e2eSEric Paris " cookie=%d event=%p\n", __func__, group, to_tell, mnt, 1795ba08e2eSEric Paris mask, data, data_is, cookie, *event); 1805ba08e2eSEric Paris 1813a9fb89fSEric Paris if (!group->ops->should_send_event(group, to_tell, mnt, mask, 1827131485aSEric Paris data, data_is)) 183c4ec54b4SEric Paris return 0; 1847131485aSEric Paris if (!*event) { 1857131485aSEric Paris *event = fsnotify_create_event(to_tell, mask, data, 1867131485aSEric Paris data_is, file_name, 1877131485aSEric Paris cookie, GFP_KERNEL); 1887131485aSEric Paris if (!*event) 189c4ec54b4SEric Paris return -ENOMEM; 1907131485aSEric Paris } 191c4ec54b4SEric Paris return group->ops->handle_event(group, *event); 1927131485aSEric Paris } 1937131485aSEric Paris 1943a9fb89fSEric Paris static bool needed_by_vfsmount(__u32 test_mask, struct vfsmount *mnt) 1957131485aSEric Paris { 1963a9fb89fSEric Paris if (!mnt) 1977131485aSEric Paris return false; 1987131485aSEric Paris 1992504c5d6SAndreas Gruenbacher return (test_mask & mnt->mnt_fsnotify_mask); 2007131485aSEric Paris } 201e8983861SEric Paris 202c28f7e56SEric Paris /* 20390586523SEric Paris * This is the main call to fsnotify. The VFS calls into hook specific functions 20490586523SEric Paris * in linux/fsnotify.h. Those functions then in turn call here. Here will call 20590586523SEric Paris * out to all of the registered fsnotify_group. Those groups can then use the 20690586523SEric Paris * notification event in whatever means they feel necessary. 20790586523SEric Paris */ 208c4ec54b4SEric Paris int fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is, 20959b0df21SEric Paris const unsigned char *file_name, u32 cookie) 21090586523SEric Paris { 21190586523SEric Paris struct fsnotify_group *group; 21290586523SEric Paris struct fsnotify_event *event = NULL; 2133a9fb89fSEric Paris struct vfsmount *mnt = NULL; 214c4ec54b4SEric Paris int idx, ret = 0; 215e42e2773SEric Paris /* global tests shouldn't care about events on child only the specific event */ 216e42e2773SEric Paris __u32 test_mask = (mask & ~FS_EVENT_ON_CHILD); 21790586523SEric Paris 2187131485aSEric Paris /* if no fsnotify listeners, nothing to do */ 2197131485aSEric Paris if (list_empty(&fsnotify_inode_groups) && 2207131485aSEric Paris list_empty(&fsnotify_vfsmount_groups)) 221c4ec54b4SEric Paris return 0; 22290586523SEric Paris 223e8983861SEric Paris if (mask & FS_MODIFY) 224e8983861SEric Paris __fsnotify_flush_ignored_mask(to_tell, data, data_is); 225e8983861SEric Paris 2267131485aSEric Paris /* if none of the directed listeners or vfsmount listeners care */ 2277131485aSEric Paris if (!(test_mask & fsnotify_inode_mask) && 2287131485aSEric Paris !(test_mask & fsnotify_vfsmount_mask)) 229c4ec54b4SEric Paris return 0; 23090586523SEric Paris 2313a9fb89fSEric Paris if (data_is == FSNOTIFY_EVENT_PATH) 2323a9fb89fSEric Paris mnt = ((struct path *)data)->mnt; 2333a9fb89fSEric Paris 2347131485aSEric Paris /* if this inode's directed listeners don't care and nothing on the vfsmount 2357131485aSEric Paris * listeners list cares, nothing to do */ 2367131485aSEric Paris if (!(test_mask & to_tell->i_fsnotify_mask) && 2373a9fb89fSEric Paris !needed_by_vfsmount(test_mask, mnt)) 238c4ec54b4SEric Paris return 0; 2397131485aSEric Paris 24090586523SEric Paris /* 24190586523SEric Paris * SRCU!! the groups list is very very much read only and the path is 24290586523SEric Paris * very hot. The VAST majority of events are not going to need to do 24390586523SEric Paris * anything other than walk the list so it's crazy to pre-allocate. 24490586523SEric Paris */ 24590586523SEric Paris idx = srcu_read_lock(&fsnotify_grp_srcu); 2467131485aSEric Paris 2477131485aSEric Paris if (test_mask & to_tell->i_fsnotify_mask) { 24819c2a0e1SEric Paris list_for_each_entry_rcu(group, &fsnotify_inode_groups, inode_group_list) { 249e42e2773SEric Paris if (test_mask & group->mask) { 250c4ec54b4SEric Paris ret = send_to_group(group, to_tell, NULL, mask, data, data_is, 2513a9fb89fSEric Paris cookie, file_name, &event); 252c4ec54b4SEric Paris if (ret) 253c4ec54b4SEric Paris goto out; 25490586523SEric Paris } 25590586523SEric Paris } 2567131485aSEric Paris } 2573a9fb89fSEric Paris if (needed_by_vfsmount(test_mask, mnt)) { 2587131485aSEric Paris list_for_each_entry_rcu(group, &fsnotify_vfsmount_groups, vfsmount_group_list) { 2597131485aSEric Paris if (test_mask & group->mask) { 260c4ec54b4SEric Paris ret = send_to_group(group, to_tell, mnt, mask, data, data_is, 2613a9fb89fSEric Paris cookie, file_name, &event); 262c4ec54b4SEric Paris if (ret) 263c4ec54b4SEric Paris goto out; 2647131485aSEric Paris } 2657131485aSEric Paris } 2667131485aSEric Paris } 267c4ec54b4SEric Paris out: 26890586523SEric Paris srcu_read_unlock(&fsnotify_grp_srcu, idx); 26990586523SEric Paris /* 27090586523SEric Paris * fsnotify_create_event() took a reference so the event can't be cleaned 27190586523SEric Paris * up while we are still trying to add it to lists, drop that one. 27290586523SEric Paris */ 27390586523SEric Paris if (event) 27490586523SEric Paris fsnotify_put_event(event); 275c4ec54b4SEric Paris 27698b5c10dSJean-Christophe Dubois return ret; 27790586523SEric Paris } 27890586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify); 27990586523SEric Paris 28090586523SEric Paris static __init int fsnotify_init(void) 28190586523SEric Paris { 28220dee624SEric Paris BUG_ON(hweight32(ALL_FSNOTIFY_EVENTS) != 23); 28320dee624SEric Paris 28490586523SEric Paris return init_srcu_struct(&fsnotify_grp_srcu); 28590586523SEric Paris } 28690586523SEric Paris subsys_initcall(fsnotify_init); 287