| .. | .. |
|---|
| 10 | 10 | #include "btrfs-tests.h" |
|---|
| 11 | 11 | #include "../ctree.h" |
|---|
| 12 | 12 | #include "../extent_io.h" |
|---|
| 13 | +#include "../btrfs_inode.h" |
|---|
| 13 | 14 | |
|---|
| 14 | 15 | #define PROCESS_UNLOCK (1 << 0) |
|---|
| 15 | 16 | #define PROCESS_RELEASE (1 << 1) |
|---|
| .. | .. |
|---|
| 58 | 59 | static int test_find_delalloc(u32 sectorsize) |
|---|
| 59 | 60 | { |
|---|
| 60 | 61 | struct inode *inode; |
|---|
| 61 | | - struct extent_io_tree tmp; |
|---|
| 62 | + struct extent_io_tree *tmp; |
|---|
| 62 | 63 | struct page *page; |
|---|
| 63 | 64 | struct page *locked_page = NULL; |
|---|
| 64 | 65 | unsigned long index = 0; |
|---|
| 65 | | - u64 total_dirty = SZ_256M; |
|---|
| 66 | | - u64 max_bytes = SZ_128M; |
|---|
| 66 | + /* In this test we need at least 2 file extents at its maximum size */ |
|---|
| 67 | + u64 max_bytes = BTRFS_MAX_EXTENT_SIZE; |
|---|
| 68 | + u64 total_dirty = 2 * max_bytes; |
|---|
| 67 | 69 | u64 start, end, test_start; |
|---|
| 68 | | - u64 found; |
|---|
| 70 | + bool found; |
|---|
| 69 | 71 | int ret = -EINVAL; |
|---|
| 70 | 72 | |
|---|
| 71 | 73 | test_msg("running find delalloc tests"); |
|---|
| 72 | 74 | |
|---|
| 73 | 75 | inode = btrfs_new_test_inode(); |
|---|
| 74 | 76 | if (!inode) { |
|---|
| 75 | | - test_err("failed to allocate test inode"); |
|---|
| 77 | + test_std_err(TEST_ALLOC_INODE); |
|---|
| 76 | 78 | return -ENOMEM; |
|---|
| 77 | 79 | } |
|---|
| 80 | + tmp = &BTRFS_I(inode)->io_tree; |
|---|
| 78 | 81 | |
|---|
| 79 | | - extent_io_tree_init(&tmp, inode); |
|---|
| 82 | + /* |
|---|
| 83 | + * Passing NULL as we don't have fs_info but tracepoints are not used |
|---|
| 84 | + * at this point |
|---|
| 85 | + */ |
|---|
| 86 | + extent_io_tree_init(NULL, tmp, IO_TREE_SELFTEST, NULL); |
|---|
| 80 | 87 | |
|---|
| 81 | 88 | /* |
|---|
| 82 | 89 | * First go through and create and mark all of our pages dirty, we pin |
|---|
| .. | .. |
|---|
| 103 | 110 | * |--- delalloc ---| |
|---|
| 104 | 111 | * |--- search ---| |
|---|
| 105 | 112 | */ |
|---|
| 106 | | - set_extent_delalloc(&tmp, 0, sectorsize - 1, 0, NULL); |
|---|
| 113 | + set_extent_delalloc(tmp, 0, sectorsize - 1, 0, NULL); |
|---|
| 107 | 114 | start = 0; |
|---|
| 108 | 115 | end = 0; |
|---|
| 109 | | - found = find_lock_delalloc_range(inode, &tmp, locked_page, &start, |
|---|
| 110 | | - &end, max_bytes); |
|---|
| 116 | + found = find_lock_delalloc_range(inode, locked_page, &start, |
|---|
| 117 | + &end); |
|---|
| 111 | 118 | if (!found) { |
|---|
| 112 | 119 | test_err("should have found at least one delalloc"); |
|---|
| 113 | 120 | goto out_bits; |
|---|
| .. | .. |
|---|
| 117 | 124 | sectorsize - 1, start, end); |
|---|
| 118 | 125 | goto out_bits; |
|---|
| 119 | 126 | } |
|---|
| 120 | | - unlock_extent(&tmp, start, end); |
|---|
| 127 | + unlock_extent(tmp, start, end); |
|---|
| 121 | 128 | unlock_page(locked_page); |
|---|
| 122 | 129 | put_page(locked_page); |
|---|
| 123 | 130 | |
|---|
| .. | .. |
|---|
| 134 | 141 | test_err("couldn't find the locked page"); |
|---|
| 135 | 142 | goto out_bits; |
|---|
| 136 | 143 | } |
|---|
| 137 | | - set_extent_delalloc(&tmp, sectorsize, max_bytes - 1, 0, NULL); |
|---|
| 144 | + set_extent_delalloc(tmp, sectorsize, max_bytes - 1, 0, NULL); |
|---|
| 138 | 145 | start = test_start; |
|---|
| 139 | 146 | end = 0; |
|---|
| 140 | | - found = find_lock_delalloc_range(inode, &tmp, locked_page, &start, |
|---|
| 141 | | - &end, max_bytes); |
|---|
| 147 | + found = find_lock_delalloc_range(inode, locked_page, &start, |
|---|
| 148 | + &end); |
|---|
| 142 | 149 | if (!found) { |
|---|
| 143 | 150 | test_err("couldn't find delalloc in our range"); |
|---|
| 144 | 151 | goto out_bits; |
|---|
| .. | .. |
|---|
| 153 | 160 | test_err("there were unlocked pages in the range"); |
|---|
| 154 | 161 | goto out_bits; |
|---|
| 155 | 162 | } |
|---|
| 156 | | - unlock_extent(&tmp, start, end); |
|---|
| 163 | + unlock_extent(tmp, start, end); |
|---|
| 157 | 164 | /* locked_page was unlocked above */ |
|---|
| 158 | 165 | put_page(locked_page); |
|---|
| 159 | 166 | |
|---|
| .. | .. |
|---|
| 171 | 178 | } |
|---|
| 172 | 179 | start = test_start; |
|---|
| 173 | 180 | end = 0; |
|---|
| 174 | | - found = find_lock_delalloc_range(inode, &tmp, locked_page, &start, |
|---|
| 175 | | - &end, max_bytes); |
|---|
| 181 | + found = find_lock_delalloc_range(inode, locked_page, &start, |
|---|
| 182 | + &end); |
|---|
| 176 | 183 | if (found) { |
|---|
| 177 | 184 | test_err("found range when we shouldn't have"); |
|---|
| 178 | 185 | goto out_bits; |
|---|
| .. | .. |
|---|
| 189 | 196 | * |
|---|
| 190 | 197 | * We are re-using our test_start from above since it works out well. |
|---|
| 191 | 198 | */ |
|---|
| 192 | | - set_extent_delalloc(&tmp, max_bytes, total_dirty - 1, 0, NULL); |
|---|
| 199 | + set_extent_delalloc(tmp, max_bytes, total_dirty - 1, 0, NULL); |
|---|
| 193 | 200 | start = test_start; |
|---|
| 194 | 201 | end = 0; |
|---|
| 195 | | - found = find_lock_delalloc_range(inode, &tmp, locked_page, &start, |
|---|
| 196 | | - &end, max_bytes); |
|---|
| 202 | + found = find_lock_delalloc_range(inode, locked_page, &start, |
|---|
| 203 | + &end); |
|---|
| 197 | 204 | if (!found) { |
|---|
| 198 | 205 | test_err("didn't find our range"); |
|---|
| 199 | 206 | goto out_bits; |
|---|
| .. | .. |
|---|
| 208 | 215 | test_err("pages in range were not all locked"); |
|---|
| 209 | 216 | goto out_bits; |
|---|
| 210 | 217 | } |
|---|
| 211 | | - unlock_extent(&tmp, start, end); |
|---|
| 218 | + unlock_extent(tmp, start, end); |
|---|
| 212 | 219 | |
|---|
| 213 | 220 | /* |
|---|
| 214 | 221 | * Now to test where we run into a page that is no longer dirty in the |
|---|
| .. | .. |
|---|
| 233 | 240 | * this changes at any point in the future we will need to fix this |
|---|
| 234 | 241 | * tests expected behavior. |
|---|
| 235 | 242 | */ |
|---|
| 236 | | - found = find_lock_delalloc_range(inode, &tmp, locked_page, &start, |
|---|
| 237 | | - &end, max_bytes); |
|---|
| 243 | + found = find_lock_delalloc_range(inode, locked_page, &start, |
|---|
| 244 | + &end); |
|---|
| 238 | 245 | if (!found) { |
|---|
| 239 | 246 | test_err("didn't find our range"); |
|---|
| 240 | 247 | goto out_bits; |
|---|
| .. | .. |
|---|
| 251 | 258 | } |
|---|
| 252 | 259 | ret = 0; |
|---|
| 253 | 260 | out_bits: |
|---|
| 254 | | - clear_extent_bits(&tmp, 0, total_dirty - 1, (unsigned)-1); |
|---|
| 261 | + clear_extent_bits(tmp, 0, total_dirty - 1, (unsigned)-1); |
|---|
| 255 | 262 | out: |
|---|
| 256 | 263 | if (locked_page) |
|---|
| 257 | 264 | put_page(locked_page); |
|---|
| .. | .. |
|---|
| 373 | 380 | { |
|---|
| 374 | 381 | struct btrfs_fs_info *fs_info; |
|---|
| 375 | 382 | unsigned long len; |
|---|
| 376 | | - unsigned long *bitmap; |
|---|
| 377 | | - struct extent_buffer *eb; |
|---|
| 383 | + unsigned long *bitmap = NULL; |
|---|
| 384 | + struct extent_buffer *eb = NULL; |
|---|
| 378 | 385 | int ret; |
|---|
| 379 | 386 | |
|---|
| 380 | 387 | test_msg("running extent buffer bitmap tests"); |
|---|
| .. | .. |
|---|
| 387 | 394 | ? sectorsize * 4 : sectorsize; |
|---|
| 388 | 395 | |
|---|
| 389 | 396 | fs_info = btrfs_alloc_dummy_fs_info(len, len); |
|---|
| 397 | + if (!fs_info) { |
|---|
| 398 | + test_std_err(TEST_ALLOC_FS_INFO); |
|---|
| 399 | + return -ENOMEM; |
|---|
| 400 | + } |
|---|
| 390 | 401 | |
|---|
| 391 | 402 | bitmap = kmalloc(len, GFP_KERNEL); |
|---|
| 392 | 403 | if (!bitmap) { |
|---|
| 393 | 404 | test_err("couldn't allocate test bitmap"); |
|---|
| 394 | | - return -ENOMEM; |
|---|
| 405 | + ret = -ENOMEM; |
|---|
| 406 | + goto out; |
|---|
| 395 | 407 | } |
|---|
| 396 | 408 | |
|---|
| 397 | 409 | eb = __alloc_dummy_extent_buffer(fs_info, 0, len); |
|---|
| 398 | 410 | if (!eb) { |
|---|
| 399 | | - test_err("couldn't allocate test extent buffer"); |
|---|
| 400 | | - kfree(bitmap); |
|---|
| 401 | | - return -ENOMEM; |
|---|
| 411 | + test_std_err(TEST_ALLOC_ROOT); |
|---|
| 412 | + ret = -ENOMEM; |
|---|
| 413 | + goto out; |
|---|
| 402 | 414 | } |
|---|
| 403 | 415 | |
|---|
| 404 | 416 | ret = __test_eb_bitmaps(bitmap, eb, len); |
|---|
| .. | .. |
|---|
| 407 | 419 | |
|---|
| 408 | 420 | /* Do it over again with an extent buffer which isn't page-aligned. */ |
|---|
| 409 | 421 | free_extent_buffer(eb); |
|---|
| 410 | | - eb = __alloc_dummy_extent_buffer(NULL, nodesize / 2, len); |
|---|
| 422 | + eb = __alloc_dummy_extent_buffer(fs_info, nodesize / 2, len); |
|---|
| 411 | 423 | if (!eb) { |
|---|
| 412 | | - test_err("couldn't allocate test extent buffer"); |
|---|
| 413 | | - kfree(bitmap); |
|---|
| 414 | | - return -ENOMEM; |
|---|
| 424 | + test_std_err(TEST_ALLOC_ROOT); |
|---|
| 425 | + ret = -ENOMEM; |
|---|
| 426 | + goto out; |
|---|
| 415 | 427 | } |
|---|
| 416 | 428 | |
|---|
| 417 | 429 | ret = __test_eb_bitmaps(bitmap, eb, len); |
|---|
| 418 | 430 | out: |
|---|
| 419 | 431 | free_extent_buffer(eb); |
|---|
| 420 | 432 | kfree(bitmap); |
|---|
| 433 | + btrfs_free_dummy_fs_info(fs_info); |
|---|
| 434 | + return ret; |
|---|
| 435 | +} |
|---|
| 436 | + |
|---|
| 437 | +static int test_find_first_clear_extent_bit(void) |
|---|
| 438 | +{ |
|---|
| 439 | + struct extent_io_tree tree; |
|---|
| 440 | + u64 start, end; |
|---|
| 441 | + int ret = -EINVAL; |
|---|
| 442 | + |
|---|
| 443 | + test_msg("running find_first_clear_extent_bit test"); |
|---|
| 444 | + |
|---|
| 445 | + extent_io_tree_init(NULL, &tree, IO_TREE_SELFTEST, NULL); |
|---|
| 446 | + |
|---|
| 447 | + /* Test correct handling of empty tree */ |
|---|
| 448 | + find_first_clear_extent_bit(&tree, 0, &start, &end, CHUNK_TRIMMED); |
|---|
| 449 | + if (start != 0 || end != -1) { |
|---|
| 450 | + test_err( |
|---|
| 451 | + "error getting a range from completely empty tree: start %llu end %llu", |
|---|
| 452 | + start, end); |
|---|
| 453 | + goto out; |
|---|
| 454 | + } |
|---|
| 455 | + /* |
|---|
| 456 | + * Set 1M-4M alloc/discard and 32M-64M thus leaving a hole between |
|---|
| 457 | + * 4M-32M |
|---|
| 458 | + */ |
|---|
| 459 | + set_extent_bits(&tree, SZ_1M, SZ_4M - 1, |
|---|
| 460 | + CHUNK_TRIMMED | CHUNK_ALLOCATED); |
|---|
| 461 | + |
|---|
| 462 | + find_first_clear_extent_bit(&tree, SZ_512K, &start, &end, |
|---|
| 463 | + CHUNK_TRIMMED | CHUNK_ALLOCATED); |
|---|
| 464 | + |
|---|
| 465 | + if (start != 0 || end != SZ_1M - 1) { |
|---|
| 466 | + test_err("error finding beginning range: start %llu end %llu", |
|---|
| 467 | + start, end); |
|---|
| 468 | + goto out; |
|---|
| 469 | + } |
|---|
| 470 | + |
|---|
| 471 | + /* Now add 32M-64M so that we have a hole between 4M-32M */ |
|---|
| 472 | + set_extent_bits(&tree, SZ_32M, SZ_64M - 1, |
|---|
| 473 | + CHUNK_TRIMMED | CHUNK_ALLOCATED); |
|---|
| 474 | + |
|---|
| 475 | + /* |
|---|
| 476 | + * Request first hole starting at 12M, we should get 4M-32M |
|---|
| 477 | + */ |
|---|
| 478 | + find_first_clear_extent_bit(&tree, 12 * SZ_1M, &start, &end, |
|---|
| 479 | + CHUNK_TRIMMED | CHUNK_ALLOCATED); |
|---|
| 480 | + |
|---|
| 481 | + if (start != SZ_4M || end != SZ_32M - 1) { |
|---|
| 482 | + test_err("error finding trimmed range: start %llu end %llu", |
|---|
| 483 | + start, end); |
|---|
| 484 | + goto out; |
|---|
| 485 | + } |
|---|
| 486 | + |
|---|
| 487 | + /* |
|---|
| 488 | + * Search in the middle of allocated range, should get the next one |
|---|
| 489 | + * available, which happens to be unallocated -> 4M-32M |
|---|
| 490 | + */ |
|---|
| 491 | + find_first_clear_extent_bit(&tree, SZ_2M, &start, &end, |
|---|
| 492 | + CHUNK_TRIMMED | CHUNK_ALLOCATED); |
|---|
| 493 | + |
|---|
| 494 | + if (start != SZ_4M || end != SZ_32M - 1) { |
|---|
| 495 | + test_err("error finding next unalloc range: start %llu end %llu", |
|---|
| 496 | + start, end); |
|---|
| 497 | + goto out; |
|---|
| 498 | + } |
|---|
| 499 | + |
|---|
| 500 | + /* |
|---|
| 501 | + * Set 64M-72M with CHUNK_ALLOC flag, then search for CHUNK_TRIMMED flag |
|---|
| 502 | + * being unset in this range, we should get the entry in range 64M-72M |
|---|
| 503 | + */ |
|---|
| 504 | + set_extent_bits(&tree, SZ_64M, SZ_64M + SZ_8M - 1, CHUNK_ALLOCATED); |
|---|
| 505 | + find_first_clear_extent_bit(&tree, SZ_64M + SZ_1M, &start, &end, |
|---|
| 506 | + CHUNK_TRIMMED); |
|---|
| 507 | + |
|---|
| 508 | + if (start != SZ_64M || end != SZ_64M + SZ_8M - 1) { |
|---|
| 509 | + test_err("error finding exact range: start %llu end %llu", |
|---|
| 510 | + start, end); |
|---|
| 511 | + goto out; |
|---|
| 512 | + } |
|---|
| 513 | + |
|---|
| 514 | + find_first_clear_extent_bit(&tree, SZ_64M - SZ_8M, &start, &end, |
|---|
| 515 | + CHUNK_TRIMMED); |
|---|
| 516 | + |
|---|
| 517 | + /* |
|---|
| 518 | + * Search in the middle of set range whose immediate neighbour doesn't |
|---|
| 519 | + * have the bits set so it must be returned |
|---|
| 520 | + */ |
|---|
| 521 | + if (start != SZ_64M || end != SZ_64M + SZ_8M - 1) { |
|---|
| 522 | + test_err("error finding next alloc range: start %llu end %llu", |
|---|
| 523 | + start, end); |
|---|
| 524 | + goto out; |
|---|
| 525 | + } |
|---|
| 526 | + |
|---|
| 527 | + /* |
|---|
| 528 | + * Search beyond any known range, shall return after last known range |
|---|
| 529 | + * and end should be -1 |
|---|
| 530 | + */ |
|---|
| 531 | + find_first_clear_extent_bit(&tree, -1, &start, &end, CHUNK_TRIMMED); |
|---|
| 532 | + if (start != SZ_64M + SZ_8M || end != -1) { |
|---|
| 533 | + test_err( |
|---|
| 534 | + "error handling beyond end of range search: start %llu end %llu", |
|---|
| 535 | + start, end); |
|---|
| 536 | + goto out; |
|---|
| 537 | + } |
|---|
| 538 | + |
|---|
| 539 | + ret = 0; |
|---|
| 540 | +out: |
|---|
| 541 | + clear_extent_bits(&tree, 0, (u64)-1, CHUNK_TRIMMED | CHUNK_ALLOCATED); |
|---|
| 542 | + |
|---|
| 421 | 543 | return ret; |
|---|
| 422 | 544 | } |
|---|
| 423 | 545 | |
|---|
| .. | .. |
|---|
| 431 | 553 | if (ret) |
|---|
| 432 | 554 | goto out; |
|---|
| 433 | 555 | |
|---|
| 556 | + ret = test_find_first_clear_extent_bit(); |
|---|
| 557 | + if (ret) |
|---|
| 558 | + goto out; |
|---|
| 559 | + |
|---|
| 434 | 560 | ret = test_eb_bitmaps(sectorsize, nodesize); |
|---|
| 435 | 561 | out: |
|---|
| 436 | | - test_msg("extent I/O tests finished"); |
|---|
| 437 | 562 | return ret; |
|---|
| 438 | 563 | } |
|---|