diff options
author | Miao Xie <miaox@cn.fujitsu.com> | 2014-01-15 07:00:55 -0500 |
---|---|---|
committer | Chris Mason <clm@fb.com> | 2014-01-28 16:20:38 -0500 |
commit | 920e4a58d27ea146b34674cf9565ab0373f9ca51 (patch) | |
tree | 79ff915c093a19fc6f9668f944fffeb17cbc08a7 /fs | |
parent | 26b47ff65bcdff8473b87680d8f876c66208087b (diff) |
Btrfs: cleanup the redundant code for the block group allocation and init
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: Chris Mason <clm@fb.com>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/btrfs/extent-tree.c | 94 |
1 files changed, 44 insertions, 50 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index db1e32ffcfbb..1efcc262be47 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c | |||
@@ -8364,6 +8364,41 @@ static void __link_block_group(struct btrfs_space_info *space_info, | |||
8364 | up_write(&space_info->groups_sem); | 8364 | up_write(&space_info->groups_sem); |
8365 | } | 8365 | } |
8366 | 8366 | ||
8367 | static struct btrfs_block_group_cache * | ||
8368 | btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size) | ||
8369 | { | ||
8370 | struct btrfs_block_group_cache *cache; | ||
8371 | |||
8372 | cache = kzalloc(sizeof(*cache), GFP_NOFS); | ||
8373 | if (!cache) | ||
8374 | return NULL; | ||
8375 | |||
8376 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), | ||
8377 | GFP_NOFS); | ||
8378 | if (!cache->free_space_ctl) { | ||
8379 | kfree(cache); | ||
8380 | return NULL; | ||
8381 | } | ||
8382 | |||
8383 | cache->key.objectid = start; | ||
8384 | cache->key.offset = size; | ||
8385 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; | ||
8386 | |||
8387 | cache->sectorsize = root->sectorsize; | ||
8388 | cache->fs_info = root->fs_info; | ||
8389 | cache->full_stripe_len = btrfs_full_stripe_len(root, | ||
8390 | &root->fs_info->mapping_tree, | ||
8391 | start); | ||
8392 | atomic_set(&cache->count, 1); | ||
8393 | spin_lock_init(&cache->lock); | ||
8394 | INIT_LIST_HEAD(&cache->list); | ||
8395 | INIT_LIST_HEAD(&cache->cluster_list); | ||
8396 | INIT_LIST_HEAD(&cache->new_bg_list); | ||
8397 | btrfs_init_free_space_ctl(cache); | ||
8398 | |||
8399 | return cache; | ||
8400 | } | ||
8401 | |||
8367 | int btrfs_read_block_groups(struct btrfs_root *root) | 8402 | int btrfs_read_block_groups(struct btrfs_root *root) |
8368 | { | 8403 | { |
8369 | struct btrfs_path *path; | 8404 | struct btrfs_path *path; |
@@ -8399,26 +8434,16 @@ int btrfs_read_block_groups(struct btrfs_root *root) | |||
8399 | break; | 8434 | break; |
8400 | if (ret != 0) | 8435 | if (ret != 0) |
8401 | goto error; | 8436 | goto error; |
8437 | |||
8402 | leaf = path->nodes[0]; | 8438 | leaf = path->nodes[0]; |
8403 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | 8439 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
8404 | cache = kzalloc(sizeof(*cache), GFP_NOFS); | 8440 | |
8441 | cache = btrfs_create_block_group_cache(root, found_key.objectid, | ||
8442 | found_key.offset); | ||
8405 | if (!cache) { | 8443 | if (!cache) { |
8406 | ret = -ENOMEM; | 8444 | ret = -ENOMEM; |
8407 | goto error; | 8445 | goto error; |
8408 | } | 8446 | } |
8409 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), | ||
8410 | GFP_NOFS); | ||
8411 | if (!cache->free_space_ctl) { | ||
8412 | kfree(cache); | ||
8413 | ret = -ENOMEM; | ||
8414 | goto error; | ||
8415 | } | ||
8416 | |||
8417 | atomic_set(&cache->count, 1); | ||
8418 | spin_lock_init(&cache->lock); | ||
8419 | cache->fs_info = info; | ||
8420 | INIT_LIST_HEAD(&cache->list); | ||
8421 | INIT_LIST_HEAD(&cache->cluster_list); | ||
8422 | 8447 | ||
8423 | if (need_clear) { | 8448 | if (need_clear) { |
8424 | /* | 8449 | /* |
@@ -8439,16 +8464,10 @@ int btrfs_read_block_groups(struct btrfs_root *root) | |||
8439 | read_extent_buffer(leaf, &cache->item, | 8464 | read_extent_buffer(leaf, &cache->item, |
8440 | btrfs_item_ptr_offset(leaf, path->slots[0]), | 8465 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
8441 | sizeof(cache->item)); | 8466 | sizeof(cache->item)); |
8442 | memcpy(&cache->key, &found_key, sizeof(found_key)); | 8467 | cache->flags = btrfs_block_group_flags(&cache->item); |
8443 | 8468 | ||
8444 | key.objectid = found_key.objectid + found_key.offset; | 8469 | key.objectid = found_key.objectid + found_key.offset; |
8445 | btrfs_release_path(path); | 8470 | btrfs_release_path(path); |
8446 | cache->flags = btrfs_block_group_flags(&cache->item); | ||
8447 | cache->sectorsize = root->sectorsize; | ||
8448 | cache->full_stripe_len = btrfs_full_stripe_len(root, | ||
8449 | &root->fs_info->mapping_tree, | ||
8450 | found_key.objectid); | ||
8451 | btrfs_init_free_space_ctl(cache); | ||
8452 | 8471 | ||
8453 | /* | 8472 | /* |
8454 | * We need to exclude the super stripes now so that the space | 8473 | * We need to exclude the super stripes now so that the space |
@@ -8462,8 +8481,7 @@ int btrfs_read_block_groups(struct btrfs_root *root) | |||
8462 | * case. | 8481 | * case. |
8463 | */ | 8482 | */ |
8464 | free_excluded_extents(root, cache); | 8483 | free_excluded_extents(root, cache); |
8465 | kfree(cache->free_space_ctl); | 8484 | btrfs_put_block_group(cache); |
8466 | kfree(cache); | ||
8467 | goto error; | 8485 | goto error; |
8468 | } | 8486 | } |
8469 | 8487 | ||
@@ -8594,38 +8612,15 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans, | |||
8594 | 8612 | ||
8595 | root->fs_info->last_trans_log_full_commit = trans->transid; | 8613 | root->fs_info->last_trans_log_full_commit = trans->transid; |
8596 | 8614 | ||
8597 | cache = kzalloc(sizeof(*cache), GFP_NOFS); | 8615 | cache = btrfs_create_block_group_cache(root, chunk_offset, size); |
8598 | if (!cache) | 8616 | if (!cache) |
8599 | return -ENOMEM; | 8617 | return -ENOMEM; |
8600 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), | ||
8601 | GFP_NOFS); | ||
8602 | if (!cache->free_space_ctl) { | ||
8603 | kfree(cache); | ||
8604 | return -ENOMEM; | ||
8605 | } | ||
8606 | |||
8607 | cache->key.objectid = chunk_offset; | ||
8608 | cache->key.offset = size; | ||
8609 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; | ||
8610 | cache->sectorsize = root->sectorsize; | ||
8611 | cache->fs_info = root->fs_info; | ||
8612 | cache->full_stripe_len = btrfs_full_stripe_len(root, | ||
8613 | &root->fs_info->mapping_tree, | ||
8614 | chunk_offset); | ||
8615 | |||
8616 | atomic_set(&cache->count, 1); | ||
8617 | spin_lock_init(&cache->lock); | ||
8618 | INIT_LIST_HEAD(&cache->list); | ||
8619 | INIT_LIST_HEAD(&cache->cluster_list); | ||
8620 | INIT_LIST_HEAD(&cache->new_bg_list); | ||
8621 | |||
8622 | btrfs_init_free_space_ctl(cache); | ||
8623 | 8618 | ||
8624 | btrfs_set_block_group_used(&cache->item, bytes_used); | 8619 | btrfs_set_block_group_used(&cache->item, bytes_used); |
8625 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); | 8620 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
8626 | cache->flags = type; | ||
8627 | btrfs_set_block_group_flags(&cache->item, type); | 8621 | btrfs_set_block_group_flags(&cache->item, type); |
8628 | 8622 | ||
8623 | cache->flags = type; | ||
8629 | cache->last_byte_to_unpin = (u64)-1; | 8624 | cache->last_byte_to_unpin = (u64)-1; |
8630 | cache->cached = BTRFS_CACHE_FINISHED; | 8625 | cache->cached = BTRFS_CACHE_FINISHED; |
8631 | ret = exclude_super_stripes(root, cache); | 8626 | ret = exclude_super_stripes(root, cache); |
@@ -8635,8 +8630,7 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans, | |||
8635 | * case. | 8630 | * case. |
8636 | */ | 8631 | */ |
8637 | free_excluded_extents(root, cache); | 8632 | free_excluded_extents(root, cache); |
8638 | kfree(cache->free_space_ctl); | 8633 | btrfs_put_block_group(cache); |
8639 | kfree(cache); | ||
8640 | return ret; | 8634 | return ret; |
8641 | } | 8635 | } |
8642 | 8636 | ||