Skip to content

Commit

Permalink
Btrfs: fix necessary chunk tree space calculation when allocating a c…
Browse files Browse the repository at this point in the history
…hunk

When allocating a new chunk or removing one we need to update num_devs
device items and insert or remove a chunk item in the chunk tree, so
in the worst case the space needed in the chunk space_info is:

  btrfs_calc_trunc_metadata_size(chunk_root, num_devs) +
     btrfs_calc_trans_metadata_size(chunk_root, 1)

That is, in the worst case we need to cow num_devs paths and cow 1 other
path that can result in splitting every node and leaf, and each path
consisting of BTRFS_MAX_LEVEL - 1 nodes and 1 leaf. We were requiring
some additional chunk_root->nodesize * BTRFS_MAX_LEVEL * num_devs bytes,
which were unnecessary since updating the existing device items does
not result in splitting the nodes and leaf since after updating them
they remain with the same size.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Chris Mason <clm@fb.com>
  • Loading branch information
Filipe Manana authored and Chris Mason committed Jun 10, 2015
1 parent 7558c8b commit 4617ea3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
3 changes: 1 addition & 2 deletions fs/btrfs/ctree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3518,8 +3518,7 @@ int btrfs_start_write_no_snapshoting(struct btrfs_root *root);
void btrfs_end_write_no_snapshoting(struct btrfs_root *root);
void check_system_chunk(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
const u64 type,
const bool is_allocation);
const u64 type);
/* ctree.c */
int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
int level, int *slot);
Expand Down
14 changes: 5 additions & 9 deletions fs/btrfs/extent-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -4116,8 +4116,7 @@ static u64 get_profile_num_devs(struct btrfs_root *root, u64 type)
*/
void check_system_chunk(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
u64 type,
const bool is_allocation)
u64 type)
{
struct btrfs_space_info *info;
u64 left;
Expand All @@ -4141,11 +4140,8 @@ void check_system_chunk(struct btrfs_trans_handle *trans,
num_devs = get_profile_num_devs(root, type);

/* num_devs device items to update and 1 chunk item to add or remove */
if (is_allocation)
thresh = btrfs_calc_trans_metadata_size(root, num_devs + 1);
else
thresh = btrfs_calc_trans_metadata_size(root, num_devs) +
btrfs_calc_trunc_metadata_size(root, 1);
thresh = btrfs_calc_trunc_metadata_size(root, num_devs) +
btrfs_calc_trans_metadata_size(root, 1);

if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
Expand Down Expand Up @@ -4258,7 +4254,7 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans,
* Check if we have enough space in SYSTEM chunk because we may need
* to update devices.
*/
check_system_chunk(trans, extent_root, flags, true);
check_system_chunk(trans, extent_root, flags);

ret = btrfs_alloc_chunk(trans, extent_root, flags);
trans->allocating_chunk = false;
Expand Down Expand Up @@ -8926,7 +8922,7 @@ int btrfs_set_block_group_ro(struct btrfs_root *root,
if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
alloc_flags = update_block_group_flags(root, cache->flags);
lock_chunks(root->fs_info->chunk_root);
check_system_chunk(trans, root, alloc_flags, true);
check_system_chunk(trans, root, alloc_flags);
unlock_chunks(root->fs_info->chunk_root);
}
mutex_unlock(&root->fs_info->ro_block_group_mutex);
Expand Down
2 changes: 1 addition & 1 deletion fs/btrfs/volumes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2626,7 +2626,7 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
}
map = (struct map_lookup *)em->bdev;
lock_chunks(root->fs_info->chunk_root);
check_system_chunk(trans, extent_root, map->type, false);
check_system_chunk(trans, extent_root, map->type);
unlock_chunks(root->fs_info->chunk_root);

for (i = 0; i < map->num_stripes; i++) {
Expand Down

0 comments on commit 4617ea3

Please sign in to comment.