From 5e79f39fbeea78703ac389c67a810406fa09bc94 Mon Sep 17 00:00:00 2001 From: Johannes Thumshirn Date: Wed, 9 Sep 2026 08:47:25 +0200 Subject: [PATCH 01/12] btrfs: zoned: only change active zone counter on successful (de)activation btrfs_zone_activate() unconditionally decrements the reserved_active_zones counter regardless if btrfs_dev_set_active_zone() fails to set the zone active, i.e. because it raced with another call that already set the bit in the bitmask, or not. This can lead to a double decrement of the counter in case the bit has already been set. Only decrement the counter *iff* btrfs_dev_set_active_zone() successfully marked the zone in the bitmap. Mirror this behaviour when clearing the bit again. Fixes: a7e1ac7bdc5a ("btrfs: zoned: reserve zones for an active metadata/system block group") Reviewed-by: Daniel Vacek Signed-off-by: Johannes Thumshirn Signed-off-by: David Sterba --- fs/btrfs/zoned.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c index 91671aa16e8a..30f1d25206de 100644 --- a/fs/btrfs/zoned.c +++ b/fs/btrfs/zoned.c @@ -1131,11 +1131,13 @@ u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start, return pos; } -static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos) +static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos, bool *new) { struct btrfs_zoned_device_info *zone_info = device->zone_info; unsigned int zno = (pos >> zone_info->zone_size_shift); + *new = false; + /* We can use any number of zones */ if (zone_info->max_active_zones == 0) return true; @@ -1147,23 +1149,29 @@ static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos) if (test_and_set_bit(zno, zone_info->active_zones)) { /* Someone already set the bit */ atomic_inc(&zone_info->active_zones_left); + } else { + *new = true; } } return true; } -static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos) +static bool btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos) { struct btrfs_zoned_device_info *zone_info = device->zone_info; unsigned int zno = (pos >> zone_info->zone_size_shift); /* We can use any number of zones */ if (zone_info->max_active_zones == 0) - return; + return false; - if (test_and_clear_bit(zno, zone_info->active_zones)) + if (test_and_clear_bit(zno, zone_info->active_zones)) { atomic_inc(&zone_info->active_zones_left); + return true; + } + + return false; } int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical, @@ -2416,6 +2424,7 @@ bool btrfs_zone_activate(struct btrfs_block_group *block_group) u64 physical; const bool is_data = (block_group->flags & BTRFS_BLOCK_GROUP_DATA); bool ret; + bool new; int i; if (!btrfs_is_zoned(block_group->fs_info)) @@ -2469,12 +2478,12 @@ bool btrfs_zone_activate(struct btrfs_block_group *block_group) goto out_unlock; } - if (!btrfs_dev_set_active_zone(device, physical)) { + if (!btrfs_dev_set_active_zone(device, physical, &new)) { /* Cannot activate the zone */ ret = false; goto out_unlock; } - if (!is_data) + if (!is_data && new) zinfo->reserved_active_zones--; } @@ -2521,6 +2530,7 @@ static int call_zone_finish(struct btrfs_block_group *block_group, struct btrfs_device *device = stripe->dev; const u64 physical = stripe->physical; struct btrfs_zoned_device_info *zinfo = device->zone_info; + bool cleared; int ret; if (!device->bdev) @@ -2542,9 +2552,9 @@ static int call_zone_finish(struct btrfs_block_group *block_group, return ret; } - if (!(block_group->flags & BTRFS_BLOCK_GROUP_DATA)) + cleared = btrfs_dev_clear_active_zone(device, physical); + if (!(block_group->flags & BTRFS_BLOCK_GROUP_DATA) && cleared) zinfo->reserved_active_zones++; - btrfs_dev_clear_active_zone(device, physical); return 0; } From 8ca411c7ad2c02ae65ae208a3f7483d6e0159c4f Mon Sep 17 00:00:00 2001 From: Johannes Thumshirn Date: Wed, 9 Sep 2026 11:45:34 +0200 Subject: [PATCH 02/12] btrfs: zoned: fix possible UAF in wait_eb_writebacks() wait_eb_writebacks() iterates the fs_info->buffer_tree xarray and waits for the writeback of these extent-buffers. Waiting for writeback needs to be done without the rcu_read_lock() held (because it can sleep) and thus the loop drops the rcu_read_lock() before calling into wait_on_extent_buffer_writeback(). But extent_buffers are freed through RCU, so if btrfs_release_extent_buffer_rcu() is scheduled while we're still waiting on the writeback, the extent_buffer will be freed causing a use-after-free. Similar to what is done in find_extent_buffer_nolock(), get a reference to the extent_buffer before calling into wait_on_extent_buffer_writeback() so a successful writeback does not free the extent_buffer while we still have a reference to it. Fixes: 2dd7e7bc0282 ("btrfs: zoned: wait for extent buffer IOs before finishing a zone") Reviewed-by: Daniel Vacek Signed-off-by: Johannes Thumshirn Signed-off-by: David Sterba --- fs/btrfs/zoned.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c index 30f1d25206de..ddcb0ca5a6bc 100644 --- a/fs/btrfs/zoned.c +++ b/fs/btrfs/zoned.c @@ -2517,8 +2517,16 @@ static void wait_eb_writebacks(struct btrfs_block_group *block_group) continue; if (eb->start >= end) break; + /* + * Skip if we can't get the refcount for the extent buffer, + * i.e. because it was already written back. Otherwise an eb + * disappering underneath us can cause a use-after-free. + */ + if (!refcount_inc_not_zero(&eb->refs)) + continue; rcu_read_unlock(); wait_on_extent_buffer_writeback(eb); + free_extent_buffer(eb); rcu_read_lock(); } rcu_read_unlock(); From c5f878eefdd4f559422a68bf49b6f59e7c10e70e Mon Sep 17 00:00:00 2001 From: Johannes Thumshirn Date: Thu, 10 Sep 2026 14:19:39 +0200 Subject: [PATCH 03/12] btrfs: zoned: requeue block group if zone reset bails out btrfs_reset_unused_block_groups() drops the block-group from fs_info->unused_bgs before performing the work that can actually fail, the actual submission of a REQ_OP_ZONE_RESET. Every failed operation after this leaves the block-group dangling, physically reset, but the in-memory structures aren't and it is not requeued to be reset on fs_info->unused_bgs. Instead of bailing out, re-add this block-group to fs_info->unused_bgs list. Fixes: 453a73c3069a ("btrfs: zoned: reclaim unused zone by zone resetting") Reviewed-by: Daniel Vacek Signed-off-by: Johannes Thumshirn Signed-off-by: David Sterba --- fs/btrfs/zoned.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c index ddcb0ca5a6bc..07bc4fb3407a 100644 --- a/fs/btrfs/zoned.c +++ b/fs/btrfs/zoned.c @@ -3141,6 +3141,8 @@ int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num { struct btrfs_fs_info *fs_info = space_info->fs_info; const sector_t zone_size_sectors = fs_info->zone_size >> SECTOR_SHIFT; + LIST_HEAD(retry_list); + int ret = 0; if (!btrfs_is_zoned(fs_info)) return 0; @@ -3182,11 +3184,10 @@ int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num } if (!found) { spin_unlock(&fs_info->unused_bgs_lock); - return 0; + goto out; } list_del_init(&bg->bg_list); - btrfs_put_block_group(bg); spin_unlock(&fs_info->unused_bgs_lock); /* @@ -3200,7 +3201,6 @@ int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num for (int i = 0; i < map->num_stripes; i++) { struct btrfs_io_stripe *stripe = &map->stripes[i]; unsigned int nofs_flags; - int ret; nofs_flags = memalloc_nofs_save(); ret = blkdev_zone_mgmt(stripe->dev->bdev, REQ_OP_ZONE_RESET, @@ -3210,7 +3210,7 @@ int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num if (ret) { up_read(&fs_info->dev_replace.rwsem); - return ret; + goto requeue; } } up_read(&fs_info->dev_replace.rwsem); @@ -3221,7 +3221,7 @@ int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num if (bg->ro) { spin_unlock(&bg->lock); spin_unlock(&space_info->lock); - continue; + goto requeue; } reclaimed = bg->alloc_offset; @@ -3250,12 +3250,28 @@ int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num btrfs_return_free_space(space_info, reclaimed); spin_unlock(&space_info->lock); + btrfs_put_block_group(bg); + if (num_bytes <= reclaimed) break; num_bytes -= reclaimed; + continue; + +requeue: + list_add_tail(&bg->bg_list, &retry_list); + + if (ret) + goto out; } - return 0; +out: + if (!list_empty(&retry_list)) { + spin_lock(&fs_info->unused_bgs_lock); + list_splice_tail(&retry_list, &fs_info->unused_bgs); + spin_unlock(&fs_info->unused_bgs_lock); + } + + return ret; } void btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct seq_file *seq) From a22acd489f3e882805c3a47d0f75a29704db5411 Mon Sep 17 00:00:00 2001 From: Johannes Thumshirn Date: Fri, 11 Sep 2026 13:41:12 +0200 Subject: [PATCH 04/12] btrfs: zoned: avoid underflow of space info bytes_zone_unusable The two functions btrfs_free_reserved_bytes() and btrfs_reset_unused_block_groups() both manipulate space_info->bytes_zone_unusable without using the accessor function btrfs_space_info_update_bytes_zone_unusable() potentially risking an underflow (in case of btrfs_reset_unused_block_groups()). Use btrfs_space_info_update_bytes_zone_unusable() in both function, in the case of btrfs_free_reserved_bytes() it is mostly for documenting purposes. Reviewed-by: Daniel Vacek Signed-off-by: Johannes Thumshirn Signed-off-by: David Sterba --- fs/btrfs/block-group.c | 2 +- fs/btrfs/zoned.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index 8ff21dde9a46..822d550e77da 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -4114,7 +4114,7 @@ void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, u64 num_bytes, if (bg_ro) space_info->bytes_readonly += num_bytes; else if (btrfs_is_zoned(cache->fs_info)) - space_info->bytes_zone_unusable += num_bytes; + btrfs_space_info_update_bytes_zone_unusable(space_info, num_bytes); space_info->bytes_reserved -= num_bytes; space_info->max_extent_size = 0; diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c index 07bc4fb3407a..1a1d2bf43009 100644 --- a/fs/btrfs/zoned.c +++ b/fs/btrfs/zoned.c @@ -3245,7 +3245,7 @@ int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num ASSERT(reclaimed == bg->zone_capacity, "reclaimed=%llu bg->zone_capacity=%llu", reclaimed, bg->zone_capacity); bg->free_space_ctl->free_space += reclaimed; - space_info->bytes_zone_unusable -= reclaimed; + btrfs_space_info_update_bytes_zone_unusable(space_info, -reclaimed); spin_unlock(&bg->lock); btrfs_return_free_space(space_info, reclaimed); spin_unlock(&space_info->lock); From bcad7df05237382237ad1517411721d839174009 Mon Sep 17 00:00:00 2001 From: Johannes Thumshirn Date: Tue, 27 Oct 2026 15:33:05 +0100 Subject: [PATCH 05/12] btrfs: zoned: don't wait for ordered extents in do_zone_finish() do_zone_finish() calls btrfs_wait_ordered_roots(), which holds fs_info->ordered_operations_mutex while waiting for writeback. It can be reached from writeback itself: cow_file_range() `-> btrfs_reserve_extent() `-> find_free_extent() `-> btrfs_zone_finish_one_bg() `-> do_zone_finish() If another task holds the mutex and waits for that writeback, this deadlocks. In general, we want to ensure all writes in the block group to complete before finishing the zone, as relocation does. However, the current callers do not need the wait (or cannot wait) in do_zone_finish(): - Relocation already waits for reservations and ordered extents itself before calling btrfs_zone_finish(). - btrfs_delete_unused_bgs() checks that ->reserved is zero and sets the block group read-only while holding groups_sem, so there can be no in-flight data write. - btrfs_zone_finish_one_bg() is called from the allocation path and cannot wait. Dropping the wait is still safe. Once the block group is read-only, ->reserved can only drop, and for data it only drops after the ordered extent's I/O has completed. So if ->reserved is zero, no data write is left in flight. If it is non-zero, do_zone_finish() returns -EAGAIN, which all callers handle. This makes btrfs_zone_finish_one_bg() fail with -EAGAIN when it races with an allocation in the selected block group. Fixes: 393f646e34c1 ("btrfs: zoned: finish least available block group on data bg allocation") Reviewed-by: Naohiro Aota Signed-off-by: Johannes Thumshirn Signed-off-by: David Sterba --- fs/btrfs/zoned.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c index 1a1d2bf43009..bd1e73d9ff53 100644 --- a/fs/btrfs/zoned.c +++ b/fs/btrfs/zoned.c @@ -2610,9 +2610,10 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ /* Ensure all writes in this block group finish */ btrfs_wait_block_group_reservations(block_group); - /* No need to wait for NOCOW writers. Zoned mode does not allow that */ - btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group); - /* Wait for extent buffers to be written. */ + /* + * No need to wait for NOCOW writers, as zoned mode does not + * allow that, only wait for extent buffers to be written. + */ if (is_metadata) wait_eb_writebacks(block_group); From 04df03585f0d8ef2727e043119bba2cdfac2d8d7 Mon Sep 17 00:00:00 2001 From: Johannes Thumshirn Date: Wed, 28 Oct 2026 12:57:27 +0100 Subject: [PATCH 06/12] btrfs: zoned: fix lockdep splat on mount When mounting a filesystem on a zoned drive passed into a VM via a virtio-blk disk lockdep sporadically complains about a possible deadlock between BTRFS' dev_replace::rwsem and virtio-blk's vdev_mutex: ====================================================== WARNING: possible circular locking dependency detected 7.3.0-rc6+ #317 Not tainted ------------------------------------------------------ mount/7418 is trying to acquire lock: ffff888102c07860 (&vblk->vdev_mutex){+.+.}-{4:4}, at: virtblk_report_zones+0xda/0x440 but task is already holding lock: ffff8881fd725ea8 (&fs_info->dev_replace.rwsem){++++}-{4:4}, at: btrfs_load_block_group_zone_info+0x186/0xe50 which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #4 (&fs_info->dev_replace.rwsem){++++}-{4:4}: down_read+0x42/0x170 btrfs_map_block+0x1a6/0xe70 btrfs_submit_bbio+0x1a3/0x910 submit_one_bio+0x93/0xd0 btrfs_readahead+0x1c4/0x200 read_pages+0x56/0x200 page_cache_ra_order+0x27c/0x410 filemap_get_pages+0x12f/0x4a0 filemap_read+0xed/0x400 vfs_read+0x240/0x340 ksys_read+0x61/0xd0 do_syscall_64+0xa3/0x4f0 entry_SYSCALL_64_after_hwframe+0x76/0x7e -> #3 (mapping.invalidate_lock#3){++++}-{4:4}: down_read+0x42/0x170 filemap_fault+0xb0/0x1a40 __do_fault+0x34/0xc0 __handle_mm_fault+0x919/0x1db0 handle_mm_fault+0x97/0x240 do_user_addr_fault+0x21d/0x690 exc_page_fault+0x8e/0x230 asm_exc_page_fault+0x26/0x30 -> #2 (vm_lock){++++}-{0:0}: __vma_start_exclude_readers+0x91/0x1b0 __vma_start_write+0x47/0xd0 vma_expand+0x4c/0x2a0 relocate_vma_down+0x126/0x200 setup_arg_pages+0x240/0x3e0 load_elf_binary+0x1be/0x1860 bprm_execve+0x2d2/0x7b0 kernel_execve+0x107/0x130 kernel_init+0xc2/0x130 ret_from_fork+0x1ff/0x2f0 ret_from_fork_asm+0x1a/0x30 -> #1 (&mm->mmap_lock){++++}-{4:4}: __might_fault+0x68/0xa0 _copy_to_user+0x22/0x70 blkdev_copy_zone_to_user+0x22/0x40 virtblk_report_zones+0x297/0x440 blkdev_report_zones_ioctl+0xfd/0x130 blkdev_ioctl+0x20f/0x2c0 __x64_sys_ioctl+0x86/0xd0 do_syscall_64+0xa3/0x4f0 entry_SYSCALL_64_after_hwframe+0x76/0x7e -> #0 (&vblk->vdev_mutex){+.+.}-{4:4}: __lock_acquire+0x15a8/0x2850 lock_acquire+0xea/0x2f0 __mutex_lock+0xa7/0x10d0 virtblk_report_zones+0xda/0x440 blkdev_report_zones_cached+0x162/0x190 btrfs_get_dev_zones+0xdc/0x2c0 btrfs_load_block_group_zone_info+0x2b6/0xe50 btrfs_read_block_groups+0x338/0xc80 open_ctree+0x1aca/0x24b3 btrfs_get_tree.cold+0x105/0x26c vfs_get_tree+0x28/0xb0 __do_sys_fsconfig+0x324/0x690 do_syscall_64+0xa3/0x4f0 entry_SYSCALL_64_after_hwframe+0x76/0x7e other info that might help us debug this: Chain exists of: &vblk->vdev_mutex --> mapping.invalidate_lock#3 --> &fs_info->dev_replace.rwsem Possible unsafe locking scenario: CPU0 CPU1 ---- ---- rlock(&fs_info->dev_replace.rwsem); lock(mapping.invalidate_lock#3); lock(&fs_info->dev_replace.rwsem); lock(&vblk->vdev_mutex); *** DEADLOCK *** locks held by mount/7418: 3, last CPU#0: #0: ffff888110aa6868 (&fc->uapi_mutex){+.+.}-{4:4}, at: __do_sys_fsconfig+0x2ae/0x690 #1: ffff888112ba20e8 (&type->s_umount_key#31/1){+.+.}-{4:4}, at: sget_fc+0x148/0x8d0 #2: ffff8881fd725ea8 (&fs_info->dev_replace.rwsem){++++}-{4:4}, at: btrfs_load_block_group_zone_info+0x186/0xe50 Holding the rwsem was widened in 0090d6e1b210 ("btrfs: zoned: fix use-after-free due to race with dev replace"), but it now also covers calling into blkdev_report_zones_cached(). The device replace ioctl() only works on a mounted filesystem and the only caller with setting 'new' to false is read_one_block_group() which is only called during mount, so it is safe to drop the rwsem before calling into blkdev_report_zones_cached(). Fixes: 0090d6e1b210 ("btrfs: zoned: fix use-after-free due to race with dev replace") Reviewed-by: Naohiro Aota Signed-off-by: Johannes Thumshirn Signed-off-by: David Sterba --- fs/btrfs/zoned.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c index bd1e73d9ff53..bff0c98a3684 100644 --- a/fs/btrfs/zoned.c +++ b/fs/btrfs/zoned.c @@ -1393,11 +1393,13 @@ static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx, return 0; } + ASSERT(!test_bit(BTRFS_FS_OPEN, &fs_info->flags)); + up_read(&dev_replace->rwsem); + nofs_flag = memalloc_nofs_save(); ret = btrfs_get_dev_zone(device, info->physical, &zone); memalloc_nofs_restore(nofs_flag); if (ret) { - up_read(&dev_replace->rwsem); if (ret != -EIO && ret != -EOPNOTSUPP) return ret; info->alloc_offset = WP_MISSING_DEV; @@ -1409,7 +1411,6 @@ static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx, "zoned: unexpected conventional zone %llu on device %s (devid %llu)", zone.start << SECTOR_SHIFT, rcu_dereference(device->name), device->devid); - up_read(&dev_replace->rwsem); return -EIO; } @@ -1437,8 +1438,6 @@ static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx, break; } - up_read(&dev_replace->rwsem); - return 0; } From bb66a38f5812f2c04abd4eb6e586a12b5a95172a Mon Sep 17 00:00:00 2001 From: Filipe Manana Date: Wed, 28 Oct 2026 16:06:27 +0000 Subject: [PATCH 07/12] btrfs: zstd: prevent leak of workspace below maximum level When putting a workspace we always protect the workspace for the maximum level from reclaim, since it can be used not just for that level but as well for any level below it. However the logic is flawed because if we do the put for a workspace at level 10 for example (maximum is level 15) and there is currently no idle workspace for the maximum level because all are currently being used, we don't add the workspace to the LRU list and that can result in leaking it if we don't get more compression or decompression requests for level 10 or any level below 10. Fix this by skipping the addition to the LRU list only if the level of the workspace matches the maximum level too (besides the list of idle workspaces for the maximum level being empty). Fixes: 3f93aef535c8 ("btrfs: add zstd compression level support") Reviewed-by: Qu Wenruo Signed-off-by: Filipe Manana Reviewed-by: David Sterba Signed-off-by: David Sterba --- fs/btrfs/zstd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/zstd.c b/fs/btrfs/zstd.c index cc92d0b1b948..9b7c7f43527f 100644 --- a/fs/btrfs/zstd.c +++ b/fs/btrfs/zstd.c @@ -347,7 +347,8 @@ void zstd_put_workspace(struct btrfs_fs_info *fs_info, struct list_head *ws) /* A node is only taken off the lru if we are the corresponding level */ if (clip_level(workspace->req_level) == workspace->level) { /* Hide a max level workspace from reclaim */ - if (list_empty(&zwsm->idle_ws[ZSTD_BTRFS_MAX_LEVEL - 1])) { + if (workspace->level == ZSTD_BTRFS_MAX_LEVEL - 1 && + list_empty(&zwsm->idle_ws[ZSTD_BTRFS_MAX_LEVEL - 1])) { INIT_LIST_HEAD(&workspace->lru_list); } else { workspace->last_used = jiffies; From 9a58e56e70f610e29b53821e9c706600e529cca3 Mon Sep 17 00:00:00 2001 From: Cheng Yongkang Date: Sat, 31 Oct 2026 23:25:39 -0700 Subject: [PATCH 08/12] btrfs: reject refs=0 subvolume without DEAD in SUBVOL_SYNC_WAIT btrfs_ioctl_subvol_sync() assumes that a cached root with zero references has BTRFS_ROOT_SUBVOL_DEAD set, and asserts that before waiting for the cleaner. Two states break the assumption: - A corrupt ROOT_ITEM with zero references and a matching orphan item, on a writable filesystem. - A failed subvolume deletion. btrfs_delete_subvolume() zeroes the root references, aborts the transaction, which forces the filesystem read-only, and then clears DEAD again. No on-disk metadata is corrupted in this case. With CONFIG_BTRFS_ASSERT=y, both reach a BUG(). Change the ASSERT() into a proper rejection to avoid triggering a BUG(). Fixes: 6c83d153ed86 ("btrfs: add new ioctl to wait for cleaned subvolumes") CC: stable@vger.kernel.org Suggested-by: Qu Wenruo Link: https://lore.kernel.org/linux-btrfs/dd97efe9-91ec-495b-b698-acff1447421c@gmx.com/ Signed-off-by: Cheng Yongkang [ Rework the commit message, drop the ad-hoc RW check. ] Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba --- fs/btrfs/ioctl.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 847fdc19bd40..000276bb49ad 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -5161,8 +5161,12 @@ static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *a return -ENOENT; wait_for_deletion = true; - ASSERT(root_flags & BTRFS_ROOT_SUBVOL_DEAD, "root_flags=0x%llx", - root_flags); + if (unlikely(!(root_flags & BTRFS_ROOT_SUBVOL_DEAD))) { + btrfs_err(fs_info, + "subvol %llu has refs 0 without DEAD flag", + args.subvolid); + return -EUCLEAN; + } sched_ret = schedule_timeout_interruptible(HZ); /* Early wake up or error. */ if (sched_ret != 0) From 8416012aa6e794a6464e073ab236f4ff862935ac Mon Sep 17 00:00:00 2001 From: Cheng Yongkang Date: Sun, 1 Nov 2026 00:14:44 -0700 Subject: [PATCH 09/12] btrfs: handle zero num_stripes in btrfs_chunk_item_size() validate_sys_chunk_array() calls btrfs_chunk_item_size() with the on-disk num_stripes before btrfs_check_chunk_valid() has checked the chunk. With CONFIG_BTRFS_ASSERT=y, a zero-stripe system chunk trips ASSERT(num_stripes) instead of being rejected: assertion failed: num_stripes, in fs/btrfs/volumes.h:714 kernel BUG at fs/btrfs/volumes.h:714! RIP: 0010:btrfs_validate_super+0x3ce7/0x4210 Call Trace: open_ctree btrfs_get_tree vfs_get_tree do_new_mount Drop the assertion and compute the size as the chunk header plus num_stripes stripes. The size is unchanged for num_stripes > 0, and a zero-stripe chunk now reaches btrfs_check_chunk_valid(), which rejects it, so the mount fails with -EUCLEAN. Suggested-by: Qu Wenruo Link: https://lore.kernel.org/linux-btrfs/d2ee0cc1-674e-4447-88da-d346122a43a4@suse.com/ Fixes: 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()") Signed-off-by: Cheng Yongkang [ Slightly rework the commit message. ] Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba --- fs/btrfs/volumes.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h index 6eddda239221..1c4cfb21ad12 100644 --- a/fs/btrfs/volumes.h +++ b/fs/btrfs/volumes.h @@ -708,9 +708,8 @@ static inline enum btrfs_map_op btrfs_op(const struct bio *bio) static inline unsigned long btrfs_chunk_item_size(int num_stripes) { - ASSERT(num_stripes); - return sizeof(struct btrfs_chunk) + - sizeof(struct btrfs_stripe) * (num_stripes - 1); + return offsetof(struct btrfs_chunk, stripe) + + sizeof(struct btrfs_stripe) * num_stripes; } /* From fb6eeb6a4bba0e027cf772859a3455ef5bacceb8 Mon Sep 17 00:00:00 2001 From: Qu Wenruo Date: Mon, 2 Nov 2026 19:58:37 +1030 Subject: [PATCH 10/12] btrfs: do not insert the data reloc tree into fs_roots_radix The reasons not to do so are: - There is already a global pointer fs_info->data_reloc_root And data reloc tree itself has TRACK_DIRTY flag set, thus its treated as a COW-only root, not a SHAREABLE root, unlike regular subvolume trees. - 32-bit conflicts Radix tree keys are unsigned longs, and therefore on 32-bit platforms they are 32 bits wide This means a subvolume with id 0xfffffff7, will conflict with the truncated data reloc tree id (0xfffffffffffffff7 -> 0xfffffff7). This will prevent btrfs from reading or creating such subvolume. Thankfully this should be very rare, as the subvolume id is reusable, and it's very hard to keep so many subvolumes before hitting other limits (e.g. anon_dev limits). So change the code to not insert data reloc tree into fs_roots_radix: - Load the tree with a dedicated helper The new helper, read_data_reloc_root() will call btrfs_read_tree_root(), with refs check, then call btrfs_init_fs_root(). It skips the fs_roots_radix tree insert completely. - Grab data_reloc_tree as a global root Reviewed-by: Filipe Manana Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba --- fs/btrfs/disk-io.c | 53 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index 91eceb1ab4f9..4356e0b3368c 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -1169,6 +1169,8 @@ static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, return btrfs_grab_root(fs_info->stripe_root); case BTRFS_REMAP_TREE_OBJECTID: return btrfs_grab_root(fs_info->remap_root); + case BTRFS_DATA_RELOC_TREE_OBJECTID: + return btrfs_grab_root(fs_info->data_reloc_root); default: return NULL; } @@ -1304,8 +1306,15 @@ static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, * * This is namely for free-space-tree and quota tree, which can change * at runtime and should only be grabbed from fs_info. + * + * The data reloc tree is also such a tree. It is loaded at mount time + * by read_data_reloc_root() and never inserted into the radix tree, as + * radix tree keys are of type `unsigned long`, which is 32 bits wide + * on 32-bit platforms. + * Casting the objectid to `unsigned long` can therefore result in a + * conflict with a subvolume. */ - if (!btrfs_is_fstree(objectid) && objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) + if (!btrfs_is_fstree(objectid)) return ERR_PTR(-ENOENT); again: root = btrfs_lookup_fs_root(fs_info, objectid); @@ -1409,6 +1418,39 @@ struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); } +/* Read and initialize the data reloc tree. */ +static struct btrfs_root *read_data_reloc_root(struct btrfs_fs_info *fs_info) +{ + struct btrfs_key key = { + .objectid = BTRFS_DATA_RELOC_TREE_OBJECTID, + .type = BTRFS_ROOT_ITEM_KEY, + .offset = (u64)-1, + }; + struct btrfs_root *root; + int ret; + + root = btrfs_read_tree_root(fs_info->tree_root, &key); + if (IS_ERR(root)) + return root; + + if (btrfs_root_refs(&root->root_item) == 0) { + ret = -ENOENT; + goto fail; + } + + /* + * This tree can share blocks with some other fs tree during + * relocation and we need a proper setup by btrfs_init_fs_root(). + */ + ret = btrfs_init_fs_root(root, 0); + if (ret) + goto fail; + return root; +fail: + btrfs_put_root(root); + return ERR_PTR(ret); +} + /* * Return a root for the given objectid. * @@ -2280,12 +2322,7 @@ static int btrfs_read_roots(struct btrfs_fs_info *fs_info) PTR_ERR(root)); } } else { - /* - * This tree can share blocks with some other fs tree during - * relocation and we need a proper setup by btrfs_get_fs_root(). - */ - root = btrfs_get_fs_root(tree_root->fs_info, - BTRFS_DATA_RELOC_TREE_OBJECTID, true); + root = read_data_reloc_root(fs_info); if (IS_ERR(root)) { if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; @@ -3835,8 +3872,6 @@ fail_block_groups: btrfs_put_block_group_cache(fs_info); fail_tree_roots: - if (fs_info->data_reloc_root) - btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); free_root_pointers(fs_info, true); invalidate_and_check_btree_folios(fs_info); From a1250669825f96ecc7045ab1b10cdc3d8832339d Mon Sep 17 00:00:00 2001 From: Siddharth Mishra Date: Mon, 2 Nov 2026 23:10:21 -0700 Subject: [PATCH 11/12] btrfs: tree-checker: reject extent owner ref without simple quota [BUG] An extent item can contain an owner ref. The owner ref is correct only when the super block has the SIMPLE_QUOTA bit. If the bit is not set, check_extent_item() gives a WARN_ON() and then accepts the leaf : case BTRFS_EXTENT_OWNER_REF_KEY: WARN_ON(!btrfs_fs_incompat(fs_info, SIMPLE_QUOTA)); break; For all other incorrect inline refs, check_extent_item() returns -EUCLEAN. After the tree-checker accepts the leaf, a free of the extent calls btrfs_get_extent_inline_ref_type(). This function ASSERT()s the SIMPLE_QUOTA bit. On a kernel with CONFIG_BTRFS_ASSERT=y, a free of such an extent gives : WARNING: fs/btrfs/tree-checker.c:1782 at __btrfs_check_leaf assertion failed: btrfs_fs_incompat(fs_info, SIMPLE_QUOTA), in fs/btrfs/extent-tree.c:354 kernel BUG at fs/btrfs/extent-tree.c:354! Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI RIP: 0010:btrfs_get_extent_inline_ref_type.cold (fs/btrfs/extent-tree.c:354) Call Trace: lookup_inline_extent_backref (fs/btrfs/extent-tree.c:919) lookup_extent_backref (fs/btrfs/extent-tree.c:1100) __btrfs_free_extent.isra.0 (fs/btrfs/extent-tree.c:3319) __btrfs_run_delayed_refs (fs/btrfs/extent-tree.c:2134) btrfs_run_delayed_refs (fs/btrfs/extent-tree.c:2246) btrfs_commit_transaction (fs/btrfs/transaction.c:2278) ksys_sync (fs/sync.c:104) Without CONFIG_BTRFS_ASSERT, the kernel gives only the WARN_ON(). Then btrfs continues to use the owner ref. [CAUSE] I made a crafted image : an owner ref, no SIMPLE_QUOTA bit, and no quota tree. I removed the file of the extent. At the next commit, btrfs frees the extent and gives the BUG above. For an owner ref without the SIMPLE_QUOTA bit, the tree-checker gives a WARN_ON() and keeps the leaf. For all other inline ref types, it rejects the leaf. Two sources can put owner refs without the SIMPLE_QUOTA bit on a disk : a damaged or crafted image, and a kernel without commit f2363e6fcc79 ("btrfs: fix transaction atomicity bug when enabling simple quotas"). [FIX] Reject the leaf with -EUCLEAN, as for all other incorrect inline refs. Then btrfs finds the problem when it reads the leaf, and the ASSERT() is not reached. Link: https://syzkaller.appspot.com/bug?extid=d7d1fc7e21835ca19219 Link: https://syzkaller.appspot.com/bug?extid=4983e68a6d77180bfe33 Reviewed-by: Qu Wenruo Signed-off-by: Siddharth Mishra Reviewed-by: David Sterba Signed-off-by: David Sterba --- fs/btrfs/tree-checker.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c index 9cd79d97b9b5..a2aa4013e082 100644 --- a/fs/btrfs/tree-checker.c +++ b/fs/btrfs/tree-checker.c @@ -1766,7 +1766,11 @@ static int check_extent_item(struct extent_buffer *leaf, inline_refs += btrfs_shared_data_ref_count(leaf, sref); break; case BTRFS_EXTENT_OWNER_REF_KEY: - WARN_ON(!btrfs_fs_incompat(fs_info, SIMPLE_QUOTA)); + if (unlikely(!btrfs_fs_incompat(fs_info, SIMPLE_QUOTA))) { + extent_err(leaf, slot, + "extent owner ref without simple quota"); + return -EUCLEAN; + } break; default: extent_err(leaf, slot, "unknown inline ref type: %u", From d27d0039bc1334be8a72c769e14c0b0c3efc317d Mon Sep 17 00:00:00 2001 From: Mani Tofigh Date: Tue, 3 Nov 2026 15:03:21 -0500 Subject: [PATCH 12/12] btrfs: fix bio counter and bioc leak when async checksumming fails A single failed checksum allocation can make a later device replace hang forever. btrfs_submit_chunk() takes a dev-replace bio counter reference and a bioc before it hands a write to the checksum worker. If the checksum fails there (e.g., its allocation fails), run_one_async_done() ends the bio but drops neither. The counter then never gets back to zero and the next replace waits for it: [ 8.369] BTRFS info (device ram0): dev_replace from /dev/ram0 (devid 1) to /dev/ram1 finished [41.534] INFO: task btrfscsum:80 blocked for more than 20 seconds. [41.534] btrfs_rm_dev_replace_blocked+0x135/0x180 [41.534] btrfs_dev_replace_finishing+0xefe/0x13b0 The worker is only used when CONFIG_BTRFS_EXPERIMENTAL is off and the checksum isn't a fast one, e.g., sha256. We hit it on a sha256 filesystem. We put the bioc and drop the counter in that error branch. We also put the bioc on the synchronous error path and on the btrfs_split_bio() error path, which both only drop the counter. With the fix, the replace doesn't hang anymore. This bug was identified and confirmed with the help of fault-injection testing on a deterministic hypervisor currently under development. Our C reproducer however hits it within about 35s. Fixes: f8a53bb58ec7 ("btrfs: handle checksum generation in the storage layer") Fixes: c7c97ceff98c ("btrfs: handle bio_split() errors") CC: stable@vger.kernel.org # 6.6+ Reviewed-by: Qu Wenruo Signed-off-by: Mani Tofigh Reviewed-by: David Sterba Signed-off-by: David Sterba --- fs/btrfs/bio.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/btrfs/bio.c b/fs/btrfs/bio.c index 2c6234f6182f..2e12a6d7e16f 100644 --- a/fs/btrfs/bio.c +++ b/fs/btrfs/bio.c @@ -624,6 +624,8 @@ static void run_one_async_done(struct btrfs_work *work, bool do_free) /* If an error occurred we just want to clean up the bio and move on. */ if (bio->bi_status) { + btrfs_put_bioc(async->bioc); + btrfs_bio_counter_dec(async->bbio->inode->root->fs_info); btrfs_bio_end_io(async->bbio, bio->bi_status); return; } @@ -762,6 +764,7 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num) split = btrfs_split_bio(fs_info, bbio, map_length); if (IS_ERR(split)) { status = errno_to_blk_status(PTR_ERR(split)); + btrfs_put_bioc(bioc); btrfs_bio_counter_dec(fs_info); goto end_bbio; } @@ -822,6 +825,7 @@ done: return map_length == length; fail: + btrfs_put_bioc(bioc); btrfs_bio_counter_dec(fs_info); /* * We have split the original bbio, now we have to end both the current