Merge tag 'for_linux-3.9' of git://git.kernel.org/pub/scm/linux/kernel/git/jwessel...
[firefly-linux-kernel-4.4.55.git] / fs / notify / mark.c
index ab25b810b14670f42d1771a65a0a52a0e614618e..fc6b49bf73600c2aad93e802865512b692a7cc24 100644 (file)
@@ -121,29 +121,19 @@ 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_locked(struct fsnotify_mark *mark,
+                                 struct fsnotify_group *group)
 {
-       struct fsnotify_group *group;
        struct inode *inode = NULL;
 
-       spin_lock(&mark->lock);
-       /* dont get the group from a mark that is not alive yet */
-       if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
-               spin_unlock(&mark->lock);
-               return;
-       }
-       fsnotify_get_group(mark->group);
-       group = mark->group;
-       spin_unlock(&mark->lock);
+       BUG_ON(!mutex_is_locked(&group->mark_mutex));
 
-       mutex_lock(&group->mark_mutex);
        spin_lock(&mark->lock);
 
        /* 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);
-               goto put_group;
+               return;
        }
 
        mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
@@ -159,12 +149,21 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark)
        list_del_init(&mark->g_list);
 
        spin_unlock(&mark->lock);
+
+       if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED))
+               iput(inode);
+       /* release lock temporarily */
        mutex_unlock(&group->mark_mutex);
 
        spin_lock(&destroy_lock);
        list_add(&mark->destroy_list, &destroy_list);
        spin_unlock(&destroy_lock);
        wake_up(&destroy_waitq);
+       /*
+        * We don't necessarily have a ref on mark from caller so the above destroy
+        * may have actually freed it, unless this group provides a 'freeing_mark'
+        * function which must be holding a reference.
+        */
 
        /*
         * Some groups like to know that marks are being freed.  This is a
@@ -186,17 +185,17 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark)
         * is just a lazy update (and could be a perf win...)
         */
 
-       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.
-        */
-
        atomic_dec(&group->num_marks);
 
-put_group:
-       fsnotify_put_group(group);
+       mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
+}
+
+void fsnotify_destroy_mark(struct fsnotify_mark *mark,
+                          struct fsnotify_group *group)
+{
+       mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
+       fsnotify_destroy_mark_locked(mark, group);
+       mutex_unlock(&group->mark_mutex);
 }
 
 void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
@@ -221,14 +220,15 @@ void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mas
  * These marks may be used for the fsnotify backend to determine which
  * event types should be delivered to which group.
  */
-int fsnotify_add_mark(struct fsnotify_mark *mark,
-                     struct fsnotify_group *group, struct inode *inode,
-                     struct vfsmount *mnt, int allow_dups)
+int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
+                            struct fsnotify_group *group, struct inode *inode,
+                            struct vfsmount *mnt, int allow_dups)
 {
        int ret = 0;
 
        BUG_ON(inode && mnt);
        BUG_ON(!inode && !mnt);
+       BUG_ON(!mutex_is_locked(&group->mark_mutex));
 
        /*
         * LOCKING ORDER!!!!
@@ -236,8 +236,6 @@ int fsnotify_add_mark(struct fsnotify_mark *mark,
         * mark->lock
         * inode->i_lock
         */
-       mutex_lock(&group->mark_mutex);
-
        spin_lock(&mark->lock);
        mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE;
 
@@ -263,8 +261,6 @@ int fsnotify_add_mark(struct fsnotify_mark *mark,
        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);
 
@@ -277,7 +273,6 @@ err:
        atomic_dec(&group->num_marks);
 
        spin_unlock(&mark->lock);
-       mutex_unlock(&group->mark_mutex);
 
        spin_lock(&destroy_lock);
        list_add(&mark->destroy_list, &destroy_list);
@@ -287,6 +282,16 @@ err:
        return ret;
 }
 
+int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
+                     struct inode *inode, struct vfsmount *mnt, int allow_dups)
+{
+       int ret;
+       mutex_lock(&group->mark_mutex);
+       ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
+       mutex_unlock(&group->mark_mutex);
+       return ret;
+}
+
 /*
  * clear any marks in a group in which mark->flags & flags is true
  */
@@ -294,22 +299,16 @@ void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
                                         unsigned int flags)
 {
        struct fsnotify_mark *lmark, *mark;
-       LIST_HEAD(free_list);
 
-       mutex_lock(&group->mark_mutex);
+       mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
        list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
                if (mark->flags & flags) {
-                       list_add(&mark->free_g_list, &free_list);
-                       list_del_init(&mark->g_list);
                        fsnotify_get_mark(mark);
+                       fsnotify_destroy_mark_locked(mark, group);
+                       fsnotify_put_mark(mark);
                }
        }
        mutex_unlock(&group->mark_mutex);
-
-       list_for_each_entry_safe(mark, lmark, &free_list, free_g_list) {
-               fsnotify_destroy_mark(mark);
-               fsnotify_put_mark(mark);
-       }
 }
 
 /*