free-space-tree-tests.c 14.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
3
4
5
/*
 * Copyright (C) 2015 Facebook.  All rights reserved.
 */

6
#include <linux/types.h>
7
8
9
10
11
#include "btrfs-tests.h"
#include "../ctree.h"
#include "../disk-io.h"
#include "../free-space-tree.h"
#include "../transaction.h"
12
#include "../block-group.h"
13
14

struct free_space_extent {
15
16
	u64 start;
	u64 length;
17
18
19
20
};

static int __check_free_space_extents(struct btrfs_trans_handle *trans,
				      struct btrfs_fs_info *fs_info,
21
				      struct btrfs_block_group *cache,
22
				      struct btrfs_path *path,
23
				      const struct free_space_extent * const extents,
24
25
26
27
28
29
30
31
32
33
				      unsigned int num_extents)
{
	struct btrfs_free_space_info *info;
	struct btrfs_key key;
	int prev_bit = 0, bit;
	u64 extent_start = 0, offset, end;
	u32 flags, extent_count;
	unsigned int i;
	int ret;

34
	info = search_free_space_info(trans, cache, path, 0);
35
	if (IS_ERR(info)) {
36
		test_err("could not find free space info");
37
38
39
40
41
42
43
		ret = PTR_ERR(info);
		goto out;
	}
	flags = btrfs_free_space_flags(path->nodes[0], info);
	extent_count = btrfs_free_space_extent_count(path->nodes[0], info);

	if (extent_count != num_extents) {
44
		test_err("extent count is wrong");
45
46
47
48
49
50
		ret = -EINVAL;
		goto out;
	}
	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
		if (path->slots[0] != 0)
			goto invalid;
51
		end = cache->start + cache->length;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
		i = 0;
		while (++path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
			if (key.type != BTRFS_FREE_SPACE_BITMAP_KEY)
				goto invalid;
			offset = key.objectid;
			while (offset < key.objectid + key.offset) {
				bit = free_space_test_bit(cache, path, offset);
				if (prev_bit == 0 && bit == 1) {
					extent_start = offset;
				} else if (prev_bit == 1 && bit == 0) {
					if (i >= num_extents)
						goto invalid;
					if (i >= num_extents ||
					    extent_start != extents[i].start ||
					    offset - extent_start != extents[i].length)
						goto invalid;
					i++;
				}
				prev_bit = bit;
72
				offset += fs_info->sectorsize;
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
			}
		}
		if (prev_bit == 1) {
			if (i >= num_extents ||
			    extent_start != extents[i].start ||
			    end - extent_start != extents[i].length)
				goto invalid;
			i++;
		}
		if (i != num_extents)
			goto invalid;
	} else {
		if (btrfs_header_nritems(path->nodes[0]) != num_extents + 1 ||
		    path->slots[0] != 0)
			goto invalid;
		for (i = 0; i < num_extents; i++) {
			path->slots[0]++;
			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
			if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY ||
			    key.objectid != extents[i].start ||
			    key.offset != extents[i].length)
				goto invalid;
		}
	}

	ret = 0;
out:
	btrfs_release_path(path);
	return ret;
invalid:
103
	test_err("free space tree is invalid");
104
105
106
107
108
109
	ret = -EINVAL;
	goto out;
}

