xref: /openbmc/linux/fs/btrfs/tests/free-space-tree-tests.c (revision 5296c2bf51700451062dda22cd03c01d0104ccbc)
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"
127c55ee0cSOmar Sandoval 
137c55ee0cSOmar Sandoval struct free_space_extent {
140e675785SDavid Sterba 	u64 start;
150e675785SDavid Sterba 	u64 length;
167c55ee0cSOmar Sandoval };
177c55ee0cSOmar Sandoval 
187c55ee0cSOmar Sandoval static int __check_free_space_extents(struct btrfs_trans_handle *trans,
197c55ee0cSOmar Sandoval 				      struct btrfs_fs_info *fs_info,
207c55ee0cSOmar Sandoval 				      struct btrfs_block_group_cache *cache,
217c55ee0cSOmar Sandoval 				      struct btrfs_path *path,
22d2d9ac6aSDavid Sterba 				      const struct free_space_extent * const extents,
237c55ee0cSOmar Sandoval 				      unsigned int num_extents)
247c55ee0cSOmar Sandoval {
257c55ee0cSOmar Sandoval 	struct btrfs_free_space_info *info;
267c55ee0cSOmar Sandoval 	struct btrfs_key key;
277c55ee0cSOmar Sandoval 	int prev_bit = 0, bit;
287c55ee0cSOmar Sandoval 	u64 extent_start = 0, offset, end;
297c55ee0cSOmar Sandoval 	u32 flags, extent_count;
307c55ee0cSOmar Sandoval 	unsigned int i;
317c55ee0cSOmar Sandoval 	int ret;
327c55ee0cSOmar Sandoval 
337c55ee0cSOmar Sandoval 	info = search_free_space_info(trans, fs_info, cache, path, 0);
347c55ee0cSOmar Sandoval 	if (IS_ERR(info)) {
357c55ee0cSOmar Sandoval 		test_msg("Could not find free space info\n");
367c55ee0cSOmar Sandoval 		ret = PTR_ERR(info);
377c55ee0cSOmar Sandoval 		goto out;
387c55ee0cSOmar Sandoval 	}
397c55ee0cSOmar Sandoval 	flags = btrfs_free_space_flags(path->nodes[0], info);
407c55ee0cSOmar Sandoval 	extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
417c55ee0cSOmar Sandoval 
427c55ee0cSOmar Sandoval 	if (extent_count != num_extents) {
437c55ee0cSOmar Sandoval 		test_msg("Extent count is wrong\n");
447c55ee0cSOmar Sandoval 		ret = -EINVAL;
457c55ee0cSOmar Sandoval 		goto out;
467c55ee0cSOmar Sandoval 	}
477c55ee0cSOmar Sandoval 	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
487c55ee0cSOmar Sandoval 		if (path->slots[0] != 0)
497c55ee0cSOmar Sandoval 			goto invalid;
507c55ee0cSOmar Sandoval 		end = cache->key.objectid + cache->key.offset;
517c55ee0cSOmar Sandoval 		i = 0;
527c55ee0cSOmar Sandoval 		while (++path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
537c55ee0cSOmar Sandoval 			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
547c55ee0cSOmar Sandoval 			if (key.type != BTRFS_FREE_SPACE_BITMAP_KEY)
557c55ee0cSOmar Sandoval 				goto invalid;
567c55ee0cSOmar Sandoval 			offset = key.objectid;
577c55ee0cSOmar Sandoval 			while (offset < key.objectid + key.offset) {
587c55ee0cSOmar Sandoval 				bit = free_space_test_bit(cache, path, offset);
597c55ee0cSOmar Sandoval 				if (prev_bit == 0 && bit == 1) {
607c55ee0cSOmar Sandoval 					extent_start = offset;
617c55ee0cSOmar Sandoval 				} else if (prev_bit == 1 && bit == 0) {
627c55ee0cSOmar Sandoval 					if (i >= num_extents)
637c55ee0cSOmar Sandoval 						goto invalid;
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:
1027c55ee0cSOmar Sandoval 	test_msg("Free space tree is invalid\n");
1037c55ee0cSOmar Sandoval 	ret = -EINVAL;
1047c55ee0cSOmar Sandoval 	goto out;
1057c55ee0cSOmar Sandoval }
1067c55ee0cSOmar Sandoval 
1077c55ee0cSOmar Sandoval static int check_free_space_extents(struct btrfs_trans_handle *trans,
1087c55ee0cSOmar Sandoval 				    struct btrfs_fs_info *fs_info,
1097c55ee0cSOmar Sandoval 				    struct btrfs_block_group_cache *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 
1187c55ee0cSOmar Sandoval 	info = search_free_space_info(trans, fs_info, cache, path, 0);
1197c55ee0cSOmar Sandoval 	if (IS_ERR(info)) {
1207c55ee0cSOmar Sandoval 		test_msg("Could not find free space info\n");
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) {
134*5296c2bfSNikolay Borisov 		ret = convert_free_space_to_extents(trans, cache, path);
1357c55ee0cSOmar Sandoval 		if (ret) {
1367c55ee0cSOmar Sandoval 			test_msg("Could not convert to extents\n");
1377c55ee0cSOmar Sandoval 			return ret;
1387c55ee0cSOmar Sandoval 		}
1397c55ee0cSOmar Sandoval 	} else {
140719fb4deSNikolay Borisov 		ret = convert_free_space_to_bitmaps(trans, cache, path);
1417c55ee0cSOmar Sandoval 		if (ret) {
1427c55ee0cSOmar Sandoval 			test_msg("Could not convert to bitmaps\n");
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 
1507c55ee0cSOmar Sandoval static int test_empty_block_group(struct btrfs_trans_handle *trans,
1517c55ee0cSOmar Sandoval 				  struct btrfs_fs_info *fs_info,
1527c55ee0cSOmar Sandoval 				  struct btrfs_block_group_cache *cache,
153781e3bcfSOmar Sandoval 				  struct btrfs_path *path,
154781e3bcfSOmar Sandoval 				  u32 alignment)
1557c55ee0cSOmar Sandoval {
156d2d9ac6aSDavid Sterba 	const struct free_space_extent extents[] = {
1577c55ee0cSOmar Sandoval 		{cache->key.objectid, cache->key.offset},
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 
1647c55ee0cSOmar Sandoval static int test_remove_all(struct btrfs_trans_handle *trans,
1657c55ee0cSOmar Sandoval 			   struct btrfs_fs_info *fs_info,
1667c55ee0cSOmar Sandoval 			   struct btrfs_block_group_cache *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 
1737c55ee0cSOmar Sandoval 	ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
1747c55ee0cSOmar Sandoval 					    cache->key.objectid,
1757c55ee0cSOmar Sandoval 					    cache->key.offset);
1767c55ee0cSOmar Sandoval 	if (ret) {
1777c55ee0cSOmar Sandoval 		test_msg("Could not remove free space\n");
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 
1857c55ee0cSOmar Sandoval static int test_remove_beginning(struct btrfs_trans_handle *trans,
1867c55ee0cSOmar Sandoval 				 struct btrfs_fs_info *fs_info,
1877c55ee0cSOmar Sandoval 				 struct btrfs_block_group_cache *cache,
188781e3bcfSOmar Sandoval 				 struct btrfs_path *path,
189781e3bcfSOmar Sandoval 				 u32 alignment)
1907c55ee0cSOmar Sandoval {
191d2d9ac6aSDavid Sterba 	const struct free_space_extent extents[] = {
192781e3bcfSOmar Sandoval 		{cache->key.objectid + alignment,
193781e3bcfSOmar Sandoval 			cache->key.offset - alignment},
1947c55ee0cSOmar Sandoval 	};
1957c55ee0cSOmar Sandoval 	int ret;
1967c55ee0cSOmar Sandoval 
1977c55ee0cSOmar Sandoval 	ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
198781e3bcfSOmar Sandoval 					    cache->key.objectid, alignment);
1997c55ee0cSOmar Sandoval 	if (ret) {
2007c55ee0cSOmar Sandoval 		test_msg("Could not remove free space\n");
2017c55ee0cSOmar Sandoval 		return ret;
2027c55ee0cSOmar Sandoval 	}
2037c55ee0cSOmar Sandoval 
2047c55ee0cSOmar Sandoval 	return check_free_space_extents(trans, fs_info, cache, path,
2057c55ee0cSOmar Sandoval 					extents, ARRAY_SIZE(extents));
2067c55ee0cSOmar Sandoval 
2077c55ee0cSOmar Sandoval }
2087c55ee0cSOmar Sandoval 
2097c55ee0cSOmar Sandoval static int test_remove_end(struct btrfs_trans_handle *trans,
2107c55ee0cSOmar Sandoval 			   struct btrfs_fs_info *fs_info,
2117c55ee0cSOmar Sandoval 			   struct btrfs_block_group_cache *cache,
212781e3bcfSOmar Sandoval 			   struct btrfs_path *path,
213781e3bcfSOmar Sandoval 			   u32 alignment)
2147c55ee0cSOmar Sandoval {
215d2d9ac6aSDavid Sterba 	const struct free_space_extent extents[] = {
216781e3bcfSOmar Sandoval 		{cache->key.objectid, cache->key.offset - alignment},
2177c55ee0cSOmar Sandoval 	};
2187c55ee0cSOmar Sandoval 	int ret;
2197c55ee0cSOmar Sandoval 
2207c55ee0cSOmar Sandoval 	ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
2217c55ee0cSOmar Sandoval 					    cache->key.objectid +
222781e3bcfSOmar Sandoval 					    cache->key.offset - alignment,
223781e3bcfSOmar Sandoval 					    alignment);
2247c55ee0cSOmar Sandoval 	if (ret) {
2257c55ee0cSOmar Sandoval 		test_msg("Could not remove free space\n");
2267c55ee0cSOmar Sandoval 		return ret;
2277c55ee0cSOmar Sandoval 	}
2287c55ee0cSOmar Sandoval 
2297c55ee0cSOmar Sandoval 	return check_free_space_extents(trans, fs_info, cache, path,
2307c55ee0cSOmar Sandoval 					extents, ARRAY_SIZE(extents));
2317c55ee0cSOmar Sandoval }
2327c55ee0cSOmar Sandoval 
2337c55ee0cSOmar Sandoval static int test_remove_middle(struct btrfs_trans_handle *trans,
2347c55ee0cSOmar Sandoval 			      struct btrfs_fs_info *fs_info,
2357c55ee0cSOmar Sandoval 			      struct btrfs_block_group_cache *cache,
236781e3bcfSOmar Sandoval 			      struct btrfs_path *path,
237781e3bcfSOmar Sandoval 			      u32 alignment)
2387c55ee0cSOmar Sandoval {
239d2d9ac6aSDavid Sterba 	const struct free_space_extent extents[] = {
240781e3bcfSOmar Sandoval 		{cache->key.objectid, alignment},
241781e3bcfSOmar Sandoval 		{cache->key.objectid + 2 * alignment,
242781e3bcfSOmar Sandoval 			cache->key.offset - 2 * alignment},
2437c55ee0cSOmar Sandoval 	};
2447c55ee0cSOmar Sandoval 	int ret;
2457c55ee0cSOmar Sandoval 
2467c55ee0cSOmar Sandoval 	ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
247781e3bcfSOmar Sandoval 					    cache->key.objectid + alignment,
248781e3bcfSOmar Sandoval 					    alignment);
2497c55ee0cSOmar Sandoval 	if (ret) {
2507c55ee0cSOmar Sandoval 		test_msg("Could not remove free space\n");
2517c55ee0cSOmar Sandoval 		return ret;
2527c55ee0cSOmar Sandoval 	}
2537c55ee0cSOmar Sandoval 
2547c55ee0cSOmar Sandoval 	return check_free_space_extents(trans, fs_info, cache, path,
2557c55ee0cSOmar Sandoval 					extents, ARRAY_SIZE(extents));
2567c55ee0cSOmar Sandoval }
2577c55ee0cSOmar Sandoval 
2587c55ee0cSOmar Sandoval static int test_merge_left(struct btrfs_trans_handle *trans,
2597c55ee0cSOmar Sandoval 			   struct btrfs_fs_info *fs_info,
2607c55ee0cSOmar Sandoval 			   struct btrfs_block_group_cache *cache,
261781e3bcfSOmar Sandoval 			   struct btrfs_path *path,
262781e3bcfSOmar Sandoval 			   u32 alignment)
2637c55ee0cSOmar Sandoval {
264d2d9ac6aSDavid Sterba 	const struct free_space_extent extents[] = {
265781e3bcfSOmar Sandoval 		{cache->key.objectid, 2 * alignment},
2667c55ee0cSOmar Sandoval 	};
2677c55ee0cSOmar Sandoval 	int ret;
2687c55ee0cSOmar Sandoval 
2697c55ee0cSOmar Sandoval 	ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
2707c55ee0cSOmar Sandoval 					    cache->key.objectid,
2717c55ee0cSOmar Sandoval 					    cache->key.offset);
2727c55ee0cSOmar Sandoval 	if (ret) {
2737c55ee0cSOmar Sandoval 		test_msg("Could not remove free space\n");
2747c55ee0cSOmar Sandoval 		return ret;
2757c55ee0cSOmar Sandoval 	}
2767c55ee0cSOmar Sandoval 
2772d5cffa1SNikolay Borisov 	ret = __add_to_free_space_tree(trans, cache, path, cache->key.objectid,
2782d5cffa1SNikolay Borisov 				       alignment);
2797c55ee0cSOmar Sandoval 	if (ret) {
2807c55ee0cSOmar Sandoval 		test_msg("Could not add free space\n");
2817c55ee0cSOmar Sandoval 		return ret;
2827c55ee0cSOmar Sandoval 	}
2837c55ee0cSOmar Sandoval 
2842d5cffa1SNikolay Borisov 	ret = __add_to_free_space_tree(trans, cache, path,
285781e3bcfSOmar Sandoval 				       cache->key.objectid + alignment,
286781e3bcfSOmar Sandoval 				       alignment);
2877c55ee0cSOmar Sandoval 	if (ret) {
2887c55ee0cSOmar Sandoval 		test_msg("Could not add free space\n");
2897c55ee0cSOmar Sandoval 		return ret;
2907c55ee0cSOmar Sandoval 	}
2917c55ee0cSOmar Sandoval 
2927c55ee0cSOmar Sandoval 	return check_free_space_extents(trans, fs_info, cache, path,
2937c55ee0cSOmar Sandoval 					extents, ARRAY_SIZE(extents));
2947c55ee0cSOmar Sandoval }
2957c55ee0cSOmar Sandoval 
2967c55ee0cSOmar Sandoval static int test_merge_right(struct btrfs_trans_handle *trans,
2977c55ee0cSOmar Sandoval 			   struct btrfs_fs_info *fs_info,
2987c55ee0cSOmar Sandoval 			   struct btrfs_block_group_cache *cache,
299781e3bcfSOmar Sandoval 			   struct btrfs_path *path,
300781e3bcfSOmar Sandoval 			   u32 alignment)
3017c55ee0cSOmar Sandoval {
302d2d9ac6aSDavid Sterba 	const struct free_space_extent extents[] = {
303781e3bcfSOmar Sandoval 		{cache->key.objectid + alignment, 2 * alignment},
3047c55ee0cSOmar Sandoval 	};
3057c55ee0cSOmar Sandoval 	int ret;
3067c55ee0cSOmar Sandoval 
3077c55ee0cSOmar Sandoval 	ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
3087c55ee0cSOmar Sandoval 					    cache->key.objectid,
3097c55ee0cSOmar Sandoval 					    cache->key.offset);
3107c55ee0cSOmar Sandoval 	if (ret) {
3117c55ee0cSOmar Sandoval 		test_msg("Could not remove free space\n");
3127c55ee0cSOmar Sandoval 		return ret;
3137c55ee0cSOmar Sandoval 	}
3147c55ee0cSOmar Sandoval 
3152d5cffa1SNikolay Borisov 	ret = __add_to_free_space_tree(trans, cache, path,
316781e3bcfSOmar Sandoval 				       cache->key.objectid + 2 * alignment,
317781e3bcfSOmar Sandoval 				       alignment);
3187c55ee0cSOmar Sandoval 	if (ret) {
3197c55ee0cSOmar Sandoval 		test_msg("Could not add free space\n");
3207c55ee0cSOmar Sandoval 		return ret;
3217c55ee0cSOmar Sandoval 	}
3227c55ee0cSOmar Sandoval 
3232d5cffa1SNikolay Borisov 	ret = __add_to_free_space_tree(trans, cache, path,
324781e3bcfSOmar Sandoval 				       cache->key.objectid + alignment,
325781e3bcfSOmar Sandoval 				       alignment);
3267c55ee0cSOmar Sandoval 	if (ret) {
3277c55ee0cSOmar Sandoval 		test_msg("Could not add free space\n");
3287c55ee0cSOmar Sandoval 		return ret;
3297c55ee0cSOmar Sandoval 	}
3307c55ee0cSOmar Sandoval 
3317c55ee0cSOmar Sandoval 	return check_free_space_extents(trans, fs_info, cache, path,
3327c55ee0cSOmar Sandoval 					extents, ARRAY_SIZE(extents));
3337c55ee0cSOmar Sandoval }
3347c55ee0cSOmar Sandoval 
3357c55ee0cSOmar Sandoval static int test_merge_both(struct btrfs_trans_handle *trans,
3367c55ee0cSOmar Sandoval 			   struct btrfs_fs_info *fs_info,
3377c55ee0cSOmar Sandoval 			   struct btrfs_block_group_cache *cache,
338781e3bcfSOmar Sandoval 			   struct btrfs_path *path,
339781e3bcfSOmar Sandoval 			   u32 alignment)
3407c55ee0cSOmar Sandoval {
341d2d9ac6aSDavid Sterba 	const struct free_space_extent extents[] = {
342781e3bcfSOmar Sandoval 		{cache->key.objectid, 3 * alignment},
3437c55ee0cSOmar Sandoval 	};
3447c55ee0cSOmar Sandoval 	int ret;
3457c55ee0cSOmar Sandoval 
3467c55ee0cSOmar Sandoval 	ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
3477c55ee0cSOmar Sandoval 					    cache->key.objectid,
3487c55ee0cSOmar Sandoval 					    cache->key.offset);
3497c55ee0cSOmar Sandoval 	if (ret) {
3507c55ee0cSOmar Sandoval 		test_msg("Could not remove free space\n");
3517c55ee0cSOmar Sandoval 		return ret;
3527c55ee0cSOmar Sandoval 	}
3537c55ee0cSOmar Sandoval 
3542d5cffa1SNikolay Borisov 	ret = __add_to_free_space_tree(trans, cache, path, cache->key.objectid,
3552d5cffa1SNikolay Borisov 				       alignment);
3567c55ee0cSOmar Sandoval 	if (ret) {
3577c55ee0cSOmar Sandoval 		test_msg("Could not add free space\n");
3587c55ee0cSOmar Sandoval 		return ret;
3597c55ee0cSOmar Sandoval 	}
3607c55ee0cSOmar Sandoval 
3612d5cffa1SNikolay Borisov 	ret = __add_to_free_space_tree(trans, cache, path,
362781e3bcfSOmar Sandoval 				       cache->key.objectid + 2 * alignment,
363781e3bcfSOmar Sandoval 				       alignment);
3647c55ee0cSOmar Sandoval 	if (ret) {
3657c55ee0cSOmar Sandoval 		test_msg("Could not add free space\n");
3667c55ee0cSOmar Sandoval 		return ret;
3677c55ee0cSOmar Sandoval 	}
3687c55ee0cSOmar Sandoval 
3692d5cffa1SNikolay Borisov 	ret = __add_to_free_space_tree(trans, cache, path,
370781e3bcfSOmar Sandoval 				       cache->key.objectid + alignment,
371781e3bcfSOmar Sandoval 				       alignment);
3727c55ee0cSOmar Sandoval 	if (ret) {
3737c55ee0cSOmar Sandoval 		test_msg("Could not add free space\n");
3747c55ee0cSOmar Sandoval 		return ret;
3757c55ee0cSOmar Sandoval 	}
3767c55ee0cSOmar Sandoval 
3777c55ee0cSOmar Sandoval 	return check_free_space_extents(trans, fs_info, cache, path,
3787c55ee0cSOmar Sandoval 					extents, ARRAY_SIZE(extents));
3797c55ee0cSOmar Sandoval }
3807c55ee0cSOmar Sandoval 
3817c55ee0cSOmar Sandoval static int test_merge_none(struct btrfs_trans_handle *trans,
3827c55ee0cSOmar Sandoval 			   struct btrfs_fs_info *fs_info,
3837c55ee0cSOmar Sandoval 			   struct btrfs_block_group_cache *cache,
384781e3bcfSOmar Sandoval 			   struct btrfs_path *path,
385781e3bcfSOmar Sandoval 			   u32 alignment)
3867c55ee0cSOmar Sandoval {
387d2d9ac6aSDavid Sterba 	const struct free_space_extent extents[] = {
388781e3bcfSOmar Sandoval 		{cache->key.objectid, alignment},
389781e3bcfSOmar Sandoval 		{cache->key.objectid + 2 * alignment, alignment},
390781e3bcfSOmar Sandoval 		{cache->key.objectid + 4 * alignment, alignment},
3917c55ee0cSOmar Sandoval 	};
3927c55ee0cSOmar Sandoval 	int ret;
3937c55ee0cSOmar Sandoval 
3947c55ee0cSOmar Sandoval 	ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
3957c55ee0cSOmar Sandoval 					    cache->key.objectid,
3967c55ee0cSOmar Sandoval 					    cache->key.offset);
3977c55ee0cSOmar Sandoval 	if (ret) {
3987c55ee0cSOmar Sandoval 		test_msg("Could not remove free space\n");
3997c55ee0cSOmar Sandoval 		return ret;
4007c55ee0cSOmar Sandoval 	}
4017c55ee0cSOmar Sandoval 
4022d5cffa1SNikolay Borisov 	ret = __add_to_free_space_tree(trans, cache, path, cache->key.objectid,
4032d5cffa1SNikolay Borisov 				       alignment);
4047c55ee0cSOmar Sandoval 	if (ret) {
4057c55ee0cSOmar Sandoval 		test_msg("Could not add free space\n");
4067c55ee0cSOmar Sandoval 		return ret;
4077c55ee0cSOmar Sandoval 	}
4087c55ee0cSOmar Sandoval 
4092d5cffa1SNikolay Borisov 	ret = __add_to_free_space_tree(trans, cache, path,
410781e3bcfSOmar Sandoval 				       cache->key.objectid + 4 * alignment,
411781e3bcfSOmar Sandoval 				       alignment);
4127c55ee0cSOmar Sandoval 	if (ret) {
4137c55ee0cSOmar Sandoval 		test_msg("Could not add free space\n");
4147c55ee0cSOmar Sandoval 		return ret;
4157c55ee0cSOmar Sandoval 	}
4167c55ee0cSOmar Sandoval 
4172d5cffa1SNikolay Borisov 	ret = __add_to_free_space_tree(trans, cache, path,
418781e3bcfSOmar Sandoval 				       cache->key.objectid + 2 * alignment,
419781e3bcfSOmar Sandoval 				       alignment);
4207c55ee0cSOmar Sandoval 	if (ret) {
4217c55ee0cSOmar Sandoval 		test_msg("Could not add free space\n");
4227c55ee0cSOmar Sandoval 		return ret;
4237c55ee0cSOmar Sandoval 	}
4247c55ee0cSOmar Sandoval 
4257c55ee0cSOmar Sandoval 	return check_free_space_extents(trans, fs_info, cache, path,
4267c55ee0cSOmar Sandoval 					extents, ARRAY_SIZE(extents));
4277c55ee0cSOmar Sandoval }
4287c55ee0cSOmar Sandoval 
4297c55ee0cSOmar Sandoval typedef int (*test_func_t)(struct btrfs_trans_handle *,
4307c55ee0cSOmar Sandoval 			   struct btrfs_fs_info *,
4317c55ee0cSOmar Sandoval 			   struct btrfs_block_group_cache *,
432781e3bcfSOmar Sandoval 			   struct btrfs_path *,
433781e3bcfSOmar Sandoval 			   u32 alignment);
4347c55ee0cSOmar Sandoval 
435781e3bcfSOmar Sandoval static int run_test(test_func_t test_func, int bitmaps, u32 sectorsize,
436781e3bcfSOmar Sandoval 		    u32 nodesize, u32 alignment)
4377c55ee0cSOmar Sandoval {
4387c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info;
4397c55ee0cSOmar Sandoval 	struct btrfs_root *root = NULL;
4407c55ee0cSOmar Sandoval 	struct btrfs_block_group_cache *cache = NULL;
4417c55ee0cSOmar Sandoval 	struct btrfs_trans_handle trans;
4427c55ee0cSOmar Sandoval 	struct btrfs_path *path = NULL;
4437c55ee0cSOmar Sandoval 	int ret;
4447c55ee0cSOmar Sandoval 
445da17066cSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
4467c0260eeSJeff Mahoney 	if (!fs_info) {
4477c0260eeSJeff Mahoney 		test_msg("Couldn't allocate dummy fs info\n");
4487c0260eeSJeff Mahoney 		ret = -ENOMEM;
4497c55ee0cSOmar Sandoval 		goto out;
4507c55ee0cSOmar Sandoval 	}
4517c55ee0cSOmar Sandoval 
452da17066cSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info);
4537c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
4547c0260eeSJeff Mahoney 		test_msg("Couldn't allocate dummy root\n");
4557c0260eeSJeff Mahoney 		ret = PTR_ERR(root);
4567c55ee0cSOmar Sandoval 		goto out;
4577c55ee0cSOmar Sandoval 	}
4587c55ee0cSOmar Sandoval 
4597c55ee0cSOmar Sandoval 	btrfs_set_super_compat_ro_flags(root->fs_info->super_copy,
4607c55ee0cSOmar Sandoval 					BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE);
4617c55ee0cSOmar Sandoval 	root->fs_info->free_space_root = root;
4627c55ee0cSOmar Sandoval 	root->fs_info->tree_root = root;
4637c55ee0cSOmar Sandoval 
464da17066cSJeff Mahoney 	root->node = alloc_test_extent_buffer(root->fs_info, nodesize);
4657c55ee0cSOmar Sandoval 	if (!root->node) {
4667c55ee0cSOmar Sandoval 		test_msg("Couldn't allocate dummy buffer\n");
4677c55ee0cSOmar Sandoval 		ret = -ENOMEM;
4687c55ee0cSOmar Sandoval 		goto out;
4697c55ee0cSOmar Sandoval 	}
4707c55ee0cSOmar Sandoval 	btrfs_set_header_level(root->node, 0);
4717c55ee0cSOmar Sandoval 	btrfs_set_header_nritems(root->node, 0);
472b9ef22deSFeifei Xu 	root->alloc_bytenr += 2 * nodesize;
4737c55ee0cSOmar Sandoval 
474da17066cSJeff Mahoney 	cache = btrfs_alloc_dummy_block_group(fs_info, 8 * alignment);
4757c55ee0cSOmar Sandoval 	if (!cache) {
4767c55ee0cSOmar Sandoval 		test_msg("Couldn't allocate dummy block group cache\n");
4777c55ee0cSOmar Sandoval 		ret = -ENOMEM;
4787c55ee0cSOmar Sandoval 		goto out;
4797c55ee0cSOmar Sandoval 	}
4807c55ee0cSOmar Sandoval 	cache->bitmap_low_thresh = 0;
4817c55ee0cSOmar Sandoval 	cache->bitmap_high_thresh = (u32)-1;
4827c55ee0cSOmar Sandoval 	cache->needs_free_space = 1;
483aa66b0bbSKinglong Mee 	cache->fs_info = root->fs_info;
4847c55ee0cSOmar Sandoval 
485483bce06SNikolay Borisov 	btrfs_init_dummy_trans(&trans, root->fs_info);
4867c55ee0cSOmar Sandoval 
4877c55ee0cSOmar Sandoval 	path = btrfs_alloc_path();
4887c55ee0cSOmar Sandoval 	if (!path) {
4897c55ee0cSOmar Sandoval 		test_msg("Couldn't allocate path\n");
4909ca2e97fSChristophe JAILLET 		ret = -ENOMEM;
4919ca2e97fSChristophe JAILLET 		goto out;
4927c55ee0cSOmar Sandoval 	}
4937c55ee0cSOmar Sandoval 
494e4e0711cSNikolay Borisov 	ret = add_block_group_free_space(&trans, cache);
4957c55ee0cSOmar Sandoval 	if (ret) {
4967c55ee0cSOmar Sandoval 		test_msg("Could not add block group free space\n");
4977c55ee0cSOmar Sandoval 		goto out;
4987c55ee0cSOmar Sandoval 	}
4997c55ee0cSOmar Sandoval 
5007c55ee0cSOmar Sandoval 	if (bitmaps) {
501719fb4deSNikolay Borisov 		ret = convert_free_space_to_bitmaps(&trans, cache, path);
5027c55ee0cSOmar Sandoval 		if (ret) {
5037c55ee0cSOmar Sandoval 			test_msg("Could not convert block group to bitmaps\n");
5047c55ee0cSOmar Sandoval 			goto out;
5057c55ee0cSOmar Sandoval 		}
5067c55ee0cSOmar Sandoval 	}
5077c55ee0cSOmar Sandoval 
508781e3bcfSOmar Sandoval 	ret = test_func(&trans, root->fs_info, cache, path, alignment);
5097c55ee0cSOmar Sandoval 	if (ret)
5107c55ee0cSOmar Sandoval 		goto out;
5117c55ee0cSOmar Sandoval 
512f3f72779SNikolay Borisov 	ret = remove_block_group_free_space(&trans, cache);
5137c55ee0cSOmar Sandoval 	if (ret) {
5147c55ee0cSOmar Sandoval 		test_msg("Could not remove block group free space\n");
5157c55ee0cSOmar Sandoval 		goto out;
5167c55ee0cSOmar Sandoval 	}
5177c55ee0cSOmar Sandoval 
5187c55ee0cSOmar Sandoval 	if (btrfs_header_nritems(root->node) != 0) {
5197c55ee0cSOmar Sandoval 		test_msg("Free space tree has leftover items\n");
5207c55ee0cSOmar Sandoval 		ret = -EINVAL;
5217c55ee0cSOmar Sandoval 		goto out;
5227c55ee0cSOmar Sandoval 	}
5237c55ee0cSOmar Sandoval 
5247c55ee0cSOmar Sandoval 	ret = 0;
5257c55ee0cSOmar Sandoval out:
5267c55ee0cSOmar Sandoval 	btrfs_free_path(path);
5277c55ee0cSOmar Sandoval 	btrfs_free_dummy_block_group(cache);
5287c55ee0cSOmar Sandoval 	btrfs_free_dummy_root(root);
5297c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
5307c55ee0cSOmar Sandoval 	return ret;
5317c55ee0cSOmar Sandoval }
5327c55ee0cSOmar Sandoval 
533781e3bcfSOmar Sandoval static int run_test_both_formats(test_func_t test_func, u32 sectorsize,
534781e3bcfSOmar Sandoval 				 u32 nodesize, u32 alignment)
5357c55ee0cSOmar Sandoval {
536781e3bcfSOmar Sandoval 	int test_ret = 0;
5377c55ee0cSOmar Sandoval 	int ret;
5387c55ee0cSOmar Sandoval 
539781e3bcfSOmar Sandoval 	ret = run_test(test_func, 0, sectorsize, nodesize, alignment);
540781e3bcfSOmar Sandoval 	if (ret) {
541781e3bcfSOmar Sandoval 		test_msg("%pf failed with extents, sectorsize=%u, nodesize=%u, alignment=%u\n",
542781e3bcfSOmar Sandoval 			 test_func, sectorsize, nodesize, alignment);
543781e3bcfSOmar Sandoval 		test_ret = ret;
544781e3bcfSOmar Sandoval 	}
545781e3bcfSOmar Sandoval 
546781e3bcfSOmar Sandoval 	ret = run_test(test_func, 1, sectorsize, nodesize, alignment);
547781e3bcfSOmar Sandoval 	if (ret) {
548781e3bcfSOmar Sandoval 		test_msg("%pf failed with bitmaps, sectorsize=%u, nodesize=%u, alignment=%u\n",
549781e3bcfSOmar Sandoval 			 test_func, sectorsize, nodesize, alignment);
550781e3bcfSOmar Sandoval 		test_ret = ret;
551781e3bcfSOmar Sandoval 	}
552781e3bcfSOmar Sandoval 
553781e3bcfSOmar Sandoval 	return test_ret;
5547c55ee0cSOmar Sandoval }
5557c55ee0cSOmar Sandoval 
556b9ef22deSFeifei Xu int btrfs_test_free_space_tree(u32 sectorsize, u32 nodesize)
5577c55ee0cSOmar Sandoval {
5587c55ee0cSOmar Sandoval 	test_func_t tests[] = {
5597c55ee0cSOmar Sandoval 		test_empty_block_group,
5607c55ee0cSOmar Sandoval 		test_remove_all,
5617c55ee0cSOmar Sandoval 		test_remove_beginning,
5627c55ee0cSOmar Sandoval 		test_remove_end,
5637c55ee0cSOmar Sandoval 		test_remove_middle,
5647c55ee0cSOmar Sandoval 		test_merge_left,
5657c55ee0cSOmar Sandoval 		test_merge_right,
5667c55ee0cSOmar Sandoval 		test_merge_both,
5677c55ee0cSOmar Sandoval 		test_merge_none,
5687c55ee0cSOmar Sandoval 	};
569781e3bcfSOmar Sandoval 	u32 bitmap_alignment;
570781e3bcfSOmar Sandoval 	int test_ret = 0;
5717c55ee0cSOmar Sandoval 	int i;
5727c55ee0cSOmar Sandoval 
573781e3bcfSOmar Sandoval 	/*
574781e3bcfSOmar Sandoval 	 * Align some operations to a page to flush out bugs in the extent
575781e3bcfSOmar Sandoval 	 * buffer bitmap handling of highmem.
576781e3bcfSOmar Sandoval 	 */
577781e3bcfSOmar Sandoval 	bitmap_alignment = BTRFS_FREE_SPACE_BITMAP_BITS * PAGE_SIZE;
578781e3bcfSOmar Sandoval 
5797c55ee0cSOmar Sandoval 	test_msg("Running free space tree tests\n");
5807c55ee0cSOmar Sandoval 	for (i = 0; i < ARRAY_SIZE(tests); i++) {
581781e3bcfSOmar Sandoval 		int ret;
582781e3bcfSOmar Sandoval 
583781e3bcfSOmar Sandoval 		ret = run_test_both_formats(tests[i], sectorsize, nodesize,
584781e3bcfSOmar Sandoval 					    sectorsize);
585781e3bcfSOmar Sandoval 		if (ret)
586781e3bcfSOmar Sandoval 			test_ret = ret;
587781e3bcfSOmar Sandoval 
588781e3bcfSOmar Sandoval 		ret = run_test_both_formats(tests[i], sectorsize, nodesize,
589781e3bcfSOmar Sandoval 					    bitmap_alignment);
590781e3bcfSOmar Sandoval 		if (ret)
591781e3bcfSOmar Sandoval 			test_ret = ret;
5927c55ee0cSOmar Sandoval 	}
5937c55ee0cSOmar Sandoval 
594781e3bcfSOmar Sandoval 	return test_ret;
5957c55ee0cSOmar Sandoval }
596