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;
43 cache
->full_stripe_len
= 4096;
45 spin_lock_init(&cache
->lock
);
46 INIT_LIST_HEAD(&cache
->list
);
47 INIT_LIST_HEAD(&cache
->cluster_list
);
48 INIT_LIST_HEAD(&cache
->bg_list
);
50 btrfs_init_free_space_ctl(cache
);
56 * This test just does basic sanity checking, making sure we can add an exten
57 * entry and remove space from either end and the middle, and make sure we can
58 * remove space that covers adjacent extent entries.
60 static int test_extents(struct btrfs_block_group_cache
*cache
)
64 test_msg("Running extent only tests\n");
66 /* First just make sure we can remove an entire entry */
67 ret
= btrfs_add_free_space(cache
, 0, 4 * 1024 * 1024);
69 test_msg("Error adding initial extents %d\n", ret
);
73 ret
= btrfs_remove_free_space(cache
, 0, 4 * 1024 * 1024);
75 test_msg("Error removing extent %d\n", ret
);
79 if (test_check_exists(cache
, 0, 4 * 1024 * 1024)) {
80 test_msg("Full remove left some lingering space\n");
84 /* Ok edge and middle cases now */
85 ret
= btrfs_add_free_space(cache
, 0, 4 * 1024 * 1024);
87 test_msg("Error adding half extent %d\n", ret
);
91 ret
= btrfs_remove_free_space(cache
, 3 * 1024 * 1024, 1 * 1024 * 1024);
93 test_msg("Error removing tail end %d\n", ret
);
97 ret
= btrfs_remove_free_space(cache
, 0, 1 * 1024 * 1024);
99 test_msg("Error removing front end %d\n", ret
);
103 ret
= btrfs_remove_free_space(cache
, 2 * 1024 * 1024, 4096);
105 test_msg("Error removing middle piece %d\n", ret
);
109 if (test_check_exists(cache
, 0, 1 * 1024 * 1024)) {
110 test_msg("Still have space at the front\n");
114 if (test_check_exists(cache
, 2 * 1024 * 1024, 4096)) {
115 test_msg("Still have space in the middle\n");
119 if (test_check_exists(cache
, 3 * 1024 * 1024, 1 * 1024 * 1024)) {
120 test_msg("Still have space at the end\n");
125 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
130 static int test_bitmaps(struct btrfs_block_group_cache
*cache
)
132 u64 next_bitmap_offset
;
135 test_msg("Running bitmap only tests\n");
137 ret
= test_add_free_space_entry(cache
, 0, 4 * 1024 * 1024, 1);
139 test_msg("Couldn't create a bitmap entry %d\n", ret
);
143 ret
= btrfs_remove_free_space(cache
, 0, 4 * 1024 * 1024);
145 test_msg("Error removing bitmap full range %d\n", ret
);
149 if (test_check_exists(cache
, 0, 4 * 1024 * 1024)) {
150 test_msg("Left some space in bitmap\n");
154 ret
= test_add_free_space_entry(cache
, 0, 4 * 1024 * 1024, 1);
156 test_msg("Couldn't add to our bitmap entry %d\n", ret
);
160 ret
= btrfs_remove_free_space(cache
, 1 * 1024 * 1024, 2 * 1024 * 1024);
162 test_msg("Couldn't remove middle chunk %d\n", ret
);
167 * The first bitmap we have starts at offset 0 so the next one is just
168 * at the end of the first bitmap.
170 next_bitmap_offset
= (u64
)(BITS_PER_BITMAP
* 4096);
172 /* Test a bit straddling two bitmaps */
173 ret
= test_add_free_space_entry(cache
, next_bitmap_offset
-
174 (2 * 1024 * 1024), 4 * 1024 * 1024, 1);
176 test_msg("Couldn't add space that straddles two bitmaps %d\n",
181 ret
= btrfs_remove_free_space(cache
, next_bitmap_offset
-
182 (1 * 1024 * 1024), 2 * 1024 * 1024);
184 test_msg("Couldn't remove overlapping space %d\n", ret
);
188 if (test_check_exists(cache
, next_bitmap_offset
- (1 * 1024 * 1024),
190 test_msg("Left some space when removing overlapping\n");
194 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
199 /* This is the high grade jackassery */
200 static int test_bitmaps_and_extents(struct btrfs_block_group_cache
*cache
)
202 u64 bitmap_offset
= (u64
)(BITS_PER_BITMAP
* 4096);
205 test_msg("Running bitmap and extent tests\n");
208 * First let's do something simple, an extent at the same offset as the
209 * bitmap, but the free space completely in the extent and then
210 * completely in the bitmap.
212 ret
= test_add_free_space_entry(cache
, 4 * 1024 * 1024, 1 * 1024 * 1024, 1);
214 test_msg("Couldn't create bitmap entry %d\n", ret
);
218 ret
= test_add_free_space_entry(cache
, 0, 1 * 1024 * 1024, 0);
220 test_msg("Couldn't add extent entry %d\n", ret
);
224 ret
= btrfs_remove_free_space(cache
, 0, 1 * 1024 * 1024);
226 test_msg("Couldn't remove extent entry %d\n", ret
);
230 if (test_check_exists(cache
, 0, 1 * 1024 * 1024)) {
231 test_msg("Left remnants after our remove\n");
235 /* Now to add back the extent entry and remove from the bitmap */
236 ret
= test_add_free_space_entry(cache
, 0, 1 * 1024 * 1024, 0);
238 test_msg("Couldn't re-add extent entry %d\n", ret
);
242 ret
= btrfs_remove_free_space(cache
, 4 * 1024 * 1024, 1 * 1024 * 1024);
244 test_msg("Couldn't remove from bitmap %d\n", ret
);
248 if (test_check_exists(cache
, 4 * 1024 * 1024, 1 * 1024 * 1024)) {
249 test_msg("Left remnants in the bitmap\n");
254 * Ok so a little more evil, extent entry and bitmap at the same offset,
255 * removing an overlapping chunk.
257 ret
= test_add_free_space_entry(cache
, 1 * 1024 * 1024, 4 * 1024 * 1024, 1);
259 test_msg("Couldn't add to a bitmap %d\n", ret
);
263 ret
= btrfs_remove_free_space(cache
, 512 * 1024, 3 * 1024 * 1024);
265 test_msg("Couldn't remove overlapping space %d\n", ret
);
269 if (test_check_exists(cache
, 512 * 1024, 3 * 1024 * 1024)) {
270 test_msg("Left over pieces after removing overlapping\n");
274 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
276 /* Now with the extent entry offset into the bitmap */
277 ret
= test_add_free_space_entry(cache
, 4 * 1024 * 1024, 4 * 1024 * 1024, 1);
279 test_msg("Couldn't add space to the bitmap %d\n", ret
);
283 ret
= test_add_free_space_entry(cache
, 2 * 1024 * 1024, 2 * 1024 * 1024, 0);
285 test_msg("Couldn't add extent to the cache %d\n", ret
);
289 ret
= btrfs_remove_free_space(cache
, 3 * 1024 * 1024, 4 * 1024 * 1024);
291 test_msg("Problem removing overlapping space %d\n", ret
);
295 if (test_check_exists(cache
, 3 * 1024 * 1024, 4 * 1024 * 1024)) {
296 test_msg("Left something behind when removing space");
301 * This has blown up in the past, the extent entry starts before the
302 * bitmap entry, but we're trying to remove an offset that falls
303 * completely within the bitmap range and is in both the extent entry
304 * and the bitmap entry, looks like this
310 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
311 ret
= test_add_free_space_entry(cache
, bitmap_offset
+ 4 * 1024 * 1024,
314 test_msg("Couldn't add bitmap %d\n", ret
);
318 ret
= test_add_free_space_entry(cache
, bitmap_offset
- 1 * 1024 * 1024,
321 test_msg("Couldn't add extent entry %d\n", ret
);
325 ret
= btrfs_remove_free_space(cache
, bitmap_offset
+ 1 * 1024 * 1024,
328 test_msg("Failed to free our space %d\n", ret
);
332 if (test_check_exists(cache
, bitmap_offset
+ 1 * 1024 * 1024,
334 test_msg("Left stuff over\n");
338 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
341 * This blew up before, we have part of the free space in a bitmap and
342 * then the entirety of the rest of the space in an extent. This used
343 * to return -EAGAIN back from btrfs_remove_extent, make sure this
346 ret
= test_add_free_space_entry(cache
, 1 * 1024 * 1024, 2 * 1024 * 1024, 1);
348 test_msg("Couldn't add bitmap entry %d\n", ret
);
352 ret
= test_add_free_space_entry(cache
, 3 * 1024 * 1024, 1 * 1024 * 1024, 0);
354 test_msg("Couldn't add extent entry %d\n", ret
);
358 ret
= btrfs_remove_free_space(cache
, 1 * 1024 * 1024, 3 * 1024 * 1024);
360 test_msg("Error removing bitmap and extent overlapping %d\n", ret
);
364 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
368 /* Used by test_steal_space_from_bitmap_to_extent(). */
369 static bool test_use_bitmap(struct btrfs_free_space_ctl
*ctl
,
370 struct btrfs_free_space
*info
)
372 return ctl
->free_extents
> 0;
375 /* Used by test_steal_space_from_bitmap_to_extent(). */
377 check_num_extents_and_bitmaps(const struct btrfs_block_group_cache
*cache
,
378 const int num_extents
,
379 const int num_bitmaps
)
381 if (cache
->free_space_ctl
->free_extents
!= num_extents
) {
382 test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
383 cache
->free_space_ctl
->free_extents
, num_extents
);
386 if (cache
->free_space_ctl
->total_bitmaps
!= num_bitmaps
) {
387 test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
388 cache
->free_space_ctl
->total_bitmaps
, num_bitmaps
);
394 /* Used by test_steal_space_from_bitmap_to_extent(). */
395 static int check_cache_empty(struct btrfs_block_group_cache
*cache
)
401 * Now lets confirm that there's absolutely no free space left to
404 if (cache
->free_space_ctl
->free_space
!= 0) {
405 test_msg("Cache free space is not 0\n");
409 /* And any allocation request, no matter how small, should fail now. */
410 offset
= btrfs_find_space_for_alloc(cache
, 0, 4096, 0,
413 test_msg("Space allocation did not fail, returned offset: %llu",
418 /* And no extent nor bitmap entries in the cache anymore. */
419 return check_num_extents_and_bitmaps(cache
, 0, 0);
423 * Before we were able to steal free space from a bitmap entry to an extent
424 * entry, we could end up with 2 entries representing a contiguous free space.
425 * One would be an extent entry and the other a bitmap entry. Since in order
426 * to allocate space to a caller we use only 1 entry, we couldn't return that
427 * whole range to the caller if it was requested. This forced the caller to
428 * either assume ENOSPC or perform several smaller space allocations, which
429 * wasn't optimal as they could be spread all over the block group while under
430 * concurrency (extra overhead and fragmentation).
432 * This stealing approach is benefical, since we always prefer to allocate from
433 * extent entries, both for clustered and non-clustered allocation requests.
436 test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache
*cache
)
442 bool (*use_bitmap_op
)(struct btrfs_free_space_ctl
*,
443 struct btrfs_free_space
*);
445 test_msg("Running space stealing from bitmap to extent\n");
448 * For this test, we want to ensure we end up with an extent entry
449 * immediately adjacent to a bitmap entry, where the bitmap starts
450 * at an offset where the extent entry ends. We keep adding and
451 * removing free space to reach into this state, but to get there
452 * we need to reach a point where marking new free space doesn't
453 * result in adding new extent entries or merging the new space
454 * with existing extent entries - the space ends up being marked
455 * in an existing bitmap that covers the new free space range.
457 * To get there, we need to reach the threshold defined set at
458 * cache->free_space_ctl->extents_thresh, which currently is
459 * 256 extents on a x86_64 system at least, and a few other
460 * conditions (check free_space_cache.c). Instead of making the
461 * test much longer and complicated, use a "use_bitmap" operation
462 * that forces use of bitmaps as soon as we have at least 1
465 use_bitmap_op
= cache
->free_space_ctl
->op
->use_bitmap
;
466 cache
->free_space_ctl
->op
->use_bitmap
= test_use_bitmap
;
469 * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
471 ret
= test_add_free_space_entry(cache
, 128 * 1024 * 1024 - 256 * 1024,
474 test_msg("Couldn't add extent entry %d\n", ret
);
478 /* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
479 ret
= test_add_free_space_entry(cache
, 128 * 1024 * 1024 + 512 * 1024,
480 128 * 1024 * 1024 - 512 * 1024, 1);
482 test_msg("Couldn't add bitmap entry %d\n", ret
);
486 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
491 * Now make only the first 256Kb of the bitmap marked as free, so that
492 * we end up with only the following ranges marked as free space:
494 * [128Mb - 256Kb, 128Mb - 128Kb[
495 * [128Mb + 512Kb, 128Mb + 768Kb[
497 ret
= btrfs_remove_free_space(cache
,
498 128 * 1024 * 1024 + 768 * 1024,
499 128 * 1024 * 1024 - 768 * 1024);
501 test_msg("Failed to free part of bitmap space %d\n", ret
);
505 /* Confirm that only those 2 ranges are marked as free. */
506 if (!test_check_exists(cache
, 128 * 1024 * 1024 - 256 * 1024,
508 test_msg("Free space range missing\n");
511 if (!test_check_exists(cache
, 128 * 1024 * 1024 + 512 * 1024,
513 test_msg("Free space range missing\n");
518 * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
521 if (test_check_exists(cache
, 128 * 1024 * 1024 + 768 * 1024,
522 128 * 1024 * 1024 - 768 * 1024)) {
523 test_msg("Bitmap region not removed from space cache\n");
528 * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
529 * covered by the bitmap, isn't marked as free.
531 if (test_check_exists(cache
, 128 * 1024 * 1024 + 256 * 1024,
533 test_msg("Invalid bitmap region marked as free\n");
538 * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
539 * by the bitmap too, isn't marked as free either.
541 if (test_check_exists(cache
, 128 * 1024 * 1024,
543 test_msg("Invalid bitmap region marked as free\n");
548 * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
549 * lets make sure the free space cache marks it as free in the bitmap,
550 * and doesn't insert a new extent entry to represent this region.
552 ret
= btrfs_add_free_space(cache
, 128 * 1024 * 1024, 512 * 1024);
554 test_msg("Error adding free space: %d\n", ret
);
557 /* Confirm the region is marked as free. */
558 if (!test_check_exists(cache
, 128 * 1024 * 1024, 512 * 1024)) {
559 test_msg("Bitmap region not marked as free\n");
564 * Confirm that no new extent entries or bitmap entries were added to
565 * the cache after adding that free space region.
567 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
572 * Now lets add a small free space region to the right of the previous
573 * one, which is not contiguous with it and is part of the bitmap too.
574 * The goal is to test that the bitmap entry space stealing doesn't
575 * steal this space region.
577 ret
= btrfs_add_free_space(cache
, 128 * 1024 * 1024 + 16 * 1024 * 1024,
580 test_msg("Error adding free space: %d\n", ret
);
585 * Confirm that no new extent entries or bitmap entries were added to
586 * the cache after adding that free space region.
588 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
593 * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
594 * expand the range covered by the existing extent entry that represents
595 * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
597 ret
= btrfs_add_free_space(cache
, 128 * 1024 * 1024 - 128 * 1024,
600 test_msg("Error adding free space: %d\n", ret
);
603 /* Confirm the region is marked as free. */
604 if (!test_check_exists(cache
, 128 * 1024 * 1024 - 128 * 1024,
606 test_msg("Extent region not marked as free\n");
611 * Confirm that our extent entry didn't stole all free space from the
612 * bitmap, because of the small 4Kb free space region.
614 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
619 * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
620 * space. Without stealing bitmap free space into extent entry space,
621 * we would have all this free space represented by 2 entries in the
624 * extent entry covering range: [128Mb - 256Kb, 128Mb[
625 * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
627 * Attempting to allocate the whole free space (1Mb) would fail, because
628 * we can't allocate from multiple entries.
629 * With the bitmap free space stealing, we get a single extent entry
630 * that represents the 1Mb free space, and therefore we're able to
631 * allocate the whole free space at once.
633 if (!test_check_exists(cache
, 128 * 1024 * 1024 - 256 * 1024,
635 test_msg("Expected region not marked as free\n");
639 if (cache
->free_space_ctl
->free_space
!= (1 * 1024 * 1024 + 4096)) {
640 test_msg("Cache free space is not 1Mb + 4Kb\n");
644 offset
= btrfs_find_space_for_alloc(cache
,
645 0, 1 * 1024 * 1024, 0,
647 if (offset
!= (128 * 1024 * 1024 - 256 * 1024)) {
648 test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
653 /* All that remains is a 4Kb free space region in a bitmap. Confirm. */
654 ret
= check_num_extents_and_bitmaps(cache
, 1, 1);
658 if (cache
->free_space_ctl
->free_space
!= 4096) {
659 test_msg("Cache free space is not 4Kb\n");
663 offset
= btrfs_find_space_for_alloc(cache
,
666 if (offset
!= (128 * 1024 * 1024 + 16 * 1024 * 1024)) {
667 test_msg("Failed to allocate 4Kb from space cache, returned offset is: %llu\n",
672 ret
= check_cache_empty(cache
);
676 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
679 * Now test a similar scenario, but where our extent entry is located
680 * to the right of the bitmap entry, so that we can check that stealing
681 * space from a bitmap to the front of an extent entry works.
685 * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
687 ret
= test_add_free_space_entry(cache
, 128 * 1024 * 1024 + 128 * 1024,
690 test_msg("Couldn't add extent entry %d\n", ret
);
694 /* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
695 ret
= test_add_free_space_entry(cache
, 0,
696 128 * 1024 * 1024 - 512 * 1024, 1);
698 test_msg("Couldn't add bitmap entry %d\n", ret
);
702 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
707 * Now make only the last 256Kb of the bitmap marked as free, so that
708 * we end up with only the following ranges marked as free space:
710 * [128Mb + 128b, 128Mb + 256Kb[
711 * [128Mb - 768Kb, 128Mb - 512Kb[
713 ret
= btrfs_remove_free_space(cache
,
715 128 * 1024 * 1024 - 768 * 1024);
717 test_msg("Failed to free part of bitmap space %d\n", ret
);
721 /* Confirm that only those 2 ranges are marked as free. */
722 if (!test_check_exists(cache
, 128 * 1024 * 1024 + 128 * 1024,
724 test_msg("Free space range missing\n");
727 if (!test_check_exists(cache
, 128 * 1024 * 1024 - 768 * 1024,
729 test_msg("Free space range missing\n");
734 * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
737 if (test_check_exists(cache
, 0,
738 128 * 1024 * 1024 - 768 * 1024)) {
739 test_msg("Bitmap region not removed from space cache\n");
744 * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
745 * covered by the bitmap, isn't marked as free.
747 if (test_check_exists(cache
, 128 * 1024 * 1024 - 512 * 1024,
749 test_msg("Invalid bitmap region marked as free\n");
754 * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
755 * lets make sure the free space cache marks it as free in the bitmap,
756 * and doesn't insert a new extent entry to represent this region.
758 ret
= btrfs_add_free_space(cache
, 128 * 1024 * 1024 - 512 * 1024,
761 test_msg("Error adding free space: %d\n", ret
);
764 /* Confirm the region is marked as free. */
765 if (!test_check_exists(cache
, 128 * 1024 * 1024 - 512 * 1024,
767 test_msg("Bitmap region not marked as free\n");
772 * Confirm that no new extent entries or bitmap entries were added to
773 * the cache after adding that free space region.
775 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
780 * Now lets add a small free space region to the left of the previous
781 * one, which is not contiguous with it and is part of the bitmap too.
782 * The goal is to test that the bitmap entry space stealing doesn't
783 * steal this space region.
785 ret
= btrfs_add_free_space(cache
, 32 * 1024 * 1024, 8192);
787 test_msg("Error adding free space: %d\n", ret
);
792 * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
793 * expand the range covered by the existing extent entry that represents
794 * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
796 ret
= btrfs_add_free_space(cache
, 128 * 1024 * 1024, 128 * 1024);
798 test_msg("Error adding free space: %d\n", ret
);
801 /* Confirm the region is marked as free. */
802 if (!test_check_exists(cache
, 128 * 1024 * 1024, 128 * 1024)) {
803 test_msg("Extent region not marked as free\n");
808 * Confirm that our extent entry didn't stole all free space from the
809 * bitmap, because of the small 8Kb free space region.
811 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
816 * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
817 * space. Without stealing bitmap free space into extent entry space,
818 * we would have all this free space represented by 2 entries in the
821 * extent entry covering range: [128Mb, 128Mb + 256Kb[
822 * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
824 * Attempting to allocate the whole free space (1Mb) would fail, because
825 * we can't allocate from multiple entries.
826 * With the bitmap free space stealing, we get a single extent entry
827 * that represents the 1Mb free space, and therefore we're able to
828 * allocate the whole free space at once.
830 if (!test_check_exists(cache
, 128 * 1024 * 1024 - 768 * 1024,
832 test_msg("Expected region not marked as free\n");
836 if (cache
->free_space_ctl
->free_space
!= (1 * 1024 * 1024 + 8192)) {
837 test_msg("Cache free space is not 1Mb + 8Kb\n");
841 offset
= btrfs_find_space_for_alloc(cache
,
842 0, 1 * 1024 * 1024, 0,
844 if (offset
!= (128 * 1024 * 1024 - 768 * 1024)) {
845 test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
850 /* All that remains is a 8Kb free space region in a bitmap. Confirm. */
851 ret
= check_num_extents_and_bitmaps(cache
, 1, 1);
855 if (cache
->free_space_ctl
->free_space
!= 8192) {
856 test_msg("Cache free space is not 8Kb\n");
860 offset
= btrfs_find_space_for_alloc(cache
,
863 if (offset
!= (32 * 1024 * 1024)) {
864 test_msg("Failed to allocate 8Kb from space cache, returned offset is: %llu\n",
869 ret
= check_cache_empty(cache
);
873 cache
->free_space_ctl
->op
->use_bitmap
= use_bitmap_op
;
874 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
879 int btrfs_test_free_space_cache(void)
881 struct btrfs_block_group_cache
*cache
;
884 test_msg("Running btrfs free space cache tests\n");
886 cache
= init_test_block_group();
888 test_msg("Couldn't run the tests\n");
892 ret
= test_extents(cache
);
895 ret
= test_bitmaps(cache
);
898 ret
= test_bitmaps_and_extents(cache
);
902 ret
= test_steal_space_from_bitmap_to_extent(cache
);
904 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
905 kfree(cache
->free_space_ctl
);
907 test_msg("Free space cache tests finished\n");