static int check_free_space_extents(struct btrfs_trans_handle *trans,
				    struct btrfs_fs_info *fs_info,
110
				    struct btrfs_block_group *cache,
111
				    struct btrfs_path *path,
112
				    const struct free_space_extent * const extents,
113
114
115
116
117
118
				    unsigned int num_extents)
{
	struct btrfs_free_space_info *info;
	u32 flags;
	int ret;

119
	info = search_free_space_info(trans, cache, path, 0);
120
	if (IS_ERR(info)) {
121
		test_err("could not find free space info");
122
123
124
125
126
127
128
129
130
131
132
133
134
		btrfs_release_path(path);
		return PTR_ERR(info);
	}
	flags = btrfs_free_space_flags(path->nodes[0], info);
	btrfs_release_path(path);

	ret = __check_free_space_extents(trans, fs_info, cache, path, extents,
					 num_extents);
	if (ret)
		return ret;

	/* Flip it to the other format and check that for good measure. */
	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
135
		ret = convert_free_space_to_extents(trans, cache, path);
136
		if (ret) {
137
			test_err("could not convert to extents");
138
139
140
			return ret;
		}
	} else {
141
		ret = convert_free_space_to_bitmaps(trans, cache, path);
142
		if (ret) {
143
			test_err("could not convert to bitmaps");
144
145
146
147
148
149
150
151
152
			return ret;
		}
	}
	return __check_free_space_extents(trans, fs_info, cache, path, extents,
					  num_extents);
}

static int test_empty_block_group(struct btrfs_trans_handle *trans,
				  struct btrfs_fs_info *fs_info,
153
				  struct btrfs_block_group *cache,
154
155
				  struct btrfs_path *path,
				  u32 alignment)
156
{
157
	const struct free_space_extent extents[] = {
158
		{cache->start, cache->length},
159
160
161
162
163
164
165
166
	};

	return check_free_space_extents(trans, fs_info, cache, path,
					extents, ARRAY_SIZE(extents));
}

static int test_remove_all(struct btrfs_trans_handle *trans,
			   struct btrfs_fs_info *fs_info,
167
			   struct btrfs_block_group *cache,
168
169
			   struct btrfs_path *path,
			   u32 alignment)
170
{
171
	const struct free_space_extent extents[] = {};
172
173
	int ret;

174
	ret = __remove_from_free_space_tree(trans, cache, path,
175
176
					    cache->start,
					    cache->length);
177
	if (ret) {
178
		test_err("could not remove free space");
179
180
181
182
183
184
185
186
187
		return ret;
	}

	return check_free_space_extents(trans, fs_info, cache, path,
					extents, ARRAY_SIZE(extents));
}

static int test_remove_beginning(struct btrfs_trans_handle *trans,
				 struct btrfs_fs_info *fs_info,
188
				 struct btrfs_block_group *cache,
189
190
				 struct btrfs_path *path,
				 u32 alignment)
191
{
192
	const struct free_space_extent extents[] = {
193
		{cache->start + alignment, cache->length - alignment},
194
195
196
	};
	int ret;

197
	ret = __remove_from_free_space_tree(trans, cache, path,
198
					    cache->start, alignment);
199
	if (ret) {
200
		test_err("could not remove free space");
201
202
203
204
205
206
207
208
209
210
		return ret;
	}

	return check_free_space_extents(trans, fs_info, cache, path,
					extents, ARRAY_SIZE(extents));

}

static int test_remove_end(struct btrfs_trans_handle *trans,
			   struct btrfs_fs_info *fs_info,
211
			   struct btrfs_block_group *cache,
212
213
			   struct btrfs_path *path,
			   u32 alignment)
214
{
215
	const struct free_space_extent extents[] = {
216
		{cache->start, cache->length - alignment},
217
218
219
	};
	int ret;

220
	ret = __remove_from_free_space_tree(trans, cache, path,
221
222
				    cache->start + cache->length - alignment,
				    alignment);
223
	if (ret) {
224
		test_err("could not remove free space");
225
226
227
228
229
230
231
232
233
		return ret;
	}

	return check_free_space_extents(trans, fs_info, cache, path,
					extents, ARRAY_SIZE(extents));
}

static int test_remove_middle(struct btrfs_trans_handle *trans,
			      struct btrfs_fs_info *fs_info,
234
			      struct btrfs_block_group *cache,
235
236
			      struct btrfs_path *path,
			      u32 alignment)
