18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2017 Oracle.  All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/types.h>
78c2ecf20Sopenharmony_ci#include "btrfs-tests.h"
88c2ecf20Sopenharmony_ci#include "../ctree.h"
98c2ecf20Sopenharmony_ci#include "../volumes.h"
108c2ecf20Sopenharmony_ci#include "../disk-io.h"
118c2ecf20Sopenharmony_ci#include "../block-group.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic void free_extent_map_tree(struct extent_map_tree *em_tree)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	struct extent_map *em;
168c2ecf20Sopenharmony_ci	struct rb_node *node;
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci	while (!RB_EMPTY_ROOT(&em_tree->map.rb_root)) {
198c2ecf20Sopenharmony_ci		node = rb_first_cached(&em_tree->map);
208c2ecf20Sopenharmony_ci		em = rb_entry(node, struct extent_map, rb_node);
218c2ecf20Sopenharmony_ci		remove_extent_mapping(em_tree, em);
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#ifdef CONFIG_BTRFS_DEBUG
248c2ecf20Sopenharmony_ci		if (refcount_read(&em->refs) != 1) {
258c2ecf20Sopenharmony_ci			test_err(
268c2ecf20Sopenharmony_ci"em leak: em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx) refs %d",
278c2ecf20Sopenharmony_ci				 em->start, em->len, em->block_start,
288c2ecf20Sopenharmony_ci				 em->block_len, refcount_read(&em->refs));
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci			refcount_set(&em->refs, 1);
318c2ecf20Sopenharmony_ci		}
328c2ecf20Sopenharmony_ci#endif
338c2ecf20Sopenharmony_ci		free_extent_map(em);
348c2ecf20Sopenharmony_ci	}
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * Test scenario:
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * Suppose that no extent map has been loaded into memory yet, there is a file
418c2ecf20Sopenharmony_ci * extent [0, 16K), followed by another file extent [16K, 20K), two dio reads
428c2ecf20Sopenharmony_ci * are entering btrfs_get_extent() concurrently, t1 is reading [8K, 16K), t2 is
438c2ecf20Sopenharmony_ci * reading [0, 8K)
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci *     t1                            t2
468c2ecf20Sopenharmony_ci *  btrfs_get_extent()              btrfs_get_extent()
478c2ecf20Sopenharmony_ci *    -> lookup_extent_mapping()      ->lookup_extent_mapping()
488c2ecf20Sopenharmony_ci *    -> add_extent_mapping(0, 16K)
498c2ecf20Sopenharmony_ci *    -> return em
508c2ecf20Sopenharmony_ci *                                    ->add_extent_mapping(0, 16K)
518c2ecf20Sopenharmony_ci *                                    -> #handle -EEXIST
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_cistatic int test_case_1(struct btrfs_fs_info *fs_info,
548c2ecf20Sopenharmony_ci		struct extent_map_tree *em_tree)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct extent_map *em;
578c2ecf20Sopenharmony_ci	u64 start = 0;
588c2ecf20Sopenharmony_ci	u64 len = SZ_8K;
598c2ecf20Sopenharmony_ci	int ret;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	em = alloc_extent_map();
628c2ecf20Sopenharmony_ci	if (!em) {
638c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
648c2ecf20Sopenharmony_ci		return -ENOMEM;
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	/* Add [0, 16K) */
688c2ecf20Sopenharmony_ci	em->start = 0;
698c2ecf20Sopenharmony_ci	em->len = SZ_16K;
708c2ecf20Sopenharmony_ci	em->block_start = 0;
718c2ecf20Sopenharmony_ci	em->block_len = SZ_16K;
728c2ecf20Sopenharmony_ci	write_lock(&em_tree->lock);
738c2ecf20Sopenharmony_ci	ret = add_extent_mapping(em_tree, em, 0);
748c2ecf20Sopenharmony_ci	write_unlock(&em_tree->lock);
758c2ecf20Sopenharmony_ci	if (ret < 0) {
768c2ecf20Sopenharmony_ci		test_err("cannot add extent range [0, 16K)");
778c2ecf20Sopenharmony_ci		goto out;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci	free_extent_map(em);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/* Add [16K, 20K) following [0, 16K)  */
828c2ecf20Sopenharmony_ci	em = alloc_extent_map();
838c2ecf20Sopenharmony_ci	if (!em) {
848c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
858c2ecf20Sopenharmony_ci		ret = -ENOMEM;
868c2ecf20Sopenharmony_ci		goto out;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	em->start = SZ_16K;
908c2ecf20Sopenharmony_ci	em->len = SZ_4K;
918c2ecf20Sopenharmony_ci	em->block_start = SZ_32K; /* avoid merging */
928c2ecf20Sopenharmony_ci	em->block_len = SZ_4K;
938c2ecf20Sopenharmony_ci	write_lock(&em_tree->lock);
948c2ecf20Sopenharmony_ci	ret = add_extent_mapping(em_tree, em, 0);
958c2ecf20Sopenharmony_ci	write_unlock(&em_tree->lock);
968c2ecf20Sopenharmony_ci	if (ret < 0) {
978c2ecf20Sopenharmony_ci		test_err("cannot add extent range [16K, 20K)");
988c2ecf20Sopenharmony_ci		goto out;
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci	free_extent_map(em);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	em = alloc_extent_map();
1038c2ecf20Sopenharmony_ci	if (!em) {
1048c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
1058c2ecf20Sopenharmony_ci		ret = -ENOMEM;
1068c2ecf20Sopenharmony_ci		goto out;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/* Add [0, 8K), should return [0, 16K) instead. */
1108c2ecf20Sopenharmony_ci	em->start = start;
1118c2ecf20Sopenharmony_ci	em->len = len;
1128c2ecf20Sopenharmony_ci	em->block_start = start;
1138c2ecf20Sopenharmony_ci	em->block_len = len;
1148c2ecf20Sopenharmony_ci	write_lock(&em_tree->lock);
1158c2ecf20Sopenharmony_ci	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
1168c2ecf20Sopenharmony_ci	write_unlock(&em_tree->lock);
1178c2ecf20Sopenharmony_ci	if (ret) {
1188c2ecf20Sopenharmony_ci		test_err("case1 [%llu %llu]: ret %d", start, start + len, ret);
1198c2ecf20Sopenharmony_ci		goto out;
1208c2ecf20Sopenharmony_ci	}
1218c2ecf20Sopenharmony_ci	if (em &&
1228c2ecf20Sopenharmony_ci	    (em->start != 0 || extent_map_end(em) != SZ_16K ||
1238c2ecf20Sopenharmony_ci	     em->block_start != 0 || em->block_len != SZ_16K)) {
1248c2ecf20Sopenharmony_ci		test_err(
1258c2ecf20Sopenharmony_ci"case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
1268c2ecf20Sopenharmony_ci			 start, start + len, ret, em->start, em->len,
1278c2ecf20Sopenharmony_ci			 em->block_start, em->block_len);
1288c2ecf20Sopenharmony_ci		ret = -EINVAL;
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci	free_extent_map(em);
1318c2ecf20Sopenharmony_ciout:
1328c2ecf20Sopenharmony_ci	free_extent_map_tree(em_tree);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return ret;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci/*
1388c2ecf20Sopenharmony_ci * Test scenario:
1398c2ecf20Sopenharmony_ci *
1408c2ecf20Sopenharmony_ci * Reading the inline ending up with EEXIST, ie. read an inline
1418c2ecf20Sopenharmony_ci * extent and discard page cache and read it again.
1428c2ecf20Sopenharmony_ci */
1438c2ecf20Sopenharmony_cistatic int test_case_2(struct btrfs_fs_info *fs_info,
1448c2ecf20Sopenharmony_ci		struct extent_map_tree *em_tree)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	struct extent_map *em;
1478c2ecf20Sopenharmony_ci	int ret;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	em = alloc_extent_map();
1508c2ecf20Sopenharmony_ci	if (!em) {
1518c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
1528c2ecf20Sopenharmony_ci		return -ENOMEM;
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	/* Add [0, 1K) */
1568c2ecf20Sopenharmony_ci	em->start = 0;
1578c2ecf20Sopenharmony_ci	em->len = SZ_1K;
1588c2ecf20Sopenharmony_ci	em->block_start = EXTENT_MAP_INLINE;
1598c2ecf20Sopenharmony_ci	em->block_len = (u64)-1;
1608c2ecf20Sopenharmony_ci	write_lock(&em_tree->lock);
1618c2ecf20Sopenharmony_ci	ret = add_extent_mapping(em_tree, em, 0);
1628c2ecf20Sopenharmony_ci	write_unlock(&em_tree->lock);
1638c2ecf20Sopenharmony_ci	if (ret < 0) {
1648c2ecf20Sopenharmony_ci		test_err("cannot add extent range [0, 1K)");
1658c2ecf20Sopenharmony_ci		goto out;
1668c2ecf20Sopenharmony_ci	}
1678c2ecf20Sopenharmony_ci	free_extent_map(em);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	/* Add [4K, 8K) following [0, 1K)  */
1708c2ecf20Sopenharmony_ci	em = alloc_extent_map();
1718c2ecf20Sopenharmony_ci	if (!em) {
1728c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
1738c2ecf20Sopenharmony_ci		ret = -ENOMEM;
1748c2ecf20Sopenharmony_ci		goto out;
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	em->start = SZ_4K;
1788c2ecf20Sopenharmony_ci	em->len = SZ_4K;
1798c2ecf20Sopenharmony_ci	em->block_start = SZ_4K;
1808c2ecf20Sopenharmony_ci	em->block_len = SZ_4K;
1818c2ecf20Sopenharmony_ci	write_lock(&em_tree->lock);
1828c2ecf20Sopenharmony_ci	ret = add_extent_mapping(em_tree, em, 0);
1838c2ecf20Sopenharmony_ci	write_unlock(&em_tree->lock);
1848c2ecf20Sopenharmony_ci	if (ret < 0) {
1858c2ecf20Sopenharmony_ci		test_err("cannot add extent range [4K, 8K)");
1868c2ecf20Sopenharmony_ci		goto out;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci	free_extent_map(em);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	em = alloc_extent_map();
1918c2ecf20Sopenharmony_ci	if (!em) {
1928c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
1938c2ecf20Sopenharmony_ci		ret = -ENOMEM;
1948c2ecf20Sopenharmony_ci		goto out;
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/* Add [0, 1K) */
1988c2ecf20Sopenharmony_ci	em->start = 0;
1998c2ecf20Sopenharmony_ci	em->len = SZ_1K;
2008c2ecf20Sopenharmony_ci	em->block_start = EXTENT_MAP_INLINE;
2018c2ecf20Sopenharmony_ci	em->block_len = (u64)-1;
2028c2ecf20Sopenharmony_ci	write_lock(&em_tree->lock);
2038c2ecf20Sopenharmony_ci	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
2048c2ecf20Sopenharmony_ci	write_unlock(&em_tree->lock);
2058c2ecf20Sopenharmony_ci	if (ret) {
2068c2ecf20Sopenharmony_ci		test_err("case2 [0 1K]: ret %d", ret);
2078c2ecf20Sopenharmony_ci		goto out;
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci	if (em &&
2108c2ecf20Sopenharmony_ci	    (em->start != 0 || extent_map_end(em) != SZ_1K ||
2118c2ecf20Sopenharmony_ci	     em->block_start != EXTENT_MAP_INLINE || em->block_len != (u64)-1)) {
2128c2ecf20Sopenharmony_ci		test_err(
2138c2ecf20Sopenharmony_ci"case2 [0 1K]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
2148c2ecf20Sopenharmony_ci			 ret, em->start, em->len, em->block_start,
2158c2ecf20Sopenharmony_ci			 em->block_len);
2168c2ecf20Sopenharmony_ci		ret = -EINVAL;
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci	free_extent_map(em);
2198c2ecf20Sopenharmony_ciout:
2208c2ecf20Sopenharmony_ci	free_extent_map_tree(em_tree);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	return ret;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic int __test_case_3(struct btrfs_fs_info *fs_info,
2268c2ecf20Sopenharmony_ci		struct extent_map_tree *em_tree, u64 start)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	struct extent_map *em;
2298c2ecf20Sopenharmony_ci	u64 len = SZ_4K;
2308c2ecf20Sopenharmony_ci	int ret;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	em = alloc_extent_map();
2338c2ecf20Sopenharmony_ci	if (!em) {
2348c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
2358c2ecf20Sopenharmony_ci		return -ENOMEM;
2368c2ecf20Sopenharmony_ci	}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	/* Add [4K, 8K) */
2398c2ecf20Sopenharmony_ci	em->start = SZ_4K;
2408c2ecf20Sopenharmony_ci	em->len = SZ_4K;
2418c2ecf20Sopenharmony_ci	em->block_start = SZ_4K;
2428c2ecf20Sopenharmony_ci	em->block_len = SZ_4K;
2438c2ecf20Sopenharmony_ci	write_lock(&em_tree->lock);
2448c2ecf20Sopenharmony_ci	ret = add_extent_mapping(em_tree, em, 0);
2458c2ecf20Sopenharmony_ci	write_unlock(&em_tree->lock);
2468c2ecf20Sopenharmony_ci	if (ret < 0) {
2478c2ecf20Sopenharmony_ci		test_err("cannot add extent range [4K, 8K)");
2488c2ecf20Sopenharmony_ci		goto out;
2498c2ecf20Sopenharmony_ci	}
2508c2ecf20Sopenharmony_ci	free_extent_map(em);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	em = alloc_extent_map();
2538c2ecf20Sopenharmony_ci	if (!em) {
2548c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
2558c2ecf20Sopenharmony_ci		ret = -ENOMEM;
2568c2ecf20Sopenharmony_ci		goto out;
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	/* Add [0, 16K) */
2608c2ecf20Sopenharmony_ci	em->start = 0;
2618c2ecf20Sopenharmony_ci	em->len = SZ_16K;
2628c2ecf20Sopenharmony_ci	em->block_start = 0;
2638c2ecf20Sopenharmony_ci	em->block_len = SZ_16K;
2648c2ecf20Sopenharmony_ci	write_lock(&em_tree->lock);
2658c2ecf20Sopenharmony_ci	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
2668c2ecf20Sopenharmony_ci	write_unlock(&em_tree->lock);
2678c2ecf20Sopenharmony_ci	if (ret) {
2688c2ecf20Sopenharmony_ci		test_err("case3 [0x%llx 0x%llx): ret %d",
2698c2ecf20Sopenharmony_ci			 start, start + len, ret);
2708c2ecf20Sopenharmony_ci		goto out;
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci	/*
2738c2ecf20Sopenharmony_ci	 * Since bytes within em are contiguous, em->block_start is identical to
2748c2ecf20Sopenharmony_ci	 * em->start.
2758c2ecf20Sopenharmony_ci	 */
2768c2ecf20Sopenharmony_ci	if (em &&
2778c2ecf20Sopenharmony_ci	    (start < em->start || start + len > extent_map_end(em) ||
2788c2ecf20Sopenharmony_ci	     em->start != em->block_start || em->len != em->block_len)) {
2798c2ecf20Sopenharmony_ci		test_err(
2808c2ecf20Sopenharmony_ci"case3 [0x%llx 0x%llx): ret %d em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)",
2818c2ecf20Sopenharmony_ci			 start, start + len, ret, em->start, em->len,
2828c2ecf20Sopenharmony_ci			 em->block_start, em->block_len);
2838c2ecf20Sopenharmony_ci		ret = -EINVAL;
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci	free_extent_map(em);
2868c2ecf20Sopenharmony_ciout:
2878c2ecf20Sopenharmony_ci	free_extent_map_tree(em_tree);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	return ret;
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci/*
2938c2ecf20Sopenharmony_ci * Test scenario:
2948c2ecf20Sopenharmony_ci *
2958c2ecf20Sopenharmony_ci * Suppose that no extent map has been loaded into memory yet.
2968c2ecf20Sopenharmony_ci * There is a file extent [0, 16K), two jobs are running concurrently
2978c2ecf20Sopenharmony_ci * against it, t1 is buffered writing to [4K, 8K) and t2 is doing dio
2988c2ecf20Sopenharmony_ci * read from [0, 4K) or [8K, 12K) or [12K, 16K).
2998c2ecf20Sopenharmony_ci *
3008c2ecf20Sopenharmony_ci * t1 goes ahead of t2 and adds em [4K, 8K) into tree.
3018c2ecf20Sopenharmony_ci *
3028c2ecf20Sopenharmony_ci *         t1                       t2
3038c2ecf20Sopenharmony_ci *  cow_file_range()	     btrfs_get_extent()
3048c2ecf20Sopenharmony_ci *                            -> lookup_extent_mapping()
3058c2ecf20Sopenharmony_ci *   -> add_extent_mapping()
3068c2ecf20Sopenharmony_ci *                            -> add_extent_mapping()
3078c2ecf20Sopenharmony_ci */
3088c2ecf20Sopenharmony_cistatic int test_case_3(struct btrfs_fs_info *fs_info,
3098c2ecf20Sopenharmony_ci		struct extent_map_tree *em_tree)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	int ret;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	ret = __test_case_3(fs_info, em_tree, 0);
3148c2ecf20Sopenharmony_ci	if (ret)
3158c2ecf20Sopenharmony_ci		return ret;
3168c2ecf20Sopenharmony_ci	ret = __test_case_3(fs_info, em_tree, SZ_8K);
3178c2ecf20Sopenharmony_ci	if (ret)
3188c2ecf20Sopenharmony_ci		return ret;
3198c2ecf20Sopenharmony_ci	ret = __test_case_3(fs_info, em_tree, (12 * SZ_1K));
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	return ret;
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic int __test_case_4(struct btrfs_fs_info *fs_info,
3258c2ecf20Sopenharmony_ci		struct extent_map_tree *em_tree, u64 start)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	struct extent_map *em;
3288c2ecf20Sopenharmony_ci	u64 len = SZ_4K;
3298c2ecf20Sopenharmony_ci	int ret;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	em = alloc_extent_map();
3328c2ecf20Sopenharmony_ci	if (!em) {
3338c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
3348c2ecf20Sopenharmony_ci		return -ENOMEM;
3358c2ecf20Sopenharmony_ci	}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	/* Add [0K, 8K) */
3388c2ecf20Sopenharmony_ci	em->start = 0;
3398c2ecf20Sopenharmony_ci	em->len = SZ_8K;
3408c2ecf20Sopenharmony_ci	em->block_start = 0;
3418c2ecf20Sopenharmony_ci	em->block_len = SZ_8K;
3428c2ecf20Sopenharmony_ci	write_lock(&em_tree->lock);
3438c2ecf20Sopenharmony_ci	ret = add_extent_mapping(em_tree, em, 0);
3448c2ecf20Sopenharmony_ci	write_unlock(&em_tree->lock);
3458c2ecf20Sopenharmony_ci	if (ret < 0) {
3468c2ecf20Sopenharmony_ci		test_err("cannot add extent range [0, 8K)");
3478c2ecf20Sopenharmony_ci		goto out;
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci	free_extent_map(em);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	em = alloc_extent_map();
3528c2ecf20Sopenharmony_ci	if (!em) {
3538c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
3548c2ecf20Sopenharmony_ci		ret = -ENOMEM;
3558c2ecf20Sopenharmony_ci		goto out;
3568c2ecf20Sopenharmony_ci	}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	/* Add [8K, 32K) */
3598c2ecf20Sopenharmony_ci	em->start = SZ_8K;
3608c2ecf20Sopenharmony_ci	em->len = 24 * SZ_1K;
3618c2ecf20Sopenharmony_ci	em->block_start = SZ_16K; /* avoid merging */
3628c2ecf20Sopenharmony_ci	em->block_len = 24 * SZ_1K;
3638c2ecf20Sopenharmony_ci	write_lock(&em_tree->lock);
3648c2ecf20Sopenharmony_ci	ret = add_extent_mapping(em_tree, em, 0);
3658c2ecf20Sopenharmony_ci	write_unlock(&em_tree->lock);
3668c2ecf20Sopenharmony_ci	if (ret < 0) {
3678c2ecf20Sopenharmony_ci		test_err("cannot add extent range [8K, 32K)");
3688c2ecf20Sopenharmony_ci		goto out;
3698c2ecf20Sopenharmony_ci	}
3708c2ecf20Sopenharmony_ci	free_extent_map(em);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	em = alloc_extent_map();
3738c2ecf20Sopenharmony_ci	if (!em) {
3748c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
3758c2ecf20Sopenharmony_ci		ret = -ENOMEM;
3768c2ecf20Sopenharmony_ci		goto out;
3778c2ecf20Sopenharmony_ci	}
3788c2ecf20Sopenharmony_ci	/* Add [0K, 32K) */
3798c2ecf20Sopenharmony_ci	em->start = 0;
3808c2ecf20Sopenharmony_ci	em->len = SZ_32K;
3818c2ecf20Sopenharmony_ci	em->block_start = 0;
3828c2ecf20Sopenharmony_ci	em->block_len = SZ_32K;
3838c2ecf20Sopenharmony_ci	write_lock(&em_tree->lock);
3848c2ecf20Sopenharmony_ci	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
3858c2ecf20Sopenharmony_ci	write_unlock(&em_tree->lock);
3868c2ecf20Sopenharmony_ci	if (ret) {
3878c2ecf20Sopenharmony_ci		test_err("case4 [0x%llx 0x%llx): ret %d",
3888c2ecf20Sopenharmony_ci			 start, len, ret);
3898c2ecf20Sopenharmony_ci		goto out;
3908c2ecf20Sopenharmony_ci	}
3918c2ecf20Sopenharmony_ci	if (em && (start < em->start || start + len > extent_map_end(em))) {
3928c2ecf20Sopenharmony_ci		test_err(
3938c2ecf20Sopenharmony_ci"case4 [0x%llx 0x%llx): ret %d, added wrong em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)",
3948c2ecf20Sopenharmony_ci			 start, len, ret, em->start, em->len, em->block_start,
3958c2ecf20Sopenharmony_ci			 em->block_len);
3968c2ecf20Sopenharmony_ci		ret = -EINVAL;
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci	free_extent_map(em);
3998c2ecf20Sopenharmony_ciout:
4008c2ecf20Sopenharmony_ci	free_extent_map_tree(em_tree);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	return ret;
4038c2ecf20Sopenharmony_ci}
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci/*
4068c2ecf20Sopenharmony_ci * Test scenario:
4078c2ecf20Sopenharmony_ci *
4088c2ecf20Sopenharmony_ci * Suppose that no extent map has been loaded into memory yet.
4098c2ecf20Sopenharmony_ci * There is a file extent [0, 32K), two jobs are running concurrently
4108c2ecf20Sopenharmony_ci * against it, t1 is doing dio write to [8K, 32K) and t2 is doing dio
4118c2ecf20Sopenharmony_ci * read from [0, 4K) or [4K, 8K).
4128c2ecf20Sopenharmony_ci *
4138c2ecf20Sopenharmony_ci * t1 goes ahead of t2 and splits em [0, 32K) to em [0K, 8K) and [8K 32K).
4148c2ecf20Sopenharmony_ci *
4158c2ecf20Sopenharmony_ci *         t1                                t2
4168c2ecf20Sopenharmony_ci *  btrfs_get_blocks_direct()	       btrfs_get_blocks_direct()
4178c2ecf20Sopenharmony_ci *   -> btrfs_get_extent()              -> btrfs_get_extent()
4188c2ecf20Sopenharmony_ci *       -> lookup_extent_mapping()
4198c2ecf20Sopenharmony_ci *       -> add_extent_mapping()            -> lookup_extent_mapping()
4208c2ecf20Sopenharmony_ci *          # load [0, 32K)
4218c2ecf20Sopenharmony_ci *   -> btrfs_new_extent_direct()
4228c2ecf20Sopenharmony_ci *       -> btrfs_drop_extent_cache()
4238c2ecf20Sopenharmony_ci *          # split [0, 32K)
4248c2ecf20Sopenharmony_ci *       -> add_extent_mapping()
4258c2ecf20Sopenharmony_ci *          # add [8K, 32K)
4268c2ecf20Sopenharmony_ci *                                          -> add_extent_mapping()
4278c2ecf20Sopenharmony_ci *                                             # handle -EEXIST when adding
4288c2ecf20Sopenharmony_ci *                                             # [0, 32K)
4298c2ecf20Sopenharmony_ci */
4308c2ecf20Sopenharmony_cistatic int test_case_4(struct btrfs_fs_info *fs_info,
4318c2ecf20Sopenharmony_ci		struct extent_map_tree *em_tree)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	int ret;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	ret = __test_case_4(fs_info, em_tree, 0);
4368c2ecf20Sopenharmony_ci	if (ret)
4378c2ecf20Sopenharmony_ci		return ret;
4388c2ecf20Sopenharmony_ci	ret = __test_case_4(fs_info, em_tree, SZ_4K);
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	return ret;
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_cistruct rmap_test_vector {
4448c2ecf20Sopenharmony_ci	u64 raid_type;
4458c2ecf20Sopenharmony_ci	u64 physical_start;
4468c2ecf20Sopenharmony_ci	u64 data_stripe_size;
4478c2ecf20Sopenharmony_ci	u64 num_data_stripes;
4488c2ecf20Sopenharmony_ci	u64 num_stripes;
4498c2ecf20Sopenharmony_ci	/* Assume we won't have more than 5 physical stripes */
4508c2ecf20Sopenharmony_ci	u64 data_stripe_phys_start[5];
4518c2ecf20Sopenharmony_ci	bool expected_mapped_addr;
4528c2ecf20Sopenharmony_ci	/* Physical to logical addresses */
4538c2ecf20Sopenharmony_ci	u64 mapped_logical[5];
4548c2ecf20Sopenharmony_ci};
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_cistatic int test_rmap_block(struct btrfs_fs_info *fs_info,
4578c2ecf20Sopenharmony_ci			   struct rmap_test_vector *test)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci	struct extent_map *em;
4608c2ecf20Sopenharmony_ci	struct map_lookup *map = NULL;
4618c2ecf20Sopenharmony_ci	u64 *logical = NULL;
4628c2ecf20Sopenharmony_ci	int i, out_ndaddrs, out_stripe_len;
4638c2ecf20Sopenharmony_ci	int ret;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	em = alloc_extent_map();
4668c2ecf20Sopenharmony_ci	if (!em) {
4678c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
4688c2ecf20Sopenharmony_ci		return -ENOMEM;
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	map = kmalloc(map_lookup_size(test->num_stripes), GFP_KERNEL);
4728c2ecf20Sopenharmony_ci	if (!map) {
4738c2ecf20Sopenharmony_ci		kfree(em);
4748c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_MAP);
4758c2ecf20Sopenharmony_ci		return -ENOMEM;
4768c2ecf20Sopenharmony_ci	}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
4798c2ecf20Sopenharmony_ci	/* Start at 4GiB logical address */
4808c2ecf20Sopenharmony_ci	em->start = SZ_4G;
4818c2ecf20Sopenharmony_ci	em->len = test->data_stripe_size * test->num_data_stripes;
4828c2ecf20Sopenharmony_ci	em->block_len = em->len;
4838c2ecf20Sopenharmony_ci	em->orig_block_len = test->data_stripe_size;
4848c2ecf20Sopenharmony_ci	em->map_lookup = map;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	map->num_stripes = test->num_stripes;
4878c2ecf20Sopenharmony_ci	map->stripe_len = BTRFS_STRIPE_LEN;
4888c2ecf20Sopenharmony_ci	map->type = test->raid_type;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	for (i = 0; i < map->num_stripes; i++) {
4918c2ecf20Sopenharmony_ci		struct btrfs_device *dev = btrfs_alloc_dummy_device(fs_info);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci		if (IS_ERR(dev)) {
4948c2ecf20Sopenharmony_ci			test_err("cannot allocate device");
4958c2ecf20Sopenharmony_ci			ret = PTR_ERR(dev);
4968c2ecf20Sopenharmony_ci			goto out;
4978c2ecf20Sopenharmony_ci		}
4988c2ecf20Sopenharmony_ci		map->stripes[i].dev = dev;
4998c2ecf20Sopenharmony_ci		map->stripes[i].physical = test->data_stripe_phys_start[i];
5008c2ecf20Sopenharmony_ci	}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	write_lock(&fs_info->mapping_tree.lock);
5038c2ecf20Sopenharmony_ci	ret = add_extent_mapping(&fs_info->mapping_tree, em, 0);
5048c2ecf20Sopenharmony_ci	write_unlock(&fs_info->mapping_tree.lock);
5058c2ecf20Sopenharmony_ci	if (ret) {
5068c2ecf20Sopenharmony_ci		test_err("error adding block group mapping to mapping tree");
5078c2ecf20Sopenharmony_ci		goto out_free;
5088c2ecf20Sopenharmony_ci	}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	ret = btrfs_rmap_block(fs_info, em->start, btrfs_sb_offset(1),
5118c2ecf20Sopenharmony_ci			       &logical, &out_ndaddrs, &out_stripe_len);
5128c2ecf20Sopenharmony_ci	if (ret || (out_ndaddrs == 0 && test->expected_mapped_addr)) {
5138c2ecf20Sopenharmony_ci		test_err("didn't rmap anything but expected %d",
5148c2ecf20Sopenharmony_ci			 test->expected_mapped_addr);
5158c2ecf20Sopenharmony_ci		goto out;
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	if (out_stripe_len != BTRFS_STRIPE_LEN) {
5198c2ecf20Sopenharmony_ci		test_err("calculated stripe length doesn't match");
5208c2ecf20Sopenharmony_ci		goto out;
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	if (out_ndaddrs != test->expected_mapped_addr) {
5248c2ecf20Sopenharmony_ci		for (i = 0; i < out_ndaddrs; i++)
5258c2ecf20Sopenharmony_ci			test_msg("mapped %llu", logical[i]);
5268c2ecf20Sopenharmony_ci		test_err("unexpected number of mapped addresses: %d", out_ndaddrs);
5278c2ecf20Sopenharmony_ci		goto out;
5288c2ecf20Sopenharmony_ci	}
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	for (i = 0; i < out_ndaddrs; i++) {
5318c2ecf20Sopenharmony_ci		if (logical[i] != test->mapped_logical[i]) {
5328c2ecf20Sopenharmony_ci			test_err("unexpected logical address mapped");
5338c2ecf20Sopenharmony_ci			goto out;
5348c2ecf20Sopenharmony_ci		}
5358c2ecf20Sopenharmony_ci	}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	ret = 0;
5388c2ecf20Sopenharmony_ciout:
5398c2ecf20Sopenharmony_ci	write_lock(&fs_info->mapping_tree.lock);
5408c2ecf20Sopenharmony_ci	remove_extent_mapping(&fs_info->mapping_tree, em);
5418c2ecf20Sopenharmony_ci	write_unlock(&fs_info->mapping_tree.lock);
5428c2ecf20Sopenharmony_ci	/* For us */
5438c2ecf20Sopenharmony_ci	free_extent_map(em);
5448c2ecf20Sopenharmony_ciout_free:
5458c2ecf20Sopenharmony_ci	/* For the tree */
5468c2ecf20Sopenharmony_ci	free_extent_map(em);
5478c2ecf20Sopenharmony_ci	kfree(logical);
5488c2ecf20Sopenharmony_ci	return ret;
5498c2ecf20Sopenharmony_ci}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ciint btrfs_test_extent_map(void)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = NULL;
5548c2ecf20Sopenharmony_ci	struct extent_map_tree *em_tree;
5558c2ecf20Sopenharmony_ci	int ret = 0, i;
5568c2ecf20Sopenharmony_ci	struct rmap_test_vector rmap_tests[] = {
5578c2ecf20Sopenharmony_ci		{
5588c2ecf20Sopenharmony_ci			/*
5598c2ecf20Sopenharmony_ci			 * Test a chunk with 2 data stripes one of which
5608c2ecf20Sopenharmony_ci			 * interesects the physical address of the super block
5618c2ecf20Sopenharmony_ci			 * is correctly recognised.
5628c2ecf20Sopenharmony_ci			 */
5638c2ecf20Sopenharmony_ci			.raid_type = BTRFS_BLOCK_GROUP_RAID1,
5648c2ecf20Sopenharmony_ci			.physical_start = SZ_64M - SZ_4M,
5658c2ecf20Sopenharmony_ci			.data_stripe_size = SZ_256M,
5668c2ecf20Sopenharmony_ci			.num_data_stripes = 2,
5678c2ecf20Sopenharmony_ci			.num_stripes = 2,
5688c2ecf20Sopenharmony_ci			.data_stripe_phys_start =
5698c2ecf20Sopenharmony_ci				{SZ_64M - SZ_4M, SZ_64M - SZ_4M + SZ_256M},
5708c2ecf20Sopenharmony_ci			.expected_mapped_addr = true,
5718c2ecf20Sopenharmony_ci			.mapped_logical= {SZ_4G + SZ_4M}
5728c2ecf20Sopenharmony_ci		},
5738c2ecf20Sopenharmony_ci		{
5748c2ecf20Sopenharmony_ci			/*
5758c2ecf20Sopenharmony_ci			 * Test that out-of-range physical addresses are
5768c2ecf20Sopenharmony_ci			 * ignored
5778c2ecf20Sopenharmony_ci			 */
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci			 /* SINGLE chunk type */
5808c2ecf20Sopenharmony_ci			.raid_type = 0,
5818c2ecf20Sopenharmony_ci			.physical_start = SZ_4G,
5828c2ecf20Sopenharmony_ci			.data_stripe_size = SZ_256M,
5838c2ecf20Sopenharmony_ci			.num_data_stripes = 1,
5848c2ecf20Sopenharmony_ci			.num_stripes = 1,
5858c2ecf20Sopenharmony_ci			.data_stripe_phys_start = {SZ_256M},
5868c2ecf20Sopenharmony_ci			.expected_mapped_addr = false,
5878c2ecf20Sopenharmony_ci			.mapped_logical = {0}
5888c2ecf20Sopenharmony_ci		}
5898c2ecf20Sopenharmony_ci	};
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	test_msg("running extent_map tests");
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	/*
5948c2ecf20Sopenharmony_ci	 * Note: the fs_info is not set up completely, we only need
5958c2ecf20Sopenharmony_ci	 * fs_info::fsid for the tracepoint.
5968c2ecf20Sopenharmony_ci	 */
5978c2ecf20Sopenharmony_ci	fs_info = btrfs_alloc_dummy_fs_info(PAGE_SIZE, PAGE_SIZE);
5988c2ecf20Sopenharmony_ci	if (!fs_info) {
5998c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_FS_INFO);
6008c2ecf20Sopenharmony_ci		return -ENOMEM;
6018c2ecf20Sopenharmony_ci	}
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	em_tree = kzalloc(sizeof(*em_tree), GFP_KERNEL);
6048c2ecf20Sopenharmony_ci	if (!em_tree) {
6058c2ecf20Sopenharmony_ci		ret = -ENOMEM;
6068c2ecf20Sopenharmony_ci		goto out;
6078c2ecf20Sopenharmony_ci	}
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	extent_map_tree_init(em_tree);
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	ret = test_case_1(fs_info, em_tree);
6128c2ecf20Sopenharmony_ci	if (ret)
6138c2ecf20Sopenharmony_ci		goto out;
6148c2ecf20Sopenharmony_ci	ret = test_case_2(fs_info, em_tree);
6158c2ecf20Sopenharmony_ci	if (ret)
6168c2ecf20Sopenharmony_ci		goto out;
6178c2ecf20Sopenharmony_ci	ret = test_case_3(fs_info, em_tree);
6188c2ecf20Sopenharmony_ci	if (ret)
6198c2ecf20Sopenharmony_ci		goto out;
6208c2ecf20Sopenharmony_ci	ret = test_case_4(fs_info, em_tree);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	test_msg("running rmap tests");
6238c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(rmap_tests); i++) {
6248c2ecf20Sopenharmony_ci		ret = test_rmap_block(fs_info, &rmap_tests[i]);
6258c2ecf20Sopenharmony_ci		if (ret)
6268c2ecf20Sopenharmony_ci			goto out;
6278c2ecf20Sopenharmony_ci	}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ciout:
6308c2ecf20Sopenharmony_ci	kfree(em_tree);
6318c2ecf20Sopenharmony_ci	btrfs_free_dummy_fs_info(fs_info);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	return ret;
6348c2ecf20Sopenharmony_ci}
635