xref: /linux/fs/btrfs/block-group.c (revision 50564b651d01c19ce732819c5b3c3fd60707188e)
12e405ad8SJosef Bacik // SPDX-License-Identifier: GPL-2.0
22e405ad8SJosef Bacik 
352bb7a21SBoris Burkov #include <linux/sizes.h>
42ca0ec77SJohannes Thumshirn #include <linux/list_sort.h>
5784352feSDavid Sterba #include "misc.h"
62e405ad8SJosef Bacik #include "ctree.h"
72e405ad8SJosef Bacik #include "block-group.h"
83eeb3226SJosef Bacik #include "space-info.h"
99f21246dSJosef Bacik #include "disk-io.h"
109f21246dSJosef Bacik #include "free-space-cache.h"
119f21246dSJosef Bacik #include "free-space-tree.h"
12e3e0520bSJosef Bacik #include "volumes.h"
13e3e0520bSJosef Bacik #include "transaction.h"
14e3e0520bSJosef Bacik #include "ref-verify.h"
154358d963SJosef Bacik #include "sysfs.h"
164358d963SJosef Bacik #include "tree-log.h"
1777745c05SJosef Bacik #include "delalloc-space.h"
18b0643e59SDennis Zhou #include "discard.h"
1996a14336SNikolay Borisov #include "raid56.h"
2008e11a3dSNaohiro Aota #include "zoned.h"
21c7f13d42SJosef Bacik #include "fs.h"
2207e81dc9SJosef Bacik #include "accessors.h"
23a0231804SJosef Bacik #include "extent-tree.h"
242e405ad8SJosef Bacik 
2506d61cb1SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
2606d61cb1SJosef Bacik int btrfs_should_fragment_free_space(struct btrfs_block_group *block_group)
2706d61cb1SJosef Bacik {
2806d61cb1SJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2906d61cb1SJosef Bacik 
3006d61cb1SJosef Bacik 	return (btrfs_test_opt(fs_info, FRAGMENT_METADATA) &&
3106d61cb1SJosef Bacik 		block_group->flags & BTRFS_BLOCK_GROUP_METADATA) ||
3206d61cb1SJosef Bacik 	       (btrfs_test_opt(fs_info, FRAGMENT_DATA) &&
3306d61cb1SJosef Bacik 		block_group->flags &  BTRFS_BLOCK_GROUP_DATA);
3406d61cb1SJosef Bacik }
3506d61cb1SJosef Bacik #endif
3606d61cb1SJosef Bacik 
37878d7b67SJosef Bacik /*
38878d7b67SJosef Bacik  * Return target flags in extended format or 0 if restripe for this chunk_type
39878d7b67SJosef Bacik  * is not in progress
40878d7b67SJosef Bacik  *
41878d7b67SJosef Bacik  * Should be called with balance_lock held
42878d7b67SJosef Bacik  */
43e11c0406SJosef Bacik static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
44878d7b67SJosef Bacik {
45878d7b67SJosef Bacik 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
46878d7b67SJosef Bacik 	u64 target = 0;
47878d7b67SJosef Bacik 
48878d7b67SJosef Bacik 	if (!bctl)
49878d7b67SJosef Bacik 		return 0;
50878d7b67SJosef Bacik 
51878d7b67SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA &&
52878d7b67SJosef Bacik 	    bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
53878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
54878d7b67SJosef Bacik 	} else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
55878d7b67SJosef Bacik 		   bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
56878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
57878d7b67SJosef Bacik 	} else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
58878d7b67SJosef Bacik 		   bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
59878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
60878d7b67SJosef Bacik 	}
61878d7b67SJosef Bacik 
62878d7b67SJosef Bacik 	return target;
63878d7b67SJosef Bacik }
64878d7b67SJosef Bacik 
65878d7b67SJosef Bacik /*
66878d7b67SJosef Bacik  * @flags: available profiles in extended format (see ctree.h)
67878d7b67SJosef Bacik  *
68878d7b67SJosef Bacik  * Return reduced profile in chunk format.  If profile changing is in progress
69878d7b67SJosef Bacik  * (either running or paused) picks the target profile (if it's already
70878d7b67SJosef Bacik  * available), otherwise falls back to plain reducing.
71878d7b67SJosef Bacik  */
72878d7b67SJosef Bacik static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
73878d7b67SJosef Bacik {
74878d7b67SJosef Bacik 	u64 num_devices = fs_info->fs_devices->rw_devices;
75878d7b67SJosef Bacik 	u64 target;
76878d7b67SJosef Bacik 	u64 raid_type;
77878d7b67SJosef Bacik 	u64 allowed = 0;
78878d7b67SJosef Bacik 
79878d7b67SJosef Bacik 	/*
80878d7b67SJosef Bacik 	 * See if restripe for this chunk_type is in progress, if so try to
81878d7b67SJosef Bacik 	 * reduce to the target profile
82878d7b67SJosef Bacik 	 */
83878d7b67SJosef Bacik 	spin_lock(&fs_info->balance_lock);
84e11c0406SJosef Bacik 	target = get_restripe_target(fs_info, flags);
85878d7b67SJosef Bacik 	if (target) {
86878d7b67SJosef Bacik 		spin_unlock(&fs_info->balance_lock);
87878d7b67SJosef Bacik 		return extended_to_chunk(target);
88878d7b67SJosef Bacik 	}
89878d7b67SJosef Bacik 	spin_unlock(&fs_info->balance_lock);
90878d7b67SJosef Bacik 
91878d7b67SJosef Bacik 	/* First, mask out the RAID levels which aren't possible */
92878d7b67SJosef Bacik 	for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
93878d7b67SJosef Bacik 		if (num_devices >= btrfs_raid_array[raid_type].devs_min)
94878d7b67SJosef Bacik 			allowed |= btrfs_raid_array[raid_type].bg_flag;
95878d7b67SJosef Bacik 	}
96878d7b67SJosef Bacik 	allowed &= flags;
97878d7b67SJosef Bacik 
98160fe8f6SMatt Corallo 	/* Select the highest-redundancy RAID level. */
99160fe8f6SMatt Corallo 	if (allowed & BTRFS_BLOCK_GROUP_RAID1C4)
100160fe8f6SMatt Corallo 		allowed = BTRFS_BLOCK_GROUP_RAID1C4;
101160fe8f6SMatt Corallo 	else if (allowed & BTRFS_BLOCK_GROUP_RAID6)
102878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID6;
103160fe8f6SMatt Corallo 	else if (allowed & BTRFS_BLOCK_GROUP_RAID1C3)
104160fe8f6SMatt Corallo 		allowed = BTRFS_BLOCK_GROUP_RAID1C3;
105878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
106878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID5;
107878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
108878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID10;
109878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
110878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID1;
111160fe8f6SMatt Corallo 	else if (allowed & BTRFS_BLOCK_GROUP_DUP)
112160fe8f6SMatt Corallo 		allowed = BTRFS_BLOCK_GROUP_DUP;
113878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
114878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID0;
115878d7b67SJosef Bacik 
116878d7b67SJosef Bacik 	flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
117878d7b67SJosef Bacik 
118878d7b67SJosef Bacik 	return extended_to_chunk(flags | allowed);
119878d7b67SJosef Bacik }
120878d7b67SJosef Bacik 
121ef0a82daSJohannes Thumshirn u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
122878d7b67SJosef Bacik {
123878d7b67SJosef Bacik 	unsigned seq;
124878d7b67SJosef Bacik 	u64 flags;
125878d7b67SJosef Bacik 
126878d7b67SJosef Bacik 	do {
127878d7b67SJosef Bacik 		flags = orig_flags;
128878d7b67SJosef Bacik 		seq = read_seqbegin(&fs_info->profiles_lock);
129878d7b67SJosef Bacik 
130878d7b67SJosef Bacik 		if (flags & BTRFS_BLOCK_GROUP_DATA)
131878d7b67SJosef Bacik 			flags |= fs_info->avail_data_alloc_bits;
132878d7b67SJosef Bacik 		else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
133878d7b67SJosef Bacik 			flags |= fs_info->avail_system_alloc_bits;
134878d7b67SJosef Bacik 		else if (flags & BTRFS_BLOCK_GROUP_METADATA)
135878d7b67SJosef Bacik 			flags |= fs_info->avail_metadata_alloc_bits;
136878d7b67SJosef Bacik 	} while (read_seqretry(&fs_info->profiles_lock, seq));
137878d7b67SJosef Bacik 
138878d7b67SJosef Bacik 	return btrfs_reduce_alloc_profile(fs_info, flags);
139878d7b67SJosef Bacik }
140878d7b67SJosef Bacik 
14132da5386SDavid Sterba void btrfs_get_block_group(struct btrfs_block_group *cache)
1423cad1284SJosef Bacik {
14348aaeebeSJosef Bacik 	refcount_inc(&cache->refs);
1443cad1284SJosef Bacik }
1453cad1284SJosef Bacik 
14632da5386SDavid Sterba void btrfs_put_block_group(struct btrfs_block_group *cache)
1473cad1284SJosef Bacik {
14848aaeebeSJosef Bacik 	if (refcount_dec_and_test(&cache->refs)) {
1493cad1284SJosef Bacik 		WARN_ON(cache->pinned > 0);
15040cdc509SFilipe Manana 		/*
15140cdc509SFilipe Manana 		 * If there was a failure to cleanup a log tree, very likely due
15240cdc509SFilipe Manana 		 * to an IO failure on a writeback attempt of one or more of its
15340cdc509SFilipe Manana 		 * extent buffers, we could not do proper (and cheap) unaccounting
15440cdc509SFilipe Manana 		 * of their reserved space, so don't warn on reserved > 0 in that
15540cdc509SFilipe Manana 		 * case.
15640cdc509SFilipe Manana 		 */
15740cdc509SFilipe Manana 		if (!(cache->flags & BTRFS_BLOCK_GROUP_METADATA) ||
15840cdc509SFilipe Manana 		    !BTRFS_FS_LOG_CLEANUP_ERROR(cache->fs_info))
1593cad1284SJosef Bacik 			WARN_ON(cache->reserved > 0);
1603cad1284SJosef Bacik 
1613cad1284SJosef Bacik 		/*
162b0643e59SDennis Zhou 		 * A block_group shouldn't be on the discard_list anymore.
163b0643e59SDennis Zhou 		 * Remove the block_group from the discard_list to prevent us
164b0643e59SDennis Zhou 		 * from causing a panic due to NULL pointer dereference.
165b0643e59SDennis Zhou 		 */
166b0643e59SDennis Zhou 		if (WARN_ON(!list_empty(&cache->discard_list)))
167b0643e59SDennis Zhou 			btrfs_discard_cancel_work(&cache->fs_info->discard_ctl,
168b0643e59SDennis Zhou 						  cache);
169b0643e59SDennis Zhou 
1703cad1284SJosef Bacik 		kfree(cache->free_space_ctl);
171dafc340dSNaohiro Aota 		kfree(cache->physical_map);
1723cad1284SJosef Bacik 		kfree(cache);
1733cad1284SJosef Bacik 	}
1743cad1284SJosef Bacik }
1753cad1284SJosef Bacik 
1762e405ad8SJosef Bacik /*
1774358d963SJosef Bacik  * This adds the block group to the fs_info rb tree for the block group cache
1784358d963SJosef Bacik  */
1794358d963SJosef Bacik static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
18032da5386SDavid Sterba 				       struct btrfs_block_group *block_group)
1814358d963SJosef Bacik {
1824358d963SJosef Bacik 	struct rb_node **p;
1834358d963SJosef Bacik 	struct rb_node *parent = NULL;
18432da5386SDavid Sterba 	struct btrfs_block_group *cache;
18508dddb29SFilipe Manana 	bool leftmost = true;
1864358d963SJosef Bacik 
1879afc6649SQu Wenruo 	ASSERT(block_group->length != 0);
1889afc6649SQu Wenruo 
18916b0c258SFilipe Manana 	write_lock(&info->block_group_cache_lock);
19008dddb29SFilipe Manana 	p = &info->block_group_cache_tree.rb_root.rb_node;
1914358d963SJosef Bacik 
1924358d963SJosef Bacik 	while (*p) {
1934358d963SJosef Bacik 		parent = *p;
19432da5386SDavid Sterba 		cache = rb_entry(parent, struct btrfs_block_group, cache_node);
195b3470b5dSDavid Sterba 		if (block_group->start < cache->start) {
1964358d963SJosef Bacik 			p = &(*p)->rb_left;
197b3470b5dSDavid Sterba 		} else if (block_group->start > cache->start) {
1984358d963SJosef Bacik 			p = &(*p)->rb_right;
19908dddb29SFilipe Manana 			leftmost = false;
2004358d963SJosef Bacik 		} else {
20116b0c258SFilipe Manana 			write_unlock(&info->block_group_cache_lock);
2024358d963SJosef Bacik 			return -EEXIST;
2034358d963SJosef Bacik 		}
2044358d963SJosef Bacik 	}
2054358d963SJosef Bacik 
2064358d963SJosef Bacik 	rb_link_node(&block_group->cache_node, parent, p);
20708dddb29SFilipe Manana 	rb_insert_color_cached(&block_group->cache_node,
20808dddb29SFilipe Manana 			       &info->block_group_cache_tree, leftmost);
2094358d963SJosef Bacik 
21016b0c258SFilipe Manana 	write_unlock(&info->block_group_cache_lock);
2114358d963SJosef Bacik 
2124358d963SJosef Bacik 	return 0;
2134358d963SJosef Bacik }
2144358d963SJosef Bacik 
2154358d963SJosef Bacik /*
2162e405ad8SJosef Bacik  * This will return the block group at or after bytenr if contains is 0, else
2172e405ad8SJosef Bacik  * it will return the block group that contains the bytenr
2182e405ad8SJosef Bacik  */
21932da5386SDavid Sterba static struct btrfs_block_group *block_group_cache_tree_search(
2202e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr, int contains)
2212e405ad8SJosef Bacik {
22232da5386SDavid Sterba 	struct btrfs_block_group *cache, *ret = NULL;
2232e405ad8SJosef Bacik 	struct rb_node *n;
2242e405ad8SJosef Bacik 	u64 end, start;
2252e405ad8SJosef Bacik 
22616b0c258SFilipe Manana 	read_lock(&info->block_group_cache_lock);
22708dddb29SFilipe Manana 	n = info->block_group_cache_tree.rb_root.rb_node;
2282e405ad8SJosef Bacik 
2292e405ad8SJosef Bacik 	while (n) {
23032da5386SDavid Sterba 		cache = rb_entry(n, struct btrfs_block_group, cache_node);
231b3470b5dSDavid Sterba 		end = cache->start + cache->length - 1;
232b3470b5dSDavid Sterba 		start = cache->start;
2332e405ad8SJosef Bacik 
2342e405ad8SJosef Bacik 		if (bytenr < start) {
235b3470b5dSDavid Sterba 			if (!contains && (!ret || start < ret->start))
2362e405ad8SJosef Bacik 				ret = cache;
2372e405ad8SJosef Bacik 			n = n->rb_left;
2382e405ad8SJosef Bacik 		} else if (bytenr > start) {
2392e405ad8SJosef Bacik 			if (contains && bytenr <= end) {
2402e405ad8SJosef Bacik 				ret = cache;
2412e405ad8SJosef Bacik 				break;
2422e405ad8SJosef Bacik 			}
2432e405ad8SJosef Bacik 			n = n->rb_right;
2442e405ad8SJosef Bacik 		} else {
2452e405ad8SJosef Bacik 			ret = cache;
2462e405ad8SJosef Bacik 			break;
2472e405ad8SJosef Bacik 		}
2482e405ad8SJosef Bacik 	}
24908dddb29SFilipe Manana 	if (ret)
2502e405ad8SJosef Bacik 		btrfs_get_block_group(ret);
25116b0c258SFilipe Manana 	read_unlock(&info->block_group_cache_lock);
2522e405ad8SJosef Bacik 
2532e405ad8SJosef Bacik 	return ret;
2542e405ad8SJosef Bacik }
2552e405ad8SJosef Bacik 
2562e405ad8SJosef Bacik /*
2572e405ad8SJosef Bacik  * Return the block group that starts at or after bytenr
2582e405ad8SJosef Bacik  */
25932da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_first_block_group(
2602e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr)
2612e405ad8SJosef Bacik {
2622e405ad8SJosef Bacik 	return block_group_cache_tree_search(info, bytenr, 0);
2632e405ad8SJosef Bacik }
2642e405ad8SJosef Bacik 
2652e405ad8SJosef Bacik /*
2662e405ad8SJosef Bacik  * Return the block group that contains the given bytenr
2672e405ad8SJosef Bacik  */
26832da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_block_group(
2692e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr)
2702e405ad8SJosef Bacik {
2712e405ad8SJosef Bacik 	return block_group_cache_tree_search(info, bytenr, 1);
2722e405ad8SJosef Bacik }
2732e405ad8SJosef Bacik 
27432da5386SDavid Sterba struct btrfs_block_group *btrfs_next_block_group(
27532da5386SDavid Sterba 		struct btrfs_block_group *cache)
2762e405ad8SJosef Bacik {
2772e405ad8SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
2782e405ad8SJosef Bacik 	struct rb_node *node;
2792e405ad8SJosef Bacik 
28016b0c258SFilipe Manana 	read_lock(&fs_info->block_group_cache_lock);
2812e405ad8SJosef Bacik 
2822e405ad8SJosef Bacik 	/* If our block group was removed, we need a full search. */
2832e405ad8SJosef Bacik 	if (RB_EMPTY_NODE(&cache->cache_node)) {
284b3470b5dSDavid Sterba 		const u64 next_bytenr = cache->start + cache->length;
2852e405ad8SJosef Bacik 
28616b0c258SFilipe Manana 		read_unlock(&fs_info->block_group_cache_lock);
2872e405ad8SJosef Bacik 		btrfs_put_block_group(cache);
2888b01f931SFilipe Manana 		return btrfs_lookup_first_block_group(fs_info, next_bytenr);
2892e405ad8SJosef Bacik 	}
2902e405ad8SJosef Bacik 	node = rb_next(&cache->cache_node);
2912e405ad8SJosef Bacik 	btrfs_put_block_group(cache);
2922e405ad8SJosef Bacik 	if (node) {
29332da5386SDavid Sterba 		cache = rb_entry(node, struct btrfs_block_group, cache_node);
2942e405ad8SJosef Bacik 		btrfs_get_block_group(cache);
2952e405ad8SJosef Bacik 	} else
2962e405ad8SJosef Bacik 		cache = NULL;
29716b0c258SFilipe Manana 	read_unlock(&fs_info->block_group_cache_lock);
2982e405ad8SJosef Bacik 	return cache;
2992e405ad8SJosef Bacik }
3003eeb3226SJosef Bacik 
30143dd529aSDavid Sterba /*
3022306e83eSFilipe Manana  * Check if we can do a NOCOW write for a given extent.
3032306e83eSFilipe Manana  *
3042306e83eSFilipe Manana  * @fs_info:       The filesystem information object.
3052306e83eSFilipe Manana  * @bytenr:        Logical start address of the extent.
3062306e83eSFilipe Manana  *
3072306e83eSFilipe Manana  * Check if we can do a NOCOW write for the given extent, and increments the
3082306e83eSFilipe Manana  * number of NOCOW writers in the block group that contains the extent, as long
3092306e83eSFilipe Manana  * as the block group exists and it's currently not in read-only mode.
3102306e83eSFilipe Manana  *
3112306e83eSFilipe Manana  * Returns: A non-NULL block group pointer if we can do a NOCOW write, the caller
3122306e83eSFilipe Manana  *          is responsible for calling btrfs_dec_nocow_writers() later.
3132306e83eSFilipe Manana  *
3142306e83eSFilipe Manana  *          Or NULL if we can not do a NOCOW write
3152306e83eSFilipe Manana  */
3162306e83eSFilipe Manana struct btrfs_block_group *btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info,
3172306e83eSFilipe Manana 						  u64 bytenr)
3183eeb3226SJosef Bacik {
31932da5386SDavid Sterba 	struct btrfs_block_group *bg;
3202306e83eSFilipe Manana 	bool can_nocow = true;
3213eeb3226SJosef Bacik 
3223eeb3226SJosef Bacik 	bg = btrfs_lookup_block_group(fs_info, bytenr);
3233eeb3226SJosef Bacik 	if (!bg)
3242306e83eSFilipe Manana 		return NULL;
3253eeb3226SJosef Bacik 
3263eeb3226SJosef Bacik 	spin_lock(&bg->lock);
3273eeb3226SJosef Bacik 	if (bg->ro)
3282306e83eSFilipe Manana 		can_nocow = false;
3293eeb3226SJosef Bacik 	else
3303eeb3226SJosef Bacik 		atomic_inc(&bg->nocow_writers);
3313eeb3226SJosef Bacik 	spin_unlock(&bg->lock);
3323eeb3226SJosef Bacik 
3332306e83eSFilipe Manana 	if (!can_nocow) {
3343eeb3226SJosef Bacik 		btrfs_put_block_group(bg);
3352306e83eSFilipe Manana 		return NULL;
3363eeb3226SJosef Bacik 	}
3373eeb3226SJosef Bacik 
3382306e83eSFilipe Manana 	/* No put on block group, done by btrfs_dec_nocow_writers(). */
3392306e83eSFilipe Manana 	return bg;
3402306e83eSFilipe Manana }
3413eeb3226SJosef Bacik 
34243dd529aSDavid Sterba /*
3432306e83eSFilipe Manana  * Decrement the number of NOCOW writers in a block group.
3442306e83eSFilipe Manana  *
3452306e83eSFilipe Manana  * This is meant to be called after a previous call to btrfs_inc_nocow_writers(),
3462306e83eSFilipe Manana  * and on the block group returned by that call. Typically this is called after
3472306e83eSFilipe Manana  * creating an ordered extent for a NOCOW write, to prevent races with scrub and
3482306e83eSFilipe Manana  * relocation.
3492306e83eSFilipe Manana  *
3502306e83eSFilipe Manana  * After this call, the caller should not use the block group anymore. It it wants
3512306e83eSFilipe Manana  * to use it, then it should get a reference on it before calling this function.
3522306e83eSFilipe Manana  */
3532306e83eSFilipe Manana void btrfs_dec_nocow_writers(struct btrfs_block_group *bg)
3542306e83eSFilipe Manana {
3553eeb3226SJosef Bacik 	if (atomic_dec_and_test(&bg->nocow_writers))
3563eeb3226SJosef Bacik 		wake_up_var(&bg->nocow_writers);
3572306e83eSFilipe Manana 
3582306e83eSFilipe Manana 	/* For the lookup done by a previous call to btrfs_inc_nocow_writers(). */
3593eeb3226SJosef Bacik 	btrfs_put_block_group(bg);
3603eeb3226SJosef Bacik }
3613eeb3226SJosef Bacik 
36232da5386SDavid Sterba void btrfs_wait_nocow_writers(struct btrfs_block_group *bg)
3633eeb3226SJosef Bacik {
3643eeb3226SJosef Bacik 	wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
3653eeb3226SJosef Bacik }
3663eeb3226SJosef Bacik 
3673eeb3226SJosef Bacik void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
3683eeb3226SJosef Bacik 					const u64 start)
3693eeb3226SJosef Bacik {
37032da5386SDavid Sterba 	struct btrfs_block_group *bg;
3713eeb3226SJosef Bacik 
3723eeb3226SJosef Bacik 	bg = btrfs_lookup_block_group(fs_info, start);
3733eeb3226SJosef Bacik 	ASSERT(bg);
3743eeb3226SJosef Bacik 	if (atomic_dec_and_test(&bg->reservations))
3753eeb3226SJosef Bacik 		wake_up_var(&bg->reservations);
3763eeb3226SJosef Bacik 	btrfs_put_block_group(bg);
3773eeb3226SJosef Bacik }
3783eeb3226SJosef Bacik 
37932da5386SDavid Sterba void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg)
3803eeb3226SJosef Bacik {
3813eeb3226SJosef Bacik 	struct btrfs_space_info *space_info = bg->space_info;
3823eeb3226SJosef Bacik 
3833eeb3226SJosef Bacik 	ASSERT(bg->ro);
3843eeb3226SJosef Bacik 
3853eeb3226SJosef Bacik 	if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
3863eeb3226SJosef Bacik 		return;
3873eeb3226SJosef Bacik 
3883eeb3226SJosef Bacik 	/*
3893eeb3226SJosef Bacik 	 * Our block group is read only but before we set it to read only,
3903eeb3226SJosef Bacik 	 * some task might have had allocated an extent from it already, but it
3913eeb3226SJosef Bacik 	 * has not yet created a respective ordered extent (and added it to a
3923eeb3226SJosef Bacik 	 * root's list of ordered extents).
3933eeb3226SJosef Bacik 	 * Therefore wait for any task currently allocating extents, since the
3943eeb3226SJosef Bacik 	 * block group's reservations counter is incremented while a read lock
3953eeb3226SJosef Bacik 	 * on the groups' semaphore is held and decremented after releasing
3963eeb3226SJosef Bacik 	 * the read access on that semaphore and creating the ordered extent.
3973eeb3226SJosef Bacik 	 */
3983eeb3226SJosef Bacik 	down_write(&space_info->groups_sem);
3993eeb3226SJosef Bacik 	up_write(&space_info->groups_sem);
4003eeb3226SJosef Bacik 
4013eeb3226SJosef Bacik 	wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
4023eeb3226SJosef Bacik }
4039f21246dSJosef Bacik 
4049f21246dSJosef Bacik struct btrfs_caching_control *btrfs_get_caching_control(
40532da5386SDavid Sterba 		struct btrfs_block_group *cache)
4069f21246dSJosef Bacik {
4079f21246dSJosef Bacik 	struct btrfs_caching_control *ctl;
4089f21246dSJosef Bacik 
4099f21246dSJosef Bacik 	spin_lock(&cache->lock);
4109f21246dSJosef Bacik 	if (!cache->caching_ctl) {
4119f21246dSJosef Bacik 		spin_unlock(&cache->lock);
4129f21246dSJosef Bacik 		return NULL;
4139f21246dSJosef Bacik 	}
4149f21246dSJosef Bacik 
4159f21246dSJosef Bacik 	ctl = cache->caching_ctl;
4169f21246dSJosef Bacik 	refcount_inc(&ctl->count);
4179f21246dSJosef Bacik 	spin_unlock(&cache->lock);
4189f21246dSJosef Bacik 	return ctl;
4199f21246dSJosef Bacik }
4209f21246dSJosef Bacik 
4219f21246dSJosef Bacik void btrfs_put_caching_control(struct btrfs_caching_control *ctl)
4229f21246dSJosef Bacik {
4239f21246dSJosef Bacik 	if (refcount_dec_and_test(&ctl->count))
4249f21246dSJosef Bacik 		kfree(ctl);
4259f21246dSJosef Bacik }
4269f21246dSJosef Bacik 
4279f21246dSJosef Bacik /*
4289f21246dSJosef Bacik  * When we wait for progress in the block group caching, its because our
4299f21246dSJosef Bacik  * allocation attempt failed at least once.  So, we must sleep and let some
4309f21246dSJosef Bacik  * progress happen before we try again.
4319f21246dSJosef Bacik  *
4329f21246dSJosef Bacik  * This function will sleep at least once waiting for new free space to show
4339f21246dSJosef Bacik  * up, and then it will check the block group free space numbers for our min
4349f21246dSJosef Bacik  * num_bytes.  Another option is to have it go ahead and look in the rbtree for
4359f21246dSJosef Bacik  * a free extent of a given size, but this is a good start.
4369f21246dSJosef Bacik  *
4379f21246dSJosef Bacik  * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
4389f21246dSJosef Bacik  * any of the information in this block group.
4399f21246dSJosef Bacik  */
44032da5386SDavid Sterba void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache,
4419f21246dSJosef Bacik 					   u64 num_bytes)
4429f21246dSJosef Bacik {
4439f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
444fc1f91b9SJosef Bacik 	int progress;
4459f21246dSJosef Bacik 
4469f21246dSJosef Bacik 	caching_ctl = btrfs_get_caching_control(cache);
4479f21246dSJosef Bacik 	if (!caching_ctl)
4489f21246dSJosef Bacik 		return;
4499f21246dSJosef Bacik 
450fc1f91b9SJosef Bacik 	/*
451fc1f91b9SJosef Bacik 	 * We've already failed to allocate from this block group, so even if
452fc1f91b9SJosef Bacik 	 * there's enough space in the block group it isn't contiguous enough to
453fc1f91b9SJosef Bacik 	 * allow for an allocation, so wait for at least the next wakeup tick,
454fc1f91b9SJosef Bacik 	 * or for the thing to be done.
455fc1f91b9SJosef Bacik 	 */
456fc1f91b9SJosef Bacik 	progress = atomic_read(&caching_ctl->progress);
457fc1f91b9SJosef Bacik 
45832da5386SDavid Sterba 	wait_event(caching_ctl->wait, btrfs_block_group_done(cache) ||
459fc1f91b9SJosef Bacik 		   (progress != atomic_read(&caching_ctl->progress) &&
460fc1f91b9SJosef Bacik 		    (cache->free_space_ctl->free_space >= num_bytes)));
4619f21246dSJosef Bacik 
4629f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
4639f21246dSJosef Bacik }
4649f21246dSJosef Bacik 
465ced8ecf0SOmar Sandoval static int btrfs_caching_ctl_wait_done(struct btrfs_block_group *cache,
466ced8ecf0SOmar Sandoval 				       struct btrfs_caching_control *caching_ctl)
467ced8ecf0SOmar Sandoval {
468ced8ecf0SOmar Sandoval 	wait_event(caching_ctl->wait, btrfs_block_group_done(cache));
469ced8ecf0SOmar Sandoval 	return cache->cached == BTRFS_CACHE_ERROR ? -EIO : 0;
470ced8ecf0SOmar Sandoval }
471ced8ecf0SOmar Sandoval 
472ced8ecf0SOmar Sandoval static int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache)
4739f21246dSJosef Bacik {
4749f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
475ced8ecf0SOmar Sandoval 	int ret;
4769f21246dSJosef Bacik 
4779f21246dSJosef Bacik 	caching_ctl = btrfs_get_caching_control(cache);
4789f21246dSJosef Bacik 	if (!caching_ctl)
4799f21246dSJosef Bacik 		return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
480ced8ecf0SOmar Sandoval 	ret = btrfs_caching_ctl_wait_done(cache, caching_ctl);
4819f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
4829f21246dSJosef Bacik 	return ret;
4839f21246dSJosef Bacik }
4849f21246dSJosef Bacik 
4859f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
48632da5386SDavid Sterba static void fragment_free_space(struct btrfs_block_group *block_group)
4879f21246dSJosef Bacik {
4889f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
489b3470b5dSDavid Sterba 	u64 start = block_group->start;
490b3470b5dSDavid Sterba 	u64 len = block_group->length;
4919f21246dSJosef Bacik 	u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
4929f21246dSJosef Bacik 		fs_info->nodesize : fs_info->sectorsize;
4939f21246dSJosef Bacik 	u64 step = chunk << 1;
4949f21246dSJosef Bacik 
4959f21246dSJosef Bacik 	while (len > chunk) {
4969f21246dSJosef Bacik 		btrfs_remove_free_space(block_group, start, chunk);
4979f21246dSJosef Bacik 		start += step;
4989f21246dSJosef Bacik 		if (len < step)
4999f21246dSJosef Bacik 			len = 0;
5009f21246dSJosef Bacik 		else
5019f21246dSJosef Bacik 			len -= step;
5029f21246dSJosef Bacik 	}
5039f21246dSJosef Bacik }
5049f21246dSJosef Bacik #endif
5059f21246dSJosef Bacik 
5069f21246dSJosef Bacik /*
50728f60894SFilipe Manana  * Add a free space range to the in memory free space cache of a block group.
50828f60894SFilipe Manana  * This checks if the range contains super block locations and any such
50928f60894SFilipe Manana  * locations are not added to the free space cache.
51028f60894SFilipe Manana  *
51128f60894SFilipe Manana  * @block_group:      The target block group.
51228f60894SFilipe Manana  * @start:            Start offset of the range.
51328f60894SFilipe Manana  * @end:              End offset of the range (exclusive).
51428f60894SFilipe Manana  * @total_added_ret:  Optional pointer to return the total amount of space
51528f60894SFilipe Manana  *                    added to the block group's free space cache.
51628f60894SFilipe Manana  *
51728f60894SFilipe Manana  * Returns 0 on success or < 0 on error.
5189f21246dSJosef Bacik  */
5193b9f0995SFilipe Manana int btrfs_add_new_free_space(struct btrfs_block_group *block_group, u64 start,
5203b9f0995SFilipe Manana 			     u64 end, u64 *total_added_ret)
5219f21246dSJosef Bacik {
5229f21246dSJosef Bacik 	struct btrfs_fs_info *info = block_group->fs_info;
523d8ccbd21SFilipe Manana 	u64 extent_start, extent_end, size;
5249f21246dSJosef Bacik 	int ret;
5259f21246dSJosef Bacik 
526d8ccbd21SFilipe Manana 	if (total_added_ret)
527d8ccbd21SFilipe Manana 		*total_added_ret = 0;
528d8ccbd21SFilipe Manana 
5299f21246dSJosef Bacik 	while (start < end) {
530e5860f82SFilipe Manana 		if (!find_first_extent_bit(&info->excluded_extents, start,
5319f21246dSJosef Bacik 					   &extent_start, &extent_end,
5329f21246dSJosef Bacik 					   EXTENT_DIRTY | EXTENT_UPTODATE,
533e5860f82SFilipe Manana 					   NULL))
5349f21246dSJosef Bacik 			break;
5359f21246dSJosef Bacik 
5369f21246dSJosef Bacik 		if (extent_start <= start) {
5379f21246dSJosef Bacik 			start = extent_end + 1;
5389f21246dSJosef Bacik 		} else if (extent_start > start && extent_start < end) {
5399f21246dSJosef Bacik 			size = extent_start - start;
540b0643e59SDennis Zhou 			ret = btrfs_add_free_space_async_trimmed(block_group,
541b0643e59SDennis Zhou 								 start, size);
542d8ccbd21SFilipe Manana 			if (ret)
543d8ccbd21SFilipe Manana 				return ret;
544d8ccbd21SFilipe Manana 			if (total_added_ret)
545d8ccbd21SFilipe Manana 				*total_added_ret += size;
5469f21246dSJosef Bacik 			start = extent_end + 1;
5479f21246dSJosef Bacik 		} else {
5489f21246dSJosef Bacik 			break;
5499f21246dSJosef Bacik 		}
5509f21246dSJosef Bacik 	}
5519f21246dSJosef Bacik 
5529f21246dSJosef Bacik 	if (start < end) {
5539f21246dSJosef Bacik 		size = end - start;
554b0643e59SDennis Zhou 		ret = btrfs_add_free_space_async_trimmed(block_group, start,
555b0643e59SDennis Zhou 							 size);
556d8ccbd21SFilipe Manana 		if (ret)
557d8ccbd21SFilipe Manana 			return ret;
558d8ccbd21SFilipe Manana 		if (total_added_ret)
559d8ccbd21SFilipe Manana 			*total_added_ret += size;
5609f21246dSJosef Bacik 	}
5619f21246dSJosef Bacik 
562d8ccbd21SFilipe Manana 	return 0;
5639f21246dSJosef Bacik }
5649f21246dSJosef Bacik 
565c7eec3d9SBoris Burkov /*
566c7eec3d9SBoris Burkov  * Get an arbitrary extent item index / max_index through the block group
567c7eec3d9SBoris Burkov  *
568c7eec3d9SBoris Burkov  * @block_group   the block group to sample from
569c7eec3d9SBoris Burkov  * @index:        the integral step through the block group to grab from
570c7eec3d9SBoris Burkov  * @max_index:    the granularity of the sampling
571c7eec3d9SBoris Burkov  * @key:          return value parameter for the item we find
572c7eec3d9SBoris Burkov  *
573c7eec3d9SBoris Burkov  * Pre-conditions on indices:
574c7eec3d9SBoris Burkov  * 0 <= index <= max_index
575c7eec3d9SBoris Burkov  * 0 < max_index
576c7eec3d9SBoris Burkov  *
577c7eec3d9SBoris Burkov  * Returns: 0 on success, 1 if the search didn't yield a useful item, negative
578c7eec3d9SBoris Burkov  * error code on error.
579c7eec3d9SBoris Burkov  */
580c7eec3d9SBoris Burkov static int sample_block_group_extent_item(struct btrfs_caching_control *caching_ctl,
581c7eec3d9SBoris Burkov 					  struct btrfs_block_group *block_group,
582c7eec3d9SBoris Burkov 					  int index, int max_index,
58312148367SBoris Burkov 					  struct btrfs_key *found_key)
584c7eec3d9SBoris Burkov {
585c7eec3d9SBoris Burkov 	struct btrfs_fs_info *fs_info = block_group->fs_info;
586c7eec3d9SBoris Burkov 	struct btrfs_root *extent_root;
587c7eec3d9SBoris Burkov 	u64 search_offset;
588c7eec3d9SBoris Burkov 	u64 search_end = block_group->start + block_group->length;
589c7eec3d9SBoris Burkov 	struct btrfs_path *path;
59012148367SBoris Burkov 	struct btrfs_key search_key;
59112148367SBoris Burkov 	int ret = 0;
592c7eec3d9SBoris Burkov 
593c7eec3d9SBoris Burkov 	ASSERT(index >= 0);
594c7eec3d9SBoris Burkov 	ASSERT(index <= max_index);
595c7eec3d9SBoris Burkov 	ASSERT(max_index > 0);
596c7eec3d9SBoris Burkov 	lockdep_assert_held(&caching_ctl->mutex);
597c7eec3d9SBoris Burkov 	lockdep_assert_held_read(&fs_info->commit_root_sem);
598c7eec3d9SBoris Burkov 
599c7eec3d9SBoris Burkov 	path = btrfs_alloc_path();
600c7eec3d9SBoris Burkov 	if (!path)
601c7eec3d9SBoris Burkov 		return -ENOMEM;
602c7eec3d9SBoris Burkov 
603c7eec3d9SBoris Burkov 	extent_root = btrfs_extent_root(fs_info, max_t(u64, block_group->start,
604c7eec3d9SBoris Burkov 						       BTRFS_SUPER_INFO_OFFSET));
605c7eec3d9SBoris Burkov 
606c7eec3d9SBoris Burkov 	path->skip_locking = 1;
607c7eec3d9SBoris Burkov 	path->search_commit_root = 1;
608c7eec3d9SBoris Burkov 	path->reada = READA_FORWARD;
609c7eec3d9SBoris Burkov 
610c7eec3d9SBoris Burkov 	search_offset = index * div_u64(block_group->length, max_index);
61112148367SBoris Burkov 	search_key.objectid = block_group->start + search_offset;
61212148367SBoris Burkov 	search_key.type = BTRFS_EXTENT_ITEM_KEY;
61312148367SBoris Burkov 	search_key.offset = 0;
614c7eec3d9SBoris Burkov 
61512148367SBoris Burkov 	btrfs_for_each_slot(extent_root, &search_key, found_key, path, ret) {
616c7eec3d9SBoris Burkov 		/* Success; sampled an extent item in the block group */
61712148367SBoris Burkov 		if (found_key->type == BTRFS_EXTENT_ITEM_KEY &&
61812148367SBoris Burkov 		    found_key->objectid >= block_group->start &&
61912148367SBoris Burkov 		    found_key->objectid + found_key->offset <= search_end)
62012148367SBoris Burkov 			break;
621c7eec3d9SBoris Burkov 
622c7eec3d9SBoris Burkov 		/* We can't possibly find a valid extent item anymore */
62312148367SBoris Burkov 		if (found_key->objectid >= search_end) {
624c7eec3d9SBoris Burkov 			ret = 1;
625c7eec3d9SBoris Burkov 			break;
626c7eec3d9SBoris Burkov 		}
627c7eec3d9SBoris Burkov 	}
62812148367SBoris Burkov 
629c7eec3d9SBoris Burkov 	lockdep_assert_held(&caching_ctl->mutex);
630c7eec3d9SBoris Burkov 	lockdep_assert_held_read(&fs_info->commit_root_sem);
631c7eec3d9SBoris Burkov 	btrfs_free_path(path);
632c7eec3d9SBoris Burkov 	return ret;
633c7eec3d9SBoris Burkov }
634c7eec3d9SBoris Burkov 
635c7eec3d9SBoris Burkov /*
636c7eec3d9SBoris Burkov  * Best effort attempt to compute a block group's size class while caching it.
637c7eec3d9SBoris Burkov  *
638c7eec3d9SBoris Burkov  * @block_group: the block group we are caching
639c7eec3d9SBoris Burkov  *
640c7eec3d9SBoris Burkov  * We cannot infer the size class while adding free space extents, because that
641c7eec3d9SBoris Burkov  * logic doesn't care about contiguous file extents (it doesn't differentiate
642c7eec3d9SBoris Burkov  * between a 100M extent and 100 contiguous 1M extents). So we need to read the
643c7eec3d9SBoris Burkov  * file extent items. Reading all of them is quite wasteful, because usually
644c7eec3d9SBoris Burkov  * only a handful are enough to give a good answer. Therefore, we just grab 5 of
645c7eec3d9SBoris Burkov  * them at even steps through the block group and pick the smallest size class
646c7eec3d9SBoris Burkov  * we see. Since size class is best effort, and not guaranteed in general,
647c7eec3d9SBoris Burkov  * inaccuracy is acceptable.
648c7eec3d9SBoris Burkov  *
649c7eec3d9SBoris Burkov  * To be more explicit about why this algorithm makes sense:
650c7eec3d9SBoris Burkov  *
651c7eec3d9SBoris Burkov  * If we are caching in a block group from disk, then there are three major cases
652c7eec3d9SBoris Burkov  * to consider:
653c7eec3d9SBoris Burkov  * 1. the block group is well behaved and all extents in it are the same size
654c7eec3d9SBoris Burkov  *    class.
655c7eec3d9SBoris Burkov  * 2. the block group is mostly one size class with rare exceptions for last
656c7eec3d9SBoris Burkov  *    ditch allocations
657c7eec3d9SBoris Burkov  * 3. the block group was populated before size classes and can have a totally
658c7eec3d9SBoris Burkov  *    arbitrary mix of size classes.
659c7eec3d9SBoris Burkov  *
660c7eec3d9SBoris Burkov  * In case 1, looking at any extent in the block group will yield the correct
661c7eec3d9SBoris Burkov  * result. For the mixed cases, taking the minimum size class seems like a good
662c7eec3d9SBoris Burkov  * approximation, since gaps from frees will be usable to the size class. For
663c7eec3d9SBoris Burkov  * 2., a small handful of file extents is likely to yield the right answer. For
664c7eec3d9SBoris Burkov  * 3, we can either read every file extent, or admit that this is best effort
665c7eec3d9SBoris Burkov  * anyway and try to stay fast.
666c7eec3d9SBoris Burkov  *
667c7eec3d9SBoris Burkov  * Returns: 0 on success, negative error code on error.
668c7eec3d9SBoris Burkov  */
669c7eec3d9SBoris Burkov static int load_block_group_size_class(struct btrfs_caching_control *caching_ctl,
670c7eec3d9SBoris Burkov 				       struct btrfs_block_group *block_group)
671c7eec3d9SBoris Burkov {
67212148367SBoris Burkov 	struct btrfs_fs_info *fs_info = block_group->fs_info;
673c7eec3d9SBoris Burkov 	struct btrfs_key key;
674c7eec3d9SBoris Burkov 	int i;
675c7eec3d9SBoris Burkov 	u64 min_size = block_group->length;
676c7eec3d9SBoris Burkov 	enum btrfs_block_group_size_class size_class = BTRFS_BG_SZ_NONE;
677c7eec3d9SBoris Burkov 	int ret;
678c7eec3d9SBoris Burkov 
679cb0922f2SBoris Burkov 	if (!btrfs_block_group_should_use_size_class(block_group))
680c7eec3d9SBoris Burkov 		return 0;
681c7eec3d9SBoris Burkov 
68212148367SBoris Burkov 	lockdep_assert_held(&caching_ctl->mutex);
68312148367SBoris Burkov 	lockdep_assert_held_read(&fs_info->commit_root_sem);
684c7eec3d9SBoris Burkov 	for (i = 0; i < 5; ++i) {
685c7eec3d9SBoris Burkov 		ret = sample_block_group_extent_item(caching_ctl, block_group, i, 5, &key);
686c7eec3d9SBoris Burkov 		if (ret < 0)
687c7eec3d9SBoris Burkov 			goto out;
688c7eec3d9SBoris Burkov 		if (ret > 0)
689c7eec3d9SBoris Burkov 			continue;
690c7eec3d9SBoris Burkov 		min_size = min_t(u64, min_size, key.offset);
691c7eec3d9SBoris Burkov 		size_class = btrfs_calc_block_group_size_class(min_size);
692c7eec3d9SBoris Burkov 	}
693c7eec3d9SBoris Burkov 	if (size_class != BTRFS_BG_SZ_NONE) {
694c7eec3d9SBoris Burkov 		spin_lock(&block_group->lock);
695c7eec3d9SBoris Burkov 		block_group->size_class = size_class;
696c7eec3d9SBoris Burkov 		spin_unlock(&block_group->lock);
697c7eec3d9SBoris Burkov 	}
698c7eec3d9SBoris Burkov out:
699c7eec3d9SBoris Burkov 	return ret;
700c7eec3d9SBoris Burkov }
701c7eec3d9SBoris Burkov 
7029f21246dSJosef Bacik static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
7039f21246dSJosef Bacik {
70432da5386SDavid Sterba 	struct btrfs_block_group *block_group = caching_ctl->block_group;
7059f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
70629cbcf40SJosef Bacik 	struct btrfs_root *extent_root;
7079f21246dSJosef Bacik 	struct btrfs_path *path;
7089f21246dSJosef Bacik 	struct extent_buffer *leaf;
7099f21246dSJosef Bacik 	struct btrfs_key key;
7109f21246dSJosef Bacik 	u64 total_found = 0;
7119f21246dSJosef Bacik 	u64 last = 0;
7129f21246dSJosef Bacik 	u32 nritems;
7139f21246dSJosef Bacik 	int ret;
7149f21246dSJosef Bacik 	bool wakeup = true;
7159f21246dSJosef Bacik 
7169f21246dSJosef Bacik 	path = btrfs_alloc_path();
7179f21246dSJosef Bacik 	if (!path)
7189f21246dSJosef Bacik 		return -ENOMEM;
7199f21246dSJosef Bacik 
720b3470b5dSDavid Sterba 	last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET);
72129cbcf40SJosef Bacik 	extent_root = btrfs_extent_root(fs_info, last);
7229f21246dSJosef Bacik 
7239f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
7249f21246dSJosef Bacik 	/*
7259f21246dSJosef Bacik 	 * If we're fragmenting we don't want to make anybody think we can
7269f21246dSJosef Bacik 	 * allocate from this block group until we've had a chance to fragment
7279f21246dSJosef Bacik 	 * the free space.
7289f21246dSJosef Bacik 	 */
7299f21246dSJosef Bacik 	if (btrfs_should_fragment_free_space(block_group))
7309f21246dSJosef Bacik 		wakeup = false;
7319f21246dSJosef Bacik #endif
7329f21246dSJosef Bacik 	/*
7339f21246dSJosef Bacik 	 * We don't want to deadlock with somebody trying to allocate a new
7349f21246dSJosef Bacik 	 * extent for the extent root while also trying to search the extent
7359f21246dSJosef Bacik 	 * root to add free space.  So we skip locking and search the commit
7369f21246dSJosef Bacik 	 * root, since its read-only
7379f21246dSJosef Bacik 	 */
7389f21246dSJosef Bacik 	path->skip_locking = 1;
7399f21246dSJosef Bacik 	path->search_commit_root = 1;
7409f21246dSJosef Bacik 	path->reada = READA_FORWARD;
7419f21246dSJosef Bacik 
7429f21246dSJosef Bacik 	key.objectid = last;
7439f21246dSJosef Bacik 	key.offset = 0;
7449f21246dSJosef Bacik 	key.type = BTRFS_EXTENT_ITEM_KEY;
7459f21246dSJosef Bacik 
7469f21246dSJosef Bacik next:
7479f21246dSJosef Bacik 	ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
7489f21246dSJosef Bacik 	if (ret < 0)
7499f21246dSJosef Bacik 		goto out;
7509f21246dSJosef Bacik 
7519f21246dSJosef Bacik 	leaf = path->nodes[0];
7529f21246dSJosef Bacik 	nritems = btrfs_header_nritems(leaf);
7539f21246dSJosef Bacik 
7549f21246dSJosef Bacik 	while (1) {
7559f21246dSJosef Bacik 		if (btrfs_fs_closing(fs_info) > 1) {
7569f21246dSJosef Bacik 			last = (u64)-1;
7579f21246dSJosef Bacik 			break;
7589f21246dSJosef Bacik 		}
7599f21246dSJosef Bacik 
7609f21246dSJosef Bacik 		if (path->slots[0] < nritems) {
7619f21246dSJosef Bacik 			btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7629f21246dSJosef Bacik 		} else {
7639f21246dSJosef Bacik 			ret = btrfs_find_next_key(extent_root, path, &key, 0, 0);
7649f21246dSJosef Bacik 			if (ret)
7659f21246dSJosef Bacik 				break;
7669f21246dSJosef Bacik 
7679f21246dSJosef Bacik 			if (need_resched() ||
7689f21246dSJosef Bacik 			    rwsem_is_contended(&fs_info->commit_root_sem)) {
7699f21246dSJosef Bacik 				btrfs_release_path(path);
7709f21246dSJosef Bacik 				up_read(&fs_info->commit_root_sem);
7719f21246dSJosef Bacik 				mutex_unlock(&caching_ctl->mutex);
7729f21246dSJosef Bacik 				cond_resched();
7739f21246dSJosef Bacik 				mutex_lock(&caching_ctl->mutex);
7749f21246dSJosef Bacik 				down_read(&fs_info->commit_root_sem);
7759f21246dSJosef Bacik 				goto next;
7769f21246dSJosef Bacik 			}
7779f21246dSJosef Bacik 
7789f21246dSJosef Bacik 			ret = btrfs_next_leaf(extent_root, path);
7799f21246dSJosef Bacik 			if (ret < 0)
7809f21246dSJosef Bacik 				goto out;
7819f21246dSJosef Bacik 			if (ret)
7829f21246dSJosef Bacik 				break;
7839f21246dSJosef Bacik 			leaf = path->nodes[0];
7849f21246dSJosef Bacik 			nritems = btrfs_header_nritems(leaf);
7859f21246dSJosef Bacik 			continue;
7869f21246dSJosef Bacik 		}
7879f21246dSJosef Bacik 
7889f21246dSJosef Bacik 		if (key.objectid < last) {
7899f21246dSJosef Bacik 			key.objectid = last;
7909f21246dSJosef Bacik 			key.offset = 0;
7919f21246dSJosef Bacik 			key.type = BTRFS_EXTENT_ITEM_KEY;
7929f21246dSJosef Bacik 			btrfs_release_path(path);
7939f21246dSJosef Bacik 			goto next;
7949f21246dSJosef Bacik 		}
7959f21246dSJosef Bacik 
796b3470b5dSDavid Sterba 		if (key.objectid < block_group->start) {
7979f21246dSJosef Bacik 			path->slots[0]++;
7989f21246dSJosef Bacik 			continue;
7999f21246dSJosef Bacik 		}
8009f21246dSJosef Bacik 
801b3470b5dSDavid Sterba 		if (key.objectid >= block_group->start + block_group->length)
8029f21246dSJosef Bacik 			break;
8039f21246dSJosef Bacik 
8049f21246dSJosef Bacik 		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
8059f21246dSJosef Bacik 		    key.type == BTRFS_METADATA_ITEM_KEY) {
806d8ccbd21SFilipe Manana 			u64 space_added;
807d8ccbd21SFilipe Manana 
8083b9f0995SFilipe Manana 			ret = btrfs_add_new_free_space(block_group, last,
8093b9f0995SFilipe Manana 						       key.objectid, &space_added);
810d8ccbd21SFilipe Manana 			if (ret)
811d8ccbd21SFilipe Manana 				goto out;
812d8ccbd21SFilipe Manana 			total_found += space_added;
8139f21246dSJosef Bacik 			if (key.type == BTRFS_METADATA_ITEM_KEY)
8149f21246dSJosef Bacik 				last = key.objectid +
8159f21246dSJosef Bacik 					fs_info->nodesize;
8169f21246dSJosef Bacik 			else
8179f21246dSJosef Bacik 				last = key.objectid + key.offset;
8189f21246dSJosef Bacik 
8199f21246dSJosef Bacik 			if (total_found > CACHING_CTL_WAKE_UP) {
8209f21246dSJosef Bacik 				total_found = 0;
821fc1f91b9SJosef Bacik 				if (wakeup) {
822fc1f91b9SJosef Bacik 					atomic_inc(&caching_ctl->progress);
8239f21246dSJosef Bacik 					wake_up(&caching_ctl->wait);
8249f21246dSJosef Bacik 				}
8259f21246dSJosef Bacik 			}
826fc1f91b9SJosef Bacik 		}
8279f21246dSJosef Bacik 		path->slots[0]++;
8289f21246dSJosef Bacik 	}
8299f21246dSJosef Bacik 
8303b9f0995SFilipe Manana 	ret = btrfs_add_new_free_space(block_group, last,
831d8ccbd21SFilipe Manana 				       block_group->start + block_group->length,
832d8ccbd21SFilipe Manana 				       NULL);
8339f21246dSJosef Bacik out:
8349f21246dSJosef Bacik 	btrfs_free_path(path);
8359f21246dSJosef Bacik 	return ret;
8369f21246dSJosef Bacik }
8379f21246dSJosef Bacik 
83898b5a8fdSFilipe Manana static inline void btrfs_free_excluded_extents(const struct btrfs_block_group *bg)
83998b5a8fdSFilipe Manana {
84098b5a8fdSFilipe Manana 	clear_extent_bits(&bg->fs_info->excluded_extents, bg->start,
84198b5a8fdSFilipe Manana 			  bg->start + bg->length - 1, EXTENT_UPTODATE);
84298b5a8fdSFilipe Manana }
84398b5a8fdSFilipe Manana 
8449f21246dSJosef Bacik static noinline void caching_thread(struct btrfs_work *work)
8459f21246dSJosef Bacik {
84632da5386SDavid Sterba 	struct btrfs_block_group *block_group;
8479f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info;
8489f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
8499f21246dSJosef Bacik 	int ret;
8509f21246dSJosef Bacik 
8519f21246dSJosef Bacik 	caching_ctl = container_of(work, struct btrfs_caching_control, work);
8529f21246dSJosef Bacik 	block_group = caching_ctl->block_group;
8539f21246dSJosef Bacik 	fs_info = block_group->fs_info;
8549f21246dSJosef Bacik 
8559f21246dSJosef Bacik 	mutex_lock(&caching_ctl->mutex);
8569f21246dSJosef Bacik 	down_read(&fs_info->commit_root_sem);
8579f21246dSJosef Bacik 
858c7eec3d9SBoris Burkov 	load_block_group_size_class(caching_ctl, block_group);
859e747853cSJosef Bacik 	if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
860e747853cSJosef Bacik 		ret = load_free_space_cache(block_group);
861e747853cSJosef Bacik 		if (ret == 1) {
862e747853cSJosef Bacik 			ret = 0;
863e747853cSJosef Bacik 			goto done;
864e747853cSJosef Bacik 		}
865e747853cSJosef Bacik 
866e747853cSJosef Bacik 		/*
867e747853cSJosef Bacik 		 * We failed to load the space cache, set ourselves to
868e747853cSJosef Bacik 		 * CACHE_STARTED and carry on.
869e747853cSJosef Bacik 		 */
870e747853cSJosef Bacik 		spin_lock(&block_group->lock);
871e747853cSJosef Bacik 		block_group->cached = BTRFS_CACHE_STARTED;
872e747853cSJosef Bacik 		spin_unlock(&block_group->lock);
873e747853cSJosef Bacik 		wake_up(&caching_ctl->wait);
874e747853cSJosef Bacik 	}
875e747853cSJosef Bacik 
8762f96e402SJosef Bacik 	/*
8772f96e402SJosef Bacik 	 * If we are in the transaction that populated the free space tree we
8782f96e402SJosef Bacik 	 * can't actually cache from the free space tree as our commit root and
8792f96e402SJosef Bacik 	 * real root are the same, so we could change the contents of the blocks
8802f96e402SJosef Bacik 	 * while caching.  Instead do the slow caching in this case, and after
8812f96e402SJosef Bacik 	 * the transaction has committed we will be safe.
8822f96e402SJosef Bacik 	 */
8832f96e402SJosef Bacik 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
8842f96e402SJosef Bacik 	    !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags)))
8859f21246dSJosef Bacik 		ret = load_free_space_tree(caching_ctl);
8869f21246dSJosef Bacik 	else
8879f21246dSJosef Bacik 		ret = load_extent_tree_free(caching_ctl);
888e747853cSJosef Bacik done:
8899f21246dSJosef Bacik 	spin_lock(&block_group->lock);
8909f21246dSJosef Bacik 	block_group->caching_ctl = NULL;
8919f21246dSJosef Bacik 	block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
8929f21246dSJosef Bacik 	spin_unlock(&block_group->lock);
8939f21246dSJosef Bacik 
8949f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
8959f21246dSJosef Bacik 	if (btrfs_should_fragment_free_space(block_group)) {
8969f21246dSJosef Bacik 		u64 bytes_used;
8979f21246dSJosef Bacik 
8989f21246dSJosef Bacik 		spin_lock(&block_group->space_info->lock);
8999f21246dSJosef Bacik 		spin_lock(&block_group->lock);
900b3470b5dSDavid Sterba 		bytes_used = block_group->length - block_group->used;
9019f21246dSJosef Bacik 		block_group->space_info->bytes_used += bytes_used >> 1;
9029f21246dSJosef Bacik 		spin_unlock(&block_group->lock);
9039f21246dSJosef Bacik 		spin_unlock(&block_group->space_info->lock);
904e11c0406SJosef Bacik 		fragment_free_space(block_group);
9059f21246dSJosef Bacik 	}
9069f21246dSJosef Bacik #endif
9079f21246dSJosef Bacik 
9089f21246dSJosef Bacik 	up_read(&fs_info->commit_root_sem);
9099f21246dSJosef Bacik 	btrfs_free_excluded_extents(block_group);
9109f21246dSJosef Bacik 	mutex_unlock(&caching_ctl->mutex);
9119f21246dSJosef Bacik 
9129f21246dSJosef Bacik 	wake_up(&caching_ctl->wait);
9139f21246dSJosef Bacik 
9149f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
9159f21246dSJosef Bacik 	btrfs_put_block_group(block_group);
9169f21246dSJosef Bacik }
9179f21246dSJosef Bacik 
918ced8ecf0SOmar Sandoval int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait)
9199f21246dSJosef Bacik {
9209f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
921e747853cSJosef Bacik 	struct btrfs_caching_control *caching_ctl = NULL;
9229f21246dSJosef Bacik 	int ret = 0;
9239f21246dSJosef Bacik 
9242eda5708SNaohiro Aota 	/* Allocator for zoned filesystems does not use the cache at all */
9252eda5708SNaohiro Aota 	if (btrfs_is_zoned(fs_info))
9262eda5708SNaohiro Aota 		return 0;
9272eda5708SNaohiro Aota 
9289f21246dSJosef Bacik 	caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
9299f21246dSJosef Bacik 	if (!caching_ctl)
9309f21246dSJosef Bacik 		return -ENOMEM;
9319f21246dSJosef Bacik 
9329f21246dSJosef Bacik 	INIT_LIST_HEAD(&caching_ctl->list);
9339f21246dSJosef Bacik 	mutex_init(&caching_ctl->mutex);
9349f21246dSJosef Bacik 	init_waitqueue_head(&caching_ctl->wait);
9359f21246dSJosef Bacik 	caching_ctl->block_group = cache;
936e747853cSJosef Bacik 	refcount_set(&caching_ctl->count, 2);
937fc1f91b9SJosef Bacik 	atomic_set(&caching_ctl->progress, 0);
938a0cac0ecSOmar Sandoval 	btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
9399f21246dSJosef Bacik 
9409f21246dSJosef Bacik 	spin_lock(&cache->lock);
9419f21246dSJosef Bacik 	if (cache->cached != BTRFS_CACHE_NO) {
9429f21246dSJosef Bacik 		kfree(caching_ctl);
943e747853cSJosef Bacik 
944e747853cSJosef Bacik 		caching_ctl = cache->caching_ctl;
945e747853cSJosef Bacik 		if (caching_ctl)
946e747853cSJosef Bacik 			refcount_inc(&caching_ctl->count);
947e747853cSJosef Bacik 		spin_unlock(&cache->lock);
948e747853cSJosef Bacik 		goto out;
9499f21246dSJosef Bacik 	}
9509f21246dSJosef Bacik 	WARN_ON(cache->caching_ctl);
9519f21246dSJosef Bacik 	cache->caching_ctl = caching_ctl;
9529f21246dSJosef Bacik 	cache->cached = BTRFS_CACHE_STARTED;
9539f21246dSJosef Bacik 	spin_unlock(&cache->lock);
9549f21246dSJosef Bacik 
95516b0c258SFilipe Manana 	write_lock(&fs_info->block_group_cache_lock);
9569f21246dSJosef Bacik 	refcount_inc(&caching_ctl->count);
9579f21246dSJosef Bacik 	list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
95816b0c258SFilipe Manana 	write_unlock(&fs_info->block_group_cache_lock);
9599f21246dSJosef Bacik 
9609f21246dSJosef Bacik 	btrfs_get_block_group(cache);
9619f21246dSJosef Bacik 
9629f21246dSJosef Bacik 	btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
963e747853cSJosef Bacik out:
964ced8ecf0SOmar Sandoval 	if (wait && caching_ctl)
965ced8ecf0SOmar Sandoval 		ret = btrfs_caching_ctl_wait_done(cache, caching_ctl);
966e747853cSJosef Bacik 	if (caching_ctl)
967e747853cSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
9689f21246dSJosef Bacik 
9699f21246dSJosef Bacik 	return ret;
9709f21246dSJosef Bacik }
971e3e0520bSJosef Bacik 
972e3e0520bSJosef Bacik static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
973e3e0520bSJosef Bacik {
974e3e0520bSJosef Bacik 	u64 extra_flags = chunk_to_extended(flags) &
975e3e0520bSJosef Bacik 				BTRFS_EXTENDED_PROFILE_MASK;
976e3e0520bSJosef Bacik 
977e3e0520bSJosef Bacik 	write_seqlock(&fs_info->profiles_lock);
978e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA)
979e3e0520bSJosef Bacik 		fs_info->avail_data_alloc_bits &= ~extra_flags;
980e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_METADATA)
981e3e0520bSJosef Bacik 		fs_info->avail_metadata_alloc_bits &= ~extra_flags;
982e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
983e3e0520bSJosef Bacik 		fs_info->avail_system_alloc_bits &= ~extra_flags;
984e3e0520bSJosef Bacik 	write_sequnlock(&fs_info->profiles_lock);
985e3e0520bSJosef Bacik }
986e3e0520bSJosef Bacik 
987e3e0520bSJosef Bacik /*
988e3e0520bSJosef Bacik  * Clear incompat bits for the following feature(s):
989e3e0520bSJosef Bacik  *
990e3e0520bSJosef Bacik  * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
991e3e0520bSJosef Bacik  *            in the whole filesystem
9929c907446SDavid Sterba  *
9939c907446SDavid Sterba  * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
994e3e0520bSJosef Bacik  */
995e3e0520bSJosef Bacik static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags)
996e3e0520bSJosef Bacik {
9979c907446SDavid Sterba 	bool found_raid56 = false;
9989c907446SDavid Sterba 	bool found_raid1c34 = false;
9999c907446SDavid Sterba 
10009c907446SDavid Sterba 	if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) ||
10019c907446SDavid Sterba 	    (flags & BTRFS_BLOCK_GROUP_RAID1C3) ||
10029c907446SDavid Sterba 	    (flags & BTRFS_BLOCK_GROUP_RAID1C4)) {
1003e3e0520bSJosef Bacik 		struct list_head *head = &fs_info->space_info;
1004e3e0520bSJosef Bacik 		struct btrfs_space_info *sinfo;
1005e3e0520bSJosef Bacik 
1006e3e0520bSJosef Bacik 		list_for_each_entry_rcu(sinfo, head, list) {
1007e3e0520bSJosef Bacik 			down_read(&sinfo->groups_sem);
1008e3e0520bSJosef Bacik 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5]))
10099c907446SDavid Sterba 				found_raid56 = true;
1010e3e0520bSJosef Bacik 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6]))
10119c907446SDavid Sterba 				found_raid56 = true;
10129c907446SDavid Sterba 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3]))
10139c907446SDavid Sterba 				found_raid1c34 = true;
10149c907446SDavid Sterba 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4]))
10159c907446SDavid Sterba 				found_raid1c34 = true;
1016e3e0520bSJosef Bacik 			up_read(&sinfo->groups_sem);
1017e3e0520bSJosef Bacik 		}
1018d8e6fd5cSFilipe Manana 		if (!found_raid56)
1019e3e0520bSJosef Bacik 			btrfs_clear_fs_incompat(fs_info, RAID56);
1020d8e6fd5cSFilipe Manana 		if (!found_raid1c34)
10219c907446SDavid Sterba 			btrfs_clear_fs_incompat(fs_info, RAID1C34);
1022e3e0520bSJosef Bacik 	}
1023e3e0520bSJosef Bacik }
1024e3e0520bSJosef Bacik 
10257357623aSQu Wenruo static int remove_block_group_item(struct btrfs_trans_handle *trans,
10267357623aSQu Wenruo 				   struct btrfs_path *path,
10277357623aSQu Wenruo 				   struct btrfs_block_group *block_group)
10287357623aSQu Wenruo {
10297357623aSQu Wenruo 	struct btrfs_fs_info *fs_info = trans->fs_info;
10307357623aSQu Wenruo 	struct btrfs_root *root;
10317357623aSQu Wenruo 	struct btrfs_key key;
10327357623aSQu Wenruo 	int ret;
10337357623aSQu Wenruo 
1034dfe8aec4SJosef Bacik 	root = btrfs_block_group_root(fs_info);
10357357623aSQu Wenruo 	key.objectid = block_group->start;
10367357623aSQu Wenruo 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
10377357623aSQu Wenruo 	key.offset = block_group->length;
10387357623aSQu Wenruo 
10397357623aSQu Wenruo 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10407357623aSQu Wenruo 	if (ret > 0)
10417357623aSQu Wenruo 		ret = -ENOENT;
10427357623aSQu Wenruo 	if (ret < 0)
10437357623aSQu Wenruo 		return ret;
10447357623aSQu Wenruo 
10457357623aSQu Wenruo 	ret = btrfs_del_item(trans, root, path);
10467357623aSQu Wenruo 	return ret;
10477357623aSQu Wenruo }
10487357623aSQu Wenruo 
1049e3e0520bSJosef Bacik int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
1050e3e0520bSJosef Bacik 			     u64 group_start, struct extent_map *em)
1051e3e0520bSJosef Bacik {
1052e3e0520bSJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
1053e3e0520bSJosef Bacik 	struct btrfs_path *path;
105432da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1055e3e0520bSJosef Bacik 	struct btrfs_free_cluster *cluster;
1056e3e0520bSJosef Bacik 	struct inode *inode;
1057e3e0520bSJosef Bacik 	struct kobject *kobj = NULL;
1058e3e0520bSJosef Bacik 	int ret;
1059e3e0520bSJosef Bacik 	int index;
1060e3e0520bSJosef Bacik 	int factor;
1061e3e0520bSJosef Bacik 	struct btrfs_caching_control *caching_ctl = NULL;
1062e3e0520bSJosef Bacik 	bool remove_em;
1063e3e0520bSJosef Bacik 	bool remove_rsv = false;
1064e3e0520bSJosef Bacik 
1065e3e0520bSJosef Bacik 	block_group = btrfs_lookup_block_group(fs_info, group_start);
1066e3e0520bSJosef Bacik 	BUG_ON(!block_group);
1067e3e0520bSJosef Bacik 	BUG_ON(!block_group->ro);
1068e3e0520bSJosef Bacik 
1069e3e0520bSJosef Bacik 	trace_btrfs_remove_block_group(block_group);
1070e3e0520bSJosef Bacik 	/*
1071e3e0520bSJosef Bacik 	 * Free the reserved super bytes from this block group before
1072e3e0520bSJosef Bacik 	 * remove it.
1073e3e0520bSJosef Bacik 	 */
1074e3e0520bSJosef Bacik 	btrfs_free_excluded_extents(block_group);
1075b3470b5dSDavid Sterba 	btrfs_free_ref_tree_range(fs_info, block_group->start,
1076b3470b5dSDavid Sterba 				  block_group->length);
1077e3e0520bSJosef Bacik 
1078e3e0520bSJosef Bacik 	index = btrfs_bg_flags_to_raid_index(block_group->flags);
1079e3e0520bSJosef Bacik 	factor = btrfs_bg_type_to_factor(block_group->flags);
1080e3e0520bSJosef Bacik 
1081e3e0520bSJosef Bacik 	/* make sure this block group isn't part of an allocation cluster */
1082e3e0520bSJosef Bacik 	cluster = &fs_info->data_alloc_cluster;
1083e3e0520bSJosef Bacik 	spin_lock(&cluster->refill_lock);
1084e3e0520bSJosef Bacik 	btrfs_return_cluster_to_free_space(block_group, cluster);
1085e3e0520bSJosef Bacik 	spin_unlock(&cluster->refill_lock);
1086e3e0520bSJosef Bacik 
1087e3e0520bSJosef Bacik 	/*
1088e3e0520bSJosef Bacik 	 * make sure this block group isn't part of a metadata
1089e3e0520bSJosef Bacik 	 * allocation cluster
1090e3e0520bSJosef Bacik 	 */
1091e3e0520bSJosef Bacik 	cluster = &fs_info->meta_alloc_cluster;
1092e3e0520bSJosef Bacik 	spin_lock(&cluster->refill_lock);
1093e3e0520bSJosef Bacik 	btrfs_return_cluster_to_free_space(block_group, cluster);
1094e3e0520bSJosef Bacik 	spin_unlock(&cluster->refill_lock);
1095e3e0520bSJosef Bacik 
109640ab3be1SNaohiro Aota 	btrfs_clear_treelog_bg(block_group);
1097c2707a25SJohannes Thumshirn 	btrfs_clear_data_reloc_bg(block_group);
109840ab3be1SNaohiro Aota 
1099e3e0520bSJosef Bacik 	path = btrfs_alloc_path();
1100e3e0520bSJosef Bacik 	if (!path) {
1101e3e0520bSJosef Bacik 		ret = -ENOMEM;
11029fecd132SFilipe Manana 		goto out;
1103e3e0520bSJosef Bacik 	}
1104e3e0520bSJosef Bacik 
1105e3e0520bSJosef Bacik 	/*
1106e3e0520bSJosef Bacik 	 * get the inode first so any iput calls done for the io_list
1107e3e0520bSJosef Bacik 	 * aren't the final iput (no unlinks allowed now)
1108e3e0520bSJosef Bacik 	 */
1109e3e0520bSJosef Bacik 	inode = lookup_free_space_inode(block_group, path);
1110e3e0520bSJosef Bacik 
1111e3e0520bSJosef Bacik 	mutex_lock(&trans->transaction->cache_write_mutex);
1112e3e0520bSJosef Bacik 	/*
1113e3e0520bSJosef Bacik 	 * Make sure our free space cache IO is done before removing the
1114e3e0520bSJosef Bacik 	 * free space inode
1115e3e0520bSJosef Bacik 	 */
1116e3e0520bSJosef Bacik 	spin_lock(&trans->transaction->dirty_bgs_lock);
1117e3e0520bSJosef Bacik 	if (!list_empty(&block_group->io_list)) {
1118e3e0520bSJosef Bacik 		list_del_init(&block_group->io_list);
1119e3e0520bSJosef Bacik 
1120e3e0520bSJosef Bacik 		WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
1121e3e0520bSJosef Bacik 
1122e3e0520bSJosef Bacik 		spin_unlock(&trans->transaction->dirty_bgs_lock);
1123e3e0520bSJosef Bacik 		btrfs_wait_cache_io(trans, block_group, path);
1124e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
1125e3e0520bSJosef Bacik 		spin_lock(&trans->transaction->dirty_bgs_lock);
1126e3e0520bSJosef Bacik 	}
1127e3e0520bSJosef Bacik 
1128e3e0520bSJosef Bacik 	if (!list_empty(&block_group->dirty_list)) {
1129e3e0520bSJosef Bacik 		list_del_init(&block_group->dirty_list);
1130e3e0520bSJosef Bacik 		remove_rsv = true;
1131e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
1132e3e0520bSJosef Bacik 	}
1133e3e0520bSJosef Bacik 	spin_unlock(&trans->transaction->dirty_bgs_lock);
1134e3e0520bSJosef Bacik 	mutex_unlock(&trans->transaction->cache_write_mutex);
1135e3e0520bSJosef Bacik 
113636b216c8SBoris Burkov 	ret = btrfs_remove_free_space_inode(trans, inode, block_group);
1137e3e0520bSJosef Bacik 	if (ret)
11389fecd132SFilipe Manana 		goto out;
1139e3e0520bSJosef Bacik 
114016b0c258SFilipe Manana 	write_lock(&fs_info->block_group_cache_lock);
114108dddb29SFilipe Manana 	rb_erase_cached(&block_group->cache_node,
1142e3e0520bSJosef Bacik 			&fs_info->block_group_cache_tree);
1143e3e0520bSJosef Bacik 	RB_CLEAR_NODE(&block_group->cache_node);
1144e3e0520bSJosef Bacik 
11459fecd132SFilipe Manana 	/* Once for the block groups rbtree */
11469fecd132SFilipe Manana 	btrfs_put_block_group(block_group);
11479fecd132SFilipe Manana 
114816b0c258SFilipe Manana 	write_unlock(&fs_info->block_group_cache_lock);
1149e3e0520bSJosef Bacik 
1150e3e0520bSJosef Bacik 	down_write(&block_group->space_info->groups_sem);
1151e3e0520bSJosef Bacik 	/*
1152e3e0520bSJosef Bacik 	 * we must use list_del_init so people can check to see if they
1153e3e0520bSJosef Bacik 	 * are still on the list after taking the semaphore
1154e3e0520bSJosef Bacik 	 */
1155e3e0520bSJosef Bacik 	list_del_init(&block_group->list);
1156e3e0520bSJosef Bacik 	if (list_empty(&block_group->space_info->block_groups[index])) {
1157e3e0520bSJosef Bacik 		kobj = block_group->space_info->block_group_kobjs[index];
1158e3e0520bSJosef Bacik 		block_group->space_info->block_group_kobjs[index] = NULL;
1159e3e0520bSJosef Bacik 		clear_avail_alloc_bits(fs_info, block_group->flags);
1160e3e0520bSJosef Bacik 	}
1161e3e0520bSJosef Bacik 	up_write(&block_group->space_info->groups_sem);
1162e3e0520bSJosef Bacik 	clear_incompat_bg_bits(fs_info, block_group->flags);
1163e3e0520bSJosef Bacik 	if (kobj) {
1164e3e0520bSJosef Bacik 		kobject_del(kobj);
1165e3e0520bSJosef Bacik 		kobject_put(kobj);
1166e3e0520bSJosef Bacik 	}
1167e3e0520bSJosef Bacik 
1168e3e0520bSJosef Bacik 	if (block_group->cached == BTRFS_CACHE_STARTED)
1169e3e0520bSJosef Bacik 		btrfs_wait_block_group_cache_done(block_group);
11707b9c293bSJosef Bacik 
117116b0c258SFilipe Manana 	write_lock(&fs_info->block_group_cache_lock);
11727b9c293bSJosef Bacik 	caching_ctl = btrfs_get_caching_control(block_group);
1173e3e0520bSJosef Bacik 	if (!caching_ctl) {
1174e3e0520bSJosef Bacik 		struct btrfs_caching_control *ctl;
1175e3e0520bSJosef Bacik 
11767b9c293bSJosef Bacik 		list_for_each_entry(ctl, &fs_info->caching_block_groups, list) {
1177e3e0520bSJosef Bacik 			if (ctl->block_group == block_group) {
1178e3e0520bSJosef Bacik 				caching_ctl = ctl;
1179e3e0520bSJosef Bacik 				refcount_inc(&caching_ctl->count);
1180e3e0520bSJosef Bacik 				break;
1181e3e0520bSJosef Bacik 			}
1182e3e0520bSJosef Bacik 		}
11837b9c293bSJosef Bacik 	}
1184e3e0520bSJosef Bacik 	if (caching_ctl)
1185e3e0520bSJosef Bacik 		list_del_init(&caching_ctl->list);
118616b0c258SFilipe Manana 	write_unlock(&fs_info->block_group_cache_lock);
11877b9c293bSJosef Bacik 
1188e3e0520bSJosef Bacik 	if (caching_ctl) {
1189e3e0520bSJosef Bacik 		/* Once for the caching bgs list and once for us. */
1190e3e0520bSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
1191e3e0520bSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
1192e3e0520bSJosef Bacik 	}
1193e3e0520bSJosef Bacik 
1194e3e0520bSJosef Bacik 	spin_lock(&trans->transaction->dirty_bgs_lock);
1195e3e0520bSJosef Bacik 	WARN_ON(!list_empty(&block_group->dirty_list));
1196e3e0520bSJosef Bacik 	WARN_ON(!list_empty(&block_group->io_list));
1197e3e0520bSJosef Bacik 	spin_unlock(&trans->transaction->dirty_bgs_lock);
1198e3e0520bSJosef Bacik 
1199e3e0520bSJosef Bacik 	btrfs_remove_free_space_cache(block_group);
1200e3e0520bSJosef Bacik 
1201e3e0520bSJosef Bacik 	spin_lock(&block_group->space_info->lock);
1202e3e0520bSJosef Bacik 	list_del_init(&block_group->ro_list);
1203e3e0520bSJosef Bacik 
1204e3e0520bSJosef Bacik 	if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
1205e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->total_bytes
1206b3470b5dSDavid Sterba 			< block_group->length);
1207e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->bytes_readonly
1208169e0da9SNaohiro Aota 			< block_group->length - block_group->zone_unusable);
1209169e0da9SNaohiro Aota 		WARN_ON(block_group->space_info->bytes_zone_unusable
1210169e0da9SNaohiro Aota 			< block_group->zone_unusable);
1211e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->disk_total
1212b3470b5dSDavid Sterba 			< block_group->length * factor);
1213e3e0520bSJosef Bacik 	}
1214b3470b5dSDavid Sterba 	block_group->space_info->total_bytes -= block_group->length;
1215169e0da9SNaohiro Aota 	block_group->space_info->bytes_readonly -=
1216169e0da9SNaohiro Aota 		(block_group->length - block_group->zone_unusable);
1217169e0da9SNaohiro Aota 	block_group->space_info->bytes_zone_unusable -=
1218169e0da9SNaohiro Aota 		block_group->zone_unusable;
1219b3470b5dSDavid Sterba 	block_group->space_info->disk_total -= block_group->length * factor;
1220e3e0520bSJosef Bacik 
1221e3e0520bSJosef Bacik 	spin_unlock(&block_group->space_info->lock);
1222e3e0520bSJosef Bacik 
1223ffcb9d44SFilipe Manana 	/*
1224ffcb9d44SFilipe Manana 	 * Remove the free space for the block group from the free space tree
1225ffcb9d44SFilipe Manana 	 * and the block group's item from the extent tree before marking the
1226ffcb9d44SFilipe Manana 	 * block group as removed. This is to prevent races with tasks that
1227ffcb9d44SFilipe Manana 	 * freeze and unfreeze a block group, this task and another task
1228ffcb9d44SFilipe Manana 	 * allocating a new block group - the unfreeze task ends up removing
1229ffcb9d44SFilipe Manana 	 * the block group's extent map before the task calling this function
1230ffcb9d44SFilipe Manana 	 * deletes the block group item from the extent tree, allowing for
1231ffcb9d44SFilipe Manana 	 * another task to attempt to create another block group with the same
1232ffcb9d44SFilipe Manana 	 * item key (and failing with -EEXIST and a transaction abort).
1233ffcb9d44SFilipe Manana 	 */
1234ffcb9d44SFilipe Manana 	ret = remove_block_group_free_space(trans, block_group);
1235ffcb9d44SFilipe Manana 	if (ret)
1236ffcb9d44SFilipe Manana 		goto out;
1237ffcb9d44SFilipe Manana 
1238ffcb9d44SFilipe Manana 	ret = remove_block_group_item(trans, path, block_group);
1239ffcb9d44SFilipe Manana 	if (ret < 0)
1240ffcb9d44SFilipe Manana 		goto out;
1241ffcb9d44SFilipe Manana 
1242e3e0520bSJosef Bacik 	spin_lock(&block_group->lock);
12433349b57fSJosef Bacik 	set_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags);
12443349b57fSJosef Bacik 
1245e3e0520bSJosef Bacik 	/*
12466b7304afSFilipe Manana 	 * At this point trimming or scrub can't start on this block group,
12476b7304afSFilipe Manana 	 * because we removed the block group from the rbtree
12486b7304afSFilipe Manana 	 * fs_info->block_group_cache_tree so no one can't find it anymore and
12496b7304afSFilipe Manana 	 * even if someone already got this block group before we removed it
12506b7304afSFilipe Manana 	 * from the rbtree, they have already incremented block_group->frozen -
12516b7304afSFilipe Manana 	 * if they didn't, for the trimming case they won't find any free space
12526b7304afSFilipe Manana 	 * entries because we already removed them all when we called
12536b7304afSFilipe Manana 	 * btrfs_remove_free_space_cache().
1254e3e0520bSJosef Bacik 	 *
1255e3e0520bSJosef Bacik 	 * And we must not remove the extent map from the fs_info->mapping_tree
1256e3e0520bSJosef Bacik 	 * to prevent the same logical address range and physical device space
12576b7304afSFilipe Manana 	 * ranges from being reused for a new block group. This is needed to
12586b7304afSFilipe Manana 	 * avoid races with trimming and scrub.
12596b7304afSFilipe Manana 	 *
12606b7304afSFilipe Manana 	 * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
1261e3e0520bSJosef Bacik 	 * completely transactionless, so while it is trimming a range the
1262e3e0520bSJosef Bacik 	 * currently running transaction might finish and a new one start,
1263e3e0520bSJosef Bacik 	 * allowing for new block groups to be created that can reuse the same
1264e3e0520bSJosef Bacik 	 * physical device locations unless we take this special care.
1265e3e0520bSJosef Bacik 	 *
1266e3e0520bSJosef Bacik 	 * There may also be an implicit trim operation if the file system
1267e3e0520bSJosef Bacik 	 * is mounted with -odiscard. The same protections must remain
1268e3e0520bSJosef Bacik 	 * in place until the extents have been discarded completely when
1269e3e0520bSJosef Bacik 	 * the transaction commit has completed.
1270e3e0520bSJosef Bacik 	 */
12716b7304afSFilipe Manana 	remove_em = (atomic_read(&block_group->frozen) == 0);
1272e3e0520bSJosef Bacik 	spin_unlock(&block_group->lock);
1273e3e0520bSJosef Bacik 
1274e3e0520bSJosef Bacik 	if (remove_em) {
1275e3e0520bSJosef Bacik 		struct extent_map_tree *em_tree;
1276e3e0520bSJosef Bacik 
1277e3e0520bSJosef Bacik 		em_tree = &fs_info->mapping_tree;
1278e3e0520bSJosef Bacik 		write_lock(&em_tree->lock);
1279e3e0520bSJosef Bacik 		remove_extent_mapping(em_tree, em);
1280e3e0520bSJosef Bacik 		write_unlock(&em_tree->lock);
1281e3e0520bSJosef Bacik 		/* once for the tree */
1282e3e0520bSJosef Bacik 		free_extent_map(em);
1283e3e0520bSJosef Bacik 	}
1284f6033c5eSXiyu Yang 
12859fecd132SFilipe Manana out:
1286f6033c5eSXiyu Yang 	/* Once for the lookup reference */
1287f6033c5eSXiyu Yang 	btrfs_put_block_group(block_group);
1288e3e0520bSJosef Bacik 	if (remove_rsv)
1289adb86dbeSFilipe Manana 		btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
1290e3e0520bSJosef Bacik 	btrfs_free_path(path);
1291e3e0520bSJosef Bacik 	return ret;
1292e3e0520bSJosef Bacik }
1293e3e0520bSJosef Bacik 
1294e3e0520bSJosef Bacik struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
1295e3e0520bSJosef Bacik 		struct btrfs_fs_info *fs_info, const u64 chunk_offset)
1296e3e0520bSJosef Bacik {
1297dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
1298e3e0520bSJosef Bacik 	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1299e3e0520bSJosef Bacik 	struct extent_map *em;
1300e3e0520bSJosef Bacik 	struct map_lookup *map;
1301e3e0520bSJosef Bacik 	unsigned int num_items;
1302e3e0520bSJosef Bacik 
1303e3e0520bSJosef Bacik 	read_lock(&em_tree->lock);
1304e3e0520bSJosef Bacik 	em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1305e3e0520bSJosef Bacik 	read_unlock(&em_tree->lock);
1306e3e0520bSJosef Bacik 	ASSERT(em && em->start == chunk_offset);
1307e3e0520bSJosef Bacik 
1308e3e0520bSJosef Bacik 	/*
1309e3e0520bSJosef Bacik 	 * We need to reserve 3 + N units from the metadata space info in order
1310e3e0520bSJosef Bacik 	 * to remove a block group (done at btrfs_remove_chunk() and at
1311e3e0520bSJosef Bacik 	 * btrfs_remove_block_group()), which are used for:
1312e3e0520bSJosef Bacik 	 *
1313e3e0520bSJosef Bacik 	 * 1 unit for adding the free space inode's orphan (located in the tree
1314e3e0520bSJosef Bacik 	 * of tree roots).
1315e3e0520bSJosef Bacik 	 * 1 unit for deleting the block group item (located in the extent
1316e3e0520bSJosef Bacik 	 * tree).
1317e3e0520bSJosef Bacik 	 * 1 unit for deleting the free space item (located in tree of tree
1318e3e0520bSJosef Bacik 	 * roots).
1319e3e0520bSJosef Bacik 	 * N units for deleting N device extent items corresponding to each
1320e3e0520bSJosef Bacik 	 * stripe (located in the device tree).
1321e3e0520bSJosef Bacik 	 *
1322e3e0520bSJosef Bacik 	 * In order to remove a block group we also need to reserve units in the
1323e3e0520bSJosef Bacik 	 * system space info in order to update the chunk tree (update one or
1324e3e0520bSJosef Bacik 	 * more device items and remove one chunk item), but this is done at
1325e3e0520bSJosef Bacik 	 * btrfs_remove_chunk() through a call to check_system_chunk().
1326e3e0520bSJosef Bacik 	 */
1327e3e0520bSJosef Bacik 	map = em->map_lookup;
1328e3e0520bSJosef Bacik 	num_items = 3 + map->num_stripes;
1329e3e0520bSJosef Bacik 	free_extent_map(em);
1330e3e0520bSJosef Bacik 
1331dfe8aec4SJosef Bacik 	return btrfs_start_transaction_fallback_global_rsv(root, num_items);
1332e3e0520bSJosef Bacik }
1333e3e0520bSJosef Bacik 
1334e3e0520bSJosef Bacik /*
133526ce2095SJosef Bacik  * Mark block group @cache read-only, so later write won't happen to block
133626ce2095SJosef Bacik  * group @cache.
133726ce2095SJosef Bacik  *
133826ce2095SJosef Bacik  * If @force is not set, this function will only mark the block group readonly
133926ce2095SJosef Bacik  * if we have enough free space (1M) in other metadata/system block groups.
134026ce2095SJosef Bacik  * If @force is not set, this function will mark the block group readonly
134126ce2095SJosef Bacik  * without checking free space.
134226ce2095SJosef Bacik  *
134326ce2095SJosef Bacik  * NOTE: This function doesn't care if other block groups can contain all the
134426ce2095SJosef Bacik  * data in this block group. That check should be done by relocation routine,
134526ce2095SJosef Bacik  * not this function.
134626ce2095SJosef Bacik  */
134732da5386SDavid Sterba static int inc_block_group_ro(struct btrfs_block_group *cache, int force)
134826ce2095SJosef Bacik {
134926ce2095SJosef Bacik 	struct btrfs_space_info *sinfo = cache->space_info;
135026ce2095SJosef Bacik 	u64 num_bytes;
135126ce2095SJosef Bacik 	int ret = -ENOSPC;
135226ce2095SJosef Bacik 
135326ce2095SJosef Bacik 	spin_lock(&sinfo->lock);
135426ce2095SJosef Bacik 	spin_lock(&cache->lock);
135526ce2095SJosef Bacik 
1356195a49eaSFilipe Manana 	if (cache->swap_extents) {
1357195a49eaSFilipe Manana 		ret = -ETXTBSY;
1358195a49eaSFilipe Manana 		goto out;
1359195a49eaSFilipe Manana 	}
1360195a49eaSFilipe Manana 
136126ce2095SJosef Bacik 	if (cache->ro) {
136226ce2095SJosef Bacik 		cache->ro++;
136326ce2095SJosef Bacik 		ret = 0;
136426ce2095SJosef Bacik 		goto out;
136526ce2095SJosef Bacik 	}
136626ce2095SJosef Bacik 
1367b3470b5dSDavid Sterba 	num_bytes = cache->length - cache->reserved - cache->pinned -
1368169e0da9SNaohiro Aota 		    cache->bytes_super - cache->zone_unusable - cache->used;
136926ce2095SJosef Bacik 
137026ce2095SJosef Bacik 	/*
1371a30a3d20SJosef Bacik 	 * Data never overcommits, even in mixed mode, so do just the straight
1372a30a3d20SJosef Bacik 	 * check of left over space in how much we have allocated.
1373a30a3d20SJosef Bacik 	 */
1374a30a3d20SJosef Bacik 	if (force) {
1375a30a3d20SJosef Bacik 		ret = 0;
1376a30a3d20SJosef Bacik 	} else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) {
1377a30a3d20SJosef Bacik 		u64 sinfo_used = btrfs_space_info_used(sinfo, true);
1378a30a3d20SJosef Bacik 
1379a30a3d20SJosef Bacik 		/*
138026ce2095SJosef Bacik 		 * Here we make sure if we mark this bg RO, we still have enough
1381f8935566SJosef Bacik 		 * free space as buffer.
138226ce2095SJosef Bacik 		 */
1383a30a3d20SJosef Bacik 		if (sinfo_used + num_bytes <= sinfo->total_bytes)
1384a30a3d20SJosef Bacik 			ret = 0;
1385a30a3d20SJosef Bacik 	} else {
1386a30a3d20SJosef Bacik 		/*
1387a30a3d20SJosef Bacik 		 * We overcommit metadata, so we need to do the
1388a30a3d20SJosef Bacik 		 * btrfs_can_overcommit check here, and we need to pass in
1389a30a3d20SJosef Bacik 		 * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of
1390a30a3d20SJosef Bacik 		 * leeway to allow us to mark this block group as read only.
1391a30a3d20SJosef Bacik 		 */
1392a30a3d20SJosef Bacik 		if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes,
1393a30a3d20SJosef Bacik 					 BTRFS_RESERVE_NO_FLUSH))
1394a30a3d20SJosef Bacik 			ret = 0;
1395a30a3d20SJosef Bacik 	}
1396a30a3d20SJosef Bacik 
1397a30a3d20SJosef Bacik 	if (!ret) {
139826ce2095SJosef Bacik 		sinfo->bytes_readonly += num_bytes;
1399169e0da9SNaohiro Aota 		if (btrfs_is_zoned(cache->fs_info)) {
1400169e0da9SNaohiro Aota 			/* Migrate zone_unusable bytes to readonly */
1401169e0da9SNaohiro Aota 			sinfo->bytes_readonly += cache->zone_unusable;
1402169e0da9SNaohiro Aota 			sinfo->bytes_zone_unusable -= cache->zone_unusable;
1403169e0da9SNaohiro Aota 			cache->zone_unusable = 0;
1404169e0da9SNaohiro Aota 		}
140526ce2095SJosef Bacik 		cache->ro++;
140626ce2095SJosef Bacik 		list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
140726ce2095SJosef Bacik 	}
140826ce2095SJosef Bacik out:
140926ce2095SJosef Bacik 	spin_unlock(&cache->lock);
141026ce2095SJosef Bacik 	spin_unlock(&sinfo->lock);
141126ce2095SJosef Bacik 	if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) {
141226ce2095SJosef Bacik 		btrfs_info(cache->fs_info,
1413b3470b5dSDavid Sterba 			"unable to make block group %llu ro", cache->start);
141426ce2095SJosef Bacik 		btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0);
141526ce2095SJosef Bacik 	}
141626ce2095SJosef Bacik 	return ret;
141726ce2095SJosef Bacik }
141826ce2095SJosef Bacik 
1419fe119a6eSNikolay Borisov static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
1420fe119a6eSNikolay Borisov 				 struct btrfs_block_group *bg)
142145bb5d6aSNikolay Borisov {
142245bb5d6aSNikolay Borisov 	struct btrfs_fs_info *fs_info = bg->fs_info;
1423fe119a6eSNikolay Borisov 	struct btrfs_transaction *prev_trans = NULL;
142445bb5d6aSNikolay Borisov 	const u64 start = bg->start;
142545bb5d6aSNikolay Borisov 	const u64 end = start + bg->length - 1;
142645bb5d6aSNikolay Borisov 	int ret;
142745bb5d6aSNikolay Borisov 
1428fe119a6eSNikolay Borisov 	spin_lock(&fs_info->trans_lock);
1429fe119a6eSNikolay Borisov 	if (trans->transaction->list.prev != &fs_info->trans_list) {
1430fe119a6eSNikolay Borisov 		prev_trans = list_last_entry(&trans->transaction->list,
1431fe119a6eSNikolay Borisov 					     struct btrfs_transaction, list);
1432fe119a6eSNikolay Borisov 		refcount_inc(&prev_trans->use_count);
1433fe119a6eSNikolay Borisov 	}
1434fe119a6eSNikolay Borisov 	spin_unlock(&fs_info->trans_lock);
1435fe119a6eSNikolay Borisov 
143645bb5d6aSNikolay Borisov 	/*
143745bb5d6aSNikolay Borisov 	 * Hold the unused_bg_unpin_mutex lock to avoid racing with
143845bb5d6aSNikolay Borisov 	 * btrfs_finish_extent_commit(). If we are at transaction N, another
143945bb5d6aSNikolay Borisov 	 * task might be running finish_extent_commit() for the previous
144045bb5d6aSNikolay Borisov 	 * transaction N - 1, and have seen a range belonging to the block
1441fe119a6eSNikolay Borisov 	 * group in pinned_extents before we were able to clear the whole block
1442fe119a6eSNikolay Borisov 	 * group range from pinned_extents. This means that task can lookup for
1443fe119a6eSNikolay Borisov 	 * the block group after we unpinned it from pinned_extents and removed
1444fe119a6eSNikolay Borisov 	 * it, leading to a BUG_ON() at unpin_extent_range().
144545bb5d6aSNikolay Borisov 	 */
144645bb5d6aSNikolay Borisov 	mutex_lock(&fs_info->unused_bg_unpin_mutex);
1447fe119a6eSNikolay Borisov 	if (prev_trans) {
1448fe119a6eSNikolay Borisov 		ret = clear_extent_bits(&prev_trans->pinned_extents, start, end,
144945bb5d6aSNikolay Borisov 					EXTENT_DIRTY);
145045bb5d6aSNikolay Borisov 		if (ret)
1451534cf531SFilipe Manana 			goto out;
1452fe119a6eSNikolay Borisov 	}
145345bb5d6aSNikolay Borisov 
1454fe119a6eSNikolay Borisov 	ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end,
145545bb5d6aSNikolay Borisov 				EXTENT_DIRTY);
1456534cf531SFilipe Manana out:
145745bb5d6aSNikolay Borisov 	mutex_unlock(&fs_info->unused_bg_unpin_mutex);
14585150bf19SFilipe Manana 	if (prev_trans)
14595150bf19SFilipe Manana 		btrfs_put_transaction(prev_trans);
146045bb5d6aSNikolay Borisov 
1461534cf531SFilipe Manana 	return ret == 0;
146245bb5d6aSNikolay Borisov }
146345bb5d6aSNikolay Borisov 
146426ce2095SJosef Bacik /*
1465e3e0520bSJosef Bacik  * Process the unused_bgs list and remove any that don't have any allocated
1466e3e0520bSJosef Bacik  * space inside of them.
1467e3e0520bSJosef Bacik  */
1468e3e0520bSJosef Bacik void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
1469e3e0520bSJosef Bacik {
147032da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1471e3e0520bSJosef Bacik 	struct btrfs_space_info *space_info;
1472e3e0520bSJosef Bacik 	struct btrfs_trans_handle *trans;
14736e80d4f8SDennis Zhou 	const bool async_trim_enabled = btrfs_test_opt(fs_info, DISCARD_ASYNC);
1474e3e0520bSJosef Bacik 	int ret = 0;
1475e3e0520bSJosef Bacik 
1476e3e0520bSJosef Bacik 	if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1477e3e0520bSJosef Bacik 		return;
1478e3e0520bSJosef Bacik 
14792f12741fSJosef Bacik 	if (btrfs_fs_closing(fs_info))
14802f12741fSJosef Bacik 		return;
14812f12741fSJosef Bacik 
1482ddfd08cbSJosef Bacik 	/*
1483ddfd08cbSJosef Bacik 	 * Long running balances can keep us blocked here for eternity, so
1484ddfd08cbSJosef Bacik 	 * simply skip deletion if we're unable to get the mutex.
1485ddfd08cbSJosef Bacik 	 */
1486f3372065SJohannes Thumshirn 	if (!mutex_trylock(&fs_info->reclaim_bgs_lock))
1487ddfd08cbSJosef Bacik 		return;
1488ddfd08cbSJosef Bacik 
1489e3e0520bSJosef Bacik 	spin_lock(&fs_info->unused_bgs_lock);
1490e3e0520bSJosef Bacik 	while (!list_empty(&fs_info->unused_bgs)) {
1491e3e0520bSJosef Bacik 		int trimming;
1492e3e0520bSJosef Bacik 
1493e3e0520bSJosef Bacik 		block_group = list_first_entry(&fs_info->unused_bgs,
149432da5386SDavid Sterba 					       struct btrfs_block_group,
1495e3e0520bSJosef Bacik 					       bg_list);
1496e3e0520bSJosef Bacik 		list_del_init(&block_group->bg_list);
1497e3e0520bSJosef Bacik 
1498e3e0520bSJosef Bacik 		space_info = block_group->space_info;
1499e3e0520bSJosef Bacik 
1500e3e0520bSJosef Bacik 		if (ret || btrfs_mixed_space_info(space_info)) {
1501e3e0520bSJosef Bacik 			btrfs_put_block_group(block_group);
1502e3e0520bSJosef Bacik 			continue;
1503e3e0520bSJosef Bacik 		}
1504e3e0520bSJosef Bacik 		spin_unlock(&fs_info->unused_bgs_lock);
1505e3e0520bSJosef Bacik 
1506b0643e59SDennis Zhou 		btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
1507b0643e59SDennis Zhou 
1508e3e0520bSJosef Bacik 		/* Don't want to race with allocators so take the groups_sem */
1509e3e0520bSJosef Bacik 		down_write(&space_info->groups_sem);
15106e80d4f8SDennis Zhou 
15116e80d4f8SDennis Zhou 		/*
15126e80d4f8SDennis Zhou 		 * Async discard moves the final block group discard to be prior
15136e80d4f8SDennis Zhou 		 * to the unused_bgs code path.  Therefore, if it's not fully
15146e80d4f8SDennis Zhou 		 * trimmed, punt it back to the async discard lists.
15156e80d4f8SDennis Zhou 		 */
15166e80d4f8SDennis Zhou 		if (btrfs_test_opt(fs_info, DISCARD_ASYNC) &&
15176e80d4f8SDennis Zhou 		    !btrfs_is_free_space_trimmed(block_group)) {
15186e80d4f8SDennis Zhou 			trace_btrfs_skip_unused_block_group(block_group);
15196e80d4f8SDennis Zhou 			up_write(&space_info->groups_sem);
15206e80d4f8SDennis Zhou 			/* Requeue if we failed because of async discard */
15216e80d4f8SDennis Zhou 			btrfs_discard_queue_work(&fs_info->discard_ctl,
15226e80d4f8SDennis Zhou 						 block_group);
15236e80d4f8SDennis Zhou 			goto next;
15246e80d4f8SDennis Zhou 		}
15256e80d4f8SDennis Zhou 
1526e3e0520bSJosef Bacik 		spin_lock(&block_group->lock);
1527e3e0520bSJosef Bacik 		if (block_group->reserved || block_group->pinned ||
1528bf38be65SDavid Sterba 		    block_group->used || block_group->ro ||
1529e3e0520bSJosef Bacik 		    list_is_singular(&block_group->list)) {
1530e3e0520bSJosef Bacik 			/*
1531e3e0520bSJosef Bacik 			 * We want to bail if we made new allocations or have
1532e3e0520bSJosef Bacik 			 * outstanding allocations in this block group.  We do
1533e3e0520bSJosef Bacik 			 * the ro check in case balance is currently acting on
1534e3e0520bSJosef Bacik 			 * this block group.
1535e3e0520bSJosef Bacik 			 */
1536e3e0520bSJosef Bacik 			trace_btrfs_skip_unused_block_group(block_group);
1537e3e0520bSJosef Bacik 			spin_unlock(&block_group->lock);
1538e3e0520bSJosef Bacik 			up_write(&space_info->groups_sem);
1539e3e0520bSJosef Bacik 			goto next;
1540e3e0520bSJosef Bacik 		}
1541e3e0520bSJosef Bacik 		spin_unlock(&block_group->lock);
1542e3e0520bSJosef Bacik 
1543e3e0520bSJosef Bacik 		/* We don't want to force the issue, only flip if it's ok. */
1544e11c0406SJosef Bacik 		ret = inc_block_group_ro(block_group, 0);
1545e3e0520bSJosef Bacik 		up_write(&space_info->groups_sem);
1546e3e0520bSJosef Bacik 		if (ret < 0) {
1547e3e0520bSJosef Bacik 			ret = 0;
1548e3e0520bSJosef Bacik 			goto next;
1549e3e0520bSJosef Bacik 		}
1550e3e0520bSJosef Bacik 
155174e91b12SNaohiro Aota 		ret = btrfs_zone_finish(block_group);
155274e91b12SNaohiro Aota 		if (ret < 0) {
155374e91b12SNaohiro Aota 			btrfs_dec_block_group_ro(block_group);
155474e91b12SNaohiro Aota 			if (ret == -EAGAIN)
155574e91b12SNaohiro Aota 				ret = 0;
155674e91b12SNaohiro Aota 			goto next;
155774e91b12SNaohiro Aota 		}
155874e91b12SNaohiro Aota 
1559e3e0520bSJosef Bacik 		/*
1560e3e0520bSJosef Bacik 		 * Want to do this before we do anything else so we can recover
1561e3e0520bSJosef Bacik 		 * properly if we fail to join the transaction.
1562e3e0520bSJosef Bacik 		 */
1563e3e0520bSJosef Bacik 		trans = btrfs_start_trans_remove_block_group(fs_info,
1564b3470b5dSDavid Sterba 						     block_group->start);
1565e3e0520bSJosef Bacik 		if (IS_ERR(trans)) {
1566e3e0520bSJosef Bacik 			btrfs_dec_block_group_ro(block_group);
1567e3e0520bSJosef Bacik 			ret = PTR_ERR(trans);
1568e3e0520bSJosef Bacik 			goto next;
1569e3e0520bSJosef Bacik 		}
1570e3e0520bSJosef Bacik 
1571e3e0520bSJosef Bacik 		/*
1572e3e0520bSJosef Bacik 		 * We could have pending pinned extents for this block group,
1573e3e0520bSJosef Bacik 		 * just delete them, we don't care about them anymore.
1574e3e0520bSJosef Bacik 		 */
1575534cf531SFilipe Manana 		if (!clean_pinned_extents(trans, block_group)) {
1576534cf531SFilipe Manana 			btrfs_dec_block_group_ro(block_group);
1577e3e0520bSJosef Bacik 			goto end_trans;
1578534cf531SFilipe Manana 		}
1579e3e0520bSJosef Bacik 
1580b0643e59SDennis Zhou 		/*
1581b0643e59SDennis Zhou 		 * At this point, the block_group is read only and should fail
1582b0643e59SDennis Zhou 		 * new allocations.  However, btrfs_finish_extent_commit() can
1583b0643e59SDennis Zhou 		 * cause this block_group to be placed back on the discard
1584b0643e59SDennis Zhou 		 * lists because now the block_group isn't fully discarded.
1585b0643e59SDennis Zhou 		 * Bail here and try again later after discarding everything.
1586b0643e59SDennis Zhou 		 */
1587b0643e59SDennis Zhou 		spin_lock(&fs_info->discard_ctl.lock);
1588b0643e59SDennis Zhou 		if (!list_empty(&block_group->discard_list)) {
1589b0643e59SDennis Zhou 			spin_unlock(&fs_info->discard_ctl.lock);
1590b0643e59SDennis Zhou 			btrfs_dec_block_group_ro(block_group);
1591b0643e59SDennis Zhou 			btrfs_discard_queue_work(&fs_info->discard_ctl,
1592b0643e59SDennis Zhou 						 block_group);
1593b0643e59SDennis Zhou 			goto end_trans;
1594b0643e59SDennis Zhou 		}
1595b0643e59SDennis Zhou 		spin_unlock(&fs_info->discard_ctl.lock);
1596b0643e59SDennis Zhou 
1597e3e0520bSJosef Bacik 		/* Reset pinned so btrfs_put_block_group doesn't complain */
1598e3e0520bSJosef Bacik 		spin_lock(&space_info->lock);
1599e3e0520bSJosef Bacik 		spin_lock(&block_group->lock);
1600e3e0520bSJosef Bacik 
1601e3e0520bSJosef Bacik 		btrfs_space_info_update_bytes_pinned(fs_info, space_info,
1602e3e0520bSJosef Bacik 						     -block_group->pinned);
1603e3e0520bSJosef Bacik 		space_info->bytes_readonly += block_group->pinned;
1604e3e0520bSJosef Bacik 		block_group->pinned = 0;
1605e3e0520bSJosef Bacik 
1606e3e0520bSJosef Bacik 		spin_unlock(&block_group->lock);
1607e3e0520bSJosef Bacik 		spin_unlock(&space_info->lock);
1608e3e0520bSJosef Bacik 
16096e80d4f8SDennis Zhou 		/*
16106e80d4f8SDennis Zhou 		 * The normal path here is an unused block group is passed here,
16116e80d4f8SDennis Zhou 		 * then trimming is handled in the transaction commit path.
16126e80d4f8SDennis Zhou 		 * Async discard interposes before this to do the trimming
16136e80d4f8SDennis Zhou 		 * before coming down the unused block group path as trimming
16146e80d4f8SDennis Zhou 		 * will no longer be done later in the transaction commit path.
16156e80d4f8SDennis Zhou 		 */
16166e80d4f8SDennis Zhou 		if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC))
16176e80d4f8SDennis Zhou 			goto flip_async;
16186e80d4f8SDennis Zhou 
1619dcba6e48SNaohiro Aota 		/*
1620dcba6e48SNaohiro Aota 		 * DISCARD can flip during remount. On zoned filesystems, we
1621dcba6e48SNaohiro Aota 		 * need to reset sequential-required zones.
1622dcba6e48SNaohiro Aota 		 */
1623dcba6e48SNaohiro Aota 		trimming = btrfs_test_opt(fs_info, DISCARD_SYNC) ||
1624dcba6e48SNaohiro Aota 				btrfs_is_zoned(fs_info);
1625e3e0520bSJosef Bacik 
1626e3e0520bSJosef Bacik 		/* Implicit trim during transaction commit. */
1627e3e0520bSJosef Bacik 		if (trimming)
16286b7304afSFilipe Manana 			btrfs_freeze_block_group(block_group);
1629e3e0520bSJosef Bacik 
1630e3e0520bSJosef Bacik 		/*
1631e3e0520bSJosef Bacik 		 * Btrfs_remove_chunk will abort the transaction if things go
1632e3e0520bSJosef Bacik 		 * horribly wrong.
1633e3e0520bSJosef Bacik 		 */
1634b3470b5dSDavid Sterba 		ret = btrfs_remove_chunk(trans, block_group->start);
1635e3e0520bSJosef Bacik 
1636e3e0520bSJosef Bacik 		if (ret) {
1637e3e0520bSJosef Bacik 			if (trimming)
16386b7304afSFilipe Manana 				btrfs_unfreeze_block_group(block_group);
1639e3e0520bSJosef Bacik 			goto end_trans;
1640e3e0520bSJosef Bacik 		}
1641e3e0520bSJosef Bacik 
1642e3e0520bSJosef Bacik 		/*
1643e3e0520bSJosef Bacik 		 * If we're not mounted with -odiscard, we can just forget
1644e3e0520bSJosef Bacik 		 * about this block group. Otherwise we'll need to wait
1645e3e0520bSJosef Bacik 		 * until transaction commit to do the actual discard.
1646e3e0520bSJosef Bacik 		 */
1647e3e0520bSJosef Bacik 		if (trimming) {
1648e3e0520bSJosef Bacik 			spin_lock(&fs_info->unused_bgs_lock);
1649e3e0520bSJosef Bacik 			/*
1650e3e0520bSJosef Bacik 			 * A concurrent scrub might have added us to the list
1651e3e0520bSJosef Bacik 			 * fs_info->unused_bgs, so use a list_move operation
1652e3e0520bSJosef Bacik 			 * to add the block group to the deleted_bgs list.
1653e3e0520bSJosef Bacik 			 */
1654e3e0520bSJosef Bacik 			list_move(&block_group->bg_list,
1655e3e0520bSJosef Bacik 				  &trans->transaction->deleted_bgs);
1656e3e0520bSJosef Bacik 			spin_unlock(&fs_info->unused_bgs_lock);
1657e3e0520bSJosef Bacik 			btrfs_get_block_group(block_group);
1658e3e0520bSJosef Bacik 		}
1659e3e0520bSJosef Bacik end_trans:
1660e3e0520bSJosef Bacik 		btrfs_end_transaction(trans);
1661e3e0520bSJosef Bacik next:
1662e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
1663e3e0520bSJosef Bacik 		spin_lock(&fs_info->unused_bgs_lock);
1664e3e0520bSJosef Bacik 	}
1665e3e0520bSJosef Bacik 	spin_unlock(&fs_info->unused_bgs_lock);
1666f3372065SJohannes Thumshirn 	mutex_unlock(&fs_info->reclaim_bgs_lock);
16676e80d4f8SDennis Zhou 	return;
16686e80d4f8SDennis Zhou 
16696e80d4f8SDennis Zhou flip_async:
16706e80d4f8SDennis Zhou 	btrfs_end_transaction(trans);
1671f3372065SJohannes Thumshirn 	mutex_unlock(&fs_info->reclaim_bgs_lock);
16726e80d4f8SDennis Zhou 	btrfs_put_block_group(block_group);
16736e80d4f8SDennis Zhou 	btrfs_discard_punt_unused_bgs_list(fs_info);
1674e3e0520bSJosef Bacik }
1675e3e0520bSJosef Bacik 
167632da5386SDavid Sterba void btrfs_mark_bg_unused(struct btrfs_block_group *bg)
1677e3e0520bSJosef Bacik {
1678e3e0520bSJosef Bacik 	struct btrfs_fs_info *fs_info = bg->fs_info;
1679e3e0520bSJosef Bacik 
1680e3e0520bSJosef Bacik 	spin_lock(&fs_info->unused_bgs_lock);
1681e3e0520bSJosef Bacik 	if (list_empty(&bg->bg_list)) {
1682e3e0520bSJosef Bacik 		btrfs_get_block_group(bg);
16830657b20cSFilipe Manana 		trace_btrfs_add_unused_block_group(bg);
1684e3e0520bSJosef Bacik 		list_add_tail(&bg->bg_list, &fs_info->unused_bgs);
16850657b20cSFilipe Manana 	} else if (!test_bit(BLOCK_GROUP_FLAG_NEW, &bg->runtime_flags)) {
1686a9f18971SNaohiro Aota 		/* Pull out the block group from the reclaim_bgs list. */
16870657b20cSFilipe Manana 		trace_btrfs_add_unused_block_group(bg);
1688a9f18971SNaohiro Aota 		list_move_tail(&bg->bg_list, &fs_info->unused_bgs);
1689e3e0520bSJosef Bacik 	}
1690e3e0520bSJosef Bacik 	spin_unlock(&fs_info->unused_bgs_lock);
1691e3e0520bSJosef Bacik }
16924358d963SJosef Bacik 
16932ca0ec77SJohannes Thumshirn /*
16942ca0ec77SJohannes Thumshirn  * We want block groups with a low number of used bytes to be in the beginning
16952ca0ec77SJohannes Thumshirn  * of the list, so they will get reclaimed first.
16962ca0ec77SJohannes Thumshirn  */
16972ca0ec77SJohannes Thumshirn static int reclaim_bgs_cmp(void *unused, const struct list_head *a,
16982ca0ec77SJohannes Thumshirn 			   const struct list_head *b)
16992ca0ec77SJohannes Thumshirn {
17002ca0ec77SJohannes Thumshirn 	const struct btrfs_block_group *bg1, *bg2;
17012ca0ec77SJohannes Thumshirn 
17022ca0ec77SJohannes Thumshirn 	bg1 = list_entry(a, struct btrfs_block_group, bg_list);
17032ca0ec77SJohannes Thumshirn 	bg2 = list_entry(b, struct btrfs_block_group, bg_list);
17042ca0ec77SJohannes Thumshirn 
17052ca0ec77SJohannes Thumshirn 	return bg1->used > bg2->used;
17062ca0ec77SJohannes Thumshirn }
17072ca0ec77SJohannes Thumshirn 
17083687fcb0SJohannes Thumshirn static inline bool btrfs_should_reclaim(struct btrfs_fs_info *fs_info)
17093687fcb0SJohannes Thumshirn {
17103687fcb0SJohannes Thumshirn 	if (btrfs_is_zoned(fs_info))
17113687fcb0SJohannes Thumshirn 		return btrfs_zoned_should_reclaim(fs_info);
17123687fcb0SJohannes Thumshirn 	return true;
17133687fcb0SJohannes Thumshirn }
17143687fcb0SJohannes Thumshirn 
171581531225SBoris Burkov static bool should_reclaim_block_group(struct btrfs_block_group *bg, u64 bytes_freed)
171681531225SBoris Burkov {
171781531225SBoris Burkov 	const struct btrfs_space_info *space_info = bg->space_info;
171881531225SBoris Burkov 	const int reclaim_thresh = READ_ONCE(space_info->bg_reclaim_threshold);
171981531225SBoris Burkov 	const u64 new_val = bg->used;
172081531225SBoris Burkov 	const u64 old_val = new_val + bytes_freed;
172181531225SBoris Burkov 	u64 thresh;
172281531225SBoris Burkov 
172381531225SBoris Burkov 	if (reclaim_thresh == 0)
172481531225SBoris Burkov 		return false;
172581531225SBoris Burkov 
1726428c8e03SDavid Sterba 	thresh = mult_perc(bg->length, reclaim_thresh);
172781531225SBoris Burkov 
172881531225SBoris Burkov 	/*
172981531225SBoris Burkov 	 * If we were below the threshold before don't reclaim, we are likely a
173081531225SBoris Burkov 	 * brand new block group and we don't want to relocate new block groups.
173181531225SBoris Burkov 	 */
173281531225SBoris Burkov 	if (old_val < thresh)
173381531225SBoris Burkov 		return false;
173481531225SBoris Burkov 	if (new_val >= thresh)
173581531225SBoris Burkov 		return false;
173681531225SBoris Burkov 	return true;
173781531225SBoris Burkov }
173881531225SBoris Burkov 
173918bb8bbfSJohannes Thumshirn void btrfs_reclaim_bgs_work(struct work_struct *work)
174018bb8bbfSJohannes Thumshirn {
174118bb8bbfSJohannes Thumshirn 	struct btrfs_fs_info *fs_info =
174218bb8bbfSJohannes Thumshirn 		container_of(work, struct btrfs_fs_info, reclaim_bgs_work);
174318bb8bbfSJohannes Thumshirn 	struct btrfs_block_group *bg;
174418bb8bbfSJohannes Thumshirn 	struct btrfs_space_info *space_info;
174518bb8bbfSJohannes Thumshirn 
174618bb8bbfSJohannes Thumshirn 	if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
174718bb8bbfSJohannes Thumshirn 		return;
174818bb8bbfSJohannes Thumshirn 
17492f12741fSJosef Bacik 	if (btrfs_fs_closing(fs_info))
17502f12741fSJosef Bacik 		return;
17512f12741fSJosef Bacik 
17523687fcb0SJohannes Thumshirn 	if (!btrfs_should_reclaim(fs_info))
17533687fcb0SJohannes Thumshirn 		return;
17543687fcb0SJohannes Thumshirn 
1755ca5e4ea0SNaohiro Aota 	sb_start_write(fs_info->sb);
1756ca5e4ea0SNaohiro Aota 
1757ca5e4ea0SNaohiro Aota 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
1758ca5e4ea0SNaohiro Aota 		sb_end_write(fs_info->sb);
175918bb8bbfSJohannes Thumshirn 		return;
1760ca5e4ea0SNaohiro Aota 	}
176118bb8bbfSJohannes Thumshirn 
17629cc0b837SJohannes Thumshirn 	/*
17639cc0b837SJohannes Thumshirn 	 * Long running balances can keep us blocked here for eternity, so
17649cc0b837SJohannes Thumshirn 	 * simply skip reclaim if we're unable to get the mutex.
17659cc0b837SJohannes Thumshirn 	 */
17669cc0b837SJohannes Thumshirn 	if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) {
17679cc0b837SJohannes Thumshirn 		btrfs_exclop_finish(fs_info);
1768ca5e4ea0SNaohiro Aota 		sb_end_write(fs_info->sb);
17699cc0b837SJohannes Thumshirn 		return;
17709cc0b837SJohannes Thumshirn 	}
17719cc0b837SJohannes Thumshirn 
177218bb8bbfSJohannes Thumshirn 	spin_lock(&fs_info->unused_bgs_lock);
17732ca0ec77SJohannes Thumshirn 	/*
17742ca0ec77SJohannes Thumshirn 	 * Sort happens under lock because we can't simply splice it and sort.
17752ca0ec77SJohannes Thumshirn 	 * The block groups might still be in use and reachable via bg_list,
17762ca0ec77SJohannes Thumshirn 	 * and their presence in the reclaim_bgs list must be preserved.
17772ca0ec77SJohannes Thumshirn 	 */
17782ca0ec77SJohannes Thumshirn 	list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp);
177918bb8bbfSJohannes Thumshirn 	while (!list_empty(&fs_info->reclaim_bgs)) {
17805f93e776SJohannes Thumshirn 		u64 zone_unusable;
17811cea5cf0SFilipe Manana 		int ret = 0;
17821cea5cf0SFilipe Manana 
178318bb8bbfSJohannes Thumshirn 		bg = list_first_entry(&fs_info->reclaim_bgs,
178418bb8bbfSJohannes Thumshirn 				      struct btrfs_block_group,
178518bb8bbfSJohannes Thumshirn 				      bg_list);
178618bb8bbfSJohannes Thumshirn 		list_del_init(&bg->bg_list);
178718bb8bbfSJohannes Thumshirn 
178818bb8bbfSJohannes Thumshirn 		space_info = bg->space_info;
178918bb8bbfSJohannes Thumshirn 		spin_unlock(&fs_info->unused_bgs_lock);
179018bb8bbfSJohannes Thumshirn 
179118bb8bbfSJohannes Thumshirn 		/* Don't race with allocators so take the groups_sem */
179218bb8bbfSJohannes Thumshirn 		down_write(&space_info->groups_sem);
179318bb8bbfSJohannes Thumshirn 
179418bb8bbfSJohannes Thumshirn 		spin_lock(&bg->lock);
179518bb8bbfSJohannes Thumshirn 		if (bg->reserved || bg->pinned || bg->ro) {
179618bb8bbfSJohannes Thumshirn 			/*
179718bb8bbfSJohannes Thumshirn 			 * We want to bail if we made new allocations or have
179818bb8bbfSJohannes Thumshirn 			 * outstanding allocations in this block group.  We do
179918bb8bbfSJohannes Thumshirn 			 * the ro check in case balance is currently acting on
180018bb8bbfSJohannes Thumshirn 			 * this block group.
180118bb8bbfSJohannes Thumshirn 			 */
180218bb8bbfSJohannes Thumshirn 			spin_unlock(&bg->lock);
180318bb8bbfSJohannes Thumshirn 			up_write(&space_info->groups_sem);
180418bb8bbfSJohannes Thumshirn 			goto next;
180518bb8bbfSJohannes Thumshirn 		}
1806cc4804bfSBoris Burkov 		if (bg->used == 0) {
1807cc4804bfSBoris Burkov 			/*
1808cc4804bfSBoris Burkov 			 * It is possible that we trigger relocation on a block
1809cc4804bfSBoris Burkov 			 * group as its extents are deleted and it first goes
1810cc4804bfSBoris Burkov 			 * below the threshold, then shortly after goes empty.
1811cc4804bfSBoris Burkov 			 *
1812cc4804bfSBoris Burkov 			 * In this case, relocating it does delete it, but has
1813cc4804bfSBoris Burkov 			 * some overhead in relocation specific metadata, looking
1814cc4804bfSBoris Burkov 			 * for the non-existent extents and running some extra
1815cc4804bfSBoris Burkov 			 * transactions, which we can avoid by using one of the
1816cc4804bfSBoris Burkov 			 * other mechanisms for dealing with empty block groups.
1817cc4804bfSBoris Burkov 			 */
1818cc4804bfSBoris Burkov 			if (!btrfs_test_opt(fs_info, DISCARD_ASYNC))
1819cc4804bfSBoris Burkov 				btrfs_mark_bg_unused(bg);
1820cc4804bfSBoris Burkov 			spin_unlock(&bg->lock);
1821cc4804bfSBoris Burkov 			up_write(&space_info->groups_sem);
1822cc4804bfSBoris Burkov 			goto next;
182381531225SBoris Burkov 
182481531225SBoris Burkov 		}
182581531225SBoris Burkov 		/*
182681531225SBoris Burkov 		 * The block group might no longer meet the reclaim condition by
182781531225SBoris Burkov 		 * the time we get around to reclaiming it, so to avoid
182881531225SBoris Burkov 		 * reclaiming overly full block_groups, skip reclaiming them.
182981531225SBoris Burkov 		 *
183081531225SBoris Burkov 		 * Since the decision making process also depends on the amount
183181531225SBoris Burkov 		 * being freed, pass in a fake giant value to skip that extra
183281531225SBoris Burkov 		 * check, which is more meaningful when adding to the list in
183381531225SBoris Burkov 		 * the first place.
183481531225SBoris Burkov 		 */
183581531225SBoris Burkov 		if (!should_reclaim_block_group(bg, bg->length)) {
183681531225SBoris Burkov 			spin_unlock(&bg->lock);
183781531225SBoris Burkov 			up_write(&space_info->groups_sem);
183881531225SBoris Burkov 			goto next;
1839cc4804bfSBoris Burkov 		}
184018bb8bbfSJohannes Thumshirn 		spin_unlock(&bg->lock);
184118bb8bbfSJohannes Thumshirn 
184293463ff7SNaohiro Aota 		/*
184393463ff7SNaohiro Aota 		 * Get out fast, in case we're read-only or unmounting the
184493463ff7SNaohiro Aota 		 * filesystem. It is OK to drop block groups from the list even
184593463ff7SNaohiro Aota 		 * for the read-only case. As we did sb_start_write(),
184693463ff7SNaohiro Aota 		 * "mount -o remount,ro" won't happen and read-only filesystem
184793463ff7SNaohiro Aota 		 * means it is forced read-only due to a fatal error. So, it
184893463ff7SNaohiro Aota 		 * never gets back to read-write to let us reclaim again.
184993463ff7SNaohiro Aota 		 */
185093463ff7SNaohiro Aota 		if (btrfs_need_cleaner_sleep(fs_info)) {
185118bb8bbfSJohannes Thumshirn 			up_write(&space_info->groups_sem);
185218bb8bbfSJohannes Thumshirn 			goto next;
185318bb8bbfSJohannes Thumshirn 		}
185418bb8bbfSJohannes Thumshirn 
18555f93e776SJohannes Thumshirn 		/*
18565f93e776SJohannes Thumshirn 		 * Cache the zone_unusable value before turning the block group
18575f93e776SJohannes Thumshirn 		 * to read only. As soon as the blog group is read only it's
18585f93e776SJohannes Thumshirn 		 * zone_unusable value gets moved to the block group's read-only
18595f93e776SJohannes Thumshirn 		 * bytes and isn't available for calculations anymore.
18605f93e776SJohannes Thumshirn 		 */
18615f93e776SJohannes Thumshirn 		zone_unusable = bg->zone_unusable;
186218bb8bbfSJohannes Thumshirn 		ret = inc_block_group_ro(bg, 0);
186318bb8bbfSJohannes Thumshirn 		up_write(&space_info->groups_sem);
186418bb8bbfSJohannes Thumshirn 		if (ret < 0)
186518bb8bbfSJohannes Thumshirn 			goto next;
186618bb8bbfSJohannes Thumshirn 
18675f93e776SJohannes Thumshirn 		btrfs_info(fs_info,
18685f93e776SJohannes Thumshirn 			"reclaiming chunk %llu with %llu%% used %llu%% unusable",
186995cd356cSJohannes Thumshirn 				bg->start,
187095cd356cSJohannes Thumshirn 				div64_u64(bg->used * 100, bg->length),
18715f93e776SJohannes Thumshirn 				div64_u64(zone_unusable * 100, bg->length));
187218bb8bbfSJohannes Thumshirn 		trace_btrfs_reclaim_block_group(bg);
187318bb8bbfSJohannes Thumshirn 		ret = btrfs_relocate_chunk(fs_info, bg->start);
187474944c87SJosef Bacik 		if (ret) {
187574944c87SJosef Bacik 			btrfs_dec_block_group_ro(bg);
187618bb8bbfSJohannes Thumshirn 			btrfs_err(fs_info, "error relocating chunk %llu",
187718bb8bbfSJohannes Thumshirn 				  bg->start);
187874944c87SJosef Bacik 		}
187918bb8bbfSJohannes Thumshirn 
188018bb8bbfSJohannes Thumshirn next:
18817e271809SNaohiro Aota 		if (ret)
18827e271809SNaohiro Aota 			btrfs_mark_bg_to_reclaim(bg);
18831cea5cf0SFilipe Manana 		btrfs_put_block_group(bg);
18843ed01616SNaohiro Aota 
18853ed01616SNaohiro Aota 		mutex_unlock(&fs_info->reclaim_bgs_lock);
18863ed01616SNaohiro Aota 		/*
18873ed01616SNaohiro Aota 		 * Reclaiming all the block groups in the list can take really
18883ed01616SNaohiro Aota 		 * long.  Prioritize cleaning up unused block groups.
18893ed01616SNaohiro Aota 		 */
18903ed01616SNaohiro Aota 		btrfs_delete_unused_bgs(fs_info);
18913ed01616SNaohiro Aota 		/*
18923ed01616SNaohiro Aota 		 * If we are interrupted by a balance, we can just bail out. The
18933ed01616SNaohiro Aota 		 * cleaner thread restart again if necessary.
18943ed01616SNaohiro Aota 		 */
18953ed01616SNaohiro Aota 		if (!mutex_trylock(&fs_info->reclaim_bgs_lock))
18963ed01616SNaohiro Aota 			goto end;
1897d96b3424SFilipe Manana 		spin_lock(&fs_info->unused_bgs_lock);
189818bb8bbfSJohannes Thumshirn 	}
189918bb8bbfSJohannes Thumshirn 	spin_unlock(&fs_info->unused_bgs_lock);
190018bb8bbfSJohannes Thumshirn 	mutex_unlock(&fs_info->reclaim_bgs_lock);
19013ed01616SNaohiro Aota end:
190218bb8bbfSJohannes Thumshirn 	btrfs_exclop_finish(fs_info);
1903ca5e4ea0SNaohiro Aota 	sb_end_write(fs_info->sb);
190418bb8bbfSJohannes Thumshirn }
190518bb8bbfSJohannes Thumshirn 
190618bb8bbfSJohannes Thumshirn void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info)
190718bb8bbfSJohannes Thumshirn {
190818bb8bbfSJohannes Thumshirn 	spin_lock(&fs_info->unused_bgs_lock);
190918bb8bbfSJohannes Thumshirn 	if (!list_empty(&fs_info->reclaim_bgs))
191018bb8bbfSJohannes Thumshirn 		queue_work(system_unbound_wq, &fs_info->reclaim_bgs_work);
191118bb8bbfSJohannes Thumshirn 	spin_unlock(&fs_info->unused_bgs_lock);
191218bb8bbfSJohannes Thumshirn }
191318bb8bbfSJohannes Thumshirn 
191418bb8bbfSJohannes Thumshirn void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg)
191518bb8bbfSJohannes Thumshirn {
191618bb8bbfSJohannes Thumshirn 	struct btrfs_fs_info *fs_info = bg->fs_info;
191718bb8bbfSJohannes Thumshirn 
191818bb8bbfSJohannes Thumshirn 	spin_lock(&fs_info->unused_bgs_lock);
191918bb8bbfSJohannes Thumshirn 	if (list_empty(&bg->bg_list)) {
192018bb8bbfSJohannes Thumshirn 		btrfs_get_block_group(bg);
192118bb8bbfSJohannes Thumshirn 		trace_btrfs_add_reclaim_block_group(bg);
192218bb8bbfSJohannes Thumshirn 		list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
192318bb8bbfSJohannes Thumshirn 	}
192418bb8bbfSJohannes Thumshirn 	spin_unlock(&fs_info->unused_bgs_lock);
192518bb8bbfSJohannes Thumshirn }
192618bb8bbfSJohannes Thumshirn 
1927e3ba67a1SJohannes Thumshirn static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
1928e3ba67a1SJohannes Thumshirn 			   struct btrfs_path *path)
1929e3ba67a1SJohannes Thumshirn {
1930e3ba67a1SJohannes Thumshirn 	struct extent_map_tree *em_tree;
1931e3ba67a1SJohannes Thumshirn 	struct extent_map *em;
1932e3ba67a1SJohannes Thumshirn 	struct btrfs_block_group_item bg;
1933e3ba67a1SJohannes Thumshirn 	struct extent_buffer *leaf;
1934e3ba67a1SJohannes Thumshirn 	int slot;
1935e3ba67a1SJohannes Thumshirn 	u64 flags;
1936e3ba67a1SJohannes Thumshirn 	int ret = 0;
1937e3ba67a1SJohannes Thumshirn 
1938e3ba67a1SJohannes Thumshirn 	slot = path->slots[0];
1939e3ba67a1SJohannes Thumshirn 	leaf = path->nodes[0];
1940e3ba67a1SJohannes Thumshirn 
1941e3ba67a1SJohannes Thumshirn 	em_tree = &fs_info->mapping_tree;
1942e3ba67a1SJohannes Thumshirn 	read_lock(&em_tree->lock);
1943e3ba67a1SJohannes Thumshirn 	em = lookup_extent_mapping(em_tree, key->objectid, key->offset);
1944e3ba67a1SJohannes Thumshirn 	read_unlock(&em_tree->lock);
1945e3ba67a1SJohannes Thumshirn 	if (!em) {
1946e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
1947e3ba67a1SJohannes Thumshirn 			  "logical %llu len %llu found bg but no related chunk",
1948e3ba67a1SJohannes Thumshirn 			  key->objectid, key->offset);
1949e3ba67a1SJohannes Thumshirn 		return -ENOENT;
1950e3ba67a1SJohannes Thumshirn 	}
1951e3ba67a1SJohannes Thumshirn 
1952e3ba67a1SJohannes Thumshirn 	if (em->start != key->objectid || em->len != key->offset) {
1953e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
1954e3ba67a1SJohannes Thumshirn 			"block group %llu len %llu mismatch with chunk %llu len %llu",
1955e3ba67a1SJohannes Thumshirn 			key->objectid, key->offset, em->start, em->len);
1956e3ba67a1SJohannes Thumshirn 		ret = -EUCLEAN;
1957e3ba67a1SJohannes Thumshirn 		goto out_free_em;
1958e3ba67a1SJohannes Thumshirn 	}
1959e3ba67a1SJohannes Thumshirn 
1960e3ba67a1SJohannes Thumshirn 	read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot),
1961e3ba67a1SJohannes Thumshirn 			   sizeof(bg));
1962e3ba67a1SJohannes Thumshirn 	flags = btrfs_stack_block_group_flags(&bg) &
1963e3ba67a1SJohannes Thumshirn 		BTRFS_BLOCK_GROUP_TYPE_MASK;
1964e3ba67a1SJohannes Thumshirn 
1965e3ba67a1SJohannes Thumshirn 	if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1966e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
1967e3ba67a1SJohannes Thumshirn "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
1968e3ba67a1SJohannes Thumshirn 			  key->objectid, key->offset, flags,
1969e3ba67a1SJohannes Thumshirn 			  (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type));
1970e3ba67a1SJohannes Thumshirn 		ret = -EUCLEAN;
1971e3ba67a1SJohannes Thumshirn 	}
1972e3ba67a1SJohannes Thumshirn 
1973e3ba67a1SJohannes Thumshirn out_free_em:
1974e3ba67a1SJohannes Thumshirn 	free_extent_map(em);
1975e3ba67a1SJohannes Thumshirn 	return ret;
1976e3ba67a1SJohannes Thumshirn }
1977e3ba67a1SJohannes Thumshirn 
19784358d963SJosef Bacik static int find_first_block_group(struct btrfs_fs_info *fs_info,
19794358d963SJosef Bacik 				  struct btrfs_path *path,
19804358d963SJosef Bacik 				  struct btrfs_key *key)
19814358d963SJosef Bacik {
1982dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
1983e3ba67a1SJohannes Thumshirn 	int ret;
19844358d963SJosef Bacik 	struct btrfs_key found_key;
19854358d963SJosef Bacik 
198636dfbbe2SGabriel Niebler 	btrfs_for_each_slot(root, key, &found_key, path, ret) {
19874358d963SJosef Bacik 		if (found_key.objectid >= key->objectid &&
19884358d963SJosef Bacik 		    found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
198936dfbbe2SGabriel Niebler 			return read_bg_from_eb(fs_info, &found_key, path);
1990e3ba67a1SJohannes Thumshirn 		}
19914358d963SJosef Bacik 	}
19924358d963SJosef Bacik 	return ret;
19934358d963SJosef Bacik }
19944358d963SJosef Bacik 
19954358d963SJosef Bacik static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
19964358d963SJosef Bacik {
19974358d963SJosef Bacik 	u64 extra_flags = chunk_to_extended(flags) &
19984358d963SJosef Bacik 				BTRFS_EXTENDED_PROFILE_MASK;
19994358d963SJosef Bacik 
20004358d963SJosef Bacik 	write_seqlock(&fs_info->profiles_lock);
20014358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA)
20024358d963SJosef Bacik 		fs_info->avail_data_alloc_bits |= extra_flags;
20034358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_METADATA)
20044358d963SJosef Bacik 		fs_info->avail_metadata_alloc_bits |= extra_flags;
20054358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
20064358d963SJosef Bacik 		fs_info->avail_system_alloc_bits |= extra_flags;
20074358d963SJosef Bacik 	write_sequnlock(&fs_info->profiles_lock);
20084358d963SJosef Bacik }
20094358d963SJosef Bacik 
201043dd529aSDavid Sterba /*
201143dd529aSDavid Sterba  * Map a physical disk address to a list of logical addresses.
20129ee9b979SNikolay Borisov  *
20139ee9b979SNikolay Borisov  * @fs_info:       the filesystem
201496a14336SNikolay Borisov  * @chunk_start:   logical address of block group
201596a14336SNikolay Borisov  * @physical:	   physical address to map to logical addresses
201696a14336SNikolay Borisov  * @logical:	   return array of logical addresses which map to @physical
201796a14336SNikolay Borisov  * @naddrs:	   length of @logical
201896a14336SNikolay Borisov  * @stripe_len:    size of IO stripe for the given block group
201996a14336SNikolay Borisov  *
202096a14336SNikolay Borisov  * Maps a particular @physical disk address to a list of @logical addresses.
202196a14336SNikolay Borisov  * Used primarily to exclude those portions of a block group that contain super
202296a14336SNikolay Borisov  * block copies.
202396a14336SNikolay Borisov  */
202496a14336SNikolay Borisov int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
20251eb82ef8SChristoph Hellwig 		     u64 physical, u64 **logical, int *naddrs, int *stripe_len)
202696a14336SNikolay Borisov {
202796a14336SNikolay Borisov 	struct extent_map *em;
202896a14336SNikolay Borisov 	struct map_lookup *map;
202996a14336SNikolay Borisov 	u64 *buf;
203096a14336SNikolay Borisov 	u64 bytenr;
20311776ad17SNikolay Borisov 	u64 data_stripe_length;
20321776ad17SNikolay Borisov 	u64 io_stripe_size;
20331776ad17SNikolay Borisov 	int i, nr = 0;
20341776ad17SNikolay Borisov 	int ret = 0;
203596a14336SNikolay Borisov 
203696a14336SNikolay Borisov 	em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
203796a14336SNikolay Borisov 	if (IS_ERR(em))
203896a14336SNikolay Borisov 		return -EIO;
203996a14336SNikolay Borisov 
204096a14336SNikolay Borisov 	map = em->map_lookup;
20419e22b925SNikolay Borisov 	data_stripe_length = em->orig_block_len;
2042a97699d1SQu Wenruo 	io_stripe_size = BTRFS_STRIPE_LEN;
2043138082f3SNaohiro Aota 	chunk_start = em->start;
204496a14336SNikolay Borisov 
20459e22b925SNikolay Borisov 	/* For RAID5/6 adjust to a full IO stripe length */
20469e22b925SNikolay Borisov 	if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2047cb091225SQu Wenruo 		io_stripe_size = btrfs_stripe_nr_to_offset(nr_data_stripes(map));
204896a14336SNikolay Borisov 
204996a14336SNikolay Borisov 	buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
20501776ad17SNikolay Borisov 	if (!buf) {
20511776ad17SNikolay Borisov 		ret = -ENOMEM;
20521776ad17SNikolay Borisov 		goto out;
20531776ad17SNikolay Borisov 	}
205496a14336SNikolay Borisov 
205596a14336SNikolay Borisov 	for (i = 0; i < map->num_stripes; i++) {
20561776ad17SNikolay Borisov 		bool already_inserted = false;
20576ded22c1SQu Wenruo 		u32 stripe_nr;
20586ded22c1SQu Wenruo 		u32 offset;
20591776ad17SNikolay Borisov 		int j;
20601776ad17SNikolay Borisov 
20611776ad17SNikolay Borisov 		if (!in_range(physical, map->stripes[i].physical,
20621776ad17SNikolay Borisov 			      data_stripe_length))
206396a14336SNikolay Borisov 			continue;
206496a14336SNikolay Borisov 
2065a97699d1SQu Wenruo 		stripe_nr = (physical - map->stripes[i].physical) >>
2066a97699d1SQu Wenruo 			    BTRFS_STRIPE_LEN_SHIFT;
2067a97699d1SQu Wenruo 		offset = (physical - map->stripes[i].physical) &
2068a97699d1SQu Wenruo 			 BTRFS_STRIPE_LEN_MASK;
206996a14336SNikolay Borisov 
2070ac067734SDavid Sterba 		if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
20716ded22c1SQu Wenruo 				 BTRFS_BLOCK_GROUP_RAID10))
20726ded22c1SQu Wenruo 			stripe_nr = div_u64(stripe_nr * map->num_stripes + i,
20736ded22c1SQu Wenruo 					    map->sub_stripes);
207496a14336SNikolay Borisov 		/*
207596a14336SNikolay Borisov 		 * The remaining case would be for RAID56, multiply by
207696a14336SNikolay Borisov 		 * nr_data_stripes().  Alternatively, just use rmap_len below
207796a14336SNikolay Borisov 		 * instead of map->stripe_len
207896a14336SNikolay Borisov 		 */
2079138082f3SNaohiro Aota 		bytenr = chunk_start + stripe_nr * io_stripe_size + offset;
20801776ad17SNikolay Borisov 
20811776ad17SNikolay Borisov 		/* Ensure we don't add duplicate addresses */
208296a14336SNikolay Borisov 		for (j = 0; j < nr; j++) {
20831776ad17SNikolay Borisov 			if (buf[j] == bytenr) {
20841776ad17SNikolay Borisov 				already_inserted = true;
208596a14336SNikolay Borisov 				break;
208696a14336SNikolay Borisov 			}
208796a14336SNikolay Borisov 		}
20881776ad17SNikolay Borisov 
20891776ad17SNikolay Borisov 		if (!already_inserted)
20901776ad17SNikolay Borisov 			buf[nr++] = bytenr;
209196a14336SNikolay Borisov 	}
209296a14336SNikolay Borisov 
209396a14336SNikolay Borisov 	*logical = buf;
209496a14336SNikolay Borisov 	*naddrs = nr;
20951776ad17SNikolay Borisov 	*stripe_len = io_stripe_size;
20961776ad17SNikolay Borisov out:
209796a14336SNikolay Borisov 	free_extent_map(em);
20981776ad17SNikolay Borisov 	return ret;
209996a14336SNikolay Borisov }
210096a14336SNikolay Borisov 
210132da5386SDavid Sterba static int exclude_super_stripes(struct btrfs_block_group *cache)
21024358d963SJosef Bacik {
21034358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
210412659251SNaohiro Aota 	const bool zoned = btrfs_is_zoned(fs_info);
21054358d963SJosef Bacik 	u64 bytenr;
21064358d963SJosef Bacik 	u64 *logical;
21074358d963SJosef Bacik 	int stripe_len;
21084358d963SJosef Bacik 	int i, nr, ret;
21094358d963SJosef Bacik 
2110b3470b5dSDavid Sterba 	if (cache->start < BTRFS_SUPER_INFO_OFFSET) {
2111b3470b5dSDavid Sterba 		stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start;
21124358d963SJosef Bacik 		cache->bytes_super += stripe_len;
2113b1c8f527SFilipe Manana 		ret = set_extent_bit(&fs_info->excluded_extents, cache->start,
2114b1c8f527SFilipe Manana 				     cache->start + stripe_len - 1,
2115b1c8f527SFilipe Manana 				     EXTENT_UPTODATE, NULL);
21164358d963SJosef Bacik 		if (ret)
21174358d963SJosef Bacik 			return ret;
21184358d963SJosef Bacik 	}
21194358d963SJosef Bacik 
21204358d963SJosef Bacik 	for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
21214358d963SJosef Bacik 		bytenr = btrfs_sb_offset(i);
21221eb82ef8SChristoph Hellwig 		ret = btrfs_rmap_block(fs_info, cache->start,
21234358d963SJosef Bacik 				       bytenr, &logical, &nr, &stripe_len);
21244358d963SJosef Bacik 		if (ret)
21254358d963SJosef Bacik 			return ret;
21264358d963SJosef Bacik 
212712659251SNaohiro Aota 		/* Shouldn't have super stripes in sequential zones */
212812659251SNaohiro Aota 		if (zoned && nr) {
2129f1a07c2bSFilipe Manana 			kfree(logical);
213012659251SNaohiro Aota 			btrfs_err(fs_info,
213112659251SNaohiro Aota 			"zoned: block group %llu must not contain super block",
213212659251SNaohiro Aota 				  cache->start);
213312659251SNaohiro Aota 			return -EUCLEAN;
213412659251SNaohiro Aota 		}
213512659251SNaohiro Aota 
21364358d963SJosef Bacik 		while (nr--) {
213796f9b0f2SNikolay Borisov 			u64 len = min_t(u64, stripe_len,
213896f9b0f2SNikolay Borisov 				cache->start + cache->length - logical[nr]);
21394358d963SJosef Bacik 
21404358d963SJosef Bacik 			cache->bytes_super += len;
2141b1c8f527SFilipe Manana 			ret = set_extent_bit(&fs_info->excluded_extents, logical[nr],
2142b1c8f527SFilipe Manana 					     logical[nr] + len - 1,
2143b1c8f527SFilipe Manana 					     EXTENT_UPTODATE, NULL);
21444358d963SJosef Bacik 			if (ret) {
21454358d963SJosef Bacik 				kfree(logical);
21464358d963SJosef Bacik 				return ret;
21474358d963SJosef Bacik 			}
21484358d963SJosef Bacik 		}
21494358d963SJosef Bacik 
21504358d963SJosef Bacik 		kfree(logical);
21514358d963SJosef Bacik 	}
21524358d963SJosef Bacik 	return 0;
21534358d963SJosef Bacik }
21544358d963SJosef Bacik 
215532da5386SDavid Sterba static struct btrfs_block_group *btrfs_create_block_group_cache(
21569afc6649SQu Wenruo 		struct btrfs_fs_info *fs_info, u64 start)
21574358d963SJosef Bacik {
215832da5386SDavid Sterba 	struct btrfs_block_group *cache;
21594358d963SJosef Bacik 
21604358d963SJosef Bacik 	cache = kzalloc(sizeof(*cache), GFP_NOFS);
21614358d963SJosef Bacik 	if (!cache)
21624358d963SJosef Bacik 		return NULL;
21634358d963SJosef Bacik 
21644358d963SJosef Bacik 	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
21654358d963SJosef Bacik 					GFP_NOFS);
21664358d963SJosef Bacik 	if (!cache->free_space_ctl) {
21674358d963SJosef Bacik 		kfree(cache);
21684358d963SJosef Bacik 		return NULL;
21694358d963SJosef Bacik 	}
21704358d963SJosef Bacik 
2171b3470b5dSDavid Sterba 	cache->start = start;
21724358d963SJosef Bacik 
21734358d963SJosef Bacik 	cache->fs_info = fs_info;
21744358d963SJosef Bacik 	cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
21754358d963SJosef Bacik 
21766e80d4f8SDennis Zhou 	cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED;
21776e80d4f8SDennis Zhou 
217848aaeebeSJosef Bacik 	refcount_set(&cache->refs, 1);
21794358d963SJosef Bacik 	spin_lock_init(&cache->lock);
21804358d963SJosef Bacik 	init_rwsem(&cache->data_rwsem);
21814358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->list);
21824358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->cluster_list);
21834358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->bg_list);
21844358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->ro_list);
2185b0643e59SDennis Zhou 	INIT_LIST_HEAD(&cache->discard_list);
21864358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->dirty_list);
21874358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->io_list);
2188afba2bc0SNaohiro Aota 	INIT_LIST_HEAD(&cache->active_bg_list);
2189cd79909bSJosef Bacik 	btrfs_init_free_space_ctl(cache, cache->free_space_ctl);
21906b7304afSFilipe Manana 	atomic_set(&cache->frozen, 0);
21914358d963SJosef Bacik 	mutex_init(&cache->free_space_lock);
21924358d963SJosef Bacik 
21934358d963SJosef Bacik 	return cache;
21944358d963SJosef Bacik }
21954358d963SJosef Bacik 
21964358d963SJosef Bacik /*
21974358d963SJosef Bacik  * Iterate all chunks and verify that each of them has the corresponding block
21984358d963SJosef Bacik  * group
21994358d963SJosef Bacik  */
22004358d963SJosef Bacik static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
22014358d963SJosef Bacik {
22024358d963SJosef Bacik 	struct extent_map_tree *map_tree = &fs_info->mapping_tree;
22034358d963SJosef Bacik 	struct extent_map *em;
220432da5386SDavid Sterba 	struct btrfs_block_group *bg;
22054358d963SJosef Bacik 	u64 start = 0;
22064358d963SJosef Bacik 	int ret = 0;
22074358d963SJosef Bacik 
22084358d963SJosef Bacik 	while (1) {
22094358d963SJosef Bacik 		read_lock(&map_tree->lock);
22104358d963SJosef Bacik 		/*
22114358d963SJosef Bacik 		 * lookup_extent_mapping will return the first extent map
22124358d963SJosef Bacik 		 * intersecting the range, so setting @len to 1 is enough to
22134358d963SJosef Bacik 		 * get the first chunk.
22144358d963SJosef Bacik 		 */
22154358d963SJosef Bacik 		em = lookup_extent_mapping(map_tree, start, 1);
22164358d963SJosef Bacik 		read_unlock(&map_tree->lock);
22174358d963SJosef Bacik 		if (!em)
22184358d963SJosef Bacik 			break;
22194358d963SJosef Bacik 
22204358d963SJosef Bacik 		bg = btrfs_lookup_block_group(fs_info, em->start);
22214358d963SJosef Bacik 		if (!bg) {
22224358d963SJosef Bacik 			btrfs_err(fs_info,
22234358d963SJosef Bacik 	"chunk start=%llu len=%llu doesn't have corresponding block group",
22244358d963SJosef Bacik 				     em->start, em->len);
22254358d963SJosef Bacik 			ret = -EUCLEAN;
22264358d963SJosef Bacik 			free_extent_map(em);
22274358d963SJosef Bacik 			break;
22284358d963SJosef Bacik 		}
2229b3470b5dSDavid Sterba 		if (bg->start != em->start || bg->length != em->len ||
22304358d963SJosef Bacik 		    (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
22314358d963SJosef Bacik 		    (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
22324358d963SJosef Bacik 			btrfs_err(fs_info,
22334358d963SJosef Bacik "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",
22344358d963SJosef Bacik 				em->start, em->len,
22354358d963SJosef Bacik 				em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
2236b3470b5dSDavid Sterba 				bg->start, bg->length,
22374358d963SJosef Bacik 				bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
22384358d963SJosef Bacik 			ret = -EUCLEAN;
22394358d963SJosef Bacik 			free_extent_map(em);
22404358d963SJosef Bacik 			btrfs_put_block_group(bg);
22414358d963SJosef Bacik 			break;
22424358d963SJosef Bacik 		}
22434358d963SJosef Bacik 		start = em->start + em->len;
22444358d963SJosef Bacik 		free_extent_map(em);
22454358d963SJosef Bacik 		btrfs_put_block_group(bg);
22464358d963SJosef Bacik 	}
22474358d963SJosef Bacik 	return ret;
22484358d963SJosef Bacik }
22494358d963SJosef Bacik 
2250ffb9e0f0SQu Wenruo static int read_one_block_group(struct btrfs_fs_info *info,
22514afd2fe8SJohannes Thumshirn 				struct btrfs_block_group_item *bgi,
2252d49a2ddbSQu Wenruo 				const struct btrfs_key *key,
2253ffb9e0f0SQu Wenruo 				int need_clear)
2254ffb9e0f0SQu Wenruo {
225532da5386SDavid Sterba 	struct btrfs_block_group *cache;
2256ffb9e0f0SQu Wenruo 	const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS);
2257ffb9e0f0SQu Wenruo 	int ret;
2258ffb9e0f0SQu Wenruo 
2259d49a2ddbSQu Wenruo 	ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY);
2260ffb9e0f0SQu Wenruo 
22619afc6649SQu Wenruo 	cache = btrfs_create_block_group_cache(info, key->objectid);
2262ffb9e0f0SQu Wenruo 	if (!cache)
2263ffb9e0f0SQu Wenruo 		return -ENOMEM;
2264ffb9e0f0SQu Wenruo 
22654afd2fe8SJohannes Thumshirn 	cache->length = key->offset;
22664afd2fe8SJohannes Thumshirn 	cache->used = btrfs_stack_block_group_used(bgi);
22677248e0ceSQu Wenruo 	cache->commit_used = cache->used;
22684afd2fe8SJohannes Thumshirn 	cache->flags = btrfs_stack_block_group_flags(bgi);
2269f7238e50SJosef Bacik 	cache->global_root_id = btrfs_stack_block_group_chunk_objectid(bgi);
22709afc6649SQu Wenruo 
2271e3e39c72SMarcos Paulo de Souza 	set_free_space_tree_thresholds(cache);
2272e3e39c72SMarcos Paulo de Souza 
2273ffb9e0f0SQu Wenruo 	if (need_clear) {
2274ffb9e0f0SQu Wenruo 		/*
2275ffb9e0f0SQu Wenruo 		 * When we mount with old space cache, we need to
2276ffb9e0f0SQu Wenruo 		 * set BTRFS_DC_CLEAR and set dirty flag.
2277ffb9e0f0SQu Wenruo 		 *
2278ffb9e0f0SQu Wenruo 		 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
2279ffb9e0f0SQu Wenruo 		 *    truncate the old free space cache inode and
2280ffb9e0f0SQu Wenruo 		 *    setup a new one.
2281ffb9e0f0SQu Wenruo 		 * b) Setting 'dirty flag' makes sure that we flush
2282ffb9e0f0SQu Wenruo 		 *    the new space cache info onto disk.
2283ffb9e0f0SQu Wenruo 		 */
2284ffb9e0f0SQu Wenruo 		if (btrfs_test_opt(info, SPACE_CACHE))
2285ffb9e0f0SQu Wenruo 			cache->disk_cache_state = BTRFS_DC_CLEAR;
2286ffb9e0f0SQu Wenruo 	}
2287ffb9e0f0SQu Wenruo 	if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
2288ffb9e0f0SQu Wenruo 	    (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
2289ffb9e0f0SQu Wenruo 			btrfs_err(info,
2290ffb9e0f0SQu Wenruo "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
2291ffb9e0f0SQu Wenruo 				  cache->start);
2292ffb9e0f0SQu Wenruo 			ret = -EINVAL;
2293ffb9e0f0SQu Wenruo 			goto error;
2294ffb9e0f0SQu Wenruo 	}
2295ffb9e0f0SQu Wenruo 
2296a94794d5SNaohiro Aota 	ret = btrfs_load_block_group_zone_info(cache, false);
229708e11a3dSNaohiro Aota 	if (ret) {
229808e11a3dSNaohiro Aota 		btrfs_err(info, "zoned: failed to load zone info of bg %llu",
229908e11a3dSNaohiro Aota 			  cache->start);
230008e11a3dSNaohiro Aota 		goto error;
230108e11a3dSNaohiro Aota 	}
230208e11a3dSNaohiro Aota 
2303ffb9e0f0SQu Wenruo 	/*
2304ffb9e0f0SQu Wenruo 	 * We need to exclude the super stripes now so that the space info has
2305ffb9e0f0SQu Wenruo 	 * super bytes accounted for, otherwise we'll think we have more space
2306ffb9e0f0SQu Wenruo 	 * than we actually do.
2307ffb9e0f0SQu Wenruo 	 */
2308ffb9e0f0SQu Wenruo 	ret = exclude_super_stripes(cache);
2309ffb9e0f0SQu Wenruo 	if (ret) {
2310ffb9e0f0SQu Wenruo 		/* We may have excluded something, so call this just in case. */
2311ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
2312ffb9e0f0SQu Wenruo 		goto error;
2313ffb9e0f0SQu Wenruo 	}
2314ffb9e0f0SQu Wenruo 
2315ffb9e0f0SQu Wenruo 	/*
2316169e0da9SNaohiro Aota 	 * For zoned filesystem, space after the allocation offset is the only
2317169e0da9SNaohiro Aota 	 * free space for a block group. So, we don't need any caching work.
2318169e0da9SNaohiro Aota 	 * btrfs_calc_zone_unusable() will set the amount of free space and
2319169e0da9SNaohiro Aota 	 * zone_unusable space.
2320169e0da9SNaohiro Aota 	 *
2321169e0da9SNaohiro Aota 	 * For regular filesystem, check for two cases, either we are full, and
2322169e0da9SNaohiro Aota 	 * therefore don't need to bother with the caching work since we won't
2323169e0da9SNaohiro Aota 	 * find any space, or we are empty, and we can just add all the space
2324169e0da9SNaohiro Aota 	 * in and be done with it.  This saves us _a_lot_ of time, particularly
2325169e0da9SNaohiro Aota 	 * in the full case.
2326ffb9e0f0SQu Wenruo 	 */
2327169e0da9SNaohiro Aota 	if (btrfs_is_zoned(info)) {
2328169e0da9SNaohiro Aota 		btrfs_calc_zone_unusable(cache);
2329c46c4247SNaohiro Aota 		/* Should not have any excluded extents. Just in case, though. */
2330c46c4247SNaohiro Aota 		btrfs_free_excluded_extents(cache);
2331169e0da9SNaohiro Aota 	} else if (cache->length == cache->used) {
2332ffb9e0f0SQu Wenruo 		cache->cached = BTRFS_CACHE_FINISHED;
2333ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
2334ffb9e0f0SQu Wenruo 	} else if (cache->used == 0) {
2335ffb9e0f0SQu Wenruo 		cache->cached = BTRFS_CACHE_FINISHED;
23363b9f0995SFilipe Manana 		ret = btrfs_add_new_free_space(cache, cache->start,
2337d8ccbd21SFilipe Manana 					       cache->start + cache->length, NULL);
2338ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
2339d8ccbd21SFilipe Manana 		if (ret)
2340d8ccbd21SFilipe Manana 			goto error;
2341ffb9e0f0SQu Wenruo 	}
2342ffb9e0f0SQu Wenruo 
2343ffb9e0f0SQu Wenruo 	ret = btrfs_add_block_group_cache(info, cache);
2344ffb9e0f0SQu Wenruo 	if (ret) {
2345ffb9e0f0SQu Wenruo 		btrfs_remove_free_space_cache(cache);
2346ffb9e0f0SQu Wenruo 		goto error;
2347ffb9e0f0SQu Wenruo 	}
2348ffb9e0f0SQu Wenruo 	trace_btrfs_add_block_group(info, cache, 0);
2349723de71dSJosef Bacik 	btrfs_add_bg_to_space_info(info, cache);
2350ffb9e0f0SQu Wenruo 
2351ffb9e0f0SQu Wenruo 	set_avail_alloc_bits(info, cache->flags);
2352a09f23c3SAnand Jain 	if (btrfs_chunk_writeable(info, cache->start)) {
2353a09f23c3SAnand Jain 		if (cache->used == 0) {
2354ffb9e0f0SQu Wenruo 			ASSERT(list_empty(&cache->bg_list));
23556e80d4f8SDennis Zhou 			if (btrfs_test_opt(info, DISCARD_ASYNC))
23566e80d4f8SDennis Zhou 				btrfs_discard_queue_work(&info->discard_ctl, cache);
23576e80d4f8SDennis Zhou 			else
2358ffb9e0f0SQu Wenruo 				btrfs_mark_bg_unused(cache);
2359ffb9e0f0SQu Wenruo 		}
2360a09f23c3SAnand Jain 	} else {
2361a09f23c3SAnand Jain 		inc_block_group_ro(cache, 1);
2362a09f23c3SAnand Jain 	}
2363a09f23c3SAnand Jain 
2364ffb9e0f0SQu Wenruo 	return 0;
2365ffb9e0f0SQu Wenruo error:
2366ffb9e0f0SQu Wenruo 	btrfs_put_block_group(cache);
2367ffb9e0f0SQu Wenruo 	return ret;
2368ffb9e0f0SQu Wenruo }
2369ffb9e0f0SQu Wenruo 
237042437a63SJosef Bacik static int fill_dummy_bgs(struct btrfs_fs_info *fs_info)
237142437a63SJosef Bacik {
237242437a63SJosef Bacik 	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
237342437a63SJosef Bacik 	struct rb_node *node;
237442437a63SJosef Bacik 	int ret = 0;
237542437a63SJosef Bacik 
237642437a63SJosef Bacik 	for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
237742437a63SJosef Bacik 		struct extent_map *em;
237842437a63SJosef Bacik 		struct map_lookup *map;
237942437a63SJosef Bacik 		struct btrfs_block_group *bg;
238042437a63SJosef Bacik 
238142437a63SJosef Bacik 		em = rb_entry(node, struct extent_map, rb_node);
238242437a63SJosef Bacik 		map = em->map_lookup;
238342437a63SJosef Bacik 		bg = btrfs_create_block_group_cache(fs_info, em->start);
238442437a63SJosef Bacik 		if (!bg) {
238542437a63SJosef Bacik 			ret = -ENOMEM;
238642437a63SJosef Bacik 			break;
238742437a63SJosef Bacik 		}
238842437a63SJosef Bacik 
238942437a63SJosef Bacik 		/* Fill dummy cache as FULL */
239042437a63SJosef Bacik 		bg->length = em->len;
239142437a63SJosef Bacik 		bg->flags = map->type;
239242437a63SJosef Bacik 		bg->cached = BTRFS_CACHE_FINISHED;
239342437a63SJosef Bacik 		bg->used = em->len;
239442437a63SJosef Bacik 		bg->flags = map->type;
239542437a63SJosef Bacik 		ret = btrfs_add_block_group_cache(fs_info, bg);
23962b29726cSQu Wenruo 		/*
23972b29726cSQu Wenruo 		 * We may have some valid block group cache added already, in
23982b29726cSQu Wenruo 		 * that case we skip to the next one.
23992b29726cSQu Wenruo 		 */
24002b29726cSQu Wenruo 		if (ret == -EEXIST) {
24012b29726cSQu Wenruo 			ret = 0;
24022b29726cSQu Wenruo 			btrfs_put_block_group(bg);
24032b29726cSQu Wenruo 			continue;
24042b29726cSQu Wenruo 		}
24052b29726cSQu Wenruo 
240642437a63SJosef Bacik 		if (ret) {
240742437a63SJosef Bacik 			btrfs_remove_free_space_cache(bg);
240842437a63SJosef Bacik 			btrfs_put_block_group(bg);
240942437a63SJosef Bacik 			break;
241042437a63SJosef Bacik 		}
24112b29726cSQu Wenruo 
2412723de71dSJosef Bacik 		btrfs_add_bg_to_space_info(fs_info, bg);
241342437a63SJosef Bacik 
241442437a63SJosef Bacik 		set_avail_alloc_bits(fs_info, bg->flags);
241542437a63SJosef Bacik 	}
241642437a63SJosef Bacik 	if (!ret)
241742437a63SJosef Bacik 		btrfs_init_global_block_rsv(fs_info);
241842437a63SJosef Bacik 	return ret;
241942437a63SJosef Bacik }
242042437a63SJosef Bacik 
24214358d963SJosef Bacik int btrfs_read_block_groups(struct btrfs_fs_info *info)
24224358d963SJosef Bacik {
2423dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(info);
24244358d963SJosef Bacik 	struct btrfs_path *path;
24254358d963SJosef Bacik 	int ret;
242632da5386SDavid Sterba 	struct btrfs_block_group *cache;
24274358d963SJosef Bacik 	struct btrfs_space_info *space_info;
24284358d963SJosef Bacik 	struct btrfs_key key;
24294358d963SJosef Bacik 	int need_clear = 0;
24304358d963SJosef Bacik 	u64 cache_gen;
24314358d963SJosef Bacik 
243281d5d614SQu Wenruo 	/*
243381d5d614SQu Wenruo 	 * Either no extent root (with ibadroots rescue option) or we have
243481d5d614SQu Wenruo 	 * unsupported RO options. The fs can never be mounted read-write, so no
243581d5d614SQu Wenruo 	 * need to waste time searching block group items.
243681d5d614SQu Wenruo 	 *
243781d5d614SQu Wenruo 	 * This also allows new extent tree related changes to be RO compat,
243881d5d614SQu Wenruo 	 * no need for a full incompat flag.
243981d5d614SQu Wenruo 	 */
244081d5d614SQu Wenruo 	if (!root || (btrfs_super_compat_ro_flags(info->super_copy) &
244181d5d614SQu Wenruo 		      ~BTRFS_FEATURE_COMPAT_RO_SUPP))
244242437a63SJosef Bacik 		return fill_dummy_bgs(info);
244342437a63SJosef Bacik 
24444358d963SJosef Bacik 	key.objectid = 0;
24454358d963SJosef Bacik 	key.offset = 0;
24464358d963SJosef Bacik 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
24474358d963SJosef Bacik 	path = btrfs_alloc_path();
24484358d963SJosef Bacik 	if (!path)
24494358d963SJosef Bacik 		return -ENOMEM;
24504358d963SJosef Bacik 
24514358d963SJosef Bacik 	cache_gen = btrfs_super_cache_generation(info->super_copy);
24524358d963SJosef Bacik 	if (btrfs_test_opt(info, SPACE_CACHE) &&
24534358d963SJosef Bacik 	    btrfs_super_generation(info->super_copy) != cache_gen)
24544358d963SJosef Bacik 		need_clear = 1;
24554358d963SJosef Bacik 	if (btrfs_test_opt(info, CLEAR_CACHE))
24564358d963SJosef Bacik 		need_clear = 1;
24574358d963SJosef Bacik 
24584358d963SJosef Bacik 	while (1) {
24594afd2fe8SJohannes Thumshirn 		struct btrfs_block_group_item bgi;
24604afd2fe8SJohannes Thumshirn 		struct extent_buffer *leaf;
24614afd2fe8SJohannes Thumshirn 		int slot;
24624afd2fe8SJohannes Thumshirn 
24634358d963SJosef Bacik 		ret = find_first_block_group(info, path, &key);
24644358d963SJosef Bacik 		if (ret > 0)
24654358d963SJosef Bacik 			break;
24664358d963SJosef Bacik 		if (ret != 0)
24674358d963SJosef Bacik 			goto error;
24684358d963SJosef Bacik 
24694afd2fe8SJohannes Thumshirn 		leaf = path->nodes[0];
24704afd2fe8SJohannes Thumshirn 		slot = path->slots[0];
24714afd2fe8SJohannes Thumshirn 
24724afd2fe8SJohannes Thumshirn 		read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
24734afd2fe8SJohannes Thumshirn 				   sizeof(bgi));
24744afd2fe8SJohannes Thumshirn 
24754afd2fe8SJohannes Thumshirn 		btrfs_item_key_to_cpu(leaf, &key, slot);
24764afd2fe8SJohannes Thumshirn 		btrfs_release_path(path);
24774afd2fe8SJohannes Thumshirn 		ret = read_one_block_group(info, &bgi, &key, need_clear);
2478ffb9e0f0SQu Wenruo 		if (ret < 0)
24794358d963SJosef Bacik 			goto error;
2480ffb9e0f0SQu Wenruo 		key.objectid += key.offset;
2481ffb9e0f0SQu Wenruo 		key.offset = 0;
24824358d963SJosef Bacik 	}
24837837fa88SJosef Bacik 	btrfs_release_path(path);
24844358d963SJosef Bacik 
248572804905SJosef Bacik 	list_for_each_entry(space_info, &info->space_info, list) {
248649ea112dSJosef Bacik 		int i;
248749ea112dSJosef Bacik 
248849ea112dSJosef Bacik 		for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
248949ea112dSJosef Bacik 			if (list_empty(&space_info->block_groups[i]))
249049ea112dSJosef Bacik 				continue;
249149ea112dSJosef Bacik 			cache = list_first_entry(&space_info->block_groups[i],
249249ea112dSJosef Bacik 						 struct btrfs_block_group,
249349ea112dSJosef Bacik 						 list);
249449ea112dSJosef Bacik 			btrfs_sysfs_add_block_group_type(cache);
249549ea112dSJosef Bacik 		}
249649ea112dSJosef Bacik 
24974358d963SJosef Bacik 		if (!(btrfs_get_alloc_profile(info, space_info->flags) &
24984358d963SJosef Bacik 		      (BTRFS_BLOCK_GROUP_RAID10 |
24994358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_RAID1_MASK |
25004358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_RAID56_MASK |
25014358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_DUP)))
25024358d963SJosef Bacik 			continue;
25034358d963SJosef Bacik 		/*
25044358d963SJosef Bacik 		 * Avoid allocating from un-mirrored block group if there are
25054358d963SJosef Bacik 		 * mirrored block groups.
25064358d963SJosef Bacik 		 */
25074358d963SJosef Bacik 		list_for_each_entry(cache,
25084358d963SJosef Bacik 				&space_info->block_groups[BTRFS_RAID_RAID0],
25094358d963SJosef Bacik 				list)
2510e11c0406SJosef Bacik 			inc_block_group_ro(cache, 1);
25114358d963SJosef Bacik 		list_for_each_entry(cache,
25124358d963SJosef Bacik 				&space_info->block_groups[BTRFS_RAID_SINGLE],
25134358d963SJosef Bacik 				list)
2514e11c0406SJosef Bacik 			inc_block_group_ro(cache, 1);
25154358d963SJosef Bacik 	}
25164358d963SJosef Bacik 
25174358d963SJosef Bacik 	btrfs_init_global_block_rsv(info);
25184358d963SJosef Bacik 	ret = check_chunk_block_group_mappings(info);
25194358d963SJosef Bacik error:
25204358d963SJosef Bacik 	btrfs_free_path(path);
25212b29726cSQu Wenruo 	/*
25222b29726cSQu Wenruo 	 * We've hit some error while reading the extent tree, and have
25232b29726cSQu Wenruo 	 * rescue=ibadroots mount option.
25242b29726cSQu Wenruo 	 * Try to fill the tree using dummy block groups so that the user can
25252b29726cSQu Wenruo 	 * continue to mount and grab their data.
25262b29726cSQu Wenruo 	 */
25272b29726cSQu Wenruo 	if (ret && btrfs_test_opt(info, IGNOREBADROOTS))
25282b29726cSQu Wenruo 		ret = fill_dummy_bgs(info);
25294358d963SJosef Bacik 	return ret;
25304358d963SJosef Bacik }
25314358d963SJosef Bacik 
253279bd3712SFilipe Manana /*
253379bd3712SFilipe Manana  * This function, insert_block_group_item(), belongs to the phase 2 of chunk
253479bd3712SFilipe Manana  * allocation.
253579bd3712SFilipe Manana  *
253679bd3712SFilipe Manana  * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
253779bd3712SFilipe Manana  * phases.
253879bd3712SFilipe Manana  */
253997f4728aSQu Wenruo static int insert_block_group_item(struct btrfs_trans_handle *trans,
254097f4728aSQu Wenruo 				   struct btrfs_block_group *block_group)
254197f4728aSQu Wenruo {
254297f4728aSQu Wenruo 	struct btrfs_fs_info *fs_info = trans->fs_info;
254397f4728aSQu Wenruo 	struct btrfs_block_group_item bgi;
2544dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
254597f4728aSQu Wenruo 	struct btrfs_key key;
2546675dfe12SFilipe Manana 	u64 old_commit_used;
2547675dfe12SFilipe Manana 	int ret;
254897f4728aSQu Wenruo 
254997f4728aSQu Wenruo 	spin_lock(&block_group->lock);
255097f4728aSQu Wenruo 	btrfs_set_stack_block_group_used(&bgi, block_group->used);
255197f4728aSQu Wenruo 	btrfs_set_stack_block_group_chunk_objectid(&bgi,
2552f7238e50SJosef Bacik 						   block_group->global_root_id);
255397f4728aSQu Wenruo 	btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
2554675dfe12SFilipe Manana 	old_commit_used = block_group->commit_used;
2555675dfe12SFilipe Manana 	block_group->commit_used = block_group->used;
255697f4728aSQu Wenruo 	key.objectid = block_group->start;
255797f4728aSQu Wenruo 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
255897f4728aSQu Wenruo 	key.offset = block_group->length;
255997f4728aSQu Wenruo 	spin_unlock(&block_group->lock);
256097f4728aSQu Wenruo 
2561675dfe12SFilipe Manana 	ret = btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi));
2562675dfe12SFilipe Manana 	if (ret < 0) {
2563675dfe12SFilipe Manana 		spin_lock(&block_group->lock);
2564675dfe12SFilipe Manana 		block_group->commit_used = old_commit_used;
2565675dfe12SFilipe Manana 		spin_unlock(&block_group->lock);
2566675dfe12SFilipe Manana 	}
2567675dfe12SFilipe Manana 
2568675dfe12SFilipe Manana 	return ret;
256997f4728aSQu Wenruo }
257097f4728aSQu Wenruo 
25712eadb9e7SNikolay Borisov static int insert_dev_extent(struct btrfs_trans_handle *trans,
25722eadb9e7SNikolay Borisov 			    struct btrfs_device *device, u64 chunk_offset,
25732eadb9e7SNikolay Borisov 			    u64 start, u64 num_bytes)
25742eadb9e7SNikolay Borisov {
25752eadb9e7SNikolay Borisov 	struct btrfs_fs_info *fs_info = device->fs_info;
25762eadb9e7SNikolay Borisov 	struct btrfs_root *root = fs_info->dev_root;
25772eadb9e7SNikolay Borisov 	struct btrfs_path *path;
25782eadb9e7SNikolay Borisov 	struct btrfs_dev_extent *extent;
25792eadb9e7SNikolay Borisov 	struct extent_buffer *leaf;
25802eadb9e7SNikolay Borisov 	struct btrfs_key key;
25812eadb9e7SNikolay Borisov 	int ret;
25822eadb9e7SNikolay Borisov 
25832eadb9e7SNikolay Borisov 	WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
25842eadb9e7SNikolay Borisov 	WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
25852eadb9e7SNikolay Borisov 	path = btrfs_alloc_path();
25862eadb9e7SNikolay Borisov 	if (!path)
25872eadb9e7SNikolay Borisov 		return -ENOMEM;
25882eadb9e7SNikolay Borisov 
25892eadb9e7SNikolay Borisov 	key.objectid = device->devid;
25902eadb9e7SNikolay Borisov 	key.type = BTRFS_DEV_EXTENT_KEY;
25912eadb9e7SNikolay Borisov 	key.offset = start;
25922eadb9e7SNikolay Borisov 	ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*extent));
25932eadb9e7SNikolay Borisov 	if (ret)
25942eadb9e7SNikolay Borisov 		goto out;
25952eadb9e7SNikolay Borisov 
25962eadb9e7SNikolay Borisov 	leaf = path->nodes[0];
25972eadb9e7SNikolay Borisov 	extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
25982eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_chunk_tree(leaf, extent, BTRFS_CHUNK_TREE_OBJECTID);
25992eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_chunk_objectid(leaf, extent,
26002eadb9e7SNikolay Borisov 					    BTRFS_FIRST_CHUNK_TREE_OBJECTID);
26012eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
26022eadb9e7SNikolay Borisov 
26032eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_length(leaf, extent, num_bytes);
2604*50564b65SFilipe Manana 	btrfs_mark_buffer_dirty(trans, leaf);
26052eadb9e7SNikolay Borisov out:
26062eadb9e7SNikolay Borisov 	btrfs_free_path(path);
26072eadb9e7SNikolay Borisov 	return ret;
26082eadb9e7SNikolay Borisov }
26092eadb9e7SNikolay Borisov 
26102eadb9e7SNikolay Borisov /*
26112eadb9e7SNikolay Borisov  * This function belongs to phase 2.
26122eadb9e7SNikolay Borisov  *
26132eadb9e7SNikolay Borisov  * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
26142eadb9e7SNikolay Borisov  * phases.
26152eadb9e7SNikolay Borisov  */
26162eadb9e7SNikolay Borisov static int insert_dev_extents(struct btrfs_trans_handle *trans,
26172eadb9e7SNikolay Borisov 				   u64 chunk_offset, u64 chunk_size)
26182eadb9e7SNikolay Borisov {
26192eadb9e7SNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
26202eadb9e7SNikolay Borisov 	struct btrfs_device *device;
26212eadb9e7SNikolay Borisov 	struct extent_map *em;
26222eadb9e7SNikolay Borisov 	struct map_lookup *map;
26232eadb9e7SNikolay Borisov 	u64 dev_offset;
26242eadb9e7SNikolay Borisov 	u64 stripe_size;
26252eadb9e7SNikolay Borisov 	int i;
26262eadb9e7SNikolay Borisov 	int ret = 0;
26272eadb9e7SNikolay Borisov 
26282eadb9e7SNikolay Borisov 	em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
26292eadb9e7SNikolay Borisov 	if (IS_ERR(em))
26302eadb9e7SNikolay Borisov 		return PTR_ERR(em);
26312eadb9e7SNikolay Borisov 
26322eadb9e7SNikolay Borisov 	map = em->map_lookup;
26332eadb9e7SNikolay Borisov 	stripe_size = em->orig_block_len;
26342eadb9e7SNikolay Borisov 
26352eadb9e7SNikolay Borisov 	/*
26362eadb9e7SNikolay Borisov 	 * Take the device list mutex to prevent races with the final phase of
26372eadb9e7SNikolay Borisov 	 * a device replace operation that replaces the device object associated
26382eadb9e7SNikolay Borisov 	 * with the map's stripes, because the device object's id can change
26392eadb9e7SNikolay Borisov 	 * at any time during that final phase of the device replace operation
26402eadb9e7SNikolay Borisov 	 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the
26412eadb9e7SNikolay Borisov 	 * replaced device and then see it with an ID of BTRFS_DEV_REPLACE_DEVID,
26422eadb9e7SNikolay Borisov 	 * resulting in persisting a device extent item with such ID.
26432eadb9e7SNikolay Borisov 	 */
26442eadb9e7SNikolay Borisov 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
26452eadb9e7SNikolay Borisov 	for (i = 0; i < map->num_stripes; i++) {
26462eadb9e7SNikolay Borisov 		device = map->stripes[i].dev;
26472eadb9e7SNikolay Borisov 		dev_offset = map->stripes[i].physical;
26482eadb9e7SNikolay Borisov 
26492eadb9e7SNikolay Borisov 		ret = insert_dev_extent(trans, device, chunk_offset, dev_offset,
26502eadb9e7SNikolay Borisov 				       stripe_size);
26512eadb9e7SNikolay Borisov 		if (ret)
26522eadb9e7SNikolay Borisov 			break;
26532eadb9e7SNikolay Borisov 	}
26542eadb9e7SNikolay Borisov 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
26552eadb9e7SNikolay Borisov 
26562eadb9e7SNikolay Borisov 	free_extent_map(em);
26572eadb9e7SNikolay Borisov 	return ret;
26582eadb9e7SNikolay Borisov }
26592eadb9e7SNikolay Borisov 
266079bd3712SFilipe Manana /*
266179bd3712SFilipe Manana  * This function, btrfs_create_pending_block_groups(), belongs to the phase 2 of
266279bd3712SFilipe Manana  * chunk allocation.
266379bd3712SFilipe Manana  *
266479bd3712SFilipe Manana  * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
266579bd3712SFilipe Manana  * phases.
266679bd3712SFilipe Manana  */
26674358d963SJosef Bacik void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
26684358d963SJosef Bacik {
26694358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
267032da5386SDavid Sterba 	struct btrfs_block_group *block_group;
26714358d963SJosef Bacik 	int ret = 0;
26724358d963SJosef Bacik 
26734358d963SJosef Bacik 	while (!list_empty(&trans->new_bgs)) {
267449ea112dSJosef Bacik 		int index;
267549ea112dSJosef Bacik 
26764358d963SJosef Bacik 		block_group = list_first_entry(&trans->new_bgs,
267732da5386SDavid Sterba 					       struct btrfs_block_group,
26784358d963SJosef Bacik 					       bg_list);
26794358d963SJosef Bacik 		if (ret)
26804358d963SJosef Bacik 			goto next;
26814358d963SJosef Bacik 
268249ea112dSJosef Bacik 		index = btrfs_bg_flags_to_raid_index(block_group->flags);
268349ea112dSJosef Bacik 
268497f4728aSQu Wenruo 		ret = insert_block_group_item(trans, block_group);
26854358d963SJosef Bacik 		if (ret)
26864358d963SJosef Bacik 			btrfs_abort_transaction(trans, ret);
26873349b57fSJosef Bacik 		if (!test_bit(BLOCK_GROUP_FLAG_CHUNK_ITEM_INSERTED,
26883349b57fSJosef Bacik 			      &block_group->runtime_flags)) {
268979bd3712SFilipe Manana 			mutex_lock(&fs_info->chunk_mutex);
269079bd3712SFilipe Manana 			ret = btrfs_chunk_alloc_add_chunk_item(trans, block_group);
269179bd3712SFilipe Manana 			mutex_unlock(&fs_info->chunk_mutex);
269279bd3712SFilipe Manana 			if (ret)
269379bd3712SFilipe Manana 				btrfs_abort_transaction(trans, ret);
269479bd3712SFilipe Manana 		}
26952eadb9e7SNikolay Borisov 		ret = insert_dev_extents(trans, block_group->start,
269697f4728aSQu Wenruo 					 block_group->length);
26974358d963SJosef Bacik 		if (ret)
26984358d963SJosef Bacik 			btrfs_abort_transaction(trans, ret);
26994358d963SJosef Bacik 		add_block_group_free_space(trans, block_group);
270049ea112dSJosef Bacik 
270149ea112dSJosef Bacik 		/*
270249ea112dSJosef Bacik 		 * If we restriped during balance, we may have added a new raid
270349ea112dSJosef Bacik 		 * type, so now add the sysfs entries when it is safe to do so.
270449ea112dSJosef Bacik 		 * We don't have to worry about locking here as it's handled in
270549ea112dSJosef Bacik 		 * btrfs_sysfs_add_block_group_type.
270649ea112dSJosef Bacik 		 */
270749ea112dSJosef Bacik 		if (block_group->space_info->block_group_kobjs[index] == NULL)
270849ea112dSJosef Bacik 			btrfs_sysfs_add_block_group_type(block_group);
270949ea112dSJosef Bacik 
27104358d963SJosef Bacik 		/* Already aborted the transaction if it failed. */
27114358d963SJosef Bacik next:
2712adb86dbeSFilipe Manana 		btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
27134358d963SJosef Bacik 		list_del_init(&block_group->bg_list);
27140657b20cSFilipe Manana 		clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags);
27154358d963SJosef Bacik 	}
27164358d963SJosef Bacik 	btrfs_trans_release_chunk_metadata(trans);
27174358d963SJosef Bacik }
27184358d963SJosef Bacik 
2719f7238e50SJosef Bacik /*
2720f7238e50SJosef Bacik  * For extent tree v2 we use the block_group_item->chunk_offset to point at our
2721f7238e50SJosef Bacik  * global root id.  For v1 it's always set to BTRFS_FIRST_CHUNK_TREE_OBJECTID.
2722f7238e50SJosef Bacik  */
2723f7238e50SJosef Bacik static u64 calculate_global_root_id(struct btrfs_fs_info *fs_info, u64 offset)
2724f7238e50SJosef Bacik {
2725f7238e50SJosef Bacik 	u64 div = SZ_1G;
2726f7238e50SJosef Bacik 	u64 index;
2727f7238e50SJosef Bacik 
2728f7238e50SJosef Bacik 	if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
2729f7238e50SJosef Bacik 		return BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2730f7238e50SJosef Bacik 
2731f7238e50SJosef Bacik 	/* If we have a smaller fs index based on 128MiB. */
2732f7238e50SJosef Bacik 	if (btrfs_super_total_bytes(fs_info->super_copy) <= (SZ_1G * 10ULL))
2733f7238e50SJosef Bacik 		div = SZ_128M;
2734f7238e50SJosef Bacik 
2735f7238e50SJosef Bacik 	offset = div64_u64(offset, div);
2736f7238e50SJosef Bacik 	div64_u64_rem(offset, fs_info->nr_global_roots, &index);
2737f7238e50SJosef Bacik 	return index;
2738f7238e50SJosef Bacik }
2739f7238e50SJosef Bacik 
274079bd3712SFilipe Manana struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *trans,
27415758d1bdSFilipe Manana 						 u64 type,
274279bd3712SFilipe Manana 						 u64 chunk_offset, u64 size)
27434358d963SJosef Bacik {
27444358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
274532da5386SDavid Sterba 	struct btrfs_block_group *cache;
27464358d963SJosef Bacik 	int ret;
27474358d963SJosef Bacik 
27484358d963SJosef Bacik 	btrfs_set_log_full_commit(trans);
27494358d963SJosef Bacik 
27509afc6649SQu Wenruo 	cache = btrfs_create_block_group_cache(fs_info, chunk_offset);
27514358d963SJosef Bacik 	if (!cache)
275279bd3712SFilipe Manana 		return ERR_PTR(-ENOMEM);
27534358d963SJosef Bacik 
27540657b20cSFilipe Manana 	/*
27550657b20cSFilipe Manana 	 * Mark it as new before adding it to the rbtree of block groups or any
27560657b20cSFilipe Manana 	 * list, so that no other task finds it and calls btrfs_mark_bg_unused()
27570657b20cSFilipe Manana 	 * before the new flag is set.
27580657b20cSFilipe Manana 	 */
27590657b20cSFilipe Manana 	set_bit(BLOCK_GROUP_FLAG_NEW, &cache->runtime_flags);
27600657b20cSFilipe Manana 
27619afc6649SQu Wenruo 	cache->length = size;
2762e3e39c72SMarcos Paulo de Souza 	set_free_space_tree_thresholds(cache);
27634358d963SJosef Bacik 	cache->flags = type;
27644358d963SJosef Bacik 	cache->cached = BTRFS_CACHE_FINISHED;
2765f7238e50SJosef Bacik 	cache->global_root_id = calculate_global_root_id(fs_info, cache->start);
2766f7238e50SJosef Bacik 
2767997e3e2eSBoris Burkov 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
27680d7764ffSDavid Sterba 		set_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &cache->runtime_flags);
276908e11a3dSNaohiro Aota 
2770a94794d5SNaohiro Aota 	ret = btrfs_load_block_group_zone_info(cache, true);
277108e11a3dSNaohiro Aota 	if (ret) {
277208e11a3dSNaohiro Aota 		btrfs_put_block_group(cache);
277379bd3712SFilipe Manana 		return ERR_PTR(ret);
277408e11a3dSNaohiro Aota 	}
277508e11a3dSNaohiro Aota 
27764358d963SJosef Bacik 	ret = exclude_super_stripes(cache);
27774358d963SJosef Bacik 	if (ret) {
27784358d963SJosef Bacik 		/* We may have excluded something, so call this just in case */
27794358d963SJosef Bacik 		btrfs_free_excluded_extents(cache);
27804358d963SJosef Bacik 		btrfs_put_block_group(cache);
278179bd3712SFilipe Manana 		return ERR_PTR(ret);
27824358d963SJosef Bacik 	}
27834358d963SJosef Bacik 
27843b9f0995SFilipe Manana 	ret = btrfs_add_new_free_space(cache, chunk_offset, chunk_offset + size, NULL);
27854358d963SJosef Bacik 	btrfs_free_excluded_extents(cache);
2786d8ccbd21SFilipe Manana 	if (ret) {
2787d8ccbd21SFilipe Manana 		btrfs_put_block_group(cache);
2788d8ccbd21SFilipe Manana 		return ERR_PTR(ret);
2789d8ccbd21SFilipe Manana 	}
27904358d963SJosef Bacik 
27914358d963SJosef Bacik 	/*
27924358d963SJosef Bacik 	 * Ensure the corresponding space_info object is created and
27934358d963SJosef Bacik 	 * assigned to our block group. We want our bg to be added to the rbtree
27944358d963SJosef Bacik 	 * with its ->space_info set.
27954358d963SJosef Bacik 	 */
27964358d963SJosef Bacik 	cache->space_info = btrfs_find_space_info(fs_info, cache->flags);
27974358d963SJosef Bacik 	ASSERT(cache->space_info);
27984358d963SJosef Bacik 
27994358d963SJosef Bacik 	ret = btrfs_add_block_group_cache(fs_info, cache);
28004358d963SJosef Bacik 	if (ret) {
28014358d963SJosef Bacik 		btrfs_remove_free_space_cache(cache);
28024358d963SJosef Bacik 		btrfs_put_block_group(cache);
280379bd3712SFilipe Manana 		return ERR_PTR(ret);
28044358d963SJosef Bacik 	}
28054358d963SJosef Bacik 
28064358d963SJosef Bacik 	/*
28074358d963SJosef Bacik 	 * Now that our block group has its ->space_info set and is inserted in
28084358d963SJosef Bacik 	 * the rbtree, update the space info's counters.
28094358d963SJosef Bacik 	 */
28104358d963SJosef Bacik 	trace_btrfs_add_block_group(fs_info, cache, 1);
2811723de71dSJosef Bacik 	btrfs_add_bg_to_space_info(fs_info, cache);
28124358d963SJosef Bacik 	btrfs_update_global_block_rsv(fs_info);
28134358d963SJosef Bacik 
28149d4b0a12SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
28159d4b0a12SJosef Bacik 	if (btrfs_should_fragment_free_space(cache)) {
28165758d1bdSFilipe Manana 		cache->space_info->bytes_used += size >> 1;
28179d4b0a12SJosef Bacik 		fragment_free_space(cache);
28189d4b0a12SJosef Bacik 	}
28199d4b0a12SJosef Bacik #endif
28204358d963SJosef Bacik 
28214358d963SJosef Bacik 	list_add_tail(&cache->bg_list, &trans->new_bgs);
28224358d963SJosef Bacik 	trans->delayed_ref_updates++;
28234358d963SJosef Bacik 	btrfs_update_delayed_refs_rsv(trans);
28244358d963SJosef Bacik 
28254358d963SJosef Bacik 	set_avail_alloc_bits(fs_info, type);
282679bd3712SFilipe Manana 	return cache;
28274358d963SJosef Bacik }
282826ce2095SJosef Bacik 
2829b12de528SQu Wenruo /*
2830b12de528SQu Wenruo  * Mark one block group RO, can be called several times for the same block
2831b12de528SQu Wenruo  * group.
2832b12de528SQu Wenruo  *
2833b12de528SQu Wenruo  * @cache:		the destination block group
2834b12de528SQu Wenruo  * @do_chunk_alloc:	whether need to do chunk pre-allocation, this is to
2835b12de528SQu Wenruo  * 			ensure we still have some free space after marking this
2836b12de528SQu Wenruo  * 			block group RO.
2837b12de528SQu Wenruo  */
2838b12de528SQu Wenruo int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
2839b12de528SQu Wenruo 			     bool do_chunk_alloc)
284026ce2095SJosef Bacik {
284126ce2095SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
284226ce2095SJosef Bacik 	struct btrfs_trans_handle *trans;
2843dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
284426ce2095SJosef Bacik 	u64 alloc_flags;
284526ce2095SJosef Bacik 	int ret;
2846b6e9f16cSNikolay Borisov 	bool dirty_bg_running;
284726ce2095SJosef Bacik 
28482d192fc4SQu Wenruo 	/*
28492d192fc4SQu Wenruo 	 * This can only happen when we are doing read-only scrub on read-only
28502d192fc4SQu Wenruo 	 * mount.
28512d192fc4SQu Wenruo 	 * In that case we should not start a new transaction on read-only fs.
28522d192fc4SQu Wenruo 	 * Thus here we skip all chunk allocations.
28532d192fc4SQu Wenruo 	 */
28542d192fc4SQu Wenruo 	if (sb_rdonly(fs_info->sb)) {
28552d192fc4SQu Wenruo 		mutex_lock(&fs_info->ro_block_group_mutex);
28562d192fc4SQu Wenruo 		ret = inc_block_group_ro(cache, 0);
28572d192fc4SQu Wenruo 		mutex_unlock(&fs_info->ro_block_group_mutex);
28582d192fc4SQu Wenruo 		return ret;
28592d192fc4SQu Wenruo 	}
28602d192fc4SQu Wenruo 
2861b6e9f16cSNikolay Borisov 	do {
2862dfe8aec4SJosef Bacik 		trans = btrfs_join_transaction(root);
286326ce2095SJosef Bacik 		if (IS_ERR(trans))
286426ce2095SJosef Bacik 			return PTR_ERR(trans);
286526ce2095SJosef Bacik 
2866b6e9f16cSNikolay Borisov 		dirty_bg_running = false;
2867b6e9f16cSNikolay Borisov 
286826ce2095SJosef Bacik 		/*
2869b6e9f16cSNikolay Borisov 		 * We're not allowed to set block groups readonly after the dirty
2870b6e9f16cSNikolay Borisov 		 * block group cache has started writing.  If it already started,
2871b6e9f16cSNikolay Borisov 		 * back off and let this transaction commit.
287226ce2095SJosef Bacik 		 */
287326ce2095SJosef Bacik 		mutex_lock(&fs_info->ro_block_group_mutex);
287426ce2095SJosef Bacik 		if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
287526ce2095SJosef Bacik 			u64 transid = trans->transid;
287626ce2095SJosef Bacik 
287726ce2095SJosef Bacik 			mutex_unlock(&fs_info->ro_block_group_mutex);
287826ce2095SJosef Bacik 			btrfs_end_transaction(trans);
287926ce2095SJosef Bacik 
288026ce2095SJosef Bacik 			ret = btrfs_wait_for_commit(fs_info, transid);
288126ce2095SJosef Bacik 			if (ret)
288226ce2095SJosef Bacik 				return ret;
2883b6e9f16cSNikolay Borisov 			dirty_bg_running = true;
288426ce2095SJosef Bacik 		}
2885b6e9f16cSNikolay Borisov 	} while (dirty_bg_running);
288626ce2095SJosef Bacik 
2887b12de528SQu Wenruo 	if (do_chunk_alloc) {
288826ce2095SJosef Bacik 		/*
2889b12de528SQu Wenruo 		 * If we are changing raid levels, try to allocate a
2890b12de528SQu Wenruo 		 * corresponding block group with the new raid level.
289126ce2095SJosef Bacik 		 */
2892349e120eSJosef Bacik 		alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
289326ce2095SJosef Bacik 		if (alloc_flags != cache->flags) {
2894b12de528SQu Wenruo 			ret = btrfs_chunk_alloc(trans, alloc_flags,
2895b12de528SQu Wenruo 						CHUNK_ALLOC_FORCE);
289626ce2095SJosef Bacik 			/*
289726ce2095SJosef Bacik 			 * ENOSPC is allowed here, we may have enough space
2898b12de528SQu Wenruo 			 * already allocated at the new raid level to carry on
289926ce2095SJosef Bacik 			 */
290026ce2095SJosef Bacik 			if (ret == -ENOSPC)
290126ce2095SJosef Bacik 				ret = 0;
290226ce2095SJosef Bacik 			if (ret < 0)
290326ce2095SJosef Bacik 				goto out;
290426ce2095SJosef Bacik 		}
2905b12de528SQu Wenruo 	}
290626ce2095SJosef Bacik 
2907a7a63accSJosef Bacik 	ret = inc_block_group_ro(cache, 0);
290826ce2095SJosef Bacik 	if (!ret)
290926ce2095SJosef Bacik 		goto out;
29107561551eSQu Wenruo 	if (ret == -ETXTBSY)
29117561551eSQu Wenruo 		goto unlock_out;
29127561551eSQu Wenruo 
29137561551eSQu Wenruo 	/*
29147561551eSQu Wenruo 	 * Skip chunk alloction if the bg is SYSTEM, this is to avoid system
29157561551eSQu Wenruo 	 * chunk allocation storm to exhaust the system chunk array.  Otherwise
29167561551eSQu Wenruo 	 * we still want to try our best to mark the block group read-only.
29177561551eSQu Wenruo 	 */
29187561551eSQu Wenruo 	if (!do_chunk_alloc && ret == -ENOSPC &&
29197561551eSQu Wenruo 	    (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM))
29207561551eSQu Wenruo 		goto unlock_out;
29217561551eSQu Wenruo 
292226ce2095SJosef Bacik 	alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags);
292326ce2095SJosef Bacik 	ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
292426ce2095SJosef Bacik 	if (ret < 0)
292526ce2095SJosef Bacik 		goto out;
2926b6a98021SNaohiro Aota 	/*
2927b6a98021SNaohiro Aota 	 * We have allocated a new chunk. We also need to activate that chunk to
2928b6a98021SNaohiro Aota 	 * grant metadata tickets for zoned filesystem.
2929b6a98021SNaohiro Aota 	 */
2930b6a98021SNaohiro Aota 	ret = btrfs_zoned_activate_one_bg(fs_info, cache->space_info, true);
2931b6a98021SNaohiro Aota 	if (ret < 0)
2932b6a98021SNaohiro Aota 		goto out;
2933b6a98021SNaohiro Aota 
2934e11c0406SJosef Bacik 	ret = inc_block_group_ro(cache, 0);
2935195a49eaSFilipe Manana 	if (ret == -ETXTBSY)
2936195a49eaSFilipe Manana 		goto unlock_out;
293726ce2095SJosef Bacik out:
293826ce2095SJosef Bacik 	if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2939349e120eSJosef Bacik 		alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
294026ce2095SJosef Bacik 		mutex_lock(&fs_info->chunk_mutex);
294126ce2095SJosef Bacik 		check_system_chunk(trans, alloc_flags);
294226ce2095SJosef Bacik 		mutex_unlock(&fs_info->chunk_mutex);
294326ce2095SJosef Bacik 	}
2944b12de528SQu Wenruo unlock_out:
294526ce2095SJosef Bacik 	mutex_unlock(&fs_info->ro_block_group_mutex);
294626ce2095SJosef Bacik 
294726ce2095SJosef Bacik 	btrfs_end_transaction(trans);
294826ce2095SJosef Bacik 	return ret;
294926ce2095SJosef Bacik }
295026ce2095SJosef Bacik 
295132da5386SDavid Sterba void btrfs_dec_block_group_ro(struct btrfs_block_group *cache)
295226ce2095SJosef Bacik {
295326ce2095SJosef Bacik 	struct btrfs_space_info *sinfo = cache->space_info;
295426ce2095SJosef Bacik 	u64 num_bytes;
295526ce2095SJosef Bacik 
295626ce2095SJosef Bacik 	BUG_ON(!cache->ro);
295726ce2095SJosef Bacik 
295826ce2095SJosef Bacik 	spin_lock(&sinfo->lock);
295926ce2095SJosef Bacik 	spin_lock(&cache->lock);
296026ce2095SJosef Bacik 	if (!--cache->ro) {
2961169e0da9SNaohiro Aota 		if (btrfs_is_zoned(cache->fs_info)) {
2962169e0da9SNaohiro Aota 			/* Migrate zone_unusable bytes back */
296398173255SNaohiro Aota 			cache->zone_unusable =
296498173255SNaohiro Aota 				(cache->alloc_offset - cache->used) +
296598173255SNaohiro Aota 				(cache->length - cache->zone_capacity);
2966169e0da9SNaohiro Aota 			sinfo->bytes_zone_unusable += cache->zone_unusable;
2967169e0da9SNaohiro Aota 			sinfo->bytes_readonly -= cache->zone_unusable;
2968169e0da9SNaohiro Aota 		}
2969f9f28e5bSNaohiro Aota 		num_bytes = cache->length - cache->reserved -
2970f9f28e5bSNaohiro Aota 			    cache->pinned - cache->bytes_super -
2971f9f28e5bSNaohiro Aota 			    cache->zone_unusable - cache->used;
2972f9f28e5bSNaohiro Aota 		sinfo->bytes_readonly -= num_bytes;
297326ce2095SJosef Bacik 		list_del_init(&cache->ro_list);
297426ce2095SJosef Bacik 	}
297526ce2095SJosef Bacik 	spin_unlock(&cache->lock);
297626ce2095SJosef Bacik 	spin_unlock(&sinfo->lock);
297726ce2095SJosef Bacik }
297877745c05SJosef Bacik 
29793be4d8efSQu Wenruo static int update_block_group_item(struct btrfs_trans_handle *trans,
298077745c05SJosef Bacik 				   struct btrfs_path *path,
298132da5386SDavid Sterba 				   struct btrfs_block_group *cache)
298277745c05SJosef Bacik {
298377745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
298477745c05SJosef Bacik 	int ret;
2985dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
298677745c05SJosef Bacik 	unsigned long bi;
298777745c05SJosef Bacik 	struct extent_buffer *leaf;
2988bf38be65SDavid Sterba 	struct btrfs_block_group_item bgi;
2989b3470b5dSDavid Sterba 	struct btrfs_key key;
29907248e0ceSQu Wenruo 	u64 old_commit_used;
29917248e0ceSQu Wenruo 	u64 used;
29927248e0ceSQu Wenruo 
29937248e0ceSQu Wenruo 	/*
29947248e0ceSQu Wenruo 	 * Block group items update can be triggered out of commit transaction
29957248e0ceSQu Wenruo 	 * critical section, thus we need a consistent view of used bytes.
29967248e0ceSQu Wenruo 	 * We cannot use cache->used directly outside of the spin lock, as it
29977248e0ceSQu Wenruo 	 * may be changed.
29987248e0ceSQu Wenruo 	 */
29997248e0ceSQu Wenruo 	spin_lock(&cache->lock);
30007248e0ceSQu Wenruo 	old_commit_used = cache->commit_used;
30017248e0ceSQu Wenruo 	used = cache->used;
30027248e0ceSQu Wenruo 	/* No change in used bytes, can safely skip it. */
30037248e0ceSQu Wenruo 	if (cache->commit_used == used) {
30047248e0ceSQu Wenruo 		spin_unlock(&cache->lock);
30057248e0ceSQu Wenruo 		return 0;
30067248e0ceSQu Wenruo 	}
30077248e0ceSQu Wenruo 	cache->commit_used = used;
30087248e0ceSQu Wenruo 	spin_unlock(&cache->lock);
300977745c05SJosef Bacik 
3010b3470b5dSDavid Sterba 	key.objectid = cache->start;
3011b3470b5dSDavid Sterba 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3012b3470b5dSDavid Sterba 	key.offset = cache->length;
3013b3470b5dSDavid Sterba 
30143be4d8efSQu Wenruo 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
301577745c05SJosef Bacik 	if (ret) {
301677745c05SJosef Bacik 		if (ret > 0)
301777745c05SJosef Bacik 			ret = -ENOENT;
301877745c05SJosef Bacik 		goto fail;
301977745c05SJosef Bacik 	}
302077745c05SJosef Bacik 
302177745c05SJosef Bacik 	leaf = path->nodes[0];
302277745c05SJosef Bacik 	bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
30237248e0ceSQu Wenruo 	btrfs_set_stack_block_group_used(&bgi, used);
3024de0dc456SDavid Sterba 	btrfs_set_stack_block_group_chunk_objectid(&bgi,
3025f7238e50SJosef Bacik 						   cache->global_root_id);
3026de0dc456SDavid Sterba 	btrfs_set_stack_block_group_flags(&bgi, cache->flags);
3027bf38be65SDavid Sterba 	write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
3028*50564b65SFilipe Manana 	btrfs_mark_buffer_dirty(trans, leaf);
302977745c05SJosef Bacik fail:
303077745c05SJosef Bacik 	btrfs_release_path(path);
30312d6cd791SFilipe Manana 	/*
30322d6cd791SFilipe Manana 	 * We didn't update the block group item, need to revert commit_used
30332d6cd791SFilipe Manana 	 * unless the block group item didn't exist yet - this is to prevent a
30342d6cd791SFilipe Manana 	 * race with a concurrent insertion of the block group item, with
30352d6cd791SFilipe Manana 	 * insert_block_group_item(), that happened just after we attempted to
30362d6cd791SFilipe Manana 	 * update. In that case we would reset commit_used to 0 just after the
30372d6cd791SFilipe Manana 	 * insertion set it to a value greater than 0 - if the block group later
30382d6cd791SFilipe Manana 	 * becomes with 0 used bytes, we would incorrectly skip its update.
30392d6cd791SFilipe Manana 	 */
30402d6cd791SFilipe Manana 	if (ret < 0 && ret != -ENOENT) {
30417248e0ceSQu Wenruo 		spin_lock(&cache->lock);
30427248e0ceSQu Wenruo 		cache->commit_used = old_commit_used;
30437248e0ceSQu Wenruo 		spin_unlock(&cache->lock);
30447248e0ceSQu Wenruo 	}
304577745c05SJosef Bacik 	return ret;
304677745c05SJosef Bacik 
304777745c05SJosef Bacik }
304877745c05SJosef Bacik 
304932da5386SDavid Sterba static int cache_save_setup(struct btrfs_block_group *block_group,
305077745c05SJosef Bacik 			    struct btrfs_trans_handle *trans,
305177745c05SJosef Bacik 			    struct btrfs_path *path)
305277745c05SJosef Bacik {
305377745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
305477745c05SJosef Bacik 	struct btrfs_root *root = fs_info->tree_root;
305577745c05SJosef Bacik 	struct inode *inode = NULL;
305677745c05SJosef Bacik 	struct extent_changeset *data_reserved = NULL;
305777745c05SJosef Bacik 	u64 alloc_hint = 0;
305877745c05SJosef Bacik 	int dcs = BTRFS_DC_ERROR;
30590044ae11SQu Wenruo 	u64 cache_size = 0;
306077745c05SJosef Bacik 	int retries = 0;
306177745c05SJosef Bacik 	int ret = 0;
306277745c05SJosef Bacik 
3063af456a2cSBoris Burkov 	if (!btrfs_test_opt(fs_info, SPACE_CACHE))
3064af456a2cSBoris Burkov 		return 0;
3065af456a2cSBoris Burkov 
306677745c05SJosef Bacik 	/*
306777745c05SJosef Bacik 	 * If this block group is smaller than 100 megs don't bother caching the
306877745c05SJosef Bacik 	 * block group.
306977745c05SJosef Bacik 	 */
3070b3470b5dSDavid Sterba 	if (block_group->length < (100 * SZ_1M)) {
307177745c05SJosef Bacik 		spin_lock(&block_group->lock);
307277745c05SJosef Bacik 		block_group->disk_cache_state = BTRFS_DC_WRITTEN;
307377745c05SJosef Bacik 		spin_unlock(&block_group->lock);
307477745c05SJosef Bacik 		return 0;
307577745c05SJosef Bacik 	}
307677745c05SJosef Bacik 
3077bf31f87fSDavid Sterba 	if (TRANS_ABORTED(trans))
307877745c05SJosef Bacik 		return 0;
307977745c05SJosef Bacik again:
308077745c05SJosef Bacik 	inode = lookup_free_space_inode(block_group, path);
308177745c05SJosef Bacik 	if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
308277745c05SJosef Bacik 		ret = PTR_ERR(inode);
308377745c05SJosef Bacik 		btrfs_release_path(path);
308477745c05SJosef Bacik 		goto out;
308577745c05SJosef Bacik 	}
308677745c05SJosef Bacik 
308777745c05SJosef Bacik 	if (IS_ERR(inode)) {
308877745c05SJosef Bacik 		BUG_ON(retries);
308977745c05SJosef Bacik 		retries++;
309077745c05SJosef Bacik 
309177745c05SJosef Bacik 		if (block_group->ro)
309277745c05SJosef Bacik 			goto out_free;
309377745c05SJosef Bacik 
309477745c05SJosef Bacik 		ret = create_free_space_inode(trans, block_group, path);
309577745c05SJosef Bacik 		if (ret)
309677745c05SJosef Bacik 			goto out_free;
309777745c05SJosef Bacik 		goto again;
309877745c05SJosef Bacik 	}
309977745c05SJosef Bacik 
310077745c05SJosef Bacik 	/*
310177745c05SJosef Bacik 	 * We want to set the generation to 0, that way if anything goes wrong
310277745c05SJosef Bacik 	 * from here on out we know not to trust this cache when we load up next
310377745c05SJosef Bacik 	 * time.
310477745c05SJosef Bacik 	 */
310577745c05SJosef Bacik 	BTRFS_I(inode)->generation = 0;
31069a56fcd1SNikolay Borisov 	ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
310777745c05SJosef Bacik 	if (ret) {
310877745c05SJosef Bacik 		/*
310977745c05SJosef Bacik 		 * So theoretically we could recover from this, simply set the
311077745c05SJosef Bacik 		 * super cache generation to 0 so we know to invalidate the
311177745c05SJosef Bacik 		 * cache, but then we'd have to keep track of the block groups
311277745c05SJosef Bacik 		 * that fail this way so we know we _have_ to reset this cache
311377745c05SJosef Bacik 		 * before the next commit or risk reading stale cache.  So to
311477745c05SJosef Bacik 		 * limit our exposure to horrible edge cases lets just abort the
311577745c05SJosef Bacik 		 * transaction, this only happens in really bad situations
311677745c05SJosef Bacik 		 * anyway.
311777745c05SJosef Bacik 		 */
311877745c05SJosef Bacik 		btrfs_abort_transaction(trans, ret);
311977745c05SJosef Bacik 		goto out_put;
312077745c05SJosef Bacik 	}
312177745c05SJosef Bacik 	WARN_ON(ret);
312277745c05SJosef Bacik 
312377745c05SJosef Bacik 	/* We've already setup this transaction, go ahead and exit */
312477745c05SJosef Bacik 	if (block_group->cache_generation == trans->transid &&
312577745c05SJosef Bacik 	    i_size_read(inode)) {
312677745c05SJosef Bacik 		dcs = BTRFS_DC_SETUP;
312777745c05SJosef Bacik 		goto out_put;
312877745c05SJosef Bacik 	}
312977745c05SJosef Bacik 
313077745c05SJosef Bacik 	if (i_size_read(inode) > 0) {
313177745c05SJosef Bacik 		ret = btrfs_check_trunc_cache_free_space(fs_info,
313277745c05SJosef Bacik 					&fs_info->global_block_rsv);
313377745c05SJosef Bacik 		if (ret)
313477745c05SJosef Bacik 			goto out_put;
313577745c05SJosef Bacik 
313677745c05SJosef Bacik 		ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
313777745c05SJosef Bacik 		if (ret)
313877745c05SJosef Bacik 			goto out_put;
313977745c05SJosef Bacik 	}
314077745c05SJosef Bacik 
314177745c05SJosef Bacik 	spin_lock(&block_group->lock);
314277745c05SJosef Bacik 	if (block_group->cached != BTRFS_CACHE_FINISHED ||
314377745c05SJosef Bacik 	    !btrfs_test_opt(fs_info, SPACE_CACHE)) {
314477745c05SJosef Bacik 		/*
314577745c05SJosef Bacik 		 * don't bother trying to write stuff out _if_
314677745c05SJosef Bacik 		 * a) we're not cached,
314777745c05SJosef Bacik 		 * b) we're with nospace_cache mount option,
314877745c05SJosef Bacik 		 * c) we're with v2 space_cache (FREE_SPACE_TREE).
314977745c05SJosef Bacik 		 */
315077745c05SJosef Bacik 		dcs = BTRFS_DC_WRITTEN;
315177745c05SJosef Bacik 		spin_unlock(&block_group->lock);
315277745c05SJosef Bacik 		goto out_put;
315377745c05SJosef Bacik 	}
315477745c05SJosef Bacik 	spin_unlock(&block_group->lock);
315577745c05SJosef Bacik 
315677745c05SJosef Bacik 	/*
315777745c05SJosef Bacik 	 * We hit an ENOSPC when setting up the cache in this transaction, just
315877745c05SJosef Bacik 	 * skip doing the setup, we've already cleared the cache so we're safe.
315977745c05SJosef Bacik 	 */
316077745c05SJosef Bacik 	if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
316177745c05SJosef Bacik 		ret = -ENOSPC;
316277745c05SJosef Bacik 		goto out_put;
316377745c05SJosef Bacik 	}
316477745c05SJosef Bacik 
316577745c05SJosef Bacik 	/*
316677745c05SJosef Bacik 	 * Try to preallocate enough space based on how big the block group is.
316777745c05SJosef Bacik 	 * Keep in mind this has to include any pinned space which could end up
316877745c05SJosef Bacik 	 * taking up quite a bit since it's not folded into the other space
316977745c05SJosef Bacik 	 * cache.
317077745c05SJosef Bacik 	 */
31710044ae11SQu Wenruo 	cache_size = div_u64(block_group->length, SZ_256M);
31720044ae11SQu Wenruo 	if (!cache_size)
31730044ae11SQu Wenruo 		cache_size = 1;
317477745c05SJosef Bacik 
31750044ae11SQu Wenruo 	cache_size *= 16;
31760044ae11SQu Wenruo 	cache_size *= fs_info->sectorsize;
317777745c05SJosef Bacik 
317836ea6f3eSNikolay Borisov 	ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, 0,
31791daedb1dSJosef Bacik 					  cache_size, false);
318077745c05SJosef Bacik 	if (ret)
318177745c05SJosef Bacik 		goto out_put;
318277745c05SJosef Bacik 
31830044ae11SQu Wenruo 	ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, cache_size,
31840044ae11SQu Wenruo 					      cache_size, cache_size,
318577745c05SJosef Bacik 					      &alloc_hint);
318677745c05SJosef Bacik 	/*
318777745c05SJosef Bacik 	 * Our cache requires contiguous chunks so that we don't modify a bunch
318877745c05SJosef Bacik 	 * of metadata or split extents when writing the cache out, which means
318977745c05SJosef Bacik 	 * we can enospc if we are heavily fragmented in addition to just normal
319077745c05SJosef Bacik 	 * out of space conditions.  So if we hit this just skip setting up any
319177745c05SJosef Bacik 	 * other block groups for this transaction, maybe we'll unpin enough
319277745c05SJosef Bacik 	 * space the next time around.
319377745c05SJosef Bacik 	 */
319477745c05SJosef Bacik 	if (!ret)
319577745c05SJosef Bacik 		dcs = BTRFS_DC_SETUP;
319677745c05SJosef Bacik 	else if (ret == -ENOSPC)
319777745c05SJosef Bacik 		set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
319877745c05SJosef Bacik 
319977745c05SJosef Bacik out_put:
320077745c05SJosef Bacik 	iput(inode);
320177745c05SJosef Bacik out_free:
320277745c05SJosef Bacik 	btrfs_release_path(path);
320377745c05SJosef Bacik out:
320477745c05SJosef Bacik 	spin_lock(&block_group->lock);
320577745c05SJosef Bacik 	if (!ret && dcs == BTRFS_DC_SETUP)
320677745c05SJosef Bacik 		block_group->cache_generation = trans->transid;
320777745c05SJosef Bacik 	block_group->disk_cache_state = dcs;
320877745c05SJosef Bacik 	spin_unlock(&block_group->lock);
320977745c05SJosef Bacik 
321077745c05SJosef Bacik 	extent_changeset_free(data_reserved);
321177745c05SJosef Bacik 	return ret;
321277745c05SJosef Bacik }
321377745c05SJosef Bacik 
321477745c05SJosef Bacik int btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
321577745c05SJosef Bacik {
321677745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
321732da5386SDavid Sterba 	struct btrfs_block_group *cache, *tmp;
321877745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
321977745c05SJosef Bacik 	struct btrfs_path *path;
322077745c05SJosef Bacik 
322177745c05SJosef Bacik 	if (list_empty(&cur_trans->dirty_bgs) ||
322277745c05SJosef Bacik 	    !btrfs_test_opt(fs_info, SPACE_CACHE))
322377745c05SJosef Bacik 		return 0;
322477745c05SJosef Bacik 
322577745c05SJosef Bacik 	path = btrfs_alloc_path();
322677745c05SJosef Bacik 	if (!path)
322777745c05SJosef Bacik 		return -ENOMEM;
322877745c05SJosef Bacik 
322977745c05SJosef Bacik 	/* Could add new block groups, use _safe just in case */
323077745c05SJosef Bacik 	list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
323177745c05SJosef Bacik 				 dirty_list) {
323277745c05SJosef Bacik 		if (cache->disk_cache_state == BTRFS_DC_CLEAR)
323377745c05SJosef Bacik 			cache_save_setup(cache, trans, path);
323477745c05SJosef Bacik 	}
323577745c05SJosef Bacik 
323677745c05SJosef Bacik 	btrfs_free_path(path);
323777745c05SJosef Bacik 	return 0;
323877745c05SJosef Bacik }
323977745c05SJosef Bacik 
324077745c05SJosef Bacik /*
324177745c05SJosef Bacik  * Transaction commit does final block group cache writeback during a critical
324277745c05SJosef Bacik  * section where nothing is allowed to change the FS.  This is required in
324377745c05SJosef Bacik  * order for the cache to actually match the block group, but can introduce a
324477745c05SJosef Bacik  * lot of latency into the commit.
324577745c05SJosef Bacik  *
324677745c05SJosef Bacik  * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
324777745c05SJosef Bacik  * There's a chance we'll have to redo some of it if the block group changes
324877745c05SJosef Bacik  * again during the commit, but it greatly reduces the commit latency by
324977745c05SJosef Bacik  * getting rid of the easy block groups while we're still allowing others to
325077745c05SJosef Bacik  * join the commit.
325177745c05SJosef Bacik  */
325277745c05SJosef Bacik int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
325377745c05SJosef Bacik {
325477745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
325532da5386SDavid Sterba 	struct btrfs_block_group *cache;
325677745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
325777745c05SJosef Bacik 	int ret = 0;
325877745c05SJosef Bacik 	int should_put;
325977745c05SJosef Bacik 	struct btrfs_path *path = NULL;
326077745c05SJosef Bacik 	LIST_HEAD(dirty);
326177745c05SJosef Bacik 	struct list_head *io = &cur_trans->io_bgs;
326277745c05SJosef Bacik 	int loops = 0;
326377745c05SJosef Bacik 
326477745c05SJosef Bacik 	spin_lock(&cur_trans->dirty_bgs_lock);
326577745c05SJosef Bacik 	if (list_empty(&cur_trans->dirty_bgs)) {
326677745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
326777745c05SJosef Bacik 		return 0;
326877745c05SJosef Bacik 	}
326977745c05SJosef Bacik 	list_splice_init(&cur_trans->dirty_bgs, &dirty);
327077745c05SJosef Bacik 	spin_unlock(&cur_trans->dirty_bgs_lock);
327177745c05SJosef Bacik 
327277745c05SJosef Bacik again:
327377745c05SJosef Bacik 	/* Make sure all the block groups on our dirty list actually exist */
327477745c05SJosef Bacik 	btrfs_create_pending_block_groups(trans);
327577745c05SJosef Bacik 
327677745c05SJosef Bacik 	if (!path) {
327777745c05SJosef Bacik 		path = btrfs_alloc_path();
3278938fcbfbSJosef Bacik 		if (!path) {
3279938fcbfbSJosef Bacik 			ret = -ENOMEM;
3280938fcbfbSJosef Bacik 			goto out;
3281938fcbfbSJosef Bacik 		}
328277745c05SJosef Bacik 	}
328377745c05SJosef Bacik 
328477745c05SJosef Bacik 	/*
328577745c05SJosef Bacik 	 * cache_write_mutex is here only to save us from balance or automatic
328677745c05SJosef Bacik 	 * removal of empty block groups deleting this block group while we are
328777745c05SJosef Bacik 	 * writing out the cache
328877745c05SJosef Bacik 	 */
328977745c05SJosef Bacik 	mutex_lock(&trans->transaction->cache_write_mutex);
329077745c05SJosef Bacik 	while (!list_empty(&dirty)) {
329177745c05SJosef Bacik 		bool drop_reserve = true;
329277745c05SJosef Bacik 
329332da5386SDavid Sterba 		cache = list_first_entry(&dirty, struct btrfs_block_group,
329477745c05SJosef Bacik 					 dirty_list);
329577745c05SJosef Bacik 		/*
329677745c05SJosef Bacik 		 * This can happen if something re-dirties a block group that
329777745c05SJosef Bacik 		 * is already under IO.  Just wait for it to finish and then do
329877745c05SJosef Bacik 		 * it all again
329977745c05SJosef Bacik 		 */
330077745c05SJosef Bacik 		if (!list_empty(&cache->io_list)) {
330177745c05SJosef Bacik 			list_del_init(&cache->io_list);
330277745c05SJosef Bacik 			btrfs_wait_cache_io(trans, cache, path);
330377745c05SJosef Bacik 			btrfs_put_block_group(cache);
330477745c05SJosef Bacik 		}
330577745c05SJosef Bacik 
330677745c05SJosef Bacik 
330777745c05SJosef Bacik 		/*
330877745c05SJosef Bacik 		 * btrfs_wait_cache_io uses the cache->dirty_list to decide if
330977745c05SJosef Bacik 		 * it should update the cache_state.  Don't delete until after
331077745c05SJosef Bacik 		 * we wait.
331177745c05SJosef Bacik 		 *
331277745c05SJosef Bacik 		 * Since we're not running in the commit critical section
331377745c05SJosef Bacik 		 * we need the dirty_bgs_lock to protect from update_block_group
331477745c05SJosef Bacik 		 */
331577745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
331677745c05SJosef Bacik 		list_del_init(&cache->dirty_list);
331777745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
331877745c05SJosef Bacik 
331977745c05SJosef Bacik 		should_put = 1;
332077745c05SJosef Bacik 
332177745c05SJosef Bacik 		cache_save_setup(cache, trans, path);
332277745c05SJosef Bacik 
332377745c05SJosef Bacik 		if (cache->disk_cache_state == BTRFS_DC_SETUP) {
332477745c05SJosef Bacik 			cache->io_ctl.inode = NULL;
332577745c05SJosef Bacik 			ret = btrfs_write_out_cache(trans, cache, path);
332677745c05SJosef Bacik 			if (ret == 0 && cache->io_ctl.inode) {
332777745c05SJosef Bacik 				should_put = 0;
332877745c05SJosef Bacik 
332977745c05SJosef Bacik 				/*
333077745c05SJosef Bacik 				 * The cache_write_mutex is protecting the
333177745c05SJosef Bacik 				 * io_list, also refer to the definition of
333277745c05SJosef Bacik 				 * btrfs_transaction::io_bgs for more details
333377745c05SJosef Bacik 				 */
333477745c05SJosef Bacik 				list_add_tail(&cache->io_list, io);
333577745c05SJosef Bacik 			} else {
333677745c05SJosef Bacik 				/*
333777745c05SJosef Bacik 				 * If we failed to write the cache, the
333877745c05SJosef Bacik 				 * generation will be bad and life goes on
333977745c05SJosef Bacik 				 */
334077745c05SJosef Bacik 				ret = 0;
334177745c05SJosef Bacik 			}
334277745c05SJosef Bacik 		}
334377745c05SJosef Bacik 		if (!ret) {
33443be4d8efSQu Wenruo 			ret = update_block_group_item(trans, path, cache);
334577745c05SJosef Bacik 			/*
334677745c05SJosef Bacik 			 * Our block group might still be attached to the list
334777745c05SJosef Bacik 			 * of new block groups in the transaction handle of some
334877745c05SJosef Bacik 			 * other task (struct btrfs_trans_handle->new_bgs). This
334977745c05SJosef Bacik 			 * means its block group item isn't yet in the extent
335077745c05SJosef Bacik 			 * tree. If this happens ignore the error, as we will
335177745c05SJosef Bacik 			 * try again later in the critical section of the
335277745c05SJosef Bacik 			 * transaction commit.
335377745c05SJosef Bacik 			 */
335477745c05SJosef Bacik 			if (ret == -ENOENT) {
335577745c05SJosef Bacik 				ret = 0;
335677745c05SJosef Bacik 				spin_lock(&cur_trans->dirty_bgs_lock);
335777745c05SJosef Bacik 				if (list_empty(&cache->dirty_list)) {
335877745c05SJosef Bacik 					list_add_tail(&cache->dirty_list,
335977745c05SJosef Bacik 						      &cur_trans->dirty_bgs);
336077745c05SJosef Bacik 					btrfs_get_block_group(cache);
336177745c05SJosef Bacik 					drop_reserve = false;
336277745c05SJosef Bacik 				}
336377745c05SJosef Bacik 				spin_unlock(&cur_trans->dirty_bgs_lock);
336477745c05SJosef Bacik 			} else if (ret) {
336577745c05SJosef Bacik 				btrfs_abort_transaction(trans, ret);
336677745c05SJosef Bacik 			}
336777745c05SJosef Bacik 		}
336877745c05SJosef Bacik 
336977745c05SJosef Bacik 		/* If it's not on the io list, we need to put the block group */
337077745c05SJosef Bacik 		if (should_put)
337177745c05SJosef Bacik 			btrfs_put_block_group(cache);
337277745c05SJosef Bacik 		if (drop_reserve)
3373adb86dbeSFilipe Manana 			btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
337477745c05SJosef Bacik 		/*
337577745c05SJosef Bacik 		 * Avoid blocking other tasks for too long. It might even save
337677745c05SJosef Bacik 		 * us from writing caches for block groups that are going to be
337777745c05SJosef Bacik 		 * removed.
337877745c05SJosef Bacik 		 */
337977745c05SJosef Bacik 		mutex_unlock(&trans->transaction->cache_write_mutex);
3380938fcbfbSJosef Bacik 		if (ret)
3381938fcbfbSJosef Bacik 			goto out;
338277745c05SJosef Bacik 		mutex_lock(&trans->transaction->cache_write_mutex);
338377745c05SJosef Bacik 	}
338477745c05SJosef Bacik 	mutex_unlock(&trans->transaction->cache_write_mutex);
338577745c05SJosef Bacik 
338677745c05SJosef Bacik 	/*
338777745c05SJosef Bacik 	 * Go through delayed refs for all the stuff we've just kicked off
338877745c05SJosef Bacik 	 * and then loop back (just once)
338977745c05SJosef Bacik 	 */
339034d1eb0eSJosef Bacik 	if (!ret)
339177745c05SJosef Bacik 		ret = btrfs_run_delayed_refs(trans, 0);
339277745c05SJosef Bacik 	if (!ret && loops == 0) {
339377745c05SJosef Bacik 		loops++;
339477745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
339577745c05SJosef Bacik 		list_splice_init(&cur_trans->dirty_bgs, &dirty);
339677745c05SJosef Bacik 		/*
339777745c05SJosef Bacik 		 * dirty_bgs_lock protects us from concurrent block group
339877745c05SJosef Bacik 		 * deletes too (not just cache_write_mutex).
339977745c05SJosef Bacik 		 */
340077745c05SJosef Bacik 		if (!list_empty(&dirty)) {
340177745c05SJosef Bacik 			spin_unlock(&cur_trans->dirty_bgs_lock);
340277745c05SJosef Bacik 			goto again;
340377745c05SJosef Bacik 		}
340477745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
3405938fcbfbSJosef Bacik 	}
3406938fcbfbSJosef Bacik out:
3407938fcbfbSJosef Bacik 	if (ret < 0) {
3408938fcbfbSJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
3409938fcbfbSJosef Bacik 		list_splice_init(&dirty, &cur_trans->dirty_bgs);
3410938fcbfbSJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
341177745c05SJosef Bacik 		btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
341277745c05SJosef Bacik 	}
341377745c05SJosef Bacik 
341477745c05SJosef Bacik 	btrfs_free_path(path);
341577745c05SJosef Bacik 	return ret;
341677745c05SJosef Bacik }
341777745c05SJosef Bacik 
341877745c05SJosef Bacik int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
341977745c05SJosef Bacik {
342077745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
342132da5386SDavid Sterba 	struct btrfs_block_group *cache;
342277745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
342377745c05SJosef Bacik 	int ret = 0;
342477745c05SJosef Bacik 	int should_put;
342577745c05SJosef Bacik 	struct btrfs_path *path;
342677745c05SJosef Bacik 	struct list_head *io = &cur_trans->io_bgs;
342777745c05SJosef Bacik 
342877745c05SJosef Bacik 	path = btrfs_alloc_path();
342977745c05SJosef Bacik 	if (!path)
343077745c05SJosef Bacik 		return -ENOMEM;
343177745c05SJosef Bacik 
343277745c05SJosef Bacik 	/*
343377745c05SJosef Bacik 	 * Even though we are in the critical section of the transaction commit,
343477745c05SJosef Bacik 	 * we can still have concurrent tasks adding elements to this
343577745c05SJosef Bacik 	 * transaction's list of dirty block groups. These tasks correspond to
343677745c05SJosef Bacik 	 * endio free space workers started when writeback finishes for a
343777745c05SJosef Bacik 	 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
343877745c05SJosef Bacik 	 * allocate new block groups as a result of COWing nodes of the root
343977745c05SJosef Bacik 	 * tree when updating the free space inode. The writeback for the space
344077745c05SJosef Bacik 	 * caches is triggered by an earlier call to
344177745c05SJosef Bacik 	 * btrfs_start_dirty_block_groups() and iterations of the following
344277745c05SJosef Bacik 	 * loop.
344377745c05SJosef Bacik 	 * Also we want to do the cache_save_setup first and then run the
344477745c05SJosef Bacik 	 * delayed refs to make sure we have the best chance at doing this all
344577745c05SJosef Bacik 	 * in one shot.
344677745c05SJosef Bacik 	 */
344777745c05SJosef Bacik 	spin_lock(&cur_trans->dirty_bgs_lock);
344877745c05SJosef Bacik 	while (!list_empty(&cur_trans->dirty_bgs)) {
344977745c05SJosef Bacik 		cache = list_first_entry(&cur_trans->dirty_bgs,
345032da5386SDavid Sterba 					 struct btrfs_block_group,
345177745c05SJosef Bacik 					 dirty_list);
345277745c05SJosef Bacik 
345377745c05SJosef Bacik 		/*
345477745c05SJosef Bacik 		 * This can happen if cache_save_setup re-dirties a block group
345577745c05SJosef Bacik 		 * that is already under IO.  Just wait for it to finish and
345677745c05SJosef Bacik 		 * then do it all again
345777745c05SJosef Bacik 		 */
345877745c05SJosef Bacik 		if (!list_empty(&cache->io_list)) {
345977745c05SJosef Bacik 			spin_unlock(&cur_trans->dirty_bgs_lock);
346077745c05SJosef Bacik 			list_del_init(&cache->io_list);
346177745c05SJosef Bacik 			btrfs_wait_cache_io(trans, cache, path);
346277745c05SJosef Bacik 			btrfs_put_block_group(cache);
346377745c05SJosef Bacik 			spin_lock(&cur_trans->dirty_bgs_lock);
346477745c05SJosef Bacik 		}
346577745c05SJosef Bacik 
346677745c05SJosef Bacik 		/*
346777745c05SJosef Bacik 		 * Don't remove from the dirty list until after we've waited on
346877745c05SJosef Bacik 		 * any pending IO
346977745c05SJosef Bacik 		 */
347077745c05SJosef Bacik 		list_del_init(&cache->dirty_list);
347177745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
347277745c05SJosef Bacik 		should_put = 1;
347377745c05SJosef Bacik 
347477745c05SJosef Bacik 		cache_save_setup(cache, trans, path);
347577745c05SJosef Bacik 
347677745c05SJosef Bacik 		if (!ret)
34778a526c44SFilipe Manana 			ret = btrfs_run_delayed_refs(trans, U64_MAX);
347877745c05SJosef Bacik 
347977745c05SJosef Bacik 		if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
348077745c05SJosef Bacik 			cache->io_ctl.inode = NULL;
348177745c05SJosef Bacik 			ret = btrfs_write_out_cache(trans, cache, path);
348277745c05SJosef Bacik 			if (ret == 0 && cache->io_ctl.inode) {
348377745c05SJosef Bacik 				should_put = 0;
348477745c05SJosef Bacik 				list_add_tail(&cache->io_list, io);
348577745c05SJosef Bacik 			} else {
348677745c05SJosef Bacik 				/*
348777745c05SJosef Bacik 				 * If we failed to write the cache, the
348877745c05SJosef Bacik 				 * generation will be bad and life goes on
348977745c05SJosef Bacik 				 */
349077745c05SJosef Bacik 				ret = 0;
349177745c05SJosef Bacik 			}
349277745c05SJosef Bacik 		}
349377745c05SJosef Bacik 		if (!ret) {
34943be4d8efSQu Wenruo 			ret = update_block_group_item(trans, path, cache);
349577745c05SJosef Bacik 			/*
349677745c05SJosef Bacik 			 * One of the free space endio workers might have
349777745c05SJosef Bacik 			 * created a new block group while updating a free space
349877745c05SJosef Bacik 			 * cache's inode (at inode.c:btrfs_finish_ordered_io())
349977745c05SJosef Bacik 			 * and hasn't released its transaction handle yet, in
350077745c05SJosef Bacik 			 * which case the new block group is still attached to
350177745c05SJosef Bacik 			 * its transaction handle and its creation has not
350277745c05SJosef Bacik 			 * finished yet (no block group item in the extent tree
350377745c05SJosef Bacik 			 * yet, etc). If this is the case, wait for all free
350477745c05SJosef Bacik 			 * space endio workers to finish and retry. This is a
3505260db43cSRandy Dunlap 			 * very rare case so no need for a more efficient and
350677745c05SJosef Bacik 			 * complex approach.
350777745c05SJosef Bacik 			 */
350877745c05SJosef Bacik 			if (ret == -ENOENT) {
350977745c05SJosef Bacik 				wait_event(cur_trans->writer_wait,
351077745c05SJosef Bacik 				   atomic_read(&cur_trans->num_writers) == 1);
35113be4d8efSQu Wenruo 				ret = update_block_group_item(trans, path, cache);
351277745c05SJosef Bacik 			}
351377745c05SJosef Bacik 			if (ret)
351477745c05SJosef Bacik 				btrfs_abort_transaction(trans, ret);
351577745c05SJosef Bacik 		}
351677745c05SJosef Bacik 
351777745c05SJosef Bacik 		/* If its not on the io list, we need to put the block group */
351877745c05SJosef Bacik 		if (should_put)
351977745c05SJosef Bacik 			btrfs_put_block_group(cache);
3520adb86dbeSFilipe Manana 		btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
352177745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
352277745c05SJosef Bacik 	}
352377745c05SJosef Bacik 	spin_unlock(&cur_trans->dirty_bgs_lock);
352477745c05SJosef Bacik 
352577745c05SJosef Bacik 	/*
352677745c05SJosef Bacik 	 * Refer to the definition of io_bgs member for details why it's safe
352777745c05SJosef Bacik 	 * to use it without any locking
352877745c05SJosef Bacik 	 */
352977745c05SJosef Bacik 	while (!list_empty(io)) {
353032da5386SDavid Sterba 		cache = list_first_entry(io, struct btrfs_block_group,
353177745c05SJosef Bacik 					 io_list);
353277745c05SJosef Bacik 		list_del_init(&cache->io_list);
353377745c05SJosef Bacik 		btrfs_wait_cache_io(trans, cache, path);
353477745c05SJosef Bacik 		btrfs_put_block_group(cache);
353577745c05SJosef Bacik 	}
353677745c05SJosef Bacik 
353777745c05SJosef Bacik 	btrfs_free_path(path);
353877745c05SJosef Bacik 	return ret;
353977745c05SJosef Bacik }
3540606d1bf1SJosef Bacik 
3541606d1bf1SJosef Bacik int btrfs_update_block_group(struct btrfs_trans_handle *trans,
354211b66fa6SAnand Jain 			     u64 bytenr, u64 num_bytes, bool alloc)
3543606d1bf1SJosef Bacik {
3544606d1bf1SJosef Bacik 	struct btrfs_fs_info *info = trans->fs_info;
354532da5386SDavid Sterba 	struct btrfs_block_group *cache = NULL;
3546606d1bf1SJosef Bacik 	u64 total = num_bytes;
3547606d1bf1SJosef Bacik 	u64 old_val;
3548606d1bf1SJosef Bacik 	u64 byte_in_group;
3549606d1bf1SJosef Bacik 	int factor;
3550606d1bf1SJosef Bacik 	int ret = 0;
3551606d1bf1SJosef Bacik 
3552606d1bf1SJosef Bacik 	/* Block accounting for super block */
3553606d1bf1SJosef Bacik 	spin_lock(&info->delalloc_root_lock);
3554606d1bf1SJosef Bacik 	old_val = btrfs_super_bytes_used(info->super_copy);
3555606d1bf1SJosef Bacik 	if (alloc)
3556606d1bf1SJosef Bacik 		old_val += num_bytes;
3557606d1bf1SJosef Bacik 	else
3558606d1bf1SJosef Bacik 		old_val -= num_bytes;
3559606d1bf1SJosef Bacik 	btrfs_set_super_bytes_used(info->super_copy, old_val);
3560606d1bf1SJosef Bacik 	spin_unlock(&info->delalloc_root_lock);
3561606d1bf1SJosef Bacik 
3562606d1bf1SJosef Bacik 	while (total) {
3563df384da5SJosef Bacik 		struct btrfs_space_info *space_info;
3564efbf35a1SJosef Bacik 		bool reclaim = false;
3565ac2f1e63SJosef Bacik 
3566606d1bf1SJosef Bacik 		cache = btrfs_lookup_block_group(info, bytenr);
3567606d1bf1SJosef Bacik 		if (!cache) {
3568606d1bf1SJosef Bacik 			ret = -ENOENT;
3569606d1bf1SJosef Bacik 			break;
3570606d1bf1SJosef Bacik 		}
3571df384da5SJosef Bacik 		space_info = cache->space_info;
3572606d1bf1SJosef Bacik 		factor = btrfs_bg_type_to_factor(cache->flags);
3573606d1bf1SJosef Bacik 
3574606d1bf1SJosef Bacik 		/*
3575606d1bf1SJosef Bacik 		 * If this block group has free space cache written out, we
3576606d1bf1SJosef Bacik 		 * need to make sure to load it if we are removing space.  This
3577606d1bf1SJosef Bacik 		 * is because we need the unpinning stage to actually add the
3578606d1bf1SJosef Bacik 		 * space back to the block group, otherwise we will leak space.
3579606d1bf1SJosef Bacik 		 */
358032da5386SDavid Sterba 		if (!alloc && !btrfs_block_group_done(cache))
3581ced8ecf0SOmar Sandoval 			btrfs_cache_block_group(cache, true);
3582606d1bf1SJosef Bacik 
3583b3470b5dSDavid Sterba 		byte_in_group = bytenr - cache->start;
3584b3470b5dSDavid Sterba 		WARN_ON(byte_in_group > cache->length);
3585606d1bf1SJosef Bacik 
3586df384da5SJosef Bacik 		spin_lock(&space_info->lock);
3587606d1bf1SJosef Bacik 		spin_lock(&cache->lock);
3588606d1bf1SJosef Bacik 
3589606d1bf1SJosef Bacik 		if (btrfs_test_opt(info, SPACE_CACHE) &&
3590606d1bf1SJosef Bacik 		    cache->disk_cache_state < BTRFS_DC_CLEAR)
3591606d1bf1SJosef Bacik 			cache->disk_cache_state = BTRFS_DC_CLEAR;
3592606d1bf1SJosef Bacik 
3593bf38be65SDavid Sterba 		old_val = cache->used;
3594b3470b5dSDavid Sterba 		num_bytes = min(total, cache->length - byte_in_group);
3595606d1bf1SJosef Bacik 		if (alloc) {
3596606d1bf1SJosef Bacik 			old_val += num_bytes;
3597bf38be65SDavid Sterba 			cache->used = old_val;
3598606d1bf1SJosef Bacik 			cache->reserved -= num_bytes;
3599df384da5SJosef Bacik 			space_info->bytes_reserved -= num_bytes;
3600df384da5SJosef Bacik 			space_info->bytes_used += num_bytes;
3601df384da5SJosef Bacik 			space_info->disk_used += num_bytes * factor;
3602606d1bf1SJosef Bacik 			spin_unlock(&cache->lock);
3603df384da5SJosef Bacik 			spin_unlock(&space_info->lock);
3604606d1bf1SJosef Bacik 		} else {
3605606d1bf1SJosef Bacik 			old_val -= num_bytes;
3606bf38be65SDavid Sterba 			cache->used = old_val;
3607606d1bf1SJosef Bacik 			cache->pinned += num_bytes;
3608df384da5SJosef Bacik 			btrfs_space_info_update_bytes_pinned(info, space_info,
3609df384da5SJosef Bacik 							     num_bytes);
3610df384da5SJosef Bacik 			space_info->bytes_used -= num_bytes;
3611df384da5SJosef Bacik 			space_info->disk_used -= num_bytes * factor;
3612ac2f1e63SJosef Bacik 
3613ac2f1e63SJosef Bacik 			reclaim = should_reclaim_block_group(cache, num_bytes);
361452bb7a21SBoris Burkov 
3615606d1bf1SJosef Bacik 			spin_unlock(&cache->lock);
3616df384da5SJosef Bacik 			spin_unlock(&space_info->lock);
3617606d1bf1SJosef Bacik 
3618fe1a598cSDavid Sterba 			set_extent_bit(&trans->transaction->pinned_extents,
3619606d1bf1SJosef Bacik 				       bytenr, bytenr + num_bytes - 1,
36201d126800SDavid Sterba 				       EXTENT_DIRTY, NULL);
3621606d1bf1SJosef Bacik 		}
3622606d1bf1SJosef Bacik 
3623606d1bf1SJosef Bacik 		spin_lock(&trans->transaction->dirty_bgs_lock);
3624606d1bf1SJosef Bacik 		if (list_empty(&cache->dirty_list)) {
3625606d1bf1SJosef Bacik 			list_add_tail(&cache->dirty_list,
3626606d1bf1SJosef Bacik 				      &trans->transaction->dirty_bgs);
3627606d1bf1SJosef Bacik 			trans->delayed_ref_updates++;
3628606d1bf1SJosef Bacik 			btrfs_get_block_group(cache);
3629606d1bf1SJosef Bacik 		}
3630606d1bf1SJosef Bacik 		spin_unlock(&trans->transaction->dirty_bgs_lock);
3631606d1bf1SJosef Bacik 
3632606d1bf1SJosef Bacik 		/*
3633606d1bf1SJosef Bacik 		 * No longer have used bytes in this block group, queue it for
3634606d1bf1SJosef Bacik 		 * deletion. We do this after adding the block group to the
3635606d1bf1SJosef Bacik 		 * dirty list to avoid races between cleaner kthread and space
3636606d1bf1SJosef Bacik 		 * cache writeout.
3637606d1bf1SJosef Bacik 		 */
36386e80d4f8SDennis Zhou 		if (!alloc && old_val == 0) {
36396e80d4f8SDennis Zhou 			if (!btrfs_test_opt(info, DISCARD_ASYNC))
3640606d1bf1SJosef Bacik 				btrfs_mark_bg_unused(cache);
3641ac2f1e63SJosef Bacik 		} else if (!alloc && reclaim) {
3642ac2f1e63SJosef Bacik 			btrfs_mark_bg_to_reclaim(cache);
36436e80d4f8SDennis Zhou 		}
3644606d1bf1SJosef Bacik 
3645606d1bf1SJosef Bacik 		btrfs_put_block_group(cache);
3646606d1bf1SJosef Bacik 		total -= num_bytes;
3647606d1bf1SJosef Bacik 		bytenr += num_bytes;
3648606d1bf1SJosef Bacik 	}
3649606d1bf1SJosef Bacik 
3650606d1bf1SJosef Bacik 	/* Modified block groups are accounted for in the delayed_refs_rsv. */
3651606d1bf1SJosef Bacik 	btrfs_update_delayed_refs_rsv(trans);
3652606d1bf1SJosef Bacik 	return ret;
3653606d1bf1SJosef Bacik }
3654606d1bf1SJosef Bacik 
365543dd529aSDavid Sterba /*
365643dd529aSDavid Sterba  * Update the block_group and space info counters.
365743dd529aSDavid Sterba  *
3658606d1bf1SJosef Bacik  * @cache:	The cache we are manipulating
3659606d1bf1SJosef Bacik  * @ram_bytes:  The number of bytes of file content, and will be same to
3660606d1bf1SJosef Bacik  *              @num_bytes except for the compress path.
3661606d1bf1SJosef Bacik  * @num_bytes:	The number of bytes in question
3662606d1bf1SJosef Bacik  * @delalloc:   The blocks are allocated for the delalloc write
3663606d1bf1SJosef Bacik  *
3664606d1bf1SJosef Bacik  * This is called by the allocator when it reserves space. If this is a
3665606d1bf1SJosef Bacik  * reservation and the block group has become read only we cannot make the
3666606d1bf1SJosef Bacik  * reservation and return -EAGAIN, otherwise this function always succeeds.
3667606d1bf1SJosef Bacik  */
366832da5386SDavid Sterba int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
366952bb7a21SBoris Burkov 			     u64 ram_bytes, u64 num_bytes, int delalloc,
367052bb7a21SBoris Burkov 			     bool force_wrong_size_class)
3671606d1bf1SJosef Bacik {
3672606d1bf1SJosef Bacik 	struct btrfs_space_info *space_info = cache->space_info;
367352bb7a21SBoris Burkov 	enum btrfs_block_group_size_class size_class;
3674606d1bf1SJosef Bacik 	int ret = 0;
3675606d1bf1SJosef Bacik 
3676606d1bf1SJosef Bacik 	spin_lock(&space_info->lock);
3677606d1bf1SJosef Bacik 	spin_lock(&cache->lock);
3678606d1bf1SJosef Bacik 	if (cache->ro) {
3679606d1bf1SJosef Bacik 		ret = -EAGAIN;
368052bb7a21SBoris Burkov 		goto out;
368152bb7a21SBoris Burkov 	}
368252bb7a21SBoris Burkov 
3683cb0922f2SBoris Burkov 	if (btrfs_block_group_should_use_size_class(cache)) {
368452bb7a21SBoris Burkov 		size_class = btrfs_calc_block_group_size_class(num_bytes);
368552bb7a21SBoris Burkov 		ret = btrfs_use_block_group_size_class(cache, size_class, force_wrong_size_class);
368652bb7a21SBoris Burkov 		if (ret)
368752bb7a21SBoris Burkov 			goto out;
368852bb7a21SBoris Burkov 	}
3689606d1bf1SJosef Bacik 	cache->reserved += num_bytes;
3690606d1bf1SJosef Bacik 	space_info->bytes_reserved += num_bytes;
3691a43c3835SJosef Bacik 	trace_btrfs_space_reservation(cache->fs_info, "space_info",
3692a43c3835SJosef Bacik 				      space_info->flags, num_bytes, 1);
3693606d1bf1SJosef Bacik 	btrfs_space_info_update_bytes_may_use(cache->fs_info,
3694606d1bf1SJosef Bacik 					      space_info, -ram_bytes);
3695606d1bf1SJosef Bacik 	if (delalloc)
3696606d1bf1SJosef Bacik 		cache->delalloc_bytes += num_bytes;
369799ffb43eSJosef Bacik 
369899ffb43eSJosef Bacik 	/*
369952bb7a21SBoris Burkov 	 * Compression can use less space than we reserved, so wake tickets if
370052bb7a21SBoris Burkov 	 * that happens.
370199ffb43eSJosef Bacik 	 */
370299ffb43eSJosef Bacik 	if (num_bytes < ram_bytes)
370399ffb43eSJosef Bacik 		btrfs_try_granting_tickets(cache->fs_info, space_info);
370452bb7a21SBoris Burkov out:
3705606d1bf1SJosef Bacik 	spin_unlock(&cache->lock);
3706606d1bf1SJosef Bacik 	spin_unlock(&space_info->lock);
3707606d1bf1SJosef Bacik 	return ret;
3708606d1bf1SJosef Bacik }
3709606d1bf1SJosef Bacik 
371043dd529aSDavid Sterba /*
371143dd529aSDavid Sterba  * Update the block_group and space info counters.
371243dd529aSDavid Sterba  *
3713606d1bf1SJosef Bacik  * @cache:      The cache we are manipulating
3714606d1bf1SJosef Bacik  * @num_bytes:  The number of bytes in question
3715606d1bf1SJosef Bacik  * @delalloc:   The blocks are allocated for the delalloc write
3716606d1bf1SJosef Bacik  *
3717606d1bf1SJosef Bacik  * This is called by somebody who is freeing space that was never actually used
3718606d1bf1SJosef Bacik  * on disk.  For example if you reserve some space for a new leaf in transaction
3719606d1bf1SJosef Bacik  * A and before transaction A commits you free that leaf, you call this with
3720606d1bf1SJosef Bacik  * reserve set to 0 in order to clear the reservation.
3721606d1bf1SJosef Bacik  */
372232da5386SDavid Sterba void btrfs_free_reserved_bytes(struct btrfs_block_group *cache,
3723606d1bf1SJosef Bacik 			       u64 num_bytes, int delalloc)
3724606d1bf1SJosef Bacik {
3725606d1bf1SJosef Bacik 	struct btrfs_space_info *space_info = cache->space_info;
3726606d1bf1SJosef Bacik 
3727606d1bf1SJosef Bacik 	spin_lock(&space_info->lock);
3728606d1bf1SJosef Bacik 	spin_lock(&cache->lock);
3729606d1bf1SJosef Bacik 	if (cache->ro)
3730606d1bf1SJosef Bacik 		space_info->bytes_readonly += num_bytes;
3731606d1bf1SJosef Bacik 	cache->reserved -= num_bytes;
3732606d1bf1SJosef Bacik 	space_info->bytes_reserved -= num_bytes;
3733606d1bf1SJosef Bacik 	space_info->max_extent_size = 0;
3734606d1bf1SJosef Bacik 
3735606d1bf1SJosef Bacik 	if (delalloc)
3736606d1bf1SJosef Bacik 		cache->delalloc_bytes -= num_bytes;
3737606d1bf1SJosef Bacik 	spin_unlock(&cache->lock);
37383308234aSJosef Bacik 
37393308234aSJosef Bacik 	btrfs_try_granting_tickets(cache->fs_info, space_info);
3740606d1bf1SJosef Bacik 	spin_unlock(&space_info->lock);
3741606d1bf1SJosef Bacik }
374207730d87SJosef Bacik 
374307730d87SJosef Bacik static void force_metadata_allocation(struct btrfs_fs_info *info)
374407730d87SJosef Bacik {
374507730d87SJosef Bacik 	struct list_head *head = &info->space_info;
374607730d87SJosef Bacik 	struct btrfs_space_info *found;
374707730d87SJosef Bacik 
374872804905SJosef Bacik 	list_for_each_entry(found, head, list) {
374907730d87SJosef Bacik 		if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
375007730d87SJosef Bacik 			found->force_alloc = CHUNK_ALLOC_FORCE;
375107730d87SJosef Bacik 	}
375207730d87SJosef Bacik }
375307730d87SJosef Bacik 
375407730d87SJosef Bacik static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
375507730d87SJosef Bacik 			      struct btrfs_space_info *sinfo, int force)
375607730d87SJosef Bacik {
375707730d87SJosef Bacik 	u64 bytes_used = btrfs_space_info_used(sinfo, false);
375807730d87SJosef Bacik 	u64 thresh;
375907730d87SJosef Bacik 
376007730d87SJosef Bacik 	if (force == CHUNK_ALLOC_FORCE)
376107730d87SJosef Bacik 		return 1;
376207730d87SJosef Bacik 
376307730d87SJosef Bacik 	/*
376407730d87SJosef Bacik 	 * in limited mode, we want to have some free space up to
376507730d87SJosef Bacik 	 * about 1% of the FS size.
376607730d87SJosef Bacik 	 */
376707730d87SJosef Bacik 	if (force == CHUNK_ALLOC_LIMITED) {
376807730d87SJosef Bacik 		thresh = btrfs_super_total_bytes(fs_info->super_copy);
3769428c8e03SDavid Sterba 		thresh = max_t(u64, SZ_64M, mult_perc(thresh, 1));
377007730d87SJosef Bacik 
377107730d87SJosef Bacik 		if (sinfo->total_bytes - bytes_used < thresh)
377207730d87SJosef Bacik 			return 1;
377307730d87SJosef Bacik 	}
377407730d87SJosef Bacik 
3775428c8e03SDavid Sterba 	if (bytes_used + SZ_2M < mult_perc(sinfo->total_bytes, 80))
377607730d87SJosef Bacik 		return 0;
377707730d87SJosef Bacik 	return 1;
377807730d87SJosef Bacik }
377907730d87SJosef Bacik 
378007730d87SJosef Bacik int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
378107730d87SJosef Bacik {
378207730d87SJosef Bacik 	u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type);
378307730d87SJosef Bacik 
378407730d87SJosef Bacik 	return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
378507730d87SJosef Bacik }
378607730d87SJosef Bacik 
3787820c363bSNaohiro Aota static struct btrfs_block_group *do_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags)
378879bd3712SFilipe Manana {
378979bd3712SFilipe Manana 	struct btrfs_block_group *bg;
379079bd3712SFilipe Manana 	int ret;
379179bd3712SFilipe Manana 
379207730d87SJosef Bacik 	/*
379379bd3712SFilipe Manana 	 * Check if we have enough space in the system space info because we
379479bd3712SFilipe Manana 	 * will need to update device items in the chunk btree and insert a new
379579bd3712SFilipe Manana 	 * chunk item in the chunk btree as well. This will allocate a new
379679bd3712SFilipe Manana 	 * system block group if needed.
379779bd3712SFilipe Manana 	 */
379879bd3712SFilipe Manana 	check_system_chunk(trans, flags);
379979bd3712SFilipe Manana 
3800f6f39f7aSNikolay Borisov 	bg = btrfs_create_chunk(trans, flags);
380179bd3712SFilipe Manana 	if (IS_ERR(bg)) {
380279bd3712SFilipe Manana 		ret = PTR_ERR(bg);
380379bd3712SFilipe Manana 		goto out;
380479bd3712SFilipe Manana 	}
380579bd3712SFilipe Manana 
380679bd3712SFilipe Manana 	ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
380779bd3712SFilipe Manana 	/*
380879bd3712SFilipe Manana 	 * Normally we are not expected to fail with -ENOSPC here, since we have
380979bd3712SFilipe Manana 	 * previously reserved space in the system space_info and allocated one
3810ecd84d54SFilipe Manana 	 * new system chunk if necessary. However there are three exceptions:
381179bd3712SFilipe Manana 	 *
381279bd3712SFilipe Manana 	 * 1) We may have enough free space in the system space_info but all the
381379bd3712SFilipe Manana 	 *    existing system block groups have a profile which can not be used
381479bd3712SFilipe Manana 	 *    for extent allocation.
381579bd3712SFilipe Manana 	 *
381679bd3712SFilipe Manana 	 *    This happens when mounting in degraded mode. For example we have a
381779bd3712SFilipe Manana 	 *    RAID1 filesystem with 2 devices, lose one device and mount the fs
381879bd3712SFilipe Manana 	 *    using the other device in degraded mode. If we then allocate a chunk,
381979bd3712SFilipe Manana 	 *    we may have enough free space in the existing system space_info, but
382079bd3712SFilipe Manana 	 *    none of the block groups can be used for extent allocation since they
382179bd3712SFilipe Manana 	 *    have a RAID1 profile, and because we are in degraded mode with a
382279bd3712SFilipe Manana 	 *    single device, we are forced to allocate a new system chunk with a
382379bd3712SFilipe Manana 	 *    SINGLE profile. Making check_system_chunk() iterate over all system
382479bd3712SFilipe Manana 	 *    block groups and check if they have a usable profile and enough space
382579bd3712SFilipe Manana 	 *    can be slow on very large filesystems, so we tolerate the -ENOSPC and
382679bd3712SFilipe Manana 	 *    try again after forcing allocation of a new system chunk. Like this
382779bd3712SFilipe Manana 	 *    we avoid paying the cost of that search in normal circumstances, when
382879bd3712SFilipe Manana 	 *    we were not mounted in degraded mode;
382979bd3712SFilipe Manana 	 *
383079bd3712SFilipe Manana 	 * 2) We had enough free space info the system space_info, and one suitable
383179bd3712SFilipe Manana 	 *    block group to allocate from when we called check_system_chunk()
383279bd3712SFilipe Manana 	 *    above. However right after we called it, the only system block group
383379bd3712SFilipe Manana 	 *    with enough free space got turned into RO mode by a running scrub,
383479bd3712SFilipe Manana 	 *    and in this case we have to allocate a new one and retry. We only
383579bd3712SFilipe Manana 	 *    need do this allocate and retry once, since we have a transaction
3836ecd84d54SFilipe Manana 	 *    handle and scrub uses the commit root to search for block groups;
3837ecd84d54SFilipe Manana 	 *
3838ecd84d54SFilipe Manana 	 * 3) We had one system block group with enough free space when we called
3839ecd84d54SFilipe Manana 	 *    check_system_chunk(), but after that, right before we tried to
3840ecd84d54SFilipe Manana 	 *    allocate the last extent buffer we needed, a discard operation came
3841ecd84d54SFilipe Manana 	 *    in and it temporarily removed the last free space entry from the
3842ecd84d54SFilipe Manana 	 *    block group (discard removes a free space entry, discards it, and
3843ecd84d54SFilipe Manana 	 *    then adds back the entry to the block group cache).
384479bd3712SFilipe Manana 	 */
384579bd3712SFilipe Manana 	if (ret == -ENOSPC) {
384679bd3712SFilipe Manana 		const u64 sys_flags = btrfs_system_alloc_profile(trans->fs_info);
384779bd3712SFilipe Manana 		struct btrfs_block_group *sys_bg;
384879bd3712SFilipe Manana 
3849f6f39f7aSNikolay Borisov 		sys_bg = btrfs_create_chunk(trans, sys_flags);
385079bd3712SFilipe Manana 		if (IS_ERR(sys_bg)) {
385179bd3712SFilipe Manana 			ret = PTR_ERR(sys_bg);
385279bd3712SFilipe Manana 			btrfs_abort_transaction(trans, ret);
385379bd3712SFilipe Manana 			goto out;
385479bd3712SFilipe Manana 		}
385579bd3712SFilipe Manana 
385679bd3712SFilipe Manana 		ret = btrfs_chunk_alloc_add_chunk_item(trans, sys_bg);
385779bd3712SFilipe Manana 		if (ret) {
385879bd3712SFilipe Manana 			btrfs_abort_transaction(trans, ret);
385979bd3712SFilipe Manana 			goto out;
386079bd3712SFilipe Manana 		}
386179bd3712SFilipe Manana 
386279bd3712SFilipe Manana 		ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
386379bd3712SFilipe Manana 		if (ret) {
386479bd3712SFilipe Manana 			btrfs_abort_transaction(trans, ret);
386579bd3712SFilipe Manana 			goto out;
386679bd3712SFilipe Manana 		}
386779bd3712SFilipe Manana 	} else if (ret) {
386879bd3712SFilipe Manana 		btrfs_abort_transaction(trans, ret);
386979bd3712SFilipe Manana 		goto out;
387079bd3712SFilipe Manana 	}
387179bd3712SFilipe Manana out:
387279bd3712SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
387379bd3712SFilipe Manana 
3874820c363bSNaohiro Aota 	if (ret)
3875820c363bSNaohiro Aota 		return ERR_PTR(ret);
3876820c363bSNaohiro Aota 
3877820c363bSNaohiro Aota 	btrfs_get_block_group(bg);
3878820c363bSNaohiro Aota 	return bg;
387979bd3712SFilipe Manana }
388079bd3712SFilipe Manana 
388179bd3712SFilipe Manana /*
388279bd3712SFilipe Manana  * Chunk allocation is done in 2 phases:
388379bd3712SFilipe Manana  *
388479bd3712SFilipe Manana  * 1) Phase 1 - through btrfs_chunk_alloc() we allocate device extents for
388579bd3712SFilipe Manana  *    the chunk, the chunk mapping, create its block group and add the items
388679bd3712SFilipe Manana  *    that belong in the chunk btree to it - more specifically, we need to
388779bd3712SFilipe Manana  *    update device items in the chunk btree and add a new chunk item to it.
388879bd3712SFilipe Manana  *
388979bd3712SFilipe Manana  * 2) Phase 2 - through btrfs_create_pending_block_groups(), we add the block
389079bd3712SFilipe Manana  *    group item to the extent btree and the device extent items to the devices
389179bd3712SFilipe Manana  *    btree.
389279bd3712SFilipe Manana  *
389379bd3712SFilipe Manana  * This is done to prevent deadlocks. For example when COWing a node from the
389479bd3712SFilipe Manana  * extent btree we are holding a write lock on the node's parent and if we
389579bd3712SFilipe Manana  * trigger chunk allocation and attempted to insert the new block group item
389679bd3712SFilipe Manana  * in the extent btree right way, we could deadlock because the path for the
389779bd3712SFilipe Manana  * insertion can include that parent node. At first glance it seems impossible
389879bd3712SFilipe Manana  * to trigger chunk allocation after starting a transaction since tasks should
389979bd3712SFilipe Manana  * reserve enough transaction units (metadata space), however while that is true
390079bd3712SFilipe Manana  * most of the time, chunk allocation may still be triggered for several reasons:
390179bd3712SFilipe Manana  *
390279bd3712SFilipe Manana  * 1) When reserving metadata, we check if there is enough free space in the
390379bd3712SFilipe Manana  *    metadata space_info and therefore don't trigger allocation of a new chunk.
390479bd3712SFilipe Manana  *    However later when the task actually tries to COW an extent buffer from
390579bd3712SFilipe Manana  *    the extent btree or from the device btree for example, it is forced to
390679bd3712SFilipe Manana  *    allocate a new block group (chunk) because the only one that had enough
390779bd3712SFilipe Manana  *    free space was just turned to RO mode by a running scrub for example (or
390879bd3712SFilipe Manana  *    device replace, block group reclaim thread, etc), so we can not use it
390979bd3712SFilipe Manana  *    for allocating an extent and end up being forced to allocate a new one;
391079bd3712SFilipe Manana  *
391179bd3712SFilipe Manana  * 2) Because we only check that the metadata space_info has enough free bytes,
391279bd3712SFilipe Manana  *    we end up not allocating a new metadata chunk in that case. However if
391379bd3712SFilipe Manana  *    the filesystem was mounted in degraded mode, none of the existing block
391479bd3712SFilipe Manana  *    groups might be suitable for extent allocation due to their incompatible
391579bd3712SFilipe Manana  *    profile (for e.g. mounting a 2 devices filesystem, where all block groups
391679bd3712SFilipe Manana  *    use a RAID1 profile, in degraded mode using a single device). In this case
391779bd3712SFilipe Manana  *    when the task attempts to COW some extent buffer of the extent btree for
391879bd3712SFilipe Manana  *    example, it will trigger allocation of a new metadata block group with a
391979bd3712SFilipe Manana  *    suitable profile (SINGLE profile in the example of the degraded mount of
392079bd3712SFilipe Manana  *    the RAID1 filesystem);
392179bd3712SFilipe Manana  *
392279bd3712SFilipe Manana  * 3) The task has reserved enough transaction units / metadata space, but when
392379bd3712SFilipe Manana  *    it attempts to COW an extent buffer from the extent or device btree for
392479bd3712SFilipe Manana  *    example, it does not find any free extent in any metadata block group,
392579bd3712SFilipe Manana  *    therefore forced to try to allocate a new metadata block group.
392679bd3712SFilipe Manana  *    This is because some other task allocated all available extents in the
392779bd3712SFilipe Manana  *    meanwhile - this typically happens with tasks that don't reserve space
392879bd3712SFilipe Manana  *    properly, either intentionally or as a bug. One example where this is
392979bd3712SFilipe Manana  *    done intentionally is fsync, as it does not reserve any transaction units
393079bd3712SFilipe Manana  *    and ends up allocating a variable number of metadata extents for log
3931ecd84d54SFilipe Manana  *    tree extent buffers;
3932ecd84d54SFilipe Manana  *
3933ecd84d54SFilipe Manana  * 4) The task has reserved enough transaction units / metadata space, but right
3934ecd84d54SFilipe Manana  *    before it tries to allocate the last extent buffer it needs, a discard
3935ecd84d54SFilipe Manana  *    operation comes in and, temporarily, removes the last free space entry from
3936ecd84d54SFilipe Manana  *    the only metadata block group that had free space (discard starts by
3937ecd84d54SFilipe Manana  *    removing a free space entry from a block group, then does the discard
3938ecd84d54SFilipe Manana  *    operation and, once it's done, it adds back the free space entry to the
3939ecd84d54SFilipe Manana  *    block group).
394079bd3712SFilipe Manana  *
394179bd3712SFilipe Manana  * We also need this 2 phases setup when adding a device to a filesystem with
394279bd3712SFilipe Manana  * a seed device - we must create new metadata and system chunks without adding
394379bd3712SFilipe Manana  * any of the block group items to the chunk, extent and device btrees. If we
394479bd3712SFilipe Manana  * did not do it this way, we would get ENOSPC when attempting to update those
394579bd3712SFilipe Manana  * btrees, since all the chunks from the seed device are read-only.
394679bd3712SFilipe Manana  *
394779bd3712SFilipe Manana  * Phase 1 does the updates and insertions to the chunk btree because if we had
394879bd3712SFilipe Manana  * it done in phase 2 and have a thundering herd of tasks allocating chunks in
394979bd3712SFilipe Manana  * parallel, we risk having too many system chunks allocated by many tasks if
395079bd3712SFilipe Manana  * many tasks reach phase 1 without the previous ones completing phase 2. In the
395179bd3712SFilipe Manana  * extreme case this leads to exhaustion of the system chunk array in the
395279bd3712SFilipe Manana  * superblock. This is easier to trigger if using a btree node/leaf size of 64K
395379bd3712SFilipe Manana  * and with RAID filesystems (so we have more device items in the chunk btree).
395479bd3712SFilipe Manana  * This has happened before and commit eafa4fd0ad0607 ("btrfs: fix exhaustion of
395579bd3712SFilipe Manana  * the system chunk array due to concurrent allocations") provides more details.
395679bd3712SFilipe Manana  *
39572bb2e00eSFilipe Manana  * Allocation of system chunks does not happen through this function. A task that
39582bb2e00eSFilipe Manana  * needs to update the chunk btree (the only btree that uses system chunks), must
39592bb2e00eSFilipe Manana  * preallocate chunk space by calling either check_system_chunk() or
39602bb2e00eSFilipe Manana  * btrfs_reserve_chunk_metadata() - the former is used when allocating a data or
39612bb2e00eSFilipe Manana  * metadata chunk or when removing a chunk, while the later is used before doing
39622bb2e00eSFilipe Manana  * a modification to the chunk btree - use cases for the later are adding,
39632bb2e00eSFilipe Manana  * removing and resizing a device as well as relocation of a system chunk.
39642bb2e00eSFilipe Manana  * See the comment below for more details.
396579bd3712SFilipe Manana  *
396679bd3712SFilipe Manana  * The reservation of system space, done through check_system_chunk(), as well
396779bd3712SFilipe Manana  * as all the updates and insertions into the chunk btree must be done while
396879bd3712SFilipe Manana  * holding fs_info->chunk_mutex. This is important to guarantee that while COWing
396979bd3712SFilipe Manana  * an extent buffer from the chunks btree we never trigger allocation of a new
397079bd3712SFilipe Manana  * system chunk, which would result in a deadlock (trying to lock twice an
397179bd3712SFilipe Manana  * extent buffer of the chunk btree, first time before triggering the chunk
397279bd3712SFilipe Manana  * allocation and the second time during chunk allocation while attempting to
397379bd3712SFilipe Manana  * update the chunks btree). The system chunk array is also updated while holding
397479bd3712SFilipe Manana  * that mutex. The same logic applies to removing chunks - we must reserve system
397579bd3712SFilipe Manana  * space, update the chunk btree and the system chunk array in the superblock
397679bd3712SFilipe Manana  * while holding fs_info->chunk_mutex.
397779bd3712SFilipe Manana  *
397879bd3712SFilipe Manana  * This function, btrfs_chunk_alloc(), belongs to phase 1.
397979bd3712SFilipe Manana  *
398079bd3712SFilipe Manana  * If @force is CHUNK_ALLOC_FORCE:
398107730d87SJosef Bacik  *    - return 1 if it successfully allocates a chunk,
398207730d87SJosef Bacik  *    - return errors including -ENOSPC otherwise.
398379bd3712SFilipe Manana  * If @force is NOT CHUNK_ALLOC_FORCE:
398407730d87SJosef Bacik  *    - return 0 if it doesn't need to allocate a new chunk,
398507730d87SJosef Bacik  *    - return 1 if it successfully allocates a chunk,
398607730d87SJosef Bacik  *    - return errors including -ENOSPC otherwise.
398707730d87SJosef Bacik  */
398807730d87SJosef Bacik int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
398907730d87SJosef Bacik 		      enum btrfs_chunk_alloc_enum force)
399007730d87SJosef Bacik {
399107730d87SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
399207730d87SJosef Bacik 	struct btrfs_space_info *space_info;
3993820c363bSNaohiro Aota 	struct btrfs_block_group *ret_bg;
399407730d87SJosef Bacik 	bool wait_for_alloc = false;
399507730d87SJosef Bacik 	bool should_alloc = false;
3996760e69c4SNaohiro Aota 	bool from_extent_allocation = false;
399707730d87SJosef Bacik 	int ret = 0;
399807730d87SJosef Bacik 
3999760e69c4SNaohiro Aota 	if (force == CHUNK_ALLOC_FORCE_FOR_EXTENT) {
4000760e69c4SNaohiro Aota 		from_extent_allocation = true;
4001760e69c4SNaohiro Aota 		force = CHUNK_ALLOC_FORCE;
4002760e69c4SNaohiro Aota 	}
4003760e69c4SNaohiro Aota 
400407730d87SJosef Bacik 	/* Don't re-enter if we're already allocating a chunk */
400507730d87SJosef Bacik 	if (trans->allocating_chunk)
400607730d87SJosef Bacik 		return -ENOSPC;
400779bd3712SFilipe Manana 	/*
40082bb2e00eSFilipe Manana 	 * Allocation of system chunks can not happen through this path, as we
40092bb2e00eSFilipe Manana 	 * could end up in a deadlock if we are allocating a data or metadata
40102bb2e00eSFilipe Manana 	 * chunk and there is another task modifying the chunk btree.
40112bb2e00eSFilipe Manana 	 *
40122bb2e00eSFilipe Manana 	 * This is because while we are holding the chunk mutex, we will attempt
40132bb2e00eSFilipe Manana 	 * to add the new chunk item to the chunk btree or update an existing
40142bb2e00eSFilipe Manana 	 * device item in the chunk btree, while the other task that is modifying
40152bb2e00eSFilipe Manana 	 * the chunk btree is attempting to COW an extent buffer while holding a
40162bb2e00eSFilipe Manana 	 * lock on it and on its parent - if the COW operation triggers a system
40172bb2e00eSFilipe Manana 	 * chunk allocation, then we can deadlock because we are holding the
40182bb2e00eSFilipe Manana 	 * chunk mutex and we may need to access that extent buffer or its parent
40192bb2e00eSFilipe Manana 	 * in order to add the chunk item or update a device item.
40202bb2e00eSFilipe Manana 	 *
40212bb2e00eSFilipe Manana 	 * Tasks that want to modify the chunk tree should reserve system space
40222bb2e00eSFilipe Manana 	 * before updating the chunk btree, by calling either
40232bb2e00eSFilipe Manana 	 * btrfs_reserve_chunk_metadata() or check_system_chunk().
40242bb2e00eSFilipe Manana 	 * It's possible that after a task reserves the space, it still ends up
40252bb2e00eSFilipe Manana 	 * here - this happens in the cases described above at do_chunk_alloc().
40262bb2e00eSFilipe Manana 	 * The task will have to either retry or fail.
402779bd3712SFilipe Manana 	 */
40282bb2e00eSFilipe Manana 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
402979bd3712SFilipe Manana 		return -ENOSPC;
403007730d87SJosef Bacik 
403107730d87SJosef Bacik 	space_info = btrfs_find_space_info(fs_info, flags);
403207730d87SJosef Bacik 	ASSERT(space_info);
403307730d87SJosef Bacik 
403407730d87SJosef Bacik 	do {
403507730d87SJosef Bacik 		spin_lock(&space_info->lock);
403607730d87SJosef Bacik 		if (force < space_info->force_alloc)
403707730d87SJosef Bacik 			force = space_info->force_alloc;
403807730d87SJosef Bacik 		should_alloc = should_alloc_chunk(fs_info, space_info, force);
403907730d87SJosef Bacik 		if (space_info->full) {
404007730d87SJosef Bacik 			/* No more free physical space */
404107730d87SJosef Bacik 			if (should_alloc)
404207730d87SJosef Bacik 				ret = -ENOSPC;
404307730d87SJosef Bacik 			else
404407730d87SJosef Bacik 				ret = 0;
404507730d87SJosef Bacik 			spin_unlock(&space_info->lock);
404607730d87SJosef Bacik 			return ret;
404707730d87SJosef Bacik 		} else if (!should_alloc) {
404807730d87SJosef Bacik 			spin_unlock(&space_info->lock);
404907730d87SJosef Bacik 			return 0;
405007730d87SJosef Bacik 		} else if (space_info->chunk_alloc) {
405107730d87SJosef Bacik 			/*
405207730d87SJosef Bacik 			 * Someone is already allocating, so we need to block
405307730d87SJosef Bacik 			 * until this someone is finished and then loop to
405407730d87SJosef Bacik 			 * recheck if we should continue with our allocation
405507730d87SJosef Bacik 			 * attempt.
405607730d87SJosef Bacik 			 */
405707730d87SJosef Bacik 			wait_for_alloc = true;
40581314ca78SJosef Bacik 			force = CHUNK_ALLOC_NO_FORCE;
405907730d87SJosef Bacik 			spin_unlock(&space_info->lock);
406007730d87SJosef Bacik 			mutex_lock(&fs_info->chunk_mutex);
406107730d87SJosef Bacik 			mutex_unlock(&fs_info->chunk_mutex);
406207730d87SJosef Bacik 		} else {
406307730d87SJosef Bacik 			/* Proceed with allocation */
406407730d87SJosef Bacik 			space_info->chunk_alloc = 1;
406507730d87SJosef Bacik 			wait_for_alloc = false;
406607730d87SJosef Bacik 			spin_unlock(&space_info->lock);
406707730d87SJosef Bacik 		}
406807730d87SJosef Bacik 
406907730d87SJosef Bacik 		cond_resched();
407007730d87SJosef Bacik 	} while (wait_for_alloc);
407107730d87SJosef Bacik 
407207730d87SJosef Bacik 	mutex_lock(&fs_info->chunk_mutex);
407307730d87SJosef Bacik 	trans->allocating_chunk = true;
407407730d87SJosef Bacik 
407507730d87SJosef Bacik 	/*
407607730d87SJosef Bacik 	 * If we have mixed data/metadata chunks we want to make sure we keep
407707730d87SJosef Bacik 	 * allocating mixed chunks instead of individual chunks.
407807730d87SJosef Bacik 	 */
407907730d87SJosef Bacik 	if (btrfs_mixed_space_info(space_info))
408007730d87SJosef Bacik 		flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
408107730d87SJosef Bacik 
408207730d87SJosef Bacik 	/*
408307730d87SJosef Bacik 	 * if we're doing a data chunk, go ahead and make sure that
408407730d87SJosef Bacik 	 * we keep a reasonable number of metadata chunks allocated in the
408507730d87SJosef Bacik 	 * FS as well.
408607730d87SJosef Bacik 	 */
408707730d87SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
408807730d87SJosef Bacik 		fs_info->data_chunk_allocations++;
408907730d87SJosef Bacik 		if (!(fs_info->data_chunk_allocations %
409007730d87SJosef Bacik 		      fs_info->metadata_ratio))
409107730d87SJosef Bacik 			force_metadata_allocation(fs_info);
409207730d87SJosef Bacik 	}
409307730d87SJosef Bacik 
4094820c363bSNaohiro Aota 	ret_bg = do_chunk_alloc(trans, flags);
409507730d87SJosef Bacik 	trans->allocating_chunk = false;
409607730d87SJosef Bacik 
4097760e69c4SNaohiro Aota 	if (IS_ERR(ret_bg)) {
4098820c363bSNaohiro Aota 		ret = PTR_ERR(ret_bg);
40995a7d107eSNaohiro Aota 	} else if (from_extent_allocation && (flags & BTRFS_BLOCK_GROUP_DATA)) {
4100760e69c4SNaohiro Aota 		/*
4101760e69c4SNaohiro Aota 		 * New block group is likely to be used soon. Try to activate
4102760e69c4SNaohiro Aota 		 * it now. Failure is OK for now.
4103760e69c4SNaohiro Aota 		 */
4104760e69c4SNaohiro Aota 		btrfs_zone_activate(ret_bg);
4105760e69c4SNaohiro Aota 	}
4106760e69c4SNaohiro Aota 
4107760e69c4SNaohiro Aota 	if (!ret)
4108820c363bSNaohiro Aota 		btrfs_put_block_group(ret_bg);
4109820c363bSNaohiro Aota 
411007730d87SJosef Bacik 	spin_lock(&space_info->lock);
411107730d87SJosef Bacik 	if (ret < 0) {
411207730d87SJosef Bacik 		if (ret == -ENOSPC)
411307730d87SJosef Bacik 			space_info->full = 1;
411407730d87SJosef Bacik 		else
411507730d87SJosef Bacik 			goto out;
411607730d87SJosef Bacik 	} else {
411707730d87SJosef Bacik 		ret = 1;
411807730d87SJosef Bacik 		space_info->max_extent_size = 0;
411907730d87SJosef Bacik 	}
412007730d87SJosef Bacik 
412107730d87SJosef Bacik 	space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
412207730d87SJosef Bacik out:
412307730d87SJosef Bacik 	space_info->chunk_alloc = 0;
412407730d87SJosef Bacik 	spin_unlock(&space_info->lock);
412507730d87SJosef Bacik 	mutex_unlock(&fs_info->chunk_mutex);
412607730d87SJosef Bacik 
412707730d87SJosef Bacik 	return ret;
412807730d87SJosef Bacik }
412907730d87SJosef Bacik 
413007730d87SJosef Bacik static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
413107730d87SJosef Bacik {
413207730d87SJosef Bacik 	u64 num_dev;
413307730d87SJosef Bacik 
413407730d87SJosef Bacik 	num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max;
413507730d87SJosef Bacik 	if (!num_dev)
413607730d87SJosef Bacik 		num_dev = fs_info->fs_devices->rw_devices;
413707730d87SJosef Bacik 
413807730d87SJosef Bacik 	return num_dev;
413907730d87SJosef Bacik }
414007730d87SJosef Bacik 
41412bb2e00eSFilipe Manana static void reserve_chunk_space(struct btrfs_trans_handle *trans,
41422bb2e00eSFilipe Manana 				u64 bytes,
41432bb2e00eSFilipe Manana 				u64 type)
414407730d87SJosef Bacik {
414507730d87SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
414607730d87SJosef Bacik 	struct btrfs_space_info *info;
414707730d87SJosef Bacik 	u64 left;
414807730d87SJosef Bacik 	int ret = 0;
414907730d87SJosef Bacik 
415007730d87SJosef Bacik 	/*
415107730d87SJosef Bacik 	 * Needed because we can end up allocating a system chunk and for an
415207730d87SJosef Bacik 	 * atomic and race free space reservation in the chunk block reserve.
415307730d87SJosef Bacik 	 */
415407730d87SJosef Bacik 	lockdep_assert_held(&fs_info->chunk_mutex);
415507730d87SJosef Bacik 
415607730d87SJosef Bacik 	info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
415707730d87SJosef Bacik 	spin_lock(&info->lock);
415807730d87SJosef Bacik 	left = info->total_bytes - btrfs_space_info_used(info, true);
415907730d87SJosef Bacik 	spin_unlock(&info->lock);
416007730d87SJosef Bacik 
41612bb2e00eSFilipe Manana 	if (left < bytes && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
416207730d87SJosef Bacik 		btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
41632bb2e00eSFilipe Manana 			   left, bytes, type);
416407730d87SJosef Bacik 		btrfs_dump_space_info(fs_info, info, 0, 0);
416507730d87SJosef Bacik 	}
416607730d87SJosef Bacik 
41672bb2e00eSFilipe Manana 	if (left < bytes) {
416807730d87SJosef Bacik 		u64 flags = btrfs_system_alloc_profile(fs_info);
416979bd3712SFilipe Manana 		struct btrfs_block_group *bg;
417007730d87SJosef Bacik 
417107730d87SJosef Bacik 		/*
417207730d87SJosef Bacik 		 * Ignore failure to create system chunk. We might end up not
417307730d87SJosef Bacik 		 * needing it, as we might not need to COW all nodes/leafs from
417407730d87SJosef Bacik 		 * the paths we visit in the chunk tree (they were already COWed
417507730d87SJosef Bacik 		 * or created in the current transaction for example).
417607730d87SJosef Bacik 		 */
4177f6f39f7aSNikolay Borisov 		bg = btrfs_create_chunk(trans, flags);
417879bd3712SFilipe Manana 		if (IS_ERR(bg)) {
417979bd3712SFilipe Manana 			ret = PTR_ERR(bg);
41802bb2e00eSFilipe Manana 		} else {
418179bd3712SFilipe Manana 			/*
4182b6a98021SNaohiro Aota 			 * We have a new chunk. We also need to activate it for
4183b6a98021SNaohiro Aota 			 * zoned filesystem.
4184b6a98021SNaohiro Aota 			 */
4185b6a98021SNaohiro Aota 			ret = btrfs_zoned_activate_one_bg(fs_info, info, true);
4186b6a98021SNaohiro Aota 			if (ret < 0)
4187b6a98021SNaohiro Aota 				return;
4188b6a98021SNaohiro Aota 
4189b6a98021SNaohiro Aota 			/*
419079bd3712SFilipe Manana 			 * If we fail to add the chunk item here, we end up
419179bd3712SFilipe Manana 			 * trying again at phase 2 of chunk allocation, at
419279bd3712SFilipe Manana 			 * btrfs_create_pending_block_groups(). So ignore
41932bb2e00eSFilipe Manana 			 * any error here. An ENOSPC here could happen, due to
41942bb2e00eSFilipe Manana 			 * the cases described at do_chunk_alloc() - the system
41952bb2e00eSFilipe Manana 			 * block group we just created was just turned into RO
41962bb2e00eSFilipe Manana 			 * mode by a scrub for example, or a running discard
41972bb2e00eSFilipe Manana 			 * temporarily removed its free space entries, etc.
419879bd3712SFilipe Manana 			 */
419979bd3712SFilipe Manana 			btrfs_chunk_alloc_add_chunk_item(trans, bg);
420079bd3712SFilipe Manana 		}
420107730d87SJosef Bacik 	}
420207730d87SJosef Bacik 
420307730d87SJosef Bacik 	if (!ret) {
42049270501cSJosef Bacik 		ret = btrfs_block_rsv_add(fs_info,
420507730d87SJosef Bacik 					  &fs_info->chunk_block_rsv,
42062bb2e00eSFilipe Manana 					  bytes, BTRFS_RESERVE_NO_FLUSH);
42071cb3db1cSFilipe Manana 		if (!ret)
42082bb2e00eSFilipe Manana 			trans->chunk_bytes_reserved += bytes;
420907730d87SJosef Bacik 	}
421007730d87SJosef Bacik }
421107730d87SJosef Bacik 
42122bb2e00eSFilipe Manana /*
42132bb2e00eSFilipe Manana  * Reserve space in the system space for allocating or removing a chunk.
42142bb2e00eSFilipe Manana  * The caller must be holding fs_info->chunk_mutex.
42152bb2e00eSFilipe Manana  */
42162bb2e00eSFilipe Manana void check_system_chunk(struct btrfs_trans_handle *trans, u64 type)
42172bb2e00eSFilipe Manana {
42182bb2e00eSFilipe Manana 	struct btrfs_fs_info *fs_info = trans->fs_info;
42192bb2e00eSFilipe Manana 	const u64 num_devs = get_profile_num_devs(fs_info, type);
42202bb2e00eSFilipe Manana 	u64 bytes;
42212bb2e00eSFilipe Manana 
42222bb2e00eSFilipe Manana 	/* num_devs device items to update and 1 chunk item to add or remove. */
42232bb2e00eSFilipe Manana 	bytes = btrfs_calc_metadata_size(fs_info, num_devs) +
42242bb2e00eSFilipe Manana 		btrfs_calc_insert_metadata_size(fs_info, 1);
42252bb2e00eSFilipe Manana 
42262bb2e00eSFilipe Manana 	reserve_chunk_space(trans, bytes, type);
42272bb2e00eSFilipe Manana }
42282bb2e00eSFilipe Manana 
42292bb2e00eSFilipe Manana /*
42302bb2e00eSFilipe Manana  * Reserve space in the system space, if needed, for doing a modification to the
42312bb2e00eSFilipe Manana  * chunk btree.
42322bb2e00eSFilipe Manana  *
42332bb2e00eSFilipe Manana  * @trans:		A transaction handle.
42342bb2e00eSFilipe Manana  * @is_item_insertion:	Indicate if the modification is for inserting a new item
42352bb2e00eSFilipe Manana  *			in the chunk btree or if it's for the deletion or update
42362bb2e00eSFilipe Manana  *			of an existing item.
42372bb2e00eSFilipe Manana  *
42382bb2e00eSFilipe Manana  * This is used in a context where we need to update the chunk btree outside
42392bb2e00eSFilipe Manana  * block group allocation and removal, to avoid a deadlock with a concurrent
42402bb2e00eSFilipe Manana  * task that is allocating a metadata or data block group and therefore needs to
42412bb2e00eSFilipe Manana  * update the chunk btree while holding the chunk mutex. After the update to the
42422bb2e00eSFilipe Manana  * chunk btree is done, btrfs_trans_release_chunk_metadata() should be called.
42432bb2e00eSFilipe Manana  *
42442bb2e00eSFilipe Manana  */
42452bb2e00eSFilipe Manana void btrfs_reserve_chunk_metadata(struct btrfs_trans_handle *trans,
42462bb2e00eSFilipe Manana 				  bool is_item_insertion)
42472bb2e00eSFilipe Manana {
42482bb2e00eSFilipe Manana 	struct btrfs_fs_info *fs_info = trans->fs_info;
42492bb2e00eSFilipe Manana 	u64 bytes;
42502bb2e00eSFilipe Manana 
42512bb2e00eSFilipe Manana 	if (is_item_insertion)
42522bb2e00eSFilipe Manana 		bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
42532bb2e00eSFilipe Manana 	else
42542bb2e00eSFilipe Manana 		bytes = btrfs_calc_metadata_size(fs_info, 1);
42552bb2e00eSFilipe Manana 
42562bb2e00eSFilipe Manana 	mutex_lock(&fs_info->chunk_mutex);
42572bb2e00eSFilipe Manana 	reserve_chunk_space(trans, bytes, BTRFS_BLOCK_GROUP_SYSTEM);
42582bb2e00eSFilipe Manana 	mutex_unlock(&fs_info->chunk_mutex);
42592bb2e00eSFilipe Manana }
42602bb2e00eSFilipe Manana 
42613e43c279SJosef Bacik void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
42623e43c279SJosef Bacik {
426332da5386SDavid Sterba 	struct btrfs_block_group *block_group;
42643e43c279SJosef Bacik 
426550c31eaaSJosef Bacik 	block_group = btrfs_lookup_first_block_group(info, 0);
42663e43c279SJosef Bacik 	while (block_group) {
42673e43c279SJosef Bacik 		btrfs_wait_block_group_cache_done(block_group);
42683e43c279SJosef Bacik 		spin_lock(&block_group->lock);
426950c31eaaSJosef Bacik 		if (test_and_clear_bit(BLOCK_GROUP_FLAG_IREF,
427050c31eaaSJosef Bacik 				       &block_group->runtime_flags)) {
427150c31eaaSJosef Bacik 			struct inode *inode = block_group->inode;
42723e43c279SJosef Bacik 
42733e43c279SJosef Bacik 			block_group->inode = NULL;
42743e43c279SJosef Bacik 			spin_unlock(&block_group->lock);
427550c31eaaSJosef Bacik 
42763e43c279SJosef Bacik 			ASSERT(block_group->io_ctl.inode == NULL);
42773e43c279SJosef Bacik 			iput(inode);
427850c31eaaSJosef Bacik 		} else {
427950c31eaaSJosef Bacik 			spin_unlock(&block_group->lock);
428050c31eaaSJosef Bacik 		}
428150c31eaaSJosef Bacik 		block_group = btrfs_next_block_group(block_group);
42823e43c279SJosef Bacik 	}
42833e43c279SJosef Bacik }
42843e43c279SJosef Bacik 
42853e43c279SJosef Bacik /*
42863e43c279SJosef Bacik  * Must be called only after stopping all workers, since we could have block
42873e43c279SJosef Bacik  * group caching kthreads running, and therefore they could race with us if we
42883e43c279SJosef Bacik  * freed the block groups before stopping them.
42893e43c279SJosef Bacik  */
42903e43c279SJosef Bacik int btrfs_free_block_groups(struct btrfs_fs_info *info)
42913e43c279SJosef Bacik {
429232da5386SDavid Sterba 	struct btrfs_block_group *block_group;
42933e43c279SJosef Bacik 	struct btrfs_space_info *space_info;
42943e43c279SJosef Bacik 	struct btrfs_caching_control *caching_ctl;
42953e43c279SJosef Bacik 	struct rb_node *n;
42963e43c279SJosef Bacik 
429713bb483dSNaohiro Aota 	if (btrfs_is_zoned(info)) {
429813bb483dSNaohiro Aota 		if (info->active_meta_bg) {
429913bb483dSNaohiro Aota 			btrfs_put_block_group(info->active_meta_bg);
430013bb483dSNaohiro Aota 			info->active_meta_bg = NULL;
430113bb483dSNaohiro Aota 		}
430213bb483dSNaohiro Aota 		if (info->active_system_bg) {
430313bb483dSNaohiro Aota 			btrfs_put_block_group(info->active_system_bg);
430413bb483dSNaohiro Aota 			info->active_system_bg = NULL;
430513bb483dSNaohiro Aota 		}
430613bb483dSNaohiro Aota 	}
430713bb483dSNaohiro Aota 
430816b0c258SFilipe Manana 	write_lock(&info->block_group_cache_lock);
43093e43c279SJosef Bacik 	while (!list_empty(&info->caching_block_groups)) {
43103e43c279SJosef Bacik 		caching_ctl = list_entry(info->caching_block_groups.next,
43113e43c279SJosef Bacik 					 struct btrfs_caching_control, list);
43123e43c279SJosef Bacik 		list_del(&caching_ctl->list);
43133e43c279SJosef Bacik 		btrfs_put_caching_control(caching_ctl);
43143e43c279SJosef Bacik 	}
431516b0c258SFilipe Manana 	write_unlock(&info->block_group_cache_lock);
43163e43c279SJosef Bacik 
43173e43c279SJosef Bacik 	spin_lock(&info->unused_bgs_lock);
43183e43c279SJosef Bacik 	while (!list_empty(&info->unused_bgs)) {
43193e43c279SJosef Bacik 		block_group = list_first_entry(&info->unused_bgs,
432032da5386SDavid Sterba 					       struct btrfs_block_group,
43213e43c279SJosef Bacik 					       bg_list);
43223e43c279SJosef Bacik 		list_del_init(&block_group->bg_list);
43233e43c279SJosef Bacik 		btrfs_put_block_group(block_group);
43243e43c279SJosef Bacik 	}
43253e43c279SJosef Bacik 
432618bb8bbfSJohannes Thumshirn 	while (!list_empty(&info->reclaim_bgs)) {
432718bb8bbfSJohannes Thumshirn 		block_group = list_first_entry(&info->reclaim_bgs,
432818bb8bbfSJohannes Thumshirn 					       struct btrfs_block_group,
432918bb8bbfSJohannes Thumshirn 					       bg_list);
433018bb8bbfSJohannes Thumshirn 		list_del_init(&block_group->bg_list);
433118bb8bbfSJohannes Thumshirn 		btrfs_put_block_group(block_group);
433218bb8bbfSJohannes Thumshirn 	}
433318bb8bbfSJohannes Thumshirn 	spin_unlock(&info->unused_bgs_lock);
433418bb8bbfSJohannes Thumshirn 
4335afba2bc0SNaohiro Aota 	spin_lock(&info->zone_active_bgs_lock);
4336afba2bc0SNaohiro Aota 	while (!list_empty(&info->zone_active_bgs)) {
4337afba2bc0SNaohiro Aota 		block_group = list_first_entry(&info->zone_active_bgs,
4338afba2bc0SNaohiro Aota 					       struct btrfs_block_group,
4339afba2bc0SNaohiro Aota 					       active_bg_list);
4340afba2bc0SNaohiro Aota 		list_del_init(&block_group->active_bg_list);
4341afba2bc0SNaohiro Aota 		btrfs_put_block_group(block_group);
4342afba2bc0SNaohiro Aota 	}
4343afba2bc0SNaohiro Aota 	spin_unlock(&info->zone_active_bgs_lock);
4344afba2bc0SNaohiro Aota 
434516b0c258SFilipe Manana 	write_lock(&info->block_group_cache_lock);
434608dddb29SFilipe Manana 	while ((n = rb_last(&info->block_group_cache_tree.rb_root)) != NULL) {
434732da5386SDavid Sterba 		block_group = rb_entry(n, struct btrfs_block_group,
43483e43c279SJosef Bacik 				       cache_node);
434908dddb29SFilipe Manana 		rb_erase_cached(&block_group->cache_node,
43503e43c279SJosef Bacik 				&info->block_group_cache_tree);
43513e43c279SJosef Bacik 		RB_CLEAR_NODE(&block_group->cache_node);
435216b0c258SFilipe Manana 		write_unlock(&info->block_group_cache_lock);
43533e43c279SJosef Bacik 
43543e43c279SJosef Bacik 		down_write(&block_group->space_info->groups_sem);
43553e43c279SJosef Bacik 		list_del(&block_group->list);
43563e43c279SJosef Bacik 		up_write(&block_group->space_info->groups_sem);
43573e43c279SJosef Bacik 
43583e43c279SJosef Bacik 		/*
43593e43c279SJosef Bacik 		 * We haven't cached this block group, which means we could
43603e43c279SJosef Bacik 		 * possibly have excluded extents on this block group.
43613e43c279SJosef Bacik 		 */
43623e43c279SJosef Bacik 		if (block_group->cached == BTRFS_CACHE_NO ||
43633e43c279SJosef Bacik 		    block_group->cached == BTRFS_CACHE_ERROR)
43643e43c279SJosef Bacik 			btrfs_free_excluded_extents(block_group);
43653e43c279SJosef Bacik 
43663e43c279SJosef Bacik 		btrfs_remove_free_space_cache(block_group);
43673e43c279SJosef Bacik 		ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
43683e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->dirty_list));
43693e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->io_list));
43703e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->bg_list));
437148aaeebeSJosef Bacik 		ASSERT(refcount_read(&block_group->refs) == 1);
4372195a49eaSFilipe Manana 		ASSERT(block_group->swap_extents == 0);
43733e43c279SJosef Bacik 		btrfs_put_block_group(block_group);
43743e43c279SJosef Bacik 
437516b0c258SFilipe Manana 		write_lock(&info->block_group_cache_lock);
43763e43c279SJosef Bacik 	}
437716b0c258SFilipe Manana 	write_unlock(&info->block_group_cache_lock);
43783e43c279SJosef Bacik 
43793e43c279SJosef Bacik 	btrfs_release_global_block_rsv(info);
43803e43c279SJosef Bacik 
43813e43c279SJosef Bacik 	while (!list_empty(&info->space_info)) {
43823e43c279SJosef Bacik 		space_info = list_entry(info->space_info.next,
43833e43c279SJosef Bacik 					struct btrfs_space_info,
43843e43c279SJosef Bacik 					list);
43853e43c279SJosef Bacik 
43863e43c279SJosef Bacik 		/*
43873e43c279SJosef Bacik 		 * Do not hide this behind enospc_debug, this is actually
43883e43c279SJosef Bacik 		 * important and indicates a real bug if this happens.
43893e43c279SJosef Bacik 		 */
43903e43c279SJosef Bacik 		if (WARN_ON(space_info->bytes_pinned > 0 ||
43913e43c279SJosef Bacik 			    space_info->bytes_may_use > 0))
43923e43c279SJosef Bacik 			btrfs_dump_space_info(info, space_info, 0, 0);
439340cdc509SFilipe Manana 
439440cdc509SFilipe Manana 		/*
439540cdc509SFilipe Manana 		 * If there was a failure to cleanup a log tree, very likely due
439640cdc509SFilipe Manana 		 * to an IO failure on a writeback attempt of one or more of its
439740cdc509SFilipe Manana 		 * extent buffers, we could not do proper (and cheap) unaccounting
439840cdc509SFilipe Manana 		 * of their reserved space, so don't warn on bytes_reserved > 0 in
439940cdc509SFilipe Manana 		 * that case.
440040cdc509SFilipe Manana 		 */
440140cdc509SFilipe Manana 		if (!(space_info->flags & BTRFS_BLOCK_GROUP_METADATA) ||
440240cdc509SFilipe Manana 		    !BTRFS_FS_LOG_CLEANUP_ERROR(info)) {
440340cdc509SFilipe Manana 			if (WARN_ON(space_info->bytes_reserved > 0))
440440cdc509SFilipe Manana 				btrfs_dump_space_info(info, space_info, 0, 0);
440540cdc509SFilipe Manana 		}
440640cdc509SFilipe Manana 
4407d611add4SFilipe Manana 		WARN_ON(space_info->reclaim_size > 0);
44083e43c279SJosef Bacik 		list_del(&space_info->list);
44093e43c279SJosef Bacik 		btrfs_sysfs_remove_space_info(space_info);
44103e43c279SJosef Bacik 	}
44113e43c279SJosef Bacik 	return 0;
44123e43c279SJosef Bacik }
4413684b752bSFilipe Manana 
4414684b752bSFilipe Manana void btrfs_freeze_block_group(struct btrfs_block_group *cache)
4415684b752bSFilipe Manana {
4416684b752bSFilipe Manana 	atomic_inc(&cache->frozen);
4417684b752bSFilipe Manana }
4418684b752bSFilipe Manana 
4419684b752bSFilipe Manana void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
4420684b752bSFilipe Manana {
4421684b752bSFilipe Manana 	struct btrfs_fs_info *fs_info = block_group->fs_info;
4422684b752bSFilipe Manana 	struct extent_map_tree *em_tree;
4423684b752bSFilipe Manana 	struct extent_map *em;
4424684b752bSFilipe Manana 	bool cleanup;
4425684b752bSFilipe Manana 
4426684b752bSFilipe Manana 	spin_lock(&block_group->lock);
4427684b752bSFilipe Manana 	cleanup = (atomic_dec_and_test(&block_group->frozen) &&
44283349b57fSJosef Bacik 		   test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags));
4429684b752bSFilipe Manana 	spin_unlock(&block_group->lock);
4430684b752bSFilipe Manana 
4431684b752bSFilipe Manana 	if (cleanup) {
4432684b752bSFilipe Manana 		em_tree = &fs_info->mapping_tree;
4433684b752bSFilipe Manana 		write_lock(&em_tree->lock);
4434684b752bSFilipe Manana 		em = lookup_extent_mapping(em_tree, block_group->start,
4435684b752bSFilipe Manana 					   1);
4436684b752bSFilipe Manana 		BUG_ON(!em); /* logic error, can't happen */
4437684b752bSFilipe Manana 		remove_extent_mapping(em_tree, em);
4438684b752bSFilipe Manana 		write_unlock(&em_tree->lock);
4439684b752bSFilipe Manana 
4440684b752bSFilipe Manana 		/* once for us and once for the tree */
4441684b752bSFilipe Manana 		free_extent_map(em);
4442684b752bSFilipe Manana 		free_extent_map(em);
4443684b752bSFilipe Manana 
4444684b752bSFilipe Manana 		/*
4445684b752bSFilipe Manana 		 * We may have left one free space entry and other possible
4446684b752bSFilipe Manana 		 * tasks trimming this block group have left 1 entry each one.
4447684b752bSFilipe Manana 		 * Free them if any.
4448684b752bSFilipe Manana 		 */
4449fc80f7acSJosef Bacik 		btrfs_remove_free_space_cache(block_group);
4450684b752bSFilipe Manana 	}
4451684b752bSFilipe Manana }
4452195a49eaSFilipe Manana 
4453195a49eaSFilipe Manana bool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg)
4454195a49eaSFilipe Manana {
4455195a49eaSFilipe Manana 	bool ret = true;
4456195a49eaSFilipe Manana 
4457195a49eaSFilipe Manana 	spin_lock(&bg->lock);
4458195a49eaSFilipe Manana 	if (bg->ro)
4459195a49eaSFilipe Manana 		ret = false;
4460195a49eaSFilipe Manana 	else
4461195a49eaSFilipe Manana 		bg->swap_extents++;
4462195a49eaSFilipe Manana 	spin_unlock(&bg->lock);
4463195a49eaSFilipe Manana 
4464195a49eaSFilipe Manana 	return ret;
4465195a49eaSFilipe Manana }
4466195a49eaSFilipe Manana 
4467195a49eaSFilipe Manana void btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount)
4468195a49eaSFilipe Manana {
4469195a49eaSFilipe Manana 	spin_lock(&bg->lock);
4470195a49eaSFilipe Manana 	ASSERT(!bg->ro);
4471195a49eaSFilipe Manana 	ASSERT(bg->swap_extents >= amount);
4472195a49eaSFilipe Manana 	bg->swap_extents -= amount;
4473195a49eaSFilipe Manana 	spin_unlock(&bg->lock);
4474195a49eaSFilipe Manana }
447552bb7a21SBoris Burkov 
447652bb7a21SBoris Burkov enum btrfs_block_group_size_class btrfs_calc_block_group_size_class(u64 size)
447752bb7a21SBoris Burkov {
447852bb7a21SBoris Burkov 	if (size <= SZ_128K)
447952bb7a21SBoris Burkov 		return BTRFS_BG_SZ_SMALL;
448052bb7a21SBoris Burkov 	if (size <= SZ_8M)
448152bb7a21SBoris Burkov 		return BTRFS_BG_SZ_MEDIUM;
448252bb7a21SBoris Burkov 	return BTRFS_BG_SZ_LARGE;
448352bb7a21SBoris Burkov }
448452bb7a21SBoris Burkov 
448552bb7a21SBoris Burkov /*
448652bb7a21SBoris Burkov  * Handle a block group allocating an extent in a size class
448752bb7a21SBoris Burkov  *
448852bb7a21SBoris Burkov  * @bg:				The block group we allocated in.
448952bb7a21SBoris Burkov  * @size_class:			The size class of the allocation.
449052bb7a21SBoris Burkov  * @force_wrong_size_class:	Whether we are desperate enough to allow
449152bb7a21SBoris Burkov  *				mismatched size classes.
449252bb7a21SBoris Burkov  *
449352bb7a21SBoris Burkov  * Returns: 0 if the size class was valid for this block_group, -EAGAIN in the
449452bb7a21SBoris Burkov  * case of a race that leads to the wrong size class without
449552bb7a21SBoris Burkov  * force_wrong_size_class set.
449652bb7a21SBoris Burkov  *
449752bb7a21SBoris Burkov  * find_free_extent will skip block groups with a mismatched size class until
449852bb7a21SBoris Burkov  * it really needs to avoid ENOSPC. In that case it will set
449952bb7a21SBoris Burkov  * force_wrong_size_class. However, if a block group is newly allocated and
450052bb7a21SBoris Burkov  * doesn't yet have a size class, then it is possible for two allocations of
450152bb7a21SBoris Burkov  * different sizes to race and both try to use it. The loser is caught here and
450252bb7a21SBoris Burkov  * has to retry.
450352bb7a21SBoris Burkov  */
450452bb7a21SBoris Burkov int btrfs_use_block_group_size_class(struct btrfs_block_group *bg,
450552bb7a21SBoris Burkov 				     enum btrfs_block_group_size_class size_class,
450652bb7a21SBoris Burkov 				     bool force_wrong_size_class)
450752bb7a21SBoris Burkov {
450852bb7a21SBoris Burkov 	ASSERT(size_class != BTRFS_BG_SZ_NONE);
450952bb7a21SBoris Burkov 
451052bb7a21SBoris Burkov 	/* The new allocation is in the right size class, do nothing */
451152bb7a21SBoris Burkov 	if (bg->size_class == size_class)
451252bb7a21SBoris Burkov 		return 0;
451352bb7a21SBoris Burkov 	/*
451452bb7a21SBoris Burkov 	 * The new allocation is in a mismatched size class.
451552bb7a21SBoris Burkov 	 * This means one of two things:
451652bb7a21SBoris Burkov 	 *
451752bb7a21SBoris Burkov 	 * 1. Two tasks in find_free_extent for different size_classes raced
451852bb7a21SBoris Burkov 	 *    and hit the same empty block_group. Make the loser try again.
451952bb7a21SBoris Burkov 	 * 2. A call to find_free_extent got desperate enough to set
452052bb7a21SBoris Burkov 	 *    'force_wrong_slab'. Don't change the size_class, but allow the
452152bb7a21SBoris Burkov 	 *    allocation.
452252bb7a21SBoris Burkov 	 */
452352bb7a21SBoris Burkov 	if (bg->size_class != BTRFS_BG_SZ_NONE) {
452452bb7a21SBoris Burkov 		if (force_wrong_size_class)
452552bb7a21SBoris Burkov 			return 0;
452652bb7a21SBoris Burkov 		return -EAGAIN;
452752bb7a21SBoris Burkov 	}
452852bb7a21SBoris Burkov 	/*
452952bb7a21SBoris Burkov 	 * The happy new block group case: the new allocation is the first
453052bb7a21SBoris Burkov 	 * one in the block_group so we set size_class.
453152bb7a21SBoris Burkov 	 */
453252bb7a21SBoris Burkov 	bg->size_class = size_class;
453352bb7a21SBoris Burkov 
453452bb7a21SBoris Burkov 	return 0;
453552bb7a21SBoris Burkov }
4536cb0922f2SBoris Burkov 
4537cb0922f2SBoris Burkov bool btrfs_block_group_should_use_size_class(struct btrfs_block_group *bg)
4538cb0922f2SBoris Burkov {
4539cb0922f2SBoris Burkov 	if (btrfs_is_zoned(bg->fs_info))
4540cb0922f2SBoris Burkov 		return false;
4541cb0922f2SBoris Burkov 	if (!btrfs_is_block_group_data_only(bg))
4542cb0922f2SBoris Burkov 		return false;
4543cb0922f2SBoris Burkov 	return true;
4544cb0922f2SBoris Burkov }
4545