xref: /openbmc/linux/fs/notify/fsnotify.c (revision 72acc854)
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 
393be25f49SEric Paris /*
40c28f7e56SEric Paris  * Given an inode, first check if we care what happens to our children.  Inotify
41c28f7e56SEric Paris  * and dnotify both tell their parents about events.  If we care about any event
42c28f7e56SEric Paris  * on a child we run all of our children and set a dentry flag saying that the
43c28f7e56SEric Paris  * parent cares.  Thus when an event happens on a child it can quickly tell if
44c28f7e56SEric Paris  * if there is a need to find a parent and send the event to the parent.
45c28f7e56SEric Paris  */
46c28f7e56SEric Paris void __fsnotify_update_child_dentry_flags(struct inode *inode)
47c28f7e56SEric Paris {
48c28f7e56SEric Paris 	struct dentry *alias;
49c28f7e56SEric Paris 	int watched;
50c28f7e56SEric Paris 
51c28f7e56SEric Paris 	if (!S_ISDIR(inode->i_mode))
52c28f7e56SEric Paris 		return;
53c28f7e56SEric Paris 
54c28f7e56SEric Paris 	/* determine if the children should tell inode about their events */
55c28f7e56SEric Paris 	watched = fsnotify_inode_watches_children(inode);
56c28f7e56SEric Paris 
57c28f7e56SEric Paris 	spin_lock(&dcache_lock);
58c28f7e56SEric Paris 	/* run all of the dentries associated with this inode.  Since this is a
59c28f7e56SEric Paris 	 * directory, there damn well better only be one item on this list */
60c28f7e56SEric Paris 	list_for_each_entry(alias, &inode->i_dentry, d_alias) {
61c28f7e56SEric Paris 		struct dentry *child;
62c28f7e56SEric Paris 
63c28f7e56SEric Paris 		/* run all of the children of the original inode and fix their
64c28f7e56SEric Paris 		 * d_flags to indicate parental interest (their parent is the
65c28f7e56SEric Paris 		 * original inode) */
66c28f7e56SEric Paris 		list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) {
67c28f7e56SEric Paris 			if (!child->d_inode)
68c28f7e56SEric Paris 				continue;
69c28f7e56SEric Paris 
70c28f7e56SEric Paris 			spin_lock(&child->d_lock);
71c28f7e56SEric Paris 			if (watched)
72c28f7e56SEric Paris 				child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
73c28f7e56SEric Paris 			else
74c28f7e56SEric Paris 				child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
75c28f7e56SEric Paris 			spin_unlock(&child->d_lock);
76c28f7e56SEric Paris 		}
77c28f7e56SEric Paris 	}
78c28f7e56SEric Paris 	spin_unlock(&dcache_lock);
79c28f7e56SEric Paris }
80c28f7e56SEric Paris 
81c28f7e56SEric Paris /* Notify this dentry's parent about a child's events. */
8272acc854SAndreas Gruenbacher void __fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
83c28f7e56SEric Paris {
84c28f7e56SEric Paris 	struct dentry *parent;
85c28f7e56SEric Paris 	struct inode *p_inode;
86c28f7e56SEric Paris 	bool send = false;
87c28f7e56SEric Paris 	bool should_update_children = false;
88c28f7e56SEric Paris 
8972acc854SAndreas Gruenbacher 	if (!dentry)
9072acc854SAndreas Gruenbacher 		dentry = path->dentry;
9128c60e37SEric Paris 
92c28f7e56SEric Paris 	if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
93c28f7e56SEric Paris 		return;
94c28f7e56SEric Paris 
95c28f7e56SEric Paris 	spin_lock(&dentry->d_lock);
96c28f7e56SEric Paris 	parent = dentry->d_parent;
97c28f7e56SEric Paris 	p_inode = parent->d_inode;
98c28f7e56SEric Paris 
99c28f7e56SEric Paris 	if (fsnotify_inode_watches_children(p_inode)) {
100c28f7e56SEric Paris 		if (p_inode->i_fsnotify_mask & mask) {
101c28f7e56SEric Paris 			dget(parent);
102c28f7e56SEric Paris 			send = true;
103c28f7e56SEric Paris 		}
104c28f7e56SEric Paris 	} else {
105c28f7e56SEric Paris 		/*
106c28f7e56SEric Paris 		 * The parent doesn't care about events on it's children but
107c28f7e56SEric Paris 		 * at least one child thought it did.  We need to run all the
108c28f7e56SEric Paris 		 * children and update their d_flags to let them know p_inode
109c28f7e56SEric Paris 		 * doesn't care about them any more.
110c28f7e56SEric Paris 		 */
111c28f7e56SEric Paris 		dget(parent);
112c28f7e56SEric Paris 		should_update_children = true;
113c28f7e56SEric Paris 	}
114c28f7e56SEric Paris 
115c28f7e56SEric Paris 	spin_unlock(&dentry->d_lock);
116c28f7e56SEric Paris 
117c28f7e56SEric Paris 	if (send) {
118c28f7e56SEric Paris 		/* we are notifying a parent so come up with the new mask which
119c28f7e56SEric Paris 		 * specifies these are events which came from a child. */
120c28f7e56SEric Paris 		mask |= FS_EVENT_ON_CHILD;
121c28f7e56SEric Paris 
12272acc854SAndreas Gruenbacher 		if (path)
12372acc854SAndreas Gruenbacher 			fsnotify(p_inode, mask, path, FSNOTIFY_EVENT_PATH,
12428c60e37SEric Paris 				 dentry->d_name.name, 0);
12528c60e37SEric Paris 		else
12662ffe5dfSEric Paris 			fsnotify(p_inode, mask, dentry->d_inode, FSNOTIFY_EVENT_INODE,
12747882c6fSEric Paris 				 dentry->d_name.name, 0);
128c28f7e56SEric Paris 		dput(parent);
129c28f7e56SEric Paris 	}
130c28f7e56SEric Paris 
131c28f7e56SEric Paris 	if (unlikely(should_update_children)) {
132c28f7e56SEric Paris 		__fsnotify_update_child_dentry_flags(p_inode);
133c28f7e56SEric Paris 		dput(parent);
134c28f7e56SEric Paris 	}
135c28f7e56SEric Paris }
136c28f7e56SEric Paris EXPORT_SYMBOL_GPL(__fsnotify_parent);
137c28f7e56SEric Paris 
1383a9fb89fSEric Paris static void send_to_group(struct fsnotify_group *group, struct inode *to_tell,
1393a9fb89fSEric Paris 			  struct vfsmount *mnt, __u32 mask, void *data,
1403a9fb89fSEric Paris 			  int data_is, u32 cookie, const char *file_name,
1413a9fb89fSEric Paris 			  struct fsnotify_event **event)
1427131485aSEric Paris {
1433a9fb89fSEric Paris 	if (!group->ops->should_send_event(group, to_tell, mnt, mask,
1447131485aSEric Paris 					   data, data_is))
1457131485aSEric Paris 		return;
1467131485aSEric Paris 	if (!*event) {
1477131485aSEric Paris 		*event = fsnotify_create_event(to_tell, mask, data,
1487131485aSEric Paris 						data_is, file_name,
1497131485aSEric Paris 						cookie, GFP_KERNEL);
1507131485aSEric Paris 		/*
1517131485aSEric Paris 		 * shit, we OOM'd and now we can't tell, maybe
1527131485aSEric Paris 		 * someday someone else will want to do something
1537131485aSEric Paris 		 * here
1547131485aSEric Paris 		 */
1557131485aSEric Paris 		if (!*event)
1567131485aSEric Paris 			return;
1577131485aSEric Paris 	}
1587131485aSEric Paris 	group->ops->handle_event(group, *event);
1597131485aSEric Paris }
1607131485aSEric Paris 
1613a9fb89fSEric Paris static bool needed_by_vfsmount(__u32 test_mask, struct vfsmount *mnt)
1627131485aSEric Paris {
1633a9fb89fSEric Paris 	if (!mnt)
1647131485aSEric Paris 		return false;
1657131485aSEric Paris 
1667131485aSEric Paris 	/* hook in this when mnt->mnt_fsnotify_mask is defined */
1677131485aSEric Paris 	/* return (test_mask & path->mnt->mnt_fsnotify_mask); */
1687131485aSEric Paris 	return false;
1697131485aSEric Paris }
170c28f7e56SEric Paris /*
17190586523SEric Paris  * This is the main call to fsnotify.  The VFS calls into hook specific functions
17290586523SEric Paris  * in linux/fsnotify.h.  Those functions then in turn call here.  Here will call
17390586523SEric Paris  * out to all of the registered fsnotify_group.  Those groups can then use the
17490586523SEric Paris  * notification event in whatever means they feel necessary.
17590586523SEric Paris  */
17647882c6fSEric Paris void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is, const char *file_name, u32 cookie)
17790586523SEric Paris {
17890586523SEric Paris 	struct fsnotify_group *group;
17990586523SEric Paris 	struct fsnotify_event *event = NULL;
1803a9fb89fSEric Paris 	struct vfsmount *mnt = NULL;
18190586523SEric Paris 	int idx;
182e42e2773SEric Paris 	/* global tests shouldn't care about events on child only the specific event */
183e42e2773SEric Paris 	__u32 test_mask = (mask & ~FS_EVENT_ON_CHILD);
18490586523SEric Paris 
1857131485aSEric Paris 	/* if no fsnotify listeners, nothing to do */
1867131485aSEric Paris 	if (list_empty(&fsnotify_inode_groups) &&
1877131485aSEric Paris 	    list_empty(&fsnotify_vfsmount_groups))
18890586523SEric Paris                 return;
18990586523SEric Paris 
1907131485aSEric Paris 	/* if none of the directed listeners or vfsmount listeners care */
1917131485aSEric Paris 	if (!(test_mask & fsnotify_inode_mask) &&
1927131485aSEric Paris 	    !(test_mask & fsnotify_vfsmount_mask))
19390586523SEric Paris                 return;
19490586523SEric Paris 
1953a9fb89fSEric Paris 	if (data_is == FSNOTIFY_EVENT_PATH)
1963a9fb89fSEric Paris 		mnt = ((struct path *)data)->mnt;
1973a9fb89fSEric Paris 
1987131485aSEric Paris 	/* if this inode's directed listeners don't care and nothing on the vfsmount
1997131485aSEric Paris 	 * listeners list cares, nothing to do */
2007131485aSEric Paris 	if (!(test_mask & to_tell->i_fsnotify_mask) &&
2013a9fb89fSEric Paris 	    !needed_by_vfsmount(test_mask, mnt))
2023be25f49SEric Paris                 return;
2037131485aSEric Paris 
20490586523SEric Paris 	/*
20590586523SEric Paris 	 * SRCU!!  the groups list is very very much read only and the path is
20690586523SEric Paris 	 * very hot.  The VAST majority of events are not going to need to do
20790586523SEric Paris 	 * anything other than walk the list so it's crazy to pre-allocate.
20890586523SEric Paris 	 */
20990586523SEric Paris 	idx = srcu_read_lock(&fsnotify_grp_srcu);
2107131485aSEric Paris 
2117131485aSEric Paris 	if (test_mask & to_tell->i_fsnotify_mask) {
21219c2a0e1SEric Paris 		list_for_each_entry_rcu(group, &fsnotify_inode_groups, inode_group_list) {
213e42e2773SEric Paris 			if (test_mask & group->mask) {
2143a9fb89fSEric Paris 				send_to_group(group, to_tell, NULL, mask, data, data_is,
2153a9fb89fSEric Paris 					      cookie, file_name, &event);
21690586523SEric Paris 			}
21790586523SEric Paris 		}
2187131485aSEric Paris 	}
2193a9fb89fSEric Paris 	if (needed_by_vfsmount(test_mask, mnt)) {
2207131485aSEric Paris 		list_for_each_entry_rcu(group, &fsnotify_vfsmount_groups, vfsmount_group_list) {
2217131485aSEric Paris 			if (test_mask & group->mask) {
2223a9fb89fSEric Paris 				send_to_group(group, to_tell, mnt, mask, data, data_is,
2233a9fb89fSEric Paris 					      cookie, file_name, &event);
2247131485aSEric Paris 			}
2257131485aSEric Paris 		}
2267131485aSEric Paris 	}
2277131485aSEric Paris 
22890586523SEric Paris 	srcu_read_unlock(&fsnotify_grp_srcu, idx);
22990586523SEric Paris 	/*
23090586523SEric Paris 	 * fsnotify_create_event() took a reference so the event can't be cleaned
23190586523SEric Paris 	 * up while we are still trying to add it to lists, drop that one.
23290586523SEric Paris 	 */
23390586523SEric Paris 	if (event)
23490586523SEric Paris 		fsnotify_put_event(event);
23590586523SEric Paris }
23690586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify);
23790586523SEric Paris 
23890586523SEric Paris static __init int fsnotify_init(void)
23990586523SEric Paris {
24090586523SEric Paris 	return init_srcu_struct(&fsnotify_grp_srcu);
24190586523SEric Paris }
24290586523SEric Paris subsys_initcall(fsnotify_init);
243