237
{
238
	const struct free_space_extent extents[] = {
239
240
		{cache->start, alignment},
		{cache->start + 2 * alignment, cache->length - 2 * alignment},
241
242
243
	};
	int ret;

244
	ret = __remove_from_free_space_tree(trans, cache, path,
245
					    cache->start + alignment,
246
					    alignment);
247
	if (ret) {
248
		test_err("could not remove free space");
249
250
251
252
253
254
255
256
257
		return ret;
	}

	return check_free_space_extents(trans, fs_info, cache, path,
					extents, ARRAY_SIZE(extents));
}

static int test_merge_left(struct btrfs_trans_handle *trans,
			   struct btrfs_fs_info *fs_info,
258
			   struct btrfs_block_group *cache,
259
260
			   struct btrfs_path *path,
			   u32 alignment)
261
{
262
	const struct free_space_extent extents[] = {
263
		{cache->start, 2 * alignment},
264
265
266
	};
	int ret;

267
	ret = __remove_from_free_space_tree(trans, cache, path,
268
					    cache->start, cache->length);
269
	if (ret) {
270
		test_err("could not remove free space");
271
272
273
		return ret;
	}

274
	ret = __add_to_free_space_tree(trans, cache, path, cache->start,
275
				       alignment);
276
	if (ret) {
277
		test_err("could not add free space");
278
279
280
		return ret;
	}

281
	ret = __add_to_free_space_tree(trans, cache, path,
282
				       cache->start + alignment,
283
				       alignment);
284
	if (ret) {
285
		test_err("could not add free space");
286
287
288
289
290
291
292
293
294
		return ret;
	}

	return check_free_space_extents(trans, fs_info, cache, path,
					extents, ARRAY_SIZE(extents));
}

static int test_merge_right(struct btrfs_trans_handle *trans,
			   struct btrfs_fs_info *fs_info,
295
			   struct btrfs_block_group *cache,
296
297
			   struct btrfs_path *path,
			   u32 alignment)
298
{
299
	const struct free_space_extent extents[] = {
300
		{cache->start + alignment, 2 * alignment},
301
302
303
	};
	int ret;

304
	ret = __remove_from_free_space_tree(trans, cache, path,
305
					    cache->start, cache->length);
306
	if (ret) {
307
		test_err("could not remove free space");
308
309
310
		return ret;
	}

311
	ret = __add_to_free_space_tree(trans, cache, path,
312
				       cache->start + 2 * alignment,
313
				       alignment);
314
	if (ret) {
315
		test_err("could not add free space");
316
317
318
		return ret;
	}

319
	ret = __add_to_free_space_tree(trans, cache, path,
320
				       cache->start + alignment,
321
				       alignment);
322
	if (ret) {
323
		test_err("could not add free space");
324
325
326
327
328
329
330
331
332
		return ret;
	}

	return check_free_space_extents(trans, fs_info, cache, path,
					extents, ARRAY_SIZE(extents));
}

static int test_merge_both(struct btrfs_trans_handle *trans,
			   struct btrfs_fs_info *fs_info,
333
			   struct btrfs_block_group *cache,
334
335
			   struct btrfs_path *path,
			   u32 alignment)
336
{
337
	const struct free_space_extent extents[] = {
338
		{cache->start, 3 * alignment},
339
340
341
	};
	int ret;

342
	ret = __remove_from_free_space_tree(trans, cache, path,
343
					    cache->start, cache->length);
344
	if (ret) {
345
		test_err("could not remove free space");
346
347
348
		return ret;
	}

349
	ret = __add_to_free_space_tree(trans, cache, path, cache->start,
350
				       alignment);
351
	if (ret) {
352
		test_err("could not add free space");
353
354
355
		return ret;
	}

356
	ret = __add_to_free_space_tree(trans, cache, path,
357
				       cache->start + 2 * alignment, alignment);
358
	if (ret) {
359
		test_err("could not add free space");
360
361
362
		return ret;
	}

363
	ret = __add_to_free_space_tree(trans, cache, path,
364
				       cache->start + alignment, alignment);
365
	if (ret) {
366
		test_err("could not add free space");
367
368
369
370
371
372
373
374
375
		return ret;
	}

	return check_free_space_extents(trans, fs_info, cache, path,
					extents, ARRAY_SIZE(extents));
}

