Skip to contentSkip to conversation

f2fs-tools: detect and fix NAT entry inconsistencies with dent_flag set

PATCH In review liujinbao1 2 participants 3 messages Dec 31, 2025 – Jan 12

lore
Share & tools
lore

liujinbao1

author
#0 lore raw

PATCH 1/1 f2fs-tools: detect and fix NAT entry inconsistencies with dent_flag set

From: liujinbao1 <[email protected]>

During SPO tests, when mounting F2FS, an -EINVAL error was returned. The issue originates from the f2fs_recover_inode_page function's check, where old_ni.blk_addr != NULL_ADDR under the conditions of IS_INODE(folio) && is_dent_dnode(folio).
Add detection and repair for this scenario in fsck.

---
 fsck/mount.c | 24 ++++++++++++++++++++++++
 fsck/node.h  | 14 ++++++++++++++
 2 files changed, 38 insertions(+)
fsck/mount.c +24 -0
index f03fa2d..07b8e6e 100644
@@ -3881,6 +3881,7 @@ int f2fs_find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head)
 
 	while (1) {
 		struct fsync_inode_entry *entry;
+		struct f2fs_nat_entry nat_entry;
 
 		if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
 			break;
@@ -3897,6 +3898,29 @@ int f2fs_find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head)
 
 		entry = get_fsync_inode(head, ino_of_node(node_blk));
 		if (!entry) {
+			if (IS_INODE(node_blk) && is_dent_dnode(node_blk)) {
+				get_nat_entry(sbi, ino_of_node(node_blk), &nat_entry);
+				if (nat_entry.block_addr != NULL_ADDR) {
+					if (!c.fix_on)
+						ASSERT_MSG("ino: %u is_dent:%d nat entry blkaddr is %#X\n",
+							ino_of_node(node_blk), is_dent_dnode(node_blk),
+							nat_entry.block_addr);
+					else if (f2fs_dev_is_writable()) {
+						int ret = 0;
+
+						ASSERT_MSG("Set node_blk %#x fsync flag [%u] -> [0]"
+							" next_blkaddr [%#x] -> [NULL_ADDR]\n",
+							blkaddr, is_fsync_dnode(node_blk),
+							F2FS_NODE_FOOTER(node_blk)->next_blkaddr);
+						F2FS_NODE_FOOTER(node_blk)->next_blkaddr = NULL_ADDR;
+						set_fsync_mark(node_blk, 0);
+						ret = dev_write_block(node_blk, blkaddr,
+							f2fs_io_type_to_rw_hint(CURSEG_WARM_NODE));
+						ASSERT(ret >= 0);
+						goto next;
+					}
+				}
+			}
 			entry = add_fsync_inode(head, ino_of_node(node_blk));
 			if (!entry) {
 				err = -1;
fsck/node.h +14 -0
index 19f1e57..7c16cc8 100644
@@ -173,6 +173,20 @@ static inline void set_cold_node(struct f2fs_node *rn, bool is_dir)
 	F2FS_NODE_FOOTER(rn)->flag = cpu_to_le32(flag);
 }
 
+static inline void set_mark(struct f2fs_node *rn, int mark, int type)
+{
+	unsigned int flag = le32_to_cpu(F2FS_NODE_FOOTER(rn)->flag);
+
+	if (mark)
+		flag |= (1 << type);
+	else
+		flag &= ~(1 << type);
+	F2FS_NODE_FOOTER(rn)->flag = cpu_to_le32(flag);
+}
+
+#define set_dentry_mark(page, mark)	set_mark(page, mark, DENT_BIT_SHIFT)
+#define set_fsync_mark(page, mark)	set_mark(page, mark, FSYNC_BIT_SHIFT)
+
 #define is_fsync_dnode(node_blk)	is_node(node_blk, FSYNC_BIT_SHIFT)
 #define is_dent_dnode(node_blk)		is_node(node_blk, DENT_BIT_SHIFT)
 
Signature · 8 lines
2.25.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw
Quoted 29 lines · On 12/31/2025 6:52 PM, liujinbao1 wrote:

From: liujinbao1 <[email protected]>

During SPO tests, when mounting F2FS, an -EINVAL error was returned. The issue originates from the f2fs_recover_inode_page function's check, where old_ni.blk_addr != NULL_ADDR under the conditions of IS_INODE(folio) && is_dent_dnode(folio).
Add detection and repair for this scenario in fsck.

---
  fsck/mount.c | 24 ++++++++++++++++++++++++
  fsck/node.h  | 14 ++++++++++++++
  2 files changed, 38 insertions(+)
fsck/mount.c +1 -0
index f03fa2d..07b8e6e 100644
@@ -3881,6 +3881,7 @@ int f2fs_find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head)
 
  	while (1) {
  		struct fsync_inode_entry *entry;
+		struct f2fs_nat_entry nat_entry;
 
  		if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
  			break;

fsck/mount.c int f2fs_find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head) · line 3903

Quoted 6 lines
fsck/mount.c +3 -0
@@ -3897,6 +3898,29 @@ int f2fs_find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head)
 
  		entry = get_fsync_inode(head, ino_of_node(node_blk));
  		if (!entry) {
+			if (IS_INODE(node_blk) && is_dent_dnode(node_blk)) {
+				get_nat_entry(sbi, ino_of_node(node_blk), &nat_entry);
+				if (nat_entry.block_addr != NULL_ADDR) {

is_valid_data_blkaddr(nat_entry.block_addr)?

fsck/mount.c int f2fs_find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head) · line 3907

Quoted 4 lines
fsck/mount.c +4 -0
+					if (!c.fix_on)
+						ASSERT_MSG("ino: %u is_dent:%d nat entry blkaddr is %#X\n",
+							ino_of_node(node_blk), is_dent_dnode(node_blk),
+							nat_entry.block_addr);

Should call ASSERT_MSG() first to print log and set c.bug_on, then check c.fix_on?

fsck/mount.c int f2fs_find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head) · line 3914

Quoted 7 lines
fsck/mount.c +7 -0
+					else if (f2fs_dev_is_writable()) {
+						int ret = 0;
+
+						ASSERT_MSG("Set node_blk %#x fsync flag [%u] -> [0]"
+							" next_blkaddr [%#x] -> [NULL_ADDR]\n",
+							blkaddr, is_fsync_dnode(node_blk),
+							F2FS_NODE_FOOTER(node_blk)->next_blkaddr);

FIX_MSG("Set node_blk...")?

fsck/mount.c int f2fs_find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head) · line 3916

Quoted 2 lines
fsck/mount.c +2 -0
+						F2FS_NODE_FOOTER(node_blk)->next_blkaddr = NULL_ADDR;
+						set_fsync_mark(node_blk, 0);

I suspect that DENT_BIT_SHIFT is set incorrectly? So can we just clear DENT_BIT_SHIFT, and continue to recovery this inode block?

Thanks,

Quoted 25 lines
fsck/mount.c +7 -0
+						ret = dev_write_block(node_blk, blkaddr,
+							f2fs_io_type_to_rw_hint(CURSEG_WARM_NODE));
+						ASSERT(ret >= 0);
+						goto next;
+					}
+				}
+			}
  			entry = add_fsync_inode(head, ino_of_node(node_blk));
  			if (!entry) {
  				err = -1;
fsck/node.h +7 -0
index 19f1e57..7c16cc8 100644
@@ -173,6 +173,20 @@ static inline void set_cold_node(struct f2fs_node *rn, bool is_dir)
  	F2FS_NODE_FOOTER(rn)->flag = cpu_to_le32(flag);
  }
 
+static inline void set_mark(struct f2fs_node *rn, int mark, int type)
+{
+	unsigned int flag = le32_to_cpu(F2FS_NODE_FOOTER(rn)->flag);
+
+	if (mark)
+		flag |= (1 << type);
+	else

fsck/node.h static inline void set_cold_node(struct f2fs_node *rn, bool is_dir) · line 192

Quoted 10 lines
fsck/node.h +7 -0
+		flag &= ~(1 << type);
+	F2FS_NODE_FOOTER(rn)->flag = cpu_to_le32(flag);
+}
+
+#define set_dentry_mark(page, mark)	set_mark(page, mark, DENT_BIT_SHIFT)
+#define set_fsync_mark(page, mark)	set_mark(page, mark, FSYNC_BIT_SHIFT)
+
  #define is_fsync_dnode(node_blk)	is_node(node_blk, FSYNC_BIT_SHIFT)
  #define is_dent_dnode(node_blk)		is_node(node_blk, DENT_BIT_SHIFT)
 

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author
#2 lore raw
Quoted 29 lines · Chao Yu
Quoted 28 lines · On 12/31/2025 6:52 PM, liujinbao1 wrote:

From: liujinbao1 <[email protected]>

During SPO tests, when mounting F2FS, an -EINVAL error was returned. The issue originates from the f2fs_recover_inode_page function's check, where old_ni.blk_addr != NULL_ADDR under the conditions of IS_INODE(folio) && is_dent_dnode(folio).
Add detection and repair for this scenario in fsck.

