xref: /openbmc/linux/fs/btrfs/free-space-tree.c (revision abed4aaa)
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>
8a5ed9182SOmar Sandoval #include "ctree.h"
9a5ed9182SOmar Sandoval #include "disk-io.h"
10a5ed9182SOmar Sandoval #include "locking.h"
11a5ed9182SOmar Sandoval #include "free-space-tree.h"
12a5ed9182SOmar Sandoval #include "transaction.h"
13aac0023cSJosef Bacik #include "block-group.h"
14a5ed9182SOmar Sandoval 
15a5ed9182SOmar Sandoval static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
1632da5386SDavid Sterba 					struct btrfs_block_group *block_group,
17a5ed9182SOmar Sandoval 					struct btrfs_path *path);
18a5ed9182SOmar Sandoval 
197939dd9fSJosef Bacik static struct btrfs_root *btrfs_free_space_root(
207939dd9fSJosef Bacik 				struct btrfs_block_group *block_group)
217939dd9fSJosef Bacik {
22*abed4aaaSJosef Bacik 	struct btrfs_key key = {
23*abed4aaaSJosef Bacik 		.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID,
24*abed4aaaSJosef Bacik 		.type = BTRFS_ROOT_ITEM_KEY,
25*abed4aaaSJosef Bacik 		.offset = 0,
26*abed4aaaSJosef Bacik 	};
27*abed4aaaSJosef Bacik 
28*abed4aaaSJosef Bacik 	return btrfs_global_root(block_group->fs_info, &key);
297939dd9fSJosef Bacik }
307939dd9fSJosef Bacik 
3132da5386SDavid Sterba void set_free_space_tree_thresholds(struct btrfs_block_group *cache)
32a5ed9182SOmar Sandoval {
33a5ed9182SOmar Sandoval 	u32 bitmap_range;
34a5ed9182SOmar Sandoval 	size_t bitmap_size;
35a5ed9182SOmar Sandoval 	u64 num_bitmaps, total_bitmap_size;
36a5ed9182SOmar Sandoval 
37e3e39c72SMarcos Paulo de Souza 	if (WARN_ON(cache->length == 0))
38e3e39c72SMarcos Paulo de Souza 		btrfs_warn(cache->fs_info, "block group %llu length is zero",
39e3e39c72SMarcos Paulo de Souza 			   cache->start);
40e3e39c72SMarcos Paulo de Souza 
41a5ed9182SOmar Sandoval 	/*
42a5ed9182SOmar Sandoval 	 * We convert to bitmaps when the disk space required for using extents
43a5ed9182SOmar Sandoval 	 * exceeds that required for using bitmaps.
44a5ed9182SOmar Sandoval 	 */
45da17066cSJeff Mahoney 	bitmap_range = cache->fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
46b3470b5dSDavid Sterba 	num_bitmaps = div_u64(cache->length + bitmap_range - 1, bitmap_range);
47a5ed9182SOmar Sandoval 	bitmap_size = sizeof(struct btrfs_item) + BTRFS_FREE_SPACE_BITMAP_SIZE;
48a5ed9182SOmar Sandoval 	total_bitmap_size = num_bitmaps * bitmap_size;
49a5ed9182SOmar Sandoval 	cache->bitmap_high_thresh = div_u64(total_bitmap_size,
50a5ed9182SOmar Sandoval 					    sizeof(struct btrfs_item));
51a5ed9182SOmar Sandoval 
52a5ed9182SOmar Sandoval 	/*
53a5ed9182SOmar Sandoval 	 * We allow for a small buffer between the high threshold and low
54a5ed9182SOmar Sandoval 	 * threshold to avoid thrashing back and forth between the two formats.
55a5ed9182SOmar Sandoval 	 */
56a5ed9182SOmar Sandoval 	if (cache->bitmap_high_thresh > 100)
57a5ed9182SOmar Sandoval 		cache->bitmap_low_thresh = cache->bitmap_high_thresh - 100;
58a5ed9182SOmar Sandoval 	else
59a5ed9182SOmar Sandoval 		cache->bitmap_low_thresh = 0;
60a5ed9182SOmar Sandoval }
61a5ed9182SOmar Sandoval 
62a5ed9182SOmar Sandoval static int add_new_free_space_info(struct btrfs_trans_handle *trans,
6332da5386SDavid Sterba 				   struct btrfs_block_group *block_group,
64a5ed9182SOmar Sandoval 				   struct btrfs_path *path)
65a5ed9182SOmar Sandoval {
667939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
67a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
68a5ed9182SOmar Sandoval 	struct btrfs_key key;
69a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
70a5ed9182SOmar Sandoval 	int ret;
71a5ed9182SOmar Sandoval 
72b3470b5dSDavid Sterba 	key.objectid = block_group->start;
73a5ed9182SOmar Sandoval 	key.type = BTRFS_FREE_SPACE_INFO_KEY;
74b3470b5dSDavid Sterba 	key.offset = block_group->length;
75a5ed9182SOmar Sandoval 
76a5ed9182SOmar Sandoval 	ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*info));
77a5ed9182SOmar Sandoval 	if (ret)
78a5ed9182SOmar Sandoval 		goto out;
79a5ed9182SOmar Sandoval 
80a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
81a5ed9182SOmar Sandoval 	info = btrfs_item_ptr(leaf, path->slots[0],
82a5ed9182SOmar Sandoval 			      struct btrfs_free_space_info);
83a5ed9182SOmar Sandoval 	btrfs_set_free_space_extent_count(leaf, info, 0);
84a5ed9182SOmar Sandoval 	btrfs_set_free_space_flags(leaf, info, 0);
85a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(leaf);
86a5ed9182SOmar Sandoval 
87a5ed9182SOmar Sandoval 	ret = 0;
88a5ed9182SOmar Sandoval out:
89a5ed9182SOmar Sandoval 	btrfs_release_path(path);
90a5ed9182SOmar Sandoval 	return ret;
91a5ed9182SOmar Sandoval }
92a5ed9182SOmar Sandoval 
93ce9f967fSJohannes Thumshirn EXPORT_FOR_TESTS
94ce9f967fSJohannes Thumshirn struct btrfs_free_space_info *search_free_space_info(
952ccf545eSDavid Sterba 		struct btrfs_trans_handle *trans,
9632da5386SDavid Sterba 		struct btrfs_block_group *block_group,
97a5ed9182SOmar Sandoval 		struct btrfs_path *path, int cow)
98a5ed9182SOmar Sandoval {
992ccf545eSDavid Sterba 	struct btrfs_fs_info *fs_info = block_group->fs_info;
1007939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
101a5ed9182SOmar Sandoval 	struct btrfs_key key;
102a5ed9182SOmar Sandoval 	int ret;
103a5ed9182SOmar Sandoval 
104b3470b5dSDavid Sterba 	key.objectid = block_group->start;
105a5ed9182SOmar Sandoval 	key.type = BTRFS_FREE_SPACE_INFO_KEY;
106b3470b5dSDavid Sterba 	key.offset = block_group->length;
107a5ed9182SOmar Sandoval 
108a5ed9182SOmar Sandoval 	ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
109a5ed9182SOmar Sandoval 	if (ret < 0)
110a5ed9182SOmar Sandoval 		return ERR_PTR(ret);
111a5ed9182SOmar Sandoval 	if (ret != 0) {
112ab8d0fc4SJeff Mahoney 		btrfs_warn(fs_info, "missing free space info for %llu",
113b3470b5dSDavid Sterba 			   block_group->start);
114a5ed9182SOmar Sandoval 		ASSERT(0);
115a5ed9182SOmar Sandoval 		return ERR_PTR(-ENOENT);
116a5ed9182SOmar Sandoval 	}
117a5ed9182SOmar Sandoval 
118a5ed9182SOmar Sandoval 	return btrfs_item_ptr(path->nodes[0], path->slots[0],
119a5ed9182SOmar Sandoval 			      struct btrfs_free_space_info);
120a5ed9182SOmar Sandoval }
121a5ed9182SOmar Sandoval 
122a5ed9182SOmar Sandoval /*
123a5ed9182SOmar Sandoval  * btrfs_search_slot() but we're looking for the greatest key less than the
124a5ed9182SOmar Sandoval  * passed key.
125a5ed9182SOmar Sandoval  */
126a5ed9182SOmar Sandoval static int btrfs_search_prev_slot(struct btrfs_trans_handle *trans,
127a5ed9182SOmar Sandoval 				  struct btrfs_root *root,
128a5ed9182SOmar Sandoval 				  struct btrfs_key *key, struct btrfs_path *p,
129a5ed9182SOmar Sandoval 				  int ins_len, int cow)
130a5ed9182SOmar Sandoval {
131a5ed9182SOmar Sandoval 	int ret;
132a5ed9182SOmar Sandoval 
133a5ed9182SOmar Sandoval 	ret = btrfs_search_slot(trans, root, key, p, ins_len, cow);
134a5ed9182SOmar Sandoval 	if (ret < 0)
135a5ed9182SOmar Sandoval 		return ret;
136a5ed9182SOmar Sandoval 
137a5ed9182SOmar Sandoval 	if (ret == 0) {
138a5ed9182SOmar Sandoval 		ASSERT(0);
139a5ed9182SOmar Sandoval 		return -EIO;
140a5ed9182SOmar Sandoval 	}
141a5ed9182SOmar Sandoval 
142a5ed9182SOmar Sandoval 	if (p->slots[0] == 0) {
143a5ed9182SOmar Sandoval 		ASSERT(0);
144a5ed9182SOmar Sandoval 		return -EIO;
145a5ed9182SOmar Sandoval 	}
146a5ed9182SOmar Sandoval 	p->slots[0]--;
147a5ed9182SOmar Sandoval 
148a5ed9182SOmar Sandoval 	return 0;
149a5ed9182SOmar Sandoval }
150a5ed9182SOmar Sandoval 
151098e6308SDavid Sterba static inline u32 free_space_bitmap_size(const struct btrfs_fs_info *fs_info,
152098e6308SDavid Sterba 					 u64 size)
153a5ed9182SOmar Sandoval {
154098e6308SDavid Sterba 	return DIV_ROUND_UP(size >> fs_info->sectorsize_bits, BITS_PER_BYTE);
155a5ed9182SOmar Sandoval }
156a5ed9182SOmar Sandoval 
157a565971fSHoward McLauchlan static unsigned long *alloc_bitmap(u32 bitmap_size)
158a5ed9182SOmar Sandoval {
159a565971fSHoward McLauchlan 	unsigned long *ret;
16025ff17e8SOmar Sandoval 	unsigned int nofs_flag;
161a565971fSHoward McLauchlan 	u32 bitmap_rounded_size = round_up(bitmap_size, sizeof(unsigned long));
16279b134a2SDavid Sterba 
16379b134a2SDavid Sterba 	/*
16425ff17e8SOmar Sandoval 	 * GFP_NOFS doesn't work with kvmalloc(), but we really can't recurse
16525ff17e8SOmar Sandoval 	 * into the filesystem as the free space bitmap can be modified in the
16625ff17e8SOmar Sandoval 	 * critical section of a transaction commit.
16725ff17e8SOmar Sandoval 	 *
16825ff17e8SOmar Sandoval 	 * TODO: push the memalloc_nofs_{save,restore}() to the caller where we
16925ff17e8SOmar Sandoval 	 * know that recursion is unsafe.
17079b134a2SDavid Sterba 	 */
17125ff17e8SOmar Sandoval 	nofs_flag = memalloc_nofs_save();
172a565971fSHoward McLauchlan 	ret = kvzalloc(bitmap_rounded_size, GFP_KERNEL);
17325ff17e8SOmar Sandoval 	memalloc_nofs_restore(nofs_flag);
17425ff17e8SOmar Sandoval 	return ret;
175a5ed9182SOmar Sandoval }
176a5ed9182SOmar Sandoval 
177a565971fSHoward McLauchlan static void le_bitmap_set(unsigned long *map, unsigned int start, int len)
1786faa8f47SHoward McLauchlan {
179a565971fSHoward McLauchlan 	u8 *p = ((u8 *)map) + BIT_BYTE(start);
1806faa8f47SHoward McLauchlan 	const unsigned int size = start + len;
1816faa8f47SHoward McLauchlan 	int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
1826faa8f47SHoward McLauchlan 	u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
1836faa8f47SHoward McLauchlan 
1846faa8f47SHoward McLauchlan 	while (len - bits_to_set >= 0) {
1856faa8f47SHoward McLauchlan 		*p |= mask_to_set;
1866faa8f47SHoward McLauchlan 		len -= bits_to_set;
1876faa8f47SHoward McLauchlan 		bits_to_set = BITS_PER_BYTE;
1886faa8f47SHoward McLauchlan 		mask_to_set = ~0;
1896faa8f47SHoward McLauchlan 		p++;
1906faa8f47SHoward McLauchlan 	}
1916faa8f47SHoward McLauchlan 	if (len) {
1926faa8f47SHoward McLauchlan 		mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
1936faa8f47SHoward McLauchlan 		*p |= mask_to_set;
1946faa8f47SHoward McLauchlan 	}
1956faa8f47SHoward McLauchlan }
1966faa8f47SHoward McLauchlan 
197ce9f967fSJohannes Thumshirn EXPORT_FOR_TESTS
198a5ed9182SOmar Sandoval int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
19932da5386SDavid Sterba 				  struct btrfs_block_group *block_group,
200a5ed9182SOmar Sandoval 				  struct btrfs_path *path)
201a5ed9182SOmar Sandoval {
202719fb4deSNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
2037939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
204a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
205a5ed9182SOmar Sandoval 	struct btrfs_key key, found_key;
206a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
207a565971fSHoward McLauchlan 	unsigned long *bitmap;
208a565971fSHoward McLauchlan 	char *bitmap_cursor;
209a5ed9182SOmar Sandoval 	u64 start, end;
210a5ed9182SOmar Sandoval 	u64 bitmap_range, i;
211a5ed9182SOmar Sandoval 	u32 bitmap_size, flags, expected_extent_count;
212a5ed9182SOmar Sandoval 	u32 extent_count = 0;
213a5ed9182SOmar Sandoval 	int done = 0, nr;
214a5ed9182SOmar Sandoval 	int ret;
215a5ed9182SOmar Sandoval 
216098e6308SDavid Sterba 	bitmap_size = free_space_bitmap_size(fs_info, block_group->length);
217a5ed9182SOmar Sandoval 	bitmap = alloc_bitmap(bitmap_size);
218a5ed9182SOmar Sandoval 	if (!bitmap) {
219a5ed9182SOmar Sandoval 		ret = -ENOMEM;
220a5ed9182SOmar Sandoval 		goto out;
221a5ed9182SOmar Sandoval 	}
222a5ed9182SOmar Sandoval 
223b3470b5dSDavid Sterba 	start = block_group->start;
224b3470b5dSDavid Sterba 	end = block_group->start + block_group->length;
225a5ed9182SOmar Sandoval 
226a5ed9182SOmar Sandoval 	key.objectid = end - 1;
227a5ed9182SOmar Sandoval 	key.type = (u8)-1;
228a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
229a5ed9182SOmar Sandoval 
230a5ed9182SOmar Sandoval 	while (!done) {
231a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
232a5ed9182SOmar Sandoval 		if (ret)
233a5ed9182SOmar Sandoval 			goto out;
234a5ed9182SOmar Sandoval 
235a5ed9182SOmar Sandoval 		leaf = path->nodes[0];
236a5ed9182SOmar Sandoval 		nr = 0;
237a5ed9182SOmar Sandoval 		path->slots[0]++;
238a5ed9182SOmar Sandoval 		while (path->slots[0] > 0) {
239a5ed9182SOmar Sandoval 			btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
240a5ed9182SOmar Sandoval 
241a5ed9182SOmar Sandoval 			if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
242b3470b5dSDavid Sterba 				ASSERT(found_key.objectid == block_group->start);
243b3470b5dSDavid Sterba 				ASSERT(found_key.offset == block_group->length);
244a5ed9182SOmar Sandoval 				done = 1;
245a5ed9182SOmar Sandoval 				break;
246a5ed9182SOmar Sandoval 			} else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) {
247a5ed9182SOmar Sandoval 				u64 first, last;
248a5ed9182SOmar Sandoval 
249a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid >= start);
250a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid < end);
251a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid + found_key.offset <= end);
252a5ed9182SOmar Sandoval 
253a5ed9182SOmar Sandoval 				first = div_u64(found_key.objectid - start,
2540b246afaSJeff Mahoney 						fs_info->sectorsize);
255a5ed9182SOmar Sandoval 				last = div_u64(found_key.objectid + found_key.offset - start,
2560b246afaSJeff Mahoney 					       fs_info->sectorsize);
2572fe1d551SOmar Sandoval 				le_bitmap_set(bitmap, first, last - first);
258a5ed9182SOmar Sandoval 
259a5ed9182SOmar Sandoval 				extent_count++;
260a5ed9182SOmar Sandoval 				nr++;
261a5ed9182SOmar Sandoval 				path->slots[0]--;
262a5ed9182SOmar Sandoval 			} else {
263a5ed9182SOmar Sandoval 				ASSERT(0);
264a5ed9182SOmar Sandoval 			}
265a5ed9182SOmar Sandoval 		}
266a5ed9182SOmar Sandoval 
267a5ed9182SOmar Sandoval 		ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
268a5ed9182SOmar Sandoval 		if (ret)
269a5ed9182SOmar Sandoval 			goto out;
270a5ed9182SOmar Sandoval 		btrfs_release_path(path);
271a5ed9182SOmar Sandoval 	}
272a5ed9182SOmar Sandoval 
2732ccf545eSDavid Sterba 	info = search_free_space_info(trans, block_group, path, 1);
274a5ed9182SOmar Sandoval 	if (IS_ERR(info)) {
275a5ed9182SOmar Sandoval 		ret = PTR_ERR(info);
276a5ed9182SOmar Sandoval 		goto out;
277a5ed9182SOmar Sandoval 	}
278a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
279a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(leaf, info);
280a5ed9182SOmar Sandoval 	flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
281a5ed9182SOmar Sandoval 	btrfs_set_free_space_flags(leaf, info, flags);
282a5ed9182SOmar Sandoval 	expected_extent_count = btrfs_free_space_extent_count(leaf, info);
283a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(leaf);
284a5ed9182SOmar Sandoval 	btrfs_release_path(path);
285a5ed9182SOmar Sandoval 
286a5ed9182SOmar Sandoval 	if (extent_count != expected_extent_count) {
2875d163e0eSJeff Mahoney 		btrfs_err(fs_info,
2885d163e0eSJeff Mahoney 			  "incorrect extent count for %llu; counted %u, expected %u",
289b3470b5dSDavid Sterba 			  block_group->start, extent_count,
290a5ed9182SOmar Sandoval 			  expected_extent_count);
291a5ed9182SOmar Sandoval 		ASSERT(0);
292a5ed9182SOmar Sandoval 		ret = -EIO;
293a5ed9182SOmar Sandoval 		goto out;
294a5ed9182SOmar Sandoval 	}
295a5ed9182SOmar Sandoval 
296a565971fSHoward McLauchlan 	bitmap_cursor = (char *)bitmap;
2970b246afaSJeff Mahoney 	bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
298a5ed9182SOmar Sandoval 	i = start;
299a5ed9182SOmar Sandoval 	while (i < end) {
300a5ed9182SOmar Sandoval 		unsigned long ptr;
301a5ed9182SOmar Sandoval 		u64 extent_size;
302a5ed9182SOmar Sandoval 		u32 data_size;
303a5ed9182SOmar Sandoval 
304a5ed9182SOmar Sandoval 		extent_size = min(end - i, bitmap_range);
305098e6308SDavid Sterba 		data_size = free_space_bitmap_size(fs_info, extent_size);
306a5ed9182SOmar Sandoval 
307a5ed9182SOmar Sandoval 		key.objectid = i;
308a5ed9182SOmar Sandoval 		key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
309a5ed9182SOmar Sandoval 		key.offset = extent_size;
310a5ed9182SOmar Sandoval 
311a5ed9182SOmar Sandoval 		ret = btrfs_insert_empty_item(trans, root, path, &key,
312a5ed9182SOmar Sandoval 					      data_size);
313a5ed9182SOmar Sandoval 		if (ret)
314a5ed9182SOmar Sandoval 			goto out;
315a5ed9182SOmar Sandoval 
316a5ed9182SOmar Sandoval 		leaf = path->nodes[0];
317a5ed9182SOmar Sandoval 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
318a5ed9182SOmar Sandoval 		write_extent_buffer(leaf, bitmap_cursor, ptr,
319a5ed9182SOmar Sandoval 				    data_size);
320a5ed9182SOmar Sandoval 		btrfs_mark_buffer_dirty(leaf);
321a5ed9182SOmar Sandoval 		btrfs_release_path(path);
322a5ed9182SOmar Sandoval 
323a5ed9182SOmar Sandoval 		i += extent_size;
324a5ed9182SOmar Sandoval 		bitmap_cursor += data_size;
325a5ed9182SOmar Sandoval 	}
326a5ed9182SOmar Sandoval 
327a5ed9182SOmar Sandoval 	ret = 0;
328a5ed9182SOmar Sandoval out:
32979b134a2SDavid Sterba 	kvfree(bitmap);
330a5ed9182SOmar Sandoval 	if (ret)
33166642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
332a5ed9182SOmar Sandoval 	return ret;
333a5ed9182SOmar Sandoval }
334a5ed9182SOmar Sandoval 
335ce9f967fSJohannes Thumshirn EXPORT_FOR_TESTS
336a5ed9182SOmar Sandoval int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
33732da5386SDavid Sterba 				  struct btrfs_block_group *block_group,
338a5ed9182SOmar Sandoval 				  struct btrfs_path *path)
339a5ed9182SOmar Sandoval {
3405296c2bfSNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
3417939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
342a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
343a5ed9182SOmar Sandoval 	struct btrfs_key key, found_key;
344a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
345a565971fSHoward McLauchlan 	unsigned long *bitmap;
346a5ed9182SOmar Sandoval 	u64 start, end;
347a5ed9182SOmar Sandoval 	u32 bitmap_size, flags, expected_extent_count;
348a565971fSHoward McLauchlan 	unsigned long nrbits, start_bit, end_bit;
349a5ed9182SOmar Sandoval 	u32 extent_count = 0;
350a5ed9182SOmar Sandoval 	int done = 0, nr;
351a5ed9182SOmar Sandoval 	int ret;
352a5ed9182SOmar Sandoval 
353098e6308SDavid Sterba 	bitmap_size = free_space_bitmap_size(fs_info, block_group->length);
354a5ed9182SOmar Sandoval 	bitmap = alloc_bitmap(bitmap_size);
355a5ed9182SOmar Sandoval 	if (!bitmap) {
356a5ed9182SOmar Sandoval 		ret = -ENOMEM;
357a5ed9182SOmar Sandoval 		goto out;
358a5ed9182SOmar Sandoval 	}
359a5ed9182SOmar Sandoval 
360b3470b5dSDavid Sterba 	start = block_group->start;
361b3470b5dSDavid Sterba 	end = block_group->start + block_group->length;
362a5ed9182SOmar Sandoval 
363a5ed9182SOmar Sandoval 	key.objectid = end - 1;
364a5ed9182SOmar Sandoval 	key.type = (u8)-1;
365a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
366a5ed9182SOmar Sandoval 
367a5ed9182SOmar Sandoval 	while (!done) {
368a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
369a5ed9182SOmar Sandoval 		if (ret)
370a5ed9182SOmar Sandoval 			goto out;
371a5ed9182SOmar Sandoval 
372a5ed9182SOmar Sandoval 		leaf = path->nodes[0];
373a5ed9182SOmar Sandoval 		nr = 0;
374a5ed9182SOmar Sandoval 		path->slots[0]++;
375a5ed9182SOmar Sandoval 		while (path->slots[0] > 0) {
376a5ed9182SOmar Sandoval 			btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
377a5ed9182SOmar Sandoval 
378a5ed9182SOmar Sandoval 			if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
379b3470b5dSDavid Sterba 				ASSERT(found_key.objectid == block_group->start);
380b3470b5dSDavid Sterba 				ASSERT(found_key.offset == block_group->length);
381a5ed9182SOmar Sandoval 				done = 1;
382a5ed9182SOmar Sandoval 				break;
383a5ed9182SOmar Sandoval 			} else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
384a5ed9182SOmar Sandoval 				unsigned long ptr;
385a565971fSHoward McLauchlan 				char *bitmap_cursor;
386a5ed9182SOmar Sandoval 				u32 bitmap_pos, data_size;
387a5ed9182SOmar Sandoval 
388a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid >= start);
389a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid < end);
390a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid + found_key.offset <= end);
391a5ed9182SOmar Sandoval 
392a5ed9182SOmar Sandoval 				bitmap_pos = div_u64(found_key.objectid - start,
3930b246afaSJeff Mahoney 						     fs_info->sectorsize *
394a5ed9182SOmar Sandoval 						     BITS_PER_BYTE);
395a565971fSHoward McLauchlan 				bitmap_cursor = ((char *)bitmap) + bitmap_pos;
396098e6308SDavid Sterba 				data_size = free_space_bitmap_size(fs_info,
397098e6308SDavid Sterba 								found_key.offset);
398a5ed9182SOmar Sandoval 
399a5ed9182SOmar Sandoval 				ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
400a5ed9182SOmar Sandoval 				read_extent_buffer(leaf, bitmap_cursor, ptr,
401a5ed9182SOmar Sandoval 						   data_size);
402a5ed9182SOmar Sandoval 
403a5ed9182SOmar Sandoval 				nr++;
404a5ed9182SOmar Sandoval 				path->slots[0]--;
405a5ed9182SOmar Sandoval 			} else {
406a5ed9182SOmar Sandoval 				ASSERT(0);
407a5ed9182SOmar Sandoval 			}
408a5ed9182SOmar Sandoval 		}
409a5ed9182SOmar Sandoval 
410a5ed9182SOmar Sandoval 		ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
411a5ed9182SOmar Sandoval 		if (ret)
412a5ed9182SOmar Sandoval 			goto out;
413a5ed9182SOmar Sandoval 		btrfs_release_path(path);
414a5ed9182SOmar Sandoval 	}
415a5ed9182SOmar Sandoval 
4162ccf545eSDavid Sterba 	info = search_free_space_info(trans, block_group, path, 1);
417a5ed9182SOmar Sandoval 	if (IS_ERR(info)) {
418a5ed9182SOmar Sandoval 		ret = PTR_ERR(info);
419a5ed9182SOmar Sandoval 		goto out;
420a5ed9182SOmar Sandoval 	}
421a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
422a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(leaf, info);
423a5ed9182SOmar Sandoval 	flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
424a5ed9182SOmar Sandoval 	btrfs_set_free_space_flags(leaf, info, flags);
425a5ed9182SOmar Sandoval 	expected_extent_count = btrfs_free_space_extent_count(leaf, info);
426a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(leaf);
427a5ed9182SOmar Sandoval 	btrfs_release_path(path);
428a5ed9182SOmar Sandoval 
429ab108d99SDavid Sterba 	nrbits = block_group->length >> block_group->fs_info->sectorsize_bits;
430a565971fSHoward McLauchlan 	start_bit = find_next_bit_le(bitmap, nrbits, 0);
431a565971fSHoward McLauchlan 
432a565971fSHoward McLauchlan 	while (start_bit < nrbits) {
433a565971fSHoward McLauchlan 		end_bit = find_next_zero_bit_le(bitmap, nrbits, start_bit);
434a565971fSHoward McLauchlan 		ASSERT(start_bit < end_bit);
435a565971fSHoward McLauchlan 
436a565971fSHoward McLauchlan 		key.objectid = start + start_bit * block_group->fs_info->sectorsize;
437a5ed9182SOmar Sandoval 		key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
438a565971fSHoward McLauchlan 		key.offset = (end_bit - start_bit) * block_group->fs_info->sectorsize;
439a5ed9182SOmar Sandoval 
440a5ed9182SOmar Sandoval 		ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
441a5ed9182SOmar Sandoval 		if (ret)
442a5ed9182SOmar Sandoval 			goto out;
443a5ed9182SOmar Sandoval 		btrfs_release_path(path);
444a5ed9182SOmar Sandoval 
445a5ed9182SOmar Sandoval 		extent_count++;
446a5ed9182SOmar Sandoval 
447a565971fSHoward McLauchlan 		start_bit = find_next_bit_le(bitmap, nrbits, end_bit);
448a5ed9182SOmar Sandoval 	}
449a5ed9182SOmar Sandoval 
450a5ed9182SOmar Sandoval 	if (extent_count != expected_extent_count) {
4515d163e0eSJeff Mahoney 		btrfs_err(fs_info,
4525d163e0eSJeff Mahoney 			  "incorrect extent count for %llu; counted %u, expected %u",
453b3470b5dSDavid Sterba 			  block_group->start, extent_count,
454a5ed9182SOmar Sandoval 			  expected_extent_count);
455a5ed9182SOmar Sandoval 		ASSERT(0);
456a5ed9182SOmar Sandoval 		ret = -EIO;
457a5ed9182SOmar Sandoval 		goto out;
458a5ed9182SOmar Sandoval 	}
459a5ed9182SOmar Sandoval 
460a5ed9182SOmar Sandoval 	ret = 0;
461a5ed9182SOmar Sandoval out:
46279b134a2SDavid Sterba 	kvfree(bitmap);
463a5ed9182SOmar Sandoval 	if (ret)
46466642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
465a5ed9182SOmar Sandoval 	return ret;
466a5ed9182SOmar Sandoval }
467a5ed9182SOmar Sandoval 
468a5ed9182SOmar Sandoval static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
46932da5386SDavid Sterba 					  struct btrfs_block_group *block_group,
470a5ed9182SOmar Sandoval 					  struct btrfs_path *path,
471a5ed9182SOmar Sandoval 					  int new_extents)
472a5ed9182SOmar Sandoval {
473a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
474a5ed9182SOmar Sandoval 	u32 flags;
475a5ed9182SOmar Sandoval 	u32 extent_count;
476a5ed9182SOmar Sandoval 	int ret = 0;
477a5ed9182SOmar Sandoval 
478a5ed9182SOmar Sandoval 	if (new_extents == 0)
479a5ed9182SOmar Sandoval 		return 0;
480a5ed9182SOmar Sandoval 
4812ccf545eSDavid Sterba 	info = search_free_space_info(trans, block_group, path, 1);
482a5ed9182SOmar Sandoval 	if (IS_ERR(info)) {
483a5ed9182SOmar Sandoval 		ret = PTR_ERR(info);
484a5ed9182SOmar Sandoval 		goto out;
485a5ed9182SOmar Sandoval 	}
486a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(path->nodes[0], info);
487a5ed9182SOmar Sandoval 	extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
488a5ed9182SOmar Sandoval 
489a5ed9182SOmar Sandoval 	extent_count += new_extents;
490a5ed9182SOmar Sandoval 	btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
491a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(path->nodes[0]);
492a5ed9182SOmar Sandoval 	btrfs_release_path(path);
493a5ed9182SOmar Sandoval 
494a5ed9182SOmar Sandoval 	if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
495a5ed9182SOmar Sandoval 	    extent_count > block_group->bitmap_high_thresh) {
496719fb4deSNikolay Borisov 		ret = convert_free_space_to_bitmaps(trans, block_group, path);
497a5ed9182SOmar Sandoval 	} else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
498a5ed9182SOmar Sandoval 		   extent_count < block_group->bitmap_low_thresh) {
4995296c2bfSNikolay Borisov 		ret = convert_free_space_to_extents(trans, block_group, path);
500a5ed9182SOmar Sandoval 	}
501a5ed9182SOmar Sandoval 
502a5ed9182SOmar Sandoval out:
503a5ed9182SOmar Sandoval 	return ret;
504a5ed9182SOmar Sandoval }
505a5ed9182SOmar Sandoval 
506ce9f967fSJohannes Thumshirn EXPORT_FOR_TESTS
50732da5386SDavid Sterba int free_space_test_bit(struct btrfs_block_group *block_group,
508a5ed9182SOmar Sandoval 			struct btrfs_path *path, u64 offset)
509a5ed9182SOmar Sandoval {
510a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
511a5ed9182SOmar Sandoval 	struct btrfs_key key;
512a5ed9182SOmar Sandoval 	u64 found_start, found_end;
513a5ed9182SOmar Sandoval 	unsigned long ptr, i;
514a5ed9182SOmar Sandoval 
515a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
516a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
517a5ed9182SOmar Sandoval 	ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
518a5ed9182SOmar Sandoval 
519a5ed9182SOmar Sandoval 	found_start = key.objectid;
520a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
521a5ed9182SOmar Sandoval 	ASSERT(offset >= found_start && offset < found_end);
522a5ed9182SOmar Sandoval 
523a5ed9182SOmar Sandoval 	ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
524da17066cSJeff Mahoney 	i = div_u64(offset - found_start,
525da17066cSJeff Mahoney 		    block_group->fs_info->sectorsize);
526a5ed9182SOmar Sandoval 	return !!extent_buffer_test_bit(leaf, ptr, i);
527a5ed9182SOmar Sandoval }
528a5ed9182SOmar Sandoval 
52932da5386SDavid Sterba static void free_space_set_bits(struct btrfs_block_group *block_group,
530a5ed9182SOmar Sandoval 				struct btrfs_path *path, u64 *start, u64 *size,
531a5ed9182SOmar Sandoval 				int bit)
532a5ed9182SOmar Sandoval {
5330b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = block_group->fs_info;
534a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
535a5ed9182SOmar Sandoval 	struct btrfs_key key;
536a5ed9182SOmar Sandoval 	u64 end = *start + *size;
537a5ed9182SOmar Sandoval 	u64 found_start, found_end;
538a5ed9182SOmar Sandoval 	unsigned long ptr, first, last;
539a5ed9182SOmar Sandoval 
540a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
541a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
542a5ed9182SOmar Sandoval 	ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
543a5ed9182SOmar Sandoval 
544a5ed9182SOmar Sandoval 	found_start = key.objectid;
545a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
546a5ed9182SOmar Sandoval 	ASSERT(*start >= found_start && *start < found_end);
547a5ed9182SOmar Sandoval 	ASSERT(end > found_start);
548a5ed9182SOmar Sandoval 
549a5ed9182SOmar Sandoval 	if (end > found_end)
550a5ed9182SOmar Sandoval 		end = found_end;
551a5ed9182SOmar Sandoval 
552a5ed9182SOmar Sandoval 	ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
553ab108d99SDavid Sterba 	first = (*start - found_start) >> fs_info->sectorsize_bits;
554ab108d99SDavid Sterba 	last = (end - found_start) >> fs_info->sectorsize_bits;
555a5ed9182SOmar Sandoval 	if (bit)
556a5ed9182SOmar Sandoval 		extent_buffer_bitmap_set(leaf, ptr, first, last - first);
557a5ed9182SOmar Sandoval 	else
558a5ed9182SOmar Sandoval 		extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
559a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(leaf);
560a5ed9182SOmar Sandoval 
561a5ed9182SOmar Sandoval 	*size -= end - *start;
562a5ed9182SOmar Sandoval 	*start = end;
563a5ed9182SOmar Sandoval }
564a5ed9182SOmar Sandoval 
565a5ed9182SOmar Sandoval /*
566a5ed9182SOmar Sandoval  * We can't use btrfs_next_item() in modify_free_space_bitmap() because
567a5ed9182SOmar Sandoval  * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
568a5ed9182SOmar Sandoval  * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
569a5ed9182SOmar Sandoval  * looking for.
570a5ed9182SOmar Sandoval  */
571a5ed9182SOmar Sandoval static int free_space_next_bitmap(struct btrfs_trans_handle *trans,
572a5ed9182SOmar Sandoval 				  struct btrfs_root *root, struct btrfs_path *p)
573a5ed9182SOmar Sandoval {
574a5ed9182SOmar Sandoval 	struct btrfs_key key;
575a5ed9182SOmar Sandoval 
576a5ed9182SOmar Sandoval 	if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
577a5ed9182SOmar Sandoval 		p->slots[0]++;
578a5ed9182SOmar Sandoval 		return 0;
579a5ed9182SOmar Sandoval 	}
580a5ed9182SOmar Sandoval 
581a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
582a5ed9182SOmar Sandoval 	btrfs_release_path(p);
583a5ed9182SOmar Sandoval 
584a5ed9182SOmar Sandoval 	key.objectid += key.offset;
585a5ed9182SOmar Sandoval 	key.type = (u8)-1;
586a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
587a5ed9182SOmar Sandoval 
588a5ed9182SOmar Sandoval 	return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
589a5ed9182SOmar Sandoval }
590a5ed9182SOmar Sandoval 
591a5ed9182SOmar Sandoval /*
592a5ed9182SOmar Sandoval  * If remove is 1, then we are removing free space, thus clearing bits in the
593a5ed9182SOmar Sandoval  * bitmap. If remove is 0, then we are adding free space, thus setting bits in
594a5ed9182SOmar Sandoval  * the bitmap.
595a5ed9182SOmar Sandoval  */
596a5ed9182SOmar Sandoval static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
59732da5386SDavid Sterba 				    struct btrfs_block_group *block_group,
598a5ed9182SOmar Sandoval 				    struct btrfs_path *path,
599a5ed9182SOmar Sandoval 				    u64 start, u64 size, int remove)
600a5ed9182SOmar Sandoval {
6017939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
602a5ed9182SOmar Sandoval 	struct btrfs_key key;
603a5ed9182SOmar Sandoval 	u64 end = start + size;
604a5ed9182SOmar Sandoval 	u64 cur_start, cur_size;
605a5ed9182SOmar Sandoval 	int prev_bit, next_bit;
606a5ed9182SOmar Sandoval 	int new_extents;
607a5ed9182SOmar Sandoval 	int ret;
608a5ed9182SOmar Sandoval 
609a5ed9182SOmar Sandoval 	/*
610a5ed9182SOmar Sandoval 	 * Read the bit for the block immediately before the extent of space if
611a5ed9182SOmar Sandoval 	 * that block is within the block group.
612a5ed9182SOmar Sandoval 	 */
613b3470b5dSDavid Sterba 	if (start > block_group->start) {
614da17066cSJeff Mahoney 		u64 prev_block = start - block_group->fs_info->sectorsize;
615a5ed9182SOmar Sandoval 
616a5ed9182SOmar Sandoval 		key.objectid = prev_block;
617a5ed9182SOmar Sandoval 		key.type = (u8)-1;
618a5ed9182SOmar Sandoval 		key.offset = (u64)-1;
619a5ed9182SOmar Sandoval 
620a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
621a5ed9182SOmar Sandoval 		if (ret)
622a5ed9182SOmar Sandoval 			goto out;
623a5ed9182SOmar Sandoval 
624a5ed9182SOmar Sandoval 		prev_bit = free_space_test_bit(block_group, path, prev_block);
625a5ed9182SOmar Sandoval 
626a5ed9182SOmar Sandoval 		/* The previous block may have been in the previous bitmap. */
627a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
628a5ed9182SOmar Sandoval 		if (start >= key.objectid + key.offset) {
629a5ed9182SOmar Sandoval 			ret = free_space_next_bitmap(trans, root, path);
630a5ed9182SOmar Sandoval 			if (ret)
631a5ed9182SOmar Sandoval 				goto out;
632a5ed9182SOmar Sandoval 		}
633a5ed9182SOmar Sandoval 	} else {
634a5ed9182SOmar Sandoval 		key.objectid = start;
635a5ed9182SOmar Sandoval 		key.type = (u8)-1;
636a5ed9182SOmar Sandoval 		key.offset = (u64)-1;
637a5ed9182SOmar Sandoval 
638a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
639a5ed9182SOmar Sandoval 		if (ret)
640a5ed9182SOmar Sandoval 			goto out;
641a5ed9182SOmar Sandoval 
642a5ed9182SOmar Sandoval 		prev_bit = -1;
643a5ed9182SOmar Sandoval 	}
644a5ed9182SOmar Sandoval 
645a5ed9182SOmar Sandoval 	/*
646a5ed9182SOmar Sandoval 	 * Iterate over all of the bitmaps overlapped by the extent of space,
647a5ed9182SOmar Sandoval 	 * clearing/setting bits as required.
648a5ed9182SOmar Sandoval 	 */
649a5ed9182SOmar Sandoval 	cur_start = start;
650a5ed9182SOmar Sandoval 	cur_size = size;
651a5ed9182SOmar Sandoval 	while (1) {
652a5ed9182SOmar Sandoval 		free_space_set_bits(block_group, path, &cur_start, &cur_size,
653a5ed9182SOmar Sandoval 				    !remove);
654a5ed9182SOmar Sandoval 		if (cur_size == 0)
655a5ed9182SOmar Sandoval 			break;
656a5ed9182SOmar Sandoval 		ret = free_space_next_bitmap(trans, root, path);
657a5ed9182SOmar Sandoval 		if (ret)
658a5ed9182SOmar Sandoval 			goto out;
659a5ed9182SOmar Sandoval 	}
660a5ed9182SOmar Sandoval 
661a5ed9182SOmar Sandoval 	/*
662a5ed9182SOmar Sandoval 	 * Read the bit for the block immediately after the extent of space if
663a5ed9182SOmar Sandoval 	 * that block is within the block group.
664a5ed9182SOmar Sandoval 	 */
665b3470b5dSDavid Sterba 	if (end < block_group->start + block_group->length) {
666a5ed9182SOmar Sandoval 		/* The next block may be in the next bitmap. */
667a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
668a5ed9182SOmar Sandoval 		if (end >= key.objectid + key.offset) {
669a5ed9182SOmar Sandoval 			ret = free_space_next_bitmap(trans, root, path);
670a5ed9182SOmar Sandoval 			if (ret)
671a5ed9182SOmar Sandoval 				goto out;
672a5ed9182SOmar Sandoval 		}
673a5ed9182SOmar Sandoval 
674a5ed9182SOmar Sandoval 		next_bit = free_space_test_bit(block_group, path, end);
675a5ed9182SOmar Sandoval 	} else {
676a5ed9182SOmar Sandoval 		next_bit = -1;
677a5ed9182SOmar Sandoval 	}
678a5ed9182SOmar Sandoval 
679a5ed9182SOmar Sandoval 	if (remove) {
680a5ed9182SOmar Sandoval 		new_extents = -1;
681a5ed9182SOmar Sandoval 		if (prev_bit == 1) {
682a5ed9182SOmar Sandoval 			/* Leftover on the left. */
683a5ed9182SOmar Sandoval 			new_extents++;
684a5ed9182SOmar Sandoval 		}
685a5ed9182SOmar Sandoval 		if (next_bit == 1) {
686a5ed9182SOmar Sandoval 			/* Leftover on the right. */
687a5ed9182SOmar Sandoval 			new_extents++;
688a5ed9182SOmar Sandoval 		}
689a5ed9182SOmar Sandoval 	} else {
690a5ed9182SOmar Sandoval 		new_extents = 1;
691a5ed9182SOmar Sandoval 		if (prev_bit == 1) {
692a5ed9182SOmar Sandoval 			/* Merging with neighbor on the left. */
693a5ed9182SOmar Sandoval 			new_extents--;
694a5ed9182SOmar Sandoval 		}
695a5ed9182SOmar Sandoval 		if (next_bit == 1) {
696a5ed9182SOmar Sandoval 			/* Merging with neighbor on the right. */
697a5ed9182SOmar Sandoval 			new_extents--;
698a5ed9182SOmar Sandoval 		}
699a5ed9182SOmar Sandoval 	}
700a5ed9182SOmar Sandoval 
701a5ed9182SOmar Sandoval 	btrfs_release_path(path);
702690d7682SNikolay Borisov 	ret = update_free_space_extent_count(trans, block_group, path,
703a5ed9182SOmar Sandoval 					     new_extents);
704a5ed9182SOmar Sandoval 
705a5ed9182SOmar Sandoval out:
706a5ed9182SOmar Sandoval 	return ret;
707a5ed9182SOmar Sandoval }
708a5ed9182SOmar Sandoval 
709a5ed9182SOmar Sandoval static int remove_free_space_extent(struct btrfs_trans_handle *trans,
71032da5386SDavid Sterba 				    struct btrfs_block_group *block_group,
711a5ed9182SOmar Sandoval 				    struct btrfs_path *path,
712a5ed9182SOmar Sandoval 				    u64 start, u64 size)
713a5ed9182SOmar Sandoval {
7147939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
715a5ed9182SOmar Sandoval 	struct btrfs_key key;
716a5ed9182SOmar Sandoval 	u64 found_start, found_end;
717a5ed9182SOmar Sandoval 	u64 end = start + size;
718a5ed9182SOmar Sandoval 	int new_extents = -1;
719a5ed9182SOmar Sandoval 	int ret;
720a5ed9182SOmar Sandoval 
721a5ed9182SOmar Sandoval 	key.objectid = start;
722a5ed9182SOmar Sandoval 	key.type = (u8)-1;
723a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
724a5ed9182SOmar Sandoval 
725a5ed9182SOmar Sandoval 	ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
726a5ed9182SOmar Sandoval 	if (ret)
727a5ed9182SOmar Sandoval 		goto out;
728a5ed9182SOmar Sandoval 
729a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
730a5ed9182SOmar Sandoval 
731a5ed9182SOmar Sandoval 	ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
732a5ed9182SOmar Sandoval 
733a5ed9182SOmar Sandoval 	found_start = key.objectid;
734a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
735a5ed9182SOmar Sandoval 	ASSERT(start >= found_start && end <= found_end);
736a5ed9182SOmar Sandoval 
737a5ed9182SOmar Sandoval 	/*
738a5ed9182SOmar Sandoval 	 * Okay, now that we've found the free space extent which contains the
739a5ed9182SOmar Sandoval 	 * free space that we are removing, there are four cases:
740a5ed9182SOmar Sandoval 	 *
741a5ed9182SOmar Sandoval 	 * 1. We're using the whole extent: delete the key we found and
742a5ed9182SOmar Sandoval 	 * decrement the free space extent count.
743a5ed9182SOmar Sandoval 	 * 2. We are using part of the extent starting at the beginning: delete
744a5ed9182SOmar Sandoval 	 * the key we found and insert a new key representing the leftover at
745a5ed9182SOmar Sandoval 	 * the end. There is no net change in the number of extents.
746a5ed9182SOmar Sandoval 	 * 3. We are using part of the extent ending at the end: delete the key
747a5ed9182SOmar Sandoval 	 * we found and insert a new key representing the leftover at the
748a5ed9182SOmar Sandoval 	 * beginning. There is no net change in the number of extents.
749a5ed9182SOmar Sandoval 	 * 4. We are using part of the extent in the middle: delete the key we
750a5ed9182SOmar Sandoval 	 * found and insert two new keys representing the leftovers on each
751a5ed9182SOmar Sandoval 	 * side. Where we used to have one extent, we now have two, so increment
752a5ed9182SOmar Sandoval 	 * the extent count. We may need to convert the block group to bitmaps
753a5ed9182SOmar Sandoval 	 * as a result.
754a5ed9182SOmar Sandoval 	 */
755a5ed9182SOmar Sandoval 
756a5ed9182SOmar Sandoval 	/* Delete the existing key (cases 1-4). */
757a5ed9182SOmar Sandoval 	ret = btrfs_del_item(trans, root, path);
758a5ed9182SOmar Sandoval 	if (ret)
759a5ed9182SOmar Sandoval 		goto out;
760a5ed9182SOmar Sandoval 
761a5ed9182SOmar Sandoval 	/* Add a key for leftovers at the beginning (cases 3 and 4). */
762a5ed9182SOmar Sandoval 	if (start > found_start) {
763a5ed9182SOmar Sandoval 		key.objectid = found_start;
764a5ed9182SOmar Sandoval 		key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
765a5ed9182SOmar Sandoval 		key.offset = start - found_start;
766a5ed9182SOmar Sandoval 
767a5ed9182SOmar Sandoval 		btrfs_release_path(path);
768a5ed9182SOmar Sandoval 		ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
769a5ed9182SOmar Sandoval 		if (ret)
770a5ed9182SOmar Sandoval 			goto out;
771a5ed9182SOmar Sandoval 		new_extents++;
772a5ed9182SOmar Sandoval 	}
773a5ed9182SOmar Sandoval 
774a5ed9182SOmar Sandoval 	/* Add a key for leftovers at the end (cases 2 and 4). */
775a5ed9182SOmar Sandoval 	if (end < found_end) {
776a5ed9182SOmar Sandoval 		key.objectid = end;
777a5ed9182SOmar Sandoval 		key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
778a5ed9182SOmar Sandoval 		key.offset = found_end - end;
779a5ed9182SOmar Sandoval 
780a5ed9182SOmar Sandoval 		btrfs_release_path(path);
781a5ed9182SOmar Sandoval 		ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
782a5ed9182SOmar Sandoval 		if (ret)
783a5ed9182SOmar Sandoval 			goto out;
784a5ed9182SOmar Sandoval 		new_extents++;
785a5ed9182SOmar Sandoval 	}
786a5ed9182SOmar Sandoval 
787a5ed9182SOmar Sandoval 	btrfs_release_path(path);
788690d7682SNikolay Borisov 	ret = update_free_space_extent_count(trans, block_group, path,
789a5ed9182SOmar Sandoval 					     new_extents);
790a5ed9182SOmar Sandoval 
791a5ed9182SOmar Sandoval out:
792a5ed9182SOmar Sandoval 	return ret;
793a5ed9182SOmar Sandoval }
794a5ed9182SOmar Sandoval 
795ce9f967fSJohannes Thumshirn EXPORT_FOR_TESTS
796a5ed9182SOmar Sandoval int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
79732da5386SDavid Sterba 				  struct btrfs_block_group *block_group,
798a5ed9182SOmar Sandoval 				  struct btrfs_path *path, u64 start, u64 size)
799a5ed9182SOmar Sandoval {
800a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
801a5ed9182SOmar Sandoval 	u32 flags;
802a5ed9182SOmar Sandoval 	int ret;
803a5ed9182SOmar Sandoval 
804a5ed9182SOmar Sandoval 	if (block_group->needs_free_space) {
8059a7e0f92SNikolay Borisov 		ret = __add_block_group_free_space(trans, block_group, path);
806a5ed9182SOmar Sandoval 		if (ret)
807a5ed9182SOmar Sandoval 			return ret;
808a5ed9182SOmar Sandoval 	}
809a5ed9182SOmar Sandoval 
8102ccf545eSDavid Sterba 	info = search_free_space_info(NULL, block_group, path, 0);
811a5ed9182SOmar Sandoval 	if (IS_ERR(info))
812a5ed9182SOmar Sandoval 		return PTR_ERR(info);
813a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(path->nodes[0], info);
814a5ed9182SOmar Sandoval 	btrfs_release_path(path);
815a5ed9182SOmar Sandoval 
816a5ed9182SOmar Sandoval 	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
81785a7ef13SNikolay Borisov 		return modify_free_space_bitmap(trans, block_group, path,
81885a7ef13SNikolay Borisov 						start, size, 1);
819a5ed9182SOmar Sandoval 	} else {
820e581168dSNikolay Borisov 		return remove_free_space_extent(trans, block_group, path,
821e581168dSNikolay Borisov 						start, size);
822a5ed9182SOmar Sandoval 	}
823a5ed9182SOmar Sandoval }
824a5ed9182SOmar Sandoval 
825a5ed9182SOmar Sandoval int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
826a5ed9182SOmar Sandoval 				u64 start, u64 size)
827a5ed9182SOmar Sandoval {
82832da5386SDavid Sterba 	struct btrfs_block_group *block_group;
829a5ed9182SOmar Sandoval 	struct btrfs_path *path;
830a5ed9182SOmar Sandoval 	int ret;
831a5ed9182SOmar Sandoval 
83225a356d3SNikolay Borisov 	if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
833a5ed9182SOmar Sandoval 		return 0;
834a5ed9182SOmar Sandoval 
835a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
836a5ed9182SOmar Sandoval 	if (!path) {
837a5ed9182SOmar Sandoval 		ret = -ENOMEM;
838a5ed9182SOmar Sandoval 		goto out;
839a5ed9182SOmar Sandoval 	}
840a5ed9182SOmar Sandoval 
84125a356d3SNikolay Borisov 	block_group = btrfs_lookup_block_group(trans->fs_info, start);
842a5ed9182SOmar Sandoval 	if (!block_group) {
843a5ed9182SOmar Sandoval 		ASSERT(0);
844a5ed9182SOmar Sandoval 		ret = -ENOENT;
845a5ed9182SOmar Sandoval 		goto out;
846a5ed9182SOmar Sandoval 	}
847a5ed9182SOmar Sandoval 
848a5ed9182SOmar Sandoval 	mutex_lock(&block_group->free_space_lock);
849c31683a6SNikolay Borisov 	ret = __remove_from_free_space_tree(trans, block_group, path, start,
850c31683a6SNikolay Borisov 					    size);
851a5ed9182SOmar Sandoval 	mutex_unlock(&block_group->free_space_lock);
852a5ed9182SOmar Sandoval 
853a5ed9182SOmar Sandoval 	btrfs_put_block_group(block_group);
854a5ed9182SOmar Sandoval out:
855a5ed9182SOmar Sandoval 	btrfs_free_path(path);
856a5ed9182SOmar Sandoval 	if (ret)
85766642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
858a5ed9182SOmar Sandoval 	return ret;
859a5ed9182SOmar Sandoval }
860a5ed9182SOmar Sandoval 
861a5ed9182SOmar Sandoval static int add_free_space_extent(struct btrfs_trans_handle *trans,
86232da5386SDavid Sterba 				 struct btrfs_block_group *block_group,
863a5ed9182SOmar Sandoval 				 struct btrfs_path *path,
864a5ed9182SOmar Sandoval 				 u64 start, u64 size)
865a5ed9182SOmar Sandoval {
8667939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
867a5ed9182SOmar Sandoval 	struct btrfs_key key, new_key;
868a5ed9182SOmar Sandoval 	u64 found_start, found_end;
869a5ed9182SOmar Sandoval 	u64 end = start + size;
870a5ed9182SOmar Sandoval 	int new_extents = 1;
871a5ed9182SOmar Sandoval 	int ret;
872a5ed9182SOmar Sandoval 
873a5ed9182SOmar Sandoval 	/*
874a5ed9182SOmar Sandoval 	 * We are adding a new extent of free space, but we need to merge
875a5ed9182SOmar Sandoval 	 * extents. There are four cases here:
876a5ed9182SOmar Sandoval 	 *
877a5ed9182SOmar Sandoval 	 * 1. The new extent does not have any immediate neighbors to merge
878a5ed9182SOmar Sandoval 	 * with: add the new key and increment the free space extent count. We
879a5ed9182SOmar Sandoval 	 * may need to convert the block group to bitmaps as a result.
880a5ed9182SOmar Sandoval 	 * 2. The new extent has an immediate neighbor before it: remove the
881a5ed9182SOmar Sandoval 	 * previous key and insert a new key combining both of them. There is no
882a5ed9182SOmar Sandoval 	 * net change in the number of extents.
883a5ed9182SOmar Sandoval 	 * 3. The new extent has an immediate neighbor after it: remove the next
884a5ed9182SOmar Sandoval 	 * key and insert a new key combining both of them. There is no net
885a5ed9182SOmar Sandoval 	 * change in the number of extents.
886a5ed9182SOmar Sandoval 	 * 4. The new extent has immediate neighbors on both sides: remove both
887a5ed9182SOmar Sandoval 	 * of the keys and insert a new key combining all of them. Where we used
888a5ed9182SOmar Sandoval 	 * to have two extents, we now have one, so decrement the extent count.
889a5ed9182SOmar Sandoval 	 */
890a5ed9182SOmar Sandoval 
891a5ed9182SOmar Sandoval 	new_key.objectid = start;
892a5ed9182SOmar Sandoval 	new_key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
893a5ed9182SOmar Sandoval 	new_key.offset = size;
894a5ed9182SOmar Sandoval 
895a5ed9182SOmar Sandoval 	/* Search for a neighbor on the left. */
896b3470b5dSDavid Sterba 	if (start == block_group->start)
897a5ed9182SOmar Sandoval 		goto right;
898a5ed9182SOmar Sandoval 	key.objectid = start - 1;
899a5ed9182SOmar Sandoval 	key.type = (u8)-1;
900a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
901a5ed9182SOmar Sandoval 
902a5ed9182SOmar Sandoval 	ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
903a5ed9182SOmar Sandoval 	if (ret)
904a5ed9182SOmar Sandoval 		goto out;
905a5ed9182SOmar Sandoval 
906a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
907a5ed9182SOmar Sandoval 
908a5ed9182SOmar Sandoval 	if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
909a5ed9182SOmar Sandoval 		ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
910a5ed9182SOmar Sandoval 		btrfs_release_path(path);
911a5ed9182SOmar Sandoval 		goto right;
912a5ed9182SOmar Sandoval 	}
913a5ed9182SOmar Sandoval 
914a5ed9182SOmar Sandoval 	found_start = key.objectid;
915a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
916b3470b5dSDavid Sterba 	ASSERT(found_start >= block_group->start &&
917b3470b5dSDavid Sterba 	       found_end > block_group->start);
918a5ed9182SOmar Sandoval 	ASSERT(found_start < start && found_end <= start);
919a5ed9182SOmar Sandoval 
920a5ed9182SOmar Sandoval 	/*
921a5ed9182SOmar Sandoval 	 * Delete the neighbor on the left and absorb it into the new key (cases
922a5ed9182SOmar Sandoval 	 * 2 and 4).
923a5ed9182SOmar Sandoval 	 */
924a5ed9182SOmar Sandoval 	if (found_end == start) {
925a5ed9182SOmar Sandoval 		ret = btrfs_del_item(trans, root, path);
926a5ed9182SOmar Sandoval 		if (ret)
927a5ed9182SOmar Sandoval 			goto out;
928a5ed9182SOmar Sandoval 		new_key.objectid = found_start;
929a5ed9182SOmar Sandoval 		new_key.offset += key.offset;
930a5ed9182SOmar Sandoval 		new_extents--;
931a5ed9182SOmar Sandoval 	}
932a5ed9182SOmar Sandoval 	btrfs_release_path(path);
933a5ed9182SOmar Sandoval 
934a5ed9182SOmar Sandoval right:
935a5ed9182SOmar Sandoval 	/* Search for a neighbor on the right. */
936b3470b5dSDavid Sterba 	if (end == block_group->start + block_group->length)
937a5ed9182SOmar Sandoval 		goto insert;
938a5ed9182SOmar Sandoval 	key.objectid = end;
939a5ed9182SOmar Sandoval 	key.type = (u8)-1;
940a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
941a5ed9182SOmar Sandoval 
942a5ed9182SOmar Sandoval 	ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
943a5ed9182SOmar Sandoval 	if (ret)
944a5ed9182SOmar Sandoval 		goto out;
945a5ed9182SOmar Sandoval 
946a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
947a5ed9182SOmar Sandoval 
948a5ed9182SOmar Sandoval 	if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
949a5ed9182SOmar Sandoval 		ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
950a5ed9182SOmar Sandoval 		btrfs_release_path(path);
951a5ed9182SOmar Sandoval 		goto insert;
952a5ed9182SOmar Sandoval 	}
953a5ed9182SOmar Sandoval 
954a5ed9182SOmar Sandoval 	found_start = key.objectid;
955a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
956b3470b5dSDavid Sterba 	ASSERT(found_start >= block_group->start &&
957b3470b5dSDavid Sterba 	       found_end > block_group->start);
958a5ed9182SOmar Sandoval 	ASSERT((found_start < start && found_end <= start) ||
959a5ed9182SOmar Sandoval 	       (found_start >= end && found_end > end));
960a5ed9182SOmar Sandoval 
961a5ed9182SOmar Sandoval 	/*
962a5ed9182SOmar Sandoval 	 * Delete the neighbor on the right and absorb it into the new key
963a5ed9182SOmar Sandoval 	 * (cases 3 and 4).
964a5ed9182SOmar Sandoval 	 */
965a5ed9182SOmar Sandoval 	if (found_start == end) {
966a5ed9182SOmar Sandoval 		ret = btrfs_del_item(trans, root, path);
967a5ed9182SOmar Sandoval 		if (ret)
968a5ed9182SOmar Sandoval 			goto out;
969a5ed9182SOmar Sandoval 		new_key.offset += key.offset;
970a5ed9182SOmar Sandoval 		new_extents--;
971a5ed9182SOmar Sandoval 	}
972a5ed9182SOmar Sandoval 	btrfs_release_path(path);
973a5ed9182SOmar Sandoval 
974a5ed9182SOmar Sandoval insert:
975a5ed9182SOmar Sandoval 	/* Insert the new key (cases 1-4). */
976a5ed9182SOmar Sandoval 	ret = btrfs_insert_empty_item(trans, root, path, &new_key, 0);
977a5ed9182SOmar Sandoval 	if (ret)
978a5ed9182SOmar Sandoval 		goto out;
979a5ed9182SOmar Sandoval 
980a5ed9182SOmar Sandoval 	btrfs_release_path(path);
981690d7682SNikolay Borisov 	ret = update_free_space_extent_count(trans, block_group, path,
982a5ed9182SOmar Sandoval 					     new_extents);
983a5ed9182SOmar Sandoval 
984a5ed9182SOmar Sandoval out:
985a5ed9182SOmar Sandoval 	return ret;
986a5ed9182SOmar Sandoval }
987a5ed9182SOmar Sandoval 
988ce9f967fSJohannes Thumshirn EXPORT_FOR_TESTS
989a5ed9182SOmar Sandoval int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
99032da5386SDavid Sterba 			     struct btrfs_block_group *block_group,
991a5ed9182SOmar Sandoval 			     struct btrfs_path *path, u64 start, u64 size)
992a5ed9182SOmar Sandoval {
993a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
994a5ed9182SOmar Sandoval 	u32 flags;
995a5ed9182SOmar Sandoval 	int ret;
996a5ed9182SOmar Sandoval 
997a5ed9182SOmar Sandoval 	if (block_group->needs_free_space) {
9989a7e0f92SNikolay Borisov 		ret = __add_block_group_free_space(trans, block_group, path);
999a5ed9182SOmar Sandoval 		if (ret)
1000a5ed9182SOmar Sandoval 			return ret;
1001a5ed9182SOmar Sandoval 	}
1002a5ed9182SOmar Sandoval 
10032ccf545eSDavid Sterba 	info = search_free_space_info(NULL, block_group, path, 0);
1004a5ed9182SOmar Sandoval 	if (IS_ERR(info))
1005a5ed9182SOmar Sandoval 		return PTR_ERR(info);
1006a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(path->nodes[0], info);
1007a5ed9182SOmar Sandoval 	btrfs_release_path(path);
1008a5ed9182SOmar Sandoval 
1009a5ed9182SOmar Sandoval 	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
101085a7ef13SNikolay Borisov 		return modify_free_space_bitmap(trans, block_group, path,
101185a7ef13SNikolay Borisov 						start, size, 0);
1012a5ed9182SOmar Sandoval 	} else {
10135cb17822SNikolay Borisov 		return add_free_space_extent(trans, block_group, path, start,
10145cb17822SNikolay Borisov 					     size);
1015a5ed9182SOmar Sandoval 	}
1016a5ed9182SOmar Sandoval }
1017a5ed9182SOmar Sandoval 
1018a5ed9182SOmar Sandoval int add_to_free_space_tree(struct btrfs_trans_handle *trans,
1019a5ed9182SOmar Sandoval 			   u64 start, u64 size)
1020a5ed9182SOmar Sandoval {
102132da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1022a5ed9182SOmar Sandoval 	struct btrfs_path *path;
1023a5ed9182SOmar Sandoval 	int ret;
1024a5ed9182SOmar Sandoval 
1025e7355e50SNikolay Borisov 	if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
1026a5ed9182SOmar Sandoval 		return 0;
1027a5ed9182SOmar Sandoval 
1028a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1029a5ed9182SOmar Sandoval 	if (!path) {
1030a5ed9182SOmar Sandoval 		ret = -ENOMEM;
1031a5ed9182SOmar Sandoval 		goto out;
1032a5ed9182SOmar Sandoval 	}
1033a5ed9182SOmar Sandoval 
1034e7355e50SNikolay Borisov 	block_group = btrfs_lookup_block_group(trans->fs_info, start);
1035a5ed9182SOmar Sandoval 	if (!block_group) {
1036a5ed9182SOmar Sandoval 		ASSERT(0);
1037a5ed9182SOmar Sandoval 		ret = -ENOENT;
1038a5ed9182SOmar Sandoval 		goto out;
1039a5ed9182SOmar Sandoval 	}
1040a5ed9182SOmar Sandoval 
1041a5ed9182SOmar Sandoval 	mutex_lock(&block_group->free_space_lock);
10422d5cffa1SNikolay Borisov 	ret = __add_to_free_space_tree(trans, block_group, path, start, size);
1043a5ed9182SOmar Sandoval 	mutex_unlock(&block_group->free_space_lock);
1044a5ed9182SOmar Sandoval 
1045a5ed9182SOmar Sandoval 	btrfs_put_block_group(block_group);
1046a5ed9182SOmar Sandoval out:
1047a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1048a5ed9182SOmar Sandoval 	if (ret)
104966642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1050a5ed9182SOmar Sandoval 	return ret;
1051a5ed9182SOmar Sandoval }
1052a5ed9182SOmar Sandoval 
1053a5ed9182SOmar Sandoval /*
1054a5ed9182SOmar Sandoval  * Populate the free space tree by walking the extent tree. Operations on the
1055a5ed9182SOmar Sandoval  * extent tree that happen as a result of writes to the free space tree will go
1056a5ed9182SOmar Sandoval  * through the normal add/remove hooks.
1057a5ed9182SOmar Sandoval  */
1058a5ed9182SOmar Sandoval static int populate_free_space_tree(struct btrfs_trans_handle *trans,
105932da5386SDavid Sterba 				    struct btrfs_block_group *block_group)
1060a5ed9182SOmar Sandoval {
106129cbcf40SJosef Bacik 	struct btrfs_root *extent_root;
1062a5ed9182SOmar Sandoval 	struct btrfs_path *path, *path2;
1063a5ed9182SOmar Sandoval 	struct btrfs_key key;
1064a5ed9182SOmar Sandoval 	u64 start, end;
1065a5ed9182SOmar Sandoval 	int ret;
1066a5ed9182SOmar Sandoval 
1067a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1068a5ed9182SOmar Sandoval 	if (!path)
1069a5ed9182SOmar Sandoval 		return -ENOMEM;
1070019599adSGu Jinxiang 	path->reada = READA_FORWARD;
1071a5ed9182SOmar Sandoval 
1072a5ed9182SOmar Sandoval 	path2 = btrfs_alloc_path();
1073a5ed9182SOmar Sandoval 	if (!path2) {
1074a5ed9182SOmar Sandoval 		btrfs_free_path(path);
1075a5ed9182SOmar Sandoval 		return -ENOMEM;
1076a5ed9182SOmar Sandoval 	}
1077a5ed9182SOmar Sandoval 
107866afee18SNikolay Borisov 	ret = add_new_free_space_info(trans, block_group, path2);
1079a5ed9182SOmar Sandoval 	if (ret)
1080a5ed9182SOmar Sandoval 		goto out;
1081a5ed9182SOmar Sandoval 
1082511711afSChris Mason 	mutex_lock(&block_group->free_space_lock);
1083511711afSChris Mason 
1084a5ed9182SOmar Sandoval 	/*
1085a5ed9182SOmar Sandoval 	 * Iterate through all of the extent and metadata items in this block
1086a5ed9182SOmar Sandoval 	 * group, adding the free space between them and the free space at the
1087a5ed9182SOmar Sandoval 	 * end. Note that EXTENT_ITEM and METADATA_ITEM are less than
1088a5ed9182SOmar Sandoval 	 * BLOCK_GROUP_ITEM, so an extent may precede the block group that it's
1089a5ed9182SOmar Sandoval 	 * contained in.
1090a5ed9182SOmar Sandoval 	 */
1091b3470b5dSDavid Sterba 	key.objectid = block_group->start;
1092a5ed9182SOmar Sandoval 	key.type = BTRFS_EXTENT_ITEM_KEY;
1093a5ed9182SOmar Sandoval 	key.offset = 0;
1094a5ed9182SOmar Sandoval 
109529cbcf40SJosef Bacik 	extent_root = btrfs_extent_root(trans->fs_info, key.objectid);
1096a5ed9182SOmar Sandoval 	ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
1097a5ed9182SOmar Sandoval 	if (ret < 0)
1098511711afSChris Mason 		goto out_locked;
1099a5ed9182SOmar Sandoval 	ASSERT(ret == 0);
1100a5ed9182SOmar Sandoval 
1101b3470b5dSDavid Sterba 	start = block_group->start;
1102b3470b5dSDavid Sterba 	end = block_group->start + block_group->length;
1103a5ed9182SOmar Sandoval 	while (1) {
1104a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1105a5ed9182SOmar Sandoval 
1106a5ed9182SOmar Sandoval 		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
1107a5ed9182SOmar Sandoval 		    key.type == BTRFS_METADATA_ITEM_KEY) {
1108a5ed9182SOmar Sandoval 			if (key.objectid >= end)
1109a5ed9182SOmar Sandoval 				break;
1110a5ed9182SOmar Sandoval 
1111a5ed9182SOmar Sandoval 			if (start < key.objectid) {
11122d5cffa1SNikolay Borisov 				ret = __add_to_free_space_tree(trans,
1113a5ed9182SOmar Sandoval 							       block_group,
1114a5ed9182SOmar Sandoval 							       path2, start,
1115a5ed9182SOmar Sandoval 							       key.objectid -
1116a5ed9182SOmar Sandoval 							       start);
1117a5ed9182SOmar Sandoval 				if (ret)
1118511711afSChris Mason 					goto out_locked;
1119a5ed9182SOmar Sandoval 			}
1120a5ed9182SOmar Sandoval 			start = key.objectid;
1121a5ed9182SOmar Sandoval 			if (key.type == BTRFS_METADATA_ITEM_KEY)
1122ffa9a9efSNikolay Borisov 				start += trans->fs_info->nodesize;
1123a5ed9182SOmar Sandoval 			else
1124a5ed9182SOmar Sandoval 				start += key.offset;
1125a5ed9182SOmar Sandoval 		} else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
1126b3470b5dSDavid Sterba 			if (key.objectid != block_group->start)
1127a5ed9182SOmar Sandoval 				break;
1128a5ed9182SOmar Sandoval 		}
1129a5ed9182SOmar Sandoval 
1130a5ed9182SOmar Sandoval 		ret = btrfs_next_item(extent_root, path);
1131a5ed9182SOmar Sandoval 		if (ret < 0)
1132511711afSChris Mason 			goto out_locked;
1133a5ed9182SOmar Sandoval 		if (ret)
1134a5ed9182SOmar Sandoval 			break;
1135a5ed9182SOmar Sandoval 	}
1136a5ed9182SOmar Sandoval 	if (start < end) {
11372d5cffa1SNikolay Borisov 		ret = __add_to_free_space_tree(trans, block_group, path2,
11382d5cffa1SNikolay Borisov 					       start, end - start);
1139a5ed9182SOmar Sandoval 		if (ret)
1140511711afSChris Mason 			goto out_locked;
1141a5ed9182SOmar Sandoval 	}
1142a5ed9182SOmar Sandoval 
1143a5ed9182SOmar Sandoval 	ret = 0;
1144511711afSChris Mason out_locked:
1145511711afSChris Mason 	mutex_unlock(&block_group->free_space_lock);
1146a5ed9182SOmar Sandoval out:
1147a5ed9182SOmar Sandoval 	btrfs_free_path(path2);
1148a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1149a5ed9182SOmar Sandoval 	return ret;
1150a5ed9182SOmar Sandoval }
1151a5ed9182SOmar Sandoval 
1152a5ed9182SOmar Sandoval int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
1153a5ed9182SOmar Sandoval {
1154a5ed9182SOmar Sandoval 	struct btrfs_trans_handle *trans;
1155a5ed9182SOmar Sandoval 	struct btrfs_root *tree_root = fs_info->tree_root;
1156a5ed9182SOmar Sandoval 	struct btrfs_root *free_space_root;
115732da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1158a5ed9182SOmar Sandoval 	struct rb_node *node;
1159a5ed9182SOmar Sandoval 	int ret;
1160a5ed9182SOmar Sandoval 
1161a5ed9182SOmar Sandoval 	trans = btrfs_start_transaction(tree_root, 0);
1162a5ed9182SOmar Sandoval 	if (IS_ERR(trans))
1163a5ed9182SOmar Sandoval 		return PTR_ERR(trans);
1164a5ed9182SOmar Sandoval 
1165afcdd129SJosef Bacik 	set_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
11662f96e402SJosef Bacik 	set_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
11679b7a2440SDavid Sterba 	free_space_root = btrfs_create_tree(trans,
1168a5ed9182SOmar Sandoval 					    BTRFS_FREE_SPACE_TREE_OBJECTID);
1169a5ed9182SOmar Sandoval 	if (IS_ERR(free_space_root)) {
1170a5ed9182SOmar Sandoval 		ret = PTR_ERR(free_space_root);
1171a5ed9182SOmar Sandoval 		goto abort;
1172a5ed9182SOmar Sandoval 	}
1173*abed4aaaSJosef Bacik 	ret = btrfs_global_root_insert(free_space_root);
1174*abed4aaaSJosef Bacik 	if (ret) {
1175*abed4aaaSJosef Bacik 		btrfs_put_root(free_space_root);
1176*abed4aaaSJosef Bacik 		goto abort;
1177*abed4aaaSJosef Bacik 	}
1178a5ed9182SOmar Sandoval 
1179a5ed9182SOmar Sandoval 	node = rb_first(&fs_info->block_group_cache_tree);
1180a5ed9182SOmar Sandoval 	while (node) {
118132da5386SDavid Sterba 		block_group = rb_entry(node, struct btrfs_block_group,
1182a5ed9182SOmar Sandoval 				       cache_node);
1183ffa9a9efSNikolay Borisov 		ret = populate_free_space_tree(trans, block_group);
1184a5ed9182SOmar Sandoval 		if (ret)
1185a5ed9182SOmar Sandoval 			goto abort;
1186a5ed9182SOmar Sandoval 		node = rb_next(node);
1187a5ed9182SOmar Sandoval 	}
1188a5ed9182SOmar Sandoval 
1189a5ed9182SOmar Sandoval 	btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
11906675df31SOmar Sandoval 	btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1191afcdd129SJosef Bacik 	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
11922f96e402SJosef Bacik 	ret = btrfs_commit_transaction(trans);
1193a5ed9182SOmar Sandoval 
11942f96e402SJosef Bacik 	/*
11952f96e402SJosef Bacik 	 * Now that we've committed the transaction any reading of our commit
11962f96e402SJosef Bacik 	 * root will be safe, so we can cache from the free space tree now.
11972f96e402SJosef Bacik 	 */
11982f96e402SJosef Bacik 	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
11992f96e402SJosef Bacik 	return ret;
1200a5ed9182SOmar Sandoval 
1201a5ed9182SOmar Sandoval abort:
1202afcdd129SJosef Bacik 	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
12032f96e402SJosef Bacik 	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
120466642832SJeff Mahoney 	btrfs_abort_transaction(trans, ret);
12053a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
1206a5ed9182SOmar Sandoval 	return ret;
1207a5ed9182SOmar Sandoval }
1208a5ed9182SOmar Sandoval 
1209a5ed9182SOmar Sandoval static int clear_free_space_tree(struct btrfs_trans_handle *trans,
1210a5ed9182SOmar Sandoval 				 struct btrfs_root *root)
1211a5ed9182SOmar Sandoval {
1212a5ed9182SOmar Sandoval 	struct btrfs_path *path;
1213a5ed9182SOmar Sandoval 	struct btrfs_key key;
1214a5ed9182SOmar Sandoval 	int nr;
1215a5ed9182SOmar Sandoval 	int ret;
1216a5ed9182SOmar Sandoval 
1217a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1218a5ed9182SOmar Sandoval 	if (!path)
1219a5ed9182SOmar Sandoval 		return -ENOMEM;
1220a5ed9182SOmar Sandoval 
1221a5ed9182SOmar Sandoval 	key.objectid = 0;
1222a5ed9182SOmar Sandoval 	key.type = 0;
1223a5ed9182SOmar Sandoval 	key.offset = 0;
1224a5ed9182SOmar Sandoval 
1225a5ed9182SOmar Sandoval 	while (1) {
1226a5ed9182SOmar Sandoval 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1227a5ed9182SOmar Sandoval 		if (ret < 0)
1228a5ed9182SOmar Sandoval 			goto out;
1229a5ed9182SOmar Sandoval 
1230a5ed9182SOmar Sandoval 		nr = btrfs_header_nritems(path->nodes[0]);
1231a5ed9182SOmar Sandoval 		if (!nr)
1232a5ed9182SOmar Sandoval 			break;
1233a5ed9182SOmar Sandoval 
1234a5ed9182SOmar Sandoval 		path->slots[0] = 0;
1235a5ed9182SOmar Sandoval 		ret = btrfs_del_items(trans, root, path, 0, nr);
1236a5ed9182SOmar Sandoval 		if (ret)
1237a5ed9182SOmar Sandoval 			goto out;
1238a5ed9182SOmar Sandoval 
1239a5ed9182SOmar Sandoval 		btrfs_release_path(path);
1240a5ed9182SOmar Sandoval 	}
1241a5ed9182SOmar Sandoval 
1242a5ed9182SOmar Sandoval 	ret = 0;
1243a5ed9182SOmar Sandoval out:
1244a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1245a5ed9182SOmar Sandoval 	return ret;
1246a5ed9182SOmar Sandoval }
1247a5ed9182SOmar Sandoval 
1248a5ed9182SOmar Sandoval int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
1249a5ed9182SOmar Sandoval {
1250a5ed9182SOmar Sandoval 	struct btrfs_trans_handle *trans;
1251a5ed9182SOmar Sandoval 	struct btrfs_root *tree_root = fs_info->tree_root;
1252*abed4aaaSJosef Bacik 	struct btrfs_key key = {
1253*abed4aaaSJosef Bacik 		.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID,
1254*abed4aaaSJosef Bacik 		.type = BTRFS_ROOT_ITEM_KEY,
1255*abed4aaaSJosef Bacik 		.offset = 0,
1256*abed4aaaSJosef Bacik 	};
1257*abed4aaaSJosef Bacik 	struct btrfs_root *free_space_root = btrfs_global_root(fs_info, &key);
1258a5ed9182SOmar Sandoval 	int ret;
1259a5ed9182SOmar Sandoval 
1260a5ed9182SOmar Sandoval 	trans = btrfs_start_transaction(tree_root, 0);
1261a5ed9182SOmar Sandoval 	if (IS_ERR(trans))
1262a5ed9182SOmar Sandoval 		return PTR_ERR(trans);
1263a5ed9182SOmar Sandoval 
1264a5ed9182SOmar Sandoval 	btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE);
12656675df31SOmar Sandoval 	btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1266a5ed9182SOmar Sandoval 
1267a5ed9182SOmar Sandoval 	ret = clear_free_space_tree(trans, free_space_root);
1268a5ed9182SOmar Sandoval 	if (ret)
1269a5ed9182SOmar Sandoval 		goto abort;
1270a5ed9182SOmar Sandoval 
1271ab9ce7d4SLu Fengqi 	ret = btrfs_del_root(trans, &free_space_root->root_key);
1272a5ed9182SOmar Sandoval 	if (ret)
1273a5ed9182SOmar Sandoval 		goto abort;
1274a5ed9182SOmar Sandoval 
1275*abed4aaaSJosef Bacik 	btrfs_global_root_delete(free_space_root);
1276a5ed9182SOmar Sandoval 	list_del(&free_space_root->dirty_list);
1277a5ed9182SOmar Sandoval 
1278a5ed9182SOmar Sandoval 	btrfs_tree_lock(free_space_root->node);
12796a884d7dSDavid Sterba 	btrfs_clean_tree_block(free_space_root->node);
1280a5ed9182SOmar Sandoval 	btrfs_tree_unlock(free_space_root->node);
12817a163608SFilipe Manana 	btrfs_free_tree_block(trans, btrfs_root_id(free_space_root),
12827a163608SFilipe Manana 			      free_space_root->node, 0, 1);
1283a5ed9182SOmar Sandoval 
128400246528SJosef Bacik 	btrfs_put_root(free_space_root);
1285a5ed9182SOmar Sandoval 
12860bef7109SSahil Kang 	return btrfs_commit_transaction(trans);
1287a5ed9182SOmar Sandoval 
1288a5ed9182SOmar Sandoval abort:
128966642832SJeff Mahoney 	btrfs_abort_transaction(trans, ret);
12903a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
1291a5ed9182SOmar Sandoval 	return ret;
1292a5ed9182SOmar Sandoval }
1293a5ed9182SOmar Sandoval 
1294a5ed9182SOmar Sandoval static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
129532da5386SDavid Sterba 					struct btrfs_block_group *block_group,
1296a5ed9182SOmar Sandoval 					struct btrfs_path *path)
1297a5ed9182SOmar Sandoval {
1298a5ed9182SOmar Sandoval 	int ret;
1299a5ed9182SOmar Sandoval 
1300a5ed9182SOmar Sandoval 	block_group->needs_free_space = 0;
1301a5ed9182SOmar Sandoval 
130266afee18SNikolay Borisov 	ret = add_new_free_space_info(trans, block_group, path);
1303a5ed9182SOmar Sandoval 	if (ret)
1304a5ed9182SOmar Sandoval 		return ret;
1305a5ed9182SOmar Sandoval 
13062d5cffa1SNikolay Borisov 	return __add_to_free_space_tree(trans, block_group, path,
1307b3470b5dSDavid Sterba 					block_group->start,
1308b3470b5dSDavid Sterba 					block_group->length);
1309a5ed9182SOmar Sandoval }
1310a5ed9182SOmar Sandoval 
1311a5ed9182SOmar Sandoval int add_block_group_free_space(struct btrfs_trans_handle *trans,
131232da5386SDavid Sterba 			       struct btrfs_block_group *block_group)
1313a5ed9182SOmar Sandoval {
1314e4e0711cSNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
1315a5ed9182SOmar Sandoval 	struct btrfs_path *path = NULL;
1316a5ed9182SOmar Sandoval 	int ret = 0;
1317a5ed9182SOmar Sandoval 
1318a5ed9182SOmar Sandoval 	if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1319a5ed9182SOmar Sandoval 		return 0;
1320a5ed9182SOmar Sandoval 
1321a5ed9182SOmar Sandoval 	mutex_lock(&block_group->free_space_lock);
1322a5ed9182SOmar Sandoval 	if (!block_group->needs_free_space)
1323a5ed9182SOmar Sandoval 		goto out;
1324a5ed9182SOmar Sandoval 
1325a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1326a5ed9182SOmar Sandoval 	if (!path) {
1327a5ed9182SOmar Sandoval 		ret = -ENOMEM;
1328a5ed9182SOmar Sandoval 		goto out;
1329a5ed9182SOmar Sandoval 	}
1330a5ed9182SOmar Sandoval 
13319a7e0f92SNikolay Borisov 	ret = __add_block_group_free_space(trans, block_group, path);
1332a5ed9182SOmar Sandoval 
1333a5ed9182SOmar Sandoval out:
1334a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1335a5ed9182SOmar Sandoval 	mutex_unlock(&block_group->free_space_lock);
1336a5ed9182SOmar Sandoval 	if (ret)
133766642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1338a5ed9182SOmar Sandoval 	return ret;
1339a5ed9182SOmar Sandoval }
1340a5ed9182SOmar Sandoval 
1341a5ed9182SOmar Sandoval int remove_block_group_free_space(struct btrfs_trans_handle *trans,
134232da5386SDavid Sterba 				  struct btrfs_block_group *block_group)
1343a5ed9182SOmar Sandoval {
13447939dd9fSJosef Bacik 	struct btrfs_root *root = btrfs_free_space_root(block_group);
1345a5ed9182SOmar Sandoval 	struct btrfs_path *path;
1346a5ed9182SOmar Sandoval 	struct btrfs_key key, found_key;
1347a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
1348a5ed9182SOmar Sandoval 	u64 start, end;
1349a5ed9182SOmar Sandoval 	int done = 0, nr;
1350a5ed9182SOmar Sandoval 	int ret;
1351a5ed9182SOmar Sandoval 
1352f3f72779SNikolay Borisov 	if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
1353a5ed9182SOmar Sandoval 		return 0;
1354a5ed9182SOmar Sandoval 
1355a5ed9182SOmar Sandoval 	if (block_group->needs_free_space) {
1356a5ed9182SOmar Sandoval 		/* We never added this block group to the free space tree. */
1357a5ed9182SOmar Sandoval 		return 0;
1358a5ed9182SOmar Sandoval 	}
1359a5ed9182SOmar Sandoval 
1360a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1361a5ed9182SOmar Sandoval 	if (!path) {
1362a5ed9182SOmar Sandoval 		ret = -ENOMEM;
1363a5ed9182SOmar Sandoval 		goto out;
1364a5ed9182SOmar Sandoval 	}
1365a5ed9182SOmar Sandoval 
1366b3470b5dSDavid Sterba 	start = block_group->start;
1367b3470b5dSDavid Sterba 	end = block_group->start + block_group->length;
1368a5ed9182SOmar Sandoval 
1369a5ed9182SOmar Sandoval 	key.objectid = end - 1;
1370a5ed9182SOmar Sandoval 	key.type = (u8)-1;
1371a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
1372a5ed9182SOmar Sandoval 
1373a5ed9182SOmar Sandoval 	while (!done) {
1374a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
1375a5ed9182SOmar Sandoval 		if (ret)
1376a5ed9182SOmar Sandoval 			goto out;
1377a5ed9182SOmar Sandoval 
1378a5ed9182SOmar Sandoval 		leaf = path->nodes[0];
1379a5ed9182SOmar Sandoval 		nr = 0;
1380a5ed9182SOmar Sandoval 		path->slots[0]++;
1381a5ed9182SOmar Sandoval 		while (path->slots[0] > 0) {
1382a5ed9182SOmar Sandoval 			btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
1383a5ed9182SOmar Sandoval 
1384a5ed9182SOmar Sandoval 			if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
1385b3470b5dSDavid Sterba 				ASSERT(found_key.objectid == block_group->start);
1386b3470b5dSDavid Sterba 				ASSERT(found_key.offset == block_group->length);
1387a5ed9182SOmar Sandoval 				done = 1;
1388a5ed9182SOmar Sandoval 				nr++;
1389a5ed9182SOmar Sandoval 				path->slots[0]--;
1390a5ed9182SOmar Sandoval 				break;
1391a5ed9182SOmar Sandoval 			} else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY ||
1392a5ed9182SOmar Sandoval 				   found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
1393a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid >= start);
1394a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid < end);
1395a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid + found_key.offset <= end);
1396a5ed9182SOmar Sandoval 				nr++;
1397a5ed9182SOmar Sandoval 				path->slots[0]--;
1398a5ed9182SOmar Sandoval 			} else {
1399a5ed9182SOmar Sandoval 				ASSERT(0);
1400a5ed9182SOmar Sandoval 			}
1401a5ed9182SOmar Sandoval 		}
1402a5ed9182SOmar Sandoval 
1403a5ed9182SOmar Sandoval 		ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
1404a5ed9182SOmar Sandoval 		if (ret)
1405a5ed9182SOmar Sandoval 			goto out;
1406a5ed9182SOmar Sandoval 		btrfs_release_path(path);
1407a5ed9182SOmar Sandoval 	}
1408a5ed9182SOmar Sandoval 
1409a5ed9182SOmar Sandoval 	ret = 0;
1410a5ed9182SOmar Sandoval out:
1411a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1412a5ed9182SOmar Sandoval 	if (ret)
141366642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1414a5ed9182SOmar Sandoval 	return ret;
1415a5ed9182SOmar Sandoval }
1416a5ed9182SOmar Sandoval 
1417a5ed9182SOmar Sandoval static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
1418a5ed9182SOmar Sandoval 				   struct btrfs_path *path,
1419a5ed9182SOmar Sandoval 				   u32 expected_extent_count)
1420a5ed9182SOmar Sandoval {
142132da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1422a5ed9182SOmar Sandoval 	struct btrfs_fs_info *fs_info;
1423a5ed9182SOmar Sandoval 	struct btrfs_root *root;
1424a5ed9182SOmar Sandoval 	struct btrfs_key key;
1425a5ed9182SOmar Sandoval 	int prev_bit = 0, bit;
1426a5ed9182SOmar Sandoval 	/* Initialize to silence GCC. */
1427a5ed9182SOmar Sandoval 	u64 extent_start = 0;
1428a5ed9182SOmar Sandoval 	u64 end, offset;
1429a5ed9182SOmar Sandoval 	u64 total_found = 0;
1430a5ed9182SOmar Sandoval 	u32 extent_count = 0;
1431a5ed9182SOmar Sandoval 	int ret;
1432a5ed9182SOmar Sandoval 
1433a5ed9182SOmar Sandoval 	block_group = caching_ctl->block_group;
1434a5ed9182SOmar Sandoval 	fs_info = block_group->fs_info;
14357939dd9fSJosef Bacik 	root = btrfs_free_space_root(block_group);
1436a5ed9182SOmar Sandoval 
1437b3470b5dSDavid Sterba 	end = block_group->start + block_group->length;
1438a5ed9182SOmar Sandoval 
1439a5ed9182SOmar Sandoval 	while (1) {
1440a5ed9182SOmar Sandoval 		ret = btrfs_next_item(root, path);
1441a5ed9182SOmar Sandoval 		if (ret < 0)
1442a5ed9182SOmar Sandoval 			goto out;
1443a5ed9182SOmar Sandoval 		if (ret)
1444a5ed9182SOmar Sandoval 			break;
1445a5ed9182SOmar Sandoval 
1446a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1447a5ed9182SOmar Sandoval 
1448a5ed9182SOmar Sandoval 		if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1449a5ed9182SOmar Sandoval 			break;
1450a5ed9182SOmar Sandoval 
1451a5ed9182SOmar Sandoval 		ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
1452a5ed9182SOmar Sandoval 		ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1453a5ed9182SOmar Sandoval 
1454a5ed9182SOmar Sandoval 		caching_ctl->progress = key.objectid;
1455a5ed9182SOmar Sandoval 
1456a5ed9182SOmar Sandoval 		offset = key.objectid;
1457a5ed9182SOmar Sandoval 		while (offset < key.objectid + key.offset) {
1458a5ed9182SOmar Sandoval 			bit = free_space_test_bit(block_group, path, offset);
1459a5ed9182SOmar Sandoval 			if (prev_bit == 0 && bit == 1) {
1460a5ed9182SOmar Sandoval 				extent_start = offset;
1461a5ed9182SOmar Sandoval 			} else if (prev_bit == 1 && bit == 0) {
1462a5ed9182SOmar Sandoval 				total_found += add_new_free_space(block_group,
1463a5ed9182SOmar Sandoval 								  extent_start,
1464a5ed9182SOmar Sandoval 								  offset);
1465a5ed9182SOmar Sandoval 				if (total_found > CACHING_CTL_WAKE_UP) {
1466a5ed9182SOmar Sandoval 					total_found = 0;
1467a5ed9182SOmar Sandoval 					wake_up(&caching_ctl->wait);
1468a5ed9182SOmar Sandoval 				}
1469a5ed9182SOmar Sandoval 				extent_count++;
1470a5ed9182SOmar Sandoval 			}
1471a5ed9182SOmar Sandoval 			prev_bit = bit;
14720b246afaSJeff Mahoney 			offset += fs_info->sectorsize;
1473a5ed9182SOmar Sandoval 		}
1474a5ed9182SOmar Sandoval 	}
1475a5ed9182SOmar Sandoval 	if (prev_bit == 1) {
14764457c1c7SNikolay Borisov 		total_found += add_new_free_space(block_group, extent_start,
14774457c1c7SNikolay Borisov 						  end);
1478a5ed9182SOmar Sandoval 		extent_count++;
1479a5ed9182SOmar Sandoval 	}
1480a5ed9182SOmar Sandoval 
1481a5ed9182SOmar Sandoval 	if (extent_count != expected_extent_count) {
14825d163e0eSJeff Mahoney 		btrfs_err(fs_info,
14835d163e0eSJeff Mahoney 			  "incorrect extent count for %llu; counted %u, expected %u",
1484b3470b5dSDavid Sterba 			  block_group->start, extent_count,
1485a5ed9182SOmar Sandoval 			  expected_extent_count);
1486a5ed9182SOmar Sandoval 		ASSERT(0);
1487a5ed9182SOmar Sandoval 		ret = -EIO;
1488a5ed9182SOmar Sandoval 		goto out;
1489a5ed9182SOmar Sandoval 	}
1490a5ed9182SOmar Sandoval 
1491a5ed9182SOmar Sandoval 	caching_ctl->progress = (u64)-1;
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 
1532a5ed9182SOmar Sandoval 		caching_ctl->progress = key.objectid;
1533a5ed9182SOmar Sandoval 
15344457c1c7SNikolay Borisov 		total_found += add_new_free_space(block_group, key.objectid,
1535a5ed9182SOmar Sandoval 						  key.objectid + key.offset);
1536a5ed9182SOmar Sandoval 		if (total_found > CACHING_CTL_WAKE_UP) {
1537a5ed9182SOmar Sandoval 			total_found = 0;
1538a5ed9182SOmar Sandoval 			wake_up(&caching_ctl->wait);
1539a5ed9182SOmar Sandoval 		}
1540a5ed9182SOmar Sandoval 		extent_count++;
1541a5ed9182SOmar Sandoval 	}
1542a5ed9182SOmar Sandoval 
1543a5ed9182SOmar Sandoval 	if (extent_count != expected_extent_count) {
15445d163e0eSJeff Mahoney 		btrfs_err(fs_info,
15455d163e0eSJeff Mahoney 			  "incorrect extent count for %llu; counted %u, expected %u",
1546b3470b5dSDavid Sterba 			  block_group->start, extent_count,
1547a5ed9182SOmar Sandoval 			  expected_extent_count);
1548a5ed9182SOmar Sandoval 		ASSERT(0);
1549a5ed9182SOmar Sandoval 		ret = -EIO;
1550a5ed9182SOmar Sandoval 		goto out;
1551a5ed9182SOmar Sandoval 	}
1552a5ed9182SOmar Sandoval 
1553a5ed9182SOmar Sandoval 	caching_ctl->progress = (u64)-1;
1554a5ed9182SOmar Sandoval 
1555a5ed9182SOmar Sandoval 	ret = 0;
1556a5ed9182SOmar Sandoval out:
1557a5ed9182SOmar Sandoval 	return ret;
1558a5ed9182SOmar Sandoval }
1559a5ed9182SOmar Sandoval 
1560a5ed9182SOmar Sandoval int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
1561a5ed9182SOmar Sandoval {
156232da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1563a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
1564a5ed9182SOmar Sandoval 	struct btrfs_path *path;
1565a5ed9182SOmar Sandoval 	u32 extent_count, flags;
1566a5ed9182SOmar Sandoval 	int ret;
1567a5ed9182SOmar Sandoval 
1568a5ed9182SOmar Sandoval 	block_group = caching_ctl->block_group;
1569a5ed9182SOmar Sandoval 
1570a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1571a5ed9182SOmar Sandoval 	if (!path)
1572a5ed9182SOmar Sandoval 		return -ENOMEM;
1573a5ed9182SOmar Sandoval 
1574a5ed9182SOmar Sandoval 	/*
1575a5ed9182SOmar Sandoval 	 * Just like caching_thread() doesn't want to deadlock on the extent
1576a5ed9182SOmar Sandoval 	 * tree, we don't want to deadlock on the free space tree.
1577a5ed9182SOmar Sandoval 	 */
1578a5ed9182SOmar Sandoval 	path->skip_locking = 1;
1579a5ed9182SOmar Sandoval 	path->search_commit_root = 1;
15807ce311d5SGu JinXiang 	path->reada = READA_FORWARD;
1581a5ed9182SOmar Sandoval 
15822ccf545eSDavid Sterba 	info = search_free_space_info(NULL, block_group, path, 0);
1583a5ed9182SOmar Sandoval 	if (IS_ERR(info)) {
1584a5ed9182SOmar Sandoval 		ret = PTR_ERR(info);
1585a5ed9182SOmar Sandoval 		goto out;
1586a5ed9182SOmar Sandoval 	}
1587a5ed9182SOmar Sandoval 	extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
1588a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(path->nodes[0], info);
1589a5ed9182SOmar Sandoval 
1590a5ed9182SOmar Sandoval 	/*
1591a5ed9182SOmar Sandoval 	 * We left path pointing to the free space info item, so now
1592a5ed9182SOmar Sandoval 	 * load_free_space_foo can just iterate through the free space tree from
1593a5ed9182SOmar Sandoval 	 * there.
1594a5ed9182SOmar Sandoval 	 */
1595a5ed9182SOmar Sandoval 	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
1596a5ed9182SOmar Sandoval 		ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
1597a5ed9182SOmar Sandoval 	else
1598a5ed9182SOmar Sandoval 		ret = load_free_space_extents(caching_ctl, path, extent_count);
1599a5ed9182SOmar Sandoval 
1600a5ed9182SOmar Sandoval out:
1601a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1602a5ed9182SOmar Sandoval 	return ret;
1603a5ed9182SOmar Sandoval }
1604