Skip to content

Commit

Permalink
btrfs: factor out btrfs_return_free_space()
Browse files Browse the repository at this point in the history
Factor out a part of unpin_extent_range() that returns space back to the
space info, prioritizing global block reserve.  Also, move the "len"
variable into the loop to clarify we don't need to carry it beyond an
iteration.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
  • Loading branch information
naota authored and kdave committed Jan 13, 2025
1 parent bfcf6d0 commit 3704db1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
25 changes: 4 additions & 21 deletions fs/btrfs/extent-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2700,15 +2700,15 @@ static int unpin_extent_range(struct btrfs_fs_info *fs_info,
{
struct btrfs_block_group *cache = NULL;
struct btrfs_space_info *space_info;
struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
struct btrfs_free_cluster *cluster = NULL;
u64 len;
u64 total_unpinned = 0;
u64 empty_cluster = 0;
bool readonly;
int ret = 0;

while (start <= end) {
u64 len;

readonly = false;
if (!cache ||
start >= cache->start + cache->length) {
Expand Down Expand Up @@ -2766,25 +2766,8 @@ static int unpin_extent_range(struct btrfs_fs_info *fs_info,
readonly = true;
}
spin_unlock(&cache->lock);
if (!readonly && return_free_space &&
global_rsv->space_info == space_info) {
spin_lock(&global_rsv->lock);
if (!global_rsv->full) {
u64 to_add = min(len, global_rsv->size -
global_rsv->reserved);

global_rsv->reserved += to_add;
btrfs_space_info_update_bytes_may_use(fs_info,
space_info, to_add);
if (global_rsv->reserved >= global_rsv->size)
global_rsv->full = 1;
len -= to_add;
}
spin_unlock(&global_rsv->lock);
}
/* Add to any tickets we may have */
if (!readonly && return_free_space && len)
btrfs_try_granting_tickets(fs_info, space_info);
if (!readonly && return_free_space)
btrfs_return_free_space(space_info, len);
spin_unlock(&space_info->lock);
}

Expand Down
29 changes: 29 additions & 0 deletions fs/btrfs/space-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -2082,3 +2082,32 @@ void btrfs_reclaim_sweep(const struct btrfs_fs_info *fs_info)
do_reclaim_sweep(space_info, raid);
}
}

void btrfs_return_free_space(struct btrfs_space_info *space_info, u64 len)
{
struct btrfs_fs_info *fs_info = space_info->fs_info;
struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;

lockdep_assert_held(&space_info->lock);

/* Prioritize the global reservation to receive the freed space. */
if (global_rsv->space_info != space_info)
goto grant;

spin_lock(&global_rsv->lock);
if (!global_rsv->full) {
u64 to_add = min(len, global_rsv->size - global_rsv->reserved);

global_rsv->reserved += to_add;
btrfs_space_info_update_bytes_may_use(fs_info, space_info, to_add);
if (global_rsv->reserved >= global_rsv->size)
global_rsv->full = 1;
len -= to_add;
}
spin_unlock(&global_rsv->lock);

grant:
/* Add to any tickets we may have. */
if (len)
btrfs_try_granting_tickets(fs_info, space_info);
}
1 change: 1 addition & 0 deletions fs/btrfs/space-info.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,5 +295,6 @@ void btrfs_set_periodic_reclaim_ready(struct btrfs_space_info *space_info, bool
bool btrfs_should_periodic_reclaim(struct btrfs_space_info *space_info);
int btrfs_calc_reclaim_threshold(const struct btrfs_space_info *space_info);
void btrfs_reclaim_sweep(const struct btrfs_fs_info *fs_info);
void btrfs_return_free_space(struct btrfs_space_info *space_info, u64 len);

#endif /* BTRFS_SPACE_INFO_H */

0 comments on commit 3704db1

Please sign in to comment.