* [PATCH v3 00/17] btrfs: remove the v1 space cache
@ 2026-09-17 3:59 Tal Zussman
2026-09-17 3:59 ` [PATCH v3 01/17] btrfs: stop enabling the v1 space cache from the on-disk state Tal Zussman
` (16 more replies)
0 siblings, 17 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 3:59 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
Since commit 545e560a5b0f ("btrfs: disable v1 space cache") the mount
options can't select the v1 space cache anymore, but the code is all
still there, and a filesystem with an old cache and no free space tree
still enabled it from the superblock. Qu suggested removing it rather
than converting its page handling to folios [1].
Patch 1 stops enabling the cache from the on-disk state, so an existing
cache is cleaned up on the next read-write mount, as -o nospace_cache
already did. This is the one user-visible change: the cleanup is now
unconditional, and a read-write mount fails if it fails. There are two
ways to fix this failure:
1. Convert to the free space tree during mount (the recommended
conversion), but this could fail for the same reason.
2. Run 'btrfs rescue clear-space-cache v1' on an unmounted filesystem.
Patches 2-6 remove the write path, 7 and 8 the load path and
disk_cache_state, and 9 and 10 the SPACE_CACHE flag and the unused half
of the cleanup helper. Patches 11-16 remove the trimming ranges and the
free space inode special cases in the write path, which only the v1
writer used. Patch 17 removes TRANS_JOIN_NOLOCK, which the space cache
was the only user of.
What's left is what's needed to find and delete the cache inodes of an
existing filesystem:
1. lookup_free_space_inode(), btrfs_remove_free_space_inode(), and
btrfs_cleanup_free_space_cache_v1(), which runs on the first
read-write mount and zeroes cache_generation in the super block.
2. btrfs_truncate_free_space_cache() and delete_v1_space_cache(), which
relocation uses to get a cache inode's extents out of a block group.
3. btrfs_is_free_space_inode(), for the evict and inode update paths.
4. The on-disk definitions: cache_generation in the super block,
BTRFS_FREE_SPACE_OBJECTID, and the free space header and entry
items.
space_cache and space_cache=v1 still fall back to nospace_cache with a
warning.
This also removes the page-based I/O in the free space code.
btrfs_io_ctl used a struct page array and was one of the last users
of clear_page_dirty_for_io().
Tested with fstests (btrfs and generic quick groups), with and without
the free space tree, with no regressions against the base kernel.
Upgrading from a filesystem with an existing v1 cache written by a 6.8
kernel was also tested. The cache is removed on the first read-write
mount and on remount from read-only, and the resulting filesystem is
clean under btrfs check and still mountable by the old kernel.
Based on for-7.3-rc2.
[1] https://lore.kernel.org/linux-btrfs/e1dd5a75-9a67-4bc6-b8d8-bfff79b5b907@suse.com/
---
Changes in v3:
- Patch 5 (new): Keep the cache_write_mutex locking that serializes
btrfs_start_dirty_block_groups() with btrfs_remove_block_group(),
since it also protects the block group item update and not just the
cache writeout. Rename it to dirty_bgs_update_mutex.
- Patch 8: Drop the "Old style space inode found, converting." branch
from lookup_free_space_inode(), as it no longer converts anything.
- Link to v2: https://patch.msgid.link/20260913-btrfs-remove-v1-space-cache-v2-0-186767a7e106@columbia.edu
Changes in v2:
- Split the TRANS_JOIN_NOLOCK removal out of the ordered extent patch
into its own patch at the end of the series, per David
- Add recovery information for a mount failure due to patch 1 to the
cover letter.
- Link to v1: https://patch.msgid.link/20260907-btrfs-remove-v1-space-cache-v1-0-5f9a5ba352a7@columbia.edu
---
Tal Zussman (17):
btrfs: stop enabling the v1 space cache from the on-disk state
btrfs: remove the v1 space cache writeout from the transaction commit
btrfs: remove the free space cache endio workqueue
btrfs: remove the v1 space cache write path
btrfs: rename cache_write_mutex to dirty_bgs_update_mutex
btrfs: drop the transaction handle from the prealloc helpers
btrfs: remove the v1 space cache load path
btrfs: remove btrfs_disk_cache_state
btrfs: remove the SPACE_CACHE mount option flag
btrfs: replace btrfs_set_free_space_cache_v1_active() with a cleanup helper
btrfs: remove the free space cache trimming ranges
btrfs: remove BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE
btrfs: remove the free space inode ordered extent special cases
btrfs: remove the free space inode special cases from the COW paths
btrfs: stop special-casing free space inodes in the delalloc accounting
btrfs: stop reading free space inodes from the commit root
btrfs: remove TRANS_JOIN_NOLOCK
fs/btrfs/block-group.c | 491 ++-----------
fs/btrfs/block-group.h | 14 -
fs/btrfs/btrfs_inode.h | 6 -
fs/btrfs/delalloc-space.c | 13 +-
fs/btrfs/disk-io.c | 81 +--
fs/btrfs/file-item.c | 11 -
fs/btrfs/free-space-cache.c | 1630 +++++--------------------------------------
fs/btrfs/free-space-cache.h | 29 +-
fs/btrfs/fs.h | 2 -
fs/btrfs/inode.c | 149 +---
fs/btrfs/ordered-data.c | 27 +-
fs/btrfs/relocation.c | 2 +-
fs/btrfs/space-info.c | 2 -
fs/btrfs/space-info.h | 4 -
fs/btrfs/super.c | 48 +-
fs/btrfs/transaction.c | 61 +-
fs/btrfs/transaction.h | 29 +-
fs/btrfs/zoned.c | 9 -
18 files changed, 273 insertions(+), 2335 deletions(-)
---
base-commit: 09f1294ee2abee7fe1c2d600671498b7642e0fe0
change-id: 20260906-btrfs-remove-v1-space-cache-45cf57c957a2
Best regards,
--
Tal Zussman <tz2294@columbia.edu>
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v3 01/17] btrfs: stop enabling the v1 space cache from the on-disk state
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
@ 2026-09-17 3:59 ` Tal Zussman
[not found] ` <20260917041202.74A691F00893@smtp.kernel.org>
2026-09-17 3:59 ` [PATCH v3 02/17] btrfs: remove the v1 space cache writeout from the transaction commit Tal Zussman
` (15 subsequent siblings)
16 siblings, 1 reply; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 3:59 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
Since commit 545e560a5b0f ("btrfs: disable v1 space cache") the mount
options can no longer request the v1 space cache, but a filesystem with
an active v1 cache and no free space tree still enables it from
cache_generation, and remount does the same. Drop both, so SPACE_CACHE
can never be set.
btrfs_start_pre_rw_mount() then sees the on-disk cache as active but
unwanted and cleans it up, as -o nospace_cache does today. That covers
the read-only to read-write remount as well, so drop the toggle in
btrfs_remount_cleanup(), which would otherwise start a transaction on
remounts of a read-only filesystem with an old cache.
The cleanup is now unconditional, and the first read-write mount fails
if it fails, as it did with -o nospace_cache. This also lets an old
filesystem mount without options when the page size is larger than the
sector size, which btrfs_check_features() rejected once SPACE_CACHE was
set from the superblock.
Assisted-by: Claude:claude-fable-5-1
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/super.c | 16 +++-------------
1 file changed, 3 insertions(+), 13 deletions(-)
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 464129b1b0d4..77443ded6db3 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -759,12 +759,12 @@ void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info)
/*
* At this point we don't have explicit options set by the user, set
- * them ourselves based on the state of the file system.
+ * them ourselves based on the state of the file system. An existing
+ * v1 space cache is no longer used and gets cleaned up once the
+ * filesystem is mounted read-write.
*/
if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
- else if (btrfs_free_space_cache_v1_active(fs_info))
- btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
}
static void set_device_specific_options(struct btrfs_fs_info *fs_info)
@@ -1264,8 +1264,6 @@ static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
unsigned long long old_opts)
{
- const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
-
/*
* We need to cleanup all defraggable inodes if the autodefragment is
* close or the filesystem is read only.
@@ -1282,10 +1280,6 @@ static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
!btrfs_test_opt(fs_info, DISCARD_ASYNC))
btrfs_discard_cleanup(fs_info);
-
- /* If we toggled space cache */
- if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
- btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
}
static int btrfs_remount_rw(struct btrfs_fs_info *fs_info)
@@ -1535,10 +1529,6 @@ static int btrfs_reconfigure(struct fs_context *fc)
btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
}
- if (btrfs_free_space_cache_v1_active(fs_info)) {
- btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
- btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
- }
}
ret = 0;
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 02/17] btrfs: remove the v1 space cache writeout from the transaction commit
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
2026-09-17 3:59 ` [PATCH v3 01/17] btrfs: stop enabling the v1 space cache from the on-disk state Tal Zussman
@ 2026-09-17 3:59 ` Tal Zussman
2026-09-17 3:59 ` [PATCH v3 03/17] btrfs: remove the free space cache endio workqueue Tal Zussman
` (14 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 3:59 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
Nothing sets SPACE_CACHE anymore, so the dirty block group writers never
have a cache to write out or wait for. Remove cache_save_setup(),
btrfs_setup_space_cache(), the io_list handling, the io_bgs list and
BTRFS_TRANS_CACHE_ENOSPC, and the abort-time cleanup of in-flight cache
IO.
The -ENOENT retry in btrfs_write_dirty_block_groups() handled a free
space endio worker creating a block group during the commit critical
section, so drop it too.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/block-group.c | 397 ++++---------------------------------------------
fs/btrfs/block-group.h | 1 -
fs/btrfs/disk-io.c | 44 ------
fs/btrfs/transaction.c | 9 +-
fs/btrfs/transaction.h | 18 ---
5 files changed, 26 insertions(+), 443 deletions(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index ee182369254c..857da5564cf8 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -1194,29 +1194,10 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
goto out;
}
- /*
- * get the inode first so any iput calls done for the io_list
- * aren't the final iput (no unlinks allowed now)
- */
inode = lookup_free_space_inode(block_group, path);
mutex_lock(&trans->transaction->cache_write_mutex);
- /*
- * Make sure our free space cache IO is done before removing the
- * free space inode
- */
spin_lock(&trans->transaction->dirty_bgs_lock);
- if (!list_empty(&block_group->io_list)) {
- list_del_init(&block_group->io_list);
-
- WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
-
- spin_unlock(&trans->transaction->dirty_bgs_lock);
- btrfs_wait_cache_io(trans, block_group, path);
- btrfs_put_block_group(block_group);
- spin_lock(&trans->transaction->dirty_bgs_lock);
- }
-
if (!list_empty(&block_group->dirty_list)) {
list_del_init(&block_group->dirty_list);
remove_rsv = true;
@@ -3373,197 +3354,6 @@ static int update_block_group_item(struct btrfs_trans_handle *trans,
}
-static void cache_save_setup(struct btrfs_block_group *block_group,
- struct btrfs_trans_handle *trans,
- struct btrfs_path *path)
-{
- struct btrfs_fs_info *fs_info = block_group->fs_info;
- struct inode *inode = NULL;
- struct extent_changeset *data_reserved = NULL;
- u64 alloc_hint = 0;
- int dcs = BTRFS_DC_ERROR;
- u64 cache_size = 0;
- int retries = 0;
- int ret = 0;
-
- if (!btrfs_test_opt(fs_info, SPACE_CACHE))
- return;
-
- /*
- * If this block group is smaller than 100 megs don't bother caching the
- * block group.
- */
- if (block_group->length < (100 * SZ_1M)) {
- spin_lock(&block_group->lock);
- block_group->disk_cache_state = BTRFS_DC_WRITTEN;
- spin_unlock(&block_group->lock);
- return;
- }
-
- if (TRANS_ABORTED(trans))
- return;
-again:
- inode = lookup_free_space_inode(block_group, path);
- if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
- ret = PTR_ERR(inode);
- btrfs_release_path(path);
- goto out;
- }
-
- if (IS_ERR(inode)) {
- if (retries) {
- ret = PTR_ERR(inode);
- btrfs_err(fs_info,
- "failed to lookup free space inode after creation for block group %llu: %d",
- block_group->start, ret);
- goto out_free;
- }
- retries++;
-
- if (block_group->ro)
- goto out_free;
-
- ret = create_free_space_inode(trans, block_group, path);
- if (ret)
- goto out_free;
- goto again;
- }
-
- /*
- * We want to set the generation to 0, that way if anything goes wrong
- * from here on out we know not to trust this cache when we load up next
- * time.
- */
- BTRFS_I(inode)->generation = 0;
- ret = btrfs_update_inode(trans, BTRFS_I(inode));
- if (unlikely(ret)) {
- /*
- * So theoretically we could recover from this, simply set the
- * super cache generation to 0 so we know to invalidate the
- * cache, but then we'd have to keep track of the block groups
- * that fail this way so we know we _have_ to reset this cache
- * before the next commit or risk reading stale cache. So to
- * limit our exposure to horrible edge cases lets just abort the
- * transaction, this only happens in really bad situations
- * anyway.
- */
- btrfs_abort_transaction(trans, ret);
- goto out_put;
- }
-
- /* We've already setup this transaction, go ahead and exit */
- if (block_group->cache_generation == trans->transid &&
- i_size_read(inode)) {
- dcs = BTRFS_DC_SETUP;
- goto out_put;
- }
-
- if (i_size_read(inode) > 0) {
- ret = btrfs_check_trunc_cache_free_space(fs_info,
- &fs_info->global_block_rsv);
- if (ret)
- goto out_put;
-
- ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
- if (ret)
- goto out_put;
- }
-
- spin_lock(&block_group->lock);
- if (block_group->cached != BTRFS_CACHE_FINISHED ||
- !btrfs_test_opt(fs_info, SPACE_CACHE)) {
- /*
- * don't bother trying to write stuff out _if_
- * a) we're not cached,
- * b) we're with nospace_cache mount option,
- * c) we're with v2 space_cache (FREE_SPACE_TREE).
- */
- dcs = BTRFS_DC_WRITTEN;
- spin_unlock(&block_group->lock);
- goto out_put;
- }
- spin_unlock(&block_group->lock);
-
- /*
- * We hit an ENOSPC when setting up the cache in this transaction, just
- * skip doing the setup, we've already cleared the cache so we're safe.
- */
- if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags))
- goto out_put;
-
- /*
- * Try to preallocate enough space based on how big the block group is.
- * Keep in mind this has to include any pinned space which could end up
- * taking up quite a bit since it's not folded into the other space
- * cache.
- */
- cache_size = div_u64(block_group->length, SZ_256M);
- if (!cache_size)
- cache_size = 1;
-
- cache_size *= 16;
- cache_size *= fs_info->sectorsize;
-
- ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, 0,
- cache_size, false);
- if (ret)
- goto out_put;
-
- ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, cache_size,
- cache_size, cache_size,
- &alloc_hint);
- /*
- * Our cache requires contiguous chunks so that we don't modify a bunch
- * of metadata or split extents when writing the cache out, which means
- * we can enospc if we are heavily fragmented in addition to just normal
- * out of space conditions. So if we hit this just skip setting up any
- * other block groups for this transaction, maybe we'll unpin enough
- * space the next time around.
- */
- if (!ret)
- dcs = BTRFS_DC_SETUP;
- else if (ret == -ENOSPC)
- set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
-
-out_put:
- iput(inode);
-out_free:
- btrfs_release_path(path);
-out:
- spin_lock(&block_group->lock);
- if (!ret && dcs == BTRFS_DC_SETUP)
- block_group->cache_generation = trans->transid;
- block_group->disk_cache_state = dcs;
- spin_unlock(&block_group->lock);
-
- extent_changeset_free(data_reserved);
-}
-
-int btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
-{
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_block_group *cache, *tmp;
- struct btrfs_transaction *cur_trans = trans->transaction;
- BTRFS_PATH_AUTO_FREE(path);
-
- if (list_empty(&cur_trans->dirty_bgs) ||
- !btrfs_test_opt(fs_info, SPACE_CACHE))
- return 0;
-
- path = btrfs_alloc_path();
- if (!path)
- return -ENOMEM;
-
- /* Could add new block groups, use _safe just in case */
- list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
- dirty_list) {
- if (cache->disk_cache_state == BTRFS_DC_CLEAR)
- cache_save_setup(cache, trans, path);
- }
-
- return 0;
-}
-
/*
* Transaction commit does final block group cache writeback during a critical
* section where nothing is allowed to change the FS. This is required in
@@ -3582,10 +3372,8 @@ int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
struct btrfs_block_group *cache;
struct btrfs_transaction *cur_trans = trans->transaction;
int ret = 0;
- int should_put;
BTRFS_PATH_AUTO_FREE(path);
LIST_HEAD(dirty);
- struct list_head *io = &cur_trans->io_bgs;
int loops = 0;
spin_lock(&cur_trans->dirty_bgs_lock);
@@ -3611,7 +3399,7 @@ int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
/*
* cache_write_mutex is here only to save us from balance or automatic
* removal of empty block groups deleting this block group while we are
- * writing out the cache
+ * updating its item
*/
mutex_lock(&trans->transaction->cache_write_mutex);
while (!list_empty(&dirty)) {
@@ -3619,23 +3407,8 @@ int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
cache = list_first_entry(&dirty, struct btrfs_block_group,
dirty_list);
- /*
- * This can happen if something re-dirties a block group that
- * is already under IO. Just wait for it to finish and then do
- * it all again
- */
- if (!list_empty(&cache->io_list)) {
- list_del_init(&cache->io_list);
- btrfs_wait_cache_io(trans, cache, path);
- btrfs_put_block_group(cache);
- }
-
/*
- * btrfs_wait_cache_io uses the cache->dirty_list to decide if
- * it should update the cache_state. Don't delete until after
- * we wait.
- *
* Since we're not running in the commit critical section
* we need the dirty_bgs_lock to protect from update_block_group
*/
@@ -3643,66 +3416,33 @@ int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
list_del_init(&cache->dirty_list);
spin_unlock(&cur_trans->dirty_bgs_lock);
- should_put = 1;
-
- cache_save_setup(cache, trans, path);
-
- if (cache->disk_cache_state == BTRFS_DC_SETUP) {
- cache->io_ctl.inode = NULL;
- ret = btrfs_write_out_cache(trans, cache, path);
- if (ret == 0 && cache->io_ctl.inode) {
- should_put = 0;
-
- /*
- * The cache_write_mutex is protecting the
- * io_list, also refer to the definition of
- * btrfs_transaction::io_bgs for more details
- */
- list_add_tail(&cache->io_list, io);
- } else {
- /*
- * If we failed to write the cache, the
- * generation will be bad and life goes on
- */
- ret = 0;
- }
- }
- if (!ret) {
- ret = update_block_group_item(trans, path, cache);
- /*
- * Our block group might still be attached to the list
- * of new block groups in the transaction handle of some
- * other task (struct btrfs_trans_handle->new_bgs). This
- * means its block group item isn't yet in the extent
- * tree. If this happens ignore the error, as we will
- * try again later in the critical section of the
- * transaction commit.
- */
- if (ret == -ENOENT) {
- ret = 0;
- spin_lock(&cur_trans->dirty_bgs_lock);
- if (list_empty(&cache->dirty_list)) {
- list_add_tail(&cache->dirty_list,
- &cur_trans->dirty_bgs);
- btrfs_get_block_group(cache);
- drop_reserve = false;
- }
- spin_unlock(&cur_trans->dirty_bgs_lock);
- } else if (ret) {
- btrfs_abort_transaction(trans, ret);
+ ret = update_block_group_item(trans, path, cache);
+ /*
+ * Our block group might still be attached to the list of new
+ * block groups in the transaction handle of some other task
+ * (struct btrfs_trans_handle->new_bgs). This means its block
+ * group item isn't yet in the extent tree. If this happens
+ * ignore the error, as we will try again later in the critical
+ * section of the transaction commit.
+ */
+ if (ret == -ENOENT) {
+ ret = 0;
+ spin_lock(&cur_trans->dirty_bgs_lock);
+ if (list_empty(&cache->dirty_list)) {
+ list_add_tail(&cache->dirty_list,
+ &cur_trans->dirty_bgs);
+ btrfs_get_block_group(cache);
+ drop_reserve = false;
}
+ spin_unlock(&cur_trans->dirty_bgs_lock);
+ } else if (ret) {
+ btrfs_abort_transaction(trans, ret);
}
- /* If it's not on the io list, we need to put the block group */
- if (should_put)
- btrfs_put_block_group(cache);
+ btrfs_put_block_group(cache);
if (drop_reserve)
btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
- /*
- * Avoid blocking other tasks for too long. It might even save
- * us from writing caches for block groups that are going to be
- * removed.
- */
+ /* Avoid blocking other tasks for too long. */
mutex_unlock(&trans->transaction->cache_write_mutex);
if (ret)
goto out;
@@ -3747,121 +3487,34 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
struct btrfs_block_group *cache;
struct btrfs_transaction *cur_trans = trans->transaction;
int ret = 0;
- int should_put;
BTRFS_PATH_AUTO_FREE(path);
- struct list_head *io = &cur_trans->io_bgs;
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
- /*
- * Even though we are in the critical section of the transaction commit,
- * we can still have concurrent tasks adding elements to this
- * transaction's list of dirty block groups. These tasks correspond to
- * endio free space workers started when writeback finishes for a
- * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
- * allocate new block groups as a result of COWing nodes of the root
- * tree when updating the free space inode. The writeback for the space
- * caches is triggered by an earlier call to
- * btrfs_start_dirty_block_groups() and iterations of the following
- * loop.
- * Also we want to do the cache_save_setup first and then run the
- * delayed refs to make sure we have the best chance at doing this all
- * in one shot.
- */
spin_lock(&cur_trans->dirty_bgs_lock);
while (!list_empty(&cur_trans->dirty_bgs)) {
cache = list_first_entry(&cur_trans->dirty_bgs,
struct btrfs_block_group,
dirty_list);
-
- /*
- * This can happen if cache_save_setup re-dirties a block group
- * that is already under IO. Just wait for it to finish and
- * then do it all again
- */
- if (!list_empty(&cache->io_list)) {
- spin_unlock(&cur_trans->dirty_bgs_lock);
- list_del_init(&cache->io_list);
- btrfs_wait_cache_io(trans, cache, path);
- btrfs_put_block_group(cache);
- spin_lock(&cur_trans->dirty_bgs_lock);
- }
-
- /*
- * Don't remove from the dirty list until after we've waited on
- * any pending IO
- */
list_del_init(&cache->dirty_list);
spin_unlock(&cur_trans->dirty_bgs_lock);
- should_put = 1;
-
- cache_save_setup(cache, trans, path);
if (!ret)
ret = btrfs_run_delayed_refs(trans, U64_MAX);
-
- if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
- cache->io_ctl.inode = NULL;
- ret = btrfs_write_out_cache(trans, cache, path);
- if (ret == 0 && cache->io_ctl.inode) {
- should_put = 0;
- list_add_tail(&cache->io_list, io);
- } else {
- /*
- * If we failed to write the cache, the
- * generation will be bad and life goes on
- */
- ret = 0;
- }
- }
if (!ret) {
ret = update_block_group_item(trans, path, cache);
- /*
- * One of the free space endio workers might have
- * created a new block group while updating a free space
- * cache's inode (at inode.c:btrfs_finish_ordered_io())
- * and hasn't released its transaction handle yet, in
- * which case the new block group is still attached to
- * its transaction handle and its creation has not
- * finished yet (no block group item in the extent tree
- * yet, etc). If this is the case, wait for all free
- * space endio workers to finish and retry. This is a
- * very rare case so no need for a more efficient and
- * complex approach.
- */
- if (ret == -ENOENT) {
- wait_event(cur_trans->writer_wait,
- atomic_read(&cur_trans->num_writers) == 1);
- ret = update_block_group_item(trans, path, cache);
- if (ret)
- btrfs_abort_transaction(trans, ret);
- } else if (ret) {
+ if (ret)
btrfs_abort_transaction(trans, ret);
- }
}
- /* If its not on the io list, we need to put the block group */
- if (should_put)
- btrfs_put_block_group(cache);
+ btrfs_put_block_group(cache);
btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
spin_lock(&cur_trans->dirty_bgs_lock);
}
spin_unlock(&cur_trans->dirty_bgs_lock);
- /*
- * Refer to the definition of io_bgs member for details why it's safe
- * to use it without any locking
- */
- while (!list_empty(io)) {
- cache = list_first_entry(io, struct btrfs_block_group,
- io_list);
- list_del_init(&cache->io_list);
- btrfs_wait_cache_io(trans, cache, path);
- btrfs_put_block_group(cache);
- }
-
return ret;
}
diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h
index 69d56864d4ba..97c8565ae4fc 100644
--- a/fs/btrfs/block-group.h
+++ b/fs/btrfs/block-group.h
@@ -368,7 +368,6 @@ int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
void btrfs_dec_block_group_ro(struct btrfs_block_group *cache);
int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans);
int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans);
-int btrfs_setup_space_cache(struct btrfs_trans_handle *trans);
int btrfs_update_block_group(struct btrfs_trans_handle *trans,
u64 bytenr, u64 num_bytes, bool alloc);
int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 819727460bcf..f544c457d4ab 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -4860,26 +4860,6 @@ static void btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
}
}
-static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache)
-{
- struct inode *inode;
-
- inode = cache->io_ctl.inode;
- if (inode) {
- unsigned int nofs_flag;
-
- nofs_flag = memalloc_nofs_save();
- invalidate_inode_pages2(inode->i_mapping);
- memalloc_nofs_restore(nofs_flag);
-
- BTRFS_I(inode)->generation = 0;
- cache->io_ctl.inode = NULL;
- iput(inode);
- }
- ASSERT(cache->io_ctl.pages == NULL);
- btrfs_put_block_group(cache);
-}
-
void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
struct btrfs_fs_info *fs_info)
{
@@ -4891,13 +4871,6 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
struct btrfs_block_group,
dirty_list);
- if (!list_empty(&cache->io_list)) {
- spin_unlock(&cur_trans->dirty_bgs_lock);
- list_del_init(&cache->io_list);
- btrfs_cleanup_bg_io(cache);
- spin_lock(&cur_trans->dirty_bgs_lock);
- }
-
list_del_init(&cache->dirty_list);
spin_lock(&cache->lock);
cache->disk_cache_state = BTRFS_DC_ERROR;
@@ -4909,22 +4882,6 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
spin_lock(&cur_trans->dirty_bgs_lock);
}
spin_unlock(&cur_trans->dirty_bgs_lock);
-
- /*
- * Refer to the definition of io_bgs member for details why it's safe
- * to use it without any locking
- */
- while (!list_empty(&cur_trans->io_bgs)) {
- cache = list_first_entry(&cur_trans->io_bgs,
- struct btrfs_block_group,
- io_list);
-
- list_del_init(&cache->io_list);
- spin_lock(&cache->lock);
- cache->disk_cache_state = BTRFS_DC_ERROR;
- spin_unlock(&cache->lock);
- btrfs_cleanup_bg_io(cache);
- }
}
static void btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info *fs_info)
@@ -4960,7 +4917,6 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans)
btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
ASSERT(list_empty(&cur_trans->dirty_bgs));
- ASSERT(list_empty(&cur_trans->io_bgs));
list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list,
post_commit_list) {
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 6802b94ed76f..6a36d29406c3 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -379,7 +379,6 @@ static noinline int join_transaction(struct btrfs_fs_info *fs_info,
INIT_LIST_HEAD(&cur_trans->dev_update_list);
INIT_LIST_HEAD(&cur_trans->switch_commits);
INIT_LIST_HEAD(&cur_trans->dirty_bgs);
- INIT_LIST_HEAD(&cur_trans->io_bgs);
INIT_LIST_HEAD(&cur_trans->dropped_roots);
mutex_init(&cur_trans->cache_write_mutex);
spin_lock_init(&cur_trans->dirty_bgs_lock);
@@ -1363,7 +1362,6 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
{
struct btrfs_fs_info *fs_info = trans->fs_info;
struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
- struct list_head *io_bgs = &trans->transaction->io_bgs;
struct extent_buffer *eb;
int ret;
@@ -1393,10 +1391,6 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
if (ret)
return ret;
- ret = btrfs_setup_space_cache(trans);
- if (ret)
- return ret;
-
again:
while (!list_empty(&fs_info->dirty_cowonly_roots)) {
struct btrfs_root *root;
@@ -1417,7 +1411,7 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
if (ret)
return ret;
- while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
+ while (!list_empty(dirty_bgs)) {
ret = btrfs_write_dirty_block_groups(trans);
if (ret)
return ret;
@@ -2543,7 +2537,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
switch_commit_roots(trans);
ASSERT(list_empty(&cur_trans->dirty_bgs));
- ASSERT(list_empty(&cur_trans->io_bgs));
update_super_roots(fs_info);
btrfs_set_super_log_root(fs_info->super_copy, 0);
diff --git a/fs/btrfs/transaction.h b/fs/btrfs/transaction.h
index 3a57f227b5ed..8245a58587c0 100644
--- a/fs/btrfs/transaction.h
+++ b/fs/btrfs/transaction.h
@@ -47,7 +47,6 @@ enum btrfs_trans_state {
#define BTRFS_TRANS_HAVE_FREE_BGS 0
#define BTRFS_TRANS_DIRTY_BG_RUN 1
-#define BTRFS_TRANS_CACHE_ENOSPC 2
struct btrfs_transaction {
u64 transid;
@@ -78,23 +77,6 @@ struct btrfs_transaction {
struct list_head dev_update_list;
struct list_head switch_commits;
struct list_head dirty_bgs;
-
- /*
- * There is no explicit lock which protects io_bgs, rather its
- * consistency is implied by the fact that all the sites which modify
- * it do so under some form of transaction critical section, namely:
- *
- * - btrfs_start_dirty_block_groups - This function can only ever be
- * run by one of the transaction committers. Refer to
- * BTRFS_TRANS_DIRTY_BG_RUN usage in btrfs_commit_transaction
- *
- * - btrfs_write_dirty_blockgroups - this is called by
- * commit_cowonly_roots from transaction critical section
- * (TRANS_STATE_COMMIT_DOING)
- *
- * - btrfs_cleanup_dirty_bgs - called on transaction abort
- */
- struct list_head io_bgs;
struct list_head dropped_roots;
struct extent_io_tree pinned_extents;
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 03/17] btrfs: remove the free space cache endio workqueue
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
2026-09-17 3:59 ` [PATCH v3 01/17] btrfs: stop enabling the v1 space cache from the on-disk state Tal Zussman
2026-09-17 3:59 ` [PATCH v3 02/17] btrfs: remove the v1 space cache writeout from the transaction commit Tal Zussman
@ 2026-09-17 3:59 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 04/17] btrfs: remove the v1 space cache write path Tal Zussman
` (13 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 3:59 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
Free space inodes are no longer written to, so nothing queues ordered
extent completion on endio_freespace_worker. Remove it and always use
endio_write_workers in btrfs_queue_ordered_fn().
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/disk-io.c | 14 +++-----------
fs/btrfs/fs.h | 1 -
fs/btrfs/ordered-data.c | 7 ++-----
fs/btrfs/super.c | 1 -
4 files changed, 5 insertions(+), 18 deletions(-)
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index f544c457d4ab..f256a91f58b4 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1769,7 +1769,6 @@ static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
if (fs_info->rmw_workers)
destroy_workqueue(fs_info->rmw_workers);
btrfs_destroy_workqueue(fs_info->endio_write_workers);
- btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
btrfs_destroy_workqueue(fs_info->delayed_workers);
btrfs_destroy_workqueue(fs_info->caching_workers);
btrfs_destroy_workqueue(fs_info->flush_workers);
@@ -1980,9 +1979,6 @@ static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
fs_info->endio_write_workers =
btrfs_alloc_workqueue(fs_info, "endio-write", flags,
max_active, 2);
- fs_info->endio_freespace_worker =
- btrfs_alloc_workqueue(fs_info, "freespace-write", flags,
- max_active, 0);
fs_info->delayed_workers =
btrfs_alloc_workqueue(fs_info, "delayed-meta", flags,
max_active, 0);
@@ -1995,8 +1991,7 @@ static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
if (!(fs_info->workers &&
fs_info->delalloc_workers && fs_info->flush_workers &&
fs_info->endio_workers && fs_info->endio_meta_workers &&
- fs_info->endio_write_workers &&
- fs_info->endio_freespace_worker && fs_info->rmw_workers &&
+ fs_info->endio_write_workers && fs_info->rmw_workers &&
fs_info->caching_workers && fs_info->fixup_workers &&
fs_info->delayed_workers && fs_info->qgroup_rescan_workers &&
fs_info->discard_ctl.discard_workers)) {
@@ -4442,9 +4437,8 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)
* to finish an ordered extent - end_bbio_compressed_write()
* calls btrfs_finish_ordered_extent() which in turns does a call to
* btrfs_queue_ordered_fn(), and that queues the ordered extent
- * completion either in the endio_write_workers work queue or in the
- * fs_info->endio_freespace_worker work queue. We flush those queues
- * below, so before we flush them we must flush this queue for the
+ * completion in the endio_write_workers work queue. We flush that
+ * queue below, so before we flush it we must flush this queue for the
* workers of compressed writes.
*/
flush_workqueue(fs_info->endio_workers);
@@ -4470,8 +4464,6 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)
* btrfs_finish_ordered_io() when we are unmounting).
*/
btrfs_flush_workqueue(fs_info->endio_write_workers);
- /* Ordered extents for free space inodes. */
- btrfs_flush_workqueue(fs_info->endio_freespace_worker);
/*
* Run delayed iputs in case an async reclaim worker is waiting for them
* to be run as mentioned above.
diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
index 10e15a319b93..96cc8c50af82 100644
--- a/fs/btrfs/fs.h
+++ b/fs/btrfs/fs.h
@@ -712,7 +712,6 @@ struct btrfs_fs_info {
struct workqueue_struct *endio_meta_workers;
struct workqueue_struct *rmw_workers;
struct btrfs_workqueue *endio_write_workers;
- struct btrfs_workqueue *endio_freespace_worker;
struct btrfs_workqueue *caching_workers;
struct workqueue_struct *fixup_workers;
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index b32d4eabe0ab..e9f1cbeb555a 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -417,13 +417,10 @@ static bool can_finish_ordered_extent(struct btrfs_ordered_extent *ordered,
static void btrfs_queue_ordered_fn(struct btrfs_ordered_extent *ordered)
{
- struct btrfs_inode *inode = ordered->inode;
- struct btrfs_fs_info *fs_info = inode->root->fs_info;
- struct btrfs_workqueue *wq = btrfs_is_free_space_inode(inode) ?
- fs_info->endio_freespace_worker : fs_info->endio_write_workers;
+ struct btrfs_fs_info *fs_info = ordered->inode->root->fs_info;
btrfs_init_work(&ordered->work, finish_ordered_fn, NULL);
- btrfs_queue_work(wq, &ordered->work);
+ btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
}
void btrfs_finish_ordered_extent(struct btrfs_ordered_extent *ordered,
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 77443ded6db3..b44b16970a62 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -1243,7 +1243,6 @@ static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);
btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
- btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
}
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 04/17] btrfs: remove the v1 space cache write path
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (2 preceding siblings ...)
2026-09-17 3:59 ` [PATCH v3 03/17] btrfs: remove the free space cache endio workqueue Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 05/17] btrfs: rename cache_write_mutex to dirty_bgs_update_mutex Tal Zussman
` (12 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
Nothing writes out a v1 space cache any more. Remove the writers and
their io_ctl helpers, along with create_free_space_inode() and
btrfs_prealloc_file_range_trans(), whose only user was the cache inode
creation. The io_list and io_ctl block group fields were only used by
the writers, so remove them too.
btrfs_truncate_free_space_cache() only needed the block group to wait
for and clear in-flight cache IO, so drop that parameter.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/block-group.c | 4 -
fs/btrfs/block-group.h | 3 -
fs/btrfs/btrfs_inode.h | 4 -
fs/btrfs/free-space-cache.c | 688 --------------------------------------------
fs/btrfs/free-space-cache.h | 10 -
fs/btrfs/inode.c | 9 -
fs/btrfs/relocation.c | 2 +-
7 files changed, 1 insertion(+), 719 deletions(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 857da5564cf8..5c2a57243de5 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -1268,7 +1268,6 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
spin_lock(&trans->transaction->dirty_bgs_lock);
WARN_ON(!list_empty(&block_group->dirty_list));
- WARN_ON(!list_empty(&block_group->io_list));
spin_unlock(&trans->transaction->dirty_bgs_lock);
btrfs_remove_free_space_cache(block_group);
@@ -2412,7 +2411,6 @@ static struct btrfs_block_group *btrfs_create_block_group(
INIT_LIST_HEAD(&cache->ro_list);
INIT_LIST_HEAD(&cache->discard_list);
INIT_LIST_HEAD(&cache->dirty_list);
- INIT_LIST_HEAD(&cache->io_list);
INIT_LIST_HEAD(&cache->active_bg_list);
btrfs_init_free_space_ctl(cache, cache->free_space_ctl);
atomic_set(&cache->frozen, 0);
@@ -4293,7 +4291,6 @@ void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
block_group->inode = NULL;
spin_unlock(&block_group->lock);
- ASSERT(block_group->io_ctl.inode == NULL);
iput(&inode->vfs_inode);
} else {
spin_unlock(&block_group->lock);
@@ -4430,7 +4427,6 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
btrfs_remove_free_space_cache(block_group);
ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
ASSERT(list_empty(&block_group->dirty_list));
- ASSERT(list_empty(&block_group->io_list));
ASSERT(list_empty(&block_group->bg_list));
ASSERT(refcount_read(&block_group->refs) == 1);
ASSERT(block_group->swap_extents == 0);
diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h
index 97c8565ae4fc..939d4bfbd4b4 100644
--- a/fs/btrfs/block-group.h
+++ b/fs/btrfs/block-group.h
@@ -228,9 +228,6 @@ struct btrfs_block_group {
/* For dirty block groups */
struct list_head dirty_list;
- struct list_head io_list;
-
- struct btrfs_io_ctl io_ctl;
/*
* Incremented when doing extent allocations and holding a read lock
diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index 1082fa92c145..26cc5d1aca4c 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -594,10 +594,6 @@ int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
int btrfs_prealloc_file_range(struct inode *inode, int mode,
u64 start, u64 num_bytes, u64 min_size,
loff_t actual_len, u64 *alloc_hint);
-int btrfs_prealloc_file_range_trans(struct inode *inode,
- struct btrfs_trans_handle *trans, int mode,
- u64 start, u64 num_bytes, u64 min_size,
- loff_t actual_len, u64 *alloc_hint);
int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct folio *locked_folio,
u64 start, u64 end, struct writeback_control *wbc);
void btrfs_queue_writepage_fixup(struct btrfs_inode *inode, struct folio *folio);
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index e2af75a205ea..336b546b0a94 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -164,78 +164,6 @@ struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
return inode;
}
-static int __create_free_space_inode(struct btrfs_root *root,
- struct btrfs_trans_handle *trans,
- struct btrfs_path *path,
- u64 ino, u64 offset)
-{
- struct btrfs_key key;
- struct btrfs_disk_key disk_key;
- struct btrfs_free_space_header *header;
- struct btrfs_inode_item *inode_item;
- struct extent_buffer *leaf;
- /* We inline CRCs for the free disk space cache */
- const u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC |
- BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
- int ret;
-
- ret = btrfs_insert_empty_inode(trans, root, path, ino);
- if (ret)
- return ret;
-
- leaf = path->nodes[0];
- inode_item = btrfs_item_ptr(leaf, path->slots[0],
- struct btrfs_inode_item);
- btrfs_item_key(leaf, &disk_key, path->slots[0]);
- memzero_extent_buffer(leaf, (unsigned long)inode_item,
- sizeof(*inode_item));
- btrfs_set_inode_generation(leaf, inode_item, trans->transid);
- btrfs_set_inode_size(leaf, inode_item, 0);
- btrfs_set_inode_nbytes(leaf, inode_item, 0);
- btrfs_set_inode_uid(leaf, inode_item, 0);
- btrfs_set_inode_gid(leaf, inode_item, 0);
- btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
- btrfs_set_inode_flags(leaf, inode_item, flags);
- btrfs_set_inode_nlink(leaf, inode_item, 1);
- btrfs_set_inode_transid(leaf, inode_item, trans->transid);
- btrfs_set_inode_block_group(leaf, inode_item, offset);
- btrfs_release_path(path);
-
- key.objectid = BTRFS_FREE_SPACE_OBJECTID;
- key.type = 0;
- key.offset = offset;
- ret = btrfs_insert_empty_item(trans, root, path, &key,
- sizeof(struct btrfs_free_space_header));
- if (ret < 0) {
- btrfs_release_path(path);
- return ret;
- }
-
- leaf = path->nodes[0];
- header = btrfs_item_ptr(leaf, path->slots[0],
- struct btrfs_free_space_header);
- memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
- btrfs_set_free_space_key(leaf, header, &disk_key);
- btrfs_release_path(path);
-
- return 0;
-}
-
-int create_free_space_inode(struct btrfs_trans_handle *trans,
- struct btrfs_block_group *block_group,
- struct btrfs_path *path)
-{
- int ret;
- u64 ino;
-
- ret = btrfs_get_free_objectid(trans->fs_info->tree_root, &ino);
- if (ret < 0)
- return ret;
-
- return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
- ino, block_group->start);
-}
-
/*
* inode is an optional sink: if it is NULL, btrfs_remove_free_space_inode
* handles lookup, otherwise it takes ownership and iputs the inode.
@@ -292,7 +220,6 @@ int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
}
int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
- struct btrfs_block_group *block_group,
struct inode *vfs_inode)
{
struct btrfs_truncate_control control = {
@@ -306,33 +233,6 @@ int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
struct btrfs_root *root = inode->root;
struct extent_state *cached_state = NULL;
int ret = 0;
- bool locked = false;
-
- if (block_group) {
- BTRFS_PATH_AUTO_FREE(path);
-
- path = btrfs_alloc_path();
- if (!path) {
- ret = -ENOMEM;
- goto fail;
- }
- locked = true;
- mutex_lock(&trans->transaction->cache_write_mutex);
- if (!list_empty(&block_group->io_list)) {
- list_del_init(&block_group->io_list);
-
- btrfs_wait_cache_io(trans, block_group, path);
- btrfs_put_block_group(block_group);
- }
-
- /*
- * now that we've truncated the cache away, its no longer
- * setup or written
- */
- spin_lock(&block_group->lock);
- block_group->disk_cache_state = BTRFS_DC_CLEAR;
- spin_unlock(&block_group->lock);
- }
btrfs_i_size_write(inode, 0);
truncate_pagecache(vfs_inode, 0);
@@ -356,8 +256,6 @@ int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
ret = btrfs_update_inode(trans, inode);
fail:
- if (locked)
- mutex_unlock(&trans->transaction->cache_write_mutex);
if (ret)
btrfs_abort_transaction(trans, ret);
@@ -490,21 +388,6 @@ static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, bool uptodate)
return 0;
}
-static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
-{
- io_ctl_map_page(io_ctl, 1);
-
- /*
- * Skip the csum areas. If we don't check crcs then we just have a
- * 64bit chunk at the front of the first page.
- */
- io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
- io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
-
- put_unaligned_le64(generation, io_ctl->cur);
- io_ctl->cur += sizeof(u64);
-}
-
static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
{
u64 cache_gen;
@@ -528,23 +411,6 @@ static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
return 0;
}
-static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
-{
- u32 *tmp;
- u32 crc = ~(u32)0;
- unsigned offset = 0;
-
- if (index == 0)
- offset = sizeof(u32) * io_ctl->num_pages;
-
- crc = crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
- btrfs_crc32c_final(crc, (u8 *)&crc);
- io_ctl_unmap_page(io_ctl);
- tmp = page_address(io_ctl->pages[0]);
- tmp += index;
- *tmp = crc;
-}
-
static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
{
u32 *tmp, val;
@@ -574,76 +440,6 @@ static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
return 0;
}
-static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
- void *bitmap)
-{
- struct btrfs_free_space_entry *entry;
-
- if (!io_ctl->cur)
- return -ENOSPC;
-
- entry = io_ctl->cur;
- put_unaligned_le64(offset, &entry->offset);
- put_unaligned_le64(bytes, &entry->bytes);
- entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
- BTRFS_FREE_SPACE_EXTENT;
- io_ctl->cur += sizeof(struct btrfs_free_space_entry);
- io_ctl->size -= sizeof(struct btrfs_free_space_entry);
-
- if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
- return 0;
-
- io_ctl_set_crc(io_ctl, io_ctl->index - 1);
-
- /* No more pages to map */
- if (io_ctl->index >= io_ctl->num_pages)
- return 0;
-
- /* map the next page */
- io_ctl_map_page(io_ctl, 1);
- return 0;
-}
-
-static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
-{
- if (!io_ctl->cur)
- return -ENOSPC;
-
- /*
- * If we aren't at the start of the current page, unmap this one and
- * map the next one if there is any left.
- */
- if (io_ctl->cur != io_ctl->orig) {
- io_ctl_set_crc(io_ctl, io_ctl->index - 1);
- if (io_ctl->index >= io_ctl->num_pages)
- return -ENOSPC;
- io_ctl_map_page(io_ctl, 0);
- }
-
- copy_page(io_ctl->cur, bitmap);
- io_ctl_set_crc(io_ctl, io_ctl->index - 1);
- if (io_ctl->index < io_ctl->num_pages)
- io_ctl_map_page(io_ctl, 0);
- return 0;
-}
-
-static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
-{
- /*
- * If we're not on the boundary we know we've modified the page and we
- * need to crc the page.
- */
- if (io_ctl->cur != io_ctl->orig)
- io_ctl_set_crc(io_ctl, io_ctl->index - 1);
- else
- io_ctl_unmap_page(io_ctl);
-
- while (io_ctl->index < io_ctl->num_pages) {
- io_ctl_map_page(io_ctl, 1);
- io_ctl_set_crc(io_ctl, io_ctl->index - 1);
- }
-}
-
static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
struct btrfs_free_space *entry, u8 *type)
{
@@ -1065,490 +861,6 @@ int load_free_space_cache(struct btrfs_block_group *block_group)
return ret;
}
-static noinline_for_stack
-int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
- struct btrfs_block_group *block_group,
- int *entries, int *bitmaps,
- struct list_head *bitmap_list)
-{
- int ret;
- struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
- struct btrfs_free_cluster *cluster = NULL;
- struct btrfs_free_cluster *cluster_locked = NULL;
- struct rb_node *node = rb_first(&ctl->free_space_offset);
- struct btrfs_trim_range *trim_entry;
-
- /* Get the cluster for this block_group if it exists */
- if (!list_empty(&block_group->cluster_list)) {
- cluster = list_first_entry(&block_group->cluster_list,
- struct btrfs_free_cluster, block_group_list);
- }
-
- if (!node && cluster) {
- cluster_locked = cluster;
- spin_lock(&cluster_locked->lock);
- node = rb_first(&cluster->root);
- cluster = NULL;
- }
-
- /* Write out the extent entries */
- while (node) {
- struct btrfs_free_space *e;
-
- e = rb_entry(node, struct btrfs_free_space, offset_index);
- *entries += 1;
-
- ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
- e->bitmap);
- if (ret)
- goto fail;
-
- if (e->bitmap) {
- list_add_tail(&e->list, bitmap_list);
- *bitmaps += 1;
- }
- node = rb_next(node);
- if (!node && cluster) {
- node = rb_first(&cluster->root);
- cluster_locked = cluster;
- spin_lock(&cluster_locked->lock);
- cluster = NULL;
- }
- }
- if (cluster_locked) {
- spin_unlock(&cluster_locked->lock);
- cluster_locked = NULL;
- }
-
- /*
- * Make sure we don't miss any range that was removed from our rbtree
- * because trimming is running. Otherwise after a umount+mount (or crash
- * after committing the transaction) we would leak free space and get
- * an inconsistent free space cache report from fsck.
- */
- list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
- ret = io_ctl_add_entry(io_ctl, trim_entry->start,
- trim_entry->bytes, NULL);
- if (ret)
- goto fail;
- *entries += 1;
- }
-
- return 0;
-fail:
- if (cluster_locked)
- spin_unlock(&cluster_locked->lock);
- return -ENOSPC;
-}
-
-static noinline_for_stack int
-update_cache_item(struct btrfs_trans_handle *trans,
- struct btrfs_root *root,
- struct inode *inode,
- struct btrfs_path *path, u64 offset,
- int entries, int bitmaps)
-{
- struct btrfs_key key;
- struct btrfs_free_space_header *header;
- struct extent_buffer *leaf;
- int ret;
-
- key.objectid = BTRFS_FREE_SPACE_OBJECTID;
- key.type = 0;
- key.offset = offset;
-
- ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
- if (ret < 0) {
- btrfs_clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
- EXTENT_DELALLOC, NULL);
- return ret;
- }
- leaf = path->nodes[0];
- if (ret > 0) {
- struct btrfs_key found_key;
- ASSERT(path->slots[0]);
- path->slots[0]--;
- btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
- if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
- found_key.offset != offset) {
- btrfs_clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
- inode->i_size - 1, EXTENT_DELALLOC,
- NULL);
- btrfs_release_path(path);
- return -ENOENT;
- }
- }
-
- BTRFS_I(inode)->generation = trans->transid;
- header = btrfs_item_ptr(leaf, path->slots[0],
- struct btrfs_free_space_header);
- btrfs_set_free_space_entries(leaf, header, entries);
- btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
- btrfs_set_free_space_generation(leaf, header, trans->transid);
- btrfs_release_path(path);
-
- return 0;
-}
-
-static noinline_for_stack int write_pinned_extent_entries(
- struct btrfs_trans_handle *trans,
- struct btrfs_block_group *block_group,
- struct btrfs_io_ctl *io_ctl,
- int *entries)
-{
- u64 start, extent_start, extent_end, len;
- const u64 block_group_end = btrfs_block_group_end(block_group);
- struct extent_io_tree *unpin = NULL;
- int ret;
-
- /*
- * We want to add any pinned extents to our free space cache
- * so we don't leak the space
- *
- * We shouldn't have switched the pinned extents yet so this is the
- * right one
- */
- unpin = &trans->transaction->pinned_extents;
-
- start = block_group->start;
-
- while (start < block_group_end) {
- if (!btrfs_find_first_extent_bit(unpin, start,
- &extent_start, &extent_end,
- EXTENT_DIRTY, NULL))
- return 0;
-
- /* This pinned extent is out of our range */
- if (extent_start >= block_group_end)
- return 0;
-
- extent_start = max(extent_start, start);
- extent_end = min(block_group_end, extent_end + 1);
- len = extent_end - extent_start;
-
- *entries += 1;
- ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
- if (ret)
- return -ENOSPC;
-
- start = extent_end;
- }
-
- return 0;
-}
-
-static noinline_for_stack int
-write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
-{
- struct btrfs_free_space *entry, *next;
- int ret;
-
- /* Write out the bitmaps */
- list_for_each_entry_safe(entry, next, bitmap_list, list) {
- ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
- if (ret)
- return -ENOSPC;
- list_del_init(&entry->list);
- }
-
- return 0;
-}
-
-static int flush_dirty_cache(struct inode *inode)
-{
- int ret;
-
- ret = btrfs_wait_ordered_range(BTRFS_I(inode), 0, (u64)-1);
- if (ret)
- btrfs_clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
- EXTENT_DELALLOC, NULL);
-
- return ret;
-}
-
-static void noinline_for_stack
-cleanup_bitmap_list(struct list_head *bitmap_list)
-{
- struct btrfs_free_space *entry, *next;
-
- list_for_each_entry_safe(entry, next, bitmap_list, list)
- list_del_init(&entry->list);
-}
-
-static void noinline_for_stack
-cleanup_write_cache_enospc(struct inode *inode,
- struct btrfs_io_ctl *io_ctl,
- struct extent_state **cached_state)
-{
- io_ctl_drop_pages(io_ctl);
- btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
- cached_state);
-}
-
-static int __btrfs_wait_cache_io(struct btrfs_root *root,
- struct btrfs_trans_handle *trans,
- struct btrfs_block_group *block_group,
- struct btrfs_io_ctl *io_ctl,
- struct btrfs_path *path, u64 offset)
-{
- int ret;
- struct inode *inode = io_ctl->inode;
-
- if (!inode)
- return 0;
-
- /* Flush the dirty pages in the cache file. */
- ret = flush_dirty_cache(inode);
- if (ret)
- goto out;
-
- /* Update the cache item to tell everyone this cache file is valid. */
- ret = update_cache_item(trans, root, inode, path, offset,
- io_ctl->entries, io_ctl->bitmaps);
-out:
- if (ret) {
- invalidate_inode_pages2(inode->i_mapping);
- BTRFS_I(inode)->generation = 0;
- if (block_group)
- btrfs_debug(root->fs_info,
- "failed to write free space cache for block group %llu error %d",
- block_group->start, ret);
- }
- btrfs_update_inode(trans, BTRFS_I(inode));
-
- if (block_group) {
- /* the dirty list is protected by the dirty_bgs_lock */
- spin_lock(&trans->transaction->dirty_bgs_lock);
-
- /* the disk_cache_state is protected by the block group lock */
- spin_lock(&block_group->lock);
-
- /*
- * only mark this as written if we didn't get put back on
- * the dirty list while waiting for IO. Otherwise our
- * cache state won't be right, and we won't get written again
- */
- if (!ret && list_empty(&block_group->dirty_list))
- block_group->disk_cache_state = BTRFS_DC_WRITTEN;
- else if (ret)
- block_group->disk_cache_state = BTRFS_DC_ERROR;
-
- spin_unlock(&block_group->lock);
- spin_unlock(&trans->transaction->dirty_bgs_lock);
- io_ctl->inode = NULL;
- iput(inode);
- }
-
- return ret;
-
-}
-
-int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
- struct btrfs_block_group *block_group,
- struct btrfs_path *path)
-{
- return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
- block_group, &block_group->io_ctl,
- path, block_group->start);
-}
-
-/*
- * Write out cached info to an inode.
- *
- * @inode: freespace inode we are writing out
- * @ctl: free space cache we are going to write out
- * @block_group: block_group for this cache if it belongs to a block_group
- * @io_ctl: holds context for the io
- * @trans: the trans handle
- *
- * This function writes out a free space cache struct to disk for quick recovery
- * on mount. This will return 0 if it was successful in writing the cache out,
- * or an errno if it was not.
- */
-static int __btrfs_write_out_cache(struct inode *inode,
- struct btrfs_block_group *block_group,
- struct btrfs_trans_handle *trans)
-{
- struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
- struct btrfs_io_ctl *io_ctl = &block_group->io_ctl;
- struct extent_state *cached_state = NULL;
- LIST_HEAD(bitmap_list);
- int entries = 0;
- int bitmaps = 0;
- int ret;
- bool must_iput = false;
- int i_size;
-
- if (!i_size_read(inode))
- return -EIO;
-
- WARN_ON(io_ctl->pages);
- ret = io_ctl_init(io_ctl, inode, 1);
- if (ret)
- return ret;
-
- if (block_group->flags & BTRFS_BLOCK_GROUP_DATA) {
- down_write(&block_group->data_rwsem);
- spin_lock(&block_group->lock);
- if (block_group->delalloc_bytes) {
- block_group->disk_cache_state = BTRFS_DC_WRITTEN;
- spin_unlock(&block_group->lock);
- up_write(&block_group->data_rwsem);
- BTRFS_I(inode)->generation = 0;
- ret = 0;
- must_iput = true;
- goto out;
- }
- spin_unlock(&block_group->lock);
- }
-
- /* Lock all pages first so we can lock the extent safely. */
- ret = io_ctl_prepare_pages(io_ctl, false);
- if (ret)
- goto out_unlock;
-
- btrfs_lock_extent(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
- &cached_state);
-
- io_ctl_set_generation(io_ctl, trans->transid);
-
- mutex_lock(&ctl->cache_writeout_mutex);
- /* Write out the extent entries in the free space cache */
- spin_lock(&ctl->tree_lock);
- ret = write_cache_extent_entries(io_ctl, block_group, &entries, &bitmaps,
- &bitmap_list);
- if (ret)
- goto out_nospc_locked;
-
- /*
- * Some spaces that are freed in the current transaction are pinned,
- * they will be added into free space cache after the transaction is
- * committed, we shouldn't lose them.
- *
- * If this changes while we are working we'll get added back to
- * the dirty list and redo it. No locking needed
- */
- ret = write_pinned_extent_entries(trans, block_group, io_ctl, &entries);
- if (ret)
- goto out_nospc_locked;
-
- /*
- * At last, we write out all the bitmaps and keep cache_writeout_mutex
- * locked while doing it because a concurrent trim can be manipulating
- * or freeing the bitmap.
- */
- ret = write_bitmap_entries(io_ctl, &bitmap_list);
- spin_unlock(&ctl->tree_lock);
- mutex_unlock(&ctl->cache_writeout_mutex);
- if (ret)
- goto out_nospc;
-
- /* Zero out the rest of the pages just to make sure */
- io_ctl_zero_remaining_pages(io_ctl);
-
- /* Everything is written out, now we dirty the pages in the file. */
- i_size = i_size_read(inode);
- for (int i = 0; i < round_up(i_size, PAGE_SIZE) / PAGE_SIZE; i++) {
- u64 dirty_start = i * PAGE_SIZE;
- u64 dirty_len = min_t(u64, dirty_start + PAGE_SIZE, i_size) - dirty_start;
-
- ret = btrfs_dirty_folio(BTRFS_I(inode), page_folio(io_ctl->pages[i]),
- dirty_start, dirty_len, &cached_state, false);
- if (ret < 0)
- goto out_nospc;
- }
-
- if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
- up_write(&block_group->data_rwsem);
- /*
- * Release the pages and unlock the extent, we will flush
- * them out later
- */
- io_ctl_drop_pages(io_ctl);
- io_ctl_free(io_ctl);
-
- btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
- &cached_state);
-
- /*
- * at this point the pages are under IO and we're happy,
- * The caller is responsible for waiting on them and updating
- * the cache and the inode
- */
- io_ctl->entries = entries;
- io_ctl->bitmaps = bitmaps;
-
- ret = btrfs_fdatawrite_range(BTRFS_I(inode), 0, (u64)-1);
- if (ret)
- goto out;
-
- return 0;
-
-out_nospc_locked:
- cleanup_bitmap_list(&bitmap_list);
- spin_unlock(&ctl->tree_lock);
- mutex_unlock(&ctl->cache_writeout_mutex);
-
-out_nospc:
- cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
-
-out_unlock:
- if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
- up_write(&block_group->data_rwsem);
-
-out:
- io_ctl->inode = NULL;
- io_ctl_free(io_ctl);
- if (ret) {
- invalidate_inode_pages2(inode->i_mapping);
- BTRFS_I(inode)->generation = 0;
- }
- btrfs_update_inode(trans, BTRFS_I(inode));
- if (must_iput)
- iput(inode);
- return ret;
-}
-
-int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
- struct btrfs_block_group *block_group,
- struct btrfs_path *path)
-{
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct inode *inode;
- int ret = 0;
-
- spin_lock(&block_group->lock);
- if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
- spin_unlock(&block_group->lock);
- return 0;
- }
- spin_unlock(&block_group->lock);
-
- inode = lookup_free_space_inode(block_group, path);
- if (IS_ERR(inode))
- return 0;
-
- ret = __btrfs_write_out_cache(inode, block_group, trans);
- if (ret) {
- btrfs_debug(fs_info,
- "failed to write free space cache for block group %llu error %d",
- block_group->start, ret);
- spin_lock(&block_group->lock);
- block_group->disk_cache_state = BTRFS_DC_ERROR;
- spin_unlock(&block_group->lock);
-
- block_group->io_ctl.inode = NULL;
- iput(inode);
- }
-
- /*
- * if ret == 0 the caller is expected to call btrfs_wait_cache_io
- * to wait for IO and put the inode
- */
-
- return ret;
-}
-
static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
u64 offset)
{
diff --git a/fs/btrfs/free-space-cache.h b/fs/btrfs/free-space-cache.h
index 53fe8e293af1..2432f1783f47 100644
--- a/fs/btrfs/free-space-cache.h
+++ b/fs/btrfs/free-space-cache.h
@@ -105,23 +105,13 @@ int __init btrfs_free_space_init(void);
void __cold btrfs_free_space_exit(void);
struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
struct btrfs_path *path);
-int create_free_space_inode(struct btrfs_trans_handle *trans,
- struct btrfs_block_group *block_group,
- struct btrfs_path *path);
int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
struct inode *inode,
struct btrfs_block_group *block_group);
int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
- struct btrfs_block_group *block_group,
struct inode *inode);
int load_free_space_cache(struct btrfs_block_group *block_group);
-int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
- struct btrfs_block_group *block_group,
- struct btrfs_path *path);
-int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
- struct btrfs_block_group *block_group,
- struct btrfs_path *path);
void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group,
struct btrfs_free_space_ctl *ctl);
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 3668cbc7598e..9c395d075a65 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -9364,15 +9364,6 @@ int btrfs_prealloc_file_range(struct inode *inode, int mode,
NULL);
}
-int btrfs_prealloc_file_range_trans(struct inode *inode,
- struct btrfs_trans_handle *trans, int mode,
- u64 start, u64 num_bytes, u64 min_size,
- loff_t actual_len, u64 *alloc_hint)
-{
- return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
- min_size, actual_len, alloc_hint, trans);
-}
-
/*
* NOTE: in case you are adding MAY_EXEC check for directories:
* we are marking them with IOP_FASTPERM_MAY_EXEC, allowing path lookup to
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index da54db75e7a9..630a7ad8f8e1 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -3357,7 +3357,7 @@ static int delete_block_group_cache(struct btrfs_block_group *block_group,
goto out;
}
- ret = btrfs_truncate_free_space_cache(trans, block_group, inode);
+ ret = btrfs_truncate_free_space_cache(trans, inode);
btrfs_end_transaction(trans);
btrfs_btree_balance_dirty(fs_info);
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 05/17] btrfs: rename cache_write_mutex to dirty_bgs_update_mutex
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (3 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 04/17] btrfs: remove the v1 space cache write path Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 06/17] btrfs: drop the transaction handle from the prealloc helpers Tal Zussman
` (11 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
The v1 space cache writeout is gone, but the mutex is still needed. It
keeps btrfs_remove_block_group() from deleting a block group item while
btrfs_start_dirty_block_groups() is updating it outside the commit
critical section.
Rename it to reflect what it protects, and update the comments around
the dirty block group writeout that still refer to the space cache.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/block-group.c | 46 +++++++++++++++++++++++++---------------------
fs/btrfs/transaction.c | 25 ++++++++++---------------
fs/btrfs/transaction.h | 8 ++++----
3 files changed, 39 insertions(+), 40 deletions(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 5c2a57243de5..34f2cc0ef33b 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -1196,7 +1196,11 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
inode = lookup_free_space_inode(block_group, path);
- mutex_lock(&trans->transaction->cache_write_mutex);
+ /*
+ * Do not delete the block group item while
+ * btrfs_start_dirty_block_groups() is updating it.
+ */
+ mutex_lock(&trans->transaction->dirty_bgs_update_mutex);
spin_lock(&trans->transaction->dirty_bgs_lock);
if (!list_empty(&block_group->dirty_list)) {
list_del_init(&block_group->dirty_list);
@@ -1204,7 +1208,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
btrfs_put_block_group(block_group);
}
spin_unlock(&trans->transaction->dirty_bgs_lock);
- mutex_unlock(&trans->transaction->cache_write_mutex);
+ mutex_unlock(&trans->transaction->dirty_bgs_update_mutex);
ret = btrfs_remove_free_space_inode(trans, inode, block_group);
if (unlikely(ret)) {
@@ -3353,15 +3357,15 @@ static int update_block_group_item(struct btrfs_trans_handle *trans,
}
/*
- * Transaction commit does final block group cache writeback during a critical
+ * Transaction commit does the final block group item updates during a critical
* section where nothing is allowed to change the FS. This is required in
- * order for the cache to actually match the block group, but can introduce a
+ * order for the items to actually match the block groups, but can introduce a
* lot of latency into the commit.
*
- * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
- * There's a chance we'll have to redo some of it if the block group changes
- * again during the commit, but it greatly reduces the commit latency by
- * getting rid of the easy block groups while we're still allowing others to
+ * So, btrfs_start_dirty_block_groups is here to update the block group items
+ * early. There's a chance we'll have to redo some of it if the block group
+ * changes again during the commit, but it greatly reduces the commit latency
+ * by getting rid of the easy block groups while we're still allowing others to
* join the commit.
*/
int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
@@ -3395,11 +3399,11 @@ int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
}
/*
- * cache_write_mutex is here only to save us from balance or automatic
- * removal of empty block groups deleting this block group while we are
- * updating its item
+ * dirty_bgs_update_mutex is here only to save us from balance or
+ * automatic removal of empty block groups deleting this block group
+ * while we are updating its item
*/
- mutex_lock(&trans->transaction->cache_write_mutex);
+ mutex_lock(&trans->transaction->dirty_bgs_update_mutex);
while (!list_empty(&dirty)) {
bool drop_reserve = true;
@@ -3441,12 +3445,12 @@ int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
if (drop_reserve)
btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
/* Avoid blocking other tasks for too long. */
- mutex_unlock(&trans->transaction->cache_write_mutex);
+ mutex_unlock(&trans->transaction->dirty_bgs_update_mutex);
if (ret)
goto out;
- mutex_lock(&trans->transaction->cache_write_mutex);
+ mutex_lock(&trans->transaction->dirty_bgs_update_mutex);
}
- mutex_unlock(&trans->transaction->cache_write_mutex);
+ mutex_unlock(&trans->transaction->dirty_bgs_update_mutex);
/*
* Go through delayed refs for all the stuff we've just kicked off
@@ -3460,7 +3464,7 @@ int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
list_splice_init(&cur_trans->dirty_bgs, &dirty);
/*
* dirty_bgs_lock protects us from concurrent block group
- * deletes too (not just cache_write_mutex).
+ * deletes too (not just dirty_bgs_update_mutex).
*/
if (!list_empty(&dirty)) {
spin_unlock(&cur_trans->dirty_bgs_lock);
@@ -3556,10 +3560,10 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
factor = btrfs_bg_type_to_factor(cache->flags);
/*
- * If this block group has free space cache written out, we need to make
- * sure to load it if we are removing space. This is because we need
- * the unpinning stage to actually add the space back to the block group,
- * otherwise we will leak space.
+ * Make sure the free space of this block group is loaded if we are
+ * removing space. This is because we need the unpinning stage to
+ * actually add the space back to the block group, otherwise we will
+ * leak space.
*/
if (!alloc && !btrfs_block_group_done(cache))
btrfs_cache_block_group(cache, true);
@@ -3615,7 +3619,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
/*
* No longer have used bytes in this block group, queue it for deletion.
* We do this after adding the block group to the dirty list to avoid
- * races between cleaner kthread and space cache writeout.
+ * races between the cleaner kthread and the dirty block group writeout.
*/
if (!alloc && old_val == 0) {
if (!btrfs_test_opt(info, DISCARD_ASYNC))
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 6a36d29406c3..6a7f33ac87f2 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -380,7 +380,7 @@ static noinline int join_transaction(struct btrfs_fs_info *fs_info,
INIT_LIST_HEAD(&cur_trans->switch_commits);
INIT_LIST_HEAD(&cur_trans->dirty_bgs);
INIT_LIST_HEAD(&cur_trans->dropped_roots);
- mutex_init(&cur_trans->cache_write_mutex);
+ mutex_init(&cur_trans->dirty_bgs_update_mutex);
spin_lock_init(&cur_trans->dirty_bgs_lock);
INIT_LIST_HEAD(&cur_trans->deleted_bgs);
spin_lock_init(&cur_trans->dropped_roots_lock);
@@ -2268,18 +2268,16 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
bool run_it = false;
- /* this mutex is also taken before trying to set
- * block groups readonly. We need to make sure
- * that nobody has set a block group readonly
- * after a extents from that block group have been
- * allocated for cache files. btrfs_set_block_group_ro
- * will wait for the transaction to commit if it
- * finds BTRFS_TRANS_DIRTY_BG_RUN set.
+ /*
+ * This mutex is also taken before trying to set block groups
+ * readonly. btrfs_inc_block_group_ro() will wait for the
+ * transaction to commit if it finds BTRFS_TRANS_DIRTY_BG_RUN
+ * set.
*
* The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
- * only one process starts all the block group IO. It wouldn't
- * hurt to have more than one go through, but there's no
- * real advantage to it either.
+ * only one process starts all the block group item updates. It
+ * wouldn't hurt to have more than one go through, but there's
+ * no real advantage to it either.
*/
mutex_lock(&fs_info->ro_block_group_mutex);
if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
@@ -2513,10 +2511,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
if (unlikely(ret))
goto unlock_reloc;
- /*
- * The tasks which save the space cache and inode cache may also
- * update ->aborted, check it.
- */
+ /* Other tasks may also have updated ->aborted, check it. */
if (TRANS_ABORTED(cur_trans)) {
ret = cur_trans->aborted;
goto unlock_reloc;
diff --git a/fs/btrfs/transaction.h b/fs/btrfs/transaction.h
index 8245a58587c0..33a1884c5765 100644
--- a/fs/btrfs/transaction.h
+++ b/fs/btrfs/transaction.h
@@ -81,11 +81,11 @@ struct btrfs_transaction {
struct extent_io_tree pinned_extents;
/*
- * we need to make sure block group deletion doesn't race with
- * free space cache writeout. This mutex keeps them from stomping
- * on each other
+ * We need to make sure block group deletion doesn't race with the
+ * dirty block group item updates done outside the commit critical
+ * section. This mutex keeps them from stomping on each other.
*/
- struct mutex cache_write_mutex;
+ struct mutex dirty_bgs_update_mutex;
spinlock_t dirty_bgs_lock;
/* Protected by spin lock fs_info->unused_bgs_lock. */
struct list_head deleted_bgs;
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 06/17] btrfs: drop the transaction handle from the prealloc helpers
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (4 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 05/17] btrfs: rename cache_write_mutex to dirty_bgs_update_mutex Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 07/17] btrfs: remove the v1 space cache load path Tal Zussman
` (10 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
The v1 space cache created its inode during the transaction commit, and
btrfs_prealloc_file_range_trans() existed so that preallocation could
reuse the open handle. It was the only caller passing a transaction, so
__btrfs_prealloc_file_range() and insert_prealloc_file_extent() now
always start their own. Fold the wrapper into
btrfs_prealloc_file_range() and drop the parameter.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/inode.c | 47 ++++++++++-------------------------------------
1 file changed, 10 insertions(+), 37 deletions(-)
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 9c395d075a65..0539f27776e1 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -9132,14 +9132,13 @@ static int btrfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
}
static struct btrfs_trans_handle *insert_prealloc_file_extent(
- struct btrfs_trans_handle *trans_in,
struct btrfs_inode *inode,
struct btrfs_key *ins,
u64 file_offset)
{
struct btrfs_file_extent_item stack_fi;
struct btrfs_replace_extent_info extent_info;
- struct btrfs_trans_handle *trans = trans_in;
+ struct btrfs_trans_handle *trans;
struct btrfs_path *path;
u64 start = ins->objectid;
u64 len = ins->offset;
@@ -9160,15 +9159,6 @@ static struct btrfs_trans_handle *insert_prealloc_file_extent(
if (ret < 0)
return ERR_PTR(ret);
- if (trans) {
- ret = insert_reserved_file_extent(trans, inode,
- file_offset, &stack_fi,
- true, qgroup_released);
- if (ret)
- goto free_qgroup;
- return trans;
- }
-
extent_info.disk_offset = start;
extent_info.disk_len = len;
extent_info.data_offset = 0;
@@ -9208,12 +9198,12 @@ static struct btrfs_trans_handle *insert_prealloc_file_extent(
return ERR_PTR(ret);
}
-static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
- u64 start, u64 num_bytes, u64 min_size,
- loff_t actual_len, u64 *alloc_hint,
- struct btrfs_trans_handle *trans)
+int btrfs_prealloc_file_range(struct inode *inode, int mode,
+ u64 start, u64 num_bytes, u64 min_size,
+ loff_t actual_len, u64 *alloc_hint)
{
struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
+ struct btrfs_trans_handle *trans;
struct extent_map *em;
struct btrfs_root *root = BTRFS_I(inode)->root;
struct btrfs_key ins;
@@ -9223,11 +9213,8 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
u64 cur_bytes;
u64 last_alloc = (u64)-1;
int ret = 0;
- bool own_trans = true;
u64 end = start + num_bytes - 1;
- if (trans)
- own_trans = false;
while (num_bytes > 0) {
cur_bytes = min_t(u64, num_bytes, SZ_256M);
cur_bytes = max(cur_bytes, min_size);
@@ -9253,8 +9240,8 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
clear_offset += ins.offset;
last_alloc = ins.offset;
- trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
- &ins, cur_offset);
+ trans = insert_prealloc_file_extent(BTRFS_I(inode), &ins,
+ cur_offset);
/*
* Now that we inserted the prealloc extent we can finally
* decrement the number of reservations in the block group.
@@ -9326,8 +9313,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
range_start, range_end - range_start);
if (ret) {
btrfs_abort_transaction(trans, ret);
- if (own_trans)
- btrfs_end_transaction(trans);
+ btrfs_end_transaction(trans);
break;
}
@@ -9339,15 +9325,11 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
- if (own_trans)
- btrfs_end_transaction(trans);
+ btrfs_end_transaction(trans);
break;
}
- if (own_trans) {
- btrfs_end_transaction(trans);
- trans = NULL;
- }
+ btrfs_end_transaction(trans);
}
if (clear_offset < end)
btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
@@ -9355,15 +9337,6 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
return ret;
}
-int btrfs_prealloc_file_range(struct inode *inode, int mode,
- u64 start, u64 num_bytes, u64 min_size,
- loff_t actual_len, u64 *alloc_hint)
-{
- return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
- min_size, actual_len, alloc_hint,
- NULL);
-}
-
/*
* NOTE: in case you are adding MAY_EXEC check for directories:
* we are marking them with IOP_FASTPERM_MAY_EXEC, allowing path lookup to
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 07/17] btrfs: remove the v1 space cache load path
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (5 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 06/17] btrfs: drop the transaction handle from the prealloc helpers Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 08/17] btrfs: remove btrfs_disk_cache_state Tal Zussman
` (9 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
Nothing writes a v1 space cache any more, and since commit 545e560a5b0f
("btrfs: disable v1 space cache") the mount option can't be enabled to
read one either. Remove load_free_space_cache(), its io_ctl helpers and
struct btrfs_io_ctl. Drop the gfp constraint on the inode mapping as
well, it only covered the cache's page cache allocations.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/block-group.c | 18 +-
fs/btrfs/free-space-cache.c | 565 --------------------------------------------
fs/btrfs/free-space-cache.h | 15 --
3 files changed, 1 insertion(+), 597 deletions(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 34f2cc0ef33b..972909b52a39 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -904,22 +904,6 @@ static noinline void caching_thread(struct btrfs_work *work)
down_read(&fs_info->commit_root_sem);
load_block_group_size_class(caching_ctl);
- if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
- ret = load_free_space_cache(block_group);
- if (ret == 1) {
- ret = 0;
- goto done;
- }
-
- /*
- * We failed to load the space cache, set ourselves to
- * CACHE_STARTED and carry on.
- */
- spin_lock(&block_group->lock);
- block_group->cached = BTRFS_CACHE_STARTED;
- spin_unlock(&block_group->lock);
- wake_up(&caching_ctl->wait);
- }
/*
* If we are in the transaction that populated the free space tree we
@@ -933,7 +917,7 @@ static noinline void caching_thread(struct btrfs_work *work)
ret = btrfs_load_free_space_tree(caching_ctl);
else
ret = load_extent_tree_free(caching_ctl);
-done:
+
spin_lock(&block_group->lock);
block_group->caching_ctl = NULL;
block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index 336b546b0a94..a25c4db561b4 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -9,7 +9,6 @@
#include <linux/slab.h>
#include <linux/math64.h>
#include <linux/ratelimit.h>
-#include <linux/error-injection.h>
#include <linux/sched/mm.h>
#include <linux/string_choices.h>
#include "extent-tree.h"
@@ -23,7 +22,6 @@
#include "space-info.h"
#include "block-group.h"
#include "discard.h"
-#include "subpage.h"
#include "inode-item.h"
#include "accessors.h"
#include "file-item.h"
@@ -57,11 +55,6 @@ static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
struct btrfs_free_space *info, u64 offset,
u64 bytes, bool update_stats);
-static void btrfs_crc32c_final(u32 crc, u8 *result)
-{
- put_unaligned_le32(~crc, result);
-}
-
static void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
{
struct btrfs_free_space *info;
@@ -123,10 +116,6 @@ static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
if (IS_ERR(inode))
return ERR_CAST(inode);
- mapping_set_gfp_mask(inode->vfs_inode.i_mapping,
- mapping_gfp_constraint(inode->vfs_inode.i_mapping,
- ~(__GFP_FS | __GFP_HIGHMEM)));
-
return &inode->vfs_inode;
}
@@ -262,226 +251,6 @@ int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
return ret;
}
-static void readahead_cache(struct inode *inode)
-{
- struct file_ra_state ra;
- pgoff_t last_index;
-
- file_ra_state_init(&ra, inode->i_mapping);
- last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
-
- page_cache_sync_readahead(inode->i_mapping, &ra, NULL, 0, last_index);
-}
-
-static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
- int write)
-{
- int num_pages;
-
- num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
-
- /* Make sure we can fit our crcs and generation into the first page */
- if (write && (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
- return -ENOSPC;
-
- memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
-
- io_ctl->pages = kzalloc_objs(struct page *, num_pages, GFP_NOFS);
- if (!io_ctl->pages)
- return -ENOMEM;
-
- io_ctl->num_pages = num_pages;
- io_ctl->fs_info = inode_to_fs_info(inode);
- io_ctl->inode = inode;
-
- return 0;
-}
-ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
-
-static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
-{
- kfree(io_ctl->pages);
- io_ctl->pages = NULL;
-}
-
-static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
-{
- if (io_ctl->cur) {
- io_ctl->cur = NULL;
- io_ctl->orig = NULL;
- }
-}
-
-static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
-{
- ASSERT(io_ctl->index < io_ctl->num_pages);
- io_ctl->page = io_ctl->pages[io_ctl->index++];
- io_ctl->cur = page_address(io_ctl->page);
- io_ctl->orig = io_ctl->cur;
- io_ctl->size = PAGE_SIZE;
- if (clear)
- clear_page(io_ctl->cur);
-}
-
-static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
-{
- int i;
-
- io_ctl_unmap_page(io_ctl);
-
- for (i = 0; i < io_ctl->num_pages; i++) {
- if (io_ctl->pages[i]) {
- unlock_page(io_ctl->pages[i]);
- put_page(io_ctl->pages[i]);
- }
- }
-}
-
-static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, bool uptodate)
-{
- struct folio *folio;
- struct inode *inode = io_ctl->inode;
- gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
- int i;
-
- for (i = 0; i < io_ctl->num_pages; i++) {
- int ret;
-
- folio = __filemap_get_folio(inode->i_mapping, i,
- FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
- mask);
- if (IS_ERR(folio)) {
- io_ctl_drop_pages(io_ctl);
- return PTR_ERR(folio);
- }
-
- ret = set_folio_extent_mapped(folio);
- if (ret < 0) {
- folio_unlock(folio);
- folio_put(folio);
- io_ctl_drop_pages(io_ctl);
- return ret;
- }
-
- io_ctl->pages[i] = &folio->page;
- if (uptodate && !folio_test_uptodate(folio)) {
- btrfs_read_folio(NULL, folio);
- folio_lock(folio);
- if (folio->mapping != inode->i_mapping) {
- btrfs_err(BTRFS_I(inode)->root->fs_info,
- "free space cache page truncated");
- io_ctl_drop_pages(io_ctl);
- return -EIO;
- }
- if (!folio_test_uptodate(folio)) {
- btrfs_err(BTRFS_I(inode)->root->fs_info,
- "error reading free space cache");
- io_ctl_drop_pages(io_ctl);
- return -EIO;
- }
- }
- }
-
- for (i = 0; i < io_ctl->num_pages; i++)
- clear_page_dirty_for_io(io_ctl->pages[i]);
-
- return 0;
-}
-
-static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
-{
- u64 cache_gen;
-
- /*
- * Skip the crc area. If we don't check crcs then we just have a 64bit
- * chunk at the front of the first page.
- */
- io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
- io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
-
- cache_gen = get_unaligned_le64(io_ctl->cur);
- if (cache_gen != generation) {
- btrfs_err_rl(io_ctl->fs_info,
- "space cache generation (%llu) does not match inode (%llu)",
- cache_gen, generation);
- io_ctl_unmap_page(io_ctl);
- return -EIO;
- }
- io_ctl->cur += sizeof(u64);
- return 0;
-}
-
-static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
-{
- u32 *tmp, val;
- u32 crc = ~(u32)0;
- unsigned offset = 0;
-
- if (index >= io_ctl->num_pages)
- return -EIO;
-
- if (index == 0)
- offset = sizeof(u32) * io_ctl->num_pages;
-
- tmp = page_address(io_ctl->pages[0]);
- tmp += index;
- val = *tmp;
-
- io_ctl_map_page(io_ctl, 0);
- crc = crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
- btrfs_crc32c_final(crc, (u8 *)&crc);
- if (val != crc) {
- btrfs_err_rl(io_ctl->fs_info,
- "csum mismatch on free space cache");
- io_ctl_unmap_page(io_ctl);
- return -EIO;
- }
-
- return 0;
-}
-
-static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
- struct btrfs_free_space *entry, u8 *type)
-{
- struct btrfs_free_space_entry *e;
- int ret;
-
- if (!io_ctl->cur) {
- ret = io_ctl_check_crc(io_ctl, io_ctl->index);
- if (ret)
- return ret;
- }
-
- e = io_ctl->cur;
- entry->offset = get_unaligned_le64(&e->offset);
- entry->bytes = get_unaligned_le64(&e->bytes);
- *type = e->type;
- io_ctl->cur += sizeof(struct btrfs_free_space_entry);
- io_ctl->size -= sizeof(struct btrfs_free_space_entry);
-
- if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
- return 0;
-
- io_ctl_unmap_page(io_ctl);
-
- return 0;
-}
-
-static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
- struct btrfs_free_space *entry)
-{
- int ret;
-
- ret = io_ctl_check_crc(io_ctl, io_ctl->index);
- if (ret)
- return ret;
-
- copy_page(entry->bitmap, io_ctl->cur);
- io_ctl_unmap_page(io_ctl);
-
- return 0;
-}
-
static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
{
struct btrfs_block_group *block_group = ctl->block_group;
@@ -527,340 +296,6 @@ static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
div_u64(extent_bytes, sizeof(struct btrfs_free_space));
}
-static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
- struct btrfs_free_space_ctl *ctl,
- struct btrfs_path *path, u64 offset)
-{
- struct btrfs_fs_info *fs_info = root->fs_info;
- struct btrfs_free_space_header *header;
- struct extent_buffer *leaf;
- struct btrfs_io_ctl io_ctl;
- struct btrfs_key key;
- struct btrfs_free_space *e, *n;
- LIST_HEAD(bitmaps);
- u64 num_entries;
- u64 num_bitmaps;
- u64 generation;
- u8 type;
- int ret = 0;
-
- /* Nothing in the space cache, goodbye */
- if (!i_size_read(inode))
- return 0;
-
- key.objectid = BTRFS_FREE_SPACE_OBJECTID;
- key.type = 0;
- key.offset = offset;
-
- ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
- if (ret < 0)
- return 0;
- else if (ret > 0) {
- btrfs_release_path(path);
- return 0;
- }
-
- ret = -1;
-
- leaf = path->nodes[0];
- header = btrfs_item_ptr(leaf, path->slots[0],
- struct btrfs_free_space_header);
- num_entries = btrfs_free_space_entries(leaf, header);
- num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
- generation = btrfs_free_space_generation(leaf, header);
- btrfs_release_path(path);
-
- if (!BTRFS_I(inode)->generation) {
- btrfs_info(fs_info,
- "the free space cache file (%llu) is invalid, skip it",
- offset);
- return 0;
- }
-
- if (BTRFS_I(inode)->generation != generation) {
- btrfs_err(fs_info,
- "free space inode generation (%llu) did not match free space cache generation (%llu)",
- BTRFS_I(inode)->generation, generation);
- return 0;
- }
-
- if (!num_entries)
- return 0;
-
- ret = io_ctl_init(&io_ctl, inode, 0);
- if (ret)
- return ret;
-
- readahead_cache(inode);
-
- ret = io_ctl_prepare_pages(&io_ctl, true);
- if (ret)
- goto out;
-
- ret = io_ctl_check_crc(&io_ctl, 0);
- if (ret)
- goto free_cache;
-
- ret = io_ctl_check_generation(&io_ctl, generation);
- if (ret)
- goto free_cache;
-
- while (num_entries) {
- e = kmem_cache_zalloc(btrfs_free_space_cachep,
- GFP_NOFS);
- if (!e) {
- ret = -ENOMEM;
- goto free_cache;
- }
-
- ret = io_ctl_read_entry(&io_ctl, e, &type);
- if (ret) {
- kmem_cache_free(btrfs_free_space_cachep, e);
- goto free_cache;
- }
-
- if (!e->bytes) {
- ret = -1;
- kmem_cache_free(btrfs_free_space_cachep, e);
- goto free_cache;
- }
-
- if (type == BTRFS_FREE_SPACE_EXTENT) {
- spin_lock(&ctl->tree_lock);
- ret = link_free_space(ctl, e);
- spin_unlock(&ctl->tree_lock);
- if (ret) {
- btrfs_err(fs_info,
- "Duplicate entries in free space cache, dumping");
- kmem_cache_free(btrfs_free_space_cachep, e);
- goto free_cache;
- }
- } else {
- ASSERT(num_bitmaps);
- num_bitmaps--;
- e->bitmap = kmem_cache_zalloc(
- btrfs_free_space_bitmap_cachep, GFP_NOFS);
- if (!e->bitmap) {
- ret = -ENOMEM;
- kmem_cache_free(
- btrfs_free_space_cachep, e);
- goto free_cache;
- }
- spin_lock(&ctl->tree_lock);
- ret = link_free_space(ctl, e);
- if (ret) {
- spin_unlock(&ctl->tree_lock);
- btrfs_err(fs_info,
- "Duplicate entries in free space cache, dumping");
- kmem_cache_free(btrfs_free_space_bitmap_cachep, e->bitmap);
- kmem_cache_free(btrfs_free_space_cachep, e);
- goto free_cache;
- }
- ctl->total_bitmaps++;
- recalculate_thresholds(ctl);
- spin_unlock(&ctl->tree_lock);
- list_add_tail(&e->list, &bitmaps);
- }
-
- num_entries--;
- }
-
- io_ctl_unmap_page(&io_ctl);
-
- /*
- * We add the bitmaps at the end of the entries in order that
- * the bitmap entries are added to the cache.
- */
- list_for_each_entry_safe(e, n, &bitmaps, list) {
- list_del_init(&e->list);
- ret = io_ctl_read_bitmap(&io_ctl, e);
- if (ret)
- goto free_cache;
- }
-
- io_ctl_drop_pages(&io_ctl);
- ret = 1;
-out:
- io_ctl_free(&io_ctl);
- return ret;
-free_cache:
- io_ctl_drop_pages(&io_ctl);
-
- spin_lock(&ctl->tree_lock);
- __btrfs_remove_free_space_cache(ctl);
- spin_unlock(&ctl->tree_lock);
- goto out;
-}
-
-static int copy_free_space_cache(struct btrfs_free_space_ctl *ctl)
-{
- struct btrfs_free_space *info;
- struct rb_node *n;
- int ret = 0;
-
- while (!ret && (n = rb_first(&ctl->free_space_offset)) != NULL) {
- info = rb_entry(n, struct btrfs_free_space, offset_index);
- if (!info->bitmap) {
- const u64 offset = info->offset;
- const u64 bytes = info->bytes;
-
- unlink_free_space(ctl, info, true);
- spin_unlock(&ctl->tree_lock);
- kmem_cache_free(btrfs_free_space_cachep, info);
- ret = btrfs_add_free_space(ctl->block_group, offset, bytes);
- spin_lock(&ctl->tree_lock);
- } else {
- u64 offset = info->offset;
- u64 bytes = ctl->block_group->fs_info->sectorsize;
-
- ret = search_bitmap(ctl, info, &offset, &bytes, false);
- if (ret == 0) {
- bitmap_clear_bits(ctl, info, offset, bytes, true);
- spin_unlock(&ctl->tree_lock);
- ret = btrfs_add_free_space(ctl->block_group, offset,
- bytes);
- spin_lock(&ctl->tree_lock);
- } else {
- free_bitmap(ctl, info);
- ret = 0;
- }
- }
- cond_resched_lock(&ctl->tree_lock);
- }
- return ret;
-}
-
-static struct lock_class_key btrfs_free_space_inode_key;
-
-int load_free_space_cache(struct btrfs_block_group *block_group)
-{
- struct btrfs_fs_info *fs_info = block_group->fs_info;
- struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
- struct btrfs_free_space_ctl tmp_ctl = {};
- struct inode *inode;
- struct btrfs_path *path;
- int ret = 0;
- bool matched;
- u64 used = block_group->used;
-
- /*
- * Because we could potentially discard our loaded free space, we want
- * to load everything into a temporary structure first, and then if it's
- * valid copy it all into the actual free space ctl.
- */
- btrfs_init_free_space_ctl(block_group, &tmp_ctl);
-
- /*
- * If this block group has been marked to be cleared for one reason or
- * another then we can't trust the on disk cache, so just return.
- */
- spin_lock(&block_group->lock);
- if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
- spin_unlock(&block_group->lock);
- return 0;
- }
- spin_unlock(&block_group->lock);
-
- path = btrfs_alloc_path();
- if (!path)
- return 0;
- path->search_commit_root = true;
- path->skip_locking = true;
-
- /*
- * We must pass a path with search_commit_root set to btrfs_iget in
- * order to avoid a deadlock when allocating extents for the tree root.
- *
- * When we are COWing an extent buffer from the tree root, when looking
- * for a free extent, at extent-tree.c:find_free_extent(), we can find
- * block group without its free space cache loaded. When we find one
- * we must load its space cache which requires reading its free space
- * cache's inode item from the root tree. If this inode item is located
- * in the same leaf that we started COWing before, then we end up in
- * deadlock on the extent buffer (trying to read lock it when we
- * previously write locked it).
- *
- * It's safe to read the inode item using the commit root because
- * block groups, once loaded, stay in memory forever (until they are
- * removed) as well as their space caches once loaded. New block groups
- * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
- * we will never try to read their inode item while the fs is mounted.
- */
- inode = lookup_free_space_inode(block_group, path);
- if (IS_ERR(inode)) {
- btrfs_free_path(path);
- return 0;
- }
-
- /* We may have converted the inode and made the cache invalid. */
- spin_lock(&block_group->lock);
- if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
- spin_unlock(&block_group->lock);
- btrfs_free_path(path);
- goto out;
- }
- spin_unlock(&block_group->lock);
-
- /*
- * Reinitialize the class of struct inode's mapping->invalidate_lock for
- * free space inodes to prevent false positives related to locks for normal
- * inodes.
- */
- lockdep_set_class(&(&inode->i_data)->invalidate_lock,
- &btrfs_free_space_inode_key);
-
- ret = __load_free_space_cache(fs_info->tree_root, inode, &tmp_ctl,
- path, block_group->start);
- btrfs_free_path(path);
- if (ret <= 0)
- goto out;
-
- matched = (tmp_ctl.free_space == (block_group->length - used -
- block_group->bytes_super));
-
- if (matched) {
- spin_lock(&tmp_ctl.tree_lock);
- ret = copy_free_space_cache(&tmp_ctl);
- spin_unlock(&tmp_ctl.tree_lock);
- /*
- * ret == 1 means we successfully loaded the free space cache,
- * so we need to re-set it here.
- */
- if (ret == 0)
- ret = 1;
- } else {
- /*
- * We need to call the _locked variant so we don't try to update
- * the discard counters.
- */
- spin_lock(&tmp_ctl.tree_lock);
- __btrfs_remove_free_space_cache(&tmp_ctl);
- spin_unlock(&tmp_ctl.tree_lock);
- btrfs_warn(fs_info,
- "block group %llu has wrong amount of free space",
- block_group->start);
- ret = -1;
- }
-out:
- if (ret < 0) {
- /* This cache is bogus, make sure it gets cleared */
- spin_lock(&block_group->lock);
- block_group->disk_cache_state = BTRFS_DC_CLEAR;
- spin_unlock(&block_group->lock);
- ret = 0;
-
- btrfs_warn(fs_info,
- "failed to load free space cache for block group %llu, rebuilding it now",
- block_group->start);
- }
-
- spin_lock(&ctl->tree_lock);
- btrfs_discard_update_discardable(block_group);
- spin_unlock(&ctl->tree_lock);
- iput(inode);
- return ret;
-}
-
static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
u64 offset)
{
diff --git a/fs/btrfs/free-space-cache.h b/fs/btrfs/free-space-cache.h
index 2432f1783f47..29166cc09b90 100644
--- a/fs/btrfs/free-space-cache.h
+++ b/fs/btrfs/free-space-cache.h
@@ -14,7 +14,6 @@
#include "fs.h"
struct inode;
-struct page;
struct btrfs_fs_info;
struct btrfs_path;
struct btrfs_trans_handle;
@@ -88,19 +87,6 @@ struct btrfs_free_space_ctl {
struct list_head trimming_ranges;
};
-struct btrfs_io_ctl {
- void *cur, *orig;
- struct page *page;
- struct page **pages;
- struct btrfs_fs_info *fs_info;
- struct inode *inode;
- unsigned long size;
- int index;
- int num_pages;
- int entries;
- int bitmaps;
-};
-
int __init btrfs_free_space_init(void);
void __cold btrfs_free_space_exit(void);
struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
@@ -111,7 +97,6 @@ int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
struct inode *inode);
-int load_free_space_cache(struct btrfs_block_group *block_group);
void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group,
struct btrfs_free_space_ctl *ctl);
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 08/17] btrfs: remove btrfs_disk_cache_state
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (6 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 07/17] btrfs: remove the v1 space cache load path Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 09/17] btrfs: remove the SPACE_CACHE mount option flag Tal Zussman
` (8 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
With neither the writer nor the loader left, nothing acts on
disk_cache_state. Remove it, the need_clear handling when reading block
groups, and the enum. While at it, drop the unused cache_generation
field from struct btrfs_block_group.
lookup_free_space_inode() converted old style space inodes by clearing
disk_cache_state so the cache would be rewritten with the new inode
flags. Without that it only sets flags on the in-memory inode, which
every remaining caller truncates or deletes right after, so drop the
conversion too.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/block-group.c | 32 ++------------------------------
fs/btrfs/block-group.h | 10 ----------
fs/btrfs/disk-io.c | 4 ----
fs/btrfs/free-space-cache.c | 8 --------
4 files changed, 2 insertions(+), 52 deletions(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 972909b52a39..7c5f80f4aa79 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -2454,8 +2454,7 @@ static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
static int read_one_block_group(struct btrfs_fs_info *info,
struct btrfs_block_group_item_v2 *bgi,
- const struct btrfs_key *key,
- bool need_clear)
+ const struct btrfs_key *key)
{
struct btrfs_block_group *cache;
const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS);
@@ -2481,20 +2480,6 @@ static int read_one_block_group(struct btrfs_fs_info *info,
btrfs_set_free_space_tree_thresholds(cache);
- if (need_clear) {
- /*
- * When we mount with old space cache, we need to
- * set BTRFS_DC_CLEAR and set dirty flag.
- *
- * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
- * truncate the old free space cache inode and
- * setup a new one.
- * b) Setting 'dirty flag' makes sure that we flush
- * the new space cache info onto disk.
- */
- if (btrfs_test_opt(info, SPACE_CACHE))
- cache->disk_cache_state = BTRFS_DC_CLEAR;
- }
if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
(cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
btrfs_err(info,
@@ -2635,8 +2620,6 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
struct btrfs_block_group *cache;
struct btrfs_space_info *space_info;
struct btrfs_key key;
- bool need_clear = false;
- u64 cache_gen;
/*
* Either no extent root (with ibadroots rescue option) or we have
@@ -2657,13 +2640,6 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
if (!path)
return -ENOMEM;
- cache_gen = btrfs_super_cache_generation(info->super_copy);
- if (btrfs_test_opt(info, SPACE_CACHE) &&
- btrfs_super_generation(info->super_copy) != cache_gen)
- need_clear = true;
- if (btrfs_test_opt(info, CLEAR_CACHE))
- need_clear = true;
-
while (1) {
struct btrfs_block_group_item_v2 bgi;
struct extent_buffer *leaf;
@@ -2692,7 +2668,7 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
btrfs_item_key_to_cpu(leaf, &key, slot);
btrfs_release_path(path);
- ret = read_one_block_group(info, &bgi, &key, need_clear);
+ ret = read_one_block_group(info, &bgi, &key);
if (ret < 0)
goto error;
key.objectid += key.offset;
@@ -3555,10 +3531,6 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
spin_lock(&space_info->lock);
spin_lock(&cache->lock);
- if (btrfs_test_opt(info, SPACE_CACHE) &&
- cache->disk_cache_state < BTRFS_DC_CLEAR)
- cache->disk_cache_state = BTRFS_DC_CLEAR;
-
old_val = cache->used;
if (alloc) {
old_val += num_bytes;
diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h
index 939d4bfbd4b4..eac794a053cd 100644
--- a/fs/btrfs/block-group.h
+++ b/fs/btrfs/block-group.h
@@ -20,13 +20,6 @@ struct btrfs_fs_info;
struct btrfs_inode;
struct btrfs_trans_handle;
-enum btrfs_disk_cache_state {
- BTRFS_DC_WRITTEN,
- BTRFS_DC_ERROR,
- BTRFS_DC_CLEAR,
- BTRFS_DC_SETUP,
-};
-
enum btrfs_block_group_size_class {
/* Unset */
BTRFS_BG_SZ_NONE,
@@ -131,7 +124,6 @@ struct btrfs_block_group {
u64 delalloc_bytes;
u64 bytes_super;
u64 flags;
- u64 cache_generation;
u64 global_root_id;
u64 remap_bytes;
u32 identity_remap_count;
@@ -171,8 +163,6 @@ struct btrfs_block_group {
unsigned long full_stripe_len;
unsigned long runtime_flags;
- enum btrfs_disk_cache_state disk_cache_state;
-
/* Cache tracking stuff */
enum btrfs_caching_type cached;
struct btrfs_caching_control *caching_ctl;
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index f256a91f58b4..881d95080709 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -4864,10 +4864,6 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
dirty_list);
list_del_init(&cache->dirty_list);
- spin_lock(&cache->lock);
- cache->disk_cache_state = BTRFS_DC_ERROR;
- spin_unlock(&cache->lock);
-
spin_unlock(&cur_trans->dirty_bgs_lock);
btrfs_put_block_group(cache);
btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index a25c4db561b4..3ba9ed4a39d0 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -124,7 +124,6 @@ struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
{
struct btrfs_fs_info *fs_info = block_group->fs_info;
struct inode *inode = NULL;
- u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
spin_lock(&block_group->lock);
if (block_group->inode)
@@ -139,13 +138,6 @@ struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
return inode;
spin_lock(&block_group->lock);
- if (!((BTRFS_I(inode)->flags & flags) == flags)) {
- btrfs_info(fs_info, "Old style space inode found, converting.");
- BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
- BTRFS_INODE_NODATACOW;
- block_group->disk_cache_state = BTRFS_DC_CLEAR;
- }
-
if (!test_and_set_bit(BLOCK_GROUP_FLAG_IREF, &block_group->runtime_flags))
block_group->inode = BTRFS_I(igrab(inode));
spin_unlock(&block_group->lock);
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 09/17] btrfs: remove the SPACE_CACHE mount option flag
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (7 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 08/17] btrfs: remove btrfs_disk_cache_state Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 10/17] btrfs: replace btrfs_set_free_space_cache_v1_active() with a cleanup helper Tal Zussman
` (7 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
Nothing sets BTRFS_MOUNT_SPACE_CACHE anymore, so every test of it is
false. Remove the flag, the checks rejecting the v1 cache on zoned
filesystems and for sector sizes other than the page size, and the
deprecation warning. Show a read-only filesystem that still has an old
cache as nospace_cache, since that's what's in effect. space_cache and
space_cache=v1 keep falling back to no space cache with a warning.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/disk-io.c | 19 ++-----------------
fs/btrfs/fs.h | 1 -
fs/btrfs/super.c | 31 ++-----------------------------
fs/btrfs/transaction.c | 4 +---
fs/btrfs/zoned.c | 9 ---------
5 files changed, 5 insertions(+), 59 deletions(-)
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 881d95080709..2ac0e7c2f1e4 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -3054,7 +3054,6 @@ static int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info)
{
int ret;
- const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
bool rebuild_free_space_tree = false;
if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
@@ -3149,8 +3148,8 @@ int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info)
}
}
- if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) {
- ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
+ if (btrfs_free_space_cache_v1_active(fs_info)) {
+ ret = btrfs_set_free_space_cache_v1_active(fs_info, false);
if (ret)
return ret;
}
@@ -3266,20 +3265,6 @@ int btrfs_check_features(struct btrfs_fs_info *fs_info, bool is_rw_mount)
return -EINVAL;
}
- /*
- * Subpage/bs > ps runtime limitation on v1 cache.
- *
- * V1 space cache still has some hard coded PAGE_SIZE usage, while
- * we're already defaulting to v2 cache, no need to bother v1 as it's
- * going to be deprecated anyway.
- */
- if (fs_info->sectorsize != PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) {
- btrfs_warn(fs_info,
- "v1 space cache is not supported for page size %lu with sectorsize %u",
- PAGE_SIZE, fs_info->sectorsize);
- return -EINVAL;
- }
-
/* This can be called by remount, we need to protect the super block. */
spin_lock(&fs_info->super_lock);
btrfs_set_super_incompat_flags(disk_super, incompat);
diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
index 96cc8c50af82..ad4b2cbf64b7 100644
--- a/fs/btrfs/fs.h
+++ b/fs/btrfs/fs.h
@@ -259,7 +259,6 @@ enum {
BTRFS_MOUNT_NOSSD = (1ULL << 9),
BTRFS_MOUNT_DISCARD_SYNC = (1ULL << 10),
BTRFS_MOUNT_FORCE_COMPRESS = (1ULL << 11),
- BTRFS_MOUNT_SPACE_CACHE = (1ULL << 12),
BTRFS_MOUNT_CLEAR_CACHE = (1ULL << 13),
BTRFS_MOUNT_USER_SUBVOL_RM_ALLOWED = (1ULL << 14),
BTRFS_MOUNT_ENOSPC_DEBUG = (1ULL << 15),
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index b44b16970a62..6ddb7b352166 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -515,7 +515,6 @@ static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
btrfs_warn(NULL,
"v1 space cache is deprecated, falling back to no space cache");
btrfs_set_opt(ctx->mount_opt, NOSPACECACHE);
- btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
break;
case Opt_space_cache_version:
@@ -524,11 +523,9 @@ static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
btrfs_warn(NULL,
"v1 space cache is deprecated, falling back to no space cache");
btrfs_set_opt(ctx->mount_opt, NOSPACECACHE);
- btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
break;
case Opt_space_cache_v2:
- btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
btrfs_set_opt(ctx->mount_opt, FREE_SPACE_TREE);
break;
default:
@@ -705,13 +702,6 @@ bool btrfs_check_options(const struct btrfs_fs_info *info,
if (btrfs_check_mountopts_zoned(info, mount_opt))
ret = false;
- if (!test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state)) {
- if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE)) {
- btrfs_warn(info,
-"space cache v1 is being deprecated and will be removed in a future release, please use -o space_cache=v2");
- }
- }
-
return ret;
}
@@ -729,14 +719,6 @@ bool btrfs_check_options(const struct btrfs_fs_info *info,
*/
void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info)
{
- if (fs_info->sectorsize != PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) {
- btrfs_info(fs_info,
- "forcing free space tree for sector size %u with page size %lu",
- fs_info->sectorsize, PAGE_SIZE);
- btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
- btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
- }
-
/*
* At this point our mount options are populated, so we only mess with
* these settings if we don't have any settings already.
@@ -751,9 +733,6 @@ void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info)
return;
}
- if (btrfs_test_opt(fs_info, SPACE_CACHE))
- return;
-
if (btrfs_test_opt(fs_info, NOSPACECACHE))
return;
@@ -1107,9 +1086,7 @@ static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
seq_puts(seq, ",discard=async");
if (!(info->sb->s_flags & SB_POSIXACL))
seq_puts(seq, ",noacl");
- if (btrfs_free_space_cache_v1_active(info))
- seq_puts(seq, ",space_cache");
- else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
+ if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
seq_puts(seq, ",space_cache=v2");
else
seq_puts(seq, ",nospace_cache");
@@ -1441,7 +1418,6 @@ static void btrfs_emit_options(struct btrfs_fs_info *info,
btrfs_info_if_set(info, old, DISCARD_SYNC, "turning on sync discard");
btrfs_info_if_set(info, old, DISCARD_ASYNC, "turning on async discard");
btrfs_info_if_set(info, old, FREE_SPACE_TREE, "enabling free space tree");
- btrfs_info_if_set(info, old, SPACE_CACHE, "enabling disk space caching");
btrfs_info_if_set(info, old, CLEAR_CACHE, "force clearing of disk cache");
btrfs_info_if_set(info, old, AUTO_DEFRAG, "enabling auto defrag");
btrfs_info_if_set(info, old, FRAGMENT_DATA, "fragmenting data");
@@ -1459,7 +1435,6 @@ static void btrfs_emit_options(struct btrfs_fs_info *info,
btrfs_info_if_unset(info, old, SSD_SPREAD, "not using spread ssd allocation scheme");
btrfs_info_if_unset(info, old, NOBARRIER, "turning on barriers");
btrfs_info_if_unset(info, old, NOTREELOG, "enabling tree log");
- btrfs_info_if_unset(info, old, SPACE_CACHE, "disabling disk space caching");
btrfs_info_if_unset(info, old, FREE_SPACE_TREE, "disabling free space tree");
btrfs_info_if_unset(info, old, AUTO_DEFRAG, "disabling auto defrag");
btrfs_info_if_unset(info, old, COMPRESS, "use no compression");
@@ -1524,10 +1499,8 @@ static int btrfs_reconfigure(struct fs_context *fc)
btrfs_warn(fs_info,
"remount supports changing free space tree only from RO to RW");
/* Make sure free space cache options match the state on disk. */
- if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
+ if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
- btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
- }
}
ret = 0;
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 6a7f33ac87f2..0ea984d641e0 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -1984,9 +1984,7 @@ static void update_super_roots(struct btrfs_fs_info *fs_info)
super->root = root_item->bytenr;
super->generation = root_item->generation;
super->root_level = root_item->level;
- if (btrfs_test_opt(fs_info, SPACE_CACHE))
- super->cache_generation = root_item->generation;
- else if (test_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags))
+ if (test_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags))
super->cache_generation = 0;
if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
super->uuid_tree_generation = root_item->generation;
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 9cc2c9c1a606..c916053aefad 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -804,15 +804,6 @@ int btrfs_check_mountopts_zoned(const struct btrfs_fs_info *info,
if (!btrfs_is_zoned(info))
return 0;
- /*
- * Space cache writing is not COWed. Disable that to avoid write errors
- * in sequential zones.
- */
- if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE)) {
- btrfs_err(info, "zoned: space cache v1 is not supported");
- return -EINVAL;
- }
-
if (btrfs_raw_test_opt(*mount_opt, NODATACOW)) {
btrfs_err(info, "zoned: NODATACOW not supported");
return -EINVAL;
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 10/17] btrfs: replace btrfs_set_free_space_cache_v1_active() with a cleanup helper
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (8 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 09/17] btrfs: remove the SPACE_CACHE mount option flag Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 11/17] btrfs: remove the free space cache trimming ranges Tal Zussman
` (6 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
The only caller passes active = false. Turn it into
btrfs_cleanup_free_space_cache_v1() and fold the block group loop into
it.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/disk-io.c | 2 +-
fs/btrfs/free-space-cache.c | 42 ++++++++++++------------------------------
fs/btrfs/free-space-cache.h | 2 +-
3 files changed, 14 insertions(+), 32 deletions(-)
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 2ac0e7c2f1e4..323fbbea48e5 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -3149,7 +3149,7 @@ int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info)
}
if (btrfs_free_space_cache_v1_active(fs_info)) {
- ret = btrfs_set_free_space_cache_v1_active(fs_info, false);
+ ret = btrfs_cleanup_free_space_cache_v1(fs_info);
if (ret)
return ret;
}
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index 3ba9ed4a39d0..fb6ff3db1241 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -2891,47 +2891,29 @@ bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info)
return btrfs_super_cache_generation(fs_info->super_copy);
}
-static int cleanup_free_space_cache_v1(struct btrfs_fs_info *fs_info,
- struct btrfs_trans_handle *trans)
+int btrfs_cleanup_free_space_cache_v1(struct btrfs_fs_info *fs_info)
{
- struct btrfs_block_group *block_group;
+ struct btrfs_trans_handle *trans;
struct rb_node *node;
+ int ret;
btrfs_info(fs_info, "cleaning free space cache v1");
- node = rb_first_cached(&fs_info->block_group_cache_tree);
- while (node) {
- int ret;
-
- block_group = rb_entry(node, struct btrfs_block_group, cache_node);
- ret = btrfs_remove_free_space_inode(trans, NULL, block_group);
- if (ret)
- return ret;
- node = rb_next(node);
- }
- return 0;
-}
-
-int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active)
-{
- struct btrfs_trans_handle *trans;
- int ret;
-
/*
- * update_super_roots will appropriately set or unset
- * super_copy->cache_generation based on SPACE_CACHE and
- * BTRFS_FS_CLEANUP_SPACE_CACHE_V1. For this reason, we need a
- * transaction commit whether we are enabling space cache v1 and don't
- * have any other work to do, or are disabling it and removing free
- * space inodes.
+ * update_super_roots() zeroes super_copy->cache_generation while
+ * BTRFS_FS_CLEANUP_SPACE_CACHE_V1 is set, so this needs a commit.
*/
trans = btrfs_start_transaction(fs_info->tree_root, 0);
if (IS_ERR(trans))
return PTR_ERR(trans);
- if (!active) {
- set_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags);
- ret = cleanup_free_space_cache_v1(fs_info, trans);
+ set_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags);
+ for (node = rb_first_cached(&fs_info->block_group_cache_tree); node;
+ node = rb_next(node)) {
+ struct btrfs_block_group *block_group;
+
+ block_group = rb_entry(node, struct btrfs_block_group, cache_node);
+ ret = btrfs_remove_free_space_inode(trans, NULL, block_group);
if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
btrfs_end_transaction(trans);
diff --git a/fs/btrfs/free-space-cache.h b/fs/btrfs/free-space-cache.h
index 29166cc09b90..f5f18e397b13 100644
--- a/fs/btrfs/free-space-cache.h
+++ b/fs/btrfs/free-space-cache.h
@@ -136,7 +136,7 @@ int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
void btrfs_trim_fully_remapped_block_group(struct btrfs_block_group *bg);
bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info);
-int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active);
+int btrfs_cleanup_free_space_cache_v1(struct btrfs_fs_info *fs_info);
/* Support functions for running our sanity tests */
#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
bool btrfs_use_bitmap(struct btrfs_free_space_ctl *ctl,
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 11/17] btrfs: remove the free space cache trimming ranges
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (9 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 10/17] btrfs: replace btrfs_set_free_space_cache_v1_active() with a cleanup helper Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 12/17] btrfs: remove BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE Tal Zussman
` (5 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
cache_writeout_mutex and trimming_ranges let the v1 cache writer see
ranges that were unlinked from the free space tree while being
discarded. Nothing consumes the list anymore, and the tree itself is
protected by tree_lock, so remove them.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/free-space-cache.c | 41 +++--------------------------------------
fs/btrfs/free-space-cache.h | 2 --
2 files changed, 3 insertions(+), 40 deletions(-)
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index fb6ff3db1241..2a40167c3fb6 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -36,12 +36,6 @@
static struct kmem_cache *btrfs_free_space_cachep;
static struct kmem_cache *btrfs_free_space_bitmap_cachep;
-struct btrfs_trim_range {
- u64 start;
- u64 bytes;
- struct list_head list;
-};
-
static int link_free_space(struct btrfs_free_space_ctl *ctl,
struct btrfs_free_space *info);
static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
@@ -1692,8 +1686,6 @@ void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group,
spin_lock_init(&ctl->tree_lock);
ctl->block_group = block_group;
ctl->free_space_bytes = RB_ROOT_CACHED;
- INIT_LIST_HEAD(&ctl->trimming_ranges);
- mutex_init(&ctl->cache_writeout_mutex);
/*
* we only want to have 32k of ram per block group for keeping
@@ -2389,12 +2381,10 @@ void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
static int do_trimming(struct btrfs_block_group *block_group,
u64 *total_trimmed, u64 start, u64 bytes,
u64 reserved_start, u64 reserved_bytes,
- enum btrfs_trim_state reserved_trim_state,
- struct btrfs_trim_range *trim_entry)
+ enum btrfs_trim_state reserved_trim_state)
{
struct btrfs_space_info *space_info = block_group->space_info;
struct btrfs_fs_info *fs_info = block_group->fs_info;
- struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
int ret;
bool bg_ro;
const u64 end = start + bytes;
@@ -2420,7 +2410,6 @@ static int do_trimming(struct btrfs_block_group *block_group,
trim_state = BTRFS_TRIM_STATE_TRIMMED;
}
- mutex_lock(&ctl->cache_writeout_mutex);
if (reserved_start < start)
__btrfs_add_free_space(block_group, reserved_start,
start - reserved_start,
@@ -2429,8 +2418,6 @@ static int do_trimming(struct btrfs_block_group *block_group,
__btrfs_add_free_space(block_group, end, reserved_end - end,
reserved_trim_state);
__btrfs_add_free_space(block_group, start, bytes, trim_state);
- list_del(&trim_entry->list);
- mutex_unlock(&ctl->cache_writeout_mutex);
if (!bg_ro) {
spin_lock(&space_info->lock);
@@ -2468,9 +2455,6 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group,
const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
while (start < end) {
- struct btrfs_trim_range trim_entry;
-
- mutex_lock(&ctl->cache_writeout_mutex);
spin_lock(&ctl->tree_lock);
if (ctl->free_space < minlen)
@@ -2501,7 +2485,6 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group,
bytes = entry->bytes;
if (bytes < minlen) {
spin_unlock(&ctl->tree_lock);
- mutex_unlock(&ctl->cache_writeout_mutex);
goto next;
}
unlink_free_space(ctl, entry, true);
@@ -2526,7 +2509,6 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group,
bytes = min(extent_start + extent_bytes, end) - start;
if (bytes < minlen) {
spin_unlock(&ctl->tree_lock);
- mutex_unlock(&ctl->cache_writeout_mutex);
goto next;
}
@@ -2535,14 +2517,9 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group,
}
spin_unlock(&ctl->tree_lock);
- trim_entry.start = extent_start;
- trim_entry.bytes = extent_bytes;
- list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
- mutex_unlock(&ctl->cache_writeout_mutex);
ret = do_trimming(block_group, total_trimmed, start, bytes,
- extent_start, extent_bytes, extent_trim_state,
- &trim_entry);
+ extent_start, extent_bytes, extent_trim_state);
if (ret) {
block_group->discard_cursor = start + bytes;
break;
@@ -2566,7 +2543,6 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group,
out_unlock:
block_group->discard_cursor = btrfs_block_group_end(block_group);
spin_unlock(&ctl->tree_lock);
- mutex_unlock(&ctl->cache_writeout_mutex);
return ret;
}
@@ -2677,16 +2653,13 @@ static int trim_bitmaps(struct btrfs_block_group *block_group,
while (offset < end) {
bool next_bitmap = false;
- struct btrfs_trim_range trim_entry;
- mutex_lock(&ctl->cache_writeout_mutex);
spin_lock(&ctl->tree_lock);
if (ctl->free_space < minlen) {
block_group->discard_cursor =
btrfs_block_group_end(block_group);
spin_unlock(&ctl->tree_lock);
- mutex_unlock(&ctl->cache_writeout_mutex);
break;
}
@@ -2702,7 +2675,6 @@ static int trim_bitmaps(struct btrfs_block_group *block_group,
if (!entry || (async && minlen && start == offset &&
btrfs_free_space_trimmed(entry))) {
spin_unlock(&ctl->tree_lock);
- mutex_unlock(&ctl->cache_writeout_mutex);
next_bitmap = true;
goto next;
}
@@ -2728,7 +2700,6 @@ static int trim_bitmaps(struct btrfs_block_group *block_group,
else
entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
spin_unlock(&ctl->tree_lock);
- mutex_unlock(&ctl->cache_writeout_mutex);
next_bitmap = true;
goto next;
}
@@ -2739,14 +2710,12 @@ static int trim_bitmaps(struct btrfs_block_group *block_group,
*/
if (async && *total_trimmed) {
spin_unlock(&ctl->tree_lock);
- mutex_unlock(&ctl->cache_writeout_mutex);
return ret;
}
bytes = min(bytes, end - start);
if (bytes < minlen || (async && maxlen && bytes > maxlen)) {
spin_unlock(&ctl->tree_lock);
- mutex_unlock(&ctl->cache_writeout_mutex);
goto next;
}
@@ -2766,13 +2735,9 @@ static int trim_bitmaps(struct btrfs_block_group *block_group,
free_bitmap(ctl, entry);
spin_unlock(&ctl->tree_lock);
- trim_entry.start = start;
- trim_entry.bytes = bytes;
- list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
- mutex_unlock(&ctl->cache_writeout_mutex);
ret = do_trimming(block_group, total_trimmed, start, bytes,
- start, bytes, 0, &trim_entry);
+ start, bytes, 0);
if (ret) {
reset_trimming_bitmap(ctl, offset);
block_group->discard_cursor =
diff --git a/fs/btrfs/free-space-cache.h b/fs/btrfs/free-space-cache.h
index f5f18e397b13..e22443598b8e 100644
--- a/fs/btrfs/free-space-cache.h
+++ b/fs/btrfs/free-space-cache.h
@@ -83,8 +83,6 @@ struct btrfs_free_space_ctl {
s32 discardable_extents[BTRFS_STAT_NR_ENTRIES];
s64 discardable_bytes[BTRFS_STAT_NR_ENTRIES];
struct btrfs_block_group *block_group;
- struct mutex cache_writeout_mutex;
- struct list_head trimming_ranges;
};
int __init btrfs_free_space_init(void);
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 12/17] btrfs: remove BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (10 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 11/17] btrfs: remove the free space cache trimming ranges Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 13/17] btrfs: remove the free space inode ordered extent special cases Tal Zussman
` (4 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
Free space inodes no longer reserve data or delalloc space, as nothing
writes to them. Remove the flush mode and the special cases that
selected it.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/delalloc-space.c | 13 ++-----------
fs/btrfs/space-info.c | 2 --
fs/btrfs/space-info.h | 4 ----
3 files changed, 2 insertions(+), 17 deletions(-)
diff --git a/fs/btrfs/delalloc-space.c b/fs/btrfs/delalloc-space.c
index d357ed7efd99..77781852e417 100644
--- a/fs/btrfs/delalloc-space.c
+++ b/fs/btrfs/delalloc-space.c
@@ -132,9 +132,7 @@ int btrfs_alloc_data_chunk_ondemand(const struct btrfs_inode *inode, u64 bytes)
/* Make sure bytes are sectorsize aligned */
bytes = ALIGN(bytes, fs_info->sectorsize);
- if (btrfs_is_free_space_inode(inode))
- flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
- else if (btrfs_is_zoned(fs_info) && btrfs_is_data_reloc_root(root))
+ if (btrfs_is_zoned(fs_info) && btrfs_is_data_reloc_root(root))
flush = BTRFS_RESERVE_FLUSH_ZONED_RELOCATION;
return btrfs_reserve_data_bytes(data_sinfo_for_inode(inode), bytes, flush);
@@ -155,8 +153,6 @@ int btrfs_check_data_free_space(struct btrfs_inode *inode,
if (noflush)
flush = BTRFS_RESERVE_NO_FLUSH;
- else if (btrfs_is_free_space_inode(inode))
- flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
ret = btrfs_reserve_data_bytes(data_sinfo_for_inode(inode), len, flush);
if (ret < 0)
@@ -326,15 +322,10 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
int ret = 0;
/*
- * If we are a free space inode we need to not flush since we will be in
- * the middle of a transaction commit. We also don't need the delalloc
- * mutex since we won't race with anybody. We need this mostly to make
- * lockdep shut its filthy mouth.
- *
* If we have a transaction open (can happen if we call truncate_block
* from truncate), then we need FLUSH_LIMIT so we don't deadlock.
*/
- if (noflush || btrfs_is_free_space_inode(inode)) {
+ if (noflush) {
flush = BTRFS_RESERVE_NO_FLUSH;
} else {
if (current->journal_info)
diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c
index 39a28e1bec8a..01018152c054 100644
--- a/fs/btrfs/space-info.c
+++ b/fs/btrfs/space-info.c
@@ -1704,7 +1704,6 @@ static int handle_reserve_ticket(struct btrfs_space_info *space_info,
evict_flush_states,
ARRAY_SIZE(evict_flush_states));
break;
- case BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE:
case BTRFS_RESERVE_FLUSH_ZONED_RELOCATION:
priority_reclaim_data_space(space_info, ticket);
break;
@@ -1968,7 +1967,6 @@ int btrfs_reserve_data_bytes(struct btrfs_space_info *space_info, u64 bytes,
int ret;
ASSERT(flush == BTRFS_RESERVE_FLUSH_DATA ||
- flush == BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE ||
flush == BTRFS_RESERVE_FLUSH_ZONED_RELOCATION ||
flush == BTRFS_RESERVE_NO_FLUSH, "flush=%d", flush);
ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_DATA,
diff --git a/fs/btrfs/space-info.h b/fs/btrfs/space-info.h
index aa836e8a9d4a..d0130c8ba3dd 100644
--- a/fs/btrfs/space-info.h
+++ b/fs/btrfs/space-info.h
@@ -66,7 +66,6 @@ enum btrfs_reserve_flush_enum {
* Can be interrupted by a fatal signal.
*/
BTRFS_RESERVE_FLUSH_DATA,
- BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE,
BTRFS_RESERVE_FLUSH_ALL,
/*
@@ -82,9 +81,6 @@ enum btrfs_reserve_flush_enum {
* priority flushing for this, because otherwise we can deadlock on
* waiting for a ticket, that cannot be granted, because we cannot do
* any allocations.
- *
- * Apart from being specific to zoned relocation, it is equal to
- * BTRFS_FLUSH_FREE_SPACE_INODE.
*/
BTRFS_RESERVE_FLUSH_ZONED_RELOCATION,
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 13/17] btrfs: remove the free space inode ordered extent special cases
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (11 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 12/17] btrfs: remove BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 14/17] btrfs: remove the free space inode special cases from the COW paths Tal Zussman
` (3 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
Free space inodes never have ordered extents anymore. Drop the lockdep
exceptions for them and btrfs_join_transaction_spacecache(), which was
only used to finish their ordered extents during a commit.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/inode.c | 20 +++-----------------
fs/btrfs/ordered-data.c | 20 ++------------------
fs/btrfs/transaction.c | 6 ------
fs/btrfs/transaction.h | 1 -
4 files changed, 5 insertions(+), 42 deletions(-)
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 0539f27776e1..dec1a848c7ca 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -3203,7 +3203,6 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
int compress_type = 0;
int ret = 0;
u64 logical_len = ordered_extent->num_bytes;
- bool freespace_inode;
bool truncated = false;
bool clear_reserved_extent = true;
unsigned int clear_bits = 0;
@@ -3220,9 +3219,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
clear_bits |= EXTENT_DEFRAG;
- freespace_inode = btrfs_is_free_space_inode(inode);
- if (!freespace_inode)
- btrfs_lockdep_acquire(fs_info, btrfs_ordered_extent);
+ btrfs_lockdep_acquire(fs_info, btrfs_ordered_extent);
if (unlikely(test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags))) {
ret = -EIO;
@@ -3257,10 +3254,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
&cached_state);
}
- if (freespace_inode)
- trans = btrfs_join_transaction_spacecache(root);
- else
- trans = btrfs_join_transaction(root);
+ trans = btrfs_join_transaction(root);
if (IS_ERR(trans)) {
ret = PTR_ERR(trans);
trans = NULL;
@@ -8131,7 +8125,6 @@ void btrfs_destroy_inode(struct inode *vfs_inode)
struct btrfs_ordered_extent *ordered;
struct btrfs_inode *inode = BTRFS_I(vfs_inode);
struct btrfs_root *root = inode->root;
- bool freespace_inode;
WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
WARN_ON(vfs_inode->i_data.nrpages);
@@ -8154,12 +8147,6 @@ void btrfs_destroy_inode(struct inode *vfs_inode)
if (!root)
return;
- /*
- * If this is a free space inode do not take the ordered extents lockdep
- * map.
- */
- freespace_inode = btrfs_is_free_space_inode(inode);
-
while (1) {
ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
if (!ordered)
@@ -8169,8 +8156,7 @@ void btrfs_destroy_inode(struct inode *vfs_inode)
"found ordered extent %llu %llu on inode cleanup",
ordered->file_offset, ordered->num_bytes);
- if (!freespace_inode)
- btrfs_lockdep_acquire(root->fs_info, btrfs_ordered_extent);
+ btrfs_lockdep_acquire(root->fs_info, btrfs_ordered_extent);
btrfs_remove_ordered_extent(ordered);
btrfs_put_ordered_extent(ordered);
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index e9f1cbeb555a..df74c75d6c29 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -654,13 +654,6 @@ void btrfs_remove_ordered_extent(struct btrfs_ordered_extent *entry)
struct btrfs_fs_info *fs_info = root->fs_info;
struct rb_node *node;
bool pending;
- bool freespace_inode;
-
- /*
- * If this is a free space inode the thread has not acquired the ordered
- * extents lockdep map.
- */
- freespace_inode = btrfs_is_free_space_inode(btrfs_inode);
btrfs_lockdep_acquire(fs_info, btrfs_trans_pending_ordered);
/* This is paired with alloc_ordered_extent(). */
@@ -735,8 +728,7 @@ void btrfs_remove_ordered_extent(struct btrfs_ordered_extent *entry)
}
spin_unlock(&root->ordered_extent_lock);
wake_up(&entry->wait);
- if (!freespace_inode)
- btrfs_lockdep_release(fs_info, btrfs_ordered_extent);
+ btrfs_lockdep_release(fs_info, btrfs_ordered_extent);
}
static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
@@ -867,16 +859,9 @@ void btrfs_start_ordered_extent_nowriteback(struct btrfs_ordered_extent *entry,
u64 start = entry->file_offset;
u64 end = start + entry->num_bytes - 1;
struct btrfs_inode *inode = entry->inode;
- bool freespace_inode;
trace_btrfs_ordered_extent_start(inode, entry);
- /*
- * If this is a free space inode do not take the ordered extents lockdep
- * map.
- */
- freespace_inode = btrfs_is_free_space_inode(inode);
-
/*
* pages in the range can be dirty, clean or writeback. We
* start IO on any dirty ones so the wait doesn't stall waiting
@@ -896,8 +881,7 @@ void btrfs_start_ordered_extent_nowriteback(struct btrfs_ordered_extent *entry,
}
}
- if (!freespace_inode)
- btrfs_might_wait_for_event(inode->root->fs_info, btrfs_ordered_extent);
+ btrfs_might_wait_for_event(inode->root->fs_info, btrfs_ordered_extent);
wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE, &entry->flags));
}
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 0ea984d641e0..fb026d81414b 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -854,12 +854,6 @@ struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
true);
}
-struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root)
-{
- return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
- BTRFS_RESERVE_NO_FLUSH, true);
-}
-
/*
* Similar to regular join but it never starts a transaction when none is
* running or when there's a running one at a state >= TRANS_STATE_UNBLOCKED.
diff --git a/fs/btrfs/transaction.h b/fs/btrfs/transaction.h
index 33a1884c5765..6b0da4eaa0fa 100644
--- a/fs/btrfs/transaction.h
+++ b/fs/btrfs/transaction.h
@@ -294,7 +294,6 @@ struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
struct btrfs_root *root,
unsigned int num_items);
struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
-struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root);
struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);
struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 14/17] btrfs: remove the free space inode special cases from the COW paths
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (12 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 13/17] btrfs: remove the free space inode ordered extent special cases Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 15/17] btrfs: stop special-casing free space inodes in the delalloc accounting Tal Zussman
` (2 subsequent siblings)
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
Free space inodes are never written anymore, so drop the special cases
for them in cow_file_range(), fallback_to_cow(),
can_nocow_file_extent() and btrfs_finish_one_ordered().
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/inode.c | 58 +++++++++++++++-----------------------------------------
1 file changed, 15 insertions(+), 43 deletions(-)
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index dec1a848c7ca..c8292174b550 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -1366,11 +1366,6 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
goto out_unlock;
}
- if (btrfs_is_free_space_inode(inode)) {
- ret = -EINVAL;
- goto out_unlock;
- }
-
num_bytes = ALIGN(end - start + 1, blocksize);
num_bytes = max(blocksize, num_bytes);
ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
@@ -1680,7 +1675,6 @@ static int fallback_to_cow(struct btrfs_inode *inode,
struct folio *locked_folio, const u64 start,
const u64 end)
{
- const bool is_space_ino = btrfs_is_free_space_inode(inode);
const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root);
const u64 range_bytes = end + 1 - start;
struct extent_io_tree *io_tree = &inode->io_tree;
@@ -1713,23 +1707,22 @@ static int fallback_to_cow(struct btrfs_inode *inode,
* extent_clear_unlock_delalloc()) the bytes_may_use counter of the
* data space info, which we incremented in the step above.
*
- * If we need to fallback to cow and the inode corresponds to a free
- * space cache inode or an inode of the data relocation tree, we must
- * also increment bytes_may_use of the data space_info for the same
- * reason. Space caches and relocated data extents always get a prealloc
- * extent for them, however scrub or balance may have set the block
- * group that contains that extent to RO mode and therefore force COW
- * when starting writeback.
+ * If we need to fallback to cow and the inode is in the data relocation
+ * tree, we must also increment bytes_may_use of the data space_info for
+ * the same reason. Relocated data extents always get a prealloc extent,
+ * however scrub or balance may have set the block group that contains
+ * that extent to RO mode and therefore force COW when starting
+ * writeback.
*/
btrfs_lock_extent(io_tree, start, end, &cached_state);
count = btrfs_count_range_bits(io_tree, &range_start, end, range_bytes,
EXTENT_NORESERVE, false, NULL);
- if (count > 0 || is_space_ino || is_reloc_ino) {
+ if (count > 0 || is_reloc_ino) {
u64 bytes = count;
struct btrfs_fs_info *fs_info = inode->root->fs_info;
struct btrfs_space_info *sinfo = fs_info->data_sinfo;
- if (is_space_ino || is_reloc_ino)
+ if (is_reloc_ino)
bytes = range_bytes;
spin_lock(&sinfo->lock);
@@ -1794,7 +1787,6 @@ static int can_nocow_file_extent(struct btrfs_path *path,
struct btrfs_inode *inode,
struct can_nocow_file_extent_args *args)
{
- const bool is_freespace_inode = btrfs_is_free_space_inode(inode);
struct extent_buffer *leaf = path->nodes[0];
struct btrfs_root *root = inode->root;
struct btrfs_file_extent_item *fi;
@@ -1807,8 +1799,7 @@ static int can_nocow_file_extent(struct btrfs_path *path,
bool nowait = path->nowait;
/* If there are pending snapshots for this root, we must do COW. */
- if (args->writeback_path && !is_freespace_inode &&
- atomic_read(&root->snapshot_force_cow))
+ if (args->writeback_path && atomic_read(&root->snapshot_force_cow))
goto out;
fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
@@ -1857,7 +1848,6 @@ static int can_nocow_file_extent(struct btrfs_path *path,
ret = btrfs_cross_ref_exist(inode, key->offset - args->file_extent.offset,
args->file_extent.disk_bytenr, path);
- WARN_ON_ONCE(ret > 0 && is_freespace_inode);
if (ret != 0)
goto out;
@@ -1892,7 +1882,6 @@ static int can_nocow_file_extent(struct btrfs_path *path,
ret = btrfs_lookup_csums_list(csum_root, io_start,
io_start + args->file_extent.num_bytes - 1,
NULL, nowait);
- WARN_ON_ONCE(ret > 0 && is_freespace_inode);
if (ret != 0)
goto out;
@@ -3203,6 +3192,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
int compress_type = 0;
int ret = 0;
u64 logical_len = ordered_extent->num_bytes;
+ u64 unwritten_start;
bool truncated = false;
bool clear_reserved_extent = true;
unsigned int clear_bits = 0;
@@ -3364,29 +3354,11 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
if (ret)
btrfs_mark_ordered_extent_error(ordered_extent);
- /*
- * Drop extent maps for the part of the extent we didn't write.
- *
- * We have an exception here for the free_space_inode, this is
- * because when we do btrfs_get_extent() on the free space inode
- * we will search the commit root. If this is a new block group
- * we won't find anything, and we will trip over the assert in
- * writepage where we do ASSERT(em->block_start !=
- * EXTENT_MAP_HOLE).
- *
- * Theoretically we could also skip this for any NOCOW extent as
- * we don't mess with the extent map tree in the NOCOW case, but
- * for now simply skip this if we are the free space inode.
- */
- if (!btrfs_is_free_space_inode(inode)) {
- u64 unwritten_start = start;
-
- if (truncated)
- unwritten_start += logical_len;
-
- btrfs_drop_extent_map_range(inode, unwritten_start,
- end, false);
- }
+ /* Drop extent maps for the part of the extent we didn't write. */
+ unwritten_start = start;
+ if (truncated)
+ unwritten_start += logical_len;
+ btrfs_drop_extent_map_range(inode, unwritten_start, end, false);
/*
* If the ordered extent had an IOERR or something else went
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 15/17] btrfs: stop special-casing free space inodes in the delalloc accounting
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (13 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 14/17] btrfs: remove the free space inode special cases from the COW paths Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 16/17] btrfs: stop reading free space inodes from the commit root Tal Zussman
2026-09-17 4:00 ` [PATCH v3 17/17] btrfs: remove TRANS_JOIN_NOLOCK Tal Zussman
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
Free space inodes never have delalloc or outstanding extents any more,
so they don't need to be kept off the root's delalloc inode list or out
of the outstanding extents tracepoint.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/btrfs_inode.h | 2 --
fs/btrfs/inode.c | 5 ++---
2 files changed, 2 insertions(+), 5 deletions(-)
diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index 26cc5d1aca4c..795bb222a2d0 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -402,8 +402,6 @@ static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode,
{
lockdep_assert_held(&inode->lock);
inode->outstanding_extents += mod;
- if (btrfs_is_free_space_inode(inode))
- return;
trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode),
mod, inode->outstanding_extents);
}
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index c8292174b550..89b50d4e0400 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -2614,7 +2614,7 @@ void btrfs_set_delalloc_extent(struct btrfs_inode *inode, struct extent_state *s
* and are therefore protected against concurrent calls of this
* function and btrfs_clear_delalloc_extent().
*/
- if (!btrfs_is_free_space_inode(inode) && prev_delalloc_bytes == 0)
+ if (prev_delalloc_bytes == 0)
btrfs_add_delalloc_inode(inode);
}
@@ -2672,7 +2672,6 @@ void btrfs_clear_delalloc_extent(struct btrfs_inode *inode,
return;
if (!btrfs_is_data_reloc_root(root) &&
- !btrfs_is_free_space_inode(inode) &&
!(state->state & EXTENT_NORESERVE) &&
(bits & EXTENT_CLEAR_DATA_RESV))
btrfs_free_reserved_data_space_noquota(inode, len);
@@ -2690,7 +2689,7 @@ void btrfs_clear_delalloc_extent(struct btrfs_inode *inode,
* and are therefore protected against concurrent calls of this
* function and btrfs_set_delalloc_extent().
*/
- if (!btrfs_is_free_space_inode(inode) && new_delalloc_bytes == 0) {
+ if (new_delalloc_bytes == 0) {
spin_lock(&root->delalloc_lock);
btrfs_del_delalloc_inode(inode);
spin_unlock(&root->delalloc_lock);
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 16/17] btrfs: stop reading free space inodes from the commit root
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (14 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 15/17] btrfs: stop special-casing free space inodes in the delalloc accounting Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
2026-09-17 4:00 ` [PATCH v3 17/17] btrfs: remove TRANS_JOIN_NOLOCK Tal Zussman
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
Free space inode data was only read when loading the v1 cache, which is
gone, so btrfs_get_extent() and btrfs_lookup_bio_sums() no longer need
to search the commit root for them.
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/file-item.c | 11 -----------
fs/btrfs/inode.c | 10 ----------
2 files changed, 21 deletions(-)
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index cf50fd623f41..484c04de1af5 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -396,17 +396,6 @@ int btrfs_lookup_bio_sums(struct btrfs_bio *bbio)
if (nblocks > fs_info->csums_per_leaf)
path->reada = READA_FORWARD;
- /*
- * the free space stuff is only read when it hasn't been
- * updated in the current transaction. So, we can safely
- * read from the commit root and sidestep a nasty deadlock
- * between reading the free space cache and updating the csum tree.
- */
- if (btrfs_is_free_space_inode(inode)) {
- path->search_commit_root = true;
- path->skip_locking = true;
- }
-
/*
* If we are searching for a csum of an extent from a past
* transaction, we can search in the commit root and reduce
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 89b50d4e0400..163f26fe36e7 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -7230,16 +7230,6 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
/* Chances are we'll be called again, so go ahead and do readahead */
path->reada = READA_FORWARD;
- /*
- * The same explanation in load_free_space_cache applies here as well,
- * we only read when we're loading the free space cache, and at that
- * point the commit_root has everything we need.
- */
- if (btrfs_is_free_space_inode(inode)) {
- path->search_commit_root = true;
- path->skip_locking = true;
- }
-
ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
if (ret < 0) {
goto out;
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v3 17/17] btrfs: remove TRANS_JOIN_NOLOCK
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
` (15 preceding siblings ...)
2026-09-17 4:00 ` [PATCH v3 16/17] btrfs: stop reading free space inodes from the commit root Tal Zussman
@ 2026-09-17 4:00 ` Tal Zussman
16 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-17 4:00 UTC (permalink / raw)
To: David Sterba, Chris Mason, Qu Wenruo
Cc: linux-btrfs, linux-kernel, Tal Zussman
btrfs_join_transaction_spacecache() was the only user of
TRANS_JOIN_NOLOCK and is gone, so remove the join type, its entries in
the blocked types table, and the special cases in join_transaction() and
start_transaction().
Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/btrfs/transaction.c | 17 +----------------
fs/btrfs/transaction.h | 2 --
2 files changed, 1 insertion(+), 18 deletions(-)
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index fb026d81414b..e96dd99ce623 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -125,17 +125,14 @@ static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
[TRANS_STATE_UNBLOCKED] = (__TRANS_START |
__TRANS_ATTACH |
__TRANS_JOIN |
- __TRANS_JOIN_NOLOCK |
__TRANS_JOIN_NOSTART),
[TRANS_STATE_SUPER_COMMITTED] = (__TRANS_START |
__TRANS_ATTACH |
__TRANS_JOIN |
- __TRANS_JOIN_NOLOCK |
__TRANS_JOIN_NOSTART),
[TRANS_STATE_COMPLETED] = (__TRANS_START |
__TRANS_ATTACH |
__TRANS_JOIN |
- __TRANS_JOIN_NOLOCK |
__TRANS_JOIN_NOSTART),
};
@@ -310,12 +307,6 @@ static noinline int join_transaction(struct btrfs_fs_info *fs_info,
if (type == TRANS_ATTACH || type == TRANS_JOIN_NOSTART)
return -ENOENT;
- /*
- * JOIN_NOLOCK only happens during the transaction commit, so
- * it is impossible that ->running_transaction is NULL
- */
- BUG_ON(type == TRANS_JOIN_NOLOCK);
-
cur_trans = kmalloc_obj(*cur_trans, GFP_NOFS);
if (!cur_trans)
return -ENOMEM;
@@ -709,14 +700,8 @@ start_transaction(struct btrfs_root *root, unsigned int num_items,
}
/*
- * If we are JOIN_NOLOCK we're already committing a transaction and
- * waiting on this guy, so we don't need to do the sb_start_intwrite
- * because we're already holding a ref. We need this because we could
- * have raced in and did an fsync() on a file which can kick a commit
- * and then we deadlock with somebody doing a freeze.
- *
* If we are ATTACH, it means we just want to catch the current
- * transaction and commit it, so we needn't do sb_start_intwrite().
+ * transaction and commit it, so we needn't do sb_start_intwrite().
*/
if (type & __TRANS_FREEZABLE)
sb_start_intwrite(fs_info->sb);
diff --git a/fs/btrfs/transaction.h b/fs/btrfs/transaction.h
index 6b0da4eaa0fa..556c094eadb9 100644
--- a/fs/btrfs/transaction.h
+++ b/fs/btrfs/transaction.h
@@ -106,7 +106,6 @@ enum {
ENUM_BIT(__TRANS_START),
ENUM_BIT(__TRANS_ATTACH),
ENUM_BIT(__TRANS_JOIN),
- ENUM_BIT(__TRANS_JOIN_NOLOCK),
ENUM_BIT(__TRANS_DUMMY),
ENUM_BIT(__TRANS_JOIN_NOSTART),
};
@@ -114,7 +113,6 @@ enum {
#define TRANS_START (__TRANS_START | __TRANS_FREEZABLE)
#define TRANS_ATTACH (__TRANS_ATTACH)
#define TRANS_JOIN (__TRANS_JOIN | __TRANS_FREEZABLE)
-#define TRANS_JOIN_NOLOCK (__TRANS_JOIN_NOLOCK)
#define TRANS_JOIN_NOSTART (__TRANS_JOIN_NOSTART)
#define TRANS_EXTWRITERS (__TRANS_START | __TRANS_ATTACH)
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread
* Re: [PATCH v3 01/17] btrfs: stop enabling the v1 space cache from the on-disk state
[not found] ` <20260917041202.74A691F00893@smtp.kernel.org>
@ 2026-09-18 9:57 ` David Sterba
2026-09-22 1:43 ` Tal Zussman
0 siblings, 1 reply; 20+ messages in thread
From: David Sterba @ 2026-09-18 9:57 UTC (permalink / raw)
To: Tal Zussman; +Cc: linux-btrfs
On Thu, Sep 17, 2026 at 04:12:02AM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [Medium] btrfs: FREE_SPACE_TREE option leaks on invalid RO remounts
> --
>
> commit 6751f883b4d1cad106e74cd1b62922302a1c3544
> Author: Tal Zussman <tz2294@columbia.edu>
>
> btrfs: stop enabling the v1 space cache from the on-disk state
>
> This commit prevents the v1 space cache from being automatically enabled
> based on the on-disk state. Remounts of read-only filesystems with an old
> cache no longer start a transaction, and space cache cleanup happens
> unconditionally upon a read-write mount.
>
> > diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
> > index 464129b1b0d4c..77443ded6db39 100644
> > --- a/fs/btrfs/super.c
> > +++ b/fs/btrfs/super.c
> [ ... ]
> > @@ -1535,10 +1529,6 @@ static int btrfs_reconfigure(struct fs_context *fc)
> > btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
> > btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
> > }
> > - if (btrfs_free_space_cache_v1_active(fs_info)) {
> > - btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
> > - btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
> > - }
> > }
> >
> > ret = 0;
>
> [Severity: Medium]
> Does removing the v1 space cache check here allow the FREE_SPACE_TREE option
> to leak on invalid read-only remounts?
>
> If a user with a filesystem using the v1 space cache attempts a read-only
> remount with "space_cache=v2", the kernel emits a warning that this is only
> supported on a read-write remount and intends to reject it.
>
> However, because the v1_active check that previously reverted the
> FREE_SPACE_TREE option was removed without an unconditional fallback, the
> option appears to silently leak into fs_info->mount_opt in
> btrfs_reconfigure().
>
> If the user later performs a standard read-write remount, could the kernel
> see the leaked option and unexpectedly create the v2 free space tree?
This looks valid, though the consequences are not disatrous but still
unexpected to create FST.
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH v3 01/17] btrfs: stop enabling the v1 space cache from the on-disk state
2026-09-18 9:57 ` David Sterba
@ 2026-09-22 1:43 ` Tal Zussman
0 siblings, 0 replies; 20+ messages in thread
From: Tal Zussman @ 2026-09-22 1:43 UTC (permalink / raw)
To: dsterba; +Cc: linux-btrfs
On 9/18/26 5:57 AM, David Sterba wrote:
> On Thu, Sep 17, 2026 at 04:12:02AM +0000, sashiko-bot@kernel.org wrote:
>> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
>> - [Medium] btrfs: FREE_SPACE_TREE option leaks on invalid RO remounts
>> --
>>
>> commit 6751f883b4d1cad106e74cd1b62922302a1c3544
>> Author: Tal Zussman <tz2294@columbia.edu>
>>
>> btrfs: stop enabling the v1 space cache from the on-disk state
>>
>> This commit prevents the v1 space cache from being automatically enabled
>> based on the on-disk state. Remounts of read-only filesystems with an old
>> cache no longer start a transaction, and space cache cleanup happens
>> unconditionally upon a read-write mount.
>>
>> > diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
>> > index 464129b1b0d4c..77443ded6db39 100644
>> > --- a/fs/btrfs/super.c
>> > +++ b/fs/btrfs/super.c
>> [ ... ]
>> > @@ -1535,10 +1529,6 @@ static int btrfs_reconfigure(struct fs_context *fc)
>> > btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
>> > btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
>> > }
>> > - if (btrfs_free_space_cache_v1_active(fs_info)) {
>> > - btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
>> > - btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
>> > - }
>> > }
>> >
>> > ret = 0;
>>
>> [Severity: Medium]
>> Does removing the v1 space cache check here allow the FREE_SPACE_TREE option
>> to leak on invalid read-only remounts?
>>
>> If a user with a filesystem using the v1 space cache attempts a read-only
>> remount with "space_cache=v2", the kernel emits a warning that this is only
>> supported on a read-write remount and intends to reject it.
>>
>> However, because the v1_active check that previously reverted the
>> FREE_SPACE_TREE option was removed without an unconditional fallback, the
>> option appears to silently leak into fs_info->mount_opt in
>> btrfs_reconfigure().
>>
>> If the user later performs a standard read-write remount, could the kernel
>> see the leaked option and unexpectedly create the v2 free space tree?
>
> This looks valid, though the consequences are not disatrous but still
> unexpected to create FST.
>
So after looking at this some more, it seems like this "leak" already exists
in a different case.
If a remount asks for space_cache=v2 and isn't going ro->rw, we warn that
we can't change the free space tree, but the remount succeeds and the options
don't get reset. On a filesystem with neither a free space tree or v1 cache,
FREE_SPACE_TREE stays set and the next rw remount will create it.
I think the simplest solution is to add:
} else {
btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
}
as part of this patch, replacing the deleted btrfs_free_space_cache_v1_active()
check.
Should I send v4 with that change?
Thanks,
Tal
^ permalink raw reply [flat|nested] 20+ messages in thread
end of thread, other threads:[~2026-09-22 1:43 UTC | newest]
Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-17 3:59 [PATCH v3 00/17] btrfs: remove the v1 space cache Tal Zussman
2026-09-17 3:59 ` [PATCH v3 01/17] btrfs: stop enabling the v1 space cache from the on-disk state Tal Zussman
[not found] ` <20260917041202.74A691F00893@smtp.kernel.org>
2026-09-18 9:57 ` David Sterba
2026-09-22 1:43 ` Tal Zussman
2026-09-17 3:59 ` [PATCH v3 02/17] btrfs: remove the v1 space cache writeout from the transaction commit Tal Zussman
2026-09-17 3:59 ` [PATCH v3 03/17] btrfs: remove the free space cache endio workqueue Tal Zussman
2026-09-17 4:00 ` [PATCH v3 04/17] btrfs: remove the v1 space cache write path Tal Zussman
2026-09-17 4:00 ` [PATCH v3 05/17] btrfs: rename cache_write_mutex to dirty_bgs_update_mutex Tal Zussman
2026-09-17 4:00 ` [PATCH v3 06/17] btrfs: drop the transaction handle from the prealloc helpers Tal Zussman
2026-09-17 4:00 ` [PATCH v3 07/17] btrfs: remove the v1 space cache load path Tal Zussman
2026-09-17 4:00 ` [PATCH v3 08/17] btrfs: remove btrfs_disk_cache_state Tal Zussman
2026-09-17 4:00 ` [PATCH v3 09/17] btrfs: remove the SPACE_CACHE mount option flag Tal Zussman
2026-09-17 4:00 ` [PATCH v3 10/17] btrfs: replace btrfs_set_free_space_cache_v1_active() with a cleanup helper Tal Zussman
2026-09-17 4:00 ` [PATCH v3 11/17] btrfs: remove the free space cache trimming ranges Tal Zussman
2026-09-17 4:00 ` [PATCH v3 12/17] btrfs: remove BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE Tal Zussman
2026-09-17 4:00 ` [PATCH v3 13/17] btrfs: remove the free space inode ordered extent special cases Tal Zussman
2026-09-17 4:00 ` [PATCH v3 14/17] btrfs: remove the free space inode special cases from the COW paths Tal Zussman
2026-09-17 4:00 ` [PATCH v3 15/17] btrfs: stop special-casing free space inodes in the delalloc accounting Tal Zussman
2026-09-17 4:00 ` [PATCH v3 16/17] btrfs: stop reading free space inodes from the commit root Tal Zussman
2026-09-17 4:00 ` [PATCH v3 17/17] btrfs: remove TRANS_JOIN_NOLOCK Tal Zussman
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox