qgroup-tests.c 12.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
3
4
5
/*
 * Copyright (C) 2013 Facebook.  All rights reserved.
 */

6
#include <linux/types.h>
7
8
9
10
11
#include "btrfs-tests.h"
#include "../ctree.h"
#include "../transaction.h"
#include "../disk-io.h"
#include "../qgroup.h"
12
#include "../backref.h"
13
14
15
16
17
18
19
20
21
22
23
24
25
26

static int insert_normal_tree_ref(struct btrfs_root *root, u64 bytenr,
				  u64 num_bytes, u64 parent, u64 root_objectid)
{
	struct btrfs_trans_handle trans;
	struct btrfs_extent_item *item;
	struct btrfs_extent_inline_ref *iref;
	struct btrfs_tree_block_info *block_info;
	struct btrfs_path *path;
	struct extent_buffer *leaf;
	struct btrfs_key ins;
	u32 size = sizeof(*item) + sizeof(*iref) + sizeof(*block_info);
	int ret;

27
	btrfs_init_dummy_trans(&trans, NULL);
28
29
30
31
32
33
34

	ins.objectid = bytenr;
	ins.type = BTRFS_EXTENT_ITEM_KEY;
	ins.offset = num_bytes;

	path = btrfs_alloc_path();
	if (!path) {
35
		test_std_err(TEST_ALLOC_ROOT);
36
37
38
39
40
41
		return -ENOMEM;
	}

	path->leave_spinning = 1;
	ret = btrfs_insert_empty_item(&trans, root, path, &ins, size);
	if (ret) {
42
		test_err("couldn't insert ref %d", ret);
43
44
45
46
47
48
49
50
51
52
		btrfs_free_path(path);
		return ret;
	}

	leaf = path->nodes[0];
	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
	btrfs_set_extent_refs(leaf, item, 1);
	btrfs_set_extent_generation(leaf, item, 1);
	btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_TREE_BLOCK);
	block_info = (struct btrfs_tree_block_info *)(item + 1);
53
	btrfs_set_tree_block_level(leaf, block_info, 0);
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
	iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
	if (parent > 0) {
		btrfs_set_extent_inline_ref_type(leaf, iref,
						 BTRFS_SHARED_BLOCK_REF_KEY);
		btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
	} else {
		btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
		btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
	}
	btrfs_free_path(path);
	return 0;
}

static int add_tree_ref(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
			u64 parent, u64 root_objectid)
{
	struct btrfs_trans_handle trans;
	struct btrfs_extent_item *item;
	struct btrfs_path *path;
	struct btrfs_key key;
	u64 refs;
	int ret;

77
	btrfs_init_dummy_trans(&trans, NULL);
78
79
80
81
82
83
84

	key.objectid = bytenr;
	key.type = BTRFS_EXTENT_ITEM_KEY;
	key.offset = num_bytes;

	path = btrfs_alloc_path();
	if (!path) {
85
		test_std_err(TEST_ALLOC_ROOT);
86
87
88
89
90
91
		return -ENOMEM;
	}

	path->leave_spinning = 1;
	ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
	if (ret) {
92
		test_err("couldn't find extent ref");
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
		btrfs_free_path(path);
		return ret;
	}

	item = btrfs_item_ptr(path->nodes[0], path->slots[0],
			      struct btrfs_extent_item);
	refs = btrfs_extent_refs(path->nodes[0], item);
	btrfs_set_extent_refs(path->nodes[0], item, refs + 1);
	btrfs_release_path(path);

	key.objectid = bytenr;
	if (parent) {
		key.type = BTRFS_SHARED_BLOCK_REF_KEY;
		key.offset = parent;
	} else {
		key.type = BTRFS_TREE_BLOCK_REF_KEY;
		key.offset = root_objectid;
	}

	ret = btrfs_insert_empty_item(&trans, root, path, &key, 0);
	if (ret)
114
		test_err("failed to insert backref");
115
116
117
118
119
120
121
122
123
124
125
126
	btrfs_free_path(path);
	return ret;
}

static int remove_extent_item(struct btrfs_root *root, u64 bytenr,
			      u64 num_bytes)
{
	struct btrfs_trans_handle trans;
	struct btrfs_key key;
	struct btrfs_path *path;
	int ret;

127
	btrfs_init_dummy_trans(&trans, NULL);
128
129
130
131
132
133
134

	key.objectid = bytenr;
	key.type = BTRFS_EXTENT_ITEM_KEY;
	key.offset = num_bytes;

	path = btrfs_alloc_path();
	if (!path) {
135
		test_std_err(TEST_ALLOC_ROOT);
136
137
138
139
140
141
		return -ENOMEM;
	}
	path->leave_spinning = 1;

	ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
	if (ret) {
142
		test_err("didn't find our key %d", ret);
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
		btrfs_free_path(path);
		return ret;
	}
	btrfs_del_item(&trans, root, path);
	btrfs_free_path(path);
	return 0;
}

static int remove_extent_ref(struct btrfs_root *root, u64 bytenr,
			     u64 num_bytes, u64 parent, u64 root_objectid)
{
	struct btrfs_trans_handle trans;
	struct btrfs_extent_item *item;
	struct btrfs_path *path;
	struct btrfs_key key;
	u64 refs;
	int ret;

161
	btrfs_init_dummy_trans(&trans, NULL);
162
163
164
165
166
167
168

	key.objectid = bytenr;
	key.type = BTRFS_EXTENT_ITEM_KEY;
	key.offset = num_bytes;

	path = btrfs_alloc_path();
	if (!path) {
169
		test_std_err(TEST_ALLOC_ROOT);
170
171
172
173
174
175
		return -ENOMEM;
	}

	path->leave_spinning = 1;
	ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
	if (ret) {
176
		test_err("couldn't find extent ref");
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
		btrfs_free_path(path);
		return ret;
	}

	item = btrfs_item_ptr(path->nodes[0], path->slots[0],
			      struct btrfs_extent_item);
	refs = btrfs_extent_refs(path->nodes[0], item);
	btrfs_set_extent_refs(path->nodes[0], item, refs - 1);
	btrfs_release_path(path);

	key.objectid = bytenr;
	if (parent) {
		key.type = BTRFS_SHARED_BLOCK_REF_KEY;
		key.offset = parent;
	} else {
		key.type = BTRFS_TREE_BLOCK_REF_KEY;
		key.offset = root_objectid;
	}

	ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
	if (ret) {
198
		test_err("couldn't find backref %d", ret);
199
200
201
202
203
204
205
206
		btrfs_free_path(path);
		return ret;
	}
	btrfs_del_item(&trans, root, path);
	btrfs_free_path(path);
	return ret;
}

207
208
static int test_no_shared_qgroup(struct btrfs_root *root,
		u32 sectorsize, u32 nodesize)
209
210
211
{
	struct btrfs_trans_handle trans;
	struct btrfs_fs_info *fs_info = root->fs_info;
212
213
	struct ulist *old_roots = NULL;
	struct ulist *new_roots = NULL;
214
215
	int ret;

216
	btrfs_init_dummy_trans(&trans, fs_info);
217

218
	test_msg("running qgroup add/remove tests");
219
	ret = btrfs_create_qgroup(&trans, BTRFS_FS_TREE_OBJECTID);
220
	if (ret) {
221
		test_err("couldn't create a qgroup %d", ret);
222
223
224
		return ret;
	}

225
	/*
226
	 * Since the test trans doesn't have the complicated delayed refs,
227
228
229
	 * we can only call btrfs_qgroup_account_extent() directly to test
	 * quota.
	 */
230
231
	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
			false);
232
	if (ret) {
233
		ulist_free(old_roots);
234
		test_err("couldn't find old roots: %d", ret);
235
236
237
		return ret;
	}

238
239
	ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
				BTRFS_FS_TREE_OBJECTID);
240
241
242
	if (ret)
		return ret;

243
244
	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
			false);
245
	if (ret) {
246
247
		ulist_free(old_roots);
		ulist_free(new_roots);
248
		test_err("couldn't find old roots: %d", ret);
249
250
251
		return ret;
	}

