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 path
->leave_spinning
= 1;
40 ret
= btrfs_insert_empty_item(&trans
, root
, path
, &ins
, size
);
42 test_err("couldn't insert ref %d", ret
);
43 btrfs_free_path(path
);
47 leaf
= path
->nodes
[0];
48 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
49 btrfs_set_extent_refs(leaf
, item
, 1);
50 btrfs_set_extent_generation(leaf
, item
, 1);
51 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_TREE_BLOCK
);
52 block_info
= (struct btrfs_tree_block_info
*)(item
+ 1);
53 btrfs_set_tree_block_level(leaf
, block_info
, 0);
54 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
56 btrfs_set_extent_inline_ref_type(leaf
, iref
,
57 BTRFS_SHARED_BLOCK_REF_KEY
);
58 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
60 btrfs_set_extent_inline_ref_type(leaf
, iref
, BTRFS_TREE_BLOCK_REF_KEY
);
61 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
63 btrfs_free_path(path
);
67 static int add_tree_ref(struct btrfs_root
*root
, u64 bytenr
, u64 num_bytes
,
68 u64 parent
, u64 root_objectid
)
70 struct btrfs_trans_handle trans
;
71 struct btrfs_extent_item
*item
;
72 struct btrfs_path
*path
;
77 btrfs_init_dummy_trans(&trans
, NULL
);
79 key
.objectid
= bytenr
;
80 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
81 key
.offset
= num_bytes
;
83 path
= btrfs_alloc_path();
85 test_std_err(TEST_ALLOC_ROOT
);
89 path
->leave_spinning
= 1;
90 ret
= btrfs_search_slot(&trans
, root
, &key
, path
, 0, 1);
92 test_err("couldn't find extent ref");
93 btrfs_free_path(path
);
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
);
103 key
.objectid
= bytenr
;
105 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
108 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
109 key
.offset
= root_objectid
;
112 ret
= btrfs_insert_empty_item(&trans
, root
, path
, &key
, 0);
114 test_err("failed to insert backref");
115 btrfs_free_path(path
);
119 static int remove_extent_item(struct btrfs_root
*root
, u64 bytenr
,
122 struct btrfs_trans_handle trans
;
123 struct btrfs_key key
;
124 struct btrfs_path
*path
;
127 btrfs_init_dummy_trans(&trans
, NULL
);
129 key
.objectid
= bytenr
;
130 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
131 key
.offset
= num_bytes
;
133 path
= btrfs_alloc_path();
135 test_std_err(TEST_ALLOC_ROOT
);
138 path
->leave_spinning
= 1;
140 ret
= btrfs_search_slot(&trans
, root
, &key
, path
, -1, 1);
142 test_err("didn't find our key %d", ret
);
143 btrfs_free_path(path
);
146 btrfs_del_item(&trans
, root
, path
);
147 btrfs_free_path(path
);
151 static int remove_extent_ref(struct btrfs_root
*root
, u64 bytenr
,
152 u64 num_bytes
, u64 parent
, u64 root_objectid
)
154 struct btrfs_trans_handle trans
;
155 struct btrfs_extent_item
*item
;
156 struct btrfs_path
*path
;
157 struct btrfs_key key
;
161 btrfs_init_dummy_trans(&trans
, NULL
);
163 key
.objectid
= bytenr
;
164 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
165 key
.offset
= num_bytes
;
167 path
= btrfs_alloc_path();
169 test_std_err(TEST_ALLOC_ROOT
);
173 path
->leave_spinning
= 1;
174 ret
= btrfs_search_slot(&trans
, root
, &key
, path
, 0, 1);
176 test_err("couldn't find extent ref");
177 btrfs_free_path(path
);
181 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
182 struct btrfs_extent_item
);
183 refs
= btrfs_extent_refs(path
->nodes
[0], item
);
184 btrfs_set_extent_refs(path
->nodes
[0], item
, refs
- 1);
185 btrfs_release_path(path
);
187 key
.objectid
= bytenr
;
189 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
192 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
193 key
.offset
= root_objectid
;
196 ret
= btrfs_search_slot(&trans
, root
, &key
, path
, -1, 1);
198 test_err("couldn't find backref %d", ret
);
199 btrfs_free_path(path
);
202 btrfs_del_item(&trans
, root
, path
);
203 btrfs_free_path(path
);
207 static int test_no_shared_qgroup(struct btrfs_root
*root
,
208 u32 sectorsize
, u32 nodesize
)
210 struct btrfs_trans_handle trans
;
211 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
212 struct ulist
*old_roots
= NULL
;
213 struct ulist
*new_roots
= NULL
;
216 btrfs_init_dummy_trans(&trans
, fs_info
);
218 test_msg("running qgroup add/remove tests");
219 ret
= btrfs_create_qgroup(&trans
, BTRFS_FS_TREE_OBJECTID
);
221 test_err("couldn't create a qgroup %d", ret
);
226 * Since the test trans doesn't have the complicated delayed refs,
227 * we can only call btrfs_qgroup_account_extent() directly to test
230 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &old_roots
,
233 ulist_free(old_roots
);
234 test_err("couldn't find old roots: %d", ret
);
238 ret
= insert_normal_tree_ref(root
, nodesize
, nodesize
, 0,
239 BTRFS_FS_TREE_OBJECTID
);
243 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &new_roots
,
246 ulist_free(old_roots
);
247 ulist_free(new_roots
);
248 test_err("couldn't find old roots: %d", ret
);
252 ret
= btrfs_qgroup_account_extent(&trans
, nodesize
, nodesize
, old_roots
,
255 test_err("couldn't account space for a qgroup %d", ret
);
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");
267 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &old_roots
,
270 ulist_free(old_roots
);
271 test_err("couldn't find old roots: %d", ret
);
275 ret
= remove_extent_item(root
, nodesize
, nodesize
);
279 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &new_roots
,
282 ulist_free(old_roots
);
283 ulist_free(new_roots
);
284 test_err("couldn't find old roots: %d", ret
);
288 ret
= btrfs_qgroup_account_extent(&trans
, nodesize
, nodesize
, old_roots
,
291 test_err("couldn't account space for a qgroup %d", ret
);
295 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
, 0, 0)) {
296 test_err("qgroup counts didn't match expected values");
304 * Add a ref for two different roots to make sure the shared value comes out
305 * right, also remove one of the roots and make sure the exclusive count is
308 static int test_multiple_refs(struct btrfs_root
*root
,
309 u32 sectorsize
, u32 nodesize
)
311 struct btrfs_trans_handle trans
;
312 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
313 struct ulist
*old_roots
= NULL
;
314 struct ulist
*new_roots
= NULL
;
317 btrfs_init_dummy_trans(&trans
, fs_info
);
319 test_msg("running qgroup multiple refs test");
322 * We have BTRFS_FS_TREE_OBJECTID created already from the
325 ret
= btrfs_create_qgroup(&trans
, BTRFS_FIRST_FREE_OBJECTID
);
327 test_err("couldn't create a qgroup %d", ret
);
331 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &old_roots
,
334 ulist_free(old_roots
);
335 test_err("couldn't find old roots: %d", ret
);
339 ret
= insert_normal_tree_ref(root
, nodesize
, nodesize
, 0,
340 BTRFS_FS_TREE_OBJECTID
);
344 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &new_roots
,
347 ulist_free(old_roots
);
348 ulist_free(new_roots
);
349 test_err("couldn't find old roots: %d", ret
);
353 ret
= btrfs_qgroup_account_extent(&trans
, nodesize
, nodesize
, old_roots
,
356 test_err("couldn't account space for a qgroup %d", 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");
366 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &old_roots
,
369 ulist_free(old_roots
);
370 test_err("couldn't find old roots: %d", ret
);
374 ret
= add_tree_ref(root
, nodesize
, nodesize
, 0,
375 BTRFS_FIRST_FREE_OBJECTID
);
379 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &new_roots
,
382 ulist_free(old_roots
);
383 ulist_free(new_roots
);
384 test_err("couldn't find old roots: %d", ret
);
388 ret
= btrfs_qgroup_account_extent(&trans
, nodesize
, nodesize
, old_roots
,
391 test_err("couldn't account space for a qgroup %d", ret
);
395 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
,
397 test_err("qgroup counts didn't match expected values");
401 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FIRST_FREE_OBJECTID
,
403 test_err("qgroup counts didn't match expected values");
407 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &old_roots
,
410 ulist_free(old_roots
);
411 test_err("couldn't find old roots: %d", ret
);
415 ret
= remove_extent_ref(root
, nodesize
, nodesize
, 0,
416 BTRFS_FIRST_FREE_OBJECTID
);
420 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &new_roots
,
423 ulist_free(old_roots
);
424 ulist_free(new_roots
);
425 test_err("couldn't find old roots: %d", ret
);
429 ret
= btrfs_qgroup_account_extent(&trans
, nodesize
, nodesize
, old_roots
,
432 test_err("couldn't account space for a qgroup %d", ret
);
436 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FIRST_FREE_OBJECTID
,
438 test_err("qgroup counts didn't match expected values");
442 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
,
443 nodesize
, nodesize
)) {
444 test_err("qgroup counts didn't match expected values");
451 int btrfs_test_qgroups(u32 sectorsize
, u32 nodesize
)
453 struct btrfs_fs_info
*fs_info
= NULL
;
454 struct btrfs_root
*root
;
455 struct btrfs_root
*tmp_root
;
458 fs_info
= btrfs_alloc_dummy_fs_info(nodesize
, sectorsize
);
460 test_std_err(TEST_ALLOC_FS_INFO
);
464 root
= btrfs_alloc_dummy_root(fs_info
);
466 test_std_err(TEST_ALLOC_ROOT
);
471 /* We are using this root as our extent root */
472 root
->fs_info
->extent_root
= root
;
475 * Some of the paths we test assume we have a filled out fs_info, so we
476 * just need to add the root in there so we don't panic.
478 root
->fs_info
->tree_root
= root
;
479 root
->fs_info
->quota_root
= root
;
480 set_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
);
483 * Can't use bytenr 0, some things freak out
484 * *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
);
492 btrfs_set_header_level(root
->node
, 0);
493 btrfs_set_header_nritems(root
->node
, 0);
494 root
->alloc_bytenr
+= 2 * nodesize
;
496 tmp_root
= btrfs_alloc_dummy_root(fs_info
);
497 if (IS_ERR(tmp_root
)) {
498 test_std_err(TEST_ALLOC_ROOT
);
499 ret
= PTR_ERR(tmp_root
);
503 tmp_root
->root_key
.objectid
= BTRFS_FS_TREE_OBJECTID
;
504 root
->fs_info
->fs_root
= tmp_root
;
505 ret
= btrfs_insert_fs_root(root
->fs_info
, tmp_root
);
507 test_err("couldn't insert fs root %d", ret
);
510 btrfs_put_root(tmp_root
);
512 tmp_root
= btrfs_alloc_dummy_root(fs_info
);
513 if (IS_ERR(tmp_root
)) {
514 test_std_err(TEST_ALLOC_ROOT
);
515 ret
= PTR_ERR(tmp_root
);
519 tmp_root
->root_key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
520 ret
= btrfs_insert_fs_root(root
->fs_info
, tmp_root
);
522 test_err("couldn't insert fs root %d", ret
);
525 btrfs_put_root(tmp_root
);
527 test_msg("running qgroup tests");
528 ret
= test_no_shared_qgroup(root
, sectorsize
, nodesize
);
531 ret
= test_multiple_refs(root
, sectorsize
, nodesize
);
533 btrfs_free_dummy_root(root
);
534 btrfs_free_dummy_fs_info(fs_info
);