Commit 4d20c1de authored by Filipe Manana's avatar Filipe Manana Committed by David Sterba

btrfs: remove pointless loop from btrfs_update_block_group()

When an extent is allocated or freed, we call btrfs_update_block_group()
to update its block group and space info. An extent always belongs to a
single block group, it can never span multiple block groups, so the loop
we have at btrfs_update_block_group() is pointless, as it always has a
single iteration. The loop was added in the very early days, 2007, when
the block group code was added in commit 9078a3e1 ("Btrfs: start of
block group code"), but even back then it seemed pointless.

So remove the loop and assert the block group containing the start offset
of the extent also contains the whole extent.
Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 4ebe8d47
...@@ -3542,12 +3542,11 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, ...@@ -3542,12 +3542,11 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
u64 bytenr, u64 num_bytes, bool alloc) u64 bytenr, u64 num_bytes, bool alloc)
{ {
struct btrfs_fs_info *info = trans->fs_info; struct btrfs_fs_info *info = trans->fs_info;
struct btrfs_block_group *cache = NULL; struct btrfs_space_info *space_info;
u64 total = num_bytes; struct btrfs_block_group *cache;
u64 old_val; u64 old_val;
u64 byte_in_group; bool reclaim = false;
int factor; int factor;
int ret = 0;
/* Block accounting for super block */ /* Block accounting for super block */
spin_lock(&info->delalloc_root_lock); spin_lock(&info->delalloc_root_lock);
...@@ -3559,30 +3558,25 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, ...@@ -3559,30 +3558,25 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
btrfs_set_super_bytes_used(info->super_copy, old_val); btrfs_set_super_bytes_used(info->super_copy, old_val);
spin_unlock(&info->delalloc_root_lock); spin_unlock(&info->delalloc_root_lock);
while (total) {
struct btrfs_space_info *space_info;
bool reclaim = false;
cache = btrfs_lookup_block_group(info, bytenr); cache = btrfs_lookup_block_group(info, bytenr);
if (!cache) { if (!cache)
ret = -ENOENT; return -ENOENT;
break;
} /* An extent can not span multiple block groups. */
ASSERT(bytenr + num_bytes <= cache->start + cache->length);
space_info = cache->space_info; space_info = cache->space_info;
factor = btrfs_bg_type_to_factor(cache->flags); factor = btrfs_bg_type_to_factor(cache->flags);
/* /*
* If this block group has free space cache written out, we * If this block group has free space cache written out, we need to make
* need to make sure to load it if we are removing space. This * sure to load it if we are removing space. This is because we need
* is because we need the unpinning stage to actually add the * the unpinning stage to actually add the space back to the block group,
* space back to the block group, otherwise we will leak space. * otherwise we will leak space.
*/ */
if (!alloc && !btrfs_block_group_done(cache)) if (!alloc && !btrfs_block_group_done(cache))
btrfs_cache_block_group(cache, true); btrfs_cache_block_group(cache, true);
byte_in_group = bytenr - cache->start;
WARN_ON(byte_in_group > cache->length);
spin_lock(&space_info->lock); spin_lock(&space_info->lock);
spin_lock(&cache->lock); spin_lock(&cache->lock);
...@@ -3591,7 +3585,6 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, ...@@ -3591,7 +3585,6 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
cache->disk_cache_state = BTRFS_DC_CLEAR; cache->disk_cache_state = BTRFS_DC_CLEAR;
old_val = cache->used; old_val = cache->used;
num_bytes = min(total, cache->length - byte_in_group);
if (alloc) { if (alloc) {
old_val += num_bytes; old_val += num_bytes;
cache->used = old_val; cache->used = old_val;
...@@ -3605,8 +3598,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, ...@@ -3605,8 +3598,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
old_val -= num_bytes; old_val -= num_bytes;
cache->used = old_val; cache->used = old_val;
cache->pinned += num_bytes; cache->pinned += num_bytes;
btrfs_space_info_update_bytes_pinned(info, space_info, btrfs_space_info_update_bytes_pinned(info, space_info, num_bytes);
num_bytes);
space_info->bytes_used -= num_bytes; space_info->bytes_used -= num_bytes;
space_info->disk_used -= num_bytes * factor; space_info->disk_used -= num_bytes * factor;
...@@ -3615,25 +3607,22 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, ...@@ -3615,25 +3607,22 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
spin_unlock(&cache->lock); spin_unlock(&cache->lock);
spin_unlock(&space_info->lock); spin_unlock(&space_info->lock);
set_extent_bit(&trans->transaction->pinned_extents, set_extent_bit(&trans->transaction->pinned_extents, bytenr,
bytenr, bytenr + num_bytes - 1, bytenr + num_bytes - 1, EXTENT_DIRTY, NULL);
EXTENT_DIRTY, NULL);
} }
spin_lock(&trans->transaction->dirty_bgs_lock); spin_lock(&trans->transaction->dirty_bgs_lock);
if (list_empty(&cache->dirty_list)) { if (list_empty(&cache->dirty_list)) {
list_add_tail(&cache->dirty_list, list_add_tail(&cache->dirty_list, &trans->transaction->dirty_bgs);
&trans->transaction->dirty_bgs);
trans->delayed_ref_updates++; trans->delayed_ref_updates++;
btrfs_get_block_group(cache); btrfs_get_block_group(cache);
} }
spin_unlock(&trans->transaction->dirty_bgs_lock); spin_unlock(&trans->transaction->dirty_bgs_lock);
/* /*
* No longer have used bytes in this block group, queue it for * No longer have used bytes in this block group, queue it for deletion.
* deletion. We do this after adding the block group to the * We do this after adding the block group to the dirty list to avoid
* dirty list to avoid races between cleaner kthread and space * races between cleaner kthread and space cache writeout.
* cache writeout.
*/ */
if (!alloc && old_val == 0) { if (!alloc && old_val == 0) {
if (!btrfs_test_opt(info, DISCARD_ASYNC)) if (!btrfs_test_opt(info, DISCARD_ASYNC))
...@@ -3643,13 +3632,11 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, ...@@ -3643,13 +3632,11 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
} }
btrfs_put_block_group(cache); btrfs_put_block_group(cache);
total -= num_bytes;
bytenr += num_bytes;
}
/* Modified block groups are accounted for in the delayed_refs_rsv. */ /* Modified block groups are accounted for in the delayed_refs_rsv. */
btrfs_update_delayed_refs_rsv(trans); btrfs_update_delayed_refs_rsv(trans);
return ret;
return 0;
} }
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment