blob: 824095db5a3b155358ec842915b158532181b3ef [file] [log] [blame]
Eric Paris5444e292009-12-17 21:24:27 -05001/*
2 * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/*
20 * fsnotify inode mark locking/lifetime/and refcnting
21 *
22 * REFCNT:
Lino Sanfilippo9756b912013-07-08 15:59:46 -070023 * The group->recnt and mark->refcnt tell how many "things" in the kernel
24 * currently are referencing the objects. Both kind of objects typically will
25 * live inside the kernel with a refcnt of 2, one for its creation and one for
26 * the reference a group and a mark hold to each other.
27 * If you are holding the appropriate locks, you can take a reference and the
28 * object itself is guaranteed to survive until the reference is dropped.
Eric Paris5444e292009-12-17 21:24:27 -050029 *
30 * LOCKING:
Lino Sanfilippo9756b912013-07-08 15:59:46 -070031 * There are 3 locks involved with fsnotify inode marks and they MUST be taken
32 * in order as follows:
Eric Paris5444e292009-12-17 21:24:27 -050033 *
Lino Sanfilippo9756b912013-07-08 15:59:46 -070034 * group->mark_mutex
Eric Paris5444e292009-12-17 21:24:27 -050035 * mark->lock
Jan Kara04662ca2017-02-01 08:19:43 +010036 * mark->connector->lock
Eric Paris5444e292009-12-17 21:24:27 -050037 *
Lino Sanfilippo9756b912013-07-08 15:59:46 -070038 * group->mark_mutex protects the marks_list anchored inside a given group and
39 * each mark is hooked via the g_list. It also protects the groups private
40 * data (i.e group limits).
41
42 * mark->lock protects the marks attributes like its masks and flags.
43 * Furthermore it protects the access to a reference of the group that the mark
44 * is assigned to as well as the access to a reference of the inode/vfsmount
45 * that is being watched by the mark.
Eric Paris5444e292009-12-17 21:24:27 -050046 *
Jan Kara04662ca2017-02-01 08:19:43 +010047 * mark->connector->lock protects the list of marks anchored inside an
48 * inode / vfsmount and each mark is hooked via the i_list.
Eric Paris5444e292009-12-17 21:24:27 -050049 *
Jan Kara04662ca2017-02-01 08:19:43 +010050 * A list of notification marks relating to inode / mnt is contained in
51 * fsnotify_mark_connector. That structure is alive as long as there are any
52 * marks in the list and is also protected by fsnotify_mark_srcu.
Eric Paris5444e292009-12-17 21:24:27 -050053 *
54 * LIFETIME:
55 * Inode marks survive between when they are added to an inode and when their
Jan Karac1f33072016-12-16 10:53:32 +010056 * refcnt==0. Marks are also protected by fsnotify_mark_srcu.
Eric Paris5444e292009-12-17 21:24:27 -050057 *
58 * The inode mark can be cleared for a number of different reasons including:
59 * - The inode is unlinked for the last time. (fsnotify_inode_remove)
60 * - The inode is being evicted from cache. (fsnotify_inode_delete)
61 * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes)
62 * - Something explicitly requests that it be removed. (fsnotify_destroy_mark)
63 * - The fsnotify_group associated with the mark is going away and all such marks
64 * need to be cleaned up. (fsnotify_clear_marks_by_group)
65 *
Eric Paris5444e292009-12-17 21:24:27 -050066 * This has the very interesting property of being able to run concurrently with
67 * any (or all) other directions.
68 */
69
70#include <linux/fs.h>
71#include <linux/init.h>
72#include <linux/kernel.h>
Eric Paris75c1be42010-07-28 10:18:38 -040073#include <linux/kthread.h>
Eric Paris5444e292009-12-17 21:24:27 -050074#include <linux/module.h>
75#include <linux/mutex.h>
76#include <linux/slab.h>
77#include <linux/spinlock.h>
Eric Paris75c1be42010-07-28 10:18:38 -040078#include <linux/srcu.h>
Eric Paris5444e292009-12-17 21:24:27 -050079
Arun Sharma600634972011-07-26 16:09:06 -070080#include <linux/atomic.h>
Eric Paris5444e292009-12-17 21:24:27 -050081
82#include <linux/fsnotify_backend.h>
83#include "fsnotify.h"
84
Jeff Layton0918f1c2016-02-17 13:11:21 -080085#define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */
86
Eric Paris75c1be42010-07-28 10:18:38 -040087struct srcu_struct fsnotify_mark_srcu;
Jan Kara9dd813c2017-03-14 12:31:02 +010088struct kmem_cache *fsnotify_mark_connector_cachep;
89
Jeff Layton13d34ac2016-02-17 13:11:18 -080090static DEFINE_SPINLOCK(destroy_lock);
91static LIST_HEAD(destroy_list);
Jan Kara08991e82017-02-01 09:21:58 +010092static struct fsnotify_mark_connector *connector_destroy_list;
Jeff Layton0918f1c2016-02-17 13:11:21 -080093
Jan Kara35e48172016-05-19 17:08:59 -070094static void fsnotify_mark_destroy_workfn(struct work_struct *work);
95static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
Eric Paris75c1be42010-07-28 10:18:38 -040096
Jan Kara08991e82017-02-01 09:21:58 +010097static void fsnotify_connector_destroy_workfn(struct work_struct *work);
98static DECLARE_WORK(connector_reaper_work, fsnotify_connector_destroy_workfn);
99
Eric Paris5444e292009-12-17 21:24:27 -0500100void fsnotify_get_mark(struct fsnotify_mark *mark)
101{
102 atomic_inc(&mark->refcnt);
103}
104
105void fsnotify_put_mark(struct fsnotify_mark *mark)
106{
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200107 if (atomic_dec_and_test(&mark->refcnt)) {
108 if (mark->group)
109 fsnotify_put_group(mark->group);
Eric Paris5444e292009-12-17 21:24:27 -0500110 mark->free_mark(mark);
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200111 }
Eric Paris5444e292009-12-17 21:24:27 -0500112}
113
Jan Karaa2426772017-03-15 09:16:27 +0100114static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
Jan Kara0809ab62014-12-12 16:58:36 -0800115{
116 u32 new_mask = 0;
117 struct fsnotify_mark *mark;
118
Jan Kara04662ca2017-02-01 08:19:43 +0100119 assert_spin_locked(&conn->lock);
Jan Kara9dd813c2017-03-14 12:31:02 +0100120 hlist_for_each_entry(mark, &conn->list, obj_list)
Jan Kara0809ab62014-12-12 16:58:36 -0800121 new_mask |= mark->mask;
Jan Kara04662ca2017-02-01 08:19:43 +0100122
Jan Karaa2426772017-03-15 09:16:27 +0100123 if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
124 conn->inode->i_fsnotify_mask = new_mask;
125 else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT)
126 real_mount(conn->mnt)->mnt_fsnotify_mask = new_mask;
127}
128
129/*
130 * Calculate mask of events for a list of marks. The caller must make sure
131 * connector cannot disappear under us (usually by holding a mark->lock or
132 * mark->group->mark_mutex for a mark on this list).
133 */
134void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
135{
136 if (!conn)
137 return;
138
Jan Kara04662ca2017-02-01 08:19:43 +0100139 spin_lock(&conn->lock);
Jan Karaa2426772017-03-15 09:16:27 +0100140 __fsnotify_recalc_mask(conn);
Jan Kara04662ca2017-02-01 08:19:43 +0100141 spin_unlock(&conn->lock);
142 if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
Jan Karaa2426772017-03-15 09:16:27 +0100143 __fsnotify_update_child_dentry_flags(conn->inode);
Jan Kara0809ab62014-12-12 16:58:36 -0800144}
145
Jan Kara08991e82017-02-01 09:21:58 +0100146/* Free all connectors queued for freeing once SRCU period ends */
147static void fsnotify_connector_destroy_workfn(struct work_struct *work)
148{
149 struct fsnotify_mark_connector *conn, *free;
150
151 spin_lock(&destroy_lock);
152 conn = connector_destroy_list;
153 connector_destroy_list = NULL;
154 spin_unlock(&destroy_lock);
155
156 synchronize_srcu(&fsnotify_mark_srcu);
157 while (conn) {
158 free = conn;
159 conn = conn->destroy_next;
160 kmem_cache_free(fsnotify_mark_connector_cachep, free);
161 }
162}
163
164
165static struct inode *fsnotify_detach_connector_from_object(
166 struct fsnotify_mark_connector *conn)
167{
168 struct inode *inode = NULL;
169
170 if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) {
171 inode = conn->inode;
172 rcu_assign_pointer(inode->i_fsnotify_marks, NULL);
173 inode->i_fsnotify_mask = 0;
174 conn->inode = NULL;
175 conn->flags &= ~FSNOTIFY_OBJ_TYPE_INODE;
176 } else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
177 rcu_assign_pointer(real_mount(conn->mnt)->mnt_fsnotify_marks,
178 NULL);
179 real_mount(conn->mnt)->mnt_fsnotify_mask = 0;
180 conn->mnt = NULL;
181 conn->flags &= ~FSNOTIFY_OBJ_TYPE_VFSMOUNT;
182 }
183
184 return inode;
185}
186
Jan Kara8212a602017-03-15 09:48:11 +0100187static struct inode *fsnotify_detach_from_object(struct fsnotify_mark *mark)
188{
189 struct fsnotify_mark_connector *conn;
190 struct inode *inode = NULL;
Jan Kara08991e82017-02-01 09:21:58 +0100191 bool free_conn = false;
Jan Kara8212a602017-03-15 09:48:11 +0100192
193 conn = mark->connector;
Jan Kara04662ca2017-02-01 08:19:43 +0100194 spin_lock(&conn->lock);
Jan Kara8212a602017-03-15 09:48:11 +0100195 hlist_del_init_rcu(&mark->obj_list);
196 if (hlist_empty(&conn->list)) {
Jan Kara08991e82017-02-01 09:21:58 +0100197 inode = fsnotify_detach_connector_from_object(conn);
198 free_conn = true;
199 } else {
200 __fsnotify_recalc_mask(conn);
Jan Kara8212a602017-03-15 09:48:11 +0100201 }
202 mark->connector = NULL;
Jan Kara04662ca2017-02-01 08:19:43 +0100203 spin_unlock(&conn->lock);
Jan Kara8212a602017-03-15 09:48:11 +0100204
Jan Kara08991e82017-02-01 09:21:58 +0100205 if (free_conn) {
206 spin_lock(&destroy_lock);
207 conn->destroy_next = connector_destroy_list;
208 connector_destroy_list = conn;
209 spin_unlock(&destroy_lock);
210 queue_work(system_unbound_wq, &connector_reaper_work);
211 }
212
Jan Kara8212a602017-03-15 09:48:11 +0100213 return inode;
214}
215
Eric Paris5444e292009-12-17 21:24:27 -0500216/*
Jan Kara4712e7222015-09-04 15:43:12 -0700217 * Remove mark from inode / vfsmount list, group list, drop inode reference
218 * if we got one.
219 *
220 * Must be called with group->mark_mutex held.
Eric Paris5444e292009-12-17 21:24:27 -0500221 */
Jan Kara4712e7222015-09-04 15:43:12 -0700222void fsnotify_detach_mark(struct fsnotify_mark *mark)
Eric Paris5444e292009-12-17 21:24:27 -0500223{
Eric Paris0d48b7f2009-12-17 21:24:27 -0500224 struct inode *inode = NULL;
Jan Kara4712e7222015-09-04 15:43:12 -0700225 struct fsnotify_group *group = mark->group;
Eric Paris5444e292009-12-17 21:24:27 -0500226
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200227 BUG_ON(!mutex_is_locked(&group->mark_mutex));
228
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200229 spin_lock(&mark->lock);
Eric Paris5444e292009-12-17 21:24:27 -0500230
Eric Paris700307a2010-07-28 10:18:38 -0400231 /* something else already called this function on this mark */
Jan Kara4712e7222015-09-04 15:43:12 -0700232 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
Eric Paris5444e292009-12-17 21:24:27 -0500233 spin_unlock(&mark->lock);
Lino Sanfilippoe2a29942011-06-14 17:29:51 +0200234 return;
Eric Paris5444e292009-12-17 21:24:27 -0500235 }
236
Jan Kara4712e7222015-09-04 15:43:12 -0700237 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
Eric Paris700307a2010-07-28 10:18:38 -0400238
Jan Kara8212a602017-03-15 09:48:11 +0100239 inode = fsnotify_detach_from_object(mark);
240
Jan Kara4712e7222015-09-04 15:43:12 -0700241 /*
242 * Note that we didn't update flags telling whether inode cares about
243 * what's happening with children. We update these flags from
244 * __fsnotify_parent() lazily when next event happens on one of our
245 * children.
246 */
Eric Paris5444e292009-12-17 21:24:27 -0500247
248 list_del_init(&mark->g_list);
Linus Torvaldsd725e662015-07-21 16:06:53 -0700249
Eric Paris5444e292009-12-17 21:24:27 -0500250 spin_unlock(&mark->lock);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200251
Jan Karae911d8a2017-03-14 14:48:00 +0100252 if (inode)
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200253 iput(inode);
Jan Kara4712e7222015-09-04 15:43:12 -0700254
255 atomic_dec(&group->num_marks);
256}
257
258/*
Jan Kara35e48172016-05-19 17:08:59 -0700259 * Prepare mark for freeing and add it to the list of marks prepared for
260 * freeing. The actual freeing must happen after SRCU period ends and the
261 * caller is responsible for this.
262 *
263 * The function returns true if the mark was added to the list of marks for
264 * freeing. The function returns false if someone else has already called
265 * __fsnotify_free_mark() for the mark.
Jan Kara4712e7222015-09-04 15:43:12 -0700266 */
Jan Kara35e48172016-05-19 17:08:59 -0700267static bool __fsnotify_free_mark(struct fsnotify_mark *mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700268{
269 struct fsnotify_group *group = mark->group;
270
271 spin_lock(&mark->lock);
272 /* something else already called this function on this mark */
273 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
274 spin_unlock(&mark->lock);
Jan Kara35e48172016-05-19 17:08:59 -0700275 return false;
Jan Kara4712e7222015-09-04 15:43:12 -0700276 }
277 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
278 spin_unlock(&mark->lock);
Eric Paris5444e292009-12-17 21:24:27 -0500279
Linus Torvaldsd725e662015-07-21 16:06:53 -0700280 /*
281 * Some groups like to know that marks are being freed. This is a
282 * callback to the group function to let it know that this mark
283 * is being freed.
284 */
285 if (group->ops->freeing_mark)
286 group->ops->freeing_mark(mark, group);
Jan Kara35e48172016-05-19 17:08:59 -0700287
288 spin_lock(&destroy_lock);
289 list_add(&mark->g_list, &destroy_list);
290 spin_unlock(&destroy_lock);
291
292 return true;
293}
294
295/*
296 * Free fsnotify mark. The freeing is actually happening from a workqueue which
297 * first waits for srcu period end. Caller must have a reference to the mark
298 * or be protected by fsnotify_mark_srcu.
299 */
300void fsnotify_free_mark(struct fsnotify_mark *mark)
301{
302 if (__fsnotify_free_mark(mark)) {
303 queue_delayed_work(system_unbound_wq, &reaper_work,
304 FSNOTIFY_REAPER_DELAY);
305 }
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200306}
307
308void fsnotify_destroy_mark(struct fsnotify_mark *mark,
309 struct fsnotify_group *group)
310{
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200311 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
Jan Kara4712e7222015-09-04 15:43:12 -0700312 fsnotify_detach_mark(mark);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200313 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700314 fsnotify_free_mark(mark);
Eric Paris5444e292009-12-17 21:24:27 -0500315}
316
Eric Paris90b1e7a2009-12-17 21:24:33 -0500317void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
318{
319 assert_spin_locked(&mark->lock);
320
321 mark->mask = mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500322}
323
Eric Paris33af5e32009-12-17 21:24:33 -0500324void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
325{
326 assert_spin_locked(&mark->lock);
327
328 mark->ignored_mask = mask;
329}
Eric Paris90b1e7a2009-12-17 21:24:33 -0500330
Eric Paris5444e292009-12-17 21:24:27 -0500331/*
Jan Kara8edc6e12014-11-13 15:19:33 -0800332 * Sorting function for lists of fsnotify marks.
333 *
334 * Fanotify supports different notification classes (reflected as priority of
335 * notification group). Events shall be passed to notification groups in
336 * decreasing priority order. To achieve this marks in notification lists for
337 * inodes and vfsmounts are sorted so that priorities of corresponding groups
338 * are descending.
339 *
340 * Furthermore correct handling of the ignore mask requires processing inode
341 * and vfsmount marks of each group together. Using the group address as
342 * further sort criterion provides a unique sorting order and thus we can
343 * merge inode and vfsmount lists of marks in linear time and find groups
344 * present in both lists.
345 *
346 * A return value of 1 signifies that b has priority over a.
347 * A return value of 0 signifies that the two marks have to be handled together.
348 * A return value of -1 signifies that a has priority over b.
349 */
350int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
351{
352 if (a == b)
353 return 0;
354 if (!a)
355 return 1;
356 if (!b)
357 return -1;
358 if (a->priority < b->priority)
359 return 1;
360 if (a->priority > b->priority)
361 return -1;
362 if (a < b)
363 return 1;
364 return -1;
365}
366
Jan Kara9dd813c2017-03-14 12:31:02 +0100367static int fsnotify_attach_connector_to_object(
Jan Kara08991e82017-02-01 09:21:58 +0100368 struct fsnotify_mark_connector __rcu **connp,
369 struct inode *inode,
370 struct vfsmount *mnt)
Jan Kara9dd813c2017-03-14 12:31:02 +0100371{
372 struct fsnotify_mark_connector *conn;
373
Jan Kara755b5bc2017-03-14 16:11:23 +0100374 conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
Jan Kara9dd813c2017-03-14 12:31:02 +0100375 if (!conn)
376 return -ENOMEM;
Jan Kara04662ca2017-02-01 08:19:43 +0100377 spin_lock_init(&conn->lock);
Jan Kara9dd813c2017-03-14 12:31:02 +0100378 INIT_HLIST_HEAD(&conn->list);
Jan Kara86ffe242017-03-14 14:29:35 +0100379 if (inode) {
380 conn->flags = FSNOTIFY_OBJ_TYPE_INODE;
Jan Kara08991e82017-02-01 09:21:58 +0100381 conn->inode = igrab(inode);
Jan Kara86ffe242017-03-14 14:29:35 +0100382 } else {
383 conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
384 conn->mnt = mnt;
385 }
Jan Kara9dd813c2017-03-14 12:31:02 +0100386 /*
Jan Kara04662ca2017-02-01 08:19:43 +0100387 * cmpxchg() provides the barrier so that readers of *connp can see
388 * only initialized structure
Jan Kara9dd813c2017-03-14 12:31:02 +0100389 */
Jan Kara04662ca2017-02-01 08:19:43 +0100390 if (cmpxchg(connp, NULL, conn)) {
391 /* Someone else created list structure for us */
Jan Kara08991e82017-02-01 09:21:58 +0100392 if (inode)
393 iput(inode);
Jan Kara755b5bc2017-03-14 16:11:23 +0100394 kmem_cache_free(fsnotify_mark_connector_cachep, conn);
Jan Kara04662ca2017-02-01 08:19:43 +0100395 }
Jan Kara9dd813c2017-03-14 12:31:02 +0100396
397 return 0;
398}
399
400/*
Jan Kara08991e82017-02-01 09:21:58 +0100401 * Get mark connector, make sure it is alive and return with its lock held.
402 * This is for users that get connector pointer from inode or mount. Users that
403 * hold reference to a mark on the list may directly lock connector->lock as
404 * they are sure list cannot go away under them.
405 */
406static struct fsnotify_mark_connector *fsnotify_grab_connector(
407 struct fsnotify_mark_connector __rcu **connp)
408{
409 struct fsnotify_mark_connector *conn;
410 int idx;
411
412 idx = srcu_read_lock(&fsnotify_mark_srcu);
413 conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
414 if (!conn)
415 goto out;
416 spin_lock(&conn->lock);
417 if (!(conn->flags & (FSNOTIFY_OBJ_TYPE_INODE |
418 FSNOTIFY_OBJ_TYPE_VFSMOUNT))) {
419 spin_unlock(&conn->lock);
420 srcu_read_unlock(&fsnotify_mark_srcu, idx);
421 return NULL;
422 }
423out:
424 srcu_read_unlock(&fsnotify_mark_srcu, idx);
425 return conn;
426}
427
428/*
Jan Kara9dd813c2017-03-14 12:31:02 +0100429 * Add mark into proper place in given list of marks. These marks may be used
430 * for the fsnotify backend to determine which event types should be delivered
431 * to which group and for which inodes. These marks are ordered according to
432 * priority, highest number first, and then by the group's location in memory.
433 */
Jan Kara755b5bc2017-03-14 16:11:23 +0100434static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
435 struct inode *inode, struct vfsmount *mnt,
436 int allow_dups)
Jan Kara0809ab62014-12-12 16:58:36 -0800437{
438 struct fsnotify_mark *lmark, *last = NULL;
Jan Kara9dd813c2017-03-14 12:31:02 +0100439 struct fsnotify_mark_connector *conn;
Jan Kara08991e82017-02-01 09:21:58 +0100440 struct fsnotify_mark_connector __rcu **connp;
Jan Kara0809ab62014-12-12 16:58:36 -0800441 int cmp;
Jan Kara755b5bc2017-03-14 16:11:23 +0100442 int err = 0;
443
444 if (WARN_ON(!inode && !mnt))
445 return -EINVAL;
Jan Kara04662ca2017-02-01 08:19:43 +0100446 if (inode)
Jan Kara755b5bc2017-03-14 16:11:23 +0100447 connp = &inode->i_fsnotify_marks;
Jan Kara04662ca2017-02-01 08:19:43 +0100448 else
Jan Kara755b5bc2017-03-14 16:11:23 +0100449 connp = &real_mount(mnt)->mnt_fsnotify_marks;
Jan Kara08991e82017-02-01 09:21:58 +0100450restart:
451 spin_lock(&mark->lock);
452 conn = fsnotify_grab_connector(connp);
453 if (!conn) {
454 spin_unlock(&mark->lock);
Jan Kara04662ca2017-02-01 08:19:43 +0100455 err = fsnotify_attach_connector_to_object(connp, inode, mnt);
Jan Kara9dd813c2017-03-14 12:31:02 +0100456 if (err)
457 return err;
Jan Kara08991e82017-02-01 09:21:58 +0100458 goto restart;
Jan Kara9dd813c2017-03-14 12:31:02 +0100459 }
Jan Kara0809ab62014-12-12 16:58:36 -0800460
461 /* is mark the first mark? */
Jan Kara9dd813c2017-03-14 12:31:02 +0100462 if (hlist_empty(&conn->list)) {
463 hlist_add_head_rcu(&mark->obj_list, &conn->list);
Jan Kara86ffe242017-03-14 14:29:35 +0100464 goto added;
Jan Kara0809ab62014-12-12 16:58:36 -0800465 }
466
467 /* should mark be in the middle of the current list? */
Jan Kara9dd813c2017-03-14 12:31:02 +0100468 hlist_for_each_entry(lmark, &conn->list, obj_list) {
Jan Kara0809ab62014-12-12 16:58:36 -0800469 last = lmark;
470
Jan Kara755b5bc2017-03-14 16:11:23 +0100471 if ((lmark->group == mark->group) && !allow_dups) {
472 err = -EEXIST;
473 goto out_err;
474 }
Jan Kara0809ab62014-12-12 16:58:36 -0800475
476 cmp = fsnotify_compare_groups(lmark->group, mark->group);
477 if (cmp >= 0) {
478 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
Jan Kara86ffe242017-03-14 14:29:35 +0100479 goto added;
Jan Kara0809ab62014-12-12 16:58:36 -0800480 }
481 }
482
483 BUG_ON(last == NULL);
484 /* mark should be the last entry. last is the current last entry */
485 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
Jan Kara86ffe242017-03-14 14:29:35 +0100486added:
487 mark->connector = conn;
Jan Kara755b5bc2017-03-14 16:11:23 +0100488out_err:
Jan Kara04662ca2017-02-01 08:19:43 +0100489 spin_unlock(&conn->lock);
Jan Kara755b5bc2017-03-14 16:11:23 +0100490 spin_unlock(&mark->lock);
491 return err;
Jan Kara0809ab62014-12-12 16:58:36 -0800492}
493
Jan Kara8edc6e12014-11-13 15:19:33 -0800494/*
Eric Paris5444e292009-12-17 21:24:27 -0500495 * Attach an initialized mark to a given group and fs object.
496 * These marks may be used for the fsnotify backend to determine which
497 * event types should be delivered to which group.
498 */
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200499int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
500 struct fsnotify_group *group, struct inode *inode,
501 struct vfsmount *mnt, int allow_dups)
Eric Paris5444e292009-12-17 21:24:27 -0500502{
503 int ret = 0;
504
Eric Paris5444e292009-12-17 21:24:27 -0500505 BUG_ON(inode && mnt);
506 BUG_ON(!inode && !mnt);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200507 BUG_ON(!mutex_is_locked(&group->mark_mutex));
Eric Paris5444e292009-12-17 21:24:27 -0500508
509 /*
Eric Paris5444e292009-12-17 21:24:27 -0500510 * LOCKING ORDER!!!!
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200511 * group->mark_mutex
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200512 * mark->lock
Jan Kara04662ca2017-02-01 08:19:43 +0100513 * mark->connector->lock
Eric Paris5444e292009-12-17 21:24:27 -0500514 */
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200515 spin_lock(&mark->lock);
Jan Kara4712e7222015-09-04 15:43:12 -0700516 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
Eric Paris700307a2010-07-28 10:18:38 -0400517
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200518 fsnotify_get_group(group);
Eric Paris5444e292009-12-17 21:24:27 -0500519 mark->group = group;
520 list_add(&mark->g_list, &group->marks_list);
521 atomic_inc(&group->num_marks);
522 fsnotify_get_mark(mark); /* for i_list and g_list */
Eric Paris5444e292009-12-17 21:24:27 -0500523 spin_unlock(&mark->lock);
524
Jan Kara755b5bc2017-03-14 16:11:23 +0100525 ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups);
526 if (ret)
527 goto err;
528
Jan Karaa2426772017-03-15 09:16:27 +0100529 if (mark->mask)
530 fsnotify_recalc_mask(mark->connector);
Eric Paris5444e292009-12-17 21:24:27 -0500531
532 return ret;
533err:
Eric Paris700307a2010-07-28 10:18:38 -0400534 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
Eric Paris5444e292009-12-17 21:24:27 -0500535 list_del_init(&mark->g_list);
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200536 fsnotify_put_group(group);
Eric Paris75c1be42010-07-28 10:18:38 -0400537 mark->group = NULL;
Eric Paris5444e292009-12-17 21:24:27 -0500538 atomic_dec(&group->num_marks);
Eric Paris5444e292009-12-17 21:24:27 -0500539
Eric Paris5444e292009-12-17 21:24:27 -0500540 spin_unlock(&mark->lock);
541
Jeff Layton13d34ac2016-02-17 13:11:18 -0800542 spin_lock(&destroy_lock);
543 list_add(&mark->g_list, &destroy_list);
544 spin_unlock(&destroy_lock);
Jeff Layton0918f1c2016-02-17 13:11:21 -0800545 queue_delayed_work(system_unbound_wq, &reaper_work,
546 FSNOTIFY_REAPER_DELAY);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800547
Eric Paris5444e292009-12-17 21:24:27 -0500548 return ret;
549}
550
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200551int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
552 struct inode *inode, struct vfsmount *mnt, int allow_dups)
553{
554 int ret;
555 mutex_lock(&group->mark_mutex);
556 ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
557 mutex_unlock(&group->mark_mutex);
558 return ret;
559}
560
Eric Paris5444e292009-12-17 21:24:27 -0500561/*
Jan Kara0809ab62014-12-12 16:58:36 -0800562 * Given a list of marks, find the mark associated with given group. If found
563 * take a reference to that mark and return it, else return NULL.
564 */
Jan Kara08991e82017-02-01 09:21:58 +0100565struct fsnotify_mark *fsnotify_find_mark(
566 struct fsnotify_mark_connector __rcu **connp,
567 struct fsnotify_group *group)
Jan Kara0809ab62014-12-12 16:58:36 -0800568{
Jan Kara08991e82017-02-01 09:21:58 +0100569 struct fsnotify_mark_connector *conn;
Jan Kara0809ab62014-12-12 16:58:36 -0800570 struct fsnotify_mark *mark;
571
Jan Kara08991e82017-02-01 09:21:58 +0100572 conn = fsnotify_grab_connector(connp);
Jan Kara9dd813c2017-03-14 12:31:02 +0100573 if (!conn)
574 return NULL;
575
576 hlist_for_each_entry(mark, &conn->list, obj_list) {
Jan Kara0809ab62014-12-12 16:58:36 -0800577 if (mark->group == group) {
578 fsnotify_get_mark(mark);
Jan Kara04662ca2017-02-01 08:19:43 +0100579 spin_unlock(&conn->lock);
Jan Kara0809ab62014-12-12 16:58:36 -0800580 return mark;
581 }
582 }
Jan Kara04662ca2017-02-01 08:19:43 +0100583 spin_unlock(&conn->lock);
Jan Kara0809ab62014-12-12 16:58:36 -0800584 return NULL;
585}
586
587/*
Linus Torvaldsd725e662015-07-21 16:06:53 -0700588 * clear any marks in a group in which mark->flags & flags is true
Eric Paris5444e292009-12-17 21:24:27 -0500589 */
Eric Paris4d926042009-12-17 21:24:34 -0500590void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
591 unsigned int flags)
Eric Paris5444e292009-12-17 21:24:27 -0500592{
593 struct fsnotify_mark *lmark, *mark;
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700594 LIST_HEAD(to_free);
Eric Paris5444e292009-12-17 21:24:27 -0500595
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700596 /*
597 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
598 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
599 * to_free list so we have to use mark_mutex even when accessing that
600 * list. And freeing mark requires us to drop mark_mutex. So we can
601 * reliably free only the first mark in the list. That's why we first
602 * move marks to free to to_free list in one go and then free marks in
603 * to_free list one by one.
604 */
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200605 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
Eric Paris5444e292009-12-17 21:24:27 -0500606 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
Jan Kara86ffe242017-03-14 14:29:35 +0100607 if (mark->connector->flags & flags)
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700608 list_move(&mark->g_list, &to_free);
Eric Paris5444e292009-12-17 21:24:27 -0500609 }
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200610 mutex_unlock(&group->mark_mutex);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700611
612 while (1) {
613 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
614 if (list_empty(&to_free)) {
615 mutex_unlock(&group->mark_mutex);
616 break;
617 }
618 mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
619 fsnotify_get_mark(mark);
Jan Kara4712e7222015-09-04 15:43:12 -0700620 fsnotify_detach_mark(mark);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700621 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700622 fsnotify_free_mark(mark);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700623 fsnotify_put_mark(mark);
624 }
Eric Paris5444e292009-12-17 21:24:27 -0500625}
626
Eric Paris4d926042009-12-17 21:24:34 -0500627/*
Jan Kara35e48172016-05-19 17:08:59 -0700628 * Given a group, prepare for freeing all the marks associated with that group.
629 * The marks are attached to the list of marks prepared for destruction, the
630 * caller is responsible for freeing marks in that list after SRCU period has
631 * ended.
Eric Paris4d926042009-12-17 21:24:34 -0500632 */
Jan Kara35e48172016-05-19 17:08:59 -0700633void fsnotify_detach_group_marks(struct fsnotify_group *group)
Eric Paris4d926042009-12-17 21:24:34 -0500634{
Jan Kara35e48172016-05-19 17:08:59 -0700635 struct fsnotify_mark *mark;
636
637 while (1) {
638 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
639 if (list_empty(&group->marks_list)) {
640 mutex_unlock(&group->mark_mutex);
641 break;
642 }
643 mark = list_first_entry(&group->marks_list,
644 struct fsnotify_mark, g_list);
645 fsnotify_get_mark(mark);
646 fsnotify_detach_mark(mark);
647 mutex_unlock(&group->mark_mutex);
648 __fsnotify_free_mark(mark);
649 fsnotify_put_mark(mark);
650 }
Eric Paris4d926042009-12-17 21:24:34 -0500651}
652
Jan Kara08991e82017-02-01 09:21:58 +0100653/* Destroy all marks attached to inode / vfsmount */
654void fsnotify_destroy_marks(struct fsnotify_mark_connector __rcu **connp)
Jan Kara0810b4f2017-02-01 09:23:48 +0100655{
Jan Kara08991e82017-02-01 09:21:58 +0100656 struct fsnotify_mark_connector *conn;
Jan Kara0810b4f2017-02-01 09:23:48 +0100657 struct fsnotify_mark *mark;
658
Jan Kara08991e82017-02-01 09:21:58 +0100659 while ((conn = fsnotify_grab_connector(connp))) {
Jan Kara0810b4f2017-02-01 09:23:48 +0100660 /*
661 * We have to be careful since we can race with e.g.
Jan Kara08991e82017-02-01 09:21:58 +0100662 * fsnotify_clear_marks_by_group() and once we drop the list
663 * lock, mark can get removed from the obj_list and destroyed.
664 * But we are holding mark reference so mark cannot be freed
665 * and calling fsnotify_destroy_mark() more than once is fine.
Jan Kara0810b4f2017-02-01 09:23:48 +0100666 */
Jan Kara0810b4f2017-02-01 09:23:48 +0100667 mark = hlist_entry(conn->list.first, struct fsnotify_mark,
668 obj_list);
Jan Kara0810b4f2017-02-01 09:23:48 +0100669 fsnotify_get_mark(mark);
Jan Kara04662ca2017-02-01 08:19:43 +0100670 spin_unlock(&conn->lock);
Jan Kara0810b4f2017-02-01 09:23:48 +0100671 fsnotify_destroy_mark(mark, mark->group);
672 fsnotify_put_mark(mark);
673 }
674}
675
Eric Paris5444e292009-12-17 21:24:27 -0500676/*
677 * Nothing fancy, just initialize lists and locks and counters.
678 */
679void fsnotify_init_mark(struct fsnotify_mark *mark,
680 void (*free_mark)(struct fsnotify_mark *mark))
681{
Eric Parisba643f02009-12-17 21:24:27 -0500682 memset(mark, 0, sizeof(*mark));
Eric Paris5444e292009-12-17 21:24:27 -0500683 spin_lock_init(&mark->lock);
684 atomic_set(&mark->refcnt, 1);
Eric Paris5444e292009-12-17 21:24:27 -0500685 mark->free_mark = free_mark;
686}
Jeff Layton13d34ac2016-02-17 13:11:18 -0800687
Jan Kara35e48172016-05-19 17:08:59 -0700688/*
689 * Destroy all marks in destroy_list, waits for SRCU period to finish before
690 * actually freeing marks.
691 */
692void fsnotify_mark_destroy_list(void)
Jeff Layton13d34ac2016-02-17 13:11:18 -0800693{
694 struct fsnotify_mark *mark, *next;
695 struct list_head private_destroy_list;
696
Jeff Layton0918f1c2016-02-17 13:11:21 -0800697 spin_lock(&destroy_lock);
698 /* exchange the list head */
699 list_replace_init(&destroy_list, &private_destroy_list);
700 spin_unlock(&destroy_lock);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800701
Jeff Layton0918f1c2016-02-17 13:11:21 -0800702 synchronize_srcu(&fsnotify_mark_srcu);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800703
Jeff Layton0918f1c2016-02-17 13:11:21 -0800704 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
705 list_del_init(&mark->g_list);
706 fsnotify_put_mark(mark);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800707 }
Jeff Layton13d34ac2016-02-17 13:11:18 -0800708}
Jan Kara35e48172016-05-19 17:08:59 -0700709
710static void fsnotify_mark_destroy_workfn(struct work_struct *work)
711{
712 fsnotify_mark_destroy_list();
713}