fsnotify: pass group to fsnotify_destroy_mark()
[linux-2.6-block.git] / fs / notify / mark.c
index f104d565b6823aa75ab39c5c0717e9e3fd9382f3..b77c833c8d0aab82fb4efca05ed8bf8406fd25f8 100644 (file)
@@ -109,8 +109,11 @@ void fsnotify_get_mark(struct fsnotify_mark *mark)
 
 void fsnotify_put_mark(struct fsnotify_mark *mark)
 {
-       if (atomic_dec_and_test(&mark->refcnt))
+       if (atomic_dec_and_test(&mark->refcnt)) {
+               if (mark->group)
+                       fsnotify_put_group(mark->group);
                mark->free_mark(mark);
+       }
 }
 
 /*
@@ -118,25 +121,23 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
  * The caller had better be holding a reference to this mark so we don't actually
  * do the final put under the mark->lock
  */
-void fsnotify_destroy_mark(struct fsnotify_mark *mark)
+void fsnotify_destroy_mark(struct fsnotify_mark *mark,
+                          struct fsnotify_group *group)
 {
-       struct fsnotify_group *group;
        struct inode *inode = NULL;
 
+       mutex_lock(&group->mark_mutex);
        spin_lock(&mark->lock);
 
-       group = mark->group;
-
        /* something else already called this function on this mark */
        if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
                spin_unlock(&mark->lock);
+               mutex_unlock(&group->mark_mutex);
                return;
        }
 
        mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
 
-       spin_lock(&group->mark_lock);
-
        if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
                inode = mark->i.inode;
                fsnotify_destroy_inode_mark(mark);
@@ -147,8 +148,8 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark)
 
        list_del_init(&mark->g_list);
 
-       spin_unlock(&group->mark_lock);
        spin_unlock(&mark->lock);
+       mutex_unlock(&group->mark_mutex);
 
        spin_lock(&destroy_lock);
        list_add(&mark->destroy_list, &destroy_list);
@@ -177,19 +178,12 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark)
 
        if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED))
                iput(inode);
-
        /*
         * We don't necessarily have a ref on mark from caller so the above iput
         * may have already destroyed it.  Don't touch from now on.
         */
 
-       /*
-        * it's possible that this group tried to destroy itself, but this
-        * this mark was simultaneously being freed by inode.  If that's the
-        * case, we finish freeing the group here.
-        */
-       if (unlikely(atomic_dec_and_test(&group->num_marks)))
-               fsnotify_final_destroy_group(group);
+       atomic_dec(&group->num_marks);
 }
 
 void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
@@ -225,15 +219,16 @@ int fsnotify_add_mark(struct fsnotify_mark *mark,
 
        /*
         * LOCKING ORDER!!!!
+        * group->mark_mutex
         * mark->lock
-        * group->mark_lock
         * inode->i_lock
         */
-       spin_lock(&mark->lock);
-       spin_lock(&group->mark_lock);
+       mutex_lock(&group->mark_mutex);
 
+       spin_lock(&mark->lock);
        mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE;
 
+       fsnotify_get_group(group);
        mark->group = group;
        list_add(&mark->g_list, &group->marks_list);
        atomic_inc(&group->num_marks);
@@ -251,13 +246,12 @@ int fsnotify_add_mark(struct fsnotify_mark *mark,
                BUG();
        }
 
-       spin_unlock(&group->mark_lock);
-
        /* this will pin the object if appropriate */
        fsnotify_set_mark_mask_locked(mark, mark->mask);
-
        spin_unlock(&mark->lock);
 
+       mutex_unlock(&group->mark_mutex);
+
        if (inode)
                __fsnotify_update_child_dentry_flags(inode);
 
@@ -265,11 +259,12 @@ int fsnotify_add_mark(struct fsnotify_mark *mark,
 err:
        mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
        list_del_init(&mark->g_list);
+       fsnotify_put_group(group);
        mark->group = NULL;
        atomic_dec(&group->num_marks);
 
-       spin_unlock(&group->mark_lock);
        spin_unlock(&mark->lock);
+       mutex_unlock(&group->mark_mutex);
 
        spin_lock(&destroy_lock);
        list_add(&mark->destroy_list, &destroy_list);
@@ -288,7 +283,7 @@ void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
        struct fsnotify_mark *lmark, *mark;
        LIST_HEAD(free_list);
 
-       spin_lock(&group->mark_lock);
+       mutex_lock(&group->mark_mutex);
        list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
                if (mark->flags & flags) {
                        list_add(&mark->free_g_list, &free_list);
@@ -296,10 +291,10 @@ void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
                        fsnotify_get_mark(mark);
                }
        }
-       spin_unlock(&group->mark_lock);
+       mutex_unlock(&group->mark_mutex);
 
        list_for_each_entry_safe(mark, lmark, &free_list, free_g_list) {
-               fsnotify_destroy_mark(mark);
+               fsnotify_destroy_mark(mark, group);
                fsnotify_put_mark(mark);
        }
 }
@@ -317,6 +312,8 @@ void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *ol
        assert_spin_locked(&old->lock);
        new->i.inode = old->i.inode;
        new->m.mnt = old->m.mnt;
+       if (old->group)
+               fsnotify_get_group(old->group);
        new->group = old->group;
        new->mask = old->mask;
        new->free_mark = old->free_mark;