linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] btrfs-progs: check/original: detect directory inode with nlinks >= 2
@ 2021-07-18 12:54 Qu Wenruo
  2021-07-18 12:54 ` [PATCH 2/2] btrfs-progs: fsck-test: add test case where one dir has two links Qu Wenruo
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Qu Wenruo @ 2021-07-18 12:54 UTC (permalink / raw)
  To: linux-btrfs

Linux VFS doesn't allow directory to have hard links, thus for btrfs
on-disk directory inode items, their nlinks should never go beyond 1.

Lowmem mode already has the check and will report it without problem.
Only original mode needs this update.

Reported-by: Pepperpoint <pepperpoint@mb.ardentcoding.com>
Link: https://lore.kernel.org/linux-btrfs/162648632340.7.1932907459648384384.10178178@mb.ardentcoding.com/
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 check/main.c          | 7 +++++++
 check/mode-original.h | 1 +
 2 files changed, 8 insertions(+)

diff --git a/check/main.c b/check/main.c
index ee6cf793251c..df2303939ffe 100644
--- a/check/main.c
+++ b/check/main.c
@@ -623,6 +623,9 @@ static void print_inode_error(struct btrfs_root *root, struct inode_record *rec)
 			rec->imode & ~07777);
 	if (errors & I_ERR_INVALID_GEN)
 		fprintf(stderr, ", invalid inode generation or transid");
+	if (errors & I_ERR_INVALID_NLINK)
+		fprintf(stderr, ", directory has invalid nlink %d",
+			rec->nlink);
 	fprintf(stderr, "\n");
 
 	/* Print the holes if needed */
@@ -909,6 +912,10 @@ static int process_inode_item(struct extent_buffer *eb,
 	if (S_ISLNK(rec->imode) &&
 	    flags & (BTRFS_INODE_IMMUTABLE | BTRFS_INODE_APPEND))
 		rec->errors |= I_ERR_ODD_INODE_FLAGS;
+
+	/* Directory should never have hard link */
+	if (S_ISDIR(rec->imode) && rec->nlink >= 2)
+		rec->errors |= I_ERR_INVALID_NLINK;
 	/*
 	 * We don't have accurate root info to determine the correct
 	 * inode generation uplimit, use super_generation + 1 anyway
diff --git a/check/mode-original.h b/check/mode-original.h
index b075a95c9757..eed16d92d0db 100644
--- a/check/mode-original.h
+++ b/check/mode-original.h
@@ -186,6 +186,7 @@ struct unaligned_extent_rec_t {
 #define I_ERR_MISMATCH_DIR_HASH		(1 << 18)
 #define I_ERR_INVALID_IMODE		(1 << 19)
 #define I_ERR_INVALID_GEN		(1 << 20)
+#define I_ERR_INVALID_NLINK		(1 << 21)
 
 struct inode_record {
 	struct list_head backrefs;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-07-30 13:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-18 12:54 [PATCH 1/2] btrfs-progs: check/original: detect directory inode with nlinks >= 2 Qu Wenruo
2021-07-18 12:54 ` [PATCH 2/2] btrfs-progs: fsck-test: add test case where one dir has two links Qu Wenruo
2021-07-19  9:57   ` Anand Jain
2021-07-19 10:25     ` Qu Wenruo
2021-07-19  0:39 ` [PATCH 1/2] btrfs-progs: check/original: detect directory inode with nlinks >= 2 Su Yue
2021-07-30 13:54 ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).