* [PATCH] btrfs: zoned: activate RAID1 data block groups on degraded mounts
@ 2026-11-21 20:56 Miroslav Cimerman
0 siblings, 0 replies; only message in thread
From: Miroslav Cimerman @ 2026-11-21 20:56 UTC (permalink / raw)
To: linux-btrfs
Cc: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
Qu Wenruo, linux-kernel
btrfs_load_block_group_raid1() compares the active state of each present
stripe's zone with the first present stripe's. If the first present zone
is active, the block group is marked active. When the states differ, a
regular mount also calls btrfs_zone_activate(), which counts every zone
against its device's active zone limit, unless the block group is
already marked active. A degraded mount skips that call, and a data
block group keeps the active state of the first present stripe.
For a data block group, this goes wrong when a device that missed writes
comes back:
- The first present zone is active and another one is empty: the block
group is marked active, but the empty zone is not counted. A zone
append write opens it, so until the block group is finished, the
device has one more active zone than btrfs counts, and opening a zone
at the device's limit can fail.
- The first present zone is empty or full and another one is active: the
block group is not marked active, while its active zone is counted.
do_zone_finish() skips block groups that are not active, so a full
block group is not finished, and its active zone stays taken until the
block group is removed.
On a degraded mount, fix up a written data block group whose zones
differ in active state. In the first case, clear the active flag and
activate the block group, so that all its zones are counted. If
btrfs_zone_activate() fails, the block group stays active as before. A
full block group gets no more writes, so mark it active without
activating it: its open zones can then be finished, and its full zones
are not counted. In the second case, a block group with room left is
left to its first allocation, as before. That activation holds back the
active zones reserved for metadata. Metadata block groups are left as
they are: their writes fail on a stripe that is behind and do not open
its zone.
Fixes: 568220fa9657 ("btrfs: zoned: support RAID0/1/10 on top of raid stripe tree")
CC: stable@vger.kernel.org # 6.7+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
Sashiko reported this in its review of "[PATCH v2 4/5] btrfs: zoned: use
the highest RAID1 write pointer on degraded mounts". This patch applies
on top of that series:
https://lore.kernel.org/linux-btrfs/20261121134550.216217-1-mc@raidlab.org/
The fix and the commit message were prepared with the help of an LLM,
which also tested it. I reviewed everything.
In testing on two-device RAID1 with a debug print of the active zone
count, both cases reproduced on for-next, with and without the series.
In the first, a device came back with its zone of a data block group
empty, the first write opened that zone, and when the block group was
finished, the zone was not set in btrfs's active zone bitmap of that
device. In the second, a full data block group with an open zone on the
returning device was not on the list of active block groups. With this
patch, the empty zone is counted when the block group is loaded, and the
full block group is on the list.
The tests did not get a device to its active zone limit while the zone
was uncounted, so there is no failing write to show. The self-tests do
not reach the new code: they load no data block groups.
In the first case, the empty zone is never on the first present stripe,
so with two-device RAID1 it is on the last stripe. The last copy of a
mirrored write is a zone append write. On for-next, the other copies are
regular writes: btrfs_submit_mirrored_bio() sets them up with
btrfs_bio_init(), which clears can_use_append. So "Data is written by
zone append to each stripe" in 4/5 holds only for the last stripe today.
I will send a fix for that separately. Until then, a write does not open
an empty zone on a middle stripe of RAID1C3 or RAID1C4, but this patch
counts it until the block group is finished.
btrfs_zone_activate() checks the active zone limit of each device before
it sees that the zone is already active. So it can fail on a device that
has no active zone left, even if its zone of the block group is active.
In the first case, the empty zone can then stay uncounted, as before
this patch, and the block group stays active. Zones counted before the
failure stay counted, as when the allocator activates a block group. The
order of the checks is not changed here.
Activation at load is not limited by the active zones reserved for
metadata: reserved_active_zones is still 0 while the block groups are
loaded. So it can take active zones meant for metadata on the returning
device, until a data block group is finished. With two-device RAID1, the
next write to the block group opens that zone anyway.
fs/btrfs/zoned.c | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 8ce329da612d..2d5f78e71620 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -1524,6 +1524,7 @@ static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
struct btrfs_fs_info *fs_info = bg->fs_info;
u64 highest = 0;
bool mismatch = false;
+ bool active_mismatch = false;
int first = -1;
int i;
@@ -1600,12 +1601,33 @@ static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
!btrfs_zone_activate(bg))) {
return -EIO;
}
+ active_mismatch = true;
} else {
if (test_bit(first, active))
set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
}
}
+ /*
+ * On a degraded mount, the active state above comes from the first
+ * present stripe. If that zone is active and another one is empty, a
+ * zone append write opens the empty one without counting it. Clear the
+ * flag and activate such a written data block group, so that all its
+ * zones are counted. A full one gets no more writes: mark it active, so
+ * that its open zones can be finished.
+ */
+ if (active_mismatch && btrfs_test_opt(fs_info, DEGRADED) &&
+ (map->type & BTRFS_BLOCK_GROUP_DATA) && highest) {
+ if (highest >= bg->zone_capacity) {
+ set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
+ } else if (test_and_clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
+ &bg->runtime_flags)) {
+ /* Failure is OK, the block group stays active. */
+ btrfs_zone_activate(bg);
+ set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
+ }
+ }
+
if (mismatch) {
btrfs_warn(fs_info,
"zoned: write pointer mismatch in %s block group %llu, using the highest",
base-commit: 184fa2c40f5c50afb1d8bf900eafadf450db392e
prerequisite-patch-id: f0678cffb57a607bde3edf038077931028c982e6
prerequisite-patch-id: 8fd088df247e1257946b793993343d85109a457b
prerequisite-patch-id: 5ac60d0cd7e210833e85f523e3dea594fa15ecd0
prerequisite-patch-id: 173c8a0cb5bd262349e48de045c0c1369707add1
prerequisite-patch-id: a764dcedb5b7d3c86d7400deee8ec63c78ee2c40
--
2.53.0
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-21 20:57 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-21 20:56 [PATCH] btrfs: zoned: activate RAID1 data block groups on degraded mounts Miroslav Cimerman
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®