18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#include "misc.h"
48c2ecf20Sopenharmony_ci#include "ctree.h"
58c2ecf20Sopenharmony_ci#include "block-group.h"
68c2ecf20Sopenharmony_ci#include "space-info.h"
78c2ecf20Sopenharmony_ci#include "disk-io.h"
88c2ecf20Sopenharmony_ci#include "free-space-cache.h"
98c2ecf20Sopenharmony_ci#include "free-space-tree.h"
108c2ecf20Sopenharmony_ci#include "volumes.h"
118c2ecf20Sopenharmony_ci#include "transaction.h"
128c2ecf20Sopenharmony_ci#include "ref-verify.h"
138c2ecf20Sopenharmony_ci#include "sysfs.h"
148c2ecf20Sopenharmony_ci#include "tree-log.h"
158c2ecf20Sopenharmony_ci#include "delalloc-space.h"
168c2ecf20Sopenharmony_ci#include "discard.h"
178c2ecf20Sopenharmony_ci#include "raid56.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * Return target flags in extended format or 0 if restripe for this chunk_type
218c2ecf20Sopenharmony_ci * is not in progress
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * Should be called with balance_lock held
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_cistatic u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
288c2ecf20Sopenharmony_ci	u64 target = 0;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	if (!bctl)
318c2ecf20Sopenharmony_ci		return 0;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	if (flags & BTRFS_BLOCK_GROUP_DATA &&
348c2ecf20Sopenharmony_ci	    bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
358c2ecf20Sopenharmony_ci		target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
368c2ecf20Sopenharmony_ci	} else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
378c2ecf20Sopenharmony_ci		   bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
388c2ecf20Sopenharmony_ci		target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
398c2ecf20Sopenharmony_ci	} else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
408c2ecf20Sopenharmony_ci		   bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
418c2ecf20Sopenharmony_ci		target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
428c2ecf20Sopenharmony_ci	}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	return target;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/*
488c2ecf20Sopenharmony_ci * @flags: available profiles in extended format (see ctree.h)
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * Return reduced profile in chunk format.  If profile changing is in progress
518c2ecf20Sopenharmony_ci * (either running or paused) picks the target profile (if it's already
528c2ecf20Sopenharmony_ci * available), otherwise falls back to plain reducing.
538c2ecf20Sopenharmony_ci */
548c2ecf20Sopenharmony_cistatic u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	u64 num_devices = fs_info->fs_devices->rw_devices;
578c2ecf20Sopenharmony_ci	u64 target;
588c2ecf20Sopenharmony_ci	u64 raid_type;
598c2ecf20Sopenharmony_ci	u64 allowed = 0;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	/*
628c2ecf20Sopenharmony_ci	 * See if restripe for this chunk_type is in progress, if so try to
638c2ecf20Sopenharmony_ci	 * reduce to the target profile
648c2ecf20Sopenharmony_ci	 */
658c2ecf20Sopenharmony_ci	spin_lock(&fs_info->balance_lock);
668c2ecf20Sopenharmony_ci	target = get_restripe_target(fs_info, flags);
678c2ecf20Sopenharmony_ci	if (target) {
688c2ecf20Sopenharmony_ci		spin_unlock(&fs_info->balance_lock);
698c2ecf20Sopenharmony_ci		return extended_to_chunk(target);
708c2ecf20Sopenharmony_ci	}
718c2ecf20Sopenharmony_ci	spin_unlock(&fs_info->balance_lock);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	/* First, mask out the RAID levels which aren't possible */
748c2ecf20Sopenharmony_ci	for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
758c2ecf20Sopenharmony_ci		if (num_devices >= btrfs_raid_array[raid_type].devs_min)
768c2ecf20Sopenharmony_ci			allowed |= btrfs_raid_array[raid_type].bg_flag;
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci	allowed &= flags;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/* Select the highest-redundancy RAID level. */
818c2ecf20Sopenharmony_ci	if (allowed & BTRFS_BLOCK_GROUP_RAID1C4)
828c2ecf20Sopenharmony_ci		allowed = BTRFS_BLOCK_GROUP_RAID1C4;
838c2ecf20Sopenharmony_ci	else if (allowed & BTRFS_BLOCK_GROUP_RAID6)
848c2ecf20Sopenharmony_ci		allowed = BTRFS_BLOCK_GROUP_RAID6;
858c2ecf20Sopenharmony_ci	else if (allowed & BTRFS_BLOCK_GROUP_RAID1C3)
868c2ecf20Sopenharmony_ci		allowed = BTRFS_BLOCK_GROUP_RAID1C3;
878c2ecf20Sopenharmony_ci	else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
888c2ecf20Sopenharmony_ci		allowed = BTRFS_BLOCK_GROUP_RAID5;
898c2ecf20Sopenharmony_ci	else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
908c2ecf20Sopenharmony_ci		allowed = BTRFS_BLOCK_GROUP_RAID10;
918c2ecf20Sopenharmony_ci	else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
928c2ecf20Sopenharmony_ci		allowed = BTRFS_BLOCK_GROUP_RAID1;
938c2ecf20Sopenharmony_ci	else if (allowed & BTRFS_BLOCK_GROUP_DUP)
948c2ecf20Sopenharmony_ci		allowed = BTRFS_BLOCK_GROUP_DUP;
958c2ecf20Sopenharmony_ci	else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
968c2ecf20Sopenharmony_ci		allowed = BTRFS_BLOCK_GROUP_RAID0;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return extended_to_chunk(flags | allowed);
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciu64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	unsigned seq;
1068c2ecf20Sopenharmony_ci	u64 flags;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	do {
1098c2ecf20Sopenharmony_ci		flags = orig_flags;
1108c2ecf20Sopenharmony_ci		seq = read_seqbegin(&fs_info->profiles_lock);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci		if (flags & BTRFS_BLOCK_GROUP_DATA)
1138c2ecf20Sopenharmony_ci			flags |= fs_info->avail_data_alloc_bits;
1148c2ecf20Sopenharmony_ci		else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1158c2ecf20Sopenharmony_ci			flags |= fs_info->avail_system_alloc_bits;
1168c2ecf20Sopenharmony_ci		else if (flags & BTRFS_BLOCK_GROUP_METADATA)
1178c2ecf20Sopenharmony_ci			flags |= fs_info->avail_metadata_alloc_bits;
1188c2ecf20Sopenharmony_ci	} while (read_seqretry(&fs_info->profiles_lock, seq));
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	return btrfs_reduce_alloc_profile(fs_info, flags);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_civoid btrfs_get_block_group(struct btrfs_block_group *cache)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	refcount_inc(&cache->refs);
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_civoid btrfs_put_block_group(struct btrfs_block_group *cache)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	if (refcount_dec_and_test(&cache->refs)) {
1318c2ecf20Sopenharmony_ci		WARN_ON(cache->pinned > 0);
1328c2ecf20Sopenharmony_ci		WARN_ON(cache->reserved > 0);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		/*
1358c2ecf20Sopenharmony_ci		 * A block_group shouldn't be on the discard_list anymore.
1368c2ecf20Sopenharmony_ci		 * Remove the block_group from the discard_list to prevent us
1378c2ecf20Sopenharmony_ci		 * from causing a panic due to NULL pointer dereference.
1388c2ecf20Sopenharmony_ci		 */
1398c2ecf20Sopenharmony_ci		if (WARN_ON(!list_empty(&cache->discard_list)))
1408c2ecf20Sopenharmony_ci			btrfs_discard_cancel_work(&cache->fs_info->discard_ctl,
1418c2ecf20Sopenharmony_ci						  cache);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci		/*
1448c2ecf20Sopenharmony_ci		 * If not empty, someone is still holding mutex of
1458c2ecf20Sopenharmony_ci		 * full_stripe_lock, which can only be released by caller.
1468c2ecf20Sopenharmony_ci		 * And it will definitely cause use-after-free when caller
1478c2ecf20Sopenharmony_ci		 * tries to release full stripe lock.
1488c2ecf20Sopenharmony_ci		 *
1498c2ecf20Sopenharmony_ci		 * No better way to resolve, but only to warn.
1508c2ecf20Sopenharmony_ci		 */
1518c2ecf20Sopenharmony_ci		WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
1528c2ecf20Sopenharmony_ci		kfree(cache->free_space_ctl);
1538c2ecf20Sopenharmony_ci		kfree(cache);
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/*
1588c2ecf20Sopenharmony_ci * This adds the block group to the fs_info rb tree for the block group cache
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_cistatic int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
1618c2ecf20Sopenharmony_ci				       struct btrfs_block_group *block_group)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	struct rb_node **p;
1648c2ecf20Sopenharmony_ci	struct rb_node *parent = NULL;
1658c2ecf20Sopenharmony_ci	struct btrfs_block_group *cache;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	ASSERT(block_group->length != 0);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	spin_lock(&info->block_group_cache_lock);
1708c2ecf20Sopenharmony_ci	p = &info->block_group_cache_tree.rb_node;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	while (*p) {
1738c2ecf20Sopenharmony_ci		parent = *p;
1748c2ecf20Sopenharmony_ci		cache = rb_entry(parent, struct btrfs_block_group, cache_node);
1758c2ecf20Sopenharmony_ci		if (block_group->start < cache->start) {
1768c2ecf20Sopenharmony_ci			p = &(*p)->rb_left;
1778c2ecf20Sopenharmony_ci		} else if (block_group->start > cache->start) {
1788c2ecf20Sopenharmony_ci			p = &(*p)->rb_right;
1798c2ecf20Sopenharmony_ci		} else {
1808c2ecf20Sopenharmony_ci			spin_unlock(&info->block_group_cache_lock);
1818c2ecf20Sopenharmony_ci			return -EEXIST;
1828c2ecf20Sopenharmony_ci		}
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	rb_link_node(&block_group->cache_node, parent, p);
1868c2ecf20Sopenharmony_ci	rb_insert_color(&block_group->cache_node,
1878c2ecf20Sopenharmony_ci			&info->block_group_cache_tree);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	if (info->first_logical_byte > block_group->start)
1908c2ecf20Sopenharmony_ci		info->first_logical_byte = block_group->start;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	spin_unlock(&info->block_group_cache_lock);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return 0;
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/*
1988c2ecf20Sopenharmony_ci * This will return the block group at or after bytenr if contains is 0, else
1998c2ecf20Sopenharmony_ci * it will return the block group that contains the bytenr
2008c2ecf20Sopenharmony_ci */
2018c2ecf20Sopenharmony_cistatic struct btrfs_block_group *block_group_cache_tree_search(
2028c2ecf20Sopenharmony_ci		struct btrfs_fs_info *info, u64 bytenr, int contains)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	struct btrfs_block_group *cache, *ret = NULL;
2058c2ecf20Sopenharmony_ci	struct rb_node *n;
2068c2ecf20Sopenharmony_ci	u64 end, start;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	spin_lock(&info->block_group_cache_lock);
2098c2ecf20Sopenharmony_ci	n = info->block_group_cache_tree.rb_node;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	while (n) {
2128c2ecf20Sopenharmony_ci		cache = rb_entry(n, struct btrfs_block_group, cache_node);
2138c2ecf20Sopenharmony_ci		end = cache->start + cache->length - 1;
2148c2ecf20Sopenharmony_ci		start = cache->start;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci		if (bytenr < start) {
2178c2ecf20Sopenharmony_ci			if (!contains && (!ret || start < ret->start))
2188c2ecf20Sopenharmony_ci				ret = cache;
2198c2ecf20Sopenharmony_ci			n = n->rb_left;
2208c2ecf20Sopenharmony_ci		} else if (bytenr > start) {
2218c2ecf20Sopenharmony_ci			if (contains && bytenr <= end) {
2228c2ecf20Sopenharmony_ci				ret = cache;
2238c2ecf20Sopenharmony_ci				break;
2248c2ecf20Sopenharmony_ci			}
2258c2ecf20Sopenharmony_ci			n = n->rb_right;
2268c2ecf20Sopenharmony_ci		} else {
2278c2ecf20Sopenharmony_ci			ret = cache;
2288c2ecf20Sopenharmony_ci			break;
2298c2ecf20Sopenharmony_ci		}
2308c2ecf20Sopenharmony_ci	}
2318c2ecf20Sopenharmony_ci	if (ret) {
2328c2ecf20Sopenharmony_ci		btrfs_get_block_group(ret);
2338c2ecf20Sopenharmony_ci		if (bytenr == 0 && info->first_logical_byte > ret->start)
2348c2ecf20Sopenharmony_ci			info->first_logical_byte = ret->start;
2358c2ecf20Sopenharmony_ci	}
2368c2ecf20Sopenharmony_ci	spin_unlock(&info->block_group_cache_lock);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	return ret;
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci/*
2428c2ecf20Sopenharmony_ci * Return the block group that starts at or after bytenr
2438c2ecf20Sopenharmony_ci */
2448c2ecf20Sopenharmony_cistruct btrfs_block_group *btrfs_lookup_first_block_group(
2458c2ecf20Sopenharmony_ci		struct btrfs_fs_info *info, u64 bytenr)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	return block_group_cache_tree_search(info, bytenr, 0);
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci/*
2518c2ecf20Sopenharmony_ci * Return the block group that contains the given bytenr
2528c2ecf20Sopenharmony_ci */
2538c2ecf20Sopenharmony_cistruct btrfs_block_group *btrfs_lookup_block_group(
2548c2ecf20Sopenharmony_ci		struct btrfs_fs_info *info, u64 bytenr)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	return block_group_cache_tree_search(info, bytenr, 1);
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cistruct btrfs_block_group *btrfs_next_block_group(
2608c2ecf20Sopenharmony_ci		struct btrfs_block_group *cache)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = cache->fs_info;
2638c2ecf20Sopenharmony_ci	struct rb_node *node;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	spin_lock(&fs_info->block_group_cache_lock);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	/* If our block group was removed, we need a full search. */
2688c2ecf20Sopenharmony_ci	if (RB_EMPTY_NODE(&cache->cache_node)) {
2698c2ecf20Sopenharmony_ci		const u64 next_bytenr = cache->start + cache->length;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci		spin_unlock(&fs_info->block_group_cache_lock);
2728c2ecf20Sopenharmony_ci		btrfs_put_block_group(cache);
2738c2ecf20Sopenharmony_ci		cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci	node = rb_next(&cache->cache_node);
2768c2ecf20Sopenharmony_ci	btrfs_put_block_group(cache);
2778c2ecf20Sopenharmony_ci	if (node) {
2788c2ecf20Sopenharmony_ci		cache = rb_entry(node, struct btrfs_block_group, cache_node);
2798c2ecf20Sopenharmony_ci		btrfs_get_block_group(cache);
2808c2ecf20Sopenharmony_ci	} else
2818c2ecf20Sopenharmony_ci		cache = NULL;
2828c2ecf20Sopenharmony_ci	spin_unlock(&fs_info->block_group_cache_lock);
2838c2ecf20Sopenharmony_ci	return cache;
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cibool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	struct btrfs_block_group *bg;
2898c2ecf20Sopenharmony_ci	bool ret = true;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	bg = btrfs_lookup_block_group(fs_info, bytenr);
2928c2ecf20Sopenharmony_ci	if (!bg)
2938c2ecf20Sopenharmony_ci		return false;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	spin_lock(&bg->lock);
2968c2ecf20Sopenharmony_ci	if (bg->ro)
2978c2ecf20Sopenharmony_ci		ret = false;
2988c2ecf20Sopenharmony_ci	else
2998c2ecf20Sopenharmony_ci		atomic_inc(&bg->nocow_writers);
3008c2ecf20Sopenharmony_ci	spin_unlock(&bg->lock);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	/* No put on block group, done by btrfs_dec_nocow_writers */
3038c2ecf20Sopenharmony_ci	if (!ret)
3048c2ecf20Sopenharmony_ci		btrfs_put_block_group(bg);
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	return ret;
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_civoid btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	struct btrfs_block_group *bg;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	bg = btrfs_lookup_block_group(fs_info, bytenr);
3148c2ecf20Sopenharmony_ci	ASSERT(bg);
3158c2ecf20Sopenharmony_ci	if (atomic_dec_and_test(&bg->nocow_writers))
3168c2ecf20Sopenharmony_ci		wake_up_var(&bg->nocow_writers);
3178c2ecf20Sopenharmony_ci	/*
3188c2ecf20Sopenharmony_ci	 * Once for our lookup and once for the lookup done by a previous call
3198c2ecf20Sopenharmony_ci	 * to btrfs_inc_nocow_writers()
3208c2ecf20Sopenharmony_ci	 */
3218c2ecf20Sopenharmony_ci	btrfs_put_block_group(bg);
3228c2ecf20Sopenharmony_ci	btrfs_put_block_group(bg);
3238c2ecf20Sopenharmony_ci}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_civoid btrfs_wait_nocow_writers(struct btrfs_block_group *bg)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
3288c2ecf20Sopenharmony_ci}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_civoid btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
3318c2ecf20Sopenharmony_ci					const u64 start)
3328c2ecf20Sopenharmony_ci{
3338c2ecf20Sopenharmony_ci	struct btrfs_block_group *bg;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	bg = btrfs_lookup_block_group(fs_info, start);
3368c2ecf20Sopenharmony_ci	ASSERT(bg);
3378c2ecf20Sopenharmony_ci	if (atomic_dec_and_test(&bg->reservations))
3388c2ecf20Sopenharmony_ci		wake_up_var(&bg->reservations);
3398c2ecf20Sopenharmony_ci	btrfs_put_block_group(bg);
3408c2ecf20Sopenharmony_ci}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_civoid btrfs_wait_block_group_reservations(struct btrfs_block_group *bg)
3438c2ecf20Sopenharmony_ci{
3448c2ecf20Sopenharmony_ci	struct btrfs_space_info *space_info = bg->space_info;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	ASSERT(bg->ro);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
3498c2ecf20Sopenharmony_ci		return;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	/*
3528c2ecf20Sopenharmony_ci	 * Our block group is read only but before we set it to read only,
3538c2ecf20Sopenharmony_ci	 * some task might have had allocated an extent from it already, but it
3548c2ecf20Sopenharmony_ci	 * has not yet created a respective ordered extent (and added it to a
3558c2ecf20Sopenharmony_ci	 * root's list of ordered extents).
3568c2ecf20Sopenharmony_ci	 * Therefore wait for any task currently allocating extents, since the
3578c2ecf20Sopenharmony_ci	 * block group's reservations counter is incremented while a read lock
3588c2ecf20Sopenharmony_ci	 * on the groups' semaphore is held and decremented after releasing
3598c2ecf20Sopenharmony_ci	 * the read access on that semaphore and creating the ordered extent.
3608c2ecf20Sopenharmony_ci	 */
3618c2ecf20Sopenharmony_ci	down_write(&space_info->groups_sem);
3628c2ecf20Sopenharmony_ci	up_write(&space_info->groups_sem);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_cistruct btrfs_caching_control *btrfs_get_caching_control(
3688c2ecf20Sopenharmony_ci		struct btrfs_block_group *cache)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	struct btrfs_caching_control *ctl;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	spin_lock(&cache->lock);
3738c2ecf20Sopenharmony_ci	if (!cache->caching_ctl) {
3748c2ecf20Sopenharmony_ci		spin_unlock(&cache->lock);
3758c2ecf20Sopenharmony_ci		return NULL;
3768c2ecf20Sopenharmony_ci	}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	ctl = cache->caching_ctl;
3798c2ecf20Sopenharmony_ci	refcount_inc(&ctl->count);
3808c2ecf20Sopenharmony_ci	spin_unlock(&cache->lock);
3818c2ecf20Sopenharmony_ci	return ctl;
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_civoid btrfs_put_caching_control(struct btrfs_caching_control *ctl)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	if (refcount_dec_and_test(&ctl->count))
3878c2ecf20Sopenharmony_ci		kfree(ctl);
3888c2ecf20Sopenharmony_ci}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci/*
3918c2ecf20Sopenharmony_ci * When we wait for progress in the block group caching, its because our
3928c2ecf20Sopenharmony_ci * allocation attempt failed at least once.  So, we must sleep and let some
3938c2ecf20Sopenharmony_ci * progress happen before we try again.
3948c2ecf20Sopenharmony_ci *
3958c2ecf20Sopenharmony_ci * This function will sleep at least once waiting for new free space to show
3968c2ecf20Sopenharmony_ci * up, and then it will check the block group free space numbers for our min
3978c2ecf20Sopenharmony_ci * num_bytes.  Another option is to have it go ahead and look in the rbtree for
3988c2ecf20Sopenharmony_ci * a free extent of a given size, but this is a good start.
3998c2ecf20Sopenharmony_ci *
4008c2ecf20Sopenharmony_ci * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
4018c2ecf20Sopenharmony_ci * any of the information in this block group.
4028c2ecf20Sopenharmony_ci */
4038c2ecf20Sopenharmony_civoid btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache,
4048c2ecf20Sopenharmony_ci					   u64 num_bytes)
4058c2ecf20Sopenharmony_ci{
4068c2ecf20Sopenharmony_ci	struct btrfs_caching_control *caching_ctl;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	caching_ctl = btrfs_get_caching_control(cache);
4098c2ecf20Sopenharmony_ci	if (!caching_ctl)
4108c2ecf20Sopenharmony_ci		return;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	wait_event(caching_ctl->wait, btrfs_block_group_done(cache) ||
4138c2ecf20Sopenharmony_ci		   (cache->free_space_ctl->free_space >= num_bytes));
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	btrfs_put_caching_control(caching_ctl);
4168c2ecf20Sopenharmony_ci}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ciint btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache)
4198c2ecf20Sopenharmony_ci{
4208c2ecf20Sopenharmony_ci	struct btrfs_caching_control *caching_ctl;
4218c2ecf20Sopenharmony_ci	int ret = 0;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	caching_ctl = btrfs_get_caching_control(cache);
4248c2ecf20Sopenharmony_ci	if (!caching_ctl)
4258c2ecf20Sopenharmony_ci		return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	wait_event(caching_ctl->wait, btrfs_block_group_done(cache));
4288c2ecf20Sopenharmony_ci	if (cache->cached == BTRFS_CACHE_ERROR)
4298c2ecf20Sopenharmony_ci		ret = -EIO;
4308c2ecf20Sopenharmony_ci	btrfs_put_caching_control(caching_ctl);
4318c2ecf20Sopenharmony_ci	return ret;
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci#ifdef CONFIG_BTRFS_DEBUG
4358c2ecf20Sopenharmony_cistatic void fragment_free_space(struct btrfs_block_group *block_group)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = block_group->fs_info;
4388c2ecf20Sopenharmony_ci	u64 start = block_group->start;
4398c2ecf20Sopenharmony_ci	u64 len = block_group->length;
4408c2ecf20Sopenharmony_ci	u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
4418c2ecf20Sopenharmony_ci		fs_info->nodesize : fs_info->sectorsize;
4428c2ecf20Sopenharmony_ci	u64 step = chunk << 1;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	while (len > chunk) {
4458c2ecf20Sopenharmony_ci		btrfs_remove_free_space(block_group, start, chunk);
4468c2ecf20Sopenharmony_ci		start += step;
4478c2ecf20Sopenharmony_ci		if (len < step)
4488c2ecf20Sopenharmony_ci			len = 0;
4498c2ecf20Sopenharmony_ci		else
4508c2ecf20Sopenharmony_ci			len -= step;
4518c2ecf20Sopenharmony_ci	}
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci#endif
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci/*
4568c2ecf20Sopenharmony_ci * This is only called by btrfs_cache_block_group, since we could have freed
4578c2ecf20Sopenharmony_ci * extents we need to check the pinned_extents for any extents that can't be
4588c2ecf20Sopenharmony_ci * used yet since their free space will be released as soon as the transaction
4598c2ecf20Sopenharmony_ci * commits.
4608c2ecf20Sopenharmony_ci */
4618c2ecf20Sopenharmony_ciu64 add_new_free_space(struct btrfs_block_group *block_group, u64 start, u64 end)
4628c2ecf20Sopenharmony_ci{
4638c2ecf20Sopenharmony_ci	struct btrfs_fs_info *info = block_group->fs_info;
4648c2ecf20Sopenharmony_ci	u64 extent_start, extent_end, size, total_added = 0;
4658c2ecf20Sopenharmony_ci	int ret;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	while (start < end) {
4688c2ecf20Sopenharmony_ci		ret = find_first_extent_bit(&info->excluded_extents, start,
4698c2ecf20Sopenharmony_ci					    &extent_start, &extent_end,
4708c2ecf20Sopenharmony_ci					    EXTENT_DIRTY | EXTENT_UPTODATE,
4718c2ecf20Sopenharmony_ci					    NULL);
4728c2ecf20Sopenharmony_ci		if (ret)
4738c2ecf20Sopenharmony_ci			break;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci		if (extent_start <= start) {
4768c2ecf20Sopenharmony_ci			start = extent_end + 1;
4778c2ecf20Sopenharmony_ci		} else if (extent_start > start && extent_start < end) {
4788c2ecf20Sopenharmony_ci			size = extent_start - start;
4798c2ecf20Sopenharmony_ci			total_added += size;
4808c2ecf20Sopenharmony_ci			ret = btrfs_add_free_space_async_trimmed(block_group,
4818c2ecf20Sopenharmony_ci								 start, size);
4828c2ecf20Sopenharmony_ci			BUG_ON(ret); /* -ENOMEM or logic error */
4838c2ecf20Sopenharmony_ci			start = extent_end + 1;
4848c2ecf20Sopenharmony_ci		} else {
4858c2ecf20Sopenharmony_ci			break;
4868c2ecf20Sopenharmony_ci		}
4878c2ecf20Sopenharmony_ci	}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	if (start < end) {
4908c2ecf20Sopenharmony_ci		size = end - start;
4918c2ecf20Sopenharmony_ci		total_added += size;
4928c2ecf20Sopenharmony_ci		ret = btrfs_add_free_space_async_trimmed(block_group, start,
4938c2ecf20Sopenharmony_ci							 size);
4948c2ecf20Sopenharmony_ci		BUG_ON(ret); /* -ENOMEM or logic error */
4958c2ecf20Sopenharmony_ci	}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	return total_added;
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group = caching_ctl->block_group;
5038c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = block_group->fs_info;
5048c2ecf20Sopenharmony_ci	struct btrfs_root *extent_root = fs_info->extent_root;
5058c2ecf20Sopenharmony_ci	struct btrfs_path *path;
5068c2ecf20Sopenharmony_ci	struct extent_buffer *leaf;
5078c2ecf20Sopenharmony_ci	struct btrfs_key key;
5088c2ecf20Sopenharmony_ci	u64 total_found = 0;
5098c2ecf20Sopenharmony_ci	u64 last = 0;
5108c2ecf20Sopenharmony_ci	u32 nritems;
5118c2ecf20Sopenharmony_ci	int ret;
5128c2ecf20Sopenharmony_ci	bool wakeup = true;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
5158c2ecf20Sopenharmony_ci	if (!path)
5168c2ecf20Sopenharmony_ci		return -ENOMEM;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET);
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci#ifdef CONFIG_BTRFS_DEBUG
5218c2ecf20Sopenharmony_ci	/*
5228c2ecf20Sopenharmony_ci	 * If we're fragmenting we don't want to make anybody think we can
5238c2ecf20Sopenharmony_ci	 * allocate from this block group until we've had a chance to fragment
5248c2ecf20Sopenharmony_ci	 * the free space.
5258c2ecf20Sopenharmony_ci	 */
5268c2ecf20Sopenharmony_ci	if (btrfs_should_fragment_free_space(block_group))
5278c2ecf20Sopenharmony_ci		wakeup = false;
5288c2ecf20Sopenharmony_ci#endif
5298c2ecf20Sopenharmony_ci	/*
5308c2ecf20Sopenharmony_ci	 * We don't want to deadlock with somebody trying to allocate a new
5318c2ecf20Sopenharmony_ci	 * extent for the extent root while also trying to search the extent
5328c2ecf20Sopenharmony_ci	 * root to add free space.  So we skip locking and search the commit
5338c2ecf20Sopenharmony_ci	 * root, since its read-only
5348c2ecf20Sopenharmony_ci	 */
5358c2ecf20Sopenharmony_ci	path->skip_locking = 1;
5368c2ecf20Sopenharmony_ci	path->search_commit_root = 1;
5378c2ecf20Sopenharmony_ci	path->reada = READA_FORWARD;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	key.objectid = last;
5408c2ecf20Sopenharmony_ci	key.offset = 0;
5418c2ecf20Sopenharmony_ci	key.type = BTRFS_EXTENT_ITEM_KEY;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_cinext:
5448c2ecf20Sopenharmony_ci	ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
5458c2ecf20Sopenharmony_ci	if (ret < 0)
5468c2ecf20Sopenharmony_ci		goto out;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	leaf = path->nodes[0];
5498c2ecf20Sopenharmony_ci	nritems = btrfs_header_nritems(leaf);
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	while (1) {
5528c2ecf20Sopenharmony_ci		if (btrfs_fs_closing(fs_info) > 1) {
5538c2ecf20Sopenharmony_ci			last = (u64)-1;
5548c2ecf20Sopenharmony_ci			break;
5558c2ecf20Sopenharmony_ci		}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci		if (path->slots[0] < nritems) {
5588c2ecf20Sopenharmony_ci			btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5598c2ecf20Sopenharmony_ci		} else {
5608c2ecf20Sopenharmony_ci			ret = btrfs_find_next_key(extent_root, path, &key, 0, 0);
5618c2ecf20Sopenharmony_ci			if (ret)
5628c2ecf20Sopenharmony_ci				break;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci			if (need_resched() ||
5658c2ecf20Sopenharmony_ci			    rwsem_is_contended(&fs_info->commit_root_sem)) {
5668c2ecf20Sopenharmony_ci				if (wakeup)
5678c2ecf20Sopenharmony_ci					caching_ctl->progress = last;
5688c2ecf20Sopenharmony_ci				btrfs_release_path(path);
5698c2ecf20Sopenharmony_ci				up_read(&fs_info->commit_root_sem);
5708c2ecf20Sopenharmony_ci				mutex_unlock(&caching_ctl->mutex);
5718c2ecf20Sopenharmony_ci				cond_resched();
5728c2ecf20Sopenharmony_ci				mutex_lock(&caching_ctl->mutex);
5738c2ecf20Sopenharmony_ci				down_read(&fs_info->commit_root_sem);
5748c2ecf20Sopenharmony_ci				goto next;
5758c2ecf20Sopenharmony_ci			}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci			ret = btrfs_next_leaf(extent_root, path);
5788c2ecf20Sopenharmony_ci			if (ret < 0)
5798c2ecf20Sopenharmony_ci				goto out;
5808c2ecf20Sopenharmony_ci			if (ret)
5818c2ecf20Sopenharmony_ci				break;
5828c2ecf20Sopenharmony_ci			leaf = path->nodes[0];
5838c2ecf20Sopenharmony_ci			nritems = btrfs_header_nritems(leaf);
5848c2ecf20Sopenharmony_ci			continue;
5858c2ecf20Sopenharmony_ci		}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci		if (key.objectid < last) {
5888c2ecf20Sopenharmony_ci			key.objectid = last;
5898c2ecf20Sopenharmony_ci			key.offset = 0;
5908c2ecf20Sopenharmony_ci			key.type = BTRFS_EXTENT_ITEM_KEY;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci			if (wakeup)
5938c2ecf20Sopenharmony_ci				caching_ctl->progress = last;
5948c2ecf20Sopenharmony_ci			btrfs_release_path(path);
5958c2ecf20Sopenharmony_ci			goto next;
5968c2ecf20Sopenharmony_ci		}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci		if (key.objectid < block_group->start) {
5998c2ecf20Sopenharmony_ci			path->slots[0]++;
6008c2ecf20Sopenharmony_ci			continue;
6018c2ecf20Sopenharmony_ci		}
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci		if (key.objectid >= block_group->start + block_group->length)
6048c2ecf20Sopenharmony_ci			break;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
6078c2ecf20Sopenharmony_ci		    key.type == BTRFS_METADATA_ITEM_KEY) {
6088c2ecf20Sopenharmony_ci			total_found += add_new_free_space(block_group, last,
6098c2ecf20Sopenharmony_ci							  key.objectid);
6108c2ecf20Sopenharmony_ci			if (key.type == BTRFS_METADATA_ITEM_KEY)
6118c2ecf20Sopenharmony_ci				last = key.objectid +
6128c2ecf20Sopenharmony_ci					fs_info->nodesize;
6138c2ecf20Sopenharmony_ci			else
6148c2ecf20Sopenharmony_ci				last = key.objectid + key.offset;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci			if (total_found > CACHING_CTL_WAKE_UP) {
6178c2ecf20Sopenharmony_ci				total_found = 0;
6188c2ecf20Sopenharmony_ci				if (wakeup)
6198c2ecf20Sopenharmony_ci					wake_up(&caching_ctl->wait);
6208c2ecf20Sopenharmony_ci			}
6218c2ecf20Sopenharmony_ci		}
6228c2ecf20Sopenharmony_ci		path->slots[0]++;
6238c2ecf20Sopenharmony_ci	}
6248c2ecf20Sopenharmony_ci	ret = 0;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	total_found += add_new_free_space(block_group, last,
6278c2ecf20Sopenharmony_ci				block_group->start + block_group->length);
6288c2ecf20Sopenharmony_ci	caching_ctl->progress = (u64)-1;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ciout:
6318c2ecf20Sopenharmony_ci	btrfs_free_path(path);
6328c2ecf20Sopenharmony_ci	return ret;
6338c2ecf20Sopenharmony_ci}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_cistatic noinline void caching_thread(struct btrfs_work *work)
6368c2ecf20Sopenharmony_ci{
6378c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group;
6388c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info;
6398c2ecf20Sopenharmony_ci	struct btrfs_caching_control *caching_ctl;
6408c2ecf20Sopenharmony_ci	int ret;
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	caching_ctl = container_of(work, struct btrfs_caching_control, work);
6438c2ecf20Sopenharmony_ci	block_group = caching_ctl->block_group;
6448c2ecf20Sopenharmony_ci	fs_info = block_group->fs_info;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	mutex_lock(&caching_ctl->mutex);
6478c2ecf20Sopenharmony_ci	down_read(&fs_info->commit_root_sem);
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	/*
6508c2ecf20Sopenharmony_ci	 * If we are in the transaction that populated the free space tree we
6518c2ecf20Sopenharmony_ci	 * can't actually cache from the free space tree as our commit root and
6528c2ecf20Sopenharmony_ci	 * real root are the same, so we could change the contents of the blocks
6538c2ecf20Sopenharmony_ci	 * while caching.  Instead do the slow caching in this case, and after
6548c2ecf20Sopenharmony_ci	 * the transaction has committed we will be safe.
6558c2ecf20Sopenharmony_ci	 */
6568c2ecf20Sopenharmony_ci	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
6578c2ecf20Sopenharmony_ci	    !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags)))
6588c2ecf20Sopenharmony_ci		ret = load_free_space_tree(caching_ctl);
6598c2ecf20Sopenharmony_ci	else
6608c2ecf20Sopenharmony_ci		ret = load_extent_tree_free(caching_ctl);
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	spin_lock(&block_group->lock);
6638c2ecf20Sopenharmony_ci	block_group->caching_ctl = NULL;
6648c2ecf20Sopenharmony_ci	block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
6658c2ecf20Sopenharmony_ci	spin_unlock(&block_group->lock);
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci#ifdef CONFIG_BTRFS_DEBUG
6688c2ecf20Sopenharmony_ci	if (btrfs_should_fragment_free_space(block_group)) {
6698c2ecf20Sopenharmony_ci		u64 bytes_used;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci		spin_lock(&block_group->space_info->lock);
6728c2ecf20Sopenharmony_ci		spin_lock(&block_group->lock);
6738c2ecf20Sopenharmony_ci		bytes_used = block_group->length - block_group->used;
6748c2ecf20Sopenharmony_ci		block_group->space_info->bytes_used += bytes_used >> 1;
6758c2ecf20Sopenharmony_ci		spin_unlock(&block_group->lock);
6768c2ecf20Sopenharmony_ci		spin_unlock(&block_group->space_info->lock);
6778c2ecf20Sopenharmony_ci		fragment_free_space(block_group);
6788c2ecf20Sopenharmony_ci	}
6798c2ecf20Sopenharmony_ci#endif
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	caching_ctl->progress = (u64)-1;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	up_read(&fs_info->commit_root_sem);
6848c2ecf20Sopenharmony_ci	btrfs_free_excluded_extents(block_group);
6858c2ecf20Sopenharmony_ci	mutex_unlock(&caching_ctl->mutex);
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	wake_up(&caching_ctl->wait);
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	btrfs_put_caching_control(caching_ctl);
6908c2ecf20Sopenharmony_ci	btrfs_put_block_group(block_group);
6918c2ecf20Sopenharmony_ci}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ciint btrfs_cache_block_group(struct btrfs_block_group *cache, int load_cache_only)
6948c2ecf20Sopenharmony_ci{
6958c2ecf20Sopenharmony_ci	DEFINE_WAIT(wait);
6968c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = cache->fs_info;
6978c2ecf20Sopenharmony_ci	struct btrfs_caching_control *caching_ctl;
6988c2ecf20Sopenharmony_ci	int ret = 0;
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
7018c2ecf20Sopenharmony_ci	if (!caching_ctl)
7028c2ecf20Sopenharmony_ci		return -ENOMEM;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&caching_ctl->list);
7058c2ecf20Sopenharmony_ci	mutex_init(&caching_ctl->mutex);
7068c2ecf20Sopenharmony_ci	init_waitqueue_head(&caching_ctl->wait);
7078c2ecf20Sopenharmony_ci	caching_ctl->block_group = cache;
7088c2ecf20Sopenharmony_ci	caching_ctl->progress = cache->start;
7098c2ecf20Sopenharmony_ci	refcount_set(&caching_ctl->count, 1);
7108c2ecf20Sopenharmony_ci	btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	spin_lock(&cache->lock);
7138c2ecf20Sopenharmony_ci	/*
7148c2ecf20Sopenharmony_ci	 * This should be a rare occasion, but this could happen I think in the
7158c2ecf20Sopenharmony_ci	 * case where one thread starts to load the space cache info, and then
7168c2ecf20Sopenharmony_ci	 * some other thread starts a transaction commit which tries to do an
7178c2ecf20Sopenharmony_ci	 * allocation while the other thread is still loading the space cache
7188c2ecf20Sopenharmony_ci	 * info.  The previous loop should have kept us from choosing this block
7198c2ecf20Sopenharmony_ci	 * group, but if we've moved to the state where we will wait on caching
7208c2ecf20Sopenharmony_ci	 * block groups we need to first check if we're doing a fast load here,
7218c2ecf20Sopenharmony_ci	 * so we can wait for it to finish, otherwise we could end up allocating
7228c2ecf20Sopenharmony_ci	 * from a block group who's cache gets evicted for one reason or
7238c2ecf20Sopenharmony_ci	 * another.
7248c2ecf20Sopenharmony_ci	 */
7258c2ecf20Sopenharmony_ci	while (cache->cached == BTRFS_CACHE_FAST) {
7268c2ecf20Sopenharmony_ci		struct btrfs_caching_control *ctl;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci		ctl = cache->caching_ctl;
7298c2ecf20Sopenharmony_ci		refcount_inc(&ctl->count);
7308c2ecf20Sopenharmony_ci		prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
7318c2ecf20Sopenharmony_ci		spin_unlock(&cache->lock);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci		schedule();
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci		finish_wait(&ctl->wait, &wait);
7368c2ecf20Sopenharmony_ci		btrfs_put_caching_control(ctl);
7378c2ecf20Sopenharmony_ci		spin_lock(&cache->lock);
7388c2ecf20Sopenharmony_ci	}
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	if (cache->cached != BTRFS_CACHE_NO) {
7418c2ecf20Sopenharmony_ci		spin_unlock(&cache->lock);
7428c2ecf20Sopenharmony_ci		kfree(caching_ctl);
7438c2ecf20Sopenharmony_ci		return 0;
7448c2ecf20Sopenharmony_ci	}
7458c2ecf20Sopenharmony_ci	WARN_ON(cache->caching_ctl);
7468c2ecf20Sopenharmony_ci	cache->caching_ctl = caching_ctl;
7478c2ecf20Sopenharmony_ci	cache->cached = BTRFS_CACHE_FAST;
7488c2ecf20Sopenharmony_ci	spin_unlock(&cache->lock);
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
7518c2ecf20Sopenharmony_ci		mutex_lock(&caching_ctl->mutex);
7528c2ecf20Sopenharmony_ci		ret = load_free_space_cache(cache);
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci		spin_lock(&cache->lock);
7558c2ecf20Sopenharmony_ci		if (ret == 1) {
7568c2ecf20Sopenharmony_ci			cache->caching_ctl = NULL;
7578c2ecf20Sopenharmony_ci			cache->cached = BTRFS_CACHE_FINISHED;
7588c2ecf20Sopenharmony_ci			cache->last_byte_to_unpin = (u64)-1;
7598c2ecf20Sopenharmony_ci			caching_ctl->progress = (u64)-1;
7608c2ecf20Sopenharmony_ci		} else {
7618c2ecf20Sopenharmony_ci			if (load_cache_only) {
7628c2ecf20Sopenharmony_ci				cache->caching_ctl = NULL;
7638c2ecf20Sopenharmony_ci				cache->cached = BTRFS_CACHE_NO;
7648c2ecf20Sopenharmony_ci			} else {
7658c2ecf20Sopenharmony_ci				cache->cached = BTRFS_CACHE_STARTED;
7668c2ecf20Sopenharmony_ci				cache->has_caching_ctl = 1;
7678c2ecf20Sopenharmony_ci			}
7688c2ecf20Sopenharmony_ci		}
7698c2ecf20Sopenharmony_ci		spin_unlock(&cache->lock);
7708c2ecf20Sopenharmony_ci#ifdef CONFIG_BTRFS_DEBUG
7718c2ecf20Sopenharmony_ci		if (ret == 1 &&
7728c2ecf20Sopenharmony_ci		    btrfs_should_fragment_free_space(cache)) {
7738c2ecf20Sopenharmony_ci			u64 bytes_used;
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci			spin_lock(&cache->space_info->lock);
7768c2ecf20Sopenharmony_ci			spin_lock(&cache->lock);
7778c2ecf20Sopenharmony_ci			bytes_used = cache->length - cache->used;
7788c2ecf20Sopenharmony_ci			cache->space_info->bytes_used += bytes_used >> 1;
7798c2ecf20Sopenharmony_ci			spin_unlock(&cache->lock);
7808c2ecf20Sopenharmony_ci			spin_unlock(&cache->space_info->lock);
7818c2ecf20Sopenharmony_ci			fragment_free_space(cache);
7828c2ecf20Sopenharmony_ci		}
7838c2ecf20Sopenharmony_ci#endif
7848c2ecf20Sopenharmony_ci		mutex_unlock(&caching_ctl->mutex);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci		wake_up(&caching_ctl->wait);
7878c2ecf20Sopenharmony_ci		if (ret == 1) {
7888c2ecf20Sopenharmony_ci			btrfs_put_caching_control(caching_ctl);
7898c2ecf20Sopenharmony_ci			btrfs_free_excluded_extents(cache);
7908c2ecf20Sopenharmony_ci			return 0;
7918c2ecf20Sopenharmony_ci		}
7928c2ecf20Sopenharmony_ci	} else {
7938c2ecf20Sopenharmony_ci		/*
7948c2ecf20Sopenharmony_ci		 * We're either using the free space tree or no caching at all.
7958c2ecf20Sopenharmony_ci		 * Set cached to the appropriate value and wakeup any waiters.
7968c2ecf20Sopenharmony_ci		 */
7978c2ecf20Sopenharmony_ci		spin_lock(&cache->lock);
7988c2ecf20Sopenharmony_ci		if (load_cache_only) {
7998c2ecf20Sopenharmony_ci			cache->caching_ctl = NULL;
8008c2ecf20Sopenharmony_ci			cache->cached = BTRFS_CACHE_NO;
8018c2ecf20Sopenharmony_ci		} else {
8028c2ecf20Sopenharmony_ci			cache->cached = BTRFS_CACHE_STARTED;
8038c2ecf20Sopenharmony_ci			cache->has_caching_ctl = 1;
8048c2ecf20Sopenharmony_ci		}
8058c2ecf20Sopenharmony_ci		spin_unlock(&cache->lock);
8068c2ecf20Sopenharmony_ci		wake_up(&caching_ctl->wait);
8078c2ecf20Sopenharmony_ci	}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	if (load_cache_only) {
8108c2ecf20Sopenharmony_ci		btrfs_put_caching_control(caching_ctl);
8118c2ecf20Sopenharmony_ci		return 0;
8128c2ecf20Sopenharmony_ci	}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	down_write(&fs_info->commit_root_sem);
8158c2ecf20Sopenharmony_ci	refcount_inc(&caching_ctl->count);
8168c2ecf20Sopenharmony_ci	list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
8178c2ecf20Sopenharmony_ci	up_write(&fs_info->commit_root_sem);
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	btrfs_get_block_group(cache);
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	return ret;
8248c2ecf20Sopenharmony_ci}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_cistatic void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8278c2ecf20Sopenharmony_ci{
8288c2ecf20Sopenharmony_ci	u64 extra_flags = chunk_to_extended(flags) &
8298c2ecf20Sopenharmony_ci				BTRFS_EXTENDED_PROFILE_MASK;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	write_seqlock(&fs_info->profiles_lock);
8328c2ecf20Sopenharmony_ci	if (flags & BTRFS_BLOCK_GROUP_DATA)
8338c2ecf20Sopenharmony_ci		fs_info->avail_data_alloc_bits &= ~extra_flags;
8348c2ecf20Sopenharmony_ci	if (flags & BTRFS_BLOCK_GROUP_METADATA)
8358c2ecf20Sopenharmony_ci		fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8368c2ecf20Sopenharmony_ci	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8378c2ecf20Sopenharmony_ci		fs_info->avail_system_alloc_bits &= ~extra_flags;
8388c2ecf20Sopenharmony_ci	write_sequnlock(&fs_info->profiles_lock);
8398c2ecf20Sopenharmony_ci}
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci/*
8428c2ecf20Sopenharmony_ci * Clear incompat bits for the following feature(s):
8438c2ecf20Sopenharmony_ci *
8448c2ecf20Sopenharmony_ci * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
8458c2ecf20Sopenharmony_ci *            in the whole filesystem
8468c2ecf20Sopenharmony_ci *
8478c2ecf20Sopenharmony_ci * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
8488c2ecf20Sopenharmony_ci */
8498c2ecf20Sopenharmony_cistatic void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags)
8508c2ecf20Sopenharmony_ci{
8518c2ecf20Sopenharmony_ci	bool found_raid56 = false;
8528c2ecf20Sopenharmony_ci	bool found_raid1c34 = false;
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) ||
8558c2ecf20Sopenharmony_ci	    (flags & BTRFS_BLOCK_GROUP_RAID1C3) ||
8568c2ecf20Sopenharmony_ci	    (flags & BTRFS_BLOCK_GROUP_RAID1C4)) {
8578c2ecf20Sopenharmony_ci		struct list_head *head = &fs_info->space_info;
8588c2ecf20Sopenharmony_ci		struct btrfs_space_info *sinfo;
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci		list_for_each_entry_rcu(sinfo, head, list) {
8618c2ecf20Sopenharmony_ci			down_read(&sinfo->groups_sem);
8628c2ecf20Sopenharmony_ci			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5]))
8638c2ecf20Sopenharmony_ci				found_raid56 = true;
8648c2ecf20Sopenharmony_ci			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6]))
8658c2ecf20Sopenharmony_ci				found_raid56 = true;
8668c2ecf20Sopenharmony_ci			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3]))
8678c2ecf20Sopenharmony_ci				found_raid1c34 = true;
8688c2ecf20Sopenharmony_ci			if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4]))
8698c2ecf20Sopenharmony_ci				found_raid1c34 = true;
8708c2ecf20Sopenharmony_ci			up_read(&sinfo->groups_sem);
8718c2ecf20Sopenharmony_ci		}
8728c2ecf20Sopenharmony_ci		if (!found_raid56)
8738c2ecf20Sopenharmony_ci			btrfs_clear_fs_incompat(fs_info, RAID56);
8748c2ecf20Sopenharmony_ci		if (!found_raid1c34)
8758c2ecf20Sopenharmony_ci			btrfs_clear_fs_incompat(fs_info, RAID1C34);
8768c2ecf20Sopenharmony_ci	}
8778c2ecf20Sopenharmony_ci}
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_cistatic int remove_block_group_item(struct btrfs_trans_handle *trans,
8808c2ecf20Sopenharmony_ci				   struct btrfs_path *path,
8818c2ecf20Sopenharmony_ci				   struct btrfs_block_group *block_group)
8828c2ecf20Sopenharmony_ci{
8838c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
8848c2ecf20Sopenharmony_ci	struct btrfs_root *root;
8858c2ecf20Sopenharmony_ci	struct btrfs_key key;
8868c2ecf20Sopenharmony_ci	int ret;
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	root = fs_info->extent_root;
8898c2ecf20Sopenharmony_ci	key.objectid = block_group->start;
8908c2ecf20Sopenharmony_ci	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8918c2ecf20Sopenharmony_ci	key.offset = block_group->length;
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8948c2ecf20Sopenharmony_ci	if (ret > 0)
8958c2ecf20Sopenharmony_ci		ret = -ENOENT;
8968c2ecf20Sopenharmony_ci	if (ret < 0)
8978c2ecf20Sopenharmony_ci		return ret;
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	ret = btrfs_del_item(trans, root, path);
9008c2ecf20Sopenharmony_ci	return ret;
9018c2ecf20Sopenharmony_ci}
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ciint btrfs_remove_block_group(struct btrfs_trans_handle *trans,
9048c2ecf20Sopenharmony_ci			     u64 group_start, struct extent_map *em)
9058c2ecf20Sopenharmony_ci{
9068c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
9078c2ecf20Sopenharmony_ci	struct btrfs_path *path;
9088c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group;
9098c2ecf20Sopenharmony_ci	struct btrfs_free_cluster *cluster;
9108c2ecf20Sopenharmony_ci	struct btrfs_root *tree_root = fs_info->tree_root;
9118c2ecf20Sopenharmony_ci	struct btrfs_key key;
9128c2ecf20Sopenharmony_ci	struct inode *inode;
9138c2ecf20Sopenharmony_ci	struct kobject *kobj = NULL;
9148c2ecf20Sopenharmony_ci	int ret;
9158c2ecf20Sopenharmony_ci	int index;
9168c2ecf20Sopenharmony_ci	int factor;
9178c2ecf20Sopenharmony_ci	struct btrfs_caching_control *caching_ctl = NULL;
9188c2ecf20Sopenharmony_ci	bool remove_em;
9198c2ecf20Sopenharmony_ci	bool remove_rsv = false;
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	block_group = btrfs_lookup_block_group(fs_info, group_start);
9228c2ecf20Sopenharmony_ci	BUG_ON(!block_group);
9238c2ecf20Sopenharmony_ci	BUG_ON(!block_group->ro);
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	trace_btrfs_remove_block_group(block_group);
9268c2ecf20Sopenharmony_ci	/*
9278c2ecf20Sopenharmony_ci	 * Free the reserved super bytes from this block group before
9288c2ecf20Sopenharmony_ci	 * remove it.
9298c2ecf20Sopenharmony_ci	 */
9308c2ecf20Sopenharmony_ci	btrfs_free_excluded_extents(block_group);
9318c2ecf20Sopenharmony_ci	btrfs_free_ref_tree_range(fs_info, block_group->start,
9328c2ecf20Sopenharmony_ci				  block_group->length);
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	index = btrfs_bg_flags_to_raid_index(block_group->flags);
9358c2ecf20Sopenharmony_ci	factor = btrfs_bg_type_to_factor(block_group->flags);
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	/* make sure this block group isn't part of an allocation cluster */
9388c2ecf20Sopenharmony_ci	cluster = &fs_info->data_alloc_cluster;
9398c2ecf20Sopenharmony_ci	spin_lock(&cluster->refill_lock);
9408c2ecf20Sopenharmony_ci	btrfs_return_cluster_to_free_space(block_group, cluster);
9418c2ecf20Sopenharmony_ci	spin_unlock(&cluster->refill_lock);
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	/*
9448c2ecf20Sopenharmony_ci	 * make sure this block group isn't part of a metadata
9458c2ecf20Sopenharmony_ci	 * allocation cluster
9468c2ecf20Sopenharmony_ci	 */
9478c2ecf20Sopenharmony_ci	cluster = &fs_info->meta_alloc_cluster;
9488c2ecf20Sopenharmony_ci	spin_lock(&cluster->refill_lock);
9498c2ecf20Sopenharmony_ci	btrfs_return_cluster_to_free_space(block_group, cluster);
9508c2ecf20Sopenharmony_ci	spin_unlock(&cluster->refill_lock);
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
9538c2ecf20Sopenharmony_ci	if (!path) {
9548c2ecf20Sopenharmony_ci		ret = -ENOMEM;
9558c2ecf20Sopenharmony_ci		goto out;
9568c2ecf20Sopenharmony_ci	}
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	/*
9598c2ecf20Sopenharmony_ci	 * get the inode first so any iput calls done for the io_list
9608c2ecf20Sopenharmony_ci	 * aren't the final iput (no unlinks allowed now)
9618c2ecf20Sopenharmony_ci	 */
9628c2ecf20Sopenharmony_ci	inode = lookup_free_space_inode(block_group, path);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	mutex_lock(&trans->transaction->cache_write_mutex);
9658c2ecf20Sopenharmony_ci	/*
9668c2ecf20Sopenharmony_ci	 * Make sure our free space cache IO is done before removing the
9678c2ecf20Sopenharmony_ci	 * free space inode
9688c2ecf20Sopenharmony_ci	 */
9698c2ecf20Sopenharmony_ci	spin_lock(&trans->transaction->dirty_bgs_lock);
9708c2ecf20Sopenharmony_ci	if (!list_empty(&block_group->io_list)) {
9718c2ecf20Sopenharmony_ci		list_del_init(&block_group->io_list);
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci		WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci		spin_unlock(&trans->transaction->dirty_bgs_lock);
9768c2ecf20Sopenharmony_ci		btrfs_wait_cache_io(trans, block_group, path);
9778c2ecf20Sopenharmony_ci		btrfs_put_block_group(block_group);
9788c2ecf20Sopenharmony_ci		spin_lock(&trans->transaction->dirty_bgs_lock);
9798c2ecf20Sopenharmony_ci	}
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	if (!list_empty(&block_group->dirty_list)) {
9828c2ecf20Sopenharmony_ci		list_del_init(&block_group->dirty_list);
9838c2ecf20Sopenharmony_ci		remove_rsv = true;
9848c2ecf20Sopenharmony_ci		btrfs_put_block_group(block_group);
9858c2ecf20Sopenharmony_ci	}
9868c2ecf20Sopenharmony_ci	spin_unlock(&trans->transaction->dirty_bgs_lock);
9878c2ecf20Sopenharmony_ci	mutex_unlock(&trans->transaction->cache_write_mutex);
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	if (!IS_ERR(inode)) {
9908c2ecf20Sopenharmony_ci		ret = btrfs_orphan_add(trans, BTRFS_I(inode));
9918c2ecf20Sopenharmony_ci		if (ret) {
9928c2ecf20Sopenharmony_ci			btrfs_add_delayed_iput(inode);
9938c2ecf20Sopenharmony_ci			goto out;
9948c2ecf20Sopenharmony_ci		}
9958c2ecf20Sopenharmony_ci		clear_nlink(inode);
9968c2ecf20Sopenharmony_ci		/* One for the block groups ref */
9978c2ecf20Sopenharmony_ci		spin_lock(&block_group->lock);
9988c2ecf20Sopenharmony_ci		if (block_group->iref) {
9998c2ecf20Sopenharmony_ci			block_group->iref = 0;
10008c2ecf20Sopenharmony_ci			block_group->inode = NULL;
10018c2ecf20Sopenharmony_ci			spin_unlock(&block_group->lock);
10028c2ecf20Sopenharmony_ci			iput(inode);
10038c2ecf20Sopenharmony_ci		} else {
10048c2ecf20Sopenharmony_ci			spin_unlock(&block_group->lock);
10058c2ecf20Sopenharmony_ci		}
10068c2ecf20Sopenharmony_ci		/* One for our lookup ref */
10078c2ecf20Sopenharmony_ci		btrfs_add_delayed_iput(inode);
10088c2ecf20Sopenharmony_ci	}
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	key.objectid = BTRFS_FREE_SPACE_OBJECTID;
10118c2ecf20Sopenharmony_ci	key.type = 0;
10128c2ecf20Sopenharmony_ci	key.offset = block_group->start;
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
10158c2ecf20Sopenharmony_ci	if (ret < 0)
10168c2ecf20Sopenharmony_ci		goto out;
10178c2ecf20Sopenharmony_ci	if (ret > 0)
10188c2ecf20Sopenharmony_ci		btrfs_release_path(path);
10198c2ecf20Sopenharmony_ci	if (ret == 0) {
10208c2ecf20Sopenharmony_ci		ret = btrfs_del_item(trans, tree_root, path);
10218c2ecf20Sopenharmony_ci		if (ret)
10228c2ecf20Sopenharmony_ci			goto out;
10238c2ecf20Sopenharmony_ci		btrfs_release_path(path);
10248c2ecf20Sopenharmony_ci	}
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	spin_lock(&fs_info->block_group_cache_lock);
10278c2ecf20Sopenharmony_ci	rb_erase(&block_group->cache_node,
10288c2ecf20Sopenharmony_ci		 &fs_info->block_group_cache_tree);
10298c2ecf20Sopenharmony_ci	RB_CLEAR_NODE(&block_group->cache_node);
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci	/* Once for the block groups rbtree */
10328c2ecf20Sopenharmony_ci	btrfs_put_block_group(block_group);
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci	if (fs_info->first_logical_byte == block_group->start)
10358c2ecf20Sopenharmony_ci		fs_info->first_logical_byte = (u64)-1;
10368c2ecf20Sopenharmony_ci	spin_unlock(&fs_info->block_group_cache_lock);
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci	down_write(&block_group->space_info->groups_sem);
10398c2ecf20Sopenharmony_ci	/*
10408c2ecf20Sopenharmony_ci	 * we must use list_del_init so people can check to see if they
10418c2ecf20Sopenharmony_ci	 * are still on the list after taking the semaphore
10428c2ecf20Sopenharmony_ci	 */
10438c2ecf20Sopenharmony_ci	list_del_init(&block_group->list);
10448c2ecf20Sopenharmony_ci	if (list_empty(&block_group->space_info->block_groups[index])) {
10458c2ecf20Sopenharmony_ci		kobj = block_group->space_info->block_group_kobjs[index];
10468c2ecf20Sopenharmony_ci		block_group->space_info->block_group_kobjs[index] = NULL;
10478c2ecf20Sopenharmony_ci		clear_avail_alloc_bits(fs_info, block_group->flags);
10488c2ecf20Sopenharmony_ci	}
10498c2ecf20Sopenharmony_ci	up_write(&block_group->space_info->groups_sem);
10508c2ecf20Sopenharmony_ci	clear_incompat_bg_bits(fs_info, block_group->flags);
10518c2ecf20Sopenharmony_ci	if (kobj) {
10528c2ecf20Sopenharmony_ci		kobject_del(kobj);
10538c2ecf20Sopenharmony_ci		kobject_put(kobj);
10548c2ecf20Sopenharmony_ci	}
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	if (block_group->has_caching_ctl)
10578c2ecf20Sopenharmony_ci		caching_ctl = btrfs_get_caching_control(block_group);
10588c2ecf20Sopenharmony_ci	if (block_group->cached == BTRFS_CACHE_STARTED)
10598c2ecf20Sopenharmony_ci		btrfs_wait_block_group_cache_done(block_group);
10608c2ecf20Sopenharmony_ci	if (block_group->has_caching_ctl) {
10618c2ecf20Sopenharmony_ci		down_write(&fs_info->commit_root_sem);
10628c2ecf20Sopenharmony_ci		if (!caching_ctl) {
10638c2ecf20Sopenharmony_ci			struct btrfs_caching_control *ctl;
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci			list_for_each_entry(ctl,
10668c2ecf20Sopenharmony_ci				    &fs_info->caching_block_groups, list)
10678c2ecf20Sopenharmony_ci				if (ctl->block_group == block_group) {
10688c2ecf20Sopenharmony_ci					caching_ctl = ctl;
10698c2ecf20Sopenharmony_ci					refcount_inc(&caching_ctl->count);
10708c2ecf20Sopenharmony_ci					break;
10718c2ecf20Sopenharmony_ci				}
10728c2ecf20Sopenharmony_ci		}
10738c2ecf20Sopenharmony_ci		if (caching_ctl)
10748c2ecf20Sopenharmony_ci			list_del_init(&caching_ctl->list);
10758c2ecf20Sopenharmony_ci		up_write(&fs_info->commit_root_sem);
10768c2ecf20Sopenharmony_ci		if (caching_ctl) {
10778c2ecf20Sopenharmony_ci			/* Once for the caching bgs list and once for us. */
10788c2ecf20Sopenharmony_ci			btrfs_put_caching_control(caching_ctl);
10798c2ecf20Sopenharmony_ci			btrfs_put_caching_control(caching_ctl);
10808c2ecf20Sopenharmony_ci		}
10818c2ecf20Sopenharmony_ci	}
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci	spin_lock(&trans->transaction->dirty_bgs_lock);
10848c2ecf20Sopenharmony_ci	WARN_ON(!list_empty(&block_group->dirty_list));
10858c2ecf20Sopenharmony_ci	WARN_ON(!list_empty(&block_group->io_list));
10868c2ecf20Sopenharmony_ci	spin_unlock(&trans->transaction->dirty_bgs_lock);
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci	btrfs_remove_free_space_cache(block_group);
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	spin_lock(&block_group->space_info->lock);
10918c2ecf20Sopenharmony_ci	list_del_init(&block_group->ro_list);
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
10948c2ecf20Sopenharmony_ci		WARN_ON(block_group->space_info->total_bytes
10958c2ecf20Sopenharmony_ci			< block_group->length);
10968c2ecf20Sopenharmony_ci		WARN_ON(block_group->space_info->bytes_readonly
10978c2ecf20Sopenharmony_ci			< block_group->length);
10988c2ecf20Sopenharmony_ci		WARN_ON(block_group->space_info->disk_total
10998c2ecf20Sopenharmony_ci			< block_group->length * factor);
11008c2ecf20Sopenharmony_ci	}
11018c2ecf20Sopenharmony_ci	block_group->space_info->total_bytes -= block_group->length;
11028c2ecf20Sopenharmony_ci	block_group->space_info->bytes_readonly -= block_group->length;
11038c2ecf20Sopenharmony_ci	block_group->space_info->disk_total -= block_group->length * factor;
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	spin_unlock(&block_group->space_info->lock);
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci	/*
11088c2ecf20Sopenharmony_ci	 * Remove the free space for the block group from the free space tree
11098c2ecf20Sopenharmony_ci	 * and the block group's item from the extent tree before marking the
11108c2ecf20Sopenharmony_ci	 * block group as removed. This is to prevent races with tasks that
11118c2ecf20Sopenharmony_ci	 * freeze and unfreeze a block group, this task and another task
11128c2ecf20Sopenharmony_ci	 * allocating a new block group - the unfreeze task ends up removing
11138c2ecf20Sopenharmony_ci	 * the block group's extent map before the task calling this function
11148c2ecf20Sopenharmony_ci	 * deletes the block group item from the extent tree, allowing for
11158c2ecf20Sopenharmony_ci	 * another task to attempt to create another block group with the same
11168c2ecf20Sopenharmony_ci	 * item key (and failing with -EEXIST and a transaction abort).
11178c2ecf20Sopenharmony_ci	 */
11188c2ecf20Sopenharmony_ci	ret = remove_block_group_free_space(trans, block_group);
11198c2ecf20Sopenharmony_ci	if (ret)
11208c2ecf20Sopenharmony_ci		goto out;
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci	ret = remove_block_group_item(trans, path, block_group);
11238c2ecf20Sopenharmony_ci	if (ret < 0)
11248c2ecf20Sopenharmony_ci		goto out;
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	spin_lock(&block_group->lock);
11278c2ecf20Sopenharmony_ci	block_group->removed = 1;
11288c2ecf20Sopenharmony_ci	/*
11298c2ecf20Sopenharmony_ci	 * At this point trimming or scrub can't start on this block group,
11308c2ecf20Sopenharmony_ci	 * because we removed the block group from the rbtree
11318c2ecf20Sopenharmony_ci	 * fs_info->block_group_cache_tree so no one can't find it anymore and
11328c2ecf20Sopenharmony_ci	 * even if someone already got this block group before we removed it
11338c2ecf20Sopenharmony_ci	 * from the rbtree, they have already incremented block_group->frozen -
11348c2ecf20Sopenharmony_ci	 * if they didn't, for the trimming case they won't find any free space
11358c2ecf20Sopenharmony_ci	 * entries because we already removed them all when we called
11368c2ecf20Sopenharmony_ci	 * btrfs_remove_free_space_cache().
11378c2ecf20Sopenharmony_ci	 *
11388c2ecf20Sopenharmony_ci	 * And we must not remove the extent map from the fs_info->mapping_tree
11398c2ecf20Sopenharmony_ci	 * to prevent the same logical address range and physical device space
11408c2ecf20Sopenharmony_ci	 * ranges from being reused for a new block group. This is needed to
11418c2ecf20Sopenharmony_ci	 * avoid races with trimming and scrub.
11428c2ecf20Sopenharmony_ci	 *
11438c2ecf20Sopenharmony_ci	 * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
11448c2ecf20Sopenharmony_ci	 * completely transactionless, so while it is trimming a range the
11458c2ecf20Sopenharmony_ci	 * currently running transaction might finish and a new one start,
11468c2ecf20Sopenharmony_ci	 * allowing for new block groups to be created that can reuse the same
11478c2ecf20Sopenharmony_ci	 * physical device locations unless we take this special care.
11488c2ecf20Sopenharmony_ci	 *
11498c2ecf20Sopenharmony_ci	 * There may also be an implicit trim operation if the file system
11508c2ecf20Sopenharmony_ci	 * is mounted with -odiscard. The same protections must remain
11518c2ecf20Sopenharmony_ci	 * in place until the extents have been discarded completely when
11528c2ecf20Sopenharmony_ci	 * the transaction commit has completed.
11538c2ecf20Sopenharmony_ci	 */
11548c2ecf20Sopenharmony_ci	remove_em = (atomic_read(&block_group->frozen) == 0);
11558c2ecf20Sopenharmony_ci	spin_unlock(&block_group->lock);
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	if (remove_em) {
11588c2ecf20Sopenharmony_ci		struct extent_map_tree *em_tree;
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci		em_tree = &fs_info->mapping_tree;
11618c2ecf20Sopenharmony_ci		write_lock(&em_tree->lock);
11628c2ecf20Sopenharmony_ci		remove_extent_mapping(em_tree, em);
11638c2ecf20Sopenharmony_ci		write_unlock(&em_tree->lock);
11648c2ecf20Sopenharmony_ci		/* once for the tree */
11658c2ecf20Sopenharmony_ci		free_extent_map(em);
11668c2ecf20Sopenharmony_ci	}
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ciout:
11698c2ecf20Sopenharmony_ci	/* Once for the lookup reference */
11708c2ecf20Sopenharmony_ci	btrfs_put_block_group(block_group);
11718c2ecf20Sopenharmony_ci	if (remove_rsv)
11728c2ecf20Sopenharmony_ci		btrfs_delayed_refs_rsv_release(fs_info, 1);
11738c2ecf20Sopenharmony_ci	btrfs_free_path(path);
11748c2ecf20Sopenharmony_ci	return ret;
11758c2ecf20Sopenharmony_ci}
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_cistruct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
11788c2ecf20Sopenharmony_ci		struct btrfs_fs_info *fs_info, const u64 chunk_offset)
11798c2ecf20Sopenharmony_ci{
11808c2ecf20Sopenharmony_ci	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
11818c2ecf20Sopenharmony_ci	struct extent_map *em;
11828c2ecf20Sopenharmony_ci	struct map_lookup *map;
11838c2ecf20Sopenharmony_ci	unsigned int num_items;
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	read_lock(&em_tree->lock);
11868c2ecf20Sopenharmony_ci	em = lookup_extent_mapping(em_tree, chunk_offset, 1);
11878c2ecf20Sopenharmony_ci	read_unlock(&em_tree->lock);
11888c2ecf20Sopenharmony_ci	ASSERT(em && em->start == chunk_offset);
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	/*
11918c2ecf20Sopenharmony_ci	 * We need to reserve 3 + N units from the metadata space info in order
11928c2ecf20Sopenharmony_ci	 * to remove a block group (done at btrfs_remove_chunk() and at
11938c2ecf20Sopenharmony_ci	 * btrfs_remove_block_group()), which are used for:
11948c2ecf20Sopenharmony_ci	 *
11958c2ecf20Sopenharmony_ci	 * 1 unit for adding the free space inode's orphan (located in the tree
11968c2ecf20Sopenharmony_ci	 * of tree roots).
11978c2ecf20Sopenharmony_ci	 * 1 unit for deleting the block group item (located in the extent
11988c2ecf20Sopenharmony_ci	 * tree).
11998c2ecf20Sopenharmony_ci	 * 1 unit for deleting the free space item (located in tree of tree
12008c2ecf20Sopenharmony_ci	 * roots).
12018c2ecf20Sopenharmony_ci	 * N units for deleting N device extent items corresponding to each
12028c2ecf20Sopenharmony_ci	 * stripe (located in the device tree).
12038c2ecf20Sopenharmony_ci	 *
12048c2ecf20Sopenharmony_ci	 * In order to remove a block group we also need to reserve units in the
12058c2ecf20Sopenharmony_ci	 * system space info in order to update the chunk tree (update one or
12068c2ecf20Sopenharmony_ci	 * more device items and remove one chunk item), but this is done at
12078c2ecf20Sopenharmony_ci	 * btrfs_remove_chunk() through a call to check_system_chunk().
12088c2ecf20Sopenharmony_ci	 */
12098c2ecf20Sopenharmony_ci	map = em->map_lookup;
12108c2ecf20Sopenharmony_ci	num_items = 3 + map->num_stripes;
12118c2ecf20Sopenharmony_ci	free_extent_map(em);
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
12148c2ecf20Sopenharmony_ci							   num_items);
12158c2ecf20Sopenharmony_ci}
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci/*
12188c2ecf20Sopenharmony_ci * Mark block group @cache read-only, so later write won't happen to block
12198c2ecf20Sopenharmony_ci * group @cache.
12208c2ecf20Sopenharmony_ci *
12218c2ecf20Sopenharmony_ci * If @force is not set, this function will only mark the block group readonly
12228c2ecf20Sopenharmony_ci * if we have enough free space (1M) in other metadata/system block groups.
12238c2ecf20Sopenharmony_ci * If @force is not set, this function will mark the block group readonly
12248c2ecf20Sopenharmony_ci * without checking free space.
12258c2ecf20Sopenharmony_ci *
12268c2ecf20Sopenharmony_ci * NOTE: This function doesn't care if other block groups can contain all the
12278c2ecf20Sopenharmony_ci * data in this block group. That check should be done by relocation routine,
12288c2ecf20Sopenharmony_ci * not this function.
12298c2ecf20Sopenharmony_ci */
12308c2ecf20Sopenharmony_cistatic int inc_block_group_ro(struct btrfs_block_group *cache, int force)
12318c2ecf20Sopenharmony_ci{
12328c2ecf20Sopenharmony_ci	struct btrfs_space_info *sinfo = cache->space_info;
12338c2ecf20Sopenharmony_ci	u64 num_bytes;
12348c2ecf20Sopenharmony_ci	int ret = -ENOSPC;
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	spin_lock(&sinfo->lock);
12378c2ecf20Sopenharmony_ci	spin_lock(&cache->lock);
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	if (cache->swap_extents) {
12408c2ecf20Sopenharmony_ci		ret = -ETXTBSY;
12418c2ecf20Sopenharmony_ci		goto out;
12428c2ecf20Sopenharmony_ci	}
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci	if (cache->ro) {
12458c2ecf20Sopenharmony_ci		cache->ro++;
12468c2ecf20Sopenharmony_ci		ret = 0;
12478c2ecf20Sopenharmony_ci		goto out;
12488c2ecf20Sopenharmony_ci	}
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	num_bytes = cache->length - cache->reserved - cache->pinned -
12518c2ecf20Sopenharmony_ci		    cache->bytes_super - cache->used;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	/*
12548c2ecf20Sopenharmony_ci	 * Data never overcommits, even in mixed mode, so do just the straight
12558c2ecf20Sopenharmony_ci	 * check of left over space in how much we have allocated.
12568c2ecf20Sopenharmony_ci	 */
12578c2ecf20Sopenharmony_ci	if (force) {
12588c2ecf20Sopenharmony_ci		ret = 0;
12598c2ecf20Sopenharmony_ci	} else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) {
12608c2ecf20Sopenharmony_ci		u64 sinfo_used = btrfs_space_info_used(sinfo, true);
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci		/*
12638c2ecf20Sopenharmony_ci		 * Here we make sure if we mark this bg RO, we still have enough
12648c2ecf20Sopenharmony_ci		 * free space as buffer.
12658c2ecf20Sopenharmony_ci		 */
12668c2ecf20Sopenharmony_ci		if (sinfo_used + num_bytes <= sinfo->total_bytes)
12678c2ecf20Sopenharmony_ci			ret = 0;
12688c2ecf20Sopenharmony_ci	} else {
12698c2ecf20Sopenharmony_ci		/*
12708c2ecf20Sopenharmony_ci		 * We overcommit metadata, so we need to do the
12718c2ecf20Sopenharmony_ci		 * btrfs_can_overcommit check here, and we need to pass in
12728c2ecf20Sopenharmony_ci		 * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of
12738c2ecf20Sopenharmony_ci		 * leeway to allow us to mark this block group as read only.
12748c2ecf20Sopenharmony_ci		 */
12758c2ecf20Sopenharmony_ci		if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes,
12768c2ecf20Sopenharmony_ci					 BTRFS_RESERVE_NO_FLUSH))
12778c2ecf20Sopenharmony_ci			ret = 0;
12788c2ecf20Sopenharmony_ci	}
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	if (!ret) {
12818c2ecf20Sopenharmony_ci		sinfo->bytes_readonly += num_bytes;
12828c2ecf20Sopenharmony_ci		cache->ro++;
12838c2ecf20Sopenharmony_ci		list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
12848c2ecf20Sopenharmony_ci	}
12858c2ecf20Sopenharmony_ciout:
12868c2ecf20Sopenharmony_ci	spin_unlock(&cache->lock);
12878c2ecf20Sopenharmony_ci	spin_unlock(&sinfo->lock);
12888c2ecf20Sopenharmony_ci	if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) {
12898c2ecf20Sopenharmony_ci		btrfs_info(cache->fs_info,
12908c2ecf20Sopenharmony_ci			"unable to make block group %llu ro", cache->start);
12918c2ecf20Sopenharmony_ci		btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0);
12928c2ecf20Sopenharmony_ci	}
12938c2ecf20Sopenharmony_ci	return ret;
12948c2ecf20Sopenharmony_ci}
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_cistatic bool clean_pinned_extents(struct btrfs_trans_handle *trans,
12978c2ecf20Sopenharmony_ci				 struct btrfs_block_group *bg)
12988c2ecf20Sopenharmony_ci{
12998c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = bg->fs_info;
13008c2ecf20Sopenharmony_ci	struct btrfs_transaction *prev_trans = NULL;
13018c2ecf20Sopenharmony_ci	const u64 start = bg->start;
13028c2ecf20Sopenharmony_ci	const u64 end = start + bg->length - 1;
13038c2ecf20Sopenharmony_ci	int ret;
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	spin_lock(&fs_info->trans_lock);
13068c2ecf20Sopenharmony_ci	if (trans->transaction->list.prev != &fs_info->trans_list) {
13078c2ecf20Sopenharmony_ci		prev_trans = list_last_entry(&trans->transaction->list,
13088c2ecf20Sopenharmony_ci					     struct btrfs_transaction, list);
13098c2ecf20Sopenharmony_ci		refcount_inc(&prev_trans->use_count);
13108c2ecf20Sopenharmony_ci	}
13118c2ecf20Sopenharmony_ci	spin_unlock(&fs_info->trans_lock);
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci	/*
13148c2ecf20Sopenharmony_ci	 * Hold the unused_bg_unpin_mutex lock to avoid racing with
13158c2ecf20Sopenharmony_ci	 * btrfs_finish_extent_commit(). If we are at transaction N, another
13168c2ecf20Sopenharmony_ci	 * task might be running finish_extent_commit() for the previous
13178c2ecf20Sopenharmony_ci	 * transaction N - 1, and have seen a range belonging to the block
13188c2ecf20Sopenharmony_ci	 * group in pinned_extents before we were able to clear the whole block
13198c2ecf20Sopenharmony_ci	 * group range from pinned_extents. This means that task can lookup for
13208c2ecf20Sopenharmony_ci	 * the block group after we unpinned it from pinned_extents and removed
13218c2ecf20Sopenharmony_ci	 * it, leading to a BUG_ON() at unpin_extent_range().
13228c2ecf20Sopenharmony_ci	 */
13238c2ecf20Sopenharmony_ci	mutex_lock(&fs_info->unused_bg_unpin_mutex);
13248c2ecf20Sopenharmony_ci	if (prev_trans) {
13258c2ecf20Sopenharmony_ci		ret = clear_extent_bits(&prev_trans->pinned_extents, start, end,
13268c2ecf20Sopenharmony_ci					EXTENT_DIRTY);
13278c2ecf20Sopenharmony_ci		if (ret)
13288c2ecf20Sopenharmony_ci			goto out;
13298c2ecf20Sopenharmony_ci	}
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ci	ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end,
13328c2ecf20Sopenharmony_ci				EXTENT_DIRTY);
13338c2ecf20Sopenharmony_ciout:
13348c2ecf20Sopenharmony_ci	mutex_unlock(&fs_info->unused_bg_unpin_mutex);
13358c2ecf20Sopenharmony_ci	if (prev_trans)
13368c2ecf20Sopenharmony_ci		btrfs_put_transaction(prev_trans);
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_ci	return ret == 0;
13398c2ecf20Sopenharmony_ci}
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci/*
13428c2ecf20Sopenharmony_ci * Process the unused_bgs list and remove any that don't have any allocated
13438c2ecf20Sopenharmony_ci * space inside of them.
13448c2ecf20Sopenharmony_ci */
13458c2ecf20Sopenharmony_civoid btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
13468c2ecf20Sopenharmony_ci{
13478c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group;
13488c2ecf20Sopenharmony_ci	struct btrfs_space_info *space_info;
13498c2ecf20Sopenharmony_ci	struct btrfs_trans_handle *trans;
13508c2ecf20Sopenharmony_ci	const bool async_trim_enabled = btrfs_test_opt(fs_info, DISCARD_ASYNC);
13518c2ecf20Sopenharmony_ci	int ret = 0;
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
13548c2ecf20Sopenharmony_ci		return;
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci	spin_lock(&fs_info->unused_bgs_lock);
13578c2ecf20Sopenharmony_ci	while (!list_empty(&fs_info->unused_bgs)) {
13588c2ecf20Sopenharmony_ci		int trimming;
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_ci		block_group = list_first_entry(&fs_info->unused_bgs,
13618c2ecf20Sopenharmony_ci					       struct btrfs_block_group,
13628c2ecf20Sopenharmony_ci					       bg_list);
13638c2ecf20Sopenharmony_ci		list_del_init(&block_group->bg_list);
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci		space_info = block_group->space_info;
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci		if (ret || btrfs_mixed_space_info(space_info)) {
13688c2ecf20Sopenharmony_ci			btrfs_put_block_group(block_group);
13698c2ecf20Sopenharmony_ci			continue;
13708c2ecf20Sopenharmony_ci		}
13718c2ecf20Sopenharmony_ci		spin_unlock(&fs_info->unused_bgs_lock);
13728c2ecf20Sopenharmony_ci
13738c2ecf20Sopenharmony_ci		btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci		mutex_lock(&fs_info->delete_unused_bgs_mutex);
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci		/* Don't want to race with allocators so take the groups_sem */
13788c2ecf20Sopenharmony_ci		down_write(&space_info->groups_sem);
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci		/*
13818c2ecf20Sopenharmony_ci		 * Async discard moves the final block group discard to be prior
13828c2ecf20Sopenharmony_ci		 * to the unused_bgs code path.  Therefore, if it's not fully
13838c2ecf20Sopenharmony_ci		 * trimmed, punt it back to the async discard lists.
13848c2ecf20Sopenharmony_ci		 */
13858c2ecf20Sopenharmony_ci		if (btrfs_test_opt(fs_info, DISCARD_ASYNC) &&
13868c2ecf20Sopenharmony_ci		    !btrfs_is_free_space_trimmed(block_group)) {
13878c2ecf20Sopenharmony_ci			trace_btrfs_skip_unused_block_group(block_group);
13888c2ecf20Sopenharmony_ci			up_write(&space_info->groups_sem);
13898c2ecf20Sopenharmony_ci			/* Requeue if we failed because of async discard */
13908c2ecf20Sopenharmony_ci			btrfs_discard_queue_work(&fs_info->discard_ctl,
13918c2ecf20Sopenharmony_ci						 block_group);
13928c2ecf20Sopenharmony_ci			goto next;
13938c2ecf20Sopenharmony_ci		}
13948c2ecf20Sopenharmony_ci
13958c2ecf20Sopenharmony_ci		spin_lock(&block_group->lock);
13968c2ecf20Sopenharmony_ci		if (block_group->reserved || block_group->pinned ||
13978c2ecf20Sopenharmony_ci		    block_group->used || block_group->ro ||
13988c2ecf20Sopenharmony_ci		    list_is_singular(&block_group->list)) {
13998c2ecf20Sopenharmony_ci			/*
14008c2ecf20Sopenharmony_ci			 * We want to bail if we made new allocations or have
14018c2ecf20Sopenharmony_ci			 * outstanding allocations in this block group.  We do
14028c2ecf20Sopenharmony_ci			 * the ro check in case balance is currently acting on
14038c2ecf20Sopenharmony_ci			 * this block group.
14048c2ecf20Sopenharmony_ci			 */
14058c2ecf20Sopenharmony_ci			trace_btrfs_skip_unused_block_group(block_group);
14068c2ecf20Sopenharmony_ci			spin_unlock(&block_group->lock);
14078c2ecf20Sopenharmony_ci			up_write(&space_info->groups_sem);
14088c2ecf20Sopenharmony_ci			goto next;
14098c2ecf20Sopenharmony_ci		}
14108c2ecf20Sopenharmony_ci		spin_unlock(&block_group->lock);
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_ci		/* We don't want to force the issue, only flip if it's ok. */
14138c2ecf20Sopenharmony_ci		ret = inc_block_group_ro(block_group, 0);
14148c2ecf20Sopenharmony_ci		up_write(&space_info->groups_sem);
14158c2ecf20Sopenharmony_ci		if (ret < 0) {
14168c2ecf20Sopenharmony_ci			ret = 0;
14178c2ecf20Sopenharmony_ci			goto next;
14188c2ecf20Sopenharmony_ci		}
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci		/*
14218c2ecf20Sopenharmony_ci		 * Want to do this before we do anything else so we can recover
14228c2ecf20Sopenharmony_ci		 * properly if we fail to join the transaction.
14238c2ecf20Sopenharmony_ci		 */
14248c2ecf20Sopenharmony_ci		trans = btrfs_start_trans_remove_block_group(fs_info,
14258c2ecf20Sopenharmony_ci						     block_group->start);
14268c2ecf20Sopenharmony_ci		if (IS_ERR(trans)) {
14278c2ecf20Sopenharmony_ci			btrfs_dec_block_group_ro(block_group);
14288c2ecf20Sopenharmony_ci			ret = PTR_ERR(trans);
14298c2ecf20Sopenharmony_ci			goto next;
14308c2ecf20Sopenharmony_ci		}
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci		/*
14338c2ecf20Sopenharmony_ci		 * We could have pending pinned extents for this block group,
14348c2ecf20Sopenharmony_ci		 * just delete them, we don't care about them anymore.
14358c2ecf20Sopenharmony_ci		 */
14368c2ecf20Sopenharmony_ci		if (!clean_pinned_extents(trans, block_group)) {
14378c2ecf20Sopenharmony_ci			btrfs_dec_block_group_ro(block_group);
14388c2ecf20Sopenharmony_ci			goto end_trans;
14398c2ecf20Sopenharmony_ci		}
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci		/*
14428c2ecf20Sopenharmony_ci		 * At this point, the block_group is read only and should fail
14438c2ecf20Sopenharmony_ci		 * new allocations.  However, btrfs_finish_extent_commit() can
14448c2ecf20Sopenharmony_ci		 * cause this block_group to be placed back on the discard
14458c2ecf20Sopenharmony_ci		 * lists because now the block_group isn't fully discarded.
14468c2ecf20Sopenharmony_ci		 * Bail here and try again later after discarding everything.
14478c2ecf20Sopenharmony_ci		 */
14488c2ecf20Sopenharmony_ci		spin_lock(&fs_info->discard_ctl.lock);
14498c2ecf20Sopenharmony_ci		if (!list_empty(&block_group->discard_list)) {
14508c2ecf20Sopenharmony_ci			spin_unlock(&fs_info->discard_ctl.lock);
14518c2ecf20Sopenharmony_ci			btrfs_dec_block_group_ro(block_group);
14528c2ecf20Sopenharmony_ci			btrfs_discard_queue_work(&fs_info->discard_ctl,
14538c2ecf20Sopenharmony_ci						 block_group);
14548c2ecf20Sopenharmony_ci			goto end_trans;
14558c2ecf20Sopenharmony_ci		}
14568c2ecf20Sopenharmony_ci		spin_unlock(&fs_info->discard_ctl.lock);
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci		/* Reset pinned so btrfs_put_block_group doesn't complain */
14598c2ecf20Sopenharmony_ci		spin_lock(&space_info->lock);
14608c2ecf20Sopenharmony_ci		spin_lock(&block_group->lock);
14618c2ecf20Sopenharmony_ci
14628c2ecf20Sopenharmony_ci		btrfs_space_info_update_bytes_pinned(fs_info, space_info,
14638c2ecf20Sopenharmony_ci						     -block_group->pinned);
14648c2ecf20Sopenharmony_ci		space_info->bytes_readonly += block_group->pinned;
14658c2ecf20Sopenharmony_ci		__btrfs_mod_total_bytes_pinned(space_info, -block_group->pinned);
14668c2ecf20Sopenharmony_ci		block_group->pinned = 0;
14678c2ecf20Sopenharmony_ci
14688c2ecf20Sopenharmony_ci		spin_unlock(&block_group->lock);
14698c2ecf20Sopenharmony_ci		spin_unlock(&space_info->lock);
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ci		/*
14728c2ecf20Sopenharmony_ci		 * The normal path here is an unused block group is passed here,
14738c2ecf20Sopenharmony_ci		 * then trimming is handled in the transaction commit path.
14748c2ecf20Sopenharmony_ci		 * Async discard interposes before this to do the trimming
14758c2ecf20Sopenharmony_ci		 * before coming down the unused block group path as trimming
14768c2ecf20Sopenharmony_ci		 * will no longer be done later in the transaction commit path.
14778c2ecf20Sopenharmony_ci		 */
14788c2ecf20Sopenharmony_ci		if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC))
14798c2ecf20Sopenharmony_ci			goto flip_async;
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci		/* DISCARD can flip during remount */
14828c2ecf20Sopenharmony_ci		trimming = btrfs_test_opt(fs_info, DISCARD_SYNC);
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci		/* Implicit trim during transaction commit. */
14858c2ecf20Sopenharmony_ci		if (trimming)
14868c2ecf20Sopenharmony_ci			btrfs_freeze_block_group(block_group);
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci		/*
14898c2ecf20Sopenharmony_ci		 * Btrfs_remove_chunk will abort the transaction if things go
14908c2ecf20Sopenharmony_ci		 * horribly wrong.
14918c2ecf20Sopenharmony_ci		 */
14928c2ecf20Sopenharmony_ci		ret = btrfs_remove_chunk(trans, block_group->start);
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_ci		if (ret) {
14958c2ecf20Sopenharmony_ci			if (trimming)
14968c2ecf20Sopenharmony_ci				btrfs_unfreeze_block_group(block_group);
14978c2ecf20Sopenharmony_ci			goto end_trans;
14988c2ecf20Sopenharmony_ci		}
14998c2ecf20Sopenharmony_ci
15008c2ecf20Sopenharmony_ci		/*
15018c2ecf20Sopenharmony_ci		 * If we're not mounted with -odiscard, we can just forget
15028c2ecf20Sopenharmony_ci		 * about this block group. Otherwise we'll need to wait
15038c2ecf20Sopenharmony_ci		 * until transaction commit to do the actual discard.
15048c2ecf20Sopenharmony_ci		 */
15058c2ecf20Sopenharmony_ci		if (trimming) {
15068c2ecf20Sopenharmony_ci			spin_lock(&fs_info->unused_bgs_lock);
15078c2ecf20Sopenharmony_ci			/*
15088c2ecf20Sopenharmony_ci			 * A concurrent scrub might have added us to the list
15098c2ecf20Sopenharmony_ci			 * fs_info->unused_bgs, so use a list_move operation
15108c2ecf20Sopenharmony_ci			 * to add the block group to the deleted_bgs list.
15118c2ecf20Sopenharmony_ci			 */
15128c2ecf20Sopenharmony_ci			list_move(&block_group->bg_list,
15138c2ecf20Sopenharmony_ci				  &trans->transaction->deleted_bgs);
15148c2ecf20Sopenharmony_ci			spin_unlock(&fs_info->unused_bgs_lock);
15158c2ecf20Sopenharmony_ci			btrfs_get_block_group(block_group);
15168c2ecf20Sopenharmony_ci		}
15178c2ecf20Sopenharmony_ciend_trans:
15188c2ecf20Sopenharmony_ci		btrfs_end_transaction(trans);
15198c2ecf20Sopenharmony_cinext:
15208c2ecf20Sopenharmony_ci		mutex_unlock(&fs_info->delete_unused_bgs_mutex);
15218c2ecf20Sopenharmony_ci		btrfs_put_block_group(block_group);
15228c2ecf20Sopenharmony_ci		spin_lock(&fs_info->unused_bgs_lock);
15238c2ecf20Sopenharmony_ci	}
15248c2ecf20Sopenharmony_ci	spin_unlock(&fs_info->unused_bgs_lock);
15258c2ecf20Sopenharmony_ci	return;
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_ciflip_async:
15288c2ecf20Sopenharmony_ci	btrfs_end_transaction(trans);
15298c2ecf20Sopenharmony_ci	mutex_unlock(&fs_info->delete_unused_bgs_mutex);
15308c2ecf20Sopenharmony_ci	btrfs_put_block_group(block_group);
15318c2ecf20Sopenharmony_ci	btrfs_discard_punt_unused_bgs_list(fs_info);
15328c2ecf20Sopenharmony_ci}
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_civoid btrfs_mark_bg_unused(struct btrfs_block_group *bg)
15358c2ecf20Sopenharmony_ci{
15368c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = bg->fs_info;
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_ci	spin_lock(&fs_info->unused_bgs_lock);
15398c2ecf20Sopenharmony_ci	if (list_empty(&bg->bg_list)) {
15408c2ecf20Sopenharmony_ci		btrfs_get_block_group(bg);
15418c2ecf20Sopenharmony_ci		trace_btrfs_add_unused_block_group(bg);
15428c2ecf20Sopenharmony_ci		list_add_tail(&bg->bg_list, &fs_info->unused_bgs);
15438c2ecf20Sopenharmony_ci	}
15448c2ecf20Sopenharmony_ci	spin_unlock(&fs_info->unused_bgs_lock);
15458c2ecf20Sopenharmony_ci}
15468c2ecf20Sopenharmony_ci
15478c2ecf20Sopenharmony_cistatic int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
15488c2ecf20Sopenharmony_ci			   struct btrfs_path *path)
15498c2ecf20Sopenharmony_ci{
15508c2ecf20Sopenharmony_ci	struct extent_map_tree *em_tree;
15518c2ecf20Sopenharmony_ci	struct extent_map *em;
15528c2ecf20Sopenharmony_ci	struct btrfs_block_group_item bg;
15538c2ecf20Sopenharmony_ci	struct extent_buffer *leaf;
15548c2ecf20Sopenharmony_ci	int slot;
15558c2ecf20Sopenharmony_ci	u64 flags;
15568c2ecf20Sopenharmony_ci	int ret = 0;
15578c2ecf20Sopenharmony_ci
15588c2ecf20Sopenharmony_ci	slot = path->slots[0];
15598c2ecf20Sopenharmony_ci	leaf = path->nodes[0];
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci	em_tree = &fs_info->mapping_tree;
15628c2ecf20Sopenharmony_ci	read_lock(&em_tree->lock);
15638c2ecf20Sopenharmony_ci	em = lookup_extent_mapping(em_tree, key->objectid, key->offset);
15648c2ecf20Sopenharmony_ci	read_unlock(&em_tree->lock);
15658c2ecf20Sopenharmony_ci	if (!em) {
15668c2ecf20Sopenharmony_ci		btrfs_err(fs_info,
15678c2ecf20Sopenharmony_ci			  "logical %llu len %llu found bg but no related chunk",
15688c2ecf20Sopenharmony_ci			  key->objectid, key->offset);
15698c2ecf20Sopenharmony_ci		return -ENOENT;
15708c2ecf20Sopenharmony_ci	}
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci	if (em->start != key->objectid || em->len != key->offset) {
15738c2ecf20Sopenharmony_ci		btrfs_err(fs_info,
15748c2ecf20Sopenharmony_ci			"block group %llu len %llu mismatch with chunk %llu len %llu",
15758c2ecf20Sopenharmony_ci			key->objectid, key->offset, em->start, em->len);
15768c2ecf20Sopenharmony_ci		ret = -EUCLEAN;
15778c2ecf20Sopenharmony_ci		goto out_free_em;
15788c2ecf20Sopenharmony_ci	}
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci	read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot),
15818c2ecf20Sopenharmony_ci			   sizeof(bg));
15828c2ecf20Sopenharmony_ci	flags = btrfs_stack_block_group_flags(&bg) &
15838c2ecf20Sopenharmony_ci		BTRFS_BLOCK_GROUP_TYPE_MASK;
15848c2ecf20Sopenharmony_ci
15858c2ecf20Sopenharmony_ci	if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
15868c2ecf20Sopenharmony_ci		btrfs_err(fs_info,
15878c2ecf20Sopenharmony_ci"block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
15888c2ecf20Sopenharmony_ci			  key->objectid, key->offset, flags,
15898c2ecf20Sopenharmony_ci			  (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type));
15908c2ecf20Sopenharmony_ci		ret = -EUCLEAN;
15918c2ecf20Sopenharmony_ci	}
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ciout_free_em:
15948c2ecf20Sopenharmony_ci	free_extent_map(em);
15958c2ecf20Sopenharmony_ci	return ret;
15968c2ecf20Sopenharmony_ci}
15978c2ecf20Sopenharmony_ci
15988c2ecf20Sopenharmony_cistatic int find_first_block_group(struct btrfs_fs_info *fs_info,
15998c2ecf20Sopenharmony_ci				  struct btrfs_path *path,
16008c2ecf20Sopenharmony_ci				  struct btrfs_key *key)
16018c2ecf20Sopenharmony_ci{
16028c2ecf20Sopenharmony_ci	struct btrfs_root *root = fs_info->extent_root;
16038c2ecf20Sopenharmony_ci	int ret;
16048c2ecf20Sopenharmony_ci	struct btrfs_key found_key;
16058c2ecf20Sopenharmony_ci	struct extent_buffer *leaf;
16068c2ecf20Sopenharmony_ci	int slot;
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_ci	ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
16098c2ecf20Sopenharmony_ci	if (ret < 0)
16108c2ecf20Sopenharmony_ci		return ret;
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_ci	while (1) {
16138c2ecf20Sopenharmony_ci		slot = path->slots[0];
16148c2ecf20Sopenharmony_ci		leaf = path->nodes[0];
16158c2ecf20Sopenharmony_ci		if (slot >= btrfs_header_nritems(leaf)) {
16168c2ecf20Sopenharmony_ci			ret = btrfs_next_leaf(root, path);
16178c2ecf20Sopenharmony_ci			if (ret == 0)
16188c2ecf20Sopenharmony_ci				continue;
16198c2ecf20Sopenharmony_ci			if (ret < 0)
16208c2ecf20Sopenharmony_ci				goto out;
16218c2ecf20Sopenharmony_ci			break;
16228c2ecf20Sopenharmony_ci		}
16238c2ecf20Sopenharmony_ci		btrfs_item_key_to_cpu(leaf, &found_key, slot);
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci		if (found_key.objectid >= key->objectid &&
16268c2ecf20Sopenharmony_ci		    found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
16278c2ecf20Sopenharmony_ci			ret = read_bg_from_eb(fs_info, &found_key, path);
16288c2ecf20Sopenharmony_ci			break;
16298c2ecf20Sopenharmony_ci		}
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci		path->slots[0]++;
16328c2ecf20Sopenharmony_ci	}
16338c2ecf20Sopenharmony_ciout:
16348c2ecf20Sopenharmony_ci	return ret;
16358c2ecf20Sopenharmony_ci}
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_cistatic void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
16388c2ecf20Sopenharmony_ci{
16398c2ecf20Sopenharmony_ci	u64 extra_flags = chunk_to_extended(flags) &
16408c2ecf20Sopenharmony_ci				BTRFS_EXTENDED_PROFILE_MASK;
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ci	write_seqlock(&fs_info->profiles_lock);
16438c2ecf20Sopenharmony_ci	if (flags & BTRFS_BLOCK_GROUP_DATA)
16448c2ecf20Sopenharmony_ci		fs_info->avail_data_alloc_bits |= extra_flags;
16458c2ecf20Sopenharmony_ci	if (flags & BTRFS_BLOCK_GROUP_METADATA)
16468c2ecf20Sopenharmony_ci		fs_info->avail_metadata_alloc_bits |= extra_flags;
16478c2ecf20Sopenharmony_ci	if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
16488c2ecf20Sopenharmony_ci		fs_info->avail_system_alloc_bits |= extra_flags;
16498c2ecf20Sopenharmony_ci	write_sequnlock(&fs_info->profiles_lock);
16508c2ecf20Sopenharmony_ci}
16518c2ecf20Sopenharmony_ci
16528c2ecf20Sopenharmony_ci/**
16538c2ecf20Sopenharmony_ci * btrfs_rmap_block - Map a physical disk address to a list of logical addresses
16548c2ecf20Sopenharmony_ci * @chunk_start:   logical address of block group
16558c2ecf20Sopenharmony_ci * @physical:	   physical address to map to logical addresses
16568c2ecf20Sopenharmony_ci * @logical:	   return array of logical addresses which map to @physical
16578c2ecf20Sopenharmony_ci * @naddrs:	   length of @logical
16588c2ecf20Sopenharmony_ci * @stripe_len:    size of IO stripe for the given block group
16598c2ecf20Sopenharmony_ci *
16608c2ecf20Sopenharmony_ci * Maps a particular @physical disk address to a list of @logical addresses.
16618c2ecf20Sopenharmony_ci * Used primarily to exclude those portions of a block group that contain super
16628c2ecf20Sopenharmony_ci * block copies.
16638c2ecf20Sopenharmony_ci */
16648c2ecf20Sopenharmony_ciEXPORT_FOR_TESTS
16658c2ecf20Sopenharmony_ciint btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
16668c2ecf20Sopenharmony_ci		     u64 physical, u64 **logical, int *naddrs, int *stripe_len)
16678c2ecf20Sopenharmony_ci{
16688c2ecf20Sopenharmony_ci	struct extent_map *em;
16698c2ecf20Sopenharmony_ci	struct map_lookup *map;
16708c2ecf20Sopenharmony_ci	u64 *buf;
16718c2ecf20Sopenharmony_ci	u64 bytenr;
16728c2ecf20Sopenharmony_ci	u64 data_stripe_length;
16738c2ecf20Sopenharmony_ci	u64 io_stripe_size;
16748c2ecf20Sopenharmony_ci	int i, nr = 0;
16758c2ecf20Sopenharmony_ci	int ret = 0;
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
16788c2ecf20Sopenharmony_ci	if (IS_ERR(em))
16798c2ecf20Sopenharmony_ci		return -EIO;
16808c2ecf20Sopenharmony_ci
16818c2ecf20Sopenharmony_ci	map = em->map_lookup;
16828c2ecf20Sopenharmony_ci	data_stripe_length = em->orig_block_len;
16838c2ecf20Sopenharmony_ci	io_stripe_size = map->stripe_len;
16848c2ecf20Sopenharmony_ci
16858c2ecf20Sopenharmony_ci	/* For RAID5/6 adjust to a full IO stripe length */
16868c2ecf20Sopenharmony_ci	if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
16878c2ecf20Sopenharmony_ci		io_stripe_size = map->stripe_len * nr_data_stripes(map);
16888c2ecf20Sopenharmony_ci
16898c2ecf20Sopenharmony_ci	buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
16908c2ecf20Sopenharmony_ci	if (!buf) {
16918c2ecf20Sopenharmony_ci		ret = -ENOMEM;
16928c2ecf20Sopenharmony_ci		goto out;
16938c2ecf20Sopenharmony_ci	}
16948c2ecf20Sopenharmony_ci
16958c2ecf20Sopenharmony_ci	for (i = 0; i < map->num_stripes; i++) {
16968c2ecf20Sopenharmony_ci		bool already_inserted = false;
16978c2ecf20Sopenharmony_ci		u64 stripe_nr;
16988c2ecf20Sopenharmony_ci		int j;
16998c2ecf20Sopenharmony_ci
17008c2ecf20Sopenharmony_ci		if (!in_range(physical, map->stripes[i].physical,
17018c2ecf20Sopenharmony_ci			      data_stripe_length))
17028c2ecf20Sopenharmony_ci			continue;
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_ci		stripe_nr = physical - map->stripes[i].physical;
17058c2ecf20Sopenharmony_ci		stripe_nr = div64_u64(stripe_nr, map->stripe_len);
17068c2ecf20Sopenharmony_ci
17078c2ecf20Sopenharmony_ci		if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
17088c2ecf20Sopenharmony_ci			stripe_nr = stripe_nr * map->num_stripes + i;
17098c2ecf20Sopenharmony_ci			stripe_nr = div_u64(stripe_nr, map->sub_stripes);
17108c2ecf20Sopenharmony_ci		} else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
17118c2ecf20Sopenharmony_ci			stripe_nr = stripe_nr * map->num_stripes + i;
17128c2ecf20Sopenharmony_ci		}
17138c2ecf20Sopenharmony_ci		/*
17148c2ecf20Sopenharmony_ci		 * The remaining case would be for RAID56, multiply by
17158c2ecf20Sopenharmony_ci		 * nr_data_stripes().  Alternatively, just use rmap_len below
17168c2ecf20Sopenharmony_ci		 * instead of map->stripe_len
17178c2ecf20Sopenharmony_ci		 */
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_ci		bytenr = chunk_start + stripe_nr * io_stripe_size;
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci		/* Ensure we don't add duplicate addresses */
17228c2ecf20Sopenharmony_ci		for (j = 0; j < nr; j++) {
17238c2ecf20Sopenharmony_ci			if (buf[j] == bytenr) {
17248c2ecf20Sopenharmony_ci				already_inserted = true;
17258c2ecf20Sopenharmony_ci				break;
17268c2ecf20Sopenharmony_ci			}
17278c2ecf20Sopenharmony_ci		}
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_ci		if (!already_inserted)
17308c2ecf20Sopenharmony_ci			buf[nr++] = bytenr;
17318c2ecf20Sopenharmony_ci	}
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci	*logical = buf;
17348c2ecf20Sopenharmony_ci	*naddrs = nr;
17358c2ecf20Sopenharmony_ci	*stripe_len = io_stripe_size;
17368c2ecf20Sopenharmony_ciout:
17378c2ecf20Sopenharmony_ci	free_extent_map(em);
17388c2ecf20Sopenharmony_ci	return ret;
17398c2ecf20Sopenharmony_ci}
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_cistatic int exclude_super_stripes(struct btrfs_block_group *cache)
17428c2ecf20Sopenharmony_ci{
17438c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = cache->fs_info;
17448c2ecf20Sopenharmony_ci	u64 bytenr;
17458c2ecf20Sopenharmony_ci	u64 *logical;
17468c2ecf20Sopenharmony_ci	int stripe_len;
17478c2ecf20Sopenharmony_ci	int i, nr, ret;
17488c2ecf20Sopenharmony_ci
17498c2ecf20Sopenharmony_ci	if (cache->start < BTRFS_SUPER_INFO_OFFSET) {
17508c2ecf20Sopenharmony_ci		stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start;
17518c2ecf20Sopenharmony_ci		cache->bytes_super += stripe_len;
17528c2ecf20Sopenharmony_ci		ret = btrfs_add_excluded_extent(fs_info, cache->start,
17538c2ecf20Sopenharmony_ci						stripe_len);
17548c2ecf20Sopenharmony_ci		if (ret)
17558c2ecf20Sopenharmony_ci			return ret;
17568c2ecf20Sopenharmony_ci	}
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_ci	for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
17598c2ecf20Sopenharmony_ci		bytenr = btrfs_sb_offset(i);
17608c2ecf20Sopenharmony_ci		ret = btrfs_rmap_block(fs_info, cache->start,
17618c2ecf20Sopenharmony_ci				       bytenr, &logical, &nr, &stripe_len);
17628c2ecf20Sopenharmony_ci		if (ret)
17638c2ecf20Sopenharmony_ci			return ret;
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_ci		while (nr--) {
17668c2ecf20Sopenharmony_ci			u64 len = min_t(u64, stripe_len,
17678c2ecf20Sopenharmony_ci				cache->start + cache->length - logical[nr]);
17688c2ecf20Sopenharmony_ci
17698c2ecf20Sopenharmony_ci			cache->bytes_super += len;
17708c2ecf20Sopenharmony_ci			ret = btrfs_add_excluded_extent(fs_info, logical[nr],
17718c2ecf20Sopenharmony_ci							len);
17728c2ecf20Sopenharmony_ci			if (ret) {
17738c2ecf20Sopenharmony_ci				kfree(logical);
17748c2ecf20Sopenharmony_ci				return ret;
17758c2ecf20Sopenharmony_ci			}
17768c2ecf20Sopenharmony_ci		}
17778c2ecf20Sopenharmony_ci
17788c2ecf20Sopenharmony_ci		kfree(logical);
17798c2ecf20Sopenharmony_ci	}
17808c2ecf20Sopenharmony_ci	return 0;
17818c2ecf20Sopenharmony_ci}
17828c2ecf20Sopenharmony_ci
17838c2ecf20Sopenharmony_cistatic void link_block_group(struct btrfs_block_group *cache)
17848c2ecf20Sopenharmony_ci{
17858c2ecf20Sopenharmony_ci	struct btrfs_space_info *space_info = cache->space_info;
17868c2ecf20Sopenharmony_ci	int index = btrfs_bg_flags_to_raid_index(cache->flags);
17878c2ecf20Sopenharmony_ci
17888c2ecf20Sopenharmony_ci	down_write(&space_info->groups_sem);
17898c2ecf20Sopenharmony_ci	list_add_tail(&cache->list, &space_info->block_groups[index]);
17908c2ecf20Sopenharmony_ci	up_write(&space_info->groups_sem);
17918c2ecf20Sopenharmony_ci}
17928c2ecf20Sopenharmony_ci
17938c2ecf20Sopenharmony_cistatic struct btrfs_block_group *btrfs_create_block_group_cache(
17948c2ecf20Sopenharmony_ci		struct btrfs_fs_info *fs_info, u64 start)
17958c2ecf20Sopenharmony_ci{
17968c2ecf20Sopenharmony_ci	struct btrfs_block_group *cache;
17978c2ecf20Sopenharmony_ci
17988c2ecf20Sopenharmony_ci	cache = kzalloc(sizeof(*cache), GFP_NOFS);
17998c2ecf20Sopenharmony_ci	if (!cache)
18008c2ecf20Sopenharmony_ci		return NULL;
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_ci	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
18038c2ecf20Sopenharmony_ci					GFP_NOFS);
18048c2ecf20Sopenharmony_ci	if (!cache->free_space_ctl) {
18058c2ecf20Sopenharmony_ci		kfree(cache);
18068c2ecf20Sopenharmony_ci		return NULL;
18078c2ecf20Sopenharmony_ci	}
18088c2ecf20Sopenharmony_ci
18098c2ecf20Sopenharmony_ci	cache->start = start;
18108c2ecf20Sopenharmony_ci
18118c2ecf20Sopenharmony_ci	cache->fs_info = fs_info;
18128c2ecf20Sopenharmony_ci	cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
18138c2ecf20Sopenharmony_ci
18148c2ecf20Sopenharmony_ci	cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED;
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci	refcount_set(&cache->refs, 1);
18178c2ecf20Sopenharmony_ci	spin_lock_init(&cache->lock);
18188c2ecf20Sopenharmony_ci	init_rwsem(&cache->data_rwsem);
18198c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cache->list);
18208c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cache->cluster_list);
18218c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cache->bg_list);
18228c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cache->ro_list);
18238c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cache->discard_list);
18248c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cache->dirty_list);
18258c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cache->io_list);
18268c2ecf20Sopenharmony_ci	btrfs_init_free_space_ctl(cache);
18278c2ecf20Sopenharmony_ci	atomic_set(&cache->frozen, 0);
18288c2ecf20Sopenharmony_ci	mutex_init(&cache->free_space_lock);
18298c2ecf20Sopenharmony_ci	btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
18308c2ecf20Sopenharmony_ci
18318c2ecf20Sopenharmony_ci	return cache;
18328c2ecf20Sopenharmony_ci}
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_ci/*
18358c2ecf20Sopenharmony_ci * Iterate all chunks and verify that each of them has the corresponding block
18368c2ecf20Sopenharmony_ci * group
18378c2ecf20Sopenharmony_ci */
18388c2ecf20Sopenharmony_cistatic int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
18398c2ecf20Sopenharmony_ci{
18408c2ecf20Sopenharmony_ci	struct extent_map_tree *map_tree = &fs_info->mapping_tree;
18418c2ecf20Sopenharmony_ci	struct extent_map *em;
18428c2ecf20Sopenharmony_ci	struct btrfs_block_group *bg;
18438c2ecf20Sopenharmony_ci	u64 start = 0;
18448c2ecf20Sopenharmony_ci	int ret = 0;
18458c2ecf20Sopenharmony_ci
18468c2ecf20Sopenharmony_ci	while (1) {
18478c2ecf20Sopenharmony_ci		read_lock(&map_tree->lock);
18488c2ecf20Sopenharmony_ci		/*
18498c2ecf20Sopenharmony_ci		 * lookup_extent_mapping will return the first extent map
18508c2ecf20Sopenharmony_ci		 * intersecting the range, so setting @len to 1 is enough to
18518c2ecf20Sopenharmony_ci		 * get the first chunk.
18528c2ecf20Sopenharmony_ci		 */
18538c2ecf20Sopenharmony_ci		em = lookup_extent_mapping(map_tree, start, 1);
18548c2ecf20Sopenharmony_ci		read_unlock(&map_tree->lock);
18558c2ecf20Sopenharmony_ci		if (!em)
18568c2ecf20Sopenharmony_ci			break;
18578c2ecf20Sopenharmony_ci
18588c2ecf20Sopenharmony_ci		bg = btrfs_lookup_block_group(fs_info, em->start);
18598c2ecf20Sopenharmony_ci		if (!bg) {
18608c2ecf20Sopenharmony_ci			btrfs_err(fs_info,
18618c2ecf20Sopenharmony_ci	"chunk start=%llu len=%llu doesn't have corresponding block group",
18628c2ecf20Sopenharmony_ci				     em->start, em->len);
18638c2ecf20Sopenharmony_ci			ret = -EUCLEAN;
18648c2ecf20Sopenharmony_ci			free_extent_map(em);
18658c2ecf20Sopenharmony_ci			break;
18668c2ecf20Sopenharmony_ci		}
18678c2ecf20Sopenharmony_ci		if (bg->start != em->start || bg->length != em->len ||
18688c2ecf20Sopenharmony_ci		    (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
18698c2ecf20Sopenharmony_ci		    (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
18708c2ecf20Sopenharmony_ci			btrfs_err(fs_info,
18718c2ecf20Sopenharmony_ci"chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",
18728c2ecf20Sopenharmony_ci				em->start, em->len,
18738c2ecf20Sopenharmony_ci				em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
18748c2ecf20Sopenharmony_ci				bg->start, bg->length,
18758c2ecf20Sopenharmony_ci				bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
18768c2ecf20Sopenharmony_ci			ret = -EUCLEAN;
18778c2ecf20Sopenharmony_ci			free_extent_map(em);
18788c2ecf20Sopenharmony_ci			btrfs_put_block_group(bg);
18798c2ecf20Sopenharmony_ci			break;
18808c2ecf20Sopenharmony_ci		}
18818c2ecf20Sopenharmony_ci		start = em->start + em->len;
18828c2ecf20Sopenharmony_ci		free_extent_map(em);
18838c2ecf20Sopenharmony_ci		btrfs_put_block_group(bg);
18848c2ecf20Sopenharmony_ci	}
18858c2ecf20Sopenharmony_ci	return ret;
18868c2ecf20Sopenharmony_ci}
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_cistatic void read_block_group_item(struct btrfs_block_group *cache,
18898c2ecf20Sopenharmony_ci				 struct btrfs_path *path,
18908c2ecf20Sopenharmony_ci				 const struct btrfs_key *key)
18918c2ecf20Sopenharmony_ci{
18928c2ecf20Sopenharmony_ci	struct extent_buffer *leaf = path->nodes[0];
18938c2ecf20Sopenharmony_ci	struct btrfs_block_group_item bgi;
18948c2ecf20Sopenharmony_ci	int slot = path->slots[0];
18958c2ecf20Sopenharmony_ci
18968c2ecf20Sopenharmony_ci	cache->length = key->offset;
18978c2ecf20Sopenharmony_ci
18988c2ecf20Sopenharmony_ci	read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
18998c2ecf20Sopenharmony_ci			   sizeof(bgi));
19008c2ecf20Sopenharmony_ci	cache->used = btrfs_stack_block_group_used(&bgi);
19018c2ecf20Sopenharmony_ci	cache->flags = btrfs_stack_block_group_flags(&bgi);
19028c2ecf20Sopenharmony_ci}
19038c2ecf20Sopenharmony_ci
19048c2ecf20Sopenharmony_cistatic int read_one_block_group(struct btrfs_fs_info *info,
19058c2ecf20Sopenharmony_ci				struct btrfs_path *path,
19068c2ecf20Sopenharmony_ci				const struct btrfs_key *key,
19078c2ecf20Sopenharmony_ci				int need_clear)
19088c2ecf20Sopenharmony_ci{
19098c2ecf20Sopenharmony_ci	struct btrfs_block_group *cache;
19108c2ecf20Sopenharmony_ci	struct btrfs_space_info *space_info;
19118c2ecf20Sopenharmony_ci	const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS);
19128c2ecf20Sopenharmony_ci	int ret;
19138c2ecf20Sopenharmony_ci
19148c2ecf20Sopenharmony_ci	ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY);
19158c2ecf20Sopenharmony_ci
19168c2ecf20Sopenharmony_ci	cache = btrfs_create_block_group_cache(info, key->objectid);
19178c2ecf20Sopenharmony_ci	if (!cache)
19188c2ecf20Sopenharmony_ci		return -ENOMEM;
19198c2ecf20Sopenharmony_ci
19208c2ecf20Sopenharmony_ci	read_block_group_item(cache, path, key);
19218c2ecf20Sopenharmony_ci
19228c2ecf20Sopenharmony_ci	set_free_space_tree_thresholds(cache);
19238c2ecf20Sopenharmony_ci
19248c2ecf20Sopenharmony_ci	if (need_clear) {
19258c2ecf20Sopenharmony_ci		/*
19268c2ecf20Sopenharmony_ci		 * When we mount with old space cache, we need to
19278c2ecf20Sopenharmony_ci		 * set BTRFS_DC_CLEAR and set dirty flag.
19288c2ecf20Sopenharmony_ci		 *
19298c2ecf20Sopenharmony_ci		 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
19308c2ecf20Sopenharmony_ci		 *    truncate the old free space cache inode and
19318c2ecf20Sopenharmony_ci		 *    setup a new one.
19328c2ecf20Sopenharmony_ci		 * b) Setting 'dirty flag' makes sure that we flush
19338c2ecf20Sopenharmony_ci		 *    the new space cache info onto disk.
19348c2ecf20Sopenharmony_ci		 */
19358c2ecf20Sopenharmony_ci		if (btrfs_test_opt(info, SPACE_CACHE))
19368c2ecf20Sopenharmony_ci			cache->disk_cache_state = BTRFS_DC_CLEAR;
19378c2ecf20Sopenharmony_ci	}
19388c2ecf20Sopenharmony_ci	if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
19398c2ecf20Sopenharmony_ci	    (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
19408c2ecf20Sopenharmony_ci			btrfs_err(info,
19418c2ecf20Sopenharmony_ci"bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
19428c2ecf20Sopenharmony_ci				  cache->start);
19438c2ecf20Sopenharmony_ci			ret = -EINVAL;
19448c2ecf20Sopenharmony_ci			goto error;
19458c2ecf20Sopenharmony_ci	}
19468c2ecf20Sopenharmony_ci
19478c2ecf20Sopenharmony_ci	/*
19488c2ecf20Sopenharmony_ci	 * We need to exclude the super stripes now so that the space info has
19498c2ecf20Sopenharmony_ci	 * super bytes accounted for, otherwise we'll think we have more space
19508c2ecf20Sopenharmony_ci	 * than we actually do.
19518c2ecf20Sopenharmony_ci	 */
19528c2ecf20Sopenharmony_ci	ret = exclude_super_stripes(cache);
19538c2ecf20Sopenharmony_ci	if (ret) {
19548c2ecf20Sopenharmony_ci		/* We may have excluded something, so call this just in case. */
19558c2ecf20Sopenharmony_ci		btrfs_free_excluded_extents(cache);
19568c2ecf20Sopenharmony_ci		goto error;
19578c2ecf20Sopenharmony_ci	}
19588c2ecf20Sopenharmony_ci
19598c2ecf20Sopenharmony_ci	/*
19608c2ecf20Sopenharmony_ci	 * Check for two cases, either we are full, and therefore don't need
19618c2ecf20Sopenharmony_ci	 * to bother with the caching work since we won't find any space, or we
19628c2ecf20Sopenharmony_ci	 * are empty, and we can just add all the space in and be done with it.
19638c2ecf20Sopenharmony_ci	 * This saves us _a_lot_ of time, particularly in the full case.
19648c2ecf20Sopenharmony_ci	 */
19658c2ecf20Sopenharmony_ci	if (cache->length == cache->used) {
19668c2ecf20Sopenharmony_ci		cache->last_byte_to_unpin = (u64)-1;
19678c2ecf20Sopenharmony_ci		cache->cached = BTRFS_CACHE_FINISHED;
19688c2ecf20Sopenharmony_ci		btrfs_free_excluded_extents(cache);
19698c2ecf20Sopenharmony_ci	} else if (cache->used == 0) {
19708c2ecf20Sopenharmony_ci		cache->last_byte_to_unpin = (u64)-1;
19718c2ecf20Sopenharmony_ci		cache->cached = BTRFS_CACHE_FINISHED;
19728c2ecf20Sopenharmony_ci		add_new_free_space(cache, cache->start,
19738c2ecf20Sopenharmony_ci				   cache->start + cache->length);
19748c2ecf20Sopenharmony_ci		btrfs_free_excluded_extents(cache);
19758c2ecf20Sopenharmony_ci	}
19768c2ecf20Sopenharmony_ci
19778c2ecf20Sopenharmony_ci	ret = btrfs_add_block_group_cache(info, cache);
19788c2ecf20Sopenharmony_ci	if (ret) {
19798c2ecf20Sopenharmony_ci		btrfs_remove_free_space_cache(cache);
19808c2ecf20Sopenharmony_ci		goto error;
19818c2ecf20Sopenharmony_ci	}
19828c2ecf20Sopenharmony_ci	trace_btrfs_add_block_group(info, cache, 0);
19838c2ecf20Sopenharmony_ci	btrfs_update_space_info(info, cache->flags, cache->length,
19848c2ecf20Sopenharmony_ci				cache->used, cache->bytes_super, &space_info);
19858c2ecf20Sopenharmony_ci
19868c2ecf20Sopenharmony_ci	cache->space_info = space_info;
19878c2ecf20Sopenharmony_ci
19888c2ecf20Sopenharmony_ci	link_block_group(cache);
19898c2ecf20Sopenharmony_ci
19908c2ecf20Sopenharmony_ci	set_avail_alloc_bits(info, cache->flags);
19918c2ecf20Sopenharmony_ci	if (btrfs_chunk_readonly(info, cache->start)) {
19928c2ecf20Sopenharmony_ci		inc_block_group_ro(cache, 1);
19938c2ecf20Sopenharmony_ci	} else if (cache->used == 0) {
19948c2ecf20Sopenharmony_ci		ASSERT(list_empty(&cache->bg_list));
19958c2ecf20Sopenharmony_ci		if (btrfs_test_opt(info, DISCARD_ASYNC))
19968c2ecf20Sopenharmony_ci			btrfs_discard_queue_work(&info->discard_ctl, cache);
19978c2ecf20Sopenharmony_ci		else
19988c2ecf20Sopenharmony_ci			btrfs_mark_bg_unused(cache);
19998c2ecf20Sopenharmony_ci	}
20008c2ecf20Sopenharmony_ci	return 0;
20018c2ecf20Sopenharmony_cierror:
20028c2ecf20Sopenharmony_ci	btrfs_put_block_group(cache);
20038c2ecf20Sopenharmony_ci	return ret;
20048c2ecf20Sopenharmony_ci}
20058c2ecf20Sopenharmony_ci
20068c2ecf20Sopenharmony_ciint btrfs_read_block_groups(struct btrfs_fs_info *info)
20078c2ecf20Sopenharmony_ci{
20088c2ecf20Sopenharmony_ci	struct btrfs_path *path;
20098c2ecf20Sopenharmony_ci	int ret;
20108c2ecf20Sopenharmony_ci	struct btrfs_block_group *cache;
20118c2ecf20Sopenharmony_ci	struct btrfs_space_info *space_info;
20128c2ecf20Sopenharmony_ci	struct btrfs_key key;
20138c2ecf20Sopenharmony_ci	int need_clear = 0;
20148c2ecf20Sopenharmony_ci	u64 cache_gen;
20158c2ecf20Sopenharmony_ci
20168c2ecf20Sopenharmony_ci	key.objectid = 0;
20178c2ecf20Sopenharmony_ci	key.offset = 0;
20188c2ecf20Sopenharmony_ci	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
20198c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
20208c2ecf20Sopenharmony_ci	if (!path)
20218c2ecf20Sopenharmony_ci		return -ENOMEM;
20228c2ecf20Sopenharmony_ci
20238c2ecf20Sopenharmony_ci	cache_gen = btrfs_super_cache_generation(info->super_copy);
20248c2ecf20Sopenharmony_ci	if (btrfs_test_opt(info, SPACE_CACHE) &&
20258c2ecf20Sopenharmony_ci	    btrfs_super_generation(info->super_copy) != cache_gen)
20268c2ecf20Sopenharmony_ci		need_clear = 1;
20278c2ecf20Sopenharmony_ci	if (btrfs_test_opt(info, CLEAR_CACHE))
20288c2ecf20Sopenharmony_ci		need_clear = 1;
20298c2ecf20Sopenharmony_ci
20308c2ecf20Sopenharmony_ci	while (1) {
20318c2ecf20Sopenharmony_ci		ret = find_first_block_group(info, path, &key);
20328c2ecf20Sopenharmony_ci		if (ret > 0)
20338c2ecf20Sopenharmony_ci			break;
20348c2ecf20Sopenharmony_ci		if (ret != 0)
20358c2ecf20Sopenharmony_ci			goto error;
20368c2ecf20Sopenharmony_ci
20378c2ecf20Sopenharmony_ci		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
20388c2ecf20Sopenharmony_ci		ret = read_one_block_group(info, path, &key, need_clear);
20398c2ecf20Sopenharmony_ci		if (ret < 0)
20408c2ecf20Sopenharmony_ci			goto error;
20418c2ecf20Sopenharmony_ci		key.objectid += key.offset;
20428c2ecf20Sopenharmony_ci		key.offset = 0;
20438c2ecf20Sopenharmony_ci		btrfs_release_path(path);
20448c2ecf20Sopenharmony_ci	}
20458c2ecf20Sopenharmony_ci	btrfs_release_path(path);
20468c2ecf20Sopenharmony_ci
20478c2ecf20Sopenharmony_ci	list_for_each_entry(space_info, &info->space_info, list) {
20488c2ecf20Sopenharmony_ci		int i;
20498c2ecf20Sopenharmony_ci
20508c2ecf20Sopenharmony_ci		for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
20518c2ecf20Sopenharmony_ci			if (list_empty(&space_info->block_groups[i]))
20528c2ecf20Sopenharmony_ci				continue;
20538c2ecf20Sopenharmony_ci			cache = list_first_entry(&space_info->block_groups[i],
20548c2ecf20Sopenharmony_ci						 struct btrfs_block_group,
20558c2ecf20Sopenharmony_ci						 list);
20568c2ecf20Sopenharmony_ci			btrfs_sysfs_add_block_group_type(cache);
20578c2ecf20Sopenharmony_ci		}
20588c2ecf20Sopenharmony_ci
20598c2ecf20Sopenharmony_ci		if (!(btrfs_get_alloc_profile(info, space_info->flags) &
20608c2ecf20Sopenharmony_ci		      (BTRFS_BLOCK_GROUP_RAID10 |
20618c2ecf20Sopenharmony_ci		       BTRFS_BLOCK_GROUP_RAID1_MASK |
20628c2ecf20Sopenharmony_ci		       BTRFS_BLOCK_GROUP_RAID56_MASK |
20638c2ecf20Sopenharmony_ci		       BTRFS_BLOCK_GROUP_DUP)))
20648c2ecf20Sopenharmony_ci			continue;
20658c2ecf20Sopenharmony_ci		/*
20668c2ecf20Sopenharmony_ci		 * Avoid allocating from un-mirrored block group if there are
20678c2ecf20Sopenharmony_ci		 * mirrored block groups.
20688c2ecf20Sopenharmony_ci		 */
20698c2ecf20Sopenharmony_ci		list_for_each_entry(cache,
20708c2ecf20Sopenharmony_ci				&space_info->block_groups[BTRFS_RAID_RAID0],
20718c2ecf20Sopenharmony_ci				list)
20728c2ecf20Sopenharmony_ci			inc_block_group_ro(cache, 1);
20738c2ecf20Sopenharmony_ci		list_for_each_entry(cache,
20748c2ecf20Sopenharmony_ci				&space_info->block_groups[BTRFS_RAID_SINGLE],
20758c2ecf20Sopenharmony_ci				list)
20768c2ecf20Sopenharmony_ci			inc_block_group_ro(cache, 1);
20778c2ecf20Sopenharmony_ci	}
20788c2ecf20Sopenharmony_ci
20798c2ecf20Sopenharmony_ci	btrfs_init_global_block_rsv(info);
20808c2ecf20Sopenharmony_ci	ret = check_chunk_block_group_mappings(info);
20818c2ecf20Sopenharmony_cierror:
20828c2ecf20Sopenharmony_ci	btrfs_free_path(path);
20838c2ecf20Sopenharmony_ci	return ret;
20848c2ecf20Sopenharmony_ci}
20858c2ecf20Sopenharmony_ci
20868c2ecf20Sopenharmony_cistatic int insert_block_group_item(struct btrfs_trans_handle *trans,
20878c2ecf20Sopenharmony_ci				   struct btrfs_block_group *block_group)
20888c2ecf20Sopenharmony_ci{
20898c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
20908c2ecf20Sopenharmony_ci	struct btrfs_block_group_item bgi;
20918c2ecf20Sopenharmony_ci	struct btrfs_root *root;
20928c2ecf20Sopenharmony_ci	struct btrfs_key key;
20938c2ecf20Sopenharmony_ci
20948c2ecf20Sopenharmony_ci	spin_lock(&block_group->lock);
20958c2ecf20Sopenharmony_ci	btrfs_set_stack_block_group_used(&bgi, block_group->used);
20968c2ecf20Sopenharmony_ci	btrfs_set_stack_block_group_chunk_objectid(&bgi,
20978c2ecf20Sopenharmony_ci				BTRFS_FIRST_CHUNK_TREE_OBJECTID);
20988c2ecf20Sopenharmony_ci	btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
20998c2ecf20Sopenharmony_ci	key.objectid = block_group->start;
21008c2ecf20Sopenharmony_ci	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
21018c2ecf20Sopenharmony_ci	key.offset = block_group->length;
21028c2ecf20Sopenharmony_ci	spin_unlock(&block_group->lock);
21038c2ecf20Sopenharmony_ci
21048c2ecf20Sopenharmony_ci	root = fs_info->extent_root;
21058c2ecf20Sopenharmony_ci	return btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi));
21068c2ecf20Sopenharmony_ci}
21078c2ecf20Sopenharmony_ci
21088c2ecf20Sopenharmony_civoid btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
21098c2ecf20Sopenharmony_ci{
21108c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
21118c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group;
21128c2ecf20Sopenharmony_ci	int ret = 0;
21138c2ecf20Sopenharmony_ci
21148c2ecf20Sopenharmony_ci	if (!trans->can_flush_pending_bgs)
21158c2ecf20Sopenharmony_ci		return;
21168c2ecf20Sopenharmony_ci
21178c2ecf20Sopenharmony_ci	while (!list_empty(&trans->new_bgs)) {
21188c2ecf20Sopenharmony_ci		int index;
21198c2ecf20Sopenharmony_ci
21208c2ecf20Sopenharmony_ci		block_group = list_first_entry(&trans->new_bgs,
21218c2ecf20Sopenharmony_ci					       struct btrfs_block_group,
21228c2ecf20Sopenharmony_ci					       bg_list);
21238c2ecf20Sopenharmony_ci		if (ret)
21248c2ecf20Sopenharmony_ci			goto next;
21258c2ecf20Sopenharmony_ci
21268c2ecf20Sopenharmony_ci		index = btrfs_bg_flags_to_raid_index(block_group->flags);
21278c2ecf20Sopenharmony_ci
21288c2ecf20Sopenharmony_ci		ret = insert_block_group_item(trans, block_group);
21298c2ecf20Sopenharmony_ci		if (ret)
21308c2ecf20Sopenharmony_ci			btrfs_abort_transaction(trans, ret);
21318c2ecf20Sopenharmony_ci		ret = btrfs_finish_chunk_alloc(trans, block_group->start,
21328c2ecf20Sopenharmony_ci					block_group->length);
21338c2ecf20Sopenharmony_ci		if (ret)
21348c2ecf20Sopenharmony_ci			btrfs_abort_transaction(trans, ret);
21358c2ecf20Sopenharmony_ci		add_block_group_free_space(trans, block_group);
21368c2ecf20Sopenharmony_ci
21378c2ecf20Sopenharmony_ci		/*
21388c2ecf20Sopenharmony_ci		 * If we restriped during balance, we may have added a new raid
21398c2ecf20Sopenharmony_ci		 * type, so now add the sysfs entries when it is safe to do so.
21408c2ecf20Sopenharmony_ci		 * We don't have to worry about locking here as it's handled in
21418c2ecf20Sopenharmony_ci		 * btrfs_sysfs_add_block_group_type.
21428c2ecf20Sopenharmony_ci		 */
21438c2ecf20Sopenharmony_ci		if (block_group->space_info->block_group_kobjs[index] == NULL)
21448c2ecf20Sopenharmony_ci			btrfs_sysfs_add_block_group_type(block_group);
21458c2ecf20Sopenharmony_ci
21468c2ecf20Sopenharmony_ci		/* Already aborted the transaction if it failed. */
21478c2ecf20Sopenharmony_cinext:
21488c2ecf20Sopenharmony_ci		btrfs_delayed_refs_rsv_release(fs_info, 1);
21498c2ecf20Sopenharmony_ci		list_del_init(&block_group->bg_list);
21508c2ecf20Sopenharmony_ci	}
21518c2ecf20Sopenharmony_ci	btrfs_trans_release_chunk_metadata(trans);
21528c2ecf20Sopenharmony_ci}
21538c2ecf20Sopenharmony_ci
21548c2ecf20Sopenharmony_ciint btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used,
21558c2ecf20Sopenharmony_ci			   u64 type, u64 chunk_offset, u64 size)
21568c2ecf20Sopenharmony_ci{
21578c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
21588c2ecf20Sopenharmony_ci	struct btrfs_block_group *cache;
21598c2ecf20Sopenharmony_ci	int ret;
21608c2ecf20Sopenharmony_ci
21618c2ecf20Sopenharmony_ci	btrfs_set_log_full_commit(trans);
21628c2ecf20Sopenharmony_ci
21638c2ecf20Sopenharmony_ci	cache = btrfs_create_block_group_cache(fs_info, chunk_offset);
21648c2ecf20Sopenharmony_ci	if (!cache)
21658c2ecf20Sopenharmony_ci		return -ENOMEM;
21668c2ecf20Sopenharmony_ci
21678c2ecf20Sopenharmony_ci	cache->length = size;
21688c2ecf20Sopenharmony_ci	set_free_space_tree_thresholds(cache);
21698c2ecf20Sopenharmony_ci	cache->used = bytes_used;
21708c2ecf20Sopenharmony_ci	cache->flags = type;
21718c2ecf20Sopenharmony_ci	cache->last_byte_to_unpin = (u64)-1;
21728c2ecf20Sopenharmony_ci	cache->cached = BTRFS_CACHE_FINISHED;
21738c2ecf20Sopenharmony_ci	cache->needs_free_space = 1;
21748c2ecf20Sopenharmony_ci	ret = exclude_super_stripes(cache);
21758c2ecf20Sopenharmony_ci	if (ret) {
21768c2ecf20Sopenharmony_ci		/* We may have excluded something, so call this just in case */
21778c2ecf20Sopenharmony_ci		btrfs_free_excluded_extents(cache);
21788c2ecf20Sopenharmony_ci		btrfs_put_block_group(cache);
21798c2ecf20Sopenharmony_ci		return ret;
21808c2ecf20Sopenharmony_ci	}
21818c2ecf20Sopenharmony_ci
21828c2ecf20Sopenharmony_ci	add_new_free_space(cache, chunk_offset, chunk_offset + size);
21838c2ecf20Sopenharmony_ci
21848c2ecf20Sopenharmony_ci	btrfs_free_excluded_extents(cache);
21858c2ecf20Sopenharmony_ci
21868c2ecf20Sopenharmony_ci#ifdef CONFIG_BTRFS_DEBUG
21878c2ecf20Sopenharmony_ci	if (btrfs_should_fragment_free_space(cache)) {
21888c2ecf20Sopenharmony_ci		u64 new_bytes_used = size - bytes_used;
21898c2ecf20Sopenharmony_ci
21908c2ecf20Sopenharmony_ci		bytes_used += new_bytes_used >> 1;
21918c2ecf20Sopenharmony_ci		fragment_free_space(cache);
21928c2ecf20Sopenharmony_ci	}
21938c2ecf20Sopenharmony_ci#endif
21948c2ecf20Sopenharmony_ci	/*
21958c2ecf20Sopenharmony_ci	 * Ensure the corresponding space_info object is created and
21968c2ecf20Sopenharmony_ci	 * assigned to our block group. We want our bg to be added to the rbtree
21978c2ecf20Sopenharmony_ci	 * with its ->space_info set.
21988c2ecf20Sopenharmony_ci	 */
21998c2ecf20Sopenharmony_ci	cache->space_info = btrfs_find_space_info(fs_info, cache->flags);
22008c2ecf20Sopenharmony_ci	ASSERT(cache->space_info);
22018c2ecf20Sopenharmony_ci
22028c2ecf20Sopenharmony_ci	ret = btrfs_add_block_group_cache(fs_info, cache);
22038c2ecf20Sopenharmony_ci	if (ret) {
22048c2ecf20Sopenharmony_ci		btrfs_remove_free_space_cache(cache);
22058c2ecf20Sopenharmony_ci		btrfs_put_block_group(cache);
22068c2ecf20Sopenharmony_ci		return ret;
22078c2ecf20Sopenharmony_ci	}
22088c2ecf20Sopenharmony_ci
22098c2ecf20Sopenharmony_ci	/*
22108c2ecf20Sopenharmony_ci	 * Now that our block group has its ->space_info set and is inserted in
22118c2ecf20Sopenharmony_ci	 * the rbtree, update the space info's counters.
22128c2ecf20Sopenharmony_ci	 */
22138c2ecf20Sopenharmony_ci	trace_btrfs_add_block_group(fs_info, cache, 1);
22148c2ecf20Sopenharmony_ci	btrfs_update_space_info(fs_info, cache->flags, size, bytes_used,
22158c2ecf20Sopenharmony_ci				cache->bytes_super, &cache->space_info);
22168c2ecf20Sopenharmony_ci	btrfs_update_global_block_rsv(fs_info);
22178c2ecf20Sopenharmony_ci
22188c2ecf20Sopenharmony_ci	link_block_group(cache);
22198c2ecf20Sopenharmony_ci
22208c2ecf20Sopenharmony_ci	list_add_tail(&cache->bg_list, &trans->new_bgs);
22218c2ecf20Sopenharmony_ci	trans->delayed_ref_updates++;
22228c2ecf20Sopenharmony_ci	btrfs_update_delayed_refs_rsv(trans);
22238c2ecf20Sopenharmony_ci
22248c2ecf20Sopenharmony_ci	set_avail_alloc_bits(fs_info, type);
22258c2ecf20Sopenharmony_ci	return 0;
22268c2ecf20Sopenharmony_ci}
22278c2ecf20Sopenharmony_ci
22288c2ecf20Sopenharmony_ci/*
22298c2ecf20Sopenharmony_ci * Mark one block group RO, can be called several times for the same block
22308c2ecf20Sopenharmony_ci * group.
22318c2ecf20Sopenharmony_ci *
22328c2ecf20Sopenharmony_ci * @cache:		the destination block group
22338c2ecf20Sopenharmony_ci * @do_chunk_alloc:	whether need to do chunk pre-allocation, this is to
22348c2ecf20Sopenharmony_ci * 			ensure we still have some free space after marking this
22358c2ecf20Sopenharmony_ci * 			block group RO.
22368c2ecf20Sopenharmony_ci */
22378c2ecf20Sopenharmony_ciint btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
22388c2ecf20Sopenharmony_ci			     bool do_chunk_alloc)
22398c2ecf20Sopenharmony_ci{
22408c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = cache->fs_info;
22418c2ecf20Sopenharmony_ci	struct btrfs_trans_handle *trans;
22428c2ecf20Sopenharmony_ci	u64 alloc_flags;
22438c2ecf20Sopenharmony_ci	int ret;
22448c2ecf20Sopenharmony_ci
22458c2ecf20Sopenharmony_ciagain:
22468c2ecf20Sopenharmony_ci	trans = btrfs_join_transaction(fs_info->extent_root);
22478c2ecf20Sopenharmony_ci	if (IS_ERR(trans))
22488c2ecf20Sopenharmony_ci		return PTR_ERR(trans);
22498c2ecf20Sopenharmony_ci
22508c2ecf20Sopenharmony_ci	/*
22518c2ecf20Sopenharmony_ci	 * we're not allowed to set block groups readonly after the dirty
22528c2ecf20Sopenharmony_ci	 * block groups cache has started writing.  If it already started,
22538c2ecf20Sopenharmony_ci	 * back off and let this transaction commit
22548c2ecf20Sopenharmony_ci	 */
22558c2ecf20Sopenharmony_ci	mutex_lock(&fs_info->ro_block_group_mutex);
22568c2ecf20Sopenharmony_ci	if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
22578c2ecf20Sopenharmony_ci		u64 transid = trans->transid;
22588c2ecf20Sopenharmony_ci
22598c2ecf20Sopenharmony_ci		mutex_unlock(&fs_info->ro_block_group_mutex);
22608c2ecf20Sopenharmony_ci		btrfs_end_transaction(trans);
22618c2ecf20Sopenharmony_ci
22628c2ecf20Sopenharmony_ci		ret = btrfs_wait_for_commit(fs_info, transid);
22638c2ecf20Sopenharmony_ci		if (ret)
22648c2ecf20Sopenharmony_ci			return ret;
22658c2ecf20Sopenharmony_ci		goto again;
22668c2ecf20Sopenharmony_ci	}
22678c2ecf20Sopenharmony_ci
22688c2ecf20Sopenharmony_ci	if (do_chunk_alloc) {
22698c2ecf20Sopenharmony_ci		/*
22708c2ecf20Sopenharmony_ci		 * If we are changing raid levels, try to allocate a
22718c2ecf20Sopenharmony_ci		 * corresponding block group with the new raid level.
22728c2ecf20Sopenharmony_ci		 */
22738c2ecf20Sopenharmony_ci		alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
22748c2ecf20Sopenharmony_ci		if (alloc_flags != cache->flags) {
22758c2ecf20Sopenharmony_ci			ret = btrfs_chunk_alloc(trans, alloc_flags,
22768c2ecf20Sopenharmony_ci						CHUNK_ALLOC_FORCE);
22778c2ecf20Sopenharmony_ci			/*
22788c2ecf20Sopenharmony_ci			 * ENOSPC is allowed here, we may have enough space
22798c2ecf20Sopenharmony_ci			 * already allocated at the new raid level to carry on
22808c2ecf20Sopenharmony_ci			 */
22818c2ecf20Sopenharmony_ci			if (ret == -ENOSPC)
22828c2ecf20Sopenharmony_ci				ret = 0;
22838c2ecf20Sopenharmony_ci			if (ret < 0)
22848c2ecf20Sopenharmony_ci				goto out;
22858c2ecf20Sopenharmony_ci		}
22868c2ecf20Sopenharmony_ci	}
22878c2ecf20Sopenharmony_ci
22888c2ecf20Sopenharmony_ci	ret = inc_block_group_ro(cache, 0);
22898c2ecf20Sopenharmony_ci	if (!ret)
22908c2ecf20Sopenharmony_ci		goto out;
22918c2ecf20Sopenharmony_ci	if (ret == -ETXTBSY)
22928c2ecf20Sopenharmony_ci		goto unlock_out;
22938c2ecf20Sopenharmony_ci
22948c2ecf20Sopenharmony_ci	/*
22958c2ecf20Sopenharmony_ci	 * Skip chunk alloction if the bg is SYSTEM, this is to avoid system
22968c2ecf20Sopenharmony_ci	 * chunk allocation storm to exhaust the system chunk array.  Otherwise
22978c2ecf20Sopenharmony_ci	 * we still want to try our best to mark the block group read-only.
22988c2ecf20Sopenharmony_ci	 */
22998c2ecf20Sopenharmony_ci	if (!do_chunk_alloc && ret == -ENOSPC &&
23008c2ecf20Sopenharmony_ci	    (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM))
23018c2ecf20Sopenharmony_ci		goto unlock_out;
23028c2ecf20Sopenharmony_ci
23038c2ecf20Sopenharmony_ci	alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags);
23048c2ecf20Sopenharmony_ci	ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
23058c2ecf20Sopenharmony_ci	if (ret < 0)
23068c2ecf20Sopenharmony_ci		goto out;
23078c2ecf20Sopenharmony_ci	ret = inc_block_group_ro(cache, 0);
23088c2ecf20Sopenharmony_ci	if (ret == -ETXTBSY)
23098c2ecf20Sopenharmony_ci		goto unlock_out;
23108c2ecf20Sopenharmony_ciout:
23118c2ecf20Sopenharmony_ci	if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
23128c2ecf20Sopenharmony_ci		alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
23138c2ecf20Sopenharmony_ci		mutex_lock(&fs_info->chunk_mutex);
23148c2ecf20Sopenharmony_ci		check_system_chunk(trans, alloc_flags);
23158c2ecf20Sopenharmony_ci		mutex_unlock(&fs_info->chunk_mutex);
23168c2ecf20Sopenharmony_ci	}
23178c2ecf20Sopenharmony_ciunlock_out:
23188c2ecf20Sopenharmony_ci	mutex_unlock(&fs_info->ro_block_group_mutex);
23198c2ecf20Sopenharmony_ci
23208c2ecf20Sopenharmony_ci	btrfs_end_transaction(trans);
23218c2ecf20Sopenharmony_ci	return ret;
23228c2ecf20Sopenharmony_ci}
23238c2ecf20Sopenharmony_ci
23248c2ecf20Sopenharmony_civoid btrfs_dec_block_group_ro(struct btrfs_block_group *cache)
23258c2ecf20Sopenharmony_ci{
23268c2ecf20Sopenharmony_ci	struct btrfs_space_info *sinfo = cache->space_info;
23278c2ecf20Sopenharmony_ci	u64 num_bytes;
23288c2ecf20Sopenharmony_ci
23298c2ecf20Sopenharmony_ci	BUG_ON(!cache->ro);
23308c2ecf20Sopenharmony_ci
23318c2ecf20Sopenharmony_ci	spin_lock(&sinfo->lock);
23328c2ecf20Sopenharmony_ci	spin_lock(&cache->lock);
23338c2ecf20Sopenharmony_ci	if (!--cache->ro) {
23348c2ecf20Sopenharmony_ci		num_bytes = cache->length - cache->reserved -
23358c2ecf20Sopenharmony_ci			    cache->pinned - cache->bytes_super - cache->used;
23368c2ecf20Sopenharmony_ci		sinfo->bytes_readonly -= num_bytes;
23378c2ecf20Sopenharmony_ci		list_del_init(&cache->ro_list);
23388c2ecf20Sopenharmony_ci	}
23398c2ecf20Sopenharmony_ci	spin_unlock(&cache->lock);
23408c2ecf20Sopenharmony_ci	spin_unlock(&sinfo->lock);
23418c2ecf20Sopenharmony_ci}
23428c2ecf20Sopenharmony_ci
23438c2ecf20Sopenharmony_cistatic int update_block_group_item(struct btrfs_trans_handle *trans,
23448c2ecf20Sopenharmony_ci				   struct btrfs_path *path,
23458c2ecf20Sopenharmony_ci				   struct btrfs_block_group *cache)
23468c2ecf20Sopenharmony_ci{
23478c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
23488c2ecf20Sopenharmony_ci	int ret;
23498c2ecf20Sopenharmony_ci	struct btrfs_root *root = fs_info->extent_root;
23508c2ecf20Sopenharmony_ci	unsigned long bi;
23518c2ecf20Sopenharmony_ci	struct extent_buffer *leaf;
23528c2ecf20Sopenharmony_ci	struct btrfs_block_group_item bgi;
23538c2ecf20Sopenharmony_ci	struct btrfs_key key;
23548c2ecf20Sopenharmony_ci
23558c2ecf20Sopenharmony_ci	key.objectid = cache->start;
23568c2ecf20Sopenharmony_ci	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
23578c2ecf20Sopenharmony_ci	key.offset = cache->length;
23588c2ecf20Sopenharmony_ci
23598c2ecf20Sopenharmony_ci	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
23608c2ecf20Sopenharmony_ci	if (ret) {
23618c2ecf20Sopenharmony_ci		if (ret > 0)
23628c2ecf20Sopenharmony_ci			ret = -ENOENT;
23638c2ecf20Sopenharmony_ci		goto fail;
23648c2ecf20Sopenharmony_ci	}
23658c2ecf20Sopenharmony_ci
23668c2ecf20Sopenharmony_ci	leaf = path->nodes[0];
23678c2ecf20Sopenharmony_ci	bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
23688c2ecf20Sopenharmony_ci	btrfs_set_stack_block_group_used(&bgi, cache->used);
23698c2ecf20Sopenharmony_ci	btrfs_set_stack_block_group_chunk_objectid(&bgi,
23708c2ecf20Sopenharmony_ci			BTRFS_FIRST_CHUNK_TREE_OBJECTID);
23718c2ecf20Sopenharmony_ci	btrfs_set_stack_block_group_flags(&bgi, cache->flags);
23728c2ecf20Sopenharmony_ci	write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
23738c2ecf20Sopenharmony_ci	btrfs_mark_buffer_dirty(leaf);
23748c2ecf20Sopenharmony_cifail:
23758c2ecf20Sopenharmony_ci	btrfs_release_path(path);
23768c2ecf20Sopenharmony_ci	return ret;
23778c2ecf20Sopenharmony_ci
23788c2ecf20Sopenharmony_ci}
23798c2ecf20Sopenharmony_ci
23808c2ecf20Sopenharmony_cistatic int cache_save_setup(struct btrfs_block_group *block_group,
23818c2ecf20Sopenharmony_ci			    struct btrfs_trans_handle *trans,
23828c2ecf20Sopenharmony_ci			    struct btrfs_path *path)
23838c2ecf20Sopenharmony_ci{
23848c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = block_group->fs_info;
23858c2ecf20Sopenharmony_ci	struct btrfs_root *root = fs_info->tree_root;
23868c2ecf20Sopenharmony_ci	struct inode *inode = NULL;
23878c2ecf20Sopenharmony_ci	struct extent_changeset *data_reserved = NULL;
23888c2ecf20Sopenharmony_ci	u64 alloc_hint = 0;
23898c2ecf20Sopenharmony_ci	int dcs = BTRFS_DC_ERROR;
23908c2ecf20Sopenharmony_ci	u64 num_pages = 0;
23918c2ecf20Sopenharmony_ci	int retries = 0;
23928c2ecf20Sopenharmony_ci	int ret = 0;
23938c2ecf20Sopenharmony_ci
23948c2ecf20Sopenharmony_ci	/*
23958c2ecf20Sopenharmony_ci	 * If this block group is smaller than 100 megs don't bother caching the
23968c2ecf20Sopenharmony_ci	 * block group.
23978c2ecf20Sopenharmony_ci	 */
23988c2ecf20Sopenharmony_ci	if (block_group->length < (100 * SZ_1M)) {
23998c2ecf20Sopenharmony_ci		spin_lock(&block_group->lock);
24008c2ecf20Sopenharmony_ci		block_group->disk_cache_state = BTRFS_DC_WRITTEN;
24018c2ecf20Sopenharmony_ci		spin_unlock(&block_group->lock);
24028c2ecf20Sopenharmony_ci		return 0;
24038c2ecf20Sopenharmony_ci	}
24048c2ecf20Sopenharmony_ci
24058c2ecf20Sopenharmony_ci	if (TRANS_ABORTED(trans))
24068c2ecf20Sopenharmony_ci		return 0;
24078c2ecf20Sopenharmony_ciagain:
24088c2ecf20Sopenharmony_ci	inode = lookup_free_space_inode(block_group, path);
24098c2ecf20Sopenharmony_ci	if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
24108c2ecf20Sopenharmony_ci		ret = PTR_ERR(inode);
24118c2ecf20Sopenharmony_ci		btrfs_release_path(path);
24128c2ecf20Sopenharmony_ci		goto out;
24138c2ecf20Sopenharmony_ci	}
24148c2ecf20Sopenharmony_ci
24158c2ecf20Sopenharmony_ci	if (IS_ERR(inode)) {
24168c2ecf20Sopenharmony_ci		BUG_ON(retries);
24178c2ecf20Sopenharmony_ci		retries++;
24188c2ecf20Sopenharmony_ci
24198c2ecf20Sopenharmony_ci		if (block_group->ro)
24208c2ecf20Sopenharmony_ci			goto out_free;
24218c2ecf20Sopenharmony_ci
24228c2ecf20Sopenharmony_ci		ret = create_free_space_inode(trans, block_group, path);
24238c2ecf20Sopenharmony_ci		if (ret)
24248c2ecf20Sopenharmony_ci			goto out_free;
24258c2ecf20Sopenharmony_ci		goto again;
24268c2ecf20Sopenharmony_ci	}
24278c2ecf20Sopenharmony_ci
24288c2ecf20Sopenharmony_ci	/*
24298c2ecf20Sopenharmony_ci	 * We want to set the generation to 0, that way if anything goes wrong
24308c2ecf20Sopenharmony_ci	 * from here on out we know not to trust this cache when we load up next
24318c2ecf20Sopenharmony_ci	 * time.
24328c2ecf20Sopenharmony_ci	 */
24338c2ecf20Sopenharmony_ci	BTRFS_I(inode)->generation = 0;
24348c2ecf20Sopenharmony_ci	ret = btrfs_update_inode(trans, root, inode);
24358c2ecf20Sopenharmony_ci	if (ret) {
24368c2ecf20Sopenharmony_ci		/*
24378c2ecf20Sopenharmony_ci		 * So theoretically we could recover from this, simply set the
24388c2ecf20Sopenharmony_ci		 * super cache generation to 0 so we know to invalidate the
24398c2ecf20Sopenharmony_ci		 * cache, but then we'd have to keep track of the block groups
24408c2ecf20Sopenharmony_ci		 * that fail this way so we know we _have_ to reset this cache
24418c2ecf20Sopenharmony_ci		 * before the next commit or risk reading stale cache.  So to
24428c2ecf20Sopenharmony_ci		 * limit our exposure to horrible edge cases lets just abort the
24438c2ecf20Sopenharmony_ci		 * transaction, this only happens in really bad situations
24448c2ecf20Sopenharmony_ci		 * anyway.
24458c2ecf20Sopenharmony_ci		 */
24468c2ecf20Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
24478c2ecf20Sopenharmony_ci		goto out_put;
24488c2ecf20Sopenharmony_ci	}
24498c2ecf20Sopenharmony_ci	WARN_ON(ret);
24508c2ecf20Sopenharmony_ci
24518c2ecf20Sopenharmony_ci	/* We've already setup this transaction, go ahead and exit */
24528c2ecf20Sopenharmony_ci	if (block_group->cache_generation == trans->transid &&
24538c2ecf20Sopenharmony_ci	    i_size_read(inode)) {
24548c2ecf20Sopenharmony_ci		dcs = BTRFS_DC_SETUP;
24558c2ecf20Sopenharmony_ci		goto out_put;
24568c2ecf20Sopenharmony_ci	}
24578c2ecf20Sopenharmony_ci
24588c2ecf20Sopenharmony_ci	if (i_size_read(inode) > 0) {
24598c2ecf20Sopenharmony_ci		ret = btrfs_check_trunc_cache_free_space(fs_info,
24608c2ecf20Sopenharmony_ci					&fs_info->global_block_rsv);
24618c2ecf20Sopenharmony_ci		if (ret)
24628c2ecf20Sopenharmony_ci			goto out_put;
24638c2ecf20Sopenharmony_ci
24648c2ecf20Sopenharmony_ci		ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
24658c2ecf20Sopenharmony_ci		if (ret)
24668c2ecf20Sopenharmony_ci			goto out_put;
24678c2ecf20Sopenharmony_ci	}
24688c2ecf20Sopenharmony_ci
24698c2ecf20Sopenharmony_ci	spin_lock(&block_group->lock);
24708c2ecf20Sopenharmony_ci	if (block_group->cached != BTRFS_CACHE_FINISHED ||
24718c2ecf20Sopenharmony_ci	    !btrfs_test_opt(fs_info, SPACE_CACHE)) {
24728c2ecf20Sopenharmony_ci		/*
24738c2ecf20Sopenharmony_ci		 * don't bother trying to write stuff out _if_
24748c2ecf20Sopenharmony_ci		 * a) we're not cached,
24758c2ecf20Sopenharmony_ci		 * b) we're with nospace_cache mount option,
24768c2ecf20Sopenharmony_ci		 * c) we're with v2 space_cache (FREE_SPACE_TREE).
24778c2ecf20Sopenharmony_ci		 */
24788c2ecf20Sopenharmony_ci		dcs = BTRFS_DC_WRITTEN;
24798c2ecf20Sopenharmony_ci		spin_unlock(&block_group->lock);
24808c2ecf20Sopenharmony_ci		goto out_put;
24818c2ecf20Sopenharmony_ci	}
24828c2ecf20Sopenharmony_ci	spin_unlock(&block_group->lock);
24838c2ecf20Sopenharmony_ci
24848c2ecf20Sopenharmony_ci	/*
24858c2ecf20Sopenharmony_ci	 * We hit an ENOSPC when setting up the cache in this transaction, just
24868c2ecf20Sopenharmony_ci	 * skip doing the setup, we've already cleared the cache so we're safe.
24878c2ecf20Sopenharmony_ci	 */
24888c2ecf20Sopenharmony_ci	if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
24898c2ecf20Sopenharmony_ci		ret = -ENOSPC;
24908c2ecf20Sopenharmony_ci		goto out_put;
24918c2ecf20Sopenharmony_ci	}
24928c2ecf20Sopenharmony_ci
24938c2ecf20Sopenharmony_ci	/*
24948c2ecf20Sopenharmony_ci	 * Try to preallocate enough space based on how big the block group is.
24958c2ecf20Sopenharmony_ci	 * Keep in mind this has to include any pinned space which could end up
24968c2ecf20Sopenharmony_ci	 * taking up quite a bit since it's not folded into the other space
24978c2ecf20Sopenharmony_ci	 * cache.
24988c2ecf20Sopenharmony_ci	 */
24998c2ecf20Sopenharmony_ci	num_pages = div_u64(block_group->length, SZ_256M);
25008c2ecf20Sopenharmony_ci	if (!num_pages)
25018c2ecf20Sopenharmony_ci		num_pages = 1;
25028c2ecf20Sopenharmony_ci
25038c2ecf20Sopenharmony_ci	num_pages *= 16;
25048c2ecf20Sopenharmony_ci	num_pages *= PAGE_SIZE;
25058c2ecf20Sopenharmony_ci
25068c2ecf20Sopenharmony_ci	ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, 0,
25078c2ecf20Sopenharmony_ci					  num_pages);
25088c2ecf20Sopenharmony_ci	if (ret)
25098c2ecf20Sopenharmony_ci		goto out_put;
25108c2ecf20Sopenharmony_ci
25118c2ecf20Sopenharmony_ci	ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
25128c2ecf20Sopenharmony_ci					      num_pages, num_pages,
25138c2ecf20Sopenharmony_ci					      &alloc_hint);
25148c2ecf20Sopenharmony_ci	/*
25158c2ecf20Sopenharmony_ci	 * Our cache requires contiguous chunks so that we don't modify a bunch
25168c2ecf20Sopenharmony_ci	 * of metadata or split extents when writing the cache out, which means
25178c2ecf20Sopenharmony_ci	 * we can enospc if we are heavily fragmented in addition to just normal
25188c2ecf20Sopenharmony_ci	 * out of space conditions.  So if we hit this just skip setting up any
25198c2ecf20Sopenharmony_ci	 * other block groups for this transaction, maybe we'll unpin enough
25208c2ecf20Sopenharmony_ci	 * space the next time around.
25218c2ecf20Sopenharmony_ci	 */
25228c2ecf20Sopenharmony_ci	if (!ret)
25238c2ecf20Sopenharmony_ci		dcs = BTRFS_DC_SETUP;
25248c2ecf20Sopenharmony_ci	else if (ret == -ENOSPC)
25258c2ecf20Sopenharmony_ci		set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
25268c2ecf20Sopenharmony_ci
25278c2ecf20Sopenharmony_ciout_put:
25288c2ecf20Sopenharmony_ci	iput(inode);
25298c2ecf20Sopenharmony_ciout_free:
25308c2ecf20Sopenharmony_ci	btrfs_release_path(path);
25318c2ecf20Sopenharmony_ciout:
25328c2ecf20Sopenharmony_ci	spin_lock(&block_group->lock);
25338c2ecf20Sopenharmony_ci	if (!ret && dcs == BTRFS_DC_SETUP)
25348c2ecf20Sopenharmony_ci		block_group->cache_generation = trans->transid;
25358c2ecf20Sopenharmony_ci	block_group->disk_cache_state = dcs;
25368c2ecf20Sopenharmony_ci	spin_unlock(&block_group->lock);
25378c2ecf20Sopenharmony_ci
25388c2ecf20Sopenharmony_ci	extent_changeset_free(data_reserved);
25398c2ecf20Sopenharmony_ci	return ret;
25408c2ecf20Sopenharmony_ci}
25418c2ecf20Sopenharmony_ci
25428c2ecf20Sopenharmony_ciint btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
25438c2ecf20Sopenharmony_ci{
25448c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
25458c2ecf20Sopenharmony_ci	struct btrfs_block_group *cache, *tmp;
25468c2ecf20Sopenharmony_ci	struct btrfs_transaction *cur_trans = trans->transaction;
25478c2ecf20Sopenharmony_ci	struct btrfs_path *path;
25488c2ecf20Sopenharmony_ci
25498c2ecf20Sopenharmony_ci	if (list_empty(&cur_trans->dirty_bgs) ||
25508c2ecf20Sopenharmony_ci	    !btrfs_test_opt(fs_info, SPACE_CACHE))
25518c2ecf20Sopenharmony_ci		return 0;
25528c2ecf20Sopenharmony_ci
25538c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
25548c2ecf20Sopenharmony_ci	if (!path)
25558c2ecf20Sopenharmony_ci		return -ENOMEM;
25568c2ecf20Sopenharmony_ci
25578c2ecf20Sopenharmony_ci	/* Could add new block groups, use _safe just in case */
25588c2ecf20Sopenharmony_ci	list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
25598c2ecf20Sopenharmony_ci				 dirty_list) {
25608c2ecf20Sopenharmony_ci		if (cache->disk_cache_state == BTRFS_DC_CLEAR)
25618c2ecf20Sopenharmony_ci			cache_save_setup(cache, trans, path);
25628c2ecf20Sopenharmony_ci	}
25638c2ecf20Sopenharmony_ci
25648c2ecf20Sopenharmony_ci	btrfs_free_path(path);
25658c2ecf20Sopenharmony_ci	return 0;
25668c2ecf20Sopenharmony_ci}
25678c2ecf20Sopenharmony_ci
25688c2ecf20Sopenharmony_ci/*
25698c2ecf20Sopenharmony_ci * Transaction commit does final block group cache writeback during a critical
25708c2ecf20Sopenharmony_ci * section where nothing is allowed to change the FS.  This is required in
25718c2ecf20Sopenharmony_ci * order for the cache to actually match the block group, but can introduce a
25728c2ecf20Sopenharmony_ci * lot of latency into the commit.
25738c2ecf20Sopenharmony_ci *
25748c2ecf20Sopenharmony_ci * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
25758c2ecf20Sopenharmony_ci * There's a chance we'll have to redo some of it if the block group changes
25768c2ecf20Sopenharmony_ci * again during the commit, but it greatly reduces the commit latency by
25778c2ecf20Sopenharmony_ci * getting rid of the easy block groups while we're still allowing others to
25788c2ecf20Sopenharmony_ci * join the commit.
25798c2ecf20Sopenharmony_ci */
25808c2ecf20Sopenharmony_ciint btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
25818c2ecf20Sopenharmony_ci{
25828c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
25838c2ecf20Sopenharmony_ci	struct btrfs_block_group *cache;
25848c2ecf20Sopenharmony_ci	struct btrfs_transaction *cur_trans = trans->transaction;
25858c2ecf20Sopenharmony_ci	int ret = 0;
25868c2ecf20Sopenharmony_ci	int should_put;
25878c2ecf20Sopenharmony_ci	struct btrfs_path *path = NULL;
25888c2ecf20Sopenharmony_ci	LIST_HEAD(dirty);
25898c2ecf20Sopenharmony_ci	struct list_head *io = &cur_trans->io_bgs;
25908c2ecf20Sopenharmony_ci	int loops = 0;
25918c2ecf20Sopenharmony_ci
25928c2ecf20Sopenharmony_ci	spin_lock(&cur_trans->dirty_bgs_lock);
25938c2ecf20Sopenharmony_ci	if (list_empty(&cur_trans->dirty_bgs)) {
25948c2ecf20Sopenharmony_ci		spin_unlock(&cur_trans->dirty_bgs_lock);
25958c2ecf20Sopenharmony_ci		return 0;
25968c2ecf20Sopenharmony_ci	}
25978c2ecf20Sopenharmony_ci	list_splice_init(&cur_trans->dirty_bgs, &dirty);
25988c2ecf20Sopenharmony_ci	spin_unlock(&cur_trans->dirty_bgs_lock);
25998c2ecf20Sopenharmony_ci
26008c2ecf20Sopenharmony_ciagain:
26018c2ecf20Sopenharmony_ci	/* Make sure all the block groups on our dirty list actually exist */
26028c2ecf20Sopenharmony_ci	btrfs_create_pending_block_groups(trans);
26038c2ecf20Sopenharmony_ci
26048c2ecf20Sopenharmony_ci	if (!path) {
26058c2ecf20Sopenharmony_ci		path = btrfs_alloc_path();
26068c2ecf20Sopenharmony_ci		if (!path) {
26078c2ecf20Sopenharmony_ci			ret = -ENOMEM;
26088c2ecf20Sopenharmony_ci			goto out;
26098c2ecf20Sopenharmony_ci		}
26108c2ecf20Sopenharmony_ci	}
26118c2ecf20Sopenharmony_ci
26128c2ecf20Sopenharmony_ci	/*
26138c2ecf20Sopenharmony_ci	 * cache_write_mutex is here only to save us from balance or automatic
26148c2ecf20Sopenharmony_ci	 * removal of empty block groups deleting this block group while we are
26158c2ecf20Sopenharmony_ci	 * writing out the cache
26168c2ecf20Sopenharmony_ci	 */
26178c2ecf20Sopenharmony_ci	mutex_lock(&trans->transaction->cache_write_mutex);
26188c2ecf20Sopenharmony_ci	while (!list_empty(&dirty)) {
26198c2ecf20Sopenharmony_ci		bool drop_reserve = true;
26208c2ecf20Sopenharmony_ci
26218c2ecf20Sopenharmony_ci		cache = list_first_entry(&dirty, struct btrfs_block_group,
26228c2ecf20Sopenharmony_ci					 dirty_list);
26238c2ecf20Sopenharmony_ci		/*
26248c2ecf20Sopenharmony_ci		 * This can happen if something re-dirties a block group that
26258c2ecf20Sopenharmony_ci		 * is already under IO.  Just wait for it to finish and then do
26268c2ecf20Sopenharmony_ci		 * it all again
26278c2ecf20Sopenharmony_ci		 */
26288c2ecf20Sopenharmony_ci		if (!list_empty(&cache->io_list)) {
26298c2ecf20Sopenharmony_ci			list_del_init(&cache->io_list);
26308c2ecf20Sopenharmony_ci			btrfs_wait_cache_io(trans, cache, path);
26318c2ecf20Sopenharmony_ci			btrfs_put_block_group(cache);
26328c2ecf20Sopenharmony_ci		}
26338c2ecf20Sopenharmony_ci
26348c2ecf20Sopenharmony_ci
26358c2ecf20Sopenharmony_ci		/*
26368c2ecf20Sopenharmony_ci		 * btrfs_wait_cache_io uses the cache->dirty_list to decide if
26378c2ecf20Sopenharmony_ci		 * it should update the cache_state.  Don't delete until after
26388c2ecf20Sopenharmony_ci		 * we wait.
26398c2ecf20Sopenharmony_ci		 *
26408c2ecf20Sopenharmony_ci		 * Since we're not running in the commit critical section
26418c2ecf20Sopenharmony_ci		 * we need the dirty_bgs_lock to protect from update_block_group
26428c2ecf20Sopenharmony_ci		 */
26438c2ecf20Sopenharmony_ci		spin_lock(&cur_trans->dirty_bgs_lock);
26448c2ecf20Sopenharmony_ci		list_del_init(&cache->dirty_list);
26458c2ecf20Sopenharmony_ci		spin_unlock(&cur_trans->dirty_bgs_lock);
26468c2ecf20Sopenharmony_ci
26478c2ecf20Sopenharmony_ci		should_put = 1;
26488c2ecf20Sopenharmony_ci
26498c2ecf20Sopenharmony_ci		cache_save_setup(cache, trans, path);
26508c2ecf20Sopenharmony_ci
26518c2ecf20Sopenharmony_ci		if (cache->disk_cache_state == BTRFS_DC_SETUP) {
26528c2ecf20Sopenharmony_ci			cache->io_ctl.inode = NULL;
26538c2ecf20Sopenharmony_ci			ret = btrfs_write_out_cache(trans, cache, path);
26548c2ecf20Sopenharmony_ci			if (ret == 0 && cache->io_ctl.inode) {
26558c2ecf20Sopenharmony_ci				should_put = 0;
26568c2ecf20Sopenharmony_ci
26578c2ecf20Sopenharmony_ci				/*
26588c2ecf20Sopenharmony_ci				 * The cache_write_mutex is protecting the
26598c2ecf20Sopenharmony_ci				 * io_list, also refer to the definition of
26608c2ecf20Sopenharmony_ci				 * btrfs_transaction::io_bgs for more details
26618c2ecf20Sopenharmony_ci				 */
26628c2ecf20Sopenharmony_ci				list_add_tail(&cache->io_list, io);
26638c2ecf20Sopenharmony_ci			} else {
26648c2ecf20Sopenharmony_ci				/*
26658c2ecf20Sopenharmony_ci				 * If we failed to write the cache, the
26668c2ecf20Sopenharmony_ci				 * generation will be bad and life goes on
26678c2ecf20Sopenharmony_ci				 */
26688c2ecf20Sopenharmony_ci				ret = 0;
26698c2ecf20Sopenharmony_ci			}
26708c2ecf20Sopenharmony_ci		}
26718c2ecf20Sopenharmony_ci		if (!ret) {
26728c2ecf20Sopenharmony_ci			ret = update_block_group_item(trans, path, cache);
26738c2ecf20Sopenharmony_ci			/*
26748c2ecf20Sopenharmony_ci			 * Our block group might still be attached to the list
26758c2ecf20Sopenharmony_ci			 * of new block groups in the transaction handle of some
26768c2ecf20Sopenharmony_ci			 * other task (struct btrfs_trans_handle->new_bgs). This
26778c2ecf20Sopenharmony_ci			 * means its block group item isn't yet in the extent
26788c2ecf20Sopenharmony_ci			 * tree. If this happens ignore the error, as we will
26798c2ecf20Sopenharmony_ci			 * try again later in the critical section of the
26808c2ecf20Sopenharmony_ci			 * transaction commit.
26818c2ecf20Sopenharmony_ci			 */
26828c2ecf20Sopenharmony_ci			if (ret == -ENOENT) {
26838c2ecf20Sopenharmony_ci				ret = 0;
26848c2ecf20Sopenharmony_ci				spin_lock(&cur_trans->dirty_bgs_lock);
26858c2ecf20Sopenharmony_ci				if (list_empty(&cache->dirty_list)) {
26868c2ecf20Sopenharmony_ci					list_add_tail(&cache->dirty_list,
26878c2ecf20Sopenharmony_ci						      &cur_trans->dirty_bgs);
26888c2ecf20Sopenharmony_ci					btrfs_get_block_group(cache);
26898c2ecf20Sopenharmony_ci					drop_reserve = false;
26908c2ecf20Sopenharmony_ci				}
26918c2ecf20Sopenharmony_ci				spin_unlock(&cur_trans->dirty_bgs_lock);
26928c2ecf20Sopenharmony_ci			} else if (ret) {
26938c2ecf20Sopenharmony_ci				btrfs_abort_transaction(trans, ret);
26948c2ecf20Sopenharmony_ci			}
26958c2ecf20Sopenharmony_ci		}
26968c2ecf20Sopenharmony_ci
26978c2ecf20Sopenharmony_ci		/* If it's not on the io list, we need to put the block group */
26988c2ecf20Sopenharmony_ci		if (should_put)
26998c2ecf20Sopenharmony_ci			btrfs_put_block_group(cache);
27008c2ecf20Sopenharmony_ci		if (drop_reserve)
27018c2ecf20Sopenharmony_ci			btrfs_delayed_refs_rsv_release(fs_info, 1);
27028c2ecf20Sopenharmony_ci		/*
27038c2ecf20Sopenharmony_ci		 * Avoid blocking other tasks for too long. It might even save
27048c2ecf20Sopenharmony_ci		 * us from writing caches for block groups that are going to be
27058c2ecf20Sopenharmony_ci		 * removed.
27068c2ecf20Sopenharmony_ci		 */
27078c2ecf20Sopenharmony_ci		mutex_unlock(&trans->transaction->cache_write_mutex);
27088c2ecf20Sopenharmony_ci		if (ret)
27098c2ecf20Sopenharmony_ci			goto out;
27108c2ecf20Sopenharmony_ci		mutex_lock(&trans->transaction->cache_write_mutex);
27118c2ecf20Sopenharmony_ci	}
27128c2ecf20Sopenharmony_ci	mutex_unlock(&trans->transaction->cache_write_mutex);
27138c2ecf20Sopenharmony_ci
27148c2ecf20Sopenharmony_ci	/*
27158c2ecf20Sopenharmony_ci	 * Go through delayed refs for all the stuff we've just kicked off
27168c2ecf20Sopenharmony_ci	 * and then loop back (just once)
27178c2ecf20Sopenharmony_ci	 */
27188c2ecf20Sopenharmony_ci	if (!ret)
27198c2ecf20Sopenharmony_ci		ret = btrfs_run_delayed_refs(trans, 0);
27208c2ecf20Sopenharmony_ci	if (!ret && loops == 0) {
27218c2ecf20Sopenharmony_ci		loops++;
27228c2ecf20Sopenharmony_ci		spin_lock(&cur_trans->dirty_bgs_lock);
27238c2ecf20Sopenharmony_ci		list_splice_init(&cur_trans->dirty_bgs, &dirty);
27248c2ecf20Sopenharmony_ci		/*
27258c2ecf20Sopenharmony_ci		 * dirty_bgs_lock protects us from concurrent block group
27268c2ecf20Sopenharmony_ci		 * deletes too (not just cache_write_mutex).
27278c2ecf20Sopenharmony_ci		 */
27288c2ecf20Sopenharmony_ci		if (!list_empty(&dirty)) {
27298c2ecf20Sopenharmony_ci			spin_unlock(&cur_trans->dirty_bgs_lock);
27308c2ecf20Sopenharmony_ci			goto again;
27318c2ecf20Sopenharmony_ci		}
27328c2ecf20Sopenharmony_ci		spin_unlock(&cur_trans->dirty_bgs_lock);
27338c2ecf20Sopenharmony_ci	}
27348c2ecf20Sopenharmony_ciout:
27358c2ecf20Sopenharmony_ci	if (ret < 0) {
27368c2ecf20Sopenharmony_ci		spin_lock(&cur_trans->dirty_bgs_lock);
27378c2ecf20Sopenharmony_ci		list_splice_init(&dirty, &cur_trans->dirty_bgs);
27388c2ecf20Sopenharmony_ci		spin_unlock(&cur_trans->dirty_bgs_lock);
27398c2ecf20Sopenharmony_ci		btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
27408c2ecf20Sopenharmony_ci	}
27418c2ecf20Sopenharmony_ci
27428c2ecf20Sopenharmony_ci	btrfs_free_path(path);
27438c2ecf20Sopenharmony_ci	return ret;
27448c2ecf20Sopenharmony_ci}
27458c2ecf20Sopenharmony_ci
27468c2ecf20Sopenharmony_ciint btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
27478c2ecf20Sopenharmony_ci{
27488c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
27498c2ecf20Sopenharmony_ci	struct btrfs_block_group *cache;
27508c2ecf20Sopenharmony_ci	struct btrfs_transaction *cur_trans = trans->transaction;
27518c2ecf20Sopenharmony_ci	int ret = 0;
27528c2ecf20Sopenharmony_ci	int should_put;
27538c2ecf20Sopenharmony_ci	struct btrfs_path *path;
27548c2ecf20Sopenharmony_ci	struct list_head *io = &cur_trans->io_bgs;
27558c2ecf20Sopenharmony_ci
27568c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
27578c2ecf20Sopenharmony_ci	if (!path)
27588c2ecf20Sopenharmony_ci		return -ENOMEM;
27598c2ecf20Sopenharmony_ci
27608c2ecf20Sopenharmony_ci	/*
27618c2ecf20Sopenharmony_ci	 * Even though we are in the critical section of the transaction commit,
27628c2ecf20Sopenharmony_ci	 * we can still have concurrent tasks adding elements to this
27638c2ecf20Sopenharmony_ci	 * transaction's list of dirty block groups. These tasks correspond to
27648c2ecf20Sopenharmony_ci	 * endio free space workers started when writeback finishes for a
27658c2ecf20Sopenharmony_ci	 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
27668c2ecf20Sopenharmony_ci	 * allocate new block groups as a result of COWing nodes of the root
27678c2ecf20Sopenharmony_ci	 * tree when updating the free space inode. The writeback for the space
27688c2ecf20Sopenharmony_ci	 * caches is triggered by an earlier call to
27698c2ecf20Sopenharmony_ci	 * btrfs_start_dirty_block_groups() and iterations of the following
27708c2ecf20Sopenharmony_ci	 * loop.
27718c2ecf20Sopenharmony_ci	 * Also we want to do the cache_save_setup first and then run the
27728c2ecf20Sopenharmony_ci	 * delayed refs to make sure we have the best chance at doing this all
27738c2ecf20Sopenharmony_ci	 * in one shot.
27748c2ecf20Sopenharmony_ci	 */
27758c2ecf20Sopenharmony_ci	spin_lock(&cur_trans->dirty_bgs_lock);
27768c2ecf20Sopenharmony_ci	while (!list_empty(&cur_trans->dirty_bgs)) {
27778c2ecf20Sopenharmony_ci		cache = list_first_entry(&cur_trans->dirty_bgs,
27788c2ecf20Sopenharmony_ci					 struct btrfs_block_group,
27798c2ecf20Sopenharmony_ci					 dirty_list);
27808c2ecf20Sopenharmony_ci
27818c2ecf20Sopenharmony_ci		/*
27828c2ecf20Sopenharmony_ci		 * This can happen if cache_save_setup re-dirties a block group
27838c2ecf20Sopenharmony_ci		 * that is already under IO.  Just wait for it to finish and
27848c2ecf20Sopenharmony_ci		 * then do it all again
27858c2ecf20Sopenharmony_ci		 */
27868c2ecf20Sopenharmony_ci		if (!list_empty(&cache->io_list)) {
27878c2ecf20Sopenharmony_ci			spin_unlock(&cur_trans->dirty_bgs_lock);
27888c2ecf20Sopenharmony_ci			list_del_init(&cache->io_list);
27898c2ecf20Sopenharmony_ci			btrfs_wait_cache_io(trans, cache, path);
27908c2ecf20Sopenharmony_ci			btrfs_put_block_group(cache);
27918c2ecf20Sopenharmony_ci			spin_lock(&cur_trans->dirty_bgs_lock);
27928c2ecf20Sopenharmony_ci		}
27938c2ecf20Sopenharmony_ci
27948c2ecf20Sopenharmony_ci		/*
27958c2ecf20Sopenharmony_ci		 * Don't remove from the dirty list until after we've waited on
27968c2ecf20Sopenharmony_ci		 * any pending IO
27978c2ecf20Sopenharmony_ci		 */
27988c2ecf20Sopenharmony_ci		list_del_init(&cache->dirty_list);
27998c2ecf20Sopenharmony_ci		spin_unlock(&cur_trans->dirty_bgs_lock);
28008c2ecf20Sopenharmony_ci		should_put = 1;
28018c2ecf20Sopenharmony_ci
28028c2ecf20Sopenharmony_ci		cache_save_setup(cache, trans, path);
28038c2ecf20Sopenharmony_ci
28048c2ecf20Sopenharmony_ci		if (!ret)
28058c2ecf20Sopenharmony_ci			ret = btrfs_run_delayed_refs(trans,
28068c2ecf20Sopenharmony_ci						     (unsigned long) -1);
28078c2ecf20Sopenharmony_ci
28088c2ecf20Sopenharmony_ci		if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
28098c2ecf20Sopenharmony_ci			cache->io_ctl.inode = NULL;
28108c2ecf20Sopenharmony_ci			ret = btrfs_write_out_cache(trans, cache, path);
28118c2ecf20Sopenharmony_ci			if (ret == 0 && cache->io_ctl.inode) {
28128c2ecf20Sopenharmony_ci				should_put = 0;
28138c2ecf20Sopenharmony_ci				list_add_tail(&cache->io_list, io);
28148c2ecf20Sopenharmony_ci			} else {
28158c2ecf20Sopenharmony_ci				/*
28168c2ecf20Sopenharmony_ci				 * If we failed to write the cache, the
28178c2ecf20Sopenharmony_ci				 * generation will be bad and life goes on
28188c2ecf20Sopenharmony_ci				 */
28198c2ecf20Sopenharmony_ci				ret = 0;
28208c2ecf20Sopenharmony_ci			}
28218c2ecf20Sopenharmony_ci		}
28228c2ecf20Sopenharmony_ci		if (!ret) {
28238c2ecf20Sopenharmony_ci			ret = update_block_group_item(trans, path, cache);
28248c2ecf20Sopenharmony_ci			/*
28258c2ecf20Sopenharmony_ci			 * One of the free space endio workers might have
28268c2ecf20Sopenharmony_ci			 * created a new block group while updating a free space
28278c2ecf20Sopenharmony_ci			 * cache's inode (at inode.c:btrfs_finish_ordered_io())
28288c2ecf20Sopenharmony_ci			 * and hasn't released its transaction handle yet, in
28298c2ecf20Sopenharmony_ci			 * which case the new block group is still attached to
28308c2ecf20Sopenharmony_ci			 * its transaction handle and its creation has not
28318c2ecf20Sopenharmony_ci			 * finished yet (no block group item in the extent tree
28328c2ecf20Sopenharmony_ci			 * yet, etc). If this is the case, wait for all free
28338c2ecf20Sopenharmony_ci			 * space endio workers to finish and retry. This is a
28348c2ecf20Sopenharmony_ci			 * very rare case so no need for a more efficient and
28358c2ecf20Sopenharmony_ci			 * complex approach.
28368c2ecf20Sopenharmony_ci			 */
28378c2ecf20Sopenharmony_ci			if (ret == -ENOENT) {
28388c2ecf20Sopenharmony_ci				wait_event(cur_trans->writer_wait,
28398c2ecf20Sopenharmony_ci				   atomic_read(&cur_trans->num_writers) == 1);
28408c2ecf20Sopenharmony_ci				ret = update_block_group_item(trans, path, cache);
28418c2ecf20Sopenharmony_ci			}
28428c2ecf20Sopenharmony_ci			if (ret)
28438c2ecf20Sopenharmony_ci				btrfs_abort_transaction(trans, ret);
28448c2ecf20Sopenharmony_ci		}
28458c2ecf20Sopenharmony_ci
28468c2ecf20Sopenharmony_ci		/* If its not on the io list, we need to put the block group */
28478c2ecf20Sopenharmony_ci		if (should_put)
28488c2ecf20Sopenharmony_ci			btrfs_put_block_group(cache);
28498c2ecf20Sopenharmony_ci		btrfs_delayed_refs_rsv_release(fs_info, 1);
28508c2ecf20Sopenharmony_ci		spin_lock(&cur_trans->dirty_bgs_lock);
28518c2ecf20Sopenharmony_ci	}
28528c2ecf20Sopenharmony_ci	spin_unlock(&cur_trans->dirty_bgs_lock);
28538c2ecf20Sopenharmony_ci
28548c2ecf20Sopenharmony_ci	/*
28558c2ecf20Sopenharmony_ci	 * Refer to the definition of io_bgs member for details why it's safe
28568c2ecf20Sopenharmony_ci	 * to use it without any locking
28578c2ecf20Sopenharmony_ci	 */
28588c2ecf20Sopenharmony_ci	while (!list_empty(io)) {
28598c2ecf20Sopenharmony_ci		cache = list_first_entry(io, struct btrfs_block_group,
28608c2ecf20Sopenharmony_ci					 io_list);
28618c2ecf20Sopenharmony_ci		list_del_init(&cache->io_list);
28628c2ecf20Sopenharmony_ci		btrfs_wait_cache_io(trans, cache, path);
28638c2ecf20Sopenharmony_ci		btrfs_put_block_group(cache);
28648c2ecf20Sopenharmony_ci	}
28658c2ecf20Sopenharmony_ci
28668c2ecf20Sopenharmony_ci	btrfs_free_path(path);
28678c2ecf20Sopenharmony_ci	return ret;
28688c2ecf20Sopenharmony_ci}
28698c2ecf20Sopenharmony_ci
28708c2ecf20Sopenharmony_ciint btrfs_update_block_group(struct btrfs_trans_handle *trans,
28718c2ecf20Sopenharmony_ci			     u64 bytenr, u64 num_bytes, int alloc)
28728c2ecf20Sopenharmony_ci{
28738c2ecf20Sopenharmony_ci	struct btrfs_fs_info *info = trans->fs_info;
28748c2ecf20Sopenharmony_ci	struct btrfs_block_group *cache = NULL;
28758c2ecf20Sopenharmony_ci	u64 total = num_bytes;
28768c2ecf20Sopenharmony_ci	u64 old_val;
28778c2ecf20Sopenharmony_ci	u64 byte_in_group;
28788c2ecf20Sopenharmony_ci	int factor;
28798c2ecf20Sopenharmony_ci	int ret = 0;
28808c2ecf20Sopenharmony_ci
28818c2ecf20Sopenharmony_ci	/* Block accounting for super block */
28828c2ecf20Sopenharmony_ci	spin_lock(&info->delalloc_root_lock);
28838c2ecf20Sopenharmony_ci	old_val = btrfs_super_bytes_used(info->super_copy);
28848c2ecf20Sopenharmony_ci	if (alloc)
28858c2ecf20Sopenharmony_ci		old_val += num_bytes;
28868c2ecf20Sopenharmony_ci	else
28878c2ecf20Sopenharmony_ci		old_val -= num_bytes;
28888c2ecf20Sopenharmony_ci	btrfs_set_super_bytes_used(info->super_copy, old_val);
28898c2ecf20Sopenharmony_ci	spin_unlock(&info->delalloc_root_lock);
28908c2ecf20Sopenharmony_ci
28918c2ecf20Sopenharmony_ci	while (total) {
28928c2ecf20Sopenharmony_ci		cache = btrfs_lookup_block_group(info, bytenr);
28938c2ecf20Sopenharmony_ci		if (!cache) {
28948c2ecf20Sopenharmony_ci			ret = -ENOENT;
28958c2ecf20Sopenharmony_ci			break;
28968c2ecf20Sopenharmony_ci		}
28978c2ecf20Sopenharmony_ci		factor = btrfs_bg_type_to_factor(cache->flags);
28988c2ecf20Sopenharmony_ci
28998c2ecf20Sopenharmony_ci		/*
29008c2ecf20Sopenharmony_ci		 * If this block group has free space cache written out, we
29018c2ecf20Sopenharmony_ci		 * need to make sure to load it if we are removing space.  This
29028c2ecf20Sopenharmony_ci		 * is because we need the unpinning stage to actually add the
29038c2ecf20Sopenharmony_ci		 * space back to the block group, otherwise we will leak space.
29048c2ecf20Sopenharmony_ci		 */
29058c2ecf20Sopenharmony_ci		if (!alloc && !btrfs_block_group_done(cache))
29068c2ecf20Sopenharmony_ci			btrfs_cache_block_group(cache, 1);
29078c2ecf20Sopenharmony_ci
29088c2ecf20Sopenharmony_ci		byte_in_group = bytenr - cache->start;
29098c2ecf20Sopenharmony_ci		WARN_ON(byte_in_group > cache->length);
29108c2ecf20Sopenharmony_ci
29118c2ecf20Sopenharmony_ci		spin_lock(&cache->space_info->lock);
29128c2ecf20Sopenharmony_ci		spin_lock(&cache->lock);
29138c2ecf20Sopenharmony_ci
29148c2ecf20Sopenharmony_ci		if (btrfs_test_opt(info, SPACE_CACHE) &&
29158c2ecf20Sopenharmony_ci		    cache->disk_cache_state < BTRFS_DC_CLEAR)
29168c2ecf20Sopenharmony_ci			cache->disk_cache_state = BTRFS_DC_CLEAR;
29178c2ecf20Sopenharmony_ci
29188c2ecf20Sopenharmony_ci		old_val = cache->used;
29198c2ecf20Sopenharmony_ci		num_bytes = min(total, cache->length - byte_in_group);
29208c2ecf20Sopenharmony_ci		if (alloc) {
29218c2ecf20Sopenharmony_ci			old_val += num_bytes;
29228c2ecf20Sopenharmony_ci			cache->used = old_val;
29238c2ecf20Sopenharmony_ci			cache->reserved -= num_bytes;
29248c2ecf20Sopenharmony_ci			cache->space_info->bytes_reserved -= num_bytes;
29258c2ecf20Sopenharmony_ci			cache->space_info->bytes_used += num_bytes;
29268c2ecf20Sopenharmony_ci			cache->space_info->disk_used += num_bytes * factor;
29278c2ecf20Sopenharmony_ci			spin_unlock(&cache->lock);
29288c2ecf20Sopenharmony_ci			spin_unlock(&cache->space_info->lock);
29298c2ecf20Sopenharmony_ci		} else {
29308c2ecf20Sopenharmony_ci			old_val -= num_bytes;
29318c2ecf20Sopenharmony_ci			cache->used = old_val;
29328c2ecf20Sopenharmony_ci			cache->pinned += num_bytes;
29338c2ecf20Sopenharmony_ci			btrfs_space_info_update_bytes_pinned(info,
29348c2ecf20Sopenharmony_ci					cache->space_info, num_bytes);
29358c2ecf20Sopenharmony_ci			cache->space_info->bytes_used -= num_bytes;
29368c2ecf20Sopenharmony_ci			cache->space_info->disk_used -= num_bytes * factor;
29378c2ecf20Sopenharmony_ci			spin_unlock(&cache->lock);
29388c2ecf20Sopenharmony_ci			spin_unlock(&cache->space_info->lock);
29398c2ecf20Sopenharmony_ci
29408c2ecf20Sopenharmony_ci			__btrfs_mod_total_bytes_pinned(cache->space_info,
29418c2ecf20Sopenharmony_ci						       num_bytes);
29428c2ecf20Sopenharmony_ci			set_extent_dirty(&trans->transaction->pinned_extents,
29438c2ecf20Sopenharmony_ci					 bytenr, bytenr + num_bytes - 1,
29448c2ecf20Sopenharmony_ci					 GFP_NOFS | __GFP_NOFAIL);
29458c2ecf20Sopenharmony_ci		}
29468c2ecf20Sopenharmony_ci
29478c2ecf20Sopenharmony_ci		spin_lock(&trans->transaction->dirty_bgs_lock);
29488c2ecf20Sopenharmony_ci		if (list_empty(&cache->dirty_list)) {
29498c2ecf20Sopenharmony_ci			list_add_tail(&cache->dirty_list,
29508c2ecf20Sopenharmony_ci				      &trans->transaction->dirty_bgs);
29518c2ecf20Sopenharmony_ci			trans->delayed_ref_updates++;
29528c2ecf20Sopenharmony_ci			btrfs_get_block_group(cache);
29538c2ecf20Sopenharmony_ci		}
29548c2ecf20Sopenharmony_ci		spin_unlock(&trans->transaction->dirty_bgs_lock);
29558c2ecf20Sopenharmony_ci
29568c2ecf20Sopenharmony_ci		/*
29578c2ecf20Sopenharmony_ci		 * No longer have used bytes in this block group, queue it for
29588c2ecf20Sopenharmony_ci		 * deletion. We do this after adding the block group to the
29598c2ecf20Sopenharmony_ci		 * dirty list to avoid races between cleaner kthread and space
29608c2ecf20Sopenharmony_ci		 * cache writeout.
29618c2ecf20Sopenharmony_ci		 */
29628c2ecf20Sopenharmony_ci		if (!alloc && old_val == 0) {
29638c2ecf20Sopenharmony_ci			if (!btrfs_test_opt(info, DISCARD_ASYNC))
29648c2ecf20Sopenharmony_ci				btrfs_mark_bg_unused(cache);
29658c2ecf20Sopenharmony_ci		}
29668c2ecf20Sopenharmony_ci
29678c2ecf20Sopenharmony_ci		btrfs_put_block_group(cache);
29688c2ecf20Sopenharmony_ci		total -= num_bytes;
29698c2ecf20Sopenharmony_ci		bytenr += num_bytes;
29708c2ecf20Sopenharmony_ci	}
29718c2ecf20Sopenharmony_ci
29728c2ecf20Sopenharmony_ci	/* Modified block groups are accounted for in the delayed_refs_rsv. */
29738c2ecf20Sopenharmony_ci	btrfs_update_delayed_refs_rsv(trans);
29748c2ecf20Sopenharmony_ci	return ret;
29758c2ecf20Sopenharmony_ci}
29768c2ecf20Sopenharmony_ci
29778c2ecf20Sopenharmony_ci/**
29788c2ecf20Sopenharmony_ci * btrfs_add_reserved_bytes - update the block_group and space info counters
29798c2ecf20Sopenharmony_ci * @cache:	The cache we are manipulating
29808c2ecf20Sopenharmony_ci * @ram_bytes:  The number of bytes of file content, and will be same to
29818c2ecf20Sopenharmony_ci *              @num_bytes except for the compress path.
29828c2ecf20Sopenharmony_ci * @num_bytes:	The number of bytes in question
29838c2ecf20Sopenharmony_ci * @delalloc:   The blocks are allocated for the delalloc write
29848c2ecf20Sopenharmony_ci *
29858c2ecf20Sopenharmony_ci * This is called by the allocator when it reserves space. If this is a
29868c2ecf20Sopenharmony_ci * reservation and the block group has become read only we cannot make the
29878c2ecf20Sopenharmony_ci * reservation and return -EAGAIN, otherwise this function always succeeds.
29888c2ecf20Sopenharmony_ci */
29898c2ecf20Sopenharmony_ciint btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
29908c2ecf20Sopenharmony_ci			     u64 ram_bytes, u64 num_bytes, int delalloc)
29918c2ecf20Sopenharmony_ci{
29928c2ecf20Sopenharmony_ci	struct btrfs_space_info *space_info = cache->space_info;
29938c2ecf20Sopenharmony_ci	int ret = 0;
29948c2ecf20Sopenharmony_ci
29958c2ecf20Sopenharmony_ci	spin_lock(&space_info->lock);
29968c2ecf20Sopenharmony_ci	spin_lock(&cache->lock);
29978c2ecf20Sopenharmony_ci	if (cache->ro) {
29988c2ecf20Sopenharmony_ci		ret = -EAGAIN;
29998c2ecf20Sopenharmony_ci	} else {
30008c2ecf20Sopenharmony_ci		cache->reserved += num_bytes;
30018c2ecf20Sopenharmony_ci		space_info->bytes_reserved += num_bytes;
30028c2ecf20Sopenharmony_ci		trace_btrfs_space_reservation(cache->fs_info, "space_info",
30038c2ecf20Sopenharmony_ci					      space_info->flags, num_bytes, 1);
30048c2ecf20Sopenharmony_ci		btrfs_space_info_update_bytes_may_use(cache->fs_info,
30058c2ecf20Sopenharmony_ci						      space_info, -ram_bytes);
30068c2ecf20Sopenharmony_ci		if (delalloc)
30078c2ecf20Sopenharmony_ci			cache->delalloc_bytes += num_bytes;
30088c2ecf20Sopenharmony_ci
30098c2ecf20Sopenharmony_ci		/*
30108c2ecf20Sopenharmony_ci		 * Compression can use less space than we reserved, so wake
30118c2ecf20Sopenharmony_ci		 * tickets if that happens
30128c2ecf20Sopenharmony_ci		 */
30138c2ecf20Sopenharmony_ci		if (num_bytes < ram_bytes)
30148c2ecf20Sopenharmony_ci			btrfs_try_granting_tickets(cache->fs_info, space_info);
30158c2ecf20Sopenharmony_ci	}
30168c2ecf20Sopenharmony_ci	spin_unlock(&cache->lock);
30178c2ecf20Sopenharmony_ci	spin_unlock(&space_info->lock);
30188c2ecf20Sopenharmony_ci	return ret;
30198c2ecf20Sopenharmony_ci}
30208c2ecf20Sopenharmony_ci
30218c2ecf20Sopenharmony_ci/**
30228c2ecf20Sopenharmony_ci * btrfs_free_reserved_bytes - update the block_group and space info counters
30238c2ecf20Sopenharmony_ci * @cache:      The cache we are manipulating
30248c2ecf20Sopenharmony_ci * @num_bytes:  The number of bytes in question
30258c2ecf20Sopenharmony_ci * @delalloc:   The blocks are allocated for the delalloc write
30268c2ecf20Sopenharmony_ci *
30278c2ecf20Sopenharmony_ci * This is called by somebody who is freeing space that was never actually used
30288c2ecf20Sopenharmony_ci * on disk.  For example if you reserve some space for a new leaf in transaction
30298c2ecf20Sopenharmony_ci * A and before transaction A commits you free that leaf, you call this with
30308c2ecf20Sopenharmony_ci * reserve set to 0 in order to clear the reservation.
30318c2ecf20Sopenharmony_ci */
30328c2ecf20Sopenharmony_civoid btrfs_free_reserved_bytes(struct btrfs_block_group *cache,
30338c2ecf20Sopenharmony_ci			       u64 num_bytes, int delalloc)
30348c2ecf20Sopenharmony_ci{
30358c2ecf20Sopenharmony_ci	struct btrfs_space_info *space_info = cache->space_info;
30368c2ecf20Sopenharmony_ci
30378c2ecf20Sopenharmony_ci	spin_lock(&space_info->lock);
30388c2ecf20Sopenharmony_ci	spin_lock(&cache->lock);
30398c2ecf20Sopenharmony_ci	if (cache->ro)
30408c2ecf20Sopenharmony_ci		space_info->bytes_readonly += num_bytes;
30418c2ecf20Sopenharmony_ci	cache->reserved -= num_bytes;
30428c2ecf20Sopenharmony_ci	space_info->bytes_reserved -= num_bytes;
30438c2ecf20Sopenharmony_ci	space_info->max_extent_size = 0;
30448c2ecf20Sopenharmony_ci
30458c2ecf20Sopenharmony_ci	if (delalloc)
30468c2ecf20Sopenharmony_ci		cache->delalloc_bytes -= num_bytes;
30478c2ecf20Sopenharmony_ci	spin_unlock(&cache->lock);
30488c2ecf20Sopenharmony_ci
30498c2ecf20Sopenharmony_ci	btrfs_try_granting_tickets(cache->fs_info, space_info);
30508c2ecf20Sopenharmony_ci	spin_unlock(&space_info->lock);
30518c2ecf20Sopenharmony_ci}
30528c2ecf20Sopenharmony_ci
30538c2ecf20Sopenharmony_cistatic void force_metadata_allocation(struct btrfs_fs_info *info)
30548c2ecf20Sopenharmony_ci{
30558c2ecf20Sopenharmony_ci	struct list_head *head = &info->space_info;
30568c2ecf20Sopenharmony_ci	struct btrfs_space_info *found;
30578c2ecf20Sopenharmony_ci
30588c2ecf20Sopenharmony_ci	list_for_each_entry(found, head, list) {
30598c2ecf20Sopenharmony_ci		if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
30608c2ecf20Sopenharmony_ci			found->force_alloc = CHUNK_ALLOC_FORCE;
30618c2ecf20Sopenharmony_ci	}
30628c2ecf20Sopenharmony_ci}
30638c2ecf20Sopenharmony_ci
30648c2ecf20Sopenharmony_cistatic int should_alloc_chunk(struct btrfs_fs_info *fs_info,
30658c2ecf20Sopenharmony_ci			      struct btrfs_space_info *sinfo, int force)
30668c2ecf20Sopenharmony_ci{
30678c2ecf20Sopenharmony_ci	u64 bytes_used = btrfs_space_info_used(sinfo, false);
30688c2ecf20Sopenharmony_ci	u64 thresh;
30698c2ecf20Sopenharmony_ci
30708c2ecf20Sopenharmony_ci	if (force == CHUNK_ALLOC_FORCE)
30718c2ecf20Sopenharmony_ci		return 1;
30728c2ecf20Sopenharmony_ci
30738c2ecf20Sopenharmony_ci	/*
30748c2ecf20Sopenharmony_ci	 * in limited mode, we want to have some free space up to
30758c2ecf20Sopenharmony_ci	 * about 1% of the FS size.
30768c2ecf20Sopenharmony_ci	 */
30778c2ecf20Sopenharmony_ci	if (force == CHUNK_ALLOC_LIMITED) {
30788c2ecf20Sopenharmony_ci		thresh = btrfs_super_total_bytes(fs_info->super_copy);
30798c2ecf20Sopenharmony_ci		thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
30808c2ecf20Sopenharmony_ci
30818c2ecf20Sopenharmony_ci		if (sinfo->total_bytes - bytes_used < thresh)
30828c2ecf20Sopenharmony_ci			return 1;
30838c2ecf20Sopenharmony_ci	}
30848c2ecf20Sopenharmony_ci
30858c2ecf20Sopenharmony_ci	if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
30868c2ecf20Sopenharmony_ci		return 0;
30878c2ecf20Sopenharmony_ci	return 1;
30888c2ecf20Sopenharmony_ci}
30898c2ecf20Sopenharmony_ci
30908c2ecf20Sopenharmony_ciint btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
30918c2ecf20Sopenharmony_ci{
30928c2ecf20Sopenharmony_ci	u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type);
30938c2ecf20Sopenharmony_ci
30948c2ecf20Sopenharmony_ci	return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
30958c2ecf20Sopenharmony_ci}
30968c2ecf20Sopenharmony_ci
30978c2ecf20Sopenharmony_ci/*
30988c2ecf20Sopenharmony_ci * If force is CHUNK_ALLOC_FORCE:
30998c2ecf20Sopenharmony_ci *    - return 1 if it successfully allocates a chunk,
31008c2ecf20Sopenharmony_ci *    - return errors including -ENOSPC otherwise.
31018c2ecf20Sopenharmony_ci * If force is NOT CHUNK_ALLOC_FORCE:
31028c2ecf20Sopenharmony_ci *    - return 0 if it doesn't need to allocate a new chunk,
31038c2ecf20Sopenharmony_ci *    - return 1 if it successfully allocates a chunk,
31048c2ecf20Sopenharmony_ci *    - return errors including -ENOSPC otherwise.
31058c2ecf20Sopenharmony_ci */
31068c2ecf20Sopenharmony_ciint btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
31078c2ecf20Sopenharmony_ci		      enum btrfs_chunk_alloc_enum force)
31088c2ecf20Sopenharmony_ci{
31098c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
31108c2ecf20Sopenharmony_ci	struct btrfs_space_info *space_info;
31118c2ecf20Sopenharmony_ci	bool wait_for_alloc = false;
31128c2ecf20Sopenharmony_ci	bool should_alloc = false;
31138c2ecf20Sopenharmony_ci	int ret = 0;
31148c2ecf20Sopenharmony_ci
31158c2ecf20Sopenharmony_ci	/* Don't re-enter if we're already allocating a chunk */
31168c2ecf20Sopenharmony_ci	if (trans->allocating_chunk)
31178c2ecf20Sopenharmony_ci		return -ENOSPC;
31188c2ecf20Sopenharmony_ci
31198c2ecf20Sopenharmony_ci	space_info = btrfs_find_space_info(fs_info, flags);
31208c2ecf20Sopenharmony_ci	ASSERT(space_info);
31218c2ecf20Sopenharmony_ci
31228c2ecf20Sopenharmony_ci	do {
31238c2ecf20Sopenharmony_ci		spin_lock(&space_info->lock);
31248c2ecf20Sopenharmony_ci		if (force < space_info->force_alloc)
31258c2ecf20Sopenharmony_ci			force = space_info->force_alloc;
31268c2ecf20Sopenharmony_ci		should_alloc = should_alloc_chunk(fs_info, space_info, force);
31278c2ecf20Sopenharmony_ci		if (space_info->full) {
31288c2ecf20Sopenharmony_ci			/* No more free physical space */
31298c2ecf20Sopenharmony_ci			if (should_alloc)
31308c2ecf20Sopenharmony_ci				ret = -ENOSPC;
31318c2ecf20Sopenharmony_ci			else
31328c2ecf20Sopenharmony_ci				ret = 0;
31338c2ecf20Sopenharmony_ci			spin_unlock(&space_info->lock);
31348c2ecf20Sopenharmony_ci			return ret;
31358c2ecf20Sopenharmony_ci		} else if (!should_alloc) {
31368c2ecf20Sopenharmony_ci			spin_unlock(&space_info->lock);
31378c2ecf20Sopenharmony_ci			return 0;
31388c2ecf20Sopenharmony_ci		} else if (space_info->chunk_alloc) {
31398c2ecf20Sopenharmony_ci			/*
31408c2ecf20Sopenharmony_ci			 * Someone is already allocating, so we need to block
31418c2ecf20Sopenharmony_ci			 * until this someone is finished and then loop to
31428c2ecf20Sopenharmony_ci			 * recheck if we should continue with our allocation
31438c2ecf20Sopenharmony_ci			 * attempt.
31448c2ecf20Sopenharmony_ci			 */
31458c2ecf20Sopenharmony_ci			wait_for_alloc = true;
31468c2ecf20Sopenharmony_ci			force = CHUNK_ALLOC_NO_FORCE;
31478c2ecf20Sopenharmony_ci			spin_unlock(&space_info->lock);
31488c2ecf20Sopenharmony_ci			mutex_lock(&fs_info->chunk_mutex);
31498c2ecf20Sopenharmony_ci			mutex_unlock(&fs_info->chunk_mutex);
31508c2ecf20Sopenharmony_ci		} else {
31518c2ecf20Sopenharmony_ci			/* Proceed with allocation */
31528c2ecf20Sopenharmony_ci			space_info->chunk_alloc = 1;
31538c2ecf20Sopenharmony_ci			wait_for_alloc = false;
31548c2ecf20Sopenharmony_ci			spin_unlock(&space_info->lock);
31558c2ecf20Sopenharmony_ci		}
31568c2ecf20Sopenharmony_ci
31578c2ecf20Sopenharmony_ci		cond_resched();
31588c2ecf20Sopenharmony_ci	} while (wait_for_alloc);
31598c2ecf20Sopenharmony_ci
31608c2ecf20Sopenharmony_ci	mutex_lock(&fs_info->chunk_mutex);
31618c2ecf20Sopenharmony_ci	trans->allocating_chunk = true;
31628c2ecf20Sopenharmony_ci
31638c2ecf20Sopenharmony_ci	/*
31648c2ecf20Sopenharmony_ci	 * If we have mixed data/metadata chunks we want to make sure we keep
31658c2ecf20Sopenharmony_ci	 * allocating mixed chunks instead of individual chunks.
31668c2ecf20Sopenharmony_ci	 */
31678c2ecf20Sopenharmony_ci	if (btrfs_mixed_space_info(space_info))
31688c2ecf20Sopenharmony_ci		flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
31698c2ecf20Sopenharmony_ci
31708c2ecf20Sopenharmony_ci	/*
31718c2ecf20Sopenharmony_ci	 * if we're doing a data chunk, go ahead and make sure that
31728c2ecf20Sopenharmony_ci	 * we keep a reasonable number of metadata chunks allocated in the
31738c2ecf20Sopenharmony_ci	 * FS as well.
31748c2ecf20Sopenharmony_ci	 */
31758c2ecf20Sopenharmony_ci	if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
31768c2ecf20Sopenharmony_ci		fs_info->data_chunk_allocations++;
31778c2ecf20Sopenharmony_ci		if (!(fs_info->data_chunk_allocations %
31788c2ecf20Sopenharmony_ci		      fs_info->metadata_ratio))
31798c2ecf20Sopenharmony_ci			force_metadata_allocation(fs_info);
31808c2ecf20Sopenharmony_ci	}
31818c2ecf20Sopenharmony_ci
31828c2ecf20Sopenharmony_ci	/*
31838c2ecf20Sopenharmony_ci	 * Check if we have enough space in SYSTEM chunk because we may need
31848c2ecf20Sopenharmony_ci	 * to update devices.
31858c2ecf20Sopenharmony_ci	 */
31868c2ecf20Sopenharmony_ci	check_system_chunk(trans, flags);
31878c2ecf20Sopenharmony_ci
31888c2ecf20Sopenharmony_ci	ret = btrfs_alloc_chunk(trans, flags);
31898c2ecf20Sopenharmony_ci	trans->allocating_chunk = false;
31908c2ecf20Sopenharmony_ci
31918c2ecf20Sopenharmony_ci	spin_lock(&space_info->lock);
31928c2ecf20Sopenharmony_ci	if (ret < 0) {
31938c2ecf20Sopenharmony_ci		if (ret == -ENOSPC)
31948c2ecf20Sopenharmony_ci			space_info->full = 1;
31958c2ecf20Sopenharmony_ci		else
31968c2ecf20Sopenharmony_ci			goto out;
31978c2ecf20Sopenharmony_ci	} else {
31988c2ecf20Sopenharmony_ci		ret = 1;
31998c2ecf20Sopenharmony_ci		space_info->max_extent_size = 0;
32008c2ecf20Sopenharmony_ci	}
32018c2ecf20Sopenharmony_ci
32028c2ecf20Sopenharmony_ci	space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
32038c2ecf20Sopenharmony_ciout:
32048c2ecf20Sopenharmony_ci	space_info->chunk_alloc = 0;
32058c2ecf20Sopenharmony_ci	spin_unlock(&space_info->lock);
32068c2ecf20Sopenharmony_ci	mutex_unlock(&fs_info->chunk_mutex);
32078c2ecf20Sopenharmony_ci	/*
32088c2ecf20Sopenharmony_ci	 * When we allocate a new chunk we reserve space in the chunk block
32098c2ecf20Sopenharmony_ci	 * reserve to make sure we can COW nodes/leafs in the chunk tree or
32108c2ecf20Sopenharmony_ci	 * add new nodes/leafs to it if we end up needing to do it when
32118c2ecf20Sopenharmony_ci	 * inserting the chunk item and updating device items as part of the
32128c2ecf20Sopenharmony_ci	 * second phase of chunk allocation, performed by
32138c2ecf20Sopenharmony_ci	 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
32148c2ecf20Sopenharmony_ci	 * large number of new block groups to create in our transaction
32158c2ecf20Sopenharmony_ci	 * handle's new_bgs list to avoid exhausting the chunk block reserve
32168c2ecf20Sopenharmony_ci	 * in extreme cases - like having a single transaction create many new
32178c2ecf20Sopenharmony_ci	 * block groups when starting to write out the free space caches of all
32188c2ecf20Sopenharmony_ci	 * the block groups that were made dirty during the lifetime of the
32198c2ecf20Sopenharmony_ci	 * transaction.
32208c2ecf20Sopenharmony_ci	 */
32218c2ecf20Sopenharmony_ci	if (trans->chunk_bytes_reserved >= (u64)SZ_2M)
32228c2ecf20Sopenharmony_ci		btrfs_create_pending_block_groups(trans);
32238c2ecf20Sopenharmony_ci
32248c2ecf20Sopenharmony_ci	return ret;
32258c2ecf20Sopenharmony_ci}
32268c2ecf20Sopenharmony_ci
32278c2ecf20Sopenharmony_cistatic u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
32288c2ecf20Sopenharmony_ci{
32298c2ecf20Sopenharmony_ci	u64 num_dev;
32308c2ecf20Sopenharmony_ci
32318c2ecf20Sopenharmony_ci	num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max;
32328c2ecf20Sopenharmony_ci	if (!num_dev)
32338c2ecf20Sopenharmony_ci		num_dev = fs_info->fs_devices->rw_devices;
32348c2ecf20Sopenharmony_ci
32358c2ecf20Sopenharmony_ci	return num_dev;
32368c2ecf20Sopenharmony_ci}
32378c2ecf20Sopenharmony_ci
32388c2ecf20Sopenharmony_ci/*
32398c2ecf20Sopenharmony_ci * Reserve space in the system space for allocating or removing a chunk
32408c2ecf20Sopenharmony_ci */
32418c2ecf20Sopenharmony_civoid check_system_chunk(struct btrfs_trans_handle *trans, u64 type)
32428c2ecf20Sopenharmony_ci{
32438c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
32448c2ecf20Sopenharmony_ci	struct btrfs_space_info *info;
32458c2ecf20Sopenharmony_ci	u64 left;
32468c2ecf20Sopenharmony_ci	u64 thresh;
32478c2ecf20Sopenharmony_ci	int ret = 0;
32488c2ecf20Sopenharmony_ci	u64 num_devs;
32498c2ecf20Sopenharmony_ci
32508c2ecf20Sopenharmony_ci	/*
32518c2ecf20Sopenharmony_ci	 * Needed because we can end up allocating a system chunk and for an
32528c2ecf20Sopenharmony_ci	 * atomic and race free space reservation in the chunk block reserve.
32538c2ecf20Sopenharmony_ci	 */
32548c2ecf20Sopenharmony_ci	lockdep_assert_held(&fs_info->chunk_mutex);
32558c2ecf20Sopenharmony_ci
32568c2ecf20Sopenharmony_ci	info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
32578c2ecf20Sopenharmony_ci	spin_lock(&info->lock);
32588c2ecf20Sopenharmony_ci	left = info->total_bytes - btrfs_space_info_used(info, true);
32598c2ecf20Sopenharmony_ci	spin_unlock(&info->lock);
32608c2ecf20Sopenharmony_ci
32618c2ecf20Sopenharmony_ci	num_devs = get_profile_num_devs(fs_info, type);
32628c2ecf20Sopenharmony_ci
32638c2ecf20Sopenharmony_ci	/* num_devs device items to update and 1 chunk item to add or remove */
32648c2ecf20Sopenharmony_ci	thresh = btrfs_calc_metadata_size(fs_info, num_devs) +
32658c2ecf20Sopenharmony_ci		btrfs_calc_insert_metadata_size(fs_info, 1);
32668c2ecf20Sopenharmony_ci
32678c2ecf20Sopenharmony_ci	if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
32688c2ecf20Sopenharmony_ci		btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
32698c2ecf20Sopenharmony_ci			   left, thresh, type);
32708c2ecf20Sopenharmony_ci		btrfs_dump_space_info(fs_info, info, 0, 0);
32718c2ecf20Sopenharmony_ci	}
32728c2ecf20Sopenharmony_ci
32738c2ecf20Sopenharmony_ci	if (left < thresh) {
32748c2ecf20Sopenharmony_ci		u64 flags = btrfs_system_alloc_profile(fs_info);
32758c2ecf20Sopenharmony_ci
32768c2ecf20Sopenharmony_ci		/*
32778c2ecf20Sopenharmony_ci		 * Ignore failure to create system chunk. We might end up not
32788c2ecf20Sopenharmony_ci		 * needing it, as we might not need to COW all nodes/leafs from
32798c2ecf20Sopenharmony_ci		 * the paths we visit in the chunk tree (they were already COWed
32808c2ecf20Sopenharmony_ci		 * or created in the current transaction for example).
32818c2ecf20Sopenharmony_ci		 */
32828c2ecf20Sopenharmony_ci		ret = btrfs_alloc_chunk(trans, flags);
32838c2ecf20Sopenharmony_ci	}
32848c2ecf20Sopenharmony_ci
32858c2ecf20Sopenharmony_ci	if (!ret) {
32868c2ecf20Sopenharmony_ci		ret = btrfs_block_rsv_add(fs_info->chunk_root,
32878c2ecf20Sopenharmony_ci					  &fs_info->chunk_block_rsv,
32888c2ecf20Sopenharmony_ci					  thresh, BTRFS_RESERVE_NO_FLUSH);
32898c2ecf20Sopenharmony_ci		if (!ret)
32908c2ecf20Sopenharmony_ci			trans->chunk_bytes_reserved += thresh;
32918c2ecf20Sopenharmony_ci	}
32928c2ecf20Sopenharmony_ci}
32938c2ecf20Sopenharmony_ci
32948c2ecf20Sopenharmony_civoid btrfs_put_block_group_cache(struct btrfs_fs_info *info)
32958c2ecf20Sopenharmony_ci{
32968c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group;
32978c2ecf20Sopenharmony_ci	u64 last = 0;
32988c2ecf20Sopenharmony_ci
32998c2ecf20Sopenharmony_ci	while (1) {
33008c2ecf20Sopenharmony_ci		struct inode *inode;
33018c2ecf20Sopenharmony_ci
33028c2ecf20Sopenharmony_ci		block_group = btrfs_lookup_first_block_group(info, last);
33038c2ecf20Sopenharmony_ci		while (block_group) {
33048c2ecf20Sopenharmony_ci			btrfs_wait_block_group_cache_done(block_group);
33058c2ecf20Sopenharmony_ci			spin_lock(&block_group->lock);
33068c2ecf20Sopenharmony_ci			if (block_group->iref)
33078c2ecf20Sopenharmony_ci				break;
33088c2ecf20Sopenharmony_ci			spin_unlock(&block_group->lock);
33098c2ecf20Sopenharmony_ci			block_group = btrfs_next_block_group(block_group);
33108c2ecf20Sopenharmony_ci		}
33118c2ecf20Sopenharmony_ci		if (!block_group) {
33128c2ecf20Sopenharmony_ci			if (last == 0)
33138c2ecf20Sopenharmony_ci				break;
33148c2ecf20Sopenharmony_ci			last = 0;
33158c2ecf20Sopenharmony_ci			continue;
33168c2ecf20Sopenharmony_ci		}
33178c2ecf20Sopenharmony_ci
33188c2ecf20Sopenharmony_ci		inode = block_group->inode;
33198c2ecf20Sopenharmony_ci		block_group->iref = 0;
33208c2ecf20Sopenharmony_ci		block_group->inode = NULL;
33218c2ecf20Sopenharmony_ci		spin_unlock(&block_group->lock);
33228c2ecf20Sopenharmony_ci		ASSERT(block_group->io_ctl.inode == NULL);
33238c2ecf20Sopenharmony_ci		iput(inode);
33248c2ecf20Sopenharmony_ci		last = block_group->start + block_group->length;
33258c2ecf20Sopenharmony_ci		btrfs_put_block_group(block_group);
33268c2ecf20Sopenharmony_ci	}
33278c2ecf20Sopenharmony_ci}
33288c2ecf20Sopenharmony_ci
33298c2ecf20Sopenharmony_ci/*
33308c2ecf20Sopenharmony_ci * Must be called only after stopping all workers, since we could have block
33318c2ecf20Sopenharmony_ci * group caching kthreads running, and therefore they could race with us if we
33328c2ecf20Sopenharmony_ci * freed the block groups before stopping them.
33338c2ecf20Sopenharmony_ci */
33348c2ecf20Sopenharmony_ciint btrfs_free_block_groups(struct btrfs_fs_info *info)
33358c2ecf20Sopenharmony_ci{
33368c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group;
33378c2ecf20Sopenharmony_ci	struct btrfs_space_info *space_info;
33388c2ecf20Sopenharmony_ci	struct btrfs_caching_control *caching_ctl;
33398c2ecf20Sopenharmony_ci	struct rb_node *n;
33408c2ecf20Sopenharmony_ci
33418c2ecf20Sopenharmony_ci	down_write(&info->commit_root_sem);
33428c2ecf20Sopenharmony_ci	while (!list_empty(&info->caching_block_groups)) {
33438c2ecf20Sopenharmony_ci		caching_ctl = list_entry(info->caching_block_groups.next,
33448c2ecf20Sopenharmony_ci					 struct btrfs_caching_control, list);
33458c2ecf20Sopenharmony_ci		list_del(&caching_ctl->list);
33468c2ecf20Sopenharmony_ci		btrfs_put_caching_control(caching_ctl);
33478c2ecf20Sopenharmony_ci	}
33488c2ecf20Sopenharmony_ci	up_write(&info->commit_root_sem);
33498c2ecf20Sopenharmony_ci
33508c2ecf20Sopenharmony_ci	spin_lock(&info->unused_bgs_lock);
33518c2ecf20Sopenharmony_ci	while (!list_empty(&info->unused_bgs)) {
33528c2ecf20Sopenharmony_ci		block_group = list_first_entry(&info->unused_bgs,
33538c2ecf20Sopenharmony_ci					       struct btrfs_block_group,
33548c2ecf20Sopenharmony_ci					       bg_list);
33558c2ecf20Sopenharmony_ci		list_del_init(&block_group->bg_list);
33568c2ecf20Sopenharmony_ci		btrfs_put_block_group(block_group);
33578c2ecf20Sopenharmony_ci	}
33588c2ecf20Sopenharmony_ci	spin_unlock(&info->unused_bgs_lock);
33598c2ecf20Sopenharmony_ci
33608c2ecf20Sopenharmony_ci	spin_lock(&info->block_group_cache_lock);
33618c2ecf20Sopenharmony_ci	while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
33628c2ecf20Sopenharmony_ci		block_group = rb_entry(n, struct btrfs_block_group,
33638c2ecf20Sopenharmony_ci				       cache_node);
33648c2ecf20Sopenharmony_ci		rb_erase(&block_group->cache_node,
33658c2ecf20Sopenharmony_ci			 &info->block_group_cache_tree);
33668c2ecf20Sopenharmony_ci		RB_CLEAR_NODE(&block_group->cache_node);
33678c2ecf20Sopenharmony_ci		spin_unlock(&info->block_group_cache_lock);
33688c2ecf20Sopenharmony_ci
33698c2ecf20Sopenharmony_ci		down_write(&block_group->space_info->groups_sem);
33708c2ecf20Sopenharmony_ci		list_del(&block_group->list);
33718c2ecf20Sopenharmony_ci		up_write(&block_group->space_info->groups_sem);
33728c2ecf20Sopenharmony_ci
33738c2ecf20Sopenharmony_ci		/*
33748c2ecf20Sopenharmony_ci		 * We haven't cached this block group, which means we could
33758c2ecf20Sopenharmony_ci		 * possibly have excluded extents on this block group.
33768c2ecf20Sopenharmony_ci		 */
33778c2ecf20Sopenharmony_ci		if (block_group->cached == BTRFS_CACHE_NO ||
33788c2ecf20Sopenharmony_ci		    block_group->cached == BTRFS_CACHE_ERROR)
33798c2ecf20Sopenharmony_ci			btrfs_free_excluded_extents(block_group);
33808c2ecf20Sopenharmony_ci
33818c2ecf20Sopenharmony_ci		btrfs_remove_free_space_cache(block_group);
33828c2ecf20Sopenharmony_ci		ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
33838c2ecf20Sopenharmony_ci		ASSERT(list_empty(&block_group->dirty_list));
33848c2ecf20Sopenharmony_ci		ASSERT(list_empty(&block_group->io_list));
33858c2ecf20Sopenharmony_ci		ASSERT(list_empty(&block_group->bg_list));
33868c2ecf20Sopenharmony_ci		ASSERT(refcount_read(&block_group->refs) == 1);
33878c2ecf20Sopenharmony_ci		ASSERT(block_group->swap_extents == 0);
33888c2ecf20Sopenharmony_ci		btrfs_put_block_group(block_group);
33898c2ecf20Sopenharmony_ci
33908c2ecf20Sopenharmony_ci		spin_lock(&info->block_group_cache_lock);
33918c2ecf20Sopenharmony_ci	}
33928c2ecf20Sopenharmony_ci	spin_unlock(&info->block_group_cache_lock);
33938c2ecf20Sopenharmony_ci
33948c2ecf20Sopenharmony_ci	btrfs_release_global_block_rsv(info);
33958c2ecf20Sopenharmony_ci
33968c2ecf20Sopenharmony_ci	while (!list_empty(&info->space_info)) {
33978c2ecf20Sopenharmony_ci		space_info = list_entry(info->space_info.next,
33988c2ecf20Sopenharmony_ci					struct btrfs_space_info,
33998c2ecf20Sopenharmony_ci					list);
34008c2ecf20Sopenharmony_ci
34018c2ecf20Sopenharmony_ci		/*
34028c2ecf20Sopenharmony_ci		 * Do not hide this behind enospc_debug, this is actually
34038c2ecf20Sopenharmony_ci		 * important and indicates a real bug if this happens.
34048c2ecf20Sopenharmony_ci		 */
34058c2ecf20Sopenharmony_ci		if (WARN_ON(space_info->bytes_pinned > 0 ||
34068c2ecf20Sopenharmony_ci			    space_info->bytes_reserved > 0 ||
34078c2ecf20Sopenharmony_ci			    space_info->bytes_may_use > 0))
34088c2ecf20Sopenharmony_ci			btrfs_dump_space_info(info, space_info, 0, 0);
34098c2ecf20Sopenharmony_ci		WARN_ON(space_info->reclaim_size > 0);
34108c2ecf20Sopenharmony_ci		list_del(&space_info->list);
34118c2ecf20Sopenharmony_ci		btrfs_sysfs_remove_space_info(space_info);
34128c2ecf20Sopenharmony_ci	}
34138c2ecf20Sopenharmony_ci	return 0;
34148c2ecf20Sopenharmony_ci}
34158c2ecf20Sopenharmony_ci
34168c2ecf20Sopenharmony_civoid btrfs_freeze_block_group(struct btrfs_block_group *cache)
34178c2ecf20Sopenharmony_ci{
34188c2ecf20Sopenharmony_ci	atomic_inc(&cache->frozen);
34198c2ecf20Sopenharmony_ci}
34208c2ecf20Sopenharmony_ci
34218c2ecf20Sopenharmony_civoid btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
34228c2ecf20Sopenharmony_ci{
34238c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = block_group->fs_info;
34248c2ecf20Sopenharmony_ci	struct extent_map_tree *em_tree;
34258c2ecf20Sopenharmony_ci	struct extent_map *em;
34268c2ecf20Sopenharmony_ci	bool cleanup;
34278c2ecf20Sopenharmony_ci
34288c2ecf20Sopenharmony_ci	spin_lock(&block_group->lock);
34298c2ecf20Sopenharmony_ci	cleanup = (atomic_dec_and_test(&block_group->frozen) &&
34308c2ecf20Sopenharmony_ci		   block_group->removed);
34318c2ecf20Sopenharmony_ci	spin_unlock(&block_group->lock);
34328c2ecf20Sopenharmony_ci
34338c2ecf20Sopenharmony_ci	if (cleanup) {
34348c2ecf20Sopenharmony_ci		em_tree = &fs_info->mapping_tree;
34358c2ecf20Sopenharmony_ci		write_lock(&em_tree->lock);
34368c2ecf20Sopenharmony_ci		em = lookup_extent_mapping(em_tree, block_group->start,
34378c2ecf20Sopenharmony_ci					   1);
34388c2ecf20Sopenharmony_ci		BUG_ON(!em); /* logic error, can't happen */
34398c2ecf20Sopenharmony_ci		remove_extent_mapping(em_tree, em);
34408c2ecf20Sopenharmony_ci		write_unlock(&em_tree->lock);
34418c2ecf20Sopenharmony_ci
34428c2ecf20Sopenharmony_ci		/* once for us and once for the tree */
34438c2ecf20Sopenharmony_ci		free_extent_map(em);
34448c2ecf20Sopenharmony_ci		free_extent_map(em);
34458c2ecf20Sopenharmony_ci
34468c2ecf20Sopenharmony_ci		/*
34478c2ecf20Sopenharmony_ci		 * We may have left one free space entry and other possible
34488c2ecf20Sopenharmony_ci		 * tasks trimming this block group have left 1 entry each one.
34498c2ecf20Sopenharmony_ci		 * Free them if any.
34508c2ecf20Sopenharmony_ci		 */
34518c2ecf20Sopenharmony_ci		__btrfs_remove_free_space_cache(block_group->free_space_ctl);
34528c2ecf20Sopenharmony_ci	}
34538c2ecf20Sopenharmony_ci}
34548c2ecf20Sopenharmony_ci
34558c2ecf20Sopenharmony_cibool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg)
34568c2ecf20Sopenharmony_ci{
34578c2ecf20Sopenharmony_ci	bool ret = true;
34588c2ecf20Sopenharmony_ci
34598c2ecf20Sopenharmony_ci	spin_lock(&bg->lock);
34608c2ecf20Sopenharmony_ci	if (bg->ro)
34618c2ecf20Sopenharmony_ci		ret = false;
34628c2ecf20Sopenharmony_ci	else
34638c2ecf20Sopenharmony_ci		bg->swap_extents++;
34648c2ecf20Sopenharmony_ci	spin_unlock(&bg->lock);
34658c2ecf20Sopenharmony_ci
34668c2ecf20Sopenharmony_ci	return ret;
34678c2ecf20Sopenharmony_ci}
34688c2ecf20Sopenharmony_ci
34698c2ecf20Sopenharmony_civoid btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount)
34708c2ecf20Sopenharmony_ci{
34718c2ecf20Sopenharmony_ci	spin_lock(&bg->lock);
34728c2ecf20Sopenharmony_ci	ASSERT(!bg->ro);
34738c2ecf20Sopenharmony_ci	ASSERT(bg->swap_extents >= amount);
34748c2ecf20Sopenharmony_ci	bg->swap_extents -= amount;
34758c2ecf20Sopenharmony_ci	spin_unlock(&bg->lock);
34768c2ecf20Sopenharmony_ci}
3477