2 * Copyright (C) 2013 Fusion IO. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/slab.h>
20 #include "btrfs-tests.h"
22 #include "../free-space-cache.h"
24 #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
25 static struct btrfs_block_group_cache
*init_test_block_group(void)
27 struct btrfs_block_group_cache
*cache
;
29 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
32 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
34 if (!cache
->free_space_ctl
) {
39 cache
->key
.objectid
= 0;
40 cache
->key
.offset
= 1024 * 1024 * 1024;
41 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
42 cache
->sectorsize
= 4096;
44 spin_lock_init(&cache
->lock
);
45 INIT_LIST_HEAD(&cache
->list
);
46 INIT_LIST_HEAD(&cache
->cluster_list
);
47 INIT_LIST_HEAD(&cache
->new_bg_list
);
49 btrfs_init_free_space_ctl(cache
);
55 * This test just does basic sanity checking, making sure we can add an exten
56 * entry and remove space from either end and the middle, and make sure we can
57 * remove space that covers adjacent extent entries.
59 static int test_extents(struct btrfs_block_group_cache
*cache
)
63 test_msg("Running extent only tests\n");
65 /* First just make sure we can remove an entire entry */
66 ret
= btrfs_add_free_space(cache
, 0, 4 * 1024 * 1024);
68 test_msg("Error adding initial extents %d\n", ret
);
72 ret
= btrfs_remove_free_space(cache
, 0, 4 * 1024 * 1024);
74 test_msg("Error removing extent %d\n", ret
);
78 if (test_check_exists(cache
, 0, 4 * 1024 * 1024)) {
79 test_msg("Full remove left some lingering space\n");
83 /* Ok edge and middle cases now */
84 ret
= btrfs_add_free_space(cache
, 0, 4 * 1024 * 1024);
86 test_msg("Error adding half extent %d\n", ret
);
90 ret
= btrfs_remove_free_space(cache
, 3 * 1024 * 1024, 1 * 1024 * 1024);
92 test_msg("Error removing tail end %d\n", ret
);
96 ret
= btrfs_remove_free_space(cache
, 0, 1 * 1024 * 1024);
98 test_msg("Error removing front end %d\n", ret
);
102 ret
= btrfs_remove_free_space(cache
, 2 * 1024 * 1024, 4096);
104 test_msg("Error removing middle piece %d\n", ret
);
108 if (test_check_exists(cache
, 0, 1 * 1024 * 1024)) {
109 test_msg("Still have space at the front\n");
113 if (test_check_exists(cache
, 2 * 1024 * 1024, 4096)) {
114 test_msg("Still have space in the middle\n");
118 if (test_check_exists(cache
, 3 * 1024 * 1024, 1 * 1024 * 1024)) {
119 test_msg("Still have space at the end\n");
124 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
129 static int test_bitmaps(struct btrfs_block_group_cache
*cache
)
131 u64 next_bitmap_offset
;
134 test_msg("Running bitmap only tests\n");
136 ret
= test_add_free_space_entry(cache
, 0, 4 * 1024 * 1024, 1);
138 test_msg("Couldn't create a bitmap entry %d\n", ret
);
142 ret
= btrfs_remove_free_space(cache
, 0, 4 * 1024 * 1024);
144 test_msg("Error removing bitmap full range %d\n", ret
);
148 if (test_check_exists(cache
, 0, 4 * 1024 * 1024)) {
149 test_msg("Left some space in bitmap\n");
153 ret
= test_add_free_space_entry(cache
, 0, 4 * 1024 * 1024, 1);
155 test_msg("Couldn't add to our bitmap entry %d\n", ret
);
159 ret
= btrfs_remove_free_space(cache
, 1 * 1024 * 1024, 2 * 1024 * 1024);
161 test_msg("Couldn't remove middle chunk %d\n", ret
);
166 * The first bitmap we have starts at offset 0 so the next one is just
167 * at the end of the first bitmap.
169 next_bitmap_offset
= (u64
)(BITS_PER_BITMAP
* 4096);
171 /* Test a bit straddling two bitmaps */
172 ret
= test_add_free_space_entry(cache
, next_bitmap_offset
-
173 (2 * 1024 * 1024), 4 * 1024 * 1024, 1);
175 test_msg("Couldn't add space that straddles two bitmaps %d\n",
180 ret
= btrfs_remove_free_space(cache
, next_bitmap_offset
-
181 (1 * 1024 * 1024), 2 * 1024 * 1024);
183 test_msg("Couldn't remove overlapping space %d\n", ret
);
187 if (test_check_exists(cache
, next_bitmap_offset
- (1 * 1024 * 1024),
189 test_msg("Left some space when removing overlapping\n");
193 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
198 /* This is the high grade jackassery */
199 static int test_bitmaps_and_extents(struct btrfs_block_group_cache
*cache
)
201 u64 bitmap_offset
= (u64
)(BITS_PER_BITMAP
* 4096);
204 test_msg("Running bitmap and extent tests\n");
207 * First let's do something simple, an extent at the same offset as the
208 * bitmap, but the free space completely in the extent and then
209 * completely in the bitmap.
211 ret
= test_add_free_space_entry(cache
, 4 * 1024 * 1024, 1 * 1024 * 1024, 1);
213 test_msg("Couldn't create bitmap entry %d\n", ret
);
217 ret
= test_add_free_space_entry(cache
, 0, 1 * 1024 * 1024, 0);
219 test_msg("Couldn't add extent entry %d\n", ret
);
223 ret
= btrfs_remove_free_space(cache
, 0, 1 * 1024 * 1024);
225 test_msg("Couldn't remove extent entry %d\n", ret
);
229 if (test_check_exists(cache
, 0, 1 * 1024 * 1024)) {
230 test_msg("Left remnants after our remove\n");
234 /* Now to add back the extent entry and remove from the bitmap */
235 ret
= test_add_free_space_entry(cache
, 0, 1 * 1024 * 1024, 0);
237 test_msg("Couldn't re-add extent entry %d\n", ret
);
241 ret
= btrfs_remove_free_space(cache
, 4 * 1024 * 1024, 1 * 1024 * 1024);
243 test_msg("Couldn't remove from bitmap %d\n", ret
);
247 if (test_check_exists(cache
, 4 * 1024 * 1024, 1 * 1024 * 1024)) {
248 test_msg("Left remnants in the bitmap\n");
253 * Ok so a little more evil, extent entry and bitmap at the same offset,
254 * removing an overlapping chunk.
256 ret
= test_add_free_space_entry(cache
, 1 * 1024 * 1024, 4 * 1024 * 1024, 1);
258 test_msg("Couldn't add to a bitmap %d\n", ret
);
262 ret
= btrfs_remove_free_space(cache
, 512 * 1024, 3 * 1024 * 1024);
264 test_msg("Couldn't remove overlapping space %d\n", ret
);
268 if (test_check_exists(cache
, 512 * 1024, 3 * 1024 * 1024)) {
269 test_msg("Left over pieces after removing overlapping\n");
273 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
275 /* Now with the extent entry offset into the bitmap */
276 ret
= test_add_free_space_entry(cache
, 4 * 1024 * 1024, 4 * 1024 * 1024, 1);
278 test_msg("Couldn't add space to the bitmap %d\n", ret
);
282 ret
= test_add_free_space_entry(cache
, 2 * 1024 * 1024, 2 * 1024 * 1024, 0);
284 test_msg("Couldn't add extent to the cache %d\n", ret
);
288 ret
= btrfs_remove_free_space(cache
, 3 * 1024 * 1024, 4 * 1024 * 1024);
290 test_msg("Problem removing overlapping space %d\n", ret
);
294 if (test_check_exists(cache
, 3 * 1024 * 1024, 4 * 1024 * 1024)) {
295 test_msg("Left something behind when removing space");
300 * This has blown up in the past, the extent entry starts before the
301 * bitmap entry, but we're trying to remove an offset that falls
302 * completely within the bitmap range and is in both the extent entry
303 * and the bitmap entry, looks like this
309 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
310 ret
= test_add_free_space_entry(cache
, bitmap_offset
+ 4 * 1024 * 1024,
313 test_msg("Couldn't add bitmap %d\n", ret
);
317 ret
= test_add_free_space_entry(cache
, bitmap_offset
- 1 * 1024 * 1024,
320 test_msg("Couldn't add extent entry %d\n", ret
);
324 ret
= btrfs_remove_free_space(cache
, bitmap_offset
+ 1 * 1024 * 1024,
327 test_msg("Failed to free our space %d\n", ret
);
331 if (test_check_exists(cache
, bitmap_offset
+ 1 * 1024 * 1024,
333 test_msg("Left stuff over\n");
337 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
340 * This blew up before, we have part of the free space in a bitmap and
341 * then the entirety of the rest of the space in an extent. This used
342 * to return -EAGAIN back from btrfs_remove_extent, make sure this
345 ret
= test_add_free_space_entry(cache
, 1 * 1024 * 1024, 2 * 1024 * 1024, 1);
347 test_msg("Couldn't add bitmap entry %d\n", ret
);
351 ret
= test_add_free_space_entry(cache
, 3 * 1024 * 1024, 1 * 1024 * 1024, 0);
353 test_msg("Couldn't add extent entry %d\n", ret
);
357 ret
= btrfs_remove_free_space(cache
, 1 * 1024 * 1024, 3 * 1024 * 1024);
359 test_msg("Error removing bitmap and extent overlapping %d\n", ret
);
363 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
367 int btrfs_test_free_space_cache(void)
369 struct btrfs_block_group_cache
*cache
;
372 test_msg("Running btrfs free space cache tests\n");
374 cache
= init_test_block_group();
376 test_msg("Couldn't run the tests\n");
380 ret
= test_extents(cache
);
383 ret
= test_bitmaps(cache
);
386 ret
= test_bitmaps_and_extents(cache
);
390 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
391 kfree(cache
->free_space_ctl
);
393 test_msg("Free space cache tests finished\n");