2023-10-18 10:09:10

by Cheng Lin

[permalink] [raw]
Subject: [RFC PATCH v2] fs: introduce check for inode nlink when delete

From: Cheng Lin <[email protected]>

Do not delete inode which nlink already zero to avoid
inode nlink underflow.

Signed-off-by: Cheng Lin <[email protected]>
---
fs/namei.c | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/fs/namei.c b/fs/namei.c
index e56ff39a7..30bc0d0a6 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -4197,6 +4197,11 @@ int vfs_rmdir(struct mnt_idmap *idmap, struct inode *dir,
if (error)
goto out;

+ if (unlikely(dir->i_nlink == 0) ||
+ unlikely(dentry->d_inode->i_nlink == 0)) {
+ error = -EUCLEAN;
+ goto out;
+ }
error = dir->i_op->rmdir(dir, dentry);
if (error)
goto out;
@@ -4326,6 +4331,10 @@ int vfs_unlink(struct mnt_idmap *idmap, struct inode *dir,
error = try_break_deleg(target, delegated_inode);
if (error)
goto out;
+ if (unlikely(target->i_nlink == 0)) {
+ error = -EUCLEAN;
+ goto out;
+ }
error = dir->i_op->unlink(dir, dentry);
if (!error) {
dont_mount(dentry);
--
2.18.1