ocfs2: fix race between searching chunks and release journal_head from buffer_head
[platform/kernel/linux-starfive.git] / fs / ocfs2 / suballoc.c
index 8521942..481017e 100644 (file)
@@ -1251,7 +1251,7 @@ static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
 {
        struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
        struct journal_head *jh;
-       int ret;
+       int ret = 1;
 
        if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
                return 0;
@@ -1259,14 +1259,18 @@ static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
        if (!buffer_jbd(bg_bh))
                return 1;
 
-       jh = bh2jh(bg_bh);
-       spin_lock(&jh->b_state_lock);
-       bg = (struct ocfs2_group_desc *) jh->b_committed_data;
-       if (bg)
-               ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
-       else
-               ret = 1;
-       spin_unlock(&jh->b_state_lock);
+       jbd_lock_bh_journal_head(bg_bh);
+       if (buffer_jbd(bg_bh)) {
+               jh = bh2jh(bg_bh);
+               spin_lock(&jh->b_state_lock);
+               bg = (struct ocfs2_group_desc *) jh->b_committed_data;
+               if (bg)
+                       ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
+               else
+                       ret = 1;
+               spin_unlock(&jh->b_state_lock);
+       }
+       jbd_unlock_bh_journal_head(bg_bh);
 
        return ret;
 }