xref: /openbmc/linux/fs/btrfs/tests/free-space-tests.c (revision c1d7c514f745628eb096c5cbb10737855879ae25)
1*c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
2dc11dd5dSJosef Bacik /*
3dc11dd5dSJosef Bacik  * Copyright (C) 2013 Fusion IO.  All rights reserved.
4dc11dd5dSJosef Bacik  */
5dc11dd5dSJosef Bacik 
6dc11dd5dSJosef Bacik #include <linux/slab.h>
7dc11dd5dSJosef Bacik #include "btrfs-tests.h"
8dc11dd5dSJosef Bacik #include "../ctree.h"
9d0bd4560SJosef Bacik #include "../disk-io.h"
10dc11dd5dSJosef Bacik #include "../free-space-cache.h"
11dc11dd5dSJosef Bacik 
120ef6447aSFeifei Xu #define BITS_PER_BITMAP		(PAGE_SIZE * 8UL)
13dc11dd5dSJosef Bacik 
14dc11dd5dSJosef Bacik /*
1501327610SNicholas D Steeves  * This test just does basic sanity checking, making sure we can add an extent
16dc11dd5dSJosef Bacik  * entry and remove space from either end and the middle, and make sure we can
17dc11dd5dSJosef Bacik  * remove space that covers adjacent extent entries.
18dc11dd5dSJosef Bacik  */
19dc11dd5dSJosef Bacik static int test_extents(struct btrfs_block_group_cache *cache)
20dc11dd5dSJosef Bacik {
21dc11dd5dSJosef Bacik 	int ret = 0;
22dc11dd5dSJosef Bacik 
23dc11dd5dSJosef Bacik 	test_msg("Running extent only tests\n");
24dc11dd5dSJosef Bacik 
25dc11dd5dSJosef Bacik 	/* First just make sure we can remove an entire entry */
26ee22184bSByongho Lee 	ret = btrfs_add_free_space(cache, 0, SZ_4M);
27dc11dd5dSJosef Bacik 	if (ret) {
28dc11dd5dSJosef Bacik 		test_msg("Error adding initial extents %d\n", ret);
29dc11dd5dSJosef Bacik 		return ret;
30dc11dd5dSJosef Bacik 	}
31dc11dd5dSJosef Bacik 
32ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, 0, SZ_4M);
33dc11dd5dSJosef Bacik 	if (ret) {
34dc11dd5dSJosef Bacik 		test_msg("Error removing extent %d\n", ret);
35dc11dd5dSJosef Bacik 		return ret;
36dc11dd5dSJosef Bacik 	}
37dc11dd5dSJosef Bacik 
38ee22184bSByongho Lee 	if (test_check_exists(cache, 0, SZ_4M)) {
39dc11dd5dSJosef Bacik 		test_msg("Full remove left some lingering space\n");
40dc11dd5dSJosef Bacik 		return -1;
41dc11dd5dSJosef Bacik 	}
42dc11dd5dSJosef Bacik 
43dc11dd5dSJosef Bacik 	/* Ok edge and middle cases now */
44ee22184bSByongho Lee 	ret = btrfs_add_free_space(cache, 0, SZ_4M);
45dc11dd5dSJosef Bacik 	if (ret) {
46dc11dd5dSJosef Bacik 		test_msg("Error adding half extent %d\n", ret);
47dc11dd5dSJosef Bacik 		return ret;
48dc11dd5dSJosef Bacik 	}
49dc11dd5dSJosef Bacik 
50ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_1M);
51dc11dd5dSJosef Bacik 	if (ret) {
52dc11dd5dSJosef Bacik 		test_msg("Error removing tail end %d\n", ret);
53dc11dd5dSJosef Bacik 		return ret;
54dc11dd5dSJosef Bacik 	}
55dc11dd5dSJosef Bacik 
56ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, 0, SZ_1M);
57dc11dd5dSJosef Bacik 	if (ret) {
58dc11dd5dSJosef Bacik 		test_msg("Error removing front end %d\n", ret);
59dc11dd5dSJosef Bacik 		return ret;
60dc11dd5dSJosef Bacik 	}
61dc11dd5dSJosef Bacik 
62ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, SZ_2M, 4096);
63dc11dd5dSJosef Bacik 	if (ret) {
6477d84ff8SMasanari Iida 		test_msg("Error removing middle piece %d\n", ret);
65dc11dd5dSJosef Bacik 		return ret;
66dc11dd5dSJosef Bacik 	}
67dc11dd5dSJosef Bacik 
68ee22184bSByongho Lee 	if (test_check_exists(cache, 0, SZ_1M)) {
69dc11dd5dSJosef Bacik 		test_msg("Still have space at the front\n");
70dc11dd5dSJosef Bacik 		return -1;
71dc11dd5dSJosef Bacik 	}
72dc11dd5dSJosef Bacik 
73ee22184bSByongho Lee 	if (test_check_exists(cache, SZ_2M, 4096)) {
74dc11dd5dSJosef Bacik 		test_msg("Still have space in the middle\n");
75dc11dd5dSJosef Bacik 		return -1;
76dc11dd5dSJosef Bacik 	}
77dc11dd5dSJosef Bacik 
78ee22184bSByongho Lee 	if (test_check_exists(cache, 3 * SZ_1M, SZ_1M)) {
79dc11dd5dSJosef Bacik 		test_msg("Still have space at the end\n");
80dc11dd5dSJosef Bacik 		return -1;
81dc11dd5dSJosef Bacik 	}
82dc11dd5dSJosef Bacik 
83dc11dd5dSJosef Bacik 	/* Cleanup */
84dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
85dc11dd5dSJosef Bacik 
86dc11dd5dSJosef Bacik 	return 0;
87dc11dd5dSJosef Bacik }
88dc11dd5dSJosef Bacik 
89b9ef22deSFeifei Xu static int test_bitmaps(struct btrfs_block_group_cache *cache,
90b9ef22deSFeifei Xu 			u32 sectorsize)
91dc11dd5dSJosef Bacik {
92dc11dd5dSJosef Bacik 	u64 next_bitmap_offset;
93dc11dd5dSJosef Bacik 	int ret;
94dc11dd5dSJosef Bacik 
95dc11dd5dSJosef Bacik 	test_msg("Running bitmap only tests\n");
96dc11dd5dSJosef Bacik 
97ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
98dc11dd5dSJosef Bacik 	if (ret) {
99dc11dd5dSJosef Bacik 		test_msg("Couldn't create a bitmap entry %d\n", ret);
100dc11dd5dSJosef Bacik 		return ret;
101dc11dd5dSJosef Bacik 	}
102dc11dd5dSJosef Bacik 
103ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, 0, SZ_4M);
104dc11dd5dSJosef Bacik 	if (ret) {
105dc11dd5dSJosef Bacik 		test_msg("Error removing bitmap full range %d\n", ret);
106dc11dd5dSJosef Bacik 		return ret;
107dc11dd5dSJosef Bacik 	}
108dc11dd5dSJosef Bacik 
109ee22184bSByongho Lee 	if (test_check_exists(cache, 0, SZ_4M)) {
110dc11dd5dSJosef Bacik 		test_msg("Left some space in bitmap\n");
111dc11dd5dSJosef Bacik 		return -1;
112dc11dd5dSJosef Bacik 	}
113dc11dd5dSJosef Bacik 
114ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
115dc11dd5dSJosef Bacik 	if (ret) {
116dc11dd5dSJosef Bacik 		test_msg("Couldn't add to our bitmap entry %d\n", ret);
117dc11dd5dSJosef Bacik 		return ret;
118dc11dd5dSJosef Bacik 	}
119dc11dd5dSJosef Bacik 
120ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, SZ_1M, SZ_2M);
121dc11dd5dSJosef Bacik 	if (ret) {
122dc11dd5dSJosef Bacik 		test_msg("Couldn't remove middle chunk %d\n", ret);
123dc11dd5dSJosef Bacik 		return ret;
124dc11dd5dSJosef Bacik 	}
125dc11dd5dSJosef Bacik 
126dc11dd5dSJosef Bacik 	/*
127dc11dd5dSJosef Bacik 	 * The first bitmap we have starts at offset 0 so the next one is just
128dc11dd5dSJosef Bacik 	 * at the end of the first bitmap.
129dc11dd5dSJosef Bacik 	 */
130b9ef22deSFeifei Xu 	next_bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize);
131dc11dd5dSJosef Bacik 
132dc11dd5dSJosef Bacik 	/* Test a bit straddling two bitmaps */
133ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, next_bitmap_offset - SZ_2M,
134ee22184bSByongho Lee 					SZ_4M, 1);
135dc11dd5dSJosef Bacik 	if (ret) {
136dc11dd5dSJosef Bacik 		test_msg("Couldn't add space that straddles two bitmaps %d\n",
137dc11dd5dSJosef Bacik 				ret);
138dc11dd5dSJosef Bacik 		return ret;
139dc11dd5dSJosef Bacik 	}
140dc11dd5dSJosef Bacik 
141ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, next_bitmap_offset - SZ_1M, SZ_2M);
142dc11dd5dSJosef Bacik 	if (ret) {
143dc11dd5dSJosef Bacik 		test_msg("Couldn't remove overlapping space %d\n", ret);
144dc11dd5dSJosef Bacik 		return ret;
145dc11dd5dSJosef Bacik 	}
146dc11dd5dSJosef Bacik 
147ee22184bSByongho Lee 	if (test_check_exists(cache, next_bitmap_offset - SZ_1M, SZ_2M)) {
148dc11dd5dSJosef Bacik 		test_msg("Left some space when removing overlapping\n");
149dc11dd5dSJosef Bacik 		return -1;
150dc11dd5dSJosef Bacik 	}
151dc11dd5dSJosef Bacik 
152dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
153dc11dd5dSJosef Bacik 
154dc11dd5dSJosef Bacik 	return 0;
155dc11dd5dSJosef Bacik }
156dc11dd5dSJosef Bacik 
157dc11dd5dSJosef Bacik /* This is the high grade jackassery */
158b9ef22deSFeifei Xu static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache,
159b9ef22deSFeifei Xu 				    u32 sectorsize)
160dc11dd5dSJosef Bacik {
161b9ef22deSFeifei Xu 	u64 bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize);
162dc11dd5dSJosef Bacik 	int ret;
163dc11dd5dSJosef Bacik 
164dc11dd5dSJosef Bacik 	test_msg("Running bitmap and extent tests\n");
165dc11dd5dSJosef Bacik 
166dc11dd5dSJosef Bacik 	/*
167dc11dd5dSJosef Bacik 	 * First let's do something simple, an extent at the same offset as the
168dc11dd5dSJosef Bacik 	 * bitmap, but the free space completely in the extent and then
169dc11dd5dSJosef Bacik 	 * completely in the bitmap.
170dc11dd5dSJosef Bacik 	 */
171ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, SZ_4M, SZ_1M, 1);
172dc11dd5dSJosef Bacik 	if (ret) {
173dc11dd5dSJosef Bacik 		test_msg("Couldn't create bitmap entry %d\n", ret);
174dc11dd5dSJosef Bacik 		return ret;
175dc11dd5dSJosef Bacik 	}
176dc11dd5dSJosef Bacik 
177ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
178dc11dd5dSJosef Bacik 	if (ret) {
179dc11dd5dSJosef Bacik 		test_msg("Couldn't add extent entry %d\n", ret);
180dc11dd5dSJosef Bacik 		return ret;
181dc11dd5dSJosef Bacik 	}
182dc11dd5dSJosef Bacik 
183ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, 0, SZ_1M);
184dc11dd5dSJosef Bacik 	if (ret) {
185dc11dd5dSJosef Bacik 		test_msg("Couldn't remove extent entry %d\n", ret);
186dc11dd5dSJosef Bacik 		return ret;
187dc11dd5dSJosef Bacik 	}
188dc11dd5dSJosef Bacik 
189ee22184bSByongho Lee 	if (test_check_exists(cache, 0, SZ_1M)) {
190dc11dd5dSJosef Bacik 		test_msg("Left remnants after our remove\n");
191dc11dd5dSJosef Bacik 		return -1;
192dc11dd5dSJosef Bacik 	}
193dc11dd5dSJosef Bacik 
194dc11dd5dSJosef Bacik 	/* Now to add back the extent entry and remove from the bitmap */
195ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
196dc11dd5dSJosef Bacik 	if (ret) {
197dc11dd5dSJosef Bacik 		test_msg("Couldn't re-add extent entry %d\n", ret);
198dc11dd5dSJosef Bacik 		return ret;
199dc11dd5dSJosef Bacik 	}
200dc11dd5dSJosef Bacik 
201ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, SZ_4M, SZ_1M);
202dc11dd5dSJosef Bacik 	if (ret) {
203dc11dd5dSJosef Bacik 		test_msg("Couldn't remove from bitmap %d\n", ret);
204dc11dd5dSJosef Bacik 		return ret;
205dc11dd5dSJosef Bacik 	}
206dc11dd5dSJosef Bacik 
207ee22184bSByongho Lee 	if (test_check_exists(cache, SZ_4M, SZ_1M)) {
208dc11dd5dSJosef Bacik 		test_msg("Left remnants in the bitmap\n");
209dc11dd5dSJosef Bacik 		return -1;
210dc11dd5dSJosef Bacik 	}
211dc11dd5dSJosef Bacik 
212dc11dd5dSJosef Bacik 	/*
213dc11dd5dSJosef Bacik 	 * Ok so a little more evil, extent entry and bitmap at the same offset,
214dc11dd5dSJosef Bacik 	 * removing an overlapping chunk.
215dc11dd5dSJosef Bacik 	 */
216ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, SZ_1M, SZ_4M, 1);
217dc11dd5dSJosef Bacik 	if (ret) {
218dc11dd5dSJosef Bacik 		test_msg("Couldn't add to a bitmap %d\n", ret);
219dc11dd5dSJosef Bacik 		return ret;
220dc11dd5dSJosef Bacik 	}
221dc11dd5dSJosef Bacik 
222ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, SZ_512K, 3 * SZ_1M);
223dc11dd5dSJosef Bacik 	if (ret) {
224dc11dd5dSJosef Bacik 		test_msg("Couldn't remove overlapping space %d\n", ret);
225dc11dd5dSJosef Bacik 		return ret;
226dc11dd5dSJosef Bacik 	}
227dc11dd5dSJosef Bacik 
228ee22184bSByongho Lee 	if (test_check_exists(cache, SZ_512K, 3 * SZ_1M)) {
2298faaaeadSMasanari Iida 		test_msg("Left over pieces after removing overlapping\n");
230dc11dd5dSJosef Bacik 		return -1;
231dc11dd5dSJosef Bacik 	}
232dc11dd5dSJosef Bacik 
233dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
234dc11dd5dSJosef Bacik 
235dc11dd5dSJosef Bacik 	/* Now with the extent entry offset into the bitmap */
236ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, SZ_4M, SZ_4M, 1);
237dc11dd5dSJosef Bacik 	if (ret) {
238dc11dd5dSJosef Bacik 		test_msg("Couldn't add space to the bitmap %d\n", ret);
239dc11dd5dSJosef Bacik 		return ret;
240dc11dd5dSJosef Bacik 	}
241dc11dd5dSJosef Bacik 
242ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, SZ_2M, SZ_2M, 0);
243dc11dd5dSJosef Bacik 	if (ret) {
244dc11dd5dSJosef Bacik 		test_msg("Couldn't add extent to the cache %d\n", ret);
245dc11dd5dSJosef Bacik 		return ret;
246dc11dd5dSJosef Bacik 	}
247dc11dd5dSJosef Bacik 
248ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_4M);
249dc11dd5dSJosef Bacik 	if (ret) {
250dc11dd5dSJosef Bacik 		test_msg("Problem removing overlapping space %d\n", ret);
251dc11dd5dSJosef Bacik 		return ret;
252dc11dd5dSJosef Bacik 	}
253dc11dd5dSJosef Bacik 
254ee22184bSByongho Lee 	if (test_check_exists(cache, 3 * SZ_1M, SZ_4M)) {
255dc11dd5dSJosef Bacik 		test_msg("Left something behind when removing space");
256dc11dd5dSJosef Bacik 		return -1;
257dc11dd5dSJosef Bacik 	}
258dc11dd5dSJosef Bacik 
259dc11dd5dSJosef Bacik 	/*
260dc11dd5dSJosef Bacik 	 * This has blown up in the past, the extent entry starts before the
261dc11dd5dSJosef Bacik 	 * bitmap entry, but we're trying to remove an offset that falls
262dc11dd5dSJosef Bacik 	 * completely within the bitmap range and is in both the extent entry
263dc11dd5dSJosef Bacik 	 * and the bitmap entry, looks like this
264dc11dd5dSJosef Bacik 	 *
265dc11dd5dSJosef Bacik 	 *   [ extent ]
266dc11dd5dSJosef Bacik 	 *      [ bitmap ]
267dc11dd5dSJosef Bacik 	 *        [ del ]
268dc11dd5dSJosef Bacik 	 */
269dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
270ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, bitmap_offset + SZ_4M, SZ_4M, 1);
271dc11dd5dSJosef Bacik 	if (ret) {
272dc11dd5dSJosef Bacik 		test_msg("Couldn't add bitmap %d\n", ret);
273dc11dd5dSJosef Bacik 		return ret;
274dc11dd5dSJosef Bacik 	}
275dc11dd5dSJosef Bacik 
276ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, bitmap_offset - SZ_1M,
277ee22184bSByongho Lee 					5 * SZ_1M, 0);
278dc11dd5dSJosef Bacik 	if (ret) {
279dc11dd5dSJosef Bacik 		test_msg("Couldn't add extent entry %d\n", ret);
280dc11dd5dSJosef Bacik 		return ret;
281dc11dd5dSJosef Bacik 	}
282dc11dd5dSJosef Bacik 
283ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, bitmap_offset + SZ_1M, 5 * SZ_1M);
284dc11dd5dSJosef Bacik 	if (ret) {
285dc11dd5dSJosef Bacik 		test_msg("Failed to free our space %d\n", ret);
286dc11dd5dSJosef Bacik 		return ret;
287dc11dd5dSJosef Bacik 	}
288dc11dd5dSJosef Bacik 
289ee22184bSByongho Lee 	if (test_check_exists(cache, bitmap_offset + SZ_1M, 5 * SZ_1M)) {
290dc11dd5dSJosef Bacik 		test_msg("Left stuff over\n");
291dc11dd5dSJosef Bacik 		return -1;
292dc11dd5dSJosef Bacik 	}
293dc11dd5dSJosef Bacik 
294dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
295dc11dd5dSJosef Bacik 
296dc11dd5dSJosef Bacik 	/*
297dc11dd5dSJosef Bacik 	 * This blew up before, we have part of the free space in a bitmap and
298dc11dd5dSJosef Bacik 	 * then the entirety of the rest of the space in an extent.  This used
299dc11dd5dSJosef Bacik 	 * to return -EAGAIN back from btrfs_remove_extent, make sure this
300dc11dd5dSJosef Bacik 	 * doesn't happen.
301dc11dd5dSJosef Bacik 	 */
302ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, SZ_1M, SZ_2M, 1);
303dc11dd5dSJosef Bacik 	if (ret) {
304dc11dd5dSJosef Bacik 		test_msg("Couldn't add bitmap entry %d\n", ret);
305dc11dd5dSJosef Bacik 		return ret;
306dc11dd5dSJosef Bacik 	}
307dc11dd5dSJosef Bacik 
308ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, 3 * SZ_1M, SZ_1M, 0);
309dc11dd5dSJosef Bacik 	if (ret) {
310dc11dd5dSJosef Bacik 		test_msg("Couldn't add extent entry %d\n", ret);
311dc11dd5dSJosef Bacik 		return ret;
312dc11dd5dSJosef Bacik 	}
313dc11dd5dSJosef Bacik 
314ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, SZ_1M, 3 * SZ_1M);
315dc11dd5dSJosef Bacik 	if (ret) {
316dc11dd5dSJosef Bacik 		test_msg("Error removing bitmap and extent overlapping %d\n", ret);
317dc11dd5dSJosef Bacik 		return ret;
318dc11dd5dSJosef Bacik 	}
319dc11dd5dSJosef Bacik 
320dc11dd5dSJosef Bacik 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
321dc11dd5dSJosef Bacik 	return 0;
322dc11dd5dSJosef Bacik }
323dc11dd5dSJosef Bacik 
32420005523SFilipe Manana /* Used by test_steal_space_from_bitmap_to_extent(). */
32520005523SFilipe Manana static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl,
32620005523SFilipe Manana 			    struct btrfs_free_space *info)
32720005523SFilipe Manana {
32820005523SFilipe Manana 	return ctl->free_extents > 0;
32920005523SFilipe Manana }
33020005523SFilipe Manana 
33120005523SFilipe Manana /* Used by test_steal_space_from_bitmap_to_extent(). */
33220005523SFilipe Manana static int
33320005523SFilipe Manana check_num_extents_and_bitmaps(const struct btrfs_block_group_cache *cache,
33420005523SFilipe Manana 			      const int num_extents,
33520005523SFilipe Manana 			      const int num_bitmaps)
33620005523SFilipe Manana {
33720005523SFilipe Manana 	if (cache->free_space_ctl->free_extents != num_extents) {
33820005523SFilipe Manana 		test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
33920005523SFilipe Manana 			 cache->free_space_ctl->free_extents, num_extents);
34020005523SFilipe Manana 		return -EINVAL;
34120005523SFilipe Manana 	}
34220005523SFilipe Manana 	if (cache->free_space_ctl->total_bitmaps != num_bitmaps) {
34320005523SFilipe Manana 		test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
34420005523SFilipe Manana 			 cache->free_space_ctl->total_bitmaps, num_bitmaps);
34520005523SFilipe Manana 		return -EINVAL;
34620005523SFilipe Manana 	}
34720005523SFilipe Manana 	return 0;
34820005523SFilipe Manana }
34920005523SFilipe Manana 
35020005523SFilipe Manana /* Used by test_steal_space_from_bitmap_to_extent(). */
35120005523SFilipe Manana static int check_cache_empty(struct btrfs_block_group_cache *cache)
35220005523SFilipe Manana {
35320005523SFilipe Manana 	u64 offset;
35420005523SFilipe Manana 	u64 max_extent_size;
35520005523SFilipe Manana 
35620005523SFilipe Manana 	/*
35720005523SFilipe Manana 	 * Now lets confirm that there's absolutely no free space left to
35820005523SFilipe Manana 	 * allocate.
35920005523SFilipe Manana 	 */
36020005523SFilipe Manana 	if (cache->free_space_ctl->free_space != 0) {
36120005523SFilipe Manana 		test_msg("Cache free space is not 0\n");
36220005523SFilipe Manana 		return -EINVAL;
36320005523SFilipe Manana 	}
36420005523SFilipe Manana 
36520005523SFilipe Manana 	/* And any allocation request, no matter how small, should fail now. */
36620005523SFilipe Manana 	offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0,
36720005523SFilipe Manana 					    &max_extent_size);
36820005523SFilipe Manana 	if (offset != 0) {
36920005523SFilipe Manana 		test_msg("Space allocation did not fail, returned offset: %llu",
37020005523SFilipe Manana 			 offset);
37120005523SFilipe Manana 		return -EINVAL;
37220005523SFilipe Manana 	}
37320005523SFilipe Manana 
37420005523SFilipe Manana 	/* And no extent nor bitmap entries in the cache anymore. */
37520005523SFilipe Manana 	return check_num_extents_and_bitmaps(cache, 0, 0);
37620005523SFilipe Manana }
37720005523SFilipe Manana 
37820005523SFilipe Manana /*
37920005523SFilipe Manana  * Before we were able to steal free space from a bitmap entry to an extent
38020005523SFilipe Manana  * entry, we could end up with 2 entries representing a contiguous free space.
38120005523SFilipe Manana  * One would be an extent entry and the other a bitmap entry. Since in order
38220005523SFilipe Manana  * to allocate space to a caller we use only 1 entry, we couldn't return that
38320005523SFilipe Manana  * whole range to the caller if it was requested. This forced the caller to
38420005523SFilipe Manana  * either assume ENOSPC or perform several smaller space allocations, which
38520005523SFilipe Manana  * wasn't optimal as they could be spread all over the block group while under
38620005523SFilipe Manana  * concurrency (extra overhead and fragmentation).
38720005523SFilipe Manana  *
38801327610SNicholas D Steeves  * This stealing approach is beneficial, since we always prefer to allocate
38901327610SNicholas D Steeves  * from extent entries, both for clustered and non-clustered allocation
39001327610SNicholas D Steeves  * requests.
39120005523SFilipe Manana  */
39220005523SFilipe Manana static int
393b9ef22deSFeifei Xu test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache,
394b9ef22deSFeifei Xu 				       u32 sectorsize)
39520005523SFilipe Manana {
39620005523SFilipe Manana 	int ret;
39720005523SFilipe Manana 	u64 offset;
39820005523SFilipe Manana 	u64 max_extent_size;
39920e5506bSDavid Sterba 	const struct btrfs_free_space_op test_free_space_ops = {
40028f0779aSDavid Sterba 		.recalc_thresholds = cache->free_space_ctl->op->recalc_thresholds,
40128f0779aSDavid Sterba 		.use_bitmap = test_use_bitmap,
40228f0779aSDavid Sterba 	};
40320e5506bSDavid Sterba 	const struct btrfs_free_space_op *orig_free_space_ops;
40420005523SFilipe Manana 
40520005523SFilipe Manana 	test_msg("Running space stealing from bitmap to extent\n");
40620005523SFilipe Manana 
40720005523SFilipe Manana 	/*
40820005523SFilipe Manana 	 * For this test, we want to ensure we end up with an extent entry
40920005523SFilipe Manana 	 * immediately adjacent to a bitmap entry, where the bitmap starts
41020005523SFilipe Manana 	 * at an offset where the extent entry ends. We keep adding and
41120005523SFilipe Manana 	 * removing free space to reach into this state, but to get there
41220005523SFilipe Manana 	 * we need to reach a point where marking new free space doesn't
41320005523SFilipe Manana 	 * result in adding new extent entries or merging the new space
41420005523SFilipe Manana 	 * with existing extent entries - the space ends up being marked
41520005523SFilipe Manana 	 * in an existing bitmap that covers the new free space range.
41620005523SFilipe Manana 	 *
41720005523SFilipe Manana 	 * To get there, we need to reach the threshold defined set at
41820005523SFilipe Manana 	 * cache->free_space_ctl->extents_thresh, which currently is
41920005523SFilipe Manana 	 * 256 extents on a x86_64 system at least, and a few other
42020005523SFilipe Manana 	 * conditions (check free_space_cache.c). Instead of making the
42120005523SFilipe Manana 	 * test much longer and complicated, use a "use_bitmap" operation
42220005523SFilipe Manana 	 * that forces use of bitmaps as soon as we have at least 1
42320005523SFilipe Manana 	 * extent entry.
42420005523SFilipe Manana 	 */
42528f0779aSDavid Sterba 	orig_free_space_ops = cache->free_space_ctl->op;
42628f0779aSDavid Sterba 	cache->free_space_ctl->op = &test_free_space_ops;
42720005523SFilipe Manana 
42820005523SFilipe Manana 	/*
42920005523SFilipe Manana 	 * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
43020005523SFilipe Manana 	 */
431ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0);
43220005523SFilipe Manana 	if (ret) {
43320005523SFilipe Manana 		test_msg("Couldn't add extent entry %d\n", ret);
43420005523SFilipe Manana 		return ret;
43520005523SFilipe Manana 	}
43620005523SFilipe Manana 
43720005523SFilipe Manana 	/* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
438ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K,
439ee22184bSByongho Lee 					SZ_128M - SZ_512K, 1);
44020005523SFilipe Manana 	if (ret) {
44120005523SFilipe Manana 		test_msg("Couldn't add bitmap entry %d\n", ret);
44220005523SFilipe Manana 		return ret;
44320005523SFilipe Manana 	}
44420005523SFilipe Manana 
44520005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
44620005523SFilipe Manana 	if (ret)
44720005523SFilipe Manana 		return ret;
44820005523SFilipe Manana 
44920005523SFilipe Manana 	/*
45020005523SFilipe Manana 	 * Now make only the first 256Kb of the bitmap marked as free, so that
45120005523SFilipe Manana 	 * we end up with only the following ranges marked as free space:
45220005523SFilipe Manana 	 *
45320005523SFilipe Manana 	 * [128Mb - 256Kb, 128Mb - 128Kb[
45420005523SFilipe Manana 	 * [128Mb + 512Kb, 128Mb + 768Kb[
45520005523SFilipe Manana 	 */
45620005523SFilipe Manana 	ret = btrfs_remove_free_space(cache,
457ee22184bSByongho Lee 				      SZ_128M + 768 * SZ_1K,
458ee22184bSByongho Lee 				      SZ_128M - 768 * SZ_1K);
45920005523SFilipe Manana 	if (ret) {
46020005523SFilipe Manana 		test_msg("Failed to free part of bitmap space %d\n", ret);
46120005523SFilipe Manana 		return ret;
46220005523SFilipe Manana 	}
46320005523SFilipe Manana 
46420005523SFilipe Manana 	/* Confirm that only those 2 ranges are marked as free. */
465ee22184bSByongho Lee 	if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) {
46620005523SFilipe Manana 		test_msg("Free space range missing\n");
46720005523SFilipe Manana 		return -ENOENT;
46820005523SFilipe Manana 	}
469ee22184bSByongho Lee 	if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) {
47020005523SFilipe Manana 		test_msg("Free space range missing\n");
47120005523SFilipe Manana 		return -ENOENT;
47220005523SFilipe Manana 	}
47320005523SFilipe Manana 
47420005523SFilipe Manana 	/*
47520005523SFilipe Manana 	 * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
47620005523SFilipe Manana 	 * as free anymore.
47720005523SFilipe Manana 	 */
478ee22184bSByongho Lee 	if (test_check_exists(cache, SZ_128M + 768 * SZ_1K,
479ee22184bSByongho Lee 			      SZ_128M - 768 * SZ_1K)) {
48020005523SFilipe Manana 		test_msg("Bitmap region not removed from space cache\n");
48120005523SFilipe Manana 		return -EINVAL;
48220005523SFilipe Manana 	}
48320005523SFilipe Manana 
48420005523SFilipe Manana 	/*
48520005523SFilipe Manana 	 * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
48620005523SFilipe Manana 	 * covered by the bitmap, isn't marked as free.
48720005523SFilipe Manana 	 */
488ee22184bSByongho Lee 	if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) {
48920005523SFilipe Manana 		test_msg("Invalid bitmap region marked as free\n");
49020005523SFilipe Manana 		return -EINVAL;
49120005523SFilipe Manana 	}
49220005523SFilipe Manana 
49320005523SFilipe Manana 	/*
49420005523SFilipe Manana 	 * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
49520005523SFilipe Manana 	 * by the bitmap too, isn't marked as free either.
49620005523SFilipe Manana 	 */
497ee22184bSByongho Lee 	if (test_check_exists(cache, SZ_128M, SZ_256K)) {
49820005523SFilipe Manana 		test_msg("Invalid bitmap region marked as free\n");
49920005523SFilipe Manana 		return -EINVAL;
50020005523SFilipe Manana 	}
50120005523SFilipe Manana 
50220005523SFilipe Manana 	/*
50320005523SFilipe Manana 	 * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
50420005523SFilipe Manana 	 * lets make sure the free space cache marks it as free in the bitmap,
50520005523SFilipe Manana 	 * and doesn't insert a new extent entry to represent this region.
50620005523SFilipe Manana 	 */
507ee22184bSByongho Lee 	ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K);
50820005523SFilipe Manana 	if (ret) {
50920005523SFilipe Manana 		test_msg("Error adding free space: %d\n", ret);
51020005523SFilipe Manana 		return ret;
51120005523SFilipe Manana 	}
51220005523SFilipe Manana 	/* Confirm the region is marked as free. */
513ee22184bSByongho Lee 	if (!test_check_exists(cache, SZ_128M, SZ_512K)) {
51420005523SFilipe Manana 		test_msg("Bitmap region not marked as free\n");
51520005523SFilipe Manana 		return -ENOENT;
51620005523SFilipe Manana 	}
51720005523SFilipe Manana 
51820005523SFilipe Manana 	/*
51920005523SFilipe Manana 	 * Confirm that no new extent entries or bitmap entries were added to
52020005523SFilipe Manana 	 * the cache after adding that free space region.
52120005523SFilipe Manana 	 */
52220005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
52320005523SFilipe Manana 	if (ret)
52420005523SFilipe Manana 		return ret;
52520005523SFilipe Manana 
52620005523SFilipe Manana 	/*
52720005523SFilipe Manana 	 * Now lets add a small free space region to the right of the previous
52820005523SFilipe Manana 	 * one, which is not contiguous with it and is part of the bitmap too.
52920005523SFilipe Manana 	 * The goal is to test that the bitmap entry space stealing doesn't
53020005523SFilipe Manana 	 * steal this space region.
53120005523SFilipe Manana 	 */
532b9ef22deSFeifei Xu 	ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, sectorsize);
53320005523SFilipe Manana 	if (ret) {
53420005523SFilipe Manana 		test_msg("Error adding free space: %d\n", ret);
53520005523SFilipe Manana 		return ret;
53620005523SFilipe Manana 	}
53720005523SFilipe Manana 
53820005523SFilipe Manana 	/*
53920005523SFilipe Manana 	 * Confirm that no new extent entries or bitmap entries were added to
54020005523SFilipe Manana 	 * the cache after adding that free space region.
54120005523SFilipe Manana 	 */
54220005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
54320005523SFilipe Manana 	if (ret)
54420005523SFilipe Manana 		return ret;
54520005523SFilipe Manana 
54620005523SFilipe Manana 	/*
54720005523SFilipe Manana 	 * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
54820005523SFilipe Manana 	 * expand the range covered by the existing extent entry that represents
54920005523SFilipe Manana 	 * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
55020005523SFilipe Manana 	 */
551ee22184bSByongho Lee 	ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K);
55220005523SFilipe Manana 	if (ret) {
55320005523SFilipe Manana 		test_msg("Error adding free space: %d\n", ret);
55420005523SFilipe Manana 		return ret;
55520005523SFilipe Manana 	}
55620005523SFilipe Manana 	/* Confirm the region is marked as free. */
557ee22184bSByongho Lee 	if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) {
55820005523SFilipe Manana 		test_msg("Extent region not marked as free\n");
55920005523SFilipe Manana 		return -ENOENT;
56020005523SFilipe Manana 	}
56120005523SFilipe Manana 
56220005523SFilipe Manana 	/*
56320005523SFilipe Manana 	 * Confirm that our extent entry didn't stole all free space from the
56420005523SFilipe Manana 	 * bitmap, because of the small 4Kb free space region.
56520005523SFilipe Manana 	 */
56620005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
56720005523SFilipe Manana 	if (ret)
56820005523SFilipe Manana 		return ret;
56920005523SFilipe Manana 
57020005523SFilipe Manana 	/*
57120005523SFilipe Manana 	 * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
57220005523SFilipe Manana 	 * space. Without stealing bitmap free space into extent entry space,
57320005523SFilipe Manana 	 * we would have all this free space represented by 2 entries in the
57420005523SFilipe Manana 	 * cache:
57520005523SFilipe Manana 	 *
57620005523SFilipe Manana 	 * extent entry covering range: [128Mb - 256Kb, 128Mb[
57720005523SFilipe Manana 	 * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
57820005523SFilipe Manana 	 *
57920005523SFilipe Manana 	 * Attempting to allocate the whole free space (1Mb) would fail, because
58020005523SFilipe Manana 	 * we can't allocate from multiple entries.
58120005523SFilipe Manana 	 * With the bitmap free space stealing, we get a single extent entry
58220005523SFilipe Manana 	 * that represents the 1Mb free space, and therefore we're able to
58320005523SFilipe Manana 	 * allocate the whole free space at once.
58420005523SFilipe Manana 	 */
585ee22184bSByongho Lee 	if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) {
58620005523SFilipe Manana 		test_msg("Expected region not marked as free\n");
58720005523SFilipe Manana 		return -ENOENT;
58820005523SFilipe Manana 	}
58920005523SFilipe Manana 
590b9ef22deSFeifei Xu 	if (cache->free_space_ctl->free_space != (SZ_1M + sectorsize)) {
591b9ef22deSFeifei Xu 		test_msg("Cache free space is not 1Mb + %u\n", sectorsize);
59220005523SFilipe Manana 		return -EINVAL;
59320005523SFilipe Manana 	}
59420005523SFilipe Manana 
59520005523SFilipe Manana 	offset = btrfs_find_space_for_alloc(cache,
596ee22184bSByongho Lee 					    0, SZ_1M, 0,
59720005523SFilipe Manana 					    &max_extent_size);
598ee22184bSByongho Lee 	if (offset != (SZ_128M - SZ_256K)) {
59920005523SFilipe Manana 		test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
60020005523SFilipe Manana 			 offset);
60120005523SFilipe Manana 		return -EINVAL;
60220005523SFilipe Manana 	}
60320005523SFilipe Manana 
604b9ef22deSFeifei Xu 	/*
605b9ef22deSFeifei Xu 	 * All that remains is a sectorsize free space region in a bitmap.
606b9ef22deSFeifei Xu 	 * Confirm.
607b9ef22deSFeifei Xu 	 */
60820005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 1, 1);
60920005523SFilipe Manana 	if (ret)
61020005523SFilipe Manana 		return ret;
61120005523SFilipe Manana 
612b9ef22deSFeifei Xu 	if (cache->free_space_ctl->free_space != sectorsize) {
613b9ef22deSFeifei Xu 		test_msg("Cache free space is not %u\n", sectorsize);
61420005523SFilipe Manana 		return -EINVAL;
61520005523SFilipe Manana 	}
61620005523SFilipe Manana 
61720005523SFilipe Manana 	offset = btrfs_find_space_for_alloc(cache,
618b9ef22deSFeifei Xu 					    0, sectorsize, 0,
61920005523SFilipe Manana 					    &max_extent_size);
620ee22184bSByongho Lee 	if (offset != (SZ_128M + SZ_16M)) {
621b9ef22deSFeifei Xu 		test_msg("Failed to allocate %u, returned offset : %llu\n",
622b9ef22deSFeifei Xu 			 sectorsize, offset);
62320005523SFilipe Manana 		return -EINVAL;
62420005523SFilipe Manana 	}
62520005523SFilipe Manana 
62620005523SFilipe Manana 	ret = check_cache_empty(cache);
62720005523SFilipe Manana 	if (ret)
62820005523SFilipe Manana 		return ret;
62920005523SFilipe Manana 
63020005523SFilipe Manana 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
63120005523SFilipe Manana 
63220005523SFilipe Manana 	/*
63320005523SFilipe Manana 	 * Now test a similar scenario, but where our extent entry is located
63420005523SFilipe Manana 	 * to the right of the bitmap entry, so that we can check that stealing
63520005523SFilipe Manana 	 * space from a bitmap to the front of an extent entry works.
63620005523SFilipe Manana 	 */
63720005523SFilipe Manana 
63820005523SFilipe Manana 	/*
63920005523SFilipe Manana 	 * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
64020005523SFilipe Manana 	 */
641ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0);
64220005523SFilipe Manana 	if (ret) {
64320005523SFilipe Manana 		test_msg("Couldn't add extent entry %d\n", ret);
64420005523SFilipe Manana 		return ret;
64520005523SFilipe Manana 	}
64620005523SFilipe Manana 
64720005523SFilipe Manana 	/* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
648ee22184bSByongho Lee 	ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1);
64920005523SFilipe Manana 	if (ret) {
65020005523SFilipe Manana 		test_msg("Couldn't add bitmap entry %d\n", ret);
65120005523SFilipe Manana 		return ret;
65220005523SFilipe Manana 	}
65320005523SFilipe Manana 
65420005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
65520005523SFilipe Manana 	if (ret)
65620005523SFilipe Manana 		return ret;
65720005523SFilipe Manana 
65820005523SFilipe Manana 	/*
65920005523SFilipe Manana 	 * Now make only the last 256Kb of the bitmap marked as free, so that
66020005523SFilipe Manana 	 * we end up with only the following ranges marked as free space:
66120005523SFilipe Manana 	 *
66220005523SFilipe Manana 	 * [128Mb + 128b, 128Mb + 256Kb[
66320005523SFilipe Manana 	 * [128Mb - 768Kb, 128Mb - 512Kb[
66420005523SFilipe Manana 	 */
665ee22184bSByongho Lee 	ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K);
66620005523SFilipe Manana 	if (ret) {
66720005523SFilipe Manana 		test_msg("Failed to free part of bitmap space %d\n", ret);
66820005523SFilipe Manana 		return ret;
66920005523SFilipe Manana 	}
67020005523SFilipe Manana 
67120005523SFilipe Manana 	/* Confirm that only those 2 ranges are marked as free. */
672ee22184bSByongho Lee 	if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) {
67320005523SFilipe Manana 		test_msg("Free space range missing\n");
67420005523SFilipe Manana 		return -ENOENT;
67520005523SFilipe Manana 	}
676ee22184bSByongho Lee 	if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) {
67720005523SFilipe Manana 		test_msg("Free space range missing\n");
67820005523SFilipe Manana 		return -ENOENT;
67920005523SFilipe Manana 	}
68020005523SFilipe Manana 
68120005523SFilipe Manana 	/*
68220005523SFilipe Manana 	 * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
68320005523SFilipe Manana 	 * as free anymore.
68420005523SFilipe Manana 	 */
685ee22184bSByongho Lee 	if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) {
68620005523SFilipe Manana 		test_msg("Bitmap region not removed from space cache\n");
68720005523SFilipe Manana 		return -EINVAL;
68820005523SFilipe Manana 	}
68920005523SFilipe Manana 
69020005523SFilipe Manana 	/*
69120005523SFilipe Manana 	 * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
69220005523SFilipe Manana 	 * covered by the bitmap, isn't marked as free.
69320005523SFilipe Manana 	 */
694ee22184bSByongho Lee 	if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
69520005523SFilipe Manana 		test_msg("Invalid bitmap region marked as free\n");
69620005523SFilipe Manana 		return -EINVAL;
69720005523SFilipe Manana 	}
69820005523SFilipe Manana 
69920005523SFilipe Manana 	/*
70020005523SFilipe Manana 	 * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
70120005523SFilipe Manana 	 * lets make sure the free space cache marks it as free in the bitmap,
70220005523SFilipe Manana 	 * and doesn't insert a new extent entry to represent this region.
70320005523SFilipe Manana 	 */
704ee22184bSByongho Lee 	ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K);
70520005523SFilipe Manana 	if (ret) {
70620005523SFilipe Manana 		test_msg("Error adding free space: %d\n", ret);
70720005523SFilipe Manana 		return ret;
70820005523SFilipe Manana 	}
70920005523SFilipe Manana 	/* Confirm the region is marked as free. */
710ee22184bSByongho Lee 	if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
71120005523SFilipe Manana 		test_msg("Bitmap region not marked as free\n");
71220005523SFilipe Manana 		return -ENOENT;
71320005523SFilipe Manana 	}
71420005523SFilipe Manana 
71520005523SFilipe Manana 	/*
71620005523SFilipe Manana 	 * Confirm that no new extent entries or bitmap entries were added to
71720005523SFilipe Manana 	 * the cache after adding that free space region.
71820005523SFilipe Manana 	 */
71920005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
72020005523SFilipe Manana 	if (ret)
72120005523SFilipe Manana 		return ret;
72220005523SFilipe Manana 
72320005523SFilipe Manana 	/*
72420005523SFilipe Manana 	 * Now lets add a small free space region to the left of the previous
72520005523SFilipe Manana 	 * one, which is not contiguous with it and is part of the bitmap too.
72620005523SFilipe Manana 	 * The goal is to test that the bitmap entry space stealing doesn't
72720005523SFilipe Manana 	 * steal this space region.
72820005523SFilipe Manana 	 */
729b9ef22deSFeifei Xu 	ret = btrfs_add_free_space(cache, SZ_32M, 2 * sectorsize);
73020005523SFilipe Manana 	if (ret) {
73120005523SFilipe Manana 		test_msg("Error adding free space: %d\n", ret);
73220005523SFilipe Manana 		return ret;
73320005523SFilipe Manana 	}
73420005523SFilipe Manana 
73520005523SFilipe Manana 	/*
73620005523SFilipe Manana 	 * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
73720005523SFilipe Manana 	 * expand the range covered by the existing extent entry that represents
73820005523SFilipe Manana 	 * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
73920005523SFilipe Manana 	 */
740ee22184bSByongho Lee 	ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K);
74120005523SFilipe Manana 	if (ret) {
74220005523SFilipe Manana 		test_msg("Error adding free space: %d\n", ret);
74320005523SFilipe Manana 		return ret;
74420005523SFilipe Manana 	}
74520005523SFilipe Manana 	/* Confirm the region is marked as free. */
746ee22184bSByongho Lee 	if (!test_check_exists(cache, SZ_128M, SZ_128K)) {
74720005523SFilipe Manana 		test_msg("Extent region not marked as free\n");
74820005523SFilipe Manana 		return -ENOENT;
74920005523SFilipe Manana 	}
75020005523SFilipe Manana 
75120005523SFilipe Manana 	/*
75220005523SFilipe Manana 	 * Confirm that our extent entry didn't stole all free space from the
753b9ef22deSFeifei Xu 	 * bitmap, because of the small 2 * sectorsize free space region.
75420005523SFilipe Manana 	 */
75520005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
75620005523SFilipe Manana 	if (ret)
75720005523SFilipe Manana 		return ret;
75820005523SFilipe Manana 
75920005523SFilipe Manana 	/*
76020005523SFilipe Manana 	 * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
76120005523SFilipe Manana 	 * space. Without stealing bitmap free space into extent entry space,
76220005523SFilipe Manana 	 * we would have all this free space represented by 2 entries in the
76320005523SFilipe Manana 	 * cache:
76420005523SFilipe Manana 	 *
76520005523SFilipe Manana 	 * extent entry covering range: [128Mb, 128Mb + 256Kb[
76620005523SFilipe Manana 	 * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
76720005523SFilipe Manana 	 *
76820005523SFilipe Manana 	 * Attempting to allocate the whole free space (1Mb) would fail, because
76920005523SFilipe Manana 	 * we can't allocate from multiple entries.
77020005523SFilipe Manana 	 * With the bitmap free space stealing, we get a single extent entry
77120005523SFilipe Manana 	 * that represents the 1Mb free space, and therefore we're able to
77220005523SFilipe Manana 	 * allocate the whole free space at once.
77320005523SFilipe Manana 	 */
774ee22184bSByongho Lee 	if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) {
77520005523SFilipe Manana 		test_msg("Expected region not marked as free\n");
77620005523SFilipe Manana 		return -ENOENT;
77720005523SFilipe Manana 	}
77820005523SFilipe Manana 
779b9ef22deSFeifei Xu 	if (cache->free_space_ctl->free_space != (SZ_1M + 2 * sectorsize)) {
780b9ef22deSFeifei Xu 		test_msg("Cache free space is not 1Mb + %u\n", 2 * sectorsize);
78120005523SFilipe Manana 		return -EINVAL;
78220005523SFilipe Manana 	}
78320005523SFilipe Manana 
784ee22184bSByongho Lee 	offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0,
78520005523SFilipe Manana 					    &max_extent_size);
786ee22184bSByongho Lee 	if (offset != (SZ_128M - 768 * SZ_1K)) {
78720005523SFilipe Manana 		test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
78820005523SFilipe Manana 			 offset);
78920005523SFilipe Manana 		return -EINVAL;
79020005523SFilipe Manana 	}
79120005523SFilipe Manana 
792b9ef22deSFeifei Xu 	/*
793b9ef22deSFeifei Xu 	 * All that remains is 2 * sectorsize free space region
794b9ef22deSFeifei Xu 	 * in a bitmap. Confirm.
795b9ef22deSFeifei Xu 	 */
79620005523SFilipe Manana 	ret = check_num_extents_and_bitmaps(cache, 1, 1);
79720005523SFilipe Manana 	if (ret)
79820005523SFilipe Manana 		return ret;
79920005523SFilipe Manana 
800b9ef22deSFeifei Xu 	if (cache->free_space_ctl->free_space != 2 * sectorsize) {
801b9ef22deSFeifei Xu 		test_msg("Cache free space is not %u\n", 2 * sectorsize);
80220005523SFilipe Manana 		return -EINVAL;
80320005523SFilipe Manana 	}
80420005523SFilipe Manana 
80520005523SFilipe Manana 	offset = btrfs_find_space_for_alloc(cache,
806b9ef22deSFeifei Xu 					    0, 2 * sectorsize, 0,
80720005523SFilipe Manana 					    &max_extent_size);
808ee22184bSByongho Lee 	if (offset != SZ_32M) {
809b9ef22deSFeifei Xu 		test_msg("Failed to allocate %u, offset: %llu\n",
810b9ef22deSFeifei Xu 			 2 * sectorsize,
81120005523SFilipe Manana 			 offset);
81220005523SFilipe Manana 		return -EINVAL;
81320005523SFilipe Manana 	}
81420005523SFilipe Manana 
81520005523SFilipe Manana 	ret = check_cache_empty(cache);
81620005523SFilipe Manana 	if (ret)
81720005523SFilipe Manana 		return ret;
81820005523SFilipe Manana 
81928f0779aSDavid Sterba 	cache->free_space_ctl->op = orig_free_space_ops;
82020005523SFilipe Manana 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
82120005523SFilipe Manana 
82220005523SFilipe Manana 	return 0;
82320005523SFilipe Manana }
82420005523SFilipe Manana 
825b9ef22deSFeifei Xu int btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize)
826dc11dd5dSJosef Bacik {
8277c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info;
828dc11dd5dSJosef Bacik 	struct btrfs_block_group_cache *cache;
829d0bd4560SJosef Bacik 	struct btrfs_root *root = NULL;
830d0bd4560SJosef Bacik 	int ret = -ENOMEM;
831dc11dd5dSJosef Bacik 
832dc11dd5dSJosef Bacik 	test_msg("Running btrfs free space cache tests\n");
833da17066cSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
834da17066cSJeff Mahoney 	if (!fs_info)
835da17066cSJeff Mahoney 		return -ENOMEM;
836da17066cSJeff Mahoney 
837dc11dd5dSJosef Bacik 
83836b3dc05SFeifei Xu 	/*
83936b3dc05SFeifei Xu 	 * For ppc64 (with 64k page size), bytes per bitmap might be
84036b3dc05SFeifei Xu 	 * larger than 1G.  To make bitmap test available in ppc64,
84136b3dc05SFeifei Xu 	 * alloc dummy block group whose size cross bitmaps.
84236b3dc05SFeifei Xu 	 */
843da17066cSJeff Mahoney 	cache = btrfs_alloc_dummy_block_group(fs_info,
844da17066cSJeff Mahoney 				      BITS_PER_BITMAP * sectorsize + PAGE_SIZE);
845dc11dd5dSJosef Bacik 	if (!cache) {
846dc11dd5dSJosef Bacik 		test_msg("Couldn't run the tests\n");
847da17066cSJeff Mahoney 		btrfs_free_dummy_fs_info(fs_info);
848dc11dd5dSJosef Bacik 		return 0;
849dc11dd5dSJosef Bacik 	}
850dc11dd5dSJosef Bacik 
851da17066cSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info);
85289b6c8d1SDan Carpenter 	if (IS_ERR(root)) {
85389b6c8d1SDan Carpenter 		ret = PTR_ERR(root);
854d0bd4560SJosef Bacik 		goto out;
85589b6c8d1SDan Carpenter 	}
856d0bd4560SJosef Bacik 
857d0bd4560SJosef Bacik 	root->fs_info->extent_root = root;
858d0bd4560SJosef Bacik 
859dc11dd5dSJosef Bacik 	ret = test_extents(cache);
860dc11dd5dSJosef Bacik 	if (ret)
861dc11dd5dSJosef Bacik 		goto out;
862b9ef22deSFeifei Xu 	ret = test_bitmaps(cache, sectorsize);
863dc11dd5dSJosef Bacik 	if (ret)
864dc11dd5dSJosef Bacik 		goto out;
865b9ef22deSFeifei Xu 	ret = test_bitmaps_and_extents(cache, sectorsize);
866dc11dd5dSJosef Bacik 	if (ret)
867dc11dd5dSJosef Bacik 		goto out;
86820005523SFilipe Manana 
869b9ef22deSFeifei Xu 	ret = test_steal_space_from_bitmap_to_extent(cache, sectorsize);
870dc11dd5dSJosef Bacik out:
8717c55ee0cSOmar Sandoval 	btrfs_free_dummy_block_group(cache);
872d0bd4560SJosef Bacik 	btrfs_free_dummy_root(root);
8737c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
874dc11dd5dSJosef Bacik 	test_msg("Free space cache tests finished\n");
875dc11dd5dSJosef Bacik 	return ret;
876dc11dd5dSJosef Bacik }
877