Skip to contentSkip to conversation

f2fs: Add sanity checks before unlinking and loading inodes

PATCH Superseded Nikola Z. Ivanov 2 participants 6 messages Nov 1, 2025 – Nov 3, 2025 F2FS file system

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover f2fs: Add sanity checks before unlinking and loading inodes

This series is provoked by syzbot warnings caused by corrupted directory inode with i_nlink == 1 that passes the initial sanity check which will only mark the filesystem as corrupted in case i_nlink == 0.

Tests:
- fio/fsmark parallel create/unlink on VM with f2fs root filesystem.
- syzbot

Changelog:
Changes from v1:
- Rename exit label in f2fs_unlink().
- Add sanity check in sanity_check_inode() and remove it from f2fs_iget(). as suggested by Chao Yu in order to detect on-disk corruption early.
https://lore.kernel.org/linux-f2fs-devel/[email protected]/T/#t

Nikola Z. Ivanov (2):
f2fs: Rename f2fs_unlink exit label
f2fs: Add sanity checks before unlinking and loading inodes

 fs/f2fs/inode.c | 10 ++++++++++
 fs/f2fs/namei.c | 27 +++++++++++++++++----------
 2 files changed, 27 insertions(+), 10 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 1/2 f2fs: Rename f2fs_unlink exit label

Rename "fail" label to "out" as it's used as a default exit path out of f2fs_unlink as well as error path.

---
 fs/f2fs/namei.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
fs/f2fs/namei.c +7 -7
index b882771e4699..40cf80fd9d9a 100644
@@ -552,21 +552,21 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 
 	if (unlikely(f2fs_cp_error(sbi))) {
 		err = -EIO;
-		goto fail;
+		goto out;
 	}
 
 	err = f2fs_dquot_initialize(dir);
 	if (err)
-		goto fail;
+		goto out;
 	err = f2fs_dquot_initialize(inode);
 	if (err)
-		goto fail;
+		goto out;
 
 	de = f2fs_find_entry(dir, &dentry->d_name, &folio);
 	if (!de) {
 		if (IS_ERR(folio))
 			err = PTR_ERR(folio);
-		goto fail;
+		goto out;
 	}
 
 	if (unlikely(inode->i_nlink == 0)) {
@@ -575,7 +575,7 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 		err = -EFSCORRUPTED;
 		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
 		f2fs_folio_put(folio, false);
-		goto fail;
+		goto out;
 	}
 
 	f2fs_balance_fs(sbi, true);
@@ -585,7 +585,7 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 	if (err) {
 		f2fs_unlock_op(sbi);
 		f2fs_folio_put(folio, false);
-		goto fail;
+		goto out;
 	}
 	f2fs_delete_entry(de, folio, dir, inode);
 	f2fs_unlock_op(sbi);
@@ -601,7 +601,7 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 
 	if (IS_DIRSYNC(dir))
 		f2fs_sync_fs(sbi->sb, 1);
-fail:
+out:
 	trace_f2fs_unlink_exit(inode, err);
 	return err;
 }
Signature · 1 line
2.51.0
#2 lore raw

PATCH v2 2/2 f2fs: Add sanity checks before unlinking and loading inodes

In reply to Nikola Z. Ivanov

Add check for inode->i_nlink == 1 for directories during unlink, as their value is decremented twice, which can trigger a warning in drop_nlink. In such case mark the filesystem as corrupted and return from the function call with the relevant failure return value.

Additionally add the 2 checks for i_nlink == 0 and i_nlink == 1 in sanity_check_inode in order to detect on-disk corruption early.

---
 fs/f2fs/inode.c | 10 ++++++++++
 fs/f2fs/namei.c | 15 +++++++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)
fs/f2fs/inode.c +10 -0
index 8c4eafe9ffac..089cbf3646f0 100644
@@ -294,6 +294,16 @@ static bool sanity_check_inode(struct inode *inode, struct folio *node_folio)
 		return false;
 	}
 
