fsnotify: Move fsnotify_destroy_marks()
[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  * inode->i_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  * 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. Marks are also protected by fsnotify_mark_srcu.
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  * 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>
71 #include <linux/kthread.h>
72 #include <linux/module.h>
73 #include <linux/mutex.h>
74 #include <linux/slab.h>
75 #include <linux/spinlock.h>
76 #include <linux/srcu.h>
77
78 #include <linux/atomic.h>
79
80 #include <linux/fsnotify_backend.h>
81 #include "fsnotify.h"
82
83 #define FSNOTIFY_REAPER_DELAY   (1)     /* 1 jiffy */
84
85 struct srcu_struct fsnotify_mark_srcu;
86 struct kmem_cache *fsnotify_mark_connector_cachep;
87
88 static DEFINE_SPINLOCK(destroy_lock);
89 static LIST_HEAD(destroy_list);
90
91 static void fsnotify_mark_destroy_workfn(struct work_struct *work);
92 static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
93
94 void fsnotify_get_mark(struct fsnotify_mark *mark)
95 {
96         atomic_inc(&mark->refcnt);
97 }
98
99 void fsnotify_put_mark(struct fsnotify_mark *mark)
100 {
101         if (atomic_dec_and_test(&mark->refcnt)) {
102                 if (mark->group)
103                         fsnotify_put_group(mark->group);
104                 mark->free_mark(mark);
105         }
106 }
107
108 /* Calculate mask of events for a list of marks */
109 u32 fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
110 {
111         u32 new_mask = 0;
112         struct fsnotify_mark *mark;
113
114         if (!conn)
115                 return 0;
116
117         hlist_for_each_entry(mark, &conn->list, obj_list)
118                 new_mask |= mark->mask;
119         return new_mask;
120 }
121
122 /*
123  * 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.
127  */
128 void fsnotify_detach_mark(struct fsnotify_mark *mark)
129 {
130         struct inode *inode = NULL;
131         struct fsnotify_group *group = mark->group;
132
133         BUG_ON(!mutex_is_locked(&group->mark_mutex));
134
135         spin_lock(&mark->lock);
136
137         /* something else already called this function on this mark */
138         if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
139                 spin_unlock(&mark->lock);
140                 return;
141         }
142
143         mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
144
145         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)
148                 fsnotify_destroy_vfsmount_mark(mark);
149         else
150                 BUG();
151         /*
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          */
157
158         list_del_init(&mark->g_list);
159
160         spin_unlock(&mark->lock);
161
162         if (inode)
163                 iput(inode);
164
165         atomic_dec(&group->num_marks);
166 }
167
168 /*
169  * 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.
176  */
177 static bool __fsnotify_free_mark(struct fsnotify_mark *mark)
178 {
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);
185                 return false;
186         }
187         mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
188         spin_unlock(&mark->lock);
189
190         /*
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);
197
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  */
210 void 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         }
216 }
217
218 void fsnotify_destroy_mark(struct fsnotify_mark *mark,
219                            struct fsnotify_group *group)
220 {
221         mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
222         fsnotify_detach_mark(mark);
223         mutex_unlock(&group->mark_mutex);
224         fsnotify_free_mark(mark);
225 }
226
227 void fsnotify_connector_free(struct fsnotify_mark_connector **connp)
228 {
229         if (*connp) {
230                 kmem_cache_free(fsnotify_mark_connector_cachep, *connp);
231                 *connp = NULL;
232         }
233 }
234
235 void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
236 {
237         assert_spin_locked(&mark->lock);
238
239         mark->mask = mask;
240 }
241
242 void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
243 {
244         assert_spin_locked(&mark->lock);
245
246         mark->ignored_mask = mask;
247 }
248
249 /*
250  * Sorting function for lists of fsnotify marks.
251  *
252  * Fanotify supports different notification classes (reflected as priority of
253  * notification group). Events shall be passed to notification groups in
254  * decreasing priority order. To achieve this marks in notification lists for
255  * inodes and vfsmounts are sorted so that priorities of corresponding groups
256  * are descending.
257  *
258  * Furthermore correct handling of the ignore mask requires processing inode
259  * and vfsmount marks of each group together. Using the group address as
260  * further sort criterion provides a unique sorting order and thus we can
261  * merge inode and vfsmount lists of marks in linear time and find groups
262  * present in both lists.
263  *
264  * A return value of 1 signifies that b has priority over a.
265  * A return value of 0 signifies that the two marks have to be handled together.
266  * A return value of -1 signifies that a has priority over b.
267  */
268 int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
269 {
270         if (a == b)
271                 return 0;
272         if (!a)
273                 return 1;
274         if (!b)
275                 return -1;
276         if (a->priority < b->priority)
277                 return 1;
278         if (a->priority > b->priority)
279                 return -1;
280         if (a < b)
281                 return 1;
282         return -1;
283 }
284
285 static int fsnotify_attach_connector_to_object(
286                                         struct fsnotify_mark_connector **connp,
287                                         spinlock_t *lock,
288                                         struct inode *inode,
289                                         struct vfsmount *mnt)
290 {
291         struct fsnotify_mark_connector *conn;
292
293         conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
294         if (!conn)
295                 return -ENOMEM;
296         INIT_HLIST_HEAD(&conn->list);
297         if (inode) {
298                 conn->flags = FSNOTIFY_OBJ_TYPE_INODE;
299                 conn->inode = inode;
300         } else {
301                 conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
302                 conn->mnt = mnt;
303         }
304         /*
305          * Make sure 'conn' initialization is visible. Matches
306          * lockless_dereference() in fsnotify().
307          */
308         smp_wmb();
309         spin_lock(lock);
310         if (!*connp)
311                 *connp = conn;
312         else
313                 kmem_cache_free(fsnotify_mark_connector_cachep, conn);
314         spin_unlock(lock);
315
316         return 0;
317 }
318
319 /*
320  * Add mark into proper place in given list of marks. These marks may be used
321  * for the fsnotify backend to determine which event types should be delivered
322  * to which group and for which inodes. These marks are ordered according to
323  * priority, highest number first, and then by the group's location in memory.
324  */
325 static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
326                                   struct inode *inode, struct vfsmount *mnt,
327                                   int allow_dups)
328 {
329         struct fsnotify_mark *lmark, *last = NULL;
330         struct fsnotify_mark_connector *conn;
331         struct fsnotify_mark_connector **connp;
332         spinlock_t *lock;
333         int cmp;
334         int err = 0;
335
336         if (WARN_ON(!inode && !mnt))
337                 return -EINVAL;
338         if (inode) {
339                 connp = &inode->i_fsnotify_marks;
340                 lock = &inode->i_lock;
341         } else {
342                 connp = &real_mount(mnt)->mnt_fsnotify_marks;
343                 lock = &mnt->mnt_root->d_lock;
344         }
345
346         if (!*connp) {
347                 err = fsnotify_attach_connector_to_object(connp, lock,
348                                                           inode, mnt);
349                 if (err)
350                         return err;
351         }
352         spin_lock(&mark->lock);
353         spin_lock(lock);
354         conn = *connp;
355
356         /* is mark the first mark? */
357         if (hlist_empty(&conn->list)) {
358                 hlist_add_head_rcu(&mark->obj_list, &conn->list);
359                 if (inode)
360                         __iget(inode);
361                 goto added;
362         }
363
364         /* should mark be in the middle of the current list? */
365         hlist_for_each_entry(lmark, &conn->list, obj_list) {
366                 last = lmark;
367
368                 if ((lmark->group == mark->group) && !allow_dups) {
369                         err = -EEXIST;
370                         goto out_err;
371                 }
372
373                 cmp = fsnotify_compare_groups(lmark->group, mark->group);
374                 if (cmp >= 0) {
375                         hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
376                         goto added;
377                 }
378         }
379
380         BUG_ON(last == NULL);
381         /* mark should be the last entry.  last is the current last entry */
382         hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
383 added:
384         mark->connector = conn;
385 out_err:
386         spin_unlock(lock);
387         spin_unlock(&mark->lock);
388         return err;
389 }
390
391 /*
392  * Attach an initialized mark to a given group and fs object.
393  * These marks may be used for the fsnotify backend to determine which
394  * event types should be delivered to which group.
395  */
396 int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
397                              struct fsnotify_group *group, struct inode *inode,
398                              struct vfsmount *mnt, int allow_dups)
399 {
400         int ret = 0;
401
402         BUG_ON(inode && mnt);
403         BUG_ON(!inode && !mnt);
404         BUG_ON(!mutex_is_locked(&group->mark_mutex));
405
406         /*
407          * LOCKING ORDER!!!!
408          * group->mark_mutex
409          * mark->lock
410          * inode->i_lock
411          */
412         spin_lock(&mark->lock);
413         mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
414
415         fsnotify_get_group(group);
416         mark->group = group;
417         list_add(&mark->g_list, &group->marks_list);
418         atomic_inc(&group->num_marks);
419         fsnotify_get_mark(mark); /* for i_list and g_list */
420         spin_unlock(&mark->lock);
421
422         ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups);
423         if (ret)
424                 goto err;
425
426         if (inode)
427                 fsnotify_recalc_inode_mask(inode);
428         else
429                 fsnotify_recalc_vfsmount_mask(mnt);
430
431         return ret;
432 err:
433         mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
434         list_del_init(&mark->g_list);
435         fsnotify_put_group(group);
436         mark->group = NULL;
437         atomic_dec(&group->num_marks);
438
439         spin_unlock(&mark->lock);
440
441         spin_lock(&destroy_lock);
442         list_add(&mark->g_list, &destroy_list);
443         spin_unlock(&destroy_lock);
444         queue_delayed_work(system_unbound_wq, &reaper_work,
445                                 FSNOTIFY_REAPER_DELAY);
446
447         return ret;
448 }
449
450 int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
451                       struct inode *inode, struct vfsmount *mnt, int allow_dups)
452 {
453         int ret;
454         mutex_lock(&group->mark_mutex);
455         ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
456         mutex_unlock(&group->mark_mutex);
457         return ret;
458 }
459
460 /*
461  * Given a list of marks, find the mark associated with given group. If found
462  * take a reference to that mark and return it, else return NULL.
463  */
464 struct fsnotify_mark *fsnotify_find_mark(struct fsnotify_mark_connector *conn,
465                                          struct fsnotify_group *group)
466 {
467         struct fsnotify_mark *mark;
468
469         if (!conn)
470                 return NULL;
471
472         hlist_for_each_entry(mark, &conn->list, obj_list) {
473                 if (mark->group == group) {
474                         fsnotify_get_mark(mark);
475                         return mark;
476                 }
477         }
478         return NULL;
479 }
480
481 /*
482  * clear any marks in a group in which mark->flags & flags is true
483  */
484 void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
485                                          unsigned int flags)
486 {
487         struct fsnotify_mark *lmark, *mark;
488         LIST_HEAD(to_free);
489
490         /*
491          * We have to be really careful here. Anytime we drop mark_mutex, e.g.
492          * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
493          * to_free list so we have to use mark_mutex even when accessing that
494          * list. And freeing mark requires us to drop mark_mutex. So we can
495          * reliably free only the first mark in the list. That's why we first
496          * move marks to free to to_free list in one go and then free marks in
497          * to_free list one by one.
498          */
499         mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
500         list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
501                 if (mark->connector->flags & flags)
502                         list_move(&mark->g_list, &to_free);
503         }
504         mutex_unlock(&group->mark_mutex);
505
506         while (1) {
507                 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
508                 if (list_empty(&to_free)) {
509                         mutex_unlock(&group->mark_mutex);
510                         break;
511                 }
512                 mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
513                 fsnotify_get_mark(mark);
514                 fsnotify_detach_mark(mark);
515                 mutex_unlock(&group->mark_mutex);
516                 fsnotify_free_mark(mark);
517                 fsnotify_put_mark(mark);
518         }
519 }
520
521 /*
522  * Given a group, prepare for freeing all the marks associated with that group.
523  * The marks are attached to the list of marks prepared for destruction, the
524  * caller is responsible for freeing marks in that list after SRCU period has
525  * ended.
526  */
527 void fsnotify_detach_group_marks(struct fsnotify_group *group)
528 {
529         struct fsnotify_mark *mark;
530
531         while (1) {
532                 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
533                 if (list_empty(&group->marks_list)) {
534                         mutex_unlock(&group->mark_mutex);
535                         break;
536                 }
537                 mark = list_first_entry(&group->marks_list,
538                                         struct fsnotify_mark, g_list);
539                 fsnotify_get_mark(mark);
540                 fsnotify_detach_mark(mark);
541                 mutex_unlock(&group->mark_mutex);
542                 __fsnotify_free_mark(mark);
543                 fsnotify_put_mark(mark);
544         }
545 }
546
547 void fsnotify_destroy_marks(struct fsnotify_mark_connector *conn,
548                             spinlock_t *lock)
549 {
550         struct fsnotify_mark *mark;
551
552         if (!conn)
553                 return;
554
555         while (1) {
556                 /*
557                  * We have to be careful since we can race with e.g.
558                  * fsnotify_clear_marks_by_group() and once we drop 'lock',
559                  * mark can get removed from the obj_list and destroyed. But
560                  * we are holding mark reference so mark cannot be freed and
561                  * calling fsnotify_destroy_mark() more than once is fine.
562                  */
563                 spin_lock(lock);
564                 if (hlist_empty(&conn->list)) {
565                         spin_unlock(lock);
566                         break;
567                 }
568                 mark = hlist_entry(conn->list.first, struct fsnotify_mark,
569                                    obj_list);
570                 /*
571                  * We don't update i_fsnotify_mask / mnt_fsnotify_mask here
572                  * since inode / mount is going away anyway. So just remove
573                  * mark from the list.
574                  */
575                 hlist_del_init_rcu(&mark->obj_list);
576                 fsnotify_get_mark(mark);
577                 spin_unlock(lock);
578                 fsnotify_destroy_mark(mark, mark->group);
579                 fsnotify_put_mark(mark);
580         }
581 }
582
583 /*
584  * Nothing fancy, just initialize lists and locks and counters.
585  */
586 void fsnotify_init_mark(struct fsnotify_mark *mark,
587                         void (*free_mark)(struct fsnotify_mark *mark))
588 {
589         memset(mark, 0, sizeof(*mark));
590         spin_lock_init(&mark->lock);
591         atomic_set(&mark->refcnt, 1);
592         mark->free_mark = free_mark;
593 }
594
595 /*
596  * Destroy all marks in destroy_list, waits for SRCU period to finish before
597  * actually freeing marks.
598  */
599 void fsnotify_mark_destroy_list(void)
600 {
601         struct fsnotify_mark *mark, *next;
602         struct list_head private_destroy_list;
603
604         spin_lock(&destroy_lock);
605         /* exchange the list head */
606         list_replace_init(&destroy_list, &private_destroy_list);
607         spin_unlock(&destroy_lock);
608
609         synchronize_srcu(&fsnotify_mark_srcu);
610
611         list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
612                 list_del_init(&mark->g_list);
613                 fsnotify_put_mark(mark);
614         }
615 }
616
617 static void fsnotify_mark_destroy_workfn(struct work_struct *work)
618 {
619         fsnotify_mark_destroy_list();
620 }