xref: /openbmc/linux/fs/notify/fsnotify.c (revision feee1ce4)
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  */
__fsnotify_inode_delete(struct inode * inode)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 
__fsnotify_vfsmount_delete(struct vfsmount * mnt)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  */
fsnotify_unmount_inodes(struct super_block * sb)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 
fsnotify_sb_delete(struct super_block * sb)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
103*feee1ce4SXin Gao  * parent cares.  Thus when an event happens on a child it can quickly tell
104c28f7e56SEric Paris  * if there is a need to find a parent and send the event to the parent.
105c28f7e56SEric Paris  */
__fsnotify_update_child_dentry_flags(struct inode * inode)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? */
fsnotify_event_needs_parent(struct inode * inode,struct mount * mnt,__u32 mask)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  */
__fsnotify_parent(struct dentry * dentry,__u32 mask,const void * data,int data_type)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 
fsnotify_handle_inode_event(struct fsnotify_group * group,struct fsnotify_mark * inode_mark,u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * name,u32 cookie)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 
25638035c04SAmir 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 
fsnotify_handle_event(struct fsnotify_group * group,__u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * name,u32 cookie,struct fsnotify_iter_info * iter_info)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) {
293fecc4559SAmir Goldstein 		ret = fsnotify_handle_inode_event(group, parent_mark, mask,
294fecc4559SAmir Goldstein 						  data, data_type, dir, name, 0);
295fecc4559SAmir Goldstein 		if (ret)
296fecc4559SAmir Goldstein 			return ret;
297fecc4559SAmir Goldstein 	}
298e730558aSAmir Goldstein 
299fecc4559SAmir Goldstein 	if (!inode_mark)
300fecc4559SAmir Goldstein 		return 0;
301fecc4559SAmir Goldstein 
302fecc4559SAmir Goldstein 	if (mask & FS_EVENT_ON_CHILD) {
303fecc4559SAmir Goldstein 		/*
304fecc4559SAmir Goldstein 		 * Some events can be sent on both parent dir and child marks
305fecc4559SAmir Goldstein 		 * (e.g. FS_ATTRIB).  If both parent dir and child are
306fecc4559SAmir Goldstein 		 * watching, report the event once to parent dir with name (if
307fecc4559SAmir Goldstein 		 * interested) and once to child without name (if interested).
308fecc4559SAmir Goldstein 		 * The child watcher is expecting an event without a file name
309fecc4559SAmir Goldstein 		 * and without the FS_EVENT_ON_CHILD flag.
310fecc4559SAmir Goldstein 		 */
311fecc4559SAmir Goldstein 		mask &= ~FS_EVENT_ON_CHILD;
312b9a1b977SAmir Goldstein 		dir = NULL;
313b9a1b977SAmir Goldstein 		name = NULL;
314b9a1b977SAmir Goldstein 	}
315b9a1b977SAmir Goldstein 
316fecc4559SAmir Goldstein 	return fsnotify_handle_inode_event(group, inode_mark, mask, data, data_type,
317950cc0d2SAmir Goldstein 					   dir, name, cookie);
318b9a1b977SAmir Goldstein }
319b9a1b977SAmir Goldstein 
send_to_group(__u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * file_name,u32 cookie,struct fsnotify_iter_info * iter_info)320b54cecf5SAmir Goldstein static int send_to_group(__u32 mask, const void *data, int data_type,
321b54cecf5SAmir Goldstein 			 struct inode *dir, const struct qstr *file_name,
322b54cecf5SAmir Goldstein 			 u32 cookie, struct fsnotify_iter_info *iter_info)
3237131485aSEric Paris {
324faa9560aSEric Paris 	struct fsnotify_group *group = NULL;
325007d1e83SAmir Goldstein 	__u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
32692183a42SAmir Goldstein 	__u32 marks_mask = 0;
32731a371e4SAmir Goldstein 	__u32 marks_ignore_mask = 0;
32831a371e4SAmir Goldstein 	bool is_dir = mask & FS_ISDIR;
3293dca1a74SAmir Goldstein 	struct fsnotify_mark *mark;
3303dca1a74SAmir Goldstein 	int type;
331613a807fSEric Paris 
33214362a25SAmir Goldstein 	if (!iter_info->report_mask)
333faa9560aSEric Paris 		return 0;
3345ba08e2eSEric Paris 
335ce8f76fbSEric Paris 	/* clear ignored on inode modification */
336ce8f76fbSEric Paris 	if (mask & FS_MODIFY) {
33714362a25SAmir Goldstein 		fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
33814362a25SAmir Goldstein 			if (!(mark->flags &
33914362a25SAmir Goldstein 			      FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
34031a371e4SAmir Goldstein 				mark->ignore_mask = 0;
3413dca1a74SAmir Goldstein 		}
342ce8f76fbSEric Paris 	}
343613a807fSEric Paris 
34414362a25SAmir Goldstein 	/* Are any of the group marks interested in this event? */
34514362a25SAmir Goldstein 	fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
3463dca1a74SAmir Goldstein 		group = mark->group;
3473dca1a74SAmir Goldstein 		marks_mask |= mark->mask;
34831a371e4SAmir Goldstein 		marks_ignore_mask |=
34931a371e4SAmir Goldstein 			fsnotify_effective_ignore_mask(mark, is_dir, type);
3503dca1a74SAmir Goldstein 	}
351ce8f76fbSEric Paris 
35231a371e4SAmir Goldstein 	pr_debug("%s: group=%p mask=%x marks_mask=%x marks_ignore_mask=%x data=%p data_type=%d dir=%p cookie=%d\n",
35331a371e4SAmir Goldstein 		 __func__, group, mask, marks_mask, marks_ignore_mask,
354b54cecf5SAmir Goldstein 		 data, data_type, dir, cookie);
355faa9560aSEric Paris 
35631a371e4SAmir Goldstein 	if (!(test_mask & marks_mask & ~marks_ignore_mask))
357613a807fSEric Paris 		return 0;
358613a807fSEric Paris 
359b9a1b977SAmir Goldstein 	if (group->ops->handle_event) {
360b54cecf5SAmir Goldstein 		return group->ops->handle_event(group, mask, data, data_type, dir,
3619385a84dSJan Kara 						file_name, cookie, iter_info);
3627131485aSEric Paris 	}
3637131485aSEric Paris 
364b9a1b977SAmir Goldstein 	return fsnotify_handle_event(group, mask, data, data_type, dir,
365b9a1b977SAmir Goldstein 				     file_name, cookie, iter_info);
366b9a1b977SAmir Goldstein }
367b9a1b977SAmir Goldstein 
fsnotify_first_mark(struct fsnotify_mark_connector ** connp)3683427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector **connp)
3693427ce71SMiklos Szeredi {
3703427ce71SMiklos Szeredi 	struct fsnotify_mark_connector *conn;
3713427ce71SMiklos Szeredi 	struct hlist_node *node = NULL;
3723427ce71SMiklos Szeredi 
3733427ce71SMiklos Szeredi 	conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
3743427ce71SMiklos Szeredi 	if (conn)
3753427ce71SMiklos Szeredi 		node = srcu_dereference(conn->list.first, &fsnotify_mark_srcu);
3763427ce71SMiklos Szeredi 
3773427ce71SMiklos Szeredi 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
3783427ce71SMiklos Szeredi }
3793427ce71SMiklos Szeredi 
fsnotify_next_mark(struct fsnotify_mark * mark)3803427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark)
3813427ce71SMiklos Szeredi {
3823427ce71SMiklos Szeredi 	struct hlist_node *node = NULL;
3833427ce71SMiklos Szeredi 
3843427ce71SMiklos Szeredi 	if (mark)
3853427ce71SMiklos Szeredi 		node = srcu_dereference(mark->obj_list.next,
3863427ce71SMiklos Szeredi 					&fsnotify_mark_srcu);
3873427ce71SMiklos Szeredi 
3883427ce71SMiklos Szeredi 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
3893427ce71SMiklos Szeredi }
3903427ce71SMiklos Szeredi 
391c28f7e56SEric Paris /*
392d9a6f30bSAmir Goldstein  * iter_info is a multi head priority queue of marks.
39314362a25SAmir Goldstein  * Pick a subset of marks from queue heads, all with the same group
39414362a25SAmir Goldstein  * and set the report_mask to a subset of the selected marks.
39514362a25SAmir Goldstein  * Returns false if there are no more groups to iterate.
396d9a6f30bSAmir Goldstein  */
fsnotify_iter_select_report_types(struct fsnotify_iter_info * iter_info)39714362a25SAmir Goldstein static bool fsnotify_iter_select_report_types(
398d9a6f30bSAmir Goldstein 		struct fsnotify_iter_info *iter_info)
399d9a6f30bSAmir Goldstein {
40047d9c7ccSAmir Goldstein 	struct fsnotify_group *max_prio_group = NULL;
40147d9c7ccSAmir Goldstein 	struct fsnotify_mark *mark;
40247d9c7ccSAmir Goldstein 	int type;
403d9a6f30bSAmir Goldstein 
40447d9c7ccSAmir Goldstein 	/* Choose max prio group among groups of all queue heads */
4051c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
40647d9c7ccSAmir Goldstein 		mark = iter_info->marks[type];
40747d9c7ccSAmir Goldstein 		if (mark &&
40847d9c7ccSAmir Goldstein 		    fsnotify_compare_groups(max_prio_group, mark->group) > 0)
40947d9c7ccSAmir Goldstein 			max_prio_group = mark->group;
410d9a6f30bSAmir Goldstein 	}
411d9a6f30bSAmir Goldstein 
41247d9c7ccSAmir Goldstein 	if (!max_prio_group)
41314362a25SAmir Goldstein 		return false;
41447d9c7ccSAmir Goldstein 
41547d9c7ccSAmir Goldstein 	/* Set the report mask for marks from same group as max prio group */
41614362a25SAmir Goldstein 	iter_info->current_group = max_prio_group;
417d9a6f30bSAmir Goldstein 	iter_info->report_mask = 0;
4181c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
41947d9c7ccSAmir Goldstein 		mark = iter_info->marks[type];
420e730558aSAmir Goldstein 		if (mark && mark->group == iter_info->current_group) {
421e730558aSAmir Goldstein 			/*
422e730558aSAmir Goldstein 			 * FSNOTIFY_ITER_TYPE_PARENT indicates that this inode
423e730558aSAmir Goldstein 			 * is watching children and interested in this event,
424e730558aSAmir Goldstein 			 * which is an event possible on child.
425e730558aSAmir Goldstein 			 * But is *this mark* watching children?
426e730558aSAmir Goldstein 			 */
427e730558aSAmir Goldstein 			if (type == FSNOTIFY_ITER_TYPE_PARENT &&
42831a371e4SAmir Goldstein 			    !(mark->mask & FS_EVENT_ON_CHILD) &&
42931a371e4SAmir Goldstein 			    !(fsnotify_ignore_mask(mark) & FS_EVENT_ON_CHILD))
430e730558aSAmir Goldstein 				continue;
431e730558aSAmir Goldstein 
43247d9c7ccSAmir Goldstein 			fsnotify_iter_set_report_type(iter_info, type);
43347d9c7ccSAmir Goldstein 		}
434e730558aSAmir Goldstein 	}
435d9a6f30bSAmir Goldstein 
43614362a25SAmir Goldstein 	return true;
437d9a6f30bSAmir Goldstein }
438d9a6f30bSAmir Goldstein 
439d9a6f30bSAmir Goldstein /*
44014362a25SAmir Goldstein  * Pop from iter_info multi head queue, the marks that belong to the group of
441d9a6f30bSAmir Goldstein  * current iteration step.
442d9a6f30bSAmir Goldstein  */
fsnotify_iter_next(struct fsnotify_iter_info * iter_info)443d9a6f30bSAmir Goldstein static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info)
444d9a6f30bSAmir Goldstein {
44514362a25SAmir Goldstein 	struct fsnotify_mark *mark;
44647d9c7ccSAmir Goldstein 	int type;
447d9a6f30bSAmir Goldstein 
44814362a25SAmir Goldstein 	/*
44914362a25SAmir Goldstein 	 * We cannot use fsnotify_foreach_iter_mark_type() here because we
45014362a25SAmir Goldstein 	 * may need to advance a mark of type X that belongs to current_group
45114362a25SAmir Goldstein 	 * but was not selected for reporting.
45214362a25SAmir Goldstein 	 */
4531c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
45414362a25SAmir Goldstein 		mark = iter_info->marks[type];
45514362a25SAmir Goldstein 		if (mark && mark->group == iter_info->current_group)
45647d9c7ccSAmir Goldstein 			iter_info->marks[type] =
45747d9c7ccSAmir Goldstein 				fsnotify_next_mark(iter_info->marks[type]);
45847d9c7ccSAmir Goldstein 	}
459d9a6f30bSAmir Goldstein }
460d9a6f30bSAmir Goldstein 
461d9a6f30bSAmir Goldstein /*
46240a100d3SAmir Goldstein  * fsnotify - This is the main call to fsnotify.
46340a100d3SAmir Goldstein  *
46440a100d3SAmir Goldstein  * The VFS calls into hook specific functions in linux/fsnotify.h.
46540a100d3SAmir Goldstein  * Those functions then in turn call here.  Here will call out to all of the
46640a100d3SAmir Goldstein  * registered fsnotify_group.  Those groups can then use the notification event
46740a100d3SAmir Goldstein  * in whatever means they feel necessary.
46840a100d3SAmir Goldstein  *
46940a100d3SAmir Goldstein  * @mask:	event type and flags
47040a100d3SAmir Goldstein  * @data:	object that event happened on
47140a100d3SAmir Goldstein  * @data_type:	type of object for fanotify_data_XXX() accessors
47240a100d3SAmir Goldstein  * @dir:	optional directory associated with event -
47340a100d3SAmir Goldstein  *		if @file_name is not NULL, this is the directory that
47440a100d3SAmir Goldstein  *		@file_name is relative to
47540a100d3SAmir Goldstein  * @file_name:	optional file name associated with event
47640a100d3SAmir Goldstein  * @inode:	optional inode associated with event -
47729335033SGabriel Krisman Bertazi  *		If @dir and @inode are both non-NULL, event may be
47829335033SGabriel Krisman Bertazi  *		reported to both.
47940a100d3SAmir Goldstein  * @cookie:	inotify rename cookie
48090586523SEric Paris  */
fsnotify(__u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * file_name,struct inode * inode,u32 cookie)48140a100d3SAmir Goldstein int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
48240a100d3SAmir Goldstein 	     const struct qstr *file_name, struct inode *inode, u32 cookie)
48390586523SEric Paris {
484b54cecf5SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
48529335033SGabriel Krisman Bertazi 	struct super_block *sb = fsnotify_data_sb(data, data_type);
4863427ce71SMiklos Szeredi 	struct fsnotify_iter_info iter_info = {};
48760f7ed8cSAmir Goldstein 	struct mount *mnt = NULL;
488e54183faSAmir Goldstein 	struct inode *inode2 = NULL;
489e54183faSAmir Goldstein 	struct dentry *moved;
490e54183faSAmir Goldstein 	int inode2_type;
4919385a84dSJan Kara 	int ret = 0;
49271d73410SMel Gorman 	__u32 test_mask, marks_mask;
49390586523SEric Paris 
49471d73410SMel Gorman 	if (path)
495aa93bdc5SAmir Goldstein 		mnt = real_mount(path->mnt);
496613a807fSEric Paris 
49740a100d3SAmir Goldstein 	if (!inode) {
49840a100d3SAmir Goldstein 		/* Dirent event - report on TYPE_INODE to dir */
49940a100d3SAmir Goldstein 		inode = dir;
500e54183faSAmir Goldstein 		/* For FS_RENAME, inode is old_dir and inode2 is new_dir */
501e54183faSAmir Goldstein 		if (mask & FS_RENAME) {
502e54183faSAmir Goldstein 			moved = fsnotify_data_dentry(data, data_type);
503e54183faSAmir Goldstein 			inode2 = moved->d_parent->d_inode;
504e54183faSAmir Goldstein 			inode2_type = FSNOTIFY_ITER_TYPE_INODE2;
505e54183faSAmir Goldstein 		}
50640a100d3SAmir Goldstein 	} else if (mask & FS_EVENT_ON_CHILD) {
50740a100d3SAmir Goldstein 		/*
508fecc4559SAmir Goldstein 		 * Event on child - report on TYPE_PARENT to dir if it is
509fecc4559SAmir Goldstein 		 * watching children and on TYPE_INODE to child.
51040a100d3SAmir Goldstein 		 */
511e54183faSAmir Goldstein 		inode2 = dir;
512e54183faSAmir Goldstein 		inode2_type = FSNOTIFY_ITER_TYPE_PARENT;
51340a100d3SAmir Goldstein 	}
514497b0c5aSAmir Goldstein 
515613a807fSEric Paris 	/*
5167c49b861SDave Hansen 	 * Optimization: srcu_read_lock() has a memory barrier which can
5177c49b861SDave Hansen 	 * be expensive.  It protects walking the *_fsnotify_marks lists.
5187c49b861SDave Hansen 	 * However, if we do not walk the lists, we do not have to do
5197c49b861SDave Hansen 	 * SRCU because we have no references to any objects and do not
5207c49b861SDave Hansen 	 * need SRCU to keep them "alive".
5217c49b861SDave Hansen 	 */
5229b93f331SAmir Goldstein 	if (!sb->s_fsnotify_marks &&
523497b0c5aSAmir Goldstein 	    (!mnt || !mnt->mnt_fsnotify_marks) &&
5249b93f331SAmir Goldstein 	    (!inode || !inode->i_fsnotify_marks) &&
525e54183faSAmir Goldstein 	    (!inode2 || !inode2->i_fsnotify_marks))
5267c49b861SDave Hansen 		return 0;
52771d73410SMel Gorman 
5289b93f331SAmir Goldstein 	marks_mask = sb->s_fsnotify_mask;
52971d73410SMel Gorman 	if (mnt)
53071d73410SMel Gorman 		marks_mask |= mnt->mnt_fsnotify_mask;
5319b93f331SAmir Goldstein 	if (inode)
5329b93f331SAmir Goldstein 		marks_mask |= inode->i_fsnotify_mask;
533e54183faSAmir Goldstein 	if (inode2)
534e54183faSAmir Goldstein 		marks_mask |= inode2->i_fsnotify_mask;
535497b0c5aSAmir Goldstein 
53671d73410SMel Gorman 
5377c49b861SDave Hansen 	/*
53831a371e4SAmir Goldstein 	 * If this is a modify event we may need to clear some ignore masks.
53931a371e4SAmir Goldstein 	 * In that case, the object with ignore masks will have the FS_MODIFY
54004e317baSAmir Goldstein 	 * event in its mask.
54104e317baSAmir Goldstein 	 * Otherwise, return if none of the marks care about this type of event.
542613a807fSEric Paris 	 */
54371d73410SMel Gorman 	test_mask = (mask & ALL_FSNOTIFY_EVENTS);
54404e317baSAmir Goldstein 	if (!(test_mask & marks_mask))
545613a807fSEric Paris 		return 0;
5463a9fb89fSEric Paris 
5479385a84dSJan Kara 	iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
5487131485aSEric Paris 
5491c9007d6SAmir Goldstein 	iter_info.marks[FSNOTIFY_ITER_TYPE_SB] =
55045a9fb37SAmir Goldstein 		fsnotify_first_mark(&sb->s_fsnotify_marks);
5519bdda4e9SAmir Goldstein 	if (mnt) {
5521c9007d6SAmir Goldstein 		iter_info.marks[FSNOTIFY_ITER_TYPE_VFSMOUNT] =
5533427ce71SMiklos Szeredi 			fsnotify_first_mark(&mnt->mnt_fsnotify_marks);
5547131485aSEric Paris 	}
5559b93f331SAmir Goldstein 	if (inode) {
5561c9007d6SAmir Goldstein 		iter_info.marks[FSNOTIFY_ITER_TYPE_INODE] =
5579b93f331SAmir Goldstein 			fsnotify_first_mark(&inode->i_fsnotify_marks);
5589b93f331SAmir Goldstein 	}
559e54183faSAmir Goldstein 	if (inode2) {
560e54183faSAmir Goldstein 		iter_info.marks[inode2_type] =
561e54183faSAmir Goldstein 			fsnotify_first_mark(&inode2->i_fsnotify_marks);
562497b0c5aSAmir Goldstein 	}
56375c1be48SEric Paris 
5648edc6e16SJan Kara 	/*
56560f7ed8cSAmir Goldstein 	 * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark
56660f7ed8cSAmir Goldstein 	 * ignore masks are properly reflected for mount/sb mark notifications.
5678edc6e16SJan Kara 	 * That's why this traversal is so complicated...
5688edc6e16SJan Kara 	 */
569d9a6f30bSAmir Goldstein 	while (fsnotify_iter_select_report_types(&iter_info)) {
570b54cecf5SAmir Goldstein 		ret = send_to_group(mask, data, data_type, dir, file_name,
571b54cecf5SAmir Goldstein 				    cookie, &iter_info);
57284a5b68eSEric Paris 
573ff8bcbd0SEric Paris 		if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS))
574ff8bcbd0SEric Paris 			goto out;
575ff8bcbd0SEric Paris 
576d9a6f30bSAmir Goldstein 		fsnotify_iter_next(&iter_info);
5777131485aSEric Paris 	}
578ff8bcbd0SEric Paris 	ret = 0;
579ff8bcbd0SEric Paris out:
5809385a84dSJan Kara 	srcu_read_unlock(&fsnotify_mark_srcu, iter_info.srcu_idx);
581c4ec54b4SEric Paris 
58298b5c10dSJean-Christophe Dubois 	return ret;
58390586523SEric Paris }
58490586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify);
58590586523SEric Paris 
fsnotify_init(void)58690586523SEric Paris static __init int fsnotify_init(void)
58790586523SEric Paris {
58875c1be48SEric Paris 	int ret;
58975c1be48SEric Paris 
59038035c04SAmir Goldstein 	BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 23);
59120dee624SEric Paris 
59275c1be48SEric Paris 	ret = init_srcu_struct(&fsnotify_mark_srcu);
59375c1be48SEric Paris 	if (ret)
59475c1be48SEric Paris 		panic("initializing fsnotify_mark_srcu");
59575c1be48SEric Paris 
5969dd813c1SJan Kara 	fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector,
5979dd813c1SJan Kara 						    SLAB_PANIC);
5989dd813c1SJan Kara 
59975c1be48SEric Paris 	return 0;
60090586523SEric Paris }
60175c1be48SEric Paris core_initcall(fsnotify_init);
602