Skip to contentSkip to conversation

f2fs: add check for deleted inode

PATCH Superseded Chao Yu 2 participants 10 messages Feb 12, 2025 – Feb 28, 2025 F2FS file system

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Chao Yu

author maintainer
#0 lore raw

PATCH v4 1/1 f2fs: add check for deleted inode

From: Leo Stone <[email protected]>

The syzbot reproducer mounts a f2fs image, then tries to unlink an existing file. However, the unlinked file already has a link count of 0 when it is read for the first time in do_read_inode().

Add a check to sanity_check_inode() for i_nlink == 0.

[Chao Yu: rebase the code and fix orphan inode recovery issue]
Reported-by [email protected]
Fixes 39a53e0ce0df ("f2fs: add superblock and major in-memory structure")
---
 fs/f2fs/checkpoint.c | 4 ++++
 fs/f2fs/f2fs.h       | 1 +
 fs/f2fs/inode.c      | 6 ++++++
 3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +4 -0
index bd890738b94d..ada2c548645c 100644
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
 		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
+
 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 		}
 		f2fs_put_page(page, 1);
 	}
+
 	/* clear Orphan Flag */
 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
 	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
 	SBI_IS_CLOSE,				/* specify unmounting */
 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
 	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
 	SBI_NEED_CP,				/* need to checkpoint */
 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 		}
 	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	return true;
 }
 
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
maintainer
#1 lore raw
Quoted 25 lines · Chao Yu

From: Leo Stone <[email protected]>

The syzbot reproducer mounts a f2fs image, then tries to unlink an existing file. However, the unlinked file already has a link count of 0 when it is read for the first time in do_read_inode().

Add a check to sanity_check_inode() for i_nlink == 0.

[Chao Yu: rebase the code and fix orphan inode recovery issue]
Reported-by [email protected]
Fixes 39a53e0ce0df ("f2fs: add superblock and major in-memory structure")
---
 fs/f2fs/checkpoint.c | 4 ++++
 fs/f2fs/f2fs.h       | 1 +
 fs/f2fs/inode.c      | 6 ++++++
 3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +0 -0
index bd890738b94d..ada2c548645c 100644

fs/f2fs/checkpoint.c int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) · line 754

Quoted 4 lines
fs/f2fs/checkpoint.c +1 -0
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
 		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);

What about using SBI_POR_DOING?

Quoted 47 lines
fs/f2fs/checkpoint.c +3 -0
+
 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 		}
 		f2fs_put_page(page, 1);
 	}
+
 	/* clear Orphan Flag */
 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
 	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
 	SBI_IS_CLOSE,				/* specify unmounting */
 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
 	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
 	SBI_NEED_CP,				/* need to checkpoint */
 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 		}
 	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	return true;
 }
 
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
author maintainer
#2 lore raw
Quoted 29 lines · Jaegeuk Kim
Quoted 28 lines · Chao Yu

From: Leo Stone <[email protected]>

The syzbot reproducer mounts a f2fs image, then tries to unlink an existing file. However, the unlinked file already has a link count of 0 when it is read for the first time in do_read_inode().

Add a check to sanity_check_inode() for i_nlink == 0.

[Chao Yu: rebase the code and fix orphan inode recovery issue]
Reported-by [email protected]
Fixes 39a53e0ce0df ("f2fs: add superblock and major in-memory structure")
---
 fs/f2fs/checkpoint.c | 4 ++++
 fs/f2fs/f2fs.h       | 1 +
 fs/f2fs/inode.c      | 6 ++++++
 3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +0 -0
index bd890738b94d..ada2c548645c 100644
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
 		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
Quoted 3 lines

fs/f2fs/checkpoint.c int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) · line 754

Quoted 1 line
fs/f2fs/checkpoint.c +1 -0
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);

What about using SBI_POR_DOING?

SBI_POR_DOING will cover most flow of f2fs_fill_super(), I think we can add a separated flag just covering f2fs_recover_orphan_inodes(), so that we can allow iget() of root_inode and all inodes during roll-forward recovery to do sanity check nlink w/ zero. What do you think?

Thanks,

Quoted 48 lines
Quoted 48 lines
fs/f2fs/checkpoint.c +3 -0
+
 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 		}
 		f2fs_put_page(page, 1);
 	}
