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" 22dc11dd5dSJosef Bacik #include "../free-space-cache.h" 23dc11dd5dSJosef Bacik 24dc11dd5dSJosef Bacik #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8) 25dc11dd5dSJosef Bacik static struct btrfs_block_group_cache *init_test_block_group(void) 26dc11dd5dSJosef Bacik { 27dc11dd5dSJosef Bacik struct btrfs_block_group_cache *cache; 28dc11dd5dSJosef Bacik 29dc11dd5dSJosef Bacik cache = kzalloc(sizeof(*cache), GFP_NOFS); 30dc11dd5dSJosef Bacik if (!cache) 31dc11dd5dSJosef Bacik return NULL; 32dc11dd5dSJosef Bacik cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), 33dc11dd5dSJosef Bacik GFP_NOFS); 34dc11dd5dSJosef Bacik if (!cache->free_space_ctl) { 35dc11dd5dSJosef Bacik kfree(cache); 36dc11dd5dSJosef Bacik return NULL; 37dc11dd5dSJosef Bacik } 38dc11dd5dSJosef Bacik 39dc11dd5dSJosef Bacik cache->key.objectid = 0; 40dc11dd5dSJosef Bacik cache->key.offset = 1024 * 1024 * 1024; 41dc11dd5dSJosef Bacik cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 42dc11dd5dSJosef Bacik cache->sectorsize = 4096; 43*20005523SFilipe Manana cache->full_stripe_len = 4096; 44dc11dd5dSJosef Bacik 45dc11dd5dSJosef Bacik spin_lock_init(&cache->lock); 46dc11dd5dSJosef Bacik INIT_LIST_HEAD(&cache->list); 47dc11dd5dSJosef Bacik INIT_LIST_HEAD(&cache->cluster_list); 48dc11dd5dSJosef Bacik INIT_LIST_HEAD(&cache->new_bg_list); 49dc11dd5dSJosef Bacik 50dc11dd5dSJosef Bacik btrfs_init_free_space_ctl(cache); 51dc11dd5dSJosef Bacik 52dc11dd5dSJosef Bacik return cache; 53dc11dd5dSJosef Bacik } 54dc11dd5dSJosef Bacik 55dc11dd5dSJosef Bacik /* 56dc11dd5dSJosef Bacik * This test just does basic sanity checking, making sure we can add an exten 57dc11dd5dSJosef Bacik * entry and remove space from either end and the middle, and make sure we can 58dc11dd5dSJosef Bacik * remove space that covers adjacent extent entries. 59dc11dd5dSJosef Bacik */ 60dc11dd5dSJosef Bacik static int test_extents(struct btrfs_block_group_cache *cache) 61dc11dd5dSJosef Bacik { 62dc11dd5dSJosef Bacik int ret = 0; 63dc11dd5dSJosef Bacik 64dc11dd5dSJosef Bacik test_msg("Running extent only tests\n"); 65dc11dd5dSJosef Bacik 66dc11dd5dSJosef Bacik /* First just make sure we can remove an entire entry */ 67dc11dd5dSJosef Bacik ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024); 68dc11dd5dSJosef Bacik if (ret) { 69dc11dd5dSJosef Bacik test_msg("Error adding initial extents %d\n", ret); 70dc11dd5dSJosef Bacik return ret; 71dc11dd5dSJosef Bacik } 72dc11dd5dSJosef Bacik 73dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024); 74dc11dd5dSJosef Bacik if (ret) { 75dc11dd5dSJosef Bacik test_msg("Error removing extent %d\n", ret); 76dc11dd5dSJosef Bacik return ret; 77dc11dd5dSJosef Bacik } 78dc11dd5dSJosef Bacik 79dc11dd5dSJosef Bacik if (test_check_exists(cache, 0, 4 * 1024 * 1024)) { 80dc11dd5dSJosef Bacik test_msg("Full remove left some lingering space\n"); 81dc11dd5dSJosef Bacik return -1; 82dc11dd5dSJosef Bacik } 83dc11dd5dSJosef Bacik 84dc11dd5dSJosef Bacik /* Ok edge and middle cases now */ 85dc11dd5dSJosef Bacik ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024); 86dc11dd5dSJosef Bacik if (ret) { 87dc11dd5dSJosef Bacik test_msg("Error adding half extent %d\n", ret); 88dc11dd5dSJosef Bacik return ret; 89dc11dd5dSJosef Bacik } 90dc11dd5dSJosef Bacik 91dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 1 * 1024 * 1024); 92dc11dd5dSJosef Bacik if (ret) { 93dc11dd5dSJosef Bacik test_msg("Error removing tail end %d\n", ret); 94dc11dd5dSJosef Bacik return ret; 95dc11dd5dSJosef Bacik } 96dc11dd5dSJosef Bacik 97dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024); 98dc11dd5dSJosef Bacik if (ret) { 99dc11dd5dSJosef Bacik test_msg("Error removing front end %d\n", ret); 100dc11dd5dSJosef Bacik return ret; 101dc11dd5dSJosef Bacik } 102dc11dd5dSJosef Bacik 103dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, 2 * 1024 * 1024, 4096); 104dc11dd5dSJosef Bacik if (ret) { 10577d84ff8SMasanari Iida test_msg("Error removing middle piece %d\n", ret); 106dc11dd5dSJosef Bacik return ret; 107dc11dd5dSJosef Bacik } 108dc11dd5dSJosef Bacik 109dc11dd5dSJosef Bacik if (test_check_exists(cache, 0, 1 * 1024 * 1024)) { 110dc11dd5dSJosef Bacik test_msg("Still have space at the front\n"); 111dc11dd5dSJosef Bacik return -1; 112dc11dd5dSJosef Bacik } 113dc11dd5dSJosef Bacik 114dc11dd5dSJosef Bacik if (test_check_exists(cache, 2 * 1024 * 1024, 4096)) { 115dc11dd5dSJosef Bacik test_msg("Still have space in the middle\n"); 116dc11dd5dSJosef Bacik return -1; 117dc11dd5dSJosef Bacik } 118dc11dd5dSJosef Bacik 119dc11dd5dSJosef Bacik if (test_check_exists(cache, 3 * 1024 * 1024, 1 * 1024 * 1024)) { 120dc11dd5dSJosef Bacik test_msg("Still have space at the end\n"); 121dc11dd5dSJosef Bacik return -1; 122dc11dd5dSJosef Bacik } 123dc11dd5dSJosef Bacik 124dc11dd5dSJosef Bacik /* Cleanup */ 125dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 126dc11dd5dSJosef Bacik 127dc11dd5dSJosef Bacik return 0; 128dc11dd5dSJosef Bacik } 129dc11dd5dSJosef Bacik 130dc11dd5dSJosef Bacik static int test_bitmaps(struct btrfs_block_group_cache *cache) 131dc11dd5dSJosef Bacik { 132dc11dd5dSJosef Bacik u64 next_bitmap_offset; 133dc11dd5dSJosef Bacik int ret; 134dc11dd5dSJosef Bacik 135dc11dd5dSJosef Bacik test_msg("Running bitmap only tests\n"); 136dc11dd5dSJosef Bacik 137dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1); 138dc11dd5dSJosef Bacik if (ret) { 139dc11dd5dSJosef Bacik test_msg("Couldn't create a bitmap entry %d\n", ret); 140dc11dd5dSJosef Bacik return ret; 141dc11dd5dSJosef Bacik } 142dc11dd5dSJosef Bacik 143dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024); 144dc11dd5dSJosef Bacik if (ret) { 145dc11dd5dSJosef Bacik test_msg("Error removing bitmap full range %d\n", ret); 146dc11dd5dSJosef Bacik return ret; 147dc11dd5dSJosef Bacik } 148dc11dd5dSJosef Bacik 149dc11dd5dSJosef Bacik if (test_check_exists(cache, 0, 4 * 1024 * 1024)) { 150dc11dd5dSJosef Bacik test_msg("Left some space in bitmap\n"); 151dc11dd5dSJosef Bacik return -1; 152dc11dd5dSJosef Bacik } 153dc11dd5dSJosef Bacik 154dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1); 155dc11dd5dSJosef Bacik if (ret) { 156dc11dd5dSJosef Bacik test_msg("Couldn't add to our bitmap entry %d\n", ret); 157dc11dd5dSJosef Bacik return ret; 158dc11dd5dSJosef Bacik } 159dc11dd5dSJosef Bacik 160dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 2 * 1024 * 1024); 161dc11dd5dSJosef Bacik if (ret) { 162dc11dd5dSJosef Bacik test_msg("Couldn't remove middle chunk %d\n", ret); 163dc11dd5dSJosef Bacik return ret; 164dc11dd5dSJosef Bacik } 165dc11dd5dSJosef Bacik 166dc11dd5dSJosef Bacik /* 167dc11dd5dSJosef Bacik * The first bitmap we have starts at offset 0 so the next one is just 168dc11dd5dSJosef Bacik * at the end of the first bitmap. 169dc11dd5dSJosef Bacik */ 170dc11dd5dSJosef Bacik next_bitmap_offset = (u64)(BITS_PER_BITMAP * 4096); 171dc11dd5dSJosef Bacik 172dc11dd5dSJosef Bacik /* Test a bit straddling two bitmaps */ 173dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, next_bitmap_offset - 174dc11dd5dSJosef Bacik (2 * 1024 * 1024), 4 * 1024 * 1024, 1); 175dc11dd5dSJosef Bacik if (ret) { 176dc11dd5dSJosef Bacik test_msg("Couldn't add space that straddles two bitmaps %d\n", 177dc11dd5dSJosef Bacik ret); 178dc11dd5dSJosef Bacik return ret; 179dc11dd5dSJosef Bacik } 180dc11dd5dSJosef Bacik 181dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, next_bitmap_offset - 182dc11dd5dSJosef Bacik (1 * 1024 * 1024), 2 * 1024 * 1024); 183dc11dd5dSJosef Bacik if (ret) { 184dc11dd5dSJosef Bacik test_msg("Couldn't remove overlapping space %d\n", ret); 185dc11dd5dSJosef Bacik return ret; 186dc11dd5dSJosef Bacik } 187dc11dd5dSJosef Bacik 188dc11dd5dSJosef Bacik if (test_check_exists(cache, next_bitmap_offset - (1 * 1024 * 1024), 189dc11dd5dSJosef Bacik 2 * 1024 * 1024)) { 190dc11dd5dSJosef Bacik test_msg("Left some space when removing overlapping\n"); 191dc11dd5dSJosef Bacik return -1; 192dc11dd5dSJosef Bacik } 193dc11dd5dSJosef Bacik 194dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 195dc11dd5dSJosef Bacik 196dc11dd5dSJosef Bacik return 0; 197dc11dd5dSJosef Bacik } 198dc11dd5dSJosef Bacik 199dc11dd5dSJosef Bacik /* This is the high grade jackassery */ 200dc11dd5dSJosef Bacik static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache) 201dc11dd5dSJosef Bacik { 202dc11dd5dSJosef Bacik u64 bitmap_offset = (u64)(BITS_PER_BITMAP * 4096); 203dc11dd5dSJosef Bacik int ret; 204dc11dd5dSJosef Bacik 205dc11dd5dSJosef Bacik test_msg("Running bitmap and extent tests\n"); 206dc11dd5dSJosef Bacik 207dc11dd5dSJosef Bacik /* 208dc11dd5dSJosef Bacik * First let's do something simple, an extent at the same offset as the 209dc11dd5dSJosef Bacik * bitmap, but the free space completely in the extent and then 210dc11dd5dSJosef Bacik * completely in the bitmap. 211dc11dd5dSJosef Bacik */ 212dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, 4 * 1024 * 1024, 1 * 1024 * 1024, 1); 213dc11dd5dSJosef Bacik if (ret) { 214dc11dd5dSJosef Bacik test_msg("Couldn't create bitmap entry %d\n", ret); 215dc11dd5dSJosef Bacik return ret; 216dc11dd5dSJosef Bacik } 217dc11dd5dSJosef Bacik 218dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0); 219dc11dd5dSJosef Bacik if (ret) { 220dc11dd5dSJosef Bacik test_msg("Couldn't add extent entry %d\n", ret); 221dc11dd5dSJosef Bacik return ret; 222dc11dd5dSJosef Bacik } 223dc11dd5dSJosef Bacik 224dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024); 225dc11dd5dSJosef Bacik if (ret) { 226dc11dd5dSJosef Bacik test_msg("Couldn't remove extent entry %d\n", ret); 227dc11dd5dSJosef Bacik return ret; 228dc11dd5dSJosef Bacik } 229dc11dd5dSJosef Bacik 230dc11dd5dSJosef Bacik if (test_check_exists(cache, 0, 1 * 1024 * 1024)) { 231dc11dd5dSJosef Bacik test_msg("Left remnants after our remove\n"); 232dc11dd5dSJosef Bacik return -1; 233dc11dd5dSJosef Bacik } 234dc11dd5dSJosef Bacik 235dc11dd5dSJosef Bacik /* Now to add back the extent entry and remove from the bitmap */ 236dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0); 237dc11dd5dSJosef Bacik if (ret) { 238dc11dd5dSJosef Bacik test_msg("Couldn't re-add extent entry %d\n", ret); 239dc11dd5dSJosef Bacik return ret; 240dc11dd5dSJosef Bacik } 241dc11dd5dSJosef Bacik 242dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, 4 * 1024 * 1024, 1 * 1024 * 1024); 243dc11dd5dSJosef Bacik if (ret) { 244dc11dd5dSJosef Bacik test_msg("Couldn't remove from bitmap %d\n", ret); 245dc11dd5dSJosef Bacik return ret; 246dc11dd5dSJosef Bacik } 247dc11dd5dSJosef Bacik 248dc11dd5dSJosef Bacik if (test_check_exists(cache, 4 * 1024 * 1024, 1 * 1024 * 1024)) { 249dc11dd5dSJosef Bacik test_msg("Left remnants in the bitmap\n"); 250dc11dd5dSJosef Bacik return -1; 251dc11dd5dSJosef Bacik } 252dc11dd5dSJosef Bacik 253dc11dd5dSJosef Bacik /* 254dc11dd5dSJosef Bacik * Ok so a little more evil, extent entry and bitmap at the same offset, 255dc11dd5dSJosef Bacik * removing an overlapping chunk. 256dc11dd5dSJosef Bacik */ 257dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, 1 * 1024 * 1024, 4 * 1024 * 1024, 1); 258dc11dd5dSJosef Bacik if (ret) { 259dc11dd5dSJosef Bacik test_msg("Couldn't add to a bitmap %d\n", ret); 260dc11dd5dSJosef Bacik return ret; 261dc11dd5dSJosef Bacik } 262dc11dd5dSJosef Bacik 263dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, 512 * 1024, 3 * 1024 * 1024); 264dc11dd5dSJosef Bacik if (ret) { 265dc11dd5dSJosef Bacik test_msg("Couldn't remove overlapping space %d\n", ret); 266dc11dd5dSJosef Bacik return ret; 267dc11dd5dSJosef Bacik } 268dc11dd5dSJosef Bacik 269dc11dd5dSJosef Bacik if (test_check_exists(cache, 512 * 1024, 3 * 1024 * 1024)) { 2708faaaeadSMasanari Iida test_msg("Left over pieces after removing overlapping\n"); 271dc11dd5dSJosef Bacik return -1; 272dc11dd5dSJosef Bacik } 273dc11dd5dSJosef Bacik 274dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 275dc11dd5dSJosef Bacik 276dc11dd5dSJosef Bacik /* Now with the extent entry offset into the bitmap */ 277dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, 4 * 1024 * 1024, 4 * 1024 * 1024, 1); 278dc11dd5dSJosef Bacik if (ret) { 279dc11dd5dSJosef Bacik test_msg("Couldn't add space to the bitmap %d\n", ret); 280dc11dd5dSJosef Bacik return ret; 281dc11dd5dSJosef Bacik } 282dc11dd5dSJosef Bacik 283dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, 2 * 1024 * 1024, 2 * 1024 * 1024, 0); 284dc11dd5dSJosef Bacik if (ret) { 285dc11dd5dSJosef Bacik test_msg("Couldn't add extent to the cache %d\n", ret); 286dc11dd5dSJosef Bacik return ret; 287dc11dd5dSJosef Bacik } 288dc11dd5dSJosef Bacik 289dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 4 * 1024 * 1024); 290dc11dd5dSJosef Bacik if (ret) { 291dc11dd5dSJosef Bacik test_msg("Problem removing overlapping space %d\n", ret); 292dc11dd5dSJosef Bacik return ret; 293dc11dd5dSJosef Bacik } 294dc11dd5dSJosef Bacik 295dc11dd5dSJosef Bacik if (test_check_exists(cache, 3 * 1024 * 1024, 4 * 1024 * 1024)) { 296dc11dd5dSJosef Bacik test_msg("Left something behind when removing space"); 297dc11dd5dSJosef Bacik return -1; 298dc11dd5dSJosef Bacik } 299dc11dd5dSJosef Bacik 300dc11dd5dSJosef Bacik /* 301dc11dd5dSJosef Bacik * This has blown up in the past, the extent entry starts before the 302dc11dd5dSJosef Bacik * bitmap entry, but we're trying to remove an offset that falls 303dc11dd5dSJosef Bacik * completely within the bitmap range and is in both the extent entry 304dc11dd5dSJosef Bacik * and the bitmap entry, looks like this 305dc11dd5dSJosef Bacik * 306dc11dd5dSJosef Bacik * [ extent ] 307dc11dd5dSJosef Bacik * [ bitmap ] 308dc11dd5dSJosef Bacik * [ del ] 309dc11dd5dSJosef Bacik */ 310dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 311dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, bitmap_offset + 4 * 1024 * 1024, 312dc11dd5dSJosef Bacik 4 * 1024 * 1024, 1); 313dc11dd5dSJosef Bacik if (ret) { 314dc11dd5dSJosef Bacik test_msg("Couldn't add bitmap %d\n", ret); 315dc11dd5dSJosef Bacik return ret; 316dc11dd5dSJosef Bacik } 317dc11dd5dSJosef Bacik 318dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, bitmap_offset - 1 * 1024 * 1024, 319dc11dd5dSJosef Bacik 5 * 1024 * 1024, 0); 320dc11dd5dSJosef Bacik if (ret) { 321dc11dd5dSJosef Bacik test_msg("Couldn't add extent entry %d\n", ret); 322dc11dd5dSJosef Bacik return ret; 323dc11dd5dSJosef Bacik } 324dc11dd5dSJosef Bacik 325dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, bitmap_offset + 1 * 1024 * 1024, 326dc11dd5dSJosef Bacik 5 * 1024 * 1024); 327dc11dd5dSJosef Bacik if (ret) { 328dc11dd5dSJosef Bacik test_msg("Failed to free our space %d\n", ret); 329dc11dd5dSJosef Bacik return ret; 330dc11dd5dSJosef Bacik } 331dc11dd5dSJosef Bacik 332dc11dd5dSJosef Bacik if (test_check_exists(cache, bitmap_offset + 1 * 1024 * 1024, 333dc11dd5dSJosef Bacik 5 * 1024 * 1024)) { 334dc11dd5dSJosef Bacik test_msg("Left stuff over\n"); 335dc11dd5dSJosef Bacik return -1; 336dc11dd5dSJosef Bacik } 337dc11dd5dSJosef Bacik 338dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 339dc11dd5dSJosef Bacik 340dc11dd5dSJosef Bacik /* 341dc11dd5dSJosef Bacik * This blew up before, we have part of the free space in a bitmap and 342dc11dd5dSJosef Bacik * then the entirety of the rest of the space in an extent. This used 343dc11dd5dSJosef Bacik * to return -EAGAIN back from btrfs_remove_extent, make sure this 344dc11dd5dSJosef Bacik * doesn't happen. 345dc11dd5dSJosef Bacik */ 346dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, 1 * 1024 * 1024, 2 * 1024 * 1024, 1); 347dc11dd5dSJosef Bacik if (ret) { 348dc11dd5dSJosef Bacik test_msg("Couldn't add bitmap entry %d\n", ret); 349dc11dd5dSJosef Bacik return ret; 350dc11dd5dSJosef Bacik } 351dc11dd5dSJosef Bacik 352dc11dd5dSJosef Bacik ret = test_add_free_space_entry(cache, 3 * 1024 * 1024, 1 * 1024 * 1024, 0); 353dc11dd5dSJosef Bacik if (ret) { 354dc11dd5dSJosef Bacik test_msg("Couldn't add extent entry %d\n", ret); 355dc11dd5dSJosef Bacik return ret; 356dc11dd5dSJosef Bacik } 357dc11dd5dSJosef Bacik 358dc11dd5dSJosef Bacik ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 3 * 1024 * 1024); 359dc11dd5dSJosef Bacik if (ret) { 360dc11dd5dSJosef Bacik test_msg("Error removing bitmap and extent overlapping %d\n", ret); 361dc11dd5dSJosef Bacik return ret; 362dc11dd5dSJosef Bacik } 363dc11dd5dSJosef Bacik 364dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 365dc11dd5dSJosef Bacik return 0; 366dc11dd5dSJosef Bacik } 367dc11dd5dSJosef Bacik 368*20005523SFilipe Manana /* Used by test_steal_space_from_bitmap_to_extent(). */ 369*20005523SFilipe Manana static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl, 370*20005523SFilipe Manana struct btrfs_free_space *info) 371*20005523SFilipe Manana { 372*20005523SFilipe Manana return ctl->free_extents > 0; 373*20005523SFilipe Manana } 374*20005523SFilipe Manana 375*20005523SFilipe Manana /* Used by test_steal_space_from_bitmap_to_extent(). */ 376*20005523SFilipe Manana static int 377*20005523SFilipe Manana check_num_extents_and_bitmaps(const struct btrfs_block_group_cache *cache, 378*20005523SFilipe Manana const int num_extents, 379*20005523SFilipe Manana const int num_bitmaps) 380*20005523SFilipe Manana { 381*20005523SFilipe Manana if (cache->free_space_ctl->free_extents != num_extents) { 382*20005523SFilipe Manana test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n", 383*20005523SFilipe Manana cache->free_space_ctl->free_extents, num_extents); 384*20005523SFilipe Manana return -EINVAL; 385*20005523SFilipe Manana } 386*20005523SFilipe Manana if (cache->free_space_ctl->total_bitmaps != num_bitmaps) { 387*20005523SFilipe Manana test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n", 388*20005523SFilipe Manana cache->free_space_ctl->total_bitmaps, num_bitmaps); 389*20005523SFilipe Manana return -EINVAL; 390*20005523SFilipe Manana } 391*20005523SFilipe Manana return 0; 392*20005523SFilipe Manana } 393*20005523SFilipe Manana 394*20005523SFilipe Manana /* Used by test_steal_space_from_bitmap_to_extent(). */ 395*20005523SFilipe Manana static int check_cache_empty(struct btrfs_block_group_cache *cache) 396*20005523SFilipe Manana { 397*20005523SFilipe Manana u64 offset; 398*20005523SFilipe Manana u64 max_extent_size; 399*20005523SFilipe Manana 400*20005523SFilipe Manana /* 401*20005523SFilipe Manana * Now lets confirm that there's absolutely no free space left to 402*20005523SFilipe Manana * allocate. 403*20005523SFilipe Manana */ 404*20005523SFilipe Manana if (cache->free_space_ctl->free_space != 0) { 405*20005523SFilipe Manana test_msg("Cache free space is not 0\n"); 406*20005523SFilipe Manana return -EINVAL; 407*20005523SFilipe Manana } 408*20005523SFilipe Manana 409*20005523SFilipe Manana /* And any allocation request, no matter how small, should fail now. */ 410*20005523SFilipe Manana offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0, 411*20005523SFilipe Manana &max_extent_size); 412*20005523SFilipe Manana if (offset != 0) { 413*20005523SFilipe Manana test_msg("Space allocation did not fail, returned offset: %llu", 414*20005523SFilipe Manana offset); 415*20005523SFilipe Manana return -EINVAL; 416*20005523SFilipe Manana } 417*20005523SFilipe Manana 418*20005523SFilipe Manana /* And no extent nor bitmap entries in the cache anymore. */ 419*20005523SFilipe Manana return check_num_extents_and_bitmaps(cache, 0, 0); 420*20005523SFilipe Manana } 421*20005523SFilipe Manana 422*20005523SFilipe Manana /* 423*20005523SFilipe Manana * Before we were able to steal free space from a bitmap entry to an extent 424*20005523SFilipe Manana * entry, we could end up with 2 entries representing a contiguous free space. 425*20005523SFilipe Manana * One would be an extent entry and the other a bitmap entry. Since in order 426*20005523SFilipe Manana * to allocate space to a caller we use only 1 entry, we couldn't return that 427*20005523SFilipe Manana * whole range to the caller if it was requested. This forced the caller to 428*20005523SFilipe Manana * either assume ENOSPC or perform several smaller space allocations, which 429*20005523SFilipe Manana * wasn't optimal as they could be spread all over the block group while under 430*20005523SFilipe Manana * concurrency (extra overhead and fragmentation). 431*20005523SFilipe Manana * 432*20005523SFilipe Manana * This stealing approach is benefical, since we always prefer to allocate from 433*20005523SFilipe Manana * extent entries, both for clustered and non-clustered allocation requests. 434*20005523SFilipe Manana */ 435*20005523SFilipe Manana static int 436*20005523SFilipe Manana test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache) 437*20005523SFilipe Manana { 438*20005523SFilipe Manana int ret; 439*20005523SFilipe Manana u64 offset; 440*20005523SFilipe Manana u64 max_extent_size; 441*20005523SFilipe Manana 442*20005523SFilipe Manana bool (*use_bitmap_op)(struct btrfs_free_space_ctl *, 443*20005523SFilipe Manana struct btrfs_free_space *); 444*20005523SFilipe Manana 445*20005523SFilipe Manana test_msg("Running space stealing from bitmap to extent\n"); 446*20005523SFilipe Manana 447*20005523SFilipe Manana /* 448*20005523SFilipe Manana * For this test, we want to ensure we end up with an extent entry 449*20005523SFilipe Manana * immediately adjacent to a bitmap entry, where the bitmap starts 450*20005523SFilipe Manana * at an offset where the extent entry ends. We keep adding and 451*20005523SFilipe Manana * removing free space to reach into this state, but to get there 452*20005523SFilipe Manana * we need to reach a point where marking new free space doesn't 453*20005523SFilipe Manana * result in adding new extent entries or merging the new space 454*20005523SFilipe Manana * with existing extent entries - the space ends up being marked 455*20005523SFilipe Manana * in an existing bitmap that covers the new free space range. 456*20005523SFilipe Manana * 457*20005523SFilipe Manana * To get there, we need to reach the threshold defined set at 458*20005523SFilipe Manana * cache->free_space_ctl->extents_thresh, which currently is 459*20005523SFilipe Manana * 256 extents on a x86_64 system at least, and a few other 460*20005523SFilipe Manana * conditions (check free_space_cache.c). Instead of making the 461*20005523SFilipe Manana * test much longer and complicated, use a "use_bitmap" operation 462*20005523SFilipe Manana * that forces use of bitmaps as soon as we have at least 1 463*20005523SFilipe Manana * extent entry. 464*20005523SFilipe Manana */ 465*20005523SFilipe Manana use_bitmap_op = cache->free_space_ctl->op->use_bitmap; 466*20005523SFilipe Manana cache->free_space_ctl->op->use_bitmap = test_use_bitmap; 467*20005523SFilipe Manana 468*20005523SFilipe Manana /* 469*20005523SFilipe Manana * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[ 470*20005523SFilipe Manana */ 471*20005523SFilipe Manana ret = test_add_free_space_entry(cache, 128 * 1024 * 1024 - 256 * 1024, 472*20005523SFilipe Manana 128 * 1024, 0); 473*20005523SFilipe Manana if (ret) { 474*20005523SFilipe Manana test_msg("Couldn't add extent entry %d\n", ret); 475*20005523SFilipe Manana return ret; 476*20005523SFilipe Manana } 477*20005523SFilipe Manana 478*20005523SFilipe Manana /* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */ 479*20005523SFilipe Manana ret = test_add_free_space_entry(cache, 128 * 1024 * 1024 + 512 * 1024, 480*20005523SFilipe Manana 128 * 1024 * 1024 - 512 * 1024, 1); 481*20005523SFilipe Manana if (ret) { 482*20005523SFilipe Manana test_msg("Couldn't add bitmap entry %d\n", ret); 483*20005523SFilipe Manana return ret; 484*20005523SFilipe Manana } 485*20005523SFilipe Manana 486*20005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 487*20005523SFilipe Manana if (ret) 488*20005523SFilipe Manana return ret; 489*20005523SFilipe Manana 490*20005523SFilipe Manana /* 491*20005523SFilipe Manana * Now make only the first 256Kb of the bitmap marked as free, so that 492*20005523SFilipe Manana * we end up with only the following ranges marked as free space: 493*20005523SFilipe Manana * 494*20005523SFilipe Manana * [128Mb - 256Kb, 128Mb - 128Kb[ 495*20005523SFilipe Manana * [128Mb + 512Kb, 128Mb + 768Kb[ 496*20005523SFilipe Manana */ 497*20005523SFilipe Manana ret = btrfs_remove_free_space(cache, 498*20005523SFilipe Manana 128 * 1024 * 1024 + 768 * 1024, 499*20005523SFilipe Manana 128 * 1024 * 1024 - 768 * 1024); 500*20005523SFilipe Manana if (ret) { 501*20005523SFilipe Manana test_msg("Failed to free part of bitmap space %d\n", ret); 502*20005523SFilipe Manana return ret; 503*20005523SFilipe Manana } 504*20005523SFilipe Manana 505*20005523SFilipe Manana /* Confirm that only those 2 ranges are marked as free. */ 506*20005523SFilipe Manana if (!test_check_exists(cache, 128 * 1024 * 1024 - 256 * 1024, 507*20005523SFilipe Manana 128 * 1024)) { 508*20005523SFilipe Manana test_msg("Free space range missing\n"); 509*20005523SFilipe Manana return -ENOENT; 510*20005523SFilipe Manana } 511*20005523SFilipe Manana if (!test_check_exists(cache, 128 * 1024 * 1024 + 512 * 1024, 512*20005523SFilipe Manana 256 * 1024)) { 513*20005523SFilipe Manana test_msg("Free space range missing\n"); 514*20005523SFilipe Manana return -ENOENT; 515*20005523SFilipe Manana } 516*20005523SFilipe Manana 517*20005523SFilipe Manana /* 518*20005523SFilipe Manana * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked 519*20005523SFilipe Manana * as free anymore. 520*20005523SFilipe Manana */ 521*20005523SFilipe Manana if (test_check_exists(cache, 128 * 1024 * 1024 + 768 * 1024, 522*20005523SFilipe Manana 128 * 1024 * 1024 - 768 * 1024)) { 523*20005523SFilipe Manana test_msg("Bitmap region not removed from space cache\n"); 524*20005523SFilipe Manana return -EINVAL; 525*20005523SFilipe Manana } 526*20005523SFilipe Manana 527*20005523SFilipe Manana /* 528*20005523SFilipe Manana * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is 529*20005523SFilipe Manana * covered by the bitmap, isn't marked as free. 530*20005523SFilipe Manana */ 531*20005523SFilipe Manana if (test_check_exists(cache, 128 * 1024 * 1024 + 256 * 1024, 532*20005523SFilipe Manana 256 * 1024)) { 533*20005523SFilipe Manana test_msg("Invalid bitmap region marked as free\n"); 534*20005523SFilipe Manana return -EINVAL; 535*20005523SFilipe Manana } 536*20005523SFilipe Manana 537*20005523SFilipe Manana /* 538*20005523SFilipe Manana * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered 539*20005523SFilipe Manana * by the bitmap too, isn't marked as free either. 540*20005523SFilipe Manana */ 541*20005523SFilipe Manana if (test_check_exists(cache, 128 * 1024 * 1024, 542*20005523SFilipe Manana 256 * 1024)) { 543*20005523SFilipe Manana test_msg("Invalid bitmap region marked as free\n"); 544*20005523SFilipe Manana return -EINVAL; 545*20005523SFilipe Manana } 546*20005523SFilipe Manana 547*20005523SFilipe Manana /* 548*20005523SFilipe Manana * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But, 549*20005523SFilipe Manana * lets make sure the free space cache marks it as free in the bitmap, 550*20005523SFilipe Manana * and doesn't insert a new extent entry to represent this region. 551*20005523SFilipe Manana */ 552*20005523SFilipe Manana ret = btrfs_add_free_space(cache, 128 * 1024 * 1024, 512 * 1024); 553*20005523SFilipe Manana if (ret) { 554*20005523SFilipe Manana test_msg("Error adding free space: %d\n", ret); 555*20005523SFilipe Manana return ret; 556*20005523SFilipe Manana } 557*20005523SFilipe Manana /* Confirm the region is marked as free. */ 558*20005523SFilipe Manana if (!test_check_exists(cache, 128 * 1024 * 1024, 512 * 1024)) { 559*20005523SFilipe Manana test_msg("Bitmap region not marked as free\n"); 560*20005523SFilipe Manana return -ENOENT; 561*20005523SFilipe Manana } 562*20005523SFilipe Manana 563*20005523SFilipe Manana /* 564*20005523SFilipe Manana * Confirm that no new extent entries or bitmap entries were added to 565*20005523SFilipe Manana * the cache after adding that free space region. 566*20005523SFilipe Manana */ 567*20005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 568*20005523SFilipe Manana if (ret) 569*20005523SFilipe Manana return ret; 570*20005523SFilipe Manana 571*20005523SFilipe Manana /* 572*20005523SFilipe Manana * Now lets add a small free space region to the right of the previous 573*20005523SFilipe Manana * one, which is not contiguous with it and is part of the bitmap too. 574*20005523SFilipe Manana * The goal is to test that the bitmap entry space stealing doesn't 575*20005523SFilipe Manana * steal this space region. 576*20005523SFilipe Manana */ 577*20005523SFilipe Manana ret = btrfs_add_free_space(cache, 128 * 1024 * 1024 + 16 * 1024 * 1024, 578*20005523SFilipe Manana 4096); 579*20005523SFilipe Manana if (ret) { 580*20005523SFilipe Manana test_msg("Error adding free space: %d\n", ret); 581*20005523SFilipe Manana return ret; 582*20005523SFilipe Manana } 583*20005523SFilipe Manana 584*20005523SFilipe Manana /* 585*20005523SFilipe Manana * Confirm that no new extent entries or bitmap entries were added to 586*20005523SFilipe Manana * the cache after adding that free space region. 587*20005523SFilipe Manana */ 588*20005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 589*20005523SFilipe Manana if (ret) 590*20005523SFilipe Manana return ret; 591*20005523SFilipe Manana 592*20005523SFilipe Manana /* 593*20005523SFilipe Manana * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will 594*20005523SFilipe Manana * expand the range covered by the existing extent entry that represents 595*20005523SFilipe Manana * the free space [128Mb - 256Kb, 128Mb - 128Kb[. 596*20005523SFilipe Manana */ 597*20005523SFilipe Manana ret = btrfs_add_free_space(cache, 128 * 1024 * 1024 - 128 * 1024, 598*20005523SFilipe Manana 128 * 1024); 599*20005523SFilipe Manana if (ret) { 600*20005523SFilipe Manana test_msg("Error adding free space: %d\n", ret); 601*20005523SFilipe Manana return ret; 602*20005523SFilipe Manana } 603*20005523SFilipe Manana /* Confirm the region is marked as free. */ 604*20005523SFilipe Manana if (!test_check_exists(cache, 128 * 1024 * 1024 - 128 * 1024, 605*20005523SFilipe Manana 128 * 1024)) { 606*20005523SFilipe Manana test_msg("Extent region not marked as free\n"); 607*20005523SFilipe Manana return -ENOENT; 608*20005523SFilipe Manana } 609*20005523SFilipe Manana 610*20005523SFilipe Manana /* 611*20005523SFilipe Manana * Confirm that our extent entry didn't stole all free space from the 612*20005523SFilipe Manana * bitmap, because of the small 4Kb free space region. 613*20005523SFilipe Manana */ 614*20005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 615*20005523SFilipe Manana if (ret) 616*20005523SFilipe Manana return ret; 617*20005523SFilipe Manana 618*20005523SFilipe Manana /* 619*20005523SFilipe Manana * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free 620*20005523SFilipe Manana * space. Without stealing bitmap free space into extent entry space, 621*20005523SFilipe Manana * we would have all this free space represented by 2 entries in the 622*20005523SFilipe Manana * cache: 623*20005523SFilipe Manana * 624*20005523SFilipe Manana * extent entry covering range: [128Mb - 256Kb, 128Mb[ 625*20005523SFilipe Manana * bitmap entry covering range: [128Mb, 128Mb + 768Kb[ 626*20005523SFilipe Manana * 627*20005523SFilipe Manana * Attempting to allocate the whole free space (1Mb) would fail, because 628*20005523SFilipe Manana * we can't allocate from multiple entries. 629*20005523SFilipe Manana * With the bitmap free space stealing, we get a single extent entry 630*20005523SFilipe Manana * that represents the 1Mb free space, and therefore we're able to 631*20005523SFilipe Manana * allocate the whole free space at once. 632*20005523SFilipe Manana */ 633*20005523SFilipe Manana if (!test_check_exists(cache, 128 * 1024 * 1024 - 256 * 1024, 634*20005523SFilipe Manana 1 * 1024 * 1024)) { 635*20005523SFilipe Manana test_msg("Expected region not marked as free\n"); 636*20005523SFilipe Manana return -ENOENT; 637*20005523SFilipe Manana } 638*20005523SFilipe Manana 639*20005523SFilipe Manana if (cache->free_space_ctl->free_space != (1 * 1024 * 1024 + 4096)) { 640*20005523SFilipe Manana test_msg("Cache free space is not 1Mb + 4Kb\n"); 641*20005523SFilipe Manana return -EINVAL; 642*20005523SFilipe Manana } 643*20005523SFilipe Manana 644*20005523SFilipe Manana offset = btrfs_find_space_for_alloc(cache, 645*20005523SFilipe Manana 0, 1 * 1024 * 1024, 0, 646*20005523SFilipe Manana &max_extent_size); 647*20005523SFilipe Manana if (offset != (128 * 1024 * 1024 - 256 * 1024)) { 648*20005523SFilipe Manana test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n", 649*20005523SFilipe Manana offset); 650*20005523SFilipe Manana return -EINVAL; 651*20005523SFilipe Manana } 652*20005523SFilipe Manana 653*20005523SFilipe Manana /* All that remains is a 4Kb free space region in a bitmap. Confirm. */ 654*20005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 1, 1); 655*20005523SFilipe Manana if (ret) 656*20005523SFilipe Manana return ret; 657*20005523SFilipe Manana 658*20005523SFilipe Manana if (cache->free_space_ctl->free_space != 4096) { 659*20005523SFilipe Manana test_msg("Cache free space is not 4Kb\n"); 660*20005523SFilipe Manana return -EINVAL; 661*20005523SFilipe Manana } 662*20005523SFilipe Manana 663*20005523SFilipe Manana offset = btrfs_find_space_for_alloc(cache, 664*20005523SFilipe Manana 0, 4096, 0, 665*20005523SFilipe Manana &max_extent_size); 666*20005523SFilipe Manana if (offset != (128 * 1024 * 1024 + 16 * 1024 * 1024)) { 667*20005523SFilipe Manana test_msg("Failed to allocate 4Kb from space cache, returned offset is: %llu\n", 668*20005523SFilipe Manana offset); 669*20005523SFilipe Manana return -EINVAL; 670*20005523SFilipe Manana } 671*20005523SFilipe Manana 672*20005523SFilipe Manana ret = check_cache_empty(cache); 673*20005523SFilipe Manana if (ret) 674*20005523SFilipe Manana return ret; 675*20005523SFilipe Manana 676*20005523SFilipe Manana __btrfs_remove_free_space_cache(cache->free_space_ctl); 677*20005523SFilipe Manana 678*20005523SFilipe Manana /* 679*20005523SFilipe Manana * Now test a similar scenario, but where our extent entry is located 680*20005523SFilipe Manana * to the right of the bitmap entry, so that we can check that stealing 681*20005523SFilipe Manana * space from a bitmap to the front of an extent entry works. 682*20005523SFilipe Manana */ 683*20005523SFilipe Manana 684*20005523SFilipe Manana /* 685*20005523SFilipe Manana * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[ 686*20005523SFilipe Manana */ 687*20005523SFilipe Manana ret = test_add_free_space_entry(cache, 128 * 1024 * 1024 + 128 * 1024, 688*20005523SFilipe Manana 128 * 1024, 0); 689*20005523SFilipe Manana if (ret) { 690*20005523SFilipe Manana test_msg("Couldn't add extent entry %d\n", ret); 691*20005523SFilipe Manana return ret; 692*20005523SFilipe Manana } 693*20005523SFilipe Manana 694*20005523SFilipe Manana /* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */ 695*20005523SFilipe Manana ret = test_add_free_space_entry(cache, 0, 696*20005523SFilipe Manana 128 * 1024 * 1024 - 512 * 1024, 1); 697*20005523SFilipe Manana if (ret) { 698*20005523SFilipe Manana test_msg("Couldn't add bitmap entry %d\n", ret); 699*20005523SFilipe Manana return ret; 700*20005523SFilipe Manana } 701*20005523SFilipe Manana 702*20005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 703*20005523SFilipe Manana if (ret) 704*20005523SFilipe Manana return ret; 705*20005523SFilipe Manana 706*20005523SFilipe Manana /* 707*20005523SFilipe Manana * Now make only the last 256Kb of the bitmap marked as free, so that 708*20005523SFilipe Manana * we end up with only the following ranges marked as free space: 709*20005523SFilipe Manana * 710*20005523SFilipe Manana * [128Mb + 128b, 128Mb + 256Kb[ 711*20005523SFilipe Manana * [128Mb - 768Kb, 128Mb - 512Kb[ 712*20005523SFilipe Manana */ 713*20005523SFilipe Manana ret = btrfs_remove_free_space(cache, 714*20005523SFilipe Manana 0, 715*20005523SFilipe Manana 128 * 1024 * 1024 - 768 * 1024); 716*20005523SFilipe Manana if (ret) { 717*20005523SFilipe Manana test_msg("Failed to free part of bitmap space %d\n", ret); 718*20005523SFilipe Manana return ret; 719*20005523SFilipe Manana } 720*20005523SFilipe Manana 721*20005523SFilipe Manana /* Confirm that only those 2 ranges are marked as free. */ 722*20005523SFilipe Manana if (!test_check_exists(cache, 128 * 1024 * 1024 + 128 * 1024, 723*20005523SFilipe Manana 128 * 1024)) { 724*20005523SFilipe Manana test_msg("Free space range missing\n"); 725*20005523SFilipe Manana return -ENOENT; 726*20005523SFilipe Manana } 727*20005523SFilipe Manana if (!test_check_exists(cache, 128 * 1024 * 1024 - 768 * 1024, 728*20005523SFilipe Manana 256 * 1024)) { 729*20005523SFilipe Manana test_msg("Free space range missing\n"); 730*20005523SFilipe Manana return -ENOENT; 731*20005523SFilipe Manana } 732*20005523SFilipe Manana 733*20005523SFilipe Manana /* 734*20005523SFilipe Manana * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked 735*20005523SFilipe Manana * as free anymore. 736*20005523SFilipe Manana */ 737*20005523SFilipe Manana if (test_check_exists(cache, 0, 738*20005523SFilipe Manana 128 * 1024 * 1024 - 768 * 1024)) { 739*20005523SFilipe Manana test_msg("Bitmap region not removed from space cache\n"); 740*20005523SFilipe Manana return -EINVAL; 741*20005523SFilipe Manana } 742*20005523SFilipe Manana 743*20005523SFilipe Manana /* 744*20005523SFilipe Manana * Confirm that the region [128Mb - 512Kb, 128Mb[, which is 745*20005523SFilipe Manana * covered by the bitmap, isn't marked as free. 746*20005523SFilipe Manana */ 747*20005523SFilipe Manana if (test_check_exists(cache, 128 * 1024 * 1024 - 512 * 1024, 748*20005523SFilipe Manana 512 * 1024)) { 749*20005523SFilipe Manana test_msg("Invalid bitmap region marked as free\n"); 750*20005523SFilipe Manana return -EINVAL; 751*20005523SFilipe Manana } 752*20005523SFilipe Manana 753*20005523SFilipe Manana /* 754*20005523SFilipe Manana * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But, 755*20005523SFilipe Manana * lets make sure the free space cache marks it as free in the bitmap, 756*20005523SFilipe Manana * and doesn't insert a new extent entry to represent this region. 757*20005523SFilipe Manana */ 758*20005523SFilipe Manana ret = btrfs_add_free_space(cache, 128 * 1024 * 1024 - 512 * 1024, 759*20005523SFilipe Manana 512 * 1024); 760*20005523SFilipe Manana if (ret) { 761*20005523SFilipe Manana test_msg("Error adding free space: %d\n", ret); 762*20005523SFilipe Manana return ret; 763*20005523SFilipe Manana } 764*20005523SFilipe Manana /* Confirm the region is marked as free. */ 765*20005523SFilipe Manana if (!test_check_exists(cache, 128 * 1024 * 1024 - 512 * 1024, 766*20005523SFilipe Manana 512 * 1024)) { 767*20005523SFilipe Manana test_msg("Bitmap region not marked as free\n"); 768*20005523SFilipe Manana return -ENOENT; 769*20005523SFilipe Manana } 770*20005523SFilipe Manana 771*20005523SFilipe Manana /* 772*20005523SFilipe Manana * Confirm that no new extent entries or bitmap entries were added to 773*20005523SFilipe Manana * the cache after adding that free space region. 774*20005523SFilipe Manana */ 775*20005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 776*20005523SFilipe Manana if (ret) 777*20005523SFilipe Manana return ret; 778*20005523SFilipe Manana 779*20005523SFilipe Manana /* 780*20005523SFilipe Manana * Now lets add a small free space region to the left of the previous 781*20005523SFilipe Manana * one, which is not contiguous with it and is part of the bitmap too. 782*20005523SFilipe Manana * The goal is to test that the bitmap entry space stealing doesn't 783*20005523SFilipe Manana * steal this space region. 784*20005523SFilipe Manana */ 785*20005523SFilipe Manana ret = btrfs_add_free_space(cache, 32 * 1024 * 1024, 8192); 786*20005523SFilipe Manana if (ret) { 787*20005523SFilipe Manana test_msg("Error adding free space: %d\n", ret); 788*20005523SFilipe Manana return ret; 789*20005523SFilipe Manana } 790*20005523SFilipe Manana 791*20005523SFilipe Manana /* 792*20005523SFilipe Manana * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will 793*20005523SFilipe Manana * expand the range covered by the existing extent entry that represents 794*20005523SFilipe Manana * the free space [128Mb + 128Kb, 128Mb + 256Kb[. 795*20005523SFilipe Manana */ 796*20005523SFilipe Manana ret = btrfs_add_free_space(cache, 128 * 1024 * 1024, 128 * 1024); 797*20005523SFilipe Manana if (ret) { 798*20005523SFilipe Manana test_msg("Error adding free space: %d\n", ret); 799*20005523SFilipe Manana return ret; 800*20005523SFilipe Manana } 801*20005523SFilipe Manana /* Confirm the region is marked as free. */ 802*20005523SFilipe Manana if (!test_check_exists(cache, 128 * 1024 * 1024, 128 * 1024)) { 803*20005523SFilipe Manana test_msg("Extent region not marked as free\n"); 804*20005523SFilipe Manana return -ENOENT; 805*20005523SFilipe Manana } 806*20005523SFilipe Manana 807*20005523SFilipe Manana /* 808*20005523SFilipe Manana * Confirm that our extent entry didn't stole all free space from the 809*20005523SFilipe Manana * bitmap, because of the small 8Kb free space region. 810*20005523SFilipe Manana */ 811*20005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 2, 1); 812*20005523SFilipe Manana if (ret) 813*20005523SFilipe Manana return ret; 814*20005523SFilipe Manana 815*20005523SFilipe Manana /* 816*20005523SFilipe Manana * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free 817*20005523SFilipe Manana * space. Without stealing bitmap free space into extent entry space, 818*20005523SFilipe Manana * we would have all this free space represented by 2 entries in the 819*20005523SFilipe Manana * cache: 820*20005523SFilipe Manana * 821*20005523SFilipe Manana * extent entry covering range: [128Mb, 128Mb + 256Kb[ 822*20005523SFilipe Manana * bitmap entry covering range: [128Mb - 768Kb, 128Mb[ 823*20005523SFilipe Manana * 824*20005523SFilipe Manana * Attempting to allocate the whole free space (1Mb) would fail, because 825*20005523SFilipe Manana * we can't allocate from multiple entries. 826*20005523SFilipe Manana * With the bitmap free space stealing, we get a single extent entry 827*20005523SFilipe Manana * that represents the 1Mb free space, and therefore we're able to 828*20005523SFilipe Manana * allocate the whole free space at once. 829*20005523SFilipe Manana */ 830*20005523SFilipe Manana if (!test_check_exists(cache, 128 * 1024 * 1024 - 768 * 1024, 831*20005523SFilipe Manana 1 * 1024 * 1024)) { 832*20005523SFilipe Manana test_msg("Expected region not marked as free\n"); 833*20005523SFilipe Manana return -ENOENT; 834*20005523SFilipe Manana } 835*20005523SFilipe Manana 836*20005523SFilipe Manana if (cache->free_space_ctl->free_space != (1 * 1024 * 1024 + 8192)) { 837*20005523SFilipe Manana test_msg("Cache free space is not 1Mb + 8Kb\n"); 838*20005523SFilipe Manana return -EINVAL; 839*20005523SFilipe Manana } 840*20005523SFilipe Manana 841*20005523SFilipe Manana offset = btrfs_find_space_for_alloc(cache, 842*20005523SFilipe Manana 0, 1 * 1024 * 1024, 0, 843*20005523SFilipe Manana &max_extent_size); 844*20005523SFilipe Manana if (offset != (128 * 1024 * 1024 - 768 * 1024)) { 845*20005523SFilipe Manana test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n", 846*20005523SFilipe Manana offset); 847*20005523SFilipe Manana return -EINVAL; 848*20005523SFilipe Manana } 849*20005523SFilipe Manana 850*20005523SFilipe Manana /* All that remains is a 8Kb free space region in a bitmap. Confirm. */ 851*20005523SFilipe Manana ret = check_num_extents_and_bitmaps(cache, 1, 1); 852*20005523SFilipe Manana if (ret) 853*20005523SFilipe Manana return ret; 854*20005523SFilipe Manana 855*20005523SFilipe Manana if (cache->free_space_ctl->free_space != 8192) { 856*20005523SFilipe Manana test_msg("Cache free space is not 8Kb\n"); 857*20005523SFilipe Manana return -EINVAL; 858*20005523SFilipe Manana } 859*20005523SFilipe Manana 860*20005523SFilipe Manana offset = btrfs_find_space_for_alloc(cache, 861*20005523SFilipe Manana 0, 8192, 0, 862*20005523SFilipe Manana &max_extent_size); 863*20005523SFilipe Manana if (offset != (32 * 1024 * 1024)) { 864*20005523SFilipe Manana test_msg("Failed to allocate 8Kb from space cache, returned offset is: %llu\n", 865*20005523SFilipe Manana offset); 866*20005523SFilipe Manana return -EINVAL; 867*20005523SFilipe Manana } 868*20005523SFilipe Manana 869*20005523SFilipe Manana ret = check_cache_empty(cache); 870*20005523SFilipe Manana if (ret) 871*20005523SFilipe Manana return ret; 872*20005523SFilipe Manana 873*20005523SFilipe Manana cache->free_space_ctl->op->use_bitmap = use_bitmap_op; 874*20005523SFilipe Manana __btrfs_remove_free_space_cache(cache->free_space_ctl); 875*20005523SFilipe Manana 876*20005523SFilipe Manana return 0; 877*20005523SFilipe Manana } 878*20005523SFilipe Manana 879dc11dd5dSJosef Bacik int btrfs_test_free_space_cache(void) 880dc11dd5dSJosef Bacik { 881dc11dd5dSJosef Bacik struct btrfs_block_group_cache *cache; 882dc11dd5dSJosef Bacik int ret; 883dc11dd5dSJosef Bacik 884dc11dd5dSJosef Bacik test_msg("Running btrfs free space cache tests\n"); 885dc11dd5dSJosef Bacik 886dc11dd5dSJosef Bacik cache = init_test_block_group(); 887dc11dd5dSJosef Bacik if (!cache) { 888dc11dd5dSJosef Bacik test_msg("Couldn't run the tests\n"); 889dc11dd5dSJosef Bacik return 0; 890dc11dd5dSJosef Bacik } 891dc11dd5dSJosef Bacik 892dc11dd5dSJosef Bacik ret = test_extents(cache); 893dc11dd5dSJosef Bacik if (ret) 894dc11dd5dSJosef Bacik goto out; 895dc11dd5dSJosef Bacik ret = test_bitmaps(cache); 896dc11dd5dSJosef Bacik if (ret) 897dc11dd5dSJosef Bacik goto out; 898dc11dd5dSJosef Bacik ret = test_bitmaps_and_extents(cache); 899dc11dd5dSJosef Bacik if (ret) 900dc11dd5dSJosef Bacik goto out; 901*20005523SFilipe Manana 902*20005523SFilipe Manana ret = test_steal_space_from_bitmap_to_extent(cache); 903dc11dd5dSJosef Bacik out: 904dc11dd5dSJosef Bacik __btrfs_remove_free_space_cache(cache->free_space_ctl); 905dc11dd5dSJosef Bacik kfree(cache->free_space_ctl); 906dc11dd5dSJosef Bacik kfree(cache); 907dc11dd5dSJosef Bacik test_msg("Free space cache tests finished\n"); 908dc11dd5dSJosef Bacik return ret; 909dc11dd5dSJosef Bacik } 910