xref: /openbmc/linux/fs/btrfs/tests/extent-map-tests.c (revision ccfada1f650b636733567e1aa059a46424fda926)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
272b28077SLiu Bo /*
372b28077SLiu Bo  * Copyright (C) 2017 Oracle.  All rights reserved.
472b28077SLiu Bo  */
572b28077SLiu Bo 
672b28077SLiu Bo #include <linux/types.h>
772b28077SLiu Bo #include "btrfs-tests.h"
872b28077SLiu Bo #include "../ctree.h"
972b28077SLiu Bo 
1072b28077SLiu Bo static void free_extent_map_tree(struct extent_map_tree *em_tree)
1172b28077SLiu Bo {
1272b28077SLiu Bo 	struct extent_map *em;
1372b28077SLiu Bo 	struct rb_node *node;
1472b28077SLiu Bo 
1507e1ce09SLiu Bo 	while (!RB_EMPTY_ROOT(&em_tree->map.rb_root)) {
1607e1ce09SLiu Bo 		node = rb_first_cached(&em_tree->map);
1772b28077SLiu Bo 		em = rb_entry(node, struct extent_map, rb_node);
1872b28077SLiu Bo 		remove_extent_mapping(em_tree, em);
1972b28077SLiu Bo 
2072b28077SLiu Bo #ifdef CONFIG_BTRFS_DEBUG
2172b28077SLiu Bo 		if (refcount_read(&em->refs) != 1) {
223c7251f2SDavid Sterba 			test_err(
233c7251f2SDavid Sterba "em leak: em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx) refs %d",
2472b28077SLiu Bo 				 em->start, em->len, em->block_start,
2572b28077SLiu Bo 				 em->block_len, refcount_read(&em->refs));
2672b28077SLiu Bo 
2772b28077SLiu Bo 			refcount_set(&em->refs, 1);
2872b28077SLiu Bo 		}
2972b28077SLiu Bo #endif
3072b28077SLiu Bo 		free_extent_map(em);
3172b28077SLiu Bo 	}
3272b28077SLiu Bo }
3372b28077SLiu Bo 
3472b28077SLiu Bo /*
3572b28077SLiu Bo  * Test scenario:
3672b28077SLiu Bo  *
3772b28077SLiu Bo  * Suppose that no extent map has been loaded into memory yet, there is a file
3872b28077SLiu Bo  * extent [0, 16K), followed by another file extent [16K, 20K), two dio reads
3972b28077SLiu Bo  * are entering btrfs_get_extent() concurrently, t1 is reading [8K, 16K), t2 is
4072b28077SLiu Bo  * reading [0, 8K)
4172b28077SLiu Bo  *
4272b28077SLiu Bo  *     t1                            t2
4372b28077SLiu Bo  *  btrfs_get_extent()              btrfs_get_extent()
4472b28077SLiu Bo  *    -> lookup_extent_mapping()      ->lookup_extent_mapping()
4572b28077SLiu Bo  *    -> add_extent_mapping(0, 16K)
4672b28077SLiu Bo  *    -> return em
4772b28077SLiu Bo  *                                    ->add_extent_mapping(0, 16K)
4872b28077SLiu Bo  *                                    -> #handle -EEXIST
4972b28077SLiu Bo  */
50488f6730SDavid Sterba static int test_case_1(struct btrfs_fs_info *fs_info,
510e08eb9bSDavid Sterba 		struct extent_map_tree *em_tree)
5272b28077SLiu Bo {
5372b28077SLiu Bo 	struct extent_map *em;
5472b28077SLiu Bo 	u64 start = 0;
5572b28077SLiu Bo 	u64 len = SZ_8K;
5672b28077SLiu Bo 	int ret;
5772b28077SLiu Bo 
5872b28077SLiu Bo 	em = alloc_extent_map();
5972b28077SLiu Bo 	if (!em)
60488f6730SDavid Sterba 		return -ENOMEM;
6172b28077SLiu Bo 
6272b28077SLiu Bo 	/* Add [0, 16K) */
6372b28077SLiu Bo 	em->start = 0;
6472b28077SLiu Bo 	em->len = SZ_16K;
6572b28077SLiu Bo 	em->block_start = 0;
6672b28077SLiu Bo 	em->block_len = SZ_16K;
6772b28077SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
68d7de4b08SDavid Sterba 	if (ret < 0) {
69d7de4b08SDavid Sterba 		test_err("cannot add extent range [0, 16K)");
70d7de4b08SDavid Sterba 		goto out;
71d7de4b08SDavid Sterba 	}
7272b28077SLiu Bo 	free_extent_map(em);
7372b28077SLiu Bo 
7472b28077SLiu Bo 	/* Add [16K, 20K) following [0, 16K)  */
7572b28077SLiu Bo 	em = alloc_extent_map();
76488f6730SDavid Sterba 	if (!em) {
77488f6730SDavid Sterba 		ret = -ENOMEM;
7872b28077SLiu Bo 		goto out;
79488f6730SDavid Sterba 	}
8072b28077SLiu Bo 
8172b28077SLiu Bo 	em->start = SZ_16K;
8272b28077SLiu Bo 	em->len = SZ_4K;
8372b28077SLiu Bo 	em->block_start = SZ_32K; /* avoid merging */
8472b28077SLiu Bo 	em->block_len = SZ_4K;
8572b28077SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
86d7de4b08SDavid Sterba 	if (ret < 0) {
87d7de4b08SDavid Sterba 		test_err("cannot add extent range [16K, 20K)");
88d7de4b08SDavid Sterba 		goto out;
89d7de4b08SDavid Sterba 	}
9072b28077SLiu Bo 	free_extent_map(em);
9172b28077SLiu Bo 
9272b28077SLiu Bo 	em = alloc_extent_map();
93488f6730SDavid Sterba 	if (!em) {
94488f6730SDavid Sterba 		ret = -ENOMEM;
9572b28077SLiu Bo 		goto out;
96488f6730SDavid Sterba 	}
9772b28077SLiu Bo 
9872b28077SLiu Bo 	/* Add [0, 8K), should return [0, 16K) instead. */
9972b28077SLiu Bo 	em->start = start;
10072b28077SLiu Bo 	em->len = len;
10172b28077SLiu Bo 	em->block_start = start;
10272b28077SLiu Bo 	em->block_len = len;
103f46b24c9SDavid Sterba 	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
104d7de4b08SDavid Sterba 	if (ret) {
1053c7251f2SDavid Sterba 		test_err("case1 [%llu %llu]: ret %d", start, start + len, ret);
106d7de4b08SDavid Sterba 		goto out;
107d7de4b08SDavid Sterba 	}
10872b28077SLiu Bo 	if (em &&
10972b28077SLiu Bo 	    (em->start != 0 || extent_map_end(em) != SZ_16K ||
110d7de4b08SDavid Sterba 	     em->block_start != 0 || em->block_len != SZ_16K)) {
1113c7251f2SDavid Sterba 		test_err(
1123c7251f2SDavid Sterba "case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
11372b28077SLiu Bo 			 start, start + len, ret, em->start, em->len,
11472b28077SLiu Bo 			 em->block_start, em->block_len);
115d7de4b08SDavid Sterba 		ret = -EINVAL;
116d7de4b08SDavid Sterba 	}
11772b28077SLiu Bo 	free_extent_map(em);
11872b28077SLiu Bo out:
11972b28077SLiu Bo 	free_extent_map_tree(em_tree);
120488f6730SDavid Sterba 
121488f6730SDavid Sterba 	return ret;
12272b28077SLiu Bo }
12372b28077SLiu Bo 
12472b28077SLiu Bo /*
12572b28077SLiu Bo  * Test scenario:
12672b28077SLiu Bo  *
12772b28077SLiu Bo  * Reading the inline ending up with EEXIST, ie. read an inline
12872b28077SLiu Bo  * extent and discard page cache and read it again.
12972b28077SLiu Bo  */
130488f6730SDavid Sterba static int test_case_2(struct btrfs_fs_info *fs_info,
1310e08eb9bSDavid Sterba 		struct extent_map_tree *em_tree)
13272b28077SLiu Bo {
13372b28077SLiu Bo 	struct extent_map *em;
13472b28077SLiu Bo 	int ret;
13572b28077SLiu Bo 
13672b28077SLiu Bo 	em = alloc_extent_map();
13772b28077SLiu Bo 	if (!em)
138488f6730SDavid Sterba 		return -ENOMEM;
13972b28077SLiu Bo 
14072b28077SLiu Bo 	/* Add [0, 1K) */
14172b28077SLiu Bo 	em->start = 0;
14272b28077SLiu Bo 	em->len = SZ_1K;
14372b28077SLiu Bo 	em->block_start = EXTENT_MAP_INLINE;
14472b28077SLiu Bo 	em->block_len = (u64)-1;
14572b28077SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
146e71f2e17SDavid Sterba 	if (ret < 0) {
147e71f2e17SDavid Sterba 		test_err("cannot add extent range [0, 1K)");
148e71f2e17SDavid Sterba 		goto out;
149e71f2e17SDavid Sterba 	}
15072b28077SLiu Bo 	free_extent_map(em);
15172b28077SLiu Bo 
15272b28077SLiu Bo 	/* Add [4K, 4K) following [0, 1K)  */
15372b28077SLiu Bo 	em = alloc_extent_map();
154488f6730SDavid Sterba 	if (!em) {
155488f6730SDavid Sterba 		ret = -ENOMEM;
15672b28077SLiu Bo 		goto out;
157488f6730SDavid Sterba 	}
15872b28077SLiu Bo 
15972b28077SLiu Bo 	em->start = SZ_4K;
16072b28077SLiu Bo 	em->len = SZ_4K;
16172b28077SLiu Bo 	em->block_start = SZ_4K;
16272b28077SLiu Bo 	em->block_len = SZ_4K;
16372b28077SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
164e71f2e17SDavid Sterba 	if (ret < 0) {
165e71f2e17SDavid Sterba 		test_err("cannot add extent range [4K, 8K)");
166e71f2e17SDavid Sterba 		goto out;
167e71f2e17SDavid Sterba 	}
16872b28077SLiu Bo 	free_extent_map(em);
16972b28077SLiu Bo 
17072b28077SLiu Bo 	em = alloc_extent_map();
171488f6730SDavid Sterba 	if (!em) {
172488f6730SDavid Sterba 		ret = -ENOMEM;
17372b28077SLiu Bo 		goto out;
174488f6730SDavid Sterba 	}
17572b28077SLiu Bo 
17672b28077SLiu Bo 	/* Add [0, 1K) */
17772b28077SLiu Bo 	em->start = 0;
17872b28077SLiu Bo 	em->len = SZ_1K;
17972b28077SLiu Bo 	em->block_start = EXTENT_MAP_INLINE;
18072b28077SLiu Bo 	em->block_len = (u64)-1;
181f46b24c9SDavid Sterba 	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
182e71f2e17SDavid Sterba 	if (ret) {
1833c7251f2SDavid Sterba 		test_err("case2 [0 1K]: ret %d", ret);
184e71f2e17SDavid Sterba 		goto out;
185e71f2e17SDavid Sterba 	}
18672b28077SLiu Bo 	if (em &&
18772b28077SLiu Bo 	    (em->start != 0 || extent_map_end(em) != SZ_1K ||
188e71f2e17SDavid Sterba 	     em->block_start != EXTENT_MAP_INLINE || em->block_len != (u64)-1)) {
1893c7251f2SDavid Sterba 		test_err(
1903c7251f2SDavid Sterba "case2 [0 1K]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
19172b28077SLiu Bo 			 ret, em->start, em->len, em->block_start,
19272b28077SLiu Bo 			 em->block_len);
193e71f2e17SDavid Sterba 		ret = -EINVAL;
194e71f2e17SDavid Sterba 	}
19572b28077SLiu Bo 	free_extent_map(em);
19672b28077SLiu Bo out:
19772b28077SLiu Bo 	free_extent_map_tree(em_tree);
198488f6730SDavid Sterba 
199488f6730SDavid Sterba 	return ret;
20072b28077SLiu Bo }
20172b28077SLiu Bo 
202488f6730SDavid Sterba static int __test_case_3(struct btrfs_fs_info *fs_info,
2030e08eb9bSDavid Sterba 		struct extent_map_tree *em_tree, u64 start)
204fd87526fSLiu Bo {
205fd87526fSLiu Bo 	struct extent_map *em;
206fd87526fSLiu Bo 	u64 len = SZ_4K;
207fd87526fSLiu Bo 	int ret;
208fd87526fSLiu Bo 
209fd87526fSLiu Bo 	em = alloc_extent_map();
210fd87526fSLiu Bo 	if (!em)
211488f6730SDavid Sterba 		return -ENOMEM;
212fd87526fSLiu Bo 
213fd87526fSLiu Bo 	/* Add [4K, 8K) */
214fd87526fSLiu Bo 	em->start = SZ_4K;
215fd87526fSLiu Bo 	em->len = SZ_4K;
216fd87526fSLiu Bo 	em->block_start = SZ_4K;
217fd87526fSLiu Bo 	em->block_len = SZ_4K;
218fd87526fSLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
219992dce74SDavid Sterba 	if (ret < 0) {
220992dce74SDavid Sterba 		test_err("cannot add extent range [4K, 8K)");
221992dce74SDavid Sterba 		goto out;
222992dce74SDavid Sterba 	}
223fd87526fSLiu Bo 	free_extent_map(em);
224fd87526fSLiu Bo 
225fd87526fSLiu Bo 	em = alloc_extent_map();
226488f6730SDavid Sterba 	if (!em) {
227488f6730SDavid Sterba 		ret = -ENOMEM;
228fd87526fSLiu Bo 		goto out;
229488f6730SDavid Sterba 	}
230fd87526fSLiu Bo 
231fd87526fSLiu Bo 	/* Add [0, 16K) */
232fd87526fSLiu Bo 	em->start = 0;
233fd87526fSLiu Bo 	em->len = SZ_16K;
234fd87526fSLiu Bo 	em->block_start = 0;
235fd87526fSLiu Bo 	em->block_len = SZ_16K;
236f46b24c9SDavid Sterba 	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
237992dce74SDavid Sterba 	if (ret) {
2383c7251f2SDavid Sterba 		test_err("case3 [0x%llx 0x%llx): ret %d",
239fd87526fSLiu Bo 			 start, start + len, ret);
240992dce74SDavid Sterba 		goto out;
241992dce74SDavid Sterba 	}
242fd87526fSLiu Bo 	/*
243fd87526fSLiu Bo 	 * Since bytes within em are contiguous, em->block_start is identical to
244fd87526fSLiu Bo 	 * em->start.
245fd87526fSLiu Bo 	 */
246fd87526fSLiu Bo 	if (em &&
247fd87526fSLiu Bo 	    (start < em->start || start + len > extent_map_end(em) ||
248992dce74SDavid Sterba 	     em->start != em->block_start || em->len != em->block_len)) {
2493c7251f2SDavid Sterba 		test_err(
2503c7251f2SDavid Sterba "case3 [0x%llx 0x%llx): ret %d em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)",
251fd87526fSLiu Bo 			 start, start + len, ret, em->start, em->len,
252fd87526fSLiu Bo 			 em->block_start, em->block_len);
253992dce74SDavid Sterba 		ret = -EINVAL;
254992dce74SDavid Sterba 	}
255fd87526fSLiu Bo 	free_extent_map(em);
256fd87526fSLiu Bo out:
257fd87526fSLiu Bo 	free_extent_map_tree(em_tree);
258488f6730SDavid Sterba 
259488f6730SDavid Sterba 	return ret;
260fd87526fSLiu Bo }
261fd87526fSLiu Bo 
262fd87526fSLiu Bo /*
263fd87526fSLiu Bo  * Test scenario:
264fd87526fSLiu Bo  *
265fd87526fSLiu Bo  * Suppose that no extent map has been loaded into memory yet.
266fd87526fSLiu Bo  * There is a file extent [0, 16K), two jobs are running concurrently
267fd87526fSLiu Bo  * against it, t1 is buffered writing to [4K, 8K) and t2 is doing dio
268fd87526fSLiu Bo  * read from [0, 4K) or [8K, 12K) or [12K, 16K).
269fd87526fSLiu Bo  *
270fd87526fSLiu Bo  * t1 goes ahead of t2 and adds em [4K, 8K) into tree.
271fd87526fSLiu Bo  *
272fd87526fSLiu Bo  *         t1                       t2
273fd87526fSLiu Bo  *  cow_file_range()	     btrfs_get_extent()
274fd87526fSLiu Bo  *                            -> lookup_extent_mapping()
275fd87526fSLiu Bo  *   -> add_extent_mapping()
276fd87526fSLiu Bo  *                            -> add_extent_mapping()
277fd87526fSLiu Bo  */
278*ccfada1fSDavid Sterba static int test_case_3(struct btrfs_fs_info *fs_info,
2790e08eb9bSDavid Sterba 		struct extent_map_tree *em_tree)
280fd87526fSLiu Bo {
281*ccfada1fSDavid Sterba 	int ret;
282*ccfada1fSDavid Sterba 
283*ccfada1fSDavid Sterba 	ret = __test_case_3(fs_info, em_tree, 0);
284*ccfada1fSDavid Sterba 	if (ret)
285*ccfada1fSDavid Sterba 		return ret;
286*ccfada1fSDavid Sterba 	ret = __test_case_3(fs_info, em_tree, SZ_8K);
287*ccfada1fSDavid Sterba 	if (ret)
288*ccfada1fSDavid Sterba 		return ret;
289*ccfada1fSDavid Sterba 	ret = __test_case_3(fs_info, em_tree, (12 * 1024ULL));
290*ccfada1fSDavid Sterba 
291*ccfada1fSDavid Sterba 	return ret;
292fd87526fSLiu Bo }
293fd87526fSLiu Bo 
294488f6730SDavid Sterba static int __test_case_4(struct btrfs_fs_info *fs_info,
2950e08eb9bSDavid Sterba 		struct extent_map_tree *em_tree, u64 start)
296cd77f4f8SLiu Bo {
297cd77f4f8SLiu Bo 	struct extent_map *em;
298cd77f4f8SLiu Bo 	u64 len = SZ_4K;
299cd77f4f8SLiu Bo 	int ret;
300cd77f4f8SLiu Bo 
301cd77f4f8SLiu Bo 	em = alloc_extent_map();
302cd77f4f8SLiu Bo 	if (!em)
303488f6730SDavid Sterba 		return -ENOMEM;
304cd77f4f8SLiu Bo 
305cd77f4f8SLiu Bo 	/* Add [0K, 8K) */
306cd77f4f8SLiu Bo 	em->start = 0;
307cd77f4f8SLiu Bo 	em->len = SZ_8K;
308cd77f4f8SLiu Bo 	em->block_start = 0;
309cd77f4f8SLiu Bo 	em->block_len = SZ_8K;
310cd77f4f8SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
3117c6f6700SDavid Sterba 	if (ret < 0) {
3127c6f6700SDavid Sterba 		test_err("cannot add extent range [0, 8K)");
3137c6f6700SDavid Sterba 		goto out;
3147c6f6700SDavid Sterba 	}
315cd77f4f8SLiu Bo 	free_extent_map(em);
316cd77f4f8SLiu Bo 
317cd77f4f8SLiu Bo 	em = alloc_extent_map();
318488f6730SDavid Sterba 	if (!em) {
319488f6730SDavid Sterba 		ret = -ENOMEM;
320cd77f4f8SLiu Bo 		goto out;
321488f6730SDavid Sterba 	}
322cd77f4f8SLiu Bo 
323cd77f4f8SLiu Bo 	/* Add [8K, 24K) */
324cd77f4f8SLiu Bo 	em->start = SZ_8K;
325cd77f4f8SLiu Bo 	em->len = 24 * 1024ULL;
326cd77f4f8SLiu Bo 	em->block_start = SZ_16K; /* avoid merging */
327cd77f4f8SLiu Bo 	em->block_len = 24 * 1024ULL;
328cd77f4f8SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
3297c6f6700SDavid Sterba 	if (ret < 0) {
3307c6f6700SDavid Sterba 		test_err("cannot add extent range [8K, 32K)");
3317c6f6700SDavid Sterba 		goto out;
3327c6f6700SDavid Sterba 	}
333cd77f4f8SLiu Bo 	free_extent_map(em);
334cd77f4f8SLiu Bo 
335cd77f4f8SLiu Bo 	em = alloc_extent_map();
336488f6730SDavid Sterba 	if (!em) {
337488f6730SDavid Sterba 		ret = -ENOMEM;
338cd77f4f8SLiu Bo 		goto out;
339488f6730SDavid Sterba 	}
340cd77f4f8SLiu Bo 	/* Add [0K, 32K) */
341cd77f4f8SLiu Bo 	em->start = 0;
342cd77f4f8SLiu Bo 	em->len = SZ_32K;
343cd77f4f8SLiu Bo 	em->block_start = 0;
344cd77f4f8SLiu Bo 	em->block_len = SZ_32K;
345f46b24c9SDavid Sterba 	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
3467c6f6700SDavid Sterba 	if (ret) {
3473c7251f2SDavid Sterba 		test_err("case4 [0x%llx 0x%llx): ret %d",
348cd77f4f8SLiu Bo 			 start, len, ret);
3497c6f6700SDavid Sterba 		goto out;
3507c6f6700SDavid Sterba 	}
3517c6f6700SDavid Sterba 	if (em && (start < em->start || start + len > extent_map_end(em))) {
3523c7251f2SDavid Sterba 		test_err(
3533c7251f2SDavid Sterba "case4 [0x%llx 0x%llx): ret %d, added wrong em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)",
354cd77f4f8SLiu Bo 			 start, len, ret, em->start, em->len, em->block_start,
355cd77f4f8SLiu Bo 			 em->block_len);
3567c6f6700SDavid Sterba 		ret = -EINVAL;
3577c6f6700SDavid Sterba 	}
358cd77f4f8SLiu Bo 	free_extent_map(em);
359cd77f4f8SLiu Bo out:
360cd77f4f8SLiu Bo 	free_extent_map_tree(em_tree);
361488f6730SDavid Sterba 
362488f6730SDavid Sterba 	return ret;
363cd77f4f8SLiu Bo }
364cd77f4f8SLiu Bo 
365cd77f4f8SLiu Bo /*
366cd77f4f8SLiu Bo  * Test scenario:
367cd77f4f8SLiu Bo  *
368cd77f4f8SLiu Bo  * Suppose that no extent map has been loaded into memory yet.
369cd77f4f8SLiu Bo  * There is a file extent [0, 32K), two jobs are running concurrently
370cd77f4f8SLiu Bo  * against it, t1 is doing dio write to [8K, 32K) and t2 is doing dio
371cd77f4f8SLiu Bo  * read from [0, 4K) or [4K, 8K).
372cd77f4f8SLiu Bo  *
373cd77f4f8SLiu Bo  * t1 goes ahead of t2 and splits em [0, 32K) to em [0K, 8K) and [8K 32K).
374cd77f4f8SLiu Bo  *
375cd77f4f8SLiu Bo  *         t1                                t2
376cd77f4f8SLiu Bo  *  btrfs_get_blocks_direct()	       btrfs_get_blocks_direct()
377cd77f4f8SLiu Bo  *   -> btrfs_get_extent()              -> btrfs_get_extent()
378cd77f4f8SLiu Bo  *       -> lookup_extent_mapping()
379cd77f4f8SLiu Bo  *       -> add_extent_mapping()            -> lookup_extent_mapping()
380cd77f4f8SLiu Bo  *          # load [0, 32K)
381cd77f4f8SLiu Bo  *   -> btrfs_new_extent_direct()
382cd77f4f8SLiu Bo  *       -> btrfs_drop_extent_cache()
383cd77f4f8SLiu Bo  *          # split [0, 32K)
384cd77f4f8SLiu Bo  *       -> add_extent_mapping()
385cd77f4f8SLiu Bo  *          # add [8K, 32K)
386cd77f4f8SLiu Bo  *                                          -> add_extent_mapping()
387cd77f4f8SLiu Bo  *                                             # handle -EEXIST when adding
388cd77f4f8SLiu Bo  *                                             # [0, 32K)
389cd77f4f8SLiu Bo  */
390*ccfada1fSDavid Sterba static int test_case_4(struct btrfs_fs_info *fs_info,
3910e08eb9bSDavid Sterba 		struct extent_map_tree *em_tree)
392cd77f4f8SLiu Bo {
393*ccfada1fSDavid Sterba 	int ret;
394*ccfada1fSDavid Sterba 
395*ccfada1fSDavid Sterba 	ret = __test_case_4(fs_info, em_tree, 0);
396*ccfada1fSDavid Sterba 	if (ret)
397*ccfada1fSDavid Sterba 		return ret;
398*ccfada1fSDavid Sterba 	ret = __test_case_4(fs_info, em_tree, SZ_4K);
399*ccfada1fSDavid Sterba 
400*ccfada1fSDavid Sterba 	return ret;
401cd77f4f8SLiu Bo }
402cd77f4f8SLiu Bo 
40397dc231eSColin Ian King int btrfs_test_extent_map(void)
40472b28077SLiu Bo {
4050e08eb9bSDavid Sterba 	struct btrfs_fs_info *fs_info = NULL;
40672b28077SLiu Bo 	struct extent_map_tree *em_tree;
407488f6730SDavid Sterba 	int ret = 0;
40872b28077SLiu Bo 
409315b76b4SDavid Sterba 	test_msg("running extent_map tests");
41072b28077SLiu Bo 
4110e08eb9bSDavid Sterba 	/*
4120e08eb9bSDavid Sterba 	 * Note: the fs_info is not set up completely, we only need
4130e08eb9bSDavid Sterba 	 * fs_info::fsid for the tracepoint.
4140e08eb9bSDavid Sterba 	 */
4150e08eb9bSDavid Sterba 	fs_info = btrfs_alloc_dummy_fs_info(PAGE_SIZE, PAGE_SIZE);
4160e08eb9bSDavid Sterba 	if (!fs_info) {
41737b2a7bcSDavid Sterba 		test_std_err(TEST_ALLOC_FS_INFO);
4180e08eb9bSDavid Sterba 		return -ENOMEM;
4190e08eb9bSDavid Sterba 	}
4200e08eb9bSDavid Sterba 
42172b28077SLiu Bo 	em_tree = kzalloc(sizeof(*em_tree), GFP_KERNEL);
422488f6730SDavid Sterba 	if (!em_tree) {
423488f6730SDavid Sterba 		ret = -ENOMEM;
4240e08eb9bSDavid Sterba 		goto out;
425488f6730SDavid Sterba 	}
42672b28077SLiu Bo 
42772b28077SLiu Bo 	extent_map_tree_init(em_tree);
42872b28077SLiu Bo 
429*ccfada1fSDavid Sterba 	ret = test_case_1(fs_info, em_tree);
430*ccfada1fSDavid Sterba 	if (ret)
431*ccfada1fSDavid Sterba 		goto out;
432*ccfada1fSDavid Sterba 	ret = test_case_2(fs_info, em_tree);
433*ccfada1fSDavid Sterba 	if (ret)
434*ccfada1fSDavid Sterba 		goto out;
435*ccfada1fSDavid Sterba 	ret = test_case_3(fs_info, em_tree);
436*ccfada1fSDavid Sterba 	if (ret)
437*ccfada1fSDavid Sterba 		goto out;
438*ccfada1fSDavid Sterba 	ret = test_case_4(fs_info, em_tree);
43972b28077SLiu Bo 
4400e08eb9bSDavid Sterba out:
441*ccfada1fSDavid Sterba 	kfree(em_tree);
4420e08eb9bSDavid Sterba 	btrfs_free_dummy_fs_info(fs_info);
4430e08eb9bSDavid Sterba 
444488f6730SDavid Sterba 	return ret;
44572b28077SLiu Bo }
446