Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Miroslav Cimerman <mc@raidlab.org>
To: linux-btrfs@vger.kernel.org
Cc: David Sterba <dsterba@suse.com>, Chris Mason <mason@kernel.org>,
	Naohiro Aota <naohiro.aota@wdc.com>,
	Johannes Thumshirn <johannes.thumshirn@wdc.com>,
	Qu Wenruo <wqu@suse.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 4/5] btrfs: zoned: use the highest RAID1 write pointer on degraded mounts
Date: Sat, 21 Nov 2026 13:45:48 +0000	[thread overview]
Message-ID: <20261121134550.216217-5-mc@raidlab.org> (raw)
In-Reply-To: <20261121134550.216217-1-mc@raidlab.org>

The write pointers of a RAID1 block group's stripes can differ after a
crash, or when a device missed committed writes and came back. A
regular mount fails then, and commit b0c26f479926 ("btrfs: zoned:
return EIO on RAID1 block group write pointer mismatch") relies on
manual intervention: a degraded mount and a full balance. On a degraded
mount, btrfs_load_block_group_raid1() ignores the mismatch and takes
the allocation offset from stripe 0. If stripe 0 is behind, new extents
are allocated over committed ones:

  stripe 0 (behind)  | a | a |   |   |
  stripe 1           | a | a | b | b |
                             ^
                             allocation offset from stripe 0,
                             new extents go over b

  BTRFS error (device nb2): failed to run delayed ref for logical 155189248 num_bytes 1048576 type 178 action 1 ref_mod 1: -17
  BTRFS error (device nb2 state A): Transaction 18 aborted (-EEXIST)

or, with CONFIG_BTRFS_ASSERT, an assertion in the free space tree fails:

  assertion failed: start >= found_start && end <= found_end, in fs/btrfs/free-space-tree.c:742

A stripe that is not behind has all committed extents below its write
pointer, the highest one. If no present stripe is up to date, the
highest write pointer can still be below committed extents, as before.
On a degraded mount, take the allocation offset from it, and warn about
the block group. Until a balance copies the block group to new zones,
reads of what the stripes behind miss fail their checksums and go to the
other copies. Writes to the stripes behind can fail, as they already
could when stripe 0 was ahead. Data without checksums can return what a
stripe behind holds there.

Once a block group with a stripe behind is full and finished, all its
zones report their capacity, and a later regular mount no longer sees
that a stripe was behind. Only the warning of the degraded mount tells
that a balance is needed.

The active state still comes from the first present stripe. If that is
an empty zone behind a written one, a metadata block group is not marked
active although it holds tree blocks. btrfs_zone_activate() then warns
at its next write: it expects a block group that is not active to be
empty. Tree blocks are written at the highest write pointer, which fails
on a stripe behind without opening its zone. So mark a metadata block
group with a mismatch active if any of its zones is. Data is written by
zone append to each stripe. So a data block group whose first present
zone is empty is still activated with all its zones at its first
allocation, if it has room and its devices have active zones left.

Compare the write pointers with the first present stripe, and leave
missing stripes out of the highest one. With stripe 0 missing, the
allocation offset came from the last stripe. In RAID1C3 and RAID1C4 that
stripe can be missing as well, and the block group then failed to load.

An offline or read-only zone, or one whose zone report fails with -EIO
or -EOPNOTSUPP, has no write pointer, as with a missing stripe. With
stripe 0's zone in that state, a regular mount failed on the comparison,
while with another stripe's zone in that state it loaded. Now both load.

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>
---
v2 (was 2/4): mark a metadata block group with a mismatch active if any
of its zones is. Sashiko found that with an empty first zone, it stayed
inactive and warned at its next write.

Found with FuzzyRAID, a fault-injection tester for RAID that is not
public at the moment. The analysis, the fix and the commit message
were prepared with the help of an LLM, which also tested it. I reviewed
everything.

Making such a block group unallocatable instead, as b1934cd60695 did,
was replaced by failing the mount in b0c26f479926. With the highest
write pointer, the degraded mount and balance that b0c26f479926
describes keep working, and in testing, a regular mount works again
after the balance.

A regular mount now loads a block group whose stripe 0 is on an offline
or read-only zone. Finishing the block group later fails on that zone,
which, for a metadata block group, turns the filesystem read-only. It
already did so with such a zone on another stripe.

Scrub on such a degraded mount finds what the stripes behind miss, but
does not repair it, as zoned repairs are skipped on degraded mounts.
Qu agreed to drop that skip in the thread of "btrfs: scrub: don't count
zoned repairs as corrected on a degraded mount", and I will send that
as its own patch:
https://lore.kernel.org/linux-btrfs/20261118175637.60283-1-mc@raidlab.org/
In testing, scrub then relocated the block groups where it found
errors, and a regular mount worked without a balance.

A degraded mount without the stripe with the smallest zone capacity uses
a larger one and can write past the smallest. This is not changed here.
With that stripe back, a degraded mount now fails to load such a block
group. Before, if that stripe was stripe 0, it took stripe 0's write
pointer, below the extents written while it was missing.

 fs/btrfs/zoned.c | 47 ++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 38 insertions(+), 9 deletions(-)

diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 48f8391a80a9..8ce329da612d 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -1522,6 +1522,8 @@ static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
 					u64 last_alloc)
 {
 	struct btrfs_fs_info *fs_info = bg->fs_info;
+	u64 highest = 0;
+	bool mismatch = false;
 	int first = -1;
 	int i;
 
@@ -1575,13 +1577,24 @@ static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
 		if (first < 0)
 			first = i;
 
-		if (unlikely((zone_info[0].alloc_offset != zone_info[i].alloc_offset) &&
-			     !btrfs_test_opt(fs_info, DEGRADED))) {
-			btrfs_err(fs_info,
-			"zoned: write pointer offset mismatch of zones in %s profile",
-				  btrfs_bg_type_to_raid_name(map->type));
-			return -EIO;
+		if (zone_info[first].alloc_offset != zone_info[i].alloc_offset) {
+			/*
+			 * A stripe is behind the others after a crash, or when
+			 * its device missed committed writes and came back.
+			 * This fails a regular mount. A degraded one allocates
+			 * from the highest write pointer: committed extents are
+			 * below it, unless all present stripes are behind.
+			 */
+			if (unlikely(!btrfs_test_opt(fs_info, DEGRADED))) {
+				btrfs_err(fs_info,
+				"zoned: write pointer offset mismatch of zones in %s profile",
+					  btrfs_bg_type_to_raid_name(map->type));
+				return -EIO;
+			}
+			mismatch = true;
 		}
+		highest = max(highest, zone_info[i].alloc_offset);
+
 		if (test_bit(first, active) != test_bit(i, active)) {
 			if (unlikely(!btrfs_test_opt(fs_info, DEGRADED) &&
 				     !btrfs_zone_activate(bg))) {
@@ -1593,10 +1606,26 @@ static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
 		}
 	}
 
-	if (zone_info[0].alloc_offset != WP_MISSING_DEV)
-		bg->alloc_offset = zone_info[0].alloc_offset;
+	if (mismatch) {
+		btrfs_warn(fs_info,
+		"zoned: write pointer mismatch in %s block group %llu, using the highest",
+			   btrfs_bg_type_to_raid_name(map->type), bg->start);
+		/*
+		 * Metadata is written at the highest write pointer, which fails
+		 * on a stripe behind and opens no zone there.
+		 * btrfs_zone_activate() expects an inactive block group to be
+		 * empty, but this one holds metadata, so it is active if any of
+		 * its zones is.
+		 */
+		if (!(map->type & BTRFS_BLOCK_GROUP_DATA) &&
+		    !bitmap_empty(active, map->num_stripes))
+			set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
+	}
+
+	if (first >= 0)
+		bg->alloc_offset = highest;
 	else
-		bg->alloc_offset = zone_info[i - 1].alloc_offset;
+		bg->alloc_offset = WP_MISSING_DEV;
 
 	return 0;
 }
-- 
2.53.0


  parent reply	other threads:[~2026-11-21 13:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21 13:45 [PATCH v2 0/5] btrfs: zoned: RAID1 block groups with missing or stale stripes Miroslav Cimerman
2026-11-21 13:45 ` [PATCH v2 1/5] btrfs: zoned: take the RAID1 active state from the first present stripe Miroslav Cimerman
2026-11-21 13:45 ` [PATCH v2 2/5] btrfs: zoned: take the RAID1 zone capacity from all present stripes Miroslav Cimerman
2026-11-21 13:45 ` [PATCH v2 3/5] btrfs: zoned: cap full RAID1 zones at the block group's capacity Miroslav Cimerman
2026-11-21 13:45 ` Miroslav Cimerman [this message]
2026-11-21 13:45 ` [PATCH v2 5/5] btrfs: tests: zoned: add RAID1 cases with missing or stale stripes Miroslav Cimerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261121134550.216217-5-mc@raidlab.org \
    --to=mc@raidlab.org \
    --cc=dsterba@suse.com \
    --cc=johannes.thumshirn@wdc.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    --cc=naohiro.aota@wdc.com \
    --cc=wqu@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox