18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Facebook.  All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/kernel.h>
78c2ecf20Sopenharmony_ci#include <linux/sched/mm.h>
88c2ecf20Sopenharmony_ci#include "ctree.h"
98c2ecf20Sopenharmony_ci#include "disk-io.h"
108c2ecf20Sopenharmony_ci#include "locking.h"
118c2ecf20Sopenharmony_ci#include "free-space-tree.h"
128c2ecf20Sopenharmony_ci#include "transaction.h"
138c2ecf20Sopenharmony_ci#include "block-group.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic int __add_block_group_free_space(struct btrfs_trans_handle *trans,
168c2ecf20Sopenharmony_ci					struct btrfs_block_group *block_group,
178c2ecf20Sopenharmony_ci					struct btrfs_path *path);
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_civoid set_free_space_tree_thresholds(struct btrfs_block_group *cache)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	u32 bitmap_range;
228c2ecf20Sopenharmony_ci	size_t bitmap_size;
238c2ecf20Sopenharmony_ci	u64 num_bitmaps, total_bitmap_size;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	if (WARN_ON(cache->length == 0))
268c2ecf20Sopenharmony_ci		btrfs_warn(cache->fs_info, "block group %llu length is zero",
278c2ecf20Sopenharmony_ci			   cache->start);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	/*
308c2ecf20Sopenharmony_ci	 * We convert to bitmaps when the disk space required for using extents
318c2ecf20Sopenharmony_ci	 * exceeds that required for using bitmaps.
328c2ecf20Sopenharmony_ci	 */
338c2ecf20Sopenharmony_ci	bitmap_range = cache->fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
348c2ecf20Sopenharmony_ci	num_bitmaps = div_u64(cache->length + bitmap_range - 1, bitmap_range);
358c2ecf20Sopenharmony_ci	bitmap_size = sizeof(struct btrfs_item) + BTRFS_FREE_SPACE_BITMAP_SIZE;
368c2ecf20Sopenharmony_ci	total_bitmap_size = num_bitmaps * bitmap_size;
378c2ecf20Sopenharmony_ci	cache->bitmap_high_thresh = div_u64(total_bitmap_size,
388c2ecf20Sopenharmony_ci					    sizeof(struct btrfs_item));
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	/*
418c2ecf20Sopenharmony_ci	 * We allow for a small buffer between the high threshold and low
428c2ecf20Sopenharmony_ci	 * threshold to avoid thrashing back and forth between the two formats.
438c2ecf20Sopenharmony_ci	 */
448c2ecf20Sopenharmony_ci	if (cache->bitmap_high_thresh > 100)
458c2ecf20Sopenharmony_ci		cache->bitmap_low_thresh = cache->bitmap_high_thresh - 100;
468c2ecf20Sopenharmony_ci	else
478c2ecf20Sopenharmony_ci		cache->bitmap_low_thresh = 0;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic int add_new_free_space_info(struct btrfs_trans_handle *trans,
518c2ecf20Sopenharmony_ci				   struct btrfs_block_group *block_group,
528c2ecf20Sopenharmony_ci				   struct btrfs_path *path)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	struct btrfs_root *root = trans->fs_info->free_space_root;
558c2ecf20Sopenharmony_ci	struct btrfs_free_space_info *info;
568c2ecf20Sopenharmony_ci	struct btrfs_key key;
578c2ecf20Sopenharmony_ci	struct extent_buffer *leaf;
588c2ecf20Sopenharmony_ci	int ret;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	key.objectid = block_group->start;
618c2ecf20Sopenharmony_ci	key.type = BTRFS_FREE_SPACE_INFO_KEY;
628c2ecf20Sopenharmony_ci	key.offset = block_group->length;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*info));
658c2ecf20Sopenharmony_ci	if (ret)
668c2ecf20Sopenharmony_ci		goto out;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	leaf = path->nodes[0];
698c2ecf20Sopenharmony_ci	info = btrfs_item_ptr(leaf, path->slots[0],
708c2ecf20Sopenharmony_ci			      struct btrfs_free_space_info);
718c2ecf20Sopenharmony_ci	btrfs_set_free_space_extent_count(leaf, info, 0);
728c2ecf20Sopenharmony_ci	btrfs_set_free_space_flags(leaf, info, 0);
738c2ecf20Sopenharmony_ci	btrfs_mark_buffer_dirty(leaf);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	ret = 0;
768c2ecf20Sopenharmony_ciout:
778c2ecf20Sopenharmony_ci	btrfs_release_path(path);
788c2ecf20Sopenharmony_ci	return ret;
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ciEXPORT_FOR_TESTS
828c2ecf20Sopenharmony_cistruct btrfs_free_space_info *search_free_space_info(
838c2ecf20Sopenharmony_ci		struct btrfs_trans_handle *trans,
848c2ecf20Sopenharmony_ci		struct btrfs_block_group *block_group,
858c2ecf20Sopenharmony_ci		struct btrfs_path *path, int cow)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = block_group->fs_info;
888c2ecf20Sopenharmony_ci	struct btrfs_root *root = fs_info->free_space_root;
898c2ecf20Sopenharmony_ci	struct btrfs_key key;
908c2ecf20Sopenharmony_ci	int ret;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	key.objectid = block_group->start;
938c2ecf20Sopenharmony_ci	key.type = BTRFS_FREE_SPACE_INFO_KEY;
948c2ecf20Sopenharmony_ci	key.offset = block_group->length;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
978c2ecf20Sopenharmony_ci	if (ret < 0)
988c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
998c2ecf20Sopenharmony_ci	if (ret != 0) {
1008c2ecf20Sopenharmony_ci		btrfs_warn(fs_info, "missing free space info for %llu",
1018c2ecf20Sopenharmony_ci			   block_group->start);
1028c2ecf20Sopenharmony_ci		ASSERT(0);
1038c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOENT);
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	return btrfs_item_ptr(path->nodes[0], path->slots[0],
1078c2ecf20Sopenharmony_ci			      struct btrfs_free_space_info);
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/*
1118c2ecf20Sopenharmony_ci * btrfs_search_slot() but we're looking for the greatest key less than the
1128c2ecf20Sopenharmony_ci * passed key.
1138c2ecf20Sopenharmony_ci */
1148c2ecf20Sopenharmony_cistatic int btrfs_search_prev_slot(struct btrfs_trans_handle *trans,
1158c2ecf20Sopenharmony_ci				  struct btrfs_root *root,
1168c2ecf20Sopenharmony_ci				  struct btrfs_key *key, struct btrfs_path *p,
1178c2ecf20Sopenharmony_ci				  int ins_len, int cow)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	int ret;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	ret = btrfs_search_slot(trans, root, key, p, ins_len, cow);
1228c2ecf20Sopenharmony_ci	if (ret < 0)
1238c2ecf20Sopenharmony_ci		return ret;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if (ret == 0) {
1268c2ecf20Sopenharmony_ci		ASSERT(0);
1278c2ecf20Sopenharmony_ci		return -EIO;
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	if (p->slots[0] == 0) {
1318c2ecf20Sopenharmony_ci		ASSERT(0);
1328c2ecf20Sopenharmony_ci		return -EIO;
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci	p->slots[0]--;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	return 0;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic inline u32 free_space_bitmap_size(u64 size, u32 sectorsize)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	return DIV_ROUND_UP((u32)div_u64(size, sectorsize), BITS_PER_BYTE);
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic unsigned long *alloc_bitmap(u32 bitmap_size)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	unsigned long *ret;
1478c2ecf20Sopenharmony_ci	unsigned int nofs_flag;
1488c2ecf20Sopenharmony_ci	u32 bitmap_rounded_size = round_up(bitmap_size, sizeof(unsigned long));
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	/*
1518c2ecf20Sopenharmony_ci	 * GFP_NOFS doesn't work with kvmalloc(), but we really can't recurse
1528c2ecf20Sopenharmony_ci	 * into the filesystem as the free space bitmap can be modified in the
1538c2ecf20Sopenharmony_ci	 * critical section of a transaction commit.
1548c2ecf20Sopenharmony_ci	 *
1558c2ecf20Sopenharmony_ci	 * TODO: push the memalloc_nofs_{save,restore}() to the caller where we
1568c2ecf20Sopenharmony_ci	 * know that recursion is unsafe.
1578c2ecf20Sopenharmony_ci	 */
1588c2ecf20Sopenharmony_ci	nofs_flag = memalloc_nofs_save();
1598c2ecf20Sopenharmony_ci	ret = kvzalloc(bitmap_rounded_size, GFP_KERNEL);
1608c2ecf20Sopenharmony_ci	memalloc_nofs_restore(nofs_flag);
1618c2ecf20Sopenharmony_ci	return ret;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic void le_bitmap_set(unsigned long *map, unsigned int start, int len)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	u8 *p = ((u8 *)map) + BIT_BYTE(start);
1678c2ecf20Sopenharmony_ci	const unsigned int size = start + len;
1688c2ecf20Sopenharmony_ci	int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
1698c2ecf20Sopenharmony_ci	u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	while (len - bits_to_set >= 0) {
1728c2ecf20Sopenharmony_ci		*p |= mask_to_set;
1738c2ecf20Sopenharmony_ci		len -= bits_to_set;
1748c2ecf20Sopenharmony_ci		bits_to_set = BITS_PER_BYTE;
1758c2ecf20Sopenharmony_ci		mask_to_set = ~0;
1768c2ecf20Sopenharmony_ci		p++;
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci	if (len) {
1798c2ecf20Sopenharmony_ci		mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
1808c2ecf20Sopenharmony_ci		*p |= mask_to_set;
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ciEXPORT_FOR_TESTS
1858c2ecf20Sopenharmony_ciint convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
1868c2ecf20Sopenharmony_ci				  struct btrfs_block_group *block_group,
1878c2ecf20Sopenharmony_ci				  struct btrfs_path *path)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
1908c2ecf20Sopenharmony_ci	struct btrfs_root *root = fs_info->free_space_root;
1918c2ecf20Sopenharmony_ci	struct btrfs_free_space_info *info;
1928c2ecf20Sopenharmony_ci	struct btrfs_key key, found_key;
1938c2ecf20Sopenharmony_ci	struct extent_buffer *leaf;
1948c2ecf20Sopenharmony_ci	unsigned long *bitmap;
1958c2ecf20Sopenharmony_ci	char *bitmap_cursor;
1968c2ecf20Sopenharmony_ci	u64 start, end;
1978c2ecf20Sopenharmony_ci	u64 bitmap_range, i;
1988c2ecf20Sopenharmony_ci	u32 bitmap_size, flags, expected_extent_count;
1998c2ecf20Sopenharmony_ci	u32 extent_count = 0;
2008c2ecf20Sopenharmony_ci	int done = 0, nr;
2018c2ecf20Sopenharmony_ci	int ret;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	bitmap_size = free_space_bitmap_size(block_group->length,
2048c2ecf20Sopenharmony_ci					     fs_info->sectorsize);
2058c2ecf20Sopenharmony_ci	bitmap = alloc_bitmap(bitmap_size);
2068c2ecf20Sopenharmony_ci	if (!bitmap) {
2078c2ecf20Sopenharmony_ci		ret = -ENOMEM;
2088c2ecf20Sopenharmony_ci		goto out;
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	start = block_group->start;
2128c2ecf20Sopenharmony_ci	end = block_group->start + block_group->length;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	key.objectid = end - 1;
2158c2ecf20Sopenharmony_ci	key.type = (u8)-1;
2168c2ecf20Sopenharmony_ci	key.offset = (u64)-1;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	while (!done) {
2198c2ecf20Sopenharmony_ci		ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
2208c2ecf20Sopenharmony_ci		if (ret)
2218c2ecf20Sopenharmony_ci			goto out;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci		leaf = path->nodes[0];
2248c2ecf20Sopenharmony_ci		nr = 0;
2258c2ecf20Sopenharmony_ci		path->slots[0]++;
2268c2ecf20Sopenharmony_ci		while (path->slots[0] > 0) {
2278c2ecf20Sopenharmony_ci			btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci			if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
2308c2ecf20Sopenharmony_ci				ASSERT(found_key.objectid == block_group->start);
2318c2ecf20Sopenharmony_ci				ASSERT(found_key.offset == block_group->length);
2328c2ecf20Sopenharmony_ci				done = 1;
2338c2ecf20Sopenharmony_ci				break;
2348c2ecf20Sopenharmony_ci			} else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) {
2358c2ecf20Sopenharmony_ci				u64 first, last;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci				ASSERT(found_key.objectid >= start);
2388c2ecf20Sopenharmony_ci				ASSERT(found_key.objectid < end);
2398c2ecf20Sopenharmony_ci				ASSERT(found_key.objectid + found_key.offset <= end);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci				first = div_u64(found_key.objectid - start,
2428c2ecf20Sopenharmony_ci						fs_info->sectorsize);
2438c2ecf20Sopenharmony_ci				last = div_u64(found_key.objectid + found_key.offset - start,
2448c2ecf20Sopenharmony_ci					       fs_info->sectorsize);
2458c2ecf20Sopenharmony_ci				le_bitmap_set(bitmap, first, last - first);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci				extent_count++;
2488c2ecf20Sopenharmony_ci				nr++;
2498c2ecf20Sopenharmony_ci				path->slots[0]--;
2508c2ecf20Sopenharmony_ci			} else {
2518c2ecf20Sopenharmony_ci				ASSERT(0);
2528c2ecf20Sopenharmony_ci			}
2538c2ecf20Sopenharmony_ci		}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci		ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
2568c2ecf20Sopenharmony_ci		if (ret)
2578c2ecf20Sopenharmony_ci			goto out;
2588c2ecf20Sopenharmony_ci		btrfs_release_path(path);
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	info = search_free_space_info(trans, block_group, path, 1);
2628c2ecf20Sopenharmony_ci	if (IS_ERR(info)) {
2638c2ecf20Sopenharmony_ci		ret = PTR_ERR(info);
2648c2ecf20Sopenharmony_ci		goto out;
2658c2ecf20Sopenharmony_ci	}
2668c2ecf20Sopenharmony_ci	leaf = path->nodes[0];
2678c2ecf20Sopenharmony_ci	flags = btrfs_free_space_flags(leaf, info);
2688c2ecf20Sopenharmony_ci	flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
2698c2ecf20Sopenharmony_ci	btrfs_set_free_space_flags(leaf, info, flags);
2708c2ecf20Sopenharmony_ci	expected_extent_count = btrfs_free_space_extent_count(leaf, info);
2718c2ecf20Sopenharmony_ci	btrfs_mark_buffer_dirty(leaf);
2728c2ecf20Sopenharmony_ci	btrfs_release_path(path);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	if (extent_count != expected_extent_count) {
2758c2ecf20Sopenharmony_ci		btrfs_err(fs_info,
2768c2ecf20Sopenharmony_ci			  "incorrect extent count for %llu; counted %u, expected %u",
2778c2ecf20Sopenharmony_ci			  block_group->start, extent_count,
2788c2ecf20Sopenharmony_ci			  expected_extent_count);
2798c2ecf20Sopenharmony_ci		ASSERT(0);
2808c2ecf20Sopenharmony_ci		ret = -EIO;
2818c2ecf20Sopenharmony_ci		goto out;
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	bitmap_cursor = (char *)bitmap;
2858c2ecf20Sopenharmony_ci	bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
2868c2ecf20Sopenharmony_ci	i = start;
2878c2ecf20Sopenharmony_ci	while (i < end) {
2888c2ecf20Sopenharmony_ci		unsigned long ptr;
2898c2ecf20Sopenharmony_ci		u64 extent_size;
2908c2ecf20Sopenharmony_ci		u32 data_size;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci		extent_size = min(end - i, bitmap_range);
2938c2ecf20Sopenharmony_ci		data_size = free_space_bitmap_size(extent_size,
2948c2ecf20Sopenharmony_ci						   fs_info->sectorsize);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci		key.objectid = i;
2978c2ecf20Sopenharmony_ci		key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
2988c2ecf20Sopenharmony_ci		key.offset = extent_size;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci		ret = btrfs_insert_empty_item(trans, root, path, &key,
3018c2ecf20Sopenharmony_ci					      data_size);
3028c2ecf20Sopenharmony_ci		if (ret)
3038c2ecf20Sopenharmony_ci			goto out;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci		leaf = path->nodes[0];
3068c2ecf20Sopenharmony_ci		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3078c2ecf20Sopenharmony_ci		write_extent_buffer(leaf, bitmap_cursor, ptr,
3088c2ecf20Sopenharmony_ci				    data_size);
3098c2ecf20Sopenharmony_ci		btrfs_mark_buffer_dirty(leaf);
3108c2ecf20Sopenharmony_ci		btrfs_release_path(path);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci		i += extent_size;
3138c2ecf20Sopenharmony_ci		bitmap_cursor += data_size;
3148c2ecf20Sopenharmony_ci	}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	ret = 0;
3178c2ecf20Sopenharmony_ciout:
3188c2ecf20Sopenharmony_ci	kvfree(bitmap);
3198c2ecf20Sopenharmony_ci	if (ret)
3208c2ecf20Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
3218c2ecf20Sopenharmony_ci	return ret;
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ciEXPORT_FOR_TESTS
3258c2ecf20Sopenharmony_ciint convert_free_space_to_extents(struct btrfs_trans_handle *trans,
3268c2ecf20Sopenharmony_ci				  struct btrfs_block_group *block_group,
3278c2ecf20Sopenharmony_ci				  struct btrfs_path *path)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
3308c2ecf20Sopenharmony_ci	struct btrfs_root *root = fs_info->free_space_root;
3318c2ecf20Sopenharmony_ci	struct btrfs_free_space_info *info;
3328c2ecf20Sopenharmony_ci	struct btrfs_key key, found_key;
3338c2ecf20Sopenharmony_ci	struct extent_buffer *leaf;
3348c2ecf20Sopenharmony_ci	unsigned long *bitmap;
3358c2ecf20Sopenharmony_ci	u64 start, end;
3368c2ecf20Sopenharmony_ci	u32 bitmap_size, flags, expected_extent_count;
3378c2ecf20Sopenharmony_ci	unsigned long nrbits, start_bit, end_bit;
3388c2ecf20Sopenharmony_ci	u32 extent_count = 0;
3398c2ecf20Sopenharmony_ci	int done = 0, nr;
3408c2ecf20Sopenharmony_ci	int ret;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	bitmap_size = free_space_bitmap_size(block_group->length,
3438c2ecf20Sopenharmony_ci					     fs_info->sectorsize);
3448c2ecf20Sopenharmony_ci	bitmap = alloc_bitmap(bitmap_size);
3458c2ecf20Sopenharmony_ci	if (!bitmap) {
3468c2ecf20Sopenharmony_ci		ret = -ENOMEM;
3478c2ecf20Sopenharmony_ci		goto out;
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	start = block_group->start;
3518c2ecf20Sopenharmony_ci	end = block_group->start + block_group->length;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	key.objectid = end - 1;
3548c2ecf20Sopenharmony_ci	key.type = (u8)-1;
3558c2ecf20Sopenharmony_ci	key.offset = (u64)-1;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	while (!done) {
3588c2ecf20Sopenharmony_ci		ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
3598c2ecf20Sopenharmony_ci		if (ret)
3608c2ecf20Sopenharmony_ci			goto out;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci		leaf = path->nodes[0];
3638c2ecf20Sopenharmony_ci		nr = 0;
3648c2ecf20Sopenharmony_ci		path->slots[0]++;
3658c2ecf20Sopenharmony_ci		while (path->slots[0] > 0) {
3668c2ecf20Sopenharmony_ci			btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci			if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
3698c2ecf20Sopenharmony_ci				ASSERT(found_key.objectid == block_group->start);
3708c2ecf20Sopenharmony_ci				ASSERT(found_key.offset == block_group->length);
3718c2ecf20Sopenharmony_ci				done = 1;
3728c2ecf20Sopenharmony_ci				break;
3738c2ecf20Sopenharmony_ci			} else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
3748c2ecf20Sopenharmony_ci				unsigned long ptr;
3758c2ecf20Sopenharmony_ci				char *bitmap_cursor;
3768c2ecf20Sopenharmony_ci				u32 bitmap_pos, data_size;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci				ASSERT(found_key.objectid >= start);
3798c2ecf20Sopenharmony_ci				ASSERT(found_key.objectid < end);
3808c2ecf20Sopenharmony_ci				ASSERT(found_key.objectid + found_key.offset <= end);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci				bitmap_pos = div_u64(found_key.objectid - start,
3838c2ecf20Sopenharmony_ci						     fs_info->sectorsize *
3848c2ecf20Sopenharmony_ci						     BITS_PER_BYTE);
3858c2ecf20Sopenharmony_ci				bitmap_cursor = ((char *)bitmap) + bitmap_pos;
3868c2ecf20Sopenharmony_ci				data_size = free_space_bitmap_size(found_key.offset,
3878c2ecf20Sopenharmony_ci								   fs_info->sectorsize);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci				ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
3908c2ecf20Sopenharmony_ci				read_extent_buffer(leaf, bitmap_cursor, ptr,
3918c2ecf20Sopenharmony_ci						   data_size);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci				nr++;
3948c2ecf20Sopenharmony_ci				path->slots[0]--;
3958c2ecf20Sopenharmony_ci			} else {
3968c2ecf20Sopenharmony_ci				ASSERT(0);
3978c2ecf20Sopenharmony_ci			}
3988c2ecf20Sopenharmony_ci		}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci		ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
4018c2ecf20Sopenharmony_ci		if (ret)
4028c2ecf20Sopenharmony_ci			goto out;
4038c2ecf20Sopenharmony_ci		btrfs_release_path(path);
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	info = search_free_space_info(trans, block_group, path, 1);
4078c2ecf20Sopenharmony_ci	if (IS_ERR(info)) {
4088c2ecf20Sopenharmony_ci		ret = PTR_ERR(info);
4098c2ecf20Sopenharmony_ci		goto out;
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci	leaf = path->nodes[0];
4128c2ecf20Sopenharmony_ci	flags = btrfs_free_space_flags(leaf, info);
4138c2ecf20Sopenharmony_ci	flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
4148c2ecf20Sopenharmony_ci	btrfs_set_free_space_flags(leaf, info, flags);
4158c2ecf20Sopenharmony_ci	expected_extent_count = btrfs_free_space_extent_count(leaf, info);
4168c2ecf20Sopenharmony_ci	btrfs_mark_buffer_dirty(leaf);
4178c2ecf20Sopenharmony_ci	btrfs_release_path(path);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	nrbits = div_u64(block_group->length, block_group->fs_info->sectorsize);
4208c2ecf20Sopenharmony_ci	start_bit = find_next_bit_le(bitmap, nrbits, 0);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	while (start_bit < nrbits) {
4238c2ecf20Sopenharmony_ci		end_bit = find_next_zero_bit_le(bitmap, nrbits, start_bit);
4248c2ecf20Sopenharmony_ci		ASSERT(start_bit < end_bit);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci		key.objectid = start + start_bit * block_group->fs_info->sectorsize;
4278c2ecf20Sopenharmony_ci		key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
4288c2ecf20Sopenharmony_ci		key.offset = (end_bit - start_bit) * block_group->fs_info->sectorsize;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci		ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
4318c2ecf20Sopenharmony_ci		if (ret)
4328c2ecf20Sopenharmony_ci			goto out;
4338c2ecf20Sopenharmony_ci		btrfs_release_path(path);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci		extent_count++;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci		start_bit = find_next_bit_le(bitmap, nrbits, end_bit);
4388c2ecf20Sopenharmony_ci	}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	if (extent_count != expected_extent_count) {
4418c2ecf20Sopenharmony_ci		btrfs_err(fs_info,
4428c2ecf20Sopenharmony_ci			  "incorrect extent count for %llu; counted %u, expected %u",
4438c2ecf20Sopenharmony_ci			  block_group->start, extent_count,
4448c2ecf20Sopenharmony_ci			  expected_extent_count);
4458c2ecf20Sopenharmony_ci		ASSERT(0);
4468c2ecf20Sopenharmony_ci		ret = -EIO;
4478c2ecf20Sopenharmony_ci		goto out;
4488c2ecf20Sopenharmony_ci	}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	ret = 0;
4518c2ecf20Sopenharmony_ciout:
4528c2ecf20Sopenharmony_ci	kvfree(bitmap);
4538c2ecf20Sopenharmony_ci	if (ret)
4548c2ecf20Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
4558c2ecf20Sopenharmony_ci	return ret;
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_cistatic int update_free_space_extent_count(struct btrfs_trans_handle *trans,
4598c2ecf20Sopenharmony_ci					  struct btrfs_block_group *block_group,
4608c2ecf20Sopenharmony_ci					  struct btrfs_path *path,
4618c2ecf20Sopenharmony_ci					  int new_extents)
4628c2ecf20Sopenharmony_ci{
4638c2ecf20Sopenharmony_ci	struct btrfs_free_space_info *info;
4648c2ecf20Sopenharmony_ci	u32 flags;
4658c2ecf20Sopenharmony_ci	u32 extent_count;
4668c2ecf20Sopenharmony_ci	int ret = 0;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	if (new_extents == 0)
4698c2ecf20Sopenharmony_ci		return 0;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	info = search_free_space_info(trans, block_group, path, 1);
4728c2ecf20Sopenharmony_ci	if (IS_ERR(info)) {
4738c2ecf20Sopenharmony_ci		ret = PTR_ERR(info);
4748c2ecf20Sopenharmony_ci		goto out;
4758c2ecf20Sopenharmony_ci	}
4768c2ecf20Sopenharmony_ci	flags = btrfs_free_space_flags(path->nodes[0], info);
4778c2ecf20Sopenharmony_ci	extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	extent_count += new_extents;
4808c2ecf20Sopenharmony_ci	btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
4818c2ecf20Sopenharmony_ci	btrfs_mark_buffer_dirty(path->nodes[0]);
4828c2ecf20Sopenharmony_ci	btrfs_release_path(path);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
4858c2ecf20Sopenharmony_ci	    extent_count > block_group->bitmap_high_thresh) {
4868c2ecf20Sopenharmony_ci		ret = convert_free_space_to_bitmaps(trans, block_group, path);
4878c2ecf20Sopenharmony_ci	} else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
4888c2ecf20Sopenharmony_ci		   extent_count < block_group->bitmap_low_thresh) {
4898c2ecf20Sopenharmony_ci		ret = convert_free_space_to_extents(trans, block_group, path);
4908c2ecf20Sopenharmony_ci	}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ciout:
4938c2ecf20Sopenharmony_ci	return ret;
4948c2ecf20Sopenharmony_ci}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ciEXPORT_FOR_TESTS
4978c2ecf20Sopenharmony_ciint free_space_test_bit(struct btrfs_block_group *block_group,
4988c2ecf20Sopenharmony_ci			struct btrfs_path *path, u64 offset)
4998c2ecf20Sopenharmony_ci{
5008c2ecf20Sopenharmony_ci	struct extent_buffer *leaf;
5018c2ecf20Sopenharmony_ci	struct btrfs_key key;
5028c2ecf20Sopenharmony_ci	u64 found_start, found_end;
5038c2ecf20Sopenharmony_ci	unsigned long ptr, i;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	leaf = path->nodes[0];
5068c2ecf20Sopenharmony_ci	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5078c2ecf20Sopenharmony_ci	ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	found_start = key.objectid;
5108c2ecf20Sopenharmony_ci	found_end = key.objectid + key.offset;
5118c2ecf20Sopenharmony_ci	ASSERT(offset >= found_start && offset < found_end);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5148c2ecf20Sopenharmony_ci	i = div_u64(offset - found_start,
5158c2ecf20Sopenharmony_ci		    block_group->fs_info->sectorsize);
5168c2ecf20Sopenharmony_ci	return !!extent_buffer_test_bit(leaf, ptr, i);
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic void free_space_set_bits(struct btrfs_block_group *block_group,
5208c2ecf20Sopenharmony_ci				struct btrfs_path *path, u64 *start, u64 *size,
5218c2ecf20Sopenharmony_ci				int bit)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = block_group->fs_info;
5248c2ecf20Sopenharmony_ci	struct extent_buffer *leaf;
5258c2ecf20Sopenharmony_ci	struct btrfs_key key;
5268c2ecf20Sopenharmony_ci	u64 end = *start + *size;
5278c2ecf20Sopenharmony_ci	u64 found_start, found_end;
5288c2ecf20Sopenharmony_ci	unsigned long ptr, first, last;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	leaf = path->nodes[0];
5318c2ecf20Sopenharmony_ci	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5328c2ecf20Sopenharmony_ci	ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	found_start = key.objectid;
5358c2ecf20Sopenharmony_ci	found_end = key.objectid + key.offset;
5368c2ecf20Sopenharmony_ci	ASSERT(*start >= found_start && *start < found_end);
5378c2ecf20Sopenharmony_ci	ASSERT(end > found_start);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	if (end > found_end)
5408c2ecf20Sopenharmony_ci		end = found_end;
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5438c2ecf20Sopenharmony_ci	first = div_u64(*start - found_start, fs_info->sectorsize);
5448c2ecf20Sopenharmony_ci	last = div_u64(end - found_start, fs_info->sectorsize);
5458c2ecf20Sopenharmony_ci	if (bit)
5468c2ecf20Sopenharmony_ci		extent_buffer_bitmap_set(leaf, ptr, first, last - first);
5478c2ecf20Sopenharmony_ci	else
5488c2ecf20Sopenharmony_ci		extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
5498c2ecf20Sopenharmony_ci	btrfs_mark_buffer_dirty(leaf);
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	*size -= end - *start;
5528c2ecf20Sopenharmony_ci	*start = end;
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci/*
5568c2ecf20Sopenharmony_ci * We can't use btrfs_next_item() in modify_free_space_bitmap() because
5578c2ecf20Sopenharmony_ci * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
5588c2ecf20Sopenharmony_ci * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
5598c2ecf20Sopenharmony_ci * looking for.
5608c2ecf20Sopenharmony_ci */
5618c2ecf20Sopenharmony_cistatic int free_space_next_bitmap(struct btrfs_trans_handle *trans,
5628c2ecf20Sopenharmony_ci				  struct btrfs_root *root, struct btrfs_path *p)
5638c2ecf20Sopenharmony_ci{
5648c2ecf20Sopenharmony_ci	struct btrfs_key key;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
5678c2ecf20Sopenharmony_ci		p->slots[0]++;
5688c2ecf20Sopenharmony_ci		return 0;
5698c2ecf20Sopenharmony_ci	}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
5728c2ecf20Sopenharmony_ci	btrfs_release_path(p);
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	key.objectid += key.offset;
5758c2ecf20Sopenharmony_ci	key.type = (u8)-1;
5768c2ecf20Sopenharmony_ci	key.offset = (u64)-1;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
5798c2ecf20Sopenharmony_ci}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci/*
5828c2ecf20Sopenharmony_ci * If remove is 1, then we are removing free space, thus clearing bits in the
5838c2ecf20Sopenharmony_ci * bitmap. If remove is 0, then we are adding free space, thus setting bits in
5848c2ecf20Sopenharmony_ci * the bitmap.
5858c2ecf20Sopenharmony_ci */
5868c2ecf20Sopenharmony_cistatic int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
5878c2ecf20Sopenharmony_ci				    struct btrfs_block_group *block_group,
5888c2ecf20Sopenharmony_ci				    struct btrfs_path *path,
5898c2ecf20Sopenharmony_ci				    u64 start, u64 size, int remove)
5908c2ecf20Sopenharmony_ci{
5918c2ecf20Sopenharmony_ci	struct btrfs_root *root = block_group->fs_info->free_space_root;
5928c2ecf20Sopenharmony_ci	struct btrfs_key key;
5938c2ecf20Sopenharmony_ci	u64 end = start + size;
5948c2ecf20Sopenharmony_ci	u64 cur_start, cur_size;
5958c2ecf20Sopenharmony_ci	int prev_bit, next_bit;
5968c2ecf20Sopenharmony_ci	int new_extents;
5978c2ecf20Sopenharmony_ci	int ret;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	/*
6008c2ecf20Sopenharmony_ci	 * Read the bit for the block immediately before the extent of space if
6018c2ecf20Sopenharmony_ci	 * that block is within the block group.
6028c2ecf20Sopenharmony_ci	 */
6038c2ecf20Sopenharmony_ci	if (start > block_group->start) {
6048c2ecf20Sopenharmony_ci		u64 prev_block = start - block_group->fs_info->sectorsize;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci		key.objectid = prev_block;
6078c2ecf20Sopenharmony_ci		key.type = (u8)-1;
6088c2ecf20Sopenharmony_ci		key.offset = (u64)-1;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci		ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
6118c2ecf20Sopenharmony_ci		if (ret)
6128c2ecf20Sopenharmony_ci			goto out;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci		prev_bit = free_space_test_bit(block_group, path, prev_block);
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci		/* The previous block may have been in the previous bitmap. */
6178c2ecf20Sopenharmony_ci		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
6188c2ecf20Sopenharmony_ci		if (start >= key.objectid + key.offset) {
6198c2ecf20Sopenharmony_ci			ret = free_space_next_bitmap(trans, root, path);
6208c2ecf20Sopenharmony_ci			if (ret)
6218c2ecf20Sopenharmony_ci				goto out;
6228c2ecf20Sopenharmony_ci		}
6238c2ecf20Sopenharmony_ci	} else {
6248c2ecf20Sopenharmony_ci		key.objectid = start;
6258c2ecf20Sopenharmony_ci		key.type = (u8)-1;
6268c2ecf20Sopenharmony_ci		key.offset = (u64)-1;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci		ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
6298c2ecf20Sopenharmony_ci		if (ret)
6308c2ecf20Sopenharmony_ci			goto out;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci		prev_bit = -1;
6338c2ecf20Sopenharmony_ci	}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	/*
6368c2ecf20Sopenharmony_ci	 * Iterate over all of the bitmaps overlapped by the extent of space,
6378c2ecf20Sopenharmony_ci	 * clearing/setting bits as required.
6388c2ecf20Sopenharmony_ci	 */
6398c2ecf20Sopenharmony_ci	cur_start = start;
6408c2ecf20Sopenharmony_ci	cur_size = size;
6418c2ecf20Sopenharmony_ci	while (1) {
6428c2ecf20Sopenharmony_ci		free_space_set_bits(block_group, path, &cur_start, &cur_size,
6438c2ecf20Sopenharmony_ci				    !remove);
6448c2ecf20Sopenharmony_ci		if (cur_size == 0)
6458c2ecf20Sopenharmony_ci			break;
6468c2ecf20Sopenharmony_ci		ret = free_space_next_bitmap(trans, root, path);
6478c2ecf20Sopenharmony_ci		if (ret)
6488c2ecf20Sopenharmony_ci			goto out;
6498c2ecf20Sopenharmony_ci	}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	/*
6528c2ecf20Sopenharmony_ci	 * Read the bit for the block immediately after the extent of space if
6538c2ecf20Sopenharmony_ci	 * that block is within the block group.
6548c2ecf20Sopenharmony_ci	 */
6558c2ecf20Sopenharmony_ci	if (end < block_group->start + block_group->length) {
6568c2ecf20Sopenharmony_ci		/* The next block may be in the next bitmap. */
6578c2ecf20Sopenharmony_ci		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
6588c2ecf20Sopenharmony_ci		if (end >= key.objectid + key.offset) {
6598c2ecf20Sopenharmony_ci			ret = free_space_next_bitmap(trans, root, path);
6608c2ecf20Sopenharmony_ci			if (ret)
6618c2ecf20Sopenharmony_ci				goto out;
6628c2ecf20Sopenharmony_ci		}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci		next_bit = free_space_test_bit(block_group, path, end);
6658c2ecf20Sopenharmony_ci	} else {
6668c2ecf20Sopenharmony_ci		next_bit = -1;
6678c2ecf20Sopenharmony_ci	}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	if (remove) {
6708c2ecf20Sopenharmony_ci		new_extents = -1;
6718c2ecf20Sopenharmony_ci		if (prev_bit == 1) {
6728c2ecf20Sopenharmony_ci			/* Leftover on the left. */
6738c2ecf20Sopenharmony_ci			new_extents++;
6748c2ecf20Sopenharmony_ci		}
6758c2ecf20Sopenharmony_ci		if (next_bit == 1) {
6768c2ecf20Sopenharmony_ci			/* Leftover on the right. */
6778c2ecf20Sopenharmony_ci			new_extents++;
6788c2ecf20Sopenharmony_ci		}
6798c2ecf20Sopenharmony_ci	} else {
6808c2ecf20Sopenharmony_ci		new_extents = 1;
6818c2ecf20Sopenharmony_ci		if (prev_bit == 1) {
6828c2ecf20Sopenharmony_ci			/* Merging with neighbor on the left. */
6838c2ecf20Sopenharmony_ci			new_extents--;
6848c2ecf20Sopenharmony_ci		}
6858c2ecf20Sopenharmony_ci		if (next_bit == 1) {
6868c2ecf20Sopenharmony_ci			/* Merging with neighbor on the right. */
6878c2ecf20Sopenharmony_ci			new_extents--;
6888c2ecf20Sopenharmony_ci		}
6898c2ecf20Sopenharmony_ci	}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	btrfs_release_path(path);
6928c2ecf20Sopenharmony_ci	ret = update_free_space_extent_count(trans, block_group, path,
6938c2ecf20Sopenharmony_ci					     new_extents);
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ciout:
6968c2ecf20Sopenharmony_ci	return ret;
6978c2ecf20Sopenharmony_ci}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_cistatic int remove_free_space_extent(struct btrfs_trans_handle *trans,
7008c2ecf20Sopenharmony_ci				    struct btrfs_block_group *block_group,
7018c2ecf20Sopenharmony_ci				    struct btrfs_path *path,
7028c2ecf20Sopenharmony_ci				    u64 start, u64 size)
7038c2ecf20Sopenharmony_ci{
7048c2ecf20Sopenharmony_ci	struct btrfs_root *root = trans->fs_info->free_space_root;
7058c2ecf20Sopenharmony_ci	struct btrfs_key key;
7068c2ecf20Sopenharmony_ci	u64 found_start, found_end;
7078c2ecf20Sopenharmony_ci	u64 end = start + size;
7088c2ecf20Sopenharmony_ci	int new_extents = -1;
7098c2ecf20Sopenharmony_ci	int ret;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	key.objectid = start;
7128c2ecf20Sopenharmony_ci	key.type = (u8)-1;
7138c2ecf20Sopenharmony_ci	key.offset = (u64)-1;
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
7168c2ecf20Sopenharmony_ci	if (ret)
7178c2ecf20Sopenharmony_ci		goto out;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	found_start = key.objectid;
7248c2ecf20Sopenharmony_ci	found_end = key.objectid + key.offset;
7258c2ecf20Sopenharmony_ci	ASSERT(start >= found_start && end <= found_end);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	/*
7288c2ecf20Sopenharmony_ci	 * Okay, now that we've found the free space extent which contains the
7298c2ecf20Sopenharmony_ci	 * free space that we are removing, there are four cases:
7308c2ecf20Sopenharmony_ci	 *
7318c2ecf20Sopenharmony_ci	 * 1. We're using the whole extent: delete the key we found and
7328c2ecf20Sopenharmony_ci	 * decrement the free space extent count.
7338c2ecf20Sopenharmony_ci	 * 2. We are using part of the extent starting at the beginning: delete
7348c2ecf20Sopenharmony_ci	 * the key we found and insert a new key representing the leftover at
7358c2ecf20Sopenharmony_ci	 * the end. There is no net change in the number of extents.
7368c2ecf20Sopenharmony_ci	 * 3. We are using part of the extent ending at the end: delete the key
7378c2ecf20Sopenharmony_ci	 * we found and insert a new key representing the leftover at the
7388c2ecf20Sopenharmony_ci	 * beginning. There is no net change in the number of extents.
7398c2ecf20Sopenharmony_ci	 * 4. We are using part of the extent in the middle: delete the key we
7408c2ecf20Sopenharmony_ci	 * found and insert two new keys representing the leftovers on each
7418c2ecf20Sopenharmony_ci	 * side. Where we used to have one extent, we now have two, so increment
7428c2ecf20Sopenharmony_ci	 * the extent count. We may need to convert the block group to bitmaps
7438c2ecf20Sopenharmony_ci	 * as a result.
7448c2ecf20Sopenharmony_ci	 */
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	/* Delete the existing key (cases 1-4). */
7478c2ecf20Sopenharmony_ci	ret = btrfs_del_item(trans, root, path);
7488c2ecf20Sopenharmony_ci	if (ret)
7498c2ecf20Sopenharmony_ci		goto out;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	/* Add a key for leftovers at the beginning (cases 3 and 4). */
7528c2ecf20Sopenharmony_ci	if (start > found_start) {
7538c2ecf20Sopenharmony_ci		key.objectid = found_start;
7548c2ecf20Sopenharmony_ci		key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
7558c2ecf20Sopenharmony_ci		key.offset = start - found_start;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci		btrfs_release_path(path);
7588c2ecf20Sopenharmony_ci		ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
7598c2ecf20Sopenharmony_ci		if (ret)
7608c2ecf20Sopenharmony_ci			goto out;
7618c2ecf20Sopenharmony_ci		new_extents++;
7628c2ecf20Sopenharmony_ci	}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	/* Add a key for leftovers at the end (cases 2 and 4). */
7658c2ecf20Sopenharmony_ci	if (end < found_end) {
7668c2ecf20Sopenharmony_ci		key.objectid = end;
7678c2ecf20Sopenharmony_ci		key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
7688c2ecf20Sopenharmony_ci		key.offset = found_end - end;
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci		btrfs_release_path(path);
7718c2ecf20Sopenharmony_ci		ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
7728c2ecf20Sopenharmony_ci		if (ret)
7738c2ecf20Sopenharmony_ci			goto out;
7748c2ecf20Sopenharmony_ci		new_extents++;
7758c2ecf20Sopenharmony_ci	}
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	btrfs_release_path(path);
7788c2ecf20Sopenharmony_ci	ret = update_free_space_extent_count(trans, block_group, path,
7798c2ecf20Sopenharmony_ci					     new_extents);
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ciout:
7828c2ecf20Sopenharmony_ci	return ret;
7838c2ecf20Sopenharmony_ci}
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ciEXPORT_FOR_TESTS
7868c2ecf20Sopenharmony_ciint __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
7878c2ecf20Sopenharmony_ci				  struct btrfs_block_group *block_group,
7888c2ecf20Sopenharmony_ci				  struct btrfs_path *path, u64 start, u64 size)
7898c2ecf20Sopenharmony_ci{
7908c2ecf20Sopenharmony_ci	struct btrfs_free_space_info *info;
7918c2ecf20Sopenharmony_ci	u32 flags;
7928c2ecf20Sopenharmony_ci	int ret;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	if (block_group->needs_free_space) {
7958c2ecf20Sopenharmony_ci		ret = __add_block_group_free_space(trans, block_group, path);
7968c2ecf20Sopenharmony_ci		if (ret)
7978c2ecf20Sopenharmony_ci			return ret;
7988c2ecf20Sopenharmony_ci	}
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	info = search_free_space_info(NULL, block_group, path, 0);
8018c2ecf20Sopenharmony_ci	if (IS_ERR(info))
8028c2ecf20Sopenharmony_ci		return PTR_ERR(info);
8038c2ecf20Sopenharmony_ci	flags = btrfs_free_space_flags(path->nodes[0], info);
8048c2ecf20Sopenharmony_ci	btrfs_release_path(path);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
8078c2ecf20Sopenharmony_ci		return modify_free_space_bitmap(trans, block_group, path,
8088c2ecf20Sopenharmony_ci						start, size, 1);
8098c2ecf20Sopenharmony_ci	} else {
8108c2ecf20Sopenharmony_ci		return remove_free_space_extent(trans, block_group, path,
8118c2ecf20Sopenharmony_ci						start, size);
8128c2ecf20Sopenharmony_ci	}
8138c2ecf20Sopenharmony_ci}
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ciint remove_from_free_space_tree(struct btrfs_trans_handle *trans,
8168c2ecf20Sopenharmony_ci				u64 start, u64 size)
8178c2ecf20Sopenharmony_ci{
8188c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group;
8198c2ecf20Sopenharmony_ci	struct btrfs_path *path;
8208c2ecf20Sopenharmony_ci	int ret;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
8238c2ecf20Sopenharmony_ci		return 0;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
8268c2ecf20Sopenharmony_ci	if (!path) {
8278c2ecf20Sopenharmony_ci		ret = -ENOMEM;
8288c2ecf20Sopenharmony_ci		goto out;
8298c2ecf20Sopenharmony_ci	}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	block_group = btrfs_lookup_block_group(trans->fs_info, start);
8328c2ecf20Sopenharmony_ci	if (!block_group) {
8338c2ecf20Sopenharmony_ci		ASSERT(0);
8348c2ecf20Sopenharmony_ci		ret = -ENOENT;
8358c2ecf20Sopenharmony_ci		goto out;
8368c2ecf20Sopenharmony_ci	}
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	mutex_lock(&block_group->free_space_lock);
8398c2ecf20Sopenharmony_ci	ret = __remove_from_free_space_tree(trans, block_group, path, start,
8408c2ecf20Sopenharmony_ci					    size);
8418c2ecf20Sopenharmony_ci	mutex_unlock(&block_group->free_space_lock);
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	btrfs_put_block_group(block_group);
8448c2ecf20Sopenharmony_ciout:
8458c2ecf20Sopenharmony_ci	btrfs_free_path(path);
8468c2ecf20Sopenharmony_ci	if (ret)
8478c2ecf20Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
8488c2ecf20Sopenharmony_ci	return ret;
8498c2ecf20Sopenharmony_ci}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_cistatic int add_free_space_extent(struct btrfs_trans_handle *trans,
8528c2ecf20Sopenharmony_ci				 struct btrfs_block_group *block_group,
8538c2ecf20Sopenharmony_ci				 struct btrfs_path *path,
8548c2ecf20Sopenharmony_ci				 u64 start, u64 size)
8558c2ecf20Sopenharmony_ci{
8568c2ecf20Sopenharmony_ci	struct btrfs_root *root = trans->fs_info->free_space_root;
8578c2ecf20Sopenharmony_ci	struct btrfs_key key, new_key;
8588c2ecf20Sopenharmony_ci	u64 found_start, found_end;
8598c2ecf20Sopenharmony_ci	u64 end = start + size;
8608c2ecf20Sopenharmony_ci	int new_extents = 1;
8618c2ecf20Sopenharmony_ci	int ret;
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	/*
8648c2ecf20Sopenharmony_ci	 * We are adding a new extent of free space, but we need to merge
8658c2ecf20Sopenharmony_ci	 * extents. There are four cases here:
8668c2ecf20Sopenharmony_ci	 *
8678c2ecf20Sopenharmony_ci	 * 1. The new extent does not have any immediate neighbors to merge
8688c2ecf20Sopenharmony_ci	 * with: add the new key and increment the free space extent count. We
8698c2ecf20Sopenharmony_ci	 * may need to convert the block group to bitmaps as a result.
8708c2ecf20Sopenharmony_ci	 * 2. The new extent has an immediate neighbor before it: remove the
8718c2ecf20Sopenharmony_ci	 * previous key and insert a new key combining both of them. There is no
8728c2ecf20Sopenharmony_ci	 * net change in the number of extents.
8738c2ecf20Sopenharmony_ci	 * 3. The new extent has an immediate neighbor after it: remove the next
8748c2ecf20Sopenharmony_ci	 * key and insert a new key combining both of them. There is no net
8758c2ecf20Sopenharmony_ci	 * change in the number of extents.
8768c2ecf20Sopenharmony_ci	 * 4. The new extent has immediate neighbors on both sides: remove both
8778c2ecf20Sopenharmony_ci	 * of the keys and insert a new key combining all of them. Where we used
8788c2ecf20Sopenharmony_ci	 * to have two extents, we now have one, so decrement the extent count.
8798c2ecf20Sopenharmony_ci	 */
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	new_key.objectid = start;
8828c2ecf20Sopenharmony_ci	new_key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
8838c2ecf20Sopenharmony_ci	new_key.offset = size;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	/* Search for a neighbor on the left. */
8868c2ecf20Sopenharmony_ci	if (start == block_group->start)
8878c2ecf20Sopenharmony_ci		goto right;
8888c2ecf20Sopenharmony_ci	key.objectid = start - 1;
8898c2ecf20Sopenharmony_ci	key.type = (u8)-1;
8908c2ecf20Sopenharmony_ci	key.offset = (u64)-1;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
8938c2ecf20Sopenharmony_ci	if (ret)
8948c2ecf20Sopenharmony_ci		goto out;
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
8998c2ecf20Sopenharmony_ci		ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
9008c2ecf20Sopenharmony_ci		btrfs_release_path(path);
9018c2ecf20Sopenharmony_ci		goto right;
9028c2ecf20Sopenharmony_ci	}
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	found_start = key.objectid;
9058c2ecf20Sopenharmony_ci	found_end = key.objectid + key.offset;
9068c2ecf20Sopenharmony_ci	ASSERT(found_start >= block_group->start &&
9078c2ecf20Sopenharmony_ci	       found_end > block_group->start);
9088c2ecf20Sopenharmony_ci	ASSERT(found_start < start && found_end <= start);
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	/*
9118c2ecf20Sopenharmony_ci	 * Delete the neighbor on the left and absorb it into the new key (cases
9128c2ecf20Sopenharmony_ci	 * 2 and 4).
9138c2ecf20Sopenharmony_ci	 */
9148c2ecf20Sopenharmony_ci	if (found_end == start) {
9158c2ecf20Sopenharmony_ci		ret = btrfs_del_item(trans, root, path);
9168c2ecf20Sopenharmony_ci		if (ret)
9178c2ecf20Sopenharmony_ci			goto out;
9188c2ecf20Sopenharmony_ci		new_key.objectid = found_start;
9198c2ecf20Sopenharmony_ci		new_key.offset += key.offset;
9208c2ecf20Sopenharmony_ci		new_extents--;
9218c2ecf20Sopenharmony_ci	}
9228c2ecf20Sopenharmony_ci	btrfs_release_path(path);
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ciright:
9258c2ecf20Sopenharmony_ci	/* Search for a neighbor on the right. */
9268c2ecf20Sopenharmony_ci	if (end == block_group->start + block_group->length)
9278c2ecf20Sopenharmony_ci		goto insert;
9288c2ecf20Sopenharmony_ci	key.objectid = end;
9298c2ecf20Sopenharmony_ci	key.type = (u8)-1;
9308c2ecf20Sopenharmony_ci	key.offset = (u64)-1;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
9338c2ecf20Sopenharmony_ci	if (ret)
9348c2ecf20Sopenharmony_ci		goto out;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
9398c2ecf20Sopenharmony_ci		ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
9408c2ecf20Sopenharmony_ci		btrfs_release_path(path);
9418c2ecf20Sopenharmony_ci		goto insert;
9428c2ecf20Sopenharmony_ci	}
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	found_start = key.objectid;
9458c2ecf20Sopenharmony_ci	found_end = key.objectid + key.offset;
9468c2ecf20Sopenharmony_ci	ASSERT(found_start >= block_group->start &&
9478c2ecf20Sopenharmony_ci	       found_end > block_group->start);
9488c2ecf20Sopenharmony_ci	ASSERT((found_start < start && found_end <= start) ||
9498c2ecf20Sopenharmony_ci	       (found_start >= end && found_end > end));
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	/*
9528c2ecf20Sopenharmony_ci	 * Delete the neighbor on the right and absorb it into the new key
9538c2ecf20Sopenharmony_ci	 * (cases 3 and 4).
9548c2ecf20Sopenharmony_ci	 */
9558c2ecf20Sopenharmony_ci	if (found_start == end) {
9568c2ecf20Sopenharmony_ci		ret = btrfs_del_item(trans, root, path);
9578c2ecf20Sopenharmony_ci		if (ret)
9588c2ecf20Sopenharmony_ci			goto out;
9598c2ecf20Sopenharmony_ci		new_key.offset += key.offset;
9608c2ecf20Sopenharmony_ci		new_extents--;
9618c2ecf20Sopenharmony_ci	}
9628c2ecf20Sopenharmony_ci	btrfs_release_path(path);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ciinsert:
9658c2ecf20Sopenharmony_ci	/* Insert the new key (cases 1-4). */
9668c2ecf20Sopenharmony_ci	ret = btrfs_insert_empty_item(trans, root, path, &new_key, 0);
9678c2ecf20Sopenharmony_ci	if (ret)
9688c2ecf20Sopenharmony_ci		goto out;
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	btrfs_release_path(path);
9718c2ecf20Sopenharmony_ci	ret = update_free_space_extent_count(trans, block_group, path,
9728c2ecf20Sopenharmony_ci					     new_extents);
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ciout:
9758c2ecf20Sopenharmony_ci	return ret;
9768c2ecf20Sopenharmony_ci}
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ciEXPORT_FOR_TESTS
9798c2ecf20Sopenharmony_ciint __add_to_free_space_tree(struct btrfs_trans_handle *trans,
9808c2ecf20Sopenharmony_ci			     struct btrfs_block_group *block_group,
9818c2ecf20Sopenharmony_ci			     struct btrfs_path *path, u64 start, u64 size)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	struct btrfs_free_space_info *info;
9848c2ecf20Sopenharmony_ci	u32 flags;
9858c2ecf20Sopenharmony_ci	int ret;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	if (block_group->needs_free_space) {
9888c2ecf20Sopenharmony_ci		ret = __add_block_group_free_space(trans, block_group, path);
9898c2ecf20Sopenharmony_ci		if (ret)
9908c2ecf20Sopenharmony_ci			return ret;
9918c2ecf20Sopenharmony_ci	}
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	info = search_free_space_info(NULL, block_group, path, 0);
9948c2ecf20Sopenharmony_ci	if (IS_ERR(info))
9958c2ecf20Sopenharmony_ci		return PTR_ERR(info);
9968c2ecf20Sopenharmony_ci	flags = btrfs_free_space_flags(path->nodes[0], info);
9978c2ecf20Sopenharmony_ci	btrfs_release_path(path);
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
10008c2ecf20Sopenharmony_ci		return modify_free_space_bitmap(trans, block_group, path,
10018c2ecf20Sopenharmony_ci						start, size, 0);
10028c2ecf20Sopenharmony_ci	} else {
10038c2ecf20Sopenharmony_ci		return add_free_space_extent(trans, block_group, path, start,
10048c2ecf20Sopenharmony_ci					     size);
10058c2ecf20Sopenharmony_ci	}
10068c2ecf20Sopenharmony_ci}
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ciint add_to_free_space_tree(struct btrfs_trans_handle *trans,
10098c2ecf20Sopenharmony_ci			   u64 start, u64 size)
10108c2ecf20Sopenharmony_ci{
10118c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group;
10128c2ecf20Sopenharmony_ci	struct btrfs_path *path;
10138c2ecf20Sopenharmony_ci	int ret;
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
10168c2ecf20Sopenharmony_ci		return 0;
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
10198c2ecf20Sopenharmony_ci	if (!path) {
10208c2ecf20Sopenharmony_ci		ret = -ENOMEM;
10218c2ecf20Sopenharmony_ci		goto out;
10228c2ecf20Sopenharmony_ci	}
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	block_group = btrfs_lookup_block_group(trans->fs_info, start);
10258c2ecf20Sopenharmony_ci	if (!block_group) {
10268c2ecf20Sopenharmony_ci		ASSERT(0);
10278c2ecf20Sopenharmony_ci		ret = -ENOENT;
10288c2ecf20Sopenharmony_ci		goto out;
10298c2ecf20Sopenharmony_ci	}
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci	mutex_lock(&block_group->free_space_lock);
10328c2ecf20Sopenharmony_ci	ret = __add_to_free_space_tree(trans, block_group, path, start, size);
10338c2ecf20Sopenharmony_ci	mutex_unlock(&block_group->free_space_lock);
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	btrfs_put_block_group(block_group);
10368c2ecf20Sopenharmony_ciout:
10378c2ecf20Sopenharmony_ci	btrfs_free_path(path);
10388c2ecf20Sopenharmony_ci	if (ret)
10398c2ecf20Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
10408c2ecf20Sopenharmony_ci	return ret;
10418c2ecf20Sopenharmony_ci}
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci/*
10448c2ecf20Sopenharmony_ci * Populate the free space tree by walking the extent tree. Operations on the
10458c2ecf20Sopenharmony_ci * extent tree that happen as a result of writes to the free space tree will go
10468c2ecf20Sopenharmony_ci * through the normal add/remove hooks.
10478c2ecf20Sopenharmony_ci */
10488c2ecf20Sopenharmony_cistatic int populate_free_space_tree(struct btrfs_trans_handle *trans,
10498c2ecf20Sopenharmony_ci				    struct btrfs_block_group *block_group)
10508c2ecf20Sopenharmony_ci{
10518c2ecf20Sopenharmony_ci	struct btrfs_root *extent_root = trans->fs_info->extent_root;
10528c2ecf20Sopenharmony_ci	struct btrfs_path *path, *path2;
10538c2ecf20Sopenharmony_ci	struct btrfs_key key;
10548c2ecf20Sopenharmony_ci	u64 start, end;
10558c2ecf20Sopenharmony_ci	int ret;
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
10588c2ecf20Sopenharmony_ci	if (!path)
10598c2ecf20Sopenharmony_ci		return -ENOMEM;
10608c2ecf20Sopenharmony_ci	path->reada = READA_FORWARD;
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	path2 = btrfs_alloc_path();
10638c2ecf20Sopenharmony_ci	if (!path2) {
10648c2ecf20Sopenharmony_ci		btrfs_free_path(path);
10658c2ecf20Sopenharmony_ci		return -ENOMEM;
10668c2ecf20Sopenharmony_ci	}
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	ret = add_new_free_space_info(trans, block_group, path2);
10698c2ecf20Sopenharmony_ci	if (ret)
10708c2ecf20Sopenharmony_ci		goto out;
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	mutex_lock(&block_group->free_space_lock);
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	/*
10758c2ecf20Sopenharmony_ci	 * Iterate through all of the extent and metadata items in this block
10768c2ecf20Sopenharmony_ci	 * group, adding the free space between them and the free space at the
10778c2ecf20Sopenharmony_ci	 * end. Note that EXTENT_ITEM and METADATA_ITEM are less than
10788c2ecf20Sopenharmony_ci	 * BLOCK_GROUP_ITEM, so an extent may precede the block group that it's
10798c2ecf20Sopenharmony_ci	 * contained in.
10808c2ecf20Sopenharmony_ci	 */
10818c2ecf20Sopenharmony_ci	key.objectid = block_group->start;
10828c2ecf20Sopenharmony_ci	key.type = BTRFS_EXTENT_ITEM_KEY;
10838c2ecf20Sopenharmony_ci	key.offset = 0;
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
10868c2ecf20Sopenharmony_ci	if (ret < 0)
10878c2ecf20Sopenharmony_ci		goto out_locked;
10888c2ecf20Sopenharmony_ci	ASSERT(ret == 0);
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	start = block_group->start;
10918c2ecf20Sopenharmony_ci	end = block_group->start + block_group->length;
10928c2ecf20Sopenharmony_ci	while (1) {
10938c2ecf20Sopenharmony_ci		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
10968c2ecf20Sopenharmony_ci		    key.type == BTRFS_METADATA_ITEM_KEY) {
10978c2ecf20Sopenharmony_ci			if (key.objectid >= end)
10988c2ecf20Sopenharmony_ci				break;
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci			if (start < key.objectid) {
11018c2ecf20Sopenharmony_ci				ret = __add_to_free_space_tree(trans,
11028c2ecf20Sopenharmony_ci							       block_group,
11038c2ecf20Sopenharmony_ci							       path2, start,
11048c2ecf20Sopenharmony_ci							       key.objectid -
11058c2ecf20Sopenharmony_ci							       start);
11068c2ecf20Sopenharmony_ci				if (ret)
11078c2ecf20Sopenharmony_ci					goto out_locked;
11088c2ecf20Sopenharmony_ci			}
11098c2ecf20Sopenharmony_ci			start = key.objectid;
11108c2ecf20Sopenharmony_ci			if (key.type == BTRFS_METADATA_ITEM_KEY)
11118c2ecf20Sopenharmony_ci				start += trans->fs_info->nodesize;
11128c2ecf20Sopenharmony_ci			else
11138c2ecf20Sopenharmony_ci				start += key.offset;
11148c2ecf20Sopenharmony_ci		} else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
11158c2ecf20Sopenharmony_ci			if (key.objectid != block_group->start)
11168c2ecf20Sopenharmony_ci				break;
11178c2ecf20Sopenharmony_ci		}
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci		ret = btrfs_next_item(extent_root, path);
11208c2ecf20Sopenharmony_ci		if (ret < 0)
11218c2ecf20Sopenharmony_ci			goto out_locked;
11228c2ecf20Sopenharmony_ci		if (ret)
11238c2ecf20Sopenharmony_ci			break;
11248c2ecf20Sopenharmony_ci	}
11258c2ecf20Sopenharmony_ci	if (start < end) {
11268c2ecf20Sopenharmony_ci		ret = __add_to_free_space_tree(trans, block_group, path2,
11278c2ecf20Sopenharmony_ci					       start, end - start);
11288c2ecf20Sopenharmony_ci		if (ret)
11298c2ecf20Sopenharmony_ci			goto out_locked;
11308c2ecf20Sopenharmony_ci	}
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_ci	ret = 0;
11338c2ecf20Sopenharmony_ciout_locked:
11348c2ecf20Sopenharmony_ci	mutex_unlock(&block_group->free_space_lock);
11358c2ecf20Sopenharmony_ciout:
11368c2ecf20Sopenharmony_ci	btrfs_free_path(path2);
11378c2ecf20Sopenharmony_ci	btrfs_free_path(path);
11388c2ecf20Sopenharmony_ci	return ret;
11398c2ecf20Sopenharmony_ci}
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ciint btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
11428c2ecf20Sopenharmony_ci{
11438c2ecf20Sopenharmony_ci	struct btrfs_trans_handle *trans;
11448c2ecf20Sopenharmony_ci	struct btrfs_root *tree_root = fs_info->tree_root;
11458c2ecf20Sopenharmony_ci	struct btrfs_root *free_space_root;
11468c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group;
11478c2ecf20Sopenharmony_ci	struct rb_node *node;
11488c2ecf20Sopenharmony_ci	int ret;
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci	trans = btrfs_start_transaction(tree_root, 0);
11518c2ecf20Sopenharmony_ci	if (IS_ERR(trans))
11528c2ecf20Sopenharmony_ci		return PTR_ERR(trans);
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci	set_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
11558c2ecf20Sopenharmony_ci	set_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
11568c2ecf20Sopenharmony_ci	free_space_root = btrfs_create_tree(trans,
11578c2ecf20Sopenharmony_ci					    BTRFS_FREE_SPACE_TREE_OBJECTID);
11588c2ecf20Sopenharmony_ci	if (IS_ERR(free_space_root)) {
11598c2ecf20Sopenharmony_ci		ret = PTR_ERR(free_space_root);
11608c2ecf20Sopenharmony_ci		goto abort;
11618c2ecf20Sopenharmony_ci	}
11628c2ecf20Sopenharmony_ci	fs_info->free_space_root = free_space_root;
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci	node = rb_first(&fs_info->block_group_cache_tree);
11658c2ecf20Sopenharmony_ci	while (node) {
11668c2ecf20Sopenharmony_ci		block_group = rb_entry(node, struct btrfs_block_group,
11678c2ecf20Sopenharmony_ci				       cache_node);
11688c2ecf20Sopenharmony_ci		ret = populate_free_space_tree(trans, block_group);
11698c2ecf20Sopenharmony_ci		if (ret)
11708c2ecf20Sopenharmony_ci			goto abort;
11718c2ecf20Sopenharmony_ci		node = rb_next(node);
11728c2ecf20Sopenharmony_ci	}
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
11758c2ecf20Sopenharmony_ci	btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
11768c2ecf20Sopenharmony_ci	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
11778c2ecf20Sopenharmony_ci	ret = btrfs_commit_transaction(trans);
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	/*
11808c2ecf20Sopenharmony_ci	 * Now that we've committed the transaction any reading of our commit
11818c2ecf20Sopenharmony_ci	 * root will be safe, so we can cache from the free space tree now.
11828c2ecf20Sopenharmony_ci	 */
11838c2ecf20Sopenharmony_ci	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
11848c2ecf20Sopenharmony_ci	return ret;
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ciabort:
11878c2ecf20Sopenharmony_ci	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
11888c2ecf20Sopenharmony_ci	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
11898c2ecf20Sopenharmony_ci	btrfs_abort_transaction(trans, ret);
11908c2ecf20Sopenharmony_ci	btrfs_end_transaction(trans);
11918c2ecf20Sopenharmony_ci	return ret;
11928c2ecf20Sopenharmony_ci}
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_cistatic int clear_free_space_tree(struct btrfs_trans_handle *trans,
11958c2ecf20Sopenharmony_ci				 struct btrfs_root *root)
11968c2ecf20Sopenharmony_ci{
11978c2ecf20Sopenharmony_ci	struct btrfs_path *path;
11988c2ecf20Sopenharmony_ci	struct btrfs_key key;
11998c2ecf20Sopenharmony_ci	int nr;
12008c2ecf20Sopenharmony_ci	int ret;
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
12038c2ecf20Sopenharmony_ci	if (!path)
12048c2ecf20Sopenharmony_ci		return -ENOMEM;
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	path->leave_spinning = 1;
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	key.objectid = 0;
12098c2ecf20Sopenharmony_ci	key.type = 0;
12108c2ecf20Sopenharmony_ci	key.offset = 0;
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	while (1) {
12138c2ecf20Sopenharmony_ci		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
12148c2ecf20Sopenharmony_ci		if (ret < 0)
12158c2ecf20Sopenharmony_ci			goto out;
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci		nr = btrfs_header_nritems(path->nodes[0]);
12188c2ecf20Sopenharmony_ci		if (!nr)
12198c2ecf20Sopenharmony_ci			break;
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci		path->slots[0] = 0;
12228c2ecf20Sopenharmony_ci		ret = btrfs_del_items(trans, root, path, 0, nr);
12238c2ecf20Sopenharmony_ci		if (ret)
12248c2ecf20Sopenharmony_ci			goto out;
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci		btrfs_release_path(path);
12278c2ecf20Sopenharmony_ci	}
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_ci	ret = 0;
12308c2ecf20Sopenharmony_ciout:
12318c2ecf20Sopenharmony_ci	btrfs_free_path(path);
12328c2ecf20Sopenharmony_ci	return ret;
12338c2ecf20Sopenharmony_ci}
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ciint btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
12368c2ecf20Sopenharmony_ci{
12378c2ecf20Sopenharmony_ci	struct btrfs_trans_handle *trans;
12388c2ecf20Sopenharmony_ci	struct btrfs_root *tree_root = fs_info->tree_root;
12398c2ecf20Sopenharmony_ci	struct btrfs_root *free_space_root = fs_info->free_space_root;
12408c2ecf20Sopenharmony_ci	int ret;
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	trans = btrfs_start_transaction(tree_root, 0);
12438c2ecf20Sopenharmony_ci	if (IS_ERR(trans))
12448c2ecf20Sopenharmony_ci		return PTR_ERR(trans);
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci	btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE);
12478c2ecf20Sopenharmony_ci	btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
12488c2ecf20Sopenharmony_ci	fs_info->free_space_root = NULL;
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	ret = clear_free_space_tree(trans, free_space_root);
12518c2ecf20Sopenharmony_ci	if (ret)
12528c2ecf20Sopenharmony_ci		goto abort;
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci	ret = btrfs_del_root(trans, &free_space_root->root_key);
12558c2ecf20Sopenharmony_ci	if (ret)
12568c2ecf20Sopenharmony_ci		goto abort;
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci	list_del(&free_space_root->dirty_list);
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci	btrfs_tree_lock(free_space_root->node);
12618c2ecf20Sopenharmony_ci	btrfs_clean_tree_block(free_space_root->node);
12628c2ecf20Sopenharmony_ci	btrfs_tree_unlock(free_space_root->node);
12638c2ecf20Sopenharmony_ci	btrfs_free_tree_block(trans, free_space_root, free_space_root->node,
12648c2ecf20Sopenharmony_ci			      0, 1);
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci	btrfs_put_root(free_space_root);
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	return btrfs_commit_transaction(trans);
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ciabort:
12718c2ecf20Sopenharmony_ci	btrfs_abort_transaction(trans, ret);
12728c2ecf20Sopenharmony_ci	btrfs_end_transaction(trans);
12738c2ecf20Sopenharmony_ci	return ret;
12748c2ecf20Sopenharmony_ci}
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_cistatic int __add_block_group_free_space(struct btrfs_trans_handle *trans,
12778c2ecf20Sopenharmony_ci					struct btrfs_block_group *block_group,
12788c2ecf20Sopenharmony_ci					struct btrfs_path *path)
12798c2ecf20Sopenharmony_ci{
12808c2ecf20Sopenharmony_ci	int ret;
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci	block_group->needs_free_space = 0;
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	ret = add_new_free_space_info(trans, block_group, path);
12858c2ecf20Sopenharmony_ci	if (ret)
12868c2ecf20Sopenharmony_ci		return ret;
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci	return __add_to_free_space_tree(trans, block_group, path,
12898c2ecf20Sopenharmony_ci					block_group->start,
12908c2ecf20Sopenharmony_ci					block_group->length);
12918c2ecf20Sopenharmony_ci}
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ciint add_block_group_free_space(struct btrfs_trans_handle *trans,
12948c2ecf20Sopenharmony_ci			       struct btrfs_block_group *block_group)
12958c2ecf20Sopenharmony_ci{
12968c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
12978c2ecf20Sopenharmony_ci	struct btrfs_path *path = NULL;
12988c2ecf20Sopenharmony_ci	int ret = 0;
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
13018c2ecf20Sopenharmony_ci		return 0;
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci	mutex_lock(&block_group->free_space_lock);
13048c2ecf20Sopenharmony_ci	if (!block_group->needs_free_space)
13058c2ecf20Sopenharmony_ci		goto out;
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
13088c2ecf20Sopenharmony_ci	if (!path) {
13098c2ecf20Sopenharmony_ci		ret = -ENOMEM;
13108c2ecf20Sopenharmony_ci		goto out;
13118c2ecf20Sopenharmony_ci	}
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci	ret = __add_block_group_free_space(trans, block_group, path);
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ciout:
13168c2ecf20Sopenharmony_ci	btrfs_free_path(path);
13178c2ecf20Sopenharmony_ci	mutex_unlock(&block_group->free_space_lock);
13188c2ecf20Sopenharmony_ci	if (ret)
13198c2ecf20Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
13208c2ecf20Sopenharmony_ci	return ret;
13218c2ecf20Sopenharmony_ci}
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_ciint remove_block_group_free_space(struct btrfs_trans_handle *trans,
13248c2ecf20Sopenharmony_ci				  struct btrfs_block_group *block_group)
13258c2ecf20Sopenharmony_ci{
13268c2ecf20Sopenharmony_ci	struct btrfs_root *root = trans->fs_info->free_space_root;
13278c2ecf20Sopenharmony_ci	struct btrfs_path *path;
13288c2ecf20Sopenharmony_ci	struct btrfs_key key, found_key;
13298c2ecf20Sopenharmony_ci	struct extent_buffer *leaf;
13308c2ecf20Sopenharmony_ci	u64 start, end;
13318c2ecf20Sopenharmony_ci	int done = 0, nr;
13328c2ecf20Sopenharmony_ci	int ret;
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci	if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
13358c2ecf20Sopenharmony_ci		return 0;
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	if (block_group->needs_free_space) {
13388c2ecf20Sopenharmony_ci		/* We never added this block group to the free space tree. */
13398c2ecf20Sopenharmony_ci		return 0;
13408c2ecf20Sopenharmony_ci	}
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
13438c2ecf20Sopenharmony_ci	if (!path) {
13448c2ecf20Sopenharmony_ci		ret = -ENOMEM;
13458c2ecf20Sopenharmony_ci		goto out;
13468c2ecf20Sopenharmony_ci	}
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	start = block_group->start;
13498c2ecf20Sopenharmony_ci	end = block_group->start + block_group->length;
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_ci	key.objectid = end - 1;
13528c2ecf20Sopenharmony_ci	key.type = (u8)-1;
13538c2ecf20Sopenharmony_ci	key.offset = (u64)-1;
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci	while (!done) {
13568c2ecf20Sopenharmony_ci		ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
13578c2ecf20Sopenharmony_ci		if (ret)
13588c2ecf20Sopenharmony_ci			goto out;
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_ci		leaf = path->nodes[0];
13618c2ecf20Sopenharmony_ci		nr = 0;
13628c2ecf20Sopenharmony_ci		path->slots[0]++;
13638c2ecf20Sopenharmony_ci		while (path->slots[0] > 0) {
13648c2ecf20Sopenharmony_ci			btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
13658c2ecf20Sopenharmony_ci
13668c2ecf20Sopenharmony_ci			if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
13678c2ecf20Sopenharmony_ci				ASSERT(found_key.objectid == block_group->start);
13688c2ecf20Sopenharmony_ci				ASSERT(found_key.offset == block_group->length);
13698c2ecf20Sopenharmony_ci				done = 1;
13708c2ecf20Sopenharmony_ci				nr++;
13718c2ecf20Sopenharmony_ci				path->slots[0]--;
13728c2ecf20Sopenharmony_ci				break;
13738c2ecf20Sopenharmony_ci			} else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY ||
13748c2ecf20Sopenharmony_ci				   found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
13758c2ecf20Sopenharmony_ci				ASSERT(found_key.objectid >= start);
13768c2ecf20Sopenharmony_ci				ASSERT(found_key.objectid < end);
13778c2ecf20Sopenharmony_ci				ASSERT(found_key.objectid + found_key.offset <= end);
13788c2ecf20Sopenharmony_ci				nr++;
13798c2ecf20Sopenharmony_ci				path->slots[0]--;
13808c2ecf20Sopenharmony_ci			} else {
13818c2ecf20Sopenharmony_ci				ASSERT(0);
13828c2ecf20Sopenharmony_ci			}
13838c2ecf20Sopenharmony_ci		}
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci		ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
13868c2ecf20Sopenharmony_ci		if (ret)
13878c2ecf20Sopenharmony_ci			goto out;
13888c2ecf20Sopenharmony_ci		btrfs_release_path(path);
13898c2ecf20Sopenharmony_ci	}
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci	ret = 0;
13928c2ecf20Sopenharmony_ciout:
13938c2ecf20Sopenharmony_ci	btrfs_free_path(path);
13948c2ecf20Sopenharmony_ci	if (ret)
13958c2ecf20Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
13968c2ecf20Sopenharmony_ci	return ret;
13978c2ecf20Sopenharmony_ci}
13988c2ecf20Sopenharmony_ci
13998c2ecf20Sopenharmony_cistatic int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
14008c2ecf20Sopenharmony_ci				   struct btrfs_path *path,
14018c2ecf20Sopenharmony_ci				   u32 expected_extent_count)
14028c2ecf20Sopenharmony_ci{
14038c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group;
14048c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info;
14058c2ecf20Sopenharmony_ci	struct btrfs_root *root;
14068c2ecf20Sopenharmony_ci	struct btrfs_key key;
14078c2ecf20Sopenharmony_ci	int prev_bit = 0, bit;
14088c2ecf20Sopenharmony_ci	/* Initialize to silence GCC. */
14098c2ecf20Sopenharmony_ci	u64 extent_start = 0;
14108c2ecf20Sopenharmony_ci	u64 end, offset;
14118c2ecf20Sopenharmony_ci	u64 total_found = 0;
14128c2ecf20Sopenharmony_ci	u32 extent_count = 0;
14138c2ecf20Sopenharmony_ci	int ret;
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_ci	block_group = caching_ctl->block_group;
14168c2ecf20Sopenharmony_ci	fs_info = block_group->fs_info;
14178c2ecf20Sopenharmony_ci	root = fs_info->free_space_root;
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci	end = block_group->start + block_group->length;
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci	while (1) {
14228c2ecf20Sopenharmony_ci		ret = btrfs_next_item(root, path);
14238c2ecf20Sopenharmony_ci		if (ret < 0)
14248c2ecf20Sopenharmony_ci			goto out;
14258c2ecf20Sopenharmony_ci		if (ret)
14268c2ecf20Sopenharmony_ci			break;
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_ci		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
14298c2ecf20Sopenharmony_ci
14308c2ecf20Sopenharmony_ci		if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
14318c2ecf20Sopenharmony_ci			break;
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci		ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
14348c2ecf20Sopenharmony_ci		ASSERT(key.objectid < end && key.objectid + key.offset <= end);
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci		caching_ctl->progress = key.objectid;
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ci		offset = key.objectid;
14398c2ecf20Sopenharmony_ci		while (offset < key.objectid + key.offset) {
14408c2ecf20Sopenharmony_ci			bit = free_space_test_bit(block_group, path, offset);
14418c2ecf20Sopenharmony_ci			if (prev_bit == 0 && bit == 1) {
14428c2ecf20Sopenharmony_ci				extent_start = offset;
14438c2ecf20Sopenharmony_ci			} else if (prev_bit == 1 && bit == 0) {
14448c2ecf20Sopenharmony_ci				total_found += add_new_free_space(block_group,
14458c2ecf20Sopenharmony_ci								  extent_start,
14468c2ecf20Sopenharmony_ci								  offset);
14478c2ecf20Sopenharmony_ci				if (total_found > CACHING_CTL_WAKE_UP) {
14488c2ecf20Sopenharmony_ci					total_found = 0;
14498c2ecf20Sopenharmony_ci					wake_up(&caching_ctl->wait);
14508c2ecf20Sopenharmony_ci				}
14518c2ecf20Sopenharmony_ci				extent_count++;
14528c2ecf20Sopenharmony_ci			}
14538c2ecf20Sopenharmony_ci			prev_bit = bit;
14548c2ecf20Sopenharmony_ci			offset += fs_info->sectorsize;
14558c2ecf20Sopenharmony_ci		}
14568c2ecf20Sopenharmony_ci	}
14578c2ecf20Sopenharmony_ci	if (prev_bit == 1) {
14588c2ecf20Sopenharmony_ci		total_found += add_new_free_space(block_group, extent_start,
14598c2ecf20Sopenharmony_ci						  end);
14608c2ecf20Sopenharmony_ci		extent_count++;
14618c2ecf20Sopenharmony_ci	}
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	if (extent_count != expected_extent_count) {
14648c2ecf20Sopenharmony_ci		btrfs_err(fs_info,
14658c2ecf20Sopenharmony_ci			  "incorrect extent count for %llu; counted %u, expected %u",
14668c2ecf20Sopenharmony_ci			  block_group->start, extent_count,
14678c2ecf20Sopenharmony_ci			  expected_extent_count);
14688c2ecf20Sopenharmony_ci		ASSERT(0);
14698c2ecf20Sopenharmony_ci		ret = -EIO;
14708c2ecf20Sopenharmony_ci		goto out;
14718c2ecf20Sopenharmony_ci	}
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_ci	caching_ctl->progress = (u64)-1;
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci	ret = 0;
14768c2ecf20Sopenharmony_ciout:
14778c2ecf20Sopenharmony_ci	return ret;
14788c2ecf20Sopenharmony_ci}
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_cistatic int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
14818c2ecf20Sopenharmony_ci				   struct btrfs_path *path,
14828c2ecf20Sopenharmony_ci				   u32 expected_extent_count)
14838c2ecf20Sopenharmony_ci{
14848c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group;
14858c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info;
14868c2ecf20Sopenharmony_ci	struct btrfs_root *root;
14878c2ecf20Sopenharmony_ci	struct btrfs_key key;
14888c2ecf20Sopenharmony_ci	u64 end;
14898c2ecf20Sopenharmony_ci	u64 total_found = 0;
14908c2ecf20Sopenharmony_ci	u32 extent_count = 0;
14918c2ecf20Sopenharmony_ci	int ret;
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_ci	block_group = caching_ctl->block_group;
14948c2ecf20Sopenharmony_ci	fs_info = block_group->fs_info;
14958c2ecf20Sopenharmony_ci	root = fs_info->free_space_root;
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_ci	end = block_group->start + block_group->length;
14988c2ecf20Sopenharmony_ci
14998c2ecf20Sopenharmony_ci	while (1) {
15008c2ecf20Sopenharmony_ci		ret = btrfs_next_item(root, path);
15018c2ecf20Sopenharmony_ci		if (ret < 0)
15028c2ecf20Sopenharmony_ci			goto out;
15038c2ecf20Sopenharmony_ci		if (ret)
15048c2ecf20Sopenharmony_ci			break;
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
15078c2ecf20Sopenharmony_ci
15088c2ecf20Sopenharmony_ci		if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
15098c2ecf20Sopenharmony_ci			break;
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_ci		ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
15128c2ecf20Sopenharmony_ci		ASSERT(key.objectid < end && key.objectid + key.offset <= end);
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_ci		caching_ctl->progress = key.objectid;
15158c2ecf20Sopenharmony_ci
15168c2ecf20Sopenharmony_ci		total_found += add_new_free_space(block_group, key.objectid,
15178c2ecf20Sopenharmony_ci						  key.objectid + key.offset);
15188c2ecf20Sopenharmony_ci		if (total_found > CACHING_CTL_WAKE_UP) {
15198c2ecf20Sopenharmony_ci			total_found = 0;
15208c2ecf20Sopenharmony_ci			wake_up(&caching_ctl->wait);
15218c2ecf20Sopenharmony_ci		}
15228c2ecf20Sopenharmony_ci		extent_count++;
15238c2ecf20Sopenharmony_ci	}
15248c2ecf20Sopenharmony_ci
15258c2ecf20Sopenharmony_ci	if (extent_count != expected_extent_count) {
15268c2ecf20Sopenharmony_ci		btrfs_err(fs_info,
15278c2ecf20Sopenharmony_ci			  "incorrect extent count for %llu; counted %u, expected %u",
15288c2ecf20Sopenharmony_ci			  block_group->start, extent_count,
15298c2ecf20Sopenharmony_ci			  expected_extent_count);
15308c2ecf20Sopenharmony_ci		ASSERT(0);
15318c2ecf20Sopenharmony_ci		ret = -EIO;
15328c2ecf20Sopenharmony_ci		goto out;
15338c2ecf20Sopenharmony_ci	}
15348c2ecf20Sopenharmony_ci
15358c2ecf20Sopenharmony_ci	caching_ctl->progress = (u64)-1;
15368c2ecf20Sopenharmony_ci
15378c2ecf20Sopenharmony_ci	ret = 0;
15388c2ecf20Sopenharmony_ciout:
15398c2ecf20Sopenharmony_ci	return ret;
15408c2ecf20Sopenharmony_ci}
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ciint load_free_space_tree(struct btrfs_caching_control *caching_ctl)
15438c2ecf20Sopenharmony_ci{
15448c2ecf20Sopenharmony_ci	struct btrfs_block_group *block_group;
15458c2ecf20Sopenharmony_ci	struct btrfs_free_space_info *info;
15468c2ecf20Sopenharmony_ci	struct btrfs_path *path;
15478c2ecf20Sopenharmony_ci	u32 extent_count, flags;
15488c2ecf20Sopenharmony_ci	int ret;
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_ci	block_group = caching_ctl->block_group;
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
15538c2ecf20Sopenharmony_ci	if (!path)
15548c2ecf20Sopenharmony_ci		return -ENOMEM;
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci	/*
15578c2ecf20Sopenharmony_ci	 * Just like caching_thread() doesn't want to deadlock on the extent
15588c2ecf20Sopenharmony_ci	 * tree, we don't want to deadlock on the free space tree.
15598c2ecf20Sopenharmony_ci	 */
15608c2ecf20Sopenharmony_ci	path->skip_locking = 1;
15618c2ecf20Sopenharmony_ci	path->search_commit_root = 1;
15628c2ecf20Sopenharmony_ci	path->reada = READA_FORWARD;
15638c2ecf20Sopenharmony_ci
15648c2ecf20Sopenharmony_ci	info = search_free_space_info(NULL, block_group, path, 0);
15658c2ecf20Sopenharmony_ci	if (IS_ERR(info)) {
15668c2ecf20Sopenharmony_ci		ret = PTR_ERR(info);
15678c2ecf20Sopenharmony_ci		goto out;
15688c2ecf20Sopenharmony_ci	}
15698c2ecf20Sopenharmony_ci	extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
15708c2ecf20Sopenharmony_ci	flags = btrfs_free_space_flags(path->nodes[0], info);
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci	/*
15738c2ecf20Sopenharmony_ci	 * We left path pointing to the free space info item, so now
15748c2ecf20Sopenharmony_ci	 * load_free_space_foo can just iterate through the free space tree from
15758c2ecf20Sopenharmony_ci	 * there.
15768c2ecf20Sopenharmony_ci	 */
15778c2ecf20Sopenharmony_ci	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
15788c2ecf20Sopenharmony_ci		ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
15798c2ecf20Sopenharmony_ci	else
15808c2ecf20Sopenharmony_ci		ret = load_free_space_extents(caching_ctl, path, extent_count);
15818c2ecf20Sopenharmony_ci
15828c2ecf20Sopenharmony_ciout:
15838c2ecf20Sopenharmony_ci	btrfs_free_path(path);
15848c2ecf20Sopenharmony_ci	return ret;
15858c2ecf20Sopenharmony_ci}
1586