2 * Copyright (C) 2015 Facebook. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/types.h>
20 #include "btrfs-tests.h"
22 #include "../disk-io.h"
23 #include "../free-space-tree.h"
24 #include "../transaction.h"
26 struct free_space_extent {
31 * The test cases align their operations to this in order to hit some of the
32 * edge cases in the bitmap code.
34 #define BITMAP_RANGE (BTRFS_FREE_SPACE_BITMAP_BITS * PAGE_SIZE)
36 static int __check_free_space_extents(struct btrfs_trans_handle *trans,
37 struct btrfs_fs_info *fs_info,
38 struct btrfs_block_group_cache *cache,
39 struct btrfs_path *path,
40 struct free_space_extent *extents,
41 unsigned int num_extents)
43 struct btrfs_free_space_info *info;
45 int prev_bit = 0, bit;
46 u64 extent_start = 0, offset, end;
47 u32 flags, extent_count;
51 info = search_free_space_info(trans, fs_info, cache, path, 0);
53 test_msg("Could not find free space info\n");
57 flags = btrfs_free_space_flags(path->nodes[0], info);
58 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
60 if (extent_count != num_extents) {
61 test_msg("Extent count is wrong\n");
65 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
66 if (path->slots[0] != 0)
68 end = cache->key.objectid + cache->key.offset;
70 while (++path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
71 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
72 if (key.type != BTRFS_FREE_SPACE_BITMAP_KEY)
74 offset = key.objectid;
75 while (offset < key.objectid + key.offset) {
76 bit = free_space_test_bit(cache, path, offset);
77 if (prev_bit == 0 && bit == 1) {
78 extent_start = offset;
79 } else if (prev_bit == 1 && bit == 0) {
82 if (i >= num_extents ||
83 extent_start != extents[i].start ||
84 offset - extent_start != extents[i].length)
89 offset += cache->sectorsize;
93 if (i >= num_extents ||
94 extent_start != extents[i].start ||
95 end - extent_start != extents[i].length)
102 if (btrfs_header_nritems(path->nodes[0]) != num_extents + 1 ||
105 for (i = 0; i < num_extents; i++) {
107 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
108 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY ||
109 key.objectid != extents[i].start ||
110 key.offset != extents[i].length)
117 btrfs_release_path(path);
120 test_msg("Free space tree is invalid\n");
125 static int check_free_space_extents(struct btrfs_trans_handle *trans,
126 struct btrfs_fs_info *fs_info,
127 struct btrfs_block_group_cache *cache,
128 struct btrfs_path *path,
129 struct free_space_extent *extents,
130 unsigned int num_extents)
132 struct btrfs_free_space_info *info;
136 info = search_free_space_info(trans, fs_info, cache, path, 0);
138 test_msg("Could not find free space info\n");
139 btrfs_release_path(path);
140 return PTR_ERR(info);
142 flags = btrfs_free_space_flags(path->nodes[0], info);
143 btrfs_release_path(path);
145 ret = __check_free_space_extents(trans, fs_info, cache, path, extents,
150 /* Flip it to the other format and check that for good measure. */
151 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
152 ret = convert_free_space_to_extents(trans, fs_info, cache, path);
154 test_msg("Could not convert to extents\n");
158 ret = convert_free_space_to_bitmaps(trans, fs_info, cache, path);
160 test_msg("Could not convert to bitmaps\n");
164 return __check_free_space_extents(trans, fs_info, cache, path, extents,
168 static int test_empty_block_group(struct btrfs_trans_handle *trans,
169 struct btrfs_fs_info *fs_info,
170 struct btrfs_block_group_cache *cache,
171 struct btrfs_path *path)
173 struct free_space_extent extents[] = {
174 {cache->key.objectid, cache->key.offset},
177 return check_free_space_extents(trans, fs_info, cache, path,
178 extents, ARRAY_SIZE(extents));
181 static int test_remove_all(struct btrfs_trans_handle *trans,
182 struct btrfs_fs_info *fs_info,
183 struct btrfs_block_group_cache *cache,
184 struct btrfs_path *path)
186 struct free_space_extent extents[] = {};
189 ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
193 test_msg("Could not remove free space\n");
197 return check_free_space_extents(trans, fs_info, cache, path,
198 extents, ARRAY_SIZE(extents));
201 static int test_remove_beginning(struct btrfs_trans_handle *trans,
202 struct btrfs_fs_info *fs_info,
203 struct btrfs_block_group_cache *cache,
204 struct btrfs_path *path)
206 struct free_space_extent extents[] = {
207 {cache->key.objectid + BITMAP_RANGE,
208 cache->key.offset - BITMAP_RANGE},
212 ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
213 cache->key.objectid, BITMAP_RANGE);
215 test_msg("Could not remove free space\n");
219 return check_free_space_extents(trans, fs_info, cache, path,
220 extents, ARRAY_SIZE(extents));
224 static int test_remove_end(struct btrfs_trans_handle *trans,
225 struct btrfs_fs_info *fs_info,
226 struct btrfs_block_group_cache *cache,
227 struct btrfs_path *path)
229 struct free_space_extent extents[] = {
230 {cache->key.objectid, cache->key.offset - BITMAP_RANGE},
234 ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
235 cache->key.objectid +
236 cache->key.offset - BITMAP_RANGE,
239 test_msg("Could not remove free space\n");
243 return check_free_space_extents(trans, fs_info, cache, path,
244 extents, ARRAY_SIZE(extents));
247 static int test_remove_middle(struct btrfs_trans_handle *trans,
248 struct btrfs_fs_info *fs_info,
249 struct btrfs_block_group_cache *cache,
250 struct btrfs_path *path)
252 struct free_space_extent extents[] = {
253 {cache->key.objectid, BITMAP_RANGE},
254 {cache->key.objectid + 2 * BITMAP_RANGE,
255 cache->key.offset - 2 * BITMAP_RANGE},
259 ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
260 cache->key.objectid + BITMAP_RANGE,
263 test_msg("Could not remove free space\n");
267 return check_free_space_extents(trans, fs_info, cache, path,
268 extents, ARRAY_SIZE(extents));
271 static int test_merge_left(struct btrfs_trans_handle *trans,
272 struct btrfs_fs_info *fs_info,
273 struct btrfs_block_group_cache *cache,
274 struct btrfs_path *path)
276 struct free_space_extent extents[] = {
277 {cache->key.objectid, 2 * BITMAP_RANGE},
281 ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
285 test_msg("Could not remove free space\n");
289 ret = __add_to_free_space_tree(trans, fs_info, cache, path,
290 cache->key.objectid, BITMAP_RANGE);
292 test_msg("Could not add free space\n");
296 ret = __add_to_free_space_tree(trans, fs_info, cache, path,
297 cache->key.objectid + BITMAP_RANGE,
300 test_msg("Could not add free space\n");
304 return check_free_space_extents(trans, fs_info, cache, path,
305 extents, ARRAY_SIZE(extents));
308 static int test_merge_right(struct btrfs_trans_handle *trans,
309 struct btrfs_fs_info *fs_info,
310 struct btrfs_block_group_cache *cache,
311 struct btrfs_path *path)
313 struct free_space_extent extents[] = {
314 {cache->key.objectid + BITMAP_RANGE, 2 * BITMAP_RANGE},
318 ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
322 test_msg("Could not remove free space\n");
326 ret = __add_to_free_space_tree(trans, fs_info, cache, path,
327 cache->key.objectid + 2 * BITMAP_RANGE,
330 test_msg("Could not add free space\n");
334 ret = __add_to_free_space_tree(trans, fs_info, cache, path,
335 cache->key.objectid + BITMAP_RANGE,
338 test_msg("Could not add free space\n");
342 return check_free_space_extents(trans, fs_info, cache, path,
343 extents, ARRAY_SIZE(extents));
346 static int test_merge_both(struct btrfs_trans_handle *trans,
347 struct btrfs_fs_info *fs_info,
348 struct btrfs_block_group_cache *cache,
349 struct btrfs_path *path)
351 struct free_space_extent extents[] = {
352 {cache->key.objectid, 3 * BITMAP_RANGE},
356 ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
360 test_msg("Could not remove free space\n");
364 ret = __add_to_free_space_tree(trans, fs_info, cache, path,
365 cache->key.objectid, BITMAP_RANGE);
367 test_msg("Could not add free space\n");
371 ret = __add_to_free_space_tree(trans, fs_info, cache, path,
372 cache->key.objectid + 2 * BITMAP_RANGE,
375 test_msg("Could not add free space\n");
379 ret = __add_to_free_space_tree(trans, fs_info, cache, path,
380 cache->key.objectid + BITMAP_RANGE,
383 test_msg("Could not add free space\n");
387 return check_free_space_extents(trans, fs_info, cache, path,
388 extents, ARRAY_SIZE(extents));
391 static int test_merge_none(struct btrfs_trans_handle *trans,
392 struct btrfs_fs_info *fs_info,
393 struct btrfs_block_group_cache *cache,
394 struct btrfs_path *path)
396 struct free_space_extent extents[] = {
397 {cache->key.objectid, BITMAP_RANGE},
398 {cache->key.objectid + 2 * BITMAP_RANGE, BITMAP_RANGE},
399 {cache->key.objectid + 4 * BITMAP_RANGE, BITMAP_RANGE},
403 ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
407 test_msg("Could not remove free space\n");
411 ret = __add_to_free_space_tree(trans, fs_info, cache, path,
412 cache->key.objectid, BITMAP_RANGE);
414 test_msg("Could not add free space\n");
418 ret = __add_to_free_space_tree(trans, fs_info, cache, path,
419 cache->key.objectid + 4 * BITMAP_RANGE,
422 test_msg("Could not add free space\n");
426 ret = __add_to_free_space_tree(trans, fs_info, cache, path,
427 cache->key.objectid + 2 * BITMAP_RANGE,
430 test_msg("Could not add free space\n");
434 return check_free_space_extents(trans, fs_info, cache, path,
435 extents, ARRAY_SIZE(extents));
438 typedef int (*test_func_t)(struct btrfs_trans_handle *,
439 struct btrfs_fs_info *,
440 struct btrfs_block_group_cache *,
441 struct btrfs_path *);
443 static int run_test(test_func_t test_func, int bitmaps,
444 u32 sectorsize, u32 nodesize)
446 struct btrfs_root *root = NULL;
447 struct btrfs_block_group_cache *cache = NULL;
448 struct btrfs_trans_handle trans;
449 struct btrfs_path *path = NULL;
452 root = btrfs_alloc_dummy_root(sectorsize, nodesize);
454 test_msg("Couldn't allocate dummy root\n");
459 root->fs_info = btrfs_alloc_dummy_fs_info();
460 if (!root->fs_info) {
461 test_msg("Couldn't allocate dummy fs info\n");
466 btrfs_set_super_compat_ro_flags(root->fs_info->super_copy,
467 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE);
468 root->fs_info->free_space_root = root;
469 root->fs_info->tree_root = root;
471 root->node = alloc_test_extent_buffer(root->fs_info,
474 test_msg("Couldn't allocate dummy buffer\n");
478 btrfs_set_header_level(root->node, 0);
479 btrfs_set_header_nritems(root->node, 0);
480 root->alloc_bytenr += 2 * nodesize;
482 cache = btrfs_alloc_dummy_block_group(8 * BITMAP_RANGE, sectorsize);
484 test_msg("Couldn't allocate dummy block group cache\n");
488 cache->bitmap_low_thresh = 0;
489 cache->bitmap_high_thresh = (u32)-1;
490 cache->needs_free_space = 1;
491 cache->fs_info = root->fs_info;
493 btrfs_init_dummy_trans(&trans);
495 path = btrfs_alloc_path();
497 test_msg("Couldn't allocate path\n");
501 ret = add_block_group_free_space(&trans, root->fs_info, cache);
503 test_msg("Could not add block group free space\n");
508 ret = convert_free_space_to_bitmaps(&trans, root->fs_info,
511 test_msg("Could not convert block group to bitmaps\n");
516 ret = test_func(&trans, root->fs_info, cache, path);
520 ret = remove_block_group_free_space(&trans, root->fs_info, cache);
522 test_msg("Could not remove block group free space\n");
526 if (btrfs_header_nritems(root->node) != 0) {
527 test_msg("Free space tree has leftover items\n");
534 btrfs_free_path(path);
535 btrfs_free_dummy_block_group(cache);
536 btrfs_free_dummy_root(root);
540 static int run_test_both_formats(test_func_t test_func,
541 u32 sectorsize, u32 nodesize)
545 ret = run_test(test_func, 0, sectorsize, nodesize);
548 return run_test(test_func, 1, sectorsize, nodesize);
551 int btrfs_test_free_space_tree(u32 sectorsize, u32 nodesize)
553 test_func_t tests[] = {
554 test_empty_block_group,
556 test_remove_beginning,
566 test_msg("Running free space tree tests\n");
567 for (i = 0; i < ARRAY_SIZE(tests); i++) {
568 int ret = run_test_both_formats(tests[i], sectorsize,
571 test_msg("%pf : sectorsize %u failed\n",
572 tests[i], sectorsize);