+
 	/* clear Orphan Flag */
 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
 	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
 	SBI_IS_CLOSE,				/* specify unmounting */
 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
 	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
 	SBI_NEED_CP,				/* need to checkpoint */
 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 		}
 	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	return true;
 }
 
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
maintainer
#3 lore raw
Quoted 30 lines · Chao Yu
Quoted 29 lines · Jaegeuk Kim
Quoted 28 lines · Chao Yu

From: Leo Stone <[email protected]>

The syzbot reproducer mounts a f2fs image, then tries to unlink an existing file. However, the unlinked file already has a link count of 0 when it is read for the first time in do_read_inode().

Add a check to sanity_check_inode() for i_nlink == 0.

[Chao Yu: rebase the code and fix orphan inode recovery issue]
Reported-by [email protected]
Fixes 39a53e0ce0df ("f2fs: add superblock and major in-memory structure")
---
 fs/f2fs/checkpoint.c | 4 ++++
 fs/f2fs/f2fs.h       | 1 +
 fs/f2fs/inode.c      | 6 ++++++
 3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +0 -0
index bd890738b94d..ada2c548645c 100644
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
 		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
Quoted 8 lines
Quoted 3 lines

fs/f2fs/checkpoint.c int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) · line 754

Quoted 1 line
fs/f2fs/checkpoint.c +1 -0
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);

What about using SBI_POR_DOING?

SBI_POR_DOING will cover most flow of f2fs_fill_super(), I think we can add a separated flag just covering f2fs_recover_orphan_inodes(), so that we can allow iget() of root_inode and all inodes during roll-forward recovery to do sanity check nlink w/ zero. What do you think?

Can we do this sanity check after f2fs_iget in the f2fs_unlink() only?

Quoted 51 lines

Thanks,

Quoted 48 lines
Quoted 48 lines
fs/f2fs/checkpoint.c +3 -0
+
 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 		}
 		f2fs_put_page(page, 1);
 	}
+
 	/* clear Orphan Flag */
 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
 	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
 	SBI_IS_CLOSE,				/* specify unmounting */
 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
 	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
 	SBI_NEED_CP,				/* need to checkpoint */
 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 		}
 	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	return true;
 }
 
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
author maintainer
#4 lore raw
Quoted 35 lines · Jaegeuk Kim
Quoted 34 lines · Chao Yu
Quoted 32 lines · Jaegeuk Kim
Quoted 25 lines · Chao Yu

From: Leo Stone <[email protected]>

The syzbot reproducer mounts a f2fs image, then tries to unlink an existing file. However, the unlinked file already has a link count of 0 when it is read for the first time in do_read_inode().

Add a check to sanity_check_inode() for i_nlink == 0.

[Chao Yu: rebase the code and fix orphan inode recovery issue]
Reported-by [email protected]
Fixes 39a53e0ce0df ("f2fs: add superblock and major in-memory structure")
---
 fs/f2fs/checkpoint.c | 4 ++++
 fs/f2fs/f2fs.h       | 1 +
 fs/f2fs/inode.c      | 6 ++++++
 3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +0 -0
index bd890738b94d..ada2c548645c 100644

fs/f2fs/checkpoint.c int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) · line 754

Quoted 4 lines
fs/f2fs/checkpoint.c +1 -0
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
 		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);

What about using SBI_POR_DOING?

Quoted 6 lines
Quoted 4 lines

SBI_POR_DOING will cover most flow of f2fs_fill_super(), I think we can add a separated flag just covering f2fs_recover_orphan_inodes(), so that we can allow iget() of root_inode and all inodes during roll-forward recovery to do sanity check nlink w/ zero. What do you think?

Can we do this sanity check after f2fs_iget in the f2fs_unlink() only?

Sure, we need to cover f2fs_rename() as well, please check this:

https://lore.kernel.org/all/[email protected]

Thanks,

Quoted 52 lines
Quoted 52 lines

Thanks,

Quoted 48 lines
Quoted 48 lines
fs/f2fs/checkpoint.c +3 -0
+
 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 		}
 		f2fs_put_page(page, 1);
 	}
