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 "../btrfs_inode.h"
10 #include "../volumes.h"
11 #include "../disk-io.h"
12 #include "../block-group.h"
14 static int free_extent_map_tree(struct btrfs_inode
*inode
)
16 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
17 struct extent_map
*em
;
21 write_lock(&em_tree
->lock
);
22 while (!RB_EMPTY_ROOT(&em_tree
->root
)) {
23 node
= rb_first(&em_tree
->root
);
24 em
= rb_entry(node
, struct extent_map
, rb_node
);
25 remove_extent_mapping(inode
, em
);
27 #ifdef CONFIG_BTRFS_DEBUG
28 if (refcount_read(&em
->refs
) != 1) {
31 "em leak: em (start %llu len %llu disk_bytenr %llu disk_num_bytes %llu offset %llu) refs %d",
32 em
->start
, em
->len
, em
->disk_bytenr
,
33 em
->disk_num_bytes
, em
->offset
,
34 refcount_read(&em
->refs
));
36 refcount_set(&em
->refs
, 1);
41 write_unlock(&em_tree
->lock
);
49 * Suppose that no extent map has been loaded into memory yet, there is a file
50 * extent [0, 16K), followed by another file extent [16K, 20K), two dio reads
51 * are entering btrfs_get_extent() concurrently, t1 is reading [8K, 16K), t2 is
55 * btrfs_get_extent() btrfs_get_extent()
56 * -> lookup_extent_mapping() ->lookup_extent_mapping()
57 * -> add_extent_mapping(0, 16K)
59 * ->add_extent_mapping(0, 16K)
62 static int test_case_1(struct btrfs_fs_info
*fs_info
, struct btrfs_inode
*inode
)
64 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
65 struct extent_map
*em
;
71 em
= alloc_extent_map();
73 test_std_err(TEST_ALLOC_EXTENT_MAP
);
81 em
->disk_num_bytes
= SZ_16K
;
82 em
->ram_bytes
= SZ_16K
;
83 write_lock(&em_tree
->lock
);
84 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
85 write_unlock(&em_tree
->lock
);
87 test_err("cannot add extent range [0, 16K)");
92 /* Add [16K, 20K) following [0, 16K) */
93 em
= alloc_extent_map();
95 test_std_err(TEST_ALLOC_EXTENT_MAP
);
102 em
->disk_bytenr
= SZ_32K
; /* avoid merging */
103 em
->disk_num_bytes
= SZ_4K
;
104 em
->ram_bytes
= SZ_4K
;
105 write_lock(&em_tree
->lock
);
106 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
107 write_unlock(&em_tree
->lock
);
109 test_err("cannot add extent range [16K, 20K)");
114 em
= alloc_extent_map();
116 test_std_err(TEST_ALLOC_EXTENT_MAP
);
121 /* Add [0, 8K), should return [0, 16K) instead. */
124 em
->disk_bytenr
= start
;
125 em
->disk_num_bytes
= len
;
127 write_lock(&em_tree
->lock
);
128 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
129 write_unlock(&em_tree
->lock
);
131 test_err("case1 [%llu %llu]: ret %d", start
, start
+ len
, ret
);
135 test_err("case1 [%llu %llu]: no extent map returned",
140 if (em
->start
!= 0 || extent_map_end(em
) != SZ_16K
||
141 em
->disk_bytenr
!= 0 || em
->disk_num_bytes
!= SZ_16K
) {
143 "case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu disk_bytenr %llu disk_num_bytes %llu",
144 start
, start
+ len
, ret
, em
->start
, em
->len
,
145 em
->disk_bytenr
, em
->disk_num_bytes
);
150 ret2
= free_extent_map_tree(inode
);
160 * Reading the inline ending up with EEXIST, ie. read an inline
161 * extent and discard page cache and read it again.
163 static int test_case_2(struct btrfs_fs_info
*fs_info
, struct btrfs_inode
*inode
)
165 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
166 struct extent_map
*em
;
170 em
= alloc_extent_map();
172 test_std_err(TEST_ALLOC_EXTENT_MAP
);
179 em
->disk_bytenr
= EXTENT_MAP_INLINE
;
180 em
->disk_num_bytes
= 0;
181 em
->ram_bytes
= SZ_1K
;
182 write_lock(&em_tree
->lock
);
183 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
184 write_unlock(&em_tree
->lock
);
186 test_err("cannot add extent range [0, 1K)");
191 /* Add [4K, 8K) following [0, 1K) */
192 em
= alloc_extent_map();
194 test_std_err(TEST_ALLOC_EXTENT_MAP
);
201 em
->disk_bytenr
= SZ_4K
;
202 em
->disk_num_bytes
= SZ_4K
;
203 em
->ram_bytes
= SZ_4K
;
204 write_lock(&em_tree
->lock
);
205 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
206 write_unlock(&em_tree
->lock
);
208 test_err("cannot add extent range [4K, 8K)");
213 em
= alloc_extent_map();
215 test_std_err(TEST_ALLOC_EXTENT_MAP
);
223 em
->disk_bytenr
= EXTENT_MAP_INLINE
;
224 em
->disk_num_bytes
= 0;
225 em
->ram_bytes
= SZ_1K
;
226 write_lock(&em_tree
->lock
);
227 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
228 write_unlock(&em_tree
->lock
);
230 test_err("case2 [0 1K]: ret %d", ret
);
234 test_err("case2 [0 1K]: no extent map returned");
238 if (em
->start
!= 0 || extent_map_end(em
) != SZ_1K
||
239 em
->disk_bytenr
!= EXTENT_MAP_INLINE
) {
241 "case2 [0 1K]: ret %d return a wrong em (start %llu len %llu disk_bytenr %llu",
242 ret
, em
->start
, em
->len
, em
->disk_bytenr
);
247 ret2
= free_extent_map_tree(inode
);
254 static int __test_case_3(struct btrfs_fs_info
*fs_info
,
255 struct btrfs_inode
*inode
, u64 start
)
257 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
258 struct extent_map
*em
;
263 em
= alloc_extent_map();
265 test_std_err(TEST_ALLOC_EXTENT_MAP
);
272 em
->disk_bytenr
= SZ_4K
;
273 em
->disk_num_bytes
= SZ_4K
;
274 em
->ram_bytes
= SZ_4K
;
275 write_lock(&em_tree
->lock
);
276 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
277 write_unlock(&em_tree
->lock
);
279 test_err("cannot add extent range [4K, 8K)");
284 em
= alloc_extent_map();
286 test_std_err(TEST_ALLOC_EXTENT_MAP
);
295 em
->disk_num_bytes
= SZ_16K
;
296 em
->ram_bytes
= SZ_16K
;
297 write_lock(&em_tree
->lock
);
298 ret
= btrfs_add_extent_mapping(inode
, &em
, start
, len
);
299 write_unlock(&em_tree
->lock
);
301 test_err("case3 [%llu %llu): ret %d",
302 start
, start
+ len
, ret
);
306 test_err("case3 [%llu %llu): no extent map returned",
312 * Since bytes within em are contiguous, em->block_start is identical to
315 if (start
< em
->start
|| start
+ len
> extent_map_end(em
) ||
316 em
->start
!= extent_map_block_start(em
)) {
318 "case3 [%llu %llu): ret %d em (start %llu len %llu disk_bytenr %llu block_len %llu)",
319 start
, start
+ len
, ret
, em
->start
, em
->len
,
320 em
->disk_bytenr
, em
->disk_num_bytes
);
325 ret2
= free_extent_map_tree(inode
);
335 * Suppose that no extent map has been loaded into memory yet.
336 * There is a file extent [0, 16K), two jobs are running concurrently
337 * against it, t1 is buffered writing to [4K, 8K) and t2 is doing dio
338 * read from [0, 4K) or [8K, 12K) or [12K, 16K).
340 * t1 goes ahead of t2 and adds em [4K, 8K) into tree.
343 * cow_file_range() btrfs_get_extent()
344 * -> lookup_extent_mapping()
345 * -> add_extent_mapping()
346 * -> add_extent_mapping()
348 static int test_case_3(struct btrfs_fs_info
*fs_info
, struct btrfs_inode
*inode
)
352 ret
= __test_case_3(fs_info
, inode
, 0);
355 ret
= __test_case_3(fs_info
, inode
, SZ_8K
);
358 ret
= __test_case_3(fs_info
, inode
, (12 * SZ_1K
));
363 static int __test_case_4(struct btrfs_fs_info
*fs_info
,
364 struct btrfs_inode
*inode
, u64 start
)
366 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
367 struct extent_map
*em
;
372 em
= alloc_extent_map();
374 test_std_err(TEST_ALLOC_EXTENT_MAP
);
382 em
->disk_num_bytes
= SZ_8K
;
383 em
->ram_bytes
= SZ_8K
;
384 write_lock(&em_tree
->lock
);
385 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
386 write_unlock(&em_tree
->lock
);
388 test_err("cannot add extent range [0, 8K)");
393 em
= alloc_extent_map();
395 test_std_err(TEST_ALLOC_EXTENT_MAP
);
402 em
->len
= 24 * SZ_1K
;
403 em
->disk_bytenr
= SZ_16K
; /* avoid merging */
404 em
->disk_num_bytes
= 24 * SZ_1K
;
405 em
->ram_bytes
= 24 * SZ_1K
;
406 write_lock(&em_tree
->lock
);
407 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
408 write_unlock(&em_tree
->lock
);
410 test_err("cannot add extent range [8K, 32K)");
415 em
= alloc_extent_map();
417 test_std_err(TEST_ALLOC_EXTENT_MAP
);
425 em
->disk_num_bytes
= SZ_32K
;
426 em
->ram_bytes
= SZ_32K
;
427 write_lock(&em_tree
->lock
);
428 ret
= btrfs_add_extent_mapping(inode
, &em
, start
, len
);
429 write_unlock(&em_tree
->lock
);
431 test_err("case4 [%llu %llu): ret %d",
432 start
, start
+ len
, ret
);
436 test_err("case4 [%llu %llu): no extent map returned",
441 if (start
< em
->start
|| start
+ len
> extent_map_end(em
)) {
443 "case4 [%llu %llu): ret %d, added wrong em (start %llu len %llu disk_bytenr %llu disk_num_bytes %llu)",
444 start
, start
+ len
, ret
, em
->start
, em
->len
,
445 em
->disk_bytenr
, em
->disk_num_bytes
);
450 ret2
= free_extent_map_tree(inode
);
460 * Suppose that no extent map has been loaded into memory yet.
461 * There is a file extent [0, 32K), two jobs are running concurrently
462 * against it, t1 is doing dio write to [8K, 32K) and t2 is doing dio
463 * read from [0, 4K) or [4K, 8K).
465 * t1 goes ahead of t2 and splits em [0, 32K) to em [0K, 8K) and [8K 32K).
468 * btrfs_get_blocks_direct() btrfs_get_blocks_direct()
469 * -> btrfs_get_extent() -> btrfs_get_extent()
470 * -> lookup_extent_mapping()
471 * -> add_extent_mapping() -> lookup_extent_mapping()
473 * -> btrfs_new_extent_direct()
474 * -> btrfs_drop_extent_cache()
476 * -> add_extent_mapping()
478 * -> add_extent_mapping()
479 * # handle -EEXIST when adding
482 static int test_case_4(struct btrfs_fs_info
*fs_info
, struct btrfs_inode
*inode
)
486 ret
= __test_case_4(fs_info
, inode
, 0);
489 ret
= __test_case_4(fs_info
, inode
, SZ_4K
);
494 static int add_compressed_extent(struct btrfs_inode
*inode
,
495 u64 start
, u64 len
, u64 block_start
)
497 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
498 struct extent_map
*em
;
501 em
= alloc_extent_map();
503 test_std_err(TEST_ALLOC_EXTENT_MAP
);
509 em
->disk_bytenr
= block_start
;
510 em
->disk_num_bytes
= SZ_4K
;
512 em
->flags
|= EXTENT_FLAG_COMPRESS_ZLIB
;
513 write_lock(&em_tree
->lock
);
514 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
515 write_unlock(&em_tree
->lock
);
518 test_err("cannot add extent map [%llu, %llu)", start
, start
+ len
);
525 struct extent_range
{
530 /* The valid states of the tree after every drop, as described below. */
531 struct extent_range valid_ranges
[][7] = {
533 { .start
= 0, .len
= SZ_8K
}, /* [0, 8K) */
534 { .start
= SZ_4K
* 3, .len
= SZ_4K
* 3}, /* [12k, 24k) */
535 { .start
= SZ_4K
* 6, .len
= SZ_4K
* 3}, /* [24k, 36k) */
536 { .start
= SZ_32K
+ SZ_4K
, .len
= SZ_4K
}, /* [36k, 40k) */
537 { .start
= SZ_4K
* 10, .len
= SZ_4K
* 6}, /* [40k, 64k) */
540 { .start
= 0, .len
= SZ_8K
}, /* [0, 8K) */
541 { .start
= SZ_4K
* 5, .len
= SZ_4K
}, /* [20k, 24k) */
542 { .start
= SZ_4K
* 6, .len
= SZ_4K
* 3}, /* [24k, 36k) */
543 { .start
= SZ_32K
+ SZ_4K
, .len
= SZ_4K
}, /* [36k, 40k) */
544 { .start
= SZ_4K
* 10, .len
= SZ_4K
* 6}, /* [40k, 64k) */
547 { .start
= 0, .len
= SZ_8K
}, /* [0, 8K) */
548 { .start
= SZ_4K
* 5, .len
= SZ_4K
}, /* [20k, 24k) */
549 { .start
= SZ_4K
* 6, .len
= SZ_4K
}, /* [24k, 28k) */
550 { .start
= SZ_32K
, .len
= SZ_4K
}, /* [32k, 36k) */
551 { .start
= SZ_32K
+ SZ_4K
, .len
= SZ_4K
}, /* [36k, 40k) */
552 { .start
= SZ_4K
* 10, .len
= SZ_4K
* 6}, /* [40k, 64k) */
555 { .start
= 0, .len
= SZ_8K
}, /* [0, 8K) */
556 { .start
= SZ_4K
* 5, .len
= SZ_4K
}, /* [20k, 24k) */
557 { .start
= SZ_4K
* 6, .len
= SZ_4K
}, /* [24k, 28k) */
561 static int validate_range(struct extent_map_tree
*em_tree
, int index
)
566 for (i
= 0, n
= rb_first(&em_tree
->root
);
567 valid_ranges
[index
][i
].len
&& n
;
568 i
++, n
= rb_next(n
)) {
569 struct extent_map
*entry
= rb_entry(n
, struct extent_map
, rb_node
);
571 if (entry
->start
!= valid_ranges
[index
][i
].start
) {
572 test_err("mapping has start %llu expected %llu",
573 entry
->start
, valid_ranges
[index
][i
].start
);
577 if (entry
->len
!= valid_ranges
[index
][i
].len
) {
578 test_err("mapping has len %llu expected %llu",
579 entry
->len
, valid_ranges
[index
][i
].len
);
585 * We exited because we don't have any more entries in the extent_map
586 * but we still expect more valid entries.
588 if (valid_ranges
[index
][i
].len
) {
589 test_err("missing an entry");
593 /* We exited the loop but still have entries in the extent map. */
595 test_err("we have a left over entry in the extent map we didn't expect");
605 * Test the various edge cases of btrfs_drop_extent_map_range, create the
608 * [0, 12k)[12k, 24k)[24k, 36k)[36k, 40k)[40k,64k)
610 * And then we'll drop:
612 * [8k, 12k) - test the single front split
613 * [12k, 20k) - test the single back split
614 * [28k, 32k) - test the double split
615 * [32k, 64k) - test whole em dropping
617 * They'll have the EXTENT_FLAG_COMPRESSED flag set to keep the em tree from
620 static int test_case_5(struct btrfs_fs_info
*fs_info
, struct btrfs_inode
*inode
)
626 test_msg("Running btrfs_drop_extent_map_range tests");
629 ret
= add_compressed_extent(inode
, 0, SZ_4K
* 3, 0);
631 test_err("cannot add extent range [0, 12K)");
636 ret
= add_compressed_extent(inode
, SZ_4K
* 3, SZ_4K
* 3, SZ_4K
);
638 test_err("cannot add extent range [12k, 24k)");
643 ret
= add_compressed_extent(inode
, SZ_4K
* 6, SZ_4K
* 3, SZ_8K
);
645 test_err("cannot add extent range [12k, 24k)");
650 ret
= add_compressed_extent(inode
, SZ_32K
+ SZ_4K
, SZ_4K
, SZ_4K
* 3);
652 test_err("cannot add extent range [12k, 24k)");
657 ret
= add_compressed_extent(inode
, SZ_4K
* 10, SZ_4K
* 6, SZ_16K
);
659 test_err("cannot add extent range [12k, 24k)");
665 end
= (3 * SZ_4K
) - 1;
666 btrfs_drop_extent_map_range(inode
, start
, end
, false);
667 ret
= validate_range(&inode
->extent_tree
, 0);
671 /* Drop [12k, 20k) */
673 end
= SZ_16K
+ SZ_4K
- 1;
674 btrfs_drop_extent_map_range(inode
, start
, end
, false);
675 ret
= validate_range(&inode
->extent_tree
, 1);
679 /* Drop [28k, 32k) */
680 start
= SZ_32K
- SZ_4K
;
682 btrfs_drop_extent_map_range(inode
, start
, end
, false);
683 ret
= validate_range(&inode
->extent_tree
, 2);
687 /* Drop [32k, 64k) */
690 btrfs_drop_extent_map_range(inode
, start
, end
, false);
691 ret
= validate_range(&inode
->extent_tree
, 3);
695 ret2
= free_extent_map_tree(inode
);
703 * Test the btrfs_add_extent_mapping helper which will attempt to create an em
704 * for areas between two existing ems. Validate it doesn't do this when there
705 * are two unmerged em's side by side.
707 static int test_case_6(struct btrfs_fs_info
*fs_info
, struct btrfs_inode
*inode
)
709 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
710 struct extent_map
*em
= NULL
;
714 ret
= add_compressed_extent(inode
, 0, SZ_4K
, 0);
718 ret
= add_compressed_extent(inode
, SZ_4K
, SZ_4K
, 0);
722 em
= alloc_extent_map();
724 test_std_err(TEST_ALLOC_EXTENT_MAP
);
731 em
->disk_bytenr
= SZ_16K
;
732 em
->disk_num_bytes
= SZ_16K
;
733 em
->ram_bytes
= SZ_16K
;
734 write_lock(&em_tree
->lock
);
735 ret
= btrfs_add_extent_mapping(inode
, &em
, 0, SZ_8K
);
736 write_unlock(&em_tree
->lock
);
739 test_err("got an error when adding our em: %d", ret
);
744 if (em
->start
!= 0) {
745 test_err("unexpected em->start at %llu, wanted 0", em
->start
);
748 if (em
->len
!= SZ_4K
) {
749 test_err("unexpected em->len %llu, expected 4K", em
->len
);
755 ret2
= free_extent_map_tree(inode
);
763 * Regression test for btrfs_drop_extent_map_range. Calling with skip_pinned ==
764 * true would mess up the start/end calculations and subsequent splits would be
767 static int test_case_7(struct btrfs_fs_info
*fs_info
, struct btrfs_inode
*inode
)
769 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
770 struct extent_map
*em
;
774 test_msg("Running btrfs_drop_extent_cache with pinned");
776 em
= alloc_extent_map();
778 test_std_err(TEST_ALLOC_EXTENT_MAP
);
782 /* [0, 16K), pinned */
786 em
->disk_num_bytes
= SZ_4K
;
787 em
->ram_bytes
= SZ_16K
;
788 em
->flags
|= (EXTENT_FLAG_PINNED
| EXTENT_FLAG_COMPRESS_ZLIB
);
789 write_lock(&em_tree
->lock
);
790 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
791 write_unlock(&em_tree
->lock
);
793 test_err("couldn't add extent map");
798 em
= alloc_extent_map();
800 test_std_err(TEST_ALLOC_EXTENT_MAP
);
805 /* [32K, 48K), not pinned */
808 em
->disk_bytenr
= SZ_32K
;
809 em
->disk_num_bytes
= SZ_16K
;
810 em
->ram_bytes
= SZ_16K
;
811 write_lock(&em_tree
->lock
);
812 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
813 write_unlock(&em_tree
->lock
);
815 test_err("couldn't add extent map");
821 * Drop [0, 36K) This should skip the [0, 4K) extent and then split the
824 btrfs_drop_extent_map_range(inode
, 0, (36 * SZ_1K
) - 1, true);
826 /* Make sure our extent maps look sane. */
829 em
= lookup_extent_mapping(em_tree
, 0, SZ_16K
);
831 test_err("didn't find an em at 0 as expected");
835 if (em
->start
!= 0) {
836 test_err("em->start is %llu, expected 0", em
->start
);
840 if (em
->len
!= SZ_16K
) {
841 test_err("em->len is %llu, expected 16K", em
->len
);
847 read_lock(&em_tree
->lock
);
848 em
= lookup_extent_mapping(em_tree
, SZ_16K
, SZ_16K
);
849 read_unlock(&em_tree
->lock
);
851 test_err("found an em when we weren't expecting one");
855 read_lock(&em_tree
->lock
);
856 em
= lookup_extent_mapping(em_tree
, SZ_32K
, SZ_16K
);
857 read_unlock(&em_tree
->lock
);
859 test_err("didn't find an em at 32K as expected");
863 if (em
->start
!= (36 * SZ_1K
)) {
864 test_err("em->start is %llu, expected 36K", em
->start
);
868 if (em
->len
!= (12 * SZ_1K
)) {
869 test_err("em->len is %llu, expected 12K", em
->len
);
873 if (extent_map_block_start(em
) != SZ_32K
+ SZ_4K
) {
874 test_err("em->block_start is %llu, expected 36K",
875 extent_map_block_start(em
));
881 read_lock(&em_tree
->lock
);
882 em
= lookup_extent_mapping(em_tree
, 48 * SZ_1K
, (u64
)-1);
883 read_unlock(&em_tree
->lock
);
885 test_err("found an unexpected em above 48K");
892 /* Unpin our extent to prevent warning when removing it below. */
893 ret2
= unpin_extent_cache(inode
, 0, SZ_16K
, 0);
896 ret2
= free_extent_map_tree(inode
);
904 * Test a regression for compressed extent map adjustment when we attempt to
905 * add an extent map that is partially overlapped by another existing extent
906 * map. The resulting extent map offset was left unchanged despite having
907 * incremented its start offset.
909 static int test_case_8(struct btrfs_fs_info
*fs_info
, struct btrfs_inode
*inode
)
911 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
912 struct extent_map
*em
;
916 em
= alloc_extent_map();
918 test_std_err(TEST_ALLOC_EXTENT_MAP
);
922 /* Compressed extent for the file range [120K, 128K). */
923 em
->start
= SZ_1K
* 120;
925 em
->disk_num_bytes
= SZ_4K
;
926 em
->ram_bytes
= SZ_8K
;
927 em
->flags
|= EXTENT_FLAG_COMPRESS_ZLIB
;
928 write_lock(&em_tree
->lock
);
929 ret
= btrfs_add_extent_mapping(inode
, &em
, em
->start
, em
->len
);
930 write_unlock(&em_tree
->lock
);
933 test_err("couldn't add extent map for range [120K, 128K)");
937 em
= alloc_extent_map();
939 test_std_err(TEST_ALLOC_EXTENT_MAP
);
945 * Compressed extent for the file range [108K, 144K), which overlaps
946 * with the [120K, 128K) we previously inserted.
948 em
->start
= SZ_1K
* 108;
949 em
->len
= SZ_1K
* 36;
950 em
->disk_num_bytes
= SZ_4K
;
951 em
->ram_bytes
= SZ_1K
* 36;
952 em
->flags
|= EXTENT_FLAG_COMPRESS_ZLIB
;
955 * Try to add the extent map but with a search range of [140K, 144K),
956 * this should succeed and adjust the extent map to the range
957 * [128K, 144K), with a length of 16K and an offset of 20K.
959 * This simulates a scenario where in the subvolume tree of an inode we
960 * have a compressed file extent item for the range [108K, 144K) and we
961 * have an overlapping compressed extent map for the range [120K, 128K),
962 * which was created by an encoded write, but its ordered extent was not
963 * yet completed, so the subvolume tree doesn't have yet the file extent
964 * item for that range - we only have the extent map in the inode's
967 write_lock(&em_tree
->lock
);
968 ret
= btrfs_add_extent_mapping(inode
, &em
, SZ_1K
* 140, SZ_4K
);
969 write_unlock(&em_tree
->lock
);
972 test_err("couldn't add extent map for range [108K, 144K)");
976 if (em
->start
!= SZ_128K
) {
977 test_err("unexpected extent map start %llu (should be 128K)", em
->start
);
981 if (em
->len
!= SZ_16K
) {
982 test_err("unexpected extent map length %llu (should be 16K)", em
->len
);
986 if (em
->offset
!= SZ_1K
* 20) {
987 test_err("unexpected extent map offset %llu (should be 20K)", em
->offset
);
992 ret2
= free_extent_map_tree(inode
);
999 struct rmap_test_vector
{
1002 u64 data_stripe_size
;
1003 u64 num_data_stripes
;
1005 /* Assume we won't have more than 5 physical stripes */
1006 u64 data_stripe_phys_start
[5];
1007 bool expected_mapped_addr
;
1008 /* Physical to logical addresses */
1009 u64 mapped_logical
[5];
1012 static int test_rmap_block(struct btrfs_fs_info
*fs_info
,
1013 struct rmap_test_vector
*test
)
1015 struct btrfs_chunk_map
*map
;
1016 u64
*logical
= NULL
;
1017 int i
, out_ndaddrs
, out_stripe_len
;
1020 map
= btrfs_alloc_chunk_map(test
->num_stripes
, GFP_KERNEL
);
1022 test_std_err(TEST_ALLOC_CHUNK_MAP
);
1026 /* Start at 4GiB logical address */
1028 map
->chunk_len
= test
->data_stripe_size
* test
->num_data_stripes
;
1029 map
->stripe_size
= test
->data_stripe_size
;
1030 map
->num_stripes
= test
->num_stripes
;
1031 map
->type
= test
->raid_type
;
1033 for (i
= 0; i
< map
->num_stripes
; i
++) {
1034 struct btrfs_device
*dev
= btrfs_alloc_dummy_device(fs_info
);
1037 test_err("cannot allocate device");
1041 map
->stripes
[i
].dev
= dev
;
1042 map
->stripes
[i
].physical
= test
->data_stripe_phys_start
[i
];
1045 ret
= btrfs_add_chunk_map(fs_info
, map
);
1047 test_err("error adding chunk map to mapping tree");
1051 ret
= btrfs_rmap_block(fs_info
, map
->start
, btrfs_sb_offset(1),
1052 &logical
, &out_ndaddrs
, &out_stripe_len
);
1053 if (ret
|| (out_ndaddrs
== 0 && test
->expected_mapped_addr
)) {
1054 test_err("didn't rmap anything but expected %d",
1055 test
->expected_mapped_addr
);
1059 if (out_stripe_len
!= BTRFS_STRIPE_LEN
) {
1060 test_err("calculated stripe length doesn't match");
1064 if (out_ndaddrs
!= test
->expected_mapped_addr
) {
1065 for (i
= 0; i
< out_ndaddrs
; i
++)
1066 test_msg("mapped %llu", logical
[i
]);
1067 test_err("unexpected number of mapped addresses: %d", out_ndaddrs
);
1071 for (i
= 0; i
< out_ndaddrs
; i
++) {
1072 if (logical
[i
] != test
->mapped_logical
[i
]) {
1073 test_err("unexpected logical address mapped");
1080 btrfs_remove_chunk_map(fs_info
, map
);
1086 int btrfs_test_extent_map(void)
1088 struct btrfs_fs_info
*fs_info
= NULL
;
1089 struct inode
*inode
;
1090 struct btrfs_root
*root
= NULL
;
1092 struct rmap_test_vector rmap_tests
[] = {
1095 * Test a chunk with 2 data stripes one of which
1096 * intersects the physical address of the super block
1097 * is correctly recognised.
1099 .raid_type
= BTRFS_BLOCK_GROUP_RAID1
,
1100 .physical_start
= SZ_64M
- SZ_4M
,
1101 .data_stripe_size
= SZ_256M
,
1102 .num_data_stripes
= 2,
1104 .data_stripe_phys_start
=
1105 {SZ_64M
- SZ_4M
, SZ_64M
- SZ_4M
+ SZ_256M
},
1106 .expected_mapped_addr
= true,
1107 .mapped_logical
= {SZ_4G
+ SZ_4M
}
1111 * Test that out-of-range physical addresses are
1115 /* SINGLE chunk type */
1117 .physical_start
= SZ_4G
,
1118 .data_stripe_size
= SZ_256M
,
1119 .num_data_stripes
= 1,
1121 .data_stripe_phys_start
= {SZ_256M
},
1122 .expected_mapped_addr
= false,
1123 .mapped_logical
= {0}
1127 test_msg("running extent_map tests");
1130 * Note: the fs_info is not set up completely, we only need
1131 * fs_info::fsid for the tracepoint.
1133 fs_info
= btrfs_alloc_dummy_fs_info(PAGE_SIZE
, PAGE_SIZE
);
1135 test_std_err(TEST_ALLOC_FS_INFO
);
1139 inode
= btrfs_new_test_inode();
1141 test_std_err(TEST_ALLOC_INODE
);
1146 root
= btrfs_alloc_dummy_root(fs_info
);
1148 test_std_err(TEST_ALLOC_ROOT
);
1149 ret
= PTR_ERR(root
);
1154 BTRFS_I(inode
)->root
= root
;
1156 ret
= test_case_1(fs_info
, BTRFS_I(inode
));
1159 ret
= test_case_2(fs_info
, BTRFS_I(inode
));
1162 ret
= test_case_3(fs_info
, BTRFS_I(inode
));
1165 ret
= test_case_4(fs_info
, BTRFS_I(inode
));
1168 ret
= test_case_5(fs_info
, BTRFS_I(inode
));
1171 ret
= test_case_6(fs_info
, BTRFS_I(inode
));
1174 ret
= test_case_7(fs_info
, BTRFS_I(inode
));
1177 ret
= test_case_8(fs_info
, BTRFS_I(inode
));
1181 test_msg("running rmap tests");
1182 for (i
= 0; i
< ARRAY_SIZE(rmap_tests
); i
++) {
1183 ret
= test_rmap_block(fs_info
, &rmap_tests
[i
]);
1190 btrfs_free_dummy_root(root
);
1191 btrfs_free_dummy_fs_info(fs_info
);