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 1572b28077SLiu Bo while (!RB_EMPTY_ROOT(&em_tree->map)) { 1672b28077SLiu Bo node = rb_first(&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) { 22*3c7251f2SDavid Sterba test_err( 23*3c7251f2SDavid 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 */ 500e08eb9bSDavid Sterba static void 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) 6072b28077SLiu Bo /* Skip the test on error. */ 6172b28077SLiu Bo return; 6272b28077SLiu Bo 6372b28077SLiu Bo /* Add [0, 16K) */ 6472b28077SLiu Bo em->start = 0; 6572b28077SLiu Bo em->len = SZ_16K; 6672b28077SLiu Bo em->block_start = 0; 6772b28077SLiu Bo em->block_len = SZ_16K; 6872b28077SLiu Bo ret = add_extent_mapping(em_tree, em, 0); 6972b28077SLiu Bo ASSERT(ret == 0); 7072b28077SLiu Bo free_extent_map(em); 7172b28077SLiu Bo 7272b28077SLiu Bo /* Add [16K, 20K) following [0, 16K) */ 7372b28077SLiu Bo em = alloc_extent_map(); 7472b28077SLiu Bo if (!em) 7572b28077SLiu Bo goto out; 7672b28077SLiu Bo 7772b28077SLiu Bo em->start = SZ_16K; 7872b28077SLiu Bo em->len = SZ_4K; 7972b28077SLiu Bo em->block_start = SZ_32K; /* avoid merging */ 8072b28077SLiu Bo em->block_len = SZ_4K; 8172b28077SLiu Bo ret = add_extent_mapping(em_tree, em, 0); 8272b28077SLiu Bo ASSERT(ret == 0); 8372b28077SLiu Bo free_extent_map(em); 8472b28077SLiu Bo 8572b28077SLiu Bo em = alloc_extent_map(); 8672b28077SLiu Bo if (!em) 8772b28077SLiu Bo goto out; 8872b28077SLiu Bo 8972b28077SLiu Bo /* Add [0, 8K), should return [0, 16K) instead. */ 9072b28077SLiu Bo em->start = start; 9172b28077SLiu Bo em->len = len; 9272b28077SLiu Bo em->block_start = start; 9372b28077SLiu Bo em->block_len = len; 94f46b24c9SDavid Sterba ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); 9572b28077SLiu Bo if (ret) 96*3c7251f2SDavid Sterba test_err("case1 [%llu %llu]: ret %d", start, start + len, ret); 9772b28077SLiu Bo if (em && 9872b28077SLiu Bo (em->start != 0 || extent_map_end(em) != SZ_16K || 9972b28077SLiu Bo em->block_start != 0 || em->block_len != SZ_16K)) 100*3c7251f2SDavid Sterba test_err( 101*3c7251f2SDavid Sterba "case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu", 10272b28077SLiu Bo start, start + len, ret, em->start, em->len, 10372b28077SLiu Bo em->block_start, em->block_len); 10472b28077SLiu Bo free_extent_map(em); 10572b28077SLiu Bo out: 10672b28077SLiu Bo /* free memory */ 10772b28077SLiu Bo free_extent_map_tree(em_tree); 10872b28077SLiu Bo } 10972b28077SLiu Bo 11072b28077SLiu Bo /* 11172b28077SLiu Bo * Test scenario: 11272b28077SLiu Bo * 11372b28077SLiu Bo * Reading the inline ending up with EEXIST, ie. read an inline 11472b28077SLiu Bo * extent and discard page cache and read it again. 11572b28077SLiu Bo */ 1160e08eb9bSDavid Sterba static void test_case_2(struct btrfs_fs_info *fs_info, 1170e08eb9bSDavid Sterba struct extent_map_tree *em_tree) 11872b28077SLiu Bo { 11972b28077SLiu Bo struct extent_map *em; 12072b28077SLiu Bo int ret; 12172b28077SLiu Bo 12272b28077SLiu Bo em = alloc_extent_map(); 12372b28077SLiu Bo if (!em) 12472b28077SLiu Bo /* Skip the test on error. */ 12572b28077SLiu Bo return; 12672b28077SLiu Bo 12772b28077SLiu Bo /* Add [0, 1K) */ 12872b28077SLiu Bo em->start = 0; 12972b28077SLiu Bo em->len = SZ_1K; 13072b28077SLiu Bo em->block_start = EXTENT_MAP_INLINE; 13172b28077SLiu Bo em->block_len = (u64)-1; 13272b28077SLiu Bo ret = add_extent_mapping(em_tree, em, 0); 13372b28077SLiu Bo ASSERT(ret == 0); 13472b28077SLiu Bo free_extent_map(em); 13572b28077SLiu Bo 13672b28077SLiu Bo /* Add [4K, 4K) following [0, 1K) */ 13772b28077SLiu Bo em = alloc_extent_map(); 13872b28077SLiu Bo if (!em) 13972b28077SLiu Bo goto out; 14072b28077SLiu Bo 14172b28077SLiu Bo em->start = SZ_4K; 14272b28077SLiu Bo em->len = SZ_4K; 14372b28077SLiu Bo em->block_start = SZ_4K; 14472b28077SLiu Bo em->block_len = SZ_4K; 14572b28077SLiu Bo ret = add_extent_mapping(em_tree, em, 0); 14672b28077SLiu Bo ASSERT(ret == 0); 14772b28077SLiu Bo free_extent_map(em); 14872b28077SLiu Bo 14972b28077SLiu Bo em = alloc_extent_map(); 15072b28077SLiu Bo if (!em) 15172b28077SLiu Bo goto out; 15272b28077SLiu Bo 15372b28077SLiu Bo /* Add [0, 1K) */ 15472b28077SLiu Bo em->start = 0; 15572b28077SLiu Bo em->len = SZ_1K; 15672b28077SLiu Bo em->block_start = EXTENT_MAP_INLINE; 15772b28077SLiu Bo em->block_len = (u64)-1; 158f46b24c9SDavid Sterba ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); 15972b28077SLiu Bo if (ret) 160*3c7251f2SDavid Sterba test_err("case2 [0 1K]: ret %d", ret); 16172b28077SLiu Bo if (em && 16272b28077SLiu Bo (em->start != 0 || extent_map_end(em) != SZ_1K || 16372b28077SLiu Bo em->block_start != EXTENT_MAP_INLINE || em->block_len != (u64)-1)) 164*3c7251f2SDavid Sterba test_err( 165*3c7251f2SDavid Sterba "case2 [0 1K]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu", 16672b28077SLiu Bo ret, em->start, em->len, em->block_start, 16772b28077SLiu Bo em->block_len); 16872b28077SLiu Bo free_extent_map(em); 16972b28077SLiu Bo out: 17072b28077SLiu Bo /* free memory */ 17172b28077SLiu Bo free_extent_map_tree(em_tree); 17272b28077SLiu Bo } 17372b28077SLiu Bo 1740e08eb9bSDavid Sterba static void __test_case_3(struct btrfs_fs_info *fs_info, 1750e08eb9bSDavid Sterba struct extent_map_tree *em_tree, u64 start) 176fd87526fSLiu Bo { 177fd87526fSLiu Bo struct extent_map *em; 178fd87526fSLiu Bo u64 len = SZ_4K; 179fd87526fSLiu Bo int ret; 180fd87526fSLiu Bo 181fd87526fSLiu Bo em = alloc_extent_map(); 182fd87526fSLiu Bo if (!em) 183fd87526fSLiu Bo /* Skip this test on error. */ 184fd87526fSLiu Bo return; 185fd87526fSLiu Bo 186fd87526fSLiu Bo /* Add [4K, 8K) */ 187fd87526fSLiu Bo em->start = SZ_4K; 188fd87526fSLiu Bo em->len = SZ_4K; 189fd87526fSLiu Bo em->block_start = SZ_4K; 190fd87526fSLiu Bo em->block_len = SZ_4K; 191fd87526fSLiu Bo ret = add_extent_mapping(em_tree, em, 0); 192fd87526fSLiu Bo ASSERT(ret == 0); 193fd87526fSLiu Bo free_extent_map(em); 194fd87526fSLiu Bo 195fd87526fSLiu Bo em = alloc_extent_map(); 196fd87526fSLiu Bo if (!em) 197fd87526fSLiu Bo goto out; 198fd87526fSLiu Bo 199fd87526fSLiu Bo /* Add [0, 16K) */ 200fd87526fSLiu Bo em->start = 0; 201fd87526fSLiu Bo em->len = SZ_16K; 202fd87526fSLiu Bo em->block_start = 0; 203fd87526fSLiu Bo em->block_len = SZ_16K; 204f46b24c9SDavid Sterba ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); 205fd87526fSLiu Bo if (ret) 206*3c7251f2SDavid Sterba test_err("case3 [0x%llx 0x%llx): ret %d", 207fd87526fSLiu Bo start, start + len, ret); 208fd87526fSLiu Bo /* 209fd87526fSLiu Bo * Since bytes within em are contiguous, em->block_start is identical to 210fd87526fSLiu Bo * em->start. 211fd87526fSLiu Bo */ 212fd87526fSLiu Bo if (em && 213fd87526fSLiu Bo (start < em->start || start + len > extent_map_end(em) || 214fd87526fSLiu Bo em->start != em->block_start || em->len != em->block_len)) 215*3c7251f2SDavid Sterba test_err( 216*3c7251f2SDavid Sterba "case3 [0x%llx 0x%llx): ret %d em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)", 217fd87526fSLiu Bo start, start + len, ret, em->start, em->len, 218fd87526fSLiu Bo em->block_start, em->block_len); 219fd87526fSLiu Bo free_extent_map(em); 220fd87526fSLiu Bo out: 221fd87526fSLiu Bo /* free memory */ 222fd87526fSLiu Bo free_extent_map_tree(em_tree); 223fd87526fSLiu Bo } 224fd87526fSLiu Bo 225fd87526fSLiu Bo /* 226fd87526fSLiu Bo * Test scenario: 227fd87526fSLiu Bo * 228fd87526fSLiu Bo * Suppose that no extent map has been loaded into memory yet. 229fd87526fSLiu Bo * There is a file extent [0, 16K), two jobs are running concurrently 230fd87526fSLiu Bo * against it, t1 is buffered writing to [4K, 8K) and t2 is doing dio 231fd87526fSLiu Bo * read from [0, 4K) or [8K, 12K) or [12K, 16K). 232fd87526fSLiu Bo * 233fd87526fSLiu Bo * t1 goes ahead of t2 and adds em [4K, 8K) into tree. 234fd87526fSLiu Bo * 235fd87526fSLiu Bo * t1 t2 236fd87526fSLiu Bo * cow_file_range() btrfs_get_extent() 237fd87526fSLiu Bo * -> lookup_extent_mapping() 238fd87526fSLiu Bo * -> add_extent_mapping() 239fd87526fSLiu Bo * -> add_extent_mapping() 240fd87526fSLiu Bo */ 2410e08eb9bSDavid Sterba static void test_case_3(struct btrfs_fs_info *fs_info, 2420e08eb9bSDavid Sterba struct extent_map_tree *em_tree) 243fd87526fSLiu Bo { 2440e08eb9bSDavid Sterba __test_case_3(fs_info, em_tree, 0); 2450e08eb9bSDavid Sterba __test_case_3(fs_info, em_tree, SZ_8K); 2460e08eb9bSDavid Sterba __test_case_3(fs_info, em_tree, (12 * 1024ULL)); 247fd87526fSLiu Bo } 248fd87526fSLiu Bo 2490e08eb9bSDavid Sterba static void __test_case_4(struct btrfs_fs_info *fs_info, 2500e08eb9bSDavid Sterba struct extent_map_tree *em_tree, u64 start) 251cd77f4f8SLiu Bo { 252cd77f4f8SLiu Bo struct extent_map *em; 253cd77f4f8SLiu Bo u64 len = SZ_4K; 254cd77f4f8SLiu Bo int ret; 255cd77f4f8SLiu Bo 256cd77f4f8SLiu Bo em = alloc_extent_map(); 257cd77f4f8SLiu Bo if (!em) 258cd77f4f8SLiu Bo /* Skip this test on error. */ 259cd77f4f8SLiu Bo return; 260cd77f4f8SLiu Bo 261cd77f4f8SLiu Bo /* Add [0K, 8K) */ 262cd77f4f8SLiu Bo em->start = 0; 263cd77f4f8SLiu Bo em->len = SZ_8K; 264cd77f4f8SLiu Bo em->block_start = 0; 265cd77f4f8SLiu Bo em->block_len = SZ_8K; 266cd77f4f8SLiu Bo ret = add_extent_mapping(em_tree, em, 0); 267cd77f4f8SLiu Bo ASSERT(ret == 0); 268cd77f4f8SLiu Bo free_extent_map(em); 269cd77f4f8SLiu Bo 270cd77f4f8SLiu Bo em = alloc_extent_map(); 271cd77f4f8SLiu Bo if (!em) 272cd77f4f8SLiu Bo goto out; 273cd77f4f8SLiu Bo 274cd77f4f8SLiu Bo /* Add [8K, 24K) */ 275cd77f4f8SLiu Bo em->start = SZ_8K; 276cd77f4f8SLiu Bo em->len = 24 * 1024ULL; 277cd77f4f8SLiu Bo em->block_start = SZ_16K; /* avoid merging */ 278cd77f4f8SLiu Bo em->block_len = 24 * 1024ULL; 279cd77f4f8SLiu Bo ret = add_extent_mapping(em_tree, em, 0); 280cd77f4f8SLiu Bo ASSERT(ret == 0); 281cd77f4f8SLiu Bo free_extent_map(em); 282cd77f4f8SLiu Bo 283cd77f4f8SLiu Bo em = alloc_extent_map(); 284cd77f4f8SLiu Bo if (!em) 285cd77f4f8SLiu Bo goto out; 286cd77f4f8SLiu Bo /* Add [0K, 32K) */ 287cd77f4f8SLiu Bo em->start = 0; 288cd77f4f8SLiu Bo em->len = SZ_32K; 289cd77f4f8SLiu Bo em->block_start = 0; 290cd77f4f8SLiu Bo em->block_len = SZ_32K; 291f46b24c9SDavid Sterba ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); 292cd77f4f8SLiu Bo if (ret) 293*3c7251f2SDavid Sterba test_err("case4 [0x%llx 0x%llx): ret %d", 294cd77f4f8SLiu Bo start, len, ret); 295cd77f4f8SLiu Bo if (em && 296cd77f4f8SLiu Bo (start < em->start || start + len > extent_map_end(em))) 297*3c7251f2SDavid Sterba test_err( 298*3c7251f2SDavid 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)", 299cd77f4f8SLiu Bo start, len, ret, em->start, em->len, em->block_start, 300cd77f4f8SLiu Bo em->block_len); 301cd77f4f8SLiu Bo free_extent_map(em); 302cd77f4f8SLiu Bo out: 303cd77f4f8SLiu Bo /* free memory */ 304cd77f4f8SLiu Bo free_extent_map_tree(em_tree); 305cd77f4f8SLiu Bo } 306cd77f4f8SLiu Bo 307cd77f4f8SLiu Bo /* 308cd77f4f8SLiu Bo * Test scenario: 309cd77f4f8SLiu Bo * 310cd77f4f8SLiu Bo * Suppose that no extent map has been loaded into memory yet. 311cd77f4f8SLiu Bo * There is a file extent [0, 32K), two jobs are running concurrently 312cd77f4f8SLiu Bo * against it, t1 is doing dio write to [8K, 32K) and t2 is doing dio 313cd77f4f8SLiu Bo * read from [0, 4K) or [4K, 8K). 314cd77f4f8SLiu Bo * 315cd77f4f8SLiu Bo * t1 goes ahead of t2 and splits em [0, 32K) to em [0K, 8K) and [8K 32K). 316cd77f4f8SLiu Bo * 317cd77f4f8SLiu Bo * t1 t2 318cd77f4f8SLiu Bo * btrfs_get_blocks_direct() btrfs_get_blocks_direct() 319cd77f4f8SLiu Bo * -> btrfs_get_extent() -> btrfs_get_extent() 320cd77f4f8SLiu Bo * -> lookup_extent_mapping() 321cd77f4f8SLiu Bo * -> add_extent_mapping() -> lookup_extent_mapping() 322cd77f4f8SLiu Bo * # load [0, 32K) 323cd77f4f8SLiu Bo * -> btrfs_new_extent_direct() 324cd77f4f8SLiu Bo * -> btrfs_drop_extent_cache() 325cd77f4f8SLiu Bo * # split [0, 32K) 326cd77f4f8SLiu Bo * -> add_extent_mapping() 327cd77f4f8SLiu Bo * # add [8K, 32K) 328cd77f4f8SLiu Bo * -> add_extent_mapping() 329cd77f4f8SLiu Bo * # handle -EEXIST when adding 330cd77f4f8SLiu Bo * # [0, 32K) 331cd77f4f8SLiu Bo */ 3320e08eb9bSDavid Sterba static void test_case_4(struct btrfs_fs_info *fs_info, 3330e08eb9bSDavid Sterba struct extent_map_tree *em_tree) 334cd77f4f8SLiu Bo { 3350e08eb9bSDavid Sterba __test_case_4(fs_info, em_tree, 0); 3360e08eb9bSDavid Sterba __test_case_4(fs_info, em_tree, SZ_4K); 337cd77f4f8SLiu Bo } 338cd77f4f8SLiu Bo 33997dc231eSColin Ian King int btrfs_test_extent_map(void) 34072b28077SLiu Bo { 3410e08eb9bSDavid Sterba struct btrfs_fs_info *fs_info = NULL; 34272b28077SLiu Bo struct extent_map_tree *em_tree; 34372b28077SLiu Bo 344*3c7251f2SDavid Sterba test_msg("running extent_map tests\n"); 34572b28077SLiu Bo 3460e08eb9bSDavid Sterba /* 3470e08eb9bSDavid Sterba * Note: the fs_info is not set up completely, we only need 3480e08eb9bSDavid Sterba * fs_info::fsid for the tracepoint. 3490e08eb9bSDavid Sterba */ 3500e08eb9bSDavid Sterba fs_info = btrfs_alloc_dummy_fs_info(PAGE_SIZE, PAGE_SIZE); 3510e08eb9bSDavid Sterba if (!fs_info) { 3520e08eb9bSDavid Sterba test_msg("Couldn't allocate dummy fs info\n"); 3530e08eb9bSDavid Sterba return -ENOMEM; 3540e08eb9bSDavid Sterba } 3550e08eb9bSDavid Sterba 35672b28077SLiu Bo em_tree = kzalloc(sizeof(*em_tree), GFP_KERNEL); 35772b28077SLiu Bo if (!em_tree) 35872b28077SLiu Bo /* Skip the test on error. */ 3590e08eb9bSDavid Sterba goto out; 36072b28077SLiu Bo 36172b28077SLiu Bo extent_map_tree_init(em_tree); 36272b28077SLiu Bo 3630e08eb9bSDavid Sterba test_case_1(fs_info, em_tree); 3640e08eb9bSDavid Sterba test_case_2(fs_info, em_tree); 3650e08eb9bSDavid Sterba test_case_3(fs_info, em_tree); 3660e08eb9bSDavid Sterba test_case_4(fs_info, em_tree); 36772b28077SLiu Bo 36872b28077SLiu Bo kfree(em_tree); 3690e08eb9bSDavid Sterba out: 3700e08eb9bSDavid Sterba btrfs_free_dummy_fs_info(fs_info); 3710e08eb9bSDavid Sterba 37272b28077SLiu Bo return 0; 37372b28077SLiu Bo } 374