Skip to contentSkip to conversation

btrfs-progs: check/original: detect directory inode with nlinks >= 2

PATCH In review Qu Wenruo 5 participants 6 messages Jul 18, 2021 – Jul 30, 2021 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/2 btrfs-progs: check/original: detect directory inode with nlinks >= 2

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.

---
 check/main.c          | 7 +++++++
 check/mode-original.h | 1 +
 2 files changed, 8 insertions(+)
check/main.c +7 -0
index ee6cf793251c..df2303939ffe 100644
@@ -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
check/mode-original.h +1 -0
index b075a95c9757..eed16d92d0db 100644
@@ -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;
Signature · 1 line
2.32.0
author
#1 lore raw

PATCH 2/2 btrfs-progs: fsck-test: add test case where one dir has two links

Make sure btrfs check can detect such problem.

Right now we have no way to fix it yet.

---
 .../049-dir-hard-link/default.img.xz          | Bin 0 -> 1712 bytes
 tests/fsck-tests/049-dir-hard-link/test.sh    |  19 ++++++++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 tests/fsck-tests/049-dir-hard-link/default.img.xz
 create mode 100755 tests/fsck-tests/049-dir-hard-link/test.sh
tests/fsck-tests/049-dir-hard-link/default.img.xz new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..fd9971acae78975f5263da022feea39e34b45139
GIT binary patch
tests/fsck-tests/049-dir-hard-link/test.sh new file +19 -0
new file mode 100755
index 000000000000..992ad638bd47
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# Verify that check can detect overlapped dir with 2 links
+#
+# There is a report that btrfs-check doesn't report dir with 2 links as
+# error, and only get caught by tree-checker.
+#
+# Make sure btrfs check can at least detect such error
+
+source "$TEST_TOP/common"
+
+check_prereq btrfs
+
+check_image() {
+	run_mustfail "dir with 2 links not detected" \
+		"$TOP/btrfs" check "$1"
+}
+
+check_all_images
Signature · 1 line
2.32.0
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] btrfs-progs: fsck-test: add test case where one dir has two links

Quoted 5 lines · Qu Wenruo

Make sure btrfs check can detect such problem.

Right now we have no way to fix it yet.

Test case runs fine. Do you use any tool to create the crafted image?

Thanks, Anand

Quoted 76 lines
---
  .../049-dir-hard-link/default.img.xz          | Bin 0 -> 1712 bytes
  tests/fsck-tests/049-dir-hard-link/test.sh    |  19 ++++++++++++++++++
  2 files changed, 19 insertions(+)

create mode 100644 tests/fsck-tests/049-dir-hard-link/default.img.xz create mode 100755 tests/fsck-tests/049-dir-hard-link/test.sh

tests/fsck-tests/049-dir-hard-link/default.img.xz new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..fd9971acae78975f5263da022feea39e34b45139
GIT binary patch
tests/fsck-tests/049-dir-hard-link/test.sh new file +19 -0
new file mode 100755
index 000000000000..992ad638bd47
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# Verify that check can detect overlapped dir with 2 links
+#
+# There is a report that btrfs-check doesn't report dir with 2 links as
+# error, and only get caught by tree-checker.
+#
+# Make sure btrfs check can at least detect such error
+
+source "$TEST_TOP/common"
+
+check_prereq btrfs
+
+check_image() {
+	run_mustfail "dir with 2 links not detected" \
+		"$TOP/btrfs" check "$1"
+}
+
+check_all_images
#3 lore raw

Re: [PATCH 2/2] btrfs-progs: fsck-test: add test case where one dir has two links

Quoted 9 lines · Anand Jain
Quoted 5 lines · Qu Wenruo

Make sure btrfs check can detect such problem.

Right now we have no way to fix it yet.

Test case runs fine. Do you use any tool to create the crafted image?

As usual, just hard coded btrfs-corrupt-block to call btrfs_add_link() for a directory.

Thus there isn't really a good tool to craft such image.

Thanks,
Qu

Quoted 86 lines

Thanks, Anand

Quoted 80 lines

---
  .../049-dir-hard-link/default.img.xz          | Bin 0 -> 1712 bytes tests/fsck-tests/049-dir-hard-link/test.sh    |  19 ++++++++++++++++++

  2 files changed, 19 insertions(+)

  create mode 100644 tests/fsck-tests/049-dir-hard-link/default.img.xz create mode 100755 tests/fsck-tests/049-dir-hard-link/test.sh

