xref: /OK3568_Linux_fs/kernel/fs/btrfs/tests/free-space-tests.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2013 Fusion IO.  All rights reserved.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/slab.h>
7*4882a593Smuzhiyun #include "btrfs-tests.h"
8*4882a593Smuzhiyun #include "../ctree.h"
9*4882a593Smuzhiyun #include "../disk-io.h"
10*4882a593Smuzhiyun #include "../free-space-cache.h"
11*4882a593Smuzhiyun #include "../block-group.h"
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #define BITS_PER_BITMAP		(PAGE_SIZE * 8UL)
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun  * This test just does basic sanity checking, making sure we can add an extent
17*4882a593Smuzhiyun  * entry and remove space from either end and the middle, and make sure we can
18*4882a593Smuzhiyun  * remove space that covers adjacent extent entries.
19*4882a593Smuzhiyun  */
test_extents(struct btrfs_block_group * cache)20*4882a593Smuzhiyun static int test_extents(struct btrfs_block_group *cache)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun 	int ret = 0;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	test_msg("running extent only tests");
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	/* First just make sure we can remove an entire entry */
27*4882a593Smuzhiyun 	ret = btrfs_add_free_space(cache, 0, SZ_4M);
28*4882a593Smuzhiyun 	if (ret) {
29*4882a593Smuzhiyun 		test_err("error adding initial extents %d", ret);
30*4882a593Smuzhiyun 		return ret;
31*4882a593Smuzhiyun 	}
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, 0, SZ_4M);
34*4882a593Smuzhiyun 	if (ret) {
35*4882a593Smuzhiyun 		test_err("error removing extent %d", ret);
36*4882a593Smuzhiyun 		return ret;
37*4882a593Smuzhiyun 	}
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	if (test_check_exists(cache, 0, SZ_4M)) {
40*4882a593Smuzhiyun 		test_err("full remove left some lingering space");
41*4882a593Smuzhiyun 		return -1;
42*4882a593Smuzhiyun 	}
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	/* Ok edge and middle cases now */
45*4882a593Smuzhiyun 	ret = btrfs_add_free_space(cache, 0, SZ_4M);
46*4882a593Smuzhiyun 	if (ret) {
47*4882a593Smuzhiyun 		test_err("error adding half extent %d", ret);
48*4882a593Smuzhiyun 		return ret;
49*4882a593Smuzhiyun 	}
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_1M);
52*4882a593Smuzhiyun 	if (ret) {
53*4882a593Smuzhiyun 		test_err("error removing tail end %d", ret);
54*4882a593Smuzhiyun 		return ret;
55*4882a593Smuzhiyun 	}
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, 0, SZ_1M);
58*4882a593Smuzhiyun 	if (ret) {
59*4882a593Smuzhiyun 		test_err("error removing front end %d", ret);
60*4882a593Smuzhiyun 		return ret;
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, SZ_2M, 4096);
64*4882a593Smuzhiyun 	if (ret) {
65*4882a593Smuzhiyun 		test_err("error removing middle piece %d", ret);
66*4882a593Smuzhiyun 		return ret;
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	if (test_check_exists(cache, 0, SZ_1M)) {
70*4882a593Smuzhiyun 		test_err("still have space at the front");
71*4882a593Smuzhiyun 		return -1;
72*4882a593Smuzhiyun 	}
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	if (test_check_exists(cache, SZ_2M, 4096)) {
75*4882a593Smuzhiyun 		test_err("still have space in the middle");
76*4882a593Smuzhiyun 		return -1;
77*4882a593Smuzhiyun 	}
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	if (test_check_exists(cache, 3 * SZ_1M, SZ_1M)) {
80*4882a593Smuzhiyun 		test_err("still have space at the end");
81*4882a593Smuzhiyun 		return -1;
82*4882a593Smuzhiyun 	}
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	/* Cleanup */
85*4882a593Smuzhiyun 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	return 0;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
test_bitmaps(struct btrfs_block_group * cache,u32 sectorsize)90*4882a593Smuzhiyun static int test_bitmaps(struct btrfs_block_group *cache, u32 sectorsize)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	u64 next_bitmap_offset;
93*4882a593Smuzhiyun 	int ret;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	test_msg("running bitmap only tests");
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
98*4882a593Smuzhiyun 	if (ret) {
99*4882a593Smuzhiyun 		test_err("couldn't create a bitmap entry %d", ret);
100*4882a593Smuzhiyun 		return ret;
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, 0, SZ_4M);
104*4882a593Smuzhiyun 	if (ret) {
105*4882a593Smuzhiyun 		test_err("error removing bitmap full range %d", ret);
106*4882a593Smuzhiyun 		return ret;
107*4882a593Smuzhiyun 	}
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	if (test_check_exists(cache, 0, SZ_4M)) {
110*4882a593Smuzhiyun 		test_err("left some space in bitmap");
111*4882a593Smuzhiyun 		return -1;
112*4882a593Smuzhiyun 	}
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
115*4882a593Smuzhiyun 	if (ret) {
116*4882a593Smuzhiyun 		test_err("couldn't add to our bitmap entry %d", ret);
117*4882a593Smuzhiyun 		return ret;
118*4882a593Smuzhiyun 	}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, SZ_1M, SZ_2M);
121*4882a593Smuzhiyun 	if (ret) {
122*4882a593Smuzhiyun 		test_err("couldn't remove middle chunk %d", ret);
123*4882a593Smuzhiyun 		return ret;
124*4882a593Smuzhiyun 	}
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	/*
127*4882a593Smuzhiyun 	 * The first bitmap we have starts at offset 0 so the next one is just
128*4882a593Smuzhiyun 	 * at the end of the first bitmap.
129*4882a593Smuzhiyun 	 */
130*4882a593Smuzhiyun 	next_bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	/* Test a bit straddling two bitmaps */
133*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, next_bitmap_offset - SZ_2M,
134*4882a593Smuzhiyun 					SZ_4M, 1);
135*4882a593Smuzhiyun 	if (ret) {
136*4882a593Smuzhiyun 		test_err("couldn't add space that straddles two bitmaps %d",
137*4882a593Smuzhiyun 				ret);
138*4882a593Smuzhiyun 		return ret;
139*4882a593Smuzhiyun 	}
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, next_bitmap_offset - SZ_1M, SZ_2M);
142*4882a593Smuzhiyun 	if (ret) {
143*4882a593Smuzhiyun 		test_err("couldn't remove overlapping space %d", ret);
144*4882a593Smuzhiyun 		return ret;
145*4882a593Smuzhiyun 	}
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	if (test_check_exists(cache, next_bitmap_offset - SZ_1M, SZ_2M)) {
148*4882a593Smuzhiyun 		test_err("left some space when removing overlapping");
149*4882a593Smuzhiyun 		return -1;
150*4882a593Smuzhiyun 	}
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	return 0;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /* This is the high grade jackassery */
test_bitmaps_and_extents(struct btrfs_block_group * cache,u32 sectorsize)158*4882a593Smuzhiyun static int test_bitmaps_and_extents(struct btrfs_block_group *cache,
159*4882a593Smuzhiyun 				    u32 sectorsize)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun 	u64 bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize);
162*4882a593Smuzhiyun 	int ret;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	test_msg("running bitmap and extent tests");
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	/*
167*4882a593Smuzhiyun 	 * First let's do something simple, an extent at the same offset as the
168*4882a593Smuzhiyun 	 * bitmap, but the free space completely in the extent and then
169*4882a593Smuzhiyun 	 * completely in the bitmap.
170*4882a593Smuzhiyun 	 */
171*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, SZ_4M, SZ_1M, 1);
172*4882a593Smuzhiyun 	if (ret) {
173*4882a593Smuzhiyun 		test_err("couldn't create bitmap entry %d", ret);
174*4882a593Smuzhiyun 		return ret;
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
178*4882a593Smuzhiyun 	if (ret) {
179*4882a593Smuzhiyun 		test_err("couldn't add extent entry %d", ret);
180*4882a593Smuzhiyun 		return ret;
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, 0, SZ_1M);
184*4882a593Smuzhiyun 	if (ret) {
185*4882a593Smuzhiyun 		test_err("couldn't remove extent entry %d", ret);
186*4882a593Smuzhiyun 		return ret;
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	if (test_check_exists(cache, 0, SZ_1M)) {
190*4882a593Smuzhiyun 		test_err("left remnants after our remove");
191*4882a593Smuzhiyun 		return -1;
192*4882a593Smuzhiyun 	}
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	/* Now to add back the extent entry and remove from the bitmap */
195*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
196*4882a593Smuzhiyun 	if (ret) {
197*4882a593Smuzhiyun 		test_err("couldn't re-add extent entry %d", ret);
198*4882a593Smuzhiyun 		return ret;
199*4882a593Smuzhiyun 	}
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, SZ_4M, SZ_1M);
202*4882a593Smuzhiyun 	if (ret) {
203*4882a593Smuzhiyun 		test_err("couldn't remove from bitmap %d", ret);
204*4882a593Smuzhiyun 		return ret;
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	if (test_check_exists(cache, SZ_4M, SZ_1M)) {
208*4882a593Smuzhiyun 		test_err("left remnants in the bitmap");
209*4882a593Smuzhiyun 		return -1;
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	/*
213*4882a593Smuzhiyun 	 * Ok so a little more evil, extent entry and bitmap at the same offset,
214*4882a593Smuzhiyun 	 * removing an overlapping chunk.
215*4882a593Smuzhiyun 	 */
216*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, SZ_1M, SZ_4M, 1);
217*4882a593Smuzhiyun 	if (ret) {
218*4882a593Smuzhiyun 		test_err("couldn't add to a bitmap %d", ret);
219*4882a593Smuzhiyun 		return ret;
220*4882a593Smuzhiyun 	}
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, SZ_512K, 3 * SZ_1M);
223*4882a593Smuzhiyun 	if (ret) {
224*4882a593Smuzhiyun 		test_err("couldn't remove overlapping space %d", ret);
225*4882a593Smuzhiyun 		return ret;
226*4882a593Smuzhiyun 	}
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	if (test_check_exists(cache, SZ_512K, 3 * SZ_1M)) {
229*4882a593Smuzhiyun 		test_err("left over pieces after removing overlapping");
230*4882a593Smuzhiyun 		return -1;
231*4882a593Smuzhiyun 	}
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	/* Now with the extent entry offset into the bitmap */
236*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, SZ_4M, SZ_4M, 1);
237*4882a593Smuzhiyun 	if (ret) {
238*4882a593Smuzhiyun 		test_err("couldn't add space to the bitmap %d", ret);
239*4882a593Smuzhiyun 		return ret;
240*4882a593Smuzhiyun 	}
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, SZ_2M, SZ_2M, 0);
243*4882a593Smuzhiyun 	if (ret) {
244*4882a593Smuzhiyun 		test_err("couldn't add extent to the cache %d", ret);
245*4882a593Smuzhiyun 		return ret;
246*4882a593Smuzhiyun 	}
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_4M);
249*4882a593Smuzhiyun 	if (ret) {
250*4882a593Smuzhiyun 		test_err("problem removing overlapping space %d", ret);
251*4882a593Smuzhiyun 		return ret;
252*4882a593Smuzhiyun 	}
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	if (test_check_exists(cache, 3 * SZ_1M, SZ_4M)) {
255*4882a593Smuzhiyun 		test_err("left something behind when removing space");
256*4882a593Smuzhiyun 		return -1;
257*4882a593Smuzhiyun 	}
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	/*
260*4882a593Smuzhiyun 	 * This has blown up in the past, the extent entry starts before the
261*4882a593Smuzhiyun 	 * bitmap entry, but we're trying to remove an offset that falls
262*4882a593Smuzhiyun 	 * completely within the bitmap range and is in both the extent entry
263*4882a593Smuzhiyun 	 * and the bitmap entry, looks like this
264*4882a593Smuzhiyun 	 *
265*4882a593Smuzhiyun 	 *   [ extent ]
266*4882a593Smuzhiyun 	 *      [ bitmap ]
267*4882a593Smuzhiyun 	 *        [ del ]
268*4882a593Smuzhiyun 	 */
269*4882a593Smuzhiyun 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
270*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, bitmap_offset + SZ_4M, SZ_4M, 1);
271*4882a593Smuzhiyun 	if (ret) {
272*4882a593Smuzhiyun 		test_err("couldn't add bitmap %d", ret);
273*4882a593Smuzhiyun 		return ret;
274*4882a593Smuzhiyun 	}
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, bitmap_offset - SZ_1M,
277*4882a593Smuzhiyun 					5 * SZ_1M, 0);
278*4882a593Smuzhiyun 	if (ret) {
279*4882a593Smuzhiyun 		test_err("couldn't add extent entry %d", ret);
280*4882a593Smuzhiyun 		return ret;
281*4882a593Smuzhiyun 	}
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, bitmap_offset + SZ_1M, 5 * SZ_1M);
284*4882a593Smuzhiyun 	if (ret) {
285*4882a593Smuzhiyun 		test_err("failed to free our space %d", ret);
286*4882a593Smuzhiyun 		return ret;
287*4882a593Smuzhiyun 	}
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	if (test_check_exists(cache, bitmap_offset + SZ_1M, 5 * SZ_1M)) {
290*4882a593Smuzhiyun 		test_err("left stuff over");
291*4882a593Smuzhiyun 		return -1;
292*4882a593Smuzhiyun 	}
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	/*
297*4882a593Smuzhiyun 	 * This blew up before, we have part of the free space in a bitmap and
298*4882a593Smuzhiyun 	 * then the entirety of the rest of the space in an extent.  This used
299*4882a593Smuzhiyun 	 * to return -EAGAIN back from btrfs_remove_extent, make sure this
300*4882a593Smuzhiyun 	 * doesn't happen.
301*4882a593Smuzhiyun 	 */
302*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, SZ_1M, SZ_2M, 1);
303*4882a593Smuzhiyun 	if (ret) {
304*4882a593Smuzhiyun 		test_err("couldn't add bitmap entry %d", ret);
305*4882a593Smuzhiyun 		return ret;
306*4882a593Smuzhiyun 	}
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, 3 * SZ_1M, SZ_1M, 0);
309*4882a593Smuzhiyun 	if (ret) {
310*4882a593Smuzhiyun 		test_err("couldn't add extent entry %d", ret);
311*4882a593Smuzhiyun 		return ret;
312*4882a593Smuzhiyun 	}
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, SZ_1M, 3 * SZ_1M);
315*4882a593Smuzhiyun 	if (ret) {
316*4882a593Smuzhiyun 		test_err("error removing bitmap and extent overlapping %d", ret);
317*4882a593Smuzhiyun 		return ret;
318*4882a593Smuzhiyun 	}
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
321*4882a593Smuzhiyun 	return 0;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun /* Used by test_steal_space_from_bitmap_to_extent(). */
test_use_bitmap(struct btrfs_free_space_ctl * ctl,struct btrfs_free_space * info)325*4882a593Smuzhiyun static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl,
326*4882a593Smuzhiyun 			    struct btrfs_free_space *info)
327*4882a593Smuzhiyun {
328*4882a593Smuzhiyun 	return ctl->free_extents > 0;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun /* Used by test_steal_space_from_bitmap_to_extent(). */
332*4882a593Smuzhiyun static int
check_num_extents_and_bitmaps(const struct btrfs_block_group * cache,const int num_extents,const int num_bitmaps)333*4882a593Smuzhiyun check_num_extents_and_bitmaps(const struct btrfs_block_group *cache,
334*4882a593Smuzhiyun 			      const int num_extents,
335*4882a593Smuzhiyun 			      const int num_bitmaps)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	if (cache->free_space_ctl->free_extents != num_extents) {
338*4882a593Smuzhiyun 		test_err(
339*4882a593Smuzhiyun 		"incorrect # of extent entries in the cache: %d, expected %d",
340*4882a593Smuzhiyun 			 cache->free_space_ctl->free_extents, num_extents);
341*4882a593Smuzhiyun 		return -EINVAL;
342*4882a593Smuzhiyun 	}
343*4882a593Smuzhiyun 	if (cache->free_space_ctl->total_bitmaps != num_bitmaps) {
344*4882a593Smuzhiyun 		test_err(
345*4882a593Smuzhiyun 		"incorrect # of extent entries in the cache: %d, expected %d",
346*4882a593Smuzhiyun 			 cache->free_space_ctl->total_bitmaps, num_bitmaps);
347*4882a593Smuzhiyun 		return -EINVAL;
348*4882a593Smuzhiyun 	}
349*4882a593Smuzhiyun 	return 0;
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun /* Used by test_steal_space_from_bitmap_to_extent(). */
check_cache_empty(struct btrfs_block_group * cache)353*4882a593Smuzhiyun static int check_cache_empty(struct btrfs_block_group *cache)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	u64 offset;
356*4882a593Smuzhiyun 	u64 max_extent_size;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	/*
359*4882a593Smuzhiyun 	 * Now lets confirm that there's absolutely no free space left to
360*4882a593Smuzhiyun 	 * allocate.
361*4882a593Smuzhiyun 	 */
362*4882a593Smuzhiyun 	if (cache->free_space_ctl->free_space != 0) {
363*4882a593Smuzhiyun 		test_err("cache free space is not 0");
364*4882a593Smuzhiyun 		return -EINVAL;
365*4882a593Smuzhiyun 	}
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	/* And any allocation request, no matter how small, should fail now. */
368*4882a593Smuzhiyun 	offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0,
369*4882a593Smuzhiyun 					    &max_extent_size);
370*4882a593Smuzhiyun 	if (offset != 0) {
371*4882a593Smuzhiyun 		test_err("space allocation did not fail, returned offset: %llu",
372*4882a593Smuzhiyun 			 offset);
373*4882a593Smuzhiyun 		return -EINVAL;
374*4882a593Smuzhiyun 	}
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	/* And no extent nor bitmap entries in the cache anymore. */
377*4882a593Smuzhiyun 	return check_num_extents_and_bitmaps(cache, 0, 0);
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun /*
381*4882a593Smuzhiyun  * Before we were able to steal free space from a bitmap entry to an extent
382*4882a593Smuzhiyun  * entry, we could end up with 2 entries representing a contiguous free space.
383*4882a593Smuzhiyun  * One would be an extent entry and the other a bitmap entry. Since in order
384*4882a593Smuzhiyun  * to allocate space to a caller we use only 1 entry, we couldn't return that
385*4882a593Smuzhiyun  * whole range to the caller if it was requested. This forced the caller to
386*4882a593Smuzhiyun  * either assume ENOSPC or perform several smaller space allocations, which
387*4882a593Smuzhiyun  * wasn't optimal as they could be spread all over the block group while under
388*4882a593Smuzhiyun  * concurrency (extra overhead and fragmentation).
389*4882a593Smuzhiyun  *
390*4882a593Smuzhiyun  * This stealing approach is beneficial, since we always prefer to allocate
391*4882a593Smuzhiyun  * from extent entries, both for clustered and non-clustered allocation
392*4882a593Smuzhiyun  * requests.
393*4882a593Smuzhiyun  */
394*4882a593Smuzhiyun static int
test_steal_space_from_bitmap_to_extent(struct btrfs_block_group * cache,u32 sectorsize)395*4882a593Smuzhiyun test_steal_space_from_bitmap_to_extent(struct btrfs_block_group *cache,
396*4882a593Smuzhiyun 				       u32 sectorsize)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun 	int ret;
399*4882a593Smuzhiyun 	u64 offset;
400*4882a593Smuzhiyun 	u64 max_extent_size;
401*4882a593Smuzhiyun 	const struct btrfs_free_space_op test_free_space_ops = {
402*4882a593Smuzhiyun 		.recalc_thresholds = cache->free_space_ctl->op->recalc_thresholds,
403*4882a593Smuzhiyun 		.use_bitmap = test_use_bitmap,
404*4882a593Smuzhiyun 	};
405*4882a593Smuzhiyun 	const struct btrfs_free_space_op *orig_free_space_ops;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	test_msg("running space stealing from bitmap to extent tests");
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	/*
410*4882a593Smuzhiyun 	 * For this test, we want to ensure we end up with an extent entry
411*4882a593Smuzhiyun 	 * immediately adjacent to a bitmap entry, where the bitmap starts
412*4882a593Smuzhiyun 	 * at an offset where the extent entry ends. We keep adding and
413*4882a593Smuzhiyun 	 * removing free space to reach into this state, but to get there
414*4882a593Smuzhiyun 	 * we need to reach a point where marking new free space doesn't
415*4882a593Smuzhiyun 	 * result in adding new extent entries or merging the new space
416*4882a593Smuzhiyun 	 * with existing extent entries - the space ends up being marked
417*4882a593Smuzhiyun 	 * in an existing bitmap that covers the new free space range.
418*4882a593Smuzhiyun 	 *
419*4882a593Smuzhiyun 	 * To get there, we need to reach the threshold defined set at
420*4882a593Smuzhiyun 	 * cache->free_space_ctl->extents_thresh, which currently is
421*4882a593Smuzhiyun 	 * 256 extents on a x86_64 system at least, and a few other
422*4882a593Smuzhiyun 	 * conditions (check free_space_cache.c). Instead of making the
423*4882a593Smuzhiyun 	 * test much longer and complicated, use a "use_bitmap" operation
424*4882a593Smuzhiyun 	 * that forces use of bitmaps as soon as we have at least 1
425*4882a593Smuzhiyun 	 * extent entry.
426*4882a593Smuzhiyun 	 */
427*4882a593Smuzhiyun 	orig_free_space_ops = cache->free_space_ctl->op;
428*4882a593Smuzhiyun 	cache->free_space_ctl->op = &test_free_space_ops;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	/*
431*4882a593Smuzhiyun 	 * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
432*4882a593Smuzhiyun 	 */
433*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0);
434*4882a593Smuzhiyun 	if (ret) {
435*4882a593Smuzhiyun 		test_err("couldn't add extent entry %d", ret);
436*4882a593Smuzhiyun 		return ret;
437*4882a593Smuzhiyun 	}
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	/* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
440*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K,
441*4882a593Smuzhiyun 					SZ_128M - SZ_512K, 1);
442*4882a593Smuzhiyun 	if (ret) {
443*4882a593Smuzhiyun 		test_err("couldn't add bitmap entry %d", ret);
444*4882a593Smuzhiyun 		return ret;
445*4882a593Smuzhiyun 	}
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
448*4882a593Smuzhiyun 	if (ret)
449*4882a593Smuzhiyun 		return ret;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	/*
452*4882a593Smuzhiyun 	 * Now make only the first 256Kb of the bitmap marked as free, so that
453*4882a593Smuzhiyun 	 * we end up with only the following ranges marked as free space:
454*4882a593Smuzhiyun 	 *
455*4882a593Smuzhiyun 	 * [128Mb - 256Kb, 128Mb - 128Kb[
456*4882a593Smuzhiyun 	 * [128Mb + 512Kb, 128Mb + 768Kb[
457*4882a593Smuzhiyun 	 */
458*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache,
459*4882a593Smuzhiyun 				      SZ_128M + 768 * SZ_1K,
460*4882a593Smuzhiyun 				      SZ_128M - 768 * SZ_1K);
461*4882a593Smuzhiyun 	if (ret) {
462*4882a593Smuzhiyun 		test_err("failed to free part of bitmap space %d", ret);
463*4882a593Smuzhiyun 		return ret;
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	/* Confirm that only those 2 ranges are marked as free. */
467*4882a593Smuzhiyun 	if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) {
468*4882a593Smuzhiyun 		test_err("free space range missing");
469*4882a593Smuzhiyun 		return -ENOENT;
470*4882a593Smuzhiyun 	}
471*4882a593Smuzhiyun 	if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) {
472*4882a593Smuzhiyun 		test_err("free space range missing");
473*4882a593Smuzhiyun 		return -ENOENT;
474*4882a593Smuzhiyun 	}
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	/*
477*4882a593Smuzhiyun 	 * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
478*4882a593Smuzhiyun 	 * as free anymore.
479*4882a593Smuzhiyun 	 */
480*4882a593Smuzhiyun 	if (test_check_exists(cache, SZ_128M + 768 * SZ_1K,
481*4882a593Smuzhiyun 			      SZ_128M - 768 * SZ_1K)) {
482*4882a593Smuzhiyun 		test_err("bitmap region not removed from space cache");
483*4882a593Smuzhiyun 		return -EINVAL;
484*4882a593Smuzhiyun 	}
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	/*
487*4882a593Smuzhiyun 	 * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
488*4882a593Smuzhiyun 	 * covered by the bitmap, isn't marked as free.
489*4882a593Smuzhiyun 	 */
490*4882a593Smuzhiyun 	if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) {
491*4882a593Smuzhiyun 		test_err("invalid bitmap region marked as free");
492*4882a593Smuzhiyun 		return -EINVAL;
493*4882a593Smuzhiyun 	}
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	/*
496*4882a593Smuzhiyun 	 * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
497*4882a593Smuzhiyun 	 * by the bitmap too, isn't marked as free either.
498*4882a593Smuzhiyun 	 */
499*4882a593Smuzhiyun 	if (test_check_exists(cache, SZ_128M, SZ_256K)) {
500*4882a593Smuzhiyun 		test_err("invalid bitmap region marked as free");
501*4882a593Smuzhiyun 		return -EINVAL;
502*4882a593Smuzhiyun 	}
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	/*
505*4882a593Smuzhiyun 	 * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
506*4882a593Smuzhiyun 	 * lets make sure the free space cache marks it as free in the bitmap,
507*4882a593Smuzhiyun 	 * and doesn't insert a new extent entry to represent this region.
508*4882a593Smuzhiyun 	 */
509*4882a593Smuzhiyun 	ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K);
510*4882a593Smuzhiyun 	if (ret) {
511*4882a593Smuzhiyun 		test_err("error adding free space: %d", ret);
512*4882a593Smuzhiyun 		return ret;
513*4882a593Smuzhiyun 	}
514*4882a593Smuzhiyun 	/* Confirm the region is marked as free. */
515*4882a593Smuzhiyun 	if (!test_check_exists(cache, SZ_128M, SZ_512K)) {
516*4882a593Smuzhiyun 		test_err("bitmap region not marked as free");
517*4882a593Smuzhiyun 		return -ENOENT;
518*4882a593Smuzhiyun 	}
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	/*
521*4882a593Smuzhiyun 	 * Confirm that no new extent entries or bitmap entries were added to
522*4882a593Smuzhiyun 	 * the cache after adding that free space region.
523*4882a593Smuzhiyun 	 */
524*4882a593Smuzhiyun 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
525*4882a593Smuzhiyun 	if (ret)
526*4882a593Smuzhiyun 		return ret;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	/*
529*4882a593Smuzhiyun 	 * Now lets add a small free space region to the right of the previous
530*4882a593Smuzhiyun 	 * one, which is not contiguous with it and is part of the bitmap too.
531*4882a593Smuzhiyun 	 * The goal is to test that the bitmap entry space stealing doesn't
532*4882a593Smuzhiyun 	 * steal this space region.
533*4882a593Smuzhiyun 	 */
534*4882a593Smuzhiyun 	ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, sectorsize);
535*4882a593Smuzhiyun 	if (ret) {
536*4882a593Smuzhiyun 		test_err("error adding free space: %d", ret);
537*4882a593Smuzhiyun 		return ret;
538*4882a593Smuzhiyun 	}
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	/*
541*4882a593Smuzhiyun 	 * Confirm that no new extent entries or bitmap entries were added to
542*4882a593Smuzhiyun 	 * the cache after adding that free space region.
543*4882a593Smuzhiyun 	 */
544*4882a593Smuzhiyun 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
545*4882a593Smuzhiyun 	if (ret)
546*4882a593Smuzhiyun 		return ret;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	/*
549*4882a593Smuzhiyun 	 * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
550*4882a593Smuzhiyun 	 * expand the range covered by the existing extent entry that represents
551*4882a593Smuzhiyun 	 * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
552*4882a593Smuzhiyun 	 */
553*4882a593Smuzhiyun 	ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K);
554*4882a593Smuzhiyun 	if (ret) {
555*4882a593Smuzhiyun 		test_err("error adding free space: %d", ret);
556*4882a593Smuzhiyun 		return ret;
557*4882a593Smuzhiyun 	}
558*4882a593Smuzhiyun 	/* Confirm the region is marked as free. */
559*4882a593Smuzhiyun 	if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) {
560*4882a593Smuzhiyun 		test_err("extent region not marked as free");
561*4882a593Smuzhiyun 		return -ENOENT;
562*4882a593Smuzhiyun 	}
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	/*
565*4882a593Smuzhiyun 	 * Confirm that our extent entry didn't stole all free space from the
566*4882a593Smuzhiyun 	 * bitmap, because of the small 4Kb free space region.
567*4882a593Smuzhiyun 	 */
568*4882a593Smuzhiyun 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
569*4882a593Smuzhiyun 	if (ret)
570*4882a593Smuzhiyun 		return ret;
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	/*
573*4882a593Smuzhiyun 	 * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
574*4882a593Smuzhiyun 	 * space. Without stealing bitmap free space into extent entry space,
575*4882a593Smuzhiyun 	 * we would have all this free space represented by 2 entries in the
576*4882a593Smuzhiyun 	 * cache:
577*4882a593Smuzhiyun 	 *
578*4882a593Smuzhiyun 	 * extent entry covering range: [128Mb - 256Kb, 128Mb[
579*4882a593Smuzhiyun 	 * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
580*4882a593Smuzhiyun 	 *
581*4882a593Smuzhiyun 	 * Attempting to allocate the whole free space (1Mb) would fail, because
582*4882a593Smuzhiyun 	 * we can't allocate from multiple entries.
583*4882a593Smuzhiyun 	 * With the bitmap free space stealing, we get a single extent entry
584*4882a593Smuzhiyun 	 * that represents the 1Mb free space, and therefore we're able to
585*4882a593Smuzhiyun 	 * allocate the whole free space at once.
586*4882a593Smuzhiyun 	 */
587*4882a593Smuzhiyun 	if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) {
588*4882a593Smuzhiyun 		test_err("expected region not marked as free");
589*4882a593Smuzhiyun 		return -ENOENT;
590*4882a593Smuzhiyun 	}
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	if (cache->free_space_ctl->free_space != (SZ_1M + sectorsize)) {
593*4882a593Smuzhiyun 		test_err("cache free space is not 1Mb + %u", sectorsize);
594*4882a593Smuzhiyun 		return -EINVAL;
595*4882a593Smuzhiyun 	}
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	offset = btrfs_find_space_for_alloc(cache,
598*4882a593Smuzhiyun 					    0, SZ_1M, 0,
599*4882a593Smuzhiyun 					    &max_extent_size);
600*4882a593Smuzhiyun 	if (offset != (SZ_128M - SZ_256K)) {
601*4882a593Smuzhiyun 		test_err(
602*4882a593Smuzhiyun 	"failed to allocate 1Mb from space cache, returned offset is: %llu",
603*4882a593Smuzhiyun 			 offset);
604*4882a593Smuzhiyun 		return -EINVAL;
605*4882a593Smuzhiyun 	}
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	/*
608*4882a593Smuzhiyun 	 * All that remains is a sectorsize free space region in a bitmap.
609*4882a593Smuzhiyun 	 * Confirm.
610*4882a593Smuzhiyun 	 */
611*4882a593Smuzhiyun 	ret = check_num_extents_and_bitmaps(cache, 1, 1);
612*4882a593Smuzhiyun 	if (ret)
613*4882a593Smuzhiyun 		return ret;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	if (cache->free_space_ctl->free_space != sectorsize) {
616*4882a593Smuzhiyun 		test_err("cache free space is not %u", sectorsize);
617*4882a593Smuzhiyun 		return -EINVAL;
618*4882a593Smuzhiyun 	}
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	offset = btrfs_find_space_for_alloc(cache,
621*4882a593Smuzhiyun 					    0, sectorsize, 0,
622*4882a593Smuzhiyun 					    &max_extent_size);
623*4882a593Smuzhiyun 	if (offset != (SZ_128M + SZ_16M)) {
624*4882a593Smuzhiyun 		test_err("failed to allocate %u, returned offset : %llu",
625*4882a593Smuzhiyun 			 sectorsize, offset);
626*4882a593Smuzhiyun 		return -EINVAL;
627*4882a593Smuzhiyun 	}
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	ret = check_cache_empty(cache);
630*4882a593Smuzhiyun 	if (ret)
631*4882a593Smuzhiyun 		return ret;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	/*
636*4882a593Smuzhiyun 	 * Now test a similar scenario, but where our extent entry is located
637*4882a593Smuzhiyun 	 * to the right of the bitmap entry, so that we can check that stealing
638*4882a593Smuzhiyun 	 * space from a bitmap to the front of an extent entry works.
639*4882a593Smuzhiyun 	 */
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	/*
642*4882a593Smuzhiyun 	 * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
643*4882a593Smuzhiyun 	 */
644*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0);
645*4882a593Smuzhiyun 	if (ret) {
646*4882a593Smuzhiyun 		test_err("couldn't add extent entry %d", ret);
647*4882a593Smuzhiyun 		return ret;
648*4882a593Smuzhiyun 	}
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 	/* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
651*4882a593Smuzhiyun 	ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1);
652*4882a593Smuzhiyun 	if (ret) {
653*4882a593Smuzhiyun 		test_err("couldn't add bitmap entry %d", ret);
654*4882a593Smuzhiyun 		return ret;
655*4882a593Smuzhiyun 	}
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
658*4882a593Smuzhiyun 	if (ret)
659*4882a593Smuzhiyun 		return ret;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	/*
662*4882a593Smuzhiyun 	 * Now make only the last 256Kb of the bitmap marked as free, so that
663*4882a593Smuzhiyun 	 * we end up with only the following ranges marked as free space:
664*4882a593Smuzhiyun 	 *
665*4882a593Smuzhiyun 	 * [128Mb + 128b, 128Mb + 256Kb[
666*4882a593Smuzhiyun 	 * [128Mb - 768Kb, 128Mb - 512Kb[
667*4882a593Smuzhiyun 	 */
668*4882a593Smuzhiyun 	ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K);
669*4882a593Smuzhiyun 	if (ret) {
670*4882a593Smuzhiyun 		test_err("failed to free part of bitmap space %d", ret);
671*4882a593Smuzhiyun 		return ret;
672*4882a593Smuzhiyun 	}
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	/* Confirm that only those 2 ranges are marked as free. */
675*4882a593Smuzhiyun 	if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) {
676*4882a593Smuzhiyun 		test_err("free space range missing");
677*4882a593Smuzhiyun 		return -ENOENT;
678*4882a593Smuzhiyun 	}
679*4882a593Smuzhiyun 	if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) {
680*4882a593Smuzhiyun 		test_err("free space range missing");
681*4882a593Smuzhiyun 		return -ENOENT;
682*4882a593Smuzhiyun 	}
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	/*
685*4882a593Smuzhiyun 	 * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
686*4882a593Smuzhiyun 	 * as free anymore.
687*4882a593Smuzhiyun 	 */
688*4882a593Smuzhiyun 	if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) {
689*4882a593Smuzhiyun 		test_err("bitmap region not removed from space cache");
690*4882a593Smuzhiyun 		return -EINVAL;
691*4882a593Smuzhiyun 	}
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	/*
694*4882a593Smuzhiyun 	 * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
695*4882a593Smuzhiyun 	 * covered by the bitmap, isn't marked as free.
696*4882a593Smuzhiyun 	 */
697*4882a593Smuzhiyun 	if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
698*4882a593Smuzhiyun 		test_err("invalid bitmap region marked as free");
699*4882a593Smuzhiyun 		return -EINVAL;
700*4882a593Smuzhiyun 	}
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	/*
703*4882a593Smuzhiyun 	 * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
704*4882a593Smuzhiyun 	 * lets make sure the free space cache marks it as free in the bitmap,
705*4882a593Smuzhiyun 	 * and doesn't insert a new extent entry to represent this region.
706*4882a593Smuzhiyun 	 */
707*4882a593Smuzhiyun 	ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K);
708*4882a593Smuzhiyun 	if (ret) {
709*4882a593Smuzhiyun 		test_err("error adding free space: %d", ret);
710*4882a593Smuzhiyun 		return ret;
711*4882a593Smuzhiyun 	}
712*4882a593Smuzhiyun 	/* Confirm the region is marked as free. */
713*4882a593Smuzhiyun 	if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
714*4882a593Smuzhiyun 		test_err("bitmap region not marked as free");
715*4882a593Smuzhiyun 		return -ENOENT;
716*4882a593Smuzhiyun 	}
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	/*
719*4882a593Smuzhiyun 	 * Confirm that no new extent entries or bitmap entries were added to
720*4882a593Smuzhiyun 	 * the cache after adding that free space region.
721*4882a593Smuzhiyun 	 */
722*4882a593Smuzhiyun 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
723*4882a593Smuzhiyun 	if (ret)
724*4882a593Smuzhiyun 		return ret;
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	/*
727*4882a593Smuzhiyun 	 * Now lets add a small free space region to the left of the previous
728*4882a593Smuzhiyun 	 * one, which is not contiguous with it and is part of the bitmap too.
729*4882a593Smuzhiyun 	 * The goal is to test that the bitmap entry space stealing doesn't
730*4882a593Smuzhiyun 	 * steal this space region.
731*4882a593Smuzhiyun 	 */
732*4882a593Smuzhiyun 	ret = btrfs_add_free_space(cache, SZ_32M, 2 * sectorsize);
733*4882a593Smuzhiyun 	if (ret) {
734*4882a593Smuzhiyun 		test_err("error adding free space: %d", ret);
735*4882a593Smuzhiyun 		return ret;
736*4882a593Smuzhiyun 	}
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	/*
739*4882a593Smuzhiyun 	 * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
740*4882a593Smuzhiyun 	 * expand the range covered by the existing extent entry that represents
741*4882a593Smuzhiyun 	 * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
742*4882a593Smuzhiyun 	 */
743*4882a593Smuzhiyun 	ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K);
744*4882a593Smuzhiyun 	if (ret) {
745*4882a593Smuzhiyun 		test_err("error adding free space: %d", ret);
746*4882a593Smuzhiyun 		return ret;
747*4882a593Smuzhiyun 	}
748*4882a593Smuzhiyun 	/* Confirm the region is marked as free. */
749*4882a593Smuzhiyun 	if (!test_check_exists(cache, SZ_128M, SZ_128K)) {
750*4882a593Smuzhiyun 		test_err("extent region not marked as free");
751*4882a593Smuzhiyun 		return -ENOENT;
752*4882a593Smuzhiyun 	}
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	/*
755*4882a593Smuzhiyun 	 * Confirm that our extent entry didn't stole all free space from the
756*4882a593Smuzhiyun 	 * bitmap, because of the small 2 * sectorsize free space region.
757*4882a593Smuzhiyun 	 */
758*4882a593Smuzhiyun 	ret = check_num_extents_and_bitmaps(cache, 2, 1);
759*4882a593Smuzhiyun 	if (ret)
760*4882a593Smuzhiyun 		return ret;
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	/*
763*4882a593Smuzhiyun 	 * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
764*4882a593Smuzhiyun 	 * space. Without stealing bitmap free space into extent entry space,
765*4882a593Smuzhiyun 	 * we would have all this free space represented by 2 entries in the
766*4882a593Smuzhiyun 	 * cache:
767*4882a593Smuzhiyun 	 *
768*4882a593Smuzhiyun 	 * extent entry covering range: [128Mb, 128Mb + 256Kb[
769*4882a593Smuzhiyun 	 * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
770*4882a593Smuzhiyun 	 *
771*4882a593Smuzhiyun 	 * Attempting to allocate the whole free space (1Mb) would fail, because
772*4882a593Smuzhiyun 	 * we can't allocate from multiple entries.
773*4882a593Smuzhiyun 	 * With the bitmap free space stealing, we get a single extent entry
774*4882a593Smuzhiyun 	 * that represents the 1Mb free space, and therefore we're able to
775*4882a593Smuzhiyun 	 * allocate the whole free space at once.
776*4882a593Smuzhiyun 	 */
777*4882a593Smuzhiyun 	if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) {
778*4882a593Smuzhiyun 		test_err("expected region not marked as free");
779*4882a593Smuzhiyun 		return -ENOENT;
780*4882a593Smuzhiyun 	}
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	if (cache->free_space_ctl->free_space != (SZ_1M + 2 * sectorsize)) {
783*4882a593Smuzhiyun 		test_err("cache free space is not 1Mb + %u", 2 * sectorsize);
784*4882a593Smuzhiyun 		return -EINVAL;
785*4882a593Smuzhiyun 	}
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0,
788*4882a593Smuzhiyun 					    &max_extent_size);
789*4882a593Smuzhiyun 	if (offset != (SZ_128M - 768 * SZ_1K)) {
790*4882a593Smuzhiyun 		test_err(
791*4882a593Smuzhiyun 	"failed to allocate 1Mb from space cache, returned offset is: %llu",
792*4882a593Smuzhiyun 			 offset);
793*4882a593Smuzhiyun 		return -EINVAL;
794*4882a593Smuzhiyun 	}
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	/*
797*4882a593Smuzhiyun 	 * All that remains is 2 * sectorsize free space region
798*4882a593Smuzhiyun 	 * in a bitmap. Confirm.
799*4882a593Smuzhiyun 	 */
800*4882a593Smuzhiyun 	ret = check_num_extents_and_bitmaps(cache, 1, 1);
801*4882a593Smuzhiyun 	if (ret)
802*4882a593Smuzhiyun 		return ret;
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	if (cache->free_space_ctl->free_space != 2 * sectorsize) {
805*4882a593Smuzhiyun 		test_err("cache free space is not %u", 2 * sectorsize);
806*4882a593Smuzhiyun 		return -EINVAL;
807*4882a593Smuzhiyun 	}
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	offset = btrfs_find_space_for_alloc(cache,
810*4882a593Smuzhiyun 					    0, 2 * sectorsize, 0,
811*4882a593Smuzhiyun 					    &max_extent_size);
812*4882a593Smuzhiyun 	if (offset != SZ_32M) {
813*4882a593Smuzhiyun 		test_err("failed to allocate %u, offset: %llu",
814*4882a593Smuzhiyun 			 2 * sectorsize, offset);
815*4882a593Smuzhiyun 		return -EINVAL;
816*4882a593Smuzhiyun 	}
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	ret = check_cache_empty(cache);
819*4882a593Smuzhiyun 	if (ret)
820*4882a593Smuzhiyun 		return ret;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	cache->free_space_ctl->op = orig_free_space_ops;
823*4882a593Smuzhiyun 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	return 0;
826*4882a593Smuzhiyun }
827*4882a593Smuzhiyun 
btrfs_test_free_space_cache(u32 sectorsize,u32 nodesize)828*4882a593Smuzhiyun int btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize)
829*4882a593Smuzhiyun {
830*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info;
831*4882a593Smuzhiyun 	struct btrfs_block_group *cache;
832*4882a593Smuzhiyun 	struct btrfs_root *root = NULL;
833*4882a593Smuzhiyun 	int ret = -ENOMEM;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	test_msg("running btrfs free space cache tests");
836*4882a593Smuzhiyun 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
837*4882a593Smuzhiyun 	if (!fs_info) {
838*4882a593Smuzhiyun 		test_std_err(TEST_ALLOC_FS_INFO);
839*4882a593Smuzhiyun 		return -ENOMEM;
840*4882a593Smuzhiyun 	}
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	/*
843*4882a593Smuzhiyun 	 * For ppc64 (with 64k page size), bytes per bitmap might be
844*4882a593Smuzhiyun 	 * larger than 1G.  To make bitmap test available in ppc64,
845*4882a593Smuzhiyun 	 * alloc dummy block group whose size cross bitmaps.
846*4882a593Smuzhiyun 	 */
847*4882a593Smuzhiyun 	cache = btrfs_alloc_dummy_block_group(fs_info,
848*4882a593Smuzhiyun 				      BITS_PER_BITMAP * sectorsize + PAGE_SIZE);
849*4882a593Smuzhiyun 	if (!cache) {
850*4882a593Smuzhiyun 		test_std_err(TEST_ALLOC_BLOCK_GROUP);
851*4882a593Smuzhiyun 		btrfs_free_dummy_fs_info(fs_info);
852*4882a593Smuzhiyun 		return 0;
853*4882a593Smuzhiyun 	}
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	root = btrfs_alloc_dummy_root(fs_info);
856*4882a593Smuzhiyun 	if (IS_ERR(root)) {
857*4882a593Smuzhiyun 		test_std_err(TEST_ALLOC_ROOT);
858*4882a593Smuzhiyun 		ret = PTR_ERR(root);
859*4882a593Smuzhiyun 		goto out;
860*4882a593Smuzhiyun 	}
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	root->fs_info->extent_root = root;
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	ret = test_extents(cache);
865*4882a593Smuzhiyun 	if (ret)
866*4882a593Smuzhiyun 		goto out;
867*4882a593Smuzhiyun 	ret = test_bitmaps(cache, sectorsize);
868*4882a593Smuzhiyun 	if (ret)
869*4882a593Smuzhiyun 		goto out;
870*4882a593Smuzhiyun 	ret = test_bitmaps_and_extents(cache, sectorsize);
871*4882a593Smuzhiyun 	if (ret)
872*4882a593Smuzhiyun 		goto out;
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	ret = test_steal_space_from_bitmap_to_extent(cache, sectorsize);
875*4882a593Smuzhiyun out:
876*4882a593Smuzhiyun 	btrfs_free_dummy_block_group(cache);
877*4882a593Smuzhiyun 	btrfs_free_dummy_root(root);
878*4882a593Smuzhiyun 	btrfs_free_dummy_fs_info(fs_info);
879*4882a593Smuzhiyun 	return ret;
880*4882a593Smuzhiyun }
881