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