18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2013 Fusion IO. All rights reserved. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/slab.h> 78c2ecf20Sopenharmony_ci#include "btrfs-tests.h" 88c2ecf20Sopenharmony_ci#include "../ctree.h" 98c2ecf20Sopenharmony_ci#include "../disk-io.h" 108c2ecf20Sopenharmony_ci#include "../free-space-cache.h" 118c2ecf20Sopenharmony_ci#include "../block-group.h" 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#define BITS_PER_BITMAP (PAGE_SIZE * 8UL) 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci/* 168c2ecf20Sopenharmony_ci * This test just does basic sanity checking, making sure we can add an extent 178c2ecf20Sopenharmony_ci * entry and remove space from either end and the middle, and make sure we can 188c2ecf20Sopenharmony_ci * remove space that covers adjacent extent entries. 198c2ecf20Sopenharmony_ci */ 208c2ecf20Sopenharmony_cistatic int test_extents(struct btrfs_block_group *cache) 218c2ecf20Sopenharmony_ci{ 228c2ecf20Sopenharmony_ci int ret = 0; 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci test_msg("running extent only tests"); 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci /* First just make sure we can remove an entire entry */ 278c2ecf20Sopenharmony_ci ret = btrfs_add_free_space(cache, 0, SZ_4M); 288c2ecf20Sopenharmony_ci if (ret) { 298c2ecf20Sopenharmony_ci test_err("error adding initial extents %d", ret); 308c2ecf20Sopenharmony_ci return ret; 318c2ecf20Sopenharmony_ci } 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, 0, SZ_4M); 348c2ecf20Sopenharmony_ci if (ret) { 358c2ecf20Sopenharmony_ci test_err("error removing extent %d", ret); 368c2ecf20Sopenharmony_ci return ret; 378c2ecf20Sopenharmony_ci } 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci if (test_check_exists(cache, 0, SZ_4M)) { 408c2ecf20Sopenharmony_ci test_err("full remove left some lingering space"); 418c2ecf20Sopenharmony_ci return -1; 428c2ecf20Sopenharmony_ci } 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci /* Ok edge and middle cases now */ 458c2ecf20Sopenharmony_ci ret = btrfs_add_free_space(cache, 0, SZ_4M); 468c2ecf20Sopenharmony_ci if (ret) { 478c2ecf20Sopenharmony_ci test_err("error adding half extent %d", ret); 488c2ecf20Sopenharmony_ci return ret; 498c2ecf20Sopenharmony_ci } 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_1M); 528c2ecf20Sopenharmony_ci if (ret) { 538c2ecf20Sopenharmony_ci test_err("error removing tail end %d", ret); 548c2ecf20Sopenharmony_ci return ret; 558c2ecf20Sopenharmony_ci } 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, 0, SZ_1M); 588c2ecf20Sopenharmony_ci if (ret) { 598c2ecf20Sopenharmony_ci test_err("error removing front end %d", ret); 608c2ecf20Sopenharmony_ci return ret; 618c2ecf20Sopenharmony_ci } 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, SZ_2M, 4096); 648c2ecf20Sopenharmony_ci if (ret) { 658c2ecf20Sopenharmony_ci test_err("error removing middle piece %d", ret); 668c2ecf20Sopenharmony_ci return ret; 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci if (test_check_exists(cache, 0, SZ_1M)) { 708c2ecf20Sopenharmony_ci test_err("still have space at the front"); 718c2ecf20Sopenharmony_ci return -1; 728c2ecf20Sopenharmony_ci } 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci if (test_check_exists(cache, SZ_2M, 4096)) { 758c2ecf20Sopenharmony_ci test_err("still have space in the middle"); 768c2ecf20Sopenharmony_ci return -1; 778c2ecf20Sopenharmony_ci } 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci if (test_check_exists(cache, 3 * SZ_1M, SZ_1M)) { 808c2ecf20Sopenharmony_ci test_err("still have space at the end"); 818c2ecf20Sopenharmony_ci return -1; 828c2ecf20Sopenharmony_ci } 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci /* Cleanup */ 858c2ecf20Sopenharmony_ci __btrfs_remove_free_space_cache(cache->free_space_ctl); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci return 0; 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistatic int test_bitmaps(struct btrfs_block_group *cache, u32 sectorsize) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci u64 next_bitmap_offset; 938c2ecf20Sopenharmony_ci int ret; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci test_msg("running bitmap only tests"); 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, 0, SZ_4M, 1); 988c2ecf20Sopenharmony_ci if (ret) { 998c2ecf20Sopenharmony_ci test_err("couldn't create a bitmap entry %d", ret); 1008c2ecf20Sopenharmony_ci return ret; 1018c2ecf20Sopenharmony_ci } 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, 0, SZ_4M); 1048c2ecf20Sopenharmony_ci if (ret) { 1058c2ecf20Sopenharmony_ci test_err("error removing bitmap full range %d", ret); 1068c2ecf20Sopenharmony_ci return ret; 1078c2ecf20Sopenharmony_ci } 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci if (test_check_exists(cache, 0, SZ_4M)) { 1108c2ecf20Sopenharmony_ci test_err("left some space in bitmap"); 1118c2ecf20Sopenharmony_ci return -1; 1128c2ecf20Sopenharmony_ci } 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, 0, SZ_4M, 1); 1158c2ecf20Sopenharmony_ci if (ret) { 1168c2ecf20Sopenharmony_ci test_err("couldn't add to our bitmap entry %d", ret); 1178c2ecf20Sopenharmony_ci return ret; 1188c2ecf20Sopenharmony_ci } 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, SZ_1M, SZ_2M); 1218c2ecf20Sopenharmony_ci if (ret) { 1228c2ecf20Sopenharmony_ci test_err("couldn't remove middle chunk %d", ret); 1238c2ecf20Sopenharmony_ci return ret; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* 1278c2ecf20Sopenharmony_ci * The first bitmap we have starts at offset 0 so the next one is just 1288c2ecf20Sopenharmony_ci * at the end of the first bitmap. 1298c2ecf20Sopenharmony_ci */ 1308c2ecf20Sopenharmony_ci next_bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci /* Test a bit straddling two bitmaps */ 1338c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, next_bitmap_offset - SZ_2M, 1348c2ecf20Sopenharmony_ci SZ_4M, 1); 1358c2ecf20Sopenharmony_ci if (ret) { 1368c2ecf20Sopenharmony_ci test_err("couldn't add space that straddles two bitmaps %d", 1378c2ecf20Sopenharmony_ci ret); 1388c2ecf20Sopenharmony_ci return ret; 1398c2ecf20Sopenharmony_ci } 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, next_bitmap_offset - SZ_1M, SZ_2M); 1428c2ecf20Sopenharmony_ci if (ret) { 1438c2ecf20Sopenharmony_ci test_err("couldn't remove overlapping space %d", ret); 1448c2ecf20Sopenharmony_ci return ret; 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci if (test_check_exists(cache, next_bitmap_offset - SZ_1M, SZ_2M)) { 1488c2ecf20Sopenharmony_ci test_err("left some space when removing overlapping"); 1498c2ecf20Sopenharmony_ci return -1; 1508c2ecf20Sopenharmony_ci } 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci __btrfs_remove_free_space_cache(cache->free_space_ctl); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci return 0; 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci/* This is the high grade jackassery */ 1588c2ecf20Sopenharmony_cistatic int test_bitmaps_and_extents(struct btrfs_block_group *cache, 1598c2ecf20Sopenharmony_ci u32 sectorsize) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci u64 bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize); 1628c2ecf20Sopenharmony_ci int ret; 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci test_msg("running bitmap and extent tests"); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci /* 1678c2ecf20Sopenharmony_ci * First let's do something simple, an extent at the same offset as the 1688c2ecf20Sopenharmony_ci * bitmap, but the free space completely in the extent and then 1698c2ecf20Sopenharmony_ci * completely in the bitmap. 1708c2ecf20Sopenharmony_ci */ 1718c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, SZ_4M, SZ_1M, 1); 1728c2ecf20Sopenharmony_ci if (ret) { 1738c2ecf20Sopenharmony_ci test_err("couldn't create bitmap entry %d", ret); 1748c2ecf20Sopenharmony_ci return ret; 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, 0, SZ_1M, 0); 1788c2ecf20Sopenharmony_ci if (ret) { 1798c2ecf20Sopenharmony_ci test_err("couldn't add extent entry %d", ret); 1808c2ecf20Sopenharmony_ci return ret; 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, 0, SZ_1M); 1848c2ecf20Sopenharmony_ci if (ret) { 1858c2ecf20Sopenharmony_ci test_err("couldn't remove extent entry %d", ret); 1868c2ecf20Sopenharmony_ci return ret; 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci if (test_check_exists(cache, 0, SZ_1M)) { 1908c2ecf20Sopenharmony_ci test_err("left remnants after our remove"); 1918c2ecf20Sopenharmony_ci return -1; 1928c2ecf20Sopenharmony_ci } 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci /* Now to add back the extent entry and remove from the bitmap */ 1958c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, 0, SZ_1M, 0); 1968c2ecf20Sopenharmony_ci if (ret) { 1978c2ecf20Sopenharmony_ci test_err("couldn't re-add extent entry %d", ret); 1988c2ecf20Sopenharmony_ci return ret; 1998c2ecf20Sopenharmony_ci } 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, SZ_4M, SZ_1M); 2028c2ecf20Sopenharmony_ci if (ret) { 2038c2ecf20Sopenharmony_ci test_err("couldn't remove from bitmap %d", ret); 2048c2ecf20Sopenharmony_ci return ret; 2058c2ecf20Sopenharmony_ci } 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci if (test_check_exists(cache, SZ_4M, SZ_1M)) { 2088c2ecf20Sopenharmony_ci test_err("left remnants in the bitmap"); 2098c2ecf20Sopenharmony_ci return -1; 2108c2ecf20Sopenharmony_ci } 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci /* 2138c2ecf20Sopenharmony_ci * Ok so a little more evil, extent entry and bitmap at the same offset, 2148c2ecf20Sopenharmony_ci * removing an overlapping chunk. 2158c2ecf20Sopenharmony_ci */ 2168c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, SZ_1M, SZ_4M, 1); 2178c2ecf20Sopenharmony_ci if (ret) { 2188c2ecf20Sopenharmony_ci test_err("couldn't add to a bitmap %d", ret); 2198c2ecf20Sopenharmony_ci return ret; 2208c2ecf20Sopenharmony_ci } 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, SZ_512K, 3 * SZ_1M); 2238c2ecf20Sopenharmony_ci if (ret) { 2248c2ecf20Sopenharmony_ci test_err("couldn't remove overlapping space %d", ret); 2258c2ecf20Sopenharmony_ci return ret; 2268c2ecf20Sopenharmony_ci } 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci if (test_check_exists(cache, SZ_512K, 3 * SZ_1M)) { 2298c2ecf20Sopenharmony_ci test_err("left over pieces after removing overlapping"); 2308c2ecf20Sopenharmony_ci return -1; 2318c2ecf20Sopenharmony_ci } 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci __btrfs_remove_free_space_cache(cache->free_space_ctl); 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci /* Now with the extent entry offset into the bitmap */ 2368c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, SZ_4M, SZ_4M, 1); 2378c2ecf20Sopenharmony_ci if (ret) { 2388c2ecf20Sopenharmony_ci test_err("couldn't add space to the bitmap %d", ret); 2398c2ecf20Sopenharmony_ci return ret; 2408c2ecf20Sopenharmony_ci } 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, SZ_2M, SZ_2M, 0); 2438c2ecf20Sopenharmony_ci if (ret) { 2448c2ecf20Sopenharmony_ci test_err("couldn't add extent to the cache %d", ret); 2458c2ecf20Sopenharmony_ci return ret; 2468c2ecf20Sopenharmony_ci } 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_4M); 2498c2ecf20Sopenharmony_ci if (ret) { 2508c2ecf20Sopenharmony_ci test_err("problem removing overlapping space %d", ret); 2518c2ecf20Sopenharmony_ci return ret; 2528c2ecf20Sopenharmony_ci } 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci if (test_check_exists(cache, 3 * SZ_1M, SZ_4M)) { 2558c2ecf20Sopenharmony_ci test_err("left something behind when removing space"); 2568c2ecf20Sopenharmony_ci return -1; 2578c2ecf20Sopenharmony_ci } 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci /* 2608c2ecf20Sopenharmony_ci * This has blown up in the past, the extent entry starts before the 2618c2ecf20Sopenharmony_ci * bitmap entry, but we're trying to remove an offset that falls 2628c2ecf20Sopenharmony_ci * completely within the bitmap range and is in both the extent entry 2638c2ecf20Sopenharmony_ci * and the bitmap entry, looks like this 2648c2ecf20Sopenharmony_ci * 2658c2ecf20Sopenharmony_ci * [ extent ] 2668c2ecf20Sopenharmony_ci * [ bitmap ] 2678c2ecf20Sopenharmony_ci * [ del ] 2688c2ecf20Sopenharmony_ci */ 2698c2ecf20Sopenharmony_ci __btrfs_remove_free_space_cache(cache->free_space_ctl); 2708c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, bitmap_offset + SZ_4M, SZ_4M, 1); 2718c2ecf20Sopenharmony_ci if (ret) { 2728c2ecf20Sopenharmony_ci test_err("couldn't add bitmap %d", ret); 2738c2ecf20Sopenharmony_ci return ret; 2748c2ecf20Sopenharmony_ci } 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, bitmap_offset - SZ_1M, 2778c2ecf20Sopenharmony_ci 5 * SZ_1M, 0); 2788c2ecf20Sopenharmony_ci if (ret) { 2798c2ecf20Sopenharmony_ci test_err("couldn't add extent entry %d", ret); 2808c2ecf20Sopenharmony_ci return ret; 2818c2ecf20Sopenharmony_ci } 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, bitmap_offset + SZ_1M, 5 * SZ_1M); 2848c2ecf20Sopenharmony_ci if (ret) { 2858c2ecf20Sopenharmony_ci test_err("failed to free our space %d", ret); 2868c2ecf20Sopenharmony_ci return ret; 2878c2ecf20Sopenharmony_ci } 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci if (test_check_exists(cache, bitmap_offset + SZ_1M, 5 * SZ_1M)) { 2908c2ecf20Sopenharmony_ci test_err("left stuff over"); 2918c2ecf20Sopenharmony_ci return -1; 2928c2ecf20Sopenharmony_ci } 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci __btrfs_remove_free_space_cache(cache->free_space_ctl); 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci /* 2978c2ecf20Sopenharmony_ci * This blew up before, we have part of the free space in a bitmap and 2988c2ecf20Sopenharmony_ci * then the entirety of the rest of the space in an extent. This used 2998c2ecf20Sopenharmony_ci * to return -EAGAIN back from btrfs_remove_extent, make sure this 3008c2ecf20Sopenharmony_ci * doesn't happen. 3018c2ecf20Sopenharmony_ci */ 3028c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, SZ_1M, SZ_2M, 1); 3038c2ecf20Sopenharmony_ci if (ret) { 3048c2ecf20Sopenharmony_ci test_err("couldn't add bitmap entry %d", ret); 3058c2ecf20Sopenharmony_ci return ret; 3068c2ecf20Sopenharmony_ci } 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, 3 * SZ_1M, SZ_1M, 0); 3098c2ecf20Sopenharmony_ci if (ret) { 3108c2ecf20Sopenharmony_ci test_err("couldn't add extent entry %d", ret); 3118c2ecf20Sopenharmony_ci return ret; 3128c2ecf20Sopenharmony_ci } 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, SZ_1M, 3 * SZ_1M); 3158c2ecf20Sopenharmony_ci if (ret) { 3168c2ecf20Sopenharmony_ci test_err("error removing bitmap and extent overlapping %d", ret); 3178c2ecf20Sopenharmony_ci return ret; 3188c2ecf20Sopenharmony_ci } 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci __btrfs_remove_free_space_cache(cache->free_space_ctl); 3218c2ecf20Sopenharmony_ci return 0; 3228c2ecf20Sopenharmony_ci} 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci/* Used by test_steal_space_from_bitmap_to_extent(). */ 3258c2ecf20Sopenharmony_cistatic bool test_use_bitmap(struct btrfs_free_space_ctl *ctl, 3268c2ecf20Sopenharmony_ci struct btrfs_free_space *info) 3278c2ecf20Sopenharmony_ci{ 3288c2ecf20Sopenharmony_ci return ctl->free_extents > 0; 3298c2ecf20Sopenharmony_ci} 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci/* Used by test_steal_space_from_bitmap_to_extent(). */ 3328c2ecf20Sopenharmony_cistatic int 3338c2ecf20Sopenharmony_cicheck_num_extents_and_bitmaps(const struct btrfs_block_group *cache, 3348c2ecf20Sopenharmony_ci const int num_extents, 3358c2ecf20Sopenharmony_ci const int num_bitmaps) 3368c2ecf20Sopenharmony_ci{ 3378c2ecf20Sopenharmony_ci if (cache->free_space_ctl->free_extents != num_extents) { 3388c2ecf20Sopenharmony_ci test_err( 3398c2ecf20Sopenharmony_ci "incorrect # of extent entries in the cache: %d, expected %d", 3408c2ecf20Sopenharmony_ci cache->free_space_ctl->free_extents, num_extents); 3418c2ecf20Sopenharmony_ci return -EINVAL; 3428c2ecf20Sopenharmony_ci } 3438c2ecf20Sopenharmony_ci if (cache->free_space_ctl->total_bitmaps != num_bitmaps) { 3448c2ecf20Sopenharmony_ci test_err( 3458c2ecf20Sopenharmony_ci "incorrect # of extent entries in the cache: %d, expected %d", 3468c2ecf20Sopenharmony_ci cache->free_space_ctl->total_bitmaps, num_bitmaps); 3478c2ecf20Sopenharmony_ci return -EINVAL; 3488c2ecf20Sopenharmony_ci } 3498c2ecf20Sopenharmony_ci return 0; 3508c2ecf20Sopenharmony_ci} 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci/* Used by test_steal_space_from_bitmap_to_extent(). */ 3538c2ecf20Sopenharmony_cistatic int check_cache_empty(struct btrfs_block_group *cache) 3548c2ecf20Sopenharmony_ci{ 3558c2ecf20Sopenharmony_ci u64 offset; 3568c2ecf20Sopenharmony_ci u64 max_extent_size; 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci /* 3598c2ecf20Sopenharmony_ci * Now lets confirm that there's absolutely no free space left to 3608c2ecf20Sopenharmony_ci * allocate. 3618c2ecf20Sopenharmony_ci */ 3628c2ecf20Sopenharmony_ci if (cache->free_space_ctl->free_space != 0) { 3638c2ecf20Sopenharmony_ci test_err("cache free space is not 0"); 3648c2ecf20Sopenharmony_ci return -EINVAL; 3658c2ecf20Sopenharmony_ci } 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci /* And any allocation request, no matter how small, should fail now. */ 3688c2ecf20Sopenharmony_ci offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0, 3698c2ecf20Sopenharmony_ci &max_extent_size); 3708c2ecf20Sopenharmony_ci if (offset != 0) { 3718c2ecf20Sopenharmony_ci test_err("space allocation did not fail, returned offset: %llu", 3728c2ecf20Sopenharmony_ci offset); 3738c2ecf20Sopenharmony_ci return -EINVAL; 3748c2ecf20Sopenharmony_ci } 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci /* And no extent nor bitmap entries in the cache anymore. */ 3778c2ecf20Sopenharmony_ci return check_num_extents_and_bitmaps(cache, 0, 0); 3788c2ecf20Sopenharmony_ci} 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci/* 3818c2ecf20Sopenharmony_ci * Before we were able to steal free space from a bitmap entry to an extent 3828c2ecf20Sopenharmony_ci * entry, we could end up with 2 entries representing a contiguous free space. 3838c2ecf20Sopenharmony_ci * One would be an extent entry and the other a bitmap entry. Since in order 3848c2ecf20Sopenharmony_ci * to allocate space to a caller we use only 1 entry, we couldn't return that 3858c2ecf20Sopenharmony_ci * whole range to the caller if it was requested. This forced the caller to 3868c2ecf20Sopenharmony_ci * either assume ENOSPC or perform several smaller space allocations, which 3878c2ecf20Sopenharmony_ci * wasn't optimal as they could be spread all over the block group while under 3888c2ecf20Sopenharmony_ci * concurrency (extra overhead and fragmentation). 3898c2ecf20Sopenharmony_ci * 3908c2ecf20Sopenharmony_ci * This stealing approach is beneficial, since we always prefer to allocate 3918c2ecf20Sopenharmony_ci * from extent entries, both for clustered and non-clustered allocation 3928c2ecf20Sopenharmony_ci * requests. 3938c2ecf20Sopenharmony_ci */ 3948c2ecf20Sopenharmony_cistatic int 3958c2ecf20Sopenharmony_citest_steal_space_from_bitmap_to_extent(struct btrfs_block_group *cache, 3968c2ecf20Sopenharmony_ci u32 sectorsize) 3978c2ecf20Sopenharmony_ci{ 3988c2ecf20Sopenharmony_ci int ret; 3998c2ecf20Sopenharmony_ci u64 offset; 4008c2ecf20Sopenharmony_ci u64 max_extent_size; 4018c2ecf20Sopenharmony_ci const struct btrfs_free_space_op test_free_space_ops = { 4028c2ecf20Sopenharmony_ci .recalc_thresholds = cache->free_space_ctl->op->recalc_thresholds, 4038c2ecf20Sopenharmony_ci .use_bitmap = test_use_bitmap, 4048c2ecf20Sopenharmony_ci }; 4058c2ecf20Sopenharmony_ci const struct btrfs_free_space_op *orig_free_space_ops; 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci test_msg("running space stealing from bitmap to extent tests"); 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci /* 4108c2ecf20Sopenharmony_ci * For this test, we want to ensure we end up with an extent entry 4118c2ecf20Sopenharmony_ci * immediately adjacent to a bitmap entry, where the bitmap starts 4128c2ecf20Sopenharmony_ci * at an offset where the extent entry ends. We keep adding and 4138c2ecf20Sopenharmony_ci * removing free space to reach into this state, but to get there 4148c2ecf20Sopenharmony_ci * we need to reach a point where marking new free space doesn't 4158c2ecf20Sopenharmony_ci * result in adding new extent entries or merging the new space 4168c2ecf20Sopenharmony_ci * with existing extent entries - the space ends up being marked 4178c2ecf20Sopenharmony_ci * in an existing bitmap that covers the new free space range. 4188c2ecf20Sopenharmony_ci * 4198c2ecf20Sopenharmony_ci * To get there, we need to reach the threshold defined set at 4208c2ecf20Sopenharmony_ci * cache->free_space_ctl->extents_thresh, which currently is 4218c2ecf20Sopenharmony_ci * 256 extents on a x86_64 system at least, and a few other 4228c2ecf20Sopenharmony_ci * conditions (check free_space_cache.c). Instead of making the 4238c2ecf20Sopenharmony_ci * test much longer and complicated, use a "use_bitmap" operation 4248c2ecf20Sopenharmony_ci * that forces use of bitmaps as soon as we have at least 1 4258c2ecf20Sopenharmony_ci * extent entry. 4268c2ecf20Sopenharmony_ci */ 4278c2ecf20Sopenharmony_ci orig_free_space_ops = cache->free_space_ctl->op; 4288c2ecf20Sopenharmony_ci cache->free_space_ctl->op = &test_free_space_ops; 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci /* 4318c2ecf20Sopenharmony_ci * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[ 4328c2ecf20Sopenharmony_ci */ 4338c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0); 4348c2ecf20Sopenharmony_ci if (ret) { 4358c2ecf20Sopenharmony_ci test_err("couldn't add extent entry %d", ret); 4368c2ecf20Sopenharmony_ci return ret; 4378c2ecf20Sopenharmony_ci } 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci /* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */ 4408c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K, 4418c2ecf20Sopenharmony_ci SZ_128M - SZ_512K, 1); 4428c2ecf20Sopenharmony_ci if (ret) { 4438c2ecf20Sopenharmony_ci test_err("couldn't add bitmap entry %d", ret); 4448c2ecf20Sopenharmony_ci return ret; 4458c2ecf20Sopenharmony_ci } 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci ret = check_num_extents_and_bitmaps(cache, 2, 1); 4488c2ecf20Sopenharmony_ci if (ret) 4498c2ecf20Sopenharmony_ci return ret; 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci /* 4528c2ecf20Sopenharmony_ci * Now make only the first 256Kb of the bitmap marked as free, so that 4538c2ecf20Sopenharmony_ci * we end up with only the following ranges marked as free space: 4548c2ecf20Sopenharmony_ci * 4558c2ecf20Sopenharmony_ci * [128Mb - 256Kb, 128Mb - 128Kb[ 4568c2ecf20Sopenharmony_ci * [128Mb + 512Kb, 128Mb + 768Kb[ 4578c2ecf20Sopenharmony_ci */ 4588c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, 4598c2ecf20Sopenharmony_ci SZ_128M + 768 * SZ_1K, 4608c2ecf20Sopenharmony_ci SZ_128M - 768 * SZ_1K); 4618c2ecf20Sopenharmony_ci if (ret) { 4628c2ecf20Sopenharmony_ci test_err("failed to free part of bitmap space %d", ret); 4638c2ecf20Sopenharmony_ci return ret; 4648c2ecf20Sopenharmony_ci } 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci /* Confirm that only those 2 ranges are marked as free. */ 4678c2ecf20Sopenharmony_ci if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) { 4688c2ecf20Sopenharmony_ci test_err("free space range missing"); 4698c2ecf20Sopenharmony_ci return -ENOENT; 4708c2ecf20Sopenharmony_ci } 4718c2ecf20Sopenharmony_ci if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) { 4728c2ecf20Sopenharmony_ci test_err("free space range missing"); 4738c2ecf20Sopenharmony_ci return -ENOENT; 4748c2ecf20Sopenharmony_ci } 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci /* 4778c2ecf20Sopenharmony_ci * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked 4788c2ecf20Sopenharmony_ci * as free anymore. 4798c2ecf20Sopenharmony_ci */ 4808c2ecf20Sopenharmony_ci if (test_check_exists(cache, SZ_128M + 768 * SZ_1K, 4818c2ecf20Sopenharmony_ci SZ_128M - 768 * SZ_1K)) { 4828c2ecf20Sopenharmony_ci test_err("bitmap region not removed from space cache"); 4838c2ecf20Sopenharmony_ci return -EINVAL; 4848c2ecf20Sopenharmony_ci } 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci /* 4878c2ecf20Sopenharmony_ci * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is 4888c2ecf20Sopenharmony_ci * covered by the bitmap, isn't marked as free. 4898c2ecf20Sopenharmony_ci */ 4908c2ecf20Sopenharmony_ci if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) { 4918c2ecf20Sopenharmony_ci test_err("invalid bitmap region marked as free"); 4928c2ecf20Sopenharmony_ci return -EINVAL; 4938c2ecf20Sopenharmony_ci } 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci /* 4968c2ecf20Sopenharmony_ci * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered 4978c2ecf20Sopenharmony_ci * by the bitmap too, isn't marked as free either. 4988c2ecf20Sopenharmony_ci */ 4998c2ecf20Sopenharmony_ci if (test_check_exists(cache, SZ_128M, SZ_256K)) { 5008c2ecf20Sopenharmony_ci test_err("invalid bitmap region marked as free"); 5018c2ecf20Sopenharmony_ci return -EINVAL; 5028c2ecf20Sopenharmony_ci } 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci /* 5058c2ecf20Sopenharmony_ci * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But, 5068c2ecf20Sopenharmony_ci * lets make sure the free space cache marks it as free in the bitmap, 5078c2ecf20Sopenharmony_ci * and doesn't insert a new extent entry to represent this region. 5088c2ecf20Sopenharmony_ci */ 5098c2ecf20Sopenharmony_ci ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K); 5108c2ecf20Sopenharmony_ci if (ret) { 5118c2ecf20Sopenharmony_ci test_err("error adding free space: %d", ret); 5128c2ecf20Sopenharmony_ci return ret; 5138c2ecf20Sopenharmony_ci } 5148c2ecf20Sopenharmony_ci /* Confirm the region is marked as free. */ 5158c2ecf20Sopenharmony_ci if (!test_check_exists(cache, SZ_128M, SZ_512K)) { 5168c2ecf20Sopenharmony_ci test_err("bitmap region not marked as free"); 5178c2ecf20Sopenharmony_ci return -ENOENT; 5188c2ecf20Sopenharmony_ci } 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci /* 5218c2ecf20Sopenharmony_ci * Confirm that no new extent entries or bitmap entries were added to 5228c2ecf20Sopenharmony_ci * the cache after adding that free space region. 5238c2ecf20Sopenharmony_ci */ 5248c2ecf20Sopenharmony_ci ret = check_num_extents_and_bitmaps(cache, 2, 1); 5258c2ecf20Sopenharmony_ci if (ret) 5268c2ecf20Sopenharmony_ci return ret; 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci /* 5298c2ecf20Sopenharmony_ci * Now lets add a small free space region to the right of the previous 5308c2ecf20Sopenharmony_ci * one, which is not contiguous with it and is part of the bitmap too. 5318c2ecf20Sopenharmony_ci * The goal is to test that the bitmap entry space stealing doesn't 5328c2ecf20Sopenharmony_ci * steal this space region. 5338c2ecf20Sopenharmony_ci */ 5348c2ecf20Sopenharmony_ci ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, sectorsize); 5358c2ecf20Sopenharmony_ci if (ret) { 5368c2ecf20Sopenharmony_ci test_err("error adding free space: %d", ret); 5378c2ecf20Sopenharmony_ci return ret; 5388c2ecf20Sopenharmony_ci } 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci /* 5418c2ecf20Sopenharmony_ci * Confirm that no new extent entries or bitmap entries were added to 5428c2ecf20Sopenharmony_ci * the cache after adding that free space region. 5438c2ecf20Sopenharmony_ci */ 5448c2ecf20Sopenharmony_ci ret = check_num_extents_and_bitmaps(cache, 2, 1); 5458c2ecf20Sopenharmony_ci if (ret) 5468c2ecf20Sopenharmony_ci return ret; 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ci /* 5498c2ecf20Sopenharmony_ci * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will 5508c2ecf20Sopenharmony_ci * expand the range covered by the existing extent entry that represents 5518c2ecf20Sopenharmony_ci * the free space [128Mb - 256Kb, 128Mb - 128Kb[. 5528c2ecf20Sopenharmony_ci */ 5538c2ecf20Sopenharmony_ci ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K); 5548c2ecf20Sopenharmony_ci if (ret) { 5558c2ecf20Sopenharmony_ci test_err("error adding free space: %d", ret); 5568c2ecf20Sopenharmony_ci return ret; 5578c2ecf20Sopenharmony_ci } 5588c2ecf20Sopenharmony_ci /* Confirm the region is marked as free. */ 5598c2ecf20Sopenharmony_ci if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) { 5608c2ecf20Sopenharmony_ci test_err("extent region not marked as free"); 5618c2ecf20Sopenharmony_ci return -ENOENT; 5628c2ecf20Sopenharmony_ci } 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci /* 5658c2ecf20Sopenharmony_ci * Confirm that our extent entry didn't stole all free space from the 5668c2ecf20Sopenharmony_ci * bitmap, because of the small 4Kb free space region. 5678c2ecf20Sopenharmony_ci */ 5688c2ecf20Sopenharmony_ci ret = check_num_extents_and_bitmaps(cache, 2, 1); 5698c2ecf20Sopenharmony_ci if (ret) 5708c2ecf20Sopenharmony_ci return ret; 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ci /* 5738c2ecf20Sopenharmony_ci * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free 5748c2ecf20Sopenharmony_ci * space. Without stealing bitmap free space into extent entry space, 5758c2ecf20Sopenharmony_ci * we would have all this free space represented by 2 entries in the 5768c2ecf20Sopenharmony_ci * cache: 5778c2ecf20Sopenharmony_ci * 5788c2ecf20Sopenharmony_ci * extent entry covering range: [128Mb - 256Kb, 128Mb[ 5798c2ecf20Sopenharmony_ci * bitmap entry covering range: [128Mb, 128Mb + 768Kb[ 5808c2ecf20Sopenharmony_ci * 5818c2ecf20Sopenharmony_ci * Attempting to allocate the whole free space (1Mb) would fail, because 5828c2ecf20Sopenharmony_ci * we can't allocate from multiple entries. 5838c2ecf20Sopenharmony_ci * With the bitmap free space stealing, we get a single extent entry 5848c2ecf20Sopenharmony_ci * that represents the 1Mb free space, and therefore we're able to 5858c2ecf20Sopenharmony_ci * allocate the whole free space at once. 5868c2ecf20Sopenharmony_ci */ 5878c2ecf20Sopenharmony_ci if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) { 5888c2ecf20Sopenharmony_ci test_err("expected region not marked as free"); 5898c2ecf20Sopenharmony_ci return -ENOENT; 5908c2ecf20Sopenharmony_ci } 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci if (cache->free_space_ctl->free_space != (SZ_1M + sectorsize)) { 5938c2ecf20Sopenharmony_ci test_err("cache free space is not 1Mb + %u", sectorsize); 5948c2ecf20Sopenharmony_ci return -EINVAL; 5958c2ecf20Sopenharmony_ci } 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci offset = btrfs_find_space_for_alloc(cache, 5988c2ecf20Sopenharmony_ci 0, SZ_1M, 0, 5998c2ecf20Sopenharmony_ci &max_extent_size); 6008c2ecf20Sopenharmony_ci if (offset != (SZ_128M - SZ_256K)) { 6018c2ecf20Sopenharmony_ci test_err( 6028c2ecf20Sopenharmony_ci "failed to allocate 1Mb from space cache, returned offset is: %llu", 6038c2ecf20Sopenharmony_ci offset); 6048c2ecf20Sopenharmony_ci return -EINVAL; 6058c2ecf20Sopenharmony_ci } 6068c2ecf20Sopenharmony_ci 6078c2ecf20Sopenharmony_ci /* 6088c2ecf20Sopenharmony_ci * All that remains is a sectorsize free space region in a bitmap. 6098c2ecf20Sopenharmony_ci * Confirm. 6108c2ecf20Sopenharmony_ci */ 6118c2ecf20Sopenharmony_ci ret = check_num_extents_and_bitmaps(cache, 1, 1); 6128c2ecf20Sopenharmony_ci if (ret) 6138c2ecf20Sopenharmony_ci return ret; 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci if (cache->free_space_ctl->free_space != sectorsize) { 6168c2ecf20Sopenharmony_ci test_err("cache free space is not %u", sectorsize); 6178c2ecf20Sopenharmony_ci return -EINVAL; 6188c2ecf20Sopenharmony_ci } 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_ci offset = btrfs_find_space_for_alloc(cache, 6218c2ecf20Sopenharmony_ci 0, sectorsize, 0, 6228c2ecf20Sopenharmony_ci &max_extent_size); 6238c2ecf20Sopenharmony_ci if (offset != (SZ_128M + SZ_16M)) { 6248c2ecf20Sopenharmony_ci test_err("failed to allocate %u, returned offset : %llu", 6258c2ecf20Sopenharmony_ci sectorsize, offset); 6268c2ecf20Sopenharmony_ci return -EINVAL; 6278c2ecf20Sopenharmony_ci } 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci ret = check_cache_empty(cache); 6308c2ecf20Sopenharmony_ci if (ret) 6318c2ecf20Sopenharmony_ci return ret; 6328c2ecf20Sopenharmony_ci 6338c2ecf20Sopenharmony_ci __btrfs_remove_free_space_cache(cache->free_space_ctl); 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ci /* 6368c2ecf20Sopenharmony_ci * Now test a similar scenario, but where our extent entry is located 6378c2ecf20Sopenharmony_ci * to the right of the bitmap entry, so that we can check that stealing 6388c2ecf20Sopenharmony_ci * space from a bitmap to the front of an extent entry works. 6398c2ecf20Sopenharmony_ci */ 6408c2ecf20Sopenharmony_ci 6418c2ecf20Sopenharmony_ci /* 6428c2ecf20Sopenharmony_ci * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[ 6438c2ecf20Sopenharmony_ci */ 6448c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0); 6458c2ecf20Sopenharmony_ci if (ret) { 6468c2ecf20Sopenharmony_ci test_err("couldn't add extent entry %d", ret); 6478c2ecf20Sopenharmony_ci return ret; 6488c2ecf20Sopenharmony_ci } 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci /* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */ 6518c2ecf20Sopenharmony_ci ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1); 6528c2ecf20Sopenharmony_ci if (ret) { 6538c2ecf20Sopenharmony_ci test_err("couldn't add bitmap entry %d", ret); 6548c2ecf20Sopenharmony_ci return ret; 6558c2ecf20Sopenharmony_ci } 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci ret = check_num_extents_and_bitmaps(cache, 2, 1); 6588c2ecf20Sopenharmony_ci if (ret) 6598c2ecf20Sopenharmony_ci return ret; 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_ci /* 6628c2ecf20Sopenharmony_ci * Now make only the last 256Kb of the bitmap marked as free, so that 6638c2ecf20Sopenharmony_ci * we end up with only the following ranges marked as free space: 6648c2ecf20Sopenharmony_ci * 6658c2ecf20Sopenharmony_ci * [128Mb + 128b, 128Mb + 256Kb[ 6668c2ecf20Sopenharmony_ci * [128Mb - 768Kb, 128Mb - 512Kb[ 6678c2ecf20Sopenharmony_ci */ 6688c2ecf20Sopenharmony_ci ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K); 6698c2ecf20Sopenharmony_ci if (ret) { 6708c2ecf20Sopenharmony_ci test_err("failed to free part of bitmap space %d", ret); 6718c2ecf20Sopenharmony_ci return ret; 6728c2ecf20Sopenharmony_ci } 6738c2ecf20Sopenharmony_ci 6748c2ecf20Sopenharmony_ci /* Confirm that only those 2 ranges are marked as free. */ 6758c2ecf20Sopenharmony_ci if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) { 6768c2ecf20Sopenharmony_ci test_err("free space range missing"); 6778c2ecf20Sopenharmony_ci return -ENOENT; 6788c2ecf20Sopenharmony_ci } 6798c2ecf20Sopenharmony_ci if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) { 6808c2ecf20Sopenharmony_ci test_err("free space range missing"); 6818c2ecf20Sopenharmony_ci return -ENOENT; 6828c2ecf20Sopenharmony_ci } 6838c2ecf20Sopenharmony_ci 6848c2ecf20Sopenharmony_ci /* 6858c2ecf20Sopenharmony_ci * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked 6868c2ecf20Sopenharmony_ci * as free anymore. 6878c2ecf20Sopenharmony_ci */ 6888c2ecf20Sopenharmony_ci if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) { 6898c2ecf20Sopenharmony_ci test_err("bitmap region not removed from space cache"); 6908c2ecf20Sopenharmony_ci return -EINVAL; 6918c2ecf20Sopenharmony_ci } 6928c2ecf20Sopenharmony_ci 6938c2ecf20Sopenharmony_ci /* 6948c2ecf20Sopenharmony_ci * Confirm that the region [128Mb - 512Kb, 128Mb[, which is 6958c2ecf20Sopenharmony_ci * covered by the bitmap, isn't marked as free. 6968c2ecf20Sopenharmony_ci */ 6978c2ecf20Sopenharmony_ci if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) { 6988c2ecf20Sopenharmony_ci test_err("invalid bitmap region marked as free"); 6998c2ecf20Sopenharmony_ci return -EINVAL; 7008c2ecf20Sopenharmony_ci } 7018c2ecf20Sopenharmony_ci 7028c2ecf20Sopenharmony_ci /* 7038c2ecf20Sopenharmony_ci * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But, 7048c2ecf20Sopenharmony_ci * lets make sure the free space cache marks it as free in the bitmap, 7058c2ecf20Sopenharmony_ci * and doesn't insert a new extent entry to represent this region. 7068c2ecf20Sopenharmony_ci */ 7078c2ecf20Sopenharmony_ci ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K); 7088c2ecf20Sopenharmony_ci if (ret) { 7098c2ecf20Sopenharmony_ci test_err("error adding free space: %d", ret); 7108c2ecf20Sopenharmony_ci return ret; 7118c2ecf20Sopenharmony_ci } 7128c2ecf20Sopenharmony_ci /* Confirm the region is marked as free. */ 7138c2ecf20Sopenharmony_ci if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) { 7148c2ecf20Sopenharmony_ci test_err("bitmap region not marked as free"); 7158c2ecf20Sopenharmony_ci return -ENOENT; 7168c2ecf20Sopenharmony_ci } 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci /* 7198c2ecf20Sopenharmony_ci * Confirm that no new extent entries or bitmap entries were added to 7208c2ecf20Sopenharmony_ci * the cache after adding that free space region. 7218c2ecf20Sopenharmony_ci */ 7228c2ecf20Sopenharmony_ci ret = check_num_extents_and_bitmaps(cache, 2, 1); 7238c2ecf20Sopenharmony_ci if (ret) 7248c2ecf20Sopenharmony_ci return ret; 7258c2ecf20Sopenharmony_ci 7268c2ecf20Sopenharmony_ci /* 7278c2ecf20Sopenharmony_ci * Now lets add a small free space region to the left of the previous 7288c2ecf20Sopenharmony_ci * one, which is not contiguous with it and is part of the bitmap too. 7298c2ecf20Sopenharmony_ci * The goal is to test that the bitmap entry space stealing doesn't 7308c2ecf20Sopenharmony_ci * steal this space region. 7318c2ecf20Sopenharmony_ci */ 7328c2ecf20Sopenharmony_ci ret = btrfs_add_free_space(cache, SZ_32M, 2 * sectorsize); 7338c2ecf20Sopenharmony_ci if (ret) { 7348c2ecf20Sopenharmony_ci test_err("error adding free space: %d", ret); 7358c2ecf20Sopenharmony_ci return ret; 7368c2ecf20Sopenharmony_ci } 7378c2ecf20Sopenharmony_ci 7388c2ecf20Sopenharmony_ci /* 7398c2ecf20Sopenharmony_ci * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will 7408c2ecf20Sopenharmony_ci * expand the range covered by the existing extent entry that represents 7418c2ecf20Sopenharmony_ci * the free space [128Mb + 128Kb, 128Mb + 256Kb[. 7428c2ecf20Sopenharmony_ci */ 7438c2ecf20Sopenharmony_ci ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K); 7448c2ecf20Sopenharmony_ci if (ret) { 7458c2ecf20Sopenharmony_ci test_err("error adding free space: %d", ret); 7468c2ecf20Sopenharmony_ci return ret; 7478c2ecf20Sopenharmony_ci } 7488c2ecf20Sopenharmony_ci /* Confirm the region is marked as free. */ 7498c2ecf20Sopenharmony_ci if (!test_check_exists(cache, SZ_128M, SZ_128K)) { 7508c2ecf20Sopenharmony_ci test_err("extent region not marked as free"); 7518c2ecf20Sopenharmony_ci return -ENOENT; 7528c2ecf20Sopenharmony_ci } 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_ci /* 7558c2ecf20Sopenharmony_ci * Confirm that our extent entry didn't stole all free space from the 7568c2ecf20Sopenharmony_ci * bitmap, because of the small 2 * sectorsize free space region. 7578c2ecf20Sopenharmony_ci */ 7588c2ecf20Sopenharmony_ci ret = check_num_extents_and_bitmaps(cache, 2, 1); 7598c2ecf20Sopenharmony_ci if (ret) 7608c2ecf20Sopenharmony_ci return ret; 7618c2ecf20Sopenharmony_ci 7628c2ecf20Sopenharmony_ci /* 7638c2ecf20Sopenharmony_ci * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free 7648c2ecf20Sopenharmony_ci * space. Without stealing bitmap free space into extent entry space, 7658c2ecf20Sopenharmony_ci * we would have all this free space represented by 2 entries in the 7668c2ecf20Sopenharmony_ci * cache: 7678c2ecf20Sopenharmony_ci * 7688c2ecf20Sopenharmony_ci * extent entry covering range: [128Mb, 128Mb + 256Kb[ 7698c2ecf20Sopenharmony_ci * bitmap entry covering range: [128Mb - 768Kb, 128Mb[ 7708c2ecf20Sopenharmony_ci * 7718c2ecf20Sopenharmony_ci * Attempting to allocate the whole free space (1Mb) would fail, because 7728c2ecf20Sopenharmony_ci * we can't allocate from multiple entries. 7738c2ecf20Sopenharmony_ci * With the bitmap free space stealing, we get a single extent entry 7748c2ecf20Sopenharmony_ci * that represents the 1Mb free space, and therefore we're able to 7758c2ecf20Sopenharmony_ci * allocate the whole free space at once. 7768c2ecf20Sopenharmony_ci */ 7778c2ecf20Sopenharmony_ci if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) { 7788c2ecf20Sopenharmony_ci test_err("expected region not marked as free"); 7798c2ecf20Sopenharmony_ci return -ENOENT; 7808c2ecf20Sopenharmony_ci } 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci if (cache->free_space_ctl->free_space != (SZ_1M + 2 * sectorsize)) { 7838c2ecf20Sopenharmony_ci test_err("cache free space is not 1Mb + %u", 2 * sectorsize); 7848c2ecf20Sopenharmony_ci return -EINVAL; 7858c2ecf20Sopenharmony_ci } 7868c2ecf20Sopenharmony_ci 7878c2ecf20Sopenharmony_ci offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0, 7888c2ecf20Sopenharmony_ci &max_extent_size); 7898c2ecf20Sopenharmony_ci if (offset != (SZ_128M - 768 * SZ_1K)) { 7908c2ecf20Sopenharmony_ci test_err( 7918c2ecf20Sopenharmony_ci "failed to allocate 1Mb from space cache, returned offset is: %llu", 7928c2ecf20Sopenharmony_ci offset); 7938c2ecf20Sopenharmony_ci return -EINVAL; 7948c2ecf20Sopenharmony_ci } 7958c2ecf20Sopenharmony_ci 7968c2ecf20Sopenharmony_ci /* 7978c2ecf20Sopenharmony_ci * All that remains is 2 * sectorsize free space region 7988c2ecf20Sopenharmony_ci * in a bitmap. Confirm. 7998c2ecf20Sopenharmony_ci */ 8008c2ecf20Sopenharmony_ci ret = check_num_extents_and_bitmaps(cache, 1, 1); 8018c2ecf20Sopenharmony_ci if (ret) 8028c2ecf20Sopenharmony_ci return ret; 8038c2ecf20Sopenharmony_ci 8048c2ecf20Sopenharmony_ci if (cache->free_space_ctl->free_space != 2 * sectorsize) { 8058c2ecf20Sopenharmony_ci test_err("cache free space is not %u", 2 * sectorsize); 8068c2ecf20Sopenharmony_ci return -EINVAL; 8078c2ecf20Sopenharmony_ci } 8088c2ecf20Sopenharmony_ci 8098c2ecf20Sopenharmony_ci offset = btrfs_find_space_for_alloc(cache, 8108c2ecf20Sopenharmony_ci 0, 2 * sectorsize, 0, 8118c2ecf20Sopenharmony_ci &max_extent_size); 8128c2ecf20Sopenharmony_ci if (offset != SZ_32M) { 8138c2ecf20Sopenharmony_ci test_err("failed to allocate %u, offset: %llu", 8148c2ecf20Sopenharmony_ci 2 * sectorsize, offset); 8158c2ecf20Sopenharmony_ci return -EINVAL; 8168c2ecf20Sopenharmony_ci } 8178c2ecf20Sopenharmony_ci 8188c2ecf20Sopenharmony_ci ret = check_cache_empty(cache); 8198c2ecf20Sopenharmony_ci if (ret) 8208c2ecf20Sopenharmony_ci return ret; 8218c2ecf20Sopenharmony_ci 8228c2ecf20Sopenharmony_ci cache->free_space_ctl->op = orig_free_space_ops; 8238c2ecf20Sopenharmony_ci __btrfs_remove_free_space_cache(cache->free_space_ctl); 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_ci return 0; 8268c2ecf20Sopenharmony_ci} 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ciint btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize) 8298c2ecf20Sopenharmony_ci{ 8308c2ecf20Sopenharmony_ci struct btrfs_fs_info *fs_info; 8318c2ecf20Sopenharmony_ci struct btrfs_block_group *cache; 8328c2ecf20Sopenharmony_ci struct btrfs_root *root = NULL; 8338c2ecf20Sopenharmony_ci int ret = -ENOMEM; 8348c2ecf20Sopenharmony_ci 8358c2ecf20Sopenharmony_ci test_msg("running btrfs free space cache tests"); 8368c2ecf20Sopenharmony_ci fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); 8378c2ecf20Sopenharmony_ci if (!fs_info) { 8388c2ecf20Sopenharmony_ci test_std_err(TEST_ALLOC_FS_INFO); 8398c2ecf20Sopenharmony_ci return -ENOMEM; 8408c2ecf20Sopenharmony_ci } 8418c2ecf20Sopenharmony_ci 8428c2ecf20Sopenharmony_ci /* 8438c2ecf20Sopenharmony_ci * For ppc64 (with 64k page size), bytes per bitmap might be 8448c2ecf20Sopenharmony_ci * larger than 1G. To make bitmap test available in ppc64, 8458c2ecf20Sopenharmony_ci * alloc dummy block group whose size cross bitmaps. 8468c2ecf20Sopenharmony_ci */ 8478c2ecf20Sopenharmony_ci cache = btrfs_alloc_dummy_block_group(fs_info, 8488c2ecf20Sopenharmony_ci BITS_PER_BITMAP * sectorsize + PAGE_SIZE); 8498c2ecf20Sopenharmony_ci if (!cache) { 8508c2ecf20Sopenharmony_ci test_std_err(TEST_ALLOC_BLOCK_GROUP); 8518c2ecf20Sopenharmony_ci btrfs_free_dummy_fs_info(fs_info); 8528c2ecf20Sopenharmony_ci return 0; 8538c2ecf20Sopenharmony_ci } 8548c2ecf20Sopenharmony_ci 8558c2ecf20Sopenharmony_ci root = btrfs_alloc_dummy_root(fs_info); 8568c2ecf20Sopenharmony_ci if (IS_ERR(root)) { 8578c2ecf20Sopenharmony_ci test_std_err(TEST_ALLOC_ROOT); 8588c2ecf20Sopenharmony_ci ret = PTR_ERR(root); 8598c2ecf20Sopenharmony_ci goto out; 8608c2ecf20Sopenharmony_ci } 8618c2ecf20Sopenharmony_ci 8628c2ecf20Sopenharmony_ci root->fs_info->extent_root = root; 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_ci ret = test_extents(cache); 8658c2ecf20Sopenharmony_ci if (ret) 8668c2ecf20Sopenharmony_ci goto out; 8678c2ecf20Sopenharmony_ci ret = test_bitmaps(cache, sectorsize); 8688c2ecf20Sopenharmony_ci if (ret) 8698c2ecf20Sopenharmony_ci goto out; 8708c2ecf20Sopenharmony_ci ret = test_bitmaps_and_extents(cache, sectorsize); 8718c2ecf20Sopenharmony_ci if (ret) 8728c2ecf20Sopenharmony_ci goto out; 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_ci ret = test_steal_space_from_bitmap_to_extent(cache, sectorsize); 8758c2ecf20Sopenharmony_ciout: 8768c2ecf20Sopenharmony_ci btrfs_free_dummy_block_group(cache); 8778c2ecf20Sopenharmony_ci btrfs_free_dummy_root(root); 8788c2ecf20Sopenharmony_ci btrfs_free_dummy_fs_info(fs_info); 8798c2ecf20Sopenharmony_ci return ret; 8808c2ecf20Sopenharmony_ci} 881