Skip to contentSkip to conversation

btrfs-progs: make "btrfstune -S1" to reject fs with dirty log

PATCH In review Qu Wenruo 2 participants 4 messages Apr 19, 2022 – Apr 25, 2022 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

v2 cover btrfs-progs: make "btrfstune -S1" to reject fs with dirty log

[CHANGELOG]
v2:
- Add a test case for it

A seed device with dirty log can be rejected by kernel, as kernel will try to replay log even on RO mount.
But log replay on RO device is strongly prohibited, thus such seed device will be rejected without a way to sprout.

Fix the problem by letting "btrfstune -S1" to check if the fs has dirty log first.

Also add a test case for it, using a btrfs-image dump.

Qu Wenruo (2):
btrfs-progs: do not allow setting seed flag on fs with dirty log
btrfs-progs: make sure "btrfstune -S1" will reject fs with dirty log

 btrfstune.c                                      |   4 ++++
 .../052-seed-dirty-log/dirty_log.img.xz          | Bin 0 -> 2140 bytes
 tests/misc-tests/052-seed-dirty-log/test.sh      |  12 ++++++++++++
 3 files changed, 16 insertions(+)
 create mode 100644 tests/misc-tests/052-seed-dirty-log/dirty_log.img.xz
 create mode 100755 tests/misc-tests/052-seed-dirty-log/test.sh
Signature · 1 line
2.35.1
author
#1 lore raw

PATCH v2 1/2 btrfs-progs: do not allow setting seed flag on fs with dirty log

[BUG]
The following sequence operation can lead to a seed fs rejected by kernel:

# Generate a fs with dirty log
mkfs.btrfs -f $file
mount $dev $mnt
xfs_io -f -c "pwrite 0 16k" -c fsync $mnt/file
cp $file $file.backup
umount $mnt
mv $file.backup $file

 # now $file has dirty log, set seed flag on it
 btrfstune -S1 $file
 # mount will fail
 mount $file $mnt

The mount failure with the following dmesg:

[  980.363667] loop0: detected capacity change from 0 to 262144
[  980.371177] BTRFS info (device loop0): flagging fs with big metadata feature
[  980.372229] BTRFS info (device loop0): using free space tree
[  980.372639] BTRFS info (device loop0): has skinny extents
[  980.375075] BTRFS info (device loop0): start tree-log replay
[  980.375513] BTRFS warning (device loop0): log replay required on RO media
[  980.381652] BTRFS error (device loop0): open_ctree failed

[CAUSE]
Although btrfs will replay its dirty log even with RO mount, but kernel will treat seed device as RO device, and dirty log can not be replayed on RO device.

This rejection is already the better end, just imagine if we don't treat seed device as RO, and replayed the dirty log.
The filesystem relying on the seed device will be completely screwed up.

[FIX]
Just add extra check on log tree in btrfstune to reject setting seed flag on filesystems with dirty log.

---
 btrfstune.c | 4 ++++
 1 file changed, 4 insertions(+)
btrfstune.c +4 -0
index c9a92349a44c..3824a3d9d8ff 100644
@@ -59,6 +59,10 @@ static int update_seeding_flag(struct btrfs_root *root, int set_flag)
 						device);
 			return 1;
 		}
+		if (btrfs_super_log_root(disk_super)) {
+			error("filesystem with dirty log detected, not setting seed flag");
+			return 1;
+		}
 		super_flags |= BTRFS_SUPER_FLAG_SEEDING;
 	} else {
 		if (!(super_flags & BTRFS_SUPER_FLAG_SEEDING)) {
Signature · 1 line
2.35.1
author
#2 lore raw

PATCH v2 2/2 btrfs-progs: make sure "btrfstune -S1" will reject fs with dirty log

In reply to Qu Wenruo

The new test case will have a image file which has dirty log (btrfs-image supports dumping log tree).

So we can easily check if "btrfstune -S" will reject fs with dirty log.

---
 .../052-seed-dirty-log/dirty_log.img.xz          | Bin 0 -> 2140 bytes
 tests/misc-tests/052-seed-dirty-log/test.sh      |  12 ++++++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 tests/misc-tests/052-seed-dirty-log/dirty_log.img.xz
 create mode 100755 tests/misc-tests/052-seed-dirty-log/test.sh
tests/misc-tests/052-seed-dirty-log/dirty_log.img.xz new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..a14b0cb02b8a5e36b4dad49e0cddf5c5dbeebed5
GIT binary patch
tests/misc-tests/052-seed-dirty-log/test.sh new file +12 -0
new file mode 100755
index 000000000000..2f16f43a29a2
@@ -0,0 +1,12 @@
+#!/bin/bash
+#
+# Make sure btrfstune will not set seed flag when the fs has dirty log
+
+source "$TEST_TOP/common"
+
+check_prereq btrfstune
+
+image=$(extract_image "./dirty_log.img.xz")
+
+run_mustfail "btrfstune should reject fs with dirty log for seeding flag" \
+	"$TOP/btrfstune" -S1 "$image"
Signature · 1 line
2.35.1
#3 lore raw
In reply to Qu Wenruo
Quoted 10 lines · Qu Wenruo

[CHANGELOG]
v2:
- Add a test case for it

A seed device with dirty log can be rejected by kernel, as kernel will try to replay log even on RO mount.
But log replay on RO device is strongly prohibited, thus such seed device will be rejected without a way to sprout.

Quoted 8 lines

Fix the problem by letting "btrfstune -S1" to check if the fs has dirty log first.

Also add a test case for it, using a btrfs-image dump.

Qu Wenruo (2):
btrfs-progs: do not allow setting seed flag on fs with dirty log
btrfs-progs: make sure "btrfstune -S1" will reject fs with dirty log

Added to devel, thanks.