Skip to contentSkip to conversation

fstests: btrfs: add a new test case for fstrim crash

PATCH Superseded Qu Wenruo 2 participants 2 messages Jun 2 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 fstests: btrfs: add a new test case for fstrim crash

There is a bug report that btrfs/242 can crash very randomly.

It turns out the crash is caused by a re-appearing missing device, which screwed up several checks in fstrim.

This new test case is a more reliable version, dedicated for such crash, which will:

- Create a raid1 btrfs
- Mount with a single device in degraded mode
- Let the missing device to re-appear
- Run fstrim

Which is reliably crashing in my environment without the submitted fix. And with the submitted fix, the test no longer crashes.

---
 tests/btrfs/350     | 38 ++++++++++++++++++++++++++++++++++++++
 tests/btrfs/350.out |  2 ++
 2 files changed, 40 insertions(+)
 create mode 100755 tests/btrfs/350
 create mode 100644 tests/btrfs/350.out
tests/btrfs/350 new file +38 -0
new file mode 100755
index 00000000..1206d66f
@@ -0,0 +1,38 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 SUSE S.A.  All Rights Reserved.
+#
+# FS QA Test 350
+#
+# Make sure fstrim will not crash when a missing device re-appeared.
+#
+. ./common/preamble
+_begin_fstest auto quick volume trim raid
+
+_fixed_by_kernel_commit XXXXXXXXXXXX \
+	"btrfs: do not trim a device which is not writeable"
+
+_require_btrfs_forget_or_module_loadable
+_require_scratch_dev_pool 2
+_scratch_dev_pool_get 2
+dev1=$(echo $SCRATCH_DEV_POOL | $AWK_PROG '{ print $1 }')
+dev2=$(echo $SCRATCH_DEV_POOL | $AWK_PROG '{ print $2 }')
+
+_scratch_pool_mkfs "-m raid1 -d raid1"
+_require_batched_discard $SCRATCH_MNT
+
+# Mount the filesystem in degraded mode
+_btrfs_forget_or_module_reload
+_mount -o degraded $dev1 $SCRATCH_MNT
+
+# Now let devid 2 reappear
+_btrfs device scan $dev2
+
+# Run fstrim, it should skip the re-appeared device, as it still
+# lacks WRITEBALE flag nor a proper bdev pointer.
+#
+# For unpatched kernel, it will crash with a NULL pointer dereference.
+$FSTRIM_PROG $SCRATCH_MNT
+
+echo "Silence is golden"
+_exit 0
tests/btrfs/350.out new file +2 -0
new file mode 100644
index 00000000..e9aad823
@@ -0,0 +1,2 @@
+QA output created by 350
+Silence is golden
Signature · 1 line
2.51.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 55 lines · Qu Wenruo

There is a bug report that btrfs/242 can crash very randomly.

It turns out the crash is caused by a re-appearing missing device, which screwed up several checks in fstrim.

This new test case is a more reliable version, dedicated for such crash, which will:

- Create a raid1 btrfs
- Mount with a single device in degraded mode
- Let the missing device to re-appear
- Run fstrim

Which is reliably crashing in my environment without the submitted fix. And with the submitted fix, the test no longer crashes.

---
 tests/btrfs/350     | 38 ++++++++++++++++++++++++++++++++++++++
 tests/btrfs/350.out |  2 ++
 2 files changed, 40 insertions(+)
 create mode 100755 tests/btrfs/350
 create mode 100644 tests/btrfs/350.out
tests/btrfs/350 new file +22 -0
new file mode 100755
index 00000000..1206d66f
@@ -0,0 +1,38 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 SUSE S.A.  All Rights Reserved.
+#
+# FS QA Test 350
+#
+# Make sure fstrim will not crash when a missing device re-appeared.
+#
+. ./common/preamble
+_begin_fstest auto quick volume trim raid
+
+_fixed_by_kernel_commit XXXXXXXXXXXX \
+       "btrfs: do not trim a device which is not writeable"
+
+_require_btrfs_forget_or_module_loadable
+_require_scratch_dev_pool 2
+_scratch_dev_pool_get 2
+dev1=$(echo $SCRATCH_DEV_POOL | $AWK_PROG '{ print $1 }')
+dev2=$(echo $SCRATCH_DEV_POOL | $AWK_PROG '{ print $2 }')
+
+_scratch_pool_mkfs "-m raid1 -d raid1"
+_require_batched_discard $SCRATCH_MNT

tests/btrfs/350 line 32

Quoted 10 lines
tests/btrfs/350 new file +10 -0
+
+# Mount the filesystem in degraded mode
+_btrfs_forget_or_module_reload
+_mount -o degraded $dev1 $SCRATCH_MNT
+
+# Now let devid 2 reappear
+_btrfs device scan $dev2
+
+# Run fstrim, it should skip the re-appeared device, as it still
+# lacks WRITEBALE flag nor a proper bdev pointer.

nor -> and

tests/btrfs/350 line 35

Quoted 3 lines
tests/btrfs/350 new file +3 -0
+#
+# For unpatched kernel, it will crash with a NULL pointer dereference.
+$FSTRIM_PROG $SCRATCH_MNT

Misses a call to: _scratch_dev_pool_put

Otherwise:

Thanks.

Quoted 15 lines
tests/btrfs/350 new file +3 -0
+
+echo "Silence is golden"
+_exit 0
tests/btrfs/350.out new file +2 -1
new file mode 100644
index 00000000..e9aad823
@@ -0,0 +1,2 @@
+QA output created by 350
+Silence is golden
--

2.51.2