1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017 Oracle. All rights reserved.
6 #include <linux/types.h>
7 #include "btrfs-tests.h"
9 #include "../volumes.h"
10 #include "../disk-io.h"
11 #include "../block-group.h"
13 static void free_extent_map_tree(struct extent_map_tree *em_tree)
15 struct extent_map *em;
18 write_lock(&em_tree->lock);
19 while (!RB_EMPTY_ROOT(&em_tree->map.rb_root)) {
20 node = rb_first_cached(&em_tree->map);
21 em = rb_entry(node, struct extent_map, rb_node);
22 remove_extent_mapping(em_tree, em);
24 #ifdef CONFIG_BTRFS_DEBUG
25 if (refcount_read(&em->refs) != 1) {
27 "em leak: em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx) refs %d",
28 em->start, em->len, em->block_start,
29 em->block_len, refcount_read(&em->refs));
31 refcount_set(&em->refs, 1);
36 write_unlock(&em_tree->lock);
42 * Suppose that no extent map has been loaded into memory yet, there is a file
43 * extent [0, 16K), followed by another file extent [16K, 20K), two dio reads
44 * are entering btrfs_get_extent() concurrently, t1 is reading [8K, 16K), t2 is
48 * btrfs_get_extent() btrfs_get_extent()
49 * -> lookup_extent_mapping() ->lookup_extent_mapping()
50 * -> add_extent_mapping(0, 16K)
52 * ->add_extent_mapping(0, 16K)
55 static int test_case_1(struct btrfs_fs_info *fs_info,
56 struct extent_map_tree *em_tree)
58 struct extent_map *em;
63 em = alloc_extent_map();
65 test_std_err(TEST_ALLOC_EXTENT_MAP);
73 em->block_len = SZ_16K;
74 write_lock(&em_tree->lock);
75 ret = add_extent_mapping(em_tree, em, 0);
76 write_unlock(&em_tree->lock);
78 test_err("cannot add extent range [0, 16K)");
83 /* Add [16K, 20K) following [0, 16K) */
84 em = alloc_extent_map();
86 test_std_err(TEST_ALLOC_EXTENT_MAP);
93 em->block_start = SZ_32K; /* avoid merging */
94 em->block_len = SZ_4K;
95 write_lock(&em_tree->lock);
96 ret = add_extent_mapping(em_tree, em, 0);
97 write_unlock(&em_tree->lock);
99 test_err("cannot add extent range [16K, 20K)");
104 em = alloc_extent_map();
106 test_std_err(TEST_ALLOC_EXTENT_MAP);
111 /* Add [0, 8K), should return [0, 16K) instead. */
114 em->block_start = start;
116 write_lock(&em_tree->lock);
117 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
118 write_unlock(&em_tree->lock);
120 test_err("case1 [%llu %llu]: ret %d", start, start + len, ret);
124 (em->start != 0 || extent_map_end(em) != SZ_16K ||
125 em->block_start != 0 || em->block_len != SZ_16K)) {
127 "case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
128 start, start + len, ret, em->start, em->len,
129 em->block_start, em->block_len);
134 free_extent_map_tree(em_tree);
142 * Reading the inline ending up with EEXIST, ie. read an inline
143 * extent and discard page cache and read it again.
145 static int test_case_2(struct btrfs_fs_info *fs_info,
146 struct extent_map_tree *em_tree)
148 struct extent_map *em;
151 em = alloc_extent_map();
153 test_std_err(TEST_ALLOC_EXTENT_MAP);
160 em->block_start = EXTENT_MAP_INLINE;
161 em->block_len = (u64)-1;
162 write_lock(&em_tree->lock);
163 ret = add_extent_mapping(em_tree, em, 0);
164 write_unlock(&em_tree->lock);
166 test_err("cannot add extent range [0, 1K)");
171 /* Add [4K, 8K) following [0, 1K) */
172 em = alloc_extent_map();
174 test_std_err(TEST_ALLOC_EXTENT_MAP);
181 em->block_start = SZ_4K;
182 em->block_len = SZ_4K;
183 write_lock(&em_tree->lock);
184 ret = add_extent_mapping(em_tree, em, 0);
185 write_unlock(&em_tree->lock);
187 test_err("cannot add extent range [4K, 8K)");
192 em = alloc_extent_map();
194 test_std_err(TEST_ALLOC_EXTENT_MAP);
202 em->block_start = EXTENT_MAP_INLINE;
203 em->block_len = (u64)-1;
204 write_lock(&em_tree->lock);
205 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
206 write_unlock(&em_tree->lock);
208 test_err("case2 [0 1K]: ret %d", ret);
212 (em->start != 0 || extent_map_end(em) != SZ_1K ||
213 em->block_start != EXTENT_MAP_INLINE || em->block_len != (u64)-1)) {
215 "case2 [0 1K]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
216 ret, em->start, em->len, em->block_start,
222 free_extent_map_tree(em_tree);
227 static int __test_case_3(struct btrfs_fs_info *fs_info,
228 struct extent_map_tree *em_tree, u64 start)
230 struct extent_map *em;
234 em = alloc_extent_map();
236 test_std_err(TEST_ALLOC_EXTENT_MAP);
243 em->block_start = SZ_4K;
244 em->block_len = SZ_4K;
245 write_lock(&em_tree->lock);
246 ret = add_extent_mapping(em_tree, em, 0);
247 write_unlock(&em_tree->lock);
249 test_err("cannot add extent range [4K, 8K)");
254 em = alloc_extent_map();
256 test_std_err(TEST_ALLOC_EXTENT_MAP);
265 em->block_len = SZ_16K;
266 write_lock(&em_tree->lock);
267 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
268 write_unlock(&em_tree->lock);
270 test_err("case3 [0x%llx 0x%llx): ret %d",
271 start, start + len, ret);
275 * Since bytes within em are contiguous, em->block_start is identical to
279 (start < em->start || start + len > extent_map_end(em) ||
280 em->start != em->block_start || em->len != em->block_len)) {
282 "case3 [0x%llx 0x%llx): ret %d em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)",
283 start, start + len, ret, em->start, em->len,
284 em->block_start, em->block_len);
289 free_extent_map_tree(em_tree);
297 * Suppose that no extent map has been loaded into memory yet.
298 * There is a file extent [0, 16K), two jobs are running concurrently
299 * against it, t1 is buffered writing to [4K, 8K) and t2 is doing dio
300 * read from [0, 4K) or [8K, 12K) or [12K, 16K).
302 * t1 goes ahead of t2 and adds em [4K, 8K) into tree.
305 * cow_file_range() btrfs_get_extent()
306 * -> lookup_extent_mapping()
307 * -> add_extent_mapping()
308 * -> add_extent_mapping()
310 static int test_case_3(struct btrfs_fs_info *fs_info,
311 struct extent_map_tree *em_tree)
315 ret = __test_case_3(fs_info, em_tree, 0);
318 ret = __test_case_3(fs_info, em_tree, SZ_8K);
321 ret = __test_case_3(fs_info, em_tree, (12 * SZ_1K));
326 static int __test_case_4(struct btrfs_fs_info *fs_info,
327 struct extent_map_tree *em_tree, u64 start)
329 struct extent_map *em;
333 em = alloc_extent_map();
335 test_std_err(TEST_ALLOC_EXTENT_MAP);
343 em->block_len = SZ_8K;
344 write_lock(&em_tree->lock);
345 ret = add_extent_mapping(em_tree, em, 0);
346 write_unlock(&em_tree->lock);
348 test_err("cannot add extent range [0, 8K)");
353 em = alloc_extent_map();
355 test_std_err(TEST_ALLOC_EXTENT_MAP);
362 em->len = 24 * SZ_1K;
363 em->block_start = SZ_16K; /* avoid merging */
364 em->block_len = 24 * SZ_1K;
365 write_lock(&em_tree->lock);
366 ret = add_extent_mapping(em_tree, em, 0);
367 write_unlock(&em_tree->lock);
369 test_err("cannot add extent range [8K, 32K)");
374 em = alloc_extent_map();
376 test_std_err(TEST_ALLOC_EXTENT_MAP);
384 em->block_len = SZ_32K;
385 write_lock(&em_tree->lock);
386 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
387 write_unlock(&em_tree->lock);
389 test_err("case4 [0x%llx 0x%llx): ret %d",
393 if (em && (start < em->start || start + len > extent_map_end(em))) {
395 "case4 [0x%llx 0x%llx): ret %d, added wrong em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)",
396 start, len, ret, em->start, em->len, em->block_start,
402 free_extent_map_tree(em_tree);
410 * Suppose that no extent map has been loaded into memory yet.
411 * There is a file extent [0, 32K), two jobs are running concurrently
412 * against it, t1 is doing dio write to [8K, 32K) and t2 is doing dio
413 * read from [0, 4K) or [4K, 8K).
415 * t1 goes ahead of t2 and splits em [0, 32K) to em [0K, 8K) and [8K 32K).
418 * btrfs_get_blocks_direct() btrfs_get_blocks_direct()
419 * -> btrfs_get_extent() -> btrfs_get_extent()
420 * -> lookup_extent_mapping()
421 * -> add_extent_mapping() -> lookup_extent_mapping()
423 * -> btrfs_new_extent_direct()
424 * -> btrfs_drop_extent_cache()
426 * -> add_extent_mapping()
428 * -> add_extent_mapping()
429 * # handle -EEXIST when adding
432 static int test_case_4(struct btrfs_fs_info *fs_info,
433 struct extent_map_tree *em_tree)
437 ret = __test_case_4(fs_info, em_tree, 0);
440 ret = __test_case_4(fs_info, em_tree, SZ_4K);
445 struct rmap_test_vector {
448 u64 data_stripe_size;
449 u64 num_data_stripes;
451 /* Assume we won't have more than 5 physical stripes */
452 u64 data_stripe_phys_start[5];
453 bool expected_mapped_addr;
454 /* Physical to logical addresses */
455 u64 mapped_logical[5];
458 static int test_rmap_block(struct btrfs_fs_info *fs_info,
459 struct rmap_test_vector *test)
461 struct extent_map *em;
462 struct map_lookup *map = NULL;
464 int i, out_ndaddrs, out_stripe_len;
467 em = alloc_extent_map();
469 test_std_err(TEST_ALLOC_EXTENT_MAP);
473 map = kmalloc(map_lookup_size(test->num_stripes), GFP_KERNEL);
476 test_std_err(TEST_ALLOC_EXTENT_MAP);
480 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
481 /* Start at 4GiB logical address */
483 em->len = test->data_stripe_size * test->num_data_stripes;
484 em->block_len = em->len;
485 em->orig_block_len = test->data_stripe_size;
486 em->map_lookup = map;
488 map->num_stripes = test->num_stripes;
489 map->type = test->raid_type;
491 for (i = 0; i < map->num_stripes; i++) {
492 struct btrfs_device *dev = btrfs_alloc_dummy_device(fs_info);
495 test_err("cannot allocate device");
499 map->stripes[i].dev = dev;
500 map->stripes[i].physical = test->data_stripe_phys_start[i];
503 write_lock(&fs_info->mapping_tree.lock);
504 ret = add_extent_mapping(&fs_info->mapping_tree, em, 0);
505 write_unlock(&fs_info->mapping_tree.lock);
507 test_err("error adding block group mapping to mapping tree");
511 ret = btrfs_rmap_block(fs_info, em->start, btrfs_sb_offset(1),
512 &logical, &out_ndaddrs, &out_stripe_len);
513 if (ret || (out_ndaddrs == 0 && test->expected_mapped_addr)) {
514 test_err("didn't rmap anything but expected %d",
515 test->expected_mapped_addr);
519 if (out_stripe_len != BTRFS_STRIPE_LEN) {
520 test_err("calculated stripe length doesn't match");
524 if (out_ndaddrs != test->expected_mapped_addr) {
525 for (i = 0; i < out_ndaddrs; i++)
526 test_msg("mapped %llu", logical[i]);
527 test_err("unexpected number of mapped addresses: %d", out_ndaddrs);
531 for (i = 0; i < out_ndaddrs; i++) {
532 if (logical[i] != test->mapped_logical[i]) {
533 test_err("unexpected logical address mapped");
540 write_lock(&fs_info->mapping_tree.lock);
541 remove_extent_mapping(&fs_info->mapping_tree, em);
542 write_unlock(&fs_info->mapping_tree.lock);
552 int btrfs_test_extent_map(void)
554 struct btrfs_fs_info *fs_info = NULL;
555 struct extent_map_tree *em_tree;
557 struct rmap_test_vector rmap_tests[] = {
560 * Test a chunk with 2 data stripes one of which
561 * intersects the physical address of the super block
562 * is correctly recognised.
564 .raid_type = BTRFS_BLOCK_GROUP_RAID1,
565 .physical_start = SZ_64M - SZ_4M,
566 .data_stripe_size = SZ_256M,
567 .num_data_stripes = 2,
569 .data_stripe_phys_start =
570 {SZ_64M - SZ_4M, SZ_64M - SZ_4M + SZ_256M},
571 .expected_mapped_addr = true,
572 .mapped_logical= {SZ_4G + SZ_4M}
576 * Test that out-of-range physical addresses are
580 /* SINGLE chunk type */
582 .physical_start = SZ_4G,
583 .data_stripe_size = SZ_256M,
584 .num_data_stripes = 1,
586 .data_stripe_phys_start = {SZ_256M},
587 .expected_mapped_addr = false,
588 .mapped_logical = {0}
592 test_msg("running extent_map tests");
595 * Note: the fs_info is not set up completely, we only need
596 * fs_info::fsid for the tracepoint.
598 fs_info = btrfs_alloc_dummy_fs_info(PAGE_SIZE, PAGE_SIZE);
600 test_std_err(TEST_ALLOC_FS_INFO);
604 em_tree = kzalloc(sizeof(*em_tree), GFP_KERNEL);
610 extent_map_tree_init(em_tree);
612 ret = test_case_1(fs_info, em_tree);
615 ret = test_case_2(fs_info, em_tree);
618 ret = test_case_3(fs_info, em_tree);
621 ret = test_case_4(fs_info, em_tree);
623 test_msg("running rmap tests");
624 for (i = 0; i < ARRAY_SIZE(rmap_tests); i++) {
625 ret = test_rmap_block(fs_info, &rmap_tests[i]);
632 btrfs_free_dummy_fs_info(fs_info);