[PATCH 2/3] btrfs: zoned: tolerate a failed zone finish on a full block group
From: Miroslav Cimerman
Date: Sun Nov 22 2026 - 18:23:43 EST
do_zone_finish() sends ZONE_FINISH to the zone of each stripe of a block
group. It stops at the first error and returns it, even if the profile
tolerates the failed device. Since commit 3d16abf6c88a ("btrfs: zoned:
directly call do_zone_finish() from btrfs_zone_finish_endio_workfn()"),
btrfs_zone_finish_endio_workfn() handles that error with
btrfs_handle_fs_error(). So one failed device of a RAID1 metadata block
group turns the filesystem read-only.
When the block group is allocated up to the zone capacity and
everything is written, the zones are already full on the devices. A
failed ZONE_FINISH does not change them. In this case, finish the zones
of all stripes. Like a write, fail only if more devices failed than the
profile tolerates. Count a missing device toward the limit. The failed
device keeps the zone counted as active until the zone is reset or the
filesystem is remounted. For metadata, the active zone reservation
comes back only at the next mount.
A partly written block group keeps the current behavior. It stops at
the first error and returns it. Finishing the zones after a failed one
would leave more zones at different write pointers.
btrfs_load_block_group_raid1() refuses such a block group at the next
mount.
Fixes: 3d16abf6c88a ("btrfs: zoned: directly call do_zone_finish() from btrfs_zone_finish_endio_workfn()")
CC: stable@xxxxxxxxxxxxxxx # 6.18+: a18a6b93a284: btrfs: zoned: finish active block group cleanup if call_zone_finish() fails
Signed-off-by: Miroslav Cimerman <mc@xxxxxxxxxxx>
---
fs/btrfs/zoned.c | 48 ++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 42 insertions(+), 6 deletions(-)
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 45bd05d8edb4..d18dc4f4dc33 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2540,9 +2540,6 @@ static int call_zone_finish(struct btrfs_block_group *block_group,
bool cleared;
int ret;
- if (!device->bdev)
- return 0;
-
if (zinfo->max_active_zones == 0)
return 0;
@@ -2573,6 +2570,9 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
const bool is_metadata = (block_group->flags &
(BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
+ enum btrfs_raid_types index;
+ bool zones_full;
+ int failed = 0;
int ret = 0;
int i;
@@ -2639,6 +2639,13 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
}
clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
+ /*
+ * If the block group is allocated up to the zone capacity,
+ * everything is written and the zones should be full already. A
+ * truncated or failed write at the end is not detected here. That
+ * would need a zone report from every device.
+ */
+ zones_full = (block_group->alloc_offset == block_group->zone_capacity);
block_group->alloc_offset = block_group->zone_capacity;
if (block_group->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM))
block_group->meta_write_pointer = block_group->start +
@@ -2651,12 +2658,41 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
down_read(&dev_replace->rwsem);
map = block_group->physical_map;
for (i = 0; i < map->num_stripes; i++) {
- ret = call_zone_finish(block_group, &map->stripes[i]);
- if (ret)
- break;
+ struct btrfs_io_stripe *stripe = &map->stripes[i];
+ int err;
+
+ if (!stripe->dev->bdev) {
+ failed++;
+ continue;
+ }
+
+ err = call_zone_finish(block_group, stripe);
+ if (err) {
+ btrfs_warn(fs_info,
+ "zoned: failed to finish zone at physical %llu on devid %llu: %d",
+ stripe->physical, stripe->dev->devid, err);
+ failed++;
+ ret = err;
+ /*
+ * Do not finish more zones of a partly written block
+ * group. They would end at a different write pointer
+ * than the failed one.
+ */
+ if (!zones_full)
+ break;
+ }
}
up_read(&dev_replace->rwsem);
+ /*
+ * Zones that are already full do not change with a failed finish.
+ * Like a write, fail only if more devices failed than the profile
+ * tolerates. A missing device counts toward the limit.
+ */
+ index = btrfs_bg_flags_to_raid_index(map->type);
+ if (zones_full && failed <= btrfs_raid_array[index].tolerated_failures)
+ ret = 0;
+
if (!ret && !fully_written)
btrfs_dec_block_group_ro(block_group);
--
2.53.0