blob: 57a12c0d680ba300461ff5ee9714833b541f2810 [file] [log] [blame]
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001/*
2 * Copyright (C) 2013 Facebook. All rights reserved.
3 *
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.
7 *
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.
12 *
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.
17 */
18
Feifei Xub9ef22d2016-06-01 19:18:25 +080019#include <linux/types.h>
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040020#include "btrfs-tests.h"
21#include "../ctree.h"
22#include "../transaction.h"
23#include "../disk-io.h"
24#include "../qgroup.h"
Qu Wenruo442244c2015-04-16 17:18:36 +080025#include "../backref.h"
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040026
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040027static int insert_normal_tree_ref(struct btrfs_root *root, u64 bytenr,
28 u64 num_bytes, u64 parent, u64 root_objectid)
29{
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;
36 struct btrfs_key ins;
37 u32 size = sizeof(*item) + sizeof(*iref) + sizeof(*block_info);
38 int ret;
39
Omar Sandoval7c55ee02015-09-29 20:50:36 -070040 btrfs_init_dummy_trans(&trans);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040041
42 ins.objectid = bytenr;
43 ins.type = BTRFS_EXTENT_ITEM_KEY;
44 ins.offset = num_bytes;
45
46 path = btrfs_alloc_path();
47 if (!path) {
48 test_msg("Couldn't allocate path\n");
49 return -ENOMEM;
50 }
51
52 path->leave_spinning = 1;
53 ret = btrfs_insert_empty_item(&trans, root, path, &ins, size);
54 if (ret) {
55 test_msg("Couldn't insert ref %d\n", ret);
56 btrfs_free_path(path);
57 return ret;
58 }
59
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);
68 if (parent > 0) {
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);
72 } else {
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);
75 }
76 btrfs_free_path(path);
77 return 0;
78}
79
80static int add_tree_ref(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
81 u64 parent, u64 root_objectid)
82{
83 struct btrfs_trans_handle trans;
84 struct btrfs_extent_item *item;
85 struct btrfs_path *path;
86 struct btrfs_key key;
87 u64 refs;
88 int ret;
89
Omar Sandoval7c55ee02015-09-29 20:50:36 -070090 btrfs_init_dummy_trans(&trans);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040091
92 key.objectid = bytenr;
93 key.type = BTRFS_EXTENT_ITEM_KEY;
94 key.offset = num_bytes;
95
96 path = btrfs_alloc_path();
97 if (!path) {
98 test_msg("Couldn't allocate path\n");
99 return -ENOMEM;
100 }
101
102 path->leave_spinning = 1;
103 ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
104 if (ret) {
105 test_msg("Couldn't find extent ref\n");
106 btrfs_free_path(path);
107 return ret;
108 }
109
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);
115
116 key.objectid = bytenr;
117 if (parent) {
118 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
119 key.offset = parent;
120 } else {
121 key.type = BTRFS_TREE_BLOCK_REF_KEY;
122 key.offset = root_objectid;
123 }
124
125 ret = btrfs_insert_empty_item(&trans, root, path, &key, 0);
126 if (ret)
127 test_msg("Failed to insert backref\n");
128 btrfs_free_path(path);
129 return ret;
130}
131
132static int remove_extent_item(struct btrfs_root *root, u64 bytenr,
133 u64 num_bytes)
134{
135 struct btrfs_trans_handle trans;
136 struct btrfs_key key;
137 struct btrfs_path *path;
138 int ret;
139
Omar Sandoval7c55ee02015-09-29 20:50:36 -0700140 btrfs_init_dummy_trans(&trans);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400141
142 key.objectid = bytenr;
143 key.type = BTRFS_EXTENT_ITEM_KEY;
144 key.offset = num_bytes;
145
146 path = btrfs_alloc_path();
147 if (!path) {
148 test_msg("Couldn't allocate path\n");
149 return -ENOMEM;
150 }
151 path->leave_spinning = 1;
152
153 ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
154 if (ret) {
155 test_msg("Didn't find our key %d\n", ret);
156 btrfs_free_path(path);
157 return ret;
158 }
159 btrfs_del_item(&trans, root, path);
160 btrfs_free_path(path);
161 return 0;
162}
163
164static int remove_extent_ref(struct btrfs_root *root, u64 bytenr,
165 u64 num_bytes, u64 parent, u64 root_objectid)
166{
167 struct btrfs_trans_handle trans;
168 struct btrfs_extent_item *item;
169 struct btrfs_path *path;
170 struct btrfs_key key;
171 u64 refs;
172 int ret;
173
Omar Sandoval7c55ee02015-09-29 20:50:36 -0700174 btrfs_init_dummy_trans(&trans);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400175
176 key.objectid = bytenr;
177 key.type = BTRFS_EXTENT_ITEM_KEY;
178 key.offset = num_bytes;
179
180 path = btrfs_alloc_path();
181 if (!path) {
182 test_msg("Couldn't allocate path\n");
183 return -ENOMEM;
184 }
185
186 path->leave_spinning = 1;
187 ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
188 if (ret) {
189 test_msg("Couldn't find extent ref\n");
190 btrfs_free_path(path);
191 return ret;
192 }
193
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);
199
200 key.objectid = bytenr;
201 if (parent) {
202 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
203 key.offset = parent;
204 } else {
205 key.type = BTRFS_TREE_BLOCK_REF_KEY;
206 key.offset = root_objectid;
207 }
208
209 ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
210 if (ret) {
211 test_msg("Couldn't find backref %d\n", ret);
212 btrfs_free_path(path);
213 return ret;
214 }
215 btrfs_del_item(&trans, root, path);
216 btrfs_free_path(path);
217 return ret;
218}
219
Feifei Xub9ef22d2016-06-01 19:18:25 +0800220static int test_no_shared_qgroup(struct btrfs_root *root,
221 u32 sectorsize, u32 nodesize)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400222{
223 struct btrfs_trans_handle trans;
224 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo442244c2015-04-16 17:18:36 +0800225 struct ulist *old_roots = NULL;
226 struct ulist *new_roots = NULL;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400227 int ret;
228
Omar Sandoval7c55ee02015-09-29 20:50:36 -0700229 btrfs_init_dummy_trans(&trans);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400230
231 test_msg("Qgroup basic add\n");
Feifei Xuef9f2db2016-06-01 19:18:28 +0800232 ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FS_TREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400233 if (ret) {
234 test_msg("Couldn't create a qgroup %d\n", ret);
235 return ret;
236 }
237
Qu Wenruo442244c2015-04-16 17:18:36 +0800238 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400239 * Since the test trans doesn't have the complicated delayed refs,
Qu Wenruo442244c2015-04-16 17:18:36 +0800240 * we can only call btrfs_qgroup_account_extent() directly to test
241 * quota.
242 */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800243 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400244 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800245 ulist_free(old_roots);
246 test_msg("Couldn't find old roots: %d\n", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400247 return ret;
248 }
249
Feifei Xuef9f2db2016-06-01 19:18:28 +0800250 ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
251 BTRFS_FS_TREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400252 if (ret)
253 return ret;
254
Feifei Xub9ef22d2016-06-01 19:18:25 +0800255 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400256 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800257 ulist_free(old_roots);
258 ulist_free(new_roots);
259 test_msg("Couldn't find old roots: %d\n", ret);
260 return ret;
261 }
262
Feifei Xub9ef22d2016-06-01 19:18:25 +0800263 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
264 nodesize, old_roots, new_roots);
Qu Wenruo442244c2015-04-16 17:18:36 +0800265 if (ret) {
266 test_msg("Couldn't account space for a qgroup %d\n", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400267 return ret;
268 }
269
Feifei Xuef9f2db2016-06-01 19:18:28 +0800270 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
271 nodesize, nodesize)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400272 test_msg("Qgroup counts didn't match expected values\n");
273 return -EINVAL;
274 }
Qu Wenruo442244c2015-04-16 17:18:36 +0800275 old_roots = NULL;
276 new_roots = NULL;
277
Feifei Xub9ef22d2016-06-01 19:18:25 +0800278 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
Qu Wenruo442244c2015-04-16 17:18:36 +0800279 if (ret) {
280 ulist_free(old_roots);
281 test_msg("Couldn't find old roots: %d\n", ret);
282 return ret;
283 }
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400284
Feifei Xub9ef22d2016-06-01 19:18:25 +0800285 ret = remove_extent_item(root, nodesize, nodesize);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400286 if (ret)
287 return -EINVAL;
288
Feifei Xub9ef22d2016-06-01 19:18:25 +0800289 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400290 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800291 ulist_free(old_roots);
292 ulist_free(new_roots);
293 test_msg("Couldn't find old roots: %d\n", ret);
294 return ret;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400295 }
296
Feifei Xub9ef22d2016-06-01 19:18:25 +0800297 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
298 nodesize, old_roots, new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400299 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800300 test_msg("Couldn't account space for a qgroup %d\n", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400301 return -EINVAL;
302 }
303
Feifei Xuef9f2db2016-06-01 19:18:28 +0800304 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400305 test_msg("Qgroup counts didn't match expected values\n");
306 return -EINVAL;
307 }
308
309 return 0;
310}
311
312/*
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
315 * adjusted properly.
316 */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800317static int test_multiple_refs(struct btrfs_root *root,
318 u32 sectorsize, u32 nodesize)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400319{
320 struct btrfs_trans_handle trans;
321 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo442244c2015-04-16 17:18:36 +0800322 struct ulist *old_roots = NULL;
323 struct ulist *new_roots = NULL;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400324 int ret;
325
Omar Sandoval7c55ee02015-09-29 20:50:36 -0700326 btrfs_init_dummy_trans(&trans);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400327
328 test_msg("Qgroup multiple refs test\n");
329
Feifei Xuef9f2db2016-06-01 19:18:28 +0800330 /*
331 * We have BTRFS_FS_TREE_OBJECTID created already from the
332 * previous test.
333 */
334 ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400335 if (ret) {
336 test_msg("Couldn't create a qgroup %d\n", ret);
337 return ret;
338 }
339
Feifei Xub9ef22d2016-06-01 19:18:25 +0800340 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
Qu Wenruo442244c2015-04-16 17:18:36 +0800341 if (ret) {
342 ulist_free(old_roots);
343 test_msg("Couldn't find old roots: %d\n", ret);
344 return ret;
345 }
346
Feifei Xuef9f2db2016-06-01 19:18:28 +0800347 ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
348 BTRFS_FS_TREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400349 if (ret)
350 return ret;
351
Feifei Xub9ef22d2016-06-01 19:18:25 +0800352 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400353 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800354 ulist_free(old_roots);
355 ulist_free(new_roots);
356 test_msg("Couldn't find old roots: %d\n", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400357 return ret;
358 }
359
Feifei Xub9ef22d2016-06-01 19:18:25 +0800360 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
361 nodesize, old_roots, new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400362 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800363 test_msg("Couldn't account space for a qgroup %d\n", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400364 return ret;
365 }
366
Feifei Xuef9f2db2016-06-01 19:18:28 +0800367 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
Feifei Xub9ef22d2016-06-01 19:18:25 +0800368 nodesize, nodesize)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400369 test_msg("Qgroup counts didn't match expected values\n");
370 return -EINVAL;
371 }
372
Feifei Xub9ef22d2016-06-01 19:18:25 +0800373 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
Qu Wenruo442244c2015-04-16 17:18:36 +0800374 if (ret) {
375 ulist_free(old_roots);
376 test_msg("Couldn't find old roots: %d\n", ret);
377 return ret;
378 }
379
Feifei Xuef9f2db2016-06-01 19:18:28 +0800380 ret = add_tree_ref(root, nodesize, nodesize, 0,
381 BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400382 if (ret)
383 return ret;
384
Feifei Xub9ef22d2016-06-01 19:18:25 +0800385 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400386 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800387 ulist_free(old_roots);
388 ulist_free(new_roots);
389 test_msg("Couldn't find old roots: %d\n", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400390 return ret;
391 }
392
Feifei Xub9ef22d2016-06-01 19:18:25 +0800393 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
394 nodesize, old_roots, new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400395 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800396 test_msg("Couldn't account space for a qgroup %d\n", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400397 return ret;
398 }
399
Feifei Xuef9f2db2016-06-01 19:18:28 +0800400 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
401 nodesize, 0)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400402 test_msg("Qgroup counts didn't match expected values\n");
403 return -EINVAL;
404 }
405
Feifei Xuef9f2db2016-06-01 19:18:28 +0800406 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
407 nodesize, 0)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400408 test_msg("Qgroup counts didn't match expected values\n");
409 return -EINVAL;
410 }
411
Feifei Xub9ef22d2016-06-01 19:18:25 +0800412 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
Qu Wenruo442244c2015-04-16 17:18:36 +0800413 if (ret) {
414 ulist_free(old_roots);
415 test_msg("Couldn't find old roots: %d\n", ret);
416 return ret;
417 }
418
Feifei Xuef9f2db2016-06-01 19:18:28 +0800419 ret = remove_extent_ref(root, nodesize, nodesize, 0,
420 BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400421 if (ret)
422 return ret;
423
Feifei Xub9ef22d2016-06-01 19:18:25 +0800424 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400425 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800426 ulist_free(old_roots);
427 ulist_free(new_roots);
428 test_msg("Couldn't find old roots: %d\n", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400429 return ret;
430 }
431
Feifei Xub9ef22d2016-06-01 19:18:25 +0800432 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
433 nodesize, old_roots, new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400434 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800435 test_msg("Couldn't account space for a qgroup %d\n", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400436 return ret;
437 }
438
Feifei Xuef9f2db2016-06-01 19:18:28 +0800439 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
440 0, 0)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400441 test_msg("Qgroup counts didn't match expected values\n");
442 return -EINVAL;
443 }
444
Feifei Xuef9f2db2016-06-01 19:18:28 +0800445 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
446 nodesize, nodesize)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400447 test_msg("Qgroup counts didn't match expected values\n");
448 return -EINVAL;
449 }
450
451 return 0;
452}
453
Feifei Xub9ef22d2016-06-01 19:18:25 +0800454int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400455{
456 struct btrfs_root *root;
457 struct btrfs_root *tmp_root;
458 int ret = 0;
459
Feifei Xub9ef22d2016-06-01 19:18:25 +0800460 root = btrfs_alloc_dummy_root(sectorsize, nodesize);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400461 if (IS_ERR(root)) {
462 test_msg("Couldn't allocate root\n");
463 return PTR_ERR(root);
464 }
465
466 root->fs_info = btrfs_alloc_dummy_fs_info();
467 if (!root->fs_info) {
468 test_msg("Couldn't allocate dummy fs info\n");
469 ret = -ENOMEM;
470 goto out;
471 }
David Sterba3f556f72014-06-15 03:20:26 +0200472 /* We are using this root as our extent root */
473 root->fs_info->extent_root = root;
474
475 /*
476 * Some of the paths we test assume we have a filled out fs_info, so we
477 * just need to add the root in there so we don't panic.
478 */
479 root->fs_info->tree_root = root;
480 root->fs_info->quota_root = root;
481 root->fs_info->quota_enabled = 1;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400482
483 /*
484 * Can't use bytenr 0, some things freak out
485 * *cough*backref walking code*cough*
486 */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800487 root->node = alloc_test_extent_buffer(root->fs_info, nodesize,
488 nodesize);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400489 if (!root->node) {
490 test_msg("Couldn't allocate dummy buffer\n");
491 ret = -ENOMEM;
492 goto out;
493 }
Filipe Mananab050f9f2014-06-12 02:47:37 +0100494 btrfs_set_header_level(root->node, 0);
495 btrfs_set_header_nritems(root->node, 0);
Feifei Xub9ef22d2016-06-01 19:18:25 +0800496 root->alloc_bytenr += 2 * nodesize;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400497
Feifei Xub9ef22d2016-06-01 19:18:25 +0800498 tmp_root = btrfs_alloc_dummy_root(sectorsize, nodesize);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400499 if (IS_ERR(tmp_root)) {
500 test_msg("Couldn't allocate a fs root\n");
501 ret = PTR_ERR(tmp_root);
502 goto out;
503 }
504
Feifei Xuef9f2db2016-06-01 19:18:28 +0800505 tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400506 root->fs_info->fs_root = tmp_root;
507 ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
508 if (ret) {
509 test_msg("Couldn't insert fs root %d\n", ret);
510 goto out;
511 }
512
Feifei Xub9ef22d2016-06-01 19:18:25 +0800513 tmp_root = btrfs_alloc_dummy_root(sectorsize, nodesize);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400514 if (IS_ERR(tmp_root)) {
515 test_msg("Couldn't allocate a fs root\n");
516 ret = PTR_ERR(tmp_root);
517 goto out;
518 }
519
Feifei Xuef9f2db2016-06-01 19:18:28 +0800520 tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400521 ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
522 if (ret) {
523 test_msg("Couldn't insert fs root %d\n", ret);
524 goto out;
525 }
526
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400527 test_msg("Running qgroup tests\n");
Feifei Xub9ef22d2016-06-01 19:18:25 +0800528 ret = test_no_shared_qgroup(root, sectorsize, nodesize);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400529 if (ret)
530 goto out;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800531 ret = test_multiple_refs(root, sectorsize, nodesize);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400532out:
533 btrfs_free_dummy_root(root);
534 return ret;
535}