xref: /OK3568_Linux_fs/kernel/fs/btrfs/block-group.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun #include "misc.h"
4*4882a593Smuzhiyun #include "ctree.h"
5*4882a593Smuzhiyun #include "block-group.h"
6*4882a593Smuzhiyun #include "space-info.h"
7*4882a593Smuzhiyun #include "disk-io.h"
8*4882a593Smuzhiyun #include "free-space-cache.h"
9*4882a593Smuzhiyun #include "free-space-tree.h"
10*4882a593Smuzhiyun #include "volumes.h"
11*4882a593Smuzhiyun #include "transaction.h"
12*4882a593Smuzhiyun #include "ref-verify.h"
13*4882a593Smuzhiyun #include "sysfs.h"
14*4882a593Smuzhiyun #include "tree-log.h"
15*4882a593Smuzhiyun #include "delalloc-space.h"
16*4882a593Smuzhiyun #include "discard.h"
17*4882a593Smuzhiyun #include "raid56.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  * Return target flags in extended format or 0 if restripe for this chunk_type
21*4882a593Smuzhiyun  * is not in progress
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Should be called with balance_lock held
24*4882a593Smuzhiyun  */
get_restripe_target(struct btrfs_fs_info * fs_info,u64 flags)25*4882a593Smuzhiyun static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
28*4882a593Smuzhiyun 	u64 target = 0;
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	if (!bctl)
31*4882a593Smuzhiyun 		return 0;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	if (flags & BTRFS_BLOCK_GROUP_DATA &&
34*4882a593Smuzhiyun 	    bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
35*4882a593Smuzhiyun 		target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
36*4882a593Smuzhiyun 	} else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
37*4882a593Smuzhiyun 		   bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
38*4882a593Smuzhiyun 		target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
39*4882a593Smuzhiyun 	} else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
40*4882a593Smuzhiyun 		   bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
41*4882a593Smuzhiyun 		target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
42*4882a593Smuzhiyun 	}
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	return target;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun  * @flags: available profiles in extended format (see ctree.h)
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * Return reduced profile in chunk format.  If profile changing is in progress
51*4882a593Smuzhiyun  * (either running or paused) picks the target profile (if it's already
52*4882a593Smuzhiyun  * available), otherwise falls back to plain reducing.
53*4882a593Smuzhiyun  */
btrfs_reduce_alloc_profile(struct btrfs_fs_info * fs_info,u64 flags)54*4882a593Smuzhiyun static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	u64 num_devices = fs_info->fs_devices->rw_devices;
57*4882a593Smuzhiyun 	u64 target;
58*4882a593Smuzhiyun 	u64 raid_type;
59*4882a593Smuzhiyun 	u64 allowed = 0;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	/*
62*4882a593Smuzhiyun 	 * See if restripe for this chunk_type is in progress, if so try to
63*4882a593Smuzhiyun 	 * reduce to the target profile
64*4882a593Smuzhiyun 	 */
65*4882a593Smuzhiyun 	spin_lock(&fs_info->balance_lock);
66*4882a593Smuzhiyun 	target = get_restripe_target(fs_info, flags);
67*4882a593Smuzhiyun 	if (target) {
68*4882a593Smuzhiyun 		spin_unlock(&fs_info->balance_lock);
69*4882a593Smuzhiyun 		return extended_to_chunk(target);
70*4882a593Smuzhiyun 	}
71*4882a593Smuzhiyun 	spin_unlock(&fs_info->balance_lock);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	/* First, mask out the RAID levels which aren't possible */
74*4882a593Smuzhiyun 	for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
75*4882a593Smuzhiyun 		if (num_devices >= btrfs_raid_array[raid_type].devs_min)
76*4882a593Smuzhiyun 			allowed |= btrfs_raid_array[raid_type].bg_flag;
77*4882a593Smuzhiyun 	}
78*4882a593Smuzhiyun 	allowed &= flags;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	if (allowed & BTRFS_BLOCK_GROUP_RAID6)
81*4882a593Smuzhiyun 		allowed = BTRFS_BLOCK_GROUP_RAID6;
82*4882a593Smuzhiyun 	else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
83*4882a593Smuzhiyun 		allowed = BTRFS_BLOCK_GROUP_RAID5;
84*4882a593Smuzhiyun 	else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
85*4882a593Smuzhiyun 		allowed = BTRFS_BLOCK_GROUP_RAID10;
86*4882a593Smuzhiyun 	else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
87*4882a593Smuzhiyun 		allowed = BTRFS_BLOCK_GROUP_RAID1;
88*4882a593Smuzhiyun 	else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
89*4882a593Smuzhiyun 		allowed = BTRFS_BLOCK_GROUP_RAID0;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	return extended_to_chunk(flags | allowed);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
btrfs_get_alloc_profile(struct btrfs_fs_info * fs_info,u64 orig_flags)96*4882a593Smuzhiyun u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	unsigned seq;
99*4882a593Smuzhiyun 	u64 flags;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	do {
102*4882a593Smuzhiyun 		flags = orig_flags;
103*4882a593Smuzhiyun 		seq = read_seqbegin(&fs_info->profiles_lock);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 		if (flags & BTRFS_BLOCK_GROUP_DATA)
106*4882a593Smuzhiyun 			flags |= fs_info->avail_data_alloc_bits;
107*4882a593Smuzhiyun 		else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
108*4882a593Smuzhiyun 			flags |= fs_info->avail_system_alloc_bits;
109*4882a593Smuzhiyun 		else if (flags & BTRFS_BLOCK_GROUP_METADATA)
110*4882a593Smuzhiyun 			flags |= fs_info->avail_metadata_alloc_bits;
111*4882a593Smuzhiyun 	} while (read_seqretry(&fs_info->profiles_lock, seq));
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	return btrfs_reduce_alloc_profile(fs_info, flags);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
btrfs_get_block_group(struct btrfs_block_group * cache)116*4882a593Smuzhiyun void btrfs_get_block_group(struct btrfs_block_group *cache)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	refcount_inc(&cache->refs);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
btrfs_put_block_group(struct btrfs_block_group * cache)121*4882a593Smuzhiyun void btrfs_put_block_group(struct btrfs_block_group *cache)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	if (refcount_dec_and_test(&cache->refs)) {
124*4882a593Smuzhiyun 		WARN_ON(cache->pinned > 0);
125*4882a593Smuzhiyun 		WARN_ON(cache->reserved > 0);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 		/*
128*4882a593Smuzhiyun 		 * A block_group shouldn't be on the discard_list anymore.
129*4882a593Smuzhiyun 		 * Remove the block_group from the discard_list to prevent us
130*4882a593Smuzhiyun 		 * from causing a panic due to NULL pointer dereference.
131*4882a593Smuzhiyun 		 */
132*4882a593Smuzhiyun 		if (WARN_ON(!list_empty(&cache->discard_list)))
133*4882a593Smuzhiyun 			btrfs_discard_cancel_work(&cache->fs_info->discard_ctl,
134*4882a593Smuzhiyun 						  cache);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 		/*
137*4882a593Smuzhiyun 		 * If not empty, someone is still holding mutex of
138*4882a593Smuzhiyun 		 * full_stripe_lock, which can only be released by caller.
139*4882a593Smuzhiyun 		 * And it will definitely cause use-after-free when caller
140*4882a593Smuzhiyun 		 * tries to release full stripe lock.
141*4882a593Smuzhiyun 		 *
142*4882a593Smuzhiyun 		 * No better way to resolve, but only to warn.
143*4882a593Smuzhiyun 		 */
144*4882a593Smuzhiyun 		WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
145*4882a593Smuzhiyun 		kfree(cache->free_space_ctl);
146*4882a593Smuzhiyun 		kfree(cache);
147*4882a593Smuzhiyun 	}
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun /*
151*4882a593Smuzhiyun  * This adds the block group to the fs_info rb tree for the block group cache
152*4882a593Smuzhiyun  */
btrfs_add_block_group_cache(struct btrfs_fs_info * info,struct btrfs_block_group * block_group)153*4882a593Smuzhiyun static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
154*4882a593Smuzhiyun 				       struct btrfs_block_group *block_group)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun 	struct rb_node **p;
157*4882a593Smuzhiyun 	struct rb_node *parent = NULL;
158*4882a593Smuzhiyun 	struct btrfs_block_group *cache;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	ASSERT(block_group->length != 0);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	spin_lock(&info->block_group_cache_lock);
163*4882a593Smuzhiyun 	p = &info->block_group_cache_tree.rb_node;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	while (*p) {
166*4882a593Smuzhiyun 		parent = *p;
167*4882a593Smuzhiyun 		cache = rb_entry(parent, struct btrfs_block_group, cache_node);
168*4882a593Smuzhiyun 		if (block_group->start < cache->start) {
169*4882a593Smuzhiyun 			p = &(*p)->rb_left;
170*4882a593Smuzhiyun 		} else if (block_group->start > cache->start) {
171*4882a593Smuzhiyun 			p = &(*p)->rb_right;
172*4882a593Smuzhiyun 		} else {
173*4882a593Smuzhiyun 			spin_unlock(&info->block_group_cache_lock);
174*4882a593Smuzhiyun 			return -EEXIST;
175*4882a593Smuzhiyun 		}
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	rb_link_node(&block_group->cache_node, parent, p);
179*4882a593Smuzhiyun 	rb_insert_color(&block_group->cache_node,
180*4882a593Smuzhiyun 			&info->block_group_cache_tree);
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	if (info->first_logical_byte > block_group->start)
183*4882a593Smuzhiyun 		info->first_logical_byte = block_group->start;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	spin_unlock(&info->block_group_cache_lock);
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	return 0;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /*
191*4882a593Smuzhiyun  * This will return the block group at or after bytenr if contains is 0, else
192*4882a593Smuzhiyun  * it will return the block group that contains the bytenr
193*4882a593Smuzhiyun  */
block_group_cache_tree_search(struct btrfs_fs_info * info,u64 bytenr,int contains)194*4882a593Smuzhiyun static struct btrfs_block_group *block_group_cache_tree_search(
195*4882a593Smuzhiyun 		struct btrfs_fs_info *info, u64 bytenr, int contains)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	struct btrfs_block_group *cache, *ret = NULL;
198*4882a593Smuzhiyun 	struct rb_node *n;
199*4882a593Smuzhiyun 	u64 end, start;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	spin_lock(&info->block_group_cache_lock);
202*4882a593Smuzhiyun 	n = info->block_group_cache_tree.rb_node;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	while (n) {
205*4882a593Smuzhiyun 		cache = rb_entry(n, struct btrfs_block_group, cache_node);
206*4882a593Smuzhiyun 		end = cache->start + cache->length - 1;
207*4882a593Smuzhiyun 		start = cache->start;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 		if (bytenr < start) {
210*4882a593Smuzhiyun 			if (!contains && (!ret || start < ret->start))
211*4882a593Smuzhiyun 				ret = cache;
212*4882a593Smuzhiyun 			n = n->rb_left;
213*4882a593Smuzhiyun 		} else if (bytenr > start) {
214*4882a593Smuzhiyun 			if (contains && bytenr <= end) {
215*4882a593Smuzhiyun 				ret = cache;
216*4882a593Smuzhiyun 				break;
217*4882a593Smuzhiyun 			}
218*4882a593Smuzhiyun 			n = n->rb_right;
219*4882a593Smuzhiyun 		} else {
220*4882a593Smuzhiyun 			ret = cache;
221*4882a593Smuzhiyun 			break;
222*4882a593Smuzhiyun 		}
223*4882a593Smuzhiyun 	}
224*4882a593Smuzhiyun 	if (ret) {
225*4882a593Smuzhiyun 		btrfs_get_block_group(ret);
226*4882a593Smuzhiyun 		if (bytenr == 0 && info->first_logical_byte > ret->start)
227*4882a593Smuzhiyun 			info->first_logical_byte = ret->start;
228*4882a593Smuzhiyun 	}
229*4882a593Smuzhiyun 	spin_unlock(&info->block_group_cache_lock);
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	return ret;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /*
235*4882a593Smuzhiyun  * Return the block group that starts at or after bytenr
236*4882a593Smuzhiyun  */
btrfs_lookup_first_block_group(struct btrfs_fs_info * info,u64 bytenr)237*4882a593Smuzhiyun struct btrfs_block_group *btrfs_lookup_first_block_group(
238*4882a593Smuzhiyun 		struct btrfs_fs_info *info, u64 bytenr)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	return block_group_cache_tree_search(info, bytenr, 0);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun /*
244*4882a593Smuzhiyun  * Return the block group that contains the given bytenr
245*4882a593Smuzhiyun  */
btrfs_lookup_block_group(struct btrfs_fs_info * info,u64 bytenr)246*4882a593Smuzhiyun struct btrfs_block_group *btrfs_lookup_block_group(
247*4882a593Smuzhiyun 		struct btrfs_fs_info *info, u64 bytenr)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun 	return block_group_cache_tree_search(info, bytenr, 1);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun 
btrfs_next_block_group(struct btrfs_block_group * cache)252*4882a593Smuzhiyun struct btrfs_block_group *btrfs_next_block_group(
253*4882a593Smuzhiyun 		struct btrfs_block_group *cache)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = cache->fs_info;
256*4882a593Smuzhiyun 	struct rb_node *node;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	spin_lock(&fs_info->block_group_cache_lock);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	/* If our block group was removed, we need a full search. */
261*4882a593Smuzhiyun 	if (RB_EMPTY_NODE(&cache->cache_node)) {
262*4882a593Smuzhiyun 		const u64 next_bytenr = cache->start + cache->length;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 		spin_unlock(&fs_info->block_group_cache_lock);
265*4882a593Smuzhiyun 		btrfs_put_block_group(cache);
266*4882a593Smuzhiyun 		cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 	node = rb_next(&cache->cache_node);
269*4882a593Smuzhiyun 	btrfs_put_block_group(cache);
270*4882a593Smuzhiyun 	if (node) {
271*4882a593Smuzhiyun 		cache = rb_entry(node, struct btrfs_block_group, cache_node);
272*4882a593Smuzhiyun 		btrfs_get_block_group(cache);
273*4882a593Smuzhiyun 	} else
274*4882a593Smuzhiyun 		cache = NULL;
275*4882a593Smuzhiyun 	spin_unlock(&fs_info->block_group_cache_lock);
276*4882a593Smuzhiyun 	return cache;
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun 
btrfs_inc_nocow_writers(struct btrfs_fs_info * fs_info,u64 bytenr)279*4882a593Smuzhiyun bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun 	struct btrfs_block_group *bg;
282*4882a593Smuzhiyun 	bool ret = true;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	bg = btrfs_lookup_block_group(fs_info, bytenr);
285*4882a593Smuzhiyun 	if (!bg)
286*4882a593Smuzhiyun 		return false;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	spin_lock(&bg->lock);
289*4882a593Smuzhiyun 	if (bg->ro)
290*4882a593Smuzhiyun 		ret = false;
291*4882a593Smuzhiyun 	else
292*4882a593Smuzhiyun 		atomic_inc(&bg->nocow_writers);
293*4882a593Smuzhiyun 	spin_unlock(&bg->lock);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	/* No put on block group, done by btrfs_dec_nocow_writers */
296*4882a593Smuzhiyun 	if (!ret)
297*4882a593Smuzhiyun 		btrfs_put_block_group(bg);
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	return ret;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun 
btrfs_dec_nocow_writers(struct btrfs_fs_info * fs_info,u64 bytenr)302*4882a593Smuzhiyun void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun 	struct btrfs_block_group *bg;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	bg = btrfs_lookup_block_group(fs_info, bytenr);
307*4882a593Smuzhiyun 	ASSERT(bg);
308*4882a593Smuzhiyun 	if (atomic_dec_and_test(&bg->nocow_writers))
309*4882a593Smuzhiyun 		wake_up_var(&bg->nocow_writers);
310*4882a593Smuzhiyun 	/*
311*4882a593Smuzhiyun 	 * Once for our lookup and once for the lookup done by a previous call
312*4882a593Smuzhiyun 	 * to btrfs_inc_nocow_writers()
313*4882a593Smuzhiyun 	 */
314*4882a593Smuzhiyun 	btrfs_put_block_group(bg);
315*4882a593Smuzhiyun 	btrfs_put_block_group(bg);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun 
btrfs_wait_nocow_writers(struct btrfs_block_group * bg)318*4882a593Smuzhiyun void btrfs_wait_nocow_writers(struct btrfs_block_group *bg)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun 	wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
btrfs_dec_block_group_reservations(struct btrfs_fs_info * fs_info,const u64 start)323*4882a593Smuzhiyun void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
324*4882a593Smuzhiyun 					const u64 start)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	struct btrfs_block_group *bg;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	bg = btrfs_lookup_block_group(fs_info, start);
329*4882a593Smuzhiyun 	ASSERT(bg);
330*4882a593Smuzhiyun 	if (atomic_dec_and_test(&bg->reservations))
331*4882a593Smuzhiyun 		wake_up_var(&bg->reservations);
332*4882a593Smuzhiyun 	btrfs_put_block_group(bg);
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
btrfs_wait_block_group_reservations(struct btrfs_block_group * bg)335*4882a593Smuzhiyun void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	struct btrfs_space_info *space_info = bg->space_info;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	ASSERT(bg->ro);
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
342*4882a593Smuzhiyun 		return;
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	/*
345*4882a593Smuzhiyun 	 * Our block group is read only but before we set it to read only,
346*4882a593Smuzhiyun 	 * some task might have had allocated an extent from it already, but it
347*4882a593Smuzhiyun 	 * has not yet created a respective ordered extent (and added it to a
348*4882a593Smuzhiyun 	 * root's list of ordered extents).
349*4882a593Smuzhiyun 	 * Therefore wait for any task currently allocating extents, since the
350*4882a593Smuzhiyun 	 * block group's reservations counter is incremented while a read lock
351*4882a593Smuzhiyun 	 * on the groups' semaphore is held and decremented after releasing
352*4882a593Smuzhiyun 	 * the read access on that semaphore and creating the ordered extent.
353*4882a593Smuzhiyun 	 */
354*4882a593Smuzhiyun 	down_write(&space_info->groups_sem);
355*4882a593Smuzhiyun 	up_write(&space_info->groups_sem);
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
btrfs_get_caching_control(struct btrfs_block_group * cache)360*4882a593Smuzhiyun struct btrfs_caching_control *btrfs_get_caching_control(
361*4882a593Smuzhiyun 		struct btrfs_block_group *cache)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun 	struct btrfs_caching_control *ctl;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	spin_lock(&cache->lock);
366*4882a593Smuzhiyun 	if (!cache->caching_ctl) {
367*4882a593Smuzhiyun 		spin_unlock(&cache->lock);
368*4882a593Smuzhiyun 		return NULL;
369*4882a593Smuzhiyun 	}
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	ctl = cache->caching_ctl;
372*4882a593Smuzhiyun 	refcount_inc(&ctl->count);
373*4882a593Smuzhiyun 	spin_unlock(&cache->lock);
374*4882a593Smuzhiyun 	return ctl;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun 
btrfs_put_caching_control(struct btrfs_caching_control * ctl)377*4882a593Smuzhiyun void btrfs_put_caching_control(struct btrfs_caching_control *ctl)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun 	if (refcount_dec_and_test(&ctl->count))
380*4882a593Smuzhiyun 		kfree(ctl);
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun /*
384*4882a593Smuzhiyun  * When we wait for progress in the block group caching, its because our
385*4882a593Smuzhiyun  * allocation attempt failed at least once.  So, we must sleep and let some
386*4882a593Smuzhiyun  * progress happen before we try again.
387*4882a593Smuzhiyun  *
388*4882a593Smuzhiyun  * This function will sleep at least once waiting for new free space to show
389*4882a593Smuzhiyun  * up, and then it will check the block group free space numbers for our min
390*4882a593Smuzhiyun  * num_bytes.  Another option is to have it go ahead and look in the rbtree for
391*4882a593Smuzhiyun  * a free extent of a given size, but this is a good start.
392*4882a593Smuzhiyun  *
393*4882a593Smuzhiyun  * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
394*4882a593Smuzhiyun  * any of the information in this block group.
395*4882a593Smuzhiyun  */
btrfs_wait_block_group_cache_progress(struct btrfs_block_group * cache,u64 num_bytes)396*4882a593Smuzhiyun void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache,
397*4882a593Smuzhiyun 					   u64 num_bytes)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun 	struct btrfs_caching_control *caching_ctl;
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	caching_ctl = btrfs_get_caching_control(cache);
402*4882a593Smuzhiyun 	if (!caching_ctl)
403*4882a593Smuzhiyun 		return;
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	wait_event(caching_ctl->wait, btrfs_block_group_done(cache) ||
406*4882a593Smuzhiyun 		   (cache->free_space_ctl->free_space >= num_bytes));
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	btrfs_put_caching_control(caching_ctl);
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun 
btrfs_wait_block_group_cache_done(struct btrfs_block_group * cache)411*4882a593Smuzhiyun int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache)
412*4882a593Smuzhiyun {
413*4882a593Smuzhiyun 	struct btrfs_caching_control *caching_ctl;
414*4882a593Smuzhiyun 	int ret = 0;
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	caching_ctl = btrfs_get_caching_control(cache);
417*4882a593Smuzhiyun 	if (!caching_ctl)
418*4882a593Smuzhiyun 		return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	wait_event(caching_ctl->wait, btrfs_block_group_done(cache));
421*4882a593Smuzhiyun 	if (cache->cached == BTRFS_CACHE_ERROR)
422*4882a593Smuzhiyun 		ret = -EIO;
423*4882a593Smuzhiyun 	btrfs_put_caching_control(caching_ctl);
424*4882a593Smuzhiyun 	return ret;
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun #ifdef CONFIG_BTRFS_DEBUG
fragment_free_space(struct btrfs_block_group * block_group)428*4882a593Smuzhiyun static void fragment_free_space(struct btrfs_block_group *block_group)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = block_group->fs_info;
431*4882a593Smuzhiyun 	u64 start = block_group->start;
432*4882a593Smuzhiyun 	u64 len = block_group->length;
433*4882a593Smuzhiyun 	u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
434*4882a593Smuzhiyun 		fs_info->nodesize : fs_info->sectorsize;
435*4882a593Smuzhiyun 	u64 step = chunk << 1;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	while (len > chunk) {
438*4882a593Smuzhiyun 		btrfs_remove_free_space(block_group, start, chunk);
439*4882a593Smuzhiyun 		start += step;
440*4882a593Smuzhiyun 		if (len < step)
441*4882a593Smuzhiyun 			len = 0;
442*4882a593Smuzhiyun 		else
443*4882a593Smuzhiyun 			len -= step;
444*4882a593Smuzhiyun 	}
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun #endif
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun /*
449*4882a593Smuzhiyun  * This is only called by btrfs_cache_block_group, since we could have freed
450*4882a593Smuzhiyun  * extents we need to check the pinned_extents for any extents that can't be
451*4882a593Smuzhiyun  * used yet since their free space will be released as soon as the transaction
452*4882a593Smuzhiyun  * commits.
453*4882a593Smuzhiyun  */
add_new_free_space(struct btrfs_block_group * block_group,u64 start,u64 end)454*4882a593Smuzhiyun u64 add_new_free_space(struct btrfs_block_group *block_group, u64 start, u64 end)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun 	struct btrfs_fs_info *info = block_group->fs_info;
457*4882a593Smuzhiyun 	u64 extent_start, extent_end, size, total_added = 0;
458*4882a593Smuzhiyun 	int ret;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	while (start < end) {
461*4882a593Smuzhiyun 		ret = find_first_extent_bit(&info->excluded_extents, start,
462*4882a593Smuzhiyun 					    &extent_start, &extent_end,
463*4882a593Smuzhiyun 					    EXTENT_DIRTY | EXTENT_UPTODATE,
464*4882a593Smuzhiyun 					    NULL);
465*4882a593Smuzhiyun 		if (ret)
466*4882a593Smuzhiyun 			break;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 		if (extent_start <= start) {
469*4882a593Smuzhiyun 			start = extent_end + 1;
470*4882a593Smuzhiyun 		} else if (extent_start > start && extent_start < end) {
471*4882a593Smuzhiyun 			size = extent_start - start;
472*4882a593Smuzhiyun 			total_added += size;
473*4882a593Smuzhiyun 			ret = btrfs_add_free_space_async_trimmed(block_group,
474*4882a593Smuzhiyun 								 start, size);
475*4882a593Smuzhiyun 			BUG_ON(ret); /* -ENOMEM or logic error */
476*4882a593Smuzhiyun 			start = extent_end + 1;
477*4882a593Smuzhiyun 		} else {
478*4882a593Smuzhiyun 			break;
479*4882a593Smuzhiyun 		}
480*4882a593Smuzhiyun 	}
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	if (start < end) {
483*4882a593Smuzhiyun 		size = end - start;
484*4882a593Smuzhiyun 		total_added += size;
485*4882a593Smuzhiyun 		ret = btrfs_add_free_space_async_trimmed(block_group, start,
486*4882a593Smuzhiyun 							 size);
487*4882a593Smuzhiyun 		BUG_ON(ret); /* -ENOMEM or logic error */
488*4882a593Smuzhiyun 	}
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	return total_added;
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun 
load_extent_tree_free(struct btrfs_caching_control * caching_ctl)493*4882a593Smuzhiyun static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
494*4882a593Smuzhiyun {
495*4882a593Smuzhiyun 	struct btrfs_block_group *block_group = caching_ctl->block_group;
496*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = block_group->fs_info;
497*4882a593Smuzhiyun 	struct btrfs_root *extent_root = fs_info->extent_root;
498*4882a593Smuzhiyun 	struct btrfs_path *path;
499*4882a593Smuzhiyun 	struct extent_buffer *leaf;
500*4882a593Smuzhiyun 	struct btrfs_key key;
501*4882a593Smuzhiyun 	u64 total_found = 0;
502*4882a593Smuzhiyun 	u64 last = 0;
503*4882a593Smuzhiyun 	u32 nritems;
504*4882a593Smuzhiyun 	int ret;
505*4882a593Smuzhiyun 	bool wakeup = true;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	path = btrfs_alloc_path();
508*4882a593Smuzhiyun 	if (!path)
509*4882a593Smuzhiyun 		return -ENOMEM;
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET);
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun #ifdef CONFIG_BTRFS_DEBUG
514*4882a593Smuzhiyun 	/*
515*4882a593Smuzhiyun 	 * If we're fragmenting we don't want to make anybody think we can
516*4882a593Smuzhiyun 	 * allocate from this block group until we've had a chance to fragment
517*4882a593Smuzhiyun 	 * the free space.
518*4882a593Smuzhiyun 	 */
519*4882a593Smuzhiyun 	if (btrfs_should_fragment_free_space(block_group))
520*4882a593Smuzhiyun 		wakeup = false;
521*4882a593Smuzhiyun #endif
522*4882a593Smuzhiyun 	/*
523*4882a593Smuzhiyun 	 * We don't want to deadlock with somebody trying to allocate a new
524*4882a593Smuzhiyun 	 * extent for the extent root while also trying to search the extent
525*4882a593Smuzhiyun 	 * root to add free space.  So we skip locking and search the commit
526*4882a593Smuzhiyun 	 * root, since its read-only
527*4882a593Smuzhiyun 	 */
528*4882a593Smuzhiyun 	path->skip_locking = 1;
529*4882a593Smuzhiyun 	path->search_commit_root = 1;
530*4882a593Smuzhiyun 	path->reada = READA_FORWARD;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	key.objectid = last;
533*4882a593Smuzhiyun 	key.offset = 0;
534*4882a593Smuzhiyun 	key.type = BTRFS_EXTENT_ITEM_KEY;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun next:
537*4882a593Smuzhiyun 	ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
538*4882a593Smuzhiyun 	if (ret < 0)
539*4882a593Smuzhiyun 		goto out;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	leaf = path->nodes[0];
542*4882a593Smuzhiyun 	nritems = btrfs_header_nritems(leaf);
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	while (1) {
545*4882a593Smuzhiyun 		if (btrfs_fs_closing(fs_info) > 1) {
546*4882a593Smuzhiyun 			last = (u64)-1;
547*4882a593Smuzhiyun 			break;
548*4882a593Smuzhiyun 		}
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 		if (path->slots[0] < nritems) {
551*4882a593Smuzhiyun 			btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
552*4882a593Smuzhiyun 		} else {
553*4882a593Smuzhiyun 			ret = btrfs_find_next_key(extent_root, path, &key, 0, 0);
554*4882a593Smuzhiyun 			if (ret)
555*4882a593Smuzhiyun 				break;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 			if (need_resched() ||
558*4882a593Smuzhiyun 			    rwsem_is_contended(&fs_info->commit_root_sem)) {
559*4882a593Smuzhiyun 				if (wakeup)
560*4882a593Smuzhiyun 					caching_ctl->progress = last;
561*4882a593Smuzhiyun 				btrfs_release_path(path);
562*4882a593Smuzhiyun 				up_read(&fs_info->commit_root_sem);
563*4882a593Smuzhiyun 				mutex_unlock(&caching_ctl->mutex);
564*4882a593Smuzhiyun 				cond_resched();
565*4882a593Smuzhiyun 				mutex_lock(&caching_ctl->mutex);
566*4882a593Smuzhiyun 				down_read(&fs_info->commit_root_sem);
567*4882a593Smuzhiyun 				goto next;
568*4882a593Smuzhiyun 			}
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 			ret = btrfs_next_leaf(extent_root, path);
571*4882a593Smuzhiyun 			if (ret < 0)
572*4882a593Smuzhiyun 				goto out;
573*4882a593Smuzhiyun 			if (ret)
574*4882a593Smuzhiyun 				break;
575*4882a593Smuzhiyun 			leaf = path->nodes[0];
576*4882a593Smuzhiyun 			nritems = btrfs_header_nritems(leaf);
577*4882a593Smuzhiyun 			continue;
578*4882a593Smuzhiyun 		}
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 		if (key.objectid < last) {
581*4882a593Smuzhiyun 			key.objectid = last;
582*4882a593Smuzhiyun 			key.offset = 0;
583*4882a593Smuzhiyun 			key.type = BTRFS_EXTENT_ITEM_KEY;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 			if (wakeup)
586*4882a593Smuzhiyun 				caching_ctl->progress = last;
587*4882a593Smuzhiyun 			btrfs_release_path(path);
588*4882a593Smuzhiyun 			goto next;
589*4882a593Smuzhiyun 		}
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 		if (key.objectid < block_group->start) {
592*4882a593Smuzhiyun 			path->slots[0]++;
593*4882a593Smuzhiyun 			continue;
594*4882a593Smuzhiyun 		}
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 		if (key.objectid >= block_group->start + block_group->length)
597*4882a593Smuzhiyun 			break;
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
600*4882a593Smuzhiyun 		    key.type == BTRFS_METADATA_ITEM_KEY) {
601*4882a593Smuzhiyun 			total_found += add_new_free_space(block_group, last,
602*4882a593Smuzhiyun 							  key.objectid);
603*4882a593Smuzhiyun 			if (key.type == BTRFS_METADATA_ITEM_KEY)
604*4882a593Smuzhiyun 				last = key.objectid +
605*4882a593Smuzhiyun 					fs_info->nodesize;
606*4882a593Smuzhiyun 			else
607*4882a593Smuzhiyun 				last = key.objectid + key.offset;
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 			if (total_found > CACHING_CTL_WAKE_UP) {
610*4882a593Smuzhiyun 				total_found = 0;
611*4882a593Smuzhiyun 				if (wakeup)
612*4882a593Smuzhiyun 					wake_up(&caching_ctl->wait);
613*4882a593Smuzhiyun 			}
614*4882a593Smuzhiyun 		}
615*4882a593Smuzhiyun 		path->slots[0]++;
616*4882a593Smuzhiyun 	}
617*4882a593Smuzhiyun 	ret = 0;
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	total_found += add_new_free_space(block_group, last,
620*4882a593Smuzhiyun 				block_group->start + block_group->length);
621*4882a593Smuzhiyun 	caching_ctl->progress = (u64)-1;
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun out:
624*4882a593Smuzhiyun 	btrfs_free_path(path);
625*4882a593Smuzhiyun 	return ret;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun 
caching_thread(struct btrfs_work * work)628*4882a593Smuzhiyun static noinline void caching_thread(struct btrfs_work *work)
629*4882a593Smuzhiyun {
630*4882a593Smuzhiyun 	struct btrfs_block_group *block_group;
631*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info;
632*4882a593Smuzhiyun 	struct btrfs_caching_control *caching_ctl;
633*4882a593Smuzhiyun 	int ret;
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	caching_ctl = container_of(work, struct btrfs_caching_control, work);
636*4882a593Smuzhiyun 	block_group = caching_ctl->block_group;
637*4882a593Smuzhiyun 	fs_info = block_group->fs_info;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	mutex_lock(&caching_ctl->mutex);
640*4882a593Smuzhiyun 	down_read(&fs_info->commit_root_sem);
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	/*
643*4882a593Smuzhiyun 	 * If we are in the transaction that populated the free space tree we
644*4882a593Smuzhiyun 	 * can't actually cache from the free space tree as our commit root and
645*4882a593Smuzhiyun 	 * real root are the same, so we could change the contents of the blocks
646*4882a593Smuzhiyun 	 * while caching.  Instead do the slow caching in this case, and after
647*4882a593Smuzhiyun 	 * the transaction has committed we will be safe.
648*4882a593Smuzhiyun 	 */
649*4882a593Smuzhiyun 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
650*4882a593Smuzhiyun 	    !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags)))
651*4882a593Smuzhiyun 		ret = load_free_space_tree(caching_ctl);
652*4882a593Smuzhiyun 	else
653*4882a593Smuzhiyun 		ret = load_extent_tree_free(caching_ctl);
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	spin_lock(&block_group->lock);
656*4882a593Smuzhiyun 	block_group->caching_ctl = NULL;
657*4882a593Smuzhiyun 	block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
658*4882a593Smuzhiyun 	spin_unlock(&block_group->lock);
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun #ifdef CONFIG_BTRFS_DEBUG
661*4882a593Smuzhiyun 	if (btrfs_should_fragment_free_space(block_group)) {
662*4882a593Smuzhiyun 		u64 bytes_used;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 		spin_lock(&block_group->space_info->lock);
665*4882a593Smuzhiyun 		spin_lock(&block_group->lock);
666*4882a593Smuzhiyun 		bytes_used = block_group->length - block_group->used;
667*4882a593Smuzhiyun 		block_group->space_info->bytes_used += bytes_used >> 1;
668*4882a593Smuzhiyun 		spin_unlock(&block_group->lock);
669*4882a593Smuzhiyun 		spin_unlock(&block_group->space_info->lock);
670*4882a593Smuzhiyun 		fragment_free_space(block_group);
671*4882a593Smuzhiyun 	}
672*4882a593Smuzhiyun #endif
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	caching_ctl->progress = (u64)-1;
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	up_read(&fs_info->commit_root_sem);
677*4882a593Smuzhiyun 	btrfs_free_excluded_extents(block_group);
678*4882a593Smuzhiyun 	mutex_unlock(&caching_ctl->mutex);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	wake_up(&caching_ctl->wait);
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	btrfs_put_caching_control(caching_ctl);
683*4882a593Smuzhiyun 	btrfs_put_block_group(block_group);
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun 
btrfs_cache_block_group(struct btrfs_block_group * cache,int load_cache_only)686*4882a593Smuzhiyun int btrfs_cache_block_group(struct btrfs_block_group *cache, int load_cache_only)
687*4882a593Smuzhiyun {
688*4882a593Smuzhiyun 	DEFINE_WAIT(wait);
689*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = cache->fs_info;
690*4882a593Smuzhiyun 	struct btrfs_caching_control *caching_ctl;
691*4882a593Smuzhiyun 	int ret = 0;
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
694*4882a593Smuzhiyun 	if (!caching_ctl)
695*4882a593Smuzhiyun 		return -ENOMEM;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	INIT_LIST_HEAD(&caching_ctl->list);
698*4882a593Smuzhiyun 	mutex_init(&caching_ctl->mutex);
699*4882a593Smuzhiyun 	init_waitqueue_head(&caching_ctl->wait);
700*4882a593Smuzhiyun 	caching_ctl->block_group = cache;
701*4882a593Smuzhiyun 	caching_ctl->progress = cache->start;
702*4882a593Smuzhiyun 	refcount_set(&caching_ctl->count, 1);
703*4882a593Smuzhiyun 	btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	spin_lock(&cache->lock);
706*4882a593Smuzhiyun 	/*
707*4882a593Smuzhiyun 	 * This should be a rare occasion, but this could happen I think in the
708*4882a593Smuzhiyun 	 * case where one thread starts to load the space cache info, and then
709*4882a593Smuzhiyun 	 * some other thread starts a transaction commit which tries to do an
710*4882a593Smuzhiyun 	 * allocation while the other thread is still loading the space cache
711*4882a593Smuzhiyun 	 * info.  The previous loop should have kept us from choosing this block
712*4882a593Smuzhiyun 	 * group, but if we've moved to the state where we will wait on caching
713*4882a593Smuzhiyun 	 * block groups we need to first check if we're doing a fast load here,
714*4882a593Smuzhiyun 	 * so we can wait for it to finish, otherwise we could end up allocating
715*4882a593Smuzhiyun 	 * from a block group who's cache gets evicted for one reason or
716*4882a593Smuzhiyun 	 * another.
717*4882a593Smuzhiyun 	 */
718*4882a593Smuzhiyun 	while (cache->cached == BTRFS_CACHE_FAST) {
719*4882a593Smuzhiyun 		struct btrfs_caching_control *ctl;
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 		ctl = cache->caching_ctl;
722*4882a593Smuzhiyun 		refcount_inc(&ctl->count);
723*4882a593Smuzhiyun 		prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
724*4882a593Smuzhiyun 		spin_unlock(&cache->lock);
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 		schedule();
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 		finish_wait(&ctl->wait, &wait);
729*4882a593Smuzhiyun 		btrfs_put_caching_control(ctl);
730*4882a593Smuzhiyun 		spin_lock(&cache->lock);
731*4882a593Smuzhiyun 	}
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	if (cache->cached != BTRFS_CACHE_NO) {
734*4882a593Smuzhiyun 		spin_unlock(&cache->lock);
735*4882a593Smuzhiyun 		kfree(caching_ctl);
736*4882a593Smuzhiyun 		return 0;
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun 	WARN_ON(cache->caching_ctl);
739*4882a593Smuzhiyun 	cache->caching_ctl = caching_ctl;
740*4882a593Smuzhiyun 	cache->cached = BTRFS_CACHE_FAST;
741*4882a593Smuzhiyun 	spin_unlock(&cache->lock);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
744*4882a593Smuzhiyun 		mutex_lock(&caching_ctl->mutex);
745*4882a593Smuzhiyun 		ret = load_free_space_cache(cache);
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 		spin_lock(&cache->lock);
748*4882a593Smuzhiyun 		if (ret == 1) {
749*4882a593Smuzhiyun 			cache->caching_ctl = NULL;
750*4882a593Smuzhiyun 			cache->cached = BTRFS_CACHE_FINISHED;
751*4882a593Smuzhiyun 			cache->last_byte_to_unpin = (u64)-1;
752*4882a593Smuzhiyun 			caching_ctl->progress = (u64)-1;
753*4882a593Smuzhiyun 		} else {
754*4882a593Smuzhiyun 			if (load_cache_only) {
755*4882a593Smuzhiyun 				cache->caching_ctl = NULL;
756*4882a593Smuzhiyun 				cache->cached = BTRFS_CACHE_NO;
757*4882a593Smuzhiyun 			} else {
758*4882a593Smuzhiyun 				cache->cached = BTRFS_CACHE_STARTED;
759*4882a593Smuzhiyun 				cache->has_caching_ctl = 1;
760*4882a593Smuzhiyun 			}
761*4882a593Smuzhiyun 		}
762*4882a593Smuzhiyun 		spin_unlock(&cache->lock);
763*4882a593Smuzhiyun #ifdef CONFIG_BTRFS_DEBUG
764*4882a593Smuzhiyun 		if (ret == 1 &&
765*4882a593Smuzhiyun 		    btrfs_should_fragment_free_space(cache)) {
766*4882a593Smuzhiyun 			u64 bytes_used;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 			spin_lock(&cache->space_info->lock);
769*4882a593Smuzhiyun 			spin_lock(&cache->lock);
770*4882a593Smuzhiyun 			bytes_used = cache->length - cache->used;
771*4882a593Smuzhiyun 			cache->space_info->bytes_used += bytes_used >> 1;
772*4882a593Smuzhiyun 			spin_unlock(&cache->lock);
773*4882a593Smuzhiyun 			spin_unlock(&cache->space_info->lock);
774*4882a593Smuzhiyun 			fragment_free_space(cache);
775*4882a593Smuzhiyun 		}
776*4882a593Smuzhiyun #endif
777*4882a593Smuzhiyun 		mutex_unlock(&caching_ctl->mutex);
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 		wake_up(&caching_ctl->wait);
780*4882a593Smuzhiyun 		if (ret == 1) {
781*4882a593Smuzhiyun 			btrfs_put_caching_control(caching_ctl);
782*4882a593Smuzhiyun 			btrfs_free_excluded_extents(cache);
783*4882a593Smuzhiyun 			return 0;
784*4882a593Smuzhiyun 		}
785*4882a593Smuzhiyun 	} else {
786*4882a593Smuzhiyun 		/*
787*4882a593Smuzhiyun 		 * We're either using the free space tree or no caching at all.
788*4882a593Smuzhiyun 		 * Set cached to the appropriate value and wakeup any waiters.
789*4882a593Smuzhiyun 		 */
790*4882a593Smuzhiyun 		spin_lock(&cache->lock);
791*4882a593Smuzhiyun 		if (load_cache_only) {
792*4882a593Smuzhiyun 			cache->caching_ctl = NULL;
793*4882a593Smuzhiyun 			cache->cached = BTRFS_CACHE_NO;
794*4882a593Smuzhiyun 		} else {
795*4882a593Smuzhiyun 			cache->cached = BTRFS_CACHE_STARTED;
796*4882a593Smuzhiyun 			cache->has_caching_ctl = 1;
797*4882a593Smuzhiyun 		}
798*4882a593Smuzhiyun 		spin_unlock(&cache->lock);
799*4882a593Smuzhiyun 		wake_up(&caching_ctl->wait);
800*4882a593Smuzhiyun 	}
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	if (load_cache_only) {
803*4882a593Smuzhiyun 		btrfs_put_caching_control(caching_ctl);
804*4882a593Smuzhiyun 		return 0;
805*4882a593Smuzhiyun 	}
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	down_write(&fs_info->commit_root_sem);
808*4882a593Smuzhiyun 	refcount_inc(&caching_ctl->count);
809*4882a593Smuzhiyun 	list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
810*4882a593Smuzhiyun 	up_write(&fs_info->commit_root_sem);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	btrfs_get_block_group(cache);
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	return ret;
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun 
clear_avail_alloc_bits(struct btrfs_fs_info * fs_info,u64 flags)819*4882a593Smuzhiyun static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
820*4882a593Smuzhiyun {
821*4882a593Smuzhiyun 	u64 extra_flags = chunk_to_extended(flags) &
822*4882a593Smuzhiyun 				BTRFS_EXTENDED_PROFILE_MASK;
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	write_seqlock(&fs_info->profiles_lock);
825*4882a593Smuzhiyun 	if (flags & BTRFS_BLOCK_GROUP_DATA)
826*4882a593Smuzhiyun 		fs_info->avail_data_alloc_bits &= ~extra_flags;
827*4882a593Smuzhiyun 	if (flags & BTRFS_BLOCK_GROUP_METADATA)
828*4882a593Smuzhiyun 		fs_info->avail_metadata_alloc_bits &= ~extra_flags;
829*4882a593Smuzhiyun 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
830*4882a593Smuzhiyun 		fs_info->avail_system_alloc_bits &= ~extra_flags;
831*4882a593Smuzhiyun 	write_sequnlock(&fs_info->profiles_lock);
832*4882a593Smuzhiyun }
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun /*
835*4882a593Smuzhiyun  * Clear incompat bits for the following feature(s):
836*4882a593Smuzhiyun  *
837*4882a593Smuzhiyun  * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
838*4882a593Smuzhiyun  *            in the whole filesystem
839*4882a593Smuzhiyun  *
840*4882a593Smuzhiyun  * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
841*4882a593Smuzhiyun  */
clear_incompat_bg_bits(struct btrfs_fs_info * fs_info,u64 flags)842*4882a593Smuzhiyun static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags)
843*4882a593Smuzhiyun {
844*4882a593Smuzhiyun 	bool found_raid56 = false;
845*4882a593Smuzhiyun 	bool found_raid1c34 = false;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) ||
848*4882a593Smuzhiyun 	    (flags & BTRFS_BLOCK_GROUP_RAID1C3) ||
849*4882a593Smuzhiyun 	    (flags & BTRFS_BLOCK_GROUP_RAID1C4)) {
850*4882a593Smuzhiyun 		struct list_head *head = &fs_info->space_info;
851*4882a593Smuzhiyun 		struct btrfs_space_info *sinfo;
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 		list_for_each_entry_rcu(sinfo, head, list) {
854*4882a593Smuzhiyun 			down_read(&sinfo->groups_sem);
855*4882a593Smuzhiyun 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5]))
856*4882a593Smuzhiyun 				found_raid56 = true;
857*4882a593Smuzhiyun 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6]))
858*4882a593Smuzhiyun 				found_raid56 = true;
859*4882a593Smuzhiyun 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3]))
860*4882a593Smuzhiyun 				found_raid1c34 = true;
861*4882a593Smuzhiyun 			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4]))
862*4882a593Smuzhiyun 				found_raid1c34 = true;
863*4882a593Smuzhiyun 			up_read(&sinfo->groups_sem);
864*4882a593Smuzhiyun 		}
865*4882a593Smuzhiyun 		if (!found_raid56)
866*4882a593Smuzhiyun 			btrfs_clear_fs_incompat(fs_info, RAID56);
867*4882a593Smuzhiyun 		if (!found_raid1c34)
868*4882a593Smuzhiyun 			btrfs_clear_fs_incompat(fs_info, RAID1C34);
869*4882a593Smuzhiyun 	}
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun 
remove_block_group_item(struct btrfs_trans_handle * trans,struct btrfs_path * path,struct btrfs_block_group * block_group)872*4882a593Smuzhiyun static int remove_block_group_item(struct btrfs_trans_handle *trans,
873*4882a593Smuzhiyun 				   struct btrfs_path *path,
874*4882a593Smuzhiyun 				   struct btrfs_block_group *block_group)
875*4882a593Smuzhiyun {
876*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = trans->fs_info;
877*4882a593Smuzhiyun 	struct btrfs_root *root;
878*4882a593Smuzhiyun 	struct btrfs_key key;
879*4882a593Smuzhiyun 	int ret;
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	root = fs_info->extent_root;
882*4882a593Smuzhiyun 	key.objectid = block_group->start;
883*4882a593Smuzhiyun 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
884*4882a593Smuzhiyun 	key.offset = block_group->length;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
887*4882a593Smuzhiyun 	if (ret > 0)
888*4882a593Smuzhiyun 		ret = -ENOENT;
889*4882a593Smuzhiyun 	if (ret < 0)
890*4882a593Smuzhiyun 		return ret;
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	ret = btrfs_del_item(trans, root, path);
893*4882a593Smuzhiyun 	return ret;
894*4882a593Smuzhiyun }
895*4882a593Smuzhiyun 
btrfs_remove_block_group(struct btrfs_trans_handle * trans,u64 group_start,struct extent_map * em)896*4882a593Smuzhiyun int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
897*4882a593Smuzhiyun 			     u64 group_start, struct extent_map *em)
898*4882a593Smuzhiyun {
899*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = trans->fs_info;
900*4882a593Smuzhiyun 	struct btrfs_path *path;
901*4882a593Smuzhiyun 	struct btrfs_block_group *block_group;
902*4882a593Smuzhiyun 	struct btrfs_free_cluster *cluster;
903*4882a593Smuzhiyun 	struct btrfs_root *tree_root = fs_info->tree_root;
904*4882a593Smuzhiyun 	struct btrfs_key key;
905*4882a593Smuzhiyun 	struct inode *inode;
906*4882a593Smuzhiyun 	struct kobject *kobj = NULL;
907*4882a593Smuzhiyun 	int ret;
908*4882a593Smuzhiyun 	int index;
909*4882a593Smuzhiyun 	int factor;
910*4882a593Smuzhiyun 	struct btrfs_caching_control *caching_ctl = NULL;
911*4882a593Smuzhiyun 	bool remove_em;
912*4882a593Smuzhiyun 	bool remove_rsv = false;
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	block_group = btrfs_lookup_block_group(fs_info, group_start);
915*4882a593Smuzhiyun 	BUG_ON(!block_group);
916*4882a593Smuzhiyun 	BUG_ON(!block_group->ro);
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	trace_btrfs_remove_block_group(block_group);
919*4882a593Smuzhiyun 	/*
920*4882a593Smuzhiyun 	 * Free the reserved super bytes from this block group before
921*4882a593Smuzhiyun 	 * remove it.
922*4882a593Smuzhiyun 	 */
923*4882a593Smuzhiyun 	btrfs_free_excluded_extents(block_group);
924*4882a593Smuzhiyun 	btrfs_free_ref_tree_range(fs_info, block_group->start,
925*4882a593Smuzhiyun 				  block_group->length);
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	index = btrfs_bg_flags_to_raid_index(block_group->flags);
928*4882a593Smuzhiyun 	factor = btrfs_bg_type_to_factor(block_group->flags);
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	/* make sure this block group isn't part of an allocation cluster */
931*4882a593Smuzhiyun 	cluster = &fs_info->data_alloc_cluster;
932*4882a593Smuzhiyun 	spin_lock(&cluster->refill_lock);
933*4882a593Smuzhiyun 	btrfs_return_cluster_to_free_space(block_group, cluster);
934*4882a593Smuzhiyun 	spin_unlock(&cluster->refill_lock);
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	/*
937*4882a593Smuzhiyun 	 * make sure this block group isn't part of a metadata
938*4882a593Smuzhiyun 	 * allocation cluster
939*4882a593Smuzhiyun 	 */
940*4882a593Smuzhiyun 	cluster = &fs_info->meta_alloc_cluster;
941*4882a593Smuzhiyun 	spin_lock(&cluster->refill_lock);
942*4882a593Smuzhiyun 	btrfs_return_cluster_to_free_space(block_group, cluster);
943*4882a593Smuzhiyun 	spin_unlock(&cluster->refill_lock);
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	path = btrfs_alloc_path();
946*4882a593Smuzhiyun 	if (!path) {
947*4882a593Smuzhiyun 		ret = -ENOMEM;
948*4882a593Smuzhiyun 		goto out;
949*4882a593Smuzhiyun 	}
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	/*
952*4882a593Smuzhiyun 	 * get the inode first so any iput calls done for the io_list
953*4882a593Smuzhiyun 	 * aren't the final iput (no unlinks allowed now)
954*4882a593Smuzhiyun 	 */
955*4882a593Smuzhiyun 	inode = lookup_free_space_inode(block_group, path);
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	mutex_lock(&trans->transaction->cache_write_mutex);
958*4882a593Smuzhiyun 	/*
959*4882a593Smuzhiyun 	 * Make sure our free space cache IO is done before removing the
960*4882a593Smuzhiyun 	 * free space inode
961*4882a593Smuzhiyun 	 */
962*4882a593Smuzhiyun 	spin_lock(&trans->transaction->dirty_bgs_lock);
963*4882a593Smuzhiyun 	if (!list_empty(&block_group->io_list)) {
964*4882a593Smuzhiyun 		list_del_init(&block_group->io_list);
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 		WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 		spin_unlock(&trans->transaction->dirty_bgs_lock);
969*4882a593Smuzhiyun 		btrfs_wait_cache_io(trans, block_group, path);
970*4882a593Smuzhiyun 		btrfs_put_block_group(block_group);
971*4882a593Smuzhiyun 		spin_lock(&trans->transaction->dirty_bgs_lock);
972*4882a593Smuzhiyun 	}
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	if (!list_empty(&block_group->dirty_list)) {
975*4882a593Smuzhiyun 		list_del_init(&block_group->dirty_list);
976*4882a593Smuzhiyun 		remove_rsv = true;
977*4882a593Smuzhiyun 		btrfs_put_block_group(block_group);
978*4882a593Smuzhiyun 	}
979*4882a593Smuzhiyun 	spin_unlock(&trans->transaction->dirty_bgs_lock);
980*4882a593Smuzhiyun 	mutex_unlock(&trans->transaction->cache_write_mutex);
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	if (!IS_ERR(inode)) {
983*4882a593Smuzhiyun 		ret = btrfs_orphan_add(trans, BTRFS_I(inode));
984*4882a593Smuzhiyun 		if (ret) {
985*4882a593Smuzhiyun 			btrfs_add_delayed_iput(inode);
986*4882a593Smuzhiyun 			goto out;
987*4882a593Smuzhiyun 		}
988*4882a593Smuzhiyun 		clear_nlink(inode);
989*4882a593Smuzhiyun 		/* One for the block groups ref */
990*4882a593Smuzhiyun 		spin_lock(&block_group->lock);
991*4882a593Smuzhiyun 		if (block_group->iref) {
992*4882a593Smuzhiyun 			block_group->iref = 0;
993*4882a593Smuzhiyun 			block_group->inode = NULL;
994*4882a593Smuzhiyun 			spin_unlock(&block_group->lock);
995*4882a593Smuzhiyun 			iput(inode);
996*4882a593Smuzhiyun 		} else {
997*4882a593Smuzhiyun 			spin_unlock(&block_group->lock);
998*4882a593Smuzhiyun 		}
999*4882a593Smuzhiyun 		/* One for our lookup ref */
1000*4882a593Smuzhiyun 		btrfs_add_delayed_iput(inode);
1001*4882a593Smuzhiyun 	}
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1004*4882a593Smuzhiyun 	key.type = 0;
1005*4882a593Smuzhiyun 	key.offset = block_group->start;
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
1008*4882a593Smuzhiyun 	if (ret < 0)
1009*4882a593Smuzhiyun 		goto out;
1010*4882a593Smuzhiyun 	if (ret > 0)
1011*4882a593Smuzhiyun 		btrfs_release_path(path);
1012*4882a593Smuzhiyun 	if (ret == 0) {
1013*4882a593Smuzhiyun 		ret = btrfs_del_item(trans, tree_root, path);
1014*4882a593Smuzhiyun 		if (ret)
1015*4882a593Smuzhiyun 			goto out;
1016*4882a593Smuzhiyun 		btrfs_release_path(path);
1017*4882a593Smuzhiyun 	}
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	spin_lock(&fs_info->block_group_cache_lock);
1020*4882a593Smuzhiyun 	rb_erase(&block_group->cache_node,
1021*4882a593Smuzhiyun 		 &fs_info->block_group_cache_tree);
1022*4882a593Smuzhiyun 	RB_CLEAR_NODE(&block_group->cache_node);
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	/* Once for the block groups rbtree */
1025*4882a593Smuzhiyun 	btrfs_put_block_group(block_group);
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 	if (fs_info->first_logical_byte == block_group->start)
1028*4882a593Smuzhiyun 		fs_info->first_logical_byte = (u64)-1;
1029*4882a593Smuzhiyun 	spin_unlock(&fs_info->block_group_cache_lock);
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 	down_write(&block_group->space_info->groups_sem);
1032*4882a593Smuzhiyun 	/*
1033*4882a593Smuzhiyun 	 * we must use list_del_init so people can check to see if they
1034*4882a593Smuzhiyun 	 * are still on the list after taking the semaphore
1035*4882a593Smuzhiyun 	 */
1036*4882a593Smuzhiyun 	list_del_init(&block_group->list);
1037*4882a593Smuzhiyun 	if (list_empty(&block_group->space_info->block_groups[index])) {
1038*4882a593Smuzhiyun 		kobj = block_group->space_info->block_group_kobjs[index];
1039*4882a593Smuzhiyun 		block_group->space_info->block_group_kobjs[index] = NULL;
1040*4882a593Smuzhiyun 		clear_avail_alloc_bits(fs_info, block_group->flags);
1041*4882a593Smuzhiyun 	}
1042*4882a593Smuzhiyun 	up_write(&block_group->space_info->groups_sem);
1043*4882a593Smuzhiyun 	clear_incompat_bg_bits(fs_info, block_group->flags);
1044*4882a593Smuzhiyun 	if (kobj) {
1045*4882a593Smuzhiyun 		kobject_del(kobj);
1046*4882a593Smuzhiyun 		kobject_put(kobj);
1047*4882a593Smuzhiyun 	}
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 	if (block_group->has_caching_ctl)
1050*4882a593Smuzhiyun 		caching_ctl = btrfs_get_caching_control(block_group);
1051*4882a593Smuzhiyun 	if (block_group->cached == BTRFS_CACHE_STARTED)
1052*4882a593Smuzhiyun 		btrfs_wait_block_group_cache_done(block_group);
1053*4882a593Smuzhiyun 	if (block_group->has_caching_ctl) {
1054*4882a593Smuzhiyun 		down_write(&fs_info->commit_root_sem);
1055*4882a593Smuzhiyun 		if (!caching_ctl) {
1056*4882a593Smuzhiyun 			struct btrfs_caching_control *ctl;
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 			list_for_each_entry(ctl,
1059*4882a593Smuzhiyun 				    &fs_info->caching_block_groups, list)
1060*4882a593Smuzhiyun 				if (ctl->block_group == block_group) {
1061*4882a593Smuzhiyun 					caching_ctl = ctl;
1062*4882a593Smuzhiyun 					refcount_inc(&caching_ctl->count);
1063*4882a593Smuzhiyun 					break;
1064*4882a593Smuzhiyun 				}
1065*4882a593Smuzhiyun 		}
1066*4882a593Smuzhiyun 		if (caching_ctl)
1067*4882a593Smuzhiyun 			list_del_init(&caching_ctl->list);
1068*4882a593Smuzhiyun 		up_write(&fs_info->commit_root_sem);
1069*4882a593Smuzhiyun 		if (caching_ctl) {
1070*4882a593Smuzhiyun 			/* Once for the caching bgs list and once for us. */
1071*4882a593Smuzhiyun 			btrfs_put_caching_control(caching_ctl);
1072*4882a593Smuzhiyun 			btrfs_put_caching_control(caching_ctl);
1073*4882a593Smuzhiyun 		}
1074*4882a593Smuzhiyun 	}
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	spin_lock(&trans->transaction->dirty_bgs_lock);
1077*4882a593Smuzhiyun 	WARN_ON(!list_empty(&block_group->dirty_list));
1078*4882a593Smuzhiyun 	WARN_ON(!list_empty(&block_group->io_list));
1079*4882a593Smuzhiyun 	spin_unlock(&trans->transaction->dirty_bgs_lock);
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	btrfs_remove_free_space_cache(block_group);
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	spin_lock(&block_group->space_info->lock);
1084*4882a593Smuzhiyun 	list_del_init(&block_group->ro_list);
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 	if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
1087*4882a593Smuzhiyun 		WARN_ON(block_group->space_info->total_bytes
1088*4882a593Smuzhiyun 			< block_group->length);
1089*4882a593Smuzhiyun 		WARN_ON(block_group->space_info->bytes_readonly
1090*4882a593Smuzhiyun 			< block_group->length);
1091*4882a593Smuzhiyun 		WARN_ON(block_group->space_info->disk_total
1092*4882a593Smuzhiyun 			< block_group->length * factor);
1093*4882a593Smuzhiyun 	}
1094*4882a593Smuzhiyun 	block_group->space_info->total_bytes -= block_group->length;
1095*4882a593Smuzhiyun 	block_group->space_info->bytes_readonly -= block_group->length;
1096*4882a593Smuzhiyun 	block_group->space_info->disk_total -= block_group->length * factor;
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	spin_unlock(&block_group->space_info->lock);
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	/*
1101*4882a593Smuzhiyun 	 * Remove the free space for the block group from the free space tree
1102*4882a593Smuzhiyun 	 * and the block group's item from the extent tree before marking the
1103*4882a593Smuzhiyun 	 * block group as removed. This is to prevent races with tasks that
1104*4882a593Smuzhiyun 	 * freeze and unfreeze a block group, this task and another task
1105*4882a593Smuzhiyun 	 * allocating a new block group - the unfreeze task ends up removing
1106*4882a593Smuzhiyun 	 * the block group's extent map before the task calling this function
1107*4882a593Smuzhiyun 	 * deletes the block group item from the extent tree, allowing for
1108*4882a593Smuzhiyun 	 * another task to attempt to create another block group with the same
1109*4882a593Smuzhiyun 	 * item key (and failing with -EEXIST and a transaction abort).
1110*4882a593Smuzhiyun 	 */
1111*4882a593Smuzhiyun 	ret = remove_block_group_free_space(trans, block_group);
1112*4882a593Smuzhiyun 	if (ret)
1113*4882a593Smuzhiyun 		goto out;
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun 	ret = remove_block_group_item(trans, path, block_group);
1116*4882a593Smuzhiyun 	if (ret < 0)
1117*4882a593Smuzhiyun 		goto out;
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 	spin_lock(&block_group->lock);
1120*4882a593Smuzhiyun 	block_group->removed = 1;
1121*4882a593Smuzhiyun 	/*
1122*4882a593Smuzhiyun 	 * At this point trimming or scrub can't start on this block group,
1123*4882a593Smuzhiyun 	 * because we removed the block group from the rbtree
1124*4882a593Smuzhiyun 	 * fs_info->block_group_cache_tree so no one can't find it anymore and
1125*4882a593Smuzhiyun 	 * even if someone already got this block group before we removed it
1126*4882a593Smuzhiyun 	 * from the rbtree, they have already incremented block_group->frozen -
1127*4882a593Smuzhiyun 	 * if they didn't, for the trimming case they won't find any free space
1128*4882a593Smuzhiyun 	 * entries because we already removed them all when we called
1129*4882a593Smuzhiyun 	 * btrfs_remove_free_space_cache().
1130*4882a593Smuzhiyun 	 *
1131*4882a593Smuzhiyun 	 * And we must not remove the extent map from the fs_info->mapping_tree
1132*4882a593Smuzhiyun 	 * to prevent the same logical address range and physical device space
1133*4882a593Smuzhiyun 	 * ranges from being reused for a new block group. This is needed to
1134*4882a593Smuzhiyun 	 * avoid races with trimming and scrub.
1135*4882a593Smuzhiyun 	 *
1136*4882a593Smuzhiyun 	 * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
1137*4882a593Smuzhiyun 	 * completely transactionless, so while it is trimming a range the
1138*4882a593Smuzhiyun 	 * currently running transaction might finish and a new one start,
1139*4882a593Smuzhiyun 	 * allowing for new block groups to be created that can reuse the same
1140*4882a593Smuzhiyun 	 * physical device locations unless we take this special care.
1141*4882a593Smuzhiyun 	 *
1142*4882a593Smuzhiyun 	 * There may also be an implicit trim operation if the file system
1143*4882a593Smuzhiyun 	 * is mounted with -odiscard. The same protections must remain
1144*4882a593Smuzhiyun 	 * in place until the extents have been discarded completely when
1145*4882a593Smuzhiyun 	 * the transaction commit has completed.
1146*4882a593Smuzhiyun 	 */
1147*4882a593Smuzhiyun 	remove_em = (atomic_read(&block_group->frozen) == 0);
1148*4882a593Smuzhiyun 	spin_unlock(&block_group->lock);
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	if (remove_em) {
1151*4882a593Smuzhiyun 		struct extent_map_tree *em_tree;
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun 		em_tree = &fs_info->mapping_tree;
1154*4882a593Smuzhiyun 		write_lock(&em_tree->lock);
1155*4882a593Smuzhiyun 		remove_extent_mapping(em_tree, em);
1156*4882a593Smuzhiyun 		write_unlock(&em_tree->lock);
1157*4882a593Smuzhiyun 		/* once for the tree */
1158*4882a593Smuzhiyun 		free_extent_map(em);
1159*4882a593Smuzhiyun 	}
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun out:
1162*4882a593Smuzhiyun 	/* Once for the lookup reference */
1163*4882a593Smuzhiyun 	btrfs_put_block_group(block_group);
1164*4882a593Smuzhiyun 	if (remove_rsv)
1165*4882a593Smuzhiyun 		btrfs_delayed_refs_rsv_release(fs_info, 1);
1166*4882a593Smuzhiyun 	btrfs_free_path(path);
1167*4882a593Smuzhiyun 	return ret;
1168*4882a593Smuzhiyun }
1169*4882a593Smuzhiyun 
btrfs_start_trans_remove_block_group(struct btrfs_fs_info * fs_info,const u64 chunk_offset)1170*4882a593Smuzhiyun struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
1171*4882a593Smuzhiyun 		struct btrfs_fs_info *fs_info, const u64 chunk_offset)
1172*4882a593Smuzhiyun {
1173*4882a593Smuzhiyun 	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1174*4882a593Smuzhiyun 	struct extent_map *em;
1175*4882a593Smuzhiyun 	struct map_lookup *map;
1176*4882a593Smuzhiyun 	unsigned int num_items;
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	read_lock(&em_tree->lock);
1179*4882a593Smuzhiyun 	em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1180*4882a593Smuzhiyun 	read_unlock(&em_tree->lock);
1181*4882a593Smuzhiyun 	ASSERT(em && em->start == chunk_offset);
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	/*
1184*4882a593Smuzhiyun 	 * We need to reserve 3 + N units from the metadata space info in order
1185*4882a593Smuzhiyun 	 * to remove a block group (done at btrfs_remove_chunk() and at
1186*4882a593Smuzhiyun 	 * btrfs_remove_block_group()), which are used for:
1187*4882a593Smuzhiyun 	 *
1188*4882a593Smuzhiyun 	 * 1 unit for adding the free space inode's orphan (located in the tree
1189*4882a593Smuzhiyun 	 * of tree roots).
1190*4882a593Smuzhiyun 	 * 1 unit for deleting the block group item (located in the extent
1191*4882a593Smuzhiyun 	 * tree).
1192*4882a593Smuzhiyun 	 * 1 unit for deleting the free space item (located in tree of tree
1193*4882a593Smuzhiyun 	 * roots).
1194*4882a593Smuzhiyun 	 * N units for deleting N device extent items corresponding to each
1195*4882a593Smuzhiyun 	 * stripe (located in the device tree).
1196*4882a593Smuzhiyun 	 *
1197*4882a593Smuzhiyun 	 * In order to remove a block group we also need to reserve units in the
1198*4882a593Smuzhiyun 	 * system space info in order to update the chunk tree (update one or
1199*4882a593Smuzhiyun 	 * more device items and remove one chunk item), but this is done at
1200*4882a593Smuzhiyun 	 * btrfs_remove_chunk() through a call to check_system_chunk().
1201*4882a593Smuzhiyun 	 */
1202*4882a593Smuzhiyun 	map = em->map_lookup;
1203*4882a593Smuzhiyun 	num_items = 3 + map->num_stripes;
1204*4882a593Smuzhiyun 	free_extent_map(em);
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 	return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
1207*4882a593Smuzhiyun 							   num_items);
1208*4882a593Smuzhiyun }
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun /*
1211*4882a593Smuzhiyun  * Mark block group @cache read-only, so later write won't happen to block
1212*4882a593Smuzhiyun  * group @cache.
1213*4882a593Smuzhiyun  *
1214*4882a593Smuzhiyun  * If @force is not set, this function will only mark the block group readonly
1215*4882a593Smuzhiyun  * if we have enough free space (1M) in other metadata/system block groups.
1216*4882a593Smuzhiyun  * If @force is not set, this function will mark the block group readonly
1217*4882a593Smuzhiyun  * without checking free space.
1218*4882a593Smuzhiyun  *
1219*4882a593Smuzhiyun  * NOTE: This function doesn't care if other block groups can contain all the
1220*4882a593Smuzhiyun  * data in this block group. That check should be done by relocation routine,
1221*4882a593Smuzhiyun  * not this function.
1222*4882a593Smuzhiyun  */
inc_block_group_ro(struct btrfs_block_group * cache,int force)1223*4882a593Smuzhiyun static int inc_block_group_ro(struct btrfs_block_group *cache, int force)
1224*4882a593Smuzhiyun {
1225*4882a593Smuzhiyun 	struct btrfs_space_info *sinfo = cache->space_info;
1226*4882a593Smuzhiyun 	u64 num_bytes;
1227*4882a593Smuzhiyun 	int ret = -ENOSPC;
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 	spin_lock(&sinfo->lock);
1230*4882a593Smuzhiyun 	spin_lock(&cache->lock);
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	if (cache->swap_extents) {
1233*4882a593Smuzhiyun 		ret = -ETXTBSY;
1234*4882a593Smuzhiyun 		goto out;
1235*4882a593Smuzhiyun 	}
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	if (cache->ro) {
1238*4882a593Smuzhiyun 		cache->ro++;
1239*4882a593Smuzhiyun 		ret = 0;
1240*4882a593Smuzhiyun 		goto out;
1241*4882a593Smuzhiyun 	}
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 	num_bytes = cache->length - cache->reserved - cache->pinned -
1244*4882a593Smuzhiyun 		    cache->bytes_super - cache->used;
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	/*
1247*4882a593Smuzhiyun 	 * Data never overcommits, even in mixed mode, so do just the straight
1248*4882a593Smuzhiyun 	 * check of left over space in how much we have allocated.
1249*4882a593Smuzhiyun 	 */
1250*4882a593Smuzhiyun 	if (force) {
1251*4882a593Smuzhiyun 		ret = 0;
1252*4882a593Smuzhiyun 	} else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) {
1253*4882a593Smuzhiyun 		u64 sinfo_used = btrfs_space_info_used(sinfo, true);
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun 		/*
1256*4882a593Smuzhiyun 		 * Here we make sure if we mark this bg RO, we still have enough
1257*4882a593Smuzhiyun 		 * free space as buffer.
1258*4882a593Smuzhiyun 		 */
1259*4882a593Smuzhiyun 		if (sinfo_used + num_bytes <= sinfo->total_bytes)
1260*4882a593Smuzhiyun 			ret = 0;
1261*4882a593Smuzhiyun 	} else {
1262*4882a593Smuzhiyun 		/*
1263*4882a593Smuzhiyun 		 * We overcommit metadata, so we need to do the
1264*4882a593Smuzhiyun 		 * btrfs_can_overcommit check here, and we need to pass in
1265*4882a593Smuzhiyun 		 * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of
1266*4882a593Smuzhiyun 		 * leeway to allow us to mark this block group as read only.
1267*4882a593Smuzhiyun 		 */
1268*4882a593Smuzhiyun 		if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes,
1269*4882a593Smuzhiyun 					 BTRFS_RESERVE_NO_FLUSH))
1270*4882a593Smuzhiyun 			ret = 0;
1271*4882a593Smuzhiyun 	}
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 	if (!ret) {
1274*4882a593Smuzhiyun 		sinfo->bytes_readonly += num_bytes;
1275*4882a593Smuzhiyun 		cache->ro++;
1276*4882a593Smuzhiyun 		list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
1277*4882a593Smuzhiyun 	}
1278*4882a593Smuzhiyun out:
1279*4882a593Smuzhiyun 	spin_unlock(&cache->lock);
1280*4882a593Smuzhiyun 	spin_unlock(&sinfo->lock);
1281*4882a593Smuzhiyun 	if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) {
1282*4882a593Smuzhiyun 		btrfs_info(cache->fs_info,
1283*4882a593Smuzhiyun 			"unable to make block group %llu ro", cache->start);
1284*4882a593Smuzhiyun 		btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0);
1285*4882a593Smuzhiyun 	}
1286*4882a593Smuzhiyun 	return ret;
1287*4882a593Smuzhiyun }
1288*4882a593Smuzhiyun 
clean_pinned_extents(struct btrfs_trans_handle * trans,struct btrfs_block_group * bg)1289*4882a593Smuzhiyun static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
1290*4882a593Smuzhiyun 				 struct btrfs_block_group *bg)
1291*4882a593Smuzhiyun {
1292*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = bg->fs_info;
1293*4882a593Smuzhiyun 	struct btrfs_transaction *prev_trans = NULL;
1294*4882a593Smuzhiyun 	const u64 start = bg->start;
1295*4882a593Smuzhiyun 	const u64 end = start + bg->length - 1;
1296*4882a593Smuzhiyun 	int ret;
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	spin_lock(&fs_info->trans_lock);
1299*4882a593Smuzhiyun 	if (trans->transaction->list.prev != &fs_info->trans_list) {
1300*4882a593Smuzhiyun 		prev_trans = list_last_entry(&trans->transaction->list,
1301*4882a593Smuzhiyun 					     struct btrfs_transaction, list);
1302*4882a593Smuzhiyun 		refcount_inc(&prev_trans->use_count);
1303*4882a593Smuzhiyun 	}
1304*4882a593Smuzhiyun 	spin_unlock(&fs_info->trans_lock);
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	/*
1307*4882a593Smuzhiyun 	 * Hold the unused_bg_unpin_mutex lock to avoid racing with
1308*4882a593Smuzhiyun 	 * btrfs_finish_extent_commit(). If we are at transaction N, another
1309*4882a593Smuzhiyun 	 * task might be running finish_extent_commit() for the previous
1310*4882a593Smuzhiyun 	 * transaction N - 1, and have seen a range belonging to the block
1311*4882a593Smuzhiyun 	 * group in pinned_extents before we were able to clear the whole block
1312*4882a593Smuzhiyun 	 * group range from pinned_extents. This means that task can lookup for
1313*4882a593Smuzhiyun 	 * the block group after we unpinned it from pinned_extents and removed
1314*4882a593Smuzhiyun 	 * it, leading to a BUG_ON() at unpin_extent_range().
1315*4882a593Smuzhiyun 	 */
1316*4882a593Smuzhiyun 	mutex_lock(&fs_info->unused_bg_unpin_mutex);
1317*4882a593Smuzhiyun 	if (prev_trans) {
1318*4882a593Smuzhiyun 		ret = clear_extent_bits(&prev_trans->pinned_extents, start, end,
1319*4882a593Smuzhiyun 					EXTENT_DIRTY);
1320*4882a593Smuzhiyun 		if (ret)
1321*4882a593Smuzhiyun 			goto out;
1322*4882a593Smuzhiyun 	}
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun 	ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end,
1325*4882a593Smuzhiyun 				EXTENT_DIRTY);
1326*4882a593Smuzhiyun out:
1327*4882a593Smuzhiyun 	mutex_unlock(&fs_info->unused_bg_unpin_mutex);
1328*4882a593Smuzhiyun 	if (prev_trans)
1329*4882a593Smuzhiyun 		btrfs_put_transaction(prev_trans);
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 	return ret == 0;
1332*4882a593Smuzhiyun }
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun /*
1335*4882a593Smuzhiyun  * Process the unused_bgs list and remove any that don't have any allocated
1336*4882a593Smuzhiyun  * space inside of them.
1337*4882a593Smuzhiyun  */
btrfs_delete_unused_bgs(struct btrfs_fs_info * fs_info)1338*4882a593Smuzhiyun void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
1339*4882a593Smuzhiyun {
1340*4882a593Smuzhiyun 	struct btrfs_block_group *block_group;
1341*4882a593Smuzhiyun 	struct btrfs_space_info *space_info;
1342*4882a593Smuzhiyun 	struct btrfs_trans_handle *trans;
1343*4882a593Smuzhiyun 	const bool async_trim_enabled = btrfs_test_opt(fs_info, DISCARD_ASYNC);
1344*4882a593Smuzhiyun 	int ret = 0;
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 	if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1347*4882a593Smuzhiyun 		return;
1348*4882a593Smuzhiyun 
1349*4882a593Smuzhiyun 	spin_lock(&fs_info->unused_bgs_lock);
1350*4882a593Smuzhiyun 	while (!list_empty(&fs_info->unused_bgs)) {
1351*4882a593Smuzhiyun 		int trimming;
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 		block_group = list_first_entry(&fs_info->unused_bgs,
1354*4882a593Smuzhiyun 					       struct btrfs_block_group,
1355*4882a593Smuzhiyun 					       bg_list);
1356*4882a593Smuzhiyun 		list_del_init(&block_group->bg_list);
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 		space_info = block_group->space_info;
1359*4882a593Smuzhiyun 
1360*4882a593Smuzhiyun 		if (ret || btrfs_mixed_space_info(space_info)) {
1361*4882a593Smuzhiyun 			btrfs_put_block_group(block_group);
1362*4882a593Smuzhiyun 			continue;
1363*4882a593Smuzhiyun 		}
1364*4882a593Smuzhiyun 		spin_unlock(&fs_info->unused_bgs_lock);
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun 		btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun 		mutex_lock(&fs_info->delete_unused_bgs_mutex);
1369*4882a593Smuzhiyun 
1370*4882a593Smuzhiyun 		/* Don't want to race with allocators so take the groups_sem */
1371*4882a593Smuzhiyun 		down_write(&space_info->groups_sem);
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun 		/*
1374*4882a593Smuzhiyun 		 * Async discard moves the final block group discard to be prior
1375*4882a593Smuzhiyun 		 * to the unused_bgs code path.  Therefore, if it's not fully
1376*4882a593Smuzhiyun 		 * trimmed, punt it back to the async discard lists.
1377*4882a593Smuzhiyun 		 */
1378*4882a593Smuzhiyun 		if (btrfs_test_opt(fs_info, DISCARD_ASYNC) &&
1379*4882a593Smuzhiyun 		    !btrfs_is_free_space_trimmed(block_group)) {
1380*4882a593Smuzhiyun 			trace_btrfs_skip_unused_block_group(block_group);
1381*4882a593Smuzhiyun 			up_write(&space_info->groups_sem);
1382*4882a593Smuzhiyun 			/* Requeue if we failed because of async discard */
1383*4882a593Smuzhiyun 			btrfs_discard_queue_work(&fs_info->discard_ctl,
1384*4882a593Smuzhiyun 						 block_group);
1385*4882a593Smuzhiyun 			goto next;
1386*4882a593Smuzhiyun 		}
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 		spin_lock(&block_group->lock);
1389*4882a593Smuzhiyun 		if (block_group->reserved || block_group->pinned ||
1390*4882a593Smuzhiyun 		    block_group->used || block_group->ro ||
1391*4882a593Smuzhiyun 		    list_is_singular(&block_group->list)) {
1392*4882a593Smuzhiyun 			/*
1393*4882a593Smuzhiyun 			 * We want to bail if we made new allocations or have
1394*4882a593Smuzhiyun 			 * outstanding allocations in this block group.  We do
1395*4882a593Smuzhiyun 			 * the ro check in case balance is currently acting on
1396*4882a593Smuzhiyun 			 * this block group.
1397*4882a593Smuzhiyun 			 */
1398*4882a593Smuzhiyun 			trace_btrfs_skip_unused_block_group(block_group);
1399*4882a593Smuzhiyun 			spin_unlock(&block_group->lock);
1400*4882a593Smuzhiyun 			up_write(&space_info->groups_sem);
1401*4882a593Smuzhiyun 			goto next;
1402*4882a593Smuzhiyun 		}
1403*4882a593Smuzhiyun 		spin_unlock(&block_group->lock);
1404*4882a593Smuzhiyun 
1405*4882a593Smuzhiyun 		/* We don't want to force the issue, only flip if it's ok. */
1406*4882a593Smuzhiyun 		ret = inc_block_group_ro(block_group, 0);
1407*4882a593Smuzhiyun 		up_write(&space_info->groups_sem);
1408*4882a593Smuzhiyun 		if (ret < 0) {
1409*4882a593Smuzhiyun 			ret = 0;
1410*4882a593Smuzhiyun 			goto next;
1411*4882a593Smuzhiyun 		}
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun 		/*
1414*4882a593Smuzhiyun 		 * Want to do this before we do anything else so we can recover
1415*4882a593Smuzhiyun 		 * properly if we fail to join the transaction.
1416*4882a593Smuzhiyun 		 */
1417*4882a593Smuzhiyun 		trans = btrfs_start_trans_remove_block_group(fs_info,
1418*4882a593Smuzhiyun 						     block_group->start);
1419*4882a593Smuzhiyun 		if (IS_ERR(trans)) {
1420*4882a593Smuzhiyun 			btrfs_dec_block_group_ro(block_group);
1421*4882a593Smuzhiyun 			ret = PTR_ERR(trans);
1422*4882a593Smuzhiyun 			goto next;
1423*4882a593Smuzhiyun 		}
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun 		/*
1426*4882a593Smuzhiyun 		 * We could have pending pinned extents for this block group,
1427*4882a593Smuzhiyun 		 * just delete them, we don't care about them anymore.
1428*4882a593Smuzhiyun 		 */
1429*4882a593Smuzhiyun 		if (!clean_pinned_extents(trans, block_group)) {
1430*4882a593Smuzhiyun 			btrfs_dec_block_group_ro(block_group);
1431*4882a593Smuzhiyun 			goto end_trans;
1432*4882a593Smuzhiyun 		}
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 		/*
1435*4882a593Smuzhiyun 		 * At this point, the block_group is read only and should fail
1436*4882a593Smuzhiyun 		 * new allocations.  However, btrfs_finish_extent_commit() can
1437*4882a593Smuzhiyun 		 * cause this block_group to be placed back on the discard
1438*4882a593Smuzhiyun 		 * lists because now the block_group isn't fully discarded.
1439*4882a593Smuzhiyun 		 * Bail here and try again later after discarding everything.
1440*4882a593Smuzhiyun 		 */
1441*4882a593Smuzhiyun 		spin_lock(&fs_info->discard_ctl.lock);
1442*4882a593Smuzhiyun 		if (!list_empty(&block_group->discard_list)) {
1443*4882a593Smuzhiyun 			spin_unlock(&fs_info->discard_ctl.lock);
1444*4882a593Smuzhiyun 			btrfs_dec_block_group_ro(block_group);
1445*4882a593Smuzhiyun 			btrfs_discard_queue_work(&fs_info->discard_ctl,
1446*4882a593Smuzhiyun 						 block_group);
1447*4882a593Smuzhiyun 			goto end_trans;
1448*4882a593Smuzhiyun 		}
1449*4882a593Smuzhiyun 		spin_unlock(&fs_info->discard_ctl.lock);
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun 		/* Reset pinned so btrfs_put_block_group doesn't complain */
1452*4882a593Smuzhiyun 		spin_lock(&space_info->lock);
1453*4882a593Smuzhiyun 		spin_lock(&block_group->lock);
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun 		btrfs_space_info_update_bytes_pinned(fs_info, space_info,
1456*4882a593Smuzhiyun 						     -block_group->pinned);
1457*4882a593Smuzhiyun 		space_info->bytes_readonly += block_group->pinned;
1458*4882a593Smuzhiyun 		__btrfs_mod_total_bytes_pinned(space_info, -block_group->pinned);
1459*4882a593Smuzhiyun 		block_group->pinned = 0;
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun 		spin_unlock(&block_group->lock);
1462*4882a593Smuzhiyun 		spin_unlock(&space_info->lock);
1463*4882a593Smuzhiyun 
1464*4882a593Smuzhiyun 		/*
1465*4882a593Smuzhiyun 		 * The normal path here is an unused block group is passed here,
1466*4882a593Smuzhiyun 		 * then trimming is handled in the transaction commit path.
1467*4882a593Smuzhiyun 		 * Async discard interposes before this to do the trimming
1468*4882a593Smuzhiyun 		 * before coming down the unused block group path as trimming
1469*4882a593Smuzhiyun 		 * will no longer be done later in the transaction commit path.
1470*4882a593Smuzhiyun 		 */
1471*4882a593Smuzhiyun 		if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC))
1472*4882a593Smuzhiyun 			goto flip_async;
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun 		/* DISCARD can flip during remount */
1475*4882a593Smuzhiyun 		trimming = btrfs_test_opt(fs_info, DISCARD_SYNC);
1476*4882a593Smuzhiyun 
1477*4882a593Smuzhiyun 		/* Implicit trim during transaction commit. */
1478*4882a593Smuzhiyun 		if (trimming)
1479*4882a593Smuzhiyun 			btrfs_freeze_block_group(block_group);
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun 		/*
1482*4882a593Smuzhiyun 		 * Btrfs_remove_chunk will abort the transaction if things go
1483*4882a593Smuzhiyun 		 * horribly wrong.
1484*4882a593Smuzhiyun 		 */
1485*4882a593Smuzhiyun 		ret = btrfs_remove_chunk(trans, block_group->start);
1486*4882a593Smuzhiyun 
1487*4882a593Smuzhiyun 		if (ret) {
1488*4882a593Smuzhiyun 			if (trimming)
1489*4882a593Smuzhiyun 				btrfs_unfreeze_block_group(block_group);
1490*4882a593Smuzhiyun 			goto end_trans;
1491*4882a593Smuzhiyun 		}
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 		/*
1494*4882a593Smuzhiyun 		 * If we're not mounted with -odiscard, we can just forget
1495*4882a593Smuzhiyun 		 * about this block group. Otherwise we'll need to wait
1496*4882a593Smuzhiyun 		 * until transaction commit to do the actual discard.
1497*4882a593Smuzhiyun 		 */
1498*4882a593Smuzhiyun 		if (trimming) {
1499*4882a593Smuzhiyun 			spin_lock(&fs_info->unused_bgs_lock);
1500*4882a593Smuzhiyun 			/*
1501*4882a593Smuzhiyun 			 * A concurrent scrub might have added us to the list
1502*4882a593Smuzhiyun 			 * fs_info->unused_bgs, so use a list_move operation
1503*4882a593Smuzhiyun 			 * to add the block group to the deleted_bgs list.
1504*4882a593Smuzhiyun 			 */
1505*4882a593Smuzhiyun 			list_move(&block_group->bg_list,
1506*4882a593Smuzhiyun 				  &trans->transaction->deleted_bgs);
1507*4882a593Smuzhiyun 			spin_unlock(&fs_info->unused_bgs_lock);
1508*4882a593Smuzhiyun 			btrfs_get_block_group(block_group);
1509*4882a593Smuzhiyun 		}
1510*4882a593Smuzhiyun end_trans:
1511*4882a593Smuzhiyun 		btrfs_end_transaction(trans);
1512*4882a593Smuzhiyun next:
1513*4882a593Smuzhiyun 		mutex_unlock(&fs_info->delete_unused_bgs_mutex);
1514*4882a593Smuzhiyun 		btrfs_put_block_group(block_group);
1515*4882a593Smuzhiyun 		spin_lock(&fs_info->unused_bgs_lock);
1516*4882a593Smuzhiyun 	}
1517*4882a593Smuzhiyun 	spin_unlock(&fs_info->unused_bgs_lock);
1518*4882a593Smuzhiyun 	return;
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun flip_async:
1521*4882a593Smuzhiyun 	btrfs_end_transaction(trans);
1522*4882a593Smuzhiyun 	mutex_unlock(&fs_info->delete_unused_bgs_mutex);
1523*4882a593Smuzhiyun 	btrfs_put_block_group(block_group);
1524*4882a593Smuzhiyun 	btrfs_discard_punt_unused_bgs_list(fs_info);
1525*4882a593Smuzhiyun }
1526*4882a593Smuzhiyun 
btrfs_mark_bg_unused(struct btrfs_block_group * bg)1527*4882a593Smuzhiyun void btrfs_mark_bg_unused(struct btrfs_block_group *bg)
1528*4882a593Smuzhiyun {
1529*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = bg->fs_info;
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun 	spin_lock(&fs_info->unused_bgs_lock);
1532*4882a593Smuzhiyun 	if (list_empty(&bg->bg_list)) {
1533*4882a593Smuzhiyun 		btrfs_get_block_group(bg);
1534*4882a593Smuzhiyun 		trace_btrfs_add_unused_block_group(bg);
1535*4882a593Smuzhiyun 		list_add_tail(&bg->bg_list, &fs_info->unused_bgs);
1536*4882a593Smuzhiyun 	}
1537*4882a593Smuzhiyun 	spin_unlock(&fs_info->unused_bgs_lock);
1538*4882a593Smuzhiyun }
1539*4882a593Smuzhiyun 
read_bg_from_eb(struct btrfs_fs_info * fs_info,struct btrfs_key * key,struct btrfs_path * path)1540*4882a593Smuzhiyun static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
1541*4882a593Smuzhiyun 			   struct btrfs_path *path)
1542*4882a593Smuzhiyun {
1543*4882a593Smuzhiyun 	struct extent_map_tree *em_tree;
1544*4882a593Smuzhiyun 	struct extent_map *em;
1545*4882a593Smuzhiyun 	struct btrfs_block_group_item bg;
1546*4882a593Smuzhiyun 	struct extent_buffer *leaf;
1547*4882a593Smuzhiyun 	int slot;
1548*4882a593Smuzhiyun 	u64 flags;
1549*4882a593Smuzhiyun 	int ret = 0;
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun 	slot = path->slots[0];
1552*4882a593Smuzhiyun 	leaf = path->nodes[0];
1553*4882a593Smuzhiyun 
1554*4882a593Smuzhiyun 	em_tree = &fs_info->mapping_tree;
1555*4882a593Smuzhiyun 	read_lock(&em_tree->lock);
1556*4882a593Smuzhiyun 	em = lookup_extent_mapping(em_tree, key->objectid, key->offset);
1557*4882a593Smuzhiyun 	read_unlock(&em_tree->lock);
1558*4882a593Smuzhiyun 	if (!em) {
1559*4882a593Smuzhiyun 		btrfs_err(fs_info,
1560*4882a593Smuzhiyun 			  "logical %llu len %llu found bg but no related chunk",
1561*4882a593Smuzhiyun 			  key->objectid, key->offset);
1562*4882a593Smuzhiyun 		return -ENOENT;
1563*4882a593Smuzhiyun 	}
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun 	if (em->start != key->objectid || em->len != key->offset) {
1566*4882a593Smuzhiyun 		btrfs_err(fs_info,
1567*4882a593Smuzhiyun 			"block group %llu len %llu mismatch with chunk %llu len %llu",
1568*4882a593Smuzhiyun 			key->objectid, key->offset, em->start, em->len);
1569*4882a593Smuzhiyun 		ret = -EUCLEAN;
1570*4882a593Smuzhiyun 		goto out_free_em;
1571*4882a593Smuzhiyun 	}
1572*4882a593Smuzhiyun 
1573*4882a593Smuzhiyun 	read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot),
1574*4882a593Smuzhiyun 			   sizeof(bg));
1575*4882a593Smuzhiyun 	flags = btrfs_stack_block_group_flags(&bg) &
1576*4882a593Smuzhiyun 		BTRFS_BLOCK_GROUP_TYPE_MASK;
1577*4882a593Smuzhiyun 
1578*4882a593Smuzhiyun 	if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1579*4882a593Smuzhiyun 		btrfs_err(fs_info,
1580*4882a593Smuzhiyun "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
1581*4882a593Smuzhiyun 			  key->objectid, key->offset, flags,
1582*4882a593Smuzhiyun 			  (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type));
1583*4882a593Smuzhiyun 		ret = -EUCLEAN;
1584*4882a593Smuzhiyun 	}
1585*4882a593Smuzhiyun 
1586*4882a593Smuzhiyun out_free_em:
1587*4882a593Smuzhiyun 	free_extent_map(em);
1588*4882a593Smuzhiyun 	return ret;
1589*4882a593Smuzhiyun }
1590*4882a593Smuzhiyun 
find_first_block_group(struct btrfs_fs_info * fs_info,struct btrfs_path * path,struct btrfs_key * key)1591*4882a593Smuzhiyun static int find_first_block_group(struct btrfs_fs_info *fs_info,
1592*4882a593Smuzhiyun 				  struct btrfs_path *path,
1593*4882a593Smuzhiyun 				  struct btrfs_key *key)
1594*4882a593Smuzhiyun {
1595*4882a593Smuzhiyun 	struct btrfs_root *root = fs_info->extent_root;
1596*4882a593Smuzhiyun 	int ret;
1597*4882a593Smuzhiyun 	struct btrfs_key found_key;
1598*4882a593Smuzhiyun 	struct extent_buffer *leaf;
1599*4882a593Smuzhiyun 	int slot;
1600*4882a593Smuzhiyun 
1601*4882a593Smuzhiyun 	ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1602*4882a593Smuzhiyun 	if (ret < 0)
1603*4882a593Smuzhiyun 		return ret;
1604*4882a593Smuzhiyun 
1605*4882a593Smuzhiyun 	while (1) {
1606*4882a593Smuzhiyun 		slot = path->slots[0];
1607*4882a593Smuzhiyun 		leaf = path->nodes[0];
1608*4882a593Smuzhiyun 		if (slot >= btrfs_header_nritems(leaf)) {
1609*4882a593Smuzhiyun 			ret = btrfs_next_leaf(root, path);
1610*4882a593Smuzhiyun 			if (ret == 0)
1611*4882a593Smuzhiyun 				continue;
1612*4882a593Smuzhiyun 			if (ret < 0)
1613*4882a593Smuzhiyun 				goto out;
1614*4882a593Smuzhiyun 			break;
1615*4882a593Smuzhiyun 		}
1616*4882a593Smuzhiyun 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
1617*4882a593Smuzhiyun 
1618*4882a593Smuzhiyun 		if (found_key.objectid >= key->objectid &&
1619*4882a593Smuzhiyun 		    found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
1620*4882a593Smuzhiyun 			ret = read_bg_from_eb(fs_info, &found_key, path);
1621*4882a593Smuzhiyun 			break;
1622*4882a593Smuzhiyun 		}
1623*4882a593Smuzhiyun 
1624*4882a593Smuzhiyun 		path->slots[0]++;
1625*4882a593Smuzhiyun 	}
1626*4882a593Smuzhiyun out:
1627*4882a593Smuzhiyun 	return ret;
1628*4882a593Smuzhiyun }
1629*4882a593Smuzhiyun 
set_avail_alloc_bits(struct btrfs_fs_info * fs_info,u64 flags)1630*4882a593Smuzhiyun static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1631*4882a593Smuzhiyun {
1632*4882a593Smuzhiyun 	u64 extra_flags = chunk_to_extended(flags) &
1633*4882a593Smuzhiyun 				BTRFS_EXTENDED_PROFILE_MASK;
1634*4882a593Smuzhiyun 
1635*4882a593Smuzhiyun 	write_seqlock(&fs_info->profiles_lock);
1636*4882a593Smuzhiyun 	if (flags & BTRFS_BLOCK_GROUP_DATA)
1637*4882a593Smuzhiyun 		fs_info->avail_data_alloc_bits |= extra_flags;
1638*4882a593Smuzhiyun 	if (flags & BTRFS_BLOCK_GROUP_METADATA)
1639*4882a593Smuzhiyun 		fs_info->avail_metadata_alloc_bits |= extra_flags;
1640*4882a593Smuzhiyun 	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1641*4882a593Smuzhiyun 		fs_info->avail_system_alloc_bits |= extra_flags;
1642*4882a593Smuzhiyun 	write_sequnlock(&fs_info->profiles_lock);
1643*4882a593Smuzhiyun }
1644*4882a593Smuzhiyun 
1645*4882a593Smuzhiyun /**
1646*4882a593Smuzhiyun  * btrfs_rmap_block - Map a physical disk address to a list of logical addresses
1647*4882a593Smuzhiyun  * @chunk_start:   logical address of block group
1648*4882a593Smuzhiyun  * @physical:	   physical address to map to logical addresses
1649*4882a593Smuzhiyun  * @logical:	   return array of logical addresses which map to @physical
1650*4882a593Smuzhiyun  * @naddrs:	   length of @logical
1651*4882a593Smuzhiyun  * @stripe_len:    size of IO stripe for the given block group
1652*4882a593Smuzhiyun  *
1653*4882a593Smuzhiyun  * Maps a particular @physical disk address to a list of @logical addresses.
1654*4882a593Smuzhiyun  * Used primarily to exclude those portions of a block group that contain super
1655*4882a593Smuzhiyun  * block copies.
1656*4882a593Smuzhiyun  */
1657*4882a593Smuzhiyun EXPORT_FOR_TESTS
btrfs_rmap_block(struct btrfs_fs_info * fs_info,u64 chunk_start,u64 physical,u64 ** logical,int * naddrs,int * stripe_len)1658*4882a593Smuzhiyun int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
1659*4882a593Smuzhiyun 		     u64 physical, u64 **logical, int *naddrs, int *stripe_len)
1660*4882a593Smuzhiyun {
1661*4882a593Smuzhiyun 	struct extent_map *em;
1662*4882a593Smuzhiyun 	struct map_lookup *map;
1663*4882a593Smuzhiyun 	u64 *buf;
1664*4882a593Smuzhiyun 	u64 bytenr;
1665*4882a593Smuzhiyun 	u64 data_stripe_length;
1666*4882a593Smuzhiyun 	u64 io_stripe_size;
1667*4882a593Smuzhiyun 	int i, nr = 0;
1668*4882a593Smuzhiyun 	int ret = 0;
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun 	em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
1671*4882a593Smuzhiyun 	if (IS_ERR(em))
1672*4882a593Smuzhiyun 		return -EIO;
1673*4882a593Smuzhiyun 
1674*4882a593Smuzhiyun 	map = em->map_lookup;
1675*4882a593Smuzhiyun 	data_stripe_length = em->orig_block_len;
1676*4882a593Smuzhiyun 	io_stripe_size = map->stripe_len;
1677*4882a593Smuzhiyun 
1678*4882a593Smuzhiyun 	/* For RAID5/6 adjust to a full IO stripe length */
1679*4882a593Smuzhiyun 	if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
1680*4882a593Smuzhiyun 		io_stripe_size = map->stripe_len * nr_data_stripes(map);
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun 	buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
1683*4882a593Smuzhiyun 	if (!buf) {
1684*4882a593Smuzhiyun 		ret = -ENOMEM;
1685*4882a593Smuzhiyun 		goto out;
1686*4882a593Smuzhiyun 	}
1687*4882a593Smuzhiyun 
1688*4882a593Smuzhiyun 	for (i = 0; i < map->num_stripes; i++) {
1689*4882a593Smuzhiyun 		bool already_inserted = false;
1690*4882a593Smuzhiyun 		u64 stripe_nr;
1691*4882a593Smuzhiyun 		int j;
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun 		if (!in_range(physical, map->stripes[i].physical,
1694*4882a593Smuzhiyun 			      data_stripe_length))
1695*4882a593Smuzhiyun 			continue;
1696*4882a593Smuzhiyun 
1697*4882a593Smuzhiyun 		stripe_nr = physical - map->stripes[i].physical;
1698*4882a593Smuzhiyun 		stripe_nr = div64_u64(stripe_nr, map->stripe_len);
1699*4882a593Smuzhiyun 
1700*4882a593Smuzhiyun 		if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1701*4882a593Smuzhiyun 			stripe_nr = stripe_nr * map->num_stripes + i;
1702*4882a593Smuzhiyun 			stripe_nr = div_u64(stripe_nr, map->sub_stripes);
1703*4882a593Smuzhiyun 		} else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1704*4882a593Smuzhiyun 			stripe_nr = stripe_nr * map->num_stripes + i;
1705*4882a593Smuzhiyun 		}
1706*4882a593Smuzhiyun 		/*
1707*4882a593Smuzhiyun 		 * The remaining case would be for RAID56, multiply by
1708*4882a593Smuzhiyun 		 * nr_data_stripes().  Alternatively, just use rmap_len below
1709*4882a593Smuzhiyun 		 * instead of map->stripe_len
1710*4882a593Smuzhiyun 		 */
1711*4882a593Smuzhiyun 
1712*4882a593Smuzhiyun 		bytenr = chunk_start + stripe_nr * io_stripe_size;
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun 		/* Ensure we don't add duplicate addresses */
1715*4882a593Smuzhiyun 		for (j = 0; j < nr; j++) {
1716*4882a593Smuzhiyun 			if (buf[j] == bytenr) {
1717*4882a593Smuzhiyun 				already_inserted = true;
1718*4882a593Smuzhiyun 				break;
1719*4882a593Smuzhiyun 			}
1720*4882a593Smuzhiyun 		}
1721*4882a593Smuzhiyun 
1722*4882a593Smuzhiyun 		if (!already_inserted)
1723*4882a593Smuzhiyun 			buf[nr++] = bytenr;
1724*4882a593Smuzhiyun 	}
1725*4882a593Smuzhiyun 
1726*4882a593Smuzhiyun 	*logical = buf;
1727*4882a593Smuzhiyun 	*naddrs = nr;
1728*4882a593Smuzhiyun 	*stripe_len = io_stripe_size;
1729*4882a593Smuzhiyun out:
1730*4882a593Smuzhiyun 	free_extent_map(em);
1731*4882a593Smuzhiyun 	return ret;
1732*4882a593Smuzhiyun }
1733*4882a593Smuzhiyun 
exclude_super_stripes(struct btrfs_block_group * cache)1734*4882a593Smuzhiyun static int exclude_super_stripes(struct btrfs_block_group *cache)
1735*4882a593Smuzhiyun {
1736*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = cache->fs_info;
1737*4882a593Smuzhiyun 	u64 bytenr;
1738*4882a593Smuzhiyun 	u64 *logical;
1739*4882a593Smuzhiyun 	int stripe_len;
1740*4882a593Smuzhiyun 	int i, nr, ret;
1741*4882a593Smuzhiyun 
1742*4882a593Smuzhiyun 	if (cache->start < BTRFS_SUPER_INFO_OFFSET) {
1743*4882a593Smuzhiyun 		stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start;
1744*4882a593Smuzhiyun 		cache->bytes_super += stripe_len;
1745*4882a593Smuzhiyun 		ret = btrfs_add_excluded_extent(fs_info, cache->start,
1746*4882a593Smuzhiyun 						stripe_len);
1747*4882a593Smuzhiyun 		if (ret)
1748*4882a593Smuzhiyun 			return ret;
1749*4882a593Smuzhiyun 	}
1750*4882a593Smuzhiyun 
1751*4882a593Smuzhiyun 	for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1752*4882a593Smuzhiyun 		bytenr = btrfs_sb_offset(i);
1753*4882a593Smuzhiyun 		ret = btrfs_rmap_block(fs_info, cache->start,
1754*4882a593Smuzhiyun 				       bytenr, &logical, &nr, &stripe_len);
1755*4882a593Smuzhiyun 		if (ret)
1756*4882a593Smuzhiyun 			return ret;
1757*4882a593Smuzhiyun 
1758*4882a593Smuzhiyun 		while (nr--) {
1759*4882a593Smuzhiyun 			u64 len = min_t(u64, stripe_len,
1760*4882a593Smuzhiyun 				cache->start + cache->length - logical[nr]);
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun 			cache->bytes_super += len;
1763*4882a593Smuzhiyun 			ret = btrfs_add_excluded_extent(fs_info, logical[nr],
1764*4882a593Smuzhiyun 							len);
1765*4882a593Smuzhiyun 			if (ret) {
1766*4882a593Smuzhiyun 				kfree(logical);
1767*4882a593Smuzhiyun 				return ret;
1768*4882a593Smuzhiyun 			}
1769*4882a593Smuzhiyun 		}
1770*4882a593Smuzhiyun 
1771*4882a593Smuzhiyun 		kfree(logical);
1772*4882a593Smuzhiyun 	}
1773*4882a593Smuzhiyun 	return 0;
1774*4882a593Smuzhiyun }
1775*4882a593Smuzhiyun 
link_block_group(struct btrfs_block_group * cache)1776*4882a593Smuzhiyun static void link_block_group(struct btrfs_block_group *cache)
1777*4882a593Smuzhiyun {
1778*4882a593Smuzhiyun 	struct btrfs_space_info *space_info = cache->space_info;
1779*4882a593Smuzhiyun 	int index = btrfs_bg_flags_to_raid_index(cache->flags);
1780*4882a593Smuzhiyun 
1781*4882a593Smuzhiyun 	down_write(&space_info->groups_sem);
1782*4882a593Smuzhiyun 	list_add_tail(&cache->list, &space_info->block_groups[index]);
1783*4882a593Smuzhiyun 	up_write(&space_info->groups_sem);
1784*4882a593Smuzhiyun }
1785*4882a593Smuzhiyun 
btrfs_create_block_group_cache(struct btrfs_fs_info * fs_info,u64 start)1786*4882a593Smuzhiyun static struct btrfs_block_group *btrfs_create_block_group_cache(
1787*4882a593Smuzhiyun 		struct btrfs_fs_info *fs_info, u64 start)
1788*4882a593Smuzhiyun {
1789*4882a593Smuzhiyun 	struct btrfs_block_group *cache;
1790*4882a593Smuzhiyun 
1791*4882a593Smuzhiyun 	cache = kzalloc(sizeof(*cache), GFP_NOFS);
1792*4882a593Smuzhiyun 	if (!cache)
1793*4882a593Smuzhiyun 		return NULL;
1794*4882a593Smuzhiyun 
1795*4882a593Smuzhiyun 	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
1796*4882a593Smuzhiyun 					GFP_NOFS);
1797*4882a593Smuzhiyun 	if (!cache->free_space_ctl) {
1798*4882a593Smuzhiyun 		kfree(cache);
1799*4882a593Smuzhiyun 		return NULL;
1800*4882a593Smuzhiyun 	}
1801*4882a593Smuzhiyun 
1802*4882a593Smuzhiyun 	cache->start = start;
1803*4882a593Smuzhiyun 
1804*4882a593Smuzhiyun 	cache->fs_info = fs_info;
1805*4882a593Smuzhiyun 	cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
1806*4882a593Smuzhiyun 
1807*4882a593Smuzhiyun 	cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED;
1808*4882a593Smuzhiyun 
1809*4882a593Smuzhiyun 	refcount_set(&cache->refs, 1);
1810*4882a593Smuzhiyun 	spin_lock_init(&cache->lock);
1811*4882a593Smuzhiyun 	init_rwsem(&cache->data_rwsem);
1812*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cache->list);
1813*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cache->cluster_list);
1814*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cache->bg_list);
1815*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cache->ro_list);
1816*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cache->discard_list);
1817*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cache->dirty_list);
1818*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cache->io_list);
1819*4882a593Smuzhiyun 	btrfs_init_free_space_ctl(cache);
1820*4882a593Smuzhiyun 	atomic_set(&cache->frozen, 0);
1821*4882a593Smuzhiyun 	mutex_init(&cache->free_space_lock);
1822*4882a593Smuzhiyun 	btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun 	return cache;
1825*4882a593Smuzhiyun }
1826*4882a593Smuzhiyun 
1827*4882a593Smuzhiyun /*
1828*4882a593Smuzhiyun  * Iterate all chunks and verify that each of them has the corresponding block
1829*4882a593Smuzhiyun  * group
1830*4882a593Smuzhiyun  */
check_chunk_block_group_mappings(struct btrfs_fs_info * fs_info)1831*4882a593Smuzhiyun static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
1832*4882a593Smuzhiyun {
1833*4882a593Smuzhiyun 	struct extent_map_tree *map_tree = &fs_info->mapping_tree;
1834*4882a593Smuzhiyun 	struct extent_map *em;
1835*4882a593Smuzhiyun 	struct btrfs_block_group *bg;
1836*4882a593Smuzhiyun 	u64 start = 0;
1837*4882a593Smuzhiyun 	int ret = 0;
1838*4882a593Smuzhiyun 
1839*4882a593Smuzhiyun 	while (1) {
1840*4882a593Smuzhiyun 		read_lock(&map_tree->lock);
1841*4882a593Smuzhiyun 		/*
1842*4882a593Smuzhiyun 		 * lookup_extent_mapping will return the first extent map
1843*4882a593Smuzhiyun 		 * intersecting the range, so setting @len to 1 is enough to
1844*4882a593Smuzhiyun 		 * get the first chunk.
1845*4882a593Smuzhiyun 		 */
1846*4882a593Smuzhiyun 		em = lookup_extent_mapping(map_tree, start, 1);
1847*4882a593Smuzhiyun 		read_unlock(&map_tree->lock);
1848*4882a593Smuzhiyun 		if (!em)
1849*4882a593Smuzhiyun 			break;
1850*4882a593Smuzhiyun 
1851*4882a593Smuzhiyun 		bg = btrfs_lookup_block_group(fs_info, em->start);
1852*4882a593Smuzhiyun 		if (!bg) {
1853*4882a593Smuzhiyun 			btrfs_err(fs_info,
1854*4882a593Smuzhiyun 	"chunk start=%llu len=%llu doesn't have corresponding block group",
1855*4882a593Smuzhiyun 				     em->start, em->len);
1856*4882a593Smuzhiyun 			ret = -EUCLEAN;
1857*4882a593Smuzhiyun 			free_extent_map(em);
1858*4882a593Smuzhiyun 			break;
1859*4882a593Smuzhiyun 		}
1860*4882a593Smuzhiyun 		if (bg->start != em->start || bg->length != em->len ||
1861*4882a593Smuzhiyun 		    (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
1862*4882a593Smuzhiyun 		    (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1863*4882a593Smuzhiyun 			btrfs_err(fs_info,
1864*4882a593Smuzhiyun "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",
1865*4882a593Smuzhiyun 				em->start, em->len,
1866*4882a593Smuzhiyun 				em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
1867*4882a593Smuzhiyun 				bg->start, bg->length,
1868*4882a593Smuzhiyun 				bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
1869*4882a593Smuzhiyun 			ret = -EUCLEAN;
1870*4882a593Smuzhiyun 			free_extent_map(em);
1871*4882a593Smuzhiyun 			btrfs_put_block_group(bg);
1872*4882a593Smuzhiyun 			break;
1873*4882a593Smuzhiyun 		}
1874*4882a593Smuzhiyun 		start = em->start + em->len;
1875*4882a593Smuzhiyun 		free_extent_map(em);
1876*4882a593Smuzhiyun 		btrfs_put_block_group(bg);
1877*4882a593Smuzhiyun 	}
1878*4882a593Smuzhiyun 	return ret;
1879*4882a593Smuzhiyun }
1880*4882a593Smuzhiyun 
read_block_group_item(struct btrfs_block_group * cache,struct btrfs_path * path,const struct btrfs_key * key)1881*4882a593Smuzhiyun static void read_block_group_item(struct btrfs_block_group *cache,
1882*4882a593Smuzhiyun 				 struct btrfs_path *path,
1883*4882a593Smuzhiyun 				 const struct btrfs_key *key)
1884*4882a593Smuzhiyun {
1885*4882a593Smuzhiyun 	struct extent_buffer *leaf = path->nodes[0];
1886*4882a593Smuzhiyun 	struct btrfs_block_group_item bgi;
1887*4882a593Smuzhiyun 	int slot = path->slots[0];
1888*4882a593Smuzhiyun 
1889*4882a593Smuzhiyun 	cache->length = key->offset;
1890*4882a593Smuzhiyun 
1891*4882a593Smuzhiyun 	read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
1892*4882a593Smuzhiyun 			   sizeof(bgi));
1893*4882a593Smuzhiyun 	cache->used = btrfs_stack_block_group_used(&bgi);
1894*4882a593Smuzhiyun 	cache->flags = btrfs_stack_block_group_flags(&bgi);
1895*4882a593Smuzhiyun }
1896*4882a593Smuzhiyun 
read_one_block_group(struct btrfs_fs_info * info,struct btrfs_path * path,const struct btrfs_key * key,int need_clear)1897*4882a593Smuzhiyun static int read_one_block_group(struct btrfs_fs_info *info,
1898*4882a593Smuzhiyun 				struct btrfs_path *path,
1899*4882a593Smuzhiyun 				const struct btrfs_key *key,
1900*4882a593Smuzhiyun 				int need_clear)
1901*4882a593Smuzhiyun {
1902*4882a593Smuzhiyun 	struct btrfs_block_group *cache;
1903*4882a593Smuzhiyun 	struct btrfs_space_info *space_info;
1904*4882a593Smuzhiyun 	const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS);
1905*4882a593Smuzhiyun 	int ret;
1906*4882a593Smuzhiyun 
1907*4882a593Smuzhiyun 	ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY);
1908*4882a593Smuzhiyun 
1909*4882a593Smuzhiyun 	cache = btrfs_create_block_group_cache(info, key->objectid);
1910*4882a593Smuzhiyun 	if (!cache)
1911*4882a593Smuzhiyun 		return -ENOMEM;
1912*4882a593Smuzhiyun 
1913*4882a593Smuzhiyun 	read_block_group_item(cache, path, key);
1914*4882a593Smuzhiyun 
1915*4882a593Smuzhiyun 	set_free_space_tree_thresholds(cache);
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun 	if (need_clear) {
1918*4882a593Smuzhiyun 		/*
1919*4882a593Smuzhiyun 		 * When we mount with old space cache, we need to
1920*4882a593Smuzhiyun 		 * set BTRFS_DC_CLEAR and set dirty flag.
1921*4882a593Smuzhiyun 		 *
1922*4882a593Smuzhiyun 		 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
1923*4882a593Smuzhiyun 		 *    truncate the old free space cache inode and
1924*4882a593Smuzhiyun 		 *    setup a new one.
1925*4882a593Smuzhiyun 		 * b) Setting 'dirty flag' makes sure that we flush
1926*4882a593Smuzhiyun 		 *    the new space cache info onto disk.
1927*4882a593Smuzhiyun 		 */
1928*4882a593Smuzhiyun 		if (btrfs_test_opt(info, SPACE_CACHE))
1929*4882a593Smuzhiyun 			cache->disk_cache_state = BTRFS_DC_CLEAR;
1930*4882a593Smuzhiyun 	}
1931*4882a593Smuzhiyun 	if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
1932*4882a593Smuzhiyun 	    (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
1933*4882a593Smuzhiyun 			btrfs_err(info,
1934*4882a593Smuzhiyun "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
1935*4882a593Smuzhiyun 				  cache->start);
1936*4882a593Smuzhiyun 			ret = -EINVAL;
1937*4882a593Smuzhiyun 			goto error;
1938*4882a593Smuzhiyun 	}
1939*4882a593Smuzhiyun 
1940*4882a593Smuzhiyun 	/*
1941*4882a593Smuzhiyun 	 * We need to exclude the super stripes now so that the space info has
1942*4882a593Smuzhiyun 	 * super bytes accounted for, otherwise we'll think we have more space
1943*4882a593Smuzhiyun 	 * than we actually do.
1944*4882a593Smuzhiyun 	 */
1945*4882a593Smuzhiyun 	ret = exclude_super_stripes(cache);
1946*4882a593Smuzhiyun 	if (ret) {
1947*4882a593Smuzhiyun 		/* We may have excluded something, so call this just in case. */
1948*4882a593Smuzhiyun 		btrfs_free_excluded_extents(cache);
1949*4882a593Smuzhiyun 		goto error;
1950*4882a593Smuzhiyun 	}
1951*4882a593Smuzhiyun 
1952*4882a593Smuzhiyun 	/*
1953*4882a593Smuzhiyun 	 * Check for two cases, either we are full, and therefore don't need
1954*4882a593Smuzhiyun 	 * to bother with the caching work since we won't find any space, or we
1955*4882a593Smuzhiyun 	 * are empty, and we can just add all the space in and be done with it.
1956*4882a593Smuzhiyun 	 * This saves us _a_lot_ of time, particularly in the full case.
1957*4882a593Smuzhiyun 	 */
1958*4882a593Smuzhiyun 	if (cache->length == cache->used) {
1959*4882a593Smuzhiyun 		cache->last_byte_to_unpin = (u64)-1;
1960*4882a593Smuzhiyun 		cache->cached = BTRFS_CACHE_FINISHED;
1961*4882a593Smuzhiyun 		btrfs_free_excluded_extents(cache);
1962*4882a593Smuzhiyun 	} else if (cache->used == 0) {
1963*4882a593Smuzhiyun 		cache->last_byte_to_unpin = (u64)-1;
1964*4882a593Smuzhiyun 		cache->cached = BTRFS_CACHE_FINISHED;
1965*4882a593Smuzhiyun 		add_new_free_space(cache, cache->start,
1966*4882a593Smuzhiyun 				   cache->start + cache->length);
1967*4882a593Smuzhiyun 		btrfs_free_excluded_extents(cache);
1968*4882a593Smuzhiyun 	}
1969*4882a593Smuzhiyun 
1970*4882a593Smuzhiyun 	ret = btrfs_add_block_group_cache(info, cache);
1971*4882a593Smuzhiyun 	if (ret) {
1972*4882a593Smuzhiyun 		btrfs_remove_free_space_cache(cache);
1973*4882a593Smuzhiyun 		goto error;
1974*4882a593Smuzhiyun 	}
1975*4882a593Smuzhiyun 	trace_btrfs_add_block_group(info, cache, 0);
1976*4882a593Smuzhiyun 	btrfs_update_space_info(info, cache->flags, cache->length,
1977*4882a593Smuzhiyun 				cache->used, cache->bytes_super, &space_info);
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun 	cache->space_info = space_info;
1980*4882a593Smuzhiyun 
1981*4882a593Smuzhiyun 	link_block_group(cache);
1982*4882a593Smuzhiyun 
1983*4882a593Smuzhiyun 	set_avail_alloc_bits(info, cache->flags);
1984*4882a593Smuzhiyun 	if (btrfs_chunk_readonly(info, cache->start)) {
1985*4882a593Smuzhiyun 		inc_block_group_ro(cache, 1);
1986*4882a593Smuzhiyun 	} else if (cache->used == 0) {
1987*4882a593Smuzhiyun 		ASSERT(list_empty(&cache->bg_list));
1988*4882a593Smuzhiyun 		if (btrfs_test_opt(info, DISCARD_ASYNC))
1989*4882a593Smuzhiyun 			btrfs_discard_queue_work(&info->discard_ctl, cache);
1990*4882a593Smuzhiyun 		else
1991*4882a593Smuzhiyun 			btrfs_mark_bg_unused(cache);
1992*4882a593Smuzhiyun 	}
1993*4882a593Smuzhiyun 	return 0;
1994*4882a593Smuzhiyun error:
1995*4882a593Smuzhiyun 	btrfs_put_block_group(cache);
1996*4882a593Smuzhiyun 	return ret;
1997*4882a593Smuzhiyun }
1998*4882a593Smuzhiyun 
btrfs_read_block_groups(struct btrfs_fs_info * info)1999*4882a593Smuzhiyun int btrfs_read_block_groups(struct btrfs_fs_info *info)
2000*4882a593Smuzhiyun {
2001*4882a593Smuzhiyun 	struct btrfs_path *path;
2002*4882a593Smuzhiyun 	int ret;
2003*4882a593Smuzhiyun 	struct btrfs_block_group *cache;
2004*4882a593Smuzhiyun 	struct btrfs_space_info *space_info;
2005*4882a593Smuzhiyun 	struct btrfs_key key;
2006*4882a593Smuzhiyun 	int need_clear = 0;
2007*4882a593Smuzhiyun 	u64 cache_gen;
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun 	key.objectid = 0;
2010*4882a593Smuzhiyun 	key.offset = 0;
2011*4882a593Smuzhiyun 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2012*4882a593Smuzhiyun 	path = btrfs_alloc_path();
2013*4882a593Smuzhiyun 	if (!path)
2014*4882a593Smuzhiyun 		return -ENOMEM;
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 	cache_gen = btrfs_super_cache_generation(info->super_copy);
2017*4882a593Smuzhiyun 	if (btrfs_test_opt(info, SPACE_CACHE) &&
2018*4882a593Smuzhiyun 	    btrfs_super_generation(info->super_copy) != cache_gen)
2019*4882a593Smuzhiyun 		need_clear = 1;
2020*4882a593Smuzhiyun 	if (btrfs_test_opt(info, CLEAR_CACHE))
2021*4882a593Smuzhiyun 		need_clear = 1;
2022*4882a593Smuzhiyun 
2023*4882a593Smuzhiyun 	while (1) {
2024*4882a593Smuzhiyun 		ret = find_first_block_group(info, path, &key);
2025*4882a593Smuzhiyun 		if (ret > 0)
2026*4882a593Smuzhiyun 			break;
2027*4882a593Smuzhiyun 		if (ret != 0)
2028*4882a593Smuzhiyun 			goto error;
2029*4882a593Smuzhiyun 
2030*4882a593Smuzhiyun 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2031*4882a593Smuzhiyun 		ret = read_one_block_group(info, path, &key, need_clear);
2032*4882a593Smuzhiyun 		if (ret < 0)
2033*4882a593Smuzhiyun 			goto error;
2034*4882a593Smuzhiyun 		key.objectid += key.offset;
2035*4882a593Smuzhiyun 		key.offset = 0;
2036*4882a593Smuzhiyun 		btrfs_release_path(path);
2037*4882a593Smuzhiyun 	}
2038*4882a593Smuzhiyun 	btrfs_release_path(path);
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun 	list_for_each_entry(space_info, &info->space_info, list) {
2041*4882a593Smuzhiyun 		int i;
2042*4882a593Smuzhiyun 
2043*4882a593Smuzhiyun 		for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2044*4882a593Smuzhiyun 			if (list_empty(&space_info->block_groups[i]))
2045*4882a593Smuzhiyun 				continue;
2046*4882a593Smuzhiyun 			cache = list_first_entry(&space_info->block_groups[i],
2047*4882a593Smuzhiyun 						 struct btrfs_block_group,
2048*4882a593Smuzhiyun 						 list);
2049*4882a593Smuzhiyun 			btrfs_sysfs_add_block_group_type(cache);
2050*4882a593Smuzhiyun 		}
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun 		if (!(btrfs_get_alloc_profile(info, space_info->flags) &
2053*4882a593Smuzhiyun 		      (BTRFS_BLOCK_GROUP_RAID10 |
2054*4882a593Smuzhiyun 		       BTRFS_BLOCK_GROUP_RAID1_MASK |
2055*4882a593Smuzhiyun 		       BTRFS_BLOCK_GROUP_RAID56_MASK |
2056*4882a593Smuzhiyun 		       BTRFS_BLOCK_GROUP_DUP)))
2057*4882a593Smuzhiyun 			continue;
2058*4882a593Smuzhiyun 		/*
2059*4882a593Smuzhiyun 		 * Avoid allocating from un-mirrored block group if there are
2060*4882a593Smuzhiyun 		 * mirrored block groups.
2061*4882a593Smuzhiyun 		 */
2062*4882a593Smuzhiyun 		list_for_each_entry(cache,
2063*4882a593Smuzhiyun 				&space_info->block_groups[BTRFS_RAID_RAID0],
2064*4882a593Smuzhiyun 				list)
2065*4882a593Smuzhiyun 			inc_block_group_ro(cache, 1);
2066*4882a593Smuzhiyun 		list_for_each_entry(cache,
2067*4882a593Smuzhiyun 				&space_info->block_groups[BTRFS_RAID_SINGLE],
2068*4882a593Smuzhiyun 				list)
2069*4882a593Smuzhiyun 			inc_block_group_ro(cache, 1);
2070*4882a593Smuzhiyun 	}
2071*4882a593Smuzhiyun 
2072*4882a593Smuzhiyun 	btrfs_init_global_block_rsv(info);
2073*4882a593Smuzhiyun 	ret = check_chunk_block_group_mappings(info);
2074*4882a593Smuzhiyun error:
2075*4882a593Smuzhiyun 	btrfs_free_path(path);
2076*4882a593Smuzhiyun 	return ret;
2077*4882a593Smuzhiyun }
2078*4882a593Smuzhiyun 
insert_block_group_item(struct btrfs_trans_handle * trans,struct btrfs_block_group * block_group)2079*4882a593Smuzhiyun static int insert_block_group_item(struct btrfs_trans_handle *trans,
2080*4882a593Smuzhiyun 				   struct btrfs_block_group *block_group)
2081*4882a593Smuzhiyun {
2082*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = trans->fs_info;
2083*4882a593Smuzhiyun 	struct btrfs_block_group_item bgi;
2084*4882a593Smuzhiyun 	struct btrfs_root *root;
2085*4882a593Smuzhiyun 	struct btrfs_key key;
2086*4882a593Smuzhiyun 
2087*4882a593Smuzhiyun 	spin_lock(&block_group->lock);
2088*4882a593Smuzhiyun 	btrfs_set_stack_block_group_used(&bgi, block_group->used);
2089*4882a593Smuzhiyun 	btrfs_set_stack_block_group_chunk_objectid(&bgi,
2090*4882a593Smuzhiyun 				BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2091*4882a593Smuzhiyun 	btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
2092*4882a593Smuzhiyun 	key.objectid = block_group->start;
2093*4882a593Smuzhiyun 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2094*4882a593Smuzhiyun 	key.offset = block_group->length;
2095*4882a593Smuzhiyun 	spin_unlock(&block_group->lock);
2096*4882a593Smuzhiyun 
2097*4882a593Smuzhiyun 	root = fs_info->extent_root;
2098*4882a593Smuzhiyun 	return btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi));
2099*4882a593Smuzhiyun }
2100*4882a593Smuzhiyun 
btrfs_create_pending_block_groups(struct btrfs_trans_handle * trans)2101*4882a593Smuzhiyun void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
2102*4882a593Smuzhiyun {
2103*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = trans->fs_info;
2104*4882a593Smuzhiyun 	struct btrfs_block_group *block_group;
2105*4882a593Smuzhiyun 	int ret = 0;
2106*4882a593Smuzhiyun 
2107*4882a593Smuzhiyun 	if (!trans->can_flush_pending_bgs)
2108*4882a593Smuzhiyun 		return;
2109*4882a593Smuzhiyun 
2110*4882a593Smuzhiyun 	while (!list_empty(&trans->new_bgs)) {
2111*4882a593Smuzhiyun 		int index;
2112*4882a593Smuzhiyun 
2113*4882a593Smuzhiyun 		block_group = list_first_entry(&trans->new_bgs,
2114*4882a593Smuzhiyun 					       struct btrfs_block_group,
2115*4882a593Smuzhiyun 					       bg_list);
2116*4882a593Smuzhiyun 		if (ret)
2117*4882a593Smuzhiyun 			goto next;
2118*4882a593Smuzhiyun 
2119*4882a593Smuzhiyun 		index = btrfs_bg_flags_to_raid_index(block_group->flags);
2120*4882a593Smuzhiyun 
2121*4882a593Smuzhiyun 		ret = insert_block_group_item(trans, block_group);
2122*4882a593Smuzhiyun 		if (ret)
2123*4882a593Smuzhiyun 			btrfs_abort_transaction(trans, ret);
2124*4882a593Smuzhiyun 		ret = btrfs_finish_chunk_alloc(trans, block_group->start,
2125*4882a593Smuzhiyun 					block_group->length);
2126*4882a593Smuzhiyun 		if (ret)
2127*4882a593Smuzhiyun 			btrfs_abort_transaction(trans, ret);
2128*4882a593Smuzhiyun 		add_block_group_free_space(trans, block_group);
2129*4882a593Smuzhiyun 
2130*4882a593Smuzhiyun 		/*
2131*4882a593Smuzhiyun 		 * If we restriped during balance, we may have added a new raid
2132*4882a593Smuzhiyun 		 * type, so now add the sysfs entries when it is safe to do so.
2133*4882a593Smuzhiyun 		 * We don't have to worry about locking here as it's handled in
2134*4882a593Smuzhiyun 		 * btrfs_sysfs_add_block_group_type.
2135*4882a593Smuzhiyun 		 */
2136*4882a593Smuzhiyun 		if (block_group->space_info->block_group_kobjs[index] == NULL)
2137*4882a593Smuzhiyun 			btrfs_sysfs_add_block_group_type(block_group);
2138*4882a593Smuzhiyun 
2139*4882a593Smuzhiyun 		/* Already aborted the transaction if it failed. */
2140*4882a593Smuzhiyun next:
2141*4882a593Smuzhiyun 		btrfs_delayed_refs_rsv_release(fs_info, 1);
2142*4882a593Smuzhiyun 		list_del_init(&block_group->bg_list);
2143*4882a593Smuzhiyun 	}
2144*4882a593Smuzhiyun 	btrfs_trans_release_chunk_metadata(trans);
2145*4882a593Smuzhiyun }
2146*4882a593Smuzhiyun 
btrfs_make_block_group(struct btrfs_trans_handle * trans,u64 bytes_used,u64 type,u64 chunk_offset,u64 size)2147*4882a593Smuzhiyun int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used,
2148*4882a593Smuzhiyun 			   u64 type, u64 chunk_offset, u64 size)
2149*4882a593Smuzhiyun {
2150*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = trans->fs_info;
2151*4882a593Smuzhiyun 	struct btrfs_block_group *cache;
2152*4882a593Smuzhiyun 	int ret;
2153*4882a593Smuzhiyun 
2154*4882a593Smuzhiyun 	btrfs_set_log_full_commit(trans);
2155*4882a593Smuzhiyun 
2156*4882a593Smuzhiyun 	cache = btrfs_create_block_group_cache(fs_info, chunk_offset);
2157*4882a593Smuzhiyun 	if (!cache)
2158*4882a593Smuzhiyun 		return -ENOMEM;
2159*4882a593Smuzhiyun 
2160*4882a593Smuzhiyun 	cache->length = size;
2161*4882a593Smuzhiyun 	set_free_space_tree_thresholds(cache);
2162*4882a593Smuzhiyun 	cache->used = bytes_used;
2163*4882a593Smuzhiyun 	cache->flags = type;
2164*4882a593Smuzhiyun 	cache->last_byte_to_unpin = (u64)-1;
2165*4882a593Smuzhiyun 	cache->cached = BTRFS_CACHE_FINISHED;
2166*4882a593Smuzhiyun 	cache->needs_free_space = 1;
2167*4882a593Smuzhiyun 	ret = exclude_super_stripes(cache);
2168*4882a593Smuzhiyun 	if (ret) {
2169*4882a593Smuzhiyun 		/* We may have excluded something, so call this just in case */
2170*4882a593Smuzhiyun 		btrfs_free_excluded_extents(cache);
2171*4882a593Smuzhiyun 		btrfs_put_block_group(cache);
2172*4882a593Smuzhiyun 		return ret;
2173*4882a593Smuzhiyun 	}
2174*4882a593Smuzhiyun 
2175*4882a593Smuzhiyun 	add_new_free_space(cache, chunk_offset, chunk_offset + size);
2176*4882a593Smuzhiyun 
2177*4882a593Smuzhiyun 	btrfs_free_excluded_extents(cache);
2178*4882a593Smuzhiyun 
2179*4882a593Smuzhiyun #ifdef CONFIG_BTRFS_DEBUG
2180*4882a593Smuzhiyun 	if (btrfs_should_fragment_free_space(cache)) {
2181*4882a593Smuzhiyun 		u64 new_bytes_used = size - bytes_used;
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun 		bytes_used += new_bytes_used >> 1;
2184*4882a593Smuzhiyun 		fragment_free_space(cache);
2185*4882a593Smuzhiyun 	}
2186*4882a593Smuzhiyun #endif
2187*4882a593Smuzhiyun 	/*
2188*4882a593Smuzhiyun 	 * Ensure the corresponding space_info object is created and
2189*4882a593Smuzhiyun 	 * assigned to our block group. We want our bg to be added to the rbtree
2190*4882a593Smuzhiyun 	 * with its ->space_info set.
2191*4882a593Smuzhiyun 	 */
2192*4882a593Smuzhiyun 	cache->space_info = btrfs_find_space_info(fs_info, cache->flags);
2193*4882a593Smuzhiyun 	ASSERT(cache->space_info);
2194*4882a593Smuzhiyun 
2195*4882a593Smuzhiyun 	ret = btrfs_add_block_group_cache(fs_info, cache);
2196*4882a593Smuzhiyun 	if (ret) {
2197*4882a593Smuzhiyun 		btrfs_remove_free_space_cache(cache);
2198*4882a593Smuzhiyun 		btrfs_put_block_group(cache);
2199*4882a593Smuzhiyun 		return ret;
2200*4882a593Smuzhiyun 	}
2201*4882a593Smuzhiyun 
2202*4882a593Smuzhiyun 	/*
2203*4882a593Smuzhiyun 	 * Now that our block group has its ->space_info set and is inserted in
2204*4882a593Smuzhiyun 	 * the rbtree, update the space info's counters.
2205*4882a593Smuzhiyun 	 */
2206*4882a593Smuzhiyun 	trace_btrfs_add_block_group(fs_info, cache, 1);
2207*4882a593Smuzhiyun 	btrfs_update_space_info(fs_info, cache->flags, size, bytes_used,
2208*4882a593Smuzhiyun 				cache->bytes_super, &cache->space_info);
2209*4882a593Smuzhiyun 	btrfs_update_global_block_rsv(fs_info);
2210*4882a593Smuzhiyun 
2211*4882a593Smuzhiyun 	link_block_group(cache);
2212*4882a593Smuzhiyun 
2213*4882a593Smuzhiyun 	list_add_tail(&cache->bg_list, &trans->new_bgs);
2214*4882a593Smuzhiyun 	trans->delayed_ref_updates++;
2215*4882a593Smuzhiyun 	btrfs_update_delayed_refs_rsv(trans);
2216*4882a593Smuzhiyun 
2217*4882a593Smuzhiyun 	set_avail_alloc_bits(fs_info, type);
2218*4882a593Smuzhiyun 	return 0;
2219*4882a593Smuzhiyun }
2220*4882a593Smuzhiyun 
2221*4882a593Smuzhiyun /*
2222*4882a593Smuzhiyun  * Mark one block group RO, can be called several times for the same block
2223*4882a593Smuzhiyun  * group.
2224*4882a593Smuzhiyun  *
2225*4882a593Smuzhiyun  * @cache:		the destination block group
2226*4882a593Smuzhiyun  * @do_chunk_alloc:	whether need to do chunk pre-allocation, this is to
2227*4882a593Smuzhiyun  * 			ensure we still have some free space after marking this
2228*4882a593Smuzhiyun  * 			block group RO.
2229*4882a593Smuzhiyun  */
btrfs_inc_block_group_ro(struct btrfs_block_group * cache,bool do_chunk_alloc)2230*4882a593Smuzhiyun int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
2231*4882a593Smuzhiyun 			     bool do_chunk_alloc)
2232*4882a593Smuzhiyun {
2233*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = cache->fs_info;
2234*4882a593Smuzhiyun 	struct btrfs_trans_handle *trans;
2235*4882a593Smuzhiyun 	u64 alloc_flags;
2236*4882a593Smuzhiyun 	int ret;
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun again:
2239*4882a593Smuzhiyun 	trans = btrfs_join_transaction(fs_info->extent_root);
2240*4882a593Smuzhiyun 	if (IS_ERR(trans))
2241*4882a593Smuzhiyun 		return PTR_ERR(trans);
2242*4882a593Smuzhiyun 
2243*4882a593Smuzhiyun 	/*
2244*4882a593Smuzhiyun 	 * we're not allowed to set block groups readonly after the dirty
2245*4882a593Smuzhiyun 	 * block groups cache has started writing.  If it already started,
2246*4882a593Smuzhiyun 	 * back off and let this transaction commit
2247*4882a593Smuzhiyun 	 */
2248*4882a593Smuzhiyun 	mutex_lock(&fs_info->ro_block_group_mutex);
2249*4882a593Smuzhiyun 	if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
2250*4882a593Smuzhiyun 		u64 transid = trans->transid;
2251*4882a593Smuzhiyun 
2252*4882a593Smuzhiyun 		mutex_unlock(&fs_info->ro_block_group_mutex);
2253*4882a593Smuzhiyun 		btrfs_end_transaction(trans);
2254*4882a593Smuzhiyun 
2255*4882a593Smuzhiyun 		ret = btrfs_wait_for_commit(fs_info, transid);
2256*4882a593Smuzhiyun 		if (ret)
2257*4882a593Smuzhiyun 			return ret;
2258*4882a593Smuzhiyun 		goto again;
2259*4882a593Smuzhiyun 	}
2260*4882a593Smuzhiyun 
2261*4882a593Smuzhiyun 	if (do_chunk_alloc) {
2262*4882a593Smuzhiyun 		/*
2263*4882a593Smuzhiyun 		 * If we are changing raid levels, try to allocate a
2264*4882a593Smuzhiyun 		 * corresponding block group with the new raid level.
2265*4882a593Smuzhiyun 		 */
2266*4882a593Smuzhiyun 		alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
2267*4882a593Smuzhiyun 		if (alloc_flags != cache->flags) {
2268*4882a593Smuzhiyun 			ret = btrfs_chunk_alloc(trans, alloc_flags,
2269*4882a593Smuzhiyun 						CHUNK_ALLOC_FORCE);
2270*4882a593Smuzhiyun 			/*
2271*4882a593Smuzhiyun 			 * ENOSPC is allowed here, we may have enough space
2272*4882a593Smuzhiyun 			 * already allocated at the new raid level to carry on
2273*4882a593Smuzhiyun 			 */
2274*4882a593Smuzhiyun 			if (ret == -ENOSPC)
2275*4882a593Smuzhiyun 				ret = 0;
2276*4882a593Smuzhiyun 			if (ret < 0)
2277*4882a593Smuzhiyun 				goto out;
2278*4882a593Smuzhiyun 		}
2279*4882a593Smuzhiyun 	}
2280*4882a593Smuzhiyun 
2281*4882a593Smuzhiyun 	ret = inc_block_group_ro(cache, 0);
2282*4882a593Smuzhiyun 	if (!do_chunk_alloc || ret == -ETXTBSY)
2283*4882a593Smuzhiyun 		goto unlock_out;
2284*4882a593Smuzhiyun 	if (!ret)
2285*4882a593Smuzhiyun 		goto out;
2286*4882a593Smuzhiyun 	alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags);
2287*4882a593Smuzhiyun 	ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
2288*4882a593Smuzhiyun 	if (ret < 0)
2289*4882a593Smuzhiyun 		goto out;
2290*4882a593Smuzhiyun 	ret = inc_block_group_ro(cache, 0);
2291*4882a593Smuzhiyun 	if (ret == -ETXTBSY)
2292*4882a593Smuzhiyun 		goto unlock_out;
2293*4882a593Smuzhiyun out:
2294*4882a593Smuzhiyun 	if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2295*4882a593Smuzhiyun 		alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
2296*4882a593Smuzhiyun 		mutex_lock(&fs_info->chunk_mutex);
2297*4882a593Smuzhiyun 		check_system_chunk(trans, alloc_flags);
2298*4882a593Smuzhiyun 		mutex_unlock(&fs_info->chunk_mutex);
2299*4882a593Smuzhiyun 	}
2300*4882a593Smuzhiyun unlock_out:
2301*4882a593Smuzhiyun 	mutex_unlock(&fs_info->ro_block_group_mutex);
2302*4882a593Smuzhiyun 
2303*4882a593Smuzhiyun 	btrfs_end_transaction(trans);
2304*4882a593Smuzhiyun 	return ret;
2305*4882a593Smuzhiyun }
2306*4882a593Smuzhiyun 
btrfs_dec_block_group_ro(struct btrfs_block_group * cache)2307*4882a593Smuzhiyun void btrfs_dec_block_group_ro(struct btrfs_block_group *cache)
2308*4882a593Smuzhiyun {
2309*4882a593Smuzhiyun 	struct btrfs_space_info *sinfo = cache->space_info;
2310*4882a593Smuzhiyun 	u64 num_bytes;
2311*4882a593Smuzhiyun 
2312*4882a593Smuzhiyun 	BUG_ON(!cache->ro);
2313*4882a593Smuzhiyun 
2314*4882a593Smuzhiyun 	spin_lock(&sinfo->lock);
2315*4882a593Smuzhiyun 	spin_lock(&cache->lock);
2316*4882a593Smuzhiyun 	if (!--cache->ro) {
2317*4882a593Smuzhiyun 		num_bytes = cache->length - cache->reserved -
2318*4882a593Smuzhiyun 			    cache->pinned - cache->bytes_super - cache->used;
2319*4882a593Smuzhiyun 		sinfo->bytes_readonly -= num_bytes;
2320*4882a593Smuzhiyun 		list_del_init(&cache->ro_list);
2321*4882a593Smuzhiyun 	}
2322*4882a593Smuzhiyun 	spin_unlock(&cache->lock);
2323*4882a593Smuzhiyun 	spin_unlock(&sinfo->lock);
2324*4882a593Smuzhiyun }
2325*4882a593Smuzhiyun 
update_block_group_item(struct btrfs_trans_handle * trans,struct btrfs_path * path,struct btrfs_block_group * cache)2326*4882a593Smuzhiyun static int update_block_group_item(struct btrfs_trans_handle *trans,
2327*4882a593Smuzhiyun 				   struct btrfs_path *path,
2328*4882a593Smuzhiyun 				   struct btrfs_block_group *cache)
2329*4882a593Smuzhiyun {
2330*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = trans->fs_info;
2331*4882a593Smuzhiyun 	int ret;
2332*4882a593Smuzhiyun 	struct btrfs_root *root = fs_info->extent_root;
2333*4882a593Smuzhiyun 	unsigned long bi;
2334*4882a593Smuzhiyun 	struct extent_buffer *leaf;
2335*4882a593Smuzhiyun 	struct btrfs_block_group_item bgi;
2336*4882a593Smuzhiyun 	struct btrfs_key key;
2337*4882a593Smuzhiyun 
2338*4882a593Smuzhiyun 	key.objectid = cache->start;
2339*4882a593Smuzhiyun 	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2340*4882a593Smuzhiyun 	key.offset = cache->length;
2341*4882a593Smuzhiyun 
2342*4882a593Smuzhiyun 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2343*4882a593Smuzhiyun 	if (ret) {
2344*4882a593Smuzhiyun 		if (ret > 0)
2345*4882a593Smuzhiyun 			ret = -ENOENT;
2346*4882a593Smuzhiyun 		goto fail;
2347*4882a593Smuzhiyun 	}
2348*4882a593Smuzhiyun 
2349*4882a593Smuzhiyun 	leaf = path->nodes[0];
2350*4882a593Smuzhiyun 	bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2351*4882a593Smuzhiyun 	btrfs_set_stack_block_group_used(&bgi, cache->used);
2352*4882a593Smuzhiyun 	btrfs_set_stack_block_group_chunk_objectid(&bgi,
2353*4882a593Smuzhiyun 			BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2354*4882a593Smuzhiyun 	btrfs_set_stack_block_group_flags(&bgi, cache->flags);
2355*4882a593Smuzhiyun 	write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
2356*4882a593Smuzhiyun 	btrfs_mark_buffer_dirty(leaf);
2357*4882a593Smuzhiyun fail:
2358*4882a593Smuzhiyun 	btrfs_release_path(path);
2359*4882a593Smuzhiyun 	return ret;
2360*4882a593Smuzhiyun 
2361*4882a593Smuzhiyun }
2362*4882a593Smuzhiyun 
cache_save_setup(struct btrfs_block_group * block_group,struct btrfs_trans_handle * trans,struct btrfs_path * path)2363*4882a593Smuzhiyun static int cache_save_setup(struct btrfs_block_group *block_group,
2364*4882a593Smuzhiyun 			    struct btrfs_trans_handle *trans,
2365*4882a593Smuzhiyun 			    struct btrfs_path *path)
2366*4882a593Smuzhiyun {
2367*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2368*4882a593Smuzhiyun 	struct btrfs_root *root = fs_info->tree_root;
2369*4882a593Smuzhiyun 	struct inode *inode = NULL;
2370*4882a593Smuzhiyun 	struct extent_changeset *data_reserved = NULL;
2371*4882a593Smuzhiyun 	u64 alloc_hint = 0;
2372*4882a593Smuzhiyun 	int dcs = BTRFS_DC_ERROR;
2373*4882a593Smuzhiyun 	u64 num_pages = 0;
2374*4882a593Smuzhiyun 	int retries = 0;
2375*4882a593Smuzhiyun 	int ret = 0;
2376*4882a593Smuzhiyun 
2377*4882a593Smuzhiyun 	/*
2378*4882a593Smuzhiyun 	 * If this block group is smaller than 100 megs don't bother caching the
2379*4882a593Smuzhiyun 	 * block group.
2380*4882a593Smuzhiyun 	 */
2381*4882a593Smuzhiyun 	if (block_group->length < (100 * SZ_1M)) {
2382*4882a593Smuzhiyun 		spin_lock(&block_group->lock);
2383*4882a593Smuzhiyun 		block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2384*4882a593Smuzhiyun 		spin_unlock(&block_group->lock);
2385*4882a593Smuzhiyun 		return 0;
2386*4882a593Smuzhiyun 	}
2387*4882a593Smuzhiyun 
2388*4882a593Smuzhiyun 	if (TRANS_ABORTED(trans))
2389*4882a593Smuzhiyun 		return 0;
2390*4882a593Smuzhiyun again:
2391*4882a593Smuzhiyun 	inode = lookup_free_space_inode(block_group, path);
2392*4882a593Smuzhiyun 	if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2393*4882a593Smuzhiyun 		ret = PTR_ERR(inode);
2394*4882a593Smuzhiyun 		btrfs_release_path(path);
2395*4882a593Smuzhiyun 		goto out;
2396*4882a593Smuzhiyun 	}
2397*4882a593Smuzhiyun 
2398*4882a593Smuzhiyun 	if (IS_ERR(inode)) {
2399*4882a593Smuzhiyun 		BUG_ON(retries);
2400*4882a593Smuzhiyun 		retries++;
2401*4882a593Smuzhiyun 
2402*4882a593Smuzhiyun 		if (block_group->ro)
2403*4882a593Smuzhiyun 			goto out_free;
2404*4882a593Smuzhiyun 
2405*4882a593Smuzhiyun 		ret = create_free_space_inode(trans, block_group, path);
2406*4882a593Smuzhiyun 		if (ret)
2407*4882a593Smuzhiyun 			goto out_free;
2408*4882a593Smuzhiyun 		goto again;
2409*4882a593Smuzhiyun 	}
2410*4882a593Smuzhiyun 
2411*4882a593Smuzhiyun 	/*
2412*4882a593Smuzhiyun 	 * We want to set the generation to 0, that way if anything goes wrong
2413*4882a593Smuzhiyun 	 * from here on out we know not to trust this cache when we load up next
2414*4882a593Smuzhiyun 	 * time.
2415*4882a593Smuzhiyun 	 */
2416*4882a593Smuzhiyun 	BTRFS_I(inode)->generation = 0;
2417*4882a593Smuzhiyun 	ret = btrfs_update_inode(trans, root, inode);
2418*4882a593Smuzhiyun 	if (ret) {
2419*4882a593Smuzhiyun 		/*
2420*4882a593Smuzhiyun 		 * So theoretically we could recover from this, simply set the
2421*4882a593Smuzhiyun 		 * super cache generation to 0 so we know to invalidate the
2422*4882a593Smuzhiyun 		 * cache, but then we'd have to keep track of the block groups
2423*4882a593Smuzhiyun 		 * that fail this way so we know we _have_ to reset this cache
2424*4882a593Smuzhiyun 		 * before the next commit or risk reading stale cache.  So to
2425*4882a593Smuzhiyun 		 * limit our exposure to horrible edge cases lets just abort the
2426*4882a593Smuzhiyun 		 * transaction, this only happens in really bad situations
2427*4882a593Smuzhiyun 		 * anyway.
2428*4882a593Smuzhiyun 		 */
2429*4882a593Smuzhiyun 		btrfs_abort_transaction(trans, ret);
2430*4882a593Smuzhiyun 		goto out_put;
2431*4882a593Smuzhiyun 	}
2432*4882a593Smuzhiyun 	WARN_ON(ret);
2433*4882a593Smuzhiyun 
2434*4882a593Smuzhiyun 	/* We've already setup this transaction, go ahead and exit */
2435*4882a593Smuzhiyun 	if (block_group->cache_generation == trans->transid &&
2436*4882a593Smuzhiyun 	    i_size_read(inode)) {
2437*4882a593Smuzhiyun 		dcs = BTRFS_DC_SETUP;
2438*4882a593Smuzhiyun 		goto out_put;
2439*4882a593Smuzhiyun 	}
2440*4882a593Smuzhiyun 
2441*4882a593Smuzhiyun 	if (i_size_read(inode) > 0) {
2442*4882a593Smuzhiyun 		ret = btrfs_check_trunc_cache_free_space(fs_info,
2443*4882a593Smuzhiyun 					&fs_info->global_block_rsv);
2444*4882a593Smuzhiyun 		if (ret)
2445*4882a593Smuzhiyun 			goto out_put;
2446*4882a593Smuzhiyun 
2447*4882a593Smuzhiyun 		ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
2448*4882a593Smuzhiyun 		if (ret)
2449*4882a593Smuzhiyun 			goto out_put;
2450*4882a593Smuzhiyun 	}
2451*4882a593Smuzhiyun 
2452*4882a593Smuzhiyun 	spin_lock(&block_group->lock);
2453*4882a593Smuzhiyun 	if (block_group->cached != BTRFS_CACHE_FINISHED ||
2454*4882a593Smuzhiyun 	    !btrfs_test_opt(fs_info, SPACE_CACHE)) {
2455*4882a593Smuzhiyun 		/*
2456*4882a593Smuzhiyun 		 * don't bother trying to write stuff out _if_
2457*4882a593Smuzhiyun 		 * a) we're not cached,
2458*4882a593Smuzhiyun 		 * b) we're with nospace_cache mount option,
2459*4882a593Smuzhiyun 		 * c) we're with v2 space_cache (FREE_SPACE_TREE).
2460*4882a593Smuzhiyun 		 */
2461*4882a593Smuzhiyun 		dcs = BTRFS_DC_WRITTEN;
2462*4882a593Smuzhiyun 		spin_unlock(&block_group->lock);
2463*4882a593Smuzhiyun 		goto out_put;
2464*4882a593Smuzhiyun 	}
2465*4882a593Smuzhiyun 	spin_unlock(&block_group->lock);
2466*4882a593Smuzhiyun 
2467*4882a593Smuzhiyun 	/*
2468*4882a593Smuzhiyun 	 * We hit an ENOSPC when setting up the cache in this transaction, just
2469*4882a593Smuzhiyun 	 * skip doing the setup, we've already cleared the cache so we're safe.
2470*4882a593Smuzhiyun 	 */
2471*4882a593Smuzhiyun 	if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
2472*4882a593Smuzhiyun 		ret = -ENOSPC;
2473*4882a593Smuzhiyun 		goto out_put;
2474*4882a593Smuzhiyun 	}
2475*4882a593Smuzhiyun 
2476*4882a593Smuzhiyun 	/*
2477*4882a593Smuzhiyun 	 * Try to preallocate enough space based on how big the block group is.
2478*4882a593Smuzhiyun 	 * Keep in mind this has to include any pinned space which could end up
2479*4882a593Smuzhiyun 	 * taking up quite a bit since it's not folded into the other space
2480*4882a593Smuzhiyun 	 * cache.
2481*4882a593Smuzhiyun 	 */
2482*4882a593Smuzhiyun 	num_pages = div_u64(block_group->length, SZ_256M);
2483*4882a593Smuzhiyun 	if (!num_pages)
2484*4882a593Smuzhiyun 		num_pages = 1;
2485*4882a593Smuzhiyun 
2486*4882a593Smuzhiyun 	num_pages *= 16;
2487*4882a593Smuzhiyun 	num_pages *= PAGE_SIZE;
2488*4882a593Smuzhiyun 
2489*4882a593Smuzhiyun 	ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, 0,
2490*4882a593Smuzhiyun 					  num_pages);
2491*4882a593Smuzhiyun 	if (ret)
2492*4882a593Smuzhiyun 		goto out_put;
2493*4882a593Smuzhiyun 
2494*4882a593Smuzhiyun 	ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2495*4882a593Smuzhiyun 					      num_pages, num_pages,
2496*4882a593Smuzhiyun 					      &alloc_hint);
2497*4882a593Smuzhiyun 	/*
2498*4882a593Smuzhiyun 	 * Our cache requires contiguous chunks so that we don't modify a bunch
2499*4882a593Smuzhiyun 	 * of metadata or split extents when writing the cache out, which means
2500*4882a593Smuzhiyun 	 * we can enospc if we are heavily fragmented in addition to just normal
2501*4882a593Smuzhiyun 	 * out of space conditions.  So if we hit this just skip setting up any
2502*4882a593Smuzhiyun 	 * other block groups for this transaction, maybe we'll unpin enough
2503*4882a593Smuzhiyun 	 * space the next time around.
2504*4882a593Smuzhiyun 	 */
2505*4882a593Smuzhiyun 	if (!ret)
2506*4882a593Smuzhiyun 		dcs = BTRFS_DC_SETUP;
2507*4882a593Smuzhiyun 	else if (ret == -ENOSPC)
2508*4882a593Smuzhiyun 		set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
2509*4882a593Smuzhiyun 
2510*4882a593Smuzhiyun out_put:
2511*4882a593Smuzhiyun 	iput(inode);
2512*4882a593Smuzhiyun out_free:
2513*4882a593Smuzhiyun 	btrfs_release_path(path);
2514*4882a593Smuzhiyun out:
2515*4882a593Smuzhiyun 	spin_lock(&block_group->lock);
2516*4882a593Smuzhiyun 	if (!ret && dcs == BTRFS_DC_SETUP)
2517*4882a593Smuzhiyun 		block_group->cache_generation = trans->transid;
2518*4882a593Smuzhiyun 	block_group->disk_cache_state = dcs;
2519*4882a593Smuzhiyun 	spin_unlock(&block_group->lock);
2520*4882a593Smuzhiyun 
2521*4882a593Smuzhiyun 	extent_changeset_free(data_reserved);
2522*4882a593Smuzhiyun 	return ret;
2523*4882a593Smuzhiyun }
2524*4882a593Smuzhiyun 
btrfs_setup_space_cache(struct btrfs_trans_handle * trans)2525*4882a593Smuzhiyun int btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
2526*4882a593Smuzhiyun {
2527*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = trans->fs_info;
2528*4882a593Smuzhiyun 	struct btrfs_block_group *cache, *tmp;
2529*4882a593Smuzhiyun 	struct btrfs_transaction *cur_trans = trans->transaction;
2530*4882a593Smuzhiyun 	struct btrfs_path *path;
2531*4882a593Smuzhiyun 
2532*4882a593Smuzhiyun 	if (list_empty(&cur_trans->dirty_bgs) ||
2533*4882a593Smuzhiyun 	    !btrfs_test_opt(fs_info, SPACE_CACHE))
2534*4882a593Smuzhiyun 		return 0;
2535*4882a593Smuzhiyun 
2536*4882a593Smuzhiyun 	path = btrfs_alloc_path();
2537*4882a593Smuzhiyun 	if (!path)
2538*4882a593Smuzhiyun 		return -ENOMEM;
2539*4882a593Smuzhiyun 
2540*4882a593Smuzhiyun 	/* Could add new block groups, use _safe just in case */
2541*4882a593Smuzhiyun 	list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
2542*4882a593Smuzhiyun 				 dirty_list) {
2543*4882a593Smuzhiyun 		if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2544*4882a593Smuzhiyun 			cache_save_setup(cache, trans, path);
2545*4882a593Smuzhiyun 	}
2546*4882a593Smuzhiyun 
2547*4882a593Smuzhiyun 	btrfs_free_path(path);
2548*4882a593Smuzhiyun 	return 0;
2549*4882a593Smuzhiyun }
2550*4882a593Smuzhiyun 
2551*4882a593Smuzhiyun /*
2552*4882a593Smuzhiyun  * Transaction commit does final block group cache writeback during a critical
2553*4882a593Smuzhiyun  * section where nothing is allowed to change the FS.  This is required in
2554*4882a593Smuzhiyun  * order for the cache to actually match the block group, but can introduce a
2555*4882a593Smuzhiyun  * lot of latency into the commit.
2556*4882a593Smuzhiyun  *
2557*4882a593Smuzhiyun  * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
2558*4882a593Smuzhiyun  * There's a chance we'll have to redo some of it if the block group changes
2559*4882a593Smuzhiyun  * again during the commit, but it greatly reduces the commit latency by
2560*4882a593Smuzhiyun  * getting rid of the easy block groups while we're still allowing others to
2561*4882a593Smuzhiyun  * join the commit.
2562*4882a593Smuzhiyun  */
btrfs_start_dirty_block_groups(struct btrfs_trans_handle * trans)2563*4882a593Smuzhiyun int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
2564*4882a593Smuzhiyun {
2565*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = trans->fs_info;
2566*4882a593Smuzhiyun 	struct btrfs_block_group *cache;
2567*4882a593Smuzhiyun 	struct btrfs_transaction *cur_trans = trans->transaction;
2568*4882a593Smuzhiyun 	int ret = 0;
2569*4882a593Smuzhiyun 	int should_put;
2570*4882a593Smuzhiyun 	struct btrfs_path *path = NULL;
2571*4882a593Smuzhiyun 	LIST_HEAD(dirty);
2572*4882a593Smuzhiyun 	struct list_head *io = &cur_trans->io_bgs;
2573*4882a593Smuzhiyun 	int loops = 0;
2574*4882a593Smuzhiyun 
2575*4882a593Smuzhiyun 	spin_lock(&cur_trans->dirty_bgs_lock);
2576*4882a593Smuzhiyun 	if (list_empty(&cur_trans->dirty_bgs)) {
2577*4882a593Smuzhiyun 		spin_unlock(&cur_trans->dirty_bgs_lock);
2578*4882a593Smuzhiyun 		return 0;
2579*4882a593Smuzhiyun 	}
2580*4882a593Smuzhiyun 	list_splice_init(&cur_trans->dirty_bgs, &dirty);
2581*4882a593Smuzhiyun 	spin_unlock(&cur_trans->dirty_bgs_lock);
2582*4882a593Smuzhiyun 
2583*4882a593Smuzhiyun again:
2584*4882a593Smuzhiyun 	/* Make sure all the block groups on our dirty list actually exist */
2585*4882a593Smuzhiyun 	btrfs_create_pending_block_groups(trans);
2586*4882a593Smuzhiyun 
2587*4882a593Smuzhiyun 	if (!path) {
2588*4882a593Smuzhiyun 		path = btrfs_alloc_path();
2589*4882a593Smuzhiyun 		if (!path) {
2590*4882a593Smuzhiyun 			ret = -ENOMEM;
2591*4882a593Smuzhiyun 			goto out;
2592*4882a593Smuzhiyun 		}
2593*4882a593Smuzhiyun 	}
2594*4882a593Smuzhiyun 
2595*4882a593Smuzhiyun 	/*
2596*4882a593Smuzhiyun 	 * cache_write_mutex is here only to save us from balance or automatic
2597*4882a593Smuzhiyun 	 * removal of empty block groups deleting this block group while we are
2598*4882a593Smuzhiyun 	 * writing out the cache
2599*4882a593Smuzhiyun 	 */
2600*4882a593Smuzhiyun 	mutex_lock(&trans->transaction->cache_write_mutex);
2601*4882a593Smuzhiyun 	while (!list_empty(&dirty)) {
2602*4882a593Smuzhiyun 		bool drop_reserve = true;
2603*4882a593Smuzhiyun 
2604*4882a593Smuzhiyun 		cache = list_first_entry(&dirty, struct btrfs_block_group,
2605*4882a593Smuzhiyun 					 dirty_list);
2606*4882a593Smuzhiyun 		/*
2607*4882a593Smuzhiyun 		 * This can happen if something re-dirties a block group that
2608*4882a593Smuzhiyun 		 * is already under IO.  Just wait for it to finish and then do
2609*4882a593Smuzhiyun 		 * it all again
2610*4882a593Smuzhiyun 		 */
2611*4882a593Smuzhiyun 		if (!list_empty(&cache->io_list)) {
2612*4882a593Smuzhiyun 			list_del_init(&cache->io_list);
2613*4882a593Smuzhiyun 			btrfs_wait_cache_io(trans, cache, path);
2614*4882a593Smuzhiyun 			btrfs_put_block_group(cache);
2615*4882a593Smuzhiyun 		}
2616*4882a593Smuzhiyun 
2617*4882a593Smuzhiyun 
2618*4882a593Smuzhiyun 		/*
2619*4882a593Smuzhiyun 		 * btrfs_wait_cache_io uses the cache->dirty_list to decide if
2620*4882a593Smuzhiyun 		 * it should update the cache_state.  Don't delete until after
2621*4882a593Smuzhiyun 		 * we wait.
2622*4882a593Smuzhiyun 		 *
2623*4882a593Smuzhiyun 		 * Since we're not running in the commit critical section
2624*4882a593Smuzhiyun 		 * we need the dirty_bgs_lock to protect from update_block_group
2625*4882a593Smuzhiyun 		 */
2626*4882a593Smuzhiyun 		spin_lock(&cur_trans->dirty_bgs_lock);
2627*4882a593Smuzhiyun 		list_del_init(&cache->dirty_list);
2628*4882a593Smuzhiyun 		spin_unlock(&cur_trans->dirty_bgs_lock);
2629*4882a593Smuzhiyun 
2630*4882a593Smuzhiyun 		should_put = 1;
2631*4882a593Smuzhiyun 
2632*4882a593Smuzhiyun 		cache_save_setup(cache, trans, path);
2633*4882a593Smuzhiyun 
2634*4882a593Smuzhiyun 		if (cache->disk_cache_state == BTRFS_DC_SETUP) {
2635*4882a593Smuzhiyun 			cache->io_ctl.inode = NULL;
2636*4882a593Smuzhiyun 			ret = btrfs_write_out_cache(trans, cache, path);
2637*4882a593Smuzhiyun 			if (ret == 0 && cache->io_ctl.inode) {
2638*4882a593Smuzhiyun 				should_put = 0;
2639*4882a593Smuzhiyun 
2640*4882a593Smuzhiyun 				/*
2641*4882a593Smuzhiyun 				 * The cache_write_mutex is protecting the
2642*4882a593Smuzhiyun 				 * io_list, also refer to the definition of
2643*4882a593Smuzhiyun 				 * btrfs_transaction::io_bgs for more details
2644*4882a593Smuzhiyun 				 */
2645*4882a593Smuzhiyun 				list_add_tail(&cache->io_list, io);
2646*4882a593Smuzhiyun 			} else {
2647*4882a593Smuzhiyun 				/*
2648*4882a593Smuzhiyun 				 * If we failed to write the cache, the
2649*4882a593Smuzhiyun 				 * generation will be bad and life goes on
2650*4882a593Smuzhiyun 				 */
2651*4882a593Smuzhiyun 				ret = 0;
2652*4882a593Smuzhiyun 			}
2653*4882a593Smuzhiyun 		}
2654*4882a593Smuzhiyun 		if (!ret) {
2655*4882a593Smuzhiyun 			ret = update_block_group_item(trans, path, cache);
2656*4882a593Smuzhiyun 			/*
2657*4882a593Smuzhiyun 			 * Our block group might still be attached to the list
2658*4882a593Smuzhiyun 			 * of new block groups in the transaction handle of some
2659*4882a593Smuzhiyun 			 * other task (struct btrfs_trans_handle->new_bgs). This
2660*4882a593Smuzhiyun 			 * means its block group item isn't yet in the extent
2661*4882a593Smuzhiyun 			 * tree. If this happens ignore the error, as we will
2662*4882a593Smuzhiyun 			 * try again later in the critical section of the
2663*4882a593Smuzhiyun 			 * transaction commit.
2664*4882a593Smuzhiyun 			 */
2665*4882a593Smuzhiyun 			if (ret == -ENOENT) {
2666*4882a593Smuzhiyun 				ret = 0;
2667*4882a593Smuzhiyun 				spin_lock(&cur_trans->dirty_bgs_lock);
2668*4882a593Smuzhiyun 				if (list_empty(&cache->dirty_list)) {
2669*4882a593Smuzhiyun 					list_add_tail(&cache->dirty_list,
2670*4882a593Smuzhiyun 						      &cur_trans->dirty_bgs);
2671*4882a593Smuzhiyun 					btrfs_get_block_group(cache);
2672*4882a593Smuzhiyun 					drop_reserve = false;
2673*4882a593Smuzhiyun 				}
2674*4882a593Smuzhiyun 				spin_unlock(&cur_trans->dirty_bgs_lock);
2675*4882a593Smuzhiyun 			} else if (ret) {
2676*4882a593Smuzhiyun 				btrfs_abort_transaction(trans, ret);
2677*4882a593Smuzhiyun 			}
2678*4882a593Smuzhiyun 		}
2679*4882a593Smuzhiyun 
2680*4882a593Smuzhiyun 		/* If it's not on the io list, we need to put the block group */
2681*4882a593Smuzhiyun 		if (should_put)
2682*4882a593Smuzhiyun 			btrfs_put_block_group(cache);
2683*4882a593Smuzhiyun 		if (drop_reserve)
2684*4882a593Smuzhiyun 			btrfs_delayed_refs_rsv_release(fs_info, 1);
2685*4882a593Smuzhiyun 		/*
2686*4882a593Smuzhiyun 		 * Avoid blocking other tasks for too long. It might even save
2687*4882a593Smuzhiyun 		 * us from writing caches for block groups that are going to be
2688*4882a593Smuzhiyun 		 * removed.
2689*4882a593Smuzhiyun 		 */
2690*4882a593Smuzhiyun 		mutex_unlock(&trans->transaction->cache_write_mutex);
2691*4882a593Smuzhiyun 		if (ret)
2692*4882a593Smuzhiyun 			goto out;
2693*4882a593Smuzhiyun 		mutex_lock(&trans->transaction->cache_write_mutex);
2694*4882a593Smuzhiyun 	}
2695*4882a593Smuzhiyun 	mutex_unlock(&trans->transaction->cache_write_mutex);
2696*4882a593Smuzhiyun 
2697*4882a593Smuzhiyun 	/*
2698*4882a593Smuzhiyun 	 * Go through delayed refs for all the stuff we've just kicked off
2699*4882a593Smuzhiyun 	 * and then loop back (just once)
2700*4882a593Smuzhiyun 	 */
2701*4882a593Smuzhiyun 	if (!ret)
2702*4882a593Smuzhiyun 		ret = btrfs_run_delayed_refs(trans, 0);
2703*4882a593Smuzhiyun 	if (!ret && loops == 0) {
2704*4882a593Smuzhiyun 		loops++;
2705*4882a593Smuzhiyun 		spin_lock(&cur_trans->dirty_bgs_lock);
2706*4882a593Smuzhiyun 		list_splice_init(&cur_trans->dirty_bgs, &dirty);
2707*4882a593Smuzhiyun 		/*
2708*4882a593Smuzhiyun 		 * dirty_bgs_lock protects us from concurrent block group
2709*4882a593Smuzhiyun 		 * deletes too (not just cache_write_mutex).
2710*4882a593Smuzhiyun 		 */
2711*4882a593Smuzhiyun 		if (!list_empty(&dirty)) {
2712*4882a593Smuzhiyun 			spin_unlock(&cur_trans->dirty_bgs_lock);
2713*4882a593Smuzhiyun 			goto again;
2714*4882a593Smuzhiyun 		}
2715*4882a593Smuzhiyun 		spin_unlock(&cur_trans->dirty_bgs_lock);
2716*4882a593Smuzhiyun 	}
2717*4882a593Smuzhiyun out:
2718*4882a593Smuzhiyun 	if (ret < 0) {
2719*4882a593Smuzhiyun 		spin_lock(&cur_trans->dirty_bgs_lock);
2720*4882a593Smuzhiyun 		list_splice_init(&dirty, &cur_trans->dirty_bgs);
2721*4882a593Smuzhiyun 		spin_unlock(&cur_trans->dirty_bgs_lock);
2722*4882a593Smuzhiyun 		btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
2723*4882a593Smuzhiyun 	}
2724*4882a593Smuzhiyun 
2725*4882a593Smuzhiyun 	btrfs_free_path(path);
2726*4882a593Smuzhiyun 	return ret;
2727*4882a593Smuzhiyun }
2728*4882a593Smuzhiyun 
btrfs_write_dirty_block_groups(struct btrfs_trans_handle * trans)2729*4882a593Smuzhiyun int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
2730*4882a593Smuzhiyun {
2731*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = trans->fs_info;
2732*4882a593Smuzhiyun 	struct btrfs_block_group *cache;
2733*4882a593Smuzhiyun 	struct btrfs_transaction *cur_trans = trans->transaction;
2734*4882a593Smuzhiyun 	int ret = 0;
2735*4882a593Smuzhiyun 	int should_put;
2736*4882a593Smuzhiyun 	struct btrfs_path *path;
2737*4882a593Smuzhiyun 	struct list_head *io = &cur_trans->io_bgs;
2738*4882a593Smuzhiyun 
2739*4882a593Smuzhiyun 	path = btrfs_alloc_path();
2740*4882a593Smuzhiyun 	if (!path)
2741*4882a593Smuzhiyun 		return -ENOMEM;
2742*4882a593Smuzhiyun 
2743*4882a593Smuzhiyun 	/*
2744*4882a593Smuzhiyun 	 * Even though we are in the critical section of the transaction commit,
2745*4882a593Smuzhiyun 	 * we can still have concurrent tasks adding elements to this
2746*4882a593Smuzhiyun 	 * transaction's list of dirty block groups. These tasks correspond to
2747*4882a593Smuzhiyun 	 * endio free space workers started when writeback finishes for a
2748*4882a593Smuzhiyun 	 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
2749*4882a593Smuzhiyun 	 * allocate new block groups as a result of COWing nodes of the root
2750*4882a593Smuzhiyun 	 * tree when updating the free space inode. The writeback for the space
2751*4882a593Smuzhiyun 	 * caches is triggered by an earlier call to
2752*4882a593Smuzhiyun 	 * btrfs_start_dirty_block_groups() and iterations of the following
2753*4882a593Smuzhiyun 	 * loop.
2754*4882a593Smuzhiyun 	 * Also we want to do the cache_save_setup first and then run the
2755*4882a593Smuzhiyun 	 * delayed refs to make sure we have the best chance at doing this all
2756*4882a593Smuzhiyun 	 * in one shot.
2757*4882a593Smuzhiyun 	 */
2758*4882a593Smuzhiyun 	spin_lock(&cur_trans->dirty_bgs_lock);
2759*4882a593Smuzhiyun 	while (!list_empty(&cur_trans->dirty_bgs)) {
2760*4882a593Smuzhiyun 		cache = list_first_entry(&cur_trans->dirty_bgs,
2761*4882a593Smuzhiyun 					 struct btrfs_block_group,
2762*4882a593Smuzhiyun 					 dirty_list);
2763*4882a593Smuzhiyun 
2764*4882a593Smuzhiyun 		/*
2765*4882a593Smuzhiyun 		 * This can happen if cache_save_setup re-dirties a block group
2766*4882a593Smuzhiyun 		 * that is already under IO.  Just wait for it to finish and
2767*4882a593Smuzhiyun 		 * then do it all again
2768*4882a593Smuzhiyun 		 */
2769*4882a593Smuzhiyun 		if (!list_empty(&cache->io_list)) {
2770*4882a593Smuzhiyun 			spin_unlock(&cur_trans->dirty_bgs_lock);
2771*4882a593Smuzhiyun 			list_del_init(&cache->io_list);
2772*4882a593Smuzhiyun 			btrfs_wait_cache_io(trans, cache, path);
2773*4882a593Smuzhiyun 			btrfs_put_block_group(cache);
2774*4882a593Smuzhiyun 			spin_lock(&cur_trans->dirty_bgs_lock);
2775*4882a593Smuzhiyun 		}
2776*4882a593Smuzhiyun 
2777*4882a593Smuzhiyun 		/*
2778*4882a593Smuzhiyun 		 * Don't remove from the dirty list until after we've waited on
2779*4882a593Smuzhiyun 		 * any pending IO
2780*4882a593Smuzhiyun 		 */
2781*4882a593Smuzhiyun 		list_del_init(&cache->dirty_list);
2782*4882a593Smuzhiyun 		spin_unlock(&cur_trans->dirty_bgs_lock);
2783*4882a593Smuzhiyun 		should_put = 1;
2784*4882a593Smuzhiyun 
2785*4882a593Smuzhiyun 		cache_save_setup(cache, trans, path);
2786*4882a593Smuzhiyun 
2787*4882a593Smuzhiyun 		if (!ret)
2788*4882a593Smuzhiyun 			ret = btrfs_run_delayed_refs(trans,
2789*4882a593Smuzhiyun 						     (unsigned long) -1);
2790*4882a593Smuzhiyun 
2791*4882a593Smuzhiyun 		if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
2792*4882a593Smuzhiyun 			cache->io_ctl.inode = NULL;
2793*4882a593Smuzhiyun 			ret = btrfs_write_out_cache(trans, cache, path);
2794*4882a593Smuzhiyun 			if (ret == 0 && cache->io_ctl.inode) {
2795*4882a593Smuzhiyun 				should_put = 0;
2796*4882a593Smuzhiyun 				list_add_tail(&cache->io_list, io);
2797*4882a593Smuzhiyun 			} else {
2798*4882a593Smuzhiyun 				/*
2799*4882a593Smuzhiyun 				 * If we failed to write the cache, the
2800*4882a593Smuzhiyun 				 * generation will be bad and life goes on
2801*4882a593Smuzhiyun 				 */
2802*4882a593Smuzhiyun 				ret = 0;
2803*4882a593Smuzhiyun 			}
2804*4882a593Smuzhiyun 		}
2805*4882a593Smuzhiyun 		if (!ret) {
2806*4882a593Smuzhiyun 			ret = update_block_group_item(trans, path, cache);
2807*4882a593Smuzhiyun 			/*
2808*4882a593Smuzhiyun 			 * One of the free space endio workers might have
2809*4882a593Smuzhiyun 			 * created a new block group while updating a free space
2810*4882a593Smuzhiyun 			 * cache's inode (at inode.c:btrfs_finish_ordered_io())
2811*4882a593Smuzhiyun 			 * and hasn't released its transaction handle yet, in
2812*4882a593Smuzhiyun 			 * which case the new block group is still attached to
2813*4882a593Smuzhiyun 			 * its transaction handle and its creation has not
2814*4882a593Smuzhiyun 			 * finished yet (no block group item in the extent tree
2815*4882a593Smuzhiyun 			 * yet, etc). If this is the case, wait for all free
2816*4882a593Smuzhiyun 			 * space endio workers to finish and retry. This is a
2817*4882a593Smuzhiyun 			 * very rare case so no need for a more efficient and
2818*4882a593Smuzhiyun 			 * complex approach.
2819*4882a593Smuzhiyun 			 */
2820*4882a593Smuzhiyun 			if (ret == -ENOENT) {
2821*4882a593Smuzhiyun 				wait_event(cur_trans->writer_wait,
2822*4882a593Smuzhiyun 				   atomic_read(&cur_trans->num_writers) == 1);
2823*4882a593Smuzhiyun 				ret = update_block_group_item(trans, path, cache);
2824*4882a593Smuzhiyun 			}
2825*4882a593Smuzhiyun 			if (ret)
2826*4882a593Smuzhiyun 				btrfs_abort_transaction(trans, ret);
2827*4882a593Smuzhiyun 		}
2828*4882a593Smuzhiyun 
2829*4882a593Smuzhiyun 		/* If its not on the io list, we need to put the block group */
2830*4882a593Smuzhiyun 		if (should_put)
2831*4882a593Smuzhiyun 			btrfs_put_block_group(cache);
2832*4882a593Smuzhiyun 		btrfs_delayed_refs_rsv_release(fs_info, 1);
2833*4882a593Smuzhiyun 		spin_lock(&cur_trans->dirty_bgs_lock);
2834*4882a593Smuzhiyun 	}
2835*4882a593Smuzhiyun 	spin_unlock(&cur_trans->dirty_bgs_lock);
2836*4882a593Smuzhiyun 
2837*4882a593Smuzhiyun 	/*
2838*4882a593Smuzhiyun 	 * Refer to the definition of io_bgs member for details why it's safe
2839*4882a593Smuzhiyun 	 * to use it without any locking
2840*4882a593Smuzhiyun 	 */
2841*4882a593Smuzhiyun 	while (!list_empty(io)) {
2842*4882a593Smuzhiyun 		cache = list_first_entry(io, struct btrfs_block_group,
2843*4882a593Smuzhiyun 					 io_list);
2844*4882a593Smuzhiyun 		list_del_init(&cache->io_list);
2845*4882a593Smuzhiyun 		btrfs_wait_cache_io(trans, cache, path);
2846*4882a593Smuzhiyun 		btrfs_put_block_group(cache);
2847*4882a593Smuzhiyun 	}
2848*4882a593Smuzhiyun 
2849*4882a593Smuzhiyun 	btrfs_free_path(path);
2850*4882a593Smuzhiyun 	return ret;
2851*4882a593Smuzhiyun }
2852*4882a593Smuzhiyun 
btrfs_update_block_group(struct btrfs_trans_handle * trans,u64 bytenr,u64 num_bytes,int alloc)2853*4882a593Smuzhiyun int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2854*4882a593Smuzhiyun 			     u64 bytenr, u64 num_bytes, int alloc)
2855*4882a593Smuzhiyun {
2856*4882a593Smuzhiyun 	struct btrfs_fs_info *info = trans->fs_info;
2857*4882a593Smuzhiyun 	struct btrfs_block_group *cache = NULL;
2858*4882a593Smuzhiyun 	u64 total = num_bytes;
2859*4882a593Smuzhiyun 	u64 old_val;
2860*4882a593Smuzhiyun 	u64 byte_in_group;
2861*4882a593Smuzhiyun 	int factor;
2862*4882a593Smuzhiyun 	int ret = 0;
2863*4882a593Smuzhiyun 
2864*4882a593Smuzhiyun 	/* Block accounting for super block */
2865*4882a593Smuzhiyun 	spin_lock(&info->delalloc_root_lock);
2866*4882a593Smuzhiyun 	old_val = btrfs_super_bytes_used(info->super_copy);
2867*4882a593Smuzhiyun 	if (alloc)
2868*4882a593Smuzhiyun 		old_val += num_bytes;
2869*4882a593Smuzhiyun 	else
2870*4882a593Smuzhiyun 		old_val -= num_bytes;
2871*4882a593Smuzhiyun 	btrfs_set_super_bytes_used(info->super_copy, old_val);
2872*4882a593Smuzhiyun 	spin_unlock(&info->delalloc_root_lock);
2873*4882a593Smuzhiyun 
2874*4882a593Smuzhiyun 	while (total) {
2875*4882a593Smuzhiyun 		cache = btrfs_lookup_block_group(info, bytenr);
2876*4882a593Smuzhiyun 		if (!cache) {
2877*4882a593Smuzhiyun 			ret = -ENOENT;
2878*4882a593Smuzhiyun 			break;
2879*4882a593Smuzhiyun 		}
2880*4882a593Smuzhiyun 		factor = btrfs_bg_type_to_factor(cache->flags);
2881*4882a593Smuzhiyun 
2882*4882a593Smuzhiyun 		/*
2883*4882a593Smuzhiyun 		 * If this block group has free space cache written out, we
2884*4882a593Smuzhiyun 		 * need to make sure to load it if we are removing space.  This
2885*4882a593Smuzhiyun 		 * is because we need the unpinning stage to actually add the
2886*4882a593Smuzhiyun 		 * space back to the block group, otherwise we will leak space.
2887*4882a593Smuzhiyun 		 */
2888*4882a593Smuzhiyun 		if (!alloc && !btrfs_block_group_done(cache))
2889*4882a593Smuzhiyun 			btrfs_cache_block_group(cache, 1);
2890*4882a593Smuzhiyun 
2891*4882a593Smuzhiyun 		byte_in_group = bytenr - cache->start;
2892*4882a593Smuzhiyun 		WARN_ON(byte_in_group > cache->length);
2893*4882a593Smuzhiyun 
2894*4882a593Smuzhiyun 		spin_lock(&cache->space_info->lock);
2895*4882a593Smuzhiyun 		spin_lock(&cache->lock);
2896*4882a593Smuzhiyun 
2897*4882a593Smuzhiyun 		if (btrfs_test_opt(info, SPACE_CACHE) &&
2898*4882a593Smuzhiyun 		    cache->disk_cache_state < BTRFS_DC_CLEAR)
2899*4882a593Smuzhiyun 			cache->disk_cache_state = BTRFS_DC_CLEAR;
2900*4882a593Smuzhiyun 
2901*4882a593Smuzhiyun 		old_val = cache->used;
2902*4882a593Smuzhiyun 		num_bytes = min(total, cache->length - byte_in_group);
2903*4882a593Smuzhiyun 		if (alloc) {
2904*4882a593Smuzhiyun 			old_val += num_bytes;
2905*4882a593Smuzhiyun 			cache->used = old_val;
2906*4882a593Smuzhiyun 			cache->reserved -= num_bytes;
2907*4882a593Smuzhiyun 			cache->space_info->bytes_reserved -= num_bytes;
2908*4882a593Smuzhiyun 			cache->space_info->bytes_used += num_bytes;
2909*4882a593Smuzhiyun 			cache->space_info->disk_used += num_bytes * factor;
2910*4882a593Smuzhiyun 			spin_unlock(&cache->lock);
2911*4882a593Smuzhiyun 			spin_unlock(&cache->space_info->lock);
2912*4882a593Smuzhiyun 		} else {
2913*4882a593Smuzhiyun 			old_val -= num_bytes;
2914*4882a593Smuzhiyun 			cache->used = old_val;
2915*4882a593Smuzhiyun 			cache->pinned += num_bytes;
2916*4882a593Smuzhiyun 			btrfs_space_info_update_bytes_pinned(info,
2917*4882a593Smuzhiyun 					cache->space_info, num_bytes);
2918*4882a593Smuzhiyun 			cache->space_info->bytes_used -= num_bytes;
2919*4882a593Smuzhiyun 			cache->space_info->disk_used -= num_bytes * factor;
2920*4882a593Smuzhiyun 			spin_unlock(&cache->lock);
2921*4882a593Smuzhiyun 			spin_unlock(&cache->space_info->lock);
2922*4882a593Smuzhiyun 
2923*4882a593Smuzhiyun 			__btrfs_mod_total_bytes_pinned(cache->space_info,
2924*4882a593Smuzhiyun 						       num_bytes);
2925*4882a593Smuzhiyun 			set_extent_dirty(&trans->transaction->pinned_extents,
2926*4882a593Smuzhiyun 					 bytenr, bytenr + num_bytes - 1,
2927*4882a593Smuzhiyun 					 GFP_NOFS | __GFP_NOFAIL);
2928*4882a593Smuzhiyun 		}
2929*4882a593Smuzhiyun 
2930*4882a593Smuzhiyun 		spin_lock(&trans->transaction->dirty_bgs_lock);
2931*4882a593Smuzhiyun 		if (list_empty(&cache->dirty_list)) {
2932*4882a593Smuzhiyun 			list_add_tail(&cache->dirty_list,
2933*4882a593Smuzhiyun 				      &trans->transaction->dirty_bgs);
2934*4882a593Smuzhiyun 			trans->delayed_ref_updates++;
2935*4882a593Smuzhiyun 			btrfs_get_block_group(cache);
2936*4882a593Smuzhiyun 		}
2937*4882a593Smuzhiyun 		spin_unlock(&trans->transaction->dirty_bgs_lock);
2938*4882a593Smuzhiyun 
2939*4882a593Smuzhiyun 		/*
2940*4882a593Smuzhiyun 		 * No longer have used bytes in this block group, queue it for
2941*4882a593Smuzhiyun 		 * deletion. We do this after adding the block group to the
2942*4882a593Smuzhiyun 		 * dirty list to avoid races between cleaner kthread and space
2943*4882a593Smuzhiyun 		 * cache writeout.
2944*4882a593Smuzhiyun 		 */
2945*4882a593Smuzhiyun 		if (!alloc && old_val == 0) {
2946*4882a593Smuzhiyun 			if (!btrfs_test_opt(info, DISCARD_ASYNC))
2947*4882a593Smuzhiyun 				btrfs_mark_bg_unused(cache);
2948*4882a593Smuzhiyun 		}
2949*4882a593Smuzhiyun 
2950*4882a593Smuzhiyun 		btrfs_put_block_group(cache);
2951*4882a593Smuzhiyun 		total -= num_bytes;
2952*4882a593Smuzhiyun 		bytenr += num_bytes;
2953*4882a593Smuzhiyun 	}
2954*4882a593Smuzhiyun 
2955*4882a593Smuzhiyun 	/* Modified block groups are accounted for in the delayed_refs_rsv. */
2956*4882a593Smuzhiyun 	btrfs_update_delayed_refs_rsv(trans);
2957*4882a593Smuzhiyun 	return ret;
2958*4882a593Smuzhiyun }
2959*4882a593Smuzhiyun 
2960*4882a593Smuzhiyun /**
2961*4882a593Smuzhiyun  * btrfs_add_reserved_bytes - update the block_group and space info counters
2962*4882a593Smuzhiyun  * @cache:	The cache we are manipulating
2963*4882a593Smuzhiyun  * @ram_bytes:  The number of bytes of file content, and will be same to
2964*4882a593Smuzhiyun  *              @num_bytes except for the compress path.
2965*4882a593Smuzhiyun  * @num_bytes:	The number of bytes in question
2966*4882a593Smuzhiyun  * @delalloc:   The blocks are allocated for the delalloc write
2967*4882a593Smuzhiyun  *
2968*4882a593Smuzhiyun  * This is called by the allocator when it reserves space. If this is a
2969*4882a593Smuzhiyun  * reservation and the block group has become read only we cannot make the
2970*4882a593Smuzhiyun  * reservation and return -EAGAIN, otherwise this function always succeeds.
2971*4882a593Smuzhiyun  */
btrfs_add_reserved_bytes(struct btrfs_block_group * cache,u64 ram_bytes,u64 num_bytes,int delalloc)2972*4882a593Smuzhiyun int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
2973*4882a593Smuzhiyun 			     u64 ram_bytes, u64 num_bytes, int delalloc)
2974*4882a593Smuzhiyun {
2975*4882a593Smuzhiyun 	struct btrfs_space_info *space_info = cache->space_info;
2976*4882a593Smuzhiyun 	int ret = 0;
2977*4882a593Smuzhiyun 
2978*4882a593Smuzhiyun 	spin_lock(&space_info->lock);
2979*4882a593Smuzhiyun 	spin_lock(&cache->lock);
2980*4882a593Smuzhiyun 	if (cache->ro) {
2981*4882a593Smuzhiyun 		ret = -EAGAIN;
2982*4882a593Smuzhiyun 	} else {
2983*4882a593Smuzhiyun 		cache->reserved += num_bytes;
2984*4882a593Smuzhiyun 		space_info->bytes_reserved += num_bytes;
2985*4882a593Smuzhiyun 		trace_btrfs_space_reservation(cache->fs_info, "space_info",
2986*4882a593Smuzhiyun 					      space_info->flags, num_bytes, 1);
2987*4882a593Smuzhiyun 		btrfs_space_info_update_bytes_may_use(cache->fs_info,
2988*4882a593Smuzhiyun 						      space_info, -ram_bytes);
2989*4882a593Smuzhiyun 		if (delalloc)
2990*4882a593Smuzhiyun 			cache->delalloc_bytes += num_bytes;
2991*4882a593Smuzhiyun 
2992*4882a593Smuzhiyun 		/*
2993*4882a593Smuzhiyun 		 * Compression can use less space than we reserved, so wake
2994*4882a593Smuzhiyun 		 * tickets if that happens
2995*4882a593Smuzhiyun 		 */
2996*4882a593Smuzhiyun 		if (num_bytes < ram_bytes)
2997*4882a593Smuzhiyun 			btrfs_try_granting_tickets(cache->fs_info, space_info);
2998*4882a593Smuzhiyun 	}
2999*4882a593Smuzhiyun 	spin_unlock(&cache->lock);
3000*4882a593Smuzhiyun 	spin_unlock(&space_info->lock);
3001*4882a593Smuzhiyun 	return ret;
3002*4882a593Smuzhiyun }
3003*4882a593Smuzhiyun 
3004*4882a593Smuzhiyun /**
3005*4882a593Smuzhiyun  * btrfs_free_reserved_bytes - update the block_group and space info counters
3006*4882a593Smuzhiyun  * @cache:      The cache we are manipulating
3007*4882a593Smuzhiyun  * @num_bytes:  The number of bytes in question
3008*4882a593Smuzhiyun  * @delalloc:   The blocks are allocated for the delalloc write
3009*4882a593Smuzhiyun  *
3010*4882a593Smuzhiyun  * This is called by somebody who is freeing space that was never actually used
3011*4882a593Smuzhiyun  * on disk.  For example if you reserve some space for a new leaf in transaction
3012*4882a593Smuzhiyun  * A and before transaction A commits you free that leaf, you call this with
3013*4882a593Smuzhiyun  * reserve set to 0 in order to clear the reservation.
3014*4882a593Smuzhiyun  */
btrfs_free_reserved_bytes(struct btrfs_block_group * cache,u64 num_bytes,int delalloc)3015*4882a593Smuzhiyun void btrfs_free_reserved_bytes(struct btrfs_block_group *cache,
3016*4882a593Smuzhiyun 			       u64 num_bytes, int delalloc)
3017*4882a593Smuzhiyun {
3018*4882a593Smuzhiyun 	struct btrfs_space_info *space_info = cache->space_info;
3019*4882a593Smuzhiyun 
3020*4882a593Smuzhiyun 	spin_lock(&space_info->lock);
3021*4882a593Smuzhiyun 	spin_lock(&cache->lock);
3022*4882a593Smuzhiyun 	if (cache->ro)
3023*4882a593Smuzhiyun 		space_info->bytes_readonly += num_bytes;
3024*4882a593Smuzhiyun 	cache->reserved -= num_bytes;
3025*4882a593Smuzhiyun 	space_info->bytes_reserved -= num_bytes;
3026*4882a593Smuzhiyun 	space_info->max_extent_size = 0;
3027*4882a593Smuzhiyun 
3028*4882a593Smuzhiyun 	if (delalloc)
3029*4882a593Smuzhiyun 		cache->delalloc_bytes -= num_bytes;
3030*4882a593Smuzhiyun 	spin_unlock(&cache->lock);
3031*4882a593Smuzhiyun 
3032*4882a593Smuzhiyun 	btrfs_try_granting_tickets(cache->fs_info, space_info);
3033*4882a593Smuzhiyun 	spin_unlock(&space_info->lock);
3034*4882a593Smuzhiyun }
3035*4882a593Smuzhiyun 
force_metadata_allocation(struct btrfs_fs_info * info)3036*4882a593Smuzhiyun static void force_metadata_allocation(struct btrfs_fs_info *info)
3037*4882a593Smuzhiyun {
3038*4882a593Smuzhiyun 	struct list_head *head = &info->space_info;
3039*4882a593Smuzhiyun 	struct btrfs_space_info *found;
3040*4882a593Smuzhiyun 
3041*4882a593Smuzhiyun 	list_for_each_entry(found, head, list) {
3042*4882a593Smuzhiyun 		if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3043*4882a593Smuzhiyun 			found->force_alloc = CHUNK_ALLOC_FORCE;
3044*4882a593Smuzhiyun 	}
3045*4882a593Smuzhiyun }
3046*4882a593Smuzhiyun 
should_alloc_chunk(struct btrfs_fs_info * fs_info,struct btrfs_space_info * sinfo,int force)3047*4882a593Smuzhiyun static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
3048*4882a593Smuzhiyun 			      struct btrfs_space_info *sinfo, int force)
3049*4882a593Smuzhiyun {
3050*4882a593Smuzhiyun 	u64 bytes_used = btrfs_space_info_used(sinfo, false);
3051*4882a593Smuzhiyun 	u64 thresh;
3052*4882a593Smuzhiyun 
3053*4882a593Smuzhiyun 	if (force == CHUNK_ALLOC_FORCE)
3054*4882a593Smuzhiyun 		return 1;
3055*4882a593Smuzhiyun 
3056*4882a593Smuzhiyun 	/*
3057*4882a593Smuzhiyun 	 * in limited mode, we want to have some free space up to
3058*4882a593Smuzhiyun 	 * about 1% of the FS size.
3059*4882a593Smuzhiyun 	 */
3060*4882a593Smuzhiyun 	if (force == CHUNK_ALLOC_LIMITED) {
3061*4882a593Smuzhiyun 		thresh = btrfs_super_total_bytes(fs_info->super_copy);
3062*4882a593Smuzhiyun 		thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
3063*4882a593Smuzhiyun 
3064*4882a593Smuzhiyun 		if (sinfo->total_bytes - bytes_used < thresh)
3065*4882a593Smuzhiyun 			return 1;
3066*4882a593Smuzhiyun 	}
3067*4882a593Smuzhiyun 
3068*4882a593Smuzhiyun 	if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
3069*4882a593Smuzhiyun 		return 0;
3070*4882a593Smuzhiyun 	return 1;
3071*4882a593Smuzhiyun }
3072*4882a593Smuzhiyun 
btrfs_force_chunk_alloc(struct btrfs_trans_handle * trans,u64 type)3073*4882a593Smuzhiyun int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
3074*4882a593Smuzhiyun {
3075*4882a593Smuzhiyun 	u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type);
3076*4882a593Smuzhiyun 
3077*4882a593Smuzhiyun 	return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
3078*4882a593Smuzhiyun }
3079*4882a593Smuzhiyun 
3080*4882a593Smuzhiyun /*
3081*4882a593Smuzhiyun  * If force is CHUNK_ALLOC_FORCE:
3082*4882a593Smuzhiyun  *    - return 1 if it successfully allocates a chunk,
3083*4882a593Smuzhiyun  *    - return errors including -ENOSPC otherwise.
3084*4882a593Smuzhiyun  * If force is NOT CHUNK_ALLOC_FORCE:
3085*4882a593Smuzhiyun  *    - return 0 if it doesn't need to allocate a new chunk,
3086*4882a593Smuzhiyun  *    - return 1 if it successfully allocates a chunk,
3087*4882a593Smuzhiyun  *    - return errors including -ENOSPC otherwise.
3088*4882a593Smuzhiyun  */
btrfs_chunk_alloc(struct btrfs_trans_handle * trans,u64 flags,enum btrfs_chunk_alloc_enum force)3089*4882a593Smuzhiyun int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
3090*4882a593Smuzhiyun 		      enum btrfs_chunk_alloc_enum force)
3091*4882a593Smuzhiyun {
3092*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = trans->fs_info;
3093*4882a593Smuzhiyun 	struct btrfs_space_info *space_info;
3094*4882a593Smuzhiyun 	bool wait_for_alloc = false;
3095*4882a593Smuzhiyun 	bool should_alloc = false;
3096*4882a593Smuzhiyun 	int ret = 0;
3097*4882a593Smuzhiyun 
3098*4882a593Smuzhiyun 	/* Don't re-enter if we're already allocating a chunk */
3099*4882a593Smuzhiyun 	if (trans->allocating_chunk)
3100*4882a593Smuzhiyun 		return -ENOSPC;
3101*4882a593Smuzhiyun 
3102*4882a593Smuzhiyun 	space_info = btrfs_find_space_info(fs_info, flags);
3103*4882a593Smuzhiyun 	ASSERT(space_info);
3104*4882a593Smuzhiyun 
3105*4882a593Smuzhiyun 	do {
3106*4882a593Smuzhiyun 		spin_lock(&space_info->lock);
3107*4882a593Smuzhiyun 		if (force < space_info->force_alloc)
3108*4882a593Smuzhiyun 			force = space_info->force_alloc;
3109*4882a593Smuzhiyun 		should_alloc = should_alloc_chunk(fs_info, space_info, force);
3110*4882a593Smuzhiyun 		if (space_info->full) {
3111*4882a593Smuzhiyun 			/* No more free physical space */
3112*4882a593Smuzhiyun 			if (should_alloc)
3113*4882a593Smuzhiyun 				ret = -ENOSPC;
3114*4882a593Smuzhiyun 			else
3115*4882a593Smuzhiyun 				ret = 0;
3116*4882a593Smuzhiyun 			spin_unlock(&space_info->lock);
3117*4882a593Smuzhiyun 			return ret;
3118*4882a593Smuzhiyun 		} else if (!should_alloc) {
3119*4882a593Smuzhiyun 			spin_unlock(&space_info->lock);
3120*4882a593Smuzhiyun 			return 0;
3121*4882a593Smuzhiyun 		} else if (space_info->chunk_alloc) {
3122*4882a593Smuzhiyun 			/*
3123*4882a593Smuzhiyun 			 * Someone is already allocating, so we need to block
3124*4882a593Smuzhiyun 			 * until this someone is finished and then loop to
3125*4882a593Smuzhiyun 			 * recheck if we should continue with our allocation
3126*4882a593Smuzhiyun 			 * attempt.
3127*4882a593Smuzhiyun 			 */
3128*4882a593Smuzhiyun 			wait_for_alloc = true;
3129*4882a593Smuzhiyun 			force = CHUNK_ALLOC_NO_FORCE;
3130*4882a593Smuzhiyun 			spin_unlock(&space_info->lock);
3131*4882a593Smuzhiyun 			mutex_lock(&fs_info->chunk_mutex);
3132*4882a593Smuzhiyun 			mutex_unlock(&fs_info->chunk_mutex);
3133*4882a593Smuzhiyun 		} else {
3134*4882a593Smuzhiyun 			/* Proceed with allocation */
3135*4882a593Smuzhiyun 			space_info->chunk_alloc = 1;
3136*4882a593Smuzhiyun 			wait_for_alloc = false;
3137*4882a593Smuzhiyun 			spin_unlock(&space_info->lock);
3138*4882a593Smuzhiyun 		}
3139*4882a593Smuzhiyun 
3140*4882a593Smuzhiyun 		cond_resched();
3141*4882a593Smuzhiyun 	} while (wait_for_alloc);
3142*4882a593Smuzhiyun 
3143*4882a593Smuzhiyun 	mutex_lock(&fs_info->chunk_mutex);
3144*4882a593Smuzhiyun 	trans->allocating_chunk = true;
3145*4882a593Smuzhiyun 
3146*4882a593Smuzhiyun 	/*
3147*4882a593Smuzhiyun 	 * If we have mixed data/metadata chunks we want to make sure we keep
3148*4882a593Smuzhiyun 	 * allocating mixed chunks instead of individual chunks.
3149*4882a593Smuzhiyun 	 */
3150*4882a593Smuzhiyun 	if (btrfs_mixed_space_info(space_info))
3151*4882a593Smuzhiyun 		flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3152*4882a593Smuzhiyun 
3153*4882a593Smuzhiyun 	/*
3154*4882a593Smuzhiyun 	 * if we're doing a data chunk, go ahead and make sure that
3155*4882a593Smuzhiyun 	 * we keep a reasonable number of metadata chunks allocated in the
3156*4882a593Smuzhiyun 	 * FS as well.
3157*4882a593Smuzhiyun 	 */
3158*4882a593Smuzhiyun 	if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3159*4882a593Smuzhiyun 		fs_info->data_chunk_allocations++;
3160*4882a593Smuzhiyun 		if (!(fs_info->data_chunk_allocations %
3161*4882a593Smuzhiyun 		      fs_info->metadata_ratio))
3162*4882a593Smuzhiyun 			force_metadata_allocation(fs_info);
3163*4882a593Smuzhiyun 	}
3164*4882a593Smuzhiyun 
3165*4882a593Smuzhiyun 	/*
3166*4882a593Smuzhiyun 	 * Check if we have enough space in SYSTEM chunk because we may need
3167*4882a593Smuzhiyun 	 * to update devices.
3168*4882a593Smuzhiyun 	 */
3169*4882a593Smuzhiyun 	check_system_chunk(trans, flags);
3170*4882a593Smuzhiyun 
3171*4882a593Smuzhiyun 	ret = btrfs_alloc_chunk(trans, flags);
3172*4882a593Smuzhiyun 	trans->allocating_chunk = false;
3173*4882a593Smuzhiyun 
3174*4882a593Smuzhiyun 	spin_lock(&space_info->lock);
3175*4882a593Smuzhiyun 	if (ret < 0) {
3176*4882a593Smuzhiyun 		if (ret == -ENOSPC)
3177*4882a593Smuzhiyun 			space_info->full = 1;
3178*4882a593Smuzhiyun 		else
3179*4882a593Smuzhiyun 			goto out;
3180*4882a593Smuzhiyun 	} else {
3181*4882a593Smuzhiyun 		ret = 1;
3182*4882a593Smuzhiyun 		space_info->max_extent_size = 0;
3183*4882a593Smuzhiyun 	}
3184*4882a593Smuzhiyun 
3185*4882a593Smuzhiyun 	space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
3186*4882a593Smuzhiyun out:
3187*4882a593Smuzhiyun 	space_info->chunk_alloc = 0;
3188*4882a593Smuzhiyun 	spin_unlock(&space_info->lock);
3189*4882a593Smuzhiyun 	mutex_unlock(&fs_info->chunk_mutex);
3190*4882a593Smuzhiyun 	/*
3191*4882a593Smuzhiyun 	 * When we allocate a new chunk we reserve space in the chunk block
3192*4882a593Smuzhiyun 	 * reserve to make sure we can COW nodes/leafs in the chunk tree or
3193*4882a593Smuzhiyun 	 * add new nodes/leafs to it if we end up needing to do it when
3194*4882a593Smuzhiyun 	 * inserting the chunk item and updating device items as part of the
3195*4882a593Smuzhiyun 	 * second phase of chunk allocation, performed by
3196*4882a593Smuzhiyun 	 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
3197*4882a593Smuzhiyun 	 * large number of new block groups to create in our transaction
3198*4882a593Smuzhiyun 	 * handle's new_bgs list to avoid exhausting the chunk block reserve
3199*4882a593Smuzhiyun 	 * in extreme cases - like having a single transaction create many new
3200*4882a593Smuzhiyun 	 * block groups when starting to write out the free space caches of all
3201*4882a593Smuzhiyun 	 * the block groups that were made dirty during the lifetime of the
3202*4882a593Smuzhiyun 	 * transaction.
3203*4882a593Smuzhiyun 	 */
3204*4882a593Smuzhiyun 	if (trans->chunk_bytes_reserved >= (u64)SZ_2M)
3205*4882a593Smuzhiyun 		btrfs_create_pending_block_groups(trans);
3206*4882a593Smuzhiyun 
3207*4882a593Smuzhiyun 	return ret;
3208*4882a593Smuzhiyun }
3209*4882a593Smuzhiyun 
get_profile_num_devs(struct btrfs_fs_info * fs_info,u64 type)3210*4882a593Smuzhiyun static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
3211*4882a593Smuzhiyun {
3212*4882a593Smuzhiyun 	u64 num_dev;
3213*4882a593Smuzhiyun 
3214*4882a593Smuzhiyun 	num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max;
3215*4882a593Smuzhiyun 	if (!num_dev)
3216*4882a593Smuzhiyun 		num_dev = fs_info->fs_devices->rw_devices;
3217*4882a593Smuzhiyun 
3218*4882a593Smuzhiyun 	return num_dev;
3219*4882a593Smuzhiyun }
3220*4882a593Smuzhiyun 
3221*4882a593Smuzhiyun /*
3222*4882a593Smuzhiyun  * Reserve space in the system space for allocating or removing a chunk
3223*4882a593Smuzhiyun  */
check_system_chunk(struct btrfs_trans_handle * trans,u64 type)3224*4882a593Smuzhiyun void check_system_chunk(struct btrfs_trans_handle *trans, u64 type)
3225*4882a593Smuzhiyun {
3226*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = trans->fs_info;
3227*4882a593Smuzhiyun 	struct btrfs_space_info *info;
3228*4882a593Smuzhiyun 	u64 left;
3229*4882a593Smuzhiyun 	u64 thresh;
3230*4882a593Smuzhiyun 	int ret = 0;
3231*4882a593Smuzhiyun 	u64 num_devs;
3232*4882a593Smuzhiyun 
3233*4882a593Smuzhiyun 	/*
3234*4882a593Smuzhiyun 	 * Needed because we can end up allocating a system chunk and for an
3235*4882a593Smuzhiyun 	 * atomic and race free space reservation in the chunk block reserve.
3236*4882a593Smuzhiyun 	 */
3237*4882a593Smuzhiyun 	lockdep_assert_held(&fs_info->chunk_mutex);
3238*4882a593Smuzhiyun 
3239*4882a593Smuzhiyun 	info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3240*4882a593Smuzhiyun 	spin_lock(&info->lock);
3241*4882a593Smuzhiyun 	left = info->total_bytes - btrfs_space_info_used(info, true);
3242*4882a593Smuzhiyun 	spin_unlock(&info->lock);
3243*4882a593Smuzhiyun 
3244*4882a593Smuzhiyun 	num_devs = get_profile_num_devs(fs_info, type);
3245*4882a593Smuzhiyun 
3246*4882a593Smuzhiyun 	/* num_devs device items to update and 1 chunk item to add or remove */
3247*4882a593Smuzhiyun 	thresh = btrfs_calc_metadata_size(fs_info, num_devs) +
3248*4882a593Smuzhiyun 		btrfs_calc_insert_metadata_size(fs_info, 1);
3249*4882a593Smuzhiyun 
3250*4882a593Smuzhiyun 	if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
3251*4882a593Smuzhiyun 		btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
3252*4882a593Smuzhiyun 			   left, thresh, type);
3253*4882a593Smuzhiyun 		btrfs_dump_space_info(fs_info, info, 0, 0);
3254*4882a593Smuzhiyun 	}
3255*4882a593Smuzhiyun 
3256*4882a593Smuzhiyun 	if (left < thresh) {
3257*4882a593Smuzhiyun 		u64 flags = btrfs_system_alloc_profile(fs_info);
3258*4882a593Smuzhiyun 
3259*4882a593Smuzhiyun 		/*
3260*4882a593Smuzhiyun 		 * Ignore failure to create system chunk. We might end up not
3261*4882a593Smuzhiyun 		 * needing it, as we might not need to COW all nodes/leafs from
3262*4882a593Smuzhiyun 		 * the paths we visit in the chunk tree (they were already COWed
3263*4882a593Smuzhiyun 		 * or created in the current transaction for example).
3264*4882a593Smuzhiyun 		 */
3265*4882a593Smuzhiyun 		ret = btrfs_alloc_chunk(trans, flags);
3266*4882a593Smuzhiyun 	}
3267*4882a593Smuzhiyun 
3268*4882a593Smuzhiyun 	if (!ret) {
3269*4882a593Smuzhiyun 		ret = btrfs_block_rsv_add(fs_info->chunk_root,
3270*4882a593Smuzhiyun 					  &fs_info->chunk_block_rsv,
3271*4882a593Smuzhiyun 					  thresh, BTRFS_RESERVE_NO_FLUSH);
3272*4882a593Smuzhiyun 		if (!ret)
3273*4882a593Smuzhiyun 			trans->chunk_bytes_reserved += thresh;
3274*4882a593Smuzhiyun 	}
3275*4882a593Smuzhiyun }
3276*4882a593Smuzhiyun 
btrfs_put_block_group_cache(struct btrfs_fs_info * info)3277*4882a593Smuzhiyun void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
3278*4882a593Smuzhiyun {
3279*4882a593Smuzhiyun 	struct btrfs_block_group *block_group;
3280*4882a593Smuzhiyun 	u64 last = 0;
3281*4882a593Smuzhiyun 
3282*4882a593Smuzhiyun 	while (1) {
3283*4882a593Smuzhiyun 		struct inode *inode;
3284*4882a593Smuzhiyun 
3285*4882a593Smuzhiyun 		block_group = btrfs_lookup_first_block_group(info, last);
3286*4882a593Smuzhiyun 		while (block_group) {
3287*4882a593Smuzhiyun 			btrfs_wait_block_group_cache_done(block_group);
3288*4882a593Smuzhiyun 			spin_lock(&block_group->lock);
3289*4882a593Smuzhiyun 			if (block_group->iref)
3290*4882a593Smuzhiyun 				break;
3291*4882a593Smuzhiyun 			spin_unlock(&block_group->lock);
3292*4882a593Smuzhiyun 			block_group = btrfs_next_block_group(block_group);
3293*4882a593Smuzhiyun 		}
3294*4882a593Smuzhiyun 		if (!block_group) {
3295*4882a593Smuzhiyun 			if (last == 0)
3296*4882a593Smuzhiyun 				break;
3297*4882a593Smuzhiyun 			last = 0;
3298*4882a593Smuzhiyun 			continue;
3299*4882a593Smuzhiyun 		}
3300*4882a593Smuzhiyun 
3301*4882a593Smuzhiyun 		inode = block_group->inode;
3302*4882a593Smuzhiyun 		block_group->iref = 0;
3303*4882a593Smuzhiyun 		block_group->inode = NULL;
3304*4882a593Smuzhiyun 		spin_unlock(&block_group->lock);
3305*4882a593Smuzhiyun 		ASSERT(block_group->io_ctl.inode == NULL);
3306*4882a593Smuzhiyun 		iput(inode);
3307*4882a593Smuzhiyun 		last = block_group->start + block_group->length;
3308*4882a593Smuzhiyun 		btrfs_put_block_group(block_group);
3309*4882a593Smuzhiyun 	}
3310*4882a593Smuzhiyun }
3311*4882a593Smuzhiyun 
3312*4882a593Smuzhiyun /*
3313*4882a593Smuzhiyun  * Must be called only after stopping all workers, since we could have block
3314*4882a593Smuzhiyun  * group caching kthreads running, and therefore they could race with us if we
3315*4882a593Smuzhiyun  * freed the block groups before stopping them.
3316*4882a593Smuzhiyun  */
btrfs_free_block_groups(struct btrfs_fs_info * info)3317*4882a593Smuzhiyun int btrfs_free_block_groups(struct btrfs_fs_info *info)
3318*4882a593Smuzhiyun {
3319*4882a593Smuzhiyun 	struct btrfs_block_group *block_group;
3320*4882a593Smuzhiyun 	struct btrfs_space_info *space_info;
3321*4882a593Smuzhiyun 	struct btrfs_caching_control *caching_ctl;
3322*4882a593Smuzhiyun 	struct rb_node *n;
3323*4882a593Smuzhiyun 
3324*4882a593Smuzhiyun 	down_write(&info->commit_root_sem);
3325*4882a593Smuzhiyun 	while (!list_empty(&info->caching_block_groups)) {
3326*4882a593Smuzhiyun 		caching_ctl = list_entry(info->caching_block_groups.next,
3327*4882a593Smuzhiyun 					 struct btrfs_caching_control, list);
3328*4882a593Smuzhiyun 		list_del(&caching_ctl->list);
3329*4882a593Smuzhiyun 		btrfs_put_caching_control(caching_ctl);
3330*4882a593Smuzhiyun 	}
3331*4882a593Smuzhiyun 	up_write(&info->commit_root_sem);
3332*4882a593Smuzhiyun 
3333*4882a593Smuzhiyun 	spin_lock(&info->unused_bgs_lock);
3334*4882a593Smuzhiyun 	while (!list_empty(&info->unused_bgs)) {
3335*4882a593Smuzhiyun 		block_group = list_first_entry(&info->unused_bgs,
3336*4882a593Smuzhiyun 					       struct btrfs_block_group,
3337*4882a593Smuzhiyun 					       bg_list);
3338*4882a593Smuzhiyun 		list_del_init(&block_group->bg_list);
3339*4882a593Smuzhiyun 		btrfs_put_block_group(block_group);
3340*4882a593Smuzhiyun 	}
3341*4882a593Smuzhiyun 	spin_unlock(&info->unused_bgs_lock);
3342*4882a593Smuzhiyun 
3343*4882a593Smuzhiyun 	spin_lock(&info->block_group_cache_lock);
3344*4882a593Smuzhiyun 	while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
3345*4882a593Smuzhiyun 		block_group = rb_entry(n, struct btrfs_block_group,
3346*4882a593Smuzhiyun 				       cache_node);
3347*4882a593Smuzhiyun 		rb_erase(&block_group->cache_node,
3348*4882a593Smuzhiyun 			 &info->block_group_cache_tree);
3349*4882a593Smuzhiyun 		RB_CLEAR_NODE(&block_group->cache_node);
3350*4882a593Smuzhiyun 		spin_unlock(&info->block_group_cache_lock);
3351*4882a593Smuzhiyun 
3352*4882a593Smuzhiyun 		down_write(&block_group->space_info->groups_sem);
3353*4882a593Smuzhiyun 		list_del(&block_group->list);
3354*4882a593Smuzhiyun 		up_write(&block_group->space_info->groups_sem);
3355*4882a593Smuzhiyun 
3356*4882a593Smuzhiyun 		/*
3357*4882a593Smuzhiyun 		 * We haven't cached this block group, which means we could
3358*4882a593Smuzhiyun 		 * possibly have excluded extents on this block group.
3359*4882a593Smuzhiyun 		 */
3360*4882a593Smuzhiyun 		if (block_group->cached == BTRFS_CACHE_NO ||
3361*4882a593Smuzhiyun 		    block_group->cached == BTRFS_CACHE_ERROR)
3362*4882a593Smuzhiyun 			btrfs_free_excluded_extents(block_group);
3363*4882a593Smuzhiyun 
3364*4882a593Smuzhiyun 		btrfs_remove_free_space_cache(block_group);
3365*4882a593Smuzhiyun 		ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
3366*4882a593Smuzhiyun 		ASSERT(list_empty(&block_group->dirty_list));
3367*4882a593Smuzhiyun 		ASSERT(list_empty(&block_group->io_list));
3368*4882a593Smuzhiyun 		ASSERT(list_empty(&block_group->bg_list));
3369*4882a593Smuzhiyun 		ASSERT(refcount_read(&block_group->refs) == 1);
3370*4882a593Smuzhiyun 		ASSERT(block_group->swap_extents == 0);
3371*4882a593Smuzhiyun 		btrfs_put_block_group(block_group);
3372*4882a593Smuzhiyun 
3373*4882a593Smuzhiyun 		spin_lock(&info->block_group_cache_lock);
3374*4882a593Smuzhiyun 	}
3375*4882a593Smuzhiyun 	spin_unlock(&info->block_group_cache_lock);
3376*4882a593Smuzhiyun 
3377*4882a593Smuzhiyun 	btrfs_release_global_block_rsv(info);
3378*4882a593Smuzhiyun 
3379*4882a593Smuzhiyun 	while (!list_empty(&info->space_info)) {
3380*4882a593Smuzhiyun 		space_info = list_entry(info->space_info.next,
3381*4882a593Smuzhiyun 					struct btrfs_space_info,
3382*4882a593Smuzhiyun 					list);
3383*4882a593Smuzhiyun 
3384*4882a593Smuzhiyun 		/*
3385*4882a593Smuzhiyun 		 * Do not hide this behind enospc_debug, this is actually
3386*4882a593Smuzhiyun 		 * important and indicates a real bug if this happens.
3387*4882a593Smuzhiyun 		 */
3388*4882a593Smuzhiyun 		if (WARN_ON(space_info->bytes_pinned > 0 ||
3389*4882a593Smuzhiyun 			    space_info->bytes_reserved > 0 ||
3390*4882a593Smuzhiyun 			    space_info->bytes_may_use > 0))
3391*4882a593Smuzhiyun 			btrfs_dump_space_info(info, space_info, 0, 0);
3392*4882a593Smuzhiyun 		WARN_ON(space_info->reclaim_size > 0);
3393*4882a593Smuzhiyun 		list_del(&space_info->list);
3394*4882a593Smuzhiyun 		btrfs_sysfs_remove_space_info(space_info);
3395*4882a593Smuzhiyun 	}
3396*4882a593Smuzhiyun 	return 0;
3397*4882a593Smuzhiyun }
3398*4882a593Smuzhiyun 
btrfs_freeze_block_group(struct btrfs_block_group * cache)3399*4882a593Smuzhiyun void btrfs_freeze_block_group(struct btrfs_block_group *cache)
3400*4882a593Smuzhiyun {
3401*4882a593Smuzhiyun 	atomic_inc(&cache->frozen);
3402*4882a593Smuzhiyun }
3403*4882a593Smuzhiyun 
btrfs_unfreeze_block_group(struct btrfs_block_group * block_group)3404*4882a593Smuzhiyun void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
3405*4882a593Smuzhiyun {
3406*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info = block_group->fs_info;
3407*4882a593Smuzhiyun 	struct extent_map_tree *em_tree;
3408*4882a593Smuzhiyun 	struct extent_map *em;
3409*4882a593Smuzhiyun 	bool cleanup;
3410*4882a593Smuzhiyun 
3411*4882a593Smuzhiyun 	spin_lock(&block_group->lock);
3412*4882a593Smuzhiyun 	cleanup = (atomic_dec_and_test(&block_group->frozen) &&
3413*4882a593Smuzhiyun 		   block_group->removed);
3414*4882a593Smuzhiyun 	spin_unlock(&block_group->lock);
3415*4882a593Smuzhiyun 
3416*4882a593Smuzhiyun 	if (cleanup) {
3417*4882a593Smuzhiyun 		em_tree = &fs_info->mapping_tree;
3418*4882a593Smuzhiyun 		write_lock(&em_tree->lock);
3419*4882a593Smuzhiyun 		em = lookup_extent_mapping(em_tree, block_group->start,
3420*4882a593Smuzhiyun 					   1);
3421*4882a593Smuzhiyun 		BUG_ON(!em); /* logic error, can't happen */
3422*4882a593Smuzhiyun 		remove_extent_mapping(em_tree, em);
3423*4882a593Smuzhiyun 		write_unlock(&em_tree->lock);
3424*4882a593Smuzhiyun 
3425*4882a593Smuzhiyun 		/* once for us and once for the tree */
3426*4882a593Smuzhiyun 		free_extent_map(em);
3427*4882a593Smuzhiyun 		free_extent_map(em);
3428*4882a593Smuzhiyun 
3429*4882a593Smuzhiyun 		/*
3430*4882a593Smuzhiyun 		 * We may have left one free space entry and other possible
3431*4882a593Smuzhiyun 		 * tasks trimming this block group have left 1 entry each one.
3432*4882a593Smuzhiyun 		 * Free them if any.
3433*4882a593Smuzhiyun 		 */
3434*4882a593Smuzhiyun 		__btrfs_remove_free_space_cache(block_group->free_space_ctl);
3435*4882a593Smuzhiyun 	}
3436*4882a593Smuzhiyun }
3437*4882a593Smuzhiyun 
btrfs_inc_block_group_swap_extents(struct btrfs_block_group * bg)3438*4882a593Smuzhiyun bool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg)
3439*4882a593Smuzhiyun {
3440*4882a593Smuzhiyun 	bool ret = true;
3441*4882a593Smuzhiyun 
3442*4882a593Smuzhiyun 	spin_lock(&bg->lock);
3443*4882a593Smuzhiyun 	if (bg->ro)
3444*4882a593Smuzhiyun 		ret = false;
3445*4882a593Smuzhiyun 	else
3446*4882a593Smuzhiyun 		bg->swap_extents++;
3447*4882a593Smuzhiyun 	spin_unlock(&bg->lock);
3448*4882a593Smuzhiyun 
3449*4882a593Smuzhiyun 	return ret;
3450*4882a593Smuzhiyun }
3451*4882a593Smuzhiyun 
btrfs_dec_block_group_swap_extents(struct btrfs_block_group * bg,int amount)3452*4882a593Smuzhiyun void btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount)
3453*4882a593Smuzhiyun {
3454*4882a593Smuzhiyun 	spin_lock(&bg->lock);
3455*4882a593Smuzhiyun 	ASSERT(!bg->ro);
3456*4882a593Smuzhiyun 	ASSERT(bg->swap_extents >= amount);
3457*4882a593Smuzhiyun 	bg->swap_extents -= amount;
3458*4882a593Smuzhiyun 	spin_unlock(&bg->lock);
3459*4882a593Smuzhiyun }
3460