^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) 2013 Fusion IO. 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/slab.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 "../disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "../free-space-cache.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) #define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * This test just does basic sanity checking, making sure we can add an extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * entry and remove space from either end and the middle, and make sure we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * remove space that covers adjacent extent entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int test_extents(struct btrfs_block_group *cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) test_msg("running extent only tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* First just make sure we can remove an entire entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) ret = btrfs_add_free_space(cache, 0, SZ_4M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) test_err("error adding initial extents %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) ret = btrfs_remove_free_space(cache, 0, SZ_4M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) test_err("error removing extent %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (test_check_exists(cache, 0, SZ_4M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) test_err("full remove left some lingering space");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* Ok edge and middle cases now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ret = btrfs_add_free_space(cache, 0, SZ_4M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) test_err("error adding half extent %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_1M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) test_err("error removing tail end %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ret = btrfs_remove_free_space(cache, 0, SZ_1M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) test_err("error removing front end %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ret = btrfs_remove_free_space(cache, SZ_2M, 4096);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) test_err("error removing middle piece %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (test_check_exists(cache, 0, SZ_1M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) test_err("still have space at the front");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (test_check_exists(cache, SZ_2M, 4096)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) test_err("still have space in the middle");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (test_check_exists(cache, 3 * SZ_1M, SZ_1M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) test_err("still have space at the end");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* Cleanup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) __btrfs_remove_free_space_cache(cache->free_space_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static int test_bitmaps(struct btrfs_block_group *cache, u32 sectorsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) u64 next_bitmap_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) test_msg("running bitmap only tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) test_err("couldn't create a bitmap entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ret = btrfs_remove_free_space(cache, 0, SZ_4M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) test_err("error removing bitmap full range %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return ret;
^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) if (test_check_exists(cache, 0, SZ_4M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) test_err("left some space in bitmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) test_err("couldn't add to our bitmap entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ret = btrfs_remove_free_space(cache, SZ_1M, SZ_2M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) test_err("couldn't remove middle chunk %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * The first bitmap we have starts at offset 0 so the next one is just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * at the end of the first bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) next_bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* Test a bit straddling two bitmaps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ret = test_add_free_space_entry(cache, next_bitmap_offset - SZ_2M,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) SZ_4M, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) test_err("couldn't add space that straddles two bitmaps %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ret = btrfs_remove_free_space(cache, next_bitmap_offset - SZ_1M, SZ_2M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) test_err("couldn't remove overlapping space %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (test_check_exists(cache, next_bitmap_offset - SZ_1M, SZ_2M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) test_err("left some space when removing overlapping");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) __btrfs_remove_free_space_cache(cache->free_space_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* This is the high grade jackassery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static int test_bitmaps_and_extents(struct btrfs_block_group *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) u32 sectorsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) u64 bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) test_msg("running bitmap and extent tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * First let's do something simple, an extent at the same offset as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * bitmap, but the free space completely in the extent and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * completely in the bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ret = test_add_free_space_entry(cache, SZ_4M, SZ_1M, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) test_err("couldn't create bitmap entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return ret;
^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) ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) test_err("couldn't add extent entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ret = btrfs_remove_free_space(cache, 0, SZ_1M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) test_err("couldn't remove extent entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (test_check_exists(cache, 0, SZ_1M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) test_err("left remnants after our remove");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /* Now to add back the extent entry and remove from the bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) test_err("couldn't re-add extent entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) ret = btrfs_remove_free_space(cache, SZ_4M, SZ_1M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) test_err("couldn't remove from bitmap %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (test_check_exists(cache, SZ_4M, SZ_1M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) test_err("left remnants in the bitmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * Ok so a little more evil, extent entry and bitmap at the same offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * removing an overlapping chunk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ret = test_add_free_space_entry(cache, SZ_1M, SZ_4M, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) test_err("couldn't add to a bitmap %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) ret = btrfs_remove_free_space(cache, SZ_512K, 3 * SZ_1M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) test_err("couldn't remove overlapping space %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (test_check_exists(cache, SZ_512K, 3 * SZ_1M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) test_err("left over pieces after removing overlapping");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) __btrfs_remove_free_space_cache(cache->free_space_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* Now with the extent entry offset into the bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) ret = test_add_free_space_entry(cache, SZ_4M, SZ_4M, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) test_err("couldn't add space to the bitmap %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ret = test_add_free_space_entry(cache, SZ_2M, SZ_2M, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) test_err("couldn't add extent to the cache %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_4M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) test_err("problem removing overlapping space %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (test_check_exists(cache, 3 * SZ_1M, SZ_4M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) test_err("left something behind when removing space");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) return -1;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * This has blown up in the past, the extent entry starts before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * bitmap entry, but we're trying to remove an offset that falls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * completely within the bitmap range and is in both the extent entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * and the bitmap entry, looks like this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * [ extent ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * [ bitmap ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * [ del ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) __btrfs_remove_free_space_cache(cache->free_space_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) ret = test_add_free_space_entry(cache, bitmap_offset + SZ_4M, SZ_4M, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) test_err("couldn't add bitmap %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ret = test_add_free_space_entry(cache, bitmap_offset - SZ_1M,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 5 * SZ_1M, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) test_err("couldn't add extent entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ret = btrfs_remove_free_space(cache, bitmap_offset + SZ_1M, 5 * SZ_1M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) test_err("failed to free our space %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (test_check_exists(cache, bitmap_offset + SZ_1M, 5 * SZ_1M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) test_err("left stuff over");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) __btrfs_remove_free_space_cache(cache->free_space_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * This blew up before, we have part of the free space in a bitmap and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * then the entirety of the rest of the space in an extent. This used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * to return -EAGAIN back from btrfs_remove_extent, make sure this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * doesn't happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) ret = test_add_free_space_entry(cache, SZ_1M, SZ_2M, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) test_err("couldn't add bitmap entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) ret = test_add_free_space_entry(cache, 3 * SZ_1M, SZ_1M, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) test_err("couldn't add extent entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ret = btrfs_remove_free_space(cache, SZ_1M, 3 * SZ_1M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) test_err("error removing bitmap and extent overlapping %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) __btrfs_remove_free_space_cache(cache->free_space_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return 0;
^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) /* Used by test_steal_space_from_bitmap_to_extent(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct btrfs_free_space *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return ctl->free_extents > 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) /* Used by test_steal_space_from_bitmap_to_extent(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) check_num_extents_and_bitmaps(const struct btrfs_block_group *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) const int num_extents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) const int num_bitmaps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (cache->free_space_ctl->free_extents != num_extents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) "incorrect # of extent entries in the cache: %d, expected %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) cache->free_space_ctl->free_extents, num_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (cache->free_space_ctl->total_bitmaps != num_bitmaps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) "incorrect # of extent entries in the cache: %d, expected %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) cache->free_space_ctl->total_bitmaps, num_bitmaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /* Used by test_steal_space_from_bitmap_to_extent(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static int check_cache_empty(struct btrfs_block_group *cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) u64 max_extent_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * Now lets confirm that there's absolutely no free space left to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * allocate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (cache->free_space_ctl->free_space != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) test_err("cache free space is not 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /* And any allocation request, no matter how small, should fail now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) &max_extent_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (offset != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) test_err("space allocation did not fail, returned offset: %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /* And no extent nor bitmap entries in the cache anymore. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return check_num_extents_and_bitmaps(cache, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * Before we were able to steal free space from a bitmap entry to an extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * entry, we could end up with 2 entries representing a contiguous free space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * One would be an extent entry and the other a bitmap entry. Since in order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * to allocate space to a caller we use only 1 entry, we couldn't return that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * whole range to the caller if it was requested. This forced the caller to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * either assume ENOSPC or perform several smaller space allocations, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * wasn't optimal as they could be spread all over the block group while under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * concurrency (extra overhead and fragmentation).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * This stealing approach is beneficial, since we always prefer to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * from extent entries, both for clustered and non-clustered allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) test_steal_space_from_bitmap_to_extent(struct btrfs_block_group *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) u32 sectorsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) u64 max_extent_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) const struct btrfs_free_space_op test_free_space_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) .recalc_thresholds = cache->free_space_ctl->op->recalc_thresholds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) .use_bitmap = test_use_bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) const struct btrfs_free_space_op *orig_free_space_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) test_msg("running space stealing from bitmap to extent tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * For this test, we want to ensure we end up with an extent entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * immediately adjacent to a bitmap entry, where the bitmap starts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * at an offset where the extent entry ends. We keep adding and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * removing free space to reach into this state, but to get there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * we need to reach a point where marking new free space doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * result in adding new extent entries or merging the new space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * with existing extent entries - the space ends up being marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * in an existing bitmap that covers the new free space range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * To get there, we need to reach the threshold defined set at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * cache->free_space_ctl->extents_thresh, which currently is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * 256 extents on a x86_64 system at least, and a few other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * conditions (check free_space_cache.c). Instead of making the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * test much longer and complicated, use a "use_bitmap" operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * that forces use of bitmaps as soon as we have at least 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * extent entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) orig_free_space_ops = cache->free_space_ctl->op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) cache->free_space_ctl->op = &test_free_space_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) test_err("couldn't add extent entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) /* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) SZ_128M - SZ_512K, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) test_err("couldn't add bitmap entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) ret = check_num_extents_and_bitmaps(cache, 2, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * Now make only the first 256Kb of the bitmap marked as free, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * we end up with only the following ranges marked as free space:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * [128Mb - 256Kb, 128Mb - 128Kb[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * [128Mb + 512Kb, 128Mb + 768Kb[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) ret = btrfs_remove_free_space(cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) SZ_128M + 768 * SZ_1K,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) SZ_128M - 768 * SZ_1K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) test_err("failed to free part of bitmap space %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /* Confirm that only those 2 ranges are marked as free. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) test_err("free space range missing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) test_err("free space range missing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * as free anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (test_check_exists(cache, SZ_128M + 768 * SZ_1K,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) SZ_128M - 768 * SZ_1K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) test_err("bitmap region not removed from space cache");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * covered by the bitmap, isn't marked as free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) test_err("invalid bitmap region marked as free");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) * by the bitmap too, isn't marked as free either.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (test_check_exists(cache, SZ_128M, SZ_256K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) test_err("invalid bitmap region marked as free");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * lets make sure the free space cache marks it as free in the bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * and doesn't insert a new extent entry to represent this region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) test_err("error adding free space: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /* Confirm the region is marked as free. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (!test_check_exists(cache, SZ_128M, SZ_512K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) test_err("bitmap region not marked as free");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) * Confirm that no new extent entries or bitmap entries were added to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) * the cache after adding that free space region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) ret = check_num_extents_and_bitmaps(cache, 2, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * Now lets add a small free space region to the right of the previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * one, which is not contiguous with it and is part of the bitmap too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * The goal is to test that the bitmap entry space stealing doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * steal this space region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) test_err("error adding free space: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * Confirm that no new extent entries or bitmap entries were added to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * the cache after adding that free space region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) ret = check_num_extents_and_bitmaps(cache, 2, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * expand the range covered by the existing extent entry that represents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) test_err("error adding free space: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) /* Confirm the region is marked as free. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) test_err("extent region not marked as free");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * Confirm that our extent entry didn't stole all free space from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) * bitmap, because of the small 4Kb free space region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) ret = check_num_extents_and_bitmaps(cache, 2, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * space. Without stealing bitmap free space into extent entry space,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * we would have all this free space represented by 2 entries in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * cache:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * extent entry covering range: [128Mb - 256Kb, 128Mb[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) * Attempting to allocate the whole free space (1Mb) would fail, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * we can't allocate from multiple entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * With the bitmap free space stealing, we get a single extent entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * that represents the 1Mb free space, and therefore we're able to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * allocate the whole free space at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) test_err("expected region not marked as free");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (cache->free_space_ctl->free_space != (SZ_1M + sectorsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) test_err("cache free space is not 1Mb + %u", sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) offset = btrfs_find_space_for_alloc(cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 0, SZ_1M, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) &max_extent_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (offset != (SZ_128M - SZ_256K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) "failed to allocate 1Mb from space cache, returned offset is: %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) * All that remains is a sectorsize free space region in a bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) * Confirm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) ret = check_num_extents_and_bitmaps(cache, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (cache->free_space_ctl->free_space != sectorsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) test_err("cache free space is not %u", sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) offset = btrfs_find_space_for_alloc(cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 0, sectorsize, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) &max_extent_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) if (offset != (SZ_128M + SZ_16M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) test_err("failed to allocate %u, returned offset : %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) sectorsize, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) return -EINVAL;
^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) ret = check_cache_empty(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) __btrfs_remove_free_space_cache(cache->free_space_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * Now test a similar scenario, but where our extent entry is located
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * to the right of the bitmap entry, so that we can check that stealing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * space from a bitmap to the front of an extent entry works.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) test_err("couldn't add extent entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) /* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) test_err("couldn't add bitmap entry %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) ret = check_num_extents_and_bitmaps(cache, 2, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * Now make only the last 256Kb of the bitmap marked as free, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) * we end up with only the following ranges marked as free space:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * [128Mb + 128b, 128Mb + 256Kb[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * [128Mb - 768Kb, 128Mb - 512Kb[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) test_err("failed to free part of bitmap space %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) /* Confirm that only those 2 ranges are marked as free. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) test_err("free space range missing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) test_err("free space range missing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) * as free anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) test_err("bitmap region not removed from space cache");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) * covered by the bitmap, isn't marked as free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) test_err("invalid bitmap region marked as free");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * lets make sure the free space cache marks it as free in the bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * and doesn't insert a new extent entry to represent this region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) test_err("error adding free space: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) /* Confirm the region is marked as free. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) test_err("bitmap region not marked as free");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * Confirm that no new extent entries or bitmap entries were added to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * the cache after adding that free space region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) ret = check_num_extents_and_bitmaps(cache, 2, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * Now lets add a small free space region to the left of the previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * one, which is not contiguous with it and is part of the bitmap too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * The goal is to test that the bitmap entry space stealing doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * steal this space region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) ret = btrfs_add_free_space(cache, SZ_32M, 2 * sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) test_err("error adding free space: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) * expand the range covered by the existing extent entry that represents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) test_err("error adding free space: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) /* Confirm the region is marked as free. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) if (!test_check_exists(cache, SZ_128M, SZ_128K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) test_err("extent region not marked as free");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * Confirm that our extent entry didn't stole all free space from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) * bitmap, because of the small 2 * sectorsize free space region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) ret = check_num_extents_and_bitmaps(cache, 2, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) * space. Without stealing bitmap free space into extent entry space,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) * we would have all this free space represented by 2 entries in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * cache:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * extent entry covering range: [128Mb, 128Mb + 256Kb[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) * Attempting to allocate the whole free space (1Mb) would fail, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * we can't allocate from multiple entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * With the bitmap free space stealing, we get a single extent entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) * that represents the 1Mb free space, and therefore we're able to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) * allocate the whole free space at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) test_err("expected region not marked as free");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (cache->free_space_ctl->free_space != (SZ_1M + 2 * sectorsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) test_err("cache free space is not 1Mb + %u", 2 * sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) &max_extent_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) if (offset != (SZ_128M - 768 * SZ_1K)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) "failed to allocate 1Mb from space cache, returned offset is: %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) * All that remains is 2 * sectorsize free space region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * in a bitmap. Confirm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) ret = check_num_extents_and_bitmaps(cache, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) if (cache->free_space_ctl->free_space != 2 * sectorsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) test_err("cache free space is not %u", 2 * sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) offset = btrfs_find_space_for_alloc(cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 0, 2 * sectorsize, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) &max_extent_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (offset != SZ_32M) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) test_err("failed to allocate %u, offset: %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 2 * sectorsize, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) ret = check_cache_empty(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) cache->free_space_ctl->op = orig_free_space_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) __btrfs_remove_free_space_cache(cache->free_space_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) int btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) struct btrfs_fs_info *fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) struct btrfs_block_group *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) struct btrfs_root *root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) test_msg("running btrfs free space cache tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (!fs_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) test_std_err(TEST_ALLOC_FS_INFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) * For ppc64 (with 64k page size), bytes per bitmap might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) * larger than 1G. To make bitmap test available in ppc64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) * alloc dummy block group whose size cross bitmaps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) cache = btrfs_alloc_dummy_block_group(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) BITS_PER_BITMAP * sectorsize + PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) if (!cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) test_std_err(TEST_ALLOC_BLOCK_GROUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) btrfs_free_dummy_fs_info(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) root = btrfs_alloc_dummy_root(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (IS_ERR(root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) test_std_err(TEST_ALLOC_ROOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) ret = PTR_ERR(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) root->fs_info->extent_root = root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) ret = test_extents(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) ret = test_bitmaps(cache, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) ret = test_bitmaps_and_extents(cache, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) ret = test_steal_space_from_bitmap_to_extent(cache, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) btrfs_free_dummy_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) btrfs_free_dummy_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) btrfs_free_dummy_fs_info(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) }