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 #include "../accessors.h"
16 static int insert_normal_tree_ref(struct btrfs_root
*root
, u64 bytenr
,
17 u64 num_bytes
, u64 parent
, u64 root_objectid
)
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
;
26 u32 size
= sizeof(*item
) + sizeof(*iref
) + sizeof(*block_info
);
29 btrfs_init_dummy_trans(&trans
, NULL
);
31 ins
.objectid
= bytenr
;
32 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
33 ins
.offset
= num_bytes
;
35 path
= btrfs_alloc_path();
37 test_std_err(TEST_ALLOC_ROOT
);
41 ret
= btrfs_insert_empty_item(&trans
, root
, path
, &ins
, size
);
43 test_err("couldn't insert ref %d", ret
);
44 btrfs_free_path(path
);
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);
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
);
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
);
64 btrfs_free_path(path
);
68 static int add_tree_ref(struct btrfs_root
*root
, u64 bytenr
, u64 num_bytes
,
69 u64 parent
, u64 root_objectid
)
71 struct btrfs_trans_handle trans
;
72 struct btrfs_extent_item
*item
;
73 struct btrfs_path
*path
;
78 btrfs_init_dummy_trans(&trans
, NULL
);
80 key
.objectid
= bytenr
;
81 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
82 key
.offset
= num_bytes
;
84 path
= btrfs_alloc_path();
86 test_std_err(TEST_ALLOC_ROOT
);
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
);
139 ret
= btrfs_search_slot(&trans
, root
, &key
, path
, -1, 1);
141 test_err("didn't find our key %d", ret
);
142 btrfs_free_path(path
);
145 btrfs_del_item(&trans
, root
, path
);
146 btrfs_free_path(path
);
150 static int remove_extent_ref(struct btrfs_root
*root
, u64 bytenr
,
151 u64 num_bytes
, u64 parent
, u64 root_objectid
)
153 struct btrfs_trans_handle trans
;
154 struct btrfs_extent_item
*item
;
155 struct btrfs_path
*path
;
156 struct btrfs_key key
;
160 btrfs_init_dummy_trans(&trans
, NULL
);
162 key
.objectid
= bytenr
;
163 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
164 key
.offset
= num_bytes
;
166 path
= btrfs_alloc_path();
168 test_std_err(TEST_ALLOC_ROOT
);
172 ret
= btrfs_search_slot(&trans
, root
, &key
, path
, 0, 1);
174 test_err("couldn't find extent ref");
175 btrfs_free_path(path
);
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
);
185 key
.objectid
= bytenr
;
187 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
190 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
191 key
.offset
= root_objectid
;
194 ret
= btrfs_search_slot(&trans
, root
, &key
, path
, -1, 1);
196 test_err("couldn't find backref %d", ret
);
197 btrfs_free_path(path
);
200 btrfs_del_item(&trans
, root
, path
);
201 btrfs_free_path(path
);
205 static int test_no_shared_qgroup(struct btrfs_root
*root
,
206 u32 sectorsize
, u32 nodesize
)
208 struct btrfs_backref_walk_ctx ctx
= { 0 };
209 struct btrfs_trans_handle trans
;
210 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
211 struct ulist
*old_roots
= NULL
;
212 struct ulist
*new_roots
= NULL
;
215 btrfs_init_dummy_trans(&trans
, fs_info
);
217 test_msg("running qgroup add/remove tests");
218 ret
= btrfs_create_qgroup(&trans
, BTRFS_FS_TREE_OBJECTID
);
220 test_err("couldn't create a qgroup %d", ret
);
224 ctx
.bytenr
= nodesize
;
226 ctx
.fs_info
= fs_info
;
229 * Since the test trans doesn't have the complicated delayed refs,
230 * we can only call btrfs_qgroup_account_extent() directly to test
233 ret
= btrfs_find_all_roots(&ctx
, false);
235 test_err("couldn't find old roots: %d", ret
);
238 old_roots
= ctx
.roots
;
241 ret
= insert_normal_tree_ref(root
, nodesize
, nodesize
, 0,
242 BTRFS_FS_TREE_OBJECTID
);
244 ulist_free(old_roots
);
248 ret
= btrfs_find_all_roots(&ctx
, false);
250 ulist_free(old_roots
);
251 test_err("couldn't find old roots: %d", ret
);
254 new_roots
= ctx
.roots
;
257 ret
= btrfs_qgroup_account_extent(&trans
, nodesize
, nodesize
, old_roots
,
260 test_err("couldn't account space for a qgroup %d", ret
);
264 /* btrfs_qgroup_account_extent() always frees the ulists passed to it. */
268 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
,
269 nodesize
, nodesize
)) {
270 test_err("qgroup counts didn't match expected values");
274 ret
= btrfs_find_all_roots(&ctx
, false);
276 test_err("couldn't find old roots: %d", ret
);
279 old_roots
= ctx
.roots
;
282 ret
= remove_extent_item(root
, nodesize
, nodesize
);
284 ulist_free(old_roots
);
288 ret
= btrfs_find_all_roots(&ctx
, false);
290 ulist_free(old_roots
);
291 test_err("couldn't find old roots: %d", ret
);
294 new_roots
= ctx
.roots
;
297 ret
= btrfs_qgroup_account_extent(&trans
, nodesize
, nodesize
, old_roots
,
300 test_err("couldn't account space for a qgroup %d", ret
);
304 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
, 0, 0)) {
305 test_err("qgroup counts didn't match expected values");
313 * Add a ref for two different roots to make sure the shared value comes out
314 * right, also remove one of the roots and make sure the exclusive count is
317 static int test_multiple_refs(struct btrfs_root
*root
,
318 u32 sectorsize
, u32 nodesize
)
320 struct btrfs_backref_walk_ctx ctx
= { 0 };
321 struct btrfs_trans_handle trans
;
322 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
323 struct ulist
*old_roots
= NULL
;
324 struct ulist
*new_roots
= NULL
;
327 btrfs_init_dummy_trans(&trans
, fs_info
);
329 test_msg("running qgroup multiple refs test");
332 * We have BTRFS_FS_TREE_OBJECTID created already from the
335 ret
= btrfs_create_qgroup(&trans
, BTRFS_FIRST_FREE_OBJECTID
);
337 test_err("couldn't create a qgroup %d", ret
);
341 ctx
.bytenr
= nodesize
;
343 ctx
.fs_info
= fs_info
;
345 ret
= btrfs_find_all_roots(&ctx
, false);
347 test_err("couldn't find old roots: %d", ret
);
350 old_roots
= ctx
.roots
;
353 ret
= insert_normal_tree_ref(root
, nodesize
, nodesize
, 0,
354 BTRFS_FS_TREE_OBJECTID
);
356 ulist_free(old_roots
);
360 ret
= btrfs_find_all_roots(&ctx
, false);
362 ulist_free(old_roots
);
363 test_err("couldn't find old roots: %d", ret
);
366 new_roots
= ctx
.roots
;
369 ret
= btrfs_qgroup_account_extent(&trans
, nodesize
, nodesize
, old_roots
,
372 test_err("couldn't account space for a qgroup %d", ret
);
376 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
,
377 nodesize
, nodesize
)) {
378 test_err("qgroup counts didn't match expected values");
382 ret
= btrfs_find_all_roots(&ctx
, false);
384 test_err("couldn't find old roots: %d", ret
);
387 old_roots
= ctx
.roots
;
390 ret
= add_tree_ref(root
, nodesize
, nodesize
, 0,
391 BTRFS_FIRST_FREE_OBJECTID
);
393 ulist_free(old_roots
);
397 ret
= btrfs_find_all_roots(&ctx
, false);
399 ulist_free(old_roots
);
400 test_err("couldn't find old roots: %d", ret
);
403 new_roots
= ctx
.roots
;
406 ret
= btrfs_qgroup_account_extent(&trans
, nodesize
, nodesize
, old_roots
,
409 test_err("couldn't account space for a qgroup %d", ret
);
413 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
,
415 test_err("qgroup counts didn't match expected values");
419 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FIRST_FREE_OBJECTID
,
421 test_err("qgroup counts didn't match expected values");
425 ret
= btrfs_find_all_roots(&ctx
, false);
427 test_err("couldn't find old roots: %d", ret
);
430 old_roots
= ctx
.roots
;
433 ret
= remove_extent_ref(root
, nodesize
, nodesize
, 0,
434 BTRFS_FIRST_FREE_OBJECTID
);
436 ulist_free(old_roots
);
440 ret
= btrfs_find_all_roots(&ctx
, false);
442 ulist_free(old_roots
);
443 test_err("couldn't find old roots: %d", ret
);
446 new_roots
= ctx
.roots
;
449 ret
= btrfs_qgroup_account_extent(&trans
, nodesize
, nodesize
, old_roots
,
452 test_err("couldn't account space for a qgroup %d", ret
);
456 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FIRST_FREE_OBJECTID
,
458 test_err("qgroup counts didn't match expected values");
462 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
,
463 nodesize
, nodesize
)) {
464 test_err("qgroup counts didn't match expected values");
471 int btrfs_test_qgroups(u32 sectorsize
, u32 nodesize
)
473 struct btrfs_fs_info
*fs_info
= NULL
;
474 struct btrfs_root
*root
;
475 struct btrfs_root
*tmp_root
;
478 fs_info
= btrfs_alloc_dummy_fs_info(nodesize
, sectorsize
);
480 test_std_err(TEST_ALLOC_FS_INFO
);
484 root
= btrfs_alloc_dummy_root(fs_info
);
486 test_std_err(TEST_ALLOC_ROOT
);
491 /* We are using this root as our extent root */
492 root
->root_key
.objectid
= BTRFS_EXTENT_TREE_OBJECTID
;
493 root
->root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
494 root
->root_key
.offset
= 0;
495 btrfs_global_root_insert(root
);
498 * Some of the paths we test assume we have a filled out fs_info, so we
499 * just need to add the root in there so we don't panic.
501 root
->fs_info
->tree_root
= root
;
502 root
->fs_info
->quota_root
= root
;
503 set_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
);
506 * Can't use bytenr 0, some things freak out
507 * *cough*backref walking code*cough*
509 root
->node
= alloc_test_extent_buffer(root
->fs_info
, nodesize
);
510 if (IS_ERR(root
->node
)) {
511 test_err("couldn't allocate dummy buffer");
512 ret
= PTR_ERR(root
->node
);
515 btrfs_set_header_level(root
->node
, 0);
516 btrfs_set_header_nritems(root
->node
, 0);
517 root
->alloc_bytenr
+= 2 * nodesize
;
519 tmp_root
= btrfs_alloc_dummy_root(fs_info
);
520 if (IS_ERR(tmp_root
)) {
521 test_std_err(TEST_ALLOC_ROOT
);
522 ret
= PTR_ERR(tmp_root
);
526 tmp_root
->root_key
.objectid
= BTRFS_FS_TREE_OBJECTID
;
527 root
->fs_info
->fs_root
= tmp_root
;
528 ret
= btrfs_insert_fs_root(root
->fs_info
, tmp_root
);
530 test_err("couldn't insert fs root %d", ret
);
533 btrfs_put_root(tmp_root
);
535 tmp_root
= btrfs_alloc_dummy_root(fs_info
);
536 if (IS_ERR(tmp_root
)) {
537 test_std_err(TEST_ALLOC_ROOT
);
538 ret
= PTR_ERR(tmp_root
);
542 tmp_root
->root_key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
543 ret
= btrfs_insert_fs_root(root
->fs_info
, tmp_root
);
545 test_err("couldn't insert fs root %d", ret
);
548 btrfs_put_root(tmp_root
);
550 test_msg("running qgroup tests");
551 ret
= test_no_shared_qgroup(root
, sectorsize
, nodesize
);
554 ret
= test_multiple_refs(root
, sectorsize
, nodesize
);
556 btrfs_free_dummy_root(root
);
557 btrfs_free_dummy_fs_info(fs_info
);