Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
[cascardo/linux.git] / fs / btrfs / tests / free-space-tests.c
index 8b72b00..c9ad97b 100644 (file)
 #include "../free-space-cache.h"
 
 #define BITS_PER_BITMAP                (PAGE_CACHE_SIZE * 8)
-static struct btrfs_block_group_cache *init_test_block_group(void)
-{
-       struct btrfs_block_group_cache *cache;
-
-       cache = kzalloc(sizeof(*cache), GFP_NOFS);
-       if (!cache)
-               return NULL;
-       cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
-                                       GFP_NOFS);
-       if (!cache->free_space_ctl) {
-               kfree(cache);
-               return NULL;
-       }
-       cache->fs_info = btrfs_alloc_dummy_fs_info();
-       if (!cache->fs_info) {
-               kfree(cache->free_space_ctl);
-               kfree(cache);
-               return NULL;
-       }
-
-       cache->key.objectid = 0;
-       cache->key.offset = 1024 * 1024 * 1024;
-       cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
-       cache->sectorsize = 4096;
-       cache->full_stripe_len = 4096;
-
-       spin_lock_init(&cache->lock);
-       INIT_LIST_HEAD(&cache->list);
-       INIT_LIST_HEAD(&cache->cluster_list);
-       INIT_LIST_HEAD(&cache->bg_list);
-
-       btrfs_init_free_space_ctl(cache);
-
-       return cache;
-}
 
 /*
  * This test just does basic sanity checking, making sure we can add an exten
@@ -71,59 +36,59 @@ static int test_extents(struct btrfs_block_group_cache *cache)
        test_msg("Running extent only tests\n");
 
        /* First just make sure we can remove an entire entry */
