xref: /linux/fs/notify/mark.c (revision be602cde657ee43d23adbf309be6d700d0106dc9)
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 
100f115815dSAmir Goldstein static fsnotify_connp_t *fsnotify_object_connp(void *obj,
101f115815dSAmir Goldstein 				enum fsnotify_obj_type obj_type)
102f115815dSAmir Goldstein {
103f115815dSAmir Goldstein 	switch (obj_type) {
104f115815dSAmir Goldstein 	case FSNOTIFY_OBJ_TYPE_INODE:
105f115815dSAmir Goldstein 		return &((struct inode *)obj)->i_fsnotify_marks;
106f115815dSAmir Goldstein 	case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
107f115815dSAmir Goldstein 		return &real_mount(obj)->mnt_fsnotify_marks;
108f115815dSAmir Goldstein 	case FSNOTIFY_OBJ_TYPE_SB:
10907a3b8d0SAmir Goldstein 		return fsnotify_sb_marks(obj);
110f115815dSAmir Goldstein 	default:
111f115815dSAmir Goldstein 		return NULL;
112f115815dSAmir Goldstein 	}
113f115815dSAmir Goldstein }
114f115815dSAmir 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 
131*35ceae44SJan Kara 	return READ_ONCE(*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 
157c9d4603bSAmir Goldstein /*
158c9d4603bSAmir Goldstein  * Grab or drop watched objects reference depending on whether the connector
159c9d4603bSAmir Goldstein  * is attached and has any marks attached.
160c9d4603bSAmir Goldstein  */
161c9d4603bSAmir Goldstein static void fsnotify_update_sb_watchers(struct super_block *sb,
162c9d4603bSAmir Goldstein 					struct fsnotify_mark_connector *conn)
163d2f277e2SAmir Goldstein {
164a5e57b4dSAmir Goldstein 	struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb);
165c9d4603bSAmir Goldstein 	bool is_watched = conn->flags & FSNOTIFY_CONN_FLAG_IS_WATCHED;
166a5e57b4dSAmir Goldstein 	struct fsnotify_mark *first_mark = NULL;
167a5e57b4dSAmir Goldstein 	unsigned int highest_prio = 0;
168d2f277e2SAmir Goldstein 
169a5e57b4dSAmir Goldstein 	if (conn->obj)
170a5e57b4dSAmir Goldstein 		first_mark = hlist_entry_safe(conn->list.first,
171a5e57b4dSAmir Goldstein 					      struct fsnotify_mark, obj_list);
172a5e57b4dSAmir Goldstein 	if (first_mark)
173a5e57b4dSAmir Goldstein 		highest_prio = first_mark->group->priority;
174a5e57b4dSAmir Goldstein 	if (WARN_ON(highest_prio >= __FSNOTIFY_PRIO_NUM))
175a5e57b4dSAmir Goldstein 		highest_prio = 0;
176a5e57b4dSAmir Goldstein 
177a5e57b4dSAmir Goldstein 	/*
178a5e57b4dSAmir Goldstein 	 * If the highest priority of group watching this object is prio,
179a5e57b4dSAmir Goldstein 	 * then watched object has a reference on counters [0..prio].
180a5e57b4dSAmir Goldstein 	 * Update priority >= 1 watched objects counters.
181a5e57b4dSAmir Goldstein 	 */
182a5e57b4dSAmir Goldstein 	for (unsigned int p = conn->prio + 1; p <= highest_prio; p++)
183a5e57b4dSAmir Goldstein 		atomic_long_inc(&sbinfo->watched_objects[p]);
184a5e57b4dSAmir Goldstein 	for (unsigned int p = conn->prio; p > highest_prio; p--)
185a5e57b4dSAmir Goldstein 		atomic_long_dec(&sbinfo->watched_objects[p]);
186a5e57b4dSAmir Goldstein 	conn->prio = highest_prio;
187a5e57b4dSAmir Goldstein 
188a5e57b4dSAmir Goldstein 	/* Update priority >= 0 (a.k.a total) watched objects counter */
189a5e57b4dSAmir Goldstein 	BUILD_BUG_ON(FSNOTIFY_PRIO_NORMAL != 0);
190a5e57b4dSAmir Goldstein 	if (first_mark && !is_watched) {
191c9d4603bSAmir Goldstein 		conn->flags |= FSNOTIFY_CONN_FLAG_IS_WATCHED;
192d2f277e2SAmir Goldstein 		fsnotify_get_sb_watched_objects(sb);
193a5e57b4dSAmir Goldstein 	} else if (!first_mark && is_watched) {
194c9d4603bSAmir Goldstein 		conn->flags &= ~FSNOTIFY_CONN_FLAG_IS_WATCHED;
195d2f277e2SAmir Goldstein 		fsnotify_put_sb_watched_objects(sb);
196c3638b5bSAmir Goldstein 	}
197c9d4603bSAmir Goldstein }
198c3638b5bSAmir Goldstein 
199c3638b5bSAmir Goldstein /*
200c3638b5bSAmir Goldstein  * Grab or drop inode reference for the connector if needed.
201c3638b5bSAmir Goldstein  *
202c3638b5bSAmir Goldstein  * When it's time to drop the reference, we only clear the HAS_IREF flag and
203c3638b5bSAmir Goldstein  * return the inode object. fsnotify_drop_object() will be resonsible for doing
204c3638b5bSAmir Goldstein  * iput() outside of spinlocks. This happens when last mark that wanted iref is
205c3638b5bSAmir Goldstein  * detached.
206c3638b5bSAmir Goldstein  */
207c3638b5bSAmir Goldstein static struct inode *fsnotify_update_iref(struct fsnotify_mark_connector *conn,
208c3638b5bSAmir Goldstein 					  bool want_iref)
209c3638b5bSAmir Goldstein {
210c3638b5bSAmir Goldstein 	bool has_iref = conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF;
211c3638b5bSAmir Goldstein 	struct inode *inode = NULL;
212c3638b5bSAmir Goldstein 
213c3638b5bSAmir Goldstein 	if (conn->type != FSNOTIFY_OBJ_TYPE_INODE ||
214c3638b5bSAmir Goldstein 	    want_iref == has_iref)
215c3638b5bSAmir Goldstein 		return NULL;
216c3638b5bSAmir Goldstein 
217c3638b5bSAmir Goldstein 	if (want_iref) {
218c3638b5bSAmir Goldstein 		/* Pin inode if any mark wants inode refcount held */
219c3638b5bSAmir Goldstein 		fsnotify_get_inode_ref(fsnotify_conn_inode(conn));
220c3638b5bSAmir Goldstein 		conn->flags |= FSNOTIFY_CONN_FLAG_HAS_IREF;
221c3638b5bSAmir Goldstein 	} else {
222c3638b5bSAmir Goldstein 		/* Unpin inode after detach of last mark that wanted iref */
223c3638b5bSAmir Goldstein 		inode = fsnotify_conn_inode(conn);
224c3638b5bSAmir Goldstein 		conn->flags &= ~FSNOTIFY_CONN_FLAG_HAS_IREF;
225c3638b5bSAmir Goldstein 	}
226c3638b5bSAmir Goldstein 
227c3638b5bSAmir Goldstein 	return inode;
228c3638b5bSAmir Goldstein }
229c3638b5bSAmir Goldstein 
230c3638b5bSAmir Goldstein static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
2310809ab69SJan Kara {
2320809ab69SJan Kara 	u32 new_mask = 0;
233c3638b5bSAmir Goldstein 	bool want_iref = false;
2340809ab69SJan Kara 	struct fsnotify_mark *mark;
2350809ab69SJan Kara 
23604662cabSJan Kara 	assert_spin_locked(&conn->lock);
237d3bc0fa8SJan Kara 	/* We can get detached connector here when inode is getting unlinked. */
238d3bc0fa8SJan Kara 	if (!fsnotify_valid_obj_type(conn->type))
239c3638b5bSAmir Goldstein 		return NULL;
2406b3f05d2SJan Kara 	hlist_for_each_entry(mark, &conn->list, obj_list) {
241c3638b5bSAmir Goldstein 		if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED))
242c3638b5bSAmir Goldstein 			continue;
2434f0b903dSAmir Goldstein 		new_mask |= fsnotify_calc_mask(mark);
244c3638b5bSAmir Goldstein 		if (conn->type == FSNOTIFY_OBJ_TYPE_INODE &&
245c3638b5bSAmir Goldstein 		    !(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
246c3638b5bSAmir Goldstein 			want_iref = true;
2476b3f05d2SJan Kara 	}
248*35ceae44SJan Kara 	/*
249*35ceae44SJan Kara 	 * We use WRITE_ONCE() to prevent silly compiler optimizations from
250*35ceae44SJan Kara 	 * confusing readers not holding conn->lock with partial updates.
251*35ceae44SJan Kara 	 */
252*35ceae44SJan Kara 	WRITE_ONCE(*fsnotify_conn_mask_p(conn), new_mask);
253c3638b5bSAmir Goldstein 
254c3638b5bSAmir Goldstein 	return fsnotify_update_iref(conn, want_iref);
255a242677bSJan Kara }
256a242677bSJan Kara 
257172e422fSAmir Goldstein static bool fsnotify_conn_watches_children(
258172e422fSAmir Goldstein 					struct fsnotify_mark_connector *conn)
259172e422fSAmir Goldstein {
260172e422fSAmir Goldstein 	if (conn->type != FSNOTIFY_OBJ_TYPE_INODE)
261172e422fSAmir Goldstein 		return false;
262172e422fSAmir Goldstein 
263172e422fSAmir Goldstein 	return fsnotify_inode_watches_children(fsnotify_conn_inode(conn));
264172e422fSAmir Goldstein }
265172e422fSAmir Goldstein 
266172e422fSAmir Goldstein static void fsnotify_conn_set_children_dentry_flags(
267172e422fSAmir Goldstein 					struct fsnotify_mark_connector *conn)
268172e422fSAmir Goldstein {
269172e422fSAmir Goldstein 	if (conn->type != FSNOTIFY_OBJ_TYPE_INODE)
270172e422fSAmir Goldstein 		return;
271172e422fSAmir Goldstein 
272172e422fSAmir Goldstein 	fsnotify_set_children_dentry_flags(fsnotify_conn_inode(conn));
273172e422fSAmir Goldstein }
274172e422fSAmir Goldstein 
275a242677bSJan Kara /*
276a242677bSJan Kara  * Calculate mask of events for a list of marks. The caller must make sure
27736f10f55SAmir Goldstein  * connector and connector->obj cannot disappear under us.  Callers achieve
2786b3f05d2SJan Kara  * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
2796b3f05d2SJan Kara  * list.
280a242677bSJan Kara  */
281a242677bSJan Kara void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
282a242677bSJan Kara {
283172e422fSAmir Goldstein 	bool update_children;
284172e422fSAmir Goldstein 
285a242677bSJan Kara 	if (!conn)
286a242677bSJan Kara 		return;
287a242677bSJan Kara 
28804662cabSJan Kara 	spin_lock(&conn->lock);
289172e422fSAmir Goldstein 	update_children = !fsnotify_conn_watches_children(conn);
290a242677bSJan Kara 	__fsnotify_recalc_mask(conn);
291172e422fSAmir Goldstein 	update_children &= fsnotify_conn_watches_children(conn);
29204662cabSJan Kara 	spin_unlock(&conn->lock);
293172e422fSAmir Goldstein 	/*
294172e422fSAmir Goldstein 	 * Set children's PARENT_WATCHED flags only if parent started watching.
295172e422fSAmir Goldstein 	 * When parent stops watching, we clear false positive PARENT_WATCHED
296172e422fSAmir Goldstein 	 * flags lazily in __fsnotify_parent().
297172e422fSAmir Goldstein 	 */
298172e422fSAmir Goldstein 	if (update_children)
299172e422fSAmir Goldstein 		fsnotify_conn_set_children_dentry_flags(conn);
3000809ab69SJan Kara }
3010809ab69SJan Kara 
30208991e83SJan Kara /* Free all connectors queued for freeing once SRCU period ends */
30308991e83SJan Kara static void fsnotify_connector_destroy_workfn(struct work_struct *work)
30408991e83SJan Kara {
30508991e83SJan Kara 	struct fsnotify_mark_connector *conn, *free;
30608991e83SJan Kara 
30708991e83SJan Kara 	spin_lock(&destroy_lock);
30808991e83SJan Kara 	conn = connector_destroy_list;
30908991e83SJan Kara 	connector_destroy_list = NULL;
31008991e83SJan Kara 	spin_unlock(&destroy_lock);
31108991e83SJan Kara 
31208991e83SJan Kara 	synchronize_srcu(&fsnotify_mark_srcu);
31308991e83SJan Kara 	while (conn) {
31408991e83SJan Kara 		free = conn;
31508991e83SJan Kara 		conn = conn->destroy_next;
31608991e83SJan Kara 		kmem_cache_free(fsnotify_mark_connector_cachep, free);
31708991e83SJan Kara 	}
31808991e83SJan Kara }
31908991e83SJan Kara 
320721fb6fbSJan Kara static void *fsnotify_detach_connector_from_object(
321721fb6fbSJan Kara 					struct fsnotify_mark_connector *conn,
322721fb6fbSJan Kara 					unsigned int *type)
32308991e83SJan Kara {
324687c217cSAmir Goldstein 	fsnotify_connp_t *connp = fsnotify_object_connp(conn->obj, conn->type);
325c9d4603bSAmir Goldstein 	struct super_block *sb = fsnotify_connector_sb(conn);
32608991e83SJan Kara 	struct inode *inode = NULL;
32708991e83SJan Kara 
328721fb6fbSJan Kara 	*type = conn->type;
32936f10f55SAmir Goldstein 	if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED)
33036f10f55SAmir Goldstein 		return NULL;
33136f10f55SAmir Goldstein 
332d6f7b98bSAmir Goldstein 	if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
33336f10f55SAmir Goldstein 		inode = fsnotify_conn_inode(conn);
33408991e83SJan Kara 		inode->i_fsnotify_mask = 0;
335c3638b5bSAmir Goldstein 
336c3638b5bSAmir Goldstein 		/* Unpin inode when detaching from connector */
337c3638b5bSAmir Goldstein 		if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF))
338c3638b5bSAmir Goldstein 			inode = NULL;
339d6f7b98bSAmir Goldstein 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
34036f10f55SAmir Goldstein 		fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0;
3411e6cb723SAmir Goldstein 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) {
3421e6cb723SAmir Goldstein 		fsnotify_conn_sb(conn)->s_fsnotify_mask = 0;
34308991e83SJan Kara 	}
34408991e83SJan Kara 
345687c217cSAmir Goldstein 	rcu_assign_pointer(*connp, NULL);
34636f10f55SAmir Goldstein 	conn->obj = NULL;
34736f10f55SAmir Goldstein 	conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
348c9d4603bSAmir Goldstein 	fsnotify_update_sb_watchers(sb, conn);
34936f10f55SAmir Goldstein 
35008991e83SJan Kara 	return inode;
35108991e83SJan Kara }
35208991e83SJan Kara 
3536b3f05d2SJan Kara static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark)
3546b3f05d2SJan Kara {
355054c636eSJan Kara 	struct fsnotify_group *group = mark->group;
356054c636eSJan Kara 
357054c636eSJan Kara 	if (WARN_ON_ONCE(!group))
358054c636eSJan Kara 		return;
359054c636eSJan Kara 	group->ops->free_mark(mark);
360054c636eSJan Kara 	fsnotify_put_group(group);
3616b3f05d2SJan Kara }
3626b3f05d2SJan Kara 
363721fb6fbSJan Kara /* Drop object reference originally held by a connector */
364721fb6fbSJan Kara static void fsnotify_drop_object(unsigned int type, void *objp)
365721fb6fbSJan Kara {
366721fb6fbSJan Kara 	if (!objp)
367721fb6fbSJan Kara 		return;
368721fb6fbSJan Kara 	/* Currently only inode references are passed to be dropped */
369721fb6fbSJan Kara 	if (WARN_ON_ONCE(type != FSNOTIFY_OBJ_TYPE_INODE))
370721fb6fbSJan Kara 		return;
37111fa333bSAmir Goldstein 	fsnotify_put_inode_ref(objp);
372721fb6fbSJan Kara }
373721fb6fbSJan Kara 
3746b3f05d2SJan Kara void fsnotify_put_mark(struct fsnotify_mark *mark)
3758212a609SJan Kara {
376b1da6a51SJan Kara 	struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector);
377721fb6fbSJan Kara 	void *objp = NULL;
378721fb6fbSJan Kara 	unsigned int type = FSNOTIFY_OBJ_TYPE_DETACHED;
37908991e83SJan Kara 	bool free_conn = false;
3808212a609SJan Kara 
3816b3f05d2SJan Kara 	/* Catch marks that were actually never attached to object */
382b1da6a51SJan Kara 	if (!conn) {
383ab97f873SElena Reshetova 		if (refcount_dec_and_test(&mark->refcnt))
3846b3f05d2SJan Kara 			fsnotify_final_mark_destroy(mark);
3856b3f05d2SJan Kara 		return;
3866b3f05d2SJan Kara 	}
3876b3f05d2SJan Kara 
3886b3f05d2SJan Kara 	/*
3896b3f05d2SJan Kara 	 * We have to be careful so that traversals of obj_list under lock can
3906b3f05d2SJan Kara 	 * safely grab mark reference.
3916b3f05d2SJan Kara 	 */
392b1da6a51SJan Kara 	if (!refcount_dec_and_lock(&mark->refcnt, &conn->lock))
3936b3f05d2SJan Kara 		return;
3946b3f05d2SJan Kara 
3958212a609SJan Kara 	hlist_del_init_rcu(&mark->obj_list);
3968212a609SJan Kara 	if (hlist_empty(&conn->list)) {
397721fb6fbSJan Kara 		objp = fsnotify_detach_connector_from_object(conn, &type);
39808991e83SJan Kara 		free_conn = true;
39908991e83SJan Kara 	} else {
400c9d4603bSAmir Goldstein 		struct super_block *sb = fsnotify_connector_sb(conn);
401c9d4603bSAmir Goldstein 
402c9d4603bSAmir Goldstein 		/* Update watched objects after detaching mark */
403c9d4603bSAmir Goldstein 		if (sb)
404c9d4603bSAmir Goldstein 			fsnotify_update_sb_watchers(sb, conn);
405c3638b5bSAmir Goldstein 		objp = __fsnotify_recalc_mask(conn);
406c3638b5bSAmir Goldstein 		type = conn->type;
40708991e83SJan Kara 	}
408b1da6a51SJan Kara 	WRITE_ONCE(mark->connector, NULL);
40904662cabSJan Kara 	spin_unlock(&conn->lock);
4108212a609SJan Kara 
411721fb6fbSJan Kara 	fsnotify_drop_object(type, objp);
4126b3f05d2SJan Kara 
41308991e83SJan Kara 	if (free_conn) {
41408991e83SJan Kara 		spin_lock(&destroy_lock);
41508991e83SJan Kara 		conn->destroy_next = connector_destroy_list;
41608991e83SJan Kara 		connector_destroy_list = conn;
41708991e83SJan Kara 		spin_unlock(&destroy_lock);
41808991e83SJan Kara 		queue_work(system_unbound_wq, &connector_reaper_work);
41908991e83SJan Kara 	}
4206b3f05d2SJan Kara 	/*
4216b3f05d2SJan Kara 	 * Note that we didn't update flags telling whether inode cares about
4226b3f05d2SJan Kara 	 * what's happening with children. We update these flags from
4236b3f05d2SJan Kara 	 * __fsnotify_parent() lazily when next event happens on one of our
4246b3f05d2SJan Kara 	 * children.
4256b3f05d2SJan Kara 	 */
4266b3f05d2SJan Kara 	spin_lock(&destroy_lock);
4276b3f05d2SJan Kara 	list_add(&mark->g_list, &destroy_list);
4286b3f05d2SJan Kara 	spin_unlock(&destroy_lock);
4296b3f05d2SJan Kara 	queue_delayed_work(system_unbound_wq, &reaper_work,
4306b3f05d2SJan Kara 			   FSNOTIFY_REAPER_DELAY);
4318212a609SJan Kara }
432b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_put_mark);
4338212a609SJan Kara 
43424c20305SMiklos Szeredi /*
43524c20305SMiklos Szeredi  * Get mark reference when we found the mark via lockless traversal of object
43624c20305SMiklos Szeredi  * list. Mark can be already removed from the list by now and on its way to be
43724c20305SMiklos Szeredi  * destroyed once SRCU period ends.
43824c20305SMiklos Szeredi  *
43924c20305SMiklos Szeredi  * Also pin the group so it doesn't disappear under us.
44024c20305SMiklos Szeredi  */
44124c20305SMiklos Szeredi static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark)
442abc77577SJan Kara {
44324c20305SMiklos Szeredi 	if (!mark)
44424c20305SMiklos Szeredi 		return true;
445abc77577SJan Kara 
446ab97f873SElena Reshetova 	if (refcount_inc_not_zero(&mark->refcnt)) {
4479a31d7adSMiklos Szeredi 		spin_lock(&mark->lock);
4489a31d7adSMiklos Szeredi 		if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) {
4499a31d7adSMiklos Szeredi 			/* mark is attached, group is still alive then */
4509a31d7adSMiklos Szeredi 			atomic_inc(&mark->group->user_waits);
4519a31d7adSMiklos Szeredi 			spin_unlock(&mark->lock);
45224c20305SMiklos Szeredi 			return true;
4539a31d7adSMiklos Szeredi 		}
4549a31d7adSMiklos Szeredi 		spin_unlock(&mark->lock);
4559a31d7adSMiklos Szeredi 		fsnotify_put_mark(mark);
4569a31d7adSMiklos Szeredi 	}
45724c20305SMiklos Szeredi 	return false;
45824c20305SMiklos Szeredi }
45924c20305SMiklos Szeredi 
46024c20305SMiklos Szeredi /*
46124c20305SMiklos Szeredi  * Puts marks and wakes up group destruction if necessary.
46224c20305SMiklos Szeredi  *
46324c20305SMiklos Szeredi  * Pairs with fsnotify_get_mark_safe()
46424c20305SMiklos Szeredi  */
46524c20305SMiklos Szeredi static void fsnotify_put_mark_wake(struct fsnotify_mark *mark)
46624c20305SMiklos Szeredi {
46724c20305SMiklos Szeredi 	if (mark) {
46824c20305SMiklos Szeredi 		struct fsnotify_group *group = mark->group;
46924c20305SMiklos Szeredi 
47024c20305SMiklos Szeredi 		fsnotify_put_mark(mark);
47124c20305SMiklos Szeredi 		/*
47224c20305SMiklos Szeredi 		 * We abuse notification_waitq on group shutdown for waiting for
47324c20305SMiklos Szeredi 		 * all marks pinned when waiting for userspace.
47424c20305SMiklos Szeredi 		 */
47524c20305SMiklos Szeredi 		if (atomic_dec_and_test(&group->user_waits) && group->shutdown)
47624c20305SMiklos Szeredi 			wake_up(&group->notification_waitq);
47724c20305SMiklos Szeredi 	}
47824c20305SMiklos Szeredi }
47924c20305SMiklos Szeredi 
48024c20305SMiklos Szeredi bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info)
48100e0afb6SJules Irenge 	__releases(&fsnotify_mark_srcu)
48224c20305SMiklos Szeredi {
48347d9c7ccSAmir Goldstein 	int type;
48447d9c7ccSAmir Goldstein 
4851c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type) {
486abc77577SJan Kara 		/* This can fail if mark is being removed */
48700e0afb6SJules Irenge 		if (!fsnotify_get_mark_safe(iter_info->marks[type])) {
48800e0afb6SJules Irenge 			__release(&fsnotify_mark_srcu);
48947d9c7ccSAmir Goldstein 			goto fail;
490abc77577SJan Kara 		}
49100e0afb6SJules Irenge 	}
492abc77577SJan Kara 
493abc77577SJan Kara 	/*
494abc77577SJan Kara 	 * Now that both marks are pinned by refcount in the inode / vfsmount
495abc77577SJan Kara 	 * lists, we can drop SRCU lock, and safely resume the list iteration
496abc77577SJan Kara 	 * once userspace returns.
497abc77577SJan Kara 	 */
498abc77577SJan Kara 	srcu_read_unlock(&fsnotify_mark_srcu, iter_info->srcu_idx);
499abc77577SJan Kara 
500abc77577SJan Kara 	return true;
50147d9c7ccSAmir Goldstein 
50247d9c7ccSAmir Goldstein fail:
50347d9c7ccSAmir Goldstein 	for (type--; type >= 0; type--)
50447d9c7ccSAmir Goldstein 		fsnotify_put_mark_wake(iter_info->marks[type]);
50547d9c7ccSAmir Goldstein 	return false;
506abc77577SJan Kara }
507abc77577SJan Kara 
508abc77577SJan Kara void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info)
50900e0afb6SJules Irenge 	__acquires(&fsnotify_mark_srcu)
510abc77577SJan Kara {
51147d9c7ccSAmir Goldstein 	int type;
51247d9c7ccSAmir Goldstein 
513abc77577SJan Kara 	iter_info->srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
5141c9007d6SAmir Goldstein 	fsnotify_foreach_iter_type(type)
51547d9c7ccSAmir Goldstein 		fsnotify_put_mark_wake(iter_info->marks[type]);
516abc77577SJan Kara }
517abc77577SJan Kara 
5185444e298SEric Paris /*
5196b3f05d2SJan Kara  * Mark mark as detached, remove it from group list. Mark still stays in object
5206b3f05d2SJan Kara  * list until its last reference is dropped. Note that we rely on mark being
5216b3f05d2SJan Kara  * removed from group list before corresponding reference to it is dropped. In
5226b3f05d2SJan Kara  * particular we rely on mark->connector being valid while we hold
5236b3f05d2SJan Kara  * group->mark_mutex if we found the mark through g_list.
5244712e722SJan Kara  *
52511375145SJan Kara  * Must be called with group->mark_mutex held. The caller must either hold
52611375145SJan Kara  * reference to the mark or be protected by fsnotify_mark_srcu.
5275444e298SEric Paris  */
5284712e722SJan Kara void fsnotify_detach_mark(struct fsnotify_mark *mark)
5295444e298SEric Paris {
53043b245a7SAmir Goldstein 	fsnotify_group_assert_locked(mark->group);
53111375145SJan Kara 	WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) &&
532ab97f873SElena Reshetova 		     refcount_read(&mark->refcnt) < 1 +
53311375145SJan Kara 			!!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED));
534d5a335b8SLino Sanfilippo 
535104d06f0SLino Sanfilippo 	spin_lock(&mark->lock);
536700307a2SEric Paris 	/* something else already called this function on this mark */
5374712e722SJan Kara 	if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
5385444e298SEric Paris 		spin_unlock(&mark->lock);
539e2a29943SLino Sanfilippo 		return;
5405444e298SEric Paris 	}
5414712e722SJan Kara 	mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
5425444e298SEric Paris 	list_del_init(&mark->g_list);
5435444e298SEric Paris 	spin_unlock(&mark->lock);
544d5a335b8SLino Sanfilippo 
54511375145SJan Kara 	/* Drop mark reference acquired in fsnotify_add_mark_locked() */
54611375145SJan Kara 	fsnotify_put_mark(mark);
5474712e722SJan Kara }
5484712e722SJan Kara 
5494712e722SJan Kara /*
55011375145SJan Kara  * Free fsnotify mark. The mark is actually only marked as being freed.  The
55111375145SJan Kara  * freeing is actually happening only once last reference to the mark is
55211375145SJan Kara  * dropped from a workqueue which first waits for srcu period end.
55335e48176SJan Kara  *
55411375145SJan Kara  * Caller must have a reference to the mark or be protected by
55511375145SJan Kara  * fsnotify_mark_srcu.
5564712e722SJan Kara  */
55711375145SJan Kara void fsnotify_free_mark(struct fsnotify_mark *mark)
5584712e722SJan Kara {
5594712e722SJan Kara 	struct fsnotify_group *group = mark->group;
5604712e722SJan Kara 
5614712e722SJan Kara 	spin_lock(&mark->lock);
5624712e722SJan Kara 	/* something else already called this function on this mark */
5634712e722SJan Kara 	if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
5644712e722SJan Kara 		spin_unlock(&mark->lock);
56511375145SJan Kara 		return;
5664712e722SJan Kara 	}
5674712e722SJan Kara 	mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
5684712e722SJan Kara 	spin_unlock(&mark->lock);
5695444e298SEric Paris 
570d725e66cSLinus Torvalds 	/*
571d725e66cSLinus Torvalds 	 * Some groups like to know that marks are being freed.  This is a
572d725e66cSLinus Torvalds 	 * callback to the group function to let it know that this mark
573d725e66cSLinus Torvalds 	 * is being freed.
574d725e66cSLinus Torvalds 	 */
575d725e66cSLinus Torvalds 	if (group->ops->freeing_mark)
576d725e66cSLinus Torvalds 		group->ops->freeing_mark(mark, group);
577d5a335b8SLino Sanfilippo }
578d5a335b8SLino Sanfilippo 
579d5a335b8SLino Sanfilippo void fsnotify_destroy_mark(struct fsnotify_mark *mark,
580d5a335b8SLino Sanfilippo 			   struct fsnotify_group *group)
581d5a335b8SLino Sanfilippo {
58243b245a7SAmir Goldstein 	fsnotify_group_lock(group);
5834712e722SJan Kara 	fsnotify_detach_mark(mark);
58443b245a7SAmir Goldstein 	fsnotify_group_unlock(group);
5854712e722SJan Kara 	fsnotify_free_mark(mark);
5865444e298SEric Paris }
587b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
5885444e298SEric Paris 
5895444e298SEric Paris /*
5908edc6e16SJan Kara  * Sorting function for lists of fsnotify marks.
5918edc6e16SJan Kara  *
5928edc6e16SJan Kara  * Fanotify supports different notification classes (reflected as priority of
5938edc6e16SJan Kara  * notification group). Events shall be passed to notification groups in
5948edc6e16SJan Kara  * decreasing priority order. To achieve this marks in notification lists for
5958edc6e16SJan Kara  * inodes and vfsmounts are sorted so that priorities of corresponding groups
5968edc6e16SJan Kara  * are descending.
5978edc6e16SJan Kara  *
5988edc6e16SJan Kara  * Furthermore correct handling of the ignore mask requires processing inode
5998edc6e16SJan Kara  * and vfsmount marks of each group together. Using the group address as
6008edc6e16SJan Kara  * further sort criterion provides a unique sorting order and thus we can
6018edc6e16SJan Kara  * merge inode and vfsmount lists of marks in linear time and find groups
6028edc6e16SJan Kara  * present in both lists.
6038edc6e16SJan Kara  *
6048edc6e16SJan Kara  * A return value of 1 signifies that b has priority over a.
6058edc6e16SJan Kara  * A return value of 0 signifies that the two marks have to be handled together.
6068edc6e16SJan Kara  * A return value of -1 signifies that a has priority over b.
6078edc6e16SJan Kara  */
6088edc6e16SJan Kara int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
6098edc6e16SJan Kara {
6108edc6e16SJan Kara 	if (a == b)
6118edc6e16SJan Kara 		return 0;
6128edc6e16SJan Kara 	if (!a)
6138edc6e16SJan Kara 		return 1;
6148edc6e16SJan Kara 	if (!b)
6158edc6e16SJan Kara 		return -1;
6168edc6e16SJan Kara 	if (a->priority < b->priority)
6178edc6e16SJan Kara 		return 1;
6188edc6e16SJan Kara 	if (a->priority > b->priority)
6198edc6e16SJan Kara 		return -1;
6208edc6e16SJan Kara 	if (a < b)
6218edc6e16SJan Kara 		return 1;
6228edc6e16SJan Kara 	return -1;
6238edc6e16SJan Kara }
6248edc6e16SJan Kara 
62507a3b8d0SAmir Goldstein static int fsnotify_attach_info_to_sb(struct super_block *sb)
62607a3b8d0SAmir Goldstein {
62707a3b8d0SAmir Goldstein 	struct fsnotify_sb_info *sbinfo;
62807a3b8d0SAmir Goldstein 
62907a3b8d0SAmir Goldstein 	/* sb info is freed on fsnotify_sb_delete() */
63007a3b8d0SAmir Goldstein 	sbinfo = kzalloc(sizeof(*sbinfo), GFP_KERNEL);
63107a3b8d0SAmir Goldstein 	if (!sbinfo)
63207a3b8d0SAmir Goldstein 		return -ENOMEM;
63307a3b8d0SAmir Goldstein 
63407a3b8d0SAmir Goldstein 	/*
63507a3b8d0SAmir Goldstein 	 * cmpxchg() provides the barrier so that callers of fsnotify_sb_info()
63607a3b8d0SAmir Goldstein 	 * will observe an initialized structure
63707a3b8d0SAmir Goldstein 	 */
63807a3b8d0SAmir Goldstein 	if (cmpxchg(&sb->s_fsnotify_info, NULL, sbinfo)) {
63907a3b8d0SAmir Goldstein 		/* Someone else created sbinfo for us */
64007a3b8d0SAmir Goldstein 		kfree(sbinfo);
64107a3b8d0SAmir Goldstein 	}
64207a3b8d0SAmir Goldstein 	return 0;
64307a3b8d0SAmir Goldstein }
64407a3b8d0SAmir Goldstein 
6459b6e5434SAmir Goldstein static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
646687c217cSAmir Goldstein 					       void *obj, unsigned int obj_type)
6479dd813c1SJan Kara {
6489dd813c1SJan Kara 	struct fsnotify_mark_connector *conn;
6499dd813c1SJan Kara 
650755b5bc6SJan Kara 	conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
6519dd813c1SJan Kara 	if (!conn)
6529dd813c1SJan Kara 		return -ENOMEM;
65304662cabSJan Kara 	spin_lock_init(&conn->lock);
6549dd813c1SJan Kara 	INIT_HLIST_HEAD(&conn->list);
655c3638b5bSAmir Goldstein 	conn->flags = 0;
656a5e57b4dSAmir Goldstein 	conn->prio = 0;
657ad69cd99SAmir Goldstein 	conn->type = obj_type;
658687c217cSAmir Goldstein 	conn->obj = obj;
65909ddbe69SAmir Goldstein 
6609dd813c1SJan Kara 	/*
66104662cabSJan Kara 	 * cmpxchg() provides the barrier so that readers of *connp can see
66204662cabSJan Kara 	 * only initialized structure
6639dd813c1SJan Kara 	 */
66404662cabSJan Kara 	if (cmpxchg(connp, NULL, conn)) {
66504662cabSJan Kara 		/* Someone else created list structure for us */
666755b5bc6SJan Kara 		kmem_cache_free(fsnotify_mark_connector_cachep, conn);
66704662cabSJan Kara 	}
6689dd813c1SJan Kara 	return 0;
6699dd813c1SJan Kara }
6709dd813c1SJan Kara 
6719dd813c1SJan Kara /*
67208991e83SJan Kara  * Get mark connector, make sure it is alive and return with its lock held.
67308991e83SJan Kara  * This is for users that get connector pointer from inode or mount. Users that
67408991e83SJan Kara  * hold reference to a mark on the list may directly lock connector->lock as
67508991e83SJan Kara  * they are sure list cannot go away under them.
67608991e83SJan Kara  */
67708991e83SJan Kara static struct fsnotify_mark_connector *fsnotify_grab_connector(
6789b6e5434SAmir Goldstein 						fsnotify_connp_t *connp)
67908991e83SJan Kara {
68008991e83SJan Kara 	struct fsnotify_mark_connector *conn;
68108991e83SJan Kara 	int idx;
68208991e83SJan Kara 
68308991e83SJan Kara 	idx = srcu_read_lock(&fsnotify_mark_srcu);
68408991e83SJan Kara 	conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
68508991e83SJan Kara 	if (!conn)
68608991e83SJan Kara 		goto out;
68708991e83SJan Kara 	spin_lock(&conn->lock);
688d6f7b98bSAmir Goldstein 	if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) {
68908991e83SJan Kara 		spin_unlock(&conn->lock);
69008991e83SJan Kara 		srcu_read_unlock(&fsnotify_mark_srcu, idx);
69108991e83SJan Kara 		return NULL;
69208991e83SJan Kara 	}
69308991e83SJan Kara out:
69408991e83SJan Kara 	srcu_read_unlock(&fsnotify_mark_srcu, idx);
69508991e83SJan Kara 	return conn;
69608991e83SJan Kara }
69708991e83SJan Kara 
69808991e83SJan Kara /*
6999dd813c1SJan Kara  * Add mark into proper place in given list of marks. These marks may be used
7009dd813c1SJan Kara  * for the fsnotify backend to determine which event types should be delivered
7019dd813c1SJan Kara  * to which group and for which inodes. These marks are ordered according to
7029dd813c1SJan Kara  * priority, highest number first, and then by the group's location in memory.
7039dd813c1SJan Kara  */
704687c217cSAmir Goldstein static int fsnotify_add_mark_list(struct fsnotify_mark *mark, void *obj,
7057232522eSAmir Goldstein 				  unsigned int obj_type, int add_flags)
7060809ab69SJan Kara {
707c9d4603bSAmir Goldstein 	struct super_block *sb = fsnotify_object_sb(obj, obj_type);
7080809ab69SJan Kara 	struct fsnotify_mark *lmark, *last = NULL;
7099dd813c1SJan Kara 	struct fsnotify_mark_connector *conn;
710687c217cSAmir Goldstein 	fsnotify_connp_t *connp;
7110809ab69SJan Kara 	int cmp;
712755b5bc6SJan Kara 	int err = 0;
713755b5bc6SJan Kara 
714ad69cd99SAmir Goldstein 	if (WARN_ON(!fsnotify_valid_obj_type(obj_type)))
715755b5bc6SJan Kara 		return -EINVAL;
71677115225SAmir Goldstein 
71707a3b8d0SAmir Goldstein 	/*
71807a3b8d0SAmir Goldstein 	 * Attach the sb info before attaching a connector to any object on sb.
71907a3b8d0SAmir Goldstein 	 * The sb info will remain attached as long as sb lives.
72007a3b8d0SAmir Goldstein 	 */
72107a3b8d0SAmir Goldstein 	if (!fsnotify_sb_info(sb)) {
72207a3b8d0SAmir Goldstein 		err = fsnotify_attach_info_to_sb(sb);
72307a3b8d0SAmir Goldstein 		if (err)
72407a3b8d0SAmir Goldstein 			return err;
72507a3b8d0SAmir Goldstein 	}
72607a3b8d0SAmir Goldstein 
727687c217cSAmir Goldstein 	connp = fsnotify_object_connp(obj, obj_type);
72808991e83SJan Kara restart:
72908991e83SJan Kara 	spin_lock(&mark->lock);
73008991e83SJan Kara 	conn = fsnotify_grab_connector(connp);
73108991e83SJan Kara 	if (!conn) {
73208991e83SJan Kara 		spin_unlock(&mark->lock);
733687c217cSAmir Goldstein 		err = fsnotify_attach_connector_to_object(connp, obj, obj_type);
7349dd813c1SJan Kara 		if (err)
7359dd813c1SJan Kara 			return err;
73608991e83SJan Kara 		goto restart;
7379dd813c1SJan Kara 	}
7380809ab69SJan Kara 
7390809ab69SJan Kara 	/* is mark the first mark? */
7409dd813c1SJan Kara 	if (hlist_empty(&conn->list)) {
7419dd813c1SJan Kara 		hlist_add_head_rcu(&mark->obj_list, &conn->list);
74286ffe245SJan Kara 		goto added;
7430809ab69SJan Kara 	}
7440809ab69SJan Kara 
7450809ab69SJan Kara 	/* should mark be in the middle of the current list? */
7469dd813c1SJan Kara 	hlist_for_each_entry(lmark, &conn->list, obj_list) {
7470809ab69SJan Kara 		last = lmark;
7480809ab69SJan Kara 
7496b3f05d2SJan Kara 		if ((lmark->group == mark->group) &&
7506b3f05d2SJan Kara 		    (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) &&
751f3010343SAmir Goldstein 		    !(mark->group->flags & FSNOTIFY_GROUP_DUPS)) {
752755b5bc6SJan Kara 			err = -EEXIST;
753755b5bc6SJan Kara 			goto out_err;
754755b5bc6SJan Kara 		}
7550809ab69SJan Kara 
7560809ab69SJan Kara 		cmp = fsnotify_compare_groups(lmark->group, mark->group);
7570809ab69SJan Kara 		if (cmp >= 0) {
7580809ab69SJan Kara 			hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
75986ffe245SJan Kara 			goto added;
7600809ab69SJan Kara 		}
7610809ab69SJan Kara 	}
7620809ab69SJan Kara 
7630809ab69SJan Kara 	BUG_ON(last == NULL);
7640809ab69SJan Kara 	/* mark should be the last entry.  last is the current last entry */
7650809ab69SJan Kara 	hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
76686ffe245SJan Kara added:
767c9d4603bSAmir Goldstein 	fsnotify_update_sb_watchers(sb, conn);
76811a6f8e2SJan Kara 	/*
76911a6f8e2SJan Kara 	 * Since connector is attached to object using cmpxchg() we are
77011a6f8e2SJan Kara 	 * guaranteed that connector initialization is fully visible by anyone
77111a6f8e2SJan Kara 	 * seeing mark->connector set.
77211a6f8e2SJan Kara 	 */
773b1da6a51SJan Kara 	WRITE_ONCE(mark->connector, conn);
774755b5bc6SJan Kara out_err:
77504662cabSJan Kara 	spin_unlock(&conn->lock);
776755b5bc6SJan Kara 	spin_unlock(&mark->lock);
777755b5bc6SJan Kara 	return err;
7780809ab69SJan Kara }
7790809ab69SJan Kara 
7808edc6e16SJan Kara /*
7815444e298SEric Paris  * Attach an initialized mark to a given group and fs object.
7825444e298SEric Paris  * These marks may be used for the fsnotify backend to determine which
7835444e298SEric Paris  * event types should be delivered to which group.
7845444e298SEric Paris  */
785b812a9f5SAmir Goldstein int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
786687c217cSAmir Goldstein 			     void *obj, unsigned int obj_type,
7877232522eSAmir Goldstein 			     int add_flags)
7885444e298SEric Paris {
7897b129323SJan Kara 	struct fsnotify_group *group = mark->group;
7905444e298SEric Paris 	int ret = 0;
7915444e298SEric Paris 
79243b245a7SAmir Goldstein 	fsnotify_group_assert_locked(group);
7935444e298SEric Paris 
7945444e298SEric Paris 	/*
7955444e298SEric Paris 	 * LOCKING ORDER!!!!
796986ab098SLino Sanfilippo 	 * group->mark_mutex
797104d06f0SLino Sanfilippo 	 * mark->lock
79804662cabSJan Kara 	 * mark->connector->lock
7995444e298SEric Paris 	 */
800104d06f0SLino Sanfilippo 	spin_lock(&mark->lock);
8014712e722SJan Kara 	mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
802700307a2SEric Paris 
8035444e298SEric Paris 	list_add(&mark->g_list, &group->marks_list);
8046b3f05d2SJan Kara 	fsnotify_get_mark(mark); /* for g_list */
8055444e298SEric Paris 	spin_unlock(&mark->lock);
8065444e298SEric Paris 
807687c217cSAmir Goldstein 	ret = fsnotify_add_mark_list(mark, obj, obj_type, add_flags);
808755b5bc6SJan Kara 	if (ret)
809755b5bc6SJan Kara 		goto err;
810755b5bc6SJan Kara 
811a242677bSJan Kara 	fsnotify_recalc_mask(mark->connector);
8125444e298SEric Paris 
8135444e298SEric Paris 	return ret;
8145444e298SEric Paris err:
8159cf90cefSJan Kara 	spin_lock(&mark->lock);
81611375145SJan Kara 	mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE |
81711375145SJan Kara 			 FSNOTIFY_MARK_FLAG_ATTACHED);
8185444e298SEric Paris 	list_del_init(&mark->g_list);
8199cf90cefSJan Kara 	spin_unlock(&mark->lock);
8205444e298SEric Paris 
82111375145SJan Kara 	fsnotify_put_mark(mark);
8225444e298SEric Paris 	return ret;
8235444e298SEric Paris }
8245444e298SEric Paris 
825687c217cSAmir Goldstein int fsnotify_add_mark(struct fsnotify_mark *mark, void *obj,
8267232522eSAmir Goldstein 		      unsigned int obj_type, int add_flags)
827d5a335b8SLino Sanfilippo {
828d5a335b8SLino Sanfilippo 	int ret;
8297b129323SJan Kara 	struct fsnotify_group *group = mark->group;
8307b129323SJan Kara 
83143b245a7SAmir Goldstein 	fsnotify_group_lock(group);
832687c217cSAmir Goldstein 	ret = fsnotify_add_mark_locked(mark, obj, obj_type, add_flags);
83343b245a7SAmir Goldstein 	fsnotify_group_unlock(group);
834d5a335b8SLino Sanfilippo 	return ret;
835d5a335b8SLino Sanfilippo }
836b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_add_mark);
837d5a335b8SLino Sanfilippo 
8385444e298SEric Paris /*
8390809ab69SJan Kara  * Given a list of marks, find the mark associated with given group. If found
8400809ab69SJan Kara  * take a reference to that mark and return it, else return NULL.
8410809ab69SJan Kara  */
842687c217cSAmir Goldstein struct fsnotify_mark *fsnotify_find_mark(void *obj, unsigned int obj_type,
8430809ab69SJan Kara 					 struct fsnotify_group *group)
8440809ab69SJan Kara {
845687c217cSAmir Goldstein 	fsnotify_connp_t *connp = fsnotify_object_connp(obj, obj_type);
84608991e83SJan Kara 	struct fsnotify_mark_connector *conn;
8470809ab69SJan Kara 	struct fsnotify_mark *mark;
8480809ab69SJan Kara 
849687c217cSAmir Goldstein 	if (!connp)
850687c217cSAmir Goldstein 		return NULL;
851687c217cSAmir Goldstein 
85208991e83SJan Kara 	conn = fsnotify_grab_connector(connp);
8539dd813c1SJan Kara 	if (!conn)
8549dd813c1SJan Kara 		return NULL;
8559dd813c1SJan Kara 
8569dd813c1SJan Kara 	hlist_for_each_entry(mark, &conn->list, obj_list) {
8576b3f05d2SJan Kara 		if (mark->group == group &&
8586b3f05d2SJan Kara 		    (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
8590809ab69SJan Kara 			fsnotify_get_mark(mark);
86004662cabSJan Kara 			spin_unlock(&conn->lock);
8610809ab69SJan Kara 			return mark;
8620809ab69SJan Kara 		}
8630809ab69SJan Kara 	}
86404662cabSJan Kara 	spin_unlock(&conn->lock);
8650809ab69SJan Kara 	return NULL;
8660809ab69SJan Kara }
867b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_find_mark);
8680809ab69SJan Kara 
869d6f7b98bSAmir Goldstein /* Clear any marks in a group with given type mask */
87018f2e0d3SJan Kara void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
871ad69cd99SAmir Goldstein 				   unsigned int obj_type)
8725444e298SEric Paris {
8735444e298SEric Paris 	struct fsnotify_mark *lmark, *mark;
8748f2f3eb5SJan Kara 	LIST_HEAD(to_free);
8752e37c6caSJan Kara 	struct list_head *head = &to_free;
8765444e298SEric Paris 
8772e37c6caSJan Kara 	/* Skip selection step if we want to clear all marks. */
878ad69cd99SAmir Goldstein 	if (obj_type == FSNOTIFY_OBJ_TYPE_ANY) {
8792e37c6caSJan Kara 		head = &group->marks_list;
8802e37c6caSJan Kara 		goto clear;
8812e37c6caSJan Kara 	}
8828f2f3eb5SJan Kara 	/*
8838f2f3eb5SJan Kara 	 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
8848f2f3eb5SJan Kara 	 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
8858f2f3eb5SJan Kara 	 * to_free list so we have to use mark_mutex even when accessing that
8868f2f3eb5SJan Kara 	 * list. And freeing mark requires us to drop mark_mutex. So we can
8878f2f3eb5SJan Kara 	 * reliably free only the first mark in the list. That's why we first
8888f2f3eb5SJan Kara 	 * move marks to free to to_free list in one go and then free marks in
8898f2f3eb5SJan Kara 	 * to_free list one by one.
8908f2f3eb5SJan Kara 	 */
89143b245a7SAmir Goldstein 	fsnotify_group_lock(group);
8925444e298SEric Paris 	list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
893ad69cd99SAmir Goldstein 		if (mark->connector->type == obj_type)
8948f2f3eb5SJan Kara 			list_move(&mark->g_list, &to_free);
8954d92604cSEric Paris 	}
89643b245a7SAmir Goldstein 	fsnotify_group_unlock(group);
8978f2f3eb5SJan Kara 
8982e37c6caSJan Kara clear:
8998f2f3eb5SJan Kara 	while (1) {
90043b245a7SAmir Goldstein 		fsnotify_group_lock(group);
9012e37c6caSJan Kara 		if (list_empty(head)) {
90243b245a7SAmir Goldstein 			fsnotify_group_unlock(group);
9038f2f3eb5SJan Kara 			break;
9048f2f3eb5SJan Kara 		}
9052e37c6caSJan Kara 		mark = list_first_entry(head, struct fsnotify_mark, g_list);
9068f2f3eb5SJan Kara 		fsnotify_get_mark(mark);
9074712e722SJan Kara 		fsnotify_detach_mark(mark);
90843b245a7SAmir Goldstein 		fsnotify_group_unlock(group);
9094712e722SJan Kara 		fsnotify_free_mark(mark);
9108f2f3eb5SJan Kara 		fsnotify_put_mark(mark);
9118f2f3eb5SJan Kara 	}
9125444e298SEric Paris }
9135444e298SEric Paris 
9149b6e5434SAmir Goldstein /* Destroy all marks attached to an object via connector */
9159b6e5434SAmir Goldstein void fsnotify_destroy_marks(fsnotify_connp_t *connp)
9160810b4f9SJan Kara {
91708991e83SJan Kara 	struct fsnotify_mark_connector *conn;
9186b3f05d2SJan Kara 	struct fsnotify_mark *mark, *old_mark = NULL;
919721fb6fbSJan Kara 	void *objp;
920721fb6fbSJan Kara 	unsigned int type;
9210810b4f9SJan Kara 
9226b3f05d2SJan Kara 	conn = fsnotify_grab_connector(connp);
9236b3f05d2SJan Kara 	if (!conn)
9246b3f05d2SJan Kara 		return;
9250810b4f9SJan Kara 	/*
9260810b4f9SJan Kara 	 * We have to be careful since we can race with e.g.
9276b3f05d2SJan Kara 	 * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the
9286b3f05d2SJan Kara 	 * list can get modified. However we are holding mark reference and
9296b3f05d2SJan Kara 	 * thus our mark cannot be removed from obj_list so we can continue
9306b3f05d2SJan Kara 	 * iteration after regaining conn->lock.
9310810b4f9SJan Kara 	 */
9326b3f05d2SJan Kara 	hlist_for_each_entry(mark, &conn->list, obj_list) {
9330810b4f9SJan Kara 		fsnotify_get_mark(mark);
93404662cabSJan Kara 		spin_unlock(&conn->lock);
9356b3f05d2SJan Kara 		if (old_mark)
9366b3f05d2SJan Kara 			fsnotify_put_mark(old_mark);
9376b3f05d2SJan Kara 		old_mark = mark;
9380810b4f9SJan Kara 		fsnotify_destroy_mark(mark, mark->group);
9396b3f05d2SJan Kara 		spin_lock(&conn->lock);
9400810b4f9SJan Kara 	}
9416b3f05d2SJan Kara 	/*
9426b3f05d2SJan Kara 	 * Detach list from object now so that we don't pin inode until all
9436b3f05d2SJan Kara 	 * mark references get dropped. It would lead to strange results such
9446b3f05d2SJan Kara 	 * as delaying inode deletion or blocking unmount.
9456b3f05d2SJan Kara 	 */
946721fb6fbSJan Kara 	objp = fsnotify_detach_connector_from_object(conn, &type);
9476b3f05d2SJan Kara 	spin_unlock(&conn->lock);
9486b3f05d2SJan Kara 	if (old_mark)
9496b3f05d2SJan Kara 		fsnotify_put_mark(old_mark);
950721fb6fbSJan Kara 	fsnotify_drop_object(type, objp);
9510810b4f9SJan Kara }
9520810b4f9SJan Kara 
9535444e298SEric Paris /*
9545444e298SEric Paris  * Nothing fancy, just initialize lists and locks and counters.
9555444e298SEric Paris  */
9565444e298SEric Paris void fsnotify_init_mark(struct fsnotify_mark *mark,
957054c636eSJan Kara 			struct fsnotify_group *group)
9585444e298SEric Paris {
959ba643f04SEric Paris 	memset(mark, 0, sizeof(*mark));
9605444e298SEric Paris 	spin_lock_init(&mark->lock);
961ab97f873SElena Reshetova 	refcount_set(&mark->refcnt, 1);
9627b129323SJan Kara 	fsnotify_get_group(group);
9637b129323SJan Kara 	mark->group = group;
964b1da6a51SJan Kara 	WRITE_ONCE(mark->connector, NULL);
9655444e298SEric Paris }
966b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_init_mark);
96713d34ac6SJeff Layton 
96835e48176SJan Kara /*
96935e48176SJan Kara  * Destroy all marks in destroy_list, waits for SRCU period to finish before
97035e48176SJan Kara  * actually freeing marks.
97135e48176SJan Kara  */
972f09b04a0SJan Kara static void fsnotify_mark_destroy_workfn(struct work_struct *work)
97313d34ac6SJeff Layton {
97413d34ac6SJeff Layton 	struct fsnotify_mark *mark, *next;
97513d34ac6SJeff Layton 	struct list_head private_destroy_list;
97613d34ac6SJeff Layton 
97713d34ac6SJeff Layton 	spin_lock(&destroy_lock);
97813d34ac6SJeff Layton 	/* exchange the list head */
97913d34ac6SJeff Layton 	list_replace_init(&destroy_list, &private_destroy_list);
98013d34ac6SJeff Layton 	spin_unlock(&destroy_lock);
98113d34ac6SJeff Layton 
98213d34ac6SJeff Layton 	synchronize_srcu(&fsnotify_mark_srcu);
98313d34ac6SJeff Layton 
98413d34ac6SJeff Layton 	list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
98513d34ac6SJeff Layton 		list_del_init(&mark->g_list);
9866b3f05d2SJan Kara 		fsnotify_final_mark_destroy(mark);
98713d34ac6SJeff Layton 	}
98813d34ac6SJeff Layton }
98935e48176SJan Kara 
990f09b04a0SJan Kara /* Wait for all marks queued for destruction to be actually destroyed */
991f09b04a0SJan Kara void fsnotify_wait_marks_destroyed(void)
99235e48176SJan Kara {
993f09b04a0SJan Kara 	flush_delayed_work(&reaper_work);
99435e48176SJan Kara }
995b72679eeSTrond Myklebust EXPORT_SYMBOL_GPL(fsnotify_wait_marks_destroyed);
996