xref: /linux/fs/notify/mark.c (revision f115815d75332f9dabb0d7c29c8f67b0f26889c5)
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 
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 
100*f115815dSAmir Goldstein static fsnotify_connp_t *fsnotify_object_connp(void *obj,
101*f115815dSAmir Goldstein 				enum fsnotify_obj_type obj_type)
102*f115815dSAmir Goldstein {
103*f115815dSAmir Goldstein 	switch (obj_type) {
104*f115815dSAmir Goldstein 	case FSNOTIFY_OBJ_TYPE_INODE:
105*f115815dSAmir Goldstein 		return &((struct inode *)obj)->i_fsnotify_marks;
106*f115815dSAmir Goldstein 	case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
107*f115815dSAmir Goldstein 		return &real_mount(obj)->mnt_fsnotify_marks;
108*f115815dSAmir Goldstein 	case FSNOTIFY_OBJ_TYPE_SB:
109*f115815dSAmir Goldstein 		return &((struct super_block *)obj)->s_fsnotify_marks;
110*f115815dSAmir Goldstein 	default:
111*f115815dSAmir Goldstein 		return NULL;
112*f115815dSAmir Goldstein 	}
113*f115815dSAmir Goldstein }
114*f115815dSAmir Goldstein 
1153ac70bfcSAmir Goldstein static __u32 *fsnotify_conn_mask_p(struct fsnotify_mark_connector *conn)
1163ac70bfcSAmir Goldstein {
1173ac70bfcSAmir Goldstein 	if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
1183ac70bfcSAmir Goldstein 		return &fsnotify_conn_inode(conn)->i_fsnotify_mask;
1193ac70bfcSAmir Goldstein 	else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT)
1203ac70bfcSAmir Goldstein 		return &fsnotify_conn_mount(conn)->mnt_fsnotify_mask;
1211e6cb723SAmir Goldstein 	else if (conn->type == FSNOTIFY_OBJ_TYPE_SB)
1221e6cb723SAmir Goldstein 		return &fsnotify_conn_sb(conn)->s_fsnotify_mask;
1233ac70bfcSAmir Goldstein 	return NULL;
1243ac70bfcSAmir Goldstein }
1253ac70bfcSAmir Goldstein 
1263ac70bfcSAmir Goldstein __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn)
1273ac70bfcSAmir Goldstein {
1283ac70bfcSAmir Goldstein 	if (WARN_ON(!fsnotify_valid_obj_type(conn->type)))
1293ac70bfcSAmir Goldstein 		return 0;
1303ac70bfcSAmir Goldstein 
1313ac70bfcSAmir Goldstein 	return *fsnotify_conn_mask_p(conn);
1323ac70bfcSAmir Goldstein }
1333ac70bfcSAmir Goldstein 
134d2f277e2SAmir Goldstein static void fsnotify_get_sb_watched_objects(struct super_block *sb)
135d2f277e2SAmir Goldstein {
136d2f277e2SAmir Goldstein 	atomic_long_inc(fsnotify_sb_watched_objects(sb));
137d2f277e2SAmir Goldstein }
138d2f277e2SAmir Goldstein 
139d2f277e2SAmir Goldstein static void fsnotify_put_sb_watched_objects(struct super_block *sb)
140d2f277e2SAmir Goldstein {
141d2f277e2SAmir Goldstein 	if (atomic_long_dec_and_test(fsnotify_sb_watched_objects(sb)))
142d2f277e2SAmir Goldstein 		wake_up_var(fsnotify_sb_watched_objects(sb));
143d2f277e2SAmir Goldstein }
144d2f277e2SAmir Goldstein 
145c3638b5bSAmir Goldstein static void fsnotify_get_inode_ref(struct inode *inode)
146c3638b5bSAmir Goldstein {
147c3638b5bSAmir Goldstein 	ihold(inode);
148d2f277e2SAmir Goldstein 	fsnotify_get_sb_watched_objects(inode->i_sb);
149d2f277e2SAmir Goldstein }
150d2f277e2SAmir Goldstein 
151d2f277e2SAmir Goldstein static void fsnotify_put_inode_ref(struct inode *inode)
152d2f277e2SAmir Goldstein {
153d2f277e2SAmir Goldstein 	fsnotify_put_sb_watched_objects(inode->i_sb);
154d2f277e2SAmir Goldstein 	iput(inode);
155d2f277e2SAmir Goldstein }
156d2f277e2SAmir Goldstein 
157d2f277e2SAmir Goldstein static void fsnotify_get_sb_watchers(struct fsnotify_mark_connector *conn)
158d2f277e2SAmir Goldstein {
159d2f277e2SAmir Goldstein 	struct super_block *sb = fsnotify_connector_sb(conn);
160d2f277e2SAmir Goldstein 
161d2f277e2SAmir Goldstein 	if (sb)
162d2f277e2SAmir Goldstein 		fsnotify_get_sb_watched_objects(sb);
163d2f277e2SAmir Goldstein }
164d2f277e2SAmir Goldstein 
165d2f277e2SAmir Goldstein static void fsnotify_put_sb_watchers(struct fsnotify_mark_connector *conn)
166d2f277e2SAmir Goldstein {
167d2f277e2SAmir Goldstein 	struct super_block *sb = fsnotify_connector_sb(conn);
168d2f277e2SAmir Goldstein 
169d2f277e2SAmir Goldstein 	if (sb)
170d2f277e2SAmir Goldstein 		fsnotify_put_sb_watched_objects(sb);
171c3638b5bSAmir Goldstein }
172c3638b5bSAmir Goldstein 
173c3638b5bSAmir Goldstein /*
174c3638b5bSAmir Goldstein  * Grab or drop inode reference for the connector if needed.
175c3638b5bSAmir Goldstein  *
176c3638b5bSAmir Goldstein  * When it's time to drop the reference, we only clear the HAS_IREF flag and
177c3638b5bSAmir Goldstein  * return the inode object. fsnotify_drop_object() will be resonsible for doing
178c3638b5bSAmir Goldstein  * iput() outside of spinlocks. This happens when last mark that wanted iref is
179c3638b5bSAmir Goldstein  * detached.
180c3638b5bSAmir Goldstein  */
181c3638b5bSAmir Goldstein static struct inode *fsnotify_update_iref(struct fsnotify_mark_connector *conn,
182c3638b5bSAmir Goldstein 					  bool want_iref)
183c3638b5bSAmir Goldstein {
184c3638b5bSAmir Goldstein 	bool has_iref = conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF;
185c3638b5bSAmir Goldstein 	struct inode *inode = NULL;
186c3638b5bSAmir Goldstein 
187c3638b5bSAmir Goldstein 	if (conn->type != FSNOTIFY_OBJ_TYPE_INODE ||
188c3638b5bSAmir Goldstein 	    want_iref == has_iref)
189c3638b5bSAmir Goldstein 		return NULL;
190c3638b5bSAmir Goldstein 
191c3638b5bSAmir Goldstein 	if (want_iref) {
192c3638b5bSAmir Goldstein 		/* Pin inode if any mark wants inode refcount held */
193c3638b5bSAmir Goldstein 		fsnotify_get_inode_ref(fsnotify_conn_inode(conn));
194c3638b5bSAmir Goldstein 		conn->flags |= FSNOTIFY_CONN_FLAG_HAS_IREF;
195c3638b5bSAmir Goldstein 	} else {
196c3638b5bSAmir Goldstein 		/* Unpin inode after detach of last mark that wanted iref */
197c3638b5bSAmir Goldstein 		inode = fsnotify_conn_inode(conn);
198c3638b5bSAmir Goldstein 		conn->flags &= ~FSNOTIFY_CONN_FLAG_HAS_IREF;
199c3638b5bSAmir Goldstein 	}
200c3638b5bSAmir Goldstein 
201c3638b5bSAmir Goldstein 	return inode;
202c3638b5bSAmir Goldstein }
203c3638b5bSAmir Goldstein 
204c3638b5bSAmir Goldstein static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
2050809ab69SJan Kara {
2060809ab69SJan Kara 	u32 new_mask = 0;
207c3638b5bSAmir Goldstein 	bool want_iref = false;
2080809ab69SJan Kara 	struct fsnotify_mark *mark;
2090809ab69SJan Kara 
21004662cabSJan Kara 	assert_spin_locked(&conn->lock);
211d3bc0fa8SJan Kara 	/* We can get detached connector here when inode is getting unlinked. */
212d3bc0fa8SJan Kara 	if (!fsnotify_valid_obj_type(conn->type))
213c3638b5bSAmir Goldstein 		return NULL;
2146b3f05d2SJan Kara 	hlist_for_each_entry(mark, &conn->list, obj_list) {
215c3638b5bSAmir Goldstein 		if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED))
216c3638b5bSAmir Goldstein 			continue;
2174f0b903dSAmir Goldstein 		new_mask |= fsnotify_calc_mask(mark);
218c3638b5bSAmir Goldstein 		if (conn->type == FSNOTIFY_OBJ_TYPE_INODE &&
219c3638b5bSAmir Goldstein 		    !(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
220c3638b5bSAmir Goldstein 			want_iref = true;
2216b3f05d2SJan Kara 	}
2223ac70bfcSAmir Goldstein 	*fsnotify_conn_mask_p(conn) = new_mask;
223c3638b5bSAmir Goldstein 
224c3638b5bSAmir Goldstein 	return fsnotify_update_iref(conn, want_iref);
225a242677bSJan Kara }
226a242677bSJan Kara 
227a242677bSJan Kara /*
228a242677bSJan Kara  * Calculate mask of events for a list of marks. The caller must make sure
22936f10f55SAmir Goldstein  * connector and connector->obj cannot disappear under us.  Callers achieve
2306b3f05d2SJan Kara  * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
2316b3f05d2SJan Kara  * list.
232a242677bSJan Kara  */
233a242677bSJan Kara void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
234a242677bSJan Kara {
235a242677bSJan Kara 	if (!conn)
236a242677bSJan Kara 		return;
237a242677bSJan Kara 
23804662cabSJan Kara 	spin_lock(&conn->lock);
239a242677bSJan Kara 	__fsnotify_recalc_mask(conn);
24004662cabSJan Kara 	spin_unlock(&conn->lock);
241d6f7b98bSAmir Goldstein 	if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
24236f10f55SAmir Goldstein 		__fsnotify_update_child_dentry_flags(
24336f10f55SAmir Goldstein 					fsnotify_conn_inode(conn));
2440809ab69SJan Kara }
2450809ab69SJan Kara 
24608991e83SJan Kara /* Free all connectors queued for freeing once SRCU period ends */
24708991e83SJan Kara static void fsnotify_connector_destroy_workfn(struct work_struct *work)
24808991e83SJan Kara {
24908991e83SJan Kara 	struct fsnotify_mark_connector *conn, *free;
25008991e83SJan Kara 
25108991e83SJan Kara 	spin_lock(&destroy_lock);
25208991e83SJan Kara 	conn = connector_destroy_list;
25308991e83SJan Kara 	connector_destroy_list = NULL;
25408991e83SJan Kara 	spin_unlock(&destroy_lock);
25508991e83SJan Kara 
25608991e83SJan Kara 	synchronize_srcu(&fsnotify_mark_srcu);
25708991e83SJan Kara 	while (conn) {
25808991e83SJan Kara 		free = conn;
25908991e83SJan Kara 		conn = conn->destroy_next;
26008991e83SJan Kara 		kmem_cache_free(fsnotify_mark_connector_cachep, free);
26108991e83SJan Kara 	}
26208991e83SJan Kara }
26308991e83SJan Kara 
264721fb6fbSJan Kara static void *fsnotify_detach_connector_from_object(
265721fb6fbSJan Kara 					struct fsnotify_mark_connector *conn,
266721fb6fbSJan Kara 					unsigned int *type)
26708991e83SJan Kara {
26808991e83SJan Kara 	struct inode *inode = NULL;
26908991e83SJan Kara 
270721fb6fbSJan Kara 	*type = conn->type;
27136f10f55SAmir Goldstein 	if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED)
27236f10f55SAmir Goldstein 		return NULL;
27336f10f55SAmir Goldstein 
274d6f7b98bSAmir Goldstein 	if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
27536f10f55SAmir Goldstein 		inode = fsnotify_conn_inode(conn);
27608991e83SJan Kara 		inode->i_fsnotify_mask = 0;
277c3638b5bSAmir Goldstein 
278c3638b5bSAmir Goldstein 		/* Unpin inode when detaching from connector */
279c3638b5bSAmir Goldstein 		if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF))
280c3638b5bSAmir Goldstein 			inode = NULL;
281d6f7b98bSAmir Goldstein 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
28236f10f55SAmir Goldstein 		fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0;
2831e6cb723SAmir Goldstein 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) {
2841e6cb723SAmir Goldstein 		fsnotify_conn_sb(conn)->s_fsnotify_mask = 0;
28508991e83SJan Kara 	}
28608991e83SJan Kara 
287d2f277e2SAmir Goldstein 	fsnotify_put_sb_watchers(conn);
28836f10f55SAmir Goldstein 	rcu_assign_pointer(*(conn->obj), NULL);
28936f10f55SAmir Goldstein 	conn->obj = NULL;
29036f10f55SAmir Goldstein 	conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
29136f10f55SAmir Goldstein 
29208991e83SJan Kara 	return inode;
29308991e83SJan Kara }
29408991e83SJan Kara 
2956b3f05d2SJan Kara static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark)
2966b3f05d2SJan Kara {
297054c636eSJan Kara 	struct fsnotify_group *group = mark->group;
298054c636eSJan Kara 
299054c636eSJan Kara 	if (WARN_ON_ONCE(!group))
300054c636eSJan Kara 		return;
301054c636eSJan Kara 	group->ops->free_mark(mark);
302054c636eSJan Kara 	fsnotify_put_group(group);
3036b3f05d2SJan Kara }
3046b3f05d2SJan Kara 
305721fb6fbSJan Kara /* Drop object reference originally held by a connector */
306721fb6fbSJan Kara static void fsnotify_drop_object(unsigned int type, void *objp)
307721fb6fbSJan Kara {
308721fb6fbSJan Kara 	if (!objp)
309721fb6fbSJan Kara 		return;
310721fb6fbSJan Kara 	/* Currently only inode references are passed to be dropped */
311721fb6fbSJan Kara 	if (WARN_ON_ONCE(type != FSNOTIFY_OBJ_TYPE_INODE))
312721fb6fbSJan Kara 		return;
31311fa333bSAmir Goldstein 	fsnotify_put_inode_ref(objp);
314721fb6fbSJan Kara }
315721fb6fbSJan Kara 
3166b3f05d2SJan Kara void fsnotify_put_mark(struct fsnotify_mark *mark)
3178212a609SJan Kara {
318b1da6a51SJan Kara 	struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector);
319721fb6fbSJan Kara 	void *objp = NULL;
320721fb6fbSJan Kara 	unsigned int type = FSNOTIFY_OBJ_TYPE_DETACHED;
32108991e83SJan Kara 	bool free_conn = false;
3228212a609SJan Kara 
3236b3f05d2SJan Kara 	/* Catch marks that were actually never attached to object */
324b1da6a51SJan Kara 	if (!conn) {
325ab97f873SElena Reshetova 		if (refcount_dec_and_test(&mark->refcnt))
3266b3f05d2SJan Kara 			fsnotify_final_mark_destroy(mark);
3276b3f05d2SJan Kara 		return;
3286b3f05d2SJan Kara 	}
3296b3f05d2SJan Kara 
3306b3f05d2SJan Kara 	/*
3316b3f05d2SJan Kara 	 * We have to be careful so that traversals of obj_list under lock can
3326b3f05d2SJan Kara 	 * safely grab mark reference.
3336b3f05d2SJan Kara 	 */
334b1da6a51SJan Kara 	if (!refcount_dec_and_lock(&mark->refcnt, &conn->lock))
3356b3f05d2SJan Kara 		return;
3366b3f05d2SJan Kara 
3378212a609SJan Kara 	hlist_del_init_rcu(&mark->obj_list);
3388212a609SJan Kara 	if (hlist_empty(&conn->list)) {
339721fb6fbSJan Kara 		objp = fsnotify_detach_connector_from_object(conn, &type);
34008991e83SJan Kara 		free_conn = true;
34108991e83SJan Kara 	} else {
342c3638b5bSAmir Goldstein 		objp = __fsnotify_recalc_mask(conn);
343c3638b5bSAmir Goldstein 		type = conn->type;
34408991e83SJan Kara 	}
345b1da6a51SJan Kara 	WRITE_ONCE(mark->connector, NULL);
34604662cabSJan Kara 	spin_unlock(&conn->lock);
3478212a609SJan Kara 
348721fb6fbSJan Kara 	fsnotify_drop_object(type, objp);
3496b3f05d2SJan Kara 
35008991e83SJan Kara 	if (free_conn) {
35108991e83SJan Kara 		spin_lock(&destroy_lock);
35208991e83SJan Kara 		conn->destroy_next = connector_destroy_list;
35308991e83SJan Kara 		connector_destroy_list = conn;
35408991e83SJan Kara 		spin_unlock(&destroy_lock);
35508991e83SJan Kara 		queue_work(system_unbound_wq, &connector_reaper_work);
35608991e83SJan Kara 	}
3576b3f05d2SJan Kara 	/*
3586b3f05d2SJan Kara 	 * Note that we didn't update flags telling whether inode cares about
3596b3f05d2SJan Kara 	 * what's happening with children. We update these flags from
3606b3f05d2SJan Kara 	 * __fsnotify_parent() lazily when next event happens on one of our
3616b3f05d2SJan Kara 	 * children.
3626b3f05d2SJan Kara 	 */
3636b3f05d2SJan Kara 	spin_lock(&destroy_lock);
3646b3f05d2SJan Kara 	list_add(&mark->g_list, &destroy_list);
3656b3f05d2SJan Kara 	spin_unlock(&destroy_lock);
3666b3f05d2SJan Kara 	queue_delayed_work(system_unbound_wq, &reaper_work,
3676b3f05d2SJan Kara 			   FSNOTIFY_REAPER_DELAY);
3688212a609SJan Kara }
369b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_put_mark);
3708212a609SJan Kara 
37124c20305SMiklos Szeredi /*
37224c20305SMiklos Szeredi  * Get mark reference when we found the mark via lockless traversal of object
37324c20305SMiklos Szeredi  * list. Mark can be already removed from the list by now and on its way to be
37424c20305SMiklos Szeredi  * destroyed once SRCU period ends.
37524c20305SMiklos Szeredi  *
37624c20305SMiklos Szeredi  * Also pin the group so it doesn't disappear under us.
37724c20305SMiklos Szeredi  */
37824c20305SMiklos Szeredi static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark)
379abc77577SJan Kara {
38024c20305SMiklos Szeredi 	if (!mark)
38124c20305SMiklos Szeredi 		return true;
382abc77577SJan Kara 
383ab97f873SElena Reshetova 	if (refcount_inc_not_zero(&mark->refcnt)) {
3849a31d7adSMiklos Szeredi 		spin_lock(&mark->lock);
3859a31d7adSMiklos Szeredi 		if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) {
3869a31d7adSMiklos Szeredi 			/* mark is attached, group is still alive then */
3879a31d7adSMiklos Szeredi 			atomic_inc(&mark->group->user_waits);
3889a31d7adSMiklos Szeredi 			spin_unlock(&mark->lock);
38924c20305SMiklos Szeredi 			return true;
3909a31d7adSMiklos Szeredi 		}
3919a31d7adSMiklos Szeredi 		spin_unlock(&mark->lock);
3929a31d7adSMiklos Szeredi 		fsnotify_put_mark(mark);
3939a31d7adSMiklos Szeredi 	}
39424c20305SMiklos Szeredi 	return false;
39524c20305SMiklos Szeredi }
39624c20305SMiklos Szeredi 
39724c20305SMiklos Szeredi /*
39824c20305SMiklos Szeredi  * Puts marks and wakes up group destruction if necessary.
39924c20305SMiklos Szeredi  *
40024c20305SMiklos Szeredi  * Pairs with fsnotify_get_mark_safe()
40124c20305SMiklos Szeredi  */
40224c20305SMiklos Szeredi static void fsnotify_put_mark_wake(struct fsnotify_mark *mark)
40324c20305SMiklos Szeredi {
40424c20305SMiklos Szeredi 	if (mark) {
40524c20305SMiklos Szeredi 		struct fsnotify_group *group = mark->group;
40624c20305SMiklos Szeredi 
40724c20305SMiklos Szeredi 		fsnotify_put_mark(mark);
40824c20305SMiklos Szeredi 		/*
40924c20305SMiklos Szeredi 		 * We abuse notification_waitq on group shutdown for waiting for
41024c20305SMiklos Szeredi 		 * all marks pinned when waiting for userspace.
41124c20305SMiklos Szeredi 		 */
41224c20305SMiklos Szeredi 		if (atomic_dec_and_test(&group->user_waits) && group->shutdown)
41324c20305SMiklos Szeredi 			wake_up(&group->notification_waitq);
41424c20305SMiklos Szeredi 	}
41524c20305SMiklos Szeredi }
41624c20305SMiklos Szeredi 
41724c20305SMiklos Szeredi bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info)
41800e0afb6SJules Irenge 	__releases(&fsnotify_mark_srcu)
41924c20305SMiklos Szeredi {
42047d9c7ccSAmir Goldstein 	int type;
42147d9c7ccSAmir Goldstein 
4221c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
423abc77577SJan Kara 		/* This can fail if mark is being removed */
42400e0afb6SJules Irenge 		if (!fsnotify_get_mark_safe(iter_info->marks[type])) {
42500e0afb6SJules Irenge 			__release(&fsnotify_mark_srcu);
42647d9c7ccSAmir Goldstein 			goto fail;
427abc77577SJan Kara 		}
42800e0afb6SJules Irenge 	}
429abc77577SJan Kara 
430abc77577SJan Kara 	/*
431abc77577SJan Kara 	 * Now that both marks are pinned by refcount in the inode / vfsmount
432abc77577SJan Kara 	 * lists, we can drop SRCU lock, and safely resume the list iteration
433abc77577SJan Kara 	 * once userspace returns.
434abc77577SJan Kara 	 */
435abc77577SJan Kara 	srcu_read_unlock(&fsnotify_mark_srcu, iter_info->srcu_idx);
436abc77577SJan Kara 
437abc77577SJan Kara 	return true;
43847d9c7ccSAmir Goldstein 
43947d9c7ccSAmir Goldstein fail:
44047d9c7ccSAmir Goldstein 	for (type--; type >= 0; type--)
44147d9c7ccSAmir Goldstein 		fsnotify_put_mark_wake(iter_info->marks[type]);
44247d9c7ccSAmir Goldstein 	return false;
443abc77577SJan Kara }
444abc77577SJan Kara 
445abc77577SJan Kara void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info)
44600e0afb6SJules Irenge 	__acquires(&fsnotify_mark_srcu)
447abc77577SJan Kara {
44847d9c7ccSAmir Goldstein 	int type;
44947d9c7ccSAmir Goldstein 
450abc77577SJan Kara 	iter_info->srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
4511c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type)
45247d9c7ccSAmir Goldstein 		fsnotify_put_mark_wake(iter_info->marks[type]);
453abc77577SJan Kara }
454abc77577SJan Kara 
4555444e298SEric Paris /*
4566b3f05d2SJan Kara  * Mark mark as detached, remove it from group list. Mark still stays in object
4576b3f05d2SJan Kara  * list until its last reference is dropped. Note that we rely on mark being
4586b3f05d2SJan Kara  * removed from group list before corresponding reference to it is dropped. In
4596b3f05d2SJan Kara  * particular we rely on mark->connector being valid while we hold
4606b3f05d2SJan Kara  * group->mark_mutex if we found the mark through g_list.
4614712e722SJan Kara  *
46211375145SJan Kara  * Must be called with group->mark_mutex held. The caller must either hold
46311375145SJan Kara  * reference to the mark or be protected by fsnotify_mark_srcu.
4645444e298SEric Paris  */
4654712e722SJan Kara void fsnotify_detach_mark(struct fsnotify_mark *mark)
4665444e298SEric Paris {
46743b245a7SAmir Goldstein 	fsnotify_group_assert_locked(mark->group);
46811375145SJan Kara 	WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) &&
469ab97f873SElena Reshetova 		     refcount_read(&mark->refcnt) < 1 +
47011375145SJan Kara 			!!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED));
471d5a335b8SLino Sanfilippo 
472104d06f0SLino Sanfilippo 	spin_lock(&mark->lock);
473700307a2SEric Paris 	/* something else already called this function on this mark */
4744712e722SJan Kara 	if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
4755444e298SEric Paris 		spin_unlock(&mark->lock);
476e2a29943SLino Sanfilippo 		return;
4775444e298SEric Paris 	}
4784712e722SJan Kara 	mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
4795444e298SEric Paris 	list_del_init(&mark->g_list);
4805444e298SEric Paris 	spin_unlock(&mark->lock);
481d5a335b8SLino Sanfilippo 
48211375145SJan Kara 	/* Drop mark reference acquired in fsnotify_add_mark_locked() */
48311375145SJan Kara 	fsnotify_put_mark(mark);
4844712e722SJan Kara }
4854712e722SJan Kara 
4864712e722SJan Kara /*
48711375145SJan Kara  * Free fsnotify mark. The mark is actually only marked as being freed.  The
48811375145SJan Kara  * freeing is actually happening only once last reference to the mark is
48911375145SJan Kara  * dropped from a workqueue which first waits for srcu period end.
49035e48176SJan Kara  *
49111375145SJan Kara  * Caller must have a reference to the mark or be protected by
49211375145SJan Kara  * fsnotify_mark_srcu.
4934712e722SJan Kara  */
49411375145SJan Kara void fsnotify_free_mark(struct fsnotify_mark *mark)
4954712e722SJan Kara {
4964712e722SJan Kara 	struct fsnotify_group *group = mark->group;
4974712e722SJan Kara 
4984712e722SJan Kara 	spin_lock(&mark->lock);
4994712e722SJan Kara 	/* something else already called this function on this mark */
5004712e722SJan Kara 	if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
5014712e722SJan Kara 		spin_unlock(&mark->lock);
50211375145SJan Kara 		return;
5034712e722SJan Kara 	}
5044712e722SJan Kara 	mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
5054712e722SJan Kara 	spin_unlock(&mark->lock);
5065444e298SEric Paris 
507d725e66cSLinus Torvalds 	/*
508d725e66cSLinus Torvalds 	 * Some groups like to know that marks are being freed.  This is a
509d725e66cSLinus Torvalds 	 * callback to the group function to let it know that this mark
510d725e66cSLinus Torvalds 	 * is being freed.
511d725e66cSLinus Torvalds 	 */
512d725e66cSLinus Torvalds 	if (group->ops->freeing_mark)
513d725e66cSLinus Torvalds 		group->ops->freeing_mark(mark, group);
514d5a335b8SLino Sanfilippo }
515d5a335b8SLino Sanfilippo 
516d5a335b8SLino Sanfilippo void fsnotify_destroy_mark(struct fsnotify_mark *mark,
517d5a335b8SLino Sanfilippo 			   struct fsnotify_group *group)
518d5a335b8SLino Sanfilippo {
51943b245a7SAmir Goldstein 	fsnotify_group_lock(group);
5204712e722SJan Kara 	fsnotify_detach_mark(mark);
52143b245a7SAmir Goldstein 	fsnotify_group_unlock(group);
5224712e722SJan Kara 	fsnotify_free_mark(mark);
5235444e298SEric Paris }
524b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
5255444e298SEric Paris 
5265444e298SEric Paris /*
5278edc6e16SJan Kara  * Sorting function for lists of fsnotify marks.
5288edc6e16SJan Kara  *
5298edc6e16SJan Kara  * Fanotify supports different notification classes (reflected as priority of
5308edc6e16SJan Kara  * notification group). Events shall be passed to notification groups in
5318edc6e16SJan Kara  * decreasing priority order. To achieve this marks in notification lists for
5328edc6e16SJan Kara  * inodes and vfsmounts are sorted so that priorities of corresponding groups
5338edc6e16SJan Kara  * are descending.
5348edc6e16SJan Kara  *
5358edc6e16SJan Kara  * Furthermore correct handling of the ignore mask requires processing inode
5368edc6e16SJan Kara  * and vfsmount marks of each group together. Using the group address as
5378edc6e16SJan Kara  * further sort criterion provides a unique sorting order and thus we can
5388edc6e16SJan Kara  * merge inode and vfsmount lists of marks in linear time and find groups
5398edc6e16SJan Kara  * present in both lists.
5408edc6e16SJan Kara  *
5418edc6e16SJan Kara  * A return value of 1 signifies that b has priority over a.
5428edc6e16SJan Kara  * A return value of 0 signifies that the two marks have to be handled together.
5438edc6e16SJan Kara  * A return value of -1 signifies that a has priority over b.
5448edc6e16SJan Kara  */
5458edc6e16SJan Kara int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
5468edc6e16SJan Kara {
5478edc6e16SJan Kara 	if (a == b)
5488edc6e16SJan Kara 		return 0;
5498edc6e16SJan Kara 	if (!a)
5508edc6e16SJan Kara 		return 1;
5518edc6e16SJan Kara 	if (!b)
5528edc6e16SJan Kara 		return -1;
5538edc6e16SJan Kara 	if (a->priority < b->priority)
5548edc6e16SJan Kara 		return 1;
5558edc6e16SJan Kara 	if (a->priority > b->priority)
5568edc6e16SJan Kara 		return -1;
5578edc6e16SJan Kara 	if (a < b)
5588edc6e16SJan Kara 		return 1;
5598edc6e16SJan Kara 	return -1;
5608edc6e16SJan Kara }
5618edc6e16SJan Kara 
5629b6e5434SAmir Goldstein static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
5637232522eSAmir Goldstein 					       unsigned int obj_type)
5649dd813c1SJan Kara {
5659dd813c1SJan Kara 	struct fsnotify_mark_connector *conn;
5669dd813c1SJan Kara 
567755b5bc6SJan Kara 	conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
5689dd813c1SJan Kara 	if (!conn)
5699dd813c1SJan Kara 		return -ENOMEM;
57004662cabSJan Kara 	spin_lock_init(&conn->lock);
5719dd813c1SJan Kara 	INIT_HLIST_HEAD(&conn->list);
572c3638b5bSAmir Goldstein 	conn->flags = 0;
573ad69cd99SAmir Goldstein 	conn->type = obj_type;
57436f10f55SAmir Goldstein 	conn->obj = connp;
57509ddbe69SAmir Goldstein 
5769dd813c1SJan Kara 	/*
57704662cabSJan Kara 	 * cmpxchg() provides the barrier so that readers of *connp can see
57804662cabSJan Kara 	 * only initialized structure
5799dd813c1SJan Kara 	 */
58004662cabSJan Kara 	if (cmpxchg(connp, NULL, conn)) {
58104662cabSJan Kara 		/* Someone else created list structure for us */
582755b5bc6SJan Kara 		kmem_cache_free(fsnotify_mark_connector_cachep, conn);
583d2f277e2SAmir Goldstein 		return 0;
58404662cabSJan Kara 	}
5859dd813c1SJan Kara 
586d2f277e2SAmir Goldstein 	fsnotify_get_sb_watchers(conn);
5879dd813c1SJan Kara 	return 0;
5889dd813c1SJan Kara }
5899dd813c1SJan Kara 
5909dd813c1SJan Kara /*
59108991e83SJan Kara  * Get mark connector, make sure it is alive and return with its lock held.
59208991e83SJan Kara  * This is for users that get connector pointer from inode or mount. Users that
59308991e83SJan Kara  * hold reference to a mark on the list may directly lock connector->lock as
59408991e83SJan Kara  * they are sure list cannot go away under them.
59508991e83SJan Kara  */
59608991e83SJan Kara static struct fsnotify_mark_connector *fsnotify_grab_connector(
5979b6e5434SAmir Goldstein 						fsnotify_connp_t *connp)
59808991e83SJan Kara {
59908991e83SJan Kara 	struct fsnotify_mark_connector *conn;
60008991e83SJan Kara 	int idx;
60108991e83SJan Kara 
60208991e83SJan Kara 	idx = srcu_read_lock(&fsnotify_mark_srcu);
60308991e83SJan Kara 	conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
60408991e83SJan Kara 	if (!conn)
60508991e83SJan Kara 		goto out;
60608991e83SJan Kara 	spin_lock(&conn->lock);
607d6f7b98bSAmir Goldstein 	if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) {
60808991e83SJan Kara 		spin_unlock(&conn->lock);
60908991e83SJan Kara 		srcu_read_unlock(&fsnotify_mark_srcu, idx);
61008991e83SJan Kara 		return NULL;
61108991e83SJan Kara 	}
61208991e83SJan Kara out:
61308991e83SJan Kara 	srcu_read_unlock(&fsnotify_mark_srcu, idx);
61408991e83SJan Kara 	return conn;
61508991e83SJan Kara }
61608991e83SJan Kara 
61708991e83SJan Kara /*
6189dd813c1SJan Kara  * Add mark into proper place in given list of marks. These marks may be used
6199dd813c1SJan Kara  * for the fsnotify backend to determine which event types should be delivered
6209dd813c1SJan Kara  * to which group and for which inodes. These marks are ordered according to
6219dd813c1SJan Kara  * priority, highest number first, and then by the group's location in memory.
6229dd813c1SJan Kara  */
623755b5bc6SJan Kara static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
624ad69cd99SAmir Goldstein 				  fsnotify_connp_t *connp,
6257232522eSAmir Goldstein 				  unsigned int obj_type, int add_flags)
6260809ab69SJan Kara {
6270809ab69SJan Kara 	struct fsnotify_mark *lmark, *last = NULL;
6289dd813c1SJan Kara 	struct fsnotify_mark_connector *conn;
6290809ab69SJan Kara 	int cmp;
630755b5bc6SJan Kara 	int err = 0;
631755b5bc6SJan Kara 
632ad69cd99SAmir Goldstein 	if (WARN_ON(!fsnotify_valid_obj_type(obj_type)))
633755b5bc6SJan Kara 		return -EINVAL;
63477115225SAmir Goldstein 
63508991e83SJan Kara restart:
63608991e83SJan Kara 	spin_lock(&mark->lock);
63708991e83SJan Kara 	conn = fsnotify_grab_connector(connp);
63808991e83SJan Kara 	if (!conn) {
63908991e83SJan Kara 		spin_unlock(&mark->lock);
6407232522eSAmir Goldstein 		err = fsnotify_attach_connector_to_object(connp, obj_type);
6419dd813c1SJan Kara 		if (err)
6429dd813c1SJan Kara 			return err;
64308991e83SJan Kara 		goto restart;
6449dd813c1SJan Kara 	}
6450809ab69SJan Kara 
6460809ab69SJan Kara 	/* is mark the first mark? */
6479dd813c1SJan Kara 	if (hlist_empty(&conn->list)) {
6489dd813c1SJan Kara 		hlist_add_head_rcu(&mark->obj_list, &conn->list);
64986ffe245SJan Kara 		goto added;
6500809ab69SJan Kara 	}
6510809ab69SJan Kara 
6520809ab69SJan Kara 	/* should mark be in the middle of the current list? */
6539dd813c1SJan Kara 	hlist_for_each_entry(lmark, &conn->list, obj_list) {
6540809ab69SJan Kara 		last = lmark;
6550809ab69SJan Kara 
6566b3f05d2SJan Kara 		if ((lmark->group == mark->group) &&
6576b3f05d2SJan Kara 		    (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) &&
658f3010343SAmir Goldstein 		    !(mark->group->flags & FSNOTIFY_GROUP_DUPS)) {
659755b5bc6SJan Kara 			err = -EEXIST;
660755b5bc6SJan Kara 			goto out_err;
661755b5bc6SJan Kara 		}
6620809ab69SJan Kara 
6630809ab69SJan Kara 		cmp = fsnotify_compare_groups(lmark->group, mark->group);
6640809ab69SJan Kara 		if (cmp >= 0) {
6650809ab69SJan Kara 			hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
66686ffe245SJan Kara 			goto added;
6670809ab69SJan Kara 		}
6680809ab69SJan Kara 	}
6690809ab69SJan Kara 
6700809ab69SJan Kara 	BUG_ON(last == NULL);
6710809ab69SJan Kara 	/* mark should be the last entry.  last is the current last entry */
6720809ab69SJan Kara 	hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
67386ffe245SJan Kara added:
67411a6f8e2SJan Kara 	/*
67511a6f8e2SJan Kara 	 * Since connector is attached to object using cmpxchg() we are
67611a6f8e2SJan Kara 	 * guaranteed that connector initialization is fully visible by anyone
67711a6f8e2SJan Kara 	 * seeing mark->connector set.
67811a6f8e2SJan Kara 	 */
679b1da6a51SJan Kara 	WRITE_ONCE(mark->connector, conn);
680755b5bc6SJan Kara out_err:
68104662cabSJan Kara 	spin_unlock(&conn->lock);
682755b5bc6SJan Kara 	spin_unlock(&mark->lock);
683755b5bc6SJan Kara 	return err;
6840809ab69SJan Kara }
6850809ab69SJan Kara 
6868edc6e16SJan Kara /*
6875444e298SEric Paris  * Attach an initialized mark to a given group and fs object.
6885444e298SEric Paris  * These marks may be used for the fsnotify backend to determine which
6895444e298SEric Paris  * event types should be delivered to which group.
6905444e298SEric Paris  */
691b812a9f5SAmir Goldstein int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
692ad69cd99SAmir Goldstein 			     fsnotify_connp_t *connp, unsigned int obj_type,
6937232522eSAmir Goldstein 			     int add_flags)
6945444e298SEric Paris {
6957b129323SJan Kara 	struct fsnotify_group *group = mark->group;
6965444e298SEric Paris 	int ret = 0;
6975444e298SEric Paris 
69843b245a7SAmir Goldstein 	fsnotify_group_assert_locked(group);
6995444e298SEric Paris 
7005444e298SEric Paris 	/*
7015444e298SEric Paris 	 * LOCKING ORDER!!!!
702986ab098SLino Sanfilippo 	 * group->mark_mutex
703104d06f0SLino Sanfilippo 	 * mark->lock
70404662cabSJan Kara 	 * mark->connector->lock
7055444e298SEric Paris 	 */
706104d06f0SLino Sanfilippo 	spin_lock(&mark->lock);
7074712e722SJan Kara 	mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
708700307a2SEric Paris 
7095444e298SEric Paris 	list_add(&mark->g_list, &group->marks_list);
7106b3f05d2SJan Kara 	fsnotify_get_mark(mark); /* for g_list */
7115444e298SEric Paris 	spin_unlock(&mark->lock);
7125444e298SEric Paris 
7137232522eSAmir Goldstein 	ret = fsnotify_add_mark_list(mark, connp, obj_type, add_flags);
714755b5bc6SJan Kara 	if (ret)
715755b5bc6SJan Kara 		goto err;
716755b5bc6SJan Kara 
717a242677bSJan Kara 	fsnotify_recalc_mask(mark->connector);
7185444e298SEric Paris 
7195444e298SEric Paris 	return ret;
7205444e298SEric Paris err:
7219cf90cefSJan Kara 	spin_lock(&mark->lock);
72211375145SJan Kara 	mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE |
72311375145SJan Kara 			 FSNOTIFY_MARK_FLAG_ATTACHED);
7245444e298SEric Paris 	list_del_init(&mark->g_list);
7259cf90cefSJan Kara 	spin_unlock(&mark->lock);
7265444e298SEric Paris 
72711375145SJan Kara 	fsnotify_put_mark(mark);
7285444e298SEric Paris 	return ret;
7295444e298SEric Paris }
7305444e298SEric Paris 
731b812a9f5SAmir Goldstein int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
7327232522eSAmir Goldstein 		      unsigned int obj_type, int add_flags)
733d5a335b8SLino Sanfilippo {
734d5a335b8SLino Sanfilippo 	int ret;
7357b129323SJan Kara 	struct fsnotify_group *group = mark->group;
7367b129323SJan Kara 
73743b245a7SAmir Goldstein 	fsnotify_group_lock(group);
7387232522eSAmir Goldstein 	ret = fsnotify_add_mark_locked(mark, connp, obj_type, add_flags);
73943b245a7SAmir Goldstein 	fsnotify_group_unlock(group);
740d5a335b8SLino Sanfilippo 	return ret;
741d5a335b8SLino Sanfilippo }
742b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_add_mark);
743d5a335b8SLino Sanfilippo 
7445444e298SEric Paris /*
7450809ab69SJan Kara  * Given a list of marks, find the mark associated with given group. If found
7460809ab69SJan Kara  * take a reference to that mark and return it, else return NULL.
7470809ab69SJan Kara  */
7489b6e5434SAmir Goldstein struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
7490809ab69SJan Kara 					 struct fsnotify_group *group)
7500809ab69SJan Kara {
75108991e83SJan Kara 	struct fsnotify_mark_connector *conn;
7520809ab69SJan Kara 	struct fsnotify_mark *mark;
7530809ab69SJan Kara 
75408991e83SJan Kara 	conn = fsnotify_grab_connector(connp);
7559dd813c1SJan Kara 	if (!conn)
7569dd813c1SJan Kara 		return NULL;
7579dd813c1SJan Kara 
7589dd813c1SJan Kara 	hlist_for_each_entry(mark, &conn->list, obj_list) {
7596b3f05d2SJan Kara 		if (mark->group == group &&
7606b3f05d2SJan Kara 		    (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
7610809ab69SJan Kara 			fsnotify_get_mark(mark);
76204662cabSJan Kara 			spin_unlock(&conn->lock);
7630809ab69SJan Kara 			return mark;
7640809ab69SJan Kara 		}
7650809ab69SJan Kara 	}
76604662cabSJan Kara 	spin_unlock(&conn->lock);
7670809ab69SJan Kara 	return NULL;
7680809ab69SJan Kara }
769b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_find_mark);
7700809ab69SJan Kara 
771d6f7b98bSAmir Goldstein /* Clear any marks in a group with given type mask */
77218f2e0d3SJan Kara void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
773ad69cd99SAmir Goldstein 				   unsigned int obj_type)
7745444e298SEric Paris {
7755444e298SEric Paris 	struct fsnotify_mark *lmark, *mark;
7768f2f3eb5SJan Kara 	LIST_HEAD(to_free);
7772e37c6caSJan Kara 	struct list_head *head = &to_free;
7785444e298SEric Paris 
7792e37c6caSJan Kara 	/* Skip selection step if we want to clear all marks. */
780ad69cd99SAmir Goldstein 	if (obj_type == FSNOTIFY_OBJ_TYPE_ANY) {
7812e37c6caSJan Kara 		head = &group->marks_list;
7822e37c6caSJan Kara 		goto clear;
7832e37c6caSJan Kara 	}
7848f2f3eb5SJan Kara 	/*
7858f2f3eb5SJan Kara 	 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
7868f2f3eb5SJan Kara 	 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
7878f2f3eb5SJan Kara 	 * to_free list so we have to use mark_mutex even when accessing that
7888f2f3eb5SJan Kara 	 * list. And freeing mark requires us to drop mark_mutex. So we can
7898f2f3eb5SJan Kara 	 * reliably free only the first mark in the list. That's why we first
7908f2f3eb5SJan Kara 	 * move marks to free to to_free list in one go and then free marks in
7918f2f3eb5SJan Kara 	 * to_free list one by one.
7928f2f3eb5SJan Kara 	 */
79343b245a7SAmir Goldstein 	fsnotify_group_lock(group);
7945444e298SEric Paris 	list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
795ad69cd99SAmir Goldstein 		if (mark->connector->type == obj_type)
7968f2f3eb5SJan Kara 			list_move(&mark->g_list, &to_free);
7974d92604cSEric Paris 	}
79843b245a7SAmir Goldstein 	fsnotify_group_unlock(group);
7998f2f3eb5SJan Kara 
8002e37c6caSJan Kara clear:
8018f2f3eb5SJan Kara 	while (1) {
80243b245a7SAmir Goldstein 		fsnotify_group_lock(group);
8032e37c6caSJan Kara 		if (list_empty(head)) {
80443b245a7SAmir Goldstein 			fsnotify_group_unlock(group);
8058f2f3eb5SJan Kara 			break;
8068f2f3eb5SJan Kara 		}
8072e37c6caSJan Kara 		mark = list_first_entry(head, struct fsnotify_mark, g_list);
8088f2f3eb5SJan Kara 		fsnotify_get_mark(mark);
8094712e722SJan Kara 		fsnotify_detach_mark(mark);
81043b245a7SAmir Goldstein 		fsnotify_group_unlock(group);
8114712e722SJan Kara 		fsnotify_free_mark(mark);
8128f2f3eb5SJan Kara 		fsnotify_put_mark(mark);
8138f2f3eb5SJan Kara 	}
8145444e298SEric Paris }
8155444e298SEric Paris 
8169b6e5434SAmir Goldstein /* Destroy all marks attached to an object via connector */
8179b6e5434SAmir Goldstein void fsnotify_destroy_marks(fsnotify_connp_t *connp)
8180810b4f9SJan Kara {
81908991e83SJan Kara 	struct fsnotify_mark_connector *conn;
8206b3f05d2SJan Kara 	struct fsnotify_mark *mark, *old_mark = NULL;
821721fb6fbSJan Kara 	void *objp;
822721fb6fbSJan Kara 	unsigned int type;
8230810b4f9SJan Kara 
8246b3f05d2SJan Kara 	conn = fsnotify_grab_connector(connp);
8256b3f05d2SJan Kara 	if (!conn)
8266b3f05d2SJan Kara 		return;
8270810b4f9SJan Kara 	/*
8280810b4f9SJan Kara 	 * We have to be careful since we can race with e.g.
8296b3f05d2SJan Kara 	 * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the
8306b3f05d2SJan Kara 	 * list can get modified. However we are holding mark reference and
8316b3f05d2SJan Kara 	 * thus our mark cannot be removed from obj_list so we can continue
8326b3f05d2SJan Kara 	 * iteration after regaining conn->lock.
8330810b4f9SJan Kara 	 */
8346b3f05d2SJan Kara 	hlist_for_each_entry(mark, &conn->list, obj_list) {
8350810b4f9SJan Kara 		fsnotify_get_mark(mark);
83604662cabSJan Kara 		spin_unlock(&conn->lock);
8376b3f05d2SJan Kara 		if (old_mark)
8386b3f05d2SJan Kara 			fsnotify_put_mark(old_mark);
8396b3f05d2SJan Kara 		old_mark = mark;
8400810b4f9SJan Kara 		fsnotify_destroy_mark(mark, mark->group);
8416b3f05d2SJan Kara 		spin_lock(&conn->lock);
8420810b4f9SJan Kara 	}
8436b3f05d2SJan Kara 	/*
8446b3f05d2SJan Kara 	 * Detach list from object now so that we don't pin inode until all
8456b3f05d2SJan Kara 	 * mark references get dropped. It would lead to strange results such
8466b3f05d2SJan Kara 	 * as delaying inode deletion or blocking unmount.
8476b3f05d2SJan Kara 	 */
848721fb6fbSJan Kara 	objp = fsnotify_detach_connector_from_object(conn, &type);
8496b3f05d2SJan Kara 	spin_unlock(&conn->lock);
8506b3f05d2SJan Kara 	if (old_mark)
8516b3f05d2SJan Kara 		fsnotify_put_mark(old_mark);
852721fb6fbSJan Kara 	fsnotify_drop_object(type, objp);
8530810b4f9SJan Kara }
8540810b4f9SJan Kara 
8555444e298SEric Paris /*
8565444e298SEric Paris  * Nothing fancy, just initialize lists and locks and counters.
8575444e298SEric Paris  */
8585444e298SEric Paris void fsnotify_init_mark(struct fsnotify_mark *mark,
859054c636eSJan Kara 			struct fsnotify_group *group)
8605444e298SEric Paris {
861ba643f04SEric Paris 	memset(mark, 0, sizeof(*mark));
8625444e298SEric Paris 	spin_lock_init(&mark->lock);
863ab97f873SElena Reshetova 	refcount_set(&mark->refcnt, 1);
8647b129323SJan Kara 	fsnotify_get_group(group);
8657b129323SJan Kara 	mark->group = group;
866b1da6a51SJan Kara 	WRITE_ONCE(mark->connector, NULL);
8675444e298SEric Paris }
868b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_init_mark);
86913d34ac6SJeff Layton 
87035e48176SJan Kara /*
87135e48176SJan Kara  * Destroy all marks in destroy_list, waits for SRCU period to finish before
87235e48176SJan Kara  * actually freeing marks.
87335e48176SJan Kara  */
874f09b04a0SJan Kara static void fsnotify_mark_destroy_workfn(struct work_struct *work)
87513d34ac6SJeff Layton {
87613d34ac6SJeff Layton 	struct fsnotify_mark *mark, *next;
87713d34ac6SJeff Layton 	struct list_head private_destroy_list;
87813d34ac6SJeff Layton 
87913d34ac6SJeff Layton 	spin_lock(&destroy_lock);
88013d34ac6SJeff Layton 	/* exchange the list head */
88113d34ac6SJeff Layton 	list_replace_init(&destroy_list, &private_destroy_list);
88213d34ac6SJeff Layton 	spin_unlock(&destroy_lock);
88313d34ac6SJeff Layton 
88413d34ac6SJeff Layton 	synchronize_srcu(&fsnotify_mark_srcu);
88513d34ac6SJeff Layton 
88613d34ac6SJeff Layton 	list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
88713d34ac6SJeff Layton 		list_del_init(&mark->g_list);
8886b3f05d2SJan Kara 		fsnotify_final_mark_destroy(mark);
88913d34ac6SJeff Layton 	}
89013d34ac6SJeff Layton }
89135e48176SJan Kara 
892f09b04a0SJan Kara /* Wait for all marks queued for destruction to be actually destroyed */
893f09b04a0SJan Kara void fsnotify_wait_marks_destroyed(void)
89435e48176SJan Kara {
895f09b04a0SJan Kara 	flush_delayed_work(&reaper_work);
89635e48176SJan Kara }
897b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_wait_marks_destroyed);
898