ocfs2: avoid system inode ref confusion by adding mutex lock
authorjiangyiwen <jiangyiwen@huawei.com>
Thu, 3 Apr 2014 21:47:13 +0000 (14:47 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 3 Apr 2014 23:20:57 +0000 (16:20 -0700)
The following case may lead to the same system inode ref in confusion.

A thread                            B thread
ocfs2_get_system_file_inode
->get_local_system_inode
->_ocfs2_get_system_file_inode
                                    because of *arr == NULL,
                                    ocfs2_get_system_file_inode
                                    ->get_local_system_inode
                                    ->_ocfs2_get_system_file_inode
gets first ref thru
_ocfs2_get_system_file_inode,
gets second ref thru igrab and
set *arr = inode
                                    at the moment, B thread also gets
                                    two refs, so lead to one more
                                    inode ref.

So add mutex lock to avoid multi thread set two inode ref once at the
same time.

Signed-off-by: jiangyiwen <jiangyiwen@huawei.com>
Reviewed-by: Joseph Qi <joseph.qi@huawei.com>
Cc: Joel Becker <jlbec@evilplan.org>
Cc: Mark Fasheh <mfasheh@suse.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
fs/ocfs2/ocfs2.h
fs/ocfs2/super.c
fs/ocfs2/sysfile.c

index a780e20d4fbaca129d43afa55d4658e8d25a9b47..8d64a97a9d5e509b0109e94b1b5e01b41669a8e8 100644 (file)
@@ -446,6 +446,8 @@ struct ocfs2_super
        /* rb tree root for refcount lock. */
        struct rb_root  osb_rf_lock_tree;
        struct ocfs2_refcount_tree *osb_ref_tree_lru;
+
+       struct mutex system_file_mutex;
 };
 
 #define OCFS2_SB(sb)       ((struct ocfs2_super *)(sb)->s_fs_info)
index 888a1457af969dc3fb4f223a845847694a07b2e7..1aecd626e645a7cf6be82f8dbf062dd1232be072 100644 (file)
@@ -2100,6 +2100,8 @@ static int ocfs2_initialize_super(struct super_block *sb,
        spin_lock_init(&osb->osb_xattr_lock);
        ocfs2_init_steal_slots(osb);
 
+       mutex_init(&osb->system_file_mutex);
+
        atomic_set(&osb->alloc_stats.moves, 0);
        atomic_set(&osb->alloc_stats.local_data, 0);
        atomic_set(&osb->alloc_stats.bitmap_data, 0);
index f053688d22a349ebf6803ecd676f64566fe90de0..af155c1831234f7cfef3d4a5d4e0d7b6359f79ed 100644 (file)
@@ -113,9 +113,11 @@ struct inode *ocfs2_get_system_file_inode(struct ocfs2_super *osb,
        } else
                arr = get_local_system_inode(osb, type, slot);
 
+       mutex_lock(&osb->system_file_mutex);
        if (arr && ((inode = *arr) != NULL)) {
                /* get a ref in addition to the array ref */
                inode = igrab(inode);
+               mutex_unlock(&osb->system_file_mutex);
                BUG_ON(!inode);
 
                return inode;
@@ -129,6 +131,7 @@ struct inode *ocfs2_get_system_file_inode(struct ocfs2_super *osb,
                *arr = igrab(inode);
                BUG_ON(!*arr);
        }
+       mutex_unlock(&osb->system_file_mutex);
        return inode;
 }