aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/extent-tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r--fs/btrfs/extent-tree.c398
1 files changed, 194 insertions, 204 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index f5e7cae63d80..178df4c67de4 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -31,6 +31,7 @@
31#include "volumes.h" 31#include "volumes.h"
32#include "locking.h" 32#include "locking.h"
33#include "ref-cache.h" 33#include "ref-cache.h"
34#include "free-space-cache.h"
34 35
35#define PENDING_EXTENT_INSERT 0 36#define PENDING_EXTENT_INSERT 0
36#define PENDING_EXTENT_DELETE 1 37#define PENDING_EXTENT_DELETE 1
@@ -166,7 +167,6 @@ static int add_new_free_space(struct btrfs_block_group_cache *block_group,
166 u64 extent_start, extent_end, size; 167 u64 extent_start, extent_end, size;
167 int ret; 168 int ret;
168 169
169 mutex_lock(&info->pinned_mutex);
170 while (start < end) { 170 while (start < end) {
171 ret = find_first_extent_bit(&info->pinned_extents, start, 171 ret = find_first_extent_bit(&info->pinned_extents, start,
172 &extent_start, &extent_end, 172 &extent_start, &extent_end,
@@ -192,7 +192,6 @@ static int add_new_free_space(struct btrfs_block_group_cache *block_group,
192 ret = btrfs_add_free_space(block_group, start, size); 192 ret = btrfs_add_free_space(block_group, start, size);
193 BUG_ON(ret); 193 BUG_ON(ret);
194 } 194 }
195 mutex_unlock(&info->pinned_mutex);
196 195
197 return 0; 196 return 0;
198} 197}
@@ -291,8 +290,8 @@ next:
291 block_group->key.objectid + 290 block_group->key.objectid +
292 block_group->key.offset); 291 block_group->key.offset);
293 292
294 remove_sb_from_cache(root, block_group);
295 block_group->cached = 1; 293 block_group->cached = 1;
294 remove_sb_from_cache(root, block_group);
296 ret = 0; 295 ret = 0;
297err: 296err:
298 btrfs_free_path(path); 297 btrfs_free_path(path);
@@ -326,7 +325,7 @@ struct btrfs_block_group_cache *btrfs_lookup_block_group(
326 return cache; 325 return cache;
327} 326}
328 327
329static inline void put_block_group(struct btrfs_block_group_cache *cache) 328void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
330{ 329{
331 if (atomic_dec_and_test(&cache->count)) 330 if (atomic_dec_and_test(&cache->count))
332 kfree(cache); 331 kfree(cache);
@@ -399,12 +398,12 @@ again:
399 div_factor(cache->key.offset, factor)) { 398 div_factor(cache->key.offset, factor)) {
400 group_start = cache->key.objectid; 399 group_start = cache->key.objectid;
401 spin_unlock(&cache->lock); 400 spin_unlock(&cache->lock);
402 put_block_group(cache); 401 btrfs_put_block_group(cache);
403 goto found; 402 goto found;
404 } 403 }
405 } 404 }
406 spin_unlock(&cache->lock); 405 spin_unlock(&cache->lock);
407 put_block_group(cache); 406 btrfs_put_block_group(cache);
408 cond_resched(); 407 cond_resched();
409 } 408 }
410 if (!wrapped) { 409 if (!wrapped) {
@@ -1594,7 +1593,7 @@ int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1594 if (!block_group || block_group->ro) 1593 if (!block_group || block_group->ro)
1595 readonly = 1; 1594 readonly = 1;
1596 if (block_group) 1595 if (block_group)
1597 put_block_group(block_group); 1596 btrfs_put_block_group(block_group);
1598 return readonly; 1597 return readonly;
1599} 1598}
1600 1599
@@ -2018,7 +2017,7 @@ static int update_block_group(struct btrfs_trans_handle *trans,
2018 WARN_ON(ret); 2017 WARN_ON(ret);
2019 } 2018 }
2020 } 2019 }
2021 put_block_group(cache); 2020 btrfs_put_block_group(cache);
2022 total -= num_bytes; 2021 total -= num_bytes;
2023 bytenr += num_bytes; 2022 bytenr += num_bytes;
2024 } 2023 }
@@ -2035,7 +2034,7 @@ static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2035 return 0; 2034 return 0;
2036 2035
2037 bytenr = cache->key.objectid; 2036 bytenr = cache->key.objectid;
2038 put_block_group(cache); 2037 btrfs_put_block_group(cache);
2039 2038
2040 return bytenr; 2039 return bytenr;
2041} 2040}
@@ -2047,7 +2046,6 @@ int btrfs_update_pinned_extents(struct btrfs_root *root,
2047 struct btrfs_block_group_cache *cache; 2046 struct btrfs_block_group_cache *cache;
2048 struct btrfs_fs_info *fs_info = root->fs_info; 2047 struct btrfs_fs_info *fs_info = root->fs_info;
2049 2048
2050 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
2051 if (pin) { 2049 if (pin) {
2052 set_extent_dirty(&fs_info->pinned_extents, 2050 set_extent_dirty(&fs_info->pinned_extents,
2053 bytenr, bytenr + num - 1, GFP_NOFS); 2051 bytenr, bytenr + num - 1, GFP_NOFS);
@@ -2055,7 +2053,6 @@ int btrfs_update_pinned_extents(struct btrfs_root *root,
2055 clear_extent_dirty(&fs_info->pinned_extents, 2053 clear_extent_dirty(&fs_info->pinned_extents,
2056 bytenr, bytenr + num - 1, GFP_NOFS); 2054 bytenr, bytenr + num - 1, GFP_NOFS);
2057 } 2055 }
2058 mutex_unlock(&root->fs_info->pinned_mutex);
2059 2056
2060 while (num > 0) { 2057 while (num > 0) {
2061 cache = btrfs_lookup_block_group(fs_info, bytenr); 2058 cache = btrfs_lookup_block_group(fs_info, bytenr);
@@ -2081,7 +2078,7 @@ int btrfs_update_pinned_extents(struct btrfs_root *root,
2081 if (cache->cached) 2078 if (cache->cached)
2082 btrfs_add_free_space(cache, bytenr, len); 2079 btrfs_add_free_space(cache, bytenr, len);
2083 } 2080 }
2084 put_block_group(cache); 2081 btrfs_put_block_group(cache);
2085 bytenr += len; 2082 bytenr += len;
2086 num -= len; 2083 num -= len;
2087 } 2084 }
@@ -2112,7 +2109,7 @@ static int update_reserved_extents(struct btrfs_root *root,
2112 } 2109 }
2113 spin_unlock(&cache->lock); 2110 spin_unlock(&cache->lock);
2114 spin_unlock(&cache->space_info->lock); 2111 spin_unlock(&cache->space_info->lock);
2115 put_block_group(cache); 2112 btrfs_put_block_group(cache);
2116 bytenr += len; 2113 bytenr += len;
2117 num -= len; 2114 num -= len;
2118 } 2115 }
@@ -2127,7 +2124,6 @@ int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2127 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; 2124 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2128 int ret; 2125 int ret;
2129 2126
2130 mutex_lock(&root->fs_info->pinned_mutex);
2131 while (1) { 2127 while (1) {
2132 ret = find_first_extent_bit(pinned_extents, last, 2128 ret = find_first_extent_bit(pinned_extents, last,
2133 &start, &end, EXTENT_DIRTY); 2129 &start, &end, EXTENT_DIRTY);
@@ -2136,7 +2132,6 @@ int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2136 set_extent_dirty(copy, start, end, GFP_NOFS); 2132 set_extent_dirty(copy, start, end, GFP_NOFS);
2137 last = end + 1; 2133 last = end + 1;
2138 } 2134 }
2139 mutex_unlock(&root->fs_info->pinned_mutex);
2140 return 0; 2135 return 0;
2141} 2136}
2142 2137
@@ -2149,7 +2144,6 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2149 int ret; 2144 int ret;
2150 2145
2151 while (1) { 2146 while (1) {
2152 mutex_lock(&root->fs_info->pinned_mutex);
2153 ret = find_first_extent_bit(unpin, 0, &start, &end, 2147 ret = find_first_extent_bit(unpin, 0, &start, &end,
2154 EXTENT_DIRTY); 2148 EXTENT_DIRTY);
2155 if (ret) 2149 if (ret)
@@ -2163,7 +2157,6 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2163 2157
2164 cond_resched(); 2158 cond_resched();
2165 } 2159 }
2166 mutex_unlock(&root->fs_info->pinned_mutex);
2167 return ret; 2160 return ret;
2168} 2161}
2169 2162
@@ -2205,7 +2198,6 @@ static int pin_down_bytes(struct btrfs_trans_handle *trans,
2205 free_extent_buffer(buf); 2198 free_extent_buffer(buf);
2206pinit: 2199pinit:
2207 btrfs_set_path_blocking(path); 2200 btrfs_set_path_blocking(path);
2208 mutex_lock(&root->fs_info->pinned_mutex);
2209 /* unlocks the pinned mutex */ 2201 /* unlocks the pinned mutex */
2210 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); 2202 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2211 2203
@@ -2511,8 +2503,6 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans,
2511 */ 2503 */
2512 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID && 2504 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID &&
2513 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { 2505 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2514 mutex_lock(&root->fs_info->pinned_mutex);
2515
2516 /* unlocks the pinned mutex */ 2506 /* unlocks the pinned mutex */
2517 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); 2507 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2518 update_reserved_extents(root, bytenr, num_bytes, 0); 2508 update_reserved_extents(root, bytenr, num_bytes, 0);
@@ -2554,228 +2544,237 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
2554{ 2544{
2555 int ret = 0; 2545 int ret = 0;
2556 struct btrfs_root *root = orig_root->fs_info->extent_root; 2546 struct btrfs_root *root = orig_root->fs_info->extent_root;
2557 u64 total_needed = num_bytes; 2547 struct btrfs_free_cluster *last_ptr = NULL;
2558 u64 *last_ptr = NULL;
2559 u64 last_wanted = 0;
2560 struct btrfs_block_group_cache *block_group = NULL; 2548 struct btrfs_block_group_cache *block_group = NULL;
2561 int chunk_alloc_done = 0;
2562 int empty_cluster = 2 * 1024 * 1024; 2549 int empty_cluster = 2 * 1024 * 1024;
2563 int allowed_chunk_alloc = 0; 2550 int allowed_chunk_alloc = 0;
2564 struct list_head *head = NULL, *cur = NULL;
2565 int loop = 0;
2566 int extra_loop = 0;
2567 struct btrfs_space_info *space_info; 2551 struct btrfs_space_info *space_info;
2552 int last_ptr_loop = 0;
2553 int loop = 0;
2568 2554
2569 WARN_ON(num_bytes < root->sectorsize); 2555 WARN_ON(num_bytes < root->sectorsize);
2570 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); 2556 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2571 ins->objectid = 0; 2557 ins->objectid = 0;
2572 ins->offset = 0; 2558 ins->offset = 0;
2573 2559
2560 space_info = __find_space_info(root->fs_info, data);
2561
2574 if (orig_root->ref_cows || empty_size) 2562 if (orig_root->ref_cows || empty_size)
2575 allowed_chunk_alloc = 1; 2563 allowed_chunk_alloc = 1;
2576 2564
2577 if (data & BTRFS_BLOCK_GROUP_METADATA) { 2565 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2578 last_ptr = &root->fs_info->last_alloc; 2566 last_ptr = &root->fs_info->meta_alloc_cluster;
2579 if (!btrfs_test_opt(root, SSD)) 2567 if (!btrfs_test_opt(root, SSD))
2580 empty_cluster = 64 * 1024; 2568 empty_cluster = 64 * 1024;
2581 } 2569 }
2582 2570
2583 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) 2571 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
2584 last_ptr = &root->fs_info->last_data_alloc; 2572 last_ptr = &root->fs_info->data_alloc_cluster;
2573 }
2585 2574
2586 if (last_ptr) { 2575 if (last_ptr) {
2587 if (*last_ptr) { 2576 spin_lock(&last_ptr->lock);
2588 hint_byte = *last_ptr; 2577 if (last_ptr->block_group)
2589 last_wanted = *last_ptr; 2578 hint_byte = last_ptr->window_start;
2590 } else 2579 spin_unlock(&last_ptr->lock);
2591 empty_size += empty_cluster;
2592 } else {
2593 empty_cluster = 0;
2594 } 2580 }
2581
2595 search_start = max(search_start, first_logical_byte(root, 0)); 2582 search_start = max(search_start, first_logical_byte(root, 0));
2596 search_start = max(search_start, hint_byte); 2583 search_start = max(search_start, hint_byte);
2597 2584
2598 if (last_wanted && search_start != last_wanted) { 2585 if (!last_ptr) {
2599 last_wanted = 0; 2586 empty_cluster = 0;
2600 empty_size += empty_cluster; 2587 loop = 1;
2601 } 2588 }
2602 2589
2603 total_needed += empty_size; 2590 if (search_start == hint_byte) {
2604 block_group = btrfs_lookup_block_group(root->fs_info, search_start); 2591 block_group = btrfs_lookup_block_group(root->fs_info,
2605 if (!block_group) 2592 search_start);
2606 block_group = btrfs_lookup_first_block_group(root->fs_info, 2593 if (block_group && block_group_bits(block_group, data)) {
2607 search_start); 2594 down_read(&space_info->groups_sem);
2608 space_info = __find_space_info(root->fs_info, data); 2595 goto have_block_group;
2596 } else if (block_group) {
2597 btrfs_put_block_group(block_group);
2598 }
2599 }
2609 2600
2601search:
2610 down_read(&space_info->groups_sem); 2602 down_read(&space_info->groups_sem);
2611 while (1) { 2603 list_for_each_entry(block_group, &space_info->block_groups, list) {
2612 struct btrfs_free_space *free_space; 2604 u64 offset;
2613 /*
2614 * the only way this happens if our hint points to a block
2615 * group thats not of the proper type, while looping this
2616 * should never happen
2617 */
2618 if (empty_size)
2619 extra_loop = 1;
2620 2605
2621 if (!block_group) 2606 atomic_inc(&block_group->count);
2622 goto new_group_no_lock; 2607 search_start = block_group->key.objectid;
2623 2608
2609have_block_group:
2624 if (unlikely(!block_group->cached)) { 2610 if (unlikely(!block_group->cached)) {
2625 mutex_lock(&block_group->cache_mutex); 2611 mutex_lock(&block_group->cache_mutex);
2626 ret = cache_block_group(root, block_group); 2612 ret = cache_block_group(root, block_group);
2627 mutex_unlock(&block_group->cache_mutex); 2613 mutex_unlock(&block_group->cache_mutex);
2628 if (ret) 2614 if (ret) {
2615 btrfs_put_block_group(block_group);
2629 break; 2616 break;
2617 }
2630 } 2618 }
2631 2619
2632 mutex_lock(&block_group->alloc_mutex);
2633 if (unlikely(!block_group_bits(block_group, data)))
2634 goto new_group;
2635
2636 if (unlikely(block_group->ro)) 2620 if (unlikely(block_group->ro))
2637 goto new_group; 2621 goto loop;
2638 2622
2639 free_space = btrfs_find_free_space(block_group, search_start, 2623 if (last_ptr) {
2640 total_needed); 2624 /*
2641 if (free_space) { 2625 * the refill lock keeps out other
2642 u64 start = block_group->key.objectid; 2626 * people trying to start a new cluster
2643 u64 end = block_group->key.objectid + 2627 */
2644 block_group->key.offset; 2628 spin_lock(&last_ptr->refill_lock);
2629 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
2630 num_bytes, search_start);
2631 if (offset) {
2632 /* we have a block, we're done */
2633 spin_unlock(&last_ptr->refill_lock);
2634 goto checks;
2635 }
2645 2636
2646 search_start = stripe_align(root, free_space->offset); 2637 spin_lock(&last_ptr->lock);
2638 /*
2639 * whoops, this cluster doesn't actually point to
2640 * this block group. Get a ref on the block
2641 * group is does point to and try again
2642 */
2643 if (!last_ptr_loop && last_ptr->block_group &&
2644 last_ptr->block_group != block_group) {
2645
2646 btrfs_put_block_group(block_group);
2647 block_group = last_ptr->block_group;
2648 atomic_inc(&block_group->count);
2649 spin_unlock(&last_ptr->lock);
2650 spin_unlock(&last_ptr->refill_lock);
2651
2652 last_ptr_loop = 1;
2653 search_start = block_group->key.objectid;
2654 goto have_block_group;
2655 }
2656 spin_unlock(&last_ptr->lock);
2647 2657
2648 /* move on to the next group */ 2658 /*
2649 if (search_start + num_bytes >= search_end) 2659 * this cluster didn't work out, free it and
2650 goto new_group; 2660 * start over
2661 */
2662 btrfs_return_cluster_to_free_space(NULL, last_ptr);
2651 2663
2652 /* move on to the next group */ 2664 last_ptr_loop = 0;
2653 if (search_start + num_bytes > end)
2654 goto new_group;
2655 2665
2656 if (last_wanted && search_start != last_wanted) { 2666 /* allocate a cluster in this block group */
2657 total_needed += empty_cluster; 2667 ret = btrfs_find_space_cluster(trans,
2658 empty_size += empty_cluster; 2668 block_group, last_ptr,
2659 last_wanted = 0; 2669 offset, num_bytes,
2670 empty_cluster + empty_size);
2671 if (ret == 0) {
2660 /* 2672 /*
2661 * if search_start is still in this block group 2673 * now pull our allocation out of this
2662 * then we just re-search this block group 2674 * cluster
2663 */ 2675 */
2664 if (search_start >= start && 2676 offset = btrfs_alloc_from_cluster(block_group,
2665 search_start < end) { 2677 last_ptr, num_bytes,
2666 mutex_unlock(&block_group->alloc_mutex); 2678 search_start);
2667 continue; 2679 if (offset) {
2680 /* we found one, proceed */
2681 spin_unlock(&last_ptr->refill_lock);
2682 goto checks;
2668 } 2683 }
2669
2670 /* else we go to the next block group */
2671 goto new_group;
2672 } 2684 }
2673 2685 /*
2674 if (exclude_nr > 0 && 2686 * at this point we either didn't find a cluster
2675 (search_start + num_bytes > exclude_start && 2687 * or we weren't able to allocate a block from our
2676 search_start < exclude_start + exclude_nr)) { 2688 * cluster. Free the cluster we've been trying
2677 search_start = exclude_start + exclude_nr; 2689 * to use, and go to the next block group
2678 /* 2690 */
2679 * if search_start is still in this block group 2691 if (loop < 2) {
2680 * then we just re-search this block group 2692 btrfs_return_cluster_to_free_space(NULL,
2681 */ 2693 last_ptr);
2682 if (search_start >= start && 2694 spin_unlock(&last_ptr->refill_lock);
2683 search_start < end) { 2695 goto loop;
2684 mutex_unlock(&block_group->alloc_mutex);
2685 last_wanted = 0;
2686 continue;
2687 }
2688
2689 /* else we go to the next block group */
2690 goto new_group;
2691 } 2696 }
2697 spin_unlock(&last_ptr->refill_lock);
2698 }
2692 2699
2693 ins->objectid = search_start; 2700 offset = btrfs_find_space_for_alloc(block_group, search_start,
2694 ins->offset = num_bytes; 2701 num_bytes, empty_size);
2702 if (!offset)
2703 goto loop;
2704checks:
2705 search_start = stripe_align(root, offset);
2706
2707 /* move on to the next group */
2708 if (search_start + num_bytes >= search_end) {
2709 btrfs_add_free_space(block_group, offset, num_bytes);
2710 goto loop;
2711 }
2695 2712
2696 btrfs_remove_free_space_lock(block_group, search_start, 2713 /* move on to the next group */
2697 num_bytes); 2714 if (search_start + num_bytes >
2698 /* we are all good, lets return */ 2715 block_group->key.objectid + block_group->key.offset) {
2699 mutex_unlock(&block_group->alloc_mutex); 2716 btrfs_add_free_space(block_group, offset, num_bytes);
2700 break; 2717 goto loop;
2701 } 2718 }
2702new_group:
2703 mutex_unlock(&block_group->alloc_mutex);
2704 put_block_group(block_group);
2705 block_group = NULL;
2706new_group_no_lock:
2707 /* don't try to compare new allocations against the
2708 * last allocation any more
2709 */
2710 last_wanted = 0;
2711 2719
2712 /* 2720 if (exclude_nr > 0 &&
2713 * Here's how this works. 2721 (search_start + num_bytes > exclude_start &&
2714 * loop == 0: we were searching a block group via a hint 2722 search_start < exclude_start + exclude_nr)) {
2715 * and didn't find anything, so we start at 2723 search_start = exclude_start + exclude_nr;
2716 * the head of the block groups and keep searching 2724
2717 * loop == 1: we're searching through all of the block groups 2725 btrfs_add_free_space(block_group, offset, num_bytes);
2718 * if we hit the head again we have searched 2726 /*
2719 * all of the block groups for this space and we 2727 * if search_start is still in this block group
2720 * need to try and allocate, if we cant error out. 2728 * then we just re-search this block group
2721 * loop == 2: we allocated more space and are looping through
2722 * all of the block groups again.
2723 */
2724 if (loop == 0) {
2725 head = &space_info->block_groups;
2726 cur = head->next;
2727 loop++;
2728 } else if (loop == 1 && cur == head) {
2729 int keep_going;
2730
2731 /* at this point we give up on the empty_size
2732 * allocations and just try to allocate the min
2733 * space.
2734 *
2735 * The extra_loop field was set if an empty_size
2736 * allocation was attempted above, and if this
2737 * is try we need to try the loop again without
2738 * the additional empty_size.
2739 */ 2729 */
2740 total_needed -= empty_size; 2730 if (search_start >= block_group->key.objectid &&
2741 empty_size = 0; 2731 search_start < (block_group->key.objectid +
2742 keep_going = extra_loop; 2732 block_group->key.offset))
2743 loop++; 2733 goto have_block_group;
2734 goto loop;
2735 }
2744 2736
2745 if (allowed_chunk_alloc && !chunk_alloc_done) { 2737 ins->objectid = search_start;
2746 up_read(&space_info->groups_sem); 2738 ins->offset = num_bytes;
2747 ret = do_chunk_alloc(trans, root, num_bytes + 2739
2748 2 * 1024 * 1024, data, 1); 2740 if (offset < search_start)
2749 down_read(&space_info->groups_sem); 2741 btrfs_add_free_space(block_group, offset,
2750 if (ret < 0) 2742 search_start - offset);
2751 goto loop_check; 2743 BUG_ON(offset > search_start);
2752 head = &space_info->block_groups; 2744
2753 /* 2745 /* we are all good, lets return */
2754 * we've allocated a new chunk, keep 2746 break;
2755 * trying 2747loop:
2756 */ 2748 btrfs_put_block_group(block_group);
2757 keep_going = 1; 2749 }
2758 chunk_alloc_done = 1; 2750 up_read(&space_info->groups_sem);
2759 } else if (!allowed_chunk_alloc) { 2751
2760 space_info->force_alloc = 1; 2752 /* loop == 0, try to find a clustered alloc in every block group
2761 } 2753 * loop == 1, try again after forcing a chunk allocation
2762loop_check: 2754 * loop == 2, set empty_size and empty_cluster to 0 and try again
2763 if (keep_going) { 2755 */
2764 cur = head->next; 2756 if (!ins->objectid && loop < 3 &&
2765 extra_loop = 0; 2757 (empty_size || empty_cluster || allowed_chunk_alloc)) {
2766 } else { 2758 if (loop >= 2) {
2767 break; 2759 empty_size = 0;
2768 } 2760 empty_cluster = 0;
2769 } else if (cur == head) {
2770 break;
2771 } 2761 }
2772 2762
2773 block_group = list_entry(cur, struct btrfs_block_group_cache, 2763 if (allowed_chunk_alloc) {
2774 list); 2764 ret = do_chunk_alloc(trans, root, num_bytes +
2775 atomic_inc(&block_group->count); 2765 2 * 1024 * 1024, data, 1);
2766 allowed_chunk_alloc = 0;
2767 } else {
2768 space_info->force_alloc = 1;
2769 }
2776 2770
2777 search_start = block_group->key.objectid; 2771 if (loop < 3) {
2778 cur = cur->next; 2772 loop++;
2773 goto search;
2774 }
2775 ret = -ENOSPC;
2776 } else if (!ins->objectid) {
2777 ret = -ENOSPC;
2779 } 2778 }
2780 2779
2781 /* we found what we needed */ 2780 /* we found what we needed */
@@ -2783,21 +2782,10 @@ loop_check:
2783 if (!(data & BTRFS_BLOCK_GROUP_DATA)) 2782 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2784 trans->block_group = block_group->key.objectid; 2783 trans->block_group = block_group->key.objectid;
2785 2784
2786 if (last_ptr) 2785 btrfs_put_block_group(block_group);
2787 *last_ptr = ins->objectid + ins->offset;
2788 ret = 0; 2786 ret = 0;
2789 } else if (!ret) {
2790 printk(KERN_ERR "btrfs searching for %llu bytes, "
2791 "num_bytes %llu, loop %d, allowed_alloc %d\n",
2792 (unsigned long long)total_needed,
2793 (unsigned long long)num_bytes,
2794 loop, allowed_chunk_alloc);
2795 ret = -ENOSPC;
2796 } 2787 }
2797 if (block_group)
2798 put_block_group(block_group);
2799 2788
2800 up_read(&space_info->groups_sem);
2801 return ret; 2789 return ret;
2802} 2790}
2803 2791
@@ -2902,7 +2890,7 @@ int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2902 ret = btrfs_discard_extent(root, start, len); 2890 ret = btrfs_discard_extent(root, start, len);
2903 2891
2904 btrfs_add_free_space(cache, start, len); 2892 btrfs_add_free_space(cache, start, len);
2905 put_block_group(cache); 2893 btrfs_put_block_group(cache);
2906 update_reserved_extents(root, start, len, 0); 2894 update_reserved_extents(root, start, len, 0);
2907 2895
2908 return ret; 2896 return ret;
@@ -3040,7 +3028,7 @@ int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
3040 ret = btrfs_remove_free_space(block_group, ins->objectid, 3028 ret = btrfs_remove_free_space(block_group, ins->objectid,
3041 ins->offset); 3029 ins->offset);
3042 BUG_ON(ret); 3030 BUG_ON(ret);
3043 put_block_group(block_group); 3031 btrfs_put_block_group(block_group);
3044 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, 3032 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3045 ref_generation, owner, ins, 1); 3033 ref_generation, owner, ins, 1);
3046 return ret; 3034 return ret;
@@ -5729,7 +5717,7 @@ next:
5729 WARN_ON(block_group->reserved > 0); 5717 WARN_ON(block_group->reserved > 0);
5730 WARN_ON(btrfs_block_group_used(&block_group->item) > 0); 5718 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5731 spin_unlock(&block_group->lock); 5719 spin_unlock(&block_group->lock);
5732 put_block_group(block_group); 5720 btrfs_put_block_group(block_group);
5733 ret = 0; 5721 ret = 0;
5734out: 5722out:
5735 btrfs_free_path(path); 5723 btrfs_free_path(path);
@@ -5856,9 +5844,10 @@ int btrfs_read_block_groups(struct btrfs_root *root)
5856 5844
5857 atomic_set(&cache->count, 1); 5845 atomic_set(&cache->count, 1);
5858 spin_lock_init(&cache->lock); 5846 spin_lock_init(&cache->lock);
5859 mutex_init(&cache->alloc_mutex); 5847 spin_lock_init(&cache->tree_lock);
5860 mutex_init(&cache->cache_mutex); 5848 mutex_init(&cache->cache_mutex);
5861 INIT_LIST_HEAD(&cache->list); 5849 INIT_LIST_HEAD(&cache->list);
5850 INIT_LIST_HEAD(&cache->cluster_list);
5862 read_extent_buffer(leaf, &cache->item, 5851 read_extent_buffer(leaf, &cache->item,
5863 btrfs_item_ptr_offset(leaf, path->slots[0]), 5852 btrfs_item_ptr_offset(leaf, path->slots[0]),
5864 sizeof(cache->item)); 5853 sizeof(cache->item));
@@ -5912,9 +5901,10 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5912 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 5901 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5913 atomic_set(&cache->count, 1); 5902 atomic_set(&cache->count, 1);
5914 spin_lock_init(&cache->lock); 5903 spin_lock_init(&cache->lock);
5915 mutex_init(&cache->alloc_mutex); 5904 spin_lock_init(&cache->tree_lock);
5916 mutex_init(&cache->cache_mutex); 5905 mutex_init(&cache->cache_mutex);
5917 INIT_LIST_HEAD(&cache->list); 5906 INIT_LIST_HEAD(&cache->list);
5907 INIT_LIST_HEAD(&cache->cluster_list);
5918 5908
5919 btrfs_set_block_group_used(&cache->item, bytes_used); 5909 btrfs_set_block_group_used(&cache->item, bytes_used);
5920 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); 5910 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
@@ -5974,8 +5964,8 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5974 spin_unlock(&block_group->space_info->lock); 5964 spin_unlock(&block_group->space_info->lock);
5975 block_group->space_info->full = 0; 5965 block_group->space_info->full = 0;
5976 5966
5977 put_block_group(block_group); 5967 btrfs_put_block_group(block_group);
5978 put_block_group(block_group); 5968 btrfs_put_block_group(block_group);
5979 5969
5980 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 5970 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5981 if (ret > 0) 5971 if (ret > 0)