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"
10 static void free_extent_map_tree(struct extent_map_tree *em_tree)
12 struct extent_map *em;
15 while (!RB_EMPTY_ROOT(&em_tree->map)) {
16 node = rb_first(&em_tree->map);
17 em = rb_entry(node, struct extent_map, rb_node);
18 remove_extent_mapping(em_tree, em);
20 #ifdef CONFIG_BTRFS_DEBUG
21 if (refcount_read(&em->refs) != 1) {
23 "em leak: em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx) refs %d\n",
24 em->start, em->len, em->block_start,
25 em->block_len, refcount_read(&em->refs));
27 refcount_set(&em->refs, 1);
37 * Suppose that no extent map has been loaded into memory yet, there is a file
38 * extent [0, 16K), followed by another file extent [16K, 20K), two dio reads
39 * are entering btrfs_get_extent() concurrently, t1 is reading [8K, 16K), t2 is
43 * btrfs_get_extent() btrfs_get_extent()
44 * -> lookup_extent_mapping() ->lookup_extent_mapping()
45 * -> add_extent_mapping(0, 16K)
47 * ->add_extent_mapping(0, 16K)
50 static void test_case_1(struct extent_map_tree *em_tree)
52 struct extent_map *em;
57 em = alloc_extent_map();
59 /* Skip the test on error. */
66 em->block_len = SZ_16K;
67 ret = add_extent_mapping(em_tree, em, 0);
71 /* Add [16K, 20K) following [0, 16K) */
72 em = alloc_extent_map();
78 em->block_start = SZ_32K; /* avoid merging */
79 em->block_len = SZ_4K;
80 ret = add_extent_mapping(em_tree, em, 0);
84 em = alloc_extent_map();
88 /* Add [0, 8K), should return [0, 16K) instead. */
91 em->block_start = start;
93 ret = btrfs_add_extent_mapping(em_tree, &em, em->start, em->len);
95 test_msg("case1 [%llu %llu]: ret %d\n", start, start + len, ret);
97 (em->start != 0 || extent_map_end(em) != SZ_16K ||
98 em->block_start != 0 || em->block_len != SZ_16K))
100 "case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu\n",
101 start, start + len, ret, em->start, em->len,
102 em->block_start, em->block_len);
106 free_extent_map_tree(em_tree);
112 * Reading the inline ending up with EEXIST, ie. read an inline
113 * extent and discard page cache and read it again.
115 static void test_case_2(struct extent_map_tree *em_tree)
117 struct extent_map *em;
120 em = alloc_extent_map();
122 /* Skip the test on error. */
128 em->block_start = EXTENT_MAP_INLINE;
129 em->block_len = (u64)-1;
130 ret = add_extent_mapping(em_tree, em, 0);
134 /* Add [4K, 4K) following [0, 1K) */
135 em = alloc_extent_map();
141 em->block_start = SZ_4K;
142 em->block_len = SZ_4K;
143 ret = add_extent_mapping(em_tree, em, 0);
147 em = alloc_extent_map();
154 em->block_start = EXTENT_MAP_INLINE;
155 em->block_len = (u64)-1;
156 ret = btrfs_add_extent_mapping(em_tree, &em, em->start, em->len);
158 test_msg("case2 [0 1K]: ret %d\n", ret);
160 (em->start != 0 || extent_map_end(em) != SZ_1K ||
161 em->block_start != EXTENT_MAP_INLINE || em->block_len != (u64)-1))
163 "case2 [0 1K]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu\n",
164 ret, em->start, em->len, em->block_start,
169 free_extent_map_tree(em_tree);
172 static void __test_case_3(struct extent_map_tree *em_tree, u64 start)
174 struct extent_map *em;
178 em = alloc_extent_map();
180 /* Skip this test on error. */
186 em->block_start = SZ_4K;
187 em->block_len = SZ_4K;
188 ret = add_extent_mapping(em_tree, em, 0);
192 em = alloc_extent_map();
200 em->block_len = SZ_16K;
201 ret = btrfs_add_extent_mapping(em_tree, &em, start, len);
203 test_msg("case3 [0x%llx 0x%llx): ret %d\n",
204 start, start + len, ret);
206 * Since bytes within em are contiguous, em->block_start is identical to
210 (start < em->start || start + len > extent_map_end(em) ||
211 em->start != em->block_start || em->len != em->block_len))
213 "case3 [0x%llx 0x%llx): ret %d em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)\n",
214 start, start + len, ret, em->start, em->len,
215 em->block_start, em->block_len);
219 free_extent_map_tree(em_tree);
225 * Suppose that no extent map has been loaded into memory yet.
226 * There is a file extent [0, 16K), two jobs are running concurrently
227 * against it, t1 is buffered writing to [4K, 8K) and t2 is doing dio
228 * read from [0, 4K) or [8K, 12K) or [12K, 16K).
230 * t1 goes ahead of t2 and adds em [4K, 8K) into tree.
233 * cow_file_range() btrfs_get_extent()
234 * -> lookup_extent_mapping()
235 * -> add_extent_mapping()
236 * -> add_extent_mapping()
238 static void test_case_3(struct extent_map_tree *em_tree)
240 __test_case_3(em_tree, 0);
241 __test_case_3(em_tree, SZ_8K);
242 __test_case_3(em_tree, (12 * 1024ULL));
245 static void __test_case_4(struct extent_map_tree *em_tree, u64 start)
247 struct extent_map *em;
251 em = alloc_extent_map();
253 /* Skip this test on error. */
260 em->block_len = SZ_8K;
261 ret = add_extent_mapping(em_tree, em, 0);
265 em = alloc_extent_map();
271 em->len = 24 * 1024ULL;
272 em->block_start = SZ_16K; /* avoid merging */
273 em->block_len = 24 * 1024ULL;
274 ret = add_extent_mapping(em_tree, em, 0);
278 em = alloc_extent_map();
285 em->block_len = SZ_32K;
286 ret = btrfs_add_extent_mapping(em_tree, &em, start, len);
288 test_msg("case4 [0x%llx 0x%llx): ret %d\n",
291 (start < em->start || start + len > extent_map_end(em)))
293 "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",
294 start, len, ret, em->start, em->len, em->block_start,
299 free_extent_map_tree(em_tree);
305 * Suppose that no extent map has been loaded into memory yet.
306 * There is a file extent [0, 32K), two jobs are running concurrently
307 * against it, t1 is doing dio write to [8K, 32K) and t2 is doing dio
308 * read from [0, 4K) or [4K, 8K).
310 * t1 goes ahead of t2 and splits em [0, 32K) to em [0K, 8K) and [8K 32K).
313 * btrfs_get_blocks_direct() btrfs_get_blocks_direct()
314 * -> btrfs_get_extent() -> btrfs_get_extent()
315 * -> lookup_extent_mapping()
316 * -> add_extent_mapping() -> lookup_extent_mapping()
318 * -> btrfs_new_extent_direct()
319 * -> btrfs_drop_extent_cache()
321 * -> add_extent_mapping()
323 * -> add_extent_mapping()
324 * # handle -EEXIST when adding
327 static void test_case_4(struct extent_map_tree *em_tree)
329 __test_case_4(em_tree, 0);
330 __test_case_4(em_tree, SZ_4K);
333 int btrfs_test_extent_map(void)
335 struct extent_map_tree *em_tree;
337 test_msg("Running extent_map tests\n");
339 em_tree = kzalloc(sizeof(*em_tree), GFP_KERNEL);
341 /* Skip the test on error. */
344 extent_map_tree_init(em_tree);
346 test_case_1(em_tree);
347 test_case_2(em_tree);
348 test_case_3(em_tree);
349 test_case_4(em_tree);