Skip to contentSkip to conversation

fs: introduce check for drop/inc_nlink

RFC PATCH In review [email protected] 3 participants 5 messages Oct 13, 2023 – Oct 17, 2023 VFS

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 fs: introduce check for drop/inc_nlink

From: Cheng Lin <[email protected]>

Avoid inode nlink overflow or underflow.

---
 fs/inode.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/inode.c +6 -0
index 67611a360..8e6d62dc4 100644
@@ -328,6 +328,9 @@ static void destroy_inode(struct inode *inode)
 void drop_nlink(struct inode *inode)
 {
 	WARN_ON(inode->i_nlink == 0);
+	if (unlikely(inode->i_nlink == 0))
+		return;
+
 	inode->__i_nlink--;
 	if (!inode->i_nlink)
 		atomic_long_inc(&inode->i_sb->s_remove_count);
@@ -388,6 +391,9 @@ void inc_nlink(struct inode *inode)
 		atomic_long_dec(&inode->i_sb->s_remove_count);
 	}
 
+	if (unlikely(inode->i_nlink == ~0U))
+		return;
+
 	inode->__i_nlink++;
 }
 EXPORT_SYMBOL(inc_nlink);
Signature · 1 line
2.18.1
#1 lore raw
Quoted 6 lines · On Fri, Oct 13, 2023 at 03:27:30PM +0800, [email protected] wrote:

From: Cheng Lin <[email protected]>

Avoid inode nlink overflow or underflow.

---

I'm very confused. There's no explanation why that's needed. As it stands it's not possible to provide a useful review.

I'm not saying it's wrong. I just don't understand why and even if this should please show up in the commit message.

Quoted 29 lines
 fs/inode.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/inode.c +6 -0
index 67611a360..8e6d62dc4 100644
@@ -328,6 +328,9 @@ static void destroy_inode(struct inode *inode)
 void drop_nlink(struct inode *inode)
 {
 	WARN_ON(inode->i_nlink == 0);
+	if (unlikely(inode->i_nlink == 0))
+		return;
+
 	inode->__i_nlink--;
 	if (!inode->i_nlink)
 		atomic_long_inc(&inode->i_sb->s_remove_count);
@@ -388,6 +391,9 @@ void inc_nlink(struct inode *inode)
 		atomic_long_dec(&inode->i_sb->s_remove_count);
 	}
 
+	if (unlikely(inode->i_nlink == ~0U))
+		return;
+
 	inode->__i_nlink++;
 }
 EXPORT_SYMBOL(inc_nlink);
Signature · 1 line
2.18.1
#2 lore raw
Quoted 11 lines · On Fri, Oct 13, 2023 at 03:27:30PM +0800, [email protected] wrote:
Quoted 6 lines

From: Cheng Lin <[email protected]>

Avoid inode nlink overflow or underflow.

---

I'm very confused. There's no explanation why that's needed. As it stands it's not possible to provide a useful review.
I'm not saying it's wrong. I just don't understand why and even if this should please show up in the commit message.

In an xfs issue, there was an nlink underflow of a directory inode. There is a key information in the kernel messages, that is the WARN_ON from drop_nlink(). However, VFS did not prevent the underflow. I'm not sure if this behavior is inadvertent or specifically designed. As an abnormal situation, perhaps prohibiting nlink overflow or underflow is a better way to handle it.
Request for your comment.

#3 lore raw
Quoted 11 lines · On Fri, Oct 13, 2023 at 05:40:57PM +0800, [email protected] wrote:
Quoted 11 lines · On Fri, Oct 13, 2023 at 03:27:30PM +0800, [email protected] wrote:
Quoted 6 lines

From: Cheng Lin <[email protected]>

Avoid inode nlink overflow or underflow.

---

I'm very confused. There's no explanation why that's needed. As it stands it's not possible to provide a useful review.
I'm not saying it's wrong. I just don't understand why and even if this should please show up in the commit message.

Quoted 7 lines

In an xfs issue, there was an nlink underflow of a directory inode. There is a key information in the kernel messages, that is the WARN_ON from drop_nlink(). However, VFS did not prevent the underflow. I'm not sure if this behavior is inadvertent or specifically designed. As an abnormal situation, perhaps prohibiting nlink overflow or underflow is a better way to handle it.
Request for your comment.

I was trying to steer you towards modifying vfs_rmdir and vfs_unlink to check that i_nlink of the files involved aren't somehow already zero and returning -EFSCORRUPTED if they are. Not messing around with drop_nlink.

--D

#4 lore raw
Quoted 19 lines · On Fri, Oct 13, 2023 at 05:40:57PM +0800, [email protected] wrote:
Quoted 11 lines
Quoted 11 lines · On Fri, Oct 13, 2023 at 03:27:30PM +0800, [email protected] wrote:
Quoted 6 lines

From: Cheng Lin <[email protected]>

Avoid inode nlink overflow or underflow.

---

I'm very confused. There's no explanation why that's needed. As it stands it's not possible to provide a useful review.
I'm not saying it's wrong. I just don't understand why and even if this should please show up in the commit message.

Quoted 7 lines

In an xfs issue, there was an nlink underflow of a directory inode. There is a key information in the kernel messages, that is the WARN_ON from drop_nlink(). However, VFS did not prevent the underflow. I'm not sure if this behavior is inadvertent or specifically designed. As an abnormal situation, perhaps prohibiting nlink overflow or underflow is a better way to handle it.
Request for your comment.

Quoted 5 lines

I was trying to steer you towards modifying vfs_rmdir and vfs_unlink to check that i_nlink of the files involved aren't somehow already zero and returning -EFSCORRUPTED if they are. Not messing around with drop_nlink.
--D

It seems that VFS is not very concerned about the values of filesystem, such as inode nlinks. And the defination of whether it is correct or incorrect is left to the specific filesystem. However, in some places do have limit up to s_max_links. Perhaps the limit down to zero is also reasonable. Is that right?