blob: ace94db09d29dc37bc8273e2ec012a2473bec6a1 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04002/*
3 * Copyright (C) 2013 Facebook. All rights reserved.
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004 */
5
Feifei Xub9ef22d2016-06-01 19:18:25 +08006#include <linux/types.h>
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007#include "btrfs-tests.h"
8#include "../ctree.h"
9#include "../transaction.h"
10#include "../disk-io.h"
11#include "../qgroup.h"
Qu Wenruo442244c2015-04-16 17:18:36 +080012#include "../backref.h"
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040013
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040014static int insert_normal_tree_ref(struct btrfs_root *root, u64 bytenr,
15 u64 num_bytes, u64 parent, u64 root_objectid)
16{
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;
23 struct btrfs_key ins;
24 u32 size = sizeof(*item) + sizeof(*iref) + sizeof(*block_info);
25 int ret;
26
Nikolay Borisov483bce02018-05-10 15:44:40 +030027 btrfs_init_dummy_trans(&trans, NULL);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040028
29 ins.objectid = bytenr;
30 ins.type = BTRFS_EXTENT_ITEM_KEY;
31 ins.offset = num_bytes;
32
33 path = btrfs_alloc_path();
34 if (!path) {
David Sterba3c7251f2018-05-17 00:00:42 +020035 test_err("couldn't allocate path");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040036 return -ENOMEM;
37 }
38
39 path->leave_spinning = 1;
40 ret = btrfs_insert_empty_item(&trans, root, path, &ins, size);
41 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +020042 test_err("couldn't insert ref %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040043 btrfs_free_path(path);
44 return ret;
45 }
46
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);
Qu Wenruo3c0efdf2018-03-27 20:44:18 +080053 btrfs_set_tree_block_level(leaf, block_info, 0);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040054 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
55 if (parent > 0) {
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);
59 } else {
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);
62 }
63 btrfs_free_path(path);
64 return 0;
65}
66
67static int add_tree_ref(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
68 u64 parent, u64 root_objectid)
69{
70 struct btrfs_trans_handle trans;
71 struct btrfs_extent_item *item;
72 struct btrfs_path *path;
73 struct btrfs_key key;
74 u64 refs;
75 int ret;
76
Nikolay Borisov483bce02018-05-10 15:44:40 +030077 btrfs_init_dummy_trans(&trans, NULL);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040078
79 key.objectid = bytenr;
80 key.type = BTRFS_EXTENT_ITEM_KEY;
81 key.offset = num_bytes;
82
83 path = btrfs_alloc_path();
84 if (!path) {
David Sterba3c7251f2018-05-17 00:00:42 +020085 test_err("couldn't allocate path");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040086 return -ENOMEM;
87 }
88
89 path->leave_spinning = 1;
90 ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
91 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +020092 test_err("couldn't find extent ref");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040093 btrfs_free_path(path);
94 return ret;
95 }
96
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);
102
103 key.objectid = bytenr;
104 if (parent) {
105 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
106 key.offset = parent;
107 } else {
108 key.type = BTRFS_TREE_BLOCK_REF_KEY;
109 key.offset = root_objectid;
110 }
111
112 ret = btrfs_insert_empty_item(&trans, root, path, &key, 0);
113 if (ret)
David Sterba3c7251f2018-05-17 00:00:42 +0200114 test_err("failed to insert backref");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400115 btrfs_free_path(path);
116 return ret;
117}
118
119static int remove_extent_item(struct btrfs_root *root, u64 bytenr,
120 u64 num_bytes)
121{
122 struct btrfs_trans_handle trans;
123 struct btrfs_key key;
124 struct btrfs_path *path;
125 int ret;
126
Nikolay Borisov483bce02018-05-10 15:44:40 +0300127 btrfs_init_dummy_trans(&trans, NULL);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400128
129 key.objectid = bytenr;
130 key.type = BTRFS_EXTENT_ITEM_KEY;
131 key.offset = num_bytes;
132
133 path = btrfs_alloc_path();
134 if (!path) {
David Sterba3c7251f2018-05-17 00:00:42 +0200135 test_err("couldn't allocate path");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400136 return -ENOMEM;
137 }
138 path->leave_spinning = 1;
139
140 ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
141 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200142 test_err("didn't find our key %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400143 btrfs_free_path(path);
144 return ret;
145 }
146 btrfs_del_item(&trans, root, path);
147 btrfs_free_path(path);
148 return 0;
149}
150
151static int remove_extent_ref(struct btrfs_root *root, u64 bytenr,
152 u64 num_bytes, u64 parent, u64 root_objectid)
153{
154 struct btrfs_trans_handle trans;
155 struct btrfs_extent_item *item;
156 struct btrfs_path *path;
157 struct btrfs_key key;
158 u64 refs;
159 int ret;
160
Nikolay Borisov483bce02018-05-10 15:44:40 +0300161 btrfs_init_dummy_trans(&trans, NULL);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400162
163 key.objectid = bytenr;
164 key.type = BTRFS_EXTENT_ITEM_KEY;
165 key.offset = num_bytes;
166
167 path = btrfs_alloc_path();
168 if (!path) {
David Sterba3c7251f2018-05-17 00:00:42 +0200169 test_err("couldn't allocate path");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400170 return -ENOMEM;
171 }
172
173 path->leave_spinning = 1;
174 ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
175 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200176 test_err("couldn't find extent ref");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400177 btrfs_free_path(path);
178 return ret;
179 }
180
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);
186
187 key.objectid = bytenr;
188 if (parent) {
189 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
190 key.offset = parent;
191 } else {
192 key.type = BTRFS_TREE_BLOCK_REF_KEY;
193 key.offset = root_objectid;
194 }
195
196 ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
197 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200198 test_err("couldn't find backref %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400199 btrfs_free_path(path);
200 return ret;
201 }
202 btrfs_del_item(&trans, root, path);
203 btrfs_free_path(path);
204 return ret;
205}
206
Feifei Xub9ef22d2016-06-01 19:18:25 +0800207static int test_no_shared_qgroup(struct btrfs_root *root,
208 u32 sectorsize, u32 nodesize)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400209{
210 struct btrfs_trans_handle trans;
211 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo442244c2015-04-16 17:18:36 +0800212 struct ulist *old_roots = NULL;
213 struct ulist *new_roots = NULL;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400214 int ret;
215
Nikolay Borisov483bce02018-05-10 15:44:40 +0300216 btrfs_init_dummy_trans(&trans, fs_info);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400217
David Sterba315b76b2018-05-17 00:00:44 +0200218 test_msg("qgroup basic add");
Feifei Xuef9f2db2016-06-01 19:18:28 +0800219 ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FS_TREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400220 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200221 test_err("couldn't create a qgroup %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400222 return ret;
223 }
224
Qu Wenruo442244c2015-04-16 17:18:36 +0800225 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400226 * Since the test trans doesn't have the complicated delayed refs,
Qu Wenruo442244c2015-04-16 17:18:36 +0800227 * we can only call btrfs_qgroup_account_extent() directly to test
228 * quota.
229 */
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400230 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
231 false);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400232 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800233 ulist_free(old_roots);
David Sterba3c7251f2018-05-17 00:00:42 +0200234 test_err("couldn't find old roots: %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400235 return ret;
236 }
237
Feifei Xuef9f2db2016-06-01 19:18:28 +0800238 ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
239 BTRFS_FS_TREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400240 if (ret)
241 return ret;
242
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400243 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
244 false);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400245 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800246 ulist_free(old_roots);
247 ulist_free(new_roots);
David Sterba3c7251f2018-05-17 00:00:42 +0200248 test_err("couldn't find old roots: %d", ret);
Qu Wenruo442244c2015-04-16 17:18:36 +0800249 return ret;
250 }
251
Feifei Xub9ef22d2016-06-01 19:18:25 +0800252 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
253 nodesize, old_roots, new_roots);
Qu Wenruo442244c2015-04-16 17:18:36 +0800254 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200255 test_err("couldn't account space for a qgroup %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400256 return ret;
257 }
258
Feifei Xuef9f2db2016-06-01 19:18:28 +0800259 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
260 nodesize, nodesize)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200261 test_err("qgroup counts didn't match expected values");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400262 return -EINVAL;
263 }
Qu Wenruo442244c2015-04-16 17:18:36 +0800264 old_roots = NULL;
265 new_roots = NULL;
266
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400267 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
268 false);
Qu Wenruo442244c2015-04-16 17:18:36 +0800269 if (ret) {
270 ulist_free(old_roots);
David Sterba3c7251f2018-05-17 00:00:42 +0200271 test_err("couldn't find old roots: %d", ret);
Qu Wenruo442244c2015-04-16 17:18:36 +0800272 return ret;
273 }
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400274
Feifei Xub9ef22d2016-06-01 19:18:25 +0800275 ret = remove_extent_item(root, nodesize, nodesize);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400276 if (ret)
277 return -EINVAL;
278
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400279 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
280 false);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400281 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800282 ulist_free(old_roots);
283 ulist_free(new_roots);
David Sterba3c7251f2018-05-17 00:00:42 +0200284 test_err("couldn't find old roots: %d", ret);
Qu Wenruo442244c2015-04-16 17:18:36 +0800285 return ret;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400286 }
287
Feifei Xub9ef22d2016-06-01 19:18:25 +0800288 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
289 nodesize, old_roots, new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400290 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200291 test_err("couldn't account space for a qgroup %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400292 return -EINVAL;
293 }
294
Feifei Xuef9f2db2016-06-01 19:18:28 +0800295 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200296 test_err("qgroup counts didn't match expected values");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400297 return -EINVAL;
298 }
299
300 return 0;
301}
302
303/*
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
306 * adjusted properly.
307 */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800308static int test_multiple_refs(struct btrfs_root *root,
309 u32 sectorsize, u32 nodesize)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400310{
311 struct btrfs_trans_handle trans;
312 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo442244c2015-04-16 17:18:36 +0800313 struct ulist *old_roots = NULL;
314 struct ulist *new_roots = NULL;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400315 int ret;
316
Nikolay Borisov483bce02018-05-10 15:44:40 +0300317 btrfs_init_dummy_trans(&trans, fs_info);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400318
David Sterba315b76b2018-05-17 00:00:44 +0200319 test_msg("qgroup multiple refs test");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400320
Feifei Xuef9f2db2016-06-01 19:18:28 +0800321 /*
322 * We have BTRFS_FS_TREE_OBJECTID created already from the
323 * previous test.
324 */
325 ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400326 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200327 test_err("couldn't create a qgroup %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400328 return ret;
329 }
330
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400331 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
332 false);
Qu Wenruo442244c2015-04-16 17:18:36 +0800333 if (ret) {
334 ulist_free(old_roots);
David Sterba3c7251f2018-05-17 00:00:42 +0200335 test_err("couldn't find old roots: %d", ret);
Qu Wenruo442244c2015-04-16 17:18:36 +0800336 return ret;
337 }
338
Feifei Xuef9f2db2016-06-01 19:18:28 +0800339 ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
340 BTRFS_FS_TREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400341 if (ret)
342 return ret;
343
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400344 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
345 false);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400346 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800347 ulist_free(old_roots);
348 ulist_free(new_roots);
David Sterba3c7251f2018-05-17 00:00:42 +0200349 test_err("couldn't find old roots: %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400350 return ret;
351 }
352
Feifei Xub9ef22d2016-06-01 19:18:25 +0800353 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
354 nodesize, old_roots, new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400355 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200356 test_err("couldn't account space for a qgroup %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400357 return ret;
358 }
359
Feifei Xuef9f2db2016-06-01 19:18:28 +0800360 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
Feifei Xub9ef22d2016-06-01 19:18:25 +0800361 nodesize, nodesize)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200362 test_err("qgroup counts didn't match expected values");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400363 return -EINVAL;
364 }
365
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400366 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
367 false);
Qu Wenruo442244c2015-04-16 17:18:36 +0800368 if (ret) {
369 ulist_free(old_roots);
David Sterba3c7251f2018-05-17 00:00:42 +0200370 test_err("couldn't find old roots: %d", ret);
Qu Wenruo442244c2015-04-16 17:18:36 +0800371 return ret;
372 }
373
Feifei Xuef9f2db2016-06-01 19:18:28 +0800374 ret = add_tree_ref(root, nodesize, nodesize, 0,
375 BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400376 if (ret)
377 return ret;
378
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400379 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
380 false);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400381 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800382 ulist_free(old_roots);
383 ulist_free(new_roots);
David Sterba3c7251f2018-05-17 00:00:42 +0200384 test_err("couldn't find old roots: %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400385 return ret;
386 }
387
Feifei Xub9ef22d2016-06-01 19:18:25 +0800388 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
389 nodesize, old_roots, new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400390 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200391 test_err("couldn't account space for a qgroup %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400392 return ret;
393 }
394
Feifei Xuef9f2db2016-06-01 19:18:28 +0800395 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
396 nodesize, 0)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200397 test_err("qgroup counts didn't match expected values");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400398 return -EINVAL;
399 }
400
Feifei Xuef9f2db2016-06-01 19:18:28 +0800401 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
402 nodesize, 0)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200403 test_err("qgroup counts didn't match expected values");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400404 return -EINVAL;
405 }
406
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400407 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
408 false);
Qu Wenruo442244c2015-04-16 17:18:36 +0800409 if (ret) {
410 ulist_free(old_roots);
David Sterba3c7251f2018-05-17 00:00:42 +0200411 test_err("couldn't find old roots: %d", ret);
Qu Wenruo442244c2015-04-16 17:18:36 +0800412 return ret;
413 }
414
Feifei Xuef9f2db2016-06-01 19:18:28 +0800415 ret = remove_extent_ref(root, nodesize, nodesize, 0,
416 BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400417 if (ret)
418 return ret;
419
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400420 ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
421 false);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400422 if (ret) {
Qu Wenruo442244c2015-04-16 17:18:36 +0800423 ulist_free(old_roots);
424 ulist_free(new_roots);
David Sterba3c7251f2018-05-17 00:00:42 +0200425 test_err("couldn't find old roots: %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400426 return ret;
427 }
428
Feifei Xub9ef22d2016-06-01 19:18:25 +0800429 ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
430 nodesize, old_roots, new_roots);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400431 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200432 test_err("couldn't account space for a qgroup %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400433 return ret;
434 }
435
Feifei Xuef9f2db2016-06-01 19:18:28 +0800436 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
437 0, 0)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200438 test_err("qgroup counts didn't match expected values");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400439 return -EINVAL;
440 }
441
Feifei Xuef9f2db2016-06-01 19:18:28 +0800442 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
443 nodesize, nodesize)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200444 test_err("qgroup counts didn't match expected values");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400445 return -EINVAL;
446 }
447
448 return 0;
449}
450
Feifei Xub9ef22d2016-06-01 19:18:25 +0800451int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400452{
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400453 struct btrfs_fs_info *fs_info = NULL;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400454 struct btrfs_root *root;
455 struct btrfs_root *tmp_root;
456 int ret = 0;
457
Jeff Mahoneyda170662016-06-15 09:22:56 -0400458 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400459 if (!fs_info) {
David Sterba3c7251f2018-05-17 00:00:42 +0200460 test_err("couldn't allocate dummy fs info");
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400461 return -ENOMEM;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400462 }
463
Jeff Mahoneyda170662016-06-15 09:22:56 -0400464 root = btrfs_alloc_dummy_root(fs_info);
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400465 if (IS_ERR(root)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200466 test_err("couldn't allocate root");
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400467 ret = PTR_ERR(root);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400468 goto out;
469 }
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400470
David Sterba3f556f72014-06-15 03:20:26 +0200471 /* We are using this root as our extent root */
472 root->fs_info->extent_root = root;
473
474 /*
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.
477 */
478 root->fs_info->tree_root = root;
479 root->fs_info->quota_root = root;
Josef Bacikafcdd122016-09-02 15:40:02 -0400480 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400481
482 /*
483 * Can't use bytenr 0, some things freak out
484 * *cough*backref walking code*cough*
485 */
Jeff Mahoneyda170662016-06-15 09:22:56 -0400486 root->node = alloc_test_extent_buffer(root->fs_info, nodesize);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400487 if (!root->node) {
David Sterba3c7251f2018-05-17 00:00:42 +0200488 test_err("couldn't allocate dummy buffer");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400489 ret = -ENOMEM;
490 goto out;
491 }
Filipe Mananab050f9f2014-06-12 02:47:37 +0100492 btrfs_set_header_level(root->node, 0);
493 btrfs_set_header_nritems(root->node, 0);
Feifei Xub9ef22d2016-06-01 19:18:25 +0800494 root->alloc_bytenr += 2 * nodesize;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400495
Jeff Mahoneyda170662016-06-15 09:22:56 -0400496 tmp_root = btrfs_alloc_dummy_root(fs_info);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400497 if (IS_ERR(tmp_root)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200498 test_err("couldn't allocate a fs root");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400499 ret = PTR_ERR(tmp_root);
500 goto out;
501 }
502
Feifei Xuef9f2db2016-06-01 19:18:28 +0800503 tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400504 root->fs_info->fs_root = tmp_root;
505 ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
506 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200507 test_err("couldn't insert fs root %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400508 goto out;
509 }
510
Jeff Mahoneyda170662016-06-15 09:22:56 -0400511 tmp_root = btrfs_alloc_dummy_root(fs_info);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400512 if (IS_ERR(tmp_root)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200513 test_err("couldn't allocate a fs root");
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400514 ret = PTR_ERR(tmp_root);
515 goto out;
516 }
517
Feifei Xuef9f2db2016-06-01 19:18:28 +0800518 tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID;
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400519 ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
520 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200521 test_err("couldn't insert fs root %d", ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400522 goto out;
523 }
524
David Sterba315b76b2018-05-17 00:00:44 +0200525 test_msg("running qgroup tests");
Feifei Xub9ef22d2016-06-01 19:18:25 +0800526 ret = test_no_shared_qgroup(root, sectorsize, nodesize);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400527 if (ret)
528 goto out;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800529 ret = test_multiple_refs(root, sectorsize, nodesize);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400530out:
531 btrfs_free_dummy_root(root);
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400532 btrfs_free_dummy_fs_info(fs_info);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400533 return ret;
534}