---
  fsck/mount.c | 24 ++++++++++++++++++++++++
  fsck/node.h  | 14 ++++++++++++++

  2 files changed, 38 insertions(+)
fsck/mount.c +1 -0
index f03fa2d..07b8e6e 100644
@@ -3881,6 +3881,7 @@ int f2fs_find_fsync_inode(struct f2fs_sb_info 
 *sbi, struct list_head *head)
         while (1) {
           struct fsync_inode_entry *entry;
+        struct f2fs_nat_entry nat_entry;
             if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
               break;
@@ -3897,6 +3898,29 @@ int f2fs_find_fsync_inode(struct f2fs_sb_info 
Quoted 8 lines

fsck/mount.c int f2fs_find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head) · line 3903

Quoted 6 lines
fsck/mount.c +3 -0
 *sbi, struct list_head *head)
             entry = get_fsync_inode(head, ino_of_node(node_blk));
           if (!entry) {
+            if (IS_INODE(node_blk) && is_dent_dnode(node_blk)) {
+                get_nat_entry(sbi, ino_of_node(node_blk), &nat_entry);
+                if (nat_entry.block_addr != NULL_ADDR) {

is_valid_data_blkaddr(nat_entry.block_addr)?

   Yes

Quoted 11 lines

fsck/mount.c int f2fs_find_fsync_inode(struct f2fs_sb_info · line 3909

Quoted 7 lines
fsck/mount.c +4 -0
+                    if (!c.fix_on)
+                        ASSERT_MSG("ino: %u is_dent:%d nat entry 
 blkaddr is %#X\n",
+                            ino_of_node(node_blk), 
 is_dent_dnode(node_blk),
+                            nat_entry.block_addr);

Should call ASSERT_MSG() first to print log and set c.bug_on, then check c.fix_on?

    Yes

Quoted 11 lines

fsck/mount.c int f2fs_find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head) · line 3914

Quoted 8 lines
fsck/mount.c +7 -1
+                    else if (f2fs_dev_is_writable()) {
+                        int ret = 0;
+
+                        ASSERT_MSG("Set node_blk %#x fsync flag [%u] 
-> [0]"
+                            " next_blkaddr [%#x] -> [NULL_ADDR]\n",
+                            blkaddr, is_fsync_dnode(node_blk),
+ F2FS_NODE_FOOTER(node_blk)->next_blkaddr);

FIX_MSG("Set node_blk...")?

    Yes

Quoted 7 lines

fsck/mount.c int f2fs_find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head) · line 3916

Quoted 2 lines
fsck/mount.c +2 -0
+ F2FS_NODE_FOOTER(node_blk)->next_blkaddr = NULL_ADDR;
+                        set_fsync_mark(node_blk, 0);

I suspect that DENT_BIT_SHIFT is set incorrectly? So can we just clear DENT_BIT_SHIFT, and continue to recovery this inode block?

Thanks,

    Ok, I'll  try it.

    Thanks

Quoted 29 lines
Quoted 29 lines
fsck/mount.c +7 -0
+                        ret = dev_write_block(node_blk, blkaddr,
+ f2fs_io_type_to_rw_hint(CURSEG_WARM_NODE));
+                        ASSERT(ret >= 0);
+                        goto next;
+                    }
+                }
+            }

              entry = add_fsync_inode(head, ino_of_node(node_blk)); if (!entry) {
                  err = -1;

fsck/node.h +9 -0
index 19f1e57..7c16cc8 100644
@@ -173,6 +173,20 @@ static inline void set_cold_node(struct 
 f2fs_node *rn, bool is_dir)
       F2FS_NODE_FOOTER(rn)->flag = cpu_to_le32(flag);
   }
   +static inline void set_mark(struct f2fs_node *rn, int mark, int type)
+{
+    unsigned int flag = le32_to_cpu(F2FS_NODE_FOOTER(rn)->flag);
+
+    if (mark)
+        flag |= (1 << type);
+    else
+        flag &= ~(1 << type);
+    F2FS_NODE_FOOTER(rn)->flag = cpu_to_le32(flag);
+}
Quoted 9 lines
Quoted 8 lines
fsck/node.h +4 -0
+
+#define set_dentry_mark(page, mark)    set_mark(page, mark, 
 DENT_BIT_SHIFT)
+#define set_fsync_mark(page, mark)    set_mark(page, mark, 
 FSYNC_BIT_SHIFT)
+

  #define is_fsync_dnode(node_blk)    is_node(node_blk, FSYNC_BIT_SHIFT) #define is_dent_dnode(node_blk)        is_node(node_blk, DENT_BIT_SHIFT)

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel