blob: 7115c5d7d373c63df1512eba5ea90351ea5c7c45 [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
54 * refcnt==0.
55 *
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 *
64 * Worst case we are given an inode and need to clean up all the marks on that
65 * inode. We take i_lock and walk the i_fsnotify_marks safely. For each
66 * mark on the list we take a reference (so the mark can't disappear under us).
67 * We remove that mark form the inode's list of marks and we add this mark to a
Lino Sanfilippo9756b912013-07-08 15:59:46 -070068 * private list anchored on the stack using i_free_list; we walk i_free_list
69 * and before we destroy the mark we make sure that we dont race with a
70 * concurrent destroy_group by getting a ref to the marks group and taking the
71 * groups mutex.
72
Eric Paris5444e292009-12-17 21:24:27 -050073 * Very similarly for freeing by group, except we use free_g_list.
74 *
75 * This has the very interesting property of being able to run concurrently with
76 * any (or all) other directions.
77 */
78
79#include <linux/fs.h>
80#include <linux/init.h>
81#include <linux/kernel.h>
Eric Paris75c1be42010-07-28 10:18:38 -040082#include <linux/kthread.h>
Eric Paris5444e292009-12-17 21:24:27 -050083#include <linux/module.h>
84#include <linux/mutex.h>
85#include <linux/slab.h>
86#include <linux/spinlock.h>
Eric Paris75c1be42010-07-28 10:18:38 -040087#include <linux/srcu.h>
Eric Paris5444e292009-12-17 21:24:27 -050088
Arun Sharma600634972011-07-26 16:09:06 -070089#include <linux/atomic.h>
Eric Paris5444e292009-12-17 21:24:27 -050090
91#include <linux/fsnotify_backend.h>
92#include "fsnotify.h"
93
Jeff Layton0918f1c2016-02-17 13:11:21 -080094#define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */
95
Eric Paris75c1be42010-07-28 10:18:38 -040096struct srcu_struct fsnotify_mark_srcu;
Jeff Layton13d34ac2016-02-17 13:11:18 -080097static DEFINE_SPINLOCK(destroy_lock);
98static LIST_HEAD(destroy_list);
Jeff Layton0918f1c2016-02-17 13:11:21 -080099
100static void fsnotify_mark_destroy(struct work_struct *work);
101static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy);
Eric Paris75c1be42010-07-28 10:18:38 -0400102
Eric Paris5444e292009-12-17 21:24:27 -0500103void fsnotify_get_mark(struct fsnotify_mark *mark)
104{
105 atomic_inc(&mark->refcnt);
106}
107
108void fsnotify_put_mark(struct fsnotify_mark *mark)
109{
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200110 if (atomic_dec_and_test(&mark->refcnt)) {
111 if (mark->group)
112 fsnotify_put_group(mark->group);
Eric Paris5444e292009-12-17 21:24:27 -0500113 mark->free_mark(mark);
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200114 }
Eric Paris5444e292009-12-17 21:24:27 -0500115}
116
Jan Kara0809ab62014-12-12 16:58:36 -0800117/* Calculate mask of events for a list of marks */
118u32 fsnotify_recalc_mask(struct hlist_head *head)
119{
120 u32 new_mask = 0;
121 struct fsnotify_mark *mark;
122
123 hlist_for_each_entry(mark, head, obj_list)
124 new_mask |= mark->mask;
125 return new_mask;
126}
127
Eric Paris5444e292009-12-17 21:24:27 -0500128/*
Jan Kara4712e7222015-09-04 15:43:12 -0700129 * Remove mark from inode / vfsmount list, group list, drop inode reference
130 * if we got one.
131 *
132 * Must be called with group->mark_mutex held.
Eric Paris5444e292009-12-17 21:24:27 -0500133 */
Jan Kara4712e7222015-09-04 15:43:12 -0700134void fsnotify_detach_mark(struct fsnotify_mark *mark)
Eric Paris5444e292009-12-17 21:24:27 -0500135{
Eric Paris0d48b7f2009-12-17 21:24:27 -0500136 struct inode *inode = NULL;
Jan Kara4712e7222015-09-04 15:43:12 -0700137 struct fsnotify_group *group = mark->group;
Eric Paris5444e292009-12-17 21:24:27 -0500138
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200139 BUG_ON(!mutex_is_locked(&group->mark_mutex));
140
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200141 spin_lock(&mark->lock);
Eric Paris5444e292009-12-17 21:24:27 -0500142
Eric Paris700307a2010-07-28 10:18:38 -0400143 /* something else already called this function on this mark */
Jan Kara4712e7222015-09-04 15:43:12 -0700144 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
Eric Paris5444e292009-12-17 21:24:27 -0500145 spin_unlock(&mark->lock);
Lino Sanfilippoe2a29942011-06-14 17:29:51 +0200146 return;
Eric Paris5444e292009-12-17 21:24:27 -0500147 }
148
Jan Kara4712e7222015-09-04 15:43:12 -0700149 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
Eric Paris700307a2010-07-28 10:18:38 -0400150
Eric Paris0d48b7f2009-12-17 21:24:27 -0500151 if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
Jan Kara0809ab62014-12-12 16:58:36 -0800152 inode = mark->inode;
Eric Parisb31d3972010-04-21 16:49:38 -0400153 fsnotify_destroy_inode_mark(mark);
Eric Paris0d48b7f2009-12-17 21:24:27 -0500154 } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT)
155 fsnotify_destroy_vfsmount_mark(mark);
Eric Paris5444e292009-12-17 21:24:27 -0500156 else
157 BUG();
Jan Kara4712e7222015-09-04 15:43:12 -0700158 /*
159 * Note that we didn't update flags telling whether inode cares about
160 * what's happening with children. We update these flags from
161 * __fsnotify_parent() lazily when next event happens on one of our
162 * children.
163 */
Eric Paris5444e292009-12-17 21:24:27 -0500164
165 list_del_init(&mark->g_list);
Linus Torvaldsd725e662015-07-21 16:06:53 -0700166
Eric Paris5444e292009-12-17 21:24:27 -0500167 spin_unlock(&mark->lock);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200168
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200169 if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED))
170 iput(inode);
Jan Kara4712e7222015-09-04 15:43:12 -0700171
172 atomic_dec(&group->num_marks);
173}
174
175/*
Jeff Layton13d34ac2016-02-17 13:11:18 -0800176 * Free fsnotify mark. The freeing is actually happening from a kthread which
177 * first waits for srcu period end. Caller must have a reference to the mark
178 * or be protected by fsnotify_mark_srcu.
Jan Kara4712e7222015-09-04 15:43:12 -0700179 */
180void fsnotify_free_mark(struct fsnotify_mark *mark)
181{
182 struct fsnotify_group *group = mark->group;
183
184 spin_lock(&mark->lock);
185 /* something else already called this function on this mark */
186 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
187 spin_unlock(&mark->lock);
188 return;
189 }
190 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
191 spin_unlock(&mark->lock);
Eric Paris5444e292009-12-17 21:24:27 -0500192
Jeff Layton13d34ac2016-02-17 13:11:18 -0800193 spin_lock(&destroy_lock);
194 list_add(&mark->g_list, &destroy_list);
195 spin_unlock(&destroy_lock);
Jeff Layton0918f1c2016-02-17 13:11:21 -0800196 queue_delayed_work(system_unbound_wq, &reaper_work,
197 FSNOTIFY_REAPER_DELAY);
Linus Torvaldsd725e662015-07-21 16:06:53 -0700198
199 /*
200 * Some groups like to know that marks are being freed. This is a
201 * callback to the group function to let it know that this mark
202 * is being freed.
203 */
204 if (group->ops->freeing_mark)
205 group->ops->freeing_mark(mark, group);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200206}
207
208void fsnotify_destroy_mark(struct fsnotify_mark *mark,
209 struct fsnotify_group *group)
210{
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200211 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
Jan Kara4712e7222015-09-04 15:43:12 -0700212 fsnotify_detach_mark(mark);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200213 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700214 fsnotify_free_mark(mark);
Eric Paris5444e292009-12-17 21:24:27 -0500215}
216
Jan Kara925d1132015-09-04 15:43:09 -0700217void fsnotify_destroy_marks(struct hlist_head *head, spinlock_t *lock)
Jan Kara0809ab62014-12-12 16:58:36 -0800218{
Jan Kara925d1132015-09-04 15:43:09 -0700219 struct fsnotify_mark *mark;
Jan Kara0809ab62014-12-12 16:58:36 -0800220
Jan Kara925d1132015-09-04 15:43:09 -0700221 while (1) {
222 /*
223 * We have to be careful since we can race with e.g.
224 * fsnotify_clear_marks_by_group() and once we drop 'lock',
225 * mark can get removed from the obj_list and destroyed. But
226 * we are holding mark reference so mark cannot be freed and
227 * calling fsnotify_destroy_mark() more than once is fine.
228 */
229 spin_lock(lock);
230 if (hlist_empty(head)) {
231 spin_unlock(lock);
232 break;
233 }
234 mark = hlist_entry(head->first, struct fsnotify_mark, obj_list);
235 /*
236 * We don't update i_fsnotify_mask / mnt_fsnotify_mask here
237 * since inode / mount is going away anyway. So just remove
238 * mark from the list.
239 */
240 hlist_del_init_rcu(&mark->obj_list);
241 fsnotify_get_mark(mark);
242 spin_unlock(lock);
243 fsnotify_destroy_mark(mark, mark->group);
Jan Kara0809ab62014-12-12 16:58:36 -0800244 fsnotify_put_mark(mark);
Jan Kara0809ab62014-12-12 16:58:36 -0800245 }
246}
247
Eric Paris90b1e7a2009-12-17 21:24:33 -0500248void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
249{
250 assert_spin_locked(&mark->lock);
251
252 mark->mask = mask;
253
254 if (mark->flags & FSNOTIFY_MARK_FLAG_INODE)
255 fsnotify_set_inode_mark_mask_locked(mark, mask);
256}
257
Eric Paris33af5e32009-12-17 21:24:33 -0500258void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
259{
260 assert_spin_locked(&mark->lock);
261
262 mark->ignored_mask = mask;
263}
Eric Paris90b1e7a2009-12-17 21:24:33 -0500264
Eric Paris5444e292009-12-17 21:24:27 -0500265/*
Jan Kara8edc6e12014-11-13 15:19:33 -0800266 * Sorting function for lists of fsnotify marks.
267 *
268 * Fanotify supports different notification classes (reflected as priority of
269 * notification group). Events shall be passed to notification groups in
270 * decreasing priority order. To achieve this marks in notification lists for
271 * inodes and vfsmounts are sorted so that priorities of corresponding groups
272 * are descending.
273 *
274 * Furthermore correct handling of the ignore mask requires processing inode
275 * and vfsmount marks of each group together. Using the group address as
276 * further sort criterion provides a unique sorting order and thus we can
277 * merge inode and vfsmount lists of marks in linear time and find groups
278 * present in both lists.
279 *
280 * A return value of 1 signifies that b has priority over a.
281 * A return value of 0 signifies that the two marks have to be handled together.
282 * A return value of -1 signifies that a has priority over b.
283 */
284int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
285{
286 if (a == b)
287 return 0;
288 if (!a)
289 return 1;
290 if (!b)
291 return -1;
292 if (a->priority < b->priority)
293 return 1;
294 if (a->priority > b->priority)
295 return -1;
296 if (a < b)
297 return 1;
298 return -1;
299}
300
Jan Kara0809ab62014-12-12 16:58:36 -0800301/* Add mark into proper place in given list of marks */
302int fsnotify_add_mark_list(struct hlist_head *head, struct fsnotify_mark *mark,
303 int allow_dups)
304{
305 struct fsnotify_mark *lmark, *last = NULL;
306 int cmp;
307
308 /* is mark the first mark? */
309 if (hlist_empty(head)) {
310 hlist_add_head_rcu(&mark->obj_list, head);
311 return 0;
312 }
313
314 /* should mark be in the middle of the current list? */
315 hlist_for_each_entry(lmark, head, obj_list) {
316 last = lmark;
317
318 if ((lmark->group == mark->group) && !allow_dups)
319 return -EEXIST;
320
321 cmp = fsnotify_compare_groups(lmark->group, mark->group);
322 if (cmp >= 0) {
323 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
324 return 0;
325 }
326 }
327
328 BUG_ON(last == NULL);
329 /* mark should be the last entry. last is the current last entry */
330 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
331 return 0;
332}
333
Jan Kara8edc6e12014-11-13 15:19:33 -0800334/*
Eric Paris5444e292009-12-17 21:24:27 -0500335 * Attach an initialized mark to a given group and fs object.
336 * These marks may be used for the fsnotify backend to determine which
337 * event types should be delivered to which group.
338 */
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200339int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
340 struct fsnotify_group *group, struct inode *inode,
341 struct vfsmount *mnt, int allow_dups)
Eric Paris5444e292009-12-17 21:24:27 -0500342{
343 int ret = 0;
344
Eric Paris5444e292009-12-17 21:24:27 -0500345 BUG_ON(inode && mnt);
346 BUG_ON(!inode && !mnt);
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200347 BUG_ON(!mutex_is_locked(&group->mark_mutex));
Eric Paris5444e292009-12-17 21:24:27 -0500348
349 /*
Eric Paris5444e292009-12-17 21:24:27 -0500350 * LOCKING ORDER!!!!
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200351 * group->mark_mutex
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200352 * mark->lock
Eric Paris5444e292009-12-17 21:24:27 -0500353 * inode->i_lock
354 */
Lino Sanfilippo104d06f2011-06-14 17:29:48 +0200355 spin_lock(&mark->lock);
Jan Kara4712e7222015-09-04 15:43:12 -0700356 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
Eric Paris700307a2010-07-28 10:18:38 -0400357
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200358 fsnotify_get_group(group);
Eric Paris5444e292009-12-17 21:24:27 -0500359 mark->group = group;
360 list_add(&mark->g_list, &group->marks_list);
361 atomic_inc(&group->num_marks);
362 fsnotify_get_mark(mark); /* for i_list and g_list */
363
364 if (inode) {
365 ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups);
366 if (ret)
367 goto err;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500368 } else if (mnt) {
369 ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups);
370 if (ret)
371 goto err;
Eric Paris5444e292009-12-17 21:24:27 -0500372 } else {
373 BUG();
374 }
375
Eric Paris90b1e7a2009-12-17 21:24:33 -0500376 /* this will pin the object if appropriate */
377 fsnotify_set_mark_mask_locked(mark, mark->mask);
Eric Paris5444e292009-12-17 21:24:27 -0500378 spin_unlock(&mark->lock);
379
380 if (inode)
381 __fsnotify_update_child_dentry_flags(inode);
382
383 return ret;
384err:
Eric Paris700307a2010-07-28 10:18:38 -0400385 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
Eric Paris5444e292009-12-17 21:24:27 -0500386 list_del_init(&mark->g_list);
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200387 fsnotify_put_group(group);
Eric Paris75c1be42010-07-28 10:18:38 -0400388 mark->group = NULL;
Eric Paris5444e292009-12-17 21:24:27 -0500389 atomic_dec(&group->num_marks);
Eric Paris5444e292009-12-17 21:24:27 -0500390
Eric Paris5444e292009-12-17 21:24:27 -0500391 spin_unlock(&mark->lock);
392
Jeff Layton13d34ac2016-02-17 13:11:18 -0800393 spin_lock(&destroy_lock);
394 list_add(&mark->g_list, &destroy_list);
395 spin_unlock(&destroy_lock);
Jeff Layton0918f1c2016-02-17 13:11:21 -0800396 queue_delayed_work(system_unbound_wq, &reaper_work,
397 FSNOTIFY_REAPER_DELAY);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800398
Eric Paris5444e292009-12-17 21:24:27 -0500399 return ret;
400}
401
Lino Sanfilippod5a335b2011-06-14 17:29:52 +0200402int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
403 struct inode *inode, struct vfsmount *mnt, int allow_dups)
404{
405 int ret;
406 mutex_lock(&group->mark_mutex);
407 ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
408 mutex_unlock(&group->mark_mutex);
409 return ret;
410}
411
Eric Paris5444e292009-12-17 21:24:27 -0500412/*
Jan Kara0809ab62014-12-12 16:58:36 -0800413 * Given a list of marks, find the mark associated with given group. If found
414 * take a reference to that mark and return it, else return NULL.
415 */
416struct fsnotify_mark *fsnotify_find_mark(struct hlist_head *head,
417 struct fsnotify_group *group)
418{
419 struct fsnotify_mark *mark;
420
421 hlist_for_each_entry(mark, head, obj_list) {
422 if (mark->group == group) {
423 fsnotify_get_mark(mark);
424 return mark;
425 }
426 }
427 return NULL;
428}
429
430/*
Linus Torvaldsd725e662015-07-21 16:06:53 -0700431 * clear any marks in a group in which mark->flags & flags is true
Eric Paris5444e292009-12-17 21:24:27 -0500432 */
Eric Paris4d926042009-12-17 21:24:34 -0500433void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
434 unsigned int flags)
Eric Paris5444e292009-12-17 21:24:27 -0500435{
436 struct fsnotify_mark *lmark, *mark;
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700437 LIST_HEAD(to_free);
Eric Paris5444e292009-12-17 21:24:27 -0500438
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700439 /*
440 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
441 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
442 * to_free list so we have to use mark_mutex even when accessing that
443 * list. And freeing mark requires us to drop mark_mutex. So we can
444 * reliably free only the first mark in the list. That's why we first
445 * move marks to free to to_free list in one go and then free marks in
446 * to_free list one by one.
447 */
Lino Sanfilippo6960b0d2011-08-12 01:13:31 +0200448 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
Eric Paris5444e292009-12-17 21:24:27 -0500449 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700450 if (mark->flags & flags)
451 list_move(&mark->g_list, &to_free);
Eric Paris5444e292009-12-17 21:24:27 -0500452 }
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200453 mutex_unlock(&group->mark_mutex);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700454
455 while (1) {
456 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
457 if (list_empty(&to_free)) {
458 mutex_unlock(&group->mark_mutex);
459 break;
460 }
461 mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
462 fsnotify_get_mark(mark);
Jan Kara4712e7222015-09-04 15:43:12 -0700463 fsnotify_detach_mark(mark);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700464 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700465 fsnotify_free_mark(mark);
Jan Kara8f2f3eb2015-08-06 15:46:42 -0700466 fsnotify_put_mark(mark);
467 }
Eric Paris5444e292009-12-17 21:24:27 -0500468}
469
Eric Paris4d926042009-12-17 21:24:34 -0500470/*
471 * Given a group, destroy all of the marks associated with that group.
472 */
473void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
474{
475 fsnotify_clear_marks_by_group_flags(group, (unsigned int)-1);
476}
477
Eric Paris5444e292009-12-17 21:24:27 -0500478void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old)
479{
480 assert_spin_locked(&old->lock);
Jan Kara0809ab62014-12-12 16:58:36 -0800481 new->inode = old->inode;
482 new->mnt = old->mnt;
Lino Sanfilippo23e964c2011-06-14 17:29:47 +0200483 if (old->group)
484 fsnotify_get_group(old->group);
Eric Paris5444e292009-12-17 21:24:27 -0500485 new->group = old->group;
486 new->mask = old->mask;
487 new->free_mark = old->free_mark;
488}
489
490/*
491 * Nothing fancy, just initialize lists and locks and counters.
492 */
493void fsnotify_init_mark(struct fsnotify_mark *mark,
494 void (*free_mark)(struct fsnotify_mark *mark))
495{
Eric Parisba643f02009-12-17 21:24:27 -0500496 memset(mark, 0, sizeof(*mark));
Eric Paris5444e292009-12-17 21:24:27 -0500497 spin_lock_init(&mark->lock);
498 atomic_set(&mark->refcnt, 1);
Eric Paris5444e292009-12-17 21:24:27 -0500499 mark->free_mark = free_mark;
500}
Jeff Layton13d34ac2016-02-17 13:11:18 -0800501
Jeff Layton0918f1c2016-02-17 13:11:21 -0800502static void fsnotify_mark_destroy(struct work_struct *work)
Jeff Layton13d34ac2016-02-17 13:11:18 -0800503{
504 struct fsnotify_mark *mark, *next;
505 struct list_head private_destroy_list;
506
Jeff Layton0918f1c2016-02-17 13:11:21 -0800507 spin_lock(&destroy_lock);
508 /* exchange the list head */
509 list_replace_init(&destroy_list, &private_destroy_list);
510 spin_unlock(&destroy_lock);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800511
Jeff Layton0918f1c2016-02-17 13:11:21 -0800512 synchronize_srcu(&fsnotify_mark_srcu);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800513
Jeff Layton0918f1c2016-02-17 13:11:21 -0800514 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
515 list_del_init(&mark->g_list);
516 fsnotify_put_mark(mark);
Jeff Layton13d34ac2016-02-17 13:11:18 -0800517 }
Jeff Layton13d34ac2016-02-17 13:11:18 -0800518}