• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
4  */
5 
6 /*
7  * fsnotify inode mark locking/lifetime/and refcnting
8  *
9  * REFCNT:
10  * The group->recnt and mark->refcnt tell how many "things" in the kernel
11  * currently are referencing the objects. Both kind of objects typically will
12  * live inside the kernel with a refcnt of 2, one for its creation and one for
13  * the reference a group and a mark hold to each other.
14  * If you are holding the appropriate locks, you can take a reference and the
15  * object itself is guaranteed to survive until the reference is dropped.
16  *
17  * LOCKING:
18  * There are 3 locks involved with fsnotify inode marks and they MUST be taken
19  * in order as follows:
20  *
21  * group->mark_mutex
22  * mark->lock
23  * mark->connector->lock
24  *
25  * group->mark_mutex protects the marks_list anchored inside a given group and
26  * each mark is hooked via the g_list.  It also protects the groups private
27  * data (i.e group limits).
28 
29  * mark->lock protects the marks attributes like its masks and flags.
30  * Furthermore it protects the access to a reference of the group that the mark
31  * is assigned to as well as the access to a reference of the inode/vfsmount
32  * that is being watched by the mark.
33  *
34  * mark->connector->lock protects the list of marks anchored inside an
35  * inode / vfsmount and each mark is hooked via the i_list.
36  *
37  * A list of notification marks relating to inode / mnt is contained in
38  * fsnotify_mark_connector. That structure is alive as long as there are any
39  * marks in the list and is also protected by fsnotify_mark_srcu. A mark gets
40  * detached from fsnotify_mark_connector when last reference to the mark is
41  * dropped.  Thus having mark reference is enough to protect mark->connector
42  * pointer and to make sure fsnotify_mark_connector cannot disappear. Also
43  * because we remove mark from g_list before dropping mark reference associated
44  * with that, any mark found through g_list is guaranteed to have
45  * mark->connector set until we drop group->mark_mutex.
46  *
47  * LIFETIME:
48  * Inode marks survive between when they are added to an inode and when their
49  * refcnt==0. Marks are also protected by fsnotify_mark_srcu.
50  *
51  * The inode mark can be cleared for a number of different reasons including:
52  * - The inode is unlinked for the last time.  (fsnotify_inode_remove)
53  * - The inode is being evicted from cache. (fsnotify_inode_delete)
54  * - The fs the inode is on is unmounted.  (fsnotify_inode_delete/fsnotify_unmount_inodes)
55  * - Something explicitly requests that it be removed.  (fsnotify_destroy_mark)
56  * - The fsnotify_group associated with the mark is going away and all such marks
57  *   need to be cleaned up. (fsnotify_clear_marks_by_group)
58  *
59  * This has the very interesting property of being able to run concurrently with
60  * any (or all) other directions.
61  */
62 
63 #include <linux/fs.h>
64 #include <linux/init.h>
65 #include <linux/kernel.h>
66 #include <linux/kthread.h>
67 #include <linux/module.h>
68 #include <linux/mutex.h>
69 #include <linux/slab.h>
70 #include <linux/spinlock.h>
71 #include <linux/srcu.h>
72 #include <linux/ratelimit.h>
73 
74 #include <linux/atomic.h>
75 
76 #include <linux/fsnotify_backend.h>
77 #include "fsnotify.h"
78 
79 #define FSNOTIFY_REAPER_DELAY	(1)	/* 1 jiffy */
80 
81 struct srcu_struct fsnotify_mark_srcu;
82 struct kmem_cache *fsnotify_mark_connector_cachep;
83 
84 static DEFINE_SPINLOCK(destroy_lock);
85 static LIST_HEAD(destroy_list);
86 static struct fsnotify_mark_connector *connector_destroy_list;
87 
88 static void fsnotify_mark_destroy_workfn(struct work_struct *work);
89 static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
90 
91 static void fsnotify_connector_destroy_workfn(struct work_struct *work);
92 static DECLARE_WORK(connector_reaper_work, fsnotify_connector_destroy_workfn);
93 
fsnotify_get_mark(struct fsnotify_mark * mark)94 void fsnotify_get_mark(struct fsnotify_mark *mark)
95 {
96 	WARN_ON_ONCE(!refcount_read(&mark->refcnt));
97 	refcount_inc(&mark->refcnt);
98 }
99 
fsnotify_conn_mask_p(struct fsnotify_mark_connector * conn)100 static __u32 *fsnotify_conn_mask_p(struct fsnotify_mark_connector *conn)
101 {
102 	if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
103 		return &fsnotify_conn_inode(conn)->i_fsnotify_mask;
104 	else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT)
105 		return &fsnotify_conn_mount(conn)->mnt_fsnotify_mask;
106 	else if (conn->type == FSNOTIFY_OBJ_TYPE_SB)
107 		return &fsnotify_conn_sb(conn)->s_fsnotify_mask;
108 	return NULL;
109 }
110 
fsnotify_conn_mask(struct fsnotify_mark_connector * conn)111 __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn)
112 {
113 	if (WARN_ON(!fsnotify_valid_obj_type(conn->type)))
114 		return 0;
115 
116 	return *fsnotify_conn_mask_p(conn);
117 }
118 
__fsnotify_recalc_mask(struct fsnotify_mark_connector * conn)119 static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
120 {
121 	u32 new_mask = 0;
122 	struct fsnotify_mark *mark;
123 
124 	assert_spin_locked(&conn->lock);
125 	/* We can get detached connector here when inode is getting unlinked. */
126 	if (!fsnotify_valid_obj_type(conn->type))
127 		return;
128 	hlist_for_each_entry(mark, &conn->list, obj_list) {
129 		if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)
130 			new_mask |= mark->mask;
131 	}
132 	*fsnotify_conn_mask_p(conn) = new_mask;
133 }
134 
fsnotify_conn_watches_children(struct fsnotify_mark_connector * conn)135 static bool fsnotify_conn_watches_children(
136 					struct fsnotify_mark_connector *conn)
137 {
138 	if (conn->type != FSNOTIFY_OBJ_TYPE_INODE)
139 		return false;
140 
141 	return fsnotify_inode_watches_children(fsnotify_conn_inode(conn));
142 }
143 
fsnotify_conn_set_children_dentry_flags(struct fsnotify_mark_connector * conn)144 static void fsnotify_conn_set_children_dentry_flags(
145 					struct fsnotify_mark_connector *conn)
146 {
147 	if (conn->type != FSNOTIFY_OBJ_TYPE_INODE)
148 		return;
149 
150 	fsnotify_set_children_dentry_flags(fsnotify_conn_inode(conn));
151 }
152 
153 /*
154  * Calculate mask of events for a list of marks. The caller must make sure
155  * connector and connector->obj cannot disappear under us.  Callers achieve
156  * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
157  * list.
158  */
fsnotify_recalc_mask(struct fsnotify_mark_connector * conn)159 void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
160 {
161 	bool update_children;
162 
163 	if (!conn)
164 		return;
165 
166 	spin_lock(&conn->lock);
167 	update_children = !fsnotify_conn_watches_children(conn);
168 	__fsnotify_recalc_mask(conn);
169 	update_children &= fsnotify_conn_watches_children(conn);
170 	spin_unlock(&conn->lock);
171 	/*
172 	 * Set children's PARENT_WATCHED flags only if parent started watching.
173 	 * When parent stops watching, we clear false positive PARENT_WATCHED
174 	 * flags lazily in __fsnotify_parent().
175 	 */
176 	if (update_children)
177 		fsnotify_conn_set_children_dentry_flags(conn);
178 }
179 
180 /* Free all connectors queued for freeing once SRCU period ends */
fsnotify_connector_destroy_workfn(struct work_struct * work)181 static void fsnotify_connector_destroy_workfn(struct work_struct *work)
182 {
183 	struct fsnotify_mark_connector *conn, *free;
184 
185 	spin_lock(&destroy_lock);
186 	conn = connector_destroy_list;
187 	connector_destroy_list = NULL;
188 	spin_unlock(&destroy_lock);
189 
190 	synchronize_srcu(&fsnotify_mark_srcu);
191 	while (conn) {
192 		free = conn;
193 		conn = conn->destroy_next;
194 		kmem_cache_free(fsnotify_mark_connector_cachep, free);
195 	}
196 }
197 
fsnotify_detach_connector_from_object(struct fsnotify_mark_connector * conn,unsigned int * type)198 static void *fsnotify_detach_connector_from_object(
199 					struct fsnotify_mark_connector *conn,
200 					unsigned int *type)
201 {
202 	struct inode *inode = NULL;
203 
204 	*type = conn->type;
205 	if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED)
206 		return NULL;
207 
208 	if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
209 		inode = fsnotify_conn_inode(conn);
210 		inode->i_fsnotify_mask = 0;
211 		atomic_long_inc(&inode->i_sb->s_fsnotify_inode_refs);
212 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
213 		fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0;
214 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) {
215 		fsnotify_conn_sb(conn)->s_fsnotify_mask = 0;
216 	}
217 
218 	rcu_assign_pointer(*(conn->obj), NULL);
219 	conn->obj = NULL;
220 	conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
221 
222 	return inode;
223 }
224 
fsnotify_final_mark_destroy(struct fsnotify_mark * mark)225 static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark)
226 {
227 	struct fsnotify_group *group = mark->group;
228 
229 	if (WARN_ON_ONCE(!group))
230 		return;
231 	group->ops->free_mark(mark);
232 	fsnotify_put_group(group);
233 }
234 
235 /* Drop object reference originally held by a connector */
fsnotify_drop_object(unsigned int type,void * objp)236 static void fsnotify_drop_object(unsigned int type, void *objp)
237 {
238 	struct inode *inode;
239 	struct super_block *sb;
240 
241 	if (!objp)
242 		return;
243 	/* Currently only inode references are passed to be dropped */
244 	if (WARN_ON_ONCE(type != FSNOTIFY_OBJ_TYPE_INODE))
245 		return;
246 	inode = objp;
247 	sb = inode->i_sb;
248 	iput(inode);
249 	if (atomic_long_dec_and_test(&sb->s_fsnotify_inode_refs))
250 		wake_up_var(&sb->s_fsnotify_inode_refs);
251 }
252 
fsnotify_put_mark(struct fsnotify_mark * mark)253 void fsnotify_put_mark(struct fsnotify_mark *mark)
254 {
255 	struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector);
256 	void *objp = NULL;
257 	unsigned int type = FSNOTIFY_OBJ_TYPE_DETACHED;
258 	bool free_conn = false;
259 
260 	/* Catch marks that were actually never attached to object */
261 	if (!conn) {
262 		if (refcount_dec_and_test(&mark->refcnt))
263 			fsnotify_final_mark_destroy(mark);
264 		return;
265 	}
266 
267 	/*
268 	 * We have to be careful so that traversals of obj_list under lock can
269 	 * safely grab mark reference.
270 	 */
271 	if (!refcount_dec_and_lock(&mark->refcnt, &conn->lock))
272 		return;
273 
274 	hlist_del_init_rcu(&mark->obj_list);
275 	if (hlist_empty(&conn->list)) {
276 		objp = fsnotify_detach_connector_from_object(conn, &type);
277 		free_conn = true;
278 	} else {
279 		__fsnotify_recalc_mask(conn);
280 	}
281 	WRITE_ONCE(mark->connector, NULL);
282 	spin_unlock(&conn->lock);
283 
284 	fsnotify_drop_object(type, objp);
285 
286 	if (free_conn) {
287 		spin_lock(&destroy_lock);
288 		conn->destroy_next = connector_destroy_list;
289 		connector_destroy_list = conn;
290 		spin_unlock(&destroy_lock);
291 		queue_work(system_unbound_wq, &connector_reaper_work);
292 	}
293 	/*
294 	 * Note that we didn't update flags telling whether inode cares about
295 	 * what's happening with children. We update these flags from
296 	 * __fsnotify_parent() lazily when next event happens on one of our
297 	 * children.
298 	 */
299 	spin_lock(&destroy_lock);
300 	list_add(&mark->g_list, &destroy_list);
301 	spin_unlock(&destroy_lock);
302 	queue_delayed_work(system_unbound_wq, &reaper_work,
303 			   FSNOTIFY_REAPER_DELAY);
304 }
305 EXPORT_SYMBOL_GPL(fsnotify_put_mark);
306 
307 /*
308  * Get mark reference when we found the mark via lockless traversal of object
309  * list. Mark can be already removed from the list by now and on its way to be
310  * destroyed once SRCU period ends.
311  *
312  * Also pin the group so it doesn't disappear under us.
313  */
fsnotify_get_mark_safe(struct fsnotify_mark * mark)314 static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark)
315 {
316 	if (!mark)
317 		return true;
318 
319 	if (refcount_inc_not_zero(&mark->refcnt)) {
320 		spin_lock(&mark->lock);
321 		if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) {
322 			/* mark is attached, group is still alive then */
323 			atomic_inc(&mark->group->user_waits);
324 			spin_unlock(&mark->lock);
325 			return true;
326 		}
327 		spin_unlock(&mark->lock);
328 		fsnotify_put_mark(mark);
329 	}
330 	return false;
331 }
332 
333 /*
334  * Puts marks and wakes up group destruction if necessary.
335  *
336  * Pairs with fsnotify_get_mark_safe()
337  */
fsnotify_put_mark_wake(struct fsnotify_mark * mark)338 static void fsnotify_put_mark_wake(struct fsnotify_mark *mark)
339 {
340 	if (mark) {
341 		struct fsnotify_group *group = mark->group;
342 
343 		fsnotify_put_mark(mark);
344 		/*
345 		 * We abuse notification_waitq on group shutdown for waiting for
346 		 * all marks pinned when waiting for userspace.
347 		 */
348 		if (atomic_dec_and_test(&group->user_waits) && group->shutdown)
349 			wake_up(&group->notification_waitq);
350 	}
351 }
352 
fsnotify_prepare_user_wait(struct fsnotify_iter_info * iter_info)353 bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info)
354 	__releases(&fsnotify_mark_srcu)
355 {
356 	int type;
357 
358 	fsnotify_foreach_obj_type(type) {
359 		/* This can fail if mark is being removed */
360 		if (!fsnotify_get_mark_safe(iter_info->marks[type])) {
361 			__release(&fsnotify_mark_srcu);
362 			goto fail;
363 		}
364 	}
365 
366 	/*
367 	 * Now that both marks are pinned by refcount in the inode / vfsmount
368 	 * lists, we can drop SRCU lock, and safely resume the list iteration
369 	 * once userspace returns.
370 	 */
371 	srcu_read_unlock(&fsnotify_mark_srcu, iter_info->srcu_idx);
372 
373 	return true;
374 
375 fail:
376 	for (type--; type >= 0; type--)
377 		fsnotify_put_mark_wake(iter_info->marks[type]);
378 	return false;
379 }
380 
fsnotify_finish_user_wait(struct fsnotify_iter_info * iter_info)381 void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info)
382 	__acquires(&fsnotify_mark_srcu)
383 {
384 	int type;
385 
386 	iter_info->srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
387 	fsnotify_foreach_obj_type(type)
388 		fsnotify_put_mark_wake(iter_info->marks[type]);
389 }
390 
391 /*
392  * Mark mark as detached, remove it from group list. Mark still stays in object
393  * list until its last reference is dropped. Note that we rely on mark being
394  * removed from group list before corresponding reference to it is dropped. In
395  * particular we rely on mark->connector being valid while we hold
396  * group->mark_mutex if we found the mark through g_list.
397  *
398  * Must be called with group->mark_mutex held. The caller must either hold
399  * reference to the mark or be protected by fsnotify_mark_srcu.
400  */
fsnotify_detach_mark(struct fsnotify_mark * mark)401 void fsnotify_detach_mark(struct fsnotify_mark *mark)
402 {
403 	struct fsnotify_group *group = mark->group;
404 
405 	WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex));
406 	WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) &&
407 		     refcount_read(&mark->refcnt) < 1 +
408 			!!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED));
409 
410 	spin_lock(&mark->lock);
411 	/* something else already called this function on this mark */
412 	if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
413 		spin_unlock(&mark->lock);
414 		return;
415 	}
416 	mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
417 	list_del_init(&mark->g_list);
418 	spin_unlock(&mark->lock);
419 
420 	atomic_dec(&group->num_marks);
421 
422 	/* Drop mark reference acquired in fsnotify_add_mark_locked() */
423 	fsnotify_put_mark(mark);
424 }
425 
426 /*
427  * Free fsnotify mark. The mark is actually only marked as being freed.  The
428  * freeing is actually happening only once last reference to the mark is
429  * dropped from a workqueue which first waits for srcu period end.
430  *
431  * Caller must have a reference to the mark or be protected by
432  * fsnotify_mark_srcu.
433  */
fsnotify_free_mark(struct fsnotify_mark * mark)434 void fsnotify_free_mark(struct fsnotify_mark *mark)
435 {
436 	struct fsnotify_group *group = mark->group;
437 
438 	spin_lock(&mark->lock);
439 	/* something else already called this function on this mark */
440 	if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
441 		spin_unlock(&mark->lock);
442 		return;
443 	}
444 	mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
445 	spin_unlock(&mark->lock);
446 
447 	/*
448 	 * Some groups like to know that marks are being freed.  This is a
449 	 * callback to the group function to let it know that this mark
450 	 * is being freed.
451 	 */
452 	if (group->ops->freeing_mark)
453 		group->ops->freeing_mark(mark, group);
454 }
455 
fsnotify_destroy_mark(struct fsnotify_mark * mark,struct fsnotify_group * group)456 void fsnotify_destroy_mark(struct fsnotify_mark *mark,
457 			   struct fsnotify_group *group)
458 {
459 	mutex_lock(&group->mark_mutex);
460 	fsnotify_detach_mark(mark);
461 	mutex_unlock(&group->mark_mutex);
462 	fsnotify_free_mark(mark);
463 }
464 EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
465 
466 /*
467  * Sorting function for lists of fsnotify marks.
468  *
469  * Fanotify supports different notification classes (reflected as priority of
470  * notification group). Events shall be passed to notification groups in
471  * decreasing priority order. To achieve this marks in notification lists for
472  * inodes and vfsmounts are sorted so that priorities of corresponding groups
473  * are descending.
474  *
475  * Furthermore correct handling of the ignore mask requires processing inode
476  * and vfsmount marks of each group together. Using the group address as
477  * further sort criterion provides a unique sorting order and thus we can
478  * merge inode and vfsmount lists of marks in linear time and find groups
479  * present in both lists.
480  *
481  * A return value of 1 signifies that b has priority over a.
482  * A return value of 0 signifies that the two marks have to be handled together.
483  * A return value of -1 signifies that a has priority over b.
484  */
fsnotify_compare_groups(struct fsnotify_group * a,struct fsnotify_group * b)485 int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
486 {
487 	if (a == b)
488 		return 0;
489 	if (!a)
490 		return 1;
491 	if (!b)
492 		return -1;
493 	if (a->priority < b->priority)
494 		return 1;
495 	if (a->priority > b->priority)
496 		return -1;
497 	if (a < b)
498 		return 1;
499 	return -1;
500 }
501 
fsnotify_attach_connector_to_object(fsnotify_connp_t * connp,unsigned int type,__kernel_fsid_t * fsid)502 static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
503 					       unsigned int type,
504 					       __kernel_fsid_t *fsid)
505 {
506 	struct inode *inode = NULL;
507 	struct fsnotify_mark_connector *conn;
508 
509 	conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
510 	if (!conn)
511 		return -ENOMEM;
512 	spin_lock_init(&conn->lock);
513 	INIT_HLIST_HEAD(&conn->list);
514 	conn->type = type;
515 	conn->obj = connp;
516 	/* Cache fsid of filesystem containing the object */
517 	if (fsid) {
518 		conn->fsid = *fsid;
519 		conn->flags = FSNOTIFY_CONN_FLAG_HAS_FSID;
520 	} else {
521 		conn->fsid.val[0] = conn->fsid.val[1] = 0;
522 		conn->flags = 0;
523 	}
524 	if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
525 		inode = igrab(fsnotify_conn_inode(conn));
526 	/*
527 	 * cmpxchg() provides the barrier so that readers of *connp can see
528 	 * only initialized structure
529 	 */
530 	if (cmpxchg(connp, NULL, conn)) {
531 		/* Someone else created list structure for us */
532 		if (inode)
533 			iput(inode);
534 		kmem_cache_free(fsnotify_mark_connector_cachep, conn);
535 	}
536 
537 	return 0;
538 }
539 
540 /*
541  * Get mark connector, make sure it is alive and return with its lock held.
542  * This is for users that get connector pointer from inode or mount. Users that
543  * hold reference to a mark on the list may directly lock connector->lock as
544  * they are sure list cannot go away under them.
545  */
fsnotify_grab_connector(fsnotify_connp_t * connp)546 static struct fsnotify_mark_connector *fsnotify_grab_connector(
547 						fsnotify_connp_t *connp)
548 {
549 	struct fsnotify_mark_connector *conn;
550 	int idx;
551 
552 	idx = srcu_read_lock(&fsnotify_mark_srcu);
553 	conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
554 	if (!conn)
555 		goto out;
556 	spin_lock(&conn->lock);
557 	if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) {
558 		spin_unlock(&conn->lock);
559 		srcu_read_unlock(&fsnotify_mark_srcu, idx);
560 		return NULL;
561 	}
562 out:
563 	srcu_read_unlock(&fsnotify_mark_srcu, idx);
564 	return conn;
565 }
566 
567 /*
568  * Add mark into proper place in given list of marks. These marks may be used
569  * for the fsnotify backend to determine which event types should be delivered
570  * to which group and for which inodes. These marks are ordered according to
571  * priority, highest number first, and then by the group's location in memory.
572  */
fsnotify_add_mark_list(struct fsnotify_mark * mark,fsnotify_connp_t * connp,unsigned int type,int allow_dups,__kernel_fsid_t * fsid)573 static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
574 				  fsnotify_connp_t *connp, unsigned int type,
575 				  int allow_dups, __kernel_fsid_t *fsid)
576 {
577 	struct fsnotify_mark *lmark, *last = NULL;
578 	struct fsnotify_mark_connector *conn;
579 	int cmp;
580 	int err = 0;
581 
582 	if (WARN_ON(!fsnotify_valid_obj_type(type)))
583 		return -EINVAL;
584 
585 	/* Backend is expected to check for zero fsid (e.g. tmpfs) */
586 	if (fsid && WARN_ON_ONCE(!fsid->val[0] && !fsid->val[1]))
587 		return -ENODEV;
588 
589 restart:
590 	spin_lock(&mark->lock);
591 	conn = fsnotify_grab_connector(connp);
592 	if (!conn) {
593 		spin_unlock(&mark->lock);
594 		err = fsnotify_attach_connector_to_object(connp, type, fsid);
595 		if (err)
596 			return err;
597 		goto restart;
598 	} else if (fsid && !(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID)) {
599 		conn->fsid = *fsid;
600 		/* Pairs with smp_rmb() in fanotify_get_fsid() */
601 		smp_wmb();
602 		conn->flags |= FSNOTIFY_CONN_FLAG_HAS_FSID;
603 	} else if (fsid && (conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID) &&
604 		   (fsid->val[0] != conn->fsid.val[0] ||
605 		    fsid->val[1] != conn->fsid.val[1])) {
606 		/*
607 		 * Backend is expected to check for non uniform fsid
608 		 * (e.g. btrfs), but maybe we missed something?
609 		 * Only allow setting conn->fsid once to non zero fsid.
610 		 * inotify and non-fid fanotify groups do not set nor test
611 		 * conn->fsid.
612 		 */
613 		pr_warn_ratelimited("%s: fsid mismatch on object of type %u: "
614 				    "%x.%x != %x.%x\n", __func__, conn->type,
615 				    fsid->val[0], fsid->val[1],
616 				    conn->fsid.val[0], conn->fsid.val[1]);
617 		err = -EXDEV;
618 		goto out_err;
619 	}
620 
621 	/* is mark the first mark? */
622 	if (hlist_empty(&conn->list)) {
623 		hlist_add_head_rcu(&mark->obj_list, &conn->list);
624 		goto added;
625 	}
626 
627 	/* should mark be in the middle of the current list? */
628 	hlist_for_each_entry(lmark, &conn->list, obj_list) {
629 		last = lmark;
630 
631 		if ((lmark->group == mark->group) &&
632 		    (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) &&
633 		    !allow_dups) {
634 			err = -EEXIST;
635 			goto out_err;
636 		}
637 
638 		cmp = fsnotify_compare_groups(lmark->group, mark->group);
639 		if (cmp >= 0) {
640 			hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
641 			goto added;
642 		}
643 	}
644 
645 	BUG_ON(last == NULL);
646 	/* mark should be the last entry.  last is the current last entry */
647 	hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
648 added:
649 	/*
650 	 * Since connector is attached to object using cmpxchg() we are
651 	 * guaranteed that connector initialization is fully visible by anyone
652 	 * seeing mark->connector set.
653 	 */
654 	WRITE_ONCE(mark->connector, conn);
655 out_err:
656 	spin_unlock(&conn->lock);
657 	spin_unlock(&mark->lock);
658 	return err;
659 }
660 
661 /*
662  * Attach an initialized mark to a given group and fs object.
663  * These marks may be used for the fsnotify backend to determine which
664  * event types should be delivered to which group.
665  */
fsnotify_add_mark_locked(struct fsnotify_mark * mark,fsnotify_connp_t * connp,unsigned int type,int allow_dups,__kernel_fsid_t * fsid)666 int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
667 			     fsnotify_connp_t *connp, unsigned int type,
668 			     int allow_dups, __kernel_fsid_t *fsid)
669 {
670 	struct fsnotify_group *group = mark->group;
671 	int ret = 0;
672 
673 	BUG_ON(!mutex_is_locked(&group->mark_mutex));
674 
675 	/*
676 	 * LOCKING ORDER!!!!
677 	 * group->mark_mutex
678 	 * mark->lock
679 	 * mark->connector->lock
680 	 */
681 	spin_lock(&mark->lock);
682 	mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
683 
684 	list_add(&mark->g_list, &group->marks_list);
685 	atomic_inc(&group->num_marks);
686 	fsnotify_get_mark(mark); /* for g_list */
687 	spin_unlock(&mark->lock);
688 
689 	ret = fsnotify_add_mark_list(mark, connp, type, allow_dups, fsid);
690 	if (ret)
691 		goto err;
692 
693 	if (mark->mask)
694 		fsnotify_recalc_mask(mark->connector);
695 
696 	return ret;
697 err:
698 	spin_lock(&mark->lock);
699 	mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE |
700 			 FSNOTIFY_MARK_FLAG_ATTACHED);
701 	list_del_init(&mark->g_list);
702 	spin_unlock(&mark->lock);
703 	atomic_dec(&group->num_marks);
704 
705 	fsnotify_put_mark(mark);
706 	return ret;
707 }
708 
fsnotify_add_mark(struct fsnotify_mark * mark,fsnotify_connp_t * connp,unsigned int type,int allow_dups,__kernel_fsid_t * fsid)709 int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
710 		      unsigned int type, int allow_dups, __kernel_fsid_t *fsid)
711 {
712 	int ret;
713 	struct fsnotify_group *group = mark->group;
714 
715 	mutex_lock(&group->mark_mutex);
716 	ret = fsnotify_add_mark_locked(mark, connp, type, allow_dups, fsid);
717 	mutex_unlock(&group->mark_mutex);
718 	return ret;
719 }
720 EXPORT_SYMBOL_GPL(fsnotify_add_mark);
721 
722 /*
723  * Given a list of marks, find the mark associated with given group. If found
724  * take a reference to that mark and return it, else return NULL.
725  */
fsnotify_find_mark(fsnotify_connp_t * connp,struct fsnotify_group * group)726 struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
727 					 struct fsnotify_group *group)
728 {
729 	struct fsnotify_mark_connector *conn;
730 	struct fsnotify_mark *mark;
731 
732 	conn = fsnotify_grab_connector(connp);
733 	if (!conn)
734 		return NULL;
735 
736 	hlist_for_each_entry(mark, &conn->list, obj_list) {
737 		if (mark->group == group &&
738 		    (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
739 			fsnotify_get_mark(mark);
740 			spin_unlock(&conn->lock);
741 			return mark;
742 		}
743 	}
744 	spin_unlock(&conn->lock);
745 	return NULL;
746 }
747 EXPORT_SYMBOL_GPL(fsnotify_find_mark);
748 
749 /* Clear any marks in a group with given type mask */
fsnotify_clear_marks_by_group(struct fsnotify_group * group,unsigned int type_mask)750 void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
751 				   unsigned int type_mask)
752 {
753 	struct fsnotify_mark *lmark, *mark;
754 	LIST_HEAD(to_free);
755 	struct list_head *head = &to_free;
756 
757 	/* Skip selection step if we want to clear all marks. */
758 	if (type_mask == FSNOTIFY_OBJ_ALL_TYPES_MASK) {
759 		head = &group->marks_list;
760 		goto clear;
761 	}
762 	/*
763 	 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
764 	 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
765 	 * to_free list so we have to use mark_mutex even when accessing that
766 	 * list. And freeing mark requires us to drop mark_mutex. So we can
767 	 * reliably free only the first mark in the list. That's why we first
768 	 * move marks to free to to_free list in one go and then free marks in
769 	 * to_free list one by one.
770 	 */
771 	mutex_lock(&group->mark_mutex);
772 	list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
773 		if ((1U << mark->connector->type) & type_mask)
774 			list_move(&mark->g_list, &to_free);
775 	}
776 	mutex_unlock(&group->mark_mutex);
777 
778 clear:
779 	while (1) {
780 		mutex_lock(&group->mark_mutex);
781 		if (list_empty(head)) {
782 			mutex_unlock(&group->mark_mutex);
783 			break;
784 		}
785 		mark = list_first_entry(head, struct fsnotify_mark, g_list);
786 		fsnotify_get_mark(mark);
787 		fsnotify_detach_mark(mark);
788 		mutex_unlock(&group->mark_mutex);
789 		fsnotify_free_mark(mark);
790 		fsnotify_put_mark(mark);
791 	}
792 }
793 
794 /* Destroy all marks attached to an object via connector */
fsnotify_destroy_marks(fsnotify_connp_t * connp)795 void fsnotify_destroy_marks(fsnotify_connp_t *connp)
796 {
797 	struct fsnotify_mark_connector *conn;
798 	struct fsnotify_mark *mark, *old_mark = NULL;
799 	void *objp;
800 	unsigned int type;
801 
802 	conn = fsnotify_grab_connector(connp);
803 	if (!conn)
804 		return;
805 	/*
806 	 * We have to be careful since we can race with e.g.
807 	 * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the
808 	 * list can get modified. However we are holding mark reference and
809 	 * thus our mark cannot be removed from obj_list so we can continue
810 	 * iteration after regaining conn->lock.
811 	 */
812 	hlist_for_each_entry(mark, &conn->list, obj_list) {
813 		fsnotify_get_mark(mark);
814 		spin_unlock(&conn->lock);
815 		if (old_mark)
816 			fsnotify_put_mark(old_mark);
817 		old_mark = mark;
818 		fsnotify_destroy_mark(mark, mark->group);
819 		spin_lock(&conn->lock);
820 	}
821 	/*
822 	 * Detach list from object now so that we don't pin inode until all
823 	 * mark references get dropped. It would lead to strange results such
824 	 * as delaying inode deletion or blocking unmount.
825 	 */
826 	objp = fsnotify_detach_connector_from_object(conn, &type);
827 	spin_unlock(&conn->lock);
828 	if (old_mark)
829 		fsnotify_put_mark(old_mark);
830 	fsnotify_drop_object(type, objp);
831 }
832 
833 /*
834  * Nothing fancy, just initialize lists and locks and counters.
835  */
fsnotify_init_mark(struct fsnotify_mark * mark,struct fsnotify_group * group)836 void fsnotify_init_mark(struct fsnotify_mark *mark,
837 			struct fsnotify_group *group)
838 {
839 	memset(mark, 0, sizeof(*mark));
840 	spin_lock_init(&mark->lock);
841 	refcount_set(&mark->refcnt, 1);
842 	fsnotify_get_group(group);
843 	mark->group = group;
844 	WRITE_ONCE(mark->connector, NULL);
845 }
846 EXPORT_SYMBOL_GPL(fsnotify_init_mark);
847 
848 /*
849  * Destroy all marks in destroy_list, waits for SRCU period to finish before
850  * actually freeing marks.
851  */
fsnotify_mark_destroy_workfn(struct work_struct * work)852 static void fsnotify_mark_destroy_workfn(struct work_struct *work)
853 {
854 	struct fsnotify_mark *mark, *next;
855 	struct list_head private_destroy_list;
856 
857 	spin_lock(&destroy_lock);
858 	/* exchange the list head */
859 	list_replace_init(&destroy_list, &private_destroy_list);
860 	spin_unlock(&destroy_lock);
861 
862 	synchronize_srcu(&fsnotify_mark_srcu);
863 
864 	list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
865 		list_del_init(&mark->g_list);
866 		fsnotify_final_mark_destroy(mark);
867 	}
868 }
869 
870 /* Wait for all marks queued for destruction to be actually destroyed */
fsnotify_wait_marks_destroyed(void)871 void fsnotify_wait_marks_destroyed(void)
872 {
873 	flush_delayed_work(&reaper_work);
874 }
875 EXPORT_SYMBOL_GPL(fsnotify_wait_marks_destroyed);
876