+
 	/* clear Orphan Flag */
 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
 	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
 	SBI_IS_CLOSE,				/* specify unmounting */
 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
 	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
 	SBI_NEED_CP,				/* need to checkpoint */
 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 		}
 	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	return true;
 }
 
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
author maintainer
#5 lore raw
Quoted 41 lines · Chao Yu
Quoted 40 lines · Jaegeuk Kim
Quoted 38 lines · Chao Yu
Quoted 29 lines · Jaegeuk Kim
Quoted 28 lines · Chao Yu

From: Leo Stone <[email protected]>

The syzbot reproducer mounts a f2fs image, then tries to unlink an existing file. However, the unlinked file already has a link count of 0 when it is read for the first time in do_read_inode().

Add a check to sanity_check_inode() for i_nlink == 0.

[Chao Yu: rebase the code and fix orphan inode recovery issue]
Reported-by [email protected]
Fixes 39a53e0ce0df ("f2fs: add superblock and major in-memory structure")
---
 fs/f2fs/checkpoint.c | 4 ++++
 fs/f2fs/f2fs.h       | 1 +
 fs/f2fs/inode.c      | 6 ++++++
 3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +0 -0
index bd890738b94d..ada2c548645c 100644
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
 		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
Quoted 3 lines

fs/f2fs/checkpoint.c int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) · line 754

Quoted 1 line
fs/f2fs/checkpoint.c +1 -0
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);

What about using SBI_POR_DOING?

SBI_POR_DOING will cover most flow of f2fs_fill_super(), I think we can add a separated flag just covering f2fs_recover_orphan_inodes(), so that we can allow iget() of root_inode and all inodes during roll-forward recovery to do sanity check nlink w/ zero. What do you think?

Quoted 5 lines
Quoted 1 line

Can we do this sanity check after f2fs_iget in the f2fs_unlink() only?

Sure, we need to cover f2fs_rename() as well, please check this:

https://lore.kernel.org/all/[email protected]

Hi Jaegeuk,

I'm testing this, seems there is a problem, once we opened an inode that has zeroed nlink, in f2fs_evict_inode(), the inode and all its data will be deleted, then leaving its stale dir entry in parent directory.

What do you think using v4? so that we may has chance to repair it w/ fsck rather than just deleting it?

---
 fs/f2fs/namei.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
fs/f2fs/namei.c +19 -0
index a278c7da8177..949621bc0d07 100644
@@ -547,6 +547,16 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 		goto fail;
 	}
 
+	if (unlikely(S_ISDIR(inode->i_mode) ?
+			inode->i_nlink <= 1 : inode->i_nlink == 0)) {
+		f2fs_err_ratelimited(sbi, "%s: inode (ino=%lx) has inconsistent nlink: %u, isdir: %d",
+				__func__, inode->i_ino, inode->i_nlink,
+				S_ISDIR(inode->i_mode));
+		err = -EFSCORRUPTED;
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		goto fail;
+	}
+
 	err = f2fs_dquot_initialize(dir);
 	if (err)
 		goto fail;
@@ -968,6 +978,15 @@ static int f2fs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
 	}
 
 	if (new_inode) {
+		if (unlikely(old_is_dir ?
+			new_inode->i_nlink <= 1 : new_inode->i_nlink == 0)) {
+			f2fs_err_ratelimited(sbi, "%s: inode (ino=%lx) has inconsistent nlink: %u, isdir: %d",
+				__func__, new_inode->i_ino, new_inode->i_nlink,
+				S_ISDIR(new_inode->i_mode));
+			err = -EFSCORRUPTED;
+			set_sbi_flag(sbi, SBI_NEED_FSCK);
+			goto out_dir;
+		}
 
 		err = -ENOTEMPTY;
 		if (old_is_dir && !f2fs_empty_dir(new_inode))
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Quoted 56 lines

Thanks,

Quoted 52 lines
Quoted 52 lines

Thanks,

Quoted 48 lines
Quoted 48 lines
fs/f2fs/checkpoint.c +3 -0
+
 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 		}
 		f2fs_put_page(page, 1);
 	}
