xref: /openbmc/linux/fs/notify/fsnotify.c (revision 9b93f331)
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.
601edc8eb2SEric Sandeen 		 * However, we should have been called /after/ evict_inodes
611edc8eb2SEric Sandeen 		 * removed all zero refcount inodes, in any case.  Test to
621edc8eb2SEric Sandeen 		 * be sure.
63ebb3b47eSJan Kara 		 */
64ebb3b47eSJan Kara 		if (!atomic_read(&inode->i_count)) {
65ebb3b47eSJan Kara 			spin_unlock(&inode->i_lock);
66ebb3b47eSJan Kara 			continue;
67ebb3b47eSJan Kara 		}
68ebb3b47eSJan Kara 
69ebb3b47eSJan Kara 		__iget(inode);
70ebb3b47eSJan Kara 		spin_unlock(&inode->i_lock);
71ebb3b47eSJan Kara 		spin_unlock(&sb->s_inode_list_lock);
72ebb3b47eSJan Kara 
73ebb3b47eSJan Kara 		if (iput_inode)
74ebb3b47eSJan Kara 			iput(iput_inode);
75ebb3b47eSJan Kara 
76ebb3b47eSJan Kara 		/* for each watch, send FS_UNMOUNT and then remove it */
7782ace1efSAmir Goldstein 		fsnotify_inode(inode, FS_UNMOUNT);
78ebb3b47eSJan Kara 
79ebb3b47eSJan Kara 		fsnotify_inode_delete(inode);
80ebb3b47eSJan Kara 
81ebb3b47eSJan Kara 		iput_inode = inode;
82ebb3b47eSJan Kara 
8304646aebSEric Sandeen 		cond_resched();
84ebb3b47eSJan Kara 		spin_lock(&sb->s_inode_list_lock);
85ebb3b47eSJan Kara 	}
86ebb3b47eSJan Kara 	spin_unlock(&sb->s_inode_list_lock);
87ebb3b47eSJan Kara 
88ebb3b47eSJan Kara 	if (iput_inode)
89ebb3b47eSJan Kara 		iput(iput_inode);
90721fb6fbSJan Kara 	/* Wait for outstanding inode references from connectors */
91721fb6fbSJan Kara 	wait_var_event(&sb->s_fsnotify_inode_refs,
92721fb6fbSJan Kara 		       !atomic_long_read(&sb->s_fsnotify_inode_refs));
93ebb3b47eSJan Kara }
94ebb3b47eSJan Kara 
951e6cb723SAmir Goldstein void fsnotify_sb_delete(struct super_block *sb)
961e6cb723SAmir Goldstein {
971e6cb723SAmir Goldstein 	fsnotify_unmount_inodes(sb);
981e6cb723SAmir Goldstein 	fsnotify_clear_marks_by_sb(sb);
991e6cb723SAmir Goldstein }
1001e6cb723SAmir Goldstein 
1013be25f49SEric Paris /*
102c28f7e56SEric Paris  * Given an inode, first check if we care what happens to our children.  Inotify
103c28f7e56SEric Paris  * and dnotify both tell their parents about events.  If we care about any event
104c28f7e56SEric Paris  * on a child we run all of our children and set a dentry flag saying that the
105c28f7e56SEric Paris  * parent cares.  Thus when an event happens on a child it can quickly tell if
106c28f7e56SEric Paris  * if there is a need to find a parent and send the event to the parent.
107c28f7e56SEric Paris  */
108c28f7e56SEric Paris void __fsnotify_update_child_dentry_flags(struct inode *inode)
109c28f7e56SEric Paris {
110c28f7e56SEric Paris 	struct dentry *alias;
111c28f7e56SEric Paris 	int watched;
112c28f7e56SEric Paris 
113c28f7e56SEric Paris 	if (!S_ISDIR(inode->i_mode))
114c28f7e56SEric Paris 		return;
115c28f7e56SEric Paris 
116c28f7e56SEric Paris 	/* determine if the children should tell inode about their events */
117c28f7e56SEric Paris 	watched = fsnotify_inode_watches_children(inode);
118c28f7e56SEric Paris 
119873feea0SNick Piggin 	spin_lock(&inode->i_lock);
120c28f7e56SEric Paris 	/* run all of the dentries associated with this inode.  Since this is a
121c28f7e56SEric Paris 	 * directory, there damn well better only be one item on this list */
122946e51f2SAl Viro 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
123c28f7e56SEric Paris 		struct dentry *child;
124c28f7e56SEric Paris 
125c28f7e56SEric Paris 		/* run all of the children of the original inode and fix their
126c28f7e56SEric Paris 		 * d_flags to indicate parental interest (their parent is the
127c28f7e56SEric Paris 		 * original inode) */
1282fd6b7f5SNick Piggin 		spin_lock(&alias->d_lock);
129946e51f2SAl Viro 		list_for_each_entry(child, &alias->d_subdirs, d_child) {
130c28f7e56SEric Paris 			if (!child->d_inode)
131c28f7e56SEric Paris 				continue;
132c28f7e56SEric Paris 
1332fd6b7f5SNick Piggin 			spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
134c28f7e56SEric Paris 			if (watched)
135c28f7e56SEric Paris 				child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
136c28f7e56SEric Paris 			else
137c28f7e56SEric Paris 				child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
138c28f7e56SEric Paris 			spin_unlock(&child->d_lock);
139c28f7e56SEric Paris 		}
1402fd6b7f5SNick Piggin 		spin_unlock(&alias->d_lock);
141c28f7e56SEric Paris 	}
142873feea0SNick Piggin 	spin_unlock(&inode->i_lock);
143c28f7e56SEric Paris }
144c28f7e56SEric Paris 
1459b93f331SAmir Goldstein /* Are inode/sb/mount interested in parent and name info with this event? */
1469b93f331SAmir Goldstein static bool fsnotify_event_needs_parent(struct inode *inode, struct mount *mnt,
1479b93f331SAmir Goldstein 					__u32 mask)
1489b93f331SAmir Goldstein {
1499b93f331SAmir Goldstein 	__u32 marks_mask = 0;
1509b93f331SAmir Goldstein 
1519b93f331SAmir Goldstein 	/* We only send parent/name to inode/sb/mount for events on non-dir */
1529b93f331SAmir Goldstein 	if (mask & FS_ISDIR)
1539b93f331SAmir Goldstein 		return false;
1549b93f331SAmir Goldstein 
1559b93f331SAmir Goldstein 	/* Did either inode/sb/mount subscribe for events with parent/name? */
1569b93f331SAmir Goldstein 	marks_mask |= fsnotify_parent_needed_mask(inode->i_fsnotify_mask);
1579b93f331SAmir Goldstein 	marks_mask |= fsnotify_parent_needed_mask(inode->i_sb->s_fsnotify_mask);
1589b93f331SAmir Goldstein 	if (mnt)
1599b93f331SAmir Goldstein 		marks_mask |= fsnotify_parent_needed_mask(mnt->mnt_fsnotify_mask);
1609b93f331SAmir Goldstein 
1619b93f331SAmir Goldstein 	/* Did they subscribe for this event with parent/name info? */
1629b93f331SAmir Goldstein 	return mask & marks_mask;
1639b93f331SAmir Goldstein }
1649b93f331SAmir Goldstein 
165c738fbabSAmir Goldstein /*
166c738fbabSAmir Goldstein  * Notify this dentry's parent about a child's events with child name info
1679b93f331SAmir Goldstein  * if parent is watching or if inode/sb/mount are interested in events with
1689b93f331SAmir Goldstein  * parent and name info.
1699b93f331SAmir Goldstein  *
1709b93f331SAmir Goldstein  * Notify only the child without name info if parent is not watching and
1719b93f331SAmir Goldstein  * inode/sb/mount are not interested in events with parent and name info.
172c738fbabSAmir Goldstein  */
17371d73410SMel Gorman int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
174017de65fSAmir Goldstein 		      int data_type)
175c28f7e56SEric Paris {
1769b93f331SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
1779b93f331SAmir Goldstein 	struct mount *mnt = path ? real_mount(path->mnt) : NULL;
178497b0c5aSAmir Goldstein 	struct inode *inode = d_inode(dentry);
179c28f7e56SEric Paris 	struct dentry *parent;
1809b93f331SAmir Goldstein 	bool parent_watched = dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED;
1819b93f331SAmir Goldstein 	__u32 p_mask;
18240a100d3SAmir Goldstein 	struct inode *p_inode = NULL;
183497b0c5aSAmir Goldstein 	struct name_snapshot name;
184497b0c5aSAmir Goldstein 	struct qstr *file_name = NULL;
18552420392SEric Paris 	int ret = 0;
186c28f7e56SEric Paris 
1879b93f331SAmir Goldstein 	/*
1889b93f331SAmir Goldstein 	 * Do inode/sb/mount care about parent and name info on non-dir?
1899b93f331SAmir Goldstein 	 * Do they care about any event at all?
1909b93f331SAmir Goldstein 	 */
1919b93f331SAmir Goldstein 	if (!inode->i_fsnotify_marks && !inode->i_sb->s_fsnotify_marks &&
1929b93f331SAmir Goldstein 	    (!mnt || !mnt->mnt_fsnotify_marks) && !parent_watched)
1939b93f331SAmir Goldstein 		return 0;
1949b93f331SAmir Goldstein 
195497b0c5aSAmir Goldstein 	parent = NULL;
1969b93f331SAmir Goldstein 	if (!parent_watched && !fsnotify_event_needs_parent(inode, mnt, mask))
197497b0c5aSAmir Goldstein 		goto notify;
198c28f7e56SEric Paris 
1999b93f331SAmir Goldstein 	/* Does parent inode care about events on children? */
2004d4eb366SChristoph Hellwig 	parent = dget_parent(dentry);
201c28f7e56SEric Paris 	p_inode = parent->d_inode;
2029b93f331SAmir Goldstein 	p_mask = fsnotify_inode_watches_children(p_inode);
2039b93f331SAmir Goldstein 	if (unlikely(parent_watched && !p_mask))
2044d4eb366SChristoph Hellwig 		__fsnotify_update_child_dentry_flags(p_inode);
2059b93f331SAmir Goldstein 
2069b93f331SAmir Goldstein 	/*
2079b93f331SAmir Goldstein 	 * Include parent/name in notification either if some notification
2089b93f331SAmir Goldstein 	 * groups require parent info (!parent_watched case) or the parent is
2099b93f331SAmir Goldstein 	 * interested in this event.
2109b93f331SAmir Goldstein 	 */
2119b93f331SAmir Goldstein 	if (!parent_watched || (mask & p_mask & ALL_FSNOTIFY_EVENTS)) {
212497b0c5aSAmir Goldstein 		/* When notifying parent, child should be passed as data */
213497b0c5aSAmir Goldstein 		WARN_ON_ONCE(inode != fsnotify_data_inode(data, data_type));
21449d31c2fSAl Viro 
215497b0c5aSAmir Goldstein 		/* Notify both parent and child with child name info */
21649d31c2fSAl Viro 		take_dentry_name_snapshot(&name, dentry);
217497b0c5aSAmir Goldstein 		file_name = &name.name;
2189b93f331SAmir Goldstein 		if (parent_watched)
219497b0c5aSAmir Goldstein 			mask |= FS_EVENT_ON_CHILD;
220c28f7e56SEric Paris 	}
221c28f7e56SEric Paris 
222497b0c5aSAmir Goldstein notify:
22340a100d3SAmir Goldstein 	ret = fsnotify(mask, data, data_type, p_inode, file_name, inode, 0);
224497b0c5aSAmir Goldstein 
225497b0c5aSAmir Goldstein 	if (file_name)
226497b0c5aSAmir Goldstein 		release_dentry_name_snapshot(&name);
227c28f7e56SEric Paris 	dput(parent);
22852420392SEric Paris 
22952420392SEric Paris 	return ret;
230c28f7e56SEric Paris }
23171d73410SMel Gorman EXPORT_SYMBOL_GPL(__fsnotify_parent);
232c28f7e56SEric Paris 
233b54cecf5SAmir Goldstein static int send_to_group(__u32 mask, const void *data, int data_type,
234b54cecf5SAmir Goldstein 			 struct inode *dir, const struct qstr *file_name,
235b54cecf5SAmir Goldstein 			 u32 cookie, struct fsnotify_iter_info *iter_info)
2367131485aSEric Paris {
237faa9560aSEric Paris 	struct fsnotify_group *group = NULL;
238007d1e83SAmir Goldstein 	__u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
23992183a42SAmir Goldstein 	__u32 marks_mask = 0;
24092183a42SAmir Goldstein 	__u32 marks_ignored_mask = 0;
2413dca1a74SAmir Goldstein 	struct fsnotify_mark *mark;
2423dca1a74SAmir Goldstein 	int type;
243613a807fSEric Paris 
2445b0457adSAmir Goldstein 	if (WARN_ON(!iter_info->report_mask))
245faa9560aSEric Paris 		return 0;
2465ba08e2eSEric Paris 
247ce8f76fbSEric Paris 	/* clear ignored on inode modification */
248ce8f76fbSEric Paris 	if (mask & FS_MODIFY) {
2493dca1a74SAmir Goldstein 		fsnotify_foreach_obj_type(type) {
2503dca1a74SAmir Goldstein 			if (!fsnotify_iter_should_report_type(iter_info, type))
2513dca1a74SAmir Goldstein 				continue;
2523dca1a74SAmir Goldstein 			mark = iter_info->marks[type];
2533dca1a74SAmir Goldstein 			if (mark &&
2543dca1a74SAmir Goldstein 			    !(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
2553dca1a74SAmir Goldstein 				mark->ignored_mask = 0;
2563dca1a74SAmir Goldstein 		}
257ce8f76fbSEric Paris 	}
258613a807fSEric Paris 
2593dca1a74SAmir Goldstein 	fsnotify_foreach_obj_type(type) {
2603dca1a74SAmir Goldstein 		if (!fsnotify_iter_should_report_type(iter_info, type))
2613dca1a74SAmir Goldstein 			continue;
2623dca1a74SAmir Goldstein 		mark = iter_info->marks[type];
2633dca1a74SAmir Goldstein 		/* does the object mark tell us to do something? */
2643dca1a74SAmir Goldstein 		if (mark) {
2653dca1a74SAmir Goldstein 			group = mark->group;
2663dca1a74SAmir Goldstein 			marks_mask |= mark->mask;
2673dca1a74SAmir Goldstein 			marks_ignored_mask |= mark->ignored_mask;
2683dca1a74SAmir Goldstein 		}
269ce8f76fbSEric Paris 	}
270ce8f76fbSEric Paris 
271b54cecf5SAmir Goldstein 	pr_debug("%s: group=%p mask=%x marks_mask=%x marks_ignored_mask=%x data=%p data_type=%d dir=%p cookie=%d\n",
272b54cecf5SAmir Goldstein 		 __func__, group, mask, marks_mask, marks_ignored_mask,
273b54cecf5SAmir Goldstein 		 data, data_type, dir, cookie);
274faa9560aSEric Paris 
27592183a42SAmir Goldstein 	if (!(test_mask & marks_mask & ~marks_ignored_mask))
276613a807fSEric Paris 		return 0;
277613a807fSEric Paris 
278b54cecf5SAmir Goldstein 	return group->ops->handle_event(group, mask, data, data_type, dir,
2799385a84dSJan Kara 					file_name, cookie, iter_info);
2807131485aSEric Paris }
2817131485aSEric Paris 
2823427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector **connp)
2833427ce71SMiklos Szeredi {
2843427ce71SMiklos Szeredi 	struct fsnotify_mark_connector *conn;
2853427ce71SMiklos Szeredi 	struct hlist_node *node = NULL;
2863427ce71SMiklos Szeredi 
2873427ce71SMiklos Szeredi 	conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
2883427ce71SMiklos Szeredi 	if (conn)
2893427ce71SMiklos Szeredi 		node = srcu_dereference(conn->list.first, &fsnotify_mark_srcu);
2903427ce71SMiklos Szeredi 
2913427ce71SMiklos Szeredi 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
2923427ce71SMiklos Szeredi }
2933427ce71SMiklos Szeredi 
2943427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark)
2953427ce71SMiklos Szeredi {
2963427ce71SMiklos Szeredi 	struct hlist_node *node = NULL;
2973427ce71SMiklos Szeredi 
2983427ce71SMiklos Szeredi 	if (mark)
2993427ce71SMiklos Szeredi 		node = srcu_dereference(mark->obj_list.next,
3003427ce71SMiklos Szeredi 					&fsnotify_mark_srcu);
3013427ce71SMiklos Szeredi 
3023427ce71SMiklos Szeredi 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
3033427ce71SMiklos Szeredi }
3043427ce71SMiklos Szeredi 
305c28f7e56SEric Paris /*
306d9a6f30bSAmir Goldstein  * iter_info is a multi head priority queue of marks.
307d9a6f30bSAmir Goldstein  * Pick a subset of marks from queue heads, all with the
308d9a6f30bSAmir Goldstein  * same group and set the report_mask for selected subset.
309d9a6f30bSAmir Goldstein  * Returns the report_mask of the selected subset.
310d9a6f30bSAmir Goldstein  */
311d9a6f30bSAmir Goldstein static unsigned int fsnotify_iter_select_report_types(
312d9a6f30bSAmir Goldstein 		struct fsnotify_iter_info *iter_info)
313d9a6f30bSAmir Goldstein {
31447d9c7ccSAmir Goldstein 	struct fsnotify_group *max_prio_group = NULL;
31547d9c7ccSAmir Goldstein 	struct fsnotify_mark *mark;
31647d9c7ccSAmir Goldstein 	int type;
317d9a6f30bSAmir Goldstein 
31847d9c7ccSAmir Goldstein 	/* Choose max prio group among groups of all queue heads */
31947d9c7ccSAmir Goldstein 	fsnotify_foreach_obj_type(type) {
32047d9c7ccSAmir Goldstein 		mark = iter_info->marks[type];
32147d9c7ccSAmir Goldstein 		if (mark &&
32247d9c7ccSAmir Goldstein 		    fsnotify_compare_groups(max_prio_group, mark->group) > 0)
32347d9c7ccSAmir Goldstein 			max_prio_group = mark->group;
324d9a6f30bSAmir Goldstein 	}
325d9a6f30bSAmir Goldstein 
32647d9c7ccSAmir Goldstein 	if (!max_prio_group)
32747d9c7ccSAmir Goldstein 		return 0;
32847d9c7ccSAmir Goldstein 
32947d9c7ccSAmir Goldstein 	/* Set the report mask for marks from same group as max prio group */
330d9a6f30bSAmir Goldstein 	iter_info->report_mask = 0;
33147d9c7ccSAmir Goldstein 	fsnotify_foreach_obj_type(type) {
33247d9c7ccSAmir Goldstein 		mark = iter_info->marks[type];
33347d9c7ccSAmir Goldstein 		if (mark &&
33447d9c7ccSAmir Goldstein 		    fsnotify_compare_groups(max_prio_group, mark->group) == 0)
33547d9c7ccSAmir Goldstein 			fsnotify_iter_set_report_type(iter_info, type);
33647d9c7ccSAmir Goldstein 	}
337d9a6f30bSAmir Goldstein 
338d9a6f30bSAmir Goldstein 	return iter_info->report_mask;
339d9a6f30bSAmir Goldstein }
340d9a6f30bSAmir Goldstein 
341d9a6f30bSAmir Goldstein /*
342d9a6f30bSAmir Goldstein  * Pop from iter_info multi head queue, the marks that were iterated in the
343d9a6f30bSAmir Goldstein  * current iteration step.
344d9a6f30bSAmir Goldstein  */
345d9a6f30bSAmir Goldstein static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info)
346d9a6f30bSAmir Goldstein {
34747d9c7ccSAmir Goldstein 	int type;
348d9a6f30bSAmir Goldstein 
34947d9c7ccSAmir Goldstein 	fsnotify_foreach_obj_type(type) {
35047d9c7ccSAmir Goldstein 		if (fsnotify_iter_should_report_type(iter_info, type))
35147d9c7ccSAmir Goldstein 			iter_info->marks[type] =
35247d9c7ccSAmir Goldstein 				fsnotify_next_mark(iter_info->marks[type]);
35347d9c7ccSAmir Goldstein 	}
354d9a6f30bSAmir Goldstein }
355d9a6f30bSAmir Goldstein 
356d9a6f30bSAmir Goldstein /*
35740a100d3SAmir Goldstein  * fsnotify - This is the main call to fsnotify.
35840a100d3SAmir Goldstein  *
35940a100d3SAmir Goldstein  * The VFS calls into hook specific functions in linux/fsnotify.h.
36040a100d3SAmir Goldstein  * Those functions then in turn call here.  Here will call out to all of the
36140a100d3SAmir Goldstein  * registered fsnotify_group.  Those groups can then use the notification event
36240a100d3SAmir Goldstein  * in whatever means they feel necessary.
36340a100d3SAmir Goldstein  *
36440a100d3SAmir Goldstein  * @mask:	event type and flags
36540a100d3SAmir Goldstein  * @data:	object that event happened on
36640a100d3SAmir Goldstein  * @data_type:	type of object for fanotify_data_XXX() accessors
36740a100d3SAmir Goldstein  * @dir:	optional directory associated with event -
36840a100d3SAmir Goldstein  *		if @file_name is not NULL, this is the directory that
36940a100d3SAmir Goldstein  *		@file_name is relative to
37040a100d3SAmir Goldstein  * @file_name:	optional file name associated with event
37140a100d3SAmir Goldstein  * @inode:	optional inode associated with event -
37240a100d3SAmir Goldstein  *		either @dir or @inode must be non-NULL.
37340a100d3SAmir Goldstein  *		if both are non-NULL event may be reported to both.
37440a100d3SAmir Goldstein  * @cookie:	inotify rename cookie
37590586523SEric Paris  */
37640a100d3SAmir Goldstein int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
37740a100d3SAmir Goldstein 	     const struct qstr *file_name, struct inode *inode, u32 cookie)
37890586523SEric Paris {
379b54cecf5SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
3803427ce71SMiklos Szeredi 	struct fsnotify_iter_info iter_info = {};
38140a100d3SAmir Goldstein 	struct super_block *sb;
38260f7ed8cSAmir Goldstein 	struct mount *mnt = NULL;
383497b0c5aSAmir Goldstein 	struct inode *child = NULL;
3849385a84dSJan Kara 	int ret = 0;
38571d73410SMel Gorman 	__u32 test_mask, marks_mask;
38690586523SEric Paris 
38771d73410SMel Gorman 	if (path)
388aa93bdc5SAmir Goldstein 		mnt = real_mount(path->mnt);
389613a807fSEric Paris 
39040a100d3SAmir Goldstein 	if (!inode) {
39140a100d3SAmir Goldstein 		/* Dirent event - report on TYPE_INODE to dir */
39240a100d3SAmir Goldstein 		inode = dir;
39340a100d3SAmir Goldstein 	} else if (mask & FS_EVENT_ON_CHILD) {
39440a100d3SAmir Goldstein 		/*
3959b93f331SAmir Goldstein 		 * Event on child - report on TYPE_INODE to dir if it is
3969b93f331SAmir Goldstein 		 * watching children and on TYPE_CHILD to child.
39740a100d3SAmir Goldstein 		 */
39840a100d3SAmir Goldstein 		child = inode;
39940a100d3SAmir Goldstein 		inode = dir;
40040a100d3SAmir Goldstein 	}
40140a100d3SAmir Goldstein 	sb = inode->i_sb;
402497b0c5aSAmir Goldstein 
403613a807fSEric Paris 	/*
4047c49b861SDave Hansen 	 * Optimization: srcu_read_lock() has a memory barrier which can
4057c49b861SDave Hansen 	 * be expensive.  It protects walking the *_fsnotify_marks lists.
4067c49b861SDave Hansen 	 * However, if we do not walk the lists, we do not have to do
4077c49b861SDave Hansen 	 * SRCU because we have no references to any objects and do not
4087c49b861SDave Hansen 	 * need SRCU to keep them "alive".
4097c49b861SDave Hansen 	 */
4109b93f331SAmir Goldstein 	if (!sb->s_fsnotify_marks &&
411497b0c5aSAmir Goldstein 	    (!mnt || !mnt->mnt_fsnotify_marks) &&
4129b93f331SAmir Goldstein 	    (!inode || !inode->i_fsnotify_marks) &&
413497b0c5aSAmir Goldstein 	    (!child || !child->i_fsnotify_marks))
4147c49b861SDave Hansen 		return 0;
41571d73410SMel Gorman 
4169b93f331SAmir Goldstein 	marks_mask = sb->s_fsnotify_mask;
41771d73410SMel Gorman 	if (mnt)
41871d73410SMel Gorman 		marks_mask |= mnt->mnt_fsnotify_mask;
4199b93f331SAmir Goldstein 	if (inode)
4209b93f331SAmir Goldstein 		marks_mask |= inode->i_fsnotify_mask;
421497b0c5aSAmir Goldstein 	if (child)
422497b0c5aSAmir Goldstein 		marks_mask |= child->i_fsnotify_mask;
423497b0c5aSAmir Goldstein 
42471d73410SMel Gorman 
4257c49b861SDave Hansen 	/*
426613a807fSEric Paris 	 * if this is a modify event we may need to clear the ignored masks
427497b0c5aSAmir Goldstein 	 * otherwise return if none of the marks care about this type of event.
428613a807fSEric Paris 	 */
42971d73410SMel Gorman 	test_mask = (mask & ALL_FSNOTIFY_EVENTS);
43071d73410SMel Gorman 	if (!(mask & FS_MODIFY) && !(test_mask & marks_mask))
431613a807fSEric Paris 		return 0;
4323a9fb89fSEric Paris 
4339385a84dSJan Kara 	iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
4347131485aSEric Paris 
43545a9fb37SAmir Goldstein 	iter_info.marks[FSNOTIFY_OBJ_TYPE_SB] =
43645a9fb37SAmir Goldstein 		fsnotify_first_mark(&sb->s_fsnotify_marks);
4379bdda4e9SAmir Goldstein 	if (mnt) {
43847d9c7ccSAmir Goldstein 		iter_info.marks[FSNOTIFY_OBJ_TYPE_VFSMOUNT] =
4393427ce71SMiklos Szeredi 			fsnotify_first_mark(&mnt->mnt_fsnotify_marks);
4407131485aSEric Paris 	}
4419b93f331SAmir Goldstein 	if (inode) {
4429b93f331SAmir Goldstein 		iter_info.marks[FSNOTIFY_OBJ_TYPE_INODE] =
4439b93f331SAmir Goldstein 			fsnotify_first_mark(&inode->i_fsnotify_marks);
4449b93f331SAmir Goldstein 	}
445497b0c5aSAmir Goldstein 	if (child) {
446497b0c5aSAmir Goldstein 		iter_info.marks[FSNOTIFY_OBJ_TYPE_CHILD] =
447497b0c5aSAmir Goldstein 			fsnotify_first_mark(&child->i_fsnotify_marks);
448497b0c5aSAmir Goldstein 	}
44975c1be48SEric Paris 
4508edc6e16SJan Kara 	/*
45160f7ed8cSAmir Goldstein 	 * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark
45260f7ed8cSAmir Goldstein 	 * ignore masks are properly reflected for mount/sb mark notifications.
4538edc6e16SJan Kara 	 * That's why this traversal is so complicated...
4548edc6e16SJan Kara 	 */
455d9a6f30bSAmir Goldstein 	while (fsnotify_iter_select_report_types(&iter_info)) {
456b54cecf5SAmir Goldstein 		ret = send_to_group(mask, data, data_type, dir, file_name,
457b54cecf5SAmir Goldstein 				    cookie, &iter_info);
45884a5b68eSEric Paris 
459ff8bcbd0SEric Paris 		if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS))
460ff8bcbd0SEric Paris 			goto out;
461ff8bcbd0SEric Paris 
462d9a6f30bSAmir Goldstein 		fsnotify_iter_next(&iter_info);
4637131485aSEric Paris 	}
464ff8bcbd0SEric Paris 	ret = 0;
465ff8bcbd0SEric Paris out:
4669385a84dSJan Kara 	srcu_read_unlock(&fsnotify_mark_srcu, iter_info.srcu_idx);
467c4ec54b4SEric Paris 
46898b5c10dSJean-Christophe Dubois 	return ret;
46990586523SEric Paris }
47090586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify);
47190586523SEric Paris 
47290586523SEric Paris static __init int fsnotify_init(void)
47390586523SEric Paris {
47475c1be48SEric Paris 	int ret;
47575c1be48SEric Paris 
47608b95c33SAmir Goldstein 	BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 25);
47720dee624SEric Paris 
47875c1be48SEric Paris 	ret = init_srcu_struct(&fsnotify_mark_srcu);
47975c1be48SEric Paris 	if (ret)
48075c1be48SEric Paris 		panic("initializing fsnotify_mark_srcu");
48175c1be48SEric Paris 
4829dd813c1SJan Kara 	fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector,
4839dd813c1SJan Kara 						    SLAB_PANIC);
4849dd813c1SJan Kara 
48575c1be48SEric Paris 	return 0;
48690586523SEric Paris }
48775c1be48SEric Paris core_initcall(fsnotify_init);
488