xref: /openbmc/linux/fs/notify/fsnotify.c (revision 38035c04)
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 		iput(iput_inode);
74ebb3b47eSJan Kara 
75ebb3b47eSJan Kara 		/* for each watch, send FS_UNMOUNT and then remove it */
7682ace1efSAmir Goldstein 		fsnotify_inode(inode, FS_UNMOUNT);
77ebb3b47eSJan Kara 
78ebb3b47eSJan Kara 		fsnotify_inode_delete(inode);
79ebb3b47eSJan Kara 
80ebb3b47eSJan Kara 		iput_inode = inode;
81ebb3b47eSJan Kara 
8204646aebSEric Sandeen 		cond_resched();
83ebb3b47eSJan Kara 		spin_lock(&sb->s_inode_list_lock);
84ebb3b47eSJan Kara 	}
85ebb3b47eSJan Kara 	spin_unlock(&sb->s_inode_list_lock);
86ebb3b47eSJan Kara 
87ebb3b47eSJan Kara 	iput(iput_inode);
88ebb3b47eSJan Kara }
89ebb3b47eSJan Kara 
901e6cb723SAmir Goldstein void fsnotify_sb_delete(struct super_block *sb)
911e6cb723SAmir Goldstein {
921e6cb723SAmir Goldstein 	fsnotify_unmount_inodes(sb);
931e6cb723SAmir Goldstein 	fsnotify_clear_marks_by_sb(sb);
94ec44610fSAmir Goldstein 	/* Wait for outstanding object references from connectors */
95ec44610fSAmir Goldstein 	wait_var_event(&sb->s_fsnotify_connectors,
96ec44610fSAmir Goldstein 		       !atomic_long_read(&sb->s_fsnotify_connectors));
971e6cb723SAmir Goldstein }
981e6cb723SAmir Goldstein 
993be25f49SEric Paris /*
100c28f7e56SEric Paris  * Given an inode, first check if we care what happens to our children.  Inotify
101c28f7e56SEric Paris  * and dnotify both tell their parents about events.  If we care about any event
102c28f7e56SEric Paris  * on a child we run all of our children and set a dentry flag saying that the
103c28f7e56SEric Paris  * parent cares.  Thus when an event happens on a child it can quickly tell if
104c28f7e56SEric Paris  * if there is a need to find a parent and send the event to the parent.
105c28f7e56SEric Paris  */
106c28f7e56SEric Paris void __fsnotify_update_child_dentry_flags(struct inode *inode)
107c28f7e56SEric Paris {
108c28f7e56SEric Paris 	struct dentry *alias;
109c28f7e56SEric Paris 	int watched;
110c28f7e56SEric Paris 
111c28f7e56SEric Paris 	if (!S_ISDIR(inode->i_mode))
112c28f7e56SEric Paris 		return;
113c28f7e56SEric Paris 
114c28f7e56SEric Paris 	/* determine if the children should tell inode about their events */
115c28f7e56SEric Paris 	watched = fsnotify_inode_watches_children(inode);
116c28f7e56SEric Paris 
117873feea0SNick Piggin 	spin_lock(&inode->i_lock);
118c28f7e56SEric Paris 	/* run all of the dentries associated with this inode.  Since this is a
119c28f7e56SEric Paris 	 * directory, there damn well better only be one item on this list */
120946e51f2SAl Viro 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
121c28f7e56SEric Paris 		struct dentry *child;
122c28f7e56SEric Paris 
123c28f7e56SEric Paris 		/* run all of the children of the original inode and fix their
124c28f7e56SEric Paris 		 * d_flags to indicate parental interest (their parent is the
125c28f7e56SEric Paris 		 * original inode) */
1262fd6b7f5SNick Piggin 		spin_lock(&alias->d_lock);
127946e51f2SAl Viro 		list_for_each_entry(child, &alias->d_subdirs, d_child) {
128c28f7e56SEric Paris 			if (!child->d_inode)
129c28f7e56SEric Paris 				continue;
130c28f7e56SEric Paris 
1312fd6b7f5SNick Piggin 			spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
132c28f7e56SEric Paris 			if (watched)
133c28f7e56SEric Paris 				child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
134c28f7e56SEric Paris 			else
135c28f7e56SEric Paris 				child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
136c28f7e56SEric Paris 			spin_unlock(&child->d_lock);
137c28f7e56SEric Paris 		}
1382fd6b7f5SNick Piggin 		spin_unlock(&alias->d_lock);
139c28f7e56SEric Paris 	}
140873feea0SNick Piggin 	spin_unlock(&inode->i_lock);
141c28f7e56SEric Paris }
142c28f7e56SEric Paris 
1439b93f331SAmir Goldstein /* Are inode/sb/mount interested in parent and name info with this event? */
1449b93f331SAmir Goldstein static bool fsnotify_event_needs_parent(struct inode *inode, struct mount *mnt,
1459b93f331SAmir Goldstein 					__u32 mask)
1469b93f331SAmir Goldstein {
1479b93f331SAmir Goldstein 	__u32 marks_mask = 0;
1489b93f331SAmir Goldstein 
1499b93f331SAmir Goldstein 	/* We only send parent/name to inode/sb/mount for events on non-dir */
1509b93f331SAmir Goldstein 	if (mask & FS_ISDIR)
1519b93f331SAmir Goldstein 		return false;
1529b93f331SAmir Goldstein 
153fecc4559SAmir Goldstein 	/*
154fecc4559SAmir Goldstein 	 * All events that are possible on child can also may be reported with
155fecc4559SAmir Goldstein 	 * parent/name info to inode/sb/mount.  Otherwise, a watching parent
156fecc4559SAmir Goldstein 	 * could result in events reported with unexpected name info to sb/mount.
157fecc4559SAmir Goldstein 	 */
158fecc4559SAmir Goldstein 	BUILD_BUG_ON(FS_EVENTS_POSS_ON_CHILD & ~FS_EVENTS_POSS_TO_PARENT);
159fecc4559SAmir Goldstein 
1609b93f331SAmir Goldstein 	/* Did either inode/sb/mount subscribe for events with parent/name? */
1619b93f331SAmir Goldstein 	marks_mask |= fsnotify_parent_needed_mask(inode->i_fsnotify_mask);
1629b93f331SAmir Goldstein 	marks_mask |= fsnotify_parent_needed_mask(inode->i_sb->s_fsnotify_mask);
1639b93f331SAmir Goldstein 	if (mnt)
1649b93f331SAmir Goldstein 		marks_mask |= fsnotify_parent_needed_mask(mnt->mnt_fsnotify_mask);
1659b93f331SAmir Goldstein 
1669b93f331SAmir Goldstein 	/* Did they subscribe for this event with parent/name info? */
1679b93f331SAmir Goldstein 	return mask & marks_mask;
1689b93f331SAmir Goldstein }
1699b93f331SAmir Goldstein 
170c738fbabSAmir Goldstein /*
171c738fbabSAmir Goldstein  * Notify this dentry's parent about a child's events with child name info
1729b93f331SAmir Goldstein  * if parent is watching or if inode/sb/mount are interested in events with
1739b93f331SAmir Goldstein  * parent and name info.
1749b93f331SAmir Goldstein  *
1759b93f331SAmir Goldstein  * Notify only the child without name info if parent is not watching and
1769b93f331SAmir Goldstein  * inode/sb/mount are not interested in events with parent and name info.
177c738fbabSAmir Goldstein  */
17871d73410SMel Gorman int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
179017de65fSAmir Goldstein 		      int data_type)
180c28f7e56SEric Paris {
1819b93f331SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
1829b93f331SAmir Goldstein 	struct mount *mnt = path ? real_mount(path->mnt) : NULL;
183497b0c5aSAmir Goldstein 	struct inode *inode = d_inode(dentry);
184c28f7e56SEric Paris 	struct dentry *parent;
1859b93f331SAmir Goldstein 	bool parent_watched = dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED;
1867372e79cSAmir Goldstein 	bool parent_needed, parent_interested;
1879b93f331SAmir Goldstein 	__u32 p_mask;
18840a100d3SAmir Goldstein 	struct inode *p_inode = NULL;
189497b0c5aSAmir Goldstein 	struct name_snapshot name;
190497b0c5aSAmir Goldstein 	struct qstr *file_name = NULL;
19152420392SEric Paris 	int ret = 0;
192c28f7e56SEric Paris 
1939b93f331SAmir Goldstein 	/*
1949b93f331SAmir Goldstein 	 * Do inode/sb/mount care about parent and name info on non-dir?
1959b93f331SAmir Goldstein 	 * Do they care about any event at all?
1969b93f331SAmir Goldstein 	 */
1979b93f331SAmir Goldstein 	if (!inode->i_fsnotify_marks && !inode->i_sb->s_fsnotify_marks &&
1989b93f331SAmir Goldstein 	    (!mnt || !mnt->mnt_fsnotify_marks) && !parent_watched)
1999b93f331SAmir Goldstein 		return 0;
2009b93f331SAmir Goldstein 
201497b0c5aSAmir Goldstein 	parent = NULL;
2027372e79cSAmir Goldstein 	parent_needed = fsnotify_event_needs_parent(inode, mnt, mask);
2037372e79cSAmir Goldstein 	if (!parent_watched && !parent_needed)
204497b0c5aSAmir Goldstein 		goto notify;
205c28f7e56SEric Paris 
2069b93f331SAmir Goldstein 	/* Does parent inode care about events on children? */
2074d4eb366SChristoph Hellwig 	parent = dget_parent(dentry);
208c28f7e56SEric Paris 	p_inode = parent->d_inode;
2099b93f331SAmir Goldstein 	p_mask = fsnotify_inode_watches_children(p_inode);
2109b93f331SAmir Goldstein 	if (unlikely(parent_watched && !p_mask))
2114d4eb366SChristoph Hellwig 		__fsnotify_update_child_dentry_flags(p_inode);
2129b93f331SAmir Goldstein 
2139b93f331SAmir Goldstein 	/*
2149b93f331SAmir Goldstein 	 * Include parent/name in notification either if some notification
2157372e79cSAmir Goldstein 	 * groups require parent info or the parent is interested in this event.
2169b93f331SAmir Goldstein 	 */
2177372e79cSAmir Goldstein 	parent_interested = mask & p_mask & ALL_FSNOTIFY_EVENTS;
2187372e79cSAmir Goldstein 	if (parent_needed || parent_interested) {
219497b0c5aSAmir Goldstein 		/* When notifying parent, child should be passed as data */
220497b0c5aSAmir Goldstein 		WARN_ON_ONCE(inode != fsnotify_data_inode(data, data_type));
22149d31c2fSAl Viro 
222497b0c5aSAmir Goldstein 		/* Notify both parent and child with child name info */
22349d31c2fSAl Viro 		take_dentry_name_snapshot(&name, dentry);
224497b0c5aSAmir Goldstein 		file_name = &name.name;
2257372e79cSAmir Goldstein 		if (parent_interested)
226497b0c5aSAmir Goldstein 			mask |= FS_EVENT_ON_CHILD;
227c28f7e56SEric Paris 	}
228c28f7e56SEric Paris 
229497b0c5aSAmir Goldstein notify:
23040a100d3SAmir Goldstein 	ret = fsnotify(mask, data, data_type, p_inode, file_name, inode, 0);
231497b0c5aSAmir Goldstein 
232497b0c5aSAmir Goldstein 	if (file_name)
233497b0c5aSAmir Goldstein 		release_dentry_name_snapshot(&name);
234c28f7e56SEric Paris 	dput(parent);
23552420392SEric Paris 
23652420392SEric Paris 	return ret;
237c28f7e56SEric Paris }
23871d73410SMel Gorman EXPORT_SYMBOL_GPL(__fsnotify_parent);
239c28f7e56SEric Paris 
240950cc0d2SAmir Goldstein static int fsnotify_handle_inode_event(struct fsnotify_group *group,
241950cc0d2SAmir Goldstein 				       struct fsnotify_mark *inode_mark,
242950cc0d2SAmir Goldstein 				       u32 mask, const void *data, int data_type,
243950cc0d2SAmir Goldstein 				       struct inode *dir, const struct qstr *name,
244950cc0d2SAmir Goldstein 				       u32 cookie)
245950cc0d2SAmir Goldstein {
246950cc0d2SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
247950cc0d2SAmir Goldstein 	struct inode *inode = fsnotify_data_inode(data, data_type);
248950cc0d2SAmir Goldstein 	const struct fsnotify_ops *ops = group->ops;
249950cc0d2SAmir Goldstein 
250950cc0d2SAmir Goldstein 	if (WARN_ON_ONCE(!ops->handle_inode_event))
251950cc0d2SAmir Goldstein 		return 0;
252950cc0d2SAmir Goldstein 
25324dca905SGabriel Krisman Bertazi 	if (WARN_ON_ONCE(!inode && !dir))
25424dca905SGabriel Krisman Bertazi 		return 0;
25524dca905SGabriel Krisman Bertazi 
256*38035c04SAmir Goldstein 	if ((inode_mark->flags & FSNOTIFY_MARK_FLAG_EXCL_UNLINK) &&
257950cc0d2SAmir Goldstein 	    path && d_unlinked(path->dentry))
258950cc0d2SAmir Goldstein 		return 0;
259950cc0d2SAmir Goldstein 
260fecc4559SAmir Goldstein 	/* Check interest of this mark in case event was sent with two marks */
261fecc4559SAmir Goldstein 	if (!(mask & inode_mark->mask & ALL_FSNOTIFY_EVENTS))
262fecc4559SAmir Goldstein 		return 0;
263fecc4559SAmir Goldstein 
264950cc0d2SAmir Goldstein 	return ops->handle_inode_event(inode_mark, mask, inode, dir, name, cookie);
265950cc0d2SAmir Goldstein }
266950cc0d2SAmir Goldstein 
267b9a1b977SAmir Goldstein static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
268b9a1b977SAmir Goldstein 				 const void *data, int data_type,
269b9a1b977SAmir Goldstein 				 struct inode *dir, const struct qstr *name,
270b9a1b977SAmir Goldstein 				 u32 cookie, struct fsnotify_iter_info *iter_info)
271b9a1b977SAmir Goldstein {
272b9a1b977SAmir Goldstein 	struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
273fecc4559SAmir Goldstein 	struct fsnotify_mark *parent_mark = fsnotify_iter_parent_mark(iter_info);
274b9a1b977SAmir Goldstein 	int ret;
275b9a1b977SAmir Goldstein 
276b9a1b977SAmir Goldstein 	if (WARN_ON_ONCE(fsnotify_iter_sb_mark(iter_info)) ||
277b9a1b977SAmir Goldstein 	    WARN_ON_ONCE(fsnotify_iter_vfsmount_mark(iter_info)))
278b9a1b977SAmir Goldstein 		return 0;
279b9a1b977SAmir Goldstein 
280e54183faSAmir Goldstein 	/*
281e54183faSAmir Goldstein 	 * For FS_RENAME, 'dir' is old dir and 'data' is new dentry.
282e54183faSAmir Goldstein 	 * The only ->handle_inode_event() backend that supports FS_RENAME is
283e54183faSAmir Goldstein 	 * dnotify, where it means file was renamed within same parent.
284e54183faSAmir Goldstein 	 */
285e54183faSAmir Goldstein 	if (mask & FS_RENAME) {
286e54183faSAmir Goldstein 		struct dentry *moved = fsnotify_data_dentry(data, data_type);
287e54183faSAmir Goldstein 
288e54183faSAmir Goldstein 		if (dir != moved->d_parent->d_inode)
289e54183faSAmir Goldstein 			return 0;
290e54183faSAmir Goldstein 	}
291e54183faSAmir Goldstein 
292fecc4559SAmir Goldstein 	if (parent_mark) {
293b9a1b977SAmir Goldstein 		/*
294fecc4559SAmir Goldstein 		 * parent_mark indicates that the parent inode is watching
295fecc4559SAmir Goldstein 		 * children and interested in this event, which is an event
296fecc4559SAmir Goldstein 		 * possible on child. But is *this mark* watching children and
297fecc4559SAmir Goldstein 		 * interested in this event?
298b9a1b977SAmir Goldstein 		 */
299fecc4559SAmir Goldstein 		if (parent_mark->mask & FS_EVENT_ON_CHILD) {
300fecc4559SAmir Goldstein 			ret = fsnotify_handle_inode_event(group, parent_mark, mask,
301fecc4559SAmir Goldstein 							  data, data_type, dir, name, 0);
302fecc4559SAmir Goldstein 			if (ret)
303fecc4559SAmir Goldstein 				return ret;
304fecc4559SAmir Goldstein 		}
305fecc4559SAmir Goldstein 		if (!inode_mark)
306fecc4559SAmir Goldstein 			return 0;
307fecc4559SAmir Goldstein 	}
308fecc4559SAmir Goldstein 
309fecc4559SAmir Goldstein 	if (mask & FS_EVENT_ON_CHILD) {
310fecc4559SAmir Goldstein 		/*
311fecc4559SAmir Goldstein 		 * Some events can be sent on both parent dir and child marks
312fecc4559SAmir Goldstein 		 * (e.g. FS_ATTRIB).  If both parent dir and child are
313fecc4559SAmir Goldstein 		 * watching, report the event once to parent dir with name (if
314fecc4559SAmir Goldstein 		 * interested) and once to child without name (if interested).
315fecc4559SAmir Goldstein 		 * The child watcher is expecting an event without a file name
316fecc4559SAmir Goldstein 		 * and without the FS_EVENT_ON_CHILD flag.
317fecc4559SAmir Goldstein 		 */
318fecc4559SAmir Goldstein 		mask &= ~FS_EVENT_ON_CHILD;
319b9a1b977SAmir Goldstein 		dir = NULL;
320b9a1b977SAmir Goldstein 		name = NULL;
321b9a1b977SAmir Goldstein 	}
322b9a1b977SAmir Goldstein 
323fecc4559SAmir Goldstein 	return fsnotify_handle_inode_event(group, inode_mark, mask, data, data_type,
324950cc0d2SAmir Goldstein 					   dir, name, cookie);
325b9a1b977SAmir Goldstein }
326b9a1b977SAmir Goldstein 
327b54cecf5SAmir Goldstein static int send_to_group(__u32 mask, const void *data, int data_type,
328b54cecf5SAmir Goldstein 			 struct inode *dir, const struct qstr *file_name,
329b54cecf5SAmir Goldstein 			 u32 cookie, struct fsnotify_iter_info *iter_info)
3307131485aSEric Paris {
331faa9560aSEric Paris 	struct fsnotify_group *group = NULL;
332007d1e83SAmir Goldstein 	__u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
33392183a42SAmir Goldstein 	__u32 marks_mask = 0;
33492183a42SAmir Goldstein 	__u32 marks_ignored_mask = 0;
3353dca1a74SAmir Goldstein 	struct fsnotify_mark *mark;
3363dca1a74SAmir Goldstein 	int type;
337613a807fSEric Paris 
3385b0457adSAmir Goldstein 	if (WARN_ON(!iter_info->report_mask))
339faa9560aSEric Paris 		return 0;
3405ba08e2eSEric Paris 
341ce8f76fbSEric Paris 	/* clear ignored on inode modification */
342ce8f76fbSEric Paris 	if (mask & FS_MODIFY) {
3431c9007d6SAmir Goldstein 		fsnotify_foreach_iter_type(type) {
3443dca1a74SAmir Goldstein 			if (!fsnotify_iter_should_report_type(iter_info, type))
3453dca1a74SAmir Goldstein 				continue;
3463dca1a74SAmir Goldstein 			mark = iter_info->marks[type];
3473dca1a74SAmir Goldstein 			if (mark &&
3483dca1a74SAmir Goldstein 			    !(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
3493dca1a74SAmir Goldstein 				mark->ignored_mask = 0;
3503dca1a74SAmir Goldstein 		}
351ce8f76fbSEric Paris 	}
352613a807fSEric Paris 
3531c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
3543dca1a74SAmir Goldstein 		if (!fsnotify_iter_should_report_type(iter_info, type))
3553dca1a74SAmir Goldstein 			continue;
3563dca1a74SAmir Goldstein 		mark = iter_info->marks[type];
3573dca1a74SAmir Goldstein 		/* does the object mark tell us to do something? */
3583dca1a74SAmir Goldstein 		if (mark) {
3593dca1a74SAmir Goldstein 			group = mark->group;
3603dca1a74SAmir Goldstein 			marks_mask |= mark->mask;
3613dca1a74SAmir Goldstein 			marks_ignored_mask |= mark->ignored_mask;
3623dca1a74SAmir Goldstein 		}
363ce8f76fbSEric Paris 	}
364ce8f76fbSEric Paris 
365b54cecf5SAmir 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",
366b54cecf5SAmir Goldstein 		 __func__, group, mask, marks_mask, marks_ignored_mask,
367b54cecf5SAmir Goldstein 		 data, data_type, dir, cookie);
368faa9560aSEric Paris 
36992183a42SAmir Goldstein 	if (!(test_mask & marks_mask & ~marks_ignored_mask))
370613a807fSEric Paris 		return 0;
371613a807fSEric Paris 
372b9a1b977SAmir Goldstein 	if (group->ops->handle_event) {
373b54cecf5SAmir Goldstein 		return group->ops->handle_event(group, mask, data, data_type, dir,
3749385a84dSJan Kara 						file_name, cookie, iter_info);
3757131485aSEric Paris 	}
3767131485aSEric Paris 
377b9a1b977SAmir Goldstein 	return fsnotify_handle_event(group, mask, data, data_type, dir,
378b9a1b977SAmir Goldstein 				     file_name, cookie, iter_info);
379b9a1b977SAmir Goldstein }
380b9a1b977SAmir Goldstein 
3813427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector **connp)
3823427ce71SMiklos Szeredi {
3833427ce71SMiklos Szeredi 	struct fsnotify_mark_connector *conn;
3843427ce71SMiklos Szeredi 	struct hlist_node *node = NULL;
3853427ce71SMiklos Szeredi 
3863427ce71SMiklos Szeredi 	conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
3873427ce71SMiklos Szeredi 	if (conn)
3883427ce71SMiklos Szeredi 		node = srcu_dereference(conn->list.first, &fsnotify_mark_srcu);
3893427ce71SMiklos Szeredi 
3903427ce71SMiklos Szeredi 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
3913427ce71SMiklos Szeredi }
3923427ce71SMiklos Szeredi 
3933427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark)
3943427ce71SMiklos Szeredi {
3953427ce71SMiklos Szeredi 	struct hlist_node *node = NULL;
3963427ce71SMiklos Szeredi 
3973427ce71SMiklos Szeredi 	if (mark)
3983427ce71SMiklos Szeredi 		node = srcu_dereference(mark->obj_list.next,
3993427ce71SMiklos Szeredi 					&fsnotify_mark_srcu);
4003427ce71SMiklos Szeredi 
4013427ce71SMiklos Szeredi 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
4023427ce71SMiklos Szeredi }
4033427ce71SMiklos Szeredi 
404c28f7e56SEric Paris /*
405d9a6f30bSAmir Goldstein  * iter_info is a multi head priority queue of marks.
406d9a6f30bSAmir Goldstein  * Pick a subset of marks from queue heads, all with the
407d9a6f30bSAmir Goldstein  * same group and set the report_mask for selected subset.
408d9a6f30bSAmir Goldstein  * Returns the report_mask of the selected subset.
409d9a6f30bSAmir Goldstein  */
410d9a6f30bSAmir Goldstein static unsigned int fsnotify_iter_select_report_types(
411d9a6f30bSAmir Goldstein 		struct fsnotify_iter_info *iter_info)
412d9a6f30bSAmir Goldstein {
41347d9c7ccSAmir Goldstein 	struct fsnotify_group *max_prio_group = NULL;
41447d9c7ccSAmir Goldstein 	struct fsnotify_mark *mark;
41547d9c7ccSAmir Goldstein 	int type;
416d9a6f30bSAmir Goldstein 
41747d9c7ccSAmir Goldstein 	/* Choose max prio group among groups of all queue heads */
4181c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
41947d9c7ccSAmir Goldstein 		mark = iter_info->marks[type];
42047d9c7ccSAmir Goldstein 		if (mark &&
42147d9c7ccSAmir Goldstein 		    fsnotify_compare_groups(max_prio_group, mark->group) > 0)
42247d9c7ccSAmir Goldstein 			max_prio_group = mark->group;
423d9a6f30bSAmir Goldstein 	}
424d9a6f30bSAmir Goldstein 
42547d9c7ccSAmir Goldstein 	if (!max_prio_group)
42647d9c7ccSAmir Goldstein 		return 0;
42747d9c7ccSAmir Goldstein 
42847d9c7ccSAmir Goldstein 	/* Set the report mask for marks from same group as max prio group */
429d9a6f30bSAmir Goldstein 	iter_info->report_mask = 0;
4301c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
43147d9c7ccSAmir Goldstein 		mark = iter_info->marks[type];
43247d9c7ccSAmir Goldstein 		if (mark &&
43347d9c7ccSAmir Goldstein 		    fsnotify_compare_groups(max_prio_group, mark->group) == 0)
43447d9c7ccSAmir Goldstein 			fsnotify_iter_set_report_type(iter_info, type);
43547d9c7ccSAmir Goldstein 	}
436d9a6f30bSAmir Goldstein 
437d9a6f30bSAmir Goldstein 	return iter_info->report_mask;
438d9a6f30bSAmir Goldstein }
439d9a6f30bSAmir Goldstein 
440d9a6f30bSAmir Goldstein /*
441d9a6f30bSAmir Goldstein  * Pop from iter_info multi head queue, the marks that were iterated in the
442d9a6f30bSAmir Goldstein  * current iteration step.
443d9a6f30bSAmir Goldstein  */
444d9a6f30bSAmir Goldstein static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info)
445d9a6f30bSAmir Goldstein {
44647d9c7ccSAmir Goldstein 	int type;
447d9a6f30bSAmir Goldstein 
4481c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
44947d9c7ccSAmir Goldstein 		if (fsnotify_iter_should_report_type(iter_info, type))
45047d9c7ccSAmir Goldstein 			iter_info->marks[type] =
45147d9c7ccSAmir Goldstein 				fsnotify_next_mark(iter_info->marks[type]);
45247d9c7ccSAmir Goldstein 	}
453d9a6f30bSAmir Goldstein }
454d9a6f30bSAmir Goldstein 
455d9a6f30bSAmir Goldstein /*
45640a100d3SAmir Goldstein  * fsnotify - This is the main call to fsnotify.
45740a100d3SAmir Goldstein  *
45840a100d3SAmir Goldstein  * The VFS calls into hook specific functions in linux/fsnotify.h.
45940a100d3SAmir Goldstein  * Those functions then in turn call here.  Here will call out to all of the
46040a100d3SAmir Goldstein  * registered fsnotify_group.  Those groups can then use the notification event
46140a100d3SAmir Goldstein  * in whatever means they feel necessary.
46240a100d3SAmir Goldstein  *
46340a100d3SAmir Goldstein  * @mask:	event type and flags
46440a100d3SAmir Goldstein  * @data:	object that event happened on
46540a100d3SAmir Goldstein  * @data_type:	type of object for fanotify_data_XXX() accessors
46640a100d3SAmir Goldstein  * @dir:	optional directory associated with event -
46740a100d3SAmir Goldstein  *		if @file_name is not NULL, this is the directory that
46840a100d3SAmir Goldstein  *		@file_name is relative to
46940a100d3SAmir Goldstein  * @file_name:	optional file name associated with event
47040a100d3SAmir Goldstein  * @inode:	optional inode associated with event -
47129335033SGabriel Krisman Bertazi  *		If @dir and @inode are both non-NULL, event may be
47229335033SGabriel Krisman Bertazi  *		reported to both.
47340a100d3SAmir Goldstein  * @cookie:	inotify rename cookie
47490586523SEric Paris  */
47540a100d3SAmir Goldstein int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
47640a100d3SAmir Goldstein 	     const struct qstr *file_name, struct inode *inode, u32 cookie)
47790586523SEric Paris {
478b54cecf5SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
47929335033SGabriel Krisman Bertazi 	struct super_block *sb = fsnotify_data_sb(data, data_type);
4803427ce71SMiklos Szeredi 	struct fsnotify_iter_info iter_info = {};
48160f7ed8cSAmir Goldstein 	struct mount *mnt = NULL;
482e54183faSAmir Goldstein 	struct inode *inode2 = NULL;
483e54183faSAmir Goldstein 	struct dentry *moved;
484e54183faSAmir Goldstein 	int inode2_type;
4859385a84dSJan Kara 	int ret = 0;
48671d73410SMel Gorman 	__u32 test_mask, marks_mask;
48790586523SEric Paris 
48871d73410SMel Gorman 	if (path)
489aa93bdc5SAmir Goldstein 		mnt = real_mount(path->mnt);
490613a807fSEric Paris 
49140a100d3SAmir Goldstein 	if (!inode) {
49240a100d3SAmir Goldstein 		/* Dirent event - report on TYPE_INODE to dir */
49340a100d3SAmir Goldstein 		inode = dir;
494e54183faSAmir Goldstein 		/* For FS_RENAME, inode is old_dir and inode2 is new_dir */
495e54183faSAmir Goldstein 		if (mask & FS_RENAME) {
496e54183faSAmir Goldstein 			moved = fsnotify_data_dentry(data, data_type);
497e54183faSAmir Goldstein 			inode2 = moved->d_parent->d_inode;
498e54183faSAmir Goldstein 			inode2_type = FSNOTIFY_ITER_TYPE_INODE2;
499e54183faSAmir Goldstein 		}
50040a100d3SAmir Goldstein 	} else if (mask & FS_EVENT_ON_CHILD) {
50140a100d3SAmir Goldstein 		/*
502fecc4559SAmir Goldstein 		 * Event on child - report on TYPE_PARENT to dir if it is
503fecc4559SAmir Goldstein 		 * watching children and on TYPE_INODE to child.
50440a100d3SAmir Goldstein 		 */
505e54183faSAmir Goldstein 		inode2 = dir;
506e54183faSAmir Goldstein 		inode2_type = FSNOTIFY_ITER_TYPE_PARENT;
50740a100d3SAmir Goldstein 	}
508497b0c5aSAmir Goldstein 
509613a807fSEric Paris 	/*
5107c49b861SDave Hansen 	 * Optimization: srcu_read_lock() has a memory barrier which can
5117c49b861SDave Hansen 	 * be expensive.  It protects walking the *_fsnotify_marks lists.
5127c49b861SDave Hansen 	 * However, if we do not walk the lists, we do not have to do
5137c49b861SDave Hansen 	 * SRCU because we have no references to any objects and do not
5147c49b861SDave Hansen 	 * need SRCU to keep them "alive".
5157c49b861SDave Hansen 	 */
5169b93f331SAmir Goldstein 	if (!sb->s_fsnotify_marks &&
517497b0c5aSAmir Goldstein 	    (!mnt || !mnt->mnt_fsnotify_marks) &&
5189b93f331SAmir Goldstein 	    (!inode || !inode->i_fsnotify_marks) &&
519e54183faSAmir Goldstein 	    (!inode2 || !inode2->i_fsnotify_marks))
5207c49b861SDave Hansen 		return 0;
52171d73410SMel Gorman 
5229b93f331SAmir Goldstein 	marks_mask = sb->s_fsnotify_mask;
52371d73410SMel Gorman 	if (mnt)
52471d73410SMel Gorman 		marks_mask |= mnt->mnt_fsnotify_mask;
5259b93f331SAmir Goldstein 	if (inode)
5269b93f331SAmir Goldstein 		marks_mask |= inode->i_fsnotify_mask;
527e54183faSAmir Goldstein 	if (inode2)
528e54183faSAmir Goldstein 		marks_mask |= inode2->i_fsnotify_mask;
529497b0c5aSAmir Goldstein 
53071d73410SMel Gorman 
5317c49b861SDave Hansen 	/*
53204e317baSAmir Goldstein 	 * If this is a modify event we may need to clear some ignored masks.
53304e317baSAmir Goldstein 	 * In that case, the object with ignored masks will have the FS_MODIFY
53404e317baSAmir Goldstein 	 * event in its mask.
53504e317baSAmir Goldstein 	 * Otherwise, return if none of the marks care about this type of event.
536613a807fSEric Paris 	 */
53771d73410SMel Gorman 	test_mask = (mask & ALL_FSNOTIFY_EVENTS);
53804e317baSAmir Goldstein 	if (!(test_mask & marks_mask))
539613a807fSEric Paris 		return 0;
5403a9fb89fSEric Paris 
5419385a84dSJan Kara 	iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
5427131485aSEric Paris 
5431c9007d6SAmir Goldstein 	iter_info.marks[FSNOTIFY_ITER_TYPE_SB] =
54445a9fb37SAmir Goldstein 		fsnotify_first_mark(&sb->s_fsnotify_marks);
5459bdda4e9SAmir Goldstein 	if (mnt) {
5461c9007d6SAmir Goldstein 		iter_info.marks[FSNOTIFY_ITER_TYPE_VFSMOUNT] =
5473427ce71SMiklos Szeredi 			fsnotify_first_mark(&mnt->mnt_fsnotify_marks);
5487131485aSEric Paris 	}
5499b93f331SAmir Goldstein 	if (inode) {
5501c9007d6SAmir Goldstein 		iter_info.marks[FSNOTIFY_ITER_TYPE_INODE] =
5519b93f331SAmir Goldstein 			fsnotify_first_mark(&inode->i_fsnotify_marks);
5529b93f331SAmir Goldstein 	}
553e54183faSAmir Goldstein 	if (inode2) {
554e54183faSAmir Goldstein 		iter_info.marks[inode2_type] =
555e54183faSAmir Goldstein 			fsnotify_first_mark(&inode2->i_fsnotify_marks);
556497b0c5aSAmir Goldstein 	}
55775c1be48SEric Paris 
5588edc6e16SJan Kara 	/*
55960f7ed8cSAmir Goldstein 	 * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark
56060f7ed8cSAmir Goldstein 	 * ignore masks are properly reflected for mount/sb mark notifications.
5618edc6e16SJan Kara 	 * That's why this traversal is so complicated...
5628edc6e16SJan Kara 	 */
563d9a6f30bSAmir Goldstein 	while (fsnotify_iter_select_report_types(&iter_info)) {
564b54cecf5SAmir Goldstein 		ret = send_to_group(mask, data, data_type, dir, file_name,
565b54cecf5SAmir Goldstein 				    cookie, &iter_info);
56684a5b68eSEric Paris 
567ff8bcbd0SEric Paris 		if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS))
568ff8bcbd0SEric Paris 			goto out;
569ff8bcbd0SEric Paris 
570d9a6f30bSAmir Goldstein 		fsnotify_iter_next(&iter_info);
5717131485aSEric Paris 	}
572ff8bcbd0SEric Paris 	ret = 0;
573ff8bcbd0SEric Paris out:
5749385a84dSJan Kara 	srcu_read_unlock(&fsnotify_mark_srcu, iter_info.srcu_idx);
575c4ec54b4SEric Paris 
57698b5c10dSJean-Christophe Dubois 	return ret;
57790586523SEric Paris }
57890586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify);
57990586523SEric Paris 
58090586523SEric Paris static __init int fsnotify_init(void)
58190586523SEric Paris {
58275c1be48SEric Paris 	int ret;
58375c1be48SEric Paris 
584*38035c04SAmir Goldstein 	BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 23);
58520dee624SEric Paris 
58675c1be48SEric Paris 	ret = init_srcu_struct(&fsnotify_mark_srcu);
58775c1be48SEric Paris 	if (ret)
58875c1be48SEric Paris 		panic("initializing fsnotify_mark_srcu");
58975c1be48SEric Paris 
5909dd813c1SJan Kara 	fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector,
5919dd813c1SJan Kara 						    SLAB_PANIC);
5929dd813c1SJan Kara 
59375c1be48SEric Paris 	return 0;
59490586523SEric Paris }
59575c1be48SEric Paris core_initcall(fsnotify_init);
596