xref: /openbmc/linux/fs/btrfs/tests/free-space-tests.c (revision 77d84ff87e9d38072abcca665ca22cb1da41cb86)
1dc11dd5dSJosef Bacik /*
2dc11dd5dSJosef Bacik  * Copyright (C) 2013 Fusion IO.  All rights reserved.
3dc11dd5dSJosef Bacik  *
4dc11dd5dSJosef Bacik  * This program is free software; you can redistribute it and/or
5dc11dd5dSJosef Bacik  * modify it under the terms of the GNU General Public
6dc11dd5dSJosef Bacik  * License v2 as published by the Free Software Foundation.
7dc11dd5dSJosef Bacik  *
8dc11dd5dSJosef Bacik  * This program is distributed in the hope that it will be useful,
9dc11dd5dSJosef Bacik  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10dc11dd5dSJosef Bacik  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11dc11dd5dSJosef Bacik  * General Public License for more details.
12dc11dd5dSJosef Bacik  *
13dc11dd5dSJosef Bacik  * You should have received a copy of the GNU General Public
14dc11dd5dSJosef Bacik  * License along with this program; if not, write to the
15dc11dd5dSJosef Bacik  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16dc11dd5dSJosef Bacik  * Boston, MA 021110-1307, USA.
17dc11dd5dSJosef Bacik  */
18dc11dd5dSJosef Bacik 
19dc11dd5dSJosef Bacik #include <linux/slab.h>
20dc11dd5dSJosef Bacik #include "btrfs-tests.h"
21dc11dd5dSJosef Bacik #include "../ctree.h"
22dc11dd5dSJosef Bacik #include "../free-space-cache.h"
23dc11dd5dSJosef Bacik 
24dc11dd5dSJosef Bacik #define BITS_PER_BITMAP		(PAGE_CACHE_SIZE * 8)
25dc11dd5dSJosef Bacik static struct btrfs_block_group_cache *init_test_block_group(void)
26dc11dd5dSJosef Bacik {
27dc11dd5dSJosef Bacik 	struct btrfs_block_group_cache *cache;
28dc11dd5dSJosef Bacik 
29dc11dd5dSJosef Bacik 	cache = kzalloc(sizeof(*cache), GFP_NOFS);
30dc11dd5dSJosef Bacik 	if (!cache)
31dc11dd5dSJosef Bacik 		return NULL;
32dc11dd5dSJosef Bacik 	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
33dc11dd5dSJosef Bacik 					GFP_NOFS);
34dc11dd5dSJosef Bacik 	if (!cache->free_space_ctl) {
35dc11dd5dSJosef Bacik 		kfree(cache);
36dc11dd5dSJosef Bacik 		return NULL;
37dc11dd5dSJosef Bacik 	}
38dc11dd5dSJosef Bacik 
39dc11dd5dSJosef Bacik 	cache->key.objectid = 0;
40dc11dd5dSJosef Bacik 	cache->key.offset = 1024 * 1024 * 1024;
41dc11dd5dSJosef Bacik 	cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
42dc11dd5dSJosef Bacik 	cache->sectorsize = 4096;
43dc11dd5dSJosef Bacik 
44dc11dd5dSJosef Bacik 	spin_lock_init(&cache->lock);
45dc11dd5dSJosef Bacik 	INIT_LIST_HEAD(&cache->list);
46dc11dd5dSJosef Bacik 	INIT_LIST_HEAD(&cache->cluster_list);
47dc11dd5dSJosef Bacik 	INIT_LIST_HEAD(&cache->new_bg_list);
48dc11dd5dSJosef Bacik 
49dc11dd5dSJosef Bacik 	btrfs_init_free_space_ctl(cache);
50dc11dd5dSJosef Bacik 
51dc11dd5dSJosef Bacik 	return cache;
52dc11dd5dSJosef Bacik }
53dc11dd5dSJosef Bacik 
54dc11dd5dSJosef Bacik /*
55dc11dd5dSJosef Bacik  * This test just does basic sanity checking, making sure we can add an exten
56dc11dd5dSJosef Bacik  * entry and remove space from either end and the middle, and make sure we can
57dc11dd5dSJosef Bacik  * remove space that covers adjacent extent entries.
58dc11dd5dSJosef Bacik  */
59dc11dd5dSJosef Bacik static int test_extents(struct btrfs_block_group_cache *cache)
60dc11dd5dSJosef Bacik {
61dc11dd5dSJosef Bacik 	int ret = 0;
62dc11dd5dSJosef Bacik 
63dc11dd5dSJosef Bacik 	test_msg("Running extent only tests\n");
64dc11dd5dSJosef Bacik 
65dc11dd5dSJosef Bacik 	/* First just make sure we can remove an entire entry */
66dc11dd5dSJosef Bacik 	ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024);
67dc11dd5dSJosef Bacik 	if (ret) {
68dc11dd5dSJosef Bacik 		test_msg("Error adding initial extents %d\n", ret);
69dc11dd5dSJosef Bacik 		return ret;
70dc11dd5dSJosef Bacik 	}
71dc11dd5dSJosef Bacik 
72dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024);
73dc11dd5dSJosef Bacik 	if (ret) {
74dc11dd5dSJosef Bacik 		test_msg("Error removing extent %d\n", ret);
75dc11dd5dSJosef Bacik 		return ret;
76dc11dd5dSJosef Bacik 	}
77dc11dd5dSJosef Bacik 
78dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 0, 4 * 1024 * 1024)) {
79dc11dd5dSJosef Bacik 		test_msg("Full remove left some lingering space\n");
80dc11dd5dSJosef Bacik 		return -1;
81dc11dd5dSJosef Bacik 	}
82dc11dd5dSJosef Bacik 
83dc11dd5dSJosef Bacik 	/* Ok edge and middle cases now */
84dc11dd5dSJosef Bacik 	ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024);
85dc11dd5dSJosef Bacik 	if (ret) {
86dc11dd5dSJosef Bacik 		test_msg("Error adding half extent %d\n", ret);
87dc11dd5dSJosef Bacik 		return ret;
88dc11dd5dSJosef Bacik 	}
89dc11dd5dSJosef Bacik 
90dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 1 * 1024 * 1024);
91dc11dd5dSJosef Bacik 	if (ret) {
92dc11dd5dSJosef Bacik 		test_msg("Error removing tail end %d\n", ret);
93dc11dd5dSJosef Bacik 		return ret;
94dc11dd5dSJosef Bacik 	}
95dc11dd5dSJosef Bacik 
96dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024);
97dc11dd5dSJosef Bacik 	if (ret) {
98dc11dd5dSJosef Bacik 		test_msg("Error removing front end %d\n", ret);
99dc11dd5dSJosef Bacik 		return ret;
100dc11dd5dSJosef Bacik 	}
101dc11dd5dSJosef Bacik 
102dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 2 * 1024 * 1024, 4096);
103dc11dd5dSJosef Bacik 	if (ret) {
104*77d84ff8SMasanari Iida 		test_msg("Error removing middle piece %d\n", ret);
105dc11dd5dSJosef Bacik 		return ret;
106dc11dd5dSJosef Bacik 	}
107dc11dd5dSJosef Bacik 
108dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 0, 1 * 1024 * 1024)) {
109dc11dd5dSJosef Bacik 		test_msg("Still have space at the front\n");
110dc11dd5dSJosef Bacik 		return -1;
111dc11dd5dSJosef Bacik 	}
112dc11dd5dSJosef Bacik 
113dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 2 * 1024 * 1024, 4096)) {
114dc11dd5dSJosef Bacik 		test_msg("Still have space in the middle\n");
115dc11dd5dSJosef Bacik 		return -1;
116dc11dd5dSJosef Bacik 	}
117dc11dd5dSJosef Bacik 
118dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 3 * 1024 * 1024, 1 * 1024 * 1024)) {
119dc11dd5dSJosef Bacik 		test_msg("Still have space at the end\n");
120dc11dd5dSJosef Bacik 		return -1;
121dc11dd5dSJosef Bacik 	}
122dc11dd5dSJosef Bacik 
123dc11dd5dSJosef Bacik 	/* Cleanup */
124dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
125dc11dd5dSJosef Bacik 
126dc11dd5dSJosef Bacik 	return 0;
127dc11dd5dSJosef Bacik }
128dc11dd5dSJosef Bacik 
129dc11dd5dSJosef Bacik static int test_bitmaps(struct btrfs_block_group_cache *cache)
130dc11dd5dSJosef Bacik {
131dc11dd5dSJosef Bacik 	u64 next_bitmap_offset;
132dc11dd5dSJosef Bacik 	int ret;
133dc11dd5dSJosef Bacik 
134dc11dd5dSJosef Bacik 	test_msg("Running bitmap only tests\n");
135dc11dd5dSJosef Bacik 
136dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1);
137dc11dd5dSJosef Bacik 	if (ret) {
138dc11dd5dSJosef Bacik 		test_msg("Couldn't create a bitmap entry %d\n", ret);
139dc11dd5dSJosef Bacik 		return ret;
140dc11dd5dSJosef Bacik 	}
141dc11dd5dSJosef Bacik 
142dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024);
143dc11dd5dSJosef Bacik 	if (ret) {
144dc11dd5dSJosef Bacik 		test_msg("Error removing bitmap full range %d\n", ret);
145dc11dd5dSJosef Bacik 		return ret;
146dc11dd5dSJosef Bacik 	}
147dc11dd5dSJosef Bacik 
148dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 0, 4 * 1024 * 1024)) {
149dc11dd5dSJosef Bacik 		test_msg("Left some space in bitmap\n");
150dc11dd5dSJosef Bacik 		return -1;
151dc11dd5dSJosef Bacik 	}
152dc11dd5dSJosef Bacik 
153dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1);
154dc11dd5dSJosef Bacik 	if (ret) {
155dc11dd5dSJosef Bacik 		test_msg("Couldn't add to our bitmap entry %d\n", ret);
156dc11dd5dSJosef Bacik 		return ret;
157dc11dd5dSJosef Bacik 	}
158dc11dd5dSJosef Bacik 
159dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 2 * 1024 * 1024);
160dc11dd5dSJosef Bacik 	if (ret) {
161dc11dd5dSJosef Bacik 		test_msg("Couldn't remove middle chunk %d\n", ret);
162dc11dd5dSJosef Bacik 		return ret;
163dc11dd5dSJosef Bacik 	}
164dc11dd5dSJosef Bacik 
165dc11dd5dSJosef Bacik 	/*
166dc11dd5dSJosef Bacik 	 * The first bitmap we have starts at offset 0 so the next one is just
167dc11dd5dSJosef Bacik 	 * at the end of the first bitmap.
168dc11dd5dSJosef Bacik 	 */
169dc11dd5dSJosef Bacik 	next_bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
170dc11dd5dSJosef Bacik 
171dc11dd5dSJosef Bacik 	/* Test a bit straddling two bitmaps */
172dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, next_bitmap_offset -
173dc11dd5dSJosef Bacik 				   (2 * 1024 * 1024), 4 * 1024 * 1024, 1);
174dc11dd5dSJosef Bacik 	if (ret) {
175dc11dd5dSJosef Bacik 		test_msg("Couldn't add space that straddles two bitmaps %d\n",
176dc11dd5dSJosef Bacik 				ret);
177dc11dd5dSJosef Bacik 		return ret;
178dc11dd5dSJosef Bacik 	}
179dc11dd5dSJosef Bacik 
180dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, next_bitmap_offset -
181dc11dd5dSJosef Bacik 				      (1 * 1024 * 1024), 2 * 1024 * 1024);
182dc11dd5dSJosef Bacik 	if (ret) {
183dc11dd5dSJosef Bacik 		test_msg("Couldn't remove overlapping space %d\n", ret);
184dc11dd5dSJosef Bacik 		return ret;
185dc11dd5dSJosef Bacik 	}
186dc11dd5dSJosef Bacik 
187dc11dd5dSJosef Bacik 	if (test_check_exists(cache, next_bitmap_offset - (1 * 1024 * 1024),
188dc11dd5dSJosef Bacik 			 2 * 1024 * 1024)) {
189dc11dd5dSJosef Bacik 		test_msg("Left some space when removing overlapping\n");
190dc11dd5dSJosef Bacik 		return -1;
191dc11dd5dSJosef Bacik 	}
192dc11dd5dSJosef Bacik 
193dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
194dc11dd5dSJosef Bacik 
195dc11dd5dSJosef Bacik 	return 0;
196dc11dd5dSJosef Bacik }
197dc11dd5dSJosef Bacik 
198dc11dd5dSJosef Bacik /* This is the high grade jackassery */
199dc11dd5dSJosef Bacik static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
200dc11dd5dSJosef Bacik {
201dc11dd5dSJosef Bacik 	u64 bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
202dc11dd5dSJosef Bacik 	int ret;
203dc11dd5dSJosef Bacik 
204dc11dd5dSJosef Bacik 	test_msg("Running bitmap and extent tests\n");
205dc11dd5dSJosef Bacik 
206dc11dd5dSJosef Bacik 	/*
207dc11dd5dSJosef Bacik 	 * First let's do something simple, an extent at the same offset as the
208dc11dd5dSJosef Bacik 	 * bitmap, but the free space completely in the extent and then
209dc11dd5dSJosef Bacik 	 * completely in the bitmap.
210dc11dd5dSJosef Bacik 	 */
211dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 4 * 1024 * 1024, 1 * 1024 * 1024, 1);
212dc11dd5dSJosef Bacik 	if (ret) {
213dc11dd5dSJosef Bacik 		test_msg("Couldn't create bitmap entry %d\n", ret);
214dc11dd5dSJosef Bacik 		return ret;
215dc11dd5dSJosef Bacik 	}
216dc11dd5dSJosef Bacik 
217dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0);
218dc11dd5dSJosef Bacik 	if (ret) {
219dc11dd5dSJosef Bacik 		test_msg("Couldn't add extent entry %d\n", ret);
220dc11dd5dSJosef Bacik 		return ret;
221dc11dd5dSJosef Bacik 	}
222dc11dd5dSJosef Bacik 
223dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024);
224dc11dd5dSJosef Bacik 	if (ret) {
225dc11dd5dSJosef Bacik 		test_msg("Couldn't remove extent entry %d\n", ret);
226dc11dd5dSJosef Bacik 		return ret;
227dc11dd5dSJosef Bacik 	}
228dc11dd5dSJosef Bacik 
229dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 0, 1 * 1024 * 1024)) {
230dc11dd5dSJosef Bacik 		test_msg("Left remnants after our remove\n");
231dc11dd5dSJosef Bacik 		return -1;
232dc11dd5dSJosef Bacik 	}
233dc11dd5dSJosef Bacik 
234dc11dd5dSJosef Bacik 	/* Now to add back the extent entry and remove from the bitmap */
235dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0);
236dc11dd5dSJosef Bacik 	if (ret) {
237dc11dd5dSJosef Bacik 		test_msg("Couldn't re-add extent entry %d\n", ret);
238dc11dd5dSJosef Bacik 		return ret;
239dc11dd5dSJosef Bacik 	}
240dc11dd5dSJosef Bacik 
241dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 4 * 1024 * 1024, 1 * 1024 * 1024);
242dc11dd5dSJosef Bacik 	if (ret) {
243dc11dd5dSJosef Bacik 		test_msg("Couldn't remove from bitmap %d\n", ret);
244dc11dd5dSJosef Bacik 		return ret;
245dc11dd5dSJosef Bacik 	}
246dc11dd5dSJosef Bacik 
247dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 4 * 1024 * 1024, 1 * 1024 * 1024)) {
248dc11dd5dSJosef Bacik 		test_msg("Left remnants in the bitmap\n");
249dc11dd5dSJosef Bacik 		return -1;
250dc11dd5dSJosef Bacik 	}
251dc11dd5dSJosef Bacik 
252dc11dd5dSJosef Bacik 	/*
253dc11dd5dSJosef Bacik 	 * Ok so a little more evil, extent entry and bitmap at the same offset,
254dc11dd5dSJosef Bacik 	 * removing an overlapping chunk.
255dc11dd5dSJosef Bacik 	 */
256dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 1 * 1024 * 1024, 4 * 1024 * 1024, 1);
257dc11dd5dSJosef Bacik 	if (ret) {
258dc11dd5dSJosef Bacik 		test_msg("Couldn't add to a bitmap %d\n", ret);
259dc11dd5dSJosef Bacik 		return ret;
260dc11dd5dSJosef Bacik 	}
261dc11dd5dSJosef Bacik 
262dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 512 * 1024, 3 * 1024 * 1024);
263dc11dd5dSJosef Bacik 	if (ret) {
264dc11dd5dSJosef Bacik 		test_msg("Couldn't remove overlapping space %d\n", ret);
265dc11dd5dSJosef Bacik 		return ret;
266dc11dd5dSJosef Bacik 	}
267dc11dd5dSJosef Bacik 
268dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 512 * 1024, 3 * 1024 * 1024)) {
269dc11dd5dSJosef Bacik 		test_msg("Left over peices after removing overlapping\n");
270dc11dd5dSJosef Bacik 		return -1;
271dc11dd5dSJosef Bacik 	}
272dc11dd5dSJosef Bacik 
273dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
274dc11dd5dSJosef Bacik 
275dc11dd5dSJosef Bacik 	/* Now with the extent entry offset into the bitmap */
276dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 4 * 1024 * 1024, 4 * 1024 * 1024, 1);
277dc11dd5dSJosef Bacik 	if (ret) {
278dc11dd5dSJosef Bacik 		test_msg("Couldn't add space to the bitmap %d\n", ret);
279dc11dd5dSJosef Bacik 		return ret;
280dc11dd5dSJosef Bacik 	}
281dc11dd5dSJosef Bacik 
282dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 2 * 1024 * 1024, 2 * 1024 * 1024, 0);
283dc11dd5dSJosef Bacik 	if (ret) {
284dc11dd5dSJosef Bacik 		test_msg("Couldn't add extent to the cache %d\n", ret);
285dc11dd5dSJosef Bacik 		return ret;
286dc11dd5dSJosef Bacik 	}
287dc11dd5dSJosef Bacik 
288dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 4 * 1024 * 1024);
289dc11dd5dSJosef Bacik 	if (ret) {
290dc11dd5dSJosef Bacik 		test_msg("Problem removing overlapping space %d\n", ret);
291dc11dd5dSJosef Bacik 		return ret;
292dc11dd5dSJosef Bacik 	}
293dc11dd5dSJosef Bacik 
294dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 3 * 1024 * 1024, 4 * 1024 * 1024)) {
295dc11dd5dSJosef Bacik 		test_msg("Left something behind when removing space");
296dc11dd5dSJosef Bacik 		return -1;
297dc11dd5dSJosef Bacik 	}
298dc11dd5dSJosef Bacik 
299dc11dd5dSJosef Bacik 	/*
300dc11dd5dSJosef Bacik 	 * This has blown up in the past, the extent entry starts before the
301dc11dd5dSJosef Bacik 	 * bitmap entry, but we're trying to remove an offset that falls
302dc11dd5dSJosef Bacik 	 * completely within the bitmap range and is in both the extent entry
303dc11dd5dSJosef Bacik 	 * and the bitmap entry, looks like this
304dc11dd5dSJosef Bacik 	 *
305dc11dd5dSJosef Bacik 	 *   [ extent ]
306dc11dd5dSJosef Bacik 	 *      [ bitmap ]
307dc11dd5dSJosef Bacik 	 *        [ del ]
308dc11dd5dSJosef Bacik 	 */
309dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
310dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, bitmap_offset + 4 * 1024 * 1024,
311dc11dd5dSJosef Bacik 				   4 * 1024 * 1024, 1);
312dc11dd5dSJosef Bacik 	if (ret) {
313dc11dd5dSJosef Bacik 		test_msg("Couldn't add bitmap %d\n", ret);
314dc11dd5dSJosef Bacik 		return ret;
315dc11dd5dSJosef Bacik 	}
316dc11dd5dSJosef Bacik 
317dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, bitmap_offset - 1 * 1024 * 1024,
318dc11dd5dSJosef Bacik 				   5 * 1024 * 1024, 0);
319dc11dd5dSJosef Bacik 	if (ret) {
320dc11dd5dSJosef Bacik 		test_msg("Couldn't add extent entry %d\n", ret);
321dc11dd5dSJosef Bacik 		return ret;
322dc11dd5dSJosef Bacik 	}
323dc11dd5dSJosef Bacik 
324dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, bitmap_offset + 1 * 1024 * 1024,
325dc11dd5dSJosef Bacik 				      5 * 1024 * 1024);
326dc11dd5dSJosef Bacik 	if (ret) {
327dc11dd5dSJosef Bacik 		test_msg("Failed to free our space %d\n", ret);
328dc11dd5dSJosef Bacik 		return ret;
329dc11dd5dSJosef Bacik 	}
330dc11dd5dSJosef Bacik 
331dc11dd5dSJosef Bacik 	if (test_check_exists(cache, bitmap_offset + 1 * 1024 * 1024,
332dc11dd5dSJosef Bacik 			 5 * 1024 * 1024)) {
333dc11dd5dSJosef Bacik 		test_msg("Left stuff over\n");
334dc11dd5dSJosef Bacik 		return -1;
335dc11dd5dSJosef Bacik 	}
336dc11dd5dSJosef Bacik 
337dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
338dc11dd5dSJosef Bacik 
339dc11dd5dSJosef Bacik 	/*
340dc11dd5dSJosef Bacik 	 * This blew up before, we have part of the free space in a bitmap and
341dc11dd5dSJosef Bacik 	 * then the entirety of the rest of the space in an extent.  This used
342dc11dd5dSJosef Bacik 	 * to return -EAGAIN back from btrfs_remove_extent, make sure this
343dc11dd5dSJosef Bacik 	 * doesn't happen.
344dc11dd5dSJosef Bacik 	 */
345dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 1 * 1024 * 1024, 2 * 1024 * 1024, 1);
346dc11dd5dSJosef Bacik 	if (ret) {
347dc11dd5dSJosef Bacik 		test_msg("Couldn't add bitmap entry %d\n", ret);
348dc11dd5dSJosef Bacik 		return ret;
349dc11dd5dSJosef Bacik 	}
350dc11dd5dSJosef Bacik 
351dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 3 * 1024 * 1024, 1 * 1024 * 1024, 0);
352dc11dd5dSJosef Bacik 	if (ret) {
353dc11dd5dSJosef Bacik 		test_msg("Couldn't add extent entry %d\n", ret);
354dc11dd5dSJosef Bacik 		return ret;
355dc11dd5dSJosef Bacik 	}
356dc11dd5dSJosef Bacik 
357dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 3 * 1024 * 1024);
358dc11dd5dSJosef Bacik 	if (ret) {
359dc11dd5dSJosef Bacik 		test_msg("Error removing bitmap and extent overlapping %d\n", ret);
360dc11dd5dSJosef Bacik 		return ret;
361dc11dd5dSJosef Bacik 	}
362dc11dd5dSJosef Bacik 
363dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
364dc11dd5dSJosef Bacik 	return 0;
365dc11dd5dSJosef Bacik }
366dc11dd5dSJosef Bacik 
367dc11dd5dSJosef Bacik int btrfs_test_free_space_cache(void)
368dc11dd5dSJosef Bacik {
369dc11dd5dSJosef Bacik 	struct btrfs_block_group_cache *cache;
370dc11dd5dSJosef Bacik 	int ret;
371dc11dd5dSJosef Bacik 
372dc11dd5dSJosef Bacik 	test_msg("Running btrfs free space cache tests\n");
373dc11dd5dSJosef Bacik 
374dc11dd5dSJosef Bacik 	cache = init_test_block_group();
375dc11dd5dSJosef Bacik 	if (!cache) {
376dc11dd5dSJosef Bacik 		test_msg("Couldn't run the tests\n");
377dc11dd5dSJosef Bacik 		return 0;
378dc11dd5dSJosef Bacik 	}
379dc11dd5dSJosef Bacik 
380dc11dd5dSJosef Bacik 	ret = test_extents(cache);
381dc11dd5dSJosef Bacik 	if (ret)
382dc11dd5dSJosef Bacik 		goto out;
383dc11dd5dSJosef Bacik 	ret = test_bitmaps(cache);
384dc11dd5dSJosef Bacik 	if (ret)
385dc11dd5dSJosef Bacik 		goto out;
386dc11dd5dSJosef Bacik 	ret = test_bitmaps_and_extents(cache);
387dc11dd5dSJosef Bacik 	if (ret)
388dc11dd5dSJosef Bacik 		goto out;
389dc11dd5dSJosef Bacik out:
390dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
391dc11dd5dSJosef Bacik 	kfree(cache->free_space_ctl);
392dc11dd5dSJosef Bacik 	kfree(cache);
393dc11dd5dSJosef Bacik 	test_msg("Free space cache tests finished\n");
394dc11dd5dSJosef Bacik 	return ret;
395dc11dd5dSJosef Bacik }
396