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_SIZE * 8)
28 * This test just does basic sanity checking, making sure we can add an exten
29 * entry and remove space from either end and the middle, and make sure we can
30 * remove space that covers adjacent extent entries.
32 static int test_extents(struct btrfs_block_group_cache *cache)
36 test_msg("Running extent only tests\n");
38 /* First just make sure we can remove an entire entry */
39 ret = btrfs_add_free_space(cache, 0, SZ_4M);
41 test_msg("Error adding initial extents %d\n", ret);
45 ret = btrfs_remove_free_space(cache, 0, SZ_4M);
47 test_msg("Error removing extent %d\n", ret);
51 if (test_check_exists(cache, 0, SZ_4M)) {
52 test_msg("Full remove left some lingering space\n");
56 /* Ok edge and middle cases now */
57 ret = btrfs_add_free_space(cache, 0, SZ_4M);
59 test_msg("Error adding half extent %d\n", ret);
63 ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_1M);
65 test_msg("Error removing tail end %d\n", ret);
69 ret = btrfs_remove_free_space(cache, 0, SZ_1M);
71 test_msg("Error removing front end %d\n", ret);
75 ret = btrfs_remove_free_space(cache, SZ_2M, 4096);
77 test_msg("Error removing middle piece %d\n", ret);
81 if (test_check_exists(cache, 0, SZ_1M)) {
82 test_msg("Still have space at the front\n");
86 if (test_check_exists(cache, SZ_2M, 4096)) {
87 test_msg("Still have space in the middle\n");
91 if (test_check_exists(cache, 3 * SZ_1M, SZ_1M)) {
92 test_msg("Still have space at the end\n");
97 __btrfs_remove_free_space_cache(cache->free_space_ctl);
102 static int test_bitmaps(struct btrfs_block_group_cache *cache)
104 u64 next_bitmap_offset;
107 test_msg("Running bitmap only tests\n");
109 ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
111 test_msg("Couldn't create a bitmap entry %d\n", ret);
115 ret = btrfs_remove_free_space(cache, 0, SZ_4M);
117 test_msg("Error removing bitmap full range %d\n", ret);
121 if (test_check_exists(cache, 0, SZ_4M)) {
122 test_msg("Left some space in bitmap\n");
126 ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
128 test_msg("Couldn't add to our bitmap entry %d\n", ret);
132 ret = btrfs_remove_free_space(cache, SZ_1M, SZ_2M);
134 test_msg("Couldn't remove middle chunk %d\n", ret);
139 * The first bitmap we have starts at offset 0 so the next one is just
140 * at the end of the first bitmap.
142 next_bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
144 /* Test a bit straddling two bitmaps */
145 ret = test_add_free_space_entry(cache, next_bitmap_offset - SZ_2M,
148 test_msg("Couldn't add space that straddles two bitmaps %d\n",
153 ret = btrfs_remove_free_space(cache, next_bitmap_offset - SZ_1M, SZ_2M);
155 test_msg("Couldn't remove overlapping space %d\n", ret);
159 if (test_check_exists(cache, next_bitmap_offset - SZ_1M, SZ_2M)) {
160 test_msg("Left some space when removing overlapping\n");
164 __btrfs_remove_free_space_cache(cache->free_space_ctl);
169 /* This is the high grade jackassery */
170 static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
172 u64 bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
175 test_msg("Running bitmap and extent tests\n");
178 * First let's do something simple, an extent at the same offset as the
179 * bitmap, but the free space completely in the extent and then
180 * completely in the bitmap.
182 ret = test_add_free_space_entry(cache, SZ_4M, SZ_1M, 1);
184 test_msg("Couldn't create bitmap entry %d\n", ret);
188 ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
190 test_msg("Couldn't add extent entry %d\n", ret);
194 ret = btrfs_remove_free_space(cache, 0, SZ_1M);
196 test_msg("Couldn't remove extent entry %d\n", ret);
200 if (test_check_exists(cache, 0, SZ_1M)) {
201 test_msg("Left remnants after our remove\n");
205 /* Now to add back the extent entry and remove from the bitmap */
206 ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
208 test_msg("Couldn't re-add extent entry %d\n", ret);
212 ret = btrfs_remove_free_space(cache, SZ_4M, SZ_1M);
214 test_msg("Couldn't remove from bitmap %d\n", ret);
218 if (test_check_exists(cache, SZ_4M, SZ_1M)) {
219 test_msg("Left remnants in the bitmap\n");
224 * Ok so a little more evil, extent entry and bitmap at the same offset,
225 * removing an overlapping chunk.
227 ret = test_add_free_space_entry(cache, SZ_1M, SZ_4M, 1);
229 test_msg("Couldn't add to a bitmap %d\n", ret);
233 ret = btrfs_remove_free_space(cache, SZ_512K, 3 * SZ_1M);
235 test_msg("Couldn't remove overlapping space %d\n", ret);
239 if (test_check_exists(cache, SZ_512K, 3 * SZ_1M)) {
240 test_msg("Left over pieces after removing overlapping\n");
244 __btrfs_remove_free_space_cache(cache->free_space_ctl);
246 /* Now with the extent entry offset into the bitmap */
247 ret = test_add_free_space_entry(cache, SZ_4M, SZ_4M, 1);
249 test_msg("Couldn't add space to the bitmap %d\n", ret);
253 ret = test_add_free_space_entry(cache, SZ_2M, SZ_2M, 0);
255 test_msg("Couldn't add extent to the cache %d\n", ret);
259 ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_4M);
261 test_msg("Problem removing overlapping space %d\n", ret);
265 if (test_check_exists(cache, 3 * SZ_1M, SZ_4M)) {
266 test_msg("Left something behind when removing space");
271 * This has blown up in the past, the extent entry starts before the
272 * bitmap entry, but we're trying to remove an offset that falls
273 * completely within the bitmap range and is in both the extent entry
274 * and the bitmap entry, looks like this
280 __btrfs_remove_free_space_cache(cache->free_space_ctl);
281 ret = test_add_free_space_entry(cache, bitmap_offset + SZ_4M, SZ_4M, 1);
283 test_msg("Couldn't add bitmap %d\n", ret);
287 ret = test_add_free_space_entry(cache, bitmap_offset - SZ_1M,
290 test_msg("Couldn't add extent entry %d\n", ret);
294 ret = btrfs_remove_free_space(cache, bitmap_offset + SZ_1M, 5 * SZ_1M);
296 test_msg("Failed to free our space %d\n", ret);
300 if (test_check_exists(cache, bitmap_offset + SZ_1M, 5 * SZ_1M)) {
301 test_msg("Left stuff over\n");
305 __btrfs_remove_free_space_cache(cache->free_space_ctl);
308 * This blew up before, we have part of the free space in a bitmap and
309 * then the entirety of the rest of the space in an extent. This used
310 * to return -EAGAIN back from btrfs_remove_extent, make sure this
313 ret = test_add_free_space_entry(cache, SZ_1M, SZ_2M, 1);
315 test_msg("Couldn't add bitmap entry %d\n", ret);
319 ret = test_add_free_space_entry(cache, 3 * SZ_1M, SZ_1M, 0);
321 test_msg("Couldn't add extent entry %d\n", ret);
325 ret = btrfs_remove_free_space(cache, SZ_1M, 3 * SZ_1M);
327 test_msg("Error removing bitmap and extent overlapping %d\n", ret);
331 __btrfs_remove_free_space_cache(cache->free_space_ctl);
335 /* Used by test_steal_space_from_bitmap_to_extent(). */
336 static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl,
337 struct btrfs_free_space *info)
339 return ctl->free_extents > 0;
342 /* Used by test_steal_space_from_bitmap_to_extent(). */
344 check_num_extents_and_bitmaps(const struct btrfs_block_group_cache *cache,
345 const int num_extents,
346 const int num_bitmaps)
348 if (cache->free_space_ctl->free_extents != num_extents) {
349 test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
350 cache->free_space_ctl->free_extents, num_extents);
353 if (cache->free_space_ctl->total_bitmaps != num_bitmaps) {
354 test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
355 cache->free_space_ctl->total_bitmaps, num_bitmaps);
361 /* Used by test_steal_space_from_bitmap_to_extent(). */
362 static int check_cache_empty(struct btrfs_block_group_cache *cache)
368 * Now lets confirm that there's absolutely no free space left to
371 if (cache->free_space_ctl->free_space != 0) {
372 test_msg("Cache free space is not 0\n");
376 /* And any allocation request, no matter how small, should fail now. */
377 offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0,
380 test_msg("Space allocation did not fail, returned offset: %llu",
385 /* And no extent nor bitmap entries in the cache anymore. */
386 return check_num_extents_and_bitmaps(cache, 0, 0);
390 * Before we were able to steal free space from a bitmap entry to an extent
391 * entry, we could end up with 2 entries representing a contiguous free space.
392 * One would be an extent entry and the other a bitmap entry. Since in order
393 * to allocate space to a caller we use only 1 entry, we couldn't return that
394 * whole range to the caller if it was requested. This forced the caller to
395 * either assume ENOSPC or perform several smaller space allocations, which
396 * wasn't optimal as they could be spread all over the block group while under
397 * concurrency (extra overhead and fragmentation).
399 * This stealing approach is benefical, since we always prefer to allocate from
400 * extent entries, both for clustered and non-clustered allocation requests.
403 test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
408 const struct btrfs_free_space_op test_free_space_ops = {
409 .recalc_thresholds = cache->free_space_ctl->op->recalc_thresholds,
410 .use_bitmap = test_use_bitmap,
412 const struct btrfs_free_space_op *orig_free_space_ops;
414 test_msg("Running space stealing from bitmap to extent\n");
417 * For this test, we want to ensure we end up with an extent entry
418 * immediately adjacent to a bitmap entry, where the bitmap starts
419 * at an offset where the extent entry ends. We keep adding and
420 * removing free space to reach into this state, but to get there
421 * we need to reach a point where marking new free space doesn't
422 * result in adding new extent entries or merging the new space
423 * with existing extent entries - the space ends up being marked
424 * in an existing bitmap that covers the new free space range.
426 * To get there, we need to reach the threshold defined set at
427 * cache->free_space_ctl->extents_thresh, which currently is
428 * 256 extents on a x86_64 system at least, and a few other
429 * conditions (check free_space_cache.c). Instead of making the
430 * test much longer and complicated, use a "use_bitmap" operation
431 * that forces use of bitmaps as soon as we have at least 1
434 orig_free_space_ops = cache->free_space_ctl->op;
435 cache->free_space_ctl->op = &test_free_space_ops;
438 * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
440 ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0);
442 test_msg("Couldn't add extent entry %d\n", ret);
446 /* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
447 ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K,
448 SZ_128M - SZ_512K, 1);
450 test_msg("Couldn't add bitmap entry %d\n", ret);
454 ret = check_num_extents_and_bitmaps(cache, 2, 1);
459 * Now make only the first 256Kb of the bitmap marked as free, so that
460 * we end up with only the following ranges marked as free space:
462 * [128Mb - 256Kb, 128Mb - 128Kb[
463 * [128Mb + 512Kb, 128Mb + 768Kb[
465 ret = btrfs_remove_free_space(cache,
466 SZ_128M + 768 * SZ_1K,
467 SZ_128M - 768 * SZ_1K);
469 test_msg("Failed to free part of bitmap space %d\n", ret);
473 /* Confirm that only those 2 ranges are marked as free. */
474 if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) {
475 test_msg("Free space range missing\n");
478 if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) {
479 test_msg("Free space range missing\n");
484 * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
487 if (test_check_exists(cache, SZ_128M + 768 * SZ_1K,
488 SZ_128M - 768 * SZ_1K)) {
489 test_msg("Bitmap region not removed from space cache\n");
494 * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
495 * covered by the bitmap, isn't marked as free.
497 if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) {
498 test_msg("Invalid bitmap region marked as free\n");
503 * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
504 * by the bitmap too, isn't marked as free either.
506 if (test_check_exists(cache, SZ_128M, SZ_256K)) {
507 test_msg("Invalid bitmap region marked as free\n");
512 * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
513 * lets make sure the free space cache marks it as free in the bitmap,
514 * and doesn't insert a new extent entry to represent this region.
516 ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K);
518 test_msg("Error adding free space: %d\n", ret);
521 /* Confirm the region is marked as free. */
522 if (!test_check_exists(cache, SZ_128M, SZ_512K)) {
523 test_msg("Bitmap region not marked as free\n");
528 * Confirm that no new extent entries or bitmap entries were added to
529 * the cache after adding that free space region.
531 ret = check_num_extents_and_bitmaps(cache, 2, 1);
536 * Now lets add a small free space region to the right of the previous
537 * one, which is not contiguous with it and is part of the bitmap too.
538 * The goal is to test that the bitmap entry space stealing doesn't
539 * steal this space region.
541 ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, 4096);
543 test_msg("Error adding free space: %d\n", ret);
548 * Confirm that no new extent entries or bitmap entries were added to
549 * the cache after adding that free space region.
551 ret = check_num_extents_and_bitmaps(cache, 2, 1);
556 * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
557 * expand the range covered by the existing extent entry that represents
558 * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
560 ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K);
562 test_msg("Error adding free space: %d\n", ret);
565 /* Confirm the region is marked as free. */
566 if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) {
567 test_msg("Extent region not marked as free\n");
572 * Confirm that our extent entry didn't stole all free space from the
573 * bitmap, because of the small 4Kb free space region.
575 ret = check_num_extents_and_bitmaps(cache, 2, 1);
580 * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
581 * space. Without stealing bitmap free space into extent entry space,
582 * we would have all this free space represented by 2 entries in the
585 * extent entry covering range: [128Mb - 256Kb, 128Mb[
586 * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
588 * Attempting to allocate the whole free space (1Mb) would fail, because
589 * we can't allocate from multiple entries.
590 * With the bitmap free space stealing, we get a single extent entry
591 * that represents the 1Mb free space, and therefore we're able to
592 * allocate the whole free space at once.
594 if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) {
595 test_msg("Expected region not marked as free\n");
599 if (cache->free_space_ctl->free_space != (SZ_1M + 4096)) {
600 test_msg("Cache free space is not 1Mb + 4Kb\n");
604 offset = btrfs_find_space_for_alloc(cache,
607 if (offset != (SZ_128M - SZ_256K)) {
608 test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
613 /* All that remains is a 4Kb free space region in a bitmap. Confirm. */
614 ret = check_num_extents_and_bitmaps(cache, 1, 1);
618 if (cache->free_space_ctl->free_space != 4096) {
619 test_msg("Cache free space is not 4Kb\n");
623 offset = btrfs_find_space_for_alloc(cache,
626 if (offset != (SZ_128M + SZ_16M)) {
627 test_msg("Failed to allocate 4Kb from space cache, returned offset is: %llu\n",
632 ret = check_cache_empty(cache);
636 __btrfs_remove_free_space_cache(cache->free_space_ctl);
639 * Now test a similar scenario, but where our extent entry is located
640 * to the right of the bitmap entry, so that we can check that stealing
641 * space from a bitmap to the front of an extent entry works.
645 * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
647 ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0);
649 test_msg("Couldn't add extent entry %d\n", ret);
653 /* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
654 ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1);
656 test_msg("Couldn't add bitmap entry %d\n", ret);
660 ret = check_num_extents_and_bitmaps(cache, 2, 1);
665 * Now make only the last 256Kb of the bitmap marked as free, so that
666 * we end up with only the following ranges marked as free space:
668 * [128Mb + 128b, 128Mb + 256Kb[
669 * [128Mb - 768Kb, 128Mb - 512Kb[
671 ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K);
673 test_msg("Failed to free part of bitmap space %d\n", ret);
677 /* Confirm that only those 2 ranges are marked as free. */
678 if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) {
679 test_msg("Free space range missing\n");
682 if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) {
683 test_msg("Free space range missing\n");
688 * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
691 if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) {
692 test_msg("Bitmap region not removed from space cache\n");
697 * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
698 * covered by the bitmap, isn't marked as free.
700 if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
701 test_msg("Invalid bitmap region marked as free\n");
706 * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
707 * lets make sure the free space cache marks it as free in the bitmap,
708 * and doesn't insert a new extent entry to represent this region.
710 ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K);
712 test_msg("Error adding free space: %d\n", ret);
715 /* Confirm the region is marked as free. */
716 if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
717 test_msg("Bitmap region not marked as free\n");
722 * Confirm that no new extent entries or bitmap entries were added to
723 * the cache after adding that free space region.
725 ret = check_num_extents_and_bitmaps(cache, 2, 1);
730 * Now lets add a small free space region to the left of the previous
731 * one, which is not contiguous with it and is part of the bitmap too.
732 * The goal is to test that the bitmap entry space stealing doesn't
733 * steal this space region.
735 ret = btrfs_add_free_space(cache, SZ_32M, 8192);
737 test_msg("Error adding free space: %d\n", ret);
742 * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
743 * expand the range covered by the existing extent entry that represents
744 * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
746 ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K);
748 test_msg("Error adding free space: %d\n", ret);
751 /* Confirm the region is marked as free. */
752 if (!test_check_exists(cache, SZ_128M, SZ_128K)) {
753 test_msg("Extent region not marked as free\n");
758 * Confirm that our extent entry didn't stole all free space from the
759 * bitmap, because of the small 8Kb free space region.
761 ret = check_num_extents_and_bitmaps(cache, 2, 1);
766 * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
767 * space. Without stealing bitmap free space into extent entry space,
768 * we would have all this free space represented by 2 entries in the
771 * extent entry covering range: [128Mb, 128Mb + 256Kb[
772 * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
774 * Attempting to allocate the whole free space (1Mb) would fail, because
775 * we can't allocate from multiple entries.
776 * With the bitmap free space stealing, we get a single extent entry
777 * that represents the 1Mb free space, and therefore we're able to
778 * allocate the whole free space at once.
780 if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) {
781 test_msg("Expected region not marked as free\n");
785 if (cache->free_space_ctl->free_space != (SZ_1M + 8192)) {
786 test_msg("Cache free space is not 1Mb + 8Kb\n");
790 offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0,
792 if (offset != (SZ_128M - 768 * SZ_1K)) {
793 test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
798 /* All that remains is a 8Kb free space region in a bitmap. Confirm. */
799 ret = check_num_extents_and_bitmaps(cache, 1, 1);
803 if (cache->free_space_ctl->free_space != 8192) {
804 test_msg("Cache free space is not 8Kb\n");
808 offset = btrfs_find_space_for_alloc(cache,
811 if (offset != SZ_32M) {
812 test_msg("Failed to allocate 8Kb from space cache, returned offset is: %llu\n",
817 ret = check_cache_empty(cache);
821 cache->free_space_ctl->op = orig_free_space_ops;
822 __btrfs_remove_free_space_cache(cache->free_space_ctl);
827 int btrfs_test_free_space_cache(void)
829 struct btrfs_block_group_cache *cache;
830 struct btrfs_root *root = NULL;
833 test_msg("Running btrfs free space cache tests\n");
835 cache = btrfs_alloc_dummy_block_group(1024 * 1024 * 1024);
837 test_msg("Couldn't run the tests\n");
841 root = btrfs_alloc_dummy_root();
847 root->fs_info = btrfs_alloc_dummy_fs_info();
851 root->fs_info->extent_root = root;
852 cache->fs_info = root->fs_info;
854 ret = test_extents(cache);
857 ret = test_bitmaps(cache);
860 ret = test_bitmaps_and_extents(cache);
864 ret = test_steal_space_from_bitmap_to_extent(cache);
866 btrfs_free_dummy_block_group(cache);
867 btrfs_free_dummy_root(root);
868 test_msg("Free space cache tests finished\n");