+	if (unlikely(inode->i_nlink == 0)) {
+		f2fs_warn(F2FS_I_SB(inode), "%s: inode (ino=%lx) has zero i_nlink",
+			  __func__, inode->i_ino);
+		return false;
+	} else if (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) {
+		f2fs_warn(F2FS_I_SB(inode), "%s: directory inode (ino=%lx) has a single i_nlink",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	if (f2fs_has_extra_attr(inode)) {
 		if (!f2fs_sb_has_extra_attr(sbi)) {
 			f2fs_warn(sbi, "%s: inode (ino=%lx) is with extra_attr, but extra_attr feature is off",
fs/f2fs/namei.c +11 -4
index 40cf80fd9d9a..d13077bad482 100644
@@ -572,10 +572,11 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 	if (unlikely(inode->i_nlink == 0)) {
 		f2fs_warn(F2FS_I_SB(inode), "%s: inode (ino=%lx) has zero i_nlink",
 			  __func__, inode->i_ino);
-		err = -EFSCORRUPTED;
-		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
-		f2fs_folio_put(folio, false);
-		goto out;
+		goto corrupted;
+	} else if (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) {
+		f2fs_warn(F2FS_I_SB(inode), "%s: directory inode (ino=%lx) has a single i_nlink",
+			  __func__, inode->i_ino);
+		goto corrupted;
 	}
 
 	f2fs_balance_fs(sbi, true);
@@ -601,6 +602,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 
 	if (IS_DIRSYNC(dir))
 		f2fs_sync_fs(sbi->sb, 1);
+
+	goto out;
+corrupted:
+	err = -EFSCORRUPTED;
+	set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
+	f2fs_folio_put(folio, false);
 out:
 	trace_f2fs_unlink_exit(inode, err);
 	return err;
Signature · 1 line
2.51.0
maintainer
#3 lore raw
Quoted 22 lines · Nikola Z. Ivanov

Add check for inode->i_nlink == 1 for directories during unlink, as their value is decremented twice, which can trigger a warning in drop_nlink. In such case mark the filesystem as corrupted and return from the function call with the relevant failure return value.

Additionally add the 2 checks for i_nlink == 0 and i_nlink == 1 in sanity_check_inode in order to detect on-disk corruption early.

---
 fs/f2fs/inode.c | 10 ++++++++++
 fs/f2fs/namei.c | 15 +++++++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)
fs/f2fs/inode.c +0 -0
index 8c4eafe9ffac..089cbf3646f0 100644

fs/f2fs/inode.c static bool sanity_check_inode(struct inode *inode, struct folio *node_folio) · line 297

Quoted 4 lines
fs/f2fs/inode.c +1 -0
@@ -294,6 +294,16 @@ static bool sanity_check_inode(struct inode *inode, struct folio *node_folio)
 		return false;
 	}
 
+	if (unlikely(inode->i_nlink == 0)) {

This is a possible case, as an orphan inode may exist in filesystem after sudden power-cut.

Thanks,

Quoted 45 lines
fs/f2fs/inode.c +9 -0
+		f2fs_warn(F2FS_I_SB(inode), "%s: inode (ino=%lx) has zero i_nlink",
+			  __func__, inode->i_ino);
+		return false;
+	} else if (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) {
+		f2fs_warn(F2FS_I_SB(inode), "%s: directory inode (ino=%lx) has a single i_nlink",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	if (f2fs_has_extra_attr(inode)) {
 		if (!f2fs_sb_has_extra_attr(sbi)) {
 			f2fs_warn(sbi, "%s: inode (ino=%lx) is with extra_attr, but extra_attr feature is off",
fs/f2fs/namei.c +11 -4
index 40cf80fd9d9a..d13077bad482 100644
@@ -572,10 +572,11 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 	if (unlikely(inode->i_nlink == 0)) {
 		f2fs_warn(F2FS_I_SB(inode), "%s: inode (ino=%lx) has zero i_nlink",
 			  __func__, inode->i_ino);
-		err = -EFSCORRUPTED;
-		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
-		f2fs_folio_put(folio, false);
-		goto out;
+		goto corrupted;
+	} else if (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) {
+		f2fs_warn(F2FS_I_SB(inode), "%s: directory inode (ino=%lx) has a single i_nlink",
+			  __func__, inode->i_ino);
+		goto corrupted;
 	}
 
 	f2fs_balance_fs(sbi, true);
@@ -601,6 +602,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 
 	if (IS_DIRSYNC(dir))
 		f2fs_sync_fs(sbi->sb, 1);
+
+	goto out;
+corrupted:
+	err = -EFSCORRUPTED;
+	set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
+	f2fs_folio_put(folio, false);
 out:
 	trace_f2fs_unlink_exit(inode, err);
 	return err;
#4 lore raw
Quoted 26 lines · Chao Yu
Quoted 25 lines · Nikola Z. Ivanov

Add check for inode->i_nlink == 1 for directories during unlink, as their value is decremented twice, which can trigger a warning in drop_nlink. In such case mark the filesystem as corrupted and return from the function call with the relevant failure return value.

Additionally add the 2 checks for i_nlink == 0 and i_nlink == 1 in sanity_check_inode in order to detect on-disk corruption early.

---
 fs/f2fs/inode.c | 10 ++++++++++
 fs/f2fs/namei.c | 15 +++++++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)
fs/f2fs/inode.c +0 -0
index 8c4eafe9ffac..089cbf3646f0 100644
@@ -294,6 +294,16 @@ static bool sanity_check_inode(struct inode *inode, struct folio *node_folio)
 		return false;
 	}
 
Quoted 7 lines

fs/f2fs/inode.c static bool sanity_check_inode(struct inode *inode, struct folio *node_folio) · line 297

Quoted 1 line
fs/f2fs/inode.c +1 -0
+	if (unlikely(inode->i_nlink == 0)) {

This is a possible case, as an orphan inode may exist in filesystem after sudden power-cut.

Thanks,

Hi Chao,

Do you suggest that it should not be wrapped in unlikely()?

I also now realise that I intended to wrap the "else if" case as well but I've missed it in the final patch.

Should I resend the patch with both cases wrapped in "unlikely()" or would you suggest otherwise?

Quoted 46 lines
Quoted 45 lines
fs/f2fs/inode.c +9 -0
+		f2fs_warn(F2FS_I_SB(inode), "%s: inode (ino=%lx) has zero i_nlink",
+			  __func__, inode->i_ino);
+		return false;
+	} else if (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) {
+		f2fs_warn(F2FS_I_SB(inode), "%s: directory inode (ino=%lx) has a single i_nlink",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	if (f2fs_has_extra_attr(inode)) {
 		if (!f2fs_sb_has_extra_attr(sbi)) {
 			f2fs_warn(sbi, "%s: inode (ino=%lx) is with extra_attr, but extra_attr feature is off",
fs/f2fs/namei.c +11 -4
index 40cf80fd9d9a..d13077bad482 100644
@@ -572,10 +572,11 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 	if (unlikely(inode->i_nlink == 0)) {
 		f2fs_warn(F2FS_I_SB(inode), "%s: inode (ino=%lx) has zero i_nlink",
 			  __func__, inode->i_ino);
-		err = -EFSCORRUPTED;
-		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
-		f2fs_folio_put(folio, false);
-		goto out;
+		goto corrupted;
+	} else if (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) {
+		f2fs_warn(F2FS_I_SB(inode), "%s: directory inode (ino=%lx) has a single i_nlink",
+			  __func__, inode->i_ino);
+		goto corrupted;
 	}
 
 	f2fs_balance_fs(sbi, true);
@@ -601,6 +602,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 
 	if (IS_DIRSYNC(dir))
 		f2fs_sync_fs(sbi->sb, 1);
+
+	goto out;
+corrupted:
+	err = -EFSCORRUPTED;
+	set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
+	f2fs_folio_put(folio, false);
 out:
 	trace_f2fs_unlink_exit(inode, err);
 	return err;
maintainer
#5 lore raw
Quoted 32 lines · Nikola Z. Ivanov
Quoted 31 lines · Chao Yu
Quoted 22 lines · Nikola Z. Ivanov

Add check for inode->i_nlink == 1 for directories during unlink, as their value is decremented twice, which can trigger a warning in drop_nlink. In such case mark the filesystem as corrupted and return from the function call with the relevant failure return value.

Additionally add the 2 checks for i_nlink == 0 and i_nlink == 1 in sanity_check_inode in order to detect on-disk corruption early.

---
 fs/f2fs/inode.c | 10 ++++++++++
 fs/f2fs/namei.c | 15 +++++++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)
fs/f2fs/inode.c +0 -0
index 8c4eafe9ffac..089cbf3646f0 100644

fs/f2fs/inode.c static bool sanity_check_inode(struct inode *inode, struct folio *node_folio) · line 297

Quoted 4 lines
fs/f2fs/inode.c +1 -0
@@ -294,6 +294,16 @@ static bool sanity_check_inode(struct inode *inode, struct folio *node_folio)
 		return false;
 	}
 
+	if (unlikely(inode->i_nlink == 0)) {

This is a possible case, as an orphan inode may exist in filesystem after sudden power-cut.

Quoted 6 lines
Quoted 2 lines

Thanks,

Hi Chao,

Do you suggest that it should not be wrapped in unlikely()?

Nikola,

No, I think we should not add this sanity check "inode->i_nlink == 0" into sanity_check_inode(), as for an orphan inode, its i_nlink is zero. We expect to get the inode w/o failure in recover_orphan_inode().

Quoted 3 lines

I also now realise that I intended to wrap the "else if" case as well but I've missed it in the final patch.

Looks fine to add unlike for the "dir->i_nlink == 1" case.

Thanks,

Quoted 51 lines

Should I resend the patch with both cases wrapped in "unlikely()" or would you suggest otherwise?

Quoted 46 lines
Quoted 45 lines
fs/f2fs/inode.c +9 -0
+		f2fs_warn(F2FS_I_SB(inode), "%s: inode (ino=%lx) has zero i_nlink",
+			  __func__, inode->i_ino);
+		return false;
+	} else if (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) {
+		f2fs_warn(F2FS_I_SB(inode), "%s: directory inode (ino=%lx) has a single i_nlink",
+			  __func__, inode->i_ino);
+		return false;
+	}
+
 	if (f2fs_has_extra_attr(inode)) {
 		if (!f2fs_sb_has_extra_attr(sbi)) {
 			f2fs_warn(sbi, "%s: inode (ino=%lx) is with extra_attr, but extra_attr feature is off",
fs/f2fs/namei.c +11 -4
index 40cf80fd9d9a..d13077bad482 100644
@@ -572,10 +572,11 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 	if (unlikely(inode->i_nlink == 0)) {
 		f2fs_warn(F2FS_I_SB(inode), "%s: inode (ino=%lx) has zero i_nlink",
 			  __func__, inode->i_ino);
-		err = -EFSCORRUPTED;
-		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
-		f2fs_folio_put(folio, false);
-		goto out;
+		goto corrupted;
+	} else if (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) {
+		f2fs_warn(F2FS_I_SB(inode), "%s: directory inode (ino=%lx) has a single i_nlink",
+			  __func__, inode->i_ino);
+		goto corrupted;
 	}
 
 	f2fs_balance_fs(sbi, true);
@@ -601,6 +602,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 
 	if (IS_DIRSYNC(dir))
 		f2fs_sync_fs(sbi->sb, 1);
+
+	goto out;
+corrupted:
+	err = -EFSCORRUPTED;
+	set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
+	f2fs_folio_put(folio, false);
 out:
 	trace_f2fs_unlink_exit(inode, err);
 	return err;