xref: /openbmc/linux/fs/notify/fsnotify.c (revision b9a1b977)
1c82ee6d3SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
290586523SEric Paris /*
390586523SEric Paris  *  Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
490586523SEric Paris  */
590586523SEric Paris 
690586523SEric Paris #include <linux/dcache.h>
790586523SEric Paris #include <linux/fs.h>
85a0e3ad6STejun Heo #include <linux/gfp.h>
990586523SEric Paris #include <linux/init.h>
1090586523SEric Paris #include <linux/module.h>
117131485aSEric Paris #include <linux/mount.h>
1290586523SEric Paris #include <linux/srcu.h>
1390586523SEric Paris 
1490586523SEric Paris #include <linux/fsnotify_backend.h>
1590586523SEric Paris #include "fsnotify.h"
1690586523SEric Paris 
1790586523SEric Paris /*
183be25f49SEric Paris  * Clear all of the marks on an inode when it is being evicted from core
193be25f49SEric Paris  */
203be25f49SEric Paris void __fsnotify_inode_delete(struct inode *inode)
213be25f49SEric Paris {
223be25f49SEric Paris 	fsnotify_clear_marks_by_inode(inode);
233be25f49SEric Paris }
243be25f49SEric Paris EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);
253be25f49SEric Paris 
26ca9c726eSAndreas Gruenbacher void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
27ca9c726eSAndreas Gruenbacher {
28ca9c726eSAndreas Gruenbacher 	fsnotify_clear_marks_by_mount(mnt);
29ca9c726eSAndreas Gruenbacher }
30ca9c726eSAndreas Gruenbacher 
31ebb3b47eSJan Kara /**
32ebb3b47eSJan Kara  * fsnotify_unmount_inodes - an sb is unmounting.  handle any watched inodes.
33ebb3b47eSJan Kara  * @sb: superblock being unmounted.
34ebb3b47eSJan Kara  *
35ebb3b47eSJan Kara  * Called during unmount with no locks held, so needs to be safe against
36ebb3b47eSJan Kara  * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block.
37ebb3b47eSJan Kara  */
381e6cb723SAmir Goldstein static void fsnotify_unmount_inodes(struct super_block *sb)
39ebb3b47eSJan Kara {
40ebb3b47eSJan Kara 	struct inode *inode, *iput_inode = NULL;
41ebb3b47eSJan Kara 
42ebb3b47eSJan Kara 	spin_lock(&sb->s_inode_list_lock);
43ebb3b47eSJan Kara 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
44ebb3b47eSJan Kara 		/*
45ebb3b47eSJan Kara 		 * We cannot __iget() an inode in state I_FREEING,
46ebb3b47eSJan Kara 		 * I_WILL_FREE, or I_NEW which is fine because by that point
47ebb3b47eSJan Kara 		 * the inode cannot have any associated watches.
48ebb3b47eSJan Kara 		 */
49ebb3b47eSJan Kara 		spin_lock(&inode->i_lock);
50ebb3b47eSJan Kara 		if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
51ebb3b47eSJan Kara 			spin_unlock(&inode->i_lock);
52ebb3b47eSJan Kara 			continue;
53ebb3b47eSJan Kara 		}
54ebb3b47eSJan Kara 
55ebb3b47eSJan Kara 		/*
56ebb3b47eSJan Kara 		 * If i_count is zero, the inode cannot have any watches and
571751e8a6SLinus Torvalds 		 * doing an __iget/iput with SB_ACTIVE clear would actually
58ebb3b47eSJan Kara 		 * evict all inodes with zero i_count from icache which is
59ebb3b47eSJan Kara 		 * unnecessarily violent and may in fact be illegal to do.
601edc8eb2SEric Sandeen 		 * However, we should have been called /after/ evict_inodes
611edc8eb2SEric Sandeen 		 * removed all zero refcount inodes, in any case.  Test to
621edc8eb2SEric Sandeen 		 * be sure.
63ebb3b47eSJan Kara 		 */
64ebb3b47eSJan Kara 		if (!atomic_read(&inode->i_count)) {
65ebb3b47eSJan Kara 			spin_unlock(&inode->i_lock);
66ebb3b47eSJan Kara 			continue;
67ebb3b47eSJan Kara 		}
68ebb3b47eSJan Kara 
69ebb3b47eSJan Kara 		__iget(inode);
70ebb3b47eSJan Kara 		spin_unlock(&inode->i_lock);
71ebb3b47eSJan Kara 		spin_unlock(&sb->s_inode_list_lock);
72ebb3b47eSJan Kara 
73ebb3b47eSJan Kara 		if (iput_inode)
74ebb3b47eSJan Kara 			iput(iput_inode);
75ebb3b47eSJan Kara 
76ebb3b47eSJan Kara 		/* for each watch, send FS_UNMOUNT and then remove it */
7782ace1efSAmir Goldstein 		fsnotify_inode(inode, FS_UNMOUNT);
78ebb3b47eSJan Kara 
79ebb3b47eSJan Kara 		fsnotify_inode_delete(inode);
80ebb3b47eSJan Kara 
81ebb3b47eSJan Kara 		iput_inode = inode;
82ebb3b47eSJan Kara 
8304646aebSEric Sandeen 		cond_resched();
84ebb3b47eSJan Kara 		spin_lock(&sb->s_inode_list_lock);
85ebb3b47eSJan Kara 	}
86ebb3b47eSJan Kara 	spin_unlock(&sb->s_inode_list_lock);
87ebb3b47eSJan Kara 
88ebb3b47eSJan Kara 	if (iput_inode)
89ebb3b47eSJan Kara 		iput(iput_inode);
90721fb6fbSJan Kara 	/* Wait for outstanding inode references from connectors */
91721fb6fbSJan Kara 	wait_var_event(&sb->s_fsnotify_inode_refs,
92721fb6fbSJan Kara 		       !atomic_long_read(&sb->s_fsnotify_inode_refs));
93ebb3b47eSJan Kara }
94ebb3b47eSJan Kara 
951e6cb723SAmir Goldstein void fsnotify_sb_delete(struct super_block *sb)
961e6cb723SAmir Goldstein {
971e6cb723SAmir Goldstein 	fsnotify_unmount_inodes(sb);
981e6cb723SAmir Goldstein 	fsnotify_clear_marks_by_sb(sb);
991e6cb723SAmir Goldstein }
1001e6cb723SAmir Goldstein 
1013be25f49SEric Paris /*
102c28f7e56SEric Paris  * Given an inode, first check if we care what happens to our children.  Inotify
103c28f7e56SEric Paris  * and dnotify both tell their parents about events.  If we care about any event
104c28f7e56SEric Paris  * on a child we run all of our children and set a dentry flag saying that the
105c28f7e56SEric Paris  * parent cares.  Thus when an event happens on a child it can quickly tell if
106c28f7e56SEric Paris  * if there is a need to find a parent and send the event to the parent.
107c28f7e56SEric Paris  */
108c28f7e56SEric Paris void __fsnotify_update_child_dentry_flags(struct inode *inode)
109c28f7e56SEric Paris {
110c28f7e56SEric Paris 	struct dentry *alias;
111c28f7e56SEric Paris 	int watched;
112c28f7e56SEric Paris 
113c28f7e56SEric Paris 	if (!S_ISDIR(inode->i_mode))
114c28f7e56SEric Paris 		return;
115c28f7e56SEric Paris 
116c28f7e56SEric Paris 	/* determine if the children should tell inode about their events */
117c28f7e56SEric Paris 	watched = fsnotify_inode_watches_children(inode);
118c28f7e56SEric Paris 
119873feea0SNick Piggin 	spin_lock(&inode->i_lock);
120c28f7e56SEric Paris 	/* run all of the dentries associated with this inode.  Since this is a
121c28f7e56SEric Paris 	 * directory, there damn well better only be one item on this list */
122946e51f2SAl Viro 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
123c28f7e56SEric Paris 		struct dentry *child;
124c28f7e56SEric Paris 
125c28f7e56SEric Paris 		/* run all of the children of the original inode and fix their
126c28f7e56SEric Paris 		 * d_flags to indicate parental interest (their parent is the
127c28f7e56SEric Paris 		 * original inode) */
1282fd6b7f5SNick Piggin 		spin_lock(&alias->d_lock);
129946e51f2SAl Viro 		list_for_each_entry(child, &alias->d_subdirs, d_child) {
130c28f7e56SEric Paris 			if (!child->d_inode)
131c28f7e56SEric Paris 				continue;
132c28f7e56SEric Paris 
1332fd6b7f5SNick Piggin 			spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
134c28f7e56SEric Paris 			if (watched)
135c28f7e56SEric Paris 				child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
136c28f7e56SEric Paris 			else
137c28f7e56SEric Paris 				child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
138c28f7e56SEric Paris 			spin_unlock(&child->d_lock);
139c28f7e56SEric Paris 		}
1402fd6b7f5SNick Piggin 		spin_unlock(&alias->d_lock);
141c28f7e56SEric Paris 	}
142873feea0SNick Piggin 	spin_unlock(&inode->i_lock);
143c28f7e56SEric Paris }
144c28f7e56SEric Paris 
1459b93f331SAmir Goldstein /* Are inode/sb/mount interested in parent and name info with this event? */
1469b93f331SAmir Goldstein static bool fsnotify_event_needs_parent(struct inode *inode, struct mount *mnt,
1479b93f331SAmir Goldstein 					__u32 mask)
1489b93f331SAmir Goldstein {
1499b93f331SAmir Goldstein 	__u32 marks_mask = 0;
1509b93f331SAmir Goldstein 
1519b93f331SAmir Goldstein 	/* We only send parent/name to inode/sb/mount for events on non-dir */
1529b93f331SAmir Goldstein 	if (mask & FS_ISDIR)
1539b93f331SAmir Goldstein 		return false;
1549b93f331SAmir Goldstein 
1559b93f331SAmir Goldstein 	/* Did either inode/sb/mount subscribe for events with parent/name? */
1569b93f331SAmir Goldstein 	marks_mask |= fsnotify_parent_needed_mask(inode->i_fsnotify_mask);
1579b93f331SAmir Goldstein 	marks_mask |= fsnotify_parent_needed_mask(inode->i_sb->s_fsnotify_mask);
1589b93f331SAmir Goldstein 	if (mnt)
1599b93f331SAmir Goldstein 		marks_mask |= fsnotify_parent_needed_mask(mnt->mnt_fsnotify_mask);
1609b93f331SAmir Goldstein 
1619b93f331SAmir Goldstein 	/* Did they subscribe for this event with parent/name info? */
1629b93f331SAmir Goldstein 	return mask & marks_mask;
1639b93f331SAmir Goldstein }
1649b93f331SAmir Goldstein 
165c738fbabSAmir Goldstein /*
166c738fbabSAmir Goldstein  * Notify this dentry's parent about a child's events with child name info
1679b93f331SAmir Goldstein  * if parent is watching or if inode/sb/mount are interested in events with
1689b93f331SAmir Goldstein  * parent and name info.
1699b93f331SAmir Goldstein  *
1709b93f331SAmir Goldstein  * Notify only the child without name info if parent is not watching and
1719b93f331SAmir Goldstein  * inode/sb/mount are not interested in events with parent and name info.
172c738fbabSAmir Goldstein  */
17371d73410SMel Gorman int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
174017de65fSAmir Goldstein 		      int data_type)
175c28f7e56SEric Paris {
1769b93f331SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
1779b93f331SAmir Goldstein 	struct mount *mnt = path ? real_mount(path->mnt) : NULL;
178497b0c5aSAmir Goldstein 	struct inode *inode = d_inode(dentry);
179c28f7e56SEric Paris 	struct dentry *parent;
1809b93f331SAmir Goldstein 	bool parent_watched = dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED;
1819b93f331SAmir Goldstein 	__u32 p_mask;
18240a100d3SAmir Goldstein 	struct inode *p_inode = NULL;
183497b0c5aSAmir Goldstein 	struct name_snapshot name;
184497b0c5aSAmir Goldstein 	struct qstr *file_name = NULL;
18552420392SEric Paris 	int ret = 0;
186c28f7e56SEric Paris 
1879b93f331SAmir Goldstein 	/*
1889b93f331SAmir Goldstein 	 * Do inode/sb/mount care about parent and name info on non-dir?
1899b93f331SAmir Goldstein 	 * Do they care about any event at all?
1909b93f331SAmir Goldstein 	 */
1919b93f331SAmir Goldstein 	if (!inode->i_fsnotify_marks && !inode->i_sb->s_fsnotify_marks &&
1929b93f331SAmir Goldstein 	    (!mnt || !mnt->mnt_fsnotify_marks) && !parent_watched)
1939b93f331SAmir Goldstein 		return 0;
1949b93f331SAmir Goldstein 
195497b0c5aSAmir Goldstein 	parent = NULL;
1969b93f331SAmir Goldstein 	if (!parent_watched && !fsnotify_event_needs_parent(inode, mnt, mask))
197497b0c5aSAmir Goldstein 		goto notify;
198c28f7e56SEric Paris 
1999b93f331SAmir Goldstein 	/* Does parent inode care about events on children? */
2004d4eb366SChristoph Hellwig 	parent = dget_parent(dentry);
201c28f7e56SEric Paris 	p_inode = parent->d_inode;
2029b93f331SAmir Goldstein 	p_mask = fsnotify_inode_watches_children(p_inode);
2039b93f331SAmir Goldstein 	if (unlikely(parent_watched && !p_mask))
2044d4eb366SChristoph Hellwig 		__fsnotify_update_child_dentry_flags(p_inode);
2059b93f331SAmir Goldstein 
2069b93f331SAmir Goldstein 	/*
2079b93f331SAmir Goldstein 	 * Include parent/name in notification either if some notification
2089b93f331SAmir Goldstein 	 * groups require parent info (!parent_watched case) or the parent is
2099b93f331SAmir Goldstein 	 * interested in this event.
2109b93f331SAmir Goldstein 	 */
2119b93f331SAmir Goldstein 	if (!parent_watched || (mask & p_mask & ALL_FSNOTIFY_EVENTS)) {
212497b0c5aSAmir Goldstein 		/* When notifying parent, child should be passed as data */
213497b0c5aSAmir Goldstein 		WARN_ON_ONCE(inode != fsnotify_data_inode(data, data_type));
21449d31c2fSAl Viro 
215497b0c5aSAmir Goldstein 		/* Notify both parent and child with child name info */
21649d31c2fSAl Viro 		take_dentry_name_snapshot(&name, dentry);
217497b0c5aSAmir Goldstein 		file_name = &name.name;
2189b93f331SAmir Goldstein 		if (parent_watched)
219497b0c5aSAmir Goldstein 			mask |= FS_EVENT_ON_CHILD;
220c28f7e56SEric Paris 	}
221c28f7e56SEric Paris 
222497b0c5aSAmir Goldstein notify:
22340a100d3SAmir Goldstein 	ret = fsnotify(mask, data, data_type, p_inode, file_name, inode, 0);
224497b0c5aSAmir Goldstein 
225497b0c5aSAmir Goldstein 	if (file_name)
226497b0c5aSAmir Goldstein 		release_dentry_name_snapshot(&name);
227c28f7e56SEric Paris 	dput(parent);
22852420392SEric Paris 
22952420392SEric Paris 	return ret;
230c28f7e56SEric Paris }
23171d73410SMel Gorman EXPORT_SYMBOL_GPL(__fsnotify_parent);
232c28f7e56SEric Paris 
233b9a1b977SAmir Goldstein static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
234b9a1b977SAmir Goldstein 				 const void *data, int data_type,
235b9a1b977SAmir Goldstein 				 struct inode *dir, const struct qstr *name,
236b9a1b977SAmir Goldstein 				 u32 cookie, struct fsnotify_iter_info *iter_info)
237b9a1b977SAmir Goldstein {
238b9a1b977SAmir Goldstein 	struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
239b9a1b977SAmir Goldstein 	struct fsnotify_mark *child_mark = fsnotify_iter_child_mark(iter_info);
240b9a1b977SAmir Goldstein 	struct inode *inode = fsnotify_data_inode(data, data_type);
241b9a1b977SAmir Goldstein 	const struct fsnotify_ops *ops = group->ops;
242b9a1b977SAmir Goldstein 	int ret;
243b9a1b977SAmir Goldstein 
244b9a1b977SAmir Goldstein 	if (WARN_ON_ONCE(!ops->handle_inode_event))
245b9a1b977SAmir Goldstein 		return 0;
246b9a1b977SAmir Goldstein 
247b9a1b977SAmir Goldstein 	if (WARN_ON_ONCE(fsnotify_iter_sb_mark(iter_info)) ||
248b9a1b977SAmir Goldstein 	    WARN_ON_ONCE(fsnotify_iter_vfsmount_mark(iter_info)))
249b9a1b977SAmir Goldstein 		return 0;
250b9a1b977SAmir Goldstein 
251b9a1b977SAmir Goldstein 	/*
252b9a1b977SAmir Goldstein 	 * An event can be sent on child mark iterator instead of inode mark
253b9a1b977SAmir Goldstein 	 * iterator because of other groups that have interest of this inode
254b9a1b977SAmir Goldstein 	 * and have marks on both parent and child.  We can simplify this case.
255b9a1b977SAmir Goldstein 	 */
256b9a1b977SAmir Goldstein 	if (!inode_mark) {
257b9a1b977SAmir Goldstein 		inode_mark = child_mark;
258b9a1b977SAmir Goldstein 		child_mark = NULL;
259b9a1b977SAmir Goldstein 		dir = NULL;
260b9a1b977SAmir Goldstein 		name = NULL;
261b9a1b977SAmir Goldstein 	}
262b9a1b977SAmir Goldstein 
263b9a1b977SAmir Goldstein 	ret = ops->handle_inode_event(inode_mark, mask, inode, dir, name);
264b9a1b977SAmir Goldstein 	if (ret || !child_mark)
265b9a1b977SAmir Goldstein 		return ret;
266b9a1b977SAmir Goldstein 
267b9a1b977SAmir Goldstein 	/*
268b9a1b977SAmir Goldstein 	 * Some events can be sent on both parent dir and child marks
269b9a1b977SAmir Goldstein 	 * (e.g. FS_ATTRIB).  If both parent dir and child are watching,
270b9a1b977SAmir Goldstein 	 * report the event once to parent dir with name and once to child
271b9a1b977SAmir Goldstein 	 * without name.
272b9a1b977SAmir Goldstein 	 */
273b9a1b977SAmir Goldstein 	return ops->handle_inode_event(child_mark, mask, inode, NULL, NULL);
274b9a1b977SAmir Goldstein }
275b9a1b977SAmir Goldstein 
276b54cecf5SAmir Goldstein static int send_to_group(__u32 mask, const void *data, int data_type,
277b54cecf5SAmir Goldstein 			 struct inode *dir, const struct qstr *file_name,
278b54cecf5SAmir Goldstein 			 u32 cookie, struct fsnotify_iter_info *iter_info)
2797131485aSEric Paris {
280faa9560aSEric Paris 	struct fsnotify_group *group = NULL;
281007d1e83SAmir Goldstein 	__u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
28292183a42SAmir Goldstein 	__u32 marks_mask = 0;
28392183a42SAmir Goldstein 	__u32 marks_ignored_mask = 0;
2843dca1a74SAmir Goldstein 	struct fsnotify_mark *mark;
2853dca1a74SAmir Goldstein 	int type;
286613a807fSEric Paris 
2875b0457adSAmir Goldstein 	if (WARN_ON(!iter_info->report_mask))
288faa9560aSEric Paris 		return 0;
2895ba08e2eSEric Paris 
290ce8f76fbSEric Paris 	/* clear ignored on inode modification */
291ce8f76fbSEric Paris 	if (mask & FS_MODIFY) {
2923dca1a74SAmir Goldstein 		fsnotify_foreach_obj_type(type) {
2933dca1a74SAmir Goldstein 			if (!fsnotify_iter_should_report_type(iter_info, type))
2943dca1a74SAmir Goldstein 				continue;
2953dca1a74SAmir Goldstein 			mark = iter_info->marks[type];
2963dca1a74SAmir Goldstein 			if (mark &&
2973dca1a74SAmir Goldstein 			    !(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
2983dca1a74SAmir Goldstein 				mark->ignored_mask = 0;
2993dca1a74SAmir Goldstein 		}
300ce8f76fbSEric Paris 	}
301613a807fSEric Paris 
3023dca1a74SAmir Goldstein 	fsnotify_foreach_obj_type(type) {
3033dca1a74SAmir Goldstein 		if (!fsnotify_iter_should_report_type(iter_info, type))
3043dca1a74SAmir Goldstein 			continue;
3053dca1a74SAmir Goldstein 		mark = iter_info->marks[type];
3063dca1a74SAmir Goldstein 		/* does the object mark tell us to do something? */
3073dca1a74SAmir Goldstein 		if (mark) {
3083dca1a74SAmir Goldstein 			group = mark->group;
3093dca1a74SAmir Goldstein 			marks_mask |= mark->mask;
3103dca1a74SAmir Goldstein 			marks_ignored_mask |= mark->ignored_mask;
3113dca1a74SAmir Goldstein 		}
312ce8f76fbSEric Paris 	}
313ce8f76fbSEric Paris 
314b54cecf5SAmir 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",
315b54cecf5SAmir Goldstein 		 __func__, group, mask, marks_mask, marks_ignored_mask,
316b54cecf5SAmir Goldstein 		 data, data_type, dir, cookie);
317faa9560aSEric Paris 
31892183a42SAmir Goldstein 	if (!(test_mask & marks_mask & ~marks_ignored_mask))
319613a807fSEric Paris 		return 0;
320613a807fSEric Paris 
321b9a1b977SAmir Goldstein 	if (group->ops->handle_event) {
322b54cecf5SAmir Goldstein 		return group->ops->handle_event(group, mask, data, data_type, dir,
3239385a84dSJan Kara 						file_name, cookie, iter_info);
3247131485aSEric Paris 	}
3257131485aSEric Paris 
326b9a1b977SAmir Goldstein 	return fsnotify_handle_event(group, mask, data, data_type, dir,
327b9a1b977SAmir Goldstein 				     file_name, cookie, iter_info);
328b9a1b977SAmir Goldstein }
329b9a1b977SAmir Goldstein 
3303427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector **connp)
3313427ce71SMiklos Szeredi {
3323427ce71SMiklos Szeredi 	struct fsnotify_mark_connector *conn;
3333427ce71SMiklos Szeredi 	struct hlist_node *node = NULL;
3343427ce71SMiklos Szeredi 
3353427ce71SMiklos Szeredi 	conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
3363427ce71SMiklos Szeredi 	if (conn)
3373427ce71SMiklos Szeredi 		node = srcu_dereference(conn->list.first, &fsnotify_mark_srcu);
3383427ce71SMiklos Szeredi 
3393427ce71SMiklos Szeredi 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
3403427ce71SMiklos Szeredi }
3413427ce71SMiklos Szeredi 
3423427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark)
3433427ce71SMiklos Szeredi {
3443427ce71SMiklos Szeredi 	struct hlist_node *node = NULL;
3453427ce71SMiklos Szeredi 
3463427ce71SMiklos Szeredi 	if (mark)
3473427ce71SMiklos Szeredi 		node = srcu_dereference(mark->obj_list.next,
3483427ce71SMiklos Szeredi 					&fsnotify_mark_srcu);
3493427ce71SMiklos Szeredi 
3503427ce71SMiklos Szeredi 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
3513427ce71SMiklos Szeredi }
3523427ce71SMiklos Szeredi 
353c28f7e56SEric Paris /*
354d9a6f30bSAmir Goldstein  * iter_info is a multi head priority queue of marks.
355d9a6f30bSAmir Goldstein  * Pick a subset of marks from queue heads, all with the
356d9a6f30bSAmir Goldstein  * same group and set the report_mask for selected subset.
357d9a6f30bSAmir Goldstein  * Returns the report_mask of the selected subset.
358d9a6f30bSAmir Goldstein  */
359d9a6f30bSAmir Goldstein static unsigned int fsnotify_iter_select_report_types(
360d9a6f30bSAmir Goldstein 		struct fsnotify_iter_info *iter_info)
361d9a6f30bSAmir Goldstein {
36247d9c7ccSAmir Goldstein 	struct fsnotify_group *max_prio_group = NULL;
36347d9c7ccSAmir Goldstein 	struct fsnotify_mark *mark;
36447d9c7ccSAmir Goldstein 	int type;
365d9a6f30bSAmir Goldstein 
36647d9c7ccSAmir Goldstein 	/* Choose max prio group among groups of all queue heads */
36747d9c7ccSAmir Goldstein 	fsnotify_foreach_obj_type(type) {
36847d9c7ccSAmir Goldstein 		mark = iter_info->marks[type];
36947d9c7ccSAmir Goldstein 		if (mark &&
37047d9c7ccSAmir Goldstein 		    fsnotify_compare_groups(max_prio_group, mark->group) > 0)
37147d9c7ccSAmir Goldstein 			max_prio_group = mark->group;
372d9a6f30bSAmir Goldstein 	}
373d9a6f30bSAmir Goldstein 
37447d9c7ccSAmir Goldstein 	if (!max_prio_group)
37547d9c7ccSAmir Goldstein 		return 0;
37647d9c7ccSAmir Goldstein 
37747d9c7ccSAmir Goldstein 	/* Set the report mask for marks from same group as max prio group */
378d9a6f30bSAmir Goldstein 	iter_info->report_mask = 0;
37947d9c7ccSAmir Goldstein 	fsnotify_foreach_obj_type(type) {
38047d9c7ccSAmir Goldstein 		mark = iter_info->marks[type];
38147d9c7ccSAmir Goldstein 		if (mark &&
38247d9c7ccSAmir Goldstein 		    fsnotify_compare_groups(max_prio_group, mark->group) == 0)
38347d9c7ccSAmir Goldstein 			fsnotify_iter_set_report_type(iter_info, type);
38447d9c7ccSAmir Goldstein 	}
385d9a6f30bSAmir Goldstein 
386d9a6f30bSAmir Goldstein 	return iter_info->report_mask;
387d9a6f30bSAmir Goldstein }
388d9a6f30bSAmir Goldstein 
389d9a6f30bSAmir Goldstein /*
390d9a6f30bSAmir Goldstein  * Pop from iter_info multi head queue, the marks that were iterated in the
391d9a6f30bSAmir Goldstein  * current iteration step.
392d9a6f30bSAmir Goldstein  */
393d9a6f30bSAmir Goldstein static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info)
394d9a6f30bSAmir Goldstein {
39547d9c7ccSAmir Goldstein 	int type;
396d9a6f30bSAmir Goldstein 
39747d9c7ccSAmir Goldstein 	fsnotify_foreach_obj_type(type) {
39847d9c7ccSAmir Goldstein 		if (fsnotify_iter_should_report_type(iter_info, type))
39947d9c7ccSAmir Goldstein 			iter_info->marks[type] =
40047d9c7ccSAmir Goldstein 				fsnotify_next_mark(iter_info->marks[type]);
40147d9c7ccSAmir Goldstein 	}
402d9a6f30bSAmir Goldstein }
403d9a6f30bSAmir Goldstein 
404d9a6f30bSAmir Goldstein /*
40540a100d3SAmir Goldstein  * fsnotify - This is the main call to fsnotify.
40640a100d3SAmir Goldstein  *
40740a100d3SAmir Goldstein  * The VFS calls into hook specific functions in linux/fsnotify.h.
40840a100d3SAmir Goldstein  * Those functions then in turn call here.  Here will call out to all of the
40940a100d3SAmir Goldstein  * registered fsnotify_group.  Those groups can then use the notification event
41040a100d3SAmir Goldstein  * in whatever means they feel necessary.
41140a100d3SAmir Goldstein  *
41240a100d3SAmir Goldstein  * @mask:	event type and flags
41340a100d3SAmir Goldstein  * @data:	object that event happened on
41440a100d3SAmir Goldstein  * @data_type:	type of object for fanotify_data_XXX() accessors
41540a100d3SAmir Goldstein  * @dir:	optional directory associated with event -
41640a100d3SAmir Goldstein  *		if @file_name is not NULL, this is the directory that
41740a100d3SAmir Goldstein  *		@file_name is relative to
41840a100d3SAmir Goldstein  * @file_name:	optional file name associated with event
41940a100d3SAmir Goldstein  * @inode:	optional inode associated with event -
42040a100d3SAmir Goldstein  *		either @dir or @inode must be non-NULL.
42140a100d3SAmir Goldstein  *		if both are non-NULL event may be reported to both.
42240a100d3SAmir Goldstein  * @cookie:	inotify rename cookie
42390586523SEric Paris  */
42440a100d3SAmir Goldstein int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
42540a100d3SAmir Goldstein 	     const struct qstr *file_name, struct inode *inode, u32 cookie)
42690586523SEric Paris {
427b54cecf5SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
4283427ce71SMiklos Szeredi 	struct fsnotify_iter_info iter_info = {};
42940a100d3SAmir Goldstein 	struct super_block *sb;
43060f7ed8cSAmir Goldstein 	struct mount *mnt = NULL;
431497b0c5aSAmir Goldstein 	struct inode *child = NULL;
4329385a84dSJan Kara 	int ret = 0;
43371d73410SMel Gorman 	__u32 test_mask, marks_mask;
43490586523SEric Paris 
43571d73410SMel Gorman 	if (path)
436aa93bdc5SAmir Goldstein 		mnt = real_mount(path->mnt);
437613a807fSEric Paris 
43840a100d3SAmir Goldstein 	if (!inode) {
43940a100d3SAmir Goldstein 		/* Dirent event - report on TYPE_INODE to dir */
44040a100d3SAmir Goldstein 		inode = dir;
44140a100d3SAmir Goldstein 	} else if (mask & FS_EVENT_ON_CHILD) {
44240a100d3SAmir Goldstein 		/*
4439b93f331SAmir Goldstein 		 * Event on child - report on TYPE_INODE to dir if it is
4449b93f331SAmir Goldstein 		 * watching children and on TYPE_CHILD to child.
44540a100d3SAmir Goldstein 		 */
44640a100d3SAmir Goldstein 		child = inode;
44740a100d3SAmir Goldstein 		inode = dir;
44840a100d3SAmir Goldstein 	}
44940a100d3SAmir Goldstein 	sb = inode->i_sb;
450497b0c5aSAmir Goldstein 
451613a807fSEric Paris 	/*
4527c49b861SDave Hansen 	 * Optimization: srcu_read_lock() has a memory barrier which can
4537c49b861SDave Hansen 	 * be expensive.  It protects walking the *_fsnotify_marks lists.
4547c49b861SDave Hansen 	 * However, if we do not walk the lists, we do not have to do
4557c49b861SDave Hansen 	 * SRCU because we have no references to any objects and do not
4567c49b861SDave Hansen 	 * need SRCU to keep them "alive".
4577c49b861SDave Hansen 	 */
4589b93f331SAmir Goldstein 	if (!sb->s_fsnotify_marks &&
459497b0c5aSAmir Goldstein 	    (!mnt || !mnt->mnt_fsnotify_marks) &&
4609b93f331SAmir Goldstein 	    (!inode || !inode->i_fsnotify_marks) &&
461497b0c5aSAmir Goldstein 	    (!child || !child->i_fsnotify_marks))
4627c49b861SDave Hansen 		return 0;
46371d73410SMel Gorman 
4649b93f331SAmir Goldstein 	marks_mask = sb->s_fsnotify_mask;
46571d73410SMel Gorman 	if (mnt)
46671d73410SMel Gorman 		marks_mask |= mnt->mnt_fsnotify_mask;
4679b93f331SAmir Goldstein 	if (inode)
4689b93f331SAmir Goldstein 		marks_mask |= inode->i_fsnotify_mask;
469497b0c5aSAmir Goldstein 	if (child)
470497b0c5aSAmir Goldstein 		marks_mask |= child->i_fsnotify_mask;
471497b0c5aSAmir Goldstein 
47271d73410SMel Gorman 
4737c49b861SDave Hansen 	/*
474613a807fSEric Paris 	 * if this is a modify event we may need to clear the ignored masks
475497b0c5aSAmir Goldstein 	 * otherwise return if none of the marks care about this type of event.
476613a807fSEric Paris 	 */
47771d73410SMel Gorman 	test_mask = (mask & ALL_FSNOTIFY_EVENTS);
47871d73410SMel Gorman 	if (!(mask & FS_MODIFY) && !(test_mask & marks_mask))
479613a807fSEric Paris 		return 0;
4803a9fb89fSEric Paris 
4819385a84dSJan Kara 	iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
4827131485aSEric Paris 
48345a9fb37SAmir Goldstein 	iter_info.marks[FSNOTIFY_OBJ_TYPE_SB] =
48445a9fb37SAmir Goldstein 		fsnotify_first_mark(&sb->s_fsnotify_marks);
4859bdda4e9SAmir Goldstein 	if (mnt) {
48647d9c7ccSAmir Goldstein 		iter_info.marks[FSNOTIFY_OBJ_TYPE_VFSMOUNT] =
4873427ce71SMiklos Szeredi 			fsnotify_first_mark(&mnt->mnt_fsnotify_marks);
4887131485aSEric Paris 	}
4899b93f331SAmir Goldstein 	if (inode) {
4909b93f331SAmir Goldstein 		iter_info.marks[FSNOTIFY_OBJ_TYPE_INODE] =
4919b93f331SAmir Goldstein 			fsnotify_first_mark(&inode->i_fsnotify_marks);
4929b93f331SAmir Goldstein 	}
493497b0c5aSAmir Goldstein 	if (child) {
494497b0c5aSAmir Goldstein 		iter_info.marks[FSNOTIFY_OBJ_TYPE_CHILD] =
495497b0c5aSAmir Goldstein 			fsnotify_first_mark(&child->i_fsnotify_marks);
496497b0c5aSAmir Goldstein 	}
49775c1be48SEric Paris 
4988edc6e16SJan Kara 	/*
49960f7ed8cSAmir Goldstein 	 * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark
50060f7ed8cSAmir Goldstein 	 * ignore masks are properly reflected for mount/sb mark notifications.
5018edc6e16SJan Kara 	 * That's why this traversal is so complicated...
5028edc6e16SJan Kara 	 */
503d9a6f30bSAmir Goldstein 	while (fsnotify_iter_select_report_types(&iter_info)) {
504b54cecf5SAmir Goldstein 		ret = send_to_group(mask, data, data_type, dir, file_name,
505b54cecf5SAmir Goldstein 				    cookie, &iter_info);
50684a5b68eSEric Paris 
507ff8bcbd0SEric Paris 		if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS))
508ff8bcbd0SEric Paris 			goto out;
509ff8bcbd0SEric Paris 
510d9a6f30bSAmir Goldstein 		fsnotify_iter_next(&iter_info);
5117131485aSEric Paris 	}
512ff8bcbd0SEric Paris 	ret = 0;
513ff8bcbd0SEric Paris out:
5149385a84dSJan Kara 	srcu_read_unlock(&fsnotify_mark_srcu, iter_info.srcu_idx);
515c4ec54b4SEric Paris 
51698b5c10dSJean-Christophe Dubois 	return ret;
51790586523SEric Paris }
51890586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify);
51990586523SEric Paris 
52090586523SEric Paris static __init int fsnotify_init(void)
52190586523SEric Paris {
52275c1be48SEric Paris 	int ret;
52375c1be48SEric Paris 
52408b95c33SAmir Goldstein 	BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 25);
52520dee624SEric Paris 
52675c1be48SEric Paris 	ret = init_srcu_struct(&fsnotify_mark_srcu);
52775c1be48SEric Paris 	if (ret)
52875c1be48SEric Paris 		panic("initializing fsnotify_mark_srcu");
52975c1be48SEric Paris 
5309dd813c1SJan Kara 	fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector,
5319dd813c1SJan Kara 						    SLAB_PANIC);
5329dd813c1SJan Kara 
53375c1be48SEric Paris 	return 0;
53490586523SEric Paris }
53575c1be48SEric Paris core_initcall(fsnotify_init);
536