[PATCH v2] fstests: btrfs: add a new test case for fstrim crash

Qu Wenruo <[email protected]>
Newsgroups org.kernel.vger.fstests,org.kernel.vger.linux-btrfs
Message-ID <[email protected]>
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.

Reported-by: Su Yue <[email protected]>
Link: https://lore.kernel.org/linux-btrfs/[email protected]/
Reviewed-by: Filipe Manana <[email protected]>
Signed-off-by: Qu Wenruo <[email protected]>
---
Changelog:
v2:
- Fix two grammar errors
- Add the missing _scratch_dev_pool_put at the end
- Add Filipe's reviewed-by tag
---
 tests/btrfs/350     | 39 +++++++++++++++++++++++++++++++++++++++
 tests/btrfs/350.out |  2 ++
 2 files changed, 41 insertions(+)
 create mode 100755 tests/btrfs/350
 create mode 100644 tests/btrfs/350.out

diff --git a/tests/btrfs/350 b/tests/btrfs/350
new file mode 100755
index 00000000..7cc04b63
--- /dev/null
+++ b/tests/btrfs/350
@@ -0,0 +1,39 @@
+#! /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 and a proper bdev pointer.
+#
+# For unpatched kernel, it will crash with a NULL pointer dereference.
+$FSTRIM_PROG $SCRATCH_MNT
+
+_scratch_dev_pool_put
+echo "Silence is golden"
+_exit 0
diff --git a/tests/btrfs/350.out b/tests/btrfs/350.out
new file mode 100644
index 00000000..e9aad823
--- /dev/null
+++ b/tests/btrfs/350.out
@@ -0,0 +1,2 @@
+QA output created by 350
+Silence is golden
-- 
2.51.2
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.