fsnotify: Move mark list head from object into dedicated structure
[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->flags & FSNOTIFY_MARK_FLAG_INODE) {
146                 inode = mark->inode;
147                 fsnotify_destroy_inode_mark(mark);
148         } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT)
149                 fsnotify_destroy_vfsmount_mark(mark);
150         else
151                 BUG();
152         /*
153          * Note that we didn't update flags telling whether inode cares about
154          * what's happening with children. We update these flags from
155          * __fsnotify_parent() lazily when next event happens on one of our
156          * children.
157          */
158
159         list_del_init(&mark->g_list);
160
161         spin_unlock(&mark->lock);
162
163         if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED))
164                 iput(inode);
165
166         atomic_dec(&group->num_marks);
167 }
168
169 /*
170  * Prepare mark for freeing and add it to the list of marks prepared for
171  * freeing. The actual freeing must happen after SRCU period ends and the
172  * caller is responsible for this.
173  *
174  * The function returns true if the mark was added to the list of marks for
175  * freeing. The function returns false if someone else has already called
176  * __fsnotify_free_mark() for the mark.
177  */
178 static bool __fsnotify_free_mark(struct fsnotify_mark *mark)
179 {
180         struct fsnotify_group *group = mark->group;
181
182         spin_lock(&mark->lock);
183         /* something else already called this function on this mark */
184         if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
185                 spin_unlock(&mark->lock);
186                 return false;
187         }
188         mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
189         spin_unlock(&mark->lock);
190
191         /*
192          * Some groups like to know that marks are being freed.  This is a
193          * callback to the group function to let it know that this mark
194          * is being freed.
195          */
196         if (group->ops->freeing_mark)
197                 group->ops->freeing_mark(mark, group);
198
199         spin_lock(&destroy_lock);
200         list_add(&mark->g_list, &destroy_list);
201         spin_unlock(&destroy_lock);
202
203         return true;
204 }
205
206 /*
207  * Free fsnotify mark. The freeing is actually happening from a workqueue which
208  * first waits for srcu period end. Caller must have a reference to the mark
209  * or be protected by fsnotify_mark_srcu.
210  */
211 void fsnotify_free_mark(struct fsnotify_mark *mark)
212 {
213         if (__fsnotify_free_mark(mark)) {
214                 queue_delayed_work(system_unbound_wq, &reaper_work,
215                                    FSNOTIFY_REAPER_DELAY);
216         }
217 }
218
219 void fsnotify_destroy_mark(struct fsnotify_mark *mark,
220                            struct fsnotify_group *group)
221 {
222         mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
223         fsnotify_detach_mark(mark);
224         mutex_unlock(&group->mark_mutex);
225         fsnotify_free_mark(mark);
226 }
227
228 void fsnotify_destroy_marks(struct fsnotify_mark_connector *conn,
229                             spinlock_t *lock)
230 {
231         struct fsnotify_mark *mark;
232
233         if (!conn)
234                 return;
235
236         while (1) {
237                 /*
238                  * We have to be careful since we can race with e.g.
239                  * fsnotify_clear_marks_by_group() and once we drop 'lock',
240                  * mark can get removed from the obj_list and destroyed. But
241                  * we are holding mark reference so mark cannot be freed and
242                  * calling fsnotify_destroy_mark() more than once is fine.
243                  */
244                 spin_lock(lock);
245                 if (hlist_empty(&conn->list)) {
246                         spin_unlock(lock);
247                         break;
248                 }
249                 mark = hlist_entry(conn->list.first, struct fsnotify_mark,
250                                    obj_list);
251                 /*
252                  * We don't update i_fsnotify_mask / mnt_fsnotify_mask here
253                  * since inode / mount is going away anyway. So just remove
254                  * mark from the list.
255                  */
256                 hlist_del_init_rcu(&mark->obj_list);
257                 fsnotify_get_mark(mark);
258                 spin_unlock(lock);
259                 fsnotify_destroy_mark(mark, mark->group);
260                 fsnotify_put_mark(mark);
261         }
262 }
263
264 void fsnotify_connector_free(struct fsnotify_mark_connector **connp)
265 {
266         if (*connp) {
267                 kmem_cache_free(fsnotify_mark_connector_cachep, *connp);
268                 *connp = NULL;
269         }
270 }
271
272 void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
273 {
274         assert_spin_locked(&mark->lock);
275
276         mark->mask = mask;
277
278         if (mark->flags & FSNOTIFY_MARK_FLAG_INODE)
279                 fsnotify_set_inode_mark_mask_locked(mark, mask);
280 }
281
282 void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
283 {
284         assert_spin_locked(&mark->lock);
285
286         mark->ignored_mask = mask;
287 }
288
289 /*
290  * Sorting function for lists of fsnotify marks.
291  *
292  * Fanotify supports different notification classes (reflected as priority of
293  * notification group). Events shall be passed to notification groups in
294  * decreasing priority order. To achieve this marks in notification lists for
295  * inodes and vfsmounts are sorted so that priorities of corresponding groups
296  * are descending.
297  *
298  * Furthermore correct handling of the ignore mask requires processing inode
299  * and vfsmount marks of each group together. Using the group address as
300  * further sort criterion provides a unique sorting order and thus we can
301  * merge inode and vfsmount lists of marks in linear time and find groups
302  * present in both lists.
303  *
304  * A return value of 1 signifies that b has priority over a.
305  * A return value of 0 signifies that the two marks have to be handled together.
306  * A return value of -1 signifies that a has priority over b.
307  */
308 int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
309 {
310         if (a == b)
311                 return 0;
312         if (!a)
313                 return 1;
314         if (!b)
315                 return -1;
316         if (a->priority < b->priority)
317                 return 1;
318         if (a->priority > b->priority)
319                 return -1;
320         if (a < b)
321                 return 1;
322         return -1;
323 }
324
325 static int fsnotify_attach_connector_to_object(
326                                         struct fsnotify_mark_connector **connp)
327 {
328         struct fsnotify_mark_connector *conn;
329
330         conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_ATOMIC);
331         if (!conn)
332                 return -ENOMEM;
333         INIT_HLIST_HEAD(&conn->list);
334         /*
335          * Make sure 'conn' initialization is visible. Matches
336          * lockless_dereference() in fsnotify().
337          */
338         smp_wmb();
339         *connp = conn;
340
341         return 0;
342 }
343
344 /*
345  * Add mark into proper place in given list of marks. These marks may be used
346  * for the fsnotify backend to determine which event types should be delivered
347  * to which group and for which inodes. These marks are ordered according to
348  * priority, highest number first, and then by the group's location in memory.
349  */
350 int fsnotify_add_mark_list(struct fsnotify_mark_connector **connp,
351                            struct fsnotify_mark *mark, int allow_dups)
352 {
353         struct fsnotify_mark *lmark, *last = NULL;
354         struct fsnotify_mark_connector *conn;
355         int cmp;
356         int err;
357
358         if (!*connp) {
359                 err = fsnotify_attach_connector_to_object(connp);
360                 if (err)
361                         return err;
362         }
363         conn = *connp;
364
365         /* is mark the first mark? */
366         if (hlist_empty(&conn->list)) {
367                 hlist_add_head_rcu(&mark->obj_list, &conn->list);
368                 return 0;
369         }
370
371         /* should mark be in the middle of the current list? */
372         hlist_for_each_entry(lmark, &conn->list, obj_list) {
373                 last = lmark;
374
375                 if ((lmark->group == mark->group) && !allow_dups)
376                         return -EEXIST;
377
378                 cmp = fsnotify_compare_groups(lmark->group, mark->group);
379                 if (cmp >= 0) {
380                         hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
381                         return 0;
382                 }
383         }
384
385         BUG_ON(last == NULL);
386         /* mark should be the last entry.  last is the current last entry */
387         hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
388         return 0;
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
421         if (inode) {
422                 ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups);
423                 if (ret)
424                         goto err;
425         } else if (mnt) {
426                 ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups);
427                 if (ret)
428                         goto err;
429         } else {
430                 BUG();
431         }
432
433         /* this will pin the object if appropriate */
434         fsnotify_set_mark_mask_locked(mark, mark->mask);
435         spin_unlock(&mark->lock);
436
437         if (inode)
438                 __fsnotify_update_child_dentry_flags(inode);
439
440         return ret;
441 err:
442         mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
443         list_del_init(&mark->g_list);
444         fsnotify_put_group(group);
445         mark->group = NULL;
446         atomic_dec(&group->num_marks);
447
448         spin_unlock(&mark->lock);
449
450         spin_lock(&destroy_lock);
451         list_add(&mark->g_list, &destroy_list);
452         spin_unlock(&destroy_lock);
453         queue_delayed_work(system_unbound_wq, &reaper_work,
454                                 FSNOTIFY_REAPER_DELAY);
455
456         return ret;
457 }
458
459 int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
460                       struct inode *inode, struct vfsmount *mnt, int allow_dups)
461 {
462         int ret;
463         mutex_lock(&group->mark_mutex);
464         ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
465         mutex_unlock(&group->mark_mutex);
466         return ret;
467 }
468
469 /*
470  * Given a list of marks, find the mark associated with given group. If found
471  * take a reference to that mark and return it, else return NULL.
472  */
473 struct fsnotify_mark *fsnotify_find_mark(struct fsnotify_mark_connector *conn,
474                                          struct fsnotify_group *group)
475 {
476         struct fsnotify_mark *mark;
477
478         if (!conn)
479                 return NULL;
480
481         hlist_for_each_entry(mark, &conn->list, obj_list) {
482                 if (mark->group == group) {
483                         fsnotify_get_mark(mark);
484                         return mark;
485                 }
486         }
487         return NULL;
488 }
489
490 /*
491  * clear any marks in a group in which mark->flags & flags is true
492  */
493 void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
494                                          unsigned int flags)
495 {
496         struct fsnotify_mark *lmark, *mark;
497         LIST_HEAD(to_free);
498
499         /*
500          * We have to be really careful here. Anytime we drop mark_mutex, e.g.
501          * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
502          * to_free list so we have to use mark_mutex even when accessing that
503          * list. And freeing mark requires us to drop mark_mutex. So we can
504          * reliably free only the first mark in the list. That's why we first
505          * move marks to free to to_free list in one go and then free marks in
506          * to_free list one by one.
507          */
508         mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
509         list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
510                 if (mark->flags & flags)
511                         list_move(&mark->g_list, &to_free);
512         }
513         mutex_unlock(&group->mark_mutex);
514
515         while (1) {
516                 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
517                 if (list_empty(&to_free)) {
518                         mutex_unlock(&group->mark_mutex);
519                         break;
520                 }
521                 mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
522                 fsnotify_get_mark(mark);
523                 fsnotify_detach_mark(mark);
524                 mutex_unlock(&group->mark_mutex);
525                 fsnotify_free_mark(mark);
526                 fsnotify_put_mark(mark);
527         }
528 }
529
530 /*
531  * Given a group, prepare for freeing all the marks associated with that group.
532  * The marks are attached to the list of marks prepared for destruction, the
533  * caller is responsible for freeing marks in that list after SRCU period has
534  * ended.
535  */
536 void fsnotify_detach_group_marks(struct fsnotify_group *group)
537 {
538         struct fsnotify_mark *mark;
539
540         while (1) {
541                 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
542                 if (list_empty(&group->marks_list)) {
543                         mutex_unlock(&group->mark_mutex);
544                         break;
545                 }
546                 mark = list_first_entry(&group->marks_list,
547                                         struct fsnotify_mark, g_list);
548                 fsnotify_get_mark(mark);
549                 fsnotify_detach_mark(mark);
550                 mutex_unlock(&group->mark_mutex);
551                 __fsnotify_free_mark(mark);
552                 fsnotify_put_mark(mark);
553         }
554 }
555
556 /*
557  * Nothing fancy, just initialize lists and locks and counters.
558  */
559 void fsnotify_init_mark(struct fsnotify_mark *mark,
560                         void (*free_mark)(struct fsnotify_mark *mark))
561 {
562         memset(mark, 0, sizeof(*mark));
563         spin_lock_init(&mark->lock);
564         atomic_set(&mark->refcnt, 1);
565         mark->free_mark = free_mark;
566 }
567
568 /*
569  * Destroy all marks in destroy_list, waits for SRCU period to finish before
570  * actually freeing marks.
571  */
572 void fsnotify_mark_destroy_list(void)
573 {
574         struct fsnotify_mark *mark, *next;
575         struct list_head private_destroy_list;
576
577         spin_lock(&destroy_lock);
578         /* exchange the list head */
579         list_replace_init(&destroy_list, &private_destroy_list);
580         spin_unlock(&destroy_lock);
581
582         synchronize_srcu(&fsnotify_mark_srcu);
583
584         list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
585                 list_del_init(&mark->g_list);
586                 fsnotify_put_mark(mark);
587         }
588 }
589
590 static void fsnotify_mark_destroy_workfn(struct work_struct *work)
591 {
592         fsnotify_mark_destroy_list();
593 }