diff options
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r-- | fs/btrfs/extent-tree.c | 147 |
1 files changed, 113 insertions, 34 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index b34d32fdaaec..f5db03669ead 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c | |||
@@ -507,6 +507,9 @@ static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, | |||
507 | struct list_head *head = &info->space_info; | 507 | struct list_head *head = &info->space_info; |
508 | struct btrfs_space_info *found; | 508 | struct btrfs_space_info *found; |
509 | 509 | ||
510 | flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM | | ||
511 | BTRFS_BLOCK_GROUP_METADATA; | ||
512 | |||
510 | rcu_read_lock(); | 513 | rcu_read_lock(); |
511 | list_for_each_entry_rcu(found, head, list) { | 514 | list_for_each_entry_rcu(found, head, list) { |
512 | if (found->flags == flags) { | 515 | if (found->flags == flags) { |
@@ -2660,12 +2663,21 @@ static int update_space_info(struct btrfs_fs_info *info, u64 flags, | |||
2660 | struct btrfs_space_info **space_info) | 2663 | struct btrfs_space_info **space_info) |
2661 | { | 2664 | { |
2662 | struct btrfs_space_info *found; | 2665 | struct btrfs_space_info *found; |
2666 | int i; | ||
2667 | int factor; | ||
2668 | |||
2669 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | | ||
2670 | BTRFS_BLOCK_GROUP_RAID10)) | ||
2671 | factor = 2; | ||
2672 | else | ||
2673 | factor = 1; | ||
2663 | 2674 | ||
2664 | found = __find_space_info(info, flags); | 2675 | found = __find_space_info(info, flags); |
2665 | if (found) { | 2676 | if (found) { |
2666 | spin_lock(&found->lock); | 2677 | spin_lock(&found->lock); |
2667 | found->total_bytes += total_bytes; | 2678 | found->total_bytes += total_bytes; |
2668 | found->bytes_used += bytes_used; | 2679 | found->bytes_used += bytes_used; |
2680 | found->disk_used += bytes_used * factor; | ||
2669 | found->full = 0; | 2681 | found->full = 0; |
2670 | spin_unlock(&found->lock); | 2682 | spin_unlock(&found->lock); |
2671 | *space_info = found; | 2683 | *space_info = found; |
@@ -2675,14 +2687,18 @@ static int update_space_info(struct btrfs_fs_info *info, u64 flags, | |||
2675 | if (!found) | 2687 | if (!found) |
2676 | return -ENOMEM; | 2688 | return -ENOMEM; |
2677 | 2689 | ||
2678 | INIT_LIST_HEAD(&found->block_groups); | 2690 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
2691 | INIT_LIST_HEAD(&found->block_groups[i]); | ||
2679 | init_rwsem(&found->groups_sem); | 2692 | init_rwsem(&found->groups_sem); |
2680 | init_waitqueue_head(&found->flush_wait); | 2693 | init_waitqueue_head(&found->flush_wait); |
2681 | init_waitqueue_head(&found->allocate_wait); | 2694 | init_waitqueue_head(&found->allocate_wait); |
2682 | spin_lock_init(&found->lock); | 2695 | spin_lock_init(&found->lock); |
2683 | found->flags = flags; | 2696 | found->flags = flags & (BTRFS_BLOCK_GROUP_DATA | |
2697 | BTRFS_BLOCK_GROUP_SYSTEM | | ||
2698 | BTRFS_BLOCK_GROUP_METADATA); | ||
2684 | found->total_bytes = total_bytes; | 2699 | found->total_bytes = total_bytes; |
2685 | found->bytes_used = bytes_used; | 2700 | found->bytes_used = bytes_used; |
2701 | found->disk_used = bytes_used * factor; | ||
2686 | found->bytes_pinned = 0; | 2702 | found->bytes_pinned = 0; |
2687 | found->bytes_reserved = 0; | 2703 | found->bytes_reserved = 0; |
2688 | found->bytes_readonly = 0; | 2704 | found->bytes_readonly = 0; |
@@ -2752,26 +2768,32 @@ u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) | |||
2752 | return flags; | 2768 | return flags; |
2753 | } | 2769 | } |
2754 | 2770 | ||
2755 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data) | 2771 | static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) |
2756 | { | 2772 | { |
2757 | struct btrfs_fs_info *info = root->fs_info; | 2773 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
2758 | u64 alloc_profile; | 2774 | flags |= root->fs_info->avail_data_alloc_bits & |
2759 | 2775 | root->fs_info->data_alloc_profile; | |
2760 | if (data) { | 2776 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
2761 | alloc_profile = info->avail_data_alloc_bits & | 2777 | flags |= root->fs_info->avail_system_alloc_bits & |
2762 | info->data_alloc_profile; | 2778 | root->fs_info->system_alloc_profile; |
2763 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; | 2779 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
2764 | } else if (root == root->fs_info->chunk_root) { | 2780 | flags |= root->fs_info->avail_metadata_alloc_bits & |
2765 | alloc_profile = info->avail_system_alloc_bits & | 2781 | root->fs_info->metadata_alloc_profile; |
2766 | info->system_alloc_profile; | 2782 | return btrfs_reduce_alloc_profile(root, flags); |
2767 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; | 2783 | } |
2768 | } else { | 2784 | |
2769 | alloc_profile = info->avail_metadata_alloc_bits & | 2785 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
2770 | info->metadata_alloc_profile; | 2786 | { |
2771 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; | 2787 | u64 flags; |
2772 | } | ||
2773 | 2788 | ||
2774 | return btrfs_reduce_alloc_profile(root, data); | 2789 | if (data) |
2790 | flags = BTRFS_BLOCK_GROUP_DATA; | ||
2791 | else if (root == root->fs_info->chunk_root) | ||
2792 | flags = BTRFS_BLOCK_GROUP_SYSTEM; | ||
2793 | else | ||
2794 | flags = BTRFS_BLOCK_GROUP_METADATA; | ||
2795 | |||
2796 | return get_alloc_profile(root, flags); | ||
2775 | } | 2797 | } |
2776 | 2798 | ||
2777 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) | 2799 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
@@ -3468,6 +3490,7 @@ static int update_block_group(struct btrfs_trans_handle *trans, | |||
3468 | { | 3490 | { |
3469 | struct btrfs_block_group_cache *cache; | 3491 | struct btrfs_block_group_cache *cache; |
3470 | struct btrfs_fs_info *info = root->fs_info; | 3492 | struct btrfs_fs_info *info = root->fs_info; |
3493 | int factor; | ||
3471 | u64 total = num_bytes; | 3494 | u64 total = num_bytes; |
3472 | u64 old_val; | 3495 | u64 old_val; |
3473 | u64 byte_in_group; | 3496 | u64 byte_in_group; |
@@ -3486,6 +3509,12 @@ static int update_block_group(struct btrfs_trans_handle *trans, | |||
3486 | cache = btrfs_lookup_block_group(info, bytenr); | 3509 | cache = btrfs_lookup_block_group(info, bytenr); |
3487 | if (!cache) | 3510 | if (!cache) |
3488 | return -1; | 3511 | return -1; |
3512 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | | ||
3513 | BTRFS_BLOCK_GROUP_RAID1 | | ||
3514 | BTRFS_BLOCK_GROUP_RAID10)) | ||
3515 | factor = 2; | ||
3516 | else | ||
3517 | factor = 1; | ||
3489 | byte_in_group = bytenr - cache->key.objectid; | 3518 | byte_in_group = bytenr - cache->key.objectid; |
3490 | WARN_ON(byte_in_group > cache->key.offset); | 3519 | WARN_ON(byte_in_group > cache->key.offset); |
3491 | 3520 | ||
@@ -3498,18 +3527,20 @@ static int update_block_group(struct btrfs_trans_handle *trans, | |||
3498 | old_val += num_bytes; | 3527 | old_val += num_bytes; |
3499 | btrfs_set_block_group_used(&cache->item, old_val); | 3528 | btrfs_set_block_group_used(&cache->item, old_val); |
3500 | cache->reserved -= num_bytes; | 3529 | cache->reserved -= num_bytes; |
3501 | cache->space_info->bytes_used += num_bytes; | ||
3502 | cache->space_info->bytes_reserved -= num_bytes; | 3530 | cache->space_info->bytes_reserved -= num_bytes; |
3531 | cache->space_info->bytes_used += num_bytes; | ||
3532 | cache->space_info->disk_used += num_bytes * factor; | ||
3503 | if (cache->ro) | 3533 | if (cache->ro) |
3504 | cache->space_info->bytes_readonly -= num_bytes; | 3534 | cache->space_info->bytes_readonly -= num_bytes; |
3505 | spin_unlock(&cache->lock); | 3535 | spin_unlock(&cache->lock); |
3506 | spin_unlock(&cache->space_info->lock); | 3536 | spin_unlock(&cache->space_info->lock); |
3507 | } else { | 3537 | } else { |
3508 | old_val -= num_bytes; | 3538 | old_val -= num_bytes; |
3539 | btrfs_set_block_group_used(&cache->item, old_val); | ||
3509 | cache->space_info->bytes_used -= num_bytes; | 3540 | cache->space_info->bytes_used -= num_bytes; |
3541 | cache->space_info->disk_used -= num_bytes * factor; | ||
3510 | if (cache->ro) | 3542 | if (cache->ro) |
3511 | cache->space_info->bytes_readonly += num_bytes; | 3543 | cache->space_info->bytes_readonly += num_bytes; |
3512 | btrfs_set_block_group_used(&cache->item, old_val); | ||
3513 | spin_unlock(&cache->lock); | 3544 | spin_unlock(&cache->lock); |
3514 | spin_unlock(&cache->space_info->lock); | 3545 | spin_unlock(&cache->space_info->lock); |
3515 | if (mark_free) { | 3546 | if (mark_free) { |
@@ -4134,6 +4165,22 @@ wait_block_group_cache_done(struct btrfs_block_group_cache *cache) | |||
4134 | return 0; | 4165 | return 0; |
4135 | } | 4166 | } |
4136 | 4167 | ||
4168 | static int get_block_group_index(struct btrfs_block_group_cache *cache) | ||
4169 | { | ||
4170 | int index; | ||
4171 | if (cache->flags & BTRFS_BLOCK_GROUP_RAID10) | ||
4172 | index = 0; | ||
4173 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1) | ||
4174 | index = 1; | ||
4175 | else if (cache->flags & BTRFS_BLOCK_GROUP_DUP) | ||
4176 | index = 2; | ||
4177 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0) | ||
4178 | index = 3; | ||
4179 | else | ||
4180 | index = 4; | ||
4181 | return index; | ||
4182 | } | ||
4183 | |||
4137 | enum btrfs_loop_type { | 4184 | enum btrfs_loop_type { |
4138 | LOOP_FIND_IDEAL = 0, | 4185 | LOOP_FIND_IDEAL = 0, |
4139 | LOOP_CACHING_NOWAIT = 1, | 4186 | LOOP_CACHING_NOWAIT = 1, |
@@ -4167,6 +4214,7 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans, | |||
4167 | int done_chunk_alloc = 0; | 4214 | int done_chunk_alloc = 0; |
4168 | struct btrfs_space_info *space_info; | 4215 | struct btrfs_space_info *space_info; |
4169 | int last_ptr_loop = 0; | 4216 | int last_ptr_loop = 0; |
4217 | int index = 0; | ||
4170 | int loop = 0; | 4218 | int loop = 0; |
4171 | bool found_uncached_bg = false; | 4219 | bool found_uncached_bg = false; |
4172 | bool failed_cluster_refill = false; | 4220 | bool failed_cluster_refill = false; |
@@ -4237,6 +4285,7 @@ ideal_cache: | |||
4237 | btrfs_put_block_group(block_group); | 4285 | btrfs_put_block_group(block_group); |
4238 | up_read(&space_info->groups_sem); | 4286 | up_read(&space_info->groups_sem); |
4239 | } else { | 4287 | } else { |
4288 | index = get_block_group_index(block_group); | ||
4240 | goto have_block_group; | 4289 | goto have_block_group; |
4241 | } | 4290 | } |
4242 | } else if (block_group) { | 4291 | } else if (block_group) { |
@@ -4245,7 +4294,8 @@ ideal_cache: | |||
4245 | } | 4294 | } |
4246 | search: | 4295 | search: |
4247 | down_read(&space_info->groups_sem); | 4296 | down_read(&space_info->groups_sem); |
4248 | list_for_each_entry(block_group, &space_info->block_groups, list) { | 4297 | list_for_each_entry(block_group, &space_info->block_groups[index], |
4298 | list) { | ||
4249 | u64 offset; | 4299 | u64 offset; |
4250 | int cached; | 4300 | int cached; |
4251 | 4301 | ||
@@ -4468,10 +4518,14 @@ checks: | |||
4468 | loop: | 4518 | loop: |
4469 | failed_cluster_refill = false; | 4519 | failed_cluster_refill = false; |
4470 | failed_alloc = false; | 4520 | failed_alloc = false; |
4521 | BUG_ON(index != get_block_group_index(block_group)); | ||
4471 | btrfs_put_block_group(block_group); | 4522 | btrfs_put_block_group(block_group); |
4472 | } | 4523 | } |
4473 | up_read(&space_info->groups_sem); | 4524 | up_read(&space_info->groups_sem); |
4474 | 4525 | ||
4526 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) | ||
4527 | goto search; | ||
4528 | |||
4475 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for | 4529 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
4476 | * for them to make caching progress. Also | 4530 | * for them to make caching progress. Also |
4477 | * determine the best possible bg to cache | 4531 | * determine the best possible bg to cache |
@@ -4485,6 +4539,7 @@ loop: | |||
4485 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && | 4539 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && |
4486 | (found_uncached_bg || empty_size || empty_cluster || | 4540 | (found_uncached_bg || empty_size || empty_cluster || |
4487 | allowed_chunk_alloc)) { | 4541 | allowed_chunk_alloc)) { |
4542 | index = 0; | ||
4488 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { | 4543 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
4489 | found_uncached_bg = false; | 4544 | found_uncached_bg = false; |
4490 | loop++; | 4545 | loop++; |
@@ -4567,6 +4622,7 @@ static void dump_space_info(struct btrfs_space_info *info, u64 bytes, | |||
4567 | int dump_block_groups) | 4622 | int dump_block_groups) |
4568 | { | 4623 | { |
4569 | struct btrfs_block_group_cache *cache; | 4624 | struct btrfs_block_group_cache *cache; |
4625 | int index = 0; | ||
4570 | 4626 | ||
4571 | spin_lock(&info->lock); | 4627 | spin_lock(&info->lock); |
4572 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", | 4628 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
@@ -4591,7 +4647,8 @@ static void dump_space_info(struct btrfs_space_info *info, u64 bytes, | |||
4591 | return; | 4647 | return; |
4592 | 4648 | ||
4593 | down_read(&info->groups_sem); | 4649 | down_read(&info->groups_sem); |
4594 | list_for_each_entry(cache, &info->block_groups, list) { | 4650 | again: |
4651 | list_for_each_entry(cache, &info->block_groups[index], list) { | ||
4595 | spin_lock(&cache->lock); | 4652 | spin_lock(&cache->lock); |
4596 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " | 4653 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
4597 | "%llu pinned %llu reserved\n", | 4654 | "%llu pinned %llu reserved\n", |
@@ -4603,6 +4660,8 @@ static void dump_space_info(struct btrfs_space_info *info, u64 bytes, | |||
4603 | btrfs_dump_free_space(cache, bytes); | 4660 | btrfs_dump_free_space(cache, bytes); |
4604 | spin_unlock(&cache->lock); | 4661 | spin_unlock(&cache->lock); |
4605 | } | 4662 | } |
4663 | if (++index < BTRFS_NR_RAID_TYPES) | ||
4664 | goto again; | ||
4606 | up_read(&info->groups_sem); | 4665 | up_read(&info->groups_sem); |
4607 | } | 4666 | } |
4608 | 4667 | ||
@@ -7447,6 +7506,16 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info) | |||
7447 | return 0; | 7506 | return 0; |
7448 | } | 7507 | } |
7449 | 7508 | ||
7509 | static void __link_block_group(struct btrfs_space_info *space_info, | ||
7510 | struct btrfs_block_group_cache *cache) | ||
7511 | { | ||
7512 | int index = get_block_group_index(cache); | ||
7513 | |||
7514 | down_write(&space_info->groups_sem); | ||
7515 | list_add_tail(&cache->list, &space_info->block_groups[index]); | ||
7516 | up_write(&space_info->groups_sem); | ||
7517 | } | ||
7518 | |||
7450 | int btrfs_read_block_groups(struct btrfs_root *root) | 7519 | int btrfs_read_block_groups(struct btrfs_root *root) |
7451 | { | 7520 | { |
7452 | struct btrfs_path *path; | 7521 | struct btrfs_path *path; |
@@ -7468,10 +7537,8 @@ int btrfs_read_block_groups(struct btrfs_root *root) | |||
7468 | 7537 | ||
7469 | while (1) { | 7538 | while (1) { |
7470 | ret = find_first_block_group(root, path, &key); | 7539 | ret = find_first_block_group(root, path, &key); |
7471 | if (ret > 0) { | 7540 | if (ret > 0) |
7472 | ret = 0; | 7541 | break; |
7473 | goto error; | ||
7474 | } | ||
7475 | if (ret != 0) | 7542 | if (ret != 0) |
7476 | goto error; | 7543 | goto error; |
7477 | 7544 | ||
@@ -7540,9 +7607,7 @@ int btrfs_read_block_groups(struct btrfs_root *root) | |||
7540 | cache->space_info->bytes_super += cache->bytes_super; | 7607 | cache->space_info->bytes_super += cache->bytes_super; |
7541 | spin_unlock(&cache->space_info->lock); | 7608 | spin_unlock(&cache->space_info->lock); |
7542 | 7609 | ||
7543 | down_write(&space_info->groups_sem); | 7610 | __link_block_group(space_info, cache); |
7544 | list_add_tail(&cache->list, &space_info->block_groups); | ||
7545 | up_write(&space_info->groups_sem); | ||
7546 | 7611 | ||
7547 | ret = btrfs_add_block_group_cache(root->fs_info, cache); | 7612 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
7548 | BUG_ON(ret); | 7613 | BUG_ON(ret); |
@@ -7551,6 +7616,22 @@ int btrfs_read_block_groups(struct btrfs_root *root) | |||
7551 | if (btrfs_chunk_readonly(root, cache->key.objectid)) | 7616 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
7552 | set_block_group_readonly(cache); | 7617 | set_block_group_readonly(cache); |
7553 | } | 7618 | } |
7619 | |||
7620 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { | ||
7621 | if (!(get_alloc_profile(root, space_info->flags) & | ||
7622 | (BTRFS_BLOCK_GROUP_RAID10 | | ||
7623 | BTRFS_BLOCK_GROUP_RAID1 | | ||
7624 | BTRFS_BLOCK_GROUP_DUP))) | ||
7625 | continue; | ||
7626 | /* | ||
7627 | * avoid allocating from un-mirrored block group if there are | ||
7628 | * mirrored block groups. | ||
7629 | */ | ||
7630 | list_for_each_entry(cache, &space_info->block_groups[3], list) | ||
7631 | set_block_group_readonly(cache); | ||
7632 | list_for_each_entry(cache, &space_info->block_groups[4], list) | ||
7633 | set_block_group_readonly(cache); | ||
7634 | } | ||
7554 | ret = 0; | 7635 | ret = 0; |
7555 | error: | 7636 | error: |
7556 | btrfs_free_path(path); | 7637 | btrfs_free_path(path); |
@@ -7614,9 +7695,7 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans, | |||
7614 | cache->space_info->bytes_super += cache->bytes_super; | 7695 | cache->space_info->bytes_super += cache->bytes_super; |
7615 | spin_unlock(&cache->space_info->lock); | 7696 | spin_unlock(&cache->space_info->lock); |
7616 | 7697 | ||
7617 | down_write(&cache->space_info->groups_sem); | 7698 | __link_block_group(cache->space_info, cache); |
7618 | list_add_tail(&cache->list, &cache->space_info->block_groups); | ||
7619 | up_write(&cache->space_info->groups_sem); | ||
7620 | 7699 | ||
7621 | ret = btrfs_add_block_group_cache(root->fs_info, cache); | 7700 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
7622 | BUG_ON(ret); | 7701 | BUG_ON(ret); |