static int test_merge_none(struct btrfs_trans_handle *trans,
			   struct btrfs_fs_info *fs_info,
376
			   struct btrfs_block_group *cache,
377
378
			   struct btrfs_path *path,
			   u32 alignment)
379
{
380
	const struct free_space_extent extents[] = {
381
382
383
		{cache->start, alignment},
		{cache->start + 2 * alignment, alignment},
		{cache->start + 4 * alignment, alignment},
384
385
386
	};
	int ret;

387
	ret = __remove_from_free_space_tree(trans, cache, path,
388
					    cache->start, cache->length);
389
	if (ret) {
390
		test_err("could not remove free space");
391
392
393
		return ret;
	}

394
	ret = __add_to_free_space_tree(trans, cache, path, cache->start,
395
				       alignment);
396
	if (ret) {
397
		test_err("could not add free space");
398
399
400
		return ret;
	}

401
	ret = __add_to_free_space_tree(trans, cache, path,
402
				       cache->start + 4 * alignment, alignment);
403
	if (ret) {
404
		test_err("could not add free space");
405
406
407
		return ret;
	}

408
	ret = __add_to_free_space_tree(trans, cache, path,
409
				       cache->start + 2 * alignment, alignment);
410
	if (ret) {
411
		test_err("could not add free space");
412
413
414
415
416
417
418
419
420
		return ret;
	}

	return check_free_space_extents(trans, fs_info, cache, path,
					extents, ARRAY_SIZE(extents));
}

typedef int (*test_func_t)(struct btrfs_trans_handle *,
			   struct btrfs_fs_info *,
421
			   struct btrfs_block_group *,
422
423
			   struct btrfs_path *,
			   u32 alignment);
424

425
426
static int run_test(test_func_t test_func, int bitmaps, u32 sectorsize,
		    u32 nodesize, u32 alignment)
427
{
428
	struct btrfs_fs_info *fs_info;
429
	struct btrfs_root *root = NULL;
430
	struct btrfs_block_group *cache = NULL;
431
432
433
434
	struct btrfs_trans_handle trans;
	struct btrfs_path *path = NULL;
	int ret;

435
	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
436
	if (!fs_info) {
437
		test_std_err(TEST_ALLOC_FS_INFO);
438
		ret = -ENOMEM;
439
440
441
		goto out;
	}

442
	root = btrfs_alloc_dummy_root(fs_info);
443
	if (IS_ERR(root)) {
444
		test_std_err(TEST_ALLOC_ROOT);
445
		ret = PTR_ERR(root);
446
447
448
449
450
451
452
453
		goto out;
	}

	btrfs_set_super_compat_ro_flags(root->fs_info->super_copy,
					BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE);
	root->fs_info->free_space_root = root;
	root->fs_info->tree_root = root;

454
	root->node = alloc_test_extent_buffer(root->fs_info, nodesize);
455
	if (IS_ERR(root->node)) {
456
		test_std_err(TEST_ALLOC_EXTENT_BUFFER);
457
		ret = PTR_ERR(root->node);
458
459
460
461
		goto out;
	}
	btrfs_set_header_level(root->node, 0);
	btrfs_set_header_nritems(root->node, 0);
462
	root->alloc_bytenr += 2 * nodesize;
463

464
	cache = btrfs_alloc_dummy_block_group(fs_info, 8 * alignment);
465
	if (!cache) {
466
		test_std_err(TEST_ALLOC_BLOCK_GROUP);
467
468
469
470
471
472
		ret = -ENOMEM;
		goto out;
	}
	cache->bitmap_low_thresh = 0;
	cache->bitmap_high_thresh = (u32)-1;
	cache->needs_free_space = 1;
473
	cache->fs_info = root->fs_info;
474

475
	btrfs_init_dummy_trans(&trans, root->fs_info);
476
477
478

	path = btrfs_alloc_path();
	if (!path) {
479
		test_std_err(TEST_ALLOC_ROOT);
480
481
		ret = -ENOMEM;
		goto out;
482
483
	}

484
	ret = add_block_group_free_space(&trans, cache);
485
	if (ret) {
486
		test_err("could not add block group free space");
487
488
489
490
		goto out;
	}

	if (bitmaps) {
491
		ret = convert_free_space_to_bitmaps(&trans, cache, path);
492
		if (ret) {
493
			test_err("could not convert block group to bitmaps");
494
495
496
497
			goto out;
		}
	}

498
	ret = test_func(&trans, root->fs_info, cache, path, alignment);
499
500
501
	if (ret)
		goto out;

502
	ret = remove_block_group_free_space(&trans, cache);
503
	if (ret) {
504
		test_err("could not remove block group free space");
505
506
507
508
		goto out;
	}

	if (btrfs_header_nritems(root->node) != 0) {
509
		test_err("free space tree has leftover items");
510
511
512
513
514
515
516
517
518
		ret = -EINVAL;
		goto out;
	}

	ret = 0;
out:
	btrfs_free_path(path);
	btrfs_free_dummy_block_group(cache);
	btrfs_free_dummy_root(root);
519
	btrfs_free_dummy_fs_info(fs_info);
520
521
522
	return ret;
}

