xref: /openbmc/linux/fs/btrfs/block-group.c (revision cc4804bfd6392bcb626a687689b36fd9cda87c11)
12e405ad8SJosef Bacik // SPDX-License-Identifier: GPL-2.0
22e405ad8SJosef Bacik 
32ca0ec77SJohannes Thumshirn #include <linux/list_sort.h>
4784352feSDavid Sterba #include "misc.h"
52e405ad8SJosef Bacik #include "ctree.h"
62e405ad8SJosef Bacik #include "block-group.h"
73eeb3226SJosef Bacik #include "space-info.h"
89f21246dSJosef Bacik #include "disk-io.h"
99f21246dSJosef Bacik #include "free-space-cache.h"
109f21246dSJosef Bacik #include "free-space-tree.h"
11e3e0520bSJosef Bacik #include "volumes.h"
12e3e0520bSJosef Bacik #include "transaction.h"
13e3e0520bSJosef Bacik #include "ref-verify.h"
144358d963SJosef Bacik #include "sysfs.h"
154358d963SJosef Bacik #include "tree-log.h"
1677745c05SJosef Bacik #include "delalloc-space.h"
17b0643e59SDennis Zhou #include "discard.h"
1896a14336SNikolay Borisov #include "raid56.h"
1908e11a3dSNaohiro Aota #include "zoned.h"
202e405ad8SJosef Bacik 
2106d61cb1SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
2206d61cb1SJosef Bacik int btrfs_should_fragment_free_space(struct btrfs_block_group *block_group)
2306d61cb1SJosef Bacik {
2406d61cb1SJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2506d61cb1SJosef Bacik 
2606d61cb1SJosef Bacik 	return (btrfs_test_opt(fs_info, FRAGMENT_METADATA) &&
2706d61cb1SJosef Bacik 		block_group->flags & BTRFS_BLOCK_GROUP_METADATA) ||
2806d61cb1SJosef Bacik 	       (btrfs_test_opt(fs_info, FRAGMENT_DATA) &&
2906d61cb1SJosef Bacik 		block_group->flags &  BTRFS_BLOCK_GROUP_DATA);
3006d61cb1SJosef Bacik }
3106d61cb1SJosef Bacik #endif
3206d61cb1SJosef Bacik 
33878d7b67SJosef Bacik /*
34878d7b67SJosef Bacik  * Return target flags in extended format or 0 if restripe for this chunk_type
35878d7b67SJosef Bacik  * is not in progress
36878d7b67SJosef Bacik  *
37878d7b67SJosef Bacik  * Should be called with balance_lock held
38878d7b67SJosef Bacik  */
39e11c0406SJosef Bacik static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
40878d7b67SJosef Bacik {
41878d7b67SJosef Bacik 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
42878d7b67SJosef Bacik 	u64 target = 0;
43878d7b67SJosef Bacik 
44878d7b67SJosef Bacik 	if (!bctl)
45878d7b67SJosef Bacik 		return 0;
46878d7b67SJosef Bacik 
47878d7b67SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA &&
48878d7b67SJosef Bacik 	    bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
49878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
50878d7b67SJosef Bacik 	} else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
51878d7b67SJosef Bacik 		   bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
52878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
53878d7b67SJosef Bacik 	} else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
54878d7b67SJosef Bacik 		   bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
55878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
56878d7b67SJosef Bacik 	}
57878d7b67SJosef Bacik 
58878d7b67SJosef Bacik 	return target;
59878d7b67SJosef Bacik }
60878d7b67SJosef Bacik 
61878d7b67SJosef Bacik /*
62878d7b67SJosef Bacik  * @flags: available profiles in extended format (see ctree.h)
63878d7b67SJosef Bacik  *
64878d7b67SJosef Bacik  * Return reduced profile in chunk format.  If profile changing is in progress
65878d7b67SJosef Bacik  * (either running or paused) picks the target profile (if it's already
66878d7b67SJosef Bacik  * available), otherwise falls back to plain reducing.
67878d7b67SJosef Bacik  */
68878d7b67SJosef Bacik static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
69878d7b67SJosef Bacik {
70878d7b67SJosef Bacik 	u64 num_devices = fs_info->fs_devices->rw_devices;
71878d7b67SJosef Bacik 	u64 target;
72878d7b67SJosef Bacik 	u64 raid_type;
73878d7b67SJosef Bacik 	u64 allowed = 0;
74878d7b67SJosef Bacik 
75878d7b67SJosef Bacik 	/*
76878d7b67SJosef Bacik 	 * See if restripe for this chunk_type is in progress, if so try to
77878d7b67SJosef Bacik 	 * reduce to the target profile
78878d7b67SJosef Bacik 	 */
79878d7b67SJosef Bacik 	spin_lock(&fs_info->balance_lock);
80e11c0406SJosef Bacik 	target = get_restripe_target(fs_info, flags);
81878d7b67SJosef Bacik 	if (target) {
82878d7b67SJosef Bacik 		spin_unlock(&fs_info->balance_lock);
83878d7b67SJosef Bacik 		return extended_to_chunk(target);
84878d7b67SJosef Bacik 	}
85878d7b67SJosef Bacik 	spin_unlock(&fs_info->balance_lock);
86878d7b67SJosef Bacik 
87878d7b67SJosef Bacik 	/* First, mask out the RAID levels which aren't possible */
88878d7b67SJosef Bacik 	for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
89878d7b67SJosef Bacik 		if (num_devices >= btrfs_raid_array[raid_type].devs_min)
90878d7b67SJosef Bacik 			allowed |= btrfs_raid_array[raid_type].bg_flag;
91878d7b67SJosef Bacik 	}
92878d7b67SJosef Bacik 	allowed &= flags;
93878d7b67SJosef Bacik 
94878d7b67SJosef Bacik 	if (allowed & BTRFS_BLOCK_GROUP_RAID6)
95878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID6;
96878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
97878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID5;
98878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
99878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID10;
100878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
101878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID1;
102878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
103878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID0;
104878d7b67SJosef Bacik 
105878d7b67SJosef Bacik 	flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
106878d7b67SJosef Bacik 
107878d7b67SJosef Bacik 	return extended_to_chunk(flags | allowed);
108878d7b67SJosef Bacik }
109878d7b67SJosef Bacik 
110ef0a82daSJohannes Thumshirn u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
111878d7b67SJosef Bacik {
112878d7b67SJosef Bacik 	unsigned seq;
113878d7b67SJosef Bacik 	u64 flags;
114878d7b67SJosef Bacik 
115878d7b67SJosef Bacik 	do {
116878d7b67SJosef Bacik 		flags = orig_flags;
117878d7b67SJosef Bacik 		seq = read_seqbegin(&fs_info->profiles_lock);
118878d7b67SJosef Bacik 
119878d7b67SJosef Bacik 		if (flags & BTRFS_BLOCK_GROUP_DATA)
120878d7b67SJosef Bacik 			flags |= fs_info->avail_data_alloc_bits;
121878d7b67SJosef Bacik 		else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
122878d7b67SJosef Bacik 			flags |= fs_info->avail_system_alloc_bits;
123878d7b67SJosef Bacik 		else if (flags & BTRFS_BLOCK_GROUP_METADATA)
124878d7b67SJosef Bacik 			flags |= fs_info->avail_metadata_alloc_bits;
125878d7b67SJosef Bacik 	} while (read_seqretry(&fs_info->profiles_lock, seq));
126878d7b67SJosef Bacik 
127878d7b67SJosef Bacik 	return btrfs_reduce_alloc_profile(fs_info, flags);
128878d7b67SJosef Bacik }
129878d7b67SJosef Bacik 
13032da5386SDavid Sterba void btrfs_get_block_group(struct btrfs_block_group *cache)
1313cad1284SJosef Bacik {
13248aaeebeSJosef Bacik 	refcount_inc(&cache->refs);
1333cad1284SJosef Bacik }
1343cad1284SJosef Bacik 
13532da5386SDavid Sterba void btrfs_put_block_group(struct btrfs_block_group *cache)
1363cad1284SJosef Bacik {
13748aaeebeSJosef Bacik 	if (refcount_dec_and_test(&cache->refs)) {
1383cad1284SJosef Bacik 		WARN_ON(cache->pinned > 0);
13940cdc509SFilipe Manana 		/*
14040cdc509SFilipe Manana 		 * If there was a failure to cleanup a log tree, very likely due
14140cdc509SFilipe Manana 		 * to an IO failure on a writeback attempt of one or more of its
14240cdc509SFilipe Manana 		 * extent buffers, we could not do proper (and cheap) unaccounting
14340cdc509SFilipe Manana 		 * of their reserved space, so don't warn on reserved > 0 in that
14440cdc509SFilipe Manana 		 * case.
14540cdc509SFilipe Manana 		 */
14640cdc509SFilipe Manana 		if (!(cache->flags & BTRFS_BLOCK_GROUP_METADATA) ||
14740cdc509SFilipe Manana 		    !BTRFS_FS_LOG_CLEANUP_ERROR(cache->fs_info))
1483cad1284SJosef Bacik 			WARN_ON(cache->reserved > 0);
1493cad1284SJosef Bacik 
1503cad1284SJosef Bacik 		/*
151b0643e59SDennis Zhou 		 * A block_group shouldn't be on the discard_list anymore.
152b0643e59SDennis Zhou 		 * Remove the block_group from the discard_list to prevent us
153b0643e59SDennis Zhou 		 * from causing a panic due to NULL pointer dereference.
154b0643e59SDennis Zhou 		 */
155b0643e59SDennis Zhou 		if (WARN_ON(!list_empty(&cache->discard_list)))
156b0643e59SDennis Zhou 			btrfs_discard_cancel_work(&cache->fs_info->discard_ctl,
157b0643e59SDennis Zhou 						  cache);
158b0643e59SDennis Zhou 
159b0643e59SDennis Zhou 		/*
1603cad1284SJosef Bacik 		 * If not empty, someone is still holding mutex of
1613cad1284SJosef Bacik 		 * full_stripe_lock, which can only be released by caller.
1623cad1284SJosef Bacik 		 * And it will definitely cause use-after-free when caller
1633cad1284SJosef Bacik 		 * tries to release full stripe lock.
1643cad1284SJosef Bacik 		 *
1653cad1284SJosef Bacik 		 * No better way to resolve, but only to warn.
1663cad1284SJosef Bacik 		 */
1673cad1284SJosef Bacik 		WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
1683cad1284SJosef Bacik 		kfree(cache->free_space_ctl);
169dafc340dSNaohiro Aota 		kfree(cache->physical_map);
1703cad1284SJosef Bacik 		kfree(cache);
1713cad1284SJosef Bacik 	}
1723cad1284SJosef Bacik }
1733cad1284SJosef Bacik 
1742e405ad8SJosef Bacik /*
1754358d963SJosef Bacik  * This adds the block group to the fs_info rb tree for the block group cache
1764358d963SJosef Bacik  */
1774358d963SJosef Bacik static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
17832da5386SDavid Sterba 				       struct btrfs_block_group *block_group)
1794358d963SJosef Bacik {
1804358d963SJosef Bacik 	struct rb_node **p;
1814358d963SJosef Bacik 	struct rb_node *parent = NULL;
18232da5386SDavid Sterba 	struct btrfs_block_group *cache;
18308dddb29SFilipe Manana 	bool leftmost = true;
1844358d963SJosef Bacik 
1859afc6649SQu Wenruo 	ASSERT(block_group->length != 0);
1869afc6649SQu Wenruo 
18716b0c258SFilipe Manana 	write_lock(&info->block_group_cache_lock);
18808dddb29SFilipe Manana 	p = &info->block_group_cache_tree.rb_root.rb_node;
1894358d963SJosef Bacik 
1904358d963SJosef Bacik 	while (*p) {
1914358d963SJosef Bacik 		parent = *p;
19232da5386SDavid Sterba 		cache = rb_entry(parent, struct btrfs_block_group, cache_node);
193b3470b5dSDavid Sterba 		if (block_group->start < cache->start) {
1944358d963SJosef Bacik 			p = &(*p)->rb_left;
195b3470b5dSDavid Sterba 		} else if (block_group->start > cache->start) {
1964358d963SJosef Bacik 			p = &(*p)->rb_right;
19708dddb29SFilipe Manana 			leftmost = false;
1984358d963SJosef Bacik 		} else {
19916b0c258SFilipe Manana 			write_unlock(&info->block_group_cache_lock);
2004358d963SJosef Bacik 			return -EEXIST;
2014358d963SJosef Bacik 		}
2024358d963SJosef Bacik 	}
2034358d963SJosef Bacik 
2044358d963SJosef Bacik 	rb_link_node(&block_group->cache_node, parent, p);
20508dddb29SFilipe Manana 	rb_insert_color_cached(&block_group->cache_node,
20608dddb29SFilipe Manana 			       &info->block_group_cache_tree, leftmost);
2074358d963SJosef Bacik 
20816b0c258SFilipe Manana 	write_unlock(&info->block_group_cache_lock);
2094358d963SJosef Bacik 
2104358d963SJosef Bacik 	return 0;
2114358d963SJosef Bacik }
2124358d963SJosef Bacik 
2134358d963SJosef Bacik /*
2142e405ad8SJosef Bacik  * This will return the block group at or after bytenr if contains is 0, else
2152e405ad8SJosef Bacik  * it will return the block group that contains the bytenr
2162e405ad8SJosef Bacik  */
21732da5386SDavid Sterba static struct btrfs_block_group *block_group_cache_tree_search(
2182e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr, int contains)
2192e405ad8SJosef Bacik {
22032da5386SDavid Sterba 	struct btrfs_block_group *cache, *ret = NULL;
2212e405ad8SJosef Bacik 	struct rb_node *n;
2222e405ad8SJosef Bacik 	u64 end, start;
2232e405ad8SJosef Bacik 
22416b0c258SFilipe Manana 	read_lock(&info->block_group_cache_lock);
22508dddb29SFilipe Manana 	n = info->block_group_cache_tree.rb_root.rb_node;
2262e405ad8SJosef Bacik 
2272e405ad8SJosef Bacik 	while (n) {
22832da5386SDavid Sterba 		cache = rb_entry(n, struct btrfs_block_group, cache_node);
229b3470b5dSDavid Sterba 		end = cache->start + cache->length - 1;
230b3470b5dSDavid Sterba 		start = cache->start;
2312e405ad8SJosef Bacik 
2322e405ad8SJosef Bacik 		if (bytenr < start) {
233b3470b5dSDavid Sterba 			if (!contains && (!ret || start < ret->start))
2342e405ad8SJosef Bacik 				ret = cache;
2352e405ad8SJosef Bacik 			n = n->rb_left;
2362e405ad8SJosef Bacik 		} else if (bytenr > start) {
2372e405ad8SJosef Bacik 			if (contains && bytenr <= end) {
2382e405ad8SJosef Bacik 				ret = cache;
2392e405ad8SJosef Bacik 				break;
2402e405ad8SJosef Bacik 			}
2412e405ad8SJosef Bacik 			n = n->rb_right;
2422e405ad8SJosef Bacik 		} else {
2432e405ad8SJosef Bacik 			ret = cache;
2442e405ad8SJosef Bacik 			break;
2452e405ad8SJosef Bacik 		}
2462e405ad8SJosef Bacik 	}
24708dddb29SFilipe Manana 	if (ret)
2482e405ad8SJosef Bacik 		btrfs_get_block_group(ret);
24916b0c258SFilipe Manana 	read_unlock(&info->block_group_cache_lock);
2502e405ad8SJosef Bacik 
2512e405ad8SJosef Bacik 	return ret;
2522e405ad8SJosef Bacik }
2532e405ad8SJosef Bacik 
2542e405ad8SJosef Bacik /*
2552e405ad8SJosef Bacik  * Return the block group that starts at or after bytenr
2562e405ad8SJosef Bacik  */
25732da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_first_block_group(
2582e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr)
2592e405ad8SJosef Bacik {
2602e405ad8SJosef Bacik 	return block_group_cache_tree_search(info, bytenr, 0);
2612e405ad8SJosef Bacik }
2622e405ad8SJosef Bacik 
2632e405ad8SJosef Bacik /*
2642e405ad8SJosef Bacik  * Return the block group that contains the given bytenr
2652e405ad8SJosef Bacik  */
26632da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_block_group(
2672e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr)
2682e405ad8SJosef Bacik {
2692e405ad8SJosef Bacik 	return block_group_cache_tree_search(info, bytenr, 1);
2702e405ad8SJosef Bacik }
2712e405ad8SJosef Bacik 
27232da5386SDavid Sterba struct btrfs_block_group *btrfs_next_block_group(
27332da5386SDavid Sterba 		struct btrfs_block_group *cache)
2742e405ad8SJosef Bacik {
2752e405ad8SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
2762e405ad8SJosef Bacik 	struct rb_node *node;
2772e405ad8SJosef Bacik 
27816b0c258SFilipe Manana 	read_lock(&fs_info->block_group_cache_lock);
2792e405ad8SJosef Bacik 
2802e405ad8SJosef Bacik 	/* If our block group was removed, we need a full search. */
2812e405ad8SJosef Bacik 	if (RB_EMPTY_NODE(&cache->cache_node)) {
282b3470b5dSDavid Sterba 		const u64 next_bytenr = cache->start + cache->length;
2832e405ad8SJosef Bacik 
28416b0c258SFilipe Manana 		read_unlock(&fs_info->block_group_cache_lock);
2852e405ad8SJosef Bacik 		btrfs_put_block_group(cache);
2868b01f931SFilipe Manana 		return btrfs_lookup_first_block_group(fs_info, next_bytenr);
2872e405ad8SJosef Bacik 	}
2882e405ad8SJosef Bacik 	node = rb_next(&cache->cache_node);
2892e405ad8SJosef Bacik 	btrfs_put_block_group(cache);
2902e405ad8SJosef Bacik 	if (node) {
29132da5386SDavid Sterba 		cache = rb_entry(node, struct btrfs_block_group, cache_node);
2922e405ad8SJosef Bacik 		btrfs_get_block_group(cache);
2932e405ad8SJosef Bacik 	} else
2942e405ad8SJosef Bacik 		cache = NULL;
29516b0c258SFilipe Manana 	read_unlock(&fs_info->block_group_cache_lock);
2962e405ad8SJosef Bacik 	return cache;
2972e405ad8SJosef Bacik }
2983eeb3226SJosef Bacik 
2992306e83eSFilipe Manana /**
3002306e83eSFilipe Manana  * Check if we can do a NOCOW write for a given extent.
3012306e83eSFilipe Manana  *
3022306e83eSFilipe Manana  * @fs_info:       The filesystem information object.
3032306e83eSFilipe Manana  * @bytenr:        Logical start address of the extent.
3042306e83eSFilipe Manana  *
3052306e83eSFilipe Manana  * Check if we can do a NOCOW write for the given extent, and increments the
3062306e83eSFilipe Manana  * number of NOCOW writers in the block group that contains the extent, as long
3072306e83eSFilipe Manana  * as the block group exists and it's currently not in read-only mode.
3082306e83eSFilipe Manana  *
3092306e83eSFilipe Manana  * Returns: A non-NULL block group pointer if we can do a NOCOW write, the caller
3102306e83eSFilipe Manana  *          is responsible for calling btrfs_dec_nocow_writers() later.
3112306e83eSFilipe Manana  *
3122306e83eSFilipe Manana  *          Or NULL if we can not do a NOCOW write
3132306e83eSFilipe Manana  */
3142306e83eSFilipe Manana struct btrfs_block_group *btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info,
3152306e83eSFilipe Manana 						  u64 bytenr)
3163eeb3226SJosef Bacik {
31732da5386SDavid Sterba 	struct btrfs_block_group *bg;
3182306e83eSFilipe Manana 	bool can_nocow = true;
3193eeb3226SJosef Bacik 
3203eeb3226SJosef Bacik 	bg = btrfs_lookup_block_group(fs_info, bytenr);
3213eeb3226SJosef Bacik 	if (!bg)
3222306e83eSFilipe Manana 		return NULL;
3233eeb3226SJosef Bacik 
3243eeb3226SJosef Bacik 	spin_lock(&bg->lock);
3253eeb3226SJosef Bacik 	if (bg->ro)
3262306e83eSFilipe Manana 		can_nocow = false;
3273eeb3226SJosef Bacik 	else
3283eeb3226SJosef Bacik 		atomic_inc(&bg->nocow_writers);
3293eeb3226SJosef Bacik 	spin_unlock(&bg->lock);
3303eeb3226SJosef Bacik 
3312306e83eSFilipe Manana 	if (!can_nocow) {
3323eeb3226SJosef Bacik 		btrfs_put_block_group(bg);
3332306e83eSFilipe Manana 		return NULL;
3343eeb3226SJosef Bacik 	}
3353eeb3226SJosef Bacik 
3362306e83eSFilipe Manana 	/* No put on block group, done by btrfs_dec_nocow_writers(). */
3372306e83eSFilipe Manana 	return bg;
3382306e83eSFilipe Manana }
3393eeb3226SJosef Bacik 
3402306e83eSFilipe Manana /**
3412306e83eSFilipe Manana  * Decrement the number of NOCOW writers in a block group.
3422306e83eSFilipe Manana  *
3432306e83eSFilipe Manana  * @bg:       The block group.
3442306e83eSFilipe Manana  *
3452306e83eSFilipe Manana  * This is meant to be called after a previous call to btrfs_inc_nocow_writers(),
3462306e83eSFilipe Manana  * and on the block group returned by that call. Typically this is called after
3472306e83eSFilipe Manana  * creating an ordered extent for a NOCOW write, to prevent races with scrub and
3482306e83eSFilipe Manana  * relocation.
3492306e83eSFilipe Manana  *
3502306e83eSFilipe Manana  * After this call, the caller should not use the block group anymore. It it wants
3512306e83eSFilipe Manana  * to use it, then it should get a reference on it before calling this function.
3522306e83eSFilipe Manana  */
3532306e83eSFilipe Manana void btrfs_dec_nocow_writers(struct btrfs_block_group *bg)
3542306e83eSFilipe Manana {
3553eeb3226SJosef Bacik 	if (atomic_dec_and_test(&bg->nocow_writers))
3563eeb3226SJosef Bacik 		wake_up_var(&bg->nocow_writers);
3572306e83eSFilipe Manana 
3582306e83eSFilipe Manana 	/* For the lookup done by a previous call to btrfs_inc_nocow_writers(). */
3593eeb3226SJosef Bacik 	btrfs_put_block_group(bg);
3603eeb3226SJosef Bacik }
3613eeb3226SJosef Bacik 
36232da5386SDavid Sterba void btrfs_wait_nocow_writers(struct btrfs_block_group *bg)
3633eeb3226SJosef Bacik {
3643eeb3226SJosef Bacik 	wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
3653eeb3226SJosef Bacik }
3663eeb3226SJosef Bacik 
3673eeb3226SJosef Bacik void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
3683eeb3226SJosef Bacik 					const u64 start)
3693eeb3226SJosef Bacik {
37032da5386SDavid Sterba 	struct btrfs_block_group *bg;
3713eeb3226SJosef Bacik 
3723eeb3226SJosef Bacik 	bg = btrfs_lookup_block_group(fs_info, start);
3733eeb3226SJosef Bacik 	ASSERT(bg);
3743eeb3226SJosef Bacik 	if (atomic_dec_and_test(&bg->reservations))
3753eeb3226SJosef Bacik 		wake_up_var(&bg->reservations);
3763eeb3226SJosef Bacik 	btrfs_put_block_group(bg);
3773eeb3226SJosef Bacik }
3783eeb3226SJosef Bacik 
37932da5386SDavid Sterba void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg)
3803eeb3226SJosef Bacik {
3813eeb3226SJosef Bacik 	struct btrfs_space_info *space_info = bg->space_info;
3823eeb3226SJosef Bacik 
3833eeb3226SJosef Bacik 	ASSERT(bg->ro);
3843eeb3226SJosef Bacik 
3853eeb3226SJosef Bacik 	if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
3863eeb3226SJosef Bacik 		return;
3873eeb3226SJosef Bacik 
3883eeb3226SJosef Bacik 	/*
3893eeb3226SJosef Bacik 	 * Our block group is read only but before we set it to read only,
3903eeb3226SJosef Bacik 	 * some task might have had allocated an extent from it already, but it
3913eeb3226SJosef Bacik 	 * has not yet created a respective ordered extent (and added it to a
3923eeb3226SJosef Bacik 	 * root's list of ordered extents).
3933eeb3226SJosef Bacik 	 * Therefore wait for any task currently allocating extents, since the
3943eeb3226SJosef Bacik 	 * block group's reservations counter is incremented while a read lock
3953eeb3226SJosef Bacik 	 * on the groups' semaphore is held and decremented after releasing
3963eeb3226SJosef Bacik 	 * the read access on that semaphore and creating the ordered extent.
3973eeb3226SJosef Bacik 	 */
3983eeb3226SJosef Bacik 	down_write(&space_info->groups_sem);
3993eeb3226SJosef Bacik 	up_write(&space_info->groups_sem);
4003eeb3226SJosef Bacik 
4013eeb3226SJosef Bacik 	wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
4023eeb3226SJosef Bacik }
4039f21246dSJosef Bacik 
4049f21246dSJosef Bacik struct btrfs_caching_control *btrfs_get_caching_control(
40532da5386SDavid Sterba 		struct btrfs_block_group *cache)
4069f21246dSJosef Bacik {
4079f21246dSJosef Bacik 	struct btrfs_caching_control *ctl;
4089f21246dSJosef Bacik 
4099f21246dSJosef Bacik 	spin_lock(&cache->lock);
4109f21246dSJosef Bacik 	if (!cache->caching_ctl) {
4119f21246dSJosef Bacik 		spin_unlock(&cache->lock);
4129f21246dSJosef Bacik 		return NULL;
4139f21246dSJosef Bacik 	}
4149f21246dSJosef Bacik 
4159f21246dSJosef Bacik 	ctl = cache->caching_ctl;
4169f21246dSJosef Bacik 	refcount_inc(&ctl->count);
4179f21246dSJosef Bacik 	spin_unlock(&cache->lock);
4189f21246dSJosef Bacik 	return ctl;
4199f21246dSJosef Bacik }
4209f21246dSJosef Bacik 
4219f21246dSJosef Bacik void btrfs_put_caching_control(struct btrfs_caching_control *ctl)
4229f21246dSJosef Bacik {
4239f21246dSJosef Bacik 	if (refcount_dec_and_test(&ctl->count))
4249f21246dSJosef Bacik 		kfree(ctl);
4259f21246dSJosef Bacik }
4269f21246dSJosef Bacik 
4279f21246dSJosef Bacik /*
4289f21246dSJosef Bacik  * When we wait for progress in the block group caching, its because our
4299f21246dSJosef Bacik  * allocation attempt failed at least once.  So, we must sleep and let some
4309f21246dSJosef Bacik  * progress happen before we try again.
4319f21246dSJosef Bacik  *
4329f21246dSJosef Bacik  * This function will sleep at least once waiting for new free space to show
4339f21246dSJosef Bacik  * up, and then it will check the block group free space numbers for our min
4349f21246dSJosef Bacik  * num_bytes.  Another option is to have it go ahead and look in the rbtree for
4359f21246dSJosef Bacik  * a free extent of a given size, but this is a good start.
4369f21246dSJosef Bacik  *
4379f21246dSJosef Bacik  * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
4389f21246dSJosef Bacik  * any of the information in this block group.
4399f21246dSJosef Bacik  */
44032da5386SDavid Sterba void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache,
4419f21246dSJosef Bacik 					   u64 num_bytes)
4429f21246dSJosef Bacik {
4439f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
4449f21246dSJosef Bacik 
4459f21246dSJosef Bacik 	caching_ctl = btrfs_get_caching_control(cache);
4469f21246dSJosef Bacik 	if (!caching_ctl)
4479f21246dSJosef Bacik 		return;
4489f21246dSJosef Bacik 
44932da5386SDavid Sterba 	wait_event(caching_ctl->wait, btrfs_block_group_done(cache) ||
4509f21246dSJosef Bacik 		   (cache->free_space_ctl->free_space >= num_bytes));
4519f21246dSJosef Bacik 
4529f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
4539f21246dSJosef Bacik }
4549f21246dSJosef Bacik 
455ced8ecf0SOmar Sandoval static int btrfs_caching_ctl_wait_done(struct btrfs_block_group *cache,
456ced8ecf0SOmar Sandoval 				       struct btrfs_caching_control *caching_ctl)
457ced8ecf0SOmar Sandoval {
458ced8ecf0SOmar Sandoval 	wait_event(caching_ctl->wait, btrfs_block_group_done(cache));
459ced8ecf0SOmar Sandoval 	return cache->cached == BTRFS_CACHE_ERROR ? -EIO : 0;
460ced8ecf0SOmar Sandoval }
461ced8ecf0SOmar Sandoval 
462ced8ecf0SOmar Sandoval static int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache)
4639f21246dSJosef Bacik {
4649f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
465ced8ecf0SOmar Sandoval 	int ret;
4669f21246dSJosef Bacik 
4679f21246dSJosef Bacik 	caching_ctl = btrfs_get_caching_control(cache);
4689f21246dSJosef Bacik 	if (!caching_ctl)
4699f21246dSJosef Bacik 		return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
470ced8ecf0SOmar Sandoval 	ret = btrfs_caching_ctl_wait_done(cache, caching_ctl);
4719f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
4729f21246dSJosef Bacik 	return ret;
4739f21246dSJosef Bacik }
4749f21246dSJosef Bacik 
4759f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
47632da5386SDavid Sterba static void fragment_free_space(struct btrfs_block_group *block_group)
4779f21246dSJosef Bacik {
4789f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
479b3470b5dSDavid Sterba 	u64 start = block_group->start;
480b3470b5dSDavid Sterba 	u64 len = block_group->length;
4819f21246dSJosef Bacik 	u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
4829f21246dSJosef Bacik 		fs_info->nodesize : fs_info->sectorsize;
4839f21246dSJosef Bacik 	u64 step = chunk << 1;
4849f21246dSJosef Bacik 
4859f21246dSJosef Bacik 	while (len > chunk) {
4869f21246dSJosef Bacik 		btrfs_remove_free_space(block_group, start, chunk);
4879f21246dSJosef Bacik 		start += step;
4889f21246dSJosef Bacik 		if (len < step)
4899f21246dSJosef Bacik 			len = 0;
4909f21246dSJosef Bacik 		else
4919f21246dSJosef Bacik 			len -= step;
4929f21246dSJosef Bacik 	}
4939f21246dSJosef Bacik }
4949f21246dSJosef Bacik #endif
4959f21246dSJosef Bacik 
4969f21246dSJosef Bacik /*
4979f21246dSJosef Bacik  * This is only called by btrfs_cache_block_group, since we could have freed
4989f21246dSJosef Bacik  * extents we need to check the pinned_extents for any extents that can't be
4999f21246dSJosef Bacik  * used yet since their free space will be released as soon as the transaction
5009f21246dSJosef Bacik  * commits.
5019f21246dSJosef Bacik  */
50232da5386SDavid Sterba u64 add_new_free_space(struct btrfs_block_group *block_group, u64 start, u64 end)
5039f21246dSJosef Bacik {
5049f21246dSJosef Bacik 	struct btrfs_fs_info *info = block_group->fs_info;
5059f21246dSJosef Bacik 	u64 extent_start, extent_end, size, total_added = 0;
5069f21246dSJosef Bacik 	int ret;
5079f21246dSJosef Bacik 
5089f21246dSJosef Bacik 	while (start < end) {
509fe119a6eSNikolay Borisov 		ret = find_first_extent_bit(&info->excluded_extents, start,
5109f21246dSJosef Bacik 					    &extent_start, &extent_end,
5119f21246dSJosef Bacik 					    EXTENT_DIRTY | EXTENT_UPTODATE,
5129f21246dSJosef Bacik 					    NULL);
5139f21246dSJosef Bacik 		if (ret)
5149f21246dSJosef Bacik 			break;
5159f21246dSJosef Bacik 
5169f21246dSJosef Bacik 		if (extent_start <= start) {
5179f21246dSJosef Bacik 			start = extent_end + 1;
5189f21246dSJosef Bacik 		} else if (extent_start > start && extent_start < end) {
5199f21246dSJosef Bacik 			size = extent_start - start;
5209f21246dSJosef Bacik 			total_added += size;
521b0643e59SDennis Zhou 			ret = btrfs_add_free_space_async_trimmed(block_group,
522b0643e59SDennis Zhou 								 start, size);
5239f21246dSJosef Bacik 			BUG_ON(ret); /* -ENOMEM or logic error */
5249f21246dSJosef Bacik 			start = extent_end + 1;
5259f21246dSJosef Bacik 		} else {
5269f21246dSJosef Bacik 			break;
5279f21246dSJosef Bacik 		}
5289f21246dSJosef Bacik 	}
5299f21246dSJosef Bacik 
5309f21246dSJosef Bacik 	if (start < end) {
5319f21246dSJosef Bacik 		size = end - start;
5329f21246dSJosef Bacik 		total_added += size;
533b0643e59SDennis Zhou 		ret = btrfs_add_free_space_async_trimmed(block_group, start,
534b0643e59SDennis Zhou 							 size);
5359f21246dSJosef Bacik 		BUG_ON(ret); /* -ENOMEM or logic error */
5369f21246dSJosef Bacik 	}
5379f21246dSJosef Bacik 
5389f21246dSJosef Bacik 	return total_added;
5399f21246dSJosef Bacik }
5409f21246dSJosef Bacik 
5419f21246dSJosef Bacik static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
5429f21246dSJosef Bacik {
54332da5386SDavid Sterba 	struct btrfs_block_group *block_group = caching_ctl->block_group;
5449f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
54529cbcf40SJosef Bacik 	struct btrfs_root *extent_root;
5469f21246dSJosef Bacik 	struct btrfs_path *path;
5479f21246dSJosef Bacik 	struct extent_buffer *leaf;
5489f21246dSJosef Bacik 	struct btrfs_key key;
5499f21246dSJosef Bacik 	u64 total_found = 0;
5509f21246dSJosef Bacik 	u64 last = 0;
5519f21246dSJosef Bacik 	u32 nritems;
5529f21246dSJosef Bacik 	int ret;
5539f21246dSJosef Bacik 	bool wakeup = true;
5549f21246dSJosef Bacik 
5559f21246dSJosef Bacik 	path = btrfs_alloc_path();
5569f21246dSJosef Bacik 	if (!path)
5579f21246dSJosef Bacik 		return -ENOMEM;
5589f21246dSJosef Bacik 
559b3470b5dSDavid Sterba 	last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET);
56029cbcf40SJosef Bacik 	extent_root = btrfs_extent_root(fs_info, last);
5619f21246dSJosef Bacik 
5629f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
5639f21246dSJosef Bacik 	/*
5649f21246dSJosef Bacik 	 * If we're fragmenting we don't want to make anybody think we can
5659f21246dSJosef Bacik 	 * allocate from this block group until we've had a chance to fragment
5669f21246dSJosef Bacik 	 * the free space.
5679f21246dSJosef Bacik 	 */
5689f21246dSJosef Bacik 	if (btrfs_should_fragment_free_space(block_group))
5699f21246dSJosef Bacik 		wakeup = false;
5709f21246dSJosef Bacik #endif
5719f21246dSJosef Bacik 	/*
5729f21246dSJosef Bacik 	 * We don't want to deadlock with somebody trying to allocate a new
5739f21246dSJosef Bacik 	 * extent for the extent root while also trying to search the extent
5749f21246dSJosef Bacik 	 * root to add free space.  So we skip locking and search the commit
5759f21246dSJosef Bacik 	 * root, since its read-only
5769f21246dSJosef Bacik 	 */
5779f21246dSJosef Bacik 	path->skip_locking = 1;
5789f21246dSJosef Bacik 	path->search_commit_root = 1;
5799f21246dSJosef Bacik 	path->reada = READA_FORWARD;
5809f21246dSJosef Bacik 
5819f21246dSJosef Bacik 	key.objectid = last;
5829f21246dSJosef Bacik 	key.offset = 0;
5839f21246dSJosef Bacik 	key.type = BTRFS_EXTENT_ITEM_KEY;
5849f21246dSJosef Bacik 
5859f21246dSJosef Bacik next:
5869f21246dSJosef Bacik 	ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
5879f21246dSJosef Bacik 	if (ret < 0)
5889f21246dSJosef Bacik 		goto out;
5899f21246dSJosef Bacik 
5909f21246dSJosef Bacik 	leaf = path->nodes[0];
5919f21246dSJosef Bacik 	nritems = btrfs_header_nritems(leaf);
5929f21246dSJosef Bacik 
5939f21246dSJosef Bacik 	while (1) {
5949f21246dSJosef Bacik 		if (btrfs_fs_closing(fs_info) > 1) {
5959f21246dSJosef Bacik 			last = (u64)-1;
5969f21246dSJosef Bacik 			break;
5979f21246dSJosef Bacik 		}
5989f21246dSJosef Bacik 
5999f21246dSJosef Bacik 		if (path->slots[0] < nritems) {
6009f21246dSJosef Bacik 			btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6019f21246dSJosef Bacik 		} else {
6029f21246dSJosef Bacik 			ret = btrfs_find_next_key(extent_root, path, &key, 0, 0);
6039f21246dSJosef Bacik 			if (ret)
6049f21246dSJosef Bacik 				break;
6059f21246dSJosef Bacik 
6069f21246dSJosef Bacik 			if (need_resched() ||
6079f21246dSJosef Bacik 			    rwsem_is_contended(&fs_info->commit_root_sem)) {
6089f21246dSJosef Bacik 				btrfs_release_path(path);
6099f21246dSJosef Bacik 				up_read(&fs_info->commit_root_sem);
6109f21246dSJosef Bacik 				mutex_unlock(&caching_ctl->mutex);
6119f21246dSJosef Bacik 				cond_resched();
6129f21246dSJosef Bacik 				mutex_lock(&caching_ctl->mutex);
6139f21246dSJosef Bacik 				down_read(&fs_info->commit_root_sem);
6149f21246dSJosef Bacik 				goto next;
6159f21246dSJosef Bacik 			}
6169f21246dSJosef Bacik 
6179f21246dSJosef Bacik 			ret = btrfs_next_leaf(extent_root, path);
6189f21246dSJosef Bacik 			if (ret < 0)
6199f21246dSJosef Bacik 				goto out;
6209f21246dSJosef Bacik 			if (ret)
6219f21246dSJosef Bacik 				break;
6229f21246dSJosef Bacik 			leaf = path->nodes[0];
6239f21246dSJosef Bacik 			nritems = btrfs_header_nritems(leaf);
6249f21246dSJosef Bacik 			continue;
6259f21246dSJosef Bacik 		}
6269f21246dSJosef Bacik 
6279f21246dSJosef Bacik 		if (key.objectid < last) {
6289f21246dSJosef Bacik 			key.objectid = last;
6299f21246dSJosef Bacik 			key.offset = 0;
6309f21246dSJosef Bacik 			key.type = BTRFS_EXTENT_ITEM_KEY;
6319f21246dSJosef Bacik 			btrfs_release_path(path);
6329f21246dSJosef Bacik 			goto next;
6339f21246dSJosef Bacik 		}
6349f21246dSJosef Bacik 
635b3470b5dSDavid Sterba 		if (key.objectid < block_group->start) {
6369f21246dSJosef Bacik 			path->slots[0]++;
6379f21246dSJosef Bacik 			continue;
6389f21246dSJosef Bacik 		}
6399f21246dSJosef Bacik 
640b3470b5dSDavid Sterba 		if (key.objectid >= block_group->start + block_group->length)
6419f21246dSJosef Bacik 			break;
6429f21246dSJosef Bacik 
6439f21246dSJosef Bacik 		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
6449f21246dSJosef Bacik 		    key.type == BTRFS_METADATA_ITEM_KEY) {
6459f21246dSJosef Bacik 			total_found += add_new_free_space(block_group, last,
6469f21246dSJosef Bacik 							  key.objectid);
6479f21246dSJosef Bacik 			if (key.type == BTRFS_METADATA_ITEM_KEY)
6489f21246dSJosef Bacik 				last = key.objectid +
6499f21246dSJosef Bacik 					fs_info->nodesize;
6509f21246dSJosef Bacik 			else
6519f21246dSJosef Bacik 				last = key.objectid + key.offset;
6529f21246dSJosef Bacik 
6539f21246dSJosef Bacik 			if (total_found > CACHING_CTL_WAKE_UP) {
6549f21246dSJosef Bacik 				total_found = 0;
6559f21246dSJosef Bacik 				if (wakeup)
6569f21246dSJosef Bacik 					wake_up(&caching_ctl->wait);
6579f21246dSJosef Bacik 			}
6589f21246dSJosef Bacik 		}
6599f21246dSJosef Bacik 		path->slots[0]++;
6609f21246dSJosef Bacik 	}
6619f21246dSJosef Bacik 	ret = 0;
6629f21246dSJosef Bacik 
6639f21246dSJosef Bacik 	total_found += add_new_free_space(block_group, last,
664b3470b5dSDavid Sterba 				block_group->start + block_group->length);
6659f21246dSJosef Bacik 
6669f21246dSJosef Bacik out:
6679f21246dSJosef Bacik 	btrfs_free_path(path);
6689f21246dSJosef Bacik 	return ret;
6699f21246dSJosef Bacik }
6709f21246dSJosef Bacik 
6719f21246dSJosef Bacik static noinline void caching_thread(struct btrfs_work *work)
6729f21246dSJosef Bacik {
67332da5386SDavid Sterba 	struct btrfs_block_group *block_group;
6749f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info;
6759f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
6769f21246dSJosef Bacik 	int ret;
6779f21246dSJosef Bacik 
6789f21246dSJosef Bacik 	caching_ctl = container_of(work, struct btrfs_caching_control, work);
6799f21246dSJosef Bacik 	block_group = caching_ctl->block_group;
6809f21246dSJosef Bacik 	fs_info = block_group->fs_info;
6819f21246dSJosef Bacik 
6829f21246dSJosef Bacik 	mutex_lock(&caching_ctl->mutex);
6839f21246dSJosef Bacik 	down_read(&fs_info->commit_root_sem);
6849f21246dSJosef Bacik 
685e747853cSJosef Bacik 	if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
686e747853cSJosef Bacik 		ret = load_free_space_cache(block_group);
687e747853cSJosef Bacik 		if (ret == 1) {
688e747853cSJosef Bacik 			ret = 0;
689e747853cSJosef Bacik 			goto done;
690e747853cSJosef Bacik 		}
691e747853cSJosef Bacik 
692e747853cSJosef Bacik 		/*
693e747853cSJosef Bacik 		 * We failed to load the space cache, set ourselves to
694e747853cSJosef Bacik 		 * CACHE_STARTED and carry on.
695e747853cSJosef Bacik 		 */
696e747853cSJosef Bacik 		spin_lock(&block_group->lock);
697e747853cSJosef Bacik 		block_group->cached = BTRFS_CACHE_STARTED;
698e747853cSJosef Bacik 		spin_unlock(&block_group->lock);
699e747853cSJosef Bacik 		wake_up(&caching_ctl->wait);
700e747853cSJosef Bacik 	}
701e747853cSJosef Bacik 
7022f96e402SJosef Bacik 	/*
7032f96e402SJosef Bacik 	 * If we are in the transaction that populated the free space tree we
7042f96e402SJosef Bacik 	 * can't actually cache from the free space tree as our commit root and
7052f96e402SJosef Bacik 	 * real root are the same, so we could change the contents of the blocks
7062f96e402SJosef Bacik 	 * while caching.  Instead do the slow caching in this case, and after
7072f96e402SJosef Bacik 	 * the transaction has committed we will be safe.
7082f96e402SJosef Bacik 	 */
7092f96e402SJosef Bacik 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
7102f96e402SJosef Bacik 	    !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags)))
7119f21246dSJosef Bacik 		ret = load_free_space_tree(caching_ctl);
7129f21246dSJosef Bacik 	else
7139f21246dSJosef Bacik 		ret = load_extent_tree_free(caching_ctl);
714e747853cSJosef Bacik done:
7159f21246dSJosef Bacik 	spin_lock(&block_group->lock);
7169f21246dSJosef Bacik 	block_group->caching_ctl = NULL;
7179f21246dSJosef Bacik 	block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
7189f21246dSJosef Bacik 	spin_unlock(&block_group->lock);
7199f21246dSJosef Bacik 
7209f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
7219f21246dSJosef Bacik 	if (btrfs_should_fragment_free_space(block_group)) {
7229f21246dSJosef Bacik 		u64 bytes_used;
7239f21246dSJosef Bacik 
7249f21246dSJosef Bacik 		spin_lock(&block_group->space_info->lock);
7259f21246dSJosef Bacik 		spin_lock(&block_group->lock);
726b3470b5dSDavid Sterba 		bytes_used = block_group->length - block_group->used;
7279f21246dSJosef Bacik 		block_group->space_info->bytes_used += bytes_used >> 1;
7289f21246dSJosef Bacik 		spin_unlock(&block_group->lock);
7299f21246dSJosef Bacik 		spin_unlock(&block_group->space_info->lock);
730e11c0406SJosef Bacik 		fragment_free_space(block_group);
7319f21246dSJosef Bacik 	}
7329f21246dSJosef Bacik #endif
7339f21246dSJosef Bacik 
7349f21246dSJosef Bacik 	up_read(&fs_info->commit_root_sem);
7359f21246dSJosef Bacik 	btrfs_free_excluded_extents(block_group);
7369f21246dSJosef Bacik 	mutex_unlock(&caching_ctl->mutex);
7379f21246dSJosef Bacik 
7389f21246dSJosef Bacik 	wake_up(&caching_ctl->wait);
7399f21246dSJosef Bacik 
7409f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
7419f21246dSJosef Bacik 	btrfs_put_block_group(block_group);
7429f21246dSJosef Bacik }
7439f21246dSJosef Bacik 
744ced8ecf0SOmar Sandoval int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait)
7459f21246dSJosef Bacik {
7469f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
747e747853cSJosef Bacik 	struct btrfs_caching_control *caching_ctl = NULL;
7489f21246dSJosef Bacik 	int ret = 0;
7499f21246dSJosef Bacik 
7502eda5708SNaohiro Aota 	/* Allocator for zoned filesystems does not use the cache at all */
7512eda5708SNaohiro Aota 	if (btrfs_is_zoned(fs_info))
7522eda5708SNaohiro Aota 		return 0;
7532eda5708SNaohiro Aota 
7549f21246dSJosef Bacik 	caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
7559f21246dSJosef Bacik 	if (!caching_ctl)
7569f21246dSJosef Bacik 		return -ENOMEM;
7579f21246dSJosef Bacik 
7589f21246dSJosef Bacik 	INIT_LIST_HEAD(&caching_ctl->list);
7599f21246dSJosef Bacik 	mutex_init(&caching_ctl->mutex);
7609f21246dSJosef Bacik 	init_waitqueue_head(&caching_ctl->wait);
7619f21246dSJosef Bacik 	caching_ctl->block_group = cache;
762e747853cSJosef Bacik 	refcount_set(&caching_ctl->count, 2);
763a0cac0ecSOmar Sandoval 	btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
7649f21246dSJosef Bacik 
7659f21246dSJosef Bacik 	spin_lock(&cache->lock);
7669f21246dSJosef Bacik 	if (cache->cached != BTRFS_CACHE_NO) {
7679f21246dSJosef Bacik 		kfree(caching_ctl);
768e747853cSJosef Bacik 
769e747853cSJosef Bacik 		caching_ctl = cache->caching_ctl;
770e747853cSJosef Bacik 		if (caching_ctl)
771e747853cSJosef Bacik 			refcount_inc(&caching_ctl->count);
772e747853cSJosef Bacik 		spin_unlock(&cache->lock);
773e747853cSJosef Bacik 		goto out;
7749f21246dSJosef Bacik 	}
7759f21246dSJosef Bacik 	WARN_ON(cache->caching_ctl);
7769f21246dSJosef Bacik 	cache->caching_ctl = caching_ctl;
7779f21246dSJosef Bacik 	cache->cached = BTRFS_CACHE_STARTED;
7789f21246dSJosef Bacik 	spin_unlock(&cache->lock);
7799f21246dSJosef Bacik 
78016b0c258SFilipe Manana 	write_lock(&fs_info->block_group_cache_lock);
7819f21246dSJosef Bacik 	refcount_inc(&caching_ctl->count);
7829f21246dSJosef Bacik 	list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
78316b0c258SFilipe Manana 	write_unlock(&fs_info->block_group_cache_lock);
7849f21246dSJosef Bacik 
7859f21246dSJosef Bacik 	btrfs_get_block_group(cache);
7869f21246dSJosef Bacik 
7879f21246dSJosef Bacik 	btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
788e747853cSJosef Bacik out:
789ced8ecf0SOmar Sandoval 	if (wait && caching_ctl)
790ced8ecf0SOmar Sandoval 		ret = btrfs_caching_ctl_wait_done(cache, caching_ctl);
791e747853cSJosef Bacik 	if (caching_ctl)
792e747853cSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
7939f21246dSJosef Bacik 
7949f21246dSJosef Bacik 	return ret;
7959f21246dSJosef Bacik }
796e3e0520bSJosef Bacik 
797e3e0520bSJosef Bacik static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
798e3e0520bSJosef Bacik {
799e3e0520bSJosef Bacik 	u64 extra_flags = chunk_to_extended(flags) &
800e3e0520bSJosef Bacik 				BTRFS_EXTENDED_PROFILE_MASK;
801e3e0520bSJosef Bacik 
802e3e0520bSJosef Bacik 	write_seqlock(&fs_info->profiles_lock);
803e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA)
804e3e0520bSJosef Bacik 		fs_info->avail_data_alloc_bits &= ~extra_flags;
805e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_METADATA)
806e3e0520bSJosef Bacik 		fs_info->avail_metadata_alloc_bits &= ~extra_flags;
807e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
808e3e0520bSJosef Bacik 		fs_info->avail_system_alloc_bits &= ~extra_flags;
809e3e0520bSJosef Bacik 	write_sequnlock(&fs_info->profiles_lock);
810e3e0520bSJosef Bacik }
811e3e0520bSJosef Bacik 
812e3e0520bSJosef Bacik /*
813e3e0520bSJosef Bacik  * Clear incompat bits for the following feature(s):
814e3e0520bSJosef Bacik  *
815e3e0520bSJosef Bacik  * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
816e3e0520bSJosef Bacik  *            in the whole filesystem
8179c907446SDavid Sterba  *
8189c907446SDavid Sterba  * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
819e3e0520bSJosef Bacik  */
820e3e0520bSJosef Bacik static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags)
821e3e0520bSJosef Bacik {
8229c907446SDavid Sterba 	bool found_raid56 = false;
8239c907446SDavid Sterba 	bool found_raid1c34 = false;
8249c907446SDavid Sterba 
8259c907446SDavid Sterba 	if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) ||
8269c907446SDavid Sterba 	    (flags & BTRFS_BLOCK_GROUP_RAID1C3) ||
8279c907446SDavid Sterba 	    (flags & BTRFS_BLOCK_GROUP_RAID1C4)) {
828e3e0520bSJosef Bacik 		struct list_head *head = &fs_info->space_info;
829e3e0520bSJosef Bacik 		struct btrfs_space_info *sinfo;
830e3e0520bSJosef Bacik 
831e3e0520bSJosef Bacik 		list_for_each_entry_rcu(sinfo, head, list) {
832e3e0520bSJosef Bacik 			down_read(&sinfo->groups_sem);
833e3e0520bSJosef Bacik 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5]))
8349c907446SDavid Sterba 				found_raid56 = true;
835e3e0520bSJosef Bacik 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6]))
8369c907446SDavid Sterba 				found_raid56 = true;
8379c907446SDavid Sterba 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3]))
8389c907446SDavid Sterba 				found_raid1c34 = true;
8399c907446SDavid Sterba 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4]))
8409c907446SDavid Sterba 				found_raid1c34 = true;
841e3e0520bSJosef Bacik 			up_read(&sinfo->groups_sem);
842e3e0520bSJosef Bacik 		}
843d8e6fd5cSFilipe Manana 		if (!found_raid56)
844e3e0520bSJosef Bacik 			btrfs_clear_fs_incompat(fs_info, RAID56);
845d8e6fd5cSFilipe Manana 		if (!found_raid1c34)
8469c907446SDavid Sterba 			btrfs_clear_fs_incompat(fs_info, RAID1C34);
847e3e0520bSJosef Bacik 	}
848e3e0520bSJosef Bacik }
849e3e0520bSJosef Bacik 
8507357623aSQu Wenruo static int remove_block_group_item(struct btrfs_trans_handle *trans,
8517357623aSQu Wenruo 				   struct btrfs_path *path,
8527357623aSQu Wenruo 				   struct btrfs_block_group *block_group)
8537357623aSQu Wenruo {
8547357623aSQu Wenruo 	struct btrfs_fs_info *fs_info = trans->fs_info;
8557357623aSQu Wenruo 	struct btrfs_root *root;
8567357623aSQu Wenruo 	struct btrfs_key key;
8577357623aSQu Wenruo 	int ret;
8587357623aSQu Wenruo 
859dfe8aec4SJosef Bacik 	root = btrfs_block_group_root(fs_info);
8607357623aSQu Wenruo 	key.objectid = block_group->start;
8617357623aSQu Wenruo 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8627357623aSQu Wenruo 	key.offset = block_group->length;
8637357623aSQu Wenruo 
8647357623aSQu Wenruo 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8657357623aSQu Wenruo 	if (ret > 0)
8667357623aSQu Wenruo 		ret = -ENOENT;
8677357623aSQu Wenruo 	if (ret < 0)
8687357623aSQu Wenruo 		return ret;
8697357623aSQu Wenruo 
8707357623aSQu Wenruo 	ret = btrfs_del_item(trans, root, path);
8717357623aSQu Wenruo 	return ret;
8727357623aSQu Wenruo }
8737357623aSQu Wenruo 
874e3e0520bSJosef Bacik int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
875e3e0520bSJosef Bacik 			     u64 group_start, struct extent_map *em)
876e3e0520bSJosef Bacik {
877e3e0520bSJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
878e3e0520bSJosef Bacik 	struct btrfs_path *path;
87932da5386SDavid Sterba 	struct btrfs_block_group *block_group;
880e3e0520bSJosef Bacik 	struct btrfs_free_cluster *cluster;
881e3e0520bSJosef Bacik 	struct inode *inode;
882e3e0520bSJosef Bacik 	struct kobject *kobj = NULL;
883e3e0520bSJosef Bacik 	int ret;
884e3e0520bSJosef Bacik 	int index;
885e3e0520bSJosef Bacik 	int factor;
886e3e0520bSJosef Bacik 	struct btrfs_caching_control *caching_ctl = NULL;
887e3e0520bSJosef Bacik 	bool remove_em;
888e3e0520bSJosef Bacik 	bool remove_rsv = false;
889e3e0520bSJosef Bacik 
890e3e0520bSJosef Bacik 	block_group = btrfs_lookup_block_group(fs_info, group_start);
891e3e0520bSJosef Bacik 	BUG_ON(!block_group);
892e3e0520bSJosef Bacik 	BUG_ON(!block_group->ro);
893e3e0520bSJosef Bacik 
894e3e0520bSJosef Bacik 	trace_btrfs_remove_block_group(block_group);
895e3e0520bSJosef Bacik 	/*
896e3e0520bSJosef Bacik 	 * Free the reserved super bytes from this block group before
897e3e0520bSJosef Bacik 	 * remove it.
898e3e0520bSJosef Bacik 	 */
899e3e0520bSJosef Bacik 	btrfs_free_excluded_extents(block_group);
900b3470b5dSDavid Sterba 	btrfs_free_ref_tree_range(fs_info, block_group->start,
901b3470b5dSDavid Sterba 				  block_group->length);
902e3e0520bSJosef Bacik 
903e3e0520bSJosef Bacik 	index = btrfs_bg_flags_to_raid_index(block_group->flags);
904e3e0520bSJosef Bacik 	factor = btrfs_bg_type_to_factor(block_group->flags);
905e3e0520bSJosef Bacik 
906e3e0520bSJosef Bacik 	/* make sure this block group isn't part of an allocation cluster */
907e3e0520bSJosef Bacik 	cluster = &fs_info->data_alloc_cluster;
908e3e0520bSJosef Bacik 	spin_lock(&cluster->refill_lock);
909e3e0520bSJosef Bacik 	btrfs_return_cluster_to_free_space(block_group, cluster);
910e3e0520bSJosef Bacik 	spin_unlock(&cluster->refill_lock);
911e3e0520bSJosef Bacik 
912e3e0520bSJosef Bacik 	/*
913e3e0520bSJosef Bacik 	 * make sure this block group isn't part of a metadata
914e3e0520bSJosef Bacik 	 * allocation cluster
915e3e0520bSJosef Bacik 	 */
916e3e0520bSJosef Bacik 	cluster = &fs_info->meta_alloc_cluster;
917e3e0520bSJosef Bacik 	spin_lock(&cluster->refill_lock);
918e3e0520bSJosef Bacik 	btrfs_return_cluster_to_free_space(block_group, cluster);
919e3e0520bSJosef Bacik 	spin_unlock(&cluster->refill_lock);
920e3e0520bSJosef Bacik 
92140ab3be1SNaohiro Aota 	btrfs_clear_treelog_bg(block_group);
922c2707a25SJohannes Thumshirn 	btrfs_clear_data_reloc_bg(block_group);
92340ab3be1SNaohiro Aota 
924e3e0520bSJosef Bacik 	path = btrfs_alloc_path();
925e3e0520bSJosef Bacik 	if (!path) {
926e3e0520bSJosef Bacik 		ret = -ENOMEM;
9279fecd132SFilipe Manana 		goto out;
928e3e0520bSJosef Bacik 	}
929e3e0520bSJosef Bacik 
930e3e0520bSJosef Bacik 	/*
931e3e0520bSJosef Bacik 	 * get the inode first so any iput calls done for the io_list
932e3e0520bSJosef Bacik 	 * aren't the final iput (no unlinks allowed now)
933e3e0520bSJosef Bacik 	 */
934e3e0520bSJosef Bacik 	inode = lookup_free_space_inode(block_group, path);
935e3e0520bSJosef Bacik 
936e3e0520bSJosef Bacik 	mutex_lock(&trans->transaction->cache_write_mutex);
937e3e0520bSJosef Bacik 	/*
938e3e0520bSJosef Bacik 	 * Make sure our free space cache IO is done before removing the
939e3e0520bSJosef Bacik 	 * free space inode
940e3e0520bSJosef Bacik 	 */
941e3e0520bSJosef Bacik 	spin_lock(&trans->transaction->dirty_bgs_lock);
942e3e0520bSJosef Bacik 	if (!list_empty(&block_group->io_list)) {
943e3e0520bSJosef Bacik 		list_del_init(&block_group->io_list);
944e3e0520bSJosef Bacik 
945e3e0520bSJosef Bacik 		WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
946e3e0520bSJosef Bacik 
947e3e0520bSJosef Bacik 		spin_unlock(&trans->transaction->dirty_bgs_lock);
948e3e0520bSJosef Bacik 		btrfs_wait_cache_io(trans, block_group, path);
949e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
950e3e0520bSJosef Bacik 		spin_lock(&trans->transaction->dirty_bgs_lock);
951e3e0520bSJosef Bacik 	}
952e3e0520bSJosef Bacik 
953e3e0520bSJosef Bacik 	if (!list_empty(&block_group->dirty_list)) {
954e3e0520bSJosef Bacik 		list_del_init(&block_group->dirty_list);
955e3e0520bSJosef Bacik 		remove_rsv = true;
956e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
957e3e0520bSJosef Bacik 	}
958e3e0520bSJosef Bacik 	spin_unlock(&trans->transaction->dirty_bgs_lock);
959e3e0520bSJosef Bacik 	mutex_unlock(&trans->transaction->cache_write_mutex);
960e3e0520bSJosef Bacik 
96136b216c8SBoris Burkov 	ret = btrfs_remove_free_space_inode(trans, inode, block_group);
962e3e0520bSJosef Bacik 	if (ret)
9639fecd132SFilipe Manana 		goto out;
964e3e0520bSJosef Bacik 
96516b0c258SFilipe Manana 	write_lock(&fs_info->block_group_cache_lock);
96608dddb29SFilipe Manana 	rb_erase_cached(&block_group->cache_node,
967e3e0520bSJosef Bacik 			&fs_info->block_group_cache_tree);
968e3e0520bSJosef Bacik 	RB_CLEAR_NODE(&block_group->cache_node);
969e3e0520bSJosef Bacik 
9709fecd132SFilipe Manana 	/* Once for the block groups rbtree */
9719fecd132SFilipe Manana 	btrfs_put_block_group(block_group);
9729fecd132SFilipe Manana 
97316b0c258SFilipe Manana 	write_unlock(&fs_info->block_group_cache_lock);
974e3e0520bSJosef Bacik 
975e3e0520bSJosef Bacik 	down_write(&block_group->space_info->groups_sem);
976e3e0520bSJosef Bacik 	/*
977e3e0520bSJosef Bacik 	 * we must use list_del_init so people can check to see if they
978e3e0520bSJosef Bacik 	 * are still on the list after taking the semaphore
979e3e0520bSJosef Bacik 	 */
980e3e0520bSJosef Bacik 	list_del_init(&block_group->list);
981e3e0520bSJosef Bacik 	if (list_empty(&block_group->space_info->block_groups[index])) {
982e3e0520bSJosef Bacik 		kobj = block_group->space_info->block_group_kobjs[index];
983e3e0520bSJosef Bacik 		block_group->space_info->block_group_kobjs[index] = NULL;
984e3e0520bSJosef Bacik 		clear_avail_alloc_bits(fs_info, block_group->flags);
985e3e0520bSJosef Bacik 	}
986e3e0520bSJosef Bacik 	up_write(&block_group->space_info->groups_sem);
987e3e0520bSJosef Bacik 	clear_incompat_bg_bits(fs_info, block_group->flags);
988e3e0520bSJosef Bacik 	if (kobj) {
989e3e0520bSJosef Bacik 		kobject_del(kobj);
990e3e0520bSJosef Bacik 		kobject_put(kobj);
991e3e0520bSJosef Bacik 	}
992e3e0520bSJosef Bacik 
993e3e0520bSJosef Bacik 	if (block_group->cached == BTRFS_CACHE_STARTED)
994e3e0520bSJosef Bacik 		btrfs_wait_block_group_cache_done(block_group);
9957b9c293bSJosef Bacik 
99616b0c258SFilipe Manana 	write_lock(&fs_info->block_group_cache_lock);
9977b9c293bSJosef Bacik 	caching_ctl = btrfs_get_caching_control(block_group);
998e3e0520bSJosef Bacik 	if (!caching_ctl) {
999e3e0520bSJosef Bacik 		struct btrfs_caching_control *ctl;
1000e3e0520bSJosef Bacik 
10017b9c293bSJosef Bacik 		list_for_each_entry(ctl, &fs_info->caching_block_groups, list) {
1002e3e0520bSJosef Bacik 			if (ctl->block_group == block_group) {
1003e3e0520bSJosef Bacik 				caching_ctl = ctl;
1004e3e0520bSJosef Bacik 				refcount_inc(&caching_ctl->count);
1005e3e0520bSJosef Bacik 				break;
1006e3e0520bSJosef Bacik 			}
1007e3e0520bSJosef Bacik 		}
10087b9c293bSJosef Bacik 	}
1009e3e0520bSJosef Bacik 	if (caching_ctl)
1010e3e0520bSJosef Bacik 		list_del_init(&caching_ctl->list);
101116b0c258SFilipe Manana 	write_unlock(&fs_info->block_group_cache_lock);
10127b9c293bSJosef Bacik 
1013e3e0520bSJosef Bacik 	if (caching_ctl) {
1014e3e0520bSJosef Bacik 		/* Once for the caching bgs list and once for us. */
1015e3e0520bSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
1016e3e0520bSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
1017e3e0520bSJosef Bacik 	}
1018e3e0520bSJosef Bacik 
1019e3e0520bSJosef Bacik 	spin_lock(&trans->transaction->dirty_bgs_lock);
1020e3e0520bSJosef Bacik 	WARN_ON(!list_empty(&block_group->dirty_list));
1021e3e0520bSJosef Bacik 	WARN_ON(!list_empty(&block_group->io_list));
1022e3e0520bSJosef Bacik 	spin_unlock(&trans->transaction->dirty_bgs_lock);
1023e3e0520bSJosef Bacik 
1024e3e0520bSJosef Bacik 	btrfs_remove_free_space_cache(block_group);
1025e3e0520bSJosef Bacik 
1026e3e0520bSJosef Bacik 	spin_lock(&block_group->space_info->lock);
1027e3e0520bSJosef Bacik 	list_del_init(&block_group->ro_list);
1028e3e0520bSJosef Bacik 
1029e3e0520bSJosef Bacik 	if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
1030e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->total_bytes
1031b3470b5dSDavid Sterba 			< block_group->length);
1032e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->bytes_readonly
1033169e0da9SNaohiro Aota 			< block_group->length - block_group->zone_unusable);
1034169e0da9SNaohiro Aota 		WARN_ON(block_group->space_info->bytes_zone_unusable
1035169e0da9SNaohiro Aota 			< block_group->zone_unusable);
1036e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->disk_total
1037b3470b5dSDavid Sterba 			< block_group->length * factor);
10383349b57fSJosef Bacik 		WARN_ON(test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
10393349b57fSJosef Bacik 				 &block_group->runtime_flags) &&
10406a921de5SNaohiro Aota 			block_group->space_info->active_total_bytes
10416a921de5SNaohiro Aota 			< block_group->length);
1042e3e0520bSJosef Bacik 	}
1043b3470b5dSDavid Sterba 	block_group->space_info->total_bytes -= block_group->length;
10443349b57fSJosef Bacik 	if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags))
10456a921de5SNaohiro Aota 		block_group->space_info->active_total_bytes -= block_group->length;
1046169e0da9SNaohiro Aota 	block_group->space_info->bytes_readonly -=
1047169e0da9SNaohiro Aota 		(block_group->length - block_group->zone_unusable);
1048169e0da9SNaohiro Aota 	block_group->space_info->bytes_zone_unusable -=
1049169e0da9SNaohiro Aota 		block_group->zone_unusable;
1050b3470b5dSDavid Sterba 	block_group->space_info->disk_total -= block_group->length * factor;
1051e3e0520bSJosef Bacik 
1052e3e0520bSJosef Bacik 	spin_unlock(&block_group->space_info->lock);
1053e3e0520bSJosef Bacik 
1054ffcb9d44SFilipe Manana 	/*
1055ffcb9d44SFilipe Manana 	 * Remove the free space for the block group from the free space tree
1056ffcb9d44SFilipe Manana 	 * and the block group's item from the extent tree before marking the
1057ffcb9d44SFilipe Manana 	 * block group as removed. This is to prevent races with tasks that
1058ffcb9d44SFilipe Manana 	 * freeze and unfreeze a block group, this task and another task
1059ffcb9d44SFilipe Manana 	 * allocating a new block group - the unfreeze task ends up removing
1060ffcb9d44SFilipe Manana 	 * the block group's extent map before the task calling this function
1061ffcb9d44SFilipe Manana 	 * deletes the block group item from the extent tree, allowing for
1062ffcb9d44SFilipe Manana 	 * another task to attempt to create another block group with the same
1063ffcb9d44SFilipe Manana 	 * item key (and failing with -EEXIST and a transaction abort).
1064ffcb9d44SFilipe Manana 	 */
1065ffcb9d44SFilipe Manana 	ret = remove_block_group_free_space(trans, block_group);
1066ffcb9d44SFilipe Manana 	if (ret)
1067ffcb9d44SFilipe Manana 		goto out;
1068ffcb9d44SFilipe Manana 
1069ffcb9d44SFilipe Manana 	ret = remove_block_group_item(trans, path, block_group);
1070ffcb9d44SFilipe Manana 	if (ret < 0)
1071ffcb9d44SFilipe Manana 		goto out;
1072ffcb9d44SFilipe Manana 
1073e3e0520bSJosef Bacik 	spin_lock(&block_group->lock);
10743349b57fSJosef Bacik 	set_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags);
10753349b57fSJosef Bacik 
1076e3e0520bSJosef Bacik 	/*
10776b7304afSFilipe Manana 	 * At this point trimming or scrub can't start on this block group,
10786b7304afSFilipe Manana 	 * because we removed the block group from the rbtree
10796b7304afSFilipe Manana 	 * fs_info->block_group_cache_tree so no one can't find it anymore and
10806b7304afSFilipe Manana 	 * even if someone already got this block group before we removed it
10816b7304afSFilipe Manana 	 * from the rbtree, they have already incremented block_group->frozen -
10826b7304afSFilipe Manana 	 * if they didn't, for the trimming case they won't find any free space
10836b7304afSFilipe Manana 	 * entries because we already removed them all when we called
10846b7304afSFilipe Manana 	 * btrfs_remove_free_space_cache().
1085e3e0520bSJosef Bacik 	 *
1086e3e0520bSJosef Bacik 	 * And we must not remove the extent map from the fs_info->mapping_tree
1087e3e0520bSJosef Bacik 	 * to prevent the same logical address range and physical device space
10886b7304afSFilipe Manana 	 * ranges from being reused for a new block group. This is needed to
10896b7304afSFilipe Manana 	 * avoid races with trimming and scrub.
10906b7304afSFilipe Manana 	 *
10916b7304afSFilipe Manana 	 * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
1092e3e0520bSJosef Bacik 	 * completely transactionless, so while it is trimming a range the
1093e3e0520bSJosef Bacik 	 * currently running transaction might finish and a new one start,
1094e3e0520bSJosef Bacik 	 * allowing for new block groups to be created that can reuse the same
1095e3e0520bSJosef Bacik 	 * physical device locations unless we take this special care.
1096e3e0520bSJosef Bacik 	 *
1097e3e0520bSJosef Bacik 	 * There may also be an implicit trim operation if the file system
1098e3e0520bSJosef Bacik 	 * is mounted with -odiscard. The same protections must remain
1099e3e0520bSJosef Bacik 	 * in place until the extents have been discarded completely when
1100e3e0520bSJosef Bacik 	 * the transaction commit has completed.
1101e3e0520bSJosef Bacik 	 */
11026b7304afSFilipe Manana 	remove_em = (atomic_read(&block_group->frozen) == 0);
1103e3e0520bSJosef Bacik 	spin_unlock(&block_group->lock);
1104e3e0520bSJosef Bacik 
1105e3e0520bSJosef Bacik 	if (remove_em) {
1106e3e0520bSJosef Bacik 		struct extent_map_tree *em_tree;
1107e3e0520bSJosef Bacik 
1108e3e0520bSJosef Bacik 		em_tree = &fs_info->mapping_tree;
1109e3e0520bSJosef Bacik 		write_lock(&em_tree->lock);
1110e3e0520bSJosef Bacik 		remove_extent_mapping(em_tree, em);
1111e3e0520bSJosef Bacik 		write_unlock(&em_tree->lock);
1112e3e0520bSJosef Bacik 		/* once for the tree */
1113e3e0520bSJosef Bacik 		free_extent_map(em);
1114e3e0520bSJosef Bacik 	}
1115f6033c5eSXiyu Yang 
11169fecd132SFilipe Manana out:
1117f6033c5eSXiyu Yang 	/* Once for the lookup reference */
1118f6033c5eSXiyu Yang 	btrfs_put_block_group(block_group);
1119e3e0520bSJosef Bacik 	if (remove_rsv)
1120e3e0520bSJosef Bacik 		btrfs_delayed_refs_rsv_release(fs_info, 1);
1121e3e0520bSJosef Bacik 	btrfs_free_path(path);
1122e3e0520bSJosef Bacik 	return ret;
1123e3e0520bSJosef Bacik }
1124e3e0520bSJosef Bacik 
1125e3e0520bSJosef Bacik struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
1126e3e0520bSJosef Bacik 		struct btrfs_fs_info *fs_info, const u64 chunk_offset)
1127e3e0520bSJosef Bacik {
1128dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
1129e3e0520bSJosef Bacik 	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1130e3e0520bSJosef Bacik 	struct extent_map *em;
1131e3e0520bSJosef Bacik 	struct map_lookup *map;
1132e3e0520bSJosef Bacik 	unsigned int num_items;
1133e3e0520bSJosef Bacik 
1134e3e0520bSJosef Bacik 	read_lock(&em_tree->lock);
1135e3e0520bSJosef Bacik 	em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1136e3e0520bSJosef Bacik 	read_unlock(&em_tree->lock);
1137e3e0520bSJosef Bacik 	ASSERT(em && em->start == chunk_offset);
1138e3e0520bSJosef Bacik 
1139e3e0520bSJosef Bacik 	/*
1140e3e0520bSJosef Bacik 	 * We need to reserve 3 + N units from the metadata space info in order
1141e3e0520bSJosef Bacik 	 * to remove a block group (done at btrfs_remove_chunk() and at
1142e3e0520bSJosef Bacik 	 * btrfs_remove_block_group()), which are used for:
1143e3e0520bSJosef Bacik 	 *
1144e3e0520bSJosef Bacik 	 * 1 unit for adding the free space inode's orphan (located in the tree
1145e3e0520bSJosef Bacik 	 * of tree roots).
1146e3e0520bSJosef Bacik 	 * 1 unit for deleting the block group item (located in the extent
1147e3e0520bSJosef Bacik 	 * tree).
1148e3e0520bSJosef Bacik 	 * 1 unit for deleting the free space item (located in tree of tree
1149e3e0520bSJosef Bacik 	 * roots).
1150e3e0520bSJosef Bacik 	 * N units for deleting N device extent items corresponding to each
1151e3e0520bSJosef Bacik 	 * stripe (located in the device tree).
1152e3e0520bSJosef Bacik 	 *
1153e3e0520bSJosef Bacik 	 * In order to remove a block group we also need to reserve units in the
1154e3e0520bSJosef Bacik 	 * system space info in order to update the chunk tree (update one or
1155e3e0520bSJosef Bacik 	 * more device items and remove one chunk item), but this is done at
1156e3e0520bSJosef Bacik 	 * btrfs_remove_chunk() through a call to check_system_chunk().
1157e3e0520bSJosef Bacik 	 */
1158e3e0520bSJosef Bacik 	map = em->map_lookup;
1159e3e0520bSJosef Bacik 	num_items = 3 + map->num_stripes;
1160e3e0520bSJosef Bacik 	free_extent_map(em);
1161e3e0520bSJosef Bacik 
1162dfe8aec4SJosef Bacik 	return btrfs_start_transaction_fallback_global_rsv(root, num_items);
1163e3e0520bSJosef Bacik }
1164e3e0520bSJosef Bacik 
1165e3e0520bSJosef Bacik /*
116626ce2095SJosef Bacik  * Mark block group @cache read-only, so later write won't happen to block
116726ce2095SJosef Bacik  * group @cache.
116826ce2095SJosef Bacik  *
116926ce2095SJosef Bacik  * If @force is not set, this function will only mark the block group readonly
117026ce2095SJosef Bacik  * if we have enough free space (1M) in other metadata/system block groups.
117126ce2095SJosef Bacik  * If @force is not set, this function will mark the block group readonly
117226ce2095SJosef Bacik  * without checking free space.
117326ce2095SJosef Bacik  *
117426ce2095SJosef Bacik  * NOTE: This function doesn't care if other block groups can contain all the
117526ce2095SJosef Bacik  * data in this block group. That check should be done by relocation routine,
117626ce2095SJosef Bacik  * not this function.
117726ce2095SJosef Bacik  */
117832da5386SDavid Sterba static int inc_block_group_ro(struct btrfs_block_group *cache, int force)
117926ce2095SJosef Bacik {
118026ce2095SJosef Bacik 	struct btrfs_space_info *sinfo = cache->space_info;
118126ce2095SJosef Bacik 	u64 num_bytes;
118226ce2095SJosef Bacik 	int ret = -ENOSPC;
118326ce2095SJosef Bacik 
118426ce2095SJosef Bacik 	spin_lock(&sinfo->lock);
118526ce2095SJosef Bacik 	spin_lock(&cache->lock);
118626ce2095SJosef Bacik 
1187195a49eaSFilipe Manana 	if (cache->swap_extents) {
1188195a49eaSFilipe Manana 		ret = -ETXTBSY;
1189195a49eaSFilipe Manana 		goto out;
1190195a49eaSFilipe Manana 	}
1191195a49eaSFilipe Manana 
119226ce2095SJosef Bacik 	if (cache->ro) {
119326ce2095SJosef Bacik 		cache->ro++;
119426ce2095SJosef Bacik 		ret = 0;
119526ce2095SJosef Bacik 		goto out;
119626ce2095SJosef Bacik 	}
119726ce2095SJosef Bacik 
1198b3470b5dSDavid Sterba 	num_bytes = cache->length - cache->reserved - cache->pinned -
1199169e0da9SNaohiro Aota 		    cache->bytes_super - cache->zone_unusable - cache->used;
120026ce2095SJosef Bacik 
120126ce2095SJosef Bacik 	/*
1202a30a3d20SJosef Bacik 	 * Data never overcommits, even in mixed mode, so do just the straight
1203a30a3d20SJosef Bacik 	 * check of left over space in how much we have allocated.
1204a30a3d20SJosef Bacik 	 */
1205a30a3d20SJosef Bacik 	if (force) {
1206a30a3d20SJosef Bacik 		ret = 0;
1207a30a3d20SJosef Bacik 	} else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) {
1208a30a3d20SJosef Bacik 		u64 sinfo_used = btrfs_space_info_used(sinfo, true);
1209a30a3d20SJosef Bacik 
1210a30a3d20SJosef Bacik 		/*
121126ce2095SJosef Bacik 		 * Here we make sure if we mark this bg RO, we still have enough
1212f8935566SJosef Bacik 		 * free space as buffer.
121326ce2095SJosef Bacik 		 */
1214a30a3d20SJosef Bacik 		if (sinfo_used + num_bytes <= sinfo->total_bytes)
1215a30a3d20SJosef Bacik 			ret = 0;
1216a30a3d20SJosef Bacik 	} else {
1217a30a3d20SJosef Bacik 		/*
1218a30a3d20SJosef Bacik 		 * We overcommit metadata, so we need to do the
1219a30a3d20SJosef Bacik 		 * btrfs_can_overcommit check here, and we need to pass in
1220a30a3d20SJosef Bacik 		 * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of
1221a30a3d20SJosef Bacik 		 * leeway to allow us to mark this block group as read only.
1222a30a3d20SJosef Bacik 		 */
1223a30a3d20SJosef Bacik 		if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes,
1224a30a3d20SJosef Bacik 					 BTRFS_RESERVE_NO_FLUSH))
1225a30a3d20SJosef Bacik 			ret = 0;
1226a30a3d20SJosef Bacik 	}
1227a30a3d20SJosef Bacik 
1228a30a3d20SJosef Bacik 	if (!ret) {
122926ce2095SJosef Bacik 		sinfo->bytes_readonly += num_bytes;
1230169e0da9SNaohiro Aota 		if (btrfs_is_zoned(cache->fs_info)) {
1231169e0da9SNaohiro Aota 			/* Migrate zone_unusable bytes to readonly */
1232169e0da9SNaohiro Aota 			sinfo->bytes_readonly += cache->zone_unusable;
1233169e0da9SNaohiro Aota 			sinfo->bytes_zone_unusable -= cache->zone_unusable;
1234169e0da9SNaohiro Aota 			cache->zone_unusable = 0;
1235169e0da9SNaohiro Aota 		}
123626ce2095SJosef Bacik 		cache->ro++;
123726ce2095SJosef Bacik 		list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
123826ce2095SJosef Bacik 	}
123926ce2095SJosef Bacik out:
124026ce2095SJosef Bacik 	spin_unlock(&cache->lock);
124126ce2095SJosef Bacik 	spin_unlock(&sinfo->lock);
124226ce2095SJosef Bacik 	if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) {
124326ce2095SJosef Bacik 		btrfs_info(cache->fs_info,
1244b3470b5dSDavid Sterba 			"unable to make block group %llu ro", cache->start);
124526ce2095SJosef Bacik 		btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0);
124626ce2095SJosef Bacik 	}
124726ce2095SJosef Bacik 	return ret;
124826ce2095SJosef Bacik }
124926ce2095SJosef Bacik 
1250fe119a6eSNikolay Borisov static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
1251fe119a6eSNikolay Borisov 				 struct btrfs_block_group *bg)
125245bb5d6aSNikolay Borisov {
125345bb5d6aSNikolay Borisov 	struct btrfs_fs_info *fs_info = bg->fs_info;
1254fe119a6eSNikolay Borisov 	struct btrfs_transaction *prev_trans = NULL;
125545bb5d6aSNikolay Borisov 	const u64 start = bg->start;
125645bb5d6aSNikolay Borisov 	const u64 end = start + bg->length - 1;
125745bb5d6aSNikolay Borisov 	int ret;
125845bb5d6aSNikolay Borisov 
1259fe119a6eSNikolay Borisov 	spin_lock(&fs_info->trans_lock);
1260fe119a6eSNikolay Borisov 	if (trans->transaction->list.prev != &fs_info->trans_list) {
1261fe119a6eSNikolay Borisov 		prev_trans = list_last_entry(&trans->transaction->list,
1262fe119a6eSNikolay Borisov 					     struct btrfs_transaction, list);
1263fe119a6eSNikolay Borisov 		refcount_inc(&prev_trans->use_count);
1264fe119a6eSNikolay Borisov 	}
1265fe119a6eSNikolay Borisov 	spin_unlock(&fs_info->trans_lock);
1266fe119a6eSNikolay Borisov 
126745bb5d6aSNikolay Borisov 	/*
126845bb5d6aSNikolay Borisov 	 * Hold the unused_bg_unpin_mutex lock to avoid racing with
126945bb5d6aSNikolay Borisov 	 * btrfs_finish_extent_commit(). If we are at transaction N, another
127045bb5d6aSNikolay Borisov 	 * task might be running finish_extent_commit() for the previous
127145bb5d6aSNikolay Borisov 	 * transaction N - 1, and have seen a range belonging to the block
1272fe119a6eSNikolay Borisov 	 * group in pinned_extents before we were able to clear the whole block
1273fe119a6eSNikolay Borisov 	 * group range from pinned_extents. This means that task can lookup for
1274fe119a6eSNikolay Borisov 	 * the block group after we unpinned it from pinned_extents and removed
1275fe119a6eSNikolay Borisov 	 * it, leading to a BUG_ON() at unpin_extent_range().
127645bb5d6aSNikolay Borisov 	 */
127745bb5d6aSNikolay Borisov 	mutex_lock(&fs_info->unused_bg_unpin_mutex);
1278fe119a6eSNikolay Borisov 	if (prev_trans) {
1279fe119a6eSNikolay Borisov 		ret = clear_extent_bits(&prev_trans->pinned_extents, start, end,
128045bb5d6aSNikolay Borisov 					EXTENT_DIRTY);
128145bb5d6aSNikolay Borisov 		if (ret)
1282534cf531SFilipe Manana 			goto out;
1283fe119a6eSNikolay Borisov 	}
128445bb5d6aSNikolay Borisov 
1285fe119a6eSNikolay Borisov 	ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end,
128645bb5d6aSNikolay Borisov 				EXTENT_DIRTY);
1287534cf531SFilipe Manana out:
128845bb5d6aSNikolay Borisov 	mutex_unlock(&fs_info->unused_bg_unpin_mutex);
12895150bf19SFilipe Manana 	if (prev_trans)
12905150bf19SFilipe Manana 		btrfs_put_transaction(prev_trans);
129145bb5d6aSNikolay Borisov 
1292534cf531SFilipe Manana 	return ret == 0;
129345bb5d6aSNikolay Borisov }
129445bb5d6aSNikolay Borisov 
129526ce2095SJosef Bacik /*
1296e3e0520bSJosef Bacik  * Process the unused_bgs list and remove any that don't have any allocated
1297e3e0520bSJosef Bacik  * space inside of them.
1298e3e0520bSJosef Bacik  */
1299e3e0520bSJosef Bacik void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
1300e3e0520bSJosef Bacik {
130132da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1302e3e0520bSJosef Bacik 	struct btrfs_space_info *space_info;
1303e3e0520bSJosef Bacik 	struct btrfs_trans_handle *trans;
13046e80d4f8SDennis Zhou 	const bool async_trim_enabled = btrfs_test_opt(fs_info, DISCARD_ASYNC);
1305e3e0520bSJosef Bacik 	int ret = 0;
1306e3e0520bSJosef Bacik 
1307e3e0520bSJosef Bacik 	if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1308e3e0520bSJosef Bacik 		return;
1309e3e0520bSJosef Bacik 
13102f12741fSJosef Bacik 	if (btrfs_fs_closing(fs_info))
13112f12741fSJosef Bacik 		return;
13122f12741fSJosef Bacik 
1313ddfd08cbSJosef Bacik 	/*
1314ddfd08cbSJosef Bacik 	 * Long running balances can keep us blocked here for eternity, so
1315ddfd08cbSJosef Bacik 	 * simply skip deletion if we're unable to get the mutex.
1316ddfd08cbSJosef Bacik 	 */
1317f3372065SJohannes Thumshirn 	if (!mutex_trylock(&fs_info->reclaim_bgs_lock))
1318ddfd08cbSJosef Bacik 		return;
1319ddfd08cbSJosef Bacik 
1320e3e0520bSJosef Bacik 	spin_lock(&fs_info->unused_bgs_lock);
1321e3e0520bSJosef Bacik 	while (!list_empty(&fs_info->unused_bgs)) {
1322e3e0520bSJosef Bacik 		int trimming;
1323e3e0520bSJosef Bacik 
1324e3e0520bSJosef Bacik 		block_group = list_first_entry(&fs_info->unused_bgs,
132532da5386SDavid Sterba 					       struct btrfs_block_group,
1326e3e0520bSJosef Bacik 					       bg_list);
1327e3e0520bSJosef Bacik 		list_del_init(&block_group->bg_list);
1328e3e0520bSJosef Bacik 
1329e3e0520bSJosef Bacik 		space_info = block_group->space_info;
1330e3e0520bSJosef Bacik 
1331e3e0520bSJosef Bacik 		if (ret || btrfs_mixed_space_info(space_info)) {
1332e3e0520bSJosef Bacik 			btrfs_put_block_group(block_group);
1333e3e0520bSJosef Bacik 			continue;
1334e3e0520bSJosef Bacik 		}
1335e3e0520bSJosef Bacik 		spin_unlock(&fs_info->unused_bgs_lock);
1336e3e0520bSJosef Bacik 
1337b0643e59SDennis Zhou 		btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
1338b0643e59SDennis Zhou 
1339e3e0520bSJosef Bacik 		/* Don't want to race with allocators so take the groups_sem */
1340e3e0520bSJosef Bacik 		down_write(&space_info->groups_sem);
13416e80d4f8SDennis Zhou 
13426e80d4f8SDennis Zhou 		/*
13436e80d4f8SDennis Zhou 		 * Async discard moves the final block group discard to be prior
13446e80d4f8SDennis Zhou 		 * to the unused_bgs code path.  Therefore, if it's not fully
13456e80d4f8SDennis Zhou 		 * trimmed, punt it back to the async discard lists.
13466e80d4f8SDennis Zhou 		 */
13476e80d4f8SDennis Zhou 		if (btrfs_test_opt(fs_info, DISCARD_ASYNC) &&
13486e80d4f8SDennis Zhou 		    !btrfs_is_free_space_trimmed(block_group)) {
13496e80d4f8SDennis Zhou 			trace_btrfs_skip_unused_block_group(block_group);
13506e80d4f8SDennis Zhou 			up_write(&space_info->groups_sem);
13516e80d4f8SDennis Zhou 			/* Requeue if we failed because of async discard */
13526e80d4f8SDennis Zhou 			btrfs_discard_queue_work(&fs_info->discard_ctl,
13536e80d4f8SDennis Zhou 						 block_group);
13546e80d4f8SDennis Zhou 			goto next;
13556e80d4f8SDennis Zhou 		}
13566e80d4f8SDennis Zhou 
1357e3e0520bSJosef Bacik 		spin_lock(&block_group->lock);
1358e3e0520bSJosef Bacik 		if (block_group->reserved || block_group->pinned ||
1359bf38be65SDavid Sterba 		    block_group->used || block_group->ro ||
1360e3e0520bSJosef Bacik 		    list_is_singular(&block_group->list)) {
1361e3e0520bSJosef Bacik 			/*
1362e3e0520bSJosef Bacik 			 * We want to bail if we made new allocations or have
1363e3e0520bSJosef Bacik 			 * outstanding allocations in this block group.  We do
1364e3e0520bSJosef Bacik 			 * the ro check in case balance is currently acting on
1365e3e0520bSJosef Bacik 			 * this block group.
1366e3e0520bSJosef Bacik 			 */
1367e3e0520bSJosef Bacik 			trace_btrfs_skip_unused_block_group(block_group);
1368e3e0520bSJosef Bacik 			spin_unlock(&block_group->lock);
1369e3e0520bSJosef Bacik 			up_write(&space_info->groups_sem);
1370e3e0520bSJosef Bacik 			goto next;
1371e3e0520bSJosef Bacik 		}
1372e3e0520bSJosef Bacik 		spin_unlock(&block_group->lock);
1373e3e0520bSJosef Bacik 
1374e3e0520bSJosef Bacik 		/* We don't want to force the issue, only flip if it's ok. */
1375e11c0406SJosef Bacik 		ret = inc_block_group_ro(block_group, 0);
1376e3e0520bSJosef Bacik 		up_write(&space_info->groups_sem);
1377e3e0520bSJosef Bacik 		if (ret < 0) {
1378e3e0520bSJosef Bacik 			ret = 0;
1379e3e0520bSJosef Bacik 			goto next;
1380e3e0520bSJosef Bacik 		}
1381e3e0520bSJosef Bacik 
138274e91b12SNaohiro Aota 		ret = btrfs_zone_finish(block_group);
138374e91b12SNaohiro Aota 		if (ret < 0) {
138474e91b12SNaohiro Aota 			btrfs_dec_block_group_ro(block_group);
138574e91b12SNaohiro Aota 			if (ret == -EAGAIN)
138674e91b12SNaohiro Aota 				ret = 0;
138774e91b12SNaohiro Aota 			goto next;
138874e91b12SNaohiro Aota 		}
138974e91b12SNaohiro Aota 
1390e3e0520bSJosef Bacik 		/*
1391e3e0520bSJosef Bacik 		 * Want to do this before we do anything else so we can recover
1392e3e0520bSJosef Bacik 		 * properly if we fail to join the transaction.
1393e3e0520bSJosef Bacik 		 */
1394e3e0520bSJosef Bacik 		trans = btrfs_start_trans_remove_block_group(fs_info,
1395b3470b5dSDavid Sterba 						     block_group->start);
1396e3e0520bSJosef Bacik 		if (IS_ERR(trans)) {
1397e3e0520bSJosef Bacik 			btrfs_dec_block_group_ro(block_group);
1398e3e0520bSJosef Bacik 			ret = PTR_ERR(trans);
1399e3e0520bSJosef Bacik 			goto next;
1400e3e0520bSJosef Bacik 		}
1401e3e0520bSJosef Bacik 
1402e3e0520bSJosef Bacik 		/*
1403e3e0520bSJosef Bacik 		 * We could have pending pinned extents for this block group,
1404e3e0520bSJosef Bacik 		 * just delete them, we don't care about them anymore.
1405e3e0520bSJosef Bacik 		 */
1406534cf531SFilipe Manana 		if (!clean_pinned_extents(trans, block_group)) {
1407534cf531SFilipe Manana 			btrfs_dec_block_group_ro(block_group);
1408e3e0520bSJosef Bacik 			goto end_trans;
1409534cf531SFilipe Manana 		}
1410e3e0520bSJosef Bacik 
1411b0643e59SDennis Zhou 		/*
1412b0643e59SDennis Zhou 		 * At this point, the block_group is read only and should fail
1413b0643e59SDennis Zhou 		 * new allocations.  However, btrfs_finish_extent_commit() can
1414b0643e59SDennis Zhou 		 * cause this block_group to be placed back on the discard
1415b0643e59SDennis Zhou 		 * lists because now the block_group isn't fully discarded.
1416b0643e59SDennis Zhou 		 * Bail here and try again later after discarding everything.
1417b0643e59SDennis Zhou 		 */
1418b0643e59SDennis Zhou 		spin_lock(&fs_info->discard_ctl.lock);
1419b0643e59SDennis Zhou 		if (!list_empty(&block_group->discard_list)) {
1420b0643e59SDennis Zhou 			spin_unlock(&fs_info->discard_ctl.lock);
1421b0643e59SDennis Zhou 			btrfs_dec_block_group_ro(block_group);
1422b0643e59SDennis Zhou 			btrfs_discard_queue_work(&fs_info->discard_ctl,
1423b0643e59SDennis Zhou 						 block_group);
1424b0643e59SDennis Zhou 			goto end_trans;
1425b0643e59SDennis Zhou 		}
1426b0643e59SDennis Zhou 		spin_unlock(&fs_info->discard_ctl.lock);
1427b0643e59SDennis Zhou 
1428e3e0520bSJosef Bacik 		/* Reset pinned so btrfs_put_block_group doesn't complain */
1429e3e0520bSJosef Bacik 		spin_lock(&space_info->lock);
1430e3e0520bSJosef Bacik 		spin_lock(&block_group->lock);
1431e3e0520bSJosef Bacik 
1432e3e0520bSJosef Bacik 		btrfs_space_info_update_bytes_pinned(fs_info, space_info,
1433e3e0520bSJosef Bacik 						     -block_group->pinned);
1434e3e0520bSJosef Bacik 		space_info->bytes_readonly += block_group->pinned;
1435e3e0520bSJosef Bacik 		block_group->pinned = 0;
1436e3e0520bSJosef Bacik 
1437e3e0520bSJosef Bacik 		spin_unlock(&block_group->lock);
1438e3e0520bSJosef Bacik 		spin_unlock(&space_info->lock);
1439e3e0520bSJosef Bacik 
14406e80d4f8SDennis Zhou 		/*
14416e80d4f8SDennis Zhou 		 * The normal path here is an unused block group is passed here,
14426e80d4f8SDennis Zhou 		 * then trimming is handled in the transaction commit path.
14436e80d4f8SDennis Zhou 		 * Async discard interposes before this to do the trimming
14446e80d4f8SDennis Zhou 		 * before coming down the unused block group path as trimming
14456e80d4f8SDennis Zhou 		 * will no longer be done later in the transaction commit path.
14466e80d4f8SDennis Zhou 		 */
14476e80d4f8SDennis Zhou 		if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC))
14486e80d4f8SDennis Zhou 			goto flip_async;
14496e80d4f8SDennis Zhou 
1450dcba6e48SNaohiro Aota 		/*
1451dcba6e48SNaohiro Aota 		 * DISCARD can flip during remount. On zoned filesystems, we
1452dcba6e48SNaohiro Aota 		 * need to reset sequential-required zones.
1453dcba6e48SNaohiro Aota 		 */
1454dcba6e48SNaohiro Aota 		trimming = btrfs_test_opt(fs_info, DISCARD_SYNC) ||
1455dcba6e48SNaohiro Aota 				btrfs_is_zoned(fs_info);
1456e3e0520bSJosef Bacik 
1457e3e0520bSJosef Bacik 		/* Implicit trim during transaction commit. */
1458e3e0520bSJosef Bacik 		if (trimming)
14596b7304afSFilipe Manana 			btrfs_freeze_block_group(block_group);
1460e3e0520bSJosef Bacik 
1461e3e0520bSJosef Bacik 		/*
1462e3e0520bSJosef Bacik 		 * Btrfs_remove_chunk will abort the transaction if things go
1463e3e0520bSJosef Bacik 		 * horribly wrong.
1464e3e0520bSJosef Bacik 		 */
1465b3470b5dSDavid Sterba 		ret = btrfs_remove_chunk(trans, block_group->start);
1466e3e0520bSJosef Bacik 
1467e3e0520bSJosef Bacik 		if (ret) {
1468e3e0520bSJosef Bacik 			if (trimming)
14696b7304afSFilipe Manana 				btrfs_unfreeze_block_group(block_group);
1470e3e0520bSJosef Bacik 			goto end_trans;
1471e3e0520bSJosef Bacik 		}
1472e3e0520bSJosef Bacik 
1473e3e0520bSJosef Bacik 		/*
1474e3e0520bSJosef Bacik 		 * If we're not mounted with -odiscard, we can just forget
1475e3e0520bSJosef Bacik 		 * about this block group. Otherwise we'll need to wait
1476e3e0520bSJosef Bacik 		 * until transaction commit to do the actual discard.
1477e3e0520bSJosef Bacik 		 */
1478e3e0520bSJosef Bacik 		if (trimming) {
1479e3e0520bSJosef Bacik 			spin_lock(&fs_info->unused_bgs_lock);
1480e3e0520bSJosef Bacik 			/*
1481e3e0520bSJosef Bacik 			 * A concurrent scrub might have added us to the list
1482e3e0520bSJosef Bacik 			 * fs_info->unused_bgs, so use a list_move operation
1483e3e0520bSJosef Bacik 			 * to add the block group to the deleted_bgs list.
1484e3e0520bSJosef Bacik 			 */
1485e3e0520bSJosef Bacik 			list_move(&block_group->bg_list,
1486e3e0520bSJosef Bacik 				  &trans->transaction->deleted_bgs);
1487e3e0520bSJosef Bacik 			spin_unlock(&fs_info->unused_bgs_lock);
1488e3e0520bSJosef Bacik 			btrfs_get_block_group(block_group);
1489e3e0520bSJosef Bacik 		}
1490e3e0520bSJosef Bacik end_trans:
1491e3e0520bSJosef Bacik 		btrfs_end_transaction(trans);
1492e3e0520bSJosef Bacik next:
1493e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
1494e3e0520bSJosef Bacik 		spin_lock(&fs_info->unused_bgs_lock);
1495e3e0520bSJosef Bacik 	}
1496e3e0520bSJosef Bacik 	spin_unlock(&fs_info->unused_bgs_lock);
1497f3372065SJohannes Thumshirn 	mutex_unlock(&fs_info->reclaim_bgs_lock);
14986e80d4f8SDennis Zhou 	return;
14996e80d4f8SDennis Zhou 
15006e80d4f8SDennis Zhou flip_async:
15016e80d4f8SDennis Zhou 	btrfs_end_transaction(trans);
1502f3372065SJohannes Thumshirn 	mutex_unlock(&fs_info->reclaim_bgs_lock);
15036e80d4f8SDennis Zhou 	btrfs_put_block_group(block_group);
15046e80d4f8SDennis Zhou 	btrfs_discard_punt_unused_bgs_list(fs_info);
1505e3e0520bSJosef Bacik }
1506e3e0520bSJosef Bacik 
150732da5386SDavid Sterba void btrfs_mark_bg_unused(struct btrfs_block_group *bg)
1508e3e0520bSJosef Bacik {
1509e3e0520bSJosef Bacik 	struct btrfs_fs_info *fs_info = bg->fs_info;
1510e3e0520bSJosef Bacik 
1511e3e0520bSJosef Bacik 	spin_lock(&fs_info->unused_bgs_lock);
1512e3e0520bSJosef Bacik 	if (list_empty(&bg->bg_list)) {
1513e3e0520bSJosef Bacik 		btrfs_get_block_group(bg);
1514e3e0520bSJosef Bacik 		trace_btrfs_add_unused_block_group(bg);
1515e3e0520bSJosef Bacik 		list_add_tail(&bg->bg_list, &fs_info->unused_bgs);
1516e3e0520bSJosef Bacik 	}
1517e3e0520bSJosef Bacik 	spin_unlock(&fs_info->unused_bgs_lock);
1518e3e0520bSJosef Bacik }
15194358d963SJosef Bacik 
15202ca0ec77SJohannes Thumshirn /*
15212ca0ec77SJohannes Thumshirn  * We want block groups with a low number of used bytes to be in the beginning
15222ca0ec77SJohannes Thumshirn  * of the list, so they will get reclaimed first.
15232ca0ec77SJohannes Thumshirn  */
15242ca0ec77SJohannes Thumshirn static int reclaim_bgs_cmp(void *unused, const struct list_head *a,
15252ca0ec77SJohannes Thumshirn 			   const struct list_head *b)
15262ca0ec77SJohannes Thumshirn {
15272ca0ec77SJohannes Thumshirn 	const struct btrfs_block_group *bg1, *bg2;
15282ca0ec77SJohannes Thumshirn 
15292ca0ec77SJohannes Thumshirn 	bg1 = list_entry(a, struct btrfs_block_group, bg_list);
15302ca0ec77SJohannes Thumshirn 	bg2 = list_entry(b, struct btrfs_block_group, bg_list);
15312ca0ec77SJohannes Thumshirn 
15322ca0ec77SJohannes Thumshirn 	return bg1->used > bg2->used;
15332ca0ec77SJohannes Thumshirn }
15342ca0ec77SJohannes Thumshirn 
15353687fcb0SJohannes Thumshirn static inline bool btrfs_should_reclaim(struct btrfs_fs_info *fs_info)
15363687fcb0SJohannes Thumshirn {
15373687fcb0SJohannes Thumshirn 	if (btrfs_is_zoned(fs_info))
15383687fcb0SJohannes Thumshirn 		return btrfs_zoned_should_reclaim(fs_info);
15393687fcb0SJohannes Thumshirn 	return true;
15403687fcb0SJohannes Thumshirn }
15413687fcb0SJohannes Thumshirn 
154218bb8bbfSJohannes Thumshirn void btrfs_reclaim_bgs_work(struct work_struct *work)
154318bb8bbfSJohannes Thumshirn {
154418bb8bbfSJohannes Thumshirn 	struct btrfs_fs_info *fs_info =
154518bb8bbfSJohannes Thumshirn 		container_of(work, struct btrfs_fs_info, reclaim_bgs_work);
154618bb8bbfSJohannes Thumshirn 	struct btrfs_block_group *bg;
154718bb8bbfSJohannes Thumshirn 	struct btrfs_space_info *space_info;
154818bb8bbfSJohannes Thumshirn 
154918bb8bbfSJohannes Thumshirn 	if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
155018bb8bbfSJohannes Thumshirn 		return;
155118bb8bbfSJohannes Thumshirn 
15522f12741fSJosef Bacik 	if (btrfs_fs_closing(fs_info))
15532f12741fSJosef Bacik 		return;
15542f12741fSJosef Bacik 
15553687fcb0SJohannes Thumshirn 	if (!btrfs_should_reclaim(fs_info))
15563687fcb0SJohannes Thumshirn 		return;
15573687fcb0SJohannes Thumshirn 
1558ca5e4ea0SNaohiro Aota 	sb_start_write(fs_info->sb);
1559ca5e4ea0SNaohiro Aota 
1560ca5e4ea0SNaohiro Aota 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
1561ca5e4ea0SNaohiro Aota 		sb_end_write(fs_info->sb);
156218bb8bbfSJohannes Thumshirn 		return;
1563ca5e4ea0SNaohiro Aota 	}
156418bb8bbfSJohannes Thumshirn 
15659cc0b837SJohannes Thumshirn 	/*
15669cc0b837SJohannes Thumshirn 	 * Long running balances can keep us blocked here for eternity, so
15679cc0b837SJohannes Thumshirn 	 * simply skip reclaim if we're unable to get the mutex.
15689cc0b837SJohannes Thumshirn 	 */
15699cc0b837SJohannes Thumshirn 	if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) {
15709cc0b837SJohannes Thumshirn 		btrfs_exclop_finish(fs_info);
1571ca5e4ea0SNaohiro Aota 		sb_end_write(fs_info->sb);
15729cc0b837SJohannes Thumshirn 		return;
15739cc0b837SJohannes Thumshirn 	}
15749cc0b837SJohannes Thumshirn 
157518bb8bbfSJohannes Thumshirn 	spin_lock(&fs_info->unused_bgs_lock);
15762ca0ec77SJohannes Thumshirn 	/*
15772ca0ec77SJohannes Thumshirn 	 * Sort happens under lock because we can't simply splice it and sort.
15782ca0ec77SJohannes Thumshirn 	 * The block groups might still be in use and reachable via bg_list,
15792ca0ec77SJohannes Thumshirn 	 * and their presence in the reclaim_bgs list must be preserved.
15802ca0ec77SJohannes Thumshirn 	 */
15812ca0ec77SJohannes Thumshirn 	list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp);
158218bb8bbfSJohannes Thumshirn 	while (!list_empty(&fs_info->reclaim_bgs)) {
15835f93e776SJohannes Thumshirn 		u64 zone_unusable;
15841cea5cf0SFilipe Manana 		int ret = 0;
15851cea5cf0SFilipe Manana 
158618bb8bbfSJohannes Thumshirn 		bg = list_first_entry(&fs_info->reclaim_bgs,
158718bb8bbfSJohannes Thumshirn 				      struct btrfs_block_group,
158818bb8bbfSJohannes Thumshirn 				      bg_list);
158918bb8bbfSJohannes Thumshirn 		list_del_init(&bg->bg_list);
159018bb8bbfSJohannes Thumshirn 
159118bb8bbfSJohannes Thumshirn 		space_info = bg->space_info;
159218bb8bbfSJohannes Thumshirn 		spin_unlock(&fs_info->unused_bgs_lock);
159318bb8bbfSJohannes Thumshirn 
159418bb8bbfSJohannes Thumshirn 		/* Don't race with allocators so take the groups_sem */
159518bb8bbfSJohannes Thumshirn 		down_write(&space_info->groups_sem);
159618bb8bbfSJohannes Thumshirn 
159718bb8bbfSJohannes Thumshirn 		spin_lock(&bg->lock);
159818bb8bbfSJohannes Thumshirn 		if (bg->reserved || bg->pinned || bg->ro) {
159918bb8bbfSJohannes Thumshirn 			/*
160018bb8bbfSJohannes Thumshirn 			 * We want to bail if we made new allocations or have
160118bb8bbfSJohannes Thumshirn 			 * outstanding allocations in this block group.  We do
160218bb8bbfSJohannes Thumshirn 			 * the ro check in case balance is currently acting on
160318bb8bbfSJohannes Thumshirn 			 * this block group.
160418bb8bbfSJohannes Thumshirn 			 */
160518bb8bbfSJohannes Thumshirn 			spin_unlock(&bg->lock);
160618bb8bbfSJohannes Thumshirn 			up_write(&space_info->groups_sem);
160718bb8bbfSJohannes Thumshirn 			goto next;
160818bb8bbfSJohannes Thumshirn 		}
1609*cc4804bfSBoris Burkov 		if (bg->used == 0) {
1610*cc4804bfSBoris Burkov 			/*
1611*cc4804bfSBoris Burkov 			 * It is possible that we trigger relocation on a block
1612*cc4804bfSBoris Burkov 			 * group as its extents are deleted and it first goes
1613*cc4804bfSBoris Burkov 			 * below the threshold, then shortly after goes empty.
1614*cc4804bfSBoris Burkov 			 *
1615*cc4804bfSBoris Burkov 			 * In this case, relocating it does delete it, but has
1616*cc4804bfSBoris Burkov 			 * some overhead in relocation specific metadata, looking
1617*cc4804bfSBoris Burkov 			 * for the non-existent extents and running some extra
1618*cc4804bfSBoris Burkov 			 * transactions, which we can avoid by using one of the
1619*cc4804bfSBoris Burkov 			 * other mechanisms for dealing with empty block groups.
1620*cc4804bfSBoris Burkov 			 */
1621*cc4804bfSBoris Burkov 			if (!btrfs_test_opt(fs_info, DISCARD_ASYNC))
1622*cc4804bfSBoris Burkov 				btrfs_mark_bg_unused(bg);
1623*cc4804bfSBoris Burkov 			spin_unlock(&bg->lock);
1624*cc4804bfSBoris Burkov 			up_write(&space_info->groups_sem);
1625*cc4804bfSBoris Burkov 			goto next;
1626*cc4804bfSBoris Burkov 		}
162718bb8bbfSJohannes Thumshirn 		spin_unlock(&bg->lock);
162818bb8bbfSJohannes Thumshirn 
162918bb8bbfSJohannes Thumshirn 		/* Get out fast, in case we're unmounting the filesystem */
163018bb8bbfSJohannes Thumshirn 		if (btrfs_fs_closing(fs_info)) {
163118bb8bbfSJohannes Thumshirn 			up_write(&space_info->groups_sem);
163218bb8bbfSJohannes Thumshirn 			goto next;
163318bb8bbfSJohannes Thumshirn 		}
163418bb8bbfSJohannes Thumshirn 
16355f93e776SJohannes Thumshirn 		/*
16365f93e776SJohannes Thumshirn 		 * Cache the zone_unusable value before turning the block group
16375f93e776SJohannes Thumshirn 		 * to read only. As soon as the blog group is read only it's
16385f93e776SJohannes Thumshirn 		 * zone_unusable value gets moved to the block group's read-only
16395f93e776SJohannes Thumshirn 		 * bytes and isn't available for calculations anymore.
16405f93e776SJohannes Thumshirn 		 */
16415f93e776SJohannes Thumshirn 		zone_unusable = bg->zone_unusable;
164218bb8bbfSJohannes Thumshirn 		ret = inc_block_group_ro(bg, 0);
164318bb8bbfSJohannes Thumshirn 		up_write(&space_info->groups_sem);
164418bb8bbfSJohannes Thumshirn 		if (ret < 0)
164518bb8bbfSJohannes Thumshirn 			goto next;
164618bb8bbfSJohannes Thumshirn 
16475f93e776SJohannes Thumshirn 		btrfs_info(fs_info,
16485f93e776SJohannes Thumshirn 			"reclaiming chunk %llu with %llu%% used %llu%% unusable",
16495f93e776SJohannes Thumshirn 				bg->start, div_u64(bg->used * 100, bg->length),
16505f93e776SJohannes Thumshirn 				div64_u64(zone_unusable * 100, bg->length));
165118bb8bbfSJohannes Thumshirn 		trace_btrfs_reclaim_block_group(bg);
165218bb8bbfSJohannes Thumshirn 		ret = btrfs_relocate_chunk(fs_info, bg->start);
165374944c87SJosef Bacik 		if (ret) {
165474944c87SJosef Bacik 			btrfs_dec_block_group_ro(bg);
165518bb8bbfSJohannes Thumshirn 			btrfs_err(fs_info, "error relocating chunk %llu",
165618bb8bbfSJohannes Thumshirn 				  bg->start);
165774944c87SJosef Bacik 		}
165818bb8bbfSJohannes Thumshirn 
165918bb8bbfSJohannes Thumshirn next:
16601cea5cf0SFilipe Manana 		btrfs_put_block_group(bg);
1661d96b3424SFilipe Manana 		spin_lock(&fs_info->unused_bgs_lock);
166218bb8bbfSJohannes Thumshirn 	}
166318bb8bbfSJohannes Thumshirn 	spin_unlock(&fs_info->unused_bgs_lock);
166418bb8bbfSJohannes Thumshirn 	mutex_unlock(&fs_info->reclaim_bgs_lock);
166518bb8bbfSJohannes Thumshirn 	btrfs_exclop_finish(fs_info);
1666ca5e4ea0SNaohiro Aota 	sb_end_write(fs_info->sb);
166718bb8bbfSJohannes Thumshirn }
166818bb8bbfSJohannes Thumshirn 
166918bb8bbfSJohannes Thumshirn void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info)
167018bb8bbfSJohannes Thumshirn {
167118bb8bbfSJohannes Thumshirn 	spin_lock(&fs_info->unused_bgs_lock);
167218bb8bbfSJohannes Thumshirn 	if (!list_empty(&fs_info->reclaim_bgs))
167318bb8bbfSJohannes Thumshirn 		queue_work(system_unbound_wq, &fs_info->reclaim_bgs_work);
167418bb8bbfSJohannes Thumshirn 	spin_unlock(&fs_info->unused_bgs_lock);
167518bb8bbfSJohannes Thumshirn }
167618bb8bbfSJohannes Thumshirn 
167718bb8bbfSJohannes Thumshirn void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg)
167818bb8bbfSJohannes Thumshirn {
167918bb8bbfSJohannes Thumshirn 	struct btrfs_fs_info *fs_info = bg->fs_info;
168018bb8bbfSJohannes Thumshirn 
168118bb8bbfSJohannes Thumshirn 	spin_lock(&fs_info->unused_bgs_lock);
168218bb8bbfSJohannes Thumshirn 	if (list_empty(&bg->bg_list)) {
168318bb8bbfSJohannes Thumshirn 		btrfs_get_block_group(bg);
168418bb8bbfSJohannes Thumshirn 		trace_btrfs_add_reclaim_block_group(bg);
168518bb8bbfSJohannes Thumshirn 		list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
168618bb8bbfSJohannes Thumshirn 	}
168718bb8bbfSJohannes Thumshirn 	spin_unlock(&fs_info->unused_bgs_lock);
168818bb8bbfSJohannes Thumshirn }
168918bb8bbfSJohannes Thumshirn 
1690e3ba67a1SJohannes Thumshirn static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
1691e3ba67a1SJohannes Thumshirn 			   struct btrfs_path *path)
1692e3ba67a1SJohannes Thumshirn {
1693e3ba67a1SJohannes Thumshirn 	struct extent_map_tree *em_tree;
1694e3ba67a1SJohannes Thumshirn 	struct extent_map *em;
1695e3ba67a1SJohannes Thumshirn 	struct btrfs_block_group_item bg;
1696e3ba67a1SJohannes Thumshirn 	struct extent_buffer *leaf;
1697e3ba67a1SJohannes Thumshirn 	int slot;
1698e3ba67a1SJohannes Thumshirn 	u64 flags;
1699e3ba67a1SJohannes Thumshirn 	int ret = 0;
1700e3ba67a1SJohannes Thumshirn 
1701e3ba67a1SJohannes Thumshirn 	slot = path->slots[0];
1702e3ba67a1SJohannes Thumshirn 	leaf = path->nodes[0];
1703e3ba67a1SJohannes Thumshirn 
1704e3ba67a1SJohannes Thumshirn 	em_tree = &fs_info->mapping_tree;
1705e3ba67a1SJohannes Thumshirn 	read_lock(&em_tree->lock);
1706e3ba67a1SJohannes Thumshirn 	em = lookup_extent_mapping(em_tree, key->objectid, key->offset);
1707e3ba67a1SJohannes Thumshirn 	read_unlock(&em_tree->lock);
1708e3ba67a1SJohannes Thumshirn 	if (!em) {
1709e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
1710e3ba67a1SJohannes Thumshirn 			  "logical %llu len %llu found bg but no related chunk",
1711e3ba67a1SJohannes Thumshirn 			  key->objectid, key->offset);
1712e3ba67a1SJohannes Thumshirn 		return -ENOENT;
1713e3ba67a1SJohannes Thumshirn 	}
1714e3ba67a1SJohannes Thumshirn 
1715e3ba67a1SJohannes Thumshirn 	if (em->start != key->objectid || em->len != key->offset) {
1716e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
1717e3ba67a1SJohannes Thumshirn 			"block group %llu len %llu mismatch with chunk %llu len %llu",
1718e3ba67a1SJohannes Thumshirn 			key->objectid, key->offset, em->start, em->len);
1719e3ba67a1SJohannes Thumshirn 		ret = -EUCLEAN;
1720e3ba67a1SJohannes Thumshirn 		goto out_free_em;
1721e3ba67a1SJohannes Thumshirn 	}
1722e3ba67a1SJohannes Thumshirn 
1723e3ba67a1SJohannes Thumshirn 	read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot),
1724e3ba67a1SJohannes Thumshirn 			   sizeof(bg));
1725e3ba67a1SJohannes Thumshirn 	flags = btrfs_stack_block_group_flags(&bg) &
1726e3ba67a1SJohannes Thumshirn 		BTRFS_BLOCK_GROUP_TYPE_MASK;
1727e3ba67a1SJohannes Thumshirn 
1728e3ba67a1SJohannes Thumshirn 	if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1729e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
1730e3ba67a1SJohannes Thumshirn "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
1731e3ba67a1SJohannes Thumshirn 			  key->objectid, key->offset, flags,
1732e3ba67a1SJohannes Thumshirn 			  (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type));
1733e3ba67a1SJohannes Thumshirn 		ret = -EUCLEAN;
1734e3ba67a1SJohannes Thumshirn 	}
1735e3ba67a1SJohannes Thumshirn 
1736e3ba67a1SJohannes Thumshirn out_free_em:
1737e3ba67a1SJohannes Thumshirn 	free_extent_map(em);
1738e3ba67a1SJohannes Thumshirn 	return ret;
1739e3ba67a1SJohannes Thumshirn }
1740e3ba67a1SJohannes Thumshirn 
17414358d963SJosef Bacik static int find_first_block_group(struct btrfs_fs_info *fs_info,
17424358d963SJosef Bacik 				  struct btrfs_path *path,
17434358d963SJosef Bacik 				  struct btrfs_key *key)
17444358d963SJosef Bacik {
1745dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
1746e3ba67a1SJohannes Thumshirn 	int ret;
17474358d963SJosef Bacik 	struct btrfs_key found_key;
17484358d963SJosef Bacik 
174936dfbbe2SGabriel Niebler 	btrfs_for_each_slot(root, key, &found_key, path, ret) {
17504358d963SJosef Bacik 		if (found_key.objectid >= key->objectid &&
17514358d963SJosef Bacik 		    found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
175236dfbbe2SGabriel Niebler 			return read_bg_from_eb(fs_info, &found_key, path);
1753e3ba67a1SJohannes Thumshirn 		}
17544358d963SJosef Bacik 	}
17554358d963SJosef Bacik 	return ret;
17564358d963SJosef Bacik }
17574358d963SJosef Bacik 
17584358d963SJosef Bacik static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
17594358d963SJosef Bacik {
17604358d963SJosef Bacik 	u64 extra_flags = chunk_to_extended(flags) &
17614358d963SJosef Bacik 				BTRFS_EXTENDED_PROFILE_MASK;
17624358d963SJosef Bacik 
17634358d963SJosef Bacik 	write_seqlock(&fs_info->profiles_lock);
17644358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA)
17654358d963SJosef Bacik 		fs_info->avail_data_alloc_bits |= extra_flags;
17664358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_METADATA)
17674358d963SJosef Bacik 		fs_info->avail_metadata_alloc_bits |= extra_flags;
17684358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
17694358d963SJosef Bacik 		fs_info->avail_system_alloc_bits |= extra_flags;
17704358d963SJosef Bacik 	write_sequnlock(&fs_info->profiles_lock);
17714358d963SJosef Bacik }
17724358d963SJosef Bacik 
177396a14336SNikolay Borisov /**
17749ee9b979SNikolay Borisov  * Map a physical disk address to a list of logical addresses
17759ee9b979SNikolay Borisov  *
17769ee9b979SNikolay Borisov  * @fs_info:       the filesystem
177796a14336SNikolay Borisov  * @chunk_start:   logical address of block group
1778138082f3SNaohiro Aota  * @bdev:	   physical device to resolve, can be NULL to indicate any device
177996a14336SNikolay Borisov  * @physical:	   physical address to map to logical addresses
178096a14336SNikolay Borisov  * @logical:	   return array of logical addresses which map to @physical
178196a14336SNikolay Borisov  * @naddrs:	   length of @logical
178296a14336SNikolay Borisov  * @stripe_len:    size of IO stripe for the given block group
178396a14336SNikolay Borisov  *
178496a14336SNikolay Borisov  * Maps a particular @physical disk address to a list of @logical addresses.
178596a14336SNikolay Borisov  * Used primarily to exclude those portions of a block group that contain super
178696a14336SNikolay Borisov  * block copies.
178796a14336SNikolay Borisov  */
178896a14336SNikolay Borisov int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
1789138082f3SNaohiro Aota 		     struct block_device *bdev, u64 physical, u64 **logical,
1790138082f3SNaohiro Aota 		     int *naddrs, int *stripe_len)
179196a14336SNikolay Borisov {
179296a14336SNikolay Borisov 	struct extent_map *em;
179396a14336SNikolay Borisov 	struct map_lookup *map;
179496a14336SNikolay Borisov 	u64 *buf;
179596a14336SNikolay Borisov 	u64 bytenr;
17961776ad17SNikolay Borisov 	u64 data_stripe_length;
17971776ad17SNikolay Borisov 	u64 io_stripe_size;
17981776ad17SNikolay Borisov 	int i, nr = 0;
17991776ad17SNikolay Borisov 	int ret = 0;
180096a14336SNikolay Borisov 
180196a14336SNikolay Borisov 	em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
180296a14336SNikolay Borisov 	if (IS_ERR(em))
180396a14336SNikolay Borisov 		return -EIO;
180496a14336SNikolay Borisov 
180596a14336SNikolay Borisov 	map = em->map_lookup;
18069e22b925SNikolay Borisov 	data_stripe_length = em->orig_block_len;
18071776ad17SNikolay Borisov 	io_stripe_size = map->stripe_len;
1808138082f3SNaohiro Aota 	chunk_start = em->start;
180996a14336SNikolay Borisov 
18109e22b925SNikolay Borisov 	/* For RAID5/6 adjust to a full IO stripe length */
18119e22b925SNikolay Borisov 	if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
18121776ad17SNikolay Borisov 		io_stripe_size = map->stripe_len * nr_data_stripes(map);
181396a14336SNikolay Borisov 
181496a14336SNikolay Borisov 	buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
18151776ad17SNikolay Borisov 	if (!buf) {
18161776ad17SNikolay Borisov 		ret = -ENOMEM;
18171776ad17SNikolay Borisov 		goto out;
18181776ad17SNikolay Borisov 	}
181996a14336SNikolay Borisov 
182096a14336SNikolay Borisov 	for (i = 0; i < map->num_stripes; i++) {
18211776ad17SNikolay Borisov 		bool already_inserted = false;
18221776ad17SNikolay Borisov 		u64 stripe_nr;
1823138082f3SNaohiro Aota 		u64 offset;
18241776ad17SNikolay Borisov 		int j;
18251776ad17SNikolay Borisov 
18261776ad17SNikolay Borisov 		if (!in_range(physical, map->stripes[i].physical,
18271776ad17SNikolay Borisov 			      data_stripe_length))
182896a14336SNikolay Borisov 			continue;
182996a14336SNikolay Borisov 
1830138082f3SNaohiro Aota 		if (bdev && map->stripes[i].dev->bdev != bdev)
1831138082f3SNaohiro Aota 			continue;
1832138082f3SNaohiro Aota 
183396a14336SNikolay Borisov 		stripe_nr = physical - map->stripes[i].physical;
1834138082f3SNaohiro Aota 		stripe_nr = div64_u64_rem(stripe_nr, map->stripe_len, &offset);
183596a14336SNikolay Borisov 
1836ac067734SDavid Sterba 		if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
1837ac067734SDavid Sterba 				 BTRFS_BLOCK_GROUP_RAID10)) {
183896a14336SNikolay Borisov 			stripe_nr = stripe_nr * map->num_stripes + i;
183996a14336SNikolay Borisov 			stripe_nr = div_u64(stripe_nr, map->sub_stripes);
184096a14336SNikolay Borisov 		}
184196a14336SNikolay Borisov 		/*
184296a14336SNikolay Borisov 		 * The remaining case would be for RAID56, multiply by
184396a14336SNikolay Borisov 		 * nr_data_stripes().  Alternatively, just use rmap_len below
184496a14336SNikolay Borisov 		 * instead of map->stripe_len
184596a14336SNikolay Borisov 		 */
184696a14336SNikolay Borisov 
1847138082f3SNaohiro Aota 		bytenr = chunk_start + stripe_nr * io_stripe_size + offset;
18481776ad17SNikolay Borisov 
18491776ad17SNikolay Borisov 		/* Ensure we don't add duplicate addresses */
185096a14336SNikolay Borisov 		for (j = 0; j < nr; j++) {
18511776ad17SNikolay Borisov 			if (buf[j] == bytenr) {
18521776ad17SNikolay Borisov 				already_inserted = true;
185396a14336SNikolay Borisov 				break;
185496a14336SNikolay Borisov 			}
185596a14336SNikolay Borisov 		}
18561776ad17SNikolay Borisov 
18571776ad17SNikolay Borisov 		if (!already_inserted)
18581776ad17SNikolay Borisov 			buf[nr++] = bytenr;
185996a14336SNikolay Borisov 	}
186096a14336SNikolay Borisov 
186196a14336SNikolay Borisov 	*logical = buf;
186296a14336SNikolay Borisov 	*naddrs = nr;
18631776ad17SNikolay Borisov 	*stripe_len = io_stripe_size;
18641776ad17SNikolay Borisov out:
186596a14336SNikolay Borisov 	free_extent_map(em);
18661776ad17SNikolay Borisov 	return ret;
186796a14336SNikolay Borisov }
186896a14336SNikolay Borisov 
186932da5386SDavid Sterba static int exclude_super_stripes(struct btrfs_block_group *cache)
18704358d963SJosef Bacik {
18714358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
187212659251SNaohiro Aota 	const bool zoned = btrfs_is_zoned(fs_info);
18734358d963SJosef Bacik 	u64 bytenr;
18744358d963SJosef Bacik 	u64 *logical;
18754358d963SJosef Bacik 	int stripe_len;
18764358d963SJosef Bacik 	int i, nr, ret;
18774358d963SJosef Bacik 
1878b3470b5dSDavid Sterba 	if (cache->start < BTRFS_SUPER_INFO_OFFSET) {
1879b3470b5dSDavid Sterba 		stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start;
18804358d963SJosef Bacik 		cache->bytes_super += stripe_len;
1881b3470b5dSDavid Sterba 		ret = btrfs_add_excluded_extent(fs_info, cache->start,
18824358d963SJosef Bacik 						stripe_len);
18834358d963SJosef Bacik 		if (ret)
18844358d963SJosef Bacik 			return ret;
18854358d963SJosef Bacik 	}
18864358d963SJosef Bacik 
18874358d963SJosef Bacik 	for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
18884358d963SJosef Bacik 		bytenr = btrfs_sb_offset(i);
1889138082f3SNaohiro Aota 		ret = btrfs_rmap_block(fs_info, cache->start, NULL,
18904358d963SJosef Bacik 				       bytenr, &logical, &nr, &stripe_len);
18914358d963SJosef Bacik 		if (ret)
18924358d963SJosef Bacik 			return ret;
18934358d963SJosef Bacik 
189412659251SNaohiro Aota 		/* Shouldn't have super stripes in sequential zones */
189512659251SNaohiro Aota 		if (zoned && nr) {
189612659251SNaohiro Aota 			btrfs_err(fs_info,
189712659251SNaohiro Aota 			"zoned: block group %llu must not contain super block",
189812659251SNaohiro Aota 				  cache->start);
189912659251SNaohiro Aota 			return -EUCLEAN;
190012659251SNaohiro Aota 		}
190112659251SNaohiro Aota 
19024358d963SJosef Bacik 		while (nr--) {
190396f9b0f2SNikolay Borisov 			u64 len = min_t(u64, stripe_len,
190496f9b0f2SNikolay Borisov 				cache->start + cache->length - logical[nr]);
19054358d963SJosef Bacik 
19064358d963SJosef Bacik 			cache->bytes_super += len;
190796f9b0f2SNikolay Borisov 			ret = btrfs_add_excluded_extent(fs_info, logical[nr],
190896f9b0f2SNikolay Borisov 							len);
19094358d963SJosef Bacik 			if (ret) {
19104358d963SJosef Bacik 				kfree(logical);
19114358d963SJosef Bacik 				return ret;
19124358d963SJosef Bacik 			}
19134358d963SJosef Bacik 		}
19144358d963SJosef Bacik 
19154358d963SJosef Bacik 		kfree(logical);
19164358d963SJosef Bacik 	}
19174358d963SJosef Bacik 	return 0;
19184358d963SJosef Bacik }
19194358d963SJosef Bacik 
192032da5386SDavid Sterba static struct btrfs_block_group *btrfs_create_block_group_cache(
19219afc6649SQu Wenruo 		struct btrfs_fs_info *fs_info, u64 start)
19224358d963SJosef Bacik {
192332da5386SDavid Sterba 	struct btrfs_block_group *cache;
19244358d963SJosef Bacik 
19254358d963SJosef Bacik 	cache = kzalloc(sizeof(*cache), GFP_NOFS);
19264358d963SJosef Bacik 	if (!cache)
19274358d963SJosef Bacik 		return NULL;
19284358d963SJosef Bacik 
19294358d963SJosef Bacik 	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
19304358d963SJosef Bacik 					GFP_NOFS);
19314358d963SJosef Bacik 	if (!cache->free_space_ctl) {
19324358d963SJosef Bacik 		kfree(cache);
19334358d963SJosef Bacik 		return NULL;
19344358d963SJosef Bacik 	}
19354358d963SJosef Bacik 
1936b3470b5dSDavid Sterba 	cache->start = start;
19374358d963SJosef Bacik 
19384358d963SJosef Bacik 	cache->fs_info = fs_info;
19394358d963SJosef Bacik 	cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
19404358d963SJosef Bacik 
19416e80d4f8SDennis Zhou 	cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED;
19426e80d4f8SDennis Zhou 
194348aaeebeSJosef Bacik 	refcount_set(&cache->refs, 1);
19444358d963SJosef Bacik 	spin_lock_init(&cache->lock);
19454358d963SJosef Bacik 	init_rwsem(&cache->data_rwsem);
19464358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->list);
19474358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->cluster_list);
19484358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->bg_list);
19494358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->ro_list);
1950b0643e59SDennis Zhou 	INIT_LIST_HEAD(&cache->discard_list);
19514358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->dirty_list);
19524358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->io_list);
1953afba2bc0SNaohiro Aota 	INIT_LIST_HEAD(&cache->active_bg_list);
1954cd79909bSJosef Bacik 	btrfs_init_free_space_ctl(cache, cache->free_space_ctl);
19556b7304afSFilipe Manana 	atomic_set(&cache->frozen, 0);
19564358d963SJosef Bacik 	mutex_init(&cache->free_space_lock);
1957c29abab4SJosef Bacik 	cache->full_stripe_locks_root.root = RB_ROOT;
1958c29abab4SJosef Bacik 	mutex_init(&cache->full_stripe_locks_root.lock);
19594358d963SJosef Bacik 
19604358d963SJosef Bacik 	return cache;
19614358d963SJosef Bacik }
19624358d963SJosef Bacik 
19634358d963SJosef Bacik /*
19644358d963SJosef Bacik  * Iterate all chunks and verify that each of them has the corresponding block
19654358d963SJosef Bacik  * group
19664358d963SJosef Bacik  */
19674358d963SJosef Bacik static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
19684358d963SJosef Bacik {
19694358d963SJosef Bacik 	struct extent_map_tree *map_tree = &fs_info->mapping_tree;
19704358d963SJosef Bacik 	struct extent_map *em;
197132da5386SDavid Sterba 	struct btrfs_block_group *bg;
19724358d963SJosef Bacik 	u64 start = 0;
19734358d963SJosef Bacik 	int ret = 0;
19744358d963SJosef Bacik 
19754358d963SJosef Bacik 	while (1) {
19764358d963SJosef Bacik 		read_lock(&map_tree->lock);
19774358d963SJosef Bacik 		/*
19784358d963SJosef Bacik 		 * lookup_extent_mapping will return the first extent map
19794358d963SJosef Bacik 		 * intersecting the range, so setting @len to 1 is enough to
19804358d963SJosef Bacik 		 * get the first chunk.
19814358d963SJosef Bacik 		 */
19824358d963SJosef Bacik 		em = lookup_extent_mapping(map_tree, start, 1);
19834358d963SJosef Bacik 		read_unlock(&map_tree->lock);
19844358d963SJosef Bacik 		if (!em)
19854358d963SJosef Bacik 			break;
19864358d963SJosef Bacik 
19874358d963SJosef Bacik 		bg = btrfs_lookup_block_group(fs_info, em->start);
19884358d963SJosef Bacik 		if (!bg) {
19894358d963SJosef Bacik 			btrfs_err(fs_info,
19904358d963SJosef Bacik 	"chunk start=%llu len=%llu doesn't have corresponding block group",
19914358d963SJosef Bacik 				     em->start, em->len);
19924358d963SJosef Bacik 			ret = -EUCLEAN;
19934358d963SJosef Bacik 			free_extent_map(em);
19944358d963SJosef Bacik 			break;
19954358d963SJosef Bacik 		}
1996b3470b5dSDavid Sterba 		if (bg->start != em->start || bg->length != em->len ||
19974358d963SJosef Bacik 		    (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
19984358d963SJosef Bacik 		    (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
19994358d963SJosef Bacik 			btrfs_err(fs_info,
20004358d963SJosef Bacik "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",
20014358d963SJosef Bacik 				em->start, em->len,
20024358d963SJosef Bacik 				em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
2003b3470b5dSDavid Sterba 				bg->start, bg->length,
20044358d963SJosef Bacik 				bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
20054358d963SJosef Bacik 			ret = -EUCLEAN;
20064358d963SJosef Bacik 			free_extent_map(em);
20074358d963SJosef Bacik 			btrfs_put_block_group(bg);
20084358d963SJosef Bacik 			break;
20094358d963SJosef Bacik 		}
20104358d963SJosef Bacik 		start = em->start + em->len;
20114358d963SJosef Bacik 		free_extent_map(em);
20124358d963SJosef Bacik 		btrfs_put_block_group(bg);
20134358d963SJosef Bacik 	}
20144358d963SJosef Bacik 	return ret;
20154358d963SJosef Bacik }
20164358d963SJosef Bacik 
2017ffb9e0f0SQu Wenruo static int read_one_block_group(struct btrfs_fs_info *info,
20184afd2fe8SJohannes Thumshirn 				struct btrfs_block_group_item *bgi,
2019d49a2ddbSQu Wenruo 				const struct btrfs_key *key,
2020ffb9e0f0SQu Wenruo 				int need_clear)
2021ffb9e0f0SQu Wenruo {
202232da5386SDavid Sterba 	struct btrfs_block_group *cache;
2023ffb9e0f0SQu Wenruo 	const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS);
2024ffb9e0f0SQu Wenruo 	int ret;
2025ffb9e0f0SQu Wenruo 
2026d49a2ddbSQu Wenruo 	ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY);
2027ffb9e0f0SQu Wenruo 
20289afc6649SQu Wenruo 	cache = btrfs_create_block_group_cache(info, key->objectid);
2029ffb9e0f0SQu Wenruo 	if (!cache)
2030ffb9e0f0SQu Wenruo 		return -ENOMEM;
2031ffb9e0f0SQu Wenruo 
20324afd2fe8SJohannes Thumshirn 	cache->length = key->offset;
20334afd2fe8SJohannes Thumshirn 	cache->used = btrfs_stack_block_group_used(bgi);
20344afd2fe8SJohannes Thumshirn 	cache->flags = btrfs_stack_block_group_flags(bgi);
2035f7238e50SJosef Bacik 	cache->global_root_id = btrfs_stack_block_group_chunk_objectid(bgi);
20369afc6649SQu Wenruo 
2037e3e39c72SMarcos Paulo de Souza 	set_free_space_tree_thresholds(cache);
2038e3e39c72SMarcos Paulo de Souza 
2039ffb9e0f0SQu Wenruo 	if (need_clear) {
2040ffb9e0f0SQu Wenruo 		/*
2041ffb9e0f0SQu Wenruo 		 * When we mount with old space cache, we need to
2042ffb9e0f0SQu Wenruo 		 * set BTRFS_DC_CLEAR and set dirty flag.
2043ffb9e0f0SQu Wenruo 		 *
2044ffb9e0f0SQu Wenruo 		 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
2045ffb9e0f0SQu Wenruo 		 *    truncate the old free space cache inode and
2046ffb9e0f0SQu Wenruo 		 *    setup a new one.
2047ffb9e0f0SQu Wenruo 		 * b) Setting 'dirty flag' makes sure that we flush
2048ffb9e0f0SQu Wenruo 		 *    the new space cache info onto disk.
2049ffb9e0f0SQu Wenruo 		 */
2050ffb9e0f0SQu Wenruo 		if (btrfs_test_opt(info, SPACE_CACHE))
2051ffb9e0f0SQu Wenruo 			cache->disk_cache_state = BTRFS_DC_CLEAR;
2052ffb9e0f0SQu Wenruo 	}
2053ffb9e0f0SQu Wenruo 	if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
2054ffb9e0f0SQu Wenruo 	    (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
2055ffb9e0f0SQu Wenruo 			btrfs_err(info,
2056ffb9e0f0SQu Wenruo "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
2057ffb9e0f0SQu Wenruo 				  cache->start);
2058ffb9e0f0SQu Wenruo 			ret = -EINVAL;
2059ffb9e0f0SQu Wenruo 			goto error;
2060ffb9e0f0SQu Wenruo 	}
2061ffb9e0f0SQu Wenruo 
2062a94794d5SNaohiro Aota 	ret = btrfs_load_block_group_zone_info(cache, false);
206308e11a3dSNaohiro Aota 	if (ret) {
206408e11a3dSNaohiro Aota 		btrfs_err(info, "zoned: failed to load zone info of bg %llu",
206508e11a3dSNaohiro Aota 			  cache->start);
206608e11a3dSNaohiro Aota 		goto error;
206708e11a3dSNaohiro Aota 	}
206808e11a3dSNaohiro Aota 
2069ffb9e0f0SQu Wenruo 	/*
2070ffb9e0f0SQu Wenruo 	 * We need to exclude the super stripes now so that the space info has
2071ffb9e0f0SQu Wenruo 	 * super bytes accounted for, otherwise we'll think we have more space
2072ffb9e0f0SQu Wenruo 	 * than we actually do.
2073ffb9e0f0SQu Wenruo 	 */
2074ffb9e0f0SQu Wenruo 	ret = exclude_super_stripes(cache);
2075ffb9e0f0SQu Wenruo 	if (ret) {
2076ffb9e0f0SQu Wenruo 		/* We may have excluded something, so call this just in case. */
2077ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
2078ffb9e0f0SQu Wenruo 		goto error;
2079ffb9e0f0SQu Wenruo 	}
2080ffb9e0f0SQu Wenruo 
2081ffb9e0f0SQu Wenruo 	/*
2082169e0da9SNaohiro Aota 	 * For zoned filesystem, space after the allocation offset is the only
2083169e0da9SNaohiro Aota 	 * free space for a block group. So, we don't need any caching work.
2084169e0da9SNaohiro Aota 	 * btrfs_calc_zone_unusable() will set the amount of free space and
2085169e0da9SNaohiro Aota 	 * zone_unusable space.
2086169e0da9SNaohiro Aota 	 *
2087169e0da9SNaohiro Aota 	 * For regular filesystem, check for two cases, either we are full, and
2088169e0da9SNaohiro Aota 	 * therefore don't need to bother with the caching work since we won't
2089169e0da9SNaohiro Aota 	 * find any space, or we are empty, and we can just add all the space
2090169e0da9SNaohiro Aota 	 * in and be done with it.  This saves us _a_lot_ of time, particularly
2091169e0da9SNaohiro Aota 	 * in the full case.
2092ffb9e0f0SQu Wenruo 	 */
2093169e0da9SNaohiro Aota 	if (btrfs_is_zoned(info)) {
2094169e0da9SNaohiro Aota 		btrfs_calc_zone_unusable(cache);
2095c46c4247SNaohiro Aota 		/* Should not have any excluded extents. Just in case, though. */
2096c46c4247SNaohiro Aota 		btrfs_free_excluded_extents(cache);
2097169e0da9SNaohiro Aota 	} else if (cache->length == cache->used) {
2098ffb9e0f0SQu Wenruo 		cache->cached = BTRFS_CACHE_FINISHED;
2099ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
2100ffb9e0f0SQu Wenruo 	} else if (cache->used == 0) {
2101ffb9e0f0SQu Wenruo 		cache->cached = BTRFS_CACHE_FINISHED;
21029afc6649SQu Wenruo 		add_new_free_space(cache, cache->start,
21039afc6649SQu Wenruo 				   cache->start + cache->length);
2104ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
2105ffb9e0f0SQu Wenruo 	}
2106ffb9e0f0SQu Wenruo 
2107ffb9e0f0SQu Wenruo 	ret = btrfs_add_block_group_cache(info, cache);
2108ffb9e0f0SQu Wenruo 	if (ret) {
2109ffb9e0f0SQu Wenruo 		btrfs_remove_free_space_cache(cache);
2110ffb9e0f0SQu Wenruo 		goto error;
2111ffb9e0f0SQu Wenruo 	}
2112ffb9e0f0SQu Wenruo 	trace_btrfs_add_block_group(info, cache, 0);
2113723de71dSJosef Bacik 	btrfs_add_bg_to_space_info(info, cache);
2114ffb9e0f0SQu Wenruo 
2115ffb9e0f0SQu Wenruo 	set_avail_alloc_bits(info, cache->flags);
2116a09f23c3SAnand Jain 	if (btrfs_chunk_writeable(info, cache->start)) {
2117a09f23c3SAnand Jain 		if (cache->used == 0) {
2118ffb9e0f0SQu Wenruo 			ASSERT(list_empty(&cache->bg_list));
21196e80d4f8SDennis Zhou 			if (btrfs_test_opt(info, DISCARD_ASYNC))
21206e80d4f8SDennis Zhou 				btrfs_discard_queue_work(&info->discard_ctl, cache);
21216e80d4f8SDennis Zhou 			else
2122ffb9e0f0SQu Wenruo 				btrfs_mark_bg_unused(cache);
2123ffb9e0f0SQu Wenruo 		}
2124a09f23c3SAnand Jain 	} else {
2125a09f23c3SAnand Jain 		inc_block_group_ro(cache, 1);
2126a09f23c3SAnand Jain 	}
2127a09f23c3SAnand Jain 
2128ffb9e0f0SQu Wenruo 	return 0;
2129ffb9e0f0SQu Wenruo error:
2130ffb9e0f0SQu Wenruo 	btrfs_put_block_group(cache);
2131ffb9e0f0SQu Wenruo 	return ret;
2132ffb9e0f0SQu Wenruo }
2133ffb9e0f0SQu Wenruo 
213442437a63SJosef Bacik static int fill_dummy_bgs(struct btrfs_fs_info *fs_info)
213542437a63SJosef Bacik {
213642437a63SJosef Bacik 	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
213742437a63SJosef Bacik 	struct rb_node *node;
213842437a63SJosef Bacik 	int ret = 0;
213942437a63SJosef Bacik 
214042437a63SJosef Bacik 	for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
214142437a63SJosef Bacik 		struct extent_map *em;
214242437a63SJosef Bacik 		struct map_lookup *map;
214342437a63SJosef Bacik 		struct btrfs_block_group *bg;
214442437a63SJosef Bacik 
214542437a63SJosef Bacik 		em = rb_entry(node, struct extent_map, rb_node);
214642437a63SJosef Bacik 		map = em->map_lookup;
214742437a63SJosef Bacik 		bg = btrfs_create_block_group_cache(fs_info, em->start);
214842437a63SJosef Bacik 		if (!bg) {
214942437a63SJosef Bacik 			ret = -ENOMEM;
215042437a63SJosef Bacik 			break;
215142437a63SJosef Bacik 		}
215242437a63SJosef Bacik 
215342437a63SJosef Bacik 		/* Fill dummy cache as FULL */
215442437a63SJosef Bacik 		bg->length = em->len;
215542437a63SJosef Bacik 		bg->flags = map->type;
215642437a63SJosef Bacik 		bg->cached = BTRFS_CACHE_FINISHED;
215742437a63SJosef Bacik 		bg->used = em->len;
215842437a63SJosef Bacik 		bg->flags = map->type;
215942437a63SJosef Bacik 		ret = btrfs_add_block_group_cache(fs_info, bg);
21602b29726cSQu Wenruo 		/*
21612b29726cSQu Wenruo 		 * We may have some valid block group cache added already, in
21622b29726cSQu Wenruo 		 * that case we skip to the next one.
21632b29726cSQu Wenruo 		 */
21642b29726cSQu Wenruo 		if (ret == -EEXIST) {
21652b29726cSQu Wenruo 			ret = 0;
21662b29726cSQu Wenruo 			btrfs_put_block_group(bg);
21672b29726cSQu Wenruo 			continue;
21682b29726cSQu Wenruo 		}
21692b29726cSQu Wenruo 
217042437a63SJosef Bacik 		if (ret) {
217142437a63SJosef Bacik 			btrfs_remove_free_space_cache(bg);
217242437a63SJosef Bacik 			btrfs_put_block_group(bg);
217342437a63SJosef Bacik 			break;
217442437a63SJosef Bacik 		}
21752b29726cSQu Wenruo 
2176723de71dSJosef Bacik 		btrfs_add_bg_to_space_info(fs_info, bg);
217742437a63SJosef Bacik 
217842437a63SJosef Bacik 		set_avail_alloc_bits(fs_info, bg->flags);
217942437a63SJosef Bacik 	}
218042437a63SJosef Bacik 	if (!ret)
218142437a63SJosef Bacik 		btrfs_init_global_block_rsv(fs_info);
218242437a63SJosef Bacik 	return ret;
218342437a63SJosef Bacik }
218442437a63SJosef Bacik 
21854358d963SJosef Bacik int btrfs_read_block_groups(struct btrfs_fs_info *info)
21864358d963SJosef Bacik {
2187dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(info);
21884358d963SJosef Bacik 	struct btrfs_path *path;
21894358d963SJosef Bacik 	int ret;
219032da5386SDavid Sterba 	struct btrfs_block_group *cache;
21914358d963SJosef Bacik 	struct btrfs_space_info *space_info;
21924358d963SJosef Bacik 	struct btrfs_key key;
21934358d963SJosef Bacik 	int need_clear = 0;
21944358d963SJosef Bacik 	u64 cache_gen;
21954358d963SJosef Bacik 
219681d5d614SQu Wenruo 	/*
219781d5d614SQu Wenruo 	 * Either no extent root (with ibadroots rescue option) or we have
219881d5d614SQu Wenruo 	 * unsupported RO options. The fs can never be mounted read-write, so no
219981d5d614SQu Wenruo 	 * need to waste time searching block group items.
220081d5d614SQu Wenruo 	 *
220181d5d614SQu Wenruo 	 * This also allows new extent tree related changes to be RO compat,
220281d5d614SQu Wenruo 	 * no need for a full incompat flag.
220381d5d614SQu Wenruo 	 */
220481d5d614SQu Wenruo 	if (!root || (btrfs_super_compat_ro_flags(info->super_copy) &
220581d5d614SQu Wenruo 		      ~BTRFS_FEATURE_COMPAT_RO_SUPP))
220642437a63SJosef Bacik 		return fill_dummy_bgs(info);
220742437a63SJosef Bacik 
22084358d963SJosef Bacik 	key.objectid = 0;
22094358d963SJosef Bacik 	key.offset = 0;
22104358d963SJosef Bacik 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
22114358d963SJosef Bacik 	path = btrfs_alloc_path();
22124358d963SJosef Bacik 	if (!path)
22134358d963SJosef Bacik 		return -ENOMEM;
22144358d963SJosef Bacik 
22154358d963SJosef Bacik 	cache_gen = btrfs_super_cache_generation(info->super_copy);
22164358d963SJosef Bacik 	if (btrfs_test_opt(info, SPACE_CACHE) &&
22174358d963SJosef Bacik 	    btrfs_super_generation(info->super_copy) != cache_gen)
22184358d963SJosef Bacik 		need_clear = 1;
22194358d963SJosef Bacik 	if (btrfs_test_opt(info, CLEAR_CACHE))
22204358d963SJosef Bacik 		need_clear = 1;
22214358d963SJosef Bacik 
22224358d963SJosef Bacik 	while (1) {
22234afd2fe8SJohannes Thumshirn 		struct btrfs_block_group_item bgi;
22244afd2fe8SJohannes Thumshirn 		struct extent_buffer *leaf;
22254afd2fe8SJohannes Thumshirn 		int slot;
22264afd2fe8SJohannes Thumshirn 
22274358d963SJosef Bacik 		ret = find_first_block_group(info, path, &key);
22284358d963SJosef Bacik 		if (ret > 0)
22294358d963SJosef Bacik 			break;
22304358d963SJosef Bacik 		if (ret != 0)
22314358d963SJosef Bacik 			goto error;
22324358d963SJosef Bacik 
22334afd2fe8SJohannes Thumshirn 		leaf = path->nodes[0];
22344afd2fe8SJohannes Thumshirn 		slot = path->slots[0];
22354afd2fe8SJohannes Thumshirn 
22364afd2fe8SJohannes Thumshirn 		read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
22374afd2fe8SJohannes Thumshirn 				   sizeof(bgi));
22384afd2fe8SJohannes Thumshirn 
22394afd2fe8SJohannes Thumshirn 		btrfs_item_key_to_cpu(leaf, &key, slot);
22404afd2fe8SJohannes Thumshirn 		btrfs_release_path(path);
22414afd2fe8SJohannes Thumshirn 		ret = read_one_block_group(info, &bgi, &key, need_clear);
2242ffb9e0f0SQu Wenruo 		if (ret < 0)
22434358d963SJosef Bacik 			goto error;
2244ffb9e0f0SQu Wenruo 		key.objectid += key.offset;
2245ffb9e0f0SQu Wenruo 		key.offset = 0;
22464358d963SJosef Bacik 	}
22477837fa88SJosef Bacik 	btrfs_release_path(path);
22484358d963SJosef Bacik 
224972804905SJosef Bacik 	list_for_each_entry(space_info, &info->space_info, list) {
225049ea112dSJosef Bacik 		int i;
225149ea112dSJosef Bacik 
225249ea112dSJosef Bacik 		for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
225349ea112dSJosef Bacik 			if (list_empty(&space_info->block_groups[i]))
225449ea112dSJosef Bacik 				continue;
225549ea112dSJosef Bacik 			cache = list_first_entry(&space_info->block_groups[i],
225649ea112dSJosef Bacik 						 struct btrfs_block_group,
225749ea112dSJosef Bacik 						 list);
225849ea112dSJosef Bacik 			btrfs_sysfs_add_block_group_type(cache);
225949ea112dSJosef Bacik 		}
226049ea112dSJosef Bacik 
22614358d963SJosef Bacik 		if (!(btrfs_get_alloc_profile(info, space_info->flags) &
22624358d963SJosef Bacik 		      (BTRFS_BLOCK_GROUP_RAID10 |
22634358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_RAID1_MASK |
22644358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_RAID56_MASK |
22654358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_DUP)))
22664358d963SJosef Bacik 			continue;
22674358d963SJosef Bacik 		/*
22684358d963SJosef Bacik 		 * Avoid allocating from un-mirrored block group if there are
22694358d963SJosef Bacik 		 * mirrored block groups.
22704358d963SJosef Bacik 		 */
22714358d963SJosef Bacik 		list_for_each_entry(cache,
22724358d963SJosef Bacik 				&space_info->block_groups[BTRFS_RAID_RAID0],
22734358d963SJosef Bacik 				list)
2274e11c0406SJosef Bacik 			inc_block_group_ro(cache, 1);
22754358d963SJosef Bacik 		list_for_each_entry(cache,
22764358d963SJosef Bacik 				&space_info->block_groups[BTRFS_RAID_SINGLE],
22774358d963SJosef Bacik 				list)
2278e11c0406SJosef Bacik 			inc_block_group_ro(cache, 1);
22794358d963SJosef Bacik 	}
22804358d963SJosef Bacik 
22814358d963SJosef Bacik 	btrfs_init_global_block_rsv(info);
22824358d963SJosef Bacik 	ret = check_chunk_block_group_mappings(info);
22834358d963SJosef Bacik error:
22844358d963SJosef Bacik 	btrfs_free_path(path);
22852b29726cSQu Wenruo 	/*
22862b29726cSQu Wenruo 	 * We've hit some error while reading the extent tree, and have
22872b29726cSQu Wenruo 	 * rescue=ibadroots mount option.
22882b29726cSQu Wenruo 	 * Try to fill the tree using dummy block groups so that the user can
22892b29726cSQu Wenruo 	 * continue to mount and grab their data.
22902b29726cSQu Wenruo 	 */
22912b29726cSQu Wenruo 	if (ret && btrfs_test_opt(info, IGNOREBADROOTS))
22922b29726cSQu Wenruo 		ret = fill_dummy_bgs(info);
22934358d963SJosef Bacik 	return ret;
22944358d963SJosef Bacik }
22954358d963SJosef Bacik 
229679bd3712SFilipe Manana /*
229779bd3712SFilipe Manana  * This function, insert_block_group_item(), belongs to the phase 2 of chunk
229879bd3712SFilipe Manana  * allocation.
229979bd3712SFilipe Manana  *
230079bd3712SFilipe Manana  * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
230179bd3712SFilipe Manana  * phases.
230279bd3712SFilipe Manana  */
230397f4728aSQu Wenruo static int insert_block_group_item(struct btrfs_trans_handle *trans,
230497f4728aSQu Wenruo 				   struct btrfs_block_group *block_group)
230597f4728aSQu Wenruo {
230697f4728aSQu Wenruo 	struct btrfs_fs_info *fs_info = trans->fs_info;
230797f4728aSQu Wenruo 	struct btrfs_block_group_item bgi;
2308dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
230997f4728aSQu Wenruo 	struct btrfs_key key;
231097f4728aSQu Wenruo 
231197f4728aSQu Wenruo 	spin_lock(&block_group->lock);
231297f4728aSQu Wenruo 	btrfs_set_stack_block_group_used(&bgi, block_group->used);
231397f4728aSQu Wenruo 	btrfs_set_stack_block_group_chunk_objectid(&bgi,
2314f7238e50SJosef Bacik 						   block_group->global_root_id);
231597f4728aSQu Wenruo 	btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
231697f4728aSQu Wenruo 	key.objectid = block_group->start;
231797f4728aSQu Wenruo 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
231897f4728aSQu Wenruo 	key.offset = block_group->length;
231997f4728aSQu Wenruo 	spin_unlock(&block_group->lock);
232097f4728aSQu Wenruo 
232197f4728aSQu Wenruo 	return btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi));
232297f4728aSQu Wenruo }
232397f4728aSQu Wenruo 
23242eadb9e7SNikolay Borisov static int insert_dev_extent(struct btrfs_trans_handle *trans,
23252eadb9e7SNikolay Borisov 			    struct btrfs_device *device, u64 chunk_offset,
23262eadb9e7SNikolay Borisov 			    u64 start, u64 num_bytes)
23272eadb9e7SNikolay Borisov {
23282eadb9e7SNikolay Borisov 	struct btrfs_fs_info *fs_info = device->fs_info;
23292eadb9e7SNikolay Borisov 	struct btrfs_root *root = fs_info->dev_root;
23302eadb9e7SNikolay Borisov 	struct btrfs_path *path;
23312eadb9e7SNikolay Borisov 	struct btrfs_dev_extent *extent;
23322eadb9e7SNikolay Borisov 	struct extent_buffer *leaf;
23332eadb9e7SNikolay Borisov 	struct btrfs_key key;
23342eadb9e7SNikolay Borisov 	int ret;
23352eadb9e7SNikolay Borisov 
23362eadb9e7SNikolay Borisov 	WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
23372eadb9e7SNikolay Borisov 	WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
23382eadb9e7SNikolay Borisov 	path = btrfs_alloc_path();
23392eadb9e7SNikolay Borisov 	if (!path)
23402eadb9e7SNikolay Borisov 		return -ENOMEM;
23412eadb9e7SNikolay Borisov 
23422eadb9e7SNikolay Borisov 	key.objectid = device->devid;
23432eadb9e7SNikolay Borisov 	key.type = BTRFS_DEV_EXTENT_KEY;
23442eadb9e7SNikolay Borisov 	key.offset = start;
23452eadb9e7SNikolay Borisov 	ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*extent));
23462eadb9e7SNikolay Borisov 	if (ret)
23472eadb9e7SNikolay Borisov 		goto out;
23482eadb9e7SNikolay Borisov 
23492eadb9e7SNikolay Borisov 	leaf = path->nodes[0];
23502eadb9e7SNikolay Borisov 	extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
23512eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_chunk_tree(leaf, extent, BTRFS_CHUNK_TREE_OBJECTID);
23522eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_chunk_objectid(leaf, extent,
23532eadb9e7SNikolay Borisov 					    BTRFS_FIRST_CHUNK_TREE_OBJECTID);
23542eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
23552eadb9e7SNikolay Borisov 
23562eadb9e7SNikolay Borisov 	btrfs_set_dev_extent_length(leaf, extent, num_bytes);
23572eadb9e7SNikolay Borisov 	btrfs_mark_buffer_dirty(leaf);
23582eadb9e7SNikolay Borisov out:
23592eadb9e7SNikolay Borisov 	btrfs_free_path(path);
23602eadb9e7SNikolay Borisov 	return ret;
23612eadb9e7SNikolay Borisov }
23622eadb9e7SNikolay Borisov 
23632eadb9e7SNikolay Borisov /*
23642eadb9e7SNikolay Borisov  * This function belongs to phase 2.
23652eadb9e7SNikolay Borisov  *
23662eadb9e7SNikolay Borisov  * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
23672eadb9e7SNikolay Borisov  * phases.
23682eadb9e7SNikolay Borisov  */
23692eadb9e7SNikolay Borisov static int insert_dev_extents(struct btrfs_trans_handle *trans,
23702eadb9e7SNikolay Borisov 				   u64 chunk_offset, u64 chunk_size)
23712eadb9e7SNikolay Borisov {
23722eadb9e7SNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
23732eadb9e7SNikolay Borisov 	struct btrfs_device *device;
23742eadb9e7SNikolay Borisov 	struct extent_map *em;
23752eadb9e7SNikolay Borisov 	struct map_lookup *map;
23762eadb9e7SNikolay Borisov 	u64 dev_offset;
23772eadb9e7SNikolay Borisov 	u64 stripe_size;
23782eadb9e7SNikolay Borisov 	int i;
23792eadb9e7SNikolay Borisov 	int ret = 0;
23802eadb9e7SNikolay Borisov 
23812eadb9e7SNikolay Borisov 	em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
23822eadb9e7SNikolay Borisov 	if (IS_ERR(em))
23832eadb9e7SNikolay Borisov 		return PTR_ERR(em);
23842eadb9e7SNikolay Borisov 
23852eadb9e7SNikolay Borisov 	map = em->map_lookup;
23862eadb9e7SNikolay Borisov 	stripe_size = em->orig_block_len;
23872eadb9e7SNikolay Borisov 
23882eadb9e7SNikolay Borisov 	/*
23892eadb9e7SNikolay Borisov 	 * Take the device list mutex to prevent races with the final phase of
23902eadb9e7SNikolay Borisov 	 * a device replace operation that replaces the device object associated
23912eadb9e7SNikolay Borisov 	 * with the map's stripes, because the device object's id can change
23922eadb9e7SNikolay Borisov 	 * at any time during that final phase of the device replace operation
23932eadb9e7SNikolay Borisov 	 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the
23942eadb9e7SNikolay Borisov 	 * replaced device and then see it with an ID of BTRFS_DEV_REPLACE_DEVID,
23952eadb9e7SNikolay Borisov 	 * resulting in persisting a device extent item with such ID.
23962eadb9e7SNikolay Borisov 	 */
23972eadb9e7SNikolay Borisov 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
23982eadb9e7SNikolay Borisov 	for (i = 0; i < map->num_stripes; i++) {
23992eadb9e7SNikolay Borisov 		device = map->stripes[i].dev;
24002eadb9e7SNikolay Borisov 		dev_offset = map->stripes[i].physical;
24012eadb9e7SNikolay Borisov 
24022eadb9e7SNikolay Borisov 		ret = insert_dev_extent(trans, device, chunk_offset, dev_offset,
24032eadb9e7SNikolay Borisov 				       stripe_size);
24042eadb9e7SNikolay Borisov 		if (ret)
24052eadb9e7SNikolay Borisov 			break;
24062eadb9e7SNikolay Borisov 	}
24072eadb9e7SNikolay Borisov 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
24082eadb9e7SNikolay Borisov 
24092eadb9e7SNikolay Borisov 	free_extent_map(em);
24102eadb9e7SNikolay Borisov 	return ret;
24112eadb9e7SNikolay Borisov }
24122eadb9e7SNikolay Borisov 
241379bd3712SFilipe Manana /*
241479bd3712SFilipe Manana  * This function, btrfs_create_pending_block_groups(), belongs to the phase 2 of
241579bd3712SFilipe Manana  * chunk allocation.
241679bd3712SFilipe Manana  *
241779bd3712SFilipe Manana  * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
241879bd3712SFilipe Manana  * phases.
241979bd3712SFilipe Manana  */
24204358d963SJosef Bacik void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
24214358d963SJosef Bacik {
24224358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
242332da5386SDavid Sterba 	struct btrfs_block_group *block_group;
24244358d963SJosef Bacik 	int ret = 0;
24254358d963SJosef Bacik 
24264358d963SJosef Bacik 	while (!list_empty(&trans->new_bgs)) {
242749ea112dSJosef Bacik 		int index;
242849ea112dSJosef Bacik 
24294358d963SJosef Bacik 		block_group = list_first_entry(&trans->new_bgs,
243032da5386SDavid Sterba 					       struct btrfs_block_group,
24314358d963SJosef Bacik 					       bg_list);
24324358d963SJosef Bacik 		if (ret)
24334358d963SJosef Bacik 			goto next;
24344358d963SJosef Bacik 
243549ea112dSJosef Bacik 		index = btrfs_bg_flags_to_raid_index(block_group->flags);
243649ea112dSJosef Bacik 
243797f4728aSQu Wenruo 		ret = insert_block_group_item(trans, block_group);
24384358d963SJosef Bacik 		if (ret)
24394358d963SJosef Bacik 			btrfs_abort_transaction(trans, ret);
24403349b57fSJosef Bacik 		if (!test_bit(BLOCK_GROUP_FLAG_CHUNK_ITEM_INSERTED,
24413349b57fSJosef Bacik 			      &block_group->runtime_flags)) {
244279bd3712SFilipe Manana 			mutex_lock(&fs_info->chunk_mutex);
244379bd3712SFilipe Manana 			ret = btrfs_chunk_alloc_add_chunk_item(trans, block_group);
244479bd3712SFilipe Manana 			mutex_unlock(&fs_info->chunk_mutex);
244579bd3712SFilipe Manana 			if (ret)
244679bd3712SFilipe Manana 				btrfs_abort_transaction(trans, ret);
244779bd3712SFilipe Manana 		}
24482eadb9e7SNikolay Borisov 		ret = insert_dev_extents(trans, block_group->start,
244997f4728aSQu Wenruo 					 block_group->length);
24504358d963SJosef Bacik 		if (ret)
24514358d963SJosef Bacik 			btrfs_abort_transaction(trans, ret);
24524358d963SJosef Bacik 		add_block_group_free_space(trans, block_group);
245349ea112dSJosef Bacik 
245449ea112dSJosef Bacik 		/*
245549ea112dSJosef Bacik 		 * If we restriped during balance, we may have added a new raid
245649ea112dSJosef Bacik 		 * type, so now add the sysfs entries when it is safe to do so.
245749ea112dSJosef Bacik 		 * We don't have to worry about locking here as it's handled in
245849ea112dSJosef Bacik 		 * btrfs_sysfs_add_block_group_type.
245949ea112dSJosef Bacik 		 */
246049ea112dSJosef Bacik 		if (block_group->space_info->block_group_kobjs[index] == NULL)
246149ea112dSJosef Bacik 			btrfs_sysfs_add_block_group_type(block_group);
246249ea112dSJosef Bacik 
24634358d963SJosef Bacik 		/* Already aborted the transaction if it failed. */
24644358d963SJosef Bacik next:
24654358d963SJosef Bacik 		btrfs_delayed_refs_rsv_release(fs_info, 1);
24664358d963SJosef Bacik 		list_del_init(&block_group->bg_list);
24674358d963SJosef Bacik 	}
24684358d963SJosef Bacik 	btrfs_trans_release_chunk_metadata(trans);
24694358d963SJosef Bacik }
24704358d963SJosef Bacik 
2471f7238e50SJosef Bacik /*
2472f7238e50SJosef Bacik  * For extent tree v2 we use the block_group_item->chunk_offset to point at our
2473f7238e50SJosef Bacik  * global root id.  For v1 it's always set to BTRFS_FIRST_CHUNK_TREE_OBJECTID.
2474f7238e50SJosef Bacik  */
2475f7238e50SJosef Bacik static u64 calculate_global_root_id(struct btrfs_fs_info *fs_info, u64 offset)
2476f7238e50SJosef Bacik {
2477f7238e50SJosef Bacik 	u64 div = SZ_1G;
2478f7238e50SJosef Bacik 	u64 index;
2479f7238e50SJosef Bacik 
2480f7238e50SJosef Bacik 	if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
2481f7238e50SJosef Bacik 		return BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2482f7238e50SJosef Bacik 
2483f7238e50SJosef Bacik 	/* If we have a smaller fs index based on 128MiB. */
2484f7238e50SJosef Bacik 	if (btrfs_super_total_bytes(fs_info->super_copy) <= (SZ_1G * 10ULL))
2485f7238e50SJosef Bacik 		div = SZ_128M;
2486f7238e50SJosef Bacik 
2487f7238e50SJosef Bacik 	offset = div64_u64(offset, div);
2488f7238e50SJosef Bacik 	div64_u64_rem(offset, fs_info->nr_global_roots, &index);
2489f7238e50SJosef Bacik 	return index;
2490f7238e50SJosef Bacik }
2491f7238e50SJosef Bacik 
249279bd3712SFilipe Manana struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *trans,
249379bd3712SFilipe Manana 						 u64 bytes_used, u64 type,
249479bd3712SFilipe Manana 						 u64 chunk_offset, u64 size)
24954358d963SJosef Bacik {
24964358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
249732da5386SDavid Sterba 	struct btrfs_block_group *cache;
24984358d963SJosef Bacik 	int ret;
24994358d963SJosef Bacik 
25004358d963SJosef Bacik 	btrfs_set_log_full_commit(trans);
25014358d963SJosef Bacik 
25029afc6649SQu Wenruo 	cache = btrfs_create_block_group_cache(fs_info, chunk_offset);
25034358d963SJosef Bacik 	if (!cache)
250479bd3712SFilipe Manana 		return ERR_PTR(-ENOMEM);
25054358d963SJosef Bacik 
25069afc6649SQu Wenruo 	cache->length = size;
2507e3e39c72SMarcos Paulo de Souza 	set_free_space_tree_thresholds(cache);
2508bf38be65SDavid Sterba 	cache->used = bytes_used;
25094358d963SJosef Bacik 	cache->flags = type;
25104358d963SJosef Bacik 	cache->cached = BTRFS_CACHE_FINISHED;
2511f7238e50SJosef Bacik 	cache->global_root_id = calculate_global_root_id(fs_info, cache->start);
2512f7238e50SJosef Bacik 
2513997e3e2eSBoris Burkov 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
25144358d963SJosef Bacik 		cache->needs_free_space = 1;
251508e11a3dSNaohiro Aota 
2516a94794d5SNaohiro Aota 	ret = btrfs_load_block_group_zone_info(cache, true);
251708e11a3dSNaohiro Aota 	if (ret) {
251808e11a3dSNaohiro Aota 		btrfs_put_block_group(cache);
251979bd3712SFilipe Manana 		return ERR_PTR(ret);
252008e11a3dSNaohiro Aota 	}
252108e11a3dSNaohiro Aota 
25224358d963SJosef Bacik 	ret = exclude_super_stripes(cache);
25234358d963SJosef Bacik 	if (ret) {
25244358d963SJosef Bacik 		/* We may have excluded something, so call this just in case */
25254358d963SJosef Bacik 		btrfs_free_excluded_extents(cache);
25264358d963SJosef Bacik 		btrfs_put_block_group(cache);
252779bd3712SFilipe Manana 		return ERR_PTR(ret);
25284358d963SJosef Bacik 	}
25294358d963SJosef Bacik 
25304358d963SJosef Bacik 	add_new_free_space(cache, chunk_offset, chunk_offset + size);
25314358d963SJosef Bacik 
25324358d963SJosef Bacik 	btrfs_free_excluded_extents(cache);
25334358d963SJosef Bacik 
25344358d963SJosef Bacik 	/*
25354358d963SJosef Bacik 	 * Ensure the corresponding space_info object is created and
25364358d963SJosef Bacik 	 * assigned to our block group. We want our bg to be added to the rbtree
25374358d963SJosef Bacik 	 * with its ->space_info set.
25384358d963SJosef Bacik 	 */
25394358d963SJosef Bacik 	cache->space_info = btrfs_find_space_info(fs_info, cache->flags);
25404358d963SJosef Bacik 	ASSERT(cache->space_info);
25414358d963SJosef Bacik 
25424358d963SJosef Bacik 	ret = btrfs_add_block_group_cache(fs_info, cache);
25434358d963SJosef Bacik 	if (ret) {
25444358d963SJosef Bacik 		btrfs_remove_free_space_cache(cache);
25454358d963SJosef Bacik 		btrfs_put_block_group(cache);
254679bd3712SFilipe Manana 		return ERR_PTR(ret);
25474358d963SJosef Bacik 	}
25484358d963SJosef Bacik 
25494358d963SJosef Bacik 	/*
25504358d963SJosef Bacik 	 * Now that our block group has its ->space_info set and is inserted in
25514358d963SJosef Bacik 	 * the rbtree, update the space info's counters.
25524358d963SJosef Bacik 	 */
25534358d963SJosef Bacik 	trace_btrfs_add_block_group(fs_info, cache, 1);
2554723de71dSJosef Bacik 	btrfs_add_bg_to_space_info(fs_info, cache);
25554358d963SJosef Bacik 	btrfs_update_global_block_rsv(fs_info);
25564358d963SJosef Bacik 
25579d4b0a12SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
25589d4b0a12SJosef Bacik 	if (btrfs_should_fragment_free_space(cache)) {
25599d4b0a12SJosef Bacik 		u64 new_bytes_used = size - bytes_used;
25609d4b0a12SJosef Bacik 
25619d4b0a12SJosef Bacik 		cache->space_info->bytes_used += new_bytes_used >> 1;
25629d4b0a12SJosef Bacik 		fragment_free_space(cache);
25639d4b0a12SJosef Bacik 	}
25649d4b0a12SJosef Bacik #endif
25654358d963SJosef Bacik 
25664358d963SJosef Bacik 	list_add_tail(&cache->bg_list, &trans->new_bgs);
25674358d963SJosef Bacik 	trans->delayed_ref_updates++;
25684358d963SJosef Bacik 	btrfs_update_delayed_refs_rsv(trans);
25694358d963SJosef Bacik 
25704358d963SJosef Bacik 	set_avail_alloc_bits(fs_info, type);
257179bd3712SFilipe Manana 	return cache;
25724358d963SJosef Bacik }
257326ce2095SJosef Bacik 
2574b12de528SQu Wenruo /*
2575b12de528SQu Wenruo  * Mark one block group RO, can be called several times for the same block
2576b12de528SQu Wenruo  * group.
2577b12de528SQu Wenruo  *
2578b12de528SQu Wenruo  * @cache:		the destination block group
2579b12de528SQu Wenruo  * @do_chunk_alloc:	whether need to do chunk pre-allocation, this is to
2580b12de528SQu Wenruo  * 			ensure we still have some free space after marking this
2581b12de528SQu Wenruo  * 			block group RO.
2582b12de528SQu Wenruo  */
2583b12de528SQu Wenruo int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
2584b12de528SQu Wenruo 			     bool do_chunk_alloc)
258526ce2095SJosef Bacik {
258626ce2095SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
258726ce2095SJosef Bacik 	struct btrfs_trans_handle *trans;
2588dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
258926ce2095SJosef Bacik 	u64 alloc_flags;
259026ce2095SJosef Bacik 	int ret;
2591b6e9f16cSNikolay Borisov 	bool dirty_bg_running;
259226ce2095SJosef Bacik 
25932d192fc4SQu Wenruo 	/*
25942d192fc4SQu Wenruo 	 * This can only happen when we are doing read-only scrub on read-only
25952d192fc4SQu Wenruo 	 * mount.
25962d192fc4SQu Wenruo 	 * In that case we should not start a new transaction on read-only fs.
25972d192fc4SQu Wenruo 	 * Thus here we skip all chunk allocations.
25982d192fc4SQu Wenruo 	 */
25992d192fc4SQu Wenruo 	if (sb_rdonly(fs_info->sb)) {
26002d192fc4SQu Wenruo 		mutex_lock(&fs_info->ro_block_group_mutex);
26012d192fc4SQu Wenruo 		ret = inc_block_group_ro(cache, 0);
26022d192fc4SQu Wenruo 		mutex_unlock(&fs_info->ro_block_group_mutex);
26032d192fc4SQu Wenruo 		return ret;
26042d192fc4SQu Wenruo 	}
26052d192fc4SQu Wenruo 
2606b6e9f16cSNikolay Borisov 	do {
2607dfe8aec4SJosef Bacik 		trans = btrfs_join_transaction(root);
260826ce2095SJosef Bacik 		if (IS_ERR(trans))
260926ce2095SJosef Bacik 			return PTR_ERR(trans);
261026ce2095SJosef Bacik 
2611b6e9f16cSNikolay Borisov 		dirty_bg_running = false;
2612b6e9f16cSNikolay Borisov 
261326ce2095SJosef Bacik 		/*
2614b6e9f16cSNikolay Borisov 		 * We're not allowed to set block groups readonly after the dirty
2615b6e9f16cSNikolay Borisov 		 * block group cache has started writing.  If it already started,
2616b6e9f16cSNikolay Borisov 		 * back off and let this transaction commit.
261726ce2095SJosef Bacik 		 */
261826ce2095SJosef Bacik 		mutex_lock(&fs_info->ro_block_group_mutex);
261926ce2095SJosef Bacik 		if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
262026ce2095SJosef Bacik 			u64 transid = trans->transid;
262126ce2095SJosef Bacik 
262226ce2095SJosef Bacik 			mutex_unlock(&fs_info->ro_block_group_mutex);
262326ce2095SJosef Bacik 			btrfs_end_transaction(trans);
262426ce2095SJosef Bacik 
262526ce2095SJosef Bacik 			ret = btrfs_wait_for_commit(fs_info, transid);
262626ce2095SJosef Bacik 			if (ret)
262726ce2095SJosef Bacik 				return ret;
2628b6e9f16cSNikolay Borisov 			dirty_bg_running = true;
262926ce2095SJosef Bacik 		}
2630b6e9f16cSNikolay Borisov 	} while (dirty_bg_running);
263126ce2095SJosef Bacik 
2632b12de528SQu Wenruo 	if (do_chunk_alloc) {
263326ce2095SJosef Bacik 		/*
2634b12de528SQu Wenruo 		 * If we are changing raid levels, try to allocate a
2635b12de528SQu Wenruo 		 * corresponding block group with the new raid level.
263626ce2095SJosef Bacik 		 */
2637349e120eSJosef Bacik 		alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
263826ce2095SJosef Bacik 		if (alloc_flags != cache->flags) {
2639b12de528SQu Wenruo 			ret = btrfs_chunk_alloc(trans, alloc_flags,
2640b12de528SQu Wenruo 						CHUNK_ALLOC_FORCE);
264126ce2095SJosef Bacik 			/*
264226ce2095SJosef Bacik 			 * ENOSPC is allowed here, we may have enough space
2643b12de528SQu Wenruo 			 * already allocated at the new raid level to carry on
264426ce2095SJosef Bacik 			 */
264526ce2095SJosef Bacik 			if (ret == -ENOSPC)
264626ce2095SJosef Bacik 				ret = 0;
264726ce2095SJosef Bacik 			if (ret < 0)
264826ce2095SJosef Bacik 				goto out;
264926ce2095SJosef Bacik 		}
2650b12de528SQu Wenruo 	}
265126ce2095SJosef Bacik 
2652a7a63accSJosef Bacik 	ret = inc_block_group_ro(cache, 0);
2653195a49eaSFilipe Manana 	if (!do_chunk_alloc || ret == -ETXTBSY)
2654b12de528SQu Wenruo 		goto unlock_out;
265526ce2095SJosef Bacik 	if (!ret)
265626ce2095SJosef Bacik 		goto out;
265726ce2095SJosef Bacik 	alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags);
265826ce2095SJosef Bacik 	ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
265926ce2095SJosef Bacik 	if (ret < 0)
266026ce2095SJosef Bacik 		goto out;
2661b6a98021SNaohiro Aota 	/*
2662b6a98021SNaohiro Aota 	 * We have allocated a new chunk. We also need to activate that chunk to
2663b6a98021SNaohiro Aota 	 * grant metadata tickets for zoned filesystem.
2664b6a98021SNaohiro Aota 	 */
2665b6a98021SNaohiro Aota 	ret = btrfs_zoned_activate_one_bg(fs_info, cache->space_info, true);
2666b6a98021SNaohiro Aota 	if (ret < 0)
2667b6a98021SNaohiro Aota 		goto out;
2668b6a98021SNaohiro Aota 
2669e11c0406SJosef Bacik 	ret = inc_block_group_ro(cache, 0);
2670195a49eaSFilipe Manana 	if (ret == -ETXTBSY)
2671195a49eaSFilipe Manana 		goto unlock_out;
267226ce2095SJosef Bacik out:
267326ce2095SJosef Bacik 	if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2674349e120eSJosef Bacik 		alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
267526ce2095SJosef Bacik 		mutex_lock(&fs_info->chunk_mutex);
267626ce2095SJosef Bacik 		check_system_chunk(trans, alloc_flags);
267726ce2095SJosef Bacik 		mutex_unlock(&fs_info->chunk_mutex);
267826ce2095SJosef Bacik 	}
2679b12de528SQu Wenruo unlock_out:
268026ce2095SJosef Bacik 	mutex_unlock(&fs_info->ro_block_group_mutex);
268126ce2095SJosef Bacik 
268226ce2095SJosef Bacik 	btrfs_end_transaction(trans);
268326ce2095SJosef Bacik 	return ret;
268426ce2095SJosef Bacik }
268526ce2095SJosef Bacik 
268632da5386SDavid Sterba void btrfs_dec_block_group_ro(struct btrfs_block_group *cache)
268726ce2095SJosef Bacik {
268826ce2095SJosef Bacik 	struct btrfs_space_info *sinfo = cache->space_info;
268926ce2095SJosef Bacik 	u64 num_bytes;
269026ce2095SJosef Bacik 
269126ce2095SJosef Bacik 	BUG_ON(!cache->ro);
269226ce2095SJosef Bacik 
269326ce2095SJosef Bacik 	spin_lock(&sinfo->lock);
269426ce2095SJosef Bacik 	spin_lock(&cache->lock);
269526ce2095SJosef Bacik 	if (!--cache->ro) {
2696169e0da9SNaohiro Aota 		if (btrfs_is_zoned(cache->fs_info)) {
2697169e0da9SNaohiro Aota 			/* Migrate zone_unusable bytes back */
269898173255SNaohiro Aota 			cache->zone_unusable =
269998173255SNaohiro Aota 				(cache->alloc_offset - cache->used) +
270098173255SNaohiro Aota 				(cache->length - cache->zone_capacity);
2701169e0da9SNaohiro Aota 			sinfo->bytes_zone_unusable += cache->zone_unusable;
2702169e0da9SNaohiro Aota 			sinfo->bytes_readonly -= cache->zone_unusable;
2703169e0da9SNaohiro Aota 		}
2704f9f28e5bSNaohiro Aota 		num_bytes = cache->length - cache->reserved -
2705f9f28e5bSNaohiro Aota 			    cache->pinned - cache->bytes_super -
2706f9f28e5bSNaohiro Aota 			    cache->zone_unusable - cache->used;
2707f9f28e5bSNaohiro Aota 		sinfo->bytes_readonly -= num_bytes;
270826ce2095SJosef Bacik 		list_del_init(&cache->ro_list);
270926ce2095SJosef Bacik 	}
271026ce2095SJosef Bacik 	spin_unlock(&cache->lock);
271126ce2095SJosef Bacik 	spin_unlock(&sinfo->lock);
271226ce2095SJosef Bacik }
271377745c05SJosef Bacik 
27143be4d8efSQu Wenruo static int update_block_group_item(struct btrfs_trans_handle *trans,
271577745c05SJosef Bacik 				   struct btrfs_path *path,
271632da5386SDavid Sterba 				   struct btrfs_block_group *cache)
271777745c05SJosef Bacik {
271877745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
271977745c05SJosef Bacik 	int ret;
2720dfe8aec4SJosef Bacik 	struct btrfs_root *root = btrfs_block_group_root(fs_info);
272177745c05SJosef Bacik 	unsigned long bi;
272277745c05SJosef Bacik 	struct extent_buffer *leaf;
2723bf38be65SDavid Sterba 	struct btrfs_block_group_item bgi;
2724b3470b5dSDavid Sterba 	struct btrfs_key key;
272577745c05SJosef Bacik 
2726b3470b5dSDavid Sterba 	key.objectid = cache->start;
2727b3470b5dSDavid Sterba 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2728b3470b5dSDavid Sterba 	key.offset = cache->length;
2729b3470b5dSDavid Sterba 
27303be4d8efSQu Wenruo 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
273177745c05SJosef Bacik 	if (ret) {
273277745c05SJosef Bacik 		if (ret > 0)
273377745c05SJosef Bacik 			ret = -ENOENT;
273477745c05SJosef Bacik 		goto fail;
273577745c05SJosef Bacik 	}
273677745c05SJosef Bacik 
273777745c05SJosef Bacik 	leaf = path->nodes[0];
273877745c05SJosef Bacik 	bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2739de0dc456SDavid Sterba 	btrfs_set_stack_block_group_used(&bgi, cache->used);
2740de0dc456SDavid Sterba 	btrfs_set_stack_block_group_chunk_objectid(&bgi,
2741f7238e50SJosef Bacik 						   cache->global_root_id);
2742de0dc456SDavid Sterba 	btrfs_set_stack_block_group_flags(&bgi, cache->flags);
2743bf38be65SDavid Sterba 	write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
274477745c05SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
274577745c05SJosef Bacik fail:
274677745c05SJosef Bacik 	btrfs_release_path(path);
274777745c05SJosef Bacik 	return ret;
274877745c05SJosef Bacik 
274977745c05SJosef Bacik }
275077745c05SJosef Bacik 
275132da5386SDavid Sterba static int cache_save_setup(struct btrfs_block_group *block_group,
275277745c05SJosef Bacik 			    struct btrfs_trans_handle *trans,
275377745c05SJosef Bacik 			    struct btrfs_path *path)
275477745c05SJosef Bacik {
275577745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
275677745c05SJosef Bacik 	struct btrfs_root *root = fs_info->tree_root;
275777745c05SJosef Bacik 	struct inode *inode = NULL;
275877745c05SJosef Bacik 	struct extent_changeset *data_reserved = NULL;
275977745c05SJosef Bacik 	u64 alloc_hint = 0;
276077745c05SJosef Bacik 	int dcs = BTRFS_DC_ERROR;
27610044ae11SQu Wenruo 	u64 cache_size = 0;
276277745c05SJosef Bacik 	int retries = 0;
276377745c05SJosef Bacik 	int ret = 0;
276477745c05SJosef Bacik 
2765af456a2cSBoris Burkov 	if (!btrfs_test_opt(fs_info, SPACE_CACHE))
2766af456a2cSBoris Burkov 		return 0;
2767af456a2cSBoris Burkov 
276877745c05SJosef Bacik 	/*
276977745c05SJosef Bacik 	 * If this block group is smaller than 100 megs don't bother caching the
277077745c05SJosef Bacik 	 * block group.
277177745c05SJosef Bacik 	 */
2772b3470b5dSDavid Sterba 	if (block_group->length < (100 * SZ_1M)) {
277377745c05SJosef Bacik 		spin_lock(&block_group->lock);
277477745c05SJosef Bacik 		block_group->disk_cache_state = BTRFS_DC_WRITTEN;
277577745c05SJosef Bacik 		spin_unlock(&block_group->lock);
277677745c05SJosef Bacik 		return 0;
277777745c05SJosef Bacik 	}
277877745c05SJosef Bacik 
2779bf31f87fSDavid Sterba 	if (TRANS_ABORTED(trans))
278077745c05SJosef Bacik 		return 0;
278177745c05SJosef Bacik again:
278277745c05SJosef Bacik 	inode = lookup_free_space_inode(block_group, path);
278377745c05SJosef Bacik 	if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
278477745c05SJosef Bacik 		ret = PTR_ERR(inode);
278577745c05SJosef Bacik 		btrfs_release_path(path);
278677745c05SJosef Bacik 		goto out;
278777745c05SJosef Bacik 	}
278877745c05SJosef Bacik 
278977745c05SJosef Bacik 	if (IS_ERR(inode)) {
279077745c05SJosef Bacik 		BUG_ON(retries);
279177745c05SJosef Bacik 		retries++;
279277745c05SJosef Bacik 
279377745c05SJosef Bacik 		if (block_group->ro)
279477745c05SJosef Bacik 			goto out_free;
279577745c05SJosef Bacik 
279677745c05SJosef Bacik 		ret = create_free_space_inode(trans, block_group, path);
279777745c05SJosef Bacik 		if (ret)
279877745c05SJosef Bacik 			goto out_free;
279977745c05SJosef Bacik 		goto again;
280077745c05SJosef Bacik 	}
280177745c05SJosef Bacik 
280277745c05SJosef Bacik 	/*
280377745c05SJosef Bacik 	 * We want to set the generation to 0, that way if anything goes wrong
280477745c05SJosef Bacik 	 * from here on out we know not to trust this cache when we load up next
280577745c05SJosef Bacik 	 * time.
280677745c05SJosef Bacik 	 */
280777745c05SJosef Bacik 	BTRFS_I(inode)->generation = 0;
28089a56fcd1SNikolay Borisov 	ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
280977745c05SJosef Bacik 	if (ret) {
281077745c05SJosef Bacik 		/*
281177745c05SJosef Bacik 		 * So theoretically we could recover from this, simply set the
281277745c05SJosef Bacik 		 * super cache generation to 0 so we know to invalidate the
281377745c05SJosef Bacik 		 * cache, but then we'd have to keep track of the block groups
281477745c05SJosef Bacik 		 * that fail this way so we know we _have_ to reset this cache
281577745c05SJosef Bacik 		 * before the next commit or risk reading stale cache.  So to
281677745c05SJosef Bacik 		 * limit our exposure to horrible edge cases lets just abort the
281777745c05SJosef Bacik 		 * transaction, this only happens in really bad situations
281877745c05SJosef Bacik 		 * anyway.
281977745c05SJosef Bacik 		 */
282077745c05SJosef Bacik 		btrfs_abort_transaction(trans, ret);
282177745c05SJosef Bacik 		goto out_put;
282277745c05SJosef Bacik 	}
282377745c05SJosef Bacik 	WARN_ON(ret);
282477745c05SJosef Bacik 
282577745c05SJosef Bacik 	/* We've already setup this transaction, go ahead and exit */
282677745c05SJosef Bacik 	if (block_group->cache_generation == trans->transid &&
282777745c05SJosef Bacik 	    i_size_read(inode)) {
282877745c05SJosef Bacik 		dcs = BTRFS_DC_SETUP;
282977745c05SJosef Bacik 		goto out_put;
283077745c05SJosef Bacik 	}
283177745c05SJosef Bacik 
283277745c05SJosef Bacik 	if (i_size_read(inode) > 0) {
283377745c05SJosef Bacik 		ret = btrfs_check_trunc_cache_free_space(fs_info,
283477745c05SJosef Bacik 					&fs_info->global_block_rsv);
283577745c05SJosef Bacik 		if (ret)
283677745c05SJosef Bacik 			goto out_put;
283777745c05SJosef Bacik 
283877745c05SJosef Bacik 		ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
283977745c05SJosef Bacik 		if (ret)
284077745c05SJosef Bacik 			goto out_put;
284177745c05SJosef Bacik 	}
284277745c05SJosef Bacik 
284377745c05SJosef Bacik 	spin_lock(&block_group->lock);
284477745c05SJosef Bacik 	if (block_group->cached != BTRFS_CACHE_FINISHED ||
284577745c05SJosef Bacik 	    !btrfs_test_opt(fs_info, SPACE_CACHE)) {
284677745c05SJosef Bacik 		/*
284777745c05SJosef Bacik 		 * don't bother trying to write stuff out _if_
284877745c05SJosef Bacik 		 * a) we're not cached,
284977745c05SJosef Bacik 		 * b) we're with nospace_cache mount option,
285077745c05SJosef Bacik 		 * c) we're with v2 space_cache (FREE_SPACE_TREE).
285177745c05SJosef Bacik 		 */
285277745c05SJosef Bacik 		dcs = BTRFS_DC_WRITTEN;
285377745c05SJosef Bacik 		spin_unlock(&block_group->lock);
285477745c05SJosef Bacik 		goto out_put;
285577745c05SJosef Bacik 	}
285677745c05SJosef Bacik 	spin_unlock(&block_group->lock);
285777745c05SJosef Bacik 
285877745c05SJosef Bacik 	/*
285977745c05SJosef Bacik 	 * We hit an ENOSPC when setting up the cache in this transaction, just
286077745c05SJosef Bacik 	 * skip doing the setup, we've already cleared the cache so we're safe.
286177745c05SJosef Bacik 	 */
286277745c05SJosef Bacik 	if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
286377745c05SJosef Bacik 		ret = -ENOSPC;
286477745c05SJosef Bacik 		goto out_put;
286577745c05SJosef Bacik 	}
286677745c05SJosef Bacik 
286777745c05SJosef Bacik 	/*
286877745c05SJosef Bacik 	 * Try to preallocate enough space based on how big the block group is.
286977745c05SJosef Bacik 	 * Keep in mind this has to include any pinned space which could end up
287077745c05SJosef Bacik 	 * taking up quite a bit since it's not folded into the other space
287177745c05SJosef Bacik 	 * cache.
287277745c05SJosef Bacik 	 */
28730044ae11SQu Wenruo 	cache_size = div_u64(block_group->length, SZ_256M);
28740044ae11SQu Wenruo 	if (!cache_size)
28750044ae11SQu Wenruo 		cache_size = 1;
287677745c05SJosef Bacik 
28770044ae11SQu Wenruo 	cache_size *= 16;
28780044ae11SQu Wenruo 	cache_size *= fs_info->sectorsize;
287977745c05SJosef Bacik 
288036ea6f3eSNikolay Borisov 	ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, 0,
28811daedb1dSJosef Bacik 					  cache_size, false);
288277745c05SJosef Bacik 	if (ret)
288377745c05SJosef Bacik 		goto out_put;
288477745c05SJosef Bacik 
28850044ae11SQu Wenruo 	ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, cache_size,
28860044ae11SQu Wenruo 					      cache_size, cache_size,
288777745c05SJosef Bacik 					      &alloc_hint);
288877745c05SJosef Bacik 	/*
288977745c05SJosef Bacik 	 * Our cache requires contiguous chunks so that we don't modify a bunch
289077745c05SJosef Bacik 	 * of metadata or split extents when writing the cache out, which means
289177745c05SJosef Bacik 	 * we can enospc if we are heavily fragmented in addition to just normal
289277745c05SJosef Bacik 	 * out of space conditions.  So if we hit this just skip setting up any
289377745c05SJosef Bacik 	 * other block groups for this transaction, maybe we'll unpin enough
289477745c05SJosef Bacik 	 * space the next time around.
289577745c05SJosef Bacik 	 */
289677745c05SJosef Bacik 	if (!ret)
289777745c05SJosef Bacik 		dcs = BTRFS_DC_SETUP;
289877745c05SJosef Bacik 	else if (ret == -ENOSPC)
289977745c05SJosef Bacik 		set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
290077745c05SJosef Bacik 
290177745c05SJosef Bacik out_put:
290277745c05SJosef Bacik 	iput(inode);
290377745c05SJosef Bacik out_free:
290477745c05SJosef Bacik 	btrfs_release_path(path);
290577745c05SJosef Bacik out:
290677745c05SJosef Bacik 	spin_lock(&block_group->lock);
290777745c05SJosef Bacik 	if (!ret && dcs == BTRFS_DC_SETUP)
290877745c05SJosef Bacik 		block_group->cache_generation = trans->transid;
290977745c05SJosef Bacik 	block_group->disk_cache_state = dcs;
291077745c05SJosef Bacik 	spin_unlock(&block_group->lock);
291177745c05SJosef Bacik 
291277745c05SJosef Bacik 	extent_changeset_free(data_reserved);
291377745c05SJosef Bacik 	return ret;
291477745c05SJosef Bacik }
291577745c05SJosef Bacik 
291677745c05SJosef Bacik int btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
291777745c05SJosef Bacik {
291877745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
291932da5386SDavid Sterba 	struct btrfs_block_group *cache, *tmp;
292077745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
292177745c05SJosef Bacik 	struct btrfs_path *path;
292277745c05SJosef Bacik 
292377745c05SJosef Bacik 	if (list_empty(&cur_trans->dirty_bgs) ||
292477745c05SJosef Bacik 	    !btrfs_test_opt(fs_info, SPACE_CACHE))
292577745c05SJosef Bacik 		return 0;
292677745c05SJosef Bacik 
292777745c05SJosef Bacik 	path = btrfs_alloc_path();
292877745c05SJosef Bacik 	if (!path)
292977745c05SJosef Bacik 		return -ENOMEM;
293077745c05SJosef Bacik 
293177745c05SJosef Bacik 	/* Could add new block groups, use _safe just in case */
293277745c05SJosef Bacik 	list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
293377745c05SJosef Bacik 				 dirty_list) {
293477745c05SJosef Bacik 		if (cache->disk_cache_state == BTRFS_DC_CLEAR)
293577745c05SJosef Bacik 			cache_save_setup(cache, trans, path);
293677745c05SJosef Bacik 	}
293777745c05SJosef Bacik 
293877745c05SJosef Bacik 	btrfs_free_path(path);
293977745c05SJosef Bacik 	return 0;
294077745c05SJosef Bacik }
294177745c05SJosef Bacik 
294277745c05SJosef Bacik /*
294377745c05SJosef Bacik  * Transaction commit does final block group cache writeback during a critical
294477745c05SJosef Bacik  * section where nothing is allowed to change the FS.  This is required in
294577745c05SJosef Bacik  * order for the cache to actually match the block group, but can introduce a
294677745c05SJosef Bacik  * lot of latency into the commit.
294777745c05SJosef Bacik  *
294877745c05SJosef Bacik  * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
294977745c05SJosef Bacik  * There's a chance we'll have to redo some of it if the block group changes
295077745c05SJosef Bacik  * again during the commit, but it greatly reduces the commit latency by
295177745c05SJosef Bacik  * getting rid of the easy block groups while we're still allowing others to
295277745c05SJosef Bacik  * join the commit.
295377745c05SJosef Bacik  */
295477745c05SJosef Bacik int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
295577745c05SJosef Bacik {
295677745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
295732da5386SDavid Sterba 	struct btrfs_block_group *cache;
295877745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
295977745c05SJosef Bacik 	int ret = 0;
296077745c05SJosef Bacik 	int should_put;
296177745c05SJosef Bacik 	struct btrfs_path *path = NULL;
296277745c05SJosef Bacik 	LIST_HEAD(dirty);
296377745c05SJosef Bacik 	struct list_head *io = &cur_trans->io_bgs;
296477745c05SJosef Bacik 	int loops = 0;
296577745c05SJosef Bacik 
296677745c05SJosef Bacik 	spin_lock(&cur_trans->dirty_bgs_lock);
296777745c05SJosef Bacik 	if (list_empty(&cur_trans->dirty_bgs)) {
296877745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
296977745c05SJosef Bacik 		return 0;
297077745c05SJosef Bacik 	}
297177745c05SJosef Bacik 	list_splice_init(&cur_trans->dirty_bgs, &dirty);
297277745c05SJosef Bacik 	spin_unlock(&cur_trans->dirty_bgs_lock);
297377745c05SJosef Bacik 
297477745c05SJosef Bacik again:
297577745c05SJosef Bacik 	/* Make sure all the block groups on our dirty list actually exist */
297677745c05SJosef Bacik 	btrfs_create_pending_block_groups(trans);
297777745c05SJosef Bacik 
297877745c05SJosef Bacik 	if (!path) {
297977745c05SJosef Bacik 		path = btrfs_alloc_path();
2980938fcbfbSJosef Bacik 		if (!path) {
2981938fcbfbSJosef Bacik 			ret = -ENOMEM;
2982938fcbfbSJosef Bacik 			goto out;
2983938fcbfbSJosef Bacik 		}
298477745c05SJosef Bacik 	}
298577745c05SJosef Bacik 
298677745c05SJosef Bacik 	/*
298777745c05SJosef Bacik 	 * cache_write_mutex is here only to save us from balance or automatic
298877745c05SJosef Bacik 	 * removal of empty block groups deleting this block group while we are
298977745c05SJosef Bacik 	 * writing out the cache
299077745c05SJosef Bacik 	 */
299177745c05SJosef Bacik 	mutex_lock(&trans->transaction->cache_write_mutex);
299277745c05SJosef Bacik 	while (!list_empty(&dirty)) {
299377745c05SJosef Bacik 		bool drop_reserve = true;
299477745c05SJosef Bacik 
299532da5386SDavid Sterba 		cache = list_first_entry(&dirty, struct btrfs_block_group,
299677745c05SJosef Bacik 					 dirty_list);
299777745c05SJosef Bacik 		/*
299877745c05SJosef Bacik 		 * This can happen if something re-dirties a block group that
299977745c05SJosef Bacik 		 * is already under IO.  Just wait for it to finish and then do
300077745c05SJosef Bacik 		 * it all again
300177745c05SJosef Bacik 		 */
300277745c05SJosef Bacik 		if (!list_empty(&cache->io_list)) {
300377745c05SJosef Bacik 			list_del_init(&cache->io_list);
300477745c05SJosef Bacik 			btrfs_wait_cache_io(trans, cache, path);
300577745c05SJosef Bacik 			btrfs_put_block_group(cache);
300677745c05SJosef Bacik 		}
300777745c05SJosef Bacik 
300877745c05SJosef Bacik 
300977745c05SJosef Bacik 		/*
301077745c05SJosef Bacik 		 * btrfs_wait_cache_io uses the cache->dirty_list to decide if
301177745c05SJosef Bacik 		 * it should update the cache_state.  Don't delete until after
301277745c05SJosef Bacik 		 * we wait.
301377745c05SJosef Bacik 		 *
301477745c05SJosef Bacik 		 * Since we're not running in the commit critical section
301577745c05SJosef Bacik 		 * we need the dirty_bgs_lock to protect from update_block_group
301677745c05SJosef Bacik 		 */
301777745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
301877745c05SJosef Bacik 		list_del_init(&cache->dirty_list);
301977745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
302077745c05SJosef Bacik 
302177745c05SJosef Bacik 		should_put = 1;
302277745c05SJosef Bacik 
302377745c05SJosef Bacik 		cache_save_setup(cache, trans, path);
302477745c05SJosef Bacik 
302577745c05SJosef Bacik 		if (cache->disk_cache_state == BTRFS_DC_SETUP) {
302677745c05SJosef Bacik 			cache->io_ctl.inode = NULL;
302777745c05SJosef Bacik 			ret = btrfs_write_out_cache(trans, cache, path);
302877745c05SJosef Bacik 			if (ret == 0 && cache->io_ctl.inode) {
302977745c05SJosef Bacik 				should_put = 0;
303077745c05SJosef Bacik 
303177745c05SJosef Bacik 				/*
303277745c05SJosef Bacik 				 * The cache_write_mutex is protecting the
303377745c05SJosef Bacik 				 * io_list, also refer to the definition of
303477745c05SJosef Bacik 				 * btrfs_transaction::io_bgs for more details
303577745c05SJosef Bacik 				 */
303677745c05SJosef Bacik 				list_add_tail(&cache->io_list, io);
303777745c05SJosef Bacik 			} else {
303877745c05SJosef Bacik 				/*
303977745c05SJosef Bacik 				 * If we failed to write the cache, the
304077745c05SJosef Bacik 				 * generation will be bad and life goes on
304177745c05SJosef Bacik 				 */
304277745c05SJosef Bacik 				ret = 0;
304377745c05SJosef Bacik 			}
304477745c05SJosef Bacik 		}
304577745c05SJosef Bacik 		if (!ret) {
30463be4d8efSQu Wenruo 			ret = update_block_group_item(trans, path, cache);
304777745c05SJosef Bacik 			/*
304877745c05SJosef Bacik 			 * Our block group might still be attached to the list
304977745c05SJosef Bacik 			 * of new block groups in the transaction handle of some
305077745c05SJosef Bacik 			 * other task (struct btrfs_trans_handle->new_bgs). This
305177745c05SJosef Bacik 			 * means its block group item isn't yet in the extent
305277745c05SJosef Bacik 			 * tree. If this happens ignore the error, as we will
305377745c05SJosef Bacik 			 * try again later in the critical section of the
305477745c05SJosef Bacik 			 * transaction commit.
305577745c05SJosef Bacik 			 */
305677745c05SJosef Bacik 			if (ret == -ENOENT) {
305777745c05SJosef Bacik 				ret = 0;
305877745c05SJosef Bacik 				spin_lock(&cur_trans->dirty_bgs_lock);
305977745c05SJosef Bacik 				if (list_empty(&cache->dirty_list)) {
306077745c05SJosef Bacik 					list_add_tail(&cache->dirty_list,
306177745c05SJosef Bacik 						      &cur_trans->dirty_bgs);
306277745c05SJosef Bacik 					btrfs_get_block_group(cache);
306377745c05SJosef Bacik 					drop_reserve = false;
306477745c05SJosef Bacik 				}
306577745c05SJosef Bacik 				spin_unlock(&cur_trans->dirty_bgs_lock);
306677745c05SJosef Bacik 			} else if (ret) {
306777745c05SJosef Bacik 				btrfs_abort_transaction(trans, ret);
306877745c05SJosef Bacik 			}
306977745c05SJosef Bacik 		}
307077745c05SJosef Bacik 
307177745c05SJosef Bacik 		/* If it's not on the io list, we need to put the block group */
307277745c05SJosef Bacik 		if (should_put)
307377745c05SJosef Bacik 			btrfs_put_block_group(cache);
307477745c05SJosef Bacik 		if (drop_reserve)
307577745c05SJosef Bacik 			btrfs_delayed_refs_rsv_release(fs_info, 1);
307677745c05SJosef Bacik 		/*
307777745c05SJosef Bacik 		 * Avoid blocking other tasks for too long. It might even save
307877745c05SJosef Bacik 		 * us from writing caches for block groups that are going to be
307977745c05SJosef Bacik 		 * removed.
308077745c05SJosef Bacik 		 */
308177745c05SJosef Bacik 		mutex_unlock(&trans->transaction->cache_write_mutex);
3082938fcbfbSJosef Bacik 		if (ret)
3083938fcbfbSJosef Bacik 			goto out;
308477745c05SJosef Bacik 		mutex_lock(&trans->transaction->cache_write_mutex);
308577745c05SJosef Bacik 	}
308677745c05SJosef Bacik 	mutex_unlock(&trans->transaction->cache_write_mutex);
308777745c05SJosef Bacik 
308877745c05SJosef Bacik 	/*
308977745c05SJosef Bacik 	 * Go through delayed refs for all the stuff we've just kicked off
309077745c05SJosef Bacik 	 * and then loop back (just once)
309177745c05SJosef Bacik 	 */
309234d1eb0eSJosef Bacik 	if (!ret)
309377745c05SJosef Bacik 		ret = btrfs_run_delayed_refs(trans, 0);
309477745c05SJosef Bacik 	if (!ret && loops == 0) {
309577745c05SJosef Bacik 		loops++;
309677745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
309777745c05SJosef Bacik 		list_splice_init(&cur_trans->dirty_bgs, &dirty);
309877745c05SJosef Bacik 		/*
309977745c05SJosef Bacik 		 * dirty_bgs_lock protects us from concurrent block group
310077745c05SJosef Bacik 		 * deletes too (not just cache_write_mutex).
310177745c05SJosef Bacik 		 */
310277745c05SJosef Bacik 		if (!list_empty(&dirty)) {
310377745c05SJosef Bacik 			spin_unlock(&cur_trans->dirty_bgs_lock);
310477745c05SJosef Bacik 			goto again;
310577745c05SJosef Bacik 		}
310677745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
3107938fcbfbSJosef Bacik 	}
3108938fcbfbSJosef Bacik out:
3109938fcbfbSJosef Bacik 	if (ret < 0) {
3110938fcbfbSJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
3111938fcbfbSJosef Bacik 		list_splice_init(&dirty, &cur_trans->dirty_bgs);
3112938fcbfbSJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
311377745c05SJosef Bacik 		btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
311477745c05SJosef Bacik 	}
311577745c05SJosef Bacik 
311677745c05SJosef Bacik 	btrfs_free_path(path);
311777745c05SJosef Bacik 	return ret;
311877745c05SJosef Bacik }
311977745c05SJosef Bacik 
312077745c05SJosef Bacik int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
312177745c05SJosef Bacik {
312277745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
312332da5386SDavid Sterba 	struct btrfs_block_group *cache;
312477745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
312577745c05SJosef Bacik 	int ret = 0;
312677745c05SJosef Bacik 	int should_put;
312777745c05SJosef Bacik 	struct btrfs_path *path;
312877745c05SJosef Bacik 	struct list_head *io = &cur_trans->io_bgs;
312977745c05SJosef Bacik 
313077745c05SJosef Bacik 	path = btrfs_alloc_path();
313177745c05SJosef Bacik 	if (!path)
313277745c05SJosef Bacik 		return -ENOMEM;
313377745c05SJosef Bacik 
313477745c05SJosef Bacik 	/*
313577745c05SJosef Bacik 	 * Even though we are in the critical section of the transaction commit,
313677745c05SJosef Bacik 	 * we can still have concurrent tasks adding elements to this
313777745c05SJosef Bacik 	 * transaction's list of dirty block groups. These tasks correspond to
313877745c05SJosef Bacik 	 * endio free space workers started when writeback finishes for a
313977745c05SJosef Bacik 	 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
314077745c05SJosef Bacik 	 * allocate new block groups as a result of COWing nodes of the root
314177745c05SJosef Bacik 	 * tree when updating the free space inode. The writeback for the space
314277745c05SJosef Bacik 	 * caches is triggered by an earlier call to
314377745c05SJosef Bacik 	 * btrfs_start_dirty_block_groups() and iterations of the following
314477745c05SJosef Bacik 	 * loop.
314577745c05SJosef Bacik 	 * Also we want to do the cache_save_setup first and then run the
314677745c05SJosef Bacik 	 * delayed refs to make sure we have the best chance at doing this all
314777745c05SJosef Bacik 	 * in one shot.
314877745c05SJosef Bacik 	 */
314977745c05SJosef Bacik 	spin_lock(&cur_trans->dirty_bgs_lock);
315077745c05SJosef Bacik 	while (!list_empty(&cur_trans->dirty_bgs)) {
315177745c05SJosef Bacik 		cache = list_first_entry(&cur_trans->dirty_bgs,
315232da5386SDavid Sterba 					 struct btrfs_block_group,
315377745c05SJosef Bacik 					 dirty_list);
315477745c05SJosef Bacik 
315577745c05SJosef Bacik 		/*
315677745c05SJosef Bacik 		 * This can happen if cache_save_setup re-dirties a block group
315777745c05SJosef Bacik 		 * that is already under IO.  Just wait for it to finish and
315877745c05SJosef Bacik 		 * then do it all again
315977745c05SJosef Bacik 		 */
316077745c05SJosef Bacik 		if (!list_empty(&cache->io_list)) {
316177745c05SJosef Bacik 			spin_unlock(&cur_trans->dirty_bgs_lock);
316277745c05SJosef Bacik 			list_del_init(&cache->io_list);
316377745c05SJosef Bacik 			btrfs_wait_cache_io(trans, cache, path);
316477745c05SJosef Bacik 			btrfs_put_block_group(cache);
316577745c05SJosef Bacik 			spin_lock(&cur_trans->dirty_bgs_lock);
316677745c05SJosef Bacik 		}
316777745c05SJosef Bacik 
316877745c05SJosef Bacik 		/*
316977745c05SJosef Bacik 		 * Don't remove from the dirty list until after we've waited on
317077745c05SJosef Bacik 		 * any pending IO
317177745c05SJosef Bacik 		 */
317277745c05SJosef Bacik 		list_del_init(&cache->dirty_list);
317377745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
317477745c05SJosef Bacik 		should_put = 1;
317577745c05SJosef Bacik 
317677745c05SJosef Bacik 		cache_save_setup(cache, trans, path);
317777745c05SJosef Bacik 
317877745c05SJosef Bacik 		if (!ret)
317977745c05SJosef Bacik 			ret = btrfs_run_delayed_refs(trans,
318077745c05SJosef Bacik 						     (unsigned long) -1);
318177745c05SJosef Bacik 
318277745c05SJosef Bacik 		if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
318377745c05SJosef Bacik 			cache->io_ctl.inode = NULL;
318477745c05SJosef Bacik 			ret = btrfs_write_out_cache(trans, cache, path);
318577745c05SJosef Bacik 			if (ret == 0 && cache->io_ctl.inode) {
318677745c05SJosef Bacik 				should_put = 0;
318777745c05SJosef Bacik 				list_add_tail(&cache->io_list, io);
318877745c05SJosef Bacik 			} else {
318977745c05SJosef Bacik 				/*
319077745c05SJosef Bacik 				 * If we failed to write the cache, the
319177745c05SJosef Bacik 				 * generation will be bad and life goes on
319277745c05SJosef Bacik 				 */
319377745c05SJosef Bacik 				ret = 0;
319477745c05SJosef Bacik 			}
319577745c05SJosef Bacik 		}
319677745c05SJosef Bacik 		if (!ret) {
31973be4d8efSQu Wenruo 			ret = update_block_group_item(trans, path, cache);
319877745c05SJosef Bacik 			/*
319977745c05SJosef Bacik 			 * One of the free space endio workers might have
320077745c05SJosef Bacik 			 * created a new block group while updating a free space
320177745c05SJosef Bacik 			 * cache's inode (at inode.c:btrfs_finish_ordered_io())
320277745c05SJosef Bacik 			 * and hasn't released its transaction handle yet, in
320377745c05SJosef Bacik 			 * which case the new block group is still attached to
320477745c05SJosef Bacik 			 * its transaction handle and its creation has not
320577745c05SJosef Bacik 			 * finished yet (no block group item in the extent tree
320677745c05SJosef Bacik 			 * yet, etc). If this is the case, wait for all free
320777745c05SJosef Bacik 			 * space endio workers to finish and retry. This is a
3208260db43cSRandy Dunlap 			 * very rare case so no need for a more efficient and
320977745c05SJosef Bacik 			 * complex approach.
321077745c05SJosef Bacik 			 */
321177745c05SJosef Bacik 			if (ret == -ENOENT) {
321277745c05SJosef Bacik 				wait_event(cur_trans->writer_wait,
321377745c05SJosef Bacik 				   atomic_read(&cur_trans->num_writers) == 1);
32143be4d8efSQu Wenruo 				ret = update_block_group_item(trans, path, cache);
321577745c05SJosef Bacik 			}
321677745c05SJosef Bacik 			if (ret)
321777745c05SJosef Bacik 				btrfs_abort_transaction(trans, ret);
321877745c05SJosef Bacik 		}
321977745c05SJosef Bacik 
322077745c05SJosef Bacik 		/* If its not on the io list, we need to put the block group */
322177745c05SJosef Bacik 		if (should_put)
322277745c05SJosef Bacik 			btrfs_put_block_group(cache);
322377745c05SJosef Bacik 		btrfs_delayed_refs_rsv_release(fs_info, 1);
322477745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
322577745c05SJosef Bacik 	}
322677745c05SJosef Bacik 	spin_unlock(&cur_trans->dirty_bgs_lock);
322777745c05SJosef Bacik 
322877745c05SJosef Bacik 	/*
322977745c05SJosef Bacik 	 * Refer to the definition of io_bgs member for details why it's safe
323077745c05SJosef Bacik 	 * to use it without any locking
323177745c05SJosef Bacik 	 */
323277745c05SJosef Bacik 	while (!list_empty(io)) {
323332da5386SDavid Sterba 		cache = list_first_entry(io, struct btrfs_block_group,
323477745c05SJosef Bacik 					 io_list);
323577745c05SJosef Bacik 		list_del_init(&cache->io_list);
323677745c05SJosef Bacik 		btrfs_wait_cache_io(trans, cache, path);
323777745c05SJosef Bacik 		btrfs_put_block_group(cache);
323877745c05SJosef Bacik 	}
323977745c05SJosef Bacik 
324077745c05SJosef Bacik 	btrfs_free_path(path);
324177745c05SJosef Bacik 	return ret;
324277745c05SJosef Bacik }
3243606d1bf1SJosef Bacik 
3244ac2f1e63SJosef Bacik static inline bool should_reclaim_block_group(struct btrfs_block_group *bg,
3245ac2f1e63SJosef Bacik 					      u64 bytes_freed)
3246ac2f1e63SJosef Bacik {
3247ac2f1e63SJosef Bacik 	const struct btrfs_space_info *space_info = bg->space_info;
3248ac2f1e63SJosef Bacik 	const int reclaim_thresh = READ_ONCE(space_info->bg_reclaim_threshold);
3249ac2f1e63SJosef Bacik 	const u64 new_val = bg->used;
3250ac2f1e63SJosef Bacik 	const u64 old_val = new_val + bytes_freed;
3251ac2f1e63SJosef Bacik 	u64 thresh;
3252ac2f1e63SJosef Bacik 
3253ac2f1e63SJosef Bacik 	if (reclaim_thresh == 0)
3254ac2f1e63SJosef Bacik 		return false;
3255ac2f1e63SJosef Bacik 
3256ac2f1e63SJosef Bacik 	thresh = div_factor_fine(bg->length, reclaim_thresh);
3257ac2f1e63SJosef Bacik 
3258ac2f1e63SJosef Bacik 	/*
3259ac2f1e63SJosef Bacik 	 * If we were below the threshold before don't reclaim, we are likely a
3260ac2f1e63SJosef Bacik 	 * brand new block group and we don't want to relocate new block groups.
3261ac2f1e63SJosef Bacik 	 */
3262ac2f1e63SJosef Bacik 	if (old_val < thresh)
3263ac2f1e63SJosef Bacik 		return false;
3264ac2f1e63SJosef Bacik 	if (new_val >= thresh)
3265ac2f1e63SJosef Bacik 		return false;
3266ac2f1e63SJosef Bacik 	return true;
3267ac2f1e63SJosef Bacik }
3268ac2f1e63SJosef Bacik 
3269606d1bf1SJosef Bacik int btrfs_update_block_group(struct btrfs_trans_handle *trans,
327011b66fa6SAnand Jain 			     u64 bytenr, u64 num_bytes, bool alloc)
3271606d1bf1SJosef Bacik {
3272606d1bf1SJosef Bacik 	struct btrfs_fs_info *info = trans->fs_info;
327332da5386SDavid Sterba 	struct btrfs_block_group *cache = NULL;
3274606d1bf1SJosef Bacik 	u64 total = num_bytes;
3275606d1bf1SJosef Bacik 	u64 old_val;
3276606d1bf1SJosef Bacik 	u64 byte_in_group;
3277606d1bf1SJosef Bacik 	int factor;
3278606d1bf1SJosef Bacik 	int ret = 0;
3279606d1bf1SJosef Bacik 
3280606d1bf1SJosef Bacik 	/* Block accounting for super block */
3281606d1bf1SJosef Bacik 	spin_lock(&info->delalloc_root_lock);
3282606d1bf1SJosef Bacik 	old_val = btrfs_super_bytes_used(info->super_copy);
3283606d1bf1SJosef Bacik 	if (alloc)
3284606d1bf1SJosef Bacik 		old_val += num_bytes;
3285606d1bf1SJosef Bacik 	else
3286606d1bf1SJosef Bacik 		old_val -= num_bytes;
3287606d1bf1SJosef Bacik 	btrfs_set_super_bytes_used(info->super_copy, old_val);
3288606d1bf1SJosef Bacik 	spin_unlock(&info->delalloc_root_lock);
3289606d1bf1SJosef Bacik 
3290606d1bf1SJosef Bacik 	while (total) {
3291ac2f1e63SJosef Bacik 		bool reclaim;
3292ac2f1e63SJosef Bacik 
3293606d1bf1SJosef Bacik 		cache = btrfs_lookup_block_group(info, bytenr);
3294606d1bf1SJosef Bacik 		if (!cache) {
3295606d1bf1SJosef Bacik 			ret = -ENOENT;
3296606d1bf1SJosef Bacik 			break;
3297606d1bf1SJosef Bacik 		}
3298606d1bf1SJosef Bacik 		factor = btrfs_bg_type_to_factor(cache->flags);
3299606d1bf1SJosef Bacik 
3300606d1bf1SJosef Bacik 		/*
3301606d1bf1SJosef Bacik 		 * If this block group has free space cache written out, we
3302606d1bf1SJosef Bacik 		 * need to make sure to load it if we are removing space.  This
3303606d1bf1SJosef Bacik 		 * is because we need the unpinning stage to actually add the
3304606d1bf1SJosef Bacik 		 * space back to the block group, otherwise we will leak space.
3305606d1bf1SJosef Bacik 		 */
330632da5386SDavid Sterba 		if (!alloc && !btrfs_block_group_done(cache))
3307ced8ecf0SOmar Sandoval 			btrfs_cache_block_group(cache, true);
3308606d1bf1SJosef Bacik 
3309b3470b5dSDavid Sterba 		byte_in_group = bytenr - cache->start;
3310b3470b5dSDavid Sterba 		WARN_ON(byte_in_group > cache->length);
3311606d1bf1SJosef Bacik 
3312606d1bf1SJosef Bacik 		spin_lock(&cache->space_info->lock);
3313606d1bf1SJosef Bacik 		spin_lock(&cache->lock);
3314606d1bf1SJosef Bacik 
3315606d1bf1SJosef Bacik 		if (btrfs_test_opt(info, SPACE_CACHE) &&
3316606d1bf1SJosef Bacik 		    cache->disk_cache_state < BTRFS_DC_CLEAR)
3317606d1bf1SJosef Bacik 			cache->disk_cache_state = BTRFS_DC_CLEAR;
3318606d1bf1SJosef Bacik 
3319bf38be65SDavid Sterba 		old_val = cache->used;
3320b3470b5dSDavid Sterba 		num_bytes = min(total, cache->length - byte_in_group);
3321606d1bf1SJosef Bacik 		if (alloc) {
3322606d1bf1SJosef Bacik 			old_val += num_bytes;
3323bf38be65SDavid Sterba 			cache->used = old_val;
3324606d1bf1SJosef Bacik 			cache->reserved -= num_bytes;
3325606d1bf1SJosef Bacik 			cache->space_info->bytes_reserved -= num_bytes;
3326606d1bf1SJosef Bacik 			cache->space_info->bytes_used += num_bytes;
3327606d1bf1SJosef Bacik 			cache->space_info->disk_used += num_bytes * factor;
3328606d1bf1SJosef Bacik 			spin_unlock(&cache->lock);
3329606d1bf1SJosef Bacik 			spin_unlock(&cache->space_info->lock);
3330606d1bf1SJosef Bacik 		} else {
3331606d1bf1SJosef Bacik 			old_val -= num_bytes;
3332bf38be65SDavid Sterba 			cache->used = old_val;
3333606d1bf1SJosef Bacik 			cache->pinned += num_bytes;
3334606d1bf1SJosef Bacik 			btrfs_space_info_update_bytes_pinned(info,
3335606d1bf1SJosef Bacik 					cache->space_info, num_bytes);
3336606d1bf1SJosef Bacik 			cache->space_info->bytes_used -= num_bytes;
3337606d1bf1SJosef Bacik 			cache->space_info->disk_used -= num_bytes * factor;
3338ac2f1e63SJosef Bacik 
3339ac2f1e63SJosef Bacik 			reclaim = should_reclaim_block_group(cache, num_bytes);
3340606d1bf1SJosef Bacik 			spin_unlock(&cache->lock);
3341606d1bf1SJosef Bacik 			spin_unlock(&cache->space_info->lock);
3342606d1bf1SJosef Bacik 
3343fe119a6eSNikolay Borisov 			set_extent_dirty(&trans->transaction->pinned_extents,
3344606d1bf1SJosef Bacik 					 bytenr, bytenr + num_bytes - 1,
3345606d1bf1SJosef Bacik 					 GFP_NOFS | __GFP_NOFAIL);
3346606d1bf1SJosef Bacik 		}
3347606d1bf1SJosef Bacik 
3348606d1bf1SJosef Bacik 		spin_lock(&trans->transaction->dirty_bgs_lock);
3349606d1bf1SJosef Bacik 		if (list_empty(&cache->dirty_list)) {
3350606d1bf1SJosef Bacik 			list_add_tail(&cache->dirty_list,
3351606d1bf1SJosef Bacik 				      &trans->transaction->dirty_bgs);
3352606d1bf1SJosef Bacik 			trans->delayed_ref_updates++;
3353606d1bf1SJosef Bacik 			btrfs_get_block_group(cache);
3354606d1bf1SJosef Bacik 		}
3355606d1bf1SJosef Bacik 		spin_unlock(&trans->transaction->dirty_bgs_lock);
3356606d1bf1SJosef Bacik 
3357606d1bf1SJosef Bacik 		/*
3358606d1bf1SJosef Bacik 		 * No longer have used bytes in this block group, queue it for
3359606d1bf1SJosef Bacik 		 * deletion. We do this after adding the block group to the
3360606d1bf1SJosef Bacik 		 * dirty list to avoid races between cleaner kthread and space
3361606d1bf1SJosef Bacik 		 * cache writeout.
3362606d1bf1SJosef Bacik 		 */
33636e80d4f8SDennis Zhou 		if (!alloc && old_val == 0) {
33646e80d4f8SDennis Zhou 			if (!btrfs_test_opt(info, DISCARD_ASYNC))
3365606d1bf1SJosef Bacik 				btrfs_mark_bg_unused(cache);
3366ac2f1e63SJosef Bacik 		} else if (!alloc && reclaim) {
3367ac2f1e63SJosef Bacik 			btrfs_mark_bg_to_reclaim(cache);
33686e80d4f8SDennis Zhou 		}
3369606d1bf1SJosef Bacik 
3370606d1bf1SJosef Bacik 		btrfs_put_block_group(cache);
3371606d1bf1SJosef Bacik 		total -= num_bytes;
3372606d1bf1SJosef Bacik 		bytenr += num_bytes;
3373606d1bf1SJosef Bacik 	}
3374606d1bf1SJosef Bacik 
3375606d1bf1SJosef Bacik 	/* Modified block groups are accounted for in the delayed_refs_rsv. */
3376606d1bf1SJosef Bacik 	btrfs_update_delayed_refs_rsv(trans);
3377606d1bf1SJosef Bacik 	return ret;
3378606d1bf1SJosef Bacik }
3379606d1bf1SJosef Bacik 
3380606d1bf1SJosef Bacik /**
3381606d1bf1SJosef Bacik  * btrfs_add_reserved_bytes - update the block_group and space info counters
3382606d1bf1SJosef Bacik  * @cache:	The cache we are manipulating
3383606d1bf1SJosef Bacik  * @ram_bytes:  The number of bytes of file content, and will be same to
3384606d1bf1SJosef Bacik  *              @num_bytes except for the compress path.
3385606d1bf1SJosef Bacik  * @num_bytes:	The number of bytes in question
3386606d1bf1SJosef Bacik  * @delalloc:   The blocks are allocated for the delalloc write
3387606d1bf1SJosef Bacik  *
3388606d1bf1SJosef Bacik  * This is called by the allocator when it reserves space. If this is a
3389606d1bf1SJosef Bacik  * reservation and the block group has become read only we cannot make the
3390606d1bf1SJosef Bacik  * reservation and return -EAGAIN, otherwise this function always succeeds.
3391606d1bf1SJosef Bacik  */
339232da5386SDavid Sterba int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
3393606d1bf1SJosef Bacik 			     u64 ram_bytes, u64 num_bytes, int delalloc)
3394606d1bf1SJosef Bacik {
3395606d1bf1SJosef Bacik 	struct btrfs_space_info *space_info = cache->space_info;
3396606d1bf1SJosef Bacik 	int ret = 0;
3397606d1bf1SJosef Bacik 
3398606d1bf1SJosef Bacik 	spin_lock(&space_info->lock);
3399606d1bf1SJosef Bacik 	spin_lock(&cache->lock);
3400606d1bf1SJosef Bacik 	if (cache->ro) {
3401606d1bf1SJosef Bacik 		ret = -EAGAIN;
3402606d1bf1SJosef Bacik 	} else {
3403606d1bf1SJosef Bacik 		cache->reserved += num_bytes;
3404606d1bf1SJosef Bacik 		space_info->bytes_reserved += num_bytes;
3405a43c3835SJosef Bacik 		trace_btrfs_space_reservation(cache->fs_info, "space_info",
3406a43c3835SJosef Bacik 					      space_info->flags, num_bytes, 1);
3407606d1bf1SJosef Bacik 		btrfs_space_info_update_bytes_may_use(cache->fs_info,
3408606d1bf1SJosef Bacik 						      space_info, -ram_bytes);
3409606d1bf1SJosef Bacik 		if (delalloc)
3410606d1bf1SJosef Bacik 			cache->delalloc_bytes += num_bytes;
341199ffb43eSJosef Bacik 
341299ffb43eSJosef Bacik 		/*
341399ffb43eSJosef Bacik 		 * Compression can use less space than we reserved, so wake
341499ffb43eSJosef Bacik 		 * tickets if that happens
341599ffb43eSJosef Bacik 		 */
341699ffb43eSJosef Bacik 		if (num_bytes < ram_bytes)
341799ffb43eSJosef Bacik 			btrfs_try_granting_tickets(cache->fs_info, space_info);
3418606d1bf1SJosef Bacik 	}
3419606d1bf1SJosef Bacik 	spin_unlock(&cache->lock);
3420606d1bf1SJosef Bacik 	spin_unlock(&space_info->lock);
3421606d1bf1SJosef Bacik 	return ret;
3422606d1bf1SJosef Bacik }
3423606d1bf1SJosef Bacik 
3424606d1bf1SJosef Bacik /**
3425606d1bf1SJosef Bacik  * btrfs_free_reserved_bytes - update the block_group and space info counters
3426606d1bf1SJosef Bacik  * @cache:      The cache we are manipulating
3427606d1bf1SJosef Bacik  * @num_bytes:  The number of bytes in question
3428606d1bf1SJosef Bacik  * @delalloc:   The blocks are allocated for the delalloc write
3429606d1bf1SJosef Bacik  *
3430606d1bf1SJosef Bacik  * This is called by somebody who is freeing space that was never actually used
3431606d1bf1SJosef Bacik  * on disk.  For example if you reserve some space for a new leaf in transaction
3432606d1bf1SJosef Bacik  * A and before transaction A commits you free that leaf, you call this with
3433606d1bf1SJosef Bacik  * reserve set to 0 in order to clear the reservation.
3434606d1bf1SJosef Bacik  */
343532da5386SDavid Sterba void btrfs_free_reserved_bytes(struct btrfs_block_group *cache,
3436606d1bf1SJosef Bacik 			       u64 num_bytes, int delalloc)
3437606d1bf1SJosef Bacik {
3438606d1bf1SJosef Bacik 	struct btrfs_space_info *space_info = cache->space_info;
3439606d1bf1SJosef Bacik 
3440606d1bf1SJosef Bacik 	spin_lock(&space_info->lock);
3441606d1bf1SJosef Bacik 	spin_lock(&cache->lock);
3442606d1bf1SJosef Bacik 	if (cache->ro)
3443606d1bf1SJosef Bacik 		space_info->bytes_readonly += num_bytes;
3444606d1bf1SJosef Bacik 	cache->reserved -= num_bytes;
3445606d1bf1SJosef Bacik 	space_info->bytes_reserved -= num_bytes;
3446606d1bf1SJosef Bacik 	space_info->max_extent_size = 0;
3447606d1bf1SJosef Bacik 
3448606d1bf1SJosef Bacik 	if (delalloc)
3449606d1bf1SJosef Bacik 		cache->delalloc_bytes -= num_bytes;
3450606d1bf1SJosef Bacik 	spin_unlock(&cache->lock);
34513308234aSJosef Bacik 
34523308234aSJosef Bacik 	btrfs_try_granting_tickets(cache->fs_info, space_info);
3453606d1bf1SJosef Bacik 	spin_unlock(&space_info->lock);
3454606d1bf1SJosef Bacik }
345507730d87SJosef Bacik 
345607730d87SJosef Bacik static void force_metadata_allocation(struct btrfs_fs_info *info)
345707730d87SJosef Bacik {
345807730d87SJosef Bacik 	struct list_head *head = &info->space_info;
345907730d87SJosef Bacik 	struct btrfs_space_info *found;
346007730d87SJosef Bacik 
346172804905SJosef Bacik 	list_for_each_entry(found, head, list) {
346207730d87SJosef Bacik 		if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
346307730d87SJosef Bacik 			found->force_alloc = CHUNK_ALLOC_FORCE;
346407730d87SJosef Bacik 	}
346507730d87SJosef Bacik }
346607730d87SJosef Bacik 
346707730d87SJosef Bacik static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
346807730d87SJosef Bacik 			      struct btrfs_space_info *sinfo, int force)
346907730d87SJosef Bacik {
347007730d87SJosef Bacik 	u64 bytes_used = btrfs_space_info_used(sinfo, false);
347107730d87SJosef Bacik 	u64 thresh;
347207730d87SJosef Bacik 
347307730d87SJosef Bacik 	if (force == CHUNK_ALLOC_FORCE)
347407730d87SJosef Bacik 		return 1;
347507730d87SJosef Bacik 
347607730d87SJosef Bacik 	/*
347707730d87SJosef Bacik 	 * in limited mode, we want to have some free space up to
347807730d87SJosef Bacik 	 * about 1% of the FS size.
347907730d87SJosef Bacik 	 */
348007730d87SJosef Bacik 	if (force == CHUNK_ALLOC_LIMITED) {
348107730d87SJosef Bacik 		thresh = btrfs_super_total_bytes(fs_info->super_copy);
348207730d87SJosef Bacik 		thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
348307730d87SJosef Bacik 
348407730d87SJosef Bacik 		if (sinfo->total_bytes - bytes_used < thresh)
348507730d87SJosef Bacik 			return 1;
348607730d87SJosef Bacik 	}
348707730d87SJosef Bacik 
348807730d87SJosef Bacik 	if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
348907730d87SJosef Bacik 		return 0;
349007730d87SJosef Bacik 	return 1;
349107730d87SJosef Bacik }
349207730d87SJosef Bacik 
349307730d87SJosef Bacik int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
349407730d87SJosef Bacik {
349507730d87SJosef Bacik 	u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type);
349607730d87SJosef Bacik 
349707730d87SJosef Bacik 	return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
349807730d87SJosef Bacik }
349907730d87SJosef Bacik 
3500820c363bSNaohiro Aota static struct btrfs_block_group *do_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags)
350179bd3712SFilipe Manana {
350279bd3712SFilipe Manana 	struct btrfs_block_group *bg;
350379bd3712SFilipe Manana 	int ret;
350479bd3712SFilipe Manana 
350507730d87SJosef Bacik 	/*
350679bd3712SFilipe Manana 	 * Check if we have enough space in the system space info because we
350779bd3712SFilipe Manana 	 * will need to update device items in the chunk btree and insert a new
350879bd3712SFilipe Manana 	 * chunk item in the chunk btree as well. This will allocate a new
350979bd3712SFilipe Manana 	 * system block group if needed.
351079bd3712SFilipe Manana 	 */
351179bd3712SFilipe Manana 	check_system_chunk(trans, flags);
351279bd3712SFilipe Manana 
3513f6f39f7aSNikolay Borisov 	bg = btrfs_create_chunk(trans, flags);
351479bd3712SFilipe Manana 	if (IS_ERR(bg)) {
351579bd3712SFilipe Manana 		ret = PTR_ERR(bg);
351679bd3712SFilipe Manana 		goto out;
351779bd3712SFilipe Manana 	}
351879bd3712SFilipe Manana 
351979bd3712SFilipe Manana 	ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
352079bd3712SFilipe Manana 	/*
352179bd3712SFilipe Manana 	 * Normally we are not expected to fail with -ENOSPC here, since we have
352279bd3712SFilipe Manana 	 * previously reserved space in the system space_info and allocated one
3523ecd84d54SFilipe Manana 	 * new system chunk if necessary. However there are three exceptions:
352479bd3712SFilipe Manana 	 *
352579bd3712SFilipe Manana 	 * 1) We may have enough free space in the system space_info but all the
352679bd3712SFilipe Manana 	 *    existing system block groups have a profile which can not be used
352779bd3712SFilipe Manana 	 *    for extent allocation.
352879bd3712SFilipe Manana 	 *
352979bd3712SFilipe Manana 	 *    This happens when mounting in degraded mode. For example we have a
353079bd3712SFilipe Manana 	 *    RAID1 filesystem with 2 devices, lose one device and mount the fs
353179bd3712SFilipe Manana 	 *    using the other device in degraded mode. If we then allocate a chunk,
353279bd3712SFilipe Manana 	 *    we may have enough free space in the existing system space_info, but
353379bd3712SFilipe Manana 	 *    none of the block groups can be used for extent allocation since they
353479bd3712SFilipe Manana 	 *    have a RAID1 profile, and because we are in degraded mode with a
353579bd3712SFilipe Manana 	 *    single device, we are forced to allocate a new system chunk with a
353679bd3712SFilipe Manana 	 *    SINGLE profile. Making check_system_chunk() iterate over all system
353779bd3712SFilipe Manana 	 *    block groups and check if they have a usable profile and enough space
353879bd3712SFilipe Manana 	 *    can be slow on very large filesystems, so we tolerate the -ENOSPC and
353979bd3712SFilipe Manana 	 *    try again after forcing allocation of a new system chunk. Like this
354079bd3712SFilipe Manana 	 *    we avoid paying the cost of that search in normal circumstances, when
354179bd3712SFilipe Manana 	 *    we were not mounted in degraded mode;
354279bd3712SFilipe Manana 	 *
354379bd3712SFilipe Manana 	 * 2) We had enough free space info the system space_info, and one suitable
354479bd3712SFilipe Manana 	 *    block group to allocate from when we called check_system_chunk()
354579bd3712SFilipe Manana 	 *    above. However right after we called it, the only system block group
354679bd3712SFilipe Manana 	 *    with enough free space got turned into RO mode by a running scrub,
354779bd3712SFilipe Manana 	 *    and in this case we have to allocate a new one and retry. We only
354879bd3712SFilipe Manana 	 *    need do this allocate and retry once, since we have a transaction
3549ecd84d54SFilipe Manana 	 *    handle and scrub uses the commit root to search for block groups;
3550ecd84d54SFilipe Manana 	 *
3551ecd84d54SFilipe Manana 	 * 3) We had one system block group with enough free space when we called
3552ecd84d54SFilipe Manana 	 *    check_system_chunk(), but after that, right before we tried to
3553ecd84d54SFilipe Manana 	 *    allocate the last extent buffer we needed, a discard operation came
3554ecd84d54SFilipe Manana 	 *    in and it temporarily removed the last free space entry from the
3555ecd84d54SFilipe Manana 	 *    block group (discard removes a free space entry, discards it, and
3556ecd84d54SFilipe Manana 	 *    then adds back the entry to the block group cache).
355779bd3712SFilipe Manana 	 */
355879bd3712SFilipe Manana 	if (ret == -ENOSPC) {
355979bd3712SFilipe Manana 		const u64 sys_flags = btrfs_system_alloc_profile(trans->fs_info);
356079bd3712SFilipe Manana 		struct btrfs_block_group *sys_bg;
356179bd3712SFilipe Manana 
3562f6f39f7aSNikolay Borisov 		sys_bg = btrfs_create_chunk(trans, sys_flags);
356379bd3712SFilipe Manana 		if (IS_ERR(sys_bg)) {
356479bd3712SFilipe Manana 			ret = PTR_ERR(sys_bg);
356579bd3712SFilipe Manana 			btrfs_abort_transaction(trans, ret);
356679bd3712SFilipe Manana 			goto out;
356779bd3712SFilipe Manana 		}
356879bd3712SFilipe Manana 
356979bd3712SFilipe Manana 		ret = btrfs_chunk_alloc_add_chunk_item(trans, sys_bg);
357079bd3712SFilipe Manana 		if (ret) {
357179bd3712SFilipe Manana 			btrfs_abort_transaction(trans, ret);
357279bd3712SFilipe Manana 			goto out;
357379bd3712SFilipe Manana 		}
357479bd3712SFilipe Manana 
357579bd3712SFilipe Manana 		ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
357679bd3712SFilipe Manana 		if (ret) {
357779bd3712SFilipe Manana 			btrfs_abort_transaction(trans, ret);
357879bd3712SFilipe Manana 			goto out;
357979bd3712SFilipe Manana 		}
358079bd3712SFilipe Manana 	} else if (ret) {
358179bd3712SFilipe Manana 		btrfs_abort_transaction(trans, ret);
358279bd3712SFilipe Manana 		goto out;
358379bd3712SFilipe Manana 	}
358479bd3712SFilipe Manana out:
358579bd3712SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
358679bd3712SFilipe Manana 
3587820c363bSNaohiro Aota 	if (ret)
3588820c363bSNaohiro Aota 		return ERR_PTR(ret);
3589820c363bSNaohiro Aota 
3590820c363bSNaohiro Aota 	btrfs_get_block_group(bg);
3591820c363bSNaohiro Aota 	return bg;
359279bd3712SFilipe Manana }
359379bd3712SFilipe Manana 
359479bd3712SFilipe Manana /*
359579bd3712SFilipe Manana  * Chunk allocation is done in 2 phases:
359679bd3712SFilipe Manana  *
359779bd3712SFilipe Manana  * 1) Phase 1 - through btrfs_chunk_alloc() we allocate device extents for
359879bd3712SFilipe Manana  *    the chunk, the chunk mapping, create its block group and add the items
359979bd3712SFilipe Manana  *    that belong in the chunk btree to it - more specifically, we need to
360079bd3712SFilipe Manana  *    update device items in the chunk btree and add a new chunk item to it.
360179bd3712SFilipe Manana  *
360279bd3712SFilipe Manana  * 2) Phase 2 - through btrfs_create_pending_block_groups(), we add the block
360379bd3712SFilipe Manana  *    group item to the extent btree and the device extent items to the devices
360479bd3712SFilipe Manana  *    btree.
360579bd3712SFilipe Manana  *
360679bd3712SFilipe Manana  * This is done to prevent deadlocks. For example when COWing a node from the
360779bd3712SFilipe Manana  * extent btree we are holding a write lock on the node's parent and if we
360879bd3712SFilipe Manana  * trigger chunk allocation and attempted to insert the new block group item
360979bd3712SFilipe Manana  * in the extent btree right way, we could deadlock because the path for the
361079bd3712SFilipe Manana  * insertion can include that parent node. At first glance it seems impossible
361179bd3712SFilipe Manana  * to trigger chunk allocation after starting a transaction since tasks should
361279bd3712SFilipe Manana  * reserve enough transaction units (metadata space), however while that is true
361379bd3712SFilipe Manana  * most of the time, chunk allocation may still be triggered for several reasons:
361479bd3712SFilipe Manana  *
361579bd3712SFilipe Manana  * 1) When reserving metadata, we check if there is enough free space in the
361679bd3712SFilipe Manana  *    metadata space_info and therefore don't trigger allocation of a new chunk.
361779bd3712SFilipe Manana  *    However later when the task actually tries to COW an extent buffer from
361879bd3712SFilipe Manana  *    the extent btree or from the device btree for example, it is forced to
361979bd3712SFilipe Manana  *    allocate a new block group (chunk) because the only one that had enough
362079bd3712SFilipe Manana  *    free space was just turned to RO mode by a running scrub for example (or
362179bd3712SFilipe Manana  *    device replace, block group reclaim thread, etc), so we can not use it
362279bd3712SFilipe Manana  *    for allocating an extent and end up being forced to allocate a new one;
362379bd3712SFilipe Manana  *
362479bd3712SFilipe Manana  * 2) Because we only check that the metadata space_info has enough free bytes,
362579bd3712SFilipe Manana  *    we end up not allocating a new metadata chunk in that case. However if
362679bd3712SFilipe Manana  *    the filesystem was mounted in degraded mode, none of the existing block
362779bd3712SFilipe Manana  *    groups might be suitable for extent allocation due to their incompatible
362879bd3712SFilipe Manana  *    profile (for e.g. mounting a 2 devices filesystem, where all block groups
362979bd3712SFilipe Manana  *    use a RAID1 profile, in degraded mode using a single device). In this case
363079bd3712SFilipe Manana  *    when the task attempts to COW some extent buffer of the extent btree for
363179bd3712SFilipe Manana  *    example, it will trigger allocation of a new metadata block group with a
363279bd3712SFilipe Manana  *    suitable profile (SINGLE profile in the example of the degraded mount of
363379bd3712SFilipe Manana  *    the RAID1 filesystem);
363479bd3712SFilipe Manana  *
363579bd3712SFilipe Manana  * 3) The task has reserved enough transaction units / metadata space, but when
363679bd3712SFilipe Manana  *    it attempts to COW an extent buffer from the extent or device btree for
363779bd3712SFilipe Manana  *    example, it does not find any free extent in any metadata block group,
363879bd3712SFilipe Manana  *    therefore forced to try to allocate a new metadata block group.
363979bd3712SFilipe Manana  *    This is because some other task allocated all available extents in the
364079bd3712SFilipe Manana  *    meanwhile - this typically happens with tasks that don't reserve space
364179bd3712SFilipe Manana  *    properly, either intentionally or as a bug. One example where this is
364279bd3712SFilipe Manana  *    done intentionally is fsync, as it does not reserve any transaction units
364379bd3712SFilipe Manana  *    and ends up allocating a variable number of metadata extents for log
3644ecd84d54SFilipe Manana  *    tree extent buffers;
3645ecd84d54SFilipe Manana  *
3646ecd84d54SFilipe Manana  * 4) The task has reserved enough transaction units / metadata space, but right
3647ecd84d54SFilipe Manana  *    before it tries to allocate the last extent buffer it needs, a discard
3648ecd84d54SFilipe Manana  *    operation comes in and, temporarily, removes the last free space entry from
3649ecd84d54SFilipe Manana  *    the only metadata block group that had free space (discard starts by
3650ecd84d54SFilipe Manana  *    removing a free space entry from a block group, then does the discard
3651ecd84d54SFilipe Manana  *    operation and, once it's done, it adds back the free space entry to the
3652ecd84d54SFilipe Manana  *    block group).
365379bd3712SFilipe Manana  *
365479bd3712SFilipe Manana  * We also need this 2 phases setup when adding a device to a filesystem with
365579bd3712SFilipe Manana  * a seed device - we must create new metadata and system chunks without adding
365679bd3712SFilipe Manana  * any of the block group items to the chunk, extent and device btrees. If we
365779bd3712SFilipe Manana  * did not do it this way, we would get ENOSPC when attempting to update those
365879bd3712SFilipe Manana  * btrees, since all the chunks from the seed device are read-only.
365979bd3712SFilipe Manana  *
366079bd3712SFilipe Manana  * Phase 1 does the updates and insertions to the chunk btree because if we had
366179bd3712SFilipe Manana  * it done in phase 2 and have a thundering herd of tasks allocating chunks in
366279bd3712SFilipe Manana  * parallel, we risk having too many system chunks allocated by many tasks if
366379bd3712SFilipe Manana  * many tasks reach phase 1 without the previous ones completing phase 2. In the
366479bd3712SFilipe Manana  * extreme case this leads to exhaustion of the system chunk array in the
366579bd3712SFilipe Manana  * superblock. This is easier to trigger if using a btree node/leaf size of 64K
366679bd3712SFilipe Manana  * and with RAID filesystems (so we have more device items in the chunk btree).
366779bd3712SFilipe Manana  * This has happened before and commit eafa4fd0ad0607 ("btrfs: fix exhaustion of
366879bd3712SFilipe Manana  * the system chunk array due to concurrent allocations") provides more details.
366979bd3712SFilipe Manana  *
36702bb2e00eSFilipe Manana  * Allocation of system chunks does not happen through this function. A task that
36712bb2e00eSFilipe Manana  * needs to update the chunk btree (the only btree that uses system chunks), must
36722bb2e00eSFilipe Manana  * preallocate chunk space by calling either check_system_chunk() or
36732bb2e00eSFilipe Manana  * btrfs_reserve_chunk_metadata() - the former is used when allocating a data or
36742bb2e00eSFilipe Manana  * metadata chunk or when removing a chunk, while the later is used before doing
36752bb2e00eSFilipe Manana  * a modification to the chunk btree - use cases for the later are adding,
36762bb2e00eSFilipe Manana  * removing and resizing a device as well as relocation of a system chunk.
36772bb2e00eSFilipe Manana  * See the comment below for more details.
367879bd3712SFilipe Manana  *
367979bd3712SFilipe Manana  * The reservation of system space, done through check_system_chunk(), as well
368079bd3712SFilipe Manana  * as all the updates and insertions into the chunk btree must be done while
368179bd3712SFilipe Manana  * holding fs_info->chunk_mutex. This is important to guarantee that while COWing
368279bd3712SFilipe Manana  * an extent buffer from the chunks btree we never trigger allocation of a new
368379bd3712SFilipe Manana  * system chunk, which would result in a deadlock (trying to lock twice an
368479bd3712SFilipe Manana  * extent buffer of the chunk btree, first time before triggering the chunk
368579bd3712SFilipe Manana  * allocation and the second time during chunk allocation while attempting to
368679bd3712SFilipe Manana  * update the chunks btree). The system chunk array is also updated while holding
368779bd3712SFilipe Manana  * that mutex. The same logic applies to removing chunks - we must reserve system
368879bd3712SFilipe Manana  * space, update the chunk btree and the system chunk array in the superblock
368979bd3712SFilipe Manana  * while holding fs_info->chunk_mutex.
369079bd3712SFilipe Manana  *
369179bd3712SFilipe Manana  * This function, btrfs_chunk_alloc(), belongs to phase 1.
369279bd3712SFilipe Manana  *
369379bd3712SFilipe Manana  * If @force is CHUNK_ALLOC_FORCE:
369407730d87SJosef Bacik  *    - return 1 if it successfully allocates a chunk,
369507730d87SJosef Bacik  *    - return errors including -ENOSPC otherwise.
369679bd3712SFilipe Manana  * If @force is NOT CHUNK_ALLOC_FORCE:
369707730d87SJosef Bacik  *    - return 0 if it doesn't need to allocate a new chunk,
369807730d87SJosef Bacik  *    - return 1 if it successfully allocates a chunk,
369907730d87SJosef Bacik  *    - return errors including -ENOSPC otherwise.
370007730d87SJosef Bacik  */
370107730d87SJosef Bacik int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
370207730d87SJosef Bacik 		      enum btrfs_chunk_alloc_enum force)
370307730d87SJosef Bacik {
370407730d87SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
370507730d87SJosef Bacik 	struct btrfs_space_info *space_info;
3706820c363bSNaohiro Aota 	struct btrfs_block_group *ret_bg;
370707730d87SJosef Bacik 	bool wait_for_alloc = false;
370807730d87SJosef Bacik 	bool should_alloc = false;
3709760e69c4SNaohiro Aota 	bool from_extent_allocation = false;
371007730d87SJosef Bacik 	int ret = 0;
371107730d87SJosef Bacik 
3712760e69c4SNaohiro Aota 	if (force == CHUNK_ALLOC_FORCE_FOR_EXTENT) {
3713760e69c4SNaohiro Aota 		from_extent_allocation = true;
3714760e69c4SNaohiro Aota 		force = CHUNK_ALLOC_FORCE;
3715760e69c4SNaohiro Aota 	}
3716760e69c4SNaohiro Aota 
371707730d87SJosef Bacik 	/* Don't re-enter if we're already allocating a chunk */
371807730d87SJosef Bacik 	if (trans->allocating_chunk)
371907730d87SJosef Bacik 		return -ENOSPC;
372079bd3712SFilipe Manana 	/*
37212bb2e00eSFilipe Manana 	 * Allocation of system chunks can not happen through this path, as we
37222bb2e00eSFilipe Manana 	 * could end up in a deadlock if we are allocating a data or metadata
37232bb2e00eSFilipe Manana 	 * chunk and there is another task modifying the chunk btree.
37242bb2e00eSFilipe Manana 	 *
37252bb2e00eSFilipe Manana 	 * This is because while we are holding the chunk mutex, we will attempt
37262bb2e00eSFilipe Manana 	 * to add the new chunk item to the chunk btree or update an existing
37272bb2e00eSFilipe Manana 	 * device item in the chunk btree, while the other task that is modifying
37282bb2e00eSFilipe Manana 	 * the chunk btree is attempting to COW an extent buffer while holding a
37292bb2e00eSFilipe Manana 	 * lock on it and on its parent - if the COW operation triggers a system
37302bb2e00eSFilipe Manana 	 * chunk allocation, then we can deadlock because we are holding the
37312bb2e00eSFilipe Manana 	 * chunk mutex and we may need to access that extent buffer or its parent
37322bb2e00eSFilipe Manana 	 * in order to add the chunk item or update a device item.
37332bb2e00eSFilipe Manana 	 *
37342bb2e00eSFilipe Manana 	 * Tasks that want to modify the chunk tree should reserve system space
37352bb2e00eSFilipe Manana 	 * before updating the chunk btree, by calling either
37362bb2e00eSFilipe Manana 	 * btrfs_reserve_chunk_metadata() or check_system_chunk().
37372bb2e00eSFilipe Manana 	 * It's possible that after a task reserves the space, it still ends up
37382bb2e00eSFilipe Manana 	 * here - this happens in the cases described above at do_chunk_alloc().
37392bb2e00eSFilipe Manana 	 * The task will have to either retry or fail.
374079bd3712SFilipe Manana 	 */
37412bb2e00eSFilipe Manana 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
374279bd3712SFilipe Manana 		return -ENOSPC;
374307730d87SJosef Bacik 
374407730d87SJosef Bacik 	space_info = btrfs_find_space_info(fs_info, flags);
374507730d87SJosef Bacik 	ASSERT(space_info);
374607730d87SJosef Bacik 
374707730d87SJosef Bacik 	do {
374807730d87SJosef Bacik 		spin_lock(&space_info->lock);
374907730d87SJosef Bacik 		if (force < space_info->force_alloc)
375007730d87SJosef Bacik 			force = space_info->force_alloc;
375107730d87SJosef Bacik 		should_alloc = should_alloc_chunk(fs_info, space_info, force);
375207730d87SJosef Bacik 		if (space_info->full) {
375307730d87SJosef Bacik 			/* No more free physical space */
375407730d87SJosef Bacik 			if (should_alloc)
375507730d87SJosef Bacik 				ret = -ENOSPC;
375607730d87SJosef Bacik 			else
375707730d87SJosef Bacik 				ret = 0;
375807730d87SJosef Bacik 			spin_unlock(&space_info->lock);
375907730d87SJosef Bacik 			return ret;
376007730d87SJosef Bacik 		} else if (!should_alloc) {
376107730d87SJosef Bacik 			spin_unlock(&space_info->lock);
376207730d87SJosef Bacik 			return 0;
376307730d87SJosef Bacik 		} else if (space_info->chunk_alloc) {
376407730d87SJosef Bacik 			/*
376507730d87SJosef Bacik 			 * Someone is already allocating, so we need to block
376607730d87SJosef Bacik 			 * until this someone is finished and then loop to
376707730d87SJosef Bacik 			 * recheck if we should continue with our allocation
376807730d87SJosef Bacik 			 * attempt.
376907730d87SJosef Bacik 			 */
377007730d87SJosef Bacik 			wait_for_alloc = true;
37711314ca78SJosef Bacik 			force = CHUNK_ALLOC_NO_FORCE;
377207730d87SJosef Bacik 			spin_unlock(&space_info->lock);
377307730d87SJosef Bacik 			mutex_lock(&fs_info->chunk_mutex);
377407730d87SJosef Bacik 			mutex_unlock(&fs_info->chunk_mutex);
377507730d87SJosef Bacik 		} else {
377607730d87SJosef Bacik 			/* Proceed with allocation */
377707730d87SJosef Bacik 			space_info->chunk_alloc = 1;
377807730d87SJosef Bacik 			wait_for_alloc = false;
377907730d87SJosef Bacik 			spin_unlock(&space_info->lock);
378007730d87SJosef Bacik 		}
378107730d87SJosef Bacik 
378207730d87SJosef Bacik 		cond_resched();
378307730d87SJosef Bacik 	} while (wait_for_alloc);
378407730d87SJosef Bacik 
378507730d87SJosef Bacik 	mutex_lock(&fs_info->chunk_mutex);
378607730d87SJosef Bacik 	trans->allocating_chunk = true;
378707730d87SJosef Bacik 
378807730d87SJosef Bacik 	/*
378907730d87SJosef Bacik 	 * If we have mixed data/metadata chunks we want to make sure we keep
379007730d87SJosef Bacik 	 * allocating mixed chunks instead of individual chunks.
379107730d87SJosef Bacik 	 */
379207730d87SJosef Bacik 	if (btrfs_mixed_space_info(space_info))
379307730d87SJosef Bacik 		flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
379407730d87SJosef Bacik 
379507730d87SJosef Bacik 	/*
379607730d87SJosef Bacik 	 * if we're doing a data chunk, go ahead and make sure that
379707730d87SJosef Bacik 	 * we keep a reasonable number of metadata chunks allocated in the
379807730d87SJosef Bacik 	 * FS as well.
379907730d87SJosef Bacik 	 */
380007730d87SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
380107730d87SJosef Bacik 		fs_info->data_chunk_allocations++;
380207730d87SJosef Bacik 		if (!(fs_info->data_chunk_allocations %
380307730d87SJosef Bacik 		      fs_info->metadata_ratio))
380407730d87SJosef Bacik 			force_metadata_allocation(fs_info);
380507730d87SJosef Bacik 	}
380607730d87SJosef Bacik 
3807820c363bSNaohiro Aota 	ret_bg = do_chunk_alloc(trans, flags);
380807730d87SJosef Bacik 	trans->allocating_chunk = false;
380907730d87SJosef Bacik 
3810760e69c4SNaohiro Aota 	if (IS_ERR(ret_bg)) {
3811820c363bSNaohiro Aota 		ret = PTR_ERR(ret_bg);
3812760e69c4SNaohiro Aota 	} else if (from_extent_allocation) {
3813760e69c4SNaohiro Aota 		/*
3814760e69c4SNaohiro Aota 		 * New block group is likely to be used soon. Try to activate
3815760e69c4SNaohiro Aota 		 * it now. Failure is OK for now.
3816760e69c4SNaohiro Aota 		 */
3817760e69c4SNaohiro Aota 		btrfs_zone_activate(ret_bg);
3818760e69c4SNaohiro Aota 	}
3819760e69c4SNaohiro Aota 
3820760e69c4SNaohiro Aota 	if (!ret)
3821820c363bSNaohiro Aota 		btrfs_put_block_group(ret_bg);
3822820c363bSNaohiro Aota 
382307730d87SJosef Bacik 	spin_lock(&space_info->lock);
382407730d87SJosef Bacik 	if (ret < 0) {
382507730d87SJosef Bacik 		if (ret == -ENOSPC)
382607730d87SJosef Bacik 			space_info->full = 1;
382707730d87SJosef Bacik 		else
382807730d87SJosef Bacik 			goto out;
382907730d87SJosef Bacik 	} else {
383007730d87SJosef Bacik 		ret = 1;
383107730d87SJosef Bacik 		space_info->max_extent_size = 0;
383207730d87SJosef Bacik 	}
383307730d87SJosef Bacik 
383407730d87SJosef Bacik 	space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
383507730d87SJosef Bacik out:
383607730d87SJosef Bacik 	space_info->chunk_alloc = 0;
383707730d87SJosef Bacik 	spin_unlock(&space_info->lock);
383807730d87SJosef Bacik 	mutex_unlock(&fs_info->chunk_mutex);
383907730d87SJosef Bacik 
384007730d87SJosef Bacik 	return ret;
384107730d87SJosef Bacik }
384207730d87SJosef Bacik 
384307730d87SJosef Bacik static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
384407730d87SJosef Bacik {
384507730d87SJosef Bacik 	u64 num_dev;
384607730d87SJosef Bacik 
384707730d87SJosef Bacik 	num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max;
384807730d87SJosef Bacik 	if (!num_dev)
384907730d87SJosef Bacik 		num_dev = fs_info->fs_devices->rw_devices;
385007730d87SJosef Bacik 
385107730d87SJosef Bacik 	return num_dev;
385207730d87SJosef Bacik }
385307730d87SJosef Bacik 
38542bb2e00eSFilipe Manana static void reserve_chunk_space(struct btrfs_trans_handle *trans,
38552bb2e00eSFilipe Manana 				u64 bytes,
38562bb2e00eSFilipe Manana 				u64 type)
385707730d87SJosef Bacik {
385807730d87SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
385907730d87SJosef Bacik 	struct btrfs_space_info *info;
386007730d87SJosef Bacik 	u64 left;
386107730d87SJosef Bacik 	int ret = 0;
386207730d87SJosef Bacik 
386307730d87SJosef Bacik 	/*
386407730d87SJosef Bacik 	 * Needed because we can end up allocating a system chunk and for an
386507730d87SJosef Bacik 	 * atomic and race free space reservation in the chunk block reserve.
386607730d87SJosef Bacik 	 */
386707730d87SJosef Bacik 	lockdep_assert_held(&fs_info->chunk_mutex);
386807730d87SJosef Bacik 
386907730d87SJosef Bacik 	info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
387007730d87SJosef Bacik 	spin_lock(&info->lock);
387107730d87SJosef Bacik 	left = info->total_bytes - btrfs_space_info_used(info, true);
387207730d87SJosef Bacik 	spin_unlock(&info->lock);
387307730d87SJosef Bacik 
38742bb2e00eSFilipe Manana 	if (left < bytes && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
387507730d87SJosef Bacik 		btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
38762bb2e00eSFilipe Manana 			   left, bytes, type);
387707730d87SJosef Bacik 		btrfs_dump_space_info(fs_info, info, 0, 0);
387807730d87SJosef Bacik 	}
387907730d87SJosef Bacik 
38802bb2e00eSFilipe Manana 	if (left < bytes) {
388107730d87SJosef Bacik 		u64 flags = btrfs_system_alloc_profile(fs_info);
388279bd3712SFilipe Manana 		struct btrfs_block_group *bg;
388307730d87SJosef Bacik 
388407730d87SJosef Bacik 		/*
388507730d87SJosef Bacik 		 * Ignore failure to create system chunk. We might end up not
388607730d87SJosef Bacik 		 * needing it, as we might not need to COW all nodes/leafs from
388707730d87SJosef Bacik 		 * the paths we visit in the chunk tree (they were already COWed
388807730d87SJosef Bacik 		 * or created in the current transaction for example).
388907730d87SJosef Bacik 		 */
3890f6f39f7aSNikolay Borisov 		bg = btrfs_create_chunk(trans, flags);
389179bd3712SFilipe Manana 		if (IS_ERR(bg)) {
389279bd3712SFilipe Manana 			ret = PTR_ERR(bg);
38932bb2e00eSFilipe Manana 		} else {
389479bd3712SFilipe Manana 			/*
3895b6a98021SNaohiro Aota 			 * We have a new chunk. We also need to activate it for
3896b6a98021SNaohiro Aota 			 * zoned filesystem.
3897b6a98021SNaohiro Aota 			 */
3898b6a98021SNaohiro Aota 			ret = btrfs_zoned_activate_one_bg(fs_info, info, true);
3899b6a98021SNaohiro Aota 			if (ret < 0)
3900b6a98021SNaohiro Aota 				return;
3901b6a98021SNaohiro Aota 
3902b6a98021SNaohiro Aota 			/*
390379bd3712SFilipe Manana 			 * If we fail to add the chunk item here, we end up
390479bd3712SFilipe Manana 			 * trying again at phase 2 of chunk allocation, at
390579bd3712SFilipe Manana 			 * btrfs_create_pending_block_groups(). So ignore
39062bb2e00eSFilipe Manana 			 * any error here. An ENOSPC here could happen, due to
39072bb2e00eSFilipe Manana 			 * the cases described at do_chunk_alloc() - the system
39082bb2e00eSFilipe Manana 			 * block group we just created was just turned into RO
39092bb2e00eSFilipe Manana 			 * mode by a scrub for example, or a running discard
39102bb2e00eSFilipe Manana 			 * temporarily removed its free space entries, etc.
391179bd3712SFilipe Manana 			 */
391279bd3712SFilipe Manana 			btrfs_chunk_alloc_add_chunk_item(trans, bg);
391379bd3712SFilipe Manana 		}
391407730d87SJosef Bacik 	}
391507730d87SJosef Bacik 
391607730d87SJosef Bacik 	if (!ret) {
39179270501cSJosef Bacik 		ret = btrfs_block_rsv_add(fs_info,
391807730d87SJosef Bacik 					  &fs_info->chunk_block_rsv,
39192bb2e00eSFilipe Manana 					  bytes, BTRFS_RESERVE_NO_FLUSH);
39201cb3db1cSFilipe Manana 		if (!ret)
39212bb2e00eSFilipe Manana 			trans->chunk_bytes_reserved += bytes;
392207730d87SJosef Bacik 	}
392307730d87SJosef Bacik }
392407730d87SJosef Bacik 
39252bb2e00eSFilipe Manana /*
39262bb2e00eSFilipe Manana  * Reserve space in the system space for allocating or removing a chunk.
39272bb2e00eSFilipe Manana  * The caller must be holding fs_info->chunk_mutex.
39282bb2e00eSFilipe Manana  */
39292bb2e00eSFilipe Manana void check_system_chunk(struct btrfs_trans_handle *trans, u64 type)
39302bb2e00eSFilipe Manana {
39312bb2e00eSFilipe Manana 	struct btrfs_fs_info *fs_info = trans->fs_info;
39322bb2e00eSFilipe Manana 	const u64 num_devs = get_profile_num_devs(fs_info, type);
39332bb2e00eSFilipe Manana 	u64 bytes;
39342bb2e00eSFilipe Manana 
39352bb2e00eSFilipe Manana 	/* num_devs device items to update and 1 chunk item to add or remove. */
39362bb2e00eSFilipe Manana 	bytes = btrfs_calc_metadata_size(fs_info, num_devs) +
39372bb2e00eSFilipe Manana 		btrfs_calc_insert_metadata_size(fs_info, 1);
39382bb2e00eSFilipe Manana 
39392bb2e00eSFilipe Manana 	reserve_chunk_space(trans, bytes, type);
39402bb2e00eSFilipe Manana }
39412bb2e00eSFilipe Manana 
39422bb2e00eSFilipe Manana /*
39432bb2e00eSFilipe Manana  * Reserve space in the system space, if needed, for doing a modification to the
39442bb2e00eSFilipe Manana  * chunk btree.
39452bb2e00eSFilipe Manana  *
39462bb2e00eSFilipe Manana  * @trans:		A transaction handle.
39472bb2e00eSFilipe Manana  * @is_item_insertion:	Indicate if the modification is for inserting a new item
39482bb2e00eSFilipe Manana  *			in the chunk btree or if it's for the deletion or update
39492bb2e00eSFilipe Manana  *			of an existing item.
39502bb2e00eSFilipe Manana  *
39512bb2e00eSFilipe Manana  * This is used in a context where we need to update the chunk btree outside
39522bb2e00eSFilipe Manana  * block group allocation and removal, to avoid a deadlock with a concurrent
39532bb2e00eSFilipe Manana  * task that is allocating a metadata or data block group and therefore needs to
39542bb2e00eSFilipe Manana  * update the chunk btree while holding the chunk mutex. After the update to the
39552bb2e00eSFilipe Manana  * chunk btree is done, btrfs_trans_release_chunk_metadata() should be called.
39562bb2e00eSFilipe Manana  *
39572bb2e00eSFilipe Manana  */
39582bb2e00eSFilipe Manana void btrfs_reserve_chunk_metadata(struct btrfs_trans_handle *trans,
39592bb2e00eSFilipe Manana 				  bool is_item_insertion)
39602bb2e00eSFilipe Manana {
39612bb2e00eSFilipe Manana 	struct btrfs_fs_info *fs_info = trans->fs_info;
39622bb2e00eSFilipe Manana 	u64 bytes;
39632bb2e00eSFilipe Manana 
39642bb2e00eSFilipe Manana 	if (is_item_insertion)
39652bb2e00eSFilipe Manana 		bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
39662bb2e00eSFilipe Manana 	else
39672bb2e00eSFilipe Manana 		bytes = btrfs_calc_metadata_size(fs_info, 1);
39682bb2e00eSFilipe Manana 
39692bb2e00eSFilipe Manana 	mutex_lock(&fs_info->chunk_mutex);
39702bb2e00eSFilipe Manana 	reserve_chunk_space(trans, bytes, BTRFS_BLOCK_GROUP_SYSTEM);
39712bb2e00eSFilipe Manana 	mutex_unlock(&fs_info->chunk_mutex);
39722bb2e00eSFilipe Manana }
39732bb2e00eSFilipe Manana 
39743e43c279SJosef Bacik void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
39753e43c279SJosef Bacik {
397632da5386SDavid Sterba 	struct btrfs_block_group *block_group;
39773e43c279SJosef Bacik 
397850c31eaaSJosef Bacik 	block_group = btrfs_lookup_first_block_group(info, 0);
39793e43c279SJosef Bacik 	while (block_group) {
39803e43c279SJosef Bacik 		btrfs_wait_block_group_cache_done(block_group);
39813e43c279SJosef Bacik 		spin_lock(&block_group->lock);
398250c31eaaSJosef Bacik 		if (test_and_clear_bit(BLOCK_GROUP_FLAG_IREF,
398350c31eaaSJosef Bacik 				       &block_group->runtime_flags)) {
398450c31eaaSJosef Bacik 			struct inode *inode = block_group->inode;
39853e43c279SJosef Bacik 
39863e43c279SJosef Bacik 			block_group->inode = NULL;
39873e43c279SJosef Bacik 			spin_unlock(&block_group->lock);
398850c31eaaSJosef Bacik 
39893e43c279SJosef Bacik 			ASSERT(block_group->io_ctl.inode == NULL);
39903e43c279SJosef Bacik 			iput(inode);
399150c31eaaSJosef Bacik 		} else {
399250c31eaaSJosef Bacik 			spin_unlock(&block_group->lock);
399350c31eaaSJosef Bacik 		}
399450c31eaaSJosef Bacik 		block_group = btrfs_next_block_group(block_group);
39953e43c279SJosef Bacik 	}
39963e43c279SJosef Bacik }
39973e43c279SJosef Bacik 
39983e43c279SJosef Bacik /*
39993e43c279SJosef Bacik  * Must be called only after stopping all workers, since we could have block
40003e43c279SJosef Bacik  * group caching kthreads running, and therefore they could race with us if we
40013e43c279SJosef Bacik  * freed the block groups before stopping them.
40023e43c279SJosef Bacik  */
40033e43c279SJosef Bacik int btrfs_free_block_groups(struct btrfs_fs_info *info)
40043e43c279SJosef Bacik {
400532da5386SDavid Sterba 	struct btrfs_block_group *block_group;
40063e43c279SJosef Bacik 	struct btrfs_space_info *space_info;
40073e43c279SJosef Bacik 	struct btrfs_caching_control *caching_ctl;
40083e43c279SJosef Bacik 	struct rb_node *n;
40093e43c279SJosef Bacik 
401016b0c258SFilipe Manana 	write_lock(&info->block_group_cache_lock);
40113e43c279SJosef Bacik 	while (!list_empty(&info->caching_block_groups)) {
40123e43c279SJosef Bacik 		caching_ctl = list_entry(info->caching_block_groups.next,
40133e43c279SJosef Bacik 					 struct btrfs_caching_control, list);
40143e43c279SJosef Bacik 		list_del(&caching_ctl->list);
40153e43c279SJosef Bacik 		btrfs_put_caching_control(caching_ctl);
40163e43c279SJosef Bacik 	}
401716b0c258SFilipe Manana 	write_unlock(&info->block_group_cache_lock);
40183e43c279SJosef Bacik 
40193e43c279SJosef Bacik 	spin_lock(&info->unused_bgs_lock);
40203e43c279SJosef Bacik 	while (!list_empty(&info->unused_bgs)) {
40213e43c279SJosef Bacik 		block_group = list_first_entry(&info->unused_bgs,
402232da5386SDavid Sterba 					       struct btrfs_block_group,
40233e43c279SJosef Bacik 					       bg_list);
40243e43c279SJosef Bacik 		list_del_init(&block_group->bg_list);
40253e43c279SJosef Bacik 		btrfs_put_block_group(block_group);
40263e43c279SJosef Bacik 	}
40273e43c279SJosef Bacik 
402818bb8bbfSJohannes Thumshirn 	while (!list_empty(&info->reclaim_bgs)) {
402918bb8bbfSJohannes Thumshirn 		block_group = list_first_entry(&info->reclaim_bgs,
403018bb8bbfSJohannes Thumshirn 					       struct btrfs_block_group,
403118bb8bbfSJohannes Thumshirn 					       bg_list);
403218bb8bbfSJohannes Thumshirn 		list_del_init(&block_group->bg_list);
403318bb8bbfSJohannes Thumshirn 		btrfs_put_block_group(block_group);
403418bb8bbfSJohannes Thumshirn 	}
403518bb8bbfSJohannes Thumshirn 	spin_unlock(&info->unused_bgs_lock);
403618bb8bbfSJohannes Thumshirn 
4037afba2bc0SNaohiro Aota 	spin_lock(&info->zone_active_bgs_lock);
4038afba2bc0SNaohiro Aota 	while (!list_empty(&info->zone_active_bgs)) {
4039afba2bc0SNaohiro Aota 		block_group = list_first_entry(&info->zone_active_bgs,
4040afba2bc0SNaohiro Aota 					       struct btrfs_block_group,
4041afba2bc0SNaohiro Aota 					       active_bg_list);
4042afba2bc0SNaohiro Aota 		list_del_init(&block_group->active_bg_list);
4043afba2bc0SNaohiro Aota 		btrfs_put_block_group(block_group);
4044afba2bc0SNaohiro Aota 	}
4045afba2bc0SNaohiro Aota 	spin_unlock(&info->zone_active_bgs_lock);
4046afba2bc0SNaohiro Aota 
404716b0c258SFilipe Manana 	write_lock(&info->block_group_cache_lock);
404808dddb29SFilipe Manana 	while ((n = rb_last(&info->block_group_cache_tree.rb_root)) != NULL) {
404932da5386SDavid Sterba 		block_group = rb_entry(n, struct btrfs_block_group,
40503e43c279SJosef Bacik 				       cache_node);
405108dddb29SFilipe Manana 		rb_erase_cached(&block_group->cache_node,
40523e43c279SJosef Bacik 				&info->block_group_cache_tree);
40533e43c279SJosef Bacik 		RB_CLEAR_NODE(&block_group->cache_node);
405416b0c258SFilipe Manana 		write_unlock(&info->block_group_cache_lock);
40553e43c279SJosef Bacik 
40563e43c279SJosef Bacik 		down_write(&block_group->space_info->groups_sem);
40573e43c279SJosef Bacik 		list_del(&block_group->list);
40583e43c279SJosef Bacik 		up_write(&block_group->space_info->groups_sem);
40593e43c279SJosef Bacik 
40603e43c279SJosef Bacik 		/*
40613e43c279SJosef Bacik 		 * We haven't cached this block group, which means we could
40623e43c279SJosef Bacik 		 * possibly have excluded extents on this block group.
40633e43c279SJosef Bacik 		 */
40643e43c279SJosef Bacik 		if (block_group->cached == BTRFS_CACHE_NO ||
40653e43c279SJosef Bacik 		    block_group->cached == BTRFS_CACHE_ERROR)
40663e43c279SJosef Bacik 			btrfs_free_excluded_extents(block_group);
40673e43c279SJosef Bacik 
40683e43c279SJosef Bacik 		btrfs_remove_free_space_cache(block_group);
40693e43c279SJosef Bacik 		ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
40703e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->dirty_list));
40713e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->io_list));
40723e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->bg_list));
407348aaeebeSJosef Bacik 		ASSERT(refcount_read(&block_group->refs) == 1);
4074195a49eaSFilipe Manana 		ASSERT(block_group->swap_extents == 0);
40753e43c279SJosef Bacik 		btrfs_put_block_group(block_group);
40763e43c279SJosef Bacik 
407716b0c258SFilipe Manana 		write_lock(&info->block_group_cache_lock);
40783e43c279SJosef Bacik 	}
407916b0c258SFilipe Manana 	write_unlock(&info->block_group_cache_lock);
40803e43c279SJosef Bacik 
40813e43c279SJosef Bacik 	btrfs_release_global_block_rsv(info);
40823e43c279SJosef Bacik 
40833e43c279SJosef Bacik 	while (!list_empty(&info->space_info)) {
40843e43c279SJosef Bacik 		space_info = list_entry(info->space_info.next,
40853e43c279SJosef Bacik 					struct btrfs_space_info,
40863e43c279SJosef Bacik 					list);
40873e43c279SJosef Bacik 
40883e43c279SJosef Bacik 		/*
40893e43c279SJosef Bacik 		 * Do not hide this behind enospc_debug, this is actually
40903e43c279SJosef Bacik 		 * important and indicates a real bug if this happens.
40913e43c279SJosef Bacik 		 */
40923e43c279SJosef Bacik 		if (WARN_ON(space_info->bytes_pinned > 0 ||
40933e43c279SJosef Bacik 			    space_info->bytes_may_use > 0))
40943e43c279SJosef Bacik 			btrfs_dump_space_info(info, space_info, 0, 0);
409540cdc509SFilipe Manana 
409640cdc509SFilipe Manana 		/*
409740cdc509SFilipe Manana 		 * If there was a failure to cleanup a log tree, very likely due
409840cdc509SFilipe Manana 		 * to an IO failure on a writeback attempt of one or more of its
409940cdc509SFilipe Manana 		 * extent buffers, we could not do proper (and cheap) unaccounting
410040cdc509SFilipe Manana 		 * of their reserved space, so don't warn on bytes_reserved > 0 in
410140cdc509SFilipe Manana 		 * that case.
410240cdc509SFilipe Manana 		 */
410340cdc509SFilipe Manana 		if (!(space_info->flags & BTRFS_BLOCK_GROUP_METADATA) ||
410440cdc509SFilipe Manana 		    !BTRFS_FS_LOG_CLEANUP_ERROR(info)) {
410540cdc509SFilipe Manana 			if (WARN_ON(space_info->bytes_reserved > 0))
410640cdc509SFilipe Manana 				btrfs_dump_space_info(info, space_info, 0, 0);
410740cdc509SFilipe Manana 		}
410840cdc509SFilipe Manana 
4109d611add4SFilipe Manana 		WARN_ON(space_info->reclaim_size > 0);
41103e43c279SJosef Bacik 		list_del(&space_info->list);
41113e43c279SJosef Bacik 		btrfs_sysfs_remove_space_info(space_info);
41123e43c279SJosef Bacik 	}
41133e43c279SJosef Bacik 	return 0;
41143e43c279SJosef Bacik }
4115684b752bSFilipe Manana 
4116684b752bSFilipe Manana void btrfs_freeze_block_group(struct btrfs_block_group *cache)
4117684b752bSFilipe Manana {
4118684b752bSFilipe Manana 	atomic_inc(&cache->frozen);
4119684b752bSFilipe Manana }
4120684b752bSFilipe Manana 
4121684b752bSFilipe Manana void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
4122684b752bSFilipe Manana {
4123684b752bSFilipe Manana 	struct btrfs_fs_info *fs_info = block_group->fs_info;
4124684b752bSFilipe Manana 	struct extent_map_tree *em_tree;
4125684b752bSFilipe Manana 	struct extent_map *em;
4126684b752bSFilipe Manana 	bool cleanup;
4127684b752bSFilipe Manana 
4128684b752bSFilipe Manana 	spin_lock(&block_group->lock);
4129684b752bSFilipe Manana 	cleanup = (atomic_dec_and_test(&block_group->frozen) &&
41303349b57fSJosef Bacik 		   test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags));
4131684b752bSFilipe Manana 	spin_unlock(&block_group->lock);
4132684b752bSFilipe Manana 
4133684b752bSFilipe Manana 	if (cleanup) {
4134684b752bSFilipe Manana 		em_tree = &fs_info->mapping_tree;
4135684b752bSFilipe Manana 		write_lock(&em_tree->lock);
4136684b752bSFilipe Manana 		em = lookup_extent_mapping(em_tree, block_group->start,
4137684b752bSFilipe Manana 					   1);
4138684b752bSFilipe Manana 		BUG_ON(!em); /* logic error, can't happen */
4139684b752bSFilipe Manana 		remove_extent_mapping(em_tree, em);
4140684b752bSFilipe Manana 		write_unlock(&em_tree->lock);
4141684b752bSFilipe Manana 
4142684b752bSFilipe Manana 		/* once for us and once for the tree */
4143684b752bSFilipe Manana 		free_extent_map(em);
4144684b752bSFilipe Manana 		free_extent_map(em);
4145684b752bSFilipe Manana 
4146684b752bSFilipe Manana 		/*
4147684b752bSFilipe Manana 		 * We may have left one free space entry and other possible
4148684b752bSFilipe Manana 		 * tasks trimming this block group have left 1 entry each one.
4149684b752bSFilipe Manana 		 * Free them if any.
4150684b752bSFilipe Manana 		 */
4151fc80f7acSJosef Bacik 		btrfs_remove_free_space_cache(block_group);
4152684b752bSFilipe Manana 	}
4153684b752bSFilipe Manana }
4154195a49eaSFilipe Manana 
4155195a49eaSFilipe Manana bool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg)
4156195a49eaSFilipe Manana {
4157195a49eaSFilipe Manana 	bool ret = true;
4158195a49eaSFilipe Manana 
4159195a49eaSFilipe Manana 	spin_lock(&bg->lock);
4160195a49eaSFilipe Manana 	if (bg->ro)
4161195a49eaSFilipe Manana 		ret = false;
4162195a49eaSFilipe Manana 	else
4163195a49eaSFilipe Manana 		bg->swap_extents++;
4164195a49eaSFilipe Manana 	spin_unlock(&bg->lock);
4165195a49eaSFilipe Manana 
4166195a49eaSFilipe Manana 	return ret;
4167195a49eaSFilipe Manana }
4168195a49eaSFilipe Manana 
4169195a49eaSFilipe Manana void btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount)
4170195a49eaSFilipe Manana {
4171195a49eaSFilipe Manana 	spin_lock(&bg->lock);
4172195a49eaSFilipe Manana 	ASSERT(!bg->ro);
4173195a49eaSFilipe Manana 	ASSERT(bg->swap_extents >= amount);
4174195a49eaSFilipe Manana 	bg->swap_extents -= amount;
4175195a49eaSFilipe Manana 	spin_unlock(&bg->lock);
4176195a49eaSFilipe Manana }
4177