xref: /linux/fs/btrfs/block-group.c (revision 7a40974fd0efa3698de4c6d1d0ee0436bcc4445d)
12e405ad8SJosef Bacik // SPDX-License-Identifier: GPL-2.0
22e405ad8SJosef Bacik 
352bb7a21SBoris Burkov #include <linux/sizes.h>
42ca0ec77SJohannes Thumshirn #include <linux/list_sort.h>
5784352feSDavid Sterba #include "misc.h"
62e405ad8SJosef Bacik #include "ctree.h"
72e405ad8SJosef Bacik #include "block-group.h"
83eeb3226SJosef Bacik #include "space-info.h"
99f21246dSJosef Bacik #include "disk-io.h"
109f21246dSJosef Bacik #include "free-space-cache.h"
119f21246dSJosef Bacik #include "free-space-tree.h"
12e3e0520bSJosef Bacik #include "volumes.h"
13e3e0520bSJosef Bacik #include "transaction.h"
14e3e0520bSJosef Bacik #include "ref-verify.h"
154358d963SJosef Bacik #include "sysfs.h"
164358d963SJosef Bacik #include "tree-log.h"
1777745c05SJosef Bacik #include "delalloc-space.h"
18b0643e59SDennis Zhou #include "discard.h"
1996a14336SNikolay Borisov #include "raid56.h"
2008e11a3dSNaohiro Aota #include "zoned.h"
21c7f13d42SJosef Bacik #include "fs.h"
2207e81dc9SJosef Bacik #include "accessors.h"
23a0231804SJosef Bacik #include "extent-tree.h"
242e405ad8SJosef Bacik 
2506d61cb1SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
btrfs_should_fragment_free_space(const struct btrfs_block_group * block_group)26*ca283ea9SDavid Sterba int btrfs_should_fragment_free_space(const struct btrfs_block_group *block_group)
2706d61cb1SJosef Bacik {
2806d61cb1SJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2906d61cb1SJosef Bacik 
3006d61cb1SJosef Bacik 	return (btrfs_test_opt(fs_info, FRAGMENT_METADATA) &&
3106d61cb1SJosef Bacik 		block_group->flags & BTRFS_BLOCK_GROUP_METADATA) ||
3206d61cb1SJosef Bacik 	       (btrfs_test_opt(fs_info, FRAGMENT_DATA) &&
3306d61cb1SJosef Bacik 		block_group->flags &  BTRFS_BLOCK_GROUP_DATA);
3406d61cb1SJosef Bacik }
3506d61cb1SJosef Bacik #endif
3606d61cb1SJosef Bacik 
37878d7b67SJosef Bacik /*
38878d7b67SJosef Bacik  * Return target flags in extended format or 0 if restripe for this chunk_type
39878d7b67SJosef Bacik  * is not in progress
40878d7b67SJosef Bacik  *
41878d7b67SJosef Bacik  * Should be called with balance_lock held
42878d7b67SJosef Bacik  */
get_restripe_target(const struct btrfs_fs_info * fs_info,u64 flags)43*ca283ea9SDavid Sterba static u64 get_restripe_target(const struct btrfs_fs_info *fs_info, u64 flags)
44878d7b67SJosef Bacik {
45*ca283ea9SDavid Sterba 	const struct btrfs_balance_control *bctl = fs_info->balance_ctl;
46878d7b67SJosef Bacik 	u64 target = 0;
47878d7b67SJosef Bacik 
48878d7b67SJosef Bacik 	if (!bctl)
49878d7b67SJosef Bacik 		return 0;
50878d7b67SJosef Bacik 
51878d7b67SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA &&
52878d7b67SJosef Bacik 	    bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
53878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
54878d7b67SJosef Bacik 	} else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
55878d7b67SJosef Bacik 		   bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
56878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
57878d7b67SJosef Bacik 	} else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
58878d7b67SJosef Bacik 		   bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
59878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
60878d7b67SJosef Bacik 	}
61878d7b67SJosef Bacik 
62878d7b67SJosef Bacik 	return target;
63878d7b67SJosef Bacik }
64878d7b67SJosef Bacik 
65878d7b67SJosef Bacik /*
66878d7b67SJosef Bacik  * @flags: available profiles in extended format (see ctree.h)
67878d7b67SJosef Bacik  *
68878d7b67SJosef Bacik  * Return reduced profile in chunk format.  If profile changing is in progress
69878d7b67SJosef Bacik  * (either running or paused) picks the target profile (if it's already
70878d7b67SJosef Bacik  * available), otherwise falls back to plain reducing.
71878d7b67SJosef Bacik  */
btrfs_reduce_alloc_profile(struct btrfs_fs_info * fs_info,u64 flags)72878d7b67SJosef Bacik static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
73878d7b67SJosef Bacik {
74878d7b67SJosef Bacik 	u64 num_devices = fs_info->fs_devices->rw_devices;
75878d7b67SJosef Bacik 	u64 target;
76878d7b67SJosef Bacik 	u64 raid_type;
77878d7b67SJosef Bacik 	u64 allowed = 0;
78878d7b67SJosef Bacik 
79878d7b67SJosef Bacik 	/*
80878d7b67SJosef Bacik 	 * See if restripe for this chunk_type is in progress, if so try to
81878d7b67SJosef Bacik 	 * reduce to the target profile
82878d7b67SJosef Bacik 	 */
83878d7b67SJosef Bacik 	spin_lock(&fs_info->balance_lock);
84e11c0406SJosef Bacik 	target = get_restripe_target(fs_info, flags);
85878d7b67SJosef Bacik 	if (target) {
86878d7b67SJosef Bacik 		spin_unlock(&fs_info->balance_lock);
87878d7b67SJosef Bacik 		return extended_to_chunk(target);
88878d7b67SJosef Bacik 	}
89878d7b67SJosef Bacik 	spin_unlock(&fs_info->balance_lock);
90878d7b67SJosef Bacik 
91878d7b67SJosef Bacik 	/* First, mask out the RAID levels which aren't possible */
92878d7b67SJosef Bacik 	for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
93878d7b67SJosef Bacik 		if (num_devices >= btrfs_raid_array[raid_type].devs_min)
94878d7b67SJosef Bacik 			allowed |= btrfs_raid_array[raid_type].bg_flag;
95878d7b67SJosef Bacik 	}
96878d7b67SJosef Bacik 	allowed &= flags;
97878d7b67SJosef Bacik 
98160fe8f6SMatt Corallo 	/* Select the highest-redundancy RAID level. */
99160fe8f6SMatt Corallo 	if (allowed & BTRFS_BLOCK_GROUP_RAID1C4)
100160fe8f6SMatt Corallo 		allowed = BTRFS_BLOCK_GROUP_RAID1C4;
101160fe8f6SMatt Corallo 	else if (allowed & BTRFS_BLOCK_GROUP_RAID6)
102878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID6;
103160fe8f6SMatt Corallo 	else if (allowed & BTRFS_BLOCK_GROUP_RAID1C3)
104160fe8f6SMatt Corallo 		allowed = BTRFS_BLOCK_GROUP_RAID1C3;
105878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
106878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID5;
107878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
108878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID10;
109878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
110878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID1;
111160fe8f6SMatt Corallo 	else if (allowed & BTRFS_BLOCK_GROUP_DUP)
112160fe8f6SMatt Corallo 		allowed = BTRFS_BLOCK_GROUP_DUP;
113878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
114878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID0;
115878d7b67SJosef Bacik 
116878d7b67SJosef Bacik 	flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
117878d7b67SJosef Bacik 
118878d7b67SJosef Bacik 	return extended_to_chunk(flags | allowed);
119878d7b67SJosef Bacik }
120878d7b67SJosef Bacik 
btrfs_get_alloc_profile(struct btrfs_fs_info * fs_info,u64 orig_flags)121ef0a82daSJohannes Thumshirn u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
122878d7b67SJosef Bacik {
123878d7b67SJosef Bacik 	unsigned seq;
124878d7b67SJosef Bacik 	u64 flags;
125878d7b67SJosef Bacik 
126878d7b67SJosef Bacik 	do {
127878d7b67SJosef Bacik 		flags = orig_flags;
128878d7b67SJosef Bacik 		seq = read_seqbegin(&fs_info->profiles_lock);
129878d7b67SJosef Bacik 
130878d7b67SJosef Bacik 		if (flags & BTRFS_BLOCK_GROUP_DATA)
131878d7b67SJosef Bacik 			flags |= fs_info->avail_data_alloc_bits;
132878d7b67SJosef Bacik 		else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
133878d7b67SJosef Bacik 			flags |= fs_info->avail_system_alloc_bits;
134878d7b67SJosef Bacik 		else if (flags & BTRFS_BLOCK_GROUP_METADATA)
135878d7b67SJosef Bacik 			flags |= fs_info->avail_metadata_alloc_bits;
136878d7b67SJosef Bacik 	} while (read_seqretry(&fs_info->profiles_lock, seq));
137878d7b67SJosef Bacik 
138878d7b67SJosef Bacik 	return btrfs_reduce_alloc_profile(fs_info, flags);
139878d7b67SJosef Bacik }
140878d7b67SJosef Bacik 
btrfs_get_block_group(struct btrfs_block_group * cache)14132da5386SDavid Sterba void btrfs_get_block_group(struct btrfs_block_group *cache)
1423cad1284SJosef Bacik {
14348aaeebeSJosef Bacik 	refcount_inc(&cache->refs);
1443cad1284SJosef Bacik }
1453cad1284SJosef Bacik 
btrfs_put_block_group(struct btrfs_block_group * cache)14632da5386SDavid Sterba void btrfs_put_block_group(struct btrfs_block_group *cache)
1473cad1284SJosef Bacik {
14848aaeebeSJosef Bacik 	if (refcount_dec_and_test(&cache->refs)) {
1493cad1284SJosef Bacik 		WARN_ON(cache->pinned > 0);
15040cdc509SFilipe Manana 		/*
15140cdc509SFilipe Manana 		 * If there was a failure to cleanup a log tree, very likely due
15240cdc509SFilipe Manana 		 * to an IO failure on a writeback attempt of one or more of its
15340cdc509SFilipe Manana 		 * extent buffers, we could not do proper (and cheap) unaccounting
15440cdc509SFilipe Manana 		 * of their reserved space, so don't warn on reserved > 0 in that
15540cdc509SFilipe Manana 		 * case.
15640cdc509SFilipe Manana 		 */
15740cdc509SFilipe Manana 		if (!(cache->flags & BTRFS_BLOCK_GROUP_METADATA) ||
15840cdc509SFilipe Manana 		    !BTRFS_FS_LOG_CLEANUP_ERROR(cache->fs_info))
1593cad1284SJosef Bacik 			WARN_ON(cache->reserved > 0);
1603cad1284SJosef Bacik 
1613cad1284SJosef Bacik 		/*
162b0643e59SDennis Zhou 		 * A block_group shouldn't be on the discard_list anymore.
163b0643e59SDennis Zhou 		 * Remove the block_group from the discard_list to prevent us
164b0643e59SDennis Zhou 		 * from causing a panic due to NULL pointer dereference.
165b0643e59SDennis Zhou 		 */
166b0643e59SDennis Zhou 		if (WARN_ON(!list_empty(&cache->discard_list)))
167b0643e59SDennis Zhou 			btrfs_discard_cancel_work(&cache->fs_info->discard_ctl,
168b0643e59SDennis Zhou 						  cache);
169b0643e59SDennis Zhou 
1703cad1284SJosef Bacik 		kfree(cache->free_space_ctl);
1717dc66abbSFilipe Manana 		btrfs_free_chunk_map(cache->physical_map);
1723cad1284SJosef Bacik 		kfree(cache);
1733cad1284SJosef Bacik 	}
1743cad1284SJosef Bacik }
1753cad1284SJosef Bacik 
1762e405ad8SJosef Bacik /*
1774358d963SJosef Bacik  * This adds the block group to the fs_info rb tree for the block group cache
1784358d963SJosef Bacik  */
btrfs_add_block_group_cache(struct btrfs_fs_info * info,struct btrfs_block_group * block_group)1794358d963SJosef Bacik static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
18032da5386SDavid Sterba 				       struct btrfs_block_group *block_group)
1814358d963SJosef Bacik {
1824358d963SJosef Bacik 	struct rb_node **p;
1834358d963SJosef Bacik 	struct rb_node *parent = NULL;
18432da5386SDavid Sterba 	struct btrfs_block_group *cache;
18508dddb29SFilipe Manana 	bool leftmost = true;
1864358d963SJosef Bacik 
1879afc6649SQu Wenruo 	ASSERT(block_group->length != 0);
1889afc6649SQu Wenruo 
18916b0c258SFilipe Manana 	write_lock(&info->block_group_cache_lock);
19008dddb29SFilipe Manana 	p = &info->block_group_cache_tree.rb_root.rb_node;
1914358d963SJosef Bacik 
1924358d963SJosef Bacik 	while (*p) {
1934358d963SJosef Bacik 		parent = *p;
19432da5386SDavid Sterba 		cache = rb_entry(parent, struct btrfs_block_group, cache_node);
195b3470b5dSDavid Sterba 		if (block_group->start < cache->start) {
1964358d963SJosef Bacik 			p = &(*p)->rb_left;
197b3470b5dSDavid Sterba 		} else if (block_group->start > cache->start) {
1984358d963SJosef Bacik 			p = &(*p)->rb_right;
19908dddb29SFilipe Manana 			leftmost = false;
2004358d963SJosef Bacik 		} else {
20116b0c258SFilipe Manana 			write_unlock(&info->block_group_cache_lock);
2024358d963SJosef Bacik 			return -EEXIST;
2034358d963SJosef Bacik 		}
2044358d963SJosef Bacik 	}
2054358d963SJosef Bacik 
2064358d963SJosef Bacik 	rb_link_node(&block_group->cache_node, parent, p);
20708dddb29SFilipe Manana 	rb_insert_color_cached(&block_group->cache_node,
20808dddb29SFilipe Manana 			       &info->block_group_cache_tree, leftmost);
2094358d963SJosef Bacik 
21016b0c258SFilipe Manana 	write_unlock(&info->block_group_cache_lock);
2114358d963SJosef Bacik 
2124358d963SJosef Bacik 	return 0;
2134358d963SJosef Bacik }
2144358d963SJosef Bacik 
2154358d963SJosef Bacik /*
2162e405ad8SJosef Bacik  * This will return the block group at or after bytenr if contains is 0, else
2172e405ad8SJosef Bacik  * it will return the block group that contains the bytenr
2182e405ad8SJosef Bacik  */
block_group_cache_tree_search(struct btrfs_fs_info * info,u64 bytenr,int contains)21932da5386SDavid Sterba static struct btrfs_block_group *block_group_cache_tree_search(
2202e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr, int contains)
2212e405ad8SJosef Bacik {
22232da5386SDavid Sterba 	struct btrfs_block_group *cache, *ret = NULL;
2232e405ad8SJosef Bacik 	struct rb_node *n;
2242e405ad8SJosef Bacik 	u64 end, start;
2252e405ad8SJosef Bacik 
22616b0c258SFilipe Manana 	read_lock(&info->block_group_cache_lock);
22708dddb29SFilipe Manana 	n = info->block_group_cache_tree.rb_root.rb_node;
2282e405ad8SJosef Bacik 
2292e405ad8SJosef Bacik 	while (n) {
23032da5386SDavid Sterba 		cache = rb_entry(n, struct btrfs_block_group, cache_node);
231b3470b5dSDavid Sterba 		end = cache->start + cache->length - 1;
232b3470b5dSDavid Sterba 		start = cache->start;
2332e405ad8SJosef Bacik 
2342e405ad8SJosef Bacik 		if (bytenr < start) {
235b3470b5dSDavid Sterba 			if (!contains && (!ret || start < ret->start))
2362e405ad8SJosef Bacik 				ret = cache;
2372e405ad8SJosef Bacik 			n = n->rb_left;
2382e405ad8SJosef Bacik 		} else if (bytenr > start) {
2392e405ad8SJosef Bacik 			if (contains && bytenr <= end) {
2402e405ad8SJosef Bacik 				ret = cache;
2412e405ad8SJosef Bacik 				break;
2422e405ad8SJosef Bacik 			}
2432e405ad8SJosef Bacik 			n = n->rb_right;
2442e405ad8SJosef Bacik 		} else {
2452e405ad8SJosef Bacik 			ret = cache;
2462e405ad8SJosef Bacik 			break;
2472e405ad8SJosef Bacik 		}
2482e405ad8SJosef Bacik 	}
24908dddb29SFilipe Manana 	if (ret)
2502e405ad8SJosef Bacik 		btrfs_get_block_group(ret);
25116b0c258SFilipe Manana 	read_unlock(&info->block_group_cache_lock);
2522e405ad8SJosef Bacik 
2532e405ad8SJosef Bacik 	return ret;
2542e405ad8SJosef Bacik }
2552e405ad8SJosef Bacik 
2562e405ad8SJosef Bacik /*
2572e405ad8SJosef Bacik  * Return the block group that starts at or after bytenr
2582e405ad8SJosef Bacik  */
btrfs_lookup_first_block_group(struct btrfs_fs_info * info,u64 bytenr)25932da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_first_block_group(
2602e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr)
2612e405ad8SJosef Bacik {
2622e405ad8SJosef Bacik 	return block_group_cache_tree_search(info, bytenr, 0);
2632e405ad8SJosef Bacik }
2642e405ad8SJosef Bacik 
2652e405ad8SJosef Bacik /*
2662e405ad8SJosef Bacik  * Return the block group that contains the given bytenr
2672e405ad8SJosef Bacik  */
btrfs_lookup_block_group(struct btrfs_fs_info * info,u64 bytenr)26832da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_block_group(
2692e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr)
2702e405ad8SJosef Bacik {
2712e405ad8SJosef Bacik 	return block_group_cache_tree_search(info, bytenr, 1);
2722e405ad8SJosef Bacik }
2732e405ad8SJosef Bacik 
btrfs_next_block_group(struct btrfs_block_group * cache)27432da5386SDavid Sterba struct btrfs_block_group *btrfs_next_block_group(
27532da5386SDavid Sterba 		struct btrfs_block_group *cache)
2762e405ad8SJosef Bacik {
2772e405ad8SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
2782e405ad8SJosef Bacik 	struct rb_node *node;
2792e405ad8SJosef Bacik 
28016b0c258SFilipe Manana 	read_lock(&fs_info->block_group_cache_lock);
2812e405ad8SJosef Bacik 
2822e405ad8SJosef Bacik 	/* If our block group was removed, we need a full search. */
2832e405ad8SJosef Bacik 	if (RB_EMPTY_NODE(&cache->cache_node)) {
284b3470b5dSDavid Sterba 		const u64 next_bytenr = cache->start + cache->length;
2852e405ad8SJosef Bacik 
28616b0c258SFilipe Manana 		read_unlock(&fs_info->block_group_cache_lock);
2872e405ad8SJosef Bacik 		btrfs_put_block_group(cache);
2888b01f931SFilipe Manana 		return btrfs_lookup_first_block_group(fs_info, next_bytenr);
2892e405ad8SJosef Bacik 	}
2902e405ad8SJosef Bacik 	node = rb_next(&cache->cache_node);
2912e405ad8SJosef Bacik 	btrfs_put_block_group(cache);
2922e405ad8SJosef Bacik 	if (node) {
29332da5386SDavid Sterba 		cache = rb_entry(node, struct btrfs_block_group, cache_node);
2942e405ad8SJosef Bacik 		btrfs_get_block_group(cache);
2952e405ad8SJosef Bacik 	} else
2962e405ad8SJosef Bacik 		cache = NULL;
29716b0c258SFilipe Manana 	read_unlock(&fs_info->block_group_cache_lock);
2982e405ad8SJosef Bacik 	return cache;
2992e405ad8SJosef Bacik }
3003eeb3226SJosef Bacik 
30143dd529aSDavid Sterba /*
3022306e83eSFilipe Manana  * Check if we can do a NOCOW write for a given extent.
3032306e83eSFilipe Manana  *
3042306e83eSFilipe Manana  * @fs_info:       The filesystem information object.
3052306e83eSFilipe Manana  * @bytenr:        Logical start address of the extent.
3062306e83eSFilipe Manana  *
3072306e83eSFilipe Manana  * Check if we can do a NOCOW write for the given extent, and increments the
3082306e83eSFilipe Manana  * number of NOCOW writers in the block group that contains the extent, as long
3092306e83eSFilipe Manana  * as the block group exists and it's currently not in read-only mode.
3102306e83eSFilipe Manana  *
3112306e83eSFilipe Manana  * Returns: A non-NULL block group pointer if we can do a NOCOW write, the caller
3122306e83eSFilipe Manana  *          is responsible for calling btrfs_dec_nocow_writers() later.
3132306e83eSFilipe Manana  *
3142306e83eSFilipe Manana  *          Or NULL if we can not do a NOCOW write
3152306e83eSFilipe Manana  */
btrfs_inc_nocow_writers(struct btrfs_fs_info * fs_info,u64 bytenr)3162306e83eSFilipe Manana struct btrfs_block_group *btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info,
3172306e83eSFilipe Manana 						  u64 bytenr)
3183eeb3226SJosef Bacik {
31932da5386SDavid Sterba 	struct btrfs_block_group *bg;
3202306e83eSFilipe Manana 	bool can_nocow = true;
3213eeb3226SJosef Bacik 
3223eeb3226SJosef Bacik 	bg = btrfs_lookup_block_group(fs_info, bytenr);
3233eeb3226SJosef Bacik 	if (!bg)
3242306e83eSFilipe Manana 		return NULL;
3253eeb3226SJosef Bacik 
3263eeb3226SJosef Bacik 	spin_lock(&bg->lock);
3273eeb3226SJosef Bacik 	if (bg->ro)
3282306e83eSFilipe Manana 		can_nocow = false;
3293eeb3226SJosef Bacik 	else
3303eeb3226SJosef Bacik 		atomic_inc(&bg->nocow_writers);
3313eeb3226SJosef Bacik 	spin_unlock(&bg->lock);
3323eeb3226SJosef Bacik 
3332306e83eSFilipe Manana 	if (!can_nocow) {
3343eeb3226SJosef Bacik 		btrfs_put_block_group(bg);
3352306e83eSFilipe Manana 		return NULL;
3363eeb3226SJosef Bacik 	}
3373eeb3226SJosef Bacik 
3382306e83eSFilipe Manana 	/* No put on block group, done by btrfs_dec_nocow_writers(). */
3392306e83eSFilipe Manana 	return bg;
3402306e83eSFilipe Manana }
3413eeb3226SJosef Bacik 
34243dd529aSDavid Sterba /*
3432306e83eSFilipe Manana  * Decrement the number of NOCOW writers in a block group.
3442306e83eSFilipe Manana  *
3452306e83eSFilipe Manana  * This is meant to be called after a previous call to btrfs_inc_nocow_writers(),
3462306e83eSFilipe Manana  * and on the block group returned by that call. Typically this is called after
3472306e83eSFilipe Manana  * creating an ordered extent for a NOCOW write, to prevent races with scrub and
3482306e83eSFilipe Manana  * relocation.
3492306e83eSFilipe Manana  *
3502306e83eSFilipe Manana  * After this call, the caller should not use the block group anymore. It it wants
3512306e83eSFilipe Manana  * to use it, then it should get a reference on it before calling this function.
3522306e83eSFilipe Manana  */
btrfs_dec_nocow_writers(struct btrfs_block_group * bg)3532306e83eSFilipe Manana void btrfs_dec_nocow_writers(struct btrfs_block_group *bg)
3542306e83eSFilipe Manana {
3553eeb3226SJosef Bacik 	if (atomic_dec_and_test(&bg->nocow_writers))
3563eeb3226SJosef Bacik 		wake_up_var(&bg->nocow_writers);
3572306e83eSFilipe Manana 
3582306e83eSFilipe Manana 	/* For the lookup done by a previous call to btrfs_inc_nocow_writers(). */
3593eeb3226SJosef Bacik 	btrfs_put_block_group(bg);
3603eeb3226SJosef Bacik }
3613eeb3226SJosef Bacik 
btrfs_wait_nocow_writers(struct btrfs_block_group * bg)36232da5386SDavid Sterba void btrfs_wait_nocow_writers(struct btrfs_block_group *bg)
3633eeb3226SJosef Bacik {
3643eeb3226SJosef Bacik 	wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
3653eeb3226SJosef Bacik }
3663eeb3226SJosef Bacik 
btrfs_dec_block_group_reservations(struct btrfs_fs_info * fs_info,const u64 start)3673eeb3226SJosef Bacik void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
3683eeb3226SJosef Bacik 					const u64 start)
3693eeb3226SJosef Bacik {
37032da5386SDavid Sterba 	struct btrfs_block_group *bg;
3713eeb3226SJosef Bacik 
3723eeb3226SJosef Bacik 	bg = btrfs_lookup_block_group(fs_info, start);
3733eeb3226SJosef Bacik 	ASSERT(bg);
3743eeb3226SJosef Bacik 	if (atomic_dec_and_test(&bg->reservations))
3753eeb3226SJosef Bacik 		wake_up_var(&bg->reservations);
3763eeb3226SJosef Bacik 	btrfs_put_block_group(bg);
3773eeb3226SJosef Bacik }
3783eeb3226SJosef Bacik 
btrfs_wait_block_group_reservations(struct btrfs_block_group * bg)37932da5386SDavid Sterba void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg)
3803eeb3226SJosef Bacik {
3813eeb3226SJosef Bacik 	struct btrfs_space_info *space_info = bg->space_info;
3823eeb3226SJosef Bacik 
3833eeb3226SJosef Bacik 	ASSERT(bg->ro);
3843eeb3226SJosef Bacik 
3853eeb3226SJosef Bacik 	if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
3863eeb3226SJosef Bacik 		return;
3873eeb3226SJosef Bacik 
3883eeb3226SJosef Bacik 	/*
3893eeb3226SJosef Bacik 	 * Our block group is read only but before we set it to read only,
3903eeb3226SJosef Bacik 	 * some task might have had allocated an extent from it already, but it
3913eeb3226SJosef Bacik 	 * has not yet created a respective ordered extent (and added it to a
3923eeb3226SJosef Bacik 	 * root's list of ordered extents).
3933eeb3226SJosef Bacik 	 * Therefore wait for any task currently allocating extents, since the
3943eeb3226SJosef Bacik 	 * block group's reservations counter is incremented while a read lock
3953eeb3226SJosef Bacik 	 * on the groups' semaphore is held and decremented after releasing
3963eeb3226SJosef Bacik 	 * the read access on that semaphore and creating the ordered extent.
3973eeb3226SJosef Bacik 	 */
3983eeb3226SJosef Bacik 	down_write(&space_info->groups_sem);
3993eeb3226SJosef Bacik 	up_write(&space_info->groups_sem);
4003eeb3226SJosef Bacik 
4013eeb3226SJosef Bacik 	wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
4023eeb3226SJosef Bacik }
4039f21246dSJosef Bacik 
btrfs_get_caching_control(struct btrfs_block_group * cache)4049f21246dSJosef Bacik struct btrfs_caching_control *btrfs_get_caching_control(
40532da5386SDavid Sterba 		struct btrfs_block_group *cache)
4069f21246dSJosef Bacik {
4079f21246dSJosef Bacik 	struct btrfs_caching_control *ctl;
4089f21246dSJosef Bacik 
4099f21246dSJosef Bacik 	spin_lock(&cache->lock);
4109f21246dSJosef Bacik 	if (!cache->caching_ctl) {
4119f21246dSJosef Bacik 		spin_unlock(&cache->lock);
4129f21246dSJosef Bacik 		return NULL;
4139f21246dSJosef Bacik 	}
4149f21246dSJosef Bacik 
4159f21246dSJosef Bacik 	ctl = cache->caching_ctl;
4169f21246dSJosef Bacik 	refcount_inc(&ctl->count);
4179f21246dSJosef Bacik 	spin_unlock(&cache->lock);
4189f21246dSJosef Bacik 	return ctl;
4199f21246dSJosef Bacik }
4209f21246dSJosef Bacik 
btrfs_put_caching_control(struct btrfs_caching_control * ctl)4217ec28f83SLijuan Li static void btrfs_put_caching_control(struct btrfs_caching_control *ctl)
4229f21246dSJosef Bacik {
4239f21246dSJosef Bacik 	if (refcount_dec_and_test(&ctl->count))
4249f21246dSJosef Bacik 		kfree(ctl);
4259f21246dSJosef Bacik }
4269f21246dSJosef Bacik 
4279f21246dSJosef Bacik /*
4289f21246dSJosef Bacik  * When we wait for progress in the block group caching, its because our
4299f21246dSJosef Bacik  * allocation attempt failed at least once.  So, we must sleep and let some
4309f21246dSJosef Bacik  * progress happen before we try again.
4319f21246dSJosef Bacik  *
4329f21246dSJosef Bacik  * This function will sleep at least once waiting for new free space to show
4339f21246dSJosef Bacik  * up, and then it will check the block group free space numbers for our min
4349f21246dSJosef Bacik  * num_bytes.  Another option is to have it go ahead and look in the rbtree for
4359f21246dSJosef Bacik  * a free extent of a given size, but this is a good start.
4369f21246dSJosef Bacik  *
4379f21246dSJosef Bacik  * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
4389f21246dSJosef Bacik  * any of the information in this block group.
4399f21246dSJosef Bacik  */
btrfs_wait_block_group_cache_progress(struct btrfs_block_group * cache,u64 num_bytes)44032da5386SDavid Sterba void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache,
4419f21246dSJosef Bacik 					   u64 num_bytes)
4429f21246dSJosef Bacik {
4439f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
444fc1f91b9SJosef Bacik 	int progress;
4459f21246dSJosef Bacik 
4469f21246dSJosef Bacik 	caching_ctl = btrfs_get_caching_control(cache);
4479f21246dSJosef Bacik 	if (!caching_ctl)
4489f21246dSJosef Bacik 		return;
4499f21246dSJosef Bacik 
450fc1f91b9SJosef Bacik 	/*
451fc1f91b9SJosef Bacik 	 * We've already failed to allocate from this block group, so even if
452fc1f91b9SJosef Bacik 	 * there's enough space in the block group it isn't contiguous enough to
453fc1f91b9SJosef Bacik 	 * allow for an allocation, so wait for at least the next wakeup tick,
454fc1f91b9SJosef Bacik 	 * or for the thing to be done.
455fc1f91b9SJosef Bacik 	 */
456fc1f91b9SJosef Bacik 	progress = atomic_read(&caching_ctl->progress);
457fc1f91b9SJosef Bacik 
45832da5386SDavid Sterba 	wait_event(caching_ctl->wait, btrfs_block_group_done(cache) ||
459fc1f91b9SJosef Bacik 		   (progress != atomic_read(&caching_ctl->progress) &&
460fc1f91b9SJosef Bacik 		    (cache->free_space_ctl->free_space >= num_bytes)));
4619f21246dSJosef Bacik 
4629f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
4639f21246dSJosef Bacik }
4649f21246dSJosef Bacik 
btrfs_caching_ctl_wait_done(struct btrfs_block_group * cache,struct btrfs_caching_control * caching_ctl)465ced8ecf0SOmar Sandoval static int btrfs_caching_ctl_wait_done(struct btrfs_block_group *cache,
466ced8ecf0SOmar Sandoval 				       struct btrfs_caching_control *caching_ctl)
467ced8ecf0SOmar Sandoval {
468ced8ecf0SOmar Sandoval 	wait_event(caching_ctl->wait, btrfs_block_group_done(cache));
469ced8ecf0SOmar Sandoval 	return cache->cached == BTRFS_CACHE_ERROR ? -EIO : 0;
470ced8ecf0SOmar Sandoval }
471ced8ecf0SOmar Sandoval 
btrfs_wait_block_group_cache_done(struct btrfs_block_group * cache)472ced8ecf0SOmar Sandoval static int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache)
4739f21246dSJosef Bacik {
4749f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
475ced8ecf0SOmar Sandoval 	int ret;
4769f21246dSJosef Bacik 
4779f21246dSJosef Bacik 	caching_ctl = btrfs_get_caching_control(cache);
4789f21246dSJosef Bacik 	if (!caching_ctl)
4799f21246dSJosef Bacik 		return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
480ced8ecf0SOmar Sandoval 	ret = btrfs_caching_ctl_wait_done(cache, caching_ctl);
4819f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
4829f21246dSJosef Bacik 	return ret;
4839f21246dSJosef Bacik }
4849f21246dSJosef Bacik 
4859f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
fragment_free_space(struct btrfs_block_group * block_group)48632da5386SDavid Sterba static void fragment_free_space(struct btrfs_block_group *block_group)
4879f21246dSJosef Bacik {
4889f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
489b3470b5dSDavid Sterba 	u64 start = block_group->start;
490b3470b5dSDavid Sterba 	u64 len = block_group->length;
4919f21246dSJosef Bacik 	u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
4929f21246dSJosef Bacik 		fs_info->nodesize : fs_info->sectorsize;
4939f21246dSJosef Bacik 	u64 step = chunk << 1;
4949f21246dSJosef Bacik 
4959f21246dSJosef Bacik 	while (len > chunk) {
4969f21246dSJosef Bacik 		btrfs_remove_free_space(block_group, start, chunk);
4979f21246dSJosef Bacik 		start += step;
4989f21246dSJosef Bacik 		if (len < step)
4999f21246dSJosef Bacik 			len = 0;
5009f21246dSJosef Bacik 		else
5019f21246dSJosef Bacik 			len -= step;
5029f21246dSJosef Bacik 	}
5039f21246dSJosef Bacik }
5049f21246dSJosef Bacik #endif
5059f21246dSJosef Bacik 
5069f21246dSJosef Bacik /*
50728f60894SFilipe Manana  * Add a free space range to the in memory free space cache of a block group.
50828f60894SFilipe Manana  * This checks if the range contains super block locations and any such
50928f60894SFilipe Manana  * locations are not added to the free space cache.
51028f60894SFilipe Manana  *
51128f60894SFilipe Manana  * @block_group:      The target block group.
51228f60894SFilipe Manana  * @start:            Start offset of the range.
51328f60894SFilipe Manana  * @end:              End offset of the range (exclusive).
51428f60894SFilipe Manana  * @total_added_ret:  Optional pointer to return the total amount of space
51528f60894SFilipe Manana  *                    added to the block group's free space cache.
51628f60894SFilipe Manana  *
51728f60894SFilipe Manana  * Returns 0 on success or < 0 on error.
5189f21246dSJosef Bacik  */
btrfs_add_new_free_space(struct btrfs_block_group * block_group,u64 start,u64 end,u64 * total_added_ret)5193b9f0995SFilipe Manana int btrfs_add_new_free_space(struct btrfs_block_group *block_group, u64 start,
5203b9f0995SFilipe Manana 			     u64 end, u64 *total_added_ret)
5219f21246dSJosef Bacik {
5229f21246dSJosef Bacik 	struct btrfs_fs_info *info = block_group->fs_info;
523d8ccbd21SFilipe Manana 	u64 extent_start, extent_end, size;
5249f21246dSJosef Bacik 	int ret;
5259f21246dSJosef Bacik 
526d8ccbd21SFilipe Manana 	if (total_added_ret)
527d8ccbd21SFilipe Manana 		*total_added_ret = 0;
528d8ccbd21SFilipe Manana 
5299f21246dSJosef Bacik 	while (start < end) {
530e5860f82SFilipe Manana 		if (!find_first_extent_bit(&info->excluded_extents, start,
5319f21246dSJosef Bacik 					   &extent_start, &extent_end,
5329f21246dSJosef Bacik 					   EXTENT_DIRTY | EXTENT_UPTODATE,
533e5860f82SFilipe Manana 					   NULL))
5349f21246dSJosef Bacik 			break;
5359f21246dSJosef Bacik 
5369f21246dSJosef Bacik 		if (extent_start <= start) {
5379f21246dSJosef Bacik 			start = extent_end + 1;
5389f21246dSJosef Bacik 		} else if (extent_start > start && extent_start < end) {
5399f21246dSJosef Bacik 			size = extent_start - start;
540b0643e59SDennis Zhou 			ret = btrfs_add_free_space_async_trimmed(block_group,
541b0643e59SDennis Zhou 								 start, size);
542d8ccbd21SFilipe Manana 			if (ret)
543d8ccbd21SFilipe Manana 				return ret;
544d8ccbd21SFilipe Manana 			if (total_added_ret)
545d8ccbd21SFilipe Manana 				*total_added_ret += size;
5469f21246dSJosef Bacik 			start = extent_end + 1;
5479f21246dSJosef Bacik 		} else {
5489f21246dSJosef Bacik 			break;
5499f21246dSJosef Bacik 		}
5509f21246dSJosef Bacik 	}
5519f21246dSJosef Bacik 
5529f21246dSJosef Bacik 	if (start < end) {
5539f21246dSJosef Bacik 		size = end - start;
554b0643e59SDennis Zhou 		ret = btrfs_add_free_space_async_trimmed(block_group, start,
555b0643e59SDennis Zhou 							 size);
556d8ccbd21SFilipe Manana 		if (ret)
557d8ccbd21SFilipe Manana 			return ret;
558d8ccbd21SFilipe Manana 		if (total_added_ret)
559d8ccbd21SFilipe Manana 			*total_added_ret += size;
5609f21246dSJosef Bacik 	}
5619f21246dSJosef Bacik 
562d8ccbd21SFilipe Manana 	return 0;
5639f21246dSJosef Bacik }
5649f21246dSJosef Bacik 
565c7eec3d9SBoris Burkov /*
566c7eec3d9SBoris Burkov  * Get an arbitrary extent item index / max_index through the block group
567c7eec3d9SBoris Burkov  *
568c7eec3d9SBoris Burkov  * @block_group   the block group to sample from
569c7eec3d9SBoris Burkov  * @index:        the integral step through the block group to grab from
570c7eec3d9SBoris Burkov  * @max_index:    the granularity of the sampling
571c7eec3d9SBoris Burkov  * @key:          return value parameter for the item we find
572c7eec3d9SBoris Burkov  *
573c7eec3d9SBoris Burkov  * Pre-conditions on indices:
574c7eec3d9SBoris Burkov  * 0 <= index <= max_index
575c7eec3d9SBoris Burkov  * 0 < max_index
576c7eec3d9SBoris Burkov  *
577c7eec3d9SBoris Burkov  * Returns: 0 on success, 1 if the search didn't yield a useful item, negative
578c7eec3d9SBoris Burkov  * error code on error.
579c7eec3d9SBoris Burkov  */
sample_block_group_extent_item(struct btrfs_caching_control * caching_ctl,struct btrfs_block_group * block_group,int index,int max_index,struct btrfs_key * found_key)580c7eec3d9SBoris Burkov static int sample_block_group_extent_item(struct btrfs_caching_control *caching_ctl,
581c7eec3d9SBoris Burkov 					  struct btrfs_block_group *block_group,
582c7eec3d9SBoris Burkov 					  int index, int max_index,
58312148367SBoris Burkov 					  struct btrfs_key *found_key)
584c7eec3d9SBoris Burkov {
585c7eec3d9SBoris Burkov 	struct btrfs_fs_info *fs_info = block_group->fs_info;
586c7eec3d9SBoris Burkov 	struct btrfs_root *extent_root;
587c7eec3d9SBoris Burkov 	u64 search_offset;
588c7eec3d9SBoris Burkov 	u64 search_end = block_group->start + block_group->length;
589c7eec3d9SBoris Burkov 	struct btrfs_path *path;
59012148367SBoris Burkov 	struct btrfs_key search_key;
59112148367SBoris Burkov 	int ret = 0;
592c7eec3d9SBoris Burkov 
593c7eec3d9SBoris Burkov 	ASSERT(index >= 0);
594c7eec3d9SBoris Burkov 	ASSERT(index <= max_index);
595c7eec3d9SBoris Burkov 	ASSERT(max_index > 0);
596c7eec3d9SBoris Burkov 	lockdep_assert_held(&caching_ctl->mutex);
597c7eec3d9SBoris Burkov 	lockdep_assert_held_read(&fs_info->commit_root_sem);
598c7eec3d9SBoris Burkov 
599c7eec3d9SBoris Burkov 	path = btrfs_alloc_path();
600c7eec3d9SBoris Burkov 	if (!path)
601c7eec3d9SBoris Burkov 		return -ENOMEM;
602c7eec3d9SBoris Burkov 
603c7eec3d9SBoris Burkov 	extent_root = btrfs_extent_root(fs_info, max_t(u64, block_group->start,
604c7eec3d9SBoris Burkov 						       BTRFS_SUPER_INFO_OFFSET));
605c7eec3d9SBoris Burkov 
606c7eec3d9SBoris Burkov 	path->skip_locking = 1;
607c7eec3d9SBoris Burkov 	path->search_commit_root = 1;
608c7eec3d9SBoris Burkov 	path->reada = READA_FORWARD;
609c7eec3d9SBoris Burkov 
610c7eec3d9SBoris Burkov 	search_offset = index * div_u64(block_group->length, max_index);
61112148367SBoris Burkov 	search_key.objectid = block_group->start + search_offset;
61212148367SBoris Burkov 	search_key.type = BTRFS_EXTENT_ITEM_KEY;
61312148367SBoris Burkov 	search_key.offset = 0;
614c7eec3d9SBoris Burkov 
61512148367SBoris Burkov 	btrfs_for_each_slot(extent_root, &search_key, found_key, path, ret) {
616c7eec3d9SBoris Burkov 		/* Success; sampled an extent item in the block group */
61712148367SBoris Burkov 		if (found_key->type == BTRFS_EXTENT_ITEM_KEY &&
61812148367SBoris Burkov 		    found_key->objectid >= block_group->start &&
61912148367SBoris Burkov 		    found_key->objectid + found_key->offset <= search_end)
62012148367SBoris Burkov 			break;
621c7eec3d9SBoris Burkov 
622c7eec3d9SBoris Burkov 		/* We can't possibly find a valid extent item anymore */
62312148367SBoris Burkov 		if (found_key->objectid >= search_end) {
624c7eec3d9SBoris Burkov 			ret = 1;
625c7eec3d9SBoris Burkov 			break;
626c7eec3d9SBoris Burkov 		}
627c7eec3d9SBoris Burkov 	}
62812148367SBoris Burkov 
629c7eec3d9SBoris Burkov 	lockdep_assert_held(&caching_ctl->mutex);
630c7eec3d9SBoris Burkov 	lockdep_assert_held_read(&fs_info->commit_root_sem);
631c7eec3d9SBoris Burkov 	btrfs_free_path(path);
632c7eec3d9SBoris Burkov 	return ret;
633c7eec3d9SBoris Burkov }
634c7eec3d9SBoris Burkov 
635c7eec3d9SBoris Burkov /*
636c7eec3d9SBoris Burkov  * Best effort attempt to compute a block group's size class while caching it.
637c7eec3d9SBoris Burkov  *
638c7eec3d9SBoris Burkov  * @block_group: the block group we are caching
639c7eec3d9SBoris Burkov  *
640c7eec3d9SBoris Burkov  * We cannot infer the size class while adding free space extents, because that
641c7eec3d9SBoris Burkov  * logic doesn't care about contiguous file extents (it doesn't differentiate
642c7eec3d9SBoris Burkov  * between a 100M extent and 100 contiguous 1M extents). So we need to read the
643c7eec3d9SBoris Burkov  * file extent items. Reading all of them is quite wasteful, because usually
644c7eec3d9SBoris Burkov  * only a handful are enough to give a good answer. Therefore, we just grab 5 of
645c7eec3d9SBoris Burkov  * them at even steps through the block group and pick the smallest size class
646c7eec3d9SBoris Burkov  * we see. Since size class is best effort, and not guaranteed in general,
647c7eec3d9SBoris Burkov  * inaccuracy is acceptable.
648c7eec3d9SBoris Burkov  *
649c7eec3d9SBoris Burkov  * To be more explicit about why this algorithm makes sense:
650c7eec3d9SBoris Burkov  *
651c7eec3d9SBoris Burkov  * If we are caching in a block group from disk, then there are three major cases
652c7eec3d9SBoris Burkov  * to consider:
653c7eec3d9SBoris Burkov  * 1. the block group is well behaved and all extents in it are the same size
654c7eec3d9SBoris Burkov  *    class.
655c7eec3d9SBoris Burkov  * 2. the block group is mostly one size class with rare exceptions for last
656c7eec3d9SBoris Burkov  *    ditch allocations
657c7eec3d9SBoris Burkov  * 3. the block group was populated before size classes and can have a totally
658c7eec3d9SBoris Burkov  *    arbitrary mix of size classes.
659c7eec3d9SBoris Burkov  *
660c7eec3d9SBoris Burkov  * In case 1, looking at any extent in the block group will yield the correct
661c7eec3d9SBoris Burkov  * result. For the mixed cases, taking the minimum size class seems like a good
662c7eec3d9SBoris Burkov  * approximation, since gaps from frees will be usable to the size class. For
663c7eec3d9SBoris Burkov  * 2., a small handful of file extents is likely to yield the right answer. For
664c7eec3d9SBoris Burkov  * 3, we can either read every file extent, or admit that this is best effort
665c7eec3d9SBoris Burkov  * anyway and try to stay fast.
666c7eec3d9SBoris Burkov  *
667c7eec3d9SBoris Burkov  * Returns: 0 on success, negative error code on error.
668c7eec3d9SBoris Burkov  */
load_block_group_size_class(struct btrfs_caching_control * caching_ctl,struct btrfs_block_group * block_group)669c7eec3d9SBoris Burkov static int load_block_group_size_class(struct btrfs_caching_control *caching_ctl,
670c7eec3d9SBoris Burkov 				       struct btrfs_block_group *block_group)
671c7eec3d9SBoris Burkov {
67212148367SBoris Burkov 	struct btrfs_fs_info *fs_info = block_group->fs_info;
673c7eec3d9SBoris Burkov 	struct btrfs_key key;
674c7eec3d9SBoris Burkov 	int i;
675c7eec3d9SBoris Burkov 	u64 min_size = block_group->length;
676c7eec3d9SBoris Burkov 	enum btrfs_block_group_size_class size_class = BTRFS_BG_SZ_NONE;
677c7eec3d9SBoris Burkov 	int ret;
678c7eec3d9SBoris Burkov 
679cb0922f2SBoris Burkov 	if (!btrfs_block_group_should_use_size_class(block_group))
680c7eec3d9SBoris Burkov 		return 0;
681c7eec3d9SBoris Burkov 
68212148367SBoris Burkov 	lockdep_assert_held(&caching_ctl->mutex);
68312148367SBoris Burkov 	lockdep_assert_held_read(&fs_info->commit_root_sem);
684c7eec3d9SBoris Burkov 	for (i = 0; i < 5; ++i) {
685c7eec3d9SBoris Burkov 		ret = sample_block_group_extent_item(caching_ctl, block_group, i, 5, &key);
686c7eec3d9SBoris Burkov 		if (ret < 0)
687c7eec3d9SBoris Burkov 			goto out;
688c7eec3d9SBoris Burkov 		if (ret > 0)
689c7eec3d9SBoris Burkov 			continue;
690c7eec3d9SBoris Burkov 		min_size = min_t(u64, min_size, key.offset);
691c7eec3d9SBoris Burkov 		size_class = btrfs_calc_block_group_size_class(min_size);
692c7eec3d9SBoris Burkov 	}
693c7eec3d9SBoris Burkov 	if (size_class != BTRFS_BG_SZ_NONE) {
694c7eec3d9SBoris Burkov 		spin_lock(&block_group->lock);
695c7eec3d9SBoris Burkov 		block_group->size_class = size_class;
696c7eec3d9SBoris Burkov 		spin_unlock(&block_group->lock);
697c7eec3d9SBoris Burkov 	}
698c7eec3d9SBoris Burkov out:
699c7eec3d9SBoris Burkov 	return ret;
700c7eec3d9SBoris Burkov }
701c7eec3d9SBoris Burkov 
load_extent_tree_free(struct btrfs_caching_control * caching_ctl)7029f21246dSJosef Bacik static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
7039f21246dSJosef Bacik {
70432da5386SDavid Sterba 	struct btrfs_block_group *block_group = caching_ctl->block_group;
7059f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
70629cbcf40SJosef Bacik 	struct btrfs_root *extent_root;
7079f21246dSJosef Bacik 	struct btrfs_path *path;
7089f21246dSJosef Bacik 	struct extent_buffer *leaf;
7099f21246dSJosef Bacik 	struct btrfs_key key;
7109f21246dSJosef Bacik 	u64 total_found = 0;
7119f21246dSJosef Bacik 	u64 last = 0;
7129f21246dSJosef Bacik 	u32 nritems;
7139f21246dSJosef Bacik 	int ret;
7149f21246dSJosef Bacik 	bool wakeup = true;
7159f21246dSJosef Bacik 
7169f21246dSJosef Bacik 	path = btrfs_alloc_path();
7179f21246dSJosef Bacik 	if (!path)
7189f21246dSJosef Bacik 		return -ENOMEM;
7199f21246dSJosef Bacik 
720b3470b5dSDavid Sterba 	last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET);
72129cbcf40SJosef Bacik 	extent_root = btrfs_extent_root(fs_info, last);
7229f21246dSJosef Bacik 
7239f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
7249f21246dSJosef Bacik 	/*
7259f21246dSJosef Bacik 	 * If we're fragmenting we don't want to make anybody think we can
7269f21246dSJosef Bacik 	 * allocate from this block group until we've had a chance to fragment
7279f21246dSJosef Bacik 	 * the free space.
7289f21246dSJosef Bacik 	 */
7299f21246dSJosef Bacik 	if (btrfs_should_fragment_free_space(block_group))
7309f21246dSJosef Bacik 		wakeup = false;
7319f21246dSJosef Bacik #endif
7329f21246dSJosef Bacik 	/*
7339f21246dSJosef Bacik 	 * We don't want to deadlock with somebody trying to allocate a new
7349f21246dSJosef Bacik 	 * extent for the extent root while also trying to search the extent
7359f21246dSJosef Bacik 	 * root to add free space.  So we skip locking and search the commit
7369f21246dSJosef Bacik 	 * root, since its read-only
7379f21246dSJosef Bacik 	 */
7389f21246dSJosef Bacik 	path->skip_locking = 1;
7399f21246dSJosef Bacik 	path->search_commit_root = 1;
7409f21246dSJosef Bacik 	path->reada = READA_FORWARD;
7419f21246dSJosef Bacik 
7429f21246dSJosef Bacik 	key.objectid = last;
7439f21246dSJosef Bacik 	key.offset = 0;
7449f21246dSJosef Bacik 	key.type = BTRFS_EXTENT_ITEM_KEY;
7459f21246dSJosef Bacik 
7469f21246dSJosef Bacik next:
7479f21246dSJosef Bacik 	ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
7489f21246dSJosef Bacik 	if (ret < 0)
7499f21246dSJosef Bacik 		goto out;
7509f21246dSJosef Bacik 
7519f21246dSJosef Bacik 	leaf = path->nodes[0];
7529f21246dSJosef Bacik 	nritems = btrfs_header_nritems(leaf);
7539f21246dSJosef Bacik 
7549f21246dSJosef Bacik 	while (1) {
7559f21246dSJosef Bacik 		if (btrfs_fs_closing(fs_info) > 1) {
7569f21246dSJosef Bacik 			last = (u64)-1;
7579f21246dSJosef Bacik 			break;
7589f21246dSJosef Bacik 		}
7599f21246dSJosef Bacik 
7609f21246dSJosef Bacik 		if (path->slots[0] < nritems) {
7619f21246dSJosef Bacik 			btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7629f21246dSJosef Bacik 		} else {
7639f21246dSJosef Bacik 			ret = btrfs_find_next_key(extent_root, path, &key, 0, 0);
7649f21246dSJosef Bacik 			if (ret)
7659f21246dSJosef Bacik 				break;
7669f21246dSJosef Bacik 
7679f21246dSJosef Bacik 			if (need_resched() ||
7689f21246dSJosef Bacik 			    rwsem_is_contended(&fs_info->commit_root_sem)) {
7699f21246dSJosef Bacik 				btrfs_release_path(path);
7709f21246dSJosef Bacik 				up_read(&fs_info->commit_root_sem);
7719f21246dSJosef Bacik 				mutex_unlock(&caching_ctl->mutex);
7729f21246dSJosef Bacik 				cond_resched();
7739f21246dSJosef Bacik 				mutex_lock(&caching_ctl->mutex);
7749f21246dSJosef Bacik 				down_read(&fs_info->commit_root_sem);
7759f21246dSJosef Bacik 				goto next;
7769f21246dSJosef Bacik 			}
7779f21246dSJosef Bacik 
7789f21246dSJosef Bacik 			ret = btrfs_next_leaf(extent_root, path);
7799f21246dSJosef Bacik 			if (ret < 0)
7809f21246dSJosef Bacik 				goto out;
7819f21246dSJosef Bacik 			if (ret)
7829f21246dSJosef Bacik 				break;
7839f21246dSJosef Bacik 			leaf = path->nodes[0];
7849f21246dSJosef Bacik 			nritems = btrfs_header_nritems(leaf);
7859f21246dSJosef Bacik 			continue;
7869f21246dSJosef Bacik 		}
7879f21246dSJosef Bacik 
7889f21246dSJosef Bacik 		if (key.objectid < last) {
7899f21246dSJosef Bacik 			key.objectid = last;
7909f21246dSJosef Bacik 			key.offset = 0;
7919f21246dSJosef Bacik 			key.type = BTRFS_EXTENT_ITEM_KEY;
7929f21246dSJosef Bacik 			btrfs_release_path(path);
7939f21246dSJosef Bacik 			goto next;
7949f21246dSJosef Bacik 		}
7959f21246dSJosef Bacik 
796b3470b5dSDavid Sterba 		if (key.objectid < block_group->start) {
7979f21246dSJosef Bacik 			path->slots[0]++;
7989f21246dSJosef Bacik 			continue;
7999f21246dSJosef Bacik 		}
8009f21246dSJosef Bacik 
801b3470b5dSDavid Sterba 		if (key.objectid >= block_group->start + block_group->length)
8029f21246dSJosef Bacik 			break;
8039f21246dSJosef Bacik 
8049f21246dSJosef Bacik 		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
8059f21246dSJosef Bacik 		    key.type == BTRFS_METADATA_ITEM_KEY) {
806d8ccbd21SFilipe Manana 			u64 space_added;
807d8ccbd21SFilipe Manana 
8083b9f0995SFilipe Manana 			ret = btrfs_add_new_free_space(block_group, last,
8093b9f0995SFilipe Manana 						       key.objectid, &space_added);
810d8ccbd21SFilipe Manana 			if (ret)
811d8ccbd21SFilipe Manana 				goto out;
812d8ccbd21SFilipe Manana 			total_found += space_added;
8139f21246dSJosef Bacik 			if (key.type == BTRFS_METADATA_ITEM_KEY)
8149f21246dSJosef Bacik 				last = key.objectid +
8159f21246dSJosef Bacik 					fs_info->nodesize;
8169f21246dSJosef Bacik 			else
8179f21246dSJosef Bacik 				last = key.objectid + key.offset;
8189f21246dSJosef Bacik 
8199f21246dSJosef Bacik 			if (total_found > CACHING_CTL_WAKE_UP) {
8209f21246dSJosef Bacik 				total_found = 0;
821fc1f91b9SJosef Bacik 				if (wakeup) {
822fc1f91b9SJosef Bacik 					atomic_inc(&caching_ctl->progress);
8239f21246dSJosef Bacik 					wake_up(&caching_ctl->wait);
8249f21246dSJosef Bacik 				}
8259f21246dSJosef Bacik 			}
826fc1f91b9SJosef Bacik 		}
8279f21246dSJosef Bacik 		path->slots[0]++;
8289f21246dSJosef Bacik 	}
8299f21246dSJosef Bacik 
8303b9f0995SFilipe Manana 	ret = btrfs_add_new_free_space(block_group, last,
831d8ccbd21SFilipe Manana 				       block_group->start + block_group->length,
832d8ccbd21SFilipe Manana 				       NULL);
8339f21246dSJosef Bacik out:
8349f21246dSJosef Bacik 	btrfs_free_path(path);
8359f21246dSJosef Bacik 	return ret;
8369f21246dSJosef Bacik }
8379f21246dSJosef Bacik 
btrfs_free_excluded_extents(const struct btrfs_block_group * bg)83898b5a8fdSFilipe Manana static inline void btrfs_free_excluded_extents(const struct btrfs_block_group *bg)
83998b5a8fdSFilipe Manana {
84098b5a8fdSFilipe Manana 	clear_extent_bits(&bg->fs_info->excluded_extents, bg->start,
84198b5a8fdSFilipe Manana 			  bg->start + bg->length - 1, EXTENT_UPTODATE);
84298b5a8fdSFilipe Manana }
84398b5a8fdSFilipe Manana 
caching_thread(struct btrfs_work * work)8449f21246dSJosef Bacik static noinline void caching_thread(struct btrfs_work *work)
8459f21246dSJosef Bacik {
84632da5386SDavid Sterba 	struct btrfs_block_group *block_group;
8479f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info;
8489f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
8499f21246dSJosef Bacik 	int ret;
8509f21246dSJosef Bacik 
8519f21246dSJosef Bacik 	caching_ctl = container_of(work, struct btrfs_caching_control, work);
8529f21246dSJosef Bacik 	block_group = caching_ctl->block_group;
8539f21246dSJosef Bacik 	fs_info = block_group->fs_info;
8549f21246dSJosef Bacik 
8559f21246dSJosef Bacik 	mutex_lock(&caching_ctl->mutex);
8569f21246dSJosef Bacik 	down_read(&fs_info->commit_root_sem);
8579f21246dSJosef Bacik 
858c7eec3d9SBoris Burkov 	load_block_group_size_class(caching_ctl, block_group);
859e747853cSJosef Bacik 	if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
860e747853cSJosef Bacik 		ret = load_free_space_cache(block_group);
861e747853cSJosef Bacik 		if (ret == 1) {
862e747853cSJosef Bacik 			ret = 0;
863e747853cSJosef Bacik 			goto done;
864e747853cSJosef Bacik 		}
865e747853cSJosef Bacik 
866e747853cSJosef Bacik 		/*
867e747853cSJosef Bacik 		 * We failed to load the space cache, set ourselves to
868e747853cSJosef Bacik 		 * CACHE_STARTED and carry on.
869e747853cSJosef Bacik 		 */
870e747853cSJosef Bacik 		spin_lock(&block_group->lock);
871e747853cSJosef Bacik 		block_group->cached = BTRFS_CACHE_STARTED;
872e747853cSJosef Bacik 		spin_unlock(&block_group->lock);
873e747853cSJosef Bacik 		wake_up(&caching_ctl->wait);
874e747853cSJosef Bacik 	}
875e747853cSJosef Bacik 
8762f96e402SJosef Bacik 	/*
8772f96e402SJosef Bacik 	 * If we are in the transaction that populated the free space tree we
8782f96e402SJosef Bacik 	 * can't actually cache from the free space tree as our commit root and
8792f96e402SJosef Bacik 	 * real root are the same, so we could change the contents of the blocks
8802f96e402SJosef Bacik 	 * while caching.  Instead do the slow caching in this case, and after
8812f96e402SJosef Bacik 	 * the transaction has committed we will be safe.
8822f96e402SJosef Bacik 	 */
8832f96e402SJosef Bacik 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
8842f96e402SJosef Bacik 	    !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags)))
8859f21246dSJosef Bacik 		ret = load_free_space_tree(caching_ctl);
8869f21246dSJosef Bacik 	else
8879f21246dSJosef Bacik 		ret = load_extent_tree_free(caching_ctl);
888e747853cSJosef Bacik done:
8899f21246dSJosef Bacik 	spin_lock(&block_group->lock);
8909f21246dSJosef Bacik 	block_group->caching_ctl = NULL;
8919f21246dSJosef Bacik 	block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
8929f21246dSJosef Bacik 	spin_unlock(&block_group->lock);
8939f21246dSJosef Bacik 
8949f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
8959f21246dSJosef Bacik 	if (btrfs_should_fragment_free_space(block_group)) {
8969f21246dSJosef Bacik 		u64 bytes_used;
8979f21246dSJosef Bacik 
8989f21246dSJosef Bacik 		spin_lock(&block_group->space_info->lock);
8999f21246dSJosef Bacik 		spin_lock(&block_group->lock);
900b3470b5dSDavid Sterba 		bytes_used = block_group->length - block_group->used;
9019f21246dSJosef Bacik 		block_group->space_info->bytes_used += bytes_used >> 1;
9029f21246dSJosef Bacik 		spin_unlock(&block_group->lock);
9039f21246dSJosef Bacik 		spin_unlock(&block_group->space_info->lock);
904e11c0406SJosef Bacik 		fragment_free_space(block_group);
9059f21246dSJosef Bacik 	}
9069f21246dSJosef Bacik #endif
9079f21246dSJosef Bacik 
9089f21246dSJosef Bacik 	up_read(&fs_info->commit_root_sem);
9099f21246dSJosef Bacik 	btrfs_free_excluded_extents(block_group);
9109f21246dSJosef Bacik 	mutex_unlock(&caching_ctl->mutex);
9119f21246dSJosef Bacik 
9129f21246dSJosef Bacik 	wake_up(&caching_ctl->wait);
9139f21246dSJosef Bacik 
9149f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
9159f21246dSJosef Bacik 	btrfs_put_block_group(block_group);
9169f21246dSJosef Bacik }
9179f21246dSJosef Bacik 
btrfs_cache_block_group(struct btrfs_block_group * cache,bool wait)918ced8ecf0SOmar Sandoval int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait)
9199f21246dSJosef Bacik {
9209f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
921e747853cSJosef Bacik 	struct btrfs_caching_control *caching_ctl = NULL;
9229f21246dSJosef Bacik 	int ret = 0;
9239f21246dSJosef Bacik 
9242eda5708SNaohiro Aota 	/* Allocator for zoned filesystems does not use the cache at all */
9252eda5708SNaohiro Aota 	if (btrfs_is_zoned(fs_info))
9262eda5708SNaohiro Aota 		return 0;
9272eda5708SNaohiro Aota 
9289f21246dSJosef Bacik 	caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
9299f21246dSJosef Bacik 	if (!caching_ctl)
9309f21246dSJosef Bacik 		return -ENOMEM;
9319f21246dSJosef Bacik 
9329f21246dSJosef Bacik 	INIT_LIST_HEAD(&caching_ctl->list);
9339f21246dSJosef Bacik 	mutex_init(&caching_ctl->mutex);
9349f21246dSJosef Bacik 	init_waitqueue_head(&caching_ctl->wait);
9359f21246dSJosef Bacik 	caching_ctl->block_group = cache;
936e747853cSJosef Bacik 	refcount_set(&caching_ctl->count, 2);
937fc1f91b9SJosef Bacik 	atomic_set(&caching_ctl->progress, 0);
938078b8b90SDavid Sterba 	btrfs_init_work(&caching_ctl->work, caching_thread, NULL);
9399f21246dSJosef Bacik 
9409f21246dSJosef Bacik 	spin_lock(&cache->lock);
9419f21246dSJosef Bacik 	if (cache->cached != BTRFS_CACHE_NO) {
9429f21246dSJosef Bacik 		kfree(caching_ctl);
943e747853cSJosef Bacik 
944e747853cSJosef Bacik 		caching_ctl = cache->caching_ctl;
945e747853cSJosef Bacik 		if (caching_ctl)
946e747853cSJosef Bacik 			refcount_inc(&caching_ctl->count);
947e747853cSJosef Bacik 		spin_unlock(&cache->lock);
948e747853cSJosef Bacik 		goto out;
9499f21246dSJosef Bacik 	}
9509f21246dSJosef Bacik 	WARN_ON(cache->caching_ctl);
9519f21246dSJosef Bacik 	cache->caching_ctl = caching_ctl;
9529f21246dSJosef Bacik 	cache->cached = BTRFS_CACHE_STARTED;
9539f21246dSJosef Bacik 	spin_unlock(&cache->lock);
9549f21246dSJosef Bacik 
95516b0c258SFilipe Manana 	write_lock(&fs_info->block_group_cache_lock);
9569f21246dSJosef Bacik 	refcount_inc(&caching_ctl->count);
9579f21246dSJosef Bacik 	list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
95816b0c258SFilipe Manana 	write_unlock(&fs_info->block_group_cache_lock);
9599f21246dSJosef Bacik 
9609f21246dSJosef Bacik 	btrfs_get_block_group(cache);
9619f21246dSJosef Bacik 
9629f21246dSJosef Bacik 	btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
963e747853cSJosef Bacik out:
964ced8ecf0SOmar Sandoval 	if (wait && caching_ctl)
965ced8ecf0SOmar Sandoval 		ret = btrfs_caching_ctl_wait_done(cache, caching_ctl);
966e747853cSJosef Bacik 	if (caching_ctl)
967e747853cSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
9689f21246dSJosef Bacik 
9699f21246dSJosef Bacik 	return ret;
9709f21246dSJosef Bacik }
971e3e0520bSJosef Bacik 
clear_avail_alloc_bits(struct btrfs_fs_info * fs_info,u64 flags)972e3e0520bSJosef Bacik static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
973e3e0520bSJosef Bacik {
974e3e0520bSJosef Bacik 	u64 extra_flags = chunk_to_extended(flags) &
975e3e0520bSJosef Bacik 				BTRFS_EXTENDED_PROFILE_MASK;
976e3e0520bSJosef Bacik 
977e3e0520bSJosef Bacik 	write_seqlock(&fs_info->profiles_lock);
978e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA)
979e3e0520bSJosef Bacik 		fs_info->avail_data_alloc_bits &= ~extra_flags;
980e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_METADATA)
981e3e0520bSJosef Bacik 		fs_info->avail_metadata_alloc_bits &= ~extra_flags;
982e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
983e3e0520bSJosef Bacik 		fs_info->avail_system_alloc_bits &= ~extra_flags;
984e3e0520bSJosef Bacik 	write_sequnlock(&fs_info->profiles_lock);
985e3e0520bSJosef Bacik }
986e3e0520bSJosef Bacik 
987e3e0520bSJosef Bacik /*
988e3e0520bSJosef Bacik  * Clear incompat bits for the following feature(s):
989e3e0520bSJosef Bacik  *
990e3e0520bSJosef Bacik  * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
991e3e0520bSJosef Bacik  *            in the whole filesystem
9929c907446SDavid Sterba  *
9939c907446SDavid Sterba  * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
994e3e0520bSJosef Bacik  */
clear_incompat_bg_bits(struct btrfs_fs_info * fs_info,u64 flags)995e3e0520bSJosef Bacik static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags)
996e3e0520bSJosef Bacik {
9979c907446SDavid Sterba 	bool found_raid56 = false;
9989c907446SDavid Sterba 	bool found_raid1c34 = false;
9999c907446SDavid Sterba 
10009c907446SDavid Sterba 	if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) ||
10019c907446SDavid Sterba 	    (flags & BTRFS_BLOCK_GROUP_RAID1C3) ||
10029c907446SDavid Sterba 	    (flags & BTRFS_BLOCK_GROUP_RAID1C4)) {
1003e3e0520bSJosef Bacik 		struct list_head *head = &fs_info->space_info;
1004e3e0520bSJosef Bacik 		struct btrfs_space_info *sinfo;
1005e3e0520bSJosef Bacik 
1006e3e0520bSJosef Bacik 		list_for_each_entry_rcu(sinfo, head, list) {
1007e3e0520bSJosef Bacik 			down_read(&sinfo->groups_sem);
1008e3e0520bSJosef Bacik 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5]))
10099c907446SDavid Sterba 				found_raid56 = true;
1010e3e0520bSJosef Bacik 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6]))
10119c907446SDavid Sterba 				found_raid56 = true;
10129c907446SDavid Sterba 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3]))
10139c907446SDavid Sterba 				found_raid1c34 = true;
10149c907446SDavid Sterba 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4]))
10159c907446SDavid Sterba 				found_raid1c34 = true;
1016e3e0520bSJosef Bacik 			up_read(&sinfo->groups_sem);
1017e3e0520bSJosef Bacik 		}
1018d8e6fd5cSFilipe Manana 		if (!found_raid56)
1019e3e0520bSJosef Bacik 			btrfs_clear_fs_incompat(fs_info, RAID56);
1020d8e6fd5cSFilipe Manana 		if (!found_raid1c34)
10219c907446SDavid Sterba 			btrfs_clear_fs_incompat(fs_info, RAID1C34);
1022e3e0520bSJosef Bacik 	}
1023e3e0520bSJosef Bacik }
1024e3e0520bSJosef Bacik 
btrfs_block_group_root(struct btrfs_fs_info * fs_info)102583937fb6SAnand Jain static struct btrfs_root *btrfs_block_group_root(struct btrfs_fs_info *fs_info)
102683937fb6SAnand Jain {
102783937fb6SAnand Jain 	if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE))
102883937fb6SAnand Jain 		return fs_info->block_group_root;
102983937fb6SAnand Jain 	return btrfs_extent_root(fs_info, 0);
103083937fb6SAnand Jain }
103183937fb6SAnand Jain 
remove_block_group_item(struct btrfs_trans_handle * trans,struct btrfs_path * path,struct btrfs_block_group * block_group)10327357623aSQu Wenruo static int remove_block_group_item(struct btrfs_trans_handle *trans,
10337357623aSQu Wenruo 				   struct btrfs_path *path,
10347357623aSQu Wenruo 				   struct btrfs_block_group *block_group)
10357357623aSQu Wenruo {
10367357623aSQu Wenruo 	struct btrfs_fs_info *fs_info = trans->fs_info;
10377357623aSQu Wenruo 	struct btrfs_root *root;
10387357623aSQu Wenruo 	struct btrfs_key key;
10397357623aSQu Wenruo 	int ret;
10407357623aSQu Wenruo 
1041dfe8aec4SJosef Bacik 	root = btrfs_block_group_root(fs_info);
10427357623aSQu Wenruo 	key.objectid = block_group->start;
10437357623aSQu Wenruo 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
10447357623aSQu Wenruo 	key.offset = block_group->length;
10457357623aSQu Wenruo 
10467357623aSQu Wenruo 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10477357623aSQu Wenruo 	if (ret > 0)
10487357623aSQu Wenruo 		ret = -ENOENT;
10497357623aSQu Wenruo 	if (ret < 0)
10507357623aSQu Wenruo 		return ret;
10517357623aSQu Wenruo 
10527357623aSQu Wenruo 	ret = btrfs_del_item(trans, root, path);
10537357623aSQu Wenruo 	return ret;
10547357623aSQu Wenruo }
10557357623aSQu Wenruo 
btrfs_remove_block_group(struct btrfs_trans_handle * trans,struct btrfs_chunk_map * map)1056e3e0520bSJosef Bacik int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10577dc66abbSFilipe Manana 			     struct btrfs_chunk_map *map)
1058e3e0520bSJosef Bacik {
1059e3e0520bSJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
1060e3e0520bSJosef Bacik 	struct btrfs_path *path;
106132da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1062e3e0520bSJosef Bacik 	struct btrfs_free_cluster *cluster;
1063e3e0520bSJosef Bacik 	struct inode *inode;
1064e3e0520bSJosef Bacik 	struct kobject *kobj = NULL;
1065e3e0520bSJosef Bacik 	int ret;
1066e3e0520bSJosef Bacik 	int index;
1067e3e0520bSJosef Bacik 	int factor;
1068e3e0520bSJosef Bacik 	struct btrfs_caching_control *caching_ctl = NULL;
10697dc66abbSFilipe Manana 	bool remove_map;
1070e3e0520bSJosef Bacik 	bool remove_rsv = false;
1071e3e0520bSJosef Bacik 
10727dc66abbSFilipe Manana 	block_group = btrfs_lookup_block_group(fs_info, map->start);
107397ec3320SDavid Sterba 	if (!block_group)
107497ec3320SDavid Sterba 		return -ENOENT;
107597ec3320SDavid Sterba 
1076e3e0520bSJosef Bacik 	BUG_ON(!block_group->ro);
1077e3e0520bSJosef Bacik 
1078e3e0520bSJosef Bacik 	trace_btrfs_remove_block_group(block_group);
1079e3e0520bSJosef Bacik 	/*
1080e3e0520bSJosef Bacik 	 * Free the reserved super bytes from this block group before
1081e3e0520bSJosef Bacik 	 * remove it.
1082e3e0520bSJosef Bacik 	 */
1083e3e0520bSJosef Bacik 	btrfs_free_excluded_extents(block_group);
1084b3470b5dSDavid Sterba 	btrfs_free_ref_tree_range(fs_info, block_group->start,
1085b3470b5dSDavid Sterba 				  block_group->length);
1086e3e0520bSJosef Bacik 
1087e3e0520bSJosef Bacik 	index = btrfs_bg_flags_to_raid_index(block_group->flags);
1088e3e0520bSJosef Bacik 	factor = btrfs_bg_type_to_factor(block_group->flags);
1089e3e0520bSJosef Bacik 
1090e3e0520bSJosef Bacik 	/* make sure this block group isn't part of an allocation cluster */
1091e3e0520bSJosef Bacik 	cluster = &fs_info->data_alloc_cluster;
1092e3e0520bSJosef Bacik 	spin_lock(&cluster->refill_lock);
1093e3e0520bSJosef Bacik 	btrfs_return_cluster_to_free_space(block_group, cluster);
1094e3e0520bSJosef Bacik 	spin_unlock(&cluster->refill_lock);
1095e3e0520bSJosef Bacik 
1096e3e0520bSJosef Bacik 	/*
1097e3e0520bSJosef Bacik 	 * make sure this block group isn't part of a metadata
1098e3e0520bSJosef Bacik 	 * allocation cluster
1099e3e0520bSJosef Bacik 	 */
1100e3e0520bSJosef Bacik 	cluster = &fs_info->meta_alloc_cluster;
1101e3e0520bSJosef Bacik 	spin_lock(&cluster->refill_lock);
1102e3e0520bSJosef Bacik 	btrfs_return_cluster_to_free_space(block_group, cluster);
1103e3e0520bSJosef Bacik 	spin_unlock(&cluster->refill_lock);
1104e3e0520bSJosef Bacik 
110540ab3be1SNaohiro Aota 	btrfs_clear_treelog_bg(block_group);
1106c2707a25SJohannes Thumshirn 	btrfs_clear_data_reloc_bg(block_group);
110740ab3be1SNaohiro Aota 
1108e3e0520bSJosef Bacik 	path = btrfs_alloc_path();
1109e3e0520bSJosef Bacik 	if (!path) {
1110e3e0520bSJosef Bacik 		ret = -ENOMEM;
11119fecd132SFilipe Manana 		goto out;
1112e3e0520bSJosef Bacik 	}
1113e3e0520bSJosef Bacik 
1114e3e0520bSJosef Bacik 	/*
1115e3e0520bSJosef Bacik 	 * get the inode first so any iput calls done for the io_list
1116e3e0520bSJosef Bacik 	 * aren't the final iput (no unlinks allowed now)
1117e3e0520bSJosef Bacik 	 */
1118e3e0520bSJosef Bacik 	inode = lookup_free_space_inode(block_group, path);
1119e3e0520bSJosef Bacik 
1120e3e0520bSJosef Bacik 	mutex_lock(&trans->transaction->cache_write_mutex);
1121e3e0520bSJosef Bacik 	/*
1122e3e0520bSJosef Bacik 	 * Make sure our free space cache IO is done before removing the
1123e3e0520bSJosef Bacik 	 * free space inode
1124e3e0520bSJosef Bacik 	 */
1125e3e0520bSJosef Bacik 	spin_lock(&trans->transaction->dirty_bgs_lock);
1126e3e0520bSJosef Bacik 	if (!list_empty(&block_group->io_list)) {
1127e3e0520bSJosef Bacik 		list_del_init(&block_group->io_list);
1128e3e0520bSJosef Bacik 
1129e3e0520bSJosef Bacik 		WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
1130e3e0520bSJosef Bacik 
1131e3e0520bSJosef Bacik 		spin_unlock(&trans->transaction->dirty_bgs_lock);
1132e3e0520bSJosef Bacik 		btrfs_wait_cache_io(trans, block_group, path);
1133e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
1134e3e0520bSJosef Bacik 		spin_lock(&trans->transaction->dirty_bgs_lock);
1135e3e0520bSJosef Bacik 	}
1136e3e0520bSJosef Bacik 
1137e3e0520bSJosef Bacik 	if (!list_empty(&block_group->dirty_list)) {
1138e3e0520bSJosef Bacik 		list_del_init(&block_group->dirty_list);
1139e3e0520bSJosef Bacik 		remove_rsv = true;
1140e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
1141e3e0520bSJosef Bacik 	}
1142e3e0520bSJosef Bacik 	spin_unlock(&trans->transaction->dirty_bgs_lock);
1143e3e0520bSJosef Bacik 	mutex_unlock(&trans->transaction->cache_write_mutex);
1144e3e0520bSJosef Bacik 
114536b216c8SBoris Burkov 	ret = btrfs_remove_free_space_inode(trans, inode, block_group);
1146e3e0520bSJosef Bacik 	if (ret)
11479fecd132SFilipe Manana 		goto out;
1148e3e0520bSJosef Bacik 
114916b0c258SFilipe Manana 	write_lock(&fs_info->block_group_cache_lock);
115008dddb29SFilipe Manana 	rb_erase_cached(&block_group->cache_node,
1151e3e0520bSJosef Bacik 			&fs_info->block_group_cache_tree);
1152e3e0520bSJosef Bacik 	RB_CLEAR_NODE(&block_group->cache_node);
1153e3e0520bSJosef Bacik 
11549fecd132SFilipe Manana 	/* Once for the block groups rbtree */
11559fecd132SFilipe Manana 	btrfs_put_block_group(block_group);
11569fecd132SFilipe Manana 
115716b0c258SFilipe Manana 	write_unlock(&fs_info->block_group_cache_lock);
1158e3e0520bSJosef Bacik 
1159e3e0520bSJosef Bacik 	down_write(&block_group->space_info->groups_sem);
1160e3e0520bSJosef Bacik 	/*
1161e3e0520bSJosef Bacik 	 * we must use list_del_init so people can check to see if they
1162e3e0520bSJosef Bacik 	 * are still on the list after taking the semaphore
1163e3e0520bSJosef Bacik 	 */
1164e3e0520bSJosef Bacik 	list_del_init(&block_group->list);
1165e3e0520bSJosef Bacik 	if (list_empty(&block_group->space_info->block_groups[index])) {
1166e3e0520bSJosef Bacik 		kobj = block_group->space_info->block_group_kobjs[index];
1167e3e0520bSJosef Bacik 		block_group->space_info->block_group_kobjs[index] = NULL;
1168e3e0520bSJosef Bacik 		clear_avail_alloc_bits(fs_info, block_group->flags);
1169e3e0520bSJosef Bacik 	}
1170e3e0520bSJosef Bacik 	up_write(&block_group->space_info->groups_sem);
1171e3e0520bSJosef Bacik 	clear_incompat_bg_bits(fs_info, block_group->flags);
1172e3e0520bSJosef Bacik 	if (kobj) {
1173e3e0520bSJosef Bacik 		kobject_del(kobj);
1174e3e0520bSJosef Bacik 		kobject_put(kobj);
1175e3e0520bSJosef Bacik 	}
1176e3e0520bSJosef Bacik 
1177e3e0520bSJosef Bacik 	if (block_group->cached == BTRFS_CACHE_STARTED)
1178e3e0520bSJosef Bacik 		btrfs_wait_block_group_cache_done(block_group);
11797b9c293bSJosef Bacik 
118016b0c258SFilipe Manana 	write_lock(&fs_info->block_group_cache_lock);
11817b9c293bSJosef Bacik 	caching_ctl = btrfs_get_caching_control(block_group);
1182e3e0520bSJosef Bacik 	if (!caching_ctl) {
1183e3e0520bSJosef Bacik 		struct btrfs_caching_control *ctl;
1184e3e0520bSJosef Bacik 
11857b9c293bSJosef Bacik 		list_for_each_entry(ctl, &fs_info->caching_block_groups, list) {
1186e3e0520bSJosef Bacik 			if (ctl->block_group == block_group) {
1187e3e0520bSJosef Bacik 				caching_ctl = ctl;
1188e3e0520bSJosef Bacik 				refcount_inc(&caching_ctl->count);
1189e3e0520bSJosef Bacik 				break;
1190e3e0520bSJosef Bacik 			}
1191e3e0520bSJosef Bacik 		}
11927b9c293bSJosef Bacik 	}
1193e3e0520bSJosef Bacik 	if (caching_ctl)
1194e3e0520bSJosef Bacik 		list_del_init(&caching_ctl->list);
119516b0c258SFilipe Manana 	write_unlock(&fs_info->block_group_cache_lock);
11967b9c293bSJosef Bacik 
1197e3e0520bSJosef Bacik 	if (caching_ctl) {
1198e3e0520bSJosef Bacik 		/* Once for the caching bgs list and once for us. */
1199e3e0520bSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
1200e3e0520bSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
1201e3e0520bSJosef Bacik 	}
1202e3e0520bSJosef Bacik 
1203e3e0520bSJosef Bacik 	spin_lock(&trans->transaction->dirty_bgs_lock);
1204e3e0520bSJosef Bacik 	WARN_ON(!list_empty(&block_group->dirty_list));
1205e3e0520bSJosef Bacik 	WARN_ON(!list_empty(&block_group->io_list));
1206e3e0520bSJosef Bacik 	spin_unlock(&trans->transaction->dirty_bgs_lock);
1207e3e0520bSJosef Bacik 
1208e3e0520bSJosef Bacik 	btrfs_remove_free_space_cache(block_group);
1209e3e0520bSJosef Bacik 
1210e3e0520bSJosef Bacik 	spin_lock(&block_group->space_info->lock);
1211e3e0520bSJosef Bacik 	list_del_init(&block_group->ro_list);
1212e3e0520bSJosef Bacik 
1213e3e0520bSJosef Bacik 	if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
1214e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->total_bytes
1215b3470b5dSDavid Sterba 			< block_group->length);
1216e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->bytes_readonly
1217169e0da9SNaohiro Aota 			< block_group->length - block_group->zone_unusable);
1218169e0da9SNaohiro Aota 		WARN_ON(block_group->space_info->bytes_zone_unusable
1219169e0da9SNaohiro Aota 			< block_group->zone_unusable);
1220e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->disk_total
1221b3470b5dSDavid Sterba 			< block_group->length * factor);
1222e3e0520bSJosef Bacik 	}
1223b3470b5dSDavid Sterba 	block_group->space_info->total_bytes -= block_group->length;
1224169e0da9SNaohiro Aota 	block_group->space_info->bytes_readonly -=
1225169e0da9SNaohiro Aota 		(block_group->length - block_group->zone_unusable);
12268cd44dd1SNaohiro Aota 	btrfs_space_info_update_bytes_zone_unusable(fs_info, block_group->space_info,
12278cd44dd1SNaohiro Aota 						    -block_group->zone_unusable);
1228b3470b5dSDavid Sterba 	block_group->space_info->disk_total -= block_group->length * factor;
1229e3e0520bSJosef Bacik 
1230e3e0520bSJosef Bacik 	spin_unlock(&block_group->space_info->lock);
1231e3e0520bSJosef Bacik 
1232ffcb9d44SFilipe Manana 	/*
1233ffcb9d44SFilipe Manana 	 * Remove the free space for the block group from the free space tree
1234ffcb9d44SFilipe Manana 	 * and the block group's item from the extent tree before marking the
1235ffcb9d44SFilipe Manana 	 * block group as removed. This is to prevent races with tasks that
1236ffcb9d44SFilipe Manana 	 * freeze and unfreeze a block group, this task and another task
1237ffcb9d44SFilipe Manana 	 * allocating a new block group - the unfreeze task ends up removing
1238ffcb9d44SFilipe Manana 	 * the block group's extent map before the task calling this function
1239ffcb9d44SFilipe Manana 	 * deletes the block group item from the extent tree, allowing for
1240ffcb9d44SFilipe Manana 	 * another task to attempt to create another block group with the same
1241ffcb9d44SFilipe Manana 	 * item key (and failing with -EEXIST and a transaction abort).
1242ffcb9d44SFilipe Manana 	 */
1243ffcb9d44SFilipe Manana 	ret = remove_block_group_free_space(trans, block_group);
1244ffcb9d44SFilipe Manana 	if (ret)
1245ffcb9d44SFilipe Manana 		goto out;
1246ffcb9d44SFilipe Manana 
1247ffcb9d44SFilipe Manana 	ret = remove_block_group_item(trans, path, block_group);
1248ffcb9d44SFilipe Manana 	if (ret < 0)
1249ffcb9d44SFilipe Manana 		goto out;
1250ffcb9d44SFilipe Manana 
1251e3e0520bSJosef Bacik 	spin_lock(&block_group->lock);
12523349b57fSJosef Bacik 	set_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags);
12533349b57fSJosef Bacik 
1254e3e0520bSJosef Bacik 	/*
12556b7304afSFilipe Manana 	 * At this point trimming or scrub can't start on this block group,
12566b7304afSFilipe Manana 	 * because we removed the block group from the rbtree
12576b7304afSFilipe Manana 	 * fs_info->block_group_cache_tree so no one can't find it anymore and
12586b7304afSFilipe Manana 	 * even if someone already got this block group before we removed it
12596b7304afSFilipe Manana 	 * from the rbtree, they have already incremented block_group->frozen -
12606b7304afSFilipe Manana 	 * if they didn't, for the trimming case they won't find any free space
12616b7304afSFilipe Manana 	 * entries because we already removed them all when we called
12626b7304afSFilipe Manana 	 * btrfs_remove_free_space_cache().
1263e3e0520bSJosef Bacik 	 *
12647dc66abbSFilipe Manana 	 * And we must not remove the chunk map from the fs_info->mapping_tree
1265e3e0520bSJosef Bacik 	 * to prevent the same logical address range and physical device space
12666b7304afSFilipe Manana 	 * ranges from being reused for a new block group. This is needed to
12676b7304afSFilipe Manana 	 * avoid races with trimming and scrub.
12686b7304afSFilipe Manana 	 *
12696b7304afSFilipe Manana 	 * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
1270e3e0520bSJosef Bacik 	 * completely transactionless, so while it is trimming a range the
1271e3e0520bSJosef Bacik 	 * currently running transaction might finish and a new one start,
1272e3e0520bSJosef Bacik 	 * allowing for new block groups to be created that can reuse the same
1273e3e0520bSJosef Bacik 	 * physical device locations unless we take this special care.
1274e3e0520bSJosef Bacik 	 *
1275e3e0520bSJosef Bacik 	 * There may also be an implicit trim operation if the file system
1276e3e0520bSJosef Bacik 	 * is mounted with -odiscard. The same protections must remain
1277e3e0520bSJosef Bacik 	 * in place until the extents have been discarded completely when
1278e3e0520bSJosef Bacik 	 * the transaction commit has completed.
1279e3e0520bSJosef Bacik 	 */
12807dc66abbSFilipe Manana 	remove_map = (atomic_read(&block_group->frozen) == 0);
1281e3e0520bSJosef Bacik 	spin_unlock(&block_group->lock);
1282e3e0520bSJosef Bacik 
12837dc66abbSFilipe Manana 	if (remove_map)
12847dc66abbSFilipe Manana 		btrfs_remove_chunk_map(fs_info, map);
1285f6033c5eSXiyu Yang 
12869fecd132SFilipe Manana out:
1287f6033c5eSXiyu Yang 	/* Once for the lookup reference */
1288f6033c5eSXiyu Yang 	btrfs_put_block_group(block_group);
1289e3e0520bSJosef Bacik 	if (remove_rsv)
1290f66e0209SFilipe Manana 		btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
1291e3e0520bSJosef Bacik 	btrfs_free_path(path);
1292e3e0520bSJosef Bacik 	return ret;
1293e3e0520bSJosef Bacik }
1294e3e0520bSJosef Bacik 
btrfs_start_trans_remove_block_group(struct btrfs_fs_info * fs_info,const u64 chunk_offset)1295e3e0520bSJosef Bacik struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
1296e3e0520bSJosef Bacik 		struct btrfs_fs_info *fs_info, const u64 chunk_offset)
1297e3e0520bSJosef Bacik {
1298dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
12997dc66abbSFilipe Manana 	struct btrfs_chunk_map *map;
1300e3e0520bSJosef Bacik 	unsigned int num_items;
1301e3e0520bSJosef Bacik 
13027dc66abbSFilipe Manana 	map = btrfs_find_chunk_map(fs_info, chunk_offset, 1);
13037dc66abbSFilipe Manana 	ASSERT(map != NULL);
13047dc66abbSFilipe Manana 	ASSERT(map->start == chunk_offset);
1305e3e0520bSJosef Bacik 
1306e3e0520bSJosef Bacik 	/*
1307e3e0520bSJosef Bacik 	 * We need to reserve 3 + N units from the metadata space info in order
1308e3e0520bSJosef Bacik 	 * to remove a block group (done at btrfs_remove_chunk() and at
1309e3e0520bSJosef Bacik 	 * btrfs_remove_block_group()), which are used for:
1310e3e0520bSJosef Bacik 	 *
1311e3e0520bSJosef Bacik 	 * 1 unit for adding the free space inode's orphan (located in the tree
1312e3e0520bSJosef Bacik 	 * of tree roots).
1313e3e0520bSJosef Bacik 	 * 1 unit for deleting the block group item (located in the extent
1314e3e0520bSJosef Bacik 	 * tree).
1315e3e0520bSJosef Bacik 	 * 1 unit for deleting the free space item (located in tree of tree
1316e3e0520bSJosef Bacik 	 * roots).
1317e3e0520bSJosef Bacik 	 * N units for deleting N device extent items corresponding to each
1318e3e0520bSJosef Bacik 	 * stripe (located in the device tree).
1319e3e0520bSJosef Bacik 	 *
1320e3e0520bSJosef Bacik 	 * In order to remove a block group we also need to reserve units in the
1321e3e0520bSJosef Bacik 	 * system space info in order to update the chunk tree (update one or
1322e3e0520bSJosef Bacik 	 * more device items and remove one chunk item), but this is done at
1323e3e0520bSJosef Bacik 	 * btrfs_remove_chunk() through a call to check_system_chunk().
1324e3e0520bSJosef Bacik 	 */
1325e3e0520bSJosef Bacik 	num_items = 3 + map->num_stripes;
13267dc66abbSFilipe Manana 	btrfs_free_chunk_map(map);
1327e3e0520bSJosef Bacik 
1328dfe8aec4SJosef Bacik 	return btrfs_start_transaction_fallback_global_rsv(root, num_items);
1329e3e0520bSJosef Bacik }
1330e3e0520bSJosef Bacik 
1331e3e0520bSJosef Bacik /*
133226ce2095SJosef Bacik  * Mark block group @cache read-only, so later write won't happen to block
133326ce2095SJosef Bacik  * group @cache.
133426ce2095SJosef Bacik  *
133526ce2095SJosef Bacik  * If @force is not set, this function will only mark the block group readonly
133626ce2095SJosef Bacik  * if we have enough free space (1M) in other metadata/system block groups.
133726ce2095SJosef Bacik  * If @force is not set, this function will mark the block group readonly
133826ce2095SJosef Bacik  * without checking free space.
133926ce2095SJosef Bacik  *
134026ce2095SJosef Bacik  * NOTE: This function doesn't care if other block groups can contain all the
134126ce2095SJosef Bacik  * data in this block group. That check should be done by relocation routine,
134226ce2095SJosef Bacik  * not this function.
134326ce2095SJosef Bacik  */
inc_block_group_ro(struct btrfs_block_group * cache,int force)134432da5386SDavid Sterba static int inc_block_group_ro(struct btrfs_block_group *cache, int force)
134526ce2095SJosef Bacik {
134626ce2095SJosef Bacik 	struct btrfs_space_info *sinfo = cache->space_info;
134726ce2095SJosef Bacik 	u64 num_bytes;
134826ce2095SJosef Bacik 	int ret = -ENOSPC;
134926ce2095SJosef Bacik 
135026ce2095SJosef Bacik 	spin_lock(&sinfo->lock);
135126ce2095SJosef Bacik 	spin_lock(&cache->lock);
135226ce2095SJosef Bacik 
1353195a49eaSFilipe Manana 	if (cache->swap_extents) {
1354195a49eaSFilipe Manana 		ret = -ETXTBSY;
1355195a49eaSFilipe Manana 		goto out;
1356195a49eaSFilipe Manana 	}
1357195a49eaSFilipe Manana 
135826ce2095SJosef Bacik 	if (cache->ro) {
135926ce2095SJosef Bacik 		cache->ro++;
136026ce2095SJosef Bacik 		ret = 0;
136126ce2095SJosef Bacik 		goto out;
136226ce2095SJosef Bacik 	}
136326ce2095SJosef Bacik 
1364b3470b5dSDavid Sterba 	num_bytes = cache->length - cache->reserved - cache->pinned -
1365169e0da9SNaohiro Aota 		    cache->bytes_super - cache->zone_unusable - cache->used;
136626ce2095SJosef Bacik 
136726ce2095SJosef Bacik 	/*
1368a30a3d20SJosef Bacik 	 * Data never overcommits, even in mixed mode, so do just the straight
1369a30a3d20SJosef Bacik 	 * check of left over space in how much we have allocated.
1370a30a3d20SJosef Bacik 	 */
1371a30a3d20SJosef Bacik 	if (force) {
1372a30a3d20SJosef Bacik 		ret = 0;
1373a30a3d20SJosef Bacik 	} else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) {
1374a30a3d20SJosef Bacik 		u64 sinfo_used = btrfs_space_info_used(sinfo, true);
1375a30a3d20SJosef Bacik 
1376a30a3d20SJosef Bacik 		/*
137726ce2095SJosef Bacik 		 * Here we make sure if we mark this bg RO, we still have enough
1378f8935566SJosef Bacik 		 * free space as buffer.
137926ce2095SJosef Bacik 		 */
1380a30a3d20SJosef Bacik 		if (sinfo_used + num_bytes <= sinfo->total_bytes)
1381a30a3d20SJosef Bacik 			ret = 0;
1382a30a3d20SJosef Bacik 	} else {
1383a30a3d20SJosef Bacik 		/*
1384a30a3d20SJosef Bacik 		 * We overcommit metadata, so we need to do the
1385a30a3d20SJosef Bacik 		 * btrfs_can_overcommit check here, and we need to pass in
1386a30a3d20SJosef Bacik 		 * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of
1387a30a3d20SJosef Bacik 		 * leeway to allow us to mark this block group as read only.
1388a30a3d20SJosef Bacik 		 */
1389a30a3d20SJosef Bacik 		if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes,
1390a30a3d20SJosef Bacik 					 BTRFS_RESERVE_NO_FLUSH))
1391a30a3d20SJosef Bacik 			ret = 0;
1392a30a3d20SJosef Bacik 	}
1393a30a3d20SJosef Bacik 
1394a30a3d20SJosef Bacik 	if (!ret) {
139526ce2095SJosef Bacik 		sinfo->bytes_readonly += num_bytes;
1396169e0da9SNaohiro Aota 		if (btrfs_is_zoned(cache->fs_info)) {
1397169e0da9SNaohiro Aota 			/* Migrate zone_unusable bytes to readonly */
1398169e0da9SNaohiro Aota 			sinfo->bytes_readonly += cache->zone_unusable;
13998cd44dd1SNaohiro Aota 			btrfs_space_info_update_bytes_zone_unusable(cache->fs_info, sinfo,
14008cd44dd1SNaohiro Aota 								    -cache->zone_unusable);
1401169e0da9SNaohiro Aota 			cache->zone_unusable = 0;
1402169e0da9SNaohiro Aota 		}
140326ce2095SJosef Bacik 		cache->ro++;
140426ce2095SJosef Bacik 		list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
140526ce2095SJosef Bacik 	}
140626ce2095SJosef Bacik out:
140726ce2095SJosef Bacik 	spin_unlock(&cache->lock);
140826ce2095SJosef Bacik 	spin_unlock(&sinfo->lock);
140926ce2095SJosef Bacik 	if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) {
141026ce2095SJosef Bacik 		btrfs_info(cache->fs_info,
1411b3470b5dSDavid Sterba 			"unable to make block group %llu ro", cache->start);
141226ce2095SJosef Bacik 		btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0);
141326ce2095SJosef Bacik 	}
141426ce2095SJosef Bacik 	return ret;
141526ce2095SJosef Bacik }
141626ce2095SJosef Bacik 
clean_pinned_extents(struct btrfs_trans_handle * trans,const struct btrfs_block_group * bg)1417fe119a6eSNikolay Borisov static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
1418*ca283ea9SDavid Sterba 				 const struct btrfs_block_group *bg)
141945bb5d6aSNikolay Borisov {
1420*ca283ea9SDavid Sterba 	struct btrfs_fs_info *fs_info = trans->fs_info;
1421fe119a6eSNikolay Borisov 	struct btrfs_transaction *prev_trans = NULL;
142245bb5d6aSNikolay Borisov 	const u64 start = bg->start;
142345bb5d6aSNikolay Borisov 	const u64 end = start + bg->length - 1;
142445bb5d6aSNikolay Borisov 	int ret;
142545bb5d6aSNikolay Borisov 
1426fe119a6eSNikolay Borisov 	spin_lock(&fs_info->trans_lock);
1427fe119a6eSNikolay Borisov 	if (trans->transaction->list.prev != &fs_info->trans_list) {
1428fe119a6eSNikolay Borisov 		prev_trans = list_last_entry(&trans->transaction->list,
1429fe119a6eSNikolay Borisov 					     struct btrfs_transaction, list);
1430fe119a6eSNikolay Borisov 		refcount_inc(&prev_trans->use_count);
1431fe119a6eSNikolay Borisov 	}
1432fe119a6eSNikolay Borisov 	spin_unlock(&fs_info->trans_lock);
1433fe119a6eSNikolay Borisov 
143445bb5d6aSNikolay Borisov 	/*
143545bb5d6aSNikolay Borisov 	 * Hold the unused_bg_unpin_mutex lock to avoid racing with
143645bb5d6aSNikolay Borisov 	 * btrfs_finish_extent_commit(). If we are at transaction N, another
143745bb5d6aSNikolay Borisov 	 * task might be running finish_extent_commit() for the previous
143845bb5d6aSNikolay Borisov 	 * transaction N - 1, and have seen a range belonging to the block
1439fe119a6eSNikolay Borisov 	 * group in pinned_extents before we were able to clear the whole block
1440fe119a6eSNikolay Borisov 	 * group range from pinned_extents. This means that task can lookup for
1441fe119a6eSNikolay Borisov 	 * the block group after we unpinned it from pinned_extents and removed
144244a6c343SDavid Sterba 	 * it, leading to an error at unpin_extent_range().
144345bb5d6aSNikolay Borisov 	 */
144445bb5d6aSNikolay Borisov 	mutex_lock(&fs_info->unused_bg_unpin_mutex);
1445fe119a6eSNikolay Borisov 	if (prev_trans) {
1446fe119a6eSNikolay Borisov 		ret = clear_extent_bits(&prev_trans->pinned_extents, start, end,
144745bb5d6aSNikolay Borisov 					EXTENT_DIRTY);
144845bb5d6aSNikolay Borisov 		if (ret)
1449534cf531SFilipe Manana 			goto out;
1450fe119a6eSNikolay Borisov 	}
145145bb5d6aSNikolay Borisov 
1452fe119a6eSNikolay Borisov 	ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end,
145345bb5d6aSNikolay Borisov 				EXTENT_DIRTY);
1454534cf531SFilipe Manana out:
145545bb5d6aSNikolay Borisov 	mutex_unlock(&fs_info->unused_bg_unpin_mutex);
14565150bf19SFilipe Manana 	if (prev_trans)
14575150bf19SFilipe Manana 		btrfs_put_transaction(prev_trans);
145845bb5d6aSNikolay Borisov 
1459534cf531SFilipe Manana 	return ret == 0;
146045bb5d6aSNikolay Borisov }
146145bb5d6aSNikolay Borisov 
146226ce2095SJosef Bacik /*
1463e3e0520bSJosef Bacik  * Process the unused_bgs list and remove any that don't have any allocated
1464e3e0520bSJosef Bacik  * space inside of them.
1465e3e0520bSJosef Bacik  */
btrfs_delete_unused_bgs(struct btrfs_fs_info * fs_info)1466e3e0520bSJosef Bacik void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
1467e3e0520bSJosef Bacik {
1468f4a9f219SFilipe Manana 	LIST_HEAD(retry_list);
146932da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1470e3e0520bSJosef Bacik 	struct btrfs_space_info *space_info;
1471e3e0520bSJosef Bacik 	struct btrfs_trans_handle *trans;
14726e80d4f8SDennis Zhou 	const bool async_trim_enabled = btrfs_test_opt(fs_info, DISCARD_ASYNC);
1473e3e0520bSJosef Bacik 	int ret = 0;
1474e3e0520bSJosef Bacik 
1475e3e0520bSJosef Bacik 	if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1476e3e0520bSJosef Bacik 		return;
1477e3e0520bSJosef Bacik 
14782f12741fSJosef Bacik 	if (btrfs_fs_closing(fs_info))
14792f12741fSJosef Bacik 		return;
14802f12741fSJosef Bacik 
1481ddfd08cbSJosef Bacik 	/*
1482ddfd08cbSJosef Bacik 	 * Long running balances can keep us blocked here for eternity, so
1483ddfd08cbSJosef Bacik 	 * simply skip deletion if we're unable to get the mutex.
1484ddfd08cbSJosef Bacik 	 */
1485f3372065SJohannes Thumshirn 	if (!mutex_trylock(&fs_info->reclaim_bgs_lock))
1486ddfd08cbSJosef Bacik 		return;
1487ddfd08cbSJosef Bacik 
1488e3e0520bSJosef Bacik 	spin_lock(&fs_info->unused_bgs_lock);
1489e3e0520bSJosef Bacik 	while (!list_empty(&fs_info->unused_bgs)) {
1490f4a9f219SFilipe Manana 		u64 used;
1491e3e0520bSJosef Bacik 		int trimming;
1492e3e0520bSJosef Bacik 
1493e3e0520bSJosef Bacik 		block_group = list_first_entry(&fs_info->unused_bgs,
149432da5386SDavid Sterba 					       struct btrfs_block_group,
1495e3e0520bSJosef Bacik 					       bg_list);
1496e3e0520bSJosef Bacik 		list_del_init(&block_group->bg_list);
1497e3e0520bSJosef Bacik 
1498e3e0520bSJosef Bacik 		space_info = block_group->space_info;
1499e3e0520bSJosef Bacik 
1500e3e0520bSJosef Bacik 		if (ret || btrfs_mixed_space_info(space_info)) {
1501e3e0520bSJosef Bacik 			btrfs_put_block_group(block_group);
1502e3e0520bSJosef Bacik 			continue;
1503e3e0520bSJosef Bacik 		}
1504e3e0520bSJosef Bacik 		spin_unlock(&fs_info->unused_bgs_lock);
1505e3e0520bSJosef Bacik 
1506b0643e59SDennis Zhou 		btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
1507b0643e59SDennis Zhou 
1508e3e0520bSJosef Bacik 		/* Don't want to race with allocators so take the groups_sem */
1509e3e0520bSJosef Bacik 		down_write(&space_info->groups_sem);
15106e80d4f8SDennis Zhou 
15116e80d4f8SDennis Zhou 		/*
15126e80d4f8SDennis Zhou 		 * Async discard moves the final block group discard to be prior
15136e80d4f8SDennis Zhou 		 * to the unused_bgs code path.  Therefore, if it's not fully
15146e80d4f8SDennis Zhou 		 * trimmed, punt it back to the async discard lists.
15156e80d4f8SDennis Zhou 		 */
15166e80d4f8SDennis Zhou 		if (btrfs_test_opt(fs_info, DISCARD_ASYNC) &&
15176e80d4f8SDennis Zhou 		    !btrfs_is_free_space_trimmed(block_group)) {
15186e80d4f8SDennis Zhou 			trace_btrfs_skip_unused_block_group(block_group);
15196e80d4f8SDennis Zhou 			up_write(&space_info->groups_sem);
15206e80d4f8SDennis Zhou 			/* Requeue if we failed because of async discard */
15216e80d4f8SDennis Zhou 			btrfs_discard_queue_work(&fs_info->discard_ctl,
15226e80d4f8SDennis Zhou 						 block_group);
15236e80d4f8SDennis Zhou 			goto next;
15246e80d4f8SDennis Zhou 		}
15256e80d4f8SDennis Zhou 
1526f4a9f219SFilipe Manana 		spin_lock(&space_info->lock);
1527e3e0520bSJosef Bacik 		spin_lock(&block_group->lock);
15281693d544SFilipe Manana 		if (btrfs_is_block_group_used(block_group) || block_group->ro ||
1529e3e0520bSJosef Bacik 		    list_is_singular(&block_group->list)) {
1530e3e0520bSJosef Bacik 			/*
1531e3e0520bSJosef Bacik 			 * We want to bail if we made new allocations or have
1532e3e0520bSJosef Bacik 			 * outstanding allocations in this block group.  We do
1533e3e0520bSJosef Bacik 			 * the ro check in case balance is currently acting on
1534e3e0520bSJosef Bacik 			 * this block group.
1535edebd19aSFilipe Manana 			 *
1536edebd19aSFilipe Manana 			 * Also bail out if this is the only block group for its
1537edebd19aSFilipe Manana 			 * type, because otherwise we would lose profile
1538edebd19aSFilipe Manana 			 * information from fs_info->avail_*_alloc_bits and the
1539edebd19aSFilipe Manana 			 * next block group of this type would be created with a
1540edebd19aSFilipe Manana 			 * "single" profile (even if we're in a raid fs) because
1541edebd19aSFilipe Manana 			 * fs_info->avail_*_alloc_bits would be 0.
1542e3e0520bSJosef Bacik 			 */
1543e3e0520bSJosef Bacik 			trace_btrfs_skip_unused_block_group(block_group);
1544e3e0520bSJosef Bacik 			spin_unlock(&block_group->lock);
1545f4a9f219SFilipe Manana 			spin_unlock(&space_info->lock);
1546e3e0520bSJosef Bacik 			up_write(&space_info->groups_sem);
1547e3e0520bSJosef Bacik 			goto next;
1548e3e0520bSJosef Bacik 		}
1549f4a9f219SFilipe Manana 
1550f4a9f219SFilipe Manana 		/*
1551f4a9f219SFilipe Manana 		 * The block group may be unused but there may be space reserved
1552f4a9f219SFilipe Manana 		 * accounting with the existence of that block group, that is,
1553f4a9f219SFilipe Manana 		 * space_info->bytes_may_use was incremented by a task but no
1554f4a9f219SFilipe Manana 		 * space was yet allocated from the block group by the task.
1555f4a9f219SFilipe Manana 		 * That space may or may not be allocated, as we are generally
1556f4a9f219SFilipe Manana 		 * pessimistic about space reservation for metadata as well as
1557f4a9f219SFilipe Manana 		 * for data when using compression (as we reserve space based on
1558f4a9f219SFilipe Manana 		 * the worst case, when data can't be compressed, and before
1559f4a9f219SFilipe Manana 		 * actually attempting compression, before starting writeback).
1560f4a9f219SFilipe Manana 		 *
1561f4a9f219SFilipe Manana 		 * So check if the total space of the space_info minus the size
1562f4a9f219SFilipe Manana 		 * of this block group is less than the used space of the
1563f4a9f219SFilipe Manana 		 * space_info - if that's the case, then it means we have tasks
1564f4a9f219SFilipe Manana 		 * that might be relying on the block group in order to allocate
1565f4a9f219SFilipe Manana 		 * extents, and add back the block group to the unused list when
1566f4a9f219SFilipe Manana 		 * we finish, so that we retry later in case no tasks ended up
1567f4a9f219SFilipe Manana 		 * needing to allocate extents from the block group.
1568f4a9f219SFilipe Manana 		 */
1569f4a9f219SFilipe Manana 		used = btrfs_space_info_used(space_info, true);
1570a8b70c7fSJohannes Thumshirn 		if (space_info->total_bytes - block_group->length < used &&
1571a8b70c7fSJohannes Thumshirn 		    block_group->zone_unusable < block_group->length) {
1572f4a9f219SFilipe Manana 			/*
1573f4a9f219SFilipe Manana 			 * Add a reference for the list, compensate for the ref
1574f4a9f219SFilipe Manana 			 * drop under the "next" label for the
1575f4a9f219SFilipe Manana 			 * fs_info->unused_bgs list.
1576f4a9f219SFilipe Manana 			 */
1577f4a9f219SFilipe Manana 			btrfs_get_block_group(block_group);
1578f4a9f219SFilipe Manana 			list_add_tail(&block_group->bg_list, &retry_list);
1579f4a9f219SFilipe Manana 
1580f4a9f219SFilipe Manana 			trace_btrfs_skip_unused_block_group(block_group);
1581e3e0520bSJosef Bacik 			spin_unlock(&block_group->lock);
1582f4a9f219SFilipe Manana 			spin_unlock(&space_info->lock);
1583f4a9f219SFilipe Manana 			up_write(&space_info->groups_sem);
1584f4a9f219SFilipe Manana 			goto next;
1585f4a9f219SFilipe Manana 		}
1586f4a9f219SFilipe Manana 
1587f4a9f219SFilipe Manana 		spin_unlock(&block_group->lock);
1588f4a9f219SFilipe Manana 		spin_unlock(&space_info->lock);
1589e3e0520bSJosef Bacik 
1590e3e0520bSJosef Bacik 		/* We don't want to force the issue, only flip if it's ok. */
1591e11c0406SJosef Bacik 		ret = inc_block_group_ro(block_group, 0);
1592e3e0520bSJosef Bacik 		up_write(&space_info->groups_sem);
1593e3e0520bSJosef Bacik 		if (ret < 0) {
1594e3e0520bSJosef Bacik 			ret = 0;
1595e3e0520bSJosef Bacik 			goto next;
1596e3e0520bSJosef Bacik 		}
1597e3e0520bSJosef Bacik 
159874e91b12SNaohiro Aota 		ret = btrfs_zone_finish(block_group);
159974e91b12SNaohiro Aota 		if (ret < 0) {
160074e91b12SNaohiro Aota 			btrfs_dec_block_group_ro(block_group);
160174e91b12SNaohiro Aota 			if (ret == -EAGAIN)
160274e91b12SNaohiro Aota 				ret = 0;
160374e91b12SNaohiro Aota 			goto next;
160474e91b12SNaohiro Aota 		}
160574e91b12SNaohiro Aota 
1606e3e0520bSJosef Bacik 		/*
1607e3e0520bSJosef Bacik 		 * Want to do this before we do anything else so we can recover
1608e3e0520bSJosef Bacik 		 * properly if we fail to join the transaction.
1609e3e0520bSJosef Bacik 		 */
1610e3e0520bSJosef Bacik 		trans = btrfs_start_trans_remove_block_group(fs_info,
1611b3470b5dSDavid Sterba 						     block_group->start);
1612e3e0520bSJosef Bacik 		if (IS_ERR(trans)) {
1613e3e0520bSJosef Bacik 			btrfs_dec_block_group_ro(block_group);
1614e3e0520bSJosef Bacik 			ret = PTR_ERR(trans);
1615e3e0520bSJosef Bacik 			goto next;
1616e3e0520bSJosef Bacik 		}
1617e3e0520bSJosef Bacik 
1618e3e0520bSJosef Bacik 		/*
1619e3e0520bSJosef Bacik 		 * We could have pending pinned extents for this block group,
1620e3e0520bSJosef Bacik 		 * just delete them, we don't care about them anymore.
1621e3e0520bSJosef Bacik 		 */
1622534cf531SFilipe Manana 		if (!clean_pinned_extents(trans, block_group)) {
1623534cf531SFilipe Manana 			btrfs_dec_block_group_ro(block_group);
1624e3e0520bSJosef Bacik 			goto end_trans;
1625534cf531SFilipe Manana 		}
1626e3e0520bSJosef Bacik 
1627b0643e59SDennis Zhou 		/*
1628b0643e59SDennis Zhou 		 * At this point, the block_group is read only and should fail
1629b0643e59SDennis Zhou 		 * new allocations.  However, btrfs_finish_extent_commit() can
1630b0643e59SDennis Zhou 		 * cause this block_group to be placed back on the discard
1631b0643e59SDennis Zhou 		 * lists because now the block_group isn't fully discarded.
1632b0643e59SDennis Zhou 		 * Bail here and try again later after discarding everything.
1633b0643e59SDennis Zhou 		 */
1634b0643e59SDennis Zhou 		spin_lock(&fs_info->discard_ctl.lock);
1635b0643e59SDennis Zhou 		if (!list_empty(&block_group->discard_list)) {
1636b0643e59SDennis Zhou 			spin_unlock(&fs_info->discard_ctl.lock);
1637b0643e59SDennis Zhou 			btrfs_dec_block_group_ro(block_group);
1638b0643e59SDennis Zhou 			btrfs_discard_queue_work(&fs_info->discard_ctl,
1639b0643e59SDennis Zhou 						 block_group);
1640b0643e59SDennis Zhou 			goto end_trans;
1641b0643e59SDennis Zhou 		}
1642b0643e59SDennis Zhou 		spin_unlock(&fs_info->discard_ctl.lock);
1643b0643e59SDennis Zhou 
1644e3e0520bSJosef Bacik 		/* Reset pinned so btrfs_put_block_group doesn't complain */
1645e3e0520bSJosef Bacik 		spin_lock(&space_info->lock);
1646e3e0520bSJosef Bacik 		spin_lock(&block_group->lock);
1647e3e0520bSJosef Bacik 
1648e3e0520bSJosef Bacik 		btrfs_space_info_update_bytes_pinned(fs_info, space_info,
1649e3e0520bSJosef Bacik 						     -block_group->pinned);
1650e3e0520bSJosef Bacik 		space_info->bytes_readonly += block_group->pinned;
1651e3e0520bSJosef Bacik 		block_group->pinned = 0;
1652e3e0520bSJosef Bacik 
1653e3e0520bSJosef Bacik 		spin_unlock(&block_group->lock);
1654e3e0520bSJosef Bacik 		spin_unlock(&space_info->lock);
1655e3e0520bSJosef Bacik 
16566e80d4f8SDennis Zhou 		/*
16576e80d4f8SDennis Zhou 		 * The normal path here is an unused block group is passed here,
16586e80d4f8SDennis Zhou 		 * then trimming is handled in the transaction commit path.
16596e80d4f8SDennis Zhou 		 * Async discard interposes before this to do the trimming
16606e80d4f8SDennis Zhou 		 * before coming down the unused block group path as trimming
16616e80d4f8SDennis Zhou 		 * will no longer be done later in the transaction commit path.
16626e80d4f8SDennis Zhou 		 */
16636e80d4f8SDennis Zhou 		if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC))
16646e80d4f8SDennis Zhou 			goto flip_async;
16656e80d4f8SDennis Zhou 
1666dcba6e48SNaohiro Aota 		/*
1667dcba6e48SNaohiro Aota 		 * DISCARD can flip during remount. On zoned filesystems, we
1668dcba6e48SNaohiro Aota 		 * need to reset sequential-required zones.
1669dcba6e48SNaohiro Aota 		 */
1670dcba6e48SNaohiro Aota 		trimming = btrfs_test_opt(fs_info, DISCARD_SYNC) ||
1671dcba6e48SNaohiro Aota 				btrfs_is_zoned(fs_info);
1672e3e0520bSJosef Bacik 
1673e3e0520bSJosef Bacik 		/* Implicit trim during transaction commit. */
1674e3e0520bSJosef Bacik 		if (trimming)
16756b7304afSFilipe Manana 			btrfs_freeze_block_group(block_group);
1676e3e0520bSJosef Bacik 
1677e3e0520bSJosef Bacik 		/*
1678e3e0520bSJosef Bacik 		 * Btrfs_remove_chunk will abort the transaction if things go
1679e3e0520bSJosef Bacik 		 * horribly wrong.
1680e3e0520bSJosef Bacik 		 */
1681b3470b5dSDavid Sterba 		ret = btrfs_remove_chunk(trans, block_group->start);
1682e3e0520bSJosef Bacik 
1683e3e0520bSJosef Bacik 		if (ret) {
1684e3e0520bSJosef Bacik 			if (trimming)
16856b7304afSFilipe Manana 				btrfs_unfreeze_block_group(block_group);
1686e3e0520bSJosef Bacik 			goto end_trans;
1687e3e0520bSJosef Bacik 		}
1688e3e0520bSJosef Bacik 
1689e3e0520bSJosef Bacik 		/*
1690e3e0520bSJosef Bacik 		 * If we're not mounted with -odiscard, we can just forget
1691e3e0520bSJosef Bacik 		 * about this block group. Otherwise we'll need to wait
1692e3e0520bSJosef Bacik 		 * until transaction commit to do the actual discard.
1693e3e0520bSJosef Bacik 		 */
1694e3e0520bSJosef Bacik 		if (trimming) {
1695e3e0520bSJosef Bacik 			spin_lock(&fs_info->unused_bgs_lock);
1696e3e0520bSJosef Bacik 			/*
1697e3e0520bSJosef Bacik 			 * A concurrent scrub might have added us to the list
1698e3e0520bSJosef Bacik 			 * fs_info->unused_bgs, so use a list_move operation
1699e3e0520bSJosef Bacik 			 * to add the block group to the deleted_bgs list.
1700e3e0520bSJosef Bacik 			 */
1701e3e0520bSJosef Bacik 			list_move(&block_group->bg_list,
1702e3e0520bSJosef Bacik 				  &trans->transaction->deleted_bgs);
1703e3e0520bSJosef Bacik 			spin_unlock(&fs_info->unused_bgs_lock);
1704e3e0520bSJosef Bacik 			btrfs_get_block_group(block_group);
1705e3e0520bSJosef Bacik 		}
1706e3e0520bSJosef Bacik end_trans:
1707e3e0520bSJosef Bacik 		btrfs_end_transaction(trans);
1708e3e0520bSJosef Bacik next:
1709e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
1710e3e0520bSJosef Bacik 		spin_lock(&fs_info->unused_bgs_lock);
1711e3e0520bSJosef Bacik 	}
1712f4a9f219SFilipe Manana 	list_splice_tail(&retry_list, &fs_info->unused_bgs);
1713e3e0520bSJosef Bacik 	spin_unlock(&fs_info->unused_bgs_lock);
1714f3372065SJohannes Thumshirn 	mutex_unlock(&fs_info->reclaim_bgs_lock);
17156e80d4f8SDennis Zhou 	return;
17166e80d4f8SDennis Zhou 
17176e80d4f8SDennis Zhou flip_async:
17186e80d4f8SDennis Zhou 	btrfs_end_transaction(trans);
1719f4a9f219SFilipe Manana 	spin_lock(&fs_info->unused_bgs_lock);
1720f4a9f219SFilipe Manana 	list_splice_tail(&retry_list, &fs_info->unused_bgs);
1721f4a9f219SFilipe Manana 	spin_unlock(&fs_info->unused_bgs_lock);
1722f3372065SJohannes Thumshirn 	mutex_unlock(&fs_info->reclaim_bgs_lock);
17236e80d4f8SDennis Zhou 	btrfs_put_block_group(block_group);
17246e80d4f8SDennis Zhou 	btrfs_discard_punt_unused_bgs_list(fs_info);
1725e3e0520bSJosef Bacik }
1726e3e0520bSJosef Bacik 
btrfs_mark_bg_unused(struct btrfs_block_group * bg)172732da5386SDavid Sterba void btrfs_mark_bg_unused(struct btrfs_block_group *bg)
1728e3e0520bSJosef Bacik {
1729e3e0520bSJosef Bacik 	struct btrfs_fs_info *fs_info = bg->fs_info;
1730e3e0520bSJosef Bacik 
1731e3e0520bSJosef Bacik 	spin_lock(&fs_info->unused_bgs_lock);
1732e3e0520bSJosef Bacik 	if (list_empty(&bg->bg_list)) {
1733e3e0520bSJosef Bacik 		btrfs_get_block_group(bg);
17340657b20cSFilipe Manana 		trace_btrfs_add_unused_block_group(bg);
1735e3e0520bSJosef Bacik 		list_add_tail(&bg->bg_list, &fs_info->unused_bgs);
17360657b20cSFilipe Manana 	} else if (!test_bit(BLOCK_GROUP_FLAG_NEW, &bg->runtime_flags)) {
1737a9f18971SNaohiro Aota 		/* Pull out the block group from the reclaim_bgs list. */
17380657b20cSFilipe Manana 		trace_btrfs_add_unused_block_group(bg);
1739a9f18971SNaohiro Aota 		list_move_tail(&bg->bg_list, &fs_info->unused_bgs);
1740e3e0520bSJosef Bacik 	}
1741e3e0520bSJosef Bacik 	spin_unlock(&fs_info->unused_bgs_lock);
1742e3e0520bSJosef Bacik }
17434358d963SJosef Bacik 
17442ca0ec77SJohannes Thumshirn /*
17452ca0ec77SJohannes Thumshirn  * We want block groups with a low number of used bytes to be in the beginning
17462ca0ec77SJohannes Thumshirn  * of the list, so they will get reclaimed first.
17472ca0ec77SJohannes Thumshirn  */
reclaim_bgs_cmp(void * unused,const struct list_head * a,const struct list_head * b)17482ca0ec77SJohannes Thumshirn static int reclaim_bgs_cmp(void *unused, const struct list_head *a,
17492ca0ec77SJohannes Thumshirn 			   const struct list_head *b)
17502ca0ec77SJohannes Thumshirn {
17512ca0ec77SJohannes Thumshirn 	const struct btrfs_block_group *bg1, *bg2;
17522ca0ec77SJohannes Thumshirn 
17532ca0ec77SJohannes Thumshirn 	bg1 = list_entry(a, struct btrfs_block_group, bg_list);
17542ca0ec77SJohannes Thumshirn 	bg2 = list_entry(b, struct btrfs_block_group, bg_list);
17552ca0ec77SJohannes Thumshirn 
17562ca0ec77SJohannes Thumshirn 	return bg1->used > bg2->used;
17572ca0ec77SJohannes Thumshirn }
17582ca0ec77SJohannes Thumshirn 
btrfs_should_reclaim(const struct btrfs_fs_info * fs_info)1759*ca283ea9SDavid Sterba static inline bool btrfs_should_reclaim(const struct btrfs_fs_info *fs_info)
17603687fcb0SJohannes Thumshirn {
17613687fcb0SJohannes Thumshirn 	if (btrfs_is_zoned(fs_info))
17623687fcb0SJohannes Thumshirn 		return btrfs_zoned_should_reclaim(fs_info);
17633687fcb0SJohannes Thumshirn 	return true;
17643687fcb0SJohannes Thumshirn }
17653687fcb0SJohannes Thumshirn 
should_reclaim_block_group(const struct btrfs_block_group * bg,u64 bytes_freed)1766*ca283ea9SDavid Sterba static bool should_reclaim_block_group(const struct btrfs_block_group *bg, u64 bytes_freed)
176781531225SBoris Burkov {
1768f5ff64ccSBoris Burkov 	const int thresh_pct = btrfs_calc_reclaim_threshold(bg->space_info);
1769f5ff64ccSBoris Burkov 	u64 thresh_bytes = mult_perc(bg->length, thresh_pct);
177081531225SBoris Burkov 	const u64 new_val = bg->used;
177181531225SBoris Burkov 	const u64 old_val = new_val + bytes_freed;
177281531225SBoris Burkov 
1773f5ff64ccSBoris Burkov 	if (thresh_bytes == 0)
177481531225SBoris Burkov 		return false;
177581531225SBoris Burkov 
177681531225SBoris Burkov 	/*
177781531225SBoris Burkov 	 * If we were below the threshold before don't reclaim, we are likely a
177881531225SBoris Burkov 	 * brand new block group and we don't want to relocate new block groups.
177981531225SBoris Burkov 	 */
1780f5ff64ccSBoris Burkov 	if (old_val < thresh_bytes)
178181531225SBoris Burkov 		return false;
1782f5ff64ccSBoris Burkov 	if (new_val >= thresh_bytes)
178381531225SBoris Burkov 		return false;
178481531225SBoris Burkov 	return true;
178581531225SBoris Burkov }
178681531225SBoris Burkov 
btrfs_reclaim_bgs_work(struct work_struct * work)178718bb8bbfSJohannes Thumshirn void btrfs_reclaim_bgs_work(struct work_struct *work)
178818bb8bbfSJohannes Thumshirn {
178918bb8bbfSJohannes Thumshirn 	struct btrfs_fs_info *fs_info =
179018bb8bbfSJohannes Thumshirn 		container_of(work, struct btrfs_fs_info, reclaim_bgs_work);
179118bb8bbfSJohannes Thumshirn 	struct btrfs_block_group *bg;
179218bb8bbfSJohannes Thumshirn 	struct btrfs_space_info *space_info;
17934eb4e85cSBoris Burkov 	LIST_HEAD(retry_list);
179418bb8bbfSJohannes Thumshirn 
179518bb8bbfSJohannes Thumshirn 	if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
179618bb8bbfSJohannes Thumshirn 		return;
179718bb8bbfSJohannes Thumshirn 
17982f12741fSJosef Bacik 	if (btrfs_fs_closing(fs_info))
17992f12741fSJosef Bacik 		return;
18002f12741fSJosef Bacik 
18013687fcb0SJohannes Thumshirn 	if (!btrfs_should_reclaim(fs_info))
18023687fcb0SJohannes Thumshirn 		return;
18033687fcb0SJohannes Thumshirn 
1804ca5e4ea0SNaohiro Aota 	sb_start_write(fs_info->sb);
1805ca5e4ea0SNaohiro Aota 
1806ca5e4ea0SNaohiro Aota 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
1807ca5e4ea0SNaohiro Aota 		sb_end_write(fs_info->sb);
180818bb8bbfSJohannes Thumshirn 		return;
1809ca5e4ea0SNaohiro Aota 	}
181018bb8bbfSJohannes Thumshirn 
18119cc0b837SJohannes Thumshirn 	/*
18129cc0b837SJohannes Thumshirn 	 * Long running balances can keep us blocked here for eternity, so
18139cc0b837SJohannes Thumshirn 	 * simply skip reclaim if we're unable to get the mutex.
18149cc0b837SJohannes Thumshirn 	 */
18159cc0b837SJohannes Thumshirn 	if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) {
18169cc0b837SJohannes Thumshirn 		btrfs_exclop_finish(fs_info);
1817ca5e4ea0SNaohiro Aota 		sb_end_write(fs_info->sb);
18189cc0b837SJohannes Thumshirn 		return;
18199cc0b837SJohannes Thumshirn 	}
18209cc0b837SJohannes Thumshirn 
182118bb8bbfSJohannes Thumshirn 	spin_lock(&fs_info->unused_bgs_lock);
18222ca0ec77SJohannes Thumshirn 	/*
18232ca0ec77SJohannes Thumshirn 	 * Sort happens under lock because we can't simply splice it and sort.
18242ca0ec77SJohannes Thumshirn 	 * The block groups might still be in use and reachable via bg_list,
18252ca0ec77SJohannes Thumshirn 	 * and their presence in the reclaim_bgs list must be preserved.
18262ca0ec77SJohannes Thumshirn 	 */
18272ca0ec77SJohannes Thumshirn 	list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp);
182818bb8bbfSJohannes Thumshirn 	while (!list_empty(&fs_info->reclaim_bgs)) {
18295f93e776SJohannes Thumshirn 		u64 zone_unusable;
1830243192b6SBoris Burkov 		u64 reclaimed;
18311cea5cf0SFilipe Manana 		int ret = 0;
18321cea5cf0SFilipe Manana 
183318bb8bbfSJohannes Thumshirn 		bg = list_first_entry(&fs_info->reclaim_bgs,
183418bb8bbfSJohannes Thumshirn 				      struct btrfs_block_group,
183518bb8bbfSJohannes Thumshirn 				      bg_list);
183618bb8bbfSJohannes Thumshirn 		list_del_init(&bg->bg_list);
183718bb8bbfSJohannes Thumshirn 
183818bb8bbfSJohannes Thumshirn 		space_info = bg->space_info;
183918bb8bbfSJohannes Thumshirn 		spin_unlock(&fs_info->unused_bgs_lock);
184018bb8bbfSJohannes Thumshirn 
184118bb8bbfSJohannes Thumshirn 		/* Don't race with allocators so take the groups_sem */
184218bb8bbfSJohannes Thumshirn 		down_write(&space_info->groups_sem);
184318bb8bbfSJohannes Thumshirn 
1844f5ff64ccSBoris Burkov 		spin_lock(&space_info->lock);
184518bb8bbfSJohannes Thumshirn 		spin_lock(&bg->lock);
184618bb8bbfSJohannes Thumshirn 		if (bg->reserved || bg->pinned || bg->ro) {
184718bb8bbfSJohannes Thumshirn 			/*
184818bb8bbfSJohannes Thumshirn 			 * We want to bail if we made new allocations or have
184918bb8bbfSJohannes Thumshirn 			 * outstanding allocations in this block group.  We do
185018bb8bbfSJohannes Thumshirn 			 * the ro check in case balance is currently acting on
185118bb8bbfSJohannes Thumshirn 			 * this block group.
185218bb8bbfSJohannes Thumshirn 			 */
185318bb8bbfSJohannes Thumshirn 			spin_unlock(&bg->lock);
1854f5ff64ccSBoris Burkov 			spin_unlock(&space_info->lock);
185518bb8bbfSJohannes Thumshirn 			up_write(&space_info->groups_sem);
185618bb8bbfSJohannes Thumshirn 			goto next;
185718bb8bbfSJohannes Thumshirn 		}
1858cc4804bfSBoris Burkov 		if (bg->used == 0) {
1859cc4804bfSBoris Burkov 			/*
1860cc4804bfSBoris Burkov 			 * It is possible that we trigger relocation on a block
1861cc4804bfSBoris Burkov 			 * group as its extents are deleted and it first goes
1862cc4804bfSBoris Burkov 			 * below the threshold, then shortly after goes empty.
1863cc4804bfSBoris Burkov 			 *
1864cc4804bfSBoris Burkov 			 * In this case, relocating it does delete it, but has
1865cc4804bfSBoris Burkov 			 * some overhead in relocation specific metadata, looking
1866cc4804bfSBoris Burkov 			 * for the non-existent extents and running some extra
1867cc4804bfSBoris Burkov 			 * transactions, which we can avoid by using one of the
1868cc4804bfSBoris Burkov 			 * other mechanisms for dealing with empty block groups.
1869cc4804bfSBoris Burkov 			 */
1870cc4804bfSBoris Burkov 			if (!btrfs_test_opt(fs_info, DISCARD_ASYNC))
1871cc4804bfSBoris Burkov 				btrfs_mark_bg_unused(bg);
1872cc4804bfSBoris Burkov 			spin_unlock(&bg->lock);
1873f5ff64ccSBoris Burkov 			spin_unlock(&space_info->lock);
1874cc4804bfSBoris Burkov 			up_write(&space_info->groups_sem);
1875cc4804bfSBoris Burkov 			goto next;
187681531225SBoris Burkov 
187781531225SBoris Burkov 		}
187881531225SBoris Burkov 		/*
187981531225SBoris Burkov 		 * The block group might no longer meet the reclaim condition by
188081531225SBoris Burkov 		 * the time we get around to reclaiming it, so to avoid
188181531225SBoris Burkov 		 * reclaiming overly full block_groups, skip reclaiming them.
188281531225SBoris Burkov 		 *
188381531225SBoris Burkov 		 * Since the decision making process also depends on the amount
188481531225SBoris Burkov 		 * being freed, pass in a fake giant value to skip that extra
188581531225SBoris Burkov 		 * check, which is more meaningful when adding to the list in
188681531225SBoris Burkov 		 * the first place.
188781531225SBoris Burkov 		 */
188881531225SBoris Burkov 		if (!should_reclaim_block_group(bg, bg->length)) {
188981531225SBoris Burkov 			spin_unlock(&bg->lock);
1890f5ff64ccSBoris Burkov 			spin_unlock(&space_info->lock);
189181531225SBoris Burkov 			up_write(&space_info->groups_sem);
189281531225SBoris Burkov 			goto next;
1893cc4804bfSBoris Burkov 		}
189418bb8bbfSJohannes Thumshirn 		spin_unlock(&bg->lock);
1895f5ff64ccSBoris Burkov 		spin_unlock(&space_info->lock);
189618bb8bbfSJohannes Thumshirn 
189793463ff7SNaohiro Aota 		/*
189893463ff7SNaohiro Aota 		 * Get out fast, in case we're read-only or unmounting the
189993463ff7SNaohiro Aota 		 * filesystem. It is OK to drop block groups from the list even
190093463ff7SNaohiro Aota 		 * for the read-only case. As we did sb_start_write(),
190193463ff7SNaohiro Aota 		 * "mount -o remount,ro" won't happen and read-only filesystem
190293463ff7SNaohiro Aota 		 * means it is forced read-only due to a fatal error. So, it
190393463ff7SNaohiro Aota 		 * never gets back to read-write to let us reclaim again.
190493463ff7SNaohiro Aota 		 */
190593463ff7SNaohiro Aota 		if (btrfs_need_cleaner_sleep(fs_info)) {
190618bb8bbfSJohannes Thumshirn 			up_write(&space_info->groups_sem);
190718bb8bbfSJohannes Thumshirn 			goto next;
190818bb8bbfSJohannes Thumshirn 		}
190918bb8bbfSJohannes Thumshirn 
19105f93e776SJohannes Thumshirn 		/*
19115f93e776SJohannes Thumshirn 		 * Cache the zone_unusable value before turning the block group
19125f93e776SJohannes Thumshirn 		 * to read only. As soon as the blog group is read only it's
19135f93e776SJohannes Thumshirn 		 * zone_unusable value gets moved to the block group's read-only
19145f93e776SJohannes Thumshirn 		 * bytes and isn't available for calculations anymore.
19155f93e776SJohannes Thumshirn 		 */
19165f93e776SJohannes Thumshirn 		zone_unusable = bg->zone_unusable;
191718bb8bbfSJohannes Thumshirn 		ret = inc_block_group_ro(bg, 0);
191818bb8bbfSJohannes Thumshirn 		up_write(&space_info->groups_sem);
191918bb8bbfSJohannes Thumshirn 		if (ret < 0)
192018bb8bbfSJohannes Thumshirn 			goto next;
192118bb8bbfSJohannes Thumshirn 
19225f93e776SJohannes Thumshirn 		btrfs_info(fs_info,
19235f93e776SJohannes Thumshirn 			"reclaiming chunk %llu with %llu%% used %llu%% unusable",
192495cd356cSJohannes Thumshirn 				bg->start,
192595cd356cSJohannes Thumshirn 				div64_u64(bg->used * 100, bg->length),
19265f93e776SJohannes Thumshirn 				div64_u64(zone_unusable * 100, bg->length));
192718bb8bbfSJohannes Thumshirn 		trace_btrfs_reclaim_block_group(bg);
1928243192b6SBoris Burkov 		reclaimed = bg->used;
192918bb8bbfSJohannes Thumshirn 		ret = btrfs_relocate_chunk(fs_info, bg->start);
193074944c87SJosef Bacik 		if (ret) {
193174944c87SJosef Bacik 			btrfs_dec_block_group_ro(bg);
193218bb8bbfSJohannes Thumshirn 			btrfs_err(fs_info, "error relocating chunk %llu",
193318bb8bbfSJohannes Thumshirn 				  bg->start);
1934243192b6SBoris Burkov 			reclaimed = 0;
1935243192b6SBoris Burkov 			spin_lock(&space_info->lock);
1936243192b6SBoris Burkov 			space_info->reclaim_errors++;
1937813d4c64SBoris Burkov 			if (READ_ONCE(space_info->periodic_reclaim))
1938813d4c64SBoris Burkov 				space_info->periodic_reclaim_ready = false;
1939243192b6SBoris Burkov 			spin_unlock(&space_info->lock);
194074944c87SJosef Bacik 		}
1941243192b6SBoris Burkov 		spin_lock(&space_info->lock);
1942243192b6SBoris Burkov 		space_info->reclaim_count++;
1943243192b6SBoris Burkov 		space_info->reclaim_bytes += reclaimed;
1944243192b6SBoris Burkov 		spin_unlock(&space_info->lock);
194518bb8bbfSJohannes Thumshirn 
194618bb8bbfSJohannes Thumshirn next:
1947813d4c64SBoris Burkov 		if (ret && !READ_ONCE(space_info->periodic_reclaim)) {
19484eb4e85cSBoris Burkov 			/* Refcount held by the reclaim_bgs list after splice. */
194948f091fdSNaohiro Aota 			spin_lock(&fs_info->unused_bgs_lock);
195048f091fdSNaohiro Aota 			/*
195148f091fdSNaohiro Aota 			 * This block group might be added to the unused list
195248f091fdSNaohiro Aota 			 * during the above process. Move it back to the
195348f091fdSNaohiro Aota 			 * reclaim list otherwise.
195448f091fdSNaohiro Aota 			 */
195548f091fdSNaohiro Aota 			if (list_empty(&bg->bg_list)) {
19564eb4e85cSBoris Burkov 				btrfs_get_block_group(bg);
19574eb4e85cSBoris Burkov 				list_add_tail(&bg->bg_list, &retry_list);
19584eb4e85cSBoris Burkov 			}
195948f091fdSNaohiro Aota 			spin_unlock(&fs_info->unused_bgs_lock);
196048f091fdSNaohiro Aota 		}
19611cea5cf0SFilipe Manana 		btrfs_put_block_group(bg);
19623ed01616SNaohiro Aota 
19633ed01616SNaohiro Aota 		mutex_unlock(&fs_info->reclaim_bgs_lock);
19643ed01616SNaohiro Aota 		/*
19653ed01616SNaohiro Aota 		 * Reclaiming all the block groups in the list can take really
19663ed01616SNaohiro Aota 		 * long.  Prioritize cleaning up unused block groups.
19673ed01616SNaohiro Aota 		 */
19683ed01616SNaohiro Aota 		btrfs_delete_unused_bgs(fs_info);
19693ed01616SNaohiro Aota 		/*
19703ed01616SNaohiro Aota 		 * If we are interrupted by a balance, we can just bail out. The
19713ed01616SNaohiro Aota 		 * cleaner thread restart again if necessary.
19723ed01616SNaohiro Aota 		 */
19733ed01616SNaohiro Aota 		if (!mutex_trylock(&fs_info->reclaim_bgs_lock))
19743ed01616SNaohiro Aota 			goto end;
1975d96b3424SFilipe Manana 		spin_lock(&fs_info->unused_bgs_lock);
197618bb8bbfSJohannes Thumshirn 	}
197718bb8bbfSJohannes Thumshirn 	spin_unlock(&fs_info->unused_bgs_lock);
197818bb8bbfSJohannes Thumshirn 	mutex_unlock(&fs_info->reclaim_bgs_lock);
19793ed01616SNaohiro Aota end:
19804eb4e85cSBoris Burkov 	spin_lock(&fs_info->unused_bgs_lock);
19814eb4e85cSBoris Burkov 	list_splice_tail(&retry_list, &fs_info->reclaim_bgs);
19824eb4e85cSBoris Burkov 	spin_unlock(&fs_info->unused_bgs_lock);
198318bb8bbfSJohannes Thumshirn 	btrfs_exclop_finish(fs_info);
1984ca5e4ea0SNaohiro Aota 	sb_end_write(fs_info->sb);
198518bb8bbfSJohannes Thumshirn }
198618bb8bbfSJohannes Thumshirn 
btrfs_reclaim_bgs(struct btrfs_fs_info * fs_info)198718bb8bbfSJohannes Thumshirn void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info)
198818bb8bbfSJohannes Thumshirn {
1989e4ca3932SBoris Burkov 	btrfs_reclaim_sweep(fs_info);
199018bb8bbfSJohannes Thumshirn 	spin_lock(&fs_info->unused_bgs_lock);
199118bb8bbfSJohannes Thumshirn 	if (!list_empty(&fs_info->reclaim_bgs))
199218bb8bbfSJohannes Thumshirn 		queue_work(system_unbound_wq, &fs_info->reclaim_bgs_work);
199318bb8bbfSJohannes Thumshirn 	spin_unlock(&fs_info->unused_bgs_lock);
199418bb8bbfSJohannes Thumshirn }
199518bb8bbfSJohannes Thumshirn 
btrfs_mark_bg_to_reclaim(struct btrfs_block_group * bg)199618bb8bbfSJohannes Thumshirn void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg)
199718bb8bbfSJohannes Thumshirn {
199818bb8bbfSJohannes Thumshirn 	struct btrfs_fs_info *fs_info = bg->fs_info;
199918bb8bbfSJohannes Thumshirn 
200018bb8bbfSJohannes Thumshirn 	spin_lock(&fs_info->unused_bgs_lock);
200118bb8bbfSJohannes Thumshirn 	if (list_empty(&bg->bg_list)) {
200218bb8bbfSJohannes Thumshirn 		btrfs_get_block_group(bg);
200318bb8bbfSJohannes Thumshirn 		trace_btrfs_add_reclaim_block_group(bg);
200418bb8bbfSJohannes Thumshirn 		list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
200518bb8bbfSJohannes Thumshirn 	}
200618bb8bbfSJohannes Thumshirn 	spin_unlock(&fs_info->unused_bgs_lock);
200718bb8bbfSJohannes Thumshirn }
200818bb8bbfSJohannes Thumshirn 
read_bg_from_eb(struct btrfs_fs_info * fs_info,const struct btrfs_key * key,const struct btrfs_path * path)2009*ca283ea9SDavid Sterba static int read_bg_from_eb(struct btrfs_fs_info *fs_info, const struct btrfs_key *key,
2010*ca283ea9SDavid Sterba 			   const struct btrfs_path *path)
2011e3ba67a1SJohannes Thumshirn {
20127dc66abbSFilipe Manana 	struct btrfs_chunk_map *map;
2013e3ba67a1SJohannes Thumshirn 	struct btrfs_block_group_item bg;
2014e3ba67a1SJohannes Thumshirn 	struct extent_buffer *leaf;
2015e3ba67a1SJohannes Thumshirn 	int slot;
2016e3ba67a1SJohannes Thumshirn 	u64 flags;
2017e3ba67a1SJohannes Thumshirn 	int ret = 0;
2018e3ba67a1SJohannes Thumshirn 
2019e3ba67a1SJohannes Thumshirn 	slot = path->slots[0];
2020e3ba67a1SJohannes Thumshirn 	leaf = path->nodes[0];
2021e3ba67a1SJohannes Thumshirn 
20227dc66abbSFilipe Manana 	map = btrfs_find_chunk_map(fs_info, key->objectid, key->offset);
20237dc66abbSFilipe Manana 	if (!map) {
2024e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
2025e3ba67a1SJohannes Thumshirn 			  "logical %llu len %llu found bg but no related chunk",
2026e3ba67a1SJohannes Thumshirn 			  key->objectid, key->offset);
2027e3ba67a1SJohannes Thumshirn 		return -ENOENT;
2028e3ba67a1SJohannes Thumshirn 	}
2029e3ba67a1SJohannes Thumshirn 
20307dc66abbSFilipe Manana 	if (map->start != key->objectid || map->chunk_len != key->offset) {
2031e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
2032e3ba67a1SJohannes Thumshirn 			"block group %llu len %llu mismatch with chunk %llu len %llu",
20337dc66abbSFilipe Manana 			  key->objectid, key->offset, map->start, map->chunk_len);
2034e3ba67a1SJohannes Thumshirn 		ret = -EUCLEAN;
20357dc66abbSFilipe Manana 		goto out_free_map;
2036e3ba67a1SJohannes Thumshirn 	}
2037e3ba67a1SJohannes Thumshirn 
2038e3ba67a1SJohannes Thumshirn 	read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot),
2039e3ba67a1SJohannes Thumshirn 			   sizeof(bg));
2040e3ba67a1SJohannes Thumshirn 	flags = btrfs_stack_block_group_flags(&bg) &
2041e3ba67a1SJohannes Thumshirn 		BTRFS_BLOCK_GROUP_TYPE_MASK;
2042e3ba67a1SJohannes Thumshirn 
20437dc66abbSFilipe Manana 	if (flags != (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
2044e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
2045e3ba67a1SJohannes Thumshirn "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
2046e3ba67a1SJohannes Thumshirn 			  key->objectid, key->offset, flags,
20477dc66abbSFilipe Manana 			  (BTRFS_BLOCK_GROUP_TYPE_MASK & map->type));
2048e3ba67a1SJohannes Thumshirn 		ret = -EUCLEAN;
2049e3ba67a1SJohannes Thumshirn 	}
2050e3ba67a1SJohannes Thumshirn 
20517dc66abbSFilipe Manana out_free_map:
20527dc66abbSFilipe Manana 	btrfs_free_chunk_map(map);
2053e3ba67a1SJohannes Thumshirn 	return ret;
2054e3ba67a1SJohannes Thumshirn }
2055e3ba67a1SJohannes Thumshirn 
find_first_block_group(struct btrfs_fs_info * fs_info,struct btrfs_path * path,const struct btrfs_key * key)20564358d963SJosef Bacik static int find_first_block_group(struct btrfs_fs_info *fs_info,
20574358d963SJosef Bacik 				  struct btrfs_path *path,
2058*ca283ea9SDavid Sterba 				  const struct btrfs_key *key)
20594358d963SJosef Bacik {
2060dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
2061e3ba67a1SJohannes Thumshirn 	int ret;
20624358d963SJosef Bacik 	struct btrfs_key found_key;
20634358d963SJosef Bacik 
206436dfbbe2SGabriel Niebler 	btrfs_for_each_slot(root, key, &found_key, path, ret) {
20654358d963SJosef Bacik 		if (found_key.objectid >= key->objectid &&
20664358d963SJosef Bacik 		    found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
206736dfbbe2SGabriel Niebler 			return read_bg_from_eb(fs_info, &found_key, path);
2068e3ba67a1SJohannes Thumshirn 		}
20694358d963SJosef Bacik 	}
20704358d963SJosef Bacik 	return ret;
20714358d963SJosef Bacik }
20724358d963SJosef Bacik 
set_avail_alloc_bits(struct btrfs_fs_info * fs_info,u64 flags)20734358d963SJosef Bacik static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
20744358d963SJosef Bacik {
20754358d963SJosef Bacik 	u64 extra_flags = chunk_to_extended(flags) &
20764358d963SJosef Bacik 				BTRFS_EXTENDED_PROFILE_MASK;
20774358d963SJosef Bacik 
20784358d963SJosef Bacik 	write_seqlock(&fs_info->profiles_lock);
20794358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA)
20804358d963SJosef Bacik 		fs_info->avail_data_alloc_bits |= extra_flags;
20814358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_METADATA)
20824358d963SJosef Bacik 		fs_info->avail_metadata_alloc_bits |= extra_flags;
20834358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
20844358d963SJosef Bacik 		fs_info->avail_system_alloc_bits |= extra_flags;
20854358d963SJosef Bacik 	write_sequnlock(&fs_info->profiles_lock);
20864358d963SJosef Bacik }
20874358d963SJosef Bacik 
208843dd529aSDavid Sterba /*
208943dd529aSDavid Sterba  * Map a physical disk address to a list of logical addresses.
20909ee9b979SNikolay Borisov  *
20919ee9b979SNikolay Borisov  * @fs_info:       the filesystem
209296a14336SNikolay Borisov  * @chunk_start:   logical address of block group
209396a14336SNikolay Borisov  * @physical:	   physical address to map to logical addresses
209496a14336SNikolay Borisov  * @logical:	   return array of logical addresses which map to @physical
209596a14336SNikolay Borisov  * @naddrs:	   length of @logical
209696a14336SNikolay Borisov  * @stripe_len:    size of IO stripe for the given block group
209796a14336SNikolay Borisov  *
209896a14336SNikolay Borisov  * Maps a particular @physical disk address to a list of @logical addresses.
209996a14336SNikolay Borisov  * Used primarily to exclude those portions of a block group that contain super
210096a14336SNikolay Borisov  * block copies.
210196a14336SNikolay Borisov  */
btrfs_rmap_block(struct btrfs_fs_info * fs_info,u64 chunk_start,u64 physical,u64 ** logical,int * naddrs,int * stripe_len)210296a14336SNikolay Borisov int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
21031eb82ef8SChristoph Hellwig 		     u64 physical, u64 **logical, int *naddrs, int *stripe_len)
210496a14336SNikolay Borisov {
21057dc66abbSFilipe Manana 	struct btrfs_chunk_map *map;
210696a14336SNikolay Borisov 	u64 *buf;
210796a14336SNikolay Borisov 	u64 bytenr;
21081776ad17SNikolay Borisov 	u64 data_stripe_length;
21091776ad17SNikolay Borisov 	u64 io_stripe_size;
21101776ad17SNikolay Borisov 	int i, nr = 0;
21111776ad17SNikolay Borisov 	int ret = 0;
211296a14336SNikolay Borisov 
21137dc66abbSFilipe Manana 	map = btrfs_get_chunk_map(fs_info, chunk_start, 1);
21147dc66abbSFilipe Manana 	if (IS_ERR(map))
211596a14336SNikolay Borisov 		return -EIO;
211696a14336SNikolay Borisov 
21177dc66abbSFilipe Manana 	data_stripe_length = map->stripe_size;
2118a97699d1SQu Wenruo 	io_stripe_size = BTRFS_STRIPE_LEN;
21197dc66abbSFilipe Manana 	chunk_start = map->start;
212096a14336SNikolay Borisov 
21219e22b925SNikolay Borisov 	/* For RAID5/6 adjust to a full IO stripe length */
21229e22b925SNikolay Borisov 	if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2123cb091225SQu Wenruo 		io_stripe_size = btrfs_stripe_nr_to_offset(nr_data_stripes(map));
212496a14336SNikolay Borisov 
212596a14336SNikolay Borisov 	buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
21261776ad17SNikolay Borisov 	if (!buf) {
21271776ad17SNikolay Borisov 		ret = -ENOMEM;
21281776ad17SNikolay Borisov 		goto out;
21291776ad17SNikolay Borisov 	}
213096a14336SNikolay Borisov 
213196a14336SNikolay Borisov 	for (i = 0; i < map->num_stripes; i++) {
21321776ad17SNikolay Borisov 		bool already_inserted = false;
21336ded22c1SQu Wenruo 		u32 stripe_nr;
21346ded22c1SQu Wenruo 		u32 offset;
21351776ad17SNikolay Borisov 		int j;
21361776ad17SNikolay Borisov 
21371776ad17SNikolay Borisov 		if (!in_range(physical, map->stripes[i].physical,
21381776ad17SNikolay Borisov 			      data_stripe_length))
213996a14336SNikolay Borisov 			continue;
214096a14336SNikolay Borisov 
2141a97699d1SQu Wenruo 		stripe_nr = (physical - map->stripes[i].physical) >>
2142a97699d1SQu Wenruo 			    BTRFS_STRIPE_LEN_SHIFT;
2143a97699d1SQu Wenruo 		offset = (physical - map->stripes[i].physical) &
2144a97699d1SQu Wenruo 			 BTRFS_STRIPE_LEN_MASK;
214596a14336SNikolay Borisov 
2146ac067734SDavid Sterba 		if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
21476ded22c1SQu Wenruo 				 BTRFS_BLOCK_GROUP_RAID10))
21486ded22c1SQu Wenruo 			stripe_nr = div_u64(stripe_nr * map->num_stripes + i,
21496ded22c1SQu Wenruo 					    map->sub_stripes);
215096a14336SNikolay Borisov 		/*
215196a14336SNikolay Borisov 		 * The remaining case would be for RAID56, multiply by
215296a14336SNikolay Borisov 		 * nr_data_stripes().  Alternatively, just use rmap_len below
215396a14336SNikolay Borisov 		 * instead of map->stripe_len
215496a14336SNikolay Borisov 		 */
2155138082f3SNaohiro Aota 		bytenr = chunk_start + stripe_nr * io_stripe_size + offset;
21561776ad17SNikolay Borisov 
21571776ad17SNikolay Borisov 		/* Ensure we don't add duplicate addresses */
215896a14336SNikolay Borisov 		for (j = 0; j < nr; j++) {
21591776ad17SNikolay Borisov 			if (buf[j] == bytenr) {
21601776ad17SNikolay Borisov 				already_inserted = true;
216196a14336SNikolay Borisov 				break;
216296a14336SNikolay Borisov 			}
216396a14336SNikolay Borisov 		}
21641776ad17SNikolay Borisov 
21651776ad17SNikolay Borisov 		if (!already_inserted)
21661776ad17SNikolay Borisov 			buf[nr++] = bytenr;
216796a14336SNikolay Borisov 	}
216896a14336SNikolay Borisov 
216996a14336SNikolay Borisov 	*logical = buf;
217096a14336SNikolay Borisov 	*naddrs = nr;
21711776ad17SNikolay Borisov 	*stripe_len = io_stripe_size;
21721776ad17SNikolay Borisov out:
21737dc66abbSFilipe Manana 	btrfs_free_chunk_map(map);
21741776ad17SNikolay Borisov 	return ret;
217596a14336SNikolay Borisov }
217696a14336SNikolay Borisov 
exclude_super_stripes(struct btrfs_block_group * cache)217732da5386SDavid Sterba static int exclude_super_stripes(struct btrfs_block_group *cache)
21784358d963SJosef Bacik {
21794358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
218012659251SNaohiro Aota 	const bool zoned = btrfs_is_zoned(fs_info);
21814358d963SJosef Bacik 	u64 bytenr;
21824358d963SJosef Bacik 	u64 *logical;
21834358d963SJosef Bacik 	int stripe_len;
21844358d963SJosef Bacik 	int i, nr, ret;
21854358d963SJosef Bacik 
2186b3470b5dSDavid Sterba 	if (cache->start < BTRFS_SUPER_INFO_OFFSET) {
2187b3470b5dSDavid Sterba 		stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start;
21884358d963SJosef Bacik 		cache->bytes_super += stripe_len;
2189b1c8f527SFilipe Manana 		ret = set_extent_bit(&fs_info->excluded_extents, cache->start,
2190b1c8f527SFilipe Manana 				     cache->start + stripe_len - 1,
2191b1c8f527SFilipe Manana 				     EXTENT_UPTODATE, NULL);
21924358d963SJosef Bacik 		if (ret)
21934358d963SJosef Bacik 			return ret;
21944358d963SJosef Bacik 	}
21954358d963SJosef Bacik 
21964358d963SJosef Bacik 	for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
21974358d963SJosef Bacik 		bytenr = btrfs_sb_offset(i);
21981eb82ef8SChristoph Hellwig 		ret = btrfs_rmap_block(fs_info, cache->start,
21994358d963SJosef Bacik 				       bytenr, &logical, &nr, &stripe_len);
22004358d963SJosef Bacik 		if (ret)
22014358d963SJosef Bacik 			return ret;
22024358d963SJosef Bacik 
220312659251SNaohiro Aota 		/* Shouldn't have super stripes in sequential zones */
220412659251SNaohiro Aota 		if (zoned && nr) {
2205f1a07c2bSFilipe Manana 			kfree(logical);
220612659251SNaohiro Aota 			btrfs_err(fs_info,
220712659251SNaohiro Aota 			"zoned: block group %llu must not contain super block",
220812659251SNaohiro Aota 				  cache->start);
220912659251SNaohiro Aota 			return -EUCLEAN;
221012659251SNaohiro Aota 		}
221112659251SNaohiro Aota 
22124358d963SJosef Bacik 		while (nr--) {
221396f9b0f2SNikolay Borisov 			u64 len = min_t(u64, stripe_len,
221496f9b0f2SNikolay Borisov 				cache->start + cache->length - logical[nr]);
22154358d963SJosef Bacik 
22164358d963SJosef Bacik 			cache->bytes_super += len;
2217b1c8f527SFilipe Manana 			ret = set_extent_bit(&fs_info->excluded_extents, logical[nr],
2218b1c8f527SFilipe Manana 					     logical[nr] + len - 1,
2219b1c8f527SFilipe Manana 					     EXTENT_UPTODATE, NULL);
22204358d963SJosef Bacik 			if (ret) {
22214358d963SJosef Bacik 				kfree(logical);
22224358d963SJosef Bacik 				return ret;
22234358d963SJosef Bacik 			}
22244358d963SJosef Bacik 		}
22254358d963SJosef Bacik 
22264358d963SJosef Bacik 		kfree(logical);
22274358d963SJosef Bacik 	}
22284358d963SJosef Bacik 	return 0;
22294358d963SJosef Bacik }
22304358d963SJosef Bacik 
btrfs_create_block_group_cache(struct btrfs_fs_info * fs_info,u64 start)223132da5386SDavid Sterba static struct btrfs_block_group *btrfs_create_block_group_cache(
22329afc6649SQu Wenruo 		struct btrfs_fs_info *fs_info, u64 start)
22334358d963SJosef Bacik {
223432da5386SDavid Sterba 	struct btrfs_block_group *cache;
22354358d963SJosef Bacik 
22364358d963SJosef Bacik 	cache = kzalloc(sizeof(*cache), GFP_NOFS);
22374358d963SJosef Bacik 	if (!cache)
22384358d963SJosef Bacik 		return NULL;
22394358d963SJosef Bacik 
22404358d963SJosef Bacik 	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
22414358d963SJosef Bacik 					GFP_NOFS);
22424358d963SJosef Bacik 	if (!cache->free_space_ctl) {
22434358d963SJosef Bacik 		kfree(cache);
22444358d963SJosef Bacik 		return NULL;
22454358d963SJosef Bacik 	}
22464358d963SJosef Bacik 
2247b3470b5dSDavid Sterba 	cache->start = start;
22484358d963SJosef Bacik 
22494358d963SJosef Bacik 	cache->fs_info = fs_info;
22504358d963SJosef Bacik 	cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
22514358d963SJosef Bacik 
22526e80d4f8SDennis Zhou 	cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED;
22536e80d4f8SDennis Zhou 
225448aaeebeSJosef Bacik 	refcount_set(&cache->refs, 1);
22554358d963SJosef Bacik 	spin_lock_init(&cache->lock);
22564358d963SJosef Bacik 	init_rwsem(&cache->data_rwsem);
22574358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->list);
22584358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->cluster_list);
22594358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->bg_list);
22604358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->ro_list);
2261b0643e59SDennis Zhou 	INIT_LIST_HEAD(&cache->discard_list);
22624358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->dirty_list);
22634358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->io_list);
2264afba2bc0SNaohiro Aota 	INIT_LIST_HEAD(&cache->active_bg_list);
2265cd79909bSJosef Bacik 	btrfs_init_free_space_ctl(cache, cache->free_space_ctl);
22666b7304afSFilipe Manana 	atomic_set(&cache->frozen, 0);
22674358d963SJosef Bacik 	mutex_init(&cache->free_space_lock);
22684358d963SJosef Bacik 
22694358d963SJosef Bacik 	return cache;
22704358d963SJosef Bacik }
22714358d963SJosef Bacik 
22724358d963SJosef Bacik /*
22734358d963SJosef Bacik  * Iterate all chunks and verify that each of them has the corresponding block
22744358d963SJosef Bacik  * group
22754358d963SJosef Bacik  */
check_chunk_block_group_mappings(struct btrfs_fs_info * fs_info)22764358d963SJosef Bacik static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
22774358d963SJosef Bacik {
22784358d963SJosef Bacik 	u64 start = 0;
22794358d963SJosef Bacik 	int ret = 0;
22804358d963SJosef Bacik 
22814358d963SJosef Bacik 	while (1) {
22827dc66abbSFilipe Manana 		struct btrfs_chunk_map *map;
22837dc66abbSFilipe Manana 		struct btrfs_block_group *bg;
22847dc66abbSFilipe Manana 
22854358d963SJosef Bacik 		/*
22867dc66abbSFilipe Manana 		 * btrfs_find_chunk_map() will return the first chunk map
22877dc66abbSFilipe Manana 		 * intersecting the range, so setting @length to 1 is enough to
22884358d963SJosef Bacik 		 * get the first chunk.
22894358d963SJosef Bacik 		 */
22907dc66abbSFilipe Manana 		map = btrfs_find_chunk_map(fs_info, start, 1);
22917dc66abbSFilipe Manana 		if (!map)
22924358d963SJosef Bacik 			break;
22934358d963SJosef Bacik 
22947dc66abbSFilipe Manana 		bg = btrfs_lookup_block_group(fs_info, map->start);
22954358d963SJosef Bacik 		if (!bg) {
22964358d963SJosef Bacik 			btrfs_err(fs_info,
22974358d963SJosef Bacik 	"chunk start=%llu len=%llu doesn't have corresponding block group",
22987dc66abbSFilipe Manana 				     map->start, map->chunk_len);
22994358d963SJosef Bacik 			ret = -EUCLEAN;
23007dc66abbSFilipe Manana 			btrfs_free_chunk_map(map);
23014358d963SJosef Bacik 			break;
23024358d963SJosef Bacik 		}
23037dc66abbSFilipe Manana 		if (bg->start != map->start || bg->length != map->chunk_len ||
23044358d963SJosef Bacik 		    (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
23057dc66abbSFilipe Manana 		    (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
23064358d963SJosef Bacik 			btrfs_err(fs_info,
23074358d963SJosef Bacik "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",
23087dc66abbSFilipe Manana 				map->start, map->chunk_len,
23097dc66abbSFilipe Manana 				map->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
2310b3470b5dSDavid Sterba 				bg->start, bg->length,
23114358d963SJosef Bacik 				bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
23124358d963SJosef Bacik 			ret = -EUCLEAN;
23137dc66abbSFilipe Manana 			btrfs_free_chunk_map(map);
23144358d963SJosef Bacik 			btrfs_put_block_group(bg);
23154358d963SJosef Bacik 			break;
23164358d963SJosef Bacik 		}
23177dc66abbSFilipe Manana 		start = map->start + map->chunk_len;
23187dc66abbSFilipe Manana 		btrfs_free_chunk_map(map);
23194358d963SJosef Bacik 		btrfs_put_block_group(bg);
23204358d963SJosef Bacik 	}
23214358d963SJosef Bacik 	return ret;
23224358d963SJosef Bacik }
23234358d963SJosef Bacik 
read_one_block_group(struct btrfs_fs_info * info,struct btrfs_block_group_item * bgi,const struct btrfs_key * key,int need_clear)2324ffb9e0f0SQu Wenruo static int read_one_block_group(struct btrfs_fs_info *info,
23254afd2fe8SJohannes Thumshirn 				struct btrfs_block_group_item *bgi,
2326d49a2ddbSQu Wenruo 				const struct btrfs_key *key,
2327ffb9e0f0SQu Wenruo 				int need_clear)
2328ffb9e0f0SQu Wenruo {
232932da5386SDavid Sterba 	struct btrfs_block_group *cache;
2330ffb9e0f0SQu Wenruo 	const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS);
2331ffb9e0f0SQu Wenruo 	int ret;
2332ffb9e0f0SQu Wenruo 
2333d49a2ddbSQu Wenruo 	ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY);
2334ffb9e0f0SQu Wenruo 
23359afc6649SQu Wenruo 	cache = btrfs_create_block_group_cache(info, key->objectid);
2336ffb9e0f0SQu Wenruo 	if (!cache)
2337ffb9e0f0SQu Wenruo 		return -ENOMEM;
2338ffb9e0f0SQu Wenruo 
23394afd2fe8SJohannes Thumshirn 	cache->length = key->offset;
23404afd2fe8SJohannes Thumshirn 	cache->used = btrfs_stack_block_group_used(bgi);
23417248e0ceSQu Wenruo 	cache->commit_used = cache->used;
23424afd2fe8SJohannes Thumshirn 	cache->flags = btrfs_stack_block_group_flags(bgi);
2343f7238e50SJosef Bacik 	cache->global_root_id = btrfs_stack_block_group_chunk_objectid(bgi);
23449afc6649SQu Wenruo 
2345e3e39c72SMarcos Paulo de Souza 	set_free_space_tree_thresholds(cache);
2346e3e39c72SMarcos Paulo de Souza 
2347ffb9e0f0SQu Wenruo 	if (need_clear) {
2348ffb9e0f0SQu Wenruo 		/*
2349ffb9e0f0SQu Wenruo 		 * When we mount with old space cache, we need to
2350ffb9e0f0SQu Wenruo 		 * set BTRFS_DC_CLEAR and set dirty flag.
2351ffb9e0f0SQu Wenruo 		 *
2352ffb9e0f0SQu Wenruo 		 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
2353ffb9e0f0SQu Wenruo 		 *    truncate the old free space cache inode and
2354ffb9e0f0SQu Wenruo 		 *    setup a new one.
2355ffb9e0f0SQu Wenruo 		 * b) Setting 'dirty flag' makes sure that we flush
2356ffb9e0f0SQu Wenruo 		 *    the new space cache info onto disk.
2357ffb9e0f0SQu Wenruo 		 */
2358ffb9e0f0SQu Wenruo 		if (btrfs_test_opt(info, SPACE_CACHE))
2359ffb9e0f0SQu Wenruo 			cache->disk_cache_state = BTRFS_DC_CLEAR;
2360ffb9e0f0SQu Wenruo 	}
2361ffb9e0f0SQu Wenruo 	if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
2362ffb9e0f0SQu Wenruo 	    (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
2363ffb9e0f0SQu Wenruo 			btrfs_err(info,
2364ffb9e0f0SQu Wenruo "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
2365ffb9e0f0SQu Wenruo 				  cache->start);
2366ffb9e0f0SQu Wenruo 			ret = -EINVAL;
2367ffb9e0f0SQu Wenruo 			goto error;
2368ffb9e0f0SQu Wenruo 	}
2369ffb9e0f0SQu Wenruo 
2370a94794d5SNaohiro Aota 	ret = btrfs_load_block_group_zone_info(cache, false);
237108e11a3dSNaohiro Aota 	if (ret) {
237208e11a3dSNaohiro Aota 		btrfs_err(info, "zoned: failed to load zone info of bg %llu",
237308e11a3dSNaohiro Aota 			  cache->start);
237408e11a3dSNaohiro Aota 		goto error;
237508e11a3dSNaohiro Aota 	}
237608e11a3dSNaohiro Aota 
2377ffb9e0f0SQu Wenruo 	/*
2378ffb9e0f0SQu Wenruo 	 * We need to exclude the super stripes now so that the space info has
2379ffb9e0f0SQu Wenruo 	 * super bytes accounted for, otherwise we'll think we have more space
2380ffb9e0f0SQu Wenruo 	 * than we actually do.
2381ffb9e0f0SQu Wenruo 	 */
2382ffb9e0f0SQu Wenruo 	ret = exclude_super_stripes(cache);
2383ffb9e0f0SQu Wenruo 	if (ret) {
2384ffb9e0f0SQu Wenruo 		/* We may have excluded something, so call this just in case. */
2385ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
2386ffb9e0f0SQu Wenruo 		goto error;
2387ffb9e0f0SQu Wenruo 	}
2388ffb9e0f0SQu Wenruo 
2389ffb9e0f0SQu Wenruo 	/*
2390169e0da9SNaohiro Aota 	 * For zoned filesystem, space after the allocation offset is the only
2391169e0da9SNaohiro Aota 	 * free space for a block group. So, we don't need any caching work.
2392169e0da9SNaohiro Aota 	 * btrfs_calc_zone_unusable() will set the amount of free space and
2393169e0da9SNaohiro Aota 	 * zone_unusable space.
2394169e0da9SNaohiro Aota 	 *
2395169e0da9SNaohiro Aota 	 * For regular filesystem, check for two cases, either we are full, and
2396169e0da9SNaohiro Aota 	 * therefore don't need to bother with the caching work since we won't
2397169e0da9SNaohiro Aota 	 * find any space, or we are empty, and we can just add all the space
2398169e0da9SNaohiro Aota 	 * in and be done with it.  This saves us _a_lot_ of time, particularly
2399169e0da9SNaohiro Aota 	 * in the full case.
2400ffb9e0f0SQu Wenruo 	 */
2401169e0da9SNaohiro Aota 	if (btrfs_is_zoned(info)) {
2402169e0da9SNaohiro Aota 		btrfs_calc_zone_unusable(cache);
2403c46c4247SNaohiro Aota 		/* Should not have any excluded extents. Just in case, though. */
2404c46c4247SNaohiro Aota 		btrfs_free_excluded_extents(cache);
2405169e0da9SNaohiro Aota 	} else if (cache->length == cache->used) {
2406ffb9e0f0SQu Wenruo 		cache->cached = BTRFS_CACHE_FINISHED;
2407ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
2408ffb9e0f0SQu Wenruo 	} else if (cache->used == 0) {
2409ffb9e0f0SQu Wenruo 		cache->cached = BTRFS_CACHE_FINISHED;
24103b9f0995SFilipe Manana 		ret = btrfs_add_new_free_space(cache, cache->start,
2411d8ccbd21SFilipe Manana 					       cache->start + cache->length, NULL);
2412ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
2413d8ccbd21SFilipe Manana 		if (ret)
2414d8ccbd21SFilipe Manana 			goto error;
2415ffb9e0f0SQu Wenruo 	}
2416ffb9e0f0SQu Wenruo 
2417ffb9e0f0SQu Wenruo 	ret = btrfs_add_block_group_cache(info, cache);
2418ffb9e0f0SQu Wenruo 	if (ret) {
2419ffb9e0f0SQu Wenruo 		btrfs_remove_free_space_cache(cache);
2420ffb9e0f0SQu Wenruo 		goto error;
2421ffb9e0f0SQu Wenruo 	}
2422ffb9e0f0SQu Wenruo 	trace_btrfs_add_block_group(info, cache, 0);
2423723de71dSJosef Bacik 	btrfs_add_bg_to_space_info(info, cache);
2424ffb9e0f0SQu Wenruo 
2425ffb9e0f0SQu Wenruo 	set_avail_alloc_bits(info, cache->flags);
2426a09f23c3SAnand Jain 	if (btrfs_chunk_writeable(info, cache->start)) {
2427a09f23c3SAnand Jain 		if (cache->used == 0) {
2428ffb9e0f0SQu Wenruo 			ASSERT(list_empty(&cache->bg_list));
24296e80d4f8SDennis Zhou 			if (btrfs_test_opt(info, DISCARD_ASYNC))
24306e80d4f8SDennis Zhou 				btrfs_discard_queue_work(&info->discard_ctl, cache);
24316e80d4f8SDennis Zhou 			else
2432ffb9e0f0SQu Wenruo 				btrfs_mark_bg_unused(cache);
2433ffb9e0f0SQu Wenruo 		}
2434a09f23c3SAnand Jain 	} else {
2435a09f23c3SAnand Jain 		inc_block_group_ro(cache, 1);
2436a09f23c3SAnand Jain 	}
2437a09f23c3SAnand Jain 
2438ffb9e0f0SQu Wenruo 	return 0;
2439ffb9e0f0SQu Wenruo error:
2440ffb9e0f0SQu Wenruo 	btrfs_put_block_group(cache);
2441ffb9e0f0SQu Wenruo 	return ret;
2442ffb9e0f0SQu Wenruo }
2443ffb9e0f0SQu Wenruo 
fill_dummy_bgs(struct btrfs_fs_info * fs_info)244442437a63SJosef Bacik static int fill_dummy_bgs(struct btrfs_fs_info *fs_info)
244542437a63SJosef Bacik {
244642437a63SJosef Bacik 	struct rb_node *node;
244742437a63SJosef Bacik 	int ret = 0;
244842437a63SJosef Bacik 
24497dc66abbSFilipe Manana 	for (node = rb_first_cached(&fs_info->mapping_tree); node; node = rb_next(node)) {
24507dc66abbSFilipe Manana 		struct btrfs_chunk_map *map;
245142437a63SJosef Bacik 		struct btrfs_block_group *bg;
245242437a63SJosef Bacik 
24537dc66abbSFilipe Manana 		map = rb_entry(node, struct btrfs_chunk_map, rb_node);
24547dc66abbSFilipe Manana 		bg = btrfs_create_block_group_cache(fs_info, map->start);
245542437a63SJosef Bacik 		if (!bg) {
245642437a63SJosef Bacik 			ret = -ENOMEM;
245742437a63SJosef Bacik 			break;
245842437a63SJosef Bacik 		}
245942437a63SJosef Bacik 
246042437a63SJosef Bacik 		/* Fill dummy cache as FULL */
24617dc66abbSFilipe Manana 		bg->length = map->chunk_len;
246242437a63SJosef Bacik 		bg->flags = map->type;
246342437a63SJosef Bacik 		bg->cached = BTRFS_CACHE_FINISHED;
24647dc66abbSFilipe Manana 		bg->used = map->chunk_len;
246542437a63SJosef Bacik 		bg->flags = map->type;
246642437a63SJosef Bacik 		ret = btrfs_add_block_group_cache(fs_info, bg);
24672b29726cSQu Wenruo 		/*
24682b29726cSQu Wenruo 		 * We may have some valid block group cache added already, in
24692b29726cSQu Wenruo 		 * that case we skip to the next one.
24702b29726cSQu Wenruo 		 */
24712b29726cSQu Wenruo 		if (ret == -EEXIST) {
24722b29726cSQu Wenruo 			ret = 0;
24732b29726cSQu Wenruo 			btrfs_put_block_group(bg);
24742b29726cSQu Wenruo 			continue;
24752b29726cSQu Wenruo 		}
24762b29726cSQu Wenruo 
247742437a63SJosef Bacik 		if (ret) {
247842437a63SJosef Bacik 			btrfs_remove_free_space_cache(bg);
247942437a63SJosef Bacik 			btrfs_put_block_group(bg);
248042437a63SJosef Bacik 			break;
248142437a63SJosef Bacik 		}
24822b29726cSQu Wenruo 
2483723de71dSJosef Bacik 		btrfs_add_bg_to_space_info(fs_info, bg);
248442437a63SJosef Bacik 
248542437a63SJosef Bacik 		set_avail_alloc_bits(fs_info, bg->flags);
248642437a63SJosef Bacik 	}
248742437a63SJosef Bacik 	if (!ret)
248842437a63SJosef Bacik 		btrfs_init_global_block_rsv(fs_info);
248942437a63SJosef Bacik 	return ret;
249042437a63SJosef Bacik }
249142437a63SJosef Bacik 
btrfs_read_block_groups(struct btrfs_fs_info * info)24924358d963SJosef Bacik int btrfs_read_block_groups(struct btrfs_fs_info *info)
24934358d963SJosef Bacik {
2494dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(info);
24954358d963SJosef Bacik 	struct btrfs_path *path;
24964358d963SJosef Bacik 	int ret;
249732da5386SDavid Sterba 	struct btrfs_block_group *cache;
24984358d963SJosef Bacik 	struct btrfs_space_info *space_info;
24994358d963SJosef Bacik 	struct btrfs_key key;
25004358d963SJosef Bacik 	int need_clear = 0;
25014358d963SJosef Bacik 	u64 cache_gen;
25024358d963SJosef Bacik 
250381d5d614SQu Wenruo 	/*
250481d5d614SQu Wenruo 	 * Either no extent root (with ibadroots rescue option) or we have
250581d5d614SQu Wenruo 	 * unsupported RO options. The fs can never be mounted read-write, so no
250681d5d614SQu Wenruo 	 * need to waste time searching block group items.
250781d5d614SQu Wenruo 	 *
250881d5d614SQu Wenruo 	 * This also allows new extent tree related changes to be RO compat,
250981d5d614SQu Wenruo 	 * no need for a full incompat flag.
251081d5d614SQu Wenruo 	 */
251181d5d614SQu Wenruo 	if (!root || (btrfs_super_compat_ro_flags(info->super_copy) &
251281d5d614SQu Wenruo 		      ~BTRFS_FEATURE_COMPAT_RO_SUPP))
251342437a63SJosef Bacik 		return fill_dummy_bgs(info);
251442437a63SJosef Bacik 
25154358d963SJosef Bacik 	key.objectid = 0;
25164358d963SJosef Bacik 	key.offset = 0;
25174358d963SJosef Bacik 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
25184358d963SJosef Bacik 	path = btrfs_alloc_path();
25194358d963SJosef Bacik 	if (!path)
25204358d963SJosef Bacik 		return -ENOMEM;
25214358d963SJosef Bacik 
25224358d963SJosef Bacik 	cache_gen = btrfs_super_cache_generation(info->super_copy);
25234358d963SJosef Bacik 	if (btrfs_test_opt(info, SPACE_CACHE) &&
25244358d963SJosef Bacik 	    btrfs_super_generation(info->super_copy) != cache_gen)
25254358d963SJosef Bacik 		need_clear = 1;
25264358d963SJosef Bacik 	if (btrfs_test_opt(info, CLEAR_CACHE))
25274358d963SJosef Bacik 		need_clear = 1;
25284358d963SJosef Bacik 
25294358d963SJosef Bacik 	while (1) {
25304afd2fe8SJohannes Thumshirn 		struct btrfs_block_group_item bgi;
25314afd2fe8SJohannes Thumshirn 		struct extent_buffer *leaf;
25324afd2fe8SJohannes Thumshirn 		int slot;
25334afd2fe8SJohannes Thumshirn 
25344358d963SJosef Bacik 		ret = find_first_block_group(info, path, &key);
25354358d963SJosef Bacik 		if (ret > 0)
25364358d963SJosef Bacik 			break;
25374358d963SJosef Bacik 		if (ret != 0)
25384358d963SJosef Bacik 			goto error;
25394358d963SJosef Bacik 
25404afd2fe8SJohannes Thumshirn 		leaf = path->nodes[0];
25414afd2fe8SJohannes Thumshirn 		slot = path->slots[0];
25424afd2fe8SJohannes Thumshirn 
25434afd2fe8SJohannes Thumshirn 		read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
25444afd2fe8SJohannes Thumshirn 				   sizeof(bgi));
25454afd2fe8SJohannes Thumshirn 
25464afd2fe8SJohannes Thumshirn 		btrfs_item_key_to_cpu(leaf, &key, slot);
25474afd2fe8SJohannes Thumshirn 		btrfs_release_path(path);
25484afd2fe8SJohannes Thumshirn 		ret = read_one_block_group(info, &bgi, &key, need_clear);
2549ffb9e0f0SQu Wenruo 		if (ret < 0)
25504358d963SJosef Bacik 			goto error;
2551ffb9e0f0SQu Wenruo 		key.objectid += key.offset;
2552ffb9e0f0SQu Wenruo 		key.offset = 0;
25534358d963SJosef Bacik 	}
25547837fa88SJosef Bacik 	btrfs_release_path(path);
25554358d963SJosef Bacik 
255672804905SJosef Bacik 	list_for_each_entry(space_info, &info->space_info, list) {
255749ea112dSJosef Bacik 		int i;
255849ea112dSJosef Bacik 
255949ea112dSJosef Bacik 		for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
256049ea112dSJosef Bacik 			if (list_empty(&space_info->block_groups[i]))
256149ea112dSJosef Bacik 				continue;
256249ea112dSJosef Bacik 			cache = list_first_entry(&space_info->block_groups[i],
256349ea112dSJosef Bacik 						 struct btrfs_block_group,
256449ea112dSJosef Bacik 						 list);
256549ea112dSJosef Bacik 			btrfs_sysfs_add_block_group_type(cache);
256649ea112dSJosef Bacik 		}
256749ea112dSJosef Bacik 
25684358d963SJosef Bacik 		if (!(btrfs_get_alloc_profile(info, space_info->flags) &
25694358d963SJosef Bacik 		      (BTRFS_BLOCK_GROUP_RAID10 |
25704358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_RAID1_MASK |
25714358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_RAID56_MASK |
25724358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_DUP)))
25734358d963SJosef Bacik 			continue;
25744358d963SJosef Bacik 		/*
25754358d963SJosef Bacik 		 * Avoid allocating from un-mirrored block group if there are
25764358d963SJosef Bacik 		 * mirrored block groups.
25774358d963SJosef Bacik 		 */
25784358d963SJosef Bacik 		list_for_each_entry(cache,
25794358d963SJosef Bacik 				&space_info->block_groups[BTRFS_RAID_RAID0],
25804358d963SJosef Bacik 				list)
2581e11c0406SJosef Bacik 			inc_block_group_ro(cache, 1);
25824358d963SJosef Bacik 		list_for_each_entry(cache,
25834358d963SJosef Bacik 				&space_info->block_groups[BTRFS_RAID_SINGLE],
25844358d963SJosef Bacik 				list)
2585e11c0406SJosef Bacik 			inc_block_group_ro(cache, 1);
25864358d963SJosef Bacik 	}
25874358d963SJosef Bacik 
25884358d963SJosef Bacik 	btrfs_init_global_block_rsv(info);
25894358d963SJosef Bacik 	ret = check_chunk_block_group_mappings(info);
25904358d963SJosef Bacik error:
25914358d963SJosef Bacik 	btrfs_free_path(path);
25922b29726cSQu Wenruo 	/*
25932b29726cSQu Wenruo 	 * We've hit some error while reading the extent tree, and have
25942b29726cSQu Wenruo 	 * rescue=ibadroots mount option.
25952b29726cSQu Wenruo 	 * Try to fill the tree using dummy block groups so that the user can
25962b29726cSQu Wenruo 	 * continue to mount and grab their data.
25972b29726cSQu Wenruo 	 */
25982b29726cSQu Wenruo 	if (ret && btrfs_test_opt(info, IGNOREBADROOTS))
25992b29726cSQu Wenruo 		ret = fill_dummy_bgs(info);
26004358d963SJosef Bacik 	return ret;
26014358d963SJosef Bacik }
26024358d963SJosef Bacik 
260379bd3712SFilipe Manana /*
260479bd3712SFilipe Manana  * This function, insert_block_group_item(), belongs to the phase 2 of chunk
260579bd3712SFilipe Manana  * allocation.
260679bd3712SFilipe Manana  *
260779bd3712SFilipe Manana  * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
260879bd3712SFilipe Manana  * phases.
260979bd3712SFilipe Manana  */
insert_block_group_item(struct btrfs_trans_handle * trans,struct btrfs_block_group * block_group)261097f4728aSQu Wenruo static int insert_block_group_item(struct btrfs_trans_handle *trans,
261197f4728aSQu Wenruo 				   struct btrfs_block_group *block_group)
261297f4728aSQu Wenruo {
261397f4728aSQu Wenruo 	struct btrfs_fs_info *fs_info = trans->fs_info;
261497f4728aSQu Wenruo 	struct btrfs_block_group_item bgi;
2615dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
261697f4728aSQu Wenruo 	struct btrfs_key key;
2617675dfe12SFilipe Manana 	u64 old_commit_used;
2618675dfe12SFilipe Manana 	int ret;
261997f4728aSQu Wenruo 
262097f4728aSQu Wenruo 	spin_lock(&block_group->lock);
262197f4728aSQu Wenruo 	btrfs_set_stack_block_group_used(&bgi, block_group->used);
262297f4728aSQu Wenruo 	btrfs_set_stack_block_group_chunk_objectid(&bgi,
2623f7238e50SJosef Bacik 						   block_group->global_root_id);
262497f4728aSQu Wenruo 	btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
2625675dfe12SFilipe Manana 	old_commit_used = block_group->commit_used;
2626675dfe12SFilipe Manana 	block_group->commit_used = block_group->used;
262797f4728aSQu Wenruo 	key.objectid = block_group->start;
262897f4728aSQu Wenruo 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
262997f4728aSQu Wenruo 	key.offset = block_group->length;
263097f4728aSQu Wenruo 	spin_unlock(&block_group->lock);
263197f4728aSQu Wenruo 
2632675dfe12SFilipe Manana 	ret = btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi));
2633675dfe12SFilipe Manana 	if (ret < 0) {
2634675dfe12SFilipe Manana 		spin_lock(&block_group->lock);
2635675dfe12SFilipe Manana 		block_group->commit_used = old_commit_used;
2636675dfe12SFilipe Manana 		spin_unlock(&block_group->lock);
2637675dfe12SFilipe Manana 	}
2638675dfe12SFilipe Manana 
2639675dfe12SFilipe Manana 	return ret;
264097f4728aSQu Wenruo }
264197f4728aSQu Wenruo 
insert_dev_extent(struct btrfs_trans_handle * trans,const struct btrfs_device * device,u64 chunk_offset,u64 start,u64 num_bytes)26422eadb9e7SNikolay Borisov static int insert_dev_extent(struct btrfs_trans_handle *trans,
2643*ca283ea9SDavid Sterba 			     const struct btrfs_device *device, u64 chunk_offset,
26442eadb9e7SNikolay Borisov 			     u64 start, u64 num_bytes)
26452eadb9e7SNikolay Borisov {
26462eadb9e7SNikolay Borisov 	struct btrfs_fs_info *fs_info = device->fs_info;
26472eadb9e7SNikolay Borisov 	struct btrfs_root *root = fs_info->dev_root;
26482eadb9e7SNikolay Borisov 	struct btrfs_path *path;
26492eadb9e7SNikolay Borisov 	struct btrfs_dev_extent *extent;
26502eadb9e7SNikolay Borisov 	struct extent_buffer *leaf;
26512eadb9e7SNikolay Borisov 	struct btrfs_key key;
26522eadb9e7SNikolay Borisov 	int ret;
26532eadb9e7SNikolay Borisov 
26542eadb9e7SNikolay Borisov 	WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
26552eadb9e7SNikolay Borisov 	WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
26562eadb9e7SNikolay Borisov 	path = btrfs_alloc_path();
26572eadb9e7SNikolay Borisov 	if (!path)
26582eadb9e7SNikolay Borisov 		return -ENOMEM;
26592eadb9e7SNikolay Borisov 
26602eadb9e7SNikolay Borisov 	key.objectid = device->devid;
26612eadb9e7SNikolay Borisov 	key.type = BTRFS_DEV_EXTENT_KEY;
26622eadb9e7SNikolay Borisov 	key.offset = start;
26632eadb9e7SNikolay Borisov 	ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*extent));
26642eadb9e7SNikolay Borisov 	if (ret)
26652eadb9e7SNikolay Borisov 		goto out;
26662eadb9e7SNikolay Borisov 
26672eadb9e7SNikolay Borisov 	leaf = path->nodes[0];
26682eadb9e7SNikolay Borisov 	extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
26692eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_chunk_tree(leaf, extent, BTRFS_CHUNK_TREE_OBJECTID);
26702eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_chunk_objectid(leaf, extent,
26712eadb9e7SNikolay Borisov 					    BTRFS_FIRST_CHUNK_TREE_OBJECTID);
26722eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
26732eadb9e7SNikolay Borisov 
26742eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_length(leaf, extent, num_bytes);
267550564b65SFilipe Manana 	btrfs_mark_buffer_dirty(trans, leaf);
26762eadb9e7SNikolay Borisov out:
26772eadb9e7SNikolay Borisov 	btrfs_free_path(path);
26782eadb9e7SNikolay Borisov 	return ret;
26792eadb9e7SNikolay Borisov }
26802eadb9e7SNikolay Borisov 
26812eadb9e7SNikolay Borisov /*
26822eadb9e7SNikolay Borisov  * This function belongs to phase 2.
26832eadb9e7SNikolay Borisov  *
26842eadb9e7SNikolay Borisov  * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
26852eadb9e7SNikolay Borisov  * phases.
26862eadb9e7SNikolay Borisov  */
insert_dev_extents(struct btrfs_trans_handle * trans,u64 chunk_offset,u64 chunk_size)26872eadb9e7SNikolay Borisov static int insert_dev_extents(struct btrfs_trans_handle *trans,
26882eadb9e7SNikolay Borisov 				   u64 chunk_offset, u64 chunk_size)
26892eadb9e7SNikolay Borisov {
26902eadb9e7SNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
26912eadb9e7SNikolay Borisov 	struct btrfs_device *device;
26927dc66abbSFilipe Manana 	struct btrfs_chunk_map *map;
26932eadb9e7SNikolay Borisov 	u64 dev_offset;
26942eadb9e7SNikolay Borisov 	int i;
26952eadb9e7SNikolay Borisov 	int ret = 0;
26962eadb9e7SNikolay Borisov 
26977dc66abbSFilipe Manana 	map = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
26987dc66abbSFilipe Manana 	if (IS_ERR(map))
26997dc66abbSFilipe Manana 		return PTR_ERR(map);
27002eadb9e7SNikolay Borisov 
27012eadb9e7SNikolay Borisov 	/*
27022eadb9e7SNikolay Borisov 	 * Take the device list mutex to prevent races with the final phase of
27032eadb9e7SNikolay Borisov 	 * a device replace operation that replaces the device object associated
27042eadb9e7SNikolay Borisov 	 * with the map's stripes, because the device object's id can change
27052eadb9e7SNikolay Borisov 	 * at any time during that final phase of the device replace operation
27062eadb9e7SNikolay Borisov 	 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the
27072eadb9e7SNikolay Borisov 	 * replaced device and then see it with an ID of BTRFS_DEV_REPLACE_DEVID,
27082eadb9e7SNikolay Borisov 	 * resulting in persisting a device extent item with such ID.
27092eadb9e7SNikolay Borisov 	 */
27102eadb9e7SNikolay Borisov 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
27112eadb9e7SNikolay Borisov 	for (i = 0; i < map->num_stripes; i++) {
27122eadb9e7SNikolay Borisov 		device = map->stripes[i].dev;
27132eadb9e7SNikolay Borisov 		dev_offset = map->stripes[i].physical;
27142eadb9e7SNikolay Borisov 
27152eadb9e7SNikolay Borisov 		ret = insert_dev_extent(trans, device, chunk_offset, dev_offset,
271671fca47bSFilipe Manana 					map->stripe_size);
27172eadb9e7SNikolay Borisov 		if (ret)
27182eadb9e7SNikolay Borisov 			break;
27192eadb9e7SNikolay Borisov 	}
27202eadb9e7SNikolay Borisov 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
27212eadb9e7SNikolay Borisov 
27227dc66abbSFilipe Manana 	btrfs_free_chunk_map(map);
27232eadb9e7SNikolay Borisov 	return ret;
27242eadb9e7SNikolay Borisov }
27252eadb9e7SNikolay Borisov 
272679bd3712SFilipe Manana /*
272779bd3712SFilipe Manana  * This function, btrfs_create_pending_block_groups(), belongs to the phase 2 of
272879bd3712SFilipe Manana  * chunk allocation.
272979bd3712SFilipe Manana  *
273079bd3712SFilipe Manana  * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
273179bd3712SFilipe Manana  * phases.
273279bd3712SFilipe Manana  */
btrfs_create_pending_block_groups(struct btrfs_trans_handle * trans)27334358d963SJosef Bacik void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
27344358d963SJosef Bacik {
27354358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
273632da5386SDavid Sterba 	struct btrfs_block_group *block_group;
27374358d963SJosef Bacik 	int ret = 0;
27384358d963SJosef Bacik 
27394358d963SJosef Bacik 	while (!list_empty(&trans->new_bgs)) {
274049ea112dSJosef Bacik 		int index;
274149ea112dSJosef Bacik 
27424358d963SJosef Bacik 		block_group = list_first_entry(&trans->new_bgs,
274332da5386SDavid Sterba 					       struct btrfs_block_group,
27444358d963SJosef Bacik 					       bg_list);
27454358d963SJosef Bacik 		if (ret)
27464358d963SJosef Bacik 			goto next;
27474358d963SJosef Bacik 
274849ea112dSJosef Bacik 		index = btrfs_bg_flags_to_raid_index(block_group->flags);
274949ea112dSJosef Bacik 
275097f4728aSQu Wenruo 		ret = insert_block_group_item(trans, block_group);
27514358d963SJosef Bacik 		if (ret)
27524358d963SJosef Bacik 			btrfs_abort_transaction(trans, ret);
27533349b57fSJosef Bacik 		if (!test_bit(BLOCK_GROUP_FLAG_CHUNK_ITEM_INSERTED,
27543349b57fSJosef Bacik 			      &block_group->runtime_flags)) {
275579bd3712SFilipe Manana 			mutex_lock(&fs_info->chunk_mutex);
275679bd3712SFilipe Manana 			ret = btrfs_chunk_alloc_add_chunk_item(trans, block_group);
275779bd3712SFilipe Manana 			mutex_unlock(&fs_info->chunk_mutex);
275879bd3712SFilipe Manana 			if (ret)
275979bd3712SFilipe Manana 				btrfs_abort_transaction(trans, ret);
276079bd3712SFilipe Manana 		}
27612eadb9e7SNikolay Borisov 		ret = insert_dev_extents(trans, block_group->start,
276297f4728aSQu Wenruo 					 block_group->length);
27634358d963SJosef Bacik 		if (ret)
27644358d963SJosef Bacik 			btrfs_abort_transaction(trans, ret);
27654358d963SJosef Bacik 		add_block_group_free_space(trans, block_group);
276649ea112dSJosef Bacik 
276749ea112dSJosef Bacik 		/*
276849ea112dSJosef Bacik 		 * If we restriped during balance, we may have added a new raid
276949ea112dSJosef Bacik 		 * type, so now add the sysfs entries when it is safe to do so.
277049ea112dSJosef Bacik 		 * We don't have to worry about locking here as it's handled in
277149ea112dSJosef Bacik 		 * btrfs_sysfs_add_block_group_type.
277249ea112dSJosef Bacik 		 */
277349ea112dSJosef Bacik 		if (block_group->space_info->block_group_kobjs[index] == NULL)
277449ea112dSJosef Bacik 			btrfs_sysfs_add_block_group_type(block_group);
277549ea112dSJosef Bacik 
27764358d963SJosef Bacik 		/* Already aborted the transaction if it failed. */
27774358d963SJosef Bacik next:
27789ef17228SFilipe Manana 		btrfs_dec_delayed_refs_rsv_bg_inserts(fs_info);
27794358d963SJosef Bacik 		list_del_init(&block_group->bg_list);
27800657b20cSFilipe Manana 		clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags);
278112c5128fSFilipe Manana 
278212c5128fSFilipe Manana 		/*
278312c5128fSFilipe Manana 		 * If the block group is still unused, add it to the list of
278412c5128fSFilipe Manana 		 * unused block groups. The block group may have been created in
278512c5128fSFilipe Manana 		 * order to satisfy a space reservation, in which case the
278612c5128fSFilipe Manana 		 * extent allocation only happens later. But often we don't
278712c5128fSFilipe Manana 		 * actually need to allocate space that we previously reserved,
278812c5128fSFilipe Manana 		 * so the block group may become unused for a long time. For
278912c5128fSFilipe Manana 		 * example for metadata we generally reserve space for a worst
279012c5128fSFilipe Manana 		 * possible scenario, but then don't end up allocating all that
279112c5128fSFilipe Manana 		 * space or none at all (due to no need to COW, extent buffers
279212c5128fSFilipe Manana 		 * were already COWed in the current transaction and still
279312c5128fSFilipe Manana 		 * unwritten, tree heights lower than the maximum possible
279412c5128fSFilipe Manana 		 * height, etc). For data we generally reserve the axact amount
279512c5128fSFilipe Manana 		 * of space we are going to allocate later, the exception is
279612c5128fSFilipe Manana 		 * when using compression, as we must reserve space based on the
279712c5128fSFilipe Manana 		 * uncompressed data size, because the compression is only done
279812c5128fSFilipe Manana 		 * when writeback triggered and we don't know how much space we
279912c5128fSFilipe Manana 		 * are actually going to need, so we reserve the uncompressed
280012c5128fSFilipe Manana 		 * size because the data may be uncompressible in the worst case.
280112c5128fSFilipe Manana 		 */
280212c5128fSFilipe Manana 		if (ret == 0) {
280312c5128fSFilipe Manana 			bool used;
280412c5128fSFilipe Manana 
280512c5128fSFilipe Manana 			spin_lock(&block_group->lock);
280612c5128fSFilipe Manana 			used = btrfs_is_block_group_used(block_group);
280712c5128fSFilipe Manana 			spin_unlock(&block_group->lock);
280812c5128fSFilipe Manana 
280912c5128fSFilipe Manana 			if (!used)
281012c5128fSFilipe Manana 				btrfs_mark_bg_unused(block_group);
281112c5128fSFilipe Manana 		}
28124358d963SJosef Bacik 	}
28134358d963SJosef Bacik 	btrfs_trans_release_chunk_metadata(trans);
28144358d963SJosef Bacik }
28154358d963SJosef Bacik 
2816f7238e50SJosef Bacik /*
2817f7238e50SJosef Bacik  * For extent tree v2 we use the block_group_item->chunk_offset to point at our
2818f7238e50SJosef Bacik  * global root id.  For v1 it's always set to BTRFS_FIRST_CHUNK_TREE_OBJECTID.
2819f7238e50SJosef Bacik  */
calculate_global_root_id(const struct btrfs_fs_info * fs_info,u64 offset)2820*ca283ea9SDavid Sterba static u64 calculate_global_root_id(const struct btrfs_fs_info *fs_info, u64 offset)
2821f7238e50SJosef Bacik {
2822f7238e50SJosef Bacik 	u64 div = SZ_1G;
2823f7238e50SJosef Bacik 	u64 index;
2824f7238e50SJosef Bacik 
2825f7238e50SJosef Bacik 	if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
2826f7238e50SJosef Bacik 		return BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2827f7238e50SJosef Bacik 
2828f7238e50SJosef Bacik 	/* If we have a smaller fs index based on 128MiB. */
2829f7238e50SJosef Bacik 	if (btrfs_super_total_bytes(fs_info->super_copy) <= (SZ_1G * 10ULL))
2830f7238e50SJosef Bacik 		div = SZ_128M;
2831f7238e50SJosef Bacik 
2832f7238e50SJosef Bacik 	offset = div64_u64(offset, div);
2833f7238e50SJosef Bacik 	div64_u64_rem(offset, fs_info->nr_global_roots, &index);
2834f7238e50SJosef Bacik 	return index;
2835f7238e50SJosef Bacik }
2836f7238e50SJosef Bacik 
btrfs_make_block_group(struct btrfs_trans_handle * trans,u64 type,u64 chunk_offset,u64 size)283779bd3712SFilipe Manana struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *trans,
28385758d1bdSFilipe Manana 						 u64 type,
283979bd3712SFilipe Manana 						 u64 chunk_offset, u64 size)
28404358d963SJosef Bacik {
28414358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
284232da5386SDavid Sterba 	struct btrfs_block_group *cache;
28434358d963SJosef Bacik 	int ret;
28444358d963SJosef Bacik 
28454358d963SJosef Bacik 	btrfs_set_log_full_commit(trans);
28464358d963SJosef Bacik 
28479afc6649SQu Wenruo 	cache = btrfs_create_block_group_cache(fs_info, chunk_offset);
28484358d963SJosef Bacik 	if (!cache)
284979bd3712SFilipe Manana 		return ERR_PTR(-ENOMEM);
28504358d963SJosef Bacik 
28510657b20cSFilipe Manana 	/*
28520657b20cSFilipe Manana 	 * Mark it as new before adding it to the rbtree of block groups or any
28530657b20cSFilipe Manana 	 * list, so that no other task finds it and calls btrfs_mark_bg_unused()
28540657b20cSFilipe Manana 	 * before the new flag is set.
28550657b20cSFilipe Manana 	 */
28560657b20cSFilipe Manana 	set_bit(BLOCK_GROUP_FLAG_NEW, &cache->runtime_flags);
28570657b20cSFilipe Manana 
28589afc6649SQu Wenruo 	cache->length = size;
2859e3e39c72SMarcos Paulo de Souza 	set_free_space_tree_thresholds(cache);
28604358d963SJosef Bacik 	cache->flags = type;
28614358d963SJosef Bacik 	cache->cached = BTRFS_CACHE_FINISHED;
2862f7238e50SJosef Bacik 	cache->global_root_id = calculate_global_root_id(fs_info, cache->start);
2863f7238e50SJosef Bacik 
2864997e3e2eSBoris Burkov 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
28650d7764ffSDavid Sterba 		set_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &cache->runtime_flags);
286608e11a3dSNaohiro Aota 
2867a94794d5SNaohiro Aota 	ret = btrfs_load_block_group_zone_info(cache, true);
286808e11a3dSNaohiro Aota 	if (ret) {
286908e11a3dSNaohiro Aota 		btrfs_put_block_group(cache);
287079bd3712SFilipe Manana 		return ERR_PTR(ret);
287108e11a3dSNaohiro Aota 	}
287208e11a3dSNaohiro Aota 
28734358d963SJosef Bacik 	ret = exclude_super_stripes(cache);
28744358d963SJosef Bacik 	if (ret) {
28754358d963SJosef Bacik 		/* We may have excluded something, so call this just in case */
28764358d963SJosef Bacik 		btrfs_free_excluded_extents(cache);
28774358d963SJosef Bacik 		btrfs_put_block_group(cache);
287879bd3712SFilipe Manana 		return ERR_PTR(ret);
28794358d963SJosef Bacik 	}
28804358d963SJosef Bacik 
28813b9f0995SFilipe Manana 	ret = btrfs_add_new_free_space(cache, chunk_offset, chunk_offset + size, NULL);
28824358d963SJosef Bacik 	btrfs_free_excluded_extents(cache);
2883d8ccbd21SFilipe Manana 	if (ret) {
2884d8ccbd21SFilipe Manana 		btrfs_put_block_group(cache);
2885d8ccbd21SFilipe Manana 		return ERR_PTR(ret);
2886d8ccbd21SFilipe Manana 	}
28874358d963SJosef Bacik 
28884358d963SJosef Bacik 	/*
28894358d963SJosef Bacik 	 * Ensure the corresponding space_info object is created and
28904358d963SJosef Bacik 	 * assigned to our block group. We want our bg to be added to the rbtree
28914358d963SJosef Bacik 	 * with its ->space_info set.
28924358d963SJosef Bacik 	 */
28934358d963SJosef Bacik 	cache->space_info = btrfs_find_space_info(fs_info, cache->flags);
28944358d963SJosef Bacik 	ASSERT(cache->space_info);
28954358d963SJosef Bacik 
28964358d963SJosef Bacik 	ret = btrfs_add_block_group_cache(fs_info, cache);
28974358d963SJosef Bacik 	if (ret) {
28984358d963SJosef Bacik 		btrfs_remove_free_space_cache(cache);
28994358d963SJosef Bacik 		btrfs_put_block_group(cache);
290079bd3712SFilipe Manana 		return ERR_PTR(ret);
29014358d963SJosef Bacik 	}
29024358d963SJosef Bacik 
29034358d963SJosef Bacik 	/*
29044358d963SJosef Bacik 	 * Now that our block group has its ->space_info set and is inserted in
29054358d963SJosef Bacik 	 * the rbtree, update the space info's counters.
29064358d963SJosef Bacik 	 */
29074358d963SJosef Bacik 	trace_btrfs_add_block_group(fs_info, cache, 1);
2908723de71dSJosef Bacik 	btrfs_add_bg_to_space_info(fs_info, cache);
29094358d963SJosef Bacik 	btrfs_update_global_block_rsv(fs_info);
29104358d963SJosef Bacik 
29119d4b0a12SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
29129d4b0a12SJosef Bacik 	if (btrfs_should_fragment_free_space(cache)) {
29135758d1bdSFilipe Manana 		cache->space_info->bytes_used += size >> 1;
29149d4b0a12SJosef Bacik 		fragment_free_space(cache);
29159d4b0a12SJosef Bacik 	}
29169d4b0a12SJosef Bacik #endif
29174358d963SJosef Bacik 
29184358d963SJosef Bacik 	list_add_tail(&cache->bg_list, &trans->new_bgs);
29199ef17228SFilipe Manana 	btrfs_inc_delayed_refs_rsv_bg_inserts(fs_info);
29204358d963SJosef Bacik 
29214358d963SJosef Bacik 	set_avail_alloc_bits(fs_info, type);
292279bd3712SFilipe Manana 	return cache;
29234358d963SJosef Bacik }
292426ce2095SJosef Bacik 
2925b12de528SQu Wenruo /*
2926b12de528SQu Wenruo  * Mark one block group RO, can be called several times for the same block
2927b12de528SQu Wenruo  * group.
2928b12de528SQu Wenruo  *
2929b12de528SQu Wenruo  * @cache:		the destination block group
2930b12de528SQu Wenruo  * @do_chunk_alloc:	whether need to do chunk pre-allocation, this is to
2931b12de528SQu Wenruo  * 			ensure we still have some free space after marking this
2932b12de528SQu Wenruo  * 			block group RO.
2933b12de528SQu Wenruo  */
btrfs_inc_block_group_ro(struct btrfs_block_group * cache,bool do_chunk_alloc)2934b12de528SQu Wenruo int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
2935b12de528SQu Wenruo 			     bool do_chunk_alloc)
293626ce2095SJosef Bacik {
293726ce2095SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
293826ce2095SJosef Bacik 	struct btrfs_trans_handle *trans;
2939dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
294026ce2095SJosef Bacik 	u64 alloc_flags;
294126ce2095SJosef Bacik 	int ret;
2942b6e9f16cSNikolay Borisov 	bool dirty_bg_running;
294326ce2095SJosef Bacik 
29442d192fc4SQu Wenruo 	/*
29452d192fc4SQu Wenruo 	 * This can only happen when we are doing read-only scrub on read-only
29462d192fc4SQu Wenruo 	 * mount.
29472d192fc4SQu Wenruo 	 * In that case we should not start a new transaction on read-only fs.
29482d192fc4SQu Wenruo 	 * Thus here we skip all chunk allocations.
29492d192fc4SQu Wenruo 	 */
29502d192fc4SQu Wenruo 	if (sb_rdonly(fs_info->sb)) {
29512d192fc4SQu Wenruo 		mutex_lock(&fs_info->ro_block_group_mutex);
29522d192fc4SQu Wenruo 		ret = inc_block_group_ro(cache, 0);
29532d192fc4SQu Wenruo 		mutex_unlock(&fs_info->ro_block_group_mutex);
29542d192fc4SQu Wenruo 		return ret;
29552d192fc4SQu Wenruo 	}
29562d192fc4SQu Wenruo 
2957b6e9f16cSNikolay Borisov 	do {
2958dfe8aec4SJosef Bacik 		trans = btrfs_join_transaction(root);
295926ce2095SJosef Bacik 		if (IS_ERR(trans))
296026ce2095SJosef Bacik 			return PTR_ERR(trans);
296126ce2095SJosef Bacik 
2962b6e9f16cSNikolay Borisov 		dirty_bg_running = false;
2963b6e9f16cSNikolay Borisov 
296426ce2095SJosef Bacik 		/*
2965b6e9f16cSNikolay Borisov 		 * We're not allowed to set block groups readonly after the dirty
2966b6e9f16cSNikolay Borisov 		 * block group cache has started writing.  If it already started,
2967b6e9f16cSNikolay Borisov 		 * back off and let this transaction commit.
296826ce2095SJosef Bacik 		 */
296926ce2095SJosef Bacik 		mutex_lock(&fs_info->ro_block_group_mutex);
297026ce2095SJosef Bacik 		if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
297126ce2095SJosef Bacik 			u64 transid = trans->transid;
297226ce2095SJosef Bacik 
297326ce2095SJosef Bacik 			mutex_unlock(&fs_info->ro_block_group_mutex);
297426ce2095SJosef Bacik 			btrfs_end_transaction(trans);
297526ce2095SJosef Bacik 
297626ce2095SJosef Bacik 			ret = btrfs_wait_for_commit(fs_info, transid);
297726ce2095SJosef Bacik 			if (ret)
297826ce2095SJosef Bacik 				return ret;
2979b6e9f16cSNikolay Borisov 			dirty_bg_running = true;
298026ce2095SJosef Bacik 		}
2981b6e9f16cSNikolay Borisov 	} while (dirty_bg_running);
298226ce2095SJosef Bacik 
2983b12de528SQu Wenruo 	if (do_chunk_alloc) {
298426ce2095SJosef Bacik 		/*
2985b12de528SQu Wenruo 		 * If we are changing raid levels, try to allocate a
2986b12de528SQu Wenruo 		 * corresponding block group with the new raid level.
298726ce2095SJosef Bacik 		 */
2988349e120eSJosef Bacik 		alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
298926ce2095SJosef Bacik 		if (alloc_flags != cache->flags) {
2990b12de528SQu Wenruo 			ret = btrfs_chunk_alloc(trans, alloc_flags,
2991b12de528SQu Wenruo 						CHUNK_ALLOC_FORCE);
299226ce2095SJosef Bacik 			/*
299326ce2095SJosef Bacik 			 * ENOSPC is allowed here, we may have enough space
2994b12de528SQu Wenruo 			 * already allocated at the new raid level to carry on
299526ce2095SJosef Bacik 			 */
299626ce2095SJosef Bacik 			if (ret == -ENOSPC)
299726ce2095SJosef Bacik 				ret = 0;
299826ce2095SJosef Bacik 			if (ret < 0)
299926ce2095SJosef Bacik 				goto out;
300026ce2095SJosef Bacik 		}
3001b12de528SQu Wenruo 	}
300226ce2095SJosef Bacik 
3003a7a63accSJosef Bacik 	ret = inc_block_group_ro(cache, 0);
300426ce2095SJosef Bacik 	if (!ret)
300526ce2095SJosef Bacik 		goto out;
30067561551eSQu Wenruo 	if (ret == -ETXTBSY)
30077561551eSQu Wenruo 		goto unlock_out;
30087561551eSQu Wenruo 
30097561551eSQu Wenruo 	/*
3010eefaf0a1SDavid Sterba 	 * Skip chunk allocation if the bg is SYSTEM, this is to avoid system
30117561551eSQu Wenruo 	 * chunk allocation storm to exhaust the system chunk array.  Otherwise
30127561551eSQu Wenruo 	 * we still want to try our best to mark the block group read-only.
30137561551eSQu Wenruo 	 */
30147561551eSQu Wenruo 	if (!do_chunk_alloc && ret == -ENOSPC &&
30157561551eSQu Wenruo 	    (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM))
30167561551eSQu Wenruo 		goto unlock_out;
30177561551eSQu Wenruo 
301826ce2095SJosef Bacik 	alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags);
301926ce2095SJosef Bacik 	ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
302026ce2095SJosef Bacik 	if (ret < 0)
302126ce2095SJosef Bacik 		goto out;
3022b6a98021SNaohiro Aota 	/*
3023b6a98021SNaohiro Aota 	 * We have allocated a new chunk. We also need to activate that chunk to
3024b6a98021SNaohiro Aota 	 * grant metadata tickets for zoned filesystem.
3025b6a98021SNaohiro Aota 	 */
3026b6a98021SNaohiro Aota 	ret = btrfs_zoned_activate_one_bg(fs_info, cache->space_info, true);
3027b6a98021SNaohiro Aota 	if (ret < 0)
3028b6a98021SNaohiro Aota 		goto out;
3029b6a98021SNaohiro Aota 
3030e11c0406SJosef Bacik 	ret = inc_block_group_ro(cache, 0);
3031195a49eaSFilipe Manana 	if (ret == -ETXTBSY)
3032195a49eaSFilipe Manana 		goto unlock_out;
303326ce2095SJosef Bacik out:
303426ce2095SJosef Bacik 	if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3035349e120eSJosef Bacik 		alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
303626ce2095SJosef Bacik 		mutex_lock(&fs_info->chunk_mutex);
303726ce2095SJosef Bacik 		check_system_chunk(trans, alloc_flags);
303826ce2095SJosef Bacik 		mutex_unlock(&fs_info->chunk_mutex);
303926ce2095SJosef Bacik 	}
3040b12de528SQu Wenruo unlock_out:
304126ce2095SJosef Bacik 	mutex_unlock(&fs_info->ro_block_group_mutex);
304226ce2095SJosef Bacik 
304326ce2095SJosef Bacik 	btrfs_end_transaction(trans);
304426ce2095SJosef Bacik 	return ret;
304526ce2095SJosef Bacik }
304626ce2095SJosef Bacik 
btrfs_dec_block_group_ro(struct btrfs_block_group * cache)304732da5386SDavid Sterba void btrfs_dec_block_group_ro(struct btrfs_block_group *cache)
304826ce2095SJosef Bacik {
304926ce2095SJosef Bacik 	struct btrfs_space_info *sinfo = cache->space_info;
305026ce2095SJosef Bacik 	u64 num_bytes;
305126ce2095SJosef Bacik 
305226ce2095SJosef Bacik 	BUG_ON(!cache->ro);
305326ce2095SJosef Bacik 
305426ce2095SJosef Bacik 	spin_lock(&sinfo->lock);
305526ce2095SJosef Bacik 	spin_lock(&cache->lock);
305626ce2095SJosef Bacik 	if (!--cache->ro) {
3057169e0da9SNaohiro Aota 		if (btrfs_is_zoned(cache->fs_info)) {
3058169e0da9SNaohiro Aota 			/* Migrate zone_unusable bytes back */
305998173255SNaohiro Aota 			cache->zone_unusable =
30608cd44dd1SNaohiro Aota 				(cache->alloc_offset - cache->used - cache->pinned -
30618cd44dd1SNaohiro Aota 				 cache->reserved) +
306298173255SNaohiro Aota 				(cache->length - cache->zone_capacity);
30638cd44dd1SNaohiro Aota 			btrfs_space_info_update_bytes_zone_unusable(cache->fs_info, sinfo,
30648cd44dd1SNaohiro Aota 								    cache->zone_unusable);
3065169e0da9SNaohiro Aota 			sinfo->bytes_readonly -= cache->zone_unusable;
3066169e0da9SNaohiro Aota 		}
3067f9f28e5bSNaohiro Aota 		num_bytes = cache->length - cache->reserved -
3068f9f28e5bSNaohiro Aota 			    cache->pinned - cache->bytes_super -
3069f9f28e5bSNaohiro Aota 			    cache->zone_unusable - cache->used;
3070f9f28e5bSNaohiro Aota 		sinfo->bytes_readonly -= num_bytes;
307126ce2095SJosef Bacik 		list_del_init(&cache->ro_list);
307226ce2095SJosef Bacik 	}
307326ce2095SJosef Bacik 	spin_unlock(&cache->lock);
307426ce2095SJosef Bacik 	spin_unlock(&sinfo->lock);
307526ce2095SJosef Bacik }
307677745c05SJosef Bacik 
update_block_group_item(struct btrfs_trans_handle * trans,struct btrfs_path * path,struct btrfs_block_group * cache)30773be4d8efSQu Wenruo static int update_block_group_item(struct btrfs_trans_handle *trans,
307877745c05SJosef Bacik 				   struct btrfs_path *path,
307932da5386SDavid Sterba 				   struct btrfs_block_group *cache)
308077745c05SJosef Bacik {
308177745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
308277745c05SJosef Bacik 	int ret;
3083dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
308477745c05SJosef Bacik 	unsigned long bi;
308577745c05SJosef Bacik 	struct extent_buffer *leaf;
3086bf38be65SDavid Sterba 	struct btrfs_block_group_item bgi;
3087b3470b5dSDavid Sterba 	struct btrfs_key key;
30887248e0ceSQu Wenruo 	u64 old_commit_used;
30897248e0ceSQu Wenruo 	u64 used;
30907248e0ceSQu Wenruo 
30917248e0ceSQu Wenruo 	/*
30927248e0ceSQu Wenruo 	 * Block group items update can be triggered out of commit transaction
30937248e0ceSQu Wenruo 	 * critical section, thus we need a consistent view of used bytes.
30947248e0ceSQu Wenruo 	 * We cannot use cache->used directly outside of the spin lock, as it
30957248e0ceSQu Wenruo 	 * may be changed.
30967248e0ceSQu Wenruo 	 */
30977248e0ceSQu Wenruo 	spin_lock(&cache->lock);
30987248e0ceSQu Wenruo 	old_commit_used = cache->commit_used;
30997248e0ceSQu Wenruo 	used = cache->used;
31007248e0ceSQu Wenruo 	/* No change in used bytes, can safely skip it. */
31017248e0ceSQu Wenruo 	if (cache->commit_used == used) {
31027248e0ceSQu Wenruo 		spin_unlock(&cache->lock);
31037248e0ceSQu Wenruo 		return 0;
31047248e0ceSQu Wenruo 	}
31057248e0ceSQu Wenruo 	cache->commit_used = used;
31067248e0ceSQu Wenruo 	spin_unlock(&cache->lock);
310777745c05SJosef Bacik 
3108b3470b5dSDavid Sterba 	key.objectid = cache->start;
3109b3470b5dSDavid Sterba 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3110b3470b5dSDavid Sterba 	key.offset = cache->length;
3111b3470b5dSDavid Sterba 
31123be4d8efSQu Wenruo 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
311377745c05SJosef Bacik 	if (ret) {
311477745c05SJosef Bacik 		if (ret > 0)
311577745c05SJosef Bacik 			ret = -ENOENT;
311677745c05SJosef Bacik 		goto fail;
311777745c05SJosef Bacik 	}
311877745c05SJosef Bacik 
311977745c05SJosef Bacik 	leaf = path->nodes[0];
312077745c05SJosef Bacik 	bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
31217248e0ceSQu Wenruo 	btrfs_set_stack_block_group_used(&bgi, used);
3122de0dc456SDavid Sterba 	btrfs_set_stack_block_group_chunk_objectid(&bgi,
3123f7238e50SJosef Bacik 						   cache->global_root_id);
3124de0dc456SDavid Sterba 	btrfs_set_stack_block_group_flags(&bgi, cache->flags);
3125bf38be65SDavid Sterba 	write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
312650564b65SFilipe Manana 	btrfs_mark_buffer_dirty(trans, leaf);
312777745c05SJosef Bacik fail:
312877745c05SJosef Bacik 	btrfs_release_path(path);
31292d6cd791SFilipe Manana 	/*
31302d6cd791SFilipe Manana 	 * We didn't update the block group item, need to revert commit_used
31312d6cd791SFilipe Manana 	 * unless the block group item didn't exist yet - this is to prevent a
31322d6cd791SFilipe Manana 	 * race with a concurrent insertion of the block group item, with
31332d6cd791SFilipe Manana 	 * insert_block_group_item(), that happened just after we attempted to
31342d6cd791SFilipe Manana 	 * update. In that case we would reset commit_used to 0 just after the
31352d6cd791SFilipe Manana 	 * insertion set it to a value greater than 0 - if the block group later
31362d6cd791SFilipe Manana 	 * becomes with 0 used bytes, we would incorrectly skip its update.
31372d6cd791SFilipe Manana 	 */
31382d6cd791SFilipe Manana 	if (ret < 0 && ret != -ENOENT) {
31397248e0ceSQu Wenruo 		spin_lock(&cache->lock);
31407248e0ceSQu Wenruo 		cache->commit_used = old_commit_used;
31417248e0ceSQu Wenruo 		spin_unlock(&cache->lock);
31427248e0ceSQu Wenruo 	}
314377745c05SJosef Bacik 	return ret;
314477745c05SJosef Bacik 
314577745c05SJosef Bacik }
314677745c05SJosef Bacik 
cache_save_setup(struct btrfs_block_group * block_group,struct btrfs_trans_handle * trans,struct btrfs_path * path)314732da5386SDavid Sterba static int cache_save_setup(struct btrfs_block_group *block_group,
314877745c05SJosef Bacik 			    struct btrfs_trans_handle *trans,
314977745c05SJosef Bacik 			    struct btrfs_path *path)
315077745c05SJosef Bacik {
315177745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
315277745c05SJosef Bacik 	struct inode *inode = NULL;
315377745c05SJosef Bacik 	struct extent_changeset *data_reserved = NULL;
315477745c05SJosef Bacik 	u64 alloc_hint = 0;
315577745c05SJosef Bacik 	int dcs = BTRFS_DC_ERROR;
31560044ae11SQu Wenruo 	u64 cache_size = 0;
315777745c05SJosef Bacik 	int retries = 0;
315877745c05SJosef Bacik 	int ret = 0;
315977745c05SJosef Bacik 
3160af456a2cSBoris Burkov 	if (!btrfs_test_opt(fs_info, SPACE_CACHE))
3161af456a2cSBoris Burkov 		return 0;
3162af456a2cSBoris Burkov 
316377745c05SJosef Bacik 	/*
316477745c05SJosef Bacik 	 * If this block group is smaller than 100 megs don't bother caching the
316577745c05SJosef Bacik 	 * block group.
316677745c05SJosef Bacik 	 */
3167b3470b5dSDavid Sterba 	if (block_group->length < (100 * SZ_1M)) {
316877745c05SJosef Bacik 		spin_lock(&block_group->lock);
316977745c05SJosef Bacik 		block_group->disk_cache_state = BTRFS_DC_WRITTEN;
317077745c05SJosef Bacik 		spin_unlock(&block_group->lock);
317177745c05SJosef Bacik 		return 0;
317277745c05SJosef Bacik 	}
317377745c05SJosef Bacik 
3174bf31f87fSDavid Sterba 	if (TRANS_ABORTED(trans))
317577745c05SJosef Bacik 		return 0;
317677745c05SJosef Bacik again:
317777745c05SJosef Bacik 	inode = lookup_free_space_inode(block_group, path);
317877745c05SJosef Bacik 	if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
317977745c05SJosef Bacik 		ret = PTR_ERR(inode);
318077745c05SJosef Bacik 		btrfs_release_path(path);
318177745c05SJosef Bacik 		goto out;
318277745c05SJosef Bacik 	}
318377745c05SJosef Bacik 
318477745c05SJosef Bacik 	if (IS_ERR(inode)) {
318577745c05SJosef Bacik 		BUG_ON(retries);
318677745c05SJosef Bacik 		retries++;
318777745c05SJosef Bacik 
318877745c05SJosef Bacik 		if (block_group->ro)
318977745c05SJosef Bacik 			goto out_free;
319077745c05SJosef Bacik 
319177745c05SJosef Bacik 		ret = create_free_space_inode(trans, block_group, path);
319277745c05SJosef Bacik 		if (ret)
319377745c05SJosef Bacik 			goto out_free;
319477745c05SJosef Bacik 		goto again;
319577745c05SJosef Bacik 	}
319677745c05SJosef Bacik 
319777745c05SJosef Bacik 	/*
319877745c05SJosef Bacik 	 * We want to set the generation to 0, that way if anything goes wrong
319977745c05SJosef Bacik 	 * from here on out we know not to trust this cache when we load up next
320077745c05SJosef Bacik 	 * time.
320177745c05SJosef Bacik 	 */
320277745c05SJosef Bacik 	BTRFS_I(inode)->generation = 0;
32038b9d0322SFilipe Manana 	ret = btrfs_update_inode(trans, BTRFS_I(inode));
320477745c05SJosef Bacik 	if (ret) {
320577745c05SJosef Bacik 		/*
320677745c05SJosef Bacik 		 * So theoretically we could recover from this, simply set the
320777745c05SJosef Bacik 		 * super cache generation to 0 so we know to invalidate the
320877745c05SJosef Bacik 		 * cache, but then we'd have to keep track of the block groups
320977745c05SJosef Bacik 		 * that fail this way so we know we _have_ to reset this cache
321077745c05SJosef Bacik 		 * before the next commit or risk reading stale cache.  So to
321177745c05SJosef Bacik 		 * limit our exposure to horrible edge cases lets just abort the
321277745c05SJosef Bacik 		 * transaction, this only happens in really bad situations
321377745c05SJosef Bacik 		 * anyway.
321477745c05SJosef Bacik 		 */
321577745c05SJosef Bacik 		btrfs_abort_transaction(trans, ret);
321677745c05SJosef Bacik 		goto out_put;
321777745c05SJosef Bacik 	}
321877745c05SJosef Bacik 	WARN_ON(ret);
321977745c05SJosef Bacik 
322077745c05SJosef Bacik 	/* We've already setup this transaction, go ahead and exit */
322177745c05SJosef Bacik 	if (block_group->cache_generation == trans->transid &&
322277745c05SJosef Bacik 	    i_size_read(inode)) {
322377745c05SJosef Bacik 		dcs = BTRFS_DC_SETUP;
322477745c05SJosef Bacik 		goto out_put;
322577745c05SJosef Bacik 	}
322677745c05SJosef Bacik 
322777745c05SJosef Bacik 	if (i_size_read(inode) > 0) {
322877745c05SJosef Bacik 		ret = btrfs_check_trunc_cache_free_space(fs_info,
322977745c05SJosef Bacik 					&fs_info->global_block_rsv);
323077745c05SJosef Bacik 		if (ret)
323177745c05SJosef Bacik 			goto out_put;
323277745c05SJosef Bacik 
323377745c05SJosef Bacik 		ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
323477745c05SJosef Bacik 		if (ret)
323577745c05SJosef Bacik 			goto out_put;
323677745c05SJosef Bacik 	}
323777745c05SJosef Bacik 
323877745c05SJosef Bacik 	spin_lock(&block_group->lock);
323977745c05SJosef Bacik 	if (block_group->cached != BTRFS_CACHE_FINISHED ||
324077745c05SJosef Bacik 	    !btrfs_test_opt(fs_info, SPACE_CACHE)) {
324177745c05SJosef Bacik 		/*
324277745c05SJosef Bacik 		 * don't bother trying to write stuff out _if_
324377745c05SJosef Bacik 		 * a) we're not cached,
324477745c05SJosef Bacik 		 * b) we're with nospace_cache mount option,
324577745c05SJosef Bacik 		 * c) we're with v2 space_cache (FREE_SPACE_TREE).
324677745c05SJosef Bacik 		 */
324777745c05SJosef Bacik 		dcs = BTRFS_DC_WRITTEN;
324877745c05SJosef Bacik 		spin_unlock(&block_group->lock);
324977745c05SJosef Bacik 		goto out_put;
325077745c05SJosef Bacik 	}
325177745c05SJosef Bacik 	spin_unlock(&block_group->lock);
325277745c05SJosef Bacik 
325377745c05SJosef Bacik 	/*
325477745c05SJosef Bacik 	 * We hit an ENOSPC when setting up the cache in this transaction, just
325577745c05SJosef Bacik 	 * skip doing the setup, we've already cleared the cache so we're safe.
325677745c05SJosef Bacik 	 */
325777745c05SJosef Bacik 	if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
325877745c05SJosef Bacik 		ret = -ENOSPC;
325977745c05SJosef Bacik 		goto out_put;
326077745c05SJosef Bacik 	}
326177745c05SJosef Bacik 
326277745c05SJosef Bacik 	/*
326377745c05SJosef Bacik 	 * Try to preallocate enough space based on how big the block group is.
326477745c05SJosef Bacik 	 * Keep in mind this has to include any pinned space which could end up
326577745c05SJosef Bacik 	 * taking up quite a bit since it's not folded into the other space
326677745c05SJosef Bacik 	 * cache.
326777745c05SJosef Bacik 	 */
32680044ae11SQu Wenruo 	cache_size = div_u64(block_group->length, SZ_256M);
32690044ae11SQu Wenruo 	if (!cache_size)
32700044ae11SQu Wenruo 		cache_size = 1;
327177745c05SJosef Bacik 
32720044ae11SQu Wenruo 	cache_size *= 16;
32730044ae11SQu Wenruo 	cache_size *= fs_info->sectorsize;
327477745c05SJosef Bacik 
327536ea6f3eSNikolay Borisov 	ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, 0,
32761daedb1dSJosef Bacik 					  cache_size, false);
327777745c05SJosef Bacik 	if (ret)
327877745c05SJosef Bacik 		goto out_put;
327977745c05SJosef Bacik 
32800044ae11SQu Wenruo 	ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, cache_size,
32810044ae11SQu Wenruo 					      cache_size, cache_size,
328277745c05SJosef Bacik 					      &alloc_hint);
328377745c05SJosef Bacik 	/*
328477745c05SJosef Bacik 	 * Our cache requires contiguous chunks so that we don't modify a bunch
328577745c05SJosef Bacik 	 * of metadata or split extents when writing the cache out, which means
328677745c05SJosef Bacik 	 * we can enospc if we are heavily fragmented in addition to just normal
328777745c05SJosef Bacik 	 * out of space conditions.  So if we hit this just skip setting up any
328877745c05SJosef Bacik 	 * other block groups for this transaction, maybe we'll unpin enough
328977745c05SJosef Bacik 	 * space the next time around.
329077745c05SJosef Bacik 	 */
329177745c05SJosef Bacik 	if (!ret)
329277745c05SJosef Bacik 		dcs = BTRFS_DC_SETUP;
329377745c05SJosef Bacik 	else if (ret == -ENOSPC)
329477745c05SJosef Bacik 		set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
329577745c05SJosef Bacik 
329677745c05SJosef Bacik out_put:
329777745c05SJosef Bacik 	iput(inode);
329877745c05SJosef Bacik out_free:
329977745c05SJosef Bacik 	btrfs_release_path(path);
330077745c05SJosef Bacik out:
330177745c05SJosef Bacik 	spin_lock(&block_group->lock);
330277745c05SJosef Bacik 	if (!ret && dcs == BTRFS_DC_SETUP)
330377745c05SJosef Bacik 		block_group->cache_generation = trans->transid;
330477745c05SJosef Bacik 	block_group->disk_cache_state = dcs;
330577745c05SJosef Bacik 	spin_unlock(&block_group->lock);
330677745c05SJosef Bacik 
330777745c05SJosef Bacik 	extent_changeset_free(data_reserved);
330877745c05SJosef Bacik 	return ret;
330977745c05SJosef Bacik }
331077745c05SJosef Bacik 
btrfs_setup_space_cache(struct btrfs_trans_handle * trans)331177745c05SJosef Bacik int btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
331277745c05SJosef Bacik {
331377745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
331432da5386SDavid Sterba 	struct btrfs_block_group *cache, *tmp;
331577745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
331677745c05SJosef Bacik 	struct btrfs_path *path;
331777745c05SJosef Bacik 
331877745c05SJosef Bacik 	if (list_empty(&cur_trans->dirty_bgs) ||
331977745c05SJosef Bacik 	    !btrfs_test_opt(fs_info, SPACE_CACHE))
332077745c05SJosef Bacik 		return 0;
332177745c05SJosef Bacik 
332277745c05SJosef Bacik 	path = btrfs_alloc_path();
332377745c05SJosef Bacik 	if (!path)
332477745c05SJosef Bacik 		return -ENOMEM;
332577745c05SJosef Bacik 
332677745c05SJosef Bacik 	/* Could add new block groups, use _safe just in case */
332777745c05SJosef Bacik 	list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
332877745c05SJosef Bacik 				 dirty_list) {
332977745c05SJosef Bacik 		if (cache->disk_cache_state == BTRFS_DC_CLEAR)
333077745c05SJosef Bacik 			cache_save_setup(cache, trans, path);
333177745c05SJosef Bacik 	}
333277745c05SJosef Bacik 
333377745c05SJosef Bacik 	btrfs_free_path(path);
333477745c05SJosef Bacik 	return 0;
333577745c05SJosef Bacik }
333677745c05SJosef Bacik 
333777745c05SJosef Bacik /*
333877745c05SJosef Bacik  * Transaction commit does final block group cache writeback during a critical
333977745c05SJosef Bacik  * section where nothing is allowed to change the FS.  This is required in
334077745c05SJosef Bacik  * order for the cache to actually match the block group, but can introduce a
334177745c05SJosef Bacik  * lot of latency into the commit.
334277745c05SJosef Bacik  *
334377745c05SJosef Bacik  * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
334477745c05SJosef Bacik  * There's a chance we'll have to redo some of it if the block group changes
334577745c05SJosef Bacik  * again during the commit, but it greatly reduces the commit latency by
334677745c05SJosef Bacik  * getting rid of the easy block groups while we're still allowing others to
334777745c05SJosef Bacik  * join the commit.
334877745c05SJosef Bacik  */
btrfs_start_dirty_block_groups(struct btrfs_trans_handle * trans)334977745c05SJosef Bacik int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
335077745c05SJosef Bacik {
335177745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
335232da5386SDavid Sterba 	struct btrfs_block_group *cache;
335377745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
335477745c05SJosef Bacik 	int ret = 0;
335577745c05SJosef Bacik 	int should_put;
335677745c05SJosef Bacik 	struct btrfs_path *path = NULL;
335777745c05SJosef Bacik 	LIST_HEAD(dirty);
335877745c05SJosef Bacik 	struct list_head *io = &cur_trans->io_bgs;
335977745c05SJosef Bacik 	int loops = 0;
336077745c05SJosef Bacik 
336177745c05SJosef Bacik 	spin_lock(&cur_trans->dirty_bgs_lock);
336277745c05SJosef Bacik 	if (list_empty(&cur_trans->dirty_bgs)) {
336377745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
336477745c05SJosef Bacik 		return 0;
336577745c05SJosef Bacik 	}
336677745c05SJosef Bacik 	list_splice_init(&cur_trans->dirty_bgs, &dirty);
336777745c05SJosef Bacik 	spin_unlock(&cur_trans->dirty_bgs_lock);
336877745c05SJosef Bacik 
336977745c05SJosef Bacik again:
337077745c05SJosef Bacik 	/* Make sure all the block groups on our dirty list actually exist */
337177745c05SJosef Bacik 	btrfs_create_pending_block_groups(trans);
337277745c05SJosef Bacik 
337377745c05SJosef Bacik 	if (!path) {
337477745c05SJosef Bacik 		path = btrfs_alloc_path();
3375938fcbfbSJosef Bacik 		if (!path) {
3376938fcbfbSJosef Bacik 			ret = -ENOMEM;
3377938fcbfbSJosef Bacik 			goto out;
3378938fcbfbSJosef Bacik 		}
337977745c05SJosef Bacik 	}
338077745c05SJosef Bacik 
338177745c05SJosef Bacik 	/*
338277745c05SJosef Bacik 	 * cache_write_mutex is here only to save us from balance or automatic
338377745c05SJosef Bacik 	 * removal of empty block groups deleting this block group while we are
338477745c05SJosef Bacik 	 * writing out the cache
338577745c05SJosef Bacik 	 */
338677745c05SJosef Bacik 	mutex_lock(&trans->transaction->cache_write_mutex);
338777745c05SJosef Bacik 	while (!list_empty(&dirty)) {
338877745c05SJosef Bacik 		bool drop_reserve = true;
338977745c05SJosef Bacik 
339032da5386SDavid Sterba 		cache = list_first_entry(&dirty, struct btrfs_block_group,
339177745c05SJosef Bacik 					 dirty_list);
339277745c05SJosef Bacik 		/*
339377745c05SJosef Bacik 		 * This can happen if something re-dirties a block group that
339477745c05SJosef Bacik 		 * is already under IO.  Just wait for it to finish and then do
339577745c05SJosef Bacik 		 * it all again
339677745c05SJosef Bacik 		 */
339777745c05SJosef Bacik 		if (!list_empty(&cache->io_list)) {
339877745c05SJosef Bacik 			list_del_init(&cache->io_list);
339977745c05SJosef Bacik 			btrfs_wait_cache_io(trans, cache, path);
340077745c05SJosef Bacik 			btrfs_put_block_group(cache);
340177745c05SJosef Bacik 		}
340277745c05SJosef Bacik 
340377745c05SJosef Bacik 
340477745c05SJosef Bacik 		/*
340577745c05SJosef Bacik 		 * btrfs_wait_cache_io uses the cache->dirty_list to decide if
340677745c05SJosef Bacik 		 * it should update the cache_state.  Don't delete until after
340777745c05SJosef Bacik 		 * we wait.
340877745c05SJosef Bacik 		 *
340977745c05SJosef Bacik 		 * Since we're not running in the commit critical section
341077745c05SJosef Bacik 		 * we need the dirty_bgs_lock to protect from update_block_group
341177745c05SJosef Bacik 		 */
341277745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
341377745c05SJosef Bacik 		list_del_init(&cache->dirty_list);
341477745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
341577745c05SJosef Bacik 
341677745c05SJosef Bacik 		should_put = 1;
341777745c05SJosef Bacik 
341877745c05SJosef Bacik 		cache_save_setup(cache, trans, path);
341977745c05SJosef Bacik 
342077745c05SJosef Bacik 		if (cache->disk_cache_state == BTRFS_DC_SETUP) {
342177745c05SJosef Bacik 			cache->io_ctl.inode = NULL;
342277745c05SJosef Bacik 			ret = btrfs_write_out_cache(trans, cache, path);
342377745c05SJosef Bacik 			if (ret == 0 && cache->io_ctl.inode) {
342477745c05SJosef Bacik 				should_put = 0;
342577745c05SJosef Bacik 
342677745c05SJosef Bacik 				/*
342777745c05SJosef Bacik 				 * The cache_write_mutex is protecting the
342877745c05SJosef Bacik 				 * io_list, also refer to the definition of
342977745c05SJosef Bacik 				 * btrfs_transaction::io_bgs for more details
343077745c05SJosef Bacik 				 */
343177745c05SJosef Bacik 				list_add_tail(&cache->io_list, io);
343277745c05SJosef Bacik 			} else {
343377745c05SJosef Bacik 				/*
343477745c05SJosef Bacik 				 * If we failed to write the cache, the
343577745c05SJosef Bacik 				 * generation will be bad and life goes on
343677745c05SJosef Bacik 				 */
343777745c05SJosef Bacik 				ret = 0;
343877745c05SJosef Bacik 			}
343977745c05SJosef Bacik 		}
344077745c05SJosef Bacik 		if (!ret) {
34413be4d8efSQu Wenruo 			ret = update_block_group_item(trans, path, cache);
344277745c05SJosef Bacik 			/*
344377745c05SJosef Bacik 			 * Our block group might still be attached to the list
344477745c05SJosef Bacik 			 * of new block groups in the transaction handle of some
344577745c05SJosef Bacik 			 * other task (struct btrfs_trans_handle->new_bgs). This
344677745c05SJosef Bacik 			 * means its block group item isn't yet in the extent
344777745c05SJosef Bacik 			 * tree. If this happens ignore the error, as we will
344877745c05SJosef Bacik 			 * try again later in the critical section of the
344977745c05SJosef Bacik 			 * transaction commit.
345077745c05SJosef Bacik 			 */
345177745c05SJosef Bacik 			if (ret == -ENOENT) {
345277745c05SJosef Bacik 				ret = 0;
345377745c05SJosef Bacik 				spin_lock(&cur_trans->dirty_bgs_lock);
345477745c05SJosef Bacik 				if (list_empty(&cache->dirty_list)) {
345577745c05SJosef Bacik 					list_add_tail(&cache->dirty_list,
345677745c05SJosef Bacik 						      &cur_trans->dirty_bgs);
345777745c05SJosef Bacik 					btrfs_get_block_group(cache);
345877745c05SJosef Bacik 					drop_reserve = false;
345977745c05SJosef Bacik 				}
346077745c05SJosef Bacik 				spin_unlock(&cur_trans->dirty_bgs_lock);
346177745c05SJosef Bacik 			} else if (ret) {
346277745c05SJosef Bacik 				btrfs_abort_transaction(trans, ret);
346377745c05SJosef Bacik 			}
346477745c05SJosef Bacik 		}
346577745c05SJosef Bacik 
346677745c05SJosef Bacik 		/* If it's not on the io list, we need to put the block group */
346777745c05SJosef Bacik 		if (should_put)
346877745c05SJosef Bacik 			btrfs_put_block_group(cache);
346977745c05SJosef Bacik 		if (drop_reserve)
3470f66e0209SFilipe Manana 			btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
347177745c05SJosef Bacik 		/*
347277745c05SJosef Bacik 		 * Avoid blocking other tasks for too long. It might even save
347377745c05SJosef Bacik 		 * us from writing caches for block groups that are going to be
347477745c05SJosef Bacik 		 * removed.
347577745c05SJosef Bacik 		 */
347677745c05SJosef Bacik 		mutex_unlock(&trans->transaction->cache_write_mutex);
3477938fcbfbSJosef Bacik 		if (ret)
3478938fcbfbSJosef Bacik 			goto out;
347977745c05SJosef Bacik 		mutex_lock(&trans->transaction->cache_write_mutex);
348077745c05SJosef Bacik 	}
348177745c05SJosef Bacik 	mutex_unlock(&trans->transaction->cache_write_mutex);
348277745c05SJosef Bacik 
348377745c05SJosef Bacik 	/*
348477745c05SJosef Bacik 	 * Go through delayed refs for all the stuff we've just kicked off
348577745c05SJosef Bacik 	 * and then loop back (just once)
348677745c05SJosef Bacik 	 */
348734d1eb0eSJosef Bacik 	if (!ret)
348877745c05SJosef Bacik 		ret = btrfs_run_delayed_refs(trans, 0);
348977745c05SJosef Bacik 	if (!ret && loops == 0) {
349077745c05SJosef Bacik 		loops++;
349177745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
349277745c05SJosef Bacik 		list_splice_init(&cur_trans->dirty_bgs, &dirty);
349377745c05SJosef Bacik 		/*
349477745c05SJosef Bacik 		 * dirty_bgs_lock protects us from concurrent block group
349577745c05SJosef Bacik 		 * deletes too (not just cache_write_mutex).
349677745c05SJosef Bacik 		 */
349777745c05SJosef Bacik 		if (!list_empty(&dirty)) {
349877745c05SJosef Bacik 			spin_unlock(&cur_trans->dirty_bgs_lock);
349977745c05SJosef Bacik 			goto again;
350077745c05SJosef Bacik 		}
350177745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
3502938fcbfbSJosef Bacik 	}
3503938fcbfbSJosef Bacik out:
3504938fcbfbSJosef Bacik 	if (ret < 0) {
3505938fcbfbSJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
3506938fcbfbSJosef Bacik 		list_splice_init(&dirty, &cur_trans->dirty_bgs);
3507938fcbfbSJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
350877745c05SJosef Bacik 		btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
350977745c05SJosef Bacik 	}
351077745c05SJosef Bacik 
351177745c05SJosef Bacik 	btrfs_free_path(path);
351277745c05SJosef Bacik 	return ret;
351377745c05SJosef Bacik }
351477745c05SJosef Bacik 
btrfs_write_dirty_block_groups(struct btrfs_trans_handle * trans)351577745c05SJosef Bacik int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
351677745c05SJosef Bacik {
351777745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
351832da5386SDavid Sterba 	struct btrfs_block_group *cache;
351977745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
352077745c05SJosef Bacik 	int ret = 0;
352177745c05SJosef Bacik 	int should_put;
352277745c05SJosef Bacik 	struct btrfs_path *path;
352377745c05SJosef Bacik 	struct list_head *io = &cur_trans->io_bgs;
352477745c05SJosef Bacik 
352577745c05SJosef Bacik 	path = btrfs_alloc_path();
352677745c05SJosef Bacik 	if (!path)
352777745c05SJosef Bacik 		return -ENOMEM;
352877745c05SJosef Bacik 
352977745c05SJosef Bacik 	/*
353077745c05SJosef Bacik 	 * Even though we are in the critical section of the transaction commit,
353177745c05SJosef Bacik 	 * we can still have concurrent tasks adding elements to this
353277745c05SJosef Bacik 	 * transaction's list of dirty block groups. These tasks correspond to
353377745c05SJosef Bacik 	 * endio free space workers started when writeback finishes for a
353477745c05SJosef Bacik 	 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
353577745c05SJosef Bacik 	 * allocate new block groups as a result of COWing nodes of the root
353677745c05SJosef Bacik 	 * tree when updating the free space inode. The writeback for the space
353777745c05SJosef Bacik 	 * caches is triggered by an earlier call to
353877745c05SJosef Bacik 	 * btrfs_start_dirty_block_groups() and iterations of the following
353977745c05SJosef Bacik 	 * loop.
354077745c05SJosef Bacik 	 * Also we want to do the cache_save_setup first and then run the
354177745c05SJosef Bacik 	 * delayed refs to make sure we have the best chance at doing this all
354277745c05SJosef Bacik 	 * in one shot.
354377745c05SJosef Bacik 	 */
354477745c05SJosef Bacik 	spin_lock(&cur_trans->dirty_bgs_lock);
354577745c05SJosef Bacik 	while (!list_empty(&cur_trans->dirty_bgs)) {
354677745c05SJosef Bacik 		cache = list_first_entry(&cur_trans->dirty_bgs,
354732da5386SDavid Sterba 					 struct btrfs_block_group,
354877745c05SJosef Bacik 					 dirty_list);
354977745c05SJosef Bacik 
355077745c05SJosef Bacik 		/*
355177745c05SJosef Bacik 		 * This can happen if cache_save_setup re-dirties a block group
355277745c05SJosef Bacik 		 * that is already under IO.  Just wait for it to finish and
355377745c05SJosef Bacik 		 * then do it all again
355477745c05SJosef Bacik 		 */
355577745c05SJosef Bacik 		if (!list_empty(&cache->io_list)) {
355677745c05SJosef Bacik 			spin_unlock(&cur_trans->dirty_bgs_lock);
355777745c05SJosef Bacik 			list_del_init(&cache->io_list);
355877745c05SJosef Bacik 			btrfs_wait_cache_io(trans, cache, path);
355977745c05SJosef Bacik 			btrfs_put_block_group(cache);
356077745c05SJosef Bacik 			spin_lock(&cur_trans->dirty_bgs_lock);
356177745c05SJosef Bacik 		}
356277745c05SJosef Bacik 
356377745c05SJosef Bacik 		/*
356477745c05SJosef Bacik 		 * Don't remove from the dirty list until after we've waited on
356577745c05SJosef Bacik 		 * any pending IO
356677745c05SJosef Bacik 		 */
356777745c05SJosef Bacik 		list_del_init(&cache->dirty_list);
356877745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
356977745c05SJosef Bacik 		should_put = 1;
357077745c05SJosef Bacik 
357177745c05SJosef Bacik 		cache_save_setup(cache, trans, path);
357277745c05SJosef Bacik 
357377745c05SJosef Bacik 		if (!ret)
35748a526c44SFilipe Manana 			ret = btrfs_run_delayed_refs(trans, U64_MAX);
357577745c05SJosef Bacik 
357677745c05SJosef Bacik 		if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
357777745c05SJosef Bacik 			cache->io_ctl.inode = NULL;
357877745c05SJosef Bacik 			ret = btrfs_write_out_cache(trans, cache, path);
357977745c05SJosef Bacik 			if (ret == 0 && cache->io_ctl.inode) {
358077745c05SJosef Bacik 				should_put = 0;
358177745c05SJosef Bacik 				list_add_tail(&cache->io_list, io);
358277745c05SJosef Bacik 			} else {
358377745c05SJosef Bacik 				/*
358477745c05SJosef Bacik 				 * If we failed to write the cache, the
358577745c05SJosef Bacik 				 * generation will be bad and life goes on
358677745c05SJosef Bacik 				 */
358777745c05SJosef Bacik 				ret = 0;
358877745c05SJosef Bacik 			}
358977745c05SJosef Bacik 		}
359077745c05SJosef Bacik 		if (!ret) {
35913be4d8efSQu Wenruo 			ret = update_block_group_item(trans, path, cache);
359277745c05SJosef Bacik 			/*
359377745c05SJosef Bacik 			 * One of the free space endio workers might have
359477745c05SJosef Bacik 			 * created a new block group while updating a free space
359577745c05SJosef Bacik 			 * cache's inode (at inode.c:btrfs_finish_ordered_io())
359677745c05SJosef Bacik 			 * and hasn't released its transaction handle yet, in
359777745c05SJosef Bacik 			 * which case the new block group is still attached to
359877745c05SJosef Bacik 			 * its transaction handle and its creation has not
359977745c05SJosef Bacik 			 * finished yet (no block group item in the extent tree
360077745c05SJosef Bacik 			 * yet, etc). If this is the case, wait for all free
360177745c05SJosef Bacik 			 * space endio workers to finish and retry. This is a
3602260db43cSRandy Dunlap 			 * very rare case so no need for a more efficient and
360377745c05SJosef Bacik 			 * complex approach.
360477745c05SJosef Bacik 			 */
360577745c05SJosef Bacik 			if (ret == -ENOENT) {
360677745c05SJosef Bacik 				wait_event(cur_trans->writer_wait,
360777745c05SJosef Bacik 				   atomic_read(&cur_trans->num_writers) == 1);
36083be4d8efSQu Wenruo 				ret = update_block_group_item(trans, path, cache);
360977745c05SJosef Bacik 			}
361077745c05SJosef Bacik 			if (ret)
361177745c05SJosef Bacik 				btrfs_abort_transaction(trans, ret);
361277745c05SJosef Bacik 		}
361377745c05SJosef Bacik 
361477745c05SJosef Bacik 		/* If its not on the io list, we need to put the block group */
361577745c05SJosef Bacik 		if (should_put)
361677745c05SJosef Bacik 			btrfs_put_block_group(cache);
3617f66e0209SFilipe Manana 		btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
361877745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
361977745c05SJosef Bacik 	}
362077745c05SJosef Bacik 	spin_unlock(&cur_trans->dirty_bgs_lock);
362177745c05SJosef Bacik 
362277745c05SJosef Bacik 	/*
362377745c05SJosef Bacik 	 * Refer to the definition of io_bgs member for details why it's safe
362477745c05SJosef Bacik 	 * to use it without any locking
362577745c05SJosef Bacik 	 */
362677745c05SJosef Bacik 	while (!list_empty(io)) {
362732da5386SDavid Sterba 		cache = list_first_entry(io, struct btrfs_block_group,
362877745c05SJosef Bacik 					 io_list);
362977745c05SJosef Bacik 		list_del_init(&cache->io_list);
363077745c05SJosef Bacik 		btrfs_wait_cache_io(trans, cache, path);
363177745c05SJosef Bacik 		btrfs_put_block_group(cache);
363277745c05SJosef Bacik 	}
363377745c05SJosef Bacik 
363477745c05SJosef Bacik 	btrfs_free_path(path);
363577745c05SJosef Bacik 	return ret;
363677745c05SJosef Bacik }
3637606d1bf1SJosef Bacik 
btrfs_update_block_group(struct btrfs_trans_handle * trans,u64 bytenr,u64 num_bytes,bool alloc)3638606d1bf1SJosef Bacik int btrfs_update_block_group(struct btrfs_trans_handle *trans,
363911b66fa6SAnand Jain 			     u64 bytenr, u64 num_bytes, bool alloc)
3640606d1bf1SJosef Bacik {
3641606d1bf1SJosef Bacik 	struct btrfs_fs_info *info = trans->fs_info;
36424d20c1deSFilipe Manana 	struct btrfs_space_info *space_info;
36434d20c1deSFilipe Manana 	struct btrfs_block_group *cache;
3644606d1bf1SJosef Bacik 	u64 old_val;
36454d20c1deSFilipe Manana 	bool reclaim = false;
3646f66e0209SFilipe Manana 	bool bg_already_dirty = true;
3647606d1bf1SJosef Bacik 	int factor;
3648606d1bf1SJosef Bacik 
3649606d1bf1SJosef Bacik 	/* Block accounting for super block */
3650606d1bf1SJosef Bacik 	spin_lock(&info->delalloc_root_lock);
3651606d1bf1SJosef Bacik 	old_val = btrfs_super_bytes_used(info->super_copy);
3652606d1bf1SJosef Bacik 	if (alloc)
3653606d1bf1SJosef Bacik 		old_val += num_bytes;
3654606d1bf1SJosef Bacik 	else
3655606d1bf1SJosef Bacik 		old_val -= num_bytes;
3656606d1bf1SJosef Bacik 	btrfs_set_super_bytes_used(info->super_copy, old_val);
3657606d1bf1SJosef Bacik 	spin_unlock(&info->delalloc_root_lock);
3658606d1bf1SJosef Bacik 
3659606d1bf1SJosef Bacik 	cache = btrfs_lookup_block_group(info, bytenr);
36604d20c1deSFilipe Manana 	if (!cache)
36614d20c1deSFilipe Manana 		return -ENOENT;
36624d20c1deSFilipe Manana 
36634d20c1deSFilipe Manana 	/* An extent can not span multiple block groups. */
36644d20c1deSFilipe Manana 	ASSERT(bytenr + num_bytes <= cache->start + cache->length);
36654d20c1deSFilipe Manana 
3666df384da5SJosef Bacik 	space_info = cache->space_info;
3667606d1bf1SJosef Bacik 	factor = btrfs_bg_type_to_factor(cache->flags);
3668606d1bf1SJosef Bacik 
3669606d1bf1SJosef Bacik 	/*
36704d20c1deSFilipe Manana 	 * If this block group has free space cache written out, we need to make
36714d20c1deSFilipe Manana 	 * sure to load it if we are removing space.  This is because we need
36724d20c1deSFilipe Manana 	 * the unpinning stage to actually add the space back to the block group,
36734d20c1deSFilipe Manana 	 * otherwise we will leak space.
3674606d1bf1SJosef Bacik 	 */
367532da5386SDavid Sterba 	if (!alloc && !btrfs_block_group_done(cache))
3676ced8ecf0SOmar Sandoval 		btrfs_cache_block_group(cache, true);
3677606d1bf1SJosef Bacik 
3678df384da5SJosef Bacik 	spin_lock(&space_info->lock);
3679606d1bf1SJosef Bacik 	spin_lock(&cache->lock);
3680606d1bf1SJosef Bacik 
3681606d1bf1SJosef Bacik 	if (btrfs_test_opt(info, SPACE_CACHE) &&
3682606d1bf1SJosef Bacik 	    cache->disk_cache_state < BTRFS_DC_CLEAR)
3683606d1bf1SJosef Bacik 		cache->disk_cache_state = BTRFS_DC_CLEAR;
3684606d1bf1SJosef Bacik 
3685bf38be65SDavid Sterba 	old_val = cache->used;
3686606d1bf1SJosef Bacik 	if (alloc) {
3687606d1bf1SJosef Bacik 		old_val += num_bytes;
3688bf38be65SDavid Sterba 		cache->used = old_val;
3689606d1bf1SJosef Bacik 		cache->reserved -= num_bytes;
3690e4ca3932SBoris Burkov 		cache->reclaim_mark = 0;
3691df384da5SJosef Bacik 		space_info->bytes_reserved -= num_bytes;
3692df384da5SJosef Bacik 		space_info->bytes_used += num_bytes;
3693df384da5SJosef Bacik 		space_info->disk_used += num_bytes * factor;
3694813d4c64SBoris Burkov 		if (READ_ONCE(space_info->periodic_reclaim))
3695813d4c64SBoris Burkov 			btrfs_space_info_update_reclaimable(space_info, -num_bytes);
3696606d1bf1SJosef Bacik 		spin_unlock(&cache->lock);
3697df384da5SJosef Bacik 		spin_unlock(&space_info->lock);
3698606d1bf1SJosef Bacik 	} else {
3699606d1bf1SJosef Bacik 		old_val -= num_bytes;
3700bf38be65SDavid Sterba 		cache->used = old_val;
3701606d1bf1SJosef Bacik 		cache->pinned += num_bytes;
37024d20c1deSFilipe Manana 		btrfs_space_info_update_bytes_pinned(info, space_info, num_bytes);
3703df384da5SJosef Bacik 		space_info->bytes_used -= num_bytes;
3704df384da5SJosef Bacik 		space_info->disk_used -= num_bytes * factor;
3705813d4c64SBoris Burkov 		if (READ_ONCE(space_info->periodic_reclaim))
3706813d4c64SBoris Burkov 			btrfs_space_info_update_reclaimable(space_info, num_bytes);
3707813d4c64SBoris Burkov 		else
3708ac2f1e63SJosef Bacik 			reclaim = should_reclaim_block_group(cache, num_bytes);
370952bb7a21SBoris Burkov 
3710606d1bf1SJosef Bacik 		spin_unlock(&cache->lock);
3711df384da5SJosef Bacik 		spin_unlock(&space_info->lock);
3712606d1bf1SJosef Bacik 
37134d20c1deSFilipe Manana 		set_extent_bit(&trans->transaction->pinned_extents, bytenr,
37144d20c1deSFilipe Manana 			       bytenr + num_bytes - 1, EXTENT_DIRTY, NULL);
3715606d1bf1SJosef Bacik 	}
3716606d1bf1SJosef Bacik 
3717606d1bf1SJosef Bacik 	spin_lock(&trans->transaction->dirty_bgs_lock);
3718606d1bf1SJosef Bacik 	if (list_empty(&cache->dirty_list)) {
37194d20c1deSFilipe Manana 		list_add_tail(&cache->dirty_list, &trans->transaction->dirty_bgs);
3720f66e0209SFilipe Manana 		bg_already_dirty = false;
3721606d1bf1SJosef Bacik 		btrfs_get_block_group(cache);
3722606d1bf1SJosef Bacik 	}
3723606d1bf1SJosef Bacik 	spin_unlock(&trans->transaction->dirty_bgs_lock);
3724606d1bf1SJosef Bacik 
3725606d1bf1SJosef Bacik 	/*
37264d20c1deSFilipe Manana 	 * No longer have used bytes in this block group, queue it for deletion.
37274d20c1deSFilipe Manana 	 * We do this after adding the block group to the dirty list to avoid
37284d20c1deSFilipe Manana 	 * races between cleaner kthread and space cache writeout.
3729606d1bf1SJosef Bacik 	 */
37306e80d4f8SDennis Zhou 	if (!alloc && old_val == 0) {
37316e80d4f8SDennis Zhou 		if (!btrfs_test_opt(info, DISCARD_ASYNC))
3732606d1bf1SJosef Bacik 			btrfs_mark_bg_unused(cache);
3733ac2f1e63SJosef Bacik 	} else if (!alloc && reclaim) {
3734ac2f1e63SJosef Bacik 		btrfs_mark_bg_to_reclaim(cache);
37356e80d4f8SDennis Zhou 	}
3736606d1bf1SJosef Bacik 
3737606d1bf1SJosef Bacik 	btrfs_put_block_group(cache);
3738606d1bf1SJosef Bacik 
3739606d1bf1SJosef Bacik 	/* Modified block groups are accounted for in the delayed_refs_rsv. */
3740f66e0209SFilipe Manana 	if (!bg_already_dirty)
3741f66e0209SFilipe Manana 		btrfs_inc_delayed_refs_rsv_bg_updates(info);
37424d20c1deSFilipe Manana 
37434d20c1deSFilipe Manana 	return 0;
3744606d1bf1SJosef Bacik }
3745606d1bf1SJosef Bacik 
374643dd529aSDavid Sterba /*
374743dd529aSDavid Sterba  * Update the block_group and space info counters.
374843dd529aSDavid Sterba  *
3749606d1bf1SJosef Bacik  * @cache:	The cache we are manipulating
3750606d1bf1SJosef Bacik  * @ram_bytes:  The number of bytes of file content, and will be same to
3751606d1bf1SJosef Bacik  *              @num_bytes except for the compress path.
3752606d1bf1SJosef Bacik  * @num_bytes:	The number of bytes in question
3753606d1bf1SJosef Bacik  * @delalloc:   The blocks are allocated for the delalloc write
3754606d1bf1SJosef Bacik  *
3755606d1bf1SJosef Bacik  * This is called by the allocator when it reserves space. If this is a
3756606d1bf1SJosef Bacik  * reservation and the block group has become read only we cannot make the
3757606d1bf1SJosef Bacik  * reservation and return -EAGAIN, otherwise this function always succeeds.
3758606d1bf1SJosef Bacik  */
btrfs_add_reserved_bytes(struct btrfs_block_group * cache,u64 ram_bytes,u64 num_bytes,int delalloc,bool force_wrong_size_class)375932da5386SDavid Sterba int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
376052bb7a21SBoris Burkov 			     u64 ram_bytes, u64 num_bytes, int delalloc,
376152bb7a21SBoris Burkov 			     bool force_wrong_size_class)
3762606d1bf1SJosef Bacik {
3763606d1bf1SJosef Bacik 	struct btrfs_space_info *space_info = cache->space_info;
376452bb7a21SBoris Burkov 	enum btrfs_block_group_size_class size_class;
3765606d1bf1SJosef Bacik 	int ret = 0;
3766606d1bf1SJosef Bacik 
3767606d1bf1SJosef Bacik 	spin_lock(&space_info->lock);
3768606d1bf1SJosef Bacik 	spin_lock(&cache->lock);
3769606d1bf1SJosef Bacik 	if (cache->ro) {
3770606d1bf1SJosef Bacik 		ret = -EAGAIN;
377152bb7a21SBoris Burkov 		goto out;
377252bb7a21SBoris Burkov 	}
377352bb7a21SBoris Burkov 
3774cb0922f2SBoris Burkov 	if (btrfs_block_group_should_use_size_class(cache)) {
377552bb7a21SBoris Burkov 		size_class = btrfs_calc_block_group_size_class(num_bytes);
377652bb7a21SBoris Burkov 		ret = btrfs_use_block_group_size_class(cache, size_class, force_wrong_size_class);
377752bb7a21SBoris Burkov 		if (ret)
377852bb7a21SBoris Burkov 			goto out;
377952bb7a21SBoris Burkov 	}
3780606d1bf1SJosef Bacik 	cache->reserved += num_bytes;
3781606d1bf1SJosef Bacik 	space_info->bytes_reserved += num_bytes;
3782a43c3835SJosef Bacik 	trace_btrfs_space_reservation(cache->fs_info, "space_info",
3783a43c3835SJosef Bacik 				      space_info->flags, num_bytes, 1);
3784606d1bf1SJosef Bacik 	btrfs_space_info_update_bytes_may_use(cache->fs_info,
3785606d1bf1SJosef Bacik 					      space_info, -ram_bytes);
3786606d1bf1SJosef Bacik 	if (delalloc)
3787606d1bf1SJosef Bacik 		cache->delalloc_bytes += num_bytes;
378899ffb43eSJosef Bacik 
378999ffb43eSJosef Bacik 	/*
379052bb7a21SBoris Burkov 	 * Compression can use less space than we reserved, so wake tickets if
379152bb7a21SBoris Burkov 	 * that happens.
379299ffb43eSJosef Bacik 	 */
379399ffb43eSJosef Bacik 	if (num_bytes < ram_bytes)
379499ffb43eSJosef Bacik 		btrfs_try_granting_tickets(cache->fs_info, space_info);
379552bb7a21SBoris Burkov out:
3796606d1bf1SJosef Bacik 	spin_unlock(&cache->lock);
3797606d1bf1SJosef Bacik 	spin_unlock(&space_info->lock);
3798606d1bf1SJosef Bacik 	return ret;
3799606d1bf1SJosef Bacik }
3800606d1bf1SJosef Bacik 
380143dd529aSDavid Sterba /*
380243dd529aSDavid Sterba  * Update the block_group and space info counters.
380343dd529aSDavid Sterba  *
3804606d1bf1SJosef Bacik  * @cache:      The cache we are manipulating
3805606d1bf1SJosef Bacik  * @num_bytes:  The number of bytes in question
3806606d1bf1SJosef Bacik  * @delalloc:   The blocks are allocated for the delalloc write
3807606d1bf1SJosef Bacik  *
3808606d1bf1SJosef Bacik  * This is called by somebody who is freeing space that was never actually used
3809606d1bf1SJosef Bacik  * on disk.  For example if you reserve some space for a new leaf in transaction
3810606d1bf1SJosef Bacik  * A and before transaction A commits you free that leaf, you call this with
3811606d1bf1SJosef Bacik  * reserve set to 0 in order to clear the reservation.
3812606d1bf1SJosef Bacik  */
btrfs_free_reserved_bytes(struct btrfs_block_group * cache,u64 num_bytes,int delalloc)381332da5386SDavid Sterba void btrfs_free_reserved_bytes(struct btrfs_block_group *cache,
3814606d1bf1SJosef Bacik 			       u64 num_bytes, int delalloc)
3815606d1bf1SJosef Bacik {
3816606d1bf1SJosef Bacik 	struct btrfs_space_info *space_info = cache->space_info;
3817606d1bf1SJosef Bacik 
3818606d1bf1SJosef Bacik 	spin_lock(&space_info->lock);
3819606d1bf1SJosef Bacik 	spin_lock(&cache->lock);
3820606d1bf1SJosef Bacik 	if (cache->ro)
3821606d1bf1SJosef Bacik 		space_info->bytes_readonly += num_bytes;
3822606d1bf1SJosef Bacik 	cache->reserved -= num_bytes;
3823606d1bf1SJosef Bacik 	space_info->bytes_reserved -= num_bytes;
3824606d1bf1SJosef Bacik 	space_info->max_extent_size = 0;
3825606d1bf1SJosef Bacik 
3826606d1bf1SJosef Bacik 	if (delalloc)
3827606d1bf1SJosef Bacik 		cache->delalloc_bytes -= num_bytes;
3828606d1bf1SJosef Bacik 	spin_unlock(&cache->lock);
38293308234aSJosef Bacik 
38303308234aSJosef Bacik 	btrfs_try_granting_tickets(cache->fs_info, space_info);
3831606d1bf1SJosef Bacik 	spin_unlock(&space_info->lock);
3832606d1bf1SJosef Bacik }
383307730d87SJosef Bacik 
force_metadata_allocation(struct btrfs_fs_info * info)383407730d87SJosef Bacik static void force_metadata_allocation(struct btrfs_fs_info *info)
383507730d87SJosef Bacik {
383607730d87SJosef Bacik 	struct list_head *head = &info->space_info;
383707730d87SJosef Bacik 	struct btrfs_space_info *found;
383807730d87SJosef Bacik 
383972804905SJosef Bacik 	list_for_each_entry(found, head, list) {
384007730d87SJosef Bacik 		if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
384107730d87SJosef Bacik 			found->force_alloc = CHUNK_ALLOC_FORCE;
384207730d87SJosef Bacik 	}
384307730d87SJosef Bacik }
384407730d87SJosef Bacik 
should_alloc_chunk(const struct btrfs_fs_info * fs_info,const struct btrfs_space_info * sinfo,int force)3845*ca283ea9SDavid Sterba static int should_alloc_chunk(const struct btrfs_fs_info *fs_info,
3846*ca283ea9SDavid Sterba 			      const struct btrfs_space_info *sinfo, int force)
384707730d87SJosef Bacik {
384807730d87SJosef Bacik 	u64 bytes_used = btrfs_space_info_used(sinfo, false);
384907730d87SJosef Bacik 	u64 thresh;
385007730d87SJosef Bacik 
385107730d87SJosef Bacik 	if (force == CHUNK_ALLOC_FORCE)
385207730d87SJosef Bacik 		return 1;
385307730d87SJosef Bacik 
385407730d87SJosef Bacik 	/*
385507730d87SJosef Bacik 	 * in limited mode, we want to have some free space up to
385607730d87SJosef Bacik 	 * about 1% of the FS size.
385707730d87SJosef Bacik 	 */
385807730d87SJosef Bacik 	if (force == CHUNK_ALLOC_LIMITED) {
385907730d87SJosef Bacik 		thresh = btrfs_super_total_bytes(fs_info->super_copy);
3860428c8e03SDavid Sterba 		thresh = max_t(u64, SZ_64M, mult_perc(thresh, 1));
386107730d87SJosef Bacik 
386207730d87SJosef Bacik 		if (sinfo->total_bytes - bytes_used < thresh)
386307730d87SJosef Bacik 			return 1;
386407730d87SJosef Bacik 	}
386507730d87SJosef Bacik 
3866428c8e03SDavid Sterba 	if (bytes_used + SZ_2M < mult_perc(sinfo->total_bytes, 80))
386707730d87SJosef Bacik 		return 0;
386807730d87SJosef Bacik 	return 1;
386907730d87SJosef Bacik }
387007730d87SJosef Bacik 
btrfs_force_chunk_alloc(struct btrfs_trans_handle * trans,u64 type)387107730d87SJosef Bacik int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
387207730d87SJosef Bacik {
387307730d87SJosef Bacik 	u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type);
387407730d87SJosef Bacik 
387507730d87SJosef Bacik 	return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
387607730d87SJosef Bacik }
387707730d87SJosef Bacik 
do_chunk_alloc(struct btrfs_trans_handle * trans,u64 flags)3878820c363bSNaohiro Aota static struct btrfs_block_group *do_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags)
387979bd3712SFilipe Manana {
388079bd3712SFilipe Manana 	struct btrfs_block_group *bg;
388179bd3712SFilipe Manana 	int ret;
388279bd3712SFilipe Manana 
388307730d87SJosef Bacik 	/*
388479bd3712SFilipe Manana 	 * Check if we have enough space in the system space info because we
388579bd3712SFilipe Manana 	 * will need to update device items in the chunk btree and insert a new
388679bd3712SFilipe Manana 	 * chunk item in the chunk btree as well. This will allocate a new
388779bd3712SFilipe Manana 	 * system block group if needed.
388879bd3712SFilipe Manana 	 */
388979bd3712SFilipe Manana 	check_system_chunk(trans, flags);
389079bd3712SFilipe Manana 
3891f6f39f7aSNikolay Borisov 	bg = btrfs_create_chunk(trans, flags);
389279bd3712SFilipe Manana 	if (IS_ERR(bg)) {
389379bd3712SFilipe Manana 		ret = PTR_ERR(bg);
389479bd3712SFilipe Manana 		goto out;
389579bd3712SFilipe Manana 	}
389679bd3712SFilipe Manana 
389779bd3712SFilipe Manana 	ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
389879bd3712SFilipe Manana 	/*
389979bd3712SFilipe Manana 	 * Normally we are not expected to fail with -ENOSPC here, since we have
390079bd3712SFilipe Manana 	 * previously reserved space in the system space_info and allocated one
3901ecd84d54SFilipe Manana 	 * new system chunk if necessary. However there are three exceptions:
390279bd3712SFilipe Manana 	 *
390379bd3712SFilipe Manana 	 * 1) We may have enough free space in the system space_info but all the
390479bd3712SFilipe Manana 	 *    existing system block groups have a profile which can not be used
390579bd3712SFilipe Manana 	 *    for extent allocation.
390679bd3712SFilipe Manana 	 *
390779bd3712SFilipe Manana 	 *    This happens when mounting in degraded mode. For example we have a
390879bd3712SFilipe Manana 	 *    RAID1 filesystem with 2 devices, lose one device and mount the fs
390979bd3712SFilipe Manana 	 *    using the other device in degraded mode. If we then allocate a chunk,
391079bd3712SFilipe Manana 	 *    we may have enough free space in the existing system space_info, but
391179bd3712SFilipe Manana 	 *    none of the block groups can be used for extent allocation since they
391279bd3712SFilipe Manana 	 *    have a RAID1 profile, and because we are in degraded mode with a
391379bd3712SFilipe Manana 	 *    single device, we are forced to allocate a new system chunk with a
391479bd3712SFilipe Manana 	 *    SINGLE profile. Making check_system_chunk() iterate over all system
391579bd3712SFilipe Manana 	 *    block groups and check if they have a usable profile and enough space
391679bd3712SFilipe Manana 	 *    can be slow on very large filesystems, so we tolerate the -ENOSPC and
391779bd3712SFilipe Manana 	 *    try again after forcing allocation of a new system chunk. Like this
391879bd3712SFilipe Manana 	 *    we avoid paying the cost of that search in normal circumstances, when
391979bd3712SFilipe Manana 	 *    we were not mounted in degraded mode;
392079bd3712SFilipe Manana 	 *
392179bd3712SFilipe Manana 	 * 2) We had enough free space info the system space_info, and one suitable
392279bd3712SFilipe Manana 	 *    block group to allocate from when we called check_system_chunk()
392379bd3712SFilipe Manana 	 *    above. However right after we called it, the only system block group
392479bd3712SFilipe Manana 	 *    with enough free space got turned into RO mode by a running scrub,
392579bd3712SFilipe Manana 	 *    and in this case we have to allocate a new one and retry. We only
392679bd3712SFilipe Manana 	 *    need do this allocate and retry once, since we have a transaction
3927ecd84d54SFilipe Manana 	 *    handle and scrub uses the commit root to search for block groups;
3928ecd84d54SFilipe Manana 	 *
3929ecd84d54SFilipe Manana 	 * 3) We had one system block group with enough free space when we called
3930ecd84d54SFilipe Manana 	 *    check_system_chunk(), but after that, right before we tried to
3931ecd84d54SFilipe Manana 	 *    allocate the last extent buffer we needed, a discard operation came
3932ecd84d54SFilipe Manana 	 *    in and it temporarily removed the last free space entry from the
3933ecd84d54SFilipe Manana 	 *    block group (discard removes a free space entry, discards it, and
3934ecd84d54SFilipe Manana 	 *    then adds back the entry to the block group cache).
393579bd3712SFilipe Manana 	 */
393679bd3712SFilipe Manana 	if (ret == -ENOSPC) {
393779bd3712SFilipe Manana 		const u64 sys_flags = btrfs_system_alloc_profile(trans->fs_info);
393879bd3712SFilipe Manana 		struct btrfs_block_group *sys_bg;
393979bd3712SFilipe Manana 
3940f6f39f7aSNikolay Borisov 		sys_bg = btrfs_create_chunk(trans, sys_flags);
394179bd3712SFilipe Manana 		if (IS_ERR(sys_bg)) {
394279bd3712SFilipe Manana 			ret = PTR_ERR(sys_bg);
394379bd3712SFilipe Manana 			btrfs_abort_transaction(trans, ret);
394479bd3712SFilipe Manana 			goto out;
394579bd3712SFilipe Manana 		}
394679bd3712SFilipe Manana 
394779bd3712SFilipe Manana 		ret = btrfs_chunk_alloc_add_chunk_item(trans, sys_bg);
394879bd3712SFilipe Manana 		if (ret) {
394979bd3712SFilipe Manana 			btrfs_abort_transaction(trans, ret);
395079bd3712SFilipe Manana 			goto out;
395179bd3712SFilipe Manana 		}
395279bd3712SFilipe Manana 
395379bd3712SFilipe Manana 		ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
395479bd3712SFilipe Manana 		if (ret) {
395579bd3712SFilipe Manana 			btrfs_abort_transaction(trans, ret);
395679bd3712SFilipe Manana 			goto out;
395779bd3712SFilipe Manana 		}
395879bd3712SFilipe Manana 	} else if (ret) {
395979bd3712SFilipe Manana 		btrfs_abort_transaction(trans, ret);
396079bd3712SFilipe Manana 		goto out;
396179bd3712SFilipe Manana 	}
396279bd3712SFilipe Manana out:
396379bd3712SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
396479bd3712SFilipe Manana 
3965820c363bSNaohiro Aota 	if (ret)
3966820c363bSNaohiro Aota 		return ERR_PTR(ret);
3967820c363bSNaohiro Aota 
3968820c363bSNaohiro Aota 	btrfs_get_block_group(bg);
3969820c363bSNaohiro Aota 	return bg;
397079bd3712SFilipe Manana }
397179bd3712SFilipe Manana 
397279bd3712SFilipe Manana /*
397379bd3712SFilipe Manana  * Chunk allocation is done in 2 phases:
397479bd3712SFilipe Manana  *
397579bd3712SFilipe Manana  * 1) Phase 1 - through btrfs_chunk_alloc() we allocate device extents for
397679bd3712SFilipe Manana  *    the chunk, the chunk mapping, create its block group and add the items
397779bd3712SFilipe Manana  *    that belong in the chunk btree to it - more specifically, we need to
397879bd3712SFilipe Manana  *    update device items in the chunk btree and add a new chunk item to it.
397979bd3712SFilipe Manana  *
398079bd3712SFilipe Manana  * 2) Phase 2 - through btrfs_create_pending_block_groups(), we add the block
398179bd3712SFilipe Manana  *    group item to the extent btree and the device extent items to the devices
398279bd3712SFilipe Manana  *    btree.
398379bd3712SFilipe Manana  *
398479bd3712SFilipe Manana  * This is done to prevent deadlocks. For example when COWing a node from the
398579bd3712SFilipe Manana  * extent btree we are holding a write lock on the node's parent and if we
398679bd3712SFilipe Manana  * trigger chunk allocation and attempted to insert the new block group item
398779bd3712SFilipe Manana  * in the extent btree right way, we could deadlock because the path for the
398879bd3712SFilipe Manana  * insertion can include that parent node. At first glance it seems impossible
398979bd3712SFilipe Manana  * to trigger chunk allocation after starting a transaction since tasks should
399079bd3712SFilipe Manana  * reserve enough transaction units (metadata space), however while that is true
399179bd3712SFilipe Manana  * most of the time, chunk allocation may still be triggered for several reasons:
399279bd3712SFilipe Manana  *
399379bd3712SFilipe Manana  * 1) When reserving metadata, we check if there is enough free space in the
399479bd3712SFilipe Manana  *    metadata space_info and therefore don't trigger allocation of a new chunk.
399579bd3712SFilipe Manana  *    However later when the task actually tries to COW an extent buffer from
399679bd3712SFilipe Manana  *    the extent btree or from the device btree for example, it is forced to
399779bd3712SFilipe Manana  *    allocate a new block group (chunk) because the only one that had enough
399879bd3712SFilipe Manana  *    free space was just turned to RO mode by a running scrub for example (or
399979bd3712SFilipe Manana  *    device replace, block group reclaim thread, etc), so we can not use it
400079bd3712SFilipe Manana  *    for allocating an extent and end up being forced to allocate a new one;
400179bd3712SFilipe Manana  *
400279bd3712SFilipe Manana  * 2) Because we only check that the metadata space_info has enough free bytes,
400379bd3712SFilipe Manana  *    we end up not allocating a new metadata chunk in that case. However if
400479bd3712SFilipe Manana  *    the filesystem was mounted in degraded mode, none of the existing block
400579bd3712SFilipe Manana  *    groups might be suitable for extent allocation due to their incompatible
400679bd3712SFilipe Manana  *    profile (for e.g. mounting a 2 devices filesystem, where all block groups
400779bd3712SFilipe Manana  *    use a RAID1 profile, in degraded mode using a single device). In this case
400879bd3712SFilipe Manana  *    when the task attempts to COW some extent buffer of the extent btree for
400979bd3712SFilipe Manana  *    example, it will trigger allocation of a new metadata block group with a
401079bd3712SFilipe Manana  *    suitable profile (SINGLE profile in the example of the degraded mount of
401179bd3712SFilipe Manana  *    the RAID1 filesystem);
401279bd3712SFilipe Manana  *
401379bd3712SFilipe Manana  * 3) The task has reserved enough transaction units / metadata space, but when
401479bd3712SFilipe Manana  *    it attempts to COW an extent buffer from the extent or device btree for
401579bd3712SFilipe Manana  *    example, it does not find any free extent in any metadata block group,
401679bd3712SFilipe Manana  *    therefore forced to try to allocate a new metadata block group.
401779bd3712SFilipe Manana  *    This is because some other task allocated all available extents in the
401879bd3712SFilipe Manana  *    meanwhile - this typically happens with tasks that don't reserve space
401979bd3712SFilipe Manana  *    properly, either intentionally or as a bug. One example where this is
402079bd3712SFilipe Manana  *    done intentionally is fsync, as it does not reserve any transaction units
402179bd3712SFilipe Manana  *    and ends up allocating a variable number of metadata extents for log
4022ecd84d54SFilipe Manana  *    tree extent buffers;
4023ecd84d54SFilipe Manana  *
4024ecd84d54SFilipe Manana  * 4) The task has reserved enough transaction units / metadata space, but right
4025ecd84d54SFilipe Manana  *    before it tries to allocate the last extent buffer it needs, a discard
4026ecd84d54SFilipe Manana  *    operation comes in and, temporarily, removes the last free space entry from
4027ecd84d54SFilipe Manana  *    the only metadata block group that had free space (discard starts by
4028ecd84d54SFilipe Manana  *    removing a free space entry from a block group, then does the discard
4029ecd84d54SFilipe Manana  *    operation and, once it's done, it adds back the free space entry to the
4030ecd84d54SFilipe Manana  *    block group).
403179bd3712SFilipe Manana  *
403279bd3712SFilipe Manana  * We also need this 2 phases setup when adding a device to a filesystem with
403379bd3712SFilipe Manana  * a seed device - we must create new metadata and system chunks without adding
403479bd3712SFilipe Manana  * any of the block group items to the chunk, extent and device btrees. If we
403579bd3712SFilipe Manana  * did not do it this way, we would get ENOSPC when attempting to update those
403679bd3712SFilipe Manana  * btrees, since all the chunks from the seed device are read-only.
403779bd3712SFilipe Manana  *
403879bd3712SFilipe Manana  * Phase 1 does the updates and insertions to the chunk btree because if we had
403979bd3712SFilipe Manana  * it done in phase 2 and have a thundering herd of tasks allocating chunks in
404079bd3712SFilipe Manana  * parallel, we risk having too many system chunks allocated by many tasks if
404179bd3712SFilipe Manana  * many tasks reach phase 1 without the previous ones completing phase 2. In the
404279bd3712SFilipe Manana  * extreme case this leads to exhaustion of the system chunk array in the
404379bd3712SFilipe Manana  * superblock. This is easier to trigger if using a btree node/leaf size of 64K
404479bd3712SFilipe Manana  * and with RAID filesystems (so we have more device items in the chunk btree).
404579bd3712SFilipe Manana  * This has happened before and commit eafa4fd0ad0607 ("btrfs: fix exhaustion of
404679bd3712SFilipe Manana  * the system chunk array due to concurrent allocations") provides more details.
404779bd3712SFilipe Manana  *
40482bb2e00eSFilipe Manana  * Allocation of system chunks does not happen through this function. A task that
40492bb2e00eSFilipe Manana  * needs to update the chunk btree (the only btree that uses system chunks), must
40502bb2e00eSFilipe Manana  * preallocate chunk space by calling either check_system_chunk() or
40512bb2e00eSFilipe Manana  * btrfs_reserve_chunk_metadata() - the former is used when allocating a data or
40522bb2e00eSFilipe Manana  * metadata chunk or when removing a chunk, while the later is used before doing
40532bb2e00eSFilipe Manana  * a modification to the chunk btree - use cases for the later are adding,
40542bb2e00eSFilipe Manana  * removing and resizing a device as well as relocation of a system chunk.
40552bb2e00eSFilipe Manana  * See the comment below for more details.
405679bd3712SFilipe Manana  *
405779bd3712SFilipe Manana  * The reservation of system space, done through check_system_chunk(), as well
405879bd3712SFilipe Manana  * as all the updates and insertions into the chunk btree must be done while
405979bd3712SFilipe Manana  * holding fs_info->chunk_mutex. This is important to guarantee that while COWing
406079bd3712SFilipe Manana  * an extent buffer from the chunks btree we never trigger allocation of a new
406179bd3712SFilipe Manana  * system chunk, which would result in a deadlock (trying to lock twice an
406279bd3712SFilipe Manana  * extent buffer of the chunk btree, first time before triggering the chunk
406379bd3712SFilipe Manana  * allocation and the second time during chunk allocation while attempting to
406479bd3712SFilipe Manana  * update the chunks btree). The system chunk array is also updated while holding
406579bd3712SFilipe Manana  * that mutex. The same logic applies to removing chunks - we must reserve system
406679bd3712SFilipe Manana  * space, update the chunk btree and the system chunk array in the superblock
406779bd3712SFilipe Manana  * while holding fs_info->chunk_mutex.
406879bd3712SFilipe Manana  *
406979bd3712SFilipe Manana  * This function, btrfs_chunk_alloc(), belongs to phase 1.
407079bd3712SFilipe Manana  *
407179bd3712SFilipe Manana  * If @force is CHUNK_ALLOC_FORCE:
407207730d87SJosef Bacik  *    - return 1 if it successfully allocates a chunk,
407307730d87SJosef Bacik  *    - return errors including -ENOSPC otherwise.
407479bd3712SFilipe Manana  * If @force is NOT CHUNK_ALLOC_FORCE:
407507730d87SJosef Bacik  *    - return 0 if it doesn't need to allocate a new chunk,
407607730d87SJosef Bacik  *    - return 1 if it successfully allocates a chunk,
407707730d87SJosef Bacik  *    - return errors including -ENOSPC otherwise.
407807730d87SJosef Bacik  */
btrfs_chunk_alloc(struct btrfs_trans_handle * trans,u64 flags,enum btrfs_chunk_alloc_enum force)407907730d87SJosef Bacik int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
408007730d87SJosef Bacik 		      enum btrfs_chunk_alloc_enum force)
408107730d87SJosef Bacik {
408207730d87SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
408307730d87SJosef Bacik 	struct btrfs_space_info *space_info;
4084820c363bSNaohiro Aota 	struct btrfs_block_group *ret_bg;
408507730d87SJosef Bacik 	bool wait_for_alloc = false;
408607730d87SJosef Bacik 	bool should_alloc = false;
4087760e69c4SNaohiro Aota 	bool from_extent_allocation = false;
408807730d87SJosef Bacik 	int ret = 0;
408907730d87SJosef Bacik 
4090760e69c4SNaohiro Aota 	if (force == CHUNK_ALLOC_FORCE_FOR_EXTENT) {
4091760e69c4SNaohiro Aota 		from_extent_allocation = true;
4092760e69c4SNaohiro Aota 		force = CHUNK_ALLOC_FORCE;
4093760e69c4SNaohiro Aota 	}
4094760e69c4SNaohiro Aota 
409507730d87SJosef Bacik 	/* Don't re-enter if we're already allocating a chunk */
409607730d87SJosef Bacik 	if (trans->allocating_chunk)
409707730d87SJosef Bacik 		return -ENOSPC;
409879bd3712SFilipe Manana 	/*
40992bb2e00eSFilipe Manana 	 * Allocation of system chunks can not happen through this path, as we
41002bb2e00eSFilipe Manana 	 * could end up in a deadlock if we are allocating a data or metadata
41012bb2e00eSFilipe Manana 	 * chunk and there is another task modifying the chunk btree.
41022bb2e00eSFilipe Manana 	 *
41032bb2e00eSFilipe Manana 	 * This is because while we are holding the chunk mutex, we will attempt
41042bb2e00eSFilipe Manana 	 * to add the new chunk item to the chunk btree or update an existing
41052bb2e00eSFilipe Manana 	 * device item in the chunk btree, while the other task that is modifying
41062bb2e00eSFilipe Manana 	 * the chunk btree is attempting to COW an extent buffer while holding a
41072bb2e00eSFilipe Manana 	 * lock on it and on its parent - if the COW operation triggers a system
41082bb2e00eSFilipe Manana 	 * chunk allocation, then we can deadlock because we are holding the
41092bb2e00eSFilipe Manana 	 * chunk mutex and we may need to access that extent buffer or its parent
41102bb2e00eSFilipe Manana 	 * in order to add the chunk item or update a device item.
41112bb2e00eSFilipe Manana 	 *
41122bb2e00eSFilipe Manana 	 * Tasks that want to modify the chunk tree should reserve system space
41132bb2e00eSFilipe Manana 	 * before updating the chunk btree, by calling either
41142bb2e00eSFilipe Manana 	 * btrfs_reserve_chunk_metadata() or check_system_chunk().
41152bb2e00eSFilipe Manana 	 * It's possible that after a task reserves the space, it still ends up
41162bb2e00eSFilipe Manana 	 * here - this happens in the cases described above at do_chunk_alloc().
41172bb2e00eSFilipe Manana 	 * The task will have to either retry or fail.
411879bd3712SFilipe Manana 	 */
41192bb2e00eSFilipe Manana 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
412079bd3712SFilipe Manana 		return -ENOSPC;
412107730d87SJosef Bacik 
412207730d87SJosef Bacik 	space_info = btrfs_find_space_info(fs_info, flags);
412307730d87SJosef Bacik 	ASSERT(space_info);
412407730d87SJosef Bacik 
412507730d87SJosef Bacik 	do {
412607730d87SJosef Bacik 		spin_lock(&space_info->lock);
412707730d87SJosef Bacik 		if (force < space_info->force_alloc)
412807730d87SJosef Bacik 			force = space_info->force_alloc;
412907730d87SJosef Bacik 		should_alloc = should_alloc_chunk(fs_info, space_info, force);
413007730d87SJosef Bacik 		if (space_info->full) {
413107730d87SJosef Bacik 			/* No more free physical space */
413207730d87SJosef Bacik 			if (should_alloc)
413307730d87SJosef Bacik 				ret = -ENOSPC;
413407730d87SJosef Bacik 			else
413507730d87SJosef Bacik 				ret = 0;
413607730d87SJosef Bacik 			spin_unlock(&space_info->lock);
413707730d87SJosef Bacik 			return ret;
413807730d87SJosef Bacik 		} else if (!should_alloc) {
413907730d87SJosef Bacik 			spin_unlock(&space_info->lock);
414007730d87SJosef Bacik 			return 0;
414107730d87SJosef Bacik 		} else if (space_info->chunk_alloc) {
414207730d87SJosef Bacik 			/*
414307730d87SJosef Bacik 			 * Someone is already allocating, so we need to block
414407730d87SJosef Bacik 			 * until this someone is finished and then loop to
414507730d87SJosef Bacik 			 * recheck if we should continue with our allocation
414607730d87SJosef Bacik 			 * attempt.
414707730d87SJosef Bacik 			 */
414807730d87SJosef Bacik 			wait_for_alloc = true;
41491314ca78SJosef Bacik 			force = CHUNK_ALLOC_NO_FORCE;
415007730d87SJosef Bacik 			spin_unlock(&space_info->lock);
415107730d87SJosef Bacik 			mutex_lock(&fs_info->chunk_mutex);
415207730d87SJosef Bacik 			mutex_unlock(&fs_info->chunk_mutex);
415307730d87SJosef Bacik 		} else {
415407730d87SJosef Bacik 			/* Proceed with allocation */
415507730d87SJosef Bacik 			space_info->chunk_alloc = 1;
415607730d87SJosef Bacik 			wait_for_alloc = false;
415707730d87SJosef Bacik 			spin_unlock(&space_info->lock);
415807730d87SJosef Bacik 		}
415907730d87SJosef Bacik 
416007730d87SJosef Bacik 		cond_resched();
416107730d87SJosef Bacik 	} while (wait_for_alloc);
416207730d87SJosef Bacik 
416307730d87SJosef Bacik 	mutex_lock(&fs_info->chunk_mutex);
416407730d87SJosef Bacik 	trans->allocating_chunk = true;
416507730d87SJosef Bacik 
416607730d87SJosef Bacik 	/*
416707730d87SJosef Bacik 	 * If we have mixed data/metadata chunks we want to make sure we keep
416807730d87SJosef Bacik 	 * allocating mixed chunks instead of individual chunks.
416907730d87SJosef Bacik 	 */
417007730d87SJosef Bacik 	if (btrfs_mixed_space_info(space_info))
417107730d87SJosef Bacik 		flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
417207730d87SJosef Bacik 
417307730d87SJosef Bacik 	/*
417407730d87SJosef Bacik 	 * if we're doing a data chunk, go ahead and make sure that
417507730d87SJosef Bacik 	 * we keep a reasonable number of metadata chunks allocated in the
417607730d87SJosef Bacik 	 * FS as well.
417707730d87SJosef Bacik 	 */
417807730d87SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
417907730d87SJosef Bacik 		fs_info->data_chunk_allocations++;
418007730d87SJosef Bacik 		if (!(fs_info->data_chunk_allocations %
418107730d87SJosef Bacik 		      fs_info->metadata_ratio))
418207730d87SJosef Bacik 			force_metadata_allocation(fs_info);
418307730d87SJosef Bacik 	}
418407730d87SJosef Bacik 
4185820c363bSNaohiro Aota 	ret_bg = do_chunk_alloc(trans, flags);
418607730d87SJosef Bacik 	trans->allocating_chunk = false;
418707730d87SJosef Bacik 
4188760e69c4SNaohiro Aota 	if (IS_ERR(ret_bg)) {
4189820c363bSNaohiro Aota 		ret = PTR_ERR(ret_bg);
41905a7d107eSNaohiro Aota 	} else if (from_extent_allocation && (flags & BTRFS_BLOCK_GROUP_DATA)) {
4191760e69c4SNaohiro Aota 		/*
4192760e69c4SNaohiro Aota 		 * New block group is likely to be used soon. Try to activate
4193760e69c4SNaohiro Aota 		 * it now. Failure is OK for now.
4194760e69c4SNaohiro Aota 		 */
4195760e69c4SNaohiro Aota 		btrfs_zone_activate(ret_bg);
4196760e69c4SNaohiro Aota 	}
4197760e69c4SNaohiro Aota 
4198760e69c4SNaohiro Aota 	if (!ret)
4199820c363bSNaohiro Aota 		btrfs_put_block_group(ret_bg);
4200820c363bSNaohiro Aota 
420107730d87SJosef Bacik 	spin_lock(&space_info->lock);
420207730d87SJosef Bacik 	if (ret < 0) {
420307730d87SJosef Bacik 		if (ret == -ENOSPC)
420407730d87SJosef Bacik 			space_info->full = 1;
420507730d87SJosef Bacik 		else
420607730d87SJosef Bacik 			goto out;
420707730d87SJosef Bacik 	} else {
420807730d87SJosef Bacik 		ret = 1;
420907730d87SJosef Bacik 		space_info->max_extent_size = 0;
421007730d87SJosef Bacik 	}
421107730d87SJosef Bacik 
421207730d87SJosef Bacik 	space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
421307730d87SJosef Bacik out:
421407730d87SJosef Bacik 	space_info->chunk_alloc = 0;
421507730d87SJosef Bacik 	spin_unlock(&space_info->lock);
421607730d87SJosef Bacik 	mutex_unlock(&fs_info->chunk_mutex);
421707730d87SJosef Bacik 
421807730d87SJosef Bacik 	return ret;
421907730d87SJosef Bacik }
422007730d87SJosef Bacik 
get_profile_num_devs(const struct btrfs_fs_info * fs_info,u64 type)4221*ca283ea9SDavid Sterba static u64 get_profile_num_devs(const struct btrfs_fs_info *fs_info, u64 type)
422207730d87SJosef Bacik {
422307730d87SJosef Bacik 	u64 num_dev;
422407730d87SJosef Bacik 
422507730d87SJosef Bacik 	num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max;
422607730d87SJosef Bacik 	if (!num_dev)
422707730d87SJosef Bacik 		num_dev = fs_info->fs_devices->rw_devices;
422807730d87SJosef Bacik 
422907730d87SJosef Bacik 	return num_dev;
423007730d87SJosef Bacik }
423107730d87SJosef Bacik 
reserve_chunk_space(struct btrfs_trans_handle * trans,u64 bytes,u64 type)42322bb2e00eSFilipe Manana static void reserve_chunk_space(struct btrfs_trans_handle *trans,
42332bb2e00eSFilipe Manana 				u64 bytes,
42342bb2e00eSFilipe Manana 				u64 type)
423507730d87SJosef Bacik {
423607730d87SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
423707730d87SJosef Bacik 	struct btrfs_space_info *info;
423807730d87SJosef Bacik 	u64 left;
423907730d87SJosef Bacik 	int ret = 0;
424007730d87SJosef Bacik 
424107730d87SJosef Bacik 	/*
424207730d87SJosef Bacik 	 * Needed because we can end up allocating a system chunk and for an
424307730d87SJosef Bacik 	 * atomic and race free space reservation in the chunk block reserve.
424407730d87SJosef Bacik 	 */
424507730d87SJosef Bacik 	lockdep_assert_held(&fs_info->chunk_mutex);
424607730d87SJosef Bacik 
424707730d87SJosef Bacik 	info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
424807730d87SJosef Bacik 	spin_lock(&info->lock);
424907730d87SJosef Bacik 	left = info->total_bytes - btrfs_space_info_used(info, true);
425007730d87SJosef Bacik 	spin_unlock(&info->lock);
425107730d87SJosef Bacik 
42522bb2e00eSFilipe Manana 	if (left < bytes && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
425307730d87SJosef Bacik 		btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
42542bb2e00eSFilipe Manana 			   left, bytes, type);
425507730d87SJosef Bacik 		btrfs_dump_space_info(fs_info, info, 0, 0);
425607730d87SJosef Bacik 	}
425707730d87SJosef Bacik 
42582bb2e00eSFilipe Manana 	if (left < bytes) {
425907730d87SJosef Bacik 		u64 flags = btrfs_system_alloc_profile(fs_info);
426079bd3712SFilipe Manana 		struct btrfs_block_group *bg;
426107730d87SJosef Bacik 
426207730d87SJosef Bacik 		/*
426307730d87SJosef Bacik 		 * Ignore failure to create system chunk. We might end up not
426407730d87SJosef Bacik 		 * needing it, as we might not need to COW all nodes/leafs from
426507730d87SJosef Bacik 		 * the paths we visit in the chunk tree (they were already COWed
426607730d87SJosef Bacik 		 * or created in the current transaction for example).
426707730d87SJosef Bacik 		 */
4268f6f39f7aSNikolay Borisov 		bg = btrfs_create_chunk(trans, flags);
426979bd3712SFilipe Manana 		if (IS_ERR(bg)) {
427079bd3712SFilipe Manana 			ret = PTR_ERR(bg);
42712bb2e00eSFilipe Manana 		} else {
427279bd3712SFilipe Manana 			/*
4273b6a98021SNaohiro Aota 			 * We have a new chunk. We also need to activate it for
4274b6a98021SNaohiro Aota 			 * zoned filesystem.
4275b6a98021SNaohiro Aota 			 */
4276b6a98021SNaohiro Aota 			ret = btrfs_zoned_activate_one_bg(fs_info, info, true);
4277b6a98021SNaohiro Aota 			if (ret < 0)
4278b6a98021SNaohiro Aota 				return;
4279b6a98021SNaohiro Aota 
4280b6a98021SNaohiro Aota 			/*
428179bd3712SFilipe Manana 			 * If we fail to add the chunk item here, we end up
428279bd3712SFilipe Manana 			 * trying again at phase 2 of chunk allocation, at
428379bd3712SFilipe Manana 			 * btrfs_create_pending_block_groups(). So ignore
42842bb2e00eSFilipe Manana 			 * any error here. An ENOSPC here could happen, due to
42852bb2e00eSFilipe Manana 			 * the cases described at do_chunk_alloc() - the system
42862bb2e00eSFilipe Manana 			 * block group we just created was just turned into RO
42872bb2e00eSFilipe Manana 			 * mode by a scrub for example, or a running discard
42882bb2e00eSFilipe Manana 			 * temporarily removed its free space entries, etc.
428979bd3712SFilipe Manana 			 */
429079bd3712SFilipe Manana 			btrfs_chunk_alloc_add_chunk_item(trans, bg);
429179bd3712SFilipe Manana 		}
429207730d87SJosef Bacik 	}
429307730d87SJosef Bacik 
429407730d87SJosef Bacik 	if (!ret) {
42959270501cSJosef Bacik 		ret = btrfs_block_rsv_add(fs_info,
429607730d87SJosef Bacik 					  &fs_info->chunk_block_rsv,
42972bb2e00eSFilipe Manana 					  bytes, BTRFS_RESERVE_NO_FLUSH);
42981cb3db1cSFilipe Manana 		if (!ret)
42992bb2e00eSFilipe Manana 			trans->chunk_bytes_reserved += bytes;
430007730d87SJosef Bacik 	}
430107730d87SJosef Bacik }
430207730d87SJosef Bacik 
43032bb2e00eSFilipe Manana /*
43042bb2e00eSFilipe Manana  * Reserve space in the system space for allocating or removing a chunk.
43052bb2e00eSFilipe Manana  * The caller must be holding fs_info->chunk_mutex.
43062bb2e00eSFilipe Manana  */
check_system_chunk(struct btrfs_trans_handle * trans,u64 type)43072bb2e00eSFilipe Manana void check_system_chunk(struct btrfs_trans_handle *trans, u64 type)
43082bb2e00eSFilipe Manana {
43092bb2e00eSFilipe Manana 	struct btrfs_fs_info *fs_info = trans->fs_info;
43102bb2e00eSFilipe Manana 	const u64 num_devs = get_profile_num_devs(fs_info, type);
43112bb2e00eSFilipe Manana 	u64 bytes;
43122bb2e00eSFilipe Manana 
43132bb2e00eSFilipe Manana 	/* num_devs device items to update and 1 chunk item to add or remove. */
43142bb2e00eSFilipe Manana 	bytes = btrfs_calc_metadata_size(fs_info, num_devs) +
43152bb2e00eSFilipe Manana 		btrfs_calc_insert_metadata_size(fs_info, 1);
43162bb2e00eSFilipe Manana 
43172bb2e00eSFilipe Manana 	reserve_chunk_space(trans, bytes, type);
43182bb2e00eSFilipe Manana }
43192bb2e00eSFilipe Manana 
43202bb2e00eSFilipe Manana /*
43212bb2e00eSFilipe Manana  * Reserve space in the system space, if needed, for doing a modification to the
43222bb2e00eSFilipe Manana  * chunk btree.
43232bb2e00eSFilipe Manana  *
43242bb2e00eSFilipe Manana  * @trans:		A transaction handle.
43252bb2e00eSFilipe Manana  * @is_item_insertion:	Indicate if the modification is for inserting a new item
43262bb2e00eSFilipe Manana  *			in the chunk btree or if it's for the deletion or update
43272bb2e00eSFilipe Manana  *			of an existing item.
43282bb2e00eSFilipe Manana  *
43292bb2e00eSFilipe Manana  * This is used in a context where we need to update the chunk btree outside
43302bb2e00eSFilipe Manana  * block group allocation and removal, to avoid a deadlock with a concurrent
43312bb2e00eSFilipe Manana  * task that is allocating a metadata or data block group and therefore needs to
43322bb2e00eSFilipe Manana  * update the chunk btree while holding the chunk mutex. After the update to the
43332bb2e00eSFilipe Manana  * chunk btree is done, btrfs_trans_release_chunk_metadata() should be called.
43342bb2e00eSFilipe Manana  *
43352bb2e00eSFilipe Manana  */
btrfs_reserve_chunk_metadata(struct btrfs_trans_handle * trans,bool is_item_insertion)43362bb2e00eSFilipe Manana void btrfs_reserve_chunk_metadata(struct btrfs_trans_handle *trans,
43372bb2e00eSFilipe Manana 				  bool is_item_insertion)
43382bb2e00eSFilipe Manana {
43392bb2e00eSFilipe Manana 	struct btrfs_fs_info *fs_info = trans->fs_info;
43402bb2e00eSFilipe Manana 	u64 bytes;
43412bb2e00eSFilipe Manana 
43422bb2e00eSFilipe Manana 	if (is_item_insertion)
43432bb2e00eSFilipe Manana 		bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
43442bb2e00eSFilipe Manana 	else
43452bb2e00eSFilipe Manana 		bytes = btrfs_calc_metadata_size(fs_info, 1);
43462bb2e00eSFilipe Manana 
43472bb2e00eSFilipe Manana 	mutex_lock(&fs_info->chunk_mutex);
43482bb2e00eSFilipe Manana 	reserve_chunk_space(trans, bytes, BTRFS_BLOCK_GROUP_SYSTEM);
43492bb2e00eSFilipe Manana 	mutex_unlock(&fs_info->chunk_mutex);
43502bb2e00eSFilipe Manana }
43512bb2e00eSFilipe Manana 
btrfs_put_block_group_cache(struct btrfs_fs_info * info)43523e43c279SJosef Bacik void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
43533e43c279SJosef Bacik {
435432da5386SDavid Sterba 	struct btrfs_block_group *block_group;
43553e43c279SJosef Bacik 
435650c31eaaSJosef Bacik 	block_group = btrfs_lookup_first_block_group(info, 0);
43573e43c279SJosef Bacik 	while (block_group) {
43583e43c279SJosef Bacik 		btrfs_wait_block_group_cache_done(block_group);
43593e43c279SJosef Bacik 		spin_lock(&block_group->lock);
436050c31eaaSJosef Bacik 		if (test_and_clear_bit(BLOCK_GROUP_FLAG_IREF,
436150c31eaaSJosef Bacik 				       &block_group->runtime_flags)) {
4362e108c86bSDavid Sterba 			struct btrfs_inode *inode = block_group->inode;
43633e43c279SJosef Bacik 
43643e43c279SJosef Bacik 			block_group->inode = NULL;
43653e43c279SJosef Bacik 			spin_unlock(&block_group->lock);
436650c31eaaSJosef Bacik 
43673e43c279SJosef Bacik 			ASSERT(block_group->io_ctl.inode == NULL);
4368e108c86bSDavid Sterba 			iput(&inode->vfs_inode);
436950c31eaaSJosef Bacik 		} else {
437050c31eaaSJosef Bacik 			spin_unlock(&block_group->lock);
437150c31eaaSJosef Bacik 		}
437250c31eaaSJosef Bacik 		block_group = btrfs_next_block_group(block_group);
43733e43c279SJosef Bacik 	}
43743e43c279SJosef Bacik }
43753e43c279SJosef Bacik 
43763e43c279SJosef Bacik /*
43773e43c279SJosef Bacik  * Must be called only after stopping all workers, since we could have block
43783e43c279SJosef Bacik  * group caching kthreads running, and therefore they could race with us if we
43793e43c279SJosef Bacik  * freed the block groups before stopping them.
43803e43c279SJosef Bacik  */
btrfs_free_block_groups(struct btrfs_fs_info * info)43813e43c279SJosef Bacik int btrfs_free_block_groups(struct btrfs_fs_info *info)
43823e43c279SJosef Bacik {
438332da5386SDavid Sterba 	struct btrfs_block_group *block_group;
43843e43c279SJosef Bacik 	struct btrfs_space_info *space_info;
43853e43c279SJosef Bacik 	struct btrfs_caching_control *caching_ctl;
43863e43c279SJosef Bacik 	struct rb_node *n;
43873e43c279SJosef Bacik 
438813bb483dSNaohiro Aota 	if (btrfs_is_zoned(info)) {
438913bb483dSNaohiro Aota 		if (info->active_meta_bg) {
439013bb483dSNaohiro Aota 			btrfs_put_block_group(info->active_meta_bg);
439113bb483dSNaohiro Aota 			info->active_meta_bg = NULL;
439213bb483dSNaohiro Aota 		}
439313bb483dSNaohiro Aota 		if (info->active_system_bg) {
439413bb483dSNaohiro Aota 			btrfs_put_block_group(info->active_system_bg);
439513bb483dSNaohiro Aota 			info->active_system_bg = NULL;
439613bb483dSNaohiro Aota 		}
439713bb483dSNaohiro Aota 	}
439813bb483dSNaohiro Aota 
439916b0c258SFilipe Manana 	write_lock(&info->block_group_cache_lock);
44003e43c279SJosef Bacik 	while (!list_empty(&info->caching_block_groups)) {
44013e43c279SJosef Bacik 		caching_ctl = list_entry(info->caching_block_groups.next,
44023e43c279SJosef Bacik 					 struct btrfs_caching_control, list);
44033e43c279SJosef Bacik 		list_del(&caching_ctl->list);
44043e43c279SJosef Bacik 		btrfs_put_caching_control(caching_ctl);
44053e43c279SJosef Bacik 	}
440616b0c258SFilipe Manana 	write_unlock(&info->block_group_cache_lock);
44073e43c279SJosef Bacik 
44083e43c279SJosef Bacik 	spin_lock(&info->unused_bgs_lock);
44093e43c279SJosef Bacik 	while (!list_empty(&info->unused_bgs)) {
44103e43c279SJosef Bacik 		block_group = list_first_entry(&info->unused_bgs,
441132da5386SDavid Sterba 					       struct btrfs_block_group,
44123e43c279SJosef Bacik 					       bg_list);
44133e43c279SJosef Bacik 		list_del_init(&block_group->bg_list);
44143e43c279SJosef Bacik 		btrfs_put_block_group(block_group);
44153e43c279SJosef Bacik 	}
44163e43c279SJosef Bacik 
441718bb8bbfSJohannes Thumshirn 	while (!list_empty(&info->reclaim_bgs)) {
441818bb8bbfSJohannes Thumshirn 		block_group = list_first_entry(&info->reclaim_bgs,
441918bb8bbfSJohannes Thumshirn 					       struct btrfs_block_group,
442018bb8bbfSJohannes Thumshirn 					       bg_list);
442118bb8bbfSJohannes Thumshirn 		list_del_init(&block_group->bg_list);
442218bb8bbfSJohannes Thumshirn 		btrfs_put_block_group(block_group);
442318bb8bbfSJohannes Thumshirn 	}
442418bb8bbfSJohannes Thumshirn 	spin_unlock(&info->unused_bgs_lock);
442518bb8bbfSJohannes Thumshirn 
4426afba2bc0SNaohiro Aota 	spin_lock(&info->zone_active_bgs_lock);
4427afba2bc0SNaohiro Aota 	while (!list_empty(&info->zone_active_bgs)) {
4428afba2bc0SNaohiro Aota 		block_group = list_first_entry(&info->zone_active_bgs,
4429afba2bc0SNaohiro Aota 					       struct btrfs_block_group,
4430afba2bc0SNaohiro Aota 					       active_bg_list);
4431afba2bc0SNaohiro Aota 		list_del_init(&block_group->active_bg_list);
4432afba2bc0SNaohiro Aota 		btrfs_put_block_group(block_group);
4433afba2bc0SNaohiro Aota 	}
4434afba2bc0SNaohiro Aota 	spin_unlock(&info->zone_active_bgs_lock);
4435afba2bc0SNaohiro Aota 
443616b0c258SFilipe Manana 	write_lock(&info->block_group_cache_lock);
443708dddb29SFilipe Manana 	while ((n = rb_last(&info->block_group_cache_tree.rb_root)) != NULL) {
443832da5386SDavid Sterba 		block_group = rb_entry(n, struct btrfs_block_group,
44393e43c279SJosef Bacik 				       cache_node);
444008dddb29SFilipe Manana 		rb_erase_cached(&block_group->cache_node,
44413e43c279SJosef Bacik 				&info->block_group_cache_tree);
44423e43c279SJosef Bacik 		RB_CLEAR_NODE(&block_group->cache_node);
444316b0c258SFilipe Manana 		write_unlock(&info->block_group_cache_lock);
44443e43c279SJosef Bacik 
44453e43c279SJosef Bacik 		down_write(&block_group->space_info->groups_sem);
44463e43c279SJosef Bacik 		list_del(&block_group->list);
44473e43c279SJosef Bacik 		up_write(&block_group->space_info->groups_sem);
44483e43c279SJosef Bacik 
44493e43c279SJosef Bacik 		/*
44503e43c279SJosef Bacik 		 * We haven't cached this block group, which means we could
44513e43c279SJosef Bacik 		 * possibly have excluded extents on this block group.
44523e43c279SJosef Bacik 		 */
44533e43c279SJosef Bacik 		if (block_group->cached == BTRFS_CACHE_NO ||
44543e43c279SJosef Bacik 		    block_group->cached == BTRFS_CACHE_ERROR)
44553e43c279SJosef Bacik 			btrfs_free_excluded_extents(block_group);
44563e43c279SJosef Bacik 
44573e43c279SJosef Bacik 		btrfs_remove_free_space_cache(block_group);
44583e43c279SJosef Bacik 		ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
44593e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->dirty_list));
44603e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->io_list));
44613e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->bg_list));
446248aaeebeSJosef Bacik 		ASSERT(refcount_read(&block_group->refs) == 1);
4463195a49eaSFilipe Manana 		ASSERT(block_group->swap_extents == 0);
44643e43c279SJosef Bacik 		btrfs_put_block_group(block_group);
44653e43c279SJosef Bacik 
446616b0c258SFilipe Manana 		write_lock(&info->block_group_cache_lock);
44673e43c279SJosef Bacik 	}
446816b0c258SFilipe Manana 	write_unlock(&info->block_group_cache_lock);
44693e43c279SJosef Bacik 
44703e43c279SJosef Bacik 	btrfs_release_global_block_rsv(info);
44713e43c279SJosef Bacik 
44723e43c279SJosef Bacik 	while (!list_empty(&info->space_info)) {
44733e43c279SJosef Bacik 		space_info = list_entry(info->space_info.next,
44743e43c279SJosef Bacik 					struct btrfs_space_info,
44753e43c279SJosef Bacik 					list);
44763e43c279SJosef Bacik 
44773e43c279SJosef Bacik 		/*
44783e43c279SJosef Bacik 		 * Do not hide this behind enospc_debug, this is actually
44793e43c279SJosef Bacik 		 * important and indicates a real bug if this happens.
44803e43c279SJosef Bacik 		 */
44813e43c279SJosef Bacik 		if (WARN_ON(space_info->bytes_pinned > 0 ||
44823e43c279SJosef Bacik 			    space_info->bytes_may_use > 0))
44833e43c279SJosef Bacik 			btrfs_dump_space_info(info, space_info, 0, 0);
448440cdc509SFilipe Manana 
448540cdc509SFilipe Manana 		/*
448640cdc509SFilipe Manana 		 * If there was a failure to cleanup a log tree, very likely due
448740cdc509SFilipe Manana 		 * to an IO failure on a writeback attempt of one or more of its
448840cdc509SFilipe Manana 		 * extent buffers, we could not do proper (and cheap) unaccounting
448940cdc509SFilipe Manana 		 * of their reserved space, so don't warn on bytes_reserved > 0 in
449040cdc509SFilipe Manana 		 * that case.
449140cdc509SFilipe Manana 		 */
449240cdc509SFilipe Manana 		if (!(space_info->flags & BTRFS_BLOCK_GROUP_METADATA) ||
449340cdc509SFilipe Manana 		    !BTRFS_FS_LOG_CLEANUP_ERROR(info)) {
449440cdc509SFilipe Manana 			if (WARN_ON(space_info->bytes_reserved > 0))
449540cdc509SFilipe Manana 				btrfs_dump_space_info(info, space_info, 0, 0);
449640cdc509SFilipe Manana 		}
449740cdc509SFilipe Manana 
4498d611add4SFilipe Manana 		WARN_ON(space_info->reclaim_size > 0);
44993e43c279SJosef Bacik 		list_del(&space_info->list);
45003e43c279SJosef Bacik 		btrfs_sysfs_remove_space_info(space_info);
45013e43c279SJosef Bacik 	}
45023e43c279SJosef Bacik 	return 0;
45033e43c279SJosef Bacik }
4504684b752bSFilipe Manana 
btrfs_freeze_block_group(struct btrfs_block_group * cache)4505684b752bSFilipe Manana void btrfs_freeze_block_group(struct btrfs_block_group *cache)
4506684b752bSFilipe Manana {
4507684b752bSFilipe Manana 	atomic_inc(&cache->frozen);
4508684b752bSFilipe Manana }
4509684b752bSFilipe Manana 
btrfs_unfreeze_block_group(struct btrfs_block_group * block_group)4510684b752bSFilipe Manana void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
4511684b752bSFilipe Manana {
4512684b752bSFilipe Manana 	struct btrfs_fs_info *fs_info = block_group->fs_info;
4513684b752bSFilipe Manana 	bool cleanup;
4514684b752bSFilipe Manana 
4515684b752bSFilipe Manana 	spin_lock(&block_group->lock);
4516684b752bSFilipe Manana 	cleanup = (atomic_dec_and_test(&block_group->frozen) &&
45173349b57fSJosef Bacik 		   test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags));
4518684b752bSFilipe Manana 	spin_unlock(&block_group->lock);
4519684b752bSFilipe Manana 
4520684b752bSFilipe Manana 	if (cleanup) {
45217dc66abbSFilipe Manana 		struct btrfs_chunk_map *map;
4522684b752bSFilipe Manana 
45237dc66abbSFilipe Manana 		map = btrfs_find_chunk_map(fs_info, block_group->start, 1);
45247dc66abbSFilipe Manana 		/* Logic error, can't happen. */
45257dc66abbSFilipe Manana 		ASSERT(map);
45267dc66abbSFilipe Manana 
45277dc66abbSFilipe Manana 		btrfs_remove_chunk_map(fs_info, map);
45287dc66abbSFilipe Manana 
45297dc66abbSFilipe Manana 		/* Once for our lookup reference. */
45307dc66abbSFilipe Manana 		btrfs_free_chunk_map(map);
4531684b752bSFilipe Manana 
4532684b752bSFilipe Manana 		/*
4533684b752bSFilipe Manana 		 * We may have left one free space entry and other possible
4534684b752bSFilipe Manana 		 * tasks trimming this block group have left 1 entry each one.
4535684b752bSFilipe Manana 		 * Free them if any.
4536684b752bSFilipe Manana 		 */
4537fc80f7acSJosef Bacik 		btrfs_remove_free_space_cache(block_group);
4538684b752bSFilipe Manana 	}
4539684b752bSFilipe Manana }
4540195a49eaSFilipe Manana 
btrfs_inc_block_group_swap_extents(struct btrfs_block_group * bg)4541195a49eaSFilipe Manana bool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg)
4542195a49eaSFilipe Manana {
4543195a49eaSFilipe Manana 	bool ret = true;
4544195a49eaSFilipe Manana 
4545195a49eaSFilipe Manana 	spin_lock(&bg->lock);
4546195a49eaSFilipe Manana 	if (bg->ro)
4547195a49eaSFilipe Manana 		ret = false;
4548195a49eaSFilipe Manana 	else
4549195a49eaSFilipe Manana 		bg->swap_extents++;
4550195a49eaSFilipe Manana 	spin_unlock(&bg->lock);
4551195a49eaSFilipe Manana 
4552195a49eaSFilipe Manana 	return ret;
4553195a49eaSFilipe Manana }
4554195a49eaSFilipe Manana 
btrfs_dec_block_group_swap_extents(struct btrfs_block_group * bg,int amount)4555195a49eaSFilipe Manana void btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount)
4556195a49eaSFilipe Manana {
4557195a49eaSFilipe Manana 	spin_lock(&bg->lock);
4558195a49eaSFilipe Manana 	ASSERT(!bg->ro);
4559195a49eaSFilipe Manana 	ASSERT(bg->swap_extents >= amount);
4560195a49eaSFilipe Manana 	bg->swap_extents -= amount;
4561195a49eaSFilipe Manana 	spin_unlock(&bg->lock);
4562195a49eaSFilipe Manana }
456352bb7a21SBoris Burkov 
btrfs_calc_block_group_size_class(u64 size)456452bb7a21SBoris Burkov enum btrfs_block_group_size_class btrfs_calc_block_group_size_class(u64 size)
456552bb7a21SBoris Burkov {
456652bb7a21SBoris Burkov 	if (size <= SZ_128K)
456752bb7a21SBoris Burkov 		return BTRFS_BG_SZ_SMALL;
456852bb7a21SBoris Burkov 	if (size <= SZ_8M)
456952bb7a21SBoris Burkov 		return BTRFS_BG_SZ_MEDIUM;
457052bb7a21SBoris Burkov 	return BTRFS_BG_SZ_LARGE;
457152bb7a21SBoris Burkov }
457252bb7a21SBoris Burkov 
457352bb7a21SBoris Burkov /*
457452bb7a21SBoris Burkov  * Handle a block group allocating an extent in a size class
457552bb7a21SBoris Burkov  *
457652bb7a21SBoris Burkov  * @bg:				The block group we allocated in.
457752bb7a21SBoris Burkov  * @size_class:			The size class of the allocation.
457852bb7a21SBoris Burkov  * @force_wrong_size_class:	Whether we are desperate enough to allow
457952bb7a21SBoris Burkov  *				mismatched size classes.
458052bb7a21SBoris Burkov  *
458152bb7a21SBoris Burkov  * Returns: 0 if the size class was valid for this block_group, -EAGAIN in the
458252bb7a21SBoris Burkov  * case of a race that leads to the wrong size class without
458352bb7a21SBoris Burkov  * force_wrong_size_class set.
458452bb7a21SBoris Burkov  *
458552bb7a21SBoris Burkov  * find_free_extent will skip block groups with a mismatched size class until
458652bb7a21SBoris Burkov  * it really needs to avoid ENOSPC. In that case it will set
458752bb7a21SBoris Burkov  * force_wrong_size_class. However, if a block group is newly allocated and
458852bb7a21SBoris Burkov  * doesn't yet have a size class, then it is possible for two allocations of
458952bb7a21SBoris Burkov  * different sizes to race and both try to use it. The loser is caught here and
459052bb7a21SBoris Burkov  * has to retry.
459152bb7a21SBoris Burkov  */
btrfs_use_block_group_size_class(struct btrfs_block_group * bg,enum btrfs_block_group_size_class size_class,bool force_wrong_size_class)459252bb7a21SBoris Burkov int btrfs_use_block_group_size_class(struct btrfs_block_group *bg,
459352bb7a21SBoris Burkov 				     enum btrfs_block_group_size_class size_class,
459452bb7a21SBoris Burkov 				     bool force_wrong_size_class)
459552bb7a21SBoris Burkov {
459652bb7a21SBoris Burkov 	ASSERT(size_class != BTRFS_BG_SZ_NONE);
459752bb7a21SBoris Burkov 
459852bb7a21SBoris Burkov 	/* The new allocation is in the right size class, do nothing */
459952bb7a21SBoris Burkov 	if (bg->size_class == size_class)
460052bb7a21SBoris Burkov 		return 0;
460152bb7a21SBoris Burkov 	/*
460252bb7a21SBoris Burkov 	 * The new allocation is in a mismatched size class.
460352bb7a21SBoris Burkov 	 * This means one of two things:
460452bb7a21SBoris Burkov 	 *
460552bb7a21SBoris Burkov 	 * 1. Two tasks in find_free_extent for different size_classes raced
460652bb7a21SBoris Burkov 	 *    and hit the same empty block_group. Make the loser try again.
460752bb7a21SBoris Burkov 	 * 2. A call to find_free_extent got desperate enough to set
460852bb7a21SBoris Burkov 	 *    'force_wrong_slab'. Don't change the size_class, but allow the
460952bb7a21SBoris Burkov 	 *    allocation.
461052bb7a21SBoris Burkov 	 */
461152bb7a21SBoris Burkov 	if (bg->size_class != BTRFS_BG_SZ_NONE) {
461252bb7a21SBoris Burkov 		if (force_wrong_size_class)
461352bb7a21SBoris Burkov 			return 0;
461452bb7a21SBoris Burkov 		return -EAGAIN;
461552bb7a21SBoris Burkov 	}
461652bb7a21SBoris Burkov 	/*
461752bb7a21SBoris Burkov 	 * The happy new block group case: the new allocation is the first
461852bb7a21SBoris Burkov 	 * one in the block_group so we set size_class.
461952bb7a21SBoris Burkov 	 */
462052bb7a21SBoris Burkov 	bg->size_class = size_class;
462152bb7a21SBoris Burkov 
462252bb7a21SBoris Burkov 	return 0;
462352bb7a21SBoris Burkov }
4624cb0922f2SBoris Burkov 
btrfs_block_group_should_use_size_class(const struct btrfs_block_group * bg)4625*ca283ea9SDavid Sterba bool btrfs_block_group_should_use_size_class(const struct btrfs_block_group *bg)
4626cb0922f2SBoris Burkov {
4627cb0922f2SBoris Burkov 	if (btrfs_is_zoned(bg->fs_info))
4628cb0922f2SBoris Burkov 		return false;
4629cb0922f2SBoris Burkov 	if (!btrfs_is_block_group_data_only(bg))
4630cb0922f2SBoris Burkov 		return false;
4631cb0922f2SBoris Burkov 	return true;
4632cb0922f2SBoris Burkov }
4633