2 * Copyright (C) 2013 Facebook. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/types.h>
20 #include "btrfs-tests.h"
22 #include "../transaction.h"
23 #include "../disk-io.h"
24 #include "../qgroup.h"
25 #include "../backref.h"
27 static int insert_normal_tree_ref(struct btrfs_root
*root
, u64 bytenr
,
28 u64 num_bytes
, u64 parent
, u64 root_objectid
)
30 struct btrfs_trans_handle trans
;
31 struct btrfs_extent_item
*item
;
32 struct btrfs_extent_inline_ref
*iref
;
33 struct btrfs_tree_block_info
*block_info
;
34 struct btrfs_path
*path
;
35 struct extent_buffer
*leaf
;
37 u32 size
= sizeof(*item
) + sizeof(*iref
) + sizeof(*block_info
);
40 btrfs_init_dummy_trans(&trans
);
42 ins
.objectid
= bytenr
;
43 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
44 ins
.offset
= num_bytes
;
46 path
= btrfs_alloc_path();
48 test_msg("Couldn't allocate path\n");
52 path
->leave_spinning
= 1;
53 ret
= btrfs_insert_empty_item(&trans
, root
, path
, &ins
, size
);
55 test_msg("Couldn't insert ref %d\n", ret
);
56 btrfs_free_path(path
);
60 leaf
= path
->nodes
[0];
61 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_extent_item
);
62 btrfs_set_extent_refs(leaf
, item
, 1);
63 btrfs_set_extent_generation(leaf
, item
, 1);
64 btrfs_set_extent_flags(leaf
, item
, BTRFS_EXTENT_FLAG_TREE_BLOCK
);
65 block_info
= (struct btrfs_tree_block_info
*)(item
+ 1);
66 btrfs_set_tree_block_level(leaf
, block_info
, 1);
67 iref
= (struct btrfs_extent_inline_ref
*)(block_info
+ 1);
69 btrfs_set_extent_inline_ref_type(leaf
, iref
,
70 BTRFS_SHARED_BLOCK_REF_KEY
);
71 btrfs_set_extent_inline_ref_offset(leaf
, iref
, parent
);
73 btrfs_set_extent_inline_ref_type(leaf
, iref
, BTRFS_TREE_BLOCK_REF_KEY
);
74 btrfs_set_extent_inline_ref_offset(leaf
, iref
, root_objectid
);
76 btrfs_free_path(path
);
80 static int add_tree_ref(struct btrfs_root
*root
, u64 bytenr
, u64 num_bytes
,
81 u64 parent
, u64 root_objectid
)
83 struct btrfs_trans_handle trans
;
84 struct btrfs_extent_item
*item
;
85 struct btrfs_path
*path
;
90 btrfs_init_dummy_trans(&trans
);
92 key
.objectid
= bytenr
;
93 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
94 key
.offset
= num_bytes
;
96 path
= btrfs_alloc_path();
98 test_msg("Couldn't allocate path\n");
102 path
->leave_spinning
= 1;
103 ret
= btrfs_search_slot(&trans
, root
, &key
, path
, 0, 1);
105 test_msg("Couldn't find extent ref\n");
106 btrfs_free_path(path
);
110 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
111 struct btrfs_extent_item
);
112 refs
= btrfs_extent_refs(path
->nodes
[0], item
);
113 btrfs_set_extent_refs(path
->nodes
[0], item
, refs
+ 1);
114 btrfs_release_path(path
);
116 key
.objectid
= bytenr
;
118 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
121 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
122 key
.offset
= root_objectid
;
125 ret
= btrfs_insert_empty_item(&trans
, root
, path
, &key
, 0);
127 test_msg("Failed to insert backref\n");
128 btrfs_free_path(path
);
132 static int remove_extent_item(struct btrfs_root
*root
, u64 bytenr
,
135 struct btrfs_trans_handle trans
;
136 struct btrfs_key key
;
137 struct btrfs_path
*path
;
140 btrfs_init_dummy_trans(&trans
);
142 key
.objectid
= bytenr
;
143 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
144 key
.offset
= num_bytes
;
146 path
= btrfs_alloc_path();
148 test_msg("Couldn't allocate path\n");
151 path
->leave_spinning
= 1;
153 ret
= btrfs_search_slot(&trans
, root
, &key
, path
, -1, 1);
155 test_msg("Didn't find our key %d\n", ret
);
156 btrfs_free_path(path
);
159 btrfs_del_item(&trans
, root
, path
);
160 btrfs_free_path(path
);
164 static int remove_extent_ref(struct btrfs_root
*root
, u64 bytenr
,
165 u64 num_bytes
, u64 parent
, u64 root_objectid
)
167 struct btrfs_trans_handle trans
;
168 struct btrfs_extent_item
*item
;
169 struct btrfs_path
*path
;
170 struct btrfs_key key
;
174 btrfs_init_dummy_trans(&trans
);
176 key
.objectid
= bytenr
;
177 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
178 key
.offset
= num_bytes
;
180 path
= btrfs_alloc_path();
182 test_msg("Couldn't allocate path\n");
186 path
->leave_spinning
= 1;
187 ret
= btrfs_search_slot(&trans
, root
, &key
, path
, 0, 1);
189 test_msg("Couldn't find extent ref\n");
190 btrfs_free_path(path
);
194 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
195 struct btrfs_extent_item
);
196 refs
= btrfs_extent_refs(path
->nodes
[0], item
);
197 btrfs_set_extent_refs(path
->nodes
[0], item
, refs
- 1);
198 btrfs_release_path(path
);
200 key
.objectid
= bytenr
;
202 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
205 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
206 key
.offset
= root_objectid
;
209 ret
= btrfs_search_slot(&trans
, root
, &key
, path
, -1, 1);
211 test_msg("Couldn't find backref %d\n", ret
);
212 btrfs_free_path(path
);
215 btrfs_del_item(&trans
, root
, path
);
216 btrfs_free_path(path
);
220 static int test_no_shared_qgroup(struct btrfs_root
*root
,
221 u32 sectorsize
, u32 nodesize
)
223 struct btrfs_trans_handle trans
;
224 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
225 struct ulist
*old_roots
= NULL
;
226 struct ulist
*new_roots
= NULL
;
229 btrfs_init_dummy_trans(&trans
);
231 test_msg("Qgroup basic add\n");
232 ret
= btrfs_create_qgroup(NULL
, fs_info
, BTRFS_FS_TREE_OBJECTID
);
234 test_msg("Couldn't create a qgroup %d\n", ret
);
239 * Since the test trans doesn't have the complicated delayed refs,
240 * we can only call btrfs_qgroup_account_extent() directly to test
243 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &old_roots
,
246 ulist_free(old_roots
);
247 test_msg("Couldn't find old roots: %d\n", ret
);
251 ret
= insert_normal_tree_ref(root
, nodesize
, nodesize
, 0,
252 BTRFS_FS_TREE_OBJECTID
);
256 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &new_roots
,
259 ulist_free(old_roots
);
260 ulist_free(new_roots
);
261 test_msg("Couldn't find old roots: %d\n", ret
);
265 ret
= btrfs_qgroup_account_extent(&trans
, fs_info
, nodesize
,
266 nodesize
, old_roots
, new_roots
);
268 test_msg("Couldn't account space for a qgroup %d\n", ret
);
272 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
,
273 nodesize
, nodesize
)) {
274 test_msg("Qgroup counts didn't match expected values\n");
280 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &old_roots
,
283 ulist_free(old_roots
);
284 test_msg("Couldn't find old roots: %d\n", ret
);
288 ret
= remove_extent_item(root
, nodesize
, nodesize
);
292 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &new_roots
,
295 ulist_free(old_roots
);
296 ulist_free(new_roots
);
297 test_msg("Couldn't find old roots: %d\n", ret
);
301 ret
= btrfs_qgroup_account_extent(&trans
, fs_info
, nodesize
,
302 nodesize
, old_roots
, new_roots
);
304 test_msg("Couldn't account space for a qgroup %d\n", ret
);
308 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
, 0, 0)) {
309 test_msg("Qgroup counts didn't match expected values\n");
317 * Add a ref for two different roots to make sure the shared value comes out
318 * right, also remove one of the roots and make sure the exclusive count is
321 static int test_multiple_refs(struct btrfs_root
*root
,
322 u32 sectorsize
, u32 nodesize
)
324 struct btrfs_trans_handle trans
;
325 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
326 struct ulist
*old_roots
= NULL
;
327 struct ulist
*new_roots
= NULL
;
330 btrfs_init_dummy_trans(&trans
);
332 test_msg("Qgroup multiple refs test\n");
335 * We have BTRFS_FS_TREE_OBJECTID created already from the
338 ret
= btrfs_create_qgroup(NULL
, fs_info
, BTRFS_FIRST_FREE_OBJECTID
);
340 test_msg("Couldn't create a qgroup %d\n", ret
);
344 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &old_roots
,
347 ulist_free(old_roots
);
348 test_msg("Couldn't find old roots: %d\n", ret
);
352 ret
= insert_normal_tree_ref(root
, nodesize
, nodesize
, 0,
353 BTRFS_FS_TREE_OBJECTID
);
357 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &new_roots
,
360 ulist_free(old_roots
);
361 ulist_free(new_roots
);
362 test_msg("Couldn't find old roots: %d\n", ret
);
366 ret
= btrfs_qgroup_account_extent(&trans
, fs_info
, nodesize
,
367 nodesize
, old_roots
, new_roots
);
369 test_msg("Couldn't account space for a qgroup %d\n", ret
);
373 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
,
374 nodesize
, nodesize
)) {
375 test_msg("Qgroup counts didn't match expected values\n");
379 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &old_roots
,
382 ulist_free(old_roots
);
383 test_msg("Couldn't find old roots: %d\n", ret
);
387 ret
= add_tree_ref(root
, nodesize
, nodesize
, 0,
388 BTRFS_FIRST_FREE_OBJECTID
);
392 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &new_roots
,
395 ulist_free(old_roots
);
396 ulist_free(new_roots
);
397 test_msg("Couldn't find old roots: %d\n", ret
);
401 ret
= btrfs_qgroup_account_extent(&trans
, fs_info
, nodesize
,
402 nodesize
, old_roots
, new_roots
);
404 test_msg("Couldn't account space for a qgroup %d\n", ret
);
408 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
,
410 test_msg("Qgroup counts didn't match expected values\n");
414 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FIRST_FREE_OBJECTID
,
416 test_msg("Qgroup counts didn't match expected values\n");
420 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &old_roots
,
423 ulist_free(old_roots
);
424 test_msg("Couldn't find old roots: %d\n", ret
);
428 ret
= remove_extent_ref(root
, nodesize
, nodesize
, 0,
429 BTRFS_FIRST_FREE_OBJECTID
);
433 ret
= btrfs_find_all_roots(&trans
, fs_info
, nodesize
, 0, &new_roots
,
436 ulist_free(old_roots
);
437 ulist_free(new_roots
);
438 test_msg("Couldn't find old roots: %d\n", ret
);
442 ret
= btrfs_qgroup_account_extent(&trans
, fs_info
, nodesize
,
443 nodesize
, old_roots
, new_roots
);
445 test_msg("Couldn't account space for a qgroup %d\n", ret
);
449 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FIRST_FREE_OBJECTID
,
451 test_msg("Qgroup counts didn't match expected values\n");
455 if (btrfs_verify_qgroup_counts(fs_info
, BTRFS_FS_TREE_OBJECTID
,
456 nodesize
, nodesize
)) {
457 test_msg("Qgroup counts didn't match expected values\n");
464 int btrfs_test_qgroups(u32 sectorsize
, u32 nodesize
)
466 struct btrfs_fs_info
*fs_info
= NULL
;
467 struct btrfs_root
*root
;
468 struct btrfs_root
*tmp_root
;
471 fs_info
= btrfs_alloc_dummy_fs_info(nodesize
, sectorsize
);
473 test_msg("Couldn't allocate dummy fs info\n");
477 root
= btrfs_alloc_dummy_root(fs_info
);
479 test_msg("Couldn't allocate root\n");
484 /* We are using this root as our extent root */
485 root
->fs_info
->extent_root
= root
;
488 * Some of the paths we test assume we have a filled out fs_info, so we
489 * just need to add the root in there so we don't panic.
491 root
->fs_info
->tree_root
= root
;
492 root
->fs_info
->quota_root
= root
;
493 set_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
);
496 * Can't use bytenr 0, some things freak out
497 * *cough*backref walking code*cough*
499 root
->node
= alloc_test_extent_buffer(root
->fs_info
, nodesize
);
501 test_msg("Couldn't allocate dummy buffer\n");
505 btrfs_set_header_level(root
->node
, 0);
506 btrfs_set_header_nritems(root
->node
, 0);
507 root
->alloc_bytenr
+= 2 * nodesize
;
509 tmp_root
= btrfs_alloc_dummy_root(fs_info
);
510 if (IS_ERR(tmp_root
)) {
511 test_msg("Couldn't allocate a fs root\n");
512 ret
= PTR_ERR(tmp_root
);
516 tmp_root
->root_key
.objectid
= BTRFS_FS_TREE_OBJECTID
;
517 root
->fs_info
->fs_root
= tmp_root
;
518 ret
= btrfs_insert_fs_root(root
->fs_info
, tmp_root
);
520 test_msg("Couldn't insert fs root %d\n", ret
);
524 tmp_root
= btrfs_alloc_dummy_root(fs_info
);
525 if (IS_ERR(tmp_root
)) {
526 test_msg("Couldn't allocate a fs root\n");
527 ret
= PTR_ERR(tmp_root
);
531 tmp_root
->root_key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
532 ret
= btrfs_insert_fs_root(root
->fs_info
, tmp_root
);
534 test_msg("Couldn't insert fs root %d\n", ret
);
538 test_msg("Running qgroup tests\n");
539 ret
= test_no_shared_qgroup(root
, sectorsize
, nodesize
);
542 ret
= test_multiple_refs(root
, sectorsize
, nodesize
);
544 btrfs_free_dummy_root(root
);
545 btrfs_free_dummy_fs_info(fs_info
);