824095db5a3b155358ec842915b158532181b3ef
[sfrench/cifs-2.6.git] / fs / notify / mark.c
1 /*
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:
23  * 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.
29  *
30  * LOCKING:
31  * There are 3 locks involved with fsnotify inode marks and they MUST be taken
32  * in order as follows:
33  *
34  * group->mark_mutex
35  * mark->lock
36  * mark->connector->lock
37  *
38  * 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.
46  *
47  * mark->connector->lock protects the list of marks anchored inside an
48  * inode / vfsmount and each mark is hooked via the i_list.
49  *
50  * 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.
53  *
54  * LIFETIME:
55  * Inode marks survive between when they are added to an inode and when their
56  * refcnt==0. Marks are also protected by fsnotify_mark_srcu.
57  *
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  *
66  * 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>
73 #include <linux/kthread.h>
74 #include <linux/module.h>
75 #include <linux/mutex.h>
76 #include <linux/slab.h>
77 #include <linux/spinlock.h>
78 #include <linux/srcu.h>
79
80 #include <linux/atomic.h>
81
82 #include <linux/fsnotify_backend.h>
83 #include "fsnotify.h"
84
85 #define FSNOTIFY_REAPER_DELAY   (1)     /* 1 jiffy */
86
87 struct srcu_struct fsnotify_mark_srcu;
88 struct kmem_cache *fsnotify_mark_connector_cachep;
89
90 static DEFINE_SPINLOCK(destroy_lock);
91 static LIST_HEAD(destroy_list);
92 static struct fsnotify_mark_connector *connector_destroy_list;
93
94 static void fsnotify_mark_destroy_workfn(struct work_struct *work);
95 static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
96
97 static void fsnotify_connector_destroy_workfn(struct work_struct *work);
98 static DECLARE_WORK(connector_reaper_work, fsnotify_connector_destroy_workfn);
99
100 void fsnotify_get_mark(struct fsnotify_mark *mark)
101 {
102         atomic_inc(&mark->refcnt);
103 }
104
105 void fsnotify_put_mark(struct fsnotify_mark *mark)
106 {
107         if (atomic_dec_and_test(&mark->refcnt)) {
108                 if (mark->group)
109                         fsnotify_put_group(mark->group);
110                 mark->free_mark(mark);
111         }
112 }
113
114 static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
115 {
116         u32 new_mask = 0;
117         struct fsnotify_mark *mark;
118
119         assert_spin_locked(&conn->lock);
120         hlist_for_each_entry(mark, &conn->list, obj_list)
121                 new_mask |= mark->mask;
122
123         if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
124                 conn->inode->i_fsnotify_mask = new_mask;
125         else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT)
126                 real_mount(conn->mnt)->mnt_fsnotify_mask = new_mask;
127 }
128
129 /*
130  * Calculate mask of events for a list of marks. The caller must make sure
131  * connector cannot disappear under us (usually by holding a mark->lock or
132  * mark->group->mark_mutex for a mark on this list).
133  */
134 void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
135 {
136         if (!conn)
137                 return;
138
139         spin_lock(&conn->lock);
140         __fsnotify_recalc_mask(conn);
141         spin_unlock(&conn->lock);
142         if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
143                 __fsnotify_update_child_dentry_flags(conn->inode);
144 }
145
146 /* Free all connectors queued for freeing once SRCU period ends */
147 static void fsnotify_connector_destroy_workfn(struct work_struct *work)
148 {
149         struct fsnotify_mark_connector *conn, *free;
150
151         spin_lock(&destroy_lock);
152         conn = connector_destroy_list;
153         connector_destroy_list = NULL;
154         spin_unlock(&destroy_lock);
155
156         synchronize_srcu(&fsnotify_mark_srcu);
157         while (conn) {
158                 free = conn;
159                 conn = conn->destroy_next;
160                 kmem_cache_free(fsnotify_mark_connector_cachep, free);
161         }
162 }
163
164
165 static struct inode *fsnotify_detach_connector_from_object(
166                                         struct fsnotify_mark_connector *conn)
167 {
168         struct inode *inode = NULL;
169
170         if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) {
171                 inode = conn->inode;
172                 rcu_assign_pointer(inode->i_fsnotify_marks, NULL);
173                 inode->i_fsnotify_mask = 0;
174                 conn->inode = NULL;
175                 conn->flags &= ~FSNOTIFY_OBJ_TYPE_INODE;
176         } else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
177                 rcu_assign_pointer(real_mount(conn->mnt)->mnt_fsnotify_marks,
178                                    NULL);
179                 real_mount(conn->mnt)->mnt_fsnotify_mask = 0;
180                 conn->mnt = NULL;
181                 conn->flags &= ~FSNOTIFY_OBJ_TYPE_VFSMOUNT;
182         }
183
184         return inode;
185 }
186
187 static struct inode *fsnotify_detach_from_object(struct fsnotify_mark *mark)
188 {
189         struct fsnotify_mark_connector *conn;
190         struct inode *inode = NULL;
191         bool free_conn = false;
192
193         conn = mark->connector;
194         spin_lock(&conn->lock);
195         hlist_del_init_rcu(&mark->obj_list);
196         if (hlist_empty(&conn->list)) {
197                 inode = fsnotify_detach_connector_from_object(conn);
198                 free_conn = true;
199         } else {
200                 __fsnotify_recalc_mask(conn);
201         }
202         mark->connector = NULL;
203         spin_unlock(&conn->lock);
204
205         if (free_conn) {
206                 spin_lock(&destroy_lock);
207                 conn->destroy_next = connector_destroy_list;
208                 connector_destroy_list = conn;
209                 spin_unlock(&destroy_lock);
210                 queue_work(system_unbound_wq, &connector_reaper_work);
211         }
212
213         return inode;
214 }
215
216 /*
217  * Remove mark from inode / vfsmount list, group list, drop inode reference
218  * if we got one.
219  *
220  * Must be called with group->mark_mutex held.
221  */
222 void fsnotify_detach_mark(struct fsnotify_mark *mark)
223 {
224         struct inode *inode = NULL;
225         struct fsnotify_group *group = mark->group;
226
227         BUG_ON(!mutex_is_locked(&group->mark_mutex));
228
229         spin_lock(&mark->lock);
230
231         /* something else already called this function on this mark */
232         if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
233                 spin_unlock(&mark->lock);
234                 return;
235         }
236
237         mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
238
239         inode = fsnotify_detach_from_object(mark);
240
241         /*
242          * Note that we didn't update flags telling whether inode cares about
243          * what's happening with children. We update these flags from
244          * __fsnotify_parent() lazily when next event happens on one of our
245          * children.
246          */
247
248         list_del_init(&mark->g_list);
249
250         spin_unlock(&mark->lock);
251
252         if (inode)
253                 iput(inode);
254
255         atomic_dec(&group->num_marks);
256 }
257
258 /*
259  * Prepare mark for freeing and add it to the list of marks prepared for
260  * freeing. The actual freeing must happen after SRCU period ends and the
261  * caller is responsible for this.
262  *
263  * The function returns true if the mark was added to the list of marks for
264  * freeing. The function returns false if someone else has already called
265  * __fsnotify_free_mark() for the mark.
266  */
267 static bool __fsnotify_free_mark(struct fsnotify_mark *mark)
268 {
269         struct fsnotify_group *group = mark->group;
270
271         spin_lock(&mark->lock);
272         /* something else already called this function on this mark */
273         if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
274                 spin_unlock(&mark->lock);
275                 return false;
276         }
277         mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
278         spin_unlock(&mark->lock);
279
280         /*
281          * Some groups like to know that marks are being freed.  This is a
282          * callback to the group function to let it know that this mark
283          * is being freed.
284          */
285         if (group->ops->freeing_mark)
286                 group->ops->freeing_mark(mark, group);
287
288         spin_lock(&destroy_lock);
289         list_add(&mark->g_list, &destroy_list);
290         spin_unlock(&destroy_lock);
291
292         return true;
293 }
294
295 /*
296  * Free fsnotify mark. The freeing is actually happening from a workqueue which
297  * first waits for srcu period end. Caller must have a reference to the mark
298  * or be protected by fsnotify_mark_srcu.
299  */
300 void fsnotify_free_mark(struct fsnotify_mark *mark)
301 {
302         if (__fsnotify_free_mark(mark)) {
303                 queue_delayed_work(system_unbound_wq, &reaper_work,
304                                    FSNOTIFY_REAPER_DELAY);
305         }
306 }
307
308 void fsnotify_destroy_mark(struct fsnotify_mark *mark,
309                            struct fsnotify_group *group)
310 {
311         mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
312         fsnotify_detach_mark(mark);
313         mutex_unlock(&group->mark_mutex);
314         fsnotify_free_mark(mark);
315 }
316
317 void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
318 {
319         assert_spin_locked(&mark->lock);
320
321         mark->mask = mask;
322 }
323
324 void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
325 {
326         assert_spin_locked(&mark->lock);
327
328         mark->ignored_mask = mask;
329 }
330
331 /*
332  * Sorting function for lists of fsnotify marks.
333  *
334  * Fanotify supports different notification classes (reflected as priority of
335  * notification group). Events shall be passed to notification groups in
336  * decreasing priority order. To achieve this marks in notification lists for
337  * inodes and vfsmounts are sorted so that priorities of corresponding groups
338  * are descending.
339  *
340  * Furthermore correct handling of the ignore mask requires processing inode
341  * and vfsmount marks of each group together. Using the group address as
342  * further sort criterion provides a unique sorting order and thus we can
343  * merge inode and vfsmount lists of marks in linear time and find groups
344  * present in both lists.
345  *
346  * A return value of 1 signifies that b has priority over a.
347  * A return value of 0 signifies that the two marks have to be handled together.
348  * A return value of -1 signifies that a has priority over b.
349  */
350 int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
351 {
352         if (a == b)
353                 return 0;
354         if (!a)
355                 return 1;
356         if (!b)
357                 return -1;
358         if (a->priority < b->priority)
359                 return 1;
360         if (a->priority > b->priority)
361                 return -1;
362         if (a < b)
363                 return 1;
364         return -1;
365 }
366
367 static int fsnotify_attach_connector_to_object(
368                                 struct fsnotify_mark_connector __rcu **connp,
369                                 struct inode *inode,
370                                 struct vfsmount *mnt)
371 {
372         struct fsnotify_mark_connector *conn;
373
374         conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
375         if (!conn)
376                 return -ENOMEM;
377         spin_lock_init(&conn->lock);
378         INIT_HLIST_HEAD(&conn->list);
379         if (inode) {
380                 conn->flags = FSNOTIFY_OBJ_TYPE_INODE;
381                 conn->inode = igrab(inode);
382         } else {
383                 conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
384                 conn->mnt = mnt;
385         }
386         /*
387          * cmpxchg() provides the barrier so that readers of *connp can see
388          * only initialized structure
389          */
390         if (cmpxchg(connp, NULL, conn)) {
391                 /* Someone else created list structure for us */
392                 if (inode)
393                         iput(inode);
394                 kmem_cache_free(fsnotify_mark_connector_cachep, conn);
395         }
396
397         return 0;
398 }
399
400 /*
401  * Get mark connector, make sure it is alive and return with its lock held.
402  * This is for users that get connector pointer from inode or mount. Users that
403  * hold reference to a mark on the list may directly lock connector->lock as
404  * they are sure list cannot go away under them.
405  */
406 static struct fsnotify_mark_connector *fsnotify_grab_connector(
407                                 struct fsnotify_mark_connector __rcu **connp)
408 {
409         struct fsnotify_mark_connector *conn;
410         int idx;
411
412         idx = srcu_read_lock(&fsnotify_mark_srcu);
413         conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
414         if (!conn)
415                 goto out;
416         spin_lock(&conn->lock);
417         if (!(conn->flags & (FSNOTIFY_OBJ_TYPE_INODE |
418                              FSNOTIFY_OBJ_TYPE_VFSMOUNT))) {
419                 spin_unlock(&conn->lock);
420                 srcu_read_unlock(&fsnotify_mark_srcu, idx);
421                 return NULL;
422         }
423 out:
424         srcu_read_unlock(&fsnotify_mark_srcu, idx);
425         return conn;
426 }
427
428 /*
429  * Add mark into proper place in given list of marks. These marks may be used
430  * for the fsnotify backend to determine which event types should be delivered
431  * to which group and for which inodes. These marks are ordered according to
432  * priority, highest number first, and then by the group's location in memory.
433  */
434 static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
435                                   struct inode *inode, struct vfsmount *mnt,
436                                   int allow_dups)
437 {
438         struct fsnotify_mark *lmark, *last = NULL;
439         struct fsnotify_mark_connector *conn;
440         struct fsnotify_mark_connector __rcu **connp;
441         int cmp;
442         int err = 0;
443
444         if (WARN_ON(!inode && !mnt))
445                 return -EINVAL;
446         if (inode)
447                 connp = &inode->i_fsnotify_marks;
448         else
449                 connp = &real_mount(mnt)->mnt_fsnotify_marks;
450 restart:
451         spin_lock(&mark->lock);
452         conn = fsnotify_grab_connector(connp);
453         if (!conn) {
454                 spin_unlock(&mark->lock);
455                 err = fsnotify_attach_connector_to_object(connp, inode, mnt);
456                 if (err)
457                         return err;
458                 goto restart;
459         }
460
461         /* is mark the first mark? */
462         if (hlist_empty(&conn->list)) {
463                 hlist_add_head_rcu(&mark->obj_list, &conn->list);
464                 goto added;
465         }
466
467         /* should mark be in the middle of the current list? */
468         hlist_for_each_entry(lmark, &conn->list, obj_list) {
469                 last = lmark;
470
471                 if ((lmark->group == mark->group) && !allow_dups) {
472                         err = -EEXIST;
473                         goto out_err;
474                 }
475
476                 cmp = fsnotify_compare_groups(lmark->group, mark->group);
477                 if (cmp >= 0) {
478                         hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
479                         goto added;
480                 }
481         }
482
483         BUG_ON(last == NULL);
484         /* mark should be the last entry.  last is the current last entry */
485         hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
486 added:
487         mark->connector = conn;
488 out_err:
489         spin_unlock(&conn->lock);
490         spin_unlock(&mark->lock);
491         return err;
492 }
493
494 /*
495  * Attach an initialized mark to a given group and fs object.
496  * These marks may be used for the fsnotify backend to determine which
497  * event types should be delivered to which group.
498  */
499 int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
500                              struct fsnotify_group *group, struct inode *inode,
501                              struct vfsmount *mnt, int allow_dups)
502 {
503         int ret = 0;
504
505         BUG_ON(inode && mnt);
506         BUG_ON(!inode && !mnt);
507         BUG_ON(!mutex_is_locked(&group->mark_mutex));
508
509         /*
510          * LOCKING ORDER!!!!
511          * group->mark_mutex
512          * mark->lock
513          * mark->connector->lock
514          */
515         spin_lock(&mark->lock);
516         mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
517
518         fsnotify_get_group(group);
519         mark->group = group;
520         list_add(&mark->g_list, &group->marks_list);
521         atomic_inc(&group->num_marks);
522         fsnotify_get_mark(mark); /* for i_list and g_list */
523         spin_unlock(&mark->lock);
524
525         ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups);
526         if (ret)
527                 goto err;
528
529         if (mark->mask)
530                 fsnotify_recalc_mask(mark->connector);
531
532         return ret;
533 err:
534         mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
535         list_del_init(&mark->g_list);
536         fsnotify_put_group(group);
537         mark->group = NULL;
538         atomic_dec(&group->num_marks);
539
540         spin_unlock(&mark->lock);
541
542         spin_lock(&destroy_lock);
543         list_add(&mark->g_list, &destroy_list);
544         spin_unlock(&destroy_lock);
545         queue_delayed_work(system_unbound_wq, &reaper_work,
546                                 FSNOTIFY_REAPER_DELAY);
547
548         return ret;
549 }
550
551 int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
552                       struct inode *inode, struct vfsmount *mnt, int allow_dups)
553 {
554         int ret;
555         mutex_lock(&group->mark_mutex);
556         ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
557         mutex_unlock(&group->mark_mutex);
558         return ret;
559 }
560
561 /*
562  * Given a list of marks, find the mark associated with given group. If found
563  * take a reference to that mark and return it, else return NULL.
564  */
565 struct fsnotify_mark *fsnotify_find_mark(
566                                 struct fsnotify_mark_connector __rcu **connp,
567                                 struct fsnotify_group *group)
568 {
569         struct fsnotify_mark_connector *conn;
570         struct fsnotify_mark *mark;
571
572         conn = fsnotify_grab_connector(connp);
573         if (!conn)
574                 return NULL;
575
576         hlist_for_each_entry(mark, &conn->list, obj_list) {
577                 if (mark->group == group) {
578                         fsnotify_get_mark(mark);
579                         spin_unlock(&conn->lock);
580                         return mark;
581                 }
582         }
583         spin_unlock(&conn->lock);
584         return NULL;
585 }
586
587 /*
588  * clear any marks in a group in which mark->flags & flags is true
589  */
590 void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
591                                          unsigned int flags)
592 {
593         struct fsnotify_mark *lmark, *mark;
594         LIST_HEAD(to_free);
595
596         /*
597          * We have to be really careful here. Anytime we drop mark_mutex, e.g.
598          * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
599          * to_free list so we have to use mark_mutex even when accessing that
600          * list. And freeing mark requires us to drop mark_mutex. So we can
601          * reliably free only the first mark in the list. That's why we first
602          * move marks to free to to_free list in one go and then free marks in
603          * to_free list one by one.
604          */
605         mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
606         list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
607                 if (mark->connector->flags & flags)
608                         list_move(&mark->g_list, &to_free);
609         }
610         mutex_unlock(&group->mark_mutex);
611
612         while (1) {
613                 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
614                 if (list_empty(&to_free)) {
615                         mutex_unlock(&group->mark_mutex);
616                         break;
617                 }
618                 mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
619                 fsnotify_get_mark(mark);
620                 fsnotify_detach_mark(mark);
621                 mutex_unlock(&group->mark_mutex);
622                 fsnotify_free_mark(mark);
623                 fsnotify_put_mark(mark);
624         }
625 }
626
627 /*
628  * Given a group, prepare for freeing all the marks associated with that group.
629  * The marks are attached to the list of marks prepared for destruction, the
630  * caller is responsible for freeing marks in that list after SRCU period has
631  * ended.
632  */
633 void fsnotify_detach_group_marks(struct fsnotify_group *group)
634 {
635         struct fsnotify_mark *mark;
636
637         while (1) {
638                 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
639                 if (list_empty(&group->marks_list)) {
640                         mutex_unlock(&group->mark_mutex);
641                         break;
642                 }
643                 mark = list_first_entry(&group->marks_list,
644                                         struct fsnotify_mark, g_list);
645                 fsnotify_get_mark(mark);
646                 fsnotify_detach_mark(mark);
647                 mutex_unlock(&group->mark_mutex);
648                 __fsnotify_free_mark(mark);
649                 fsnotify_put_mark(mark);
650         }
651 }
652
653 /* Destroy all marks attached to inode / vfsmount */
654 void fsnotify_destroy_marks(struct fsnotify_mark_connector __rcu **connp)
655 {
656         struct fsnotify_mark_connector *conn;
657         struct fsnotify_mark *mark;
658
659         while ((conn = fsnotify_grab_connector(connp))) {
660                 /*
661                  * We have to be careful since we can race with e.g.
662                  * fsnotify_clear_marks_by_group() and once we drop the list
663                  * lock, mark can get removed from the obj_list and destroyed.
664                  * But we are holding mark reference so mark cannot be freed
665                  * and calling fsnotify_destroy_mark() more than once is fine.
666                  */
667                 mark = hlist_entry(conn->list.first, struct fsnotify_mark,
668                                    obj_list);
669                 fsnotify_get_mark(mark);
670                 spin_unlock(&conn->lock);
671                 fsnotify_destroy_mark(mark, mark->group);
672                 fsnotify_put_mark(mark);
673         }
674 }
675
676 /*
677  * Nothing fancy, just initialize lists and locks and counters.
678  */
679 void fsnotify_init_mark(struct fsnotify_mark *mark,
680                         void (*free_mark)(struct fsnotify_mark *mark))
681 {
682         memset(mark, 0, sizeof(*mark));
683         spin_lock_init(&mark->lock);
684         atomic_set(&mark->refcnt, 1);
685         mark->free_mark = free_mark;
686 }
687
688 /*
689  * Destroy all marks in destroy_list, waits for SRCU period to finish before
690  * actually freeing marks.
691  */
692 void fsnotify_mark_destroy_list(void)
693 {
694         struct fsnotify_mark *mark, *next;
695         struct list_head private_destroy_list;
696
697         spin_lock(&destroy_lock);
698         /* exchange the list head */
699         list_replace_init(&destroy_list, &private_destroy_list);
700         spin_unlock(&destroy_lock);
701
702         synchronize_srcu(&fsnotify_mark_srcu);
703
704         list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
705                 list_del_init(&mark->g_list);
706                 fsnotify_put_mark(mark);
707         }
708 }
709
710 static void fsnotify_mark_destroy_workfn(struct work_struct *work)
711 {
712         fsnotify_mark_destroy_list();
713 }