xref: /openbmc/linux/fs/btrfs/free-space-tree.c (revision 7ce311d5)
1a5ed9182SOmar Sandoval /*
2a5ed9182SOmar Sandoval  * Copyright (C) 2015 Facebook.  All rights reserved.
3a5ed9182SOmar Sandoval  *
4a5ed9182SOmar Sandoval  * This program is free software; you can redistribute it and/or
5a5ed9182SOmar Sandoval  * modify it under the terms of the GNU General Public
6a5ed9182SOmar Sandoval  * License v2 as published by the Free Software Foundation.
7a5ed9182SOmar Sandoval  *
8a5ed9182SOmar Sandoval  * This program is distributed in the hope that it will be useful,
9a5ed9182SOmar Sandoval  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10a5ed9182SOmar Sandoval  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11a5ed9182SOmar Sandoval  * General Public License for more details.
12a5ed9182SOmar Sandoval  *
13a5ed9182SOmar Sandoval  * You should have received a copy of the GNU General Public
14a5ed9182SOmar Sandoval  * License along with this program; if not, write to the
15a5ed9182SOmar Sandoval  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16a5ed9182SOmar Sandoval  * Boston, MA 021110-1307, USA.
17a5ed9182SOmar Sandoval  */
18a5ed9182SOmar Sandoval 
19a5ed9182SOmar Sandoval #include <linux/kernel.h>
2025ff17e8SOmar Sandoval #include <linux/sched/mm.h>
21a5ed9182SOmar Sandoval #include "ctree.h"
22a5ed9182SOmar Sandoval #include "disk-io.h"
23a5ed9182SOmar Sandoval #include "locking.h"
24a5ed9182SOmar Sandoval #include "free-space-tree.h"
25a5ed9182SOmar Sandoval #include "transaction.h"
26a5ed9182SOmar Sandoval 
27a5ed9182SOmar Sandoval static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
28a5ed9182SOmar Sandoval 					struct btrfs_fs_info *fs_info,
29a5ed9182SOmar Sandoval 					struct btrfs_block_group_cache *block_group,
30a5ed9182SOmar Sandoval 					struct btrfs_path *path);
31a5ed9182SOmar Sandoval 
32a5ed9182SOmar Sandoval void set_free_space_tree_thresholds(struct btrfs_block_group_cache *cache)
33a5ed9182SOmar Sandoval {
34a5ed9182SOmar Sandoval 	u32 bitmap_range;
35a5ed9182SOmar Sandoval 	size_t bitmap_size;
36a5ed9182SOmar Sandoval 	u64 num_bitmaps, total_bitmap_size;
37a5ed9182SOmar Sandoval 
38a5ed9182SOmar Sandoval 	/*
39a5ed9182SOmar Sandoval 	 * We convert to bitmaps when the disk space required for using extents
40a5ed9182SOmar Sandoval 	 * exceeds that required for using bitmaps.
41a5ed9182SOmar Sandoval 	 */
42da17066cSJeff Mahoney 	bitmap_range = cache->fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
43a5ed9182SOmar Sandoval 	num_bitmaps = div_u64(cache->key.offset + bitmap_range - 1,
44a5ed9182SOmar Sandoval 			      bitmap_range);
45a5ed9182SOmar Sandoval 	bitmap_size = sizeof(struct btrfs_item) + BTRFS_FREE_SPACE_BITMAP_SIZE;
46a5ed9182SOmar Sandoval 	total_bitmap_size = num_bitmaps * bitmap_size;
47a5ed9182SOmar Sandoval 	cache->bitmap_high_thresh = div_u64(total_bitmap_size,
48a5ed9182SOmar Sandoval 					    sizeof(struct btrfs_item));
49a5ed9182SOmar Sandoval 
50a5ed9182SOmar Sandoval 	/*
51a5ed9182SOmar Sandoval 	 * We allow for a small buffer between the high threshold and low
52a5ed9182SOmar Sandoval 	 * threshold to avoid thrashing back and forth between the two formats.
53a5ed9182SOmar Sandoval 	 */
54a5ed9182SOmar Sandoval 	if (cache->bitmap_high_thresh > 100)
55a5ed9182SOmar Sandoval 		cache->bitmap_low_thresh = cache->bitmap_high_thresh - 100;
56a5ed9182SOmar Sandoval 	else
57a5ed9182SOmar Sandoval 		cache->bitmap_low_thresh = 0;
58a5ed9182SOmar Sandoval }
59a5ed9182SOmar Sandoval 
60a5ed9182SOmar Sandoval static int add_new_free_space_info(struct btrfs_trans_handle *trans,
61a5ed9182SOmar Sandoval 				   struct btrfs_fs_info *fs_info,
62a5ed9182SOmar Sandoval 				   struct btrfs_block_group_cache *block_group,
63a5ed9182SOmar Sandoval 				   struct btrfs_path *path)
64a5ed9182SOmar Sandoval {
65a5ed9182SOmar Sandoval 	struct btrfs_root *root = fs_info->free_space_root;
66a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
67a5ed9182SOmar Sandoval 	struct btrfs_key key;
68a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
69a5ed9182SOmar Sandoval 	int ret;
70a5ed9182SOmar Sandoval 
71a5ed9182SOmar Sandoval 	key.objectid = block_group->key.objectid;
72a5ed9182SOmar Sandoval 	key.type = BTRFS_FREE_SPACE_INFO_KEY;
73a5ed9182SOmar Sandoval 	key.offset = block_group->key.offset;
74a5ed9182SOmar Sandoval 
75a5ed9182SOmar Sandoval 	ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*info));
76a5ed9182SOmar Sandoval 	if (ret)
77a5ed9182SOmar Sandoval 		goto out;
78a5ed9182SOmar Sandoval 
79a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
80a5ed9182SOmar Sandoval 	info = btrfs_item_ptr(leaf, path->slots[0],
81a5ed9182SOmar Sandoval 			      struct btrfs_free_space_info);
82a5ed9182SOmar Sandoval 	btrfs_set_free_space_extent_count(leaf, info, 0);
83a5ed9182SOmar Sandoval 	btrfs_set_free_space_flags(leaf, info, 0);
84a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(leaf);
85a5ed9182SOmar Sandoval 
86a5ed9182SOmar Sandoval 	ret = 0;
87a5ed9182SOmar Sandoval out:
88a5ed9182SOmar Sandoval 	btrfs_release_path(path);
89a5ed9182SOmar Sandoval 	return ret;
90a5ed9182SOmar Sandoval }
91a5ed9182SOmar Sandoval 
92a5ed9182SOmar Sandoval struct btrfs_free_space_info *
93a5ed9182SOmar Sandoval search_free_space_info(struct btrfs_trans_handle *trans,
94a5ed9182SOmar Sandoval 		       struct btrfs_fs_info *fs_info,
95a5ed9182SOmar Sandoval 		       struct btrfs_block_group_cache *block_group,
96a5ed9182SOmar Sandoval 		       struct btrfs_path *path, int cow)
97a5ed9182SOmar Sandoval {
98a5ed9182SOmar Sandoval 	struct btrfs_root *root = fs_info->free_space_root;
99a5ed9182SOmar Sandoval 	struct btrfs_key key;
100a5ed9182SOmar Sandoval 	int ret;
101a5ed9182SOmar Sandoval 
102a5ed9182SOmar Sandoval 	key.objectid = block_group->key.objectid;
103a5ed9182SOmar Sandoval 	key.type = BTRFS_FREE_SPACE_INFO_KEY;
104a5ed9182SOmar Sandoval 	key.offset = block_group->key.offset;
105a5ed9182SOmar Sandoval 
106a5ed9182SOmar Sandoval 	ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
107a5ed9182SOmar Sandoval 	if (ret < 0)
108a5ed9182SOmar Sandoval 		return ERR_PTR(ret);
109a5ed9182SOmar Sandoval 	if (ret != 0) {
110ab8d0fc4SJeff Mahoney 		btrfs_warn(fs_info, "missing free space info for %llu",
111a5ed9182SOmar Sandoval 			   block_group->key.objectid);
112a5ed9182SOmar Sandoval 		ASSERT(0);
113a5ed9182SOmar Sandoval 		return ERR_PTR(-ENOENT);
114a5ed9182SOmar Sandoval 	}
115a5ed9182SOmar Sandoval 
116a5ed9182SOmar Sandoval 	return btrfs_item_ptr(path->nodes[0], path->slots[0],
117a5ed9182SOmar Sandoval 			      struct btrfs_free_space_info);
118a5ed9182SOmar Sandoval }
119a5ed9182SOmar Sandoval 
120a5ed9182SOmar Sandoval /*
121a5ed9182SOmar Sandoval  * btrfs_search_slot() but we're looking for the greatest key less than the
122a5ed9182SOmar Sandoval  * passed key.
123a5ed9182SOmar Sandoval  */
124a5ed9182SOmar Sandoval static int btrfs_search_prev_slot(struct btrfs_trans_handle *trans,
125a5ed9182SOmar Sandoval 				  struct btrfs_root *root,
126a5ed9182SOmar Sandoval 				  struct btrfs_key *key, struct btrfs_path *p,
127a5ed9182SOmar Sandoval 				  int ins_len, int cow)
128a5ed9182SOmar Sandoval {
129a5ed9182SOmar Sandoval 	int ret;
130a5ed9182SOmar Sandoval 
131a5ed9182SOmar Sandoval 	ret = btrfs_search_slot(trans, root, key, p, ins_len, cow);
132a5ed9182SOmar Sandoval 	if (ret < 0)
133a5ed9182SOmar Sandoval 		return ret;
134a5ed9182SOmar Sandoval 
135a5ed9182SOmar Sandoval 	if (ret == 0) {
136a5ed9182SOmar Sandoval 		ASSERT(0);
137a5ed9182SOmar Sandoval 		return -EIO;
138a5ed9182SOmar Sandoval 	}
139a5ed9182SOmar Sandoval 
140a5ed9182SOmar Sandoval 	if (p->slots[0] == 0) {
141a5ed9182SOmar Sandoval 		ASSERT(0);
142a5ed9182SOmar Sandoval 		return -EIO;
143a5ed9182SOmar Sandoval 	}
144a5ed9182SOmar Sandoval 	p->slots[0]--;
145a5ed9182SOmar Sandoval 
146a5ed9182SOmar Sandoval 	return 0;
147a5ed9182SOmar Sandoval }
148a5ed9182SOmar Sandoval 
149a5ed9182SOmar Sandoval static inline u32 free_space_bitmap_size(u64 size, u32 sectorsize)
150a5ed9182SOmar Sandoval {
151a5ed9182SOmar Sandoval 	return DIV_ROUND_UP((u32)div_u64(size, sectorsize), BITS_PER_BYTE);
152a5ed9182SOmar Sandoval }
153a5ed9182SOmar Sandoval 
1542fe1d551SOmar Sandoval static u8 *alloc_bitmap(u32 bitmap_size)
155a5ed9182SOmar Sandoval {
15625ff17e8SOmar Sandoval 	u8 *ret;
15725ff17e8SOmar Sandoval 	unsigned int nofs_flag;
15879b134a2SDavid Sterba 
15979b134a2SDavid Sterba 	/*
16025ff17e8SOmar Sandoval 	 * GFP_NOFS doesn't work with kvmalloc(), but we really can't recurse
16125ff17e8SOmar Sandoval 	 * into the filesystem as the free space bitmap can be modified in the
16225ff17e8SOmar Sandoval 	 * critical section of a transaction commit.
16325ff17e8SOmar Sandoval 	 *
16425ff17e8SOmar Sandoval 	 * TODO: push the memalloc_nofs_{save,restore}() to the caller where we
16525ff17e8SOmar Sandoval 	 * know that recursion is unsafe.
16679b134a2SDavid Sterba 	 */
16725ff17e8SOmar Sandoval 	nofs_flag = memalloc_nofs_save();
16825ff17e8SOmar Sandoval 	ret = kvzalloc(bitmap_size, GFP_KERNEL);
16925ff17e8SOmar Sandoval 	memalloc_nofs_restore(nofs_flag);
17025ff17e8SOmar Sandoval 	return ret;
171a5ed9182SOmar Sandoval }
172a5ed9182SOmar Sandoval 
173a5ed9182SOmar Sandoval int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
174a5ed9182SOmar Sandoval 				  struct btrfs_fs_info *fs_info,
175a5ed9182SOmar Sandoval 				  struct btrfs_block_group_cache *block_group,
176a5ed9182SOmar Sandoval 				  struct btrfs_path *path)
177a5ed9182SOmar Sandoval {
178a5ed9182SOmar Sandoval 	struct btrfs_root *root = fs_info->free_space_root;
179a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
180a5ed9182SOmar Sandoval 	struct btrfs_key key, found_key;
181a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
1822fe1d551SOmar Sandoval 	u8 *bitmap, *bitmap_cursor;
183a5ed9182SOmar Sandoval 	u64 start, end;
184a5ed9182SOmar Sandoval 	u64 bitmap_range, i;
185a5ed9182SOmar Sandoval 	u32 bitmap_size, flags, expected_extent_count;
186a5ed9182SOmar Sandoval 	u32 extent_count = 0;
187a5ed9182SOmar Sandoval 	int done = 0, nr;
188a5ed9182SOmar Sandoval 	int ret;
189a5ed9182SOmar Sandoval 
190a5ed9182SOmar Sandoval 	bitmap_size = free_space_bitmap_size(block_group->key.offset,
1910b246afaSJeff Mahoney 					     fs_info->sectorsize);
192a5ed9182SOmar Sandoval 	bitmap = alloc_bitmap(bitmap_size);
193a5ed9182SOmar Sandoval 	if (!bitmap) {
194a5ed9182SOmar Sandoval 		ret = -ENOMEM;
195a5ed9182SOmar Sandoval 		goto out;
196a5ed9182SOmar Sandoval 	}
197a5ed9182SOmar Sandoval 
198a5ed9182SOmar Sandoval 	start = block_group->key.objectid;
199a5ed9182SOmar Sandoval 	end = block_group->key.objectid + block_group->key.offset;
200a5ed9182SOmar Sandoval 
201a5ed9182SOmar Sandoval 	key.objectid = end - 1;
202a5ed9182SOmar Sandoval 	key.type = (u8)-1;
203a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
204a5ed9182SOmar Sandoval 
205a5ed9182SOmar Sandoval 	while (!done) {
206a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
207a5ed9182SOmar Sandoval 		if (ret)
208a5ed9182SOmar Sandoval 			goto out;
209a5ed9182SOmar Sandoval 
210a5ed9182SOmar Sandoval 		leaf = path->nodes[0];
211a5ed9182SOmar Sandoval 		nr = 0;
212a5ed9182SOmar Sandoval 		path->slots[0]++;
213a5ed9182SOmar Sandoval 		while (path->slots[0] > 0) {
214a5ed9182SOmar Sandoval 			btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
215a5ed9182SOmar Sandoval 
216a5ed9182SOmar Sandoval 			if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
217a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid == block_group->key.objectid);
218a5ed9182SOmar Sandoval 				ASSERT(found_key.offset == block_group->key.offset);
219a5ed9182SOmar Sandoval 				done = 1;
220a5ed9182SOmar Sandoval 				break;
221a5ed9182SOmar Sandoval 			} else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) {
222a5ed9182SOmar Sandoval 				u64 first, last;
223a5ed9182SOmar Sandoval 
224a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid >= start);
225a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid < end);
226a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid + found_key.offset <= end);
227a5ed9182SOmar Sandoval 
228a5ed9182SOmar Sandoval 				first = div_u64(found_key.objectid - start,
2290b246afaSJeff Mahoney 						fs_info->sectorsize);
230a5ed9182SOmar Sandoval 				last = div_u64(found_key.objectid + found_key.offset - start,
2310b246afaSJeff Mahoney 					       fs_info->sectorsize);
2322fe1d551SOmar Sandoval 				le_bitmap_set(bitmap, first, last - first);
233a5ed9182SOmar Sandoval 
234a5ed9182SOmar Sandoval 				extent_count++;
235a5ed9182SOmar Sandoval 				nr++;
236a5ed9182SOmar Sandoval 				path->slots[0]--;
237a5ed9182SOmar Sandoval 			} else {
238a5ed9182SOmar Sandoval 				ASSERT(0);
239a5ed9182SOmar Sandoval 			}
240a5ed9182SOmar Sandoval 		}
241a5ed9182SOmar Sandoval 
242a5ed9182SOmar Sandoval 		ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
243a5ed9182SOmar Sandoval 		if (ret)
244a5ed9182SOmar Sandoval 			goto out;
245a5ed9182SOmar Sandoval 		btrfs_release_path(path);
246a5ed9182SOmar Sandoval 	}
247a5ed9182SOmar Sandoval 
248a5ed9182SOmar Sandoval 	info = search_free_space_info(trans, fs_info, block_group, path, 1);
249a5ed9182SOmar Sandoval 	if (IS_ERR(info)) {
250a5ed9182SOmar Sandoval 		ret = PTR_ERR(info);
251a5ed9182SOmar Sandoval 		goto out;
252a5ed9182SOmar Sandoval 	}
253a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
254a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(leaf, info);
255a5ed9182SOmar Sandoval 	flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
256a5ed9182SOmar Sandoval 	btrfs_set_free_space_flags(leaf, info, flags);
257a5ed9182SOmar Sandoval 	expected_extent_count = btrfs_free_space_extent_count(leaf, info);
258a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(leaf);
259a5ed9182SOmar Sandoval 	btrfs_release_path(path);
260a5ed9182SOmar Sandoval 
261a5ed9182SOmar Sandoval 	if (extent_count != expected_extent_count) {
2625d163e0eSJeff Mahoney 		btrfs_err(fs_info,
2635d163e0eSJeff Mahoney 			  "incorrect extent count for %llu; counted %u, expected %u",
264a5ed9182SOmar Sandoval 			  block_group->key.objectid, extent_count,
265a5ed9182SOmar Sandoval 			  expected_extent_count);
266a5ed9182SOmar Sandoval 		ASSERT(0);
267a5ed9182SOmar Sandoval 		ret = -EIO;
268a5ed9182SOmar Sandoval 		goto out;
269a5ed9182SOmar Sandoval 	}
270a5ed9182SOmar Sandoval 
2712fe1d551SOmar Sandoval 	bitmap_cursor = bitmap;
2720b246afaSJeff Mahoney 	bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
273a5ed9182SOmar Sandoval 	i = start;
274a5ed9182SOmar Sandoval 	while (i < end) {
275a5ed9182SOmar Sandoval 		unsigned long ptr;
276a5ed9182SOmar Sandoval 		u64 extent_size;
277a5ed9182SOmar Sandoval 		u32 data_size;
278a5ed9182SOmar Sandoval 
279a5ed9182SOmar Sandoval 		extent_size = min(end - i, bitmap_range);
280a5ed9182SOmar Sandoval 		data_size = free_space_bitmap_size(extent_size,
2810b246afaSJeff Mahoney 						   fs_info->sectorsize);
282a5ed9182SOmar Sandoval 
283a5ed9182SOmar Sandoval 		key.objectid = i;
284a5ed9182SOmar Sandoval 		key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
285a5ed9182SOmar Sandoval 		key.offset = extent_size;
286a5ed9182SOmar Sandoval 
287a5ed9182SOmar Sandoval 		ret = btrfs_insert_empty_item(trans, root, path, &key,
288a5ed9182SOmar Sandoval 					      data_size);
289a5ed9182SOmar Sandoval 		if (ret)
290a5ed9182SOmar Sandoval 			goto out;
291a5ed9182SOmar Sandoval 
292a5ed9182SOmar Sandoval 		leaf = path->nodes[0];
293a5ed9182SOmar Sandoval 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
294a5ed9182SOmar Sandoval 		write_extent_buffer(leaf, bitmap_cursor, ptr,
295a5ed9182SOmar Sandoval 				    data_size);
296a5ed9182SOmar Sandoval 		btrfs_mark_buffer_dirty(leaf);
297a5ed9182SOmar Sandoval 		btrfs_release_path(path);
298a5ed9182SOmar Sandoval 
299a5ed9182SOmar Sandoval 		i += extent_size;
300a5ed9182SOmar Sandoval 		bitmap_cursor += data_size;
301a5ed9182SOmar Sandoval 	}
302a5ed9182SOmar Sandoval 
303a5ed9182SOmar Sandoval 	ret = 0;
304a5ed9182SOmar Sandoval out:
30579b134a2SDavid Sterba 	kvfree(bitmap);
306a5ed9182SOmar Sandoval 	if (ret)
30766642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
308a5ed9182SOmar Sandoval 	return ret;
309a5ed9182SOmar Sandoval }
310a5ed9182SOmar Sandoval 
311a5ed9182SOmar Sandoval int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
312a5ed9182SOmar Sandoval 				  struct btrfs_fs_info *fs_info,
313a5ed9182SOmar Sandoval 				  struct btrfs_block_group_cache *block_group,
314a5ed9182SOmar Sandoval 				  struct btrfs_path *path)
315a5ed9182SOmar Sandoval {
316a5ed9182SOmar Sandoval 	struct btrfs_root *root = fs_info->free_space_root;
317a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
318a5ed9182SOmar Sandoval 	struct btrfs_key key, found_key;
319a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
3202fe1d551SOmar Sandoval 	u8 *bitmap;
321a5ed9182SOmar Sandoval 	u64 start, end;
322a5ed9182SOmar Sandoval 	/* Initialize to silence GCC. */
323a5ed9182SOmar Sandoval 	u64 extent_start = 0;
324a5ed9182SOmar Sandoval 	u64 offset;
325a5ed9182SOmar Sandoval 	u32 bitmap_size, flags, expected_extent_count;
326a5ed9182SOmar Sandoval 	int prev_bit = 0, bit, bitnr;
327a5ed9182SOmar Sandoval 	u32 extent_count = 0;
328a5ed9182SOmar Sandoval 	int done = 0, nr;
329a5ed9182SOmar Sandoval 	int ret;
330a5ed9182SOmar Sandoval 
331a5ed9182SOmar Sandoval 	bitmap_size = free_space_bitmap_size(block_group->key.offset,
3320b246afaSJeff Mahoney 					     fs_info->sectorsize);
333a5ed9182SOmar Sandoval 	bitmap = alloc_bitmap(bitmap_size);
334a5ed9182SOmar Sandoval 	if (!bitmap) {
335a5ed9182SOmar Sandoval 		ret = -ENOMEM;
336a5ed9182SOmar Sandoval 		goto out;
337a5ed9182SOmar Sandoval 	}
338a5ed9182SOmar Sandoval 
339a5ed9182SOmar Sandoval 	start = block_group->key.objectid;
340a5ed9182SOmar Sandoval 	end = block_group->key.objectid + block_group->key.offset;
341a5ed9182SOmar Sandoval 
342a5ed9182SOmar Sandoval 	key.objectid = end - 1;
343a5ed9182SOmar Sandoval 	key.type = (u8)-1;
344a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
345a5ed9182SOmar Sandoval 
346a5ed9182SOmar Sandoval 	while (!done) {
347a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
348a5ed9182SOmar Sandoval 		if (ret)
349a5ed9182SOmar Sandoval 			goto out;
350a5ed9182SOmar Sandoval 
351a5ed9182SOmar Sandoval 		leaf = path->nodes[0];
352a5ed9182SOmar Sandoval 		nr = 0;
353a5ed9182SOmar Sandoval 		path->slots[0]++;
354a5ed9182SOmar Sandoval 		while (path->slots[0] > 0) {
355a5ed9182SOmar Sandoval 			btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
356a5ed9182SOmar Sandoval 
357a5ed9182SOmar Sandoval 			if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
358a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid == block_group->key.objectid);
359a5ed9182SOmar Sandoval 				ASSERT(found_key.offset == block_group->key.offset);
360a5ed9182SOmar Sandoval 				done = 1;
361a5ed9182SOmar Sandoval 				break;
362a5ed9182SOmar Sandoval 			} else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
363a5ed9182SOmar Sandoval 				unsigned long ptr;
3642fe1d551SOmar Sandoval 				u8 *bitmap_cursor;
365a5ed9182SOmar Sandoval 				u32 bitmap_pos, data_size;
366a5ed9182SOmar Sandoval 
367a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid >= start);
368a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid < end);
369a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid + found_key.offset <= end);
370a5ed9182SOmar Sandoval 
371a5ed9182SOmar Sandoval 				bitmap_pos = div_u64(found_key.objectid - start,
3720b246afaSJeff Mahoney 						     fs_info->sectorsize *
373a5ed9182SOmar Sandoval 						     BITS_PER_BYTE);
3742fe1d551SOmar Sandoval 				bitmap_cursor = bitmap + bitmap_pos;
375a5ed9182SOmar Sandoval 				data_size = free_space_bitmap_size(found_key.offset,
3760b246afaSJeff Mahoney 								   fs_info->sectorsize);
377a5ed9182SOmar Sandoval 
378a5ed9182SOmar Sandoval 				ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
379a5ed9182SOmar Sandoval 				read_extent_buffer(leaf, bitmap_cursor, ptr,
380a5ed9182SOmar Sandoval 						   data_size);
381a5ed9182SOmar Sandoval 
382a5ed9182SOmar Sandoval 				nr++;
383a5ed9182SOmar Sandoval 				path->slots[0]--;
384a5ed9182SOmar Sandoval 			} else {
385a5ed9182SOmar Sandoval 				ASSERT(0);
386a5ed9182SOmar Sandoval 			}
387a5ed9182SOmar Sandoval 		}
388a5ed9182SOmar Sandoval 
389a5ed9182SOmar Sandoval 		ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
390a5ed9182SOmar Sandoval 		if (ret)
391a5ed9182SOmar Sandoval 			goto out;
392a5ed9182SOmar Sandoval 		btrfs_release_path(path);
393a5ed9182SOmar Sandoval 	}
394a5ed9182SOmar Sandoval 
395a5ed9182SOmar Sandoval 	info = search_free_space_info(trans, fs_info, block_group, path, 1);
396a5ed9182SOmar Sandoval 	if (IS_ERR(info)) {
397a5ed9182SOmar Sandoval 		ret = PTR_ERR(info);
398a5ed9182SOmar Sandoval 		goto out;
399a5ed9182SOmar Sandoval 	}
400a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
401a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(leaf, info);
402a5ed9182SOmar Sandoval 	flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
403a5ed9182SOmar Sandoval 	btrfs_set_free_space_flags(leaf, info, flags);
404a5ed9182SOmar Sandoval 	expected_extent_count = btrfs_free_space_extent_count(leaf, info);
405a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(leaf);
406a5ed9182SOmar Sandoval 	btrfs_release_path(path);
407a5ed9182SOmar Sandoval 
408a5ed9182SOmar Sandoval 	offset = start;
409a5ed9182SOmar Sandoval 	bitnr = 0;
410a5ed9182SOmar Sandoval 	while (offset < end) {
4112fe1d551SOmar Sandoval 		bit = !!le_test_bit(bitnr, bitmap);
412a5ed9182SOmar Sandoval 		if (prev_bit == 0 && bit == 1) {
413a5ed9182SOmar Sandoval 			extent_start = offset;
414a5ed9182SOmar Sandoval 		} else if (prev_bit == 1 && bit == 0) {
415a5ed9182SOmar Sandoval 			key.objectid = extent_start;
416a5ed9182SOmar Sandoval 			key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
417a5ed9182SOmar Sandoval 			key.offset = offset - extent_start;
418a5ed9182SOmar Sandoval 
419a5ed9182SOmar Sandoval 			ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
420a5ed9182SOmar Sandoval 			if (ret)
421a5ed9182SOmar Sandoval 				goto out;
422a5ed9182SOmar Sandoval 			btrfs_release_path(path);
423a5ed9182SOmar Sandoval 
424a5ed9182SOmar Sandoval 			extent_count++;
425a5ed9182SOmar Sandoval 		}
426a5ed9182SOmar Sandoval 		prev_bit = bit;
4270b246afaSJeff Mahoney 		offset += fs_info->sectorsize;
428a5ed9182SOmar Sandoval 		bitnr++;
429a5ed9182SOmar Sandoval 	}
430a5ed9182SOmar Sandoval 	if (prev_bit == 1) {
431a5ed9182SOmar Sandoval 		key.objectid = extent_start;
432a5ed9182SOmar Sandoval 		key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
433a5ed9182SOmar Sandoval 		key.offset = end - extent_start;
434a5ed9182SOmar Sandoval 
435a5ed9182SOmar Sandoval 		ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
436a5ed9182SOmar Sandoval 		if (ret)
437a5ed9182SOmar Sandoval 			goto out;
438a5ed9182SOmar Sandoval 		btrfs_release_path(path);
439a5ed9182SOmar Sandoval 
440a5ed9182SOmar Sandoval 		extent_count++;
441a5ed9182SOmar Sandoval 	}
442a5ed9182SOmar Sandoval 
443a5ed9182SOmar Sandoval 	if (extent_count != expected_extent_count) {
4445d163e0eSJeff Mahoney 		btrfs_err(fs_info,
4455d163e0eSJeff Mahoney 			  "incorrect extent count for %llu; counted %u, expected %u",
446a5ed9182SOmar Sandoval 			  block_group->key.objectid, extent_count,
447a5ed9182SOmar Sandoval 			  expected_extent_count);
448a5ed9182SOmar Sandoval 		ASSERT(0);
449a5ed9182SOmar Sandoval 		ret = -EIO;
450a5ed9182SOmar Sandoval 		goto out;
451a5ed9182SOmar Sandoval 	}
452a5ed9182SOmar Sandoval 
453a5ed9182SOmar Sandoval 	ret = 0;
454a5ed9182SOmar Sandoval out:
45579b134a2SDavid Sterba 	kvfree(bitmap);
456a5ed9182SOmar Sandoval 	if (ret)
45766642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
458a5ed9182SOmar Sandoval 	return ret;
459a5ed9182SOmar Sandoval }
460a5ed9182SOmar Sandoval 
461a5ed9182SOmar Sandoval static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
462a5ed9182SOmar Sandoval 					  struct btrfs_fs_info *fs_info,
463a5ed9182SOmar Sandoval 					  struct btrfs_block_group_cache *block_group,
464a5ed9182SOmar Sandoval 					  struct btrfs_path *path,
465a5ed9182SOmar Sandoval 					  int new_extents)
466a5ed9182SOmar Sandoval {
467a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
468a5ed9182SOmar Sandoval 	u32 flags;
469a5ed9182SOmar Sandoval 	u32 extent_count;
470a5ed9182SOmar Sandoval 	int ret = 0;
471a5ed9182SOmar Sandoval 
472a5ed9182SOmar Sandoval 	if (new_extents == 0)
473a5ed9182SOmar Sandoval 		return 0;
474a5ed9182SOmar Sandoval 
475a5ed9182SOmar Sandoval 	info = search_free_space_info(trans, fs_info, block_group, path, 1);
476a5ed9182SOmar Sandoval 	if (IS_ERR(info)) {
477a5ed9182SOmar Sandoval 		ret = PTR_ERR(info);
478a5ed9182SOmar Sandoval 		goto out;
479a5ed9182SOmar Sandoval 	}
480a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(path->nodes[0], info);
481a5ed9182SOmar Sandoval 	extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
482a5ed9182SOmar Sandoval 
483a5ed9182SOmar Sandoval 	extent_count += new_extents;
484a5ed9182SOmar Sandoval 	btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
485a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(path->nodes[0]);
486a5ed9182SOmar Sandoval 	btrfs_release_path(path);
487a5ed9182SOmar Sandoval 
488a5ed9182SOmar Sandoval 	if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
489a5ed9182SOmar Sandoval 	    extent_count > block_group->bitmap_high_thresh) {
490a5ed9182SOmar Sandoval 		ret = convert_free_space_to_bitmaps(trans, fs_info, block_group,
491a5ed9182SOmar Sandoval 						    path);
492a5ed9182SOmar Sandoval 	} else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
493a5ed9182SOmar Sandoval 		   extent_count < block_group->bitmap_low_thresh) {
494a5ed9182SOmar Sandoval 		ret = convert_free_space_to_extents(trans, fs_info, block_group,
495a5ed9182SOmar Sandoval 						    path);
496a5ed9182SOmar Sandoval 	}
497a5ed9182SOmar Sandoval 
498a5ed9182SOmar Sandoval out:
499a5ed9182SOmar Sandoval 	return ret;
500a5ed9182SOmar Sandoval }
501a5ed9182SOmar Sandoval 
502a5ed9182SOmar Sandoval int free_space_test_bit(struct btrfs_block_group_cache *block_group,
503a5ed9182SOmar Sandoval 			struct btrfs_path *path, u64 offset)
504a5ed9182SOmar Sandoval {
505a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
506a5ed9182SOmar Sandoval 	struct btrfs_key key;
507a5ed9182SOmar Sandoval 	u64 found_start, found_end;
508a5ed9182SOmar Sandoval 	unsigned long ptr, i;
509a5ed9182SOmar Sandoval 
510a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
511a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
512a5ed9182SOmar Sandoval 	ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
513a5ed9182SOmar Sandoval 
514a5ed9182SOmar Sandoval 	found_start = key.objectid;
515a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
516a5ed9182SOmar Sandoval 	ASSERT(offset >= found_start && offset < found_end);
517a5ed9182SOmar Sandoval 
518a5ed9182SOmar Sandoval 	ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
519da17066cSJeff Mahoney 	i = div_u64(offset - found_start,
520da17066cSJeff Mahoney 		    block_group->fs_info->sectorsize);
521a5ed9182SOmar Sandoval 	return !!extent_buffer_test_bit(leaf, ptr, i);
522a5ed9182SOmar Sandoval }
523a5ed9182SOmar Sandoval 
524a5ed9182SOmar Sandoval static void free_space_set_bits(struct btrfs_block_group_cache *block_group,
525a5ed9182SOmar Sandoval 				struct btrfs_path *path, u64 *start, u64 *size,
526a5ed9182SOmar Sandoval 				int bit)
527a5ed9182SOmar Sandoval {
5280b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = block_group->fs_info;
529a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
530a5ed9182SOmar Sandoval 	struct btrfs_key key;
531a5ed9182SOmar Sandoval 	u64 end = *start + *size;
532a5ed9182SOmar Sandoval 	u64 found_start, found_end;
533a5ed9182SOmar Sandoval 	unsigned long ptr, first, last;
534a5ed9182SOmar Sandoval 
535a5ed9182SOmar Sandoval 	leaf = path->nodes[0];
536a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
537a5ed9182SOmar Sandoval 	ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
538a5ed9182SOmar Sandoval 
539a5ed9182SOmar Sandoval 	found_start = key.objectid;
540a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
541a5ed9182SOmar Sandoval 	ASSERT(*start >= found_start && *start < found_end);
542a5ed9182SOmar Sandoval 	ASSERT(end > found_start);
543a5ed9182SOmar Sandoval 
544a5ed9182SOmar Sandoval 	if (end > found_end)
545a5ed9182SOmar Sandoval 		end = found_end;
546a5ed9182SOmar Sandoval 
547a5ed9182SOmar Sandoval 	ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5480b246afaSJeff Mahoney 	first = div_u64(*start - found_start, fs_info->sectorsize);
5490b246afaSJeff Mahoney 	last = div_u64(end - found_start, fs_info->sectorsize);
550a5ed9182SOmar Sandoval 	if (bit)
551a5ed9182SOmar Sandoval 		extent_buffer_bitmap_set(leaf, ptr, first, last - first);
552a5ed9182SOmar Sandoval 	else
553a5ed9182SOmar Sandoval 		extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
554a5ed9182SOmar Sandoval 	btrfs_mark_buffer_dirty(leaf);
555a5ed9182SOmar Sandoval 
556a5ed9182SOmar Sandoval 	*size -= end - *start;
557a5ed9182SOmar Sandoval 	*start = end;
558a5ed9182SOmar Sandoval }
559a5ed9182SOmar Sandoval 
560a5ed9182SOmar Sandoval /*
561a5ed9182SOmar Sandoval  * We can't use btrfs_next_item() in modify_free_space_bitmap() because
562a5ed9182SOmar Sandoval  * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
563a5ed9182SOmar Sandoval  * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
564a5ed9182SOmar Sandoval  * looking for.
565a5ed9182SOmar Sandoval  */
566a5ed9182SOmar Sandoval static int free_space_next_bitmap(struct btrfs_trans_handle *trans,
567a5ed9182SOmar Sandoval 				  struct btrfs_root *root, struct btrfs_path *p)
568a5ed9182SOmar Sandoval {
569a5ed9182SOmar Sandoval 	struct btrfs_key key;
570a5ed9182SOmar Sandoval 
571a5ed9182SOmar Sandoval 	if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
572a5ed9182SOmar Sandoval 		p->slots[0]++;
573a5ed9182SOmar Sandoval 		return 0;
574a5ed9182SOmar Sandoval 	}
575a5ed9182SOmar Sandoval 
576a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
577a5ed9182SOmar Sandoval 	btrfs_release_path(p);
578a5ed9182SOmar Sandoval 
579a5ed9182SOmar Sandoval 	key.objectid += key.offset;
580a5ed9182SOmar Sandoval 	key.type = (u8)-1;
581a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
582a5ed9182SOmar Sandoval 
583a5ed9182SOmar Sandoval 	return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
584a5ed9182SOmar Sandoval }
585a5ed9182SOmar Sandoval 
586a5ed9182SOmar Sandoval /*
587a5ed9182SOmar Sandoval  * If remove is 1, then we are removing free space, thus clearing bits in the
588a5ed9182SOmar Sandoval  * bitmap. If remove is 0, then we are adding free space, thus setting bits in
589a5ed9182SOmar Sandoval  * the bitmap.
590a5ed9182SOmar Sandoval  */
591a5ed9182SOmar Sandoval static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
592a5ed9182SOmar Sandoval 				    struct btrfs_fs_info *fs_info,
593a5ed9182SOmar Sandoval 				    struct btrfs_block_group_cache *block_group,
594a5ed9182SOmar Sandoval 				    struct btrfs_path *path,
595a5ed9182SOmar Sandoval 				    u64 start, u64 size, int remove)
596a5ed9182SOmar Sandoval {
597a5ed9182SOmar Sandoval 	struct btrfs_root *root = fs_info->free_space_root;
598a5ed9182SOmar Sandoval 	struct btrfs_key key;
599a5ed9182SOmar Sandoval 	u64 end = start + size;
600a5ed9182SOmar Sandoval 	u64 cur_start, cur_size;
601a5ed9182SOmar Sandoval 	int prev_bit, next_bit;
602a5ed9182SOmar Sandoval 	int new_extents;
603a5ed9182SOmar Sandoval 	int ret;
604a5ed9182SOmar Sandoval 
605a5ed9182SOmar Sandoval 	/*
606a5ed9182SOmar Sandoval 	 * Read the bit for the block immediately before the extent of space if
607a5ed9182SOmar Sandoval 	 * that block is within the block group.
608a5ed9182SOmar Sandoval 	 */
609a5ed9182SOmar Sandoval 	if (start > block_group->key.objectid) {
610da17066cSJeff Mahoney 		u64 prev_block = start - block_group->fs_info->sectorsize;
611a5ed9182SOmar Sandoval 
612a5ed9182SOmar Sandoval 		key.objectid = prev_block;
613a5ed9182SOmar Sandoval 		key.type = (u8)-1;
614a5ed9182SOmar Sandoval 		key.offset = (u64)-1;
615a5ed9182SOmar Sandoval 
616a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
617a5ed9182SOmar Sandoval 		if (ret)
618a5ed9182SOmar Sandoval 			goto out;
619a5ed9182SOmar Sandoval 
620a5ed9182SOmar Sandoval 		prev_bit = free_space_test_bit(block_group, path, prev_block);
621a5ed9182SOmar Sandoval 
622a5ed9182SOmar Sandoval 		/* The previous block may have been in the previous bitmap. */
623a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
624a5ed9182SOmar Sandoval 		if (start >= key.objectid + key.offset) {
625a5ed9182SOmar Sandoval 			ret = free_space_next_bitmap(trans, root, path);
626a5ed9182SOmar Sandoval 			if (ret)
627a5ed9182SOmar Sandoval 				goto out;
628a5ed9182SOmar Sandoval 		}
629a5ed9182SOmar Sandoval 	} else {
630a5ed9182SOmar Sandoval 		key.objectid = start;
631a5ed9182SOmar Sandoval 		key.type = (u8)-1;
632a5ed9182SOmar Sandoval 		key.offset = (u64)-1;
633a5ed9182SOmar Sandoval 
634a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
635a5ed9182SOmar Sandoval 		if (ret)
636a5ed9182SOmar Sandoval 			goto out;
637a5ed9182SOmar Sandoval 
638a5ed9182SOmar Sandoval 		prev_bit = -1;
639a5ed9182SOmar Sandoval 	}
640a5ed9182SOmar Sandoval 
641a5ed9182SOmar Sandoval 	/*
642a5ed9182SOmar Sandoval 	 * Iterate over all of the bitmaps overlapped by the extent of space,
643a5ed9182SOmar Sandoval 	 * clearing/setting bits as required.
644a5ed9182SOmar Sandoval 	 */
645a5ed9182SOmar Sandoval 	cur_start = start;
646a5ed9182SOmar Sandoval 	cur_size = size;
647a5ed9182SOmar Sandoval 	while (1) {
648a5ed9182SOmar Sandoval 		free_space_set_bits(block_group, path, &cur_start, &cur_size,
649a5ed9182SOmar Sandoval 				    !remove);
650a5ed9182SOmar Sandoval 		if (cur_size == 0)
651a5ed9182SOmar Sandoval 			break;
652a5ed9182SOmar Sandoval 		ret = free_space_next_bitmap(trans, root, path);
653a5ed9182SOmar Sandoval 		if (ret)
654a5ed9182SOmar Sandoval 			goto out;
655a5ed9182SOmar Sandoval 	}
656a5ed9182SOmar Sandoval 
657a5ed9182SOmar Sandoval 	/*
658a5ed9182SOmar Sandoval 	 * Read the bit for the block immediately after the extent of space if
659a5ed9182SOmar Sandoval 	 * that block is within the block group.
660a5ed9182SOmar Sandoval 	 */
661a5ed9182SOmar Sandoval 	if (end < block_group->key.objectid + block_group->key.offset) {
662a5ed9182SOmar Sandoval 		/* The next block may be in the next bitmap. */
663a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
664a5ed9182SOmar Sandoval 		if (end >= key.objectid + key.offset) {
665a5ed9182SOmar Sandoval 			ret = free_space_next_bitmap(trans, root, path);
666a5ed9182SOmar Sandoval 			if (ret)
667a5ed9182SOmar Sandoval 				goto out;
668a5ed9182SOmar Sandoval 		}
669a5ed9182SOmar Sandoval 
670a5ed9182SOmar Sandoval 		next_bit = free_space_test_bit(block_group, path, end);
671a5ed9182SOmar Sandoval 	} else {
672a5ed9182SOmar Sandoval 		next_bit = -1;
673a5ed9182SOmar Sandoval 	}
674a5ed9182SOmar Sandoval 
675a5ed9182SOmar Sandoval 	if (remove) {
676a5ed9182SOmar Sandoval 		new_extents = -1;
677a5ed9182SOmar Sandoval 		if (prev_bit == 1) {
678a5ed9182SOmar Sandoval 			/* Leftover on the left. */
679a5ed9182SOmar Sandoval 			new_extents++;
680a5ed9182SOmar Sandoval 		}
681a5ed9182SOmar Sandoval 		if (next_bit == 1) {
682a5ed9182SOmar Sandoval 			/* Leftover on the right. */
683a5ed9182SOmar Sandoval 			new_extents++;
684a5ed9182SOmar Sandoval 		}
685a5ed9182SOmar Sandoval 	} else {
686a5ed9182SOmar Sandoval 		new_extents = 1;
687a5ed9182SOmar Sandoval 		if (prev_bit == 1) {
688a5ed9182SOmar Sandoval 			/* Merging with neighbor on the left. */
689a5ed9182SOmar Sandoval 			new_extents--;
690a5ed9182SOmar Sandoval 		}
691a5ed9182SOmar Sandoval 		if (next_bit == 1) {
692a5ed9182SOmar Sandoval 			/* Merging with neighbor on the right. */
693a5ed9182SOmar Sandoval 			new_extents--;
694a5ed9182SOmar Sandoval 		}
695a5ed9182SOmar Sandoval 	}
696a5ed9182SOmar Sandoval 
697a5ed9182SOmar Sandoval 	btrfs_release_path(path);
698a5ed9182SOmar Sandoval 	ret = update_free_space_extent_count(trans, fs_info, block_group, path,
699a5ed9182SOmar Sandoval 					     new_extents);
700a5ed9182SOmar Sandoval 
701a5ed9182SOmar Sandoval out:
702a5ed9182SOmar Sandoval 	return ret;
703a5ed9182SOmar Sandoval }
704a5ed9182SOmar Sandoval 
705a5ed9182SOmar Sandoval static int remove_free_space_extent(struct btrfs_trans_handle *trans,
706a5ed9182SOmar Sandoval 				    struct btrfs_fs_info *fs_info,
707a5ed9182SOmar Sandoval 				    struct btrfs_block_group_cache *block_group,
708a5ed9182SOmar Sandoval 				    struct btrfs_path *path,
709a5ed9182SOmar Sandoval 				    u64 start, u64 size)
710a5ed9182SOmar Sandoval {
711a5ed9182SOmar Sandoval 	struct btrfs_root *root = fs_info->free_space_root;
712a5ed9182SOmar Sandoval 	struct btrfs_key key;
713a5ed9182SOmar Sandoval 	u64 found_start, found_end;
714a5ed9182SOmar Sandoval 	u64 end = start + size;
715a5ed9182SOmar Sandoval 	int new_extents = -1;
716a5ed9182SOmar Sandoval 	int ret;
717a5ed9182SOmar Sandoval 
718a5ed9182SOmar Sandoval 	key.objectid = start;
719a5ed9182SOmar Sandoval 	key.type = (u8)-1;
720a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
721a5ed9182SOmar Sandoval 
722a5ed9182SOmar Sandoval 	ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
723a5ed9182SOmar Sandoval 	if (ret)
724a5ed9182SOmar Sandoval 		goto out;
725a5ed9182SOmar Sandoval 
726a5ed9182SOmar Sandoval 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
727a5ed9182SOmar Sandoval 
728a5ed9182SOmar Sandoval 	ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
729a5ed9182SOmar Sandoval 
730a5ed9182SOmar Sandoval 	found_start = key.objectid;
731a5ed9182SOmar Sandoval 	found_end = key.objectid + key.offset;
732a5ed9182SOmar Sandoval 	ASSERT(start >= found_start && end <= found_end);
733a5ed9182SOmar Sandoval 
734a5ed9182SOmar Sandoval 	/*
735a5ed9182SOmar Sandoval 	 * Okay, now that we've found the free space extent which contains the
736a5ed9182SOmar Sandoval 	 * free space that we are removing, there are four cases:
737a5ed9182SOmar Sandoval 	 *
738a5ed9182SOmar Sandoval 	 * 1. We're using the whole extent: delete the key we found and
739a5ed9182SOmar Sandoval 	 * decrement the free space extent count.
740a5ed9182SOmar Sandoval 	 * 2. We are using part of the extent starting at the beginning: delete
741a5ed9182SOmar Sandoval 	 * the key we found and insert a new key representing the leftover at
742a5ed9182SOmar Sandoval 	 * the end. There is no net change in the number of extents.
743a5ed9182SOmar Sandoval 	 * 3. We are using part of the extent ending at the end: delete the key
744a5ed9182SOmar Sandoval 	 * we found and insert a new key representing the leftover at the
745a5ed9182SOmar Sandoval 	 * beginning. There is no net change in the number of extents.
746a5ed9182SOmar Sandoval 	 * 4. We are using part of the extent in the middle: delete the key we
747a5ed9182SOmar Sandoval 	 * found and insert two new keys representing the leftovers on each
748a5ed9182SOmar Sandoval 	 * side. Where we used to have one extent, we now have two, so increment
749a5ed9182SOmar Sandoval 	 * the extent count. We may need to convert the block group to bitmaps
750a5ed9182SOmar Sandoval 	 * as a result.
751a5ed9182SOmar Sandoval 	 */
752a5ed9182SOmar Sandoval 
753a5ed9182SOmar Sandoval 	/* Delete the existing key (cases 1-4). */
754a5ed9182SOmar Sandoval 	ret = btrfs_del_item(trans, root, path);
755a5ed9182SOmar Sandoval 	if (ret)
756a5ed9182SOmar Sandoval 		goto out;
757a5ed9182SOmar Sandoval 
758a5ed9182SOmar Sandoval 	/* Add a key for leftovers at the beginning (cases 3 and 4). */
759a5ed9182SOmar Sandoval 	if (start > found_start) {
760a5ed9182SOmar Sandoval 		key.objectid = found_start;
761a5ed9182SOmar Sandoval 		key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
762a5ed9182SOmar Sandoval 		key.offset = start - found_start;
763a5ed9182SOmar Sandoval 
764a5ed9182SOmar Sandoval 		btrfs_release_path(path);
765a5ed9182SOmar Sandoval 		ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
766a5ed9182SOmar Sandoval 		if (ret)
767a5ed9182SOmar Sandoval 			goto out;
768a5ed9182SOmar Sandoval 		new_extents++;
769a5ed9182SOmar Sandoval 	}
770a5ed9182SOmar Sandoval 
771a5ed9182SOmar Sandoval 	/* Add a key for leftovers at the end (cases 2 and 4). */
772a5ed9182SOmar Sandoval 	if (end < found_end) {
773a5ed9182SOmar Sandoval 		key.objectid = end;
774a5ed9182SOmar Sandoval 		key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
775a5ed9182SOmar Sandoval 		key.offset = found_end - end;
776a5ed9182SOmar Sandoval 
777a5ed9182SOmar Sandoval 		btrfs_release_path(path);
778a5ed9182SOmar Sandoval 		ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
779a5ed9182SOmar Sandoval 		if (ret)
780a5ed9182SOmar Sandoval 			goto out;
781a5ed9182SOmar Sandoval 		new_extents++;
782a5ed9182SOmar Sandoval 	}
783a5ed9182SOmar Sandoval 
784a5ed9182SOmar Sandoval 	btrfs_release_path(path);
785a5ed9182SOmar Sandoval 	ret = update_free_space_extent_count(trans, fs_info, block_group, path,
786a5ed9182SOmar Sandoval 					     new_extents);
787a5ed9182SOmar Sandoval 
788a5ed9182SOmar Sandoval out:
789a5ed9182SOmar Sandoval 	return ret;
790a5ed9182SOmar Sandoval }
791a5ed9182SOmar Sandoval 
792a5ed9182SOmar Sandoval int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
793a5ed9182SOmar Sandoval 				  struct btrfs_fs_info *fs_info,
794a5ed9182SOmar Sandoval 				  struct btrfs_block_group_cache *block_group,
795a5ed9182SOmar Sandoval 				  struct btrfs_path *path, u64 start, u64 size)
796a5ed9182SOmar Sandoval {
797a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
798a5ed9182SOmar Sandoval 	u32 flags;
799a5ed9182SOmar Sandoval 	int ret;
800a5ed9182SOmar Sandoval 
801a5ed9182SOmar Sandoval 	if (block_group->needs_free_space) {
802a5ed9182SOmar Sandoval 		ret = __add_block_group_free_space(trans, fs_info, block_group,
803a5ed9182SOmar Sandoval 						   path);
804a5ed9182SOmar Sandoval 		if (ret)
805a5ed9182SOmar Sandoval 			return ret;
806a5ed9182SOmar Sandoval 	}
807a5ed9182SOmar Sandoval 
808a5ed9182SOmar Sandoval 	info = search_free_space_info(NULL, fs_info, block_group, path, 0);
809a5ed9182SOmar Sandoval 	if (IS_ERR(info))
810a5ed9182SOmar Sandoval 		return PTR_ERR(info);
811a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(path->nodes[0], info);
812a5ed9182SOmar Sandoval 	btrfs_release_path(path);
813a5ed9182SOmar Sandoval 
814a5ed9182SOmar Sandoval 	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
815a5ed9182SOmar Sandoval 		return modify_free_space_bitmap(trans, fs_info, block_group,
816a5ed9182SOmar Sandoval 						path, start, size, 1);
817a5ed9182SOmar Sandoval 	} else {
818a5ed9182SOmar Sandoval 		return remove_free_space_extent(trans, fs_info, block_group,
819a5ed9182SOmar Sandoval 						path, start, size);
820a5ed9182SOmar Sandoval 	}
821a5ed9182SOmar Sandoval }
822a5ed9182SOmar Sandoval 
823a5ed9182SOmar Sandoval int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
824a5ed9182SOmar Sandoval 				struct btrfs_fs_info *fs_info,
825a5ed9182SOmar Sandoval 				u64 start, u64 size)
826a5ed9182SOmar Sandoval {
827a5ed9182SOmar Sandoval 	struct btrfs_block_group_cache *block_group;
828a5ed9182SOmar Sandoval 	struct btrfs_path *path;
829a5ed9182SOmar Sandoval 	int ret;
830a5ed9182SOmar Sandoval 
831a5ed9182SOmar Sandoval 	if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
832a5ed9182SOmar Sandoval 		return 0;
833a5ed9182SOmar Sandoval 
834a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
835a5ed9182SOmar Sandoval 	if (!path) {
836a5ed9182SOmar Sandoval 		ret = -ENOMEM;
837a5ed9182SOmar Sandoval 		goto out;
838a5ed9182SOmar Sandoval 	}
839a5ed9182SOmar Sandoval 
840a5ed9182SOmar Sandoval 	block_group = btrfs_lookup_block_group(fs_info, start);
841a5ed9182SOmar Sandoval 	if (!block_group) {
842a5ed9182SOmar Sandoval 		ASSERT(0);
843a5ed9182SOmar Sandoval 		ret = -ENOENT;
844a5ed9182SOmar Sandoval 		goto out;
845a5ed9182SOmar Sandoval 	}
846a5ed9182SOmar Sandoval 
847a5ed9182SOmar Sandoval 	mutex_lock(&block_group->free_space_lock);
848a5ed9182SOmar Sandoval 	ret = __remove_from_free_space_tree(trans, fs_info, block_group, path,
849a5ed9182SOmar Sandoval 					    start, size);
850a5ed9182SOmar Sandoval 	mutex_unlock(&block_group->free_space_lock);
851a5ed9182SOmar Sandoval 
852a5ed9182SOmar Sandoval 	btrfs_put_block_group(block_group);
853a5ed9182SOmar Sandoval out:
854a5ed9182SOmar Sandoval 	btrfs_free_path(path);
855a5ed9182SOmar Sandoval 	if (ret)
85666642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
857a5ed9182SOmar Sandoval 	return ret;
858a5ed9182SOmar Sandoval }
859a5ed9182SOmar Sandoval 
860a5ed9182SOmar Sandoval static int add_free_space_extent(struct btrfs_trans_handle *trans,
861a5ed9182SOmar Sandoval 				 struct btrfs_fs_info *fs_info,
862a5ed9182SOmar Sandoval 				 struct btrfs_block_group_cache *block_group,
863a5ed9182SOmar Sandoval 				 struct btrfs_path *path,
864a5ed9182SOmar Sandoval 				 u64 start, u64 size)
865a5ed9182SOmar Sandoval {
866a5ed9182SOmar Sandoval 	struct btrfs_root *root = fs_info->free_space_root;
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. */
896a5ed9182SOmar Sandoval 	if (start == block_group->key.objectid)
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;
916a5ed9182SOmar Sandoval 	ASSERT(found_start >= block_group->key.objectid &&
917a5ed9182SOmar Sandoval 	       found_end > block_group->key.objectid);
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. */
936a5ed9182SOmar Sandoval 	if (end == block_group->key.objectid + block_group->key.offset)
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;
956a5ed9182SOmar Sandoval 	ASSERT(found_start >= block_group->key.objectid &&
957a5ed9182SOmar Sandoval 	       found_end > block_group->key.objectid);
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);
981a5ed9182SOmar Sandoval 	ret = update_free_space_extent_count(trans, fs_info, block_group, path,
982a5ed9182SOmar Sandoval 					     new_extents);
983a5ed9182SOmar Sandoval 
984a5ed9182SOmar Sandoval out:
985a5ed9182SOmar Sandoval 	return ret;
986a5ed9182SOmar Sandoval }
987a5ed9182SOmar Sandoval 
988a5ed9182SOmar Sandoval int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
989a5ed9182SOmar Sandoval 			     struct btrfs_fs_info *fs_info,
990a5ed9182SOmar Sandoval 			     struct btrfs_block_group_cache *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) {
998a5ed9182SOmar Sandoval 		ret = __add_block_group_free_space(trans, fs_info, block_group,
999a5ed9182SOmar Sandoval 						   path);
1000a5ed9182SOmar Sandoval 		if (ret)
1001a5ed9182SOmar Sandoval 			return ret;
1002a5ed9182SOmar Sandoval 	}
1003a5ed9182SOmar Sandoval 
1004a5ed9182SOmar Sandoval 	info = search_free_space_info(NULL, fs_info, block_group, path, 0);
1005a5ed9182SOmar Sandoval 	if (IS_ERR(info))
1006a5ed9182SOmar Sandoval 		return PTR_ERR(info);
1007a5ed9182SOmar Sandoval 	flags = btrfs_free_space_flags(path->nodes[0], info);
1008a5ed9182SOmar Sandoval 	btrfs_release_path(path);
1009a5ed9182SOmar Sandoval 
1010a5ed9182SOmar Sandoval 	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
1011a5ed9182SOmar Sandoval 		return modify_free_space_bitmap(trans, fs_info, block_group,
1012a5ed9182SOmar Sandoval 						path, start, size, 0);
1013a5ed9182SOmar Sandoval 	} else {
1014a5ed9182SOmar Sandoval 		return add_free_space_extent(trans, fs_info, block_group, path,
1015a5ed9182SOmar Sandoval 					     start, size);
1016a5ed9182SOmar Sandoval 	}
1017a5ed9182SOmar Sandoval }
1018a5ed9182SOmar Sandoval 
1019a5ed9182SOmar Sandoval int add_to_free_space_tree(struct btrfs_trans_handle *trans,
1020a5ed9182SOmar Sandoval 			   struct btrfs_fs_info *fs_info,
1021a5ed9182SOmar Sandoval 			   u64 start, u64 size)
1022a5ed9182SOmar Sandoval {
1023a5ed9182SOmar Sandoval 	struct btrfs_block_group_cache *block_group;
1024a5ed9182SOmar Sandoval 	struct btrfs_path *path;
1025a5ed9182SOmar Sandoval 	int ret;
1026a5ed9182SOmar Sandoval 
1027a5ed9182SOmar Sandoval 	if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1028a5ed9182SOmar Sandoval 		return 0;
1029a5ed9182SOmar Sandoval 
1030a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1031a5ed9182SOmar Sandoval 	if (!path) {
1032a5ed9182SOmar Sandoval 		ret = -ENOMEM;
1033a5ed9182SOmar Sandoval 		goto out;
1034a5ed9182SOmar Sandoval 	}
1035a5ed9182SOmar Sandoval 
1036a5ed9182SOmar Sandoval 	block_group = btrfs_lookup_block_group(fs_info, start);
1037a5ed9182SOmar Sandoval 	if (!block_group) {
1038a5ed9182SOmar Sandoval 		ASSERT(0);
1039a5ed9182SOmar Sandoval 		ret = -ENOENT;
1040a5ed9182SOmar Sandoval 		goto out;
1041a5ed9182SOmar Sandoval 	}
1042a5ed9182SOmar Sandoval 
1043a5ed9182SOmar Sandoval 	mutex_lock(&block_group->free_space_lock);
1044a5ed9182SOmar Sandoval 	ret = __add_to_free_space_tree(trans, fs_info, block_group, path, start,
1045a5ed9182SOmar Sandoval 				       size);
1046a5ed9182SOmar Sandoval 	mutex_unlock(&block_group->free_space_lock);
1047a5ed9182SOmar Sandoval 
1048a5ed9182SOmar Sandoval 	btrfs_put_block_group(block_group);
1049a5ed9182SOmar Sandoval out:
1050a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1051a5ed9182SOmar Sandoval 	if (ret)
105266642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1053a5ed9182SOmar Sandoval 	return ret;
1054a5ed9182SOmar Sandoval }
1055a5ed9182SOmar Sandoval 
1056a5ed9182SOmar Sandoval /*
1057a5ed9182SOmar Sandoval  * Populate the free space tree by walking the extent tree. Operations on the
1058a5ed9182SOmar Sandoval  * extent tree that happen as a result of writes to the free space tree will go
1059a5ed9182SOmar Sandoval  * through the normal add/remove hooks.
1060a5ed9182SOmar Sandoval  */
1061a5ed9182SOmar Sandoval static int populate_free_space_tree(struct btrfs_trans_handle *trans,
1062a5ed9182SOmar Sandoval 				    struct btrfs_fs_info *fs_info,
1063a5ed9182SOmar Sandoval 				    struct btrfs_block_group_cache *block_group)
1064a5ed9182SOmar Sandoval {
1065a5ed9182SOmar Sandoval 	struct btrfs_root *extent_root = fs_info->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 
1082a5ed9182SOmar Sandoval 	ret = add_new_free_space_info(trans, fs_info, 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 	 */
1095a5ed9182SOmar Sandoval 	key.objectid = block_group->key.objectid;
1096a5ed9182SOmar Sandoval 	key.type = BTRFS_EXTENT_ITEM_KEY;
1097a5ed9182SOmar Sandoval 	key.offset = 0;
1098a5ed9182SOmar Sandoval 
1099a5ed9182SOmar Sandoval 	ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
1100a5ed9182SOmar Sandoval 	if (ret < 0)
1101511711afSChris Mason 		goto out_locked;
1102a5ed9182SOmar Sandoval 	ASSERT(ret == 0);
1103a5ed9182SOmar Sandoval 
1104a5ed9182SOmar Sandoval 	start = block_group->key.objectid;
1105a5ed9182SOmar Sandoval 	end = block_group->key.objectid + block_group->key.offset;
1106a5ed9182SOmar Sandoval 	while (1) {
1107a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1108a5ed9182SOmar Sandoval 
1109a5ed9182SOmar Sandoval 		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
1110a5ed9182SOmar Sandoval 		    key.type == BTRFS_METADATA_ITEM_KEY) {
1111a5ed9182SOmar Sandoval 			if (key.objectid >= end)
1112a5ed9182SOmar Sandoval 				break;
1113a5ed9182SOmar Sandoval 
1114a5ed9182SOmar Sandoval 			if (start < key.objectid) {
1115a5ed9182SOmar Sandoval 				ret = __add_to_free_space_tree(trans, fs_info,
1116a5ed9182SOmar Sandoval 							       block_group,
1117a5ed9182SOmar Sandoval 							       path2, start,
1118a5ed9182SOmar Sandoval 							       key.objectid -
1119a5ed9182SOmar Sandoval 							       start);
1120a5ed9182SOmar Sandoval 				if (ret)
1121511711afSChris Mason 					goto out_locked;
1122a5ed9182SOmar Sandoval 			}
1123a5ed9182SOmar Sandoval 			start = key.objectid;
1124a5ed9182SOmar Sandoval 			if (key.type == BTRFS_METADATA_ITEM_KEY)
1125da17066cSJeff Mahoney 				start += fs_info->nodesize;
1126a5ed9182SOmar Sandoval 			else
1127a5ed9182SOmar Sandoval 				start += key.offset;
1128a5ed9182SOmar Sandoval 		} else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
1129a5ed9182SOmar Sandoval 			if (key.objectid != block_group->key.objectid)
1130a5ed9182SOmar Sandoval 				break;
1131a5ed9182SOmar Sandoval 		}
1132a5ed9182SOmar Sandoval 
1133a5ed9182SOmar Sandoval 		ret = btrfs_next_item(extent_root, path);
1134a5ed9182SOmar Sandoval 		if (ret < 0)
1135511711afSChris Mason 			goto out_locked;
1136a5ed9182SOmar Sandoval 		if (ret)
1137a5ed9182SOmar Sandoval 			break;
1138a5ed9182SOmar Sandoval 	}
1139a5ed9182SOmar Sandoval 	if (start < end) {
1140a5ed9182SOmar Sandoval 		ret = __add_to_free_space_tree(trans, fs_info, block_group,
1141a5ed9182SOmar Sandoval 					       path2, start, end - start);
1142a5ed9182SOmar Sandoval 		if (ret)
1143511711afSChris Mason 			goto out_locked;
1144a5ed9182SOmar Sandoval 	}
1145a5ed9182SOmar Sandoval 
1146a5ed9182SOmar Sandoval 	ret = 0;
1147511711afSChris Mason out_locked:
1148511711afSChris Mason 	mutex_unlock(&block_group->free_space_lock);
1149a5ed9182SOmar Sandoval out:
1150a5ed9182SOmar Sandoval 	btrfs_free_path(path2);
1151a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1152a5ed9182SOmar Sandoval 	return ret;
1153a5ed9182SOmar Sandoval }
1154a5ed9182SOmar Sandoval 
1155a5ed9182SOmar Sandoval int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
1156a5ed9182SOmar Sandoval {
1157a5ed9182SOmar Sandoval 	struct btrfs_trans_handle *trans;
1158a5ed9182SOmar Sandoval 	struct btrfs_root *tree_root = fs_info->tree_root;
1159a5ed9182SOmar Sandoval 	struct btrfs_root *free_space_root;
1160a5ed9182SOmar Sandoval 	struct btrfs_block_group_cache *block_group;
1161a5ed9182SOmar Sandoval 	struct rb_node *node;
1162a5ed9182SOmar Sandoval 	int ret;
1163a5ed9182SOmar Sandoval 
1164a5ed9182SOmar Sandoval 	trans = btrfs_start_transaction(tree_root, 0);
1165a5ed9182SOmar Sandoval 	if (IS_ERR(trans))
1166a5ed9182SOmar Sandoval 		return PTR_ERR(trans);
1167a5ed9182SOmar Sandoval 
1168afcdd129SJosef Bacik 	set_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1169a5ed9182SOmar Sandoval 	free_space_root = btrfs_create_tree(trans, fs_info,
1170a5ed9182SOmar Sandoval 					    BTRFS_FREE_SPACE_TREE_OBJECTID);
1171a5ed9182SOmar Sandoval 	if (IS_ERR(free_space_root)) {
1172a5ed9182SOmar Sandoval 		ret = PTR_ERR(free_space_root);
1173a5ed9182SOmar Sandoval 		goto abort;
1174a5ed9182SOmar Sandoval 	}
1175a5ed9182SOmar Sandoval 	fs_info->free_space_root = free_space_root;
1176a5ed9182SOmar Sandoval 
1177a5ed9182SOmar Sandoval 	node = rb_first(&fs_info->block_group_cache_tree);
1178a5ed9182SOmar Sandoval 	while (node) {
1179a5ed9182SOmar Sandoval 		block_group = rb_entry(node, struct btrfs_block_group_cache,
1180a5ed9182SOmar Sandoval 				       cache_node);
1181a5ed9182SOmar Sandoval 		ret = populate_free_space_tree(trans, fs_info, block_group);
1182a5ed9182SOmar Sandoval 		if (ret)
1183a5ed9182SOmar Sandoval 			goto abort;
1184a5ed9182SOmar Sandoval 		node = rb_next(node);
1185a5ed9182SOmar Sandoval 	}
1186a5ed9182SOmar Sandoval 
1187a5ed9182SOmar Sandoval 	btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
11886675df31SOmar Sandoval 	btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1189afcdd129SJosef Bacik 	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1190a5ed9182SOmar Sandoval 
11910bef7109SSahil Kang 	return btrfs_commit_transaction(trans);
1192a5ed9182SOmar Sandoval 
1193a5ed9182SOmar Sandoval abort:
1194afcdd129SJosef Bacik 	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
119566642832SJeff Mahoney 	btrfs_abort_transaction(trans, ret);
11963a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
1197a5ed9182SOmar Sandoval 	return ret;
1198a5ed9182SOmar Sandoval }
1199a5ed9182SOmar Sandoval 
1200a5ed9182SOmar Sandoval static int clear_free_space_tree(struct btrfs_trans_handle *trans,
1201a5ed9182SOmar Sandoval 				 struct btrfs_root *root)
1202a5ed9182SOmar Sandoval {
1203a5ed9182SOmar Sandoval 	struct btrfs_path *path;
1204a5ed9182SOmar Sandoval 	struct btrfs_key key;
1205a5ed9182SOmar Sandoval 	int nr;
1206a5ed9182SOmar Sandoval 	int ret;
1207a5ed9182SOmar Sandoval 
1208a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1209a5ed9182SOmar Sandoval 	if (!path)
1210a5ed9182SOmar Sandoval 		return -ENOMEM;
1211a5ed9182SOmar Sandoval 
1212a5ed9182SOmar Sandoval 	path->leave_spinning = 1;
1213a5ed9182SOmar Sandoval 
1214a5ed9182SOmar Sandoval 	key.objectid = 0;
1215a5ed9182SOmar Sandoval 	key.type = 0;
1216a5ed9182SOmar Sandoval 	key.offset = 0;
1217a5ed9182SOmar Sandoval 
1218a5ed9182SOmar Sandoval 	while (1) {
1219a5ed9182SOmar Sandoval 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1220a5ed9182SOmar Sandoval 		if (ret < 0)
1221a5ed9182SOmar Sandoval 			goto out;
1222a5ed9182SOmar Sandoval 
1223a5ed9182SOmar Sandoval 		nr = btrfs_header_nritems(path->nodes[0]);
1224a5ed9182SOmar Sandoval 		if (!nr)
1225a5ed9182SOmar Sandoval 			break;
1226a5ed9182SOmar Sandoval 
1227a5ed9182SOmar Sandoval 		path->slots[0] = 0;
1228a5ed9182SOmar Sandoval 		ret = btrfs_del_items(trans, root, path, 0, nr);
1229a5ed9182SOmar Sandoval 		if (ret)
1230a5ed9182SOmar Sandoval 			goto out;
1231a5ed9182SOmar Sandoval 
1232a5ed9182SOmar Sandoval 		btrfs_release_path(path);
1233a5ed9182SOmar Sandoval 	}
1234a5ed9182SOmar Sandoval 
1235a5ed9182SOmar Sandoval 	ret = 0;
1236a5ed9182SOmar Sandoval out:
1237a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1238a5ed9182SOmar Sandoval 	return ret;
1239a5ed9182SOmar Sandoval }
1240a5ed9182SOmar Sandoval 
1241a5ed9182SOmar Sandoval int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
1242a5ed9182SOmar Sandoval {
1243a5ed9182SOmar Sandoval 	struct btrfs_trans_handle *trans;
1244a5ed9182SOmar Sandoval 	struct btrfs_root *tree_root = fs_info->tree_root;
1245a5ed9182SOmar Sandoval 	struct btrfs_root *free_space_root = fs_info->free_space_root;
1246a5ed9182SOmar Sandoval 	int ret;
1247a5ed9182SOmar Sandoval 
1248a5ed9182SOmar Sandoval 	trans = btrfs_start_transaction(tree_root, 0);
1249a5ed9182SOmar Sandoval 	if (IS_ERR(trans))
1250a5ed9182SOmar Sandoval 		return PTR_ERR(trans);
1251a5ed9182SOmar Sandoval 
1252a5ed9182SOmar Sandoval 	btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE);
12536675df31SOmar Sandoval 	btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1254a5ed9182SOmar Sandoval 	fs_info->free_space_root = NULL;
1255a5ed9182SOmar Sandoval 
1256a5ed9182SOmar Sandoval 	ret = clear_free_space_tree(trans, free_space_root);
1257a5ed9182SOmar Sandoval 	if (ret)
1258a5ed9182SOmar Sandoval 		goto abort;
1259a5ed9182SOmar Sandoval 
12601cd5447eSJeff Mahoney 	ret = btrfs_del_root(trans, fs_info, &free_space_root->root_key);
1261a5ed9182SOmar Sandoval 	if (ret)
1262a5ed9182SOmar Sandoval 		goto abort;
1263a5ed9182SOmar Sandoval 
1264a5ed9182SOmar Sandoval 	list_del(&free_space_root->dirty_list);
1265a5ed9182SOmar Sandoval 
1266a5ed9182SOmar Sandoval 	btrfs_tree_lock(free_space_root->node);
12677c302b49SDavid Sterba 	clean_tree_block(fs_info, free_space_root->node);
1268a5ed9182SOmar Sandoval 	btrfs_tree_unlock(free_space_root->node);
1269a5ed9182SOmar Sandoval 	btrfs_free_tree_block(trans, free_space_root, free_space_root->node,
1270a5ed9182SOmar Sandoval 			      0, 1);
1271a5ed9182SOmar Sandoval 
1272a5ed9182SOmar Sandoval 	free_extent_buffer(free_space_root->node);
1273a5ed9182SOmar Sandoval 	free_extent_buffer(free_space_root->commit_root);
1274a5ed9182SOmar Sandoval 	kfree(free_space_root);
1275a5ed9182SOmar Sandoval 
12760bef7109SSahil Kang 	return btrfs_commit_transaction(trans);
1277a5ed9182SOmar Sandoval 
1278a5ed9182SOmar Sandoval abort:
127966642832SJeff Mahoney 	btrfs_abort_transaction(trans, ret);
12803a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
1281a5ed9182SOmar Sandoval 	return ret;
1282a5ed9182SOmar Sandoval }
1283a5ed9182SOmar Sandoval 
1284a5ed9182SOmar Sandoval static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
1285a5ed9182SOmar Sandoval 					struct btrfs_fs_info *fs_info,
1286a5ed9182SOmar Sandoval 					struct btrfs_block_group_cache *block_group,
1287a5ed9182SOmar Sandoval 					struct btrfs_path *path)
1288a5ed9182SOmar Sandoval {
1289a5ed9182SOmar Sandoval 	int ret;
1290a5ed9182SOmar Sandoval 
1291a5ed9182SOmar Sandoval 	block_group->needs_free_space = 0;
1292a5ed9182SOmar Sandoval 
1293a5ed9182SOmar Sandoval 	ret = add_new_free_space_info(trans, fs_info, block_group, path);
1294a5ed9182SOmar Sandoval 	if (ret)
1295a5ed9182SOmar Sandoval 		return ret;
1296a5ed9182SOmar Sandoval 
1297a5ed9182SOmar Sandoval 	return __add_to_free_space_tree(trans, fs_info, block_group, path,
1298a5ed9182SOmar Sandoval 					block_group->key.objectid,
1299a5ed9182SOmar Sandoval 					block_group->key.offset);
1300a5ed9182SOmar Sandoval }
1301a5ed9182SOmar Sandoval 
1302a5ed9182SOmar Sandoval int add_block_group_free_space(struct btrfs_trans_handle *trans,
1303a5ed9182SOmar Sandoval 			       struct btrfs_fs_info *fs_info,
1304a5ed9182SOmar Sandoval 			       struct btrfs_block_group_cache *block_group)
1305a5ed9182SOmar Sandoval {
1306a5ed9182SOmar Sandoval 	struct btrfs_path *path = NULL;
1307a5ed9182SOmar Sandoval 	int ret = 0;
1308a5ed9182SOmar Sandoval 
1309a5ed9182SOmar Sandoval 	if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1310a5ed9182SOmar Sandoval 		return 0;
1311a5ed9182SOmar Sandoval 
1312a5ed9182SOmar Sandoval 	mutex_lock(&block_group->free_space_lock);
1313a5ed9182SOmar Sandoval 	if (!block_group->needs_free_space)
1314a5ed9182SOmar Sandoval 		goto out;
1315a5ed9182SOmar Sandoval 
1316a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1317a5ed9182SOmar Sandoval 	if (!path) {
1318a5ed9182SOmar Sandoval 		ret = -ENOMEM;
1319a5ed9182SOmar Sandoval 		goto out;
1320a5ed9182SOmar Sandoval 	}
1321a5ed9182SOmar Sandoval 
1322a5ed9182SOmar Sandoval 	ret = __add_block_group_free_space(trans, fs_info, block_group, path);
1323a5ed9182SOmar Sandoval 
1324a5ed9182SOmar Sandoval out:
1325a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1326a5ed9182SOmar Sandoval 	mutex_unlock(&block_group->free_space_lock);
1327a5ed9182SOmar Sandoval 	if (ret)
132866642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1329a5ed9182SOmar Sandoval 	return ret;
1330a5ed9182SOmar Sandoval }
1331a5ed9182SOmar Sandoval 
1332a5ed9182SOmar Sandoval int remove_block_group_free_space(struct btrfs_trans_handle *trans,
1333a5ed9182SOmar Sandoval 				  struct btrfs_fs_info *fs_info,
1334a5ed9182SOmar Sandoval 				  struct btrfs_block_group_cache *block_group)
1335a5ed9182SOmar Sandoval {
1336a5ed9182SOmar Sandoval 	struct btrfs_root *root = fs_info->free_space_root;
1337a5ed9182SOmar Sandoval 	struct btrfs_path *path;
1338a5ed9182SOmar Sandoval 	struct btrfs_key key, found_key;
1339a5ed9182SOmar Sandoval 	struct extent_buffer *leaf;
1340a5ed9182SOmar Sandoval 	u64 start, end;
1341a5ed9182SOmar Sandoval 	int done = 0, nr;
1342a5ed9182SOmar Sandoval 	int ret;
1343a5ed9182SOmar Sandoval 
1344a5ed9182SOmar Sandoval 	if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1345a5ed9182SOmar Sandoval 		return 0;
1346a5ed9182SOmar Sandoval 
1347a5ed9182SOmar Sandoval 	if (block_group->needs_free_space) {
1348a5ed9182SOmar Sandoval 		/* We never added this block group to the free space tree. */
1349a5ed9182SOmar Sandoval 		return 0;
1350a5ed9182SOmar Sandoval 	}
1351a5ed9182SOmar Sandoval 
1352a5ed9182SOmar Sandoval 	path = btrfs_alloc_path();
1353a5ed9182SOmar Sandoval 	if (!path) {
1354a5ed9182SOmar Sandoval 		ret = -ENOMEM;
1355a5ed9182SOmar Sandoval 		goto out;
1356a5ed9182SOmar Sandoval 	}
1357a5ed9182SOmar Sandoval 
1358a5ed9182SOmar Sandoval 	start = block_group->key.objectid;
1359a5ed9182SOmar Sandoval 	end = block_group->key.objectid + block_group->key.offset;
1360a5ed9182SOmar Sandoval 
1361a5ed9182SOmar Sandoval 	key.objectid = end - 1;
1362a5ed9182SOmar Sandoval 	key.type = (u8)-1;
1363a5ed9182SOmar Sandoval 	key.offset = (u64)-1;
1364a5ed9182SOmar Sandoval 
1365a5ed9182SOmar Sandoval 	while (!done) {
1366a5ed9182SOmar Sandoval 		ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
1367a5ed9182SOmar Sandoval 		if (ret)
1368a5ed9182SOmar Sandoval 			goto out;
1369a5ed9182SOmar Sandoval 
1370a5ed9182SOmar Sandoval 		leaf = path->nodes[0];
1371a5ed9182SOmar Sandoval 		nr = 0;
1372a5ed9182SOmar Sandoval 		path->slots[0]++;
1373a5ed9182SOmar Sandoval 		while (path->slots[0] > 0) {
1374a5ed9182SOmar Sandoval 			btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
1375a5ed9182SOmar Sandoval 
1376a5ed9182SOmar Sandoval 			if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
1377a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid == block_group->key.objectid);
1378a5ed9182SOmar Sandoval 				ASSERT(found_key.offset == block_group->key.offset);
1379a5ed9182SOmar Sandoval 				done = 1;
1380a5ed9182SOmar Sandoval 				nr++;
1381a5ed9182SOmar Sandoval 				path->slots[0]--;
1382a5ed9182SOmar Sandoval 				break;
1383a5ed9182SOmar Sandoval 			} else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY ||
1384a5ed9182SOmar Sandoval 				   found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
1385a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid >= start);
1386a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid < end);
1387a5ed9182SOmar Sandoval 				ASSERT(found_key.objectid + found_key.offset <= end);
1388a5ed9182SOmar Sandoval 				nr++;
1389a5ed9182SOmar Sandoval 				path->slots[0]--;
1390a5ed9182SOmar Sandoval 			} else {
1391a5ed9182SOmar Sandoval 				ASSERT(0);
1392a5ed9182SOmar Sandoval 			}
1393a5ed9182SOmar Sandoval 		}
1394a5ed9182SOmar Sandoval 
1395a5ed9182SOmar Sandoval 		ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
1396a5ed9182SOmar Sandoval 		if (ret)
1397a5ed9182SOmar Sandoval 			goto out;
1398a5ed9182SOmar Sandoval 		btrfs_release_path(path);
1399a5ed9182SOmar Sandoval 	}
1400a5ed9182SOmar Sandoval 
1401a5ed9182SOmar Sandoval 	ret = 0;
1402a5ed9182SOmar Sandoval out:
1403a5ed9182SOmar Sandoval 	btrfs_free_path(path);
1404a5ed9182SOmar Sandoval 	if (ret)
140566642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1406a5ed9182SOmar Sandoval 	return ret;
1407a5ed9182SOmar Sandoval }
1408a5ed9182SOmar Sandoval 
1409a5ed9182SOmar Sandoval static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
1410a5ed9182SOmar Sandoval 				   struct btrfs_path *path,
1411a5ed9182SOmar Sandoval 				   u32 expected_extent_count)
1412a5ed9182SOmar Sandoval {
1413a5ed9182SOmar Sandoval 	struct btrfs_block_group_cache *block_group;
1414a5ed9182SOmar Sandoval 	struct btrfs_fs_info *fs_info;
1415a5ed9182SOmar Sandoval 	struct btrfs_root *root;
1416a5ed9182SOmar Sandoval 	struct btrfs_key key;
1417a5ed9182SOmar Sandoval 	int prev_bit = 0, bit;
1418a5ed9182SOmar Sandoval 	/* Initialize to silence GCC. */
1419a5ed9182SOmar Sandoval 	u64 extent_start = 0;
1420a5ed9182SOmar Sandoval 	u64 end, offset;
1421a5ed9182SOmar Sandoval 	u64 total_found = 0;
1422a5ed9182SOmar Sandoval 	u32 extent_count = 0;
1423a5ed9182SOmar Sandoval 	int ret;
1424a5ed9182SOmar Sandoval 
1425a5ed9182SOmar Sandoval 	block_group = caching_ctl->block_group;
1426a5ed9182SOmar Sandoval 	fs_info = block_group->fs_info;
1427a5ed9182SOmar Sandoval 	root = fs_info->free_space_root;
1428a5ed9182SOmar Sandoval 
1429a5ed9182SOmar Sandoval 	end = block_group->key.objectid + block_group->key.offset;
1430a5ed9182SOmar Sandoval 
1431a5ed9182SOmar Sandoval 	while (1) {
1432a5ed9182SOmar Sandoval 		ret = btrfs_next_item(root, path);
1433a5ed9182SOmar Sandoval 		if (ret < 0)
1434a5ed9182SOmar Sandoval 			goto out;
1435a5ed9182SOmar Sandoval 		if (ret)
1436a5ed9182SOmar Sandoval 			break;
1437a5ed9182SOmar Sandoval 
1438a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1439a5ed9182SOmar Sandoval 
1440a5ed9182SOmar Sandoval 		if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1441a5ed9182SOmar Sandoval 			break;
1442a5ed9182SOmar Sandoval 
1443a5ed9182SOmar Sandoval 		ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
1444a5ed9182SOmar Sandoval 		ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1445a5ed9182SOmar Sandoval 
1446a5ed9182SOmar Sandoval 		caching_ctl->progress = key.objectid;
1447a5ed9182SOmar Sandoval 
1448a5ed9182SOmar Sandoval 		offset = key.objectid;
1449a5ed9182SOmar Sandoval 		while (offset < key.objectid + key.offset) {
1450a5ed9182SOmar Sandoval 			bit = free_space_test_bit(block_group, path, offset);
1451a5ed9182SOmar Sandoval 			if (prev_bit == 0 && bit == 1) {
1452a5ed9182SOmar Sandoval 				extent_start = offset;
1453a5ed9182SOmar Sandoval 			} else if (prev_bit == 1 && bit == 0) {
1454a5ed9182SOmar Sandoval 				total_found += add_new_free_space(block_group,
1455a5ed9182SOmar Sandoval 								  fs_info,
1456a5ed9182SOmar Sandoval 								  extent_start,
1457a5ed9182SOmar Sandoval 								  offset);
1458a5ed9182SOmar Sandoval 				if (total_found > CACHING_CTL_WAKE_UP) {
1459a5ed9182SOmar Sandoval 					total_found = 0;
1460a5ed9182SOmar Sandoval 					wake_up(&caching_ctl->wait);
1461a5ed9182SOmar Sandoval 				}
1462a5ed9182SOmar Sandoval 				extent_count++;
1463a5ed9182SOmar Sandoval 			}
1464a5ed9182SOmar Sandoval 			prev_bit = bit;
14650b246afaSJeff Mahoney 			offset += fs_info->sectorsize;
1466a5ed9182SOmar Sandoval 		}
1467a5ed9182SOmar Sandoval 	}
1468a5ed9182SOmar Sandoval 	if (prev_bit == 1) {
1469a5ed9182SOmar Sandoval 		total_found += add_new_free_space(block_group, fs_info,
1470a5ed9182SOmar Sandoval 						  extent_start, end);
1471a5ed9182SOmar Sandoval 		extent_count++;
1472a5ed9182SOmar Sandoval 	}
1473a5ed9182SOmar Sandoval 
1474a5ed9182SOmar Sandoval 	if (extent_count != expected_extent_count) {
14755d163e0eSJeff Mahoney 		btrfs_err(fs_info,
14765d163e0eSJeff Mahoney 			  "incorrect extent count for %llu; counted %u, expected %u",
1477a5ed9182SOmar Sandoval 			  block_group->key.objectid, extent_count,
1478a5ed9182SOmar Sandoval 			  expected_extent_count);
1479a5ed9182SOmar Sandoval 		ASSERT(0);
1480a5ed9182SOmar Sandoval 		ret = -EIO;
1481a5ed9182SOmar Sandoval 		goto out;
1482a5ed9182SOmar Sandoval 	}
1483a5ed9182SOmar Sandoval 
1484a5ed9182SOmar Sandoval 	caching_ctl->progress = (u64)-1;
1485a5ed9182SOmar Sandoval 
1486a5ed9182SOmar Sandoval 	ret = 0;
1487a5ed9182SOmar Sandoval out:
1488a5ed9182SOmar Sandoval 	return ret;
1489a5ed9182SOmar Sandoval }
1490a5ed9182SOmar Sandoval 
1491a5ed9182SOmar Sandoval static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
1492a5ed9182SOmar Sandoval 				   struct btrfs_path *path,
1493a5ed9182SOmar Sandoval 				   u32 expected_extent_count)
1494a5ed9182SOmar Sandoval {
1495a5ed9182SOmar Sandoval 	struct btrfs_block_group_cache *block_group;
1496a5ed9182SOmar Sandoval 	struct btrfs_fs_info *fs_info;
1497a5ed9182SOmar Sandoval 	struct btrfs_root *root;
1498a5ed9182SOmar Sandoval 	struct btrfs_key key;
1499a5ed9182SOmar Sandoval 	u64 end;
1500a5ed9182SOmar Sandoval 	u64 total_found = 0;
1501a5ed9182SOmar Sandoval 	u32 extent_count = 0;
1502a5ed9182SOmar Sandoval 	int ret;
1503a5ed9182SOmar Sandoval 
1504a5ed9182SOmar Sandoval 	block_group = caching_ctl->block_group;
1505a5ed9182SOmar Sandoval 	fs_info = block_group->fs_info;
1506a5ed9182SOmar Sandoval 	root = fs_info->free_space_root;
1507a5ed9182SOmar Sandoval 
1508a5ed9182SOmar Sandoval 	end = block_group->key.objectid + block_group->key.offset;
1509a5ed9182SOmar Sandoval 
1510a5ed9182SOmar Sandoval 	while (1) {
1511a5ed9182SOmar Sandoval 		ret = btrfs_next_item(root, path);
1512a5ed9182SOmar Sandoval 		if (ret < 0)
1513a5ed9182SOmar Sandoval 			goto out;
1514a5ed9182SOmar Sandoval 		if (ret)
1515a5ed9182SOmar Sandoval 			break;
1516a5ed9182SOmar Sandoval 
1517a5ed9182SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1518a5ed9182SOmar Sandoval 
1519a5ed9182SOmar Sandoval 		if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1520a5ed9182SOmar Sandoval 			break;
1521a5ed9182SOmar Sandoval 
1522a5ed9182SOmar Sandoval 		ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
1523a5ed9182SOmar Sandoval 		ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1524a5ed9182SOmar Sandoval 
1525a5ed9182SOmar Sandoval 		caching_ctl->progress = key.objectid;
1526a5ed9182SOmar Sandoval 
1527a5ed9182SOmar Sandoval 		total_found += add_new_free_space(block_group, fs_info,
1528a5ed9182SOmar Sandoval 						  key.objectid,
1529a5ed9182SOmar Sandoval 						  key.objectid + key.offset);
1530a5ed9182SOmar Sandoval 		if (total_found > CACHING_CTL_WAKE_UP) {
1531a5ed9182SOmar Sandoval 			total_found = 0;
1532a5ed9182SOmar Sandoval 			wake_up(&caching_ctl->wait);
1533a5ed9182SOmar Sandoval 		}
1534a5ed9182SOmar Sandoval 		extent_count++;
1535a5ed9182SOmar Sandoval 	}
1536a5ed9182SOmar Sandoval 
1537a5ed9182SOmar Sandoval 	if (extent_count != expected_extent_count) {
15385d163e0eSJeff Mahoney 		btrfs_err(fs_info,
15395d163e0eSJeff Mahoney 			  "incorrect extent count for %llu; counted %u, expected %u",
1540a5ed9182SOmar Sandoval 			  block_group->key.objectid, extent_count,
1541a5ed9182SOmar Sandoval 			  expected_extent_count);
1542a5ed9182SOmar Sandoval 		ASSERT(0);
1543a5ed9182SOmar Sandoval 		ret = -EIO;
1544a5ed9182SOmar Sandoval 		goto out;
1545a5ed9182SOmar Sandoval 	}
1546a5ed9182SOmar Sandoval 
1547a5ed9182SOmar Sandoval 	caching_ctl->progress = (u64)-1;
1548a5ed9182SOmar Sandoval 
1549a5ed9182SOmar Sandoval 	ret = 0;
1550a5ed9182SOmar Sandoval out:
1551a5ed9182SOmar Sandoval 	return ret;
1552a5ed9182SOmar Sandoval }
1553a5ed9182SOmar Sandoval 
1554a5ed9182SOmar Sandoval int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
1555a5ed9182SOmar Sandoval {
1556a5ed9182SOmar Sandoval 	struct btrfs_block_group_cache *block_group;
1557a5ed9182SOmar Sandoval 	struct btrfs_fs_info *fs_info;
1558a5ed9182SOmar Sandoval 	struct btrfs_free_space_info *info;
1559a5ed9182SOmar Sandoval 	struct btrfs_path *path;
1560a5ed9182SOmar Sandoval 	u32 extent_count, flags;
1561a5ed9182SOmar Sandoval 	int ret;
1562a5ed9182SOmar Sandoval 
1563a5ed9182SOmar Sandoval 	block_group = caching_ctl->block_group;
1564a5ed9182SOmar Sandoval 	fs_info = block_group->fs_info;
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 
1578a5ed9182SOmar Sandoval 	info = search_free_space_info(NULL, fs_info, 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