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