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, 0);
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);
245 ulist_free(old_roots);
246 test_msg("Couldn't find old roots: %d\n", ret);
250 ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
251 BTRFS_FS_TREE_OBJECTID);
255 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
257 ulist_free(old_roots);
258 ulist_free(new_roots);
259 test_msg("Couldn't find old roots: %d\n", ret);
263 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
264 nodesize, old_roots, new_roots);
266 test_msg("Couldn't account space for a qgroup %d\n", ret);
270 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
271 nodesize, nodesize)) {
272 test_msg("Qgroup counts didn't match expected values\n");
278 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
280 ulist_free(old_roots);
281 test_msg("Couldn't find old roots: %d\n", ret);
285 ret = remove_extent_item(root, nodesize, nodesize);
289 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
291 ulist_free(old_roots);
292 ulist_free(new_roots);
293 test_msg("Couldn't find old roots: %d\n", ret);
297 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
298 nodesize, old_roots, new_roots);
300 test_msg("Couldn't account space for a qgroup %d\n", ret);
304 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
305 test_msg("Qgroup counts didn't match expected values\n");
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_trans_handle trans;
321 struct btrfs_fs_info *fs_info = root->fs_info;
322 struct ulist *old_roots = NULL;
323 struct ulist *new_roots = NULL;
326 btrfs_init_dummy_trans(&trans);
328 test_msg("Qgroup multiple refs test\n");
331 * We have BTRFS_FS_TREE_OBJECTID created already from the
334 ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FIRST_FREE_OBJECTID);
336 test_msg("Couldn't create a qgroup %d\n", ret);
340 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
342 ulist_free(old_roots);
343 test_msg("Couldn't find old roots: %d\n", ret);
347 ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
348 BTRFS_FS_TREE_OBJECTID);
352 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
354 ulist_free(old_roots);
355 ulist_free(new_roots);
356 test_msg("Couldn't find old roots: %d\n", ret);
360 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
361 nodesize, old_roots, new_roots);
363 test_msg("Couldn't account space for a qgroup %d\n", ret);
367 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
368 nodesize, nodesize)) {
369 test_msg("Qgroup counts didn't match expected values\n");
373 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
375 ulist_free(old_roots);
376 test_msg("Couldn't find old roots: %d\n", ret);
380 ret = add_tree_ref(root, nodesize, nodesize, 0,
381 BTRFS_FIRST_FREE_OBJECTID);
385 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
387 ulist_free(old_roots);
388 ulist_free(new_roots);
389 test_msg("Couldn't find old roots: %d\n", ret);
393 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
394 nodesize, old_roots, new_roots);
396 test_msg("Couldn't account space for a qgroup %d\n", ret);
400 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
402 test_msg("Qgroup counts didn't match expected values\n");
406 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
408 test_msg("Qgroup counts didn't match expected values\n");
412 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
414 ulist_free(old_roots);
415 test_msg("Couldn't find old roots: %d\n", ret);
419 ret = remove_extent_ref(root, nodesize, nodesize, 0,
420 BTRFS_FIRST_FREE_OBJECTID);
424 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
426 ulist_free(old_roots);
427 ulist_free(new_roots);
428 test_msg("Couldn't find old roots: %d\n", ret);
432 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
433 nodesize, old_roots, new_roots);
435 test_msg("Couldn't account space for a qgroup %d\n", ret);
439 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
441 test_msg("Qgroup counts didn't match expected values\n");
445 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
446 nodesize, nodesize)) {
447 test_msg("Qgroup counts didn't match expected values\n");
454 int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
456 struct btrfs_fs_info *fs_info = NULL;
457 struct btrfs_root *root;
458 struct btrfs_root *tmp_root;
461 fs_info = btrfs_alloc_dummy_fs_info();
463 test_msg("Couldn't allocate dummy fs info\n");
467 root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
469 test_msg("Couldn't allocate root\n");
474 /* We are using this root as our extent root */
475 root->fs_info->extent_root = root;
478 * Some of the paths we test assume we have a filled out fs_info, so we
479 * just need to add the root in there so we don't panic.
481 root->fs_info->tree_root = root;
482 root->fs_info->quota_root = root;
483 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
486 * Can't use bytenr 0, some things freak out
487 * *cough*backref walking code*cough*
489 root->node = alloc_test_extent_buffer(root->fs_info, nodesize,
491 if (IS_ERR(root->node)) {
492 test_msg("Couldn't allocate dummy buffer\n");
493 ret = PTR_ERR(root->node);
496 btrfs_set_header_level(root->node, 0);
497 btrfs_set_header_nritems(root->node, 0);
498 root->alloc_bytenr += 2 * nodesize;
500 tmp_root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
501 if (IS_ERR(tmp_root)) {
502 test_msg("Couldn't allocate a fs root\n");
503 ret = PTR_ERR(tmp_root);
507 tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID;
508 root->fs_info->fs_root = tmp_root;
509 ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
511 test_msg("Couldn't insert fs root %d\n", ret);
515 tmp_root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
516 if (IS_ERR(tmp_root)) {
517 test_msg("Couldn't allocate a fs root\n");
518 ret = PTR_ERR(tmp_root);
522 tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID;
523 ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
525 test_msg("Couldn't insert fs root %d\n", ret);
529 test_msg("Running qgroup tests\n");
530 ret = test_no_shared_qgroup(root, sectorsize, nodesize);
533 ret = test_multiple_refs(root, sectorsize, nodesize);
535 btrfs_free_dummy_root(root);
536 btrfs_free_dummy_fs_info(fs_info);