1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
27c55ee0cSOmar Sandoval /*
37c55ee0cSOmar Sandoval * Copyright (C) 2015 Facebook. All rights reserved.
47c55ee0cSOmar Sandoval */
57c55ee0cSOmar Sandoval
6b9ef22deSFeifei Xu #include <linux/types.h>
77c55ee0cSOmar Sandoval #include "btrfs-tests.h"
87c55ee0cSOmar Sandoval #include "../ctree.h"
97c55ee0cSOmar Sandoval #include "../disk-io.h"
107c55ee0cSOmar Sandoval #include "../free-space-tree.h"
117c55ee0cSOmar Sandoval #include "../transaction.h"
12aac0023cSJosef Bacik #include "../block-group.h"
1307e81dc9SJosef Bacik #include "../accessors.h"
147c55ee0cSOmar Sandoval
157c55ee0cSOmar Sandoval struct free_space_extent {
160e675785SDavid Sterba u64 start;
170e675785SDavid Sterba u64 length;
187c55ee0cSOmar Sandoval };
197c55ee0cSOmar Sandoval
__check_free_space_extents(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info,struct btrfs_block_group * cache,struct btrfs_path * path,const struct free_space_extent * const extents,unsigned int num_extents)207c55ee0cSOmar Sandoval static int __check_free_space_extents(struct btrfs_trans_handle *trans,
217c55ee0cSOmar Sandoval struct btrfs_fs_info *fs_info,
2232da5386SDavid Sterba struct btrfs_block_group *cache,
237c55ee0cSOmar Sandoval struct btrfs_path *path,
24d2d9ac6aSDavid Sterba const struct free_space_extent * const extents,
257c55ee0cSOmar Sandoval unsigned int num_extents)
267c55ee0cSOmar Sandoval {
277c55ee0cSOmar Sandoval struct btrfs_free_space_info *info;
287c55ee0cSOmar Sandoval struct btrfs_key key;
297c55ee0cSOmar Sandoval int prev_bit = 0, bit;
307c55ee0cSOmar Sandoval u64 extent_start = 0, offset, end;
317c55ee0cSOmar Sandoval u32 flags, extent_count;
327c55ee0cSOmar Sandoval unsigned int i;
337c55ee0cSOmar Sandoval int ret;
347c55ee0cSOmar Sandoval
352ccf545eSDavid Sterba info = search_free_space_info(trans, cache, path, 0);
367c55ee0cSOmar Sandoval if (IS_ERR(info)) {
373c7251f2SDavid Sterba test_err("could not find free space info");
387c55ee0cSOmar Sandoval ret = PTR_ERR(info);
397c55ee0cSOmar Sandoval goto out;
407c55ee0cSOmar Sandoval }
417c55ee0cSOmar Sandoval flags = btrfs_free_space_flags(path->nodes[0], info);
427c55ee0cSOmar Sandoval extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
437c55ee0cSOmar Sandoval
447c55ee0cSOmar Sandoval if (extent_count != num_extents) {
453c7251f2SDavid Sterba test_err("extent count is wrong");
467c55ee0cSOmar Sandoval ret = -EINVAL;
477c55ee0cSOmar Sandoval goto out;
487c55ee0cSOmar Sandoval }
497c55ee0cSOmar Sandoval if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
507c55ee0cSOmar Sandoval if (path->slots[0] != 0)
517c55ee0cSOmar Sandoval goto invalid;
52b3470b5dSDavid Sterba end = cache->start + cache->length;
537c55ee0cSOmar Sandoval i = 0;
547c55ee0cSOmar Sandoval while (++path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
557c55ee0cSOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
567c55ee0cSOmar Sandoval if (key.type != BTRFS_FREE_SPACE_BITMAP_KEY)
577c55ee0cSOmar Sandoval goto invalid;
587c55ee0cSOmar Sandoval offset = key.objectid;
597c55ee0cSOmar Sandoval while (offset < key.objectid + key.offset) {
607c55ee0cSOmar Sandoval bit = free_space_test_bit(cache, path, offset);
617c55ee0cSOmar Sandoval if (prev_bit == 0 && bit == 1) {
627c55ee0cSOmar Sandoval extent_start = offset;
637c55ee0cSOmar Sandoval } else if (prev_bit == 1 && bit == 0) {
647c55ee0cSOmar Sandoval if (i >= num_extents ||
657c55ee0cSOmar Sandoval extent_start != extents[i].start ||
667c55ee0cSOmar Sandoval offset - extent_start != extents[i].length)
677c55ee0cSOmar Sandoval goto invalid;
687c55ee0cSOmar Sandoval i++;
697c55ee0cSOmar Sandoval }
707c55ee0cSOmar Sandoval prev_bit = bit;
7123d1f737SNikolay Borisov offset += fs_info->sectorsize;
727c55ee0cSOmar Sandoval }
737c55ee0cSOmar Sandoval }
747c55ee0cSOmar Sandoval if (prev_bit == 1) {
757c55ee0cSOmar Sandoval if (i >= num_extents ||
767c55ee0cSOmar Sandoval extent_start != extents[i].start ||
777c55ee0cSOmar Sandoval end - extent_start != extents[i].length)
787c55ee0cSOmar Sandoval goto invalid;
797c55ee0cSOmar Sandoval i++;
807c55ee0cSOmar Sandoval }
817c55ee0cSOmar Sandoval if (i != num_extents)
827c55ee0cSOmar Sandoval goto invalid;
837c55ee0cSOmar Sandoval } else {
847c55ee0cSOmar Sandoval if (btrfs_header_nritems(path->nodes[0]) != num_extents + 1 ||
857c55ee0cSOmar Sandoval path->slots[0] != 0)
867c55ee0cSOmar Sandoval goto invalid;
877c55ee0cSOmar Sandoval for (i = 0; i < num_extents; i++) {
887c55ee0cSOmar Sandoval path->slots[0]++;
897c55ee0cSOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
907c55ee0cSOmar Sandoval if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY ||
917c55ee0cSOmar Sandoval key.objectid != extents[i].start ||
927c55ee0cSOmar Sandoval key.offset != extents[i].length)
937c55ee0cSOmar Sandoval goto invalid;
947c55ee0cSOmar Sandoval }
957c55ee0cSOmar Sandoval }
967c55ee0cSOmar Sandoval
977c55ee0cSOmar Sandoval ret = 0;
987c55ee0cSOmar Sandoval out:
997c55ee0cSOmar Sandoval btrfs_release_path(path);
1007c55ee0cSOmar Sandoval return ret;
1017c55ee0cSOmar Sandoval invalid:
1023c7251f2SDavid Sterba test_err("free space tree is invalid");
1037c55ee0cSOmar Sandoval ret = -EINVAL;
1047c55ee0cSOmar Sandoval goto out;
1057c55ee0cSOmar Sandoval }
1067c55ee0cSOmar Sandoval
check_free_space_extents(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info,struct btrfs_block_group * cache,struct btrfs_path * path,const struct free_space_extent * const extents,unsigned int num_extents)1077c55ee0cSOmar Sandoval static int check_free_space_extents(struct btrfs_trans_handle *trans,
1087c55ee0cSOmar Sandoval struct btrfs_fs_info *fs_info,
10932da5386SDavid Sterba struct btrfs_block_group *cache,
1107c55ee0cSOmar Sandoval struct btrfs_path *path,
111d2d9ac6aSDavid Sterba const struct free_space_extent * const extents,
1127c55ee0cSOmar Sandoval unsigned int num_extents)
1137c55ee0cSOmar Sandoval {
1147c55ee0cSOmar Sandoval struct btrfs_free_space_info *info;
1157c55ee0cSOmar Sandoval u32 flags;
1167c55ee0cSOmar Sandoval int ret;
1177c55ee0cSOmar Sandoval
1182ccf545eSDavid Sterba info = search_free_space_info(trans, cache, path, 0);
1197c55ee0cSOmar Sandoval if (IS_ERR(info)) {
1203c7251f2SDavid Sterba test_err("could not find free space info");
1217c55ee0cSOmar Sandoval btrfs_release_path(path);
1227c55ee0cSOmar Sandoval return PTR_ERR(info);
1237c55ee0cSOmar Sandoval }
1247c55ee0cSOmar Sandoval flags = btrfs_free_space_flags(path->nodes[0], info);
1257c55ee0cSOmar Sandoval btrfs_release_path(path);
1267c55ee0cSOmar Sandoval
1277c55ee0cSOmar Sandoval ret = __check_free_space_extents(trans, fs_info, cache, path, extents,
1287c55ee0cSOmar Sandoval num_extents);
1297c55ee0cSOmar Sandoval if (ret)
1307c55ee0cSOmar Sandoval return ret;
1317c55ee0cSOmar Sandoval
1327c55ee0cSOmar Sandoval /* Flip it to the other format and check that for good measure. */
1337c55ee0cSOmar Sandoval if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
1345296c2bfSNikolay Borisov ret = convert_free_space_to_extents(trans, cache, path);
1357c55ee0cSOmar Sandoval if (ret) {
1363c7251f2SDavid Sterba test_err("could not convert to extents");
1377c55ee0cSOmar Sandoval return ret;
1387c55ee0cSOmar Sandoval }
1397c55ee0cSOmar Sandoval } else {
140719fb4deSNikolay Borisov ret = convert_free_space_to_bitmaps(trans, cache, path);
1417c55ee0cSOmar Sandoval if (ret) {
1423c7251f2SDavid Sterba test_err("could not convert to bitmaps");
1437c55ee0cSOmar Sandoval return ret;
1447c55ee0cSOmar Sandoval }
1457c55ee0cSOmar Sandoval }
1467c55ee0cSOmar Sandoval return __check_free_space_extents(trans, fs_info, cache, path, extents,
1477c55ee0cSOmar Sandoval num_extents);
1487c55ee0cSOmar Sandoval }
1497c55ee0cSOmar Sandoval
test_empty_block_group(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info,struct btrfs_block_group * cache,struct btrfs_path * path,u32 alignment)1507c55ee0cSOmar Sandoval static int test_empty_block_group(struct btrfs_trans_handle *trans,
1517c55ee0cSOmar Sandoval struct btrfs_fs_info *fs_info,
15232da5386SDavid Sterba struct btrfs_block_group *cache,
153781e3bcfSOmar Sandoval struct btrfs_path *path,
154781e3bcfSOmar Sandoval u32 alignment)
1557c55ee0cSOmar Sandoval {
156d2d9ac6aSDavid Sterba const struct free_space_extent extents[] = {
157b3470b5dSDavid Sterba {cache->start, cache->length},
1587c55ee0cSOmar Sandoval };
1597c55ee0cSOmar Sandoval
1607c55ee0cSOmar Sandoval return check_free_space_extents(trans, fs_info, cache, path,
1617c55ee0cSOmar Sandoval extents, ARRAY_SIZE(extents));
1627c55ee0cSOmar Sandoval }
1637c55ee0cSOmar Sandoval
test_remove_all(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info,struct btrfs_block_group * cache,struct btrfs_path * path,u32 alignment)1647c55ee0cSOmar Sandoval static int test_remove_all(struct btrfs_trans_handle *trans,
1657c55ee0cSOmar Sandoval struct btrfs_fs_info *fs_info,
16632da5386SDavid Sterba struct btrfs_block_group *cache,
167781e3bcfSOmar Sandoval struct btrfs_path *path,
168781e3bcfSOmar Sandoval u32 alignment)
1697c55ee0cSOmar Sandoval {
170d2d9ac6aSDavid Sterba const struct free_space_extent extents[] = {};
1717c55ee0cSOmar Sandoval int ret;
1727c55ee0cSOmar Sandoval
173c31683a6SNikolay Borisov ret = __remove_from_free_space_tree(trans, cache, path,
174b3470b5dSDavid Sterba cache->start,
175b3470b5dSDavid Sterba cache->length);
1767c55ee0cSOmar Sandoval if (ret) {
1773c7251f2SDavid Sterba test_err("could not remove free space");
1787c55ee0cSOmar Sandoval return ret;
1797c55ee0cSOmar Sandoval }
1807c55ee0cSOmar Sandoval
1817c55ee0cSOmar Sandoval return check_free_space_extents(trans, fs_info, cache, path,
1827c55ee0cSOmar Sandoval extents, ARRAY_SIZE(extents));
1837c55ee0cSOmar Sandoval }
1847c55ee0cSOmar Sandoval
test_remove_beginning(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info,struct btrfs_block_group * cache,struct btrfs_path * path,u32 alignment)1857c55ee0cSOmar Sandoval static int test_remove_beginning(struct btrfs_trans_handle *trans,
1867c55ee0cSOmar Sandoval struct btrfs_fs_info *fs_info,
18732da5386SDavid Sterba struct btrfs_block_group *cache,
188781e3bcfSOmar Sandoval struct btrfs_path *path,
189781e3bcfSOmar Sandoval u32 alignment)
1907c55ee0cSOmar Sandoval {
191d2d9ac6aSDavid Sterba const struct free_space_extent extents[] = {
192b3470b5dSDavid Sterba {cache->start + alignment, cache->length - alignment},
1937c55ee0cSOmar Sandoval };
1947c55ee0cSOmar Sandoval int ret;
1957c55ee0cSOmar Sandoval
196c31683a6SNikolay Borisov ret = __remove_from_free_space_tree(trans, cache, path,
197b3470b5dSDavid Sterba cache->start, alignment);
1987c55ee0cSOmar Sandoval if (ret) {
1993c7251f2SDavid Sterba test_err("could not remove free space");
2007c55ee0cSOmar Sandoval return ret;
2017c55ee0cSOmar Sandoval }
2027c55ee0cSOmar Sandoval
2037c55ee0cSOmar Sandoval return check_free_space_extents(trans, fs_info, cache, path,
2047c55ee0cSOmar Sandoval extents, ARRAY_SIZE(extents));
2057c55ee0cSOmar Sandoval
2067c55ee0cSOmar Sandoval }
2077c55ee0cSOmar Sandoval
test_remove_end(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info,struct btrfs_block_group * cache,struct btrfs_path * path,u32 alignment)2087c55ee0cSOmar Sandoval static int test_remove_end(struct btrfs_trans_handle *trans,
2097c55ee0cSOmar Sandoval struct btrfs_fs_info *fs_info,
21032da5386SDavid Sterba struct btrfs_block_group *cache,
211781e3bcfSOmar Sandoval struct btrfs_path *path,
212781e3bcfSOmar Sandoval u32 alignment)
2137c55ee0cSOmar Sandoval {
214d2d9ac6aSDavid Sterba const struct free_space_extent extents[] = {
215b3470b5dSDavid Sterba {cache->start, cache->length - alignment},
2167c55ee0cSOmar Sandoval };
2177c55ee0cSOmar Sandoval int ret;
2187c55ee0cSOmar Sandoval
219c31683a6SNikolay Borisov ret = __remove_from_free_space_tree(trans, cache, path,
220b3470b5dSDavid Sterba cache->start + cache->length - alignment,
221781e3bcfSOmar Sandoval alignment);
2227c55ee0cSOmar Sandoval if (ret) {
2233c7251f2SDavid Sterba test_err("could not remove free space");
2247c55ee0cSOmar Sandoval return ret;
2257c55ee0cSOmar Sandoval }
2267c55ee0cSOmar Sandoval
2277c55ee0cSOmar Sandoval return check_free_space_extents(trans, fs_info, cache, path,
2287c55ee0cSOmar Sandoval extents, ARRAY_SIZE(extents));
2297c55ee0cSOmar Sandoval }
2307c55ee0cSOmar Sandoval
test_remove_middle(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info,struct btrfs_block_group * cache,struct btrfs_path * path,u32 alignment)2317c55ee0cSOmar Sandoval static int test_remove_middle(struct btrfs_trans_handle *trans,
2327c55ee0cSOmar Sandoval struct btrfs_fs_info *fs_info,
23332da5386SDavid Sterba struct btrfs_block_group *cache,
234781e3bcfSOmar Sandoval struct btrfs_path *path,
235781e3bcfSOmar Sandoval u32 alignment)
2367c55ee0cSOmar Sandoval {
237d2d9ac6aSDavid Sterba const struct free_space_extent extents[] = {
238b3470b5dSDavid Sterba {cache->start, alignment},
239b3470b5dSDavid Sterba {cache->start + 2 * alignment, cache->length - 2 * alignment},
2407c55ee0cSOmar Sandoval };
2417c55ee0cSOmar Sandoval int ret;
2427c55ee0cSOmar Sandoval
243c31683a6SNikolay Borisov ret = __remove_from_free_space_tree(trans, cache, path,
244b3470b5dSDavid Sterba cache->start + alignment,
245781e3bcfSOmar Sandoval alignment);
2467c55ee0cSOmar Sandoval if (ret) {
2473c7251f2SDavid Sterba test_err("could not remove free space");
2487c55ee0cSOmar Sandoval return ret;
2497c55ee0cSOmar Sandoval }
2507c55ee0cSOmar Sandoval
2517c55ee0cSOmar Sandoval return check_free_space_extents(trans, fs_info, cache, path,
2527c55ee0cSOmar Sandoval extents, ARRAY_SIZE(extents));
2537c55ee0cSOmar Sandoval }
2547c55ee0cSOmar Sandoval
test_merge_left(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info,struct btrfs_block_group * cache,struct btrfs_path * path,u32 alignment)2557c55ee0cSOmar Sandoval static int test_merge_left(struct btrfs_trans_handle *trans,
2567c55ee0cSOmar Sandoval struct btrfs_fs_info *fs_info,
25732da5386SDavid Sterba struct btrfs_block_group *cache,
258781e3bcfSOmar Sandoval struct btrfs_path *path,
259781e3bcfSOmar Sandoval u32 alignment)
2607c55ee0cSOmar Sandoval {
261d2d9ac6aSDavid Sterba const struct free_space_extent extents[] = {
262b3470b5dSDavid Sterba {cache->start, 2 * alignment},
2637c55ee0cSOmar Sandoval };
2647c55ee0cSOmar Sandoval int ret;
2657c55ee0cSOmar Sandoval
266c31683a6SNikolay Borisov ret = __remove_from_free_space_tree(trans, cache, path,
267b3470b5dSDavid Sterba cache->start, cache->length);
2687c55ee0cSOmar Sandoval if (ret) {
2693c7251f2SDavid Sterba test_err("could not remove free space");
2707c55ee0cSOmar Sandoval return ret;
2717c55ee0cSOmar Sandoval }
2727c55ee0cSOmar Sandoval
273b3470b5dSDavid Sterba ret = __add_to_free_space_tree(trans, cache, path, cache->start,
2742d5cffa1SNikolay Borisov alignment);
2757c55ee0cSOmar Sandoval if (ret) {
2763c7251f2SDavid Sterba test_err("could not add free space");
2777c55ee0cSOmar Sandoval return ret;
2787c55ee0cSOmar Sandoval }
2797c55ee0cSOmar Sandoval
2802d5cffa1SNikolay Borisov ret = __add_to_free_space_tree(trans, cache, path,
281b3470b5dSDavid Sterba cache->start + alignment,
282781e3bcfSOmar Sandoval alignment);
2837c55ee0cSOmar Sandoval if (ret) {
2843c7251f2SDavid Sterba test_err("could not add free space");
2857c55ee0cSOmar Sandoval return ret;
2867c55ee0cSOmar Sandoval }
2877c55ee0cSOmar Sandoval
2887c55ee0cSOmar Sandoval return check_free_space_extents(trans, fs_info, cache, path,
2897c55ee0cSOmar Sandoval extents, ARRAY_SIZE(extents));
2907c55ee0cSOmar Sandoval }
2917c55ee0cSOmar Sandoval
test_merge_right(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info,struct btrfs_block_group * cache,struct btrfs_path * path,u32 alignment)2927c55ee0cSOmar Sandoval static int test_merge_right(struct btrfs_trans_handle *trans,
2937c55ee0cSOmar Sandoval struct btrfs_fs_info *fs_info,
29432da5386SDavid Sterba struct btrfs_block_group *cache,
295781e3bcfSOmar Sandoval struct btrfs_path *path,
296781e3bcfSOmar Sandoval u32 alignment)
2977c55ee0cSOmar Sandoval {
298d2d9ac6aSDavid Sterba const struct free_space_extent extents[] = {
299b3470b5dSDavid Sterba {cache->start + alignment, 2 * alignment},
3007c55ee0cSOmar Sandoval };
3017c55ee0cSOmar Sandoval int ret;
3027c55ee0cSOmar Sandoval
303c31683a6SNikolay Borisov ret = __remove_from_free_space_tree(trans, cache, path,
304b3470b5dSDavid Sterba cache->start, cache->length);
3057c55ee0cSOmar Sandoval if (ret) {
3063c7251f2SDavid Sterba test_err("could not remove free space");
3077c55ee0cSOmar Sandoval return ret;
3087c55ee0cSOmar Sandoval }
3097c55ee0cSOmar Sandoval
3102d5cffa1SNikolay Borisov ret = __add_to_free_space_tree(trans, cache, path,
311b3470b5dSDavid Sterba cache->start + 2 * alignment,
312781e3bcfSOmar Sandoval alignment);
3137c55ee0cSOmar Sandoval if (ret) {
3143c7251f2SDavid Sterba test_err("could not add free space");
3157c55ee0cSOmar Sandoval return ret;
3167c55ee0cSOmar Sandoval }
3177c55ee0cSOmar Sandoval
3182d5cffa1SNikolay Borisov ret = __add_to_free_space_tree(trans, cache, path,
319b3470b5dSDavid Sterba cache->start + alignment,
320781e3bcfSOmar Sandoval alignment);
3217c55ee0cSOmar Sandoval if (ret) {
3223c7251f2SDavid Sterba test_err("could not add free space");
3237c55ee0cSOmar Sandoval return ret;
3247c55ee0cSOmar Sandoval }
3257c55ee0cSOmar Sandoval
3267c55ee0cSOmar Sandoval return check_free_space_extents(trans, fs_info, cache, path,
3277c55ee0cSOmar Sandoval extents, ARRAY_SIZE(extents));
3287c55ee0cSOmar Sandoval }
3297c55ee0cSOmar Sandoval
test_merge_both(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info,struct btrfs_block_group * cache,struct btrfs_path * path,u32 alignment)3307c55ee0cSOmar Sandoval static int test_merge_both(struct btrfs_trans_handle *trans,
3317c55ee0cSOmar Sandoval struct btrfs_fs_info *fs_info,
33232da5386SDavid Sterba struct btrfs_block_group *cache,
333781e3bcfSOmar Sandoval struct btrfs_path *path,
334781e3bcfSOmar Sandoval u32 alignment)
3357c55ee0cSOmar Sandoval {
336d2d9ac6aSDavid Sterba const struct free_space_extent extents[] = {
337b3470b5dSDavid Sterba {cache->start, 3 * alignment},
3387c55ee0cSOmar Sandoval };
3397c55ee0cSOmar Sandoval int ret;
3407c55ee0cSOmar Sandoval
341c31683a6SNikolay Borisov ret = __remove_from_free_space_tree(trans, cache, path,
342b3470b5dSDavid Sterba cache->start, cache->length);
3437c55ee0cSOmar Sandoval if (ret) {
3443c7251f2SDavid Sterba test_err("could not remove free space");
3457c55ee0cSOmar Sandoval return ret;
3467c55ee0cSOmar Sandoval }
3477c55ee0cSOmar Sandoval
348b3470b5dSDavid Sterba ret = __add_to_free_space_tree(trans, cache, path, cache->start,
3492d5cffa1SNikolay Borisov alignment);
3507c55ee0cSOmar Sandoval if (ret) {
3513c7251f2SDavid Sterba test_err("could not add free space");
3527c55ee0cSOmar Sandoval return ret;
3537c55ee0cSOmar Sandoval }
3547c55ee0cSOmar Sandoval
3552d5cffa1SNikolay Borisov ret = __add_to_free_space_tree(trans, cache, path,
356b3470b5dSDavid Sterba cache->start + 2 * alignment, alignment);
3577c55ee0cSOmar Sandoval if (ret) {
3583c7251f2SDavid Sterba test_err("could not add free space");
3597c55ee0cSOmar Sandoval return ret;
3607c55ee0cSOmar Sandoval }
3617c55ee0cSOmar Sandoval
3622d5cffa1SNikolay Borisov ret = __add_to_free_space_tree(trans, cache, path,
363b3470b5dSDavid Sterba cache->start + alignment, alignment);
3647c55ee0cSOmar Sandoval if (ret) {
3653c7251f2SDavid Sterba test_err("could not add free space");
3667c55ee0cSOmar Sandoval return ret;
3677c55ee0cSOmar Sandoval }
3687c55ee0cSOmar Sandoval
3697c55ee0cSOmar Sandoval return check_free_space_extents(trans, fs_info, cache, path,
3707c55ee0cSOmar Sandoval extents, ARRAY_SIZE(extents));
3717c55ee0cSOmar Sandoval }
3727c55ee0cSOmar Sandoval
test_merge_none(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info,struct btrfs_block_group * cache,struct btrfs_path * path,u32 alignment)3737c55ee0cSOmar Sandoval static int test_merge_none(struct btrfs_trans_handle *trans,
3747c55ee0cSOmar Sandoval struct btrfs_fs_info *fs_info,
37532da5386SDavid Sterba struct btrfs_block_group *cache,
376781e3bcfSOmar Sandoval struct btrfs_path *path,
377781e3bcfSOmar Sandoval u32 alignment)
3787c55ee0cSOmar Sandoval {
379d2d9ac6aSDavid Sterba const struct free_space_extent extents[] = {
380b3470b5dSDavid Sterba {cache->start, alignment},
381b3470b5dSDavid Sterba {cache->start + 2 * alignment, alignment},
382b3470b5dSDavid Sterba {cache->start + 4 * alignment, alignment},
3837c55ee0cSOmar Sandoval };
3847c55ee0cSOmar Sandoval int ret;
3857c55ee0cSOmar Sandoval
386c31683a6SNikolay Borisov ret = __remove_from_free_space_tree(trans, cache, path,
387b3470b5dSDavid Sterba cache->start, cache->length);
3887c55ee0cSOmar Sandoval if (ret) {
3893c7251f2SDavid Sterba test_err("could not remove free space");
3907c55ee0cSOmar Sandoval return ret;
3917c55ee0cSOmar Sandoval }
3927c55ee0cSOmar Sandoval
393b3470b5dSDavid Sterba ret = __add_to_free_space_tree(trans, cache, path, cache->start,
3942d5cffa1SNikolay Borisov alignment);
3957c55ee0cSOmar Sandoval if (ret) {
3963c7251f2SDavid Sterba test_err("could not add free space");
3977c55ee0cSOmar Sandoval return ret;
3987c55ee0cSOmar Sandoval }
3997c55ee0cSOmar Sandoval
4002d5cffa1SNikolay Borisov ret = __add_to_free_space_tree(trans, cache, path,
401b3470b5dSDavid Sterba cache->start + 4 * alignment, alignment);
4027c55ee0cSOmar Sandoval if (ret) {
4033c7251f2SDavid Sterba test_err("could not add free space");
4047c55ee0cSOmar Sandoval return ret;
4057c55ee0cSOmar Sandoval }
4067c55ee0cSOmar Sandoval
4072d5cffa1SNikolay Borisov ret = __add_to_free_space_tree(trans, cache, path,
408b3470b5dSDavid Sterba cache->start + 2 * alignment, alignment);
4097c55ee0cSOmar Sandoval if (ret) {
4103c7251f2SDavid Sterba test_err("could not add free space");
4117c55ee0cSOmar Sandoval return ret;
4127c55ee0cSOmar Sandoval }
4137c55ee0cSOmar Sandoval
4147c55ee0cSOmar Sandoval return check_free_space_extents(trans, fs_info, cache, path,
4157c55ee0cSOmar Sandoval extents, ARRAY_SIZE(extents));
4167c55ee0cSOmar Sandoval }
4177c55ee0cSOmar Sandoval
4187c55ee0cSOmar Sandoval typedef int (*test_func_t)(struct btrfs_trans_handle *,
4197c55ee0cSOmar Sandoval struct btrfs_fs_info *,
42032da5386SDavid Sterba struct btrfs_block_group *,
421781e3bcfSOmar Sandoval struct btrfs_path *,
422781e3bcfSOmar Sandoval u32 alignment);
4237c55ee0cSOmar Sandoval
run_test(test_func_t test_func,int bitmaps,u32 sectorsize,u32 nodesize,u32 alignment)424781e3bcfSOmar Sandoval static int run_test(test_func_t test_func, int bitmaps, u32 sectorsize,
425781e3bcfSOmar Sandoval u32 nodesize, u32 alignment)
4267c55ee0cSOmar Sandoval {
4277c0260eeSJeff Mahoney struct btrfs_fs_info *fs_info;
4287c55ee0cSOmar Sandoval struct btrfs_root *root = NULL;
42932da5386SDavid Sterba struct btrfs_block_group *cache = NULL;
4307c55ee0cSOmar Sandoval struct btrfs_trans_handle trans;
4317c55ee0cSOmar Sandoval struct btrfs_path *path = NULL;
4327c55ee0cSOmar Sandoval int ret;
4337c55ee0cSOmar Sandoval
434da17066cSJeff Mahoney fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
4357c0260eeSJeff Mahoney if (!fs_info) {
43637b2a7bcSDavid Sterba test_std_err(TEST_ALLOC_FS_INFO);
4377c0260eeSJeff Mahoney ret = -ENOMEM;
4387c55ee0cSOmar Sandoval goto out;
4397c55ee0cSOmar Sandoval }
4407c55ee0cSOmar Sandoval
441da17066cSJeff Mahoney root = btrfs_alloc_dummy_root(fs_info);
4427c0260eeSJeff Mahoney if (IS_ERR(root)) {
44352ab7bcaSDavid Sterba test_std_err(TEST_ALLOC_ROOT);
4447c0260eeSJeff Mahoney ret = PTR_ERR(root);
4457c55ee0cSOmar Sandoval goto out;
4467c55ee0cSOmar Sandoval }
4477c55ee0cSOmar Sandoval
4487c55ee0cSOmar Sandoval btrfs_set_super_compat_ro_flags(root->fs_info->super_copy,
4497c55ee0cSOmar Sandoval BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE);
450abed4aaaSJosef Bacik root->root_key.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID;
451abed4aaaSJosef Bacik root->root_key.type = BTRFS_ROOT_ITEM_KEY;
452abed4aaaSJosef Bacik root->root_key.offset = 0;
453abed4aaaSJosef Bacik btrfs_global_root_insert(root);
4547c55ee0cSOmar Sandoval root->fs_info->tree_root = root;
4557c55ee0cSOmar Sandoval
456da17066cSJeff Mahoney root->node = alloc_test_extent_buffer(root->fs_info, nodesize);
457b6293c82SDan Carpenter if (IS_ERR(root->node)) {
4589e3d9f84SDavid Sterba test_std_err(TEST_ALLOC_EXTENT_BUFFER);
459b6293c82SDan Carpenter ret = PTR_ERR(root->node);
4607c55ee0cSOmar Sandoval goto out;
4617c55ee0cSOmar Sandoval }
4627c55ee0cSOmar Sandoval btrfs_set_header_level(root->node, 0);
4637c55ee0cSOmar Sandoval btrfs_set_header_nritems(root->node, 0);
464b9ef22deSFeifei Xu root->alloc_bytenr += 2 * nodesize;
4657c55ee0cSOmar Sandoval
466da17066cSJeff Mahoney cache = btrfs_alloc_dummy_block_group(fs_info, 8 * alignment);
4677c55ee0cSOmar Sandoval if (!cache) {
4683199366dSDavid Sterba test_std_err(TEST_ALLOC_BLOCK_GROUP);
4697c55ee0cSOmar Sandoval ret = -ENOMEM;
4707c55ee0cSOmar Sandoval goto out;
4717c55ee0cSOmar Sandoval }
4727c55ee0cSOmar Sandoval cache->bitmap_low_thresh = 0;
4737c55ee0cSOmar Sandoval cache->bitmap_high_thresh = (u32)-1;
474*0d7764ffSDavid Sterba set_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &cache->runtime_flags);
475aa66b0bbSKinglong Mee cache->fs_info = root->fs_info;
4767c55ee0cSOmar Sandoval
477483bce06SNikolay Borisov btrfs_init_dummy_trans(&trans, root->fs_info);
4787c55ee0cSOmar Sandoval
4797c55ee0cSOmar Sandoval path = btrfs_alloc_path();
4807c55ee0cSOmar Sandoval if (!path) {
481770e0cc0SDavid Sterba test_std_err(TEST_ALLOC_ROOT);
4829ca2e97fSChristophe JAILLET ret = -ENOMEM;
4839ca2e97fSChristophe JAILLET goto out;
4847c55ee0cSOmar Sandoval }
4857c55ee0cSOmar Sandoval
486e4e0711cSNikolay Borisov ret = add_block_group_free_space(&trans, cache);
4877c55ee0cSOmar Sandoval if (ret) {
4883c7251f2SDavid Sterba test_err("could not add block group free space");
4897c55ee0cSOmar Sandoval goto out;
4907c55ee0cSOmar Sandoval }
4917c55ee0cSOmar Sandoval
4927c55ee0cSOmar Sandoval if (bitmaps) {
493719fb4deSNikolay Borisov ret = convert_free_space_to_bitmaps(&trans, cache, path);
4947c55ee0cSOmar Sandoval if (ret) {
4953c7251f2SDavid Sterba test_err("could not convert block group to bitmaps");
4967c55ee0cSOmar Sandoval goto out;
4977c55ee0cSOmar Sandoval }
4987c55ee0cSOmar Sandoval }
4997c55ee0cSOmar Sandoval
500781e3bcfSOmar Sandoval ret = test_func(&trans, root->fs_info, cache, path, alignment);
5017c55ee0cSOmar Sandoval if (ret)
5027c55ee0cSOmar Sandoval goto out;
5037c55ee0cSOmar Sandoval
504f3f72779SNikolay Borisov ret = remove_block_group_free_space(&trans, cache);
5057c55ee0cSOmar Sandoval if (ret) {
5063c7251f2SDavid Sterba test_err("could not remove block group free space");
5077c55ee0cSOmar Sandoval goto out;
5087c55ee0cSOmar Sandoval }
5097c55ee0cSOmar Sandoval
5107c55ee0cSOmar Sandoval if (btrfs_header_nritems(root->node) != 0) {
5113c7251f2SDavid Sterba test_err("free space tree has leftover items");
5127c55ee0cSOmar Sandoval ret = -EINVAL;
5137c55ee0cSOmar Sandoval goto out;
5147c55ee0cSOmar Sandoval }
5157c55ee0cSOmar Sandoval
5167c55ee0cSOmar Sandoval ret = 0;
5177c55ee0cSOmar Sandoval out:
5187c55ee0cSOmar Sandoval btrfs_free_path(path);
5197c55ee0cSOmar Sandoval btrfs_free_dummy_block_group(cache);
5207c55ee0cSOmar Sandoval btrfs_free_dummy_root(root);
5217c0260eeSJeff Mahoney btrfs_free_dummy_fs_info(fs_info);
5227c55ee0cSOmar Sandoval return ret;
5237c55ee0cSOmar Sandoval }
5247c55ee0cSOmar Sandoval
run_test_both_formats(test_func_t test_func,u32 sectorsize,u32 nodesize,u32 alignment)525781e3bcfSOmar Sandoval static int run_test_both_formats(test_func_t test_func, u32 sectorsize,
526781e3bcfSOmar Sandoval u32 nodesize, u32 alignment)
5277c55ee0cSOmar Sandoval {
528781e3bcfSOmar Sandoval int test_ret = 0;
5297c55ee0cSOmar Sandoval int ret;
5307c55ee0cSOmar Sandoval
531781e3bcfSOmar Sandoval ret = run_test(test_func, 0, sectorsize, nodesize, alignment);
532781e3bcfSOmar Sandoval if (ret) {
5333c7251f2SDavid Sterba test_err(
534d75f773cSSakari Ailus "%ps failed with extents, sectorsize=%u, nodesize=%u, alignment=%u",
535781e3bcfSOmar Sandoval test_func, sectorsize, nodesize, alignment);
536781e3bcfSOmar Sandoval test_ret = ret;
537781e3bcfSOmar Sandoval }
538781e3bcfSOmar Sandoval
539781e3bcfSOmar Sandoval ret = run_test(test_func, 1, sectorsize, nodesize, alignment);
540781e3bcfSOmar Sandoval if (ret) {
5413c7251f2SDavid Sterba test_err(
542d75f773cSSakari Ailus "%ps failed with bitmaps, sectorsize=%u, nodesize=%u, alignment=%u",
543781e3bcfSOmar Sandoval test_func, sectorsize, nodesize, alignment);
544781e3bcfSOmar Sandoval test_ret = ret;
545781e3bcfSOmar Sandoval }
546781e3bcfSOmar Sandoval
547781e3bcfSOmar Sandoval return test_ret;
5487c55ee0cSOmar Sandoval }
5497c55ee0cSOmar Sandoval
btrfs_test_free_space_tree(u32 sectorsize,u32 nodesize)550b9ef22deSFeifei Xu int btrfs_test_free_space_tree(u32 sectorsize, u32 nodesize)
5517c55ee0cSOmar Sandoval {
5527c55ee0cSOmar Sandoval test_func_t tests[] = {
5537c55ee0cSOmar Sandoval test_empty_block_group,
5547c55ee0cSOmar Sandoval test_remove_all,
5557c55ee0cSOmar Sandoval test_remove_beginning,
5567c55ee0cSOmar Sandoval test_remove_end,
5577c55ee0cSOmar Sandoval test_remove_middle,
5587c55ee0cSOmar Sandoval test_merge_left,
5597c55ee0cSOmar Sandoval test_merge_right,
5607c55ee0cSOmar Sandoval test_merge_both,
5617c55ee0cSOmar Sandoval test_merge_none,
5627c55ee0cSOmar Sandoval };
563781e3bcfSOmar Sandoval u32 bitmap_alignment;
564781e3bcfSOmar Sandoval int test_ret = 0;
5657c55ee0cSOmar Sandoval int i;
5667c55ee0cSOmar Sandoval
567781e3bcfSOmar Sandoval /*
568781e3bcfSOmar Sandoval * Align some operations to a page to flush out bugs in the extent
569781e3bcfSOmar Sandoval * buffer bitmap handling of highmem.
570781e3bcfSOmar Sandoval */
571781e3bcfSOmar Sandoval bitmap_alignment = BTRFS_FREE_SPACE_BITMAP_BITS * PAGE_SIZE;
572781e3bcfSOmar Sandoval
573315b76b4SDavid Sterba test_msg("running free space tree tests");
5747c55ee0cSOmar Sandoval for (i = 0; i < ARRAY_SIZE(tests); i++) {
575781e3bcfSOmar Sandoval int ret;
576781e3bcfSOmar Sandoval
577781e3bcfSOmar Sandoval ret = run_test_both_formats(tests[i], sectorsize, nodesize,
578781e3bcfSOmar Sandoval sectorsize);
579781e3bcfSOmar Sandoval if (ret)
580781e3bcfSOmar Sandoval test_ret = ret;
581781e3bcfSOmar Sandoval
582781e3bcfSOmar Sandoval ret = run_test_both_formats(tests[i], sectorsize, nodesize,
583781e3bcfSOmar Sandoval bitmap_alignment);
584781e3bcfSOmar Sandoval if (ret)
585781e3bcfSOmar Sandoval test_ret = ret;
5867c55ee0cSOmar Sandoval }
5877c55ee0cSOmar Sandoval
588781e3bcfSOmar Sandoval return test_ret;
5897c55ee0cSOmar Sandoval }
590