-       ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024);
+       ret = btrfs_add_free_space(cache, 0, SZ_4M);
        if (ret) {
                test_msg("Error adding initial extents %d\n", ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024);
+       ret = btrfs_remove_free_space(cache, 0, SZ_4M);
        if (ret) {
                test_msg("Error removing extent %d\n", ret);
                return ret;
        }
 
-       if (test_check_exists(cache, 0, 4 * 1024 * 1024)) {
+       if (test_check_exists(cache, 0, SZ_4M)) {
                test_msg("Full remove left some lingering space\n");
                return -1;
        }
 
        /* Ok edge and middle cases now */
-       ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024);
+       ret = btrfs_add_free_space(cache, 0, SZ_4M);
        if (ret) {
                test_msg("Error adding half extent %d\n", ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 1 * 1024 * 1024);
+       ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_1M);
        if (ret) {
                test_msg("Error removing tail end %d\n", ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024);
+       ret = btrfs_remove_free_space(cache, 0, SZ_1M);
        if (ret) {
                test_msg("Error removing front end %d\n", ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, 2 * 1024 * 1024, 4096);
+       ret = btrfs_remove_free_space(cache, SZ_2M, 4096);
        if (ret) {
                test_msg("Error removing middle piece %d\n", ret);
                return ret;
        }
 
-       if (test_check_exists(cache, 0, 1 * 1024 * 1024)) {
+       if (test_check_exists(cache, 0, SZ_1M)) {
                test_msg("Still have space at the front\n");
                return -1;
        }
 
-       if (test_check_exists(cache, 2 * 1024 * 1024, 4096)) {
+       if (test_check_exists(cache, SZ_2M, 4096)) {
                test_msg("Still have space in the middle\n");
                return -1;
        }
 
-       if (test_check_exists(cache, 3 * 1024 * 1024, 1 * 1024 * 1024)) {
+       if (test_check_exists(cache, 3 * SZ_1M, SZ_1M)) {
                test_msg("Still have space at the end\n");
                return -1;
        }
@@ -141,30 +106,30 @@ static int test_bitmaps(struct btrfs_block_group_cache *cache)
 
        test_msg("Running bitmap only tests\n");
 
-       ret = test_add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1);
+       ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
        if (ret) {
                test_msg("Couldn't create a bitmap entry %d\n", ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024);
+       ret = btrfs_remove_free_space(cache, 0, SZ_4M);
        if (ret) {
                test_msg("Error removing bitmap full range %d\n", ret);
                return ret;
        }
 
-       if (test_check_exists(cache, 0, 4 * 1024 * 1024)) {
+       if (test_check_exists(cache, 0, SZ_4M)) {
                test_msg("Left some space in bitmap\n");
                return -1;
        }
 
-       ret = test_add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1);
+       ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
        if (ret) {
                test_msg("Couldn't add to our bitmap entry %d\n", ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 2 * 1024 * 1024);
+       ret = btrfs_remove_free_space(cache, SZ_1M, SZ_2M);
        if (ret) {
                test_msg("Couldn't remove middle chunk %d\n", ret);
                return ret;
@@ -177,23 +142,21 @@ static int test_bitmaps(struct btrfs_block_group_cache *cache)
        next_bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
 
        /* Test a bit straddling two bitmaps */
-       ret = test_add_free_space_entry(cache, next_bitmap_offset -
-                                  (2 * 1024 * 1024), 4 * 1024 * 1024, 1);
+       ret = test_add_free_space_entry(cache, next_bitmap_offset - SZ_2M,
+                                       SZ_4M, 1);
        if (ret) {
                test_msg("Couldn't add space that straddles two bitmaps %d\n",
                                ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, next_bitmap_offset -
-                                     (1 * 1024 * 1024), 2 * 1024 * 1024);
+       ret = btrfs_remove_free_space(cache, next_bitmap_offset - SZ_1M, SZ_2M);
        if (ret) {
                test_msg("Couldn't remove overlapping space %d\n", ret);
                return ret;
        }
 
-       if (test_check_exists(cache, next_bitmap_offset - (1 * 1024 * 1024),
-                        2 * 1024 * 1024)) {
+       if (test_check_exists(cache, next_bitmap_offset - SZ_1M, SZ_2M)) {
                test_msg("Left some space when removing overlapping\n");
                return -1;
        }
@@ -216,43 +179,43 @@ static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
         * bitmap, but the free space completely in the extent and then
         * completely in the bitmap.
         */
-       ret = test_add_free_space_entry(cache, 4 * 1024 * 1024, 1 * 1024 * 1024, 1);
+       ret = test_add_free_space_entry(cache, SZ_4M, SZ_1M, 1);
        if (ret) {
                test_msg("Couldn't create bitmap entry %d\n", ret);
                return ret;
        }
 
-       ret = test_add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0);
+       ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
        if (ret) {
                test_msg("Couldn't add extent entry %d\n", ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024);
+       ret = btrfs_remove_free_space(cache, 0, SZ_1M);
        if (ret) {
                test_msg("Couldn't remove extent entry %d\n", ret);
                return ret;
        }
 
-       if (test_check_exists(cache, 0, 1 * 1024 * 1024)) {
+       if (test_check_exists(cache, 0, SZ_1M)) {
                test_msg("Left remnants after our remove\n");
                return -1;
        }
 
        /* Now to add back the extent entry and remove from the bitmap */
-       ret = test_add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0);
+       ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
        if (ret) {
                test_msg("Couldn't re-add extent entry %d\n", ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, 4 * 1024 * 1024, 1 * 1024 * 1024);
+       ret = btrfs_remove_free_space(cache, SZ_4M, SZ_1M);
        if (ret) {
                test_msg("Couldn't remove from bitmap %d\n", ret);
                return ret;
        }
 
-       if (test_check_exists(cache, 4 * 1024 * 1024, 1 * 1024 * 1024)) {
+       if (test_check_exists(cache, SZ_4M, SZ_1M)) {
                test_msg("Left remnants in the bitmap\n");
                return -1;
        }
@@ -261,19 +224,19 @@ static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
         * Ok so a little more evil, extent entry and bitmap at the same offset,
         * removing an overlapping chunk.
         */
-       ret = test_add_free_space_entry(cache, 1 * 1024 * 1024, 4 * 1024 * 1024, 1);
+       ret = test_add_free_space_entry(cache, SZ_1M, SZ_4M, 1);
        if (ret) {
                test_msg("Couldn't add to a bitmap %d\n", ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, 512 * 1024, 3 * 1024 * 1024);
+       ret = btrfs_remove_free_space(cache, SZ_512K, 3 * SZ_1M);
        if (ret) {
                test_msg("Couldn't remove overlapping space %d\n", ret);
                return ret;
        }
 
-       if (test_check_exists(cache, 512 * 1024, 3 * 1024 * 1024)) {
+       if (test_check_exists(cache, SZ_512K, 3 * SZ_1M)) {
                test_msg("Left over pieces after removing overlapping\n");
                return -1;
        }
@@ -281,25 +244,25 @@ static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
        __btrfs_remove_free_space_cache(cache->free_space_ctl);
 
        /* Now with the extent entry offset into the bitmap */
-       ret = test_add_free_space_entry(cache, 4 * 1024 * 1024, 4 * 1024 * 1024, 1);
+       ret = test_add_free_space_entry(cache, SZ_4M, SZ_4M, 1);
        if (ret) {
                test_msg("Couldn't add space to the bitmap %d\n", ret);
                return ret;
        }
 
-       ret = test_add_free_space_entry(cache, 2 * 1024 * 1024, 2 * 1024 * 1024, 0);
+       ret = test_add_free_space_entry(cache, SZ_2M, SZ_2M, 0);
        if (ret) {
                test_msg("Couldn't add extent to the cache %d\n", ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 4 * 1024 * 1024);
+       ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_4M);
        if (ret) {
                test_msg("Problem removing overlapping space %d\n", ret);
                return ret;
        }
 
-       if (test_check_exists(cache, 3 * 1024 * 1024, 4 * 1024 * 1024)) {
+       if (test_check_exists(cache, 3 * SZ_1M, SZ_4M)) {
                test_msg("Left something behind when removing space");
                return -1;
        }
@@ -315,29 +278,26 @@ static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
         *        [ del ]
         */
        __btrfs_remove_free_space_cache(cache->free_space_ctl);
-       ret = test_add_free_space_entry(cache, bitmap_offset + 4 * 1024 * 1024,
-                                  4 * 1024 * 1024, 1);
+       ret = test_add_free_space_entry(cache, bitmap_offset + SZ_4M, SZ_4M, 1);
        if (ret) {
                test_msg("Couldn't add bitmap %d\n", ret);
                return ret;
        }
 
-       ret = test_add_free_space_entry(cache, bitmap_offset - 1 * 1024 * 1024,
-                                  5 * 1024 * 1024, 0);
+       ret = test_add_free_space_entry(cache, bitmap_offset - SZ_1M,
+                                       5 * SZ_1M, 0);
        if (ret) {
                test_msg("Couldn't add extent entry %d\n", ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, bitmap_offset + 1 * 1024 * 1024,
-                                     5 * 1024 * 1024);
+       ret = btrfs_remove_free_space(cache, bitmap_offset + SZ_1M, 5 * SZ_1M);
        if (ret) {
                test_msg("Failed to free our space %d\n", ret);
                return ret;
        }
 
-       if (test_check_exists(cache, bitmap_offset + 1 * 1024 * 1024,
-                        5 * 1024 * 1024)) {
+       if (test_check_exists(cache, bitmap_offset + SZ_1M, 5 * SZ_1M)) {
                test_msg("Left stuff over\n");
                return -1;
        }
@@ -350,19 +310,19 @@ static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
         * to return -EAGAIN back from btrfs_remove_extent, make sure this
         * doesn't happen.
         */
-       ret = test_add_free_space_entry(cache, 1 * 1024 * 1024, 2 * 1024 * 1024, 1);
+       ret = test_add_free_space_entry(cache, SZ_1M, SZ_2M, 1);
        if (ret) {
                test_msg("Couldn't add bitmap entry %d\n", ret);
                return ret;
        }
 
-       ret = test_add_free_space_entry(cache, 3 * 1024 * 1024, 1 * 1024 * 1024, 0);
+       ret = test_add_free_space_entry(cache, 3 * SZ_1M, SZ_1M, 0);
        if (ret) {
                test_msg("Couldn't add extent entry %d\n", ret);
                return ret;
        }
 
-       ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 3 * 1024 * 1024);
+       ret = btrfs_remove_free_space(cache, SZ_1M, 3 * SZ_1M);
        if (ret) {
                test_msg("Error removing bitmap and extent overlapping %d\n", ret);
                return ret;
@@ -445,9 +405,11 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
        int ret;
        u64 offset;
        u64 max_extent_size;
-
-       bool (*use_bitmap_op)(struct btrfs_free_space_ctl *,
-                             struct btrfs_free_space *);
+       const struct btrfs_free_space_op test_free_space_ops = {
+               .recalc_thresholds = cache->free_space_ctl->op->recalc_thresholds,
+               .use_bitmap = test_use_bitmap,
+       };
+       const struct btrfs_free_space_op *orig_free_space_ops;
 
        test_msg("Running space stealing from bitmap to extent\n");
 
@@ -469,22 +431,21 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * that forces use of bitmaps as soon as we have at least 1
         * extent entry.
         */
-       use_bitmap_op = cache->free_space_ctl->op->use_bitmap;
-       cache->free_space_ctl->op->use_bitmap = test_use_bitmap;
+       orig_free_space_ops = cache->free_space_ctl->op;
+       cache->free_space_ctl->op = &test_free_space_ops;
 
        /*
         * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
         */
-       ret = test_add_free_space_entry(cache, 128 * 1024 * 1024 - 256 * 1024,
-                                       128 * 1024, 0);
+       ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0);
        if (ret) {
                test_msg("Couldn't add extent entry %d\n", ret);
                return ret;
        }
 
        /* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
-       ret = test_add_free_space_entry(cache, 128 * 1024 * 1024 + 512 * 1024,
-                                       128 * 1024 * 1024 - 512 * 1024, 1);
+       ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K,
+                                       SZ_128M - SZ_512K, 1);
        if (ret) {
                test_msg("Couldn't add bitmap entry %d\n", ret);
                return ret;
@@ -502,21 +463,19 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * [128Mb + 512Kb, 128Mb + 768Kb[
         */
        ret = btrfs_remove_free_space(cache,
-                                     128 * 1024 * 1024 + 768 * 1024,
-                                     128 * 1024 * 1024 - 768 * 1024);
+                                     SZ_128M + 768 * SZ_1K,
+                                     SZ_128M - 768 * SZ_1K);
        if (ret) {
                test_msg("Failed to free part of bitmap space %d\n", ret);
                return ret;
        }
 
        /* Confirm that only those 2 ranges are marked as free. */
-       if (!test_check_exists(cache, 128 * 1024 * 1024 - 256 * 1024,
-                              128 * 1024)) {
+       if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) {
                test_msg("Free space range missing\n");
                return -ENOENT;
        }
-       if (!test_check_exists(cache, 128 * 1024 * 1024 + 512 * 1024,
-                              256 * 1024)) {
+       if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) {
                test_msg("Free space range missing\n");
                return -ENOENT;
        }
@@ -525,8 +484,8 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
         * as free anymore.
         */
-       if (test_check_exists(cache, 128 * 1024 * 1024 + 768 * 1024,
-                             128 * 1024 * 1024 - 768 * 1024)) {
+       if (test_check_exists(cache, SZ_128M + 768 * SZ_1K,
+                             SZ_128M - 768 * SZ_1K)) {
                test_msg("Bitmap region not removed from space cache\n");
                return -EINVAL;
        }
@@ -535,8 +494,7 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
         * covered by the bitmap, isn't marked as free.
         */
-       if (test_check_exists(cache, 128 * 1024 * 1024 + 256 * 1024,
-                             256 * 1024)) {
+       if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) {
                test_msg("Invalid bitmap region marked as free\n");
                return -EINVAL;
        }
@@ -545,8 +503,7 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
         * by the bitmap too, isn't marked as free either.
         */
-       if (test_check_exists(cache, 128 * 1024 * 1024,
-                             256 * 1024)) {
+       if (test_check_exists(cache, SZ_128M, SZ_256K)) {
                test_msg("Invalid bitmap region marked as free\n");
                return -EINVAL;
        }
@@ -556,13 +513,13 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * lets make sure the free space cache marks it as free in the bitmap,
         * and doesn't insert a new extent entry to represent this region.
         */
-       ret = btrfs_add_free_space(cache, 128 * 1024 * 1024, 512 * 1024);
+       ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K);
        if (ret) {
                test_msg("Error adding free space: %d\n", ret);
                return ret;
        }
        /* Confirm the region is marked as free. */
-       if (!test_check_exists(cache, 128 * 1024 * 1024, 512 * 1024)) {
+       if (!test_check_exists(cache, SZ_128M, SZ_512K)) {
                test_msg("Bitmap region not marked as free\n");
                return -ENOENT;
        }
@@ -581,8 +538,7 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * The goal is to test that the bitmap entry space stealing doesn't
         * steal this space region.
         */
-       ret = btrfs_add_free_space(cache, 128 * 1024 * 1024 + 16 * 1024 * 1024,
-                                  4096);
+       ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, 4096);
        if (ret) {
                test_msg("Error adding free space: %d\n", ret);
                return ret;
@@ -601,15 +557,13 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * expand the range covered by the existing extent entry that represents
         * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
         */
-       ret = btrfs_add_free_space(cache, 128 * 1024 * 1024 - 128 * 1024,
-                                  128 * 1024);
+       ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K);
        if (ret) {
                test_msg("Error adding free space: %d\n", ret);
                return ret;
        }
        /* Confirm the region is marked as free. */
-       if (!test_check_exists(cache, 128 * 1024 * 1024 - 128 * 1024,
-                              128 * 1024)) {
+       if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) {
                test_msg("Extent region not marked as free\n");
                return -ENOENT;
        }
@@ -637,21 +591,20 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * that represents the 1Mb free space, and therefore we're able to
         * allocate the whole free space at once.
         */
-       if (!test_check_exists(cache, 128 * 1024 * 1024 - 256 * 1024,
-                              1 * 1024 * 1024)) {
+       if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) {
                test_msg("Expected region not marked as free\n");
                return -ENOENT;
        }
 
-       if (cache->free_space_ctl->free_space != (1 * 1024 * 1024 + 4096)) {
+       if (cache->free_space_ctl->free_space != (SZ_1M + 4096)) {
                test_msg("Cache free space is not 1Mb + 4Kb\n");
                return -EINVAL;
        }
 
        offset = btrfs_find_space_for_alloc(cache,
-                                           0, 1 * 1024 * 1024, 0,
+                                           0, SZ_1M, 0,
                                            &max_extent_size);
-       if (offset != (128 * 1024 * 1024 - 256 * 1024)) {
+       if (offset != (SZ_128M - SZ_256K)) {
                test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
                         offset);
                return -EINVAL;
@@ -670,7 +623,7 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
        offset = btrfs_find_space_for_alloc(cache,
                                            0, 4096, 0,
                                            &max_extent_size);
-       if (offset != (128 * 1024 * 1024 + 16 * 1024 * 1024)) {
+       if (offset != (SZ_128M + SZ_16M)) {
                test_msg("Failed to allocate 4Kb from space cache, returned offset is: %llu\n",
                         offset);
                return -EINVAL;
@@ -691,16 +644,14 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
        /*
         * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
         */
-       ret = test_add_free_space_entry(cache, 128 * 1024 * 1024 + 128 * 1024,
-                                       128 * 1024, 0);
+       ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0);
        if (ret) {
                test_msg("Couldn't add extent entry %d\n", ret);
                return ret;
        }
 
        /* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
-       ret = test_add_free_space_entry(cache, 0,
-                                       128 * 1024 * 1024 - 512 * 1024, 1);
+       ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1);
        if (ret) {
                test_msg("Couldn't add bitmap entry %d\n", ret);
                return ret;
@@ -717,22 +668,18 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * [128Mb + 128b, 128Mb + 256Kb[
         * [128Mb - 768Kb, 128Mb - 512Kb[
         */
-       ret = btrfs_remove_free_space(cache,
-                                     0,
-                                     128 * 1024 * 1024 - 768 * 1024);
+       ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K);
        if (ret) {
                test_msg("Failed to free part of bitmap space %d\n", ret);
                return ret;
        }
 
        /* Confirm that only those 2 ranges are marked as free. */
-       if (!test_check_exists(cache, 128 * 1024 * 1024 + 128 * 1024,
-                              128 * 1024)) {
+       if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) {
                test_msg("Free space range missing\n");
                return -ENOENT;
        }
-       if (!test_check_exists(cache, 128 * 1024 * 1024 - 768 * 1024,
-                              256 * 1024)) {
+       if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) {
                test_msg("Free space range missing\n");
                return -ENOENT;
        }
@@ -741,8 +688,7 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
         * as free anymore.
         */
-       if (test_check_exists(cache, 0,
-                             128 * 1024 * 1024 - 768 * 1024)) {
+       if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) {
                test_msg("Bitmap region not removed from space cache\n");
                return -EINVAL;
        }
@@ -751,8 +697,7 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
         * covered by the bitmap, isn't marked as free.
         */
-       if (test_check_exists(cache, 128 * 1024 * 1024 - 512 * 1024,
-                             512 * 1024)) {
+       if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
                test_msg("Invalid bitmap region marked as free\n");
                return -EINVAL;
        }
@@ -762,15 +707,13 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * lets make sure the free space cache marks it as free in the bitmap,
         * and doesn't insert a new extent entry to represent this region.
         */
-       ret = btrfs_add_free_space(cache, 128 * 1024 * 1024 - 512 * 1024,
-                                  512 * 1024);
+       ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K);
        if (ret) {
                test_msg("Error adding free space: %d\n", ret);
                return ret;
        }
        /* Confirm the region is marked as free. */
-       if (!test_check_exists(cache, 128 * 1024 * 1024 - 512 * 1024,
-                              512 * 1024)) {
+       if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
                test_msg("Bitmap region not marked as free\n");
                return -ENOENT;
        }
@@ -789,7 +732,7 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * The goal is to test that the bitmap entry space stealing doesn't
         * steal this space region.
         */
-       ret = btrfs_add_free_space(cache, 32 * 1024 * 1024, 8192);
+       ret = btrfs_add_free_space(cache, SZ_32M, 8192);
        if (ret) {
                test_msg("Error adding free space: %d\n", ret);
                return ret;
@@ -800,13 +743,13 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * expand the range covered by the existing extent entry that represents
         * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
         */
-       ret = btrfs_add_free_space(cache, 128 * 1024 * 1024, 128 * 1024);
+       ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K);
        if (ret) {
                test_msg("Error adding free space: %d\n", ret);
                return ret;
        }
        /* Confirm the region is marked as free. */
-       if (!test_check_exists(cache, 128 * 1024 * 1024, 128 * 1024)) {
+       if (!test_check_exists(cache, SZ_128M, SZ_128K)) {
                test_msg("Extent region not marked as free\n");
                return -ENOENT;
        }
@@ -834,21 +777,19 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
         * that represents the 1Mb free space, and therefore we're able to
         * allocate the whole free space at once.
         */
-       if (!test_check_exists(cache, 128 * 1024 * 1024 - 768 * 1024,
-                              1 * 1024 * 1024)) {
+       if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) {
                test_msg("Expected region not marked as free\n");
                return -ENOENT;
        }
 
-       if (cache->free_space_ctl->free_space != (1 * 1024 * 1024 + 8192)) {
+       if (cache->free_space_ctl->free_space != (SZ_1M + 8192)) {
                test_msg("Cache free space is not 1Mb + 8Kb\n");
                return -EINVAL;
        }
 
-       offset = btrfs_find_space_for_alloc(cache,
-                                           0, 1 * 1024 * 1024, 0,
+       offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0,
                                            &max_extent_size);
-       if (offset != (128 * 1024 * 1024 - 768 * 1024)) {
+       if (offset != (SZ_128M - 768 * SZ_1K)) {
                test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
                         offset);
                return -EINVAL;
@@ -867,7 +808,7 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
        offset = btrfs_find_space_for_alloc(cache,
                                            0, 8192, 0,
                                            &max_extent_size);
-       if (offset != (32 * 1024 * 1024)) {
+       if (offset != SZ_32M) {
                test_msg("Failed to allocate 8Kb from space cache, returned offset is: %llu\n",
                         offset);
                return -EINVAL;
@@ -877,7 +818,7 @@ test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
        if (ret)
                return ret;
 
-       cache->free_space_ctl->op->use_bitmap = use_bitmap_op;
+       cache->free_space_ctl->op = orig_free_space_ops;
        __btrfs_remove_free_space_cache(cache->free_space_ctl);
 
        return 0;
@@ -891,7 +832,7 @@ int btrfs_test_free_space_cache(void)
 
        test_msg("Running btrfs free space cache tests\n");
 
-       cache = init_test_block_group();
+       cache = btrfs_alloc_dummy_block_group(1024 * 1024 * 1024);
        if (!cache) {
                test_msg("Couldn't run the tests\n");
                return 0;
@@ -922,9 +863,7 @@ int btrfs_test_free_space_cache(void)
 
        ret = test_steal_space_from_bitmap_to_extent(cache);
 out:
-       __btrfs_remove_free_space_cache(cache->free_space_ctl);
-       kfree(cache->free_space_ctl);
-       kfree(cache);
+       btrfs_free_dummy_block_group(cache);
        btrfs_free_dummy_root(root);
        test_msg("Free space cache tests finished\n");
        return ret;