Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2017 Oracle.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "btrfs-tests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "../ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "../volumes.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "../disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "../block-group.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) static void free_extent_map_tree(struct extent_map_tree *em_tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	while (!RB_EMPTY_ROOT(&em_tree->map.rb_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 		node = rb_first_cached(&em_tree->map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 		em = rb_entry(node, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 		remove_extent_mapping(em_tree, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #ifdef CONFIG_BTRFS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		if (refcount_read(&em->refs) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 			test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) "em leak: em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx) refs %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 				 em->start, em->len, em->block_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 				 em->block_len, refcount_read(&em->refs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 			refcount_set(&em->refs, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * Test scenario:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * Suppose that no extent map has been loaded into memory yet, there is a file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * extent [0, 16K), followed by another file extent [16K, 20K), two dio reads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * are entering btrfs_get_extent() concurrently, t1 is reading [8K, 16K), t2 is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * reading [0, 8K)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  *     t1                            t2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *  btrfs_get_extent()              btrfs_get_extent()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  *    -> lookup_extent_mapping()      ->lookup_extent_mapping()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *    -> add_extent_mapping(0, 16K)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *    -> return em
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  *                                    ->add_extent_mapping(0, 16K)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  *                                    -> #handle -EEXIST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static int test_case_1(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		struct extent_map_tree *em_tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	u64 start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	u64 len = SZ_8K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	/* Add [0, 16K) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	em->start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	em->len = SZ_16K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	em->block_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	em->block_len = SZ_16K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	ret = add_extent_mapping(em_tree, em, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		test_err("cannot add extent range [0, 16K)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	/* Add [16K, 20K) following [0, 16K)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	em->start = SZ_16K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	em->len = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	em->block_start = SZ_32K; /* avoid merging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	em->block_len = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	ret = add_extent_mapping(em_tree, em, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		test_err("cannot add extent range [16K, 20K)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	/* Add [0, 8K), should return [0, 16K) instead. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	em->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	em->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	em->block_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	em->block_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		test_err("case1 [%llu %llu]: ret %d", start, start + len, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (em &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	    (em->start != 0 || extent_map_end(em) != SZ_16K ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	     em->block_start != 0 || em->block_len != SZ_16K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) "case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			 start, start + len, ret, em->start, em->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			 em->block_start, em->block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	free_extent_map_tree(em_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  * Test scenario:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  * Reading the inline ending up with EEXIST, ie. read an inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  * extent and discard page cache and read it again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int test_case_2(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		struct extent_map_tree *em_tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	/* Add [0, 1K) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	em->start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	em->len = SZ_1K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	em->block_start = EXTENT_MAP_INLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	em->block_len = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	ret = add_extent_mapping(em_tree, em, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		test_err("cannot add extent range [0, 1K)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	/* Add [4K, 8K) following [0, 1K)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	em->start = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	em->len = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	em->block_start = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	em->block_len = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	ret = add_extent_mapping(em_tree, em, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		test_err("cannot add extent range [4K, 8K)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	/* Add [0, 1K) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	em->start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	em->len = SZ_1K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	em->block_start = EXTENT_MAP_INLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	em->block_len = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		test_err("case2 [0 1K]: ret %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	if (em &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	    (em->start != 0 || extent_map_end(em) != SZ_1K ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	     em->block_start != EXTENT_MAP_INLINE || em->block_len != (u64)-1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) "case2 [0 1K]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			 ret, em->start, em->len, em->block_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			 em->block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	free_extent_map_tree(em_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static int __test_case_3(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		struct extent_map_tree *em_tree, u64 start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	u64 len = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	/* Add [4K, 8K) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	em->start = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	em->len = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	em->block_start = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	em->block_len = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	ret = add_extent_mapping(em_tree, em, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		test_err("cannot add extent range [4K, 8K)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	/* Add [0, 16K) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	em->start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	em->len = SZ_16K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	em->block_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	em->block_len = SZ_16K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		test_err("case3 [0x%llx 0x%llx): ret %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			 start, start + len, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	 * Since bytes within em are contiguous, em->block_start is identical to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	 * em->start.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (em &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	    (start < em->start || start + len > extent_map_end(em) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	     em->start != em->block_start || em->len != em->block_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) "case3 [0x%llx 0x%llx): ret %d em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			 start, start + len, ret, em->start, em->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			 em->block_start, em->block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	free_extent_map_tree(em_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  * Test scenario:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  * Suppose that no extent map has been loaded into memory yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  * There is a file extent [0, 16K), two jobs are running concurrently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)  * against it, t1 is buffered writing to [4K, 8K) and t2 is doing dio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)  * read from [0, 4K) or [8K, 12K) or [12K, 16K).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  * t1 goes ahead of t2 and adds em [4K, 8K) into tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  *         t1                       t2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  *  cow_file_range()	     btrfs_get_extent()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  *                            -> lookup_extent_mapping()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)  *   -> add_extent_mapping()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  *                            -> add_extent_mapping()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static int test_case_3(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		struct extent_map_tree *em_tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	ret = __test_case_3(fs_info, em_tree, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	ret = __test_case_3(fs_info, em_tree, SZ_8K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	ret = __test_case_3(fs_info, em_tree, (12 * SZ_1K));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int __test_case_4(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		struct extent_map_tree *em_tree, u64 start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	u64 len = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	/* Add [0K, 8K) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	em->start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	em->len = SZ_8K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	em->block_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	em->block_len = SZ_8K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	ret = add_extent_mapping(em_tree, em, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		test_err("cannot add extent range [0, 8K)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	/* Add [8K, 32K) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	em->start = SZ_8K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	em->len = 24 * SZ_1K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	em->block_start = SZ_16K; /* avoid merging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	em->block_len = 24 * SZ_1K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	ret = add_extent_mapping(em_tree, em, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		test_err("cannot add extent range [8K, 32K)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	/* Add [0K, 32K) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	em->start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	em->len = SZ_32K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	em->block_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	em->block_len = SZ_32K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		test_err("case4 [0x%llx 0x%llx): ret %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			 start, len, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	if (em && (start < em->start || start + len > extent_map_end(em))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) "case4 [0x%llx 0x%llx): ret %d, added wrong em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			 start, len, ret, em->start, em->len, em->block_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			 em->block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	free_extent_map_tree(em_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)  * Test scenario:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  * Suppose that no extent map has been loaded into memory yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)  * There is a file extent [0, 32K), two jobs are running concurrently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)  * against it, t1 is doing dio write to [8K, 32K) and t2 is doing dio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)  * read from [0, 4K) or [4K, 8K).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)  * t1 goes ahead of t2 and splits em [0, 32K) to em [0K, 8K) and [8K 32K).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)  *         t1                                t2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)  *  btrfs_get_blocks_direct()	       btrfs_get_blocks_direct()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)  *   -> btrfs_get_extent()              -> btrfs_get_extent()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)  *       -> lookup_extent_mapping()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  *       -> add_extent_mapping()            -> lookup_extent_mapping()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  *          # load [0, 32K)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)  *   -> btrfs_new_extent_direct()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)  *       -> btrfs_drop_extent_cache()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)  *          # split [0, 32K)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)  *       -> add_extent_mapping()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)  *          # add [8K, 32K)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)  *                                          -> add_extent_mapping()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)  *                                             # handle -EEXIST when adding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)  *                                             # [0, 32K)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static int test_case_4(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		struct extent_map_tree *em_tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	ret = __test_case_4(fs_info, em_tree, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	ret = __test_case_4(fs_info, em_tree, SZ_4K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) struct rmap_test_vector {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	u64 raid_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	u64 physical_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	u64 data_stripe_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	u64 num_data_stripes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	u64 num_stripes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	/* Assume we won't have more than 5 physical stripes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	u64 data_stripe_phys_start[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	bool expected_mapped_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	/* Physical to logical addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	u64 mapped_logical[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) static int test_rmap_block(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			   struct rmap_test_vector *test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	struct map_lookup *map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	u64 *logical = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	int i, out_ndaddrs, out_stripe_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	map = kmalloc(map_lookup_size(test->num_stripes), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	if (!map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		kfree(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		test_std_err(TEST_ALLOC_EXTENT_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	/* Start at 4GiB logical address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	em->start = SZ_4G;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	em->len = test->data_stripe_size * test->num_data_stripes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	em->block_len = em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	em->orig_block_len = test->data_stripe_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	em->map_lookup = map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	map->num_stripes = test->num_stripes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	map->stripe_len = BTRFS_STRIPE_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	map->type = test->raid_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	for (i = 0; i < map->num_stripes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		struct btrfs_device *dev = btrfs_alloc_dummy_device(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		if (IS_ERR(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 			test_err("cannot allocate device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			ret = PTR_ERR(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		map->stripes[i].dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		map->stripes[i].physical = test->data_stripe_phys_start[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	write_lock(&fs_info->mapping_tree.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	ret = add_extent_mapping(&fs_info->mapping_tree, em, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	write_unlock(&fs_info->mapping_tree.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		test_err("error adding block group mapping to mapping tree");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	ret = btrfs_rmap_block(fs_info, em->start, btrfs_sb_offset(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 			       &logical, &out_ndaddrs, &out_stripe_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	if (ret || (out_ndaddrs == 0 && test->expected_mapped_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		test_err("didn't rmap anything but expected %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 			 test->expected_mapped_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (out_stripe_len != BTRFS_STRIPE_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		test_err("calculated stripe length doesn't match");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	if (out_ndaddrs != test->expected_mapped_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		for (i = 0; i < out_ndaddrs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 			test_msg("mapped %llu", logical[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		test_err("unexpected number of mapped addresses: %d", out_ndaddrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	for (i = 0; i < out_ndaddrs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		if (logical[i] != test->mapped_logical[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 			test_err("unexpected logical address mapped");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	write_lock(&fs_info->mapping_tree.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	remove_extent_mapping(&fs_info->mapping_tree, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	write_unlock(&fs_info->mapping_tree.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	/* For us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	/* For the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	kfree(logical);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) int btrfs_test_extent_map(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	struct btrfs_fs_info *fs_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	struct extent_map_tree *em_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	int ret = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	struct rmap_test_vector rmap_tests[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 			 * Test a chunk with 2 data stripes one of which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			 * interesects the physical address of the super block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 			 * is correctly recognised.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 			.raid_type = BTRFS_BLOCK_GROUP_RAID1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			.physical_start = SZ_64M - SZ_4M,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			.data_stripe_size = SZ_256M,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 			.num_data_stripes = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			.num_stripes = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 			.data_stripe_phys_start =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 				{SZ_64M - SZ_4M, SZ_64M - SZ_4M + SZ_256M},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 			.expected_mapped_addr = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 			.mapped_logical= {SZ_4G + SZ_4M}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			 * Test that out-of-range physical addresses are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 			 * ignored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 			 /* SINGLE chunk type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 			.raid_type = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 			.physical_start = SZ_4G,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 			.data_stripe_size = SZ_256M,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 			.num_data_stripes = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 			.num_stripes = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 			.data_stripe_phys_start = {SZ_256M},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 			.expected_mapped_addr = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 			.mapped_logical = {0}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	test_msg("running extent_map tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	 * Note: the fs_info is not set up completely, we only need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	 * fs_info::fsid for the tracepoint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	fs_info = btrfs_alloc_dummy_fs_info(PAGE_SIZE, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	if (!fs_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		test_std_err(TEST_ALLOC_FS_INFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	em_tree = kzalloc(sizeof(*em_tree), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	if (!em_tree) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	extent_map_tree_init(em_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	ret = test_case_1(fs_info, em_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	ret = test_case_2(fs_info, em_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	ret = test_case_3(fs_info, em_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	ret = test_case_4(fs_info, em_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	test_msg("running rmap tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	for (i = 0; i < ARRAY_SIZE(rmap_tests); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		ret = test_rmap_block(fs_info, &rmap_tests[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	kfree(em_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	btrfs_free_dummy_fs_info(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }