Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2013 Facebook. All rights reserved.
4 */
5
6#include <linux/types.h>
7#include "btrfs-tests.h"
8#include "../ctree.h"
9#include "../transaction.h"
10#include "../disk-io.h"
11#include "../qgroup.h"
12#include "../backref.h"
13#include "../fs.h"
14#include "../accessors.h"
15
16static int insert_normal_tree_ref(struct btrfs_root *root, u64 bytenr,
17 u64 num_bytes, u64 parent, u64 root_objectid)
18{
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;
25 struct btrfs_key ins;
26 u32 size = sizeof(*item) + sizeof(*iref) + sizeof(*block_info);
27 int ret;
28
29 btrfs_init_dummy_trans(&trans, NULL);
30
31 ins.objectid = bytenr;
32 ins.type = BTRFS_EXTENT_ITEM_KEY;
33 ins.offset = num_bytes;
34
35 path = btrfs_alloc_path();
36 if (!path) {
37 test_std_err(TEST_ALLOC_ROOT);
38 return -ENOMEM;
39 }
40
41 ret = btrfs_insert_empty_item(&trans, root, path, &ins, size);
42 if (ret) {
43 test_err("couldn't insert ref %d", ret);
44 btrfs_free_path(path);
45 return ret;
46 }
47
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);
56 if (parent > 0) {
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);
60 } else {
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);
63 }
64 btrfs_free_path(path);
65 return 0;
66}
67
68static int add_tree_ref(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
69 u64 parent, u64 root_objectid)
70{
71 struct btrfs_trans_handle trans;
72 struct btrfs_extent_item *item;
73 struct btrfs_path *path;
74 struct btrfs_key key;
75 u64 refs;
76 int ret;
77
78 btrfs_init_dummy_trans(&trans, NULL);
79
80 key.objectid = bytenr;
81 key.type = BTRFS_EXTENT_ITEM_KEY;
82 key.offset = num_bytes;
83
84 path = btrfs_alloc_path();
85 if (!path) {
86 test_std_err(TEST_ALLOC_ROOT);
87 return -ENOMEM;
88 }
89
90 ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
91 if (ret) {
92 test_err("couldn't find extent ref");
93 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)
114 test_err("failed to insert backref");
115 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
127 btrfs_init_dummy_trans(&trans, NULL);
128
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) {
135 test_std_err(TEST_ALLOC_ROOT);
136 return -ENOMEM;
137 }
138
139 ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
140 if (ret) {
141 test_err("didn't find our key %d", ret);
142 btrfs_free_path(path);
143 return ret;
144 }
145 btrfs_del_item(&trans, root, path);
146 btrfs_free_path(path);
147 return 0;
148}
149
150static int remove_extent_ref(struct btrfs_root *root, u64 bytenr,
151 u64 num_bytes, u64 parent, u64 root_objectid)
152{
153 struct btrfs_trans_handle trans;
154 struct btrfs_extent_item *item;
155 struct btrfs_path *path;
156 struct btrfs_key key;
157 u64 refs;
158 int ret;
159
160 btrfs_init_dummy_trans(&trans, NULL);
161
162 key.objectid = bytenr;
163 key.type = BTRFS_EXTENT_ITEM_KEY;
164 key.offset = num_bytes;
165
166 path = btrfs_alloc_path();
167 if (!path) {
168 test_std_err(TEST_ALLOC_ROOT);
169 return -ENOMEM;
170 }
171
172 ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
173 if (ret) {
174 test_err("couldn't find extent ref");
175 btrfs_free_path(path);
176 return ret;
177 }
178
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);
184
185 key.objectid = bytenr;
186 if (parent) {
187 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
188 key.offset = parent;
189 } else {
190 key.type = BTRFS_TREE_BLOCK_REF_KEY;
191 key.offset = root_objectid;
192 }
193
194 ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
195 if (ret) {
196 test_err("couldn't find backref %d", ret);
197 btrfs_free_path(path);
198 return ret;
199 }
200 btrfs_del_item(&trans, root, path);
201 btrfs_free_path(path);
202 return ret;
203}
204
205static int test_no_shared_qgroup(struct btrfs_root *root,
206 u32 sectorsize, u32 nodesize)
207{
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;
213 int ret;
214
215 btrfs_init_dummy_trans(&trans, fs_info);
216
217 test_msg("running qgroup add/remove tests");
218 ret = btrfs_create_qgroup(&trans, BTRFS_FS_TREE_OBJECTID);
219 if (ret) {
220 test_err("couldn't create a qgroup %d", ret);
221 return ret;
222 }
223
224 ctx.bytenr = nodesize;
225 ctx.trans = &trans;
226 ctx.fs_info = fs_info;
227
228 /*
229 * Since the test trans doesn't have the complicated delayed refs,
230 * we can only call btrfs_qgroup_account_extent() directly to test
231 * quota.
232 */
233 ret = btrfs_find_all_roots(&ctx, false);
234 if (ret) {
235 test_err("couldn't find old roots: %d", ret);
236 return ret;
237 }
238 old_roots = ctx.roots;
239 ctx.roots = NULL;
240
241 ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
242 BTRFS_FS_TREE_OBJECTID);
243 if (ret) {
244 ulist_free(old_roots);
245 return ret;
246 }
247
248 ret = btrfs_find_all_roots(&ctx, false);
249 if (ret) {
250 ulist_free(old_roots);
251 test_err("couldn't find old roots: %d", ret);
252 return ret;
253 }
254 new_roots = ctx.roots;
255 ctx.roots = NULL;
256
257 ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
258 new_roots);
259 if (ret) {
260 test_err("couldn't account space for a qgroup %d", ret);
261 return ret;
262 }
263
264 /* btrfs_qgroup_account_extent() always frees the ulists passed to it. */
265 old_roots = NULL;
266 new_roots = NULL;
267
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");
271 return -EINVAL;
272 }
273
274 ret = btrfs_find_all_roots(&ctx, false);
275 if (ret) {
276 test_err("couldn't find old roots: %d", ret);
277 return ret;
278 }
279 old_roots = ctx.roots;
280 ctx.roots = NULL;
281
282 ret = remove_extent_item(root, nodesize, nodesize);
283 if (ret) {
284 ulist_free(old_roots);
285 return -EINVAL;
286 }
287
288 ret = btrfs_find_all_roots(&ctx, false);
289 if (ret) {
290 ulist_free(old_roots);
291 test_err("couldn't find old roots: %d", ret);
292 return ret;
293 }
294 new_roots = ctx.roots;
295 ctx.roots = NULL;
296
297 ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
298 new_roots);
299 if (ret) {
300 test_err("couldn't account space for a qgroup %d", ret);
301 return -EINVAL;
302 }
303
304 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
305 test_err("qgroup counts didn't match expected values");
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 */
317static int test_multiple_refs(struct btrfs_root *root,
318 u32 sectorsize, u32 nodesize)
319{
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;
325 int ret;
326
327 btrfs_init_dummy_trans(&trans, fs_info);
328
329 test_msg("running qgroup multiple refs test");
330
331 /*
332 * We have BTRFS_FS_TREE_OBJECTID created already from the
333 * previous test.
334 */
335 ret = btrfs_create_qgroup(&trans, BTRFS_FIRST_FREE_OBJECTID);
336 if (ret) {
337 test_err("couldn't create a qgroup %d", ret);
338 return ret;
339 }
340
341 ctx.bytenr = nodesize;
342 ctx.trans = &trans;
343 ctx.fs_info = fs_info;
344
345 ret = btrfs_find_all_roots(&ctx, false);
346 if (ret) {
347 test_err("couldn't find old roots: %d", ret);
348 return ret;
349 }
350 old_roots = ctx.roots;
351 ctx.roots = NULL;
352
353 ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
354 BTRFS_FS_TREE_OBJECTID);
355 if (ret) {
356 ulist_free(old_roots);
357 return ret;
358 }
359
360 ret = btrfs_find_all_roots(&ctx, false);
361 if (ret) {
362 ulist_free(old_roots);
363 test_err("couldn't find old roots: %d", ret);
364 return ret;
365 }
366 new_roots = ctx.roots;
367 ctx.roots = NULL;
368
369 ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
370 new_roots);
371 if (ret) {
372 test_err("couldn't account space for a qgroup %d", ret);
373 return ret;
374 }
375
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");
379 return -EINVAL;
380 }
381
382 ret = btrfs_find_all_roots(&ctx, false);
383 if (ret) {
384 test_err("couldn't find old roots: %d", ret);
385 return ret;
386 }
387 old_roots = ctx.roots;
388 ctx.roots = NULL;
389
390 ret = add_tree_ref(root, nodesize, nodesize, 0,
391 BTRFS_FIRST_FREE_OBJECTID);
392 if (ret) {
393 ulist_free(old_roots);
394 return ret;
395 }
396
397 ret = btrfs_find_all_roots(&ctx, false);
398 if (ret) {
399 ulist_free(old_roots);
400 test_err("couldn't find old roots: %d", ret);
401 return ret;
402 }
403 new_roots = ctx.roots;
404 ctx.roots = NULL;
405
406 ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
407 new_roots);
408 if (ret) {
409 test_err("couldn't account space for a qgroup %d", ret);
410 return ret;
411 }
412
413 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
414 nodesize, 0)) {
415 test_err("qgroup counts didn't match expected values");
416 return -EINVAL;
417 }
418
419 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
420 nodesize, 0)) {
421 test_err("qgroup counts didn't match expected values");
422 return -EINVAL;
423 }
424
425 ret = btrfs_find_all_roots(&ctx, false);
426 if (ret) {
427 test_err("couldn't find old roots: %d", ret);
428 return ret;
429 }
430 old_roots = ctx.roots;
431 ctx.roots = NULL;
432
433 ret = remove_extent_ref(root, nodesize, nodesize, 0,
434 BTRFS_FIRST_FREE_OBJECTID);
435 if (ret) {
436 ulist_free(old_roots);
437 return ret;
438 }
439
440 ret = btrfs_find_all_roots(&ctx, false);
441 if (ret) {
442 ulist_free(old_roots);
443 test_err("couldn't find old roots: %d", ret);
444 return ret;
445 }
446 new_roots = ctx.roots;
447 ctx.roots = NULL;
448
449 ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
450 new_roots);
451 if (ret) {
452 test_err("couldn't account space for a qgroup %d", ret);
453 return ret;
454 }
455
456 if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
457 0, 0)) {
458 test_err("qgroup counts didn't match expected values");
459 return -EINVAL;
460 }
461
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");
465 return -EINVAL;
466 }
467
468 return 0;
469}
470
471int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
472{
473 struct btrfs_fs_info *fs_info = NULL;
474 struct btrfs_root *root;
475 struct btrfs_root *tmp_root;
476 int ret = 0;
477
478 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
479 if (!fs_info) {
480 test_std_err(TEST_ALLOC_FS_INFO);
481 return -ENOMEM;
482 }
483
484 root = btrfs_alloc_dummy_root(fs_info);
485 if (IS_ERR(root)) {
486 test_std_err(TEST_ALLOC_ROOT);
487 ret = PTR_ERR(root);
488 goto out;
489 }
490
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);
496
497 /*
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.
500 */
501 root->fs_info->tree_root = root;
502 root->fs_info->quota_root = root;
503 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
504
505 /*
506 * Can't use bytenr 0, some things freak out
507 * *cough*backref walking code*cough*
508 */
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);
513 goto out;
514 }
515 btrfs_set_header_level(root->node, 0);
516 btrfs_set_header_nritems(root->node, 0);
517 root->alloc_bytenr += 2 * nodesize;
518
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);
523 goto out;
524 }
525
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);
529 if (ret) {
530 test_err("couldn't insert fs root %d", ret);
531 goto out;
532 }
533 btrfs_put_root(tmp_root);
534
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);
539 goto out;
540 }
541
542 tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID;
543 ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
544 if (ret) {
545 test_err("couldn't insert fs root %d", ret);
546 goto out;
547 }
548 btrfs_put_root(tmp_root);
549
550 test_msg("running qgroup tests");
551 ret = test_no_shared_qgroup(root, sectorsize, nodesize);
552 if (ret)
553 goto out;
554 ret = test_multiple_refs(root, sectorsize, nodesize);
555out:
556 btrfs_free_dummy_root(root);
557 btrfs_free_dummy_fs_info(fs_info);
558 return ret;
559}
1/*
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
19#include "btrfs-tests.h"
20#include "../ctree.h"
21#include "../transaction.h"
22#include "../disk-io.h"
23#include "../qgroup.h"
24#include "../backref.h"
25
26static int insert_normal_tree_ref(struct btrfs_root *root, u64 bytenr,
27 u64 num_bytes, u64 parent, u64 root_objectid)
28{
29 struct btrfs_trans_handle trans;
30 struct btrfs_extent_item *item;
31 struct btrfs_extent_inline_ref *iref;
32 struct btrfs_tree_block_info *block_info;
33 struct btrfs_path *path;
34 struct extent_buffer *leaf;
35 struct btrfs_key ins;
36 u32 size = sizeof(*item) + sizeof(*iref) + sizeof(*block_info);
37 int ret;
38
39 btrfs_init_dummy_trans(&trans);
40
41 ins.objectid = bytenr;
42 ins.type = BTRFS_EXTENT_ITEM_KEY;
43 ins.offset = num_bytes;
44
45 path = btrfs_alloc_path();
46 if (!path) {
47 test_msg("Couldn't allocate path\n");
48 return -ENOMEM;
49 }
50
51 path->leave_spinning = 1;
52 ret = btrfs_insert_empty_item(&trans, root, path, &ins, size);
53 if (ret) {
54 test_msg("Couldn't insert ref %d\n", ret);
55 btrfs_free_path(path);
56 return ret;
57 }
58
59 leaf = path->nodes[0];
60 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
61 btrfs_set_extent_refs(leaf, item, 1);
62 btrfs_set_extent_generation(leaf, item, 1);
63 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_TREE_BLOCK);
64 block_info = (struct btrfs_tree_block_info *)(item + 1);
65 btrfs_set_tree_block_level(leaf, block_info, 1);
66 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
67 if (parent > 0) {
68 btrfs_set_extent_inline_ref_type(leaf, iref,
69 BTRFS_SHARED_BLOCK_REF_KEY);
70 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
71 } else {
72 btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
73 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
74 }
75 btrfs_free_path(path);
76 return 0;
77}
78
79static int add_tree_ref(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
80 u64 parent, u64 root_objectid)
81{
82 struct btrfs_trans_handle trans;
83 struct btrfs_extent_item *item;
84 struct btrfs_path *path;
85 struct btrfs_key key;
86 u64 refs;
87 int ret;
88
89 btrfs_init_dummy_trans(&trans);
90
91 key.objectid = bytenr;
92 key.type = BTRFS_EXTENT_ITEM_KEY;
93 key.offset = num_bytes;
94
95 path = btrfs_alloc_path();
96 if (!path) {
97 test_msg("Couldn't allocate path\n");
98 return -ENOMEM;
99 }
100
101 path->leave_spinning = 1;
102 ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
103 if (ret) {
104 test_msg("Couldn't find extent ref\n");
105 btrfs_free_path(path);
106 return ret;
107 }
108
109 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
110 struct btrfs_extent_item);
111 refs = btrfs_extent_refs(path->nodes[0], item);
112 btrfs_set_extent_refs(path->nodes[0], item, refs + 1);
113 btrfs_release_path(path);
114
115 key.objectid = bytenr;
116 if (parent) {
117 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
118 key.offset = parent;
119 } else {
120 key.type = BTRFS_TREE_BLOCK_REF_KEY;
121 key.offset = root_objectid;
122 }
123
124 ret = btrfs_insert_empty_item(&trans, root, path, &key, 0);
125 if (ret)
126 test_msg("Failed to insert backref\n");
127 btrfs_free_path(path);
128 return ret;
129}
130
131static int remove_extent_item(struct btrfs_root *root, u64 bytenr,
132 u64 num_bytes)
133{
134 struct btrfs_trans_handle trans;
135 struct btrfs_key key;
136 struct btrfs_path *path;
137 int ret;
138
139 btrfs_init_dummy_trans(&trans);
140
141 key.objectid = bytenr;
142 key.type = BTRFS_EXTENT_ITEM_KEY;
143 key.offset = num_bytes;
144
145 path = btrfs_alloc_path();
146 if (!path) {
147 test_msg("Couldn't allocate path\n");
148 return -ENOMEM;
149 }
150 path->leave_spinning = 1;
151
152 ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
153 if (ret) {
154 test_msg("Didn't find our key %d\n", ret);
155 btrfs_free_path(path);
156 return ret;
157 }
158 btrfs_del_item(&trans, root, path);
159 btrfs_free_path(path);
160 return 0;
161}
162
163static int remove_extent_ref(struct btrfs_root *root, u64 bytenr,
164 u64 num_bytes, u64 parent, u64 root_objectid)
165{
166 struct btrfs_trans_handle trans;
167 struct btrfs_extent_item *item;
168 struct btrfs_path *path;
169 struct btrfs_key key;
170 u64 refs;
171 int ret;
172
173 btrfs_init_dummy_trans(&trans);
174
175 key.objectid = bytenr;
176 key.type = BTRFS_EXTENT_ITEM_KEY;
177 key.offset = num_bytes;
178
179 path = btrfs_alloc_path();
180 if (!path) {
181 test_msg("Couldn't allocate path\n");
182 return -ENOMEM;
183 }
184
185 path->leave_spinning = 1;
186 ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
187 if (ret) {
188 test_msg("Couldn't find extent ref\n");
189 btrfs_free_path(path);
190 return ret;
191 }
192
193 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
194 struct btrfs_extent_item);
195 refs = btrfs_extent_refs(path->nodes[0], item);
196 btrfs_set_extent_refs(path->nodes[0], item, refs - 1);
197 btrfs_release_path(path);
198
199 key.objectid = bytenr;
200 if (parent) {
201 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
202 key.offset = parent;
203 } else {
204 key.type = BTRFS_TREE_BLOCK_REF_KEY;
205 key.offset = root_objectid;
206 }
207
208 ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
209 if (ret) {
210 test_msg("Couldn't find backref %d\n", ret);
211 btrfs_free_path(path);
212 return ret;
213 }
214 btrfs_del_item(&trans, root, path);
215 btrfs_free_path(path);
216 return ret;
217}
218
219static int test_no_shared_qgroup(struct btrfs_root *root)
220{
221 struct btrfs_trans_handle trans;
222 struct btrfs_fs_info *fs_info = root->fs_info;
223 struct ulist *old_roots = NULL;
224 struct ulist *new_roots = NULL;
225 int ret;
226
227 btrfs_init_dummy_trans(&trans);
228
229 test_msg("Qgroup basic add\n");
230 ret = btrfs_create_qgroup(NULL, fs_info, 5);
231 if (ret) {
232 test_msg("Couldn't create a qgroup %d\n", ret);
233 return ret;
234 }
235
236 /*
237 * Since the test trans doesn't havee the complicated delayed refs,
238 * we can only call btrfs_qgroup_account_extent() directly to test
239 * quota.
240 */
241 ret = btrfs_find_all_roots(&trans, fs_info, 4096, 0, &old_roots);
242 if (ret) {
243 ulist_free(old_roots);
244 test_msg("Couldn't find old roots: %d\n", ret);
245 return ret;
246 }
247
248 ret = insert_normal_tree_ref(root, 4096, 4096, 0, 5);
249 if (ret)
250 return ret;
251
252 ret = btrfs_find_all_roots(&trans, fs_info, 4096, 0, &new_roots);
253 if (ret) {
254 ulist_free(old_roots);
255 ulist_free(new_roots);
256 test_msg("Couldn't find old roots: %d\n", ret);
257 return ret;
258 }
259
260 ret = btrfs_qgroup_account_extent(&trans, fs_info, 4096, 4096,
261 old_roots, new_roots);
262 if (ret) {
263 test_msg("Couldn't account space for a qgroup %d\n", ret);
264 return ret;
265 }
266
267 if (btrfs_verify_qgroup_counts(fs_info, 5, 4096, 4096)) {
268 test_msg("Qgroup counts didn't match expected values\n");
269 return -EINVAL;
270 }
271 old_roots = NULL;
272 new_roots = NULL;
273
274 ret = btrfs_find_all_roots(&trans, fs_info, 4096, 0, &old_roots);
275 if (ret) {
276 ulist_free(old_roots);
277 test_msg("Couldn't find old roots: %d\n", ret);
278 return ret;
279 }
280
281 ret = remove_extent_item(root, 4096, 4096);
282 if (ret)
283 return -EINVAL;
284
285 ret = btrfs_find_all_roots(&trans, fs_info, 4096, 0, &new_roots);
286 if (ret) {
287 ulist_free(old_roots);
288 ulist_free(new_roots);
289 test_msg("Couldn't find old roots: %d\n", ret);
290 return ret;
291 }
292
293 ret = btrfs_qgroup_account_extent(&trans, fs_info, 4096, 4096,
294 old_roots, new_roots);
295 if (ret) {
296 test_msg("Couldn't account space for a qgroup %d\n", ret);
297 return -EINVAL;
298 }
299
300 if (btrfs_verify_qgroup_counts(fs_info, 5, 0, 0)) {
301 test_msg("Qgroup counts didn't match expected values\n");
302 return -EINVAL;
303 }
304
305 return 0;
306}
307
308/*
309 * Add a ref for two different roots to make sure the shared value comes out
310 * right, also remove one of the roots and make sure the exclusive count is
311 * adjusted properly.
312 */
313static int test_multiple_refs(struct btrfs_root *root)
314{
315 struct btrfs_trans_handle trans;
316 struct btrfs_fs_info *fs_info = root->fs_info;
317 struct ulist *old_roots = NULL;
318 struct ulist *new_roots = NULL;
319 int ret;
320
321 btrfs_init_dummy_trans(&trans);
322
323 test_msg("Qgroup multiple refs test\n");
324
325 /* We have 5 created already from the previous test */
326 ret = btrfs_create_qgroup(NULL, fs_info, 256);
327 if (ret) {
328 test_msg("Couldn't create a qgroup %d\n", ret);
329 return ret;
330 }
331
332 ret = btrfs_find_all_roots(&trans, fs_info, 4096, 0, &old_roots);
333 if (ret) {
334 ulist_free(old_roots);
335 test_msg("Couldn't find old roots: %d\n", ret);
336 return ret;
337 }
338
339 ret = insert_normal_tree_ref(root, 4096, 4096, 0, 5);
340 if (ret)
341 return ret;
342
343 ret = btrfs_find_all_roots(&trans, fs_info, 4096, 0, &new_roots);
344 if (ret) {
345 ulist_free(old_roots);
346 ulist_free(new_roots);
347 test_msg("Couldn't find old roots: %d\n", ret);
348 return ret;
349 }
350
351 ret = btrfs_qgroup_account_extent(&trans, fs_info, 4096, 4096,
352 old_roots, new_roots);
353 if (ret) {
354 test_msg("Couldn't account space for a qgroup %d\n", ret);
355 return ret;
356 }
357
358 if (btrfs_verify_qgroup_counts(fs_info, 5, 4096, 4096)) {
359 test_msg("Qgroup counts didn't match expected values\n");
360 return -EINVAL;
361 }
362
363 ret = btrfs_find_all_roots(&trans, fs_info, 4096, 0, &old_roots);
364 if (ret) {
365 ulist_free(old_roots);
366 test_msg("Couldn't find old roots: %d\n", ret);
367 return ret;
368 }
369
370 ret = add_tree_ref(root, 4096, 4096, 0, 256);
371 if (ret)
372 return ret;
373
374 ret = btrfs_find_all_roots(&trans, fs_info, 4096, 0, &new_roots);
375 if (ret) {
376 ulist_free(old_roots);
377 ulist_free(new_roots);
378 test_msg("Couldn't find old roots: %d\n", ret);
379 return ret;
380 }
381
382 ret = btrfs_qgroup_account_extent(&trans, fs_info, 4096, 4096,
383 old_roots, new_roots);
384 if (ret) {
385 test_msg("Couldn't account space for a qgroup %d\n", ret);
386 return ret;
387 }
388
389 if (btrfs_verify_qgroup_counts(fs_info, 5, 4096, 0)) {
390 test_msg("Qgroup counts didn't match expected values\n");
391 return -EINVAL;
392 }
393
394 if (btrfs_verify_qgroup_counts(fs_info, 256, 4096, 0)) {
395 test_msg("Qgroup counts didn't match expected values\n");
396 return -EINVAL;
397 }
398
399 ret = btrfs_find_all_roots(&trans, fs_info, 4096, 0, &old_roots);
400 if (ret) {
401 ulist_free(old_roots);
402 test_msg("Couldn't find old roots: %d\n", ret);
403 return ret;
404 }
405
406 ret = remove_extent_ref(root, 4096, 4096, 0, 256);
407 if (ret)
408 return ret;
409
410 ret = btrfs_find_all_roots(&trans, fs_info, 4096, 0, &new_roots);
411 if (ret) {
412 ulist_free(old_roots);
413 ulist_free(new_roots);
414 test_msg("Couldn't find old roots: %d\n", ret);
415 return ret;
416 }
417
418 ret = btrfs_qgroup_account_extent(&trans, fs_info, 4096, 4096,
419 old_roots, new_roots);
420 if (ret) {
421 test_msg("Couldn't account space for a qgroup %d\n", ret);
422 return ret;
423 }
424
425 if (btrfs_verify_qgroup_counts(fs_info, 256, 0, 0)) {
426 test_msg("Qgroup counts didn't match expected values\n");
427 return -EINVAL;
428 }
429
430 if (btrfs_verify_qgroup_counts(fs_info, 5, 4096, 4096)) {
431 test_msg("Qgroup counts didn't match expected values\n");
432 return -EINVAL;
433 }
434
435 return 0;
436}
437
438int btrfs_test_qgroups(void)
439{
440 struct btrfs_root *root;
441 struct btrfs_root *tmp_root;
442 int ret = 0;
443
444 root = btrfs_alloc_dummy_root();
445 if (IS_ERR(root)) {
446 test_msg("Couldn't allocate root\n");
447 return PTR_ERR(root);
448 }
449
450 root->fs_info = btrfs_alloc_dummy_fs_info();
451 if (!root->fs_info) {
452 test_msg("Couldn't allocate dummy fs info\n");
453 ret = -ENOMEM;
454 goto out;
455 }
456 /* We are using this root as our extent root */
457 root->fs_info->extent_root = root;
458
459 /*
460 * Some of the paths we test assume we have a filled out fs_info, so we
461 * just need to add the root in there so we don't panic.
462 */
463 root->fs_info->tree_root = root;
464 root->fs_info->quota_root = root;
465 root->fs_info->quota_enabled = 1;
466
467 /*
468 * Can't use bytenr 0, some things freak out
469 * *cough*backref walking code*cough*
470 */
471 root->node = alloc_test_extent_buffer(root->fs_info, 4096);
472 if (!root->node) {
473 test_msg("Couldn't allocate dummy buffer\n");
474 ret = -ENOMEM;
475 goto out;
476 }
477 btrfs_set_header_level(root->node, 0);
478 btrfs_set_header_nritems(root->node, 0);
479 root->alloc_bytenr += 8192;
480
481 tmp_root = btrfs_alloc_dummy_root();
482 if (IS_ERR(tmp_root)) {
483 test_msg("Couldn't allocate a fs root\n");
484 ret = PTR_ERR(tmp_root);
485 goto out;
486 }
487
488 tmp_root->root_key.objectid = 5;
489 root->fs_info->fs_root = tmp_root;
490 ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
491 if (ret) {
492 test_msg("Couldn't insert fs root %d\n", ret);
493 goto out;
494 }
495
496 tmp_root = btrfs_alloc_dummy_root();
497 if (IS_ERR(tmp_root)) {
498 test_msg("Couldn't allocate a fs root\n");
499 ret = PTR_ERR(tmp_root);
500 goto out;
501 }
502
503 tmp_root->root_key.objectid = 256;
504 ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
505 if (ret) {
506 test_msg("Couldn't insert fs root %d\n", ret);
507 goto out;
508 }
509
510 test_msg("Running qgroup tests\n");
511 ret = test_no_shared_qgroup(root);
512 if (ret)
513 goto out;
514 ret = test_multiple_refs(root);
515out:
516 btrfs_free_dummy_root(root);
517 return ret;
518}