+
 	/* clear Orphan Flag */
 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
 	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
 	SBI_IS_CLOSE,				/* specify unmounting */
 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
 	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
 	SBI_NEED_CP,				/* need to checkpoint */
 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 		}
 	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	return true;
 }
 
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
maintainer
#6 lore raw
Quoted 48 lines · Chao Yu
Quoted 41 lines · Chao Yu
Quoted 40 lines · Jaegeuk Kim
Quoted 38 lines · Chao Yu
Quoted 29 lines · Jaegeuk Kim
Quoted 28 lines · Chao Yu

From: Leo Stone <[email protected]>

The syzbot reproducer mounts a f2fs image, then tries to unlink an existing file. However, the unlinked file already has a link count of 0 when it is read for the first time in do_read_inode().

Add a check to sanity_check_inode() for i_nlink == 0.

[Chao Yu: rebase the code and fix orphan inode recovery issue]
Reported-by [email protected]
Fixes 39a53e0ce0df ("f2fs: add superblock and major in-memory structure")
---
 fs/f2fs/checkpoint.c | 4 ++++
 fs/f2fs/f2fs.h       | 1 +
 fs/f2fs/inode.c      | 6 ++++++
 3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +0 -0
index bd890738b94d..ada2c548645c 100644
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
 		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
Quoted 3 lines

fs/f2fs/checkpoint.c int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) · line 754

Quoted 1 line
fs/f2fs/checkpoint.c +1 -0
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);

What about using SBI_POR_DOING?

SBI_POR_DOING will cover most flow of f2fs_fill_super(), I think we can add a separated flag just covering f2fs_recover_orphan_inodes(), so that we can allow iget() of root_inode and all inodes during roll-forward recovery to do sanity check nlink w/ zero. What do you think?

Quoted 5 lines
Quoted 1 line

Can we do this sanity check after f2fs_iget in the f2fs_unlink() only?

Sure, we need to cover f2fs_rename() as well, please check this:

https://lore.kernel.org/all/[email protected]

Quoted 8 lines

Hi Jaegeuk,

I'm testing this, seems there is a problem, once we opened an inode that has zeroed nlink, in f2fs_evict_inode(), the inode and all its data will be deleted, then leaving its stale dir entry in parent directory.

What do you think using v4? so that we may has chance to repair it w/ fsck rather than just deleting it?

Do you mean v4 as the below change?

Quoted 101 lines
---
 fs/f2fs/namei.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
fs/f2fs/namei.c +19 -0
index a278c7da8177..949621bc0d07 100644
@@ -547,6 +547,16 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 		goto fail;
 	}
 
+	if (unlikely(S_ISDIR(inode->i_mode) ?
+			inode->i_nlink <= 1 : inode->i_nlink == 0)) {
+		f2fs_err_ratelimited(sbi, "%s: inode (ino=%lx) has inconsistent nlink: %u, isdir: %d",
+				__func__, inode->i_ino, inode->i_nlink,
+				S_ISDIR(inode->i_mode));
+		err = -EFSCORRUPTED;
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		goto fail;
+	}
+
 	err = f2fs_dquot_initialize(dir);
 	if (err)
 		goto fail;
@@ -968,6 +978,15 @@ static int f2fs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
 	}
 
 	if (new_inode) {
+		if (unlikely(old_is_dir ?
+			new_inode->i_nlink <= 1 : new_inode->i_nlink == 0)) {
+			f2fs_err_ratelimited(sbi, "%s: inode (ino=%lx) has inconsistent nlink: %u, isdir: %d",
+				__func__, new_inode->i_ino, new_inode->i_nlink,
+				S_ISDIR(new_inode->i_mode));
+			err = -EFSCORRUPTED;
+			set_sbi_flag(sbi, SBI_NEED_FSCK);
+			goto out_dir;
+		}
 
 		err = -ENOTEMPTY;
 		if (old_is_dir && !f2fs_empty_dir(new_inode))
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Quoted 56 lines

Thanks,

Quoted 52 lines
Quoted 52 lines

Thanks,

Quoted 48 lines
Quoted 48 lines
fs/f2fs/checkpoint.c +3 -0
+
 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 		}
 		f2fs_put_page(page, 1);
 	}
