xref: /linux/fs/btrfs/block-group.c (revision 428c8e03109e717d90e0d1329dc3d926b9421ad3)
12e405ad8SJosef Bacik // SPDX-License-Identifier: GPL-2.0
22e405ad8SJosef Bacik 
32ca0ec77SJohannes Thumshirn #include <linux/list_sort.h>
4784352feSDavid Sterba #include "misc.h"
52e405ad8SJosef Bacik #include "ctree.h"
62e405ad8SJosef Bacik #include "block-group.h"
73eeb3226SJosef Bacik #include "space-info.h"
89f21246dSJosef Bacik #include "disk-io.h"
99f21246dSJosef Bacik #include "free-space-cache.h"
109f21246dSJosef Bacik #include "free-space-tree.h"
11e3e0520bSJosef Bacik #include "volumes.h"
12e3e0520bSJosef Bacik #include "transaction.h"
13e3e0520bSJosef Bacik #include "ref-verify.h"
144358d963SJosef Bacik #include "sysfs.h"
154358d963SJosef Bacik #include "tree-log.h"
1677745c05SJosef Bacik #include "delalloc-space.h"
17b0643e59SDennis Zhou #include "discard.h"
1896a14336SNikolay Borisov #include "raid56.h"
1908e11a3dSNaohiro Aota #include "zoned.h"
20c7f13d42SJosef Bacik #include "fs.h"
2107e81dc9SJosef Bacik #include "accessors.h"
22a0231804SJosef Bacik #include "extent-tree.h"
232e405ad8SJosef Bacik 
2406d61cb1SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
2506d61cb1SJosef Bacik int btrfs_should_fragment_free_space(struct btrfs_block_group *block_group)
2606d61cb1SJosef Bacik {
2706d61cb1SJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2806d61cb1SJosef Bacik 
2906d61cb1SJosef Bacik 	return (btrfs_test_opt(fs_info, FRAGMENT_METADATA) &&
3006d61cb1SJosef Bacik 		block_group->flags & BTRFS_BLOCK_GROUP_METADATA) ||
3106d61cb1SJosef Bacik 	       (btrfs_test_opt(fs_info, FRAGMENT_DATA) &&
3206d61cb1SJosef Bacik 		block_group->flags &  BTRFS_BLOCK_GROUP_DATA);
3306d61cb1SJosef Bacik }
3406d61cb1SJosef Bacik #endif
3506d61cb1SJosef Bacik 
36878d7b67SJosef Bacik /*
37878d7b67SJosef Bacik  * Return target flags in extended format or 0 if restripe for this chunk_type
38878d7b67SJosef Bacik  * is not in progress
39878d7b67SJosef Bacik  *
40878d7b67SJosef Bacik  * Should be called with balance_lock held
41878d7b67SJosef Bacik  */
42e11c0406SJosef Bacik static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
43878d7b67SJosef Bacik {
44878d7b67SJosef Bacik 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
45878d7b67SJosef Bacik 	u64 target = 0;
46878d7b67SJosef Bacik 
47878d7b67SJosef Bacik 	if (!bctl)
48878d7b67SJosef Bacik 		return 0;
49878d7b67SJosef Bacik 
50878d7b67SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA &&
51878d7b67SJosef Bacik 	    bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
52878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
53878d7b67SJosef Bacik 	} else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
54878d7b67SJosef Bacik 		   bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
55878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
56878d7b67SJosef Bacik 	} else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
57878d7b67SJosef Bacik 		   bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
58878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
59878d7b67SJosef Bacik 	}
60878d7b67SJosef Bacik 
61878d7b67SJosef Bacik 	return target;
62878d7b67SJosef Bacik }
63878d7b67SJosef Bacik 
64878d7b67SJosef Bacik /*
65878d7b67SJosef Bacik  * @flags: available profiles in extended format (see ctree.h)
66878d7b67SJosef Bacik  *
67878d7b67SJosef Bacik  * Return reduced profile in chunk format.  If profile changing is in progress
68878d7b67SJosef Bacik  * (either running or paused) picks the target profile (if it's already
69878d7b67SJosef Bacik  * available), otherwise falls back to plain reducing.
70878d7b67SJosef Bacik  */
71878d7b67SJosef Bacik static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
72878d7b67SJosef Bacik {
73878d7b67SJosef Bacik 	u64 num_devices = fs_info->fs_devices->rw_devices;
74878d7b67SJosef Bacik 	u64 target;
75878d7b67SJosef Bacik 	u64 raid_type;
76878d7b67SJosef Bacik 	u64 allowed = 0;
77878d7b67SJosef Bacik 
78878d7b67SJosef Bacik 	/*
79878d7b67SJosef Bacik 	 * See if restripe for this chunk_type is in progress, if so try to
80878d7b67SJosef Bacik 	 * reduce to the target profile
81878d7b67SJosef Bacik 	 */
82878d7b67SJosef Bacik 	spin_lock(&fs_info->balance_lock);
83e11c0406SJosef Bacik 	target = get_restripe_target(fs_info, flags);
84878d7b67SJosef Bacik 	if (target) {
85878d7b67SJosef Bacik 		spin_unlock(&fs_info->balance_lock);
86878d7b67SJosef Bacik 		return extended_to_chunk(target);
87878d7b67SJosef Bacik 	}
88878d7b67SJosef Bacik 	spin_unlock(&fs_info->balance_lock);
89878d7b67SJosef Bacik 
90878d7b67SJosef Bacik 	/* First, mask out the RAID levels which aren't possible */
91878d7b67SJosef Bacik 	for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
92878d7b67SJosef Bacik 		if (num_devices >= btrfs_raid_array[raid_type].devs_min)
93878d7b67SJosef Bacik 			allowed |= btrfs_raid_array[raid_type].bg_flag;
94878d7b67SJosef Bacik 	}
95878d7b67SJosef Bacik 	allowed &= flags;
96878d7b67SJosef Bacik 
97878d7b67SJosef Bacik 	if (allowed & BTRFS_BLOCK_GROUP_RAID6)
98878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID6;
99878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
100878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID5;
101878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
102878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID10;
103878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
104878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID1;
105878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
106878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID0;
107878d7b67SJosef Bacik 
108878d7b67SJosef Bacik 	flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
109878d7b67SJosef Bacik 
110878d7b67SJosef Bacik 	return extended_to_chunk(flags | allowed);
111878d7b67SJosef Bacik }
112878d7b67SJosef Bacik 
113ef0a82daSJohannes Thumshirn u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
114878d7b67SJosef Bacik {
115878d7b67SJosef Bacik 	unsigned seq;
116878d7b67SJosef Bacik 	u64 flags;
117878d7b67SJosef Bacik 
118878d7b67SJosef Bacik 	do {
119878d7b67SJosef Bacik 		flags = orig_flags;
120878d7b67SJosef Bacik 		seq = read_seqbegin(&fs_info->profiles_lock);
121878d7b67SJosef Bacik 
122878d7b67SJosef Bacik 		if (flags & BTRFS_BLOCK_GROUP_DATA)
123878d7b67SJosef Bacik 			flags |= fs_info->avail_data_alloc_bits;
124878d7b67SJosef Bacik 		else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
125878d7b67SJosef Bacik 			flags |= fs_info->avail_system_alloc_bits;
126878d7b67SJosef Bacik 		else if (flags & BTRFS_BLOCK_GROUP_METADATA)
127878d7b67SJosef Bacik 			flags |= fs_info->avail_metadata_alloc_bits;
128878d7b67SJosef Bacik 	} while (read_seqretry(&fs_info->profiles_lock, seq));
129878d7b67SJosef Bacik 
130878d7b67SJosef Bacik 	return btrfs_reduce_alloc_profile(fs_info, flags);
131878d7b67SJosef Bacik }
132878d7b67SJosef Bacik 
13332da5386SDavid Sterba void btrfs_get_block_group(struct btrfs_block_group *cache)
1343cad1284SJosef Bacik {
13548aaeebeSJosef Bacik 	refcount_inc(&cache->refs);
1363cad1284SJosef Bacik }
1373cad1284SJosef Bacik 
13832da5386SDavid Sterba void btrfs_put_block_group(struct btrfs_block_group *cache)
1393cad1284SJosef Bacik {
14048aaeebeSJosef Bacik 	if (refcount_dec_and_test(&cache->refs)) {
1413cad1284SJosef Bacik 		WARN_ON(cache->pinned > 0);
14240cdc509SFilipe Manana 		/*
14340cdc509SFilipe Manana 		 * If there was a failure to cleanup a log tree, very likely due
14440cdc509SFilipe Manana 		 * to an IO failure on a writeback attempt of one or more of its
14540cdc509SFilipe Manana 		 * extent buffers, we could not do proper (and cheap) unaccounting
14640cdc509SFilipe Manana 		 * of their reserved space, so don't warn on reserved > 0 in that
14740cdc509SFilipe Manana 		 * case.
14840cdc509SFilipe Manana 		 */
14940cdc509SFilipe Manana 		if (!(cache->flags & BTRFS_BLOCK_GROUP_METADATA) ||
15040cdc509SFilipe Manana 		    !BTRFS_FS_LOG_CLEANUP_ERROR(cache->fs_info))
1513cad1284SJosef Bacik 			WARN_ON(cache->reserved > 0);
1523cad1284SJosef Bacik 
1533cad1284SJosef Bacik 		/*
154b0643e59SDennis Zhou 		 * A block_group shouldn't be on the discard_list anymore.
155b0643e59SDennis Zhou 		 * Remove the block_group from the discard_list to prevent us
156b0643e59SDennis Zhou 		 * from causing a panic due to NULL pointer dereference.
157b0643e59SDennis Zhou 		 */
158b0643e59SDennis Zhou 		if (WARN_ON(!list_empty(&cache->discard_list)))
159b0643e59SDennis Zhou 			btrfs_discard_cancel_work(&cache->fs_info->discard_ctl,
160b0643e59SDennis Zhou 						  cache);
161b0643e59SDennis Zhou 
162b0643e59SDennis Zhou 		/*
1633cad1284SJosef Bacik 		 * If not empty, someone is still holding mutex of
1643cad1284SJosef Bacik 		 * full_stripe_lock, which can only be released by caller.
1653cad1284SJosef Bacik 		 * And it will definitely cause use-after-free when caller
1663cad1284SJosef Bacik 		 * tries to release full stripe lock.
1673cad1284SJosef Bacik 		 *
1683cad1284SJosef Bacik 		 * No better way to resolve, but only to warn.
1693cad1284SJosef Bacik 		 */
1703cad1284SJosef Bacik 		WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
1713cad1284SJosef Bacik 		kfree(cache->free_space_ctl);
172dafc340dSNaohiro Aota 		kfree(cache->physical_map);
1733cad1284SJosef Bacik 		kfree(cache);
1743cad1284SJosef Bacik 	}
1753cad1284SJosef Bacik }
1763cad1284SJosef Bacik 
1772e405ad8SJosef Bacik /*
1784358d963SJosef Bacik  * This adds the block group to the fs_info rb tree for the block group cache
1794358d963SJosef Bacik  */
1804358d963SJosef Bacik static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
18132da5386SDavid Sterba 				       struct btrfs_block_group *block_group)
1824358d963SJosef Bacik {
1834358d963SJosef Bacik 	struct rb_node **p;
1844358d963SJosef Bacik 	struct rb_node *parent = NULL;
18532da5386SDavid Sterba 	struct btrfs_block_group *cache;
18608dddb29SFilipe Manana 	bool leftmost = true;
1874358d963SJosef Bacik 
1889afc6649SQu Wenruo 	ASSERT(block_group->length != 0);
1899afc6649SQu Wenruo 
19016b0c258SFilipe Manana 	write_lock(&info->block_group_cache_lock);
19108dddb29SFilipe Manana 	p = &info->block_group_cache_tree.rb_root.rb_node;
1924358d963SJosef Bacik 
1934358d963SJosef Bacik 	while (*p) {
1944358d963SJosef Bacik 		parent = *p;
19532da5386SDavid Sterba 		cache = rb_entry(parent, struct btrfs_block_group, cache_node);
196b3470b5dSDavid Sterba 		if (block_group->start < cache->start) {
1974358d963SJosef Bacik 			p = &(*p)->rb_left;
198b3470b5dSDavid Sterba 		} else if (block_group->start > cache->start) {
1994358d963SJosef Bacik 			p = &(*p)->rb_right;
20008dddb29SFilipe Manana 			leftmost = false;
2014358d963SJosef Bacik 		} else {
20216b0c258SFilipe Manana 			write_unlock(&info->block_group_cache_lock);
2034358d963SJosef Bacik 			return -EEXIST;
2044358d963SJosef Bacik 		}
2054358d963SJosef Bacik 	}
2064358d963SJosef Bacik 
2074358d963SJosef Bacik 	rb_link_node(&block_group->cache_node, parent, p);
20808dddb29SFilipe Manana 	rb_insert_color_cached(&block_group->cache_node,
20908dddb29SFilipe Manana 			       &info->block_group_cache_tree, leftmost);
2104358d963SJosef Bacik 
21116b0c258SFilipe Manana 	write_unlock(&info->block_group_cache_lock);
2124358d963SJosef Bacik 
2134358d963SJosef Bacik 	return 0;
2144358d963SJosef Bacik }
2154358d963SJosef Bacik 
2164358d963SJosef Bacik /*
2172e405ad8SJosef Bacik  * This will return the block group at or after bytenr if contains is 0, else
2182e405ad8SJosef Bacik  * it will return the block group that contains the bytenr
2192e405ad8SJosef Bacik  */
22032da5386SDavid Sterba static struct btrfs_block_group *block_group_cache_tree_search(
2212e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr, int contains)
2222e405ad8SJosef Bacik {
22332da5386SDavid Sterba 	struct btrfs_block_group *cache, *ret = NULL;
2242e405ad8SJosef Bacik 	struct rb_node *n;
2252e405ad8SJosef Bacik 	u64 end, start;
2262e405ad8SJosef Bacik 
22716b0c258SFilipe Manana 	read_lock(&info->block_group_cache_lock);
22808dddb29SFilipe Manana 	n = info->block_group_cache_tree.rb_root.rb_node;
2292e405ad8SJosef Bacik 
2302e405ad8SJosef Bacik 	while (n) {
23132da5386SDavid Sterba 		cache = rb_entry(n, struct btrfs_block_group, cache_node);
232b3470b5dSDavid Sterba 		end = cache->start + cache->length - 1;
233b3470b5dSDavid Sterba 		start = cache->start;
2342e405ad8SJosef Bacik 
2352e405ad8SJosef Bacik 		if (bytenr < start) {
236b3470b5dSDavid Sterba 			if (!contains && (!ret || start < ret->start))
2372e405ad8SJosef Bacik 				ret = cache;
2382e405ad8SJosef Bacik 			n = n->rb_left;
2392e405ad8SJosef Bacik 		} else if (bytenr > start) {
2402e405ad8SJosef Bacik 			if (contains && bytenr <= end) {
2412e405ad8SJosef Bacik 				ret = cache;
2422e405ad8SJosef Bacik 				break;
2432e405ad8SJosef Bacik 			}
2442e405ad8SJosef Bacik 			n = n->rb_right;
2452e405ad8SJosef Bacik 		} else {
2462e405ad8SJosef Bacik 			ret = cache;
2472e405ad8SJosef Bacik 			break;
2482e405ad8SJosef Bacik 		}
2492e405ad8SJosef Bacik 	}
25008dddb29SFilipe Manana 	if (ret)
2512e405ad8SJosef Bacik 		btrfs_get_block_group(ret);
25216b0c258SFilipe Manana 	read_unlock(&info->block_group_cache_lock);
2532e405ad8SJosef Bacik 
2542e405ad8SJosef Bacik 	return ret;
2552e405ad8SJosef Bacik }
2562e405ad8SJosef Bacik 
2572e405ad8SJosef Bacik /*
2582e405ad8SJosef Bacik  * Return the block group that starts at or after bytenr
2592e405ad8SJosef Bacik  */
26032da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_first_block_group(
2612e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr)
2622e405ad8SJosef Bacik {
2632e405ad8SJosef Bacik 	return block_group_cache_tree_search(info, bytenr, 0);
2642e405ad8SJosef Bacik }
2652e405ad8SJosef Bacik 
2662e405ad8SJosef Bacik /*
2672e405ad8SJosef Bacik  * Return the block group that contains the given bytenr
2682e405ad8SJosef Bacik  */
26932da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_block_group(
2702e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr)
2712e405ad8SJosef Bacik {
2722e405ad8SJosef Bacik 	return block_group_cache_tree_search(info, bytenr, 1);
2732e405ad8SJosef Bacik }
2742e405ad8SJosef Bacik 
27532da5386SDavid Sterba struct btrfs_block_group *btrfs_next_block_group(
27632da5386SDavid Sterba 		struct btrfs_block_group *cache)
2772e405ad8SJosef Bacik {
2782e405ad8SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
2792e405ad8SJosef Bacik 	struct rb_node *node;
2802e405ad8SJosef Bacik 
28116b0c258SFilipe Manana 	read_lock(&fs_info->block_group_cache_lock);
2822e405ad8SJosef Bacik 
2832e405ad8SJosef Bacik 	/* If our block group was removed, we need a full search. */
2842e405ad8SJosef Bacik 	if (RB_EMPTY_NODE(&cache->cache_node)) {
285b3470b5dSDavid Sterba 		const u64 next_bytenr = cache->start + cache->length;
2862e405ad8SJosef Bacik 
28716b0c258SFilipe Manana 		read_unlock(&fs_info->block_group_cache_lock);
2882e405ad8SJosef Bacik 		btrfs_put_block_group(cache);
2898b01f931SFilipe Manana 		return btrfs_lookup_first_block_group(fs_info, next_bytenr);
2902e405ad8SJosef Bacik 	}
2912e405ad8SJosef Bacik 	node = rb_next(&cache->cache_node);
2922e405ad8SJosef Bacik 	btrfs_put_block_group(cache);
2932e405ad8SJosef Bacik 	if (node) {
29432da5386SDavid Sterba 		cache = rb_entry(node, struct btrfs_block_group, cache_node);
2952e405ad8SJosef Bacik 		btrfs_get_block_group(cache);
2962e405ad8SJosef Bacik 	} else
2972e405ad8SJosef Bacik 		cache = NULL;
29816b0c258SFilipe Manana 	read_unlock(&fs_info->block_group_cache_lock);
2992e405ad8SJosef Bacik 	return cache;
3002e405ad8SJosef Bacik }
3013eeb3226SJosef Bacik 
30243dd529aSDavid Sterba /*
3032306e83eSFilipe Manana  * Check if we can do a NOCOW write for a given extent.
3042306e83eSFilipe Manana  *
3052306e83eSFilipe Manana  * @fs_info:       The filesystem information object.
3062306e83eSFilipe Manana  * @bytenr:        Logical start address of the extent.
3072306e83eSFilipe Manana  *
3082306e83eSFilipe Manana  * Check if we can do a NOCOW write for the given extent, and increments the
3092306e83eSFilipe Manana  * number of NOCOW writers in the block group that contains the extent, as long
3102306e83eSFilipe Manana  * as the block group exists and it's currently not in read-only mode.
3112306e83eSFilipe Manana  *
3122306e83eSFilipe Manana  * Returns: A non-NULL block group pointer if we can do a NOCOW write, the caller
3132306e83eSFilipe Manana  *          is responsible for calling btrfs_dec_nocow_writers() later.
3142306e83eSFilipe Manana  *
3152306e83eSFilipe Manana  *          Or NULL if we can not do a NOCOW write
3162306e83eSFilipe Manana  */
3172306e83eSFilipe Manana struct btrfs_block_group *btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info,
3182306e83eSFilipe Manana 						  u64 bytenr)
3193eeb3226SJosef Bacik {
32032da5386SDavid Sterba 	struct btrfs_block_group *bg;
3212306e83eSFilipe Manana 	bool can_nocow = true;
3223eeb3226SJosef Bacik 
3233eeb3226SJosef Bacik 	bg = btrfs_lookup_block_group(fs_info, bytenr);
3243eeb3226SJosef Bacik 	if (!bg)
3252306e83eSFilipe Manana 		return NULL;
3263eeb3226SJosef Bacik 
3273eeb3226SJosef Bacik 	spin_lock(&bg->lock);
3283eeb3226SJosef Bacik 	if (bg->ro)
3292306e83eSFilipe Manana 		can_nocow = false;
3303eeb3226SJosef Bacik 	else
3313eeb3226SJosef Bacik 		atomic_inc(&bg->nocow_writers);
3323eeb3226SJosef Bacik 	spin_unlock(&bg->lock);
3333eeb3226SJosef Bacik 
3342306e83eSFilipe Manana 	if (!can_nocow) {
3353eeb3226SJosef Bacik 		btrfs_put_block_group(bg);
3362306e83eSFilipe Manana 		return NULL;
3373eeb3226SJosef Bacik 	}
3383eeb3226SJosef Bacik 
3392306e83eSFilipe Manana 	/* No put on block group, done by btrfs_dec_nocow_writers(). */
3402306e83eSFilipe Manana 	return bg;
3412306e83eSFilipe Manana }
3423eeb3226SJosef Bacik 
34343dd529aSDavid Sterba /*
3442306e83eSFilipe Manana  * Decrement the number of NOCOW writers in a block group.
3452306e83eSFilipe Manana  *
3462306e83eSFilipe Manana  * This is meant to be called after a previous call to btrfs_inc_nocow_writers(),
3472306e83eSFilipe Manana  * and on the block group returned by that call. Typically this is called after
3482306e83eSFilipe Manana  * creating an ordered extent for a NOCOW write, to prevent races with scrub and
3492306e83eSFilipe Manana  * relocation.
3502306e83eSFilipe Manana  *
3512306e83eSFilipe Manana  * After this call, the caller should not use the block group anymore. It it wants
3522306e83eSFilipe Manana  * to use it, then it should get a reference on it before calling this function.
3532306e83eSFilipe Manana  */
3542306e83eSFilipe Manana void btrfs_dec_nocow_writers(struct btrfs_block_group *bg)
3552306e83eSFilipe Manana {
3563eeb3226SJosef Bacik 	if (atomic_dec_and_test(&bg->nocow_writers))
3573eeb3226SJosef Bacik 		wake_up_var(&bg->nocow_writers);
3582306e83eSFilipe Manana 
3592306e83eSFilipe Manana 	/* For the lookup done by a previous call to btrfs_inc_nocow_writers(). */
3603eeb3226SJosef Bacik 	btrfs_put_block_group(bg);
3613eeb3226SJosef Bacik }
3623eeb3226SJosef Bacik 
36332da5386SDavid Sterba void btrfs_wait_nocow_writers(struct btrfs_block_group *bg)
3643eeb3226SJosef Bacik {
3653eeb3226SJosef Bacik 	wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
3663eeb3226SJosef Bacik }
3673eeb3226SJosef Bacik 
3683eeb3226SJosef Bacik void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
3693eeb3226SJosef Bacik 					const u64 start)
3703eeb3226SJosef Bacik {
37132da5386SDavid Sterba 	struct btrfs_block_group *bg;
3723eeb3226SJosef Bacik 
3733eeb3226SJosef Bacik 	bg = btrfs_lookup_block_group(fs_info, start);
3743eeb3226SJosef Bacik 	ASSERT(bg);
3753eeb3226SJosef Bacik 	if (atomic_dec_and_test(&bg->reservations))
3763eeb3226SJosef Bacik 		wake_up_var(&bg->reservations);
3773eeb3226SJosef Bacik 	btrfs_put_block_group(bg);
3783eeb3226SJosef Bacik }
3793eeb3226SJosef Bacik 
38032da5386SDavid Sterba void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg)
3813eeb3226SJosef Bacik {
3823eeb3226SJosef Bacik 	struct btrfs_space_info *space_info = bg->space_info;
3833eeb3226SJosef Bacik 
3843eeb3226SJosef Bacik 	ASSERT(bg->ro);
3853eeb3226SJosef Bacik 
3863eeb3226SJosef Bacik 	if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
3873eeb3226SJosef Bacik 		return;
3883eeb3226SJosef Bacik 
3893eeb3226SJosef Bacik 	/*
3903eeb3226SJosef Bacik 	 * Our block group is read only but before we set it to read only,
3913eeb3226SJosef Bacik 	 * some task might have had allocated an extent from it already, but it
3923eeb3226SJosef Bacik 	 * has not yet created a respective ordered extent (and added it to a
3933eeb3226SJosef Bacik 	 * root's list of ordered extents).
3943eeb3226SJosef Bacik 	 * Therefore wait for any task currently allocating extents, since the
3953eeb3226SJosef Bacik 	 * block group's reservations counter is incremented while a read lock
3963eeb3226SJosef Bacik 	 * on the groups' semaphore is held and decremented after releasing
3973eeb3226SJosef Bacik 	 * the read access on that semaphore and creating the ordered extent.
3983eeb3226SJosef Bacik 	 */
3993eeb3226SJosef Bacik 	down_write(&space_info->groups_sem);
4003eeb3226SJosef Bacik 	up_write(&space_info->groups_sem);
4013eeb3226SJosef Bacik 
4023eeb3226SJosef Bacik 	wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
4033eeb3226SJosef Bacik }
4049f21246dSJosef Bacik 
4059f21246dSJosef Bacik struct btrfs_caching_control *btrfs_get_caching_control(
40632da5386SDavid Sterba 		struct btrfs_block_group *cache)
4079f21246dSJosef Bacik {
4089f21246dSJosef Bacik 	struct btrfs_caching_control *ctl;
4099f21246dSJosef Bacik 
4109f21246dSJosef Bacik 	spin_lock(&cache->lock);
4119f21246dSJosef Bacik 	if (!cache->caching_ctl) {
4129f21246dSJosef Bacik 		spin_unlock(&cache->lock);
4139f21246dSJosef Bacik 		return NULL;
4149f21246dSJosef Bacik 	}
4159f21246dSJosef Bacik 
4169f21246dSJosef Bacik 	ctl = cache->caching_ctl;
4179f21246dSJosef Bacik 	refcount_inc(&ctl->count);
4189f21246dSJosef Bacik 	spin_unlock(&cache->lock);
4199f21246dSJosef Bacik 	return ctl;
4209f21246dSJosef Bacik }
4219f21246dSJosef Bacik 
4229f21246dSJosef Bacik void btrfs_put_caching_control(struct btrfs_caching_control *ctl)
4239f21246dSJosef Bacik {
4249f21246dSJosef Bacik 	if (refcount_dec_and_test(&ctl->count))
4259f21246dSJosef Bacik 		kfree(ctl);
4269f21246dSJosef Bacik }
4279f21246dSJosef Bacik 
4289f21246dSJosef Bacik /*
4299f21246dSJosef Bacik  * When we wait for progress in the block group caching, its because our
4309f21246dSJosef Bacik  * allocation attempt failed at least once.  So, we must sleep and let some
4319f21246dSJosef Bacik  * progress happen before we try again.
4329f21246dSJosef Bacik  *
4339f21246dSJosef Bacik  * This function will sleep at least once waiting for new free space to show
4349f21246dSJosef Bacik  * up, and then it will check the block group free space numbers for our min
4359f21246dSJosef Bacik  * num_bytes.  Another option is to have it go ahead and look in the rbtree for
4369f21246dSJosef Bacik  * a free extent of a given size, but this is a good start.
4379f21246dSJosef Bacik  *
4389f21246dSJosef Bacik  * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
4399f21246dSJosef Bacik  * any of the information in this block group.
4409f21246dSJosef Bacik  */
44132da5386SDavid Sterba void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache,
4429f21246dSJosef Bacik 					   u64 num_bytes)
4439f21246dSJosef Bacik {
4449f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
4459f21246dSJosef Bacik 
4469f21246dSJosef Bacik 	caching_ctl = btrfs_get_caching_control(cache);
4479f21246dSJosef Bacik 	if (!caching_ctl)
4489f21246dSJosef Bacik 		return;
4499f21246dSJosef Bacik 
45032da5386SDavid Sterba 	wait_event(caching_ctl->wait, btrfs_block_group_done(cache) ||
4519f21246dSJosef Bacik 		   (cache->free_space_ctl->free_space >= num_bytes));
4529f21246dSJosef Bacik 
4539f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
4549f21246dSJosef Bacik }
4559f21246dSJosef Bacik 
456ced8ecf0SOmar Sandoval static int btrfs_caching_ctl_wait_done(struct btrfs_block_group *cache,
457ced8ecf0SOmar Sandoval 				       struct btrfs_caching_control *caching_ctl)
458ced8ecf0SOmar Sandoval {
459ced8ecf0SOmar Sandoval 	wait_event(caching_ctl->wait, btrfs_block_group_done(cache));
460ced8ecf0SOmar Sandoval 	return cache->cached == BTRFS_CACHE_ERROR ? -EIO : 0;
461ced8ecf0SOmar Sandoval }
462ced8ecf0SOmar Sandoval 
463ced8ecf0SOmar Sandoval static int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache)
4649f21246dSJosef Bacik {
4659f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
466ced8ecf0SOmar Sandoval 	int ret;
4679f21246dSJosef Bacik 
4689f21246dSJosef Bacik 	caching_ctl = btrfs_get_caching_control(cache);
4699f21246dSJosef Bacik 	if (!caching_ctl)
4709f21246dSJosef Bacik 		return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
471ced8ecf0SOmar Sandoval 	ret = btrfs_caching_ctl_wait_done(cache, caching_ctl);
4729f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
4739f21246dSJosef Bacik 	return ret;
4749f21246dSJosef Bacik }
4759f21246dSJosef Bacik 
4769f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
47732da5386SDavid Sterba static void fragment_free_space(struct btrfs_block_group *block_group)
4789f21246dSJosef Bacik {
4799f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
480b3470b5dSDavid Sterba 	u64 start = block_group->start;
481b3470b5dSDavid Sterba 	u64 len = block_group->length;
4829f21246dSJosef Bacik 	u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
4839f21246dSJosef Bacik 		fs_info->nodesize : fs_info->sectorsize;
4849f21246dSJosef Bacik 	u64 step = chunk << 1;
4859f21246dSJosef Bacik 
4869f21246dSJosef Bacik 	while (len > chunk) {
4879f21246dSJosef Bacik 		btrfs_remove_free_space(block_group, start, chunk);
4889f21246dSJosef Bacik 		start += step;
4899f21246dSJosef Bacik 		if (len < step)
4909f21246dSJosef Bacik 			len = 0;
4919f21246dSJosef Bacik 		else
4929f21246dSJosef Bacik 			len -= step;
4939f21246dSJosef Bacik 	}
4949f21246dSJosef Bacik }
4959f21246dSJosef Bacik #endif
4969f21246dSJosef Bacik 
4979f21246dSJosef Bacik /*
4989f21246dSJosef Bacik  * This is only called by btrfs_cache_block_group, since we could have freed
4999f21246dSJosef Bacik  * extents we need to check the pinned_extents for any extents that can't be
5009f21246dSJosef Bacik  * used yet since their free space will be released as soon as the transaction
5019f21246dSJosef Bacik  * commits.
5029f21246dSJosef Bacik  */
50332da5386SDavid Sterba u64 add_new_free_space(struct btrfs_block_group *block_group, u64 start, u64 end)
5049f21246dSJosef Bacik {
5059f21246dSJosef Bacik 	struct btrfs_fs_info *info = block_group->fs_info;
5069f21246dSJosef Bacik 	u64 extent_start, extent_end, size, total_added = 0;
5079f21246dSJosef Bacik 	int ret;
5089f21246dSJosef Bacik 
5099f21246dSJosef Bacik 	while (start < end) {
510fe119a6eSNikolay Borisov 		ret = find_first_extent_bit(&info->excluded_extents, start,
5119f21246dSJosef Bacik 					    &extent_start, &extent_end,
5129f21246dSJosef Bacik 					    EXTENT_DIRTY | EXTENT_UPTODATE,
5139f21246dSJosef Bacik 					    NULL);
5149f21246dSJosef Bacik 		if (ret)
5159f21246dSJosef Bacik 			break;
5169f21246dSJosef Bacik 
5179f21246dSJosef Bacik 		if (extent_start <= start) {
5189f21246dSJosef Bacik 			start = extent_end + 1;
5199f21246dSJosef Bacik 		} else if (extent_start > start && extent_start < end) {
5209f21246dSJosef Bacik 			size = extent_start - start;
5219f21246dSJosef Bacik 			total_added += size;
522b0643e59SDennis Zhou 			ret = btrfs_add_free_space_async_trimmed(block_group,
523b0643e59SDennis Zhou 								 start, size);
5249f21246dSJosef Bacik 			BUG_ON(ret); /* -ENOMEM or logic error */
5259f21246dSJosef Bacik 			start = extent_end + 1;
5269f21246dSJosef Bacik 		} else {
5279f21246dSJosef Bacik 			break;
5289f21246dSJosef Bacik 		}
5299f21246dSJosef Bacik 	}
5309f21246dSJosef Bacik 
5319f21246dSJosef Bacik 	if (start < end) {
5329f21246dSJosef Bacik 		size = end - start;
5339f21246dSJosef Bacik 		total_added += size;
534b0643e59SDennis Zhou 		ret = btrfs_add_free_space_async_trimmed(block_group, start,
535b0643e59SDennis Zhou 							 size);
5369f21246dSJosef Bacik 		BUG_ON(ret); /* -ENOMEM or logic error */
5379f21246dSJosef Bacik 	}
5389f21246dSJosef Bacik 
5399f21246dSJosef Bacik 	return total_added;
5409f21246dSJosef Bacik }
5419f21246dSJosef Bacik 
5429f21246dSJosef Bacik static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
5439f21246dSJosef Bacik {
54432da5386SDavid Sterba 	struct btrfs_block_group *block_group = caching_ctl->block_group;
5459f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
54629cbcf40SJosef Bacik 	struct btrfs_root *extent_root;
5479f21246dSJosef Bacik 	struct btrfs_path *path;
5489f21246dSJosef Bacik 	struct extent_buffer *leaf;
5499f21246dSJosef Bacik 	struct btrfs_key key;
5509f21246dSJosef Bacik 	u64 total_found = 0;
5519f21246dSJosef Bacik 	u64 last = 0;
5529f21246dSJosef Bacik 	u32 nritems;
5539f21246dSJosef Bacik 	int ret;
5549f21246dSJosef Bacik 	bool wakeup = true;
5559f21246dSJosef Bacik 
5569f21246dSJosef Bacik 	path = btrfs_alloc_path();
5579f21246dSJosef Bacik 	if (!path)
5589f21246dSJosef Bacik 		return -ENOMEM;
5599f21246dSJosef Bacik 
560b3470b5dSDavid Sterba 	last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET);
56129cbcf40SJosef Bacik 	extent_root = btrfs_extent_root(fs_info, last);
5629f21246dSJosef Bacik 
5639f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
5649f21246dSJosef Bacik 	/*
5659f21246dSJosef Bacik 	 * If we're fragmenting we don't want to make anybody think we can
5669f21246dSJosef Bacik 	 * allocate from this block group until we've had a chance to fragment
5679f21246dSJosef Bacik 	 * the free space.
5689f21246dSJosef Bacik 	 */
5699f21246dSJosef Bacik 	if (btrfs_should_fragment_free_space(block_group))
5709f21246dSJosef Bacik 		wakeup = false;
5719f21246dSJosef Bacik #endif
5729f21246dSJosef Bacik 	/*
5739f21246dSJosef Bacik 	 * We don't want to deadlock with somebody trying to allocate a new
5749f21246dSJosef Bacik 	 * extent for the extent root while also trying to search the extent
5759f21246dSJosef Bacik 	 * root to add free space.  So we skip locking and search the commit
5769f21246dSJosef Bacik 	 * root, since its read-only
5779f21246dSJosef Bacik 	 */
5789f21246dSJosef Bacik 	path->skip_locking = 1;
5799f21246dSJosef Bacik 	path->search_commit_root = 1;
5809f21246dSJosef Bacik 	path->reada = READA_FORWARD;
5819f21246dSJosef Bacik 
5829f21246dSJosef Bacik 	key.objectid = last;
5839f21246dSJosef Bacik 	key.offset = 0;
5849f21246dSJosef Bacik 	key.type = BTRFS_EXTENT_ITEM_KEY;
5859f21246dSJosef Bacik 
5869f21246dSJosef Bacik next:
5879f21246dSJosef Bacik 	ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
5889f21246dSJosef Bacik 	if (ret < 0)
5899f21246dSJosef Bacik 		goto out;
5909f21246dSJosef Bacik 
5919f21246dSJosef Bacik 	leaf = path->nodes[0];
5929f21246dSJosef Bacik 	nritems = btrfs_header_nritems(leaf);
5939f21246dSJosef Bacik 
5949f21246dSJosef Bacik 	while (1) {
5959f21246dSJosef Bacik 		if (btrfs_fs_closing(fs_info) > 1) {
5969f21246dSJosef Bacik 			last = (u64)-1;
5979f21246dSJosef Bacik 			break;
5989f21246dSJosef Bacik 		}
5999f21246dSJosef Bacik 
6009f21246dSJosef Bacik 		if (path->slots[0] < nritems) {
6019f21246dSJosef Bacik 			btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6029f21246dSJosef Bacik 		} else {
6039f21246dSJosef Bacik 			ret = btrfs_find_next_key(extent_root, path, &key, 0, 0);
6049f21246dSJosef Bacik 			if (ret)
6059f21246dSJosef Bacik 				break;
6069f21246dSJosef Bacik 
6079f21246dSJosef Bacik 			if (need_resched() ||
6089f21246dSJosef Bacik 			    rwsem_is_contended(&fs_info->commit_root_sem)) {
6099f21246dSJosef Bacik 				btrfs_release_path(path);
6109f21246dSJosef Bacik 				up_read(&fs_info->commit_root_sem);
6119f21246dSJosef Bacik 				mutex_unlock(&caching_ctl->mutex);
6129f21246dSJosef Bacik 				cond_resched();
6139f21246dSJosef Bacik 				mutex_lock(&caching_ctl->mutex);
6149f21246dSJosef Bacik 				down_read(&fs_info->commit_root_sem);
6159f21246dSJosef Bacik 				goto next;
6169f21246dSJosef Bacik 			}
6179f21246dSJosef Bacik 
6189f21246dSJosef Bacik 			ret = btrfs_next_leaf(extent_root, path);
6199f21246dSJosef Bacik 			if (ret < 0)
6209f21246dSJosef Bacik 				goto out;
6219f21246dSJosef Bacik 			if (ret)
6229f21246dSJosef Bacik 				break;
6239f21246dSJosef Bacik 			leaf = path->nodes[0];
6249f21246dSJosef Bacik 			nritems = btrfs_header_nritems(leaf);
6259f21246dSJosef Bacik 			continue;
6269f21246dSJosef Bacik 		}
6279f21246dSJosef Bacik 
6289f21246dSJosef Bacik 		if (key.objectid < last) {
6299f21246dSJosef Bacik 			key.objectid = last;
6309f21246dSJosef Bacik 			key.offset = 0;
6319f21246dSJosef Bacik 			key.type = BTRFS_EXTENT_ITEM_KEY;
6329f21246dSJosef Bacik 			btrfs_release_path(path);
6339f21246dSJosef Bacik 			goto next;
6349f21246dSJosef Bacik 		}
6359f21246dSJosef Bacik 
636b3470b5dSDavid Sterba 		if (key.objectid < block_group->start) {
6379f21246dSJosef Bacik 			path->slots[0]++;
6389f21246dSJosef Bacik 			continue;
6399f21246dSJosef Bacik 		}
6409f21246dSJosef Bacik 
641b3470b5dSDavid Sterba 		if (key.objectid >= block_group->start + block_group->length)
6429f21246dSJosef Bacik 			break;
6439f21246dSJosef Bacik 
6449f21246dSJosef Bacik 		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
6459f21246dSJosef Bacik 		    key.type == BTRFS_METADATA_ITEM_KEY) {
6469f21246dSJosef Bacik 			total_found += add_new_free_space(block_group, last,
6479f21246dSJosef Bacik 							  key.objectid);
6489f21246dSJosef Bacik 			if (key.type == BTRFS_METADATA_ITEM_KEY)
6499f21246dSJosef Bacik 				last = key.objectid +
6509f21246dSJosef Bacik 					fs_info->nodesize;
6519f21246dSJosef Bacik 			else
6529f21246dSJosef Bacik 				last = key.objectid + key.offset;
6539f21246dSJosef Bacik 
6549f21246dSJosef Bacik 			if (total_found > CACHING_CTL_WAKE_UP) {
6559f21246dSJosef Bacik 				total_found = 0;
6569f21246dSJosef Bacik 				if (wakeup)
6579f21246dSJosef Bacik 					wake_up(&caching_ctl->wait);
6589f21246dSJosef Bacik 			}
6599f21246dSJosef Bacik 		}
6609f21246dSJosef Bacik 		path->slots[0]++;
6619f21246dSJosef Bacik 	}
6629f21246dSJosef Bacik 	ret = 0;
6639f21246dSJosef Bacik 
6649f21246dSJosef Bacik 	total_found += add_new_free_space(block_group, last,
665b3470b5dSDavid Sterba 				block_group->start + block_group->length);
6669f21246dSJosef Bacik 
6679f21246dSJosef Bacik out:
6689f21246dSJosef Bacik 	btrfs_free_path(path);
6699f21246dSJosef Bacik 	return ret;
6709f21246dSJosef Bacik }
6719f21246dSJosef Bacik 
6729f21246dSJosef Bacik static noinline void caching_thread(struct btrfs_work *work)
6739f21246dSJosef Bacik {
67432da5386SDavid Sterba 	struct btrfs_block_group *block_group;
6759f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info;
6769f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
6779f21246dSJosef Bacik 	int ret;
6789f21246dSJosef Bacik 
6799f21246dSJosef Bacik 	caching_ctl = container_of(work, struct btrfs_caching_control, work);
6809f21246dSJosef Bacik 	block_group = caching_ctl->block_group;
6819f21246dSJosef Bacik 	fs_info = block_group->fs_info;
6829f21246dSJosef Bacik 
6839f21246dSJosef Bacik 	mutex_lock(&caching_ctl->mutex);
6849f21246dSJosef Bacik 	down_read(&fs_info->commit_root_sem);
6859f21246dSJosef Bacik 
686e747853cSJosef Bacik 	if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
687e747853cSJosef Bacik 		ret = load_free_space_cache(block_group);
688e747853cSJosef Bacik 		if (ret == 1) {
689e747853cSJosef Bacik 			ret = 0;
690e747853cSJosef Bacik 			goto done;
691e747853cSJosef Bacik 		}
692e747853cSJosef Bacik 
693e747853cSJosef Bacik 		/*
694e747853cSJosef Bacik 		 * We failed to load the space cache, set ourselves to
695e747853cSJosef Bacik 		 * CACHE_STARTED and carry on.
696e747853cSJosef Bacik 		 */
697e747853cSJosef Bacik 		spin_lock(&block_group->lock);
698e747853cSJosef Bacik 		block_group->cached = BTRFS_CACHE_STARTED;
699e747853cSJosef Bacik 		spin_unlock(&block_group->lock);
700e747853cSJosef Bacik 		wake_up(&caching_ctl->wait);
701e747853cSJosef Bacik 	}
702e747853cSJosef Bacik 
7032f96e402SJosef Bacik 	/*
7042f96e402SJosef Bacik 	 * If we are in the transaction that populated the free space tree we
7052f96e402SJosef Bacik 	 * can't actually cache from the free space tree as our commit root and
7062f96e402SJosef Bacik 	 * real root are the same, so we could change the contents of the blocks
7072f96e402SJosef Bacik 	 * while caching.  Instead do the slow caching in this case, and after
7082f96e402SJosef Bacik 	 * the transaction has committed we will be safe.
7092f96e402SJosef Bacik 	 */
7102f96e402SJosef Bacik 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
7112f96e402SJosef Bacik 	    !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags)))
7129f21246dSJosef Bacik 		ret = load_free_space_tree(caching_ctl);
7139f21246dSJosef Bacik 	else
7149f21246dSJosef Bacik 		ret = load_extent_tree_free(caching_ctl);
715e747853cSJosef Bacik done:
7169f21246dSJosef Bacik 	spin_lock(&block_group->lock);
7179f21246dSJosef Bacik 	block_group->caching_ctl = NULL;
7189f21246dSJosef Bacik 	block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
7199f21246dSJosef Bacik 	spin_unlock(&block_group->lock);
7209f21246dSJosef Bacik 
7219f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
7229f21246dSJosef Bacik 	if (btrfs_should_fragment_free_space(block_group)) {
7239f21246dSJosef Bacik 		u64 bytes_used;
7249f21246dSJosef Bacik 
7259f21246dSJosef Bacik 		spin_lock(&block_group->space_info->lock);
7269f21246dSJosef Bacik 		spin_lock(&block_group->lock);
727b3470b5dSDavid Sterba 		bytes_used = block_group->length - block_group->used;
7289f21246dSJosef Bacik 		block_group->space_info->bytes_used += bytes_used >> 1;
7299f21246dSJosef Bacik 		spin_unlock(&block_group->lock);
7309f21246dSJosef Bacik 		spin_unlock(&block_group->space_info->lock);
731e11c0406SJosef Bacik 		fragment_free_space(block_group);
7329f21246dSJosef Bacik 	}
7339f21246dSJosef Bacik #endif
7349f21246dSJosef Bacik 
7359f21246dSJosef Bacik 	up_read(&fs_info->commit_root_sem);
7369f21246dSJosef Bacik 	btrfs_free_excluded_extents(block_group);
7379f21246dSJosef Bacik 	mutex_unlock(&caching_ctl->mutex);
7389f21246dSJosef Bacik 
7399f21246dSJosef Bacik 	wake_up(&caching_ctl->wait);
7409f21246dSJosef Bacik 
7419f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
7429f21246dSJosef Bacik 	btrfs_put_block_group(block_group);
7439f21246dSJosef Bacik }
7449f21246dSJosef Bacik 
745ced8ecf0SOmar Sandoval int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait)
7469f21246dSJosef Bacik {
7479f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
748e747853cSJosef Bacik 	struct btrfs_caching_control *caching_ctl = NULL;
7499f21246dSJosef Bacik 	int ret = 0;
7509f21246dSJosef Bacik 
7512eda5708SNaohiro Aota 	/* Allocator for zoned filesystems does not use the cache at all */
7522eda5708SNaohiro Aota 	if (btrfs_is_zoned(fs_info))
7532eda5708SNaohiro Aota 		return 0;
7542eda5708SNaohiro Aota 
7559f21246dSJosef Bacik 	caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
7569f21246dSJosef Bacik 	if (!caching_ctl)
7579f21246dSJosef Bacik 		return -ENOMEM;
7589f21246dSJosef Bacik 
7599f21246dSJosef Bacik 	INIT_LIST_HEAD(&caching_ctl->list);
7609f21246dSJosef Bacik 	mutex_init(&caching_ctl->mutex);
7619f21246dSJosef Bacik 	init_waitqueue_head(&caching_ctl->wait);
7629f21246dSJosef Bacik 	caching_ctl->block_group = cache;
763e747853cSJosef Bacik 	refcount_set(&caching_ctl->count, 2);
764a0cac0ecSOmar Sandoval 	btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
7659f21246dSJosef Bacik 
7669f21246dSJosef Bacik 	spin_lock(&cache->lock);
7679f21246dSJosef Bacik 	if (cache->cached != BTRFS_CACHE_NO) {
7689f21246dSJosef Bacik 		kfree(caching_ctl);
769e747853cSJosef Bacik 
770e747853cSJosef Bacik 		caching_ctl = cache->caching_ctl;
771e747853cSJosef Bacik 		if (caching_ctl)
772e747853cSJosef Bacik 			refcount_inc(&caching_ctl->count);
773e747853cSJosef Bacik 		spin_unlock(&cache->lock);
774e747853cSJosef Bacik 		goto out;
7759f21246dSJosef Bacik 	}
7769f21246dSJosef Bacik 	WARN_ON(cache->caching_ctl);
7779f21246dSJosef Bacik 	cache->caching_ctl = caching_ctl;
7789f21246dSJosef Bacik 	cache->cached = BTRFS_CACHE_STARTED;
7799f21246dSJosef Bacik 	spin_unlock(&cache->lock);
7809f21246dSJosef Bacik 
78116b0c258SFilipe Manana 	write_lock(&fs_info->block_group_cache_lock);
7829f21246dSJosef Bacik 	refcount_inc(&caching_ctl->count);
7839f21246dSJosef Bacik 	list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
78416b0c258SFilipe Manana 	write_unlock(&fs_info->block_group_cache_lock);
7859f21246dSJosef Bacik 
7869f21246dSJosef Bacik 	btrfs_get_block_group(cache);
7879f21246dSJosef Bacik 
7889f21246dSJosef Bacik 	btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
789e747853cSJosef Bacik out:
790ced8ecf0SOmar Sandoval 	if (wait && caching_ctl)
791ced8ecf0SOmar Sandoval 		ret = btrfs_caching_ctl_wait_done(cache, caching_ctl);
792e747853cSJosef Bacik 	if (caching_ctl)
793e747853cSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
7949f21246dSJosef Bacik 
7959f21246dSJosef Bacik 	return ret;
7969f21246dSJosef Bacik }
797e3e0520bSJosef Bacik 
798e3e0520bSJosef Bacik static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
799e3e0520bSJosef Bacik {
800e3e0520bSJosef Bacik 	u64 extra_flags = chunk_to_extended(flags) &
801e3e0520bSJosef Bacik 				BTRFS_EXTENDED_PROFILE_MASK;
802e3e0520bSJosef Bacik 
803e3e0520bSJosef Bacik 	write_seqlock(&fs_info->profiles_lock);
804e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA)
805e3e0520bSJosef Bacik 		fs_info->avail_data_alloc_bits &= ~extra_flags;
806e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_METADATA)
807e3e0520bSJosef Bacik 		fs_info->avail_metadata_alloc_bits &= ~extra_flags;
808e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
809e3e0520bSJosef Bacik 		fs_info->avail_system_alloc_bits &= ~extra_flags;
810e3e0520bSJosef Bacik 	write_sequnlock(&fs_info->profiles_lock);
811e3e0520bSJosef Bacik }
812e3e0520bSJosef Bacik 
813e3e0520bSJosef Bacik /*
814e3e0520bSJosef Bacik  * Clear incompat bits for the following feature(s):
815e3e0520bSJosef Bacik  *
816e3e0520bSJosef Bacik  * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
817e3e0520bSJosef Bacik  *            in the whole filesystem
8189c907446SDavid Sterba  *
8199c907446SDavid Sterba  * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
820e3e0520bSJosef Bacik  */
821e3e0520bSJosef Bacik static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags)
822e3e0520bSJosef Bacik {
8239c907446SDavid Sterba 	bool found_raid56 = false;
8249c907446SDavid Sterba 	bool found_raid1c34 = false;
8259c907446SDavid Sterba 
8269c907446SDavid Sterba 	if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) ||
8279c907446SDavid Sterba 	    (flags & BTRFS_BLOCK_GROUP_RAID1C3) ||
8289c907446SDavid Sterba 	    (flags & BTRFS_BLOCK_GROUP_RAID1C4)) {
829e3e0520bSJosef Bacik 		struct list_head *head = &fs_info->space_info;
830e3e0520bSJosef Bacik 		struct btrfs_space_info *sinfo;
831e3e0520bSJosef Bacik 
832e3e0520bSJosef Bacik 		list_for_each_entry_rcu(sinfo, head, list) {
833e3e0520bSJosef Bacik 			down_read(&sinfo->groups_sem);
834e3e0520bSJosef Bacik 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5]))
8359c907446SDavid Sterba 				found_raid56 = true;
836e3e0520bSJosef Bacik 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6]))
8379c907446SDavid Sterba 				found_raid56 = true;
8389c907446SDavid Sterba 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3]))
8399c907446SDavid Sterba 				found_raid1c34 = true;
8409c907446SDavid Sterba 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4]))
8419c907446SDavid Sterba 				found_raid1c34 = true;
842e3e0520bSJosef Bacik 			up_read(&sinfo->groups_sem);
843e3e0520bSJosef Bacik 		}
844d8e6fd5cSFilipe Manana 		if (!found_raid56)
845e3e0520bSJosef Bacik 			btrfs_clear_fs_incompat(fs_info, RAID56);
846d8e6fd5cSFilipe Manana 		if (!found_raid1c34)
8479c907446SDavid Sterba 			btrfs_clear_fs_incompat(fs_info, RAID1C34);
848e3e0520bSJosef Bacik 	}
849e3e0520bSJosef Bacik }
850e3e0520bSJosef Bacik 
8517357623aSQu Wenruo static int remove_block_group_item(struct btrfs_trans_handle *trans,
8527357623aSQu Wenruo 				   struct btrfs_path *path,
8537357623aSQu Wenruo 				   struct btrfs_block_group *block_group)
8547357623aSQu Wenruo {
8557357623aSQu Wenruo 	struct btrfs_fs_info *fs_info = trans->fs_info;
8567357623aSQu Wenruo 	struct btrfs_root *root;
8577357623aSQu Wenruo 	struct btrfs_key key;
8587357623aSQu Wenruo 	int ret;
8597357623aSQu Wenruo 
860dfe8aec4SJosef Bacik 	root = btrfs_block_group_root(fs_info);
8617357623aSQu Wenruo 	key.objectid = block_group->start;
8627357623aSQu Wenruo 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8637357623aSQu Wenruo 	key.offset = block_group->length;
8647357623aSQu Wenruo 
8657357623aSQu Wenruo 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8667357623aSQu Wenruo 	if (ret > 0)
8677357623aSQu Wenruo 		ret = -ENOENT;
8687357623aSQu Wenruo 	if (ret < 0)
8697357623aSQu Wenruo 		return ret;
8707357623aSQu Wenruo 
8717357623aSQu Wenruo 	ret = btrfs_del_item(trans, root, path);
8727357623aSQu Wenruo 	return ret;
8737357623aSQu Wenruo }
8747357623aSQu Wenruo 
875e3e0520bSJosef Bacik int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
876e3e0520bSJosef Bacik 			     u64 group_start, struct extent_map *em)
877e3e0520bSJosef Bacik {
878e3e0520bSJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
879e3e0520bSJosef Bacik 	struct btrfs_path *path;
88032da5386SDavid Sterba 	struct btrfs_block_group *block_group;
881e3e0520bSJosef Bacik 	struct btrfs_free_cluster *cluster;
882e3e0520bSJosef Bacik 	struct inode *inode;
883e3e0520bSJosef Bacik 	struct kobject *kobj = NULL;
884e3e0520bSJosef Bacik 	int ret;
885e3e0520bSJosef Bacik 	int index;
886e3e0520bSJosef Bacik 	int factor;
887e3e0520bSJosef Bacik 	struct btrfs_caching_control *caching_ctl = NULL;
888e3e0520bSJosef Bacik 	bool remove_em;
889e3e0520bSJosef Bacik 	bool remove_rsv = false;
890e3e0520bSJosef Bacik 
891e3e0520bSJosef Bacik 	block_group = btrfs_lookup_block_group(fs_info, group_start);
892e3e0520bSJosef Bacik 	BUG_ON(!block_group);
893e3e0520bSJosef Bacik 	BUG_ON(!block_group->ro);
894e3e0520bSJosef Bacik 
895e3e0520bSJosef Bacik 	trace_btrfs_remove_block_group(block_group);
896e3e0520bSJosef Bacik 	/*
897e3e0520bSJosef Bacik 	 * Free the reserved super bytes from this block group before
898e3e0520bSJosef Bacik 	 * remove it.
899e3e0520bSJosef Bacik 	 */
900e3e0520bSJosef Bacik 	btrfs_free_excluded_extents(block_group);
901b3470b5dSDavid Sterba 	btrfs_free_ref_tree_range(fs_info, block_group->start,
902b3470b5dSDavid Sterba 				  block_group->length);
903e3e0520bSJosef Bacik 
904e3e0520bSJosef Bacik 	index = btrfs_bg_flags_to_raid_index(block_group->flags);
905e3e0520bSJosef Bacik 	factor = btrfs_bg_type_to_factor(block_group->flags);
906e3e0520bSJosef Bacik 
907e3e0520bSJosef Bacik 	/* make sure this block group isn't part of an allocation cluster */
908e3e0520bSJosef Bacik 	cluster = &fs_info->data_alloc_cluster;
909e3e0520bSJosef Bacik 	spin_lock(&cluster->refill_lock);
910e3e0520bSJosef Bacik 	btrfs_return_cluster_to_free_space(block_group, cluster);
911e3e0520bSJosef Bacik 	spin_unlock(&cluster->refill_lock);
912e3e0520bSJosef Bacik 
913e3e0520bSJosef Bacik 	/*
914e3e0520bSJosef Bacik 	 * make sure this block group isn't part of a metadata
915e3e0520bSJosef Bacik 	 * allocation cluster
916e3e0520bSJosef Bacik 	 */
917e3e0520bSJosef Bacik 	cluster = &fs_info->meta_alloc_cluster;
918e3e0520bSJosef Bacik 	spin_lock(&cluster->refill_lock);
919e3e0520bSJosef Bacik 	btrfs_return_cluster_to_free_space(block_group, cluster);
920e3e0520bSJosef Bacik 	spin_unlock(&cluster->refill_lock);
921e3e0520bSJosef Bacik 
92240ab3be1SNaohiro Aota 	btrfs_clear_treelog_bg(block_group);
923c2707a25SJohannes Thumshirn 	btrfs_clear_data_reloc_bg(block_group);
92440ab3be1SNaohiro Aota 
925e3e0520bSJosef Bacik 	path = btrfs_alloc_path();
926e3e0520bSJosef Bacik 	if (!path) {
927e3e0520bSJosef Bacik 		ret = -ENOMEM;
9289fecd132SFilipe Manana 		goto out;
929e3e0520bSJosef Bacik 	}
930e3e0520bSJosef Bacik 
931e3e0520bSJosef Bacik 	/*
932e3e0520bSJosef Bacik 	 * get the inode first so any iput calls done for the io_list
933e3e0520bSJosef Bacik 	 * aren't the final iput (no unlinks allowed now)
934e3e0520bSJosef Bacik 	 */
935e3e0520bSJosef Bacik 	inode = lookup_free_space_inode(block_group, path);
936e3e0520bSJosef Bacik 
937e3e0520bSJosef Bacik 	mutex_lock(&trans->transaction->cache_write_mutex);
938e3e0520bSJosef Bacik 	/*
939e3e0520bSJosef Bacik 	 * Make sure our free space cache IO is done before removing the
940e3e0520bSJosef Bacik 	 * free space inode
941e3e0520bSJosef Bacik 	 */
942e3e0520bSJosef Bacik 	spin_lock(&trans->transaction->dirty_bgs_lock);
943e3e0520bSJosef Bacik 	if (!list_empty(&block_group->io_list)) {
944e3e0520bSJosef Bacik 		list_del_init(&block_group->io_list);
945e3e0520bSJosef Bacik 
946e3e0520bSJosef Bacik 		WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
947e3e0520bSJosef Bacik 
948e3e0520bSJosef Bacik 		spin_unlock(&trans->transaction->dirty_bgs_lock);
949e3e0520bSJosef Bacik 		btrfs_wait_cache_io(trans, block_group, path);
950e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
951e3e0520bSJosef Bacik 		spin_lock(&trans->transaction->dirty_bgs_lock);
952e3e0520bSJosef Bacik 	}
953e3e0520bSJosef Bacik 
954e3e0520bSJosef Bacik 	if (!list_empty(&block_group->dirty_list)) {
955e3e0520bSJosef Bacik 		list_del_init(&block_group->dirty_list);
956e3e0520bSJosef Bacik 		remove_rsv = true;
957e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
958e3e0520bSJosef Bacik 	}
959e3e0520bSJosef Bacik 	spin_unlock(&trans->transaction->dirty_bgs_lock);
960e3e0520bSJosef Bacik 	mutex_unlock(&trans->transaction->cache_write_mutex);
961e3e0520bSJosef Bacik 
96236b216c8SBoris Burkov 	ret = btrfs_remove_free_space_inode(trans, inode, block_group);
963e3e0520bSJosef Bacik 	if (ret)
9649fecd132SFilipe Manana 		goto out;
965e3e0520bSJosef Bacik 
96616b0c258SFilipe Manana 	write_lock(&fs_info->block_group_cache_lock);
96708dddb29SFilipe Manana 	rb_erase_cached(&block_group->cache_node,
968e3e0520bSJosef Bacik 			&fs_info->block_group_cache_tree);
969e3e0520bSJosef Bacik 	RB_CLEAR_NODE(&block_group->cache_node);
970e3e0520bSJosef Bacik 
9719fecd132SFilipe Manana 	/* Once for the block groups rbtree */
9729fecd132SFilipe Manana 	btrfs_put_block_group(block_group);
9739fecd132SFilipe Manana 
97416b0c258SFilipe Manana 	write_unlock(&fs_info->block_group_cache_lock);
975e3e0520bSJosef Bacik 
976e3e0520bSJosef Bacik 	down_write(&block_group->space_info->groups_sem);
977e3e0520bSJosef Bacik 	/*
978e3e0520bSJosef Bacik 	 * we must use list_del_init so people can check to see if they
979e3e0520bSJosef Bacik 	 * are still on the list after taking the semaphore
980e3e0520bSJosef Bacik 	 */
981e3e0520bSJosef Bacik 	list_del_init(&block_group->list);
982e3e0520bSJosef Bacik 	if (list_empty(&block_group->space_info->block_groups[index])) {
983e3e0520bSJosef Bacik 		kobj = block_group->space_info->block_group_kobjs[index];
984e3e0520bSJosef Bacik 		block_group->space_info->block_group_kobjs[index] = NULL;
985e3e0520bSJosef Bacik 		clear_avail_alloc_bits(fs_info, block_group->flags);
986e3e0520bSJosef Bacik 	}
987e3e0520bSJosef Bacik 	up_write(&block_group->space_info->groups_sem);
988e3e0520bSJosef Bacik 	clear_incompat_bg_bits(fs_info, block_group->flags);
989e3e0520bSJosef Bacik 	if (kobj) {
990e3e0520bSJosef Bacik 		kobject_del(kobj);
991e3e0520bSJosef Bacik 		kobject_put(kobj);
992e3e0520bSJosef Bacik 	}
993e3e0520bSJosef Bacik 
994e3e0520bSJosef Bacik 	if (block_group->cached == BTRFS_CACHE_STARTED)
995e3e0520bSJosef Bacik 		btrfs_wait_block_group_cache_done(block_group);
9967b9c293bSJosef Bacik 
99716b0c258SFilipe Manana 	write_lock(&fs_info->block_group_cache_lock);
9987b9c293bSJosef Bacik 	caching_ctl = btrfs_get_caching_control(block_group);
999e3e0520bSJosef Bacik 	if (!caching_ctl) {
1000e3e0520bSJosef Bacik 		struct btrfs_caching_control *ctl;
1001e3e0520bSJosef Bacik 
10027b9c293bSJosef Bacik 		list_for_each_entry(ctl, &fs_info->caching_block_groups, list) {
1003e3e0520bSJosef Bacik 			if (ctl->block_group == block_group) {
1004e3e0520bSJosef Bacik 				caching_ctl = ctl;
1005e3e0520bSJosef Bacik 				refcount_inc(&caching_ctl->count);
1006e3e0520bSJosef Bacik 				break;
1007e3e0520bSJosef Bacik 			}
1008e3e0520bSJosef Bacik 		}
10097b9c293bSJosef Bacik 	}
1010e3e0520bSJosef Bacik 	if (caching_ctl)
1011e3e0520bSJosef Bacik 		list_del_init(&caching_ctl->list);
101216b0c258SFilipe Manana 	write_unlock(&fs_info->block_group_cache_lock);
10137b9c293bSJosef Bacik 
1014e3e0520bSJosef Bacik 	if (caching_ctl) {
1015e3e0520bSJosef Bacik 		/* Once for the caching bgs list and once for us. */
1016e3e0520bSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
1017e3e0520bSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
1018e3e0520bSJosef Bacik 	}
1019e3e0520bSJosef Bacik 
1020e3e0520bSJosef Bacik 	spin_lock(&trans->transaction->dirty_bgs_lock);
1021e3e0520bSJosef Bacik 	WARN_ON(!list_empty(&block_group->dirty_list));
1022e3e0520bSJosef Bacik 	WARN_ON(!list_empty(&block_group->io_list));
1023e3e0520bSJosef Bacik 	spin_unlock(&trans->transaction->dirty_bgs_lock);
1024e3e0520bSJosef Bacik 
1025e3e0520bSJosef Bacik 	btrfs_remove_free_space_cache(block_group);
1026e3e0520bSJosef Bacik 
1027e3e0520bSJosef Bacik 	spin_lock(&block_group->space_info->lock);
1028e3e0520bSJosef Bacik 	list_del_init(&block_group->ro_list);
1029e3e0520bSJosef Bacik 
1030e3e0520bSJosef Bacik 	if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
1031e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->total_bytes
1032b3470b5dSDavid Sterba 			< block_group->length);
1033e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->bytes_readonly
1034169e0da9SNaohiro Aota 			< block_group->length - block_group->zone_unusable);
1035169e0da9SNaohiro Aota 		WARN_ON(block_group->space_info->bytes_zone_unusable
1036169e0da9SNaohiro Aota 			< block_group->zone_unusable);
1037e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->disk_total
1038b3470b5dSDavid Sterba 			< block_group->length * factor);
10393349b57fSJosef Bacik 		WARN_ON(test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
10403349b57fSJosef Bacik 				 &block_group->runtime_flags) &&
10416a921de5SNaohiro Aota 			block_group->space_info->active_total_bytes
10426a921de5SNaohiro Aota 			< block_group->length);
1043e3e0520bSJosef Bacik 	}
1044b3470b5dSDavid Sterba 	block_group->space_info->total_bytes -= block_group->length;
10453349b57fSJosef Bacik 	if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags))
10466a921de5SNaohiro Aota 		block_group->space_info->active_total_bytes -= block_group->length;
1047169e0da9SNaohiro Aota 	block_group->space_info->bytes_readonly -=
1048169e0da9SNaohiro Aota 		(block_group->length - block_group->zone_unusable);
1049169e0da9SNaohiro Aota 	block_group->space_info->bytes_zone_unusable -=
1050169e0da9SNaohiro Aota 		block_group->zone_unusable;
1051b3470b5dSDavid Sterba 	block_group->space_info->disk_total -= block_group->length * factor;
1052e3e0520bSJosef Bacik 
1053e3e0520bSJosef Bacik 	spin_unlock(&block_group->space_info->lock);
1054e3e0520bSJosef Bacik 
1055ffcb9d44SFilipe Manana 	/*
1056ffcb9d44SFilipe Manana 	 * Remove the free space for the block group from the free space tree
1057ffcb9d44SFilipe Manana 	 * and the block group's item from the extent tree before marking the
1058ffcb9d44SFilipe Manana 	 * block group as removed. This is to prevent races with tasks that
1059ffcb9d44SFilipe Manana 	 * freeze and unfreeze a block group, this task and another task
1060ffcb9d44SFilipe Manana 	 * allocating a new block group - the unfreeze task ends up removing
1061ffcb9d44SFilipe Manana 	 * the block group's extent map before the task calling this function
1062ffcb9d44SFilipe Manana 	 * deletes the block group item from the extent tree, allowing for
1063ffcb9d44SFilipe Manana 	 * another task to attempt to create another block group with the same
1064ffcb9d44SFilipe Manana 	 * item key (and failing with -EEXIST and a transaction abort).
1065ffcb9d44SFilipe Manana 	 */
1066ffcb9d44SFilipe Manana 	ret = remove_block_group_free_space(trans, block_group);
1067ffcb9d44SFilipe Manana 	if (ret)
1068ffcb9d44SFilipe Manana 		goto out;
1069ffcb9d44SFilipe Manana 
1070ffcb9d44SFilipe Manana 	ret = remove_block_group_item(trans, path, block_group);
1071ffcb9d44SFilipe Manana 	if (ret < 0)
1072ffcb9d44SFilipe Manana 		goto out;
1073ffcb9d44SFilipe Manana 
1074e3e0520bSJosef Bacik 	spin_lock(&block_group->lock);
10753349b57fSJosef Bacik 	set_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags);
10763349b57fSJosef Bacik 
1077e3e0520bSJosef Bacik 	/*
10786b7304afSFilipe Manana 	 * At this point trimming or scrub can't start on this block group,
10796b7304afSFilipe Manana 	 * because we removed the block group from the rbtree
10806b7304afSFilipe Manana 	 * fs_info->block_group_cache_tree so no one can't find it anymore and
10816b7304afSFilipe Manana 	 * even if someone already got this block group before we removed it
10826b7304afSFilipe Manana 	 * from the rbtree, they have already incremented block_group->frozen -
10836b7304afSFilipe Manana 	 * if they didn't, for the trimming case they won't find any free space
10846b7304afSFilipe Manana 	 * entries because we already removed them all when we called
10856b7304afSFilipe Manana 	 * btrfs_remove_free_space_cache().
1086e3e0520bSJosef Bacik 	 *
1087e3e0520bSJosef Bacik 	 * And we must not remove the extent map from the fs_info->mapping_tree
1088e3e0520bSJosef Bacik 	 * to prevent the same logical address range and physical device space
10896b7304afSFilipe Manana 	 * ranges from being reused for a new block group. This is needed to
10906b7304afSFilipe Manana 	 * avoid races with trimming and scrub.
10916b7304afSFilipe Manana 	 *
10926b7304afSFilipe Manana 	 * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
1093e3e0520bSJosef Bacik 	 * completely transactionless, so while it is trimming a range the
1094e3e0520bSJosef Bacik 	 * currently running transaction might finish and a new one start,
1095e3e0520bSJosef Bacik 	 * allowing for new block groups to be created that can reuse the same
1096e3e0520bSJosef Bacik 	 * physical device locations unless we take this special care.
1097e3e0520bSJosef Bacik 	 *
1098e3e0520bSJosef Bacik 	 * There may also be an implicit trim operation if the file system
1099e3e0520bSJosef Bacik 	 * is mounted with -odiscard. The same protections must remain
1100e3e0520bSJosef Bacik 	 * in place until the extents have been discarded completely when
1101e3e0520bSJosef Bacik 	 * the transaction commit has completed.
1102e3e0520bSJosef Bacik 	 */
11036b7304afSFilipe Manana 	remove_em = (atomic_read(&block_group->frozen) == 0);
1104e3e0520bSJosef Bacik 	spin_unlock(&block_group->lock);
1105e3e0520bSJosef Bacik 
1106e3e0520bSJosef Bacik 	if (remove_em) {
1107e3e0520bSJosef Bacik 		struct extent_map_tree *em_tree;
1108e3e0520bSJosef Bacik 
1109e3e0520bSJosef Bacik 		em_tree = &fs_info->mapping_tree;
1110e3e0520bSJosef Bacik 		write_lock(&em_tree->lock);
1111e3e0520bSJosef Bacik 		remove_extent_mapping(em_tree, em);
1112e3e0520bSJosef Bacik 		write_unlock(&em_tree->lock);
1113e3e0520bSJosef Bacik 		/* once for the tree */
1114e3e0520bSJosef Bacik 		free_extent_map(em);
1115e3e0520bSJosef Bacik 	}
1116f6033c5eSXiyu Yang 
11179fecd132SFilipe Manana out:
1118f6033c5eSXiyu Yang 	/* Once for the lookup reference */
1119f6033c5eSXiyu Yang 	btrfs_put_block_group(block_group);
1120e3e0520bSJosef Bacik 	if (remove_rsv)
1121e3e0520bSJosef Bacik 		btrfs_delayed_refs_rsv_release(fs_info, 1);
1122e3e0520bSJosef Bacik 	btrfs_free_path(path);
1123e3e0520bSJosef Bacik 	return ret;
1124e3e0520bSJosef Bacik }
1125e3e0520bSJosef Bacik 
1126e3e0520bSJosef Bacik struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
1127e3e0520bSJosef Bacik 		struct btrfs_fs_info *fs_info, const u64 chunk_offset)
1128e3e0520bSJosef Bacik {
1129dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
1130e3e0520bSJosef Bacik 	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1131e3e0520bSJosef Bacik 	struct extent_map *em;
1132e3e0520bSJosef Bacik 	struct map_lookup *map;
1133e3e0520bSJosef Bacik 	unsigned int num_items;
1134e3e0520bSJosef Bacik 
1135e3e0520bSJosef Bacik 	read_lock(&em_tree->lock);
1136e3e0520bSJosef Bacik 	em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1137e3e0520bSJosef Bacik 	read_unlock(&em_tree->lock);
1138e3e0520bSJosef Bacik 	ASSERT(em && em->start == chunk_offset);
1139e3e0520bSJosef Bacik 
1140e3e0520bSJosef Bacik 	/*
1141e3e0520bSJosef Bacik 	 * We need to reserve 3 + N units from the metadata space info in order
1142e3e0520bSJosef Bacik 	 * to remove a block group (done at btrfs_remove_chunk() and at
1143e3e0520bSJosef Bacik 	 * btrfs_remove_block_group()), which are used for:
1144e3e0520bSJosef Bacik 	 *
1145e3e0520bSJosef Bacik 	 * 1 unit for adding the free space inode's orphan (located in the tree
1146e3e0520bSJosef Bacik 	 * of tree roots).
1147e3e0520bSJosef Bacik 	 * 1 unit for deleting the block group item (located in the extent
1148e3e0520bSJosef Bacik 	 * tree).
1149e3e0520bSJosef Bacik 	 * 1 unit for deleting the free space item (located in tree of tree
1150e3e0520bSJosef Bacik 	 * roots).
1151e3e0520bSJosef Bacik 	 * N units for deleting N device extent items corresponding to each
1152e3e0520bSJosef Bacik 	 * stripe (located in the device tree).
1153e3e0520bSJosef Bacik 	 *
1154e3e0520bSJosef Bacik 	 * In order to remove a block group we also need to reserve units in the
1155e3e0520bSJosef Bacik 	 * system space info in order to update the chunk tree (update one or
1156e3e0520bSJosef Bacik 	 * more device items and remove one chunk item), but this is done at
1157e3e0520bSJosef Bacik 	 * btrfs_remove_chunk() through a call to check_system_chunk().
1158e3e0520bSJosef Bacik 	 */
1159e3e0520bSJosef Bacik 	map = em->map_lookup;
1160e3e0520bSJosef Bacik 	num_items = 3 + map->num_stripes;
1161e3e0520bSJosef Bacik 	free_extent_map(em);
1162e3e0520bSJosef Bacik 
1163dfe8aec4SJosef Bacik 	return btrfs_start_transaction_fallback_global_rsv(root, num_items);
1164e3e0520bSJosef Bacik }
1165e3e0520bSJosef Bacik 
1166e3e0520bSJosef Bacik /*
116726ce2095SJosef Bacik  * Mark block group @cache read-only, so later write won't happen to block
116826ce2095SJosef Bacik  * group @cache.
116926ce2095SJosef Bacik  *
117026ce2095SJosef Bacik  * If @force is not set, this function will only mark the block group readonly
117126ce2095SJosef Bacik  * if we have enough free space (1M) in other metadata/system block groups.
117226ce2095SJosef Bacik  * If @force is not set, this function will mark the block group readonly
117326ce2095SJosef Bacik  * without checking free space.
117426ce2095SJosef Bacik  *
117526ce2095SJosef Bacik  * NOTE: This function doesn't care if other block groups can contain all the
117626ce2095SJosef Bacik  * data in this block group. That check should be done by relocation routine,
117726ce2095SJosef Bacik  * not this function.
117826ce2095SJosef Bacik  */
117932da5386SDavid Sterba static int inc_block_group_ro(struct btrfs_block_group *cache, int force)
118026ce2095SJosef Bacik {
118126ce2095SJosef Bacik 	struct btrfs_space_info *sinfo = cache->space_info;
118226ce2095SJosef Bacik 	u64 num_bytes;
118326ce2095SJosef Bacik 	int ret = -ENOSPC;
118426ce2095SJosef Bacik 
118526ce2095SJosef Bacik 	spin_lock(&sinfo->lock);
118626ce2095SJosef Bacik 	spin_lock(&cache->lock);
118726ce2095SJosef Bacik 
1188195a49eaSFilipe Manana 	if (cache->swap_extents) {
1189195a49eaSFilipe Manana 		ret = -ETXTBSY;
1190195a49eaSFilipe Manana 		goto out;
1191195a49eaSFilipe Manana 	}
1192195a49eaSFilipe Manana 
119326ce2095SJosef Bacik 	if (cache->ro) {
119426ce2095SJosef Bacik 		cache->ro++;
119526ce2095SJosef Bacik 		ret = 0;
119626ce2095SJosef Bacik 		goto out;
119726ce2095SJosef Bacik 	}
119826ce2095SJosef Bacik 
1199b3470b5dSDavid Sterba 	num_bytes = cache->length - cache->reserved - cache->pinned -
1200169e0da9SNaohiro Aota 		    cache->bytes_super - cache->zone_unusable - cache->used;
120126ce2095SJosef Bacik 
120226ce2095SJosef Bacik 	/*
1203a30a3d20SJosef Bacik 	 * Data never overcommits, even in mixed mode, so do just the straight
1204a30a3d20SJosef Bacik 	 * check of left over space in how much we have allocated.
1205a30a3d20SJosef Bacik 	 */
1206a30a3d20SJosef Bacik 	if (force) {
1207a30a3d20SJosef Bacik 		ret = 0;
1208a30a3d20SJosef Bacik 	} else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) {
1209a30a3d20SJosef Bacik 		u64 sinfo_used = btrfs_space_info_used(sinfo, true);
1210a30a3d20SJosef Bacik 
1211a30a3d20SJosef Bacik 		/*
121226ce2095SJosef Bacik 		 * Here we make sure if we mark this bg RO, we still have enough
1213f8935566SJosef Bacik 		 * free space as buffer.
121426ce2095SJosef Bacik 		 */
1215a30a3d20SJosef Bacik 		if (sinfo_used + num_bytes <= sinfo->total_bytes)
1216a30a3d20SJosef Bacik 			ret = 0;
1217a30a3d20SJosef Bacik 	} else {
1218a30a3d20SJosef Bacik 		/*
1219a30a3d20SJosef Bacik 		 * We overcommit metadata, so we need to do the
1220a30a3d20SJosef Bacik 		 * btrfs_can_overcommit check here, and we need to pass in
1221a30a3d20SJosef Bacik 		 * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of
1222a30a3d20SJosef Bacik 		 * leeway to allow us to mark this block group as read only.
1223a30a3d20SJosef Bacik 		 */
1224a30a3d20SJosef Bacik 		if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes,
1225a30a3d20SJosef Bacik 					 BTRFS_RESERVE_NO_FLUSH))
1226a30a3d20SJosef Bacik 			ret = 0;
1227a30a3d20SJosef Bacik 	}
1228a30a3d20SJosef Bacik 
1229a30a3d20SJosef Bacik 	if (!ret) {
123026ce2095SJosef Bacik 		sinfo->bytes_readonly += num_bytes;
1231169e0da9SNaohiro Aota 		if (btrfs_is_zoned(cache->fs_info)) {
1232169e0da9SNaohiro Aota 			/* Migrate zone_unusable bytes to readonly */
1233169e0da9SNaohiro Aota 			sinfo->bytes_readonly += cache->zone_unusable;
1234169e0da9SNaohiro Aota 			sinfo->bytes_zone_unusable -= cache->zone_unusable;
1235169e0da9SNaohiro Aota 			cache->zone_unusable = 0;
1236169e0da9SNaohiro Aota 		}
123726ce2095SJosef Bacik 		cache->ro++;
123826ce2095SJosef Bacik 		list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
123926ce2095SJosef Bacik 	}
124026ce2095SJosef Bacik out:
124126ce2095SJosef Bacik 	spin_unlock(&cache->lock);
124226ce2095SJosef Bacik 	spin_unlock(&sinfo->lock);
124326ce2095SJosef Bacik 	if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) {
124426ce2095SJosef Bacik 		btrfs_info(cache->fs_info,
1245b3470b5dSDavid Sterba 			"unable to make block group %llu ro", cache->start);
124626ce2095SJosef Bacik 		btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0);
124726ce2095SJosef Bacik 	}
124826ce2095SJosef Bacik 	return ret;
124926ce2095SJosef Bacik }
125026ce2095SJosef Bacik 
1251fe119a6eSNikolay Borisov static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
1252fe119a6eSNikolay Borisov 				 struct btrfs_block_group *bg)
125345bb5d6aSNikolay Borisov {
125445bb5d6aSNikolay Borisov 	struct btrfs_fs_info *fs_info = bg->fs_info;
1255fe119a6eSNikolay Borisov 	struct btrfs_transaction *prev_trans = NULL;
125645bb5d6aSNikolay Borisov 	const u64 start = bg->start;
125745bb5d6aSNikolay Borisov 	const u64 end = start + bg->length - 1;
125845bb5d6aSNikolay Borisov 	int ret;
125945bb5d6aSNikolay Borisov 
1260fe119a6eSNikolay Borisov 	spin_lock(&fs_info->trans_lock);
1261fe119a6eSNikolay Borisov 	if (trans->transaction->list.prev != &fs_info->trans_list) {
1262fe119a6eSNikolay Borisov 		prev_trans = list_last_entry(&trans->transaction->list,
1263fe119a6eSNikolay Borisov 					     struct btrfs_transaction, list);
1264fe119a6eSNikolay Borisov 		refcount_inc(&prev_trans->use_count);
1265fe119a6eSNikolay Borisov 	}
1266fe119a6eSNikolay Borisov 	spin_unlock(&fs_info->trans_lock);
1267fe119a6eSNikolay Borisov 
126845bb5d6aSNikolay Borisov 	/*
126945bb5d6aSNikolay Borisov 	 * Hold the unused_bg_unpin_mutex lock to avoid racing with
127045bb5d6aSNikolay Borisov 	 * btrfs_finish_extent_commit(). If we are at transaction N, another
127145bb5d6aSNikolay Borisov 	 * task might be running finish_extent_commit() for the previous
127245bb5d6aSNikolay Borisov 	 * transaction N - 1, and have seen a range belonging to the block
1273fe119a6eSNikolay Borisov 	 * group in pinned_extents before we were able to clear the whole block
1274fe119a6eSNikolay Borisov 	 * group range from pinned_extents. This means that task can lookup for
1275fe119a6eSNikolay Borisov 	 * the block group after we unpinned it from pinned_extents and removed
1276fe119a6eSNikolay Borisov 	 * it, leading to a BUG_ON() at unpin_extent_range().
127745bb5d6aSNikolay Borisov 	 */
127845bb5d6aSNikolay Borisov 	mutex_lock(&fs_info->unused_bg_unpin_mutex);
1279fe119a6eSNikolay Borisov 	if (prev_trans) {
1280fe119a6eSNikolay Borisov 		ret = clear_extent_bits(&prev_trans->pinned_extents, start, end,
128145bb5d6aSNikolay Borisov 					EXTENT_DIRTY);
128245bb5d6aSNikolay Borisov 		if (ret)
1283534cf531SFilipe Manana 			goto out;
1284fe119a6eSNikolay Borisov 	}
128545bb5d6aSNikolay Borisov 
1286fe119a6eSNikolay Borisov 	ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end,
128745bb5d6aSNikolay Borisov 				EXTENT_DIRTY);
1288534cf531SFilipe Manana out:
128945bb5d6aSNikolay Borisov 	mutex_unlock(&fs_info->unused_bg_unpin_mutex);
12905150bf19SFilipe Manana 	if (prev_trans)
12915150bf19SFilipe Manana 		btrfs_put_transaction(prev_trans);
129245bb5d6aSNikolay Borisov 
1293534cf531SFilipe Manana 	return ret == 0;
129445bb5d6aSNikolay Borisov }
129545bb5d6aSNikolay Borisov 
129626ce2095SJosef Bacik /*
1297e3e0520bSJosef Bacik  * Process the unused_bgs list and remove any that don't have any allocated
1298e3e0520bSJosef Bacik  * space inside of them.
1299e3e0520bSJosef Bacik  */
1300e3e0520bSJosef Bacik void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
1301e3e0520bSJosef Bacik {
130232da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1303e3e0520bSJosef Bacik 	struct btrfs_space_info *space_info;
1304e3e0520bSJosef Bacik 	struct btrfs_trans_handle *trans;
13056e80d4f8SDennis Zhou 	const bool async_trim_enabled = btrfs_test_opt(fs_info, DISCARD_ASYNC);
1306e3e0520bSJosef Bacik 	int ret = 0;
1307e3e0520bSJosef Bacik 
1308e3e0520bSJosef Bacik 	if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1309e3e0520bSJosef Bacik 		return;
1310e3e0520bSJosef Bacik 
13112f12741fSJosef Bacik 	if (btrfs_fs_closing(fs_info))
13122f12741fSJosef Bacik 		return;
13132f12741fSJosef Bacik 
1314ddfd08cbSJosef Bacik 	/*
1315ddfd08cbSJosef Bacik 	 * Long running balances can keep us blocked here for eternity, so
1316ddfd08cbSJosef Bacik 	 * simply skip deletion if we're unable to get the mutex.
1317ddfd08cbSJosef Bacik 	 */
1318f3372065SJohannes Thumshirn 	if (!mutex_trylock(&fs_info->reclaim_bgs_lock))
1319ddfd08cbSJosef Bacik 		return;
1320ddfd08cbSJosef Bacik 
1321e3e0520bSJosef Bacik 	spin_lock(&fs_info->unused_bgs_lock);
1322e3e0520bSJosef Bacik 	while (!list_empty(&fs_info->unused_bgs)) {
1323e3e0520bSJosef Bacik 		int trimming;
1324e3e0520bSJosef Bacik 
1325e3e0520bSJosef Bacik 		block_group = list_first_entry(&fs_info->unused_bgs,
132632da5386SDavid Sterba 					       struct btrfs_block_group,
1327e3e0520bSJosef Bacik 					       bg_list);
1328e3e0520bSJosef Bacik 		list_del_init(&block_group->bg_list);
1329e3e0520bSJosef Bacik 
1330e3e0520bSJosef Bacik 		space_info = block_group->space_info;
1331e3e0520bSJosef Bacik 
1332e3e0520bSJosef Bacik 		if (ret || btrfs_mixed_space_info(space_info)) {
1333e3e0520bSJosef Bacik 			btrfs_put_block_group(block_group);
1334e3e0520bSJosef Bacik 			continue;
1335e3e0520bSJosef Bacik 		}
1336e3e0520bSJosef Bacik 		spin_unlock(&fs_info->unused_bgs_lock);
1337e3e0520bSJosef Bacik 
1338b0643e59SDennis Zhou 		btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
1339b0643e59SDennis Zhou 
1340e3e0520bSJosef Bacik 		/* Don't want to race with allocators so take the groups_sem */
1341e3e0520bSJosef Bacik 		down_write(&space_info->groups_sem);
13426e80d4f8SDennis Zhou 
13436e80d4f8SDennis Zhou 		/*
13446e80d4f8SDennis Zhou 		 * Async discard moves the final block group discard to be prior
13456e80d4f8SDennis Zhou 		 * to the unused_bgs code path.  Therefore, if it's not fully
13466e80d4f8SDennis Zhou 		 * trimmed, punt it back to the async discard lists.
13476e80d4f8SDennis Zhou 		 */
13486e80d4f8SDennis Zhou 		if (btrfs_test_opt(fs_info, DISCARD_ASYNC) &&
13496e80d4f8SDennis Zhou 		    !btrfs_is_free_space_trimmed(block_group)) {
13506e80d4f8SDennis Zhou 			trace_btrfs_skip_unused_block_group(block_group);
13516e80d4f8SDennis Zhou 			up_write(&space_info->groups_sem);
13526e80d4f8SDennis Zhou 			/* Requeue if we failed because of async discard */
13536e80d4f8SDennis Zhou 			btrfs_discard_queue_work(&fs_info->discard_ctl,
13546e80d4f8SDennis Zhou 						 block_group);
13556e80d4f8SDennis Zhou 			goto next;
13566e80d4f8SDennis Zhou 		}
13576e80d4f8SDennis Zhou 
1358e3e0520bSJosef Bacik 		spin_lock(&block_group->lock);
1359e3e0520bSJosef Bacik 		if (block_group->reserved || block_group->pinned ||
1360bf38be65SDavid Sterba 		    block_group->used || block_group->ro ||
1361e3e0520bSJosef Bacik 		    list_is_singular(&block_group->list)) {
1362e3e0520bSJosef Bacik 			/*
1363e3e0520bSJosef Bacik 			 * We want to bail if we made new allocations or have
1364e3e0520bSJosef Bacik 			 * outstanding allocations in this block group.  We do
1365e3e0520bSJosef Bacik 			 * the ro check in case balance is currently acting on
1366e3e0520bSJosef Bacik 			 * this block group.
1367e3e0520bSJosef Bacik 			 */
1368e3e0520bSJosef Bacik 			trace_btrfs_skip_unused_block_group(block_group);
1369e3e0520bSJosef Bacik 			spin_unlock(&block_group->lock);
1370e3e0520bSJosef Bacik 			up_write(&space_info->groups_sem);
1371e3e0520bSJosef Bacik 			goto next;
1372e3e0520bSJosef Bacik 		}
1373e3e0520bSJosef Bacik 		spin_unlock(&block_group->lock);
1374e3e0520bSJosef Bacik 
1375e3e0520bSJosef Bacik 		/* We don't want to force the issue, only flip if it's ok. */
1376e11c0406SJosef Bacik 		ret = inc_block_group_ro(block_group, 0);
1377e3e0520bSJosef Bacik 		up_write(&space_info->groups_sem);
1378e3e0520bSJosef Bacik 		if (ret < 0) {
1379e3e0520bSJosef Bacik 			ret = 0;
1380e3e0520bSJosef Bacik 			goto next;
1381e3e0520bSJosef Bacik 		}
1382e3e0520bSJosef Bacik 
138374e91b12SNaohiro Aota 		ret = btrfs_zone_finish(block_group);
138474e91b12SNaohiro Aota 		if (ret < 0) {
138574e91b12SNaohiro Aota 			btrfs_dec_block_group_ro(block_group);
138674e91b12SNaohiro Aota 			if (ret == -EAGAIN)
138774e91b12SNaohiro Aota 				ret = 0;
138874e91b12SNaohiro Aota 			goto next;
138974e91b12SNaohiro Aota 		}
139074e91b12SNaohiro Aota 
1391e3e0520bSJosef Bacik 		/*
1392e3e0520bSJosef Bacik 		 * Want to do this before we do anything else so we can recover
1393e3e0520bSJosef Bacik 		 * properly if we fail to join the transaction.
1394e3e0520bSJosef Bacik 		 */
1395e3e0520bSJosef Bacik 		trans = btrfs_start_trans_remove_block_group(fs_info,
1396b3470b5dSDavid Sterba 						     block_group->start);
1397e3e0520bSJosef Bacik 		if (IS_ERR(trans)) {
1398e3e0520bSJosef Bacik 			btrfs_dec_block_group_ro(block_group);
1399e3e0520bSJosef Bacik 			ret = PTR_ERR(trans);
1400e3e0520bSJosef Bacik 			goto next;
1401e3e0520bSJosef Bacik 		}
1402e3e0520bSJosef Bacik 
1403e3e0520bSJosef Bacik 		/*
1404e3e0520bSJosef Bacik 		 * We could have pending pinned extents for this block group,
1405e3e0520bSJosef Bacik 		 * just delete them, we don't care about them anymore.
1406e3e0520bSJosef Bacik 		 */
1407534cf531SFilipe Manana 		if (!clean_pinned_extents(trans, block_group)) {
1408534cf531SFilipe Manana 			btrfs_dec_block_group_ro(block_group);
1409e3e0520bSJosef Bacik 			goto end_trans;
1410534cf531SFilipe Manana 		}
1411e3e0520bSJosef Bacik 
1412b0643e59SDennis Zhou 		/*
1413b0643e59SDennis Zhou 		 * At this point, the block_group is read only and should fail
1414b0643e59SDennis Zhou 		 * new allocations.  However, btrfs_finish_extent_commit() can
1415b0643e59SDennis Zhou 		 * cause this block_group to be placed back on the discard
1416b0643e59SDennis Zhou 		 * lists because now the block_group isn't fully discarded.
1417b0643e59SDennis Zhou 		 * Bail here and try again later after discarding everything.
1418b0643e59SDennis Zhou 		 */
1419b0643e59SDennis Zhou 		spin_lock(&fs_info->discard_ctl.lock);
1420b0643e59SDennis Zhou 		if (!list_empty(&block_group->discard_list)) {
1421b0643e59SDennis Zhou 			spin_unlock(&fs_info->discard_ctl.lock);
1422b0643e59SDennis Zhou 			btrfs_dec_block_group_ro(block_group);
1423b0643e59SDennis Zhou 			btrfs_discard_queue_work(&fs_info->discard_ctl,
1424b0643e59SDennis Zhou 						 block_group);
1425b0643e59SDennis Zhou 			goto end_trans;
1426b0643e59SDennis Zhou 		}
1427b0643e59SDennis Zhou 		spin_unlock(&fs_info->discard_ctl.lock);
1428b0643e59SDennis Zhou 
1429e3e0520bSJosef Bacik 		/* Reset pinned so btrfs_put_block_group doesn't complain */
1430e3e0520bSJosef Bacik 		spin_lock(&space_info->lock);
1431e3e0520bSJosef Bacik 		spin_lock(&block_group->lock);
1432e3e0520bSJosef Bacik 
1433e3e0520bSJosef Bacik 		btrfs_space_info_update_bytes_pinned(fs_info, space_info,
1434e3e0520bSJosef Bacik 						     -block_group->pinned);
1435e3e0520bSJosef Bacik 		space_info->bytes_readonly += block_group->pinned;
1436e3e0520bSJosef Bacik 		block_group->pinned = 0;
1437e3e0520bSJosef Bacik 
1438e3e0520bSJosef Bacik 		spin_unlock(&block_group->lock);
1439e3e0520bSJosef Bacik 		spin_unlock(&space_info->lock);
1440e3e0520bSJosef Bacik 
14416e80d4f8SDennis Zhou 		/*
14426e80d4f8SDennis Zhou 		 * The normal path here is an unused block group is passed here,
14436e80d4f8SDennis Zhou 		 * then trimming is handled in the transaction commit path.
14446e80d4f8SDennis Zhou 		 * Async discard interposes before this to do the trimming
14456e80d4f8SDennis Zhou 		 * before coming down the unused block group path as trimming
14466e80d4f8SDennis Zhou 		 * will no longer be done later in the transaction commit path.
14476e80d4f8SDennis Zhou 		 */
14486e80d4f8SDennis Zhou 		if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC))
14496e80d4f8SDennis Zhou 			goto flip_async;
14506e80d4f8SDennis Zhou 
1451dcba6e48SNaohiro Aota 		/*
1452dcba6e48SNaohiro Aota 		 * DISCARD can flip during remount. On zoned filesystems, we
1453dcba6e48SNaohiro Aota 		 * need to reset sequential-required zones.
1454dcba6e48SNaohiro Aota 		 */
1455dcba6e48SNaohiro Aota 		trimming = btrfs_test_opt(fs_info, DISCARD_SYNC) ||
1456dcba6e48SNaohiro Aota 				btrfs_is_zoned(fs_info);
1457e3e0520bSJosef Bacik 
1458e3e0520bSJosef Bacik 		/* Implicit trim during transaction commit. */
1459e3e0520bSJosef Bacik 		if (trimming)
14606b7304afSFilipe Manana 			btrfs_freeze_block_group(block_group);
1461e3e0520bSJosef Bacik 
1462e3e0520bSJosef Bacik 		/*
1463e3e0520bSJosef Bacik 		 * Btrfs_remove_chunk will abort the transaction if things go
1464e3e0520bSJosef Bacik 		 * horribly wrong.
1465e3e0520bSJosef Bacik 		 */
1466b3470b5dSDavid Sterba 		ret = btrfs_remove_chunk(trans, block_group->start);
1467e3e0520bSJosef Bacik 
1468e3e0520bSJosef Bacik 		if (ret) {
1469e3e0520bSJosef Bacik 			if (trimming)
14706b7304afSFilipe Manana 				btrfs_unfreeze_block_group(block_group);
1471e3e0520bSJosef Bacik 			goto end_trans;
1472e3e0520bSJosef Bacik 		}
1473e3e0520bSJosef Bacik 
1474e3e0520bSJosef Bacik 		/*
1475e3e0520bSJosef Bacik 		 * If we're not mounted with -odiscard, we can just forget
1476e3e0520bSJosef Bacik 		 * about this block group. Otherwise we'll need to wait
1477e3e0520bSJosef Bacik 		 * until transaction commit to do the actual discard.
1478e3e0520bSJosef Bacik 		 */
1479e3e0520bSJosef Bacik 		if (trimming) {
1480e3e0520bSJosef Bacik 			spin_lock(&fs_info->unused_bgs_lock);
1481e3e0520bSJosef Bacik 			/*
1482e3e0520bSJosef Bacik 			 * A concurrent scrub might have added us to the list
1483e3e0520bSJosef Bacik 			 * fs_info->unused_bgs, so use a list_move operation
1484e3e0520bSJosef Bacik 			 * to add the block group to the deleted_bgs list.
1485e3e0520bSJosef Bacik 			 */
1486e3e0520bSJosef Bacik 			list_move(&block_group->bg_list,
1487e3e0520bSJosef Bacik 				  &trans->transaction->deleted_bgs);
1488e3e0520bSJosef Bacik 			spin_unlock(&fs_info->unused_bgs_lock);
1489e3e0520bSJosef Bacik 			btrfs_get_block_group(block_group);
1490e3e0520bSJosef Bacik 		}
1491e3e0520bSJosef Bacik end_trans:
1492e3e0520bSJosef Bacik 		btrfs_end_transaction(trans);
1493e3e0520bSJosef Bacik next:
1494e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
1495e3e0520bSJosef Bacik 		spin_lock(&fs_info->unused_bgs_lock);
1496e3e0520bSJosef Bacik 	}
1497e3e0520bSJosef Bacik 	spin_unlock(&fs_info->unused_bgs_lock);
1498f3372065SJohannes Thumshirn 	mutex_unlock(&fs_info->reclaim_bgs_lock);
14996e80d4f8SDennis Zhou 	return;
15006e80d4f8SDennis Zhou 
15016e80d4f8SDennis Zhou flip_async:
15026e80d4f8SDennis Zhou 	btrfs_end_transaction(trans);
1503f3372065SJohannes Thumshirn 	mutex_unlock(&fs_info->reclaim_bgs_lock);
15046e80d4f8SDennis Zhou 	btrfs_put_block_group(block_group);
15056e80d4f8SDennis Zhou 	btrfs_discard_punt_unused_bgs_list(fs_info);
1506e3e0520bSJosef Bacik }
1507e3e0520bSJosef Bacik 
150832da5386SDavid Sterba void btrfs_mark_bg_unused(struct btrfs_block_group *bg)
1509e3e0520bSJosef Bacik {
1510e3e0520bSJosef Bacik 	struct btrfs_fs_info *fs_info = bg->fs_info;
1511e3e0520bSJosef Bacik 
1512e3e0520bSJosef Bacik 	spin_lock(&fs_info->unused_bgs_lock);
1513e3e0520bSJosef Bacik 	if (list_empty(&bg->bg_list)) {
1514e3e0520bSJosef Bacik 		btrfs_get_block_group(bg);
1515e3e0520bSJosef Bacik 		trace_btrfs_add_unused_block_group(bg);
1516e3e0520bSJosef Bacik 		list_add_tail(&bg->bg_list, &fs_info->unused_bgs);
1517e3e0520bSJosef Bacik 	}
1518e3e0520bSJosef Bacik 	spin_unlock(&fs_info->unused_bgs_lock);
1519e3e0520bSJosef Bacik }
15204358d963SJosef Bacik 
15212ca0ec77SJohannes Thumshirn /*
15222ca0ec77SJohannes Thumshirn  * We want block groups with a low number of used bytes to be in the beginning
15232ca0ec77SJohannes Thumshirn  * of the list, so they will get reclaimed first.
15242ca0ec77SJohannes Thumshirn  */
15252ca0ec77SJohannes Thumshirn static int reclaim_bgs_cmp(void *unused, const struct list_head *a,
15262ca0ec77SJohannes Thumshirn 			   const struct list_head *b)
15272ca0ec77SJohannes Thumshirn {
15282ca0ec77SJohannes Thumshirn 	const struct btrfs_block_group *bg1, *bg2;
15292ca0ec77SJohannes Thumshirn 
15302ca0ec77SJohannes Thumshirn 	bg1 = list_entry(a, struct btrfs_block_group, bg_list);
15312ca0ec77SJohannes Thumshirn 	bg2 = list_entry(b, struct btrfs_block_group, bg_list);
15322ca0ec77SJohannes Thumshirn 
15332ca0ec77SJohannes Thumshirn 	return bg1->used > bg2->used;
15342ca0ec77SJohannes Thumshirn }
15352ca0ec77SJohannes Thumshirn 
15363687fcb0SJohannes Thumshirn static inline bool btrfs_should_reclaim(struct btrfs_fs_info *fs_info)
15373687fcb0SJohannes Thumshirn {
15383687fcb0SJohannes Thumshirn 	if (btrfs_is_zoned(fs_info))
15393687fcb0SJohannes Thumshirn 		return btrfs_zoned_should_reclaim(fs_info);
15403687fcb0SJohannes Thumshirn 	return true;
15413687fcb0SJohannes Thumshirn }
15423687fcb0SJohannes Thumshirn 
154381531225SBoris Burkov static bool should_reclaim_block_group(struct btrfs_block_group *bg, u64 bytes_freed)
154481531225SBoris Burkov {
154581531225SBoris Burkov 	const struct btrfs_space_info *space_info = bg->space_info;
154681531225SBoris Burkov 	const int reclaim_thresh = READ_ONCE(space_info->bg_reclaim_threshold);
154781531225SBoris Burkov 	const u64 new_val = bg->used;
154881531225SBoris Burkov 	const u64 old_val = new_val + bytes_freed;
154981531225SBoris Burkov 	u64 thresh;
155081531225SBoris Burkov 
155181531225SBoris Burkov 	if (reclaim_thresh == 0)
155281531225SBoris Burkov 		return false;
155381531225SBoris Burkov 
1554*428c8e03SDavid Sterba 	thresh = mult_perc(bg->length, reclaim_thresh);
155581531225SBoris Burkov 
155681531225SBoris Burkov 	/*
155781531225SBoris Burkov 	 * If we were below the threshold before don't reclaim, we are likely a
155881531225SBoris Burkov 	 * brand new block group and we don't want to relocate new block groups.
155981531225SBoris Burkov 	 */
156081531225SBoris Burkov 	if (old_val < thresh)
156181531225SBoris Burkov 		return false;
156281531225SBoris Burkov 	if (new_val >= thresh)
156381531225SBoris Burkov 		return false;
156481531225SBoris Burkov 	return true;
156581531225SBoris Burkov }
156681531225SBoris Burkov 
156718bb8bbfSJohannes Thumshirn void btrfs_reclaim_bgs_work(struct work_struct *work)
156818bb8bbfSJohannes Thumshirn {
156918bb8bbfSJohannes Thumshirn 	struct btrfs_fs_info *fs_info =
157018bb8bbfSJohannes Thumshirn 		container_of(work, struct btrfs_fs_info, reclaim_bgs_work);
157118bb8bbfSJohannes Thumshirn 	struct btrfs_block_group *bg;
157218bb8bbfSJohannes Thumshirn 	struct btrfs_space_info *space_info;
157318bb8bbfSJohannes Thumshirn 
157418bb8bbfSJohannes Thumshirn 	if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
157518bb8bbfSJohannes Thumshirn 		return;
157618bb8bbfSJohannes Thumshirn 
15772f12741fSJosef Bacik 	if (btrfs_fs_closing(fs_info))
15782f12741fSJosef Bacik 		return;
15792f12741fSJosef Bacik 
15803687fcb0SJohannes Thumshirn 	if (!btrfs_should_reclaim(fs_info))
15813687fcb0SJohannes Thumshirn 		return;
15823687fcb0SJohannes Thumshirn 
1583ca5e4ea0SNaohiro Aota 	sb_start_write(fs_info->sb);
1584ca5e4ea0SNaohiro Aota 
1585ca5e4ea0SNaohiro Aota 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
1586ca5e4ea0SNaohiro Aota 		sb_end_write(fs_info->sb);
158718bb8bbfSJohannes Thumshirn 		return;
1588ca5e4ea0SNaohiro Aota 	}
158918bb8bbfSJohannes Thumshirn 
15909cc0b837SJohannes Thumshirn 	/*
15919cc0b837SJohannes Thumshirn 	 * Long running balances can keep us blocked here for eternity, so
15929cc0b837SJohannes Thumshirn 	 * simply skip reclaim if we're unable to get the mutex.
15939cc0b837SJohannes Thumshirn 	 */
15949cc0b837SJohannes Thumshirn 	if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) {
15959cc0b837SJohannes Thumshirn 		btrfs_exclop_finish(fs_info);
1596ca5e4ea0SNaohiro Aota 		sb_end_write(fs_info->sb);
15979cc0b837SJohannes Thumshirn 		return;
15989cc0b837SJohannes Thumshirn 	}
15999cc0b837SJohannes Thumshirn 
160018bb8bbfSJohannes Thumshirn 	spin_lock(&fs_info->unused_bgs_lock);
16012ca0ec77SJohannes Thumshirn 	/*
16022ca0ec77SJohannes Thumshirn 	 * Sort happens under lock because we can't simply splice it and sort.
16032ca0ec77SJohannes Thumshirn 	 * The block groups might still be in use and reachable via bg_list,
16042ca0ec77SJohannes Thumshirn 	 * and their presence in the reclaim_bgs list must be preserved.
16052ca0ec77SJohannes Thumshirn 	 */
16062ca0ec77SJohannes Thumshirn 	list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp);
160718bb8bbfSJohannes Thumshirn 	while (!list_empty(&fs_info->reclaim_bgs)) {
16085f93e776SJohannes Thumshirn 		u64 zone_unusable;
16091cea5cf0SFilipe Manana 		int ret = 0;
16101cea5cf0SFilipe Manana 
161118bb8bbfSJohannes Thumshirn 		bg = list_first_entry(&fs_info->reclaim_bgs,
161218bb8bbfSJohannes Thumshirn 				      struct btrfs_block_group,
161318bb8bbfSJohannes Thumshirn 				      bg_list);
161418bb8bbfSJohannes Thumshirn 		list_del_init(&bg->bg_list);
161518bb8bbfSJohannes Thumshirn 
161618bb8bbfSJohannes Thumshirn 		space_info = bg->space_info;
161718bb8bbfSJohannes Thumshirn 		spin_unlock(&fs_info->unused_bgs_lock);
161818bb8bbfSJohannes Thumshirn 
161918bb8bbfSJohannes Thumshirn 		/* Don't race with allocators so take the groups_sem */
162018bb8bbfSJohannes Thumshirn 		down_write(&space_info->groups_sem);
162118bb8bbfSJohannes Thumshirn 
162218bb8bbfSJohannes Thumshirn 		spin_lock(&bg->lock);
162318bb8bbfSJohannes Thumshirn 		if (bg->reserved || bg->pinned || bg->ro) {
162418bb8bbfSJohannes Thumshirn 			/*
162518bb8bbfSJohannes Thumshirn 			 * We want to bail if we made new allocations or have
162618bb8bbfSJohannes Thumshirn 			 * outstanding allocations in this block group.  We do
162718bb8bbfSJohannes Thumshirn 			 * the ro check in case balance is currently acting on
162818bb8bbfSJohannes Thumshirn 			 * this block group.
162918bb8bbfSJohannes Thumshirn 			 */
163018bb8bbfSJohannes Thumshirn 			spin_unlock(&bg->lock);
163118bb8bbfSJohannes Thumshirn 			up_write(&space_info->groups_sem);
163218bb8bbfSJohannes Thumshirn 			goto next;
163318bb8bbfSJohannes Thumshirn 		}
1634cc4804bfSBoris Burkov 		if (bg->used == 0) {
1635cc4804bfSBoris Burkov 			/*
1636cc4804bfSBoris Burkov 			 * It is possible that we trigger relocation on a block
1637cc4804bfSBoris Burkov 			 * group as its extents are deleted and it first goes
1638cc4804bfSBoris Burkov 			 * below the threshold, then shortly after goes empty.
1639cc4804bfSBoris Burkov 			 *
1640cc4804bfSBoris Burkov 			 * In this case, relocating it does delete it, but has
1641cc4804bfSBoris Burkov 			 * some overhead in relocation specific metadata, looking
1642cc4804bfSBoris Burkov 			 * for the non-existent extents and running some extra
1643cc4804bfSBoris Burkov 			 * transactions, which we can avoid by using one of the
1644cc4804bfSBoris Burkov 			 * other mechanisms for dealing with empty block groups.
1645cc4804bfSBoris Burkov 			 */
1646cc4804bfSBoris Burkov 			if (!btrfs_test_opt(fs_info, DISCARD_ASYNC))
1647cc4804bfSBoris Burkov 				btrfs_mark_bg_unused(bg);
1648cc4804bfSBoris Burkov 			spin_unlock(&bg->lock);
1649cc4804bfSBoris Burkov 			up_write(&space_info->groups_sem);
1650cc4804bfSBoris Burkov 			goto next;
165181531225SBoris Burkov 
165281531225SBoris Burkov 		}
165381531225SBoris Burkov 		/*
165481531225SBoris Burkov 		 * The block group might no longer meet the reclaim condition by
165581531225SBoris Burkov 		 * the time we get around to reclaiming it, so to avoid
165681531225SBoris Burkov 		 * reclaiming overly full block_groups, skip reclaiming them.
165781531225SBoris Burkov 		 *
165881531225SBoris Burkov 		 * Since the decision making process also depends on the amount
165981531225SBoris Burkov 		 * being freed, pass in a fake giant value to skip that extra
166081531225SBoris Burkov 		 * check, which is more meaningful when adding to the list in
166181531225SBoris Burkov 		 * the first place.
166281531225SBoris Burkov 		 */
166381531225SBoris Burkov 		if (!should_reclaim_block_group(bg, bg->length)) {
166481531225SBoris Burkov 			spin_unlock(&bg->lock);
166581531225SBoris Burkov 			up_write(&space_info->groups_sem);
166681531225SBoris Burkov 			goto next;
1667cc4804bfSBoris Burkov 		}
166818bb8bbfSJohannes Thumshirn 		spin_unlock(&bg->lock);
166918bb8bbfSJohannes Thumshirn 
167018bb8bbfSJohannes Thumshirn 		/* Get out fast, in case we're unmounting the filesystem */
167118bb8bbfSJohannes Thumshirn 		if (btrfs_fs_closing(fs_info)) {
167218bb8bbfSJohannes Thumshirn 			up_write(&space_info->groups_sem);
167318bb8bbfSJohannes Thumshirn 			goto next;
167418bb8bbfSJohannes Thumshirn 		}
167518bb8bbfSJohannes Thumshirn 
16765f93e776SJohannes Thumshirn 		/*
16775f93e776SJohannes Thumshirn 		 * Cache the zone_unusable value before turning the block group
16785f93e776SJohannes Thumshirn 		 * to read only. As soon as the blog group is read only it's
16795f93e776SJohannes Thumshirn 		 * zone_unusable value gets moved to the block group's read-only
16805f93e776SJohannes Thumshirn 		 * bytes and isn't available for calculations anymore.
16815f93e776SJohannes Thumshirn 		 */
16825f93e776SJohannes Thumshirn 		zone_unusable = bg->zone_unusable;
168318bb8bbfSJohannes Thumshirn 		ret = inc_block_group_ro(bg, 0);
168418bb8bbfSJohannes Thumshirn 		up_write(&space_info->groups_sem);
168518bb8bbfSJohannes Thumshirn 		if (ret < 0)
168618bb8bbfSJohannes Thumshirn 			goto next;
168718bb8bbfSJohannes Thumshirn 
16885f93e776SJohannes Thumshirn 		btrfs_info(fs_info,
16895f93e776SJohannes Thumshirn 			"reclaiming chunk %llu with %llu%% used %llu%% unusable",
16905f93e776SJohannes Thumshirn 				bg->start, div_u64(bg->used * 100, bg->length),
16915f93e776SJohannes Thumshirn 				div64_u64(zone_unusable * 100, bg->length));
169218bb8bbfSJohannes Thumshirn 		trace_btrfs_reclaim_block_group(bg);
169318bb8bbfSJohannes Thumshirn 		ret = btrfs_relocate_chunk(fs_info, bg->start);
169474944c87SJosef Bacik 		if (ret) {
169574944c87SJosef Bacik 			btrfs_dec_block_group_ro(bg);
169618bb8bbfSJohannes Thumshirn 			btrfs_err(fs_info, "error relocating chunk %llu",
169718bb8bbfSJohannes Thumshirn 				  bg->start);
169874944c87SJosef Bacik 		}
169918bb8bbfSJohannes Thumshirn 
170018bb8bbfSJohannes Thumshirn next:
17011cea5cf0SFilipe Manana 		btrfs_put_block_group(bg);
1702d96b3424SFilipe Manana 		spin_lock(&fs_info->unused_bgs_lock);
170318bb8bbfSJohannes Thumshirn 	}
170418bb8bbfSJohannes Thumshirn 	spin_unlock(&fs_info->unused_bgs_lock);
170518bb8bbfSJohannes Thumshirn 	mutex_unlock(&fs_info->reclaim_bgs_lock);
170618bb8bbfSJohannes Thumshirn 	btrfs_exclop_finish(fs_info);
1707ca5e4ea0SNaohiro Aota 	sb_end_write(fs_info->sb);
170818bb8bbfSJohannes Thumshirn }
170918bb8bbfSJohannes Thumshirn 
171018bb8bbfSJohannes Thumshirn void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info)
171118bb8bbfSJohannes Thumshirn {
171218bb8bbfSJohannes Thumshirn 	spin_lock(&fs_info->unused_bgs_lock);
171318bb8bbfSJohannes Thumshirn 	if (!list_empty(&fs_info->reclaim_bgs))
171418bb8bbfSJohannes Thumshirn 		queue_work(system_unbound_wq, &fs_info->reclaim_bgs_work);
171518bb8bbfSJohannes Thumshirn 	spin_unlock(&fs_info->unused_bgs_lock);
171618bb8bbfSJohannes Thumshirn }
171718bb8bbfSJohannes Thumshirn 
171818bb8bbfSJohannes Thumshirn void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg)
171918bb8bbfSJohannes Thumshirn {
172018bb8bbfSJohannes Thumshirn 	struct btrfs_fs_info *fs_info = bg->fs_info;
172118bb8bbfSJohannes Thumshirn 
172218bb8bbfSJohannes Thumshirn 	spin_lock(&fs_info->unused_bgs_lock);
172318bb8bbfSJohannes Thumshirn 	if (list_empty(&bg->bg_list)) {
172418bb8bbfSJohannes Thumshirn 		btrfs_get_block_group(bg);
172518bb8bbfSJohannes Thumshirn 		trace_btrfs_add_reclaim_block_group(bg);
172618bb8bbfSJohannes Thumshirn 		list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
172718bb8bbfSJohannes Thumshirn 	}
172818bb8bbfSJohannes Thumshirn 	spin_unlock(&fs_info->unused_bgs_lock);
172918bb8bbfSJohannes Thumshirn }
173018bb8bbfSJohannes Thumshirn 
1731e3ba67a1SJohannes Thumshirn static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
1732e3ba67a1SJohannes Thumshirn 			   struct btrfs_path *path)
1733e3ba67a1SJohannes Thumshirn {
1734e3ba67a1SJohannes Thumshirn 	struct extent_map_tree *em_tree;
1735e3ba67a1SJohannes Thumshirn 	struct extent_map *em;
1736e3ba67a1SJohannes Thumshirn 	struct btrfs_block_group_item bg;
1737e3ba67a1SJohannes Thumshirn 	struct extent_buffer *leaf;
1738e3ba67a1SJohannes Thumshirn 	int slot;
1739e3ba67a1SJohannes Thumshirn 	u64 flags;
1740e3ba67a1SJohannes Thumshirn 	int ret = 0;
1741e3ba67a1SJohannes Thumshirn 
1742e3ba67a1SJohannes Thumshirn 	slot = path->slots[0];
1743e3ba67a1SJohannes Thumshirn 	leaf = path->nodes[0];
1744e3ba67a1SJohannes Thumshirn 
1745e3ba67a1SJohannes Thumshirn 	em_tree = &fs_info->mapping_tree;
1746e3ba67a1SJohannes Thumshirn 	read_lock(&em_tree->lock);
1747e3ba67a1SJohannes Thumshirn 	em = lookup_extent_mapping(em_tree, key->objectid, key->offset);
1748e3ba67a1SJohannes Thumshirn 	read_unlock(&em_tree->lock);
1749e3ba67a1SJohannes Thumshirn 	if (!em) {
1750e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
1751e3ba67a1SJohannes Thumshirn 			  "logical %llu len %llu found bg but no related chunk",
1752e3ba67a1SJohannes Thumshirn 			  key->objectid, key->offset);
1753e3ba67a1SJohannes Thumshirn 		return -ENOENT;
1754e3ba67a1SJohannes Thumshirn 	}
1755e3ba67a1SJohannes Thumshirn 
1756e3ba67a1SJohannes Thumshirn 	if (em->start != key->objectid || em->len != key->offset) {
1757e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
1758e3ba67a1SJohannes Thumshirn 			"block group %llu len %llu mismatch with chunk %llu len %llu",
1759e3ba67a1SJohannes Thumshirn 			key->objectid, key->offset, em->start, em->len);
1760e3ba67a1SJohannes Thumshirn 		ret = -EUCLEAN;
1761e3ba67a1SJohannes Thumshirn 		goto out_free_em;
1762e3ba67a1SJohannes Thumshirn 	}
1763e3ba67a1SJohannes Thumshirn 
1764e3ba67a1SJohannes Thumshirn 	read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot),
1765e3ba67a1SJohannes Thumshirn 			   sizeof(bg));
1766e3ba67a1SJohannes Thumshirn 	flags = btrfs_stack_block_group_flags(&bg) &
1767e3ba67a1SJohannes Thumshirn 		BTRFS_BLOCK_GROUP_TYPE_MASK;
1768e3ba67a1SJohannes Thumshirn 
1769e3ba67a1SJohannes Thumshirn 	if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1770e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
1771e3ba67a1SJohannes Thumshirn "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
1772e3ba67a1SJohannes Thumshirn 			  key->objectid, key->offset, flags,
1773e3ba67a1SJohannes Thumshirn 			  (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type));
1774e3ba67a1SJohannes Thumshirn 		ret = -EUCLEAN;
1775e3ba67a1SJohannes Thumshirn 	}
1776e3ba67a1SJohannes Thumshirn 
1777e3ba67a1SJohannes Thumshirn out_free_em:
1778e3ba67a1SJohannes Thumshirn 	free_extent_map(em);
1779e3ba67a1SJohannes Thumshirn 	return ret;
1780e3ba67a1SJohannes Thumshirn }
1781e3ba67a1SJohannes Thumshirn 
17824358d963SJosef Bacik static int find_first_block_group(struct btrfs_fs_info *fs_info,
17834358d963SJosef Bacik 				  struct btrfs_path *path,
17844358d963SJosef Bacik 				  struct btrfs_key *key)
17854358d963SJosef Bacik {
1786dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
1787e3ba67a1SJohannes Thumshirn 	int ret;
17884358d963SJosef Bacik 	struct btrfs_key found_key;
17894358d963SJosef Bacik 
179036dfbbe2SGabriel Niebler 	btrfs_for_each_slot(root, key, &found_key, path, ret) {
17914358d963SJosef Bacik 		if (found_key.objectid >= key->objectid &&
17924358d963SJosef Bacik 		    found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
179336dfbbe2SGabriel Niebler 			return read_bg_from_eb(fs_info, &found_key, path);
1794e3ba67a1SJohannes Thumshirn 		}
17954358d963SJosef Bacik 	}
17964358d963SJosef Bacik 	return ret;
17974358d963SJosef Bacik }
17984358d963SJosef Bacik 
17994358d963SJosef Bacik static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
18004358d963SJosef Bacik {
18014358d963SJosef Bacik 	u64 extra_flags = chunk_to_extended(flags) &
18024358d963SJosef Bacik 				BTRFS_EXTENDED_PROFILE_MASK;
18034358d963SJosef Bacik 
18044358d963SJosef Bacik 	write_seqlock(&fs_info->profiles_lock);
18054358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA)
18064358d963SJosef Bacik 		fs_info->avail_data_alloc_bits |= extra_flags;
18074358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_METADATA)
18084358d963SJosef Bacik 		fs_info->avail_metadata_alloc_bits |= extra_flags;
18094358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
18104358d963SJosef Bacik 		fs_info->avail_system_alloc_bits |= extra_flags;
18114358d963SJosef Bacik 	write_sequnlock(&fs_info->profiles_lock);
18124358d963SJosef Bacik }
18134358d963SJosef Bacik 
181443dd529aSDavid Sterba /*
181543dd529aSDavid Sterba  * Map a physical disk address to a list of logical addresses.
18169ee9b979SNikolay Borisov  *
18179ee9b979SNikolay Borisov  * @fs_info:       the filesystem
181896a14336SNikolay Borisov  * @chunk_start:   logical address of block group
1819138082f3SNaohiro Aota  * @bdev:	   physical device to resolve, can be NULL to indicate any device
182096a14336SNikolay Borisov  * @physical:	   physical address to map to logical addresses
182196a14336SNikolay Borisov  * @logical:	   return array of logical addresses which map to @physical
182296a14336SNikolay Borisov  * @naddrs:	   length of @logical
182396a14336SNikolay Borisov  * @stripe_len:    size of IO stripe for the given block group
182496a14336SNikolay Borisov  *
182596a14336SNikolay Borisov  * Maps a particular @physical disk address to a list of @logical addresses.
182696a14336SNikolay Borisov  * Used primarily to exclude those portions of a block group that contain super
182796a14336SNikolay Borisov  * block copies.
182896a14336SNikolay Borisov  */
182996a14336SNikolay Borisov int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
1830138082f3SNaohiro Aota 		     struct block_device *bdev, u64 physical, u64 **logical,
1831138082f3SNaohiro Aota 		     int *naddrs, int *stripe_len)
183296a14336SNikolay Borisov {
183396a14336SNikolay Borisov 	struct extent_map *em;
183496a14336SNikolay Borisov 	struct map_lookup *map;
183596a14336SNikolay Borisov 	u64 *buf;
183696a14336SNikolay Borisov 	u64 bytenr;
18371776ad17SNikolay Borisov 	u64 data_stripe_length;
18381776ad17SNikolay Borisov 	u64 io_stripe_size;
18391776ad17SNikolay Borisov 	int i, nr = 0;
18401776ad17SNikolay Borisov 	int ret = 0;
184196a14336SNikolay Borisov 
184296a14336SNikolay Borisov 	em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
184396a14336SNikolay Borisov 	if (IS_ERR(em))
184496a14336SNikolay Borisov 		return -EIO;
184596a14336SNikolay Borisov 
184696a14336SNikolay Borisov 	map = em->map_lookup;
18479e22b925SNikolay Borisov 	data_stripe_length = em->orig_block_len;
18481776ad17SNikolay Borisov 	io_stripe_size = map->stripe_len;
1849138082f3SNaohiro Aota 	chunk_start = em->start;
185096a14336SNikolay Borisov 
18519e22b925SNikolay Borisov 	/* For RAID5/6 adjust to a full IO stripe length */
18529e22b925SNikolay Borisov 	if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
18531776ad17SNikolay Borisov 		io_stripe_size = map->stripe_len * nr_data_stripes(map);
185496a14336SNikolay Borisov 
185596a14336SNikolay Borisov 	buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
18561776ad17SNikolay Borisov 	if (!buf) {
18571776ad17SNikolay Borisov 		ret = -ENOMEM;
18581776ad17SNikolay Borisov 		goto out;
18591776ad17SNikolay Borisov 	}
186096a14336SNikolay Borisov 
186196a14336SNikolay Borisov 	for (i = 0; i < map->num_stripes; i++) {
18621776ad17SNikolay Borisov 		bool already_inserted = false;
18631776ad17SNikolay Borisov 		u64 stripe_nr;
1864138082f3SNaohiro Aota 		u64 offset;
18651776ad17SNikolay Borisov 		int j;
18661776ad17SNikolay Borisov 
18671776ad17SNikolay Borisov 		if (!in_range(physical, map->stripes[i].physical,
18681776ad17SNikolay Borisov 			      data_stripe_length))
186996a14336SNikolay Borisov 			continue;
187096a14336SNikolay Borisov 
1871138082f3SNaohiro Aota 		if (bdev && map->stripes[i].dev->bdev != bdev)
1872138082f3SNaohiro Aota 			continue;
1873138082f3SNaohiro Aota 
187496a14336SNikolay Borisov 		stripe_nr = physical - map->stripes[i].physical;
1875138082f3SNaohiro Aota 		stripe_nr = div64_u64_rem(stripe_nr, map->stripe_len, &offset);
187696a14336SNikolay Borisov 
1877ac067734SDavid Sterba 		if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
1878ac067734SDavid Sterba 				 BTRFS_BLOCK_GROUP_RAID10)) {
187996a14336SNikolay Borisov 			stripe_nr = stripe_nr * map->num_stripes + i;
188096a14336SNikolay Borisov 			stripe_nr = div_u64(stripe_nr, map->sub_stripes);
188196a14336SNikolay Borisov 		}
188296a14336SNikolay Borisov 		/*
188396a14336SNikolay Borisov 		 * The remaining case would be for RAID56, multiply by
188496a14336SNikolay Borisov 		 * nr_data_stripes().  Alternatively, just use rmap_len below
188596a14336SNikolay Borisov 		 * instead of map->stripe_len
188696a14336SNikolay Borisov 		 */
188796a14336SNikolay Borisov 
1888138082f3SNaohiro Aota 		bytenr = chunk_start + stripe_nr * io_stripe_size + offset;
18891776ad17SNikolay Borisov 
18901776ad17SNikolay Borisov 		/* Ensure we don't add duplicate addresses */
189196a14336SNikolay Borisov 		for (j = 0; j < nr; j++) {
18921776ad17SNikolay Borisov 			if (buf[j] == bytenr) {
18931776ad17SNikolay Borisov 				already_inserted = true;
189496a14336SNikolay Borisov 				break;
189596a14336SNikolay Borisov 			}
189696a14336SNikolay Borisov 		}
18971776ad17SNikolay Borisov 
18981776ad17SNikolay Borisov 		if (!already_inserted)
18991776ad17SNikolay Borisov 			buf[nr++] = bytenr;
190096a14336SNikolay Borisov 	}
190196a14336SNikolay Borisov 
190296a14336SNikolay Borisov 	*logical = buf;
190396a14336SNikolay Borisov 	*naddrs = nr;
19041776ad17SNikolay Borisov 	*stripe_len = io_stripe_size;
19051776ad17SNikolay Borisov out:
190696a14336SNikolay Borisov 	free_extent_map(em);
19071776ad17SNikolay Borisov 	return ret;
190896a14336SNikolay Borisov }
190996a14336SNikolay Borisov 
191032da5386SDavid Sterba static int exclude_super_stripes(struct btrfs_block_group *cache)
19114358d963SJosef Bacik {
19124358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
191312659251SNaohiro Aota 	const bool zoned = btrfs_is_zoned(fs_info);
19144358d963SJosef Bacik 	u64 bytenr;
19154358d963SJosef Bacik 	u64 *logical;
19164358d963SJosef Bacik 	int stripe_len;
19174358d963SJosef Bacik 	int i, nr, ret;
19184358d963SJosef Bacik 
1919b3470b5dSDavid Sterba 	if (cache->start < BTRFS_SUPER_INFO_OFFSET) {
1920b3470b5dSDavid Sterba 		stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start;
19214358d963SJosef Bacik 		cache->bytes_super += stripe_len;
1922b3470b5dSDavid Sterba 		ret = btrfs_add_excluded_extent(fs_info, cache->start,
19234358d963SJosef Bacik 						stripe_len);
19244358d963SJosef Bacik 		if (ret)
19254358d963SJosef Bacik 			return ret;
19264358d963SJosef Bacik 	}
19274358d963SJosef Bacik 
19284358d963SJosef Bacik 	for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
19294358d963SJosef Bacik 		bytenr = btrfs_sb_offset(i);
1930138082f3SNaohiro Aota 		ret = btrfs_rmap_block(fs_info, cache->start, NULL,
19314358d963SJosef Bacik 				       bytenr, &logical, &nr, &stripe_len);
19324358d963SJosef Bacik 		if (ret)
19334358d963SJosef Bacik 			return ret;
19344358d963SJosef Bacik 
193512659251SNaohiro Aota 		/* Shouldn't have super stripes in sequential zones */
193612659251SNaohiro Aota 		if (zoned && nr) {
193712659251SNaohiro Aota 			btrfs_err(fs_info,
193812659251SNaohiro Aota 			"zoned: block group %llu must not contain super block",
193912659251SNaohiro Aota 				  cache->start);
194012659251SNaohiro Aota 			return -EUCLEAN;
194112659251SNaohiro Aota 		}
194212659251SNaohiro Aota 
19434358d963SJosef Bacik 		while (nr--) {
194496f9b0f2SNikolay Borisov 			u64 len = min_t(u64, stripe_len,
194596f9b0f2SNikolay Borisov 				cache->start + cache->length - logical[nr]);
19464358d963SJosef Bacik 
19474358d963SJosef Bacik 			cache->bytes_super += len;
194896f9b0f2SNikolay Borisov 			ret = btrfs_add_excluded_extent(fs_info, logical[nr],
194996f9b0f2SNikolay Borisov 							len);
19504358d963SJosef Bacik 			if (ret) {
19514358d963SJosef Bacik 				kfree(logical);
19524358d963SJosef Bacik 				return ret;
19534358d963SJosef Bacik 			}
19544358d963SJosef Bacik 		}
19554358d963SJosef Bacik 
19564358d963SJosef Bacik 		kfree(logical);
19574358d963SJosef Bacik 	}
19584358d963SJosef Bacik 	return 0;
19594358d963SJosef Bacik }
19604358d963SJosef Bacik 
196132da5386SDavid Sterba static struct btrfs_block_group *btrfs_create_block_group_cache(
19629afc6649SQu Wenruo 		struct btrfs_fs_info *fs_info, u64 start)
19634358d963SJosef Bacik {
196432da5386SDavid Sterba 	struct btrfs_block_group *cache;
19654358d963SJosef Bacik 
19664358d963SJosef Bacik 	cache = kzalloc(sizeof(*cache), GFP_NOFS);
19674358d963SJosef Bacik 	if (!cache)
19684358d963SJosef Bacik 		return NULL;
19694358d963SJosef Bacik 
19704358d963SJosef Bacik 	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
19714358d963SJosef Bacik 					GFP_NOFS);
19724358d963SJosef Bacik 	if (!cache->free_space_ctl) {
19734358d963SJosef Bacik 		kfree(cache);
19744358d963SJosef Bacik 		return NULL;
19754358d963SJosef Bacik 	}
19764358d963SJosef Bacik 
1977b3470b5dSDavid Sterba 	cache->start = start;
19784358d963SJosef Bacik 
19794358d963SJosef Bacik 	cache->fs_info = fs_info;
19804358d963SJosef Bacik 	cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
19814358d963SJosef Bacik 
19826e80d4f8SDennis Zhou 	cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED;
19836e80d4f8SDennis Zhou 
198448aaeebeSJosef Bacik 	refcount_set(&cache->refs, 1);
19854358d963SJosef Bacik 	spin_lock_init(&cache->lock);
19864358d963SJosef Bacik 	init_rwsem(&cache->data_rwsem);
19874358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->list);
19884358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->cluster_list);
19894358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->bg_list);
19904358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->ro_list);
1991b0643e59SDennis Zhou 	INIT_LIST_HEAD(&cache->discard_list);
19924358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->dirty_list);
19934358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->io_list);
1994afba2bc0SNaohiro Aota 	INIT_LIST_HEAD(&cache->active_bg_list);
1995cd79909bSJosef Bacik 	btrfs_init_free_space_ctl(cache, cache->free_space_ctl);
19966b7304afSFilipe Manana 	atomic_set(&cache->frozen, 0);
19974358d963SJosef Bacik 	mutex_init(&cache->free_space_lock);
1998c29abab4SJosef Bacik 	cache->full_stripe_locks_root.root = RB_ROOT;
1999c29abab4SJosef Bacik 	mutex_init(&cache->full_stripe_locks_root.lock);
20004358d963SJosef Bacik 
20014358d963SJosef Bacik 	return cache;
20024358d963SJosef Bacik }
20034358d963SJosef Bacik 
20044358d963SJosef Bacik /*
20054358d963SJosef Bacik  * Iterate all chunks and verify that each of them has the corresponding block
20064358d963SJosef Bacik  * group
20074358d963SJosef Bacik  */
20084358d963SJosef Bacik static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
20094358d963SJosef Bacik {
20104358d963SJosef Bacik 	struct extent_map_tree *map_tree = &fs_info->mapping_tree;
20114358d963SJosef Bacik 	struct extent_map *em;
201232da5386SDavid Sterba 	struct btrfs_block_group *bg;
20134358d963SJosef Bacik 	u64 start = 0;
20144358d963SJosef Bacik 	int ret = 0;
20154358d963SJosef Bacik 
20164358d963SJosef Bacik 	while (1) {
20174358d963SJosef Bacik 		read_lock(&map_tree->lock);
20184358d963SJosef Bacik 		/*
20194358d963SJosef Bacik 		 * lookup_extent_mapping will return the first extent map
20204358d963SJosef Bacik 		 * intersecting the range, so setting @len to 1 is enough to
20214358d963SJosef Bacik 		 * get the first chunk.
20224358d963SJosef Bacik 		 */
20234358d963SJosef Bacik 		em = lookup_extent_mapping(map_tree, start, 1);
20244358d963SJosef Bacik 		read_unlock(&map_tree->lock);
20254358d963SJosef Bacik 		if (!em)
20264358d963SJosef Bacik 			break;
20274358d963SJosef Bacik 
20284358d963SJosef Bacik 		bg = btrfs_lookup_block_group(fs_info, em->start);
20294358d963SJosef Bacik 		if (!bg) {
20304358d963SJosef Bacik 			btrfs_err(fs_info,
20314358d963SJosef Bacik 	"chunk start=%llu len=%llu doesn't have corresponding block group",
20324358d963SJosef Bacik 				     em->start, em->len);
20334358d963SJosef Bacik 			ret = -EUCLEAN;
20344358d963SJosef Bacik 			free_extent_map(em);
20354358d963SJosef Bacik 			break;
20364358d963SJosef Bacik 		}
2037b3470b5dSDavid Sterba 		if (bg->start != em->start || bg->length != em->len ||
20384358d963SJosef Bacik 		    (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
20394358d963SJosef Bacik 		    (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
20404358d963SJosef Bacik 			btrfs_err(fs_info,
20414358d963SJosef Bacik "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",
20424358d963SJosef Bacik 				em->start, em->len,
20434358d963SJosef Bacik 				em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
2044b3470b5dSDavid Sterba 				bg->start, bg->length,
20454358d963SJosef Bacik 				bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
20464358d963SJosef Bacik 			ret = -EUCLEAN;
20474358d963SJosef Bacik 			free_extent_map(em);
20484358d963SJosef Bacik 			btrfs_put_block_group(bg);
20494358d963SJosef Bacik 			break;
20504358d963SJosef Bacik 		}
20514358d963SJosef Bacik 		start = em->start + em->len;
20524358d963SJosef Bacik 		free_extent_map(em);
20534358d963SJosef Bacik 		btrfs_put_block_group(bg);
20544358d963SJosef Bacik 	}
20554358d963SJosef Bacik 	return ret;
20564358d963SJosef Bacik }
20574358d963SJosef Bacik 
2058ffb9e0f0SQu Wenruo static int read_one_block_group(struct btrfs_fs_info *info,
20594afd2fe8SJohannes Thumshirn 				struct btrfs_block_group_item *bgi,
2060d49a2ddbSQu Wenruo 				const struct btrfs_key *key,
2061ffb9e0f0SQu Wenruo 				int need_clear)
2062ffb9e0f0SQu Wenruo {
206332da5386SDavid Sterba 	struct btrfs_block_group *cache;
2064ffb9e0f0SQu Wenruo 	const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS);
2065ffb9e0f0SQu Wenruo 	int ret;
2066ffb9e0f0SQu Wenruo 
2067d49a2ddbSQu Wenruo 	ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY);
2068ffb9e0f0SQu Wenruo 
20699afc6649SQu Wenruo 	cache = btrfs_create_block_group_cache(info, key->objectid);
2070ffb9e0f0SQu Wenruo 	if (!cache)
2071ffb9e0f0SQu Wenruo 		return -ENOMEM;
2072ffb9e0f0SQu Wenruo 
20734afd2fe8SJohannes Thumshirn 	cache->length = key->offset;
20744afd2fe8SJohannes Thumshirn 	cache->used = btrfs_stack_block_group_used(bgi);
20757248e0ceSQu Wenruo 	cache->commit_used = cache->used;
20764afd2fe8SJohannes Thumshirn 	cache->flags = btrfs_stack_block_group_flags(bgi);
2077f7238e50SJosef Bacik 	cache->global_root_id = btrfs_stack_block_group_chunk_objectid(bgi);
20789afc6649SQu Wenruo 
2079e3e39c72SMarcos Paulo de Souza 	set_free_space_tree_thresholds(cache);
2080e3e39c72SMarcos Paulo de Souza 
2081ffb9e0f0SQu Wenruo 	if (need_clear) {
2082ffb9e0f0SQu Wenruo 		/*
2083ffb9e0f0SQu Wenruo 		 * When we mount with old space cache, we need to
2084ffb9e0f0SQu Wenruo 		 * set BTRFS_DC_CLEAR and set dirty flag.
2085ffb9e0f0SQu Wenruo 		 *
2086ffb9e0f0SQu Wenruo 		 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
2087ffb9e0f0SQu Wenruo 		 *    truncate the old free space cache inode and
2088ffb9e0f0SQu Wenruo 		 *    setup a new one.
2089ffb9e0f0SQu Wenruo 		 * b) Setting 'dirty flag' makes sure that we flush
2090ffb9e0f0SQu Wenruo 		 *    the new space cache info onto disk.
2091ffb9e0f0SQu Wenruo 		 */
2092ffb9e0f0SQu Wenruo 		if (btrfs_test_opt(info, SPACE_CACHE))
2093ffb9e0f0SQu Wenruo 			cache->disk_cache_state = BTRFS_DC_CLEAR;
2094ffb9e0f0SQu Wenruo 	}
2095ffb9e0f0SQu Wenruo 	if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
2096ffb9e0f0SQu Wenruo 	    (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
2097ffb9e0f0SQu Wenruo 			btrfs_err(info,
2098ffb9e0f0SQu Wenruo "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
2099ffb9e0f0SQu Wenruo 				  cache->start);
2100ffb9e0f0SQu Wenruo 			ret = -EINVAL;
2101ffb9e0f0SQu Wenruo 			goto error;
2102ffb9e0f0SQu Wenruo 	}
2103ffb9e0f0SQu Wenruo 
2104a94794d5SNaohiro Aota 	ret = btrfs_load_block_group_zone_info(cache, false);
210508e11a3dSNaohiro Aota 	if (ret) {
210608e11a3dSNaohiro Aota 		btrfs_err(info, "zoned: failed to load zone info of bg %llu",
210708e11a3dSNaohiro Aota 			  cache->start);
210808e11a3dSNaohiro Aota 		goto error;
210908e11a3dSNaohiro Aota 	}
211008e11a3dSNaohiro Aota 
2111ffb9e0f0SQu Wenruo 	/*
2112ffb9e0f0SQu Wenruo 	 * We need to exclude the super stripes now so that the space info has
2113ffb9e0f0SQu Wenruo 	 * super bytes accounted for, otherwise we'll think we have more space
2114ffb9e0f0SQu Wenruo 	 * than we actually do.
2115ffb9e0f0SQu Wenruo 	 */
2116ffb9e0f0SQu Wenruo 	ret = exclude_super_stripes(cache);
2117ffb9e0f0SQu Wenruo 	if (ret) {
2118ffb9e0f0SQu Wenruo 		/* We may have excluded something, so call this just in case. */
2119ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
2120ffb9e0f0SQu Wenruo 		goto error;
2121ffb9e0f0SQu Wenruo 	}
2122ffb9e0f0SQu Wenruo 
2123ffb9e0f0SQu Wenruo 	/*
2124169e0da9SNaohiro Aota 	 * For zoned filesystem, space after the allocation offset is the only
2125169e0da9SNaohiro Aota 	 * free space for a block group. So, we don't need any caching work.
2126169e0da9SNaohiro Aota 	 * btrfs_calc_zone_unusable() will set the amount of free space and
2127169e0da9SNaohiro Aota 	 * zone_unusable space.
2128169e0da9SNaohiro Aota 	 *
2129169e0da9SNaohiro Aota 	 * For regular filesystem, check for two cases, either we are full, and
2130169e0da9SNaohiro Aota 	 * therefore don't need to bother with the caching work since we won't
2131169e0da9SNaohiro Aota 	 * find any space, or we are empty, and we can just add all the space
2132169e0da9SNaohiro Aota 	 * in and be done with it.  This saves us _a_lot_ of time, particularly
2133169e0da9SNaohiro Aota 	 * in the full case.
2134ffb9e0f0SQu Wenruo 	 */
2135169e0da9SNaohiro Aota 	if (btrfs_is_zoned(info)) {
2136169e0da9SNaohiro Aota 		btrfs_calc_zone_unusable(cache);
2137c46c4247SNaohiro Aota 		/* Should not have any excluded extents. Just in case, though. */
2138c46c4247SNaohiro Aota 		btrfs_free_excluded_extents(cache);
2139169e0da9SNaohiro Aota 	} else if (cache->length == cache->used) {
2140ffb9e0f0SQu Wenruo 		cache->cached = BTRFS_CACHE_FINISHED;
2141ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
2142ffb9e0f0SQu Wenruo 	} else if (cache->used == 0) {
2143ffb9e0f0SQu Wenruo 		cache->cached = BTRFS_CACHE_FINISHED;
21449afc6649SQu Wenruo 		add_new_free_space(cache, cache->start,
21459afc6649SQu Wenruo 				   cache->start + cache->length);
2146ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
2147ffb9e0f0SQu Wenruo 	}
2148ffb9e0f0SQu Wenruo 
2149ffb9e0f0SQu Wenruo 	ret = btrfs_add_block_group_cache(info, cache);
2150ffb9e0f0SQu Wenruo 	if (ret) {
2151ffb9e0f0SQu Wenruo 		btrfs_remove_free_space_cache(cache);
2152ffb9e0f0SQu Wenruo 		goto error;
2153ffb9e0f0SQu Wenruo 	}
2154ffb9e0f0SQu Wenruo 	trace_btrfs_add_block_group(info, cache, 0);
2155723de71dSJosef Bacik 	btrfs_add_bg_to_space_info(info, cache);
2156ffb9e0f0SQu Wenruo 
2157ffb9e0f0SQu Wenruo 	set_avail_alloc_bits(info, cache->flags);
2158a09f23c3SAnand Jain 	if (btrfs_chunk_writeable(info, cache->start)) {
2159a09f23c3SAnand Jain 		if (cache->used == 0) {
2160ffb9e0f0SQu Wenruo 			ASSERT(list_empty(&cache->bg_list));
21616e80d4f8SDennis Zhou 			if (btrfs_test_opt(info, DISCARD_ASYNC))
21626e80d4f8SDennis Zhou 				btrfs_discard_queue_work(&info->discard_ctl, cache);
21636e80d4f8SDennis Zhou 			else
2164ffb9e0f0SQu Wenruo 				btrfs_mark_bg_unused(cache);
2165ffb9e0f0SQu Wenruo 		}
2166a09f23c3SAnand Jain 	} else {
2167a09f23c3SAnand Jain 		inc_block_group_ro(cache, 1);
2168a09f23c3SAnand Jain 	}
2169a09f23c3SAnand Jain 
2170ffb9e0f0SQu Wenruo 	return 0;
2171ffb9e0f0SQu Wenruo error:
2172ffb9e0f0SQu Wenruo 	btrfs_put_block_group(cache);
2173ffb9e0f0SQu Wenruo 	return ret;
2174ffb9e0f0SQu Wenruo }
2175ffb9e0f0SQu Wenruo 
217642437a63SJosef Bacik static int fill_dummy_bgs(struct btrfs_fs_info *fs_info)
217742437a63SJosef Bacik {
217842437a63SJosef Bacik 	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
217942437a63SJosef Bacik 	struct rb_node *node;
218042437a63SJosef Bacik 	int ret = 0;
218142437a63SJosef Bacik 
218242437a63SJosef Bacik 	for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
218342437a63SJosef Bacik 		struct extent_map *em;
218442437a63SJosef Bacik 		struct map_lookup *map;
218542437a63SJosef Bacik 		struct btrfs_block_group *bg;
218642437a63SJosef Bacik 
218742437a63SJosef Bacik 		em = rb_entry(node, struct extent_map, rb_node);
218842437a63SJosef Bacik 		map = em->map_lookup;
218942437a63SJosef Bacik 		bg = btrfs_create_block_group_cache(fs_info, em->start);
219042437a63SJosef Bacik 		if (!bg) {
219142437a63SJosef Bacik 			ret = -ENOMEM;
219242437a63SJosef Bacik 			break;
219342437a63SJosef Bacik 		}
219442437a63SJosef Bacik 
219542437a63SJosef Bacik 		/* Fill dummy cache as FULL */
219642437a63SJosef Bacik 		bg->length = em->len;
219742437a63SJosef Bacik 		bg->flags = map->type;
219842437a63SJosef Bacik 		bg->cached = BTRFS_CACHE_FINISHED;
219942437a63SJosef Bacik 		bg->used = em->len;
220042437a63SJosef Bacik 		bg->flags = map->type;
220142437a63SJosef Bacik 		ret = btrfs_add_block_group_cache(fs_info, bg);
22022b29726cSQu Wenruo 		/*
22032b29726cSQu Wenruo 		 * We may have some valid block group cache added already, in
22042b29726cSQu Wenruo 		 * that case we skip to the next one.
22052b29726cSQu Wenruo 		 */
22062b29726cSQu Wenruo 		if (ret == -EEXIST) {
22072b29726cSQu Wenruo 			ret = 0;
22082b29726cSQu Wenruo 			btrfs_put_block_group(bg);
22092b29726cSQu Wenruo 			continue;
22102b29726cSQu Wenruo 		}
22112b29726cSQu Wenruo 
221242437a63SJosef Bacik 		if (ret) {
221342437a63SJosef Bacik 			btrfs_remove_free_space_cache(bg);
221442437a63SJosef Bacik 			btrfs_put_block_group(bg);
221542437a63SJosef Bacik 			break;
221642437a63SJosef Bacik 		}
22172b29726cSQu Wenruo 
2218723de71dSJosef Bacik 		btrfs_add_bg_to_space_info(fs_info, bg);
221942437a63SJosef Bacik 
222042437a63SJosef Bacik 		set_avail_alloc_bits(fs_info, bg->flags);
222142437a63SJosef Bacik 	}
222242437a63SJosef Bacik 	if (!ret)
222342437a63SJosef Bacik 		btrfs_init_global_block_rsv(fs_info);
222442437a63SJosef Bacik 	return ret;
222542437a63SJosef Bacik }
222642437a63SJosef Bacik 
22274358d963SJosef Bacik int btrfs_read_block_groups(struct btrfs_fs_info *info)
22284358d963SJosef Bacik {
2229dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(info);
22304358d963SJosef Bacik 	struct btrfs_path *path;
22314358d963SJosef Bacik 	int ret;
223232da5386SDavid Sterba 	struct btrfs_block_group *cache;
22334358d963SJosef Bacik 	struct btrfs_space_info *space_info;
22344358d963SJosef Bacik 	struct btrfs_key key;
22354358d963SJosef Bacik 	int need_clear = 0;
22364358d963SJosef Bacik 	u64 cache_gen;
22374358d963SJosef Bacik 
223881d5d614SQu Wenruo 	/*
223981d5d614SQu Wenruo 	 * Either no extent root (with ibadroots rescue option) or we have
224081d5d614SQu Wenruo 	 * unsupported RO options. The fs can never be mounted read-write, so no
224181d5d614SQu Wenruo 	 * need to waste time searching block group items.
224281d5d614SQu Wenruo 	 *
224381d5d614SQu Wenruo 	 * This also allows new extent tree related changes to be RO compat,
224481d5d614SQu Wenruo 	 * no need for a full incompat flag.
224581d5d614SQu Wenruo 	 */
224681d5d614SQu Wenruo 	if (!root || (btrfs_super_compat_ro_flags(info->super_copy) &
224781d5d614SQu Wenruo 		      ~BTRFS_FEATURE_COMPAT_RO_SUPP))
224842437a63SJosef Bacik 		return fill_dummy_bgs(info);
224942437a63SJosef Bacik 
22504358d963SJosef Bacik 	key.objectid = 0;
22514358d963SJosef Bacik 	key.offset = 0;
22524358d963SJosef Bacik 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
22534358d963SJosef Bacik 	path = btrfs_alloc_path();
22544358d963SJosef Bacik 	if (!path)
22554358d963SJosef Bacik 		return -ENOMEM;
22564358d963SJosef Bacik 
22574358d963SJosef Bacik 	cache_gen = btrfs_super_cache_generation(info->super_copy);
22584358d963SJosef Bacik 	if (btrfs_test_opt(info, SPACE_CACHE) &&
22594358d963SJosef Bacik 	    btrfs_super_generation(info->super_copy) != cache_gen)
22604358d963SJosef Bacik 		need_clear = 1;
22614358d963SJosef Bacik 	if (btrfs_test_opt(info, CLEAR_CACHE))
22624358d963SJosef Bacik 		need_clear = 1;
22634358d963SJosef Bacik 
22644358d963SJosef Bacik 	while (1) {
22654afd2fe8SJohannes Thumshirn 		struct btrfs_block_group_item bgi;
22664afd2fe8SJohannes Thumshirn 		struct extent_buffer *leaf;
22674afd2fe8SJohannes Thumshirn 		int slot;
22684afd2fe8SJohannes Thumshirn 
22694358d963SJosef Bacik 		ret = find_first_block_group(info, path, &key);
22704358d963SJosef Bacik 		if (ret > 0)
22714358d963SJosef Bacik 			break;
22724358d963SJosef Bacik 		if (ret != 0)
22734358d963SJosef Bacik 			goto error;
22744358d963SJosef Bacik 
22754afd2fe8SJohannes Thumshirn 		leaf = path->nodes[0];
22764afd2fe8SJohannes Thumshirn 		slot = path->slots[0];
22774afd2fe8SJohannes Thumshirn 
22784afd2fe8SJohannes Thumshirn 		read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
22794afd2fe8SJohannes Thumshirn 				   sizeof(bgi));
22804afd2fe8SJohannes Thumshirn 
22814afd2fe8SJohannes Thumshirn 		btrfs_item_key_to_cpu(leaf, &key, slot);
22824afd2fe8SJohannes Thumshirn 		btrfs_release_path(path);
22834afd2fe8SJohannes Thumshirn 		ret = read_one_block_group(info, &bgi, &key, need_clear);
2284ffb9e0f0SQu Wenruo 		if (ret < 0)
22854358d963SJosef Bacik 			goto error;
2286ffb9e0f0SQu Wenruo 		key.objectid += key.offset;
2287ffb9e0f0SQu Wenruo 		key.offset = 0;
22884358d963SJosef Bacik 	}
22897837fa88SJosef Bacik 	btrfs_release_path(path);
22904358d963SJosef Bacik 
229172804905SJosef Bacik 	list_for_each_entry(space_info, &info->space_info, list) {
229249ea112dSJosef Bacik 		int i;
229349ea112dSJosef Bacik 
229449ea112dSJosef Bacik 		for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
229549ea112dSJosef Bacik 			if (list_empty(&space_info->block_groups[i]))
229649ea112dSJosef Bacik 				continue;
229749ea112dSJosef Bacik 			cache = list_first_entry(&space_info->block_groups[i],
229849ea112dSJosef Bacik 						 struct btrfs_block_group,
229949ea112dSJosef Bacik 						 list);
230049ea112dSJosef Bacik 			btrfs_sysfs_add_block_group_type(cache);
230149ea112dSJosef Bacik 		}
230249ea112dSJosef Bacik 
23034358d963SJosef Bacik 		if (!(btrfs_get_alloc_profile(info, space_info->flags) &
23044358d963SJosef Bacik 		      (BTRFS_BLOCK_GROUP_RAID10 |
23054358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_RAID1_MASK |
23064358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_RAID56_MASK |
23074358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_DUP)))
23084358d963SJosef Bacik 			continue;
23094358d963SJosef Bacik 		/*
23104358d963SJosef Bacik 		 * Avoid allocating from un-mirrored block group if there are
23114358d963SJosef Bacik 		 * mirrored block groups.
23124358d963SJosef Bacik 		 */
23134358d963SJosef Bacik 		list_for_each_entry(cache,
23144358d963SJosef Bacik 				&space_info->block_groups[BTRFS_RAID_RAID0],
23154358d963SJosef Bacik 				list)
2316e11c0406SJosef Bacik 			inc_block_group_ro(cache, 1);
23174358d963SJosef Bacik 		list_for_each_entry(cache,
23184358d963SJosef Bacik 				&space_info->block_groups[BTRFS_RAID_SINGLE],
23194358d963SJosef Bacik 				list)
2320e11c0406SJosef Bacik 			inc_block_group_ro(cache, 1);
23214358d963SJosef Bacik 	}
23224358d963SJosef Bacik 
23234358d963SJosef Bacik 	btrfs_init_global_block_rsv(info);
23244358d963SJosef Bacik 	ret = check_chunk_block_group_mappings(info);
23254358d963SJosef Bacik error:
23264358d963SJosef Bacik 	btrfs_free_path(path);
23272b29726cSQu Wenruo 	/*
23282b29726cSQu Wenruo 	 * We've hit some error while reading the extent tree, and have
23292b29726cSQu Wenruo 	 * rescue=ibadroots mount option.
23302b29726cSQu Wenruo 	 * Try to fill the tree using dummy block groups so that the user can
23312b29726cSQu Wenruo 	 * continue to mount and grab their data.
23322b29726cSQu Wenruo 	 */
23332b29726cSQu Wenruo 	if (ret && btrfs_test_opt(info, IGNOREBADROOTS))
23342b29726cSQu Wenruo 		ret = fill_dummy_bgs(info);
23354358d963SJosef Bacik 	return ret;
23364358d963SJosef Bacik }
23374358d963SJosef Bacik 
233879bd3712SFilipe Manana /*
233979bd3712SFilipe Manana  * This function, insert_block_group_item(), belongs to the phase 2 of chunk
234079bd3712SFilipe Manana  * allocation.
234179bd3712SFilipe Manana  *
234279bd3712SFilipe Manana  * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
234379bd3712SFilipe Manana  * phases.
234479bd3712SFilipe Manana  */
234597f4728aSQu Wenruo static int insert_block_group_item(struct btrfs_trans_handle *trans,
234697f4728aSQu Wenruo 				   struct btrfs_block_group *block_group)
234797f4728aSQu Wenruo {
234897f4728aSQu Wenruo 	struct btrfs_fs_info *fs_info = trans->fs_info;
234997f4728aSQu Wenruo 	struct btrfs_block_group_item bgi;
2350dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
235197f4728aSQu Wenruo 	struct btrfs_key key;
235297f4728aSQu Wenruo 
235397f4728aSQu Wenruo 	spin_lock(&block_group->lock);
235497f4728aSQu Wenruo 	btrfs_set_stack_block_group_used(&bgi, block_group->used);
235597f4728aSQu Wenruo 	btrfs_set_stack_block_group_chunk_objectid(&bgi,
2356f7238e50SJosef Bacik 						   block_group->global_root_id);
235797f4728aSQu Wenruo 	btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
235897f4728aSQu Wenruo 	key.objectid = block_group->start;
235997f4728aSQu Wenruo 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
236097f4728aSQu Wenruo 	key.offset = block_group->length;
236197f4728aSQu Wenruo 	spin_unlock(&block_group->lock);
236297f4728aSQu Wenruo 
236397f4728aSQu Wenruo 	return btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi));
236497f4728aSQu Wenruo }
236597f4728aSQu Wenruo 
23662eadb9e7SNikolay Borisov static int insert_dev_extent(struct btrfs_trans_handle *trans,
23672eadb9e7SNikolay Borisov 			    struct btrfs_device *device, u64 chunk_offset,
23682eadb9e7SNikolay Borisov 			    u64 start, u64 num_bytes)
23692eadb9e7SNikolay Borisov {
23702eadb9e7SNikolay Borisov 	struct btrfs_fs_info *fs_info = device->fs_info;
23712eadb9e7SNikolay Borisov 	struct btrfs_root *root = fs_info->dev_root;
23722eadb9e7SNikolay Borisov 	struct btrfs_path *path;
23732eadb9e7SNikolay Borisov 	struct btrfs_dev_extent *extent;
23742eadb9e7SNikolay Borisov 	struct extent_buffer *leaf;
23752eadb9e7SNikolay Borisov 	struct btrfs_key key;
23762eadb9e7SNikolay Borisov 	int ret;
23772eadb9e7SNikolay Borisov 
23782eadb9e7SNikolay Borisov 	WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
23792eadb9e7SNikolay Borisov 	WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
23802eadb9e7SNikolay Borisov 	path = btrfs_alloc_path();
23812eadb9e7SNikolay Borisov 	if (!path)
23822eadb9e7SNikolay Borisov 		return -ENOMEM;
23832eadb9e7SNikolay Borisov 
23842eadb9e7SNikolay Borisov 	key.objectid = device->devid;
23852eadb9e7SNikolay Borisov 	key.type = BTRFS_DEV_EXTENT_KEY;
23862eadb9e7SNikolay Borisov 	key.offset = start;
23872eadb9e7SNikolay Borisov 	ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*extent));
23882eadb9e7SNikolay Borisov 	if (ret)
23892eadb9e7SNikolay Borisov 		goto out;
23902eadb9e7SNikolay Borisov 
23912eadb9e7SNikolay Borisov 	leaf = path->nodes[0];
23922eadb9e7SNikolay Borisov 	extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
23932eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_chunk_tree(leaf, extent, BTRFS_CHUNK_TREE_OBJECTID);
23942eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_chunk_objectid(leaf, extent,
23952eadb9e7SNikolay Borisov 					    BTRFS_FIRST_CHUNK_TREE_OBJECTID);
23962eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
23972eadb9e7SNikolay Borisov 
23982eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_length(leaf, extent, num_bytes);
23992eadb9e7SNikolay Borisov 	btrfs_mark_buffer_dirty(leaf);
24002eadb9e7SNikolay Borisov out:
24012eadb9e7SNikolay Borisov 	btrfs_free_path(path);
24022eadb9e7SNikolay Borisov 	return ret;
24032eadb9e7SNikolay Borisov }
24042eadb9e7SNikolay Borisov 
24052eadb9e7SNikolay Borisov /*
24062eadb9e7SNikolay Borisov  * This function belongs to phase 2.
24072eadb9e7SNikolay Borisov  *
24082eadb9e7SNikolay Borisov  * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
24092eadb9e7SNikolay Borisov  * phases.
24102eadb9e7SNikolay Borisov  */
24112eadb9e7SNikolay Borisov static int insert_dev_extents(struct btrfs_trans_handle *trans,
24122eadb9e7SNikolay Borisov 				   u64 chunk_offset, u64 chunk_size)
24132eadb9e7SNikolay Borisov {
24142eadb9e7SNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
24152eadb9e7SNikolay Borisov 	struct btrfs_device *device;
24162eadb9e7SNikolay Borisov 	struct extent_map *em;
24172eadb9e7SNikolay Borisov 	struct map_lookup *map;
24182eadb9e7SNikolay Borisov 	u64 dev_offset;
24192eadb9e7SNikolay Borisov 	u64 stripe_size;
24202eadb9e7SNikolay Borisov 	int i;
24212eadb9e7SNikolay Borisov 	int ret = 0;
24222eadb9e7SNikolay Borisov 
24232eadb9e7SNikolay Borisov 	em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
24242eadb9e7SNikolay Borisov 	if (IS_ERR(em))
24252eadb9e7SNikolay Borisov 		return PTR_ERR(em);
24262eadb9e7SNikolay Borisov 
24272eadb9e7SNikolay Borisov 	map = em->map_lookup;
24282eadb9e7SNikolay Borisov 	stripe_size = em->orig_block_len;
24292eadb9e7SNikolay Borisov 
24302eadb9e7SNikolay Borisov 	/*
24312eadb9e7SNikolay Borisov 	 * Take the device list mutex to prevent races with the final phase of
24322eadb9e7SNikolay Borisov 	 * a device replace operation that replaces the device object associated
24332eadb9e7SNikolay Borisov 	 * with the map's stripes, because the device object's id can change
24342eadb9e7SNikolay Borisov 	 * at any time during that final phase of the device replace operation
24352eadb9e7SNikolay Borisov 	 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the
24362eadb9e7SNikolay Borisov 	 * replaced device and then see it with an ID of BTRFS_DEV_REPLACE_DEVID,
24372eadb9e7SNikolay Borisov 	 * resulting in persisting a device extent item with such ID.
24382eadb9e7SNikolay Borisov 	 */
24392eadb9e7SNikolay Borisov 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
24402eadb9e7SNikolay Borisov 	for (i = 0; i < map->num_stripes; i++) {
24412eadb9e7SNikolay Borisov 		device = map->stripes[i].dev;
24422eadb9e7SNikolay Borisov 		dev_offset = map->stripes[i].physical;
24432eadb9e7SNikolay Borisov 
24442eadb9e7SNikolay Borisov 		ret = insert_dev_extent(trans, device, chunk_offset, dev_offset,
24452eadb9e7SNikolay Borisov 				       stripe_size);
24462eadb9e7SNikolay Borisov 		if (ret)
24472eadb9e7SNikolay Borisov 			break;
24482eadb9e7SNikolay Borisov 	}
24492eadb9e7SNikolay Borisov 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
24502eadb9e7SNikolay Borisov 
24512eadb9e7SNikolay Borisov 	free_extent_map(em);
24522eadb9e7SNikolay Borisov 	return ret;
24532eadb9e7SNikolay Borisov }
24542eadb9e7SNikolay Borisov 
245579bd3712SFilipe Manana /*
245679bd3712SFilipe Manana  * This function, btrfs_create_pending_block_groups(), belongs to the phase 2 of
245779bd3712SFilipe Manana  * chunk allocation.
245879bd3712SFilipe Manana  *
245979bd3712SFilipe Manana  * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
246079bd3712SFilipe Manana  * phases.
246179bd3712SFilipe Manana  */
24624358d963SJosef Bacik void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
24634358d963SJosef Bacik {
24644358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
246532da5386SDavid Sterba 	struct btrfs_block_group *block_group;
24664358d963SJosef Bacik 	int ret = 0;
24674358d963SJosef Bacik 
24684358d963SJosef Bacik 	while (!list_empty(&trans->new_bgs)) {
246949ea112dSJosef Bacik 		int index;
247049ea112dSJosef Bacik 
24714358d963SJosef Bacik 		block_group = list_first_entry(&trans->new_bgs,
247232da5386SDavid Sterba 					       struct btrfs_block_group,
24734358d963SJosef Bacik 					       bg_list);
24744358d963SJosef Bacik 		if (ret)
24754358d963SJosef Bacik 			goto next;
24764358d963SJosef Bacik 
247749ea112dSJosef Bacik 		index = btrfs_bg_flags_to_raid_index(block_group->flags);
247849ea112dSJosef Bacik 
247997f4728aSQu Wenruo 		ret = insert_block_group_item(trans, block_group);
24804358d963SJosef Bacik 		if (ret)
24814358d963SJosef Bacik 			btrfs_abort_transaction(trans, ret);
24823349b57fSJosef Bacik 		if (!test_bit(BLOCK_GROUP_FLAG_CHUNK_ITEM_INSERTED,
24833349b57fSJosef Bacik 			      &block_group->runtime_flags)) {
248479bd3712SFilipe Manana 			mutex_lock(&fs_info->chunk_mutex);
248579bd3712SFilipe Manana 			ret = btrfs_chunk_alloc_add_chunk_item(trans, block_group);
248679bd3712SFilipe Manana 			mutex_unlock(&fs_info->chunk_mutex);
248779bd3712SFilipe Manana 			if (ret)
248879bd3712SFilipe Manana 				btrfs_abort_transaction(trans, ret);
248979bd3712SFilipe Manana 		}
24902eadb9e7SNikolay Borisov 		ret = insert_dev_extents(trans, block_group->start,
249197f4728aSQu Wenruo 					 block_group->length);
24924358d963SJosef Bacik 		if (ret)
24934358d963SJosef Bacik 			btrfs_abort_transaction(trans, ret);
24944358d963SJosef Bacik 		add_block_group_free_space(trans, block_group);
249549ea112dSJosef Bacik 
249649ea112dSJosef Bacik 		/*
249749ea112dSJosef Bacik 		 * If we restriped during balance, we may have added a new raid
249849ea112dSJosef Bacik 		 * type, so now add the sysfs entries when it is safe to do so.
249949ea112dSJosef Bacik 		 * We don't have to worry about locking here as it's handled in
250049ea112dSJosef Bacik 		 * btrfs_sysfs_add_block_group_type.
250149ea112dSJosef Bacik 		 */
250249ea112dSJosef Bacik 		if (block_group->space_info->block_group_kobjs[index] == NULL)
250349ea112dSJosef Bacik 			btrfs_sysfs_add_block_group_type(block_group);
250449ea112dSJosef Bacik 
25054358d963SJosef Bacik 		/* Already aborted the transaction if it failed. */
25064358d963SJosef Bacik next:
25074358d963SJosef Bacik 		btrfs_delayed_refs_rsv_release(fs_info, 1);
25084358d963SJosef Bacik 		list_del_init(&block_group->bg_list);
25094358d963SJosef Bacik 	}
25104358d963SJosef Bacik 	btrfs_trans_release_chunk_metadata(trans);
25114358d963SJosef Bacik }
25124358d963SJosef Bacik 
2513f7238e50SJosef Bacik /*
2514f7238e50SJosef Bacik  * For extent tree v2 we use the block_group_item->chunk_offset to point at our
2515f7238e50SJosef Bacik  * global root id.  For v1 it's always set to BTRFS_FIRST_CHUNK_TREE_OBJECTID.
2516f7238e50SJosef Bacik  */
2517f7238e50SJosef Bacik static u64 calculate_global_root_id(struct btrfs_fs_info *fs_info, u64 offset)
2518f7238e50SJosef Bacik {
2519f7238e50SJosef Bacik 	u64 div = SZ_1G;
2520f7238e50SJosef Bacik 	u64 index;
2521f7238e50SJosef Bacik 
2522f7238e50SJosef Bacik 	if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
2523f7238e50SJosef Bacik 		return BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2524f7238e50SJosef Bacik 
2525f7238e50SJosef Bacik 	/* If we have a smaller fs index based on 128MiB. */
2526f7238e50SJosef Bacik 	if (btrfs_super_total_bytes(fs_info->super_copy) <= (SZ_1G * 10ULL))
2527f7238e50SJosef Bacik 		div = SZ_128M;
2528f7238e50SJosef Bacik 
2529f7238e50SJosef Bacik 	offset = div64_u64(offset, div);
2530f7238e50SJosef Bacik 	div64_u64_rem(offset, fs_info->nr_global_roots, &index);
2531f7238e50SJosef Bacik 	return index;
2532f7238e50SJosef Bacik }
2533f7238e50SJosef Bacik 
253479bd3712SFilipe Manana struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *trans,
253579bd3712SFilipe Manana 						 u64 bytes_used, u64 type,
253679bd3712SFilipe Manana 						 u64 chunk_offset, u64 size)
25374358d963SJosef Bacik {
25384358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
253932da5386SDavid Sterba 	struct btrfs_block_group *cache;
25404358d963SJosef Bacik 	int ret;
25414358d963SJosef Bacik 
25424358d963SJosef Bacik 	btrfs_set_log_full_commit(trans);
25434358d963SJosef Bacik 
25449afc6649SQu Wenruo 	cache = btrfs_create_block_group_cache(fs_info, chunk_offset);
25454358d963SJosef Bacik 	if (!cache)
254679bd3712SFilipe Manana 		return ERR_PTR(-ENOMEM);
25474358d963SJosef Bacik 
25489afc6649SQu Wenruo 	cache->length = size;
2549e3e39c72SMarcos Paulo de Souza 	set_free_space_tree_thresholds(cache);
2550bf38be65SDavid Sterba 	cache->used = bytes_used;
25514358d963SJosef Bacik 	cache->flags = type;
25524358d963SJosef Bacik 	cache->cached = BTRFS_CACHE_FINISHED;
2553f7238e50SJosef Bacik 	cache->global_root_id = calculate_global_root_id(fs_info, cache->start);
2554f7238e50SJosef Bacik 
2555997e3e2eSBoris Burkov 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
25564358d963SJosef Bacik 		cache->needs_free_space = 1;
255708e11a3dSNaohiro Aota 
2558a94794d5SNaohiro Aota 	ret = btrfs_load_block_group_zone_info(cache, true);
255908e11a3dSNaohiro Aota 	if (ret) {
256008e11a3dSNaohiro Aota 		btrfs_put_block_group(cache);
256179bd3712SFilipe Manana 		return ERR_PTR(ret);
256208e11a3dSNaohiro Aota 	}
256308e11a3dSNaohiro Aota 
25644358d963SJosef Bacik 	ret = exclude_super_stripes(cache);
25654358d963SJosef Bacik 	if (ret) {
25664358d963SJosef Bacik 		/* We may have excluded something, so call this just in case */
25674358d963SJosef Bacik 		btrfs_free_excluded_extents(cache);
25684358d963SJosef Bacik 		btrfs_put_block_group(cache);
256979bd3712SFilipe Manana 		return ERR_PTR(ret);
25704358d963SJosef Bacik 	}
25714358d963SJosef Bacik 
25724358d963SJosef Bacik 	add_new_free_space(cache, chunk_offset, chunk_offset + size);
25734358d963SJosef Bacik 
25744358d963SJosef Bacik 	btrfs_free_excluded_extents(cache);
25754358d963SJosef Bacik 
25764358d963SJosef Bacik 	/*
25774358d963SJosef Bacik 	 * Ensure the corresponding space_info object is created and
25784358d963SJosef Bacik 	 * assigned to our block group. We want our bg to be added to the rbtree
25794358d963SJosef Bacik 	 * with its ->space_info set.
25804358d963SJosef Bacik 	 */
25814358d963SJosef Bacik 	cache->space_info = btrfs_find_space_info(fs_info, cache->flags);
25824358d963SJosef Bacik 	ASSERT(cache->space_info);
25834358d963SJosef Bacik 
25844358d963SJosef Bacik 	ret = btrfs_add_block_group_cache(fs_info, cache);
25854358d963SJosef Bacik 	if (ret) {
25864358d963SJosef Bacik 		btrfs_remove_free_space_cache(cache);
25874358d963SJosef Bacik 		btrfs_put_block_group(cache);
258879bd3712SFilipe Manana 		return ERR_PTR(ret);
25894358d963SJosef Bacik 	}
25904358d963SJosef Bacik 
25914358d963SJosef Bacik 	/*
25924358d963SJosef Bacik 	 * Now that our block group has its ->space_info set and is inserted in
25934358d963SJosef Bacik 	 * the rbtree, update the space info's counters.
25944358d963SJosef Bacik 	 */
25954358d963SJosef Bacik 	trace_btrfs_add_block_group(fs_info, cache, 1);
2596723de71dSJosef Bacik 	btrfs_add_bg_to_space_info(fs_info, cache);
25974358d963SJosef Bacik 	btrfs_update_global_block_rsv(fs_info);
25984358d963SJosef Bacik 
25999d4b0a12SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
26009d4b0a12SJosef Bacik 	if (btrfs_should_fragment_free_space(cache)) {
26019d4b0a12SJosef Bacik 		u64 new_bytes_used = size - bytes_used;
26029d4b0a12SJosef Bacik 
26039d4b0a12SJosef Bacik 		cache->space_info->bytes_used += new_bytes_used >> 1;
26049d4b0a12SJosef Bacik 		fragment_free_space(cache);
26059d4b0a12SJosef Bacik 	}
26069d4b0a12SJosef Bacik #endif
26074358d963SJosef Bacik 
26084358d963SJosef Bacik 	list_add_tail(&cache->bg_list, &trans->new_bgs);
26094358d963SJosef Bacik 	trans->delayed_ref_updates++;
26104358d963SJosef Bacik 	btrfs_update_delayed_refs_rsv(trans);
26114358d963SJosef Bacik 
26124358d963SJosef Bacik 	set_avail_alloc_bits(fs_info, type);
261379bd3712SFilipe Manana 	return cache;
26144358d963SJosef Bacik }
261526ce2095SJosef Bacik 
2616b12de528SQu Wenruo /*
2617b12de528SQu Wenruo  * Mark one block group RO, can be called several times for the same block
2618b12de528SQu Wenruo  * group.
2619b12de528SQu Wenruo  *
2620b12de528SQu Wenruo  * @cache:		the destination block group
2621b12de528SQu Wenruo  * @do_chunk_alloc:	whether need to do chunk pre-allocation, this is to
2622b12de528SQu Wenruo  * 			ensure we still have some free space after marking this
2623b12de528SQu Wenruo  * 			block group RO.
2624b12de528SQu Wenruo  */
2625b12de528SQu Wenruo int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
2626b12de528SQu Wenruo 			     bool do_chunk_alloc)
262726ce2095SJosef Bacik {
262826ce2095SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
262926ce2095SJosef Bacik 	struct btrfs_trans_handle *trans;
2630dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
263126ce2095SJosef Bacik 	u64 alloc_flags;
263226ce2095SJosef Bacik 	int ret;
2633b6e9f16cSNikolay Borisov 	bool dirty_bg_running;
263426ce2095SJosef Bacik 
26352d192fc4SQu Wenruo 	/*
26362d192fc4SQu Wenruo 	 * This can only happen when we are doing read-only scrub on read-only
26372d192fc4SQu Wenruo 	 * mount.
26382d192fc4SQu Wenruo 	 * In that case we should not start a new transaction on read-only fs.
26392d192fc4SQu Wenruo 	 * Thus here we skip all chunk allocations.
26402d192fc4SQu Wenruo 	 */
26412d192fc4SQu Wenruo 	if (sb_rdonly(fs_info->sb)) {
26422d192fc4SQu Wenruo 		mutex_lock(&fs_info->ro_block_group_mutex);
26432d192fc4SQu Wenruo 		ret = inc_block_group_ro(cache, 0);
26442d192fc4SQu Wenruo 		mutex_unlock(&fs_info->ro_block_group_mutex);
26452d192fc4SQu Wenruo 		return ret;
26462d192fc4SQu Wenruo 	}
26472d192fc4SQu Wenruo 
2648b6e9f16cSNikolay Borisov 	do {
2649dfe8aec4SJosef Bacik 		trans = btrfs_join_transaction(root);
265026ce2095SJosef Bacik 		if (IS_ERR(trans))
265126ce2095SJosef Bacik 			return PTR_ERR(trans);
265226ce2095SJosef Bacik 
2653b6e9f16cSNikolay Borisov 		dirty_bg_running = false;
2654b6e9f16cSNikolay Borisov 
265526ce2095SJosef Bacik 		/*
2656b6e9f16cSNikolay Borisov 		 * We're not allowed to set block groups readonly after the dirty
2657b6e9f16cSNikolay Borisov 		 * block group cache has started writing.  If it already started,
2658b6e9f16cSNikolay Borisov 		 * back off and let this transaction commit.
265926ce2095SJosef Bacik 		 */
266026ce2095SJosef Bacik 		mutex_lock(&fs_info->ro_block_group_mutex);
266126ce2095SJosef Bacik 		if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
266226ce2095SJosef Bacik 			u64 transid = trans->transid;
266326ce2095SJosef Bacik 
266426ce2095SJosef Bacik 			mutex_unlock(&fs_info->ro_block_group_mutex);
266526ce2095SJosef Bacik 			btrfs_end_transaction(trans);
266626ce2095SJosef Bacik 
266726ce2095SJosef Bacik 			ret = btrfs_wait_for_commit(fs_info, transid);
266826ce2095SJosef Bacik 			if (ret)
266926ce2095SJosef Bacik 				return ret;
2670b6e9f16cSNikolay Borisov 			dirty_bg_running = true;
267126ce2095SJosef Bacik 		}
2672b6e9f16cSNikolay Borisov 	} while (dirty_bg_running);
267326ce2095SJosef Bacik 
2674b12de528SQu Wenruo 	if (do_chunk_alloc) {
267526ce2095SJosef Bacik 		/*
2676b12de528SQu Wenruo 		 * If we are changing raid levels, try to allocate a
2677b12de528SQu Wenruo 		 * corresponding block group with the new raid level.
267826ce2095SJosef Bacik 		 */
2679349e120eSJosef Bacik 		alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
268026ce2095SJosef Bacik 		if (alloc_flags != cache->flags) {
2681b12de528SQu Wenruo 			ret = btrfs_chunk_alloc(trans, alloc_flags,
2682b12de528SQu Wenruo 						CHUNK_ALLOC_FORCE);
268326ce2095SJosef Bacik 			/*
268426ce2095SJosef Bacik 			 * ENOSPC is allowed here, we may have enough space
2685b12de528SQu Wenruo 			 * already allocated at the new raid level to carry on
268626ce2095SJosef Bacik 			 */
268726ce2095SJosef Bacik 			if (ret == -ENOSPC)
268826ce2095SJosef Bacik 				ret = 0;
268926ce2095SJosef Bacik 			if (ret < 0)
269026ce2095SJosef Bacik 				goto out;
269126ce2095SJosef Bacik 		}
2692b12de528SQu Wenruo 	}
269326ce2095SJosef Bacik 
2694a7a63accSJosef Bacik 	ret = inc_block_group_ro(cache, 0);
2695195a49eaSFilipe Manana 	if (!do_chunk_alloc || ret == -ETXTBSY)
2696b12de528SQu Wenruo 		goto unlock_out;
269726ce2095SJosef Bacik 	if (!ret)
269826ce2095SJosef Bacik 		goto out;
269926ce2095SJosef Bacik 	alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags);
270026ce2095SJosef Bacik 	ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
270126ce2095SJosef Bacik 	if (ret < 0)
270226ce2095SJosef Bacik 		goto out;
2703b6a98021SNaohiro Aota 	/*
2704b6a98021SNaohiro Aota 	 * We have allocated a new chunk. We also need to activate that chunk to
2705b6a98021SNaohiro Aota 	 * grant metadata tickets for zoned filesystem.
2706b6a98021SNaohiro Aota 	 */
2707b6a98021SNaohiro Aota 	ret = btrfs_zoned_activate_one_bg(fs_info, cache->space_info, true);
2708b6a98021SNaohiro Aota 	if (ret < 0)
2709b6a98021SNaohiro Aota 		goto out;
2710b6a98021SNaohiro Aota 
2711e11c0406SJosef Bacik 	ret = inc_block_group_ro(cache, 0);
2712195a49eaSFilipe Manana 	if (ret == -ETXTBSY)
2713195a49eaSFilipe Manana 		goto unlock_out;
271426ce2095SJosef Bacik out:
271526ce2095SJosef Bacik 	if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2716349e120eSJosef Bacik 		alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
271726ce2095SJosef Bacik 		mutex_lock(&fs_info->chunk_mutex);
271826ce2095SJosef Bacik 		check_system_chunk(trans, alloc_flags);
271926ce2095SJosef Bacik 		mutex_unlock(&fs_info->chunk_mutex);
272026ce2095SJosef Bacik 	}
2721b12de528SQu Wenruo unlock_out:
272226ce2095SJosef Bacik 	mutex_unlock(&fs_info->ro_block_group_mutex);
272326ce2095SJosef Bacik 
272426ce2095SJosef Bacik 	btrfs_end_transaction(trans);
272526ce2095SJosef Bacik 	return ret;
272626ce2095SJosef Bacik }
272726ce2095SJosef Bacik 
272832da5386SDavid Sterba void btrfs_dec_block_group_ro(struct btrfs_block_group *cache)
272926ce2095SJosef Bacik {
273026ce2095SJosef Bacik 	struct btrfs_space_info *sinfo = cache->space_info;
273126ce2095SJosef Bacik 	u64 num_bytes;
273226ce2095SJosef Bacik 
273326ce2095SJosef Bacik 	BUG_ON(!cache->ro);
273426ce2095SJosef Bacik 
273526ce2095SJosef Bacik 	spin_lock(&sinfo->lock);
273626ce2095SJosef Bacik 	spin_lock(&cache->lock);
273726ce2095SJosef Bacik 	if (!--cache->ro) {
2738169e0da9SNaohiro Aota 		if (btrfs_is_zoned(cache->fs_info)) {
2739169e0da9SNaohiro Aota 			/* Migrate zone_unusable bytes back */
274098173255SNaohiro Aota 			cache->zone_unusable =
274198173255SNaohiro Aota 				(cache->alloc_offset - cache->used) +
274298173255SNaohiro Aota 				(cache->length - cache->zone_capacity);
2743169e0da9SNaohiro Aota 			sinfo->bytes_zone_unusable += cache->zone_unusable;
2744169e0da9SNaohiro Aota 			sinfo->bytes_readonly -= cache->zone_unusable;
2745169e0da9SNaohiro Aota 		}
2746f9f28e5bSNaohiro Aota 		num_bytes = cache->length - cache->reserved -
2747f9f28e5bSNaohiro Aota 			    cache->pinned - cache->bytes_super -
2748f9f28e5bSNaohiro Aota 			    cache->zone_unusable - cache->used;
2749f9f28e5bSNaohiro Aota 		sinfo->bytes_readonly -= num_bytes;
275026ce2095SJosef Bacik 		list_del_init(&cache->ro_list);
275126ce2095SJosef Bacik 	}
275226ce2095SJosef Bacik 	spin_unlock(&cache->lock);
275326ce2095SJosef Bacik 	spin_unlock(&sinfo->lock);
275426ce2095SJosef Bacik }
275577745c05SJosef Bacik 
27563be4d8efSQu Wenruo static int update_block_group_item(struct btrfs_trans_handle *trans,
275777745c05SJosef Bacik 				   struct btrfs_path *path,
275832da5386SDavid Sterba 				   struct btrfs_block_group *cache)
275977745c05SJosef Bacik {
276077745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
276177745c05SJosef Bacik 	int ret;
2762dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
276377745c05SJosef Bacik 	unsigned long bi;
276477745c05SJosef Bacik 	struct extent_buffer *leaf;
2765bf38be65SDavid Sterba 	struct btrfs_block_group_item bgi;
2766b3470b5dSDavid Sterba 	struct btrfs_key key;
27677248e0ceSQu Wenruo 	u64 old_commit_used;
27687248e0ceSQu Wenruo 	u64 used;
27697248e0ceSQu Wenruo 
27707248e0ceSQu Wenruo 	/*
27717248e0ceSQu Wenruo 	 * Block group items update can be triggered out of commit transaction
27727248e0ceSQu Wenruo 	 * critical section, thus we need a consistent view of used bytes.
27737248e0ceSQu Wenruo 	 * We cannot use cache->used directly outside of the spin lock, as it
27747248e0ceSQu Wenruo 	 * may be changed.
27757248e0ceSQu Wenruo 	 */
27767248e0ceSQu Wenruo 	spin_lock(&cache->lock);
27777248e0ceSQu Wenruo 	old_commit_used = cache->commit_used;
27787248e0ceSQu Wenruo 	used = cache->used;
27797248e0ceSQu Wenruo 	/* No change in used bytes, can safely skip it. */
27807248e0ceSQu Wenruo 	if (cache->commit_used == used) {
27817248e0ceSQu Wenruo 		spin_unlock(&cache->lock);
27827248e0ceSQu Wenruo 		return 0;
27837248e0ceSQu Wenruo 	}
27847248e0ceSQu Wenruo 	cache->commit_used = used;
27857248e0ceSQu Wenruo 	spin_unlock(&cache->lock);
278677745c05SJosef Bacik 
2787b3470b5dSDavid Sterba 	key.objectid = cache->start;
2788b3470b5dSDavid Sterba 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2789b3470b5dSDavid Sterba 	key.offset = cache->length;
2790b3470b5dSDavid Sterba 
27913be4d8efSQu Wenruo 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
279277745c05SJosef Bacik 	if (ret) {
279377745c05SJosef Bacik 		if (ret > 0)
279477745c05SJosef Bacik 			ret = -ENOENT;
279577745c05SJosef Bacik 		goto fail;
279677745c05SJosef Bacik 	}
279777745c05SJosef Bacik 
279877745c05SJosef Bacik 	leaf = path->nodes[0];
279977745c05SJosef Bacik 	bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
28007248e0ceSQu Wenruo 	btrfs_set_stack_block_group_used(&bgi, used);
2801de0dc456SDavid Sterba 	btrfs_set_stack_block_group_chunk_objectid(&bgi,
2802f7238e50SJosef Bacik 						   cache->global_root_id);
2803de0dc456SDavid Sterba 	btrfs_set_stack_block_group_flags(&bgi, cache->flags);
2804bf38be65SDavid Sterba 	write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
280577745c05SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
280677745c05SJosef Bacik fail:
280777745c05SJosef Bacik 	btrfs_release_path(path);
28087248e0ceSQu Wenruo 	/* We didn't update the block group item, need to revert @commit_used. */
28097248e0ceSQu Wenruo 	if (ret < 0) {
28107248e0ceSQu Wenruo 		spin_lock(&cache->lock);
28117248e0ceSQu Wenruo 		cache->commit_used = old_commit_used;
28127248e0ceSQu Wenruo 		spin_unlock(&cache->lock);
28137248e0ceSQu Wenruo 	}
281477745c05SJosef Bacik 	return ret;
281577745c05SJosef Bacik 
281677745c05SJosef Bacik }
281777745c05SJosef Bacik 
281832da5386SDavid Sterba static int cache_save_setup(struct btrfs_block_group *block_group,
281977745c05SJosef Bacik 			    struct btrfs_trans_handle *trans,
282077745c05SJosef Bacik 			    struct btrfs_path *path)
282177745c05SJosef Bacik {
282277745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
282377745c05SJosef Bacik 	struct btrfs_root *root = fs_info->tree_root;
282477745c05SJosef Bacik 	struct inode *inode = NULL;
282577745c05SJosef Bacik 	struct extent_changeset *data_reserved = NULL;
282677745c05SJosef Bacik 	u64 alloc_hint = 0;
282777745c05SJosef Bacik 	int dcs = BTRFS_DC_ERROR;
28280044ae11SQu Wenruo 	u64 cache_size = 0;
282977745c05SJosef Bacik 	int retries = 0;
283077745c05SJosef Bacik 	int ret = 0;
283177745c05SJosef Bacik 
2832af456a2cSBoris Burkov 	if (!btrfs_test_opt(fs_info, SPACE_CACHE))
2833af456a2cSBoris Burkov 		return 0;
2834af456a2cSBoris Burkov 
283577745c05SJosef Bacik 	/*
283677745c05SJosef Bacik 	 * If this block group is smaller than 100 megs don't bother caching the
283777745c05SJosef Bacik 	 * block group.
283877745c05SJosef Bacik 	 */
2839b3470b5dSDavid Sterba 	if (block_group->length < (100 * SZ_1M)) {
284077745c05SJosef Bacik 		spin_lock(&block_group->lock);
284177745c05SJosef Bacik 		block_group->disk_cache_state = BTRFS_DC_WRITTEN;
284277745c05SJosef Bacik 		spin_unlock(&block_group->lock);
284377745c05SJosef Bacik 		return 0;
284477745c05SJosef Bacik 	}
284577745c05SJosef Bacik 
2846bf31f87fSDavid Sterba 	if (TRANS_ABORTED(trans))
284777745c05SJosef Bacik 		return 0;
284877745c05SJosef Bacik again:
284977745c05SJosef Bacik 	inode = lookup_free_space_inode(block_group, path);
285077745c05SJosef Bacik 	if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
285177745c05SJosef Bacik 		ret = PTR_ERR(inode);
285277745c05SJosef Bacik 		btrfs_release_path(path);
285377745c05SJosef Bacik 		goto out;
285477745c05SJosef Bacik 	}
285577745c05SJosef Bacik 
285677745c05SJosef Bacik 	if (IS_ERR(inode)) {
285777745c05SJosef Bacik 		BUG_ON(retries);
285877745c05SJosef Bacik 		retries++;
285977745c05SJosef Bacik 
286077745c05SJosef Bacik 		if (block_group->ro)
286177745c05SJosef Bacik 			goto out_free;
286277745c05SJosef Bacik 
286377745c05SJosef Bacik 		ret = create_free_space_inode(trans, block_group, path);
286477745c05SJosef Bacik 		if (ret)
286577745c05SJosef Bacik 			goto out_free;
286677745c05SJosef Bacik 		goto again;
286777745c05SJosef Bacik 	}
286877745c05SJosef Bacik 
286977745c05SJosef Bacik 	/*
287077745c05SJosef Bacik 	 * We want to set the generation to 0, that way if anything goes wrong
287177745c05SJosef Bacik 	 * from here on out we know not to trust this cache when we load up next
287277745c05SJosef Bacik 	 * time.
287377745c05SJosef Bacik 	 */
287477745c05SJosef Bacik 	BTRFS_I(inode)->generation = 0;
28759a56fcd1SNikolay Borisov 	ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
287677745c05SJosef Bacik 	if (ret) {
287777745c05SJosef Bacik 		/*
287877745c05SJosef Bacik 		 * So theoretically we could recover from this, simply set the
287977745c05SJosef Bacik 		 * super cache generation to 0 so we know to invalidate the
288077745c05SJosef Bacik 		 * cache, but then we'd have to keep track of the block groups
288177745c05SJosef Bacik 		 * that fail this way so we know we _have_ to reset this cache
288277745c05SJosef Bacik 		 * before the next commit or risk reading stale cache.  So to
288377745c05SJosef Bacik 		 * limit our exposure to horrible edge cases lets just abort the
288477745c05SJosef Bacik 		 * transaction, this only happens in really bad situations
288577745c05SJosef Bacik 		 * anyway.
288677745c05SJosef Bacik 		 */
288777745c05SJosef Bacik 		btrfs_abort_transaction(trans, ret);
288877745c05SJosef Bacik 		goto out_put;
288977745c05SJosef Bacik 	}
289077745c05SJosef Bacik 	WARN_ON(ret);
289177745c05SJosef Bacik 
289277745c05SJosef Bacik 	/* We've already setup this transaction, go ahead and exit */
289377745c05SJosef Bacik 	if (block_group->cache_generation == trans->transid &&
289477745c05SJosef Bacik 	    i_size_read(inode)) {
289577745c05SJosef Bacik 		dcs = BTRFS_DC_SETUP;
289677745c05SJosef Bacik 		goto out_put;
289777745c05SJosef Bacik 	}
289877745c05SJosef Bacik 
289977745c05SJosef Bacik 	if (i_size_read(inode) > 0) {
290077745c05SJosef Bacik 		ret = btrfs_check_trunc_cache_free_space(fs_info,
290177745c05SJosef Bacik 					&fs_info->global_block_rsv);
290277745c05SJosef Bacik 		if (ret)
290377745c05SJosef Bacik 			goto out_put;
290477745c05SJosef Bacik 
290577745c05SJosef Bacik 		ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
290677745c05SJosef Bacik 		if (ret)
290777745c05SJosef Bacik 			goto out_put;
290877745c05SJosef Bacik 	}
290977745c05SJosef Bacik 
291077745c05SJosef Bacik 	spin_lock(&block_group->lock);
291177745c05SJosef Bacik 	if (block_group->cached != BTRFS_CACHE_FINISHED ||
291277745c05SJosef Bacik 	    !btrfs_test_opt(fs_info, SPACE_CACHE)) {
291377745c05SJosef Bacik 		/*
291477745c05SJosef Bacik 		 * don't bother trying to write stuff out _if_
291577745c05SJosef Bacik 		 * a) we're not cached,
291677745c05SJosef Bacik 		 * b) we're with nospace_cache mount option,
291777745c05SJosef Bacik 		 * c) we're with v2 space_cache (FREE_SPACE_TREE).
291877745c05SJosef Bacik 		 */
291977745c05SJosef Bacik 		dcs = BTRFS_DC_WRITTEN;
292077745c05SJosef Bacik 		spin_unlock(&block_group->lock);
292177745c05SJosef Bacik 		goto out_put;
292277745c05SJosef Bacik 	}
292377745c05SJosef Bacik 	spin_unlock(&block_group->lock);
292477745c05SJosef Bacik 
292577745c05SJosef Bacik 	/*
292677745c05SJosef Bacik 	 * We hit an ENOSPC when setting up the cache in this transaction, just
292777745c05SJosef Bacik 	 * skip doing the setup, we've already cleared the cache so we're safe.
292877745c05SJosef Bacik 	 */
292977745c05SJosef Bacik 	if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
293077745c05SJosef Bacik 		ret = -ENOSPC;
293177745c05SJosef Bacik 		goto out_put;
293277745c05SJosef Bacik 	}
293377745c05SJosef Bacik 
293477745c05SJosef Bacik 	/*
293577745c05SJosef Bacik 	 * Try to preallocate enough space based on how big the block group is.
293677745c05SJosef Bacik 	 * Keep in mind this has to include any pinned space which could end up
293777745c05SJosef Bacik 	 * taking up quite a bit since it's not folded into the other space
293877745c05SJosef Bacik 	 * cache.
293977745c05SJosef Bacik 	 */
29400044ae11SQu Wenruo 	cache_size = div_u64(block_group->length, SZ_256M);
29410044ae11SQu Wenruo 	if (!cache_size)
29420044ae11SQu Wenruo 		cache_size = 1;
294377745c05SJosef Bacik 
29440044ae11SQu Wenruo 	cache_size *= 16;
29450044ae11SQu Wenruo 	cache_size *= fs_info->sectorsize;
294677745c05SJosef Bacik 
294736ea6f3eSNikolay Borisov 	ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, 0,
29481daedb1dSJosef Bacik 					  cache_size, false);
294977745c05SJosef Bacik 	if (ret)
295077745c05SJosef Bacik 		goto out_put;
295177745c05SJosef Bacik 
29520044ae11SQu Wenruo 	ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, cache_size,
29530044ae11SQu Wenruo 					      cache_size, cache_size,
295477745c05SJosef Bacik 					      &alloc_hint);
295577745c05SJosef Bacik 	/*
295677745c05SJosef Bacik 	 * Our cache requires contiguous chunks so that we don't modify a bunch
295777745c05SJosef Bacik 	 * of metadata or split extents when writing the cache out, which means
295877745c05SJosef Bacik 	 * we can enospc if we are heavily fragmented in addition to just normal
295977745c05SJosef Bacik 	 * out of space conditions.  So if we hit this just skip setting up any
296077745c05SJosef Bacik 	 * other block groups for this transaction, maybe we'll unpin enough
296177745c05SJosef Bacik 	 * space the next time around.
296277745c05SJosef Bacik 	 */
296377745c05SJosef Bacik 	if (!ret)
296477745c05SJosef Bacik 		dcs = BTRFS_DC_SETUP;
296577745c05SJosef Bacik 	else if (ret == -ENOSPC)
296677745c05SJosef Bacik 		set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
296777745c05SJosef Bacik 
296877745c05SJosef Bacik out_put:
296977745c05SJosef Bacik 	iput(inode);
297077745c05SJosef Bacik out_free:
297177745c05SJosef Bacik 	btrfs_release_path(path);
297277745c05SJosef Bacik out:
297377745c05SJosef Bacik 	spin_lock(&block_group->lock);
297477745c05SJosef Bacik 	if (!ret && dcs == BTRFS_DC_SETUP)
297577745c05SJosef Bacik 		block_group->cache_generation = trans->transid;
297677745c05SJosef Bacik 	block_group->disk_cache_state = dcs;
297777745c05SJosef Bacik 	spin_unlock(&block_group->lock);
297877745c05SJosef Bacik 
297977745c05SJosef Bacik 	extent_changeset_free(data_reserved);
298077745c05SJosef Bacik 	return ret;
298177745c05SJosef Bacik }
298277745c05SJosef Bacik 
298377745c05SJosef Bacik int btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
298477745c05SJosef Bacik {
298577745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
298632da5386SDavid Sterba 	struct btrfs_block_group *cache, *tmp;
298777745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
298877745c05SJosef Bacik 	struct btrfs_path *path;
298977745c05SJosef Bacik 
299077745c05SJosef Bacik 	if (list_empty(&cur_trans->dirty_bgs) ||
299177745c05SJosef Bacik 	    !btrfs_test_opt(fs_info, SPACE_CACHE))
299277745c05SJosef Bacik 		return 0;
299377745c05SJosef Bacik 
299477745c05SJosef Bacik 	path = btrfs_alloc_path();
299577745c05SJosef Bacik 	if (!path)
299677745c05SJosef Bacik 		return -ENOMEM;
299777745c05SJosef Bacik 
299877745c05SJosef Bacik 	/* Could add new block groups, use _safe just in case */
299977745c05SJosef Bacik 	list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
300077745c05SJosef Bacik 				 dirty_list) {
300177745c05SJosef Bacik 		if (cache->disk_cache_state == BTRFS_DC_CLEAR)
300277745c05SJosef Bacik 			cache_save_setup(cache, trans, path);
300377745c05SJosef Bacik 	}
300477745c05SJosef Bacik 
300577745c05SJosef Bacik 	btrfs_free_path(path);
300677745c05SJosef Bacik 	return 0;
300777745c05SJosef Bacik }
300877745c05SJosef Bacik 
300977745c05SJosef Bacik /*
301077745c05SJosef Bacik  * Transaction commit does final block group cache writeback during a critical
301177745c05SJosef Bacik  * section where nothing is allowed to change the FS.  This is required in
301277745c05SJosef Bacik  * order for the cache to actually match the block group, but can introduce a
301377745c05SJosef Bacik  * lot of latency into the commit.
301477745c05SJosef Bacik  *
301577745c05SJosef Bacik  * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
301677745c05SJosef Bacik  * There's a chance we'll have to redo some of it if the block group changes
301777745c05SJosef Bacik  * again during the commit, but it greatly reduces the commit latency by
301877745c05SJosef Bacik  * getting rid of the easy block groups while we're still allowing others to
301977745c05SJosef Bacik  * join the commit.
302077745c05SJosef Bacik  */
302177745c05SJosef Bacik int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
302277745c05SJosef Bacik {
302377745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
302432da5386SDavid Sterba 	struct btrfs_block_group *cache;
302577745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
302677745c05SJosef Bacik 	int ret = 0;
302777745c05SJosef Bacik 	int should_put;
302877745c05SJosef Bacik 	struct btrfs_path *path = NULL;
302977745c05SJosef Bacik 	LIST_HEAD(dirty);
303077745c05SJosef Bacik 	struct list_head *io = &cur_trans->io_bgs;
303177745c05SJosef Bacik 	int loops = 0;
303277745c05SJosef Bacik 
303377745c05SJosef Bacik 	spin_lock(&cur_trans->dirty_bgs_lock);
303477745c05SJosef Bacik 	if (list_empty(&cur_trans->dirty_bgs)) {
303577745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
303677745c05SJosef Bacik 		return 0;
303777745c05SJosef Bacik 	}
303877745c05SJosef Bacik 	list_splice_init(&cur_trans->dirty_bgs, &dirty);
303977745c05SJosef Bacik 	spin_unlock(&cur_trans->dirty_bgs_lock);
304077745c05SJosef Bacik 
304177745c05SJosef Bacik again:
304277745c05SJosef Bacik 	/* Make sure all the block groups on our dirty list actually exist */
304377745c05SJosef Bacik 	btrfs_create_pending_block_groups(trans);
304477745c05SJosef Bacik 
304577745c05SJosef Bacik 	if (!path) {
304677745c05SJosef Bacik 		path = btrfs_alloc_path();
3047938fcbfbSJosef Bacik 		if (!path) {
3048938fcbfbSJosef Bacik 			ret = -ENOMEM;
3049938fcbfbSJosef Bacik 			goto out;
3050938fcbfbSJosef Bacik 		}
305177745c05SJosef Bacik 	}
305277745c05SJosef Bacik 
305377745c05SJosef Bacik 	/*
305477745c05SJosef Bacik 	 * cache_write_mutex is here only to save us from balance or automatic
305577745c05SJosef Bacik 	 * removal of empty block groups deleting this block group while we are
305677745c05SJosef Bacik 	 * writing out the cache
305777745c05SJosef Bacik 	 */
305877745c05SJosef Bacik 	mutex_lock(&trans->transaction->cache_write_mutex);
305977745c05SJosef Bacik 	while (!list_empty(&dirty)) {
306077745c05SJosef Bacik 		bool drop_reserve = true;
306177745c05SJosef Bacik 
306232da5386SDavid Sterba 		cache = list_first_entry(&dirty, struct btrfs_block_group,
306377745c05SJosef Bacik 					 dirty_list);
306477745c05SJosef Bacik 		/*
306577745c05SJosef Bacik 		 * This can happen if something re-dirties a block group that
306677745c05SJosef Bacik 		 * is already under IO.  Just wait for it to finish and then do
306777745c05SJosef Bacik 		 * it all again
306877745c05SJosef Bacik 		 */
306977745c05SJosef Bacik 		if (!list_empty(&cache->io_list)) {
307077745c05SJosef Bacik 			list_del_init(&cache->io_list);
307177745c05SJosef Bacik 			btrfs_wait_cache_io(trans, cache, path);
307277745c05SJosef Bacik 			btrfs_put_block_group(cache);
307377745c05SJosef Bacik 		}
307477745c05SJosef Bacik 
307577745c05SJosef Bacik 
307677745c05SJosef Bacik 		/*
307777745c05SJosef Bacik 		 * btrfs_wait_cache_io uses the cache->dirty_list to decide if
307877745c05SJosef Bacik 		 * it should update the cache_state.  Don't delete until after
307977745c05SJosef Bacik 		 * we wait.
308077745c05SJosef Bacik 		 *
308177745c05SJosef Bacik 		 * Since we're not running in the commit critical section
308277745c05SJosef Bacik 		 * we need the dirty_bgs_lock to protect from update_block_group
308377745c05SJosef Bacik 		 */
308477745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
308577745c05SJosef Bacik 		list_del_init(&cache->dirty_list);
308677745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
308777745c05SJosef Bacik 
308877745c05SJosef Bacik 		should_put = 1;
308977745c05SJosef Bacik 
309077745c05SJosef Bacik 		cache_save_setup(cache, trans, path);
309177745c05SJosef Bacik 
309277745c05SJosef Bacik 		if (cache->disk_cache_state == BTRFS_DC_SETUP) {
309377745c05SJosef Bacik 			cache->io_ctl.inode = NULL;
309477745c05SJosef Bacik 			ret = btrfs_write_out_cache(trans, cache, path);
309577745c05SJosef Bacik 			if (ret == 0 && cache->io_ctl.inode) {
309677745c05SJosef Bacik 				should_put = 0;
309777745c05SJosef Bacik 
309877745c05SJosef Bacik 				/*
309977745c05SJosef Bacik 				 * The cache_write_mutex is protecting the
310077745c05SJosef Bacik 				 * io_list, also refer to the definition of
310177745c05SJosef Bacik 				 * btrfs_transaction::io_bgs for more details
310277745c05SJosef Bacik 				 */
310377745c05SJosef Bacik 				list_add_tail(&cache->io_list, io);
310477745c05SJosef Bacik 			} else {
310577745c05SJosef Bacik 				/*
310677745c05SJosef Bacik 				 * If we failed to write the cache, the
310777745c05SJosef Bacik 				 * generation will be bad and life goes on
310877745c05SJosef Bacik 				 */
310977745c05SJosef Bacik 				ret = 0;
311077745c05SJosef Bacik 			}
311177745c05SJosef Bacik 		}
311277745c05SJosef Bacik 		if (!ret) {
31133be4d8efSQu Wenruo 			ret = update_block_group_item(trans, path, cache);
311477745c05SJosef Bacik 			/*
311577745c05SJosef Bacik 			 * Our block group might still be attached to the list
311677745c05SJosef Bacik 			 * of new block groups in the transaction handle of some
311777745c05SJosef Bacik 			 * other task (struct btrfs_trans_handle->new_bgs). This
311877745c05SJosef Bacik 			 * means its block group item isn't yet in the extent
311977745c05SJosef Bacik 			 * tree. If this happens ignore the error, as we will
312077745c05SJosef Bacik 			 * try again later in the critical section of the
312177745c05SJosef Bacik 			 * transaction commit.
312277745c05SJosef Bacik 			 */
312377745c05SJosef Bacik 			if (ret == -ENOENT) {
312477745c05SJosef Bacik 				ret = 0;
312577745c05SJosef Bacik 				spin_lock(&cur_trans->dirty_bgs_lock);
312677745c05SJosef Bacik 				if (list_empty(&cache->dirty_list)) {
312777745c05SJosef Bacik 					list_add_tail(&cache->dirty_list,
312877745c05SJosef Bacik 						      &cur_trans->dirty_bgs);
312977745c05SJosef Bacik 					btrfs_get_block_group(cache);
313077745c05SJosef Bacik 					drop_reserve = false;
313177745c05SJosef Bacik 				}
313277745c05SJosef Bacik 				spin_unlock(&cur_trans->dirty_bgs_lock);
313377745c05SJosef Bacik 			} else if (ret) {
313477745c05SJosef Bacik 				btrfs_abort_transaction(trans, ret);
313577745c05SJosef Bacik 			}
313677745c05SJosef Bacik 		}
313777745c05SJosef Bacik 
313877745c05SJosef Bacik 		/* If it's not on the io list, we need to put the block group */
313977745c05SJosef Bacik 		if (should_put)
314077745c05SJosef Bacik 			btrfs_put_block_group(cache);
314177745c05SJosef Bacik 		if (drop_reserve)
314277745c05SJosef Bacik 			btrfs_delayed_refs_rsv_release(fs_info, 1);
314377745c05SJosef Bacik 		/*
314477745c05SJosef Bacik 		 * Avoid blocking other tasks for too long. It might even save
314577745c05SJosef Bacik 		 * us from writing caches for block groups that are going to be
314677745c05SJosef Bacik 		 * removed.
314777745c05SJosef Bacik 		 */
314877745c05SJosef Bacik 		mutex_unlock(&trans->transaction->cache_write_mutex);
3149938fcbfbSJosef Bacik 		if (ret)
3150938fcbfbSJosef Bacik 			goto out;
315177745c05SJosef Bacik 		mutex_lock(&trans->transaction->cache_write_mutex);
315277745c05SJosef Bacik 	}
315377745c05SJosef Bacik 	mutex_unlock(&trans->transaction->cache_write_mutex);
315477745c05SJosef Bacik 
315577745c05SJosef Bacik 	/*
315677745c05SJosef Bacik 	 * Go through delayed refs for all the stuff we've just kicked off
315777745c05SJosef Bacik 	 * and then loop back (just once)
315877745c05SJosef Bacik 	 */
315934d1eb0eSJosef Bacik 	if (!ret)
316077745c05SJosef Bacik 		ret = btrfs_run_delayed_refs(trans, 0);
316177745c05SJosef Bacik 	if (!ret && loops == 0) {
316277745c05SJosef Bacik 		loops++;
316377745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
316477745c05SJosef Bacik 		list_splice_init(&cur_trans->dirty_bgs, &dirty);
316577745c05SJosef Bacik 		/*
316677745c05SJosef Bacik 		 * dirty_bgs_lock protects us from concurrent block group
316777745c05SJosef Bacik 		 * deletes too (not just cache_write_mutex).
316877745c05SJosef Bacik 		 */
316977745c05SJosef Bacik 		if (!list_empty(&dirty)) {
317077745c05SJosef Bacik 			spin_unlock(&cur_trans->dirty_bgs_lock);
317177745c05SJosef Bacik 			goto again;
317277745c05SJosef Bacik 		}
317377745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
3174938fcbfbSJosef Bacik 	}
3175938fcbfbSJosef Bacik out:
3176938fcbfbSJosef Bacik 	if (ret < 0) {
3177938fcbfbSJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
3178938fcbfbSJosef Bacik 		list_splice_init(&dirty, &cur_trans->dirty_bgs);
3179938fcbfbSJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
318077745c05SJosef Bacik 		btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
318177745c05SJosef Bacik 	}
318277745c05SJosef Bacik 
318377745c05SJosef Bacik 	btrfs_free_path(path);
318477745c05SJosef Bacik 	return ret;
318577745c05SJosef Bacik }
318677745c05SJosef Bacik 
318777745c05SJosef Bacik int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
318877745c05SJosef Bacik {
318977745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
319032da5386SDavid Sterba 	struct btrfs_block_group *cache;
319177745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
319277745c05SJosef Bacik 	int ret = 0;
319377745c05SJosef Bacik 	int should_put;
319477745c05SJosef Bacik 	struct btrfs_path *path;
319577745c05SJosef Bacik 	struct list_head *io = &cur_trans->io_bgs;
319677745c05SJosef Bacik 
319777745c05SJosef Bacik 	path = btrfs_alloc_path();
319877745c05SJosef Bacik 	if (!path)
319977745c05SJosef Bacik 		return -ENOMEM;
320077745c05SJosef Bacik 
320177745c05SJosef Bacik 	/*
320277745c05SJosef Bacik 	 * Even though we are in the critical section of the transaction commit,
320377745c05SJosef Bacik 	 * we can still have concurrent tasks adding elements to this
320477745c05SJosef Bacik 	 * transaction's list of dirty block groups. These tasks correspond to
320577745c05SJosef Bacik 	 * endio free space workers started when writeback finishes for a
320677745c05SJosef Bacik 	 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
320777745c05SJosef Bacik 	 * allocate new block groups as a result of COWing nodes of the root
320877745c05SJosef Bacik 	 * tree when updating the free space inode. The writeback for the space
320977745c05SJosef Bacik 	 * caches is triggered by an earlier call to
321077745c05SJosef Bacik 	 * btrfs_start_dirty_block_groups() and iterations of the following
321177745c05SJosef Bacik 	 * loop.
321277745c05SJosef Bacik 	 * Also we want to do the cache_save_setup first and then run the
321377745c05SJosef Bacik 	 * delayed refs to make sure we have the best chance at doing this all
321477745c05SJosef Bacik 	 * in one shot.
321577745c05SJosef Bacik 	 */
321677745c05SJosef Bacik 	spin_lock(&cur_trans->dirty_bgs_lock);
321777745c05SJosef Bacik 	while (!list_empty(&cur_trans->dirty_bgs)) {
321877745c05SJosef Bacik 		cache = list_first_entry(&cur_trans->dirty_bgs,
321932da5386SDavid Sterba 					 struct btrfs_block_group,
322077745c05SJosef Bacik 					 dirty_list);
322177745c05SJosef Bacik 
322277745c05SJosef Bacik 		/*
322377745c05SJosef Bacik 		 * This can happen if cache_save_setup re-dirties a block group
322477745c05SJosef Bacik 		 * that is already under IO.  Just wait for it to finish and
322577745c05SJosef Bacik 		 * then do it all again
322677745c05SJosef Bacik 		 */
322777745c05SJosef Bacik 		if (!list_empty(&cache->io_list)) {
322877745c05SJosef Bacik 			spin_unlock(&cur_trans->dirty_bgs_lock);
322977745c05SJosef Bacik 			list_del_init(&cache->io_list);
323077745c05SJosef Bacik 			btrfs_wait_cache_io(trans, cache, path);
323177745c05SJosef Bacik 			btrfs_put_block_group(cache);
323277745c05SJosef Bacik 			spin_lock(&cur_trans->dirty_bgs_lock);
323377745c05SJosef Bacik 		}
323477745c05SJosef Bacik 
323577745c05SJosef Bacik 		/*
323677745c05SJosef Bacik 		 * Don't remove from the dirty list until after we've waited on
323777745c05SJosef Bacik 		 * any pending IO
323877745c05SJosef Bacik 		 */
323977745c05SJosef Bacik 		list_del_init(&cache->dirty_list);
324077745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
324177745c05SJosef Bacik 		should_put = 1;
324277745c05SJosef Bacik 
324377745c05SJosef Bacik 		cache_save_setup(cache, trans, path);
324477745c05SJosef Bacik 
324577745c05SJosef Bacik 		if (!ret)
324677745c05SJosef Bacik 			ret = btrfs_run_delayed_refs(trans,
324777745c05SJosef Bacik 						     (unsigned long) -1);
324877745c05SJosef Bacik 
324977745c05SJosef Bacik 		if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
325077745c05SJosef Bacik 			cache->io_ctl.inode = NULL;
325177745c05SJosef Bacik 			ret = btrfs_write_out_cache(trans, cache, path);
325277745c05SJosef Bacik 			if (ret == 0 && cache->io_ctl.inode) {
325377745c05SJosef Bacik 				should_put = 0;
325477745c05SJosef Bacik 				list_add_tail(&cache->io_list, io);
325577745c05SJosef Bacik 			} else {
325677745c05SJosef Bacik 				/*
325777745c05SJosef Bacik 				 * If we failed to write the cache, the
325877745c05SJosef Bacik 				 * generation will be bad and life goes on
325977745c05SJosef Bacik 				 */
326077745c05SJosef Bacik 				ret = 0;
326177745c05SJosef Bacik 			}
326277745c05SJosef Bacik 		}
326377745c05SJosef Bacik 		if (!ret) {
32643be4d8efSQu Wenruo 			ret = update_block_group_item(trans, path, cache);
326577745c05SJosef Bacik 			/*
326677745c05SJosef Bacik 			 * One of the free space endio workers might have
326777745c05SJosef Bacik 			 * created a new block group while updating a free space
326877745c05SJosef Bacik 			 * cache's inode (at inode.c:btrfs_finish_ordered_io())
326977745c05SJosef Bacik 			 * and hasn't released its transaction handle yet, in
327077745c05SJosef Bacik 			 * which case the new block group is still attached to
327177745c05SJosef Bacik 			 * its transaction handle and its creation has not
327277745c05SJosef Bacik 			 * finished yet (no block group item in the extent tree
327377745c05SJosef Bacik 			 * yet, etc). If this is the case, wait for all free
327477745c05SJosef Bacik 			 * space endio workers to finish and retry. This is a
3275260db43cSRandy Dunlap 			 * very rare case so no need for a more efficient and
327677745c05SJosef Bacik 			 * complex approach.
327777745c05SJosef Bacik 			 */
327877745c05SJosef Bacik 			if (ret == -ENOENT) {
327977745c05SJosef Bacik 				wait_event(cur_trans->writer_wait,
328077745c05SJosef Bacik 				   atomic_read(&cur_trans->num_writers) == 1);
32813be4d8efSQu Wenruo 				ret = update_block_group_item(trans, path, cache);
328277745c05SJosef Bacik 			}
328377745c05SJosef Bacik 			if (ret)
328477745c05SJosef Bacik 				btrfs_abort_transaction(trans, ret);
328577745c05SJosef Bacik 		}
328677745c05SJosef Bacik 
328777745c05SJosef Bacik 		/* If its not on the io list, we need to put the block group */
328877745c05SJosef Bacik 		if (should_put)
328977745c05SJosef Bacik 			btrfs_put_block_group(cache);
329077745c05SJosef Bacik 		btrfs_delayed_refs_rsv_release(fs_info, 1);
329177745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
329277745c05SJosef Bacik 	}
329377745c05SJosef Bacik 	spin_unlock(&cur_trans->dirty_bgs_lock);
329477745c05SJosef Bacik 
329577745c05SJosef Bacik 	/*
329677745c05SJosef Bacik 	 * Refer to the definition of io_bgs member for details why it's safe
329777745c05SJosef Bacik 	 * to use it without any locking
329877745c05SJosef Bacik 	 */
329977745c05SJosef Bacik 	while (!list_empty(io)) {
330032da5386SDavid Sterba 		cache = list_first_entry(io, struct btrfs_block_group,
330177745c05SJosef Bacik 					 io_list);
330277745c05SJosef Bacik 		list_del_init(&cache->io_list);
330377745c05SJosef Bacik 		btrfs_wait_cache_io(trans, cache, path);
330477745c05SJosef Bacik 		btrfs_put_block_group(cache);
330577745c05SJosef Bacik 	}
330677745c05SJosef Bacik 
330777745c05SJosef Bacik 	btrfs_free_path(path);
330877745c05SJosef Bacik 	return ret;
330977745c05SJosef Bacik }
3310606d1bf1SJosef Bacik 
3311606d1bf1SJosef Bacik int btrfs_update_block_group(struct btrfs_trans_handle *trans,
331211b66fa6SAnand Jain 			     u64 bytenr, u64 num_bytes, bool alloc)
3313606d1bf1SJosef Bacik {
3314606d1bf1SJosef Bacik 	struct btrfs_fs_info *info = trans->fs_info;
331532da5386SDavid Sterba 	struct btrfs_block_group *cache = NULL;
3316606d1bf1SJosef Bacik 	u64 total = num_bytes;
3317606d1bf1SJosef Bacik 	u64 old_val;
3318606d1bf1SJosef Bacik 	u64 byte_in_group;
3319606d1bf1SJosef Bacik 	int factor;
3320606d1bf1SJosef Bacik 	int ret = 0;
3321606d1bf1SJosef Bacik 
3322606d1bf1SJosef Bacik 	/* Block accounting for super block */
3323606d1bf1SJosef Bacik 	spin_lock(&info->delalloc_root_lock);
3324606d1bf1SJosef Bacik 	old_val = btrfs_super_bytes_used(info->super_copy);
3325606d1bf1SJosef Bacik 	if (alloc)
3326606d1bf1SJosef Bacik 		old_val += num_bytes;
3327606d1bf1SJosef Bacik 	else
3328606d1bf1SJosef Bacik 		old_val -= num_bytes;
3329606d1bf1SJosef Bacik 	btrfs_set_super_bytes_used(info->super_copy, old_val);
3330606d1bf1SJosef Bacik 	spin_unlock(&info->delalloc_root_lock);
3331606d1bf1SJosef Bacik 
3332606d1bf1SJosef Bacik 	while (total) {
3333ac2f1e63SJosef Bacik 		bool reclaim;
3334ac2f1e63SJosef Bacik 
3335606d1bf1SJosef Bacik 		cache = btrfs_lookup_block_group(info, bytenr);
3336606d1bf1SJosef Bacik 		if (!cache) {
3337606d1bf1SJosef Bacik 			ret = -ENOENT;
3338606d1bf1SJosef Bacik 			break;
3339606d1bf1SJosef Bacik 		}
3340606d1bf1SJosef Bacik 		factor = btrfs_bg_type_to_factor(cache->flags);
3341606d1bf1SJosef Bacik 
3342606d1bf1SJosef Bacik 		/*
3343606d1bf1SJosef Bacik 		 * If this block group has free space cache written out, we
3344606d1bf1SJosef Bacik 		 * need to make sure to load it if we are removing space.  This
3345606d1bf1SJosef Bacik 		 * is because we need the unpinning stage to actually add the
3346606d1bf1SJosef Bacik 		 * space back to the block group, otherwise we will leak space.
3347606d1bf1SJosef Bacik 		 */
334832da5386SDavid Sterba 		if (!alloc && !btrfs_block_group_done(cache))
3349ced8ecf0SOmar Sandoval 			btrfs_cache_block_group(cache, true);
3350606d1bf1SJosef Bacik 
3351b3470b5dSDavid Sterba 		byte_in_group = bytenr - cache->start;
3352b3470b5dSDavid Sterba 		WARN_ON(byte_in_group > cache->length);
3353606d1bf1SJosef Bacik 
3354606d1bf1SJosef Bacik 		spin_lock(&cache->space_info->lock);
3355606d1bf1SJosef Bacik 		spin_lock(&cache->lock);
3356606d1bf1SJosef Bacik 
3357606d1bf1SJosef Bacik 		if (btrfs_test_opt(info, SPACE_CACHE) &&
3358606d1bf1SJosef Bacik 		    cache->disk_cache_state < BTRFS_DC_CLEAR)
3359606d1bf1SJosef Bacik 			cache->disk_cache_state = BTRFS_DC_CLEAR;
3360606d1bf1SJosef Bacik 
3361bf38be65SDavid Sterba 		old_val = cache->used;
3362b3470b5dSDavid Sterba 		num_bytes = min(total, cache->length - byte_in_group);
3363606d1bf1SJosef Bacik 		if (alloc) {
3364606d1bf1SJosef Bacik 			old_val += num_bytes;
3365bf38be65SDavid Sterba 			cache->used = old_val;
3366606d1bf1SJosef Bacik 			cache->reserved -= num_bytes;
3367606d1bf1SJosef Bacik 			cache->space_info->bytes_reserved -= num_bytes;
3368606d1bf1SJosef Bacik 			cache->space_info->bytes_used += num_bytes;
3369606d1bf1SJosef Bacik 			cache->space_info->disk_used += num_bytes * factor;
3370606d1bf1SJosef Bacik 			spin_unlock(&cache->lock);
3371606d1bf1SJosef Bacik 			spin_unlock(&cache->space_info->lock);
3372606d1bf1SJosef Bacik 		} else {
3373606d1bf1SJosef Bacik 			old_val -= num_bytes;
3374bf38be65SDavid Sterba 			cache->used = old_val;
3375606d1bf1SJosef Bacik 			cache->pinned += num_bytes;
3376606d1bf1SJosef Bacik 			btrfs_space_info_update_bytes_pinned(info,
3377606d1bf1SJosef Bacik 					cache->space_info, num_bytes);
3378606d1bf1SJosef Bacik 			cache->space_info->bytes_used -= num_bytes;
3379606d1bf1SJosef Bacik 			cache->space_info->disk_used -= num_bytes * factor;
3380ac2f1e63SJosef Bacik 
3381ac2f1e63SJosef Bacik 			reclaim = should_reclaim_block_group(cache, num_bytes);
3382606d1bf1SJosef Bacik 			spin_unlock(&cache->lock);
3383606d1bf1SJosef Bacik 			spin_unlock(&cache->space_info->lock);
3384606d1bf1SJosef Bacik 
3385fe119a6eSNikolay Borisov 			set_extent_dirty(&trans->transaction->pinned_extents,
3386606d1bf1SJosef Bacik 					 bytenr, bytenr + num_bytes - 1,
3387606d1bf1SJosef Bacik 					 GFP_NOFS | __GFP_NOFAIL);
3388606d1bf1SJosef Bacik 		}
3389606d1bf1SJosef Bacik 
3390606d1bf1SJosef Bacik 		spin_lock(&trans->transaction->dirty_bgs_lock);
3391606d1bf1SJosef Bacik 		if (list_empty(&cache->dirty_list)) {
3392606d1bf1SJosef Bacik 			list_add_tail(&cache->dirty_list,
3393606d1bf1SJosef Bacik 				      &trans->transaction->dirty_bgs);
3394606d1bf1SJosef Bacik 			trans->delayed_ref_updates++;
3395606d1bf1SJosef Bacik 			btrfs_get_block_group(cache);
3396606d1bf1SJosef Bacik 		}
3397606d1bf1SJosef Bacik 		spin_unlock(&trans->transaction->dirty_bgs_lock);
3398606d1bf1SJosef Bacik 
3399606d1bf1SJosef Bacik 		/*
3400606d1bf1SJosef Bacik 		 * No longer have used bytes in this block group, queue it for
3401606d1bf1SJosef Bacik 		 * deletion. We do this after adding the block group to the
3402606d1bf1SJosef Bacik 		 * dirty list to avoid races between cleaner kthread and space
3403606d1bf1SJosef Bacik 		 * cache writeout.
3404606d1bf1SJosef Bacik 		 */
34056e80d4f8SDennis Zhou 		if (!alloc && old_val == 0) {
34066e80d4f8SDennis Zhou 			if (!btrfs_test_opt(info, DISCARD_ASYNC))
3407606d1bf1SJosef Bacik 				btrfs_mark_bg_unused(cache);
3408ac2f1e63SJosef Bacik 		} else if (!alloc && reclaim) {
3409ac2f1e63SJosef Bacik 			btrfs_mark_bg_to_reclaim(cache);
34106e80d4f8SDennis Zhou 		}
3411606d1bf1SJosef Bacik 
3412606d1bf1SJosef Bacik 		btrfs_put_block_group(cache);
3413606d1bf1SJosef Bacik 		total -= num_bytes;
3414606d1bf1SJosef Bacik 		bytenr += num_bytes;
3415606d1bf1SJosef Bacik 	}
3416606d1bf1SJosef Bacik 
3417606d1bf1SJosef Bacik 	/* Modified block groups are accounted for in the delayed_refs_rsv. */
3418606d1bf1SJosef Bacik 	btrfs_update_delayed_refs_rsv(trans);
3419606d1bf1SJosef Bacik 	return ret;
3420606d1bf1SJosef Bacik }
3421606d1bf1SJosef Bacik 
342243dd529aSDavid Sterba /*
342343dd529aSDavid Sterba  * Update the block_group and space info counters.
342443dd529aSDavid Sterba  *
3425606d1bf1SJosef Bacik  * @cache:	The cache we are manipulating
3426606d1bf1SJosef Bacik  * @ram_bytes:  The number of bytes of file content, and will be same to
3427606d1bf1SJosef Bacik  *              @num_bytes except for the compress path.
3428606d1bf1SJosef Bacik  * @num_bytes:	The number of bytes in question
3429606d1bf1SJosef Bacik  * @delalloc:   The blocks are allocated for the delalloc write
3430606d1bf1SJosef Bacik  *
3431606d1bf1SJosef Bacik  * This is called by the allocator when it reserves space. If this is a
3432606d1bf1SJosef Bacik  * reservation and the block group has become read only we cannot make the
3433606d1bf1SJosef Bacik  * reservation and return -EAGAIN, otherwise this function always succeeds.
3434606d1bf1SJosef Bacik  */
343532da5386SDavid Sterba int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
3436606d1bf1SJosef Bacik 			     u64 ram_bytes, u64 num_bytes, int delalloc)
3437606d1bf1SJosef Bacik {
3438606d1bf1SJosef Bacik 	struct btrfs_space_info *space_info = cache->space_info;
3439606d1bf1SJosef Bacik 	int ret = 0;
3440606d1bf1SJosef Bacik 
3441606d1bf1SJosef Bacik 	spin_lock(&space_info->lock);
3442606d1bf1SJosef Bacik 	spin_lock(&cache->lock);
3443606d1bf1SJosef Bacik 	if (cache->ro) {
3444606d1bf1SJosef Bacik 		ret = -EAGAIN;
3445606d1bf1SJosef Bacik 	} else {
3446606d1bf1SJosef Bacik 		cache->reserved += num_bytes;
3447606d1bf1SJosef Bacik 		space_info->bytes_reserved += num_bytes;
3448a43c3835SJosef Bacik 		trace_btrfs_space_reservation(cache->fs_info, "space_info",
3449a43c3835SJosef Bacik 					      space_info->flags, num_bytes, 1);
3450606d1bf1SJosef Bacik 		btrfs_space_info_update_bytes_may_use(cache->fs_info,
3451606d1bf1SJosef Bacik 						      space_info, -ram_bytes);
3452606d1bf1SJosef Bacik 		if (delalloc)
3453606d1bf1SJosef Bacik 			cache->delalloc_bytes += num_bytes;
345499ffb43eSJosef Bacik 
345599ffb43eSJosef Bacik 		/*
345699ffb43eSJosef Bacik 		 * Compression can use less space than we reserved, so wake
345799ffb43eSJosef Bacik 		 * tickets if that happens
345899ffb43eSJosef Bacik 		 */
345999ffb43eSJosef Bacik 		if (num_bytes < ram_bytes)
346099ffb43eSJosef Bacik 			btrfs_try_granting_tickets(cache->fs_info, space_info);
3461606d1bf1SJosef Bacik 	}
3462606d1bf1SJosef Bacik 	spin_unlock(&cache->lock);
3463606d1bf1SJosef Bacik 	spin_unlock(&space_info->lock);
3464606d1bf1SJosef Bacik 	return ret;
3465606d1bf1SJosef Bacik }
3466606d1bf1SJosef Bacik 
346743dd529aSDavid Sterba /*
346843dd529aSDavid Sterba  * Update the block_group and space info counters.
346943dd529aSDavid Sterba  *
3470606d1bf1SJosef Bacik  * @cache:      The cache we are manipulating
3471606d1bf1SJosef Bacik  * @num_bytes:  The number of bytes in question
3472606d1bf1SJosef Bacik  * @delalloc:   The blocks are allocated for the delalloc write
3473606d1bf1SJosef Bacik  *
3474606d1bf1SJosef Bacik  * This is called by somebody who is freeing space that was never actually used
3475606d1bf1SJosef Bacik  * on disk.  For example if you reserve some space for a new leaf in transaction
3476606d1bf1SJosef Bacik  * A and before transaction A commits you free that leaf, you call this with
3477606d1bf1SJosef Bacik  * reserve set to 0 in order to clear the reservation.
3478606d1bf1SJosef Bacik  */
347932da5386SDavid Sterba void btrfs_free_reserved_bytes(struct btrfs_block_group *cache,
3480606d1bf1SJosef Bacik 			       u64 num_bytes, int delalloc)
3481606d1bf1SJosef Bacik {
3482606d1bf1SJosef Bacik 	struct btrfs_space_info *space_info = cache->space_info;
3483606d1bf1SJosef Bacik 
3484606d1bf1SJosef Bacik 	spin_lock(&space_info->lock);
3485606d1bf1SJosef Bacik 	spin_lock(&cache->lock);
3486606d1bf1SJosef Bacik 	if (cache->ro)
3487606d1bf1SJosef Bacik 		space_info->bytes_readonly += num_bytes;
3488606d1bf1SJosef Bacik 	cache->reserved -= num_bytes;
3489606d1bf1SJosef Bacik 	space_info->bytes_reserved -= num_bytes;
3490606d1bf1SJosef Bacik 	space_info->max_extent_size = 0;
3491606d1bf1SJosef Bacik 
3492606d1bf1SJosef Bacik 	if (delalloc)
3493606d1bf1SJosef Bacik 		cache->delalloc_bytes -= num_bytes;
3494606d1bf1SJosef Bacik 	spin_unlock(&cache->lock);
34953308234aSJosef Bacik 
34963308234aSJosef Bacik 	btrfs_try_granting_tickets(cache->fs_info, space_info);
3497606d1bf1SJosef Bacik 	spin_unlock(&space_info->lock);
3498606d1bf1SJosef Bacik }
349907730d87SJosef Bacik 
350007730d87SJosef Bacik static void force_metadata_allocation(struct btrfs_fs_info *info)
350107730d87SJosef Bacik {
350207730d87SJosef Bacik 	struct list_head *head = &info->space_info;
350307730d87SJosef Bacik 	struct btrfs_space_info *found;
350407730d87SJosef Bacik 
350572804905SJosef Bacik 	list_for_each_entry(found, head, list) {
350607730d87SJosef Bacik 		if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
350707730d87SJosef Bacik 			found->force_alloc = CHUNK_ALLOC_FORCE;
350807730d87SJosef Bacik 	}
350907730d87SJosef Bacik }
351007730d87SJosef Bacik 
351107730d87SJosef Bacik static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
351207730d87SJosef Bacik 			      struct btrfs_space_info *sinfo, int force)
351307730d87SJosef Bacik {
351407730d87SJosef Bacik 	u64 bytes_used = btrfs_space_info_used(sinfo, false);
351507730d87SJosef Bacik 	u64 thresh;
351607730d87SJosef Bacik 
351707730d87SJosef Bacik 	if (force == CHUNK_ALLOC_FORCE)
351807730d87SJosef Bacik 		return 1;
351907730d87SJosef Bacik 
352007730d87SJosef Bacik 	/*
352107730d87SJosef Bacik 	 * in limited mode, we want to have some free space up to
352207730d87SJosef Bacik 	 * about 1% of the FS size.
352307730d87SJosef Bacik 	 */
352407730d87SJosef Bacik 	if (force == CHUNK_ALLOC_LIMITED) {
352507730d87SJosef Bacik 		thresh = btrfs_super_total_bytes(fs_info->super_copy);
3526*428c8e03SDavid Sterba 		thresh = max_t(u64, SZ_64M, mult_perc(thresh, 1));
352707730d87SJosef Bacik 
352807730d87SJosef Bacik 		if (sinfo->total_bytes - bytes_used < thresh)
352907730d87SJosef Bacik 			return 1;
353007730d87SJosef Bacik 	}
353107730d87SJosef Bacik 
3532*428c8e03SDavid Sterba 	if (bytes_used + SZ_2M < mult_perc(sinfo->total_bytes, 80))
353307730d87SJosef Bacik 		return 0;
353407730d87SJosef Bacik 	return 1;
353507730d87SJosef Bacik }
353607730d87SJosef Bacik 
353707730d87SJosef Bacik int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
353807730d87SJosef Bacik {
353907730d87SJosef Bacik 	u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type);
354007730d87SJosef Bacik 
354107730d87SJosef Bacik 	return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
354207730d87SJosef Bacik }
354307730d87SJosef Bacik 
3544820c363bSNaohiro Aota static struct btrfs_block_group *do_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags)
354579bd3712SFilipe Manana {
354679bd3712SFilipe Manana 	struct btrfs_block_group *bg;
354779bd3712SFilipe Manana 	int ret;
354879bd3712SFilipe Manana 
354907730d87SJosef Bacik 	/*
355079bd3712SFilipe Manana 	 * Check if we have enough space in the system space info because we
355179bd3712SFilipe Manana 	 * will need to update device items in the chunk btree and insert a new
355279bd3712SFilipe Manana 	 * chunk item in the chunk btree as well. This will allocate a new
355379bd3712SFilipe Manana 	 * system block group if needed.
355479bd3712SFilipe Manana 	 */
355579bd3712SFilipe Manana 	check_system_chunk(trans, flags);
355679bd3712SFilipe Manana 
3557f6f39f7aSNikolay Borisov 	bg = btrfs_create_chunk(trans, flags);
355879bd3712SFilipe Manana 	if (IS_ERR(bg)) {
355979bd3712SFilipe Manana 		ret = PTR_ERR(bg);
356079bd3712SFilipe Manana 		goto out;
356179bd3712SFilipe Manana 	}
356279bd3712SFilipe Manana 
356379bd3712SFilipe Manana 	ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
356479bd3712SFilipe Manana 	/*
356579bd3712SFilipe Manana 	 * Normally we are not expected to fail with -ENOSPC here, since we have
356679bd3712SFilipe Manana 	 * previously reserved space in the system space_info and allocated one
3567ecd84d54SFilipe Manana 	 * new system chunk if necessary. However there are three exceptions:
356879bd3712SFilipe Manana 	 *
356979bd3712SFilipe Manana 	 * 1) We may have enough free space in the system space_info but all the
357079bd3712SFilipe Manana 	 *    existing system block groups have a profile which can not be used
357179bd3712SFilipe Manana 	 *    for extent allocation.
357279bd3712SFilipe Manana 	 *
357379bd3712SFilipe Manana 	 *    This happens when mounting in degraded mode. For example we have a
357479bd3712SFilipe Manana 	 *    RAID1 filesystem with 2 devices, lose one device and mount the fs
357579bd3712SFilipe Manana 	 *    using the other device in degraded mode. If we then allocate a chunk,
357679bd3712SFilipe Manana 	 *    we may have enough free space in the existing system space_info, but
357779bd3712SFilipe Manana 	 *    none of the block groups can be used for extent allocation since they
357879bd3712SFilipe Manana 	 *    have a RAID1 profile, and because we are in degraded mode with a
357979bd3712SFilipe Manana 	 *    single device, we are forced to allocate a new system chunk with a
358079bd3712SFilipe Manana 	 *    SINGLE profile. Making check_system_chunk() iterate over all system
358179bd3712SFilipe Manana 	 *    block groups and check if they have a usable profile and enough space
358279bd3712SFilipe Manana 	 *    can be slow on very large filesystems, so we tolerate the -ENOSPC and
358379bd3712SFilipe Manana 	 *    try again after forcing allocation of a new system chunk. Like this
358479bd3712SFilipe Manana 	 *    we avoid paying the cost of that search in normal circumstances, when
358579bd3712SFilipe Manana 	 *    we were not mounted in degraded mode;
358679bd3712SFilipe Manana 	 *
358779bd3712SFilipe Manana 	 * 2) We had enough free space info the system space_info, and one suitable
358879bd3712SFilipe Manana 	 *    block group to allocate from when we called check_system_chunk()
358979bd3712SFilipe Manana 	 *    above. However right after we called it, the only system block group
359079bd3712SFilipe Manana 	 *    with enough free space got turned into RO mode by a running scrub,
359179bd3712SFilipe Manana 	 *    and in this case we have to allocate a new one and retry. We only
359279bd3712SFilipe Manana 	 *    need do this allocate and retry once, since we have a transaction
3593ecd84d54SFilipe Manana 	 *    handle and scrub uses the commit root to search for block groups;
3594ecd84d54SFilipe Manana 	 *
3595ecd84d54SFilipe Manana 	 * 3) We had one system block group with enough free space when we called
3596ecd84d54SFilipe Manana 	 *    check_system_chunk(), but after that, right before we tried to
3597ecd84d54SFilipe Manana 	 *    allocate the last extent buffer we needed, a discard operation came
3598ecd84d54SFilipe Manana 	 *    in and it temporarily removed the last free space entry from the
3599ecd84d54SFilipe Manana 	 *    block group (discard removes a free space entry, discards it, and
3600ecd84d54SFilipe Manana 	 *    then adds back the entry to the block group cache).
360179bd3712SFilipe Manana 	 */
360279bd3712SFilipe Manana 	if (ret == -ENOSPC) {
360379bd3712SFilipe Manana 		const u64 sys_flags = btrfs_system_alloc_profile(trans->fs_info);
360479bd3712SFilipe Manana 		struct btrfs_block_group *sys_bg;
360579bd3712SFilipe Manana 
3606f6f39f7aSNikolay Borisov 		sys_bg = btrfs_create_chunk(trans, sys_flags);
360779bd3712SFilipe Manana 		if (IS_ERR(sys_bg)) {
360879bd3712SFilipe Manana 			ret = PTR_ERR(sys_bg);
360979bd3712SFilipe Manana 			btrfs_abort_transaction(trans, ret);
361079bd3712SFilipe Manana 			goto out;
361179bd3712SFilipe Manana 		}
361279bd3712SFilipe Manana 
361379bd3712SFilipe Manana 		ret = btrfs_chunk_alloc_add_chunk_item(trans, sys_bg);
361479bd3712SFilipe Manana 		if (ret) {
361579bd3712SFilipe Manana 			btrfs_abort_transaction(trans, ret);
361679bd3712SFilipe Manana 			goto out;
361779bd3712SFilipe Manana 		}
361879bd3712SFilipe Manana 
361979bd3712SFilipe Manana 		ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
362079bd3712SFilipe Manana 		if (ret) {
362179bd3712SFilipe Manana 			btrfs_abort_transaction(trans, ret);
362279bd3712SFilipe Manana 			goto out;
362379bd3712SFilipe Manana 		}
362479bd3712SFilipe Manana 	} else if (ret) {
362579bd3712SFilipe Manana 		btrfs_abort_transaction(trans, ret);
362679bd3712SFilipe Manana 		goto out;
362779bd3712SFilipe Manana 	}
362879bd3712SFilipe Manana out:
362979bd3712SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
363079bd3712SFilipe Manana 
3631820c363bSNaohiro Aota 	if (ret)
3632820c363bSNaohiro Aota 		return ERR_PTR(ret);
3633820c363bSNaohiro Aota 
3634820c363bSNaohiro Aota 	btrfs_get_block_group(bg);
3635820c363bSNaohiro Aota 	return bg;
363679bd3712SFilipe Manana }
363779bd3712SFilipe Manana 
363879bd3712SFilipe Manana /*
363979bd3712SFilipe Manana  * Chunk allocation is done in 2 phases:
364079bd3712SFilipe Manana  *
364179bd3712SFilipe Manana  * 1) Phase 1 - through btrfs_chunk_alloc() we allocate device extents for
364279bd3712SFilipe Manana  *    the chunk, the chunk mapping, create its block group and add the items
364379bd3712SFilipe Manana  *    that belong in the chunk btree to it - more specifically, we need to
364479bd3712SFilipe Manana  *    update device items in the chunk btree and add a new chunk item to it.
364579bd3712SFilipe Manana  *
364679bd3712SFilipe Manana  * 2) Phase 2 - through btrfs_create_pending_block_groups(), we add the block
364779bd3712SFilipe Manana  *    group item to the extent btree and the device extent items to the devices
364879bd3712SFilipe Manana  *    btree.
364979bd3712SFilipe Manana  *
365079bd3712SFilipe Manana  * This is done to prevent deadlocks. For example when COWing a node from the
365179bd3712SFilipe Manana  * extent btree we are holding a write lock on the node's parent and if we
365279bd3712SFilipe Manana  * trigger chunk allocation and attempted to insert the new block group item
365379bd3712SFilipe Manana  * in the extent btree right way, we could deadlock because the path for the
365479bd3712SFilipe Manana  * insertion can include that parent node. At first glance it seems impossible
365579bd3712SFilipe Manana  * to trigger chunk allocation after starting a transaction since tasks should
365679bd3712SFilipe Manana  * reserve enough transaction units (metadata space), however while that is true
365779bd3712SFilipe Manana  * most of the time, chunk allocation may still be triggered for several reasons:
365879bd3712SFilipe Manana  *
365979bd3712SFilipe Manana  * 1) When reserving metadata, we check if there is enough free space in the
366079bd3712SFilipe Manana  *    metadata space_info and therefore don't trigger allocation of a new chunk.
366179bd3712SFilipe Manana  *    However later when the task actually tries to COW an extent buffer from
366279bd3712SFilipe Manana  *    the extent btree or from the device btree for example, it is forced to
366379bd3712SFilipe Manana  *    allocate a new block group (chunk) because the only one that had enough
366479bd3712SFilipe Manana  *    free space was just turned to RO mode by a running scrub for example (or
366579bd3712SFilipe Manana  *    device replace, block group reclaim thread, etc), so we can not use it
366679bd3712SFilipe Manana  *    for allocating an extent and end up being forced to allocate a new one;
366779bd3712SFilipe Manana  *
366879bd3712SFilipe Manana  * 2) Because we only check that the metadata space_info has enough free bytes,
366979bd3712SFilipe Manana  *    we end up not allocating a new metadata chunk in that case. However if
367079bd3712SFilipe Manana  *    the filesystem was mounted in degraded mode, none of the existing block
367179bd3712SFilipe Manana  *    groups might be suitable for extent allocation due to their incompatible
367279bd3712SFilipe Manana  *    profile (for e.g. mounting a 2 devices filesystem, where all block groups
367379bd3712SFilipe Manana  *    use a RAID1 profile, in degraded mode using a single device). In this case
367479bd3712SFilipe Manana  *    when the task attempts to COW some extent buffer of the extent btree for
367579bd3712SFilipe Manana  *    example, it will trigger allocation of a new metadata block group with a
367679bd3712SFilipe Manana  *    suitable profile (SINGLE profile in the example of the degraded mount of
367779bd3712SFilipe Manana  *    the RAID1 filesystem);
367879bd3712SFilipe Manana  *
367979bd3712SFilipe Manana  * 3) The task has reserved enough transaction units / metadata space, but when
368079bd3712SFilipe Manana  *    it attempts to COW an extent buffer from the extent or device btree for
368179bd3712SFilipe Manana  *    example, it does not find any free extent in any metadata block group,
368279bd3712SFilipe Manana  *    therefore forced to try to allocate a new metadata block group.
368379bd3712SFilipe Manana  *    This is because some other task allocated all available extents in the
368479bd3712SFilipe Manana  *    meanwhile - this typically happens with tasks that don't reserve space
368579bd3712SFilipe Manana  *    properly, either intentionally or as a bug. One example where this is
368679bd3712SFilipe Manana  *    done intentionally is fsync, as it does not reserve any transaction units
368779bd3712SFilipe Manana  *    and ends up allocating a variable number of metadata extents for log
3688ecd84d54SFilipe Manana  *    tree extent buffers;
3689ecd84d54SFilipe Manana  *
3690ecd84d54SFilipe Manana  * 4) The task has reserved enough transaction units / metadata space, but right
3691ecd84d54SFilipe Manana  *    before it tries to allocate the last extent buffer it needs, a discard
3692ecd84d54SFilipe Manana  *    operation comes in and, temporarily, removes the last free space entry from
3693ecd84d54SFilipe Manana  *    the only metadata block group that had free space (discard starts by
3694ecd84d54SFilipe Manana  *    removing a free space entry from a block group, then does the discard
3695ecd84d54SFilipe Manana  *    operation and, once it's done, it adds back the free space entry to the
3696ecd84d54SFilipe Manana  *    block group).
369779bd3712SFilipe Manana  *
369879bd3712SFilipe Manana  * We also need this 2 phases setup when adding a device to a filesystem with
369979bd3712SFilipe Manana  * a seed device - we must create new metadata and system chunks without adding
370079bd3712SFilipe Manana  * any of the block group items to the chunk, extent and device btrees. If we
370179bd3712SFilipe Manana  * did not do it this way, we would get ENOSPC when attempting to update those
370279bd3712SFilipe Manana  * btrees, since all the chunks from the seed device are read-only.
370379bd3712SFilipe Manana  *
370479bd3712SFilipe Manana  * Phase 1 does the updates and insertions to the chunk btree because if we had
370579bd3712SFilipe Manana  * it done in phase 2 and have a thundering herd of tasks allocating chunks in
370679bd3712SFilipe Manana  * parallel, we risk having too many system chunks allocated by many tasks if
370779bd3712SFilipe Manana  * many tasks reach phase 1 without the previous ones completing phase 2. In the
370879bd3712SFilipe Manana  * extreme case this leads to exhaustion of the system chunk array in the
370979bd3712SFilipe Manana  * superblock. This is easier to trigger if using a btree node/leaf size of 64K
371079bd3712SFilipe Manana  * and with RAID filesystems (so we have more device items in the chunk btree).
371179bd3712SFilipe Manana  * This has happened before and commit eafa4fd0ad0607 ("btrfs: fix exhaustion of
371279bd3712SFilipe Manana  * the system chunk array due to concurrent allocations") provides more details.
371379bd3712SFilipe Manana  *
37142bb2e00eSFilipe Manana  * Allocation of system chunks does not happen through this function. A task that
37152bb2e00eSFilipe Manana  * needs to update the chunk btree (the only btree that uses system chunks), must
37162bb2e00eSFilipe Manana  * preallocate chunk space by calling either check_system_chunk() or
37172bb2e00eSFilipe Manana  * btrfs_reserve_chunk_metadata() - the former is used when allocating a data or
37182bb2e00eSFilipe Manana  * metadata chunk or when removing a chunk, while the later is used before doing
37192bb2e00eSFilipe Manana  * a modification to the chunk btree - use cases for the later are adding,
37202bb2e00eSFilipe Manana  * removing and resizing a device as well as relocation of a system chunk.
37212bb2e00eSFilipe Manana  * See the comment below for more details.
372279bd3712SFilipe Manana  *
372379bd3712SFilipe Manana  * The reservation of system space, done through check_system_chunk(), as well
372479bd3712SFilipe Manana  * as all the updates and insertions into the chunk btree must be done while
372579bd3712SFilipe Manana  * holding fs_info->chunk_mutex. This is important to guarantee that while COWing
372679bd3712SFilipe Manana  * an extent buffer from the chunks btree we never trigger allocation of a new
372779bd3712SFilipe Manana  * system chunk, which would result in a deadlock (trying to lock twice an
372879bd3712SFilipe Manana  * extent buffer of the chunk btree, first time before triggering the chunk
372979bd3712SFilipe Manana  * allocation and the second time during chunk allocation while attempting to
373079bd3712SFilipe Manana  * update the chunks btree). The system chunk array is also updated while holding
373179bd3712SFilipe Manana  * that mutex. The same logic applies to removing chunks - we must reserve system
373279bd3712SFilipe Manana  * space, update the chunk btree and the system chunk array in the superblock
373379bd3712SFilipe Manana  * while holding fs_info->chunk_mutex.
373479bd3712SFilipe Manana  *
373579bd3712SFilipe Manana  * This function, btrfs_chunk_alloc(), belongs to phase 1.
373679bd3712SFilipe Manana  *
373779bd3712SFilipe Manana  * If @force is CHUNK_ALLOC_FORCE:
373807730d87SJosef Bacik  *    - return 1 if it successfully allocates a chunk,
373907730d87SJosef Bacik  *    - return errors including -ENOSPC otherwise.
374079bd3712SFilipe Manana  * If @force is NOT CHUNK_ALLOC_FORCE:
374107730d87SJosef Bacik  *    - return 0 if it doesn't need to allocate a new chunk,
374207730d87SJosef Bacik  *    - return 1 if it successfully allocates a chunk,
374307730d87SJosef Bacik  *    - return errors including -ENOSPC otherwise.
374407730d87SJosef Bacik  */
374507730d87SJosef Bacik int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
374607730d87SJosef Bacik 		      enum btrfs_chunk_alloc_enum force)
374707730d87SJosef Bacik {
374807730d87SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
374907730d87SJosef Bacik 	struct btrfs_space_info *space_info;
3750820c363bSNaohiro Aota 	struct btrfs_block_group *ret_bg;
375107730d87SJosef Bacik 	bool wait_for_alloc = false;
375207730d87SJosef Bacik 	bool should_alloc = false;
3753760e69c4SNaohiro Aota 	bool from_extent_allocation = false;
375407730d87SJosef Bacik 	int ret = 0;
375507730d87SJosef Bacik 
3756760e69c4SNaohiro Aota 	if (force == CHUNK_ALLOC_FORCE_FOR_EXTENT) {
3757760e69c4SNaohiro Aota 		from_extent_allocation = true;
3758760e69c4SNaohiro Aota 		force = CHUNK_ALLOC_FORCE;
3759760e69c4SNaohiro Aota 	}
3760760e69c4SNaohiro Aota 
376107730d87SJosef Bacik 	/* Don't re-enter if we're already allocating a chunk */
376207730d87SJosef Bacik 	if (trans->allocating_chunk)
376307730d87SJosef Bacik 		return -ENOSPC;
376479bd3712SFilipe Manana 	/*
37652bb2e00eSFilipe Manana 	 * Allocation of system chunks can not happen through this path, as we
37662bb2e00eSFilipe Manana 	 * could end up in a deadlock if we are allocating a data or metadata
37672bb2e00eSFilipe Manana 	 * chunk and there is another task modifying the chunk btree.
37682bb2e00eSFilipe Manana 	 *
37692bb2e00eSFilipe Manana 	 * This is because while we are holding the chunk mutex, we will attempt
37702bb2e00eSFilipe Manana 	 * to add the new chunk item to the chunk btree or update an existing
37712bb2e00eSFilipe Manana 	 * device item in the chunk btree, while the other task that is modifying
37722bb2e00eSFilipe Manana 	 * the chunk btree is attempting to COW an extent buffer while holding a
37732bb2e00eSFilipe Manana 	 * lock on it and on its parent - if the COW operation triggers a system
37742bb2e00eSFilipe Manana 	 * chunk allocation, then we can deadlock because we are holding the
37752bb2e00eSFilipe Manana 	 * chunk mutex and we may need to access that extent buffer or its parent
37762bb2e00eSFilipe Manana 	 * in order to add the chunk item or update a device item.
37772bb2e00eSFilipe Manana 	 *
37782bb2e00eSFilipe Manana 	 * Tasks that want to modify the chunk tree should reserve system space
37792bb2e00eSFilipe Manana 	 * before updating the chunk btree, by calling either
37802bb2e00eSFilipe Manana 	 * btrfs_reserve_chunk_metadata() or check_system_chunk().
37812bb2e00eSFilipe Manana 	 * It's possible that after a task reserves the space, it still ends up
37822bb2e00eSFilipe Manana 	 * here - this happens in the cases described above at do_chunk_alloc().
37832bb2e00eSFilipe Manana 	 * The task will have to either retry or fail.
378479bd3712SFilipe Manana 	 */
37852bb2e00eSFilipe Manana 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
378679bd3712SFilipe Manana 		return -ENOSPC;
378707730d87SJosef Bacik 
378807730d87SJosef Bacik 	space_info = btrfs_find_space_info(fs_info, flags);
378907730d87SJosef Bacik 	ASSERT(space_info);
379007730d87SJosef Bacik 
379107730d87SJosef Bacik 	do {
379207730d87SJosef Bacik 		spin_lock(&space_info->lock);
379307730d87SJosef Bacik 		if (force < space_info->force_alloc)
379407730d87SJosef Bacik 			force = space_info->force_alloc;
379507730d87SJosef Bacik 		should_alloc = should_alloc_chunk(fs_info, space_info, force);
379607730d87SJosef Bacik 		if (space_info->full) {
379707730d87SJosef Bacik 			/* No more free physical space */
379807730d87SJosef Bacik 			if (should_alloc)
379907730d87SJosef Bacik 				ret = -ENOSPC;
380007730d87SJosef Bacik 			else
380107730d87SJosef Bacik 				ret = 0;
380207730d87SJosef Bacik 			spin_unlock(&space_info->lock);
380307730d87SJosef Bacik 			return ret;
380407730d87SJosef Bacik 		} else if (!should_alloc) {
380507730d87SJosef Bacik 			spin_unlock(&space_info->lock);
380607730d87SJosef Bacik 			return 0;
380707730d87SJosef Bacik 		} else if (space_info->chunk_alloc) {
380807730d87SJosef Bacik 			/*
380907730d87SJosef Bacik 			 * Someone is already allocating, so we need to block
381007730d87SJosef Bacik 			 * until this someone is finished and then loop to
381107730d87SJosef Bacik 			 * recheck if we should continue with our allocation
381207730d87SJosef Bacik 			 * attempt.
381307730d87SJosef Bacik 			 */
381407730d87SJosef Bacik 			wait_for_alloc = true;
38151314ca78SJosef Bacik 			force = CHUNK_ALLOC_NO_FORCE;
381607730d87SJosef Bacik 			spin_unlock(&space_info->lock);
381707730d87SJosef Bacik 			mutex_lock(&fs_info->chunk_mutex);
381807730d87SJosef Bacik 			mutex_unlock(&fs_info->chunk_mutex);
381907730d87SJosef Bacik 		} else {
382007730d87SJosef Bacik 			/* Proceed with allocation */
382107730d87SJosef Bacik 			space_info->chunk_alloc = 1;
382207730d87SJosef Bacik 			wait_for_alloc = false;
382307730d87SJosef Bacik 			spin_unlock(&space_info->lock);
382407730d87SJosef Bacik 		}
382507730d87SJosef Bacik 
382607730d87SJosef Bacik 		cond_resched();
382707730d87SJosef Bacik 	} while (wait_for_alloc);
382807730d87SJosef Bacik 
382907730d87SJosef Bacik 	mutex_lock(&fs_info->chunk_mutex);
383007730d87SJosef Bacik 	trans->allocating_chunk = true;
383107730d87SJosef Bacik 
383207730d87SJosef Bacik 	/*
383307730d87SJosef Bacik 	 * If we have mixed data/metadata chunks we want to make sure we keep
383407730d87SJosef Bacik 	 * allocating mixed chunks instead of individual chunks.
383507730d87SJosef Bacik 	 */
383607730d87SJosef Bacik 	if (btrfs_mixed_space_info(space_info))
383707730d87SJosef Bacik 		flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
383807730d87SJosef Bacik 
383907730d87SJosef Bacik 	/*
384007730d87SJosef Bacik 	 * if we're doing a data chunk, go ahead and make sure that
384107730d87SJosef Bacik 	 * we keep a reasonable number of metadata chunks allocated in the
384207730d87SJosef Bacik 	 * FS as well.
384307730d87SJosef Bacik 	 */
384407730d87SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
384507730d87SJosef Bacik 		fs_info->data_chunk_allocations++;
384607730d87SJosef Bacik 		if (!(fs_info->data_chunk_allocations %
384707730d87SJosef Bacik 		      fs_info->metadata_ratio))
384807730d87SJosef Bacik 			force_metadata_allocation(fs_info);
384907730d87SJosef Bacik 	}
385007730d87SJosef Bacik 
3851820c363bSNaohiro Aota 	ret_bg = do_chunk_alloc(trans, flags);
385207730d87SJosef Bacik 	trans->allocating_chunk = false;
385307730d87SJosef Bacik 
3854760e69c4SNaohiro Aota 	if (IS_ERR(ret_bg)) {
3855820c363bSNaohiro Aota 		ret = PTR_ERR(ret_bg);
3856760e69c4SNaohiro Aota 	} else if (from_extent_allocation) {
3857760e69c4SNaohiro Aota 		/*
3858760e69c4SNaohiro Aota 		 * New block group is likely to be used soon. Try to activate
3859760e69c4SNaohiro Aota 		 * it now. Failure is OK for now.
3860760e69c4SNaohiro Aota 		 */
3861760e69c4SNaohiro Aota 		btrfs_zone_activate(ret_bg);
3862760e69c4SNaohiro Aota 	}
3863760e69c4SNaohiro Aota 
3864760e69c4SNaohiro Aota 	if (!ret)
3865820c363bSNaohiro Aota 		btrfs_put_block_group(ret_bg);
3866820c363bSNaohiro Aota 
386707730d87SJosef Bacik 	spin_lock(&space_info->lock);
386807730d87SJosef Bacik 	if (ret < 0) {
386907730d87SJosef Bacik 		if (ret == -ENOSPC)
387007730d87SJosef Bacik 			space_info->full = 1;
387107730d87SJosef Bacik 		else
387207730d87SJosef Bacik 			goto out;
387307730d87SJosef Bacik 	} else {
387407730d87SJosef Bacik 		ret = 1;
387507730d87SJosef Bacik 		space_info->max_extent_size = 0;
387607730d87SJosef Bacik 	}
387707730d87SJosef Bacik 
387807730d87SJosef Bacik 	space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
387907730d87SJosef Bacik out:
388007730d87SJosef Bacik 	space_info->chunk_alloc = 0;
388107730d87SJosef Bacik 	spin_unlock(&space_info->lock);
388207730d87SJosef Bacik 	mutex_unlock(&fs_info->chunk_mutex);
388307730d87SJosef Bacik 
388407730d87SJosef Bacik 	return ret;
388507730d87SJosef Bacik }
388607730d87SJosef Bacik 
388707730d87SJosef Bacik static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
388807730d87SJosef Bacik {
388907730d87SJosef Bacik 	u64 num_dev;
389007730d87SJosef Bacik 
389107730d87SJosef Bacik 	num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max;
389207730d87SJosef Bacik 	if (!num_dev)
389307730d87SJosef Bacik 		num_dev = fs_info->fs_devices->rw_devices;
389407730d87SJosef Bacik 
389507730d87SJosef Bacik 	return num_dev;
389607730d87SJosef Bacik }
389707730d87SJosef Bacik 
38982bb2e00eSFilipe Manana static void reserve_chunk_space(struct btrfs_trans_handle *trans,
38992bb2e00eSFilipe Manana 				u64 bytes,
39002bb2e00eSFilipe Manana 				u64 type)
390107730d87SJosef Bacik {
390207730d87SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
390307730d87SJosef Bacik 	struct btrfs_space_info *info;
390407730d87SJosef Bacik 	u64 left;
390507730d87SJosef Bacik 	int ret = 0;
390607730d87SJosef Bacik 
390707730d87SJosef Bacik 	/*
390807730d87SJosef Bacik 	 * Needed because we can end up allocating a system chunk and for an
390907730d87SJosef Bacik 	 * atomic and race free space reservation in the chunk block reserve.
391007730d87SJosef Bacik 	 */
391107730d87SJosef Bacik 	lockdep_assert_held(&fs_info->chunk_mutex);
391207730d87SJosef Bacik 
391307730d87SJosef Bacik 	info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
391407730d87SJosef Bacik 	spin_lock(&info->lock);
391507730d87SJosef Bacik 	left = info->total_bytes - btrfs_space_info_used(info, true);
391607730d87SJosef Bacik 	spin_unlock(&info->lock);
391707730d87SJosef Bacik 
39182bb2e00eSFilipe Manana 	if (left < bytes && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
391907730d87SJosef Bacik 		btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
39202bb2e00eSFilipe Manana 			   left, bytes, type);
392107730d87SJosef Bacik 		btrfs_dump_space_info(fs_info, info, 0, 0);
392207730d87SJosef Bacik 	}
392307730d87SJosef Bacik 
39242bb2e00eSFilipe Manana 	if (left < bytes) {
392507730d87SJosef Bacik 		u64 flags = btrfs_system_alloc_profile(fs_info);
392679bd3712SFilipe Manana 		struct btrfs_block_group *bg;
392707730d87SJosef Bacik 
392807730d87SJosef Bacik 		/*
392907730d87SJosef Bacik 		 * Ignore failure to create system chunk. We might end up not
393007730d87SJosef Bacik 		 * needing it, as we might not need to COW all nodes/leafs from
393107730d87SJosef Bacik 		 * the paths we visit in the chunk tree (they were already COWed
393207730d87SJosef Bacik 		 * or created in the current transaction for example).
393307730d87SJosef Bacik 		 */
3934f6f39f7aSNikolay Borisov 		bg = btrfs_create_chunk(trans, flags);
393579bd3712SFilipe Manana 		if (IS_ERR(bg)) {
393679bd3712SFilipe Manana 			ret = PTR_ERR(bg);
39372bb2e00eSFilipe Manana 		} else {
393879bd3712SFilipe Manana 			/*
3939b6a98021SNaohiro Aota 			 * We have a new chunk. We also need to activate it for
3940b6a98021SNaohiro Aota 			 * zoned filesystem.
3941b6a98021SNaohiro Aota 			 */
3942b6a98021SNaohiro Aota 			ret = btrfs_zoned_activate_one_bg(fs_info, info, true);
3943b6a98021SNaohiro Aota 			if (ret < 0)
3944b6a98021SNaohiro Aota 				return;
3945b6a98021SNaohiro Aota 
3946b6a98021SNaohiro Aota 			/*
394779bd3712SFilipe Manana 			 * If we fail to add the chunk item here, we end up
394879bd3712SFilipe Manana 			 * trying again at phase 2 of chunk allocation, at
394979bd3712SFilipe Manana 			 * btrfs_create_pending_block_groups(). So ignore
39502bb2e00eSFilipe Manana 			 * any error here. An ENOSPC here could happen, due to
39512bb2e00eSFilipe Manana 			 * the cases described at do_chunk_alloc() - the system
39522bb2e00eSFilipe Manana 			 * block group we just created was just turned into RO
39532bb2e00eSFilipe Manana 			 * mode by a scrub for example, or a running discard
39542bb2e00eSFilipe Manana 			 * temporarily removed its free space entries, etc.
395579bd3712SFilipe Manana 			 */
395679bd3712SFilipe Manana 			btrfs_chunk_alloc_add_chunk_item(trans, bg);
395779bd3712SFilipe Manana 		}
395807730d87SJosef Bacik 	}
395907730d87SJosef Bacik 
396007730d87SJosef Bacik 	if (!ret) {
39619270501cSJosef Bacik 		ret = btrfs_block_rsv_add(fs_info,
396207730d87SJosef Bacik 					  &fs_info->chunk_block_rsv,
39632bb2e00eSFilipe Manana 					  bytes, BTRFS_RESERVE_NO_FLUSH);
39641cb3db1cSFilipe Manana 		if (!ret)
39652bb2e00eSFilipe Manana 			trans->chunk_bytes_reserved += bytes;
396607730d87SJosef Bacik 	}
396707730d87SJosef Bacik }
396807730d87SJosef Bacik 
39692bb2e00eSFilipe Manana /*
39702bb2e00eSFilipe Manana  * Reserve space in the system space for allocating or removing a chunk.
39712bb2e00eSFilipe Manana  * The caller must be holding fs_info->chunk_mutex.
39722bb2e00eSFilipe Manana  */
39732bb2e00eSFilipe Manana void check_system_chunk(struct btrfs_trans_handle *trans, u64 type)
39742bb2e00eSFilipe Manana {
39752bb2e00eSFilipe Manana 	struct btrfs_fs_info *fs_info = trans->fs_info;
39762bb2e00eSFilipe Manana 	const u64 num_devs = get_profile_num_devs(fs_info, type);
39772bb2e00eSFilipe Manana 	u64 bytes;
39782bb2e00eSFilipe Manana 
39792bb2e00eSFilipe Manana 	/* num_devs device items to update and 1 chunk item to add or remove. */
39802bb2e00eSFilipe Manana 	bytes = btrfs_calc_metadata_size(fs_info, num_devs) +
39812bb2e00eSFilipe Manana 		btrfs_calc_insert_metadata_size(fs_info, 1);
39822bb2e00eSFilipe Manana 
39832bb2e00eSFilipe Manana 	reserve_chunk_space(trans, bytes, type);
39842bb2e00eSFilipe Manana }
39852bb2e00eSFilipe Manana 
39862bb2e00eSFilipe Manana /*
39872bb2e00eSFilipe Manana  * Reserve space in the system space, if needed, for doing a modification to the
39882bb2e00eSFilipe Manana  * chunk btree.
39892bb2e00eSFilipe Manana  *
39902bb2e00eSFilipe Manana  * @trans:		A transaction handle.
39912bb2e00eSFilipe Manana  * @is_item_insertion:	Indicate if the modification is for inserting a new item
39922bb2e00eSFilipe Manana  *			in the chunk btree or if it's for the deletion or update
39932bb2e00eSFilipe Manana  *			of an existing item.
39942bb2e00eSFilipe Manana  *
39952bb2e00eSFilipe Manana  * This is used in a context where we need to update the chunk btree outside
39962bb2e00eSFilipe Manana  * block group allocation and removal, to avoid a deadlock with a concurrent
39972bb2e00eSFilipe Manana  * task that is allocating a metadata or data block group and therefore needs to
39982bb2e00eSFilipe Manana  * update the chunk btree while holding the chunk mutex. After the update to the
39992bb2e00eSFilipe Manana  * chunk btree is done, btrfs_trans_release_chunk_metadata() should be called.
40002bb2e00eSFilipe Manana  *
40012bb2e00eSFilipe Manana  */
40022bb2e00eSFilipe Manana void btrfs_reserve_chunk_metadata(struct btrfs_trans_handle *trans,
40032bb2e00eSFilipe Manana 				  bool is_item_insertion)
40042bb2e00eSFilipe Manana {
40052bb2e00eSFilipe Manana 	struct btrfs_fs_info *fs_info = trans->fs_info;
40062bb2e00eSFilipe Manana 	u64 bytes;
40072bb2e00eSFilipe Manana 
40082bb2e00eSFilipe Manana 	if (is_item_insertion)
40092bb2e00eSFilipe Manana 		bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
40102bb2e00eSFilipe Manana 	else
40112bb2e00eSFilipe Manana 		bytes = btrfs_calc_metadata_size(fs_info, 1);
40122bb2e00eSFilipe Manana 
40132bb2e00eSFilipe Manana 	mutex_lock(&fs_info->chunk_mutex);
40142bb2e00eSFilipe Manana 	reserve_chunk_space(trans, bytes, BTRFS_BLOCK_GROUP_SYSTEM);
40152bb2e00eSFilipe Manana 	mutex_unlock(&fs_info->chunk_mutex);
40162bb2e00eSFilipe Manana }
40172bb2e00eSFilipe Manana 
40183e43c279SJosef Bacik void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
40193e43c279SJosef Bacik {
402032da5386SDavid Sterba 	struct btrfs_block_group *block_group;
40213e43c279SJosef Bacik 
402250c31eaaSJosef Bacik 	block_group = btrfs_lookup_first_block_group(info, 0);
40233e43c279SJosef Bacik 	while (block_group) {
40243e43c279SJosef Bacik 		btrfs_wait_block_group_cache_done(block_group);
40253e43c279SJosef Bacik 		spin_lock(&block_group->lock);
402650c31eaaSJosef Bacik 		if (test_and_clear_bit(BLOCK_GROUP_FLAG_IREF,
402750c31eaaSJosef Bacik 				       &block_group->runtime_flags)) {
402850c31eaaSJosef Bacik 			struct inode *inode = block_group->inode;
40293e43c279SJosef Bacik 
40303e43c279SJosef Bacik 			block_group->inode = NULL;
40313e43c279SJosef Bacik 			spin_unlock(&block_group->lock);
403250c31eaaSJosef Bacik 
40333e43c279SJosef Bacik 			ASSERT(block_group->io_ctl.inode == NULL);
40343e43c279SJosef Bacik 			iput(inode);
403550c31eaaSJosef Bacik 		} else {
403650c31eaaSJosef Bacik 			spin_unlock(&block_group->lock);
403750c31eaaSJosef Bacik 		}
403850c31eaaSJosef Bacik 		block_group = btrfs_next_block_group(block_group);
40393e43c279SJosef Bacik 	}
40403e43c279SJosef Bacik }
40413e43c279SJosef Bacik 
40423e43c279SJosef Bacik /*
40433e43c279SJosef Bacik  * Must be called only after stopping all workers, since we could have block
40443e43c279SJosef Bacik  * group caching kthreads running, and therefore they could race with us if we
40453e43c279SJosef Bacik  * freed the block groups before stopping them.
40463e43c279SJosef Bacik  */
40473e43c279SJosef Bacik int btrfs_free_block_groups(struct btrfs_fs_info *info)
40483e43c279SJosef Bacik {
404932da5386SDavid Sterba 	struct btrfs_block_group *block_group;
40503e43c279SJosef Bacik 	struct btrfs_space_info *space_info;
40513e43c279SJosef Bacik 	struct btrfs_caching_control *caching_ctl;
40523e43c279SJosef Bacik 	struct rb_node *n;
40533e43c279SJosef Bacik 
405416b0c258SFilipe Manana 	write_lock(&info->block_group_cache_lock);
40553e43c279SJosef Bacik 	while (!list_empty(&info->caching_block_groups)) {
40563e43c279SJosef Bacik 		caching_ctl = list_entry(info->caching_block_groups.next,
40573e43c279SJosef Bacik 					 struct btrfs_caching_control, list);
40583e43c279SJosef Bacik 		list_del(&caching_ctl->list);
40593e43c279SJosef Bacik 		btrfs_put_caching_control(caching_ctl);
40603e43c279SJosef Bacik 	}
406116b0c258SFilipe Manana 	write_unlock(&info->block_group_cache_lock);
40623e43c279SJosef Bacik 
40633e43c279SJosef Bacik 	spin_lock(&info->unused_bgs_lock);
40643e43c279SJosef Bacik 	while (!list_empty(&info->unused_bgs)) {
40653e43c279SJosef Bacik 		block_group = list_first_entry(&info->unused_bgs,
406632da5386SDavid Sterba 					       struct btrfs_block_group,
40673e43c279SJosef Bacik 					       bg_list);
40683e43c279SJosef Bacik 		list_del_init(&block_group->bg_list);
40693e43c279SJosef Bacik 		btrfs_put_block_group(block_group);
40703e43c279SJosef Bacik 	}
40713e43c279SJosef Bacik 
407218bb8bbfSJohannes Thumshirn 	while (!list_empty(&info->reclaim_bgs)) {
407318bb8bbfSJohannes Thumshirn 		block_group = list_first_entry(&info->reclaim_bgs,
407418bb8bbfSJohannes Thumshirn 					       struct btrfs_block_group,
407518bb8bbfSJohannes Thumshirn 					       bg_list);
407618bb8bbfSJohannes Thumshirn 		list_del_init(&block_group->bg_list);
407718bb8bbfSJohannes Thumshirn 		btrfs_put_block_group(block_group);
407818bb8bbfSJohannes Thumshirn 	}
407918bb8bbfSJohannes Thumshirn 	spin_unlock(&info->unused_bgs_lock);
408018bb8bbfSJohannes Thumshirn 
4081afba2bc0SNaohiro Aota 	spin_lock(&info->zone_active_bgs_lock);
4082afba2bc0SNaohiro Aota 	while (!list_empty(&info->zone_active_bgs)) {
4083afba2bc0SNaohiro Aota 		block_group = list_first_entry(&info->zone_active_bgs,
4084afba2bc0SNaohiro Aota 					       struct btrfs_block_group,
4085afba2bc0SNaohiro Aota 					       active_bg_list);
4086afba2bc0SNaohiro Aota 		list_del_init(&block_group->active_bg_list);
4087afba2bc0SNaohiro Aota 		btrfs_put_block_group(block_group);
4088afba2bc0SNaohiro Aota 	}
4089afba2bc0SNaohiro Aota 	spin_unlock(&info->zone_active_bgs_lock);
4090afba2bc0SNaohiro Aota 
409116b0c258SFilipe Manana 	write_lock(&info->block_group_cache_lock);
409208dddb29SFilipe Manana 	while ((n = rb_last(&info->block_group_cache_tree.rb_root)) != NULL) {
409332da5386SDavid Sterba 		block_group = rb_entry(n, struct btrfs_block_group,
40943e43c279SJosef Bacik 				       cache_node);
409508dddb29SFilipe Manana 		rb_erase_cached(&block_group->cache_node,
40963e43c279SJosef Bacik 				&info->block_group_cache_tree);
40973e43c279SJosef Bacik 		RB_CLEAR_NODE(&block_group->cache_node);
409816b0c258SFilipe Manana 		write_unlock(&info->block_group_cache_lock);
40993e43c279SJosef Bacik 
41003e43c279SJosef Bacik 		down_write(&block_group->space_info->groups_sem);
41013e43c279SJosef Bacik 		list_del(&block_group->list);
41023e43c279SJosef Bacik 		up_write(&block_group->space_info->groups_sem);
41033e43c279SJosef Bacik 
41043e43c279SJosef Bacik 		/*
41053e43c279SJosef Bacik 		 * We haven't cached this block group, which means we could
41063e43c279SJosef Bacik 		 * possibly have excluded extents on this block group.
41073e43c279SJosef Bacik 		 */
41083e43c279SJosef Bacik 		if (block_group->cached == BTRFS_CACHE_NO ||
41093e43c279SJosef Bacik 		    block_group->cached == BTRFS_CACHE_ERROR)
41103e43c279SJosef Bacik 			btrfs_free_excluded_extents(block_group);
41113e43c279SJosef Bacik 
41123e43c279SJosef Bacik 		btrfs_remove_free_space_cache(block_group);
41133e43c279SJosef Bacik 		ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
41143e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->dirty_list));
41153e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->io_list));
41163e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->bg_list));
411748aaeebeSJosef Bacik 		ASSERT(refcount_read(&block_group->refs) == 1);
4118195a49eaSFilipe Manana 		ASSERT(block_group->swap_extents == 0);
41193e43c279SJosef Bacik 		btrfs_put_block_group(block_group);
41203e43c279SJosef Bacik 
412116b0c258SFilipe Manana 		write_lock(&info->block_group_cache_lock);
41223e43c279SJosef Bacik 	}
412316b0c258SFilipe Manana 	write_unlock(&info->block_group_cache_lock);
41243e43c279SJosef Bacik 
41253e43c279SJosef Bacik 	btrfs_release_global_block_rsv(info);
41263e43c279SJosef Bacik 
41273e43c279SJosef Bacik 	while (!list_empty(&info->space_info)) {
41283e43c279SJosef Bacik 		space_info = list_entry(info->space_info.next,
41293e43c279SJosef Bacik 					struct btrfs_space_info,
41303e43c279SJosef Bacik 					list);
41313e43c279SJosef Bacik 
41323e43c279SJosef Bacik 		/*
41333e43c279SJosef Bacik 		 * Do not hide this behind enospc_debug, this is actually
41343e43c279SJosef Bacik 		 * important and indicates a real bug if this happens.
41353e43c279SJosef Bacik 		 */
41363e43c279SJosef Bacik 		if (WARN_ON(space_info->bytes_pinned > 0 ||
41373e43c279SJosef Bacik 			    space_info->bytes_may_use > 0))
41383e43c279SJosef Bacik 			btrfs_dump_space_info(info, space_info, 0, 0);
413940cdc509SFilipe Manana 
414040cdc509SFilipe Manana 		/*
414140cdc509SFilipe Manana 		 * If there was a failure to cleanup a log tree, very likely due
414240cdc509SFilipe Manana 		 * to an IO failure on a writeback attempt of one or more of its
414340cdc509SFilipe Manana 		 * extent buffers, we could not do proper (and cheap) unaccounting
414440cdc509SFilipe Manana 		 * of their reserved space, so don't warn on bytes_reserved > 0 in
414540cdc509SFilipe Manana 		 * that case.
414640cdc509SFilipe Manana 		 */
414740cdc509SFilipe Manana 		if (!(space_info->flags & BTRFS_BLOCK_GROUP_METADATA) ||
414840cdc509SFilipe Manana 		    !BTRFS_FS_LOG_CLEANUP_ERROR(info)) {
414940cdc509SFilipe Manana 			if (WARN_ON(space_info->bytes_reserved > 0))
415040cdc509SFilipe Manana 				btrfs_dump_space_info(info, space_info, 0, 0);
415140cdc509SFilipe Manana 		}
415240cdc509SFilipe Manana 
4153d611add4SFilipe Manana 		WARN_ON(space_info->reclaim_size > 0);
41543e43c279SJosef Bacik 		list_del(&space_info->list);
41553e43c279SJosef Bacik 		btrfs_sysfs_remove_space_info(space_info);
41563e43c279SJosef Bacik 	}
41573e43c279SJosef Bacik 	return 0;
41583e43c279SJosef Bacik }
4159684b752bSFilipe Manana 
4160684b752bSFilipe Manana void btrfs_freeze_block_group(struct btrfs_block_group *cache)
4161684b752bSFilipe Manana {
4162684b752bSFilipe Manana 	atomic_inc(&cache->frozen);
4163684b752bSFilipe Manana }
4164684b752bSFilipe Manana 
4165684b752bSFilipe Manana void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
4166684b752bSFilipe Manana {
4167684b752bSFilipe Manana 	struct btrfs_fs_info *fs_info = block_group->fs_info;
4168684b752bSFilipe Manana 	struct extent_map_tree *em_tree;
4169684b752bSFilipe Manana 	struct extent_map *em;
4170684b752bSFilipe Manana 	bool cleanup;
4171684b752bSFilipe Manana 
4172684b752bSFilipe Manana 	spin_lock(&block_group->lock);
4173684b752bSFilipe Manana 	cleanup = (atomic_dec_and_test(&block_group->frozen) &&
41743349b57fSJosef Bacik 		   test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags));
4175684b752bSFilipe Manana 	spin_unlock(&block_group->lock);
4176684b752bSFilipe Manana 
4177684b752bSFilipe Manana 	if (cleanup) {
4178684b752bSFilipe Manana 		em_tree = &fs_info->mapping_tree;
4179684b752bSFilipe Manana 		write_lock(&em_tree->lock);
4180684b752bSFilipe Manana 		em = lookup_extent_mapping(em_tree, block_group->start,
4181684b752bSFilipe Manana 					   1);
4182684b752bSFilipe Manana 		BUG_ON(!em); /* logic error, can't happen */
4183684b752bSFilipe Manana 		remove_extent_mapping(em_tree, em);
4184684b752bSFilipe Manana 		write_unlock(&em_tree->lock);
4185684b752bSFilipe Manana 
4186684b752bSFilipe Manana 		/* once for us and once for the tree */
4187684b752bSFilipe Manana 		free_extent_map(em);
4188684b752bSFilipe Manana 		free_extent_map(em);
4189684b752bSFilipe Manana 
4190684b752bSFilipe Manana 		/*
4191684b752bSFilipe Manana 		 * We may have left one free space entry and other possible
4192684b752bSFilipe Manana 		 * tasks trimming this block group have left 1 entry each one.
4193684b752bSFilipe Manana 		 * Free them if any.
4194684b752bSFilipe Manana 		 */
4195fc80f7acSJosef Bacik 		btrfs_remove_free_space_cache(block_group);
4196684b752bSFilipe Manana 	}
4197684b752bSFilipe Manana }
4198195a49eaSFilipe Manana 
4199195a49eaSFilipe Manana bool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg)
4200195a49eaSFilipe Manana {
4201195a49eaSFilipe Manana 	bool ret = true;
4202195a49eaSFilipe Manana 
4203195a49eaSFilipe Manana 	spin_lock(&bg->lock);
4204195a49eaSFilipe Manana 	if (bg->ro)
4205195a49eaSFilipe Manana 		ret = false;
4206195a49eaSFilipe Manana 	else
4207195a49eaSFilipe Manana 		bg->swap_extents++;
4208195a49eaSFilipe Manana 	spin_unlock(&bg->lock);
4209195a49eaSFilipe Manana 
4210195a49eaSFilipe Manana 	return ret;
4211195a49eaSFilipe Manana }
4212195a49eaSFilipe Manana 
4213195a49eaSFilipe Manana void btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount)
4214195a49eaSFilipe Manana {
4215195a49eaSFilipe Manana 	spin_lock(&bg->lock);
4216195a49eaSFilipe Manana 	ASSERT(!bg->ro);
4217195a49eaSFilipe Manana 	ASSERT(bg->swap_extents >= amount);
4218195a49eaSFilipe Manana 	bg->swap_extents -= amount;
4219195a49eaSFilipe Manana 	spin_unlock(&bg->lock);
4220195a49eaSFilipe Manana }
4221