252
253
	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
					  new_roots);
254
	if (ret) {
255
		test_err("couldn't account space for a qgroup %d", ret);
256
257
258
		return ret;
	}

259
260
	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
				nodesize, nodesize)) {
261
		test_err("qgroup counts didn't match expected values");
262
263
		return -EINVAL;
	}
264
265
266
	old_roots = NULL;
	new_roots = NULL;

267
268
	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
			false);
269
270
	if (ret) {
		ulist_free(old_roots);
271
		test_err("couldn't find old roots: %d", ret);
272
273
		return ret;
	}
274

275
	ret = remove_extent_item(root, nodesize, nodesize);
276
277
278
	if (ret)
		return -EINVAL;

279
280
	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
			false);
281
	if (ret) {
282
283
		ulist_free(old_roots);
		ulist_free(new_roots);
284
		test_err("couldn't find old roots: %d", ret);
285
		return ret;
286
287
	}

288
289
	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
					  new_roots);
290
	if (ret) {
291
		test_err("couldn't account space for a qgroup %d", ret);
292
293
294
		return -EINVAL;
	}

295
	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
296
		test_err("qgroup counts didn't match expected values");
297
298
299
300
301
302
303
304
305
306
307
		return -EINVAL;
	}

	return 0;
}

/*
 * Add a ref for two different roots to make sure the shared value comes out
 * right, also remove one of the roots and make sure the exclusive count is
 * adjusted properly.
 */
308
309
static int test_multiple_refs(struct btrfs_root *root,
		u32 sectorsize, u32 nodesize)
310
311
312
{
	struct btrfs_trans_handle trans;
	struct btrfs_fs_info *fs_info = root->fs_info;
313
314
	struct ulist *old_roots = NULL;
	struct ulist *new_roots = NULL;
315
316
	int ret;

317
	btrfs_init_dummy_trans(&trans, fs_info);
318

319
	test_msg("running qgroup multiple refs test");
320

321
322
323
324
	/*
	 * We have BTRFS_FS_TREE_OBJECTID created already from the
	 * previous test.
	 */
325
	ret = btrfs_create_qgroup(&trans, BTRFS_FIRST_FREE_OBJECTID);
326
	if (ret) {
327
		test_err("couldn't create a qgroup %d", ret);
328
329
330
		return ret;
	}

331
332
	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
			false);
333
334
	if (ret) {
		ulist_free(old_roots);
335
		test_err("couldn't find old roots: %d", ret);
336
337
338
		return ret;
	}

339
340
	ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
				BTRFS_FS_TREE_OBJECTID);
341
342
343
	if (ret)
		return ret;

344
345
	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
			false);
346
	if (ret) {
347
348
		ulist_free(old_roots);
		ulist_free(new_roots);
349
		test_err("couldn't find old roots: %d", ret);
350
351
352
		return ret;
	}

353
354
	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
					  new_roots);
355
	if (ret) {
356
		test_err("couldn't account space for a qgroup %d", ret);
357
358
359
		return ret;
	}

360
	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
361
				       nodesize, nodesize)) {
362
		test_err("qgroup counts didn't match expected values");
363
364
365
		return -EINVAL;
	}

366
367
	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
			false);
368
369
	if (ret) {
		ulist_free(old_roots);
370
		test_err("couldn't find old roots: %d", ret);
371
372
373
		return ret;
	}

374
375
	ret = add_tree_ref(root, nodesize, nodesize, 0,
			BTRFS_FIRST_FREE_OBJECTID);
376
377
378
	if (ret)
		return ret;

379
380
	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
			false);
381
	if (ret) {
382
383
		ulist_free(old_roots);
		ulist_free(new_roots);
384
		test_err("couldn't find old roots: %d", ret);
385
386
387
		return ret;
	}

388
389
	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
					  new_roots);
390
	if (ret) {
391
		test_err("couldn't account space for a qgroup %d", ret);
392
393
394
		return ret;
	}

395
396
	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
					nodesize, 0)) {
397
		test_err("qgroup counts didn't match expected values");
398
399
400
		return -EINVAL;
	}

