f2fs: fix uninitialized skipped_gc_rwsem
[platform/kernel/linux-starfive.git] / fs / f2fs / gc.c
index 4546e01..aa928d1 100644 (file)
@@ -96,16 +96,6 @@ static int gc_thread_func(void *data)
                 * invalidated soon after by user update or deletion.
                 * So, I'd like to wait some time to collect dirty segments.
                 */
-               if (sbi->gc_mode == GC_URGENT_HIGH) {
-                       spin_lock(&sbi->gc_urgent_high_lock);
-                       if (sbi->gc_urgent_high_remaining) {
-                               sbi->gc_urgent_high_remaining--;
-                               if (!sbi->gc_urgent_high_remaining)
-                                       sbi->gc_mode = GC_NORMAL;
-                       }
-                       spin_unlock(&sbi->gc_urgent_high_lock);
-               }
-
                if (sbi->gc_mode == GC_URGENT_HIGH ||
                                sbi->gc_mode == GC_URGENT_MID) {
                        wait_ms = gc_th->urgent_sleep_time;
@@ -162,6 +152,15 @@ do_gc:
                /* balancing f2fs's metadata periodically */
                f2fs_balance_fs_bg(sbi, true);
 next:
+               if (sbi->gc_mode == GC_URGENT_HIGH) {
+                       spin_lock(&sbi->gc_urgent_high_lock);
+                       if (sbi->gc_urgent_high_remaining) {
+                               sbi->gc_urgent_high_remaining--;
+                               if (!sbi->gc_urgent_high_remaining)
+                                       sbi->gc_mode = GC_NORMAL;
+                       }
+                       spin_unlock(&sbi->gc_urgent_high_lock);
+               }
                sb_end_write(sbi->sb);
 
        } while (!kthread_should_stop());
@@ -1079,7 +1078,7 @@ static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
 {
        struct page *node_page;
        nid_t nid;
-       unsigned int ofs_in_node, max_addrs;
+       unsigned int ofs_in_node, max_addrs, base;
        block_t source_blkaddr;
 
        nid = le32_to_cpu(sum->nid);
@@ -1105,11 +1104,18 @@ static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
                return false;
        }
 
-       max_addrs = IS_INODE(node_page) ? DEF_ADDRS_PER_INODE :
-                                               DEF_ADDRS_PER_BLOCK;
-       if (ofs_in_node >= max_addrs) {
-               f2fs_err(sbi, "Inconsistent ofs_in_node:%u in summary, ino:%u, nid:%u, max:%u",
-                       ofs_in_node, dni->ino, dni->nid, max_addrs);
+       if (IS_INODE(node_page)) {
+               base = offset_in_addr(F2FS_INODE(node_page));
+               max_addrs = DEF_ADDRS_PER_INODE;
+       } else {
+               base = 0;
+               max_addrs = DEF_ADDRS_PER_BLOCK;
+       }
+
+       if (base + ofs_in_node >= max_addrs) {
+               f2fs_err(sbi, "Inconsistent blkaddr offset: base:%u, ofs_in_node:%u, max:%u, ino:%u, nid:%u",
+                       base, ofs_in_node, max_addrs, dni->ino, dni->nid);
+               f2fs_put_page(node_page, 1);
                return false;
        }
 
@@ -1744,8 +1750,9 @@ freed:
                                get_valid_blocks(sbi, segno, false) == 0)
                        seg_freed++;
 
-               if (__is_large_section(sbi) && segno + 1 < end_segno)
-                       sbi->next_victim_seg[gc_type] = segno + 1;
+               if (__is_large_section(sbi))
+                       sbi->next_victim_seg[gc_type] =
+                               (segno + 1 < end_segno) ? segno + 1 : NULL_SEGNO;
 skip:
                f2fs_put_page(sum_page, 0);
        }
@@ -1785,8 +1792,8 @@ int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control)
                                prefree_segments(sbi));
 
        cpc.reason = __get_cp_reason(sbi);
-       sbi->skipped_gc_rwsem = 0;
 gc_more:
+       sbi->skipped_gc_rwsem = 0;
        if (unlikely(!(sbi->sb->s_flags & SB_ACTIVE))) {
                ret = -EINVAL;
                goto stop;
@@ -2133,8 +2140,6 @@ out_unlock:
        if (err)
                return err;
 
-       set_sbi_flag(sbi, SBI_IS_RESIZEFS);
-
        freeze_super(sbi->sb);
        f2fs_down_write(&sbi->gc_lock);
        f2fs_down_write(&sbi->cp_global_sem);
@@ -2150,6 +2155,7 @@ out_unlock:
        if (err)
                goto out_err;
 
+       set_sbi_flag(sbi, SBI_IS_RESIZEFS);
        err = free_segment_range(sbi, secs, false);
        if (err)
                goto recover_out;
@@ -2173,6 +2179,7 @@ out_unlock:
                f2fs_commit_super(sbi, false);
        }
 recover_out:
+       clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
        if (err) {
                set_sbi_flag(sbi, SBI_NEED_FSCK);
                f2fs_err(sbi, "resize_fs failed, should run fsck to repair!");
@@ -2185,6 +2192,5 @@ out_err:
        f2fs_up_write(&sbi->cp_global_sem);
        f2fs_up_write(&sbi->gc_lock);
        thaw_super(sbi->sb);
-       clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
        return err;
 }