Skip to content

Commit f345dbd

Browse files
josefbacikkdave
authored andcommitted
btrfs: tests: add a test for btrfs_add_extent_mapping
This helper is different from the normal add_extent_mapping in that it will stuff an em into a gap that exists between overlapping em's in the tree. It appeared there was a bug so I wrote a self test to validate it did the correct thing when it worked with two side by side ems. Thankfully it is correct, but more testing is better. Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: Josef Bacik <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 89c3760 commit f345dbd

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

fs/btrfs/tests/extent-map-tests.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,59 @@ static int test_case_5(void)
655655
return ret;
656656
}
657657

658+
/*
659+
* Test the btrfs_add_extent_mapping helper which will attempt to create an em
660+
* for areas between two existing ems. Validate it doesn't do this when there
661+
* are two unmerged em's side by side.
662+
*/
663+
static int test_case_6(struct btrfs_fs_info *fs_info, struct extent_map_tree *em_tree)
664+
{
665+
struct extent_map *em = NULL;
666+
int ret;
667+
668+
ret = add_compressed_extent(em_tree, 0, SZ_4K, 0);
669+
if (ret)
670+
goto out;
671+
672+
ret = add_compressed_extent(em_tree, SZ_4K, SZ_4K, 0);
673+
if (ret)
674+
goto out;
675+
676+
em = alloc_extent_map();
677+
if (!em) {
678+
test_std_err(TEST_ALLOC_EXTENT_MAP);
679+
return -ENOMEM;
680+
}
681+
682+
em->start = SZ_4K;
683+
em->len = SZ_4K;
684+
em->block_start = SZ_16K;
685+
em->block_len = SZ_16K;
686+
write_lock(&em_tree->lock);
687+
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, 0, SZ_8K);
688+
write_unlock(&em_tree->lock);
689+
690+
if (ret != 0) {
691+
test_err("got an error when adding our em: %d", ret);
692+
goto out;
693+
}
694+
695+
ret = -EINVAL;
696+
if (em->start != 0) {
697+
test_err("unexpected em->start at %llu, wanted 0", em->start);
698+
goto out;
699+
}
700+
if (em->len != SZ_4K) {
701+
test_err("unexpected em->len %llu, expected 4K", em->len);
702+
goto out;
703+
}
704+
ret = 0;
705+
out:
706+
free_extent_map(em);
707+
free_extent_map_tree(em_tree);
708+
return ret;
709+
}
710+
658711
struct rmap_test_vector {
659712
u64 raid_type;
660713
u64 physical_start;
@@ -835,6 +888,9 @@ int btrfs_test_extent_map(void)
835888
if (ret)
836889
goto out;
837890
ret = test_case_5();
891+
if (ret)
892+
goto out;
893+
ret = test_case_6(fs_info, em_tree);
838894
if (ret)
839895
goto out;
840896

0 commit comments

Comments
 (0)