xref: /openbmc/linux/fs/btrfs/free-space-tree.c (revision 9b569ea0)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
2a5ed9182SOmar Sandoval /*
3a5ed9182SOmar Sandoval  * Copyright (C) 2015 Facebook.  All rights reserved.
4a5ed9182SOmar Sandoval  */
5a5ed9182SOmar Sandoval 
6a5ed9182SOmar Sandoval #include <linux/kernel.h>
725ff17e8SOmar Sandoval #include <linux/sched/mm.h>
8*9b569ea0SJosef Bacik #include "messages.h"
9a5ed9182SOmar Sandoval #include "ctree.h"
10a5ed9182SOmar Sandoval #include "disk-io.h"
11a5ed9182SOmar Sandoval #include "locking.h"
12a5ed9182SOmar Sandoval #include "free-space-tree.h"
13a5ed9182SOmar Sandoval #include "transaction.h"
14aac0023cSJosef Bacik #include "block-group.h"
15c7f13d42SJosef Bacik #include "fs.h"
16a5ed9182SOmar Sandoval 
17a5ed9182SOmar Sandoval static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
1832da5386SDavid Sterba 					struct btrfs_block_group *block_group,
19a5ed9182SOmar Sandoval 					struct btrfs_path *path);
20a5ed9182SOmar Sandoval 
217939dd9fSJosef Bacik static struct btrfs_root *btrfs_free_space_root(
227939dd9fSJosef Bacik 				struct btrfs_block_group *block_group)
237939dd9fSJosef Bacik {
24abed4aaaSJosef Bacik 	struct btrfs_key key = {
25abed4aaaSJosef Bacik 		.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID,
26abed4aaaSJosef Bacik 		.type = BTRFS_ROOT_ITEM_KEY,
27abed4aaaSJosef Bacik 		.offset = 0,
28abed4aaaSJosef Bacik 	};
29abed4aaaSJosef Bacik 
30f7238e50SJosef Bacik 	if (btrfs_fs_incompat(block_group->fs_info, EXTENT_TREE_V2))
31f7238e50SJosef Bacik 		key.offset = block_group->global_root_id;
32abed4aaaSJosef Bacik 	return btrfs_global_root(block_group->fs_info, &key);
337939dd9fSJosef Bacik }
347939dd9fSJosef Bacik 
3532da5386SDavid Sterba void set_free_space_tree_thresholds(struct btrfs_block_group *cache)
36a5ed9182SOmar Sandoval {
37a5ed9182SOmar Sandoval 	u32 bitmap_range;
38a5ed9182SOmar Sandoval 	size_t bitmap_size;
39a5ed9182SOmar Sandoval 	u64 num_bitmaps, total_bitmap_size;
40a5ed9182SOmar Sandoval 
41e3e39c72SMarcos Paulo de Souza 	if (WARN_ON(cache->length == 0))
42e3e39c72SMarcos Paulo de Souza 		btrfs_warn(cache->fs_info, "block group %llu length is zero",
43e3e39c72SMarcos Paulo de Souza 			   cache->start);
44e3e39c72SMarcos Paulo de Souza 
45a5ed9182SOmar Sandoval 	/*
46a5ed9182SOmar Sandoval 	 * We convert to bitmaps when the disk space required for using extents
47a5ed9182SOmar Sandoval 	 * exceeds that required for using bitmaps.
48a5ed9182SOmar Sandoval 	 */
49da17066cSJeff Mahoney 	bitmap_range = cache->fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
50b3470b5dSDavid Sterba 	num_bitmaps = div_u64(cache->length + bitmap_range - 1, bitmap_range);
51a5ed9182SOmar Sandoval 	bitmap_size = sizeof(struct btrfs_item) + BTRFS_FREE_SPACE_BITMAP_SIZE;
52a5ed9182SOmar Sandoval 	total_bitmap_size = num_bitmaps * bitmap_size;
53a5ed9182SOmar Sandoval 	cache->bitmap_high_thresh = div_u64(total_bitmap_size,
54a5ed9182SOmar Sandoval 					    sizeof(struct btrfs_item));
55a5ed9182SOmar Sandoval 
56a5ed9182SOmar Sandoval 	/*
57a5ed9182SOmar Sandoval 	 * We allow for a small buffer between the high threshold and low
58a5ed9182SOmar Sandoval 	 * threshold to avoid thrashing back and forth between the two formats.
59a5ed9182SOmar Sandoval 	 */
60a5ed9182SOmar Sandoval 	if (cache->bitmap_high_thresh > 100)
61a5ed9182SOmar Sandoval 		cache->bitmap_low_thresh = cache->bitmap_high_thresh - 100;
62a5ed9182SOmar Sandoval 	else
63a5ed9182SOmar Sandoval 		cache->bitmap_low_thresh = 0;
64a5ed9182SOmar Sandoval }
65a5ed9182SOmar Sandoval 
66a5ed9182SOmar Sandoval static int add_new_free_space_info(struct btrfs_trans_handle *trans,
6732da5386SDavid Sterba 				   struct btrfs_block_group *block_group,
68a5ed9182SOmar Sandoval 				   struct btrfs_path *path)
69a5ed9182SOmar Sandoval {
707939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
71a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
72a5ed9182SOmar Sandoval 	struct btrfs_key key;
73a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
74a5ed9182SOmar Sandoval 	int ret;
75a5ed9182SOmar Sandoval 
76b3470b5dSDavid Sterba 	key.objectid = block_group->start;
77a5ed9182SOmar Sandoval 	key.type = BTRFS_FREE_SPACE_INFO_KEY;
78b3470b5dSDavid Sterba 	key.offset = block_group->length;
79a5ed9182SOmar Sandoval 
80a5ed9182SOmar Sandoval 	ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*info));
81a5ed9182SOmar Sandoval 	if (ret)
82a5ed9182SOmar Sandoval 		goto out;
83a5ed9182SOmar Sandoval 
84a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
85a5ed9182SOmar Sandoval 	info = btrfs_item_ptr(leaf, path->slots[0],
86a5ed9182SOmar Sandoval 			      struct btrfs_free_space_info);
87a5ed9182SOmar Sandoval 	btrfs_set_free_space_extent_count(leaf, info, 0);
88a5ed9182SOmar Sandoval 	btrfs_set_free_space_flags(leaf, info, 0);
89a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(leaf);
90a5ed9182SOmar Sandoval 
91a5ed9182SOmar Sandoval 	ret = 0;
92a5ed9182SOmar Sandoval out:
93a5ed9182SOmar Sandoval 	btrfs_release_path(path);
94a5ed9182SOmar Sandoval 	return ret;
95a5ed9182SOmar Sandoval }
96a5ed9182SOmar Sandoval 
97ce9f967fSJohannes Thumshirn EXPORT_FOR_TESTS
98ce9f967fSJohannes Thumshirn struct btrfs_free_space_info *search_free_space_info(
992ccf545eSDavid Sterba 		struct btrfs_trans_handle *trans,
10032da5386SDavid Sterba 		struct btrfs_block_group *block_group,
101a5ed9182SOmar Sandoval 		struct btrfs_path *path, int cow)
102a5ed9182SOmar Sandoval {
1032ccf545eSDavid Sterba 	struct btrfs_fs_info *fs_info = block_group->fs_info;
1047939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
105a5ed9182SOmar Sandoval 	struct btrfs_key key;
106a5ed9182SOmar Sandoval 	int ret;
107a5ed9182SOmar Sandoval 
108b3470b5dSDavid Sterba 	key.objectid = block_group->start;
109a5ed9182SOmar Sandoval 	key.type = BTRFS_FREE_SPACE_INFO_KEY;
110b3470b5dSDavid Sterba 	key.offset = block_group->length;
111a5ed9182SOmar Sandoval 
112a5ed9182SOmar Sandoval 	ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
113a5ed9182SOmar Sandoval 	if (ret < 0)
114a5ed9182SOmar Sandoval 		return ERR_PTR(ret);
115a5ed9182SOmar Sandoval 	if (ret != 0) {
116ab8d0fc4SJeff Mahoney 		btrfs_warn(fs_info, "missing free space info for %llu",
117b3470b5dSDavid Sterba 			   block_group->start);
118a5ed9182SOmar Sandoval 		ASSERT(0);
119a5ed9182SOmar Sandoval 		return ERR_PTR(-ENOENT);
120a5ed9182SOmar Sandoval 	}
121a5ed9182SOmar Sandoval 
122a5ed9182SOmar Sandoval 	return btrfs_item_ptr(path->nodes[0], path->slots[0],
123a5ed9182SOmar Sandoval 			      struct btrfs_free_space_info);
124a5ed9182SOmar Sandoval }
125a5ed9182SOmar Sandoval 
126a5ed9182SOmar Sandoval /*
127a5ed9182SOmar Sandoval  * btrfs_search_slot() but we're looking for the greatest key less than the
128a5ed9182SOmar Sandoval  * passed key.
129a5ed9182SOmar Sandoval  */
130a5ed9182SOmar Sandoval static int btrfs_search_prev_slot(struct btrfs_trans_handle *trans,
131a5ed9182SOmar Sandoval 				  struct btrfs_root *root,
132a5ed9182SOmar Sandoval 				  struct btrfs_key *key, struct btrfs_path *p,
133a5ed9182SOmar Sandoval 				  int ins_len, int cow)
134a5ed9182SOmar Sandoval {
135a5ed9182SOmar Sandoval 	int ret;
136a5ed9182SOmar Sandoval 
137a5ed9182SOmar Sandoval 	ret = btrfs_search_slot(trans, root, key, p, ins_len, cow);
138a5ed9182SOmar Sandoval 	if (ret < 0)
139a5ed9182SOmar Sandoval 		return ret;
140a5ed9182SOmar Sandoval 
141a5ed9182SOmar Sandoval 	if (ret == 0) {
142a5ed9182SOmar Sandoval 		ASSERT(0);
143a5ed9182SOmar Sandoval 		return -EIO;
144a5ed9182SOmar Sandoval 	}
145a5ed9182SOmar Sandoval 
146a5ed9182SOmar Sandoval 	if (p->slots[0] == 0) {
147a5ed9182SOmar Sandoval 		ASSERT(0);
148a5ed9182SOmar Sandoval 		return -EIO;
149a5ed9182SOmar Sandoval 	}
150a5ed9182SOmar Sandoval 	p->slots[0]--;
151a5ed9182SOmar Sandoval 
152a5ed9182SOmar Sandoval 	return 0;
153a5ed9182SOmar Sandoval }
154a5ed9182SOmar Sandoval 
155098e6308SDavid Sterba static inline u32 free_space_bitmap_size(const struct btrfs_fs_info *fs_info,
156098e6308SDavid Sterba 					 u64 size)
157a5ed9182SOmar Sandoval {
158098e6308SDavid Sterba 	return DIV_ROUND_UP(size >> fs_info->sectorsize_bits, BITS_PER_BYTE);
159a5ed9182SOmar Sandoval }
160a5ed9182SOmar Sandoval 
161a565971fSHoward McLauchlan static unsigned long *alloc_bitmap(u32 bitmap_size)
162a5ed9182SOmar Sandoval {
163a565971fSHoward McLauchlan 	unsigned long *ret;
16425ff17e8SOmar Sandoval 	unsigned int nofs_flag;
165a565971fSHoward McLauchlan 	u32 bitmap_rounded_size = round_up(bitmap_size, sizeof(unsigned long));
16679b134a2SDavid Sterba 
16779b134a2SDavid Sterba 	/*
16825ff17e8SOmar Sandoval 	 * GFP_NOFS doesn't work with kvmalloc(), but we really can't recurse
16925ff17e8SOmar Sandoval 	 * into the filesystem as the free space bitmap can be modified in the
17025ff17e8SOmar Sandoval 	 * critical section of a transaction commit.
17125ff17e8SOmar Sandoval 	 *
17225ff17e8SOmar Sandoval 	 * TODO: push the memalloc_nofs_{save,restore}() to the caller where we
17325ff17e8SOmar Sandoval 	 * know that recursion is unsafe.
17479b134a2SDavid Sterba 	 */
17525ff17e8SOmar Sandoval 	nofs_flag = memalloc_nofs_save();
176a565971fSHoward McLauchlan 	ret = kvzalloc(bitmap_rounded_size, GFP_KERNEL);
17725ff17e8SOmar Sandoval 	memalloc_nofs_restore(nofs_flag);
17825ff17e8SOmar Sandoval 	return ret;
179a5ed9182SOmar Sandoval }
180a5ed9182SOmar Sandoval 
181a565971fSHoward McLauchlan static void le_bitmap_set(unsigned long *map, unsigned int start, int len)
1826faa8f47SHoward McLauchlan {
183a565971fSHoward McLauchlan 	u8 *p = ((u8 *)map) + BIT_BYTE(start);
1846faa8f47SHoward McLauchlan 	const unsigned int size = start + len;
1856faa8f47SHoward McLauchlan 	int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
1866faa8f47SHoward McLauchlan 	u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
1876faa8f47SHoward McLauchlan 
1886faa8f47SHoward McLauchlan 	while (len - bits_to_set >= 0) {
1896faa8f47SHoward McLauchlan 		*p |= mask_to_set;
1906faa8f47SHoward McLauchlan 		len -= bits_to_set;
1916faa8f47SHoward McLauchlan 		bits_to_set = BITS_PER_BYTE;
1926faa8f47SHoward McLauchlan 		mask_to_set = ~0;
1936faa8f47SHoward McLauchlan 		p++;
1946faa8f47SHoward McLauchlan 	}
1956faa8f47SHoward McLauchlan 	if (len) {
1966faa8f47SHoward McLauchlan 		mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
1976faa8f47SHoward McLauchlan 		*p |= mask_to_set;
1986faa8f47SHoward McLauchlan 	}
1996faa8f47SHoward McLauchlan }
2006faa8f47SHoward McLauchlan 
201ce9f967fSJohannes Thumshirn EXPORT_FOR_TESTS
202a5ed9182SOmar Sandoval int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
20332da5386SDavid Sterba 				  struct btrfs_block_group *block_group,
204a5ed9182SOmar Sandoval 				  struct btrfs_path *path)
205a5ed9182SOmar Sandoval {
206719fb4deSNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
2077939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
208a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
209a5ed9182SOmar Sandoval 	struct btrfs_key key, found_key;
210a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
211a565971fSHoward McLauchlan 	unsigned long *bitmap;
212a565971fSHoward McLauchlan 	char *bitmap_cursor;
213a5ed9182SOmar Sandoval 	u64 start, end;
214a5ed9182SOmar Sandoval 	u64 bitmap_range, i;
215a5ed9182SOmar Sandoval 	u32 bitmap_size, flags, expected_extent_count;
216a5ed9182SOmar Sandoval 	u32 extent_count = 0;
217a5ed9182SOmar Sandoval 	int done = 0, nr;
218a5ed9182SOmar Sandoval 	int ret;
219a5ed9182SOmar Sandoval 
220098e6308SDavid Sterba 	bitmap_size = free_space_bitmap_size(fs_info, block_group->length);
221a5ed9182SOmar Sandoval 	bitmap = alloc_bitmap(bitmap_size);
222a5ed9182SOmar Sandoval 	if (!bitmap) {
223a5ed9182SOmar Sandoval 		ret = -ENOMEM;
224a5ed9182SOmar Sandoval 		goto out;
225a5ed9182SOmar Sandoval 	}
226a5ed9182SOmar Sandoval 
227b3470b5dSDavid Sterba 	start = block_group->start;
228b3470b5dSDavid Sterba 	end = block_group->start + block_group->length;
229a5ed9182SOmar Sandoval 
230a5ed9182SOmar Sandoval 	key.objectid = end - 1;
231a5ed9182SOmar Sandoval 	key.type = (u8)-1;
232a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
233a5ed9182SOmar Sandoval 
234a5ed9182SOmar Sandoval 	while (!done) {
235a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
236a5ed9182SOmar Sandoval 		if (ret)
237a5ed9182SOmar Sandoval 			goto out;
238a5ed9182SOmar Sandoval 
239a5ed9182SOmar Sandoval 		leaf = path->nodes[0];
240a5ed9182SOmar Sandoval 		nr = 0;
241a5ed9182SOmar Sandoval 		path->slots[0]++;
242a5ed9182SOmar Sandoval 		while (path->slots[0] > 0) {
243a5ed9182SOmar Sandoval 			btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
244a5ed9182SOmar Sandoval 
245a5ed9182SOmar Sandoval 			if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
246b3470b5dSDavid Sterba 				ASSERT(found_key.objectid == block_group->start);
247b3470b5dSDavid Sterba 				ASSERT(found_key.offset == block_group->length);
248a5ed9182SOmar Sandoval 				done = 1;
249a5ed9182SOmar Sandoval 				break;
250a5ed9182SOmar Sandoval 			} else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) {
251a5ed9182SOmar Sandoval 				u64 first, last;
252a5ed9182SOmar Sandoval 
253a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid >= start);
254a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid < end);
255a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid + found_key.offset <= end);
256a5ed9182SOmar Sandoval 
257a5ed9182SOmar Sandoval 				first = div_u64(found_key.objectid - start,
2580b246afaSJeff Mahoney 						fs_info->sectorsize);
259a5ed9182SOmar Sandoval 				last = div_u64(found_key.objectid + found_key.offset - start,
2600b246afaSJeff Mahoney 					       fs_info->sectorsize);
2612fe1d551SOmar Sandoval 				le_bitmap_set(bitmap, first, last - first);
262a5ed9182SOmar Sandoval 
263a5ed9182SOmar Sandoval 				extent_count++;
264a5ed9182SOmar Sandoval 				nr++;
265a5ed9182SOmar Sandoval 				path->slots[0]--;
266a5ed9182SOmar Sandoval 			} else {
267a5ed9182SOmar Sandoval 				ASSERT(0);
268a5ed9182SOmar Sandoval 			}
269a5ed9182SOmar Sandoval 		}
270a5ed9182SOmar Sandoval 
271a5ed9182SOmar Sandoval 		ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
272a5ed9182SOmar Sandoval 		if (ret)
273a5ed9182SOmar Sandoval 			goto out;
274a5ed9182SOmar Sandoval 		btrfs_release_path(path);
275a5ed9182SOmar Sandoval 	}
276a5ed9182SOmar Sandoval 
2772ccf545eSDavid Sterba 	info = search_free_space_info(trans, block_group, path, 1);
278a5ed9182SOmar Sandoval 	if (IS_ERR(info)) {
279a5ed9182SOmar Sandoval 		ret = PTR_ERR(info);
280a5ed9182SOmar Sandoval 		goto out;
281a5ed9182SOmar Sandoval 	}
282a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
283a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(leaf, info);
284a5ed9182SOmar Sandoval 	flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
285a5ed9182SOmar Sandoval 	btrfs_set_free_space_flags(leaf, info, flags);
286a5ed9182SOmar Sandoval 	expected_extent_count = btrfs_free_space_extent_count(leaf, info);
287a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(leaf);
288a5ed9182SOmar Sandoval 	btrfs_release_path(path);
289a5ed9182SOmar Sandoval 
290a5ed9182SOmar Sandoval 	if (extent_count != expected_extent_count) {
2915d163e0eSJeff Mahoney 		btrfs_err(fs_info,
2925d163e0eSJeff Mahoney 			  "incorrect extent count for %llu; counted %u, expected %u",
293b3470b5dSDavid Sterba 			  block_group->start, extent_count,
294a5ed9182SOmar Sandoval 			  expected_extent_count);
295a5ed9182SOmar Sandoval 		ASSERT(0);
296a5ed9182SOmar Sandoval 		ret = -EIO;
297a5ed9182SOmar Sandoval 		goto out;
298a5ed9182SOmar Sandoval 	}
299a5ed9182SOmar Sandoval 
300a565971fSHoward McLauchlan 	bitmap_cursor = (char *)bitmap;
3010b246afaSJeff Mahoney 	bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
302a5ed9182SOmar Sandoval 	i = start;
303a5ed9182SOmar Sandoval 	while (i < end) {
304a5ed9182SOmar Sandoval 		unsigned long ptr;
305a5ed9182SOmar Sandoval 		u64 extent_size;
306a5ed9182SOmar Sandoval 		u32 data_size;
307a5ed9182SOmar Sandoval 
308a5ed9182SOmar Sandoval 		extent_size = min(end - i, bitmap_range);
309098e6308SDavid Sterba 		data_size = free_space_bitmap_size(fs_info, extent_size);
310a5ed9182SOmar Sandoval 
311a5ed9182SOmar Sandoval 		key.objectid = i;
312a5ed9182SOmar Sandoval 		key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
313a5ed9182SOmar Sandoval 		key.offset = extent_size;
314a5ed9182SOmar Sandoval 
315a5ed9182SOmar Sandoval 		ret = btrfs_insert_empty_item(trans, root, path, &key,
316a5ed9182SOmar Sandoval 					      data_size);
317a5ed9182SOmar Sandoval 		if (ret)
318a5ed9182SOmar Sandoval 			goto out;
319a5ed9182SOmar Sandoval 
320a5ed9182SOmar Sandoval 		leaf = path->nodes[0];
321a5ed9182SOmar Sandoval 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
322a5ed9182SOmar Sandoval 		write_extent_buffer(leaf, bitmap_cursor, ptr,
323a5ed9182SOmar Sandoval 				    data_size);
324a5ed9182SOmar Sandoval 		btrfs_mark_buffer_dirty(leaf);
325a5ed9182SOmar Sandoval 		btrfs_release_path(path);
326a5ed9182SOmar Sandoval 
327a5ed9182SOmar Sandoval 		i += extent_size;
328a5ed9182SOmar Sandoval 		bitmap_cursor += data_size;
329a5ed9182SOmar Sandoval 	}
330a5ed9182SOmar Sandoval 
331a5ed9182SOmar Sandoval 	ret = 0;
332a5ed9182SOmar Sandoval out:
33379b134a2SDavid Sterba 	kvfree(bitmap);
334a5ed9182SOmar Sandoval 	if (ret)
33566642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
336a5ed9182SOmar Sandoval 	return ret;
337a5ed9182SOmar Sandoval }
338a5ed9182SOmar Sandoval 
339ce9f967fSJohannes Thumshirn EXPORT_FOR_TESTS
340a5ed9182SOmar Sandoval int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
34132da5386SDavid Sterba 				  struct btrfs_block_group *block_group,
342a5ed9182SOmar Sandoval 				  struct btrfs_path *path)
343a5ed9182SOmar Sandoval {
3445296c2bfSNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
3457939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
346a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
347a5ed9182SOmar Sandoval 	struct btrfs_key key, found_key;
348a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
349a565971fSHoward McLauchlan 	unsigned long *bitmap;
350a5ed9182SOmar Sandoval 	u64 start, end;
351a5ed9182SOmar Sandoval 	u32 bitmap_size, flags, expected_extent_count;
352a565971fSHoward McLauchlan 	unsigned long nrbits, start_bit, end_bit;
353a5ed9182SOmar Sandoval 	u32 extent_count = 0;
354a5ed9182SOmar Sandoval 	int done = 0, nr;
355a5ed9182SOmar Sandoval 	int ret;
356a5ed9182SOmar Sandoval 
357098e6308SDavid Sterba 	bitmap_size = free_space_bitmap_size(fs_info, block_group->length);
358a5ed9182SOmar Sandoval 	bitmap = alloc_bitmap(bitmap_size);
359a5ed9182SOmar Sandoval 	if (!bitmap) {
360a5ed9182SOmar Sandoval 		ret = -ENOMEM;
361a5ed9182SOmar Sandoval 		goto out;
362a5ed9182SOmar Sandoval 	}
363a5ed9182SOmar Sandoval 
364b3470b5dSDavid Sterba 	start = block_group->start;
365b3470b5dSDavid Sterba 	end = block_group->start + block_group->length;
366a5ed9182SOmar Sandoval 
367a5ed9182SOmar Sandoval 	key.objectid = end - 1;
368a5ed9182SOmar Sandoval 	key.type = (u8)-1;
369a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
370a5ed9182SOmar Sandoval 
371a5ed9182SOmar Sandoval 	while (!done) {
372a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
373a5ed9182SOmar Sandoval 		if (ret)
374a5ed9182SOmar Sandoval 			goto out;
375a5ed9182SOmar Sandoval 
376a5ed9182SOmar Sandoval 		leaf = path->nodes[0];
377a5ed9182SOmar Sandoval 		nr = 0;
378a5ed9182SOmar Sandoval 		path->slots[0]++;
379a5ed9182SOmar Sandoval 		while (path->slots[0] > 0) {
380a5ed9182SOmar Sandoval 			btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
381a5ed9182SOmar Sandoval 
382a5ed9182SOmar Sandoval 			if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
383b3470b5dSDavid Sterba 				ASSERT(found_key.objectid == block_group->start);
384b3470b5dSDavid Sterba 				ASSERT(found_key.offset == block_group->length);
385a5ed9182SOmar Sandoval 				done = 1;
386a5ed9182SOmar Sandoval 				break;
387a5ed9182SOmar Sandoval 			} else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
388a5ed9182SOmar Sandoval 				unsigned long ptr;
389a565971fSHoward McLauchlan 				char *bitmap_cursor;
390a5ed9182SOmar Sandoval 				u32 bitmap_pos, data_size;
391a5ed9182SOmar Sandoval 
392a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid >= start);
393a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid < end);
394a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid + found_key.offset <= end);
395a5ed9182SOmar Sandoval 
396a5ed9182SOmar Sandoval 				bitmap_pos = div_u64(found_key.objectid - start,
3970b246afaSJeff Mahoney 						     fs_info->sectorsize *
398a5ed9182SOmar Sandoval 						     BITS_PER_BYTE);
399a565971fSHoward McLauchlan 				bitmap_cursor = ((char *)bitmap) + bitmap_pos;
400098e6308SDavid Sterba 				data_size = free_space_bitmap_size(fs_info,
401098e6308SDavid Sterba 								found_key.offset);
402a5ed9182SOmar Sandoval 
403a5ed9182SOmar Sandoval 				ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
404a5ed9182SOmar Sandoval 				read_extent_buffer(leaf, bitmap_cursor, ptr,
405a5ed9182SOmar Sandoval 						   data_size);
406a5ed9182SOmar Sandoval 
407a5ed9182SOmar Sandoval 				nr++;
408a5ed9182SOmar Sandoval 				path->slots[0]--;
409a5ed9182SOmar Sandoval 			} else {
410a5ed9182SOmar Sandoval 				ASSERT(0);
411a5ed9182SOmar Sandoval 			}
412a5ed9182SOmar Sandoval 		}
413a5ed9182SOmar Sandoval 
414a5ed9182SOmar Sandoval 		ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
415a5ed9182SOmar Sandoval 		if (ret)
416a5ed9182SOmar Sandoval 			goto out;
417a5ed9182SOmar Sandoval 		btrfs_release_path(path);
418a5ed9182SOmar Sandoval 	}
419a5ed9182SOmar Sandoval 
4202ccf545eSDavid Sterba 	info = search_free_space_info(trans, block_group, path, 1);
421a5ed9182SOmar Sandoval 	if (IS_ERR(info)) {
422a5ed9182SOmar Sandoval 		ret = PTR_ERR(info);
423a5ed9182SOmar Sandoval 		goto out;
424a5ed9182SOmar Sandoval 	}
425a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
426a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(leaf, info);
427a5ed9182SOmar Sandoval 	flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
428a5ed9182SOmar Sandoval 	btrfs_set_free_space_flags(leaf, info, flags);
429a5ed9182SOmar Sandoval 	expected_extent_count = btrfs_free_space_extent_count(leaf, info);
430a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(leaf);
431a5ed9182SOmar Sandoval 	btrfs_release_path(path);
432a5ed9182SOmar Sandoval 
433ab108d99SDavid Sterba 	nrbits = block_group->length >> block_group->fs_info->sectorsize_bits;
434a565971fSHoward McLauchlan 	start_bit = find_next_bit_le(bitmap, nrbits, 0);
435a565971fSHoward McLauchlan 
436a565971fSHoward McLauchlan 	while (start_bit < nrbits) {
437a565971fSHoward McLauchlan 		end_bit = find_next_zero_bit_le(bitmap, nrbits, start_bit);
438a565971fSHoward McLauchlan 		ASSERT(start_bit < end_bit);
439a565971fSHoward McLauchlan 
440a565971fSHoward McLauchlan 		key.objectid = start + start_bit * block_group->fs_info->sectorsize;
441a5ed9182SOmar Sandoval 		key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
442a565971fSHoward McLauchlan 		key.offset = (end_bit - start_bit) * block_group->fs_info->sectorsize;
443a5ed9182SOmar Sandoval 
444a5ed9182SOmar Sandoval 		ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
445a5ed9182SOmar Sandoval 		if (ret)
446a5ed9182SOmar Sandoval 			goto out;
447a5ed9182SOmar Sandoval 		btrfs_release_path(path);
448a5ed9182SOmar Sandoval 
449a5ed9182SOmar Sandoval 		extent_count++;
450a5ed9182SOmar Sandoval 
451a565971fSHoward McLauchlan 		start_bit = find_next_bit_le(bitmap, nrbits, end_bit);
452a5ed9182SOmar Sandoval 	}
453a5ed9182SOmar Sandoval 
454a5ed9182SOmar Sandoval 	if (extent_count != expected_extent_count) {
4555d163e0eSJeff Mahoney 		btrfs_err(fs_info,
4565d163e0eSJeff Mahoney 			  "incorrect extent count for %llu; counted %u, expected %u",
457b3470b5dSDavid Sterba 			  block_group->start, extent_count,
458a5ed9182SOmar Sandoval 			  expected_extent_count);
459a5ed9182SOmar Sandoval 		ASSERT(0);
460a5ed9182SOmar Sandoval 		ret = -EIO;
461a5ed9182SOmar Sandoval 		goto out;
462a5ed9182SOmar Sandoval 	}
463a5ed9182SOmar Sandoval 
464a5ed9182SOmar Sandoval 	ret = 0;
465a5ed9182SOmar Sandoval out:
46679b134a2SDavid Sterba 	kvfree(bitmap);
467a5ed9182SOmar Sandoval 	if (ret)
46866642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
469a5ed9182SOmar Sandoval 	return ret;
470a5ed9182SOmar Sandoval }
471a5ed9182SOmar Sandoval 
472a5ed9182SOmar Sandoval static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
47332da5386SDavid Sterba 					  struct btrfs_block_group *block_group,
474a5ed9182SOmar Sandoval 					  struct btrfs_path *path,
475a5ed9182SOmar Sandoval 					  int new_extents)
476a5ed9182SOmar Sandoval {
477a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
478a5ed9182SOmar Sandoval 	u32 flags;
479a5ed9182SOmar Sandoval 	u32 extent_count;
480a5ed9182SOmar Sandoval 	int ret = 0;
481a5ed9182SOmar Sandoval 
482a5ed9182SOmar Sandoval 	if (new_extents == 0)
483a5ed9182SOmar Sandoval 		return 0;
484a5ed9182SOmar Sandoval 
4852ccf545eSDavid Sterba 	info = search_free_space_info(trans, block_group, path, 1);
486a5ed9182SOmar Sandoval 	if (IS_ERR(info)) {
487a5ed9182SOmar Sandoval 		ret = PTR_ERR(info);
488a5ed9182SOmar Sandoval 		goto out;
489a5ed9182SOmar Sandoval 	}
490a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(path->nodes[0], info);
491a5ed9182SOmar Sandoval 	extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
492a5ed9182SOmar Sandoval 
493a5ed9182SOmar Sandoval 	extent_count += new_extents;
494a5ed9182SOmar Sandoval 	btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
495a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(path->nodes[0]);
496a5ed9182SOmar Sandoval 	btrfs_release_path(path);
497a5ed9182SOmar Sandoval 
498a5ed9182SOmar Sandoval 	if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
499a5ed9182SOmar Sandoval 	    extent_count > block_group->bitmap_high_thresh) {
500719fb4deSNikolay Borisov 		ret = convert_free_space_to_bitmaps(trans, block_group, path);
501a5ed9182SOmar Sandoval 	} else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
502a5ed9182SOmar Sandoval 		   extent_count < block_group->bitmap_low_thresh) {
5035296c2bfSNikolay Borisov 		ret = convert_free_space_to_extents(trans, block_group, path);
504a5ed9182SOmar Sandoval 	}
505a5ed9182SOmar Sandoval 
506a5ed9182SOmar Sandoval out:
507a5ed9182SOmar Sandoval 	return ret;
508a5ed9182SOmar Sandoval }
509a5ed9182SOmar Sandoval 
510ce9f967fSJohannes Thumshirn EXPORT_FOR_TESTS
51132da5386SDavid Sterba int free_space_test_bit(struct btrfs_block_group *block_group,
512a5ed9182SOmar Sandoval 			struct btrfs_path *path, u64 offset)
513a5ed9182SOmar Sandoval {
514a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
515a5ed9182SOmar Sandoval 	struct btrfs_key key;
516a5ed9182SOmar Sandoval 	u64 found_start, found_end;
517a5ed9182SOmar Sandoval 	unsigned long ptr, i;
518a5ed9182SOmar Sandoval 
519a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
520a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
521a5ed9182SOmar Sandoval 	ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
522a5ed9182SOmar Sandoval 
523a5ed9182SOmar Sandoval 	found_start = key.objectid;
524a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
525a5ed9182SOmar Sandoval 	ASSERT(offset >= found_start && offset < found_end);
526a5ed9182SOmar Sandoval 
527a5ed9182SOmar Sandoval 	ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
528da17066cSJeff Mahoney 	i = div_u64(offset - found_start,
529da17066cSJeff Mahoney 		    block_group->fs_info->sectorsize);
530a5ed9182SOmar Sandoval 	return !!extent_buffer_test_bit(leaf, ptr, i);
531a5ed9182SOmar Sandoval }
532a5ed9182SOmar Sandoval 
53332da5386SDavid Sterba static void free_space_set_bits(struct btrfs_block_group *block_group,
534a5ed9182SOmar Sandoval 				struct btrfs_path *path, u64 *start, u64 *size,
535a5ed9182SOmar Sandoval 				int bit)
536a5ed9182SOmar Sandoval {
5370b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = block_group->fs_info;
538a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
539a5ed9182SOmar Sandoval 	struct btrfs_key key;
540a5ed9182SOmar Sandoval 	u64 end = *start + *size;
541a5ed9182SOmar Sandoval 	u64 found_start, found_end;
542a5ed9182SOmar Sandoval 	unsigned long ptr, first, last;
543a5ed9182SOmar Sandoval 
544a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
545a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
546a5ed9182SOmar Sandoval 	ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
547a5ed9182SOmar Sandoval 
548a5ed9182SOmar Sandoval 	found_start = key.objectid;
549a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
550a5ed9182SOmar Sandoval 	ASSERT(*start >= found_start && *start < found_end);
551a5ed9182SOmar Sandoval 	ASSERT(end > found_start);
552a5ed9182SOmar Sandoval 
553a5ed9182SOmar Sandoval 	if (end > found_end)
554a5ed9182SOmar Sandoval 		end = found_end;
555a5ed9182SOmar Sandoval 
556a5ed9182SOmar Sandoval 	ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
557ab108d99SDavid Sterba 	first = (*start - found_start) >> fs_info->sectorsize_bits;
558ab108d99SDavid Sterba 	last = (end - found_start) >> fs_info->sectorsize_bits;
559a5ed9182SOmar Sandoval 	if (bit)
560a5ed9182SOmar Sandoval 		extent_buffer_bitmap_set(leaf, ptr, first, last - first);
561a5ed9182SOmar Sandoval 	else
562a5ed9182SOmar Sandoval 		extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
563a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(leaf);
564a5ed9182SOmar Sandoval 
565a5ed9182SOmar Sandoval 	*size -= end - *start;
566a5ed9182SOmar Sandoval 	*start = end;
567a5ed9182SOmar Sandoval }
568a5ed9182SOmar Sandoval 
569a5ed9182SOmar Sandoval /*
570a5ed9182SOmar Sandoval  * We can't use btrfs_next_item() in modify_free_space_bitmap() because
571a5ed9182SOmar Sandoval  * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
572a5ed9182SOmar Sandoval  * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
573a5ed9182SOmar Sandoval  * looking for.
574a5ed9182SOmar Sandoval  */
575a5ed9182SOmar Sandoval static int free_space_next_bitmap(struct btrfs_trans_handle *trans,
576a5ed9182SOmar Sandoval 				  struct btrfs_root *root, struct btrfs_path *p)
577a5ed9182SOmar Sandoval {
578a5ed9182SOmar Sandoval 	struct btrfs_key key;
579a5ed9182SOmar Sandoval 
580a5ed9182SOmar Sandoval 	if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
581a5ed9182SOmar Sandoval 		p->slots[0]++;
582a5ed9182SOmar Sandoval 		return 0;
583a5ed9182SOmar Sandoval 	}
584a5ed9182SOmar Sandoval 
585a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
586a5ed9182SOmar Sandoval 	btrfs_release_path(p);
587a5ed9182SOmar Sandoval 
588a5ed9182SOmar Sandoval 	key.objectid += key.offset;
589a5ed9182SOmar Sandoval 	key.type = (u8)-1;
590a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
591a5ed9182SOmar Sandoval 
592a5ed9182SOmar Sandoval 	return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
593a5ed9182SOmar Sandoval }
594a5ed9182SOmar Sandoval 
595a5ed9182SOmar Sandoval /*
596a5ed9182SOmar Sandoval  * If remove is 1, then we are removing free space, thus clearing bits in the
597a5ed9182SOmar Sandoval  * bitmap. If remove is 0, then we are adding free space, thus setting bits in
598a5ed9182SOmar Sandoval  * the bitmap.
599a5ed9182SOmar Sandoval  */
600a5ed9182SOmar Sandoval static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
60132da5386SDavid Sterba 				    struct btrfs_block_group *block_group,
602a5ed9182SOmar Sandoval 				    struct btrfs_path *path,
603a5ed9182SOmar Sandoval 				    u64 start, u64 size, int remove)
604a5ed9182SOmar Sandoval {
6057939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
606a5ed9182SOmar Sandoval 	struct btrfs_key key;
607a5ed9182SOmar Sandoval 	u64 end = start + size;
608a5ed9182SOmar Sandoval 	u64 cur_start, cur_size;
609a5ed9182SOmar Sandoval 	int prev_bit, next_bit;
610a5ed9182SOmar Sandoval 	int new_extents;
611a5ed9182SOmar Sandoval 	int ret;
612a5ed9182SOmar Sandoval 
613a5ed9182SOmar Sandoval 	/*
614a5ed9182SOmar Sandoval 	 * Read the bit for the block immediately before the extent of space if
615a5ed9182SOmar Sandoval 	 * that block is within the block group.
616a5ed9182SOmar Sandoval 	 */
617b3470b5dSDavid Sterba 	if (start > block_group->start) {
618da17066cSJeff Mahoney 		u64 prev_block = start - block_group->fs_info->sectorsize;
619a5ed9182SOmar Sandoval 
620a5ed9182SOmar Sandoval 		key.objectid = prev_block;
621a5ed9182SOmar Sandoval 		key.type = (u8)-1;
622a5ed9182SOmar Sandoval 		key.offset = (u64)-1;
623a5ed9182SOmar Sandoval 
624a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
625a5ed9182SOmar Sandoval 		if (ret)
626a5ed9182SOmar Sandoval 			goto out;
627a5ed9182SOmar Sandoval 
628a5ed9182SOmar Sandoval 		prev_bit = free_space_test_bit(block_group, path, prev_block);
629a5ed9182SOmar Sandoval 
630a5ed9182SOmar Sandoval 		/* The previous block may have been in the previous bitmap. */
631a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
632a5ed9182SOmar Sandoval 		if (start >= key.objectid + key.offset) {
633a5ed9182SOmar Sandoval 			ret = free_space_next_bitmap(trans, root, path);
634a5ed9182SOmar Sandoval 			if (ret)
635a5ed9182SOmar Sandoval 				goto out;
636a5ed9182SOmar Sandoval 		}
637a5ed9182SOmar Sandoval 	} else {
638a5ed9182SOmar Sandoval 		key.objectid = start;
639a5ed9182SOmar Sandoval 		key.type = (u8)-1;
640a5ed9182SOmar Sandoval 		key.offset = (u64)-1;
641a5ed9182SOmar Sandoval 
642a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
643a5ed9182SOmar Sandoval 		if (ret)
644a5ed9182SOmar Sandoval 			goto out;
645a5ed9182SOmar Sandoval 
646a5ed9182SOmar Sandoval 		prev_bit = -1;
647a5ed9182SOmar Sandoval 	}
648a5ed9182SOmar Sandoval 
649a5ed9182SOmar Sandoval 	/*
650a5ed9182SOmar Sandoval 	 * Iterate over all of the bitmaps overlapped by the extent of space,
651a5ed9182SOmar Sandoval 	 * clearing/setting bits as required.
652a5ed9182SOmar Sandoval 	 */
653a5ed9182SOmar Sandoval 	cur_start = start;
654a5ed9182SOmar Sandoval 	cur_size = size;
655a5ed9182SOmar Sandoval 	while (1) {
656a5ed9182SOmar Sandoval 		free_space_set_bits(block_group, path, &cur_start, &cur_size,
657a5ed9182SOmar Sandoval 				    !remove);
658a5ed9182SOmar Sandoval 		if (cur_size == 0)
659a5ed9182SOmar Sandoval 			break;
660a5ed9182SOmar Sandoval 		ret = free_space_next_bitmap(trans, root, path);
661a5ed9182SOmar Sandoval 		if (ret)
662a5ed9182SOmar Sandoval 			goto out;
663a5ed9182SOmar Sandoval 	}
664a5ed9182SOmar Sandoval 
665a5ed9182SOmar Sandoval 	/*
666a5ed9182SOmar Sandoval 	 * Read the bit for the block immediately after the extent of space if
667a5ed9182SOmar Sandoval 	 * that block is within the block group.
668a5ed9182SOmar Sandoval 	 */
669b3470b5dSDavid Sterba 	if (end < block_group->start + block_group->length) {
670a5ed9182SOmar Sandoval 		/* The next block may be in the next bitmap. */
671a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
672a5ed9182SOmar Sandoval 		if (end >= key.objectid + key.offset) {
673a5ed9182SOmar Sandoval 			ret = free_space_next_bitmap(trans, root, path);
674a5ed9182SOmar Sandoval 			if (ret)
675a5ed9182SOmar Sandoval 				goto out;
676a5ed9182SOmar Sandoval 		}
677a5ed9182SOmar Sandoval 
678a5ed9182SOmar Sandoval 		next_bit = free_space_test_bit(block_group, path, end);
679a5ed9182SOmar Sandoval 	} else {
680a5ed9182SOmar Sandoval 		next_bit = -1;
681a5ed9182SOmar Sandoval 	}
682a5ed9182SOmar Sandoval 
683a5ed9182SOmar Sandoval 	if (remove) {
684a5ed9182SOmar Sandoval 		new_extents = -1;
685a5ed9182SOmar Sandoval 		if (prev_bit == 1) {
686a5ed9182SOmar Sandoval 			/* Leftover on the left. */
687a5ed9182SOmar Sandoval 			new_extents++;
688a5ed9182SOmar Sandoval 		}
689a5ed9182SOmar Sandoval 		if (next_bit == 1) {
690a5ed9182SOmar Sandoval 			/* Leftover on the right. */
691a5ed9182SOmar Sandoval 			new_extents++;
692a5ed9182SOmar Sandoval 		}
693a5ed9182SOmar Sandoval 	} else {
694a5ed9182SOmar Sandoval 		new_extents = 1;
695a5ed9182SOmar Sandoval 		if (prev_bit == 1) {
696a5ed9182SOmar Sandoval 			/* Merging with neighbor on the left. */
697a5ed9182SOmar Sandoval 			new_extents--;
698a5ed9182SOmar Sandoval 		}
699a5ed9182SOmar Sandoval 		if (next_bit == 1) {
700a5ed9182SOmar Sandoval 			/* Merging with neighbor on the right. */
701a5ed9182SOmar Sandoval 			new_extents--;
702a5ed9182SOmar Sandoval 		}
703a5ed9182SOmar Sandoval 	}
704a5ed9182SOmar Sandoval 
705a5ed9182SOmar Sandoval 	btrfs_release_path(path);
706690d7682SNikolay Borisov 	ret = update_free_space_extent_count(trans, block_group, path,
707a5ed9182SOmar Sandoval 					     new_extents);
708a5ed9182SOmar Sandoval 
709a5ed9182SOmar Sandoval out:
710a5ed9182SOmar Sandoval 	return ret;
711a5ed9182SOmar Sandoval }
712a5ed9182SOmar Sandoval 
713a5ed9182SOmar Sandoval static int remove_free_space_extent(struct btrfs_trans_handle *trans,
71432da5386SDavid Sterba 				    struct btrfs_block_group *block_group,
715a5ed9182SOmar Sandoval 				    struct btrfs_path *path,
716a5ed9182SOmar Sandoval 				    u64 start, u64 size)
717a5ed9182SOmar Sandoval {
7187939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
719a5ed9182SOmar Sandoval 	struct btrfs_key key;
720a5ed9182SOmar Sandoval 	u64 found_start, found_end;
721a5ed9182SOmar Sandoval 	u64 end = start + size;
722a5ed9182SOmar Sandoval 	int new_extents = -1;
723a5ed9182SOmar Sandoval 	int ret;
724a5ed9182SOmar Sandoval 
725a5ed9182SOmar Sandoval 	key.objectid = start;
726a5ed9182SOmar Sandoval 	key.type = (u8)-1;
727a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
728a5ed9182SOmar Sandoval 
729a5ed9182SOmar Sandoval 	ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
730a5ed9182SOmar Sandoval 	if (ret)
731a5ed9182SOmar Sandoval 		goto out;
732a5ed9182SOmar Sandoval 
733a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
734a5ed9182SOmar Sandoval 
735a5ed9182SOmar Sandoval 	ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
736a5ed9182SOmar Sandoval 
737a5ed9182SOmar Sandoval 	found_start = key.objectid;
738a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
739a5ed9182SOmar Sandoval 	ASSERT(start >= found_start && end <= found_end);
740a5ed9182SOmar Sandoval 
741a5ed9182SOmar Sandoval 	/*
742a5ed9182SOmar Sandoval 	 * Okay, now that we've found the free space extent which contains the
743a5ed9182SOmar Sandoval 	 * free space that we are removing, there are four cases:
744a5ed9182SOmar Sandoval 	 *
745a5ed9182SOmar Sandoval 	 * 1. We're using the whole extent: delete the key we found and
746a5ed9182SOmar Sandoval 	 * decrement the free space extent count.
747a5ed9182SOmar Sandoval 	 * 2. We are using part of the extent starting at the beginning: delete
748a5ed9182SOmar Sandoval 	 * the key we found and insert a new key representing the leftover at
749a5ed9182SOmar Sandoval 	 * the end. There is no net change in the number of extents.
750a5ed9182SOmar Sandoval 	 * 3. We are using part of the extent ending at the end: delete the key
751a5ed9182SOmar Sandoval 	 * we found and insert a new key representing the leftover at the
752a5ed9182SOmar Sandoval 	 * beginning. There is no net change in the number of extents.
753a5ed9182SOmar Sandoval 	 * 4. We are using part of the extent in the middle: delete the key we
754a5ed9182SOmar Sandoval 	 * found and insert two new keys representing the leftovers on each
755a5ed9182SOmar Sandoval 	 * side. Where we used to have one extent, we now have two, so increment
756a5ed9182SOmar Sandoval 	 * the extent count. We may need to convert the block group to bitmaps
757a5ed9182SOmar Sandoval 	 * as a result.
758a5ed9182SOmar Sandoval 	 */
759a5ed9182SOmar Sandoval 
760a5ed9182SOmar Sandoval 	/* Delete the existing key (cases 1-4). */
761a5ed9182SOmar Sandoval 	ret = btrfs_del_item(trans, root, path);
762a5ed9182SOmar Sandoval 	if (ret)
763a5ed9182SOmar Sandoval 		goto out;
764a5ed9182SOmar Sandoval 
765a5ed9182SOmar Sandoval 	/* Add a key for leftovers at the beginning (cases 3 and 4). */
766a5ed9182SOmar Sandoval 	if (start > found_start) {
767a5ed9182SOmar Sandoval 		key.objectid = found_start;
768a5ed9182SOmar Sandoval 		key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
769a5ed9182SOmar Sandoval 		key.offset = start - found_start;
770a5ed9182SOmar Sandoval 
771a5ed9182SOmar Sandoval 		btrfs_release_path(path);
772a5ed9182SOmar Sandoval 		ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
773a5ed9182SOmar Sandoval 		if (ret)
774a5ed9182SOmar Sandoval 			goto out;
775a5ed9182SOmar Sandoval 		new_extents++;
776a5ed9182SOmar Sandoval 	}
777a5ed9182SOmar Sandoval 
778a5ed9182SOmar Sandoval 	/* Add a key for leftovers at the end (cases 2 and 4). */
779a5ed9182SOmar Sandoval 	if (end < found_end) {
780a5ed9182SOmar Sandoval 		key.objectid = end;
781a5ed9182SOmar Sandoval 		key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
782a5ed9182SOmar Sandoval 		key.offset = found_end - end;
783a5ed9182SOmar Sandoval 
784a5ed9182SOmar Sandoval 		btrfs_release_path(path);
785a5ed9182SOmar Sandoval 		ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
786a5ed9182SOmar Sandoval 		if (ret)
787a5ed9182SOmar Sandoval 			goto out;
788a5ed9182SOmar Sandoval 		new_extents++;
789a5ed9182SOmar Sandoval 	}
790a5ed9182SOmar Sandoval 
791a5ed9182SOmar Sandoval 	btrfs_release_path(path);
792690d7682SNikolay Borisov 	ret = update_free_space_extent_count(trans, block_group, path,
793a5ed9182SOmar Sandoval 					     new_extents);
794a5ed9182SOmar Sandoval 
795a5ed9182SOmar Sandoval out:
796a5ed9182SOmar Sandoval 	return ret;
797a5ed9182SOmar Sandoval }
798a5ed9182SOmar Sandoval 
799ce9f967fSJohannes Thumshirn EXPORT_FOR_TESTS
800a5ed9182SOmar Sandoval int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
80132da5386SDavid Sterba 				  struct btrfs_block_group *block_group,
802a5ed9182SOmar Sandoval 				  struct btrfs_path *path, u64 start, u64 size)
803a5ed9182SOmar Sandoval {
804a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
805a5ed9182SOmar Sandoval 	u32 flags;
806a5ed9182SOmar Sandoval 	int ret;
807a5ed9182SOmar Sandoval 
808a5ed9182SOmar Sandoval 	if (block_group->needs_free_space) {
8099a7e0f92SNikolay Borisov 		ret = __add_block_group_free_space(trans, block_group, path);
810a5ed9182SOmar Sandoval 		if (ret)
811a5ed9182SOmar Sandoval 			return ret;
812a5ed9182SOmar Sandoval 	}
813a5ed9182SOmar Sandoval 
8142ccf545eSDavid Sterba 	info = search_free_space_info(NULL, block_group, path, 0);
815a5ed9182SOmar Sandoval 	if (IS_ERR(info))
816a5ed9182SOmar Sandoval 		return PTR_ERR(info);
817a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(path->nodes[0], info);
818a5ed9182SOmar Sandoval 	btrfs_release_path(path);
819a5ed9182SOmar Sandoval 
820a5ed9182SOmar Sandoval 	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
82185a7ef13SNikolay Borisov 		return modify_free_space_bitmap(trans, block_group, path,
82285a7ef13SNikolay Borisov 						start, size, 1);
823a5ed9182SOmar Sandoval 	} else {
824e581168dSNikolay Borisov 		return remove_free_space_extent(trans, block_group, path,
825e581168dSNikolay Borisov 						start, size);
826a5ed9182SOmar Sandoval 	}
827a5ed9182SOmar Sandoval }
828a5ed9182SOmar Sandoval 
829a5ed9182SOmar Sandoval int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
830a5ed9182SOmar Sandoval 				u64 start, u64 size)
831a5ed9182SOmar Sandoval {
83232da5386SDavid Sterba 	struct btrfs_block_group *block_group;
833a5ed9182SOmar Sandoval 	struct btrfs_path *path;
834a5ed9182SOmar Sandoval 	int ret;
835a5ed9182SOmar Sandoval 
83625a356d3SNikolay Borisov 	if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
837a5ed9182SOmar Sandoval 		return 0;
838a5ed9182SOmar Sandoval 
839a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
840a5ed9182SOmar Sandoval 	if (!path) {
841a5ed9182SOmar Sandoval 		ret = -ENOMEM;
842a5ed9182SOmar Sandoval 		goto out;
843a5ed9182SOmar Sandoval 	}
844a5ed9182SOmar Sandoval 
84525a356d3SNikolay Borisov 	block_group = btrfs_lookup_block_group(trans->fs_info, start);
846a5ed9182SOmar Sandoval 	if (!block_group) {
847a5ed9182SOmar Sandoval 		ASSERT(0);
848a5ed9182SOmar Sandoval 		ret = -ENOENT;
849a5ed9182SOmar Sandoval 		goto out;
850a5ed9182SOmar Sandoval 	}
851a5ed9182SOmar Sandoval 
852a5ed9182SOmar Sandoval 	mutex_lock(&block_group->free_space_lock);
853c31683a6SNikolay Borisov 	ret = __remove_from_free_space_tree(trans, block_group, path, start,
854c31683a6SNikolay Borisov 					    size);
855a5ed9182SOmar Sandoval 	mutex_unlock(&block_group->free_space_lock);
856a5ed9182SOmar Sandoval 
857a5ed9182SOmar Sandoval 	btrfs_put_block_group(block_group);
858a5ed9182SOmar Sandoval out:
859a5ed9182SOmar Sandoval 	btrfs_free_path(path);
860a5ed9182SOmar Sandoval 	if (ret)
86166642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
862a5ed9182SOmar Sandoval 	return ret;
863a5ed9182SOmar Sandoval }
864a5ed9182SOmar Sandoval 
865a5ed9182SOmar Sandoval static int add_free_space_extent(struct btrfs_trans_handle *trans,
86632da5386SDavid Sterba 				 struct btrfs_block_group *block_group,
867a5ed9182SOmar Sandoval 				 struct btrfs_path *path,
868a5ed9182SOmar Sandoval 				 u64 start, u64 size)
869a5ed9182SOmar Sandoval {
8707939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
871a5ed9182SOmar Sandoval 	struct btrfs_key key, new_key;
872a5ed9182SOmar Sandoval 	u64 found_start, found_end;
873a5ed9182SOmar Sandoval 	u64 end = start + size;
874a5ed9182SOmar Sandoval 	int new_extents = 1;
875a5ed9182SOmar Sandoval 	int ret;
876a5ed9182SOmar Sandoval 
877a5ed9182SOmar Sandoval 	/*
878a5ed9182SOmar Sandoval 	 * We are adding a new extent of free space, but we need to merge
879a5ed9182SOmar Sandoval 	 * extents. There are four cases here:
880a5ed9182SOmar Sandoval 	 *
881a5ed9182SOmar Sandoval 	 * 1. The new extent does not have any immediate neighbors to merge
882a5ed9182SOmar Sandoval 	 * with: add the new key and increment the free space extent count. We
883a5ed9182SOmar Sandoval 	 * may need to convert the block group to bitmaps as a result.
884a5ed9182SOmar Sandoval 	 * 2. The new extent has an immediate neighbor before it: remove the
885a5ed9182SOmar Sandoval 	 * previous key and insert a new key combining both of them. There is no
886a5ed9182SOmar Sandoval 	 * net change in the number of extents.
887a5ed9182SOmar Sandoval 	 * 3. The new extent has an immediate neighbor after it: remove the next
888a5ed9182SOmar Sandoval 	 * key and insert a new key combining both of them. There is no net
889a5ed9182SOmar Sandoval 	 * change in the number of extents.
890a5ed9182SOmar Sandoval 	 * 4. The new extent has immediate neighbors on both sides: remove both
891a5ed9182SOmar Sandoval 	 * of the keys and insert a new key combining all of them. Where we used
892a5ed9182SOmar Sandoval 	 * to have two extents, we now have one, so decrement the extent count.
893a5ed9182SOmar Sandoval 	 */
894a5ed9182SOmar Sandoval 
895a5ed9182SOmar Sandoval 	new_key.objectid = start;
896a5ed9182SOmar Sandoval 	new_key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
897a5ed9182SOmar Sandoval 	new_key.offset = size;
898a5ed9182SOmar Sandoval 
899a5ed9182SOmar Sandoval 	/* Search for a neighbor on the left. */
900b3470b5dSDavid Sterba 	if (start == block_group->start)
901a5ed9182SOmar Sandoval 		goto right;
902a5ed9182SOmar Sandoval 	key.objectid = start - 1;
903a5ed9182SOmar Sandoval 	key.type = (u8)-1;
904a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
905a5ed9182SOmar Sandoval 
906a5ed9182SOmar Sandoval 	ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
907a5ed9182SOmar Sandoval 	if (ret)
908a5ed9182SOmar Sandoval 		goto out;
909a5ed9182SOmar Sandoval 
910a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
911a5ed9182SOmar Sandoval 
912a5ed9182SOmar Sandoval 	if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
913a5ed9182SOmar Sandoval 		ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
914a5ed9182SOmar Sandoval 		btrfs_release_path(path);
915a5ed9182SOmar Sandoval 		goto right;
916a5ed9182SOmar Sandoval 	}
917a5ed9182SOmar Sandoval 
918a5ed9182SOmar Sandoval 	found_start = key.objectid;
919a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
920b3470b5dSDavid Sterba 	ASSERT(found_start >= block_group->start &&
921b3470b5dSDavid Sterba 	       found_end > block_group->start);
922a5ed9182SOmar Sandoval 	ASSERT(found_start < start && found_end <= start);
923a5ed9182SOmar Sandoval 
924a5ed9182SOmar Sandoval 	/*
925a5ed9182SOmar Sandoval 	 * Delete the neighbor on the left and absorb it into the new key (cases
926a5ed9182SOmar Sandoval 	 * 2 and 4).
927a5ed9182SOmar Sandoval 	 */
928a5ed9182SOmar Sandoval 	if (found_end == start) {
929a5ed9182SOmar Sandoval 		ret = btrfs_del_item(trans, root, path);
930a5ed9182SOmar Sandoval 		if (ret)
931a5ed9182SOmar Sandoval 			goto out;
932a5ed9182SOmar Sandoval 		new_key.objectid = found_start;
933a5ed9182SOmar Sandoval 		new_key.offset += key.offset;
934a5ed9182SOmar Sandoval 		new_extents--;
935a5ed9182SOmar Sandoval 	}
936a5ed9182SOmar Sandoval 	btrfs_release_path(path);
937a5ed9182SOmar Sandoval 
938a5ed9182SOmar Sandoval right:
939a5ed9182SOmar Sandoval 	/* Search for a neighbor on the right. */
940b3470b5dSDavid Sterba 	if (end == block_group->start + block_group->length)
941a5ed9182SOmar Sandoval 		goto insert;
942a5ed9182SOmar Sandoval 	key.objectid = end;
943a5ed9182SOmar Sandoval 	key.type = (u8)-1;
944a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
945a5ed9182SOmar Sandoval 
946a5ed9182SOmar Sandoval 	ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
947a5ed9182SOmar Sandoval 	if (ret)
948a5ed9182SOmar Sandoval 		goto out;
949a5ed9182SOmar Sandoval 
950a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
951a5ed9182SOmar Sandoval 
952a5ed9182SOmar Sandoval 	if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
953a5ed9182SOmar Sandoval 		ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
954a5ed9182SOmar Sandoval 		btrfs_release_path(path);
955a5ed9182SOmar Sandoval 		goto insert;
956a5ed9182SOmar Sandoval 	}
957a5ed9182SOmar Sandoval 
958a5ed9182SOmar Sandoval 	found_start = key.objectid;
959a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
960b3470b5dSDavid Sterba 	ASSERT(found_start >= block_group->start &&
961b3470b5dSDavid Sterba 	       found_end > block_group->start);
962a5ed9182SOmar Sandoval 	ASSERT((found_start < start && found_end <= start) ||
963a5ed9182SOmar Sandoval 	       (found_start >= end && found_end > end));
964a5ed9182SOmar Sandoval 
965a5ed9182SOmar Sandoval 	/*
966a5ed9182SOmar Sandoval 	 * Delete the neighbor on the right and absorb it into the new key
967a5ed9182SOmar Sandoval 	 * (cases 3 and 4).
968a5ed9182SOmar Sandoval 	 */
969a5ed9182SOmar Sandoval 	if (found_start == end) {
970a5ed9182SOmar Sandoval 		ret = btrfs_del_item(trans, root, path);
971a5ed9182SOmar Sandoval 		if (ret)
972a5ed9182SOmar Sandoval 			goto out;
973a5ed9182SOmar Sandoval 		new_key.offset += key.offset;
974a5ed9182SOmar Sandoval 		new_extents--;
975a5ed9182SOmar Sandoval 	}
976a5ed9182SOmar Sandoval 	btrfs_release_path(path);
977a5ed9182SOmar Sandoval 
978a5ed9182SOmar Sandoval insert:
979a5ed9182SOmar Sandoval 	/* Insert the new key (cases 1-4). */
980a5ed9182SOmar Sandoval 	ret = btrfs_insert_empty_item(trans, root, path, &new_key, 0);
981a5ed9182SOmar Sandoval 	if (ret)
982a5ed9182SOmar Sandoval 		goto out;
983a5ed9182SOmar Sandoval 
984a5ed9182SOmar Sandoval 	btrfs_release_path(path);
985690d7682SNikolay Borisov 	ret = update_free_space_extent_count(trans, block_group, path,
986a5ed9182SOmar Sandoval 					     new_extents);
987a5ed9182SOmar Sandoval 
988a5ed9182SOmar Sandoval out:
989a5ed9182SOmar Sandoval 	return ret;
990a5ed9182SOmar Sandoval }
991a5ed9182SOmar Sandoval 
992ce9f967fSJohannes Thumshirn EXPORT_FOR_TESTS
993a5ed9182SOmar Sandoval int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
99432da5386SDavid Sterba 			     struct btrfs_block_group *block_group,
995a5ed9182SOmar Sandoval 			     struct btrfs_path *path, u64 start, u64 size)
996a5ed9182SOmar Sandoval {
997a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
998a5ed9182SOmar Sandoval 	u32 flags;
999a5ed9182SOmar Sandoval 	int ret;
1000a5ed9182SOmar Sandoval 
1001a5ed9182SOmar Sandoval 	if (block_group->needs_free_space) {
10029a7e0f92SNikolay Borisov 		ret = __add_block_group_free_space(trans, block_group, path);
1003a5ed9182SOmar Sandoval 		if (ret)
1004a5ed9182SOmar Sandoval 			return ret;
1005a5ed9182SOmar Sandoval 	}
1006a5ed9182SOmar Sandoval 
10072ccf545eSDavid Sterba 	info = search_free_space_info(NULL, block_group, path, 0);
1008a5ed9182SOmar Sandoval 	if (IS_ERR(info))
1009a5ed9182SOmar Sandoval 		return PTR_ERR(info);
1010a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(path->nodes[0], info);
1011a5ed9182SOmar Sandoval 	btrfs_release_path(path);
1012a5ed9182SOmar Sandoval 
1013a5ed9182SOmar Sandoval 	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
101485a7ef13SNikolay Borisov 		return modify_free_space_bitmap(trans, block_group, path,
101585a7ef13SNikolay Borisov 						start, size, 0);
1016a5ed9182SOmar Sandoval 	} else {
10175cb17822SNikolay Borisov 		return add_free_space_extent(trans, block_group, path, start,
10185cb17822SNikolay Borisov 					     size);
1019a5ed9182SOmar Sandoval 	}
1020a5ed9182SOmar Sandoval }
1021a5ed9182SOmar Sandoval 
1022a5ed9182SOmar Sandoval int add_to_free_space_tree(struct btrfs_trans_handle *trans,
1023a5ed9182SOmar Sandoval 			   u64 start, u64 size)
1024a5ed9182SOmar Sandoval {
102532da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1026a5ed9182SOmar Sandoval 	struct btrfs_path *path;
1027a5ed9182SOmar Sandoval 	int ret;
1028a5ed9182SOmar Sandoval 
1029e7355e50SNikolay Borisov 	if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
1030a5ed9182SOmar Sandoval 		return 0;
1031a5ed9182SOmar Sandoval 
1032a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1033a5ed9182SOmar Sandoval 	if (!path) {
1034a5ed9182SOmar Sandoval 		ret = -ENOMEM;
1035a5ed9182SOmar Sandoval 		goto out;
1036a5ed9182SOmar Sandoval 	}
1037a5ed9182SOmar Sandoval 
1038e7355e50SNikolay Borisov 	block_group = btrfs_lookup_block_group(trans->fs_info, start);
1039a5ed9182SOmar Sandoval 	if (!block_group) {
1040a5ed9182SOmar Sandoval 		ASSERT(0);
1041a5ed9182SOmar Sandoval 		ret = -ENOENT;
1042a5ed9182SOmar Sandoval 		goto out;
1043a5ed9182SOmar Sandoval 	}
1044a5ed9182SOmar Sandoval 
1045a5ed9182SOmar Sandoval 	mutex_lock(&block_group->free_space_lock);
10462d5cffa1SNikolay Borisov 	ret = __add_to_free_space_tree(trans, block_group, path, start, size);
1047a5ed9182SOmar Sandoval 	mutex_unlock(&block_group->free_space_lock);
1048a5ed9182SOmar Sandoval 
1049a5ed9182SOmar Sandoval 	btrfs_put_block_group(block_group);
1050a5ed9182SOmar Sandoval out:
1051a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1052a5ed9182SOmar Sandoval 	if (ret)
105366642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1054a5ed9182SOmar Sandoval 	return ret;
1055a5ed9182SOmar Sandoval }
1056a5ed9182SOmar Sandoval 
1057a5ed9182SOmar Sandoval /*
1058a5ed9182SOmar Sandoval  * Populate the free space tree by walking the extent tree. Operations on the
1059a5ed9182SOmar Sandoval  * extent tree that happen as a result of writes to the free space tree will go
1060a5ed9182SOmar Sandoval  * through the normal add/remove hooks.
1061a5ed9182SOmar Sandoval  */
1062a5ed9182SOmar Sandoval static int populate_free_space_tree(struct btrfs_trans_handle *trans,
106332da5386SDavid Sterba 				    struct btrfs_block_group *block_group)
1064a5ed9182SOmar Sandoval {
106529cbcf40SJosef Bacik 	struct btrfs_root *extent_root;
1066a5ed9182SOmar Sandoval 	struct btrfs_path *path, *path2;
1067a5ed9182SOmar Sandoval 	struct btrfs_key key;
1068a5ed9182SOmar Sandoval 	u64 start, end;
1069a5ed9182SOmar Sandoval 	int ret;
1070a5ed9182SOmar Sandoval 
1071a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1072a5ed9182SOmar Sandoval 	if (!path)
1073a5ed9182SOmar Sandoval 		return -ENOMEM;
1074019599adSGu Jinxiang 	path->reada = READA_FORWARD;
1075a5ed9182SOmar Sandoval 
1076a5ed9182SOmar Sandoval 	path2 = btrfs_alloc_path();
1077a5ed9182SOmar Sandoval 	if (!path2) {
1078a5ed9182SOmar Sandoval 		btrfs_free_path(path);
1079a5ed9182SOmar Sandoval 		return -ENOMEM;
1080a5ed9182SOmar Sandoval 	}
1081a5ed9182SOmar Sandoval 
108266afee18SNikolay Borisov 	ret = add_new_free_space_info(trans, block_group, path2);
1083a5ed9182SOmar Sandoval 	if (ret)
1084a5ed9182SOmar Sandoval 		goto out;
1085a5ed9182SOmar Sandoval 
1086511711afSChris Mason 	mutex_lock(&block_group->free_space_lock);
1087511711afSChris Mason 
1088a5ed9182SOmar Sandoval 	/*
1089a5ed9182SOmar Sandoval 	 * Iterate through all of the extent and metadata items in this block
1090a5ed9182SOmar Sandoval 	 * group, adding the free space between them and the free space at the
1091a5ed9182SOmar Sandoval 	 * end. Note that EXTENT_ITEM and METADATA_ITEM are less than
1092a5ed9182SOmar Sandoval 	 * BLOCK_GROUP_ITEM, so an extent may precede the block group that it's
1093a5ed9182SOmar Sandoval 	 * contained in.
1094a5ed9182SOmar Sandoval 	 */
1095b3470b5dSDavid Sterba 	key.objectid = block_group->start;
1096a5ed9182SOmar Sandoval 	key.type = BTRFS_EXTENT_ITEM_KEY;
1097a5ed9182SOmar Sandoval 	key.offset = 0;
1098a5ed9182SOmar Sandoval 
109929cbcf40SJosef Bacik 	extent_root = btrfs_extent_root(trans->fs_info, key.objectid);
1100a5ed9182SOmar Sandoval 	ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
1101a5ed9182SOmar Sandoval 	if (ret < 0)
1102511711afSChris Mason 		goto out_locked;
1103a5ed9182SOmar Sandoval 	ASSERT(ret == 0);
1104a5ed9182SOmar Sandoval 
1105b3470b5dSDavid Sterba 	start = block_group->start;
1106b3470b5dSDavid Sterba 	end = block_group->start + block_group->length;
1107a5ed9182SOmar Sandoval 	while (1) {
1108a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1109a5ed9182SOmar Sandoval 
1110a5ed9182SOmar Sandoval 		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
1111a5ed9182SOmar Sandoval 		    key.type == BTRFS_METADATA_ITEM_KEY) {
1112a5ed9182SOmar Sandoval 			if (key.objectid >= end)
1113a5ed9182SOmar Sandoval 				break;
1114a5ed9182SOmar Sandoval 
1115a5ed9182SOmar Sandoval 			if (start < key.objectid) {
11162d5cffa1SNikolay Borisov 				ret = __add_to_free_space_tree(trans,
1117a5ed9182SOmar Sandoval 							       block_group,
1118a5ed9182SOmar Sandoval 							       path2, start,
1119a5ed9182SOmar Sandoval 							       key.objectid -
1120a5ed9182SOmar Sandoval 							       start);
1121a5ed9182SOmar Sandoval 				if (ret)
1122511711afSChris Mason 					goto out_locked;
1123a5ed9182SOmar Sandoval 			}
1124a5ed9182SOmar Sandoval 			start = key.objectid;
1125a5ed9182SOmar Sandoval 			if (key.type == BTRFS_METADATA_ITEM_KEY)
1126ffa9a9efSNikolay Borisov 				start += trans->fs_info->nodesize;
1127a5ed9182SOmar Sandoval 			else
1128a5ed9182SOmar Sandoval 				start += key.offset;
1129a5ed9182SOmar Sandoval 		} else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
1130b3470b5dSDavid Sterba 			if (key.objectid != block_group->start)
1131a5ed9182SOmar Sandoval 				break;
1132a5ed9182SOmar Sandoval 		}
1133a5ed9182SOmar Sandoval 
1134a5ed9182SOmar Sandoval 		ret = btrfs_next_item(extent_root, path);
1135a5ed9182SOmar Sandoval 		if (ret < 0)
1136511711afSChris Mason 			goto out_locked;
1137a5ed9182SOmar Sandoval 		if (ret)
1138a5ed9182SOmar Sandoval 			break;
1139a5ed9182SOmar Sandoval 	}
1140a5ed9182SOmar Sandoval 	if (start < end) {
11412d5cffa1SNikolay Borisov 		ret = __add_to_free_space_tree(trans, block_group, path2,
11422d5cffa1SNikolay Borisov 					       start, end - start);
1143a5ed9182SOmar Sandoval 		if (ret)
1144511711afSChris Mason 			goto out_locked;
1145a5ed9182SOmar Sandoval 	}
1146a5ed9182SOmar Sandoval 
1147a5ed9182SOmar Sandoval 	ret = 0;
1148511711afSChris Mason out_locked:
1149511711afSChris Mason 	mutex_unlock(&block_group->free_space_lock);
1150a5ed9182SOmar Sandoval out:
1151a5ed9182SOmar Sandoval 	btrfs_free_path(path2);
1152a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1153a5ed9182SOmar Sandoval 	return ret;
1154a5ed9182SOmar Sandoval }
1155a5ed9182SOmar Sandoval 
1156a5ed9182SOmar Sandoval int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
1157a5ed9182SOmar Sandoval {
1158a5ed9182SOmar Sandoval 	struct btrfs_trans_handle *trans;
1159a5ed9182SOmar Sandoval 	struct btrfs_root *tree_root = fs_info->tree_root;
1160a5ed9182SOmar Sandoval 	struct btrfs_root *free_space_root;
116132da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1162a5ed9182SOmar Sandoval 	struct rb_node *node;
1163a5ed9182SOmar Sandoval 	int ret;
1164a5ed9182SOmar Sandoval 
1165a5ed9182SOmar Sandoval 	trans = btrfs_start_transaction(tree_root, 0);
1166a5ed9182SOmar Sandoval 	if (IS_ERR(trans))
1167a5ed9182SOmar Sandoval 		return PTR_ERR(trans);
1168a5ed9182SOmar Sandoval 
1169afcdd129SJosef Bacik 	set_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
11702f96e402SJosef Bacik 	set_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
11719b7a2440SDavid Sterba 	free_space_root = btrfs_create_tree(trans,
1172a5ed9182SOmar Sandoval 					    BTRFS_FREE_SPACE_TREE_OBJECTID);
1173a5ed9182SOmar Sandoval 	if (IS_ERR(free_space_root)) {
1174a5ed9182SOmar Sandoval 		ret = PTR_ERR(free_space_root);
1175a5ed9182SOmar Sandoval 		goto abort;
1176a5ed9182SOmar Sandoval 	}
1177abed4aaaSJosef Bacik 	ret = btrfs_global_root_insert(free_space_root);
1178abed4aaaSJosef Bacik 	if (ret) {
1179abed4aaaSJosef Bacik 		btrfs_put_root(free_space_root);
1180abed4aaaSJosef Bacik 		goto abort;
1181abed4aaaSJosef Bacik 	}
1182a5ed9182SOmar Sandoval 
118308dddb29SFilipe Manana 	node = rb_first_cached(&fs_info->block_group_cache_tree);
1184a5ed9182SOmar Sandoval 	while (node) {
118532da5386SDavid Sterba 		block_group = rb_entry(node, struct btrfs_block_group,
1186a5ed9182SOmar Sandoval 				       cache_node);
1187ffa9a9efSNikolay Borisov 		ret = populate_free_space_tree(trans, block_group);
1188a5ed9182SOmar Sandoval 		if (ret)
1189a5ed9182SOmar Sandoval 			goto abort;
1190a5ed9182SOmar Sandoval 		node = rb_next(node);
1191a5ed9182SOmar Sandoval 	}
1192a5ed9182SOmar Sandoval 
1193a5ed9182SOmar Sandoval 	btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
11946675df31SOmar Sandoval 	btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1195afcdd129SJosef Bacik 	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
11962f96e402SJosef Bacik 	ret = btrfs_commit_transaction(trans);
1197a5ed9182SOmar Sandoval 
11982f96e402SJosef Bacik 	/*
11992f96e402SJosef Bacik 	 * Now that we've committed the transaction any reading of our commit
12002f96e402SJosef Bacik 	 * root will be safe, so we can cache from the free space tree now.
12012f96e402SJosef Bacik 	 */
12022f96e402SJosef Bacik 	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
12032f96e402SJosef Bacik 	return ret;
1204a5ed9182SOmar Sandoval 
1205a5ed9182SOmar Sandoval abort:
1206afcdd129SJosef Bacik 	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
12072f96e402SJosef Bacik 	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
120866642832SJeff Mahoney 	btrfs_abort_transaction(trans, ret);
12093a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
1210a5ed9182SOmar Sandoval 	return ret;
1211a5ed9182SOmar Sandoval }
1212a5ed9182SOmar Sandoval 
1213a5ed9182SOmar Sandoval static int clear_free_space_tree(struct btrfs_trans_handle *trans,
1214a5ed9182SOmar Sandoval 				 struct btrfs_root *root)
1215a5ed9182SOmar Sandoval {
1216a5ed9182SOmar Sandoval 	struct btrfs_path *path;
1217a5ed9182SOmar Sandoval 	struct btrfs_key key;
1218a5ed9182SOmar Sandoval 	int nr;
1219a5ed9182SOmar Sandoval 	int ret;
1220a5ed9182SOmar Sandoval 
1221a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1222a5ed9182SOmar Sandoval 	if (!path)
1223a5ed9182SOmar Sandoval 		return -ENOMEM;
1224a5ed9182SOmar Sandoval 
1225a5ed9182SOmar Sandoval 	key.objectid = 0;
1226a5ed9182SOmar Sandoval 	key.type = 0;
1227a5ed9182SOmar Sandoval 	key.offset = 0;
1228a5ed9182SOmar Sandoval 
1229a5ed9182SOmar Sandoval 	while (1) {
1230a5ed9182SOmar Sandoval 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1231a5ed9182SOmar Sandoval 		if (ret < 0)
1232a5ed9182SOmar Sandoval 			goto out;
1233a5ed9182SOmar Sandoval 
1234a5ed9182SOmar Sandoval 		nr = btrfs_header_nritems(path->nodes[0]);
1235a5ed9182SOmar Sandoval 		if (!nr)
1236a5ed9182SOmar Sandoval 			break;
1237a5ed9182SOmar Sandoval 
1238a5ed9182SOmar Sandoval 		path->slots[0] = 0;
1239a5ed9182SOmar Sandoval 		ret = btrfs_del_items(trans, root, path, 0, nr);
1240a5ed9182SOmar Sandoval 		if (ret)
1241a5ed9182SOmar Sandoval 			goto out;
1242a5ed9182SOmar Sandoval 
1243a5ed9182SOmar Sandoval 		btrfs_release_path(path);
1244a5ed9182SOmar Sandoval 	}
1245a5ed9182SOmar Sandoval 
1246a5ed9182SOmar Sandoval 	ret = 0;
1247a5ed9182SOmar Sandoval out:
1248a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1249a5ed9182SOmar Sandoval 	return ret;
1250a5ed9182SOmar Sandoval }
1251a5ed9182SOmar Sandoval 
1252a5ed9182SOmar Sandoval int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
1253a5ed9182SOmar Sandoval {
1254a5ed9182SOmar Sandoval 	struct btrfs_trans_handle *trans;
1255a5ed9182SOmar Sandoval 	struct btrfs_root *tree_root = fs_info->tree_root;
1256abed4aaaSJosef Bacik 	struct btrfs_key key = {
1257abed4aaaSJosef Bacik 		.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID,
1258abed4aaaSJosef Bacik 		.type = BTRFS_ROOT_ITEM_KEY,
1259abed4aaaSJosef Bacik 		.offset = 0,
1260abed4aaaSJosef Bacik 	};
1261abed4aaaSJosef Bacik 	struct btrfs_root *free_space_root = btrfs_global_root(fs_info, &key);
1262a5ed9182SOmar Sandoval 	int ret;
1263a5ed9182SOmar Sandoval 
1264a5ed9182SOmar Sandoval 	trans = btrfs_start_transaction(tree_root, 0);
1265a5ed9182SOmar Sandoval 	if (IS_ERR(trans))
1266a5ed9182SOmar Sandoval 		return PTR_ERR(trans);
1267a5ed9182SOmar Sandoval 
1268a5ed9182SOmar Sandoval 	btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE);
12696675df31SOmar Sandoval 	btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1270a5ed9182SOmar Sandoval 
1271a5ed9182SOmar Sandoval 	ret = clear_free_space_tree(trans, free_space_root);
1272a5ed9182SOmar Sandoval 	if (ret)
1273a5ed9182SOmar Sandoval 		goto abort;
1274a5ed9182SOmar Sandoval 
1275ab9ce7d4SLu Fengqi 	ret = btrfs_del_root(trans, &free_space_root->root_key);
1276a5ed9182SOmar Sandoval 	if (ret)
1277a5ed9182SOmar Sandoval 		goto abort;
1278a5ed9182SOmar Sandoval 
1279abed4aaaSJosef Bacik 	btrfs_global_root_delete(free_space_root);
1280a5ed9182SOmar Sandoval 	list_del(&free_space_root->dirty_list);
1281a5ed9182SOmar Sandoval 
1282a5ed9182SOmar Sandoval 	btrfs_tree_lock(free_space_root->node);
12836a884d7dSDavid Sterba 	btrfs_clean_tree_block(free_space_root->node);
1284a5ed9182SOmar Sandoval 	btrfs_tree_unlock(free_space_root->node);
12857a163608SFilipe Manana 	btrfs_free_tree_block(trans, btrfs_root_id(free_space_root),
12867a163608SFilipe Manana 			      free_space_root->node, 0, 1);
1287a5ed9182SOmar Sandoval 
128800246528SJosef Bacik 	btrfs_put_root(free_space_root);
1289a5ed9182SOmar Sandoval 
12900bef7109SSahil Kang 	return btrfs_commit_transaction(trans);
1291a5ed9182SOmar Sandoval 
1292a5ed9182SOmar Sandoval abort:
129366642832SJeff Mahoney 	btrfs_abort_transaction(trans, ret);
12943a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
1295a5ed9182SOmar Sandoval 	return ret;
1296a5ed9182SOmar Sandoval }
1297a5ed9182SOmar Sandoval 
1298a5ed9182SOmar Sandoval static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
129932da5386SDavid Sterba 					struct btrfs_block_group *block_group,
1300a5ed9182SOmar Sandoval 					struct btrfs_path *path)
1301a5ed9182SOmar Sandoval {
1302a5ed9182SOmar Sandoval 	int ret;
1303a5ed9182SOmar Sandoval 
1304a5ed9182SOmar Sandoval 	block_group->needs_free_space = 0;
1305a5ed9182SOmar Sandoval 
130666afee18SNikolay Borisov 	ret = add_new_free_space_info(trans, block_group, path);
1307a5ed9182SOmar Sandoval 	if (ret)
1308a5ed9182SOmar Sandoval 		return ret;
1309a5ed9182SOmar Sandoval 
13102d5cffa1SNikolay Borisov 	return __add_to_free_space_tree(trans, block_group, path,
1311b3470b5dSDavid Sterba 					block_group->start,
1312b3470b5dSDavid Sterba 					block_group->length);
1313a5ed9182SOmar Sandoval }
1314a5ed9182SOmar Sandoval 
1315a5ed9182SOmar Sandoval int add_block_group_free_space(struct btrfs_trans_handle *trans,
131632da5386SDavid Sterba 			       struct btrfs_block_group *block_group)
1317a5ed9182SOmar Sandoval {
1318e4e0711cSNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
1319a5ed9182SOmar Sandoval 	struct btrfs_path *path = NULL;
1320a5ed9182SOmar Sandoval 	int ret = 0;
1321a5ed9182SOmar Sandoval 
1322a5ed9182SOmar Sandoval 	if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1323a5ed9182SOmar Sandoval 		return 0;
1324a5ed9182SOmar Sandoval 
1325a5ed9182SOmar Sandoval 	mutex_lock(&block_group->free_space_lock);
1326a5ed9182SOmar Sandoval 	if (!block_group->needs_free_space)
1327a5ed9182SOmar Sandoval 		goto out;
1328a5ed9182SOmar Sandoval 
1329a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1330a5ed9182SOmar Sandoval 	if (!path) {
1331a5ed9182SOmar Sandoval 		ret = -ENOMEM;
1332a5ed9182SOmar Sandoval 		goto out;
1333a5ed9182SOmar Sandoval 	}
1334a5ed9182SOmar Sandoval 
13359a7e0f92SNikolay Borisov 	ret = __add_block_group_free_space(trans, block_group, path);
1336a5ed9182SOmar Sandoval 
1337a5ed9182SOmar Sandoval out:
1338a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1339a5ed9182SOmar Sandoval 	mutex_unlock(&block_group->free_space_lock);
1340a5ed9182SOmar Sandoval 	if (ret)
134166642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1342a5ed9182SOmar Sandoval 	return ret;
1343a5ed9182SOmar Sandoval }
1344a5ed9182SOmar Sandoval 
1345a5ed9182SOmar Sandoval int remove_block_group_free_space(struct btrfs_trans_handle *trans,
134632da5386SDavid Sterba 				  struct btrfs_block_group *block_group)
1347a5ed9182SOmar Sandoval {
13487939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
1349a5ed9182SOmar Sandoval 	struct btrfs_path *path;
1350a5ed9182SOmar Sandoval 	struct btrfs_key key, found_key;
1351a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
1352a5ed9182SOmar Sandoval 	u64 start, end;
1353a5ed9182SOmar Sandoval 	int done = 0, nr;
1354a5ed9182SOmar Sandoval 	int ret;
1355a5ed9182SOmar Sandoval 
1356f3f72779SNikolay Borisov 	if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
1357a5ed9182SOmar Sandoval 		return 0;
1358a5ed9182SOmar Sandoval 
1359a5ed9182SOmar Sandoval 	if (block_group->needs_free_space) {
1360a5ed9182SOmar Sandoval 		/* We never added this block group to the free space tree. */
1361a5ed9182SOmar Sandoval 		return 0;
1362a5ed9182SOmar Sandoval 	}
1363a5ed9182SOmar Sandoval 
1364a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1365a5ed9182SOmar Sandoval 	if (!path) {
1366a5ed9182SOmar Sandoval 		ret = -ENOMEM;
1367a5ed9182SOmar Sandoval 		goto out;
1368a5ed9182SOmar Sandoval 	}
1369a5ed9182SOmar Sandoval 
1370b3470b5dSDavid Sterba 	start = block_group->start;
1371b3470b5dSDavid Sterba 	end = block_group->start + block_group->length;
1372a5ed9182SOmar Sandoval 
1373a5ed9182SOmar Sandoval 	key.objectid = end - 1;
1374a5ed9182SOmar Sandoval 	key.type = (u8)-1;
1375a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
1376a5ed9182SOmar Sandoval 
1377a5ed9182SOmar Sandoval 	while (!done) {
1378a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
1379a5ed9182SOmar Sandoval 		if (ret)
1380a5ed9182SOmar Sandoval 			goto out;
1381a5ed9182SOmar Sandoval 
1382a5ed9182SOmar Sandoval 		leaf = path->nodes[0];
1383a5ed9182SOmar Sandoval 		nr = 0;
1384a5ed9182SOmar Sandoval 		path->slots[0]++;
1385a5ed9182SOmar Sandoval 		while (path->slots[0] > 0) {
1386a5ed9182SOmar Sandoval 			btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
1387a5ed9182SOmar Sandoval 
1388a5ed9182SOmar Sandoval 			if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
1389b3470b5dSDavid Sterba 				ASSERT(found_key.objectid == block_group->start);
1390b3470b5dSDavid Sterba 				ASSERT(found_key.offset == block_group->length);
1391a5ed9182SOmar Sandoval 				done = 1;
1392a5ed9182SOmar Sandoval 				nr++;
1393a5ed9182SOmar Sandoval 				path->slots[0]--;
1394a5ed9182SOmar Sandoval 				break;
1395a5ed9182SOmar Sandoval 			} else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY ||
1396a5ed9182SOmar Sandoval 				   found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
1397a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid >= start);
1398a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid < end);
1399a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid + found_key.offset <= end);
1400a5ed9182SOmar Sandoval 				nr++;
1401a5ed9182SOmar Sandoval 				path->slots[0]--;
1402a5ed9182SOmar Sandoval 			} else {
1403a5ed9182SOmar Sandoval 				ASSERT(0);
1404a5ed9182SOmar Sandoval 			}
1405a5ed9182SOmar Sandoval 		}
1406a5ed9182SOmar Sandoval 
1407a5ed9182SOmar Sandoval 		ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
1408a5ed9182SOmar Sandoval 		if (ret)
1409a5ed9182SOmar Sandoval 			goto out;
1410a5ed9182SOmar Sandoval 		btrfs_release_path(path);
1411a5ed9182SOmar Sandoval 	}
1412a5ed9182SOmar Sandoval 
1413a5ed9182SOmar Sandoval 	ret = 0;
1414a5ed9182SOmar Sandoval out:
1415a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1416a5ed9182SOmar Sandoval 	if (ret)
141766642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1418a5ed9182SOmar Sandoval 	return ret;
1419a5ed9182SOmar Sandoval }
1420a5ed9182SOmar Sandoval 
1421a5ed9182SOmar Sandoval static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
1422a5ed9182SOmar Sandoval 				   struct btrfs_path *path,
1423a5ed9182SOmar Sandoval 				   u32 expected_extent_count)
1424a5ed9182SOmar Sandoval {
142532da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1426a5ed9182SOmar Sandoval 	struct btrfs_fs_info *fs_info;
1427a5ed9182SOmar Sandoval 	struct btrfs_root *root;
1428a5ed9182SOmar Sandoval 	struct btrfs_key key;
1429a5ed9182SOmar Sandoval 	int prev_bit = 0, bit;
1430a5ed9182SOmar Sandoval 	/* Initialize to silence GCC. */
1431a5ed9182SOmar Sandoval 	u64 extent_start = 0;
1432a5ed9182SOmar Sandoval 	u64 end, offset;
1433a5ed9182SOmar Sandoval 	u64 total_found = 0;
1434a5ed9182SOmar Sandoval 	u32 extent_count = 0;
1435a5ed9182SOmar Sandoval 	int ret;
1436a5ed9182SOmar Sandoval 
1437a5ed9182SOmar Sandoval 	block_group = caching_ctl->block_group;
1438a5ed9182SOmar Sandoval 	fs_info = block_group->fs_info;
14397939dd9fSJosef Bacik 	root = btrfs_free_space_root(block_group);
1440a5ed9182SOmar Sandoval 
1441b3470b5dSDavid Sterba 	end = block_group->start + block_group->length;
1442a5ed9182SOmar Sandoval 
1443a5ed9182SOmar Sandoval 	while (1) {
1444a5ed9182SOmar Sandoval 		ret = btrfs_next_item(root, path);
1445a5ed9182SOmar Sandoval 		if (ret < 0)
1446a5ed9182SOmar Sandoval 			goto out;
1447a5ed9182SOmar Sandoval 		if (ret)
1448a5ed9182SOmar Sandoval 			break;
1449a5ed9182SOmar Sandoval 
1450a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1451a5ed9182SOmar Sandoval 
1452a5ed9182SOmar Sandoval 		if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1453a5ed9182SOmar Sandoval 			break;
1454a5ed9182SOmar Sandoval 
1455a5ed9182SOmar Sandoval 		ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
1456a5ed9182SOmar Sandoval 		ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1457a5ed9182SOmar Sandoval 
1458a5ed9182SOmar Sandoval 		offset = key.objectid;
1459a5ed9182SOmar Sandoval 		while (offset < key.objectid + key.offset) {
1460a5ed9182SOmar Sandoval 			bit = free_space_test_bit(block_group, path, offset);
1461a5ed9182SOmar Sandoval 			if (prev_bit == 0 && bit == 1) {
1462a5ed9182SOmar Sandoval 				extent_start = offset;
1463a5ed9182SOmar Sandoval 			} else if (prev_bit == 1 && bit == 0) {
1464a5ed9182SOmar Sandoval 				total_found += add_new_free_space(block_group,
1465a5ed9182SOmar Sandoval 								  extent_start,
1466a5ed9182SOmar Sandoval 								  offset);
1467a5ed9182SOmar Sandoval 				if (total_found > CACHING_CTL_WAKE_UP) {
1468a5ed9182SOmar Sandoval 					total_found = 0;
1469a5ed9182SOmar Sandoval 					wake_up(&caching_ctl->wait);
1470a5ed9182SOmar Sandoval 				}
1471a5ed9182SOmar Sandoval 				extent_count++;
1472a5ed9182SOmar Sandoval 			}
1473a5ed9182SOmar Sandoval 			prev_bit = bit;
14740b246afaSJeff Mahoney 			offset += fs_info->sectorsize;
1475a5ed9182SOmar Sandoval 		}
1476a5ed9182SOmar Sandoval 	}
1477a5ed9182SOmar Sandoval 	if (prev_bit == 1) {
14784457c1c7SNikolay Borisov 		total_found += add_new_free_space(block_group, extent_start,
14794457c1c7SNikolay Borisov 						  end);
1480a5ed9182SOmar Sandoval 		extent_count++;
1481a5ed9182SOmar Sandoval 	}
1482a5ed9182SOmar Sandoval 
1483a5ed9182SOmar Sandoval 	if (extent_count != expected_extent_count) {
14845d163e0eSJeff Mahoney 		btrfs_err(fs_info,
14855d163e0eSJeff Mahoney 			  "incorrect extent count for %llu; counted %u, expected %u",
1486b3470b5dSDavid Sterba 			  block_group->start, extent_count,
1487a5ed9182SOmar Sandoval 			  expected_extent_count);
1488a5ed9182SOmar Sandoval 		ASSERT(0);
1489a5ed9182SOmar Sandoval 		ret = -EIO;
1490a5ed9182SOmar Sandoval 		goto out;
1491a5ed9182SOmar Sandoval 	}
1492a5ed9182SOmar Sandoval 
1493a5ed9182SOmar Sandoval 	ret = 0;
1494a5ed9182SOmar Sandoval out:
1495a5ed9182SOmar Sandoval 	return ret;
1496a5ed9182SOmar Sandoval }
1497a5ed9182SOmar Sandoval 
1498a5ed9182SOmar Sandoval static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
1499a5ed9182SOmar Sandoval 				   struct btrfs_path *path,
1500a5ed9182SOmar Sandoval 				   u32 expected_extent_count)
1501a5ed9182SOmar Sandoval {
150232da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1503a5ed9182SOmar Sandoval 	struct btrfs_fs_info *fs_info;
1504a5ed9182SOmar Sandoval 	struct btrfs_root *root;
1505a5ed9182SOmar Sandoval 	struct btrfs_key key;
1506a5ed9182SOmar Sandoval 	u64 end;
1507a5ed9182SOmar Sandoval 	u64 total_found = 0;
1508a5ed9182SOmar Sandoval 	u32 extent_count = 0;
1509a5ed9182SOmar Sandoval 	int ret;
1510a5ed9182SOmar Sandoval 
1511a5ed9182SOmar Sandoval 	block_group = caching_ctl->block_group;
1512a5ed9182SOmar Sandoval 	fs_info = block_group->fs_info;
15137939dd9fSJosef Bacik 	root = btrfs_free_space_root(block_group);
1514a5ed9182SOmar Sandoval 
1515b3470b5dSDavid Sterba 	end = block_group->start + block_group->length;
1516a5ed9182SOmar Sandoval 
1517a5ed9182SOmar Sandoval 	while (1) {
1518a5ed9182SOmar Sandoval 		ret = btrfs_next_item(root, path);
1519a5ed9182SOmar Sandoval 		if (ret < 0)
1520a5ed9182SOmar Sandoval 			goto out;
1521a5ed9182SOmar Sandoval 		if (ret)
1522a5ed9182SOmar Sandoval 			break;
1523a5ed9182SOmar Sandoval 
1524a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1525a5ed9182SOmar Sandoval 
1526a5ed9182SOmar Sandoval 		if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1527a5ed9182SOmar Sandoval 			break;
1528a5ed9182SOmar Sandoval 
1529a5ed9182SOmar Sandoval 		ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
1530a5ed9182SOmar Sandoval 		ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1531a5ed9182SOmar Sandoval 
15324457c1c7SNikolay Borisov 		total_found += add_new_free_space(block_group, key.objectid,
1533a5ed9182SOmar Sandoval 						  key.objectid + key.offset);
1534a5ed9182SOmar Sandoval 		if (total_found > CACHING_CTL_WAKE_UP) {
1535a5ed9182SOmar Sandoval 			total_found = 0;
1536a5ed9182SOmar Sandoval 			wake_up(&caching_ctl->wait);
1537a5ed9182SOmar Sandoval 		}
1538a5ed9182SOmar Sandoval 		extent_count++;
1539a5ed9182SOmar Sandoval 	}
1540a5ed9182SOmar Sandoval 
1541a5ed9182SOmar Sandoval 	if (extent_count != expected_extent_count) {
15425d163e0eSJeff Mahoney 		btrfs_err(fs_info,
15435d163e0eSJeff Mahoney 			  "incorrect extent count for %llu; counted %u, expected %u",
1544b3470b5dSDavid Sterba 			  block_group->start, extent_count,
1545a5ed9182SOmar Sandoval 			  expected_extent_count);
1546a5ed9182SOmar Sandoval 		ASSERT(0);
1547a5ed9182SOmar Sandoval 		ret = -EIO;
1548a5ed9182SOmar Sandoval 		goto out;
1549a5ed9182SOmar Sandoval 	}
1550a5ed9182SOmar Sandoval 
1551a5ed9182SOmar Sandoval 	ret = 0;
1552a5ed9182SOmar Sandoval out:
1553a5ed9182SOmar Sandoval 	return ret;
1554a5ed9182SOmar Sandoval }
1555a5ed9182SOmar Sandoval 
1556a5ed9182SOmar Sandoval int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
1557a5ed9182SOmar Sandoval {
155832da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1559a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
1560a5ed9182SOmar Sandoval 	struct btrfs_path *path;
1561a5ed9182SOmar Sandoval 	u32 extent_count, flags;
1562a5ed9182SOmar Sandoval 	int ret;
1563a5ed9182SOmar Sandoval 
1564a5ed9182SOmar Sandoval 	block_group = caching_ctl->block_group;
1565a5ed9182SOmar Sandoval 
1566a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1567a5ed9182SOmar Sandoval 	if (!path)
1568a5ed9182SOmar Sandoval 		return -ENOMEM;
1569a5ed9182SOmar Sandoval 
1570a5ed9182SOmar Sandoval 	/*
1571a5ed9182SOmar Sandoval 	 * Just like caching_thread() doesn't want to deadlock on the extent
1572a5ed9182SOmar Sandoval 	 * tree, we don't want to deadlock on the free space tree.
1573a5ed9182SOmar Sandoval 	 */
1574a5ed9182SOmar Sandoval 	path->skip_locking = 1;
1575a5ed9182SOmar Sandoval 	path->search_commit_root = 1;
15767ce311d5SGu JinXiang 	path->reada = READA_FORWARD;
1577a5ed9182SOmar Sandoval 
15782ccf545eSDavid Sterba 	info = search_free_space_info(NULL, block_group, path, 0);
1579a5ed9182SOmar Sandoval 	if (IS_ERR(info)) {
1580a5ed9182SOmar Sandoval 		ret = PTR_ERR(info);
1581a5ed9182SOmar Sandoval 		goto out;
1582a5ed9182SOmar Sandoval 	}
1583a5ed9182SOmar Sandoval 	extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
1584a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(path->nodes[0], info);
1585a5ed9182SOmar Sandoval 
1586a5ed9182SOmar Sandoval 	/*
1587a5ed9182SOmar Sandoval 	 * We left path pointing to the free space info item, so now
1588a5ed9182SOmar Sandoval 	 * load_free_space_foo can just iterate through the free space tree from
1589a5ed9182SOmar Sandoval 	 * there.
1590a5ed9182SOmar Sandoval 	 */
1591a5ed9182SOmar Sandoval 	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
1592a5ed9182SOmar Sandoval 		ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
1593a5ed9182SOmar Sandoval 	else
1594a5ed9182SOmar Sandoval 		ret = load_free_space_extents(caching_ctl, path, extent_count);
1595a5ed9182SOmar Sandoval 
1596a5ed9182SOmar Sandoval out:
1597a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1598a5ed9182SOmar Sandoval 	return ret;
1599a5ed9182SOmar Sandoval }
1600