1dc11dd5dSJosef Bacik /* 2dc11dd5dSJosef Bacik * Copyright (C) 2013 Fusion IO. All rights reserved. 3dc11dd5dSJosef Bacik * 4dc11dd5dSJosef Bacik * This program is free software; you can redistribute it and/or 5dc11dd5dSJosef Bacik * modify it under the terms of the GNU General Public 6dc11dd5dSJosef Bacik * License v2 as published by the Free Software Foundation. 7dc11dd5dSJosef Bacik * 8dc11dd5dSJosef Bacik * This program is distributed in the hope that it will be useful, 9dc11dd5dSJosef Bacik * but WITHOUT ANY WARRANTY; without even the implied warranty of 10dc11dd5dSJosef Bacik * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11dc11dd5dSJosef Bacik * General Public License for more details. 12dc11dd5dSJosef Bacik * 13dc11dd5dSJosef Bacik * You should have received a copy of the GNU General Public 14dc11dd5dSJosef Bacik * License along with this program; if not, write to the 15dc11dd5dSJosef Bacik * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16dc11dd5dSJosef Bacik * Boston, MA 021110-1307, USA. 17dc11dd5dSJosef Bacik */ 18dc11dd5dSJosef Bacik 19dc11dd5dSJosef Bacik #include <linux/slab.h> 20dc11dd5dSJosef Bacik #include "btrfs-tests.h" 21dc11dd5dSJosef Bacik #include "../ctree.h" 22d0bd4560SJosef Bacik #include "../disk-io.h" 23dc11dd5dSJosef Bacik #include "../free-space-cache.h" 24dc11dd5dSJosef Bacik 25dc11dd5dSJosef Bacik #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8) 26dc11dd5dSJosef Bacik static struct btrfs_block_group_cache *init_test_block_group(void) 27dc11dd5dSJosef Bacik { 28dc11dd5dSJosef Bacik struct btrfs_block_group_cache *cache; 29dc11dd5dSJosef Bacik 30dc11dd5dSJosef Bacik cache = kzalloc(sizeof(*cache), GFP_NOFS); 31dc11dd5dSJosef Bacik if (!cache) 32dc11dd5dSJosef Bacik return NULL; 33dc11dd5dSJosef Bacik cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), 34dc11dd5dSJosef Bacik GFP_NOFS); 35dc11dd5dSJosef Bacik if (!cache->free_space_ctl) { 36dc11dd5dSJosef Bacik kfree(cache); 37dc11dd5dSJosef Bacik return NULL; 38dc11dd5dSJosef Bacik } 39d0bd4560SJosef Bacik cache->fs_info = btrfs_alloc_dummy_fs_info(); 40d0bd4560SJosef Bacik if (!cache->fs_info) { 41d0bd4560SJosef Bacik kfree(cache->free_space_ctl); 42d0bd4560SJosef Bacik kfree(cache); 43d0bd4560SJosef Bacik return NULL; 44d0bd4560SJosef Bacik } 45dc11dd5dSJosef Bacik 46dc11dd5dSJosef Bacik cache->key.objectid = 0; 47*ee22184bSByongho Lee cache->key.offset = SZ_1G; 48dc11dd5dSJosef Bacik cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 49dc11dd5dSJosef Bacik cache->sectorsize = 4096; 5020005523SFilipe Manana cache->full_stripe_len = 4096; 51dc11dd5dSJosef Bacik 52dc11dd5dSJosef Bacik spin_lock_init(&cache->lock); 53dc11dd5dSJosef Bacik INIT_LIST_HEAD(&cache->list); 54dc11dd5dSJosef Bacik INIT_LIST_HEAD(&cache->cluster_list); 5547ab2a6cSJosef Bacik INIT_LIST_HEAD(&cache->bg_list); 56dc11dd5dSJosef Bacik 57dc11dd5dSJosef Bacik btrfs_init_free_space_ctl(cache); 58dc11dd5dSJosef Bacik 59dc11dd5dSJosef Bacik return cache; 60dc11dd5dSJosef Bacik } 61dc11dd5dSJosef Bacik 62dc11dd5dSJosef Bacik /* 63dc11dd5dSJosef Bacik * This test just does basic sanity checking, making sure we can add an exten 64dc11dd5dSJosef Bacik * entry and remove space from either end and the middle, and make sure we can 65dc11dd5dSJosef Bacik * remove space that covers adjacent extent entries. 66dc11dd5dSJosef Bacik */ 67dc11dd5dSJosef Bacik static int test_extents(struct btrfs_block_group_cache *cache) 68dc11dd5dSJosef Bacik { 69dc11dd5dSJosef Bacik int ret = 0; 70dc11dd5dSJosef Bacik 71dc11dd5dSJosef Bacik test_msg("Running extent only tests\n"); 72dc11dd5dSJosef Bacik 73dc11dd5dSJosef Bacik /* First just make sure we can remove an entire entry */ 74*ee22184bSByongho Lee ret = btrfs_add_free_space(cache, 0, SZ_4M); 75dc11dd5dSJosef Bacik if (ret) { 76dc11dd5dSJosef Bacik test_msg("Error adding initial extents %d\n", ret); 77dc11dd5dSJosef Bacik return ret; 78dc11dd5dSJosef Bacik } 79dc11dd5dSJosef Bacik 80*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, 0, SZ_4M); 81dc11dd5dSJosef Bacik if (ret) { 82dc11dd5dSJosef Bacik test_msg("Error removing extent %d\n", ret); 83dc11dd5dSJosef Bacik return ret; 84dc11dd5dSJosef Bacik } 85dc11dd5dSJosef Bacik 86*ee22184bSByongho Lee if (test_check_exists(cache, 0, SZ_4M)) { 87dc11dd5dSJosef Bacik test_msg("Full remove left some lingering space\n"); 88dc11dd5dSJosef Bacik return -1; 89dc11dd5dSJosef Bacik } 90dc11dd5dSJosef Bacik 91dc11dd5dSJosef Bacik /* Ok edge and middle cases now */ 92*ee22184bSByongho Lee ret = btrfs_add_free_space(cache, 0, SZ_4M); 93dc11dd5dSJosef Bacik if (ret) { 94dc11dd5dSJosef Bacik test_msg("Error adding half extent %d\n", ret); 95dc11dd5dSJosef Bacik return ret; 96dc11dd5dSJosef Bacik } 97dc11dd5dSJosef Bacik 98*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_1M); 99dc11dd5dSJosef Bacik if (ret) { 100dc11dd5dSJosef Bacik test_msg("Error removing tail end %d\n", ret); 101dc11dd5dSJosef Bacik return ret; 102dc11dd5dSJosef Bacik } 103dc11dd5dSJosef Bacik 104*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, 0, SZ_1M); 105dc11dd5dSJosef Bacik if (ret) { 106dc11dd5dSJosef Bacik test_msg("Error removing front end %d\n", ret); 107dc11dd5dSJosef Bacik return ret; 108dc11dd5dSJosef Bacik } 109dc11dd5dSJosef Bacik 110*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, SZ_2M, 4096); 111dc11dd5dSJosef Bacik if (ret) { 11277d84ff8SMasanari Iida test_msg("Error removing middle piece %d\n", ret); 113dc11dd5dSJosef Bacik return ret; 114dc11dd5dSJosef Bacik } 115dc11dd5dSJosef Bacik 116*ee22184bSByongho Lee if (test_check_exists(cache, 0, SZ_1M)) { 117dc11dd5dSJosef Bacik test_msg("Still have space at the front\n"); 118dc11dd5dSJosef Bacik return -1; 119dc11dd5dSJosef Bacik } 120dc11dd5dSJosef Bacik 121*ee22184bSByongho Lee if (test_check_exists(cache, SZ_2M, 4096)) { 122dc11dd5dSJosef Bacik test_msg("Still have space in the middle\n"); 123dc11dd5dSJosef Bacik return -1; 124dc11dd5dSJosef Bacik } 125dc11dd5dSJosef Bacik 126*ee22184bSByongho Lee if (test_check_exists(cache, 3 * SZ_1M, SZ_1M)) { 127dc11dd5dSJosef Bacik test_msg("Still have space at the end\n"); 128dc11dd5dSJosef Bacik return -1; 129dc11dd5dSJosef Bacik } 130dc11dd5dSJosef Bacik 131dc11dd5dSJosef Bacik /* Cleanup */ 132dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 133dc11dd5dSJosef Bacik 134dc11dd5dSJosef Bacik return 0; 135dc11dd5dSJosef Bacik } 136dc11dd5dSJosef Bacik 137dc11dd5dSJosef Bacik static int test_bitmaps(struct btrfs_block_group_cache *cache) 138dc11dd5dSJosef Bacik { 139dc11dd5dSJosef Bacik u64 next_bitmap_offset; 140dc11dd5dSJosef Bacik int ret; 141dc11dd5dSJosef Bacik 142dc11dd5dSJosef Bacik test_msg("Running bitmap only tests\n"); 143dc11dd5dSJosef Bacik 144*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, 0, SZ_4M, 1); 145dc11dd5dSJosef Bacik if (ret) { 146dc11dd5dSJosef Bacik test_msg("Couldn't create a bitmap entry %d\n", ret); 147dc11dd5dSJosef Bacik return ret; 148dc11dd5dSJosef Bacik } 149dc11dd5dSJosef Bacik 150*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, 0, SZ_4M); 151dc11dd5dSJosef Bacik if (ret) { 152dc11dd5dSJosef Bacik test_msg("Error removing bitmap full range %d\n", ret); 153dc11dd5dSJosef Bacik return ret; 154dc11dd5dSJosef Bacik } 155dc11dd5dSJosef Bacik 156*ee22184bSByongho Lee if (test_check_exists(cache, 0, SZ_4M)) { 157dc11dd5dSJosef Bacik test_msg("Left some space in bitmap\n"); 158dc11dd5dSJosef Bacik return -1; 159dc11dd5dSJosef Bacik } 160dc11dd5dSJosef Bacik 161*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, 0, SZ_4M, 1); 162dc11dd5dSJosef Bacik if (ret) { 163dc11dd5dSJosef Bacik test_msg("Couldn't add to our bitmap entry %d\n", ret); 164dc11dd5dSJosef Bacik return ret; 165dc11dd5dSJosef Bacik } 166dc11dd5dSJosef Bacik 167*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, SZ_1M, SZ_2M); 168dc11dd5dSJosef Bacik if (ret) { 169dc11dd5dSJosef Bacik test_msg("Couldn't remove middle chunk %d\n", ret); 170dc11dd5dSJosef Bacik return ret; 171dc11dd5dSJosef Bacik } 172dc11dd5dSJosef Bacik 173dc11dd5dSJosef Bacik /* 174dc11dd5dSJosef Bacik * The first bitmap we have starts at offset 0 so the next one is just 175dc11dd5dSJosef Bacik * at the end of the first bitmap. 176dc11dd5dSJosef Bacik */ 177dc11dd5dSJosef Bacik next_bitmap_offset = (u64)(BITS_PER_BITMAP * 4096); 178dc11dd5dSJosef Bacik 179dc11dd5dSJosef Bacik /* Test a bit straddling two bitmaps */ 180*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, next_bitmap_offset - SZ_2M, 181*ee22184bSByongho Lee SZ_4M, 1); 182dc11dd5dSJosef Bacik if (ret) { 183dc11dd5dSJosef Bacik test_msg("Couldn't add space that straddles two bitmaps %d\n", 184dc11dd5dSJosef Bacik ret); 185dc11dd5dSJosef Bacik return ret; 186dc11dd5dSJosef Bacik } 187dc11dd5dSJosef Bacik 188*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, next_bitmap_offset - SZ_1M, SZ_2M); 189dc11dd5dSJosef Bacik if (ret) { 190dc11dd5dSJosef Bacik test_msg("Couldn't remove overlapping space %d\n", ret); 191dc11dd5dSJosef Bacik return ret; 192dc11dd5dSJosef Bacik } 193dc11dd5dSJosef Bacik 194*ee22184bSByongho Lee if (test_check_exists(cache, next_bitmap_offset - SZ_1M, SZ_2M)) { 195dc11dd5dSJosef Bacik test_msg("Left some space when removing overlapping\n"); 196dc11dd5dSJosef Bacik return -1; 197dc11dd5dSJosef Bacik } 198dc11dd5dSJosef Bacik 199dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 200dc11dd5dSJosef Bacik 201dc11dd5dSJosef Bacik return 0; 202dc11dd5dSJosef Bacik } 203dc11dd5dSJosef Bacik 204dc11dd5dSJosef Bacik /* This is the high grade jackassery */ 205dc11dd5dSJosef Bacik static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache) 206dc11dd5dSJosef Bacik { 207dc11dd5dSJosef Bacik u64 bitmap_offset = (u64)(BITS_PER_BITMAP * 4096); 208dc11dd5dSJosef Bacik int ret; 209dc11dd5dSJosef Bacik 210dc11dd5dSJosef Bacik test_msg("Running bitmap and extent tests\n"); 211dc11dd5dSJosef Bacik 212dc11dd5dSJosef Bacik /* 213dc11dd5dSJosef Bacik * First let's do something simple, an extent at the same offset as the 214dc11dd5dSJosef Bacik * bitmap, but the free space completely in the extent and then 215dc11dd5dSJosef Bacik * completely in the bitmap. 216dc11dd5dSJosef Bacik */ 217*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, SZ_4M, SZ_1M, 1); 218dc11dd5dSJosef Bacik if (ret) { 219dc11dd5dSJosef Bacik test_msg("Couldn't create bitmap entry %d\n", ret); 220dc11dd5dSJosef Bacik return ret; 221dc11dd5dSJosef Bacik } 222dc11dd5dSJosef Bacik 223*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, 0, SZ_1M, 0); 224dc11dd5dSJosef Bacik if (ret) { 225dc11dd5dSJosef Bacik test_msg("Couldn't add extent entry %d\n", ret); 226dc11dd5dSJosef Bacik return ret; 227dc11dd5dSJosef Bacik } 228dc11dd5dSJosef Bacik 229*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, 0, SZ_1M); 230dc11dd5dSJosef Bacik if (ret) { 231dc11dd5dSJosef Bacik test_msg("Couldn't remove extent entry %d\n", ret); 232dc11dd5dSJosef Bacik return ret; 233dc11dd5dSJosef Bacik } 234dc11dd5dSJosef Bacik 235*ee22184bSByongho Lee if (test_check_exists(cache, 0, SZ_1M)) { 236dc11dd5dSJosef Bacik test_msg("Left remnants after our remove\n"); 237dc11dd5dSJosef Bacik return -1; 238dc11dd5dSJosef Bacik } 239dc11dd5dSJosef Bacik 240dc11dd5dSJosef Bacik /* Now to add back the extent entry and remove from the bitmap */ 241*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, 0, SZ_1M, 0); 242dc11dd5dSJosef Bacik if (ret) { 243dc11dd5dSJosef Bacik test_msg("Couldn't re-add extent entry %d\n", ret); 244dc11dd5dSJosef Bacik return ret; 245dc11dd5dSJosef Bacik } 246dc11dd5dSJosef Bacik 247*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, SZ_4M, SZ_1M); 248dc11dd5dSJosef Bacik if (ret) { 249dc11dd5dSJosef Bacik test_msg("Couldn't remove from bitmap %d\n", ret); 250dc11dd5dSJosef Bacik return ret; 251dc11dd5dSJosef Bacik } 252dc11dd5dSJosef Bacik 253*ee22184bSByongho Lee if (test_check_exists(cache, SZ_4M, SZ_1M)) { 254dc11dd5dSJosef Bacik test_msg("Left remnants in the bitmap\n"); 255dc11dd5dSJosef Bacik return -1; 256dc11dd5dSJosef Bacik } 257dc11dd5dSJosef Bacik 258dc11dd5dSJosef Bacik /* 259dc11dd5dSJosef Bacik * Ok so a little more evil, extent entry and bitmap at the same offset, 260dc11dd5dSJosef Bacik * removing an overlapping chunk. 261dc11dd5dSJosef Bacik */ 262*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, SZ_1M, SZ_4M, 1); 263dc11dd5dSJosef Bacik if (ret) { 264dc11dd5dSJosef Bacik test_msg("Couldn't add to a bitmap %d\n", ret); 265dc11dd5dSJosef Bacik return ret; 266dc11dd5dSJosef Bacik } 267dc11dd5dSJosef Bacik 268*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, SZ_512K, 3 * SZ_1M); 269dc11dd5dSJosef Bacik if (ret) { 270dc11dd5dSJosef Bacik test_msg("Couldn't remove overlapping space %d\n", ret); 271dc11dd5dSJosef Bacik return ret; 272dc11dd5dSJosef Bacik } 273dc11dd5dSJosef Bacik 274*ee22184bSByongho Lee if (test_check_exists(cache, SZ_512K, 3 * SZ_1M)) { 2758faaaeadSMasanari Iida test_msg("Left over pieces after removing overlapping\n"); 276dc11dd5dSJosef Bacik return -1; 277dc11dd5dSJosef Bacik } 278dc11dd5dSJosef Bacik 279dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 280dc11dd5dSJosef Bacik 281dc11dd5dSJosef Bacik /* Now with the extent entry offset into the bitmap */ 282*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, SZ_4M, SZ_4M, 1); 283dc11dd5dSJosef Bacik if (ret) { 284dc11dd5dSJosef Bacik test_msg("Couldn't add space to the bitmap %d\n", ret); 285dc11dd5dSJosef Bacik return ret; 286dc11dd5dSJosef Bacik } 287dc11dd5dSJosef Bacik 288*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, SZ_2M, SZ_2M, 0); 289dc11dd5dSJosef Bacik if (ret) { 290dc11dd5dSJosef Bacik test_msg("Couldn't add extent to the cache %d\n", ret); 291dc11dd5dSJosef Bacik return ret; 292dc11dd5dSJosef Bacik } 293dc11dd5dSJosef Bacik 294*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_4M); 295dc11dd5dSJosef Bacik if (ret) { 296dc11dd5dSJosef Bacik test_msg("Problem removing overlapping space %d\n", ret); 297dc11dd5dSJosef Bacik return ret; 298dc11dd5dSJosef Bacik } 299dc11dd5dSJosef Bacik 300*ee22184bSByongho Lee if (test_check_exists(cache, 3 * SZ_1M, SZ_4M)) { 301dc11dd5dSJosef Bacik test_msg("Left something behind when removing space"); 302dc11dd5dSJosef Bacik return -1; 303dc11dd5dSJosef Bacik } 304dc11dd5dSJosef Bacik 305dc11dd5dSJosef Bacik /* 306dc11dd5dSJosef Bacik * This has blown up in the past, the extent entry starts before the 307dc11dd5dSJosef Bacik * bitmap entry, but we're trying to remove an offset that falls 308dc11dd5dSJosef Bacik * completely within the bitmap range and is in both the extent entry 309dc11dd5dSJosef Bacik * and the bitmap entry, looks like this 310dc11dd5dSJosef Bacik * 311dc11dd5dSJosef Bacik * [ extent ] 312dc11dd5dSJosef Bacik * [ bitmap ] 313dc11dd5dSJosef Bacik * [ del ] 314dc11dd5dSJosef Bacik */ 315dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 316*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, bitmap_offset + SZ_4M, SZ_4M, 1); 317dc11dd5dSJosef Bacik if (ret) { 318dc11dd5dSJosef Bacik test_msg("Couldn't add bitmap %d\n", ret); 319dc11dd5dSJosef Bacik return ret; 320dc11dd5dSJosef Bacik } 321dc11dd5dSJosef Bacik 322*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, bitmap_offset - SZ_1M, 323*ee22184bSByongho Lee 5 * SZ_1M, 0); 324dc11dd5dSJosef Bacik if (ret) { 325dc11dd5dSJosef Bacik test_msg("Couldn't add extent entry %d\n", ret); 326dc11dd5dSJosef Bacik return ret; 327dc11dd5dSJosef Bacik } 328dc11dd5dSJosef Bacik 329*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, bitmap_offset + SZ_1M, 5 * SZ_1M); 330dc11dd5dSJosef Bacik if (ret) { 331dc11dd5dSJosef Bacik test_msg("Failed to free our space %d\n", ret); 332dc11dd5dSJosef Bacik return ret; 333dc11dd5dSJosef Bacik } 334dc11dd5dSJosef Bacik 335*ee22184bSByongho Lee if (test_check_exists(cache, bitmap_offset + SZ_1M, 5 * SZ_1M)) { 336dc11dd5dSJosef Bacik test_msg("Left stuff over\n"); 337dc11dd5dSJosef Bacik return -1; 338dc11dd5dSJosef Bacik } 339dc11dd5dSJosef Bacik 340dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 341dc11dd5dSJosef Bacik 342dc11dd5dSJosef Bacik /* 343dc11dd5dSJosef Bacik * This blew up before, we have part of the free space in a bitmap and 344dc11dd5dSJosef Bacik * then the entirety of the rest of the space in an extent. This used 345dc11dd5dSJosef Bacik * to return -EAGAIN back from btrfs_remove_extent, make sure this 346dc11dd5dSJosef Bacik * doesn't happen. 347dc11dd5dSJosef Bacik */ 348*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, SZ_1M, SZ_2M, 1); 349dc11dd5dSJosef Bacik if (ret) { 350dc11dd5dSJosef Bacik test_msg("Couldn't add bitmap entry %d\n", ret); 351dc11dd5dSJosef Bacik return ret; 352dc11dd5dSJosef Bacik } 353dc11dd5dSJosef Bacik 354*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, 3 * SZ_1M, SZ_1M, 0); 355dc11dd5dSJosef Bacik if (ret) { 356dc11dd5dSJosef Bacik test_msg("Couldn't add extent entry %d\n", ret); 357dc11dd5dSJosef Bacik return ret; 358dc11dd5dSJosef Bacik } 359dc11dd5dSJosef Bacik 360*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, SZ_1M, 3 * SZ_1M); 361dc11dd5dSJosef Bacik if (ret) { 362dc11dd5dSJosef Bacik test_msg("Error removing bitmap and extent overlapping %d\n", ret); 363dc11dd5dSJosef Bacik return ret; 364dc11dd5dSJosef Bacik } 365dc11dd5dSJosef Bacik 366dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 367dc11dd5dSJosef Bacik return 0; 368dc11dd5dSJosef Bacik } 369dc11dd5dSJosef Bacik 37020005523SFilipe Manana /* Used by test_steal_space_from_bitmap_to_extent(). */ 37120005523SFilipe Manana static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl, 37220005523SFilipe Manana struct btrfs_free_space *info) 37320005523SFilipe Manana { 37420005523SFilipe Manana return ctl->free_extents > 0; 37520005523SFilipe Manana } 37620005523SFilipe Manana 37720005523SFilipe Manana /* Used by test_steal_space_from_bitmap_to_extent(). */ 37820005523SFilipe Manana static int 37920005523SFilipe Manana check_num_extents_and_bitmaps(const struct btrfs_block_group_cache *cache, 38020005523SFilipe Manana const int num_extents, 38120005523SFilipe Manana const int num_bitmaps) 38220005523SFilipe Manana { 38320005523SFilipe Manana if (cache->free_space_ctl->free_extents != num_extents) { 38420005523SFilipe Manana test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n", 38520005523SFilipe Manana cache->free_space_ctl->free_extents, num_extents); 38620005523SFilipe Manana return -EINVAL; 38720005523SFilipe Manana } 38820005523SFilipe Manana if (cache->free_space_ctl->total_bitmaps != num_bitmaps) { 38920005523SFilipe Manana test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n", 39020005523SFilipe Manana cache->free_space_ctl->total_bitmaps, num_bitmaps); 39120005523SFilipe Manana return -EINVAL; 39220005523SFilipe Manana } 39320005523SFilipe Manana return 0; 39420005523SFilipe Manana } 39520005523SFilipe Manana 39620005523SFilipe Manana /* Used by test_steal_space_from_bitmap_to_extent(). */ 39720005523SFilipe Manana static int check_cache_empty(struct btrfs_block_group_cache *cache) 39820005523SFilipe Manana { 39920005523SFilipe Manana u64 offset; 40020005523SFilipe Manana u64 max_extent_size; 40120005523SFilipe Manana 40220005523SFilipe Manana /* 40320005523SFilipe Manana * Now lets confirm that there's absolutely no free space left to 40420005523SFilipe Manana * allocate. 40520005523SFilipe Manana */ 40620005523SFilipe Manana if (cache->free_space_ctl->free_space != 0) { 40720005523SFilipe Manana test_msg("Cache free space is not 0\n"); 40820005523SFilipe Manana return -EINVAL; 40920005523SFilipe Manana } 41020005523SFilipe Manana 41120005523SFilipe Manana /* And any allocation request, no matter how small, should fail now. */ 41220005523SFilipe Manana offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0, 41320005523SFilipe Manana &max_extent_size); 41420005523SFilipe Manana if (offset != 0) { 41520005523SFilipe Manana test_msg("Space allocation did not fail, returned offset: %llu", 41620005523SFilipe Manana offset); 41720005523SFilipe Manana return -EINVAL; 41820005523SFilipe Manana } 41920005523SFilipe Manana 42020005523SFilipe Manana /* And no extent nor bitmap entries in the cache anymore. */ 42120005523SFilipe Manana return check_num_extents_and_bitmaps(cache, 0, 0); 42220005523SFilipe Manana } 42320005523SFilipe Manana 42420005523SFilipe Manana /* 42520005523SFilipe Manana * Before we were able to steal free space from a bitmap entry to an extent 42620005523SFilipe Manana * entry, we could end up with 2 entries representing a contiguous free space. 42720005523SFilipe Manana * One would be an extent entry and the other a bitmap entry. Since in order 42820005523SFilipe Manana * to allocate space to a caller we use only 1 entry, we couldn't return that 42920005523SFilipe Manana * whole range to the caller if it was requested. This forced the caller to 43020005523SFilipe Manana * either assume ENOSPC or perform several smaller space allocations, which 43120005523SFilipe Manana * wasn't optimal as they could be spread all over the block group while under 43220005523SFilipe Manana * concurrency (extra overhead and fragmentation). 43320005523SFilipe Manana * 43420005523SFilipe Manana * This stealing approach is benefical, since we always prefer to allocate from 43520005523SFilipe Manana * extent entries, both for clustered and non-clustered allocation requests. 43620005523SFilipe Manana */ 43720005523SFilipe Manana static int 43820005523SFilipe Manana test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache) 43920005523SFilipe Manana { 44020005523SFilipe Manana int ret; 44120005523SFilipe Manana u64 offset; 44220005523SFilipe Manana u64 max_extent_size; 44320005523SFilipe Manana 44420005523SFilipe Manana bool (*use_bitmap_op)(struct btrfs_free_space_ctl *, 44520005523SFilipe Manana struct btrfs_free_space *); 44620005523SFilipe Manana 44720005523SFilipe Manana test_msg("Running space stealing from bitmap to extent\n"); 44820005523SFilipe Manana 44920005523SFilipe Manana /* 45020005523SFilipe Manana * For this test, we want to ensure we end up with an extent entry 45120005523SFilipe Manana * immediately adjacent to a bitmap entry, where the bitmap starts 45220005523SFilipe Manana * at an offset where the extent entry ends. We keep adding and 45320005523SFilipe Manana * removing free space to reach into this state, but to get there 45420005523SFilipe Manana * we need to reach a point where marking new free space doesn't 45520005523SFilipe Manana * result in adding new extent entries or merging the new space 45620005523SFilipe Manana * with existing extent entries - the space ends up being marked 45720005523SFilipe Manana * in an existing bitmap that covers the new free space range. 45820005523SFilipe Manana * 45920005523SFilipe Manana * To get there, we need to reach the threshold defined set at 46020005523SFilipe Manana * cache->free_space_ctl->extents_thresh, which currently is 46120005523SFilipe Manana * 256 extents on a x86_64 system at least, and a few other 46220005523SFilipe Manana * conditions (check free_space_cache.c). Instead of making the 46320005523SFilipe Manana * test much longer and complicated, use a "use_bitmap" operation 46420005523SFilipe Manana * that forces use of bitmaps as soon as we have at least 1 46520005523SFilipe Manana * extent entry. 46620005523SFilipe Manana */ 46720005523SFilipe Manana use_bitmap_op = cache->free_space_ctl->op->use_bitmap; 46820005523SFilipe Manana cache->free_space_ctl->op->use_bitmap = test_use_bitmap; 46920005523SFilipe Manana 47020005523SFilipe Manana /* 47120005523SFilipe Manana * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[ 47220005523SFilipe Manana */ 473*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0); 47420005523SFilipe Manana if (ret) { 47520005523SFilipe Manana test_msg("Couldn't add extent entry %d\n", ret); 47620005523SFilipe Manana return ret; 47720005523SFilipe Manana } 47820005523SFilipe Manana 47920005523SFilipe Manana /* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */ 480*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K, 481*ee22184bSByongho Lee SZ_128M - SZ_512K, 1); 48220005523SFilipe Manana if (ret) { 48320005523SFilipe Manana test_msg("Couldn't add bitmap entry %d\n", ret); 48420005523SFilipe Manana return ret; 48520005523SFilipe Manana } 48620005523SFilipe Manana 48720005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 48820005523SFilipe Manana if (ret) 48920005523SFilipe Manana return ret; 49020005523SFilipe Manana 49120005523SFilipe Manana /* 49220005523SFilipe Manana * Now make only the first 256Kb of the bitmap marked as free, so that 49320005523SFilipe Manana * we end up with only the following ranges marked as free space: 49420005523SFilipe Manana * 49520005523SFilipe Manana * [128Mb - 256Kb, 128Mb - 128Kb[ 49620005523SFilipe Manana * [128Mb + 512Kb, 128Mb + 768Kb[ 49720005523SFilipe Manana */ 49820005523SFilipe Manana ret = btrfs_remove_free_space(cache, 499*ee22184bSByongho Lee SZ_128M + 768 * SZ_1K, 500*ee22184bSByongho Lee SZ_128M - 768 * SZ_1K); 50120005523SFilipe Manana if (ret) { 50220005523SFilipe Manana test_msg("Failed to free part of bitmap space %d\n", ret); 50320005523SFilipe Manana return ret; 50420005523SFilipe Manana } 50520005523SFilipe Manana 50620005523SFilipe Manana /* Confirm that only those 2 ranges are marked as free. */ 507*ee22184bSByongho Lee if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) { 50820005523SFilipe Manana test_msg("Free space range missing\n"); 50920005523SFilipe Manana return -ENOENT; 51020005523SFilipe Manana } 511*ee22184bSByongho Lee if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) { 51220005523SFilipe Manana test_msg("Free space range missing\n"); 51320005523SFilipe Manana return -ENOENT; 51420005523SFilipe Manana } 51520005523SFilipe Manana 51620005523SFilipe Manana /* 51720005523SFilipe Manana * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked 51820005523SFilipe Manana * as free anymore. 51920005523SFilipe Manana */ 520*ee22184bSByongho Lee if (test_check_exists(cache, SZ_128M + 768 * SZ_1K, 521*ee22184bSByongho Lee SZ_128M - 768 * SZ_1K)) { 52220005523SFilipe Manana test_msg("Bitmap region not removed from space cache\n"); 52320005523SFilipe Manana return -EINVAL; 52420005523SFilipe Manana } 52520005523SFilipe Manana 52620005523SFilipe Manana /* 52720005523SFilipe Manana * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is 52820005523SFilipe Manana * covered by the bitmap, isn't marked as free. 52920005523SFilipe Manana */ 530*ee22184bSByongho Lee if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) { 53120005523SFilipe Manana test_msg("Invalid bitmap region marked as free\n"); 53220005523SFilipe Manana return -EINVAL; 53320005523SFilipe Manana } 53420005523SFilipe Manana 53520005523SFilipe Manana /* 53620005523SFilipe Manana * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered 53720005523SFilipe Manana * by the bitmap too, isn't marked as free either. 53820005523SFilipe Manana */ 539*ee22184bSByongho Lee if (test_check_exists(cache, SZ_128M, SZ_256K)) { 54020005523SFilipe Manana test_msg("Invalid bitmap region marked as free\n"); 54120005523SFilipe Manana return -EINVAL; 54220005523SFilipe Manana } 54320005523SFilipe Manana 54420005523SFilipe Manana /* 54520005523SFilipe Manana * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But, 54620005523SFilipe Manana * lets make sure the free space cache marks it as free in the bitmap, 54720005523SFilipe Manana * and doesn't insert a new extent entry to represent this region. 54820005523SFilipe Manana */ 549*ee22184bSByongho Lee ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K); 55020005523SFilipe Manana if (ret) { 55120005523SFilipe Manana test_msg("Error adding free space: %d\n", ret); 55220005523SFilipe Manana return ret; 55320005523SFilipe Manana } 55420005523SFilipe Manana /* Confirm the region is marked as free. */ 555*ee22184bSByongho Lee if (!test_check_exists(cache, SZ_128M, SZ_512K)) { 55620005523SFilipe Manana test_msg("Bitmap region not marked as free\n"); 55720005523SFilipe Manana return -ENOENT; 55820005523SFilipe Manana } 55920005523SFilipe Manana 56020005523SFilipe Manana /* 56120005523SFilipe Manana * Confirm that no new extent entries or bitmap entries were added to 56220005523SFilipe Manana * the cache after adding that free space region. 56320005523SFilipe Manana */ 56420005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 56520005523SFilipe Manana if (ret) 56620005523SFilipe Manana return ret; 56720005523SFilipe Manana 56820005523SFilipe Manana /* 56920005523SFilipe Manana * Now lets add a small free space region to the right of the previous 57020005523SFilipe Manana * one, which is not contiguous with it and is part of the bitmap too. 57120005523SFilipe Manana * The goal is to test that the bitmap entry space stealing doesn't 57220005523SFilipe Manana * steal this space region. 57320005523SFilipe Manana */ 574*ee22184bSByongho Lee ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, 4096); 57520005523SFilipe Manana if (ret) { 57620005523SFilipe Manana test_msg("Error adding free space: %d\n", ret); 57720005523SFilipe Manana return ret; 57820005523SFilipe Manana } 57920005523SFilipe Manana 58020005523SFilipe Manana /* 58120005523SFilipe Manana * Confirm that no new extent entries or bitmap entries were added to 58220005523SFilipe Manana * the cache after adding that free space region. 58320005523SFilipe Manana */ 58420005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 58520005523SFilipe Manana if (ret) 58620005523SFilipe Manana return ret; 58720005523SFilipe Manana 58820005523SFilipe Manana /* 58920005523SFilipe Manana * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will 59020005523SFilipe Manana * expand the range covered by the existing extent entry that represents 59120005523SFilipe Manana * the free space [128Mb - 256Kb, 128Mb - 128Kb[. 59220005523SFilipe Manana */ 593*ee22184bSByongho Lee ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K); 59420005523SFilipe Manana if (ret) { 59520005523SFilipe Manana test_msg("Error adding free space: %d\n", ret); 59620005523SFilipe Manana return ret; 59720005523SFilipe Manana } 59820005523SFilipe Manana /* Confirm the region is marked as free. */ 599*ee22184bSByongho Lee if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) { 60020005523SFilipe Manana test_msg("Extent region not marked as free\n"); 60120005523SFilipe Manana return -ENOENT; 60220005523SFilipe Manana } 60320005523SFilipe Manana 60420005523SFilipe Manana /* 60520005523SFilipe Manana * Confirm that our extent entry didn't stole all free space from the 60620005523SFilipe Manana * bitmap, because of the small 4Kb free space region. 60720005523SFilipe Manana */ 60820005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 60920005523SFilipe Manana if (ret) 61020005523SFilipe Manana return ret; 61120005523SFilipe Manana 61220005523SFilipe Manana /* 61320005523SFilipe Manana * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free 61420005523SFilipe Manana * space. Without stealing bitmap free space into extent entry space, 61520005523SFilipe Manana * we would have all this free space represented by 2 entries in the 61620005523SFilipe Manana * cache: 61720005523SFilipe Manana * 61820005523SFilipe Manana * extent entry covering range: [128Mb - 256Kb, 128Mb[ 61920005523SFilipe Manana * bitmap entry covering range: [128Mb, 128Mb + 768Kb[ 62020005523SFilipe Manana * 62120005523SFilipe Manana * Attempting to allocate the whole free space (1Mb) would fail, because 62220005523SFilipe Manana * we can't allocate from multiple entries. 62320005523SFilipe Manana * With the bitmap free space stealing, we get a single extent entry 62420005523SFilipe Manana * that represents the 1Mb free space, and therefore we're able to 62520005523SFilipe Manana * allocate the whole free space at once. 62620005523SFilipe Manana */ 627*ee22184bSByongho Lee if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) { 62820005523SFilipe Manana test_msg("Expected region not marked as free\n"); 62920005523SFilipe Manana return -ENOENT; 63020005523SFilipe Manana } 63120005523SFilipe Manana 632*ee22184bSByongho Lee if (cache->free_space_ctl->free_space != (SZ_1M + 4096)) { 63320005523SFilipe Manana test_msg("Cache free space is not 1Mb + 4Kb\n"); 63420005523SFilipe Manana return -EINVAL; 63520005523SFilipe Manana } 63620005523SFilipe Manana 63720005523SFilipe Manana offset = btrfs_find_space_for_alloc(cache, 638*ee22184bSByongho Lee 0, SZ_1M, 0, 63920005523SFilipe Manana &max_extent_size); 640*ee22184bSByongho Lee if (offset != (SZ_128M - SZ_256K)) { 64120005523SFilipe Manana test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n", 64220005523SFilipe Manana offset); 64320005523SFilipe Manana return -EINVAL; 64420005523SFilipe Manana } 64520005523SFilipe Manana 64620005523SFilipe Manana /* All that remains is a 4Kb free space region in a bitmap. Confirm. */ 64720005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 1, 1); 64820005523SFilipe Manana if (ret) 64920005523SFilipe Manana return ret; 65020005523SFilipe Manana 65120005523SFilipe Manana if (cache->free_space_ctl->free_space != 4096) { 65220005523SFilipe Manana test_msg("Cache free space is not 4Kb\n"); 65320005523SFilipe Manana return -EINVAL; 65420005523SFilipe Manana } 65520005523SFilipe Manana 65620005523SFilipe Manana offset = btrfs_find_space_for_alloc(cache, 65720005523SFilipe Manana 0, 4096, 0, 65820005523SFilipe Manana &max_extent_size); 659*ee22184bSByongho Lee if (offset != (SZ_128M + SZ_16M)) { 66020005523SFilipe Manana test_msg("Failed to allocate 4Kb from space cache, returned offset is: %llu\n", 66120005523SFilipe Manana offset); 66220005523SFilipe Manana return -EINVAL; 66320005523SFilipe Manana } 66420005523SFilipe Manana 66520005523SFilipe Manana ret = check_cache_empty(cache); 66620005523SFilipe Manana if (ret) 66720005523SFilipe Manana return ret; 66820005523SFilipe Manana 66920005523SFilipe Manana __btrfs_remove_free_space_cache(cache->free_space_ctl); 67020005523SFilipe Manana 67120005523SFilipe Manana /* 67220005523SFilipe Manana * Now test a similar scenario, but where our extent entry is located 67320005523SFilipe Manana * to the right of the bitmap entry, so that we can check that stealing 67420005523SFilipe Manana * space from a bitmap to the front of an extent entry works. 67520005523SFilipe Manana */ 67620005523SFilipe Manana 67720005523SFilipe Manana /* 67820005523SFilipe Manana * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[ 67920005523SFilipe Manana */ 680*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0); 68120005523SFilipe Manana if (ret) { 68220005523SFilipe Manana test_msg("Couldn't add extent entry %d\n", ret); 68320005523SFilipe Manana return ret; 68420005523SFilipe Manana } 68520005523SFilipe Manana 68620005523SFilipe Manana /* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */ 687*ee22184bSByongho Lee ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1); 68820005523SFilipe Manana if (ret) { 68920005523SFilipe Manana test_msg("Couldn't add bitmap entry %d\n", ret); 69020005523SFilipe Manana return ret; 69120005523SFilipe Manana } 69220005523SFilipe Manana 69320005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 69420005523SFilipe Manana if (ret) 69520005523SFilipe Manana return ret; 69620005523SFilipe Manana 69720005523SFilipe Manana /* 69820005523SFilipe Manana * Now make only the last 256Kb of the bitmap marked as free, so that 69920005523SFilipe Manana * we end up with only the following ranges marked as free space: 70020005523SFilipe Manana * 70120005523SFilipe Manana * [128Mb + 128b, 128Mb + 256Kb[ 70220005523SFilipe Manana * [128Mb - 768Kb, 128Mb - 512Kb[ 70320005523SFilipe Manana */ 704*ee22184bSByongho Lee ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K); 70520005523SFilipe Manana if (ret) { 70620005523SFilipe Manana test_msg("Failed to free part of bitmap space %d\n", ret); 70720005523SFilipe Manana return ret; 70820005523SFilipe Manana } 70920005523SFilipe Manana 71020005523SFilipe Manana /* Confirm that only those 2 ranges are marked as free. */ 711*ee22184bSByongho Lee if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) { 71220005523SFilipe Manana test_msg("Free space range missing\n"); 71320005523SFilipe Manana return -ENOENT; 71420005523SFilipe Manana } 715*ee22184bSByongho Lee if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) { 71620005523SFilipe Manana test_msg("Free space range missing\n"); 71720005523SFilipe Manana return -ENOENT; 71820005523SFilipe Manana } 71920005523SFilipe Manana 72020005523SFilipe Manana /* 72120005523SFilipe Manana * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked 72220005523SFilipe Manana * as free anymore. 72320005523SFilipe Manana */ 724*ee22184bSByongho Lee if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) { 72520005523SFilipe Manana test_msg("Bitmap region not removed from space cache\n"); 72620005523SFilipe Manana return -EINVAL; 72720005523SFilipe Manana } 72820005523SFilipe Manana 72920005523SFilipe Manana /* 73020005523SFilipe Manana * Confirm that the region [128Mb - 512Kb, 128Mb[, which is 73120005523SFilipe Manana * covered by the bitmap, isn't marked as free. 73220005523SFilipe Manana */ 733*ee22184bSByongho Lee if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) { 73420005523SFilipe Manana test_msg("Invalid bitmap region marked as free\n"); 73520005523SFilipe Manana return -EINVAL; 73620005523SFilipe Manana } 73720005523SFilipe Manana 73820005523SFilipe Manana /* 73920005523SFilipe Manana * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But, 74020005523SFilipe Manana * lets make sure the free space cache marks it as free in the bitmap, 74120005523SFilipe Manana * and doesn't insert a new extent entry to represent this region. 74220005523SFilipe Manana */ 743*ee22184bSByongho Lee ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K); 74420005523SFilipe Manana if (ret) { 74520005523SFilipe Manana test_msg("Error adding free space: %d\n", ret); 74620005523SFilipe Manana return ret; 74720005523SFilipe Manana } 74820005523SFilipe Manana /* Confirm the region is marked as free. */ 749*ee22184bSByongho Lee if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) { 75020005523SFilipe Manana test_msg("Bitmap region not marked as free\n"); 75120005523SFilipe Manana return -ENOENT; 75220005523SFilipe Manana } 75320005523SFilipe Manana 75420005523SFilipe Manana /* 75520005523SFilipe Manana * Confirm that no new extent entries or bitmap entries were added to 75620005523SFilipe Manana * the cache after adding that free space region. 75720005523SFilipe Manana */ 75820005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 75920005523SFilipe Manana if (ret) 76020005523SFilipe Manana return ret; 76120005523SFilipe Manana 76220005523SFilipe Manana /* 76320005523SFilipe Manana * Now lets add a small free space region to the left of the previous 76420005523SFilipe Manana * one, which is not contiguous with it and is part of the bitmap too. 76520005523SFilipe Manana * The goal is to test that the bitmap entry space stealing doesn't 76620005523SFilipe Manana * steal this space region. 76720005523SFilipe Manana */ 768*ee22184bSByongho Lee ret = btrfs_add_free_space(cache, SZ_32M, 8192); 76920005523SFilipe Manana if (ret) { 77020005523SFilipe Manana test_msg("Error adding free space: %d\n", ret); 77120005523SFilipe Manana return ret; 77220005523SFilipe Manana } 77320005523SFilipe Manana 77420005523SFilipe Manana /* 77520005523SFilipe Manana * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will 77620005523SFilipe Manana * expand the range covered by the existing extent entry that represents 77720005523SFilipe Manana * the free space [128Mb + 128Kb, 128Mb + 256Kb[. 77820005523SFilipe Manana */ 779*ee22184bSByongho Lee ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K); 78020005523SFilipe Manana if (ret) { 78120005523SFilipe Manana test_msg("Error adding free space: %d\n", ret); 78220005523SFilipe Manana return ret; 78320005523SFilipe Manana } 78420005523SFilipe Manana /* Confirm the region is marked as free. */ 785*ee22184bSByongho Lee if (!test_check_exists(cache, SZ_128M, SZ_128K)) { 78620005523SFilipe Manana test_msg("Extent region not marked as free\n"); 78720005523SFilipe Manana return -ENOENT; 78820005523SFilipe Manana } 78920005523SFilipe Manana 79020005523SFilipe Manana /* 79120005523SFilipe Manana * Confirm that our extent entry didn't stole all free space from the 79220005523SFilipe Manana * bitmap, because of the small 8Kb free space region. 79320005523SFilipe Manana */ 79420005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 79520005523SFilipe Manana if (ret) 79620005523SFilipe Manana return ret; 79720005523SFilipe Manana 79820005523SFilipe Manana /* 79920005523SFilipe Manana * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free 80020005523SFilipe Manana * space. Without stealing bitmap free space into extent entry space, 80120005523SFilipe Manana * we would have all this free space represented by 2 entries in the 80220005523SFilipe Manana * cache: 80320005523SFilipe Manana * 80420005523SFilipe Manana * extent entry covering range: [128Mb, 128Mb + 256Kb[ 80520005523SFilipe Manana * bitmap entry covering range: [128Mb - 768Kb, 128Mb[ 80620005523SFilipe Manana * 80720005523SFilipe Manana * Attempting to allocate the whole free space (1Mb) would fail, because 80820005523SFilipe Manana * we can't allocate from multiple entries. 80920005523SFilipe Manana * With the bitmap free space stealing, we get a single extent entry 81020005523SFilipe Manana * that represents the 1Mb free space, and therefore we're able to 81120005523SFilipe Manana * allocate the whole free space at once. 81220005523SFilipe Manana */ 813*ee22184bSByongho Lee if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) { 81420005523SFilipe Manana test_msg("Expected region not marked as free\n"); 81520005523SFilipe Manana return -ENOENT; 81620005523SFilipe Manana } 81720005523SFilipe Manana 818*ee22184bSByongho Lee if (cache->free_space_ctl->free_space != (SZ_1M + 8192)) { 81920005523SFilipe Manana test_msg("Cache free space is not 1Mb + 8Kb\n"); 82020005523SFilipe Manana return -EINVAL; 82120005523SFilipe Manana } 82220005523SFilipe Manana 823*ee22184bSByongho Lee offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0, 82420005523SFilipe Manana &max_extent_size); 825*ee22184bSByongho Lee if (offset != (SZ_128M - 768 * SZ_1K)) { 82620005523SFilipe Manana test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n", 82720005523SFilipe Manana offset); 82820005523SFilipe Manana return -EINVAL; 82920005523SFilipe Manana } 83020005523SFilipe Manana 83120005523SFilipe Manana /* All that remains is a 8Kb free space region in a bitmap. Confirm. */ 83220005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 1, 1); 83320005523SFilipe Manana if (ret) 83420005523SFilipe Manana return ret; 83520005523SFilipe Manana 83620005523SFilipe Manana if (cache->free_space_ctl->free_space != 8192) { 83720005523SFilipe Manana test_msg("Cache free space is not 8Kb\n"); 83820005523SFilipe Manana return -EINVAL; 83920005523SFilipe Manana } 84020005523SFilipe Manana 84120005523SFilipe Manana offset = btrfs_find_space_for_alloc(cache, 84220005523SFilipe Manana 0, 8192, 0, 84320005523SFilipe Manana &max_extent_size); 844*ee22184bSByongho Lee if (offset != SZ_32M) { 84520005523SFilipe Manana test_msg("Failed to allocate 8Kb from space cache, returned offset is: %llu\n", 84620005523SFilipe Manana offset); 84720005523SFilipe Manana return -EINVAL; 84820005523SFilipe Manana } 84920005523SFilipe Manana 85020005523SFilipe Manana ret = check_cache_empty(cache); 85120005523SFilipe Manana if (ret) 85220005523SFilipe Manana return ret; 85320005523SFilipe Manana 85420005523SFilipe Manana cache->free_space_ctl->op->use_bitmap = use_bitmap_op; 85520005523SFilipe Manana __btrfs_remove_free_space_cache(cache->free_space_ctl); 85620005523SFilipe Manana 85720005523SFilipe Manana return 0; 85820005523SFilipe Manana } 85920005523SFilipe Manana 860dc11dd5dSJosef Bacik int btrfs_test_free_space_cache(void) 861dc11dd5dSJosef Bacik { 862dc11dd5dSJosef Bacik struct btrfs_block_group_cache *cache; 863d0bd4560SJosef Bacik struct btrfs_root *root = NULL; 864d0bd4560SJosef Bacik int ret = -ENOMEM; 865dc11dd5dSJosef Bacik 866dc11dd5dSJosef Bacik test_msg("Running btrfs free space cache tests\n"); 867dc11dd5dSJosef Bacik 868dc11dd5dSJosef Bacik cache = init_test_block_group(); 869dc11dd5dSJosef Bacik if (!cache) { 870dc11dd5dSJosef Bacik test_msg("Couldn't run the tests\n"); 871dc11dd5dSJosef Bacik return 0; 872dc11dd5dSJosef Bacik } 873dc11dd5dSJosef Bacik 874d0bd4560SJosef Bacik root = btrfs_alloc_dummy_root(); 87589b6c8d1SDan Carpenter if (IS_ERR(root)) { 87689b6c8d1SDan Carpenter ret = PTR_ERR(root); 877d0bd4560SJosef Bacik goto out; 87889b6c8d1SDan Carpenter } 879d0bd4560SJosef Bacik 880d0bd4560SJosef Bacik root->fs_info = btrfs_alloc_dummy_fs_info(); 881d0bd4560SJosef Bacik if (!root->fs_info) 882d0bd4560SJosef Bacik goto out; 883d0bd4560SJosef Bacik 884d0bd4560SJosef Bacik root->fs_info->extent_root = root; 885d0bd4560SJosef Bacik cache->fs_info = root->fs_info; 886d0bd4560SJosef Bacik 887dc11dd5dSJosef Bacik ret = test_extents(cache); 888dc11dd5dSJosef Bacik if (ret) 889dc11dd5dSJosef Bacik goto out; 890dc11dd5dSJosef Bacik ret = test_bitmaps(cache); 891dc11dd5dSJosef Bacik if (ret) 892dc11dd5dSJosef Bacik goto out; 893dc11dd5dSJosef Bacik ret = test_bitmaps_and_extents(cache); 894dc11dd5dSJosef Bacik if (ret) 895dc11dd5dSJosef Bacik goto out; 89620005523SFilipe Manana 89720005523SFilipe Manana ret = test_steal_space_from_bitmap_to_extent(cache); 898dc11dd5dSJosef Bacik out: 899dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 900dc11dd5dSJosef Bacik kfree(cache->free_space_ctl); 901dc11dd5dSJosef Bacik kfree(cache); 902d0bd4560SJosef Bacik btrfs_free_dummy_root(root); 903dc11dd5dSJosef Bacik test_msg("Free space cache tests finished\n"); 904dc11dd5dSJosef Bacik return ret; 905dc11dd5dSJosef Bacik } 906