401
402
	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
					nodesize, 0)) {
403
		test_err("qgroup counts didn't match expected values");
404
405
406
		return -EINVAL;
	}

407
408
	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
			false);
409
410
	if (ret) {
		ulist_free(old_roots);
411
		test_err("couldn't find old roots: %d", ret);
412
413
414
		return ret;
	}

415
416
	ret = remove_extent_ref(root, nodesize, nodesize, 0,
				BTRFS_FIRST_FREE_OBJECTID);
417
418
419
	if (ret)
		return ret;

420
421
	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
			false);
422
	if (ret) {
423
424
		ulist_free(old_roots);
		ulist_free(new_roots);
425
		test_err("couldn't find old roots: %d", ret);
426
427
428
		return ret;
	}

429
430
	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
					  new_roots);
431
	if (ret) {
432
		test_err("couldn't account space for a qgroup %d", ret);
433
434
435
		return ret;
	}

436
437
	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
					0, 0)) {
438
		test_err("qgroup counts didn't match expected values");
439
440
441
		return -EINVAL;
	}

442
443
	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
					nodesize, nodesize)) {
444
		test_err("qgroup counts didn't match expected values");
445
446
447
448
449
450
		return -EINVAL;
	}

	return 0;
}

451
int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
452
{
453
	struct btrfs_fs_info *fs_info = NULL;
454
455
456
457
	struct btrfs_root *root;
	struct btrfs_root *tmp_root;
	int ret = 0;

458
	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
459
	if (!fs_info) {
460
		test_std_err(TEST_ALLOC_FS_INFO);
461
		return -ENOMEM;
462
463
	}

464
	root = btrfs_alloc_dummy_root(fs_info);
465
	if (IS_ERR(root)) {
466
		test_std_err(TEST_ALLOC_ROOT);
467
		ret = PTR_ERR(root);
468
469
		goto out;
	}
470

471
472
473
474
475
476
477
478
479
	/* We are using this root as our extent root */
	root->fs_info->extent_root = root;

	/*
	 * Some of the paths we test assume we have a filled out fs_info, so we
	 * just need to add the root in there so we don't panic.
	 */
	root->fs_info->tree_root = root;
	root->fs_info->quota_root = root;
480
	set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
481
482
483
484
485

	/*
	 * Can't use bytenr 0, some things freak out
	 * *cough*backref walking code*cough*
	 */
486
	root->node = alloc_test_extent_buffer(root->fs_info, nodesize);
487
	if (IS_ERR(root->node)) {
488
		test_err("couldn't allocate dummy buffer");
489
		ret = PTR_ERR(root->node);
490
491
		goto out;
	}
492
493
	btrfs_set_header_level(root->node, 0);
	btrfs_set_header_nritems(root->node, 0);
494
	root->alloc_bytenr += 2 * nodesize;
495

496
	tmp_root = btrfs_alloc_dummy_root(fs_info);
497
	if (IS_ERR(tmp_root)) {
498
		test_std_err(TEST_ALLOC_ROOT);
499
500
501
502
		ret = PTR_ERR(tmp_root);
		goto out;
	}

503
	tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID;
504
505
506
	root->fs_info->fs_root = tmp_root;
	ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
	if (ret) {
507
		test_err("couldn't insert fs root %d", ret);
508
509
510
		goto out;
	}

511
	tmp_root = btrfs_alloc_dummy_root(fs_info);
512
	if (IS_ERR(tmp_root)) {
513
		test_std_err(TEST_ALLOC_ROOT);
514
515
516
517
		ret = PTR_ERR(tmp_root);
		goto out;
	}

518
	tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID;
519
520
	ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
	if (ret) {
521
		test_err("couldn't insert fs root %d", ret);
522
523
524
		goto out;
	}

525
	test_msg("running qgroup tests");
526
	ret = test_no_shared_qgroup(root, sectorsize, nodesize);
527
528
	if (ret)
		goto out;
529
	ret = test_multiple_refs(root, sectorsize, nodesize);
530
531
out:
	btrfs_free_dummy_root(root);
532
	btrfs_free_dummy_fs_info(fs_info);
533
534
	return ret;
}