* [PATCH] btrfs: zoned: don't rewind alloc_offset past a later allocation
@ 2026-11-30 16:38 Miroslav Cimerman
0 siblings, 0 replies; only message in thread
From: Miroslav Cimerman @ 2026-11-30 16:38 UTC (permalink / raw)
To: linux-btrfs
Cc: David Sterba, Chris Mason, Josef Bacik, Naohiro Aota,
Johannes Thumshirn
do_allocation_zoned() takes a range from a zoned block group and
advances alloc_offset. If the block group becomes read-only before
btrfs_add_reserved_bytes(), that returns -EAGAIN, and
__btrfs_add_free_space_zoned() gives the range back by moving
alloc_offset back by its size. After a later allocation, alloc_offset
then points into that allocation's extent:
task A task B
do_allocation_zoned()
[14M, 15M), alloc_offset 15M
do_allocation_zoned()
[15M, 16M), alloc_offset 16M
btrfs_add_reserved_bytes()
(the block group becomes read-only)
btrfs_add_reserved_bytes()
-EAGAIN
btrfs_add_free_space_unused()
alloc_offset 15M
(the block group becomes writable)
the next allocation gets [15M, 16M) again
Metadata, raid stripe tree data and data in conventional zones keep the
allocated address. Two tree blocks at one address abort the transaction
with -EEXIST. On RAID1 with the raid stripe tree, the extent written
first reads back the data of the other one.
On a zoned filesystem, reserve the range even if the block group became
read-only. do_allocation_zoned() took it before that. A range reserved
just before inc_block_group_ro() is in the same state already. Data
waiters still see it: the allocator holds groups_sem until it counted
the reservation. For metadata, relocation and device replace commit the
transaction before they look for tree blocks. Move the range from
bytes_readonly to bytes_reserved and wake waiting tickets.
Fixes: 2eda57089ea3 ("btrfs: zoned: implement sequential extent allocation")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://sashiko.dev/#/patchset/20261129235226.459661-1-mc@raidlab.org?part=3
CC: stable@vger.kernel.org # 6.1+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
Found by Sashiko in its review of [1]: 3/3 there relies on alloc_offset
not moving back. Reproduced with a test-only change that holds one
allocation for 500 ms and makes the block group read-only meanwhile.
Without this patch, two direct writers to one file on RAID1 with the
raid stripe tree left two ranges of the file sharing an extent in 3 of
3 runs. File creation in two subvolumes aborted the transaction with
-EEXIST in 3 of 3 runs. With this patch, 0 of 3 each, and bytes_readonly
was back to its value after each read-only phase.
Data written with zone append and without the raid stripe tree is not
affected: btrfs_finish_ordered_zoned() moves each extent to the address
it was written at. Giving back only the last allocation is not enough:
any other range would stay a hole that metadata writeback cannot pass.
After this patch, find_free_extent() no longer gives a range back on a
zoned filesystem. I can follow up with removing the rewind in
__btrfs_add_free_space_zoned().
The analysis, the fix and the test were done with the help of an LLM. I
reviewed everything.
[1] https://lore.kernel.org/linux-btrfs/20261129235226.459661-1-mc@raidlab.org/
fs/btrfs/block-group.c | 29 +++++++++++++++++++++--------
1 file changed, 21 insertions(+), 8 deletions(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 0736132624f8..d15d7c841511 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -3656,9 +3656,10 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
* @num_bytes: The number of bytes in question
* @delalloc: The blocks are allocated for the delalloc write
*
- * This is called by the allocator when it reserves space. If this is a
- * reservation and the block group has become read only we cannot make the
- * reservation and return -EAGAIN, otherwise this function always succeeds.
+ * This is called by the allocator when it reserves space. On a non-zoned
+ * filesystem, return -EAGAIN if the block group has become read only or the
+ * size class does not match. On a zoned filesystem this function always
+ * succeeds.
*/
int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
u64 ram_bytes, u64 num_bytes, bool delalloc,
@@ -3666,13 +3667,24 @@ int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
{
struct btrfs_space_info *space_info = cache->space_info;
enum btrfs_block_group_size_class size_class;
+ bool zoned_ro = false;
int ret = 0;
spin_lock(&space_info->lock);
spin_lock(&cache->lock);
if (cache->ro) {
- ret = -EAGAIN;
- goto out_error;
+ /*
+ * On a zoned filesystem, do_allocation_zoned() took the range
+ * before the block group became read only. alloc_offset cannot
+ * move back past a later allocation, so reserve the range.
+ * inc_block_group_ro() counted it in bytes_readonly.
+ */
+ if (!btrfs_is_zoned(cache->fs_info)) {
+ ret = -EAGAIN;
+ goto out_error;
+ }
+ space_info->bytes_readonly -= num_bytes;
+ zoned_ro = true;
}
if (btrfs_block_group_should_use_size_class(cache)) {
@@ -3694,10 +3706,11 @@ int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
btrfs_space_info_update_bytes_may_use(space_info, -ram_bytes);
/*
- * Compression can use less space than we reserved, so wake tickets if
- * that happens.
+ * Compression can use less space than we reserved, and a read only
+ * zoned block group no longer counts the range in bytes_readonly. Wake
+ * tickets if either happens.
*/
- if (num_bytes < ram_bytes)
+ if (num_bytes < ram_bytes || zoned_ro)
btrfs_try_granting_tickets(space_info);
spin_unlock(&space_info->lock);
base-commit: 418f7723b545cf064fc584529710b28d1347188e
--
2.53.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-30 16:38 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 16:38 [PATCH] btrfs: zoned: don't rewind alloc_offset past a later allocation Miroslav Cimerman
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox