xref: /openbmc/linux/fs/notify/fsnotify.c (revision c82ee6d3)
1c82ee6d3SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
290586523SEric Paris /*
390586523SEric Paris  *  Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
490586523SEric Paris  */
590586523SEric Paris 
690586523SEric Paris #include <linux/dcache.h>
790586523SEric Paris #include <linux/fs.h>
85a0e3ad6STejun Heo #include <linux/gfp.h>
990586523SEric Paris #include <linux/init.h>
1090586523SEric Paris #include <linux/module.h>
117131485aSEric Paris #include <linux/mount.h>
1290586523SEric Paris #include <linux/srcu.h>
1390586523SEric Paris 
1490586523SEric Paris #include <linux/fsnotify_backend.h>
1590586523SEric Paris #include "fsnotify.h"
1690586523SEric Paris 
1790586523SEric Paris /*
183be25f49SEric Paris  * Clear all of the marks on an inode when it is being evicted from core
193be25f49SEric Paris  */
203be25f49SEric Paris void __fsnotify_inode_delete(struct inode *inode)
213be25f49SEric Paris {
223be25f49SEric Paris 	fsnotify_clear_marks_by_inode(inode);
233be25f49SEric Paris }
243be25f49SEric Paris EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);
253be25f49SEric Paris 
26ca9c726eSAndreas Gruenbacher void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
27ca9c726eSAndreas Gruenbacher {
28ca9c726eSAndreas Gruenbacher 	fsnotify_clear_marks_by_mount(mnt);
29ca9c726eSAndreas Gruenbacher }
30ca9c726eSAndreas Gruenbacher 
31ebb3b47eSJan Kara /**
32ebb3b47eSJan Kara  * fsnotify_unmount_inodes - an sb is unmounting.  handle any watched inodes.
33ebb3b47eSJan Kara  * @sb: superblock being unmounted.
34ebb3b47eSJan Kara  *
35ebb3b47eSJan Kara  * Called during unmount with no locks held, so needs to be safe against
36ebb3b47eSJan Kara  * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block.
37ebb3b47eSJan Kara  */
381e6cb723SAmir Goldstein static void fsnotify_unmount_inodes(struct super_block *sb)
39ebb3b47eSJan Kara {
40ebb3b47eSJan Kara 	struct inode *inode, *iput_inode = NULL;
41ebb3b47eSJan Kara 
42ebb3b47eSJan Kara 	spin_lock(&sb->s_inode_list_lock);
43ebb3b47eSJan Kara 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
44ebb3b47eSJan Kara 		/*
45ebb3b47eSJan Kara 		 * We cannot __iget() an inode in state I_FREEING,
46ebb3b47eSJan Kara 		 * I_WILL_FREE, or I_NEW which is fine because by that point
47ebb3b47eSJan Kara 		 * the inode cannot have any associated watches.
48ebb3b47eSJan Kara 		 */
49ebb3b47eSJan Kara 		spin_lock(&inode->i_lock);
50ebb3b47eSJan Kara 		if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
51ebb3b47eSJan Kara 			spin_unlock(&inode->i_lock);
52ebb3b47eSJan Kara 			continue;
53ebb3b47eSJan Kara 		}
54ebb3b47eSJan Kara 
55ebb3b47eSJan Kara 		/*
56ebb3b47eSJan Kara 		 * If i_count is zero, the inode cannot have any watches and
571751e8a6SLinus Torvalds 		 * doing an __iget/iput with SB_ACTIVE clear would actually
58ebb3b47eSJan Kara 		 * evict all inodes with zero i_count from icache which is
59ebb3b47eSJan Kara 		 * unnecessarily violent and may in fact be illegal to do.
60ebb3b47eSJan Kara 		 */
61ebb3b47eSJan Kara 		if (!atomic_read(&inode->i_count)) {
62ebb3b47eSJan Kara 			spin_unlock(&inode->i_lock);
63ebb3b47eSJan Kara 			continue;
64ebb3b47eSJan Kara 		}
65ebb3b47eSJan Kara 
66ebb3b47eSJan Kara 		__iget(inode);
67ebb3b47eSJan Kara 		spin_unlock(&inode->i_lock);
68ebb3b47eSJan Kara 		spin_unlock(&sb->s_inode_list_lock);
69ebb3b47eSJan Kara 
70ebb3b47eSJan Kara 		if (iput_inode)
71ebb3b47eSJan Kara 			iput(iput_inode);
72ebb3b47eSJan Kara 
73ebb3b47eSJan Kara 		/* for each watch, send FS_UNMOUNT and then remove it */
74ebb3b47eSJan Kara 		fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
75ebb3b47eSJan Kara 
76ebb3b47eSJan Kara 		fsnotify_inode_delete(inode);
77ebb3b47eSJan Kara 
78ebb3b47eSJan Kara 		iput_inode = inode;
79ebb3b47eSJan Kara 
80ebb3b47eSJan Kara 		spin_lock(&sb->s_inode_list_lock);
81ebb3b47eSJan Kara 	}
82ebb3b47eSJan Kara 	spin_unlock(&sb->s_inode_list_lock);
83ebb3b47eSJan Kara 
84ebb3b47eSJan Kara 	if (iput_inode)
85ebb3b47eSJan Kara 		iput(iput_inode);
86721fb6fbSJan Kara 	/* Wait for outstanding inode references from connectors */
87721fb6fbSJan Kara 	wait_var_event(&sb->s_fsnotify_inode_refs,
88721fb6fbSJan Kara 		       !atomic_long_read(&sb->s_fsnotify_inode_refs));
89ebb3b47eSJan Kara }
90ebb3b47eSJan Kara 
911e6cb723SAmir Goldstein void fsnotify_sb_delete(struct super_block *sb)
921e6cb723SAmir Goldstein {
931e6cb723SAmir Goldstein 	fsnotify_unmount_inodes(sb);
941e6cb723SAmir Goldstein 	fsnotify_clear_marks_by_sb(sb);
951e6cb723SAmir Goldstein }
961e6cb723SAmir Goldstein 
973be25f49SEric Paris /*
984d8e7055SAmir Goldstein  * fsnotify_nameremove - a filename was removed from a directory
994d8e7055SAmir Goldstein  *
1004d8e7055SAmir Goldstein  * This is mostly called under parent vfs inode lock so name and
1014d8e7055SAmir Goldstein  * dentry->d_parent should be stable. However there are some corner cases where
1024d8e7055SAmir Goldstein  * inode lock is not held. So to be on the safe side and be reselient to future
1034d8e7055SAmir Goldstein  * callers and out of tree users of d_delete(), we do not assume that d_parent
1044d8e7055SAmir Goldstein  * and d_name are stable and we use dget_parent() and
1054d8e7055SAmir Goldstein  * take_dentry_name_snapshot() to grab stable references.
1064d8e7055SAmir Goldstein  */
1074d8e7055SAmir Goldstein void fsnotify_nameremove(struct dentry *dentry, int isdir)
1084d8e7055SAmir Goldstein {
1094d8e7055SAmir Goldstein 	struct dentry *parent;
1104d8e7055SAmir Goldstein 	struct name_snapshot name;
1114d8e7055SAmir Goldstein 	__u32 mask = FS_DELETE;
1124d8e7055SAmir Goldstein 
1134d8e7055SAmir Goldstein 	/* d_delete() of pseudo inode? (e.g. __ns_get_path() playing tricks) */
1144d8e7055SAmir Goldstein 	if (IS_ROOT(dentry))
1154d8e7055SAmir Goldstein 		return;
1164d8e7055SAmir Goldstein 
1174d8e7055SAmir Goldstein 	if (isdir)
1184d8e7055SAmir Goldstein 		mask |= FS_ISDIR;
1194d8e7055SAmir Goldstein 
1204d8e7055SAmir Goldstein 	parent = dget_parent(dentry);
1214d8e7055SAmir Goldstein 	/* Avoid unneeded take_dentry_name_snapshot() */
1224d8e7055SAmir Goldstein 	if (!(d_inode(parent)->i_fsnotify_mask & FS_DELETE) &&
1234d8e7055SAmir Goldstein 	    !(dentry->d_sb->s_fsnotify_mask & FS_DELETE))
1244d8e7055SAmir Goldstein 		goto out_dput;
1254d8e7055SAmir Goldstein 
1264d8e7055SAmir Goldstein 	take_dentry_name_snapshot(&name, dentry);
1274d8e7055SAmir Goldstein 
1284d8e7055SAmir Goldstein 	fsnotify(d_inode(parent), mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
129d4c60811SLinus Torvalds 		 &name.name, 0);
1304d8e7055SAmir Goldstein 
1314d8e7055SAmir Goldstein 	release_dentry_name_snapshot(&name);
1324d8e7055SAmir Goldstein 
1334d8e7055SAmir Goldstein out_dput:
1344d8e7055SAmir Goldstein 	dput(parent);
1354d8e7055SAmir Goldstein }
1364d8e7055SAmir Goldstein EXPORT_SYMBOL(fsnotify_nameremove);
1374d8e7055SAmir Goldstein 
1384d8e7055SAmir Goldstein /*
139c28f7e56SEric Paris  * Given an inode, first check if we care what happens to our children.  Inotify
140c28f7e56SEric Paris  * and dnotify both tell their parents about events.  If we care about any event
141c28f7e56SEric Paris  * on a child we run all of our children and set a dentry flag saying that the
142c28f7e56SEric Paris  * parent cares.  Thus when an event happens on a child it can quickly tell if
143c28f7e56SEric Paris  * if there is a need to find a parent and send the event to the parent.
144c28f7e56SEric Paris  */
145c28f7e56SEric Paris void __fsnotify_update_child_dentry_flags(struct inode *inode)
146c28f7e56SEric Paris {
147c28f7e56SEric Paris 	struct dentry *alias;
148c28f7e56SEric Paris 	int watched;
149c28f7e56SEric Paris 
150c28f7e56SEric Paris 	if (!S_ISDIR(inode->i_mode))
151c28f7e56SEric Paris 		return;
152c28f7e56SEric Paris 
153c28f7e56SEric Paris 	/* determine if the children should tell inode about their events */
154c28f7e56SEric Paris 	watched = fsnotify_inode_watches_children(inode);
155c28f7e56SEric Paris 
156873feea0SNick Piggin 	spin_lock(&inode->i_lock);
157c28f7e56SEric Paris 	/* run all of the dentries associated with this inode.  Since this is a
158c28f7e56SEric Paris 	 * directory, there damn well better only be one item on this list */
159946e51f2SAl Viro 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
160c28f7e56SEric Paris 		struct dentry *child;
161c28f7e56SEric Paris 
162c28f7e56SEric Paris 		/* run all of the children of the original inode and fix their
163c28f7e56SEric Paris 		 * d_flags to indicate parental interest (their parent is the
164c28f7e56SEric Paris 		 * original inode) */
1652fd6b7f5SNick Piggin 		spin_lock(&alias->d_lock);
166946e51f2SAl Viro 		list_for_each_entry(child, &alias->d_subdirs, d_child) {
167c28f7e56SEric Paris 			if (!child->d_inode)
168c28f7e56SEric Paris 				continue;
169c28f7e56SEric Paris 
1702fd6b7f5SNick Piggin 			spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
171c28f7e56SEric Paris 			if (watched)
172c28f7e56SEric Paris 				child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
173c28f7e56SEric Paris 			else
174c28f7e56SEric Paris 				child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
175c28f7e56SEric Paris 			spin_unlock(&child->d_lock);
176c28f7e56SEric Paris 		}
1772fd6b7f5SNick Piggin 		spin_unlock(&alias->d_lock);
178c28f7e56SEric Paris 	}
179873feea0SNick Piggin 	spin_unlock(&inode->i_lock);
180c28f7e56SEric Paris }
181c28f7e56SEric Paris 
182c28f7e56SEric Paris /* Notify this dentry's parent about a child's events. */
18312c7f9dcSAl Viro int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask)
184c28f7e56SEric Paris {
185c28f7e56SEric Paris 	struct dentry *parent;
186c28f7e56SEric Paris 	struct inode *p_inode;
18752420392SEric Paris 	int ret = 0;
188c28f7e56SEric Paris 
18972acc854SAndreas Gruenbacher 	if (!dentry)
1902069601bSLinus Torvalds 		dentry = path->dentry;
19128c60e37SEric Paris 
192c28f7e56SEric Paris 	if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
19352420392SEric Paris 		return 0;
194c28f7e56SEric Paris 
1954d4eb366SChristoph Hellwig 	parent = dget_parent(dentry);
196c28f7e56SEric Paris 	p_inode = parent->d_inode;
197c28f7e56SEric Paris 
198b469e7e4SAmir Goldstein 	if (unlikely(!fsnotify_inode_watches_children(p_inode))) {
1994d4eb366SChristoph Hellwig 		__fsnotify_update_child_dentry_flags(p_inode);
200b469e7e4SAmir Goldstein 	} else if (p_inode->i_fsnotify_mask & mask & ALL_FSNOTIFY_EVENTS) {
20149d31c2fSAl Viro 		struct name_snapshot name;
20249d31c2fSAl Viro 
203c28f7e56SEric Paris 		/* we are notifying a parent so come up with the new mask which
204c28f7e56SEric Paris 		 * specifies these are events which came from a child. */
205c28f7e56SEric Paris 		mask |= FS_EVENT_ON_CHILD;
206c28f7e56SEric Paris 
20749d31c2fSAl Viro 		take_dentry_name_snapshot(&name, dentry);
2082069601bSLinus Torvalds 		if (path)
20952420392SEric Paris 			ret = fsnotify(p_inode, mask, path, FSNOTIFY_EVENT_PATH,
21025b229dfSAl Viro 				       &name.name, 0);
21128c60e37SEric Paris 		else
21252420392SEric Paris 			ret = fsnotify(p_inode, mask, dentry->d_inode, FSNOTIFY_EVENT_INODE,
21325b229dfSAl Viro 				       &name.name, 0);
21449d31c2fSAl Viro 		release_dentry_name_snapshot(&name);
215c28f7e56SEric Paris 	}
216c28f7e56SEric Paris 
217c28f7e56SEric Paris 	dput(parent);
21852420392SEric Paris 
21952420392SEric Paris 	return ret;
220c28f7e56SEric Paris }
221c28f7e56SEric Paris EXPORT_SYMBOL_GPL(__fsnotify_parent);
222c28f7e56SEric Paris 
223fd657170SDan Carpenter static int send_to_group(struct inode *to_tell,
224e637835eSAl Viro 			 __u32 mask, const void *data,
225613a807fSEric Paris 			 int data_is, u32 cookie,
226e43e9c33SAl Viro 			 const struct qstr *file_name,
2279385a84dSJan Kara 			 struct fsnotify_iter_info *iter_info)
2287131485aSEric Paris {
229faa9560aSEric Paris 	struct fsnotify_group *group = NULL;
230007d1e83SAmir Goldstein 	__u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
23192183a42SAmir Goldstein 	__u32 marks_mask = 0;
23292183a42SAmir Goldstein 	__u32 marks_ignored_mask = 0;
2333dca1a74SAmir Goldstein 	struct fsnotify_mark *mark;
2343dca1a74SAmir Goldstein 	int type;
235613a807fSEric Paris 
2365b0457adSAmir Goldstein 	if (WARN_ON(!iter_info->report_mask))
237faa9560aSEric Paris 		return 0;
2385ba08e2eSEric Paris 
239ce8f76fbSEric Paris 	/* clear ignored on inode modification */
240ce8f76fbSEric Paris 	if (mask & FS_MODIFY) {
2413dca1a74SAmir Goldstein 		fsnotify_foreach_obj_type(type) {
2423dca1a74SAmir Goldstein 			if (!fsnotify_iter_should_report_type(iter_info, type))
2433dca1a74SAmir Goldstein 				continue;
2443dca1a74SAmir Goldstein 			mark = iter_info->marks[type];
2453dca1a74SAmir Goldstein 			if (mark &&
2463dca1a74SAmir Goldstein 			    !(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
2473dca1a74SAmir Goldstein 				mark->ignored_mask = 0;
2483dca1a74SAmir Goldstein 		}
249ce8f76fbSEric Paris 	}
250613a807fSEric Paris 
2513dca1a74SAmir Goldstein 	fsnotify_foreach_obj_type(type) {
2523dca1a74SAmir Goldstein 		if (!fsnotify_iter_should_report_type(iter_info, type))
2533dca1a74SAmir Goldstein 			continue;
2543dca1a74SAmir Goldstein 		mark = iter_info->marks[type];
2553dca1a74SAmir Goldstein 		/* does the object mark tell us to do something? */
2563dca1a74SAmir Goldstein 		if (mark) {
2573dca1a74SAmir Goldstein 			group = mark->group;
2583dca1a74SAmir Goldstein 			marks_mask |= mark->mask;
2593dca1a74SAmir Goldstein 			marks_ignored_mask |= mark->ignored_mask;
2603dca1a74SAmir Goldstein 		}
261ce8f76fbSEric Paris 	}
262ce8f76fbSEric Paris 
2633dca1a74SAmir Goldstein 	pr_debug("%s: group=%p to_tell=%p mask=%x marks_mask=%x marks_ignored_mask=%x"
2647053aee2SJan Kara 		 " data=%p data_is=%d cookie=%d\n",
2653dca1a74SAmir Goldstein 		 __func__, group, to_tell, mask, marks_mask, marks_ignored_mask,
2663dca1a74SAmir Goldstein 		 data, data_is, cookie);
267faa9560aSEric Paris 
26892183a42SAmir Goldstein 	if (!(test_mask & marks_mask & ~marks_ignored_mask))
269613a807fSEric Paris 		return 0;
270613a807fSEric Paris 
2715b0457adSAmir Goldstein 	return group->ops->handle_event(group, to_tell, mask, data, data_is,
2729385a84dSJan Kara 					file_name, cookie, iter_info);
2737131485aSEric Paris }
2747131485aSEric Paris 
2753427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector **connp)
2763427ce71SMiklos Szeredi {
2773427ce71SMiklos Szeredi 	struct fsnotify_mark_connector *conn;
2783427ce71SMiklos Szeredi 	struct hlist_node *node = NULL;
2793427ce71SMiklos Szeredi 
2803427ce71SMiklos Szeredi 	conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
2813427ce71SMiklos Szeredi 	if (conn)
2823427ce71SMiklos Szeredi 		node = srcu_dereference(conn->list.first, &fsnotify_mark_srcu);
2833427ce71SMiklos Szeredi 
2843427ce71SMiklos Szeredi 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
2853427ce71SMiklos Szeredi }
2863427ce71SMiklos Szeredi 
2873427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark)
2883427ce71SMiklos Szeredi {
2893427ce71SMiklos Szeredi 	struct hlist_node *node = NULL;
2903427ce71SMiklos Szeredi 
2913427ce71SMiklos Szeredi 	if (mark)
2923427ce71SMiklos Szeredi 		node = srcu_dereference(mark->obj_list.next,
2933427ce71SMiklos Szeredi 					&fsnotify_mark_srcu);
2943427ce71SMiklos Szeredi 
2953427ce71SMiklos Szeredi 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
2963427ce71SMiklos Szeredi }
2973427ce71SMiklos Szeredi 
298c28f7e56SEric Paris /*
299d9a6f30bSAmir Goldstein  * iter_info is a multi head priority queue of marks.
300d9a6f30bSAmir Goldstein  * Pick a subset of marks from queue heads, all with the
301d9a6f30bSAmir Goldstein  * same group and set the report_mask for selected subset.
302d9a6f30bSAmir Goldstein  * Returns the report_mask of the selected subset.
303d9a6f30bSAmir Goldstein  */
304d9a6f30bSAmir Goldstein static unsigned int fsnotify_iter_select_report_types(
305d9a6f30bSAmir Goldstein 		struct fsnotify_iter_info *iter_info)
306d9a6f30bSAmir Goldstein {
30747d9c7ccSAmir Goldstein 	struct fsnotify_group *max_prio_group = NULL;
30847d9c7ccSAmir Goldstein 	struct fsnotify_mark *mark;
30947d9c7ccSAmir Goldstein 	int type;
310d9a6f30bSAmir Goldstein 
31147d9c7ccSAmir Goldstein 	/* Choose max prio group among groups of all queue heads */
31247d9c7ccSAmir Goldstein 	fsnotify_foreach_obj_type(type) {
31347d9c7ccSAmir Goldstein 		mark = iter_info->marks[type];
31447d9c7ccSAmir Goldstein 		if (mark &&
31547d9c7ccSAmir Goldstein 		    fsnotify_compare_groups(max_prio_group, mark->group) > 0)
31647d9c7ccSAmir Goldstein 			max_prio_group = mark->group;
317d9a6f30bSAmir Goldstein 	}
318d9a6f30bSAmir Goldstein 
31947d9c7ccSAmir Goldstein 	if (!max_prio_group)
32047d9c7ccSAmir Goldstein 		return 0;
32147d9c7ccSAmir Goldstein 
32247d9c7ccSAmir Goldstein 	/* Set the report mask for marks from same group as max prio group */
323d9a6f30bSAmir Goldstein 	iter_info->report_mask = 0;
32447d9c7ccSAmir Goldstein 	fsnotify_foreach_obj_type(type) {
32547d9c7ccSAmir Goldstein 		mark = iter_info->marks[type];
32647d9c7ccSAmir Goldstein 		if (mark &&
32747d9c7ccSAmir Goldstein 		    fsnotify_compare_groups(max_prio_group, mark->group) == 0)
32847d9c7ccSAmir Goldstein 			fsnotify_iter_set_report_type(iter_info, type);
32947d9c7ccSAmir Goldstein 	}
330d9a6f30bSAmir Goldstein 
331d9a6f30bSAmir Goldstein 	return iter_info->report_mask;
332d9a6f30bSAmir Goldstein }
333d9a6f30bSAmir Goldstein 
334d9a6f30bSAmir Goldstein /*
335d9a6f30bSAmir Goldstein  * Pop from iter_info multi head queue, the marks that were iterated in the
336d9a6f30bSAmir Goldstein  * current iteration step.
337d9a6f30bSAmir Goldstein  */
338d9a6f30bSAmir Goldstein static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info)
339d9a6f30bSAmir Goldstein {
34047d9c7ccSAmir Goldstein 	int type;
341d9a6f30bSAmir Goldstein 
34247d9c7ccSAmir Goldstein 	fsnotify_foreach_obj_type(type) {
34347d9c7ccSAmir Goldstein 		if (fsnotify_iter_should_report_type(iter_info, type))
34447d9c7ccSAmir Goldstein 			iter_info->marks[type] =
34547d9c7ccSAmir Goldstein 				fsnotify_next_mark(iter_info->marks[type]);
34647d9c7ccSAmir Goldstein 	}
347d9a6f30bSAmir Goldstein }
348d9a6f30bSAmir Goldstein 
349d9a6f30bSAmir Goldstein /*
35090586523SEric Paris  * This is the main call to fsnotify.  The VFS calls into hook specific functions
35190586523SEric Paris  * in linux/fsnotify.h.  Those functions then in turn call here.  Here will call
35290586523SEric Paris  * out to all of the registered fsnotify_group.  Those groups can then use the
35390586523SEric Paris  * notification event in whatever means they feel necessary.
35490586523SEric Paris  */
355e637835eSAl Viro int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_is,
35625b229dfSAl Viro 	     const struct qstr *file_name, u32 cookie)
35790586523SEric Paris {
3583427ce71SMiklos Szeredi 	struct fsnotify_iter_info iter_info = {};
35945a9fb37SAmir Goldstein 	struct super_block *sb = to_tell->i_sb;
36060f7ed8cSAmir Goldstein 	struct mount *mnt = NULL;
36145a9fb37SAmir Goldstein 	__u32 mnt_or_sb_mask = sb->s_fsnotify_mask;
3629385a84dSJan Kara 	int ret = 0;
363007d1e83SAmir Goldstein 	__u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
36490586523SEric Paris 
36560f7ed8cSAmir Goldstein 	if (data_is == FSNOTIFY_EVENT_PATH) {
366e637835eSAl Viro 		mnt = real_mount(((const struct path *)data)->mnt);
36745a9fb37SAmir Goldstein 		mnt_or_sb_mask |= mnt->mnt_fsnotify_mask;
36860f7ed8cSAmir Goldstein 	}
369b469e7e4SAmir Goldstein 	/* An event "on child" is not intended for a mount/sb mark */
370b469e7e4SAmir Goldstein 	if (mask & FS_EVENT_ON_CHILD)
371b469e7e4SAmir Goldstein 		mnt_or_sb_mask = 0;
372613a807fSEric Paris 
373613a807fSEric Paris 	/*
3747c49b861SDave Hansen 	 * Optimization: srcu_read_lock() has a memory barrier which can
3757c49b861SDave Hansen 	 * be expensive.  It protects walking the *_fsnotify_marks lists.
3767c49b861SDave Hansen 	 * However, if we do not walk the lists, we do not have to do
3777c49b861SDave Hansen 	 * SRCU because we have no references to any objects and do not
3787c49b861SDave Hansen 	 * need SRCU to keep them "alive".
3797c49b861SDave Hansen 	 */
38045a9fb37SAmir Goldstein 	if (!to_tell->i_fsnotify_marks && !sb->s_fsnotify_marks &&
38145a9fb37SAmir Goldstein 	    (!mnt || !mnt->mnt_fsnotify_marks))
3827c49b861SDave Hansen 		return 0;
3837c49b861SDave Hansen 	/*
384613a807fSEric Paris 	 * if this is a modify event we may need to clear the ignored masks
38560f7ed8cSAmir Goldstein 	 * otherwise return if neither the inode nor the vfsmount/sb care about
386613a807fSEric Paris 	 * this type of event.
387613a807fSEric Paris 	 */
388613a807fSEric Paris 	if (!(mask & FS_MODIFY) &&
38960f7ed8cSAmir Goldstein 	    !(test_mask & (to_tell->i_fsnotify_mask | mnt_or_sb_mask)))
390613a807fSEric Paris 		return 0;
3913a9fb89fSEric Paris 
3929385a84dSJan Kara 	iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
3937131485aSEric Paris 
39447d9c7ccSAmir Goldstein 	iter_info.marks[FSNOTIFY_OBJ_TYPE_INODE] =
3953427ce71SMiklos Szeredi 		fsnotify_first_mark(&to_tell->i_fsnotify_marks);
39645a9fb37SAmir Goldstein 	iter_info.marks[FSNOTIFY_OBJ_TYPE_SB] =
39745a9fb37SAmir Goldstein 		fsnotify_first_mark(&sb->s_fsnotify_marks);
3989bdda4e9SAmir Goldstein 	if (mnt) {
39947d9c7ccSAmir Goldstein 		iter_info.marks[FSNOTIFY_OBJ_TYPE_VFSMOUNT] =
4003427ce71SMiklos Szeredi 			fsnotify_first_mark(&mnt->mnt_fsnotify_marks);
4017131485aSEric Paris 	}
40275c1be48SEric Paris 
4038edc6e16SJan Kara 	/*
40460f7ed8cSAmir Goldstein 	 * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark
40560f7ed8cSAmir Goldstein 	 * ignore masks are properly reflected for mount/sb mark notifications.
4068edc6e16SJan Kara 	 * That's why this traversal is so complicated...
4078edc6e16SJan Kara 	 */
408d9a6f30bSAmir Goldstein 	while (fsnotify_iter_select_report_types(&iter_info)) {
4095b0457adSAmir Goldstein 		ret = send_to_group(to_tell, mask, data, data_is, cookie,
410e43e9c33SAl Viro 				    file_name, &iter_info);
41184a5b68eSEric Paris 
412ff8bcbd0SEric Paris 		if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS))
413ff8bcbd0SEric Paris 			goto out;
414ff8bcbd0SEric Paris 
415d9a6f30bSAmir Goldstein 		fsnotify_iter_next(&iter_info);
4167131485aSEric Paris 	}
417ff8bcbd0SEric Paris 	ret = 0;
418ff8bcbd0SEric Paris out:
4199385a84dSJan Kara 	srcu_read_unlock(&fsnotify_mark_srcu, iter_info.srcu_idx);
420c4ec54b4SEric Paris 
42198b5c10dSJean-Christophe Dubois 	return ret;
42290586523SEric Paris }
42390586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify);
42490586523SEric Paris 
4259dd813c1SJan Kara extern struct kmem_cache *fsnotify_mark_connector_cachep;
4269dd813c1SJan Kara 
42790586523SEric Paris static __init int fsnotify_init(void)
42890586523SEric Paris {
42975c1be48SEric Paris 	int ret;
43075c1be48SEric Paris 
43166917a31SMatthew Bobrowski 	BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 25);
43220dee624SEric Paris 
43375c1be48SEric Paris 	ret = init_srcu_struct(&fsnotify_mark_srcu);
43475c1be48SEric Paris 	if (ret)
43575c1be48SEric Paris 		panic("initializing fsnotify_mark_srcu");
43675c1be48SEric Paris 
4379dd813c1SJan Kara 	fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector,
4389dd813c1SJan Kara 						    SLAB_PANIC);
4399dd813c1SJan Kara 
44075c1be48SEric Paris 	return 0;
44190586523SEric Paris }
44275c1be48SEric Paris core_initcall(fsnotify_init);
443