blob: bfb415d0d7575dcefbd3c32a1ad999d07651a2f8 [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);
Jeff Layton0918f1c2016-02-17 13:11:21 -080092
Jan Kara35e48172016-05-19 17:08:59 -070093static void fsnotify_mark_destroy_workfn(struct work_struct *work);
94static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
Eric Paris75c1be42010-07-28 10:18:38 -040095
Eric Paris5444e292009-12-17 21:24:27 -050096void fsnotify_get_mark(struct fsnotify_mark *mark)
97{
98 atomic_inc(&mark->refcnt);
99}
100
101void fsnotify_put_mark(struct fsnotify_mark *mark)
102{
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200103 if (atomic_dec_and_test(&mark->refcnt)) {
104 if (mark->group)
105 fsnotify_put_group(mark->group);
Eric Paris5444e292009-12-17 21:24:27 -0500106 mark->free_mark(mark);
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200107 }
Eric Paris5444e292009-12-17 21:24:27 -0500108}
109
Jan Karaa2426772017-03-15 09:16:27 +0100110static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
Jan Kara0809ab62014-12-12 16:58:36 -0800111{
112 u32 new_mask = 0;
113 struct fsnotify_mark *mark;
114
Jan Kara04662ca2017-02-01 08:19:43 +0100115 assert_spin_locked(&conn->lock);
Jan Kara9dd813c2017-03-14 12:31:02 +0100116 hlist_for_each_entry(mark, &conn->list, obj_list)
Jan Kara0809ab62014-12-12 16:58:36 -0800117 new_mask |= mark->mask;
Jan Kara04662ca2017-02-01 08:19:43 +0100118
Jan Karaa2426772017-03-15 09:16:27 +0100119 if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
120 conn->inode->i_fsnotify_mask = new_mask;
121 else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT)
122 real_mount(conn->mnt)->mnt_fsnotify_mask = new_mask;
123}
124
125/*
126 * Calculate mask of events for a list of marks. The caller must make sure
127 * connector cannot disappear under us (usually by holding a mark->lock or
128 * mark->group->mark_mutex for a mark on this list).
129 */
130void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
131{
132 if (!conn)
133 return;
134
Jan Kara04662ca2017-02-01 08:19:43 +0100135 spin_lock(&conn->lock);
Jan Karaa2426772017-03-15 09:16:27 +0100136 __fsnotify_recalc_mask(conn);
Jan Kara04662ca2017-02-01 08:19:43 +0100137 spin_unlock(&conn->lock);
138 if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
Jan Karaa2426772017-03-15 09:16:27 +0100139 __fsnotify_update_child_dentry_flags(conn->inode);
Jan Kara0809ab62014-12-12 16:58:36 -0800140}
141
Jan Kara8212a602017-03-15 09:48:11 +0100142static struct inode *fsnotify_detach_from_object(struct fsnotify_mark *mark)
143{
144 struct fsnotify_mark_connector *conn;
145 struct inode *inode = NULL;
Jan Kara8212a602017-03-15 09:48:11 +0100146
147 conn = mark->connector;
Jan Kara04662ca2017-02-01 08:19:43 +0100148 spin_lock(&conn->lock);
Jan Kara8212a602017-03-15 09:48:11 +0100149 hlist_del_init_rcu(&mark->obj_list);
150 if (hlist_empty(&conn->list)) {
151 if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
152 inode = conn->inode;
153 }
Jan Kara73cd3c32017-03-30 09:14:47 +0200154 __fsnotify_recalc_mask(conn);
Jan Kara8212a602017-03-15 09:48:11 +0100155 mark->connector = NULL;
Jan Kara04662ca2017-02-01 08:19:43 +0100156 spin_unlock(&conn->lock);
Jan Kara8212a602017-03-15 09:48:11 +0100157
158 return inode;
159}
160
Eric Paris5444e292009-12-17 21:24:27 -0500161/*
Jan Kara4712e7222015-09-04 15:43:12 -0700162 * Remove mark from inode / vfsmount list, group list, drop inode reference
163 * if we got one.
164 *
165 * Must be called with group->mark_mutex held.
Eric Paris5444e292009-12-17 21:24:27 -0500166 */
Jan Kara4712e7222015-09-04 15:43:12 -0700167void fsnotify_detach_mark(struct fsnotify_mark *mark)
Eric Paris5444e292009-12-17 21:24:27 -0500168{
Eric Paris0d48b7f2009-12-17 21:24:27 -0500169 struct inode *inode = NULL;
Jan Kara4712e7222015-09-04 15:43:12 -0700170 struct fsnotify_group *group = mark->group;
Eric Paris5444e292009-12-17 21:24:27 -0500171
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200172 BUG_ON(!mutex_is_locked(&group->mark_mutex));
173
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200174 spin_lock(&mark->lock);
Eric Paris5444e292009-12-17 21:24:27 -0500175
Eric Paris700307a2010-07-28 10:18:38 -0400176 /* something else already called this function on this mark */
Jan Kara4712e7222015-09-04 15:43:12 -0700177 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
Eric Paris5444e292009-12-17 21:24:27 -0500178 spin_unlock(&mark->lock);
Lino Sanfilippoe2a29942011-06-14 17:29:51 +0200179 return;
Eric Paris5444e292009-12-17 21:24:27 -0500180 }
181
Jan Kara4712e7222015-09-04 15:43:12 -0700182 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
Eric Paris700307a2010-07-28 10:18:38 -0400183
Jan Kara8212a602017-03-15 09:48:11 +0100184 inode = fsnotify_detach_from_object(mark);
185
Jan Kara4712e7222015-09-04 15:43:12 -0700186 /*
187 * Note that we didn't update flags telling whether inode cares about
188 * what's happening with children. We update these flags from
189 * __fsnotify_parent() lazily when next event happens on one of our
190 * children.
191 */
Eric Paris5444e292009-12-17 21:24:27 -0500192
193 list_del_init(&mark->g_list);
Linus Torvaldsd725e662015-07-21 16:06:53 -0700194
Eric Paris5444e292009-12-17 21:24:27 -0500195 spin_unlock(&mark->lock);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200196
Jan Karae911d8a2017-03-14 14:48:00 +0100197 if (inode)
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200198 iput(inode);
Jan Kara4712e7222015-09-04 15:43:12 -0700199
200 atomic_dec(&group->num_marks);
201}
202
203/*
Jan Kara35e48172016-05-19 17:08:59 -0700204 * Prepare mark for freeing and add it to the list of marks prepared for
205 * freeing. The actual freeing must happen after SRCU period ends and the
206 * caller is responsible for this.
207 *
208 * The function returns true if the mark was added to the list of marks for
209 * freeing. The function returns false if someone else has already called
210 * __fsnotify_free_mark() for the mark.
Jan Kara4712e7222015-09-04 15:43:12 -0700211 */
Jan Kara35e48172016-05-19 17:08:59 -0700212static bool __fsnotify_free_mark(struct fsnotify_mark *mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700213{
214 struct fsnotify_group *group = mark->group;
215
216 spin_lock(&mark->lock);
217 /* something else already called this function on this mark */
218 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
219 spin_unlock(&mark->lock);
Jan Kara35e48172016-05-19 17:08:59 -0700220 return false;
Jan Kara4712e7222015-09-04 15:43:12 -0700221 }
222 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
223 spin_unlock(&mark->lock);
Eric Paris5444e292009-12-17 21:24:27 -0500224
Linus Torvaldsd725e662015-07-21 16:06:53 -0700225 /*
226 * Some groups like to know that marks are being freed. This is a
227 * callback to the group function to let it know that this mark
228 * is being freed.
229 */
230 if (group->ops->freeing_mark)
231 group->ops->freeing_mark(mark, group);
Jan Kara35e48172016-05-19 17:08:59 -0700232
233 spin_lock(&destroy_lock);
234 list_add(&mark->g_list, &destroy_list);
235 spin_unlock(&destroy_lock);
236
237 return true;
238}
239
240/*
241 * Free fsnotify mark. The freeing is actually happening from a workqueue which
242 * first waits for srcu period end. Caller must have a reference to the mark
243 * or be protected by fsnotify_mark_srcu.
244 */
245void fsnotify_free_mark(struct fsnotify_mark *mark)
246{
247 if (__fsnotify_free_mark(mark)) {
248 queue_delayed_work(system_unbound_wq, &reaper_work,
249 FSNOTIFY_REAPER_DELAY);
250 }
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200251}
252
253void fsnotify_destroy_mark(struct fsnotify_mark *mark,
254 struct fsnotify_group *group)
255{
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200256 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
Jan Kara4712e7222015-09-04 15:43:12 -0700257 fsnotify_detach_mark(mark);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200258 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700259 fsnotify_free_mark(mark);
Eric Paris5444e292009-12-17 21:24:27 -0500260}
261
Jan Kara9dd813c2017-03-14 12:31:02 +0100262void fsnotify_connector_free(struct fsnotify_mark_connector **connp)
263{
264 if (*connp) {
265 kmem_cache_free(fsnotify_mark_connector_cachep, *connp);
266 *connp = NULL;
267 }
268}
269
Eric Paris90b1e7a2009-12-17 21:24:33 -0500270void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
271{
272 assert_spin_locked(&mark->lock);
273
274 mark->mask = mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500275}
276
Eric Paris33af5e32009-12-17 21:24:33 -0500277void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
278{
279 assert_spin_locked(&mark->lock);
280
281 mark->ignored_mask = mask;
282}
Eric Paris90b1e7a2009-12-17 21:24:33 -0500283
Eric Paris5444e292009-12-17 21:24:27 -0500284/*
Jan Kara8edc6e12014-11-13 15:19:33 -0800285 * Sorting function for lists of fsnotify marks.
286 *
287 * Fanotify supports different notification classes (reflected as priority of
288 * notification group). Events shall be passed to notification groups in
289 * decreasing priority order. To achieve this marks in notification lists for
290 * inodes and vfsmounts are sorted so that priorities of corresponding groups
291 * are descending.
292 *
293 * Furthermore correct handling of the ignore mask requires processing inode
294 * and vfsmount marks of each group together. Using the group address as
295 * further sort criterion provides a unique sorting order and thus we can
296 * merge inode and vfsmount lists of marks in linear time and find groups
297 * present in both lists.
298 *
299 * A return value of 1 signifies that b has priority over a.
300 * A return value of 0 signifies that the two marks have to be handled together.
301 * A return value of -1 signifies that a has priority over b.
302 */
303int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
304{
305 if (a == b)
306 return 0;
307 if (!a)
308 return 1;
309 if (!b)
310 return -1;
311 if (a->priority < b->priority)
312 return 1;
313 if (a->priority > b->priority)
314 return -1;
315 if (a < b)
316 return 1;
317 return -1;
318}
319
Jan Kara9dd813c2017-03-14 12:31:02 +0100320static int fsnotify_attach_connector_to_object(
Jan Kara86ffe242017-03-14 14:29:35 +0100321 struct fsnotify_mark_connector **connp,
322 struct inode *inode,
323 struct vfsmount *mnt)
Jan Kara9dd813c2017-03-14 12:31:02 +0100324{
325 struct fsnotify_mark_connector *conn;
326
Jan Kara755b5bc2017-03-14 16:11:23 +0100327 conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
Jan Kara9dd813c2017-03-14 12:31:02 +0100328 if (!conn)
329 return -ENOMEM;
Jan Kara04662ca2017-02-01 08:19:43 +0100330 spin_lock_init(&conn->lock);
Jan Kara9dd813c2017-03-14 12:31:02 +0100331 INIT_HLIST_HEAD(&conn->list);
Jan Kara86ffe242017-03-14 14:29:35 +0100332 if (inode) {
333 conn->flags = FSNOTIFY_OBJ_TYPE_INODE;
334 conn->inode = inode;
335 } else {
336 conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
337 conn->mnt = mnt;
338 }
Jan Kara9dd813c2017-03-14 12:31:02 +0100339 /*
Jan Kara04662ca2017-02-01 08:19:43 +0100340 * cmpxchg() provides the barrier so that readers of *connp can see
341 * only initialized structure
Jan Kara9dd813c2017-03-14 12:31:02 +0100342 */
Jan Kara04662ca2017-02-01 08:19:43 +0100343 if (cmpxchg(connp, NULL, conn)) {
344 /* Someone else created list structure for us */
Jan Kara755b5bc2017-03-14 16:11:23 +0100345 kmem_cache_free(fsnotify_mark_connector_cachep, conn);
Jan Kara04662ca2017-02-01 08:19:43 +0100346 }
Jan Kara9dd813c2017-03-14 12:31:02 +0100347
348 return 0;
349}
350
351/*
352 * Add mark into proper place in given list of marks. These marks may be used
353 * for the fsnotify backend to determine which event types should be delivered
354 * to which group and for which inodes. These marks are ordered according to
355 * priority, highest number first, and then by the group's location in memory.
356 */
Jan Kara755b5bc2017-03-14 16:11:23 +0100357static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
358 struct inode *inode, struct vfsmount *mnt,
359 int allow_dups)
Jan Kara0809ab62014-12-12 16:58:36 -0800360{
361 struct fsnotify_mark *lmark, *last = NULL;
Jan Kara9dd813c2017-03-14 12:31:02 +0100362 struct fsnotify_mark_connector *conn;
Jan Kara755b5bc2017-03-14 16:11:23 +0100363 struct fsnotify_mark_connector **connp;
Jan Kara0809ab62014-12-12 16:58:36 -0800364 int cmp;
Jan Kara755b5bc2017-03-14 16:11:23 +0100365 int err = 0;
366
367 if (WARN_ON(!inode && !mnt))
368 return -EINVAL;
Jan Kara04662ca2017-02-01 08:19:43 +0100369 if (inode)
Jan Kara755b5bc2017-03-14 16:11:23 +0100370 connp = &inode->i_fsnotify_marks;
Jan Kara04662ca2017-02-01 08:19:43 +0100371 else
Jan Kara755b5bc2017-03-14 16:11:23 +0100372 connp = &real_mount(mnt)->mnt_fsnotify_marks;
Jan Kara9dd813c2017-03-14 12:31:02 +0100373
374 if (!*connp) {
Jan Kara04662ca2017-02-01 08:19:43 +0100375 err = fsnotify_attach_connector_to_object(connp, inode, mnt);
Jan Kara9dd813c2017-03-14 12:31:02 +0100376 if (err)
377 return err;
378 }
Jan Kara755b5bc2017-03-14 16:11:23 +0100379 spin_lock(&mark->lock);
Jan Kara9dd813c2017-03-14 12:31:02 +0100380 conn = *connp;
Jan Kara04662ca2017-02-01 08:19:43 +0100381 spin_lock(&conn->lock);
Jan Kara0809ab62014-12-12 16:58:36 -0800382
383 /* is mark the first mark? */
Jan Kara9dd813c2017-03-14 12:31:02 +0100384 if (hlist_empty(&conn->list)) {
385 hlist_add_head_rcu(&mark->obj_list, &conn->list);
Jan Karae911d8a2017-03-14 14:48:00 +0100386 if (inode)
Jan Kara04662ca2017-02-01 08:19:43 +0100387 igrab(inode);
Jan Kara86ffe242017-03-14 14:29:35 +0100388 goto added;
Jan Kara0809ab62014-12-12 16:58:36 -0800389 }
390
391 /* should mark be in the middle of the current list? */
Jan Kara9dd813c2017-03-14 12:31:02 +0100392 hlist_for_each_entry(lmark, &conn->list, obj_list) {
Jan Kara0809ab62014-12-12 16:58:36 -0800393 last = lmark;
394
Jan Kara755b5bc2017-03-14 16:11:23 +0100395 if ((lmark->group == mark->group) && !allow_dups) {
396 err = -EEXIST;
397 goto out_err;
398 }
Jan Kara0809ab62014-12-12 16:58:36 -0800399
400 cmp = fsnotify_compare_groups(lmark->group, mark->group);
401 if (cmp >= 0) {
402 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
Jan Kara86ffe242017-03-14 14:29:35 +0100403 goto added;
Jan Kara0809ab62014-12-12 16:58:36 -0800404 }
405 }
406
407 BUG_ON(last == NULL);
408 /* mark should be the last entry. last is the current last entry */
409 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
Jan Kara86ffe242017-03-14 14:29:35 +0100410added:
411 mark->connector = conn;
Jan Kara755b5bc2017-03-14 16:11:23 +0100412out_err:
Jan Kara04662ca2017-02-01 08:19:43 +0100413 spin_unlock(&conn->lock);
Jan Kara755b5bc2017-03-14 16:11:23 +0100414 spin_unlock(&mark->lock);
415 return err;
Jan Kara0809ab62014-12-12 16:58:36 -0800416}
417
Jan Kara8edc6e12014-11-13 15:19:33 -0800418/*
Eric Paris5444e292009-12-17 21:24:27 -0500419 * Attach an initialized mark to a given group and fs object.
420 * These marks may be used for the fsnotify backend to determine which
421 * event types should be delivered to which group.
422 */
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200423int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
424 struct fsnotify_group *group, struct inode *inode,
425 struct vfsmount *mnt, int allow_dups)
Eric Paris5444e292009-12-17 21:24:27 -0500426{
427 int ret = 0;
428
Eric Paris5444e292009-12-17 21:24:27 -0500429 BUG_ON(inode && mnt);
430 BUG_ON(!inode && !mnt);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200431 BUG_ON(!mutex_is_locked(&group->mark_mutex));
Eric Paris5444e292009-12-17 21:24:27 -0500432
433 /*
Eric Paris5444e292009-12-17 21:24:27 -0500434 * LOCKING ORDER!!!!
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200435 * group->mark_mutex
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200436 * mark->lock
Jan Kara04662ca2017-02-01 08:19:43 +0100437 * mark->connector->lock
Eric Paris5444e292009-12-17 21:24:27 -0500438 */
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200439 spin_lock(&mark->lock);
Jan Kara4712e7222015-09-04 15:43:12 -0700440 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
Eric Paris700307a2010-07-28 10:18:38 -0400441
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200442 fsnotify_get_group(group);
Eric Paris5444e292009-12-17 21:24:27 -0500443 mark->group = group;
444 list_add(&mark->g_list, &group->marks_list);
445 atomic_inc(&group->num_marks);
446 fsnotify_get_mark(mark); /* for i_list and g_list */
Eric Paris5444e292009-12-17 21:24:27 -0500447 spin_unlock(&mark->lock);
448
Jan Kara755b5bc2017-03-14 16:11:23 +0100449 ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups);
450 if (ret)
451 goto err;
452
Jan Karaa2426772017-03-15 09:16:27 +0100453 if (mark->mask)
454 fsnotify_recalc_mask(mark->connector);
Eric Paris5444e292009-12-17 21:24:27 -0500455
456 return ret;
457err:
Eric Paris700307a2010-07-28 10:18:38 -0400458 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
Eric Paris5444e292009-12-17 21:24:27 -0500459 list_del_init(&mark->g_list);
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200460 fsnotify_put_group(group);
Eric Paris75c1be42010-07-28 10:18:38 -0400461 mark->group = NULL;
Eric Paris5444e292009-12-17 21:24:27 -0500462 atomic_dec(&group->num_marks);
Eric Paris5444e292009-12-17 21:24:27 -0500463
Eric Paris5444e292009-12-17 21:24:27 -0500464 spin_unlock(&mark->lock);
465
Jeff Layton13d34ac2016-02-17 13:11:18 -0800466 spin_lock(&destroy_lock);
467 list_add(&mark->g_list, &destroy_list);
468 spin_unlock(&destroy_lock);
Jeff Layton0918f1c2016-02-17 13:11:21 -0800469 queue_delayed_work(system_unbound_wq, &reaper_work,
470 FSNOTIFY_REAPER_DELAY);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800471
Eric Paris5444e292009-12-17 21:24:27 -0500472 return ret;
473}
474
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200475int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
476 struct inode *inode, struct vfsmount *mnt, int allow_dups)
477{
478 int ret;
479 mutex_lock(&group->mark_mutex);
480 ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
481 mutex_unlock(&group->mark_mutex);
482 return ret;
483}
484
Eric Paris5444e292009-12-17 21:24:27 -0500485/*
Jan Kara0809ab62014-12-12 16:58:36 -0800486 * Given a list of marks, find the mark associated with given group. If found
487 * take a reference to that mark and return it, else return NULL.
488 */
Jan Kara9dd813c2017-03-14 12:31:02 +0100489struct fsnotify_mark *fsnotify_find_mark(struct fsnotify_mark_connector *conn,
Jan Kara0809ab62014-12-12 16:58:36 -0800490 struct fsnotify_group *group)
491{
492 struct fsnotify_mark *mark;
493
Jan Kara9dd813c2017-03-14 12:31:02 +0100494 if (!conn)
495 return NULL;
496
Jan Kara04662ca2017-02-01 08:19:43 +0100497 spin_lock(&conn->lock);
Jan Kara9dd813c2017-03-14 12:31:02 +0100498 hlist_for_each_entry(mark, &conn->list, obj_list) {
Jan Kara0809ab62014-12-12 16:58:36 -0800499 if (mark->group == group) {
500 fsnotify_get_mark(mark);
Jan Kara04662ca2017-02-01 08:19:43 +0100501 spin_unlock(&conn->lock);
Jan Kara0809ab62014-12-12 16:58:36 -0800502 return mark;
503 }
504 }
Jan Kara04662ca2017-02-01 08:19:43 +0100505 spin_unlock(&conn->lock);
Jan Kara0809ab62014-12-12 16:58:36 -0800506 return NULL;
507}
508
509/*
Linus Torvaldsd725e662015-07-21 16:06:53 -0700510 * clear any marks in a group in which mark->flags & flags is true
Eric Paris5444e292009-12-17 21:24:27 -0500511 */
Eric Paris4d926042009-12-17 21:24:34 -0500512void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
513 unsigned int flags)
Eric Paris5444e292009-12-17 21:24:27 -0500514{
515 struct fsnotify_mark *lmark, *mark;
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700516 LIST_HEAD(to_free);
Eric Paris5444e292009-12-17 21:24:27 -0500517
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700518 /*
519 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
520 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
521 * to_free list so we have to use mark_mutex even when accessing that
522 * list. And freeing mark requires us to drop mark_mutex. So we can
523 * reliably free only the first mark in the list. That's why we first
524 * move marks to free to to_free list in one go and then free marks in
525 * to_free list one by one.
526 */
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200527 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
Eric Paris5444e292009-12-17 21:24:27 -0500528 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
Jan Kara86ffe242017-03-14 14:29:35 +0100529 if (mark->connector->flags & flags)
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700530 list_move(&mark->g_list, &to_free);
Eric Paris5444e292009-12-17 21:24:27 -0500531 }
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200532 mutex_unlock(&group->mark_mutex);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700533
534 while (1) {
535 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
536 if (list_empty(&to_free)) {
537 mutex_unlock(&group->mark_mutex);
538 break;
539 }
540 mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
541 fsnotify_get_mark(mark);
Jan Kara4712e7222015-09-04 15:43:12 -0700542 fsnotify_detach_mark(mark);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700543 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700544 fsnotify_free_mark(mark);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700545 fsnotify_put_mark(mark);
546 }
Eric Paris5444e292009-12-17 21:24:27 -0500547}
548
Eric Paris4d926042009-12-17 21:24:34 -0500549/*
Jan Kara35e48172016-05-19 17:08:59 -0700550 * Given a group, prepare for freeing all the marks associated with that group.
551 * The marks are attached to the list of marks prepared for destruction, the
552 * caller is responsible for freeing marks in that list after SRCU period has
553 * ended.
Eric Paris4d926042009-12-17 21:24:34 -0500554 */
Jan Kara35e48172016-05-19 17:08:59 -0700555void fsnotify_detach_group_marks(struct fsnotify_group *group)
Eric Paris4d926042009-12-17 21:24:34 -0500556{
Jan Kara35e48172016-05-19 17:08:59 -0700557 struct fsnotify_mark *mark;
558
559 while (1) {
560 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
561 if (list_empty(&group->marks_list)) {
562 mutex_unlock(&group->mark_mutex);
563 break;
564 }
565 mark = list_first_entry(&group->marks_list,
566 struct fsnotify_mark, g_list);
567 fsnotify_get_mark(mark);
568 fsnotify_detach_mark(mark);
569 mutex_unlock(&group->mark_mutex);
570 __fsnotify_free_mark(mark);
571 fsnotify_put_mark(mark);
572 }
Eric Paris4d926042009-12-17 21:24:34 -0500573}
574
Jan Karaa03e2e42017-03-15 09:41:15 +0100575void fsnotify_destroy_marks(struct fsnotify_mark_connector *conn)
Jan Kara0810b4f2017-02-01 09:23:48 +0100576{
577 struct fsnotify_mark *mark;
578
579 if (!conn)
580 return;
581
582 while (1) {
583 /*
584 * We have to be careful since we can race with e.g.
585 * fsnotify_clear_marks_by_group() and once we drop 'lock',
586 * mark can get removed from the obj_list and destroyed. But
587 * we are holding mark reference so mark cannot be freed and
588 * calling fsnotify_destroy_mark() more than once is fine.
589 */
Jan Kara04662ca2017-02-01 08:19:43 +0100590 spin_lock(&conn->lock);
Jan Kara0810b4f2017-02-01 09:23:48 +0100591 if (hlist_empty(&conn->list)) {
Jan Kara04662ca2017-02-01 08:19:43 +0100592 spin_unlock(&conn->lock);
Jan Kara0810b4f2017-02-01 09:23:48 +0100593 break;
594 }
595 mark = hlist_entry(conn->list.first, struct fsnotify_mark,
596 obj_list);
Jan Kara0810b4f2017-02-01 09:23:48 +0100597 fsnotify_get_mark(mark);
Jan Kara04662ca2017-02-01 08:19:43 +0100598 spin_unlock(&conn->lock);
Jan Kara0810b4f2017-02-01 09:23:48 +0100599 fsnotify_destroy_mark(mark, mark->group);
600 fsnotify_put_mark(mark);
601 }
602}
603
Eric Paris5444e292009-12-17 21:24:27 -0500604/*
605 * Nothing fancy, just initialize lists and locks and counters.
606 */
607void fsnotify_init_mark(struct fsnotify_mark *mark,
608 void (*free_mark)(struct fsnotify_mark *mark))
609{
Eric Parisba643f02009-12-17 21:24:27 -0500610 memset(mark, 0, sizeof(*mark));
Eric Paris5444e292009-12-17 21:24:27 -0500611 spin_lock_init(&mark->lock);
612 atomic_set(&mark->refcnt, 1);
Eric Paris5444e292009-12-17 21:24:27 -0500613 mark->free_mark = free_mark;
614}
Jeff Layton13d34ac2016-02-17 13:11:18 -0800615
Jan Kara35e48172016-05-19 17:08:59 -0700616/*
617 * Destroy all marks in destroy_list, waits for SRCU period to finish before
618 * actually freeing marks.
619 */
620void fsnotify_mark_destroy_list(void)
Jeff Layton13d34ac2016-02-17 13:11:18 -0800621{
622 struct fsnotify_mark *mark, *next;
623 struct list_head private_destroy_list;
624
Jeff Layton0918f1c2016-02-17 13:11:21 -0800625 spin_lock(&destroy_lock);
626 /* exchange the list head */
627 list_replace_init(&destroy_list, &private_destroy_list);
628 spin_unlock(&destroy_lock);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800629
Jeff Layton0918f1c2016-02-17 13:11:21 -0800630 synchronize_srcu(&fsnotify_mark_srcu);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800631
Jeff Layton0918f1c2016-02-17 13:11:21 -0800632 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
633 list_del_init(&mark->g_list);
634 fsnotify_put_mark(mark);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800635 }
Jeff Layton13d34ac2016-02-17 13:11:18 -0800636}
Jan Kara35e48172016-05-19 17:08:59 -0700637
638static void fsnotify_mark_destroy_workfn(struct work_struct *work)
639{
640 fsnotify_mark_destroy_list();
641}