|
6 | 6 | #include <linux/types.h>
|
7 | 7 | #include "btrfs-tests.h"
|
8 | 8 | #include "../ctree.h"
|
| 9 | +#include "../btrfs_inode.h" |
9 | 10 | #include "../volumes.h"
|
10 | 11 | #include "../disk-io.h"
|
11 | 12 | #include "../block-group.h"
|
@@ -442,6 +443,218 @@ static int test_case_4(struct btrfs_fs_info *fs_info,
|
442 | 443 | return ret;
|
443 | 444 | }
|
444 | 445 |
|
| 446 | +static int add_compressed_extent(struct extent_map_tree *em_tree, |
| 447 | + u64 start, u64 len, u64 block_start) |
| 448 | +{ |
| 449 | + struct extent_map *em; |
| 450 | + int ret; |
| 451 | + |
| 452 | + em = alloc_extent_map(); |
| 453 | + if (!em) { |
| 454 | + test_std_err(TEST_ALLOC_EXTENT_MAP); |
| 455 | + return -ENOMEM; |
| 456 | + } |
| 457 | + |
| 458 | + em->start = start; |
| 459 | + em->len = len; |
| 460 | + em->block_start = block_start; |
| 461 | + em->block_len = SZ_4K; |
| 462 | + set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 463 | + write_lock(&em_tree->lock); |
| 464 | + ret = add_extent_mapping(em_tree, em, 0); |
| 465 | + write_unlock(&em_tree->lock); |
| 466 | + free_extent_map(em); |
| 467 | + if (ret < 0) { |
| 468 | + test_err("cannot add extent map [%llu, %llu)", start, start + len); |
| 469 | + return ret; |
| 470 | + } |
| 471 | + |
| 472 | + return 0; |
| 473 | +} |
| 474 | + |
| 475 | +struct extent_range { |
| 476 | + u64 start; |
| 477 | + u64 len; |
| 478 | +}; |
| 479 | + |
| 480 | +/* The valid states of the tree after every drop, as described below. */ |
| 481 | +struct extent_range valid_ranges[][7] = { |
| 482 | + { |
| 483 | + { .start = 0, .len = SZ_8K }, /* [0, 8K) */ |
| 484 | + { .start = SZ_4K * 3, .len = SZ_4K * 3}, /* [12k, 24k) */ |
| 485 | + { .start = SZ_4K * 6, .len = SZ_4K * 3}, /* [24k, 36k) */ |
| 486 | + { .start = SZ_32K + SZ_4K, .len = SZ_4K}, /* [36k, 40k) */ |
| 487 | + { .start = SZ_4K * 10, .len = SZ_4K * 6}, /* [40k, 64k) */ |
| 488 | + }, |
| 489 | + { |
| 490 | + { .start = 0, .len = SZ_8K }, /* [0, 8K) */ |
| 491 | + { .start = SZ_4K * 5, .len = SZ_4K}, /* [20k, 24k) */ |
| 492 | + { .start = SZ_4K * 6, .len = SZ_4K * 3}, /* [24k, 36k) */ |
| 493 | + { .start = SZ_32K + SZ_4K, .len = SZ_4K}, /* [36k, 40k) */ |
| 494 | + { .start = SZ_4K * 10, .len = SZ_4K * 6}, /* [40k, 64k) */ |
| 495 | + }, |
| 496 | + { |
| 497 | + { .start = 0, .len = SZ_8K }, /* [0, 8K) */ |
| 498 | + { .start = SZ_4K * 5, .len = SZ_4K}, /* [20k, 24k) */ |
| 499 | + { .start = SZ_4K * 6, .len = SZ_4K}, /* [24k, 28k) */ |
| 500 | + { .start = SZ_32K, .len = SZ_4K}, /* [32k, 36k) */ |
| 501 | + { .start = SZ_32K + SZ_4K, .len = SZ_4K}, /* [36k, 40k) */ |
| 502 | + { .start = SZ_4K * 10, .len = SZ_4K * 6}, /* [40k, 64k) */ |
| 503 | + }, |
| 504 | + { |
| 505 | + { .start = 0, .len = SZ_8K}, /* [0, 8K) */ |
| 506 | + { .start = SZ_4K * 5, .len = SZ_4K}, /* [20k, 24k) */ |
| 507 | + { .start = SZ_4K * 6, .len = SZ_4K}, /* [24k, 28k) */ |
| 508 | + } |
| 509 | +}; |
| 510 | + |
| 511 | +static int validate_range(struct extent_map_tree *em_tree, int index) |
| 512 | +{ |
| 513 | + struct rb_node *n; |
| 514 | + int i; |
| 515 | + |
| 516 | + for (i = 0, n = rb_first_cached(&em_tree->map); |
| 517 | + valid_ranges[index][i].len && n; |
| 518 | + i++, n = rb_next(n)) { |
| 519 | + struct extent_map *entry = rb_entry(n, struct extent_map, rb_node); |
| 520 | + |
| 521 | + if (entry->start != valid_ranges[index][i].start) { |
| 522 | + test_err("mapping has start %llu expected %llu", |
| 523 | + entry->start, valid_ranges[index][i].start); |
| 524 | + return -EINVAL; |
| 525 | + } |
| 526 | + |
| 527 | + if (entry->len != valid_ranges[index][i].len) { |
| 528 | + test_err("mapping has len %llu expected %llu", |
| 529 | + entry->len, valid_ranges[index][i].len); |
| 530 | + return -EINVAL; |
| 531 | + } |
| 532 | + } |
| 533 | + |
| 534 | + /* |
| 535 | + * We exited because we don't have any more entries in the extent_map |
| 536 | + * but we still expect more valid entries. |
| 537 | + */ |
| 538 | + if (valid_ranges[index][i].len) { |
| 539 | + test_err("missing an entry"); |
| 540 | + return -EINVAL; |
| 541 | + } |
| 542 | + |
| 543 | + /* We exited the loop but still have entries in the extent map. */ |
| 544 | + if (n) { |
| 545 | + test_err("we have a left over entry in the extent map we didn't expect"); |
| 546 | + return -EINVAL; |
| 547 | + } |
| 548 | + |
| 549 | + return 0; |
| 550 | +} |
| 551 | + |
| 552 | +/* |
| 553 | + * Test scenario: |
| 554 | + * |
| 555 | + * Test the various edge cases of btrfs_drop_extent_map_range, create the |
| 556 | + * following ranges |
| 557 | + * |
| 558 | + * [0, 12k)[12k, 24k)[24k, 36k)[36k, 40k)[40k,64k) |
| 559 | + * |
| 560 | + * And then we'll drop: |
| 561 | + * |
| 562 | + * [8k, 12k) - test the single front split |
| 563 | + * [12k, 20k) - test the single back split |
| 564 | + * [28k, 32k) - test the double split |
| 565 | + * [32k, 64k) - test whole em dropping |
| 566 | + * |
| 567 | + * They'll have the EXTENT_FLAG_COMPRESSED flag set to keep the em tree from |
| 568 | + * merging the em's. |
| 569 | + */ |
| 570 | +static int test_case_5(void) |
| 571 | +{ |
| 572 | + struct extent_map_tree *em_tree; |
| 573 | + struct inode *inode; |
| 574 | + u64 start, end; |
| 575 | + int ret; |
| 576 | + |
| 577 | + test_msg("Running btrfs_drop_extent_map_range tests"); |
| 578 | + |
| 579 | + inode = btrfs_new_test_inode(); |
| 580 | + if (!inode) { |
| 581 | + test_std_err(TEST_ALLOC_INODE); |
| 582 | + return -ENOMEM; |
| 583 | + } |
| 584 | + |
| 585 | + em_tree = &BTRFS_I(inode)->extent_tree; |
| 586 | + |
| 587 | + /* [0, 12k) */ |
| 588 | + ret = add_compressed_extent(em_tree, 0, SZ_4K * 3, 0); |
| 589 | + if (ret) { |
| 590 | + test_err("cannot add extent range [0, 12K)"); |
| 591 | + goto out; |
| 592 | + } |
| 593 | + |
| 594 | + /* [12k, 24k) */ |
| 595 | + ret = add_compressed_extent(em_tree, SZ_4K * 3, SZ_4K * 3, SZ_4K); |
| 596 | + if (ret) { |
| 597 | + test_err("cannot add extent range [12k, 24k)"); |
| 598 | + goto out; |
| 599 | + } |
| 600 | + |
| 601 | + /* [24k, 36k) */ |
| 602 | + ret = add_compressed_extent(em_tree, SZ_4K * 6, SZ_4K * 3, SZ_8K); |
| 603 | + if (ret) { |
| 604 | + test_err("cannot add extent range [12k, 24k)"); |
| 605 | + goto out; |
| 606 | + } |
| 607 | + |
| 608 | + /* [36k, 40k) */ |
| 609 | + ret = add_compressed_extent(em_tree, SZ_32K + SZ_4K, SZ_4K, SZ_4K * 3); |
| 610 | + if (ret) { |
| 611 | + test_err("cannot add extent range [12k, 24k)"); |
| 612 | + goto out; |
| 613 | + } |
| 614 | + |
| 615 | + /* [40k, 64k) */ |
| 616 | + ret = add_compressed_extent(em_tree, SZ_4K * 10, SZ_4K * 6, SZ_16K); |
| 617 | + if (ret) { |
| 618 | + test_err("cannot add extent range [12k, 24k)"); |
| 619 | + goto out; |
| 620 | + } |
| 621 | + |
| 622 | + /* Drop [8k, 12k) */ |
| 623 | + start = SZ_8K; |
| 624 | + end = (3 * SZ_4K) - 1; |
| 625 | + btrfs_drop_extent_map_range(BTRFS_I(inode), start, end, false); |
| 626 | + ret = validate_range(&BTRFS_I(inode)->extent_tree, 0); |
| 627 | + if (ret) |
| 628 | + goto out; |
| 629 | + |
| 630 | + /* Drop [12k, 20k) */ |
| 631 | + start = SZ_4K * 3; |
| 632 | + end = SZ_16K + SZ_4K - 1; |
| 633 | + btrfs_drop_extent_map_range(BTRFS_I(inode), start, end, false); |
| 634 | + ret = validate_range(&BTRFS_I(inode)->extent_tree, 1); |
| 635 | + if (ret) |
| 636 | + goto out; |
| 637 | + |
| 638 | + /* Drop [28k, 32k) */ |
| 639 | + start = SZ_32K - SZ_4K; |
| 640 | + end = SZ_32K - 1; |
| 641 | + btrfs_drop_extent_map_range(BTRFS_I(inode), start, end, false); |
| 642 | + ret = validate_range(&BTRFS_I(inode)->extent_tree, 2); |
| 643 | + if (ret) |
| 644 | + goto out; |
| 645 | + |
| 646 | + /* Drop [32k, 64k) */ |
| 647 | + start = SZ_32K; |
| 648 | + end = SZ_64K - 1; |
| 649 | + btrfs_drop_extent_map_range(BTRFS_I(inode), start, end, false); |
| 650 | + ret = validate_range(&BTRFS_I(inode)->extent_tree, 3); |
| 651 | + if (ret) |
| 652 | + goto out; |
| 653 | +out: |
| 654 | + iput(inode); |
| 655 | + return ret; |
| 656 | +} |
| 657 | + |
445 | 658 | struct rmap_test_vector {
|
446 | 659 | u64 raid_type;
|
447 | 660 | u64 physical_start;
|
@@ -619,6 +832,11 @@ int btrfs_test_extent_map(void)
|
619 | 832 | if (ret)
|
620 | 833 | goto out;
|
621 | 834 | ret = test_case_4(fs_info, em_tree);
|
| 835 | + if (ret) |
| 836 | + goto out; |
| 837 | + ret = test_case_5(); |
| 838 | + if (ret) |
| 839 | + goto out; |
622 | 840 |
|
623 | 841 | test_msg("running rmap tests");
|
624 | 842 | for (i = 0; i < ARRAY_SIZE(rmap_tests); i++) {
|
|
0 commit comments