+
 	/* clear Orphan Flag */
 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
 	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
 	SBI_IS_CLOSE,				/* specify unmounting */
 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
 	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
 	SBI_NEED_CP,				/* need to checkpoint */
 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 		}
 	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	return true;
 }
 
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
author maintainer
#7 lore raw
Quoted 51 lines · Jaegeuk Kim
Quoted 50 lines · Chao Yu
Quoted 41 lines · Chao Yu
Quoted 40 lines · Jaegeuk Kim
Quoted 38 lines · Chao Yu
Quoted 29 lines · Jaegeuk Kim
Quoted 28 lines · Chao Yu

From: Leo Stone <[email protected]>

The syzbot reproducer mounts a f2fs image, then tries to unlink an existing file. However, the unlinked file already has a link count of 0 when it is read for the first time in do_read_inode().

Add a check to sanity_check_inode() for i_nlink == 0.

[Chao Yu: rebase the code and fix orphan inode recovery issue]
Reported-by [email protected]
Fixes 39a53e0ce0df ("f2fs: add superblock and major in-memory structure")
---
 fs/f2fs/checkpoint.c | 4 ++++
 fs/f2fs/f2fs.h       | 1 +
 fs/f2fs/inode.c      | 6 ++++++
 3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +0 -0
index bd890738b94d..ada2c548645c 100644
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
 		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
Quoted 3 lines

fs/f2fs/checkpoint.c int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) · line 754

Quoted 1 line
fs/f2fs/checkpoint.c +1 -0
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);

What about using SBI_POR_DOING?

SBI_POR_DOING will cover most flow of f2fs_fill_super(), I think we can add a separated flag just covering f2fs_recover_orphan_inodes(), so that we can allow iget() of root_inode and all inodes during roll-forward recovery to do sanity check nlink w/ zero. What do you think?

Quoted 5 lines
Quoted 1 line

Can we do this sanity check after f2fs_iget in the f2fs_unlink() only?

Sure, we need to cover f2fs_rename() as well, please check this:

https://lore.kernel.org/all/[email protected]

Hi Jaegeuk,

Quoted 8 lines
Quoted 6 lines

I'm testing this, seems there is a problem, once we opened an inode that has zeroed nlink, in f2fs_evict_inode(), the inode and all its data will be deleted, then leaving its stale dir entry in parent directory.

What do you think using v4? so that we may has chance to repair it w/ fsck rather than just deleting it?

Do you mean v4 as the below change?

I mean this v4 patch:

---
 fs/f2fs/checkpoint.c | 4 ++++
 fs/f2fs/f2fs.h       | 1 +
 fs/f2fs/inode.c      | 6 ++++++
 3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +4 -0
index bd890738b94d..ada2c548645c 100644
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
 		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
+
 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 		}
 		f2fs_put_page(page, 1);
 	}
+
 	/* clear Orphan Flag */
 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
 	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
 	SBI_IS_CLOSE,				/* specify unmounting */
 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
 	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
 	SBI_NEED_CP,				/* need to checkpoint */
 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 		}
 	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	return true;
 }
 

Thanks,

Quoted 102 lines
Quoted 102 lines

---
 fs/f2fs/namei.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
fs/f2fs/namei.c +19 -0
index a278c7da8177..949621bc0d07 100644
@@ -547,6 +547,16 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 		goto fail;
 	}
 
+	if (unlikely(S_ISDIR(inode->i_mode) ?
+			inode->i_nlink <= 1 : inode->i_nlink == 0)) {
+		f2fs_err_ratelimited(sbi, "%s: inode (ino=%lx) has inconsistent nlink: %u, isdir: %d",
+				__func__, inode->i_ino, inode->i_nlink,
+				S_ISDIR(inode->i_mode));
+		err = -EFSCORRUPTED;
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		goto fail;
+	}
+
 	err = f2fs_dquot_initialize(dir);
 	if (err)
 		goto fail;
