xref: /openbmc/linux/fs/notify/fsnotify.c (revision 98b5c10d)
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 {
1773a9fb89fSEric Paris 	if (!group->ops->should_send_event(group, to_tell, mnt, mask,
1787131485aSEric Paris 					   data, data_is))
179c4ec54b4SEric Paris 		return 0;
1807131485aSEric Paris 	if (!*event) {
1817131485aSEric Paris 		*event = fsnotify_create_event(to_tell, mask, data,
1827131485aSEric Paris 						data_is, file_name,
1837131485aSEric Paris 						cookie, GFP_KERNEL);
1847131485aSEric Paris 		if (!*event)
185c4ec54b4SEric Paris 			return -ENOMEM;
1867131485aSEric Paris 	}
187c4ec54b4SEric Paris 	return group->ops->handle_event(group, *event);
1887131485aSEric Paris }
1897131485aSEric Paris 
1903a9fb89fSEric Paris static bool needed_by_vfsmount(__u32 test_mask, struct vfsmount *mnt)
1917131485aSEric Paris {
1923a9fb89fSEric Paris 	if (!mnt)
1937131485aSEric Paris 		return false;
1947131485aSEric Paris 
1952504c5d6SAndreas Gruenbacher 	return (test_mask & mnt->mnt_fsnotify_mask);
1967131485aSEric Paris }
197e8983861SEric Paris 
198c28f7e56SEric Paris /*
19990586523SEric Paris  * This is the main call to fsnotify.  The VFS calls into hook specific functions
20090586523SEric Paris  * in linux/fsnotify.h.  Those functions then in turn call here.  Here will call
20190586523SEric Paris  * out to all of the registered fsnotify_group.  Those groups can then use the
20290586523SEric Paris  * notification event in whatever means they feel necessary.
20390586523SEric Paris  */
204c4ec54b4SEric Paris int fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is,
20559b0df21SEric Paris 	     const unsigned char *file_name, u32 cookie)
20690586523SEric Paris {
20790586523SEric Paris 	struct fsnotify_group *group;
20890586523SEric Paris 	struct fsnotify_event *event = NULL;
2093a9fb89fSEric Paris 	struct vfsmount *mnt = NULL;
210c4ec54b4SEric Paris 	int idx, ret = 0;
211e42e2773SEric Paris 	/* global tests shouldn't care about events on child only the specific event */
212e42e2773SEric Paris 	__u32 test_mask = (mask & ~FS_EVENT_ON_CHILD);
21390586523SEric Paris 
2147131485aSEric Paris 	/* if no fsnotify listeners, nothing to do */
2157131485aSEric Paris 	if (list_empty(&fsnotify_inode_groups) &&
2167131485aSEric Paris 	    list_empty(&fsnotify_vfsmount_groups))
217c4ec54b4SEric Paris 		return 0;
21890586523SEric Paris 
219e8983861SEric Paris 	if (mask & FS_MODIFY)
220e8983861SEric Paris 		__fsnotify_flush_ignored_mask(to_tell, data, data_is);
221e8983861SEric Paris 
2227131485aSEric Paris 	/* if none of the directed listeners or vfsmount listeners care */
2237131485aSEric Paris 	if (!(test_mask & fsnotify_inode_mask) &&
2247131485aSEric Paris 	    !(test_mask & fsnotify_vfsmount_mask))
225c4ec54b4SEric Paris 		return 0;
22690586523SEric Paris 
2273a9fb89fSEric Paris 	if (data_is == FSNOTIFY_EVENT_PATH)
2283a9fb89fSEric Paris 		mnt = ((struct path *)data)->mnt;
2293a9fb89fSEric Paris 
2307131485aSEric Paris 	/* if this inode's directed listeners don't care and nothing on the vfsmount
2317131485aSEric Paris 	 * listeners list cares, nothing to do */
2327131485aSEric Paris 	if (!(test_mask & to_tell->i_fsnotify_mask) &&
2333a9fb89fSEric Paris 	    !needed_by_vfsmount(test_mask, mnt))
234c4ec54b4SEric Paris 		return 0;
2357131485aSEric Paris 
23690586523SEric Paris 	/*
23790586523SEric Paris 	 * SRCU!!  the groups list is very very much read only and the path is
23890586523SEric Paris 	 * very hot.  The VAST majority of events are not going to need to do
23990586523SEric Paris 	 * anything other than walk the list so it's crazy to pre-allocate.
24090586523SEric Paris 	 */
24190586523SEric Paris 	idx = srcu_read_lock(&fsnotify_grp_srcu);
2427131485aSEric Paris 
2437131485aSEric Paris 	if (test_mask & to_tell->i_fsnotify_mask) {
24419c2a0e1SEric Paris 		list_for_each_entry_rcu(group, &fsnotify_inode_groups, inode_group_list) {
245e42e2773SEric Paris 			if (test_mask & group->mask) {
246c4ec54b4SEric Paris 				ret = send_to_group(group, to_tell, NULL, mask, data, data_is,
2473a9fb89fSEric Paris 						    cookie, file_name, &event);
248c4ec54b4SEric Paris 				if (ret)
249c4ec54b4SEric Paris 					goto out;
25090586523SEric Paris 			}
25190586523SEric Paris 		}
2527131485aSEric Paris 	}
2533a9fb89fSEric Paris 	if (needed_by_vfsmount(test_mask, mnt)) {
2547131485aSEric Paris 		list_for_each_entry_rcu(group, &fsnotify_vfsmount_groups, vfsmount_group_list) {
2557131485aSEric Paris 			if (test_mask & group->mask) {
256c4ec54b4SEric Paris 				ret = send_to_group(group, to_tell, mnt, mask, data, data_is,
2573a9fb89fSEric Paris 						    cookie, file_name, &event);
258c4ec54b4SEric Paris 				if (ret)
259c4ec54b4SEric Paris 					goto out;
2607131485aSEric Paris 			}
2617131485aSEric Paris 		}
2627131485aSEric Paris 	}
263c4ec54b4SEric Paris out:
26490586523SEric Paris 	srcu_read_unlock(&fsnotify_grp_srcu, idx);
26590586523SEric Paris 	/*
26690586523SEric Paris 	 * fsnotify_create_event() took a reference so the event can't be cleaned
26790586523SEric Paris 	 * up while we are still trying to add it to lists, drop that one.
26890586523SEric Paris 	 */
26990586523SEric Paris 	if (event)
27090586523SEric Paris 		fsnotify_put_event(event);
271c4ec54b4SEric Paris 
27298b5c10dSJean-Christophe Dubois 	return ret;
27390586523SEric Paris }
27490586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify);
27590586523SEric Paris 
27690586523SEric Paris static __init int fsnotify_init(void)
27790586523SEric Paris {
27890586523SEric Paris 	return init_srcu_struct(&fsnotify_grp_srcu);
27990586523SEric Paris }
28090586523SEric Paris subsys_initcall(fsnotify_init);
281