12e405ad8SJosef Bacik // SPDX-License-Identifier: GPL-2.0 22e405ad8SJosef Bacik 3784352feSDavid Sterba #include "misc.h" 42e405ad8SJosef Bacik #include "ctree.h" 52e405ad8SJosef Bacik #include "block-group.h" 63eeb3226SJosef Bacik #include "space-info.h" 79f21246dSJosef Bacik #include "disk-io.h" 89f21246dSJosef Bacik #include "free-space-cache.h" 99f21246dSJosef Bacik #include "free-space-tree.h" 10e3e0520bSJosef Bacik #include "volumes.h" 11e3e0520bSJosef Bacik #include "transaction.h" 12e3e0520bSJosef Bacik #include "ref-verify.h" 134358d963SJosef Bacik #include "sysfs.h" 144358d963SJosef Bacik #include "tree-log.h" 1577745c05SJosef Bacik #include "delalloc-space.h" 16b0643e59SDennis Zhou #include "discard.h" 1796a14336SNikolay Borisov #include "raid56.h" 182e405ad8SJosef Bacik 19878d7b67SJosef Bacik /* 20878d7b67SJosef Bacik * Return target flags in extended format or 0 if restripe for this chunk_type 21878d7b67SJosef Bacik * is not in progress 22878d7b67SJosef Bacik * 23878d7b67SJosef Bacik * Should be called with balance_lock held 24878d7b67SJosef Bacik */ 25e11c0406SJosef Bacik static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) 26878d7b67SJosef Bacik { 27878d7b67SJosef Bacik struct btrfs_balance_control *bctl = fs_info->balance_ctl; 28878d7b67SJosef Bacik u64 target = 0; 29878d7b67SJosef Bacik 30878d7b67SJosef Bacik if (!bctl) 31878d7b67SJosef Bacik return 0; 32878d7b67SJosef Bacik 33878d7b67SJosef Bacik if (flags & BTRFS_BLOCK_GROUP_DATA && 34878d7b67SJosef Bacik bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { 35878d7b67SJosef Bacik target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; 36878d7b67SJosef Bacik } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && 37878d7b67SJosef Bacik bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { 38878d7b67SJosef Bacik target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; 39878d7b67SJosef Bacik } else if (flags & BTRFS_BLOCK_GROUP_METADATA && 40878d7b67SJosef Bacik bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { 41878d7b67SJosef Bacik target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; 42878d7b67SJosef Bacik } 43878d7b67SJosef Bacik 44878d7b67SJosef Bacik return target; 45878d7b67SJosef Bacik } 46878d7b67SJosef Bacik 47878d7b67SJosef Bacik /* 48878d7b67SJosef Bacik * @flags: available profiles in extended format (see ctree.h) 49878d7b67SJosef Bacik * 50878d7b67SJosef Bacik * Return reduced profile in chunk format. If profile changing is in progress 51878d7b67SJosef Bacik * (either running or paused) picks the target profile (if it's already 52878d7b67SJosef Bacik * available), otherwise falls back to plain reducing. 53878d7b67SJosef Bacik */ 54878d7b67SJosef Bacik static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags) 55878d7b67SJosef Bacik { 56878d7b67SJosef Bacik u64 num_devices = fs_info->fs_devices->rw_devices; 57878d7b67SJosef Bacik u64 target; 58878d7b67SJosef Bacik u64 raid_type; 59878d7b67SJosef Bacik u64 allowed = 0; 60878d7b67SJosef Bacik 61878d7b67SJosef Bacik /* 62878d7b67SJosef Bacik * See if restripe for this chunk_type is in progress, if so try to 63878d7b67SJosef Bacik * reduce to the target profile 64878d7b67SJosef Bacik */ 65878d7b67SJosef Bacik spin_lock(&fs_info->balance_lock); 66e11c0406SJosef Bacik target = get_restripe_target(fs_info, flags); 67878d7b67SJosef Bacik if (target) { 68878d7b67SJosef Bacik spin_unlock(&fs_info->balance_lock); 69878d7b67SJosef Bacik return extended_to_chunk(target); 70878d7b67SJosef Bacik } 71878d7b67SJosef Bacik spin_unlock(&fs_info->balance_lock); 72878d7b67SJosef Bacik 73878d7b67SJosef Bacik /* First, mask out the RAID levels which aren't possible */ 74878d7b67SJosef Bacik for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 75878d7b67SJosef Bacik if (num_devices >= btrfs_raid_array[raid_type].devs_min) 76878d7b67SJosef Bacik allowed |= btrfs_raid_array[raid_type].bg_flag; 77878d7b67SJosef Bacik } 78878d7b67SJosef Bacik allowed &= flags; 79878d7b67SJosef Bacik 80878d7b67SJosef Bacik if (allowed & BTRFS_BLOCK_GROUP_RAID6) 81878d7b67SJosef Bacik allowed = BTRFS_BLOCK_GROUP_RAID6; 82878d7b67SJosef Bacik else if (allowed & BTRFS_BLOCK_GROUP_RAID5) 83878d7b67SJosef Bacik allowed = BTRFS_BLOCK_GROUP_RAID5; 84878d7b67SJosef Bacik else if (allowed & BTRFS_BLOCK_GROUP_RAID10) 85878d7b67SJosef Bacik allowed = BTRFS_BLOCK_GROUP_RAID10; 86878d7b67SJosef Bacik else if (allowed & BTRFS_BLOCK_GROUP_RAID1) 87878d7b67SJosef Bacik allowed = BTRFS_BLOCK_GROUP_RAID1; 88878d7b67SJosef Bacik else if (allowed & BTRFS_BLOCK_GROUP_RAID0) 89878d7b67SJosef Bacik allowed = BTRFS_BLOCK_GROUP_RAID0; 90878d7b67SJosef Bacik 91878d7b67SJosef Bacik flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK; 92878d7b67SJosef Bacik 93878d7b67SJosef Bacik return extended_to_chunk(flags | allowed); 94878d7b67SJosef Bacik } 95878d7b67SJosef Bacik 96ef0a82daSJohannes Thumshirn u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags) 97878d7b67SJosef Bacik { 98878d7b67SJosef Bacik unsigned seq; 99878d7b67SJosef Bacik u64 flags; 100878d7b67SJosef Bacik 101878d7b67SJosef Bacik do { 102878d7b67SJosef Bacik flags = orig_flags; 103878d7b67SJosef Bacik seq = read_seqbegin(&fs_info->profiles_lock); 104878d7b67SJosef Bacik 105878d7b67SJosef Bacik if (flags & BTRFS_BLOCK_GROUP_DATA) 106878d7b67SJosef Bacik flags |= fs_info->avail_data_alloc_bits; 107878d7b67SJosef Bacik else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 108878d7b67SJosef Bacik flags |= fs_info->avail_system_alloc_bits; 109878d7b67SJosef Bacik else if (flags & BTRFS_BLOCK_GROUP_METADATA) 110878d7b67SJosef Bacik flags |= fs_info->avail_metadata_alloc_bits; 111878d7b67SJosef Bacik } while (read_seqretry(&fs_info->profiles_lock, seq)); 112878d7b67SJosef Bacik 113878d7b67SJosef Bacik return btrfs_reduce_alloc_profile(fs_info, flags); 114878d7b67SJosef Bacik } 115878d7b67SJosef Bacik 11632da5386SDavid Sterba void btrfs_get_block_group(struct btrfs_block_group *cache) 1173cad1284SJosef Bacik { 11848aaeebeSJosef Bacik refcount_inc(&cache->refs); 1193cad1284SJosef Bacik } 1203cad1284SJosef Bacik 12132da5386SDavid Sterba void btrfs_put_block_group(struct btrfs_block_group *cache) 1223cad1284SJosef Bacik { 12348aaeebeSJosef Bacik if (refcount_dec_and_test(&cache->refs)) { 1243cad1284SJosef Bacik WARN_ON(cache->pinned > 0); 1253cad1284SJosef Bacik WARN_ON(cache->reserved > 0); 1263cad1284SJosef Bacik 1273cad1284SJosef Bacik /* 128b0643e59SDennis Zhou * A block_group shouldn't be on the discard_list anymore. 129b0643e59SDennis Zhou * Remove the block_group from the discard_list to prevent us 130b0643e59SDennis Zhou * from causing a panic due to NULL pointer dereference. 131b0643e59SDennis Zhou */ 132b0643e59SDennis Zhou if (WARN_ON(!list_empty(&cache->discard_list))) 133b0643e59SDennis Zhou btrfs_discard_cancel_work(&cache->fs_info->discard_ctl, 134b0643e59SDennis Zhou cache); 135b0643e59SDennis Zhou 136b0643e59SDennis Zhou /* 1373cad1284SJosef Bacik * If not empty, someone is still holding mutex of 1383cad1284SJosef Bacik * full_stripe_lock, which can only be released by caller. 1393cad1284SJosef Bacik * And it will definitely cause use-after-free when caller 1403cad1284SJosef Bacik * tries to release full stripe lock. 1413cad1284SJosef Bacik * 1423cad1284SJosef Bacik * No better way to resolve, but only to warn. 1433cad1284SJosef Bacik */ 1443cad1284SJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root)); 1453cad1284SJosef Bacik kfree(cache->free_space_ctl); 1463cad1284SJosef Bacik kfree(cache); 1473cad1284SJosef Bacik } 1483cad1284SJosef Bacik } 1493cad1284SJosef Bacik 1502e405ad8SJosef Bacik /* 1514358d963SJosef Bacik * This adds the block group to the fs_info rb tree for the block group cache 1524358d963SJosef Bacik */ 1534358d963SJosef Bacik static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, 15432da5386SDavid Sterba struct btrfs_block_group *block_group) 1554358d963SJosef Bacik { 1564358d963SJosef Bacik struct rb_node **p; 1574358d963SJosef Bacik struct rb_node *parent = NULL; 15832da5386SDavid Sterba struct btrfs_block_group *cache; 1594358d963SJosef Bacik 1609afc6649SQu Wenruo ASSERT(block_group->length != 0); 1619afc6649SQu Wenruo 1624358d963SJosef Bacik spin_lock(&info->block_group_cache_lock); 1634358d963SJosef Bacik p = &info->block_group_cache_tree.rb_node; 1644358d963SJosef Bacik 1654358d963SJosef Bacik while (*p) { 1664358d963SJosef Bacik parent = *p; 16732da5386SDavid Sterba cache = rb_entry(parent, struct btrfs_block_group, cache_node); 168b3470b5dSDavid Sterba if (block_group->start < cache->start) { 1694358d963SJosef Bacik p = &(*p)->rb_left; 170b3470b5dSDavid Sterba } else if (block_group->start > cache->start) { 1714358d963SJosef Bacik p = &(*p)->rb_right; 1724358d963SJosef Bacik } else { 1734358d963SJosef Bacik spin_unlock(&info->block_group_cache_lock); 1744358d963SJosef Bacik return -EEXIST; 1754358d963SJosef Bacik } 1764358d963SJosef Bacik } 1774358d963SJosef Bacik 1784358d963SJosef Bacik rb_link_node(&block_group->cache_node, parent, p); 1794358d963SJosef Bacik rb_insert_color(&block_group->cache_node, 1804358d963SJosef Bacik &info->block_group_cache_tree); 1814358d963SJosef Bacik 182b3470b5dSDavid Sterba if (info->first_logical_byte > block_group->start) 183b3470b5dSDavid Sterba info->first_logical_byte = block_group->start; 1844358d963SJosef Bacik 1854358d963SJosef Bacik spin_unlock(&info->block_group_cache_lock); 1864358d963SJosef Bacik 1874358d963SJosef Bacik return 0; 1884358d963SJosef Bacik } 1894358d963SJosef Bacik 1904358d963SJosef Bacik /* 1912e405ad8SJosef Bacik * This will return the block group at or after bytenr if contains is 0, else 1922e405ad8SJosef Bacik * it will return the block group that contains the bytenr 1932e405ad8SJosef Bacik */ 19432da5386SDavid Sterba static struct btrfs_block_group *block_group_cache_tree_search( 1952e405ad8SJosef Bacik struct btrfs_fs_info *info, u64 bytenr, int contains) 1962e405ad8SJosef Bacik { 19732da5386SDavid Sterba struct btrfs_block_group *cache, *ret = NULL; 1982e405ad8SJosef Bacik struct rb_node *n; 1992e405ad8SJosef Bacik u64 end, start; 2002e405ad8SJosef Bacik 2012e405ad8SJosef Bacik spin_lock(&info->block_group_cache_lock); 2022e405ad8SJosef Bacik n = info->block_group_cache_tree.rb_node; 2032e405ad8SJosef Bacik 2042e405ad8SJosef Bacik while (n) { 20532da5386SDavid Sterba cache = rb_entry(n, struct btrfs_block_group, cache_node); 206b3470b5dSDavid Sterba end = cache->start + cache->length - 1; 207b3470b5dSDavid Sterba start = cache->start; 2082e405ad8SJosef Bacik 2092e405ad8SJosef Bacik if (bytenr < start) { 210b3470b5dSDavid Sterba if (!contains && (!ret || start < ret->start)) 2112e405ad8SJosef Bacik ret = cache; 2122e405ad8SJosef Bacik n = n->rb_left; 2132e405ad8SJosef Bacik } else if (bytenr > start) { 2142e405ad8SJosef Bacik if (contains && bytenr <= end) { 2152e405ad8SJosef Bacik ret = cache; 2162e405ad8SJosef Bacik break; 2172e405ad8SJosef Bacik } 2182e405ad8SJosef Bacik n = n->rb_right; 2192e405ad8SJosef Bacik } else { 2202e405ad8SJosef Bacik ret = cache; 2212e405ad8SJosef Bacik break; 2222e405ad8SJosef Bacik } 2232e405ad8SJosef Bacik } 2242e405ad8SJosef Bacik if (ret) { 2252e405ad8SJosef Bacik btrfs_get_block_group(ret); 226b3470b5dSDavid Sterba if (bytenr == 0 && info->first_logical_byte > ret->start) 227b3470b5dSDavid Sterba info->first_logical_byte = ret->start; 2282e405ad8SJosef Bacik } 2292e405ad8SJosef Bacik spin_unlock(&info->block_group_cache_lock); 2302e405ad8SJosef Bacik 2312e405ad8SJosef Bacik return ret; 2322e405ad8SJosef Bacik } 2332e405ad8SJosef Bacik 2342e405ad8SJosef Bacik /* 2352e405ad8SJosef Bacik * Return the block group that starts at or after bytenr 2362e405ad8SJosef Bacik */ 23732da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_first_block_group( 2382e405ad8SJosef Bacik struct btrfs_fs_info *info, u64 bytenr) 2392e405ad8SJosef Bacik { 2402e405ad8SJosef Bacik return block_group_cache_tree_search(info, bytenr, 0); 2412e405ad8SJosef Bacik } 2422e405ad8SJosef Bacik 2432e405ad8SJosef Bacik /* 2442e405ad8SJosef Bacik * Return the block group that contains the given bytenr 2452e405ad8SJosef Bacik */ 24632da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_block_group( 2472e405ad8SJosef Bacik struct btrfs_fs_info *info, u64 bytenr) 2482e405ad8SJosef Bacik { 2492e405ad8SJosef Bacik return block_group_cache_tree_search(info, bytenr, 1); 2502e405ad8SJosef Bacik } 2512e405ad8SJosef Bacik 25232da5386SDavid Sterba struct btrfs_block_group *btrfs_next_block_group( 25332da5386SDavid Sterba struct btrfs_block_group *cache) 2542e405ad8SJosef Bacik { 2552e405ad8SJosef Bacik struct btrfs_fs_info *fs_info = cache->fs_info; 2562e405ad8SJosef Bacik struct rb_node *node; 2572e405ad8SJosef Bacik 2582e405ad8SJosef Bacik spin_lock(&fs_info->block_group_cache_lock); 2592e405ad8SJosef Bacik 2602e405ad8SJosef Bacik /* If our block group was removed, we need a full search. */ 2612e405ad8SJosef Bacik if (RB_EMPTY_NODE(&cache->cache_node)) { 262b3470b5dSDavid Sterba const u64 next_bytenr = cache->start + cache->length; 2632e405ad8SJosef Bacik 2642e405ad8SJosef Bacik spin_unlock(&fs_info->block_group_cache_lock); 2652e405ad8SJosef Bacik btrfs_put_block_group(cache); 2662e405ad8SJosef Bacik cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache; 2672e405ad8SJosef Bacik } 2682e405ad8SJosef Bacik node = rb_next(&cache->cache_node); 2692e405ad8SJosef Bacik btrfs_put_block_group(cache); 2702e405ad8SJosef Bacik if (node) { 27132da5386SDavid Sterba cache = rb_entry(node, struct btrfs_block_group, cache_node); 2722e405ad8SJosef Bacik btrfs_get_block_group(cache); 2732e405ad8SJosef Bacik } else 2742e405ad8SJosef Bacik cache = NULL; 2752e405ad8SJosef Bacik spin_unlock(&fs_info->block_group_cache_lock); 2762e405ad8SJosef Bacik return cache; 2772e405ad8SJosef Bacik } 2783eeb3226SJosef Bacik 2793eeb3226SJosef Bacik bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) 2803eeb3226SJosef Bacik { 28132da5386SDavid Sterba struct btrfs_block_group *bg; 2823eeb3226SJosef Bacik bool ret = true; 2833eeb3226SJosef Bacik 2843eeb3226SJosef Bacik bg = btrfs_lookup_block_group(fs_info, bytenr); 2853eeb3226SJosef Bacik if (!bg) 2863eeb3226SJosef Bacik return false; 2873eeb3226SJosef Bacik 2883eeb3226SJosef Bacik spin_lock(&bg->lock); 2893eeb3226SJosef Bacik if (bg->ro) 2903eeb3226SJosef Bacik ret = false; 2913eeb3226SJosef Bacik else 2923eeb3226SJosef Bacik atomic_inc(&bg->nocow_writers); 2933eeb3226SJosef Bacik spin_unlock(&bg->lock); 2943eeb3226SJosef Bacik 2953eeb3226SJosef Bacik /* No put on block group, done by btrfs_dec_nocow_writers */ 2963eeb3226SJosef Bacik if (!ret) 2973eeb3226SJosef Bacik btrfs_put_block_group(bg); 2983eeb3226SJosef Bacik 2993eeb3226SJosef Bacik return ret; 3003eeb3226SJosef Bacik } 3013eeb3226SJosef Bacik 3023eeb3226SJosef Bacik void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) 3033eeb3226SJosef Bacik { 30432da5386SDavid Sterba struct btrfs_block_group *bg; 3053eeb3226SJosef Bacik 3063eeb3226SJosef Bacik bg = btrfs_lookup_block_group(fs_info, bytenr); 3073eeb3226SJosef Bacik ASSERT(bg); 3083eeb3226SJosef Bacik if (atomic_dec_and_test(&bg->nocow_writers)) 3093eeb3226SJosef Bacik wake_up_var(&bg->nocow_writers); 3103eeb3226SJosef Bacik /* 3113eeb3226SJosef Bacik * Once for our lookup and once for the lookup done by a previous call 3123eeb3226SJosef Bacik * to btrfs_inc_nocow_writers() 3133eeb3226SJosef Bacik */ 3143eeb3226SJosef Bacik btrfs_put_block_group(bg); 3153eeb3226SJosef Bacik btrfs_put_block_group(bg); 3163eeb3226SJosef Bacik } 3173eeb3226SJosef Bacik 31832da5386SDavid Sterba void btrfs_wait_nocow_writers(struct btrfs_block_group *bg) 3193eeb3226SJosef Bacik { 3203eeb3226SJosef Bacik wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers)); 3213eeb3226SJosef Bacik } 3223eeb3226SJosef Bacik 3233eeb3226SJosef Bacik void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, 3243eeb3226SJosef Bacik const u64 start) 3253eeb3226SJosef Bacik { 32632da5386SDavid Sterba struct btrfs_block_group *bg; 3273eeb3226SJosef Bacik 3283eeb3226SJosef Bacik bg = btrfs_lookup_block_group(fs_info, start); 3293eeb3226SJosef Bacik ASSERT(bg); 3303eeb3226SJosef Bacik if (atomic_dec_and_test(&bg->reservations)) 3313eeb3226SJosef Bacik wake_up_var(&bg->reservations); 3323eeb3226SJosef Bacik btrfs_put_block_group(bg); 3333eeb3226SJosef Bacik } 3343eeb3226SJosef Bacik 33532da5386SDavid Sterba void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg) 3363eeb3226SJosef Bacik { 3373eeb3226SJosef Bacik struct btrfs_space_info *space_info = bg->space_info; 3383eeb3226SJosef Bacik 3393eeb3226SJosef Bacik ASSERT(bg->ro); 3403eeb3226SJosef Bacik 3413eeb3226SJosef Bacik if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA)) 3423eeb3226SJosef Bacik return; 3433eeb3226SJosef Bacik 3443eeb3226SJosef Bacik /* 3453eeb3226SJosef Bacik * Our block group is read only but before we set it to read only, 3463eeb3226SJosef Bacik * some task might have had allocated an extent from it already, but it 3473eeb3226SJosef Bacik * has not yet created a respective ordered extent (and added it to a 3483eeb3226SJosef Bacik * root's list of ordered extents). 3493eeb3226SJosef Bacik * Therefore wait for any task currently allocating extents, since the 3503eeb3226SJosef Bacik * block group's reservations counter is incremented while a read lock 3513eeb3226SJosef Bacik * on the groups' semaphore is held and decremented after releasing 3523eeb3226SJosef Bacik * the read access on that semaphore and creating the ordered extent. 3533eeb3226SJosef Bacik */ 3543eeb3226SJosef Bacik down_write(&space_info->groups_sem); 3553eeb3226SJosef Bacik up_write(&space_info->groups_sem); 3563eeb3226SJosef Bacik 3573eeb3226SJosef Bacik wait_var_event(&bg->reservations, !atomic_read(&bg->reservations)); 3583eeb3226SJosef Bacik } 3599f21246dSJosef Bacik 3609f21246dSJosef Bacik struct btrfs_caching_control *btrfs_get_caching_control( 36132da5386SDavid Sterba struct btrfs_block_group *cache) 3629f21246dSJosef Bacik { 3639f21246dSJosef Bacik struct btrfs_caching_control *ctl; 3649f21246dSJosef Bacik 3659f21246dSJosef Bacik spin_lock(&cache->lock); 3669f21246dSJosef Bacik if (!cache->caching_ctl) { 3679f21246dSJosef Bacik spin_unlock(&cache->lock); 3689f21246dSJosef Bacik return NULL; 3699f21246dSJosef Bacik } 3709f21246dSJosef Bacik 3719f21246dSJosef Bacik ctl = cache->caching_ctl; 3729f21246dSJosef Bacik refcount_inc(&ctl->count); 3739f21246dSJosef Bacik spin_unlock(&cache->lock); 3749f21246dSJosef Bacik return ctl; 3759f21246dSJosef Bacik } 3769f21246dSJosef Bacik 3779f21246dSJosef Bacik void btrfs_put_caching_control(struct btrfs_caching_control *ctl) 3789f21246dSJosef Bacik { 3799f21246dSJosef Bacik if (refcount_dec_and_test(&ctl->count)) 3809f21246dSJosef Bacik kfree(ctl); 3819f21246dSJosef Bacik } 3829f21246dSJosef Bacik 3839f21246dSJosef Bacik /* 3849f21246dSJosef Bacik * When we wait for progress in the block group caching, its because our 3859f21246dSJosef Bacik * allocation attempt failed at least once. So, we must sleep and let some 3869f21246dSJosef Bacik * progress happen before we try again. 3879f21246dSJosef Bacik * 3889f21246dSJosef Bacik * This function will sleep at least once waiting for new free space to show 3899f21246dSJosef Bacik * up, and then it will check the block group free space numbers for our min 3909f21246dSJosef Bacik * num_bytes. Another option is to have it go ahead and look in the rbtree for 3919f21246dSJosef Bacik * a free extent of a given size, but this is a good start. 3929f21246dSJosef Bacik * 3939f21246dSJosef Bacik * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using 3949f21246dSJosef Bacik * any of the information in this block group. 3959f21246dSJosef Bacik */ 39632da5386SDavid Sterba void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache, 3979f21246dSJosef Bacik u64 num_bytes) 3989f21246dSJosef Bacik { 3999f21246dSJosef Bacik struct btrfs_caching_control *caching_ctl; 4009f21246dSJosef Bacik 4019f21246dSJosef Bacik caching_ctl = btrfs_get_caching_control(cache); 4029f21246dSJosef Bacik if (!caching_ctl) 4039f21246dSJosef Bacik return; 4049f21246dSJosef Bacik 40532da5386SDavid Sterba wait_event(caching_ctl->wait, btrfs_block_group_done(cache) || 4069f21246dSJosef Bacik (cache->free_space_ctl->free_space >= num_bytes)); 4079f21246dSJosef Bacik 4089f21246dSJosef Bacik btrfs_put_caching_control(caching_ctl); 4099f21246dSJosef Bacik } 4109f21246dSJosef Bacik 41132da5386SDavid Sterba int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache) 4129f21246dSJosef Bacik { 4139f21246dSJosef Bacik struct btrfs_caching_control *caching_ctl; 4149f21246dSJosef Bacik int ret = 0; 4159f21246dSJosef Bacik 4169f21246dSJosef Bacik caching_ctl = btrfs_get_caching_control(cache); 4179f21246dSJosef Bacik if (!caching_ctl) 4189f21246dSJosef Bacik return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; 4199f21246dSJosef Bacik 42032da5386SDavid Sterba wait_event(caching_ctl->wait, btrfs_block_group_done(cache)); 4219f21246dSJosef Bacik if (cache->cached == BTRFS_CACHE_ERROR) 4229f21246dSJosef Bacik ret = -EIO; 4239f21246dSJosef Bacik btrfs_put_caching_control(caching_ctl); 4249f21246dSJosef Bacik return ret; 4259f21246dSJosef Bacik } 4269f21246dSJosef Bacik 427e747853cSJosef Bacik static bool space_cache_v1_done(struct btrfs_block_group *cache) 428e747853cSJosef Bacik { 429e747853cSJosef Bacik bool ret; 430e747853cSJosef Bacik 431e747853cSJosef Bacik spin_lock(&cache->lock); 432e747853cSJosef Bacik ret = cache->cached != BTRFS_CACHE_FAST; 433e747853cSJosef Bacik spin_unlock(&cache->lock); 434e747853cSJosef Bacik 435e747853cSJosef Bacik return ret; 436e747853cSJosef Bacik } 437e747853cSJosef Bacik 438e747853cSJosef Bacik void btrfs_wait_space_cache_v1_finished(struct btrfs_block_group *cache, 439e747853cSJosef Bacik struct btrfs_caching_control *caching_ctl) 440e747853cSJosef Bacik { 441e747853cSJosef Bacik wait_event(caching_ctl->wait, space_cache_v1_done(cache)); 442e747853cSJosef Bacik } 443e747853cSJosef Bacik 4449f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 44532da5386SDavid Sterba static void fragment_free_space(struct btrfs_block_group *block_group) 4469f21246dSJosef Bacik { 4479f21246dSJosef Bacik struct btrfs_fs_info *fs_info = block_group->fs_info; 448b3470b5dSDavid Sterba u64 start = block_group->start; 449b3470b5dSDavid Sterba u64 len = block_group->length; 4509f21246dSJosef Bacik u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? 4519f21246dSJosef Bacik fs_info->nodesize : fs_info->sectorsize; 4529f21246dSJosef Bacik u64 step = chunk << 1; 4539f21246dSJosef Bacik 4549f21246dSJosef Bacik while (len > chunk) { 4559f21246dSJosef Bacik btrfs_remove_free_space(block_group, start, chunk); 4569f21246dSJosef Bacik start += step; 4579f21246dSJosef Bacik if (len < step) 4589f21246dSJosef Bacik len = 0; 4599f21246dSJosef Bacik else 4609f21246dSJosef Bacik len -= step; 4619f21246dSJosef Bacik } 4629f21246dSJosef Bacik } 4639f21246dSJosef Bacik #endif 4649f21246dSJosef Bacik 4659f21246dSJosef Bacik /* 4669f21246dSJosef Bacik * This is only called by btrfs_cache_block_group, since we could have freed 4679f21246dSJosef Bacik * extents we need to check the pinned_extents for any extents that can't be 4689f21246dSJosef Bacik * used yet since their free space will be released as soon as the transaction 4699f21246dSJosef Bacik * commits. 4709f21246dSJosef Bacik */ 47132da5386SDavid Sterba u64 add_new_free_space(struct btrfs_block_group *block_group, u64 start, u64 end) 4729f21246dSJosef Bacik { 4739f21246dSJosef Bacik struct btrfs_fs_info *info = block_group->fs_info; 4749f21246dSJosef Bacik u64 extent_start, extent_end, size, total_added = 0; 4759f21246dSJosef Bacik int ret; 4769f21246dSJosef Bacik 4779f21246dSJosef Bacik while (start < end) { 478fe119a6eSNikolay Borisov ret = find_first_extent_bit(&info->excluded_extents, start, 4799f21246dSJosef Bacik &extent_start, &extent_end, 4809f21246dSJosef Bacik EXTENT_DIRTY | EXTENT_UPTODATE, 4819f21246dSJosef Bacik NULL); 4829f21246dSJosef Bacik if (ret) 4839f21246dSJosef Bacik break; 4849f21246dSJosef Bacik 4859f21246dSJosef Bacik if (extent_start <= start) { 4869f21246dSJosef Bacik start = extent_end + 1; 4879f21246dSJosef Bacik } else if (extent_start > start && extent_start < end) { 4889f21246dSJosef Bacik size = extent_start - start; 4899f21246dSJosef Bacik total_added += size; 490b0643e59SDennis Zhou ret = btrfs_add_free_space_async_trimmed(block_group, 491b0643e59SDennis Zhou start, size); 4929f21246dSJosef Bacik BUG_ON(ret); /* -ENOMEM or logic error */ 4939f21246dSJosef Bacik start = extent_end + 1; 4949f21246dSJosef Bacik } else { 4959f21246dSJosef Bacik break; 4969f21246dSJosef Bacik } 4979f21246dSJosef Bacik } 4989f21246dSJosef Bacik 4999f21246dSJosef Bacik if (start < end) { 5009f21246dSJosef Bacik size = end - start; 5019f21246dSJosef Bacik total_added += size; 502b0643e59SDennis Zhou ret = btrfs_add_free_space_async_trimmed(block_group, start, 503b0643e59SDennis Zhou size); 5049f21246dSJosef Bacik BUG_ON(ret); /* -ENOMEM or logic error */ 5059f21246dSJosef Bacik } 5069f21246dSJosef Bacik 5079f21246dSJosef Bacik return total_added; 5089f21246dSJosef Bacik } 5099f21246dSJosef Bacik 5109f21246dSJosef Bacik static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl) 5119f21246dSJosef Bacik { 51232da5386SDavid Sterba struct btrfs_block_group *block_group = caching_ctl->block_group; 5139f21246dSJosef Bacik struct btrfs_fs_info *fs_info = block_group->fs_info; 5149f21246dSJosef Bacik struct btrfs_root *extent_root = fs_info->extent_root; 5159f21246dSJosef Bacik struct btrfs_path *path; 5169f21246dSJosef Bacik struct extent_buffer *leaf; 5179f21246dSJosef Bacik struct btrfs_key key; 5189f21246dSJosef Bacik u64 total_found = 0; 5199f21246dSJosef Bacik u64 last = 0; 5209f21246dSJosef Bacik u32 nritems; 5219f21246dSJosef Bacik int ret; 5229f21246dSJosef Bacik bool wakeup = true; 5239f21246dSJosef Bacik 5249f21246dSJosef Bacik path = btrfs_alloc_path(); 5259f21246dSJosef Bacik if (!path) 5269f21246dSJosef Bacik return -ENOMEM; 5279f21246dSJosef Bacik 528b3470b5dSDavid Sterba last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET); 5299f21246dSJosef Bacik 5309f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 5319f21246dSJosef Bacik /* 5329f21246dSJosef Bacik * If we're fragmenting we don't want to make anybody think we can 5339f21246dSJosef Bacik * allocate from this block group until we've had a chance to fragment 5349f21246dSJosef Bacik * the free space. 5359f21246dSJosef Bacik */ 5369f21246dSJosef Bacik if (btrfs_should_fragment_free_space(block_group)) 5379f21246dSJosef Bacik wakeup = false; 5389f21246dSJosef Bacik #endif 5399f21246dSJosef Bacik /* 5409f21246dSJosef Bacik * We don't want to deadlock with somebody trying to allocate a new 5419f21246dSJosef Bacik * extent for the extent root while also trying to search the extent 5429f21246dSJosef Bacik * root to add free space. So we skip locking and search the commit 5439f21246dSJosef Bacik * root, since its read-only 5449f21246dSJosef Bacik */ 5459f21246dSJosef Bacik path->skip_locking = 1; 5469f21246dSJosef Bacik path->search_commit_root = 1; 5479f21246dSJosef Bacik path->reada = READA_FORWARD; 5489f21246dSJosef Bacik 5499f21246dSJosef Bacik key.objectid = last; 5509f21246dSJosef Bacik key.offset = 0; 5519f21246dSJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 5529f21246dSJosef Bacik 5539f21246dSJosef Bacik next: 5549f21246dSJosef Bacik ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 5559f21246dSJosef Bacik if (ret < 0) 5569f21246dSJosef Bacik goto out; 5579f21246dSJosef Bacik 5589f21246dSJosef Bacik leaf = path->nodes[0]; 5599f21246dSJosef Bacik nritems = btrfs_header_nritems(leaf); 5609f21246dSJosef Bacik 5619f21246dSJosef Bacik while (1) { 5629f21246dSJosef Bacik if (btrfs_fs_closing(fs_info) > 1) { 5639f21246dSJosef Bacik last = (u64)-1; 5649f21246dSJosef Bacik break; 5659f21246dSJosef Bacik } 5669f21246dSJosef Bacik 5679f21246dSJosef Bacik if (path->slots[0] < nritems) { 5689f21246dSJosef Bacik btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 5699f21246dSJosef Bacik } else { 5709f21246dSJosef Bacik ret = btrfs_find_next_key(extent_root, path, &key, 0, 0); 5719f21246dSJosef Bacik if (ret) 5729f21246dSJosef Bacik break; 5739f21246dSJosef Bacik 5749f21246dSJosef Bacik if (need_resched() || 5759f21246dSJosef Bacik rwsem_is_contended(&fs_info->commit_root_sem)) { 5769f21246dSJosef Bacik if (wakeup) 5779f21246dSJosef Bacik caching_ctl->progress = last; 5789f21246dSJosef Bacik btrfs_release_path(path); 5799f21246dSJosef Bacik up_read(&fs_info->commit_root_sem); 5809f21246dSJosef Bacik mutex_unlock(&caching_ctl->mutex); 5819f21246dSJosef Bacik cond_resched(); 5829f21246dSJosef Bacik mutex_lock(&caching_ctl->mutex); 5839f21246dSJosef Bacik down_read(&fs_info->commit_root_sem); 5849f21246dSJosef Bacik goto next; 5859f21246dSJosef Bacik } 5869f21246dSJosef Bacik 5879f21246dSJosef Bacik ret = btrfs_next_leaf(extent_root, path); 5889f21246dSJosef Bacik if (ret < 0) 5899f21246dSJosef Bacik goto out; 5909f21246dSJosef Bacik if (ret) 5919f21246dSJosef Bacik break; 5929f21246dSJosef Bacik leaf = path->nodes[0]; 5939f21246dSJosef Bacik nritems = btrfs_header_nritems(leaf); 5949f21246dSJosef Bacik continue; 5959f21246dSJosef Bacik } 5969f21246dSJosef Bacik 5979f21246dSJosef Bacik if (key.objectid < last) { 5989f21246dSJosef Bacik key.objectid = last; 5999f21246dSJosef Bacik key.offset = 0; 6009f21246dSJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 6019f21246dSJosef Bacik 6029f21246dSJosef Bacik if (wakeup) 6039f21246dSJosef Bacik caching_ctl->progress = last; 6049f21246dSJosef Bacik btrfs_release_path(path); 6059f21246dSJosef Bacik goto next; 6069f21246dSJosef Bacik } 6079f21246dSJosef Bacik 608b3470b5dSDavid Sterba if (key.objectid < block_group->start) { 6099f21246dSJosef Bacik path->slots[0]++; 6109f21246dSJosef Bacik continue; 6119f21246dSJosef Bacik } 6129f21246dSJosef Bacik 613b3470b5dSDavid Sterba if (key.objectid >= block_group->start + block_group->length) 6149f21246dSJosef Bacik break; 6159f21246dSJosef Bacik 6169f21246dSJosef Bacik if (key.type == BTRFS_EXTENT_ITEM_KEY || 6179f21246dSJosef Bacik key.type == BTRFS_METADATA_ITEM_KEY) { 6189f21246dSJosef Bacik total_found += add_new_free_space(block_group, last, 6199f21246dSJosef Bacik key.objectid); 6209f21246dSJosef Bacik if (key.type == BTRFS_METADATA_ITEM_KEY) 6219f21246dSJosef Bacik last = key.objectid + 6229f21246dSJosef Bacik fs_info->nodesize; 6239f21246dSJosef Bacik else 6249f21246dSJosef Bacik last = key.objectid + key.offset; 6259f21246dSJosef Bacik 6269f21246dSJosef Bacik if (total_found > CACHING_CTL_WAKE_UP) { 6279f21246dSJosef Bacik total_found = 0; 6289f21246dSJosef Bacik if (wakeup) 6299f21246dSJosef Bacik wake_up(&caching_ctl->wait); 6309f21246dSJosef Bacik } 6319f21246dSJosef Bacik } 6329f21246dSJosef Bacik path->slots[0]++; 6339f21246dSJosef Bacik } 6349f21246dSJosef Bacik ret = 0; 6359f21246dSJosef Bacik 6369f21246dSJosef Bacik total_found += add_new_free_space(block_group, last, 637b3470b5dSDavid Sterba block_group->start + block_group->length); 6389f21246dSJosef Bacik caching_ctl->progress = (u64)-1; 6399f21246dSJosef Bacik 6409f21246dSJosef Bacik out: 6419f21246dSJosef Bacik btrfs_free_path(path); 6429f21246dSJosef Bacik return ret; 6439f21246dSJosef Bacik } 6449f21246dSJosef Bacik 6459f21246dSJosef Bacik static noinline void caching_thread(struct btrfs_work *work) 6469f21246dSJosef Bacik { 64732da5386SDavid Sterba struct btrfs_block_group *block_group; 6489f21246dSJosef Bacik struct btrfs_fs_info *fs_info; 6499f21246dSJosef Bacik struct btrfs_caching_control *caching_ctl; 6509f21246dSJosef Bacik int ret; 6519f21246dSJosef Bacik 6529f21246dSJosef Bacik caching_ctl = container_of(work, struct btrfs_caching_control, work); 6539f21246dSJosef Bacik block_group = caching_ctl->block_group; 6549f21246dSJosef Bacik fs_info = block_group->fs_info; 6559f21246dSJosef Bacik 6569f21246dSJosef Bacik mutex_lock(&caching_ctl->mutex); 6579f21246dSJosef Bacik down_read(&fs_info->commit_root_sem); 6589f21246dSJosef Bacik 659e747853cSJosef Bacik if (btrfs_test_opt(fs_info, SPACE_CACHE)) { 660e747853cSJosef Bacik ret = load_free_space_cache(block_group); 661e747853cSJosef Bacik if (ret == 1) { 662e747853cSJosef Bacik ret = 0; 663e747853cSJosef Bacik goto done; 664e747853cSJosef Bacik } 665e747853cSJosef Bacik 666e747853cSJosef Bacik /* 667e747853cSJosef Bacik * We failed to load the space cache, set ourselves to 668e747853cSJosef Bacik * CACHE_STARTED and carry on. 669e747853cSJosef Bacik */ 670e747853cSJosef Bacik spin_lock(&block_group->lock); 671e747853cSJosef Bacik block_group->cached = BTRFS_CACHE_STARTED; 672e747853cSJosef Bacik spin_unlock(&block_group->lock); 673e747853cSJosef Bacik wake_up(&caching_ctl->wait); 674e747853cSJosef Bacik } 675e747853cSJosef Bacik 6769f21246dSJosef Bacik if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) 6779f21246dSJosef Bacik ret = load_free_space_tree(caching_ctl); 6789f21246dSJosef Bacik else 6799f21246dSJosef Bacik ret = load_extent_tree_free(caching_ctl); 680e747853cSJosef Bacik done: 6819f21246dSJosef Bacik spin_lock(&block_group->lock); 6829f21246dSJosef Bacik block_group->caching_ctl = NULL; 6839f21246dSJosef Bacik block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; 6849f21246dSJosef Bacik spin_unlock(&block_group->lock); 6859f21246dSJosef Bacik 6869f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 6879f21246dSJosef Bacik if (btrfs_should_fragment_free_space(block_group)) { 6889f21246dSJosef Bacik u64 bytes_used; 6899f21246dSJosef Bacik 6909f21246dSJosef Bacik spin_lock(&block_group->space_info->lock); 6919f21246dSJosef Bacik spin_lock(&block_group->lock); 692b3470b5dSDavid Sterba bytes_used = block_group->length - block_group->used; 6939f21246dSJosef Bacik block_group->space_info->bytes_used += bytes_used >> 1; 6949f21246dSJosef Bacik spin_unlock(&block_group->lock); 6959f21246dSJosef Bacik spin_unlock(&block_group->space_info->lock); 696e11c0406SJosef Bacik fragment_free_space(block_group); 6979f21246dSJosef Bacik } 6989f21246dSJosef Bacik #endif 6999f21246dSJosef Bacik 7009f21246dSJosef Bacik caching_ctl->progress = (u64)-1; 7019f21246dSJosef Bacik 7029f21246dSJosef Bacik up_read(&fs_info->commit_root_sem); 7039f21246dSJosef Bacik btrfs_free_excluded_extents(block_group); 7049f21246dSJosef Bacik mutex_unlock(&caching_ctl->mutex); 7059f21246dSJosef Bacik 7069f21246dSJosef Bacik wake_up(&caching_ctl->wait); 7079f21246dSJosef Bacik 7089f21246dSJosef Bacik btrfs_put_caching_control(caching_ctl); 7099f21246dSJosef Bacik btrfs_put_block_group(block_group); 7109f21246dSJosef Bacik } 7119f21246dSJosef Bacik 71232da5386SDavid Sterba int btrfs_cache_block_group(struct btrfs_block_group *cache, int load_cache_only) 7139f21246dSJosef Bacik { 7149f21246dSJosef Bacik DEFINE_WAIT(wait); 7159f21246dSJosef Bacik struct btrfs_fs_info *fs_info = cache->fs_info; 716e747853cSJosef Bacik struct btrfs_caching_control *caching_ctl = NULL; 7179f21246dSJosef Bacik int ret = 0; 7189f21246dSJosef Bacik 7199f21246dSJosef Bacik caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); 7209f21246dSJosef Bacik if (!caching_ctl) 7219f21246dSJosef Bacik return -ENOMEM; 7229f21246dSJosef Bacik 7239f21246dSJosef Bacik INIT_LIST_HEAD(&caching_ctl->list); 7249f21246dSJosef Bacik mutex_init(&caching_ctl->mutex); 7259f21246dSJosef Bacik init_waitqueue_head(&caching_ctl->wait); 7269f21246dSJosef Bacik caching_ctl->block_group = cache; 727b3470b5dSDavid Sterba caching_ctl->progress = cache->start; 728e747853cSJosef Bacik refcount_set(&caching_ctl->count, 2); 729a0cac0ecSOmar Sandoval btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL); 7309f21246dSJosef Bacik 7319f21246dSJosef Bacik spin_lock(&cache->lock); 7329f21246dSJosef Bacik if (cache->cached != BTRFS_CACHE_NO) { 7339f21246dSJosef Bacik kfree(caching_ctl); 734e747853cSJosef Bacik 735e747853cSJosef Bacik caching_ctl = cache->caching_ctl; 736e747853cSJosef Bacik if (caching_ctl) 737e747853cSJosef Bacik refcount_inc(&caching_ctl->count); 738e747853cSJosef Bacik spin_unlock(&cache->lock); 739e747853cSJosef Bacik goto out; 7409f21246dSJosef Bacik } 7419f21246dSJosef Bacik WARN_ON(cache->caching_ctl); 7429f21246dSJosef Bacik cache->caching_ctl = caching_ctl; 743e747853cSJosef Bacik if (btrfs_test_opt(fs_info, SPACE_CACHE)) 7449f21246dSJosef Bacik cache->cached = BTRFS_CACHE_FAST; 745e747853cSJosef Bacik else 7469f21246dSJosef Bacik cache->cached = BTRFS_CACHE_STARTED; 7479f21246dSJosef Bacik cache->has_caching_ctl = 1; 7489f21246dSJosef Bacik spin_unlock(&cache->lock); 7499f21246dSJosef Bacik 750bbb86a37SJosef Bacik spin_lock(&fs_info->block_group_cache_lock); 7519f21246dSJosef Bacik refcount_inc(&caching_ctl->count); 7529f21246dSJosef Bacik list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); 753bbb86a37SJosef Bacik spin_unlock(&fs_info->block_group_cache_lock); 7549f21246dSJosef Bacik 7559f21246dSJosef Bacik btrfs_get_block_group(cache); 7569f21246dSJosef Bacik 7579f21246dSJosef Bacik btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); 758e747853cSJosef Bacik out: 759e747853cSJosef Bacik if (load_cache_only && caching_ctl) 760e747853cSJosef Bacik btrfs_wait_space_cache_v1_finished(cache, caching_ctl); 761e747853cSJosef Bacik if (caching_ctl) 762e747853cSJosef Bacik btrfs_put_caching_control(caching_ctl); 7639f21246dSJosef Bacik 7649f21246dSJosef Bacik return ret; 7659f21246dSJosef Bacik } 766e3e0520bSJosef Bacik 767e3e0520bSJosef Bacik static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) 768e3e0520bSJosef Bacik { 769e3e0520bSJosef Bacik u64 extra_flags = chunk_to_extended(flags) & 770e3e0520bSJosef Bacik BTRFS_EXTENDED_PROFILE_MASK; 771e3e0520bSJosef Bacik 772e3e0520bSJosef Bacik write_seqlock(&fs_info->profiles_lock); 773e3e0520bSJosef Bacik if (flags & BTRFS_BLOCK_GROUP_DATA) 774e3e0520bSJosef Bacik fs_info->avail_data_alloc_bits &= ~extra_flags; 775e3e0520bSJosef Bacik if (flags & BTRFS_BLOCK_GROUP_METADATA) 776e3e0520bSJosef Bacik fs_info->avail_metadata_alloc_bits &= ~extra_flags; 777e3e0520bSJosef Bacik if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 778e3e0520bSJosef Bacik fs_info->avail_system_alloc_bits &= ~extra_flags; 779e3e0520bSJosef Bacik write_sequnlock(&fs_info->profiles_lock); 780e3e0520bSJosef Bacik } 781e3e0520bSJosef Bacik 782e3e0520bSJosef Bacik /* 783e3e0520bSJosef Bacik * Clear incompat bits for the following feature(s): 784e3e0520bSJosef Bacik * 785e3e0520bSJosef Bacik * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group 786e3e0520bSJosef Bacik * in the whole filesystem 7879c907446SDavid Sterba * 7889c907446SDavid Sterba * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups 789e3e0520bSJosef Bacik */ 790e3e0520bSJosef Bacik static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags) 791e3e0520bSJosef Bacik { 7929c907446SDavid Sterba bool found_raid56 = false; 7939c907446SDavid Sterba bool found_raid1c34 = false; 7949c907446SDavid Sterba 7959c907446SDavid Sterba if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) || 7969c907446SDavid Sterba (flags & BTRFS_BLOCK_GROUP_RAID1C3) || 7979c907446SDavid Sterba (flags & BTRFS_BLOCK_GROUP_RAID1C4)) { 798e3e0520bSJosef Bacik struct list_head *head = &fs_info->space_info; 799e3e0520bSJosef Bacik struct btrfs_space_info *sinfo; 800e3e0520bSJosef Bacik 801e3e0520bSJosef Bacik list_for_each_entry_rcu(sinfo, head, list) { 802e3e0520bSJosef Bacik down_read(&sinfo->groups_sem); 803e3e0520bSJosef Bacik if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5])) 8049c907446SDavid Sterba found_raid56 = true; 805e3e0520bSJosef Bacik if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6])) 8069c907446SDavid Sterba found_raid56 = true; 8079c907446SDavid Sterba if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3])) 8089c907446SDavid Sterba found_raid1c34 = true; 8099c907446SDavid Sterba if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4])) 8109c907446SDavid Sterba found_raid1c34 = true; 811e3e0520bSJosef Bacik up_read(&sinfo->groups_sem); 812e3e0520bSJosef Bacik } 813d8e6fd5cSFilipe Manana if (!found_raid56) 814e3e0520bSJosef Bacik btrfs_clear_fs_incompat(fs_info, RAID56); 815d8e6fd5cSFilipe Manana if (!found_raid1c34) 8169c907446SDavid Sterba btrfs_clear_fs_incompat(fs_info, RAID1C34); 817e3e0520bSJosef Bacik } 818e3e0520bSJosef Bacik } 819e3e0520bSJosef Bacik 8207357623aSQu Wenruo static int remove_block_group_item(struct btrfs_trans_handle *trans, 8217357623aSQu Wenruo struct btrfs_path *path, 8227357623aSQu Wenruo struct btrfs_block_group *block_group) 8237357623aSQu Wenruo { 8247357623aSQu Wenruo struct btrfs_fs_info *fs_info = trans->fs_info; 8257357623aSQu Wenruo struct btrfs_root *root; 8267357623aSQu Wenruo struct btrfs_key key; 8277357623aSQu Wenruo int ret; 8287357623aSQu Wenruo 8297357623aSQu Wenruo root = fs_info->extent_root; 8307357623aSQu Wenruo key.objectid = block_group->start; 8317357623aSQu Wenruo key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 8327357623aSQu Wenruo key.offset = block_group->length; 8337357623aSQu Wenruo 8347357623aSQu Wenruo ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 8357357623aSQu Wenruo if (ret > 0) 8367357623aSQu Wenruo ret = -ENOENT; 8377357623aSQu Wenruo if (ret < 0) 8387357623aSQu Wenruo return ret; 8397357623aSQu Wenruo 8407357623aSQu Wenruo ret = btrfs_del_item(trans, root, path); 8417357623aSQu Wenruo return ret; 8427357623aSQu Wenruo } 8437357623aSQu Wenruo 844e3e0520bSJosef Bacik int btrfs_remove_block_group(struct btrfs_trans_handle *trans, 845e3e0520bSJosef Bacik u64 group_start, struct extent_map *em) 846e3e0520bSJosef Bacik { 847e3e0520bSJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 848e3e0520bSJosef Bacik struct btrfs_path *path; 84932da5386SDavid Sterba struct btrfs_block_group *block_group; 850e3e0520bSJosef Bacik struct btrfs_free_cluster *cluster; 851e3e0520bSJosef Bacik struct inode *inode; 852e3e0520bSJosef Bacik struct kobject *kobj = NULL; 853e3e0520bSJosef Bacik int ret; 854e3e0520bSJosef Bacik int index; 855e3e0520bSJosef Bacik int factor; 856e3e0520bSJosef Bacik struct btrfs_caching_control *caching_ctl = NULL; 857e3e0520bSJosef Bacik bool remove_em; 858e3e0520bSJosef Bacik bool remove_rsv = false; 859e3e0520bSJosef Bacik 860e3e0520bSJosef Bacik block_group = btrfs_lookup_block_group(fs_info, group_start); 861e3e0520bSJosef Bacik BUG_ON(!block_group); 862e3e0520bSJosef Bacik BUG_ON(!block_group->ro); 863e3e0520bSJosef Bacik 864e3e0520bSJosef Bacik trace_btrfs_remove_block_group(block_group); 865e3e0520bSJosef Bacik /* 866e3e0520bSJosef Bacik * Free the reserved super bytes from this block group before 867e3e0520bSJosef Bacik * remove it. 868e3e0520bSJosef Bacik */ 869e3e0520bSJosef Bacik btrfs_free_excluded_extents(block_group); 870b3470b5dSDavid Sterba btrfs_free_ref_tree_range(fs_info, block_group->start, 871b3470b5dSDavid Sterba block_group->length); 872e3e0520bSJosef Bacik 873e3e0520bSJosef Bacik index = btrfs_bg_flags_to_raid_index(block_group->flags); 874e3e0520bSJosef Bacik factor = btrfs_bg_type_to_factor(block_group->flags); 875e3e0520bSJosef Bacik 876e3e0520bSJosef Bacik /* make sure this block group isn't part of an allocation cluster */ 877e3e0520bSJosef Bacik cluster = &fs_info->data_alloc_cluster; 878e3e0520bSJosef Bacik spin_lock(&cluster->refill_lock); 879e3e0520bSJosef Bacik btrfs_return_cluster_to_free_space(block_group, cluster); 880e3e0520bSJosef Bacik spin_unlock(&cluster->refill_lock); 881e3e0520bSJosef Bacik 882e3e0520bSJosef Bacik /* 883e3e0520bSJosef Bacik * make sure this block group isn't part of a metadata 884e3e0520bSJosef Bacik * allocation cluster 885e3e0520bSJosef Bacik */ 886e3e0520bSJosef Bacik cluster = &fs_info->meta_alloc_cluster; 887e3e0520bSJosef Bacik spin_lock(&cluster->refill_lock); 888e3e0520bSJosef Bacik btrfs_return_cluster_to_free_space(block_group, cluster); 889e3e0520bSJosef Bacik spin_unlock(&cluster->refill_lock); 890e3e0520bSJosef Bacik 891e3e0520bSJosef Bacik path = btrfs_alloc_path(); 892e3e0520bSJosef Bacik if (!path) { 893e3e0520bSJosef Bacik ret = -ENOMEM; 8949fecd132SFilipe Manana goto out; 895e3e0520bSJosef Bacik } 896e3e0520bSJosef Bacik 897e3e0520bSJosef Bacik /* 898e3e0520bSJosef Bacik * get the inode first so any iput calls done for the io_list 899e3e0520bSJosef Bacik * aren't the final iput (no unlinks allowed now) 900e3e0520bSJosef Bacik */ 901e3e0520bSJosef Bacik inode = lookup_free_space_inode(block_group, path); 902e3e0520bSJosef Bacik 903e3e0520bSJosef Bacik mutex_lock(&trans->transaction->cache_write_mutex); 904e3e0520bSJosef Bacik /* 905e3e0520bSJosef Bacik * Make sure our free space cache IO is done before removing the 906e3e0520bSJosef Bacik * free space inode 907e3e0520bSJosef Bacik */ 908e3e0520bSJosef Bacik spin_lock(&trans->transaction->dirty_bgs_lock); 909e3e0520bSJosef Bacik if (!list_empty(&block_group->io_list)) { 910e3e0520bSJosef Bacik list_del_init(&block_group->io_list); 911e3e0520bSJosef Bacik 912e3e0520bSJosef Bacik WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); 913e3e0520bSJosef Bacik 914e3e0520bSJosef Bacik spin_unlock(&trans->transaction->dirty_bgs_lock); 915e3e0520bSJosef Bacik btrfs_wait_cache_io(trans, block_group, path); 916e3e0520bSJosef Bacik btrfs_put_block_group(block_group); 917e3e0520bSJosef Bacik spin_lock(&trans->transaction->dirty_bgs_lock); 918e3e0520bSJosef Bacik } 919e3e0520bSJosef Bacik 920e3e0520bSJosef Bacik if (!list_empty(&block_group->dirty_list)) { 921e3e0520bSJosef Bacik list_del_init(&block_group->dirty_list); 922e3e0520bSJosef Bacik remove_rsv = true; 923e3e0520bSJosef Bacik btrfs_put_block_group(block_group); 924e3e0520bSJosef Bacik } 925e3e0520bSJosef Bacik spin_unlock(&trans->transaction->dirty_bgs_lock); 926e3e0520bSJosef Bacik mutex_unlock(&trans->transaction->cache_write_mutex); 927e3e0520bSJosef Bacik 92836b216c8SBoris Burkov ret = btrfs_remove_free_space_inode(trans, inode, block_group); 929e3e0520bSJosef Bacik if (ret) 9309fecd132SFilipe Manana goto out; 931e3e0520bSJosef Bacik 932e3e0520bSJosef Bacik spin_lock(&fs_info->block_group_cache_lock); 933e3e0520bSJosef Bacik rb_erase(&block_group->cache_node, 934e3e0520bSJosef Bacik &fs_info->block_group_cache_tree); 935e3e0520bSJosef Bacik RB_CLEAR_NODE(&block_group->cache_node); 936e3e0520bSJosef Bacik 9379fecd132SFilipe Manana /* Once for the block groups rbtree */ 9389fecd132SFilipe Manana btrfs_put_block_group(block_group); 9399fecd132SFilipe Manana 940b3470b5dSDavid Sterba if (fs_info->first_logical_byte == block_group->start) 941e3e0520bSJosef Bacik fs_info->first_logical_byte = (u64)-1; 942e3e0520bSJosef Bacik spin_unlock(&fs_info->block_group_cache_lock); 943e3e0520bSJosef Bacik 944e3e0520bSJosef Bacik down_write(&block_group->space_info->groups_sem); 945e3e0520bSJosef Bacik /* 946e3e0520bSJosef Bacik * we must use list_del_init so people can check to see if they 947e3e0520bSJosef Bacik * are still on the list after taking the semaphore 948e3e0520bSJosef Bacik */ 949e3e0520bSJosef Bacik list_del_init(&block_group->list); 950e3e0520bSJosef Bacik if (list_empty(&block_group->space_info->block_groups[index])) { 951e3e0520bSJosef Bacik kobj = block_group->space_info->block_group_kobjs[index]; 952e3e0520bSJosef Bacik block_group->space_info->block_group_kobjs[index] = NULL; 953e3e0520bSJosef Bacik clear_avail_alloc_bits(fs_info, block_group->flags); 954e3e0520bSJosef Bacik } 955e3e0520bSJosef Bacik up_write(&block_group->space_info->groups_sem); 956e3e0520bSJosef Bacik clear_incompat_bg_bits(fs_info, block_group->flags); 957e3e0520bSJosef Bacik if (kobj) { 958e3e0520bSJosef Bacik kobject_del(kobj); 959e3e0520bSJosef Bacik kobject_put(kobj); 960e3e0520bSJosef Bacik } 961e3e0520bSJosef Bacik 962e3e0520bSJosef Bacik if (block_group->has_caching_ctl) 963e3e0520bSJosef Bacik caching_ctl = btrfs_get_caching_control(block_group); 964e3e0520bSJosef Bacik if (block_group->cached == BTRFS_CACHE_STARTED) 965e3e0520bSJosef Bacik btrfs_wait_block_group_cache_done(block_group); 966e3e0520bSJosef Bacik if (block_group->has_caching_ctl) { 967bbb86a37SJosef Bacik spin_lock(&fs_info->block_group_cache_lock); 968e3e0520bSJosef Bacik if (!caching_ctl) { 969e3e0520bSJosef Bacik struct btrfs_caching_control *ctl; 970e3e0520bSJosef Bacik 971e3e0520bSJosef Bacik list_for_each_entry(ctl, 972e3e0520bSJosef Bacik &fs_info->caching_block_groups, list) 973e3e0520bSJosef Bacik if (ctl->block_group == block_group) { 974e3e0520bSJosef Bacik caching_ctl = ctl; 975e3e0520bSJosef Bacik refcount_inc(&caching_ctl->count); 976e3e0520bSJosef Bacik break; 977e3e0520bSJosef Bacik } 978e3e0520bSJosef Bacik } 979e3e0520bSJosef Bacik if (caching_ctl) 980e3e0520bSJosef Bacik list_del_init(&caching_ctl->list); 981bbb86a37SJosef Bacik spin_unlock(&fs_info->block_group_cache_lock); 982e3e0520bSJosef Bacik if (caching_ctl) { 983e3e0520bSJosef Bacik /* Once for the caching bgs list and once for us. */ 984e3e0520bSJosef Bacik btrfs_put_caching_control(caching_ctl); 985e3e0520bSJosef Bacik btrfs_put_caching_control(caching_ctl); 986e3e0520bSJosef Bacik } 987e3e0520bSJosef Bacik } 988e3e0520bSJosef Bacik 989e3e0520bSJosef Bacik spin_lock(&trans->transaction->dirty_bgs_lock); 990e3e0520bSJosef Bacik WARN_ON(!list_empty(&block_group->dirty_list)); 991e3e0520bSJosef Bacik WARN_ON(!list_empty(&block_group->io_list)); 992e3e0520bSJosef Bacik spin_unlock(&trans->transaction->dirty_bgs_lock); 993e3e0520bSJosef Bacik 994e3e0520bSJosef Bacik btrfs_remove_free_space_cache(block_group); 995e3e0520bSJosef Bacik 996e3e0520bSJosef Bacik spin_lock(&block_group->space_info->lock); 997e3e0520bSJosef Bacik list_del_init(&block_group->ro_list); 998e3e0520bSJosef Bacik 999e3e0520bSJosef Bacik if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { 1000e3e0520bSJosef Bacik WARN_ON(block_group->space_info->total_bytes 1001b3470b5dSDavid Sterba < block_group->length); 1002e3e0520bSJosef Bacik WARN_ON(block_group->space_info->bytes_readonly 1003b3470b5dSDavid Sterba < block_group->length); 1004e3e0520bSJosef Bacik WARN_ON(block_group->space_info->disk_total 1005b3470b5dSDavid Sterba < block_group->length * factor); 1006e3e0520bSJosef Bacik } 1007b3470b5dSDavid Sterba block_group->space_info->total_bytes -= block_group->length; 1008b3470b5dSDavid Sterba block_group->space_info->bytes_readonly -= block_group->length; 1009b3470b5dSDavid Sterba block_group->space_info->disk_total -= block_group->length * factor; 1010e3e0520bSJosef Bacik 1011e3e0520bSJosef Bacik spin_unlock(&block_group->space_info->lock); 1012e3e0520bSJosef Bacik 1013ffcb9d44SFilipe Manana /* 1014ffcb9d44SFilipe Manana * Remove the free space for the block group from the free space tree 1015ffcb9d44SFilipe Manana * and the block group's item from the extent tree before marking the 1016ffcb9d44SFilipe Manana * block group as removed. This is to prevent races with tasks that 1017ffcb9d44SFilipe Manana * freeze and unfreeze a block group, this task and another task 1018ffcb9d44SFilipe Manana * allocating a new block group - the unfreeze task ends up removing 1019ffcb9d44SFilipe Manana * the block group's extent map before the task calling this function 1020ffcb9d44SFilipe Manana * deletes the block group item from the extent tree, allowing for 1021ffcb9d44SFilipe Manana * another task to attempt to create another block group with the same 1022ffcb9d44SFilipe Manana * item key (and failing with -EEXIST and a transaction abort). 1023ffcb9d44SFilipe Manana */ 1024ffcb9d44SFilipe Manana ret = remove_block_group_free_space(trans, block_group); 1025ffcb9d44SFilipe Manana if (ret) 1026ffcb9d44SFilipe Manana goto out; 1027ffcb9d44SFilipe Manana 1028ffcb9d44SFilipe Manana ret = remove_block_group_item(trans, path, block_group); 1029ffcb9d44SFilipe Manana if (ret < 0) 1030ffcb9d44SFilipe Manana goto out; 1031ffcb9d44SFilipe Manana 1032e3e0520bSJosef Bacik spin_lock(&block_group->lock); 1033e3e0520bSJosef Bacik block_group->removed = 1; 1034e3e0520bSJosef Bacik /* 10356b7304afSFilipe Manana * At this point trimming or scrub can't start on this block group, 10366b7304afSFilipe Manana * because we removed the block group from the rbtree 10376b7304afSFilipe Manana * fs_info->block_group_cache_tree so no one can't find it anymore and 10386b7304afSFilipe Manana * even if someone already got this block group before we removed it 10396b7304afSFilipe Manana * from the rbtree, they have already incremented block_group->frozen - 10406b7304afSFilipe Manana * if they didn't, for the trimming case they won't find any free space 10416b7304afSFilipe Manana * entries because we already removed them all when we called 10426b7304afSFilipe Manana * btrfs_remove_free_space_cache(). 1043e3e0520bSJosef Bacik * 1044e3e0520bSJosef Bacik * And we must not remove the extent map from the fs_info->mapping_tree 1045e3e0520bSJosef Bacik * to prevent the same logical address range and physical device space 10466b7304afSFilipe Manana * ranges from being reused for a new block group. This is needed to 10476b7304afSFilipe Manana * avoid races with trimming and scrub. 10486b7304afSFilipe Manana * 10496b7304afSFilipe Manana * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is 1050e3e0520bSJosef Bacik * completely transactionless, so while it is trimming a range the 1051e3e0520bSJosef Bacik * currently running transaction might finish and a new one start, 1052e3e0520bSJosef Bacik * allowing for new block groups to be created that can reuse the same 1053e3e0520bSJosef Bacik * physical device locations unless we take this special care. 1054e3e0520bSJosef Bacik * 1055e3e0520bSJosef Bacik * There may also be an implicit trim operation if the file system 1056e3e0520bSJosef Bacik * is mounted with -odiscard. The same protections must remain 1057e3e0520bSJosef Bacik * in place until the extents have been discarded completely when 1058e3e0520bSJosef Bacik * the transaction commit has completed. 1059e3e0520bSJosef Bacik */ 10606b7304afSFilipe Manana remove_em = (atomic_read(&block_group->frozen) == 0); 1061e3e0520bSJosef Bacik spin_unlock(&block_group->lock); 1062e3e0520bSJosef Bacik 1063e3e0520bSJosef Bacik if (remove_em) { 1064e3e0520bSJosef Bacik struct extent_map_tree *em_tree; 1065e3e0520bSJosef Bacik 1066e3e0520bSJosef Bacik em_tree = &fs_info->mapping_tree; 1067e3e0520bSJosef Bacik write_lock(&em_tree->lock); 1068e3e0520bSJosef Bacik remove_extent_mapping(em_tree, em); 1069e3e0520bSJosef Bacik write_unlock(&em_tree->lock); 1070e3e0520bSJosef Bacik /* once for the tree */ 1071e3e0520bSJosef Bacik free_extent_map(em); 1072e3e0520bSJosef Bacik } 1073f6033c5eSXiyu Yang 10749fecd132SFilipe Manana out: 1075f6033c5eSXiyu Yang /* Once for the lookup reference */ 1076f6033c5eSXiyu Yang btrfs_put_block_group(block_group); 1077e3e0520bSJosef Bacik if (remove_rsv) 1078e3e0520bSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 1079e3e0520bSJosef Bacik btrfs_free_path(path); 1080e3e0520bSJosef Bacik return ret; 1081e3e0520bSJosef Bacik } 1082e3e0520bSJosef Bacik 1083e3e0520bSJosef Bacik struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( 1084e3e0520bSJosef Bacik struct btrfs_fs_info *fs_info, const u64 chunk_offset) 1085e3e0520bSJosef Bacik { 1086e3e0520bSJosef Bacik struct extent_map_tree *em_tree = &fs_info->mapping_tree; 1087e3e0520bSJosef Bacik struct extent_map *em; 1088e3e0520bSJosef Bacik struct map_lookup *map; 1089e3e0520bSJosef Bacik unsigned int num_items; 1090e3e0520bSJosef Bacik 1091e3e0520bSJosef Bacik read_lock(&em_tree->lock); 1092e3e0520bSJosef Bacik em = lookup_extent_mapping(em_tree, chunk_offset, 1); 1093e3e0520bSJosef Bacik read_unlock(&em_tree->lock); 1094e3e0520bSJosef Bacik ASSERT(em && em->start == chunk_offset); 1095e3e0520bSJosef Bacik 1096e3e0520bSJosef Bacik /* 1097e3e0520bSJosef Bacik * We need to reserve 3 + N units from the metadata space info in order 1098e3e0520bSJosef Bacik * to remove a block group (done at btrfs_remove_chunk() and at 1099e3e0520bSJosef Bacik * btrfs_remove_block_group()), which are used for: 1100e3e0520bSJosef Bacik * 1101e3e0520bSJosef Bacik * 1 unit for adding the free space inode's orphan (located in the tree 1102e3e0520bSJosef Bacik * of tree roots). 1103e3e0520bSJosef Bacik * 1 unit for deleting the block group item (located in the extent 1104e3e0520bSJosef Bacik * tree). 1105e3e0520bSJosef Bacik * 1 unit for deleting the free space item (located in tree of tree 1106e3e0520bSJosef Bacik * roots). 1107e3e0520bSJosef Bacik * N units for deleting N device extent items corresponding to each 1108e3e0520bSJosef Bacik * stripe (located in the device tree). 1109e3e0520bSJosef Bacik * 1110e3e0520bSJosef Bacik * In order to remove a block group we also need to reserve units in the 1111e3e0520bSJosef Bacik * system space info in order to update the chunk tree (update one or 1112e3e0520bSJosef Bacik * more device items and remove one chunk item), but this is done at 1113e3e0520bSJosef Bacik * btrfs_remove_chunk() through a call to check_system_chunk(). 1114e3e0520bSJosef Bacik */ 1115e3e0520bSJosef Bacik map = em->map_lookup; 1116e3e0520bSJosef Bacik num_items = 3 + map->num_stripes; 1117e3e0520bSJosef Bacik free_extent_map(em); 1118e3e0520bSJosef Bacik 1119e3e0520bSJosef Bacik return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root, 11207f9fe614SJosef Bacik num_items); 1121e3e0520bSJosef Bacik } 1122e3e0520bSJosef Bacik 1123e3e0520bSJosef Bacik /* 112426ce2095SJosef Bacik * Mark block group @cache read-only, so later write won't happen to block 112526ce2095SJosef Bacik * group @cache. 112626ce2095SJosef Bacik * 112726ce2095SJosef Bacik * If @force is not set, this function will only mark the block group readonly 112826ce2095SJosef Bacik * if we have enough free space (1M) in other metadata/system block groups. 112926ce2095SJosef Bacik * If @force is not set, this function will mark the block group readonly 113026ce2095SJosef Bacik * without checking free space. 113126ce2095SJosef Bacik * 113226ce2095SJosef Bacik * NOTE: This function doesn't care if other block groups can contain all the 113326ce2095SJosef Bacik * data in this block group. That check should be done by relocation routine, 113426ce2095SJosef Bacik * not this function. 113526ce2095SJosef Bacik */ 113632da5386SDavid Sterba static int inc_block_group_ro(struct btrfs_block_group *cache, int force) 113726ce2095SJosef Bacik { 113826ce2095SJosef Bacik struct btrfs_space_info *sinfo = cache->space_info; 113926ce2095SJosef Bacik u64 num_bytes; 114026ce2095SJosef Bacik int ret = -ENOSPC; 114126ce2095SJosef Bacik 114226ce2095SJosef Bacik spin_lock(&sinfo->lock); 114326ce2095SJosef Bacik spin_lock(&cache->lock); 114426ce2095SJosef Bacik 114526ce2095SJosef Bacik if (cache->ro) { 114626ce2095SJosef Bacik cache->ro++; 114726ce2095SJosef Bacik ret = 0; 114826ce2095SJosef Bacik goto out; 114926ce2095SJosef Bacik } 115026ce2095SJosef Bacik 1151b3470b5dSDavid Sterba num_bytes = cache->length - cache->reserved - cache->pinned - 1152bf38be65SDavid Sterba cache->bytes_super - cache->used; 115326ce2095SJosef Bacik 115426ce2095SJosef Bacik /* 1155a30a3d20SJosef Bacik * Data never overcommits, even in mixed mode, so do just the straight 1156a30a3d20SJosef Bacik * check of left over space in how much we have allocated. 1157a30a3d20SJosef Bacik */ 1158a30a3d20SJosef Bacik if (force) { 1159a30a3d20SJosef Bacik ret = 0; 1160a30a3d20SJosef Bacik } else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) { 1161a30a3d20SJosef Bacik u64 sinfo_used = btrfs_space_info_used(sinfo, true); 1162a30a3d20SJosef Bacik 1163a30a3d20SJosef Bacik /* 116426ce2095SJosef Bacik * Here we make sure if we mark this bg RO, we still have enough 1165f8935566SJosef Bacik * free space as buffer. 116626ce2095SJosef Bacik */ 1167a30a3d20SJosef Bacik if (sinfo_used + num_bytes <= sinfo->total_bytes) 1168a30a3d20SJosef Bacik ret = 0; 1169a30a3d20SJosef Bacik } else { 1170a30a3d20SJosef Bacik /* 1171a30a3d20SJosef Bacik * We overcommit metadata, so we need to do the 1172a30a3d20SJosef Bacik * btrfs_can_overcommit check here, and we need to pass in 1173a30a3d20SJosef Bacik * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of 1174a30a3d20SJosef Bacik * leeway to allow us to mark this block group as read only. 1175a30a3d20SJosef Bacik */ 1176a30a3d20SJosef Bacik if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes, 1177a30a3d20SJosef Bacik BTRFS_RESERVE_NO_FLUSH)) 1178a30a3d20SJosef Bacik ret = 0; 1179a30a3d20SJosef Bacik } 1180a30a3d20SJosef Bacik 1181a30a3d20SJosef Bacik if (!ret) { 118226ce2095SJosef Bacik sinfo->bytes_readonly += num_bytes; 118326ce2095SJosef Bacik cache->ro++; 118426ce2095SJosef Bacik list_add_tail(&cache->ro_list, &sinfo->ro_bgs); 118526ce2095SJosef Bacik } 118626ce2095SJosef Bacik out: 118726ce2095SJosef Bacik spin_unlock(&cache->lock); 118826ce2095SJosef Bacik spin_unlock(&sinfo->lock); 118926ce2095SJosef Bacik if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) { 119026ce2095SJosef Bacik btrfs_info(cache->fs_info, 1191b3470b5dSDavid Sterba "unable to make block group %llu ro", cache->start); 119226ce2095SJosef Bacik btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0); 119326ce2095SJosef Bacik } 119426ce2095SJosef Bacik return ret; 119526ce2095SJosef Bacik } 119626ce2095SJosef Bacik 1197fe119a6eSNikolay Borisov static bool clean_pinned_extents(struct btrfs_trans_handle *trans, 1198fe119a6eSNikolay Borisov struct btrfs_block_group *bg) 119945bb5d6aSNikolay Borisov { 120045bb5d6aSNikolay Borisov struct btrfs_fs_info *fs_info = bg->fs_info; 1201fe119a6eSNikolay Borisov struct btrfs_transaction *prev_trans = NULL; 120245bb5d6aSNikolay Borisov const u64 start = bg->start; 120345bb5d6aSNikolay Borisov const u64 end = start + bg->length - 1; 120445bb5d6aSNikolay Borisov int ret; 120545bb5d6aSNikolay Borisov 1206fe119a6eSNikolay Borisov spin_lock(&fs_info->trans_lock); 1207fe119a6eSNikolay Borisov if (trans->transaction->list.prev != &fs_info->trans_list) { 1208fe119a6eSNikolay Borisov prev_trans = list_last_entry(&trans->transaction->list, 1209fe119a6eSNikolay Borisov struct btrfs_transaction, list); 1210fe119a6eSNikolay Borisov refcount_inc(&prev_trans->use_count); 1211fe119a6eSNikolay Borisov } 1212fe119a6eSNikolay Borisov spin_unlock(&fs_info->trans_lock); 1213fe119a6eSNikolay Borisov 121445bb5d6aSNikolay Borisov /* 121545bb5d6aSNikolay Borisov * Hold the unused_bg_unpin_mutex lock to avoid racing with 121645bb5d6aSNikolay Borisov * btrfs_finish_extent_commit(). If we are at transaction N, another 121745bb5d6aSNikolay Borisov * task might be running finish_extent_commit() for the previous 121845bb5d6aSNikolay Borisov * transaction N - 1, and have seen a range belonging to the block 1219fe119a6eSNikolay Borisov * group in pinned_extents before we were able to clear the whole block 1220fe119a6eSNikolay Borisov * group range from pinned_extents. This means that task can lookup for 1221fe119a6eSNikolay Borisov * the block group after we unpinned it from pinned_extents and removed 1222fe119a6eSNikolay Borisov * it, leading to a BUG_ON() at unpin_extent_range(). 122345bb5d6aSNikolay Borisov */ 122445bb5d6aSNikolay Borisov mutex_lock(&fs_info->unused_bg_unpin_mutex); 1225fe119a6eSNikolay Borisov if (prev_trans) { 1226fe119a6eSNikolay Borisov ret = clear_extent_bits(&prev_trans->pinned_extents, start, end, 122745bb5d6aSNikolay Borisov EXTENT_DIRTY); 122845bb5d6aSNikolay Borisov if (ret) 1229534cf531SFilipe Manana goto out; 1230fe119a6eSNikolay Borisov } 123145bb5d6aSNikolay Borisov 1232fe119a6eSNikolay Borisov ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end, 123345bb5d6aSNikolay Borisov EXTENT_DIRTY); 1234534cf531SFilipe Manana out: 123545bb5d6aSNikolay Borisov mutex_unlock(&fs_info->unused_bg_unpin_mutex); 12365150bf19SFilipe Manana if (prev_trans) 12375150bf19SFilipe Manana btrfs_put_transaction(prev_trans); 123845bb5d6aSNikolay Borisov 1239534cf531SFilipe Manana return ret == 0; 124045bb5d6aSNikolay Borisov } 124145bb5d6aSNikolay Borisov 124226ce2095SJosef Bacik /* 1243e3e0520bSJosef Bacik * Process the unused_bgs list and remove any that don't have any allocated 1244e3e0520bSJosef Bacik * space inside of them. 1245e3e0520bSJosef Bacik */ 1246e3e0520bSJosef Bacik void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) 1247e3e0520bSJosef Bacik { 124832da5386SDavid Sterba struct btrfs_block_group *block_group; 1249e3e0520bSJosef Bacik struct btrfs_space_info *space_info; 1250e3e0520bSJosef Bacik struct btrfs_trans_handle *trans; 12516e80d4f8SDennis Zhou const bool async_trim_enabled = btrfs_test_opt(fs_info, DISCARD_ASYNC); 1252e3e0520bSJosef Bacik int ret = 0; 1253e3e0520bSJosef Bacik 1254e3e0520bSJosef Bacik if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 1255e3e0520bSJosef Bacik return; 1256e3e0520bSJosef Bacik 1257e3e0520bSJosef Bacik spin_lock(&fs_info->unused_bgs_lock); 1258e3e0520bSJosef Bacik while (!list_empty(&fs_info->unused_bgs)) { 1259e3e0520bSJosef Bacik int trimming; 1260e3e0520bSJosef Bacik 1261e3e0520bSJosef Bacik block_group = list_first_entry(&fs_info->unused_bgs, 126232da5386SDavid Sterba struct btrfs_block_group, 1263e3e0520bSJosef Bacik bg_list); 1264e3e0520bSJosef Bacik list_del_init(&block_group->bg_list); 1265e3e0520bSJosef Bacik 1266e3e0520bSJosef Bacik space_info = block_group->space_info; 1267e3e0520bSJosef Bacik 1268e3e0520bSJosef Bacik if (ret || btrfs_mixed_space_info(space_info)) { 1269e3e0520bSJosef Bacik btrfs_put_block_group(block_group); 1270e3e0520bSJosef Bacik continue; 1271e3e0520bSJosef Bacik } 1272e3e0520bSJosef Bacik spin_unlock(&fs_info->unused_bgs_lock); 1273e3e0520bSJosef Bacik 1274b0643e59SDennis Zhou btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); 1275b0643e59SDennis Zhou 1276e3e0520bSJosef Bacik mutex_lock(&fs_info->delete_unused_bgs_mutex); 1277e3e0520bSJosef Bacik 1278e3e0520bSJosef Bacik /* Don't want to race with allocators so take the groups_sem */ 1279e3e0520bSJosef Bacik down_write(&space_info->groups_sem); 12806e80d4f8SDennis Zhou 12816e80d4f8SDennis Zhou /* 12826e80d4f8SDennis Zhou * Async discard moves the final block group discard to be prior 12836e80d4f8SDennis Zhou * to the unused_bgs code path. Therefore, if it's not fully 12846e80d4f8SDennis Zhou * trimmed, punt it back to the async discard lists. 12856e80d4f8SDennis Zhou */ 12866e80d4f8SDennis Zhou if (btrfs_test_opt(fs_info, DISCARD_ASYNC) && 12876e80d4f8SDennis Zhou !btrfs_is_free_space_trimmed(block_group)) { 12886e80d4f8SDennis Zhou trace_btrfs_skip_unused_block_group(block_group); 12896e80d4f8SDennis Zhou up_write(&space_info->groups_sem); 12906e80d4f8SDennis Zhou /* Requeue if we failed because of async discard */ 12916e80d4f8SDennis Zhou btrfs_discard_queue_work(&fs_info->discard_ctl, 12926e80d4f8SDennis Zhou block_group); 12936e80d4f8SDennis Zhou goto next; 12946e80d4f8SDennis Zhou } 12956e80d4f8SDennis Zhou 1296e3e0520bSJosef Bacik spin_lock(&block_group->lock); 1297e3e0520bSJosef Bacik if (block_group->reserved || block_group->pinned || 1298bf38be65SDavid Sterba block_group->used || block_group->ro || 1299e3e0520bSJosef Bacik list_is_singular(&block_group->list)) { 1300e3e0520bSJosef Bacik /* 1301e3e0520bSJosef Bacik * We want to bail if we made new allocations or have 1302e3e0520bSJosef Bacik * outstanding allocations in this block group. We do 1303e3e0520bSJosef Bacik * the ro check in case balance is currently acting on 1304e3e0520bSJosef Bacik * this block group. 1305e3e0520bSJosef Bacik */ 1306e3e0520bSJosef Bacik trace_btrfs_skip_unused_block_group(block_group); 1307e3e0520bSJosef Bacik spin_unlock(&block_group->lock); 1308e3e0520bSJosef Bacik up_write(&space_info->groups_sem); 1309e3e0520bSJosef Bacik goto next; 1310e3e0520bSJosef Bacik } 1311e3e0520bSJosef Bacik spin_unlock(&block_group->lock); 1312e3e0520bSJosef Bacik 1313e3e0520bSJosef Bacik /* We don't want to force the issue, only flip if it's ok. */ 1314e11c0406SJosef Bacik ret = inc_block_group_ro(block_group, 0); 1315e3e0520bSJosef Bacik up_write(&space_info->groups_sem); 1316e3e0520bSJosef Bacik if (ret < 0) { 1317e3e0520bSJosef Bacik ret = 0; 1318e3e0520bSJosef Bacik goto next; 1319e3e0520bSJosef Bacik } 1320e3e0520bSJosef Bacik 1321e3e0520bSJosef Bacik /* 1322e3e0520bSJosef Bacik * Want to do this before we do anything else so we can recover 1323e3e0520bSJosef Bacik * properly if we fail to join the transaction. 1324e3e0520bSJosef Bacik */ 1325e3e0520bSJosef Bacik trans = btrfs_start_trans_remove_block_group(fs_info, 1326b3470b5dSDavid Sterba block_group->start); 1327e3e0520bSJosef Bacik if (IS_ERR(trans)) { 1328e3e0520bSJosef Bacik btrfs_dec_block_group_ro(block_group); 1329e3e0520bSJosef Bacik ret = PTR_ERR(trans); 1330e3e0520bSJosef Bacik goto next; 1331e3e0520bSJosef Bacik } 1332e3e0520bSJosef Bacik 1333e3e0520bSJosef Bacik /* 1334e3e0520bSJosef Bacik * We could have pending pinned extents for this block group, 1335e3e0520bSJosef Bacik * just delete them, we don't care about them anymore. 1336e3e0520bSJosef Bacik */ 1337534cf531SFilipe Manana if (!clean_pinned_extents(trans, block_group)) { 1338534cf531SFilipe Manana btrfs_dec_block_group_ro(block_group); 1339e3e0520bSJosef Bacik goto end_trans; 1340534cf531SFilipe Manana } 1341e3e0520bSJosef Bacik 1342b0643e59SDennis Zhou /* 1343b0643e59SDennis Zhou * At this point, the block_group is read only and should fail 1344b0643e59SDennis Zhou * new allocations. However, btrfs_finish_extent_commit() can 1345b0643e59SDennis Zhou * cause this block_group to be placed back on the discard 1346b0643e59SDennis Zhou * lists because now the block_group isn't fully discarded. 1347b0643e59SDennis Zhou * Bail here and try again later after discarding everything. 1348b0643e59SDennis Zhou */ 1349b0643e59SDennis Zhou spin_lock(&fs_info->discard_ctl.lock); 1350b0643e59SDennis Zhou if (!list_empty(&block_group->discard_list)) { 1351b0643e59SDennis Zhou spin_unlock(&fs_info->discard_ctl.lock); 1352b0643e59SDennis Zhou btrfs_dec_block_group_ro(block_group); 1353b0643e59SDennis Zhou btrfs_discard_queue_work(&fs_info->discard_ctl, 1354b0643e59SDennis Zhou block_group); 1355b0643e59SDennis Zhou goto end_trans; 1356b0643e59SDennis Zhou } 1357b0643e59SDennis Zhou spin_unlock(&fs_info->discard_ctl.lock); 1358b0643e59SDennis Zhou 1359e3e0520bSJosef Bacik /* Reset pinned so btrfs_put_block_group doesn't complain */ 1360e3e0520bSJosef Bacik spin_lock(&space_info->lock); 1361e3e0520bSJosef Bacik spin_lock(&block_group->lock); 1362e3e0520bSJosef Bacik 1363e3e0520bSJosef Bacik btrfs_space_info_update_bytes_pinned(fs_info, space_info, 1364e3e0520bSJosef Bacik -block_group->pinned); 1365e3e0520bSJosef Bacik space_info->bytes_readonly += block_group->pinned; 1366e3e0520bSJosef Bacik percpu_counter_add_batch(&space_info->total_bytes_pinned, 1367e3e0520bSJosef Bacik -block_group->pinned, 1368e3e0520bSJosef Bacik BTRFS_TOTAL_BYTES_PINNED_BATCH); 1369e3e0520bSJosef Bacik block_group->pinned = 0; 1370e3e0520bSJosef Bacik 1371e3e0520bSJosef Bacik spin_unlock(&block_group->lock); 1372e3e0520bSJosef Bacik spin_unlock(&space_info->lock); 1373e3e0520bSJosef Bacik 13746e80d4f8SDennis Zhou /* 13756e80d4f8SDennis Zhou * The normal path here is an unused block group is passed here, 13766e80d4f8SDennis Zhou * then trimming is handled in the transaction commit path. 13776e80d4f8SDennis Zhou * Async discard interposes before this to do the trimming 13786e80d4f8SDennis Zhou * before coming down the unused block group path as trimming 13796e80d4f8SDennis Zhou * will no longer be done later in the transaction commit path. 13806e80d4f8SDennis Zhou */ 13816e80d4f8SDennis Zhou if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC)) 13826e80d4f8SDennis Zhou goto flip_async; 13836e80d4f8SDennis Zhou 1384e3e0520bSJosef Bacik /* DISCARD can flip during remount */ 138546b27f50SDennis Zhou trimming = btrfs_test_opt(fs_info, DISCARD_SYNC); 1386e3e0520bSJosef Bacik 1387e3e0520bSJosef Bacik /* Implicit trim during transaction commit. */ 1388e3e0520bSJosef Bacik if (trimming) 13896b7304afSFilipe Manana btrfs_freeze_block_group(block_group); 1390e3e0520bSJosef Bacik 1391e3e0520bSJosef Bacik /* 1392e3e0520bSJosef Bacik * Btrfs_remove_chunk will abort the transaction if things go 1393e3e0520bSJosef Bacik * horribly wrong. 1394e3e0520bSJosef Bacik */ 1395b3470b5dSDavid Sterba ret = btrfs_remove_chunk(trans, block_group->start); 1396e3e0520bSJosef Bacik 1397e3e0520bSJosef Bacik if (ret) { 1398e3e0520bSJosef Bacik if (trimming) 13996b7304afSFilipe Manana btrfs_unfreeze_block_group(block_group); 1400e3e0520bSJosef Bacik goto end_trans; 1401e3e0520bSJosef Bacik } 1402e3e0520bSJosef Bacik 1403e3e0520bSJosef Bacik /* 1404e3e0520bSJosef Bacik * If we're not mounted with -odiscard, we can just forget 1405e3e0520bSJosef Bacik * about this block group. Otherwise we'll need to wait 1406e3e0520bSJosef Bacik * until transaction commit to do the actual discard. 1407e3e0520bSJosef Bacik */ 1408e3e0520bSJosef Bacik if (trimming) { 1409e3e0520bSJosef Bacik spin_lock(&fs_info->unused_bgs_lock); 1410e3e0520bSJosef Bacik /* 1411e3e0520bSJosef Bacik * A concurrent scrub might have added us to the list 1412e3e0520bSJosef Bacik * fs_info->unused_bgs, so use a list_move operation 1413e3e0520bSJosef Bacik * to add the block group to the deleted_bgs list. 1414e3e0520bSJosef Bacik */ 1415e3e0520bSJosef Bacik list_move(&block_group->bg_list, 1416e3e0520bSJosef Bacik &trans->transaction->deleted_bgs); 1417e3e0520bSJosef Bacik spin_unlock(&fs_info->unused_bgs_lock); 1418e3e0520bSJosef Bacik btrfs_get_block_group(block_group); 1419e3e0520bSJosef Bacik } 1420e3e0520bSJosef Bacik end_trans: 1421e3e0520bSJosef Bacik btrfs_end_transaction(trans); 1422e3e0520bSJosef Bacik next: 1423e3e0520bSJosef Bacik mutex_unlock(&fs_info->delete_unused_bgs_mutex); 1424e3e0520bSJosef Bacik btrfs_put_block_group(block_group); 1425e3e0520bSJosef Bacik spin_lock(&fs_info->unused_bgs_lock); 1426e3e0520bSJosef Bacik } 1427e3e0520bSJosef Bacik spin_unlock(&fs_info->unused_bgs_lock); 14286e80d4f8SDennis Zhou return; 14296e80d4f8SDennis Zhou 14306e80d4f8SDennis Zhou flip_async: 14316e80d4f8SDennis Zhou btrfs_end_transaction(trans); 14326e80d4f8SDennis Zhou mutex_unlock(&fs_info->delete_unused_bgs_mutex); 14336e80d4f8SDennis Zhou btrfs_put_block_group(block_group); 14346e80d4f8SDennis Zhou btrfs_discard_punt_unused_bgs_list(fs_info); 1435e3e0520bSJosef Bacik } 1436e3e0520bSJosef Bacik 143732da5386SDavid Sterba void btrfs_mark_bg_unused(struct btrfs_block_group *bg) 1438e3e0520bSJosef Bacik { 1439e3e0520bSJosef Bacik struct btrfs_fs_info *fs_info = bg->fs_info; 1440e3e0520bSJosef Bacik 1441e3e0520bSJosef Bacik spin_lock(&fs_info->unused_bgs_lock); 1442e3e0520bSJosef Bacik if (list_empty(&bg->bg_list)) { 1443e3e0520bSJosef Bacik btrfs_get_block_group(bg); 1444e3e0520bSJosef Bacik trace_btrfs_add_unused_block_group(bg); 1445e3e0520bSJosef Bacik list_add_tail(&bg->bg_list, &fs_info->unused_bgs); 1446e3e0520bSJosef Bacik } 1447e3e0520bSJosef Bacik spin_unlock(&fs_info->unused_bgs_lock); 1448e3e0520bSJosef Bacik } 14494358d963SJosef Bacik 1450e3ba67a1SJohannes Thumshirn static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key, 1451e3ba67a1SJohannes Thumshirn struct btrfs_path *path) 1452e3ba67a1SJohannes Thumshirn { 1453e3ba67a1SJohannes Thumshirn struct extent_map_tree *em_tree; 1454e3ba67a1SJohannes Thumshirn struct extent_map *em; 1455e3ba67a1SJohannes Thumshirn struct btrfs_block_group_item bg; 1456e3ba67a1SJohannes Thumshirn struct extent_buffer *leaf; 1457e3ba67a1SJohannes Thumshirn int slot; 1458e3ba67a1SJohannes Thumshirn u64 flags; 1459e3ba67a1SJohannes Thumshirn int ret = 0; 1460e3ba67a1SJohannes Thumshirn 1461e3ba67a1SJohannes Thumshirn slot = path->slots[0]; 1462e3ba67a1SJohannes Thumshirn leaf = path->nodes[0]; 1463e3ba67a1SJohannes Thumshirn 1464e3ba67a1SJohannes Thumshirn em_tree = &fs_info->mapping_tree; 1465e3ba67a1SJohannes Thumshirn read_lock(&em_tree->lock); 1466e3ba67a1SJohannes Thumshirn em = lookup_extent_mapping(em_tree, key->objectid, key->offset); 1467e3ba67a1SJohannes Thumshirn read_unlock(&em_tree->lock); 1468e3ba67a1SJohannes Thumshirn if (!em) { 1469e3ba67a1SJohannes Thumshirn btrfs_err(fs_info, 1470e3ba67a1SJohannes Thumshirn "logical %llu len %llu found bg but no related chunk", 1471e3ba67a1SJohannes Thumshirn key->objectid, key->offset); 1472e3ba67a1SJohannes Thumshirn return -ENOENT; 1473e3ba67a1SJohannes Thumshirn } 1474e3ba67a1SJohannes Thumshirn 1475e3ba67a1SJohannes Thumshirn if (em->start != key->objectid || em->len != key->offset) { 1476e3ba67a1SJohannes Thumshirn btrfs_err(fs_info, 1477e3ba67a1SJohannes Thumshirn "block group %llu len %llu mismatch with chunk %llu len %llu", 1478e3ba67a1SJohannes Thumshirn key->objectid, key->offset, em->start, em->len); 1479e3ba67a1SJohannes Thumshirn ret = -EUCLEAN; 1480e3ba67a1SJohannes Thumshirn goto out_free_em; 1481e3ba67a1SJohannes Thumshirn } 1482e3ba67a1SJohannes Thumshirn 1483e3ba67a1SJohannes Thumshirn read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot), 1484e3ba67a1SJohannes Thumshirn sizeof(bg)); 1485e3ba67a1SJohannes Thumshirn flags = btrfs_stack_block_group_flags(&bg) & 1486e3ba67a1SJohannes Thumshirn BTRFS_BLOCK_GROUP_TYPE_MASK; 1487e3ba67a1SJohannes Thumshirn 1488e3ba67a1SJohannes Thumshirn if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { 1489e3ba67a1SJohannes Thumshirn btrfs_err(fs_info, 1490e3ba67a1SJohannes Thumshirn "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx", 1491e3ba67a1SJohannes Thumshirn key->objectid, key->offset, flags, 1492e3ba67a1SJohannes Thumshirn (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type)); 1493e3ba67a1SJohannes Thumshirn ret = -EUCLEAN; 1494e3ba67a1SJohannes Thumshirn } 1495e3ba67a1SJohannes Thumshirn 1496e3ba67a1SJohannes Thumshirn out_free_em: 1497e3ba67a1SJohannes Thumshirn free_extent_map(em); 1498e3ba67a1SJohannes Thumshirn return ret; 1499e3ba67a1SJohannes Thumshirn } 1500e3ba67a1SJohannes Thumshirn 15014358d963SJosef Bacik static int find_first_block_group(struct btrfs_fs_info *fs_info, 15024358d963SJosef Bacik struct btrfs_path *path, 15034358d963SJosef Bacik struct btrfs_key *key) 15044358d963SJosef Bacik { 15054358d963SJosef Bacik struct btrfs_root *root = fs_info->extent_root; 1506e3ba67a1SJohannes Thumshirn int ret; 15074358d963SJosef Bacik struct btrfs_key found_key; 15084358d963SJosef Bacik struct extent_buffer *leaf; 15094358d963SJosef Bacik int slot; 15104358d963SJosef Bacik 15114358d963SJosef Bacik ret = btrfs_search_slot(NULL, root, key, path, 0, 0); 15124358d963SJosef Bacik if (ret < 0) 1513e3ba67a1SJohannes Thumshirn return ret; 15144358d963SJosef Bacik 15154358d963SJosef Bacik while (1) { 15164358d963SJosef Bacik slot = path->slots[0]; 15174358d963SJosef Bacik leaf = path->nodes[0]; 15184358d963SJosef Bacik if (slot >= btrfs_header_nritems(leaf)) { 15194358d963SJosef Bacik ret = btrfs_next_leaf(root, path); 15204358d963SJosef Bacik if (ret == 0) 15214358d963SJosef Bacik continue; 15224358d963SJosef Bacik if (ret < 0) 15234358d963SJosef Bacik goto out; 15244358d963SJosef Bacik break; 15254358d963SJosef Bacik } 15264358d963SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 15274358d963SJosef Bacik 15284358d963SJosef Bacik if (found_key.objectid >= key->objectid && 15294358d963SJosef Bacik found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { 1530e3ba67a1SJohannes Thumshirn ret = read_bg_from_eb(fs_info, &found_key, path); 1531e3ba67a1SJohannes Thumshirn break; 1532e3ba67a1SJohannes Thumshirn } 15334358d963SJosef Bacik 15344358d963SJosef Bacik path->slots[0]++; 15354358d963SJosef Bacik } 15364358d963SJosef Bacik out: 15374358d963SJosef Bacik return ret; 15384358d963SJosef Bacik } 15394358d963SJosef Bacik 15404358d963SJosef Bacik static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) 15414358d963SJosef Bacik { 15424358d963SJosef Bacik u64 extra_flags = chunk_to_extended(flags) & 15434358d963SJosef Bacik BTRFS_EXTENDED_PROFILE_MASK; 15444358d963SJosef Bacik 15454358d963SJosef Bacik write_seqlock(&fs_info->profiles_lock); 15464358d963SJosef Bacik if (flags & BTRFS_BLOCK_GROUP_DATA) 15474358d963SJosef Bacik fs_info->avail_data_alloc_bits |= extra_flags; 15484358d963SJosef Bacik if (flags & BTRFS_BLOCK_GROUP_METADATA) 15494358d963SJosef Bacik fs_info->avail_metadata_alloc_bits |= extra_flags; 15504358d963SJosef Bacik if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 15514358d963SJosef Bacik fs_info->avail_system_alloc_bits |= extra_flags; 15524358d963SJosef Bacik write_sequnlock(&fs_info->profiles_lock); 15534358d963SJosef Bacik } 15544358d963SJosef Bacik 155596a14336SNikolay Borisov /** 155696a14336SNikolay Borisov * btrfs_rmap_block - Map a physical disk address to a list of logical addresses 155796a14336SNikolay Borisov * @chunk_start: logical address of block group 155896a14336SNikolay Borisov * @physical: physical address to map to logical addresses 155996a14336SNikolay Borisov * @logical: return array of logical addresses which map to @physical 156096a14336SNikolay Borisov * @naddrs: length of @logical 156196a14336SNikolay Borisov * @stripe_len: size of IO stripe for the given block group 156296a14336SNikolay Borisov * 156396a14336SNikolay Borisov * Maps a particular @physical disk address to a list of @logical addresses. 156496a14336SNikolay Borisov * Used primarily to exclude those portions of a block group that contain super 156596a14336SNikolay Borisov * block copies. 156696a14336SNikolay Borisov */ 156796a14336SNikolay Borisov EXPORT_FOR_TESTS 156896a14336SNikolay Borisov int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, 156996a14336SNikolay Borisov u64 physical, u64 **logical, int *naddrs, int *stripe_len) 157096a14336SNikolay Borisov { 157196a14336SNikolay Borisov struct extent_map *em; 157296a14336SNikolay Borisov struct map_lookup *map; 157396a14336SNikolay Borisov u64 *buf; 157496a14336SNikolay Borisov u64 bytenr; 15751776ad17SNikolay Borisov u64 data_stripe_length; 15761776ad17SNikolay Borisov u64 io_stripe_size; 15771776ad17SNikolay Borisov int i, nr = 0; 15781776ad17SNikolay Borisov int ret = 0; 157996a14336SNikolay Borisov 158096a14336SNikolay Borisov em = btrfs_get_chunk_map(fs_info, chunk_start, 1); 158196a14336SNikolay Borisov if (IS_ERR(em)) 158296a14336SNikolay Borisov return -EIO; 158396a14336SNikolay Borisov 158496a14336SNikolay Borisov map = em->map_lookup; 15859e22b925SNikolay Borisov data_stripe_length = em->orig_block_len; 15861776ad17SNikolay Borisov io_stripe_size = map->stripe_len; 158796a14336SNikolay Borisov 15889e22b925SNikolay Borisov /* For RAID5/6 adjust to a full IO stripe length */ 15899e22b925SNikolay Borisov if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) 15901776ad17SNikolay Borisov io_stripe_size = map->stripe_len * nr_data_stripes(map); 159196a14336SNikolay Borisov 159296a14336SNikolay Borisov buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); 15931776ad17SNikolay Borisov if (!buf) { 15941776ad17SNikolay Borisov ret = -ENOMEM; 15951776ad17SNikolay Borisov goto out; 15961776ad17SNikolay Borisov } 159796a14336SNikolay Borisov 159896a14336SNikolay Borisov for (i = 0; i < map->num_stripes; i++) { 15991776ad17SNikolay Borisov bool already_inserted = false; 16001776ad17SNikolay Borisov u64 stripe_nr; 16011776ad17SNikolay Borisov int j; 16021776ad17SNikolay Borisov 16031776ad17SNikolay Borisov if (!in_range(physical, map->stripes[i].physical, 16041776ad17SNikolay Borisov data_stripe_length)) 160596a14336SNikolay Borisov continue; 160696a14336SNikolay Borisov 160796a14336SNikolay Borisov stripe_nr = physical - map->stripes[i].physical; 160896a14336SNikolay Borisov stripe_nr = div64_u64(stripe_nr, map->stripe_len); 160996a14336SNikolay Borisov 161096a14336SNikolay Borisov if (map->type & BTRFS_BLOCK_GROUP_RAID10) { 161196a14336SNikolay Borisov stripe_nr = stripe_nr * map->num_stripes + i; 161296a14336SNikolay Borisov stripe_nr = div_u64(stripe_nr, map->sub_stripes); 161396a14336SNikolay Borisov } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { 161496a14336SNikolay Borisov stripe_nr = stripe_nr * map->num_stripes + i; 161596a14336SNikolay Borisov } 161696a14336SNikolay Borisov /* 161796a14336SNikolay Borisov * The remaining case would be for RAID56, multiply by 161896a14336SNikolay Borisov * nr_data_stripes(). Alternatively, just use rmap_len below 161996a14336SNikolay Borisov * instead of map->stripe_len 162096a14336SNikolay Borisov */ 162196a14336SNikolay Borisov 16221776ad17SNikolay Borisov bytenr = chunk_start + stripe_nr * io_stripe_size; 16231776ad17SNikolay Borisov 16241776ad17SNikolay Borisov /* Ensure we don't add duplicate addresses */ 162596a14336SNikolay Borisov for (j = 0; j < nr; j++) { 16261776ad17SNikolay Borisov if (buf[j] == bytenr) { 16271776ad17SNikolay Borisov already_inserted = true; 162896a14336SNikolay Borisov break; 162996a14336SNikolay Borisov } 163096a14336SNikolay Borisov } 16311776ad17SNikolay Borisov 16321776ad17SNikolay Borisov if (!already_inserted) 16331776ad17SNikolay Borisov buf[nr++] = bytenr; 163496a14336SNikolay Borisov } 163596a14336SNikolay Borisov 163696a14336SNikolay Borisov *logical = buf; 163796a14336SNikolay Borisov *naddrs = nr; 16381776ad17SNikolay Borisov *stripe_len = io_stripe_size; 16391776ad17SNikolay Borisov out: 164096a14336SNikolay Borisov free_extent_map(em); 16411776ad17SNikolay Borisov return ret; 164296a14336SNikolay Borisov } 164396a14336SNikolay Borisov 164432da5386SDavid Sterba static int exclude_super_stripes(struct btrfs_block_group *cache) 16454358d963SJosef Bacik { 16464358d963SJosef Bacik struct btrfs_fs_info *fs_info = cache->fs_info; 164712659251SNaohiro Aota const bool zoned = btrfs_is_zoned(fs_info); 16484358d963SJosef Bacik u64 bytenr; 16494358d963SJosef Bacik u64 *logical; 16504358d963SJosef Bacik int stripe_len; 16514358d963SJosef Bacik int i, nr, ret; 16524358d963SJosef Bacik 1653b3470b5dSDavid Sterba if (cache->start < BTRFS_SUPER_INFO_OFFSET) { 1654b3470b5dSDavid Sterba stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start; 16554358d963SJosef Bacik cache->bytes_super += stripe_len; 1656b3470b5dSDavid Sterba ret = btrfs_add_excluded_extent(fs_info, cache->start, 16574358d963SJosef Bacik stripe_len); 16584358d963SJosef Bacik if (ret) 16594358d963SJosef Bacik return ret; 16604358d963SJosef Bacik } 16614358d963SJosef Bacik 16624358d963SJosef Bacik for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { 16634358d963SJosef Bacik bytenr = btrfs_sb_offset(i); 1664b3470b5dSDavid Sterba ret = btrfs_rmap_block(fs_info, cache->start, 16654358d963SJosef Bacik bytenr, &logical, &nr, &stripe_len); 16664358d963SJosef Bacik if (ret) 16674358d963SJosef Bacik return ret; 16684358d963SJosef Bacik 166912659251SNaohiro Aota /* Shouldn't have super stripes in sequential zones */ 167012659251SNaohiro Aota if (zoned && nr) { 167112659251SNaohiro Aota btrfs_err(fs_info, 167212659251SNaohiro Aota "zoned: block group %llu must not contain super block", 167312659251SNaohiro Aota cache->start); 167412659251SNaohiro Aota return -EUCLEAN; 167512659251SNaohiro Aota } 167612659251SNaohiro Aota 16774358d963SJosef Bacik while (nr--) { 167896f9b0f2SNikolay Borisov u64 len = min_t(u64, stripe_len, 167996f9b0f2SNikolay Borisov cache->start + cache->length - logical[nr]); 16804358d963SJosef Bacik 16814358d963SJosef Bacik cache->bytes_super += len; 168296f9b0f2SNikolay Borisov ret = btrfs_add_excluded_extent(fs_info, logical[nr], 168396f9b0f2SNikolay Borisov len); 16844358d963SJosef Bacik if (ret) { 16854358d963SJosef Bacik kfree(logical); 16864358d963SJosef Bacik return ret; 16874358d963SJosef Bacik } 16884358d963SJosef Bacik } 16894358d963SJosef Bacik 16904358d963SJosef Bacik kfree(logical); 16914358d963SJosef Bacik } 16924358d963SJosef Bacik return 0; 16934358d963SJosef Bacik } 16944358d963SJosef Bacik 169532da5386SDavid Sterba static void link_block_group(struct btrfs_block_group *cache) 16964358d963SJosef Bacik { 16974358d963SJosef Bacik struct btrfs_space_info *space_info = cache->space_info; 16984358d963SJosef Bacik int index = btrfs_bg_flags_to_raid_index(cache->flags); 16994358d963SJosef Bacik 17004358d963SJosef Bacik down_write(&space_info->groups_sem); 17014358d963SJosef Bacik list_add_tail(&cache->list, &space_info->block_groups[index]); 17024358d963SJosef Bacik up_write(&space_info->groups_sem); 17034358d963SJosef Bacik } 17044358d963SJosef Bacik 170532da5386SDavid Sterba static struct btrfs_block_group *btrfs_create_block_group_cache( 17069afc6649SQu Wenruo struct btrfs_fs_info *fs_info, u64 start) 17074358d963SJosef Bacik { 170832da5386SDavid Sterba struct btrfs_block_group *cache; 17094358d963SJosef Bacik 17104358d963SJosef Bacik cache = kzalloc(sizeof(*cache), GFP_NOFS); 17114358d963SJosef Bacik if (!cache) 17124358d963SJosef Bacik return NULL; 17134358d963SJosef Bacik 17144358d963SJosef Bacik cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), 17154358d963SJosef Bacik GFP_NOFS); 17164358d963SJosef Bacik if (!cache->free_space_ctl) { 17174358d963SJosef Bacik kfree(cache); 17184358d963SJosef Bacik return NULL; 17194358d963SJosef Bacik } 17204358d963SJosef Bacik 1721b3470b5dSDavid Sterba cache->start = start; 17224358d963SJosef Bacik 17234358d963SJosef Bacik cache->fs_info = fs_info; 17244358d963SJosef Bacik cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start); 17254358d963SJosef Bacik 17266e80d4f8SDennis Zhou cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED; 17276e80d4f8SDennis Zhou 172848aaeebeSJosef Bacik refcount_set(&cache->refs, 1); 17294358d963SJosef Bacik spin_lock_init(&cache->lock); 17304358d963SJosef Bacik init_rwsem(&cache->data_rwsem); 17314358d963SJosef Bacik INIT_LIST_HEAD(&cache->list); 17324358d963SJosef Bacik INIT_LIST_HEAD(&cache->cluster_list); 17334358d963SJosef Bacik INIT_LIST_HEAD(&cache->bg_list); 17344358d963SJosef Bacik INIT_LIST_HEAD(&cache->ro_list); 1735b0643e59SDennis Zhou INIT_LIST_HEAD(&cache->discard_list); 17364358d963SJosef Bacik INIT_LIST_HEAD(&cache->dirty_list); 17374358d963SJosef Bacik INIT_LIST_HEAD(&cache->io_list); 1738cd79909bSJosef Bacik btrfs_init_free_space_ctl(cache, cache->free_space_ctl); 17396b7304afSFilipe Manana atomic_set(&cache->frozen, 0); 17404358d963SJosef Bacik mutex_init(&cache->free_space_lock); 17414358d963SJosef Bacik btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root); 17424358d963SJosef Bacik 17434358d963SJosef Bacik return cache; 17444358d963SJosef Bacik } 17454358d963SJosef Bacik 17464358d963SJosef Bacik /* 17474358d963SJosef Bacik * Iterate all chunks and verify that each of them has the corresponding block 17484358d963SJosef Bacik * group 17494358d963SJosef Bacik */ 17504358d963SJosef Bacik static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info) 17514358d963SJosef Bacik { 17524358d963SJosef Bacik struct extent_map_tree *map_tree = &fs_info->mapping_tree; 17534358d963SJosef Bacik struct extent_map *em; 175432da5386SDavid Sterba struct btrfs_block_group *bg; 17554358d963SJosef Bacik u64 start = 0; 17564358d963SJosef Bacik int ret = 0; 17574358d963SJosef Bacik 17584358d963SJosef Bacik while (1) { 17594358d963SJosef Bacik read_lock(&map_tree->lock); 17604358d963SJosef Bacik /* 17614358d963SJosef Bacik * lookup_extent_mapping will return the first extent map 17624358d963SJosef Bacik * intersecting the range, so setting @len to 1 is enough to 17634358d963SJosef Bacik * get the first chunk. 17644358d963SJosef Bacik */ 17654358d963SJosef Bacik em = lookup_extent_mapping(map_tree, start, 1); 17664358d963SJosef Bacik read_unlock(&map_tree->lock); 17674358d963SJosef Bacik if (!em) 17684358d963SJosef Bacik break; 17694358d963SJosef Bacik 17704358d963SJosef Bacik bg = btrfs_lookup_block_group(fs_info, em->start); 17714358d963SJosef Bacik if (!bg) { 17724358d963SJosef Bacik btrfs_err(fs_info, 17734358d963SJosef Bacik "chunk start=%llu len=%llu doesn't have corresponding block group", 17744358d963SJosef Bacik em->start, em->len); 17754358d963SJosef Bacik ret = -EUCLEAN; 17764358d963SJosef Bacik free_extent_map(em); 17774358d963SJosef Bacik break; 17784358d963SJosef Bacik } 1779b3470b5dSDavid Sterba if (bg->start != em->start || bg->length != em->len || 17804358d963SJosef Bacik (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != 17814358d963SJosef Bacik (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { 17824358d963SJosef Bacik btrfs_err(fs_info, 17834358d963SJosef Bacik "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx", 17844358d963SJosef Bacik em->start, em->len, 17854358d963SJosef Bacik em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK, 1786b3470b5dSDavid Sterba bg->start, bg->length, 17874358d963SJosef Bacik bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK); 17884358d963SJosef Bacik ret = -EUCLEAN; 17894358d963SJosef Bacik free_extent_map(em); 17904358d963SJosef Bacik btrfs_put_block_group(bg); 17914358d963SJosef Bacik break; 17924358d963SJosef Bacik } 17934358d963SJosef Bacik start = em->start + em->len; 17944358d963SJosef Bacik free_extent_map(em); 17954358d963SJosef Bacik btrfs_put_block_group(bg); 17964358d963SJosef Bacik } 17974358d963SJosef Bacik return ret; 17984358d963SJosef Bacik } 17994358d963SJosef Bacik 18004c448ce8SMarcos Paulo de Souza static void read_block_group_item(struct btrfs_block_group *cache, 18019afc6649SQu Wenruo struct btrfs_path *path, 18029afc6649SQu Wenruo const struct btrfs_key *key) 18039afc6649SQu Wenruo { 18049afc6649SQu Wenruo struct extent_buffer *leaf = path->nodes[0]; 18059afc6649SQu Wenruo struct btrfs_block_group_item bgi; 18069afc6649SQu Wenruo int slot = path->slots[0]; 18079afc6649SQu Wenruo 18089afc6649SQu Wenruo cache->length = key->offset; 18099afc6649SQu Wenruo 18109afc6649SQu Wenruo read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot), 18119afc6649SQu Wenruo sizeof(bgi)); 18129afc6649SQu Wenruo cache->used = btrfs_stack_block_group_used(&bgi); 18139afc6649SQu Wenruo cache->flags = btrfs_stack_block_group_flags(&bgi); 18149afc6649SQu Wenruo } 18159afc6649SQu Wenruo 1816ffb9e0f0SQu Wenruo static int read_one_block_group(struct btrfs_fs_info *info, 1817ffb9e0f0SQu Wenruo struct btrfs_path *path, 1818d49a2ddbSQu Wenruo const struct btrfs_key *key, 1819ffb9e0f0SQu Wenruo int need_clear) 1820ffb9e0f0SQu Wenruo { 182132da5386SDavid Sterba struct btrfs_block_group *cache; 1822ffb9e0f0SQu Wenruo struct btrfs_space_info *space_info; 1823ffb9e0f0SQu Wenruo const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS); 1824ffb9e0f0SQu Wenruo int ret; 1825ffb9e0f0SQu Wenruo 1826d49a2ddbSQu Wenruo ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY); 1827ffb9e0f0SQu Wenruo 18289afc6649SQu Wenruo cache = btrfs_create_block_group_cache(info, key->objectid); 1829ffb9e0f0SQu Wenruo if (!cache) 1830ffb9e0f0SQu Wenruo return -ENOMEM; 1831ffb9e0f0SQu Wenruo 18324c448ce8SMarcos Paulo de Souza read_block_group_item(cache, path, key); 18339afc6649SQu Wenruo 1834e3e39c72SMarcos Paulo de Souza set_free_space_tree_thresholds(cache); 1835e3e39c72SMarcos Paulo de Souza 1836ffb9e0f0SQu Wenruo if (need_clear) { 1837ffb9e0f0SQu Wenruo /* 1838ffb9e0f0SQu Wenruo * When we mount with old space cache, we need to 1839ffb9e0f0SQu Wenruo * set BTRFS_DC_CLEAR and set dirty flag. 1840ffb9e0f0SQu Wenruo * 1841ffb9e0f0SQu Wenruo * a) Setting 'BTRFS_DC_CLEAR' makes sure that we 1842ffb9e0f0SQu Wenruo * truncate the old free space cache inode and 1843ffb9e0f0SQu Wenruo * setup a new one. 1844ffb9e0f0SQu Wenruo * b) Setting 'dirty flag' makes sure that we flush 1845ffb9e0f0SQu Wenruo * the new space cache info onto disk. 1846ffb9e0f0SQu Wenruo */ 1847ffb9e0f0SQu Wenruo if (btrfs_test_opt(info, SPACE_CACHE)) 1848ffb9e0f0SQu Wenruo cache->disk_cache_state = BTRFS_DC_CLEAR; 1849ffb9e0f0SQu Wenruo } 1850ffb9e0f0SQu Wenruo if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && 1851ffb9e0f0SQu Wenruo (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { 1852ffb9e0f0SQu Wenruo btrfs_err(info, 1853ffb9e0f0SQu Wenruo "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", 1854ffb9e0f0SQu Wenruo cache->start); 1855ffb9e0f0SQu Wenruo ret = -EINVAL; 1856ffb9e0f0SQu Wenruo goto error; 1857ffb9e0f0SQu Wenruo } 1858ffb9e0f0SQu Wenruo 1859ffb9e0f0SQu Wenruo /* 1860ffb9e0f0SQu Wenruo * We need to exclude the super stripes now so that the space info has 1861ffb9e0f0SQu Wenruo * super bytes accounted for, otherwise we'll think we have more space 1862ffb9e0f0SQu Wenruo * than we actually do. 1863ffb9e0f0SQu Wenruo */ 1864ffb9e0f0SQu Wenruo ret = exclude_super_stripes(cache); 1865ffb9e0f0SQu Wenruo if (ret) { 1866ffb9e0f0SQu Wenruo /* We may have excluded something, so call this just in case. */ 1867ffb9e0f0SQu Wenruo btrfs_free_excluded_extents(cache); 1868ffb9e0f0SQu Wenruo goto error; 1869ffb9e0f0SQu Wenruo } 1870ffb9e0f0SQu Wenruo 1871ffb9e0f0SQu Wenruo /* 1872ffb9e0f0SQu Wenruo * Check for two cases, either we are full, and therefore don't need 1873ffb9e0f0SQu Wenruo * to bother with the caching work since we won't find any space, or we 1874ffb9e0f0SQu Wenruo * are empty, and we can just add all the space in and be done with it. 1875ffb9e0f0SQu Wenruo * This saves us _a_lot_ of time, particularly in the full case. 1876ffb9e0f0SQu Wenruo */ 18779afc6649SQu Wenruo if (cache->length == cache->used) { 1878ffb9e0f0SQu Wenruo cache->last_byte_to_unpin = (u64)-1; 1879ffb9e0f0SQu Wenruo cache->cached = BTRFS_CACHE_FINISHED; 1880ffb9e0f0SQu Wenruo btrfs_free_excluded_extents(cache); 1881ffb9e0f0SQu Wenruo } else if (cache->used == 0) { 1882ffb9e0f0SQu Wenruo cache->last_byte_to_unpin = (u64)-1; 1883ffb9e0f0SQu Wenruo cache->cached = BTRFS_CACHE_FINISHED; 18849afc6649SQu Wenruo add_new_free_space(cache, cache->start, 18859afc6649SQu Wenruo cache->start + cache->length); 1886ffb9e0f0SQu Wenruo btrfs_free_excluded_extents(cache); 1887ffb9e0f0SQu Wenruo } 1888ffb9e0f0SQu Wenruo 1889ffb9e0f0SQu Wenruo ret = btrfs_add_block_group_cache(info, cache); 1890ffb9e0f0SQu Wenruo if (ret) { 1891ffb9e0f0SQu Wenruo btrfs_remove_free_space_cache(cache); 1892ffb9e0f0SQu Wenruo goto error; 1893ffb9e0f0SQu Wenruo } 1894ffb9e0f0SQu Wenruo trace_btrfs_add_block_group(info, cache, 0); 18959afc6649SQu Wenruo btrfs_update_space_info(info, cache->flags, cache->length, 1896ffb9e0f0SQu Wenruo cache->used, cache->bytes_super, &space_info); 1897ffb9e0f0SQu Wenruo 1898ffb9e0f0SQu Wenruo cache->space_info = space_info; 1899ffb9e0f0SQu Wenruo 1900ffb9e0f0SQu Wenruo link_block_group(cache); 1901ffb9e0f0SQu Wenruo 1902ffb9e0f0SQu Wenruo set_avail_alloc_bits(info, cache->flags); 1903ffb9e0f0SQu Wenruo if (btrfs_chunk_readonly(info, cache->start)) { 1904ffb9e0f0SQu Wenruo inc_block_group_ro(cache, 1); 1905ffb9e0f0SQu Wenruo } else if (cache->used == 0) { 1906ffb9e0f0SQu Wenruo ASSERT(list_empty(&cache->bg_list)); 19076e80d4f8SDennis Zhou if (btrfs_test_opt(info, DISCARD_ASYNC)) 19086e80d4f8SDennis Zhou btrfs_discard_queue_work(&info->discard_ctl, cache); 19096e80d4f8SDennis Zhou else 1910ffb9e0f0SQu Wenruo btrfs_mark_bg_unused(cache); 1911ffb9e0f0SQu Wenruo } 1912ffb9e0f0SQu Wenruo return 0; 1913ffb9e0f0SQu Wenruo error: 1914ffb9e0f0SQu Wenruo btrfs_put_block_group(cache); 1915ffb9e0f0SQu Wenruo return ret; 1916ffb9e0f0SQu Wenruo } 1917ffb9e0f0SQu Wenruo 191842437a63SJosef Bacik static int fill_dummy_bgs(struct btrfs_fs_info *fs_info) 191942437a63SJosef Bacik { 192042437a63SJosef Bacik struct extent_map_tree *em_tree = &fs_info->mapping_tree; 192142437a63SJosef Bacik struct btrfs_space_info *space_info; 192242437a63SJosef Bacik struct rb_node *node; 192342437a63SJosef Bacik int ret = 0; 192442437a63SJosef Bacik 192542437a63SJosef Bacik for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) { 192642437a63SJosef Bacik struct extent_map *em; 192742437a63SJosef Bacik struct map_lookup *map; 192842437a63SJosef Bacik struct btrfs_block_group *bg; 192942437a63SJosef Bacik 193042437a63SJosef Bacik em = rb_entry(node, struct extent_map, rb_node); 193142437a63SJosef Bacik map = em->map_lookup; 193242437a63SJosef Bacik bg = btrfs_create_block_group_cache(fs_info, em->start); 193342437a63SJosef Bacik if (!bg) { 193442437a63SJosef Bacik ret = -ENOMEM; 193542437a63SJosef Bacik break; 193642437a63SJosef Bacik } 193742437a63SJosef Bacik 193842437a63SJosef Bacik /* Fill dummy cache as FULL */ 193942437a63SJosef Bacik bg->length = em->len; 194042437a63SJosef Bacik bg->flags = map->type; 194142437a63SJosef Bacik bg->last_byte_to_unpin = (u64)-1; 194242437a63SJosef Bacik bg->cached = BTRFS_CACHE_FINISHED; 194342437a63SJosef Bacik bg->used = em->len; 194442437a63SJosef Bacik bg->flags = map->type; 194542437a63SJosef Bacik ret = btrfs_add_block_group_cache(fs_info, bg); 194642437a63SJosef Bacik if (ret) { 194742437a63SJosef Bacik btrfs_remove_free_space_cache(bg); 194842437a63SJosef Bacik btrfs_put_block_group(bg); 194942437a63SJosef Bacik break; 195042437a63SJosef Bacik } 195142437a63SJosef Bacik btrfs_update_space_info(fs_info, bg->flags, em->len, em->len, 195242437a63SJosef Bacik 0, &space_info); 195342437a63SJosef Bacik bg->space_info = space_info; 195442437a63SJosef Bacik link_block_group(bg); 195542437a63SJosef Bacik 195642437a63SJosef Bacik set_avail_alloc_bits(fs_info, bg->flags); 195742437a63SJosef Bacik } 195842437a63SJosef Bacik if (!ret) 195942437a63SJosef Bacik btrfs_init_global_block_rsv(fs_info); 196042437a63SJosef Bacik return ret; 196142437a63SJosef Bacik } 196242437a63SJosef Bacik 19634358d963SJosef Bacik int btrfs_read_block_groups(struct btrfs_fs_info *info) 19644358d963SJosef Bacik { 19654358d963SJosef Bacik struct btrfs_path *path; 19664358d963SJosef Bacik int ret; 196732da5386SDavid Sterba struct btrfs_block_group *cache; 19684358d963SJosef Bacik struct btrfs_space_info *space_info; 19694358d963SJosef Bacik struct btrfs_key key; 19704358d963SJosef Bacik int need_clear = 0; 19714358d963SJosef Bacik u64 cache_gen; 19724358d963SJosef Bacik 197342437a63SJosef Bacik if (!info->extent_root) 197442437a63SJosef Bacik return fill_dummy_bgs(info); 197542437a63SJosef Bacik 19764358d963SJosef Bacik key.objectid = 0; 19774358d963SJosef Bacik key.offset = 0; 19784358d963SJosef Bacik key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 19794358d963SJosef Bacik path = btrfs_alloc_path(); 19804358d963SJosef Bacik if (!path) 19814358d963SJosef Bacik return -ENOMEM; 19824358d963SJosef Bacik 19834358d963SJosef Bacik cache_gen = btrfs_super_cache_generation(info->super_copy); 19844358d963SJosef Bacik if (btrfs_test_opt(info, SPACE_CACHE) && 19854358d963SJosef Bacik btrfs_super_generation(info->super_copy) != cache_gen) 19864358d963SJosef Bacik need_clear = 1; 19874358d963SJosef Bacik if (btrfs_test_opt(info, CLEAR_CACHE)) 19884358d963SJosef Bacik need_clear = 1; 19894358d963SJosef Bacik 19904358d963SJosef Bacik while (1) { 19914358d963SJosef Bacik ret = find_first_block_group(info, path, &key); 19924358d963SJosef Bacik if (ret > 0) 19934358d963SJosef Bacik break; 19944358d963SJosef Bacik if (ret != 0) 19954358d963SJosef Bacik goto error; 19964358d963SJosef Bacik 1997ffb9e0f0SQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 1998d49a2ddbSQu Wenruo ret = read_one_block_group(info, path, &key, need_clear); 1999ffb9e0f0SQu Wenruo if (ret < 0) 20004358d963SJosef Bacik goto error; 2001ffb9e0f0SQu Wenruo key.objectid += key.offset; 2002ffb9e0f0SQu Wenruo key.offset = 0; 20034358d963SJosef Bacik btrfs_release_path(path); 20044358d963SJosef Bacik } 20057837fa88SJosef Bacik btrfs_release_path(path); 20064358d963SJosef Bacik 200772804905SJosef Bacik list_for_each_entry(space_info, &info->space_info, list) { 200849ea112dSJosef Bacik int i; 200949ea112dSJosef Bacik 201049ea112dSJosef Bacik for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 201149ea112dSJosef Bacik if (list_empty(&space_info->block_groups[i])) 201249ea112dSJosef Bacik continue; 201349ea112dSJosef Bacik cache = list_first_entry(&space_info->block_groups[i], 201449ea112dSJosef Bacik struct btrfs_block_group, 201549ea112dSJosef Bacik list); 201649ea112dSJosef Bacik btrfs_sysfs_add_block_group_type(cache); 201749ea112dSJosef Bacik } 201849ea112dSJosef Bacik 20194358d963SJosef Bacik if (!(btrfs_get_alloc_profile(info, space_info->flags) & 20204358d963SJosef Bacik (BTRFS_BLOCK_GROUP_RAID10 | 20214358d963SJosef Bacik BTRFS_BLOCK_GROUP_RAID1_MASK | 20224358d963SJosef Bacik BTRFS_BLOCK_GROUP_RAID56_MASK | 20234358d963SJosef Bacik BTRFS_BLOCK_GROUP_DUP))) 20244358d963SJosef Bacik continue; 20254358d963SJosef Bacik /* 20264358d963SJosef Bacik * Avoid allocating from un-mirrored block group if there are 20274358d963SJosef Bacik * mirrored block groups. 20284358d963SJosef Bacik */ 20294358d963SJosef Bacik list_for_each_entry(cache, 20304358d963SJosef Bacik &space_info->block_groups[BTRFS_RAID_RAID0], 20314358d963SJosef Bacik list) 2032e11c0406SJosef Bacik inc_block_group_ro(cache, 1); 20334358d963SJosef Bacik list_for_each_entry(cache, 20344358d963SJosef Bacik &space_info->block_groups[BTRFS_RAID_SINGLE], 20354358d963SJosef Bacik list) 2036e11c0406SJosef Bacik inc_block_group_ro(cache, 1); 20374358d963SJosef Bacik } 20384358d963SJosef Bacik 20394358d963SJosef Bacik btrfs_init_global_block_rsv(info); 20404358d963SJosef Bacik ret = check_chunk_block_group_mappings(info); 20414358d963SJosef Bacik error: 20424358d963SJosef Bacik btrfs_free_path(path); 20434358d963SJosef Bacik return ret; 20444358d963SJosef Bacik } 20454358d963SJosef Bacik 204697f4728aSQu Wenruo static int insert_block_group_item(struct btrfs_trans_handle *trans, 204797f4728aSQu Wenruo struct btrfs_block_group *block_group) 204897f4728aSQu Wenruo { 204997f4728aSQu Wenruo struct btrfs_fs_info *fs_info = trans->fs_info; 205097f4728aSQu Wenruo struct btrfs_block_group_item bgi; 205197f4728aSQu Wenruo struct btrfs_root *root; 205297f4728aSQu Wenruo struct btrfs_key key; 205397f4728aSQu Wenruo 205497f4728aSQu Wenruo spin_lock(&block_group->lock); 205597f4728aSQu Wenruo btrfs_set_stack_block_group_used(&bgi, block_group->used); 205697f4728aSQu Wenruo btrfs_set_stack_block_group_chunk_objectid(&bgi, 205797f4728aSQu Wenruo BTRFS_FIRST_CHUNK_TREE_OBJECTID); 205897f4728aSQu Wenruo btrfs_set_stack_block_group_flags(&bgi, block_group->flags); 205997f4728aSQu Wenruo key.objectid = block_group->start; 206097f4728aSQu Wenruo key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 206197f4728aSQu Wenruo key.offset = block_group->length; 206297f4728aSQu Wenruo spin_unlock(&block_group->lock); 206397f4728aSQu Wenruo 206497f4728aSQu Wenruo root = fs_info->extent_root; 206597f4728aSQu Wenruo return btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi)); 206697f4728aSQu Wenruo } 206797f4728aSQu Wenruo 20684358d963SJosef Bacik void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans) 20694358d963SJosef Bacik { 20704358d963SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 207132da5386SDavid Sterba struct btrfs_block_group *block_group; 20724358d963SJosef Bacik int ret = 0; 20734358d963SJosef Bacik 20744358d963SJosef Bacik if (!trans->can_flush_pending_bgs) 20754358d963SJosef Bacik return; 20764358d963SJosef Bacik 20774358d963SJosef Bacik while (!list_empty(&trans->new_bgs)) { 207849ea112dSJosef Bacik int index; 207949ea112dSJosef Bacik 20804358d963SJosef Bacik block_group = list_first_entry(&trans->new_bgs, 208132da5386SDavid Sterba struct btrfs_block_group, 20824358d963SJosef Bacik bg_list); 20834358d963SJosef Bacik if (ret) 20844358d963SJosef Bacik goto next; 20854358d963SJosef Bacik 208649ea112dSJosef Bacik index = btrfs_bg_flags_to_raid_index(block_group->flags); 208749ea112dSJosef Bacik 208897f4728aSQu Wenruo ret = insert_block_group_item(trans, block_group); 20894358d963SJosef Bacik if (ret) 20904358d963SJosef Bacik btrfs_abort_transaction(trans, ret); 209197f4728aSQu Wenruo ret = btrfs_finish_chunk_alloc(trans, block_group->start, 209297f4728aSQu Wenruo block_group->length); 20934358d963SJosef Bacik if (ret) 20944358d963SJosef Bacik btrfs_abort_transaction(trans, ret); 20954358d963SJosef Bacik add_block_group_free_space(trans, block_group); 209649ea112dSJosef Bacik 209749ea112dSJosef Bacik /* 209849ea112dSJosef Bacik * If we restriped during balance, we may have added a new raid 209949ea112dSJosef Bacik * type, so now add the sysfs entries when it is safe to do so. 210049ea112dSJosef Bacik * We don't have to worry about locking here as it's handled in 210149ea112dSJosef Bacik * btrfs_sysfs_add_block_group_type. 210249ea112dSJosef Bacik */ 210349ea112dSJosef Bacik if (block_group->space_info->block_group_kobjs[index] == NULL) 210449ea112dSJosef Bacik btrfs_sysfs_add_block_group_type(block_group); 210549ea112dSJosef Bacik 21064358d963SJosef Bacik /* Already aborted the transaction if it failed. */ 21074358d963SJosef Bacik next: 21084358d963SJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 21094358d963SJosef Bacik list_del_init(&block_group->bg_list); 21104358d963SJosef Bacik } 21114358d963SJosef Bacik btrfs_trans_release_chunk_metadata(trans); 21124358d963SJosef Bacik } 21134358d963SJosef Bacik 21144358d963SJosef Bacik int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used, 21154358d963SJosef Bacik u64 type, u64 chunk_offset, u64 size) 21164358d963SJosef Bacik { 21174358d963SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 211832da5386SDavid Sterba struct btrfs_block_group *cache; 21194358d963SJosef Bacik int ret; 21204358d963SJosef Bacik 21214358d963SJosef Bacik btrfs_set_log_full_commit(trans); 21224358d963SJosef Bacik 21239afc6649SQu Wenruo cache = btrfs_create_block_group_cache(fs_info, chunk_offset); 21244358d963SJosef Bacik if (!cache) 21254358d963SJosef Bacik return -ENOMEM; 21264358d963SJosef Bacik 21279afc6649SQu Wenruo cache->length = size; 2128e3e39c72SMarcos Paulo de Souza set_free_space_tree_thresholds(cache); 2129bf38be65SDavid Sterba cache->used = bytes_used; 21304358d963SJosef Bacik cache->flags = type; 21314358d963SJosef Bacik cache->last_byte_to_unpin = (u64)-1; 21324358d963SJosef Bacik cache->cached = BTRFS_CACHE_FINISHED; 2133997e3e2eSBoris Burkov if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) 21344358d963SJosef Bacik cache->needs_free_space = 1; 21354358d963SJosef Bacik ret = exclude_super_stripes(cache); 21364358d963SJosef Bacik if (ret) { 21374358d963SJosef Bacik /* We may have excluded something, so call this just in case */ 21384358d963SJosef Bacik btrfs_free_excluded_extents(cache); 21394358d963SJosef Bacik btrfs_put_block_group(cache); 21404358d963SJosef Bacik return ret; 21414358d963SJosef Bacik } 21424358d963SJosef Bacik 21434358d963SJosef Bacik add_new_free_space(cache, chunk_offset, chunk_offset + size); 21444358d963SJosef Bacik 21454358d963SJosef Bacik btrfs_free_excluded_extents(cache); 21464358d963SJosef Bacik 21474358d963SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 21484358d963SJosef Bacik if (btrfs_should_fragment_free_space(cache)) { 21494358d963SJosef Bacik u64 new_bytes_used = size - bytes_used; 21504358d963SJosef Bacik 21514358d963SJosef Bacik bytes_used += new_bytes_used >> 1; 2152e11c0406SJosef Bacik fragment_free_space(cache); 21534358d963SJosef Bacik } 21544358d963SJosef Bacik #endif 21554358d963SJosef Bacik /* 21564358d963SJosef Bacik * Ensure the corresponding space_info object is created and 21574358d963SJosef Bacik * assigned to our block group. We want our bg to be added to the rbtree 21584358d963SJosef Bacik * with its ->space_info set. 21594358d963SJosef Bacik */ 21604358d963SJosef Bacik cache->space_info = btrfs_find_space_info(fs_info, cache->flags); 21614358d963SJosef Bacik ASSERT(cache->space_info); 21624358d963SJosef Bacik 21634358d963SJosef Bacik ret = btrfs_add_block_group_cache(fs_info, cache); 21644358d963SJosef Bacik if (ret) { 21654358d963SJosef Bacik btrfs_remove_free_space_cache(cache); 21664358d963SJosef Bacik btrfs_put_block_group(cache); 21674358d963SJosef Bacik return ret; 21684358d963SJosef Bacik } 21694358d963SJosef Bacik 21704358d963SJosef Bacik /* 21714358d963SJosef Bacik * Now that our block group has its ->space_info set and is inserted in 21724358d963SJosef Bacik * the rbtree, update the space info's counters. 21734358d963SJosef Bacik */ 21744358d963SJosef Bacik trace_btrfs_add_block_group(fs_info, cache, 1); 21754358d963SJosef Bacik btrfs_update_space_info(fs_info, cache->flags, size, bytes_used, 21764358d963SJosef Bacik cache->bytes_super, &cache->space_info); 21774358d963SJosef Bacik btrfs_update_global_block_rsv(fs_info); 21784358d963SJosef Bacik 21794358d963SJosef Bacik link_block_group(cache); 21804358d963SJosef Bacik 21814358d963SJosef Bacik list_add_tail(&cache->bg_list, &trans->new_bgs); 21824358d963SJosef Bacik trans->delayed_ref_updates++; 21834358d963SJosef Bacik btrfs_update_delayed_refs_rsv(trans); 21844358d963SJosef Bacik 21854358d963SJosef Bacik set_avail_alloc_bits(fs_info, type); 21864358d963SJosef Bacik return 0; 21874358d963SJosef Bacik } 218826ce2095SJosef Bacik 2189b12de528SQu Wenruo /* 2190b12de528SQu Wenruo * Mark one block group RO, can be called several times for the same block 2191b12de528SQu Wenruo * group. 2192b12de528SQu Wenruo * 2193b12de528SQu Wenruo * @cache: the destination block group 2194b12de528SQu Wenruo * @do_chunk_alloc: whether need to do chunk pre-allocation, this is to 2195b12de528SQu Wenruo * ensure we still have some free space after marking this 2196b12de528SQu Wenruo * block group RO. 2197b12de528SQu Wenruo */ 2198b12de528SQu Wenruo int btrfs_inc_block_group_ro(struct btrfs_block_group *cache, 2199b12de528SQu Wenruo bool do_chunk_alloc) 220026ce2095SJosef Bacik { 220126ce2095SJosef Bacik struct btrfs_fs_info *fs_info = cache->fs_info; 220226ce2095SJosef Bacik struct btrfs_trans_handle *trans; 220326ce2095SJosef Bacik u64 alloc_flags; 220426ce2095SJosef Bacik int ret; 220526ce2095SJosef Bacik 220626ce2095SJosef Bacik again: 220726ce2095SJosef Bacik trans = btrfs_join_transaction(fs_info->extent_root); 220826ce2095SJosef Bacik if (IS_ERR(trans)) 220926ce2095SJosef Bacik return PTR_ERR(trans); 221026ce2095SJosef Bacik 221126ce2095SJosef Bacik /* 221226ce2095SJosef Bacik * we're not allowed to set block groups readonly after the dirty 221326ce2095SJosef Bacik * block groups cache has started writing. If it already started, 221426ce2095SJosef Bacik * back off and let this transaction commit 221526ce2095SJosef Bacik */ 221626ce2095SJosef Bacik mutex_lock(&fs_info->ro_block_group_mutex); 221726ce2095SJosef Bacik if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { 221826ce2095SJosef Bacik u64 transid = trans->transid; 221926ce2095SJosef Bacik 222026ce2095SJosef Bacik mutex_unlock(&fs_info->ro_block_group_mutex); 222126ce2095SJosef Bacik btrfs_end_transaction(trans); 222226ce2095SJosef Bacik 222326ce2095SJosef Bacik ret = btrfs_wait_for_commit(fs_info, transid); 222426ce2095SJosef Bacik if (ret) 222526ce2095SJosef Bacik return ret; 222626ce2095SJosef Bacik goto again; 222726ce2095SJosef Bacik } 222826ce2095SJosef Bacik 2229b12de528SQu Wenruo if (do_chunk_alloc) { 223026ce2095SJosef Bacik /* 2231b12de528SQu Wenruo * If we are changing raid levels, try to allocate a 2232b12de528SQu Wenruo * corresponding block group with the new raid level. 223326ce2095SJosef Bacik */ 2234349e120eSJosef Bacik alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); 223526ce2095SJosef Bacik if (alloc_flags != cache->flags) { 2236b12de528SQu Wenruo ret = btrfs_chunk_alloc(trans, alloc_flags, 2237b12de528SQu Wenruo CHUNK_ALLOC_FORCE); 223826ce2095SJosef Bacik /* 223926ce2095SJosef Bacik * ENOSPC is allowed here, we may have enough space 2240b12de528SQu Wenruo * already allocated at the new raid level to carry on 224126ce2095SJosef Bacik */ 224226ce2095SJosef Bacik if (ret == -ENOSPC) 224326ce2095SJosef Bacik ret = 0; 224426ce2095SJosef Bacik if (ret < 0) 224526ce2095SJosef Bacik goto out; 224626ce2095SJosef Bacik } 2247b12de528SQu Wenruo } 224826ce2095SJosef Bacik 2249a7a63accSJosef Bacik ret = inc_block_group_ro(cache, 0); 2250b12de528SQu Wenruo if (!do_chunk_alloc) 2251b12de528SQu Wenruo goto unlock_out; 225226ce2095SJosef Bacik if (!ret) 225326ce2095SJosef Bacik goto out; 225426ce2095SJosef Bacik alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags); 225526ce2095SJosef Bacik ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE); 225626ce2095SJosef Bacik if (ret < 0) 225726ce2095SJosef Bacik goto out; 2258e11c0406SJosef Bacik ret = inc_block_group_ro(cache, 0); 225926ce2095SJosef Bacik out: 226026ce2095SJosef Bacik if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { 2261349e120eSJosef Bacik alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); 226226ce2095SJosef Bacik mutex_lock(&fs_info->chunk_mutex); 226326ce2095SJosef Bacik check_system_chunk(trans, alloc_flags); 226426ce2095SJosef Bacik mutex_unlock(&fs_info->chunk_mutex); 226526ce2095SJosef Bacik } 2266b12de528SQu Wenruo unlock_out: 226726ce2095SJosef Bacik mutex_unlock(&fs_info->ro_block_group_mutex); 226826ce2095SJosef Bacik 226926ce2095SJosef Bacik btrfs_end_transaction(trans); 227026ce2095SJosef Bacik return ret; 227126ce2095SJosef Bacik } 227226ce2095SJosef Bacik 227332da5386SDavid Sterba void btrfs_dec_block_group_ro(struct btrfs_block_group *cache) 227426ce2095SJosef Bacik { 227526ce2095SJosef Bacik struct btrfs_space_info *sinfo = cache->space_info; 227626ce2095SJosef Bacik u64 num_bytes; 227726ce2095SJosef Bacik 227826ce2095SJosef Bacik BUG_ON(!cache->ro); 227926ce2095SJosef Bacik 228026ce2095SJosef Bacik spin_lock(&sinfo->lock); 228126ce2095SJosef Bacik spin_lock(&cache->lock); 228226ce2095SJosef Bacik if (!--cache->ro) { 2283b3470b5dSDavid Sterba num_bytes = cache->length - cache->reserved - 2284bf38be65SDavid Sterba cache->pinned - cache->bytes_super - cache->used; 228526ce2095SJosef Bacik sinfo->bytes_readonly -= num_bytes; 228626ce2095SJosef Bacik list_del_init(&cache->ro_list); 228726ce2095SJosef Bacik } 228826ce2095SJosef Bacik spin_unlock(&cache->lock); 228926ce2095SJosef Bacik spin_unlock(&sinfo->lock); 229026ce2095SJosef Bacik } 229177745c05SJosef Bacik 22923be4d8efSQu Wenruo static int update_block_group_item(struct btrfs_trans_handle *trans, 229377745c05SJosef Bacik struct btrfs_path *path, 229432da5386SDavid Sterba struct btrfs_block_group *cache) 229577745c05SJosef Bacik { 229677745c05SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 229777745c05SJosef Bacik int ret; 22983be4d8efSQu Wenruo struct btrfs_root *root = fs_info->extent_root; 229977745c05SJosef Bacik unsigned long bi; 230077745c05SJosef Bacik struct extent_buffer *leaf; 2301bf38be65SDavid Sterba struct btrfs_block_group_item bgi; 2302b3470b5dSDavid Sterba struct btrfs_key key; 230377745c05SJosef Bacik 2304b3470b5dSDavid Sterba key.objectid = cache->start; 2305b3470b5dSDavid Sterba key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 2306b3470b5dSDavid Sterba key.offset = cache->length; 2307b3470b5dSDavid Sterba 23083be4d8efSQu Wenruo ret = btrfs_search_slot(trans, root, &key, path, 0, 1); 230977745c05SJosef Bacik if (ret) { 231077745c05SJosef Bacik if (ret > 0) 231177745c05SJosef Bacik ret = -ENOENT; 231277745c05SJosef Bacik goto fail; 231377745c05SJosef Bacik } 231477745c05SJosef Bacik 231577745c05SJosef Bacik leaf = path->nodes[0]; 231677745c05SJosef Bacik bi = btrfs_item_ptr_offset(leaf, path->slots[0]); 2317de0dc456SDavid Sterba btrfs_set_stack_block_group_used(&bgi, cache->used); 2318de0dc456SDavid Sterba btrfs_set_stack_block_group_chunk_objectid(&bgi, 23193d976388SDavid Sterba BTRFS_FIRST_CHUNK_TREE_OBJECTID); 2320de0dc456SDavid Sterba btrfs_set_stack_block_group_flags(&bgi, cache->flags); 2321bf38be65SDavid Sterba write_extent_buffer(leaf, &bgi, bi, sizeof(bgi)); 232277745c05SJosef Bacik btrfs_mark_buffer_dirty(leaf); 232377745c05SJosef Bacik fail: 232477745c05SJosef Bacik btrfs_release_path(path); 232577745c05SJosef Bacik return ret; 232677745c05SJosef Bacik 232777745c05SJosef Bacik } 232877745c05SJosef Bacik 232932da5386SDavid Sterba static int cache_save_setup(struct btrfs_block_group *block_group, 233077745c05SJosef Bacik struct btrfs_trans_handle *trans, 233177745c05SJosef Bacik struct btrfs_path *path) 233277745c05SJosef Bacik { 233377745c05SJosef Bacik struct btrfs_fs_info *fs_info = block_group->fs_info; 233477745c05SJosef Bacik struct btrfs_root *root = fs_info->tree_root; 233577745c05SJosef Bacik struct inode *inode = NULL; 233677745c05SJosef Bacik struct extent_changeset *data_reserved = NULL; 233777745c05SJosef Bacik u64 alloc_hint = 0; 233877745c05SJosef Bacik int dcs = BTRFS_DC_ERROR; 233977745c05SJosef Bacik u64 num_pages = 0; 234077745c05SJosef Bacik int retries = 0; 234177745c05SJosef Bacik int ret = 0; 234277745c05SJosef Bacik 2343af456a2cSBoris Burkov if (!btrfs_test_opt(fs_info, SPACE_CACHE)) 2344af456a2cSBoris Burkov return 0; 2345af456a2cSBoris Burkov 234677745c05SJosef Bacik /* 234777745c05SJosef Bacik * If this block group is smaller than 100 megs don't bother caching the 234877745c05SJosef Bacik * block group. 234977745c05SJosef Bacik */ 2350b3470b5dSDavid Sterba if (block_group->length < (100 * SZ_1M)) { 235177745c05SJosef Bacik spin_lock(&block_group->lock); 235277745c05SJosef Bacik block_group->disk_cache_state = BTRFS_DC_WRITTEN; 235377745c05SJosef Bacik spin_unlock(&block_group->lock); 235477745c05SJosef Bacik return 0; 235577745c05SJosef Bacik } 235677745c05SJosef Bacik 2357bf31f87fSDavid Sterba if (TRANS_ABORTED(trans)) 235877745c05SJosef Bacik return 0; 235977745c05SJosef Bacik again: 236077745c05SJosef Bacik inode = lookup_free_space_inode(block_group, path); 236177745c05SJosef Bacik if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { 236277745c05SJosef Bacik ret = PTR_ERR(inode); 236377745c05SJosef Bacik btrfs_release_path(path); 236477745c05SJosef Bacik goto out; 236577745c05SJosef Bacik } 236677745c05SJosef Bacik 236777745c05SJosef Bacik if (IS_ERR(inode)) { 236877745c05SJosef Bacik BUG_ON(retries); 236977745c05SJosef Bacik retries++; 237077745c05SJosef Bacik 237177745c05SJosef Bacik if (block_group->ro) 237277745c05SJosef Bacik goto out_free; 237377745c05SJosef Bacik 237477745c05SJosef Bacik ret = create_free_space_inode(trans, block_group, path); 237577745c05SJosef Bacik if (ret) 237677745c05SJosef Bacik goto out_free; 237777745c05SJosef Bacik goto again; 237877745c05SJosef Bacik } 237977745c05SJosef Bacik 238077745c05SJosef Bacik /* 238177745c05SJosef Bacik * We want to set the generation to 0, that way if anything goes wrong 238277745c05SJosef Bacik * from here on out we know not to trust this cache when we load up next 238377745c05SJosef Bacik * time. 238477745c05SJosef Bacik */ 238577745c05SJosef Bacik BTRFS_I(inode)->generation = 0; 23869a56fcd1SNikolay Borisov ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); 238777745c05SJosef Bacik if (ret) { 238877745c05SJosef Bacik /* 238977745c05SJosef Bacik * So theoretically we could recover from this, simply set the 239077745c05SJosef Bacik * super cache generation to 0 so we know to invalidate the 239177745c05SJosef Bacik * cache, but then we'd have to keep track of the block groups 239277745c05SJosef Bacik * that fail this way so we know we _have_ to reset this cache 239377745c05SJosef Bacik * before the next commit or risk reading stale cache. So to 239477745c05SJosef Bacik * limit our exposure to horrible edge cases lets just abort the 239577745c05SJosef Bacik * transaction, this only happens in really bad situations 239677745c05SJosef Bacik * anyway. 239777745c05SJosef Bacik */ 239877745c05SJosef Bacik btrfs_abort_transaction(trans, ret); 239977745c05SJosef Bacik goto out_put; 240077745c05SJosef Bacik } 240177745c05SJosef Bacik WARN_ON(ret); 240277745c05SJosef Bacik 240377745c05SJosef Bacik /* We've already setup this transaction, go ahead and exit */ 240477745c05SJosef Bacik if (block_group->cache_generation == trans->transid && 240577745c05SJosef Bacik i_size_read(inode)) { 240677745c05SJosef Bacik dcs = BTRFS_DC_SETUP; 240777745c05SJosef Bacik goto out_put; 240877745c05SJosef Bacik } 240977745c05SJosef Bacik 241077745c05SJosef Bacik if (i_size_read(inode) > 0) { 241177745c05SJosef Bacik ret = btrfs_check_trunc_cache_free_space(fs_info, 241277745c05SJosef Bacik &fs_info->global_block_rsv); 241377745c05SJosef Bacik if (ret) 241477745c05SJosef Bacik goto out_put; 241577745c05SJosef Bacik 241677745c05SJosef Bacik ret = btrfs_truncate_free_space_cache(trans, NULL, inode); 241777745c05SJosef Bacik if (ret) 241877745c05SJosef Bacik goto out_put; 241977745c05SJosef Bacik } 242077745c05SJosef Bacik 242177745c05SJosef Bacik spin_lock(&block_group->lock); 242277745c05SJosef Bacik if (block_group->cached != BTRFS_CACHE_FINISHED || 242377745c05SJosef Bacik !btrfs_test_opt(fs_info, SPACE_CACHE)) { 242477745c05SJosef Bacik /* 242577745c05SJosef Bacik * don't bother trying to write stuff out _if_ 242677745c05SJosef Bacik * a) we're not cached, 242777745c05SJosef Bacik * b) we're with nospace_cache mount option, 242877745c05SJosef Bacik * c) we're with v2 space_cache (FREE_SPACE_TREE). 242977745c05SJosef Bacik */ 243077745c05SJosef Bacik dcs = BTRFS_DC_WRITTEN; 243177745c05SJosef Bacik spin_unlock(&block_group->lock); 243277745c05SJosef Bacik goto out_put; 243377745c05SJosef Bacik } 243477745c05SJosef Bacik spin_unlock(&block_group->lock); 243577745c05SJosef Bacik 243677745c05SJosef Bacik /* 243777745c05SJosef Bacik * We hit an ENOSPC when setting up the cache in this transaction, just 243877745c05SJosef Bacik * skip doing the setup, we've already cleared the cache so we're safe. 243977745c05SJosef Bacik */ 244077745c05SJosef Bacik if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { 244177745c05SJosef Bacik ret = -ENOSPC; 244277745c05SJosef Bacik goto out_put; 244377745c05SJosef Bacik } 244477745c05SJosef Bacik 244577745c05SJosef Bacik /* 244677745c05SJosef Bacik * Try to preallocate enough space based on how big the block group is. 244777745c05SJosef Bacik * Keep in mind this has to include any pinned space which could end up 244877745c05SJosef Bacik * taking up quite a bit since it's not folded into the other space 244977745c05SJosef Bacik * cache. 245077745c05SJosef Bacik */ 2451b3470b5dSDavid Sterba num_pages = div_u64(block_group->length, SZ_256M); 245277745c05SJosef Bacik if (!num_pages) 245377745c05SJosef Bacik num_pages = 1; 245477745c05SJosef Bacik 245577745c05SJosef Bacik num_pages *= 16; 245677745c05SJosef Bacik num_pages *= PAGE_SIZE; 245777745c05SJosef Bacik 245836ea6f3eSNikolay Borisov ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, 0, 245936ea6f3eSNikolay Borisov num_pages); 246077745c05SJosef Bacik if (ret) 246177745c05SJosef Bacik goto out_put; 246277745c05SJosef Bacik 246377745c05SJosef Bacik ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, 246477745c05SJosef Bacik num_pages, num_pages, 246577745c05SJosef Bacik &alloc_hint); 246677745c05SJosef Bacik /* 246777745c05SJosef Bacik * Our cache requires contiguous chunks so that we don't modify a bunch 246877745c05SJosef Bacik * of metadata or split extents when writing the cache out, which means 246977745c05SJosef Bacik * we can enospc if we are heavily fragmented in addition to just normal 247077745c05SJosef Bacik * out of space conditions. So if we hit this just skip setting up any 247177745c05SJosef Bacik * other block groups for this transaction, maybe we'll unpin enough 247277745c05SJosef Bacik * space the next time around. 247377745c05SJosef Bacik */ 247477745c05SJosef Bacik if (!ret) 247577745c05SJosef Bacik dcs = BTRFS_DC_SETUP; 247677745c05SJosef Bacik else if (ret == -ENOSPC) 247777745c05SJosef Bacik set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); 247877745c05SJosef Bacik 247977745c05SJosef Bacik out_put: 248077745c05SJosef Bacik iput(inode); 248177745c05SJosef Bacik out_free: 248277745c05SJosef Bacik btrfs_release_path(path); 248377745c05SJosef Bacik out: 248477745c05SJosef Bacik spin_lock(&block_group->lock); 248577745c05SJosef Bacik if (!ret && dcs == BTRFS_DC_SETUP) 248677745c05SJosef Bacik block_group->cache_generation = trans->transid; 248777745c05SJosef Bacik block_group->disk_cache_state = dcs; 248877745c05SJosef Bacik spin_unlock(&block_group->lock); 248977745c05SJosef Bacik 249077745c05SJosef Bacik extent_changeset_free(data_reserved); 249177745c05SJosef Bacik return ret; 249277745c05SJosef Bacik } 249377745c05SJosef Bacik 249477745c05SJosef Bacik int btrfs_setup_space_cache(struct btrfs_trans_handle *trans) 249577745c05SJosef Bacik { 249677745c05SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 249732da5386SDavid Sterba struct btrfs_block_group *cache, *tmp; 249877745c05SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 249977745c05SJosef Bacik struct btrfs_path *path; 250077745c05SJosef Bacik 250177745c05SJosef Bacik if (list_empty(&cur_trans->dirty_bgs) || 250277745c05SJosef Bacik !btrfs_test_opt(fs_info, SPACE_CACHE)) 250377745c05SJosef Bacik return 0; 250477745c05SJosef Bacik 250577745c05SJosef Bacik path = btrfs_alloc_path(); 250677745c05SJosef Bacik if (!path) 250777745c05SJosef Bacik return -ENOMEM; 250877745c05SJosef Bacik 250977745c05SJosef Bacik /* Could add new block groups, use _safe just in case */ 251077745c05SJosef Bacik list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, 251177745c05SJosef Bacik dirty_list) { 251277745c05SJosef Bacik if (cache->disk_cache_state == BTRFS_DC_CLEAR) 251377745c05SJosef Bacik cache_save_setup(cache, trans, path); 251477745c05SJosef Bacik } 251577745c05SJosef Bacik 251677745c05SJosef Bacik btrfs_free_path(path); 251777745c05SJosef Bacik return 0; 251877745c05SJosef Bacik } 251977745c05SJosef Bacik 252077745c05SJosef Bacik /* 252177745c05SJosef Bacik * Transaction commit does final block group cache writeback during a critical 252277745c05SJosef Bacik * section where nothing is allowed to change the FS. This is required in 252377745c05SJosef Bacik * order for the cache to actually match the block group, but can introduce a 252477745c05SJosef Bacik * lot of latency into the commit. 252577745c05SJosef Bacik * 252677745c05SJosef Bacik * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO. 252777745c05SJosef Bacik * There's a chance we'll have to redo some of it if the block group changes 252877745c05SJosef Bacik * again during the commit, but it greatly reduces the commit latency by 252977745c05SJosef Bacik * getting rid of the easy block groups while we're still allowing others to 253077745c05SJosef Bacik * join the commit. 253177745c05SJosef Bacik */ 253277745c05SJosef Bacik int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans) 253377745c05SJosef Bacik { 253477745c05SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 253532da5386SDavid Sterba struct btrfs_block_group *cache; 253677745c05SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 253777745c05SJosef Bacik int ret = 0; 253877745c05SJosef Bacik int should_put; 253977745c05SJosef Bacik struct btrfs_path *path = NULL; 254077745c05SJosef Bacik LIST_HEAD(dirty); 254177745c05SJosef Bacik struct list_head *io = &cur_trans->io_bgs; 254277745c05SJosef Bacik int num_started = 0; 254377745c05SJosef Bacik int loops = 0; 254477745c05SJosef Bacik 254577745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 254677745c05SJosef Bacik if (list_empty(&cur_trans->dirty_bgs)) { 254777745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 254877745c05SJosef Bacik return 0; 254977745c05SJosef Bacik } 255077745c05SJosef Bacik list_splice_init(&cur_trans->dirty_bgs, &dirty); 255177745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 255277745c05SJosef Bacik 255377745c05SJosef Bacik again: 255477745c05SJosef Bacik /* Make sure all the block groups on our dirty list actually exist */ 255577745c05SJosef Bacik btrfs_create_pending_block_groups(trans); 255677745c05SJosef Bacik 255777745c05SJosef Bacik if (!path) { 255877745c05SJosef Bacik path = btrfs_alloc_path(); 255977745c05SJosef Bacik if (!path) 256077745c05SJosef Bacik return -ENOMEM; 256177745c05SJosef Bacik } 256277745c05SJosef Bacik 256377745c05SJosef Bacik /* 256477745c05SJosef Bacik * cache_write_mutex is here only to save us from balance or automatic 256577745c05SJosef Bacik * removal of empty block groups deleting this block group while we are 256677745c05SJosef Bacik * writing out the cache 256777745c05SJosef Bacik */ 256877745c05SJosef Bacik mutex_lock(&trans->transaction->cache_write_mutex); 256977745c05SJosef Bacik while (!list_empty(&dirty)) { 257077745c05SJosef Bacik bool drop_reserve = true; 257177745c05SJosef Bacik 257232da5386SDavid Sterba cache = list_first_entry(&dirty, struct btrfs_block_group, 257377745c05SJosef Bacik dirty_list); 257477745c05SJosef Bacik /* 257577745c05SJosef Bacik * This can happen if something re-dirties a block group that 257677745c05SJosef Bacik * is already under IO. Just wait for it to finish and then do 257777745c05SJosef Bacik * it all again 257877745c05SJosef Bacik */ 257977745c05SJosef Bacik if (!list_empty(&cache->io_list)) { 258077745c05SJosef Bacik list_del_init(&cache->io_list); 258177745c05SJosef Bacik btrfs_wait_cache_io(trans, cache, path); 258277745c05SJosef Bacik btrfs_put_block_group(cache); 258377745c05SJosef Bacik } 258477745c05SJosef Bacik 258577745c05SJosef Bacik 258677745c05SJosef Bacik /* 258777745c05SJosef Bacik * btrfs_wait_cache_io uses the cache->dirty_list to decide if 258877745c05SJosef Bacik * it should update the cache_state. Don't delete until after 258977745c05SJosef Bacik * we wait. 259077745c05SJosef Bacik * 259177745c05SJosef Bacik * Since we're not running in the commit critical section 259277745c05SJosef Bacik * we need the dirty_bgs_lock to protect from update_block_group 259377745c05SJosef Bacik */ 259477745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 259577745c05SJosef Bacik list_del_init(&cache->dirty_list); 259677745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 259777745c05SJosef Bacik 259877745c05SJosef Bacik should_put = 1; 259977745c05SJosef Bacik 260077745c05SJosef Bacik cache_save_setup(cache, trans, path); 260177745c05SJosef Bacik 260277745c05SJosef Bacik if (cache->disk_cache_state == BTRFS_DC_SETUP) { 260377745c05SJosef Bacik cache->io_ctl.inode = NULL; 260477745c05SJosef Bacik ret = btrfs_write_out_cache(trans, cache, path); 260577745c05SJosef Bacik if (ret == 0 && cache->io_ctl.inode) { 260677745c05SJosef Bacik num_started++; 260777745c05SJosef Bacik should_put = 0; 260877745c05SJosef Bacik 260977745c05SJosef Bacik /* 261077745c05SJosef Bacik * The cache_write_mutex is protecting the 261177745c05SJosef Bacik * io_list, also refer to the definition of 261277745c05SJosef Bacik * btrfs_transaction::io_bgs for more details 261377745c05SJosef Bacik */ 261477745c05SJosef Bacik list_add_tail(&cache->io_list, io); 261577745c05SJosef Bacik } else { 261677745c05SJosef Bacik /* 261777745c05SJosef Bacik * If we failed to write the cache, the 261877745c05SJosef Bacik * generation will be bad and life goes on 261977745c05SJosef Bacik */ 262077745c05SJosef Bacik ret = 0; 262177745c05SJosef Bacik } 262277745c05SJosef Bacik } 262377745c05SJosef Bacik if (!ret) { 26243be4d8efSQu Wenruo ret = update_block_group_item(trans, path, cache); 262577745c05SJosef Bacik /* 262677745c05SJosef Bacik * Our block group might still be attached to the list 262777745c05SJosef Bacik * of new block groups in the transaction handle of some 262877745c05SJosef Bacik * other task (struct btrfs_trans_handle->new_bgs). This 262977745c05SJosef Bacik * means its block group item isn't yet in the extent 263077745c05SJosef Bacik * tree. If this happens ignore the error, as we will 263177745c05SJosef Bacik * try again later in the critical section of the 263277745c05SJosef Bacik * transaction commit. 263377745c05SJosef Bacik */ 263477745c05SJosef Bacik if (ret == -ENOENT) { 263577745c05SJosef Bacik ret = 0; 263677745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 263777745c05SJosef Bacik if (list_empty(&cache->dirty_list)) { 263877745c05SJosef Bacik list_add_tail(&cache->dirty_list, 263977745c05SJosef Bacik &cur_trans->dirty_bgs); 264077745c05SJosef Bacik btrfs_get_block_group(cache); 264177745c05SJosef Bacik drop_reserve = false; 264277745c05SJosef Bacik } 264377745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 264477745c05SJosef Bacik } else if (ret) { 264577745c05SJosef Bacik btrfs_abort_transaction(trans, ret); 264677745c05SJosef Bacik } 264777745c05SJosef Bacik } 264877745c05SJosef Bacik 264977745c05SJosef Bacik /* If it's not on the io list, we need to put the block group */ 265077745c05SJosef Bacik if (should_put) 265177745c05SJosef Bacik btrfs_put_block_group(cache); 265277745c05SJosef Bacik if (drop_reserve) 265377745c05SJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 265477745c05SJosef Bacik 265577745c05SJosef Bacik if (ret) 265677745c05SJosef Bacik break; 265777745c05SJosef Bacik 265877745c05SJosef Bacik /* 265977745c05SJosef Bacik * Avoid blocking other tasks for too long. It might even save 266077745c05SJosef Bacik * us from writing caches for block groups that are going to be 266177745c05SJosef Bacik * removed. 266277745c05SJosef Bacik */ 266377745c05SJosef Bacik mutex_unlock(&trans->transaction->cache_write_mutex); 266477745c05SJosef Bacik mutex_lock(&trans->transaction->cache_write_mutex); 266577745c05SJosef Bacik } 266677745c05SJosef Bacik mutex_unlock(&trans->transaction->cache_write_mutex); 266777745c05SJosef Bacik 266877745c05SJosef Bacik /* 266977745c05SJosef Bacik * Go through delayed refs for all the stuff we've just kicked off 267077745c05SJosef Bacik * and then loop back (just once) 267177745c05SJosef Bacik */ 2672*34d1eb0eSJosef Bacik if (!ret) 267377745c05SJosef Bacik ret = btrfs_run_delayed_refs(trans, 0); 267477745c05SJosef Bacik if (!ret && loops == 0) { 267577745c05SJosef Bacik loops++; 267677745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 267777745c05SJosef Bacik list_splice_init(&cur_trans->dirty_bgs, &dirty); 267877745c05SJosef Bacik /* 267977745c05SJosef Bacik * dirty_bgs_lock protects us from concurrent block group 268077745c05SJosef Bacik * deletes too (not just cache_write_mutex). 268177745c05SJosef Bacik */ 268277745c05SJosef Bacik if (!list_empty(&dirty)) { 268377745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 268477745c05SJosef Bacik goto again; 268577745c05SJosef Bacik } 268677745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 268777745c05SJosef Bacik } else if (ret < 0) { 268877745c05SJosef Bacik btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 268977745c05SJosef Bacik } 269077745c05SJosef Bacik 269177745c05SJosef Bacik btrfs_free_path(path); 269277745c05SJosef Bacik return ret; 269377745c05SJosef Bacik } 269477745c05SJosef Bacik 269577745c05SJosef Bacik int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) 269677745c05SJosef Bacik { 269777745c05SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 269832da5386SDavid Sterba struct btrfs_block_group *cache; 269977745c05SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 270077745c05SJosef Bacik int ret = 0; 270177745c05SJosef Bacik int should_put; 270277745c05SJosef Bacik struct btrfs_path *path; 270377745c05SJosef Bacik struct list_head *io = &cur_trans->io_bgs; 270477745c05SJosef Bacik int num_started = 0; 270577745c05SJosef Bacik 270677745c05SJosef Bacik path = btrfs_alloc_path(); 270777745c05SJosef Bacik if (!path) 270877745c05SJosef Bacik return -ENOMEM; 270977745c05SJosef Bacik 271077745c05SJosef Bacik /* 271177745c05SJosef Bacik * Even though we are in the critical section of the transaction commit, 271277745c05SJosef Bacik * we can still have concurrent tasks adding elements to this 271377745c05SJosef Bacik * transaction's list of dirty block groups. These tasks correspond to 271477745c05SJosef Bacik * endio free space workers started when writeback finishes for a 271577745c05SJosef Bacik * space cache, which run inode.c:btrfs_finish_ordered_io(), and can 271677745c05SJosef Bacik * allocate new block groups as a result of COWing nodes of the root 271777745c05SJosef Bacik * tree when updating the free space inode. The writeback for the space 271877745c05SJosef Bacik * caches is triggered by an earlier call to 271977745c05SJosef Bacik * btrfs_start_dirty_block_groups() and iterations of the following 272077745c05SJosef Bacik * loop. 272177745c05SJosef Bacik * Also we want to do the cache_save_setup first and then run the 272277745c05SJosef Bacik * delayed refs to make sure we have the best chance at doing this all 272377745c05SJosef Bacik * in one shot. 272477745c05SJosef Bacik */ 272577745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 272677745c05SJosef Bacik while (!list_empty(&cur_trans->dirty_bgs)) { 272777745c05SJosef Bacik cache = list_first_entry(&cur_trans->dirty_bgs, 272832da5386SDavid Sterba struct btrfs_block_group, 272977745c05SJosef Bacik dirty_list); 273077745c05SJosef Bacik 273177745c05SJosef Bacik /* 273277745c05SJosef Bacik * This can happen if cache_save_setup re-dirties a block group 273377745c05SJosef Bacik * that is already under IO. Just wait for it to finish and 273477745c05SJosef Bacik * then do it all again 273577745c05SJosef Bacik */ 273677745c05SJosef Bacik if (!list_empty(&cache->io_list)) { 273777745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 273877745c05SJosef Bacik list_del_init(&cache->io_list); 273977745c05SJosef Bacik btrfs_wait_cache_io(trans, cache, path); 274077745c05SJosef Bacik btrfs_put_block_group(cache); 274177745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 274277745c05SJosef Bacik } 274377745c05SJosef Bacik 274477745c05SJosef Bacik /* 274577745c05SJosef Bacik * Don't remove from the dirty list until after we've waited on 274677745c05SJosef Bacik * any pending IO 274777745c05SJosef Bacik */ 274877745c05SJosef Bacik list_del_init(&cache->dirty_list); 274977745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 275077745c05SJosef Bacik should_put = 1; 275177745c05SJosef Bacik 275277745c05SJosef Bacik cache_save_setup(cache, trans, path); 275377745c05SJosef Bacik 275477745c05SJosef Bacik if (!ret) 275577745c05SJosef Bacik ret = btrfs_run_delayed_refs(trans, 275677745c05SJosef Bacik (unsigned long) -1); 275777745c05SJosef Bacik 275877745c05SJosef Bacik if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { 275977745c05SJosef Bacik cache->io_ctl.inode = NULL; 276077745c05SJosef Bacik ret = btrfs_write_out_cache(trans, cache, path); 276177745c05SJosef Bacik if (ret == 0 && cache->io_ctl.inode) { 276277745c05SJosef Bacik num_started++; 276377745c05SJosef Bacik should_put = 0; 276477745c05SJosef Bacik list_add_tail(&cache->io_list, io); 276577745c05SJosef Bacik } else { 276677745c05SJosef Bacik /* 276777745c05SJosef Bacik * If we failed to write the cache, the 276877745c05SJosef Bacik * generation will be bad and life goes on 276977745c05SJosef Bacik */ 277077745c05SJosef Bacik ret = 0; 277177745c05SJosef Bacik } 277277745c05SJosef Bacik } 277377745c05SJosef Bacik if (!ret) { 27743be4d8efSQu Wenruo ret = update_block_group_item(trans, path, cache); 277577745c05SJosef Bacik /* 277677745c05SJosef Bacik * One of the free space endio workers might have 277777745c05SJosef Bacik * created a new block group while updating a free space 277877745c05SJosef Bacik * cache's inode (at inode.c:btrfs_finish_ordered_io()) 277977745c05SJosef Bacik * and hasn't released its transaction handle yet, in 278077745c05SJosef Bacik * which case the new block group is still attached to 278177745c05SJosef Bacik * its transaction handle and its creation has not 278277745c05SJosef Bacik * finished yet (no block group item in the extent tree 278377745c05SJosef Bacik * yet, etc). If this is the case, wait for all free 278477745c05SJosef Bacik * space endio workers to finish and retry. This is a 2785260db43cSRandy Dunlap * very rare case so no need for a more efficient and 278677745c05SJosef Bacik * complex approach. 278777745c05SJosef Bacik */ 278877745c05SJosef Bacik if (ret == -ENOENT) { 278977745c05SJosef Bacik wait_event(cur_trans->writer_wait, 279077745c05SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 27913be4d8efSQu Wenruo ret = update_block_group_item(trans, path, cache); 279277745c05SJosef Bacik } 279377745c05SJosef Bacik if (ret) 279477745c05SJosef Bacik btrfs_abort_transaction(trans, ret); 279577745c05SJosef Bacik } 279677745c05SJosef Bacik 279777745c05SJosef Bacik /* If its not on the io list, we need to put the block group */ 279877745c05SJosef Bacik if (should_put) 279977745c05SJosef Bacik btrfs_put_block_group(cache); 280077745c05SJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 280177745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 280277745c05SJosef Bacik } 280377745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 280477745c05SJosef Bacik 280577745c05SJosef Bacik /* 280677745c05SJosef Bacik * Refer to the definition of io_bgs member for details why it's safe 280777745c05SJosef Bacik * to use it without any locking 280877745c05SJosef Bacik */ 280977745c05SJosef Bacik while (!list_empty(io)) { 281032da5386SDavid Sterba cache = list_first_entry(io, struct btrfs_block_group, 281177745c05SJosef Bacik io_list); 281277745c05SJosef Bacik list_del_init(&cache->io_list); 281377745c05SJosef Bacik btrfs_wait_cache_io(trans, cache, path); 281477745c05SJosef Bacik btrfs_put_block_group(cache); 281577745c05SJosef Bacik } 281677745c05SJosef Bacik 281777745c05SJosef Bacik btrfs_free_path(path); 281877745c05SJosef Bacik return ret; 281977745c05SJosef Bacik } 2820606d1bf1SJosef Bacik 2821606d1bf1SJosef Bacik int btrfs_update_block_group(struct btrfs_trans_handle *trans, 2822606d1bf1SJosef Bacik u64 bytenr, u64 num_bytes, int alloc) 2823606d1bf1SJosef Bacik { 2824606d1bf1SJosef Bacik struct btrfs_fs_info *info = trans->fs_info; 282532da5386SDavid Sterba struct btrfs_block_group *cache = NULL; 2826606d1bf1SJosef Bacik u64 total = num_bytes; 2827606d1bf1SJosef Bacik u64 old_val; 2828606d1bf1SJosef Bacik u64 byte_in_group; 2829606d1bf1SJosef Bacik int factor; 2830606d1bf1SJosef Bacik int ret = 0; 2831606d1bf1SJosef Bacik 2832606d1bf1SJosef Bacik /* Block accounting for super block */ 2833606d1bf1SJosef Bacik spin_lock(&info->delalloc_root_lock); 2834606d1bf1SJosef Bacik old_val = btrfs_super_bytes_used(info->super_copy); 2835606d1bf1SJosef Bacik if (alloc) 2836606d1bf1SJosef Bacik old_val += num_bytes; 2837606d1bf1SJosef Bacik else 2838606d1bf1SJosef Bacik old_val -= num_bytes; 2839606d1bf1SJosef Bacik btrfs_set_super_bytes_used(info->super_copy, old_val); 2840606d1bf1SJosef Bacik spin_unlock(&info->delalloc_root_lock); 2841606d1bf1SJosef Bacik 2842606d1bf1SJosef Bacik while (total) { 2843606d1bf1SJosef Bacik cache = btrfs_lookup_block_group(info, bytenr); 2844606d1bf1SJosef Bacik if (!cache) { 2845606d1bf1SJosef Bacik ret = -ENOENT; 2846606d1bf1SJosef Bacik break; 2847606d1bf1SJosef Bacik } 2848606d1bf1SJosef Bacik factor = btrfs_bg_type_to_factor(cache->flags); 2849606d1bf1SJosef Bacik 2850606d1bf1SJosef Bacik /* 2851606d1bf1SJosef Bacik * If this block group has free space cache written out, we 2852606d1bf1SJosef Bacik * need to make sure to load it if we are removing space. This 2853606d1bf1SJosef Bacik * is because we need the unpinning stage to actually add the 2854606d1bf1SJosef Bacik * space back to the block group, otherwise we will leak space. 2855606d1bf1SJosef Bacik */ 285632da5386SDavid Sterba if (!alloc && !btrfs_block_group_done(cache)) 2857606d1bf1SJosef Bacik btrfs_cache_block_group(cache, 1); 2858606d1bf1SJosef Bacik 2859b3470b5dSDavid Sterba byte_in_group = bytenr - cache->start; 2860b3470b5dSDavid Sterba WARN_ON(byte_in_group > cache->length); 2861606d1bf1SJosef Bacik 2862606d1bf1SJosef Bacik spin_lock(&cache->space_info->lock); 2863606d1bf1SJosef Bacik spin_lock(&cache->lock); 2864606d1bf1SJosef Bacik 2865606d1bf1SJosef Bacik if (btrfs_test_opt(info, SPACE_CACHE) && 2866606d1bf1SJosef Bacik cache->disk_cache_state < BTRFS_DC_CLEAR) 2867606d1bf1SJosef Bacik cache->disk_cache_state = BTRFS_DC_CLEAR; 2868606d1bf1SJosef Bacik 2869bf38be65SDavid Sterba old_val = cache->used; 2870b3470b5dSDavid Sterba num_bytes = min(total, cache->length - byte_in_group); 2871606d1bf1SJosef Bacik if (alloc) { 2872606d1bf1SJosef Bacik old_val += num_bytes; 2873bf38be65SDavid Sterba cache->used = old_val; 2874606d1bf1SJosef Bacik cache->reserved -= num_bytes; 2875606d1bf1SJosef Bacik cache->space_info->bytes_reserved -= num_bytes; 2876606d1bf1SJosef Bacik cache->space_info->bytes_used += num_bytes; 2877606d1bf1SJosef Bacik cache->space_info->disk_used += num_bytes * factor; 2878606d1bf1SJosef Bacik spin_unlock(&cache->lock); 2879606d1bf1SJosef Bacik spin_unlock(&cache->space_info->lock); 2880606d1bf1SJosef Bacik } else { 2881606d1bf1SJosef Bacik old_val -= num_bytes; 2882bf38be65SDavid Sterba cache->used = old_val; 2883606d1bf1SJosef Bacik cache->pinned += num_bytes; 2884606d1bf1SJosef Bacik btrfs_space_info_update_bytes_pinned(info, 2885606d1bf1SJosef Bacik cache->space_info, num_bytes); 2886606d1bf1SJosef Bacik cache->space_info->bytes_used -= num_bytes; 2887606d1bf1SJosef Bacik cache->space_info->disk_used -= num_bytes * factor; 2888606d1bf1SJosef Bacik spin_unlock(&cache->lock); 2889606d1bf1SJosef Bacik spin_unlock(&cache->space_info->lock); 2890606d1bf1SJosef Bacik 2891606d1bf1SJosef Bacik percpu_counter_add_batch( 2892606d1bf1SJosef Bacik &cache->space_info->total_bytes_pinned, 2893606d1bf1SJosef Bacik num_bytes, 2894606d1bf1SJosef Bacik BTRFS_TOTAL_BYTES_PINNED_BATCH); 2895fe119a6eSNikolay Borisov set_extent_dirty(&trans->transaction->pinned_extents, 2896606d1bf1SJosef Bacik bytenr, bytenr + num_bytes - 1, 2897606d1bf1SJosef Bacik GFP_NOFS | __GFP_NOFAIL); 2898606d1bf1SJosef Bacik } 2899606d1bf1SJosef Bacik 2900606d1bf1SJosef Bacik spin_lock(&trans->transaction->dirty_bgs_lock); 2901606d1bf1SJosef Bacik if (list_empty(&cache->dirty_list)) { 2902606d1bf1SJosef Bacik list_add_tail(&cache->dirty_list, 2903606d1bf1SJosef Bacik &trans->transaction->dirty_bgs); 2904606d1bf1SJosef Bacik trans->delayed_ref_updates++; 2905606d1bf1SJosef Bacik btrfs_get_block_group(cache); 2906606d1bf1SJosef Bacik } 2907606d1bf1SJosef Bacik spin_unlock(&trans->transaction->dirty_bgs_lock); 2908606d1bf1SJosef Bacik 2909606d1bf1SJosef Bacik /* 2910606d1bf1SJosef Bacik * No longer have used bytes in this block group, queue it for 2911606d1bf1SJosef Bacik * deletion. We do this after adding the block group to the 2912606d1bf1SJosef Bacik * dirty list to avoid races between cleaner kthread and space 2913606d1bf1SJosef Bacik * cache writeout. 2914606d1bf1SJosef Bacik */ 29156e80d4f8SDennis Zhou if (!alloc && old_val == 0) { 29166e80d4f8SDennis Zhou if (!btrfs_test_opt(info, DISCARD_ASYNC)) 2917606d1bf1SJosef Bacik btrfs_mark_bg_unused(cache); 29186e80d4f8SDennis Zhou } 2919606d1bf1SJosef Bacik 2920606d1bf1SJosef Bacik btrfs_put_block_group(cache); 2921606d1bf1SJosef Bacik total -= num_bytes; 2922606d1bf1SJosef Bacik bytenr += num_bytes; 2923606d1bf1SJosef Bacik } 2924606d1bf1SJosef Bacik 2925606d1bf1SJosef Bacik /* Modified block groups are accounted for in the delayed_refs_rsv. */ 2926606d1bf1SJosef Bacik btrfs_update_delayed_refs_rsv(trans); 2927606d1bf1SJosef Bacik return ret; 2928606d1bf1SJosef Bacik } 2929606d1bf1SJosef Bacik 2930606d1bf1SJosef Bacik /** 2931606d1bf1SJosef Bacik * btrfs_add_reserved_bytes - update the block_group and space info counters 2932606d1bf1SJosef Bacik * @cache: The cache we are manipulating 2933606d1bf1SJosef Bacik * @ram_bytes: The number of bytes of file content, and will be same to 2934606d1bf1SJosef Bacik * @num_bytes except for the compress path. 2935606d1bf1SJosef Bacik * @num_bytes: The number of bytes in question 2936606d1bf1SJosef Bacik * @delalloc: The blocks are allocated for the delalloc write 2937606d1bf1SJosef Bacik * 2938606d1bf1SJosef Bacik * This is called by the allocator when it reserves space. If this is a 2939606d1bf1SJosef Bacik * reservation and the block group has become read only we cannot make the 2940606d1bf1SJosef Bacik * reservation and return -EAGAIN, otherwise this function always succeeds. 2941606d1bf1SJosef Bacik */ 294232da5386SDavid Sterba int btrfs_add_reserved_bytes(struct btrfs_block_group *cache, 2943606d1bf1SJosef Bacik u64 ram_bytes, u64 num_bytes, int delalloc) 2944606d1bf1SJosef Bacik { 2945606d1bf1SJosef Bacik struct btrfs_space_info *space_info = cache->space_info; 2946606d1bf1SJosef Bacik int ret = 0; 2947606d1bf1SJosef Bacik 2948606d1bf1SJosef Bacik spin_lock(&space_info->lock); 2949606d1bf1SJosef Bacik spin_lock(&cache->lock); 2950606d1bf1SJosef Bacik if (cache->ro) { 2951606d1bf1SJosef Bacik ret = -EAGAIN; 2952606d1bf1SJosef Bacik } else { 2953606d1bf1SJosef Bacik cache->reserved += num_bytes; 2954606d1bf1SJosef Bacik space_info->bytes_reserved += num_bytes; 2955a43c3835SJosef Bacik trace_btrfs_space_reservation(cache->fs_info, "space_info", 2956a43c3835SJosef Bacik space_info->flags, num_bytes, 1); 2957606d1bf1SJosef Bacik btrfs_space_info_update_bytes_may_use(cache->fs_info, 2958606d1bf1SJosef Bacik space_info, -ram_bytes); 2959606d1bf1SJosef Bacik if (delalloc) 2960606d1bf1SJosef Bacik cache->delalloc_bytes += num_bytes; 296199ffb43eSJosef Bacik 296299ffb43eSJosef Bacik /* 296399ffb43eSJosef Bacik * Compression can use less space than we reserved, so wake 296499ffb43eSJosef Bacik * tickets if that happens 296599ffb43eSJosef Bacik */ 296699ffb43eSJosef Bacik if (num_bytes < ram_bytes) 296799ffb43eSJosef Bacik btrfs_try_granting_tickets(cache->fs_info, space_info); 2968606d1bf1SJosef Bacik } 2969606d1bf1SJosef Bacik spin_unlock(&cache->lock); 2970606d1bf1SJosef Bacik spin_unlock(&space_info->lock); 2971606d1bf1SJosef Bacik return ret; 2972606d1bf1SJosef Bacik } 2973606d1bf1SJosef Bacik 2974606d1bf1SJosef Bacik /** 2975606d1bf1SJosef Bacik * btrfs_free_reserved_bytes - update the block_group and space info counters 2976606d1bf1SJosef Bacik * @cache: The cache we are manipulating 2977606d1bf1SJosef Bacik * @num_bytes: The number of bytes in question 2978606d1bf1SJosef Bacik * @delalloc: The blocks are allocated for the delalloc write 2979606d1bf1SJosef Bacik * 2980606d1bf1SJosef Bacik * This is called by somebody who is freeing space that was never actually used 2981606d1bf1SJosef Bacik * on disk. For example if you reserve some space for a new leaf in transaction 2982606d1bf1SJosef Bacik * A and before transaction A commits you free that leaf, you call this with 2983606d1bf1SJosef Bacik * reserve set to 0 in order to clear the reservation. 2984606d1bf1SJosef Bacik */ 298532da5386SDavid Sterba void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, 2986606d1bf1SJosef Bacik u64 num_bytes, int delalloc) 2987606d1bf1SJosef Bacik { 2988606d1bf1SJosef Bacik struct btrfs_space_info *space_info = cache->space_info; 2989606d1bf1SJosef Bacik 2990606d1bf1SJosef Bacik spin_lock(&space_info->lock); 2991606d1bf1SJosef Bacik spin_lock(&cache->lock); 2992606d1bf1SJosef Bacik if (cache->ro) 2993606d1bf1SJosef Bacik space_info->bytes_readonly += num_bytes; 2994606d1bf1SJosef Bacik cache->reserved -= num_bytes; 2995606d1bf1SJosef Bacik space_info->bytes_reserved -= num_bytes; 2996606d1bf1SJosef Bacik space_info->max_extent_size = 0; 2997606d1bf1SJosef Bacik 2998606d1bf1SJosef Bacik if (delalloc) 2999606d1bf1SJosef Bacik cache->delalloc_bytes -= num_bytes; 3000606d1bf1SJosef Bacik spin_unlock(&cache->lock); 30013308234aSJosef Bacik 30023308234aSJosef Bacik btrfs_try_granting_tickets(cache->fs_info, space_info); 3003606d1bf1SJosef Bacik spin_unlock(&space_info->lock); 3004606d1bf1SJosef Bacik } 300507730d87SJosef Bacik 300607730d87SJosef Bacik static void force_metadata_allocation(struct btrfs_fs_info *info) 300707730d87SJosef Bacik { 300807730d87SJosef Bacik struct list_head *head = &info->space_info; 300907730d87SJosef Bacik struct btrfs_space_info *found; 301007730d87SJosef Bacik 301172804905SJosef Bacik list_for_each_entry(found, head, list) { 301207730d87SJosef Bacik if (found->flags & BTRFS_BLOCK_GROUP_METADATA) 301307730d87SJosef Bacik found->force_alloc = CHUNK_ALLOC_FORCE; 301407730d87SJosef Bacik } 301507730d87SJosef Bacik } 301607730d87SJosef Bacik 301707730d87SJosef Bacik static int should_alloc_chunk(struct btrfs_fs_info *fs_info, 301807730d87SJosef Bacik struct btrfs_space_info *sinfo, int force) 301907730d87SJosef Bacik { 302007730d87SJosef Bacik u64 bytes_used = btrfs_space_info_used(sinfo, false); 302107730d87SJosef Bacik u64 thresh; 302207730d87SJosef Bacik 302307730d87SJosef Bacik if (force == CHUNK_ALLOC_FORCE) 302407730d87SJosef Bacik return 1; 302507730d87SJosef Bacik 302607730d87SJosef Bacik /* 302707730d87SJosef Bacik * in limited mode, we want to have some free space up to 302807730d87SJosef Bacik * about 1% of the FS size. 302907730d87SJosef Bacik */ 303007730d87SJosef Bacik if (force == CHUNK_ALLOC_LIMITED) { 303107730d87SJosef Bacik thresh = btrfs_super_total_bytes(fs_info->super_copy); 303207730d87SJosef Bacik thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1)); 303307730d87SJosef Bacik 303407730d87SJosef Bacik if (sinfo->total_bytes - bytes_used < thresh) 303507730d87SJosef Bacik return 1; 303607730d87SJosef Bacik } 303707730d87SJosef Bacik 303807730d87SJosef Bacik if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8)) 303907730d87SJosef Bacik return 0; 304007730d87SJosef Bacik return 1; 304107730d87SJosef Bacik } 304207730d87SJosef Bacik 304307730d87SJosef Bacik int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type) 304407730d87SJosef Bacik { 304507730d87SJosef Bacik u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type); 304607730d87SJosef Bacik 304707730d87SJosef Bacik return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE); 304807730d87SJosef Bacik } 304907730d87SJosef Bacik 305007730d87SJosef Bacik /* 305107730d87SJosef Bacik * If force is CHUNK_ALLOC_FORCE: 305207730d87SJosef Bacik * - return 1 if it successfully allocates a chunk, 305307730d87SJosef Bacik * - return errors including -ENOSPC otherwise. 305407730d87SJosef Bacik * If force is NOT CHUNK_ALLOC_FORCE: 305507730d87SJosef Bacik * - return 0 if it doesn't need to allocate a new chunk, 305607730d87SJosef Bacik * - return 1 if it successfully allocates a chunk, 305707730d87SJosef Bacik * - return errors including -ENOSPC otherwise. 305807730d87SJosef Bacik */ 305907730d87SJosef Bacik int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags, 306007730d87SJosef Bacik enum btrfs_chunk_alloc_enum force) 306107730d87SJosef Bacik { 306207730d87SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 306307730d87SJosef Bacik struct btrfs_space_info *space_info; 306407730d87SJosef Bacik bool wait_for_alloc = false; 306507730d87SJosef Bacik bool should_alloc = false; 306607730d87SJosef Bacik int ret = 0; 306707730d87SJosef Bacik 306807730d87SJosef Bacik /* Don't re-enter if we're already allocating a chunk */ 306907730d87SJosef Bacik if (trans->allocating_chunk) 307007730d87SJosef Bacik return -ENOSPC; 307107730d87SJosef Bacik 307207730d87SJosef Bacik space_info = btrfs_find_space_info(fs_info, flags); 307307730d87SJosef Bacik ASSERT(space_info); 307407730d87SJosef Bacik 307507730d87SJosef Bacik do { 307607730d87SJosef Bacik spin_lock(&space_info->lock); 307707730d87SJosef Bacik if (force < space_info->force_alloc) 307807730d87SJosef Bacik force = space_info->force_alloc; 307907730d87SJosef Bacik should_alloc = should_alloc_chunk(fs_info, space_info, force); 308007730d87SJosef Bacik if (space_info->full) { 308107730d87SJosef Bacik /* No more free physical space */ 308207730d87SJosef Bacik if (should_alloc) 308307730d87SJosef Bacik ret = -ENOSPC; 308407730d87SJosef Bacik else 308507730d87SJosef Bacik ret = 0; 308607730d87SJosef Bacik spin_unlock(&space_info->lock); 308707730d87SJosef Bacik return ret; 308807730d87SJosef Bacik } else if (!should_alloc) { 308907730d87SJosef Bacik spin_unlock(&space_info->lock); 309007730d87SJosef Bacik return 0; 309107730d87SJosef Bacik } else if (space_info->chunk_alloc) { 309207730d87SJosef Bacik /* 309307730d87SJosef Bacik * Someone is already allocating, so we need to block 309407730d87SJosef Bacik * until this someone is finished and then loop to 309507730d87SJosef Bacik * recheck if we should continue with our allocation 309607730d87SJosef Bacik * attempt. 309707730d87SJosef Bacik */ 309807730d87SJosef Bacik wait_for_alloc = true; 309907730d87SJosef Bacik spin_unlock(&space_info->lock); 310007730d87SJosef Bacik mutex_lock(&fs_info->chunk_mutex); 310107730d87SJosef Bacik mutex_unlock(&fs_info->chunk_mutex); 310207730d87SJosef Bacik } else { 310307730d87SJosef Bacik /* Proceed with allocation */ 310407730d87SJosef Bacik space_info->chunk_alloc = 1; 310507730d87SJosef Bacik wait_for_alloc = false; 310607730d87SJosef Bacik spin_unlock(&space_info->lock); 310707730d87SJosef Bacik } 310807730d87SJosef Bacik 310907730d87SJosef Bacik cond_resched(); 311007730d87SJosef Bacik } while (wait_for_alloc); 311107730d87SJosef Bacik 311207730d87SJosef Bacik mutex_lock(&fs_info->chunk_mutex); 311307730d87SJosef Bacik trans->allocating_chunk = true; 311407730d87SJosef Bacik 311507730d87SJosef Bacik /* 311607730d87SJosef Bacik * If we have mixed data/metadata chunks we want to make sure we keep 311707730d87SJosef Bacik * allocating mixed chunks instead of individual chunks. 311807730d87SJosef Bacik */ 311907730d87SJosef Bacik if (btrfs_mixed_space_info(space_info)) 312007730d87SJosef Bacik flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); 312107730d87SJosef Bacik 312207730d87SJosef Bacik /* 312307730d87SJosef Bacik * if we're doing a data chunk, go ahead and make sure that 312407730d87SJosef Bacik * we keep a reasonable number of metadata chunks allocated in the 312507730d87SJosef Bacik * FS as well. 312607730d87SJosef Bacik */ 312707730d87SJosef Bacik if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { 312807730d87SJosef Bacik fs_info->data_chunk_allocations++; 312907730d87SJosef Bacik if (!(fs_info->data_chunk_allocations % 313007730d87SJosef Bacik fs_info->metadata_ratio)) 313107730d87SJosef Bacik force_metadata_allocation(fs_info); 313207730d87SJosef Bacik } 313307730d87SJosef Bacik 313407730d87SJosef Bacik /* 313507730d87SJosef Bacik * Check if we have enough space in SYSTEM chunk because we may need 313607730d87SJosef Bacik * to update devices. 313707730d87SJosef Bacik */ 313807730d87SJosef Bacik check_system_chunk(trans, flags); 313907730d87SJosef Bacik 314007730d87SJosef Bacik ret = btrfs_alloc_chunk(trans, flags); 314107730d87SJosef Bacik trans->allocating_chunk = false; 314207730d87SJosef Bacik 314307730d87SJosef Bacik spin_lock(&space_info->lock); 314407730d87SJosef Bacik if (ret < 0) { 314507730d87SJosef Bacik if (ret == -ENOSPC) 314607730d87SJosef Bacik space_info->full = 1; 314707730d87SJosef Bacik else 314807730d87SJosef Bacik goto out; 314907730d87SJosef Bacik } else { 315007730d87SJosef Bacik ret = 1; 315107730d87SJosef Bacik space_info->max_extent_size = 0; 315207730d87SJosef Bacik } 315307730d87SJosef Bacik 315407730d87SJosef Bacik space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; 315507730d87SJosef Bacik out: 315607730d87SJosef Bacik space_info->chunk_alloc = 0; 315707730d87SJosef Bacik spin_unlock(&space_info->lock); 315807730d87SJosef Bacik mutex_unlock(&fs_info->chunk_mutex); 315907730d87SJosef Bacik /* 316007730d87SJosef Bacik * When we allocate a new chunk we reserve space in the chunk block 316107730d87SJosef Bacik * reserve to make sure we can COW nodes/leafs in the chunk tree or 316207730d87SJosef Bacik * add new nodes/leafs to it if we end up needing to do it when 316307730d87SJosef Bacik * inserting the chunk item and updating device items as part of the 316407730d87SJosef Bacik * second phase of chunk allocation, performed by 316507730d87SJosef Bacik * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a 316607730d87SJosef Bacik * large number of new block groups to create in our transaction 316707730d87SJosef Bacik * handle's new_bgs list to avoid exhausting the chunk block reserve 316807730d87SJosef Bacik * in extreme cases - like having a single transaction create many new 316907730d87SJosef Bacik * block groups when starting to write out the free space caches of all 317007730d87SJosef Bacik * the block groups that were made dirty during the lifetime of the 317107730d87SJosef Bacik * transaction. 317207730d87SJosef Bacik */ 317307730d87SJosef Bacik if (trans->chunk_bytes_reserved >= (u64)SZ_2M) 317407730d87SJosef Bacik btrfs_create_pending_block_groups(trans); 317507730d87SJosef Bacik 317607730d87SJosef Bacik return ret; 317707730d87SJosef Bacik } 317807730d87SJosef Bacik 317907730d87SJosef Bacik static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type) 318007730d87SJosef Bacik { 318107730d87SJosef Bacik u64 num_dev; 318207730d87SJosef Bacik 318307730d87SJosef Bacik num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max; 318407730d87SJosef Bacik if (!num_dev) 318507730d87SJosef Bacik num_dev = fs_info->fs_devices->rw_devices; 318607730d87SJosef Bacik 318707730d87SJosef Bacik return num_dev; 318807730d87SJosef Bacik } 318907730d87SJosef Bacik 319007730d87SJosef Bacik /* 3191a9143bd3SMarcos Paulo de Souza * Reserve space in the system space for allocating or removing a chunk 319207730d87SJosef Bacik */ 319307730d87SJosef Bacik void check_system_chunk(struct btrfs_trans_handle *trans, u64 type) 319407730d87SJosef Bacik { 319507730d87SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 319607730d87SJosef Bacik struct btrfs_space_info *info; 319707730d87SJosef Bacik u64 left; 319807730d87SJosef Bacik u64 thresh; 319907730d87SJosef Bacik int ret = 0; 320007730d87SJosef Bacik u64 num_devs; 320107730d87SJosef Bacik 320207730d87SJosef Bacik /* 320307730d87SJosef Bacik * Needed because we can end up allocating a system chunk and for an 320407730d87SJosef Bacik * atomic and race free space reservation in the chunk block reserve. 320507730d87SJosef Bacik */ 320607730d87SJosef Bacik lockdep_assert_held(&fs_info->chunk_mutex); 320707730d87SJosef Bacik 320807730d87SJosef Bacik info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); 320907730d87SJosef Bacik spin_lock(&info->lock); 321007730d87SJosef Bacik left = info->total_bytes - btrfs_space_info_used(info, true); 321107730d87SJosef Bacik spin_unlock(&info->lock); 321207730d87SJosef Bacik 321307730d87SJosef Bacik num_devs = get_profile_num_devs(fs_info, type); 321407730d87SJosef Bacik 321507730d87SJosef Bacik /* num_devs device items to update and 1 chunk item to add or remove */ 32162bd36e7bSJosef Bacik thresh = btrfs_calc_metadata_size(fs_info, num_devs) + 32172bd36e7bSJosef Bacik btrfs_calc_insert_metadata_size(fs_info, 1); 321807730d87SJosef Bacik 321907730d87SJosef Bacik if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { 322007730d87SJosef Bacik btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu", 322107730d87SJosef Bacik left, thresh, type); 322207730d87SJosef Bacik btrfs_dump_space_info(fs_info, info, 0, 0); 322307730d87SJosef Bacik } 322407730d87SJosef Bacik 322507730d87SJosef Bacik if (left < thresh) { 322607730d87SJosef Bacik u64 flags = btrfs_system_alloc_profile(fs_info); 322707730d87SJosef Bacik 322807730d87SJosef Bacik /* 322907730d87SJosef Bacik * Ignore failure to create system chunk. We might end up not 323007730d87SJosef Bacik * needing it, as we might not need to COW all nodes/leafs from 323107730d87SJosef Bacik * the paths we visit in the chunk tree (they were already COWed 323207730d87SJosef Bacik * or created in the current transaction for example). 323307730d87SJosef Bacik */ 323407730d87SJosef Bacik ret = btrfs_alloc_chunk(trans, flags); 323507730d87SJosef Bacik } 323607730d87SJosef Bacik 323707730d87SJosef Bacik if (!ret) { 323807730d87SJosef Bacik ret = btrfs_block_rsv_add(fs_info->chunk_root, 323907730d87SJosef Bacik &fs_info->chunk_block_rsv, 324007730d87SJosef Bacik thresh, BTRFS_RESERVE_NO_FLUSH); 324107730d87SJosef Bacik if (!ret) 324207730d87SJosef Bacik trans->chunk_bytes_reserved += thresh; 324307730d87SJosef Bacik } 324407730d87SJosef Bacik } 324507730d87SJosef Bacik 32463e43c279SJosef Bacik void btrfs_put_block_group_cache(struct btrfs_fs_info *info) 32473e43c279SJosef Bacik { 324832da5386SDavid Sterba struct btrfs_block_group *block_group; 32493e43c279SJosef Bacik u64 last = 0; 32503e43c279SJosef Bacik 32513e43c279SJosef Bacik while (1) { 32523e43c279SJosef Bacik struct inode *inode; 32533e43c279SJosef Bacik 32543e43c279SJosef Bacik block_group = btrfs_lookup_first_block_group(info, last); 32553e43c279SJosef Bacik while (block_group) { 32563e43c279SJosef Bacik btrfs_wait_block_group_cache_done(block_group); 32573e43c279SJosef Bacik spin_lock(&block_group->lock); 32583e43c279SJosef Bacik if (block_group->iref) 32593e43c279SJosef Bacik break; 32603e43c279SJosef Bacik spin_unlock(&block_group->lock); 32613e43c279SJosef Bacik block_group = btrfs_next_block_group(block_group); 32623e43c279SJosef Bacik } 32633e43c279SJosef Bacik if (!block_group) { 32643e43c279SJosef Bacik if (last == 0) 32653e43c279SJosef Bacik break; 32663e43c279SJosef Bacik last = 0; 32673e43c279SJosef Bacik continue; 32683e43c279SJosef Bacik } 32693e43c279SJosef Bacik 32703e43c279SJosef Bacik inode = block_group->inode; 32713e43c279SJosef Bacik block_group->iref = 0; 32723e43c279SJosef Bacik block_group->inode = NULL; 32733e43c279SJosef Bacik spin_unlock(&block_group->lock); 32743e43c279SJosef Bacik ASSERT(block_group->io_ctl.inode == NULL); 32753e43c279SJosef Bacik iput(inode); 3276b3470b5dSDavid Sterba last = block_group->start + block_group->length; 32773e43c279SJosef Bacik btrfs_put_block_group(block_group); 32783e43c279SJosef Bacik } 32793e43c279SJosef Bacik } 32803e43c279SJosef Bacik 32813e43c279SJosef Bacik /* 32823e43c279SJosef Bacik * Must be called only after stopping all workers, since we could have block 32833e43c279SJosef Bacik * group caching kthreads running, and therefore they could race with us if we 32843e43c279SJosef Bacik * freed the block groups before stopping them. 32853e43c279SJosef Bacik */ 32863e43c279SJosef Bacik int btrfs_free_block_groups(struct btrfs_fs_info *info) 32873e43c279SJosef Bacik { 328832da5386SDavid Sterba struct btrfs_block_group *block_group; 32893e43c279SJosef Bacik struct btrfs_space_info *space_info; 32903e43c279SJosef Bacik struct btrfs_caching_control *caching_ctl; 32913e43c279SJosef Bacik struct rb_node *n; 32923e43c279SJosef Bacik 3293bbb86a37SJosef Bacik spin_lock(&info->block_group_cache_lock); 32943e43c279SJosef Bacik while (!list_empty(&info->caching_block_groups)) { 32953e43c279SJosef Bacik caching_ctl = list_entry(info->caching_block_groups.next, 32963e43c279SJosef Bacik struct btrfs_caching_control, list); 32973e43c279SJosef Bacik list_del(&caching_ctl->list); 32983e43c279SJosef Bacik btrfs_put_caching_control(caching_ctl); 32993e43c279SJosef Bacik } 3300bbb86a37SJosef Bacik spin_unlock(&info->block_group_cache_lock); 33013e43c279SJosef Bacik 33023e43c279SJosef Bacik spin_lock(&info->unused_bgs_lock); 33033e43c279SJosef Bacik while (!list_empty(&info->unused_bgs)) { 33043e43c279SJosef Bacik block_group = list_first_entry(&info->unused_bgs, 330532da5386SDavid Sterba struct btrfs_block_group, 33063e43c279SJosef Bacik bg_list); 33073e43c279SJosef Bacik list_del_init(&block_group->bg_list); 33083e43c279SJosef Bacik btrfs_put_block_group(block_group); 33093e43c279SJosef Bacik } 33103e43c279SJosef Bacik spin_unlock(&info->unused_bgs_lock); 33113e43c279SJosef Bacik 33123e43c279SJosef Bacik spin_lock(&info->block_group_cache_lock); 33133e43c279SJosef Bacik while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { 331432da5386SDavid Sterba block_group = rb_entry(n, struct btrfs_block_group, 33153e43c279SJosef Bacik cache_node); 33163e43c279SJosef Bacik rb_erase(&block_group->cache_node, 33173e43c279SJosef Bacik &info->block_group_cache_tree); 33183e43c279SJosef Bacik RB_CLEAR_NODE(&block_group->cache_node); 33193e43c279SJosef Bacik spin_unlock(&info->block_group_cache_lock); 33203e43c279SJosef Bacik 33213e43c279SJosef Bacik down_write(&block_group->space_info->groups_sem); 33223e43c279SJosef Bacik list_del(&block_group->list); 33233e43c279SJosef Bacik up_write(&block_group->space_info->groups_sem); 33243e43c279SJosef Bacik 33253e43c279SJosef Bacik /* 33263e43c279SJosef Bacik * We haven't cached this block group, which means we could 33273e43c279SJosef Bacik * possibly have excluded extents on this block group. 33283e43c279SJosef Bacik */ 33293e43c279SJosef Bacik if (block_group->cached == BTRFS_CACHE_NO || 33303e43c279SJosef Bacik block_group->cached == BTRFS_CACHE_ERROR) 33313e43c279SJosef Bacik btrfs_free_excluded_extents(block_group); 33323e43c279SJosef Bacik 33333e43c279SJosef Bacik btrfs_remove_free_space_cache(block_group); 33343e43c279SJosef Bacik ASSERT(block_group->cached != BTRFS_CACHE_STARTED); 33353e43c279SJosef Bacik ASSERT(list_empty(&block_group->dirty_list)); 33363e43c279SJosef Bacik ASSERT(list_empty(&block_group->io_list)); 33373e43c279SJosef Bacik ASSERT(list_empty(&block_group->bg_list)); 333848aaeebeSJosef Bacik ASSERT(refcount_read(&block_group->refs) == 1); 33393e43c279SJosef Bacik btrfs_put_block_group(block_group); 33403e43c279SJosef Bacik 33413e43c279SJosef Bacik spin_lock(&info->block_group_cache_lock); 33423e43c279SJosef Bacik } 33433e43c279SJosef Bacik spin_unlock(&info->block_group_cache_lock); 33443e43c279SJosef Bacik 33453e43c279SJosef Bacik btrfs_release_global_block_rsv(info); 33463e43c279SJosef Bacik 33473e43c279SJosef Bacik while (!list_empty(&info->space_info)) { 33483e43c279SJosef Bacik space_info = list_entry(info->space_info.next, 33493e43c279SJosef Bacik struct btrfs_space_info, 33503e43c279SJosef Bacik list); 33513e43c279SJosef Bacik 33523e43c279SJosef Bacik /* 33533e43c279SJosef Bacik * Do not hide this behind enospc_debug, this is actually 33543e43c279SJosef Bacik * important and indicates a real bug if this happens. 33553e43c279SJosef Bacik */ 33563e43c279SJosef Bacik if (WARN_ON(space_info->bytes_pinned > 0 || 33573e43c279SJosef Bacik space_info->bytes_reserved > 0 || 33583e43c279SJosef Bacik space_info->bytes_may_use > 0)) 33593e43c279SJosef Bacik btrfs_dump_space_info(info, space_info, 0, 0); 3360d611add4SFilipe Manana WARN_ON(space_info->reclaim_size > 0); 33613e43c279SJosef Bacik list_del(&space_info->list); 33623e43c279SJosef Bacik btrfs_sysfs_remove_space_info(space_info); 33633e43c279SJosef Bacik } 33643e43c279SJosef Bacik return 0; 33653e43c279SJosef Bacik } 3366684b752bSFilipe Manana 3367684b752bSFilipe Manana void btrfs_freeze_block_group(struct btrfs_block_group *cache) 3368684b752bSFilipe Manana { 3369684b752bSFilipe Manana atomic_inc(&cache->frozen); 3370684b752bSFilipe Manana } 3371684b752bSFilipe Manana 3372684b752bSFilipe Manana void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group) 3373684b752bSFilipe Manana { 3374684b752bSFilipe Manana struct btrfs_fs_info *fs_info = block_group->fs_info; 3375684b752bSFilipe Manana struct extent_map_tree *em_tree; 3376684b752bSFilipe Manana struct extent_map *em; 3377684b752bSFilipe Manana bool cleanup; 3378684b752bSFilipe Manana 3379684b752bSFilipe Manana spin_lock(&block_group->lock); 3380684b752bSFilipe Manana cleanup = (atomic_dec_and_test(&block_group->frozen) && 3381684b752bSFilipe Manana block_group->removed); 3382684b752bSFilipe Manana spin_unlock(&block_group->lock); 3383684b752bSFilipe Manana 3384684b752bSFilipe Manana if (cleanup) { 3385684b752bSFilipe Manana em_tree = &fs_info->mapping_tree; 3386684b752bSFilipe Manana write_lock(&em_tree->lock); 3387684b752bSFilipe Manana em = lookup_extent_mapping(em_tree, block_group->start, 3388684b752bSFilipe Manana 1); 3389684b752bSFilipe Manana BUG_ON(!em); /* logic error, can't happen */ 3390684b752bSFilipe Manana remove_extent_mapping(em_tree, em); 3391684b752bSFilipe Manana write_unlock(&em_tree->lock); 3392684b752bSFilipe Manana 3393684b752bSFilipe Manana /* once for us and once for the tree */ 3394684b752bSFilipe Manana free_extent_map(em); 3395684b752bSFilipe Manana free_extent_map(em); 3396684b752bSFilipe Manana 3397684b752bSFilipe Manana /* 3398684b752bSFilipe Manana * We may have left one free space entry and other possible 3399684b752bSFilipe Manana * tasks trimming this block group have left 1 entry each one. 3400684b752bSFilipe Manana * Free them if any. 3401684b752bSFilipe Manana */ 3402684b752bSFilipe Manana __btrfs_remove_free_space_cache(block_group->free_space_ctl); 3403684b752bSFilipe Manana } 3404684b752bSFilipe Manana } 3405