1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Facebook. All rights reserved.
6 #include <linux/types.h>
7 #include "btrfs-tests.h"
9 #include "../transaction.h"
10 #include "../disk-io.h"
11 #include "../qgroup.h"
12 #include "../backref.h"
14 static int insert_normal_tree_ref(struct btrfs_root *root, u64 bytenr,
15 u64 num_bytes, u64 parent, u64 root_objectid)
17 struct btrfs_trans_handle trans;
18 struct btrfs_extent_item *item;
19 struct btrfs_extent_inline_ref *iref;
20 struct btrfs_tree_block_info *block_info;
21 struct btrfs_path *path;
22 struct extent_buffer *leaf;
24 u32 size = sizeof(*item) + sizeof(*iref) + sizeof(*block_info);
27 btrfs_init_dummy_trans(&trans, NULL);
29 ins.objectid = bytenr;
30 ins.type = BTRFS_EXTENT_ITEM_KEY;
31 ins.offset = num_bytes;
33 path = btrfs_alloc_path();
35 test_std_err(TEST_ALLOC_ROOT);
39 ret = btrfs_insert_empty_item(&trans, root, path, &ins, size);
41 test_err("couldn't insert ref %d", ret);
42 btrfs_free_path(path);
46 leaf = path->nodes[0];
47 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
48 btrfs_set_extent_refs(leaf, item, 1);
49 btrfs_set_extent_generation(leaf, item, 1);
50 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_TREE_BLOCK);
51 block_info = (struct btrfs_tree_block_info *)(item + 1);
52 btrfs_set_tree_block_level(leaf, block_info, 0);
53 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
55 btrfs_set_extent_inline_ref_type(leaf, iref,
56 BTRFS_SHARED_BLOCK_REF_KEY);
57 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
59 btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
60 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
62 btrfs_free_path(path);
66 static int add_tree_ref(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
67 u64 parent, u64 root_objectid)
69 struct btrfs_trans_handle trans;
70 struct btrfs_extent_item *item;
71 struct btrfs_path *path;
76 btrfs_init_dummy_trans(&trans, NULL);
78 key.objectid = bytenr;
79 key.type = BTRFS_EXTENT_ITEM_KEY;
80 key.offset = num_bytes;
82 path = btrfs_alloc_path();
84 test_std_err(TEST_ALLOC_ROOT);
88 ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
90 test_err("couldn't find extent ref");
91 btrfs_free_path(path);
95 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
96 struct btrfs_extent_item);
97 refs = btrfs_extent_refs(path->nodes[0], item);
98 btrfs_set_extent_refs(path->nodes[0], item, refs + 1);
99 btrfs_release_path(path);
101 key.objectid = bytenr;
103 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
106 key.type = BTRFS_TREE_BLOCK_REF_KEY;
107 key.offset = root_objectid;
110 ret = btrfs_insert_empty_item(&trans, root, path, &key, 0);
112 test_err("failed to insert backref");
113 btrfs_free_path(path);
117 static int remove_extent_item(struct btrfs_root *root, u64 bytenr,
120 struct btrfs_trans_handle trans;
121 struct btrfs_key key;
122 struct btrfs_path *path;
125 btrfs_init_dummy_trans(&trans, NULL);
127 key.objectid = bytenr;
128 key.type = BTRFS_EXTENT_ITEM_KEY;
129 key.offset = num_bytes;
131 path = btrfs_alloc_path();
133 test_std_err(TEST_ALLOC_ROOT);
137 ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
139 test_err("didn't find our key %d", ret);
140 btrfs_free_path(path);
143 btrfs_del_item(&trans, root, path);
144 btrfs_free_path(path);
148 static int remove_extent_ref(struct btrfs_root *root, u64 bytenr,
149 u64 num_bytes, u64 parent, u64 root_objectid)
151 struct btrfs_trans_handle trans;
152 struct btrfs_extent_item *item;
153 struct btrfs_path *path;
154 struct btrfs_key key;
158 btrfs_init_dummy_trans(&trans, NULL);
160 key.objectid = bytenr;
161 key.type = BTRFS_EXTENT_ITEM_KEY;
162 key.offset = num_bytes;
164 path = btrfs_alloc_path();
166 test_std_err(TEST_ALLOC_ROOT);
170 ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
172 test_err("couldn't find extent ref");
173 btrfs_free_path(path);
177 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
178 struct btrfs_extent_item);
179 refs = btrfs_extent_refs(path->nodes[0], item);
180 btrfs_set_extent_refs(path->nodes[0], item, refs - 1);
181 btrfs_release_path(path);
183 key.objectid = bytenr;
185 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
188 key.type = BTRFS_TREE_BLOCK_REF_KEY;
189 key.offset = root_objectid;
192 ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
194 test_err("couldn't find backref %d", ret);
195 btrfs_free_path(path);
198 btrfs_del_item(&trans, root, path);
199 btrfs_free_path(path);
203 static int test_no_shared_qgroup(struct btrfs_root *root,
204 u32 sectorsize, u32 nodesize)
206 struct btrfs_trans_handle trans;
207 struct btrfs_fs_info *fs_info = root->fs_info;
208 struct ulist *old_roots = NULL;
209 struct ulist *new_roots = NULL;
212 btrfs_init_dummy_trans(&trans, fs_info);
214 test_msg("running qgroup add/remove tests");
215 ret = btrfs_create_qgroup(&trans, BTRFS_FS_TREE_OBJECTID);
217 test_err("couldn't create a qgroup %d", ret);
222 * Since the test trans doesn't have the complicated delayed refs,
223 * we can only call btrfs_qgroup_account_extent() directly to test
226 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, false);
228 test_err("couldn't find old roots: %d", ret);
232 ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
233 BTRFS_FS_TREE_OBJECTID);
235 ulist_free(old_roots);
239 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, false);
241 ulist_free(old_roots);
242 test_err("couldn't find old roots: %d", ret);
246 ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
249 test_err("couldn't account space for a qgroup %d", ret);
253 /* btrfs_qgroup_account_extent() always frees the ulists passed to it. */
257 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
258 nodesize, nodesize)) {
259 test_err("qgroup counts didn't match expected values");
263 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, false);
265 test_err("couldn't find old roots: %d", ret);
269 ret = remove_extent_item(root, nodesize, nodesize);
271 ulist_free(old_roots);
275 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, false);
277 ulist_free(old_roots);
278 test_err("couldn't find old roots: %d", ret);
282 ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
285 test_err("couldn't account space for a qgroup %d", ret);
289 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
290 test_err("qgroup counts didn't match expected values");
298 * Add a ref for two different roots to make sure the shared value comes out
299 * right, also remove one of the roots and make sure the exclusive count is
302 static int test_multiple_refs(struct btrfs_root *root,
303 u32 sectorsize, u32 nodesize)
305 struct btrfs_trans_handle trans;
306 struct btrfs_fs_info *fs_info = root->fs_info;
307 struct ulist *old_roots = NULL;
308 struct ulist *new_roots = NULL;
311 btrfs_init_dummy_trans(&trans, fs_info);
313 test_msg("running qgroup multiple refs test");
316 * We have BTRFS_FS_TREE_OBJECTID created already from the
319 ret = btrfs_create_qgroup(&trans, BTRFS_FIRST_FREE_OBJECTID);
321 test_err("couldn't create a qgroup %d", ret);
325 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, false);
327 test_err("couldn't find old roots: %d", ret);
331 ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
332 BTRFS_FS_TREE_OBJECTID);
334 ulist_free(old_roots);
338 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, false);
340 ulist_free(old_roots);
341 test_err("couldn't find old roots: %d", ret);
345 ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
348 test_err("couldn't account space for a qgroup %d", ret);
352 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
353 nodesize, nodesize)) {
354 test_err("qgroup counts didn't match expected values");
358 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, false);
360 test_err("couldn't find old roots: %d", ret);
364 ret = add_tree_ref(root, nodesize, nodesize, 0,
365 BTRFS_FIRST_FREE_OBJECTID);
367 ulist_free(old_roots);
371 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, false);
373 ulist_free(old_roots);
374 test_err("couldn't find old roots: %d", ret);
378 ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
381 test_err("couldn't account space for a qgroup %d", ret);
385 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
387 test_err("qgroup counts didn't match expected values");
391 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
393 test_err("qgroup counts didn't match expected values");
397 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, false);
399 test_err("couldn't find old roots: %d", ret);
403 ret = remove_extent_ref(root, nodesize, nodesize, 0,
404 BTRFS_FIRST_FREE_OBJECTID);
406 ulist_free(old_roots);
410 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, false);
412 ulist_free(old_roots);
413 test_err("couldn't find old roots: %d", ret);
417 ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
420 test_err("couldn't account space for a qgroup %d", ret);
424 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
426 test_err("qgroup counts didn't match expected values");
430 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
431 nodesize, nodesize)) {
432 test_err("qgroup counts didn't match expected values");
439 int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
441 struct btrfs_fs_info *fs_info = NULL;
442 struct btrfs_root *root;
443 struct btrfs_root *tmp_root;
446 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
448 test_std_err(TEST_ALLOC_FS_INFO);
452 root = btrfs_alloc_dummy_root(fs_info);
454 test_std_err(TEST_ALLOC_ROOT);
459 /* We are using this root as our extent root */
460 root->root_key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
461 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
462 root->root_key.offset = 0;
463 btrfs_global_root_insert(root);
466 * Some of the paths we test assume we have a filled out fs_info, so we
467 * just need to add the root in there so we don't panic.
469 root->fs_info->tree_root = root;
470 root->fs_info->quota_root = root;
471 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
474 * Can't use bytenr 0, some things freak out
475 * *cough*backref walking code*cough*
477 root->node = alloc_test_extent_buffer(root->fs_info, nodesize);
478 if (IS_ERR(root->node)) {
479 test_err("couldn't allocate dummy buffer");
480 ret = PTR_ERR(root->node);
483 btrfs_set_header_level(root->node, 0);
484 btrfs_set_header_nritems(root->node, 0);
485 root->alloc_bytenr += 2 * nodesize;
487 tmp_root = btrfs_alloc_dummy_root(fs_info);
488 if (IS_ERR(tmp_root)) {
489 test_std_err(TEST_ALLOC_ROOT);
490 ret = PTR_ERR(tmp_root);
494 tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID;
495 root->fs_info->fs_root = tmp_root;
496 ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
498 test_err("couldn't insert fs root %d", ret);
501 btrfs_put_root(tmp_root);
503 tmp_root = btrfs_alloc_dummy_root(fs_info);
504 if (IS_ERR(tmp_root)) {
505 test_std_err(TEST_ALLOC_ROOT);
506 ret = PTR_ERR(tmp_root);
510 tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID;
511 ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
513 test_err("couldn't insert fs root %d", ret);
516 btrfs_put_root(tmp_root);
518 test_msg("running qgroup tests");
519 ret = test_no_shared_qgroup(root, sectorsize, nodesize);
522 ret = test_multiple_refs(root, sectorsize, nodesize);
524 btrfs_free_dummy_root(root);
525 btrfs_free_dummy_fs_info(fs_info);