xref: /openbmc/linux/fs/notify/fsnotify.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
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
103feee1ce4SXin 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_set_children_dentry_flags(struct inode * inode)106fc1b1e13SAmir Goldstein void fsnotify_set_children_dentry_flags(struct inode *inode)
107c28f7e56SEric Paris {
108c28f7e56SEric Paris 	struct dentry *alias;
109c28f7e56SEric Paris 
110c28f7e56SEric Paris 	if (!S_ISDIR(inode->i_mode))
111c28f7e56SEric Paris 		return;
112c28f7e56SEric Paris 
113873feea0SNick Piggin 	spin_lock(&inode->i_lock);
114c28f7e56SEric Paris 	/* run all of the dentries associated with this inode.  Since this is a
115c28f7e56SEric Paris 	 * directory, there damn well better only be one item on this list */
116946e51f2SAl Viro 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
117c28f7e56SEric Paris 		struct dentry *child;
118c28f7e56SEric Paris 
119c28f7e56SEric Paris 		/* run all of the children of the original inode and fix their
120c28f7e56SEric Paris 		 * d_flags to indicate parental interest (their parent is the
121c28f7e56SEric Paris 		 * original inode) */
1222fd6b7f5SNick Piggin 		spin_lock(&alias->d_lock);
123946e51f2SAl Viro 		list_for_each_entry(child, &alias->d_subdirs, d_child) {
124c28f7e56SEric Paris 			if (!child->d_inode)
125c28f7e56SEric Paris 				continue;
126c28f7e56SEric Paris 
1272fd6b7f5SNick Piggin 			spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
128c28f7e56SEric Paris 			child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
129c28f7e56SEric Paris 			spin_unlock(&child->d_lock);
130c28f7e56SEric Paris 		}
1312fd6b7f5SNick Piggin 		spin_unlock(&alias->d_lock);
132c28f7e56SEric Paris 	}
133873feea0SNick Piggin 	spin_unlock(&inode->i_lock);
134c28f7e56SEric Paris }
135c28f7e56SEric Paris 
136fc1b1e13SAmir Goldstein /*
137fc1b1e13SAmir Goldstein  * Lazily clear false positive PARENT_WATCHED flag for child whose parent had
138fc1b1e13SAmir Goldstein  * stopped watching children.
139fc1b1e13SAmir Goldstein  */
fsnotify_clear_child_dentry_flag(struct inode * pinode,struct dentry * dentry)140fc1b1e13SAmir Goldstein static void fsnotify_clear_child_dentry_flag(struct inode *pinode,
141fc1b1e13SAmir Goldstein 					     struct dentry *dentry)
142fc1b1e13SAmir Goldstein {
143fc1b1e13SAmir Goldstein 	spin_lock(&dentry->d_lock);
144fc1b1e13SAmir Goldstein 	/*
145fc1b1e13SAmir Goldstein 	 * d_lock is a sufficient barrier to prevent observing a non-watched
146fc1b1e13SAmir Goldstein 	 * parent state from before the fsnotify_set_children_dentry_flags()
147fc1b1e13SAmir Goldstein 	 * or fsnotify_update_flags() call that had set PARENT_WATCHED.
148fc1b1e13SAmir Goldstein 	 */
149fc1b1e13SAmir Goldstein 	if (!fsnotify_inode_watches_children(pinode))
150fc1b1e13SAmir Goldstein 		dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
151fc1b1e13SAmir Goldstein 	spin_unlock(&dentry->d_lock);
152fc1b1e13SAmir Goldstein }
153fc1b1e13SAmir Goldstein 
1549b93f331SAmir 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)1559b93f331SAmir Goldstein static bool fsnotify_event_needs_parent(struct inode *inode, struct mount *mnt,
1569b93f331SAmir Goldstein 					__u32 mask)
1579b93f331SAmir Goldstein {
1589b93f331SAmir Goldstein 	__u32 marks_mask = 0;
1599b93f331SAmir Goldstein 
1609b93f331SAmir Goldstein 	/* We only send parent/name to inode/sb/mount for events on non-dir */
1619b93f331SAmir Goldstein 	if (mask & FS_ISDIR)
1629b93f331SAmir Goldstein 		return false;
1639b93f331SAmir Goldstein 
164fecc4559SAmir Goldstein 	/*
165fecc4559SAmir Goldstein 	 * All events that are possible on child can also may be reported with
166fecc4559SAmir Goldstein 	 * parent/name info to inode/sb/mount.  Otherwise, a watching parent
167fecc4559SAmir Goldstein 	 * could result in events reported with unexpected name info to sb/mount.
168fecc4559SAmir Goldstein 	 */
169fecc4559SAmir Goldstein 	BUILD_BUG_ON(FS_EVENTS_POSS_ON_CHILD & ~FS_EVENTS_POSS_TO_PARENT);
170fecc4559SAmir Goldstein 
1719b93f331SAmir Goldstein 	/* Did either inode/sb/mount subscribe for events with parent/name? */
1729b93f331SAmir Goldstein 	marks_mask |= fsnotify_parent_needed_mask(inode->i_fsnotify_mask);
1739b93f331SAmir Goldstein 	marks_mask |= fsnotify_parent_needed_mask(inode->i_sb->s_fsnotify_mask);
1749b93f331SAmir Goldstein 	if (mnt)
1759b93f331SAmir Goldstein 		marks_mask |= fsnotify_parent_needed_mask(mnt->mnt_fsnotify_mask);
1769b93f331SAmir Goldstein 
1779b93f331SAmir Goldstein 	/* Did they subscribe for this event with parent/name info? */
1789b93f331SAmir Goldstein 	return mask & marks_mask;
1799b93f331SAmir Goldstein }
1809b93f331SAmir Goldstein 
181c738fbabSAmir Goldstein /*
182c738fbabSAmir Goldstein  * Notify this dentry's parent about a child's events with child name info
1839b93f331SAmir Goldstein  * if parent is watching or if inode/sb/mount are interested in events with
1849b93f331SAmir Goldstein  * parent and name info.
1859b93f331SAmir Goldstein  *
1869b93f331SAmir Goldstein  * Notify only the child without name info if parent is not watching and
1879b93f331SAmir Goldstein  * inode/sb/mount are not interested in events with parent and name info.
188c738fbabSAmir Goldstein  */
__fsnotify_parent(struct dentry * dentry,__u32 mask,const void * data,int data_type)18971d73410SMel Gorman int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
190017de65fSAmir Goldstein 		      int data_type)
191c28f7e56SEric Paris {
1929b93f331SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
1939b93f331SAmir Goldstein 	struct mount *mnt = path ? real_mount(path->mnt) : NULL;
194497b0c5aSAmir Goldstein 	struct inode *inode = d_inode(dentry);
195c28f7e56SEric Paris 	struct dentry *parent;
1969b93f331SAmir Goldstein 	bool parent_watched = dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED;
1977372e79cSAmir Goldstein 	bool parent_needed, parent_interested;
1989b93f331SAmir Goldstein 	__u32 p_mask;
19940a100d3SAmir Goldstein 	struct inode *p_inode = NULL;
200497b0c5aSAmir Goldstein 	struct name_snapshot name;
201497b0c5aSAmir Goldstein 	struct qstr *file_name = NULL;
20252420392SEric Paris 	int ret = 0;
203c28f7e56SEric Paris 
2049b93f331SAmir Goldstein 	/*
2059b93f331SAmir Goldstein 	 * Do inode/sb/mount care about parent and name info on non-dir?
2069b93f331SAmir Goldstein 	 * Do they care about any event at all?
2079b93f331SAmir Goldstein 	 */
2089b93f331SAmir Goldstein 	if (!inode->i_fsnotify_marks && !inode->i_sb->s_fsnotify_marks &&
2099b93f331SAmir Goldstein 	    (!mnt || !mnt->mnt_fsnotify_marks) && !parent_watched)
2109b93f331SAmir Goldstein 		return 0;
2119b93f331SAmir Goldstein 
212497b0c5aSAmir Goldstein 	parent = NULL;
2137372e79cSAmir Goldstein 	parent_needed = fsnotify_event_needs_parent(inode, mnt, mask);
2147372e79cSAmir Goldstein 	if (!parent_watched && !parent_needed)
215497b0c5aSAmir Goldstein 		goto notify;
216c28f7e56SEric Paris 
2179b93f331SAmir Goldstein 	/* Does parent inode care about events on children? */
2184d4eb366SChristoph Hellwig 	parent = dget_parent(dentry);
219c28f7e56SEric Paris 	p_inode = parent->d_inode;
2209b93f331SAmir Goldstein 	p_mask = fsnotify_inode_watches_children(p_inode);
2219b93f331SAmir Goldstein 	if (unlikely(parent_watched && !p_mask))
222fc1b1e13SAmir Goldstein 		fsnotify_clear_child_dentry_flag(p_inode, dentry);
2239b93f331SAmir Goldstein 
2249b93f331SAmir Goldstein 	/*
2259b93f331SAmir Goldstein 	 * Include parent/name in notification either if some notification
2267372e79cSAmir Goldstein 	 * groups require parent info or the parent is interested in this event.
2279b93f331SAmir Goldstein 	 */
2287372e79cSAmir Goldstein 	parent_interested = mask & p_mask & ALL_FSNOTIFY_EVENTS;
2297372e79cSAmir Goldstein 	if (parent_needed || parent_interested) {
230497b0c5aSAmir Goldstein 		/* When notifying parent, child should be passed as data */
231497b0c5aSAmir Goldstein 		WARN_ON_ONCE(inode != fsnotify_data_inode(data, data_type));
23249d31c2fSAl Viro 
233497b0c5aSAmir Goldstein 		/* Notify both parent and child with child name info */
23449d31c2fSAl Viro 		take_dentry_name_snapshot(&name, dentry);
235497b0c5aSAmir Goldstein 		file_name = &name.name;
2367372e79cSAmir Goldstein 		if (parent_interested)
237497b0c5aSAmir Goldstein 			mask |= FS_EVENT_ON_CHILD;
238c28f7e56SEric Paris 	}
239c28f7e56SEric Paris 
240497b0c5aSAmir Goldstein notify:
24140a100d3SAmir Goldstein 	ret = fsnotify(mask, data, data_type, p_inode, file_name, inode, 0);
242497b0c5aSAmir Goldstein 
243497b0c5aSAmir Goldstein 	if (file_name)
244497b0c5aSAmir Goldstein 		release_dentry_name_snapshot(&name);
245c28f7e56SEric Paris 	dput(parent);
24652420392SEric Paris 
24752420392SEric Paris 	return ret;
248c28f7e56SEric Paris }
24971d73410SMel Gorman EXPORT_SYMBOL_GPL(__fsnotify_parent);
250c28f7e56SEric 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)251950cc0d2SAmir Goldstein static int fsnotify_handle_inode_event(struct fsnotify_group *group,
252950cc0d2SAmir Goldstein 				       struct fsnotify_mark *inode_mark,
253950cc0d2SAmir Goldstein 				       u32 mask, const void *data, int data_type,
254950cc0d2SAmir Goldstein 				       struct inode *dir, const struct qstr *name,
255950cc0d2SAmir Goldstein 				       u32 cookie)
256950cc0d2SAmir Goldstein {
257950cc0d2SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
258950cc0d2SAmir Goldstein 	struct inode *inode = fsnotify_data_inode(data, data_type);
259950cc0d2SAmir Goldstein 	const struct fsnotify_ops *ops = group->ops;
260950cc0d2SAmir Goldstein 
261950cc0d2SAmir Goldstein 	if (WARN_ON_ONCE(!ops->handle_inode_event))
262950cc0d2SAmir Goldstein 		return 0;
263950cc0d2SAmir Goldstein 
26424dca905SGabriel Krisman Bertazi 	if (WARN_ON_ONCE(!inode && !dir))
26524dca905SGabriel Krisman Bertazi 		return 0;
26624dca905SGabriel Krisman Bertazi 
26738035c04SAmir Goldstein 	if ((inode_mark->flags & FSNOTIFY_MARK_FLAG_EXCL_UNLINK) &&
268950cc0d2SAmir Goldstein 	    path && d_unlinked(path->dentry))
269950cc0d2SAmir Goldstein 		return 0;
270950cc0d2SAmir Goldstein 
271fecc4559SAmir Goldstein 	/* Check interest of this mark in case event was sent with two marks */
272fecc4559SAmir Goldstein 	if (!(mask & inode_mark->mask & ALL_FSNOTIFY_EVENTS))
273fecc4559SAmir Goldstein 		return 0;
274fecc4559SAmir Goldstein 
275950cc0d2SAmir Goldstein 	return ops->handle_inode_event(inode_mark, mask, inode, dir, name, cookie);
276950cc0d2SAmir Goldstein }
277950cc0d2SAmir 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)278b9a1b977SAmir Goldstein static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
279b9a1b977SAmir Goldstein 				 const void *data, int data_type,
280b9a1b977SAmir Goldstein 				 struct inode *dir, const struct qstr *name,
281b9a1b977SAmir Goldstein 				 u32 cookie, struct fsnotify_iter_info *iter_info)
282b9a1b977SAmir Goldstein {
283b9a1b977SAmir Goldstein 	struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
284fecc4559SAmir Goldstein 	struct fsnotify_mark *parent_mark = fsnotify_iter_parent_mark(iter_info);
285b9a1b977SAmir Goldstein 	int ret;
286b9a1b977SAmir Goldstein 
287b9a1b977SAmir Goldstein 	if (WARN_ON_ONCE(fsnotify_iter_sb_mark(iter_info)) ||
288b9a1b977SAmir Goldstein 	    WARN_ON_ONCE(fsnotify_iter_vfsmount_mark(iter_info)))
289b9a1b977SAmir Goldstein 		return 0;
290b9a1b977SAmir Goldstein 
291e54183faSAmir Goldstein 	/*
292e54183faSAmir Goldstein 	 * For FS_RENAME, 'dir' is old dir and 'data' is new dentry.
293e54183faSAmir Goldstein 	 * The only ->handle_inode_event() backend that supports FS_RENAME is
294e54183faSAmir Goldstein 	 * dnotify, where it means file was renamed within same parent.
295e54183faSAmir Goldstein 	 */
296e54183faSAmir Goldstein 	if (mask & FS_RENAME) {
297e54183faSAmir Goldstein 		struct dentry *moved = fsnotify_data_dentry(data, data_type);
298e54183faSAmir Goldstein 
299e54183faSAmir Goldstein 		if (dir != moved->d_parent->d_inode)
300e54183faSAmir Goldstein 			return 0;
301e54183faSAmir Goldstein 	}
302e54183faSAmir Goldstein 
303fecc4559SAmir Goldstein 	if (parent_mark) {
304fecc4559SAmir Goldstein 		ret = fsnotify_handle_inode_event(group, parent_mark, mask,
305fecc4559SAmir Goldstein 						  data, data_type, dir, name, 0);
306fecc4559SAmir Goldstein 		if (ret)
307fecc4559SAmir Goldstein 			return ret;
308fecc4559SAmir Goldstein 	}
309e730558aSAmir Goldstein 
310fecc4559SAmir Goldstein 	if (!inode_mark)
311fecc4559SAmir Goldstein 		return 0;
312fecc4559SAmir Goldstein 
313fecc4559SAmir Goldstein 	/*
314*1b8868b8SAmir Goldstein 	 * Some events can be sent on both parent dir and child marks (e.g.
315*1b8868b8SAmir Goldstein 	 * FS_ATTRIB).  If both parent dir and child are watching, report the
316*1b8868b8SAmir Goldstein 	 * event once to parent dir with name (if interested) and once to child
317*1b8868b8SAmir Goldstein 	 * without name (if interested).
318*1b8868b8SAmir Goldstein 	 *
319*1b8868b8SAmir Goldstein 	 * In any case regardless whether the parent is watching or not, the
320*1b8868b8SAmir Goldstein 	 * child watcher is expecting an event without the FS_EVENT_ON_CHILD
321*1b8868b8SAmir Goldstein 	 * flag. The file name is expected if and only if this is a directory
322*1b8868b8SAmir Goldstein 	 * event.
323fecc4559SAmir Goldstein 	 */
324fecc4559SAmir Goldstein 	mask &= ~FS_EVENT_ON_CHILD;
325*1b8868b8SAmir Goldstein 	if (!(mask & ALL_FSNOTIFY_DIRENT_EVENTS)) {
326b9a1b977SAmir Goldstein 		dir = NULL;
327b9a1b977SAmir Goldstein 		name = NULL;
328b9a1b977SAmir Goldstein 	}
329b9a1b977SAmir Goldstein 
330fecc4559SAmir Goldstein 	return fsnotify_handle_inode_event(group, inode_mark, mask, data, data_type,
331950cc0d2SAmir Goldstein 					   dir, name, cookie);
332b9a1b977SAmir Goldstein }
333b9a1b977SAmir 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)334b54cecf5SAmir Goldstein static int send_to_group(__u32 mask, const void *data, int data_type,
335b54cecf5SAmir Goldstein 			 struct inode *dir, const struct qstr *file_name,
336b54cecf5SAmir Goldstein 			 u32 cookie, struct fsnotify_iter_info *iter_info)
3377131485aSEric Paris {
338faa9560aSEric Paris 	struct fsnotify_group *group = NULL;
339007d1e83SAmir Goldstein 	__u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
34092183a42SAmir Goldstein 	__u32 marks_mask = 0;
34131a371e4SAmir Goldstein 	__u32 marks_ignore_mask = 0;
34231a371e4SAmir Goldstein 	bool is_dir = mask & FS_ISDIR;
3433dca1a74SAmir Goldstein 	struct fsnotify_mark *mark;
3443dca1a74SAmir Goldstein 	int type;
345613a807fSEric Paris 
34614362a25SAmir Goldstein 	if (!iter_info->report_mask)
347faa9560aSEric Paris 		return 0;
3485ba08e2eSEric Paris 
349ce8f76fbSEric Paris 	/* clear ignored on inode modification */
350ce8f76fbSEric Paris 	if (mask & FS_MODIFY) {
35114362a25SAmir Goldstein 		fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
35214362a25SAmir Goldstein 			if (!(mark->flags &
35314362a25SAmir Goldstein 			      FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
35431a371e4SAmir Goldstein 				mark->ignore_mask = 0;
3553dca1a74SAmir Goldstein 		}
356ce8f76fbSEric Paris 	}
357613a807fSEric Paris 
35814362a25SAmir Goldstein 	/* Are any of the group marks interested in this event? */
35914362a25SAmir Goldstein 	fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
3603dca1a74SAmir Goldstein 		group = mark->group;
3613dca1a74SAmir Goldstein 		marks_mask |= mark->mask;
36231a371e4SAmir Goldstein 		marks_ignore_mask |=
36331a371e4SAmir Goldstein 			fsnotify_effective_ignore_mask(mark, is_dir, type);
3643dca1a74SAmir Goldstein 	}
365ce8f76fbSEric Paris 
36631a371e4SAmir 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",
36731a371e4SAmir Goldstein 		 __func__, group, mask, marks_mask, marks_ignore_mask,
368b54cecf5SAmir Goldstein 		 data, data_type, dir, cookie);
369faa9560aSEric Paris 
37031a371e4SAmir Goldstein 	if (!(test_mask & marks_mask & ~marks_ignore_mask))
371613a807fSEric Paris 		return 0;
372613a807fSEric Paris 
373b9a1b977SAmir Goldstein 	if (group->ops->handle_event) {
374b54cecf5SAmir Goldstein 		return group->ops->handle_event(group, mask, data, data_type, dir,
3759385a84dSJan Kara 						file_name, cookie, iter_info);
3767131485aSEric Paris 	}
3777131485aSEric Paris 
378b9a1b977SAmir Goldstein 	return fsnotify_handle_event(group, mask, data, data_type, dir,
379b9a1b977SAmir Goldstein 				     file_name, cookie, iter_info);
380b9a1b977SAmir Goldstein }
381b9a1b977SAmir Goldstein 
fsnotify_first_mark(struct fsnotify_mark_connector ** connp)3823427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector **connp)
3833427ce71SMiklos Szeredi {
3843427ce71SMiklos Szeredi 	struct fsnotify_mark_connector *conn;
3853427ce71SMiklos Szeredi 	struct hlist_node *node = NULL;
3863427ce71SMiklos Szeredi 
3873427ce71SMiklos Szeredi 	conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
3883427ce71SMiklos Szeredi 	if (conn)
3893427ce71SMiklos Szeredi 		node = srcu_dereference(conn->list.first, &fsnotify_mark_srcu);
3903427ce71SMiklos Szeredi 
3913427ce71SMiklos Szeredi 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
3923427ce71SMiklos Szeredi }
3933427ce71SMiklos Szeredi 
fsnotify_next_mark(struct fsnotify_mark * mark)3943427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark)
3953427ce71SMiklos Szeredi {
3963427ce71SMiklos Szeredi 	struct hlist_node *node = NULL;
3973427ce71SMiklos Szeredi 
3983427ce71SMiklos Szeredi 	if (mark)
3993427ce71SMiklos Szeredi 		node = srcu_dereference(mark->obj_list.next,
4003427ce71SMiklos Szeredi 					&fsnotify_mark_srcu);
4013427ce71SMiklos Szeredi 
4023427ce71SMiklos Szeredi 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
4033427ce71SMiklos Szeredi }
4043427ce71SMiklos Szeredi 
405c28f7e56SEric Paris /*
406d9a6f30bSAmir Goldstein  * iter_info is a multi head priority queue of marks.
40714362a25SAmir Goldstein  * Pick a subset of marks from queue heads, all with the same group
40814362a25SAmir Goldstein  * and set the report_mask to a subset of the selected marks.
40914362a25SAmir Goldstein  * Returns false if there are no more groups to iterate.
410d9a6f30bSAmir Goldstein  */
fsnotify_iter_select_report_types(struct fsnotify_iter_info * iter_info)41114362a25SAmir Goldstein static bool fsnotify_iter_select_report_types(
412d9a6f30bSAmir Goldstein 		struct fsnotify_iter_info *iter_info)
413d9a6f30bSAmir Goldstein {
41447d9c7ccSAmir Goldstein 	struct fsnotify_group *max_prio_group = NULL;
41547d9c7ccSAmir Goldstein 	struct fsnotify_mark *mark;
41647d9c7ccSAmir Goldstein 	int type;
417d9a6f30bSAmir Goldstein 
41847d9c7ccSAmir Goldstein 	/* Choose max prio group among groups of all queue heads */
4191c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
42047d9c7ccSAmir Goldstein 		mark = iter_info->marks[type];
42147d9c7ccSAmir Goldstein 		if (mark &&
42247d9c7ccSAmir Goldstein 		    fsnotify_compare_groups(max_prio_group, mark->group) > 0)
42347d9c7ccSAmir Goldstein 			max_prio_group = mark->group;
424d9a6f30bSAmir Goldstein 	}
425d9a6f30bSAmir Goldstein 
42647d9c7ccSAmir Goldstein 	if (!max_prio_group)
42714362a25SAmir Goldstein 		return false;
42847d9c7ccSAmir Goldstein 
42947d9c7ccSAmir Goldstein 	/* Set the report mask for marks from same group as max prio group */
43014362a25SAmir Goldstein 	iter_info->current_group = max_prio_group;
431d9a6f30bSAmir Goldstein 	iter_info->report_mask = 0;
4321c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
43347d9c7ccSAmir Goldstein 		mark = iter_info->marks[type];
434e730558aSAmir Goldstein 		if (mark && mark->group == iter_info->current_group) {
435e730558aSAmir Goldstein 			/*
436e730558aSAmir Goldstein 			 * FSNOTIFY_ITER_TYPE_PARENT indicates that this inode
437e730558aSAmir Goldstein 			 * is watching children and interested in this event,
438e730558aSAmir Goldstein 			 * which is an event possible on child.
439e730558aSAmir Goldstein 			 * But is *this mark* watching children?
440e730558aSAmir Goldstein 			 */
441e730558aSAmir Goldstein 			if (type == FSNOTIFY_ITER_TYPE_PARENT &&
44231a371e4SAmir Goldstein 			    !(mark->mask & FS_EVENT_ON_CHILD) &&
44331a371e4SAmir Goldstein 			    !(fsnotify_ignore_mask(mark) & FS_EVENT_ON_CHILD))
444e730558aSAmir Goldstein 				continue;
445e730558aSAmir Goldstein 
44647d9c7ccSAmir Goldstein 			fsnotify_iter_set_report_type(iter_info, type);
44747d9c7ccSAmir Goldstein 		}
448e730558aSAmir Goldstein 	}
449d9a6f30bSAmir Goldstein 
45014362a25SAmir Goldstein 	return true;
451d9a6f30bSAmir Goldstein }
452d9a6f30bSAmir Goldstein 
453d9a6f30bSAmir Goldstein /*
45414362a25SAmir Goldstein  * Pop from iter_info multi head queue, the marks that belong to the group of
455d9a6f30bSAmir Goldstein  * current iteration step.
456d9a6f30bSAmir Goldstein  */
fsnotify_iter_next(struct fsnotify_iter_info * iter_info)457d9a6f30bSAmir Goldstein static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info)
458d9a6f30bSAmir Goldstein {
45914362a25SAmir Goldstein 	struct fsnotify_mark *mark;
46047d9c7ccSAmir Goldstein 	int type;
461d9a6f30bSAmir Goldstein 
46214362a25SAmir Goldstein 	/*
46314362a25SAmir Goldstein 	 * We cannot use fsnotify_foreach_iter_mark_type() here because we
46414362a25SAmir Goldstein 	 * may need to advance a mark of type X that belongs to current_group
46514362a25SAmir Goldstein 	 * but was not selected for reporting.
46614362a25SAmir Goldstein 	 */
4671c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
46814362a25SAmir Goldstein 		mark = iter_info->marks[type];
46914362a25SAmir Goldstein 		if (mark && mark->group == iter_info->current_group)
47047d9c7ccSAmir Goldstein 			iter_info->marks[type] =
47147d9c7ccSAmir Goldstein 				fsnotify_next_mark(iter_info->marks[type]);
47247d9c7ccSAmir Goldstein 	}
473d9a6f30bSAmir Goldstein }
474d9a6f30bSAmir Goldstein 
475d9a6f30bSAmir Goldstein /*
47640a100d3SAmir Goldstein  * fsnotify - This is the main call to fsnotify.
47740a100d3SAmir Goldstein  *
47840a100d3SAmir Goldstein  * The VFS calls into hook specific functions in linux/fsnotify.h.
47940a100d3SAmir Goldstein  * Those functions then in turn call here.  Here will call out to all of the
48040a100d3SAmir Goldstein  * registered fsnotify_group.  Those groups can then use the notification event
48140a100d3SAmir Goldstein  * in whatever means they feel necessary.
48240a100d3SAmir Goldstein  *
48340a100d3SAmir Goldstein  * @mask:	event type and flags
48440a100d3SAmir Goldstein  * @data:	object that event happened on
48540a100d3SAmir Goldstein  * @data_type:	type of object for fanotify_data_XXX() accessors
48640a100d3SAmir Goldstein  * @dir:	optional directory associated with event -
48740a100d3SAmir Goldstein  *		if @file_name is not NULL, this is the directory that
48840a100d3SAmir Goldstein  *		@file_name is relative to
48940a100d3SAmir Goldstein  * @file_name:	optional file name associated with event
49040a100d3SAmir Goldstein  * @inode:	optional inode associated with event -
49129335033SGabriel Krisman Bertazi  *		If @dir and @inode are both non-NULL, event may be
49229335033SGabriel Krisman Bertazi  *		reported to both.
49340a100d3SAmir Goldstein  * @cookie:	inotify rename cookie
49490586523SEric Paris  */
fsnotify(__u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * file_name,struct inode * inode,u32 cookie)49540a100d3SAmir Goldstein int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
49640a100d3SAmir Goldstein 	     const struct qstr *file_name, struct inode *inode, u32 cookie)
49790586523SEric Paris {
498b54cecf5SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
49929335033SGabriel Krisman Bertazi 	struct super_block *sb = fsnotify_data_sb(data, data_type);
5003427ce71SMiklos Szeredi 	struct fsnotify_iter_info iter_info = {};
50160f7ed8cSAmir Goldstein 	struct mount *mnt = NULL;
502e54183faSAmir Goldstein 	struct inode *inode2 = NULL;
503e54183faSAmir Goldstein 	struct dentry *moved;
504e54183faSAmir Goldstein 	int inode2_type;
5059385a84dSJan Kara 	int ret = 0;
50671d73410SMel Gorman 	__u32 test_mask, marks_mask;
50790586523SEric Paris 
50871d73410SMel Gorman 	if (path)
509aa93bdc5SAmir Goldstein 		mnt = real_mount(path->mnt);
510613a807fSEric Paris 
51140a100d3SAmir Goldstein 	if (!inode) {
51240a100d3SAmir Goldstein 		/* Dirent event - report on TYPE_INODE to dir */
51340a100d3SAmir Goldstein 		inode = dir;
514e54183faSAmir Goldstein 		/* For FS_RENAME, inode is old_dir and inode2 is new_dir */
515e54183faSAmir Goldstein 		if (mask & FS_RENAME) {
516e54183faSAmir Goldstein 			moved = fsnotify_data_dentry(data, data_type);
517e54183faSAmir Goldstein 			inode2 = moved->d_parent->d_inode;
518e54183faSAmir Goldstein 			inode2_type = FSNOTIFY_ITER_TYPE_INODE2;
519e54183faSAmir Goldstein 		}
52040a100d3SAmir Goldstein 	} else if (mask & FS_EVENT_ON_CHILD) {
52140a100d3SAmir Goldstein 		/*
522fecc4559SAmir Goldstein 		 * Event on child - report on TYPE_PARENT to dir if it is
523fecc4559SAmir Goldstein 		 * watching children and on TYPE_INODE to child.
52440a100d3SAmir Goldstein 		 */
525e54183faSAmir Goldstein 		inode2 = dir;
526e54183faSAmir Goldstein 		inode2_type = FSNOTIFY_ITER_TYPE_PARENT;
52740a100d3SAmir Goldstein 	}
528497b0c5aSAmir Goldstein 
529613a807fSEric Paris 	/*
5307c49b861SDave Hansen 	 * Optimization: srcu_read_lock() has a memory barrier which can
5317c49b861SDave Hansen 	 * be expensive.  It protects walking the *_fsnotify_marks lists.
5327c49b861SDave Hansen 	 * However, if we do not walk the lists, we do not have to do
5337c49b861SDave Hansen 	 * SRCU because we have no references to any objects and do not
5347c49b861SDave Hansen 	 * need SRCU to keep them "alive".
5357c49b861SDave Hansen 	 */
5369b93f331SAmir Goldstein 	if (!sb->s_fsnotify_marks &&
537497b0c5aSAmir Goldstein 	    (!mnt || !mnt->mnt_fsnotify_marks) &&
5389b93f331SAmir Goldstein 	    (!inode || !inode->i_fsnotify_marks) &&
539e54183faSAmir Goldstein 	    (!inode2 || !inode2->i_fsnotify_marks))
5407c49b861SDave Hansen 		return 0;
54171d73410SMel Gorman 
5429b93f331SAmir Goldstein 	marks_mask = sb->s_fsnotify_mask;
54371d73410SMel Gorman 	if (mnt)
54471d73410SMel Gorman 		marks_mask |= mnt->mnt_fsnotify_mask;
5459b93f331SAmir Goldstein 	if (inode)
5469b93f331SAmir Goldstein 		marks_mask |= inode->i_fsnotify_mask;
547e54183faSAmir Goldstein 	if (inode2)
548e54183faSAmir Goldstein 		marks_mask |= inode2->i_fsnotify_mask;
549497b0c5aSAmir Goldstein 
55071d73410SMel Gorman 
5517c49b861SDave Hansen 	/*
55231a371e4SAmir Goldstein 	 * If this is a modify event we may need to clear some ignore masks.
55331a371e4SAmir Goldstein 	 * In that case, the object with ignore masks will have the FS_MODIFY
55404e317baSAmir Goldstein 	 * event in its mask.
55504e317baSAmir Goldstein 	 * Otherwise, return if none of the marks care about this type of event.
556613a807fSEric Paris 	 */
55771d73410SMel Gorman 	test_mask = (mask & ALL_FSNOTIFY_EVENTS);
55804e317baSAmir Goldstein 	if (!(test_mask & marks_mask))
559613a807fSEric Paris 		return 0;
5603a9fb89fSEric Paris 
5619385a84dSJan Kara 	iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
5627131485aSEric Paris 
5631c9007d6SAmir Goldstein 	iter_info.marks[FSNOTIFY_ITER_TYPE_SB] =
56445a9fb37SAmir Goldstein 		fsnotify_first_mark(&sb->s_fsnotify_marks);
5659bdda4e9SAmir Goldstein 	if (mnt) {
5661c9007d6SAmir Goldstein 		iter_info.marks[FSNOTIFY_ITER_TYPE_VFSMOUNT] =
5673427ce71SMiklos Szeredi 			fsnotify_first_mark(&mnt->mnt_fsnotify_marks);
5687131485aSEric Paris 	}
5699b93f331SAmir Goldstein 	if (inode) {
5701c9007d6SAmir Goldstein 		iter_info.marks[FSNOTIFY_ITER_TYPE_INODE] =
5719b93f331SAmir Goldstein 			fsnotify_first_mark(&inode->i_fsnotify_marks);
5729b93f331SAmir Goldstein 	}
573e54183faSAmir Goldstein 	if (inode2) {
574e54183faSAmir Goldstein 		iter_info.marks[inode2_type] =
575e54183faSAmir Goldstein 			fsnotify_first_mark(&inode2->i_fsnotify_marks);
576497b0c5aSAmir Goldstein 	}
57775c1be48SEric Paris 
5788edc6e16SJan Kara 	/*
57960f7ed8cSAmir Goldstein 	 * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark
58060f7ed8cSAmir Goldstein 	 * ignore masks are properly reflected for mount/sb mark notifications.
5818edc6e16SJan Kara 	 * That's why this traversal is so complicated...
5828edc6e16SJan Kara 	 */
583d9a6f30bSAmir Goldstein 	while (fsnotify_iter_select_report_types(&iter_info)) {
584b54cecf5SAmir Goldstein 		ret = send_to_group(mask, data, data_type, dir, file_name,
585b54cecf5SAmir Goldstein 				    cookie, &iter_info);
58684a5b68eSEric Paris 
587ff8bcbd0SEric Paris 		if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS))
588ff8bcbd0SEric Paris 			goto out;
589ff8bcbd0SEric Paris 
590d9a6f30bSAmir Goldstein 		fsnotify_iter_next(&iter_info);
5917131485aSEric Paris 	}
592ff8bcbd0SEric Paris 	ret = 0;
593ff8bcbd0SEric Paris out:
5949385a84dSJan Kara 	srcu_read_unlock(&fsnotify_mark_srcu, iter_info.srcu_idx);
595c4ec54b4SEric Paris 
59698b5c10dSJean-Christophe Dubois 	return ret;
59790586523SEric Paris }
59890586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify);
59990586523SEric Paris 
fsnotify_init(void)60090586523SEric Paris static __init int fsnotify_init(void)
60190586523SEric Paris {
60275c1be48SEric Paris 	int ret;
60375c1be48SEric Paris 
60438035c04SAmir Goldstein 	BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 23);
60520dee624SEric Paris 
60675c1be48SEric Paris 	ret = init_srcu_struct(&fsnotify_mark_srcu);
60775c1be48SEric Paris 	if (ret)
60875c1be48SEric Paris 		panic("initializing fsnotify_mark_srcu");
60975c1be48SEric Paris 
6109dd813c1SJan Kara 	fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector,
6119dd813c1SJan Kara 						    SLAB_PANIC);
6129dd813c1SJan Kara 
61375c1be48SEric Paris 	return 0;
61490586523SEric Paris }
61575c1be48SEric Paris core_initcall(fsnotify_init);
616