2 * Copyright (C) 2017 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/types.h>
20 #include "btrfs-tests.h"
23 static void free_extent_map_tree(struct extent_map_tree
*em_tree
)
25 struct extent_map
*em
;
28 while (!RB_EMPTY_ROOT(&em_tree
->map
)) {
29 node
= rb_first(&em_tree
->map
);
30 em
= rb_entry(node
, struct extent_map
, rb_node
);
31 remove_extent_mapping(em_tree
, em
);
33 #ifdef CONFIG_BTRFS_DEBUG
34 if (refcount_read(&em
->refs
) != 1) {
36 "em leak: em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx) refs %d\n",
37 em
->start
, em
->len
, em
->block_start
,
38 em
->block_len
, refcount_read(&em
->refs
));
40 refcount_set(&em
->refs
, 1);
50 * Suppose that no extent map has been loaded into memory yet, there is a file
51 * extent [0, 16K), followed by another file extent [16K, 20K), two dio reads
52 * are entering btrfs_get_extent() concurrently, t1 is reading [8K, 16K), t2 is
56 * btrfs_get_extent() btrfs_get_extent()
57 * -> lookup_extent_mapping() ->lookup_extent_mapping()
58 * -> add_extent_mapping(0, 16K)
60 * ->add_extent_mapping(0, 16K)
63 static void test_case_1(struct extent_map_tree
*em_tree
)
65 struct extent_map
*em
;
70 em
= alloc_extent_map();
72 /* Skip the test on error. */
79 em
->block_len
= SZ_16K
;
80 ret
= add_extent_mapping(em_tree
, em
, 0);
84 /* Add [16K, 20K) following [0, 16K) */
85 em
= alloc_extent_map();
91 em
->block_start
= SZ_32K
; /* avoid merging */
92 em
->block_len
= SZ_4K
;
93 ret
= add_extent_mapping(em_tree
, em
, 0);
97 em
= alloc_extent_map();
101 /* Add [0, 8K), should return [0, 16K) instead. */
104 em
->block_start
= start
;
106 ret
= btrfs_add_extent_mapping(em_tree
, &em
, em
->start
, em
->len
);
108 test_msg("case1 [%llu %llu]: ret %d\n", start
, start
+ len
, ret
);
110 (em
->start
!= 0 || extent_map_end(em
) != SZ_16K
||
111 em
->block_start
!= 0 || em
->block_len
!= SZ_16K
))
113 "case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu\n",
114 start
, start
+ len
, ret
, em
->start
, em
->len
,
115 em
->block_start
, em
->block_len
);
119 free_extent_map_tree(em_tree
);
125 * Reading the inline ending up with EEXIST, ie. read an inline
126 * extent and discard page cache and read it again.
128 static void test_case_2(struct extent_map_tree
*em_tree
)
130 struct extent_map
*em
;
133 em
= alloc_extent_map();
135 /* Skip the test on error. */
141 em
->block_start
= EXTENT_MAP_INLINE
;
142 em
->block_len
= (u64
)-1;
143 ret
= add_extent_mapping(em_tree
, em
, 0);
147 /* Add [4K, 4K) following [0, 1K) */
148 em
= alloc_extent_map();
154 em
->block_start
= SZ_4K
;
155 em
->block_len
= SZ_4K
;
156 ret
= add_extent_mapping(em_tree
, em
, 0);
160 em
= alloc_extent_map();
167 em
->block_start
= EXTENT_MAP_INLINE
;
168 em
->block_len
= (u64
)-1;
169 ret
= btrfs_add_extent_mapping(em_tree
, &em
, em
->start
, em
->len
);
171 test_msg("case2 [0 1K]: ret %d\n", ret
);
173 (em
->start
!= 0 || extent_map_end(em
) != SZ_1K
||
174 em
->block_start
!= EXTENT_MAP_INLINE
|| em
->block_len
!= (u64
)-1))
176 "case2 [0 1K]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu\n",
177 ret
, em
->start
, em
->len
, em
->block_start
,
182 free_extent_map_tree(em_tree
);
185 static void __test_case_3(struct extent_map_tree
*em_tree
, u64 start
)
187 struct extent_map
*em
;
191 em
= alloc_extent_map();
193 /* Skip this test on error. */
199 em
->block_start
= SZ_4K
;
200 em
->block_len
= SZ_4K
;
201 ret
= add_extent_mapping(em_tree
, em
, 0);
205 em
= alloc_extent_map();
213 em
->block_len
= SZ_16K
;
214 ret
= btrfs_add_extent_mapping(em_tree
, &em
, start
, len
);
216 test_msg("case3 [0x%llx 0x%llx): ret %d\n",
217 start
, start
+ len
, ret
);
219 * Since bytes within em are contiguous, em->block_start is identical to
223 (start
< em
->start
|| start
+ len
> extent_map_end(em
) ||
224 em
->start
!= em
->block_start
|| em
->len
!= em
->block_len
))
226 "case3 [0x%llx 0x%llx): ret %d em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)\n",
227 start
, start
+ len
, ret
, em
->start
, em
->len
,
228 em
->block_start
, em
->block_len
);
232 free_extent_map_tree(em_tree
);
238 * Suppose that no extent map has been loaded into memory yet.
239 * There is a file extent [0, 16K), two jobs are running concurrently
240 * against it, t1 is buffered writing to [4K, 8K) and t2 is doing dio
241 * read from [0, 4K) or [8K, 12K) or [12K, 16K).
243 * t1 goes ahead of t2 and adds em [4K, 8K) into tree.
246 * cow_file_range() btrfs_get_extent()
247 * -> lookup_extent_mapping()
248 * -> add_extent_mapping()
249 * -> add_extent_mapping()
251 static void test_case_3(struct extent_map_tree
*em_tree
)
253 __test_case_3(em_tree
, 0);
254 __test_case_3(em_tree
, SZ_8K
);
255 __test_case_3(em_tree
, (12 * 1024ULL));
258 static void __test_case_4(struct extent_map_tree
*em_tree
, u64 start
)
260 struct extent_map
*em
;
264 em
= alloc_extent_map();
266 /* Skip this test on error. */
273 em
->block_len
= SZ_8K
;
274 ret
= add_extent_mapping(em_tree
, em
, 0);
278 em
= alloc_extent_map();
284 em
->len
= 24 * 1024ULL;
285 em
->block_start
= SZ_16K
; /* avoid merging */
286 em
->block_len
= 24 * 1024ULL;
287 ret
= add_extent_mapping(em_tree
, em
, 0);
291 em
= alloc_extent_map();
298 em
->block_len
= SZ_32K
;
299 ret
= btrfs_add_extent_mapping(em_tree
, &em
, start
, len
);
301 test_msg("case4 [0x%llx 0x%llx): ret %d\n",
304 (start
< em
->start
|| start
+ len
> extent_map_end(em
)))
306 "case4 [0x%llx 0x%llx): ret %d, added wrong em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)\n",
307 start
, len
, ret
, em
->start
, em
->len
, em
->block_start
,
312 free_extent_map_tree(em_tree
);
318 * Suppose that no extent map has been loaded into memory yet.
319 * There is a file extent [0, 32K), two jobs are running concurrently
320 * against it, t1 is doing dio write to [8K, 32K) and t2 is doing dio
321 * read from [0, 4K) or [4K, 8K).
323 * t1 goes ahead of t2 and splits em [0, 32K) to em [0K, 8K) and [8K 32K).
326 * btrfs_get_blocks_direct() btrfs_get_blocks_direct()
327 * -> btrfs_get_extent() -> btrfs_get_extent()
328 * -> lookup_extent_mapping()
329 * -> add_extent_mapping() -> lookup_extent_mapping()
331 * -> btrfs_new_extent_direct()
332 * -> btrfs_drop_extent_cache()
334 * -> add_extent_mapping()
336 * -> add_extent_mapping()
337 * # handle -EEXIST when adding
340 static void test_case_4(struct extent_map_tree
*em_tree
)
342 __test_case_4(em_tree
, 0);
343 __test_case_4(em_tree
, SZ_4K
);
346 int btrfs_test_extent_map()
348 struct extent_map_tree
*em_tree
;
350 test_msg("Running extent_map tests\n");
352 em_tree
= kzalloc(sizeof(*em_tree
), GFP_KERNEL
);
354 /* Skip the test on error. */
357 extent_map_tree_init(em_tree
);
359 test_case_1(em_tree
);
360 test_case_2(em_tree
);
361 test_case_3(em_tree
);
362 test_case_4(em_tree
);