12e405ad8SJosef Bacik // SPDX-License-Identifier: GPL-2.0 22e405ad8SJosef Bacik 32ca0ec77SJohannes Thumshirn #include <linux/list_sort.h> 4784352feSDavid Sterba #include "misc.h" 52e405ad8SJosef Bacik #include "ctree.h" 62e405ad8SJosef Bacik #include "block-group.h" 73eeb3226SJosef Bacik #include "space-info.h" 89f21246dSJosef Bacik #include "disk-io.h" 99f21246dSJosef Bacik #include "free-space-cache.h" 109f21246dSJosef Bacik #include "free-space-tree.h" 11e3e0520bSJosef Bacik #include "volumes.h" 12e3e0520bSJosef Bacik #include "transaction.h" 13e3e0520bSJosef Bacik #include "ref-verify.h" 144358d963SJosef Bacik #include "sysfs.h" 154358d963SJosef Bacik #include "tree-log.h" 1677745c05SJosef Bacik #include "delalloc-space.h" 17b0643e59SDennis Zhou #include "discard.h" 1896a14336SNikolay Borisov #include "raid56.h" 1908e11a3dSNaohiro Aota #include "zoned.h" 202e405ad8SJosef Bacik 21878d7b67SJosef Bacik /* 22878d7b67SJosef Bacik * Return target flags in extended format or 0 if restripe for this chunk_type 23878d7b67SJosef Bacik * is not in progress 24878d7b67SJosef Bacik * 25878d7b67SJosef Bacik * Should be called with balance_lock held 26878d7b67SJosef Bacik */ 27e11c0406SJosef Bacik static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) 28878d7b67SJosef Bacik { 29878d7b67SJosef Bacik struct btrfs_balance_control *bctl = fs_info->balance_ctl; 30878d7b67SJosef Bacik u64 target = 0; 31878d7b67SJosef Bacik 32878d7b67SJosef Bacik if (!bctl) 33878d7b67SJosef Bacik return 0; 34878d7b67SJosef Bacik 35878d7b67SJosef Bacik if (flags & BTRFS_BLOCK_GROUP_DATA && 36878d7b67SJosef Bacik bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { 37878d7b67SJosef Bacik target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; 38878d7b67SJosef Bacik } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && 39878d7b67SJosef Bacik bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { 40878d7b67SJosef Bacik target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; 41878d7b67SJosef Bacik } else if (flags & BTRFS_BLOCK_GROUP_METADATA && 42878d7b67SJosef Bacik bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { 43878d7b67SJosef Bacik target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; 44878d7b67SJosef Bacik } 45878d7b67SJosef Bacik 46878d7b67SJosef Bacik return target; 47878d7b67SJosef Bacik } 48878d7b67SJosef Bacik 49878d7b67SJosef Bacik /* 50878d7b67SJosef Bacik * @flags: available profiles in extended format (see ctree.h) 51878d7b67SJosef Bacik * 52878d7b67SJosef Bacik * Return reduced profile in chunk format. If profile changing is in progress 53878d7b67SJosef Bacik * (either running or paused) picks the target profile (if it's already 54878d7b67SJosef Bacik * available), otherwise falls back to plain reducing. 55878d7b67SJosef Bacik */ 56878d7b67SJosef Bacik static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags) 57878d7b67SJosef Bacik { 58878d7b67SJosef Bacik u64 num_devices = fs_info->fs_devices->rw_devices; 59878d7b67SJosef Bacik u64 target; 60878d7b67SJosef Bacik u64 raid_type; 61878d7b67SJosef Bacik u64 allowed = 0; 62878d7b67SJosef Bacik 63878d7b67SJosef Bacik /* 64878d7b67SJosef Bacik * See if restripe for this chunk_type is in progress, if so try to 65878d7b67SJosef Bacik * reduce to the target profile 66878d7b67SJosef Bacik */ 67878d7b67SJosef Bacik spin_lock(&fs_info->balance_lock); 68e11c0406SJosef Bacik target = get_restripe_target(fs_info, flags); 69878d7b67SJosef Bacik if (target) { 70878d7b67SJosef Bacik spin_unlock(&fs_info->balance_lock); 71878d7b67SJosef Bacik return extended_to_chunk(target); 72878d7b67SJosef Bacik } 73878d7b67SJosef Bacik spin_unlock(&fs_info->balance_lock); 74878d7b67SJosef Bacik 75878d7b67SJosef Bacik /* First, mask out the RAID levels which aren't possible */ 76878d7b67SJosef Bacik for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 77878d7b67SJosef Bacik if (num_devices >= btrfs_raid_array[raid_type].devs_min) 78878d7b67SJosef Bacik allowed |= btrfs_raid_array[raid_type].bg_flag; 79878d7b67SJosef Bacik } 80878d7b67SJosef Bacik allowed &= flags; 81878d7b67SJosef Bacik 82878d7b67SJosef Bacik if (allowed & BTRFS_BLOCK_GROUP_RAID6) 83878d7b67SJosef Bacik allowed = BTRFS_BLOCK_GROUP_RAID6; 84878d7b67SJosef Bacik else if (allowed & BTRFS_BLOCK_GROUP_RAID5) 85878d7b67SJosef Bacik allowed = BTRFS_BLOCK_GROUP_RAID5; 86878d7b67SJosef Bacik else if (allowed & BTRFS_BLOCK_GROUP_RAID10) 87878d7b67SJosef Bacik allowed = BTRFS_BLOCK_GROUP_RAID10; 88878d7b67SJosef Bacik else if (allowed & BTRFS_BLOCK_GROUP_RAID1) 89878d7b67SJosef Bacik allowed = BTRFS_BLOCK_GROUP_RAID1; 90878d7b67SJosef Bacik else if (allowed & BTRFS_BLOCK_GROUP_RAID0) 91878d7b67SJosef Bacik allowed = BTRFS_BLOCK_GROUP_RAID0; 92878d7b67SJosef Bacik 93878d7b67SJosef Bacik flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK; 94878d7b67SJosef Bacik 95878d7b67SJosef Bacik return extended_to_chunk(flags | allowed); 96878d7b67SJosef Bacik } 97878d7b67SJosef Bacik 98ef0a82daSJohannes Thumshirn u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags) 99878d7b67SJosef Bacik { 100878d7b67SJosef Bacik unsigned seq; 101878d7b67SJosef Bacik u64 flags; 102878d7b67SJosef Bacik 103878d7b67SJosef Bacik do { 104878d7b67SJosef Bacik flags = orig_flags; 105878d7b67SJosef Bacik seq = read_seqbegin(&fs_info->profiles_lock); 106878d7b67SJosef Bacik 107878d7b67SJosef Bacik if (flags & BTRFS_BLOCK_GROUP_DATA) 108878d7b67SJosef Bacik flags |= fs_info->avail_data_alloc_bits; 109878d7b67SJosef Bacik else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 110878d7b67SJosef Bacik flags |= fs_info->avail_system_alloc_bits; 111878d7b67SJosef Bacik else if (flags & BTRFS_BLOCK_GROUP_METADATA) 112878d7b67SJosef Bacik flags |= fs_info->avail_metadata_alloc_bits; 113878d7b67SJosef Bacik } while (read_seqretry(&fs_info->profiles_lock, seq)); 114878d7b67SJosef Bacik 115878d7b67SJosef Bacik return btrfs_reduce_alloc_profile(fs_info, flags); 116878d7b67SJosef Bacik } 117878d7b67SJosef Bacik 11832da5386SDavid Sterba void btrfs_get_block_group(struct btrfs_block_group *cache) 1193cad1284SJosef Bacik { 12048aaeebeSJosef Bacik refcount_inc(&cache->refs); 1213cad1284SJosef Bacik } 1223cad1284SJosef Bacik 12332da5386SDavid Sterba void btrfs_put_block_group(struct btrfs_block_group *cache) 1243cad1284SJosef Bacik { 12548aaeebeSJosef Bacik if (refcount_dec_and_test(&cache->refs)) { 1263cad1284SJosef Bacik WARN_ON(cache->pinned > 0); 12740cdc509SFilipe Manana /* 12840cdc509SFilipe Manana * If there was a failure to cleanup a log tree, very likely due 12940cdc509SFilipe Manana * to an IO failure on a writeback attempt of one or more of its 13040cdc509SFilipe Manana * extent buffers, we could not do proper (and cheap) unaccounting 13140cdc509SFilipe Manana * of their reserved space, so don't warn on reserved > 0 in that 13240cdc509SFilipe Manana * case. 13340cdc509SFilipe Manana */ 13440cdc509SFilipe Manana if (!(cache->flags & BTRFS_BLOCK_GROUP_METADATA) || 13540cdc509SFilipe Manana !BTRFS_FS_LOG_CLEANUP_ERROR(cache->fs_info)) 1363cad1284SJosef Bacik WARN_ON(cache->reserved > 0); 1373cad1284SJosef Bacik 1383cad1284SJosef Bacik /* 139b0643e59SDennis Zhou * A block_group shouldn't be on the discard_list anymore. 140b0643e59SDennis Zhou * Remove the block_group from the discard_list to prevent us 141b0643e59SDennis Zhou * from causing a panic due to NULL pointer dereference. 142b0643e59SDennis Zhou */ 143b0643e59SDennis Zhou if (WARN_ON(!list_empty(&cache->discard_list))) 144b0643e59SDennis Zhou btrfs_discard_cancel_work(&cache->fs_info->discard_ctl, 145b0643e59SDennis Zhou cache); 146b0643e59SDennis Zhou 147b0643e59SDennis Zhou /* 1483cad1284SJosef Bacik * If not empty, someone is still holding mutex of 1493cad1284SJosef Bacik * full_stripe_lock, which can only be released by caller. 1503cad1284SJosef Bacik * And it will definitely cause use-after-free when caller 1513cad1284SJosef Bacik * tries to release full stripe lock. 1523cad1284SJosef Bacik * 1533cad1284SJosef Bacik * No better way to resolve, but only to warn. 1543cad1284SJosef Bacik */ 1553cad1284SJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root)); 1563cad1284SJosef Bacik kfree(cache->free_space_ctl); 157dafc340dSNaohiro Aota kfree(cache->physical_map); 1583cad1284SJosef Bacik kfree(cache); 1593cad1284SJosef Bacik } 1603cad1284SJosef Bacik } 1613cad1284SJosef Bacik 1622e405ad8SJosef Bacik /* 1634358d963SJosef Bacik * This adds the block group to the fs_info rb tree for the block group cache 1644358d963SJosef Bacik */ 1654358d963SJosef Bacik static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, 16632da5386SDavid Sterba struct btrfs_block_group *block_group) 1674358d963SJosef Bacik { 1684358d963SJosef Bacik struct rb_node **p; 1694358d963SJosef Bacik struct rb_node *parent = NULL; 17032da5386SDavid Sterba struct btrfs_block_group *cache; 1714358d963SJosef Bacik 1729afc6649SQu Wenruo ASSERT(block_group->length != 0); 1739afc6649SQu Wenruo 1744358d963SJosef Bacik spin_lock(&info->block_group_cache_lock); 1754358d963SJosef Bacik p = &info->block_group_cache_tree.rb_node; 1764358d963SJosef Bacik 1774358d963SJosef Bacik while (*p) { 1784358d963SJosef Bacik parent = *p; 17932da5386SDavid Sterba cache = rb_entry(parent, struct btrfs_block_group, cache_node); 180b3470b5dSDavid Sterba if (block_group->start < cache->start) { 1814358d963SJosef Bacik p = &(*p)->rb_left; 182b3470b5dSDavid Sterba } else if (block_group->start > cache->start) { 1834358d963SJosef Bacik p = &(*p)->rb_right; 1844358d963SJosef Bacik } else { 1854358d963SJosef Bacik spin_unlock(&info->block_group_cache_lock); 1864358d963SJosef Bacik return -EEXIST; 1874358d963SJosef Bacik } 1884358d963SJosef Bacik } 1894358d963SJosef Bacik 1904358d963SJosef Bacik rb_link_node(&block_group->cache_node, parent, p); 1914358d963SJosef Bacik rb_insert_color(&block_group->cache_node, 1924358d963SJosef Bacik &info->block_group_cache_tree); 1934358d963SJosef Bacik 194b3470b5dSDavid Sterba if (info->first_logical_byte > block_group->start) 195b3470b5dSDavid Sterba info->first_logical_byte = block_group->start; 1964358d963SJosef Bacik 1974358d963SJosef Bacik spin_unlock(&info->block_group_cache_lock); 1984358d963SJosef Bacik 1994358d963SJosef Bacik return 0; 2004358d963SJosef Bacik } 2014358d963SJosef Bacik 2024358d963SJosef Bacik /* 2032e405ad8SJosef Bacik * This will return the block group at or after bytenr if contains is 0, else 2042e405ad8SJosef Bacik * it will return the block group that contains the bytenr 2052e405ad8SJosef Bacik */ 20632da5386SDavid Sterba static struct btrfs_block_group *block_group_cache_tree_search( 2072e405ad8SJosef Bacik struct btrfs_fs_info *info, u64 bytenr, int contains) 2082e405ad8SJosef Bacik { 20932da5386SDavid Sterba struct btrfs_block_group *cache, *ret = NULL; 2102e405ad8SJosef Bacik struct rb_node *n; 2112e405ad8SJosef Bacik u64 end, start; 2122e405ad8SJosef Bacik 2132e405ad8SJosef Bacik spin_lock(&info->block_group_cache_lock); 2142e405ad8SJosef Bacik n = info->block_group_cache_tree.rb_node; 2152e405ad8SJosef Bacik 2162e405ad8SJosef Bacik while (n) { 21732da5386SDavid Sterba cache = rb_entry(n, struct btrfs_block_group, cache_node); 218b3470b5dSDavid Sterba end = cache->start + cache->length - 1; 219b3470b5dSDavid Sterba start = cache->start; 2202e405ad8SJosef Bacik 2212e405ad8SJosef Bacik if (bytenr < start) { 222b3470b5dSDavid Sterba if (!contains && (!ret || start < ret->start)) 2232e405ad8SJosef Bacik ret = cache; 2242e405ad8SJosef Bacik n = n->rb_left; 2252e405ad8SJosef Bacik } else if (bytenr > start) { 2262e405ad8SJosef Bacik if (contains && bytenr <= end) { 2272e405ad8SJosef Bacik ret = cache; 2282e405ad8SJosef Bacik break; 2292e405ad8SJosef Bacik } 2302e405ad8SJosef Bacik n = n->rb_right; 2312e405ad8SJosef Bacik } else { 2322e405ad8SJosef Bacik ret = cache; 2332e405ad8SJosef Bacik break; 2342e405ad8SJosef Bacik } 2352e405ad8SJosef Bacik } 2362e405ad8SJosef Bacik if (ret) { 2372e405ad8SJosef Bacik btrfs_get_block_group(ret); 238b3470b5dSDavid Sterba if (bytenr == 0 && info->first_logical_byte > ret->start) 239b3470b5dSDavid Sterba info->first_logical_byte = ret->start; 2402e405ad8SJosef Bacik } 2412e405ad8SJosef Bacik spin_unlock(&info->block_group_cache_lock); 2422e405ad8SJosef Bacik 2432e405ad8SJosef Bacik return ret; 2442e405ad8SJosef Bacik } 2452e405ad8SJosef Bacik 2462e405ad8SJosef Bacik /* 2472e405ad8SJosef Bacik * Return the block group that starts at or after bytenr 2482e405ad8SJosef Bacik */ 24932da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_first_block_group( 2502e405ad8SJosef Bacik struct btrfs_fs_info *info, u64 bytenr) 2512e405ad8SJosef Bacik { 2522e405ad8SJosef Bacik return block_group_cache_tree_search(info, bytenr, 0); 2532e405ad8SJosef Bacik } 2542e405ad8SJosef Bacik 2552e405ad8SJosef Bacik /* 2562e405ad8SJosef Bacik * Return the block group that contains the given bytenr 2572e405ad8SJosef Bacik */ 25832da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_block_group( 2592e405ad8SJosef Bacik struct btrfs_fs_info *info, u64 bytenr) 2602e405ad8SJosef Bacik { 2612e405ad8SJosef Bacik return block_group_cache_tree_search(info, bytenr, 1); 2622e405ad8SJosef Bacik } 2632e405ad8SJosef Bacik 26432da5386SDavid Sterba struct btrfs_block_group *btrfs_next_block_group( 26532da5386SDavid Sterba struct btrfs_block_group *cache) 2662e405ad8SJosef Bacik { 2672e405ad8SJosef Bacik struct btrfs_fs_info *fs_info = cache->fs_info; 2682e405ad8SJosef Bacik struct rb_node *node; 2692e405ad8SJosef Bacik 2702e405ad8SJosef Bacik spin_lock(&fs_info->block_group_cache_lock); 2712e405ad8SJosef Bacik 2722e405ad8SJosef Bacik /* If our block group was removed, we need a full search. */ 2732e405ad8SJosef Bacik if (RB_EMPTY_NODE(&cache->cache_node)) { 274b3470b5dSDavid Sterba const u64 next_bytenr = cache->start + cache->length; 2752e405ad8SJosef Bacik 2762e405ad8SJosef Bacik spin_unlock(&fs_info->block_group_cache_lock); 2772e405ad8SJosef Bacik btrfs_put_block_group(cache); 2782e405ad8SJosef Bacik cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache; 2792e405ad8SJosef Bacik } 2802e405ad8SJosef Bacik node = rb_next(&cache->cache_node); 2812e405ad8SJosef Bacik btrfs_put_block_group(cache); 2822e405ad8SJosef Bacik if (node) { 28332da5386SDavid Sterba cache = rb_entry(node, struct btrfs_block_group, cache_node); 2842e405ad8SJosef Bacik btrfs_get_block_group(cache); 2852e405ad8SJosef Bacik } else 2862e405ad8SJosef Bacik cache = NULL; 2872e405ad8SJosef Bacik spin_unlock(&fs_info->block_group_cache_lock); 2882e405ad8SJosef Bacik return cache; 2892e405ad8SJosef Bacik } 2903eeb3226SJosef Bacik 2913eeb3226SJosef Bacik bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) 2923eeb3226SJosef Bacik { 29332da5386SDavid Sterba struct btrfs_block_group *bg; 2943eeb3226SJosef Bacik bool ret = true; 2953eeb3226SJosef Bacik 2963eeb3226SJosef Bacik bg = btrfs_lookup_block_group(fs_info, bytenr); 2973eeb3226SJosef Bacik if (!bg) 2983eeb3226SJosef Bacik return false; 2993eeb3226SJosef Bacik 3003eeb3226SJosef Bacik spin_lock(&bg->lock); 3013eeb3226SJosef Bacik if (bg->ro) 3023eeb3226SJosef Bacik ret = false; 3033eeb3226SJosef Bacik else 3043eeb3226SJosef Bacik atomic_inc(&bg->nocow_writers); 3053eeb3226SJosef Bacik spin_unlock(&bg->lock); 3063eeb3226SJosef Bacik 3073eeb3226SJosef Bacik /* No put on block group, done by btrfs_dec_nocow_writers */ 3083eeb3226SJosef Bacik if (!ret) 3093eeb3226SJosef Bacik btrfs_put_block_group(bg); 3103eeb3226SJosef Bacik 3113eeb3226SJosef Bacik return ret; 3123eeb3226SJosef Bacik } 3133eeb3226SJosef Bacik 3143eeb3226SJosef Bacik void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) 3153eeb3226SJosef Bacik { 31632da5386SDavid Sterba struct btrfs_block_group *bg; 3173eeb3226SJosef Bacik 3183eeb3226SJosef Bacik bg = btrfs_lookup_block_group(fs_info, bytenr); 3193eeb3226SJosef Bacik ASSERT(bg); 3203eeb3226SJosef Bacik if (atomic_dec_and_test(&bg->nocow_writers)) 3213eeb3226SJosef Bacik wake_up_var(&bg->nocow_writers); 3223eeb3226SJosef Bacik /* 3233eeb3226SJosef Bacik * Once for our lookup and once for the lookup done by a previous call 3243eeb3226SJosef Bacik * to btrfs_inc_nocow_writers() 3253eeb3226SJosef Bacik */ 3263eeb3226SJosef Bacik btrfs_put_block_group(bg); 3273eeb3226SJosef Bacik btrfs_put_block_group(bg); 3283eeb3226SJosef Bacik } 3293eeb3226SJosef Bacik 33032da5386SDavid Sterba void btrfs_wait_nocow_writers(struct btrfs_block_group *bg) 3313eeb3226SJosef Bacik { 3323eeb3226SJosef Bacik wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers)); 3333eeb3226SJosef Bacik } 3343eeb3226SJosef Bacik 3353eeb3226SJosef Bacik void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, 3363eeb3226SJosef Bacik const u64 start) 3373eeb3226SJosef Bacik { 33832da5386SDavid Sterba struct btrfs_block_group *bg; 3393eeb3226SJosef Bacik 3403eeb3226SJosef Bacik bg = btrfs_lookup_block_group(fs_info, start); 3413eeb3226SJosef Bacik ASSERT(bg); 3423eeb3226SJosef Bacik if (atomic_dec_and_test(&bg->reservations)) 3433eeb3226SJosef Bacik wake_up_var(&bg->reservations); 3443eeb3226SJosef Bacik btrfs_put_block_group(bg); 3453eeb3226SJosef Bacik } 3463eeb3226SJosef Bacik 34732da5386SDavid Sterba void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg) 3483eeb3226SJosef Bacik { 3493eeb3226SJosef Bacik struct btrfs_space_info *space_info = bg->space_info; 3503eeb3226SJosef Bacik 3513eeb3226SJosef Bacik ASSERT(bg->ro); 3523eeb3226SJosef Bacik 3533eeb3226SJosef Bacik if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA)) 3543eeb3226SJosef Bacik return; 3553eeb3226SJosef Bacik 3563eeb3226SJosef Bacik /* 3573eeb3226SJosef Bacik * Our block group is read only but before we set it to read only, 3583eeb3226SJosef Bacik * some task might have had allocated an extent from it already, but it 3593eeb3226SJosef Bacik * has not yet created a respective ordered extent (and added it to a 3603eeb3226SJosef Bacik * root's list of ordered extents). 3613eeb3226SJosef Bacik * Therefore wait for any task currently allocating extents, since the 3623eeb3226SJosef Bacik * block group's reservations counter is incremented while a read lock 3633eeb3226SJosef Bacik * on the groups' semaphore is held and decremented after releasing 3643eeb3226SJosef Bacik * the read access on that semaphore and creating the ordered extent. 3653eeb3226SJosef Bacik */ 3663eeb3226SJosef Bacik down_write(&space_info->groups_sem); 3673eeb3226SJosef Bacik up_write(&space_info->groups_sem); 3683eeb3226SJosef Bacik 3693eeb3226SJosef Bacik wait_var_event(&bg->reservations, !atomic_read(&bg->reservations)); 3703eeb3226SJosef Bacik } 3719f21246dSJosef Bacik 3729f21246dSJosef Bacik struct btrfs_caching_control *btrfs_get_caching_control( 37332da5386SDavid Sterba struct btrfs_block_group *cache) 3749f21246dSJosef Bacik { 3759f21246dSJosef Bacik struct btrfs_caching_control *ctl; 3769f21246dSJosef Bacik 3779f21246dSJosef Bacik spin_lock(&cache->lock); 3789f21246dSJosef Bacik if (!cache->caching_ctl) { 3799f21246dSJosef Bacik spin_unlock(&cache->lock); 3809f21246dSJosef Bacik return NULL; 3819f21246dSJosef Bacik } 3829f21246dSJosef Bacik 3839f21246dSJosef Bacik ctl = cache->caching_ctl; 3849f21246dSJosef Bacik refcount_inc(&ctl->count); 3859f21246dSJosef Bacik spin_unlock(&cache->lock); 3869f21246dSJosef Bacik return ctl; 3879f21246dSJosef Bacik } 3889f21246dSJosef Bacik 3899f21246dSJosef Bacik void btrfs_put_caching_control(struct btrfs_caching_control *ctl) 3909f21246dSJosef Bacik { 3919f21246dSJosef Bacik if (refcount_dec_and_test(&ctl->count)) 3929f21246dSJosef Bacik kfree(ctl); 3939f21246dSJosef Bacik } 3949f21246dSJosef Bacik 3959f21246dSJosef Bacik /* 3969f21246dSJosef Bacik * When we wait for progress in the block group caching, its because our 3979f21246dSJosef Bacik * allocation attempt failed at least once. So, we must sleep and let some 3989f21246dSJosef Bacik * progress happen before we try again. 3999f21246dSJosef Bacik * 4009f21246dSJosef Bacik * This function will sleep at least once waiting for new free space to show 4019f21246dSJosef Bacik * up, and then it will check the block group free space numbers for our min 4029f21246dSJosef Bacik * num_bytes. Another option is to have it go ahead and look in the rbtree for 4039f21246dSJosef Bacik * a free extent of a given size, but this is a good start. 4049f21246dSJosef Bacik * 4059f21246dSJosef Bacik * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using 4069f21246dSJosef Bacik * any of the information in this block group. 4079f21246dSJosef Bacik */ 40832da5386SDavid Sterba void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache, 4099f21246dSJosef Bacik u64 num_bytes) 4109f21246dSJosef Bacik { 4119f21246dSJosef Bacik struct btrfs_caching_control *caching_ctl; 4129f21246dSJosef Bacik 4139f21246dSJosef Bacik caching_ctl = btrfs_get_caching_control(cache); 4149f21246dSJosef Bacik if (!caching_ctl) 4159f21246dSJosef Bacik return; 4169f21246dSJosef Bacik 41732da5386SDavid Sterba wait_event(caching_ctl->wait, btrfs_block_group_done(cache) || 4189f21246dSJosef Bacik (cache->free_space_ctl->free_space >= num_bytes)); 4199f21246dSJosef Bacik 4209f21246dSJosef Bacik btrfs_put_caching_control(caching_ctl); 4219f21246dSJosef Bacik } 4229f21246dSJosef Bacik 42332da5386SDavid Sterba int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache) 4249f21246dSJosef Bacik { 4259f21246dSJosef Bacik struct btrfs_caching_control *caching_ctl; 4269f21246dSJosef Bacik int ret = 0; 4279f21246dSJosef Bacik 4289f21246dSJosef Bacik caching_ctl = btrfs_get_caching_control(cache); 4299f21246dSJosef Bacik if (!caching_ctl) 4309f21246dSJosef Bacik return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; 4319f21246dSJosef Bacik 43232da5386SDavid Sterba wait_event(caching_ctl->wait, btrfs_block_group_done(cache)); 4339f21246dSJosef Bacik if (cache->cached == BTRFS_CACHE_ERROR) 4349f21246dSJosef Bacik ret = -EIO; 4359f21246dSJosef Bacik btrfs_put_caching_control(caching_ctl); 4369f21246dSJosef Bacik return ret; 4379f21246dSJosef Bacik } 4389f21246dSJosef Bacik 439e747853cSJosef Bacik static bool space_cache_v1_done(struct btrfs_block_group *cache) 440e747853cSJosef Bacik { 441e747853cSJosef Bacik bool ret; 442e747853cSJosef Bacik 443e747853cSJosef Bacik spin_lock(&cache->lock); 444e747853cSJosef Bacik ret = cache->cached != BTRFS_CACHE_FAST; 445e747853cSJosef Bacik spin_unlock(&cache->lock); 446e747853cSJosef Bacik 447e747853cSJosef Bacik return ret; 448e747853cSJosef Bacik } 449e747853cSJosef Bacik 450e747853cSJosef Bacik void btrfs_wait_space_cache_v1_finished(struct btrfs_block_group *cache, 451e747853cSJosef Bacik struct btrfs_caching_control *caching_ctl) 452e747853cSJosef Bacik { 453e747853cSJosef Bacik wait_event(caching_ctl->wait, space_cache_v1_done(cache)); 454e747853cSJosef Bacik } 455e747853cSJosef Bacik 4569f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 45732da5386SDavid Sterba static void fragment_free_space(struct btrfs_block_group *block_group) 4589f21246dSJosef Bacik { 4599f21246dSJosef Bacik struct btrfs_fs_info *fs_info = block_group->fs_info; 460b3470b5dSDavid Sterba u64 start = block_group->start; 461b3470b5dSDavid Sterba u64 len = block_group->length; 4629f21246dSJosef Bacik u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? 4639f21246dSJosef Bacik fs_info->nodesize : fs_info->sectorsize; 4649f21246dSJosef Bacik u64 step = chunk << 1; 4659f21246dSJosef Bacik 4669f21246dSJosef Bacik while (len > chunk) { 4679f21246dSJosef Bacik btrfs_remove_free_space(block_group, start, chunk); 4689f21246dSJosef Bacik start += step; 4699f21246dSJosef Bacik if (len < step) 4709f21246dSJosef Bacik len = 0; 4719f21246dSJosef Bacik else 4729f21246dSJosef Bacik len -= step; 4739f21246dSJosef Bacik } 4749f21246dSJosef Bacik } 4759f21246dSJosef Bacik #endif 4769f21246dSJosef Bacik 4779f21246dSJosef Bacik /* 4789f21246dSJosef Bacik * This is only called by btrfs_cache_block_group, since we could have freed 4799f21246dSJosef Bacik * extents we need to check the pinned_extents for any extents that can't be 4809f21246dSJosef Bacik * used yet since their free space will be released as soon as the transaction 4819f21246dSJosef Bacik * commits. 4829f21246dSJosef Bacik */ 48332da5386SDavid Sterba u64 add_new_free_space(struct btrfs_block_group *block_group, u64 start, u64 end) 4849f21246dSJosef Bacik { 4859f21246dSJosef Bacik struct btrfs_fs_info *info = block_group->fs_info; 4869f21246dSJosef Bacik u64 extent_start, extent_end, size, total_added = 0; 4879f21246dSJosef Bacik int ret; 4889f21246dSJosef Bacik 4899f21246dSJosef Bacik while (start < end) { 490fe119a6eSNikolay Borisov ret = find_first_extent_bit(&info->excluded_extents, start, 4919f21246dSJosef Bacik &extent_start, &extent_end, 4929f21246dSJosef Bacik EXTENT_DIRTY | EXTENT_UPTODATE, 4939f21246dSJosef Bacik NULL); 4949f21246dSJosef Bacik if (ret) 4959f21246dSJosef Bacik break; 4969f21246dSJosef Bacik 4979f21246dSJosef Bacik if (extent_start <= start) { 4989f21246dSJosef Bacik start = extent_end + 1; 4999f21246dSJosef Bacik } else if (extent_start > start && extent_start < end) { 5009f21246dSJosef Bacik size = extent_start - start; 5019f21246dSJosef Bacik total_added += size; 502b0643e59SDennis Zhou ret = btrfs_add_free_space_async_trimmed(block_group, 503b0643e59SDennis Zhou start, size); 5049f21246dSJosef Bacik BUG_ON(ret); /* -ENOMEM or logic error */ 5059f21246dSJosef Bacik start = extent_end + 1; 5069f21246dSJosef Bacik } else { 5079f21246dSJosef Bacik break; 5089f21246dSJosef Bacik } 5099f21246dSJosef Bacik } 5109f21246dSJosef Bacik 5119f21246dSJosef Bacik if (start < end) { 5129f21246dSJosef Bacik size = end - start; 5139f21246dSJosef Bacik total_added += size; 514b0643e59SDennis Zhou ret = btrfs_add_free_space_async_trimmed(block_group, start, 515b0643e59SDennis Zhou size); 5169f21246dSJosef Bacik BUG_ON(ret); /* -ENOMEM or logic error */ 5179f21246dSJosef Bacik } 5189f21246dSJosef Bacik 5199f21246dSJosef Bacik return total_added; 5209f21246dSJosef Bacik } 5219f21246dSJosef Bacik 5229f21246dSJosef Bacik static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl) 5239f21246dSJosef Bacik { 52432da5386SDavid Sterba struct btrfs_block_group *block_group = caching_ctl->block_group; 5259f21246dSJosef Bacik struct btrfs_fs_info *fs_info = block_group->fs_info; 52629cbcf40SJosef Bacik struct btrfs_root *extent_root; 5279f21246dSJosef Bacik struct btrfs_path *path; 5289f21246dSJosef Bacik struct extent_buffer *leaf; 5299f21246dSJosef Bacik struct btrfs_key key; 5309f21246dSJosef Bacik u64 total_found = 0; 5319f21246dSJosef Bacik u64 last = 0; 5329f21246dSJosef Bacik u32 nritems; 5339f21246dSJosef Bacik int ret; 5349f21246dSJosef Bacik bool wakeup = true; 5359f21246dSJosef Bacik 5369f21246dSJosef Bacik path = btrfs_alloc_path(); 5379f21246dSJosef Bacik if (!path) 5389f21246dSJosef Bacik return -ENOMEM; 5399f21246dSJosef Bacik 540b3470b5dSDavid Sterba last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET); 54129cbcf40SJosef Bacik extent_root = btrfs_extent_root(fs_info, last); 5429f21246dSJosef Bacik 5439f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 5449f21246dSJosef Bacik /* 5459f21246dSJosef Bacik * If we're fragmenting we don't want to make anybody think we can 5469f21246dSJosef Bacik * allocate from this block group until we've had a chance to fragment 5479f21246dSJosef Bacik * the free space. 5489f21246dSJosef Bacik */ 5499f21246dSJosef Bacik if (btrfs_should_fragment_free_space(block_group)) 5509f21246dSJosef Bacik wakeup = false; 5519f21246dSJosef Bacik #endif 5529f21246dSJosef Bacik /* 5539f21246dSJosef Bacik * We don't want to deadlock with somebody trying to allocate a new 5549f21246dSJosef Bacik * extent for the extent root while also trying to search the extent 5559f21246dSJosef Bacik * root to add free space. So we skip locking and search the commit 5569f21246dSJosef Bacik * root, since its read-only 5579f21246dSJosef Bacik */ 5589f21246dSJosef Bacik path->skip_locking = 1; 5599f21246dSJosef Bacik path->search_commit_root = 1; 5609f21246dSJosef Bacik path->reada = READA_FORWARD; 5619f21246dSJosef Bacik 5629f21246dSJosef Bacik key.objectid = last; 5639f21246dSJosef Bacik key.offset = 0; 5649f21246dSJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 5659f21246dSJosef Bacik 5669f21246dSJosef Bacik next: 5679f21246dSJosef Bacik ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 5689f21246dSJosef Bacik if (ret < 0) 5699f21246dSJosef Bacik goto out; 5709f21246dSJosef Bacik 5719f21246dSJosef Bacik leaf = path->nodes[0]; 5729f21246dSJosef Bacik nritems = btrfs_header_nritems(leaf); 5739f21246dSJosef Bacik 5749f21246dSJosef Bacik while (1) { 5759f21246dSJosef Bacik if (btrfs_fs_closing(fs_info) > 1) { 5769f21246dSJosef Bacik last = (u64)-1; 5779f21246dSJosef Bacik break; 5789f21246dSJosef Bacik } 5799f21246dSJosef Bacik 5809f21246dSJosef Bacik if (path->slots[0] < nritems) { 5819f21246dSJosef Bacik btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 5829f21246dSJosef Bacik } else { 5839f21246dSJosef Bacik ret = btrfs_find_next_key(extent_root, path, &key, 0, 0); 5849f21246dSJosef Bacik if (ret) 5859f21246dSJosef Bacik break; 5869f21246dSJosef Bacik 5879f21246dSJosef Bacik if (need_resched() || 5889f21246dSJosef Bacik rwsem_is_contended(&fs_info->commit_root_sem)) { 5899f21246dSJosef Bacik if (wakeup) 5909f21246dSJosef Bacik caching_ctl->progress = last; 5919f21246dSJosef Bacik btrfs_release_path(path); 5929f21246dSJosef Bacik up_read(&fs_info->commit_root_sem); 5939f21246dSJosef Bacik mutex_unlock(&caching_ctl->mutex); 5949f21246dSJosef Bacik cond_resched(); 5959f21246dSJosef Bacik mutex_lock(&caching_ctl->mutex); 5969f21246dSJosef Bacik down_read(&fs_info->commit_root_sem); 5979f21246dSJosef Bacik goto next; 5989f21246dSJosef Bacik } 5999f21246dSJosef Bacik 6009f21246dSJosef Bacik ret = btrfs_next_leaf(extent_root, path); 6019f21246dSJosef Bacik if (ret < 0) 6029f21246dSJosef Bacik goto out; 6039f21246dSJosef Bacik if (ret) 6049f21246dSJosef Bacik break; 6059f21246dSJosef Bacik leaf = path->nodes[0]; 6069f21246dSJosef Bacik nritems = btrfs_header_nritems(leaf); 6079f21246dSJosef Bacik continue; 6089f21246dSJosef Bacik } 6099f21246dSJosef Bacik 6109f21246dSJosef Bacik if (key.objectid < last) { 6119f21246dSJosef Bacik key.objectid = last; 6129f21246dSJosef Bacik key.offset = 0; 6139f21246dSJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 6149f21246dSJosef Bacik 6159f21246dSJosef Bacik if (wakeup) 6169f21246dSJosef Bacik caching_ctl->progress = last; 6179f21246dSJosef Bacik btrfs_release_path(path); 6189f21246dSJosef Bacik goto next; 6199f21246dSJosef Bacik } 6209f21246dSJosef Bacik 621b3470b5dSDavid Sterba if (key.objectid < block_group->start) { 6229f21246dSJosef Bacik path->slots[0]++; 6239f21246dSJosef Bacik continue; 6249f21246dSJosef Bacik } 6259f21246dSJosef Bacik 626b3470b5dSDavid Sterba if (key.objectid >= block_group->start + block_group->length) 6279f21246dSJosef Bacik break; 6289f21246dSJosef Bacik 6299f21246dSJosef Bacik if (key.type == BTRFS_EXTENT_ITEM_KEY || 6309f21246dSJosef Bacik key.type == BTRFS_METADATA_ITEM_KEY) { 6319f21246dSJosef Bacik total_found += add_new_free_space(block_group, last, 6329f21246dSJosef Bacik key.objectid); 6339f21246dSJosef Bacik if (key.type == BTRFS_METADATA_ITEM_KEY) 6349f21246dSJosef Bacik last = key.objectid + 6359f21246dSJosef Bacik fs_info->nodesize; 6369f21246dSJosef Bacik else 6379f21246dSJosef Bacik last = key.objectid + key.offset; 6389f21246dSJosef Bacik 6399f21246dSJosef Bacik if (total_found > CACHING_CTL_WAKE_UP) { 6409f21246dSJosef Bacik total_found = 0; 6419f21246dSJosef Bacik if (wakeup) 6429f21246dSJosef Bacik wake_up(&caching_ctl->wait); 6439f21246dSJosef Bacik } 6449f21246dSJosef Bacik } 6459f21246dSJosef Bacik path->slots[0]++; 6469f21246dSJosef Bacik } 6479f21246dSJosef Bacik ret = 0; 6489f21246dSJosef Bacik 6499f21246dSJosef Bacik total_found += add_new_free_space(block_group, last, 650b3470b5dSDavid Sterba block_group->start + block_group->length); 6519f21246dSJosef Bacik caching_ctl->progress = (u64)-1; 6529f21246dSJosef Bacik 6539f21246dSJosef Bacik out: 6549f21246dSJosef Bacik btrfs_free_path(path); 6559f21246dSJosef Bacik return ret; 6569f21246dSJosef Bacik } 6579f21246dSJosef Bacik 6589f21246dSJosef Bacik static noinline void caching_thread(struct btrfs_work *work) 6599f21246dSJosef Bacik { 66032da5386SDavid Sterba struct btrfs_block_group *block_group; 6619f21246dSJosef Bacik struct btrfs_fs_info *fs_info; 6629f21246dSJosef Bacik struct btrfs_caching_control *caching_ctl; 6639f21246dSJosef Bacik int ret; 6649f21246dSJosef Bacik 6659f21246dSJosef Bacik caching_ctl = container_of(work, struct btrfs_caching_control, work); 6669f21246dSJosef Bacik block_group = caching_ctl->block_group; 6679f21246dSJosef Bacik fs_info = block_group->fs_info; 6689f21246dSJosef Bacik 6699f21246dSJosef Bacik mutex_lock(&caching_ctl->mutex); 6709f21246dSJosef Bacik down_read(&fs_info->commit_root_sem); 6719f21246dSJosef Bacik 672e747853cSJosef Bacik if (btrfs_test_opt(fs_info, SPACE_CACHE)) { 673e747853cSJosef Bacik ret = load_free_space_cache(block_group); 674e747853cSJosef Bacik if (ret == 1) { 675e747853cSJosef Bacik ret = 0; 676e747853cSJosef Bacik goto done; 677e747853cSJosef Bacik } 678e747853cSJosef Bacik 679e747853cSJosef Bacik /* 680e747853cSJosef Bacik * We failed to load the space cache, set ourselves to 681e747853cSJosef Bacik * CACHE_STARTED and carry on. 682e747853cSJosef Bacik */ 683e747853cSJosef Bacik spin_lock(&block_group->lock); 684e747853cSJosef Bacik block_group->cached = BTRFS_CACHE_STARTED; 685e747853cSJosef Bacik spin_unlock(&block_group->lock); 686e747853cSJosef Bacik wake_up(&caching_ctl->wait); 687e747853cSJosef Bacik } 688e747853cSJosef Bacik 6892f96e402SJosef Bacik /* 6902f96e402SJosef Bacik * If we are in the transaction that populated the free space tree we 6912f96e402SJosef Bacik * can't actually cache from the free space tree as our commit root and 6922f96e402SJosef Bacik * real root are the same, so we could change the contents of the blocks 6932f96e402SJosef Bacik * while caching. Instead do the slow caching in this case, and after 6942f96e402SJosef Bacik * the transaction has committed we will be safe. 6952f96e402SJosef Bacik */ 6962f96e402SJosef Bacik if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 6972f96e402SJosef Bacik !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags))) 6989f21246dSJosef Bacik ret = load_free_space_tree(caching_ctl); 6999f21246dSJosef Bacik else 7009f21246dSJosef Bacik ret = load_extent_tree_free(caching_ctl); 701e747853cSJosef Bacik done: 7029f21246dSJosef Bacik spin_lock(&block_group->lock); 7039f21246dSJosef Bacik block_group->caching_ctl = NULL; 7049f21246dSJosef Bacik block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; 7059f21246dSJosef Bacik spin_unlock(&block_group->lock); 7069f21246dSJosef Bacik 7079f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 7089f21246dSJosef Bacik if (btrfs_should_fragment_free_space(block_group)) { 7099f21246dSJosef Bacik u64 bytes_used; 7109f21246dSJosef Bacik 7119f21246dSJosef Bacik spin_lock(&block_group->space_info->lock); 7129f21246dSJosef Bacik spin_lock(&block_group->lock); 713b3470b5dSDavid Sterba bytes_used = block_group->length - block_group->used; 7149f21246dSJosef Bacik block_group->space_info->bytes_used += bytes_used >> 1; 7159f21246dSJosef Bacik spin_unlock(&block_group->lock); 7169f21246dSJosef Bacik spin_unlock(&block_group->space_info->lock); 717e11c0406SJosef Bacik fragment_free_space(block_group); 7189f21246dSJosef Bacik } 7199f21246dSJosef Bacik #endif 7209f21246dSJosef Bacik 7219f21246dSJosef Bacik caching_ctl->progress = (u64)-1; 7229f21246dSJosef Bacik 7239f21246dSJosef Bacik up_read(&fs_info->commit_root_sem); 7249f21246dSJosef Bacik btrfs_free_excluded_extents(block_group); 7259f21246dSJosef Bacik mutex_unlock(&caching_ctl->mutex); 7269f21246dSJosef Bacik 7279f21246dSJosef Bacik wake_up(&caching_ctl->wait); 7289f21246dSJosef Bacik 7299f21246dSJosef Bacik btrfs_put_caching_control(caching_ctl); 7309f21246dSJosef Bacik btrfs_put_block_group(block_group); 7319f21246dSJosef Bacik } 7329f21246dSJosef Bacik 73332da5386SDavid Sterba int btrfs_cache_block_group(struct btrfs_block_group *cache, int load_cache_only) 7349f21246dSJosef Bacik { 7359f21246dSJosef Bacik DEFINE_WAIT(wait); 7369f21246dSJosef Bacik struct btrfs_fs_info *fs_info = cache->fs_info; 737e747853cSJosef Bacik struct btrfs_caching_control *caching_ctl = NULL; 7389f21246dSJosef Bacik int ret = 0; 7399f21246dSJosef Bacik 7402eda5708SNaohiro Aota /* Allocator for zoned filesystems does not use the cache at all */ 7412eda5708SNaohiro Aota if (btrfs_is_zoned(fs_info)) 7422eda5708SNaohiro Aota return 0; 7432eda5708SNaohiro Aota 7449f21246dSJosef Bacik caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); 7459f21246dSJosef Bacik if (!caching_ctl) 7469f21246dSJosef Bacik return -ENOMEM; 7479f21246dSJosef Bacik 7489f21246dSJosef Bacik INIT_LIST_HEAD(&caching_ctl->list); 7499f21246dSJosef Bacik mutex_init(&caching_ctl->mutex); 7509f21246dSJosef Bacik init_waitqueue_head(&caching_ctl->wait); 7519f21246dSJosef Bacik caching_ctl->block_group = cache; 752b3470b5dSDavid Sterba caching_ctl->progress = cache->start; 753e747853cSJosef Bacik refcount_set(&caching_ctl->count, 2); 754a0cac0ecSOmar Sandoval btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL); 7559f21246dSJosef Bacik 7569f21246dSJosef Bacik spin_lock(&cache->lock); 7579f21246dSJosef Bacik if (cache->cached != BTRFS_CACHE_NO) { 7589f21246dSJosef Bacik kfree(caching_ctl); 759e747853cSJosef Bacik 760e747853cSJosef Bacik caching_ctl = cache->caching_ctl; 761e747853cSJosef Bacik if (caching_ctl) 762e747853cSJosef Bacik refcount_inc(&caching_ctl->count); 763e747853cSJosef Bacik spin_unlock(&cache->lock); 764e747853cSJosef Bacik goto out; 7659f21246dSJosef Bacik } 7669f21246dSJosef Bacik WARN_ON(cache->caching_ctl); 7679f21246dSJosef Bacik cache->caching_ctl = caching_ctl; 768e747853cSJosef Bacik if (btrfs_test_opt(fs_info, SPACE_CACHE)) 7699f21246dSJosef Bacik cache->cached = BTRFS_CACHE_FAST; 770e747853cSJosef Bacik else 7719f21246dSJosef Bacik cache->cached = BTRFS_CACHE_STARTED; 7729f21246dSJosef Bacik cache->has_caching_ctl = 1; 7739f21246dSJosef Bacik spin_unlock(&cache->lock); 7749f21246dSJosef Bacik 775bbb86a37SJosef Bacik spin_lock(&fs_info->block_group_cache_lock); 7769f21246dSJosef Bacik refcount_inc(&caching_ctl->count); 7779f21246dSJosef Bacik list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); 778bbb86a37SJosef Bacik spin_unlock(&fs_info->block_group_cache_lock); 7799f21246dSJosef Bacik 7809f21246dSJosef Bacik btrfs_get_block_group(cache); 7819f21246dSJosef Bacik 7829f21246dSJosef Bacik btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); 783e747853cSJosef Bacik out: 784e747853cSJosef Bacik if (load_cache_only && caching_ctl) 785e747853cSJosef Bacik btrfs_wait_space_cache_v1_finished(cache, caching_ctl); 786e747853cSJosef Bacik if (caching_ctl) 787e747853cSJosef Bacik btrfs_put_caching_control(caching_ctl); 7889f21246dSJosef Bacik 7899f21246dSJosef Bacik return ret; 7909f21246dSJosef Bacik } 791e3e0520bSJosef Bacik 792e3e0520bSJosef Bacik static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) 793e3e0520bSJosef Bacik { 794e3e0520bSJosef Bacik u64 extra_flags = chunk_to_extended(flags) & 795e3e0520bSJosef Bacik BTRFS_EXTENDED_PROFILE_MASK; 796e3e0520bSJosef Bacik 797e3e0520bSJosef Bacik write_seqlock(&fs_info->profiles_lock); 798e3e0520bSJosef Bacik if (flags & BTRFS_BLOCK_GROUP_DATA) 799e3e0520bSJosef Bacik fs_info->avail_data_alloc_bits &= ~extra_flags; 800e3e0520bSJosef Bacik if (flags & BTRFS_BLOCK_GROUP_METADATA) 801e3e0520bSJosef Bacik fs_info->avail_metadata_alloc_bits &= ~extra_flags; 802e3e0520bSJosef Bacik if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 803e3e0520bSJosef Bacik fs_info->avail_system_alloc_bits &= ~extra_flags; 804e3e0520bSJosef Bacik write_sequnlock(&fs_info->profiles_lock); 805e3e0520bSJosef Bacik } 806e3e0520bSJosef Bacik 807e3e0520bSJosef Bacik /* 808e3e0520bSJosef Bacik * Clear incompat bits for the following feature(s): 809e3e0520bSJosef Bacik * 810e3e0520bSJosef Bacik * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group 811e3e0520bSJosef Bacik * in the whole filesystem 8129c907446SDavid Sterba * 8139c907446SDavid Sterba * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups 814e3e0520bSJosef Bacik */ 815e3e0520bSJosef Bacik static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags) 816e3e0520bSJosef Bacik { 8179c907446SDavid Sterba bool found_raid56 = false; 8189c907446SDavid Sterba bool found_raid1c34 = false; 8199c907446SDavid Sterba 8209c907446SDavid Sterba if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) || 8219c907446SDavid Sterba (flags & BTRFS_BLOCK_GROUP_RAID1C3) || 8229c907446SDavid Sterba (flags & BTRFS_BLOCK_GROUP_RAID1C4)) { 823e3e0520bSJosef Bacik struct list_head *head = &fs_info->space_info; 824e3e0520bSJosef Bacik struct btrfs_space_info *sinfo; 825e3e0520bSJosef Bacik 826e3e0520bSJosef Bacik list_for_each_entry_rcu(sinfo, head, list) { 827e3e0520bSJosef Bacik down_read(&sinfo->groups_sem); 828e3e0520bSJosef Bacik if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5])) 8299c907446SDavid Sterba found_raid56 = true; 830e3e0520bSJosef Bacik if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6])) 8319c907446SDavid Sterba found_raid56 = true; 8329c907446SDavid Sterba if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3])) 8339c907446SDavid Sterba found_raid1c34 = true; 8349c907446SDavid Sterba if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4])) 8359c907446SDavid Sterba found_raid1c34 = true; 836e3e0520bSJosef Bacik up_read(&sinfo->groups_sem); 837e3e0520bSJosef Bacik } 838d8e6fd5cSFilipe Manana if (!found_raid56) 839e3e0520bSJosef Bacik btrfs_clear_fs_incompat(fs_info, RAID56); 840d8e6fd5cSFilipe Manana if (!found_raid1c34) 8419c907446SDavid Sterba btrfs_clear_fs_incompat(fs_info, RAID1C34); 842e3e0520bSJosef Bacik } 843e3e0520bSJosef Bacik } 844e3e0520bSJosef Bacik 8457357623aSQu Wenruo static int remove_block_group_item(struct btrfs_trans_handle *trans, 8467357623aSQu Wenruo struct btrfs_path *path, 8477357623aSQu Wenruo struct btrfs_block_group *block_group) 8487357623aSQu Wenruo { 8497357623aSQu Wenruo struct btrfs_fs_info *fs_info = trans->fs_info; 8507357623aSQu Wenruo struct btrfs_root *root; 8517357623aSQu Wenruo struct btrfs_key key; 8527357623aSQu Wenruo int ret; 8537357623aSQu Wenruo 854dfe8aec4SJosef Bacik root = btrfs_block_group_root(fs_info); 8557357623aSQu Wenruo key.objectid = block_group->start; 8567357623aSQu Wenruo key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 8577357623aSQu Wenruo key.offset = block_group->length; 8587357623aSQu Wenruo 8597357623aSQu Wenruo ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 8607357623aSQu Wenruo if (ret > 0) 8617357623aSQu Wenruo ret = -ENOENT; 8627357623aSQu Wenruo if (ret < 0) 8637357623aSQu Wenruo return ret; 8647357623aSQu Wenruo 8657357623aSQu Wenruo ret = btrfs_del_item(trans, root, path); 8667357623aSQu Wenruo return ret; 8677357623aSQu Wenruo } 8687357623aSQu Wenruo 869e3e0520bSJosef Bacik int btrfs_remove_block_group(struct btrfs_trans_handle *trans, 870e3e0520bSJosef Bacik u64 group_start, struct extent_map *em) 871e3e0520bSJosef Bacik { 872e3e0520bSJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 873e3e0520bSJosef Bacik struct btrfs_path *path; 87432da5386SDavid Sterba struct btrfs_block_group *block_group; 875e3e0520bSJosef Bacik struct btrfs_free_cluster *cluster; 876e3e0520bSJosef Bacik struct inode *inode; 877e3e0520bSJosef Bacik struct kobject *kobj = NULL; 878e3e0520bSJosef Bacik int ret; 879e3e0520bSJosef Bacik int index; 880e3e0520bSJosef Bacik int factor; 881e3e0520bSJosef Bacik struct btrfs_caching_control *caching_ctl = NULL; 882e3e0520bSJosef Bacik bool remove_em; 883e3e0520bSJosef Bacik bool remove_rsv = false; 884e3e0520bSJosef Bacik 885e3e0520bSJosef Bacik block_group = btrfs_lookup_block_group(fs_info, group_start); 886e3e0520bSJosef Bacik BUG_ON(!block_group); 887e3e0520bSJosef Bacik BUG_ON(!block_group->ro); 888e3e0520bSJosef Bacik 889e3e0520bSJosef Bacik trace_btrfs_remove_block_group(block_group); 890e3e0520bSJosef Bacik /* 891e3e0520bSJosef Bacik * Free the reserved super bytes from this block group before 892e3e0520bSJosef Bacik * remove it. 893e3e0520bSJosef Bacik */ 894e3e0520bSJosef Bacik btrfs_free_excluded_extents(block_group); 895b3470b5dSDavid Sterba btrfs_free_ref_tree_range(fs_info, block_group->start, 896b3470b5dSDavid Sterba block_group->length); 897e3e0520bSJosef Bacik 898e3e0520bSJosef Bacik index = btrfs_bg_flags_to_raid_index(block_group->flags); 899e3e0520bSJosef Bacik factor = btrfs_bg_type_to_factor(block_group->flags); 900e3e0520bSJosef Bacik 901e3e0520bSJosef Bacik /* make sure this block group isn't part of an allocation cluster */ 902e3e0520bSJosef Bacik cluster = &fs_info->data_alloc_cluster; 903e3e0520bSJosef Bacik spin_lock(&cluster->refill_lock); 904e3e0520bSJosef Bacik btrfs_return_cluster_to_free_space(block_group, cluster); 905e3e0520bSJosef Bacik spin_unlock(&cluster->refill_lock); 906e3e0520bSJosef Bacik 907e3e0520bSJosef Bacik /* 908e3e0520bSJosef Bacik * make sure this block group isn't part of a metadata 909e3e0520bSJosef Bacik * allocation cluster 910e3e0520bSJosef Bacik */ 911e3e0520bSJosef Bacik cluster = &fs_info->meta_alloc_cluster; 912e3e0520bSJosef Bacik spin_lock(&cluster->refill_lock); 913e3e0520bSJosef Bacik btrfs_return_cluster_to_free_space(block_group, cluster); 914e3e0520bSJosef Bacik spin_unlock(&cluster->refill_lock); 915e3e0520bSJosef Bacik 91640ab3be1SNaohiro Aota btrfs_clear_treelog_bg(block_group); 917c2707a25SJohannes Thumshirn btrfs_clear_data_reloc_bg(block_group); 91840ab3be1SNaohiro Aota 919e3e0520bSJosef Bacik path = btrfs_alloc_path(); 920e3e0520bSJosef Bacik if (!path) { 921e3e0520bSJosef Bacik ret = -ENOMEM; 9229fecd132SFilipe Manana goto out; 923e3e0520bSJosef Bacik } 924e3e0520bSJosef Bacik 925e3e0520bSJosef Bacik /* 926e3e0520bSJosef Bacik * get the inode first so any iput calls done for the io_list 927e3e0520bSJosef Bacik * aren't the final iput (no unlinks allowed now) 928e3e0520bSJosef Bacik */ 929e3e0520bSJosef Bacik inode = lookup_free_space_inode(block_group, path); 930e3e0520bSJosef Bacik 931e3e0520bSJosef Bacik mutex_lock(&trans->transaction->cache_write_mutex); 932e3e0520bSJosef Bacik /* 933e3e0520bSJosef Bacik * Make sure our free space cache IO is done before removing the 934e3e0520bSJosef Bacik * free space inode 935e3e0520bSJosef Bacik */ 936e3e0520bSJosef Bacik spin_lock(&trans->transaction->dirty_bgs_lock); 937e3e0520bSJosef Bacik if (!list_empty(&block_group->io_list)) { 938e3e0520bSJosef Bacik list_del_init(&block_group->io_list); 939e3e0520bSJosef Bacik 940e3e0520bSJosef Bacik WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); 941e3e0520bSJosef Bacik 942e3e0520bSJosef Bacik spin_unlock(&trans->transaction->dirty_bgs_lock); 943e3e0520bSJosef Bacik btrfs_wait_cache_io(trans, block_group, path); 944e3e0520bSJosef Bacik btrfs_put_block_group(block_group); 945e3e0520bSJosef Bacik spin_lock(&trans->transaction->dirty_bgs_lock); 946e3e0520bSJosef Bacik } 947e3e0520bSJosef Bacik 948e3e0520bSJosef Bacik if (!list_empty(&block_group->dirty_list)) { 949e3e0520bSJosef Bacik list_del_init(&block_group->dirty_list); 950e3e0520bSJosef Bacik remove_rsv = true; 951e3e0520bSJosef Bacik btrfs_put_block_group(block_group); 952e3e0520bSJosef Bacik } 953e3e0520bSJosef Bacik spin_unlock(&trans->transaction->dirty_bgs_lock); 954e3e0520bSJosef Bacik mutex_unlock(&trans->transaction->cache_write_mutex); 955e3e0520bSJosef Bacik 95636b216c8SBoris Burkov ret = btrfs_remove_free_space_inode(trans, inode, block_group); 957e3e0520bSJosef Bacik if (ret) 9589fecd132SFilipe Manana goto out; 959e3e0520bSJosef Bacik 960e3e0520bSJosef Bacik spin_lock(&fs_info->block_group_cache_lock); 961e3e0520bSJosef Bacik rb_erase(&block_group->cache_node, 962e3e0520bSJosef Bacik &fs_info->block_group_cache_tree); 963e3e0520bSJosef Bacik RB_CLEAR_NODE(&block_group->cache_node); 964e3e0520bSJosef Bacik 9659fecd132SFilipe Manana /* Once for the block groups rbtree */ 9669fecd132SFilipe Manana btrfs_put_block_group(block_group); 9679fecd132SFilipe Manana 968b3470b5dSDavid Sterba if (fs_info->first_logical_byte == block_group->start) 969e3e0520bSJosef Bacik fs_info->first_logical_byte = (u64)-1; 970e3e0520bSJosef Bacik spin_unlock(&fs_info->block_group_cache_lock); 971e3e0520bSJosef Bacik 972e3e0520bSJosef Bacik down_write(&block_group->space_info->groups_sem); 973e3e0520bSJosef Bacik /* 974e3e0520bSJosef Bacik * we must use list_del_init so people can check to see if they 975e3e0520bSJosef Bacik * are still on the list after taking the semaphore 976e3e0520bSJosef Bacik */ 977e3e0520bSJosef Bacik list_del_init(&block_group->list); 978e3e0520bSJosef Bacik if (list_empty(&block_group->space_info->block_groups[index])) { 979e3e0520bSJosef Bacik kobj = block_group->space_info->block_group_kobjs[index]; 980e3e0520bSJosef Bacik block_group->space_info->block_group_kobjs[index] = NULL; 981e3e0520bSJosef Bacik clear_avail_alloc_bits(fs_info, block_group->flags); 982e3e0520bSJosef Bacik } 983e3e0520bSJosef Bacik up_write(&block_group->space_info->groups_sem); 984e3e0520bSJosef Bacik clear_incompat_bg_bits(fs_info, block_group->flags); 985e3e0520bSJosef Bacik if (kobj) { 986e3e0520bSJosef Bacik kobject_del(kobj); 987e3e0520bSJosef Bacik kobject_put(kobj); 988e3e0520bSJosef Bacik } 989e3e0520bSJosef Bacik 990e3e0520bSJosef Bacik if (block_group->has_caching_ctl) 991e3e0520bSJosef Bacik caching_ctl = btrfs_get_caching_control(block_group); 992e3e0520bSJosef Bacik if (block_group->cached == BTRFS_CACHE_STARTED) 993e3e0520bSJosef Bacik btrfs_wait_block_group_cache_done(block_group); 994e3e0520bSJosef Bacik if (block_group->has_caching_ctl) { 995bbb86a37SJosef Bacik spin_lock(&fs_info->block_group_cache_lock); 996e3e0520bSJosef Bacik if (!caching_ctl) { 997e3e0520bSJosef Bacik struct btrfs_caching_control *ctl; 998e3e0520bSJosef Bacik 999e3e0520bSJosef Bacik list_for_each_entry(ctl, 1000e3e0520bSJosef Bacik &fs_info->caching_block_groups, list) 1001e3e0520bSJosef Bacik if (ctl->block_group == block_group) { 1002e3e0520bSJosef Bacik caching_ctl = ctl; 1003e3e0520bSJosef Bacik refcount_inc(&caching_ctl->count); 1004e3e0520bSJosef Bacik break; 1005e3e0520bSJosef Bacik } 1006e3e0520bSJosef Bacik } 1007e3e0520bSJosef Bacik if (caching_ctl) 1008e3e0520bSJosef Bacik list_del_init(&caching_ctl->list); 1009bbb86a37SJosef Bacik spin_unlock(&fs_info->block_group_cache_lock); 1010e3e0520bSJosef Bacik if (caching_ctl) { 1011e3e0520bSJosef Bacik /* Once for the caching bgs list and once for us. */ 1012e3e0520bSJosef Bacik btrfs_put_caching_control(caching_ctl); 1013e3e0520bSJosef Bacik btrfs_put_caching_control(caching_ctl); 1014e3e0520bSJosef Bacik } 1015e3e0520bSJosef Bacik } 1016e3e0520bSJosef Bacik 1017e3e0520bSJosef Bacik spin_lock(&trans->transaction->dirty_bgs_lock); 1018e3e0520bSJosef Bacik WARN_ON(!list_empty(&block_group->dirty_list)); 1019e3e0520bSJosef Bacik WARN_ON(!list_empty(&block_group->io_list)); 1020e3e0520bSJosef Bacik spin_unlock(&trans->transaction->dirty_bgs_lock); 1021e3e0520bSJosef Bacik 1022e3e0520bSJosef Bacik btrfs_remove_free_space_cache(block_group); 1023e3e0520bSJosef Bacik 1024e3e0520bSJosef Bacik spin_lock(&block_group->space_info->lock); 1025e3e0520bSJosef Bacik list_del_init(&block_group->ro_list); 1026e3e0520bSJosef Bacik 1027e3e0520bSJosef Bacik if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { 1028e3e0520bSJosef Bacik WARN_ON(block_group->space_info->total_bytes 1029b3470b5dSDavid Sterba < block_group->length); 1030e3e0520bSJosef Bacik WARN_ON(block_group->space_info->bytes_readonly 1031169e0da9SNaohiro Aota < block_group->length - block_group->zone_unusable); 1032169e0da9SNaohiro Aota WARN_ON(block_group->space_info->bytes_zone_unusable 1033169e0da9SNaohiro Aota < block_group->zone_unusable); 1034e3e0520bSJosef Bacik WARN_ON(block_group->space_info->disk_total 1035b3470b5dSDavid Sterba < block_group->length * factor); 1036e3e0520bSJosef Bacik } 1037b3470b5dSDavid Sterba block_group->space_info->total_bytes -= block_group->length; 1038169e0da9SNaohiro Aota block_group->space_info->bytes_readonly -= 1039169e0da9SNaohiro Aota (block_group->length - block_group->zone_unusable); 1040169e0da9SNaohiro Aota block_group->space_info->bytes_zone_unusable -= 1041169e0da9SNaohiro Aota block_group->zone_unusable; 1042b3470b5dSDavid Sterba block_group->space_info->disk_total -= block_group->length * factor; 1043e3e0520bSJosef Bacik 1044e3e0520bSJosef Bacik spin_unlock(&block_group->space_info->lock); 1045e3e0520bSJosef Bacik 1046ffcb9d44SFilipe Manana /* 1047ffcb9d44SFilipe Manana * Remove the free space for the block group from the free space tree 1048ffcb9d44SFilipe Manana * and the block group's item from the extent tree before marking the 1049ffcb9d44SFilipe Manana * block group as removed. This is to prevent races with tasks that 1050ffcb9d44SFilipe Manana * freeze and unfreeze a block group, this task and another task 1051ffcb9d44SFilipe Manana * allocating a new block group - the unfreeze task ends up removing 1052ffcb9d44SFilipe Manana * the block group's extent map before the task calling this function 1053ffcb9d44SFilipe Manana * deletes the block group item from the extent tree, allowing for 1054ffcb9d44SFilipe Manana * another task to attempt to create another block group with the same 1055ffcb9d44SFilipe Manana * item key (and failing with -EEXIST and a transaction abort). 1056ffcb9d44SFilipe Manana */ 1057ffcb9d44SFilipe Manana ret = remove_block_group_free_space(trans, block_group); 1058ffcb9d44SFilipe Manana if (ret) 1059ffcb9d44SFilipe Manana goto out; 1060ffcb9d44SFilipe Manana 1061ffcb9d44SFilipe Manana ret = remove_block_group_item(trans, path, block_group); 1062ffcb9d44SFilipe Manana if (ret < 0) 1063ffcb9d44SFilipe Manana goto out; 1064ffcb9d44SFilipe Manana 1065e3e0520bSJosef Bacik spin_lock(&block_group->lock); 1066e3e0520bSJosef Bacik block_group->removed = 1; 1067e3e0520bSJosef Bacik /* 10686b7304afSFilipe Manana * At this point trimming or scrub can't start on this block group, 10696b7304afSFilipe Manana * because we removed the block group from the rbtree 10706b7304afSFilipe Manana * fs_info->block_group_cache_tree so no one can't find it anymore and 10716b7304afSFilipe Manana * even if someone already got this block group before we removed it 10726b7304afSFilipe Manana * from the rbtree, they have already incremented block_group->frozen - 10736b7304afSFilipe Manana * if they didn't, for the trimming case they won't find any free space 10746b7304afSFilipe Manana * entries because we already removed them all when we called 10756b7304afSFilipe Manana * btrfs_remove_free_space_cache(). 1076e3e0520bSJosef Bacik * 1077e3e0520bSJosef Bacik * And we must not remove the extent map from the fs_info->mapping_tree 1078e3e0520bSJosef Bacik * to prevent the same logical address range and physical device space 10796b7304afSFilipe Manana * ranges from being reused for a new block group. This is needed to 10806b7304afSFilipe Manana * avoid races with trimming and scrub. 10816b7304afSFilipe Manana * 10826b7304afSFilipe Manana * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is 1083e3e0520bSJosef Bacik * completely transactionless, so while it is trimming a range the 1084e3e0520bSJosef Bacik * currently running transaction might finish and a new one start, 1085e3e0520bSJosef Bacik * allowing for new block groups to be created that can reuse the same 1086e3e0520bSJosef Bacik * physical device locations unless we take this special care. 1087e3e0520bSJosef Bacik * 1088e3e0520bSJosef Bacik * There may also be an implicit trim operation if the file system 1089e3e0520bSJosef Bacik * is mounted with -odiscard. The same protections must remain 1090e3e0520bSJosef Bacik * in place until the extents have been discarded completely when 1091e3e0520bSJosef Bacik * the transaction commit has completed. 1092e3e0520bSJosef Bacik */ 10936b7304afSFilipe Manana remove_em = (atomic_read(&block_group->frozen) == 0); 1094e3e0520bSJosef Bacik spin_unlock(&block_group->lock); 1095e3e0520bSJosef Bacik 1096e3e0520bSJosef Bacik if (remove_em) { 1097e3e0520bSJosef Bacik struct extent_map_tree *em_tree; 1098e3e0520bSJosef Bacik 1099e3e0520bSJosef Bacik em_tree = &fs_info->mapping_tree; 1100e3e0520bSJosef Bacik write_lock(&em_tree->lock); 1101e3e0520bSJosef Bacik remove_extent_mapping(em_tree, em); 1102e3e0520bSJosef Bacik write_unlock(&em_tree->lock); 1103e3e0520bSJosef Bacik /* once for the tree */ 1104e3e0520bSJosef Bacik free_extent_map(em); 1105e3e0520bSJosef Bacik } 1106f6033c5eSXiyu Yang 11079fecd132SFilipe Manana out: 1108f6033c5eSXiyu Yang /* Once for the lookup reference */ 1109f6033c5eSXiyu Yang btrfs_put_block_group(block_group); 1110e3e0520bSJosef Bacik if (remove_rsv) 1111e3e0520bSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 1112e3e0520bSJosef Bacik btrfs_free_path(path); 1113e3e0520bSJosef Bacik return ret; 1114e3e0520bSJosef Bacik } 1115e3e0520bSJosef Bacik 1116e3e0520bSJosef Bacik struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( 1117e3e0520bSJosef Bacik struct btrfs_fs_info *fs_info, const u64 chunk_offset) 1118e3e0520bSJosef Bacik { 1119dfe8aec4SJosef Bacik struct btrfs_root *root = btrfs_block_group_root(fs_info); 1120e3e0520bSJosef Bacik struct extent_map_tree *em_tree = &fs_info->mapping_tree; 1121e3e0520bSJosef Bacik struct extent_map *em; 1122e3e0520bSJosef Bacik struct map_lookup *map; 1123e3e0520bSJosef Bacik unsigned int num_items; 1124e3e0520bSJosef Bacik 1125e3e0520bSJosef Bacik read_lock(&em_tree->lock); 1126e3e0520bSJosef Bacik em = lookup_extent_mapping(em_tree, chunk_offset, 1); 1127e3e0520bSJosef Bacik read_unlock(&em_tree->lock); 1128e3e0520bSJosef Bacik ASSERT(em && em->start == chunk_offset); 1129e3e0520bSJosef Bacik 1130e3e0520bSJosef Bacik /* 1131e3e0520bSJosef Bacik * We need to reserve 3 + N units from the metadata space info in order 1132e3e0520bSJosef Bacik * to remove a block group (done at btrfs_remove_chunk() and at 1133e3e0520bSJosef Bacik * btrfs_remove_block_group()), which are used for: 1134e3e0520bSJosef Bacik * 1135e3e0520bSJosef Bacik * 1 unit for adding the free space inode's orphan (located in the tree 1136e3e0520bSJosef Bacik * of tree roots). 1137e3e0520bSJosef Bacik * 1 unit for deleting the block group item (located in the extent 1138e3e0520bSJosef Bacik * tree). 1139e3e0520bSJosef Bacik * 1 unit for deleting the free space item (located in tree of tree 1140e3e0520bSJosef Bacik * roots). 1141e3e0520bSJosef Bacik * N units for deleting N device extent items corresponding to each 1142e3e0520bSJosef Bacik * stripe (located in the device tree). 1143e3e0520bSJosef Bacik * 1144e3e0520bSJosef Bacik * In order to remove a block group we also need to reserve units in the 1145e3e0520bSJosef Bacik * system space info in order to update the chunk tree (update one or 1146e3e0520bSJosef Bacik * more device items and remove one chunk item), but this is done at 1147e3e0520bSJosef Bacik * btrfs_remove_chunk() through a call to check_system_chunk(). 1148e3e0520bSJosef Bacik */ 1149e3e0520bSJosef Bacik map = em->map_lookup; 1150e3e0520bSJosef Bacik num_items = 3 + map->num_stripes; 1151e3e0520bSJosef Bacik free_extent_map(em); 1152e3e0520bSJosef Bacik 1153dfe8aec4SJosef Bacik return btrfs_start_transaction_fallback_global_rsv(root, num_items); 1154e3e0520bSJosef Bacik } 1155e3e0520bSJosef Bacik 1156e3e0520bSJosef Bacik /* 115726ce2095SJosef Bacik * Mark block group @cache read-only, so later write won't happen to block 115826ce2095SJosef Bacik * group @cache. 115926ce2095SJosef Bacik * 116026ce2095SJosef Bacik * If @force is not set, this function will only mark the block group readonly 116126ce2095SJosef Bacik * if we have enough free space (1M) in other metadata/system block groups. 116226ce2095SJosef Bacik * If @force is not set, this function will mark the block group readonly 116326ce2095SJosef Bacik * without checking free space. 116426ce2095SJosef Bacik * 116526ce2095SJosef Bacik * NOTE: This function doesn't care if other block groups can contain all the 116626ce2095SJosef Bacik * data in this block group. That check should be done by relocation routine, 116726ce2095SJosef Bacik * not this function. 116826ce2095SJosef Bacik */ 116932da5386SDavid Sterba static int inc_block_group_ro(struct btrfs_block_group *cache, int force) 117026ce2095SJosef Bacik { 117126ce2095SJosef Bacik struct btrfs_space_info *sinfo = cache->space_info; 117226ce2095SJosef Bacik u64 num_bytes; 117326ce2095SJosef Bacik int ret = -ENOSPC; 117426ce2095SJosef Bacik 117526ce2095SJosef Bacik spin_lock(&sinfo->lock); 117626ce2095SJosef Bacik spin_lock(&cache->lock); 117726ce2095SJosef Bacik 1178195a49eaSFilipe Manana if (cache->swap_extents) { 1179195a49eaSFilipe Manana ret = -ETXTBSY; 1180195a49eaSFilipe Manana goto out; 1181195a49eaSFilipe Manana } 1182195a49eaSFilipe Manana 118326ce2095SJosef Bacik if (cache->ro) { 118426ce2095SJosef Bacik cache->ro++; 118526ce2095SJosef Bacik ret = 0; 118626ce2095SJosef Bacik goto out; 118726ce2095SJosef Bacik } 118826ce2095SJosef Bacik 1189b3470b5dSDavid Sterba num_bytes = cache->length - cache->reserved - cache->pinned - 1190169e0da9SNaohiro Aota cache->bytes_super - cache->zone_unusable - cache->used; 119126ce2095SJosef Bacik 119226ce2095SJosef Bacik /* 1193a30a3d20SJosef Bacik * Data never overcommits, even in mixed mode, so do just the straight 1194a30a3d20SJosef Bacik * check of left over space in how much we have allocated. 1195a30a3d20SJosef Bacik */ 1196a30a3d20SJosef Bacik if (force) { 1197a30a3d20SJosef Bacik ret = 0; 1198a30a3d20SJosef Bacik } else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) { 1199a30a3d20SJosef Bacik u64 sinfo_used = btrfs_space_info_used(sinfo, true); 1200a30a3d20SJosef Bacik 1201a30a3d20SJosef Bacik /* 120226ce2095SJosef Bacik * Here we make sure if we mark this bg RO, we still have enough 1203f8935566SJosef Bacik * free space as buffer. 120426ce2095SJosef Bacik */ 1205a30a3d20SJosef Bacik if (sinfo_used + num_bytes <= sinfo->total_bytes) 1206a30a3d20SJosef Bacik ret = 0; 1207a30a3d20SJosef Bacik } else { 1208a30a3d20SJosef Bacik /* 1209a30a3d20SJosef Bacik * We overcommit metadata, so we need to do the 1210a30a3d20SJosef Bacik * btrfs_can_overcommit check here, and we need to pass in 1211a30a3d20SJosef Bacik * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of 1212a30a3d20SJosef Bacik * leeway to allow us to mark this block group as read only. 1213a30a3d20SJosef Bacik */ 1214a30a3d20SJosef Bacik if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes, 1215a30a3d20SJosef Bacik BTRFS_RESERVE_NO_FLUSH)) 1216a30a3d20SJosef Bacik ret = 0; 1217a30a3d20SJosef Bacik } 1218a30a3d20SJosef Bacik 1219a30a3d20SJosef Bacik if (!ret) { 122026ce2095SJosef Bacik sinfo->bytes_readonly += num_bytes; 1221169e0da9SNaohiro Aota if (btrfs_is_zoned(cache->fs_info)) { 1222169e0da9SNaohiro Aota /* Migrate zone_unusable bytes to readonly */ 1223169e0da9SNaohiro Aota sinfo->bytes_readonly += cache->zone_unusable; 1224169e0da9SNaohiro Aota sinfo->bytes_zone_unusable -= cache->zone_unusable; 1225169e0da9SNaohiro Aota cache->zone_unusable = 0; 1226169e0da9SNaohiro Aota } 122726ce2095SJosef Bacik cache->ro++; 122826ce2095SJosef Bacik list_add_tail(&cache->ro_list, &sinfo->ro_bgs); 122926ce2095SJosef Bacik } 123026ce2095SJosef Bacik out: 123126ce2095SJosef Bacik spin_unlock(&cache->lock); 123226ce2095SJosef Bacik spin_unlock(&sinfo->lock); 123326ce2095SJosef Bacik if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) { 123426ce2095SJosef Bacik btrfs_info(cache->fs_info, 1235b3470b5dSDavid Sterba "unable to make block group %llu ro", cache->start); 123626ce2095SJosef Bacik btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0); 123726ce2095SJosef Bacik } 123826ce2095SJosef Bacik return ret; 123926ce2095SJosef Bacik } 124026ce2095SJosef Bacik 1241fe119a6eSNikolay Borisov static bool clean_pinned_extents(struct btrfs_trans_handle *trans, 1242fe119a6eSNikolay Borisov struct btrfs_block_group *bg) 124345bb5d6aSNikolay Borisov { 124445bb5d6aSNikolay Borisov struct btrfs_fs_info *fs_info = bg->fs_info; 1245fe119a6eSNikolay Borisov struct btrfs_transaction *prev_trans = NULL; 124645bb5d6aSNikolay Borisov const u64 start = bg->start; 124745bb5d6aSNikolay Borisov const u64 end = start + bg->length - 1; 124845bb5d6aSNikolay Borisov int ret; 124945bb5d6aSNikolay Borisov 1250fe119a6eSNikolay Borisov spin_lock(&fs_info->trans_lock); 1251fe119a6eSNikolay Borisov if (trans->transaction->list.prev != &fs_info->trans_list) { 1252fe119a6eSNikolay Borisov prev_trans = list_last_entry(&trans->transaction->list, 1253fe119a6eSNikolay Borisov struct btrfs_transaction, list); 1254fe119a6eSNikolay Borisov refcount_inc(&prev_trans->use_count); 1255fe119a6eSNikolay Borisov } 1256fe119a6eSNikolay Borisov spin_unlock(&fs_info->trans_lock); 1257fe119a6eSNikolay Borisov 125845bb5d6aSNikolay Borisov /* 125945bb5d6aSNikolay Borisov * Hold the unused_bg_unpin_mutex lock to avoid racing with 126045bb5d6aSNikolay Borisov * btrfs_finish_extent_commit(). If we are at transaction N, another 126145bb5d6aSNikolay Borisov * task might be running finish_extent_commit() for the previous 126245bb5d6aSNikolay Borisov * transaction N - 1, and have seen a range belonging to the block 1263fe119a6eSNikolay Borisov * group in pinned_extents before we were able to clear the whole block 1264fe119a6eSNikolay Borisov * group range from pinned_extents. This means that task can lookup for 1265fe119a6eSNikolay Borisov * the block group after we unpinned it from pinned_extents and removed 1266fe119a6eSNikolay Borisov * it, leading to a BUG_ON() at unpin_extent_range(). 126745bb5d6aSNikolay Borisov */ 126845bb5d6aSNikolay Borisov mutex_lock(&fs_info->unused_bg_unpin_mutex); 1269fe119a6eSNikolay Borisov if (prev_trans) { 1270fe119a6eSNikolay Borisov ret = clear_extent_bits(&prev_trans->pinned_extents, start, end, 127145bb5d6aSNikolay Borisov EXTENT_DIRTY); 127245bb5d6aSNikolay Borisov if (ret) 1273534cf531SFilipe Manana goto out; 1274fe119a6eSNikolay Borisov } 127545bb5d6aSNikolay Borisov 1276fe119a6eSNikolay Borisov ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end, 127745bb5d6aSNikolay Borisov EXTENT_DIRTY); 1278534cf531SFilipe Manana out: 127945bb5d6aSNikolay Borisov mutex_unlock(&fs_info->unused_bg_unpin_mutex); 12805150bf19SFilipe Manana if (prev_trans) 12815150bf19SFilipe Manana btrfs_put_transaction(prev_trans); 128245bb5d6aSNikolay Borisov 1283534cf531SFilipe Manana return ret == 0; 128445bb5d6aSNikolay Borisov } 128545bb5d6aSNikolay Borisov 128626ce2095SJosef Bacik /* 1287e3e0520bSJosef Bacik * Process the unused_bgs list and remove any that don't have any allocated 1288e3e0520bSJosef Bacik * space inside of them. 1289e3e0520bSJosef Bacik */ 1290e3e0520bSJosef Bacik void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) 1291e3e0520bSJosef Bacik { 129232da5386SDavid Sterba struct btrfs_block_group *block_group; 1293e3e0520bSJosef Bacik struct btrfs_space_info *space_info; 1294e3e0520bSJosef Bacik struct btrfs_trans_handle *trans; 12956e80d4f8SDennis Zhou const bool async_trim_enabled = btrfs_test_opt(fs_info, DISCARD_ASYNC); 1296e3e0520bSJosef Bacik int ret = 0; 1297e3e0520bSJosef Bacik 1298e3e0520bSJosef Bacik if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 1299e3e0520bSJosef Bacik return; 1300e3e0520bSJosef Bacik 1301ddfd08cbSJosef Bacik /* 1302ddfd08cbSJosef Bacik * Long running balances can keep us blocked here for eternity, so 1303ddfd08cbSJosef Bacik * simply skip deletion if we're unable to get the mutex. 1304ddfd08cbSJosef Bacik */ 1305f3372065SJohannes Thumshirn if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) 1306ddfd08cbSJosef Bacik return; 1307ddfd08cbSJosef Bacik 1308e3e0520bSJosef Bacik spin_lock(&fs_info->unused_bgs_lock); 1309e3e0520bSJosef Bacik while (!list_empty(&fs_info->unused_bgs)) { 1310e3e0520bSJosef Bacik int trimming; 1311e3e0520bSJosef Bacik 1312e3e0520bSJosef Bacik block_group = list_first_entry(&fs_info->unused_bgs, 131332da5386SDavid Sterba struct btrfs_block_group, 1314e3e0520bSJosef Bacik bg_list); 1315e3e0520bSJosef Bacik list_del_init(&block_group->bg_list); 1316e3e0520bSJosef Bacik 1317e3e0520bSJosef Bacik space_info = block_group->space_info; 1318e3e0520bSJosef Bacik 1319e3e0520bSJosef Bacik if (ret || btrfs_mixed_space_info(space_info)) { 1320e3e0520bSJosef Bacik btrfs_put_block_group(block_group); 1321e3e0520bSJosef Bacik continue; 1322e3e0520bSJosef Bacik } 1323e3e0520bSJosef Bacik spin_unlock(&fs_info->unused_bgs_lock); 1324e3e0520bSJosef Bacik 1325b0643e59SDennis Zhou btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); 1326b0643e59SDennis Zhou 1327e3e0520bSJosef Bacik /* Don't want to race with allocators so take the groups_sem */ 1328e3e0520bSJosef Bacik down_write(&space_info->groups_sem); 13296e80d4f8SDennis Zhou 13306e80d4f8SDennis Zhou /* 13316e80d4f8SDennis Zhou * Async discard moves the final block group discard to be prior 13326e80d4f8SDennis Zhou * to the unused_bgs code path. Therefore, if it's not fully 13336e80d4f8SDennis Zhou * trimmed, punt it back to the async discard lists. 13346e80d4f8SDennis Zhou */ 13356e80d4f8SDennis Zhou if (btrfs_test_opt(fs_info, DISCARD_ASYNC) && 13366e80d4f8SDennis Zhou !btrfs_is_free_space_trimmed(block_group)) { 13376e80d4f8SDennis Zhou trace_btrfs_skip_unused_block_group(block_group); 13386e80d4f8SDennis Zhou up_write(&space_info->groups_sem); 13396e80d4f8SDennis Zhou /* Requeue if we failed because of async discard */ 13406e80d4f8SDennis Zhou btrfs_discard_queue_work(&fs_info->discard_ctl, 13416e80d4f8SDennis Zhou block_group); 13426e80d4f8SDennis Zhou goto next; 13436e80d4f8SDennis Zhou } 13446e80d4f8SDennis Zhou 1345e3e0520bSJosef Bacik spin_lock(&block_group->lock); 1346e3e0520bSJosef Bacik if (block_group->reserved || block_group->pinned || 1347bf38be65SDavid Sterba block_group->used || block_group->ro || 1348e3e0520bSJosef Bacik list_is_singular(&block_group->list)) { 1349e3e0520bSJosef Bacik /* 1350e3e0520bSJosef Bacik * We want to bail if we made new allocations or have 1351e3e0520bSJosef Bacik * outstanding allocations in this block group. We do 1352e3e0520bSJosef Bacik * the ro check in case balance is currently acting on 1353e3e0520bSJosef Bacik * this block group. 1354e3e0520bSJosef Bacik */ 1355e3e0520bSJosef Bacik trace_btrfs_skip_unused_block_group(block_group); 1356e3e0520bSJosef Bacik spin_unlock(&block_group->lock); 1357e3e0520bSJosef Bacik up_write(&space_info->groups_sem); 1358e3e0520bSJosef Bacik goto next; 1359e3e0520bSJosef Bacik } 1360e3e0520bSJosef Bacik spin_unlock(&block_group->lock); 1361e3e0520bSJosef Bacik 1362e3e0520bSJosef Bacik /* We don't want to force the issue, only flip if it's ok. */ 1363e11c0406SJosef Bacik ret = inc_block_group_ro(block_group, 0); 1364e3e0520bSJosef Bacik up_write(&space_info->groups_sem); 1365e3e0520bSJosef Bacik if (ret < 0) { 1366e3e0520bSJosef Bacik ret = 0; 1367e3e0520bSJosef Bacik goto next; 1368e3e0520bSJosef Bacik } 1369e3e0520bSJosef Bacik 1370e3e0520bSJosef Bacik /* 1371e3e0520bSJosef Bacik * Want to do this before we do anything else so we can recover 1372e3e0520bSJosef Bacik * properly if we fail to join the transaction. 1373e3e0520bSJosef Bacik */ 1374e3e0520bSJosef Bacik trans = btrfs_start_trans_remove_block_group(fs_info, 1375b3470b5dSDavid Sterba block_group->start); 1376e3e0520bSJosef Bacik if (IS_ERR(trans)) { 1377e3e0520bSJosef Bacik btrfs_dec_block_group_ro(block_group); 1378e3e0520bSJosef Bacik ret = PTR_ERR(trans); 1379e3e0520bSJosef Bacik goto next; 1380e3e0520bSJosef Bacik } 1381e3e0520bSJosef Bacik 1382e3e0520bSJosef Bacik /* 1383e3e0520bSJosef Bacik * We could have pending pinned extents for this block group, 1384e3e0520bSJosef Bacik * just delete them, we don't care about them anymore. 1385e3e0520bSJosef Bacik */ 1386534cf531SFilipe Manana if (!clean_pinned_extents(trans, block_group)) { 1387534cf531SFilipe Manana btrfs_dec_block_group_ro(block_group); 1388e3e0520bSJosef Bacik goto end_trans; 1389534cf531SFilipe Manana } 1390e3e0520bSJosef Bacik 1391b0643e59SDennis Zhou /* 1392b0643e59SDennis Zhou * At this point, the block_group is read only and should fail 1393b0643e59SDennis Zhou * new allocations. However, btrfs_finish_extent_commit() can 1394b0643e59SDennis Zhou * cause this block_group to be placed back on the discard 1395b0643e59SDennis Zhou * lists because now the block_group isn't fully discarded. 1396b0643e59SDennis Zhou * Bail here and try again later after discarding everything. 1397b0643e59SDennis Zhou */ 1398b0643e59SDennis Zhou spin_lock(&fs_info->discard_ctl.lock); 1399b0643e59SDennis Zhou if (!list_empty(&block_group->discard_list)) { 1400b0643e59SDennis Zhou spin_unlock(&fs_info->discard_ctl.lock); 1401b0643e59SDennis Zhou btrfs_dec_block_group_ro(block_group); 1402b0643e59SDennis Zhou btrfs_discard_queue_work(&fs_info->discard_ctl, 1403b0643e59SDennis Zhou block_group); 1404b0643e59SDennis Zhou goto end_trans; 1405b0643e59SDennis Zhou } 1406b0643e59SDennis Zhou spin_unlock(&fs_info->discard_ctl.lock); 1407b0643e59SDennis Zhou 1408e3e0520bSJosef Bacik /* Reset pinned so btrfs_put_block_group doesn't complain */ 1409e3e0520bSJosef Bacik spin_lock(&space_info->lock); 1410e3e0520bSJosef Bacik spin_lock(&block_group->lock); 1411e3e0520bSJosef Bacik 1412e3e0520bSJosef Bacik btrfs_space_info_update_bytes_pinned(fs_info, space_info, 1413e3e0520bSJosef Bacik -block_group->pinned); 1414e3e0520bSJosef Bacik space_info->bytes_readonly += block_group->pinned; 1415e3e0520bSJosef Bacik block_group->pinned = 0; 1416e3e0520bSJosef Bacik 1417e3e0520bSJosef Bacik spin_unlock(&block_group->lock); 1418e3e0520bSJosef Bacik spin_unlock(&space_info->lock); 1419e3e0520bSJosef Bacik 14206e80d4f8SDennis Zhou /* 14216e80d4f8SDennis Zhou * The normal path here is an unused block group is passed here, 14226e80d4f8SDennis Zhou * then trimming is handled in the transaction commit path. 14236e80d4f8SDennis Zhou * Async discard interposes before this to do the trimming 14246e80d4f8SDennis Zhou * before coming down the unused block group path as trimming 14256e80d4f8SDennis Zhou * will no longer be done later in the transaction commit path. 14266e80d4f8SDennis Zhou */ 14276e80d4f8SDennis Zhou if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC)) 14286e80d4f8SDennis Zhou goto flip_async; 14296e80d4f8SDennis Zhou 1430dcba6e48SNaohiro Aota /* 1431dcba6e48SNaohiro Aota * DISCARD can flip during remount. On zoned filesystems, we 1432dcba6e48SNaohiro Aota * need to reset sequential-required zones. 1433dcba6e48SNaohiro Aota */ 1434dcba6e48SNaohiro Aota trimming = btrfs_test_opt(fs_info, DISCARD_SYNC) || 1435dcba6e48SNaohiro Aota btrfs_is_zoned(fs_info); 1436e3e0520bSJosef Bacik 1437e3e0520bSJosef Bacik /* Implicit trim during transaction commit. */ 1438e3e0520bSJosef Bacik if (trimming) 14396b7304afSFilipe Manana btrfs_freeze_block_group(block_group); 1440e3e0520bSJosef Bacik 1441e3e0520bSJosef Bacik /* 1442e3e0520bSJosef Bacik * Btrfs_remove_chunk will abort the transaction if things go 1443e3e0520bSJosef Bacik * horribly wrong. 1444e3e0520bSJosef Bacik */ 1445b3470b5dSDavid Sterba ret = btrfs_remove_chunk(trans, block_group->start); 1446e3e0520bSJosef Bacik 1447e3e0520bSJosef Bacik if (ret) { 1448e3e0520bSJosef Bacik if (trimming) 14496b7304afSFilipe Manana btrfs_unfreeze_block_group(block_group); 1450e3e0520bSJosef Bacik goto end_trans; 1451e3e0520bSJosef Bacik } 1452e3e0520bSJosef Bacik 1453e3e0520bSJosef Bacik /* 1454e3e0520bSJosef Bacik * If we're not mounted with -odiscard, we can just forget 1455e3e0520bSJosef Bacik * about this block group. Otherwise we'll need to wait 1456e3e0520bSJosef Bacik * until transaction commit to do the actual discard. 1457e3e0520bSJosef Bacik */ 1458e3e0520bSJosef Bacik if (trimming) { 1459e3e0520bSJosef Bacik spin_lock(&fs_info->unused_bgs_lock); 1460e3e0520bSJosef Bacik /* 1461e3e0520bSJosef Bacik * A concurrent scrub might have added us to the list 1462e3e0520bSJosef Bacik * fs_info->unused_bgs, so use a list_move operation 1463e3e0520bSJosef Bacik * to add the block group to the deleted_bgs list. 1464e3e0520bSJosef Bacik */ 1465e3e0520bSJosef Bacik list_move(&block_group->bg_list, 1466e3e0520bSJosef Bacik &trans->transaction->deleted_bgs); 1467e3e0520bSJosef Bacik spin_unlock(&fs_info->unused_bgs_lock); 1468e3e0520bSJosef Bacik btrfs_get_block_group(block_group); 1469e3e0520bSJosef Bacik } 1470e3e0520bSJosef Bacik end_trans: 1471e3e0520bSJosef Bacik btrfs_end_transaction(trans); 1472e3e0520bSJosef Bacik next: 1473e3e0520bSJosef Bacik btrfs_put_block_group(block_group); 1474e3e0520bSJosef Bacik spin_lock(&fs_info->unused_bgs_lock); 1475e3e0520bSJosef Bacik } 1476e3e0520bSJosef Bacik spin_unlock(&fs_info->unused_bgs_lock); 1477f3372065SJohannes Thumshirn mutex_unlock(&fs_info->reclaim_bgs_lock); 14786e80d4f8SDennis Zhou return; 14796e80d4f8SDennis Zhou 14806e80d4f8SDennis Zhou flip_async: 14816e80d4f8SDennis Zhou btrfs_end_transaction(trans); 1482f3372065SJohannes Thumshirn mutex_unlock(&fs_info->reclaim_bgs_lock); 14836e80d4f8SDennis Zhou btrfs_put_block_group(block_group); 14846e80d4f8SDennis Zhou btrfs_discard_punt_unused_bgs_list(fs_info); 1485e3e0520bSJosef Bacik } 1486e3e0520bSJosef Bacik 148732da5386SDavid Sterba void btrfs_mark_bg_unused(struct btrfs_block_group *bg) 1488e3e0520bSJosef Bacik { 1489e3e0520bSJosef Bacik struct btrfs_fs_info *fs_info = bg->fs_info; 1490e3e0520bSJosef Bacik 1491e3e0520bSJosef Bacik spin_lock(&fs_info->unused_bgs_lock); 1492e3e0520bSJosef Bacik if (list_empty(&bg->bg_list)) { 1493e3e0520bSJosef Bacik btrfs_get_block_group(bg); 1494e3e0520bSJosef Bacik trace_btrfs_add_unused_block_group(bg); 1495e3e0520bSJosef Bacik list_add_tail(&bg->bg_list, &fs_info->unused_bgs); 1496e3e0520bSJosef Bacik } 1497e3e0520bSJosef Bacik spin_unlock(&fs_info->unused_bgs_lock); 1498e3e0520bSJosef Bacik } 14994358d963SJosef Bacik 15002ca0ec77SJohannes Thumshirn /* 15012ca0ec77SJohannes Thumshirn * We want block groups with a low number of used bytes to be in the beginning 15022ca0ec77SJohannes Thumshirn * of the list, so they will get reclaimed first. 15032ca0ec77SJohannes Thumshirn */ 15042ca0ec77SJohannes Thumshirn static int reclaim_bgs_cmp(void *unused, const struct list_head *a, 15052ca0ec77SJohannes Thumshirn const struct list_head *b) 15062ca0ec77SJohannes Thumshirn { 15072ca0ec77SJohannes Thumshirn const struct btrfs_block_group *bg1, *bg2; 15082ca0ec77SJohannes Thumshirn 15092ca0ec77SJohannes Thumshirn bg1 = list_entry(a, struct btrfs_block_group, bg_list); 15102ca0ec77SJohannes Thumshirn bg2 = list_entry(b, struct btrfs_block_group, bg_list); 15112ca0ec77SJohannes Thumshirn 15122ca0ec77SJohannes Thumshirn return bg1->used > bg2->used; 15132ca0ec77SJohannes Thumshirn } 15142ca0ec77SJohannes Thumshirn 151518bb8bbfSJohannes Thumshirn void btrfs_reclaim_bgs_work(struct work_struct *work) 151618bb8bbfSJohannes Thumshirn { 151718bb8bbfSJohannes Thumshirn struct btrfs_fs_info *fs_info = 151818bb8bbfSJohannes Thumshirn container_of(work, struct btrfs_fs_info, reclaim_bgs_work); 151918bb8bbfSJohannes Thumshirn struct btrfs_block_group *bg; 152018bb8bbfSJohannes Thumshirn struct btrfs_space_info *space_info; 152118bb8bbfSJohannes Thumshirn 152218bb8bbfSJohannes Thumshirn if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 152318bb8bbfSJohannes Thumshirn return; 152418bb8bbfSJohannes Thumshirn 152518bb8bbfSJohannes Thumshirn if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) 152618bb8bbfSJohannes Thumshirn return; 152718bb8bbfSJohannes Thumshirn 15289cc0b837SJohannes Thumshirn /* 15299cc0b837SJohannes Thumshirn * Long running balances can keep us blocked here for eternity, so 15309cc0b837SJohannes Thumshirn * simply skip reclaim if we're unable to get the mutex. 15319cc0b837SJohannes Thumshirn */ 15329cc0b837SJohannes Thumshirn if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) { 15339cc0b837SJohannes Thumshirn btrfs_exclop_finish(fs_info); 15349cc0b837SJohannes Thumshirn return; 15359cc0b837SJohannes Thumshirn } 15369cc0b837SJohannes Thumshirn 153718bb8bbfSJohannes Thumshirn spin_lock(&fs_info->unused_bgs_lock); 15382ca0ec77SJohannes Thumshirn /* 15392ca0ec77SJohannes Thumshirn * Sort happens under lock because we can't simply splice it and sort. 15402ca0ec77SJohannes Thumshirn * The block groups might still be in use and reachable via bg_list, 15412ca0ec77SJohannes Thumshirn * and their presence in the reclaim_bgs list must be preserved. 15422ca0ec77SJohannes Thumshirn */ 15432ca0ec77SJohannes Thumshirn list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp); 154418bb8bbfSJohannes Thumshirn while (!list_empty(&fs_info->reclaim_bgs)) { 15455f93e776SJohannes Thumshirn u64 zone_unusable; 15461cea5cf0SFilipe Manana int ret = 0; 15471cea5cf0SFilipe Manana 154818bb8bbfSJohannes Thumshirn bg = list_first_entry(&fs_info->reclaim_bgs, 154918bb8bbfSJohannes Thumshirn struct btrfs_block_group, 155018bb8bbfSJohannes Thumshirn bg_list); 155118bb8bbfSJohannes Thumshirn list_del_init(&bg->bg_list); 155218bb8bbfSJohannes Thumshirn 155318bb8bbfSJohannes Thumshirn space_info = bg->space_info; 155418bb8bbfSJohannes Thumshirn spin_unlock(&fs_info->unused_bgs_lock); 155518bb8bbfSJohannes Thumshirn 155618bb8bbfSJohannes Thumshirn /* Don't race with allocators so take the groups_sem */ 155718bb8bbfSJohannes Thumshirn down_write(&space_info->groups_sem); 155818bb8bbfSJohannes Thumshirn 155918bb8bbfSJohannes Thumshirn spin_lock(&bg->lock); 156018bb8bbfSJohannes Thumshirn if (bg->reserved || bg->pinned || bg->ro) { 156118bb8bbfSJohannes Thumshirn /* 156218bb8bbfSJohannes Thumshirn * We want to bail if we made new allocations or have 156318bb8bbfSJohannes Thumshirn * outstanding allocations in this block group. We do 156418bb8bbfSJohannes Thumshirn * the ro check in case balance is currently acting on 156518bb8bbfSJohannes Thumshirn * this block group. 156618bb8bbfSJohannes Thumshirn */ 156718bb8bbfSJohannes Thumshirn spin_unlock(&bg->lock); 156818bb8bbfSJohannes Thumshirn up_write(&space_info->groups_sem); 156918bb8bbfSJohannes Thumshirn goto next; 157018bb8bbfSJohannes Thumshirn } 157118bb8bbfSJohannes Thumshirn spin_unlock(&bg->lock); 157218bb8bbfSJohannes Thumshirn 157318bb8bbfSJohannes Thumshirn /* Get out fast, in case we're unmounting the filesystem */ 157418bb8bbfSJohannes Thumshirn if (btrfs_fs_closing(fs_info)) { 157518bb8bbfSJohannes Thumshirn up_write(&space_info->groups_sem); 157618bb8bbfSJohannes Thumshirn goto next; 157718bb8bbfSJohannes Thumshirn } 157818bb8bbfSJohannes Thumshirn 15795f93e776SJohannes Thumshirn /* 15805f93e776SJohannes Thumshirn * Cache the zone_unusable value before turning the block group 15815f93e776SJohannes Thumshirn * to read only. As soon as the blog group is read only it's 15825f93e776SJohannes Thumshirn * zone_unusable value gets moved to the block group's read-only 15835f93e776SJohannes Thumshirn * bytes and isn't available for calculations anymore. 15845f93e776SJohannes Thumshirn */ 15855f93e776SJohannes Thumshirn zone_unusable = bg->zone_unusable; 158618bb8bbfSJohannes Thumshirn ret = inc_block_group_ro(bg, 0); 158718bb8bbfSJohannes Thumshirn up_write(&space_info->groups_sem); 158818bb8bbfSJohannes Thumshirn if (ret < 0) 158918bb8bbfSJohannes Thumshirn goto next; 159018bb8bbfSJohannes Thumshirn 15915f93e776SJohannes Thumshirn btrfs_info(fs_info, 15925f93e776SJohannes Thumshirn "reclaiming chunk %llu with %llu%% used %llu%% unusable", 15935f93e776SJohannes Thumshirn bg->start, div_u64(bg->used * 100, bg->length), 15945f93e776SJohannes Thumshirn div64_u64(zone_unusable * 100, bg->length)); 159518bb8bbfSJohannes Thumshirn trace_btrfs_reclaim_block_group(bg); 159618bb8bbfSJohannes Thumshirn ret = btrfs_relocate_chunk(fs_info, bg->start); 1597d96b3424SFilipe Manana if (ret) 159818bb8bbfSJohannes Thumshirn btrfs_err(fs_info, "error relocating chunk %llu", 159918bb8bbfSJohannes Thumshirn bg->start); 160018bb8bbfSJohannes Thumshirn 160118bb8bbfSJohannes Thumshirn next: 16021cea5cf0SFilipe Manana btrfs_put_block_group(bg); 1603d96b3424SFilipe Manana spin_lock(&fs_info->unused_bgs_lock); 160418bb8bbfSJohannes Thumshirn } 160518bb8bbfSJohannes Thumshirn spin_unlock(&fs_info->unused_bgs_lock); 160618bb8bbfSJohannes Thumshirn mutex_unlock(&fs_info->reclaim_bgs_lock); 160718bb8bbfSJohannes Thumshirn btrfs_exclop_finish(fs_info); 160818bb8bbfSJohannes Thumshirn } 160918bb8bbfSJohannes Thumshirn 161018bb8bbfSJohannes Thumshirn void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info) 161118bb8bbfSJohannes Thumshirn { 161218bb8bbfSJohannes Thumshirn spin_lock(&fs_info->unused_bgs_lock); 161318bb8bbfSJohannes Thumshirn if (!list_empty(&fs_info->reclaim_bgs)) 161418bb8bbfSJohannes Thumshirn queue_work(system_unbound_wq, &fs_info->reclaim_bgs_work); 161518bb8bbfSJohannes Thumshirn spin_unlock(&fs_info->unused_bgs_lock); 161618bb8bbfSJohannes Thumshirn } 161718bb8bbfSJohannes Thumshirn 161818bb8bbfSJohannes Thumshirn void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg) 161918bb8bbfSJohannes Thumshirn { 162018bb8bbfSJohannes Thumshirn struct btrfs_fs_info *fs_info = bg->fs_info; 162118bb8bbfSJohannes Thumshirn 162218bb8bbfSJohannes Thumshirn spin_lock(&fs_info->unused_bgs_lock); 162318bb8bbfSJohannes Thumshirn if (list_empty(&bg->bg_list)) { 162418bb8bbfSJohannes Thumshirn btrfs_get_block_group(bg); 162518bb8bbfSJohannes Thumshirn trace_btrfs_add_reclaim_block_group(bg); 162618bb8bbfSJohannes Thumshirn list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs); 162718bb8bbfSJohannes Thumshirn } 162818bb8bbfSJohannes Thumshirn spin_unlock(&fs_info->unused_bgs_lock); 162918bb8bbfSJohannes Thumshirn } 163018bb8bbfSJohannes Thumshirn 1631e3ba67a1SJohannes Thumshirn static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key, 1632e3ba67a1SJohannes Thumshirn struct btrfs_path *path) 1633e3ba67a1SJohannes Thumshirn { 1634e3ba67a1SJohannes Thumshirn struct extent_map_tree *em_tree; 1635e3ba67a1SJohannes Thumshirn struct extent_map *em; 1636e3ba67a1SJohannes Thumshirn struct btrfs_block_group_item bg; 1637e3ba67a1SJohannes Thumshirn struct extent_buffer *leaf; 1638e3ba67a1SJohannes Thumshirn int slot; 1639e3ba67a1SJohannes Thumshirn u64 flags; 1640e3ba67a1SJohannes Thumshirn int ret = 0; 1641e3ba67a1SJohannes Thumshirn 1642e3ba67a1SJohannes Thumshirn slot = path->slots[0]; 1643e3ba67a1SJohannes Thumshirn leaf = path->nodes[0]; 1644e3ba67a1SJohannes Thumshirn 1645e3ba67a1SJohannes Thumshirn em_tree = &fs_info->mapping_tree; 1646e3ba67a1SJohannes Thumshirn read_lock(&em_tree->lock); 1647e3ba67a1SJohannes Thumshirn em = lookup_extent_mapping(em_tree, key->objectid, key->offset); 1648e3ba67a1SJohannes Thumshirn read_unlock(&em_tree->lock); 1649e3ba67a1SJohannes Thumshirn if (!em) { 1650e3ba67a1SJohannes Thumshirn btrfs_err(fs_info, 1651e3ba67a1SJohannes Thumshirn "logical %llu len %llu found bg but no related chunk", 1652e3ba67a1SJohannes Thumshirn key->objectid, key->offset); 1653e3ba67a1SJohannes Thumshirn return -ENOENT; 1654e3ba67a1SJohannes Thumshirn } 1655e3ba67a1SJohannes Thumshirn 1656e3ba67a1SJohannes Thumshirn if (em->start != key->objectid || em->len != key->offset) { 1657e3ba67a1SJohannes Thumshirn btrfs_err(fs_info, 1658e3ba67a1SJohannes Thumshirn "block group %llu len %llu mismatch with chunk %llu len %llu", 1659e3ba67a1SJohannes Thumshirn key->objectid, key->offset, em->start, em->len); 1660e3ba67a1SJohannes Thumshirn ret = -EUCLEAN; 1661e3ba67a1SJohannes Thumshirn goto out_free_em; 1662e3ba67a1SJohannes Thumshirn } 1663e3ba67a1SJohannes Thumshirn 1664e3ba67a1SJohannes Thumshirn read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot), 1665e3ba67a1SJohannes Thumshirn sizeof(bg)); 1666e3ba67a1SJohannes Thumshirn flags = btrfs_stack_block_group_flags(&bg) & 1667e3ba67a1SJohannes Thumshirn BTRFS_BLOCK_GROUP_TYPE_MASK; 1668e3ba67a1SJohannes Thumshirn 1669e3ba67a1SJohannes Thumshirn if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { 1670e3ba67a1SJohannes Thumshirn btrfs_err(fs_info, 1671e3ba67a1SJohannes Thumshirn "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx", 1672e3ba67a1SJohannes Thumshirn key->objectid, key->offset, flags, 1673e3ba67a1SJohannes Thumshirn (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type)); 1674e3ba67a1SJohannes Thumshirn ret = -EUCLEAN; 1675e3ba67a1SJohannes Thumshirn } 1676e3ba67a1SJohannes Thumshirn 1677e3ba67a1SJohannes Thumshirn out_free_em: 1678e3ba67a1SJohannes Thumshirn free_extent_map(em); 1679e3ba67a1SJohannes Thumshirn return ret; 1680e3ba67a1SJohannes Thumshirn } 1681e3ba67a1SJohannes Thumshirn 16824358d963SJosef Bacik static int find_first_block_group(struct btrfs_fs_info *fs_info, 16834358d963SJosef Bacik struct btrfs_path *path, 16844358d963SJosef Bacik struct btrfs_key *key) 16854358d963SJosef Bacik { 1686dfe8aec4SJosef Bacik struct btrfs_root *root = btrfs_block_group_root(fs_info); 1687e3ba67a1SJohannes Thumshirn int ret; 16884358d963SJosef Bacik struct btrfs_key found_key; 16894358d963SJosef Bacik struct extent_buffer *leaf; 16904358d963SJosef Bacik int slot; 16914358d963SJosef Bacik 16924358d963SJosef Bacik ret = btrfs_search_slot(NULL, root, key, path, 0, 0); 16934358d963SJosef Bacik if (ret < 0) 1694e3ba67a1SJohannes Thumshirn return ret; 16954358d963SJosef Bacik 16964358d963SJosef Bacik while (1) { 16974358d963SJosef Bacik slot = path->slots[0]; 16984358d963SJosef Bacik leaf = path->nodes[0]; 16994358d963SJosef Bacik if (slot >= btrfs_header_nritems(leaf)) { 17004358d963SJosef Bacik ret = btrfs_next_leaf(root, path); 17014358d963SJosef Bacik if (ret == 0) 17024358d963SJosef Bacik continue; 17034358d963SJosef Bacik if (ret < 0) 17044358d963SJosef Bacik goto out; 17054358d963SJosef Bacik break; 17064358d963SJosef Bacik } 17074358d963SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 17084358d963SJosef Bacik 17094358d963SJosef Bacik if (found_key.objectid >= key->objectid && 17104358d963SJosef Bacik found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { 1711e3ba67a1SJohannes Thumshirn ret = read_bg_from_eb(fs_info, &found_key, path); 1712e3ba67a1SJohannes Thumshirn break; 1713e3ba67a1SJohannes Thumshirn } 17144358d963SJosef Bacik 17154358d963SJosef Bacik path->slots[0]++; 17164358d963SJosef Bacik } 17174358d963SJosef Bacik out: 17184358d963SJosef Bacik return ret; 17194358d963SJosef Bacik } 17204358d963SJosef Bacik 17214358d963SJosef Bacik static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) 17224358d963SJosef Bacik { 17234358d963SJosef Bacik u64 extra_flags = chunk_to_extended(flags) & 17244358d963SJosef Bacik BTRFS_EXTENDED_PROFILE_MASK; 17254358d963SJosef Bacik 17264358d963SJosef Bacik write_seqlock(&fs_info->profiles_lock); 17274358d963SJosef Bacik if (flags & BTRFS_BLOCK_GROUP_DATA) 17284358d963SJosef Bacik fs_info->avail_data_alloc_bits |= extra_flags; 17294358d963SJosef Bacik if (flags & BTRFS_BLOCK_GROUP_METADATA) 17304358d963SJosef Bacik fs_info->avail_metadata_alloc_bits |= extra_flags; 17314358d963SJosef Bacik if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 17324358d963SJosef Bacik fs_info->avail_system_alloc_bits |= extra_flags; 17334358d963SJosef Bacik write_sequnlock(&fs_info->profiles_lock); 17344358d963SJosef Bacik } 17354358d963SJosef Bacik 173696a14336SNikolay Borisov /** 17379ee9b979SNikolay Borisov * Map a physical disk address to a list of logical addresses 17389ee9b979SNikolay Borisov * 17399ee9b979SNikolay Borisov * @fs_info: the filesystem 174096a14336SNikolay Borisov * @chunk_start: logical address of block group 1741138082f3SNaohiro Aota * @bdev: physical device to resolve, can be NULL to indicate any device 174296a14336SNikolay Borisov * @physical: physical address to map to logical addresses 174396a14336SNikolay Borisov * @logical: return array of logical addresses which map to @physical 174496a14336SNikolay Borisov * @naddrs: length of @logical 174596a14336SNikolay Borisov * @stripe_len: size of IO stripe for the given block group 174696a14336SNikolay Borisov * 174796a14336SNikolay Borisov * Maps a particular @physical disk address to a list of @logical addresses. 174896a14336SNikolay Borisov * Used primarily to exclude those portions of a block group that contain super 174996a14336SNikolay Borisov * block copies. 175096a14336SNikolay Borisov */ 175196a14336SNikolay Borisov int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, 1752138082f3SNaohiro Aota struct block_device *bdev, u64 physical, u64 **logical, 1753138082f3SNaohiro Aota int *naddrs, int *stripe_len) 175496a14336SNikolay Borisov { 175596a14336SNikolay Borisov struct extent_map *em; 175696a14336SNikolay Borisov struct map_lookup *map; 175796a14336SNikolay Borisov u64 *buf; 175896a14336SNikolay Borisov u64 bytenr; 17591776ad17SNikolay Borisov u64 data_stripe_length; 17601776ad17SNikolay Borisov u64 io_stripe_size; 17611776ad17SNikolay Borisov int i, nr = 0; 17621776ad17SNikolay Borisov int ret = 0; 176396a14336SNikolay Borisov 176496a14336SNikolay Borisov em = btrfs_get_chunk_map(fs_info, chunk_start, 1); 176596a14336SNikolay Borisov if (IS_ERR(em)) 176696a14336SNikolay Borisov return -EIO; 176796a14336SNikolay Borisov 176896a14336SNikolay Borisov map = em->map_lookup; 17699e22b925SNikolay Borisov data_stripe_length = em->orig_block_len; 17701776ad17SNikolay Borisov io_stripe_size = map->stripe_len; 1771138082f3SNaohiro Aota chunk_start = em->start; 177296a14336SNikolay Borisov 17739e22b925SNikolay Borisov /* For RAID5/6 adjust to a full IO stripe length */ 17749e22b925SNikolay Borisov if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) 17751776ad17SNikolay Borisov io_stripe_size = map->stripe_len * nr_data_stripes(map); 177696a14336SNikolay Borisov 177796a14336SNikolay Borisov buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); 17781776ad17SNikolay Borisov if (!buf) { 17791776ad17SNikolay Borisov ret = -ENOMEM; 17801776ad17SNikolay Borisov goto out; 17811776ad17SNikolay Borisov } 178296a14336SNikolay Borisov 178396a14336SNikolay Borisov for (i = 0; i < map->num_stripes; i++) { 17841776ad17SNikolay Borisov bool already_inserted = false; 17851776ad17SNikolay Borisov u64 stripe_nr; 1786138082f3SNaohiro Aota u64 offset; 17871776ad17SNikolay Borisov int j; 17881776ad17SNikolay Borisov 17891776ad17SNikolay Borisov if (!in_range(physical, map->stripes[i].physical, 17901776ad17SNikolay Borisov data_stripe_length)) 179196a14336SNikolay Borisov continue; 179296a14336SNikolay Borisov 1793138082f3SNaohiro Aota if (bdev && map->stripes[i].dev->bdev != bdev) 1794138082f3SNaohiro Aota continue; 1795138082f3SNaohiro Aota 179696a14336SNikolay Borisov stripe_nr = physical - map->stripes[i].physical; 1797138082f3SNaohiro Aota stripe_nr = div64_u64_rem(stripe_nr, map->stripe_len, &offset); 179896a14336SNikolay Borisov 179996a14336SNikolay Borisov if (map->type & BTRFS_BLOCK_GROUP_RAID10) { 180096a14336SNikolay Borisov stripe_nr = stripe_nr * map->num_stripes + i; 180196a14336SNikolay Borisov stripe_nr = div_u64(stripe_nr, map->sub_stripes); 180296a14336SNikolay Borisov } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { 180396a14336SNikolay Borisov stripe_nr = stripe_nr * map->num_stripes + i; 180496a14336SNikolay Borisov } 180596a14336SNikolay Borisov /* 180696a14336SNikolay Borisov * The remaining case would be for RAID56, multiply by 180796a14336SNikolay Borisov * nr_data_stripes(). Alternatively, just use rmap_len below 180896a14336SNikolay Borisov * instead of map->stripe_len 180996a14336SNikolay Borisov */ 181096a14336SNikolay Borisov 1811138082f3SNaohiro Aota bytenr = chunk_start + stripe_nr * io_stripe_size + offset; 18121776ad17SNikolay Borisov 18131776ad17SNikolay Borisov /* Ensure we don't add duplicate addresses */ 181496a14336SNikolay Borisov for (j = 0; j < nr; j++) { 18151776ad17SNikolay Borisov if (buf[j] == bytenr) { 18161776ad17SNikolay Borisov already_inserted = true; 181796a14336SNikolay Borisov break; 181896a14336SNikolay Borisov } 181996a14336SNikolay Borisov } 18201776ad17SNikolay Borisov 18211776ad17SNikolay Borisov if (!already_inserted) 18221776ad17SNikolay Borisov buf[nr++] = bytenr; 182396a14336SNikolay Borisov } 182496a14336SNikolay Borisov 182596a14336SNikolay Borisov *logical = buf; 182696a14336SNikolay Borisov *naddrs = nr; 18271776ad17SNikolay Borisov *stripe_len = io_stripe_size; 18281776ad17SNikolay Borisov out: 182996a14336SNikolay Borisov free_extent_map(em); 18301776ad17SNikolay Borisov return ret; 183196a14336SNikolay Borisov } 183296a14336SNikolay Borisov 183332da5386SDavid Sterba static int exclude_super_stripes(struct btrfs_block_group *cache) 18344358d963SJosef Bacik { 18354358d963SJosef Bacik struct btrfs_fs_info *fs_info = cache->fs_info; 183612659251SNaohiro Aota const bool zoned = btrfs_is_zoned(fs_info); 18374358d963SJosef Bacik u64 bytenr; 18384358d963SJosef Bacik u64 *logical; 18394358d963SJosef Bacik int stripe_len; 18404358d963SJosef Bacik int i, nr, ret; 18414358d963SJosef Bacik 1842b3470b5dSDavid Sterba if (cache->start < BTRFS_SUPER_INFO_OFFSET) { 1843b3470b5dSDavid Sterba stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start; 18444358d963SJosef Bacik cache->bytes_super += stripe_len; 1845b3470b5dSDavid Sterba ret = btrfs_add_excluded_extent(fs_info, cache->start, 18464358d963SJosef Bacik stripe_len); 18474358d963SJosef Bacik if (ret) 18484358d963SJosef Bacik return ret; 18494358d963SJosef Bacik } 18504358d963SJosef Bacik 18514358d963SJosef Bacik for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { 18524358d963SJosef Bacik bytenr = btrfs_sb_offset(i); 1853138082f3SNaohiro Aota ret = btrfs_rmap_block(fs_info, cache->start, NULL, 18544358d963SJosef Bacik bytenr, &logical, &nr, &stripe_len); 18554358d963SJosef Bacik if (ret) 18564358d963SJosef Bacik return ret; 18574358d963SJosef Bacik 185812659251SNaohiro Aota /* Shouldn't have super stripes in sequential zones */ 185912659251SNaohiro Aota if (zoned && nr) { 186012659251SNaohiro Aota btrfs_err(fs_info, 186112659251SNaohiro Aota "zoned: block group %llu must not contain super block", 186212659251SNaohiro Aota cache->start); 186312659251SNaohiro Aota return -EUCLEAN; 186412659251SNaohiro Aota } 186512659251SNaohiro Aota 18664358d963SJosef Bacik while (nr--) { 186796f9b0f2SNikolay Borisov u64 len = min_t(u64, stripe_len, 186896f9b0f2SNikolay Borisov cache->start + cache->length - logical[nr]); 18694358d963SJosef Bacik 18704358d963SJosef Bacik cache->bytes_super += len; 187196f9b0f2SNikolay Borisov ret = btrfs_add_excluded_extent(fs_info, logical[nr], 187296f9b0f2SNikolay Borisov len); 18734358d963SJosef Bacik if (ret) { 18744358d963SJosef Bacik kfree(logical); 18754358d963SJosef Bacik return ret; 18764358d963SJosef Bacik } 18774358d963SJosef Bacik } 18784358d963SJosef Bacik 18794358d963SJosef Bacik kfree(logical); 18804358d963SJosef Bacik } 18814358d963SJosef Bacik return 0; 18824358d963SJosef Bacik } 18834358d963SJosef Bacik 188432da5386SDavid Sterba static void link_block_group(struct btrfs_block_group *cache) 18854358d963SJosef Bacik { 18864358d963SJosef Bacik struct btrfs_space_info *space_info = cache->space_info; 18874358d963SJosef Bacik int index = btrfs_bg_flags_to_raid_index(cache->flags); 18884358d963SJosef Bacik 18894358d963SJosef Bacik down_write(&space_info->groups_sem); 18904358d963SJosef Bacik list_add_tail(&cache->list, &space_info->block_groups[index]); 18914358d963SJosef Bacik up_write(&space_info->groups_sem); 18924358d963SJosef Bacik } 18934358d963SJosef Bacik 189432da5386SDavid Sterba static struct btrfs_block_group *btrfs_create_block_group_cache( 18959afc6649SQu Wenruo struct btrfs_fs_info *fs_info, u64 start) 18964358d963SJosef Bacik { 189732da5386SDavid Sterba struct btrfs_block_group *cache; 18984358d963SJosef Bacik 18994358d963SJosef Bacik cache = kzalloc(sizeof(*cache), GFP_NOFS); 19004358d963SJosef Bacik if (!cache) 19014358d963SJosef Bacik return NULL; 19024358d963SJosef Bacik 19034358d963SJosef Bacik cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), 19044358d963SJosef Bacik GFP_NOFS); 19054358d963SJosef Bacik if (!cache->free_space_ctl) { 19064358d963SJosef Bacik kfree(cache); 19074358d963SJosef Bacik return NULL; 19084358d963SJosef Bacik } 19094358d963SJosef Bacik 1910b3470b5dSDavid Sterba cache->start = start; 19114358d963SJosef Bacik 19124358d963SJosef Bacik cache->fs_info = fs_info; 19134358d963SJosef Bacik cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start); 19144358d963SJosef Bacik 19156e80d4f8SDennis Zhou cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED; 19166e80d4f8SDennis Zhou 191748aaeebeSJosef Bacik refcount_set(&cache->refs, 1); 19184358d963SJosef Bacik spin_lock_init(&cache->lock); 19194358d963SJosef Bacik init_rwsem(&cache->data_rwsem); 19204358d963SJosef Bacik INIT_LIST_HEAD(&cache->list); 19214358d963SJosef Bacik INIT_LIST_HEAD(&cache->cluster_list); 19224358d963SJosef Bacik INIT_LIST_HEAD(&cache->bg_list); 19234358d963SJosef Bacik INIT_LIST_HEAD(&cache->ro_list); 1924b0643e59SDennis Zhou INIT_LIST_HEAD(&cache->discard_list); 19254358d963SJosef Bacik INIT_LIST_HEAD(&cache->dirty_list); 19264358d963SJosef Bacik INIT_LIST_HEAD(&cache->io_list); 1927afba2bc0SNaohiro Aota INIT_LIST_HEAD(&cache->active_bg_list); 1928cd79909bSJosef Bacik btrfs_init_free_space_ctl(cache, cache->free_space_ctl); 19296b7304afSFilipe Manana atomic_set(&cache->frozen, 0); 19304358d963SJosef Bacik mutex_init(&cache->free_space_lock); 19314358d963SJosef Bacik btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root); 19324358d963SJosef Bacik 19334358d963SJosef Bacik return cache; 19344358d963SJosef Bacik } 19354358d963SJosef Bacik 19364358d963SJosef Bacik /* 19374358d963SJosef Bacik * Iterate all chunks and verify that each of them has the corresponding block 19384358d963SJosef Bacik * group 19394358d963SJosef Bacik */ 19404358d963SJosef Bacik static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info) 19414358d963SJosef Bacik { 19424358d963SJosef Bacik struct extent_map_tree *map_tree = &fs_info->mapping_tree; 19434358d963SJosef Bacik struct extent_map *em; 194432da5386SDavid Sterba struct btrfs_block_group *bg; 19454358d963SJosef Bacik u64 start = 0; 19464358d963SJosef Bacik int ret = 0; 19474358d963SJosef Bacik 19484358d963SJosef Bacik while (1) { 19494358d963SJosef Bacik read_lock(&map_tree->lock); 19504358d963SJosef Bacik /* 19514358d963SJosef Bacik * lookup_extent_mapping will return the first extent map 19524358d963SJosef Bacik * intersecting the range, so setting @len to 1 is enough to 19534358d963SJosef Bacik * get the first chunk. 19544358d963SJosef Bacik */ 19554358d963SJosef Bacik em = lookup_extent_mapping(map_tree, start, 1); 19564358d963SJosef Bacik read_unlock(&map_tree->lock); 19574358d963SJosef Bacik if (!em) 19584358d963SJosef Bacik break; 19594358d963SJosef Bacik 19604358d963SJosef Bacik bg = btrfs_lookup_block_group(fs_info, em->start); 19614358d963SJosef Bacik if (!bg) { 19624358d963SJosef Bacik btrfs_err(fs_info, 19634358d963SJosef Bacik "chunk start=%llu len=%llu doesn't have corresponding block group", 19644358d963SJosef Bacik em->start, em->len); 19654358d963SJosef Bacik ret = -EUCLEAN; 19664358d963SJosef Bacik free_extent_map(em); 19674358d963SJosef Bacik break; 19684358d963SJosef Bacik } 1969b3470b5dSDavid Sterba if (bg->start != em->start || bg->length != em->len || 19704358d963SJosef Bacik (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != 19714358d963SJosef Bacik (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { 19724358d963SJosef Bacik btrfs_err(fs_info, 19734358d963SJosef Bacik "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx", 19744358d963SJosef Bacik em->start, em->len, 19754358d963SJosef Bacik em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK, 1976b3470b5dSDavid Sterba bg->start, bg->length, 19774358d963SJosef Bacik bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK); 19784358d963SJosef Bacik ret = -EUCLEAN; 19794358d963SJosef Bacik free_extent_map(em); 19804358d963SJosef Bacik btrfs_put_block_group(bg); 19814358d963SJosef Bacik break; 19824358d963SJosef Bacik } 19834358d963SJosef Bacik start = em->start + em->len; 19844358d963SJosef Bacik free_extent_map(em); 19854358d963SJosef Bacik btrfs_put_block_group(bg); 19864358d963SJosef Bacik } 19874358d963SJosef Bacik return ret; 19884358d963SJosef Bacik } 19894358d963SJosef Bacik 1990ffb9e0f0SQu Wenruo static int read_one_block_group(struct btrfs_fs_info *info, 19914afd2fe8SJohannes Thumshirn struct btrfs_block_group_item *bgi, 1992d49a2ddbSQu Wenruo const struct btrfs_key *key, 1993ffb9e0f0SQu Wenruo int need_clear) 1994ffb9e0f0SQu Wenruo { 199532da5386SDavid Sterba struct btrfs_block_group *cache; 1996ffb9e0f0SQu Wenruo struct btrfs_space_info *space_info; 1997ffb9e0f0SQu Wenruo const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS); 1998ffb9e0f0SQu Wenruo int ret; 1999ffb9e0f0SQu Wenruo 2000d49a2ddbSQu Wenruo ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY); 2001ffb9e0f0SQu Wenruo 20029afc6649SQu Wenruo cache = btrfs_create_block_group_cache(info, key->objectid); 2003ffb9e0f0SQu Wenruo if (!cache) 2004ffb9e0f0SQu Wenruo return -ENOMEM; 2005ffb9e0f0SQu Wenruo 20064afd2fe8SJohannes Thumshirn cache->length = key->offset; 20074afd2fe8SJohannes Thumshirn cache->used = btrfs_stack_block_group_used(bgi); 20084afd2fe8SJohannes Thumshirn cache->flags = btrfs_stack_block_group_flags(bgi); 2009*f7238e50SJosef Bacik cache->global_root_id = btrfs_stack_block_group_chunk_objectid(bgi); 20109afc6649SQu Wenruo 2011e3e39c72SMarcos Paulo de Souza set_free_space_tree_thresholds(cache); 2012e3e39c72SMarcos Paulo de Souza 2013ffb9e0f0SQu Wenruo if (need_clear) { 2014ffb9e0f0SQu Wenruo /* 2015ffb9e0f0SQu Wenruo * When we mount with old space cache, we need to 2016ffb9e0f0SQu Wenruo * set BTRFS_DC_CLEAR and set dirty flag. 2017ffb9e0f0SQu Wenruo * 2018ffb9e0f0SQu Wenruo * a) Setting 'BTRFS_DC_CLEAR' makes sure that we 2019ffb9e0f0SQu Wenruo * truncate the old free space cache inode and 2020ffb9e0f0SQu Wenruo * setup a new one. 2021ffb9e0f0SQu Wenruo * b) Setting 'dirty flag' makes sure that we flush 2022ffb9e0f0SQu Wenruo * the new space cache info onto disk. 2023ffb9e0f0SQu Wenruo */ 2024ffb9e0f0SQu Wenruo if (btrfs_test_opt(info, SPACE_CACHE)) 2025ffb9e0f0SQu Wenruo cache->disk_cache_state = BTRFS_DC_CLEAR; 2026ffb9e0f0SQu Wenruo } 2027ffb9e0f0SQu Wenruo if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && 2028ffb9e0f0SQu Wenruo (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { 2029ffb9e0f0SQu Wenruo btrfs_err(info, 2030ffb9e0f0SQu Wenruo "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", 2031ffb9e0f0SQu Wenruo cache->start); 2032ffb9e0f0SQu Wenruo ret = -EINVAL; 2033ffb9e0f0SQu Wenruo goto error; 2034ffb9e0f0SQu Wenruo } 2035ffb9e0f0SQu Wenruo 2036a94794d5SNaohiro Aota ret = btrfs_load_block_group_zone_info(cache, false); 203708e11a3dSNaohiro Aota if (ret) { 203808e11a3dSNaohiro Aota btrfs_err(info, "zoned: failed to load zone info of bg %llu", 203908e11a3dSNaohiro Aota cache->start); 204008e11a3dSNaohiro Aota goto error; 204108e11a3dSNaohiro Aota } 204208e11a3dSNaohiro Aota 2043ffb9e0f0SQu Wenruo /* 2044ffb9e0f0SQu Wenruo * We need to exclude the super stripes now so that the space info has 2045ffb9e0f0SQu Wenruo * super bytes accounted for, otherwise we'll think we have more space 2046ffb9e0f0SQu Wenruo * than we actually do. 2047ffb9e0f0SQu Wenruo */ 2048ffb9e0f0SQu Wenruo ret = exclude_super_stripes(cache); 2049ffb9e0f0SQu Wenruo if (ret) { 2050ffb9e0f0SQu Wenruo /* We may have excluded something, so call this just in case. */ 2051ffb9e0f0SQu Wenruo btrfs_free_excluded_extents(cache); 2052ffb9e0f0SQu Wenruo goto error; 2053ffb9e0f0SQu Wenruo } 2054ffb9e0f0SQu Wenruo 2055ffb9e0f0SQu Wenruo /* 2056169e0da9SNaohiro Aota * For zoned filesystem, space after the allocation offset is the only 2057169e0da9SNaohiro Aota * free space for a block group. So, we don't need any caching work. 2058169e0da9SNaohiro Aota * btrfs_calc_zone_unusable() will set the amount of free space and 2059169e0da9SNaohiro Aota * zone_unusable space. 2060169e0da9SNaohiro Aota * 2061169e0da9SNaohiro Aota * For regular filesystem, check for two cases, either we are full, and 2062169e0da9SNaohiro Aota * therefore don't need to bother with the caching work since we won't 2063169e0da9SNaohiro Aota * find any space, or we are empty, and we can just add all the space 2064169e0da9SNaohiro Aota * in and be done with it. This saves us _a_lot_ of time, particularly 2065169e0da9SNaohiro Aota * in the full case. 2066ffb9e0f0SQu Wenruo */ 2067169e0da9SNaohiro Aota if (btrfs_is_zoned(info)) { 2068169e0da9SNaohiro Aota btrfs_calc_zone_unusable(cache); 2069c46c4247SNaohiro Aota /* Should not have any excluded extents. Just in case, though. */ 2070c46c4247SNaohiro Aota btrfs_free_excluded_extents(cache); 2071169e0da9SNaohiro Aota } else if (cache->length == cache->used) { 2072ffb9e0f0SQu Wenruo cache->last_byte_to_unpin = (u64)-1; 2073ffb9e0f0SQu Wenruo cache->cached = BTRFS_CACHE_FINISHED; 2074ffb9e0f0SQu Wenruo btrfs_free_excluded_extents(cache); 2075ffb9e0f0SQu Wenruo } else if (cache->used == 0) { 2076ffb9e0f0SQu Wenruo cache->last_byte_to_unpin = (u64)-1; 2077ffb9e0f0SQu Wenruo cache->cached = BTRFS_CACHE_FINISHED; 20789afc6649SQu Wenruo add_new_free_space(cache, cache->start, 20799afc6649SQu Wenruo cache->start + cache->length); 2080ffb9e0f0SQu Wenruo btrfs_free_excluded_extents(cache); 2081ffb9e0f0SQu Wenruo } 2082ffb9e0f0SQu Wenruo 2083ffb9e0f0SQu Wenruo ret = btrfs_add_block_group_cache(info, cache); 2084ffb9e0f0SQu Wenruo if (ret) { 2085ffb9e0f0SQu Wenruo btrfs_remove_free_space_cache(cache); 2086ffb9e0f0SQu Wenruo goto error; 2087ffb9e0f0SQu Wenruo } 2088ffb9e0f0SQu Wenruo trace_btrfs_add_block_group(info, cache, 0); 20899afc6649SQu Wenruo btrfs_update_space_info(info, cache->flags, cache->length, 2090169e0da9SNaohiro Aota cache->used, cache->bytes_super, 2091169e0da9SNaohiro Aota cache->zone_unusable, &space_info); 2092ffb9e0f0SQu Wenruo 2093ffb9e0f0SQu Wenruo cache->space_info = space_info; 2094ffb9e0f0SQu Wenruo 2095ffb9e0f0SQu Wenruo link_block_group(cache); 2096ffb9e0f0SQu Wenruo 2097ffb9e0f0SQu Wenruo set_avail_alloc_bits(info, cache->flags); 2098a09f23c3SAnand Jain if (btrfs_chunk_writeable(info, cache->start)) { 2099a09f23c3SAnand Jain if (cache->used == 0) { 2100ffb9e0f0SQu Wenruo ASSERT(list_empty(&cache->bg_list)); 21016e80d4f8SDennis Zhou if (btrfs_test_opt(info, DISCARD_ASYNC)) 21026e80d4f8SDennis Zhou btrfs_discard_queue_work(&info->discard_ctl, cache); 21036e80d4f8SDennis Zhou else 2104ffb9e0f0SQu Wenruo btrfs_mark_bg_unused(cache); 2105ffb9e0f0SQu Wenruo } 2106a09f23c3SAnand Jain } else { 2107a09f23c3SAnand Jain inc_block_group_ro(cache, 1); 2108a09f23c3SAnand Jain } 2109a09f23c3SAnand Jain 2110ffb9e0f0SQu Wenruo return 0; 2111ffb9e0f0SQu Wenruo error: 2112ffb9e0f0SQu Wenruo btrfs_put_block_group(cache); 2113ffb9e0f0SQu Wenruo return ret; 2114ffb9e0f0SQu Wenruo } 2115ffb9e0f0SQu Wenruo 211642437a63SJosef Bacik static int fill_dummy_bgs(struct btrfs_fs_info *fs_info) 211742437a63SJosef Bacik { 211842437a63SJosef Bacik struct extent_map_tree *em_tree = &fs_info->mapping_tree; 211942437a63SJosef Bacik struct btrfs_space_info *space_info; 212042437a63SJosef Bacik struct rb_node *node; 212142437a63SJosef Bacik int ret = 0; 212242437a63SJosef Bacik 212342437a63SJosef Bacik for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) { 212442437a63SJosef Bacik struct extent_map *em; 212542437a63SJosef Bacik struct map_lookup *map; 212642437a63SJosef Bacik struct btrfs_block_group *bg; 212742437a63SJosef Bacik 212842437a63SJosef Bacik em = rb_entry(node, struct extent_map, rb_node); 212942437a63SJosef Bacik map = em->map_lookup; 213042437a63SJosef Bacik bg = btrfs_create_block_group_cache(fs_info, em->start); 213142437a63SJosef Bacik if (!bg) { 213242437a63SJosef Bacik ret = -ENOMEM; 213342437a63SJosef Bacik break; 213442437a63SJosef Bacik } 213542437a63SJosef Bacik 213642437a63SJosef Bacik /* Fill dummy cache as FULL */ 213742437a63SJosef Bacik bg->length = em->len; 213842437a63SJosef Bacik bg->flags = map->type; 213942437a63SJosef Bacik bg->last_byte_to_unpin = (u64)-1; 214042437a63SJosef Bacik bg->cached = BTRFS_CACHE_FINISHED; 214142437a63SJosef Bacik bg->used = em->len; 214242437a63SJosef Bacik bg->flags = map->type; 214342437a63SJosef Bacik ret = btrfs_add_block_group_cache(fs_info, bg); 21442b29726cSQu Wenruo /* 21452b29726cSQu Wenruo * We may have some valid block group cache added already, in 21462b29726cSQu Wenruo * that case we skip to the next one. 21472b29726cSQu Wenruo */ 21482b29726cSQu Wenruo if (ret == -EEXIST) { 21492b29726cSQu Wenruo ret = 0; 21502b29726cSQu Wenruo btrfs_put_block_group(bg); 21512b29726cSQu Wenruo continue; 21522b29726cSQu Wenruo } 21532b29726cSQu Wenruo 215442437a63SJosef Bacik if (ret) { 215542437a63SJosef Bacik btrfs_remove_free_space_cache(bg); 215642437a63SJosef Bacik btrfs_put_block_group(bg); 215742437a63SJosef Bacik break; 215842437a63SJosef Bacik } 21592b29726cSQu Wenruo 216042437a63SJosef Bacik btrfs_update_space_info(fs_info, bg->flags, em->len, em->len, 2161169e0da9SNaohiro Aota 0, 0, &space_info); 216242437a63SJosef Bacik bg->space_info = space_info; 216342437a63SJosef Bacik link_block_group(bg); 216442437a63SJosef Bacik 216542437a63SJosef Bacik set_avail_alloc_bits(fs_info, bg->flags); 216642437a63SJosef Bacik } 216742437a63SJosef Bacik if (!ret) 216842437a63SJosef Bacik btrfs_init_global_block_rsv(fs_info); 216942437a63SJosef Bacik return ret; 217042437a63SJosef Bacik } 217142437a63SJosef Bacik 21724358d963SJosef Bacik int btrfs_read_block_groups(struct btrfs_fs_info *info) 21734358d963SJosef Bacik { 2174dfe8aec4SJosef Bacik struct btrfs_root *root = btrfs_block_group_root(info); 21754358d963SJosef Bacik struct btrfs_path *path; 21764358d963SJosef Bacik int ret; 217732da5386SDavid Sterba struct btrfs_block_group *cache; 21784358d963SJosef Bacik struct btrfs_space_info *space_info; 21794358d963SJosef Bacik struct btrfs_key key; 21804358d963SJosef Bacik int need_clear = 0; 21814358d963SJosef Bacik u64 cache_gen; 21824358d963SJosef Bacik 2183dfe8aec4SJosef Bacik if (!root) 218442437a63SJosef Bacik return fill_dummy_bgs(info); 218542437a63SJosef Bacik 21864358d963SJosef Bacik key.objectid = 0; 21874358d963SJosef Bacik key.offset = 0; 21884358d963SJosef Bacik key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 21894358d963SJosef Bacik path = btrfs_alloc_path(); 21904358d963SJosef Bacik if (!path) 21914358d963SJosef Bacik return -ENOMEM; 21924358d963SJosef Bacik 21934358d963SJosef Bacik cache_gen = btrfs_super_cache_generation(info->super_copy); 21944358d963SJosef Bacik if (btrfs_test_opt(info, SPACE_CACHE) && 21954358d963SJosef Bacik btrfs_super_generation(info->super_copy) != cache_gen) 21964358d963SJosef Bacik need_clear = 1; 21974358d963SJosef Bacik if (btrfs_test_opt(info, CLEAR_CACHE)) 21984358d963SJosef Bacik need_clear = 1; 21994358d963SJosef Bacik 22004358d963SJosef Bacik while (1) { 22014afd2fe8SJohannes Thumshirn struct btrfs_block_group_item bgi; 22024afd2fe8SJohannes Thumshirn struct extent_buffer *leaf; 22034afd2fe8SJohannes Thumshirn int slot; 22044afd2fe8SJohannes Thumshirn 22054358d963SJosef Bacik ret = find_first_block_group(info, path, &key); 22064358d963SJosef Bacik if (ret > 0) 22074358d963SJosef Bacik break; 22084358d963SJosef Bacik if (ret != 0) 22094358d963SJosef Bacik goto error; 22104358d963SJosef Bacik 22114afd2fe8SJohannes Thumshirn leaf = path->nodes[0]; 22124afd2fe8SJohannes Thumshirn slot = path->slots[0]; 22134afd2fe8SJohannes Thumshirn 22144afd2fe8SJohannes Thumshirn read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot), 22154afd2fe8SJohannes Thumshirn sizeof(bgi)); 22164afd2fe8SJohannes Thumshirn 22174afd2fe8SJohannes Thumshirn btrfs_item_key_to_cpu(leaf, &key, slot); 22184afd2fe8SJohannes Thumshirn btrfs_release_path(path); 22194afd2fe8SJohannes Thumshirn ret = read_one_block_group(info, &bgi, &key, need_clear); 2220ffb9e0f0SQu Wenruo if (ret < 0) 22214358d963SJosef Bacik goto error; 2222ffb9e0f0SQu Wenruo key.objectid += key.offset; 2223ffb9e0f0SQu Wenruo key.offset = 0; 22244358d963SJosef Bacik } 22257837fa88SJosef Bacik btrfs_release_path(path); 22264358d963SJosef Bacik 222772804905SJosef Bacik list_for_each_entry(space_info, &info->space_info, list) { 222849ea112dSJosef Bacik int i; 222949ea112dSJosef Bacik 223049ea112dSJosef Bacik for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 223149ea112dSJosef Bacik if (list_empty(&space_info->block_groups[i])) 223249ea112dSJosef Bacik continue; 223349ea112dSJosef Bacik cache = list_first_entry(&space_info->block_groups[i], 223449ea112dSJosef Bacik struct btrfs_block_group, 223549ea112dSJosef Bacik list); 223649ea112dSJosef Bacik btrfs_sysfs_add_block_group_type(cache); 223749ea112dSJosef Bacik } 223849ea112dSJosef Bacik 22394358d963SJosef Bacik if (!(btrfs_get_alloc_profile(info, space_info->flags) & 22404358d963SJosef Bacik (BTRFS_BLOCK_GROUP_RAID10 | 22414358d963SJosef Bacik BTRFS_BLOCK_GROUP_RAID1_MASK | 22424358d963SJosef Bacik BTRFS_BLOCK_GROUP_RAID56_MASK | 22434358d963SJosef Bacik BTRFS_BLOCK_GROUP_DUP))) 22444358d963SJosef Bacik continue; 22454358d963SJosef Bacik /* 22464358d963SJosef Bacik * Avoid allocating from un-mirrored block group if there are 22474358d963SJosef Bacik * mirrored block groups. 22484358d963SJosef Bacik */ 22494358d963SJosef Bacik list_for_each_entry(cache, 22504358d963SJosef Bacik &space_info->block_groups[BTRFS_RAID_RAID0], 22514358d963SJosef Bacik list) 2252e11c0406SJosef Bacik inc_block_group_ro(cache, 1); 22534358d963SJosef Bacik list_for_each_entry(cache, 22544358d963SJosef Bacik &space_info->block_groups[BTRFS_RAID_SINGLE], 22554358d963SJosef Bacik list) 2256e11c0406SJosef Bacik inc_block_group_ro(cache, 1); 22574358d963SJosef Bacik } 22584358d963SJosef Bacik 22594358d963SJosef Bacik btrfs_init_global_block_rsv(info); 22604358d963SJosef Bacik ret = check_chunk_block_group_mappings(info); 22614358d963SJosef Bacik error: 22624358d963SJosef Bacik btrfs_free_path(path); 22632b29726cSQu Wenruo /* 22642b29726cSQu Wenruo * We've hit some error while reading the extent tree, and have 22652b29726cSQu Wenruo * rescue=ibadroots mount option. 22662b29726cSQu Wenruo * Try to fill the tree using dummy block groups so that the user can 22672b29726cSQu Wenruo * continue to mount and grab their data. 22682b29726cSQu Wenruo */ 22692b29726cSQu Wenruo if (ret && btrfs_test_opt(info, IGNOREBADROOTS)) 22702b29726cSQu Wenruo ret = fill_dummy_bgs(info); 22714358d963SJosef Bacik return ret; 22724358d963SJosef Bacik } 22734358d963SJosef Bacik 227479bd3712SFilipe Manana /* 227579bd3712SFilipe Manana * This function, insert_block_group_item(), belongs to the phase 2 of chunk 227679bd3712SFilipe Manana * allocation. 227779bd3712SFilipe Manana * 227879bd3712SFilipe Manana * See the comment at btrfs_chunk_alloc() for details about the chunk allocation 227979bd3712SFilipe Manana * phases. 228079bd3712SFilipe Manana */ 228197f4728aSQu Wenruo static int insert_block_group_item(struct btrfs_trans_handle *trans, 228297f4728aSQu Wenruo struct btrfs_block_group *block_group) 228397f4728aSQu Wenruo { 228497f4728aSQu Wenruo struct btrfs_fs_info *fs_info = trans->fs_info; 228597f4728aSQu Wenruo struct btrfs_block_group_item bgi; 2286dfe8aec4SJosef Bacik struct btrfs_root *root = btrfs_block_group_root(fs_info); 228797f4728aSQu Wenruo struct btrfs_key key; 228897f4728aSQu Wenruo 228997f4728aSQu Wenruo spin_lock(&block_group->lock); 229097f4728aSQu Wenruo btrfs_set_stack_block_group_used(&bgi, block_group->used); 229197f4728aSQu Wenruo btrfs_set_stack_block_group_chunk_objectid(&bgi, 2292*f7238e50SJosef Bacik block_group->global_root_id); 229397f4728aSQu Wenruo btrfs_set_stack_block_group_flags(&bgi, block_group->flags); 229497f4728aSQu Wenruo key.objectid = block_group->start; 229597f4728aSQu Wenruo key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 229697f4728aSQu Wenruo key.offset = block_group->length; 229797f4728aSQu Wenruo spin_unlock(&block_group->lock); 229897f4728aSQu Wenruo 229997f4728aSQu Wenruo return btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi)); 230097f4728aSQu Wenruo } 230197f4728aSQu Wenruo 23022eadb9e7SNikolay Borisov static int insert_dev_extent(struct btrfs_trans_handle *trans, 23032eadb9e7SNikolay Borisov struct btrfs_device *device, u64 chunk_offset, 23042eadb9e7SNikolay Borisov u64 start, u64 num_bytes) 23052eadb9e7SNikolay Borisov { 23062eadb9e7SNikolay Borisov struct btrfs_fs_info *fs_info = device->fs_info; 23072eadb9e7SNikolay Borisov struct btrfs_root *root = fs_info->dev_root; 23082eadb9e7SNikolay Borisov struct btrfs_path *path; 23092eadb9e7SNikolay Borisov struct btrfs_dev_extent *extent; 23102eadb9e7SNikolay Borisov struct extent_buffer *leaf; 23112eadb9e7SNikolay Borisov struct btrfs_key key; 23122eadb9e7SNikolay Borisov int ret; 23132eadb9e7SNikolay Borisov 23142eadb9e7SNikolay Borisov WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)); 23152eadb9e7SNikolay Borisov WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); 23162eadb9e7SNikolay Borisov path = btrfs_alloc_path(); 23172eadb9e7SNikolay Borisov if (!path) 23182eadb9e7SNikolay Borisov return -ENOMEM; 23192eadb9e7SNikolay Borisov 23202eadb9e7SNikolay Borisov key.objectid = device->devid; 23212eadb9e7SNikolay Borisov key.type = BTRFS_DEV_EXTENT_KEY; 23222eadb9e7SNikolay Borisov key.offset = start; 23232eadb9e7SNikolay Borisov ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*extent)); 23242eadb9e7SNikolay Borisov if (ret) 23252eadb9e7SNikolay Borisov goto out; 23262eadb9e7SNikolay Borisov 23272eadb9e7SNikolay Borisov leaf = path->nodes[0]; 23282eadb9e7SNikolay Borisov extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent); 23292eadb9e7SNikolay Borisov btrfs_set_dev_extent_chunk_tree(leaf, extent, BTRFS_CHUNK_TREE_OBJECTID); 23302eadb9e7SNikolay Borisov btrfs_set_dev_extent_chunk_objectid(leaf, extent, 23312eadb9e7SNikolay Borisov BTRFS_FIRST_CHUNK_TREE_OBJECTID); 23322eadb9e7SNikolay Borisov btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); 23332eadb9e7SNikolay Borisov 23342eadb9e7SNikolay Borisov btrfs_set_dev_extent_length(leaf, extent, num_bytes); 23352eadb9e7SNikolay Borisov btrfs_mark_buffer_dirty(leaf); 23362eadb9e7SNikolay Borisov out: 23372eadb9e7SNikolay Borisov btrfs_free_path(path); 23382eadb9e7SNikolay Borisov return ret; 23392eadb9e7SNikolay Borisov } 23402eadb9e7SNikolay Borisov 23412eadb9e7SNikolay Borisov /* 23422eadb9e7SNikolay Borisov * This function belongs to phase 2. 23432eadb9e7SNikolay Borisov * 23442eadb9e7SNikolay Borisov * See the comment at btrfs_chunk_alloc() for details about the chunk allocation 23452eadb9e7SNikolay Borisov * phases. 23462eadb9e7SNikolay Borisov */ 23472eadb9e7SNikolay Borisov static int insert_dev_extents(struct btrfs_trans_handle *trans, 23482eadb9e7SNikolay Borisov u64 chunk_offset, u64 chunk_size) 23492eadb9e7SNikolay Borisov { 23502eadb9e7SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 23512eadb9e7SNikolay Borisov struct btrfs_device *device; 23522eadb9e7SNikolay Borisov struct extent_map *em; 23532eadb9e7SNikolay Borisov struct map_lookup *map; 23542eadb9e7SNikolay Borisov u64 dev_offset; 23552eadb9e7SNikolay Borisov u64 stripe_size; 23562eadb9e7SNikolay Borisov int i; 23572eadb9e7SNikolay Borisov int ret = 0; 23582eadb9e7SNikolay Borisov 23592eadb9e7SNikolay Borisov em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size); 23602eadb9e7SNikolay Borisov if (IS_ERR(em)) 23612eadb9e7SNikolay Borisov return PTR_ERR(em); 23622eadb9e7SNikolay Borisov 23632eadb9e7SNikolay Borisov map = em->map_lookup; 23642eadb9e7SNikolay Borisov stripe_size = em->orig_block_len; 23652eadb9e7SNikolay Borisov 23662eadb9e7SNikolay Borisov /* 23672eadb9e7SNikolay Borisov * Take the device list mutex to prevent races with the final phase of 23682eadb9e7SNikolay Borisov * a device replace operation that replaces the device object associated 23692eadb9e7SNikolay Borisov * with the map's stripes, because the device object's id can change 23702eadb9e7SNikolay Borisov * at any time during that final phase of the device replace operation 23712eadb9e7SNikolay Borisov * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the 23722eadb9e7SNikolay Borisov * replaced device and then see it with an ID of BTRFS_DEV_REPLACE_DEVID, 23732eadb9e7SNikolay Borisov * resulting in persisting a device extent item with such ID. 23742eadb9e7SNikolay Borisov */ 23752eadb9e7SNikolay Borisov mutex_lock(&fs_info->fs_devices->device_list_mutex); 23762eadb9e7SNikolay Borisov for (i = 0; i < map->num_stripes; i++) { 23772eadb9e7SNikolay Borisov device = map->stripes[i].dev; 23782eadb9e7SNikolay Borisov dev_offset = map->stripes[i].physical; 23792eadb9e7SNikolay Borisov 23802eadb9e7SNikolay Borisov ret = insert_dev_extent(trans, device, chunk_offset, dev_offset, 23812eadb9e7SNikolay Borisov stripe_size); 23822eadb9e7SNikolay Borisov if (ret) 23832eadb9e7SNikolay Borisov break; 23842eadb9e7SNikolay Borisov } 23852eadb9e7SNikolay Borisov mutex_unlock(&fs_info->fs_devices->device_list_mutex); 23862eadb9e7SNikolay Borisov 23872eadb9e7SNikolay Borisov free_extent_map(em); 23882eadb9e7SNikolay Borisov return ret; 23892eadb9e7SNikolay Borisov } 23902eadb9e7SNikolay Borisov 239179bd3712SFilipe Manana /* 239279bd3712SFilipe Manana * This function, btrfs_create_pending_block_groups(), belongs to the phase 2 of 239379bd3712SFilipe Manana * chunk allocation. 239479bd3712SFilipe Manana * 239579bd3712SFilipe Manana * See the comment at btrfs_chunk_alloc() for details about the chunk allocation 239679bd3712SFilipe Manana * phases. 239779bd3712SFilipe Manana */ 23984358d963SJosef Bacik void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans) 23994358d963SJosef Bacik { 24004358d963SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 240132da5386SDavid Sterba struct btrfs_block_group *block_group; 24024358d963SJosef Bacik int ret = 0; 24034358d963SJosef Bacik 24044358d963SJosef Bacik while (!list_empty(&trans->new_bgs)) { 240549ea112dSJosef Bacik int index; 240649ea112dSJosef Bacik 24074358d963SJosef Bacik block_group = list_first_entry(&trans->new_bgs, 240832da5386SDavid Sterba struct btrfs_block_group, 24094358d963SJosef Bacik bg_list); 24104358d963SJosef Bacik if (ret) 24114358d963SJosef Bacik goto next; 24124358d963SJosef Bacik 241349ea112dSJosef Bacik index = btrfs_bg_flags_to_raid_index(block_group->flags); 241449ea112dSJosef Bacik 241597f4728aSQu Wenruo ret = insert_block_group_item(trans, block_group); 24164358d963SJosef Bacik if (ret) 24174358d963SJosef Bacik btrfs_abort_transaction(trans, ret); 241879bd3712SFilipe Manana if (!block_group->chunk_item_inserted) { 241979bd3712SFilipe Manana mutex_lock(&fs_info->chunk_mutex); 242079bd3712SFilipe Manana ret = btrfs_chunk_alloc_add_chunk_item(trans, block_group); 242179bd3712SFilipe Manana mutex_unlock(&fs_info->chunk_mutex); 242279bd3712SFilipe Manana if (ret) 242379bd3712SFilipe Manana btrfs_abort_transaction(trans, ret); 242479bd3712SFilipe Manana } 24252eadb9e7SNikolay Borisov ret = insert_dev_extents(trans, block_group->start, 242697f4728aSQu Wenruo block_group->length); 24274358d963SJosef Bacik if (ret) 24284358d963SJosef Bacik btrfs_abort_transaction(trans, ret); 24294358d963SJosef Bacik add_block_group_free_space(trans, block_group); 243049ea112dSJosef Bacik 243149ea112dSJosef Bacik /* 243249ea112dSJosef Bacik * If we restriped during balance, we may have added a new raid 243349ea112dSJosef Bacik * type, so now add the sysfs entries when it is safe to do so. 243449ea112dSJosef Bacik * We don't have to worry about locking here as it's handled in 243549ea112dSJosef Bacik * btrfs_sysfs_add_block_group_type. 243649ea112dSJosef Bacik */ 243749ea112dSJosef Bacik if (block_group->space_info->block_group_kobjs[index] == NULL) 243849ea112dSJosef Bacik btrfs_sysfs_add_block_group_type(block_group); 243949ea112dSJosef Bacik 24404358d963SJosef Bacik /* Already aborted the transaction if it failed. */ 24414358d963SJosef Bacik next: 24424358d963SJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 24434358d963SJosef Bacik list_del_init(&block_group->bg_list); 24444358d963SJosef Bacik } 24454358d963SJosef Bacik btrfs_trans_release_chunk_metadata(trans); 24464358d963SJosef Bacik } 24474358d963SJosef Bacik 2448*f7238e50SJosef Bacik /* 2449*f7238e50SJosef Bacik * For extent tree v2 we use the block_group_item->chunk_offset to point at our 2450*f7238e50SJosef Bacik * global root id. For v1 it's always set to BTRFS_FIRST_CHUNK_TREE_OBJECTID. 2451*f7238e50SJosef Bacik */ 2452*f7238e50SJosef Bacik static u64 calculate_global_root_id(struct btrfs_fs_info *fs_info, u64 offset) 2453*f7238e50SJosef Bacik { 2454*f7238e50SJosef Bacik u64 div = SZ_1G; 2455*f7238e50SJosef Bacik u64 index; 2456*f7238e50SJosef Bacik 2457*f7238e50SJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 2458*f7238e50SJosef Bacik return BTRFS_FIRST_CHUNK_TREE_OBJECTID; 2459*f7238e50SJosef Bacik 2460*f7238e50SJosef Bacik /* If we have a smaller fs index based on 128MiB. */ 2461*f7238e50SJosef Bacik if (btrfs_super_total_bytes(fs_info->super_copy) <= (SZ_1G * 10ULL)) 2462*f7238e50SJosef Bacik div = SZ_128M; 2463*f7238e50SJosef Bacik 2464*f7238e50SJosef Bacik offset = div64_u64(offset, div); 2465*f7238e50SJosef Bacik div64_u64_rem(offset, fs_info->nr_global_roots, &index); 2466*f7238e50SJosef Bacik return index; 2467*f7238e50SJosef Bacik } 2468*f7238e50SJosef Bacik 246979bd3712SFilipe Manana struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *trans, 247079bd3712SFilipe Manana u64 bytes_used, u64 type, 247179bd3712SFilipe Manana u64 chunk_offset, u64 size) 24724358d963SJosef Bacik { 24734358d963SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 247432da5386SDavid Sterba struct btrfs_block_group *cache; 24754358d963SJosef Bacik int ret; 24764358d963SJosef Bacik 24774358d963SJosef Bacik btrfs_set_log_full_commit(trans); 24784358d963SJosef Bacik 24799afc6649SQu Wenruo cache = btrfs_create_block_group_cache(fs_info, chunk_offset); 24804358d963SJosef Bacik if (!cache) 248179bd3712SFilipe Manana return ERR_PTR(-ENOMEM); 24824358d963SJosef Bacik 24839afc6649SQu Wenruo cache->length = size; 2484e3e39c72SMarcos Paulo de Souza set_free_space_tree_thresholds(cache); 2485bf38be65SDavid Sterba cache->used = bytes_used; 24864358d963SJosef Bacik cache->flags = type; 24874358d963SJosef Bacik cache->last_byte_to_unpin = (u64)-1; 24884358d963SJosef Bacik cache->cached = BTRFS_CACHE_FINISHED; 2489*f7238e50SJosef Bacik cache->global_root_id = calculate_global_root_id(fs_info, cache->start); 2490*f7238e50SJosef Bacik 2491997e3e2eSBoris Burkov if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) 24924358d963SJosef Bacik cache->needs_free_space = 1; 249308e11a3dSNaohiro Aota 2494a94794d5SNaohiro Aota ret = btrfs_load_block_group_zone_info(cache, true); 249508e11a3dSNaohiro Aota if (ret) { 249608e11a3dSNaohiro Aota btrfs_put_block_group(cache); 249779bd3712SFilipe Manana return ERR_PTR(ret); 249808e11a3dSNaohiro Aota } 249908e11a3dSNaohiro Aota 2500eb66a010SNaohiro Aota /* 2501eb66a010SNaohiro Aota * New block group is likely to be used soon. Try to activate it now. 2502eb66a010SNaohiro Aota * Failure is OK for now. 2503eb66a010SNaohiro Aota */ 2504eb66a010SNaohiro Aota btrfs_zone_activate(cache); 2505eb66a010SNaohiro Aota 25064358d963SJosef Bacik ret = exclude_super_stripes(cache); 25074358d963SJosef Bacik if (ret) { 25084358d963SJosef Bacik /* We may have excluded something, so call this just in case */ 25094358d963SJosef Bacik btrfs_free_excluded_extents(cache); 25104358d963SJosef Bacik btrfs_put_block_group(cache); 251179bd3712SFilipe Manana return ERR_PTR(ret); 25124358d963SJosef Bacik } 25134358d963SJosef Bacik 25144358d963SJosef Bacik add_new_free_space(cache, chunk_offset, chunk_offset + size); 25154358d963SJosef Bacik 25164358d963SJosef Bacik btrfs_free_excluded_extents(cache); 25174358d963SJosef Bacik 25184358d963SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 25194358d963SJosef Bacik if (btrfs_should_fragment_free_space(cache)) { 25204358d963SJosef Bacik u64 new_bytes_used = size - bytes_used; 25214358d963SJosef Bacik 25224358d963SJosef Bacik bytes_used += new_bytes_used >> 1; 2523e11c0406SJosef Bacik fragment_free_space(cache); 25244358d963SJosef Bacik } 25254358d963SJosef Bacik #endif 25264358d963SJosef Bacik /* 25274358d963SJosef Bacik * Ensure the corresponding space_info object is created and 25284358d963SJosef Bacik * assigned to our block group. We want our bg to be added to the rbtree 25294358d963SJosef Bacik * with its ->space_info set. 25304358d963SJosef Bacik */ 25314358d963SJosef Bacik cache->space_info = btrfs_find_space_info(fs_info, cache->flags); 25324358d963SJosef Bacik ASSERT(cache->space_info); 25334358d963SJosef Bacik 25344358d963SJosef Bacik ret = btrfs_add_block_group_cache(fs_info, cache); 25354358d963SJosef Bacik if (ret) { 25364358d963SJosef Bacik btrfs_remove_free_space_cache(cache); 25374358d963SJosef Bacik btrfs_put_block_group(cache); 253879bd3712SFilipe Manana return ERR_PTR(ret); 25394358d963SJosef Bacik } 25404358d963SJosef Bacik 25414358d963SJosef Bacik /* 25424358d963SJosef Bacik * Now that our block group has its ->space_info set and is inserted in 25434358d963SJosef Bacik * the rbtree, update the space info's counters. 25444358d963SJosef Bacik */ 25454358d963SJosef Bacik trace_btrfs_add_block_group(fs_info, cache, 1); 25464358d963SJosef Bacik btrfs_update_space_info(fs_info, cache->flags, size, bytes_used, 254798173255SNaohiro Aota cache->bytes_super, cache->zone_unusable, 254898173255SNaohiro Aota &cache->space_info); 25494358d963SJosef Bacik btrfs_update_global_block_rsv(fs_info); 25504358d963SJosef Bacik 25514358d963SJosef Bacik link_block_group(cache); 25524358d963SJosef Bacik 25534358d963SJosef Bacik list_add_tail(&cache->bg_list, &trans->new_bgs); 25544358d963SJosef Bacik trans->delayed_ref_updates++; 25554358d963SJosef Bacik btrfs_update_delayed_refs_rsv(trans); 25564358d963SJosef Bacik 25574358d963SJosef Bacik set_avail_alloc_bits(fs_info, type); 255879bd3712SFilipe Manana return cache; 25594358d963SJosef Bacik } 256026ce2095SJosef Bacik 2561b12de528SQu Wenruo /* 2562b12de528SQu Wenruo * Mark one block group RO, can be called several times for the same block 2563b12de528SQu Wenruo * group. 2564b12de528SQu Wenruo * 2565b12de528SQu Wenruo * @cache: the destination block group 2566b12de528SQu Wenruo * @do_chunk_alloc: whether need to do chunk pre-allocation, this is to 2567b12de528SQu Wenruo * ensure we still have some free space after marking this 2568b12de528SQu Wenruo * block group RO. 2569b12de528SQu Wenruo */ 2570b12de528SQu Wenruo int btrfs_inc_block_group_ro(struct btrfs_block_group *cache, 2571b12de528SQu Wenruo bool do_chunk_alloc) 257226ce2095SJosef Bacik { 257326ce2095SJosef Bacik struct btrfs_fs_info *fs_info = cache->fs_info; 257426ce2095SJosef Bacik struct btrfs_trans_handle *trans; 2575dfe8aec4SJosef Bacik struct btrfs_root *root = btrfs_block_group_root(fs_info); 257626ce2095SJosef Bacik u64 alloc_flags; 257726ce2095SJosef Bacik int ret; 2578b6e9f16cSNikolay Borisov bool dirty_bg_running; 257926ce2095SJosef Bacik 25802d192fc4SQu Wenruo /* 25812d192fc4SQu Wenruo * This can only happen when we are doing read-only scrub on read-only 25822d192fc4SQu Wenruo * mount. 25832d192fc4SQu Wenruo * In that case we should not start a new transaction on read-only fs. 25842d192fc4SQu Wenruo * Thus here we skip all chunk allocations. 25852d192fc4SQu Wenruo */ 25862d192fc4SQu Wenruo if (sb_rdonly(fs_info->sb)) { 25872d192fc4SQu Wenruo mutex_lock(&fs_info->ro_block_group_mutex); 25882d192fc4SQu Wenruo ret = inc_block_group_ro(cache, 0); 25892d192fc4SQu Wenruo mutex_unlock(&fs_info->ro_block_group_mutex); 25902d192fc4SQu Wenruo return ret; 25912d192fc4SQu Wenruo } 25922d192fc4SQu Wenruo 2593b6e9f16cSNikolay Borisov do { 2594dfe8aec4SJosef Bacik trans = btrfs_join_transaction(root); 259526ce2095SJosef Bacik if (IS_ERR(trans)) 259626ce2095SJosef Bacik return PTR_ERR(trans); 259726ce2095SJosef Bacik 2598b6e9f16cSNikolay Borisov dirty_bg_running = false; 2599b6e9f16cSNikolay Borisov 260026ce2095SJosef Bacik /* 2601b6e9f16cSNikolay Borisov * We're not allowed to set block groups readonly after the dirty 2602b6e9f16cSNikolay Borisov * block group cache has started writing. If it already started, 2603b6e9f16cSNikolay Borisov * back off and let this transaction commit. 260426ce2095SJosef Bacik */ 260526ce2095SJosef Bacik mutex_lock(&fs_info->ro_block_group_mutex); 260626ce2095SJosef Bacik if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { 260726ce2095SJosef Bacik u64 transid = trans->transid; 260826ce2095SJosef Bacik 260926ce2095SJosef Bacik mutex_unlock(&fs_info->ro_block_group_mutex); 261026ce2095SJosef Bacik btrfs_end_transaction(trans); 261126ce2095SJosef Bacik 261226ce2095SJosef Bacik ret = btrfs_wait_for_commit(fs_info, transid); 261326ce2095SJosef Bacik if (ret) 261426ce2095SJosef Bacik return ret; 2615b6e9f16cSNikolay Borisov dirty_bg_running = true; 261626ce2095SJosef Bacik } 2617b6e9f16cSNikolay Borisov } while (dirty_bg_running); 261826ce2095SJosef Bacik 2619b12de528SQu Wenruo if (do_chunk_alloc) { 262026ce2095SJosef Bacik /* 2621b12de528SQu Wenruo * If we are changing raid levels, try to allocate a 2622b12de528SQu Wenruo * corresponding block group with the new raid level. 262326ce2095SJosef Bacik */ 2624349e120eSJosef Bacik alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); 262526ce2095SJosef Bacik if (alloc_flags != cache->flags) { 2626b12de528SQu Wenruo ret = btrfs_chunk_alloc(trans, alloc_flags, 2627b12de528SQu Wenruo CHUNK_ALLOC_FORCE); 262826ce2095SJosef Bacik /* 262926ce2095SJosef Bacik * ENOSPC is allowed here, we may have enough space 2630b12de528SQu Wenruo * already allocated at the new raid level to carry on 263126ce2095SJosef Bacik */ 263226ce2095SJosef Bacik if (ret == -ENOSPC) 263326ce2095SJosef Bacik ret = 0; 263426ce2095SJosef Bacik if (ret < 0) 263526ce2095SJosef Bacik goto out; 263626ce2095SJosef Bacik } 2637b12de528SQu Wenruo } 263826ce2095SJosef Bacik 2639a7a63accSJosef Bacik ret = inc_block_group_ro(cache, 0); 2640195a49eaSFilipe Manana if (!do_chunk_alloc || ret == -ETXTBSY) 2641b12de528SQu Wenruo goto unlock_out; 264226ce2095SJosef Bacik if (!ret) 264326ce2095SJosef Bacik goto out; 264426ce2095SJosef Bacik alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags); 264526ce2095SJosef Bacik ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE); 264626ce2095SJosef Bacik if (ret < 0) 264726ce2095SJosef Bacik goto out; 2648e11c0406SJosef Bacik ret = inc_block_group_ro(cache, 0); 2649195a49eaSFilipe Manana if (ret == -ETXTBSY) 2650195a49eaSFilipe Manana goto unlock_out; 265126ce2095SJosef Bacik out: 265226ce2095SJosef Bacik if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { 2653349e120eSJosef Bacik alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); 265426ce2095SJosef Bacik mutex_lock(&fs_info->chunk_mutex); 265526ce2095SJosef Bacik check_system_chunk(trans, alloc_flags); 265626ce2095SJosef Bacik mutex_unlock(&fs_info->chunk_mutex); 265726ce2095SJosef Bacik } 2658b12de528SQu Wenruo unlock_out: 265926ce2095SJosef Bacik mutex_unlock(&fs_info->ro_block_group_mutex); 266026ce2095SJosef Bacik 266126ce2095SJosef Bacik btrfs_end_transaction(trans); 266226ce2095SJosef Bacik return ret; 266326ce2095SJosef Bacik } 266426ce2095SJosef Bacik 266532da5386SDavid Sterba void btrfs_dec_block_group_ro(struct btrfs_block_group *cache) 266626ce2095SJosef Bacik { 266726ce2095SJosef Bacik struct btrfs_space_info *sinfo = cache->space_info; 266826ce2095SJosef Bacik u64 num_bytes; 266926ce2095SJosef Bacik 267026ce2095SJosef Bacik BUG_ON(!cache->ro); 267126ce2095SJosef Bacik 267226ce2095SJosef Bacik spin_lock(&sinfo->lock); 267326ce2095SJosef Bacik spin_lock(&cache->lock); 267426ce2095SJosef Bacik if (!--cache->ro) { 2675169e0da9SNaohiro Aota if (btrfs_is_zoned(cache->fs_info)) { 2676169e0da9SNaohiro Aota /* Migrate zone_unusable bytes back */ 267798173255SNaohiro Aota cache->zone_unusable = 267898173255SNaohiro Aota (cache->alloc_offset - cache->used) + 267998173255SNaohiro Aota (cache->length - cache->zone_capacity); 2680169e0da9SNaohiro Aota sinfo->bytes_zone_unusable += cache->zone_unusable; 2681169e0da9SNaohiro Aota sinfo->bytes_readonly -= cache->zone_unusable; 2682169e0da9SNaohiro Aota } 2683f9f28e5bSNaohiro Aota num_bytes = cache->length - cache->reserved - 2684f9f28e5bSNaohiro Aota cache->pinned - cache->bytes_super - 2685f9f28e5bSNaohiro Aota cache->zone_unusable - cache->used; 2686f9f28e5bSNaohiro Aota sinfo->bytes_readonly -= num_bytes; 268726ce2095SJosef Bacik list_del_init(&cache->ro_list); 268826ce2095SJosef Bacik } 268926ce2095SJosef Bacik spin_unlock(&cache->lock); 269026ce2095SJosef Bacik spin_unlock(&sinfo->lock); 269126ce2095SJosef Bacik } 269277745c05SJosef Bacik 26933be4d8efSQu Wenruo static int update_block_group_item(struct btrfs_trans_handle *trans, 269477745c05SJosef Bacik struct btrfs_path *path, 269532da5386SDavid Sterba struct btrfs_block_group *cache) 269677745c05SJosef Bacik { 269777745c05SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 269877745c05SJosef Bacik int ret; 2699dfe8aec4SJosef Bacik struct btrfs_root *root = btrfs_block_group_root(fs_info); 270077745c05SJosef Bacik unsigned long bi; 270177745c05SJosef Bacik struct extent_buffer *leaf; 2702bf38be65SDavid Sterba struct btrfs_block_group_item bgi; 2703b3470b5dSDavid Sterba struct btrfs_key key; 270477745c05SJosef Bacik 2705b3470b5dSDavid Sterba key.objectid = cache->start; 2706b3470b5dSDavid Sterba key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 2707b3470b5dSDavid Sterba key.offset = cache->length; 2708b3470b5dSDavid Sterba 27093be4d8efSQu Wenruo ret = btrfs_search_slot(trans, root, &key, path, 0, 1); 271077745c05SJosef Bacik if (ret) { 271177745c05SJosef Bacik if (ret > 0) 271277745c05SJosef Bacik ret = -ENOENT; 271377745c05SJosef Bacik goto fail; 271477745c05SJosef Bacik } 271577745c05SJosef Bacik 271677745c05SJosef Bacik leaf = path->nodes[0]; 271777745c05SJosef Bacik bi = btrfs_item_ptr_offset(leaf, path->slots[0]); 2718de0dc456SDavid Sterba btrfs_set_stack_block_group_used(&bgi, cache->used); 2719de0dc456SDavid Sterba btrfs_set_stack_block_group_chunk_objectid(&bgi, 2720*f7238e50SJosef Bacik cache->global_root_id); 2721de0dc456SDavid Sterba btrfs_set_stack_block_group_flags(&bgi, cache->flags); 2722bf38be65SDavid Sterba write_extent_buffer(leaf, &bgi, bi, sizeof(bgi)); 272377745c05SJosef Bacik btrfs_mark_buffer_dirty(leaf); 272477745c05SJosef Bacik fail: 272577745c05SJosef Bacik btrfs_release_path(path); 272677745c05SJosef Bacik return ret; 272777745c05SJosef Bacik 272877745c05SJosef Bacik } 272977745c05SJosef Bacik 273032da5386SDavid Sterba static int cache_save_setup(struct btrfs_block_group *block_group, 273177745c05SJosef Bacik struct btrfs_trans_handle *trans, 273277745c05SJosef Bacik struct btrfs_path *path) 273377745c05SJosef Bacik { 273477745c05SJosef Bacik struct btrfs_fs_info *fs_info = block_group->fs_info; 273577745c05SJosef Bacik struct btrfs_root *root = fs_info->tree_root; 273677745c05SJosef Bacik struct inode *inode = NULL; 273777745c05SJosef Bacik struct extent_changeset *data_reserved = NULL; 273877745c05SJosef Bacik u64 alloc_hint = 0; 273977745c05SJosef Bacik int dcs = BTRFS_DC_ERROR; 27400044ae11SQu Wenruo u64 cache_size = 0; 274177745c05SJosef Bacik int retries = 0; 274277745c05SJosef Bacik int ret = 0; 274377745c05SJosef Bacik 2744af456a2cSBoris Burkov if (!btrfs_test_opt(fs_info, SPACE_CACHE)) 2745af456a2cSBoris Burkov return 0; 2746af456a2cSBoris Burkov 274777745c05SJosef Bacik /* 274877745c05SJosef Bacik * If this block group is smaller than 100 megs don't bother caching the 274977745c05SJosef Bacik * block group. 275077745c05SJosef Bacik */ 2751b3470b5dSDavid Sterba if (block_group->length < (100 * SZ_1M)) { 275277745c05SJosef Bacik spin_lock(&block_group->lock); 275377745c05SJosef Bacik block_group->disk_cache_state = BTRFS_DC_WRITTEN; 275477745c05SJosef Bacik spin_unlock(&block_group->lock); 275577745c05SJosef Bacik return 0; 275677745c05SJosef Bacik } 275777745c05SJosef Bacik 2758bf31f87fSDavid Sterba if (TRANS_ABORTED(trans)) 275977745c05SJosef Bacik return 0; 276077745c05SJosef Bacik again: 276177745c05SJosef Bacik inode = lookup_free_space_inode(block_group, path); 276277745c05SJosef Bacik if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { 276377745c05SJosef Bacik ret = PTR_ERR(inode); 276477745c05SJosef Bacik btrfs_release_path(path); 276577745c05SJosef Bacik goto out; 276677745c05SJosef Bacik } 276777745c05SJosef Bacik 276877745c05SJosef Bacik if (IS_ERR(inode)) { 276977745c05SJosef Bacik BUG_ON(retries); 277077745c05SJosef Bacik retries++; 277177745c05SJosef Bacik 277277745c05SJosef Bacik if (block_group->ro) 277377745c05SJosef Bacik goto out_free; 277477745c05SJosef Bacik 277577745c05SJosef Bacik ret = create_free_space_inode(trans, block_group, path); 277677745c05SJosef Bacik if (ret) 277777745c05SJosef Bacik goto out_free; 277877745c05SJosef Bacik goto again; 277977745c05SJosef Bacik } 278077745c05SJosef Bacik 278177745c05SJosef Bacik /* 278277745c05SJosef Bacik * We want to set the generation to 0, that way if anything goes wrong 278377745c05SJosef Bacik * from here on out we know not to trust this cache when we load up next 278477745c05SJosef Bacik * time. 278577745c05SJosef Bacik */ 278677745c05SJosef Bacik BTRFS_I(inode)->generation = 0; 27879a56fcd1SNikolay Borisov ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); 278877745c05SJosef Bacik if (ret) { 278977745c05SJosef Bacik /* 279077745c05SJosef Bacik * So theoretically we could recover from this, simply set the 279177745c05SJosef Bacik * super cache generation to 0 so we know to invalidate the 279277745c05SJosef Bacik * cache, but then we'd have to keep track of the block groups 279377745c05SJosef Bacik * that fail this way so we know we _have_ to reset this cache 279477745c05SJosef Bacik * before the next commit or risk reading stale cache. So to 279577745c05SJosef Bacik * limit our exposure to horrible edge cases lets just abort the 279677745c05SJosef Bacik * transaction, this only happens in really bad situations 279777745c05SJosef Bacik * anyway. 279877745c05SJosef Bacik */ 279977745c05SJosef Bacik btrfs_abort_transaction(trans, ret); 280077745c05SJosef Bacik goto out_put; 280177745c05SJosef Bacik } 280277745c05SJosef Bacik WARN_ON(ret); 280377745c05SJosef Bacik 280477745c05SJosef Bacik /* We've already setup this transaction, go ahead and exit */ 280577745c05SJosef Bacik if (block_group->cache_generation == trans->transid && 280677745c05SJosef Bacik i_size_read(inode)) { 280777745c05SJosef Bacik dcs = BTRFS_DC_SETUP; 280877745c05SJosef Bacik goto out_put; 280977745c05SJosef Bacik } 281077745c05SJosef Bacik 281177745c05SJosef Bacik if (i_size_read(inode) > 0) { 281277745c05SJosef Bacik ret = btrfs_check_trunc_cache_free_space(fs_info, 281377745c05SJosef Bacik &fs_info->global_block_rsv); 281477745c05SJosef Bacik if (ret) 281577745c05SJosef Bacik goto out_put; 281677745c05SJosef Bacik 281777745c05SJosef Bacik ret = btrfs_truncate_free_space_cache(trans, NULL, inode); 281877745c05SJosef Bacik if (ret) 281977745c05SJosef Bacik goto out_put; 282077745c05SJosef Bacik } 282177745c05SJosef Bacik 282277745c05SJosef Bacik spin_lock(&block_group->lock); 282377745c05SJosef Bacik if (block_group->cached != BTRFS_CACHE_FINISHED || 282477745c05SJosef Bacik !btrfs_test_opt(fs_info, SPACE_CACHE)) { 282577745c05SJosef Bacik /* 282677745c05SJosef Bacik * don't bother trying to write stuff out _if_ 282777745c05SJosef Bacik * a) we're not cached, 282877745c05SJosef Bacik * b) we're with nospace_cache mount option, 282977745c05SJosef Bacik * c) we're with v2 space_cache (FREE_SPACE_TREE). 283077745c05SJosef Bacik */ 283177745c05SJosef Bacik dcs = BTRFS_DC_WRITTEN; 283277745c05SJosef Bacik spin_unlock(&block_group->lock); 283377745c05SJosef Bacik goto out_put; 283477745c05SJosef Bacik } 283577745c05SJosef Bacik spin_unlock(&block_group->lock); 283677745c05SJosef Bacik 283777745c05SJosef Bacik /* 283877745c05SJosef Bacik * We hit an ENOSPC when setting up the cache in this transaction, just 283977745c05SJosef Bacik * skip doing the setup, we've already cleared the cache so we're safe. 284077745c05SJosef Bacik */ 284177745c05SJosef Bacik if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { 284277745c05SJosef Bacik ret = -ENOSPC; 284377745c05SJosef Bacik goto out_put; 284477745c05SJosef Bacik } 284577745c05SJosef Bacik 284677745c05SJosef Bacik /* 284777745c05SJosef Bacik * Try to preallocate enough space based on how big the block group is. 284877745c05SJosef Bacik * Keep in mind this has to include any pinned space which could end up 284977745c05SJosef Bacik * taking up quite a bit since it's not folded into the other space 285077745c05SJosef Bacik * cache. 285177745c05SJosef Bacik */ 28520044ae11SQu Wenruo cache_size = div_u64(block_group->length, SZ_256M); 28530044ae11SQu Wenruo if (!cache_size) 28540044ae11SQu Wenruo cache_size = 1; 285577745c05SJosef Bacik 28560044ae11SQu Wenruo cache_size *= 16; 28570044ae11SQu Wenruo cache_size *= fs_info->sectorsize; 285877745c05SJosef Bacik 285936ea6f3eSNikolay Borisov ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, 0, 28600044ae11SQu Wenruo cache_size); 286177745c05SJosef Bacik if (ret) 286277745c05SJosef Bacik goto out_put; 286377745c05SJosef Bacik 28640044ae11SQu Wenruo ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, cache_size, 28650044ae11SQu Wenruo cache_size, cache_size, 286677745c05SJosef Bacik &alloc_hint); 286777745c05SJosef Bacik /* 286877745c05SJosef Bacik * Our cache requires contiguous chunks so that we don't modify a bunch 286977745c05SJosef Bacik * of metadata or split extents when writing the cache out, which means 287077745c05SJosef Bacik * we can enospc if we are heavily fragmented in addition to just normal 287177745c05SJosef Bacik * out of space conditions. So if we hit this just skip setting up any 287277745c05SJosef Bacik * other block groups for this transaction, maybe we'll unpin enough 287377745c05SJosef Bacik * space the next time around. 287477745c05SJosef Bacik */ 287577745c05SJosef Bacik if (!ret) 287677745c05SJosef Bacik dcs = BTRFS_DC_SETUP; 287777745c05SJosef Bacik else if (ret == -ENOSPC) 287877745c05SJosef Bacik set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); 287977745c05SJosef Bacik 288077745c05SJosef Bacik out_put: 288177745c05SJosef Bacik iput(inode); 288277745c05SJosef Bacik out_free: 288377745c05SJosef Bacik btrfs_release_path(path); 288477745c05SJosef Bacik out: 288577745c05SJosef Bacik spin_lock(&block_group->lock); 288677745c05SJosef Bacik if (!ret && dcs == BTRFS_DC_SETUP) 288777745c05SJosef Bacik block_group->cache_generation = trans->transid; 288877745c05SJosef Bacik block_group->disk_cache_state = dcs; 288977745c05SJosef Bacik spin_unlock(&block_group->lock); 289077745c05SJosef Bacik 289177745c05SJosef Bacik extent_changeset_free(data_reserved); 289277745c05SJosef Bacik return ret; 289377745c05SJosef Bacik } 289477745c05SJosef Bacik 289577745c05SJosef Bacik int btrfs_setup_space_cache(struct btrfs_trans_handle *trans) 289677745c05SJosef Bacik { 289777745c05SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 289832da5386SDavid Sterba struct btrfs_block_group *cache, *tmp; 289977745c05SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 290077745c05SJosef Bacik struct btrfs_path *path; 290177745c05SJosef Bacik 290277745c05SJosef Bacik if (list_empty(&cur_trans->dirty_bgs) || 290377745c05SJosef Bacik !btrfs_test_opt(fs_info, SPACE_CACHE)) 290477745c05SJosef Bacik return 0; 290577745c05SJosef Bacik 290677745c05SJosef Bacik path = btrfs_alloc_path(); 290777745c05SJosef Bacik if (!path) 290877745c05SJosef Bacik return -ENOMEM; 290977745c05SJosef Bacik 291077745c05SJosef Bacik /* Could add new block groups, use _safe just in case */ 291177745c05SJosef Bacik list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, 291277745c05SJosef Bacik dirty_list) { 291377745c05SJosef Bacik if (cache->disk_cache_state == BTRFS_DC_CLEAR) 291477745c05SJosef Bacik cache_save_setup(cache, trans, path); 291577745c05SJosef Bacik } 291677745c05SJosef Bacik 291777745c05SJosef Bacik btrfs_free_path(path); 291877745c05SJosef Bacik return 0; 291977745c05SJosef Bacik } 292077745c05SJosef Bacik 292177745c05SJosef Bacik /* 292277745c05SJosef Bacik * Transaction commit does final block group cache writeback during a critical 292377745c05SJosef Bacik * section where nothing is allowed to change the FS. This is required in 292477745c05SJosef Bacik * order for the cache to actually match the block group, but can introduce a 292577745c05SJosef Bacik * lot of latency into the commit. 292677745c05SJosef Bacik * 292777745c05SJosef Bacik * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO. 292877745c05SJosef Bacik * There's a chance we'll have to redo some of it if the block group changes 292977745c05SJosef Bacik * again during the commit, but it greatly reduces the commit latency by 293077745c05SJosef Bacik * getting rid of the easy block groups while we're still allowing others to 293177745c05SJosef Bacik * join the commit. 293277745c05SJosef Bacik */ 293377745c05SJosef Bacik int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans) 293477745c05SJosef Bacik { 293577745c05SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 293632da5386SDavid Sterba struct btrfs_block_group *cache; 293777745c05SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 293877745c05SJosef Bacik int ret = 0; 293977745c05SJosef Bacik int should_put; 294077745c05SJosef Bacik struct btrfs_path *path = NULL; 294177745c05SJosef Bacik LIST_HEAD(dirty); 294277745c05SJosef Bacik struct list_head *io = &cur_trans->io_bgs; 294377745c05SJosef Bacik int num_started = 0; 294477745c05SJosef Bacik int loops = 0; 294577745c05SJosef Bacik 294677745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 294777745c05SJosef Bacik if (list_empty(&cur_trans->dirty_bgs)) { 294877745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 294977745c05SJosef Bacik return 0; 295077745c05SJosef Bacik } 295177745c05SJosef Bacik list_splice_init(&cur_trans->dirty_bgs, &dirty); 295277745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 295377745c05SJosef Bacik 295477745c05SJosef Bacik again: 295577745c05SJosef Bacik /* Make sure all the block groups on our dirty list actually exist */ 295677745c05SJosef Bacik btrfs_create_pending_block_groups(trans); 295777745c05SJosef Bacik 295877745c05SJosef Bacik if (!path) { 295977745c05SJosef Bacik path = btrfs_alloc_path(); 2960938fcbfbSJosef Bacik if (!path) { 2961938fcbfbSJosef Bacik ret = -ENOMEM; 2962938fcbfbSJosef Bacik goto out; 2963938fcbfbSJosef Bacik } 296477745c05SJosef Bacik } 296577745c05SJosef Bacik 296677745c05SJosef Bacik /* 296777745c05SJosef Bacik * cache_write_mutex is here only to save us from balance or automatic 296877745c05SJosef Bacik * removal of empty block groups deleting this block group while we are 296977745c05SJosef Bacik * writing out the cache 297077745c05SJosef Bacik */ 297177745c05SJosef Bacik mutex_lock(&trans->transaction->cache_write_mutex); 297277745c05SJosef Bacik while (!list_empty(&dirty)) { 297377745c05SJosef Bacik bool drop_reserve = true; 297477745c05SJosef Bacik 297532da5386SDavid Sterba cache = list_first_entry(&dirty, struct btrfs_block_group, 297677745c05SJosef Bacik dirty_list); 297777745c05SJosef Bacik /* 297877745c05SJosef Bacik * This can happen if something re-dirties a block group that 297977745c05SJosef Bacik * is already under IO. Just wait for it to finish and then do 298077745c05SJosef Bacik * it all again 298177745c05SJosef Bacik */ 298277745c05SJosef Bacik if (!list_empty(&cache->io_list)) { 298377745c05SJosef Bacik list_del_init(&cache->io_list); 298477745c05SJosef Bacik btrfs_wait_cache_io(trans, cache, path); 298577745c05SJosef Bacik btrfs_put_block_group(cache); 298677745c05SJosef Bacik } 298777745c05SJosef Bacik 298877745c05SJosef Bacik 298977745c05SJosef Bacik /* 299077745c05SJosef Bacik * btrfs_wait_cache_io uses the cache->dirty_list to decide if 299177745c05SJosef Bacik * it should update the cache_state. Don't delete until after 299277745c05SJosef Bacik * we wait. 299377745c05SJosef Bacik * 299477745c05SJosef Bacik * Since we're not running in the commit critical section 299577745c05SJosef Bacik * we need the dirty_bgs_lock to protect from update_block_group 299677745c05SJosef Bacik */ 299777745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 299877745c05SJosef Bacik list_del_init(&cache->dirty_list); 299977745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 300077745c05SJosef Bacik 300177745c05SJosef Bacik should_put = 1; 300277745c05SJosef Bacik 300377745c05SJosef Bacik cache_save_setup(cache, trans, path); 300477745c05SJosef Bacik 300577745c05SJosef Bacik if (cache->disk_cache_state == BTRFS_DC_SETUP) { 300677745c05SJosef Bacik cache->io_ctl.inode = NULL; 300777745c05SJosef Bacik ret = btrfs_write_out_cache(trans, cache, path); 300877745c05SJosef Bacik if (ret == 0 && cache->io_ctl.inode) { 300977745c05SJosef Bacik num_started++; 301077745c05SJosef Bacik should_put = 0; 301177745c05SJosef Bacik 301277745c05SJosef Bacik /* 301377745c05SJosef Bacik * The cache_write_mutex is protecting the 301477745c05SJosef Bacik * io_list, also refer to the definition of 301577745c05SJosef Bacik * btrfs_transaction::io_bgs for more details 301677745c05SJosef Bacik */ 301777745c05SJosef Bacik list_add_tail(&cache->io_list, io); 301877745c05SJosef Bacik } else { 301977745c05SJosef Bacik /* 302077745c05SJosef Bacik * If we failed to write the cache, the 302177745c05SJosef Bacik * generation will be bad and life goes on 302277745c05SJosef Bacik */ 302377745c05SJosef Bacik ret = 0; 302477745c05SJosef Bacik } 302577745c05SJosef Bacik } 302677745c05SJosef Bacik if (!ret) { 30273be4d8efSQu Wenruo ret = update_block_group_item(trans, path, cache); 302877745c05SJosef Bacik /* 302977745c05SJosef Bacik * Our block group might still be attached to the list 303077745c05SJosef Bacik * of new block groups in the transaction handle of some 303177745c05SJosef Bacik * other task (struct btrfs_trans_handle->new_bgs). This 303277745c05SJosef Bacik * means its block group item isn't yet in the extent 303377745c05SJosef Bacik * tree. If this happens ignore the error, as we will 303477745c05SJosef Bacik * try again later in the critical section of the 303577745c05SJosef Bacik * transaction commit. 303677745c05SJosef Bacik */ 303777745c05SJosef Bacik if (ret == -ENOENT) { 303877745c05SJosef Bacik ret = 0; 303977745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 304077745c05SJosef Bacik if (list_empty(&cache->dirty_list)) { 304177745c05SJosef Bacik list_add_tail(&cache->dirty_list, 304277745c05SJosef Bacik &cur_trans->dirty_bgs); 304377745c05SJosef Bacik btrfs_get_block_group(cache); 304477745c05SJosef Bacik drop_reserve = false; 304577745c05SJosef Bacik } 304677745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 304777745c05SJosef Bacik } else if (ret) { 304877745c05SJosef Bacik btrfs_abort_transaction(trans, ret); 304977745c05SJosef Bacik } 305077745c05SJosef Bacik } 305177745c05SJosef Bacik 305277745c05SJosef Bacik /* If it's not on the io list, we need to put the block group */ 305377745c05SJosef Bacik if (should_put) 305477745c05SJosef Bacik btrfs_put_block_group(cache); 305577745c05SJosef Bacik if (drop_reserve) 305677745c05SJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 305777745c05SJosef Bacik /* 305877745c05SJosef Bacik * Avoid blocking other tasks for too long. It might even save 305977745c05SJosef Bacik * us from writing caches for block groups that are going to be 306077745c05SJosef Bacik * removed. 306177745c05SJosef Bacik */ 306277745c05SJosef Bacik mutex_unlock(&trans->transaction->cache_write_mutex); 3063938fcbfbSJosef Bacik if (ret) 3064938fcbfbSJosef Bacik goto out; 306577745c05SJosef Bacik mutex_lock(&trans->transaction->cache_write_mutex); 306677745c05SJosef Bacik } 306777745c05SJosef Bacik mutex_unlock(&trans->transaction->cache_write_mutex); 306877745c05SJosef Bacik 306977745c05SJosef Bacik /* 307077745c05SJosef Bacik * Go through delayed refs for all the stuff we've just kicked off 307177745c05SJosef Bacik * and then loop back (just once) 307277745c05SJosef Bacik */ 307334d1eb0eSJosef Bacik if (!ret) 307477745c05SJosef Bacik ret = btrfs_run_delayed_refs(trans, 0); 307577745c05SJosef Bacik if (!ret && loops == 0) { 307677745c05SJosef Bacik loops++; 307777745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 307877745c05SJosef Bacik list_splice_init(&cur_trans->dirty_bgs, &dirty); 307977745c05SJosef Bacik /* 308077745c05SJosef Bacik * dirty_bgs_lock protects us from concurrent block group 308177745c05SJosef Bacik * deletes too (not just cache_write_mutex). 308277745c05SJosef Bacik */ 308377745c05SJosef Bacik if (!list_empty(&dirty)) { 308477745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 308577745c05SJosef Bacik goto again; 308677745c05SJosef Bacik } 308777745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 3088938fcbfbSJosef Bacik } 3089938fcbfbSJosef Bacik out: 3090938fcbfbSJosef Bacik if (ret < 0) { 3091938fcbfbSJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 3092938fcbfbSJosef Bacik list_splice_init(&dirty, &cur_trans->dirty_bgs); 3093938fcbfbSJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 309477745c05SJosef Bacik btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 309577745c05SJosef Bacik } 309677745c05SJosef Bacik 309777745c05SJosef Bacik btrfs_free_path(path); 309877745c05SJosef Bacik return ret; 309977745c05SJosef Bacik } 310077745c05SJosef Bacik 310177745c05SJosef Bacik int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) 310277745c05SJosef Bacik { 310377745c05SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 310432da5386SDavid Sterba struct btrfs_block_group *cache; 310577745c05SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 310677745c05SJosef Bacik int ret = 0; 310777745c05SJosef Bacik int should_put; 310877745c05SJosef Bacik struct btrfs_path *path; 310977745c05SJosef Bacik struct list_head *io = &cur_trans->io_bgs; 311077745c05SJosef Bacik int num_started = 0; 311177745c05SJosef Bacik 311277745c05SJosef Bacik path = btrfs_alloc_path(); 311377745c05SJosef Bacik if (!path) 311477745c05SJosef Bacik return -ENOMEM; 311577745c05SJosef Bacik 311677745c05SJosef Bacik /* 311777745c05SJosef Bacik * Even though we are in the critical section of the transaction commit, 311877745c05SJosef Bacik * we can still have concurrent tasks adding elements to this 311977745c05SJosef Bacik * transaction's list of dirty block groups. These tasks correspond to 312077745c05SJosef Bacik * endio free space workers started when writeback finishes for a 312177745c05SJosef Bacik * space cache, which run inode.c:btrfs_finish_ordered_io(), and can 312277745c05SJosef Bacik * allocate new block groups as a result of COWing nodes of the root 312377745c05SJosef Bacik * tree when updating the free space inode. The writeback for the space 312477745c05SJosef Bacik * caches is triggered by an earlier call to 312577745c05SJosef Bacik * btrfs_start_dirty_block_groups() and iterations of the following 312677745c05SJosef Bacik * loop. 312777745c05SJosef Bacik * Also we want to do the cache_save_setup first and then run the 312877745c05SJosef Bacik * delayed refs to make sure we have the best chance at doing this all 312977745c05SJosef Bacik * in one shot. 313077745c05SJosef Bacik */ 313177745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 313277745c05SJosef Bacik while (!list_empty(&cur_trans->dirty_bgs)) { 313377745c05SJosef Bacik cache = list_first_entry(&cur_trans->dirty_bgs, 313432da5386SDavid Sterba struct btrfs_block_group, 313577745c05SJosef Bacik dirty_list); 313677745c05SJosef Bacik 313777745c05SJosef Bacik /* 313877745c05SJosef Bacik * This can happen if cache_save_setup re-dirties a block group 313977745c05SJosef Bacik * that is already under IO. Just wait for it to finish and 314077745c05SJosef Bacik * then do it all again 314177745c05SJosef Bacik */ 314277745c05SJosef Bacik if (!list_empty(&cache->io_list)) { 314377745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 314477745c05SJosef Bacik list_del_init(&cache->io_list); 314577745c05SJosef Bacik btrfs_wait_cache_io(trans, cache, path); 314677745c05SJosef Bacik btrfs_put_block_group(cache); 314777745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 314877745c05SJosef Bacik } 314977745c05SJosef Bacik 315077745c05SJosef Bacik /* 315177745c05SJosef Bacik * Don't remove from the dirty list until after we've waited on 315277745c05SJosef Bacik * any pending IO 315377745c05SJosef Bacik */ 315477745c05SJosef Bacik list_del_init(&cache->dirty_list); 315577745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 315677745c05SJosef Bacik should_put = 1; 315777745c05SJosef Bacik 315877745c05SJosef Bacik cache_save_setup(cache, trans, path); 315977745c05SJosef Bacik 316077745c05SJosef Bacik if (!ret) 316177745c05SJosef Bacik ret = btrfs_run_delayed_refs(trans, 316277745c05SJosef Bacik (unsigned long) -1); 316377745c05SJosef Bacik 316477745c05SJosef Bacik if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { 316577745c05SJosef Bacik cache->io_ctl.inode = NULL; 316677745c05SJosef Bacik ret = btrfs_write_out_cache(trans, cache, path); 316777745c05SJosef Bacik if (ret == 0 && cache->io_ctl.inode) { 316877745c05SJosef Bacik num_started++; 316977745c05SJosef Bacik should_put = 0; 317077745c05SJosef Bacik list_add_tail(&cache->io_list, io); 317177745c05SJosef Bacik } else { 317277745c05SJosef Bacik /* 317377745c05SJosef Bacik * If we failed to write the cache, the 317477745c05SJosef Bacik * generation will be bad and life goes on 317577745c05SJosef Bacik */ 317677745c05SJosef Bacik ret = 0; 317777745c05SJosef Bacik } 317877745c05SJosef Bacik } 317977745c05SJosef Bacik if (!ret) { 31803be4d8efSQu Wenruo ret = update_block_group_item(trans, path, cache); 318177745c05SJosef Bacik /* 318277745c05SJosef Bacik * One of the free space endio workers might have 318377745c05SJosef Bacik * created a new block group while updating a free space 318477745c05SJosef Bacik * cache's inode (at inode.c:btrfs_finish_ordered_io()) 318577745c05SJosef Bacik * and hasn't released its transaction handle yet, in 318677745c05SJosef Bacik * which case the new block group is still attached to 318777745c05SJosef Bacik * its transaction handle and its creation has not 318877745c05SJosef Bacik * finished yet (no block group item in the extent tree 318977745c05SJosef Bacik * yet, etc). If this is the case, wait for all free 319077745c05SJosef Bacik * space endio workers to finish and retry. This is a 3191260db43cSRandy Dunlap * very rare case so no need for a more efficient and 319277745c05SJosef Bacik * complex approach. 319377745c05SJosef Bacik */ 319477745c05SJosef Bacik if (ret == -ENOENT) { 319577745c05SJosef Bacik wait_event(cur_trans->writer_wait, 319677745c05SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 31973be4d8efSQu Wenruo ret = update_block_group_item(trans, path, cache); 319877745c05SJosef Bacik } 319977745c05SJosef Bacik if (ret) 320077745c05SJosef Bacik btrfs_abort_transaction(trans, ret); 320177745c05SJosef Bacik } 320277745c05SJosef Bacik 320377745c05SJosef Bacik /* If its not on the io list, we need to put the block group */ 320477745c05SJosef Bacik if (should_put) 320577745c05SJosef Bacik btrfs_put_block_group(cache); 320677745c05SJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 320777745c05SJosef Bacik spin_lock(&cur_trans->dirty_bgs_lock); 320877745c05SJosef Bacik } 320977745c05SJosef Bacik spin_unlock(&cur_trans->dirty_bgs_lock); 321077745c05SJosef Bacik 321177745c05SJosef Bacik /* 321277745c05SJosef Bacik * Refer to the definition of io_bgs member for details why it's safe 321377745c05SJosef Bacik * to use it without any locking 321477745c05SJosef Bacik */ 321577745c05SJosef Bacik while (!list_empty(io)) { 321632da5386SDavid Sterba cache = list_first_entry(io, struct btrfs_block_group, 321777745c05SJosef Bacik io_list); 321877745c05SJosef Bacik list_del_init(&cache->io_list); 321977745c05SJosef Bacik btrfs_wait_cache_io(trans, cache, path); 322077745c05SJosef Bacik btrfs_put_block_group(cache); 322177745c05SJosef Bacik } 322277745c05SJosef Bacik 322377745c05SJosef Bacik btrfs_free_path(path); 322477745c05SJosef Bacik return ret; 322577745c05SJosef Bacik } 3226606d1bf1SJosef Bacik 3227606d1bf1SJosef Bacik int btrfs_update_block_group(struct btrfs_trans_handle *trans, 322811b66fa6SAnand Jain u64 bytenr, u64 num_bytes, bool alloc) 3229606d1bf1SJosef Bacik { 3230606d1bf1SJosef Bacik struct btrfs_fs_info *info = trans->fs_info; 323132da5386SDavid Sterba struct btrfs_block_group *cache = NULL; 3232606d1bf1SJosef Bacik u64 total = num_bytes; 3233606d1bf1SJosef Bacik u64 old_val; 3234606d1bf1SJosef Bacik u64 byte_in_group; 3235606d1bf1SJosef Bacik int factor; 3236606d1bf1SJosef Bacik int ret = 0; 3237606d1bf1SJosef Bacik 3238606d1bf1SJosef Bacik /* Block accounting for super block */ 3239606d1bf1SJosef Bacik spin_lock(&info->delalloc_root_lock); 3240606d1bf1SJosef Bacik old_val = btrfs_super_bytes_used(info->super_copy); 3241606d1bf1SJosef Bacik if (alloc) 3242606d1bf1SJosef Bacik old_val += num_bytes; 3243606d1bf1SJosef Bacik else 3244606d1bf1SJosef Bacik old_val -= num_bytes; 3245606d1bf1SJosef Bacik btrfs_set_super_bytes_used(info->super_copy, old_val); 3246606d1bf1SJosef Bacik spin_unlock(&info->delalloc_root_lock); 3247606d1bf1SJosef Bacik 3248606d1bf1SJosef Bacik while (total) { 3249606d1bf1SJosef Bacik cache = btrfs_lookup_block_group(info, bytenr); 3250606d1bf1SJosef Bacik if (!cache) { 3251606d1bf1SJosef Bacik ret = -ENOENT; 3252606d1bf1SJosef Bacik break; 3253606d1bf1SJosef Bacik } 3254606d1bf1SJosef Bacik factor = btrfs_bg_type_to_factor(cache->flags); 3255606d1bf1SJosef Bacik 3256606d1bf1SJosef Bacik /* 3257606d1bf1SJosef Bacik * If this block group has free space cache written out, we 3258606d1bf1SJosef Bacik * need to make sure to load it if we are removing space. This 3259606d1bf1SJosef Bacik * is because we need the unpinning stage to actually add the 3260606d1bf1SJosef Bacik * space back to the block group, otherwise we will leak space. 3261606d1bf1SJosef Bacik */ 326232da5386SDavid Sterba if (!alloc && !btrfs_block_group_done(cache)) 3263606d1bf1SJosef Bacik btrfs_cache_block_group(cache, 1); 3264606d1bf1SJosef Bacik 3265b3470b5dSDavid Sterba byte_in_group = bytenr - cache->start; 3266b3470b5dSDavid Sterba WARN_ON(byte_in_group > cache->length); 3267606d1bf1SJosef Bacik 3268606d1bf1SJosef Bacik spin_lock(&cache->space_info->lock); 3269606d1bf1SJosef Bacik spin_lock(&cache->lock); 3270606d1bf1SJosef Bacik 3271606d1bf1SJosef Bacik if (btrfs_test_opt(info, SPACE_CACHE) && 3272606d1bf1SJosef Bacik cache->disk_cache_state < BTRFS_DC_CLEAR) 3273606d1bf1SJosef Bacik cache->disk_cache_state = BTRFS_DC_CLEAR; 3274606d1bf1SJosef Bacik 3275bf38be65SDavid Sterba old_val = cache->used; 3276b3470b5dSDavid Sterba num_bytes = min(total, cache->length - byte_in_group); 3277606d1bf1SJosef Bacik if (alloc) { 3278606d1bf1SJosef Bacik old_val += num_bytes; 3279bf38be65SDavid Sterba cache->used = old_val; 3280606d1bf1SJosef Bacik cache->reserved -= num_bytes; 3281606d1bf1SJosef Bacik cache->space_info->bytes_reserved -= num_bytes; 3282606d1bf1SJosef Bacik cache->space_info->bytes_used += num_bytes; 3283606d1bf1SJosef Bacik cache->space_info->disk_used += num_bytes * factor; 3284606d1bf1SJosef Bacik spin_unlock(&cache->lock); 3285606d1bf1SJosef Bacik spin_unlock(&cache->space_info->lock); 3286606d1bf1SJosef Bacik } else { 3287606d1bf1SJosef Bacik old_val -= num_bytes; 3288bf38be65SDavid Sterba cache->used = old_val; 3289606d1bf1SJosef Bacik cache->pinned += num_bytes; 3290606d1bf1SJosef Bacik btrfs_space_info_update_bytes_pinned(info, 3291606d1bf1SJosef Bacik cache->space_info, num_bytes); 3292606d1bf1SJosef Bacik cache->space_info->bytes_used -= num_bytes; 3293606d1bf1SJosef Bacik cache->space_info->disk_used -= num_bytes * factor; 3294606d1bf1SJosef Bacik spin_unlock(&cache->lock); 3295606d1bf1SJosef Bacik spin_unlock(&cache->space_info->lock); 3296606d1bf1SJosef Bacik 3297fe119a6eSNikolay Borisov set_extent_dirty(&trans->transaction->pinned_extents, 3298606d1bf1SJosef Bacik bytenr, bytenr + num_bytes - 1, 3299606d1bf1SJosef Bacik GFP_NOFS | __GFP_NOFAIL); 3300606d1bf1SJosef Bacik } 3301606d1bf1SJosef Bacik 3302606d1bf1SJosef Bacik spin_lock(&trans->transaction->dirty_bgs_lock); 3303606d1bf1SJosef Bacik if (list_empty(&cache->dirty_list)) { 3304606d1bf1SJosef Bacik list_add_tail(&cache->dirty_list, 3305606d1bf1SJosef Bacik &trans->transaction->dirty_bgs); 3306606d1bf1SJosef Bacik trans->delayed_ref_updates++; 3307606d1bf1SJosef Bacik btrfs_get_block_group(cache); 3308606d1bf1SJosef Bacik } 3309606d1bf1SJosef Bacik spin_unlock(&trans->transaction->dirty_bgs_lock); 3310606d1bf1SJosef Bacik 3311606d1bf1SJosef Bacik /* 3312606d1bf1SJosef Bacik * No longer have used bytes in this block group, queue it for 3313606d1bf1SJosef Bacik * deletion. We do this after adding the block group to the 3314606d1bf1SJosef Bacik * dirty list to avoid races between cleaner kthread and space 3315606d1bf1SJosef Bacik * cache writeout. 3316606d1bf1SJosef Bacik */ 33176e80d4f8SDennis Zhou if (!alloc && old_val == 0) { 33186e80d4f8SDennis Zhou if (!btrfs_test_opt(info, DISCARD_ASYNC)) 3319606d1bf1SJosef Bacik btrfs_mark_bg_unused(cache); 33206e80d4f8SDennis Zhou } 3321606d1bf1SJosef Bacik 3322606d1bf1SJosef Bacik btrfs_put_block_group(cache); 3323606d1bf1SJosef Bacik total -= num_bytes; 3324606d1bf1SJosef Bacik bytenr += num_bytes; 3325606d1bf1SJosef Bacik } 3326606d1bf1SJosef Bacik 3327606d1bf1SJosef Bacik /* Modified block groups are accounted for in the delayed_refs_rsv. */ 3328606d1bf1SJosef Bacik btrfs_update_delayed_refs_rsv(trans); 3329606d1bf1SJosef Bacik return ret; 3330606d1bf1SJosef Bacik } 3331606d1bf1SJosef Bacik 3332606d1bf1SJosef Bacik /** 3333606d1bf1SJosef Bacik * btrfs_add_reserved_bytes - update the block_group and space info counters 3334606d1bf1SJosef Bacik * @cache: The cache we are manipulating 3335606d1bf1SJosef Bacik * @ram_bytes: The number of bytes of file content, and will be same to 3336606d1bf1SJosef Bacik * @num_bytes except for the compress path. 3337606d1bf1SJosef Bacik * @num_bytes: The number of bytes in question 3338606d1bf1SJosef Bacik * @delalloc: The blocks are allocated for the delalloc write 3339606d1bf1SJosef Bacik * 3340606d1bf1SJosef Bacik * This is called by the allocator when it reserves space. If this is a 3341606d1bf1SJosef Bacik * reservation and the block group has become read only we cannot make the 3342606d1bf1SJosef Bacik * reservation and return -EAGAIN, otherwise this function always succeeds. 3343606d1bf1SJosef Bacik */ 334432da5386SDavid Sterba int btrfs_add_reserved_bytes(struct btrfs_block_group *cache, 3345606d1bf1SJosef Bacik u64 ram_bytes, u64 num_bytes, int delalloc) 3346606d1bf1SJosef Bacik { 3347606d1bf1SJosef Bacik struct btrfs_space_info *space_info = cache->space_info; 3348606d1bf1SJosef Bacik int ret = 0; 3349606d1bf1SJosef Bacik 3350606d1bf1SJosef Bacik spin_lock(&space_info->lock); 3351606d1bf1SJosef Bacik spin_lock(&cache->lock); 3352606d1bf1SJosef Bacik if (cache->ro) { 3353606d1bf1SJosef Bacik ret = -EAGAIN; 3354606d1bf1SJosef Bacik } else { 3355606d1bf1SJosef Bacik cache->reserved += num_bytes; 3356606d1bf1SJosef Bacik space_info->bytes_reserved += num_bytes; 3357a43c3835SJosef Bacik trace_btrfs_space_reservation(cache->fs_info, "space_info", 3358a43c3835SJosef Bacik space_info->flags, num_bytes, 1); 3359606d1bf1SJosef Bacik btrfs_space_info_update_bytes_may_use(cache->fs_info, 3360606d1bf1SJosef Bacik space_info, -ram_bytes); 3361606d1bf1SJosef Bacik if (delalloc) 3362606d1bf1SJosef Bacik cache->delalloc_bytes += num_bytes; 336399ffb43eSJosef Bacik 336499ffb43eSJosef Bacik /* 336599ffb43eSJosef Bacik * Compression can use less space than we reserved, so wake 336699ffb43eSJosef Bacik * tickets if that happens 336799ffb43eSJosef Bacik */ 336899ffb43eSJosef Bacik if (num_bytes < ram_bytes) 336999ffb43eSJosef Bacik btrfs_try_granting_tickets(cache->fs_info, space_info); 3370606d1bf1SJosef Bacik } 3371606d1bf1SJosef Bacik spin_unlock(&cache->lock); 3372606d1bf1SJosef Bacik spin_unlock(&space_info->lock); 3373606d1bf1SJosef Bacik return ret; 3374606d1bf1SJosef Bacik } 3375606d1bf1SJosef Bacik 3376606d1bf1SJosef Bacik /** 3377606d1bf1SJosef Bacik * btrfs_free_reserved_bytes - update the block_group and space info counters 3378606d1bf1SJosef Bacik * @cache: The cache we are manipulating 3379606d1bf1SJosef Bacik * @num_bytes: The number of bytes in question 3380606d1bf1SJosef Bacik * @delalloc: The blocks are allocated for the delalloc write 3381606d1bf1SJosef Bacik * 3382606d1bf1SJosef Bacik * This is called by somebody who is freeing space that was never actually used 3383606d1bf1SJosef Bacik * on disk. For example if you reserve some space for a new leaf in transaction 3384606d1bf1SJosef Bacik * A and before transaction A commits you free that leaf, you call this with 3385606d1bf1SJosef Bacik * reserve set to 0 in order to clear the reservation. 3386606d1bf1SJosef Bacik */ 338732da5386SDavid Sterba void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, 3388606d1bf1SJosef Bacik u64 num_bytes, int delalloc) 3389606d1bf1SJosef Bacik { 3390606d1bf1SJosef Bacik struct btrfs_space_info *space_info = cache->space_info; 3391606d1bf1SJosef Bacik 3392606d1bf1SJosef Bacik spin_lock(&space_info->lock); 3393606d1bf1SJosef Bacik spin_lock(&cache->lock); 3394606d1bf1SJosef Bacik if (cache->ro) 3395606d1bf1SJosef Bacik space_info->bytes_readonly += num_bytes; 3396606d1bf1SJosef Bacik cache->reserved -= num_bytes; 3397606d1bf1SJosef Bacik space_info->bytes_reserved -= num_bytes; 3398606d1bf1SJosef Bacik space_info->max_extent_size = 0; 3399606d1bf1SJosef Bacik 3400606d1bf1SJosef Bacik if (delalloc) 3401606d1bf1SJosef Bacik cache->delalloc_bytes -= num_bytes; 3402606d1bf1SJosef Bacik spin_unlock(&cache->lock); 34033308234aSJosef Bacik 34043308234aSJosef Bacik btrfs_try_granting_tickets(cache->fs_info, space_info); 3405606d1bf1SJosef Bacik spin_unlock(&space_info->lock); 3406606d1bf1SJosef Bacik } 340707730d87SJosef Bacik 340807730d87SJosef Bacik static void force_metadata_allocation(struct btrfs_fs_info *info) 340907730d87SJosef Bacik { 341007730d87SJosef Bacik struct list_head *head = &info->space_info; 341107730d87SJosef Bacik struct btrfs_space_info *found; 341207730d87SJosef Bacik 341372804905SJosef Bacik list_for_each_entry(found, head, list) { 341407730d87SJosef Bacik if (found->flags & BTRFS_BLOCK_GROUP_METADATA) 341507730d87SJosef Bacik found->force_alloc = CHUNK_ALLOC_FORCE; 341607730d87SJosef Bacik } 341707730d87SJosef Bacik } 341807730d87SJosef Bacik 341907730d87SJosef Bacik static int should_alloc_chunk(struct btrfs_fs_info *fs_info, 342007730d87SJosef Bacik struct btrfs_space_info *sinfo, int force) 342107730d87SJosef Bacik { 342207730d87SJosef Bacik u64 bytes_used = btrfs_space_info_used(sinfo, false); 342307730d87SJosef Bacik u64 thresh; 342407730d87SJosef Bacik 342507730d87SJosef Bacik if (force == CHUNK_ALLOC_FORCE) 342607730d87SJosef Bacik return 1; 342707730d87SJosef Bacik 342807730d87SJosef Bacik /* 342907730d87SJosef Bacik * in limited mode, we want to have some free space up to 343007730d87SJosef Bacik * about 1% of the FS size. 343107730d87SJosef Bacik */ 343207730d87SJosef Bacik if (force == CHUNK_ALLOC_LIMITED) { 343307730d87SJosef Bacik thresh = btrfs_super_total_bytes(fs_info->super_copy); 343407730d87SJosef Bacik thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1)); 343507730d87SJosef Bacik 343607730d87SJosef Bacik if (sinfo->total_bytes - bytes_used < thresh) 343707730d87SJosef Bacik return 1; 343807730d87SJosef Bacik } 343907730d87SJosef Bacik 344007730d87SJosef Bacik if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8)) 344107730d87SJosef Bacik return 0; 344207730d87SJosef Bacik return 1; 344307730d87SJosef Bacik } 344407730d87SJosef Bacik 344507730d87SJosef Bacik int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type) 344607730d87SJosef Bacik { 344707730d87SJosef Bacik u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type); 344807730d87SJosef Bacik 344907730d87SJosef Bacik return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE); 345007730d87SJosef Bacik } 345107730d87SJosef Bacik 345279bd3712SFilipe Manana static int do_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags) 345379bd3712SFilipe Manana { 345479bd3712SFilipe Manana struct btrfs_block_group *bg; 345579bd3712SFilipe Manana int ret; 345679bd3712SFilipe Manana 345707730d87SJosef Bacik /* 345879bd3712SFilipe Manana * Check if we have enough space in the system space info because we 345979bd3712SFilipe Manana * will need to update device items in the chunk btree and insert a new 346079bd3712SFilipe Manana * chunk item in the chunk btree as well. This will allocate a new 346179bd3712SFilipe Manana * system block group if needed. 346279bd3712SFilipe Manana */ 346379bd3712SFilipe Manana check_system_chunk(trans, flags); 346479bd3712SFilipe Manana 3465f6f39f7aSNikolay Borisov bg = btrfs_create_chunk(trans, flags); 346679bd3712SFilipe Manana if (IS_ERR(bg)) { 346779bd3712SFilipe Manana ret = PTR_ERR(bg); 346879bd3712SFilipe Manana goto out; 346979bd3712SFilipe Manana } 347079bd3712SFilipe Manana 347179bd3712SFilipe Manana ret = btrfs_chunk_alloc_add_chunk_item(trans, bg); 347279bd3712SFilipe Manana /* 347379bd3712SFilipe Manana * Normally we are not expected to fail with -ENOSPC here, since we have 347479bd3712SFilipe Manana * previously reserved space in the system space_info and allocated one 3475ecd84d54SFilipe Manana * new system chunk if necessary. However there are three exceptions: 347679bd3712SFilipe Manana * 347779bd3712SFilipe Manana * 1) We may have enough free space in the system space_info but all the 347879bd3712SFilipe Manana * existing system block groups have a profile which can not be used 347979bd3712SFilipe Manana * for extent allocation. 348079bd3712SFilipe Manana * 348179bd3712SFilipe Manana * This happens when mounting in degraded mode. For example we have a 348279bd3712SFilipe Manana * RAID1 filesystem with 2 devices, lose one device and mount the fs 348379bd3712SFilipe Manana * using the other device in degraded mode. If we then allocate a chunk, 348479bd3712SFilipe Manana * we may have enough free space in the existing system space_info, but 348579bd3712SFilipe Manana * none of the block groups can be used for extent allocation since they 348679bd3712SFilipe Manana * have a RAID1 profile, and because we are in degraded mode with a 348779bd3712SFilipe Manana * single device, we are forced to allocate a new system chunk with a 348879bd3712SFilipe Manana * SINGLE profile. Making check_system_chunk() iterate over all system 348979bd3712SFilipe Manana * block groups and check if they have a usable profile and enough space 349079bd3712SFilipe Manana * can be slow on very large filesystems, so we tolerate the -ENOSPC and 349179bd3712SFilipe Manana * try again after forcing allocation of a new system chunk. Like this 349279bd3712SFilipe Manana * we avoid paying the cost of that search in normal circumstances, when 349379bd3712SFilipe Manana * we were not mounted in degraded mode; 349479bd3712SFilipe Manana * 349579bd3712SFilipe Manana * 2) We had enough free space info the system space_info, and one suitable 349679bd3712SFilipe Manana * block group to allocate from when we called check_system_chunk() 349779bd3712SFilipe Manana * above. However right after we called it, the only system block group 349879bd3712SFilipe Manana * with enough free space got turned into RO mode by a running scrub, 349979bd3712SFilipe Manana * and in this case we have to allocate a new one and retry. We only 350079bd3712SFilipe Manana * need do this allocate and retry once, since we have a transaction 3501ecd84d54SFilipe Manana * handle and scrub uses the commit root to search for block groups; 3502ecd84d54SFilipe Manana * 3503ecd84d54SFilipe Manana * 3) We had one system block group with enough free space when we called 3504ecd84d54SFilipe Manana * check_system_chunk(), but after that, right before we tried to 3505ecd84d54SFilipe Manana * allocate the last extent buffer we needed, a discard operation came 3506ecd84d54SFilipe Manana * in and it temporarily removed the last free space entry from the 3507ecd84d54SFilipe Manana * block group (discard removes a free space entry, discards it, and 3508ecd84d54SFilipe Manana * then adds back the entry to the block group cache). 350979bd3712SFilipe Manana */ 351079bd3712SFilipe Manana if (ret == -ENOSPC) { 351179bd3712SFilipe Manana const u64 sys_flags = btrfs_system_alloc_profile(trans->fs_info); 351279bd3712SFilipe Manana struct btrfs_block_group *sys_bg; 351379bd3712SFilipe Manana 3514f6f39f7aSNikolay Borisov sys_bg = btrfs_create_chunk(trans, sys_flags); 351579bd3712SFilipe Manana if (IS_ERR(sys_bg)) { 351679bd3712SFilipe Manana ret = PTR_ERR(sys_bg); 351779bd3712SFilipe Manana btrfs_abort_transaction(trans, ret); 351879bd3712SFilipe Manana goto out; 351979bd3712SFilipe Manana } 352079bd3712SFilipe Manana 352179bd3712SFilipe Manana ret = btrfs_chunk_alloc_add_chunk_item(trans, sys_bg); 352279bd3712SFilipe Manana if (ret) { 352379bd3712SFilipe Manana btrfs_abort_transaction(trans, ret); 352479bd3712SFilipe Manana goto out; 352579bd3712SFilipe Manana } 352679bd3712SFilipe Manana 352779bd3712SFilipe Manana ret = btrfs_chunk_alloc_add_chunk_item(trans, bg); 352879bd3712SFilipe Manana if (ret) { 352979bd3712SFilipe Manana btrfs_abort_transaction(trans, ret); 353079bd3712SFilipe Manana goto out; 353179bd3712SFilipe Manana } 353279bd3712SFilipe Manana } else if (ret) { 353379bd3712SFilipe Manana btrfs_abort_transaction(trans, ret); 353479bd3712SFilipe Manana goto out; 353579bd3712SFilipe Manana } 353679bd3712SFilipe Manana out: 353779bd3712SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 353879bd3712SFilipe Manana 353979bd3712SFilipe Manana return ret; 354079bd3712SFilipe Manana } 354179bd3712SFilipe Manana 354279bd3712SFilipe Manana /* 354379bd3712SFilipe Manana * Chunk allocation is done in 2 phases: 354479bd3712SFilipe Manana * 354579bd3712SFilipe Manana * 1) Phase 1 - through btrfs_chunk_alloc() we allocate device extents for 354679bd3712SFilipe Manana * the chunk, the chunk mapping, create its block group and add the items 354779bd3712SFilipe Manana * that belong in the chunk btree to it - more specifically, we need to 354879bd3712SFilipe Manana * update device items in the chunk btree and add a new chunk item to it. 354979bd3712SFilipe Manana * 355079bd3712SFilipe Manana * 2) Phase 2 - through btrfs_create_pending_block_groups(), we add the block 355179bd3712SFilipe Manana * group item to the extent btree and the device extent items to the devices 355279bd3712SFilipe Manana * btree. 355379bd3712SFilipe Manana * 355479bd3712SFilipe Manana * This is done to prevent deadlocks. For example when COWing a node from the 355579bd3712SFilipe Manana * extent btree we are holding a write lock on the node's parent and if we 355679bd3712SFilipe Manana * trigger chunk allocation and attempted to insert the new block group item 355779bd3712SFilipe Manana * in the extent btree right way, we could deadlock because the path for the 355879bd3712SFilipe Manana * insertion can include that parent node. At first glance it seems impossible 355979bd3712SFilipe Manana * to trigger chunk allocation after starting a transaction since tasks should 356079bd3712SFilipe Manana * reserve enough transaction units (metadata space), however while that is true 356179bd3712SFilipe Manana * most of the time, chunk allocation may still be triggered for several reasons: 356279bd3712SFilipe Manana * 356379bd3712SFilipe Manana * 1) When reserving metadata, we check if there is enough free space in the 356479bd3712SFilipe Manana * metadata space_info and therefore don't trigger allocation of a new chunk. 356579bd3712SFilipe Manana * However later when the task actually tries to COW an extent buffer from 356679bd3712SFilipe Manana * the extent btree or from the device btree for example, it is forced to 356779bd3712SFilipe Manana * allocate a new block group (chunk) because the only one that had enough 356879bd3712SFilipe Manana * free space was just turned to RO mode by a running scrub for example (or 356979bd3712SFilipe Manana * device replace, block group reclaim thread, etc), so we can not use it 357079bd3712SFilipe Manana * for allocating an extent and end up being forced to allocate a new one; 357179bd3712SFilipe Manana * 357279bd3712SFilipe Manana * 2) Because we only check that the metadata space_info has enough free bytes, 357379bd3712SFilipe Manana * we end up not allocating a new metadata chunk in that case. However if 357479bd3712SFilipe Manana * the filesystem was mounted in degraded mode, none of the existing block 357579bd3712SFilipe Manana * groups might be suitable for extent allocation due to their incompatible 357679bd3712SFilipe Manana * profile (for e.g. mounting a 2 devices filesystem, where all block groups 357779bd3712SFilipe Manana * use a RAID1 profile, in degraded mode using a single device). In this case 357879bd3712SFilipe Manana * when the task attempts to COW some extent buffer of the extent btree for 357979bd3712SFilipe Manana * example, it will trigger allocation of a new metadata block group with a 358079bd3712SFilipe Manana * suitable profile (SINGLE profile in the example of the degraded mount of 358179bd3712SFilipe Manana * the RAID1 filesystem); 358279bd3712SFilipe Manana * 358379bd3712SFilipe Manana * 3) The task has reserved enough transaction units / metadata space, but when 358479bd3712SFilipe Manana * it attempts to COW an extent buffer from the extent or device btree for 358579bd3712SFilipe Manana * example, it does not find any free extent in any metadata block group, 358679bd3712SFilipe Manana * therefore forced to try to allocate a new metadata block group. 358779bd3712SFilipe Manana * This is because some other task allocated all available extents in the 358879bd3712SFilipe Manana * meanwhile - this typically happens with tasks that don't reserve space 358979bd3712SFilipe Manana * properly, either intentionally or as a bug. One example where this is 359079bd3712SFilipe Manana * done intentionally is fsync, as it does not reserve any transaction units 359179bd3712SFilipe Manana * and ends up allocating a variable number of metadata extents for log 3592ecd84d54SFilipe Manana * tree extent buffers; 3593ecd84d54SFilipe Manana * 3594ecd84d54SFilipe Manana * 4) The task has reserved enough transaction units / metadata space, but right 3595ecd84d54SFilipe Manana * before it tries to allocate the last extent buffer it needs, a discard 3596ecd84d54SFilipe Manana * operation comes in and, temporarily, removes the last free space entry from 3597ecd84d54SFilipe Manana * the only metadata block group that had free space (discard starts by 3598ecd84d54SFilipe Manana * removing a free space entry from a block group, then does the discard 3599ecd84d54SFilipe Manana * operation and, once it's done, it adds back the free space entry to the 3600ecd84d54SFilipe Manana * block group). 360179bd3712SFilipe Manana * 360279bd3712SFilipe Manana * We also need this 2 phases setup when adding a device to a filesystem with 360379bd3712SFilipe Manana * a seed device - we must create new metadata and system chunks without adding 360479bd3712SFilipe Manana * any of the block group items to the chunk, extent and device btrees. If we 360579bd3712SFilipe Manana * did not do it this way, we would get ENOSPC when attempting to update those 360679bd3712SFilipe Manana * btrees, since all the chunks from the seed device are read-only. 360779bd3712SFilipe Manana * 360879bd3712SFilipe Manana * Phase 1 does the updates and insertions to the chunk btree because if we had 360979bd3712SFilipe Manana * it done in phase 2 and have a thundering herd of tasks allocating chunks in 361079bd3712SFilipe Manana * parallel, we risk having too many system chunks allocated by many tasks if 361179bd3712SFilipe Manana * many tasks reach phase 1 without the previous ones completing phase 2. In the 361279bd3712SFilipe Manana * extreme case this leads to exhaustion of the system chunk array in the 361379bd3712SFilipe Manana * superblock. This is easier to trigger if using a btree node/leaf size of 64K 361479bd3712SFilipe Manana * and with RAID filesystems (so we have more device items in the chunk btree). 361579bd3712SFilipe Manana * This has happened before and commit eafa4fd0ad0607 ("btrfs: fix exhaustion of 361679bd3712SFilipe Manana * the system chunk array due to concurrent allocations") provides more details. 361779bd3712SFilipe Manana * 36182bb2e00eSFilipe Manana * Allocation of system chunks does not happen through this function. A task that 36192bb2e00eSFilipe Manana * needs to update the chunk btree (the only btree that uses system chunks), must 36202bb2e00eSFilipe Manana * preallocate chunk space by calling either check_system_chunk() or 36212bb2e00eSFilipe Manana * btrfs_reserve_chunk_metadata() - the former is used when allocating a data or 36222bb2e00eSFilipe Manana * metadata chunk or when removing a chunk, while the later is used before doing 36232bb2e00eSFilipe Manana * a modification to the chunk btree - use cases for the later are adding, 36242bb2e00eSFilipe Manana * removing and resizing a device as well as relocation of a system chunk. 36252bb2e00eSFilipe Manana * See the comment below for more details. 362679bd3712SFilipe Manana * 362779bd3712SFilipe Manana * The reservation of system space, done through check_system_chunk(), as well 362879bd3712SFilipe Manana * as all the updates and insertions into the chunk btree must be done while 362979bd3712SFilipe Manana * holding fs_info->chunk_mutex. This is important to guarantee that while COWing 363079bd3712SFilipe Manana * an extent buffer from the chunks btree we never trigger allocation of a new 363179bd3712SFilipe Manana * system chunk, which would result in a deadlock (trying to lock twice an 363279bd3712SFilipe Manana * extent buffer of the chunk btree, first time before triggering the chunk 363379bd3712SFilipe Manana * allocation and the second time during chunk allocation while attempting to 363479bd3712SFilipe Manana * update the chunks btree). The system chunk array is also updated while holding 363579bd3712SFilipe Manana * that mutex. The same logic applies to removing chunks - we must reserve system 363679bd3712SFilipe Manana * space, update the chunk btree and the system chunk array in the superblock 363779bd3712SFilipe Manana * while holding fs_info->chunk_mutex. 363879bd3712SFilipe Manana * 363979bd3712SFilipe Manana * This function, btrfs_chunk_alloc(), belongs to phase 1. 364079bd3712SFilipe Manana * 364179bd3712SFilipe Manana * If @force is CHUNK_ALLOC_FORCE: 364207730d87SJosef Bacik * - return 1 if it successfully allocates a chunk, 364307730d87SJosef Bacik * - return errors including -ENOSPC otherwise. 364479bd3712SFilipe Manana * If @force is NOT CHUNK_ALLOC_FORCE: 364507730d87SJosef Bacik * - return 0 if it doesn't need to allocate a new chunk, 364607730d87SJosef Bacik * - return 1 if it successfully allocates a chunk, 364707730d87SJosef Bacik * - return errors including -ENOSPC otherwise. 364807730d87SJosef Bacik */ 364907730d87SJosef Bacik int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags, 365007730d87SJosef Bacik enum btrfs_chunk_alloc_enum force) 365107730d87SJosef Bacik { 365207730d87SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 365307730d87SJosef Bacik struct btrfs_space_info *space_info; 365407730d87SJosef Bacik bool wait_for_alloc = false; 365507730d87SJosef Bacik bool should_alloc = false; 365607730d87SJosef Bacik int ret = 0; 365707730d87SJosef Bacik 365807730d87SJosef Bacik /* Don't re-enter if we're already allocating a chunk */ 365907730d87SJosef Bacik if (trans->allocating_chunk) 366007730d87SJosef Bacik return -ENOSPC; 366179bd3712SFilipe Manana /* 36622bb2e00eSFilipe Manana * Allocation of system chunks can not happen through this path, as we 36632bb2e00eSFilipe Manana * could end up in a deadlock if we are allocating a data or metadata 36642bb2e00eSFilipe Manana * chunk and there is another task modifying the chunk btree. 36652bb2e00eSFilipe Manana * 36662bb2e00eSFilipe Manana * This is because while we are holding the chunk mutex, we will attempt 36672bb2e00eSFilipe Manana * to add the new chunk item to the chunk btree or update an existing 36682bb2e00eSFilipe Manana * device item in the chunk btree, while the other task that is modifying 36692bb2e00eSFilipe Manana * the chunk btree is attempting to COW an extent buffer while holding a 36702bb2e00eSFilipe Manana * lock on it and on its parent - if the COW operation triggers a system 36712bb2e00eSFilipe Manana * chunk allocation, then we can deadlock because we are holding the 36722bb2e00eSFilipe Manana * chunk mutex and we may need to access that extent buffer or its parent 36732bb2e00eSFilipe Manana * in order to add the chunk item or update a device item. 36742bb2e00eSFilipe Manana * 36752bb2e00eSFilipe Manana * Tasks that want to modify the chunk tree should reserve system space 36762bb2e00eSFilipe Manana * before updating the chunk btree, by calling either 36772bb2e00eSFilipe Manana * btrfs_reserve_chunk_metadata() or check_system_chunk(). 36782bb2e00eSFilipe Manana * It's possible that after a task reserves the space, it still ends up 36792bb2e00eSFilipe Manana * here - this happens in the cases described above at do_chunk_alloc(). 36802bb2e00eSFilipe Manana * The task will have to either retry or fail. 368179bd3712SFilipe Manana */ 36822bb2e00eSFilipe Manana if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 368379bd3712SFilipe Manana return -ENOSPC; 368407730d87SJosef Bacik 368507730d87SJosef Bacik space_info = btrfs_find_space_info(fs_info, flags); 368607730d87SJosef Bacik ASSERT(space_info); 368707730d87SJosef Bacik 368807730d87SJosef Bacik do { 368907730d87SJosef Bacik spin_lock(&space_info->lock); 369007730d87SJosef Bacik if (force < space_info->force_alloc) 369107730d87SJosef Bacik force = space_info->force_alloc; 369207730d87SJosef Bacik should_alloc = should_alloc_chunk(fs_info, space_info, force); 369307730d87SJosef Bacik if (space_info->full) { 369407730d87SJosef Bacik /* No more free physical space */ 369507730d87SJosef Bacik if (should_alloc) 369607730d87SJosef Bacik ret = -ENOSPC; 369707730d87SJosef Bacik else 369807730d87SJosef Bacik ret = 0; 369907730d87SJosef Bacik spin_unlock(&space_info->lock); 370007730d87SJosef Bacik return ret; 370107730d87SJosef Bacik } else if (!should_alloc) { 370207730d87SJosef Bacik spin_unlock(&space_info->lock); 370307730d87SJosef Bacik return 0; 370407730d87SJosef Bacik } else if (space_info->chunk_alloc) { 370507730d87SJosef Bacik /* 370607730d87SJosef Bacik * Someone is already allocating, so we need to block 370707730d87SJosef Bacik * until this someone is finished and then loop to 370807730d87SJosef Bacik * recheck if we should continue with our allocation 370907730d87SJosef Bacik * attempt. 371007730d87SJosef Bacik */ 371107730d87SJosef Bacik wait_for_alloc = true; 371207730d87SJosef Bacik spin_unlock(&space_info->lock); 371307730d87SJosef Bacik mutex_lock(&fs_info->chunk_mutex); 371407730d87SJosef Bacik mutex_unlock(&fs_info->chunk_mutex); 371507730d87SJosef Bacik } else { 371607730d87SJosef Bacik /* Proceed with allocation */ 371707730d87SJosef Bacik space_info->chunk_alloc = 1; 371807730d87SJosef Bacik wait_for_alloc = false; 371907730d87SJosef Bacik spin_unlock(&space_info->lock); 372007730d87SJosef Bacik } 372107730d87SJosef Bacik 372207730d87SJosef Bacik cond_resched(); 372307730d87SJosef Bacik } while (wait_for_alloc); 372407730d87SJosef Bacik 372507730d87SJosef Bacik mutex_lock(&fs_info->chunk_mutex); 372607730d87SJosef Bacik trans->allocating_chunk = true; 372707730d87SJosef Bacik 372807730d87SJosef Bacik /* 372907730d87SJosef Bacik * If we have mixed data/metadata chunks we want to make sure we keep 373007730d87SJosef Bacik * allocating mixed chunks instead of individual chunks. 373107730d87SJosef Bacik */ 373207730d87SJosef Bacik if (btrfs_mixed_space_info(space_info)) 373307730d87SJosef Bacik flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); 373407730d87SJosef Bacik 373507730d87SJosef Bacik /* 373607730d87SJosef Bacik * if we're doing a data chunk, go ahead and make sure that 373707730d87SJosef Bacik * we keep a reasonable number of metadata chunks allocated in the 373807730d87SJosef Bacik * FS as well. 373907730d87SJosef Bacik */ 374007730d87SJosef Bacik if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { 374107730d87SJosef Bacik fs_info->data_chunk_allocations++; 374207730d87SJosef Bacik if (!(fs_info->data_chunk_allocations % 374307730d87SJosef Bacik fs_info->metadata_ratio)) 374407730d87SJosef Bacik force_metadata_allocation(fs_info); 374507730d87SJosef Bacik } 374607730d87SJosef Bacik 374779bd3712SFilipe Manana ret = do_chunk_alloc(trans, flags); 374807730d87SJosef Bacik trans->allocating_chunk = false; 374907730d87SJosef Bacik 375007730d87SJosef Bacik spin_lock(&space_info->lock); 375107730d87SJosef Bacik if (ret < 0) { 375207730d87SJosef Bacik if (ret == -ENOSPC) 375307730d87SJosef Bacik space_info->full = 1; 375407730d87SJosef Bacik else 375507730d87SJosef Bacik goto out; 375607730d87SJosef Bacik } else { 375707730d87SJosef Bacik ret = 1; 375807730d87SJosef Bacik space_info->max_extent_size = 0; 375907730d87SJosef Bacik } 376007730d87SJosef Bacik 376107730d87SJosef Bacik space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; 376207730d87SJosef Bacik out: 376307730d87SJosef Bacik space_info->chunk_alloc = 0; 376407730d87SJosef Bacik spin_unlock(&space_info->lock); 376507730d87SJosef Bacik mutex_unlock(&fs_info->chunk_mutex); 376607730d87SJosef Bacik 376707730d87SJosef Bacik return ret; 376807730d87SJosef Bacik } 376907730d87SJosef Bacik 377007730d87SJosef Bacik static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type) 377107730d87SJosef Bacik { 377207730d87SJosef Bacik u64 num_dev; 377307730d87SJosef Bacik 377407730d87SJosef Bacik num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max; 377507730d87SJosef Bacik if (!num_dev) 377607730d87SJosef Bacik num_dev = fs_info->fs_devices->rw_devices; 377707730d87SJosef Bacik 377807730d87SJosef Bacik return num_dev; 377907730d87SJosef Bacik } 378007730d87SJosef Bacik 37812bb2e00eSFilipe Manana static void reserve_chunk_space(struct btrfs_trans_handle *trans, 37822bb2e00eSFilipe Manana u64 bytes, 37832bb2e00eSFilipe Manana u64 type) 378407730d87SJosef Bacik { 378507730d87SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 378607730d87SJosef Bacik struct btrfs_space_info *info; 378707730d87SJosef Bacik u64 left; 378807730d87SJosef Bacik int ret = 0; 378907730d87SJosef Bacik 379007730d87SJosef Bacik /* 379107730d87SJosef Bacik * Needed because we can end up allocating a system chunk and for an 379207730d87SJosef Bacik * atomic and race free space reservation in the chunk block reserve. 379307730d87SJosef Bacik */ 379407730d87SJosef Bacik lockdep_assert_held(&fs_info->chunk_mutex); 379507730d87SJosef Bacik 379607730d87SJosef Bacik info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); 379707730d87SJosef Bacik spin_lock(&info->lock); 379807730d87SJosef Bacik left = info->total_bytes - btrfs_space_info_used(info, true); 379907730d87SJosef Bacik spin_unlock(&info->lock); 380007730d87SJosef Bacik 38012bb2e00eSFilipe Manana if (left < bytes && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { 380207730d87SJosef Bacik btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu", 38032bb2e00eSFilipe Manana left, bytes, type); 380407730d87SJosef Bacik btrfs_dump_space_info(fs_info, info, 0, 0); 380507730d87SJosef Bacik } 380607730d87SJosef Bacik 38072bb2e00eSFilipe Manana if (left < bytes) { 380807730d87SJosef Bacik u64 flags = btrfs_system_alloc_profile(fs_info); 380979bd3712SFilipe Manana struct btrfs_block_group *bg; 381007730d87SJosef Bacik 381107730d87SJosef Bacik /* 381207730d87SJosef Bacik * Ignore failure to create system chunk. We might end up not 381307730d87SJosef Bacik * needing it, as we might not need to COW all nodes/leafs from 381407730d87SJosef Bacik * the paths we visit in the chunk tree (they were already COWed 381507730d87SJosef Bacik * or created in the current transaction for example). 381607730d87SJosef Bacik */ 3817f6f39f7aSNikolay Borisov bg = btrfs_create_chunk(trans, flags); 381879bd3712SFilipe Manana if (IS_ERR(bg)) { 381979bd3712SFilipe Manana ret = PTR_ERR(bg); 38202bb2e00eSFilipe Manana } else { 382179bd3712SFilipe Manana /* 382279bd3712SFilipe Manana * If we fail to add the chunk item here, we end up 382379bd3712SFilipe Manana * trying again at phase 2 of chunk allocation, at 382479bd3712SFilipe Manana * btrfs_create_pending_block_groups(). So ignore 38252bb2e00eSFilipe Manana * any error here. An ENOSPC here could happen, due to 38262bb2e00eSFilipe Manana * the cases described at do_chunk_alloc() - the system 38272bb2e00eSFilipe Manana * block group we just created was just turned into RO 38282bb2e00eSFilipe Manana * mode by a scrub for example, or a running discard 38292bb2e00eSFilipe Manana * temporarily removed its free space entries, etc. 383079bd3712SFilipe Manana */ 383179bd3712SFilipe Manana btrfs_chunk_alloc_add_chunk_item(trans, bg); 383279bd3712SFilipe Manana } 383307730d87SJosef Bacik } 383407730d87SJosef Bacik 383507730d87SJosef Bacik if (!ret) { 38369270501cSJosef Bacik ret = btrfs_block_rsv_add(fs_info, 383707730d87SJosef Bacik &fs_info->chunk_block_rsv, 38382bb2e00eSFilipe Manana bytes, BTRFS_RESERVE_NO_FLUSH); 38391cb3db1cSFilipe Manana if (!ret) 38402bb2e00eSFilipe Manana trans->chunk_bytes_reserved += bytes; 384107730d87SJosef Bacik } 384207730d87SJosef Bacik } 384307730d87SJosef Bacik 38442bb2e00eSFilipe Manana /* 38452bb2e00eSFilipe Manana * Reserve space in the system space for allocating or removing a chunk. 38462bb2e00eSFilipe Manana * The caller must be holding fs_info->chunk_mutex. 38472bb2e00eSFilipe Manana */ 38482bb2e00eSFilipe Manana void check_system_chunk(struct btrfs_trans_handle *trans, u64 type) 38492bb2e00eSFilipe Manana { 38502bb2e00eSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 38512bb2e00eSFilipe Manana const u64 num_devs = get_profile_num_devs(fs_info, type); 38522bb2e00eSFilipe Manana u64 bytes; 38532bb2e00eSFilipe Manana 38542bb2e00eSFilipe Manana /* num_devs device items to update and 1 chunk item to add or remove. */ 38552bb2e00eSFilipe Manana bytes = btrfs_calc_metadata_size(fs_info, num_devs) + 38562bb2e00eSFilipe Manana btrfs_calc_insert_metadata_size(fs_info, 1); 38572bb2e00eSFilipe Manana 38582bb2e00eSFilipe Manana reserve_chunk_space(trans, bytes, type); 38592bb2e00eSFilipe Manana } 38602bb2e00eSFilipe Manana 38612bb2e00eSFilipe Manana /* 38622bb2e00eSFilipe Manana * Reserve space in the system space, if needed, for doing a modification to the 38632bb2e00eSFilipe Manana * chunk btree. 38642bb2e00eSFilipe Manana * 38652bb2e00eSFilipe Manana * @trans: A transaction handle. 38662bb2e00eSFilipe Manana * @is_item_insertion: Indicate if the modification is for inserting a new item 38672bb2e00eSFilipe Manana * in the chunk btree or if it's for the deletion or update 38682bb2e00eSFilipe Manana * of an existing item. 38692bb2e00eSFilipe Manana * 38702bb2e00eSFilipe Manana * This is used in a context where we need to update the chunk btree outside 38712bb2e00eSFilipe Manana * block group allocation and removal, to avoid a deadlock with a concurrent 38722bb2e00eSFilipe Manana * task that is allocating a metadata or data block group and therefore needs to 38732bb2e00eSFilipe Manana * update the chunk btree while holding the chunk mutex. After the update to the 38742bb2e00eSFilipe Manana * chunk btree is done, btrfs_trans_release_chunk_metadata() should be called. 38752bb2e00eSFilipe Manana * 38762bb2e00eSFilipe Manana */ 38772bb2e00eSFilipe Manana void btrfs_reserve_chunk_metadata(struct btrfs_trans_handle *trans, 38782bb2e00eSFilipe Manana bool is_item_insertion) 38792bb2e00eSFilipe Manana { 38802bb2e00eSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 38812bb2e00eSFilipe Manana u64 bytes; 38822bb2e00eSFilipe Manana 38832bb2e00eSFilipe Manana if (is_item_insertion) 38842bb2e00eSFilipe Manana bytes = btrfs_calc_insert_metadata_size(fs_info, 1); 38852bb2e00eSFilipe Manana else 38862bb2e00eSFilipe Manana bytes = btrfs_calc_metadata_size(fs_info, 1); 38872bb2e00eSFilipe Manana 38882bb2e00eSFilipe Manana mutex_lock(&fs_info->chunk_mutex); 38892bb2e00eSFilipe Manana reserve_chunk_space(trans, bytes, BTRFS_BLOCK_GROUP_SYSTEM); 38902bb2e00eSFilipe Manana mutex_unlock(&fs_info->chunk_mutex); 38912bb2e00eSFilipe Manana } 38922bb2e00eSFilipe Manana 38933e43c279SJosef Bacik void btrfs_put_block_group_cache(struct btrfs_fs_info *info) 38943e43c279SJosef Bacik { 389532da5386SDavid Sterba struct btrfs_block_group *block_group; 38963e43c279SJosef Bacik u64 last = 0; 38973e43c279SJosef Bacik 38983e43c279SJosef Bacik while (1) { 38993e43c279SJosef Bacik struct inode *inode; 39003e43c279SJosef Bacik 39013e43c279SJosef Bacik block_group = btrfs_lookup_first_block_group(info, last); 39023e43c279SJosef Bacik while (block_group) { 39033e43c279SJosef Bacik btrfs_wait_block_group_cache_done(block_group); 39043e43c279SJosef Bacik spin_lock(&block_group->lock); 39053e43c279SJosef Bacik if (block_group->iref) 39063e43c279SJosef Bacik break; 39073e43c279SJosef Bacik spin_unlock(&block_group->lock); 39083e43c279SJosef Bacik block_group = btrfs_next_block_group(block_group); 39093e43c279SJosef Bacik } 39103e43c279SJosef Bacik if (!block_group) { 39113e43c279SJosef Bacik if (last == 0) 39123e43c279SJosef Bacik break; 39133e43c279SJosef Bacik last = 0; 39143e43c279SJosef Bacik continue; 39153e43c279SJosef Bacik } 39163e43c279SJosef Bacik 39173e43c279SJosef Bacik inode = block_group->inode; 39183e43c279SJosef Bacik block_group->iref = 0; 39193e43c279SJosef Bacik block_group->inode = NULL; 39203e43c279SJosef Bacik spin_unlock(&block_group->lock); 39213e43c279SJosef Bacik ASSERT(block_group->io_ctl.inode == NULL); 39223e43c279SJosef Bacik iput(inode); 3923b3470b5dSDavid Sterba last = block_group->start + block_group->length; 39243e43c279SJosef Bacik btrfs_put_block_group(block_group); 39253e43c279SJosef Bacik } 39263e43c279SJosef Bacik } 39273e43c279SJosef Bacik 39283e43c279SJosef Bacik /* 39293e43c279SJosef Bacik * Must be called only after stopping all workers, since we could have block 39303e43c279SJosef Bacik * group caching kthreads running, and therefore they could race with us if we 39313e43c279SJosef Bacik * freed the block groups before stopping them. 39323e43c279SJosef Bacik */ 39333e43c279SJosef Bacik int btrfs_free_block_groups(struct btrfs_fs_info *info) 39343e43c279SJosef Bacik { 393532da5386SDavid Sterba struct btrfs_block_group *block_group; 39363e43c279SJosef Bacik struct btrfs_space_info *space_info; 39373e43c279SJosef Bacik struct btrfs_caching_control *caching_ctl; 39383e43c279SJosef Bacik struct rb_node *n; 39393e43c279SJosef Bacik 3940bbb86a37SJosef Bacik spin_lock(&info->block_group_cache_lock); 39413e43c279SJosef Bacik while (!list_empty(&info->caching_block_groups)) { 39423e43c279SJosef Bacik caching_ctl = list_entry(info->caching_block_groups.next, 39433e43c279SJosef Bacik struct btrfs_caching_control, list); 39443e43c279SJosef Bacik list_del(&caching_ctl->list); 39453e43c279SJosef Bacik btrfs_put_caching_control(caching_ctl); 39463e43c279SJosef Bacik } 3947bbb86a37SJosef Bacik spin_unlock(&info->block_group_cache_lock); 39483e43c279SJosef Bacik 39493e43c279SJosef Bacik spin_lock(&info->unused_bgs_lock); 39503e43c279SJosef Bacik while (!list_empty(&info->unused_bgs)) { 39513e43c279SJosef Bacik block_group = list_first_entry(&info->unused_bgs, 395232da5386SDavid Sterba struct btrfs_block_group, 39533e43c279SJosef Bacik bg_list); 39543e43c279SJosef Bacik list_del_init(&block_group->bg_list); 39553e43c279SJosef Bacik btrfs_put_block_group(block_group); 39563e43c279SJosef Bacik } 39573e43c279SJosef Bacik 395818bb8bbfSJohannes Thumshirn while (!list_empty(&info->reclaim_bgs)) { 395918bb8bbfSJohannes Thumshirn block_group = list_first_entry(&info->reclaim_bgs, 396018bb8bbfSJohannes Thumshirn struct btrfs_block_group, 396118bb8bbfSJohannes Thumshirn bg_list); 396218bb8bbfSJohannes Thumshirn list_del_init(&block_group->bg_list); 396318bb8bbfSJohannes Thumshirn btrfs_put_block_group(block_group); 396418bb8bbfSJohannes Thumshirn } 396518bb8bbfSJohannes Thumshirn spin_unlock(&info->unused_bgs_lock); 396618bb8bbfSJohannes Thumshirn 3967afba2bc0SNaohiro Aota spin_lock(&info->zone_active_bgs_lock); 3968afba2bc0SNaohiro Aota while (!list_empty(&info->zone_active_bgs)) { 3969afba2bc0SNaohiro Aota block_group = list_first_entry(&info->zone_active_bgs, 3970afba2bc0SNaohiro Aota struct btrfs_block_group, 3971afba2bc0SNaohiro Aota active_bg_list); 3972afba2bc0SNaohiro Aota list_del_init(&block_group->active_bg_list); 3973afba2bc0SNaohiro Aota btrfs_put_block_group(block_group); 3974afba2bc0SNaohiro Aota } 3975afba2bc0SNaohiro Aota spin_unlock(&info->zone_active_bgs_lock); 3976afba2bc0SNaohiro Aota 39773e43c279SJosef Bacik spin_lock(&info->block_group_cache_lock); 39783e43c279SJosef Bacik while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { 397932da5386SDavid Sterba block_group = rb_entry(n, struct btrfs_block_group, 39803e43c279SJosef Bacik cache_node); 39813e43c279SJosef Bacik rb_erase(&block_group->cache_node, 39823e43c279SJosef Bacik &info->block_group_cache_tree); 39833e43c279SJosef Bacik RB_CLEAR_NODE(&block_group->cache_node); 39843e43c279SJosef Bacik spin_unlock(&info->block_group_cache_lock); 39853e43c279SJosef Bacik 39863e43c279SJosef Bacik down_write(&block_group->space_info->groups_sem); 39873e43c279SJosef Bacik list_del(&block_group->list); 39883e43c279SJosef Bacik up_write(&block_group->space_info->groups_sem); 39893e43c279SJosef Bacik 39903e43c279SJosef Bacik /* 39913e43c279SJosef Bacik * We haven't cached this block group, which means we could 39923e43c279SJosef Bacik * possibly have excluded extents on this block group. 39933e43c279SJosef Bacik */ 39943e43c279SJosef Bacik if (block_group->cached == BTRFS_CACHE_NO || 39953e43c279SJosef Bacik block_group->cached == BTRFS_CACHE_ERROR) 39963e43c279SJosef Bacik btrfs_free_excluded_extents(block_group); 39973e43c279SJosef Bacik 39983e43c279SJosef Bacik btrfs_remove_free_space_cache(block_group); 39993e43c279SJosef Bacik ASSERT(block_group->cached != BTRFS_CACHE_STARTED); 40003e43c279SJosef Bacik ASSERT(list_empty(&block_group->dirty_list)); 40013e43c279SJosef Bacik ASSERT(list_empty(&block_group->io_list)); 40023e43c279SJosef Bacik ASSERT(list_empty(&block_group->bg_list)); 400348aaeebeSJosef Bacik ASSERT(refcount_read(&block_group->refs) == 1); 4004195a49eaSFilipe Manana ASSERT(block_group->swap_extents == 0); 40053e43c279SJosef Bacik btrfs_put_block_group(block_group); 40063e43c279SJosef Bacik 40073e43c279SJosef Bacik spin_lock(&info->block_group_cache_lock); 40083e43c279SJosef Bacik } 40093e43c279SJosef Bacik spin_unlock(&info->block_group_cache_lock); 40103e43c279SJosef Bacik 40113e43c279SJosef Bacik btrfs_release_global_block_rsv(info); 40123e43c279SJosef Bacik 40133e43c279SJosef Bacik while (!list_empty(&info->space_info)) { 40143e43c279SJosef Bacik space_info = list_entry(info->space_info.next, 40153e43c279SJosef Bacik struct btrfs_space_info, 40163e43c279SJosef Bacik list); 40173e43c279SJosef Bacik 40183e43c279SJosef Bacik /* 40193e43c279SJosef Bacik * Do not hide this behind enospc_debug, this is actually 40203e43c279SJosef Bacik * important and indicates a real bug if this happens. 40213e43c279SJosef Bacik */ 40223e43c279SJosef Bacik if (WARN_ON(space_info->bytes_pinned > 0 || 40233e43c279SJosef Bacik space_info->bytes_may_use > 0)) 40243e43c279SJosef Bacik btrfs_dump_space_info(info, space_info, 0, 0); 402540cdc509SFilipe Manana 402640cdc509SFilipe Manana /* 402740cdc509SFilipe Manana * If there was a failure to cleanup a log tree, very likely due 402840cdc509SFilipe Manana * to an IO failure on a writeback attempt of one or more of its 402940cdc509SFilipe Manana * extent buffers, we could not do proper (and cheap) unaccounting 403040cdc509SFilipe Manana * of their reserved space, so don't warn on bytes_reserved > 0 in 403140cdc509SFilipe Manana * that case. 403240cdc509SFilipe Manana */ 403340cdc509SFilipe Manana if (!(space_info->flags & BTRFS_BLOCK_GROUP_METADATA) || 403440cdc509SFilipe Manana !BTRFS_FS_LOG_CLEANUP_ERROR(info)) { 403540cdc509SFilipe Manana if (WARN_ON(space_info->bytes_reserved > 0)) 403640cdc509SFilipe Manana btrfs_dump_space_info(info, space_info, 0, 0); 403740cdc509SFilipe Manana } 403840cdc509SFilipe Manana 4039d611add4SFilipe Manana WARN_ON(space_info->reclaim_size > 0); 40403e43c279SJosef Bacik list_del(&space_info->list); 40413e43c279SJosef Bacik btrfs_sysfs_remove_space_info(space_info); 40423e43c279SJosef Bacik } 40433e43c279SJosef Bacik return 0; 40443e43c279SJosef Bacik } 4045684b752bSFilipe Manana 4046684b752bSFilipe Manana void btrfs_freeze_block_group(struct btrfs_block_group *cache) 4047684b752bSFilipe Manana { 4048684b752bSFilipe Manana atomic_inc(&cache->frozen); 4049684b752bSFilipe Manana } 4050684b752bSFilipe Manana 4051684b752bSFilipe Manana void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group) 4052684b752bSFilipe Manana { 4053684b752bSFilipe Manana struct btrfs_fs_info *fs_info = block_group->fs_info; 4054684b752bSFilipe Manana struct extent_map_tree *em_tree; 4055684b752bSFilipe Manana struct extent_map *em; 4056684b752bSFilipe Manana bool cleanup; 4057684b752bSFilipe Manana 4058684b752bSFilipe Manana spin_lock(&block_group->lock); 4059684b752bSFilipe Manana cleanup = (atomic_dec_and_test(&block_group->frozen) && 4060684b752bSFilipe Manana block_group->removed); 4061684b752bSFilipe Manana spin_unlock(&block_group->lock); 4062684b752bSFilipe Manana 4063684b752bSFilipe Manana if (cleanup) { 4064684b752bSFilipe Manana em_tree = &fs_info->mapping_tree; 4065684b752bSFilipe Manana write_lock(&em_tree->lock); 4066684b752bSFilipe Manana em = lookup_extent_mapping(em_tree, block_group->start, 4067684b752bSFilipe Manana 1); 4068684b752bSFilipe Manana BUG_ON(!em); /* logic error, can't happen */ 4069684b752bSFilipe Manana remove_extent_mapping(em_tree, em); 4070684b752bSFilipe Manana write_unlock(&em_tree->lock); 4071684b752bSFilipe Manana 4072684b752bSFilipe Manana /* once for us and once for the tree */ 4073684b752bSFilipe Manana free_extent_map(em); 4074684b752bSFilipe Manana free_extent_map(em); 4075684b752bSFilipe Manana 4076684b752bSFilipe Manana /* 4077684b752bSFilipe Manana * We may have left one free space entry and other possible 4078684b752bSFilipe Manana * tasks trimming this block group have left 1 entry each one. 4079684b752bSFilipe Manana * Free them if any. 4080684b752bSFilipe Manana */ 4081684b752bSFilipe Manana __btrfs_remove_free_space_cache(block_group->free_space_ctl); 4082684b752bSFilipe Manana } 4083684b752bSFilipe Manana } 4084195a49eaSFilipe Manana 4085195a49eaSFilipe Manana bool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg) 4086195a49eaSFilipe Manana { 4087195a49eaSFilipe Manana bool ret = true; 4088195a49eaSFilipe Manana 4089195a49eaSFilipe Manana spin_lock(&bg->lock); 4090195a49eaSFilipe Manana if (bg->ro) 4091195a49eaSFilipe Manana ret = false; 4092195a49eaSFilipe Manana else 4093195a49eaSFilipe Manana bg->swap_extents++; 4094195a49eaSFilipe Manana spin_unlock(&bg->lock); 4095195a49eaSFilipe Manana 4096195a49eaSFilipe Manana return ret; 4097195a49eaSFilipe Manana } 4098195a49eaSFilipe Manana 4099195a49eaSFilipe Manana void btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount) 4100195a49eaSFilipe Manana { 4101195a49eaSFilipe Manana spin_lock(&bg->lock); 4102195a49eaSFilipe Manana ASSERT(!bg->ro); 4103195a49eaSFilipe Manana ASSERT(bg->swap_extents >= amount); 4104195a49eaSFilipe Manana bg->swap_extents -= amount; 4105195a49eaSFilipe Manana spin_unlock(&bg->lock); 4106195a49eaSFilipe Manana } 4107