]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
f2fs: fix fsck inconsistency caused by FGGC of node block
authorYongpeng Yang <yangyongpeng@xiaomi.com>
Wed, 18 Mar 2026 08:45:34 +0000 (16:45 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Thu, 2 Apr 2026 16:24:19 +0000 (16:24 +0000)
During FGGC node block migration, fsck may incorrectly treat the
migrated node block as fsync-written data.

The reproduction scenario:
root@vm:/mnt/f2fs# seq 1 2048 | xargs -n 1 ./test_sync // write inline inode and sync
root@vm:/mnt/f2fs# rm -f 1
root@vm:/mnt/f2fs# sync
root@vm:/mnt/f2fs# f2fs_io gc_range // move data block in sync mode and not write CP
  SPO, "fsck --dry-run" find inode has already checkpointed but still
  with DENT_BIT_SHIFT set

The root cause is that GC does not clear the dentry mark and fsync mark
during node block migration, leading fsck to misinterpret them as
user-issued fsync writes.

In BGGC mode, node block migration is handled by f2fs_sync_node_pages(),
which guarantees the dentry and fsync marks are cleared before writing.

This patch move the set/clear of the fsync|dentry marks into
__write_node_folio to make the logic clearer, and ensures the
fsync|dentry mark is cleared in FGGC.

Cc: stable@kernel.org
Fixes: da011cc0da8c ("f2fs: move node pages only in victim section during GC")
Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com>
Reviewed-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/node.c

index 630fd3b43a089e2cea1385210e7d2399cc828ee5..c7499cb527453de65d73548d82a2f2d81db6c97e 100644 (file)
@@ -1727,9 +1727,10 @@ continue_unlock:
        return last_folio;
 }
 
-static bool __write_node_folio(struct folio *folio, bool atomic, bool *submitted,
-                               struct writeback_control *wbc, bool do_balance,
-                               enum iostat_type io_type, unsigned int *seq_id)
+static bool __write_node_folio(struct folio *folio, bool atomic, bool do_fsync,
+                               bool *submitted, struct writeback_control *wbc,
+                               bool do_balance, enum iostat_type io_type,
+                               unsigned int *seq_id)
 {
        struct f2fs_sb_info *sbi = F2FS_F_SB(folio);
        nid_t nid;
@@ -1802,6 +1803,8 @@ static bool __write_node_folio(struct folio *folio, bool atomic, bool *submitted
        if (atomic && !test_opt(sbi, NOBARRIER))
                fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
 
+       set_dentry_mark(folio, false);
+       set_fsync_mark(folio, do_fsync);
        if (IS_INODE(folio) && (atomic || is_fsync_dnode(folio)))
                set_dentry_mark(folio,
                                f2fs_need_dentry_mark(sbi, ino_of_node(folio)));
@@ -1868,7 +1871,7 @@ static int f2fs_write_single_node_folio(struct folio *node_folio, int sync_mode,
                goto out_folio;
        }
 
-       if (!__write_node_folio(node_folio, false, NULL,
+       if (!__write_node_folio(node_folio, false, false, NULL,
                                &wbc, false, FS_GC_NODE_IO, NULL))
                err = -EAGAIN;
        goto release_folio;
@@ -1915,6 +1918,7 @@ retry:
                for (i = 0; i < nr_folios; i++) {
                        struct folio *folio = fbatch.folios[i];
                        bool submitted = false;
+                       bool do_fsync = false;
 
                        if (unlikely(f2fs_cp_error(sbi))) {
                                f2fs_folio_put(last_folio, false);
@@ -1945,11 +1949,8 @@ continue_unlock:
 
                        f2fs_folio_wait_writeback(folio, NODE, true, true);
 
-                       set_fsync_mark(folio, 0);
-                       set_dentry_mark(folio, 0);
-
                        if (!atomic || folio == last_folio) {
-                               set_fsync_mark(folio, 1);
+                               do_fsync = true;
                                percpu_counter_inc(&sbi->rf_node_block_count);
                                if (IS_INODE(folio)) {
                                        if (is_inode_flag_set(inode,
@@ -1966,8 +1967,9 @@ continue_unlock:
 
                        if (!__write_node_folio(folio, atomic &&
                                                folio == last_folio,
-                                               &submitted, wbc, true,
-                                               FS_NODE_IO, seq_id)) {
+                                               do_fsync, &submitted,
+                                               wbc, true, FS_NODE_IO,
+                                               seq_id)) {
                                f2fs_folio_put(last_folio, false);
                                folio_batch_release(&fbatch);
                                ret = -EIO;
@@ -2167,10 +2169,7 @@ write_node:
                        if (!folio_clear_dirty_for_io(folio))
                                goto continue_unlock;
 
-                       set_fsync_mark(folio, 0);
-                       set_dentry_mark(folio, 0);
-
-                       if (!__write_node_folio(folio, false, &submitted,
+                       if (!__write_node_folio(folio, false, false, &submitted,
                                        wbc, do_balance, io_type, NULL)) {
                                folio_batch_release(&fbatch);
                                ret = -EIO;