The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: <cheng.lin130@zte.com.cn>
To: <brauner@kernel.org>, <viro@zeniv.linux.org.uk>, <djwong@kernel.org>
Cc: <linux-fsdevel@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<david@fromorbit.com>, <hch@infradead.org>,
	<jiang.yong5@zte.com.cn>, <wang.liang82@zte.com.cn>,
	<liu.dong3@zte.com.cn>
Subject: [RFC PATCH v2] fs: introduce check for inode nlink when delete
Date: Wed, 18 Oct 2023 18:08:45 +0800 (CST)	[thread overview]
Message-ID: <202310181808454230330@zte.com.cn> (raw)

From: Cheng Lin <cheng.lin130@zte.com.cn>

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

Signed-off-by: Cheng Lin <cheng.lin130@zte.com.cn>
---
 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

                 reply	other threads:[~2023-10-18 10:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=202310181808454230330@zte.com.cn \
    --to=cheng.lin130@zte.com.cn \
    --cc=brauner@kernel.org \
    --cc=david@fromorbit.com \
    --cc=djwong@kernel.org \
    --cc=hch@infradead.org \
    --cc=jiang.yong5@zte.com.cn \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liu.dong3@zte.com.cn \
    --cc=viro@zeniv.linux.org.uk \
    --cc=wang.liang82@zte.com.cn \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox