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>,
	Qu Wenruo <wqu@suse.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 3/5] btrfs: zoned: cap full RAID1 zones at the block group's capacity
Date: Sat, 21 Nov 2026 13:45:47 +0000	[thread overview]
Message-ID: <20261121134550.216217-4-mc@raidlab.org> (raw)
In-Reply-To: <20261121134550.216217-1-mc@raidlab.org>

The zone capacity of a RAID1 block group is the smallest of its
stripes'. Finishing the block group finishes its zones on devices with
an active zone limit, and a full zone reports its own capacity as its
write pointer. With zone capacities that differ between the devices, the
write pointers of a full block group then differ, and the next mount
fails:

  BTRFS error (device nb2): zoned: write pointer offset mismatch of zones in raid1 profile
  BTRFS error (device nb2): zoned: failed to load zone info of bg 150994944

A degraded mount with all devices present takes stripe 0's write
pointer, and fails too unless stripe 0 has the smallest capacity:

  BTRFS error (device nb2): zoned: invalid write pointer 14680064 (larger than zone capacity 12582912) in block group 150994944

Take a full zone with a larger capacity as written up to the block
group's capacity, unless an extent of the block group ends past it. Then
the block group was written with a larger capacity, e.g. by a degraded
mount without the stripe with the smallest capacity, or in RAID1C3 and
RAID1C4 before the previous patch. Such a block group still fails a
regular mount. Get the end of the last extent from the extent tree when
the capacities differ, as for conventional zones.

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: new. An LLM review of v1 found that v1 failed a degraded mount here,
where it loaded before. A later review found that capping zones with
extents past the capacity let a regular mount accept a stale stripe. The
fix and the commit message were prepared with the help of an LLM, which
also tested it. I reviewed everything.

btrfs sets an active zone limit for devices that report none, so the
zones are finished on almost every device. DUP, RAID0 and RAID10 are not
changed here. RAID0 and RAID10 add up the capacities of their stripes.
In testing with different zone capacities, writes past the smallest one
failed, and so did the next mount. RAID10 also compares the two stripes
of a pair as RAID1 did. I will send fixes for RAID0 and RAID10 with the
RAID10 fixes mentioned in 1/5.

Blocks of the log tree are never in the extent tree. File data is never
only in the log tree here: zoned RAID1 data needs the raid-stripe-tree,
and an fsync of new RAID1 data commits the transaction. A tree-log block
group written past the capacity only by log tree blocks would still be
capped, and log replay would then move the allocation offset past the
capacity. I was not able to get that tested: in my tests on null_blk,
every fsync on a zoned filesystem committed the transaction, also for
changes without new data.

Not changed here, and found only by reading the code: with different
capacities, a crash between the last write to a block group and its zone
finish leaves the smaller zone FULL and the larger one open, at the same
write pointer. A regular mount then activates the block group while
loading it, which warns for metadata and fails the mount for data when
no active zone is left.

 fs/btrfs/zoned.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index ce7c535adff6..48f8391a80a9 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -1536,6 +1536,20 @@ static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
 	for (i = 0; i < map->num_stripes; i++)
 		bg->zone_capacity = min_not_zero(bg->zone_capacity, zone_info[i].capacity);
 
+	/*
+	 * A full zone reports its own capacity as its write pointer. With more
+	 * capacity than the block group, take it as written up to the block
+	 * group's capacity, unless an extent ends past that capacity: then the
+	 * block group was written with a larger one, and the zone keeps its
+	 * write pointer.
+	 */
+	for (i = 0; i < map->num_stripes; i++) {
+		if (zone_info[i].alloc_offset == zone_info[i].capacity &&
+		    zone_info[i].alloc_offset > bg->zone_capacity &&
+		    last_alloc <= bg->zone_capacity)
+			zone_info[i].alloc_offset = bg->zone_capacity;
+	}
+
 	/*
 	 * When the last extent is removed, last_alloc can be smaller than the other write
 	 * pointer. In that case, last_alloc should be moved to the corresponding write
@@ -1920,6 +1934,22 @@ int btrfs_load_block_group_by_raid_type(struct btrfs_block_group *bg,
 	return ret;
 }
 
+/* Whether the zones of the present stripes have different capacities. */
+static bool zone_capacities_differ(const struct btrfs_chunk_map *map,
+				   const struct zone_info *zone_info)
+{
+	u64 capacity = 0;
+
+	for (int i = 0; i < map->num_stripes; i++) {
+		if (!zone_info[i].capacity)
+			continue;
+		if (capacity && zone_info[i].capacity != capacity)
+			return true;
+		capacity = zone_info[i].capacity;
+	}
+	return false;
+}
+
 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
 {
 	struct btrfs_fs_info *fs_info = cache->fs_info;
@@ -1989,6 +2019,19 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
 			set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
 			goto out;
 		}
+	} else if ((map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) &&
+		   zone_capacities_differ(map, zone_info)) {
+		/*
+		 * The RAID1 loader caps a full zone only if no extent
+		 * ends past the block group's capacity.
+		 */
+		ret = calculate_alloc_pointer(cache, &last_alloc, new);
+		if (ret) {
+			btrfs_err(fs_info,
+			"zoned: failed to determine allocation offset of bg %llu",
+				  cache->start);
+			goto out;
+		}
 	}
 
 	ret = btrfs_load_block_group_by_raid_type(cache, map, zone_info, active, last_alloc);
-- 
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 ` Miroslav Cimerman [this message]
2026-11-21 13:45 ` [PATCH v2 4/5] btrfs: zoned: use the highest RAID1 write pointer on degraded mounts Miroslav Cimerman
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-4-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

all inboxes | Powered by JetHome®