Skip to contentSkip to conversation

nilfs2: fix link count underflow when removing corrupted directory

PATCH Applied Ryusuke Konishi 2 participants 2 messages Oct 6 NILFS2 filesystem

lore
Share & tools
lore

Ryusuke Konishi

author maintainer
#0 lore raw

PATCH 1/1 nilfs2: fix link count underflow when removing corrupted directory

When removing an empty directory with a corrupted link count (e.g., i_nlink is 1 due to filesystem corruption), nilfs_rmdir() calls drop_nlink() via nilfs_do_unlink() and then calls drop_nlink() again on the directory inode. This second call causes an underflow of i_nlink, triggering a kernel WARNING in drop_nlink().

Fix this issue by using clear_nlink() instead of drop_nlink() to finalize the link count reset for the target directory in nilfs_rmdir(). Additionally, log a warning message when i_nlink is not 2 before removing an empty directory to notify about the link count inconsistency.

Reported-by [email protected]
Fixes d25006523d0b ("nilfs2: pathname operations")

---
Hi Viacheslav,

Please apply this for the next cycle.

This fixes a link count underflow during rmdir on a directory with a corrupted link count (an issue recently reported by syzbot).

Thanks,
Ryusuke Konishi

 fs/nilfs2/namei.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/nilfs2/namei.c +7 -1
index e037e0c6e31a..d326bb1acb11 100644
@@ -341,10 +341,16 @@ static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
 
 	err = -ENOTEMPTY;
 	if (nilfs_empty_dir(inode)) {
+		if (unlikely(inode->i_nlink != 2))
+			nilfs_warn(dir->i_sb,
+				"inconsistent empty directory link count %u (ino=%llu)",
+				inode->i_nlink,
+				(unsigned long long)inode->i_ino);
+
 		err = nilfs_do_unlink(dir, dentry);
 		if (!err) {
 			inode->i_size = 0;
-			drop_nlink(inode);
+			clear_nlink(inode);
 			nilfs_mark_inode_dirty(inode);
 			drop_nlink(dir);
 			nilfs_mark_inode_dirty(dir);
Signature · 1 line
2.53.0
maintainer Applied
#1 lore raw
Quoted 52 lines · Ryusuke Konishi

When removing an empty directory with a corrupted link count (e.g., i_nlink is 1 due to filesystem corruption), nilfs_rmdir() calls drop_nlink() via nilfs_do_unlink() and then calls drop_nlink() again on the directory inode.  This second call causes an underflow of i_nlink, triggering a kernel WARNING in drop_nlink().

Fix this issue by using clear_nlink() instead of drop_nlink() to finalize the link count reset for the target directory in nilfs_rmdir().
Additionally, log a warning message when i_nlink is not 2 before removing an empty directory to notify about the link count inconsistency.

Reported-by [email protected]
Fixes d25006523d0b ("nilfs2: pathname operations")

---
Hi Viacheslav,

Please apply this for the next cycle.

This fixes a link count underflow during rmdir on a directory with a corrupted link count (an issue recently reported by syzbot).

Thanks,
Ryusuke Konishi

 fs/nilfs2/namei.c | 8 +++++++-

 1 file changed, 7 insertions(+), 1 deletion(-)
fs/nilfs2/namei.c +0 -0
index e037e0c6e31a..d326bb1acb11 100644
@@ -341,10 +341,16 @@ static int nilfs_rmdir(struct inode *dir,
 struct dentry *dentry)


  err = -ENOTEMPTY;
  if (nilfs_empty_dir(inode)) {

fs/nilfs2/namei.c +3 -0
+		if (unlikely(inode->i_nlink != 2))
+			nilfs_warn(dir->i_sb,
+				"inconsistent empty directory link

count %u (ino=%llu)",

fs/nilfs2/namei.c +3 -0
+				inode->i_nlink,
+				(unsigned long long)inode->i_ino);
+

  err = nilfs_do_unlink(dir, dentry); if (!err) {
  inode->i_size = 0;

fs/nilfs2/namei.c +1 -1
-			drop_nlink(inode);
+			clear_nlink(inode);
Quoted 3 lines

  nilfs_mark_inode_dirty(inode); drop_nlink(dir); nilfs_mark_inode_dirty(dir);

Applied.

Thanks,
Slava.