From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.70.43.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796056703; b=3bNoVfg7iNgYcwbV4mfQCpvIpDM0S/+HzPI2ncUXOMbVke0OcCbWCm+ny1hsFYV8ghrP N8gGxH3krGx30ekBLOcqRqyAcqI29YGLoAegamOG7EU7ZKeQqkLltpS2eLjiCIEltNu6p +IWNEgybWS5uLl74KM5GhDr0H4/5001Ast0ZMTIGEkgNpXmaZeXKR5cNxnxmPQMhvOt5i f24Y5EUsJl2imtVZ5CnR+oh9eWLRhV3wpZkjUFtvQyEUm40A7gDX7zWVIzoxYOFUOud1k 6AWyA5RUrgxy0sjfQHIGMrcd/uQnSDXTzp+vpnoq/YcXVfQ62YXq1JoOrGcIGQKaUog== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796056703; h=DMARC-Filter:DKIM-Signature:X-Pm-Submission-Id:From:To:Cc:Subject: Date:Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=0XhvYxZRQ5wahpTz3GLPrnC0Iv4Md02c+OtznwQ46AQ=; b=cWHeXbYWE0xAR/TDvUwiEZwZiv7QQJ8/NOLR6mX9GeIAsM60lmI4o2qknEnzDm2bG8br B94TNXjCLFrAMct6tzJZ9GB7nJMCyT2CITcqjRPLtKpSyINWqOtflXaj6NPDgFRZZDMiD vJIJVvW5xXS2KlKJKe9rP6saZmWFMUQD3J5I/RHdaGwmoTau6b7RTzK6BzKEMjzAxoxHu L1Ur5HW+Ut/NslU9A+uSmJWlsUJWWJ9k1rXzFakpEQGGjp8kUCmaCeMYGfuZTt3ifX8qc 9nnXhJ+uDDzzYOM9PNKgW08TwtkBcOhHLHeZCx4XlQ5JoLXRJfnAOsr3MZcMYSIbkAA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=raidlab.org; spf=pass smtp.mailfrom=raidlab.org; dkim=pass header.d=raidlab.org header.i=@raidlab.org header.a=rsa-sha256 header.s=protonmail header.b=qKAVC3dt; arc=none smtp.remote-ip=185.70.43.172 Received: from mail-43172.protonmail.ch (mail-43172.protonmail.ch [185.70.43.172]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 551F0606A31C for ; Mon, 30 Nov 2026 16:38:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 551F0606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=raidlab.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=raidlab.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=raidlab.org header.i=@raidlab.org header.a=rsa-sha256 header.s=protonmail header.b=qKAVC3dt DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raidlab.org; s=protonmail; t=1796056699; x=1796315899; bh=0XhvYxZRQ5wahpTz3GLPrnC0Iv4Md02c+OtznwQ46AQ=; h=From:To:Cc:Subject:Date:Message-ID:From:To:Cc:Date:Subject: Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=qKAVC3dtQtw8912NhUcmUhLkSLIKa6isK2lqeTGMVnhK0/lEzuC9PxLyD0arkStZI dmC1mWuXUWPqOKxxPPKwu6OAJZtP6G61voB24iwnGyDx4nQWeMgHdigacanlQ9pBkC 93qLsSq5BFw4wfAkHTcRu12KZUCHjt50pIsinZ7aU8TmPLiWyzKieo593H6D35wNq2 rTY7InXH2YWArBVXwlr/02eThePs8mY30rocD8sFRcpoEMij1XFSC71qCfDwuHM96I WB3PX7NPRFExpOZOpE01OFi/2sqxfz/p26bDEetpGpbBemfJrj6cwFjnvmnHcVEQV+ 5QDuCZOiYF8jQ== X-Pm-Submission-Id: 4jdV7s6mT2z1DFF4 From: Miroslav Cimerman To: linux-btrfs@vger.kernel.org Cc: David Sterba , Chris Mason , Josef Bacik , Naohiro Aota , Johannes Thumshirn Subject: [PATCH] btrfs: zoned: don't rewind alloc_offset past a later allocation Date: Mon, 30 Nov 2026 16:38:00 +0000 Message-ID: <20261130163800.175298-1-mc@raidlab.org> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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 --- 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