[PATCH v2 5/5] btrfs: tests: zoned: add RAID1 cases with missing or stale stripes

From: Miroslav Cimerman

Date: Sat Nov 21 2026 - 08:46:40 EST


Add cases for the previous patches. On a degraded mount:

- RAID1 with the first device missing, and RAID1 metadata with an empty
first zone: both are marked active
- RAID1C3 with the first and the last, and with the first two devices
missing
- RAID1 and RAID1C3 with different write pointers: the highest one is
taken
- RAID1 with full zones of different capacities
- RAID1 with a full zone that has extents past the smaller capacity: not
capped, so the caller then refuses the block group

On a regular mount:

- RAID1 with the first zone offline, with a failed zone report for it,
and with full zones of different capacities: now load
- RAID1C3 with a smaller third zone: sets the capacity
- two cases that still fail: a write pointer past the capacity in a zone
that is not full, and full zones with extents past the capacity

Let a case set the zone capacity of each stripe. A stripe without a
write pointer has a capacity of 0 unless the case sets one:
btrfs_load_zone_info() gives 0 for a missing device or a failed zone
report, while an offline zone keeps its capacity. Derive the active
state from the write pointer and the capacity, and check the zone
capacity and the active state where a case asks for it. All new cases
fail without the previous patches, except the two that fail either way.
Those make sure that only full zones without extents past the capacity
are capped.

Signed-off-by: Miroslav Cimerman <mc@xxxxxxxxxxx>
---
v2 (was 4/4): add the cases with an empty first zone, with different
zone capacities and with a failed zone report, and a capacity per
stripe. The case with an offline zone gives that zone a capacity now.

The subject was "btrfs: tests: zoned: add degraded RAID1 cases", but
6 of the 14 new cases are on a regular mount.

Prepared with the help of an LLM, which also ran the tests. I reviewed
everything.

fs/btrfs/tests/zoned-tests.c | 203 ++++++++++++++++++++++++++++++++++-
1 file changed, 201 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/tests/zoned-tests.c b/fs/btrfs/tests/zoned-tests.c
index bb9969210444..4848aa6fde11 100644
--- a/fs/btrfs/tests/zoned-tests.c
+++ b/fs/btrfs/tests/zoned-tests.c
@@ -21,12 +21,15 @@ struct load_zone_info_test_vector {
u64 raid_type;
u64 num_stripes;
u64 alloc_offsets[8];
+ u64 capacities[8];
u64 last_alloc;
u64 bg_length;
bool degraded;

int expected_result;
u64 expected_alloc_offset;
+ u64 expected_zone_capacity;
+ bool expected_active;

const char *description;
};
@@ -77,8 +80,16 @@ static int test_load_zone_info(struct btrfs_fs_info *fs_info,
for (int i = 0; i < test->num_stripes; i++) {
zone_info[i].physical = 0;
zone_info[i].alloc_offset = test->alloc_offsets[i];
- zone_info[i].capacity = ZONE_SIZE;
- if (zone_info[i].alloc_offset && zone_info[i].alloc_offset < ZONE_SIZE)
+ /*
+ * Without a write pointer, the capacity is 0 for a missing device
+ * or a failed zone report, while an offline zone keeps its own.
+ */
+ if (zone_info[i].alloc_offset == WP_MISSING_DEV)
+ zone_info[i].capacity = test->capacities[i];
+ else
+ zone_info[i].capacity = test->capacities[i] ?: ZONE_SIZE;
+ if (zone_info[i].alloc_offset &&
+ zone_info[i].alloc_offset < zone_info[i].capacity)
__set_bit(i, active);
}
if (test->degraded)
@@ -101,6 +112,19 @@ static int test_load_zone_info(struct btrfs_fs_info *fs_info,
return -EINVAL;
}

+ if (!ret && test->expected_zone_capacity &&
+ bg->zone_capacity != test->expected_zone_capacity) {
+ test_err("unexpected zone_capacity: zone_capacity %llu expected %llu",
+ bg->zone_capacity, test->expected_zone_capacity);
+ return -EINVAL;
+ }
+
+ if (!ret && test->expected_active &&
+ !test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags)) {
+ test_err("block group not marked active");
+ return -EINVAL;
+ }
+
return 0;
}

