Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/sage/ceph...
[cascardo/linux.git] / fs / notify / mark.c
index d296ec9..325185e 100644 (file)
 #include <linux/fs.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
+#include <linux/kthread.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
+#include <linux/srcu.h>
 #include <linux/writeback.h> /* for inode_lock */
 
 #include <asm/atomic.h>
 #include <linux/fsnotify_backend.h>
 #include "fsnotify.h"
 
+struct srcu_struct fsnotify_mark_srcu;
+static DEFINE_SPINLOCK(destroy_lock);
+static LIST_HEAD(destroy_list);
+static DECLARE_WAIT_QUEUE_HEAD(destroy_waitq);
+
 void fsnotify_get_mark(struct fsnotify_mark *mark)
 {
        atomic_inc(&mark->refcnt);
@@ -121,33 +128,37 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark)
 
        group = mark->group;
 
-       /* if !group something else already marked this to die */
-       if (!group) {
+       /* something else already called this function on this mark */
+       if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
                spin_unlock(&mark->lock);
                return;
        }
 
+       mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
+
        /* 1 from caller and 1 for being on i_list/g_list */
        BUG_ON(atomic_read(&mark->refcnt) < 2);
 
        spin_lock(&group->mark_lock);
 
        if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
-               fsnotify_destroy_inode_mark(mark);
                inode = mark->i.inode;
+               fsnotify_destroy_inode_mark(mark);
        } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT)
                fsnotify_destroy_vfsmount_mark(mark);
        else
                BUG();
 
        list_del_init(&mark->g_list);
-       mark->group = NULL;
-
-       fsnotify_put_mark(mark); /* for i_list and g_list */
 
        spin_unlock(&group->mark_lock);
        spin_unlock(&mark->lock);
 
+       spin_lock(&destroy_lock);
+       list_add(&mark->destroy_list, &destroy_list);
+       spin_unlock(&destroy_lock);
+       wake_up(&destroy_waitq);
+
        /*
         * Some groups like to know that marks are being freed.  This is a
         * callback to the group function to let it know that this mark
@@ -168,7 +179,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark)
         * is just a lazy update (and could be a perf win...)
         */
 
-       if (inode)
+       if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED))
                iput(inode);
 
        /*
@@ -180,6 +191,23 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark)
                fsnotify_final_destroy_group(group);
 }
 
+void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
+{
+       assert_spin_locked(&mark->lock);
+
+       mark->mask = mask;
+
+       if (mark->flags & FSNOTIFY_MARK_FLAG_INODE)
+               fsnotify_set_inode_mark_mask_locked(mark, mask);
+}
+
+void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
+{
+       assert_spin_locked(&mark->lock);
+
+       mark->ignored_mask = mask;
+}
+
 /*
  * Attach an initialized mark to a given group and fs object.
  * These marks may be used for the fsnotify backend to determine which
@@ -194,15 +222,6 @@ int fsnotify_add_mark(struct fsnotify_mark *mark,
        BUG_ON(inode && mnt);
        BUG_ON(!inode && !mnt);
 
-       /*
-        * if this group isn't being testing for inode type events we need
-        * to start testing
-        */
-       if (inode && unlikely(list_empty(&group->inode_group_list)))
-               fsnotify_add_inode_group(group);
-       else if (mnt && unlikely(list_empty(&group->vfsmount_group_list)))
-               fsnotify_add_vfsmount_group(group);
-
        /*
         * LOCKING ORDER!!!!
         * mark->lock
@@ -212,6 +231,8 @@ int fsnotify_add_mark(struct fsnotify_mark *mark,
        spin_lock(&mark->lock);
        spin_lock(&group->mark_lock);
 
+       mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE;
+
        mark->group = group;
        list_add(&mark->g_list, &group->marks_list);
        atomic_inc(&group->num_marks);
@@ -230,6 +251,10 @@ int fsnotify_add_mark(struct fsnotify_mark *mark,
        }
 
        spin_unlock(&group->mark_lock);
+
+       /* this will pin the object if appropriate */
+       fsnotify_set_mark_mask_locked(mark, mark->mask);
+
        spin_unlock(&mark->lock);
 
        if (inode)
@@ -237,30 +262,38 @@ int fsnotify_add_mark(struct fsnotify_mark *mark,
 
        return ret;
 err:
-       mark->group = NULL;
+       mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
        list_del_init(&mark->g_list);
+       mark->group = NULL;
        atomic_dec(&group->num_marks);
-       fsnotify_put_mark(mark);
 
        spin_unlock(&group->mark_lock);
        spin_unlock(&mark->lock);
 
+       spin_lock(&destroy_lock);
+       list_add(&mark->destroy_list, &destroy_list);
+       spin_unlock(&destroy_lock);
+       wake_up(&destroy_waitq);
+
        return ret;
 }
 
 /*
- * Given a group, destroy all of the marks associated with that group.
+ * clear any marks in a group in which mark->flags & flags is true
  */
-void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
+void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
+                                        unsigned int flags)
 {
        struct fsnotify_mark *lmark, *mark;
        LIST_HEAD(free_list);
 
        spin_lock(&group->mark_lock);
        list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
-               list_add(&mark->free_g_list, &free_list);
-               list_del_init(&mark->g_list);
-               fsnotify_get_mark(mark);
+               if (mark->flags & flags) {
+                       list_add(&mark->free_g_list, &free_list);
+                       list_del_init(&mark->g_list);
+                       fsnotify_get_mark(mark);
+               }
        }
        spin_unlock(&group->mark_lock);
 
@@ -270,6 +303,14 @@ void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
        }
 }
 
+/*
+ * Given a group, destroy all of the marks associated with that group.
+ */
+void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
+{
+       fsnotify_clear_marks_by_group_flags(group, (unsigned int)-1);
+}
+
 void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old)
 {
        assert_spin_locked(&old->lock);
@@ -291,3 +332,40 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
        atomic_set(&mark->refcnt, 1);
        mark->free_mark = free_mark;
 }
+
+static int fsnotify_mark_destroy(void *ignored)
+{
+       struct fsnotify_mark *mark, *next;
+       LIST_HEAD(private_destroy_list);
+
+       for (;;) {
+               spin_lock(&destroy_lock);
+               /* exchange the list head */
+               list_replace_init(&destroy_list, &private_destroy_list);
+               spin_unlock(&destroy_lock);
+
+               synchronize_srcu(&fsnotify_mark_srcu);
+
+               list_for_each_entry_safe(mark, next, &private_destroy_list, destroy_list) {
+                       list_del_init(&mark->destroy_list);
+                       fsnotify_put_mark(mark);
+               }
+
+               wait_event_interruptible(destroy_waitq, !list_empty(&destroy_list));
+       }
+
+       return 0;
+}
+
+static int __init fsnotify_mark_init(void)
+{
+       struct task_struct *thread;
+
+       thread = kthread_run(fsnotify_mark_destroy, NULL,
+                            "fsnotify_mark");
+       if (IS_ERR(thread))
+               panic("unable to start fsnotify mark destruction thread.");
+
+       return 0;
+}
+device_initcall(fsnotify_mark_init);