mirror of https://lore.kernel.org/lkml/
 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>,
	linux-kernel@vger.kernel.org
Subject: [PATCH 2/4] btrfs: zoned: use the highest RAID1 write pointer on degraded mounts
Date: Thu, 19 Nov 2026 21:38:33 +0000	[thread overview]
Message-ID: <20261119213835.103131-3-mc@raidlab.org> (raw)
In-Reply-To: <20261119213835.103131-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, and writes to them can fail, as they already could when
stripe 0 was ahead. Data without checksums can return what a stripe
behind holds there.

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, which can be missing as
well in RAID1C3 and RAID1C4, and the block group then failed to load.

An offline or read-only zone, or one whose zone report fails with -EIO
or -EOPNOTSUPP, counts as 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>
---
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.

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.
Repairing there would let scrub fix the block groups where it finds
errors, instead of a balance. That is discussed in the thread of
"btrfs: scrub: don't count zoned repairs as corrected on a degraded
mount":
https://lore.kernel.org/linux-btrfs/20261118175637.60283-1-mc@raidlab.org/

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

diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 8eac6e2283a2..8bd44dd260a4 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;
 
@@ -1559,13 +1561,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))) {
@@ -1577,10 +1590,15 @@ 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);
+
+	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-19 21:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 21:38 [PATCH 0/4] btrfs: zoned: RAID1 block groups with missing or stale stripes Miroslav Cimerman
2026-11-19 21:38 ` [PATCH 1/4] btrfs: zoned: take the RAID1 active state from the first present stripe Miroslav Cimerman
2026-11-19 21:38 ` Miroslav Cimerman [this message]
2026-11-19 21:38 ` [PATCH 3/4] btrfs: zoned: take the RAID1 zone capacity from a " Miroslav Cimerman
2026-11-19 21:38 ` [PATCH 4/4] btrfs: tests: zoned: add degraded RAID1 cases 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=20261119213835.103131-3-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 \
    /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

all inboxes | Powered by JetHome®