xref: /openbmc/linux/fs/btrfs/tests/qgroup-tests.c (revision 07e81dc9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2013 Facebook.  All rights reserved.
4  */
5 
6 #include <linux/types.h>
7 #include "btrfs-tests.h"
8 #include "../ctree.h"
9 #include "../transaction.h"
10 #include "../disk-io.h"
11 #include "../qgroup.h"
12 #include "../backref.h"
13 #include "../fs.h"
14 #include "../accessors.h"
15 
16 static int insert_normal_tree_ref(struct btrfs_root *root, u64 bytenr,
17 				  u64 num_bytes, u64 parent, u64 root_objectid)
18 {
19 	struct btrfs_trans_handle trans;
20 	struct btrfs_extent_item *item;
21 	struct btrfs_extent_inline_ref *iref;
22 	struct btrfs_tree_block_info *block_info;
23 	struct btrfs_path *path;
24 	struct extent_buffer *leaf;
25 	struct btrfs_key ins;
26 	u32 size = sizeof(*item) + sizeof(*iref) + sizeof(*block_info);
27 	int ret;
28 
29 	btrfs_init_dummy_trans(&trans, NULL);
30 
31 	ins.objectid = bytenr;
32 	ins.type = BTRFS_EXTENT_ITEM_KEY;
33 	ins.offset = num_bytes;
34 
35 	path = btrfs_alloc_path();
36 	if (!path) {
37 		test_std_err(TEST_ALLOC_ROOT);
38 		return -ENOMEM;
39 	}
40 
41 	ret = btrfs_insert_empty_item(&trans, root, path, &ins, size);
42 	if (ret) {
43 		test_err("couldn't insert ref %d", ret);
44 		btrfs_free_path(path);
45 		return ret;
46 	}
47 
48 	leaf = path->nodes[0];
49 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
50 	btrfs_set_extent_refs(leaf, item, 1);
51 	btrfs_set_extent_generation(leaf, item, 1);
52 	btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_TREE_BLOCK);
53 	block_info = (struct btrfs_tree_block_info *)(item + 1);
54 	btrfs_set_tree_block_level(leaf, block_info, 0);
55 	iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
56 	if (parent > 0) {
57 		btrfs_set_extent_inline_ref_type(leaf, iref,
58 						 BTRFS_SHARED_BLOCK_REF_KEY);
59 		btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
60 	} else {
61 		btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
62 		btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
63 	}
64 	btrfs_free_path(path);
65 	return 0;
66 }
67 
68 static int add_tree_ref(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
69 			u64 parent, u64 root_objectid)
70 {
71 	struct btrfs_trans_handle trans;
72 	struct btrfs_extent_item *item;
73 	struct btrfs_path *path;
74 	struct btrfs_key key;
75 	u64 refs;
76 	int ret;
77 
78 	btrfs_init_dummy_trans(&trans, NULL);
79 
80 	key.objectid = bytenr;
81 	key.type = BTRFS_EXTENT_ITEM_KEY;
82 	key.offset = num_bytes;
83 
84 	path = btrfs_alloc_path();
85 	if (!path) {
86 		test_std_err(TEST_ALLOC_ROOT);
87 		return -ENOMEM;
88 	}
89 
90 	ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
91 	if (ret) {
92 		test_err("couldn't find extent ref");
93 		btrfs_free_path(path);
94 		return ret;
95 	}
96 
97 	item = btrfs_item_ptr(path->nodes[0], path->slots[0],
98 			      struct btrfs_extent_item);
99 	refs = btrfs_extent_refs(path->nodes[0], item);
100 	btrfs_set_extent_refs(path->nodes[0], item, refs + 1);
101 	btrfs_release_path(path);
102 
103 	key.objectid = bytenr;
104 	if (parent) {
105 		key.type = BTRFS_SHARED_BLOCK_REF_KEY;
106 		key.offset = parent;
107 	} else {
108 		key.type = BTRFS_TREE_BLOCK_REF_KEY;
109 		key.offset = root_objectid;
110 	}
111 
112 	ret = btrfs_insert_empty_item(&trans, root, path, &key, 0);
113 	if (ret)
114 		test_err("failed to insert backref");
115 	btrfs_free_path(path);
116 	return ret;
117 }
118 
119 static int remove_extent_item(struct btrfs_root *root, u64 bytenr,
120 			      u64 num_bytes)
121 {
122 	struct btrfs_trans_handle trans;
123 	struct btrfs_key key;
124 	struct btrfs_path *path;
125 	int ret;
126 
127 	btrfs_init_dummy_trans(&trans, NULL);
128 
129 	key.objectid = bytenr;
130 	key.type = BTRFS_EXTENT_ITEM_KEY;
131 	key.offset = num_bytes;
132 
133 	path = btrfs_alloc_path();
134 	if (!path) {
135 		test_std_err(TEST_ALLOC_ROOT);
136 		return -ENOMEM;
137 	}
138 
139 	ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
140 	if (ret) {
141 		test_err("didn't find our key %d", ret);
142 		btrfs_free_path(path);
143 		return ret;
144 	}
145 	btrfs_del_item(&trans, root, path);
146 	btrfs_free_path(path);
147 	return 0;
148 }
149 
150 static int remove_extent_ref(struct btrfs_root *root, u64 bytenr,
151 			     u64 num_bytes, u64 parent, u64 root_objectid)
152 {
153 	struct btrfs_trans_handle trans;
154 	struct btrfs_extent_item *item;
155 	struct btrfs_path *path;
156 	struct btrfs_key key;
157 	u64 refs;
158 	int ret;
159 
160 	btrfs_init_dummy_trans(&trans, NULL);
161 
162 	key.objectid = bytenr;
163 	key.type = BTRFS_EXTENT_ITEM_KEY;
164 	key.offset = num_bytes;
165 
166 	path = btrfs_alloc_path();
167 	if (!path) {
168 		test_std_err(TEST_ALLOC_ROOT);
169 		return -ENOMEM;
170 	}
171 
172 	ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
173 	if (ret) {
174 		test_err("couldn't find extent ref");
175 		btrfs_free_path(path);
176 		return ret;
177 	}
178 
179 	item = btrfs_item_ptr(path->nodes[0], path->slots[0],
180 			      struct btrfs_extent_item);
181 	refs = btrfs_extent_refs(path->nodes[0], item);
182 	btrfs_set_extent_refs(path->nodes[0], item, refs - 1);
183 	btrfs_release_path(path);
184 
185 	key.objectid = bytenr;
186 	if (parent) {
187 		key.type = BTRFS_SHARED_BLOCK_REF_KEY;
188 		key.offset = parent;
189 	} else {
190 		key.type = BTRFS_TREE_BLOCK_REF_KEY;
191 		key.offset = root_objectid;
192 	}
193 
194 	ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
195 	if (ret) {
196 		test_err("couldn't find backref %d", ret);
197 		btrfs_free_path(path);
198 		return ret;
199 	}
200 	btrfs_del_item(&trans, root, path);
201 	btrfs_free_path(path);
202 	return ret;
203 }
204 
205 static int test_no_shared_qgroup(struct btrfs_root *root,
206 		u32 sectorsize, u32 nodesize)
207 {
208 	struct btrfs_trans_handle trans;
209 	struct btrfs_fs_info *fs_info = root->fs_info;
210 	struct ulist *old_roots = NULL;
211 	struct ulist *new_roots = NULL;
212 	int ret;
213 
214 	btrfs_init_dummy_trans(&trans, fs_info);
215 
216 	test_msg("running qgroup add/remove tests");
217 	ret = btrfs_create_qgroup(&trans, BTRFS_FS_TREE_OBJECTID);
218 	if (ret) {
219 		test_err("couldn't create a qgroup %d", ret);
220 		return ret;
221 	}
222 
223 	/*
224 	 * Since the test trans doesn't have the complicated delayed refs,
225 	 * we can only call btrfs_qgroup_account_extent() directly to test
226 	 * quota.
227 	 */
228 	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, false);
229 	if (ret) {
230 		test_err("couldn't find old roots: %d", ret);
231 		return ret;
232 	}
233 
234 	ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
235 				BTRFS_FS_TREE_OBJECTID);
236 	if (ret) {
237 		ulist_free(old_roots);
238 		return ret;
239 	}
240 
241 	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, false);
242 	if (ret) {
243 		ulist_free(old_roots);
244 		test_err("couldn't find old roots: %d", ret);
245 		return ret;
246 	}
247 
248 	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
249 					  new_roots);
250 	if (ret) {
251 		test_err("couldn't account space for a qgroup %d", ret);
252 		return ret;
253 	}
254 
255 	/* btrfs_qgroup_account_extent() always frees the ulists passed to it. */
256 	old_roots = NULL;
257 	new_roots = NULL;
258 
259 	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
260 				nodesize, nodesize)) {
261 		test_err("qgroup counts didn't match expected values");
262 		return -EINVAL;
263 	}
264 
265 	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, false);
266 	if (ret) {
267 		test_err("couldn't find old roots: %d", ret);
268 		return ret;
269 	}
270 
271 	ret = remove_extent_item(root, nodesize, nodesize);
272 	if (ret) {
273 		ulist_free(old_roots);
274 		return -EINVAL;
275 	}
276 
277 	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, false);
278 	if (ret) {
279 		ulist_free(old_roots);
280 		test_err("couldn't find old roots: %d", ret);
281 		return ret;
282 	}
283 
284 	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
285 					  new_roots);
286 	if (ret) {
287 		test_err("couldn't account space for a qgroup %d", ret);
288 		return -EINVAL;
289 	}
290 
291 	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
292 		test_err("qgroup counts didn't match expected values");
293 		return -EINVAL;
294 	}
295 
296 	return 0;
297 }
298 
299 /*
300  * Add a ref for two different roots to make sure the shared value comes out
301  * right, also remove one of the roots and make sure the exclusive count is
302  * adjusted properly.
303  */
304 static int test_multiple_refs(struct btrfs_root *root,
305 		u32 sectorsize, u32 nodesize)
306 {
307 	struct btrfs_trans_handle trans;
308 	struct btrfs_fs_info *fs_info = root->fs_info;
309 	struct ulist *old_roots = NULL;
310 	struct ulist *new_roots = NULL;
311 	int ret;
312 
313 	btrfs_init_dummy_trans(&trans, fs_info);
314 
315 	test_msg("running qgroup multiple refs test");
316 
317 	/*
318 	 * We have BTRFS_FS_TREE_OBJECTID created already from the
319 	 * previous test.
320 	 */
321 	ret = btrfs_create_qgroup(&trans, BTRFS_FIRST_FREE_OBJECTID);
322 	if (ret) {
323 		test_err("couldn't create a qgroup %d", ret);
324 		return ret;
325 	}
326 
327 	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, false);
328 	if (ret) {
329 		test_err("couldn't find old roots: %d", ret);
330 		return ret;
331 	}
332 
333 	ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
334 				BTRFS_FS_TREE_OBJECTID);
335 	if (ret) {
336 		ulist_free(old_roots);
337 		return ret;
338 	}
339 
340 	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, false);
341 	if (ret) {
342 		ulist_free(old_roots);
343 		test_err("couldn't find old roots: %d", ret);
344 		return ret;
345 	}
346 
347 	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
348 					  new_roots);
349 	if (ret) {
350 		test_err("couldn't account space for a qgroup %d", ret);
351 		return ret;
352 	}
353 
354 	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
355 				       nodesize, nodesize)) {
356 		test_err("qgroup counts didn't match expected values");
357 		return -EINVAL;
358 	}
359 
360 	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, false);
361 	if (ret) {
362 		test_err("couldn't find old roots: %d", ret);
363 		return ret;
364 	}
365 
366 	ret = add_tree_ref(root, nodesize, nodesize, 0,
367 			BTRFS_FIRST_FREE_OBJECTID);
368 	if (ret) {
369 		ulist_free(old_roots);
370 		return ret;
371 	}
372 
373 	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, false);
374 	if (ret) {
375 		ulist_free(old_roots);
376 		test_err("couldn't find old roots: %d", ret);
377 		return ret;
378 	}
379 
380 	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
381 					  new_roots);
382 	if (ret) {
383 		test_err("couldn't account space for a qgroup %d", ret);
384 		return ret;
385 	}
386 
387 	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
388 					nodesize, 0)) {
389 		test_err("qgroup counts didn't match expected values");
390 		return -EINVAL;
391 	}
392 
393 	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
394 					nodesize, 0)) {
395 		test_err("qgroup counts didn't match expected values");
396 		return -EINVAL;
397 	}
398 
399 	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, false);
400 	if (ret) {
401 		test_err("couldn't find old roots: %d", ret);
402 		return ret;
403 	}
404 
405 	ret = remove_extent_ref(root, nodesize, nodesize, 0,
406 				BTRFS_FIRST_FREE_OBJECTID);
407 	if (ret) {
408 		ulist_free(old_roots);
409 		return ret;
410 	}
411 
412 	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, false);
413 	if (ret) {
414 		ulist_free(old_roots);
415 		test_err("couldn't find old roots: %d", ret);
416 		return ret;
417 	}
418 
419 	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
420 					  new_roots);
421 	if (ret) {
422 		test_err("couldn't account space for a qgroup %d", ret);
423 		return ret;
424 	}
425 
426 	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
427 					0, 0)) {
428 		test_err("qgroup counts didn't match expected values");
429 		return -EINVAL;
430 	}
431 
432 	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
433 					nodesize, nodesize)) {
434 		test_err("qgroup counts didn't match expected values");
435 		return -EINVAL;
436 	}
437 
438 	return 0;
439 }
440 
441 int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
442 {
443 	struct btrfs_fs_info *fs_info = NULL;
444 	struct btrfs_root *root;
445 	struct btrfs_root *tmp_root;
446 	int ret = 0;
447 
448 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
449 	if (!fs_info) {
450 		test_std_err(TEST_ALLOC_FS_INFO);
451 		return -ENOMEM;
452 	}
453 
454 	root = btrfs_alloc_dummy_root(fs_info);
455 	if (IS_ERR(root)) {
456 		test_std_err(TEST_ALLOC_ROOT);
457 		ret = PTR_ERR(root);
458 		goto out;
459 	}
460 
461 	/* We are using this root as our extent root */
462 	root->root_key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
463 	root->root_key.type = BTRFS_ROOT_ITEM_KEY;
464 	root->root_key.offset = 0;
465 	btrfs_global_root_insert(root);
466 
467 	/*
468 	 * Some of the paths we test assume we have a filled out fs_info, so we
469 	 * just need to add the root in there so we don't panic.
470 	 */
471 	root->fs_info->tree_root = root;
472 	root->fs_info->quota_root = root;
473 	set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
474 
475 	/*
476 	 * Can't use bytenr 0, some things freak out
477 	 * *cough*backref walking code*cough*
478 	 */
479 	root->node = alloc_test_extent_buffer(root->fs_info, nodesize);
480 	if (IS_ERR(root->node)) {
481 		test_err("couldn't allocate dummy buffer");
482 		ret = PTR_ERR(root->node);
483 		goto out;
484 	}
485 	btrfs_set_header_level(root->node, 0);
486 	btrfs_set_header_nritems(root->node, 0);
487 	root->alloc_bytenr += 2 * nodesize;
488 
489 	tmp_root = btrfs_alloc_dummy_root(fs_info);
490 	if (IS_ERR(tmp_root)) {
491 		test_std_err(TEST_ALLOC_ROOT);
492 		ret = PTR_ERR(tmp_root);
493 		goto out;
494 	}
495 
496 	tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID;
497 	root->fs_info->fs_root = tmp_root;
498 	ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
499 	if (ret) {
500 		test_err("couldn't insert fs root %d", ret);
501 		goto out;
502 	}
503 	btrfs_put_root(tmp_root);
504 
505 	tmp_root = btrfs_alloc_dummy_root(fs_info);
506 	if (IS_ERR(tmp_root)) {
507 		test_std_err(TEST_ALLOC_ROOT);
508 		ret = PTR_ERR(tmp_root);
509 		goto out;
510 	}
511 
512 	tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID;
513 	ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
514 	if (ret) {
515 		test_err("couldn't insert fs root %d", ret);
516 		goto out;
517 	}
518 	btrfs_put_root(tmp_root);
519 
520 	test_msg("running qgroup tests");
521 	ret = test_no_shared_qgroup(root, sectorsize, nodesize);
522 	if (ret)
523 		goto out;
524 	ret = test_multiple_refs(root, sectorsize, nodesize);
525 out:
526 	btrfs_free_dummy_root(root);
527 	btrfs_free_dummy_fs_info(fs_info);
528 	return ret;
529 }
530