blob: 06faf166c7aecbccb8589c52b72f1d08d963409a [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
Eric Paris5444e292009-12-17 21:24:27 -050036 * inode->i_lock
37 *
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 *
47 * inode->i_lock protects the i_fsnotify_marks list anchored inside a
48 * given inode and each mark is hooked via the i_list. (and sorta the
49 * free_i_list)
50 *
51 *
52 * LIFETIME:
53 * Inode marks survive between when they are added to an inode and when their
Jan Karac1f33072016-12-16 10:53:32 +010054 * refcnt==0. Marks are also protected by fsnotify_mark_srcu.
Eric Paris5444e292009-12-17 21:24:27 -050055 *
56 * The inode mark can be cleared for a number of different reasons including:
57 * - The inode is unlinked for the last time. (fsnotify_inode_remove)
58 * - The inode is being evicted from cache. (fsnotify_inode_delete)
59 * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes)
60 * - Something explicitly requests that it be removed. (fsnotify_destroy_mark)
61 * - The fsnotify_group associated with the mark is going away and all such marks
62 * need to be cleaned up. (fsnotify_clear_marks_by_group)
63 *
Eric Paris5444e292009-12-17 21:24:27 -050064 * This has the very interesting property of being able to run concurrently with
65 * any (or all) other directions.
66 */
67
68#include <linux/fs.h>
69#include <linux/init.h>
70#include <linux/kernel.h>
Eric Paris75c1be42010-07-28 10:18:38 -040071#include <linux/kthread.h>
Eric Paris5444e292009-12-17 21:24:27 -050072#include <linux/module.h>
73#include <linux/mutex.h>
74#include <linux/slab.h>
75#include <linux/spinlock.h>
Eric Paris75c1be42010-07-28 10:18:38 -040076#include <linux/srcu.h>
Eric Paris5444e292009-12-17 21:24:27 -050077
Arun Sharma600634972011-07-26 16:09:06 -070078#include <linux/atomic.h>
Eric Paris5444e292009-12-17 21:24:27 -050079
80#include <linux/fsnotify_backend.h>
81#include "fsnotify.h"
82
Jeff Layton0918f1c2016-02-17 13:11:21 -080083#define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */
84
Eric Paris75c1be42010-07-28 10:18:38 -040085struct srcu_struct fsnotify_mark_srcu;
Jan Kara9dd813c2017-03-14 12:31:02 +010086struct kmem_cache *fsnotify_mark_connector_cachep;
87
Jeff Layton13d34ac2016-02-17 13:11:18 -080088static DEFINE_SPINLOCK(destroy_lock);
89static LIST_HEAD(destroy_list);
Jeff Layton0918f1c2016-02-17 13:11:21 -080090
Jan Kara35e48172016-05-19 17:08:59 -070091static void fsnotify_mark_destroy_workfn(struct work_struct *work);
92static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
Eric Paris75c1be42010-07-28 10:18:38 -040093
Eric Paris5444e292009-12-17 21:24:27 -050094void fsnotify_get_mark(struct fsnotify_mark *mark)
95{
96 atomic_inc(&mark->refcnt);
97}
98
99void fsnotify_put_mark(struct fsnotify_mark *mark)
100{
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200101 if (atomic_dec_and_test(&mark->refcnt)) {
102 if (mark->group)
103 fsnotify_put_group(mark->group);
Eric Paris5444e292009-12-17 21:24:27 -0500104 mark->free_mark(mark);
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200105 }
Eric Paris5444e292009-12-17 21:24:27 -0500106}
107
Jan Karaa2426772017-03-15 09:16:27 +0100108static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
Jan Kara0809ab62014-12-12 16:58:36 -0800109{
110 u32 new_mask = 0;
111 struct fsnotify_mark *mark;
112
Jan Kara9dd813c2017-03-14 12:31:02 +0100113 hlist_for_each_entry(mark, &conn->list, obj_list)
Jan Kara0809ab62014-12-12 16:58:36 -0800114 new_mask |= mark->mask;
Jan Karaa2426772017-03-15 09:16:27 +0100115 if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
116 conn->inode->i_fsnotify_mask = new_mask;
117 else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT)
118 real_mount(conn->mnt)->mnt_fsnotify_mask = new_mask;
119}
120
121/*
122 * Calculate mask of events for a list of marks. The caller must make sure
123 * connector cannot disappear under us (usually by holding a mark->lock or
124 * mark->group->mark_mutex for a mark on this list).
125 */
126void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
127{
128 if (!conn)
129 return;
130
131 if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
132 spin_lock(&conn->inode->i_lock);
133 else
134 spin_lock(&conn->mnt->mnt_root->d_lock);
135 __fsnotify_recalc_mask(conn);
136 if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) {
137 spin_unlock(&conn->inode->i_lock);
138 __fsnotify_update_child_dentry_flags(conn->inode);
139 } else {
140 spin_unlock(&conn->mnt->mnt_root->d_lock);
141 }
Jan Kara0809ab62014-12-12 16:58:36 -0800142}
143
Eric Paris5444e292009-12-17 21:24:27 -0500144/*
Jan Kara4712e7222015-09-04 15:43:12 -0700145 * Remove mark from inode / vfsmount list, group list, drop inode reference
146 * if we got one.
147 *
148 * Must be called with group->mark_mutex held.
Eric Paris5444e292009-12-17 21:24:27 -0500149 */
Jan Kara4712e7222015-09-04 15:43:12 -0700150void fsnotify_detach_mark(struct fsnotify_mark *mark)
Eric Paris5444e292009-12-17 21:24:27 -0500151{
Eric Paris0d48b7f2009-12-17 21:24:27 -0500152 struct inode *inode = NULL;
Jan Kara4712e7222015-09-04 15:43:12 -0700153 struct fsnotify_group *group = mark->group;
Eric Paris5444e292009-12-17 21:24:27 -0500154
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200155 BUG_ON(!mutex_is_locked(&group->mark_mutex));
156
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200157 spin_lock(&mark->lock);
Eric Paris5444e292009-12-17 21:24:27 -0500158
Eric Paris700307a2010-07-28 10:18:38 -0400159 /* something else already called this function on this mark */
Jan Kara4712e7222015-09-04 15:43:12 -0700160 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
Eric Paris5444e292009-12-17 21:24:27 -0500161 spin_unlock(&mark->lock);
Lino Sanfilippoe2a29942011-06-14 17:29:51 +0200162 return;
Eric Paris5444e292009-12-17 21:24:27 -0500163 }
164
Jan Kara4712e7222015-09-04 15:43:12 -0700165 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
Eric Paris700307a2010-07-28 10:18:38 -0400166
Jan Karae911d8a2017-03-14 14:48:00 +0100167 if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_INODE)
168 inode = fsnotify_destroy_inode_mark(mark);
169 else if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT)
Eric Paris0d48b7f2009-12-17 21:24:27 -0500170 fsnotify_destroy_vfsmount_mark(mark);
Eric Paris5444e292009-12-17 21:24:27 -0500171 else
172 BUG();
Jan Kara4712e7222015-09-04 15:43:12 -0700173 /*
174 * Note that we didn't update flags telling whether inode cares about
175 * what's happening with children. We update these flags from
176 * __fsnotify_parent() lazily when next event happens on one of our
177 * children.
178 */
Eric Paris5444e292009-12-17 21:24:27 -0500179
180 list_del_init(&mark->g_list);
Linus Torvaldsd725e662015-07-21 16:06:53 -0700181
Eric Paris5444e292009-12-17 21:24:27 -0500182 spin_unlock(&mark->lock);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200183
Jan Karae911d8a2017-03-14 14:48:00 +0100184 if (inode)
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200185 iput(inode);
Jan Kara4712e7222015-09-04 15:43:12 -0700186
187 atomic_dec(&group->num_marks);
188}
189
190/*
Jan Kara35e48172016-05-19 17:08:59 -0700191 * Prepare mark for freeing and add it to the list of marks prepared for
192 * freeing. The actual freeing must happen after SRCU period ends and the
193 * caller is responsible for this.
194 *
195 * The function returns true if the mark was added to the list of marks for
196 * freeing. The function returns false if someone else has already called
197 * __fsnotify_free_mark() for the mark.
Jan Kara4712e7222015-09-04 15:43:12 -0700198 */
Jan Kara35e48172016-05-19 17:08:59 -0700199static bool __fsnotify_free_mark(struct fsnotify_mark *mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700200{
201 struct fsnotify_group *group = mark->group;
202
203 spin_lock(&mark->lock);
204 /* something else already called this function on this mark */
205 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
206 spin_unlock(&mark->lock);
Jan Kara35e48172016-05-19 17:08:59 -0700207 return false;
Jan Kara4712e7222015-09-04 15:43:12 -0700208 }
209 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
210 spin_unlock(&mark->lock);
Eric Paris5444e292009-12-17 21:24:27 -0500211
Linus Torvaldsd725e662015-07-21 16:06:53 -0700212 /*
213 * Some groups like to know that marks are being freed. This is a
214 * callback to the group function to let it know that this mark
215 * is being freed.
216 */
217 if (group->ops->freeing_mark)
218 group->ops->freeing_mark(mark, group);
Jan Kara35e48172016-05-19 17:08:59 -0700219
220 spin_lock(&destroy_lock);
221 list_add(&mark->g_list, &destroy_list);
222 spin_unlock(&destroy_lock);
223
224 return true;
225}
226
227/*
228 * Free fsnotify mark. The freeing is actually happening from a workqueue which
229 * first waits for srcu period end. Caller must have a reference to the mark
230 * or be protected by fsnotify_mark_srcu.
231 */
232void fsnotify_free_mark(struct fsnotify_mark *mark)
233{
234 if (__fsnotify_free_mark(mark)) {
235 queue_delayed_work(system_unbound_wq, &reaper_work,
236 FSNOTIFY_REAPER_DELAY);
237 }
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200238}
239
240void fsnotify_destroy_mark(struct fsnotify_mark *mark,
241 struct fsnotify_group *group)
242{
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200243 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
Jan Kara4712e7222015-09-04 15:43:12 -0700244 fsnotify_detach_mark(mark);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200245 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700246 fsnotify_free_mark(mark);
Eric Paris5444e292009-12-17 21:24:27 -0500247}
248
Jan Kara9dd813c2017-03-14 12:31:02 +0100249void fsnotify_connector_free(struct fsnotify_mark_connector **connp)
250{
251 if (*connp) {
252 kmem_cache_free(fsnotify_mark_connector_cachep, *connp);
253 *connp = NULL;
254 }
255}
256
Eric Paris90b1e7a2009-12-17 21:24:33 -0500257void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
258{
259 assert_spin_locked(&mark->lock);
260
261 mark->mask = mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500262}
263
Eric Paris33af5e32009-12-17 21:24:33 -0500264void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
265{
266 assert_spin_locked(&mark->lock);
267
268 mark->ignored_mask = mask;
269}
Eric Paris90b1e7a2009-12-17 21:24:33 -0500270
Eric Paris5444e292009-12-17 21:24:27 -0500271/*
Jan Kara8edc6e12014-11-13 15:19:33 -0800272 * Sorting function for lists of fsnotify marks.
273 *
274 * Fanotify supports different notification classes (reflected as priority of
275 * notification group). Events shall be passed to notification groups in
276 * decreasing priority order. To achieve this marks in notification lists for
277 * inodes and vfsmounts are sorted so that priorities of corresponding groups
278 * are descending.
279 *
280 * Furthermore correct handling of the ignore mask requires processing inode
281 * and vfsmount marks of each group together. Using the group address as
282 * further sort criterion provides a unique sorting order and thus we can
283 * merge inode and vfsmount lists of marks in linear time and find groups
284 * present in both lists.
285 *
286 * A return value of 1 signifies that b has priority over a.
287 * A return value of 0 signifies that the two marks have to be handled together.
288 * A return value of -1 signifies that a has priority over b.
289 */
290int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
291{
292 if (a == b)
293 return 0;
294 if (!a)
295 return 1;
296 if (!b)
297 return -1;
298 if (a->priority < b->priority)
299 return 1;
300 if (a->priority > b->priority)
301 return -1;
302 if (a < b)
303 return 1;
304 return -1;
305}
306
Jan Kara9dd813c2017-03-14 12:31:02 +0100307static int fsnotify_attach_connector_to_object(
Jan Kara86ffe242017-03-14 14:29:35 +0100308 struct fsnotify_mark_connector **connp,
Jan Kara755b5bc2017-03-14 16:11:23 +0100309 spinlock_t *lock,
Jan Kara86ffe242017-03-14 14:29:35 +0100310 struct inode *inode,
311 struct vfsmount *mnt)
Jan Kara9dd813c2017-03-14 12:31:02 +0100312{
313 struct fsnotify_mark_connector *conn;
314
Jan Kara755b5bc2017-03-14 16:11:23 +0100315 conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
Jan Kara9dd813c2017-03-14 12:31:02 +0100316 if (!conn)
317 return -ENOMEM;
318 INIT_HLIST_HEAD(&conn->list);
Jan Kara86ffe242017-03-14 14:29:35 +0100319 if (inode) {
320 conn->flags = FSNOTIFY_OBJ_TYPE_INODE;
321 conn->inode = inode;
322 } else {
323 conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
324 conn->mnt = mnt;
325 }
Jan Kara9dd813c2017-03-14 12:31:02 +0100326 /*
327 * Make sure 'conn' initialization is visible. Matches
328 * lockless_dereference() in fsnotify().
329 */
330 smp_wmb();
Jan Kara755b5bc2017-03-14 16:11:23 +0100331 spin_lock(lock);
332 if (!*connp)
333 *connp = conn;
334 else
335 kmem_cache_free(fsnotify_mark_connector_cachep, conn);
336 spin_unlock(lock);
Jan Kara9dd813c2017-03-14 12:31:02 +0100337
338 return 0;
339}
340
341/*
342 * Add mark into proper place in given list of marks. These marks may be used
343 * for the fsnotify backend to determine which event types should be delivered
344 * to which group and for which inodes. These marks are ordered according to
345 * priority, highest number first, and then by the group's location in memory.
346 */
Jan Kara755b5bc2017-03-14 16:11:23 +0100347static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
348 struct inode *inode, struct vfsmount *mnt,
349 int allow_dups)
Jan Kara0809ab62014-12-12 16:58:36 -0800350{
351 struct fsnotify_mark *lmark, *last = NULL;
Jan Kara9dd813c2017-03-14 12:31:02 +0100352 struct fsnotify_mark_connector *conn;
Jan Kara755b5bc2017-03-14 16:11:23 +0100353 struct fsnotify_mark_connector **connp;
354 spinlock_t *lock;
Jan Kara0809ab62014-12-12 16:58:36 -0800355 int cmp;
Jan Kara755b5bc2017-03-14 16:11:23 +0100356 int err = 0;
357
358 if (WARN_ON(!inode && !mnt))
359 return -EINVAL;
360 if (inode) {
361 connp = &inode->i_fsnotify_marks;
362 lock = &inode->i_lock;
363 } else {
364 connp = &real_mount(mnt)->mnt_fsnotify_marks;
365 lock = &mnt->mnt_root->d_lock;
366 }
Jan Kara9dd813c2017-03-14 12:31:02 +0100367
368 if (!*connp) {
Jan Kara755b5bc2017-03-14 16:11:23 +0100369 err = fsnotify_attach_connector_to_object(connp, lock,
370 inode, mnt);
Jan Kara9dd813c2017-03-14 12:31:02 +0100371 if (err)
372 return err;
373 }
Jan Kara755b5bc2017-03-14 16:11:23 +0100374 spin_lock(&mark->lock);
375 spin_lock(lock);
Jan Kara9dd813c2017-03-14 12:31:02 +0100376 conn = *connp;
Jan Kara0809ab62014-12-12 16:58:36 -0800377
378 /* is mark the first mark? */
Jan Kara9dd813c2017-03-14 12:31:02 +0100379 if (hlist_empty(&conn->list)) {
380 hlist_add_head_rcu(&mark->obj_list, &conn->list);
Jan Karae911d8a2017-03-14 14:48:00 +0100381 if (inode)
382 __iget(inode);
Jan Kara86ffe242017-03-14 14:29:35 +0100383 goto added;
Jan Kara0809ab62014-12-12 16:58:36 -0800384 }
385
386 /* should mark be in the middle of the current list? */
Jan Kara9dd813c2017-03-14 12:31:02 +0100387 hlist_for_each_entry(lmark, &conn->list, obj_list) {
Jan Kara0809ab62014-12-12 16:58:36 -0800388 last = lmark;
389
Jan Kara755b5bc2017-03-14 16:11:23 +0100390 if ((lmark->group == mark->group) && !allow_dups) {
391 err = -EEXIST;
392 goto out_err;
393 }
Jan Kara0809ab62014-12-12 16:58:36 -0800394
395 cmp = fsnotify_compare_groups(lmark->group, mark->group);
396 if (cmp >= 0) {
397 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
Jan Kara86ffe242017-03-14 14:29:35 +0100398 goto added;
Jan Kara0809ab62014-12-12 16:58:36 -0800399 }
400 }
401
402 BUG_ON(last == NULL);
403 /* mark should be the last entry. last is the current last entry */
404 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
Jan Kara86ffe242017-03-14 14:29:35 +0100405added:
406 mark->connector = conn;
Jan Kara755b5bc2017-03-14 16:11:23 +0100407out_err:
408 spin_unlock(lock);
409 spin_unlock(&mark->lock);
410 return err;
Jan Kara0809ab62014-12-12 16:58:36 -0800411}
412
Jan Kara8edc6e12014-11-13 15:19:33 -0800413/*
Eric Paris5444e292009-12-17 21:24:27 -0500414 * Attach an initialized mark to a given group and fs object.
415 * These marks may be used for the fsnotify backend to determine which
416 * event types should be delivered to which group.
417 */
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200418int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
419 struct fsnotify_group *group, struct inode *inode,
420 struct vfsmount *mnt, int allow_dups)
Eric Paris5444e292009-12-17 21:24:27 -0500421{
422 int ret = 0;
423
Eric Paris5444e292009-12-17 21:24:27 -0500424 BUG_ON(inode && mnt);
425 BUG_ON(!inode && !mnt);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200426 BUG_ON(!mutex_is_locked(&group->mark_mutex));
Eric Paris5444e292009-12-17 21:24:27 -0500427
428 /*
Eric Paris5444e292009-12-17 21:24:27 -0500429 * LOCKING ORDER!!!!
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200430 * group->mark_mutex
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200431 * mark->lock
Eric Paris5444e292009-12-17 21:24:27 -0500432 * inode->i_lock
433 */
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200434 spin_lock(&mark->lock);
Jan Kara4712e7222015-09-04 15:43:12 -0700435 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
Eric Paris700307a2010-07-28 10:18:38 -0400436
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200437 fsnotify_get_group(group);
Eric Paris5444e292009-12-17 21:24:27 -0500438 mark->group = group;
439 list_add(&mark->g_list, &group->marks_list);
440 atomic_inc(&group->num_marks);
441 fsnotify_get_mark(mark); /* for i_list and g_list */
Eric Paris5444e292009-12-17 21:24:27 -0500442 spin_unlock(&mark->lock);
443
Jan Kara755b5bc2017-03-14 16:11:23 +0100444 ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups);
445 if (ret)
446 goto err;
447
Jan Karaa2426772017-03-15 09:16:27 +0100448 if (mark->mask)
449 fsnotify_recalc_mask(mark->connector);
Eric Paris5444e292009-12-17 21:24:27 -0500450
451 return ret;
452err:
Eric Paris700307a2010-07-28 10:18:38 -0400453 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
Eric Paris5444e292009-12-17 21:24:27 -0500454 list_del_init(&mark->g_list);
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200455 fsnotify_put_group(group);
Eric Paris75c1be42010-07-28 10:18:38 -0400456 mark->group = NULL;
Eric Paris5444e292009-12-17 21:24:27 -0500457 atomic_dec(&group->num_marks);
Eric Paris5444e292009-12-17 21:24:27 -0500458
Eric Paris5444e292009-12-17 21:24:27 -0500459 spin_unlock(&mark->lock);
460
Jeff Layton13d34ac2016-02-17 13:11:18 -0800461 spin_lock(&destroy_lock);
462 list_add(&mark->g_list, &destroy_list);
463 spin_unlock(&destroy_lock);
Jeff Layton0918f1c2016-02-17 13:11:21 -0800464 queue_delayed_work(system_unbound_wq, &reaper_work,
465 FSNOTIFY_REAPER_DELAY);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800466
Eric Paris5444e292009-12-17 21:24:27 -0500467 return ret;
468}
469
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200470int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
471 struct inode *inode, struct vfsmount *mnt, int allow_dups)
472{
473 int ret;
474 mutex_lock(&group->mark_mutex);
475 ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
476 mutex_unlock(&group->mark_mutex);
477 return ret;
478}
479
Eric Paris5444e292009-12-17 21:24:27 -0500480/*
Jan Kara0809ab62014-12-12 16:58:36 -0800481 * Given a list of marks, find the mark associated with given group. If found
482 * take a reference to that mark and return it, else return NULL.
483 */
Jan Kara9dd813c2017-03-14 12:31:02 +0100484struct fsnotify_mark *fsnotify_find_mark(struct fsnotify_mark_connector *conn,
Jan Kara0809ab62014-12-12 16:58:36 -0800485 struct fsnotify_group *group)
486{
487 struct fsnotify_mark *mark;
488
Jan Kara9dd813c2017-03-14 12:31:02 +0100489 if (!conn)
490 return NULL;
491
492 hlist_for_each_entry(mark, &conn->list, obj_list) {
Jan Kara0809ab62014-12-12 16:58:36 -0800493 if (mark->group == group) {
494 fsnotify_get_mark(mark);
495 return mark;
496 }
497 }
498 return NULL;
499}
500
501/*
Linus Torvaldsd725e662015-07-21 16:06:53 -0700502 * clear any marks in a group in which mark->flags & flags is true
Eric Paris5444e292009-12-17 21:24:27 -0500503 */
Eric Paris4d926042009-12-17 21:24:34 -0500504void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
505 unsigned int flags)
Eric Paris5444e292009-12-17 21:24:27 -0500506{
507 struct fsnotify_mark *lmark, *mark;
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700508 LIST_HEAD(to_free);
Eric Paris5444e292009-12-17 21:24:27 -0500509
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700510 /*
511 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
512 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
513 * to_free list so we have to use mark_mutex even when accessing that
514 * list. And freeing mark requires us to drop mark_mutex. So we can
515 * reliably free only the first mark in the list. That's why we first
516 * move marks to free to to_free list in one go and then free marks in
517 * to_free list one by one.
518 */
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200519 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
Eric Paris5444e292009-12-17 21:24:27 -0500520 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
Jan Kara86ffe242017-03-14 14:29:35 +0100521 if (mark->connector->flags & flags)
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700522 list_move(&mark->g_list, &to_free);
Eric Paris5444e292009-12-17 21:24:27 -0500523 }
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200524 mutex_unlock(&group->mark_mutex);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700525
526 while (1) {
527 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
528 if (list_empty(&to_free)) {
529 mutex_unlock(&group->mark_mutex);
530 break;
531 }
532 mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
533 fsnotify_get_mark(mark);
Jan Kara4712e7222015-09-04 15:43:12 -0700534 fsnotify_detach_mark(mark);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700535 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700536 fsnotify_free_mark(mark);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700537 fsnotify_put_mark(mark);
538 }
Eric Paris5444e292009-12-17 21:24:27 -0500539}
540
Eric Paris4d926042009-12-17 21:24:34 -0500541/*
Jan Kara35e48172016-05-19 17:08:59 -0700542 * Given a group, prepare for freeing all the marks associated with that group.
543 * The marks are attached to the list of marks prepared for destruction, the
544 * caller is responsible for freeing marks in that list after SRCU period has
545 * ended.
Eric Paris4d926042009-12-17 21:24:34 -0500546 */
Jan Kara35e48172016-05-19 17:08:59 -0700547void fsnotify_detach_group_marks(struct fsnotify_group *group)
Eric Paris4d926042009-12-17 21:24:34 -0500548{
Jan Kara35e48172016-05-19 17:08:59 -0700549 struct fsnotify_mark *mark;
550
551 while (1) {
552 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
553 if (list_empty(&group->marks_list)) {
554 mutex_unlock(&group->mark_mutex);
555 break;
556 }
557 mark = list_first_entry(&group->marks_list,
558 struct fsnotify_mark, g_list);
559 fsnotify_get_mark(mark);
560 fsnotify_detach_mark(mark);
561 mutex_unlock(&group->mark_mutex);
562 __fsnotify_free_mark(mark);
563 fsnotify_put_mark(mark);
564 }
Eric Paris4d926042009-12-17 21:24:34 -0500565}
566
Jan Kara0810b4f2017-02-01 09:23:48 +0100567void fsnotify_destroy_marks(struct fsnotify_mark_connector *conn,
568 spinlock_t *lock)
569{
570 struct fsnotify_mark *mark;
571
572 if (!conn)
573 return;
574
575 while (1) {
576 /*
577 * We have to be careful since we can race with e.g.
578 * fsnotify_clear_marks_by_group() and once we drop 'lock',
579 * mark can get removed from the obj_list and destroyed. But
580 * we are holding mark reference so mark cannot be freed and
581 * calling fsnotify_destroy_mark() more than once is fine.
582 */
583 spin_lock(lock);
584 if (hlist_empty(&conn->list)) {
585 spin_unlock(lock);
586 break;
587 }
588 mark = hlist_entry(conn->list.first, struct fsnotify_mark,
589 obj_list);
590 /*
591 * We don't update i_fsnotify_mask / mnt_fsnotify_mask here
592 * since inode / mount is going away anyway. So just remove
593 * mark from the list.
594 */
595 hlist_del_init_rcu(&mark->obj_list);
596 fsnotify_get_mark(mark);
597 spin_unlock(lock);
598 fsnotify_destroy_mark(mark, mark->group);
599 fsnotify_put_mark(mark);
600 }
601}
602
Eric Paris5444e292009-12-17 21:24:27 -0500603/*
604 * Nothing fancy, just initialize lists and locks and counters.
605 */
606void fsnotify_init_mark(struct fsnotify_mark *mark,
607 void (*free_mark)(struct fsnotify_mark *mark))
608{
Eric Parisba643f02009-12-17 21:24:27 -0500609 memset(mark, 0, sizeof(*mark));
Eric Paris5444e292009-12-17 21:24:27 -0500610 spin_lock_init(&mark->lock);
611 atomic_set(&mark->refcnt, 1);
Eric Paris5444e292009-12-17 21:24:27 -0500612 mark->free_mark = free_mark;
613}
Jeff Layton13d34ac2016-02-17 13:11:18 -0800614
Jan Kara35e48172016-05-19 17:08:59 -0700615/*
616 * Destroy all marks in destroy_list, waits for SRCU period to finish before
617 * actually freeing marks.
618 */
619void fsnotify_mark_destroy_list(void)
Jeff Layton13d34ac2016-02-17 13:11:18 -0800620{
621 struct fsnotify_mark *mark, *next;
622 struct list_head private_destroy_list;
623
Jeff Layton0918f1c2016-02-17 13:11:21 -0800624 spin_lock(&destroy_lock);
625 /* exchange the list head */
626 list_replace_init(&destroy_list, &private_destroy_list);
627 spin_unlock(&destroy_lock);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800628
Jeff Layton0918f1c2016-02-17 13:11:21 -0800629 synchronize_srcu(&fsnotify_mark_srcu);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800630
Jeff Layton0918f1c2016-02-17 13:11:21 -0800631 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
632 list_del_init(&mark->g_list);
633 fsnotify_put_mark(mark);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800634 }
Jeff Layton13d34ac2016-02-17 13:11:18 -0800635}
Jan Kara35e48172016-05-19 17:08:59 -0700636
637static void fsnotify_mark_destroy_workfn(struct work_struct *work)
638{
639 fsnotify_mark_destroy_list();
640}