523
524
static int run_test_both_formats(test_func_t test_func, u32 sectorsize,
				 u32 nodesize, u32 alignment)
525
{
526
	int test_ret = 0;
527
528
	int ret;

529
530
	ret = run_test(test_func, 0, sectorsize, nodesize, alignment);
	if (ret) {
531
		test_err(
532
	"%ps failed with extents, sectorsize=%u, nodesize=%u, alignment=%u",
533
534
535
536
537
538
			 test_func, sectorsize, nodesize, alignment);
		test_ret = ret;
	}

	ret = run_test(test_func, 1, sectorsize, nodesize, alignment);
	if (ret) {
539
		test_err(
540
	"%ps failed with bitmaps, sectorsize=%u, nodesize=%u, alignment=%u",
541
542
543
544
545
			 test_func, sectorsize, nodesize, alignment);
		test_ret = ret;
	}

	return test_ret;
546
547
}

548
int btrfs_test_free_space_tree(u32 sectorsize, u32 nodesize)
549
550
551
552
553
554
555
556
557
558
559
560
{
	test_func_t tests[] = {
		test_empty_block_group,
		test_remove_all,
		test_remove_beginning,
		test_remove_end,
		test_remove_middle,
		test_merge_left,
		test_merge_right,
		test_merge_both,
		test_merge_none,
	};
561
562
	u32 bitmap_alignment;
	int test_ret = 0;
563
564
	int i;

565
566
567
568
569
570
	/*
	 * Align some operations to a page to flush out bugs in the extent
	 * buffer bitmap handling of highmem.
	 */
	bitmap_alignment = BTRFS_FREE_SPACE_BITMAP_BITS * PAGE_SIZE;

571
	test_msg("running free space tree tests");
572
	for (i = 0; i < ARRAY_SIZE(tests); i++) {
573
574
575
576
577
578
579
580
581
582
583
		int ret;

		ret = run_test_both_formats(tests[i], sectorsize, nodesize,
					    sectorsize);
		if (ret)
			test_ret = ret;

		ret = run_test_both_formats(tests[i], sectorsize, nodesize,
					    bitmap_alignment);
		if (ret)
			test_ret = ret;
584
585
	}

586
	return test_ret;
587
}