@@ -968,6 +978,15 @@ static int f2fs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
 	}
 
 	if (new_inode) {
+		if (unlikely(old_is_dir ?
+			new_inode->i_nlink <= 1 : new_inode->i_nlink == 0)) {
+			f2fs_err_ratelimited(sbi, "%s: inode (ino=%lx) has inconsistent nlink: %u, isdir: %d",
+				__func__, new_inode->i_ino, new_inode->i_nlink,
+				S_ISDIR(new_inode->i_mode));
+			err = -EFSCORRUPTED;
+			set_sbi_flag(sbi, SBI_NEED_FSCK);
+			goto out_dir;
+		}
 
 		err = -ENOTEMPTY;
 		if (old_is_dir && !f2fs_empty_dir(new_inode))
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Quoted 56 lines

Thanks,

Quoted 52 lines
Quoted 52 lines

Thanks,

Quoted 48 lines
Quoted 48 lines
fs/f2fs/checkpoint.c +3 -0
+
 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 		}
 		f2fs_put_page(page, 1);
 	}
+
 	/* clear Orphan Flag */
 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
 	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
 	SBI_IS_CLOSE,				/* specify unmounting */
 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
 	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
 	SBI_NEED_CP,				/* need to checkpoint */
 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 		}
 	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	return true;
 }
 
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
maintainer
#8 lore raw
Quoted 56 lines · Chao Yu
Quoted 55 lines · Jaegeuk Kim
Quoted 54 lines · Chao Yu
Quoted 41 lines · Chao Yu
Quoted 40 lines · Jaegeuk Kim
Quoted 38 lines · Chao Yu
Quoted 29 lines · Jaegeuk Kim
Quoted 28 lines · Chao Yu

From: Leo Stone <[email protected]>

The syzbot reproducer mounts a f2fs image, then tries to unlink an existing file. However, the unlinked file already has a link count of 0 when it is read for the first time in do_read_inode().

Add a check to sanity_check_inode() for i_nlink == 0.

[Chao Yu: rebase the code and fix orphan inode recovery issue]
Reported-by [email protected]
Fixes 39a53e0ce0df ("f2fs: add superblock and major in-memory structure")
---
 fs/f2fs/checkpoint.c | 4 ++++
 fs/f2fs/f2fs.h       | 1 +
 fs/f2fs/inode.c      | 6 ++++++
 3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +0 -0
index bd890738b94d..ada2c548645c 100644
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
 		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
Quoted 3 lines

fs/f2fs/checkpoint.c int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) · line 754

Quoted 1 line
fs/f2fs/checkpoint.c +1 -0
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);

What about using SBI_POR_DOING?

SBI_POR_DOING will cover most flow of f2fs_fill_super(), I think we can add a separated flag just covering f2fs_recover_orphan_inodes(), so that we can allow iget() of root_inode and all inodes during roll-forward recovery to do sanity check nlink w/ zero. What do you think?

Quoted 5 lines
Quoted 1 line

Can we do this sanity check after f2fs_iget in the f2fs_unlink() only?

Sure, we need to cover f2fs_rename() as well, please check this:

https://lore.kernel.org/all/[email protected]

Hi Jaegeuk,

I'm testing this, seems there is a problem, once we opened an inode that has zeroed nlink, in f2fs_evict_inode(), the inode and all its data will be deleted, then leaving its stale dir entry in parent directory.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

What do you think using v4? so that we may has chance to repair it w/ fsck rather than just deleting it?

Do you mean v4 as the below change?

I mean this v4 patch:

What about checking i_nlink right after f2fs_iget in f2fs_lookup?

Quoted 165 lines
---
 fs/f2fs/checkpoint.c | 4 ++++
 fs/f2fs/f2fs.h       | 1 +
 fs/f2fs/inode.c      | 6 ++++++
 3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +4 -0
index bd890738b94d..ada2c548645c 100644
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
 		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
+
 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 		}
 		f2fs_put_page(page, 1);
 	}
+
 	/* clear Orphan Flag */
 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
 	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
 	SBI_IS_CLOSE,				/* specify unmounting */
 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
 	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
 	SBI_NEED_CP,				/* need to checkpoint */
 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 		}
 	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	return true;
 }
 

Thanks,

Quoted 102 lines
Quoted 102 lines

---
 fs/f2fs/namei.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
fs/f2fs/namei.c +19 -0
index a278c7da8177..949621bc0d07 100644
@@ -547,6 +547,16 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 		goto fail;
 	}
 
