xref: /linux/fs/btrfs/block-group.c (revision e3ba67a108ff5a2657990014d3ed4488bd665be6)
12e405ad8SJosef Bacik // SPDX-License-Identifier: GPL-2.0
22e405ad8SJosef Bacik 
3784352feSDavid Sterba #include "misc.h"
42e405ad8SJosef Bacik #include "ctree.h"
52e405ad8SJosef Bacik #include "block-group.h"
63eeb3226SJosef Bacik #include "space-info.h"
79f21246dSJosef Bacik #include "disk-io.h"
89f21246dSJosef Bacik #include "free-space-cache.h"
99f21246dSJosef Bacik #include "free-space-tree.h"
10e3e0520bSJosef Bacik #include "volumes.h"
11e3e0520bSJosef Bacik #include "transaction.h"
12e3e0520bSJosef Bacik #include "ref-verify.h"
134358d963SJosef Bacik #include "sysfs.h"
144358d963SJosef Bacik #include "tree-log.h"
1577745c05SJosef Bacik #include "delalloc-space.h"
16b0643e59SDennis Zhou #include "discard.h"
1796a14336SNikolay Borisov #include "raid56.h"
182e405ad8SJosef Bacik 
19878d7b67SJosef Bacik /*
20878d7b67SJosef Bacik  * Return target flags in extended format or 0 if restripe for this chunk_type
21878d7b67SJosef Bacik  * is not in progress
22878d7b67SJosef Bacik  *
23878d7b67SJosef Bacik  * Should be called with balance_lock held
24878d7b67SJosef Bacik  */
25e11c0406SJosef Bacik static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
26878d7b67SJosef Bacik {
27878d7b67SJosef Bacik 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
28878d7b67SJosef Bacik 	u64 target = 0;
29878d7b67SJosef Bacik 
30878d7b67SJosef Bacik 	if (!bctl)
31878d7b67SJosef Bacik 		return 0;
32878d7b67SJosef Bacik 
33878d7b67SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA &&
34878d7b67SJosef Bacik 	    bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
35878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
36878d7b67SJosef Bacik 	} else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
37878d7b67SJosef Bacik 		   bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
38878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
39878d7b67SJosef Bacik 	} else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
40878d7b67SJosef Bacik 		   bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
41878d7b67SJosef Bacik 		target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
42878d7b67SJosef Bacik 	}
43878d7b67SJosef Bacik 
44878d7b67SJosef Bacik 	return target;
45878d7b67SJosef Bacik }
46878d7b67SJosef Bacik 
47878d7b67SJosef Bacik /*
48878d7b67SJosef Bacik  * @flags: available profiles in extended format (see ctree.h)
49878d7b67SJosef Bacik  *
50878d7b67SJosef Bacik  * Return reduced profile in chunk format.  If profile changing is in progress
51878d7b67SJosef Bacik  * (either running or paused) picks the target profile (if it's already
52878d7b67SJosef Bacik  * available), otherwise falls back to plain reducing.
53878d7b67SJosef Bacik  */
54878d7b67SJosef Bacik static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
55878d7b67SJosef Bacik {
56878d7b67SJosef Bacik 	u64 num_devices = fs_info->fs_devices->rw_devices;
57878d7b67SJosef Bacik 	u64 target;
58878d7b67SJosef Bacik 	u64 raid_type;
59878d7b67SJosef Bacik 	u64 allowed = 0;
60878d7b67SJosef Bacik 
61878d7b67SJosef Bacik 	/*
62878d7b67SJosef Bacik 	 * See if restripe for this chunk_type is in progress, if so try to
63878d7b67SJosef Bacik 	 * reduce to the target profile
64878d7b67SJosef Bacik 	 */
65878d7b67SJosef Bacik 	spin_lock(&fs_info->balance_lock);
66e11c0406SJosef Bacik 	target = get_restripe_target(fs_info, flags);
67878d7b67SJosef Bacik 	if (target) {
68878d7b67SJosef Bacik 		/* Pick target profile only if it's already available */
69878d7b67SJosef Bacik 		if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
70878d7b67SJosef Bacik 			spin_unlock(&fs_info->balance_lock);
71878d7b67SJosef Bacik 			return extended_to_chunk(target);
72878d7b67SJosef Bacik 		}
73878d7b67SJosef Bacik 	}
74878d7b67SJosef Bacik 	spin_unlock(&fs_info->balance_lock);
75878d7b67SJosef Bacik 
76878d7b67SJosef Bacik 	/* First, mask out the RAID levels which aren't possible */
77878d7b67SJosef Bacik 	for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
78878d7b67SJosef Bacik 		if (num_devices >= btrfs_raid_array[raid_type].devs_min)
79878d7b67SJosef Bacik 			allowed |= btrfs_raid_array[raid_type].bg_flag;
80878d7b67SJosef Bacik 	}
81878d7b67SJosef Bacik 	allowed &= flags;
82878d7b67SJosef Bacik 
83878d7b67SJosef Bacik 	if (allowed & BTRFS_BLOCK_GROUP_RAID6)
84878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID6;
85878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
86878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID5;
87878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
88878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID10;
89878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
90878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID1;
91878d7b67SJosef Bacik 	else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
92878d7b67SJosef Bacik 		allowed = BTRFS_BLOCK_GROUP_RAID0;
93878d7b67SJosef Bacik 
94878d7b67SJosef Bacik 	flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
95878d7b67SJosef Bacik 
96878d7b67SJosef Bacik 	return extended_to_chunk(flags | allowed);
97878d7b67SJosef Bacik }
98878d7b67SJosef Bacik 
99ef0a82daSJohannes Thumshirn u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
100878d7b67SJosef Bacik {
101878d7b67SJosef Bacik 	unsigned seq;
102878d7b67SJosef Bacik 	u64 flags;
103878d7b67SJosef Bacik 
104878d7b67SJosef Bacik 	do {
105878d7b67SJosef Bacik 		flags = orig_flags;
106878d7b67SJosef Bacik 		seq = read_seqbegin(&fs_info->profiles_lock);
107878d7b67SJosef Bacik 
108878d7b67SJosef Bacik 		if (flags & BTRFS_BLOCK_GROUP_DATA)
109878d7b67SJosef Bacik 			flags |= fs_info->avail_data_alloc_bits;
110878d7b67SJosef Bacik 		else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
111878d7b67SJosef Bacik 			flags |= fs_info->avail_system_alloc_bits;
112878d7b67SJosef Bacik 		else if (flags & BTRFS_BLOCK_GROUP_METADATA)
113878d7b67SJosef Bacik 			flags |= fs_info->avail_metadata_alloc_bits;
114878d7b67SJosef Bacik 	} while (read_seqretry(&fs_info->profiles_lock, seq));
115878d7b67SJosef Bacik 
116878d7b67SJosef Bacik 	return btrfs_reduce_alloc_profile(fs_info, flags);
117878d7b67SJosef Bacik }
118878d7b67SJosef Bacik 
11932da5386SDavid Sterba void btrfs_get_block_group(struct btrfs_block_group *cache)
1203cad1284SJosef Bacik {
1213cad1284SJosef Bacik 	atomic_inc(&cache->count);
1223cad1284SJosef Bacik }
1233cad1284SJosef Bacik 
12432da5386SDavid Sterba void btrfs_put_block_group(struct btrfs_block_group *cache)
1253cad1284SJosef Bacik {
1263cad1284SJosef Bacik 	if (atomic_dec_and_test(&cache->count)) {
1273cad1284SJosef Bacik 		WARN_ON(cache->pinned > 0);
1283cad1284SJosef Bacik 		WARN_ON(cache->reserved > 0);
1293cad1284SJosef Bacik 
1303cad1284SJosef Bacik 		/*
131b0643e59SDennis Zhou 		 * A block_group shouldn't be on the discard_list anymore.
132b0643e59SDennis Zhou 		 * Remove the block_group from the discard_list to prevent us
133b0643e59SDennis Zhou 		 * from causing a panic due to NULL pointer dereference.
134b0643e59SDennis Zhou 		 */
135b0643e59SDennis Zhou 		if (WARN_ON(!list_empty(&cache->discard_list)))
136b0643e59SDennis Zhou 			btrfs_discard_cancel_work(&cache->fs_info->discard_ctl,
137b0643e59SDennis Zhou 						  cache);
138b0643e59SDennis Zhou 
139b0643e59SDennis Zhou 		/*
1403cad1284SJosef Bacik 		 * If not empty, someone is still holding mutex of
1413cad1284SJosef Bacik 		 * full_stripe_lock, which can only be released by caller.
1423cad1284SJosef Bacik 		 * And it will definitely cause use-after-free when caller
1433cad1284SJosef Bacik 		 * tries to release full stripe lock.
1443cad1284SJosef Bacik 		 *
1453cad1284SJosef Bacik 		 * No better way to resolve, but only to warn.
1463cad1284SJosef Bacik 		 */
1473cad1284SJosef Bacik 		WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
1483cad1284SJosef Bacik 		kfree(cache->free_space_ctl);
1493cad1284SJosef Bacik 		kfree(cache);
1503cad1284SJosef Bacik 	}
1513cad1284SJosef Bacik }
1523cad1284SJosef Bacik 
1532e405ad8SJosef Bacik /*
1544358d963SJosef Bacik  * This adds the block group to the fs_info rb tree for the block group cache
1554358d963SJosef Bacik  */
1564358d963SJosef Bacik static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
15732da5386SDavid Sterba 				       struct btrfs_block_group *block_group)
1584358d963SJosef Bacik {
1594358d963SJosef Bacik 	struct rb_node **p;
1604358d963SJosef Bacik 	struct rb_node *parent = NULL;
16132da5386SDavid Sterba 	struct btrfs_block_group *cache;
1624358d963SJosef Bacik 
1639afc6649SQu Wenruo 	ASSERT(block_group->length != 0);
1649afc6649SQu Wenruo 
1654358d963SJosef Bacik 	spin_lock(&info->block_group_cache_lock);
1664358d963SJosef Bacik 	p = &info->block_group_cache_tree.rb_node;
1674358d963SJosef Bacik 
1684358d963SJosef Bacik 	while (*p) {
1694358d963SJosef Bacik 		parent = *p;
17032da5386SDavid Sterba 		cache = rb_entry(parent, struct btrfs_block_group, cache_node);
171b3470b5dSDavid Sterba 		if (block_group->start < cache->start) {
1724358d963SJosef Bacik 			p = &(*p)->rb_left;
173b3470b5dSDavid Sterba 		} else if (block_group->start > cache->start) {
1744358d963SJosef Bacik 			p = &(*p)->rb_right;
1754358d963SJosef Bacik 		} else {
1764358d963SJosef Bacik 			spin_unlock(&info->block_group_cache_lock);
1774358d963SJosef Bacik 			return -EEXIST;
1784358d963SJosef Bacik 		}
1794358d963SJosef Bacik 	}
1804358d963SJosef Bacik 
1814358d963SJosef Bacik 	rb_link_node(&block_group->cache_node, parent, p);
1824358d963SJosef Bacik 	rb_insert_color(&block_group->cache_node,
1834358d963SJosef Bacik 			&info->block_group_cache_tree);
1844358d963SJosef Bacik 
185b3470b5dSDavid Sterba 	if (info->first_logical_byte > block_group->start)
186b3470b5dSDavid Sterba 		info->first_logical_byte = block_group->start;
1874358d963SJosef Bacik 
1884358d963SJosef Bacik 	spin_unlock(&info->block_group_cache_lock);
1894358d963SJosef Bacik 
1904358d963SJosef Bacik 	return 0;
1914358d963SJosef Bacik }
1924358d963SJosef Bacik 
1934358d963SJosef Bacik /*
1942e405ad8SJosef Bacik  * This will return the block group at or after bytenr if contains is 0, else
1952e405ad8SJosef Bacik  * it will return the block group that contains the bytenr
1962e405ad8SJosef Bacik  */
19732da5386SDavid Sterba static struct btrfs_block_group *block_group_cache_tree_search(
1982e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr, int contains)
1992e405ad8SJosef Bacik {
20032da5386SDavid Sterba 	struct btrfs_block_group *cache, *ret = NULL;
2012e405ad8SJosef Bacik 	struct rb_node *n;
2022e405ad8SJosef Bacik 	u64 end, start;
2032e405ad8SJosef Bacik 
2042e405ad8SJosef Bacik 	spin_lock(&info->block_group_cache_lock);
2052e405ad8SJosef Bacik 	n = info->block_group_cache_tree.rb_node;
2062e405ad8SJosef Bacik 
2072e405ad8SJosef Bacik 	while (n) {
20832da5386SDavid Sterba 		cache = rb_entry(n, struct btrfs_block_group, cache_node);
209b3470b5dSDavid Sterba 		end = cache->start + cache->length - 1;
210b3470b5dSDavid Sterba 		start = cache->start;
2112e405ad8SJosef Bacik 
2122e405ad8SJosef Bacik 		if (bytenr < start) {
213b3470b5dSDavid Sterba 			if (!contains && (!ret || start < ret->start))
2142e405ad8SJosef Bacik 				ret = cache;
2152e405ad8SJosef Bacik 			n = n->rb_left;
2162e405ad8SJosef Bacik 		} else if (bytenr > start) {
2172e405ad8SJosef Bacik 			if (contains && bytenr <= end) {
2182e405ad8SJosef Bacik 				ret = cache;
2192e405ad8SJosef Bacik 				break;
2202e405ad8SJosef Bacik 			}
2212e405ad8SJosef Bacik 			n = n->rb_right;
2222e405ad8SJosef Bacik 		} else {
2232e405ad8SJosef Bacik 			ret = cache;
2242e405ad8SJosef Bacik 			break;
2252e405ad8SJosef Bacik 		}
2262e405ad8SJosef Bacik 	}
2272e405ad8SJosef Bacik 	if (ret) {
2282e405ad8SJosef Bacik 		btrfs_get_block_group(ret);
229b3470b5dSDavid Sterba 		if (bytenr == 0 && info->first_logical_byte > ret->start)
230b3470b5dSDavid Sterba 			info->first_logical_byte = ret->start;
2312e405ad8SJosef Bacik 	}
2322e405ad8SJosef Bacik 	spin_unlock(&info->block_group_cache_lock);
2332e405ad8SJosef Bacik 
2342e405ad8SJosef Bacik 	return ret;
2352e405ad8SJosef Bacik }
2362e405ad8SJosef Bacik 
2372e405ad8SJosef Bacik /*
2382e405ad8SJosef Bacik  * Return the block group that starts at or after bytenr
2392e405ad8SJosef Bacik  */
24032da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_first_block_group(
2412e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr)
2422e405ad8SJosef Bacik {
2432e405ad8SJosef Bacik 	return block_group_cache_tree_search(info, bytenr, 0);
2442e405ad8SJosef Bacik }
2452e405ad8SJosef Bacik 
2462e405ad8SJosef Bacik /*
2472e405ad8SJosef Bacik  * Return the block group that contains the given bytenr
2482e405ad8SJosef Bacik  */
24932da5386SDavid Sterba struct btrfs_block_group *btrfs_lookup_block_group(
2502e405ad8SJosef Bacik 		struct btrfs_fs_info *info, u64 bytenr)
2512e405ad8SJosef Bacik {
2522e405ad8SJosef Bacik 	return block_group_cache_tree_search(info, bytenr, 1);
2532e405ad8SJosef Bacik }
2542e405ad8SJosef Bacik 
25532da5386SDavid Sterba struct btrfs_block_group *btrfs_next_block_group(
25632da5386SDavid Sterba 		struct btrfs_block_group *cache)
2572e405ad8SJosef Bacik {
2582e405ad8SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
2592e405ad8SJosef Bacik 	struct rb_node *node;
2602e405ad8SJosef Bacik 
2612e405ad8SJosef Bacik 	spin_lock(&fs_info->block_group_cache_lock);
2622e405ad8SJosef Bacik 
2632e405ad8SJosef Bacik 	/* If our block group was removed, we need a full search. */
2642e405ad8SJosef Bacik 	if (RB_EMPTY_NODE(&cache->cache_node)) {
265b3470b5dSDavid Sterba 		const u64 next_bytenr = cache->start + cache->length;
2662e405ad8SJosef Bacik 
2672e405ad8SJosef Bacik 		spin_unlock(&fs_info->block_group_cache_lock);
2682e405ad8SJosef Bacik 		btrfs_put_block_group(cache);
2692e405ad8SJosef Bacik 		cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
2702e405ad8SJosef Bacik 	}
2712e405ad8SJosef Bacik 	node = rb_next(&cache->cache_node);
2722e405ad8SJosef Bacik 	btrfs_put_block_group(cache);
2732e405ad8SJosef Bacik 	if (node) {
27432da5386SDavid Sterba 		cache = rb_entry(node, struct btrfs_block_group, cache_node);
2752e405ad8SJosef Bacik 		btrfs_get_block_group(cache);
2762e405ad8SJosef Bacik 	} else
2772e405ad8SJosef Bacik 		cache = NULL;
2782e405ad8SJosef Bacik 	spin_unlock(&fs_info->block_group_cache_lock);
2792e405ad8SJosef Bacik 	return cache;
2802e405ad8SJosef Bacik }
2813eeb3226SJosef Bacik 
2823eeb3226SJosef Bacik bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
2833eeb3226SJosef Bacik {
28432da5386SDavid Sterba 	struct btrfs_block_group *bg;
2853eeb3226SJosef Bacik 	bool ret = true;
2863eeb3226SJosef Bacik 
2873eeb3226SJosef Bacik 	bg = btrfs_lookup_block_group(fs_info, bytenr);
2883eeb3226SJosef Bacik 	if (!bg)
2893eeb3226SJosef Bacik 		return false;
2903eeb3226SJosef Bacik 
2913eeb3226SJosef Bacik 	spin_lock(&bg->lock);
2923eeb3226SJosef Bacik 	if (bg->ro)
2933eeb3226SJosef Bacik 		ret = false;
2943eeb3226SJosef Bacik 	else
2953eeb3226SJosef Bacik 		atomic_inc(&bg->nocow_writers);
2963eeb3226SJosef Bacik 	spin_unlock(&bg->lock);
2973eeb3226SJosef Bacik 
2983eeb3226SJosef Bacik 	/* No put on block group, done by btrfs_dec_nocow_writers */
2993eeb3226SJosef Bacik 	if (!ret)
3003eeb3226SJosef Bacik 		btrfs_put_block_group(bg);
3013eeb3226SJosef Bacik 
3023eeb3226SJosef Bacik 	return ret;
3033eeb3226SJosef Bacik }
3043eeb3226SJosef Bacik 
3053eeb3226SJosef Bacik void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3063eeb3226SJosef Bacik {
30732da5386SDavid Sterba 	struct btrfs_block_group *bg;
3083eeb3226SJosef Bacik 
3093eeb3226SJosef Bacik 	bg = btrfs_lookup_block_group(fs_info, bytenr);
3103eeb3226SJosef Bacik 	ASSERT(bg);
3113eeb3226SJosef Bacik 	if (atomic_dec_and_test(&bg->nocow_writers))
3123eeb3226SJosef Bacik 		wake_up_var(&bg->nocow_writers);
3133eeb3226SJosef Bacik 	/*
3143eeb3226SJosef Bacik 	 * Once for our lookup and once for the lookup done by a previous call
3153eeb3226SJosef Bacik 	 * to btrfs_inc_nocow_writers()
3163eeb3226SJosef Bacik 	 */
3173eeb3226SJosef Bacik 	btrfs_put_block_group(bg);
3183eeb3226SJosef Bacik 	btrfs_put_block_group(bg);
3193eeb3226SJosef Bacik }
3203eeb3226SJosef Bacik 
32132da5386SDavid Sterba void btrfs_wait_nocow_writers(struct btrfs_block_group *bg)
3223eeb3226SJosef Bacik {
3233eeb3226SJosef Bacik 	wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
3243eeb3226SJosef Bacik }
3253eeb3226SJosef Bacik 
3263eeb3226SJosef Bacik void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
3273eeb3226SJosef Bacik 					const u64 start)
3283eeb3226SJosef Bacik {
32932da5386SDavid Sterba 	struct btrfs_block_group *bg;
3303eeb3226SJosef Bacik 
3313eeb3226SJosef Bacik 	bg = btrfs_lookup_block_group(fs_info, start);
3323eeb3226SJosef Bacik 	ASSERT(bg);
3333eeb3226SJosef Bacik 	if (atomic_dec_and_test(&bg->reservations))
3343eeb3226SJosef Bacik 		wake_up_var(&bg->reservations);
3353eeb3226SJosef Bacik 	btrfs_put_block_group(bg);
3363eeb3226SJosef Bacik }
3373eeb3226SJosef Bacik 
33832da5386SDavid Sterba void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg)
3393eeb3226SJosef Bacik {
3403eeb3226SJosef Bacik 	struct btrfs_space_info *space_info = bg->space_info;
3413eeb3226SJosef Bacik 
3423eeb3226SJosef Bacik 	ASSERT(bg->ro);
3433eeb3226SJosef Bacik 
3443eeb3226SJosef Bacik 	if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
3453eeb3226SJosef Bacik 		return;
3463eeb3226SJosef Bacik 
3473eeb3226SJosef Bacik 	/*
3483eeb3226SJosef Bacik 	 * Our block group is read only but before we set it to read only,
3493eeb3226SJosef Bacik 	 * some task might have had allocated an extent from it already, but it
3503eeb3226SJosef Bacik 	 * has not yet created a respective ordered extent (and added it to a
3513eeb3226SJosef Bacik 	 * root's list of ordered extents).
3523eeb3226SJosef Bacik 	 * Therefore wait for any task currently allocating extents, since the
3533eeb3226SJosef Bacik 	 * block group's reservations counter is incremented while a read lock
3543eeb3226SJosef Bacik 	 * on the groups' semaphore is held and decremented after releasing
3553eeb3226SJosef Bacik 	 * the read access on that semaphore and creating the ordered extent.
3563eeb3226SJosef Bacik 	 */
3573eeb3226SJosef Bacik 	down_write(&space_info->groups_sem);
3583eeb3226SJosef Bacik 	up_write(&space_info->groups_sem);
3593eeb3226SJosef Bacik 
3603eeb3226SJosef Bacik 	wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
3613eeb3226SJosef Bacik }
3629f21246dSJosef Bacik 
3639f21246dSJosef Bacik struct btrfs_caching_control *btrfs_get_caching_control(
36432da5386SDavid Sterba 		struct btrfs_block_group *cache)
3659f21246dSJosef Bacik {
3669f21246dSJosef Bacik 	struct btrfs_caching_control *ctl;
3679f21246dSJosef Bacik 
3689f21246dSJosef Bacik 	spin_lock(&cache->lock);
3699f21246dSJosef Bacik 	if (!cache->caching_ctl) {
3709f21246dSJosef Bacik 		spin_unlock(&cache->lock);
3719f21246dSJosef Bacik 		return NULL;
3729f21246dSJosef Bacik 	}
3739f21246dSJosef Bacik 
3749f21246dSJosef Bacik 	ctl = cache->caching_ctl;
3759f21246dSJosef Bacik 	refcount_inc(&ctl->count);
3769f21246dSJosef Bacik 	spin_unlock(&cache->lock);
3779f21246dSJosef Bacik 	return ctl;
3789f21246dSJosef Bacik }
3799f21246dSJosef Bacik 
3809f21246dSJosef Bacik void btrfs_put_caching_control(struct btrfs_caching_control *ctl)
3819f21246dSJosef Bacik {
3829f21246dSJosef Bacik 	if (refcount_dec_and_test(&ctl->count))
3839f21246dSJosef Bacik 		kfree(ctl);
3849f21246dSJosef Bacik }
3859f21246dSJosef Bacik 
3869f21246dSJosef Bacik /*
3879f21246dSJosef Bacik  * When we wait for progress in the block group caching, its because our
3889f21246dSJosef Bacik  * allocation attempt failed at least once.  So, we must sleep and let some
3899f21246dSJosef Bacik  * progress happen before we try again.
3909f21246dSJosef Bacik  *
3919f21246dSJosef Bacik  * This function will sleep at least once waiting for new free space to show
3929f21246dSJosef Bacik  * up, and then it will check the block group free space numbers for our min
3939f21246dSJosef Bacik  * num_bytes.  Another option is to have it go ahead and look in the rbtree for
3949f21246dSJosef Bacik  * a free extent of a given size, but this is a good start.
3959f21246dSJosef Bacik  *
3969f21246dSJosef Bacik  * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
3979f21246dSJosef Bacik  * any of the information in this block group.
3989f21246dSJosef Bacik  */
39932da5386SDavid Sterba void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache,
4009f21246dSJosef Bacik 					   u64 num_bytes)
4019f21246dSJosef Bacik {
4029f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
4039f21246dSJosef Bacik 
4049f21246dSJosef Bacik 	caching_ctl = btrfs_get_caching_control(cache);
4059f21246dSJosef Bacik 	if (!caching_ctl)
4069f21246dSJosef Bacik 		return;
4079f21246dSJosef Bacik 
40832da5386SDavid Sterba 	wait_event(caching_ctl->wait, btrfs_block_group_done(cache) ||
4099f21246dSJosef Bacik 		   (cache->free_space_ctl->free_space >= num_bytes));
4109f21246dSJosef Bacik 
4119f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
4129f21246dSJosef Bacik }
4139f21246dSJosef Bacik 
41432da5386SDavid Sterba int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache)
4159f21246dSJosef Bacik {
4169f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
4179f21246dSJosef Bacik 	int ret = 0;
4189f21246dSJosef Bacik 
4199f21246dSJosef Bacik 	caching_ctl = btrfs_get_caching_control(cache);
4209f21246dSJosef Bacik 	if (!caching_ctl)
4219f21246dSJosef Bacik 		return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
4229f21246dSJosef Bacik 
42332da5386SDavid Sterba 	wait_event(caching_ctl->wait, btrfs_block_group_done(cache));
4249f21246dSJosef Bacik 	if (cache->cached == BTRFS_CACHE_ERROR)
4259f21246dSJosef Bacik 		ret = -EIO;
4269f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
4279f21246dSJosef Bacik 	return ret;
4289f21246dSJosef Bacik }
4299f21246dSJosef Bacik 
4309f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
43132da5386SDavid Sterba static void fragment_free_space(struct btrfs_block_group *block_group)
4329f21246dSJosef Bacik {
4339f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
434b3470b5dSDavid Sterba 	u64 start = block_group->start;
435b3470b5dSDavid Sterba 	u64 len = block_group->length;
4369f21246dSJosef Bacik 	u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
4379f21246dSJosef Bacik 		fs_info->nodesize : fs_info->sectorsize;
4389f21246dSJosef Bacik 	u64 step = chunk << 1;
4399f21246dSJosef Bacik 
4409f21246dSJosef Bacik 	while (len > chunk) {
4419f21246dSJosef Bacik 		btrfs_remove_free_space(block_group, start, chunk);
4429f21246dSJosef Bacik 		start += step;
4439f21246dSJosef Bacik 		if (len < step)
4449f21246dSJosef Bacik 			len = 0;
4459f21246dSJosef Bacik 		else
4469f21246dSJosef Bacik 			len -= step;
4479f21246dSJosef Bacik 	}
4489f21246dSJosef Bacik }
4499f21246dSJosef Bacik #endif
4509f21246dSJosef Bacik 
4519f21246dSJosef Bacik /*
4529f21246dSJosef Bacik  * This is only called by btrfs_cache_block_group, since we could have freed
4539f21246dSJosef Bacik  * extents we need to check the pinned_extents for any extents that can't be
4549f21246dSJosef Bacik  * used yet since their free space will be released as soon as the transaction
4559f21246dSJosef Bacik  * commits.
4569f21246dSJosef Bacik  */
45732da5386SDavid Sterba u64 add_new_free_space(struct btrfs_block_group *block_group, u64 start, u64 end)
4589f21246dSJosef Bacik {
4599f21246dSJosef Bacik 	struct btrfs_fs_info *info = block_group->fs_info;
4609f21246dSJosef Bacik 	u64 extent_start, extent_end, size, total_added = 0;
4619f21246dSJosef Bacik 	int ret;
4629f21246dSJosef Bacik 
4639f21246dSJosef Bacik 	while (start < end) {
464fe119a6eSNikolay Borisov 		ret = find_first_extent_bit(&info->excluded_extents, start,
4659f21246dSJosef Bacik 					    &extent_start, &extent_end,
4669f21246dSJosef Bacik 					    EXTENT_DIRTY | EXTENT_UPTODATE,
4679f21246dSJosef Bacik 					    NULL);
4689f21246dSJosef Bacik 		if (ret)
4699f21246dSJosef Bacik 			break;
4709f21246dSJosef Bacik 
4719f21246dSJosef Bacik 		if (extent_start <= start) {
4729f21246dSJosef Bacik 			start = extent_end + 1;
4739f21246dSJosef Bacik 		} else if (extent_start > start && extent_start < end) {
4749f21246dSJosef Bacik 			size = extent_start - start;
4759f21246dSJosef Bacik 			total_added += size;
476b0643e59SDennis Zhou 			ret = btrfs_add_free_space_async_trimmed(block_group,
477b0643e59SDennis Zhou 								 start, size);
4789f21246dSJosef Bacik 			BUG_ON(ret); /* -ENOMEM or logic error */
4799f21246dSJosef Bacik 			start = extent_end + 1;
4809f21246dSJosef Bacik 		} else {
4819f21246dSJosef Bacik 			break;
4829f21246dSJosef Bacik 		}
4839f21246dSJosef Bacik 	}
4849f21246dSJosef Bacik 
4859f21246dSJosef Bacik 	if (start < end) {
4869f21246dSJosef Bacik 		size = end - start;
4879f21246dSJosef Bacik 		total_added += size;
488b0643e59SDennis Zhou 		ret = btrfs_add_free_space_async_trimmed(block_group, start,
489b0643e59SDennis Zhou 							 size);
4909f21246dSJosef Bacik 		BUG_ON(ret); /* -ENOMEM or logic error */
4919f21246dSJosef Bacik 	}
4929f21246dSJosef Bacik 
4939f21246dSJosef Bacik 	return total_added;
4949f21246dSJosef Bacik }
4959f21246dSJosef Bacik 
4969f21246dSJosef Bacik static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
4979f21246dSJosef Bacik {
49832da5386SDavid Sterba 	struct btrfs_block_group *block_group = caching_ctl->block_group;
4999f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
5009f21246dSJosef Bacik 	struct btrfs_root *extent_root = fs_info->extent_root;
5019f21246dSJosef Bacik 	struct btrfs_path *path;
5029f21246dSJosef Bacik 	struct extent_buffer *leaf;
5039f21246dSJosef Bacik 	struct btrfs_key key;
5049f21246dSJosef Bacik 	u64 total_found = 0;
5059f21246dSJosef Bacik 	u64 last = 0;
5069f21246dSJosef Bacik 	u32 nritems;
5079f21246dSJosef Bacik 	int ret;
5089f21246dSJosef Bacik 	bool wakeup = true;
5099f21246dSJosef Bacik 
5109f21246dSJosef Bacik 	path = btrfs_alloc_path();
5119f21246dSJosef Bacik 	if (!path)
5129f21246dSJosef Bacik 		return -ENOMEM;
5139f21246dSJosef Bacik 
514b3470b5dSDavid Sterba 	last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET);
5159f21246dSJosef Bacik 
5169f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
5179f21246dSJosef Bacik 	/*
5189f21246dSJosef Bacik 	 * If we're fragmenting we don't want to make anybody think we can
5199f21246dSJosef Bacik 	 * allocate from this block group until we've had a chance to fragment
5209f21246dSJosef Bacik 	 * the free space.
5219f21246dSJosef Bacik 	 */
5229f21246dSJosef Bacik 	if (btrfs_should_fragment_free_space(block_group))
5239f21246dSJosef Bacik 		wakeup = false;
5249f21246dSJosef Bacik #endif
5259f21246dSJosef Bacik 	/*
5269f21246dSJosef Bacik 	 * We don't want to deadlock with somebody trying to allocate a new
5279f21246dSJosef Bacik 	 * extent for the extent root while also trying to search the extent
5289f21246dSJosef Bacik 	 * root to add free space.  So we skip locking and search the commit
5299f21246dSJosef Bacik 	 * root, since its read-only
5309f21246dSJosef Bacik 	 */
5319f21246dSJosef Bacik 	path->skip_locking = 1;
5329f21246dSJosef Bacik 	path->search_commit_root = 1;
5339f21246dSJosef Bacik 	path->reada = READA_FORWARD;
5349f21246dSJosef Bacik 
5359f21246dSJosef Bacik 	key.objectid = last;
5369f21246dSJosef Bacik 	key.offset = 0;
5379f21246dSJosef Bacik 	key.type = BTRFS_EXTENT_ITEM_KEY;
5389f21246dSJosef Bacik 
5399f21246dSJosef Bacik next:
5409f21246dSJosef Bacik 	ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
5419f21246dSJosef Bacik 	if (ret < 0)
5429f21246dSJosef Bacik 		goto out;
5439f21246dSJosef Bacik 
5449f21246dSJosef Bacik 	leaf = path->nodes[0];
5459f21246dSJosef Bacik 	nritems = btrfs_header_nritems(leaf);
5469f21246dSJosef Bacik 
5479f21246dSJosef Bacik 	while (1) {
5489f21246dSJosef Bacik 		if (btrfs_fs_closing(fs_info) > 1) {
5499f21246dSJosef Bacik 			last = (u64)-1;
5509f21246dSJosef Bacik 			break;
5519f21246dSJosef Bacik 		}
5529f21246dSJosef Bacik 
5539f21246dSJosef Bacik 		if (path->slots[0] < nritems) {
5549f21246dSJosef Bacik 			btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5559f21246dSJosef Bacik 		} else {
5569f21246dSJosef Bacik 			ret = btrfs_find_next_key(extent_root, path, &key, 0, 0);
5579f21246dSJosef Bacik 			if (ret)
5589f21246dSJosef Bacik 				break;
5599f21246dSJosef Bacik 
5609f21246dSJosef Bacik 			if (need_resched() ||
5619f21246dSJosef Bacik 			    rwsem_is_contended(&fs_info->commit_root_sem)) {
5629f21246dSJosef Bacik 				if (wakeup)
5639f21246dSJosef Bacik 					caching_ctl->progress = last;
5649f21246dSJosef Bacik 				btrfs_release_path(path);
5659f21246dSJosef Bacik 				up_read(&fs_info->commit_root_sem);
5669f21246dSJosef Bacik 				mutex_unlock(&caching_ctl->mutex);
5679f21246dSJosef Bacik 				cond_resched();
5689f21246dSJosef Bacik 				mutex_lock(&caching_ctl->mutex);
5699f21246dSJosef Bacik 				down_read(&fs_info->commit_root_sem);
5709f21246dSJosef Bacik 				goto next;
5719f21246dSJosef Bacik 			}
5729f21246dSJosef Bacik 
5739f21246dSJosef Bacik 			ret = btrfs_next_leaf(extent_root, path);
5749f21246dSJosef Bacik 			if (ret < 0)
5759f21246dSJosef Bacik 				goto out;
5769f21246dSJosef Bacik 			if (ret)
5779f21246dSJosef Bacik 				break;
5789f21246dSJosef Bacik 			leaf = path->nodes[0];
5799f21246dSJosef Bacik 			nritems = btrfs_header_nritems(leaf);
5809f21246dSJosef Bacik 			continue;
5819f21246dSJosef Bacik 		}
5829f21246dSJosef Bacik 
5839f21246dSJosef Bacik 		if (key.objectid < last) {
5849f21246dSJosef Bacik 			key.objectid = last;
5859f21246dSJosef Bacik 			key.offset = 0;
5869f21246dSJosef Bacik 			key.type = BTRFS_EXTENT_ITEM_KEY;
5879f21246dSJosef Bacik 
5889f21246dSJosef Bacik 			if (wakeup)
5899f21246dSJosef Bacik 				caching_ctl->progress = last;
5909f21246dSJosef Bacik 			btrfs_release_path(path);
5919f21246dSJosef Bacik 			goto next;
5929f21246dSJosef Bacik 		}
5939f21246dSJosef Bacik 
594b3470b5dSDavid Sterba 		if (key.objectid < block_group->start) {
5959f21246dSJosef Bacik 			path->slots[0]++;
5969f21246dSJosef Bacik 			continue;
5979f21246dSJosef Bacik 		}
5989f21246dSJosef Bacik 
599b3470b5dSDavid Sterba 		if (key.objectid >= block_group->start + block_group->length)
6009f21246dSJosef Bacik 			break;
6019f21246dSJosef Bacik 
6029f21246dSJosef Bacik 		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
6039f21246dSJosef Bacik 		    key.type == BTRFS_METADATA_ITEM_KEY) {
6049f21246dSJosef Bacik 			total_found += add_new_free_space(block_group, last,
6059f21246dSJosef Bacik 							  key.objectid);
6069f21246dSJosef Bacik 			if (key.type == BTRFS_METADATA_ITEM_KEY)
6079f21246dSJosef Bacik 				last = key.objectid +
6089f21246dSJosef Bacik 					fs_info->nodesize;
6099f21246dSJosef Bacik 			else
6109f21246dSJosef Bacik 				last = key.objectid + key.offset;
6119f21246dSJosef Bacik 
6129f21246dSJosef Bacik 			if (total_found > CACHING_CTL_WAKE_UP) {
6139f21246dSJosef Bacik 				total_found = 0;
6149f21246dSJosef Bacik 				if (wakeup)
6159f21246dSJosef Bacik 					wake_up(&caching_ctl->wait);
6169f21246dSJosef Bacik 			}
6179f21246dSJosef Bacik 		}
6189f21246dSJosef Bacik 		path->slots[0]++;
6199f21246dSJosef Bacik 	}
6209f21246dSJosef Bacik 	ret = 0;
6219f21246dSJosef Bacik 
6229f21246dSJosef Bacik 	total_found += add_new_free_space(block_group, last,
623b3470b5dSDavid Sterba 				block_group->start + block_group->length);
6249f21246dSJosef Bacik 	caching_ctl->progress = (u64)-1;
6259f21246dSJosef Bacik 
6269f21246dSJosef Bacik out:
6279f21246dSJosef Bacik 	btrfs_free_path(path);
6289f21246dSJosef Bacik 	return ret;
6299f21246dSJosef Bacik }
6309f21246dSJosef Bacik 
6319f21246dSJosef Bacik static noinline void caching_thread(struct btrfs_work *work)
6329f21246dSJosef Bacik {
63332da5386SDavid Sterba 	struct btrfs_block_group *block_group;
6349f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info;
6359f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
6369f21246dSJosef Bacik 	int ret;
6379f21246dSJosef Bacik 
6389f21246dSJosef Bacik 	caching_ctl = container_of(work, struct btrfs_caching_control, work);
6399f21246dSJosef Bacik 	block_group = caching_ctl->block_group;
6409f21246dSJosef Bacik 	fs_info = block_group->fs_info;
6419f21246dSJosef Bacik 
6429f21246dSJosef Bacik 	mutex_lock(&caching_ctl->mutex);
6439f21246dSJosef Bacik 	down_read(&fs_info->commit_root_sem);
6449f21246dSJosef Bacik 
6459f21246dSJosef Bacik 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
6469f21246dSJosef Bacik 		ret = load_free_space_tree(caching_ctl);
6479f21246dSJosef Bacik 	else
6489f21246dSJosef Bacik 		ret = load_extent_tree_free(caching_ctl);
6499f21246dSJosef Bacik 
6509f21246dSJosef Bacik 	spin_lock(&block_group->lock);
6519f21246dSJosef Bacik 	block_group->caching_ctl = NULL;
6529f21246dSJosef Bacik 	block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
6539f21246dSJosef Bacik 	spin_unlock(&block_group->lock);
6549f21246dSJosef Bacik 
6559f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
6569f21246dSJosef Bacik 	if (btrfs_should_fragment_free_space(block_group)) {
6579f21246dSJosef Bacik 		u64 bytes_used;
6589f21246dSJosef Bacik 
6599f21246dSJosef Bacik 		spin_lock(&block_group->space_info->lock);
6609f21246dSJosef Bacik 		spin_lock(&block_group->lock);
661b3470b5dSDavid Sterba 		bytes_used = block_group->length - block_group->used;
6629f21246dSJosef Bacik 		block_group->space_info->bytes_used += bytes_used >> 1;
6639f21246dSJosef Bacik 		spin_unlock(&block_group->lock);
6649f21246dSJosef Bacik 		spin_unlock(&block_group->space_info->lock);
665e11c0406SJosef Bacik 		fragment_free_space(block_group);
6669f21246dSJosef Bacik 	}
6679f21246dSJosef Bacik #endif
6689f21246dSJosef Bacik 
6699f21246dSJosef Bacik 	caching_ctl->progress = (u64)-1;
6709f21246dSJosef Bacik 
6719f21246dSJosef Bacik 	up_read(&fs_info->commit_root_sem);
6729f21246dSJosef Bacik 	btrfs_free_excluded_extents(block_group);
6739f21246dSJosef Bacik 	mutex_unlock(&caching_ctl->mutex);
6749f21246dSJosef Bacik 
6759f21246dSJosef Bacik 	wake_up(&caching_ctl->wait);
6769f21246dSJosef Bacik 
6779f21246dSJosef Bacik 	btrfs_put_caching_control(caching_ctl);
6789f21246dSJosef Bacik 	btrfs_put_block_group(block_group);
6799f21246dSJosef Bacik }
6809f21246dSJosef Bacik 
68132da5386SDavid Sterba int btrfs_cache_block_group(struct btrfs_block_group *cache, int load_cache_only)
6829f21246dSJosef Bacik {
6839f21246dSJosef Bacik 	DEFINE_WAIT(wait);
6849f21246dSJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
6859f21246dSJosef Bacik 	struct btrfs_caching_control *caching_ctl;
6869f21246dSJosef Bacik 	int ret = 0;
6879f21246dSJosef Bacik 
6889f21246dSJosef Bacik 	caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
6899f21246dSJosef Bacik 	if (!caching_ctl)
6909f21246dSJosef Bacik 		return -ENOMEM;
6919f21246dSJosef Bacik 
6929f21246dSJosef Bacik 	INIT_LIST_HEAD(&caching_ctl->list);
6939f21246dSJosef Bacik 	mutex_init(&caching_ctl->mutex);
6949f21246dSJosef Bacik 	init_waitqueue_head(&caching_ctl->wait);
6959f21246dSJosef Bacik 	caching_ctl->block_group = cache;
696b3470b5dSDavid Sterba 	caching_ctl->progress = cache->start;
6979f21246dSJosef Bacik 	refcount_set(&caching_ctl->count, 1);
698a0cac0ecSOmar Sandoval 	btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
6999f21246dSJosef Bacik 
7009f21246dSJosef Bacik 	spin_lock(&cache->lock);
7019f21246dSJosef Bacik 	/*
7029f21246dSJosef Bacik 	 * This should be a rare occasion, but this could happen I think in the
7039f21246dSJosef Bacik 	 * case where one thread starts to load the space cache info, and then
7049f21246dSJosef Bacik 	 * some other thread starts a transaction commit which tries to do an
7059f21246dSJosef Bacik 	 * allocation while the other thread is still loading the space cache
7069f21246dSJosef Bacik 	 * info.  The previous loop should have kept us from choosing this block
7079f21246dSJosef Bacik 	 * group, but if we've moved to the state where we will wait on caching
7089f21246dSJosef Bacik 	 * block groups we need to first check if we're doing a fast load here,
7099f21246dSJosef Bacik 	 * so we can wait for it to finish, otherwise we could end up allocating
7109f21246dSJosef Bacik 	 * from a block group who's cache gets evicted for one reason or
7119f21246dSJosef Bacik 	 * another.
7129f21246dSJosef Bacik 	 */
7139f21246dSJosef Bacik 	while (cache->cached == BTRFS_CACHE_FAST) {
7149f21246dSJosef Bacik 		struct btrfs_caching_control *ctl;
7159f21246dSJosef Bacik 
7169f21246dSJosef Bacik 		ctl = cache->caching_ctl;
7179f21246dSJosef Bacik 		refcount_inc(&ctl->count);
7189f21246dSJosef Bacik 		prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
7199f21246dSJosef Bacik 		spin_unlock(&cache->lock);
7209f21246dSJosef Bacik 
7219f21246dSJosef Bacik 		schedule();
7229f21246dSJosef Bacik 
7239f21246dSJosef Bacik 		finish_wait(&ctl->wait, &wait);
7249f21246dSJosef Bacik 		btrfs_put_caching_control(ctl);
7259f21246dSJosef Bacik 		spin_lock(&cache->lock);
7269f21246dSJosef Bacik 	}
7279f21246dSJosef Bacik 
7289f21246dSJosef Bacik 	if (cache->cached != BTRFS_CACHE_NO) {
7299f21246dSJosef Bacik 		spin_unlock(&cache->lock);
7309f21246dSJosef Bacik 		kfree(caching_ctl);
7319f21246dSJosef Bacik 		return 0;
7329f21246dSJosef Bacik 	}
7339f21246dSJosef Bacik 	WARN_ON(cache->caching_ctl);
7349f21246dSJosef Bacik 	cache->caching_ctl = caching_ctl;
7359f21246dSJosef Bacik 	cache->cached = BTRFS_CACHE_FAST;
7369f21246dSJosef Bacik 	spin_unlock(&cache->lock);
7379f21246dSJosef Bacik 
7389f21246dSJosef Bacik 	if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
7399f21246dSJosef Bacik 		mutex_lock(&caching_ctl->mutex);
7409f21246dSJosef Bacik 		ret = load_free_space_cache(cache);
7419f21246dSJosef Bacik 
7429f21246dSJosef Bacik 		spin_lock(&cache->lock);
7439f21246dSJosef Bacik 		if (ret == 1) {
7449f21246dSJosef Bacik 			cache->caching_ctl = NULL;
7459f21246dSJosef Bacik 			cache->cached = BTRFS_CACHE_FINISHED;
7469f21246dSJosef Bacik 			cache->last_byte_to_unpin = (u64)-1;
7479f21246dSJosef Bacik 			caching_ctl->progress = (u64)-1;
7489f21246dSJosef Bacik 		} else {
7499f21246dSJosef Bacik 			if (load_cache_only) {
7509f21246dSJosef Bacik 				cache->caching_ctl = NULL;
7519f21246dSJosef Bacik 				cache->cached = BTRFS_CACHE_NO;
7529f21246dSJosef Bacik 			} else {
7539f21246dSJosef Bacik 				cache->cached = BTRFS_CACHE_STARTED;
7549f21246dSJosef Bacik 				cache->has_caching_ctl = 1;
7559f21246dSJosef Bacik 			}
7569f21246dSJosef Bacik 		}
7579f21246dSJosef Bacik 		spin_unlock(&cache->lock);
7589f21246dSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
7599f21246dSJosef Bacik 		if (ret == 1 &&
7609f21246dSJosef Bacik 		    btrfs_should_fragment_free_space(cache)) {
7619f21246dSJosef Bacik 			u64 bytes_used;
7629f21246dSJosef Bacik 
7639f21246dSJosef Bacik 			spin_lock(&cache->space_info->lock);
7649f21246dSJosef Bacik 			spin_lock(&cache->lock);
765b3470b5dSDavid Sterba 			bytes_used = cache->length - cache->used;
7669f21246dSJosef Bacik 			cache->space_info->bytes_used += bytes_used >> 1;
7679f21246dSJosef Bacik 			spin_unlock(&cache->lock);
7689f21246dSJosef Bacik 			spin_unlock(&cache->space_info->lock);
769e11c0406SJosef Bacik 			fragment_free_space(cache);
7709f21246dSJosef Bacik 		}
7719f21246dSJosef Bacik #endif
7729f21246dSJosef Bacik 		mutex_unlock(&caching_ctl->mutex);
7739f21246dSJosef Bacik 
7749f21246dSJosef Bacik 		wake_up(&caching_ctl->wait);
7759f21246dSJosef Bacik 		if (ret == 1) {
7769f21246dSJosef Bacik 			btrfs_put_caching_control(caching_ctl);
7779f21246dSJosef Bacik 			btrfs_free_excluded_extents(cache);
7789f21246dSJosef Bacik 			return 0;
7799f21246dSJosef Bacik 		}
7809f21246dSJosef Bacik 	} else {
7819f21246dSJosef Bacik 		/*
7829f21246dSJosef Bacik 		 * We're either using the free space tree or no caching at all.
7839f21246dSJosef Bacik 		 * Set cached to the appropriate value and wakeup any waiters.
7849f21246dSJosef Bacik 		 */
7859f21246dSJosef Bacik 		spin_lock(&cache->lock);
7869f21246dSJosef Bacik 		if (load_cache_only) {
7879f21246dSJosef Bacik 			cache->caching_ctl = NULL;
7889f21246dSJosef Bacik 			cache->cached = BTRFS_CACHE_NO;
7899f21246dSJosef Bacik 		} else {
7909f21246dSJosef Bacik 			cache->cached = BTRFS_CACHE_STARTED;
7919f21246dSJosef Bacik 			cache->has_caching_ctl = 1;
7929f21246dSJosef Bacik 		}
7939f21246dSJosef Bacik 		spin_unlock(&cache->lock);
7949f21246dSJosef Bacik 		wake_up(&caching_ctl->wait);
7959f21246dSJosef Bacik 	}
7969f21246dSJosef Bacik 
7979f21246dSJosef Bacik 	if (load_cache_only) {
7989f21246dSJosef Bacik 		btrfs_put_caching_control(caching_ctl);
7999f21246dSJosef Bacik 		return 0;
8009f21246dSJosef Bacik 	}
8019f21246dSJosef Bacik 
8029f21246dSJosef Bacik 	down_write(&fs_info->commit_root_sem);
8039f21246dSJosef Bacik 	refcount_inc(&caching_ctl->count);
8049f21246dSJosef Bacik 	list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
8059f21246dSJosef Bacik 	up_write(&fs_info->commit_root_sem);
8069f21246dSJosef Bacik 
8079f21246dSJosef Bacik 	btrfs_get_block_group(cache);
8089f21246dSJosef Bacik 
8099f21246dSJosef Bacik 	btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
8109f21246dSJosef Bacik 
8119f21246dSJosef Bacik 	return ret;
8129f21246dSJosef Bacik }
813e3e0520bSJosef Bacik 
814e3e0520bSJosef Bacik static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
815e3e0520bSJosef Bacik {
816e3e0520bSJosef Bacik 	u64 extra_flags = chunk_to_extended(flags) &
817e3e0520bSJosef Bacik 				BTRFS_EXTENDED_PROFILE_MASK;
818e3e0520bSJosef Bacik 
819e3e0520bSJosef Bacik 	write_seqlock(&fs_info->profiles_lock);
820e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA)
821e3e0520bSJosef Bacik 		fs_info->avail_data_alloc_bits &= ~extra_flags;
822e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_METADATA)
823e3e0520bSJosef Bacik 		fs_info->avail_metadata_alloc_bits &= ~extra_flags;
824e3e0520bSJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
825e3e0520bSJosef Bacik 		fs_info->avail_system_alloc_bits &= ~extra_flags;
826e3e0520bSJosef Bacik 	write_sequnlock(&fs_info->profiles_lock);
827e3e0520bSJosef Bacik }
828e3e0520bSJosef Bacik 
829e3e0520bSJosef Bacik /*
830e3e0520bSJosef Bacik  * Clear incompat bits for the following feature(s):
831e3e0520bSJosef Bacik  *
832e3e0520bSJosef Bacik  * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
833e3e0520bSJosef Bacik  *            in the whole filesystem
8349c907446SDavid Sterba  *
8359c907446SDavid Sterba  * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
836e3e0520bSJosef Bacik  */
837e3e0520bSJosef Bacik static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags)
838e3e0520bSJosef Bacik {
8399c907446SDavid Sterba 	bool found_raid56 = false;
8409c907446SDavid Sterba 	bool found_raid1c34 = false;
8419c907446SDavid Sterba 
8429c907446SDavid Sterba 	if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) ||
8439c907446SDavid Sterba 	    (flags & BTRFS_BLOCK_GROUP_RAID1C3) ||
8449c907446SDavid Sterba 	    (flags & BTRFS_BLOCK_GROUP_RAID1C4)) {
845e3e0520bSJosef Bacik 		struct list_head *head = &fs_info->space_info;
846e3e0520bSJosef Bacik 		struct btrfs_space_info *sinfo;
847e3e0520bSJosef Bacik 
848e3e0520bSJosef Bacik 		list_for_each_entry_rcu(sinfo, head, list) {
849e3e0520bSJosef Bacik 			down_read(&sinfo->groups_sem);
850e3e0520bSJosef Bacik 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5]))
8519c907446SDavid Sterba 				found_raid56 = true;
852e3e0520bSJosef Bacik 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6]))
8539c907446SDavid Sterba 				found_raid56 = true;
8549c907446SDavid Sterba 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3]))
8559c907446SDavid Sterba 				found_raid1c34 = true;
8569c907446SDavid Sterba 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4]))
8579c907446SDavid Sterba 				found_raid1c34 = true;
858e3e0520bSJosef Bacik 			up_read(&sinfo->groups_sem);
859e3e0520bSJosef Bacik 		}
860d8e6fd5cSFilipe Manana 		if (!found_raid56)
861e3e0520bSJosef Bacik 			btrfs_clear_fs_incompat(fs_info, RAID56);
862d8e6fd5cSFilipe Manana 		if (!found_raid1c34)
8639c907446SDavid Sterba 			btrfs_clear_fs_incompat(fs_info, RAID1C34);
864e3e0520bSJosef Bacik 	}
865e3e0520bSJosef Bacik }
866e3e0520bSJosef Bacik 
8677357623aSQu Wenruo static int remove_block_group_item(struct btrfs_trans_handle *trans,
8687357623aSQu Wenruo 				   struct btrfs_path *path,
8697357623aSQu Wenruo 				   struct btrfs_block_group *block_group)
8707357623aSQu Wenruo {
8717357623aSQu Wenruo 	struct btrfs_fs_info *fs_info = trans->fs_info;
8727357623aSQu Wenruo 	struct btrfs_root *root;
8737357623aSQu Wenruo 	struct btrfs_key key;
8747357623aSQu Wenruo 	int ret;
8757357623aSQu Wenruo 
8767357623aSQu Wenruo 	root = fs_info->extent_root;
8777357623aSQu Wenruo 	key.objectid = block_group->start;
8787357623aSQu Wenruo 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8797357623aSQu Wenruo 	key.offset = block_group->length;
8807357623aSQu Wenruo 
8817357623aSQu Wenruo 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8827357623aSQu Wenruo 	if (ret > 0)
8837357623aSQu Wenruo 		ret = -ENOENT;
8847357623aSQu Wenruo 	if (ret < 0)
8857357623aSQu Wenruo 		return ret;
8867357623aSQu Wenruo 
8877357623aSQu Wenruo 	ret = btrfs_del_item(trans, root, path);
8887357623aSQu Wenruo 	return ret;
8897357623aSQu Wenruo }
8907357623aSQu Wenruo 
891e3e0520bSJosef Bacik int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
892e3e0520bSJosef Bacik 			     u64 group_start, struct extent_map *em)
893e3e0520bSJosef Bacik {
894e3e0520bSJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
895e3e0520bSJosef Bacik 	struct btrfs_path *path;
89632da5386SDavid Sterba 	struct btrfs_block_group *block_group;
897e3e0520bSJosef Bacik 	struct btrfs_free_cluster *cluster;
898e3e0520bSJosef Bacik 	struct btrfs_root *tree_root = fs_info->tree_root;
899e3e0520bSJosef Bacik 	struct btrfs_key key;
900e3e0520bSJosef Bacik 	struct inode *inode;
901e3e0520bSJosef Bacik 	struct kobject *kobj = NULL;
902e3e0520bSJosef Bacik 	int ret;
903e3e0520bSJosef Bacik 	int index;
904e3e0520bSJosef Bacik 	int factor;
905e3e0520bSJosef Bacik 	struct btrfs_caching_control *caching_ctl = NULL;
906e3e0520bSJosef Bacik 	bool remove_em;
907e3e0520bSJosef Bacik 	bool remove_rsv = false;
908e3e0520bSJosef Bacik 
909e3e0520bSJosef Bacik 	block_group = btrfs_lookup_block_group(fs_info, group_start);
910e3e0520bSJosef Bacik 	BUG_ON(!block_group);
911e3e0520bSJosef Bacik 	BUG_ON(!block_group->ro);
912e3e0520bSJosef Bacik 
913e3e0520bSJosef Bacik 	trace_btrfs_remove_block_group(block_group);
914e3e0520bSJosef Bacik 	/*
915e3e0520bSJosef Bacik 	 * Free the reserved super bytes from this block group before
916e3e0520bSJosef Bacik 	 * remove it.
917e3e0520bSJosef Bacik 	 */
918e3e0520bSJosef Bacik 	btrfs_free_excluded_extents(block_group);
919b3470b5dSDavid Sterba 	btrfs_free_ref_tree_range(fs_info, block_group->start,
920b3470b5dSDavid Sterba 				  block_group->length);
921e3e0520bSJosef Bacik 
922e3e0520bSJosef Bacik 	index = btrfs_bg_flags_to_raid_index(block_group->flags);
923e3e0520bSJosef Bacik 	factor = btrfs_bg_type_to_factor(block_group->flags);
924e3e0520bSJosef Bacik 
925e3e0520bSJosef Bacik 	/* make sure this block group isn't part of an allocation cluster */
926e3e0520bSJosef Bacik 	cluster = &fs_info->data_alloc_cluster;
927e3e0520bSJosef Bacik 	spin_lock(&cluster->refill_lock);
928e3e0520bSJosef Bacik 	btrfs_return_cluster_to_free_space(block_group, cluster);
929e3e0520bSJosef Bacik 	spin_unlock(&cluster->refill_lock);
930e3e0520bSJosef Bacik 
931e3e0520bSJosef Bacik 	/*
932e3e0520bSJosef Bacik 	 * make sure this block group isn't part of a metadata
933e3e0520bSJosef Bacik 	 * allocation cluster
934e3e0520bSJosef Bacik 	 */
935e3e0520bSJosef Bacik 	cluster = &fs_info->meta_alloc_cluster;
936e3e0520bSJosef Bacik 	spin_lock(&cluster->refill_lock);
937e3e0520bSJosef Bacik 	btrfs_return_cluster_to_free_space(block_group, cluster);
938e3e0520bSJosef Bacik 	spin_unlock(&cluster->refill_lock);
939e3e0520bSJosef Bacik 
940e3e0520bSJosef Bacik 	path = btrfs_alloc_path();
941e3e0520bSJosef Bacik 	if (!path) {
942e3e0520bSJosef Bacik 		ret = -ENOMEM;
9439fecd132SFilipe Manana 		goto out;
944e3e0520bSJosef Bacik 	}
945e3e0520bSJosef Bacik 
946e3e0520bSJosef Bacik 	/*
947e3e0520bSJosef Bacik 	 * get the inode first so any iput calls done for the io_list
948e3e0520bSJosef Bacik 	 * aren't the final iput (no unlinks allowed now)
949e3e0520bSJosef Bacik 	 */
950e3e0520bSJosef Bacik 	inode = lookup_free_space_inode(block_group, path);
951e3e0520bSJosef Bacik 
952e3e0520bSJosef Bacik 	mutex_lock(&trans->transaction->cache_write_mutex);
953e3e0520bSJosef Bacik 	/*
954e3e0520bSJosef Bacik 	 * Make sure our free space cache IO is done before removing the
955e3e0520bSJosef Bacik 	 * free space inode
956e3e0520bSJosef Bacik 	 */
957e3e0520bSJosef Bacik 	spin_lock(&trans->transaction->dirty_bgs_lock);
958e3e0520bSJosef Bacik 	if (!list_empty(&block_group->io_list)) {
959e3e0520bSJosef Bacik 		list_del_init(&block_group->io_list);
960e3e0520bSJosef Bacik 
961e3e0520bSJosef Bacik 		WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
962e3e0520bSJosef Bacik 
963e3e0520bSJosef Bacik 		spin_unlock(&trans->transaction->dirty_bgs_lock);
964e3e0520bSJosef Bacik 		btrfs_wait_cache_io(trans, block_group, path);
965e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
966e3e0520bSJosef Bacik 		spin_lock(&trans->transaction->dirty_bgs_lock);
967e3e0520bSJosef Bacik 	}
968e3e0520bSJosef Bacik 
969e3e0520bSJosef Bacik 	if (!list_empty(&block_group->dirty_list)) {
970e3e0520bSJosef Bacik 		list_del_init(&block_group->dirty_list);
971e3e0520bSJosef Bacik 		remove_rsv = true;
972e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
973e3e0520bSJosef Bacik 	}
974e3e0520bSJosef Bacik 	spin_unlock(&trans->transaction->dirty_bgs_lock);
975e3e0520bSJosef Bacik 	mutex_unlock(&trans->transaction->cache_write_mutex);
976e3e0520bSJosef Bacik 
977e3e0520bSJosef Bacik 	if (!IS_ERR(inode)) {
978e3e0520bSJosef Bacik 		ret = btrfs_orphan_add(trans, BTRFS_I(inode));
979e3e0520bSJosef Bacik 		if (ret) {
980e3e0520bSJosef Bacik 			btrfs_add_delayed_iput(inode);
9819fecd132SFilipe Manana 			goto out;
982e3e0520bSJosef Bacik 		}
983e3e0520bSJosef Bacik 		clear_nlink(inode);
984e3e0520bSJosef Bacik 		/* One for the block groups ref */
985e3e0520bSJosef Bacik 		spin_lock(&block_group->lock);
986e3e0520bSJosef Bacik 		if (block_group->iref) {
987e3e0520bSJosef Bacik 			block_group->iref = 0;
988e3e0520bSJosef Bacik 			block_group->inode = NULL;
989e3e0520bSJosef Bacik 			spin_unlock(&block_group->lock);
990e3e0520bSJosef Bacik 			iput(inode);
991e3e0520bSJosef Bacik 		} else {
992e3e0520bSJosef Bacik 			spin_unlock(&block_group->lock);
993e3e0520bSJosef Bacik 		}
994e3e0520bSJosef Bacik 		/* One for our lookup ref */
995e3e0520bSJosef Bacik 		btrfs_add_delayed_iput(inode);
996e3e0520bSJosef Bacik 	}
997e3e0520bSJosef Bacik 
998e3e0520bSJosef Bacik 	key.objectid = BTRFS_FREE_SPACE_OBJECTID;
999e3e0520bSJosef Bacik 	key.type = 0;
1000b3470b5dSDavid Sterba 	key.offset = block_group->start;
1001e3e0520bSJosef Bacik 
1002e3e0520bSJosef Bacik 	ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
1003e3e0520bSJosef Bacik 	if (ret < 0)
10049fecd132SFilipe Manana 		goto out;
1005e3e0520bSJosef Bacik 	if (ret > 0)
1006e3e0520bSJosef Bacik 		btrfs_release_path(path);
1007e3e0520bSJosef Bacik 	if (ret == 0) {
1008e3e0520bSJosef Bacik 		ret = btrfs_del_item(trans, tree_root, path);
1009e3e0520bSJosef Bacik 		if (ret)
10109fecd132SFilipe Manana 			goto out;
1011e3e0520bSJosef Bacik 		btrfs_release_path(path);
1012e3e0520bSJosef Bacik 	}
1013e3e0520bSJosef Bacik 
1014e3e0520bSJosef Bacik 	spin_lock(&fs_info->block_group_cache_lock);
1015e3e0520bSJosef Bacik 	rb_erase(&block_group->cache_node,
1016e3e0520bSJosef Bacik 		 &fs_info->block_group_cache_tree);
1017e3e0520bSJosef Bacik 	RB_CLEAR_NODE(&block_group->cache_node);
1018e3e0520bSJosef Bacik 
10199fecd132SFilipe Manana 	/* Once for the block groups rbtree */
10209fecd132SFilipe Manana 	btrfs_put_block_group(block_group);
10219fecd132SFilipe Manana 
1022b3470b5dSDavid Sterba 	if (fs_info->first_logical_byte == block_group->start)
1023e3e0520bSJosef Bacik 		fs_info->first_logical_byte = (u64)-1;
1024e3e0520bSJosef Bacik 	spin_unlock(&fs_info->block_group_cache_lock);
1025e3e0520bSJosef Bacik 
1026e3e0520bSJosef Bacik 	down_write(&block_group->space_info->groups_sem);
1027e3e0520bSJosef Bacik 	/*
1028e3e0520bSJosef Bacik 	 * we must use list_del_init so people can check to see if they
1029e3e0520bSJosef Bacik 	 * are still on the list after taking the semaphore
1030e3e0520bSJosef Bacik 	 */
1031e3e0520bSJosef Bacik 	list_del_init(&block_group->list);
1032e3e0520bSJosef Bacik 	if (list_empty(&block_group->space_info->block_groups[index])) {
1033e3e0520bSJosef Bacik 		kobj = block_group->space_info->block_group_kobjs[index];
1034e3e0520bSJosef Bacik 		block_group->space_info->block_group_kobjs[index] = NULL;
1035e3e0520bSJosef Bacik 		clear_avail_alloc_bits(fs_info, block_group->flags);
1036e3e0520bSJosef Bacik 	}
1037e3e0520bSJosef Bacik 	up_write(&block_group->space_info->groups_sem);
1038e3e0520bSJosef Bacik 	clear_incompat_bg_bits(fs_info, block_group->flags);
1039e3e0520bSJosef Bacik 	if (kobj) {
1040e3e0520bSJosef Bacik 		kobject_del(kobj);
1041e3e0520bSJosef Bacik 		kobject_put(kobj);
1042e3e0520bSJosef Bacik 	}
1043e3e0520bSJosef Bacik 
1044e3e0520bSJosef Bacik 	if (block_group->has_caching_ctl)
1045e3e0520bSJosef Bacik 		caching_ctl = btrfs_get_caching_control(block_group);
1046e3e0520bSJosef Bacik 	if (block_group->cached == BTRFS_CACHE_STARTED)
1047e3e0520bSJosef Bacik 		btrfs_wait_block_group_cache_done(block_group);
1048e3e0520bSJosef Bacik 	if (block_group->has_caching_ctl) {
1049e3e0520bSJosef Bacik 		down_write(&fs_info->commit_root_sem);
1050e3e0520bSJosef Bacik 		if (!caching_ctl) {
1051e3e0520bSJosef Bacik 			struct btrfs_caching_control *ctl;
1052e3e0520bSJosef Bacik 
1053e3e0520bSJosef Bacik 			list_for_each_entry(ctl,
1054e3e0520bSJosef Bacik 				    &fs_info->caching_block_groups, list)
1055e3e0520bSJosef Bacik 				if (ctl->block_group == block_group) {
1056e3e0520bSJosef Bacik 					caching_ctl = ctl;
1057e3e0520bSJosef Bacik 					refcount_inc(&caching_ctl->count);
1058e3e0520bSJosef Bacik 					break;
1059e3e0520bSJosef Bacik 				}
1060e3e0520bSJosef Bacik 		}
1061e3e0520bSJosef Bacik 		if (caching_ctl)
1062e3e0520bSJosef Bacik 			list_del_init(&caching_ctl->list);
1063e3e0520bSJosef Bacik 		up_write(&fs_info->commit_root_sem);
1064e3e0520bSJosef Bacik 		if (caching_ctl) {
1065e3e0520bSJosef Bacik 			/* Once for the caching bgs list and once for us. */
1066e3e0520bSJosef Bacik 			btrfs_put_caching_control(caching_ctl);
1067e3e0520bSJosef Bacik 			btrfs_put_caching_control(caching_ctl);
1068e3e0520bSJosef Bacik 		}
1069e3e0520bSJosef Bacik 	}
1070e3e0520bSJosef Bacik 
1071e3e0520bSJosef Bacik 	spin_lock(&trans->transaction->dirty_bgs_lock);
1072e3e0520bSJosef Bacik 	WARN_ON(!list_empty(&block_group->dirty_list));
1073e3e0520bSJosef Bacik 	WARN_ON(!list_empty(&block_group->io_list));
1074e3e0520bSJosef Bacik 	spin_unlock(&trans->transaction->dirty_bgs_lock);
1075e3e0520bSJosef Bacik 
1076e3e0520bSJosef Bacik 	btrfs_remove_free_space_cache(block_group);
1077e3e0520bSJosef Bacik 
1078e3e0520bSJosef Bacik 	spin_lock(&block_group->space_info->lock);
1079e3e0520bSJosef Bacik 	list_del_init(&block_group->ro_list);
1080e3e0520bSJosef Bacik 
1081e3e0520bSJosef Bacik 	if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
1082e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->total_bytes
1083b3470b5dSDavid Sterba 			< block_group->length);
1084e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->bytes_readonly
1085b3470b5dSDavid Sterba 			< block_group->length);
1086e3e0520bSJosef Bacik 		WARN_ON(block_group->space_info->disk_total
1087b3470b5dSDavid Sterba 			< block_group->length * factor);
1088e3e0520bSJosef Bacik 	}
1089b3470b5dSDavid Sterba 	block_group->space_info->total_bytes -= block_group->length;
1090b3470b5dSDavid Sterba 	block_group->space_info->bytes_readonly -= block_group->length;
1091b3470b5dSDavid Sterba 	block_group->space_info->disk_total -= block_group->length * factor;
1092e3e0520bSJosef Bacik 
1093e3e0520bSJosef Bacik 	spin_unlock(&block_group->space_info->lock);
1094e3e0520bSJosef Bacik 
1095ffcb9d44SFilipe Manana 	/*
1096ffcb9d44SFilipe Manana 	 * Remove the free space for the block group from the free space tree
1097ffcb9d44SFilipe Manana 	 * and the block group's item from the extent tree before marking the
1098ffcb9d44SFilipe Manana 	 * block group as removed. This is to prevent races with tasks that
1099ffcb9d44SFilipe Manana 	 * freeze and unfreeze a block group, this task and another task
1100ffcb9d44SFilipe Manana 	 * allocating a new block group - the unfreeze task ends up removing
1101ffcb9d44SFilipe Manana 	 * the block group's extent map before the task calling this function
1102ffcb9d44SFilipe Manana 	 * deletes the block group item from the extent tree, allowing for
1103ffcb9d44SFilipe Manana 	 * another task to attempt to create another block group with the same
1104ffcb9d44SFilipe Manana 	 * item key (and failing with -EEXIST and a transaction abort).
1105ffcb9d44SFilipe Manana 	 */
1106ffcb9d44SFilipe Manana 	ret = remove_block_group_free_space(trans, block_group);
1107ffcb9d44SFilipe Manana 	if (ret)
1108ffcb9d44SFilipe Manana 		goto out;
1109ffcb9d44SFilipe Manana 
1110ffcb9d44SFilipe Manana 	ret = remove_block_group_item(trans, path, block_group);
1111ffcb9d44SFilipe Manana 	if (ret < 0)
1112ffcb9d44SFilipe Manana 		goto out;
1113ffcb9d44SFilipe Manana 
1114e3e0520bSJosef Bacik 	mutex_lock(&fs_info->chunk_mutex);
1115e3e0520bSJosef Bacik 	spin_lock(&block_group->lock);
1116e3e0520bSJosef Bacik 	block_group->removed = 1;
1117e3e0520bSJosef Bacik 	/*
11186b7304afSFilipe Manana 	 * At this point trimming or scrub can't start on this block group,
11196b7304afSFilipe Manana 	 * because we removed the block group from the rbtree
11206b7304afSFilipe Manana 	 * fs_info->block_group_cache_tree so no one can't find it anymore and
11216b7304afSFilipe Manana 	 * even if someone already got this block group before we removed it
11226b7304afSFilipe Manana 	 * from the rbtree, they have already incremented block_group->frozen -
11236b7304afSFilipe Manana 	 * if they didn't, for the trimming case they won't find any free space
11246b7304afSFilipe Manana 	 * entries because we already removed them all when we called
11256b7304afSFilipe Manana 	 * btrfs_remove_free_space_cache().
1126e3e0520bSJosef Bacik 	 *
1127e3e0520bSJosef Bacik 	 * And we must not remove the extent map from the fs_info->mapping_tree
1128e3e0520bSJosef Bacik 	 * to prevent the same logical address range and physical device space
11296b7304afSFilipe Manana 	 * ranges from being reused for a new block group. This is needed to
11306b7304afSFilipe Manana 	 * avoid races with trimming and scrub.
11316b7304afSFilipe Manana 	 *
11326b7304afSFilipe Manana 	 * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
1133e3e0520bSJosef Bacik 	 * completely transactionless, so while it is trimming a range the
1134e3e0520bSJosef Bacik 	 * currently running transaction might finish and a new one start,
1135e3e0520bSJosef Bacik 	 * allowing for new block groups to be created that can reuse the same
1136e3e0520bSJosef Bacik 	 * physical device locations unless we take this special care.
1137e3e0520bSJosef Bacik 	 *
1138e3e0520bSJosef Bacik 	 * There may also be an implicit trim operation if the file system
1139e3e0520bSJosef Bacik 	 * is mounted with -odiscard. The same protections must remain
1140e3e0520bSJosef Bacik 	 * in place until the extents have been discarded completely when
1141e3e0520bSJosef Bacik 	 * the transaction commit has completed.
1142e3e0520bSJosef Bacik 	 */
11436b7304afSFilipe Manana 	remove_em = (atomic_read(&block_group->frozen) == 0);
1144e3e0520bSJosef Bacik 	spin_unlock(&block_group->lock);
1145e3e0520bSJosef Bacik 
1146e3e0520bSJosef Bacik 	mutex_unlock(&fs_info->chunk_mutex);
1147e3e0520bSJosef Bacik 
1148e3e0520bSJosef Bacik 	if (remove_em) {
1149e3e0520bSJosef Bacik 		struct extent_map_tree *em_tree;
1150e3e0520bSJosef Bacik 
1151e3e0520bSJosef Bacik 		em_tree = &fs_info->mapping_tree;
1152e3e0520bSJosef Bacik 		write_lock(&em_tree->lock);
1153e3e0520bSJosef Bacik 		remove_extent_mapping(em_tree, em);
1154e3e0520bSJosef Bacik 		write_unlock(&em_tree->lock);
1155e3e0520bSJosef Bacik 		/* once for the tree */
1156e3e0520bSJosef Bacik 		free_extent_map(em);
1157e3e0520bSJosef Bacik 	}
1158f6033c5eSXiyu Yang 
11599fecd132SFilipe Manana out:
1160f6033c5eSXiyu Yang 	/* Once for the lookup reference */
1161f6033c5eSXiyu Yang 	btrfs_put_block_group(block_group);
1162e3e0520bSJosef Bacik 	if (remove_rsv)
1163e3e0520bSJosef Bacik 		btrfs_delayed_refs_rsv_release(fs_info, 1);
1164e3e0520bSJosef Bacik 	btrfs_free_path(path);
1165e3e0520bSJosef Bacik 	return ret;
1166e3e0520bSJosef Bacik }
1167e3e0520bSJosef Bacik 
1168e3e0520bSJosef Bacik struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
1169e3e0520bSJosef Bacik 		struct btrfs_fs_info *fs_info, const u64 chunk_offset)
1170e3e0520bSJosef Bacik {
1171e3e0520bSJosef Bacik 	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1172e3e0520bSJosef Bacik 	struct extent_map *em;
1173e3e0520bSJosef Bacik 	struct map_lookup *map;
1174e3e0520bSJosef Bacik 	unsigned int num_items;
1175e3e0520bSJosef Bacik 
1176e3e0520bSJosef Bacik 	read_lock(&em_tree->lock);
1177e3e0520bSJosef Bacik 	em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1178e3e0520bSJosef Bacik 	read_unlock(&em_tree->lock);
1179e3e0520bSJosef Bacik 	ASSERT(em && em->start == chunk_offset);
1180e3e0520bSJosef Bacik 
1181e3e0520bSJosef Bacik 	/*
1182e3e0520bSJosef Bacik 	 * We need to reserve 3 + N units from the metadata space info in order
1183e3e0520bSJosef Bacik 	 * to remove a block group (done at btrfs_remove_chunk() and at
1184e3e0520bSJosef Bacik 	 * btrfs_remove_block_group()), which are used for:
1185e3e0520bSJosef Bacik 	 *
1186e3e0520bSJosef Bacik 	 * 1 unit for adding the free space inode's orphan (located in the tree
1187e3e0520bSJosef Bacik 	 * of tree roots).
1188e3e0520bSJosef Bacik 	 * 1 unit for deleting the block group item (located in the extent
1189e3e0520bSJosef Bacik 	 * tree).
1190e3e0520bSJosef Bacik 	 * 1 unit for deleting the free space item (located in tree of tree
1191e3e0520bSJosef Bacik 	 * roots).
1192e3e0520bSJosef Bacik 	 * N units for deleting N device extent items corresponding to each
1193e3e0520bSJosef Bacik 	 * stripe (located in the device tree).
1194e3e0520bSJosef Bacik 	 *
1195e3e0520bSJosef Bacik 	 * In order to remove a block group we also need to reserve units in the
1196e3e0520bSJosef Bacik 	 * system space info in order to update the chunk tree (update one or
1197e3e0520bSJosef Bacik 	 * more device items and remove one chunk item), but this is done at
1198e3e0520bSJosef Bacik 	 * btrfs_remove_chunk() through a call to check_system_chunk().
1199e3e0520bSJosef Bacik 	 */
1200e3e0520bSJosef Bacik 	map = em->map_lookup;
1201e3e0520bSJosef Bacik 	num_items = 3 + map->num_stripes;
1202e3e0520bSJosef Bacik 	free_extent_map(em);
1203e3e0520bSJosef Bacik 
1204e3e0520bSJosef Bacik 	return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
12057f9fe614SJosef Bacik 							   num_items);
1206e3e0520bSJosef Bacik }
1207e3e0520bSJosef Bacik 
1208e3e0520bSJosef Bacik /*
120926ce2095SJosef Bacik  * Mark block group @cache read-only, so later write won't happen to block
121026ce2095SJosef Bacik  * group @cache.
121126ce2095SJosef Bacik  *
121226ce2095SJosef Bacik  * If @force is not set, this function will only mark the block group readonly
121326ce2095SJosef Bacik  * if we have enough free space (1M) in other metadata/system block groups.
121426ce2095SJosef Bacik  * If @force is not set, this function will mark the block group readonly
121526ce2095SJosef Bacik  * without checking free space.
121626ce2095SJosef Bacik  *
121726ce2095SJosef Bacik  * NOTE: This function doesn't care if other block groups can contain all the
121826ce2095SJosef Bacik  * data in this block group. That check should be done by relocation routine,
121926ce2095SJosef Bacik  * not this function.
122026ce2095SJosef Bacik  */
122132da5386SDavid Sterba static int inc_block_group_ro(struct btrfs_block_group *cache, int force)
122226ce2095SJosef Bacik {
122326ce2095SJosef Bacik 	struct btrfs_space_info *sinfo = cache->space_info;
122426ce2095SJosef Bacik 	u64 num_bytes;
122526ce2095SJosef Bacik 	int ret = -ENOSPC;
122626ce2095SJosef Bacik 
122726ce2095SJosef Bacik 	spin_lock(&sinfo->lock);
122826ce2095SJosef Bacik 	spin_lock(&cache->lock);
122926ce2095SJosef Bacik 
123026ce2095SJosef Bacik 	if (cache->ro) {
123126ce2095SJosef Bacik 		cache->ro++;
123226ce2095SJosef Bacik 		ret = 0;
123326ce2095SJosef Bacik 		goto out;
123426ce2095SJosef Bacik 	}
123526ce2095SJosef Bacik 
1236b3470b5dSDavid Sterba 	num_bytes = cache->length - cache->reserved - cache->pinned -
1237bf38be65SDavid Sterba 		    cache->bytes_super - cache->used;
123826ce2095SJosef Bacik 
123926ce2095SJosef Bacik 	/*
1240a30a3d20SJosef Bacik 	 * Data never overcommits, even in mixed mode, so do just the straight
1241a30a3d20SJosef Bacik 	 * check of left over space in how much we have allocated.
1242a30a3d20SJosef Bacik 	 */
1243a30a3d20SJosef Bacik 	if (force) {
1244a30a3d20SJosef Bacik 		ret = 0;
1245a30a3d20SJosef Bacik 	} else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) {
1246a30a3d20SJosef Bacik 		u64 sinfo_used = btrfs_space_info_used(sinfo, true);
1247a30a3d20SJosef Bacik 
1248a30a3d20SJosef Bacik 		/*
124926ce2095SJosef Bacik 		 * Here we make sure if we mark this bg RO, we still have enough
1250f8935566SJosef Bacik 		 * free space as buffer.
125126ce2095SJosef Bacik 		 */
1252a30a3d20SJosef Bacik 		if (sinfo_used + num_bytes <= sinfo->total_bytes)
1253a30a3d20SJosef Bacik 			ret = 0;
1254a30a3d20SJosef Bacik 	} else {
1255a30a3d20SJosef Bacik 		/*
1256a30a3d20SJosef Bacik 		 * We overcommit metadata, so we need to do the
1257a30a3d20SJosef Bacik 		 * btrfs_can_overcommit check here, and we need to pass in
1258a30a3d20SJosef Bacik 		 * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of
1259a30a3d20SJosef Bacik 		 * leeway to allow us to mark this block group as read only.
1260a30a3d20SJosef Bacik 		 */
1261a30a3d20SJosef Bacik 		if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes,
1262a30a3d20SJosef Bacik 					 BTRFS_RESERVE_NO_FLUSH))
1263a30a3d20SJosef Bacik 			ret = 0;
1264a30a3d20SJosef Bacik 	}
1265a30a3d20SJosef Bacik 
1266a30a3d20SJosef Bacik 	if (!ret) {
126726ce2095SJosef Bacik 		sinfo->bytes_readonly += num_bytes;
126826ce2095SJosef Bacik 		cache->ro++;
126926ce2095SJosef Bacik 		list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
127026ce2095SJosef Bacik 	}
127126ce2095SJosef Bacik out:
127226ce2095SJosef Bacik 	spin_unlock(&cache->lock);
127326ce2095SJosef Bacik 	spin_unlock(&sinfo->lock);
127426ce2095SJosef Bacik 	if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) {
127526ce2095SJosef Bacik 		btrfs_info(cache->fs_info,
1276b3470b5dSDavid Sterba 			"unable to make block group %llu ro", cache->start);
127726ce2095SJosef Bacik 		btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0);
127826ce2095SJosef Bacik 	}
127926ce2095SJosef Bacik 	return ret;
128026ce2095SJosef Bacik }
128126ce2095SJosef Bacik 
1282fe119a6eSNikolay Borisov static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
1283fe119a6eSNikolay Borisov 				 struct btrfs_block_group *bg)
128445bb5d6aSNikolay Borisov {
128545bb5d6aSNikolay Borisov 	struct btrfs_fs_info *fs_info = bg->fs_info;
1286fe119a6eSNikolay Borisov 	struct btrfs_transaction *prev_trans = NULL;
128745bb5d6aSNikolay Borisov 	const u64 start = bg->start;
128845bb5d6aSNikolay Borisov 	const u64 end = start + bg->length - 1;
128945bb5d6aSNikolay Borisov 	int ret;
129045bb5d6aSNikolay Borisov 
1291fe119a6eSNikolay Borisov 	spin_lock(&fs_info->trans_lock);
1292fe119a6eSNikolay Borisov 	if (trans->transaction->list.prev != &fs_info->trans_list) {
1293fe119a6eSNikolay Borisov 		prev_trans = list_last_entry(&trans->transaction->list,
1294fe119a6eSNikolay Borisov 					     struct btrfs_transaction, list);
1295fe119a6eSNikolay Borisov 		refcount_inc(&prev_trans->use_count);
1296fe119a6eSNikolay Borisov 	}
1297fe119a6eSNikolay Borisov 	spin_unlock(&fs_info->trans_lock);
1298fe119a6eSNikolay Borisov 
129945bb5d6aSNikolay Borisov 	/*
130045bb5d6aSNikolay Borisov 	 * Hold the unused_bg_unpin_mutex lock to avoid racing with
130145bb5d6aSNikolay Borisov 	 * btrfs_finish_extent_commit(). If we are at transaction N, another
130245bb5d6aSNikolay Borisov 	 * task might be running finish_extent_commit() for the previous
130345bb5d6aSNikolay Borisov 	 * transaction N - 1, and have seen a range belonging to the block
1304fe119a6eSNikolay Borisov 	 * group in pinned_extents before we were able to clear the whole block
1305fe119a6eSNikolay Borisov 	 * group range from pinned_extents. This means that task can lookup for
1306fe119a6eSNikolay Borisov 	 * the block group after we unpinned it from pinned_extents and removed
1307fe119a6eSNikolay Borisov 	 * it, leading to a BUG_ON() at unpin_extent_range().
130845bb5d6aSNikolay Borisov 	 */
130945bb5d6aSNikolay Borisov 	mutex_lock(&fs_info->unused_bg_unpin_mutex);
1310fe119a6eSNikolay Borisov 	if (prev_trans) {
1311fe119a6eSNikolay Borisov 		ret = clear_extent_bits(&prev_trans->pinned_extents, start, end,
131245bb5d6aSNikolay Borisov 					EXTENT_DIRTY);
131345bb5d6aSNikolay Borisov 		if (ret)
1314534cf531SFilipe Manana 			goto out;
1315fe119a6eSNikolay Borisov 	}
131645bb5d6aSNikolay Borisov 
1317fe119a6eSNikolay Borisov 	ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end,
131845bb5d6aSNikolay Borisov 				EXTENT_DIRTY);
1319534cf531SFilipe Manana out:
132045bb5d6aSNikolay Borisov 	mutex_unlock(&fs_info->unused_bg_unpin_mutex);
13215150bf19SFilipe Manana 	if (prev_trans)
13225150bf19SFilipe Manana 		btrfs_put_transaction(prev_trans);
132345bb5d6aSNikolay Borisov 
1324534cf531SFilipe Manana 	return ret == 0;
132545bb5d6aSNikolay Borisov }
132645bb5d6aSNikolay Borisov 
132726ce2095SJosef Bacik /*
1328e3e0520bSJosef Bacik  * Process the unused_bgs list and remove any that don't have any allocated
1329e3e0520bSJosef Bacik  * space inside of them.
1330e3e0520bSJosef Bacik  */
1331e3e0520bSJosef Bacik void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
1332e3e0520bSJosef Bacik {
133332da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1334e3e0520bSJosef Bacik 	struct btrfs_space_info *space_info;
1335e3e0520bSJosef Bacik 	struct btrfs_trans_handle *trans;
13366e80d4f8SDennis Zhou 	const bool async_trim_enabled = btrfs_test_opt(fs_info, DISCARD_ASYNC);
1337e3e0520bSJosef Bacik 	int ret = 0;
1338e3e0520bSJosef Bacik 
1339e3e0520bSJosef Bacik 	if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1340e3e0520bSJosef Bacik 		return;
1341e3e0520bSJosef Bacik 
1342e3e0520bSJosef Bacik 	spin_lock(&fs_info->unused_bgs_lock);
1343e3e0520bSJosef Bacik 	while (!list_empty(&fs_info->unused_bgs)) {
1344e3e0520bSJosef Bacik 		int trimming;
1345e3e0520bSJosef Bacik 
1346e3e0520bSJosef Bacik 		block_group = list_first_entry(&fs_info->unused_bgs,
134732da5386SDavid Sterba 					       struct btrfs_block_group,
1348e3e0520bSJosef Bacik 					       bg_list);
1349e3e0520bSJosef Bacik 		list_del_init(&block_group->bg_list);
1350e3e0520bSJosef Bacik 
1351e3e0520bSJosef Bacik 		space_info = block_group->space_info;
1352e3e0520bSJosef Bacik 
1353e3e0520bSJosef Bacik 		if (ret || btrfs_mixed_space_info(space_info)) {
1354e3e0520bSJosef Bacik 			btrfs_put_block_group(block_group);
1355e3e0520bSJosef Bacik 			continue;
1356e3e0520bSJosef Bacik 		}
1357e3e0520bSJosef Bacik 		spin_unlock(&fs_info->unused_bgs_lock);
1358e3e0520bSJosef Bacik 
1359b0643e59SDennis Zhou 		btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
1360b0643e59SDennis Zhou 
1361e3e0520bSJosef Bacik 		mutex_lock(&fs_info->delete_unused_bgs_mutex);
1362e3e0520bSJosef Bacik 
1363e3e0520bSJosef Bacik 		/* Don't want to race with allocators so take the groups_sem */
1364e3e0520bSJosef Bacik 		down_write(&space_info->groups_sem);
13656e80d4f8SDennis Zhou 
13666e80d4f8SDennis Zhou 		/*
13676e80d4f8SDennis Zhou 		 * Async discard moves the final block group discard to be prior
13686e80d4f8SDennis Zhou 		 * to the unused_bgs code path.  Therefore, if it's not fully
13696e80d4f8SDennis Zhou 		 * trimmed, punt it back to the async discard lists.
13706e80d4f8SDennis Zhou 		 */
13716e80d4f8SDennis Zhou 		if (btrfs_test_opt(fs_info, DISCARD_ASYNC) &&
13726e80d4f8SDennis Zhou 		    !btrfs_is_free_space_trimmed(block_group)) {
13736e80d4f8SDennis Zhou 			trace_btrfs_skip_unused_block_group(block_group);
13746e80d4f8SDennis Zhou 			up_write(&space_info->groups_sem);
13756e80d4f8SDennis Zhou 			/* Requeue if we failed because of async discard */
13766e80d4f8SDennis Zhou 			btrfs_discard_queue_work(&fs_info->discard_ctl,
13776e80d4f8SDennis Zhou 						 block_group);
13786e80d4f8SDennis Zhou 			goto next;
13796e80d4f8SDennis Zhou 		}
13806e80d4f8SDennis Zhou 
1381e3e0520bSJosef Bacik 		spin_lock(&block_group->lock);
1382e3e0520bSJosef Bacik 		if (block_group->reserved || block_group->pinned ||
1383bf38be65SDavid Sterba 		    block_group->used || block_group->ro ||
1384e3e0520bSJosef Bacik 		    list_is_singular(&block_group->list)) {
1385e3e0520bSJosef Bacik 			/*
1386e3e0520bSJosef Bacik 			 * We want to bail if we made new allocations or have
1387e3e0520bSJosef Bacik 			 * outstanding allocations in this block group.  We do
1388e3e0520bSJosef Bacik 			 * the ro check in case balance is currently acting on
1389e3e0520bSJosef Bacik 			 * this block group.
1390e3e0520bSJosef Bacik 			 */
1391e3e0520bSJosef Bacik 			trace_btrfs_skip_unused_block_group(block_group);
1392e3e0520bSJosef Bacik 			spin_unlock(&block_group->lock);
1393e3e0520bSJosef Bacik 			up_write(&space_info->groups_sem);
1394e3e0520bSJosef Bacik 			goto next;
1395e3e0520bSJosef Bacik 		}
1396e3e0520bSJosef Bacik 		spin_unlock(&block_group->lock);
1397e3e0520bSJosef Bacik 
1398e3e0520bSJosef Bacik 		/* We don't want to force the issue, only flip if it's ok. */
1399e11c0406SJosef Bacik 		ret = inc_block_group_ro(block_group, 0);
1400e3e0520bSJosef Bacik 		up_write(&space_info->groups_sem);
1401e3e0520bSJosef Bacik 		if (ret < 0) {
1402e3e0520bSJosef Bacik 			ret = 0;
1403e3e0520bSJosef Bacik 			goto next;
1404e3e0520bSJosef Bacik 		}
1405e3e0520bSJosef Bacik 
1406e3e0520bSJosef Bacik 		/*
1407e3e0520bSJosef Bacik 		 * Want to do this before we do anything else so we can recover
1408e3e0520bSJosef Bacik 		 * properly if we fail to join the transaction.
1409e3e0520bSJosef Bacik 		 */
1410e3e0520bSJosef Bacik 		trans = btrfs_start_trans_remove_block_group(fs_info,
1411b3470b5dSDavid Sterba 						     block_group->start);
1412e3e0520bSJosef Bacik 		if (IS_ERR(trans)) {
1413e3e0520bSJosef Bacik 			btrfs_dec_block_group_ro(block_group);
1414e3e0520bSJosef Bacik 			ret = PTR_ERR(trans);
1415e3e0520bSJosef Bacik 			goto next;
1416e3e0520bSJosef Bacik 		}
1417e3e0520bSJosef Bacik 
1418e3e0520bSJosef Bacik 		/*
1419e3e0520bSJosef Bacik 		 * We could have pending pinned extents for this block group,
1420e3e0520bSJosef Bacik 		 * just delete them, we don't care about them anymore.
1421e3e0520bSJosef Bacik 		 */
1422534cf531SFilipe Manana 		if (!clean_pinned_extents(trans, block_group)) {
1423534cf531SFilipe Manana 			btrfs_dec_block_group_ro(block_group);
1424e3e0520bSJosef Bacik 			goto end_trans;
1425534cf531SFilipe Manana 		}
1426e3e0520bSJosef Bacik 
1427b0643e59SDennis Zhou 		/*
1428b0643e59SDennis Zhou 		 * At this point, the block_group is read only and should fail
1429b0643e59SDennis Zhou 		 * new allocations.  However, btrfs_finish_extent_commit() can
1430b0643e59SDennis Zhou 		 * cause this block_group to be placed back on the discard
1431b0643e59SDennis Zhou 		 * lists because now the block_group isn't fully discarded.
1432b0643e59SDennis Zhou 		 * Bail here and try again later after discarding everything.
1433b0643e59SDennis Zhou 		 */
1434b0643e59SDennis Zhou 		spin_lock(&fs_info->discard_ctl.lock);
1435b0643e59SDennis Zhou 		if (!list_empty(&block_group->discard_list)) {
1436b0643e59SDennis Zhou 			spin_unlock(&fs_info->discard_ctl.lock);
1437b0643e59SDennis Zhou 			btrfs_dec_block_group_ro(block_group);
1438b0643e59SDennis Zhou 			btrfs_discard_queue_work(&fs_info->discard_ctl,
1439b0643e59SDennis Zhou 						 block_group);
1440b0643e59SDennis Zhou 			goto end_trans;
1441b0643e59SDennis Zhou 		}
1442b0643e59SDennis Zhou 		spin_unlock(&fs_info->discard_ctl.lock);
1443b0643e59SDennis Zhou 
1444e3e0520bSJosef Bacik 		/* Reset pinned so btrfs_put_block_group doesn't complain */
1445e3e0520bSJosef Bacik 		spin_lock(&space_info->lock);
1446e3e0520bSJosef Bacik 		spin_lock(&block_group->lock);
1447e3e0520bSJosef Bacik 
1448e3e0520bSJosef Bacik 		btrfs_space_info_update_bytes_pinned(fs_info, space_info,
1449e3e0520bSJosef Bacik 						     -block_group->pinned);
1450e3e0520bSJosef Bacik 		space_info->bytes_readonly += block_group->pinned;
1451e3e0520bSJosef Bacik 		percpu_counter_add_batch(&space_info->total_bytes_pinned,
1452e3e0520bSJosef Bacik 				   -block_group->pinned,
1453e3e0520bSJosef Bacik 				   BTRFS_TOTAL_BYTES_PINNED_BATCH);
1454e3e0520bSJosef Bacik 		block_group->pinned = 0;
1455e3e0520bSJosef Bacik 
1456e3e0520bSJosef Bacik 		spin_unlock(&block_group->lock);
1457e3e0520bSJosef Bacik 		spin_unlock(&space_info->lock);
1458e3e0520bSJosef Bacik 
14596e80d4f8SDennis Zhou 		/*
14606e80d4f8SDennis Zhou 		 * The normal path here is an unused block group is passed here,
14616e80d4f8SDennis Zhou 		 * then trimming is handled in the transaction commit path.
14626e80d4f8SDennis Zhou 		 * Async discard interposes before this to do the trimming
14636e80d4f8SDennis Zhou 		 * before coming down the unused block group path as trimming
14646e80d4f8SDennis Zhou 		 * will no longer be done later in the transaction commit path.
14656e80d4f8SDennis Zhou 		 */
14666e80d4f8SDennis Zhou 		if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC))
14676e80d4f8SDennis Zhou 			goto flip_async;
14686e80d4f8SDennis Zhou 
1469e3e0520bSJosef Bacik 		/* DISCARD can flip during remount */
147046b27f50SDennis Zhou 		trimming = btrfs_test_opt(fs_info, DISCARD_SYNC);
1471e3e0520bSJosef Bacik 
1472e3e0520bSJosef Bacik 		/* Implicit trim during transaction commit. */
1473e3e0520bSJosef Bacik 		if (trimming)
14746b7304afSFilipe Manana 			btrfs_freeze_block_group(block_group);
1475e3e0520bSJosef Bacik 
1476e3e0520bSJosef Bacik 		/*
1477e3e0520bSJosef Bacik 		 * Btrfs_remove_chunk will abort the transaction if things go
1478e3e0520bSJosef Bacik 		 * horribly wrong.
1479e3e0520bSJosef Bacik 		 */
1480b3470b5dSDavid Sterba 		ret = btrfs_remove_chunk(trans, block_group->start);
1481e3e0520bSJosef Bacik 
1482e3e0520bSJosef Bacik 		if (ret) {
1483e3e0520bSJosef Bacik 			if (trimming)
14846b7304afSFilipe Manana 				btrfs_unfreeze_block_group(block_group);
1485e3e0520bSJosef Bacik 			goto end_trans;
1486e3e0520bSJosef Bacik 		}
1487e3e0520bSJosef Bacik 
1488e3e0520bSJosef Bacik 		/*
1489e3e0520bSJosef Bacik 		 * If we're not mounted with -odiscard, we can just forget
1490e3e0520bSJosef Bacik 		 * about this block group. Otherwise we'll need to wait
1491e3e0520bSJosef Bacik 		 * until transaction commit to do the actual discard.
1492e3e0520bSJosef Bacik 		 */
1493e3e0520bSJosef Bacik 		if (trimming) {
1494e3e0520bSJosef Bacik 			spin_lock(&fs_info->unused_bgs_lock);
1495e3e0520bSJosef Bacik 			/*
1496e3e0520bSJosef Bacik 			 * A concurrent scrub might have added us to the list
1497e3e0520bSJosef Bacik 			 * fs_info->unused_bgs, so use a list_move operation
1498e3e0520bSJosef Bacik 			 * to add the block group to the deleted_bgs list.
1499e3e0520bSJosef Bacik 			 */
1500e3e0520bSJosef Bacik 			list_move(&block_group->bg_list,
1501e3e0520bSJosef Bacik 				  &trans->transaction->deleted_bgs);
1502e3e0520bSJosef Bacik 			spin_unlock(&fs_info->unused_bgs_lock);
1503e3e0520bSJosef Bacik 			btrfs_get_block_group(block_group);
1504e3e0520bSJosef Bacik 		}
1505e3e0520bSJosef Bacik end_trans:
1506e3e0520bSJosef Bacik 		btrfs_end_transaction(trans);
1507e3e0520bSJosef Bacik next:
1508e3e0520bSJosef Bacik 		mutex_unlock(&fs_info->delete_unused_bgs_mutex);
1509e3e0520bSJosef Bacik 		btrfs_put_block_group(block_group);
1510e3e0520bSJosef Bacik 		spin_lock(&fs_info->unused_bgs_lock);
1511e3e0520bSJosef Bacik 	}
1512e3e0520bSJosef Bacik 	spin_unlock(&fs_info->unused_bgs_lock);
15136e80d4f8SDennis Zhou 	return;
15146e80d4f8SDennis Zhou 
15156e80d4f8SDennis Zhou flip_async:
15166e80d4f8SDennis Zhou 	btrfs_end_transaction(trans);
15176e80d4f8SDennis Zhou 	mutex_unlock(&fs_info->delete_unused_bgs_mutex);
15186e80d4f8SDennis Zhou 	btrfs_put_block_group(block_group);
15196e80d4f8SDennis Zhou 	btrfs_discard_punt_unused_bgs_list(fs_info);
1520e3e0520bSJosef Bacik }
1521e3e0520bSJosef Bacik 
152232da5386SDavid Sterba void btrfs_mark_bg_unused(struct btrfs_block_group *bg)
1523e3e0520bSJosef Bacik {
1524e3e0520bSJosef Bacik 	struct btrfs_fs_info *fs_info = bg->fs_info;
1525e3e0520bSJosef Bacik 
1526e3e0520bSJosef Bacik 	spin_lock(&fs_info->unused_bgs_lock);
1527e3e0520bSJosef Bacik 	if (list_empty(&bg->bg_list)) {
1528e3e0520bSJosef Bacik 		btrfs_get_block_group(bg);
1529e3e0520bSJosef Bacik 		trace_btrfs_add_unused_block_group(bg);
1530e3e0520bSJosef Bacik 		list_add_tail(&bg->bg_list, &fs_info->unused_bgs);
1531e3e0520bSJosef Bacik 	}
1532e3e0520bSJosef Bacik 	spin_unlock(&fs_info->unused_bgs_lock);
1533e3e0520bSJosef Bacik }
15344358d963SJosef Bacik 
1535*e3ba67a1SJohannes Thumshirn static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
1536*e3ba67a1SJohannes Thumshirn 			   struct btrfs_path *path)
1537*e3ba67a1SJohannes Thumshirn {
1538*e3ba67a1SJohannes Thumshirn 	struct extent_map_tree *em_tree;
1539*e3ba67a1SJohannes Thumshirn 	struct extent_map *em;
1540*e3ba67a1SJohannes Thumshirn 	struct btrfs_block_group_item bg;
1541*e3ba67a1SJohannes Thumshirn 	struct extent_buffer *leaf;
1542*e3ba67a1SJohannes Thumshirn 	int slot;
1543*e3ba67a1SJohannes Thumshirn 	u64 flags;
1544*e3ba67a1SJohannes Thumshirn 	int ret = 0;
1545*e3ba67a1SJohannes Thumshirn 
1546*e3ba67a1SJohannes Thumshirn 	slot = path->slots[0];
1547*e3ba67a1SJohannes Thumshirn 	leaf = path->nodes[0];
1548*e3ba67a1SJohannes Thumshirn 
1549*e3ba67a1SJohannes Thumshirn 	em_tree = &fs_info->mapping_tree;
1550*e3ba67a1SJohannes Thumshirn 	read_lock(&em_tree->lock);
1551*e3ba67a1SJohannes Thumshirn 	em = lookup_extent_mapping(em_tree, key->objectid, key->offset);
1552*e3ba67a1SJohannes Thumshirn 	read_unlock(&em_tree->lock);
1553*e3ba67a1SJohannes Thumshirn 	if (!em) {
1554*e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
1555*e3ba67a1SJohannes Thumshirn 			  "logical %llu len %llu found bg but no related chunk",
1556*e3ba67a1SJohannes Thumshirn 			  key->objectid, key->offset);
1557*e3ba67a1SJohannes Thumshirn 		return -ENOENT;
1558*e3ba67a1SJohannes Thumshirn 	}
1559*e3ba67a1SJohannes Thumshirn 
1560*e3ba67a1SJohannes Thumshirn 	if (em->start != key->objectid || em->len != key->offset) {
1561*e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
1562*e3ba67a1SJohannes Thumshirn 			"block group %llu len %llu mismatch with chunk %llu len %llu",
1563*e3ba67a1SJohannes Thumshirn 			key->objectid, key->offset, em->start, em->len);
1564*e3ba67a1SJohannes Thumshirn 		ret = -EUCLEAN;
1565*e3ba67a1SJohannes Thumshirn 		goto out_free_em;
1566*e3ba67a1SJohannes Thumshirn 	}
1567*e3ba67a1SJohannes Thumshirn 
1568*e3ba67a1SJohannes Thumshirn 	read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot),
1569*e3ba67a1SJohannes Thumshirn 			   sizeof(bg));
1570*e3ba67a1SJohannes Thumshirn 	flags = btrfs_stack_block_group_flags(&bg) &
1571*e3ba67a1SJohannes Thumshirn 		BTRFS_BLOCK_GROUP_TYPE_MASK;
1572*e3ba67a1SJohannes Thumshirn 
1573*e3ba67a1SJohannes Thumshirn 	if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1574*e3ba67a1SJohannes Thumshirn 		btrfs_err(fs_info,
1575*e3ba67a1SJohannes Thumshirn "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
1576*e3ba67a1SJohannes Thumshirn 			  key->objectid, key->offset, flags,
1577*e3ba67a1SJohannes Thumshirn 			  (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type));
1578*e3ba67a1SJohannes Thumshirn 		ret = -EUCLEAN;
1579*e3ba67a1SJohannes Thumshirn 	}
1580*e3ba67a1SJohannes Thumshirn 
1581*e3ba67a1SJohannes Thumshirn out_free_em:
1582*e3ba67a1SJohannes Thumshirn 	free_extent_map(em);
1583*e3ba67a1SJohannes Thumshirn 	return ret;
1584*e3ba67a1SJohannes Thumshirn }
1585*e3ba67a1SJohannes Thumshirn 
15864358d963SJosef Bacik static int find_first_block_group(struct btrfs_fs_info *fs_info,
15874358d963SJosef Bacik 				  struct btrfs_path *path,
15884358d963SJosef Bacik 				  struct btrfs_key *key)
15894358d963SJosef Bacik {
15904358d963SJosef Bacik 	struct btrfs_root *root = fs_info->extent_root;
1591*e3ba67a1SJohannes Thumshirn 	int ret;
15924358d963SJosef Bacik 	struct btrfs_key found_key;
15934358d963SJosef Bacik 	struct extent_buffer *leaf;
15944358d963SJosef Bacik 	int slot;
15954358d963SJosef Bacik 
15964358d963SJosef Bacik 	ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
15974358d963SJosef Bacik 	if (ret < 0)
1598*e3ba67a1SJohannes Thumshirn 		return ret;
15994358d963SJosef Bacik 
16004358d963SJosef Bacik 	while (1) {
16014358d963SJosef Bacik 		slot = path->slots[0];
16024358d963SJosef Bacik 		leaf = path->nodes[0];
16034358d963SJosef Bacik 		if (slot >= btrfs_header_nritems(leaf)) {
16044358d963SJosef Bacik 			ret = btrfs_next_leaf(root, path);
16054358d963SJosef Bacik 			if (ret == 0)
16064358d963SJosef Bacik 				continue;
16074358d963SJosef Bacik 			if (ret < 0)
16084358d963SJosef Bacik 				goto out;
16094358d963SJosef Bacik 			break;
16104358d963SJosef Bacik 		}
16114358d963SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
16124358d963SJosef Bacik 
16134358d963SJosef Bacik 		if (found_key.objectid >= key->objectid &&
16144358d963SJosef Bacik 		    found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
1615*e3ba67a1SJohannes Thumshirn 			ret = read_bg_from_eb(fs_info, &found_key, path);
1616*e3ba67a1SJohannes Thumshirn 			break;
1617*e3ba67a1SJohannes Thumshirn 		}
16184358d963SJosef Bacik 
16194358d963SJosef Bacik 		path->slots[0]++;
16204358d963SJosef Bacik 	}
16214358d963SJosef Bacik out:
16224358d963SJosef Bacik 	return ret;
16234358d963SJosef Bacik }
16244358d963SJosef Bacik 
16254358d963SJosef Bacik static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
16264358d963SJosef Bacik {
16274358d963SJosef Bacik 	u64 extra_flags = chunk_to_extended(flags) &
16284358d963SJosef Bacik 				BTRFS_EXTENDED_PROFILE_MASK;
16294358d963SJosef Bacik 
16304358d963SJosef Bacik 	write_seqlock(&fs_info->profiles_lock);
16314358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA)
16324358d963SJosef Bacik 		fs_info->avail_data_alloc_bits |= extra_flags;
16334358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_METADATA)
16344358d963SJosef Bacik 		fs_info->avail_metadata_alloc_bits |= extra_flags;
16354358d963SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
16364358d963SJosef Bacik 		fs_info->avail_system_alloc_bits |= extra_flags;
16374358d963SJosef Bacik 	write_sequnlock(&fs_info->profiles_lock);
16384358d963SJosef Bacik }
16394358d963SJosef Bacik 
164096a14336SNikolay Borisov /**
164196a14336SNikolay Borisov  * btrfs_rmap_block - Map a physical disk address to a list of logical addresses
164296a14336SNikolay Borisov  * @chunk_start:   logical address of block group
164396a14336SNikolay Borisov  * @physical:	   physical address to map to logical addresses
164496a14336SNikolay Borisov  * @logical:	   return array of logical addresses which map to @physical
164596a14336SNikolay Borisov  * @naddrs:	   length of @logical
164696a14336SNikolay Borisov  * @stripe_len:    size of IO stripe for the given block group
164796a14336SNikolay Borisov  *
164896a14336SNikolay Borisov  * Maps a particular @physical disk address to a list of @logical addresses.
164996a14336SNikolay Borisov  * Used primarily to exclude those portions of a block group that contain super
165096a14336SNikolay Borisov  * block copies.
165196a14336SNikolay Borisov  */
165296a14336SNikolay Borisov EXPORT_FOR_TESTS
165396a14336SNikolay Borisov int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
165496a14336SNikolay Borisov 		     u64 physical, u64 **logical, int *naddrs, int *stripe_len)
165596a14336SNikolay Borisov {
165696a14336SNikolay Borisov 	struct extent_map *em;
165796a14336SNikolay Borisov 	struct map_lookup *map;
165896a14336SNikolay Borisov 	u64 *buf;
165996a14336SNikolay Borisov 	u64 bytenr;
16601776ad17SNikolay Borisov 	u64 data_stripe_length;
16611776ad17SNikolay Borisov 	u64 io_stripe_size;
16621776ad17SNikolay Borisov 	int i, nr = 0;
16631776ad17SNikolay Borisov 	int ret = 0;
166496a14336SNikolay Borisov 
166596a14336SNikolay Borisov 	em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
166696a14336SNikolay Borisov 	if (IS_ERR(em))
166796a14336SNikolay Borisov 		return -EIO;
166896a14336SNikolay Borisov 
166996a14336SNikolay Borisov 	map = em->map_lookup;
16709e22b925SNikolay Borisov 	data_stripe_length = em->orig_block_len;
16711776ad17SNikolay Borisov 	io_stripe_size = map->stripe_len;
167296a14336SNikolay Borisov 
16739e22b925SNikolay Borisov 	/* For RAID5/6 adjust to a full IO stripe length */
16749e22b925SNikolay Borisov 	if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
16751776ad17SNikolay Borisov 		io_stripe_size = map->stripe_len * nr_data_stripes(map);
167696a14336SNikolay Borisov 
167796a14336SNikolay Borisov 	buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
16781776ad17SNikolay Borisov 	if (!buf) {
16791776ad17SNikolay Borisov 		ret = -ENOMEM;
16801776ad17SNikolay Borisov 		goto out;
16811776ad17SNikolay Borisov 	}
168296a14336SNikolay Borisov 
168396a14336SNikolay Borisov 	for (i = 0; i < map->num_stripes; i++) {
16841776ad17SNikolay Borisov 		bool already_inserted = false;
16851776ad17SNikolay Borisov 		u64 stripe_nr;
16861776ad17SNikolay Borisov 		int j;
16871776ad17SNikolay Borisov 
16881776ad17SNikolay Borisov 		if (!in_range(physical, map->stripes[i].physical,
16891776ad17SNikolay Borisov 			      data_stripe_length))
169096a14336SNikolay Borisov 			continue;
169196a14336SNikolay Borisov 
169296a14336SNikolay Borisov 		stripe_nr = physical - map->stripes[i].physical;
169396a14336SNikolay Borisov 		stripe_nr = div64_u64(stripe_nr, map->stripe_len);
169496a14336SNikolay Borisov 
169596a14336SNikolay Borisov 		if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
169696a14336SNikolay Borisov 			stripe_nr = stripe_nr * map->num_stripes + i;
169796a14336SNikolay Borisov 			stripe_nr = div_u64(stripe_nr, map->sub_stripes);
169896a14336SNikolay Borisov 		} else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
169996a14336SNikolay Borisov 			stripe_nr = stripe_nr * map->num_stripes + i;
170096a14336SNikolay Borisov 		}
170196a14336SNikolay Borisov 		/*
170296a14336SNikolay Borisov 		 * The remaining case would be for RAID56, multiply by
170396a14336SNikolay Borisov 		 * nr_data_stripes().  Alternatively, just use rmap_len below
170496a14336SNikolay Borisov 		 * instead of map->stripe_len
170596a14336SNikolay Borisov 		 */
170696a14336SNikolay Borisov 
17071776ad17SNikolay Borisov 		bytenr = chunk_start + stripe_nr * io_stripe_size;
17081776ad17SNikolay Borisov 
17091776ad17SNikolay Borisov 		/* Ensure we don't add duplicate addresses */
171096a14336SNikolay Borisov 		for (j = 0; j < nr; j++) {
17111776ad17SNikolay Borisov 			if (buf[j] == bytenr) {
17121776ad17SNikolay Borisov 				already_inserted = true;
171396a14336SNikolay Borisov 				break;
171496a14336SNikolay Borisov 			}
171596a14336SNikolay Borisov 		}
17161776ad17SNikolay Borisov 
17171776ad17SNikolay Borisov 		if (!already_inserted)
17181776ad17SNikolay Borisov 			buf[nr++] = bytenr;
171996a14336SNikolay Borisov 	}
172096a14336SNikolay Borisov 
172196a14336SNikolay Borisov 	*logical = buf;
172296a14336SNikolay Borisov 	*naddrs = nr;
17231776ad17SNikolay Borisov 	*stripe_len = io_stripe_size;
17241776ad17SNikolay Borisov out:
172596a14336SNikolay Borisov 	free_extent_map(em);
17261776ad17SNikolay Borisov 	return ret;
172796a14336SNikolay Borisov }
172896a14336SNikolay Borisov 
172932da5386SDavid Sterba static int exclude_super_stripes(struct btrfs_block_group *cache)
17304358d963SJosef Bacik {
17314358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
17324358d963SJosef Bacik 	u64 bytenr;
17334358d963SJosef Bacik 	u64 *logical;
17344358d963SJosef Bacik 	int stripe_len;
17354358d963SJosef Bacik 	int i, nr, ret;
17364358d963SJosef Bacik 
1737b3470b5dSDavid Sterba 	if (cache->start < BTRFS_SUPER_INFO_OFFSET) {
1738b3470b5dSDavid Sterba 		stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start;
17394358d963SJosef Bacik 		cache->bytes_super += stripe_len;
1740b3470b5dSDavid Sterba 		ret = btrfs_add_excluded_extent(fs_info, cache->start,
17414358d963SJosef Bacik 						stripe_len);
17424358d963SJosef Bacik 		if (ret)
17434358d963SJosef Bacik 			return ret;
17444358d963SJosef Bacik 	}
17454358d963SJosef Bacik 
17464358d963SJosef Bacik 	for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
17474358d963SJosef Bacik 		bytenr = btrfs_sb_offset(i);
1748b3470b5dSDavid Sterba 		ret = btrfs_rmap_block(fs_info, cache->start,
17494358d963SJosef Bacik 				       bytenr, &logical, &nr, &stripe_len);
17504358d963SJosef Bacik 		if (ret)
17514358d963SJosef Bacik 			return ret;
17524358d963SJosef Bacik 
17534358d963SJosef Bacik 		while (nr--) {
175496f9b0f2SNikolay Borisov 			u64 len = min_t(u64, stripe_len,
175596f9b0f2SNikolay Borisov 				cache->start + cache->length - logical[nr]);
17564358d963SJosef Bacik 
17574358d963SJosef Bacik 			cache->bytes_super += len;
175896f9b0f2SNikolay Borisov 			ret = btrfs_add_excluded_extent(fs_info, logical[nr],
175996f9b0f2SNikolay Borisov 							len);
17604358d963SJosef Bacik 			if (ret) {
17614358d963SJosef Bacik 				kfree(logical);
17624358d963SJosef Bacik 				return ret;
17634358d963SJosef Bacik 			}
17644358d963SJosef Bacik 		}
17654358d963SJosef Bacik 
17664358d963SJosef Bacik 		kfree(logical);
17674358d963SJosef Bacik 	}
17684358d963SJosef Bacik 	return 0;
17694358d963SJosef Bacik }
17704358d963SJosef Bacik 
177132da5386SDavid Sterba static void link_block_group(struct btrfs_block_group *cache)
17724358d963SJosef Bacik {
17734358d963SJosef Bacik 	struct btrfs_space_info *space_info = cache->space_info;
17744358d963SJosef Bacik 	int index = btrfs_bg_flags_to_raid_index(cache->flags);
17754358d963SJosef Bacik 	bool first = false;
17764358d963SJosef Bacik 
17774358d963SJosef Bacik 	down_write(&space_info->groups_sem);
17784358d963SJosef Bacik 	if (list_empty(&space_info->block_groups[index]))
17794358d963SJosef Bacik 		first = true;
17804358d963SJosef Bacik 	list_add_tail(&cache->list, &space_info->block_groups[index]);
17814358d963SJosef Bacik 	up_write(&space_info->groups_sem);
17824358d963SJosef Bacik 
17834358d963SJosef Bacik 	if (first)
17844358d963SJosef Bacik 		btrfs_sysfs_add_block_group_type(cache);
17854358d963SJosef Bacik }
17864358d963SJosef Bacik 
178732da5386SDavid Sterba static struct btrfs_block_group *btrfs_create_block_group_cache(
17889afc6649SQu Wenruo 		struct btrfs_fs_info *fs_info, u64 start)
17894358d963SJosef Bacik {
179032da5386SDavid Sterba 	struct btrfs_block_group *cache;
17914358d963SJosef Bacik 
17924358d963SJosef Bacik 	cache = kzalloc(sizeof(*cache), GFP_NOFS);
17934358d963SJosef Bacik 	if (!cache)
17944358d963SJosef Bacik 		return NULL;
17954358d963SJosef Bacik 
17964358d963SJosef Bacik 	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
17974358d963SJosef Bacik 					GFP_NOFS);
17984358d963SJosef Bacik 	if (!cache->free_space_ctl) {
17994358d963SJosef Bacik 		kfree(cache);
18004358d963SJosef Bacik 		return NULL;
18014358d963SJosef Bacik 	}
18024358d963SJosef Bacik 
1803b3470b5dSDavid Sterba 	cache->start = start;
18044358d963SJosef Bacik 
18054358d963SJosef Bacik 	cache->fs_info = fs_info;
18064358d963SJosef Bacik 	cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
18074358d963SJosef Bacik 	set_free_space_tree_thresholds(cache);
18084358d963SJosef Bacik 
18096e80d4f8SDennis Zhou 	cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED;
18106e80d4f8SDennis Zhou 
18114358d963SJosef Bacik 	atomic_set(&cache->count, 1);
18124358d963SJosef Bacik 	spin_lock_init(&cache->lock);
18134358d963SJosef Bacik 	init_rwsem(&cache->data_rwsem);
18144358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->list);
18154358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->cluster_list);
18164358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->bg_list);
18174358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->ro_list);
1818b0643e59SDennis Zhou 	INIT_LIST_HEAD(&cache->discard_list);
18194358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->dirty_list);
18204358d963SJosef Bacik 	INIT_LIST_HEAD(&cache->io_list);
18214358d963SJosef Bacik 	btrfs_init_free_space_ctl(cache);
18226b7304afSFilipe Manana 	atomic_set(&cache->frozen, 0);
18234358d963SJosef Bacik 	mutex_init(&cache->free_space_lock);
18244358d963SJosef Bacik 	btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
18254358d963SJosef Bacik 
18264358d963SJosef Bacik 	return cache;
18274358d963SJosef Bacik }
18284358d963SJosef Bacik 
18294358d963SJosef Bacik /*
18304358d963SJosef Bacik  * Iterate all chunks and verify that each of them has the corresponding block
18314358d963SJosef Bacik  * group
18324358d963SJosef Bacik  */
18334358d963SJosef Bacik static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
18344358d963SJosef Bacik {
18354358d963SJosef Bacik 	struct extent_map_tree *map_tree = &fs_info->mapping_tree;
18364358d963SJosef Bacik 	struct extent_map *em;
183732da5386SDavid Sterba 	struct btrfs_block_group *bg;
18384358d963SJosef Bacik 	u64 start = 0;
18394358d963SJosef Bacik 	int ret = 0;
18404358d963SJosef Bacik 
18414358d963SJosef Bacik 	while (1) {
18424358d963SJosef Bacik 		read_lock(&map_tree->lock);
18434358d963SJosef Bacik 		/*
18444358d963SJosef Bacik 		 * lookup_extent_mapping will return the first extent map
18454358d963SJosef Bacik 		 * intersecting the range, so setting @len to 1 is enough to
18464358d963SJosef Bacik 		 * get the first chunk.
18474358d963SJosef Bacik 		 */
18484358d963SJosef Bacik 		em = lookup_extent_mapping(map_tree, start, 1);
18494358d963SJosef Bacik 		read_unlock(&map_tree->lock);
18504358d963SJosef Bacik 		if (!em)
18514358d963SJosef Bacik 			break;
18524358d963SJosef Bacik 
18534358d963SJosef Bacik 		bg = btrfs_lookup_block_group(fs_info, em->start);
18544358d963SJosef Bacik 		if (!bg) {
18554358d963SJosef Bacik 			btrfs_err(fs_info,
18564358d963SJosef Bacik 	"chunk start=%llu len=%llu doesn't have corresponding block group",
18574358d963SJosef Bacik 				     em->start, em->len);
18584358d963SJosef Bacik 			ret = -EUCLEAN;
18594358d963SJosef Bacik 			free_extent_map(em);
18604358d963SJosef Bacik 			break;
18614358d963SJosef Bacik 		}
1862b3470b5dSDavid Sterba 		if (bg->start != em->start || bg->length != em->len ||
18634358d963SJosef Bacik 		    (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
18644358d963SJosef Bacik 		    (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
18654358d963SJosef Bacik 			btrfs_err(fs_info,
18664358d963SJosef Bacik "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",
18674358d963SJosef Bacik 				em->start, em->len,
18684358d963SJosef Bacik 				em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
1869b3470b5dSDavid Sterba 				bg->start, bg->length,
18704358d963SJosef Bacik 				bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
18714358d963SJosef Bacik 			ret = -EUCLEAN;
18724358d963SJosef Bacik 			free_extent_map(em);
18734358d963SJosef Bacik 			btrfs_put_block_group(bg);
18744358d963SJosef Bacik 			break;
18754358d963SJosef Bacik 		}
18764358d963SJosef Bacik 		start = em->start + em->len;
18774358d963SJosef Bacik 		free_extent_map(em);
18784358d963SJosef Bacik 		btrfs_put_block_group(bg);
18794358d963SJosef Bacik 	}
18804358d963SJosef Bacik 	return ret;
18814358d963SJosef Bacik }
18824358d963SJosef Bacik 
18839afc6649SQu Wenruo static int read_block_group_item(struct btrfs_block_group *cache,
18849afc6649SQu Wenruo 				 struct btrfs_path *path,
18859afc6649SQu Wenruo 				 const struct btrfs_key *key)
18869afc6649SQu Wenruo {
18879afc6649SQu Wenruo 	struct extent_buffer *leaf = path->nodes[0];
18889afc6649SQu Wenruo 	struct btrfs_block_group_item bgi;
18899afc6649SQu Wenruo 	int slot = path->slots[0];
18909afc6649SQu Wenruo 
18919afc6649SQu Wenruo 	cache->length = key->offset;
18929afc6649SQu Wenruo 
18939afc6649SQu Wenruo 	read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
18949afc6649SQu Wenruo 			   sizeof(bgi));
18959afc6649SQu Wenruo 	cache->used = btrfs_stack_block_group_used(&bgi);
18969afc6649SQu Wenruo 	cache->flags = btrfs_stack_block_group_flags(&bgi);
18979afc6649SQu Wenruo 
18989afc6649SQu Wenruo 	return 0;
18999afc6649SQu Wenruo }
19009afc6649SQu Wenruo 
1901ffb9e0f0SQu Wenruo static int read_one_block_group(struct btrfs_fs_info *info,
1902ffb9e0f0SQu Wenruo 				struct btrfs_path *path,
1903d49a2ddbSQu Wenruo 				const struct btrfs_key *key,
1904ffb9e0f0SQu Wenruo 				int need_clear)
1905ffb9e0f0SQu Wenruo {
190632da5386SDavid Sterba 	struct btrfs_block_group *cache;
1907ffb9e0f0SQu Wenruo 	struct btrfs_space_info *space_info;
1908ffb9e0f0SQu Wenruo 	const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS);
1909ffb9e0f0SQu Wenruo 	int ret;
1910ffb9e0f0SQu Wenruo 
1911d49a2ddbSQu Wenruo 	ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY);
1912ffb9e0f0SQu Wenruo 
19139afc6649SQu Wenruo 	cache = btrfs_create_block_group_cache(info, key->objectid);
1914ffb9e0f0SQu Wenruo 	if (!cache)
1915ffb9e0f0SQu Wenruo 		return -ENOMEM;
1916ffb9e0f0SQu Wenruo 
19179afc6649SQu Wenruo 	ret = read_block_group_item(cache, path, key);
19189afc6649SQu Wenruo 	if (ret < 0)
19199afc6649SQu Wenruo 		goto error;
19209afc6649SQu Wenruo 
1921ffb9e0f0SQu Wenruo 	if (need_clear) {
1922ffb9e0f0SQu Wenruo 		/*
1923ffb9e0f0SQu Wenruo 		 * When we mount with old space cache, we need to
1924ffb9e0f0SQu Wenruo 		 * set BTRFS_DC_CLEAR and set dirty flag.
1925ffb9e0f0SQu Wenruo 		 *
1926ffb9e0f0SQu Wenruo 		 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
1927ffb9e0f0SQu Wenruo 		 *    truncate the old free space cache inode and
1928ffb9e0f0SQu Wenruo 		 *    setup a new one.
1929ffb9e0f0SQu Wenruo 		 * b) Setting 'dirty flag' makes sure that we flush
1930ffb9e0f0SQu Wenruo 		 *    the new space cache info onto disk.
1931ffb9e0f0SQu Wenruo 		 */
1932ffb9e0f0SQu Wenruo 		if (btrfs_test_opt(info, SPACE_CACHE))
1933ffb9e0f0SQu Wenruo 			cache->disk_cache_state = BTRFS_DC_CLEAR;
1934ffb9e0f0SQu Wenruo 	}
1935ffb9e0f0SQu Wenruo 	if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
1936ffb9e0f0SQu Wenruo 	    (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
1937ffb9e0f0SQu Wenruo 			btrfs_err(info,
1938ffb9e0f0SQu Wenruo "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
1939ffb9e0f0SQu Wenruo 				  cache->start);
1940ffb9e0f0SQu Wenruo 			ret = -EINVAL;
1941ffb9e0f0SQu Wenruo 			goto error;
1942ffb9e0f0SQu Wenruo 	}
1943ffb9e0f0SQu Wenruo 
1944ffb9e0f0SQu Wenruo 	/*
1945ffb9e0f0SQu Wenruo 	 * We need to exclude the super stripes now so that the space info has
1946ffb9e0f0SQu Wenruo 	 * super bytes accounted for, otherwise we'll think we have more space
1947ffb9e0f0SQu Wenruo 	 * than we actually do.
1948ffb9e0f0SQu Wenruo 	 */
1949ffb9e0f0SQu Wenruo 	ret = exclude_super_stripes(cache);
1950ffb9e0f0SQu Wenruo 	if (ret) {
1951ffb9e0f0SQu Wenruo 		/* We may have excluded something, so call this just in case. */
1952ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
1953ffb9e0f0SQu Wenruo 		goto error;
1954ffb9e0f0SQu Wenruo 	}
1955ffb9e0f0SQu Wenruo 
1956ffb9e0f0SQu Wenruo 	/*
1957ffb9e0f0SQu Wenruo 	 * Check for two cases, either we are full, and therefore don't need
1958ffb9e0f0SQu Wenruo 	 * to bother with the caching work since we won't find any space, or we
1959ffb9e0f0SQu Wenruo 	 * are empty, and we can just add all the space in and be done with it.
1960ffb9e0f0SQu Wenruo 	 * This saves us _a_lot_ of time, particularly in the full case.
1961ffb9e0f0SQu Wenruo 	 */
19629afc6649SQu Wenruo 	if (cache->length == cache->used) {
1963ffb9e0f0SQu Wenruo 		cache->last_byte_to_unpin = (u64)-1;
1964ffb9e0f0SQu Wenruo 		cache->cached = BTRFS_CACHE_FINISHED;
1965ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
1966ffb9e0f0SQu Wenruo 	} else if (cache->used == 0) {
1967ffb9e0f0SQu Wenruo 		cache->last_byte_to_unpin = (u64)-1;
1968ffb9e0f0SQu Wenruo 		cache->cached = BTRFS_CACHE_FINISHED;
19699afc6649SQu Wenruo 		add_new_free_space(cache, cache->start,
19709afc6649SQu Wenruo 				   cache->start + cache->length);
1971ffb9e0f0SQu Wenruo 		btrfs_free_excluded_extents(cache);
1972ffb9e0f0SQu Wenruo 	}
1973ffb9e0f0SQu Wenruo 
1974ffb9e0f0SQu Wenruo 	ret = btrfs_add_block_group_cache(info, cache);
1975ffb9e0f0SQu Wenruo 	if (ret) {
1976ffb9e0f0SQu Wenruo 		btrfs_remove_free_space_cache(cache);
1977ffb9e0f0SQu Wenruo 		goto error;
1978ffb9e0f0SQu Wenruo 	}
1979ffb9e0f0SQu Wenruo 	trace_btrfs_add_block_group(info, cache, 0);
19809afc6649SQu Wenruo 	btrfs_update_space_info(info, cache->flags, cache->length,
1981ffb9e0f0SQu Wenruo 				cache->used, cache->bytes_super, &space_info);
1982ffb9e0f0SQu Wenruo 
1983ffb9e0f0SQu Wenruo 	cache->space_info = space_info;
1984ffb9e0f0SQu Wenruo 
1985ffb9e0f0SQu Wenruo 	link_block_group(cache);
1986ffb9e0f0SQu Wenruo 
1987ffb9e0f0SQu Wenruo 	set_avail_alloc_bits(info, cache->flags);
1988ffb9e0f0SQu Wenruo 	if (btrfs_chunk_readonly(info, cache->start)) {
1989ffb9e0f0SQu Wenruo 		inc_block_group_ro(cache, 1);
1990ffb9e0f0SQu Wenruo 	} else if (cache->used == 0) {
1991ffb9e0f0SQu Wenruo 		ASSERT(list_empty(&cache->bg_list));
19926e80d4f8SDennis Zhou 		if (btrfs_test_opt(info, DISCARD_ASYNC))
19936e80d4f8SDennis Zhou 			btrfs_discard_queue_work(&info->discard_ctl, cache);
19946e80d4f8SDennis Zhou 		else
1995ffb9e0f0SQu Wenruo 			btrfs_mark_bg_unused(cache);
1996ffb9e0f0SQu Wenruo 	}
1997ffb9e0f0SQu Wenruo 	return 0;
1998ffb9e0f0SQu Wenruo error:
1999ffb9e0f0SQu Wenruo 	btrfs_put_block_group(cache);
2000ffb9e0f0SQu Wenruo 	return ret;
2001ffb9e0f0SQu Wenruo }
2002ffb9e0f0SQu Wenruo 
20034358d963SJosef Bacik int btrfs_read_block_groups(struct btrfs_fs_info *info)
20044358d963SJosef Bacik {
20054358d963SJosef Bacik 	struct btrfs_path *path;
20064358d963SJosef Bacik 	int ret;
200732da5386SDavid Sterba 	struct btrfs_block_group *cache;
20084358d963SJosef Bacik 	struct btrfs_space_info *space_info;
20094358d963SJosef Bacik 	struct btrfs_key key;
20104358d963SJosef Bacik 	int need_clear = 0;
20114358d963SJosef Bacik 	u64 cache_gen;
20124358d963SJosef Bacik 
20134358d963SJosef Bacik 	key.objectid = 0;
20144358d963SJosef Bacik 	key.offset = 0;
20154358d963SJosef Bacik 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
20164358d963SJosef Bacik 	path = btrfs_alloc_path();
20174358d963SJosef Bacik 	if (!path)
20184358d963SJosef Bacik 		return -ENOMEM;
20194358d963SJosef Bacik 
20204358d963SJosef Bacik 	cache_gen = btrfs_super_cache_generation(info->super_copy);
20214358d963SJosef Bacik 	if (btrfs_test_opt(info, SPACE_CACHE) &&
20224358d963SJosef Bacik 	    btrfs_super_generation(info->super_copy) != cache_gen)
20234358d963SJosef Bacik 		need_clear = 1;
20244358d963SJosef Bacik 	if (btrfs_test_opt(info, CLEAR_CACHE))
20254358d963SJosef Bacik 		need_clear = 1;
20264358d963SJosef Bacik 
20274358d963SJosef Bacik 	while (1) {
20284358d963SJosef Bacik 		ret = find_first_block_group(info, path, &key);
20294358d963SJosef Bacik 		if (ret > 0)
20304358d963SJosef Bacik 			break;
20314358d963SJosef Bacik 		if (ret != 0)
20324358d963SJosef Bacik 			goto error;
20334358d963SJosef Bacik 
2034ffb9e0f0SQu Wenruo 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2035d49a2ddbSQu Wenruo 		ret = read_one_block_group(info, path, &key, need_clear);
2036ffb9e0f0SQu Wenruo 		if (ret < 0)
20374358d963SJosef Bacik 			goto error;
2038ffb9e0f0SQu Wenruo 		key.objectid += key.offset;
2039ffb9e0f0SQu Wenruo 		key.offset = 0;
20404358d963SJosef Bacik 		btrfs_release_path(path);
20414358d963SJosef Bacik 	}
20424358d963SJosef Bacik 
204329566c9cSMadhuparna Bhowmik 	rcu_read_lock();
20444358d963SJosef Bacik 	list_for_each_entry_rcu(space_info, &info->space_info, list) {
20454358d963SJosef Bacik 		if (!(btrfs_get_alloc_profile(info, space_info->flags) &
20464358d963SJosef Bacik 		      (BTRFS_BLOCK_GROUP_RAID10 |
20474358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_RAID1_MASK |
20484358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_RAID56_MASK |
20494358d963SJosef Bacik 		       BTRFS_BLOCK_GROUP_DUP)))
20504358d963SJosef Bacik 			continue;
20514358d963SJosef Bacik 		/*
20524358d963SJosef Bacik 		 * Avoid allocating from un-mirrored block group if there are
20534358d963SJosef Bacik 		 * mirrored block groups.
20544358d963SJosef Bacik 		 */
20554358d963SJosef Bacik 		list_for_each_entry(cache,
20564358d963SJosef Bacik 				&space_info->block_groups[BTRFS_RAID_RAID0],
20574358d963SJosef Bacik 				list)
2058e11c0406SJosef Bacik 			inc_block_group_ro(cache, 1);
20594358d963SJosef Bacik 		list_for_each_entry(cache,
20604358d963SJosef Bacik 				&space_info->block_groups[BTRFS_RAID_SINGLE],
20614358d963SJosef Bacik 				list)
2062e11c0406SJosef Bacik 			inc_block_group_ro(cache, 1);
20634358d963SJosef Bacik 	}
206429566c9cSMadhuparna Bhowmik 	rcu_read_unlock();
20654358d963SJosef Bacik 
20664358d963SJosef Bacik 	btrfs_init_global_block_rsv(info);
20674358d963SJosef Bacik 	ret = check_chunk_block_group_mappings(info);
20684358d963SJosef Bacik error:
20694358d963SJosef Bacik 	btrfs_free_path(path);
20704358d963SJosef Bacik 	return ret;
20714358d963SJosef Bacik }
20724358d963SJosef Bacik 
207397f4728aSQu Wenruo static int insert_block_group_item(struct btrfs_trans_handle *trans,
207497f4728aSQu Wenruo 				   struct btrfs_block_group *block_group)
207597f4728aSQu Wenruo {
207697f4728aSQu Wenruo 	struct btrfs_fs_info *fs_info = trans->fs_info;
207797f4728aSQu Wenruo 	struct btrfs_block_group_item bgi;
207897f4728aSQu Wenruo 	struct btrfs_root *root;
207997f4728aSQu Wenruo 	struct btrfs_key key;
208097f4728aSQu Wenruo 
208197f4728aSQu Wenruo 	spin_lock(&block_group->lock);
208297f4728aSQu Wenruo 	btrfs_set_stack_block_group_used(&bgi, block_group->used);
208397f4728aSQu Wenruo 	btrfs_set_stack_block_group_chunk_objectid(&bgi,
208497f4728aSQu Wenruo 				BTRFS_FIRST_CHUNK_TREE_OBJECTID);
208597f4728aSQu Wenruo 	btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
208697f4728aSQu Wenruo 	key.objectid = block_group->start;
208797f4728aSQu Wenruo 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
208897f4728aSQu Wenruo 	key.offset = block_group->length;
208997f4728aSQu Wenruo 	spin_unlock(&block_group->lock);
209097f4728aSQu Wenruo 
209197f4728aSQu Wenruo 	root = fs_info->extent_root;
209297f4728aSQu Wenruo 	return btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi));
209397f4728aSQu Wenruo }
209497f4728aSQu Wenruo 
20954358d963SJosef Bacik void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
20964358d963SJosef Bacik {
20974358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
209832da5386SDavid Sterba 	struct btrfs_block_group *block_group;
20994358d963SJosef Bacik 	int ret = 0;
21004358d963SJosef Bacik 
21014358d963SJosef Bacik 	if (!trans->can_flush_pending_bgs)
21024358d963SJosef Bacik 		return;
21034358d963SJosef Bacik 
21044358d963SJosef Bacik 	while (!list_empty(&trans->new_bgs)) {
21054358d963SJosef Bacik 		block_group = list_first_entry(&trans->new_bgs,
210632da5386SDavid Sterba 					       struct btrfs_block_group,
21074358d963SJosef Bacik 					       bg_list);
21084358d963SJosef Bacik 		if (ret)
21094358d963SJosef Bacik 			goto next;
21104358d963SJosef Bacik 
211197f4728aSQu Wenruo 		ret = insert_block_group_item(trans, block_group);
21124358d963SJosef Bacik 		if (ret)
21134358d963SJosef Bacik 			btrfs_abort_transaction(trans, ret);
211497f4728aSQu Wenruo 		ret = btrfs_finish_chunk_alloc(trans, block_group->start,
211597f4728aSQu Wenruo 					block_group->length);
21164358d963SJosef Bacik 		if (ret)
21174358d963SJosef Bacik 			btrfs_abort_transaction(trans, ret);
21184358d963SJosef Bacik 		add_block_group_free_space(trans, block_group);
21194358d963SJosef Bacik 		/* Already aborted the transaction if it failed. */
21204358d963SJosef Bacik next:
21214358d963SJosef Bacik 		btrfs_delayed_refs_rsv_release(fs_info, 1);
21224358d963SJosef Bacik 		list_del_init(&block_group->bg_list);
21234358d963SJosef Bacik 	}
21244358d963SJosef Bacik 	btrfs_trans_release_chunk_metadata(trans);
21254358d963SJosef Bacik }
21264358d963SJosef Bacik 
21274358d963SJosef Bacik int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used,
21284358d963SJosef Bacik 			   u64 type, u64 chunk_offset, u64 size)
21294358d963SJosef Bacik {
21304358d963SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
213132da5386SDavid Sterba 	struct btrfs_block_group *cache;
21324358d963SJosef Bacik 	int ret;
21334358d963SJosef Bacik 
21344358d963SJosef Bacik 	btrfs_set_log_full_commit(trans);
21354358d963SJosef Bacik 
21369afc6649SQu Wenruo 	cache = btrfs_create_block_group_cache(fs_info, chunk_offset);
21374358d963SJosef Bacik 	if (!cache)
21384358d963SJosef Bacik 		return -ENOMEM;
21394358d963SJosef Bacik 
21409afc6649SQu Wenruo 	cache->length = size;
2141bf38be65SDavid Sterba 	cache->used = bytes_used;
21424358d963SJosef Bacik 	cache->flags = type;
21434358d963SJosef Bacik 	cache->last_byte_to_unpin = (u64)-1;
21444358d963SJosef Bacik 	cache->cached = BTRFS_CACHE_FINISHED;
21454358d963SJosef Bacik 	cache->needs_free_space = 1;
21464358d963SJosef Bacik 	ret = exclude_super_stripes(cache);
21474358d963SJosef Bacik 	if (ret) {
21484358d963SJosef Bacik 		/* We may have excluded something, so call this just in case */
21494358d963SJosef Bacik 		btrfs_free_excluded_extents(cache);
21504358d963SJosef Bacik 		btrfs_put_block_group(cache);
21514358d963SJosef Bacik 		return ret;
21524358d963SJosef Bacik 	}
21534358d963SJosef Bacik 
21544358d963SJosef Bacik 	add_new_free_space(cache, chunk_offset, chunk_offset + size);
21554358d963SJosef Bacik 
21564358d963SJosef Bacik 	btrfs_free_excluded_extents(cache);
21574358d963SJosef Bacik 
21584358d963SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
21594358d963SJosef Bacik 	if (btrfs_should_fragment_free_space(cache)) {
21604358d963SJosef Bacik 		u64 new_bytes_used = size - bytes_used;
21614358d963SJosef Bacik 
21624358d963SJosef Bacik 		bytes_used += new_bytes_used >> 1;
2163e11c0406SJosef Bacik 		fragment_free_space(cache);
21644358d963SJosef Bacik 	}
21654358d963SJosef Bacik #endif
21664358d963SJosef Bacik 	/*
21674358d963SJosef Bacik 	 * Ensure the corresponding space_info object is created and
21684358d963SJosef Bacik 	 * assigned to our block group. We want our bg to be added to the rbtree
21694358d963SJosef Bacik 	 * with its ->space_info set.
21704358d963SJosef Bacik 	 */
21714358d963SJosef Bacik 	cache->space_info = btrfs_find_space_info(fs_info, cache->flags);
21724358d963SJosef Bacik 	ASSERT(cache->space_info);
21734358d963SJosef Bacik 
21744358d963SJosef Bacik 	ret = btrfs_add_block_group_cache(fs_info, cache);
21754358d963SJosef Bacik 	if (ret) {
21764358d963SJosef Bacik 		btrfs_remove_free_space_cache(cache);
21774358d963SJosef Bacik 		btrfs_put_block_group(cache);
21784358d963SJosef Bacik 		return ret;
21794358d963SJosef Bacik 	}
21804358d963SJosef Bacik 
21814358d963SJosef Bacik 	/*
21824358d963SJosef Bacik 	 * Now that our block group has its ->space_info set and is inserted in
21834358d963SJosef Bacik 	 * the rbtree, update the space info's counters.
21844358d963SJosef Bacik 	 */
21854358d963SJosef Bacik 	trace_btrfs_add_block_group(fs_info, cache, 1);
21864358d963SJosef Bacik 	btrfs_update_space_info(fs_info, cache->flags, size, bytes_used,
21874358d963SJosef Bacik 				cache->bytes_super, &cache->space_info);
21884358d963SJosef Bacik 	btrfs_update_global_block_rsv(fs_info);
21894358d963SJosef Bacik 
21904358d963SJosef Bacik 	link_block_group(cache);
21914358d963SJosef Bacik 
21924358d963SJosef Bacik 	list_add_tail(&cache->bg_list, &trans->new_bgs);
21934358d963SJosef Bacik 	trans->delayed_ref_updates++;
21944358d963SJosef Bacik 	btrfs_update_delayed_refs_rsv(trans);
21954358d963SJosef Bacik 
21964358d963SJosef Bacik 	set_avail_alloc_bits(fs_info, type);
21974358d963SJosef Bacik 	return 0;
21984358d963SJosef Bacik }
219926ce2095SJosef Bacik 
220026ce2095SJosef Bacik static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags)
220126ce2095SJosef Bacik {
220226ce2095SJosef Bacik 	u64 num_devices;
220326ce2095SJosef Bacik 	u64 stripped;
220426ce2095SJosef Bacik 
220526ce2095SJosef Bacik 	/*
220626ce2095SJosef Bacik 	 * if restripe for this chunk_type is on pick target profile and
220726ce2095SJosef Bacik 	 * return, otherwise do the usual balance
220826ce2095SJosef Bacik 	 */
2209e11c0406SJosef Bacik 	stripped = get_restripe_target(fs_info, flags);
221026ce2095SJosef Bacik 	if (stripped)
221126ce2095SJosef Bacik 		return extended_to_chunk(stripped);
221226ce2095SJosef Bacik 
221326ce2095SJosef Bacik 	num_devices = fs_info->fs_devices->rw_devices;
221426ce2095SJosef Bacik 
221526ce2095SJosef Bacik 	stripped = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID56_MASK |
221626ce2095SJosef Bacik 		BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10;
221726ce2095SJosef Bacik 
221826ce2095SJosef Bacik 	if (num_devices == 1) {
221926ce2095SJosef Bacik 		stripped |= BTRFS_BLOCK_GROUP_DUP;
222026ce2095SJosef Bacik 		stripped = flags & ~stripped;
222126ce2095SJosef Bacik 
222226ce2095SJosef Bacik 		/* turn raid0 into single device chunks */
222326ce2095SJosef Bacik 		if (flags & BTRFS_BLOCK_GROUP_RAID0)
222426ce2095SJosef Bacik 			return stripped;
222526ce2095SJosef Bacik 
222626ce2095SJosef Bacik 		/* turn mirroring into duplication */
222726ce2095SJosef Bacik 		if (flags & (BTRFS_BLOCK_GROUP_RAID1_MASK |
222826ce2095SJosef Bacik 			     BTRFS_BLOCK_GROUP_RAID10))
222926ce2095SJosef Bacik 			return stripped | BTRFS_BLOCK_GROUP_DUP;
223026ce2095SJosef Bacik 	} else {
223126ce2095SJosef Bacik 		/* they already had raid on here, just return */
223226ce2095SJosef Bacik 		if (flags & stripped)
223326ce2095SJosef Bacik 			return flags;
223426ce2095SJosef Bacik 
223526ce2095SJosef Bacik 		stripped |= BTRFS_BLOCK_GROUP_DUP;
223626ce2095SJosef Bacik 		stripped = flags & ~stripped;
223726ce2095SJosef Bacik 
223826ce2095SJosef Bacik 		/* switch duplicated blocks with raid1 */
223926ce2095SJosef Bacik 		if (flags & BTRFS_BLOCK_GROUP_DUP)
224026ce2095SJosef Bacik 			return stripped | BTRFS_BLOCK_GROUP_RAID1;
224126ce2095SJosef Bacik 
224226ce2095SJosef Bacik 		/* this is drive concat, leave it alone */
224326ce2095SJosef Bacik 	}
224426ce2095SJosef Bacik 
224526ce2095SJosef Bacik 	return flags;
224626ce2095SJosef Bacik }
224726ce2095SJosef Bacik 
2248b12de528SQu Wenruo /*
2249b12de528SQu Wenruo  * Mark one block group RO, can be called several times for the same block
2250b12de528SQu Wenruo  * group.
2251b12de528SQu Wenruo  *
2252b12de528SQu Wenruo  * @cache:		the destination block group
2253b12de528SQu Wenruo  * @do_chunk_alloc:	whether need to do chunk pre-allocation, this is to
2254b12de528SQu Wenruo  * 			ensure we still have some free space after marking this
2255b12de528SQu Wenruo  * 			block group RO.
2256b12de528SQu Wenruo  */
2257b12de528SQu Wenruo int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
2258b12de528SQu Wenruo 			     bool do_chunk_alloc)
225926ce2095SJosef Bacik {
226026ce2095SJosef Bacik 	struct btrfs_fs_info *fs_info = cache->fs_info;
226126ce2095SJosef Bacik 	struct btrfs_trans_handle *trans;
226226ce2095SJosef Bacik 	u64 alloc_flags;
226326ce2095SJosef Bacik 	int ret;
226426ce2095SJosef Bacik 
226526ce2095SJosef Bacik again:
226626ce2095SJosef Bacik 	trans = btrfs_join_transaction(fs_info->extent_root);
226726ce2095SJosef Bacik 	if (IS_ERR(trans))
226826ce2095SJosef Bacik 		return PTR_ERR(trans);
226926ce2095SJosef Bacik 
227026ce2095SJosef Bacik 	/*
227126ce2095SJosef Bacik 	 * we're not allowed to set block groups readonly after the dirty
227226ce2095SJosef Bacik 	 * block groups cache has started writing.  If it already started,
227326ce2095SJosef Bacik 	 * back off and let this transaction commit
227426ce2095SJosef Bacik 	 */
227526ce2095SJosef Bacik 	mutex_lock(&fs_info->ro_block_group_mutex);
227626ce2095SJosef Bacik 	if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
227726ce2095SJosef Bacik 		u64 transid = trans->transid;
227826ce2095SJosef Bacik 
227926ce2095SJosef Bacik 		mutex_unlock(&fs_info->ro_block_group_mutex);
228026ce2095SJosef Bacik 		btrfs_end_transaction(trans);
228126ce2095SJosef Bacik 
228226ce2095SJosef Bacik 		ret = btrfs_wait_for_commit(fs_info, transid);
228326ce2095SJosef Bacik 		if (ret)
228426ce2095SJosef Bacik 			return ret;
228526ce2095SJosef Bacik 		goto again;
228626ce2095SJosef Bacik 	}
228726ce2095SJosef Bacik 
2288b12de528SQu Wenruo 	if (do_chunk_alloc) {
228926ce2095SJosef Bacik 		/*
2290b12de528SQu Wenruo 		 * If we are changing raid levels, try to allocate a
2291b12de528SQu Wenruo 		 * corresponding block group with the new raid level.
229226ce2095SJosef Bacik 		 */
229326ce2095SJosef Bacik 		alloc_flags = update_block_group_flags(fs_info, cache->flags);
229426ce2095SJosef Bacik 		if (alloc_flags != cache->flags) {
2295b12de528SQu Wenruo 			ret = btrfs_chunk_alloc(trans, alloc_flags,
2296b12de528SQu Wenruo 						CHUNK_ALLOC_FORCE);
229726ce2095SJosef Bacik 			/*
229826ce2095SJosef Bacik 			 * ENOSPC is allowed here, we may have enough space
2299b12de528SQu Wenruo 			 * already allocated at the new raid level to carry on
230026ce2095SJosef Bacik 			 */
230126ce2095SJosef Bacik 			if (ret == -ENOSPC)
230226ce2095SJosef Bacik 				ret = 0;
230326ce2095SJosef Bacik 			if (ret < 0)
230426ce2095SJosef Bacik 				goto out;
230526ce2095SJosef Bacik 		}
2306b12de528SQu Wenruo 	}
230726ce2095SJosef Bacik 
2308a7a63accSJosef Bacik 	ret = inc_block_group_ro(cache, 0);
2309b12de528SQu Wenruo 	if (!do_chunk_alloc)
2310b12de528SQu Wenruo 		goto unlock_out;
231126ce2095SJosef Bacik 	if (!ret)
231226ce2095SJosef Bacik 		goto out;
231326ce2095SJosef Bacik 	alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags);
231426ce2095SJosef Bacik 	ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
231526ce2095SJosef Bacik 	if (ret < 0)
231626ce2095SJosef Bacik 		goto out;
2317e11c0406SJosef Bacik 	ret = inc_block_group_ro(cache, 0);
231826ce2095SJosef Bacik out:
231926ce2095SJosef Bacik 	if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
232026ce2095SJosef Bacik 		alloc_flags = update_block_group_flags(fs_info, cache->flags);
232126ce2095SJosef Bacik 		mutex_lock(&fs_info->chunk_mutex);
232226ce2095SJosef Bacik 		check_system_chunk(trans, alloc_flags);
232326ce2095SJosef Bacik 		mutex_unlock(&fs_info->chunk_mutex);
232426ce2095SJosef Bacik 	}
2325b12de528SQu Wenruo unlock_out:
232626ce2095SJosef Bacik 	mutex_unlock(&fs_info->ro_block_group_mutex);
232726ce2095SJosef Bacik 
232826ce2095SJosef Bacik 	btrfs_end_transaction(trans);
232926ce2095SJosef Bacik 	return ret;
233026ce2095SJosef Bacik }
233126ce2095SJosef Bacik 
233232da5386SDavid Sterba void btrfs_dec_block_group_ro(struct btrfs_block_group *cache)
233326ce2095SJosef Bacik {
233426ce2095SJosef Bacik 	struct btrfs_space_info *sinfo = cache->space_info;
233526ce2095SJosef Bacik 	u64 num_bytes;
233626ce2095SJosef Bacik 
233726ce2095SJosef Bacik 	BUG_ON(!cache->ro);
233826ce2095SJosef Bacik 
233926ce2095SJosef Bacik 	spin_lock(&sinfo->lock);
234026ce2095SJosef Bacik 	spin_lock(&cache->lock);
234126ce2095SJosef Bacik 	if (!--cache->ro) {
2342b3470b5dSDavid Sterba 		num_bytes = cache->length - cache->reserved -
2343bf38be65SDavid Sterba 			    cache->pinned - cache->bytes_super - cache->used;
234426ce2095SJosef Bacik 		sinfo->bytes_readonly -= num_bytes;
234526ce2095SJosef Bacik 		list_del_init(&cache->ro_list);
234626ce2095SJosef Bacik 	}
234726ce2095SJosef Bacik 	spin_unlock(&cache->lock);
234826ce2095SJosef Bacik 	spin_unlock(&sinfo->lock);
234926ce2095SJosef Bacik }
235077745c05SJosef Bacik 
23513be4d8efSQu Wenruo static int update_block_group_item(struct btrfs_trans_handle *trans,
235277745c05SJosef Bacik 				   struct btrfs_path *path,
235332da5386SDavid Sterba 				   struct btrfs_block_group *cache)
235477745c05SJosef Bacik {
235577745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
235677745c05SJosef Bacik 	int ret;
23573be4d8efSQu Wenruo 	struct btrfs_root *root = fs_info->extent_root;
235877745c05SJosef Bacik 	unsigned long bi;
235977745c05SJosef Bacik 	struct extent_buffer *leaf;
2360bf38be65SDavid Sterba 	struct btrfs_block_group_item bgi;
2361b3470b5dSDavid Sterba 	struct btrfs_key key;
236277745c05SJosef Bacik 
2363b3470b5dSDavid Sterba 	key.objectid = cache->start;
2364b3470b5dSDavid Sterba 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2365b3470b5dSDavid Sterba 	key.offset = cache->length;
2366b3470b5dSDavid Sterba 
23673be4d8efSQu Wenruo 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
236877745c05SJosef Bacik 	if (ret) {
236977745c05SJosef Bacik 		if (ret > 0)
237077745c05SJosef Bacik 			ret = -ENOENT;
237177745c05SJosef Bacik 		goto fail;
237277745c05SJosef Bacik 	}
237377745c05SJosef Bacik 
237477745c05SJosef Bacik 	leaf = path->nodes[0];
237577745c05SJosef Bacik 	bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2376de0dc456SDavid Sterba 	btrfs_set_stack_block_group_used(&bgi, cache->used);
2377de0dc456SDavid Sterba 	btrfs_set_stack_block_group_chunk_objectid(&bgi,
23783d976388SDavid Sterba 			BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2379de0dc456SDavid Sterba 	btrfs_set_stack_block_group_flags(&bgi, cache->flags);
2380bf38be65SDavid Sterba 	write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
238177745c05SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
238277745c05SJosef Bacik fail:
238377745c05SJosef Bacik 	btrfs_release_path(path);
238477745c05SJosef Bacik 	return ret;
238577745c05SJosef Bacik 
238677745c05SJosef Bacik }
238777745c05SJosef Bacik 
238832da5386SDavid Sterba static int cache_save_setup(struct btrfs_block_group *block_group,
238977745c05SJosef Bacik 			    struct btrfs_trans_handle *trans,
239077745c05SJosef Bacik 			    struct btrfs_path *path)
239177745c05SJosef Bacik {
239277745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = block_group->fs_info;
239377745c05SJosef Bacik 	struct btrfs_root *root = fs_info->tree_root;
239477745c05SJosef Bacik 	struct inode *inode = NULL;
239577745c05SJosef Bacik 	struct extent_changeset *data_reserved = NULL;
239677745c05SJosef Bacik 	u64 alloc_hint = 0;
239777745c05SJosef Bacik 	int dcs = BTRFS_DC_ERROR;
239877745c05SJosef Bacik 	u64 num_pages = 0;
239977745c05SJosef Bacik 	int retries = 0;
240077745c05SJosef Bacik 	int ret = 0;
240177745c05SJosef Bacik 
240277745c05SJosef Bacik 	/*
240377745c05SJosef Bacik 	 * If this block group is smaller than 100 megs don't bother caching the
240477745c05SJosef Bacik 	 * block group.
240577745c05SJosef Bacik 	 */
2406b3470b5dSDavid Sterba 	if (block_group->length < (100 * SZ_1M)) {
240777745c05SJosef Bacik 		spin_lock(&block_group->lock);
240877745c05SJosef Bacik 		block_group->disk_cache_state = BTRFS_DC_WRITTEN;
240977745c05SJosef Bacik 		spin_unlock(&block_group->lock);
241077745c05SJosef Bacik 		return 0;
241177745c05SJosef Bacik 	}
241277745c05SJosef Bacik 
2413bf31f87fSDavid Sterba 	if (TRANS_ABORTED(trans))
241477745c05SJosef Bacik 		return 0;
241577745c05SJosef Bacik again:
241677745c05SJosef Bacik 	inode = lookup_free_space_inode(block_group, path);
241777745c05SJosef Bacik 	if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
241877745c05SJosef Bacik 		ret = PTR_ERR(inode);
241977745c05SJosef Bacik 		btrfs_release_path(path);
242077745c05SJosef Bacik 		goto out;
242177745c05SJosef Bacik 	}
242277745c05SJosef Bacik 
242377745c05SJosef Bacik 	if (IS_ERR(inode)) {
242477745c05SJosef Bacik 		BUG_ON(retries);
242577745c05SJosef Bacik 		retries++;
242677745c05SJosef Bacik 
242777745c05SJosef Bacik 		if (block_group->ro)
242877745c05SJosef Bacik 			goto out_free;
242977745c05SJosef Bacik 
243077745c05SJosef Bacik 		ret = create_free_space_inode(trans, block_group, path);
243177745c05SJosef Bacik 		if (ret)
243277745c05SJosef Bacik 			goto out_free;
243377745c05SJosef Bacik 		goto again;
243477745c05SJosef Bacik 	}
243577745c05SJosef Bacik 
243677745c05SJosef Bacik 	/*
243777745c05SJosef Bacik 	 * We want to set the generation to 0, that way if anything goes wrong
243877745c05SJosef Bacik 	 * from here on out we know not to trust this cache when we load up next
243977745c05SJosef Bacik 	 * time.
244077745c05SJosef Bacik 	 */
244177745c05SJosef Bacik 	BTRFS_I(inode)->generation = 0;
244277745c05SJosef Bacik 	ret = btrfs_update_inode(trans, root, inode);
244377745c05SJosef Bacik 	if (ret) {
244477745c05SJosef Bacik 		/*
244577745c05SJosef Bacik 		 * So theoretically we could recover from this, simply set the
244677745c05SJosef Bacik 		 * super cache generation to 0 so we know to invalidate the
244777745c05SJosef Bacik 		 * cache, but then we'd have to keep track of the block groups
244877745c05SJosef Bacik 		 * that fail this way so we know we _have_ to reset this cache
244977745c05SJosef Bacik 		 * before the next commit or risk reading stale cache.  So to
245077745c05SJosef Bacik 		 * limit our exposure to horrible edge cases lets just abort the
245177745c05SJosef Bacik 		 * transaction, this only happens in really bad situations
245277745c05SJosef Bacik 		 * anyway.
245377745c05SJosef Bacik 		 */
245477745c05SJosef Bacik 		btrfs_abort_transaction(trans, ret);
245577745c05SJosef Bacik 		goto out_put;
245677745c05SJosef Bacik 	}
245777745c05SJosef Bacik 	WARN_ON(ret);
245877745c05SJosef Bacik 
245977745c05SJosef Bacik 	/* We've already setup this transaction, go ahead and exit */
246077745c05SJosef Bacik 	if (block_group->cache_generation == trans->transid &&
246177745c05SJosef Bacik 	    i_size_read(inode)) {
246277745c05SJosef Bacik 		dcs = BTRFS_DC_SETUP;
246377745c05SJosef Bacik 		goto out_put;
246477745c05SJosef Bacik 	}
246577745c05SJosef Bacik 
246677745c05SJosef Bacik 	if (i_size_read(inode) > 0) {
246777745c05SJosef Bacik 		ret = btrfs_check_trunc_cache_free_space(fs_info,
246877745c05SJosef Bacik 					&fs_info->global_block_rsv);
246977745c05SJosef Bacik 		if (ret)
247077745c05SJosef Bacik 			goto out_put;
247177745c05SJosef Bacik 
247277745c05SJosef Bacik 		ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
247377745c05SJosef Bacik 		if (ret)
247477745c05SJosef Bacik 			goto out_put;
247577745c05SJosef Bacik 	}
247677745c05SJosef Bacik 
247777745c05SJosef Bacik 	spin_lock(&block_group->lock);
247877745c05SJosef Bacik 	if (block_group->cached != BTRFS_CACHE_FINISHED ||
247977745c05SJosef Bacik 	    !btrfs_test_opt(fs_info, SPACE_CACHE)) {
248077745c05SJosef Bacik 		/*
248177745c05SJosef Bacik 		 * don't bother trying to write stuff out _if_
248277745c05SJosef Bacik 		 * a) we're not cached,
248377745c05SJosef Bacik 		 * b) we're with nospace_cache mount option,
248477745c05SJosef Bacik 		 * c) we're with v2 space_cache (FREE_SPACE_TREE).
248577745c05SJosef Bacik 		 */
248677745c05SJosef Bacik 		dcs = BTRFS_DC_WRITTEN;
248777745c05SJosef Bacik 		spin_unlock(&block_group->lock);
248877745c05SJosef Bacik 		goto out_put;
248977745c05SJosef Bacik 	}
249077745c05SJosef Bacik 	spin_unlock(&block_group->lock);
249177745c05SJosef Bacik 
249277745c05SJosef Bacik 	/*
249377745c05SJosef Bacik 	 * We hit an ENOSPC when setting up the cache in this transaction, just
249477745c05SJosef Bacik 	 * skip doing the setup, we've already cleared the cache so we're safe.
249577745c05SJosef Bacik 	 */
249677745c05SJosef Bacik 	if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
249777745c05SJosef Bacik 		ret = -ENOSPC;
249877745c05SJosef Bacik 		goto out_put;
249977745c05SJosef Bacik 	}
250077745c05SJosef Bacik 
250177745c05SJosef Bacik 	/*
250277745c05SJosef Bacik 	 * Try to preallocate enough space based on how big the block group is.
250377745c05SJosef Bacik 	 * Keep in mind this has to include any pinned space which could end up
250477745c05SJosef Bacik 	 * taking up quite a bit since it's not folded into the other space
250577745c05SJosef Bacik 	 * cache.
250677745c05SJosef Bacik 	 */
2507b3470b5dSDavid Sterba 	num_pages = div_u64(block_group->length, SZ_256M);
250877745c05SJosef Bacik 	if (!num_pages)
250977745c05SJosef Bacik 		num_pages = 1;
251077745c05SJosef Bacik 
251177745c05SJosef Bacik 	num_pages *= 16;
251277745c05SJosef Bacik 	num_pages *= PAGE_SIZE;
251377745c05SJosef Bacik 
251477745c05SJosef Bacik 	ret = btrfs_check_data_free_space(inode, &data_reserved, 0, num_pages);
251577745c05SJosef Bacik 	if (ret)
251677745c05SJosef Bacik 		goto out_put;
251777745c05SJosef Bacik 
251877745c05SJosef Bacik 	ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
251977745c05SJosef Bacik 					      num_pages, num_pages,
252077745c05SJosef Bacik 					      &alloc_hint);
252177745c05SJosef Bacik 	/*
252277745c05SJosef Bacik 	 * Our cache requires contiguous chunks so that we don't modify a bunch
252377745c05SJosef Bacik 	 * of metadata or split extents when writing the cache out, which means
252477745c05SJosef Bacik 	 * we can enospc if we are heavily fragmented in addition to just normal
252577745c05SJosef Bacik 	 * out of space conditions.  So if we hit this just skip setting up any
252677745c05SJosef Bacik 	 * other block groups for this transaction, maybe we'll unpin enough
252777745c05SJosef Bacik 	 * space the next time around.
252877745c05SJosef Bacik 	 */
252977745c05SJosef Bacik 	if (!ret)
253077745c05SJosef Bacik 		dcs = BTRFS_DC_SETUP;
253177745c05SJosef Bacik 	else if (ret == -ENOSPC)
253277745c05SJosef Bacik 		set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
253377745c05SJosef Bacik 
253477745c05SJosef Bacik out_put:
253577745c05SJosef Bacik 	iput(inode);
253677745c05SJosef Bacik out_free:
253777745c05SJosef Bacik 	btrfs_release_path(path);
253877745c05SJosef Bacik out:
253977745c05SJosef Bacik 	spin_lock(&block_group->lock);
254077745c05SJosef Bacik 	if (!ret && dcs == BTRFS_DC_SETUP)
254177745c05SJosef Bacik 		block_group->cache_generation = trans->transid;
254277745c05SJosef Bacik 	block_group->disk_cache_state = dcs;
254377745c05SJosef Bacik 	spin_unlock(&block_group->lock);
254477745c05SJosef Bacik 
254577745c05SJosef Bacik 	extent_changeset_free(data_reserved);
254677745c05SJosef Bacik 	return ret;
254777745c05SJosef Bacik }
254877745c05SJosef Bacik 
254977745c05SJosef Bacik int btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
255077745c05SJosef Bacik {
255177745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
255232da5386SDavid Sterba 	struct btrfs_block_group *cache, *tmp;
255377745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
255477745c05SJosef Bacik 	struct btrfs_path *path;
255577745c05SJosef Bacik 
255677745c05SJosef Bacik 	if (list_empty(&cur_trans->dirty_bgs) ||
255777745c05SJosef Bacik 	    !btrfs_test_opt(fs_info, SPACE_CACHE))
255877745c05SJosef Bacik 		return 0;
255977745c05SJosef Bacik 
256077745c05SJosef Bacik 	path = btrfs_alloc_path();
256177745c05SJosef Bacik 	if (!path)
256277745c05SJosef Bacik 		return -ENOMEM;
256377745c05SJosef Bacik 
256477745c05SJosef Bacik 	/* Could add new block groups, use _safe just in case */
256577745c05SJosef Bacik 	list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
256677745c05SJosef Bacik 				 dirty_list) {
256777745c05SJosef Bacik 		if (cache->disk_cache_state == BTRFS_DC_CLEAR)
256877745c05SJosef Bacik 			cache_save_setup(cache, trans, path);
256977745c05SJosef Bacik 	}
257077745c05SJosef Bacik 
257177745c05SJosef Bacik 	btrfs_free_path(path);
257277745c05SJosef Bacik 	return 0;
257377745c05SJosef Bacik }
257477745c05SJosef Bacik 
257577745c05SJosef Bacik /*
257677745c05SJosef Bacik  * Transaction commit does final block group cache writeback during a critical
257777745c05SJosef Bacik  * section where nothing is allowed to change the FS.  This is required in
257877745c05SJosef Bacik  * order for the cache to actually match the block group, but can introduce a
257977745c05SJosef Bacik  * lot of latency into the commit.
258077745c05SJosef Bacik  *
258177745c05SJosef Bacik  * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
258277745c05SJosef Bacik  * There's a chance we'll have to redo some of it if the block group changes
258377745c05SJosef Bacik  * again during the commit, but it greatly reduces the commit latency by
258477745c05SJosef Bacik  * getting rid of the easy block groups while we're still allowing others to
258577745c05SJosef Bacik  * join the commit.
258677745c05SJosef Bacik  */
258777745c05SJosef Bacik int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
258877745c05SJosef Bacik {
258977745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
259032da5386SDavid Sterba 	struct btrfs_block_group *cache;
259177745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
259277745c05SJosef Bacik 	int ret = 0;
259377745c05SJosef Bacik 	int should_put;
259477745c05SJosef Bacik 	struct btrfs_path *path = NULL;
259577745c05SJosef Bacik 	LIST_HEAD(dirty);
259677745c05SJosef Bacik 	struct list_head *io = &cur_trans->io_bgs;
259777745c05SJosef Bacik 	int num_started = 0;
259877745c05SJosef Bacik 	int loops = 0;
259977745c05SJosef Bacik 
260077745c05SJosef Bacik 	spin_lock(&cur_trans->dirty_bgs_lock);
260177745c05SJosef Bacik 	if (list_empty(&cur_trans->dirty_bgs)) {
260277745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
260377745c05SJosef Bacik 		return 0;
260477745c05SJosef Bacik 	}
260577745c05SJosef Bacik 	list_splice_init(&cur_trans->dirty_bgs, &dirty);
260677745c05SJosef Bacik 	spin_unlock(&cur_trans->dirty_bgs_lock);
260777745c05SJosef Bacik 
260877745c05SJosef Bacik again:
260977745c05SJosef Bacik 	/* Make sure all the block groups on our dirty list actually exist */
261077745c05SJosef Bacik 	btrfs_create_pending_block_groups(trans);
261177745c05SJosef Bacik 
261277745c05SJosef Bacik 	if (!path) {
261377745c05SJosef Bacik 		path = btrfs_alloc_path();
261477745c05SJosef Bacik 		if (!path)
261577745c05SJosef Bacik 			return -ENOMEM;
261677745c05SJosef Bacik 	}
261777745c05SJosef Bacik 
261877745c05SJosef Bacik 	/*
261977745c05SJosef Bacik 	 * cache_write_mutex is here only to save us from balance or automatic
262077745c05SJosef Bacik 	 * removal of empty block groups deleting this block group while we are
262177745c05SJosef Bacik 	 * writing out the cache
262277745c05SJosef Bacik 	 */
262377745c05SJosef Bacik 	mutex_lock(&trans->transaction->cache_write_mutex);
262477745c05SJosef Bacik 	while (!list_empty(&dirty)) {
262577745c05SJosef Bacik 		bool drop_reserve = true;
262677745c05SJosef Bacik 
262732da5386SDavid Sterba 		cache = list_first_entry(&dirty, struct btrfs_block_group,
262877745c05SJosef Bacik 					 dirty_list);
262977745c05SJosef Bacik 		/*
263077745c05SJosef Bacik 		 * This can happen if something re-dirties a block group that
263177745c05SJosef Bacik 		 * is already under IO.  Just wait for it to finish and then do
263277745c05SJosef Bacik 		 * it all again
263377745c05SJosef Bacik 		 */
263477745c05SJosef Bacik 		if (!list_empty(&cache->io_list)) {
263577745c05SJosef Bacik 			list_del_init(&cache->io_list);
263677745c05SJosef Bacik 			btrfs_wait_cache_io(trans, cache, path);
263777745c05SJosef Bacik 			btrfs_put_block_group(cache);
263877745c05SJosef Bacik 		}
263977745c05SJosef Bacik 
264077745c05SJosef Bacik 
264177745c05SJosef Bacik 		/*
264277745c05SJosef Bacik 		 * btrfs_wait_cache_io uses the cache->dirty_list to decide if
264377745c05SJosef Bacik 		 * it should update the cache_state.  Don't delete until after
264477745c05SJosef Bacik 		 * we wait.
264577745c05SJosef Bacik 		 *
264677745c05SJosef Bacik 		 * Since we're not running in the commit critical section
264777745c05SJosef Bacik 		 * we need the dirty_bgs_lock to protect from update_block_group
264877745c05SJosef Bacik 		 */
264977745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
265077745c05SJosef Bacik 		list_del_init(&cache->dirty_list);
265177745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
265277745c05SJosef Bacik 
265377745c05SJosef Bacik 		should_put = 1;
265477745c05SJosef Bacik 
265577745c05SJosef Bacik 		cache_save_setup(cache, trans, path);
265677745c05SJosef Bacik 
265777745c05SJosef Bacik 		if (cache->disk_cache_state == BTRFS_DC_SETUP) {
265877745c05SJosef Bacik 			cache->io_ctl.inode = NULL;
265977745c05SJosef Bacik 			ret = btrfs_write_out_cache(trans, cache, path);
266077745c05SJosef Bacik 			if (ret == 0 && cache->io_ctl.inode) {
266177745c05SJosef Bacik 				num_started++;
266277745c05SJosef Bacik 				should_put = 0;
266377745c05SJosef Bacik 
266477745c05SJosef Bacik 				/*
266577745c05SJosef Bacik 				 * The cache_write_mutex is protecting the
266677745c05SJosef Bacik 				 * io_list, also refer to the definition of
266777745c05SJosef Bacik 				 * btrfs_transaction::io_bgs for more details
266877745c05SJosef Bacik 				 */
266977745c05SJosef Bacik 				list_add_tail(&cache->io_list, io);
267077745c05SJosef Bacik 			} else {
267177745c05SJosef Bacik 				/*
267277745c05SJosef Bacik 				 * If we failed to write the cache, the
267377745c05SJosef Bacik 				 * generation will be bad and life goes on
267477745c05SJosef Bacik 				 */
267577745c05SJosef Bacik 				ret = 0;
267677745c05SJosef Bacik 			}
267777745c05SJosef Bacik 		}
267877745c05SJosef Bacik 		if (!ret) {
26793be4d8efSQu Wenruo 			ret = update_block_group_item(trans, path, cache);
268077745c05SJosef Bacik 			/*
268177745c05SJosef Bacik 			 * Our block group might still be attached to the list
268277745c05SJosef Bacik 			 * of new block groups in the transaction handle of some
268377745c05SJosef Bacik 			 * other task (struct btrfs_trans_handle->new_bgs). This
268477745c05SJosef Bacik 			 * means its block group item isn't yet in the extent
268577745c05SJosef Bacik 			 * tree. If this happens ignore the error, as we will
268677745c05SJosef Bacik 			 * try again later in the critical section of the
268777745c05SJosef Bacik 			 * transaction commit.
268877745c05SJosef Bacik 			 */
268977745c05SJosef Bacik 			if (ret == -ENOENT) {
269077745c05SJosef Bacik 				ret = 0;
269177745c05SJosef Bacik 				spin_lock(&cur_trans->dirty_bgs_lock);
269277745c05SJosef Bacik 				if (list_empty(&cache->dirty_list)) {
269377745c05SJosef Bacik 					list_add_tail(&cache->dirty_list,
269477745c05SJosef Bacik 						      &cur_trans->dirty_bgs);
269577745c05SJosef Bacik 					btrfs_get_block_group(cache);
269677745c05SJosef Bacik 					drop_reserve = false;
269777745c05SJosef Bacik 				}
269877745c05SJosef Bacik 				spin_unlock(&cur_trans->dirty_bgs_lock);
269977745c05SJosef Bacik 			} else if (ret) {
270077745c05SJosef Bacik 				btrfs_abort_transaction(trans, ret);
270177745c05SJosef Bacik 			}
270277745c05SJosef Bacik 		}
270377745c05SJosef Bacik 
270477745c05SJosef Bacik 		/* If it's not on the io list, we need to put the block group */
270577745c05SJosef Bacik 		if (should_put)
270677745c05SJosef Bacik 			btrfs_put_block_group(cache);
270777745c05SJosef Bacik 		if (drop_reserve)
270877745c05SJosef Bacik 			btrfs_delayed_refs_rsv_release(fs_info, 1);
270977745c05SJosef Bacik 
271077745c05SJosef Bacik 		if (ret)
271177745c05SJosef Bacik 			break;
271277745c05SJosef Bacik 
271377745c05SJosef Bacik 		/*
271477745c05SJosef Bacik 		 * Avoid blocking other tasks for too long. It might even save
271577745c05SJosef Bacik 		 * us from writing caches for block groups that are going to be
271677745c05SJosef Bacik 		 * removed.
271777745c05SJosef Bacik 		 */
271877745c05SJosef Bacik 		mutex_unlock(&trans->transaction->cache_write_mutex);
271977745c05SJosef Bacik 		mutex_lock(&trans->transaction->cache_write_mutex);
272077745c05SJosef Bacik 	}
272177745c05SJosef Bacik 	mutex_unlock(&trans->transaction->cache_write_mutex);
272277745c05SJosef Bacik 
272377745c05SJosef Bacik 	/*
272477745c05SJosef Bacik 	 * Go through delayed refs for all the stuff we've just kicked off
272577745c05SJosef Bacik 	 * and then loop back (just once)
272677745c05SJosef Bacik 	 */
272777745c05SJosef Bacik 	ret = btrfs_run_delayed_refs(trans, 0);
272877745c05SJosef Bacik 	if (!ret && loops == 0) {
272977745c05SJosef Bacik 		loops++;
273077745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
273177745c05SJosef Bacik 		list_splice_init(&cur_trans->dirty_bgs, &dirty);
273277745c05SJosef Bacik 		/*
273377745c05SJosef Bacik 		 * dirty_bgs_lock protects us from concurrent block group
273477745c05SJosef Bacik 		 * deletes too (not just cache_write_mutex).
273577745c05SJosef Bacik 		 */
273677745c05SJosef Bacik 		if (!list_empty(&dirty)) {
273777745c05SJosef Bacik 			spin_unlock(&cur_trans->dirty_bgs_lock);
273877745c05SJosef Bacik 			goto again;
273977745c05SJosef Bacik 		}
274077745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
274177745c05SJosef Bacik 	} else if (ret < 0) {
274277745c05SJosef Bacik 		btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
274377745c05SJosef Bacik 	}
274477745c05SJosef Bacik 
274577745c05SJosef Bacik 	btrfs_free_path(path);
274677745c05SJosef Bacik 	return ret;
274777745c05SJosef Bacik }
274877745c05SJosef Bacik 
274977745c05SJosef Bacik int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
275077745c05SJosef Bacik {
275177745c05SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
275232da5386SDavid Sterba 	struct btrfs_block_group *cache;
275377745c05SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
275477745c05SJosef Bacik 	int ret = 0;
275577745c05SJosef Bacik 	int should_put;
275677745c05SJosef Bacik 	struct btrfs_path *path;
275777745c05SJosef Bacik 	struct list_head *io = &cur_trans->io_bgs;
275877745c05SJosef Bacik 	int num_started = 0;
275977745c05SJosef Bacik 
276077745c05SJosef Bacik 	path = btrfs_alloc_path();
276177745c05SJosef Bacik 	if (!path)
276277745c05SJosef Bacik 		return -ENOMEM;
276377745c05SJosef Bacik 
276477745c05SJosef Bacik 	/*
276577745c05SJosef Bacik 	 * Even though we are in the critical section of the transaction commit,
276677745c05SJosef Bacik 	 * we can still have concurrent tasks adding elements to this
276777745c05SJosef Bacik 	 * transaction's list of dirty block groups. These tasks correspond to
276877745c05SJosef Bacik 	 * endio free space workers started when writeback finishes for a
276977745c05SJosef Bacik 	 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
277077745c05SJosef Bacik 	 * allocate new block groups as a result of COWing nodes of the root
277177745c05SJosef Bacik 	 * tree when updating the free space inode. The writeback for the space
277277745c05SJosef Bacik 	 * caches is triggered by an earlier call to
277377745c05SJosef Bacik 	 * btrfs_start_dirty_block_groups() and iterations of the following
277477745c05SJosef Bacik 	 * loop.
277577745c05SJosef Bacik 	 * Also we want to do the cache_save_setup first and then run the
277677745c05SJosef Bacik 	 * delayed refs to make sure we have the best chance at doing this all
277777745c05SJosef Bacik 	 * in one shot.
277877745c05SJosef Bacik 	 */
277977745c05SJosef Bacik 	spin_lock(&cur_trans->dirty_bgs_lock);
278077745c05SJosef Bacik 	while (!list_empty(&cur_trans->dirty_bgs)) {
278177745c05SJosef Bacik 		cache = list_first_entry(&cur_trans->dirty_bgs,
278232da5386SDavid Sterba 					 struct btrfs_block_group,
278377745c05SJosef Bacik 					 dirty_list);
278477745c05SJosef Bacik 
278577745c05SJosef Bacik 		/*
278677745c05SJosef Bacik 		 * This can happen if cache_save_setup re-dirties a block group
278777745c05SJosef Bacik 		 * that is already under IO.  Just wait for it to finish and
278877745c05SJosef Bacik 		 * then do it all again
278977745c05SJosef Bacik 		 */
279077745c05SJosef Bacik 		if (!list_empty(&cache->io_list)) {
279177745c05SJosef Bacik 			spin_unlock(&cur_trans->dirty_bgs_lock);
279277745c05SJosef Bacik 			list_del_init(&cache->io_list);
279377745c05SJosef Bacik 			btrfs_wait_cache_io(trans, cache, path);
279477745c05SJosef Bacik 			btrfs_put_block_group(cache);
279577745c05SJosef Bacik 			spin_lock(&cur_trans->dirty_bgs_lock);
279677745c05SJosef Bacik 		}
279777745c05SJosef Bacik 
279877745c05SJosef Bacik 		/*
279977745c05SJosef Bacik 		 * Don't remove from the dirty list until after we've waited on
280077745c05SJosef Bacik 		 * any pending IO
280177745c05SJosef Bacik 		 */
280277745c05SJosef Bacik 		list_del_init(&cache->dirty_list);
280377745c05SJosef Bacik 		spin_unlock(&cur_trans->dirty_bgs_lock);
280477745c05SJosef Bacik 		should_put = 1;
280577745c05SJosef Bacik 
280677745c05SJosef Bacik 		cache_save_setup(cache, trans, path);
280777745c05SJosef Bacik 
280877745c05SJosef Bacik 		if (!ret)
280977745c05SJosef Bacik 			ret = btrfs_run_delayed_refs(trans,
281077745c05SJosef Bacik 						     (unsigned long) -1);
281177745c05SJosef Bacik 
281277745c05SJosef Bacik 		if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
281377745c05SJosef Bacik 			cache->io_ctl.inode = NULL;
281477745c05SJosef Bacik 			ret = btrfs_write_out_cache(trans, cache, path);
281577745c05SJosef Bacik 			if (ret == 0 && cache->io_ctl.inode) {
281677745c05SJosef Bacik 				num_started++;
281777745c05SJosef Bacik 				should_put = 0;
281877745c05SJosef Bacik 				list_add_tail(&cache->io_list, io);
281977745c05SJosef Bacik 			} else {
282077745c05SJosef Bacik 				/*
282177745c05SJosef Bacik 				 * If we failed to write the cache, the
282277745c05SJosef Bacik 				 * generation will be bad and life goes on
282377745c05SJosef Bacik 				 */
282477745c05SJosef Bacik 				ret = 0;
282577745c05SJosef Bacik 			}
282677745c05SJosef Bacik 		}
282777745c05SJosef Bacik 		if (!ret) {
28283be4d8efSQu Wenruo 			ret = update_block_group_item(trans, path, cache);
282977745c05SJosef Bacik 			/*
283077745c05SJosef Bacik 			 * One of the free space endio workers might have
283177745c05SJosef Bacik 			 * created a new block group while updating a free space
283277745c05SJosef Bacik 			 * cache's inode (at inode.c:btrfs_finish_ordered_io())
283377745c05SJosef Bacik 			 * and hasn't released its transaction handle yet, in
283477745c05SJosef Bacik 			 * which case the new block group is still attached to
283577745c05SJosef Bacik 			 * its transaction handle and its creation has not
283677745c05SJosef Bacik 			 * finished yet (no block group item in the extent tree
283777745c05SJosef Bacik 			 * yet, etc). If this is the case, wait for all free
283877745c05SJosef Bacik 			 * space endio workers to finish and retry. This is a
283977745c05SJosef Bacik 			 * a very rare case so no need for a more efficient and
284077745c05SJosef Bacik 			 * complex approach.
284177745c05SJosef Bacik 			 */
284277745c05SJosef Bacik 			if (ret == -ENOENT) {
284377745c05SJosef Bacik 				wait_event(cur_trans->writer_wait,
284477745c05SJosef Bacik 				   atomic_read(&cur_trans->num_writers) == 1);
28453be4d8efSQu Wenruo 				ret = update_block_group_item(trans, path, cache);
284677745c05SJosef Bacik 			}
284777745c05SJosef Bacik 			if (ret)
284877745c05SJosef Bacik 				btrfs_abort_transaction(trans, ret);
284977745c05SJosef Bacik 		}
285077745c05SJosef Bacik 
285177745c05SJosef Bacik 		/* If its not on the io list, we need to put the block group */
285277745c05SJosef Bacik 		if (should_put)
285377745c05SJosef Bacik 			btrfs_put_block_group(cache);
285477745c05SJosef Bacik 		btrfs_delayed_refs_rsv_release(fs_info, 1);
285577745c05SJosef Bacik 		spin_lock(&cur_trans->dirty_bgs_lock);
285677745c05SJosef Bacik 	}
285777745c05SJosef Bacik 	spin_unlock(&cur_trans->dirty_bgs_lock);
285877745c05SJosef Bacik 
285977745c05SJosef Bacik 	/*
286077745c05SJosef Bacik 	 * Refer to the definition of io_bgs member for details why it's safe
286177745c05SJosef Bacik 	 * to use it without any locking
286277745c05SJosef Bacik 	 */
286377745c05SJosef Bacik 	while (!list_empty(io)) {
286432da5386SDavid Sterba 		cache = list_first_entry(io, struct btrfs_block_group,
286577745c05SJosef Bacik 					 io_list);
286677745c05SJosef Bacik 		list_del_init(&cache->io_list);
286777745c05SJosef Bacik 		btrfs_wait_cache_io(trans, cache, path);
286877745c05SJosef Bacik 		btrfs_put_block_group(cache);
286977745c05SJosef Bacik 	}
287077745c05SJosef Bacik 
287177745c05SJosef Bacik 	btrfs_free_path(path);
287277745c05SJosef Bacik 	return ret;
287377745c05SJosef Bacik }
2874606d1bf1SJosef Bacik 
2875606d1bf1SJosef Bacik int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2876606d1bf1SJosef Bacik 			     u64 bytenr, u64 num_bytes, int alloc)
2877606d1bf1SJosef Bacik {
2878606d1bf1SJosef Bacik 	struct btrfs_fs_info *info = trans->fs_info;
287932da5386SDavid Sterba 	struct btrfs_block_group *cache = NULL;
2880606d1bf1SJosef Bacik 	u64 total = num_bytes;
2881606d1bf1SJosef Bacik 	u64 old_val;
2882606d1bf1SJosef Bacik 	u64 byte_in_group;
2883606d1bf1SJosef Bacik 	int factor;
2884606d1bf1SJosef Bacik 	int ret = 0;
2885606d1bf1SJosef Bacik 
2886606d1bf1SJosef Bacik 	/* Block accounting for super block */
2887606d1bf1SJosef Bacik 	spin_lock(&info->delalloc_root_lock);
2888606d1bf1SJosef Bacik 	old_val = btrfs_super_bytes_used(info->super_copy);
2889606d1bf1SJosef Bacik 	if (alloc)
2890606d1bf1SJosef Bacik 		old_val += num_bytes;
2891606d1bf1SJosef Bacik 	else
2892606d1bf1SJosef Bacik 		old_val -= num_bytes;
2893606d1bf1SJosef Bacik 	btrfs_set_super_bytes_used(info->super_copy, old_val);
2894606d1bf1SJosef Bacik 	spin_unlock(&info->delalloc_root_lock);
2895606d1bf1SJosef Bacik 
2896606d1bf1SJosef Bacik 	while (total) {
2897606d1bf1SJosef Bacik 		cache = btrfs_lookup_block_group(info, bytenr);
2898606d1bf1SJosef Bacik 		if (!cache) {
2899606d1bf1SJosef Bacik 			ret = -ENOENT;
2900606d1bf1SJosef Bacik 			break;
2901606d1bf1SJosef Bacik 		}
2902606d1bf1SJosef Bacik 		factor = btrfs_bg_type_to_factor(cache->flags);
2903606d1bf1SJosef Bacik 
2904606d1bf1SJosef Bacik 		/*
2905606d1bf1SJosef Bacik 		 * If this block group has free space cache written out, we
2906606d1bf1SJosef Bacik 		 * need to make sure to load it if we are removing space.  This
2907606d1bf1SJosef Bacik 		 * is because we need the unpinning stage to actually add the
2908606d1bf1SJosef Bacik 		 * space back to the block group, otherwise we will leak space.
2909606d1bf1SJosef Bacik 		 */
291032da5386SDavid Sterba 		if (!alloc && !btrfs_block_group_done(cache))
2911606d1bf1SJosef Bacik 			btrfs_cache_block_group(cache, 1);
2912606d1bf1SJosef Bacik 
2913b3470b5dSDavid Sterba 		byte_in_group = bytenr - cache->start;
2914b3470b5dSDavid Sterba 		WARN_ON(byte_in_group > cache->length);
2915606d1bf1SJosef Bacik 
2916606d1bf1SJosef Bacik 		spin_lock(&cache->space_info->lock);
2917606d1bf1SJosef Bacik 		spin_lock(&cache->lock);
2918606d1bf1SJosef Bacik 
2919606d1bf1SJosef Bacik 		if (btrfs_test_opt(info, SPACE_CACHE) &&
2920606d1bf1SJosef Bacik 		    cache->disk_cache_state < BTRFS_DC_CLEAR)
2921606d1bf1SJosef Bacik 			cache->disk_cache_state = BTRFS_DC_CLEAR;
2922606d1bf1SJosef Bacik 
2923bf38be65SDavid Sterba 		old_val = cache->used;
2924b3470b5dSDavid Sterba 		num_bytes = min(total, cache->length - byte_in_group);
2925606d1bf1SJosef Bacik 		if (alloc) {
2926606d1bf1SJosef Bacik 			old_val += num_bytes;
2927bf38be65SDavid Sterba 			cache->used = old_val;
2928606d1bf1SJosef Bacik 			cache->reserved -= num_bytes;
2929606d1bf1SJosef Bacik 			cache->space_info->bytes_reserved -= num_bytes;
2930606d1bf1SJosef Bacik 			cache->space_info->bytes_used += num_bytes;
2931606d1bf1SJosef Bacik 			cache->space_info->disk_used += num_bytes * factor;
2932606d1bf1SJosef Bacik 			spin_unlock(&cache->lock);
2933606d1bf1SJosef Bacik 			spin_unlock(&cache->space_info->lock);
2934606d1bf1SJosef Bacik 		} else {
2935606d1bf1SJosef Bacik 			old_val -= num_bytes;
2936bf38be65SDavid Sterba 			cache->used = old_val;
2937606d1bf1SJosef Bacik 			cache->pinned += num_bytes;
2938606d1bf1SJosef Bacik 			btrfs_space_info_update_bytes_pinned(info,
2939606d1bf1SJosef Bacik 					cache->space_info, num_bytes);
2940606d1bf1SJosef Bacik 			cache->space_info->bytes_used -= num_bytes;
2941606d1bf1SJosef Bacik 			cache->space_info->disk_used -= num_bytes * factor;
2942606d1bf1SJosef Bacik 			spin_unlock(&cache->lock);
2943606d1bf1SJosef Bacik 			spin_unlock(&cache->space_info->lock);
2944606d1bf1SJosef Bacik 
2945606d1bf1SJosef Bacik 			percpu_counter_add_batch(
2946606d1bf1SJosef Bacik 					&cache->space_info->total_bytes_pinned,
2947606d1bf1SJosef Bacik 					num_bytes,
2948606d1bf1SJosef Bacik 					BTRFS_TOTAL_BYTES_PINNED_BATCH);
2949fe119a6eSNikolay Borisov 			set_extent_dirty(&trans->transaction->pinned_extents,
2950606d1bf1SJosef Bacik 					 bytenr, bytenr + num_bytes - 1,
2951606d1bf1SJosef Bacik 					 GFP_NOFS | __GFP_NOFAIL);
2952606d1bf1SJosef Bacik 		}
2953606d1bf1SJosef Bacik 
2954606d1bf1SJosef Bacik 		spin_lock(&trans->transaction->dirty_bgs_lock);
2955606d1bf1SJosef Bacik 		if (list_empty(&cache->dirty_list)) {
2956606d1bf1SJosef Bacik 			list_add_tail(&cache->dirty_list,
2957606d1bf1SJosef Bacik 				      &trans->transaction->dirty_bgs);
2958606d1bf1SJosef Bacik 			trans->delayed_ref_updates++;
2959606d1bf1SJosef Bacik 			btrfs_get_block_group(cache);
2960606d1bf1SJosef Bacik 		}
2961606d1bf1SJosef Bacik 		spin_unlock(&trans->transaction->dirty_bgs_lock);
2962606d1bf1SJosef Bacik 
2963606d1bf1SJosef Bacik 		/*
2964606d1bf1SJosef Bacik 		 * No longer have used bytes in this block group, queue it for
2965606d1bf1SJosef Bacik 		 * deletion. We do this after adding the block group to the
2966606d1bf1SJosef Bacik 		 * dirty list to avoid races between cleaner kthread and space
2967606d1bf1SJosef Bacik 		 * cache writeout.
2968606d1bf1SJosef Bacik 		 */
29696e80d4f8SDennis Zhou 		if (!alloc && old_val == 0) {
29706e80d4f8SDennis Zhou 			if (!btrfs_test_opt(info, DISCARD_ASYNC))
2971606d1bf1SJosef Bacik 				btrfs_mark_bg_unused(cache);
29726e80d4f8SDennis Zhou 		}
2973606d1bf1SJosef Bacik 
2974606d1bf1SJosef Bacik 		btrfs_put_block_group(cache);
2975606d1bf1SJosef Bacik 		total -= num_bytes;
2976606d1bf1SJosef Bacik 		bytenr += num_bytes;
2977606d1bf1SJosef Bacik 	}
2978606d1bf1SJosef Bacik 
2979606d1bf1SJosef Bacik 	/* Modified block groups are accounted for in the delayed_refs_rsv. */
2980606d1bf1SJosef Bacik 	btrfs_update_delayed_refs_rsv(trans);
2981606d1bf1SJosef Bacik 	return ret;
2982606d1bf1SJosef Bacik }
2983606d1bf1SJosef Bacik 
2984606d1bf1SJosef Bacik /**
2985606d1bf1SJosef Bacik  * btrfs_add_reserved_bytes - update the block_group and space info counters
2986606d1bf1SJosef Bacik  * @cache:	The cache we are manipulating
2987606d1bf1SJosef Bacik  * @ram_bytes:  The number of bytes of file content, and will be same to
2988606d1bf1SJosef Bacik  *              @num_bytes except for the compress path.
2989606d1bf1SJosef Bacik  * @num_bytes:	The number of bytes in question
2990606d1bf1SJosef Bacik  * @delalloc:   The blocks are allocated for the delalloc write
2991606d1bf1SJosef Bacik  *
2992606d1bf1SJosef Bacik  * This is called by the allocator when it reserves space. If this is a
2993606d1bf1SJosef Bacik  * reservation and the block group has become read only we cannot make the
2994606d1bf1SJosef Bacik  * reservation and return -EAGAIN, otherwise this function always succeeds.
2995606d1bf1SJosef Bacik  */
299632da5386SDavid Sterba int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
2997606d1bf1SJosef Bacik 			     u64 ram_bytes, u64 num_bytes, int delalloc)
2998606d1bf1SJosef Bacik {
2999606d1bf1SJosef Bacik 	struct btrfs_space_info *space_info = cache->space_info;
3000606d1bf1SJosef Bacik 	int ret = 0;
3001606d1bf1SJosef Bacik 
3002606d1bf1SJosef Bacik 	spin_lock(&space_info->lock);
3003606d1bf1SJosef Bacik 	spin_lock(&cache->lock);
3004606d1bf1SJosef Bacik 	if (cache->ro) {
3005606d1bf1SJosef Bacik 		ret = -EAGAIN;
3006606d1bf1SJosef Bacik 	} else {
3007606d1bf1SJosef Bacik 		cache->reserved += num_bytes;
3008606d1bf1SJosef Bacik 		space_info->bytes_reserved += num_bytes;
3009a43c3835SJosef Bacik 		trace_btrfs_space_reservation(cache->fs_info, "space_info",
3010a43c3835SJosef Bacik 					      space_info->flags, num_bytes, 1);
3011606d1bf1SJosef Bacik 		btrfs_space_info_update_bytes_may_use(cache->fs_info,
3012606d1bf1SJosef Bacik 						      space_info, -ram_bytes);
3013606d1bf1SJosef Bacik 		if (delalloc)
3014606d1bf1SJosef Bacik 			cache->delalloc_bytes += num_bytes;
3015606d1bf1SJosef Bacik 	}
3016606d1bf1SJosef Bacik 	spin_unlock(&cache->lock);
3017606d1bf1SJosef Bacik 	spin_unlock(&space_info->lock);
3018606d1bf1SJosef Bacik 	return ret;
3019606d1bf1SJosef Bacik }
3020606d1bf1SJosef Bacik 
3021606d1bf1SJosef Bacik /**
3022606d1bf1SJosef Bacik  * btrfs_free_reserved_bytes - update the block_group and space info counters
3023606d1bf1SJosef Bacik  * @cache:      The cache we are manipulating
3024606d1bf1SJosef Bacik  * @num_bytes:  The number of bytes in question
3025606d1bf1SJosef Bacik  * @delalloc:   The blocks are allocated for the delalloc write
3026606d1bf1SJosef Bacik  *
3027606d1bf1SJosef Bacik  * This is called by somebody who is freeing space that was never actually used
3028606d1bf1SJosef Bacik  * on disk.  For example if you reserve some space for a new leaf in transaction
3029606d1bf1SJosef Bacik  * A and before transaction A commits you free that leaf, you call this with
3030606d1bf1SJosef Bacik  * reserve set to 0 in order to clear the reservation.
3031606d1bf1SJosef Bacik  */
303232da5386SDavid Sterba void btrfs_free_reserved_bytes(struct btrfs_block_group *cache,
3033606d1bf1SJosef Bacik 			       u64 num_bytes, int delalloc)
3034606d1bf1SJosef Bacik {
3035606d1bf1SJosef Bacik 	struct btrfs_space_info *space_info = cache->space_info;
3036606d1bf1SJosef Bacik 
3037606d1bf1SJosef Bacik 	spin_lock(&space_info->lock);
3038606d1bf1SJosef Bacik 	spin_lock(&cache->lock);
3039606d1bf1SJosef Bacik 	if (cache->ro)
3040606d1bf1SJosef Bacik 		space_info->bytes_readonly += num_bytes;
3041606d1bf1SJosef Bacik 	cache->reserved -= num_bytes;
3042606d1bf1SJosef Bacik 	space_info->bytes_reserved -= num_bytes;
3043606d1bf1SJosef Bacik 	space_info->max_extent_size = 0;
3044606d1bf1SJosef Bacik 
3045606d1bf1SJosef Bacik 	if (delalloc)
3046606d1bf1SJosef Bacik 		cache->delalloc_bytes -= num_bytes;
3047606d1bf1SJosef Bacik 	spin_unlock(&cache->lock);
3048606d1bf1SJosef Bacik 	spin_unlock(&space_info->lock);
3049606d1bf1SJosef Bacik }
305007730d87SJosef Bacik 
305107730d87SJosef Bacik static void force_metadata_allocation(struct btrfs_fs_info *info)
305207730d87SJosef Bacik {
305307730d87SJosef Bacik 	struct list_head *head = &info->space_info;
305407730d87SJosef Bacik 	struct btrfs_space_info *found;
305507730d87SJosef Bacik 
305607730d87SJosef Bacik 	rcu_read_lock();
305707730d87SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
305807730d87SJosef Bacik 		if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
305907730d87SJosef Bacik 			found->force_alloc = CHUNK_ALLOC_FORCE;
306007730d87SJosef Bacik 	}
306107730d87SJosef Bacik 	rcu_read_unlock();
306207730d87SJosef Bacik }
306307730d87SJosef Bacik 
306407730d87SJosef Bacik static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
306507730d87SJosef Bacik 			      struct btrfs_space_info *sinfo, int force)
306607730d87SJosef Bacik {
306707730d87SJosef Bacik 	u64 bytes_used = btrfs_space_info_used(sinfo, false);
306807730d87SJosef Bacik 	u64 thresh;
306907730d87SJosef Bacik 
307007730d87SJosef Bacik 	if (force == CHUNK_ALLOC_FORCE)
307107730d87SJosef Bacik 		return 1;
307207730d87SJosef Bacik 
307307730d87SJosef Bacik 	/*
307407730d87SJosef Bacik 	 * in limited mode, we want to have some free space up to
307507730d87SJosef Bacik 	 * about 1% of the FS size.
307607730d87SJosef Bacik 	 */
307707730d87SJosef Bacik 	if (force == CHUNK_ALLOC_LIMITED) {
307807730d87SJosef Bacik 		thresh = btrfs_super_total_bytes(fs_info->super_copy);
307907730d87SJosef Bacik 		thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
308007730d87SJosef Bacik 
308107730d87SJosef Bacik 		if (sinfo->total_bytes - bytes_used < thresh)
308207730d87SJosef Bacik 			return 1;
308307730d87SJosef Bacik 	}
308407730d87SJosef Bacik 
308507730d87SJosef Bacik 	if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
308607730d87SJosef Bacik 		return 0;
308707730d87SJosef Bacik 	return 1;
308807730d87SJosef Bacik }
308907730d87SJosef Bacik 
309007730d87SJosef Bacik int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
309107730d87SJosef Bacik {
309207730d87SJosef Bacik 	u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type);
309307730d87SJosef Bacik 
309407730d87SJosef Bacik 	return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
309507730d87SJosef Bacik }
309607730d87SJosef Bacik 
309707730d87SJosef Bacik /*
309807730d87SJosef Bacik  * If force is CHUNK_ALLOC_FORCE:
309907730d87SJosef Bacik  *    - return 1 if it successfully allocates a chunk,
310007730d87SJosef Bacik  *    - return errors including -ENOSPC otherwise.
310107730d87SJosef Bacik  * If force is NOT CHUNK_ALLOC_FORCE:
310207730d87SJosef Bacik  *    - return 0 if it doesn't need to allocate a new chunk,
310307730d87SJosef Bacik  *    - return 1 if it successfully allocates a chunk,
310407730d87SJosef Bacik  *    - return errors including -ENOSPC otherwise.
310507730d87SJosef Bacik  */
310607730d87SJosef Bacik int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
310707730d87SJosef Bacik 		      enum btrfs_chunk_alloc_enum force)
310807730d87SJosef Bacik {
310907730d87SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
311007730d87SJosef Bacik 	struct btrfs_space_info *space_info;
311107730d87SJosef Bacik 	bool wait_for_alloc = false;
311207730d87SJosef Bacik 	bool should_alloc = false;
311307730d87SJosef Bacik 	int ret = 0;
311407730d87SJosef Bacik 
311507730d87SJosef Bacik 	/* Don't re-enter if we're already allocating a chunk */
311607730d87SJosef Bacik 	if (trans->allocating_chunk)
311707730d87SJosef Bacik 		return -ENOSPC;
311807730d87SJosef Bacik 
311907730d87SJosef Bacik 	space_info = btrfs_find_space_info(fs_info, flags);
312007730d87SJosef Bacik 	ASSERT(space_info);
312107730d87SJosef Bacik 
312207730d87SJosef Bacik 	do {
312307730d87SJosef Bacik 		spin_lock(&space_info->lock);
312407730d87SJosef Bacik 		if (force < space_info->force_alloc)
312507730d87SJosef Bacik 			force = space_info->force_alloc;
312607730d87SJosef Bacik 		should_alloc = should_alloc_chunk(fs_info, space_info, force);
312707730d87SJosef Bacik 		if (space_info->full) {
312807730d87SJosef Bacik 			/* No more free physical space */
312907730d87SJosef Bacik 			if (should_alloc)
313007730d87SJosef Bacik 				ret = -ENOSPC;
313107730d87SJosef Bacik 			else
313207730d87SJosef Bacik 				ret = 0;
313307730d87SJosef Bacik 			spin_unlock(&space_info->lock);
313407730d87SJosef Bacik 			return ret;
313507730d87SJosef Bacik 		} else if (!should_alloc) {
313607730d87SJosef Bacik 			spin_unlock(&space_info->lock);
313707730d87SJosef Bacik 			return 0;
313807730d87SJosef Bacik 		} else if (space_info->chunk_alloc) {
313907730d87SJosef Bacik 			/*
314007730d87SJosef Bacik 			 * Someone is already allocating, so we need to block
314107730d87SJosef Bacik 			 * until this someone is finished and then loop to
314207730d87SJosef Bacik 			 * recheck if we should continue with our allocation
314307730d87SJosef Bacik 			 * attempt.
314407730d87SJosef Bacik 			 */
314507730d87SJosef Bacik 			wait_for_alloc = true;
314607730d87SJosef Bacik 			spin_unlock(&space_info->lock);
314707730d87SJosef Bacik 			mutex_lock(&fs_info->chunk_mutex);
314807730d87SJosef Bacik 			mutex_unlock(&fs_info->chunk_mutex);
314907730d87SJosef Bacik 		} else {
315007730d87SJosef Bacik 			/* Proceed with allocation */
315107730d87SJosef Bacik 			space_info->chunk_alloc = 1;
315207730d87SJosef Bacik 			wait_for_alloc = false;
315307730d87SJosef Bacik 			spin_unlock(&space_info->lock);
315407730d87SJosef Bacik 		}
315507730d87SJosef Bacik 
315607730d87SJosef Bacik 		cond_resched();
315707730d87SJosef Bacik 	} while (wait_for_alloc);
315807730d87SJosef Bacik 
315907730d87SJosef Bacik 	mutex_lock(&fs_info->chunk_mutex);
316007730d87SJosef Bacik 	trans->allocating_chunk = true;
316107730d87SJosef Bacik 
316207730d87SJosef Bacik 	/*
316307730d87SJosef Bacik 	 * If we have mixed data/metadata chunks we want to make sure we keep
316407730d87SJosef Bacik 	 * allocating mixed chunks instead of individual chunks.
316507730d87SJosef Bacik 	 */
316607730d87SJosef Bacik 	if (btrfs_mixed_space_info(space_info))
316707730d87SJosef Bacik 		flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
316807730d87SJosef Bacik 
316907730d87SJosef Bacik 	/*
317007730d87SJosef Bacik 	 * if we're doing a data chunk, go ahead and make sure that
317107730d87SJosef Bacik 	 * we keep a reasonable number of metadata chunks allocated in the
317207730d87SJosef Bacik 	 * FS as well.
317307730d87SJosef Bacik 	 */
317407730d87SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
317507730d87SJosef Bacik 		fs_info->data_chunk_allocations++;
317607730d87SJosef Bacik 		if (!(fs_info->data_chunk_allocations %
317707730d87SJosef Bacik 		      fs_info->metadata_ratio))
317807730d87SJosef Bacik 			force_metadata_allocation(fs_info);
317907730d87SJosef Bacik 	}
318007730d87SJosef Bacik 
318107730d87SJosef Bacik 	/*
318207730d87SJosef Bacik 	 * Check if we have enough space in SYSTEM chunk because we may need
318307730d87SJosef Bacik 	 * to update devices.
318407730d87SJosef Bacik 	 */
318507730d87SJosef Bacik 	check_system_chunk(trans, flags);
318607730d87SJosef Bacik 
318707730d87SJosef Bacik 	ret = btrfs_alloc_chunk(trans, flags);
318807730d87SJosef Bacik 	trans->allocating_chunk = false;
318907730d87SJosef Bacik 
319007730d87SJosef Bacik 	spin_lock(&space_info->lock);
319107730d87SJosef Bacik 	if (ret < 0) {
319207730d87SJosef Bacik 		if (ret == -ENOSPC)
319307730d87SJosef Bacik 			space_info->full = 1;
319407730d87SJosef Bacik 		else
319507730d87SJosef Bacik 			goto out;
319607730d87SJosef Bacik 	} else {
319707730d87SJosef Bacik 		ret = 1;
319807730d87SJosef Bacik 		space_info->max_extent_size = 0;
319907730d87SJosef Bacik 	}
320007730d87SJosef Bacik 
320107730d87SJosef Bacik 	space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
320207730d87SJosef Bacik out:
320307730d87SJosef Bacik 	space_info->chunk_alloc = 0;
320407730d87SJosef Bacik 	spin_unlock(&space_info->lock);
320507730d87SJosef Bacik 	mutex_unlock(&fs_info->chunk_mutex);
320607730d87SJosef Bacik 	/*
320707730d87SJosef Bacik 	 * When we allocate a new chunk we reserve space in the chunk block
320807730d87SJosef Bacik 	 * reserve to make sure we can COW nodes/leafs in the chunk tree or
320907730d87SJosef Bacik 	 * add new nodes/leafs to it if we end up needing to do it when
321007730d87SJosef Bacik 	 * inserting the chunk item and updating device items as part of the
321107730d87SJosef Bacik 	 * second phase of chunk allocation, performed by
321207730d87SJosef Bacik 	 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
321307730d87SJosef Bacik 	 * large number of new block groups to create in our transaction
321407730d87SJosef Bacik 	 * handle's new_bgs list to avoid exhausting the chunk block reserve
321507730d87SJosef Bacik 	 * in extreme cases - like having a single transaction create many new
321607730d87SJosef Bacik 	 * block groups when starting to write out the free space caches of all
321707730d87SJosef Bacik 	 * the block groups that were made dirty during the lifetime of the
321807730d87SJosef Bacik 	 * transaction.
321907730d87SJosef Bacik 	 */
322007730d87SJosef Bacik 	if (trans->chunk_bytes_reserved >= (u64)SZ_2M)
322107730d87SJosef Bacik 		btrfs_create_pending_block_groups(trans);
322207730d87SJosef Bacik 
322307730d87SJosef Bacik 	return ret;
322407730d87SJosef Bacik }
322507730d87SJosef Bacik 
322607730d87SJosef Bacik static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
322707730d87SJosef Bacik {
322807730d87SJosef Bacik 	u64 num_dev;
322907730d87SJosef Bacik 
323007730d87SJosef Bacik 	num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max;
323107730d87SJosef Bacik 	if (!num_dev)
323207730d87SJosef Bacik 		num_dev = fs_info->fs_devices->rw_devices;
323307730d87SJosef Bacik 
323407730d87SJosef Bacik 	return num_dev;
323507730d87SJosef Bacik }
323607730d87SJosef Bacik 
323707730d87SJosef Bacik /*
3238a9143bd3SMarcos Paulo de Souza  * Reserve space in the system space for allocating or removing a chunk
323907730d87SJosef Bacik  */
324007730d87SJosef Bacik void check_system_chunk(struct btrfs_trans_handle *trans, u64 type)
324107730d87SJosef Bacik {
324207730d87SJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
324307730d87SJosef Bacik 	struct btrfs_space_info *info;
324407730d87SJosef Bacik 	u64 left;
324507730d87SJosef Bacik 	u64 thresh;
324607730d87SJosef Bacik 	int ret = 0;
324707730d87SJosef Bacik 	u64 num_devs;
324807730d87SJosef Bacik 
324907730d87SJosef Bacik 	/*
325007730d87SJosef Bacik 	 * Needed because we can end up allocating a system chunk and for an
325107730d87SJosef Bacik 	 * atomic and race free space reservation in the chunk block reserve.
325207730d87SJosef Bacik 	 */
325307730d87SJosef Bacik 	lockdep_assert_held(&fs_info->chunk_mutex);
325407730d87SJosef Bacik 
325507730d87SJosef Bacik 	info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
325607730d87SJosef Bacik 	spin_lock(&info->lock);
325707730d87SJosef Bacik 	left = info->total_bytes - btrfs_space_info_used(info, true);
325807730d87SJosef Bacik 	spin_unlock(&info->lock);
325907730d87SJosef Bacik 
326007730d87SJosef Bacik 	num_devs = get_profile_num_devs(fs_info, type);
326107730d87SJosef Bacik 
326207730d87SJosef Bacik 	/* num_devs device items to update and 1 chunk item to add or remove */
32632bd36e7bSJosef Bacik 	thresh = btrfs_calc_metadata_size(fs_info, num_devs) +
32642bd36e7bSJosef Bacik 		btrfs_calc_insert_metadata_size(fs_info, 1);
326507730d87SJosef Bacik 
326607730d87SJosef Bacik 	if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
326707730d87SJosef Bacik 		btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
326807730d87SJosef Bacik 			   left, thresh, type);
326907730d87SJosef Bacik 		btrfs_dump_space_info(fs_info, info, 0, 0);
327007730d87SJosef Bacik 	}
327107730d87SJosef Bacik 
327207730d87SJosef Bacik 	if (left < thresh) {
327307730d87SJosef Bacik 		u64 flags = btrfs_system_alloc_profile(fs_info);
327407730d87SJosef Bacik 
327507730d87SJosef Bacik 		/*
327607730d87SJosef Bacik 		 * Ignore failure to create system chunk. We might end up not
327707730d87SJosef Bacik 		 * needing it, as we might not need to COW all nodes/leafs from
327807730d87SJosef Bacik 		 * the paths we visit in the chunk tree (they were already COWed
327907730d87SJosef Bacik 		 * or created in the current transaction for example).
328007730d87SJosef Bacik 		 */
328107730d87SJosef Bacik 		ret = btrfs_alloc_chunk(trans, flags);
328207730d87SJosef Bacik 	}
328307730d87SJosef Bacik 
328407730d87SJosef Bacik 	if (!ret) {
328507730d87SJosef Bacik 		ret = btrfs_block_rsv_add(fs_info->chunk_root,
328607730d87SJosef Bacik 					  &fs_info->chunk_block_rsv,
328707730d87SJosef Bacik 					  thresh, BTRFS_RESERVE_NO_FLUSH);
328807730d87SJosef Bacik 		if (!ret)
328907730d87SJosef Bacik 			trans->chunk_bytes_reserved += thresh;
329007730d87SJosef Bacik 	}
329107730d87SJosef Bacik }
329207730d87SJosef Bacik 
32933e43c279SJosef Bacik void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
32943e43c279SJosef Bacik {
329532da5386SDavid Sterba 	struct btrfs_block_group *block_group;
32963e43c279SJosef Bacik 	u64 last = 0;
32973e43c279SJosef Bacik 
32983e43c279SJosef Bacik 	while (1) {
32993e43c279SJosef Bacik 		struct inode *inode;
33003e43c279SJosef Bacik 
33013e43c279SJosef Bacik 		block_group = btrfs_lookup_first_block_group(info, last);
33023e43c279SJosef Bacik 		while (block_group) {
33033e43c279SJosef Bacik 			btrfs_wait_block_group_cache_done(block_group);
33043e43c279SJosef Bacik 			spin_lock(&block_group->lock);
33053e43c279SJosef Bacik 			if (block_group->iref)
33063e43c279SJosef Bacik 				break;
33073e43c279SJosef Bacik 			spin_unlock(&block_group->lock);
33083e43c279SJosef Bacik 			block_group = btrfs_next_block_group(block_group);
33093e43c279SJosef Bacik 		}
33103e43c279SJosef Bacik 		if (!block_group) {
33113e43c279SJosef Bacik 			if (last == 0)
33123e43c279SJosef Bacik 				break;
33133e43c279SJosef Bacik 			last = 0;
33143e43c279SJosef Bacik 			continue;
33153e43c279SJosef Bacik 		}
33163e43c279SJosef Bacik 
33173e43c279SJosef Bacik 		inode = block_group->inode;
33183e43c279SJosef Bacik 		block_group->iref = 0;
33193e43c279SJosef Bacik 		block_group->inode = NULL;
33203e43c279SJosef Bacik 		spin_unlock(&block_group->lock);
33213e43c279SJosef Bacik 		ASSERT(block_group->io_ctl.inode == NULL);
33223e43c279SJosef Bacik 		iput(inode);
3323b3470b5dSDavid Sterba 		last = block_group->start + block_group->length;
33243e43c279SJosef Bacik 		btrfs_put_block_group(block_group);
33253e43c279SJosef Bacik 	}
33263e43c279SJosef Bacik }
33273e43c279SJosef Bacik 
33283e43c279SJosef Bacik /*
33293e43c279SJosef Bacik  * Must be called only after stopping all workers, since we could have block
33303e43c279SJosef Bacik  * group caching kthreads running, and therefore they could race with us if we
33313e43c279SJosef Bacik  * freed the block groups before stopping them.
33323e43c279SJosef Bacik  */
33333e43c279SJosef Bacik int btrfs_free_block_groups(struct btrfs_fs_info *info)
33343e43c279SJosef Bacik {
333532da5386SDavid Sterba 	struct btrfs_block_group *block_group;
33363e43c279SJosef Bacik 	struct btrfs_space_info *space_info;
33373e43c279SJosef Bacik 	struct btrfs_caching_control *caching_ctl;
33383e43c279SJosef Bacik 	struct rb_node *n;
33393e43c279SJosef Bacik 
33403e43c279SJosef Bacik 	down_write(&info->commit_root_sem);
33413e43c279SJosef Bacik 	while (!list_empty(&info->caching_block_groups)) {
33423e43c279SJosef Bacik 		caching_ctl = list_entry(info->caching_block_groups.next,
33433e43c279SJosef Bacik 					 struct btrfs_caching_control, list);
33443e43c279SJosef Bacik 		list_del(&caching_ctl->list);
33453e43c279SJosef Bacik 		btrfs_put_caching_control(caching_ctl);
33463e43c279SJosef Bacik 	}
33473e43c279SJosef Bacik 	up_write(&info->commit_root_sem);
33483e43c279SJosef Bacik 
33493e43c279SJosef Bacik 	spin_lock(&info->unused_bgs_lock);
33503e43c279SJosef Bacik 	while (!list_empty(&info->unused_bgs)) {
33513e43c279SJosef Bacik 		block_group = list_first_entry(&info->unused_bgs,
335232da5386SDavid Sterba 					       struct btrfs_block_group,
33533e43c279SJosef Bacik 					       bg_list);
33543e43c279SJosef Bacik 		list_del_init(&block_group->bg_list);
33553e43c279SJosef Bacik 		btrfs_put_block_group(block_group);
33563e43c279SJosef Bacik 	}
33573e43c279SJosef Bacik 	spin_unlock(&info->unused_bgs_lock);
33583e43c279SJosef Bacik 
33593e43c279SJosef Bacik 	spin_lock(&info->block_group_cache_lock);
33603e43c279SJosef Bacik 	while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
336132da5386SDavid Sterba 		block_group = rb_entry(n, struct btrfs_block_group,
33623e43c279SJosef Bacik 				       cache_node);
33633e43c279SJosef Bacik 		rb_erase(&block_group->cache_node,
33643e43c279SJosef Bacik 			 &info->block_group_cache_tree);
33653e43c279SJosef Bacik 		RB_CLEAR_NODE(&block_group->cache_node);
33663e43c279SJosef Bacik 		spin_unlock(&info->block_group_cache_lock);
33673e43c279SJosef Bacik 
33683e43c279SJosef Bacik 		down_write(&block_group->space_info->groups_sem);
33693e43c279SJosef Bacik 		list_del(&block_group->list);
33703e43c279SJosef Bacik 		up_write(&block_group->space_info->groups_sem);
33713e43c279SJosef Bacik 
33723e43c279SJosef Bacik 		/*
33733e43c279SJosef Bacik 		 * We haven't cached this block group, which means we could
33743e43c279SJosef Bacik 		 * possibly have excluded extents on this block group.
33753e43c279SJosef Bacik 		 */
33763e43c279SJosef Bacik 		if (block_group->cached == BTRFS_CACHE_NO ||
33773e43c279SJosef Bacik 		    block_group->cached == BTRFS_CACHE_ERROR)
33783e43c279SJosef Bacik 			btrfs_free_excluded_extents(block_group);
33793e43c279SJosef Bacik 
33803e43c279SJosef Bacik 		btrfs_remove_free_space_cache(block_group);
33813e43c279SJosef Bacik 		ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
33823e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->dirty_list));
33833e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->io_list));
33843e43c279SJosef Bacik 		ASSERT(list_empty(&block_group->bg_list));
33853e43c279SJosef Bacik 		ASSERT(atomic_read(&block_group->count) == 1);
33863e43c279SJosef Bacik 		btrfs_put_block_group(block_group);
33873e43c279SJosef Bacik 
33883e43c279SJosef Bacik 		spin_lock(&info->block_group_cache_lock);
33893e43c279SJosef Bacik 	}
33903e43c279SJosef Bacik 	spin_unlock(&info->block_group_cache_lock);
33913e43c279SJosef Bacik 
33923e43c279SJosef Bacik 	/*
33933e43c279SJosef Bacik 	 * Now that all the block groups are freed, go through and free all the
33943e43c279SJosef Bacik 	 * space_info structs.  This is only called during the final stages of
33953e43c279SJosef Bacik 	 * unmount, and so we know nobody is using them.  We call
33963e43c279SJosef Bacik 	 * synchronize_rcu() once before we start, just to be on the safe side.
33973e43c279SJosef Bacik 	 */
33983e43c279SJosef Bacik 	synchronize_rcu();
33993e43c279SJosef Bacik 
34003e43c279SJosef Bacik 	btrfs_release_global_block_rsv(info);
34013e43c279SJosef Bacik 
34023e43c279SJosef Bacik 	while (!list_empty(&info->space_info)) {
34033e43c279SJosef Bacik 		space_info = list_entry(info->space_info.next,
34043e43c279SJosef Bacik 					struct btrfs_space_info,
34053e43c279SJosef Bacik 					list);
34063e43c279SJosef Bacik 
34073e43c279SJosef Bacik 		/*
34083e43c279SJosef Bacik 		 * Do not hide this behind enospc_debug, this is actually
34093e43c279SJosef Bacik 		 * important and indicates a real bug if this happens.
34103e43c279SJosef Bacik 		 */
34113e43c279SJosef Bacik 		if (WARN_ON(space_info->bytes_pinned > 0 ||
34123e43c279SJosef Bacik 			    space_info->bytes_reserved > 0 ||
34133e43c279SJosef Bacik 			    space_info->bytes_may_use > 0))
34143e43c279SJosef Bacik 			btrfs_dump_space_info(info, space_info, 0, 0);
3415d611add4SFilipe Manana 		WARN_ON(space_info->reclaim_size > 0);
34163e43c279SJosef Bacik 		list_del(&space_info->list);
34173e43c279SJosef Bacik 		btrfs_sysfs_remove_space_info(space_info);
34183e43c279SJosef Bacik 	}
34193e43c279SJosef Bacik 	return 0;
34203e43c279SJosef Bacik }
3421684b752bSFilipe Manana 
3422684b752bSFilipe Manana void btrfs_freeze_block_group(struct btrfs_block_group *cache)
3423684b752bSFilipe Manana {
3424684b752bSFilipe Manana 	atomic_inc(&cache->frozen);
3425684b752bSFilipe Manana }
3426684b752bSFilipe Manana 
3427684b752bSFilipe Manana void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
3428684b752bSFilipe Manana {
3429684b752bSFilipe Manana 	struct btrfs_fs_info *fs_info = block_group->fs_info;
3430684b752bSFilipe Manana 	struct extent_map_tree *em_tree;
3431684b752bSFilipe Manana 	struct extent_map *em;
3432684b752bSFilipe Manana 	bool cleanup;
3433684b752bSFilipe Manana 
3434684b752bSFilipe Manana 	spin_lock(&block_group->lock);
3435684b752bSFilipe Manana 	cleanup = (atomic_dec_and_test(&block_group->frozen) &&
3436684b752bSFilipe Manana 		   block_group->removed);
3437684b752bSFilipe Manana 	spin_unlock(&block_group->lock);
3438684b752bSFilipe Manana 
3439684b752bSFilipe Manana 	if (cleanup) {
3440684b752bSFilipe Manana 		mutex_lock(&fs_info->chunk_mutex);
3441684b752bSFilipe Manana 		em_tree = &fs_info->mapping_tree;
3442684b752bSFilipe Manana 		write_lock(&em_tree->lock);
3443684b752bSFilipe Manana 		em = lookup_extent_mapping(em_tree, block_group->start,
3444684b752bSFilipe Manana 					   1);
3445684b752bSFilipe Manana 		BUG_ON(!em); /* logic error, can't happen */
3446684b752bSFilipe Manana 		remove_extent_mapping(em_tree, em);
3447684b752bSFilipe Manana 		write_unlock(&em_tree->lock);
3448684b752bSFilipe Manana 		mutex_unlock(&fs_info->chunk_mutex);
3449684b752bSFilipe Manana 
3450684b752bSFilipe Manana 		/* once for us and once for the tree */
3451684b752bSFilipe Manana 		free_extent_map(em);
3452684b752bSFilipe Manana 		free_extent_map(em);
3453684b752bSFilipe Manana 
3454684b752bSFilipe Manana 		/*
3455684b752bSFilipe Manana 		 * We may have left one free space entry and other possible
3456684b752bSFilipe Manana 		 * tasks trimming this block group have left 1 entry each one.
3457684b752bSFilipe Manana 		 * Free them if any.
3458684b752bSFilipe Manana 		 */
3459684b752bSFilipe Manana 		__btrfs_remove_free_space_cache(block_group->free_space_ctl);
3460684b752bSFilipe Manana 	}
3461684b752bSFilipe Manana }
3462