xref: /openbmc/linux/fs/btrfs/tests/free-space-tests.c (revision 28f0779a3fd6ef015303780f0b9a92b24728bc4b)
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"
22d0bd4560SJosef Bacik #include "../disk-io.h"
23dc11dd5dSJosef Bacik #include "../free-space-cache.h"
24dc11dd5dSJosef Bacik 
25dc11dd5dSJosef Bacik #define BITS_PER_BITMAP		(PAGE_CACHE_SIZE * 8)
26dc11dd5dSJosef Bacik static struct btrfs_block_group_cache *init_test_block_group(void)
27dc11dd5dSJosef Bacik {
28dc11dd5dSJosef Bacik 	struct btrfs_block_group_cache *cache;
29dc11dd5dSJosef Bacik 
30dc11dd5dSJosef Bacik 	cache = kzalloc(sizeof(*cache), GFP_NOFS);
31dc11dd5dSJosef Bacik 	if (!cache)
32dc11dd5dSJosef Bacik 		return NULL;
33dc11dd5dSJosef Bacik 	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
34dc11dd5dSJosef Bacik 					GFP_NOFS);
35dc11dd5dSJosef Bacik 	if (!cache->free_space_ctl) {
36dc11dd5dSJosef Bacik 		kfree(cache);
37dc11dd5dSJosef Bacik 		return NULL;
38dc11dd5dSJosef Bacik 	}
39d0bd4560SJosef Bacik 	cache->fs_info = btrfs_alloc_dummy_fs_info();
40d0bd4560SJosef Bacik 	if (!cache->fs_info) {
41d0bd4560SJosef Bacik 		kfree(cache->free_space_ctl);
42d0bd4560SJosef Bacik 		kfree(cache);
43d0bd4560SJosef Bacik 		return NULL;
44d0bd4560SJosef Bacik 	}
45dc11dd5dSJosef Bacik 
46dc11dd5dSJosef Bacik 	cache->key.objectid = 0;
47dc11dd5dSJosef Bacik 	cache->key.offset = 1024 * 1024 * 1024;
48dc11dd5dSJosef Bacik 	cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
49dc11dd5dSJosef Bacik 	cache->sectorsize = 4096;
5020005523SFilipe Manana 	cache->full_stripe_len = 4096;
51dc11dd5dSJosef Bacik 
52dc11dd5dSJosef Bacik 	spin_lock_init(&cache->lock);
53dc11dd5dSJosef Bacik 	INIT_LIST_HEAD(&cache->list);
54dc11dd5dSJosef Bacik 	INIT_LIST_HEAD(&cache->cluster_list);
5547ab2a6cSJosef Bacik 	INIT_LIST_HEAD(&cache->bg_list);
56dc11dd5dSJosef Bacik 
57dc11dd5dSJosef Bacik 	btrfs_init_free_space_ctl(cache);
58dc11dd5dSJosef Bacik 
59dc11dd5dSJosef Bacik 	return cache;
60dc11dd5dSJosef Bacik }
61dc11dd5dSJosef Bacik 
62dc11dd5dSJosef Bacik /*
63dc11dd5dSJosef Bacik  * This test just does basic sanity checking, making sure we can add an exten
64dc11dd5dSJosef Bacik  * entry and remove space from either end and the middle, and make sure we can
65dc11dd5dSJosef Bacik  * remove space that covers adjacent extent entries.
66dc11dd5dSJosef Bacik  */
67dc11dd5dSJosef Bacik static int test_extents(struct btrfs_block_group_cache *cache)
68dc11dd5dSJosef Bacik {
69dc11dd5dSJosef Bacik 	int ret = 0;
70dc11dd5dSJosef Bacik 
71dc11dd5dSJosef Bacik 	test_msg("Running extent only tests\n");
72dc11dd5dSJosef Bacik 
73dc11dd5dSJosef Bacik 	/* First just make sure we can remove an entire entry */
74dc11dd5dSJosef Bacik 	ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024);
75dc11dd5dSJosef Bacik 	if (ret) {
76dc11dd5dSJosef Bacik 		test_msg("Error adding initial extents %d\n", ret);
77dc11dd5dSJosef Bacik 		return ret;
78dc11dd5dSJosef Bacik 	}
79dc11dd5dSJosef Bacik 
80dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024);
81dc11dd5dSJosef Bacik 	if (ret) {
82dc11dd5dSJosef Bacik 		test_msg("Error removing extent %d\n", ret);
83dc11dd5dSJosef Bacik 		return ret;
84dc11dd5dSJosef Bacik 	}
85dc11dd5dSJosef Bacik 
86dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 0, 4 * 1024 * 1024)) {
87dc11dd5dSJosef Bacik 		test_msg("Full remove left some lingering space\n");
88dc11dd5dSJosef Bacik 		return -1;
89dc11dd5dSJosef Bacik 	}
90dc11dd5dSJosef Bacik 
91dc11dd5dSJosef Bacik 	/* Ok edge and middle cases now */
92dc11dd5dSJosef Bacik 	ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024);
93dc11dd5dSJosef Bacik 	if (ret) {
94dc11dd5dSJosef Bacik 		test_msg("Error adding half extent %d\n", ret);
95dc11dd5dSJosef Bacik 		return ret;
96dc11dd5dSJosef Bacik 	}
97dc11dd5dSJosef Bacik 
98dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 1 * 1024 * 1024);
99dc11dd5dSJosef Bacik 	if (ret) {
100dc11dd5dSJosef Bacik 		test_msg("Error removing tail end %d\n", ret);
101dc11dd5dSJosef Bacik 		return ret;
102dc11dd5dSJosef Bacik 	}
103dc11dd5dSJosef Bacik 
104dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024);
105dc11dd5dSJosef Bacik 	if (ret) {
106dc11dd5dSJosef Bacik 		test_msg("Error removing front end %d\n", ret);
107dc11dd5dSJosef Bacik 		return ret;
108dc11dd5dSJosef Bacik 	}
109dc11dd5dSJosef Bacik 
110dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 2 * 1024 * 1024, 4096);
111dc11dd5dSJosef Bacik 	if (ret) {
11277d84ff8SMasanari Iida 		test_msg("Error removing middle piece %d\n", ret);
113dc11dd5dSJosef Bacik 		return ret;
114dc11dd5dSJosef Bacik 	}
115dc11dd5dSJosef Bacik 
116dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 0, 1 * 1024 * 1024)) {
117dc11dd5dSJosef Bacik 		test_msg("Still have space at the front\n");
118dc11dd5dSJosef Bacik 		return -1;
119dc11dd5dSJosef Bacik 	}
120dc11dd5dSJosef Bacik 
121dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 2 * 1024 * 1024, 4096)) {
122dc11dd5dSJosef Bacik 		test_msg("Still have space in the middle\n");
123dc11dd5dSJosef Bacik 		return -1;
124dc11dd5dSJosef Bacik 	}
125dc11dd5dSJosef Bacik 
126dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 3 * 1024 * 1024, 1 * 1024 * 1024)) {
127dc11dd5dSJosef Bacik 		test_msg("Still have space at the end\n");
128dc11dd5dSJosef Bacik 		return -1;
129dc11dd5dSJosef Bacik 	}
130dc11dd5dSJosef Bacik 
131dc11dd5dSJosef Bacik 	/* Cleanup */
132dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
133dc11dd5dSJosef Bacik 
134dc11dd5dSJosef Bacik 	return 0;
135dc11dd5dSJosef Bacik }
136dc11dd5dSJosef Bacik 
137dc11dd5dSJosef Bacik static int test_bitmaps(struct btrfs_block_group_cache *cache)
138dc11dd5dSJosef Bacik {
139dc11dd5dSJosef Bacik 	u64 next_bitmap_offset;
140dc11dd5dSJosef Bacik 	int ret;
141dc11dd5dSJosef Bacik 
142dc11dd5dSJosef Bacik 	test_msg("Running bitmap only tests\n");
143dc11dd5dSJosef Bacik 
144dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1);
145dc11dd5dSJosef Bacik 	if (ret) {
146dc11dd5dSJosef Bacik 		test_msg("Couldn't create a bitmap entry %d\n", ret);
147dc11dd5dSJosef Bacik 		return ret;
148dc11dd5dSJosef Bacik 	}
149dc11dd5dSJosef Bacik 
150dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024);
151dc11dd5dSJosef Bacik 	if (ret) {
152dc11dd5dSJosef Bacik 		test_msg("Error removing bitmap full range %d\n", ret);
153dc11dd5dSJosef Bacik 		return ret;
154dc11dd5dSJosef Bacik 	}
155dc11dd5dSJosef Bacik 
156dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 0, 4 * 1024 * 1024)) {
157dc11dd5dSJosef Bacik 		test_msg("Left some space in bitmap\n");
158dc11dd5dSJosef Bacik 		return -1;
159dc11dd5dSJosef Bacik 	}
160dc11dd5dSJosef Bacik 
161dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1);
162dc11dd5dSJosef Bacik 	if (ret) {
163dc11dd5dSJosef Bacik 		test_msg("Couldn't add to our bitmap entry %d\n", ret);
164dc11dd5dSJosef Bacik 		return ret;
165dc11dd5dSJosef Bacik 	}
166dc11dd5dSJosef Bacik 
167dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 2 * 1024 * 1024);
168dc11dd5dSJosef Bacik 	if (ret) {
169dc11dd5dSJosef Bacik 		test_msg("Couldn't remove middle chunk %d\n", ret);
170dc11dd5dSJosef Bacik 		return ret;
171dc11dd5dSJosef Bacik 	}
172dc11dd5dSJosef Bacik 
173dc11dd5dSJosef Bacik 	/*
174dc11dd5dSJosef Bacik 	 * The first bitmap we have starts at offset 0 so the next one is just
175dc11dd5dSJosef Bacik 	 * at the end of the first bitmap.
176dc11dd5dSJosef Bacik 	 */
177dc11dd5dSJosef Bacik 	next_bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
178dc11dd5dSJosef Bacik 
179dc11dd5dSJosef Bacik 	/* Test a bit straddling two bitmaps */
180dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, next_bitmap_offset -
181dc11dd5dSJosef Bacik 				   (2 * 1024 * 1024), 4 * 1024 * 1024, 1);
182dc11dd5dSJosef Bacik 	if (ret) {
183dc11dd5dSJosef Bacik 		test_msg("Couldn't add space that straddles two bitmaps %d\n",
184dc11dd5dSJosef Bacik 				ret);
185dc11dd5dSJosef Bacik 		return ret;
186dc11dd5dSJosef Bacik 	}
187dc11dd5dSJosef Bacik 
188dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, next_bitmap_offset -
189dc11dd5dSJosef Bacik 				      (1 * 1024 * 1024), 2 * 1024 * 1024);
190dc11dd5dSJosef Bacik 	if (ret) {
191dc11dd5dSJosef Bacik 		test_msg("Couldn't remove overlapping space %d\n", ret);
192dc11dd5dSJosef Bacik 		return ret;
193dc11dd5dSJosef Bacik 	}
194dc11dd5dSJosef Bacik 
195dc11dd5dSJosef Bacik 	if (test_check_exists(cache, next_bitmap_offset - (1 * 1024 * 1024),
196dc11dd5dSJosef Bacik 			 2 * 1024 * 1024)) {
197dc11dd5dSJosef Bacik 		test_msg("Left some space when removing overlapping\n");
198dc11dd5dSJosef Bacik 		return -1;
199dc11dd5dSJosef Bacik 	}
200dc11dd5dSJosef Bacik 
201dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
202dc11dd5dSJosef Bacik 
203dc11dd5dSJosef Bacik 	return 0;
204dc11dd5dSJosef Bacik }
205dc11dd5dSJosef Bacik 
206dc11dd5dSJosef Bacik /* This is the high grade jackassery */
207dc11dd5dSJosef Bacik static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
208dc11dd5dSJosef Bacik {
209dc11dd5dSJosef Bacik 	u64 bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
210dc11dd5dSJosef Bacik 	int ret;
211dc11dd5dSJosef Bacik 
212dc11dd5dSJosef Bacik 	test_msg("Running bitmap and extent tests\n");
213dc11dd5dSJosef Bacik 
214dc11dd5dSJosef Bacik 	/*
215dc11dd5dSJosef Bacik 	 * First let's do something simple, an extent at the same offset as the
216dc11dd5dSJosef Bacik 	 * bitmap, but the free space completely in the extent and then
217dc11dd5dSJosef Bacik 	 * completely in the bitmap.
218dc11dd5dSJosef Bacik 	 */
219dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 4 * 1024 * 1024, 1 * 1024 * 1024, 1);
220dc11dd5dSJosef Bacik 	if (ret) {
221dc11dd5dSJosef Bacik 		test_msg("Couldn't create bitmap entry %d\n", ret);
222dc11dd5dSJosef Bacik 		return ret;
223dc11dd5dSJosef Bacik 	}
224dc11dd5dSJosef Bacik 
225dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0);
226dc11dd5dSJosef Bacik 	if (ret) {
227dc11dd5dSJosef Bacik 		test_msg("Couldn't add extent entry %d\n", ret);
228dc11dd5dSJosef Bacik 		return ret;
229dc11dd5dSJosef Bacik 	}
230dc11dd5dSJosef Bacik 
231dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024);
232dc11dd5dSJosef Bacik 	if (ret) {
233dc11dd5dSJosef Bacik 		test_msg("Couldn't remove extent entry %d\n", ret);
234dc11dd5dSJosef Bacik 		return ret;
235dc11dd5dSJosef Bacik 	}
236dc11dd5dSJosef Bacik 
237dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 0, 1 * 1024 * 1024)) {
238dc11dd5dSJosef Bacik 		test_msg("Left remnants after our remove\n");
239dc11dd5dSJosef Bacik 		return -1;
240dc11dd5dSJosef Bacik 	}
241dc11dd5dSJosef Bacik 
242dc11dd5dSJosef Bacik 	/* Now to add back the extent entry and remove from the bitmap */
243dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0);
244dc11dd5dSJosef Bacik 	if (ret) {
245dc11dd5dSJosef Bacik 		test_msg("Couldn't re-add extent entry %d\n", ret);
246dc11dd5dSJosef Bacik 		return ret;
247dc11dd5dSJosef Bacik 	}
248dc11dd5dSJosef Bacik 
249dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 4 * 1024 * 1024, 1 * 1024 * 1024);
250dc11dd5dSJosef Bacik 	if (ret) {
251dc11dd5dSJosef Bacik 		test_msg("Couldn't remove from bitmap %d\n", ret);
252dc11dd5dSJosef Bacik 		return ret;
253dc11dd5dSJosef Bacik 	}
254dc11dd5dSJosef Bacik 
255dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 4 * 1024 * 1024, 1 * 1024 * 1024)) {
256dc11dd5dSJosef Bacik 		test_msg("Left remnants in the bitmap\n");
257dc11dd5dSJosef Bacik 		return -1;
258dc11dd5dSJosef Bacik 	}
259dc11dd5dSJosef Bacik 
260dc11dd5dSJosef Bacik 	/*
261dc11dd5dSJosef Bacik 	 * Ok so a little more evil, extent entry and bitmap at the same offset,
262dc11dd5dSJosef Bacik 	 * removing an overlapping chunk.
263dc11dd5dSJosef Bacik 	 */
264dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 1 * 1024 * 1024, 4 * 1024 * 1024, 1);
265dc11dd5dSJosef Bacik 	if (ret) {
266dc11dd5dSJosef Bacik 		test_msg("Couldn't add to a bitmap %d\n", ret);
267dc11dd5dSJosef Bacik 		return ret;
268dc11dd5dSJosef Bacik 	}
269dc11dd5dSJosef Bacik 
270dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 512 * 1024, 3 * 1024 * 1024);
271dc11dd5dSJosef Bacik 	if (ret) {
272dc11dd5dSJosef Bacik 		test_msg("Couldn't remove overlapping space %d\n", ret);
273dc11dd5dSJosef Bacik 		return ret;
274dc11dd5dSJosef Bacik 	}
275dc11dd5dSJosef Bacik 
276dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 512 * 1024, 3 * 1024 * 1024)) {
2778faaaeadSMasanari Iida 		test_msg("Left over pieces after removing overlapping\n");
278dc11dd5dSJosef Bacik 		return -1;
279dc11dd5dSJosef Bacik 	}
280dc11dd5dSJosef Bacik 
281dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
282dc11dd5dSJosef Bacik 
283dc11dd5dSJosef Bacik 	/* Now with the extent entry offset into the bitmap */
284dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 4 * 1024 * 1024, 4 * 1024 * 1024, 1);
285dc11dd5dSJosef Bacik 	if (ret) {
286dc11dd5dSJosef Bacik 		test_msg("Couldn't add space to the bitmap %d\n", ret);
287dc11dd5dSJosef Bacik 		return ret;
288dc11dd5dSJosef Bacik 	}
289dc11dd5dSJosef Bacik 
290dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 2 * 1024 * 1024, 2 * 1024 * 1024, 0);
291dc11dd5dSJosef Bacik 	if (ret) {
292dc11dd5dSJosef Bacik 		test_msg("Couldn't add extent to the cache %d\n", ret);
293dc11dd5dSJosef Bacik 		return ret;
294dc11dd5dSJosef Bacik 	}
295dc11dd5dSJosef Bacik 
296dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 4 * 1024 * 1024);
297dc11dd5dSJosef Bacik 	if (ret) {
298dc11dd5dSJosef Bacik 		test_msg("Problem removing overlapping space %d\n", ret);
299dc11dd5dSJosef Bacik 		return ret;
300dc11dd5dSJosef Bacik 	}
301dc11dd5dSJosef Bacik 
302dc11dd5dSJosef Bacik 	if (test_check_exists(cache, 3 * 1024 * 1024, 4 * 1024 * 1024)) {
303dc11dd5dSJosef Bacik 		test_msg("Left something behind when removing space");
304dc11dd5dSJosef Bacik 		return -1;
305dc11dd5dSJosef Bacik 	}
306dc11dd5dSJosef Bacik 
307dc11dd5dSJosef Bacik 	/*
308dc11dd5dSJosef Bacik 	 * This has blown up in the past, the extent entry starts before the
309dc11dd5dSJosef Bacik 	 * bitmap entry, but we're trying to remove an offset that falls
310dc11dd5dSJosef Bacik 	 * completely within the bitmap range and is in both the extent entry
311dc11dd5dSJosef Bacik 	 * and the bitmap entry, looks like this
312dc11dd5dSJosef Bacik 	 *
313dc11dd5dSJosef Bacik 	 *   [ extent ]
314dc11dd5dSJosef Bacik 	 *      [ bitmap ]
315dc11dd5dSJosef Bacik 	 *        [ del ]
316dc11dd5dSJosef Bacik 	 */
317dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
318dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, bitmap_offset + 4 * 1024 * 1024,
319dc11dd5dSJosef Bacik 				   4 * 1024 * 1024, 1);
320dc11dd5dSJosef Bacik 	if (ret) {
321dc11dd5dSJosef Bacik 		test_msg("Couldn't add bitmap %d\n", ret);
322dc11dd5dSJosef Bacik 		return ret;
323dc11dd5dSJosef Bacik 	}
324dc11dd5dSJosef Bacik 
325dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, bitmap_offset - 1 * 1024 * 1024,
326dc11dd5dSJosef Bacik 				   5 * 1024 * 1024, 0);
327dc11dd5dSJosef Bacik 	if (ret) {
328dc11dd5dSJosef Bacik 		test_msg("Couldn't add extent entry %d\n", ret);
329dc11dd5dSJosef Bacik 		return ret;
330dc11dd5dSJosef Bacik 	}
331dc11dd5dSJosef Bacik 
332dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, bitmap_offset + 1 * 1024 * 1024,
333dc11dd5dSJosef Bacik 				      5 * 1024 * 1024);
334dc11dd5dSJosef Bacik 	if (ret) {
335dc11dd5dSJosef Bacik 		test_msg("Failed to free our space %d\n", ret);
336dc11dd5dSJosef Bacik 		return ret;
337dc11dd5dSJosef Bacik 	}
338dc11dd5dSJosef Bacik 
339dc11dd5dSJosef Bacik 	if (test_check_exists(cache, bitmap_offset + 1 * 1024 * 1024,
340dc11dd5dSJosef Bacik 			 5 * 1024 * 1024)) {
341dc11dd5dSJosef Bacik 		test_msg("Left stuff over\n");
342dc11dd5dSJosef Bacik 		return -1;
343dc11dd5dSJosef Bacik 	}
344dc11dd5dSJosef Bacik 
345dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
346dc11dd5dSJosef Bacik 
347dc11dd5dSJosef Bacik 	/*
348dc11dd5dSJosef Bacik 	 * This blew up before, we have part of the free space in a bitmap and
349dc11dd5dSJosef Bacik 	 * then the entirety of the rest of the space in an extent.  This used
350dc11dd5dSJosef Bacik 	 * to return -EAGAIN back from btrfs_remove_extent, make sure this
351dc11dd5dSJosef Bacik 	 * doesn't happen.
352dc11dd5dSJosef Bacik 	 */
353dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 1 * 1024 * 1024, 2 * 1024 * 1024, 1);
354dc11dd5dSJosef Bacik 	if (ret) {
355dc11dd5dSJosef Bacik 		test_msg("Couldn't add bitmap entry %d\n", ret);
356dc11dd5dSJosef Bacik 		return ret;
357dc11dd5dSJosef Bacik 	}
358dc11dd5dSJosef Bacik 
359dc11dd5dSJosef Bacik 	ret = test_add_free_space_entry(cache, 3 * 1024 * 1024, 1 * 1024 * 1024, 0);
360dc11dd5dSJosef Bacik 	if (ret) {
361dc11dd5dSJosef Bacik 		test_msg("Couldn't add extent entry %d\n", ret);
362dc11dd5dSJosef Bacik 		return ret;
363dc11dd5dSJosef Bacik 	}
364dc11dd5dSJosef Bacik 
365dc11dd5dSJosef Bacik 	ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 3 * 1024 * 1024);
366dc11dd5dSJosef Bacik 	if (ret) {
367dc11dd5dSJosef Bacik 		test_msg("Error removing bitmap and extent overlapping %d\n", ret);
368dc11dd5dSJosef Bacik 		return ret;
369dc11dd5dSJosef Bacik 	}
370dc11dd5dSJosef Bacik 
371dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
372dc11dd5dSJosef Bacik 	return 0;
373dc11dd5dSJosef Bacik }
374dc11dd5dSJosef Bacik 
37520005523SFilipe Manana /* Used by test_steal_space_from_bitmap_to_extent(). */
37620005523SFilipe Manana static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl,
37720005523SFilipe Manana 			    struct btrfs_free_space *info)
37820005523SFilipe Manana {
37920005523SFilipe Manana 	return ctl->free_extents > 0;
38020005523SFilipe Manana }
38120005523SFilipe Manana 
38220005523SFilipe Manana /* Used by test_steal_space_from_bitmap_to_extent(). */
38320005523SFilipe Manana static int
38420005523SFilipe Manana check_num_extents_and_bitmaps(const struct btrfs_block_group_cache *cache,
38520005523SFilipe Manana 			      const int num_extents,
38620005523SFilipe Manana 			      const int num_bitmaps)
38720005523SFilipe Manana {
38820005523SFilipe Manana 	if (cache->free_space_ctl->free_extents != num_extents) {
38920005523SFilipe Manana 		test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
39020005523SFilipe Manana 			 cache->free_space_ctl->free_extents, num_extents);
39120005523SFilipe Manana 		return -EINVAL;
39220005523SFilipe Manana 	}
39320005523SFilipe Manana 	if (cache->free_space_ctl->total_bitmaps != num_bitmaps) {
39420005523SFilipe Manana 		test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
39520005523SFilipe Manana 			 cache->free_space_ctl->total_bitmaps, num_bitmaps);
39620005523SFilipe Manana 		return -EINVAL;
39720005523SFilipe Manana 	}
39820005523SFilipe Manana 	return 0;
39920005523SFilipe Manana }
40020005523SFilipe Manana 
40120005523SFilipe Manana /* Used by test_steal_space_from_bitmap_to_extent(). */
40220005523SFilipe Manana static int check_cache_empty(struct btrfs_block_group_cache *cache)
40320005523SFilipe Manana {
40420005523SFilipe Manana 	u64 offset;
40520005523SFilipe Manana 	u64 max_extent_size;
40620005523SFilipe Manana 
40720005523SFilipe Manana 	/*
40820005523SFilipe Manana 	 * Now lets confirm that there's absolutely no free space left to
40920005523SFilipe Manana 	 * allocate.
41020005523SFilipe Manana 	 */
41120005523SFilipe Manana 	if (cache->free_space_ctl->free_space != 0) {
41220005523SFilipe Manana 		test_msg("Cache free space is not 0\n");
41320005523SFilipe Manana 		return -EINVAL;
41420005523SFilipe Manana 	}
41520005523SFilipe Manana 
41620005523SFilipe Manana 	/* And any allocation request, no matter how small, should fail now. */
41720005523SFilipe Manana 	offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0,
41820005523SFilipe Manana 					    &max_extent_size);
41920005523SFilipe Manana 	if (offset != 0) {
42020005523SFilipe Manana 		test_msg("Space allocation did not fail, returned offset: %llu",
42120005523SFilipe Manana 			 offset);
42220005523SFilipe Manana 		return -EINVAL;
42320005523SFilipe Manana 	}
42420005523SFilipe Manana 
42520005523SFilipe Manana 	/* And no extent nor bitmap entries in the cache anymore. */
42620005523SFilipe Manana 	return check_num_extents_and_bitmaps(cache, 0, 0);
42720005523SFilipe Manana }
42820005523SFilipe Manana 
42920005523SFilipe Manana /*
43020005523SFilipe Manana  * Before we were able to steal free space from a bitmap entry to an extent
43120005523SFilipe Manana  * entry, we could end up with 2 entries representing a contiguous free space.
43220005523SFilipe Manana  * One would be an extent entry and the other a bitmap entry. Since in order
43320005523SFilipe Manana  * to allocate space to a caller we use only 1 entry, we couldn't return that
43420005523SFilipe Manana  * whole range to the caller if it was requested. This forced the caller to
43520005523SFilipe Manana  * either assume ENOSPC or perform several smaller space allocations, which
43620005523SFilipe Manana  * wasn't optimal as they could be spread all over the block group while under
43720005523SFilipe Manana  * concurrency (extra overhead and fragmentation).
43820005523SFilipe Manana  *
43920005523SFilipe Manana  * This stealing approach is benefical, since we always prefer to allocate from
44020005523SFilipe Manana  * extent entries, both for clustered and non-clustered allocation requests.
44120005523SFilipe Manana  */
44220005523SFilipe Manana static int
44320005523SFilipe Manana test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
44420005523SFilipe Manana {
44520005523SFilipe Manana 	int ret;
44620005523SFilipe Manana 	u64 offset;
44720005523SFilipe Manana 	u64 max_extent_size;
448*28f0779aSDavid Sterba 	struct btrfs_free_space_op test_free_space_ops = {
449*28f0779aSDavid Sterba 		.recalc_thresholds = cache->free_space_ctl->op->recalc_thresholds,
450*28f0779aSDavid Sterba 		.use_bitmap = test_use_bitmap,
451*28f0779aSDavid Sterba 	};
452*28f0779aSDavid Sterba 	struct btrfs_free_space_op *orig_free_space_ops;
45320005523SFilipe Manana 
45420005523SFilipe Manana 	test_msg("Running space stealing from bitmap to extent\n");
45520005523SFilipe Manana 
45620005523SFilipe Manana 	/*
45720005523SFilipe Manana 	 * For this test, we want to ensure we end up with an extent entry
45820005523SFilipe Manana 	 * immediately adjacent to a bitmap entry, where the bitmap starts
45920005523SFilipe Manana 	 * at an offset where the extent entry ends. We keep adding and
46020005523SFilipe Manana 	 * removing free space to reach into this state, but to get there
46120005523SFilipe Manana 	 * we need to reach a point where marking new free space doesn't
46220005523SFilipe Manana 	 * result in adding new extent entries or merging the new space
46320005523SFilipe Manana 	 * with existing extent entries - the space ends up being marked
46420005523SFilipe Manana 	 * in an existing bitmap that covers the new free space range.
46520005523SFilipe Manana 	 *
46620005523SFilipe Manana 	 * To get there, we need to reach the threshold defined set at
46720005523SFilipe Manana 	 * cache->free_space_ctl->extents_thresh, which currently is
46820005523SFilipe Manana 	 * 256 extents on a x86_64 system at least, and a few other
46920005523SFilipe Manana 	 * conditions (check free_space_cache.c). Instead of making the
47020005523SFilipe Manana 	 * test much longer and complicated, use a "use_bitmap" operation
47120005523SFilipe Manana 	 * that forces use of bitmaps as soon as we have at least 1
47220005523SFilipe Manana 	 * extent entry.
47320005523SFilipe Manana 	 */
474*28f0779aSDavid Sterba 	orig_free_space_ops = cache->free_space_ctl->op;
475*28f0779aSDavid Sterba 	cache->free_space_ctl->op = &test_free_space_ops;
47620005523SFilipe Manana 
47720005523SFilipe Manana 	/*
47820005523SFilipe Manana 	 * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
47920005523SFilipe Manana 	 */
48020005523SFilipe Manana 	ret = test_add_free_space_entry(cache, 128 * 1024 * 1024 - 256 * 1024,
48120005523SFilipe Manana 					128 * 1024, 0);
48220005523SFilipe Manana 	if (ret) {
48320005523SFilipe Manana 		test_msg("Couldn't add extent entry %d\n", ret);
48420005523SFilipe Manana 		return ret;
48520005523SFilipe Manana 	}
48620005523SFilipe Manana 
48720005523SFilipe Manana 	/* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
48820005523SFilipe Manana 	ret = test_add_free_space_entry(cache, 128 * 1024 * 1024 + 512 * 1024,
48920005523SFilipe Manana 					128 * 1024 * 1024 - 512 * 1024, 1);
49020005523SFilipe Manana 	if (ret) {
49120005523SFilipe Manana 		test_msg("Couldn't add bitmap entry %d\n", ret);
49220005523SFilipe Manana 		return ret;
49320005523SFilipe Manana 	}
49420005523SFilipe Manana 
49520005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
49620005523SFilipe Manana 	if (ret)
49720005523SFilipe Manana 		return ret;
49820005523SFilipe Manana 
49920005523SFilipe Manana 	/*
50020005523SFilipe Manana 	 * Now make only the first 256Kb of the bitmap marked as free, so that
50120005523SFilipe Manana 	 * we end up with only the following ranges marked as free space:
50220005523SFilipe Manana 	 *
50320005523SFilipe Manana 	 * [128Mb - 256Kb, 128Mb - 128Kb[
50420005523SFilipe Manana 	 * [128Mb + 512Kb, 128Mb + 768Kb[
50520005523SFilipe Manana 	 */
50620005523SFilipe Manana 	ret = btrfs_remove_free_space(cache,
50720005523SFilipe Manana 				      128 * 1024 * 1024 + 768 * 1024,
50820005523SFilipe Manana 				      128 * 1024 * 1024 - 768 * 1024);
50920005523SFilipe Manana 	if (ret) {
51020005523SFilipe Manana 		test_msg("Failed to free part of bitmap space %d\n", ret);
51120005523SFilipe Manana 		return ret;
51220005523SFilipe Manana 	}
51320005523SFilipe Manana 
51420005523SFilipe Manana 	/* Confirm that only those 2 ranges are marked as free. */
51520005523SFilipe Manana 	if (!test_check_exists(cache, 128 * 1024 * 1024 - 256 * 1024,
51620005523SFilipe Manana 			       128 * 1024)) {
51720005523SFilipe Manana 		test_msg("Free space range missing\n");
51820005523SFilipe Manana 		return -ENOENT;
51920005523SFilipe Manana 	}
52020005523SFilipe Manana 	if (!test_check_exists(cache, 128 * 1024 * 1024 + 512 * 1024,
52120005523SFilipe Manana 			       256 * 1024)) {
52220005523SFilipe Manana 		test_msg("Free space range missing\n");
52320005523SFilipe Manana 		return -ENOENT;
52420005523SFilipe Manana 	}
52520005523SFilipe Manana 
52620005523SFilipe Manana 	/*
52720005523SFilipe Manana 	 * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
52820005523SFilipe Manana 	 * as free anymore.
52920005523SFilipe Manana 	 */
53020005523SFilipe Manana 	if (test_check_exists(cache, 128 * 1024 * 1024 + 768 * 1024,
53120005523SFilipe Manana 			      128 * 1024 * 1024 - 768 * 1024)) {
53220005523SFilipe Manana 		test_msg("Bitmap region not removed from space cache\n");
53320005523SFilipe Manana 		return -EINVAL;
53420005523SFilipe Manana 	}
53520005523SFilipe Manana 
53620005523SFilipe Manana 	/*
53720005523SFilipe Manana 	 * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
53820005523SFilipe Manana 	 * covered by the bitmap, isn't marked as free.
53920005523SFilipe Manana 	 */
54020005523SFilipe Manana 	if (test_check_exists(cache, 128 * 1024 * 1024 + 256 * 1024,
54120005523SFilipe Manana 			      256 * 1024)) {
54220005523SFilipe Manana 		test_msg("Invalid bitmap region marked as free\n");
54320005523SFilipe Manana 		return -EINVAL;
54420005523SFilipe Manana 	}
54520005523SFilipe Manana 
54620005523SFilipe Manana 	/*
54720005523SFilipe Manana 	 * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
54820005523SFilipe Manana 	 * by the bitmap too, isn't marked as free either.
54920005523SFilipe Manana 	 */
55020005523SFilipe Manana 	if (test_check_exists(cache, 128 * 1024 * 1024,
55120005523SFilipe Manana 			      256 * 1024)) {
55220005523SFilipe Manana 		test_msg("Invalid bitmap region marked as free\n");
55320005523SFilipe Manana 		return -EINVAL;
55420005523SFilipe Manana 	}
55520005523SFilipe Manana 
55620005523SFilipe Manana 	/*
55720005523SFilipe Manana 	 * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
55820005523SFilipe Manana 	 * lets make sure the free space cache marks it as free in the bitmap,
55920005523SFilipe Manana 	 * and doesn't insert a new extent entry to represent this region.
56020005523SFilipe Manana 	 */
56120005523SFilipe Manana 	ret = btrfs_add_free_space(cache, 128 * 1024 * 1024, 512 * 1024);
56220005523SFilipe Manana 	if (ret) {
56320005523SFilipe Manana 		test_msg("Error adding free space: %d\n", ret);
56420005523SFilipe Manana 		return ret;
56520005523SFilipe Manana 	}
56620005523SFilipe Manana 	/* Confirm the region is marked as free. */
56720005523SFilipe Manana 	if (!test_check_exists(cache, 128 * 1024 * 1024, 512 * 1024)) {
56820005523SFilipe Manana 		test_msg("Bitmap region not marked as free\n");
56920005523SFilipe Manana 		return -ENOENT;
57020005523SFilipe Manana 	}
57120005523SFilipe Manana 
57220005523SFilipe Manana 	/*
57320005523SFilipe Manana 	 * Confirm that no new extent entries or bitmap entries were added to
57420005523SFilipe Manana 	 * the cache after adding that free space region.
57520005523SFilipe Manana 	 */
57620005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
57720005523SFilipe Manana 	if (ret)
57820005523SFilipe Manana 		return ret;
57920005523SFilipe Manana 
58020005523SFilipe Manana 	/*
58120005523SFilipe Manana 	 * Now lets add a small free space region to the right of the previous
58220005523SFilipe Manana 	 * one, which is not contiguous with it and is part of the bitmap too.
58320005523SFilipe Manana 	 * The goal is to test that the bitmap entry space stealing doesn't
58420005523SFilipe Manana 	 * steal this space region.
58520005523SFilipe Manana 	 */
58620005523SFilipe Manana 	ret = btrfs_add_free_space(cache, 128 * 1024 * 1024 + 16 * 1024 * 1024,
58720005523SFilipe Manana 				   4096);
58820005523SFilipe Manana 	if (ret) {
58920005523SFilipe Manana 		test_msg("Error adding free space: %d\n", ret);
59020005523SFilipe Manana 		return ret;
59120005523SFilipe Manana 	}
59220005523SFilipe Manana 
59320005523SFilipe Manana 	/*
59420005523SFilipe Manana 	 * Confirm that no new extent entries or bitmap entries were added to
59520005523SFilipe Manana 	 * the cache after adding that free space region.
59620005523SFilipe Manana 	 */
59720005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
59820005523SFilipe Manana 	if (ret)
59920005523SFilipe Manana 		return ret;
60020005523SFilipe Manana 
60120005523SFilipe Manana 	/*
60220005523SFilipe Manana 	 * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
60320005523SFilipe Manana 	 * expand the range covered by the existing extent entry that represents
60420005523SFilipe Manana 	 * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
60520005523SFilipe Manana 	 */
60620005523SFilipe Manana 	ret = btrfs_add_free_space(cache, 128 * 1024 * 1024 - 128 * 1024,
60720005523SFilipe Manana 				   128 * 1024);
60820005523SFilipe Manana 	if (ret) {
60920005523SFilipe Manana 		test_msg("Error adding free space: %d\n", ret);
61020005523SFilipe Manana 		return ret;
61120005523SFilipe Manana 	}
61220005523SFilipe Manana 	/* Confirm the region is marked as free. */
61320005523SFilipe Manana 	if (!test_check_exists(cache, 128 * 1024 * 1024 - 128 * 1024,
61420005523SFilipe Manana 			       128 * 1024)) {
61520005523SFilipe Manana 		test_msg("Extent region not marked as free\n");
61620005523SFilipe Manana 		return -ENOENT;
61720005523SFilipe Manana 	}
61820005523SFilipe Manana 
61920005523SFilipe Manana 	/*
62020005523SFilipe Manana 	 * Confirm that our extent entry didn't stole all free space from the
62120005523SFilipe Manana 	 * bitmap, because of the small 4Kb free space region.
62220005523SFilipe Manana 	 */
62320005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
62420005523SFilipe Manana 	if (ret)
62520005523SFilipe Manana 		return ret;
62620005523SFilipe Manana 
62720005523SFilipe Manana 	/*
62820005523SFilipe Manana 	 * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
62920005523SFilipe Manana 	 * space. Without stealing bitmap free space into extent entry space,
63020005523SFilipe Manana 	 * we would have all this free space represented by 2 entries in the
63120005523SFilipe Manana 	 * cache:
63220005523SFilipe Manana 	 *
63320005523SFilipe Manana 	 * extent entry covering range: [128Mb - 256Kb, 128Mb[
63420005523SFilipe Manana 	 * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
63520005523SFilipe Manana 	 *
63620005523SFilipe Manana 	 * Attempting to allocate the whole free space (1Mb) would fail, because
63720005523SFilipe Manana 	 * we can't allocate from multiple entries.
63820005523SFilipe Manana 	 * With the bitmap free space stealing, we get a single extent entry
63920005523SFilipe Manana 	 * that represents the 1Mb free space, and therefore we're able to
64020005523SFilipe Manana 	 * allocate the whole free space at once.
64120005523SFilipe Manana 	 */
64220005523SFilipe Manana 	if (!test_check_exists(cache, 128 * 1024 * 1024 - 256 * 1024,
64320005523SFilipe Manana 			       1 * 1024 * 1024)) {
64420005523SFilipe Manana 		test_msg("Expected region not marked as free\n");
64520005523SFilipe Manana 		return -ENOENT;
64620005523SFilipe Manana 	}
64720005523SFilipe Manana 
64820005523SFilipe Manana 	if (cache->free_space_ctl->free_space != (1 * 1024 * 1024 + 4096)) {
64920005523SFilipe Manana 		test_msg("Cache free space is not 1Mb + 4Kb\n");
65020005523SFilipe Manana 		return -EINVAL;
65120005523SFilipe Manana 	}
65220005523SFilipe Manana 
65320005523SFilipe Manana 	offset = btrfs_find_space_for_alloc(cache,
65420005523SFilipe Manana 					    0, 1 * 1024 * 1024, 0,
65520005523SFilipe Manana 					    &max_extent_size);
65620005523SFilipe Manana 	if (offset != (128 * 1024 * 1024 - 256 * 1024)) {
65720005523SFilipe Manana 		test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
65820005523SFilipe Manana 			 offset);
65920005523SFilipe Manana 		return -EINVAL;
66020005523SFilipe Manana 	}
66120005523SFilipe Manana 
66220005523SFilipe Manana 	/* All that remains is a 4Kb free space region in a bitmap. Confirm. */
66320005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 1, 1);
66420005523SFilipe Manana 	if (ret)
66520005523SFilipe Manana 		return ret;
66620005523SFilipe Manana 
66720005523SFilipe Manana 	if (cache->free_space_ctl->free_space != 4096) {
66820005523SFilipe Manana 		test_msg("Cache free space is not 4Kb\n");
66920005523SFilipe Manana 		return -EINVAL;
67020005523SFilipe Manana 	}
67120005523SFilipe Manana 
67220005523SFilipe Manana 	offset = btrfs_find_space_for_alloc(cache,
67320005523SFilipe Manana 					    0, 4096, 0,
67420005523SFilipe Manana 					    &max_extent_size);
67520005523SFilipe Manana 	if (offset != (128 * 1024 * 1024 + 16 * 1024 * 1024)) {
67620005523SFilipe Manana 		test_msg("Failed to allocate 4Kb from space cache, returned offset is: %llu\n",
67720005523SFilipe Manana 			 offset);
67820005523SFilipe Manana 		return -EINVAL;
67920005523SFilipe Manana 	}
68020005523SFilipe Manana 
68120005523SFilipe Manana 	ret = check_cache_empty(cache);
68220005523SFilipe Manana 	if (ret)
68320005523SFilipe Manana 		return ret;
68420005523SFilipe Manana 
68520005523SFilipe Manana 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
68620005523SFilipe Manana 
68720005523SFilipe Manana 	/*
68820005523SFilipe Manana 	 * Now test a similar scenario, but where our extent entry is located
68920005523SFilipe Manana 	 * to the right of the bitmap entry, so that we can check that stealing
69020005523SFilipe Manana 	 * space from a bitmap to the front of an extent entry works.
69120005523SFilipe Manana 	 */
69220005523SFilipe Manana 
69320005523SFilipe Manana 	/*
69420005523SFilipe Manana 	 * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
69520005523SFilipe Manana 	 */
69620005523SFilipe Manana 	ret = test_add_free_space_entry(cache, 128 * 1024 * 1024 + 128 * 1024,
69720005523SFilipe Manana 					128 * 1024, 0);
69820005523SFilipe Manana 	if (ret) {
69920005523SFilipe Manana 		test_msg("Couldn't add extent entry %d\n", ret);
70020005523SFilipe Manana 		return ret;
70120005523SFilipe Manana 	}
70220005523SFilipe Manana 
70320005523SFilipe Manana 	/* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
70420005523SFilipe Manana 	ret = test_add_free_space_entry(cache, 0,
70520005523SFilipe Manana 					128 * 1024 * 1024 - 512 * 1024, 1);
70620005523SFilipe Manana 	if (ret) {
70720005523SFilipe Manana 		test_msg("Couldn't add bitmap entry %d\n", ret);
70820005523SFilipe Manana 		return ret;
70920005523SFilipe Manana 	}
71020005523SFilipe Manana 
71120005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
71220005523SFilipe Manana 	if (ret)
71320005523SFilipe Manana 		return ret;
71420005523SFilipe Manana 
71520005523SFilipe Manana 	/*
71620005523SFilipe Manana 	 * Now make only the last 256Kb of the bitmap marked as free, so that
71720005523SFilipe Manana 	 * we end up with only the following ranges marked as free space:
71820005523SFilipe Manana 	 *
71920005523SFilipe Manana 	 * [128Mb + 128b, 128Mb + 256Kb[
72020005523SFilipe Manana 	 * [128Mb - 768Kb, 128Mb - 512Kb[
72120005523SFilipe Manana 	 */
72220005523SFilipe Manana 	ret = btrfs_remove_free_space(cache,
72320005523SFilipe Manana 				      0,
72420005523SFilipe Manana 				      128 * 1024 * 1024 - 768 * 1024);
72520005523SFilipe Manana 	if (ret) {
72620005523SFilipe Manana 		test_msg("Failed to free part of bitmap space %d\n", ret);
72720005523SFilipe Manana 		return ret;
72820005523SFilipe Manana 	}
72920005523SFilipe Manana 
73020005523SFilipe Manana 	/* Confirm that only those 2 ranges are marked as free. */
73120005523SFilipe Manana 	if (!test_check_exists(cache, 128 * 1024 * 1024 + 128 * 1024,
73220005523SFilipe Manana 			       128 * 1024)) {
73320005523SFilipe Manana 		test_msg("Free space range missing\n");
73420005523SFilipe Manana 		return -ENOENT;
73520005523SFilipe Manana 	}
73620005523SFilipe Manana 	if (!test_check_exists(cache, 128 * 1024 * 1024 - 768 * 1024,
73720005523SFilipe Manana 			       256 * 1024)) {
73820005523SFilipe Manana 		test_msg("Free space range missing\n");
73920005523SFilipe Manana 		return -ENOENT;
74020005523SFilipe Manana 	}
74120005523SFilipe Manana 
74220005523SFilipe Manana 	/*
74320005523SFilipe Manana 	 * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
74420005523SFilipe Manana 	 * as free anymore.
74520005523SFilipe Manana 	 */
74620005523SFilipe Manana 	if (test_check_exists(cache, 0,
74720005523SFilipe Manana 			      128 * 1024 * 1024 - 768 * 1024)) {
74820005523SFilipe Manana 		test_msg("Bitmap region not removed from space cache\n");
74920005523SFilipe Manana 		return -EINVAL;
75020005523SFilipe Manana 	}
75120005523SFilipe Manana 
75220005523SFilipe Manana 	/*
75320005523SFilipe Manana 	 * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
75420005523SFilipe Manana 	 * covered by the bitmap, isn't marked as free.
75520005523SFilipe Manana 	 */
75620005523SFilipe Manana 	if (test_check_exists(cache, 128 * 1024 * 1024 - 512 * 1024,
75720005523SFilipe Manana 			      512 * 1024)) {
75820005523SFilipe Manana 		test_msg("Invalid bitmap region marked as free\n");
75920005523SFilipe Manana 		return -EINVAL;
76020005523SFilipe Manana 	}
76120005523SFilipe Manana 
76220005523SFilipe Manana 	/*
76320005523SFilipe Manana 	 * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
76420005523SFilipe Manana 	 * lets make sure the free space cache marks it as free in the bitmap,
76520005523SFilipe Manana 	 * and doesn't insert a new extent entry to represent this region.
76620005523SFilipe Manana 	 */
76720005523SFilipe Manana 	ret = btrfs_add_free_space(cache, 128 * 1024 * 1024 - 512 * 1024,
76820005523SFilipe Manana 				   512 * 1024);
76920005523SFilipe Manana 	if (ret) {
77020005523SFilipe Manana 		test_msg("Error adding free space: %d\n", ret);
77120005523SFilipe Manana 		return ret;
77220005523SFilipe Manana 	}
77320005523SFilipe Manana 	/* Confirm the region is marked as free. */
77420005523SFilipe Manana 	if (!test_check_exists(cache, 128 * 1024 * 1024 - 512 * 1024,
77520005523SFilipe Manana 			       512 * 1024)) {
77620005523SFilipe Manana 		test_msg("Bitmap region not marked as free\n");
77720005523SFilipe Manana 		return -ENOENT;
77820005523SFilipe Manana 	}
77920005523SFilipe Manana 
78020005523SFilipe Manana 	/*
78120005523SFilipe Manana 	 * Confirm that no new extent entries or bitmap entries were added to
78220005523SFilipe Manana 	 * the cache after adding that free space region.
78320005523SFilipe Manana 	 */
78420005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
78520005523SFilipe Manana 	if (ret)
78620005523SFilipe Manana 		return ret;
78720005523SFilipe Manana 
78820005523SFilipe Manana 	/*
78920005523SFilipe Manana 	 * Now lets add a small free space region to the left of the previous
79020005523SFilipe Manana 	 * one, which is not contiguous with it and is part of the bitmap too.
79120005523SFilipe Manana 	 * The goal is to test that the bitmap entry space stealing doesn't
79220005523SFilipe Manana 	 * steal this space region.
79320005523SFilipe Manana 	 */
79420005523SFilipe Manana 	ret = btrfs_add_free_space(cache, 32 * 1024 * 1024, 8192);
79520005523SFilipe Manana 	if (ret) {
79620005523SFilipe Manana 		test_msg("Error adding free space: %d\n", ret);
79720005523SFilipe Manana 		return ret;
79820005523SFilipe Manana 	}
79920005523SFilipe Manana 
80020005523SFilipe Manana 	/*
80120005523SFilipe Manana 	 * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
80220005523SFilipe Manana 	 * expand the range covered by the existing extent entry that represents
80320005523SFilipe Manana 	 * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
80420005523SFilipe Manana 	 */
80520005523SFilipe Manana 	ret = btrfs_add_free_space(cache, 128 * 1024 * 1024, 128 * 1024);
80620005523SFilipe Manana 	if (ret) {
80720005523SFilipe Manana 		test_msg("Error adding free space: %d\n", ret);
80820005523SFilipe Manana 		return ret;
80920005523SFilipe Manana 	}
81020005523SFilipe Manana 	/* Confirm the region is marked as free. */
81120005523SFilipe Manana 	if (!test_check_exists(cache, 128 * 1024 * 1024, 128 * 1024)) {
81220005523SFilipe Manana 		test_msg("Extent region not marked as free\n");
81320005523SFilipe Manana 		return -ENOENT;
81420005523SFilipe Manana 	}
81520005523SFilipe Manana 
81620005523SFilipe Manana 	/*
81720005523SFilipe Manana 	 * Confirm that our extent entry didn't stole all free space from the
81820005523SFilipe Manana 	 * bitmap, because of the small 8Kb free space region.
81920005523SFilipe Manana 	 */
82020005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
82120005523SFilipe Manana 	if (ret)
82220005523SFilipe Manana 		return ret;
82320005523SFilipe Manana 
82420005523SFilipe Manana 	/*
82520005523SFilipe Manana 	 * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
82620005523SFilipe Manana 	 * space. Without stealing bitmap free space into extent entry space,
82720005523SFilipe Manana 	 * we would have all this free space represented by 2 entries in the
82820005523SFilipe Manana 	 * cache:
82920005523SFilipe Manana 	 *
83020005523SFilipe Manana 	 * extent entry covering range: [128Mb, 128Mb + 256Kb[
83120005523SFilipe Manana 	 * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
83220005523SFilipe Manana 	 *
83320005523SFilipe Manana 	 * Attempting to allocate the whole free space (1Mb) would fail, because
83420005523SFilipe Manana 	 * we can't allocate from multiple entries.
83520005523SFilipe Manana 	 * With the bitmap free space stealing, we get a single extent entry
83620005523SFilipe Manana 	 * that represents the 1Mb free space, and therefore we're able to
83720005523SFilipe Manana 	 * allocate the whole free space at once.
83820005523SFilipe Manana 	 */
83920005523SFilipe Manana 	if (!test_check_exists(cache, 128 * 1024 * 1024 - 768 * 1024,
84020005523SFilipe Manana 			       1 * 1024 * 1024)) {
84120005523SFilipe Manana 		test_msg("Expected region not marked as free\n");
84220005523SFilipe Manana 		return -ENOENT;
84320005523SFilipe Manana 	}
84420005523SFilipe Manana 
84520005523SFilipe Manana 	if (cache->free_space_ctl->free_space != (1 * 1024 * 1024 + 8192)) {
84620005523SFilipe Manana 		test_msg("Cache free space is not 1Mb + 8Kb\n");
84720005523SFilipe Manana 		return -EINVAL;
84820005523SFilipe Manana 	}
84920005523SFilipe Manana 
85020005523SFilipe Manana 	offset = btrfs_find_space_for_alloc(cache,
85120005523SFilipe Manana 					    0, 1 * 1024 * 1024, 0,
85220005523SFilipe Manana 					    &max_extent_size);
85320005523SFilipe Manana 	if (offset != (128 * 1024 * 1024 - 768 * 1024)) {
85420005523SFilipe Manana 		test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
85520005523SFilipe Manana 			 offset);
85620005523SFilipe Manana 		return -EINVAL;
85720005523SFilipe Manana 	}
85820005523SFilipe Manana 
85920005523SFilipe Manana 	/* All that remains is a 8Kb free space region in a bitmap. Confirm. */
86020005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 1, 1);
86120005523SFilipe Manana 	if (ret)
86220005523SFilipe Manana 		return ret;
86320005523SFilipe Manana 
86420005523SFilipe Manana 	if (cache->free_space_ctl->free_space != 8192) {
86520005523SFilipe Manana 		test_msg("Cache free space is not 8Kb\n");
86620005523SFilipe Manana 		return -EINVAL;
86720005523SFilipe Manana 	}
86820005523SFilipe Manana 
86920005523SFilipe Manana 	offset = btrfs_find_space_for_alloc(cache,
87020005523SFilipe Manana 					    0, 8192, 0,
87120005523SFilipe Manana 					    &max_extent_size);
87220005523SFilipe Manana 	if (offset != (32 * 1024 * 1024)) {
87320005523SFilipe Manana 		test_msg("Failed to allocate 8Kb from space cache, returned offset is: %llu\n",
87420005523SFilipe Manana 			 offset);
87520005523SFilipe Manana 		return -EINVAL;
87620005523SFilipe Manana 	}
87720005523SFilipe Manana 
87820005523SFilipe Manana 	ret = check_cache_empty(cache);
87920005523SFilipe Manana 	if (ret)
88020005523SFilipe Manana 		return ret;
88120005523SFilipe Manana 
882*28f0779aSDavid Sterba 	cache->free_space_ctl->op = orig_free_space_ops;
88320005523SFilipe Manana 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
88420005523SFilipe Manana 
88520005523SFilipe Manana 	return 0;
88620005523SFilipe Manana }
88720005523SFilipe Manana 
888dc11dd5dSJosef Bacik int btrfs_test_free_space_cache(void)
889dc11dd5dSJosef Bacik {
890dc11dd5dSJosef Bacik 	struct btrfs_block_group_cache *cache;
891d0bd4560SJosef Bacik 	struct btrfs_root *root = NULL;
892d0bd4560SJosef Bacik 	int ret = -ENOMEM;
893dc11dd5dSJosef Bacik 
894dc11dd5dSJosef Bacik 	test_msg("Running btrfs free space cache tests\n");
895dc11dd5dSJosef Bacik 
896dc11dd5dSJosef Bacik 	cache = init_test_block_group();
897dc11dd5dSJosef Bacik 	if (!cache) {
898dc11dd5dSJosef Bacik 		test_msg("Couldn't run the tests\n");
899dc11dd5dSJosef Bacik 		return 0;
900dc11dd5dSJosef Bacik 	}
901dc11dd5dSJosef Bacik 
902d0bd4560SJosef Bacik 	root = btrfs_alloc_dummy_root();
90389b6c8d1SDan Carpenter 	if (IS_ERR(root)) {
90489b6c8d1SDan Carpenter 		ret = PTR_ERR(root);
905d0bd4560SJosef Bacik 		goto out;
90689b6c8d1SDan Carpenter 	}
907d0bd4560SJosef Bacik 
908d0bd4560SJosef Bacik 	root->fs_info = btrfs_alloc_dummy_fs_info();
909d0bd4560SJosef Bacik 	if (!root->fs_info)
910d0bd4560SJosef Bacik 		goto out;
911d0bd4560SJosef Bacik 
912d0bd4560SJosef Bacik 	root->fs_info->extent_root = root;
913d0bd4560SJosef Bacik 	cache->fs_info = root->fs_info;
914d0bd4560SJosef Bacik 
915dc11dd5dSJosef Bacik 	ret = test_extents(cache);
916dc11dd5dSJosef Bacik 	if (ret)
917dc11dd5dSJosef Bacik 		goto out;
918dc11dd5dSJosef Bacik 	ret = test_bitmaps(cache);
919dc11dd5dSJosef Bacik 	if (ret)
920dc11dd5dSJosef Bacik 		goto out;
921dc11dd5dSJosef Bacik 	ret = test_bitmaps_and_extents(cache);
922dc11dd5dSJosef Bacik 	if (ret)
923dc11dd5dSJosef Bacik 		goto out;
92420005523SFilipe Manana 
92520005523SFilipe Manana 	ret = test_steal_space_from_bitmap_to_extent(cache);
926dc11dd5dSJosef Bacik out:
927dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
928dc11dd5dSJosef Bacik 	kfree(cache->free_space_ctl);
929dc11dd5dSJosef Bacik 	kfree(cache);
930d0bd4560SJosef Bacik 	btrfs_free_dummy_root(root);
931dc11dd5dSJosef Bacik 	test_msg("Free space cache tests finished\n");
932dc11dd5dSJosef Bacik 	return ret;
933dc11dd5dSJosef Bacik }
934