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 "../disk-io.h"
23 #include "../free-space-cache.h"
25 #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
26 static struct btrfs_block_group_cache
*init_test_block_group(void)
28 struct btrfs_block_group_cache
*cache
;
30 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
33 cache
->free_space_ctl
= kzalloc(sizeof(*cache
->free_space_ctl
),
35 if (!cache
->free_space_ctl
) {
39 cache
->fs_info
= btrfs_alloc_dummy_fs_info();
40 if (!cache
->fs_info
) {
41 kfree(cache
->free_space_ctl
);
46 cache
->key
.objectid
= 0;
47 cache
->key
.offset
= 1024 * 1024 * 1024;
48 cache
->key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
49 cache
->sectorsize
= 4096;
50 cache
->full_stripe_len
= 4096;
52 spin_lock_init(&cache
->lock
);
53 INIT_LIST_HEAD(&cache
->list
);
54 INIT_LIST_HEAD(&cache
->cluster_list
);
55 INIT_LIST_HEAD(&cache
->bg_list
);
57 btrfs_init_free_space_ctl(cache
);
63 * This test just does basic sanity checking, making sure we can add an exten
64 * entry and remove space from either end and the middle, and make sure we can
65 * remove space that covers adjacent extent entries.
67 static int test_extents(struct btrfs_block_group_cache
*cache
)
71 test_msg("Running extent only tests\n");
73 /* First just make sure we can remove an entire entry */
74 ret
= btrfs_add_free_space(cache
, 0, 4 * 1024 * 1024);
76 test_msg("Error adding initial extents %d\n", ret
);
80 ret
= btrfs_remove_free_space(cache
, 0, 4 * 1024 * 1024);
82 test_msg("Error removing extent %d\n", ret
);
86 if (test_check_exists(cache
, 0, 4 * 1024 * 1024)) {
87 test_msg("Full remove left some lingering space\n");
91 /* Ok edge and middle cases now */
92 ret
= btrfs_add_free_space(cache
, 0, 4 * 1024 * 1024);
94 test_msg("Error adding half extent %d\n", ret
);
98 ret
= btrfs_remove_free_space(cache
, 3 * 1024 * 1024, 1 * 1024 * 1024);
100 test_msg("Error removing tail end %d\n", ret
);
104 ret
= btrfs_remove_free_space(cache
, 0, 1 * 1024 * 1024);
106 test_msg("Error removing front end %d\n", ret
);
110 ret
= btrfs_remove_free_space(cache
, 2 * 1024 * 1024, 4096);
112 test_msg("Error removing middle piece %d\n", ret
);
116 if (test_check_exists(cache
, 0, 1 * 1024 * 1024)) {
117 test_msg("Still have space at the front\n");
121 if (test_check_exists(cache
, 2 * 1024 * 1024, 4096)) {
122 test_msg("Still have space in the middle\n");
126 if (test_check_exists(cache
, 3 * 1024 * 1024, 1 * 1024 * 1024)) {
127 test_msg("Still have space at the end\n");
132 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
137 static int test_bitmaps(struct btrfs_block_group_cache
*cache
)
139 u64 next_bitmap_offset
;
142 test_msg("Running bitmap only tests\n");
144 ret
= test_add_free_space_entry(cache
, 0, 4 * 1024 * 1024, 1);
146 test_msg("Couldn't create a bitmap entry %d\n", ret
);
150 ret
= btrfs_remove_free_space(cache
, 0, 4 * 1024 * 1024);
152 test_msg("Error removing bitmap full range %d\n", ret
);
156 if (test_check_exists(cache
, 0, 4 * 1024 * 1024)) {
157 test_msg("Left some space in bitmap\n");
161 ret
= test_add_free_space_entry(cache
, 0, 4 * 1024 * 1024, 1);
163 test_msg("Couldn't add to our bitmap entry %d\n", ret
);
167 ret
= btrfs_remove_free_space(cache
, 1 * 1024 * 1024, 2 * 1024 * 1024);
169 test_msg("Couldn't remove middle chunk %d\n", ret
);
174 * The first bitmap we have starts at offset 0 so the next one is just
175 * at the end of the first bitmap.
177 next_bitmap_offset
= (u64
)(BITS_PER_BITMAP
* 4096);
179 /* Test a bit straddling two bitmaps */
180 ret
= test_add_free_space_entry(cache
, next_bitmap_offset
-
181 (2 * 1024 * 1024), 4 * 1024 * 1024, 1);
183 test_msg("Couldn't add space that straddles two bitmaps %d\n",
188 ret
= btrfs_remove_free_space(cache
, next_bitmap_offset
-
189 (1 * 1024 * 1024), 2 * 1024 * 1024);
191 test_msg("Couldn't remove overlapping space %d\n", ret
);
195 if (test_check_exists(cache
, next_bitmap_offset
- (1 * 1024 * 1024),
197 test_msg("Left some space when removing overlapping\n");
201 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
206 /* This is the high grade jackassery */
207 static int test_bitmaps_and_extents(struct btrfs_block_group_cache
*cache
)
209 u64 bitmap_offset
= (u64
)(BITS_PER_BITMAP
* 4096);
212 test_msg("Running bitmap and extent tests\n");
215 * First let's do something simple, an extent at the same offset as the
216 * bitmap, but the free space completely in the extent and then
217 * completely in the bitmap.
219 ret
= test_add_free_space_entry(cache
, 4 * 1024 * 1024, 1 * 1024 * 1024, 1);
221 test_msg("Couldn't create bitmap entry %d\n", ret
);
225 ret
= test_add_free_space_entry(cache
, 0, 1 * 1024 * 1024, 0);
227 test_msg("Couldn't add extent entry %d\n", ret
);
231 ret
= btrfs_remove_free_space(cache
, 0, 1 * 1024 * 1024);
233 test_msg("Couldn't remove extent entry %d\n", ret
);
237 if (test_check_exists(cache
, 0, 1 * 1024 * 1024)) {
238 test_msg("Left remnants after our remove\n");
242 /* Now to add back the extent entry and remove from the bitmap */
243 ret
= test_add_free_space_entry(cache
, 0, 1 * 1024 * 1024, 0);
245 test_msg("Couldn't re-add extent entry %d\n", ret
);
249 ret
= btrfs_remove_free_space(cache
, 4 * 1024 * 1024, 1 * 1024 * 1024);
251 test_msg("Couldn't remove from bitmap %d\n", ret
);
255 if (test_check_exists(cache
, 4 * 1024 * 1024, 1 * 1024 * 1024)) {
256 test_msg("Left remnants in the bitmap\n");
261 * Ok so a little more evil, extent entry and bitmap at the same offset,
262 * removing an overlapping chunk.
264 ret
= test_add_free_space_entry(cache
, 1 * 1024 * 1024, 4 * 1024 * 1024, 1);
266 test_msg("Couldn't add to a bitmap %d\n", ret
);
270 ret
= btrfs_remove_free_space(cache
, 512 * 1024, 3 * 1024 * 1024);
272 test_msg("Couldn't remove overlapping space %d\n", ret
);
276 if (test_check_exists(cache
, 512 * 1024, 3 * 1024 * 1024)) {
277 test_msg("Left over pieces after removing overlapping\n");
281 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
283 /* Now with the extent entry offset into the bitmap */
284 ret
= test_add_free_space_entry(cache
, 4 * 1024 * 1024, 4 * 1024 * 1024, 1);
286 test_msg("Couldn't add space to the bitmap %d\n", ret
);
290 ret
= test_add_free_space_entry(cache
, 2 * 1024 * 1024, 2 * 1024 * 1024, 0);
292 test_msg("Couldn't add extent to the cache %d\n", ret
);
296 ret
= btrfs_remove_free_space(cache
, 3 * 1024 * 1024, 4 * 1024 * 1024);
298 test_msg("Problem removing overlapping space %d\n", ret
);
302 if (test_check_exists(cache
, 3 * 1024 * 1024, 4 * 1024 * 1024)) {
303 test_msg("Left something behind when removing space");
308 * This has blown up in the past, the extent entry starts before the
309 * bitmap entry, but we're trying to remove an offset that falls
310 * completely within the bitmap range and is in both the extent entry
311 * and the bitmap entry, looks like this
317 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
318 ret
= test_add_free_space_entry(cache
, bitmap_offset
+ 4 * 1024 * 1024,
321 test_msg("Couldn't add bitmap %d\n", ret
);
325 ret
= test_add_free_space_entry(cache
, bitmap_offset
- 1 * 1024 * 1024,
328 test_msg("Couldn't add extent entry %d\n", ret
);
332 ret
= btrfs_remove_free_space(cache
, bitmap_offset
+ 1 * 1024 * 1024,
335 test_msg("Failed to free our space %d\n", ret
);
339 if (test_check_exists(cache
, bitmap_offset
+ 1 * 1024 * 1024,
341 test_msg("Left stuff over\n");
345 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
348 * This blew up before, we have part of the free space in a bitmap and
349 * then the entirety of the rest of the space in an extent. This used
350 * to return -EAGAIN back from btrfs_remove_extent, make sure this
353 ret
= test_add_free_space_entry(cache
, 1 * 1024 * 1024, 2 * 1024 * 1024, 1);
355 test_msg("Couldn't add bitmap entry %d\n", ret
);
359 ret
= test_add_free_space_entry(cache
, 3 * 1024 * 1024, 1 * 1024 * 1024, 0);
361 test_msg("Couldn't add extent entry %d\n", ret
);
365 ret
= btrfs_remove_free_space(cache
, 1 * 1024 * 1024, 3 * 1024 * 1024);
367 test_msg("Error removing bitmap and extent overlapping %d\n", ret
);
371 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
375 /* Used by test_steal_space_from_bitmap_to_extent(). */
376 static bool test_use_bitmap(struct btrfs_free_space_ctl
*ctl
,
377 struct btrfs_free_space
*info
)
379 return ctl
->free_extents
> 0;
382 /* Used by test_steal_space_from_bitmap_to_extent(). */
384 check_num_extents_and_bitmaps(const struct btrfs_block_group_cache
*cache
,
385 const int num_extents
,
386 const int num_bitmaps
)
388 if (cache
->free_space_ctl
->free_extents
!= num_extents
) {
389 test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
390 cache
->free_space_ctl
->free_extents
, num_extents
);
393 if (cache
->free_space_ctl
->total_bitmaps
!= num_bitmaps
) {
394 test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
395 cache
->free_space_ctl
->total_bitmaps
, num_bitmaps
);
401 /* Used by test_steal_space_from_bitmap_to_extent(). */
402 static int check_cache_empty(struct btrfs_block_group_cache
*cache
)
408 * Now lets confirm that there's absolutely no free space left to
411 if (cache
->free_space_ctl
->free_space
!= 0) {
412 test_msg("Cache free space is not 0\n");
416 /* And any allocation request, no matter how small, should fail now. */
417 offset
= btrfs_find_space_for_alloc(cache
, 0, 4096, 0,
420 test_msg("Space allocation did not fail, returned offset: %llu",
425 /* And no extent nor bitmap entries in the cache anymore. */
426 return check_num_extents_and_bitmaps(cache
, 0, 0);
430 * Before we were able to steal free space from a bitmap entry to an extent
431 * entry, we could end up with 2 entries representing a contiguous free space.
432 * One would be an extent entry and the other a bitmap entry. Since in order
433 * to allocate space to a caller we use only 1 entry, we couldn't return that
434 * whole range to the caller if it was requested. This forced the caller to
435 * either assume ENOSPC or perform several smaller space allocations, which
436 * wasn't optimal as they could be spread all over the block group while under
437 * concurrency (extra overhead and fragmentation).
439 * This stealing approach is benefical, since we always prefer to allocate from
440 * extent entries, both for clustered and non-clustered allocation requests.
443 test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache
*cache
)
449 bool (*use_bitmap_op
)(struct btrfs_free_space_ctl
*,
450 struct btrfs_free_space
*);
452 test_msg("Running space stealing from bitmap to extent\n");
455 * For this test, we want to ensure we end up with an extent entry
456 * immediately adjacent to a bitmap entry, where the bitmap starts
457 * at an offset where the extent entry ends. We keep adding and
458 * removing free space to reach into this state, but to get there
459 * we need to reach a point where marking new free space doesn't
460 * result in adding new extent entries or merging the new space
461 * with existing extent entries - the space ends up being marked
462 * in an existing bitmap that covers the new free space range.
464 * To get there, we need to reach the threshold defined set at
465 * cache->free_space_ctl->extents_thresh, which currently is
466 * 256 extents on a x86_64 system at least, and a few other
467 * conditions (check free_space_cache.c). Instead of making the
468 * test much longer and complicated, use a "use_bitmap" operation
469 * that forces use of bitmaps as soon as we have at least 1
472 use_bitmap_op
= cache
->free_space_ctl
->op
->use_bitmap
;
473 cache
->free_space_ctl
->op
->use_bitmap
= test_use_bitmap
;
476 * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
478 ret
= test_add_free_space_entry(cache
, 128 * 1024 * 1024 - 256 * 1024,
481 test_msg("Couldn't add extent entry %d\n", ret
);
485 /* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
486 ret
= test_add_free_space_entry(cache
, 128 * 1024 * 1024 + 512 * 1024,
487 128 * 1024 * 1024 - 512 * 1024, 1);
489 test_msg("Couldn't add bitmap entry %d\n", ret
);
493 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
498 * Now make only the first 256Kb of the bitmap marked as free, so that
499 * we end up with only the following ranges marked as free space:
501 * [128Mb - 256Kb, 128Mb - 128Kb[
502 * [128Mb + 512Kb, 128Mb + 768Kb[
504 ret
= btrfs_remove_free_space(cache
,
505 128 * 1024 * 1024 + 768 * 1024,
506 128 * 1024 * 1024 - 768 * 1024);
508 test_msg("Failed to free part of bitmap space %d\n", ret
);
512 /* Confirm that only those 2 ranges are marked as free. */
513 if (!test_check_exists(cache
, 128 * 1024 * 1024 - 256 * 1024,
515 test_msg("Free space range missing\n");
518 if (!test_check_exists(cache
, 128 * 1024 * 1024 + 512 * 1024,
520 test_msg("Free space range missing\n");
525 * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
528 if (test_check_exists(cache
, 128 * 1024 * 1024 + 768 * 1024,
529 128 * 1024 * 1024 - 768 * 1024)) {
530 test_msg("Bitmap region not removed from space cache\n");
535 * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
536 * covered by the bitmap, isn't marked as free.
538 if (test_check_exists(cache
, 128 * 1024 * 1024 + 256 * 1024,
540 test_msg("Invalid bitmap region marked as free\n");
545 * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
546 * by the bitmap too, isn't marked as free either.
548 if (test_check_exists(cache
, 128 * 1024 * 1024,
550 test_msg("Invalid bitmap region marked as free\n");
555 * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
556 * lets make sure the free space cache marks it as free in the bitmap,
557 * and doesn't insert a new extent entry to represent this region.
559 ret
= btrfs_add_free_space(cache
, 128 * 1024 * 1024, 512 * 1024);
561 test_msg("Error adding free space: %d\n", ret
);
564 /* Confirm the region is marked as free. */
565 if (!test_check_exists(cache
, 128 * 1024 * 1024, 512 * 1024)) {
566 test_msg("Bitmap region not marked as free\n");
571 * Confirm that no new extent entries or bitmap entries were added to
572 * the cache after adding that free space region.
574 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
579 * Now lets add a small free space region to the right of the previous
580 * one, which is not contiguous with it and is part of the bitmap too.
581 * The goal is to test that the bitmap entry space stealing doesn't
582 * steal this space region.
584 ret
= btrfs_add_free_space(cache
, 128 * 1024 * 1024 + 16 * 1024 * 1024,
587 test_msg("Error adding free space: %d\n", ret
);
592 * Confirm that no new extent entries or bitmap entries were added to
593 * the cache after adding that free space region.
595 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
600 * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
601 * expand the range covered by the existing extent entry that represents
602 * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
604 ret
= btrfs_add_free_space(cache
, 128 * 1024 * 1024 - 128 * 1024,
607 test_msg("Error adding free space: %d\n", ret
);
610 /* Confirm the region is marked as free. */
611 if (!test_check_exists(cache
, 128 * 1024 * 1024 - 128 * 1024,
613 test_msg("Extent region not marked as free\n");
618 * Confirm that our extent entry didn't stole all free space from the
619 * bitmap, because of the small 4Kb free space region.
621 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
626 * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
627 * space. Without stealing bitmap free space into extent entry space,
628 * we would have all this free space represented by 2 entries in the
631 * extent entry covering range: [128Mb - 256Kb, 128Mb[
632 * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
634 * Attempting to allocate the whole free space (1Mb) would fail, because
635 * we can't allocate from multiple entries.
636 * With the bitmap free space stealing, we get a single extent entry
637 * that represents the 1Mb free space, and therefore we're able to
638 * allocate the whole free space at once.
640 if (!test_check_exists(cache
, 128 * 1024 * 1024 - 256 * 1024,
642 test_msg("Expected region not marked as free\n");
646 if (cache
->free_space_ctl
->free_space
!= (1 * 1024 * 1024 + 4096)) {
647 test_msg("Cache free space is not 1Mb + 4Kb\n");
651 offset
= btrfs_find_space_for_alloc(cache
,
652 0, 1 * 1024 * 1024, 0,
654 if (offset
!= (128 * 1024 * 1024 - 256 * 1024)) {
655 test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
660 /* All that remains is a 4Kb free space region in a bitmap. Confirm. */
661 ret
= check_num_extents_and_bitmaps(cache
, 1, 1);
665 if (cache
->free_space_ctl
->free_space
!= 4096) {
666 test_msg("Cache free space is not 4Kb\n");
670 offset
= btrfs_find_space_for_alloc(cache
,
673 if (offset
!= (128 * 1024 * 1024 + 16 * 1024 * 1024)) {
674 test_msg("Failed to allocate 4Kb from space cache, returned offset is: %llu\n",
679 ret
= check_cache_empty(cache
);
683 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
686 * Now test a similar scenario, but where our extent entry is located
687 * to the right of the bitmap entry, so that we can check that stealing
688 * space from a bitmap to the front of an extent entry works.
692 * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
694 ret
= test_add_free_space_entry(cache
, 128 * 1024 * 1024 + 128 * 1024,
697 test_msg("Couldn't add extent entry %d\n", ret
);
701 /* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
702 ret
= test_add_free_space_entry(cache
, 0,
703 128 * 1024 * 1024 - 512 * 1024, 1);
705 test_msg("Couldn't add bitmap entry %d\n", ret
);
709 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
714 * Now make only the last 256Kb of the bitmap marked as free, so that
715 * we end up with only the following ranges marked as free space:
717 * [128Mb + 128b, 128Mb + 256Kb[
718 * [128Mb - 768Kb, 128Mb - 512Kb[
720 ret
= btrfs_remove_free_space(cache
,
722 128 * 1024 * 1024 - 768 * 1024);
724 test_msg("Failed to free part of bitmap space %d\n", ret
);
728 /* Confirm that only those 2 ranges are marked as free. */
729 if (!test_check_exists(cache
, 128 * 1024 * 1024 + 128 * 1024,
731 test_msg("Free space range missing\n");
734 if (!test_check_exists(cache
, 128 * 1024 * 1024 - 768 * 1024,
736 test_msg("Free space range missing\n");
741 * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
744 if (test_check_exists(cache
, 0,
745 128 * 1024 * 1024 - 768 * 1024)) {
746 test_msg("Bitmap region not removed from space cache\n");
751 * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
752 * covered by the bitmap, isn't marked as free.
754 if (test_check_exists(cache
, 128 * 1024 * 1024 - 512 * 1024,
756 test_msg("Invalid bitmap region marked as free\n");
761 * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
762 * lets make sure the free space cache marks it as free in the bitmap,
763 * and doesn't insert a new extent entry to represent this region.
765 ret
= btrfs_add_free_space(cache
, 128 * 1024 * 1024 - 512 * 1024,
768 test_msg("Error adding free space: %d\n", ret
);
771 /* Confirm the region is marked as free. */
772 if (!test_check_exists(cache
, 128 * 1024 * 1024 - 512 * 1024,
774 test_msg("Bitmap region not marked as free\n");
779 * Confirm that no new extent entries or bitmap entries were added to
780 * the cache after adding that free space region.
782 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
787 * Now lets add a small free space region to the left of the previous
788 * one, which is not contiguous with it and is part of the bitmap too.
789 * The goal is to test that the bitmap entry space stealing doesn't
790 * steal this space region.
792 ret
= btrfs_add_free_space(cache
, 32 * 1024 * 1024, 8192);
794 test_msg("Error adding free space: %d\n", ret
);
799 * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
800 * expand the range covered by the existing extent entry that represents
801 * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
803 ret
= btrfs_add_free_space(cache
, 128 * 1024 * 1024, 128 * 1024);
805 test_msg("Error adding free space: %d\n", ret
);
808 /* Confirm the region is marked as free. */
809 if (!test_check_exists(cache
, 128 * 1024 * 1024, 128 * 1024)) {
810 test_msg("Extent region not marked as free\n");
815 * Confirm that our extent entry didn't stole all free space from the
816 * bitmap, because of the small 8Kb free space region.
818 ret
= check_num_extents_and_bitmaps(cache
, 2, 1);
823 * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
824 * space. Without stealing bitmap free space into extent entry space,
825 * we would have all this free space represented by 2 entries in the
828 * extent entry covering range: [128Mb, 128Mb + 256Kb[
829 * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
831 * Attempting to allocate the whole free space (1Mb) would fail, because
832 * we can't allocate from multiple entries.
833 * With the bitmap free space stealing, we get a single extent entry
834 * that represents the 1Mb free space, and therefore we're able to
835 * allocate the whole free space at once.
837 if (!test_check_exists(cache
, 128 * 1024 * 1024 - 768 * 1024,
839 test_msg("Expected region not marked as free\n");
843 if (cache
->free_space_ctl
->free_space
!= (1 * 1024 * 1024 + 8192)) {
844 test_msg("Cache free space is not 1Mb + 8Kb\n");
848 offset
= btrfs_find_space_for_alloc(cache
,
849 0, 1 * 1024 * 1024, 0,
851 if (offset
!= (128 * 1024 * 1024 - 768 * 1024)) {
852 test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
857 /* All that remains is a 8Kb free space region in a bitmap. Confirm. */
858 ret
= check_num_extents_and_bitmaps(cache
, 1, 1);
862 if (cache
->free_space_ctl
->free_space
!= 8192) {
863 test_msg("Cache free space is not 8Kb\n");
867 offset
= btrfs_find_space_for_alloc(cache
,
870 if (offset
!= (32 * 1024 * 1024)) {
871 test_msg("Failed to allocate 8Kb from space cache, returned offset is: %llu\n",
876 ret
= check_cache_empty(cache
);
880 cache
->free_space_ctl
->op
->use_bitmap
= use_bitmap_op
;
881 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
886 int btrfs_test_free_space_cache(void)
888 struct btrfs_block_group_cache
*cache
;
889 struct btrfs_root
*root
= NULL
;
892 test_msg("Running btrfs free space cache tests\n");
894 cache
= init_test_block_group();
896 test_msg("Couldn't run the tests\n");
900 root
= btrfs_alloc_dummy_root();
906 root
->fs_info
= btrfs_alloc_dummy_fs_info();
910 root
->fs_info
->extent_root
= root
;
911 cache
->fs_info
= root
->fs_info
;
913 ret
= test_extents(cache
);
916 ret
= test_bitmaps(cache
);
919 ret
= test_bitmaps_and_extents(cache
);
923 ret
= test_steal_space_from_bitmap_to_extent(cache
);
925 __btrfs_remove_free_space_cache(cache
->free_space_ctl
);
926 kfree(cache
->free_space_ctl
);
928 btrfs_free_dummy_root(root
);
929 test_msg("Free space cache tests finished\n");