xref: /openbmc/linux/fs/notify/fsnotify.c (revision 62ffe5df)
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>
2190586523SEric Paris #include <linux/init.h>
2290586523SEric Paris #include <linux/module.h>
2390586523SEric Paris #include <linux/srcu.h>
2490586523SEric Paris 
2590586523SEric Paris #include <linux/fsnotify_backend.h>
2690586523SEric Paris #include "fsnotify.h"
2790586523SEric Paris 
2890586523SEric Paris /*
293be25f49SEric Paris  * Clear all of the marks on an inode when it is being evicted from core
303be25f49SEric Paris  */
313be25f49SEric Paris void __fsnotify_inode_delete(struct inode *inode)
323be25f49SEric Paris {
333be25f49SEric Paris 	fsnotify_clear_marks_by_inode(inode);
343be25f49SEric Paris }
353be25f49SEric Paris EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);
363be25f49SEric Paris 
373be25f49SEric Paris /*
38c28f7e56SEric Paris  * Given an inode, first check if we care what happens to our children.  Inotify
39c28f7e56SEric Paris  * and dnotify both tell their parents about events.  If we care about any event
40c28f7e56SEric Paris  * on a child we run all of our children and set a dentry flag saying that the
41c28f7e56SEric Paris  * parent cares.  Thus when an event happens on a child it can quickly tell if
42c28f7e56SEric Paris  * if there is a need to find a parent and send the event to the parent.
43c28f7e56SEric Paris  */
44c28f7e56SEric Paris void __fsnotify_update_child_dentry_flags(struct inode *inode)
45c28f7e56SEric Paris {
46c28f7e56SEric Paris 	struct dentry *alias;
47c28f7e56SEric Paris 	int watched;
48c28f7e56SEric Paris 
49c28f7e56SEric Paris 	if (!S_ISDIR(inode->i_mode))
50c28f7e56SEric Paris 		return;
51c28f7e56SEric Paris 
52c28f7e56SEric Paris 	/* determine if the children should tell inode about their events */
53c28f7e56SEric Paris 	watched = fsnotify_inode_watches_children(inode);
54c28f7e56SEric Paris 
55c28f7e56SEric Paris 	spin_lock(&dcache_lock);
56c28f7e56SEric Paris 	/* run all of the dentries associated with this inode.  Since this is a
57c28f7e56SEric Paris 	 * directory, there damn well better only be one item on this list */
58c28f7e56SEric Paris 	list_for_each_entry(alias, &inode->i_dentry, d_alias) {
59c28f7e56SEric Paris 		struct dentry *child;
60c28f7e56SEric Paris 
61c28f7e56SEric Paris 		/* run all of the children of the original inode and fix their
62c28f7e56SEric Paris 		 * d_flags to indicate parental interest (their parent is the
63c28f7e56SEric Paris 		 * original inode) */
64c28f7e56SEric Paris 		list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) {
65c28f7e56SEric Paris 			if (!child->d_inode)
66c28f7e56SEric Paris 				continue;
67c28f7e56SEric Paris 
68c28f7e56SEric Paris 			spin_lock(&child->d_lock);
69c28f7e56SEric Paris 			if (watched)
70c28f7e56SEric Paris 				child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
71c28f7e56SEric Paris 			else
72c28f7e56SEric Paris 				child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
73c28f7e56SEric Paris 			spin_unlock(&child->d_lock);
74c28f7e56SEric Paris 		}
75c28f7e56SEric Paris 	}
76c28f7e56SEric Paris 	spin_unlock(&dcache_lock);
77c28f7e56SEric Paris }
78c28f7e56SEric Paris 
79c28f7e56SEric Paris /* Notify this dentry's parent about a child's events. */
80c28f7e56SEric Paris void __fsnotify_parent(struct dentry *dentry, __u32 mask)
81c28f7e56SEric Paris {
82c28f7e56SEric Paris 	struct dentry *parent;
83c28f7e56SEric Paris 	struct inode *p_inode;
84c28f7e56SEric Paris 	bool send = false;
85c28f7e56SEric Paris 	bool should_update_children = false;
86c28f7e56SEric Paris 
87c28f7e56SEric Paris 	if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
88c28f7e56SEric Paris 		return;
89c28f7e56SEric Paris 
90c28f7e56SEric Paris 	spin_lock(&dentry->d_lock);
91c28f7e56SEric Paris 	parent = dentry->d_parent;
92c28f7e56SEric Paris 	p_inode = parent->d_inode;
93c28f7e56SEric Paris 
94c28f7e56SEric Paris 	if (fsnotify_inode_watches_children(p_inode)) {
95c28f7e56SEric Paris 		if (p_inode->i_fsnotify_mask & mask) {
96c28f7e56SEric Paris 			dget(parent);
97c28f7e56SEric Paris 			send = true;
98c28f7e56SEric Paris 		}
99c28f7e56SEric Paris 	} else {
100c28f7e56SEric Paris 		/*
101c28f7e56SEric Paris 		 * The parent doesn't care about events on it's children but
102c28f7e56SEric Paris 		 * at least one child thought it did.  We need to run all the
103c28f7e56SEric Paris 		 * children and update their d_flags to let them know p_inode
104c28f7e56SEric Paris 		 * doesn't care about them any more.
105c28f7e56SEric Paris 		 */
106c28f7e56SEric Paris 		dget(parent);
107c28f7e56SEric Paris 		should_update_children = true;
108c28f7e56SEric Paris 	}
109c28f7e56SEric Paris 
110c28f7e56SEric Paris 	spin_unlock(&dentry->d_lock);
111c28f7e56SEric Paris 
112c28f7e56SEric Paris 	if (send) {
113c28f7e56SEric Paris 		/* we are notifying a parent so come up with the new mask which
114c28f7e56SEric Paris 		 * specifies these are events which came from a child. */
115c28f7e56SEric Paris 		mask |= FS_EVENT_ON_CHILD;
116c28f7e56SEric Paris 
11762ffe5dfSEric Paris 		fsnotify(p_inode, mask, dentry->d_inode, FSNOTIFY_EVENT_INODE,
11862ffe5dfSEric Paris 			 dentry->d_name.name);
119c28f7e56SEric Paris 		dput(parent);
120c28f7e56SEric Paris 	}
121c28f7e56SEric Paris 
122c28f7e56SEric Paris 	if (unlikely(should_update_children)) {
123c28f7e56SEric Paris 		__fsnotify_update_child_dentry_flags(p_inode);
124c28f7e56SEric Paris 		dput(parent);
125c28f7e56SEric Paris 	}
126c28f7e56SEric Paris }
127c28f7e56SEric Paris EXPORT_SYMBOL_GPL(__fsnotify_parent);
128c28f7e56SEric Paris 
129c28f7e56SEric Paris /*
13090586523SEric Paris  * This is the main call to fsnotify.  The VFS calls into hook specific functions
13190586523SEric Paris  * in linux/fsnotify.h.  Those functions then in turn call here.  Here will call
13290586523SEric Paris  * out to all of the registered fsnotify_group.  Those groups can then use the
13390586523SEric Paris  * notification event in whatever means they feel necessary.
13490586523SEric Paris  */
13562ffe5dfSEric Paris void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is, const char *file_name)
13690586523SEric Paris {
13790586523SEric Paris 	struct fsnotify_group *group;
13890586523SEric Paris 	struct fsnotify_event *event = NULL;
13990586523SEric Paris 	int idx;
14090586523SEric Paris 
14190586523SEric Paris 	if (list_empty(&fsnotify_groups))
14290586523SEric Paris 		return;
14390586523SEric Paris 
14490586523SEric Paris 	if (!(mask & fsnotify_mask))
14590586523SEric Paris 		return;
14690586523SEric Paris 
1473be25f49SEric Paris 	if (!(mask & to_tell->i_fsnotify_mask))
1483be25f49SEric Paris 		return;
14990586523SEric Paris 	/*
15090586523SEric Paris 	 * SRCU!!  the groups list is very very much read only and the path is
15190586523SEric Paris 	 * very hot.  The VAST majority of events are not going to need to do
15290586523SEric Paris 	 * anything other than walk the list so it's crazy to pre-allocate.
15390586523SEric Paris 	 */
15490586523SEric Paris 	idx = srcu_read_lock(&fsnotify_grp_srcu);
15590586523SEric Paris 	list_for_each_entry_rcu(group, &fsnotify_groups, group_list) {
15690586523SEric Paris 		if (mask & group->mask) {
1573be25f49SEric Paris 			if (!group->ops->should_send_event(group, to_tell, mask))
1583be25f49SEric Paris 				continue;
15990586523SEric Paris 			if (!event) {
16062ffe5dfSEric Paris 				event = fsnotify_create_event(to_tell, mask, data, data_is, file_name);
16190586523SEric Paris 				/* shit, we OOM'd and now we can't tell, maybe
16290586523SEric Paris 				 * someday someone else will want to do something
16390586523SEric Paris 				 * here */
16490586523SEric Paris 				if (!event)
16590586523SEric Paris 					break;
16690586523SEric Paris 			}
16790586523SEric Paris 			group->ops->handle_event(group, event);
16890586523SEric Paris 		}
16990586523SEric Paris 	}
17090586523SEric Paris 	srcu_read_unlock(&fsnotify_grp_srcu, idx);
17190586523SEric Paris 	/*
17290586523SEric Paris 	 * fsnotify_create_event() took a reference so the event can't be cleaned
17390586523SEric Paris 	 * up while we are still trying to add it to lists, drop that one.
17490586523SEric Paris 	 */
17590586523SEric Paris 	if (event)
17690586523SEric Paris 		fsnotify_put_event(event);
17790586523SEric Paris }
17890586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify);
17990586523SEric Paris 
18090586523SEric Paris static __init int fsnotify_init(void)
18190586523SEric Paris {
18290586523SEric Paris 	return init_srcu_struct(&fsnotify_grp_srcu);
18390586523SEric Paris }
18490586523SEric Paris subsys_initcall(fsnotify_init);
185