blob: 8a15c64fbe806ecfaa2403b349b055013d146491 [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 Kara0809ab62014-12-12 16:58:36 -0800108/* Calculate mask of events for a list of marks */
Jan Kara9dd813c2017-03-14 12:31:02 +0100109u32 fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
Jan Kara0809ab62014-12-12 16:58:36 -0800110{
111 u32 new_mask = 0;
112 struct fsnotify_mark *mark;
113
Jan Kara9dd813c2017-03-14 12:31:02 +0100114 if (!conn)
115 return 0;
116
117 hlist_for_each_entry(mark, &conn->list, obj_list)
Jan Kara0809ab62014-12-12 16:58:36 -0800118 new_mask |= mark->mask;
119 return new_mask;
120}
121
Eric Paris5444e292009-12-17 21:24:27 -0500122/*
Jan Kara4712e7222015-09-04 15:43:12 -0700123 * Remove mark from inode / vfsmount list, group list, drop inode reference
124 * if we got one.
125 *
126 * Must be called with group->mark_mutex held.
Eric Paris5444e292009-12-17 21:24:27 -0500127 */
Jan Kara4712e7222015-09-04 15:43:12 -0700128void fsnotify_detach_mark(struct fsnotify_mark *mark)
Eric Paris5444e292009-12-17 21:24:27 -0500129{
Eric Paris0d48b7f2009-12-17 21:24:27 -0500130 struct inode *inode = NULL;
Jan Kara4712e7222015-09-04 15:43:12 -0700131 struct fsnotify_group *group = mark->group;
Eric Paris5444e292009-12-17 21:24:27 -0500132
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200133 BUG_ON(!mutex_is_locked(&group->mark_mutex));
134
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200135 spin_lock(&mark->lock);
Eric Paris5444e292009-12-17 21:24:27 -0500136
Eric Paris700307a2010-07-28 10:18:38 -0400137 /* something else already called this function on this mark */
Jan Kara4712e7222015-09-04 15:43:12 -0700138 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
Eric Paris5444e292009-12-17 21:24:27 -0500139 spin_unlock(&mark->lock);
Lino Sanfilippoe2a29942011-06-14 17:29:51 +0200140 return;
Eric Paris5444e292009-12-17 21:24:27 -0500141 }
142
Jan Kara4712e7222015-09-04 15:43:12 -0700143 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
Eric Paris700307a2010-07-28 10:18:38 -0400144
Jan Karae911d8a2017-03-14 14:48:00 +0100145 if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_INODE)
146 inode = fsnotify_destroy_inode_mark(mark);
147 else if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT)
Eric Paris0d48b7f2009-12-17 21:24:27 -0500148 fsnotify_destroy_vfsmount_mark(mark);
Eric Paris5444e292009-12-17 21:24:27 -0500149 else
150 BUG();
Jan Kara4712e7222015-09-04 15:43:12 -0700151 /*
152 * Note that we didn't update flags telling whether inode cares about
153 * what's happening with children. We update these flags from
154 * __fsnotify_parent() lazily when next event happens on one of our
155 * children.
156 */
Eric Paris5444e292009-12-17 21:24:27 -0500157
158 list_del_init(&mark->g_list);
Linus Torvaldsd725e662015-07-21 16:06:53 -0700159
Eric Paris5444e292009-12-17 21:24:27 -0500160 spin_unlock(&mark->lock);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200161
Jan Karae911d8a2017-03-14 14:48:00 +0100162 if (inode)
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200163 iput(inode);
Jan Kara4712e7222015-09-04 15:43:12 -0700164
165 atomic_dec(&group->num_marks);
166}
167
168/*
Jan Kara35e48172016-05-19 17:08:59 -0700169 * Prepare mark for freeing and add it to the list of marks prepared for
170 * freeing. The actual freeing must happen after SRCU period ends and the
171 * caller is responsible for this.
172 *
173 * The function returns true if the mark was added to the list of marks for
174 * freeing. The function returns false if someone else has already called
175 * __fsnotify_free_mark() for the mark.
Jan Kara4712e7222015-09-04 15:43:12 -0700176 */
Jan Kara35e48172016-05-19 17:08:59 -0700177static bool __fsnotify_free_mark(struct fsnotify_mark *mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700178{
179 struct fsnotify_group *group = mark->group;
180
181 spin_lock(&mark->lock);
182 /* something else already called this function on this mark */
183 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
184 spin_unlock(&mark->lock);
Jan Kara35e48172016-05-19 17:08:59 -0700185 return false;
Jan Kara4712e7222015-09-04 15:43:12 -0700186 }
187 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
188 spin_unlock(&mark->lock);
Eric Paris5444e292009-12-17 21:24:27 -0500189
Linus Torvaldsd725e662015-07-21 16:06:53 -0700190 /*
191 * Some groups like to know that marks are being freed. This is a
192 * callback to the group function to let it know that this mark
193 * is being freed.
194 */
195 if (group->ops->freeing_mark)
196 group->ops->freeing_mark(mark, group);
Jan Kara35e48172016-05-19 17:08:59 -0700197
198 spin_lock(&destroy_lock);
199 list_add(&mark->g_list, &destroy_list);
200 spin_unlock(&destroy_lock);
201
202 return true;
203}
204
205/*
206 * Free fsnotify mark. The freeing is actually happening from a workqueue which
207 * first waits for srcu period end. Caller must have a reference to the mark
208 * or be protected by fsnotify_mark_srcu.
209 */
210void fsnotify_free_mark(struct fsnotify_mark *mark)
211{
212 if (__fsnotify_free_mark(mark)) {
213 queue_delayed_work(system_unbound_wq, &reaper_work,
214 FSNOTIFY_REAPER_DELAY);
215 }
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200216}
217
218void fsnotify_destroy_mark(struct fsnotify_mark *mark,
219 struct fsnotify_group *group)
220{
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200221 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
Jan Kara4712e7222015-09-04 15:43:12 -0700222 fsnotify_detach_mark(mark);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200223 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700224 fsnotify_free_mark(mark);
Eric Paris5444e292009-12-17 21:24:27 -0500225}
226
Jan Kara9dd813c2017-03-14 12:31:02 +0100227void fsnotify_destroy_marks(struct fsnotify_mark_connector *conn,
228 spinlock_t *lock)
Jan Kara0809ab62014-12-12 16:58:36 -0800229{
Jan Kara925d1132015-09-04 15:43:09 -0700230 struct fsnotify_mark *mark;
Jan Kara0809ab62014-12-12 16:58:36 -0800231
Jan Kara9dd813c2017-03-14 12:31:02 +0100232 if (!conn)
233 return;
234
Jan Kara925d1132015-09-04 15:43:09 -0700235 while (1) {
236 /*
237 * We have to be careful since we can race with e.g.
238 * fsnotify_clear_marks_by_group() and once we drop 'lock',
239 * mark can get removed from the obj_list and destroyed. But
240 * we are holding mark reference so mark cannot be freed and
241 * calling fsnotify_destroy_mark() more than once is fine.
242 */
243 spin_lock(lock);
Jan Kara9dd813c2017-03-14 12:31:02 +0100244 if (hlist_empty(&conn->list)) {
Jan Kara925d1132015-09-04 15:43:09 -0700245 spin_unlock(lock);
246 break;
247 }
Jan Kara9dd813c2017-03-14 12:31:02 +0100248 mark = hlist_entry(conn->list.first, struct fsnotify_mark,
249 obj_list);
Jan Kara925d1132015-09-04 15:43:09 -0700250 /*
251 * We don't update i_fsnotify_mask / mnt_fsnotify_mask here
252 * since inode / mount is going away anyway. So just remove
253 * mark from the list.
254 */
255 hlist_del_init_rcu(&mark->obj_list);
256 fsnotify_get_mark(mark);
257 spin_unlock(lock);
258 fsnotify_destroy_mark(mark, mark->group);
Jan Kara0809ab62014-12-12 16:58:36 -0800259 fsnotify_put_mark(mark);
Jan Kara0809ab62014-12-12 16:58:36 -0800260 }
261}
262
Jan Kara9dd813c2017-03-14 12:31:02 +0100263void fsnotify_connector_free(struct fsnotify_mark_connector **connp)
264{
265 if (*connp) {
266 kmem_cache_free(fsnotify_mark_connector_cachep, *connp);
267 *connp = NULL;
268 }
269}
270
Eric Paris90b1e7a2009-12-17 21:24:33 -0500271void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
272{
273 assert_spin_locked(&mark->lock);
274
275 mark->mask = mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500276}
277
Eric Paris33af5e32009-12-17 21:24:33 -0500278void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
279{
280 assert_spin_locked(&mark->lock);
281
282 mark->ignored_mask = mask;
283}
Eric Paris90b1e7a2009-12-17 21:24:33 -0500284
Eric Paris5444e292009-12-17 21:24:27 -0500285/*
Jan Kara8edc6e12014-11-13 15:19:33 -0800286 * Sorting function for lists of fsnotify marks.
287 *
288 * Fanotify supports different notification classes (reflected as priority of
289 * notification group). Events shall be passed to notification groups in
290 * decreasing priority order. To achieve this marks in notification lists for
291 * inodes and vfsmounts are sorted so that priorities of corresponding groups
292 * are descending.
293 *
294 * Furthermore correct handling of the ignore mask requires processing inode
295 * and vfsmount marks of each group together. Using the group address as
296 * further sort criterion provides a unique sorting order and thus we can
297 * merge inode and vfsmount lists of marks in linear time and find groups
298 * present in both lists.
299 *
300 * A return value of 1 signifies that b has priority over a.
301 * A return value of 0 signifies that the two marks have to be handled together.
302 * A return value of -1 signifies that a has priority over b.
303 */
304int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
305{
306 if (a == b)
307 return 0;
308 if (!a)
309 return 1;
310 if (!b)
311 return -1;
312 if (a->priority < b->priority)
313 return 1;
314 if (a->priority > b->priority)
315 return -1;
316 if (a < b)
317 return 1;
318 return -1;
319}
320
Jan Kara9dd813c2017-03-14 12:31:02 +0100321static int fsnotify_attach_connector_to_object(
Jan Kara86ffe242017-03-14 14:29:35 +0100322 struct fsnotify_mark_connector **connp,
323 struct inode *inode,
324 struct vfsmount *mnt)
Jan Kara9dd813c2017-03-14 12:31:02 +0100325{
326 struct fsnotify_mark_connector *conn;
327
328 conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_ATOMIC);
329 if (!conn)
330 return -ENOMEM;
331 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 /*
340 * Make sure 'conn' initialization is visible. Matches
341 * lockless_dereference() in fsnotify().
342 */
343 smp_wmb();
344 *connp = conn;
345
346 return 0;
347}
348
349/*
350 * Add mark into proper place in given list of marks. These marks may be used
351 * for the fsnotify backend to determine which event types should be delivered
352 * to which group and for which inodes. These marks are ordered according to
353 * priority, highest number first, and then by the group's location in memory.
354 */
355int fsnotify_add_mark_list(struct fsnotify_mark_connector **connp,
Jan Kara86ffe242017-03-14 14:29:35 +0100356 struct fsnotify_mark *mark, struct inode *inode,
357 struct vfsmount *mnt, int allow_dups)
Jan Kara0809ab62014-12-12 16:58:36 -0800358{
359 struct fsnotify_mark *lmark, *last = NULL;
Jan Kara9dd813c2017-03-14 12:31:02 +0100360 struct fsnotify_mark_connector *conn;
Jan Kara0809ab62014-12-12 16:58:36 -0800361 int cmp;
Jan Kara9dd813c2017-03-14 12:31:02 +0100362 int err;
363
364 if (!*connp) {
Jan Kara86ffe242017-03-14 14:29:35 +0100365 err = fsnotify_attach_connector_to_object(connp, inode, mnt);
Jan Kara9dd813c2017-03-14 12:31:02 +0100366 if (err)
367 return err;
368 }
369 conn = *connp;
Jan Kara0809ab62014-12-12 16:58:36 -0800370
371 /* is mark the first mark? */
Jan Kara9dd813c2017-03-14 12:31:02 +0100372 if (hlist_empty(&conn->list)) {
373 hlist_add_head_rcu(&mark->obj_list, &conn->list);
Jan Karae911d8a2017-03-14 14:48:00 +0100374 if (inode)
375 __iget(inode);
Jan Kara86ffe242017-03-14 14:29:35 +0100376 goto added;
Jan Kara0809ab62014-12-12 16:58:36 -0800377 }
378
379 /* should mark be in the middle of the current list? */
Jan Kara9dd813c2017-03-14 12:31:02 +0100380 hlist_for_each_entry(lmark, &conn->list, obj_list) {
Jan Kara0809ab62014-12-12 16:58:36 -0800381 last = lmark;
382
383 if ((lmark->group == mark->group) && !allow_dups)
384 return -EEXIST;
385
386 cmp = fsnotify_compare_groups(lmark->group, mark->group);
387 if (cmp >= 0) {
388 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
Jan Kara86ffe242017-03-14 14:29:35 +0100389 goto added;
Jan Kara0809ab62014-12-12 16:58:36 -0800390 }
391 }
392
393 BUG_ON(last == NULL);
394 /* mark should be the last entry. last is the current last entry */
395 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
Jan Kara86ffe242017-03-14 14:29:35 +0100396added:
397 mark->connector = conn;
Jan Kara0809ab62014-12-12 16:58:36 -0800398 return 0;
399}
400
Jan Kara8edc6e12014-11-13 15:19:33 -0800401/*
Eric Paris5444e292009-12-17 21:24:27 -0500402 * Attach an initialized mark to a given group and fs object.
403 * These marks may be used for the fsnotify backend to determine which
404 * event types should be delivered to which group.
405 */
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200406int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
407 struct fsnotify_group *group, struct inode *inode,
408 struct vfsmount *mnt, int allow_dups)
Eric Paris5444e292009-12-17 21:24:27 -0500409{
410 int ret = 0;
411
Eric Paris5444e292009-12-17 21:24:27 -0500412 BUG_ON(inode && mnt);
413 BUG_ON(!inode && !mnt);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200414 BUG_ON(!mutex_is_locked(&group->mark_mutex));
Eric Paris5444e292009-12-17 21:24:27 -0500415
416 /*
Eric Paris5444e292009-12-17 21:24:27 -0500417 * LOCKING ORDER!!!!
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200418 * group->mark_mutex
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200419 * mark->lock
Eric Paris5444e292009-12-17 21:24:27 -0500420 * inode->i_lock
421 */
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200422 spin_lock(&mark->lock);
Jan Kara4712e7222015-09-04 15:43:12 -0700423 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
Eric Paris700307a2010-07-28 10:18:38 -0400424
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200425 fsnotify_get_group(group);
Eric Paris5444e292009-12-17 21:24:27 -0500426 mark->group = group;
427 list_add(&mark->g_list, &group->marks_list);
428 atomic_inc(&group->num_marks);
429 fsnotify_get_mark(mark); /* for i_list and g_list */
430
431 if (inode) {
432 ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups);
433 if (ret)
434 goto err;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500435 } else if (mnt) {
436 ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups);
437 if (ret)
438 goto err;
Eric Paris5444e292009-12-17 21:24:27 -0500439 } else {
440 BUG();
441 }
Eric Paris5444e292009-12-17 21:24:27 -0500442 spin_unlock(&mark->lock);
443
444 if (inode)
445 __fsnotify_update_child_dentry_flags(inode);
446
447 return ret;
448err:
Eric Paris700307a2010-07-28 10:18:38 -0400449 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
Eric Paris5444e292009-12-17 21:24:27 -0500450 list_del_init(&mark->g_list);
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200451 fsnotify_put_group(group);
Eric Paris75c1be42010-07-28 10:18:38 -0400452 mark->group = NULL;
Eric Paris5444e292009-12-17 21:24:27 -0500453 atomic_dec(&group->num_marks);
Eric Paris5444e292009-12-17 21:24:27 -0500454
Eric Paris5444e292009-12-17 21:24:27 -0500455 spin_unlock(&mark->lock);
456
Jeff Layton13d34ac2016-02-17 13:11:18 -0800457 spin_lock(&destroy_lock);
458 list_add(&mark->g_list, &destroy_list);
459 spin_unlock(&destroy_lock);
Jeff Layton0918f1c2016-02-17 13:11:21 -0800460 queue_delayed_work(system_unbound_wq, &reaper_work,
461 FSNOTIFY_REAPER_DELAY);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800462
Eric Paris5444e292009-12-17 21:24:27 -0500463 return ret;
464}
465
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200466int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
467 struct inode *inode, struct vfsmount *mnt, int allow_dups)
468{
469 int ret;
470 mutex_lock(&group->mark_mutex);
471 ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
472 mutex_unlock(&group->mark_mutex);
473 return ret;
474}
475
Eric Paris5444e292009-12-17 21:24:27 -0500476/*
Jan Kara0809ab62014-12-12 16:58:36 -0800477 * Given a list of marks, find the mark associated with given group. If found
478 * take a reference to that mark and return it, else return NULL.
479 */
Jan Kara9dd813c2017-03-14 12:31:02 +0100480struct fsnotify_mark *fsnotify_find_mark(struct fsnotify_mark_connector *conn,
Jan Kara0809ab62014-12-12 16:58:36 -0800481 struct fsnotify_group *group)
482{
483 struct fsnotify_mark *mark;
484
Jan Kara9dd813c2017-03-14 12:31:02 +0100485 if (!conn)
486 return NULL;
487
488 hlist_for_each_entry(mark, &conn->list, obj_list) {
Jan Kara0809ab62014-12-12 16:58:36 -0800489 if (mark->group == group) {
490 fsnotify_get_mark(mark);
491 return mark;
492 }
493 }
494 return NULL;
495}
496
497/*
Linus Torvaldsd725e662015-07-21 16:06:53 -0700498 * clear any marks in a group in which mark->flags & flags is true
Eric Paris5444e292009-12-17 21:24:27 -0500499 */
Eric Paris4d926042009-12-17 21:24:34 -0500500void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
501 unsigned int flags)
Eric Paris5444e292009-12-17 21:24:27 -0500502{
503 struct fsnotify_mark *lmark, *mark;
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700504 LIST_HEAD(to_free);
Eric Paris5444e292009-12-17 21:24:27 -0500505
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700506 /*
507 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
508 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
509 * to_free list so we have to use mark_mutex even when accessing that
510 * list. And freeing mark requires us to drop mark_mutex. So we can
511 * reliably free only the first mark in the list. That's why we first
512 * move marks to free to to_free list in one go and then free marks in
513 * to_free list one by one.
514 */
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200515 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
Eric Paris5444e292009-12-17 21:24:27 -0500516 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
Jan Kara86ffe242017-03-14 14:29:35 +0100517 if (mark->connector->flags & flags)
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700518 list_move(&mark->g_list, &to_free);
Eric Paris5444e292009-12-17 21:24:27 -0500519 }
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200520 mutex_unlock(&group->mark_mutex);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700521
522 while (1) {
523 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
524 if (list_empty(&to_free)) {
525 mutex_unlock(&group->mark_mutex);
526 break;
527 }
528 mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
529 fsnotify_get_mark(mark);
Jan Kara4712e7222015-09-04 15:43:12 -0700530 fsnotify_detach_mark(mark);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700531 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700532 fsnotify_free_mark(mark);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700533 fsnotify_put_mark(mark);
534 }
Eric Paris5444e292009-12-17 21:24:27 -0500535}
536
Eric Paris4d926042009-12-17 21:24:34 -0500537/*
Jan Kara35e48172016-05-19 17:08:59 -0700538 * Given a group, prepare for freeing all the marks associated with that group.
539 * The marks are attached to the list of marks prepared for destruction, the
540 * caller is responsible for freeing marks in that list after SRCU period has
541 * ended.
Eric Paris4d926042009-12-17 21:24:34 -0500542 */
Jan Kara35e48172016-05-19 17:08:59 -0700543void fsnotify_detach_group_marks(struct fsnotify_group *group)
Eric Paris4d926042009-12-17 21:24:34 -0500544{
Jan Kara35e48172016-05-19 17:08:59 -0700545 struct fsnotify_mark *mark;
546
547 while (1) {
548 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
549 if (list_empty(&group->marks_list)) {
550 mutex_unlock(&group->mark_mutex);
551 break;
552 }
553 mark = list_first_entry(&group->marks_list,
554 struct fsnotify_mark, g_list);
555 fsnotify_get_mark(mark);
556 fsnotify_detach_mark(mark);
557 mutex_unlock(&group->mark_mutex);
558 __fsnotify_free_mark(mark);
559 fsnotify_put_mark(mark);
560 }
Eric Paris4d926042009-12-17 21:24:34 -0500561}
562
Eric Paris5444e292009-12-17 21:24:27 -0500563/*
564 * Nothing fancy, just initialize lists and locks and counters.
565 */
566void fsnotify_init_mark(struct fsnotify_mark *mark,
567 void (*free_mark)(struct fsnotify_mark *mark))
568{
Eric Parisba643f02009-12-17 21:24:27 -0500569 memset(mark, 0, sizeof(*mark));
Eric Paris5444e292009-12-17 21:24:27 -0500570 spin_lock_init(&mark->lock);
571 atomic_set(&mark->refcnt, 1);
Eric Paris5444e292009-12-17 21:24:27 -0500572 mark->free_mark = free_mark;
573}
Jeff Layton13d34ac2016-02-17 13:11:18 -0800574
Jan Kara35e48172016-05-19 17:08:59 -0700575/*
576 * Destroy all marks in destroy_list, waits for SRCU period to finish before
577 * actually freeing marks.
578 */
579void fsnotify_mark_destroy_list(void)
Jeff Layton13d34ac2016-02-17 13:11:18 -0800580{
581 struct fsnotify_mark *mark, *next;
582 struct list_head private_destroy_list;
583
Jeff Layton0918f1c2016-02-17 13:11:21 -0800584 spin_lock(&destroy_lock);
585 /* exchange the list head */
586 list_replace_init(&destroy_list, &private_destroy_list);
587 spin_unlock(&destroy_lock);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800588
Jeff Layton0918f1c2016-02-17 13:11:21 -0800589 synchronize_srcu(&fsnotify_mark_srcu);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800590
Jeff Layton0918f1c2016-02-17 13:11:21 -0800591 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
592 list_del_init(&mark->g_list);
593 fsnotify_put_mark(mark);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800594 }
Jeff Layton13d34ac2016-02-17 13:11:18 -0800595}
Jan Kara35e48172016-05-19 17:08:59 -0700596
597static void fsnotify_mark_destroy_workfn(struct work_struct *work)
598{
599 fsnotify_mark_destroy_list();
600}