Skip to contentSkip to conversation

btrfs: add a test case to check btrfs won't crash on certain corruption

PATCH In review Qu Wenruo 2 participants 2 messages Feb 25, 2023 – May 9, 2023 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs: add a test case to check btrfs won't crash on certain corruption

There seems to be a newly introduced regression that btrfs no longer properly handles critical errors during mount.

Such regression lead to crash when mounting an fs with a corrupted tree root.

The test case would reproduce the situation by creating an empty fs, with SINGLE metadata profile, then corrupt the tree root manually. Finally try mounting the corrupted fs, the mount should fail while our kernel should not fail.

---
The fix is titled "btrfs: fix the mount crash caused by confusing return value", and is already submitted to the btrfs list.

Unfortunately git blame doesn't give a good enough clue on which commit introduced the regression.

---
 tests/btrfs/288     | 37 +++++++++++++++++++++++++++++++++++++
 tests/btrfs/288.out |  2 ++
 2 files changed, 39 insertions(+)
 create mode 100755 tests/btrfs/288
 create mode 100644 tests/btrfs/288.out
tests/btrfs/288 new file +37 -0
new file mode 100755
index 00000000..029603c8
@@ -0,0 +1,37 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2023 SUSE Linux Products GmbH. All Rights Reserved.
+#
+# FS QA Test 288
+#
+# Make sure btrfs handles critical errors gracefully during mount.
+#
+. ./common/preamble
+_begin_fstest auto quick dangerous
+
+. ./common/filter
+_supported_fs btrfs
+_require_scratch
+
+# Use single metadata profile so we only need to corrupt one copy of tree block
+_scratch_mkfs -m single > $seqres.full
+
+logical_root=$($BTRFS_UTIL_PROG inspect dump-tree -t root "$SCRATCH_DEV" | grep leaf | head -n1 | cut -f2 -d\  )
+physical_root=$(_btrfs_get_physical $logical_root 1)
+
+echo "tree root logical=$logical_root" >> $seqres.full
+echo "tree root physical=$physical_root" >> $seqres.full
+
+_pwrite_byte 0x00 "$physical_root" 4 $SCRATCH_DEV > $seqres.full
+
+# For unpatched kernel, the mount may lead to crash
+_try_scratch_mount >> $seqres.full 2>&1
+
+echo "Silence is golden"
+
+# Re-create the fs to avoid false alert from the corrupted fs.
+_scratch_mkfs -m single > $seqres.full
+
+# success, all done
+status=0
+exit
tests/btrfs/288.out new file +2 -0
new file mode 100644
index 00000000..2958a5c3
@@ -0,0 +1,2 @@
+QA output created by 288
+Silence is golden
Signature · 1 line
2.39.0
#1 lore raw

Added to local branch with the following changed. Under testing.

Quoted 6 lines · Qu Wenruo

There seems to be a newly introduced regression that btrfs no longer properly handles critical errors during mount.

Such regression lead to crash when mounting an fs with a corrupted tree root.

We can now remove this, as we have identified and resolved a regression in a local branch. The fix went into

[PATCH] btrfs: move all btree initialization into btrfs_init_btree_inode

Quoted 36 lines

The test case would reproduce the situation by creating an empty fs, with SINGLE metadata profile, then corrupt the tree root manually. Finally try mounting the corrupted fs, the mount should fail while our kernel should not fail.

---
The fix is titled "btrfs: fix the mount crash caused by confusing return value", and is already submitted to the btrfs list.

Unfortunately git blame doesn't give a good enough clue on which commit introduced the regression.

---
  tests/btrfs/288     | 37 +++++++++++++++++++++++++++++++++++++
  tests/btrfs/288.out |  2 ++
  2 files changed, 39 insertions(+)

create mode 100755 tests/btrfs/288
create mode 100644 tests/btrfs/288.out

tests/btrfs/288 new file +10 -0
new file mode 100755
index 00000000..029603c8
@@ -0,0 +1,37 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2023 SUSE Linux Products GmbH. All Rights Reserved.
+#
+# FS QA Test 288
+#
+# Make sure btrfs handles critical errors gracefully during mount.
+#
+. ./common/preamble
+_begin_fstest auto quick dangerous

tests/btrfs/288 line 19

Quoted 10 lines
tests/btrfs/288 new file +9 -0
+
+. ./common/filter
+_supported_fs btrfs
+_require_scratch
+
+# Use single metadata profile so we only need to corrupt one copy of tree block
+_scratch_mkfs -m single > $seqres.full
+
 
+logical_root=$($BTRFS_UTIL_PROG inspect dump-tree -t root "$SCRATCH_DEV" | grep leaf | head -n1 | cut -f2 -d\  )

Fixed 80-char length.

tests/btrfs/288 line 25

Quoted 6 lines
tests/btrfs/288 new file +6 -0
+physical_root=$(_btrfs_get_physical $logical_root 1)
+
+echo "tree root logical=$logical_root" >> $seqres.full
+echo "tree root physical=$physical_root" >> $seqres.full
+
+_pwrite_byte 0x00 "$physical_root" 4 $SCRATCH_DEV > $seqres.full

Append $seqres.full

tests/btrfs/288

Quoted 2 lines
tests/btrfs/288 new file +2 -0
+
+# For unpatched kernel, the mount may lead to crash

Fix comment.

tests/btrfs/288 line 33

Quoted 6 lines
tests/btrfs/288 new file +6 -0
+_try_scratch_mount >> $seqres.full 2>&1
+
+echo "Silence is golden"
+
+# Re-create the fs to avoid false alert from the corrupted fs.
+_scratch_mkfs -m single > $seqres.full

Append $seqres.full

Thanks, Anand

Quoted 12 lines
tests/btrfs/288 new file +4 -0
+
+# success, all done
+status=0
+exit
tests/btrfs/288.out new file +2 -0
new file mode 100644
index 00000000..2958a5c3
@@ -0,0 +1,2 @@
+QA output created by 288
+Silence is golden