[PATCH 10/15] btrfs: remove the free space cache trimming ranges
From: Tal Zussman
Date: Mon Sep 07 2026 - 21:23:42 EST
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@xxxxxxxxxxxx>
---
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 a36d0465510d..8a6d896b4108 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,
@@ -1699,8 +1693,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
@@ -2396,12 +2388,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;
@@ -2427,7 +2417,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,
@@ -2436,8 +2425,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);
@@ -2475,9 +2462,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)
@@ -2508,7 +2492,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);
@@ -2533,7 +2516,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;
}
@@ -2542,14 +2524,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;
@@ -2573,7 +2550,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;
}
@@ -2684,16 +2660,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;
}
@@ -2709,7 +2682,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;
}
@@ -2735,7 +2707,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;
}
@@ -2746,14 +2717,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;
}
@@ -2773,13 +2742,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