tests/fsck-tests/049-dir-hard-link/default.img.xz new file +0 -0
new file mode 100644

index 0000000000000000000000000000000000000000..fd9971acae78975f5263da022feea39e34b45139

GIT binary patch
literal 1712 zcmV;h22c6@H+ooF000E$*0e?f03iV!0000G&sfah3;zaiT>wRyj;C3^v%$$4d1lq4 zhDvnFHzu^a0%EHA+fjUfU^d-f8FD79nuh5qo92GkFUET_5R5Y~d)afdfW4sb2eW+N z(-N9N;@zX$?eR^yQlFZnSsfYzwplHEI#}i{O3&^g-%*#*J}6=@&Mr933Z)S+DYvtb z5k;9l__I-yJAS&v0F{j8)>U_CUcN9uhMMZ>r0aFa$nT3PO%||kR#<>b&X;^jZx! z?T$2In^=Ho4dSN<1OU-V{iJKl4iT0qZ?qN2!NSU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_!f!HtAShXA6eHuXWK0PVh5TX(wgYr!l9e|jW&M_)<e3^X<w4M zY_P_kf=8+|nGi>DRcZBVfPyNr$65P*t{sM-vjLXqP~HHY0FV{L*~3B=dzP+MOU1@ zRYWfI#vm2^g>N$aie=a^L(xQu@pRjw4DXt%wU7m~@-}@QvcEN^UEJ@e6Xm8m=^ZRq z)cUF@zR;E|i)7w)u*a&WXIzpygnt3|17G^sPOyW0?V~!r7SasXdubQAKKtR^|uf z4;4WRHG()C2~#M*GC^l@k>w3^5M{@6>W%dgSU>Zqn+vNd*2f><3(CVuqAnX=(Zu(G zF(j4;!MKu6~PVFgQz$=jt#O~3U4TtOFg;!(X?=T=jRzhT3yzSXD>1GA8~FpzJVnt zUmIYu;aQe?QMT(4UkP16u@m}5@AX6}DPA>a!#dt3qbX_CfS5)G-*MI@B#;(Ap1{; z3VE<Z0{u?EC{{mUj$DSQ|Lv6DL62yckCVEF6WgD4l%e+sw}4(qMD4Y=X-!VdYJo{ zKb+fu*f8Tk!0fZzzcR|8Tl0H%R#9A&@3h+YzekzO#*X*=^j()i-pyn&%m-Ln_2H! z&+nI1M9}8)2($&79_I@LdA5md{Ue@-$tE1%0OeJZ{UaXg=~jG>C#T>wd^;QzL0K(} z6tjZD62IVa<@fK+TssaPJ~B{HR=bR5r$9ZK8P2Nah9n(3W~$(5piy!QO^Al)?l4Q z5~x}6Pex_Yr=?Q*&mMGj7l@I6&y+9_H<1QmoCJTx0Y6JJmE9n=>iO#ToI;6F9dI) z!d^0KXk;SSyDf^@yN)Q-8qxAM^Z)=ciSQA~y}h;o0gMfR7ytk~fZMIH#Ao{g00000 G1X)_MbXXt&

literal 0
HcmV?d00001

tests/fsck-tests/049-dir-hard-link/test.sh new file +19 -0
new file mode 100755
index 000000000000..992ad638bd47
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# Verify that check can detect overlapped dir with 2 links
+#
+# There is a report that btrfs-check doesn't report dir with 2 links as
+# error, and only get caught by tree-checker.
+#
+# Make sure btrfs check can at least detect such error
+
+source "$TEST_TOP/common"
+
+check_prereq btrfs
+
+check_image() {
+    run_mustfail "dir with 2 links not detected" \
+        "$TOP/btrfs" check "$1"
+}
+
+check_all_images
Reviewed-by on v1 1/2
#4 lore raw
In reply to Qu Wenruo
Quoted 10 lines · Qu Wenruo

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.

--
Su

Quoted 44 lines
---
 check/main.c          | 7 +++++++
 check/mode-original.h | 1 +
 2 files changed, 8 insertions(+)
check/main.c +3 -0
index ee6cf793251c..df2303939ffe 100644
@@ -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");
 
check/main.c +4 -0
 	/* 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
check/main.c +0 -0
 	 * inode generation uplimit, use super_generation + 1 anyway
check/mode-original.h +1 -0
index b075a95c9757..eed16d92d0db 100644
@@ -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;
#5 lore raw
In reply to Qu Wenruo
Quoted 9 lines · Qu Wenruo

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.

Added to devel, thanks.