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>,
	Josef Bacik <josef@toxicpanda.com>,
	Naohiro Aota <naohiro.aota@wdc.com>,
	Johannes Thumshirn <johannes.thumshirn@wdc.com>
Subject: [PATCH] btrfs: zoned: don't rewind alloc_offset past a later allocation
Date: Mon, 30 Nov 2026 16:38:00 +0000	[thread overview]
Message-ID: <20261130163800.175298-1-mc@raidlab.org> (raw)

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


                 reply	other threads:[~2026-11-30 16:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261130163800.175298-1-mc@raidlab.org \
    --to=mc@raidlab.org \
    --cc=dsterba@suse.com \
    --cc=johannes.thumshirn@wdc.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@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