All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chao Yu via Linux-f2fs-devel <linux-f2fs-devel@lists.sourceforge.net>
To: "Nikola Z. Ivanov" <zlatistiv@gmail.com>
Cc: david.hunter.linux@gmail.com,
	syzbot+c07d47c7bc68f47b9083@syzkaller.appspotmail.com,
	linux-kernel@vger.kernel.org,
	linux-f2fs-devel@lists.sourceforge.net, khalid@kernel.org,
	skhan@linuxfoundation.org, jaegeuk@kernel.org,
	linux-kernel-mentees@lists.linuxfoundation.org
Subject: Re: [f2fs-dev] [PATCH v2 2/2] f2fs: Add sanity checks before unlinking and loading inodes
Date: Mon, 3 Nov 2025 16:12:15 +0800	[thread overview]
Message-ID: <1f519357-a489-41fe-8159-a8e319aedd17@kernel.org> (raw)
In-Reply-To: <pcxf66ac2yjkqyvhb6xgbk6jiihcejuncgbblkewz6rs7i5uzt@m6yjin7t67is>

On 11/3/25 15:55, Nikola Z. Ivanov wrote:
> On Mon, Nov 03, 2025 at 10:35:17AM +0800, Chao Yu wrote:
>> On 11/1/25 20:56, Nikola Z. Ivanov wrote:
>>> 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.
>>>
>>> Reported-by: syzbot+c07d47c7bc68f47b9083@syzkaller.appspotmail.com
>>> Closes: https://syzkaller.appspot.com/bug?extid=c07d47c7bc68f47b9083
>>> Tested-by: syzbot+c07d47c7bc68f47b9083@syzkaller.appspotmail.com
>>> Signed-off-by: Nikola Z. Ivanov <zlatistiv@gmail.com>
>>> ---
>>>  fs/f2fs/inode.c | 10 ++++++++++
>>>  fs/f2fs/namei.c | 15 +++++++++++----
>>>  2 files changed, 21 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
>>> index 8c4eafe9ffac..089cbf3646f0 100644
>>> --- a/fs/f2fs/inode.c
>>> +++ b/fs/f2fs/inode.c
>>> @@ -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,
>>
> 
> 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().

> 
> 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,

> 
> Should I resend the patch with both cases wrapped in "unlikely()"
> or would you suggest otherwise?
> 
> 
>>> +		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",
>>> diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
>>> index 40cf80fd9d9a..d13077bad482 100644
>>> --- a/fs/f2fs/namei.c
>>> +++ b/fs/f2fs/namei.c
>>> @@ -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;
>>



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

WARNING: multiple messages have this Message-ID (diff)
From: Chao Yu <chao@kernel.org>
To: "Nikola Z. Ivanov" <zlatistiv@gmail.com>
Cc: chao@kernel.org, jaegeuk@kernel.org,
	linux-f2fs-devel@lists.sourceforge.net,
	linux-kernel@vger.kernel.org, skhan@linuxfoundation.org,
	david.hunter.linux@gmail.com,
	linux-kernel-mentees@lists.linuxfoundation.org,
	khalid@kernel.org,
	syzbot+c07d47c7bc68f47b9083@syzkaller.appspotmail.com
Subject: Re: [PATCH v2 2/2] f2fs: Add sanity checks before unlinking and loading inodes
Date: Mon, 3 Nov 2025 16:12:15 +0800	[thread overview]
Message-ID: <1f519357-a489-41fe-8159-a8e319aedd17@kernel.org> (raw)
In-Reply-To: <pcxf66ac2yjkqyvhb6xgbk6jiihcejuncgbblkewz6rs7i5uzt@m6yjin7t67is>

On 11/3/25 15:55, Nikola Z. Ivanov wrote:
> On Mon, Nov 03, 2025 at 10:35:17AM +0800, Chao Yu wrote:
>> On 11/1/25 20:56, Nikola Z. Ivanov wrote:
>>> 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.
>>>
>>> Reported-by: syzbot+c07d47c7bc68f47b9083@syzkaller.appspotmail.com
>>> Closes: https://syzkaller.appspot.com/bug?extid=c07d47c7bc68f47b9083
>>> Tested-by: syzbot+c07d47c7bc68f47b9083@syzkaller.appspotmail.com
>>> Signed-off-by: Nikola Z. Ivanov <zlatistiv@gmail.com>
>>> ---
>>>  fs/f2fs/inode.c | 10 ++++++++++
>>>  fs/f2fs/namei.c | 15 +++++++++++----
>>>  2 files changed, 21 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
>>> index 8c4eafe9ffac..089cbf3646f0 100644
>>> --- a/fs/f2fs/inode.c
>>> +++ b/fs/f2fs/inode.c
>>> @@ -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,
>>
> 
> 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().

> 
> 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,

> 
> Should I resend the patch with both cases wrapped in "unlikely()"
> or would you suggest otherwise?
> 
> 
>>> +		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",
>>> diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
>>> index 40cf80fd9d9a..d13077bad482 100644
>>> --- a/fs/f2fs/namei.c
>>> +++ b/fs/f2fs/namei.c
>>> @@ -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;
>>


  reply	other threads:[~2025-11-03  8:12 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-01 12:56 [f2fs-dev] [PATCH v2 0/2] f2fs: Add sanity checks before unlinking and loading inodes Nikola Z. Ivanov
2025-11-01 12:56 ` Nikola Z. Ivanov
2025-11-01 12:56 ` [f2fs-dev] [PATCH v2 1/2] f2fs: Rename f2fs_unlink exit label Nikola Z. Ivanov
2025-11-01 12:56   ` Nikola Z. Ivanov
2025-11-01 12:56 ` [f2fs-dev] [PATCH v2 2/2] f2fs: Add sanity checks before unlinking and loading inodes Nikola Z. Ivanov
2025-11-01 12:56   ` Nikola Z. Ivanov
2025-11-03  2:35   ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2025-11-03  2:35     ` Chao Yu
2025-11-03  7:55     ` [f2fs-dev] " Nikola Z. Ivanov
2025-11-03  7:55       ` Nikola Z. Ivanov
2025-11-03  8:12       ` Chao Yu via Linux-f2fs-devel [this message]
2025-11-03  8:12         ` Chao Yu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1f519357-a489-41fe-8159-a8e319aedd17@kernel.org \
    --to=linux-f2fs-devel@lists.sourceforge.net \
    --cc=chao@kernel.org \
    --cc=david.hunter.linux@gmail.com \
    --cc=jaegeuk@kernel.org \
    --cc=khalid@kernel.org \
    --cc=linux-kernel-mentees@lists.linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=syzbot+c07d47c7bc68f47b9083@syzkaller.appspotmail.com \
    --cc=zlatistiv@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.