+	if (unlikely(S_ISDIR(inode->i_mode) ?
+			inode->i_nlink <= 1 : inode->i_nlink == 0)) {
+		f2fs_err_ratelimited(sbi, "%s: inode (ino=%lx) has inconsistent nlink: %u, isdir: %d",
+				__func__, inode->i_ino, inode->i_nlink,
+				S_ISDIR(inode->i_mode));
+		err = -EFSCORRUPTED;
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		goto fail;
+	}
+
 	err = f2fs_dquot_initialize(dir);
 	if (err)
 		goto fail;
@@ -968,6 +978,15 @@ static int f2fs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
 	}
 
 	if (new_inode) {
+		if (unlikely(old_is_dir ?
+			new_inode->i_nlink <= 1 : new_inode->i_nlink == 0)) {
+			f2fs_err_ratelimited(sbi, "%s: inode (ino=%lx) has inconsistent nlink: %u, isdir: %d",
+				__func__, new_inode->i_ino, new_inode->i_nlink,
+				S_ISDIR(new_inode->i_mode));
+			err = -EFSCORRUPTED;
+			set_sbi_flag(sbi, SBI_NEED_FSCK);
+			goto out_dir;
+		}
 
 		err = -ENOTEMPTY;
 		if (old_is_dir && !f2fs_empty_dir(new_inode))
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Quoted 56 lines

Thanks,

Quoted 52 lines
Quoted 52 lines

Thanks,

Quoted 48 lines
Quoted 48 lines
fs/f2fs/checkpoint.c +3 -0
+
 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 		}
 		f2fs_put_page(page, 1);
 	}
+
 	/* clear Orphan Flag */
 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
 	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
 	SBI_IS_CLOSE,				/* specify unmounting */
 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
 	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
 	SBI_NEED_CP,				/* need to checkpoint */
 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 		}
 	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	return true;
 }
 
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
author maintainer
#9 lore raw
Quoted 57 lines · Jaegeuk Kim
Quoted 56 lines · Chao Yu
Quoted 55 lines · Jaegeuk Kim
Quoted 54 lines · Chao Yu
Quoted 41 lines · Chao Yu
Quoted 40 lines · Jaegeuk Kim
Quoted 38 lines · Chao Yu
Quoted 29 lines · Jaegeuk Kim
Quoted 28 lines · Chao Yu

From: Leo Stone <[email protected]>

The syzbot reproducer mounts a f2fs image, then tries to unlink an existing file. However, the unlinked file already has a link count of 0 when it is read for the first time in do_read_inode().

Add a check to sanity_check_inode() for i_nlink == 0.

[Chao Yu: rebase the code and fix orphan inode recovery issue]
Reported-by [email protected]
Fixes 39a53e0ce0df ("f2fs: add superblock and major in-memory structure")
---
  fs/f2fs/checkpoint.c | 4 ++++
  fs/f2fs/f2fs.h       | 1 +
  fs/f2fs/inode.c      | 6 ++++++
  3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +0 -0
index bd890738b94d..ada2c548645c 100644
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
  	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
  		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
Quoted 3 lines

fs/f2fs/checkpoint.c int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) · line 754

Quoted 1 line
fs/f2fs/checkpoint.c +1 -0
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);

What about using SBI_POR_DOING?

SBI_POR_DOING will cover most flow of f2fs_fill_super(), I think we can add a separated flag just covering f2fs_recover_orphan_inodes(), so that we can allow iget() of root_inode and all inodes during roll-forward recovery to do sanity check nlink w/ zero. What do you think?

Quoted 5 lines
Quoted 1 line

Can we do this sanity check after f2fs_iget in the f2fs_unlink() only?

Sure, we need to cover f2fs_rename() as well, please check this:

https://lore.kernel.org/all/[email protected]

Hi Jaegeuk,

I'm testing this, seems there is a problem, once we opened an inode that has zeroed nlink, in f2fs_evict_inode(), the inode and all its data will be deleted, then leaving its stale dir entry in parent directory.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

What do you think using v4? so that we may has chance to repair it w/ fsck rather than just deleting it?

Do you mean v4 as the below change?

I mean this v4 patch:

What about checking i_nlink right after f2fs_iget in f2fs_lookup?

Fine to me, I've updated in v5.

Thanks,

