f2fs: Perform sanity check before unlinking directory inode
PATCH 1/1 f2fs: Perform sanity check before unlinking directory inode
Current i_nlink corruption check does not take into account directory inodes which have one additional i_nlink for their "." entry.
Add additional check and a common corruption path.
--- fs/f2fs/namei.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)
fs/f2fs/namei.c +20 -8
@@ -502,12 +502,14 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, goto out; } -if (inode->i_nlink ==0 ) { +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 ); - goto out_iput; + goto corrupted; + }else if (unlikely (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; } if (IS_ENCRYPTED(dir) &&
@@ -533,6 +535,9 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,trace_f2fs_lookup_end (dir, !IS_ERR_OR_NULL (new) ? new : dentry, ino,IS_ERR (new) ?PTR_ERR (new) : err);return new; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); out_iput:iput (inode); out:
@@ -572,10 +577,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 fail; + goto corrupted; + }else if (unlikely (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 +607,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))f2fs_sync_fs (sbi->sb,1 ); + + goto fail; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); +f2fs_folio_put (folio, false); fail:trace_f2fs_unlink_exit (inode, err);return err;
Signature · 1 line
2.51.0
Quoted 4 lines · Nikola Z. Ivanov
Current
i_nlinkcorruption check does not take into account directory inodes which have one additionali_nlinkfor their "." entry.Add additional check and a common corruption path.
How does this change fix the problem? What is common corruption path in this context?
Quoted 5 lines
Reported-by [email protected]Fixes 81edb983b3f5 ("f2fs: add check for deleted inode")Signed-off-by Nikola Z. Ivanov <[email protected]>
How did you test this patch? Can add details?
Quoted 66 lines
--- fs/f2fs/namei.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)fs/f2fs/namei.c +20
-8@@ -502,12 +502,14 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, goto out; } -if (inode->i_nlink ==0 ) { +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 ); - goto out_iput; + goto corrupted; + }else if (unlikely (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; } if (IS_ENCRYPTED(dir) &&@@ -533,6 +535,9 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,trace_f2fs_lookup_end (dir, !IS_ERR_OR_NULL (new) ? new : dentry, ino,IS_ERR (new) ?PTR_ERR (new) : err);return new; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); out_iput:iput (inode); out:@@ -572,10 +577,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 fail; + goto corrupted; + }else if (unlikely (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 +607,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))f2fs_sync_fs (sbi->sb,1 ); + + goto fail; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); +f2fs_folio_put (folio, false); fail:trace_f2fs_unlink_exit (inode, err);return err;
Chao Yu
maintainerQuoted 23 lines · Nikola Z. Ivanov
Current
i_nlinkcorruption check does not take into account directory inodes which have one additionali_nlinkfor their "." entry.Add additional check and a common corruption path.
Reported-by [email protected]Fixes 81edb983b3f5 ("f2fs: add check for deleted inode")Signed-off-by Nikola Z. Ivanov <[email protected]>--- fs/f2fs/namei.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)fs/f2fs/namei.c +1
-1@@ -502,12 +502,14 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, goto out; } -if (inode->i_nlink ==0 ) { +if (unlikely (inode->i_nlink ==0 )) {
fs/f2fs/namei.c static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, · line 512
Quoted 10 lines
fs/f2fs/namei.c +5
-3f2fs_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 ); - goto out_iput; + goto corrupted; + }else if (unlikely (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;
Can we detect such corruption in sanity_check_inode() as well? So that if f2fs internal flow calls f2fs_iget() on corrupted inode, we can set SBI_NEED_FSCK flag and then triggering fsck repairment later.
Thanks,
Quoted 42 lines
fs/f2fs/namei.c +14
-4} if (IS_ENCRYPTED(dir) &&@@ -533,6 +535,9 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,trace_f2fs_lookup_end (dir, !IS_ERR_OR_NULL (new) ? new : dentry, ino,IS_ERR (new) ?PTR_ERR (new) : err);return new; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); out_iput:iput (inode); out:@@ -572,10 +577,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 fail; + goto corrupted; + }else if (unlikely (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 +607,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))f2fs_sync_fs (sbi->sb,1 ); + + goto fail; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); +f2fs_folio_put (folio, false); fail:trace_f2fs_unlink_exit (inode, err);return err;
Quoted 71 lines · Chao Yu
Quoted 22 lines · Nikola Z. Ivanov
Current
i_nlinkcorruption check does not take into account directory inodes which have one additionali_nlinkfor their "." entry.Add additional check and a common corruption path.
Reported-by [email protected]Fixes 81edb983b3f5 ("f2fs: add check for deleted inode")Signed-off-by Nikola Z. Ivanov <[email protected]>--- fs/f2fs/namei.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)fs/f2fs/namei.c +1
-1@@ -502,12 +502,14 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, goto out; } -if (inode->i_nlink ==0 ) { +if (unlikely (inode->i_nlink ==0 )) {fs/f2fs/namei.c static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, · line 511
Quoted 10 lines
fs/f2fs/namei.c +5
-3f2fs_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 ); - goto out_iput; + goto corrupted; + }else if (unlikely (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;Can we detect such corruption in
sanity_check_inode()as well? So that if f2fs internal flow callsf2fs_iget()on corrupted inode, we can setSBI_NEED_FSCKflag and then triggering fsck repairment later.Thanks,
Quoted 31 lines
fs/f2fs/namei.c +9
-4} if (IS_ENCRYPTED(dir) &&@@ -533,6 +535,9 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,trace_f2fs_lookup_end (dir, !IS_ERR_OR_NULL (new) ? new : dentry, ino,IS_ERR (new) ?PTR_ERR (new) : err);return new; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); out_iput:iput (inode); out:@@ -572,10 +577,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 fail; + goto corrupted; + }else if (unlikely (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 +607,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))f2fs_sync_fs (sbi->sb,1 ); +
Quoted 9 lines
fs/f2fs/namei.c static int f2fs_unlink(struct inode *dir, struct dentry *dentry) · line 618
Quoted 8 lines
fs/f2fs/namei.c +5
-0+ goto fail; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); +f2fs_folio_put (folio, false); fail:trace_f2fs_unlink_exit (inode, err);return err;
Hi Chao,
Thank you for the suggestion.
I will add this to sanity_check_inode and remove it from f2fs_lookup as it becomes redundant since f2fs_lookup obtains the inode through f2fs_iget. For f2fs_unlink I will move the i_nlink == 1 check to f2fs_rmdir.
I will send v2 as soon as I do some more testing.
Chao Yu
maintainerQuoted 82 lines · Nikola Z. Ivanov
Quoted 71 lines · Chao Yu
Quoted 22 lines · Nikola Z. Ivanov
Current
i_nlinkcorruption check does not take into account directory inodes which have one additionali_nlinkfor their "." entry.Add additional check and a common corruption path.
Reported-by [email protected]Fixes 81edb983b3f5 ("f2fs: add check for deleted inode")Signed-off-by Nikola Z. Ivanov <[email protected]>--- fs/f2fs/namei.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)fs/f2fs/namei.c +1
-1@@ -502,12 +502,14 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, goto out; } -if (inode->i_nlink ==0 ) { +if (unlikely (inode->i_nlink ==0 )) {fs/f2fs/namei.c static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, · line 511
Quoted 10 lines
fs/f2fs/namei.c +5
-3f2fs_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 ); - goto out_iput; + goto corrupted; + }else if (unlikely (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;Can we detect such corruption in
sanity_check_inode()as well? So that if f2fs internal flow callsf2fs_iget()on corrupted inode, we can setSBI_NEED_FSCKflag and then triggering fsck repairment later.Thanks,
Quoted 31 lines
fs/f2fs/namei.c +9
-4} if (IS_ENCRYPTED(dir) &&@@ -533,6 +535,9 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,trace_f2fs_lookup_end (dir, !IS_ERR_OR_NULL (new) ? new : dentry, ino,IS_ERR (new) ?PTR_ERR (new) : err);return new; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); out_iput:iput (inode); out:@@ -572,10 +577,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 fail; + goto corrupted; + }else if (unlikely (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 +607,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))f2fs_sync_fs (sbi->sb,1 ); +Quoted 9 lines
fs/f2fs/namei.c static int f2fs_unlink(struct inode *dir, struct dentry *dentry) · line 618
Quoted 8 lines
fs/f2fs/namei.c +5
-0+ goto fail; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); +f2fs_folio_put (folio, false); fail:trace_f2fs_unlink_exit (inode, err);return err;
Quoted 7 lines
Hi Chao,
Thank you for the suggestion.
I will add this tosanity_check_inodeand remove it fromf2fs_lookupas it becomes redundant sincef2fs_lookupobtains the inode throughf2fs_iget. Forf2fs_unlinkI will move thei_nlink== 1 check tof2fs_rmdir.
Hi Nikola,
I meant we can move the i_nlink == 1 check from both f2fs_lookup() and f2fs_unlink() to sanity_check_inode(), because before we create in-memory inode, we will always call sanity_check_inode().
Let me know if you have other concerns.
Thanks,
Quoted 2 lines
I will send v2 as soon as I do some more testing.
Quoted 93 lines · Chao Yu
Quoted 82 lines · Nikola Z. Ivanov
Quoted 71 lines · Chao Yu
Quoted 22 lines · Nikola Z. Ivanov
Current
i_nlinkcorruption check does not take into account directory inodes which have one additionali_nlinkfor their "." entry.Add additional check and a common corruption path.
Reported-by [email protected]Fixes 81edb983b3f5 ("f2fs: add check for deleted inode")Signed-off-by Nikola Z. Ivanov <[email protected]>--- fs/f2fs/namei.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)fs/f2fs/namei.c +1
-1@@ -502,12 +502,14 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, goto out; } -if (inode->i_nlink ==0 ) { +if (unlikely (inode->i_nlink ==0 )) {fs/f2fs/namei.c static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, · line 511
Quoted 10 lines
fs/f2fs/namei.c +5
-3f2fs_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 ); - goto out_iput; + goto corrupted; + }else if (unlikely (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;Can we detect such corruption in
sanity_check_inode()as well? So that if f2fs internal flow callsf2fs_iget()on corrupted inode, we can setSBI_NEED_FSCKflag and then triggering fsck repairment later.Thanks,
Quoted 31 lines
fs/f2fs/namei.c +9
-4} if (IS_ENCRYPTED(dir) &&@@ -533,6 +535,9 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,trace_f2fs_lookup_end (dir, !IS_ERR_OR_NULL (new) ? new : dentry, ino,IS_ERR (new) ?PTR_ERR (new) : err);return new; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); out_iput:iput (inode); out:@@ -572,10 +577,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 fail; + goto corrupted; + }else if (unlikely (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 +607,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))f2fs_sync_fs (sbi->sb,1 ); +Quoted 9 lines
fs/f2fs/namei.c static int f2fs_unlink(struct inode *dir, struct dentry *dentry) · line 618
Quoted 8 lines
fs/f2fs/namei.c +5
-0+ goto fail; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); +f2fs_folio_put (folio, false); fail:trace_f2fs_unlink_exit (inode, err);return err;Quoted 7 lines
Hi Chao,
Thank you for the suggestion.
I will add this tosanity_check_inodeand remove it fromf2fs_lookupas it becomes redundant sincef2fs_lookupobtains the inode throughf2fs_iget. Forf2fs_unlinkI will move thei_nlink== 1 check tof2fs_rmdir.Hi Nikola,
Quoted 8 lines
I meant we can move the
i_nlink== 1 check from bothf2fs_lookup()andf2fs_unlink()tosanity_check_inode(), because before we create in-memory inode, we will always callsanity_check_inode().Let me know if you have other concerns.
Thanks,
The issue here is that sanity_check_inode will be called only when we initially read the inode off disk, not when it's already in the cache
The syzkaller repro does something like this:
Creates a directory structure /dir1/dir2 where dir1 has i_nlink == 2, which is one less than it should. It then does rmdir(/dir1/dir2) followed by rmdir(/dir1) which leads to the warning.
In such case what would you say should happen, should the second rmdir fail and report the corruption, or do we close our eyes and just drop i_nlink to 0 and possibly log a message that something isn't quite right?
Thank you,
Quoted 3 lines
Quoted 2 lines
I will send v2 as soon as I do some more testing.
Chao Yu
maintainerQuoted 103 lines · Nikola Z. Ivanov
Quoted 93 lines · Chao Yu
Quoted 82 lines · Nikola Z. Ivanov
Quoted 71 lines · Chao Yu
Quoted 22 lines · Nikola Z. Ivanov
Current
i_nlinkcorruption check does not take into account directory inodes which have one additionali_nlinkfor their "." entry.Add additional check and a common corruption path.
Reported-by [email protected]Fixes 81edb983b3f5 ("f2fs: add check for deleted inode")Signed-off-by Nikola Z. Ivanov <[email protected]>--- fs/f2fs/namei.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)fs/f2fs/namei.c +1
-1@@ -502,12 +502,14 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, goto out; } -if (inode->i_nlink ==0 ) { +if (unlikely (inode->i_nlink ==0 )) {fs/f2fs/namei.c static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, · line 511
Quoted 10 lines
fs/f2fs/namei.c +5
-3f2fs_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 ); - goto out_iput; + goto corrupted; + }else if (unlikely (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;Can we detect such corruption in
sanity_check_inode()as well? So that if f2fs internal flow callsf2fs_iget()on corrupted inode, we can setSBI_NEED_FSCKflag and then triggering fsck repairment later.Thanks,
Quoted 31 lines
fs/f2fs/namei.c +9
-4} if (IS_ENCRYPTED(dir) &&@@ -533,6 +535,9 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,trace_f2fs_lookup_end (dir, !IS_ERR_OR_NULL (new) ? new : dentry, ino,IS_ERR (new) ?PTR_ERR (new) : err);return new; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); out_iput:iput (inode); out:@@ -572,10 +577,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 fail; + goto corrupted; + }else if (unlikely (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 +607,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))f2fs_sync_fs (sbi->sb,1 ); +Quoted 9 lines
fs/f2fs/namei.c static int f2fs_unlink(struct inode *dir, struct dentry *dentry) · line 618
Quoted 8 lines
fs/f2fs/namei.c +5
-0+ goto fail; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); +f2fs_folio_put (folio, false); fail:trace_f2fs_unlink_exit (inode, err);return err;Quoted 7 lines
Hi Chao,
Thank you for the suggestion.
I will add this tosanity_check_inodeand remove it fromf2fs_lookupas it becomes redundant sincef2fs_lookupobtains the inode throughf2fs_iget. Forf2fs_unlinkI will move thei_nlink== 1 check tof2fs_rmdir.Hi Nikola,
Quoted 8 lines
I meant we can move the
i_nlink== 1 check from bothf2fs_lookup()andf2fs_unlink()tosanity_check_inode(), because before we create in-memory inode, we will always callsanity_check_inode().Let me know if you have other concerns.
Thanks,
Quoted 7 lines
The issue here is that
sanity_check_inodewill be called only when we initially read the inode off disk, not when it's already in the cacheThe syzkaller repro does something like this:
Creates a directory structure /dir1/dir2 where dir1 hasi_nlink== 2, which is one less than it should. It then does rmdir(/dir1/dir2) followed by rmdir(/dir1) which leads to the warning.
Oh, I missed this case.
Quoted 4 lines
In such case what would you say should happen, should the second rmdir fail and report the corruption, or do we close our eyes and just drop
i_nlinkto 0 and possibly log a message that something isn't quite right?
I agreed that we should keep i_nlink == 1 check in f2fs_unlink().
Thanks,
Quoted 6 lines
Thank you,
Quoted 3 lines
Quoted 2 lines
I will send v2 as soon as I do some more testing.
Quoted 115 lines · Chao Yu
Quoted 103 lines · Nikola Z. Ivanov
Quoted 93 lines · Chao Yu
Quoted 82 lines · Nikola Z. Ivanov
Quoted 71 lines · Chao Yu
Quoted 22 lines · Nikola Z. Ivanov
Current
i_nlinkcorruption check does not take into account directory inodes which have one additionali_nlinkfor their "." entry.Add additional check and a common corruption path.
Reported-by [email protected]Fixes 81edb983b3f5 ("f2fs: add check for deleted inode")Signed-off-by Nikola Z. Ivanov <[email protected]>--- fs/f2fs/namei.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)fs/f2fs/namei.c +1
-1@@ -502,12 +502,14 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, goto out; } -if (inode->i_nlink ==0 ) { +if (unlikely (inode->i_nlink ==0 )) {fs/f2fs/namei.c static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, · line 511
Quoted 10 lines
fs/f2fs/namei.c +5
-3f2fs_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 ); - goto out_iput; + goto corrupted; + }else if (unlikely (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;Can we detect such corruption in
sanity_check_inode()as well? So that if f2fs internal flow callsf2fs_iget()on corrupted inode, we can setSBI_NEED_FSCKflag and then triggering fsck repairment later.Thanks,
Quoted 31 lines
fs/f2fs/namei.c +9
-4} if (IS_ENCRYPTED(dir) &&@@ -533,6 +535,9 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,trace_f2fs_lookup_end (dir, !IS_ERR_OR_NULL (new) ? new : dentry, ino,IS_ERR (new) ?PTR_ERR (new) : err);return new; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); out_iput:iput (inode); out:@@ -572,10 +577,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 fail; + goto corrupted; + }else if (unlikely (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 +607,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))f2fs_sync_fs (sbi->sb,1 ); +Quoted 9 lines
fs/f2fs/namei.c static int f2fs_unlink(struct inode *dir, struct dentry *dentry) · line 618
Quoted 8 lines
fs/f2fs/namei.c +5
-0+ goto fail; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); +f2fs_folio_put (folio, false); fail:trace_f2fs_unlink_exit (inode, err);return err;Quoted 7 lines
Hi Chao,
Thank you for the suggestion.
I will add this tosanity_check_inodeand remove it fromf2fs_lookupas it becomes redundant sincef2fs_lookupobtains the inode throughf2fs_iget. Forf2fs_unlinkI will move thei_nlink== 1 check tof2fs_rmdir.Hi Nikola,
Quoted 8 lines
I meant we can move the
i_nlink== 1 check from bothf2fs_lookup()andf2fs_unlink()tosanity_check_inode(), because before we create in-memory inode, we will always callsanity_check_inode().Let me know if you have other concerns.
Thanks,
Quoted 7 lines
The issue here is that
sanity_check_inodewill be called only when we initially read the inode off disk, not when it's already in the cacheThe syzkaller repro does something like this:
Creates a directory structure /dir1/dir2 where dir1 hasi_nlink== 2, which is one less than it should. It then does rmdir(/dir1/dir2) followed by rmdir(/dir1) which leads to the warning.Oh, I missed this case.
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
In such case what would you say should happen, should the second rmdir fail and report the corruption, or do we close our eyes and just drop
i_nlinkto 0 and possibly log a message that something isn't quite right?I agreed that we should keep
i_nlink== 1 check inf2fs_unlink().Thanks,
Hi Chao,
Just to make sure we're on the same page, do you mean to keep the check in f2fs_unlink as well as sanity_check_inode, or only do it in f2fs_unlink?
Quoted 7 lines
Quoted 6 lines
Thank you,
Quoted 3 lines
Quoted 2 lines
I will send v2 as soon as I do some more testing.
Quoted 122 lines · Nikola Z. Ivanov
Quoted 121 lines · Chao Yu
Quoted 103 lines · Nikola Z. Ivanov
Quoted 93 lines · Chao Yu
Quoted 82 lines · Nikola Z. Ivanov
Quoted 71 lines · Chao Yu
Quoted 22 lines · Nikola Z. Ivanov
Current
i_nlinkcorruption check does not take into account directory inodes which have one additionali_nlinkfor their "." entry.Add additional check and a common corruption path.
Reported-by [email protected]Fixes 81edb983b3f5 ("f2fs: add check for deleted inode")Signed-off-by Nikola Z. Ivanov <[email protected]>--- fs/f2fs/namei.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)fs/f2fs/namei.c +1
-1@@ -502,12 +502,14 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, goto out; } -if (inode->i_nlink ==0 ) { +if (unlikely (inode->i_nlink ==0 )) {fs/f2fs/namei.c static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, · line 511
Quoted 10 lines
fs/f2fs/namei.c +5
-3f2fs_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 ); - goto out_iput; + goto corrupted; + }else if (unlikely (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;Can we detect such corruption in
sanity_check_inode()as well? So that if f2fs internal flow callsf2fs_iget()on corrupted inode, we can setSBI_NEED_FSCKflag and then triggering fsck repairment later.Thanks,
Quoted 31 lines
fs/f2fs/namei.c +9
-4} if (IS_ENCRYPTED(dir) &&@@ -533,6 +535,9 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,trace_f2fs_lookup_end (dir, !IS_ERR_OR_NULL (new) ? new : dentry, ino,IS_ERR (new) ?PTR_ERR (new) : err);return new; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); out_iput:iput (inode); out:@@ -572,10 +577,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 fail; + goto corrupted; + }else if (unlikely (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 +607,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))f2fs_sync_fs (sbi->sb,1 ); +Quoted 9 lines
fs/f2fs/namei.c static int f2fs_unlink(struct inode *dir, struct dentry *dentry) · line 617
Quoted 8 lines
fs/f2fs/namei.c +5
-0+ goto fail; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); +f2fs_folio_put (folio, false); fail:trace_f2fs_unlink_exit (inode, err);return err;Quoted 7 lines
Hi Chao,
Thank you for the suggestion.
I will add this tosanity_check_inodeand remove it fromf2fs_lookupas it becomes redundant sincef2fs_lookupobtains the inode throughf2fs_iget. Forf2fs_unlinkI will move thei_nlink== 1 check tof2fs_rmdir.Hi Nikola,
Quoted 8 lines
I meant we can move the
i_nlink== 1 check from bothf2fs_lookup()andf2fs_unlink()tosanity_check_inode(), because before we create in-memory inode, we will always callsanity_check_inode().Let me know if you have other concerns.
Thanks,
Quoted 7 lines
The issue here is that
sanity_check_inodewill be called only when we initially read the inode off disk, not when it's already in the cacheThe syzkaller repro does something like this:
Creates a directory structure /dir1/dir2 where dir1 hasi_nlink== 2, which is one less than it should. It then does rmdir(/dir1/dir2) followed by rmdir(/dir1) which leads to the warning.Oh, I missed this case.
Quoted 4 lines
In such case what would you say should happen, should the second rmdir fail and report the corruption, or do we close our eyes and just drop
i_nlinkto 0 and possibly log a message that something isn't quite right?I agreed that we should keep
i_nlink== 1 check inf2fs_unlink().
Quoted 8 lines
Quoted 2 lines
Thanks,
Hi Chao,
Just to make sure we're on the same page, do you mean to keep the check in
f2fs_unlinkas well assanity_check_inode, or only do it inf2fs_unlink?
Hello,
Kindly sending a reminder here, could you please
provide input when you have the chance?
Thank you!
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Thank you,
Quoted 3 lines
Quoted 2 lines
I will send v2 as soon as I do some more testing.
Chao Yu
maintainerQuoted 128 lines · Nikola Z. Ivanov
Quoted 127 lines · Nikola Z. Ivanov
Quoted 115 lines · Chao Yu
Quoted 103 lines · Nikola Z. Ivanov
Quoted 93 lines · Chao Yu
Quoted 82 lines · Nikola Z. Ivanov
Quoted 71 lines · Chao Yu
Quoted 22 lines · Nikola Z. Ivanov
Current
i_nlinkcorruption check does not take into account directory inodes which have one additionali_nlinkfor their "." entry.Add additional check and a common corruption path.
Reported-by [email protected]Fixes 81edb983b3f5 ("f2fs: add check for deleted inode")Signed-off-by Nikola Z. Ivanov <[email protected]>--- fs/f2fs/namei.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)fs/f2fs/namei.c +1
-1@@ -502,12 +502,14 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, goto out; } -if (inode->i_nlink ==0 ) { +if (unlikely (inode->i_nlink ==0 )) {fs/f2fs/namei.c static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, · line 511
Quoted 10 lines
fs/f2fs/namei.c +5
-3f2fs_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 ); - goto out_iput; + goto corrupted; + }else if (unlikely (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;Can we detect such corruption in
sanity_check_inode()as well? So that if f2fs internal flow callsf2fs_iget()on corrupted inode, we can setSBI_NEED_FSCKflag and then triggering fsck repairment later.Thanks,
Quoted 31 lines
fs/f2fs/namei.c +9
-4} if (IS_ENCRYPTED(dir) &&@@ -533,6 +535,9 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,trace_f2fs_lookup_end (dir, !IS_ERR_OR_NULL (new) ? new : dentry, ino,IS_ERR (new) ?PTR_ERR (new) : err);return new; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); out_iput:iput (inode); out:@@ -572,10 +577,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 fail; + goto corrupted; + }else if (unlikely (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 +607,12 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))f2fs_sync_fs (sbi->sb,1 ); +Quoted 9 lines
fs/f2fs/namei.c static int f2fs_unlink(struct inode *dir, struct dentry *dentry) · line 617
Quoted 8 lines
fs/f2fs/namei.c +5
-0+ goto fail; +corrupted: + err = -EFSCORRUPTED ; +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); +f2fs_folio_put (folio, false); fail:trace_f2fs_unlink_exit (inode, err);return err;Quoted 7 lines
Hi Chao,
Thank you for the suggestion.
I will add this tosanity_check_inodeand remove it fromf2fs_lookupas it becomes redundant sincef2fs_lookupobtains the inode throughf2fs_iget. Forf2fs_unlinkI will move thei_nlink== 1 check tof2fs_rmdir.Hi Nikola,
Quoted 8 lines
I meant we can move the
i_nlink== 1 check from bothf2fs_lookup()andf2fs_unlink()tosanity_check_inode(), because before we create in-memory inode, we will always callsanity_check_inode().Let me know if you have other concerns.
Thanks,
Quoted 7 lines
The issue here is that
sanity_check_inodewill be called only when we initially read the inode off disk, not when it's already in the cacheThe syzkaller repro does something like this:
Creates a directory structure /dir1/dir2 where dir1 hasi_nlink== 2, which is one less than it should. It then does rmdir(/dir1/dir2) followed by rmdir(/dir1) which leads to the warning.Oh, I missed this case.
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
In such case what would you say should happen, should the second rmdir fail and report the corruption, or do we close our eyes and just drop
i_nlinkto 0 and possibly log a message that something isn't quite right?I agreed that we should keep
i_nlink== 1 check inf2fs_unlink().Thanks,
Hi Chao,
Quoted 8 lines
Quoted 3 lines
Just to make sure we're on the same page, do you mean to keep the check in
f2fs_unlinkas well assanity_check_inode, or only do it inf2fs_unlink?Hello,
Kindly sending a reminder here, could you please
provide input when you have the chance?
Hi Nikola,
Sorry for the delay, I missed to reply.
I meant that we can add sanity check (i_nlink == 1) for directory in:
- f2fs_unlink(), so that, we can detect runtime i_nlink inconsistency.
- sanity_check_inode(), then, we can detect on-disk i_nlink inconsistency.
Thanks,
Quoted 10 lines
Thank you!
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Thank you,
Quoted 3 lines
Quoted 2 lines
I will send v2 as soon as I do some more testing.