@@ -244,7 +268,182 @@ static const struct load_zone_info_test_vector load_zone_info_tests[] = {
.degraded = true,
.expected_alloc_offset = SZ_1M,
},
+ {
+ .description = "RAID1: missing first device on DEGRADED",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1,
+ .num_stripes = 2,
+ .alloc_offsets = {
+ WP_MISSING_DEV, SZ_1M,
+ },
+ .degraded = true,
+ .expected_alloc_offset = SZ_1M,
+ .expected_active = true,
+ },
+ {
+ .description = "RAID1C3: missing first and last device on DEGRADED",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1C3,
+ .num_stripes = 3,
+ .alloc_offsets = {
+ WP_MISSING_DEV, SZ_1M, WP_MISSING_DEV,
+ },
+ .degraded = true,
+ .expected_alloc_offset = SZ_1M,
+ },
+ {
+ .description = "RAID1C3: missing first and second device on DEGRADED",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1C3,
+ .num_stripes = 3,
+ .alloc_offsets = {
+ WP_MISSING_DEV, WP_MISSING_DEV, SZ_1M,
+ },
+ .degraded = true,
+ .expected_alloc_offset = SZ_1M,
+ .expected_zone_capacity = ZONE_SIZE,
+ },
+ /* A stripe behind the others: the highest write pointer on DEGRADED. */
+ {
+ .description = "RAID1: metadata, empty first zone on DEGRADED",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1,
+ .num_stripes = 2,
+ .alloc_offsets = {
+ 0, SZ_1M,
+ },
+ .degraded = true,
+ .expected_alloc_offset = SZ_1M,
+ .expected_active = true,
+ },
+ {
+ .description = "RAID1: different write pointers on DEGRADED",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1,
+ .num_stripes = 2,
+ .alloc_offsets = {
+ SZ_1M, SZ_2M,
+ },
+ .degraded = true,
+ .expected_alloc_offset = SZ_2M,
+ },
+ {
+ .description = "RAID1C3: first missing, different write pointers on DEGRADED",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1C3,
+ .num_stripes = 3,
+ .alloc_offsets = {
+ WP_MISSING_DEV, SZ_2M, SZ_1M,
+ },
+ .degraded = true,
+ .expected_alloc_offset = SZ_2M,
+ },
+ /*
+ * An offline zone or a failed zone report has no write pointer on a
+ * regular mount either. An offline zone still has its capacity.
+ */
+ {
+ .description = "RAID1: first zone offline",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1,
+ .num_stripes = 2,
+ .alloc_offsets = {
+ WP_MISSING_DEV, SZ_1M,
+ },
+ .capacities = {
+ ZONE_SIZE - SZ_1M,
+ },
+ .expected_alloc_offset = SZ_1M,
+ .expected_zone_capacity = ZONE_SIZE - SZ_1M,
+ },
+ {
+ .description = "RAID1: failed zone report of the first zone",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1,
+ .num_stripes = 2,
+ .alloc_offsets = {
+ WP_MISSING_DEV, SZ_1M,
+ },
+ .expected_alloc_offset = SZ_1M,
+ .expected_zone_capacity = ZONE_SIZE,
+ },
+ /* The block group's capacity is the smallest of its stripes'. */
+ {
+ .description = "RAID1C3: smaller third zone capacity",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1C3,
+ .num_stripes = 3,
+ .alloc_offsets = {
+ SZ_1M, SZ_1M, SZ_1M,
+ },
+ .capacities = {
+ ZONE_SIZE, ZONE_SIZE, ZONE_SIZE - SZ_1M,
+ },
+ .expected_alloc_offset = SZ_1M,
+ .expected_zone_capacity = ZONE_SIZE - SZ_1M,
+ },
+ /* Full zones report their own capacity, which can differ by device. */
+ {
+ .description = "RAID1: full zones with different capacities",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1,
+ .num_stripes = 2,
+ .alloc_offsets = {
+ ZONE_SIZE - SZ_1M, ZONE_SIZE,
+ },
+ .capacities = {
+ ZONE_SIZE - SZ_1M, ZONE_SIZE,
+ },
+ .last_alloc = ZONE_SIZE - SZ_1M,
+ .expected_alloc_offset = ZONE_SIZE - SZ_1M,
+ .expected_zone_capacity = ZONE_SIZE - SZ_1M,
+ },
+ {
+ .description = "RAID1: full zones with different capacities on DEGRADED",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1,
+ .num_stripes = 2,
+ .alloc_offsets = {
+ ZONE_SIZE, ZONE_SIZE - SZ_1M,
+ },
+ .capacities = {
+ ZONE_SIZE, ZONE_SIZE - SZ_1M,
+ },
+ .last_alloc = ZONE_SIZE - SZ_1M,
+ .degraded = true,
+ .expected_alloc_offset = ZONE_SIZE - SZ_1M,
+ .expected_zone_capacity = ZONE_SIZE - SZ_1M,
+ },
+ {
+ .description = "RAID1: full zone with extents past the capacity on DEGRADED",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1,
+ .num_stripes = 2,
+ .alloc_offsets = {
+ SZ_1M, ZONE_SIZE,
+ },
+ .capacities = {
+ ZONE_SIZE - SZ_1M, ZONE_SIZE,
+ },
+ .last_alloc = ZONE_SIZE,
+ .degraded = true,
+ .expected_alloc_offset = ZONE_SIZE,
+ .expected_zone_capacity = ZONE_SIZE - SZ_1M,
+ },
/* Error case: having different write pointers. */
+ {
+ .description = "RAID1: fail: write pointer past the capacity",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1,
+ .num_stripes = 2,
+ .alloc_offsets = {
+ ZONE_SIZE - SZ_1M, ZONE_SIZE - SZ_512K,
+ },
+ .capacities = {
+ ZONE_SIZE - SZ_1M, ZONE_SIZE,
+ },
+ .expected_result = -EIO,
+ },
+ {
+ .description = "RAID1: fail: full zones with extents past the capacity",
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1,
+ .num_stripes = 2,
+ .alloc_offsets = {
+ ZONE_SIZE - SZ_1M, ZONE_SIZE,
+ },
+ .capacities = {
+ ZONE_SIZE - SZ_1M, ZONE_SIZE,
+ },
+ .last_alloc = ZONE_SIZE,
+ .expected_result = -EIO,
+ },
{
.description = "RAID1: fail: different write pointers",
.raid_type = BTRFS_BLOCK_GROUP_RAID1,
--
2.53.0