xref: /openbmc/linux/fs/notify/mark.c (revision ac8f933664c3a0e2d42f6ee9a2a6d25f87cb23f6)
1c82ee6d3SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25444e298SEric Paris /*
35444e298SEric Paris  *  Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
45444e298SEric Paris  */
55444e298SEric Paris 
65444e298SEric Paris /*
75444e298SEric Paris  * fsnotify inode mark locking/lifetime/and refcnting
85444e298SEric Paris  *
95444e298SEric Paris  * REFCNT:
109756b918SLino Sanfilippo  * The group->recnt and mark->refcnt tell how many "things" in the kernel
119756b918SLino Sanfilippo  * currently are referencing the objects. Both kind of objects typically will
129756b918SLino Sanfilippo  * live inside the kernel with a refcnt of 2, one for its creation and one for
139756b918SLino Sanfilippo  * the reference a group and a mark hold to each other.
149756b918SLino Sanfilippo  * If you are holding the appropriate locks, you can take a reference and the
159756b918SLino Sanfilippo  * object itself is guaranteed to survive until the reference is dropped.
165444e298SEric Paris  *
175444e298SEric Paris  * LOCKING:
189756b918SLino Sanfilippo  * There are 3 locks involved with fsnotify inode marks and they MUST be taken
199756b918SLino Sanfilippo  * in order as follows:
205444e298SEric Paris  *
219756b918SLino Sanfilippo  * group->mark_mutex
225444e298SEric Paris  * mark->lock
2304662cabSJan Kara  * mark->connector->lock
245444e298SEric Paris  *
259756b918SLino Sanfilippo  * group->mark_mutex protects the marks_list anchored inside a given group and
269756b918SLino Sanfilippo  * each mark is hooked via the g_list.  It also protects the groups private
279756b918SLino Sanfilippo  * data (i.e group limits).
289756b918SLino Sanfilippo 
299756b918SLino Sanfilippo  * mark->lock protects the marks attributes like its masks and flags.
309756b918SLino Sanfilippo  * Furthermore it protects the access to a reference of the group that the mark
319756b918SLino Sanfilippo  * is assigned to as well as the access to a reference of the inode/vfsmount
329756b918SLino Sanfilippo  * that is being watched by the mark.
335444e298SEric Paris  *
3404662cabSJan Kara  * mark->connector->lock protects the list of marks anchored inside an
3504662cabSJan Kara  * inode / vfsmount and each mark is hooked via the i_list.
365444e298SEric Paris  *
3704662cabSJan Kara  * A list of notification marks relating to inode / mnt is contained in
3804662cabSJan Kara  * fsnotify_mark_connector. That structure is alive as long as there are any
396b3f05d2SJan Kara  * marks in the list and is also protected by fsnotify_mark_srcu. A mark gets
406b3f05d2SJan Kara  * detached from fsnotify_mark_connector when last reference to the mark is
416b3f05d2SJan Kara  * dropped.  Thus having mark reference is enough to protect mark->connector
426b3f05d2SJan Kara  * pointer and to make sure fsnotify_mark_connector cannot disappear. Also
436b3f05d2SJan Kara  * because we remove mark from g_list before dropping mark reference associated
446b3f05d2SJan Kara  * with that, any mark found through g_list is guaranteed to have
456b3f05d2SJan Kara  * mark->connector set until we drop group->mark_mutex.
465444e298SEric Paris  *
475444e298SEric Paris  * LIFETIME:
485444e298SEric Paris  * Inode marks survive between when they are added to an inode and when their
49c1f33073SJan Kara  * refcnt==0. Marks are also protected by fsnotify_mark_srcu.
505444e298SEric Paris  *
515444e298SEric Paris  * The inode mark can be cleared for a number of different reasons including:
525444e298SEric Paris  * - The inode is unlinked for the last time.  (fsnotify_inode_remove)
535444e298SEric Paris  * - The inode is being evicted from cache. (fsnotify_inode_delete)
545444e298SEric Paris  * - The fs the inode is on is unmounted.  (fsnotify_inode_delete/fsnotify_unmount_inodes)
555444e298SEric Paris  * - Something explicitly requests that it be removed.  (fsnotify_destroy_mark)
565444e298SEric Paris  * - The fsnotify_group associated with the mark is going away and all such marks
572e37c6caSJan Kara  *   need to be cleaned up. (fsnotify_clear_marks_by_group)
585444e298SEric Paris  *
595444e298SEric Paris  * This has the very interesting property of being able to run concurrently with
605444e298SEric Paris  * any (or all) other directions.
615444e298SEric Paris  */
625444e298SEric Paris 
635444e298SEric Paris #include <linux/fs.h>
645444e298SEric Paris #include <linux/init.h>
655444e298SEric Paris #include <linux/kernel.h>
6675c1be48SEric Paris #include <linux/kthread.h>
675444e298SEric Paris #include <linux/module.h>
685444e298SEric Paris #include <linux/mutex.h>
695444e298SEric Paris #include <linux/slab.h>
705444e298SEric Paris #include <linux/spinlock.h>
7175c1be48SEric Paris #include <linux/srcu.h>
7277115225SAmir Goldstein #include <linux/ratelimit.h>
735444e298SEric Paris 
7460063497SArun Sharma #include <linux/atomic.h>
755444e298SEric Paris 
765444e298SEric Paris #include <linux/fsnotify_backend.h>
775444e298SEric Paris #include "fsnotify.h"
785444e298SEric Paris 
790918f1c3SJeff Layton #define FSNOTIFY_REAPER_DELAY	(1)	/* 1 jiffy */
800918f1c3SJeff Layton 
8175c1be48SEric Paris struct srcu_struct fsnotify_mark_srcu;
829dd813c1SJan Kara struct kmem_cache *fsnotify_mark_connector_cachep;
839dd813c1SJan Kara 
8413d34ac6SJeff Layton static DEFINE_SPINLOCK(destroy_lock);
8513d34ac6SJeff Layton static LIST_HEAD(destroy_list);
8608991e83SJan Kara static struct fsnotify_mark_connector *connector_destroy_list;
870918f1c3SJeff Layton 
8835e48176SJan Kara static void fsnotify_mark_destroy_workfn(struct work_struct *work);
8935e48176SJan Kara static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
9075c1be48SEric Paris 
9108991e83SJan Kara static void fsnotify_connector_destroy_workfn(struct work_struct *work);
9208991e83SJan Kara static DECLARE_WORK(connector_reaper_work, fsnotify_connector_destroy_workfn);
9308991e83SJan Kara 
fsnotify_get_mark(struct fsnotify_mark * mark)945444e298SEric Paris void fsnotify_get_mark(struct fsnotify_mark *mark)
955444e298SEric Paris {
96ab97f873SElena Reshetova 	WARN_ON_ONCE(!refcount_read(&mark->refcnt));
97ab97f873SElena Reshetova 	refcount_inc(&mark->refcnt);
985444e298SEric Paris }
995444e298SEric Paris 
fsnotify_conn_mask_p(struct fsnotify_mark_connector * conn)1003ac70bfcSAmir Goldstein static __u32 *fsnotify_conn_mask_p(struct fsnotify_mark_connector *conn)
1013ac70bfcSAmir Goldstein {
1023ac70bfcSAmir Goldstein 	if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
1033ac70bfcSAmir Goldstein 		return &fsnotify_conn_inode(conn)->i_fsnotify_mask;
1043ac70bfcSAmir Goldstein 	else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT)
1053ac70bfcSAmir Goldstein 		return &fsnotify_conn_mount(conn)->mnt_fsnotify_mask;
1061e6cb723SAmir Goldstein 	else if (conn->type == FSNOTIFY_OBJ_TYPE_SB)
1071e6cb723SAmir Goldstein 		return &fsnotify_conn_sb(conn)->s_fsnotify_mask;
1083ac70bfcSAmir Goldstein 	return NULL;
1093ac70bfcSAmir Goldstein }
1103ac70bfcSAmir Goldstein 
fsnotify_conn_mask(struct fsnotify_mark_connector * conn)1113ac70bfcSAmir Goldstein __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn)
1123ac70bfcSAmir Goldstein {
1133ac70bfcSAmir Goldstein 	if (WARN_ON(!fsnotify_valid_obj_type(conn->type)))
1143ac70bfcSAmir Goldstein 		return 0;
1153ac70bfcSAmir Goldstein 
1163ac70bfcSAmir Goldstein 	return *fsnotify_conn_mask_p(conn);
1173ac70bfcSAmir Goldstein }
1183ac70bfcSAmir Goldstein 
fsnotify_get_inode_ref(struct inode * inode)119c3638b5bSAmir Goldstein static void fsnotify_get_inode_ref(struct inode *inode)
120c3638b5bSAmir Goldstein {
121c3638b5bSAmir Goldstein 	ihold(inode);
122c3638b5bSAmir Goldstein 	atomic_long_inc(&inode->i_sb->s_fsnotify_connectors);
123c3638b5bSAmir Goldstein }
124c3638b5bSAmir Goldstein 
125c3638b5bSAmir Goldstein /*
126c3638b5bSAmir Goldstein  * Grab or drop inode reference for the connector if needed.
127c3638b5bSAmir Goldstein  *
128c3638b5bSAmir Goldstein  * When it's time to drop the reference, we only clear the HAS_IREF flag and
129c3638b5bSAmir Goldstein  * return the inode object. fsnotify_drop_object() will be resonsible for doing
130c3638b5bSAmir Goldstein  * iput() outside of spinlocks. This happens when last mark that wanted iref is
131c3638b5bSAmir Goldstein  * detached.
132c3638b5bSAmir Goldstein  */
fsnotify_update_iref(struct fsnotify_mark_connector * conn,bool want_iref)133c3638b5bSAmir Goldstein static struct inode *fsnotify_update_iref(struct fsnotify_mark_connector *conn,
134c3638b5bSAmir Goldstein 					  bool want_iref)
135c3638b5bSAmir Goldstein {
136c3638b5bSAmir Goldstein 	bool has_iref = conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF;
137c3638b5bSAmir Goldstein 	struct inode *inode = NULL;
138c3638b5bSAmir Goldstein 
139c3638b5bSAmir Goldstein 	if (conn->type != FSNOTIFY_OBJ_TYPE_INODE ||
140c3638b5bSAmir Goldstein 	    want_iref == has_iref)
141c3638b5bSAmir Goldstein 		return NULL;
142c3638b5bSAmir Goldstein 
143c3638b5bSAmir Goldstein 	if (want_iref) {
144c3638b5bSAmir Goldstein 		/* Pin inode if any mark wants inode refcount held */
145c3638b5bSAmir Goldstein 		fsnotify_get_inode_ref(fsnotify_conn_inode(conn));
146c3638b5bSAmir Goldstein 		conn->flags |= FSNOTIFY_CONN_FLAG_HAS_IREF;
147c3638b5bSAmir Goldstein 	} else {
148c3638b5bSAmir Goldstein 		/* Unpin inode after detach of last mark that wanted iref */
149c3638b5bSAmir Goldstein 		inode = fsnotify_conn_inode(conn);
150c3638b5bSAmir Goldstein 		conn->flags &= ~FSNOTIFY_CONN_FLAG_HAS_IREF;
151c3638b5bSAmir Goldstein 	}
152c3638b5bSAmir Goldstein 
153c3638b5bSAmir Goldstein 	return inode;
154c3638b5bSAmir Goldstein }
155c3638b5bSAmir Goldstein 
__fsnotify_recalc_mask(struct fsnotify_mark_connector * conn)156c3638b5bSAmir Goldstein static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
1570809ab69SJan Kara {
1580809ab69SJan Kara 	u32 new_mask = 0;
159c3638b5bSAmir Goldstein 	bool want_iref = false;
1600809ab69SJan Kara 	struct fsnotify_mark *mark;
1610809ab69SJan Kara 
16204662cabSJan Kara 	assert_spin_locked(&conn->lock);
163d3bc0fa8SJan Kara 	/* We can get detached connector here when inode is getting unlinked. */
164d3bc0fa8SJan Kara 	if (!fsnotify_valid_obj_type(conn->type))
165c3638b5bSAmir Goldstein 		return NULL;
1666b3f05d2SJan Kara 	hlist_for_each_entry(mark, &conn->list, obj_list) {
167c3638b5bSAmir Goldstein 		if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED))
168c3638b5bSAmir Goldstein 			continue;
1694f0b903dSAmir Goldstein 		new_mask |= fsnotify_calc_mask(mark);
170c3638b5bSAmir Goldstein 		if (conn->type == FSNOTIFY_OBJ_TYPE_INODE &&
171c3638b5bSAmir Goldstein 		    !(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
172c3638b5bSAmir Goldstein 			want_iref = true;
1736b3f05d2SJan Kara 	}
1743ac70bfcSAmir Goldstein 	*fsnotify_conn_mask_p(conn) = new_mask;
175c3638b5bSAmir Goldstein 
176c3638b5bSAmir Goldstein 	return fsnotify_update_iref(conn, want_iref);
177a242677bSJan Kara }
178a242677bSJan Kara 
fsnotify_conn_watches_children(struct fsnotify_mark_connector * conn)179*fc1b1e13SAmir Goldstein static bool fsnotify_conn_watches_children(
180*fc1b1e13SAmir Goldstein 					struct fsnotify_mark_connector *conn)
181*fc1b1e13SAmir Goldstein {
182*fc1b1e13SAmir Goldstein 	if (conn->type != FSNOTIFY_OBJ_TYPE_INODE)
183*fc1b1e13SAmir Goldstein 		return false;
184*fc1b1e13SAmir Goldstein 
185*fc1b1e13SAmir Goldstein 	return fsnotify_inode_watches_children(fsnotify_conn_inode(conn));
186*fc1b1e13SAmir Goldstein }
187*fc1b1e13SAmir Goldstein 
fsnotify_conn_set_children_dentry_flags(struct fsnotify_mark_connector * conn)188*fc1b1e13SAmir Goldstein static void fsnotify_conn_set_children_dentry_flags(
189*fc1b1e13SAmir Goldstein 					struct fsnotify_mark_connector *conn)
190*fc1b1e13SAmir Goldstein {
191*fc1b1e13SAmir Goldstein 	if (conn->type != FSNOTIFY_OBJ_TYPE_INODE)
192*fc1b1e13SAmir Goldstein 		return;
193*fc1b1e13SAmir Goldstein 
194*fc1b1e13SAmir Goldstein 	fsnotify_set_children_dentry_flags(fsnotify_conn_inode(conn));
195*fc1b1e13SAmir Goldstein }
196*fc1b1e13SAmir Goldstein 
197a242677bSJan Kara /*
198a242677bSJan Kara  * Calculate mask of events for a list of marks. The caller must make sure
19936f10f55SAmir Goldstein  * connector and connector->obj cannot disappear under us.  Callers achieve
2006b3f05d2SJan Kara  * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
2016b3f05d2SJan Kara  * list.
202a242677bSJan Kara  */
fsnotify_recalc_mask(struct fsnotify_mark_connector * conn)203a242677bSJan Kara void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
204a242677bSJan Kara {
205*fc1b1e13SAmir Goldstein 	bool update_children;
206*fc1b1e13SAmir Goldstein 
207a242677bSJan Kara 	if (!conn)
208a242677bSJan Kara 		return;
209a242677bSJan Kara 
21004662cabSJan Kara 	spin_lock(&conn->lock);
211*fc1b1e13SAmir Goldstein 	update_children = !fsnotify_conn_watches_children(conn);
212a242677bSJan Kara 	__fsnotify_recalc_mask(conn);
213*fc1b1e13SAmir Goldstein 	update_children &= fsnotify_conn_watches_children(conn);
21404662cabSJan Kara 	spin_unlock(&conn->lock);
215*fc1b1e13SAmir Goldstein 	/*
216*fc1b1e13SAmir Goldstein 	 * Set children's PARENT_WATCHED flags only if parent started watching.
217*fc1b1e13SAmir Goldstein 	 * When parent stops watching, we clear false positive PARENT_WATCHED
218*fc1b1e13SAmir Goldstein 	 * flags lazily in __fsnotify_parent().
219*fc1b1e13SAmir Goldstein 	 */
220*fc1b1e13SAmir Goldstein 	if (update_children)
221*fc1b1e13SAmir Goldstein 		fsnotify_conn_set_children_dentry_flags(conn);
2220809ab69SJan Kara }
2230809ab69SJan Kara 
22408991e83SJan Kara /* Free all connectors queued for freeing once SRCU period ends */
fsnotify_connector_destroy_workfn(struct work_struct * work)22508991e83SJan Kara static void fsnotify_connector_destroy_workfn(struct work_struct *work)
22608991e83SJan Kara {
22708991e83SJan Kara 	struct fsnotify_mark_connector *conn, *free;
22808991e83SJan Kara 
22908991e83SJan Kara 	spin_lock(&destroy_lock);
23008991e83SJan Kara 	conn = connector_destroy_list;
23108991e83SJan Kara 	connector_destroy_list = NULL;
23208991e83SJan Kara 	spin_unlock(&destroy_lock);
23308991e83SJan Kara 
23408991e83SJan Kara 	synchronize_srcu(&fsnotify_mark_srcu);
23508991e83SJan Kara 	while (conn) {
23608991e83SJan Kara 		free = conn;
23708991e83SJan Kara 		conn = conn->destroy_next;
23808991e83SJan Kara 		kmem_cache_free(fsnotify_mark_connector_cachep, free);
23908991e83SJan Kara 	}
24008991e83SJan Kara }
24108991e83SJan Kara 
fsnotify_put_inode_ref(struct inode * inode)24211fa333bSAmir Goldstein static void fsnotify_put_inode_ref(struct inode *inode)
24311fa333bSAmir Goldstein {
24411fa333bSAmir Goldstein 	struct super_block *sb = inode->i_sb;
24511fa333bSAmir Goldstein 
24611fa333bSAmir Goldstein 	iput(inode);
247ec44610fSAmir Goldstein 	if (atomic_long_dec_and_test(&sb->s_fsnotify_connectors))
248ec44610fSAmir Goldstein 		wake_up_var(&sb->s_fsnotify_connectors);
249ec44610fSAmir Goldstein }
250ec44610fSAmir Goldstein 
fsnotify_get_sb_connectors(struct fsnotify_mark_connector * conn)251ec44610fSAmir Goldstein static void fsnotify_get_sb_connectors(struct fsnotify_mark_connector *conn)
252ec44610fSAmir Goldstein {
253ec44610fSAmir Goldstein 	struct super_block *sb = fsnotify_connector_sb(conn);
254ec44610fSAmir Goldstein 
255ec44610fSAmir Goldstein 	if (sb)
256ec44610fSAmir Goldstein 		atomic_long_inc(&sb->s_fsnotify_connectors);
257ec44610fSAmir Goldstein }
258ec44610fSAmir Goldstein 
fsnotify_put_sb_connectors(struct fsnotify_mark_connector * conn)259ec44610fSAmir Goldstein static void fsnotify_put_sb_connectors(struct fsnotify_mark_connector *conn)
260ec44610fSAmir Goldstein {
261ec44610fSAmir Goldstein 	struct super_block *sb = fsnotify_connector_sb(conn);
262ec44610fSAmir Goldstein 
263ec44610fSAmir Goldstein 	if (sb && atomic_long_dec_and_test(&sb->s_fsnotify_connectors))
264ec44610fSAmir Goldstein 		wake_up_var(&sb->s_fsnotify_connectors);
26511fa333bSAmir Goldstein }
26611fa333bSAmir Goldstein 
fsnotify_detach_connector_from_object(struct fsnotify_mark_connector * conn,unsigned int * type)267721fb6fbSJan Kara static void *fsnotify_detach_connector_from_object(
268721fb6fbSJan Kara 					struct fsnotify_mark_connector *conn,
269721fb6fbSJan Kara 					unsigned int *type)
27008991e83SJan Kara {
27108991e83SJan Kara 	struct inode *inode = NULL;
27208991e83SJan Kara 
273721fb6fbSJan Kara 	*type = conn->type;
27436f10f55SAmir Goldstein 	if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED)
27536f10f55SAmir Goldstein 		return NULL;
27636f10f55SAmir Goldstein 
277d6f7b98bSAmir Goldstein 	if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
27836f10f55SAmir Goldstein 		inode = fsnotify_conn_inode(conn);
27908991e83SJan Kara 		inode->i_fsnotify_mask = 0;
280c3638b5bSAmir Goldstein 
281c3638b5bSAmir Goldstein 		/* Unpin inode when detaching from connector */
282c3638b5bSAmir Goldstein 		if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF))
283c3638b5bSAmir Goldstein 			inode = NULL;
284d6f7b98bSAmir Goldstein 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
28536f10f55SAmir Goldstein 		fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0;
2861e6cb723SAmir Goldstein 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) {
2871e6cb723SAmir Goldstein 		fsnotify_conn_sb(conn)->s_fsnotify_mask = 0;
28808991e83SJan Kara 	}
28908991e83SJan Kara 
290ec44610fSAmir Goldstein 	fsnotify_put_sb_connectors(conn);
29136f10f55SAmir Goldstein 	rcu_assign_pointer(*(conn->obj), NULL);
29236f10f55SAmir Goldstein 	conn->obj = NULL;
29336f10f55SAmir Goldstein 	conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
29436f10f55SAmir Goldstein 
29508991e83SJan Kara 	return inode;
29608991e83SJan Kara }
29708991e83SJan Kara 
fsnotify_final_mark_destroy(struct fsnotify_mark * mark)2986b3f05d2SJan Kara static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark)
2996b3f05d2SJan Kara {
300054c636eSJan Kara 	struct fsnotify_group *group = mark->group;
301054c636eSJan Kara 
302054c636eSJan Kara 	if (WARN_ON_ONCE(!group))
303054c636eSJan Kara 		return;
304054c636eSJan Kara 	group->ops->free_mark(mark);
305054c636eSJan Kara 	fsnotify_put_group(group);
3066b3f05d2SJan Kara }
3076b3f05d2SJan Kara 
308721fb6fbSJan Kara /* Drop object reference originally held by a connector */
fsnotify_drop_object(unsigned int type,void * objp)309721fb6fbSJan Kara static void fsnotify_drop_object(unsigned int type, void *objp)
310721fb6fbSJan Kara {
311721fb6fbSJan Kara 	if (!objp)
312721fb6fbSJan Kara 		return;
313721fb6fbSJan Kara 	/* Currently only inode references are passed to be dropped */
314721fb6fbSJan Kara 	if (WARN_ON_ONCE(type != FSNOTIFY_OBJ_TYPE_INODE))
315721fb6fbSJan Kara 		return;
31611fa333bSAmir Goldstein 	fsnotify_put_inode_ref(objp);
317721fb6fbSJan Kara }
318721fb6fbSJan Kara 
fsnotify_put_mark(struct fsnotify_mark * mark)3196b3f05d2SJan Kara void fsnotify_put_mark(struct fsnotify_mark *mark)
3208212a609SJan Kara {
321b1da6a51SJan Kara 	struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector);
322721fb6fbSJan Kara 	void *objp = NULL;
323721fb6fbSJan Kara 	unsigned int type = FSNOTIFY_OBJ_TYPE_DETACHED;
32408991e83SJan Kara 	bool free_conn = false;
3258212a609SJan Kara 
3266b3f05d2SJan Kara 	/* Catch marks that were actually never attached to object */
327b1da6a51SJan Kara 	if (!conn) {
328ab97f873SElena Reshetova 		if (refcount_dec_and_test(&mark->refcnt))
3296b3f05d2SJan Kara 			fsnotify_final_mark_destroy(mark);
3306b3f05d2SJan Kara 		return;
3316b3f05d2SJan Kara 	}
3326b3f05d2SJan Kara 
3336b3f05d2SJan Kara 	/*
3346b3f05d2SJan Kara 	 * We have to be careful so that traversals of obj_list under lock can
3356b3f05d2SJan Kara 	 * safely grab mark reference.
3366b3f05d2SJan Kara 	 */
337b1da6a51SJan Kara 	if (!refcount_dec_and_lock(&mark->refcnt, &conn->lock))
3386b3f05d2SJan Kara 		return;
3396b3f05d2SJan Kara 
3408212a609SJan Kara 	hlist_del_init_rcu(&mark->obj_list);
3418212a609SJan Kara 	if (hlist_empty(&conn->list)) {
342721fb6fbSJan Kara 		objp = fsnotify_detach_connector_from_object(conn, &type);
34308991e83SJan Kara 		free_conn = true;
34408991e83SJan Kara 	} else {
345c3638b5bSAmir Goldstein 		objp = __fsnotify_recalc_mask(conn);
346c3638b5bSAmir Goldstein 		type = conn->type;
34708991e83SJan Kara 	}
348b1da6a51SJan Kara 	WRITE_ONCE(mark->connector, NULL);
34904662cabSJan Kara 	spin_unlock(&conn->lock);
3508212a609SJan Kara 
351721fb6fbSJan Kara 	fsnotify_drop_object(type, objp);
3526b3f05d2SJan Kara 
35308991e83SJan Kara 	if (free_conn) {
35408991e83SJan Kara 		spin_lock(&destroy_lock);
35508991e83SJan Kara 		conn->destroy_next = connector_destroy_list;
35608991e83SJan Kara 		connector_destroy_list = conn;
35708991e83SJan Kara 		spin_unlock(&destroy_lock);
35808991e83SJan Kara 		queue_work(system_unbound_wq, &connector_reaper_work);
35908991e83SJan Kara 	}
3606b3f05d2SJan Kara 	/*
3616b3f05d2SJan Kara 	 * Note that we didn't update flags telling whether inode cares about
3626b3f05d2SJan Kara 	 * what's happening with children. We update these flags from
3636b3f05d2SJan Kara 	 * __fsnotify_parent() lazily when next event happens on one of our
3646b3f05d2SJan Kara 	 * children.
3656b3f05d2SJan Kara 	 */
3666b3f05d2SJan Kara 	spin_lock(&destroy_lock);
3676b3f05d2SJan Kara 	list_add(&mark->g_list, &destroy_list);
3686b3f05d2SJan Kara 	spin_unlock(&destroy_lock);
3696b3f05d2SJan Kara 	queue_delayed_work(system_unbound_wq, &reaper_work,
3706b3f05d2SJan Kara 			   FSNOTIFY_REAPER_DELAY);
3718212a609SJan Kara }
372b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_put_mark);
3738212a609SJan Kara 
37424c20305SMiklos Szeredi /*
37524c20305SMiklos Szeredi  * Get mark reference when we found the mark via lockless traversal of object
37624c20305SMiklos Szeredi  * list. Mark can be already removed from the list by now and on its way to be
37724c20305SMiklos Szeredi  * destroyed once SRCU period ends.
37824c20305SMiklos Szeredi  *
37924c20305SMiklos Szeredi  * Also pin the group so it doesn't disappear under us.
38024c20305SMiklos Szeredi  */
fsnotify_get_mark_safe(struct fsnotify_mark * mark)38124c20305SMiklos Szeredi static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark)
382abc77577SJan Kara {
38324c20305SMiklos Szeredi 	if (!mark)
38424c20305SMiklos Szeredi 		return true;
385abc77577SJan Kara 
386ab97f873SElena Reshetova 	if (refcount_inc_not_zero(&mark->refcnt)) {
3879a31d7adSMiklos Szeredi 		spin_lock(&mark->lock);
3889a31d7adSMiklos Szeredi 		if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) {
3899a31d7adSMiklos Szeredi 			/* mark is attached, group is still alive then */
3909a31d7adSMiklos Szeredi 			atomic_inc(&mark->group->user_waits);
3919a31d7adSMiklos Szeredi 			spin_unlock(&mark->lock);
39224c20305SMiklos Szeredi 			return true;
3939a31d7adSMiklos Szeredi 		}
3949a31d7adSMiklos Szeredi 		spin_unlock(&mark->lock);
3959a31d7adSMiklos Szeredi 		fsnotify_put_mark(mark);
3969a31d7adSMiklos Szeredi 	}
39724c20305SMiklos Szeredi 	return false;
39824c20305SMiklos Szeredi }
39924c20305SMiklos Szeredi 
40024c20305SMiklos Szeredi /*
40124c20305SMiklos Szeredi  * Puts marks and wakes up group destruction if necessary.
40224c20305SMiklos Szeredi  *
40324c20305SMiklos Szeredi  * Pairs with fsnotify_get_mark_safe()
40424c20305SMiklos Szeredi  */
fsnotify_put_mark_wake(struct fsnotify_mark * mark)40524c20305SMiklos Szeredi static void fsnotify_put_mark_wake(struct fsnotify_mark *mark)
40624c20305SMiklos Szeredi {
40724c20305SMiklos Szeredi 	if (mark) {
40824c20305SMiklos Szeredi 		struct fsnotify_group *group = mark->group;
40924c20305SMiklos Szeredi 
41024c20305SMiklos Szeredi 		fsnotify_put_mark(mark);
41124c20305SMiklos Szeredi 		/*
41224c20305SMiklos Szeredi 		 * We abuse notification_waitq on group shutdown for waiting for
41324c20305SMiklos Szeredi 		 * all marks pinned when waiting for userspace.
41424c20305SMiklos Szeredi 		 */
41524c20305SMiklos Szeredi 		if (atomic_dec_and_test(&group->user_waits) && group->shutdown)
41624c20305SMiklos Szeredi 			wake_up(&group->notification_waitq);
41724c20305SMiklos Szeredi 	}
41824c20305SMiklos Szeredi }
41924c20305SMiklos Szeredi 
fsnotify_prepare_user_wait(struct fsnotify_iter_info * iter_info)42024c20305SMiklos Szeredi bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info)
42100e0afb6SJules Irenge 	__releases(&fsnotify_mark_srcu)
42224c20305SMiklos Szeredi {
42347d9c7ccSAmir Goldstein 	int type;
42447d9c7ccSAmir Goldstein 
4251c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
426abc77577SJan Kara 		/* This can fail if mark is being removed */
42700e0afb6SJules Irenge 		if (!fsnotify_get_mark_safe(iter_info->marks[type])) {
42800e0afb6SJules Irenge 			__release(&fsnotify_mark_srcu);
42947d9c7ccSAmir Goldstein 			goto fail;
430abc77577SJan Kara 		}
43100e0afb6SJules Irenge 	}
432abc77577SJan Kara 
433abc77577SJan Kara 	/*
434abc77577SJan Kara 	 * Now that both marks are pinned by refcount in the inode / vfsmount
435abc77577SJan Kara 	 * lists, we can drop SRCU lock, and safely resume the list iteration
436abc77577SJan Kara 	 * once userspace returns.
437abc77577SJan Kara 	 */
438abc77577SJan Kara 	srcu_read_unlock(&fsnotify_mark_srcu, iter_info->srcu_idx);
439abc77577SJan Kara 
440abc77577SJan Kara 	return true;
44147d9c7ccSAmir Goldstein 
44247d9c7ccSAmir Goldstein fail:
44347d9c7ccSAmir Goldstein 	for (type--; type >= 0; type--)
44447d9c7ccSAmir Goldstein 		fsnotify_put_mark_wake(iter_info->marks[type]);
44547d9c7ccSAmir Goldstein 	return false;
446abc77577SJan Kara }
447abc77577SJan Kara 
fsnotify_finish_user_wait(struct fsnotify_iter_info * iter_info)448abc77577SJan Kara void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info)
44900e0afb6SJules Irenge 	__acquires(&fsnotify_mark_srcu)
450abc77577SJan Kara {
45147d9c7ccSAmir Goldstein 	int type;
45247d9c7ccSAmir Goldstein 
453abc77577SJan Kara 	iter_info->srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
4541c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type)
45547d9c7ccSAmir Goldstein 		fsnotify_put_mark_wake(iter_info->marks[type]);
456abc77577SJan Kara }
457abc77577SJan Kara 
4585444e298SEric Paris /*
4596b3f05d2SJan Kara  * Mark mark as detached, remove it from group list. Mark still stays in object
4606b3f05d2SJan Kara  * list until its last reference is dropped. Note that we rely on mark being
4616b3f05d2SJan Kara  * removed from group list before corresponding reference to it is dropped. In
4626b3f05d2SJan Kara  * particular we rely on mark->connector being valid while we hold
4636b3f05d2SJan Kara  * group->mark_mutex if we found the mark through g_list.
4644712e722SJan Kara  *
46511375145SJan Kara  * Must be called with group->mark_mutex held. The caller must either hold
46611375145SJan Kara  * reference to the mark or be protected by fsnotify_mark_srcu.
4675444e298SEric Paris  */
fsnotify_detach_mark(struct fsnotify_mark * mark)4684712e722SJan Kara void fsnotify_detach_mark(struct fsnotify_mark *mark)
4695444e298SEric Paris {
47043b245a7SAmir Goldstein 	fsnotify_group_assert_locked(mark->group);
47111375145SJan Kara 	WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) &&
472ab97f873SElena Reshetova 		     refcount_read(&mark->refcnt) < 1 +
47311375145SJan Kara 			!!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED));
474d5a335b8SLino Sanfilippo 
475104d06f0SLino Sanfilippo 	spin_lock(&mark->lock);
476700307a2SEric Paris 	/* something else already called this function on this mark */
4774712e722SJan Kara 	if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
4785444e298SEric Paris 		spin_unlock(&mark->lock);
479e2a29943SLino Sanfilippo 		return;
4805444e298SEric Paris 	}
4814712e722SJan Kara 	mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
4825444e298SEric Paris 	list_del_init(&mark->g_list);
4835444e298SEric Paris 	spin_unlock(&mark->lock);
484d5a335b8SLino Sanfilippo 
48511375145SJan Kara 	/* Drop mark reference acquired in fsnotify_add_mark_locked() */
48611375145SJan Kara 	fsnotify_put_mark(mark);
4874712e722SJan Kara }
4884712e722SJan Kara 
4894712e722SJan Kara /*
49011375145SJan Kara  * Free fsnotify mark. The mark is actually only marked as being freed.  The
49111375145SJan Kara  * freeing is actually happening only once last reference to the mark is
49211375145SJan Kara  * dropped from a workqueue which first waits for srcu period end.
49335e48176SJan Kara  *
49411375145SJan Kara  * Caller must have a reference to the mark or be protected by
49511375145SJan Kara  * fsnotify_mark_srcu.
4964712e722SJan Kara  */
fsnotify_free_mark(struct fsnotify_mark * mark)49711375145SJan Kara void fsnotify_free_mark(struct fsnotify_mark *mark)
4984712e722SJan Kara {
4994712e722SJan Kara 	struct fsnotify_group *group = mark->group;
5004712e722SJan Kara 
5014712e722SJan Kara 	spin_lock(&mark->lock);
5024712e722SJan Kara 	/* something else already called this function on this mark */
5034712e722SJan Kara 	if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
5044712e722SJan Kara 		spin_unlock(&mark->lock);
50511375145SJan Kara 		return;
5064712e722SJan Kara 	}
5074712e722SJan Kara 	mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
5084712e722SJan Kara 	spin_unlock(&mark->lock);
5095444e298SEric Paris 
510d725e66cSLinus Torvalds 	/*
511d725e66cSLinus Torvalds 	 * Some groups like to know that marks are being freed.  This is a
512d725e66cSLinus Torvalds 	 * callback to the group function to let it know that this mark
513d725e66cSLinus Torvalds 	 * is being freed.
514d725e66cSLinus Torvalds 	 */
515d725e66cSLinus Torvalds 	if (group->ops->freeing_mark)
516d725e66cSLinus Torvalds 		group->ops->freeing_mark(mark, group);
517d5a335b8SLino Sanfilippo }
518d5a335b8SLino Sanfilippo 
fsnotify_destroy_mark(struct fsnotify_mark * mark,struct fsnotify_group * group)519d5a335b8SLino Sanfilippo void fsnotify_destroy_mark(struct fsnotify_mark *mark,
520d5a335b8SLino Sanfilippo 			   struct fsnotify_group *group)
521d5a335b8SLino Sanfilippo {
52243b245a7SAmir Goldstein 	fsnotify_group_lock(group);
5234712e722SJan Kara 	fsnotify_detach_mark(mark);
52443b245a7SAmir Goldstein 	fsnotify_group_unlock(group);
5254712e722SJan Kara 	fsnotify_free_mark(mark);
5265444e298SEric Paris }
527b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
5285444e298SEric Paris 
5295444e298SEric Paris /*
5308edc6e16SJan Kara  * Sorting function for lists of fsnotify marks.
5318edc6e16SJan Kara  *
5328edc6e16SJan Kara  * Fanotify supports different notification classes (reflected as priority of
5338edc6e16SJan Kara  * notification group). Events shall be passed to notification groups in
5348edc6e16SJan Kara  * decreasing priority order. To achieve this marks in notification lists for
5358edc6e16SJan Kara  * inodes and vfsmounts are sorted so that priorities of corresponding groups
5368edc6e16SJan Kara  * are descending.
5378edc6e16SJan Kara  *
5388edc6e16SJan Kara  * Furthermore correct handling of the ignore mask requires processing inode
5398edc6e16SJan Kara  * and vfsmount marks of each group together. Using the group address as
5408edc6e16SJan Kara  * further sort criterion provides a unique sorting order and thus we can
5418edc6e16SJan Kara  * merge inode and vfsmount lists of marks in linear time and find groups
5428edc6e16SJan Kara  * present in both lists.
5438edc6e16SJan Kara  *
5448edc6e16SJan Kara  * A return value of 1 signifies that b has priority over a.
5458edc6e16SJan Kara  * A return value of 0 signifies that the two marks have to be handled together.
5468edc6e16SJan Kara  * A return value of -1 signifies that a has priority over b.
5478edc6e16SJan Kara  */
fsnotify_compare_groups(struct fsnotify_group * a,struct fsnotify_group * b)5488edc6e16SJan Kara int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
5498edc6e16SJan Kara {
5508edc6e16SJan Kara 	if (a == b)
5518edc6e16SJan Kara 		return 0;
5528edc6e16SJan Kara 	if (!a)
5538edc6e16SJan Kara 		return 1;
5548edc6e16SJan Kara 	if (!b)
5558edc6e16SJan Kara 		return -1;
5568edc6e16SJan Kara 	if (a->priority < b->priority)
5578edc6e16SJan Kara 		return 1;
5588edc6e16SJan Kara 	if (a->priority > b->priority)
5598edc6e16SJan Kara 		return -1;
5608edc6e16SJan Kara 	if (a < b)
5618edc6e16SJan Kara 		return 1;
5628edc6e16SJan Kara 	return -1;
5638edc6e16SJan Kara }
5648edc6e16SJan Kara 
fsnotify_attach_connector_to_object(fsnotify_connp_t * connp,unsigned int obj_type,__kernel_fsid_t * fsid)5659b6e5434SAmir Goldstein static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
566ad69cd99SAmir Goldstein 					       unsigned int obj_type,
56777115225SAmir Goldstein 					       __kernel_fsid_t *fsid)
5689dd813c1SJan Kara {
5699dd813c1SJan Kara 	struct fsnotify_mark_connector *conn;
5709dd813c1SJan Kara 
571755b5bc6SJan Kara 	conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
5729dd813c1SJan Kara 	if (!conn)
5739dd813c1SJan Kara 		return -ENOMEM;
57404662cabSJan Kara 	spin_lock_init(&conn->lock);
5759dd813c1SJan Kara 	INIT_HLIST_HEAD(&conn->list);
576c3638b5bSAmir Goldstein 	conn->flags = 0;
577ad69cd99SAmir Goldstein 	conn->type = obj_type;
57836f10f55SAmir Goldstein 	conn->obj = connp;
57977115225SAmir Goldstein 	/* Cache fsid of filesystem containing the object */
580c285a2f0SAmir Goldstein 	if (fsid) {
58177115225SAmir Goldstein 		conn->fsid = *fsid;
582c285a2f0SAmir Goldstein 		conn->flags = FSNOTIFY_CONN_FLAG_HAS_FSID;
583c285a2f0SAmir Goldstein 	} else {
58477115225SAmir Goldstein 		conn->fsid.val[0] = conn->fsid.val[1] = 0;
585c285a2f0SAmir Goldstein 		conn->flags = 0;
586c285a2f0SAmir Goldstein 	}
587ec44610fSAmir Goldstein 	fsnotify_get_sb_connectors(conn);
58809ddbe69SAmir Goldstein 
5899dd813c1SJan Kara 	/*
59004662cabSJan Kara 	 * cmpxchg() provides the barrier so that readers of *connp can see
59104662cabSJan Kara 	 * only initialized structure
5929dd813c1SJan Kara 	 */
59304662cabSJan Kara 	if (cmpxchg(connp, NULL, conn)) {
59404662cabSJan Kara 		/* Someone else created list structure for us */
5954396a731SAmir Goldstein 		fsnotify_put_sb_connectors(conn);
596755b5bc6SJan Kara 		kmem_cache_free(fsnotify_mark_connector_cachep, conn);
59704662cabSJan Kara 	}
5989dd813c1SJan Kara 
5999dd813c1SJan Kara 	return 0;
6009dd813c1SJan Kara }
6019dd813c1SJan Kara 
6029dd813c1SJan Kara /*
60308991e83SJan Kara  * Get mark connector, make sure it is alive and return with its lock held.
60408991e83SJan Kara  * This is for users that get connector pointer from inode or mount. Users that
60508991e83SJan Kara  * hold reference to a mark on the list may directly lock connector->lock as
60608991e83SJan Kara  * they are sure list cannot go away under them.
60708991e83SJan Kara  */
fsnotify_grab_connector(fsnotify_connp_t * connp)60808991e83SJan Kara static struct fsnotify_mark_connector *fsnotify_grab_connector(
6099b6e5434SAmir Goldstein 						fsnotify_connp_t *connp)
61008991e83SJan Kara {
61108991e83SJan Kara 	struct fsnotify_mark_connector *conn;
61208991e83SJan Kara 	int idx;
61308991e83SJan Kara 
61408991e83SJan Kara 	idx = srcu_read_lock(&fsnotify_mark_srcu);
61508991e83SJan Kara 	conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
61608991e83SJan Kara 	if (!conn)
61708991e83SJan Kara 		goto out;
61808991e83SJan Kara 	spin_lock(&conn->lock);
619d6f7b98bSAmir Goldstein 	if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) {
62008991e83SJan Kara 		spin_unlock(&conn->lock);
62108991e83SJan Kara 		srcu_read_unlock(&fsnotify_mark_srcu, idx);
62208991e83SJan Kara 		return NULL;
62308991e83SJan Kara 	}
62408991e83SJan Kara out:
62508991e83SJan Kara 	srcu_read_unlock(&fsnotify_mark_srcu, idx);
62608991e83SJan Kara 	return conn;
62708991e83SJan Kara }
62808991e83SJan Kara 
62908991e83SJan Kara /*
6309dd813c1SJan Kara  * Add mark into proper place in given list of marks. These marks may be used
6319dd813c1SJan Kara  * for the fsnotify backend to determine which event types should be delivered
6329dd813c1SJan Kara  * to which group and for which inodes. These marks are ordered according to
6339dd813c1SJan Kara  * priority, highest number first, and then by the group's location in memory.
6349dd813c1SJan Kara  */
fsnotify_add_mark_list(struct fsnotify_mark * mark,fsnotify_connp_t * connp,unsigned int obj_type,int add_flags,__kernel_fsid_t * fsid)635755b5bc6SJan Kara static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
636ad69cd99SAmir Goldstein 				  fsnotify_connp_t *connp,
637ad69cd99SAmir Goldstein 				  unsigned int obj_type,
638f3010343SAmir Goldstein 				  int add_flags, __kernel_fsid_t *fsid)
6390809ab69SJan Kara {
6400809ab69SJan Kara 	struct fsnotify_mark *lmark, *last = NULL;
6419dd813c1SJan Kara 	struct fsnotify_mark_connector *conn;
6420809ab69SJan Kara 	int cmp;
643755b5bc6SJan Kara 	int err = 0;
644755b5bc6SJan Kara 
645ad69cd99SAmir Goldstein 	if (WARN_ON(!fsnotify_valid_obj_type(obj_type)))
646755b5bc6SJan Kara 		return -EINVAL;
64777115225SAmir Goldstein 
64877115225SAmir Goldstein 	/* Backend is expected to check for zero fsid (e.g. tmpfs) */
64977115225SAmir Goldstein 	if (fsid && WARN_ON_ONCE(!fsid->val[0] && !fsid->val[1]))
65077115225SAmir Goldstein 		return -ENODEV;
65177115225SAmir Goldstein 
65208991e83SJan Kara restart:
65308991e83SJan Kara 	spin_lock(&mark->lock);
65408991e83SJan Kara 	conn = fsnotify_grab_connector(connp);
65508991e83SJan Kara 	if (!conn) {
65608991e83SJan Kara 		spin_unlock(&mark->lock);
657ad69cd99SAmir Goldstein 		err = fsnotify_attach_connector_to_object(connp, obj_type,
658ad69cd99SAmir Goldstein 							  fsid);
6599dd813c1SJan Kara 		if (err)
6609dd813c1SJan Kara 			return err;
66108991e83SJan Kara 		goto restart;
662c285a2f0SAmir Goldstein 	} else if (fsid && !(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID)) {
663c285a2f0SAmir Goldstein 		conn->fsid = *fsid;
664c285a2f0SAmir Goldstein 		/* Pairs with smp_rmb() in fanotify_get_fsid() */
665c285a2f0SAmir Goldstein 		smp_wmb();
666c285a2f0SAmir Goldstein 		conn->flags |= FSNOTIFY_CONN_FLAG_HAS_FSID;
667c285a2f0SAmir Goldstein 	} else if (fsid && (conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID) &&
66877115225SAmir Goldstein 		   (fsid->val[0] != conn->fsid.val[0] ||
66977115225SAmir Goldstein 		    fsid->val[1] != conn->fsid.val[1])) {
67077115225SAmir Goldstein 		/*
67177115225SAmir Goldstein 		 * Backend is expected to check for non uniform fsid
67277115225SAmir Goldstein 		 * (e.g. btrfs), but maybe we missed something?
67377115225SAmir Goldstein 		 * Only allow setting conn->fsid once to non zero fsid.
67477115225SAmir Goldstein 		 * inotify and non-fid fanotify groups do not set nor test
67577115225SAmir Goldstein 		 * conn->fsid.
67677115225SAmir Goldstein 		 */
67777115225SAmir Goldstein 		pr_warn_ratelimited("%s: fsid mismatch on object of type %u: "
67877115225SAmir Goldstein 				    "%x.%x != %x.%x\n", __func__, conn->type,
67977115225SAmir Goldstein 				    fsid->val[0], fsid->val[1],
68077115225SAmir Goldstein 				    conn->fsid.val[0], conn->fsid.val[1]);
68177115225SAmir Goldstein 		err = -EXDEV;
68277115225SAmir Goldstein 		goto out_err;
6839dd813c1SJan Kara 	}
6840809ab69SJan Kara 
6850809ab69SJan Kara 	/* is mark the first mark? */
6869dd813c1SJan Kara 	if (hlist_empty(&conn->list)) {
6879dd813c1SJan Kara 		hlist_add_head_rcu(&mark->obj_list, &conn->list);
68886ffe245SJan Kara 		goto added;
6890809ab69SJan Kara 	}
6900809ab69SJan Kara 
6910809ab69SJan Kara 	/* should mark be in the middle of the current list? */
6929dd813c1SJan Kara 	hlist_for_each_entry(lmark, &conn->list, obj_list) {
6930809ab69SJan Kara 		last = lmark;
6940809ab69SJan Kara 
6956b3f05d2SJan Kara 		if ((lmark->group == mark->group) &&
6966b3f05d2SJan Kara 		    (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) &&
697f3010343SAmir Goldstein 		    !(mark->group->flags & FSNOTIFY_GROUP_DUPS)) {
698755b5bc6SJan Kara 			err = -EEXIST;
699755b5bc6SJan Kara 			goto out_err;
700755b5bc6SJan Kara 		}
7010809ab69SJan Kara 
7020809ab69SJan Kara 		cmp = fsnotify_compare_groups(lmark->group, mark->group);
7030809ab69SJan Kara 		if (cmp >= 0) {
7040809ab69SJan Kara 			hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
70586ffe245SJan Kara 			goto added;
7060809ab69SJan Kara 		}
7070809ab69SJan Kara 	}
7080809ab69SJan Kara 
7090809ab69SJan Kara 	BUG_ON(last == NULL);
7100809ab69SJan Kara 	/* mark should be the last entry.  last is the current last entry */
7110809ab69SJan Kara 	hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
71286ffe245SJan Kara added:
71311a6f8e2SJan Kara 	/*
71411a6f8e2SJan Kara 	 * Since connector is attached to object using cmpxchg() we are
71511a6f8e2SJan Kara 	 * guaranteed that connector initialization is fully visible by anyone
71611a6f8e2SJan Kara 	 * seeing mark->connector set.
71711a6f8e2SJan Kara 	 */
718b1da6a51SJan Kara 	WRITE_ONCE(mark->connector, conn);
719755b5bc6SJan Kara out_err:
72004662cabSJan Kara 	spin_unlock(&conn->lock);
721755b5bc6SJan Kara 	spin_unlock(&mark->lock);
722755b5bc6SJan Kara 	return err;
7230809ab69SJan Kara }
7240809ab69SJan Kara 
7258edc6e16SJan Kara /*
7265444e298SEric Paris  * Attach an initialized mark to a given group and fs object.
7275444e298SEric Paris  * These marks may be used for the fsnotify backend to determine which
7285444e298SEric Paris  * event types should be delivered to which group.
7295444e298SEric Paris  */
fsnotify_add_mark_locked(struct fsnotify_mark * mark,fsnotify_connp_t * connp,unsigned int obj_type,int add_flags,__kernel_fsid_t * fsid)730b812a9f5SAmir Goldstein int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
731ad69cd99SAmir Goldstein 			     fsnotify_connp_t *connp, unsigned int obj_type,
732f3010343SAmir Goldstein 			     int add_flags, __kernel_fsid_t *fsid)
7335444e298SEric Paris {
7347b129323SJan Kara 	struct fsnotify_group *group = mark->group;
7355444e298SEric Paris 	int ret = 0;
7365444e298SEric Paris 
73743b245a7SAmir Goldstein 	fsnotify_group_assert_locked(group);
7385444e298SEric Paris 
7395444e298SEric Paris 	/*
7405444e298SEric Paris 	 * LOCKING ORDER!!!!
741986ab098SLino Sanfilippo 	 * group->mark_mutex
742104d06f0SLino Sanfilippo 	 * mark->lock
74304662cabSJan Kara 	 * mark->connector->lock
7445444e298SEric Paris 	 */
745104d06f0SLino Sanfilippo 	spin_lock(&mark->lock);
7464712e722SJan Kara 	mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
747700307a2SEric Paris 
7485444e298SEric Paris 	list_add(&mark->g_list, &group->marks_list);
7496b3f05d2SJan Kara 	fsnotify_get_mark(mark); /* for g_list */
7505444e298SEric Paris 	spin_unlock(&mark->lock);
7515444e298SEric Paris 
752f3010343SAmir Goldstein 	ret = fsnotify_add_mark_list(mark, connp, obj_type, add_flags, fsid);
753755b5bc6SJan Kara 	if (ret)
754755b5bc6SJan Kara 		goto err;
755755b5bc6SJan Kara 
756a242677bSJan Kara 	fsnotify_recalc_mask(mark->connector);
7575444e298SEric Paris 
7585444e298SEric Paris 	return ret;
7595444e298SEric Paris err:
7609cf90cefSJan Kara 	spin_lock(&mark->lock);
76111375145SJan Kara 	mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE |
76211375145SJan Kara 			 FSNOTIFY_MARK_FLAG_ATTACHED);
7635444e298SEric Paris 	list_del_init(&mark->g_list);
7649cf90cefSJan Kara 	spin_unlock(&mark->lock);
7655444e298SEric Paris 
76611375145SJan Kara 	fsnotify_put_mark(mark);
7675444e298SEric Paris 	return ret;
7685444e298SEric Paris }
7695444e298SEric Paris 
fsnotify_add_mark(struct fsnotify_mark * mark,fsnotify_connp_t * connp,unsigned int obj_type,int add_flags,__kernel_fsid_t * fsid)770b812a9f5SAmir Goldstein int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
771f3010343SAmir Goldstein 		      unsigned int obj_type, int add_flags,
772ad69cd99SAmir Goldstein 		      __kernel_fsid_t *fsid)
773d5a335b8SLino Sanfilippo {
774d5a335b8SLino Sanfilippo 	int ret;
7757b129323SJan Kara 	struct fsnotify_group *group = mark->group;
7767b129323SJan Kara 
77743b245a7SAmir Goldstein 	fsnotify_group_lock(group);
778f3010343SAmir Goldstein 	ret = fsnotify_add_mark_locked(mark, connp, obj_type, add_flags, fsid);
77943b245a7SAmir Goldstein 	fsnotify_group_unlock(group);
780d5a335b8SLino Sanfilippo 	return ret;
781d5a335b8SLino Sanfilippo }
782b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_add_mark);
783d5a335b8SLino Sanfilippo 
7845444e298SEric Paris /*
7850809ab69SJan Kara  * Given a list of marks, find the mark associated with given group. If found
7860809ab69SJan Kara  * take a reference to that mark and return it, else return NULL.
7870809ab69SJan Kara  */
fsnotify_find_mark(fsnotify_connp_t * connp,struct fsnotify_group * group)7889b6e5434SAmir Goldstein struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
7890809ab69SJan Kara 					 struct fsnotify_group *group)
7900809ab69SJan Kara {
79108991e83SJan Kara 	struct fsnotify_mark_connector *conn;
7920809ab69SJan Kara 	struct fsnotify_mark *mark;
7930809ab69SJan Kara 
79408991e83SJan Kara 	conn = fsnotify_grab_connector(connp);
7959dd813c1SJan Kara 	if (!conn)
7969dd813c1SJan Kara 		return NULL;
7979dd813c1SJan Kara 
7989dd813c1SJan Kara 	hlist_for_each_entry(mark, &conn->list, obj_list) {
7996b3f05d2SJan Kara 		if (mark->group == group &&
8006b3f05d2SJan Kara 		    (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
8010809ab69SJan Kara 			fsnotify_get_mark(mark);
80204662cabSJan Kara 			spin_unlock(&conn->lock);
8030809ab69SJan Kara 			return mark;
8040809ab69SJan Kara 		}
8050809ab69SJan Kara 	}
80604662cabSJan Kara 	spin_unlock(&conn->lock);
8070809ab69SJan Kara 	return NULL;
8080809ab69SJan Kara }
809b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_find_mark);
8100809ab69SJan Kara 
811d6f7b98bSAmir Goldstein /* Clear any marks in a group with given type mask */
fsnotify_clear_marks_by_group(struct fsnotify_group * group,unsigned int obj_type)81218f2e0d3SJan Kara void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
813ad69cd99SAmir Goldstein 				   unsigned int obj_type)
8145444e298SEric Paris {
8155444e298SEric Paris 	struct fsnotify_mark *lmark, *mark;
8168f2f3eb5SJan Kara 	LIST_HEAD(to_free);
8172e37c6caSJan Kara 	struct list_head *head = &to_free;
8185444e298SEric Paris 
8192e37c6caSJan Kara 	/* Skip selection step if we want to clear all marks. */
820ad69cd99SAmir Goldstein 	if (obj_type == FSNOTIFY_OBJ_TYPE_ANY) {
8212e37c6caSJan Kara 		head = &group->marks_list;
8222e37c6caSJan Kara 		goto clear;
8232e37c6caSJan Kara 	}
8248f2f3eb5SJan Kara 	/*
8258f2f3eb5SJan Kara 	 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
8268f2f3eb5SJan Kara 	 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
8278f2f3eb5SJan Kara 	 * to_free list so we have to use mark_mutex even when accessing that
8288f2f3eb5SJan Kara 	 * list. And freeing mark requires us to drop mark_mutex. So we can
8298f2f3eb5SJan Kara 	 * reliably free only the first mark in the list. That's why we first
8308f2f3eb5SJan Kara 	 * move marks to free to to_free list in one go and then free marks in
8318f2f3eb5SJan Kara 	 * to_free list one by one.
8328f2f3eb5SJan Kara 	 */
83343b245a7SAmir Goldstein 	fsnotify_group_lock(group);
8345444e298SEric Paris 	list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
835ad69cd99SAmir Goldstein 		if (mark->connector->type == obj_type)
8368f2f3eb5SJan Kara 			list_move(&mark->g_list, &to_free);
8374d92604cSEric Paris 	}
83843b245a7SAmir Goldstein 	fsnotify_group_unlock(group);
8398f2f3eb5SJan Kara 
8402e37c6caSJan Kara clear:
8418f2f3eb5SJan Kara 	while (1) {
84243b245a7SAmir Goldstein 		fsnotify_group_lock(group);
8432e37c6caSJan Kara 		if (list_empty(head)) {
84443b245a7SAmir Goldstein 			fsnotify_group_unlock(group);
8458f2f3eb5SJan Kara 			break;
8468f2f3eb5SJan Kara 		}
8472e37c6caSJan Kara 		mark = list_first_entry(head, struct fsnotify_mark, g_list);
8488f2f3eb5SJan Kara 		fsnotify_get_mark(mark);
8494712e722SJan Kara 		fsnotify_detach_mark(mark);
85043b245a7SAmir Goldstein 		fsnotify_group_unlock(group);
8514712e722SJan Kara 		fsnotify_free_mark(mark);
8528f2f3eb5SJan Kara 		fsnotify_put_mark(mark);
8538f2f3eb5SJan Kara 	}
8545444e298SEric Paris }
8555444e298SEric Paris 
8569b6e5434SAmir Goldstein /* Destroy all marks attached to an object via connector */
fsnotify_destroy_marks(fsnotify_connp_t * connp)8579b6e5434SAmir Goldstein void fsnotify_destroy_marks(fsnotify_connp_t *connp)
8580810b4f9SJan Kara {
85908991e83SJan Kara 	struct fsnotify_mark_connector *conn;
8606b3f05d2SJan Kara 	struct fsnotify_mark *mark, *old_mark = NULL;
861721fb6fbSJan Kara 	void *objp;
862721fb6fbSJan Kara 	unsigned int type;
8630810b4f9SJan Kara 
8646b3f05d2SJan Kara 	conn = fsnotify_grab_connector(connp);
8656b3f05d2SJan Kara 	if (!conn)
8666b3f05d2SJan Kara 		return;
8670810b4f9SJan Kara 	/*
8680810b4f9SJan Kara 	 * We have to be careful since we can race with e.g.
8696b3f05d2SJan Kara 	 * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the
8706b3f05d2SJan Kara 	 * list can get modified. However we are holding mark reference and
8716b3f05d2SJan Kara 	 * thus our mark cannot be removed from obj_list so we can continue
8726b3f05d2SJan Kara 	 * iteration after regaining conn->lock.
8730810b4f9SJan Kara 	 */
8746b3f05d2SJan Kara 	hlist_for_each_entry(mark, &conn->list, obj_list) {
8750810b4f9SJan Kara 		fsnotify_get_mark(mark);
87604662cabSJan Kara 		spin_unlock(&conn->lock);
8776b3f05d2SJan Kara 		if (old_mark)
8786b3f05d2SJan Kara 			fsnotify_put_mark(old_mark);
8796b3f05d2SJan Kara 		old_mark = mark;
8800810b4f9SJan Kara 		fsnotify_destroy_mark(mark, mark->group);
8816b3f05d2SJan Kara 		spin_lock(&conn->lock);
8820810b4f9SJan Kara 	}
8836b3f05d2SJan Kara 	/*
8846b3f05d2SJan Kara 	 * Detach list from object now so that we don't pin inode until all
8856b3f05d2SJan Kara 	 * mark references get dropped. It would lead to strange results such
8866b3f05d2SJan Kara 	 * as delaying inode deletion or blocking unmount.
8876b3f05d2SJan Kara 	 */
888721fb6fbSJan Kara 	objp = fsnotify_detach_connector_from_object(conn, &type);
8896b3f05d2SJan Kara 	spin_unlock(&conn->lock);
8906b3f05d2SJan Kara 	if (old_mark)
8916b3f05d2SJan Kara 		fsnotify_put_mark(old_mark);
892721fb6fbSJan Kara 	fsnotify_drop_object(type, objp);
8930810b4f9SJan Kara }
8940810b4f9SJan Kara 
8955444e298SEric Paris /*
8965444e298SEric Paris  * Nothing fancy, just initialize lists and locks and counters.
8975444e298SEric Paris  */
fsnotify_init_mark(struct fsnotify_mark * mark,struct fsnotify_group * group)8985444e298SEric Paris void fsnotify_init_mark(struct fsnotify_mark *mark,
899054c636eSJan Kara 			struct fsnotify_group *group)
9005444e298SEric Paris {
901ba643f04SEric Paris 	memset(mark, 0, sizeof(*mark));
9025444e298SEric Paris 	spin_lock_init(&mark->lock);
903ab97f873SElena Reshetova 	refcount_set(&mark->refcnt, 1);
9047b129323SJan Kara 	fsnotify_get_group(group);
9057b129323SJan Kara 	mark->group = group;
906b1da6a51SJan Kara 	WRITE_ONCE(mark->connector, NULL);
9075444e298SEric Paris }
908b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_init_mark);
90913d34ac6SJeff Layton 
91035e48176SJan Kara /*
91135e48176SJan Kara  * Destroy all marks in destroy_list, waits for SRCU period to finish before
91235e48176SJan Kara  * actually freeing marks.
91335e48176SJan Kara  */
fsnotify_mark_destroy_workfn(struct work_struct * work)914f09b04a0SJan Kara static void fsnotify_mark_destroy_workfn(struct work_struct *work)
91513d34ac6SJeff Layton {
91613d34ac6SJeff Layton 	struct fsnotify_mark *mark, *next;
91713d34ac6SJeff Layton 	struct list_head private_destroy_list;
91813d34ac6SJeff Layton 
91913d34ac6SJeff Layton 	spin_lock(&destroy_lock);
92013d34ac6SJeff Layton 	/* exchange the list head */
92113d34ac6SJeff Layton 	list_replace_init(&destroy_list, &private_destroy_list);
92213d34ac6SJeff Layton 	spin_unlock(&destroy_lock);
92313d34ac6SJeff Layton 
92413d34ac6SJeff Layton 	synchronize_srcu(&fsnotify_mark_srcu);
92513d34ac6SJeff Layton 
92613d34ac6SJeff Layton 	list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
92713d34ac6SJeff Layton 		list_del_init(&mark->g_list);
9286b3f05d2SJan Kara 		fsnotify_final_mark_destroy(mark);
92913d34ac6SJeff Layton 	}
93013d34ac6SJeff Layton }
93135e48176SJan Kara 
932f09b04a0SJan Kara /* Wait for all marks queued for destruction to be actually destroyed */
fsnotify_wait_marks_destroyed(void)933f09b04a0SJan Kara void fsnotify_wait_marks_destroyed(void)
93435e48176SJan Kara {
935f09b04a0SJan Kara 	flush_delayed_work(&reaper_work);
93635e48176SJan Kara }
937b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_wait_marks_destroyed);
938