Quoted 166 lines
Quoted 166 lines

---
  fs/f2fs/checkpoint.c | 4 ++++
  fs/f2fs/f2fs.h       | 1 +
  fs/f2fs/inode.c      | 6 ++++++
  3 files changed, 11 insertions(+)
fs/f2fs/checkpoint.c +4 -0
index bd890738b94d..ada2c548645c 100644
@@ -751,6 +751,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
  	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
  		f2fs_info(sbi, "orphan cleanup on readonly fs");
 
+	set_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
+
  	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
  	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
  		}
  		f2fs_put_page(page, 1);
  	}
+
  	/* clear Orphan Flag */
  	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
  out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
  	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
  	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
  	SBI_IS_CLOSE,				/* specify unmounting */
  	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
  	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
  	SBI_NEED_SB_WRITE,			/* need to recover superblock */
  	SBI_NEED_CP,				/* need to checkpoint */
  	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
  		}
  	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
  	return true;
  }
 

Thanks,

Quoted 102 lines
Quoted 102 lines

---
  fs/f2fs/namei.c | 19 +++++++++++++++++++
  1 file changed, 19 insertions(+)
fs/f2fs/namei.c +19 -0
index a278c7da8177..949621bc0d07 100644
@@ -547,6 +547,16 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
  		goto fail;
  	}
 
+	if (unlikely(S_ISDIR(inode->i_mode) ?
+			inode->i_nlink <= 1 : inode->i_nlink == 0)) {
+		f2fs_err_ratelimited(sbi, "%s: inode (ino=%lx) has inconsistent nlink: %u, isdir: %d",
+				__func__, inode->i_ino, inode->i_nlink,
+				S_ISDIR(inode->i_mode));
+		err = -EFSCORRUPTED;
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		goto fail;
+	}
+
  	err = f2fs_dquot_initialize(dir);
  	if (err)
  		goto fail;
@@ -968,6 +978,15 @@ static int f2fs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
  	}
 
  	if (new_inode) {
+		if (unlikely(old_is_dir ?
+			new_inode->i_nlink <= 1 : new_inode->i_nlink == 0)) {
+			f2fs_err_ratelimited(sbi, "%s: inode (ino=%lx) has inconsistent nlink: %u, isdir: %d",
+				__func__, new_inode->i_ino, new_inode->i_nlink,
+				S_ISDIR(new_inode->i_mode));
+			err = -EFSCORRUPTED;
+			set_sbi_flag(sbi, SBI_NEED_FSCK);
+			goto out_dir;
+		}
 
  		err = -ENOTEMPTY;
  		if (old_is_dir && !f2fs_empty_dir(new_inode))
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Quoted 56 lines

Thanks,

Quoted 52 lines
Quoted 52 lines

Thanks,

Quoted 48 lines
Quoted 48 lines
fs/f2fs/checkpoint.c +3 -0
+
  	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
  	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
@@ -778,9 +780,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
  		}
  		f2fs_put_page(page, 1);
  	}
+
  	/* clear Orphan Flag */
  	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
  out:
+	clear_sbi_flag(sbi, SBI_ORPHAN_RECOVERY);
  	set_sbi_flag(sbi, SBI_IS_RECOVERED);
 
  	return err;
fs/f2fs/f2fs.h +1 -0
index 05879c6dc4d6..1c75081c0c14 100644
@@ -1322,6 +1322,7 @@ enum {
  	SBI_IS_CLOSE,				/* specify unmounting */
  	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
  	SBI_POR_DOING,				/* recovery is doing or not */
+	SBI_ORPHAN_RECOVERY,			/* orphan inodes recovery is doing */
  	SBI_NEED_SB_WRITE,			/* need to recover superblock */
  	SBI_NEED_CP,				/* need to checkpoint */
  	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
fs/f2fs/inode.c +6 -0
index d6ad7810df69..02f1b69d03d8 100644
@@ -386,6 +386,12 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
  		}
  	}
 
+	if (inode->i_nlink == 0 && !is_sbi_flag_set(sbi, SBI_ORPHAN_RECOVERY)) {
+		f2fs_warn(sbi, "%s: inode (ino=%lx) has a link count of 0",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
  	return true;
  }
 
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog