Linux Audio

Check our new training course

Loading...
v5.4
  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
 14static 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
 27	btrfs_init_dummy_trans(&trans, NULL);
 28
 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) {
 35		test_std_err(TEST_ALLOC_ROOT);
 36		return -ENOMEM;
 37	}
 38
 39	path->leave_spinning = 1;
 40	ret = btrfs_insert_empty_item(&trans, root, path, &ins, size);
 41	if (ret) {
 42		test_err("couldn't insert ref %d", ret);
 43		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);
 53	btrfs_set_tree_block_level(leaf, block_info, 0);
 54	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
 77	btrfs_init_dummy_trans(&trans, NULL);
 78
 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) {
 85		test_std_err(TEST_ALLOC_ROOT);
 86		return -ENOMEM;
 87	}
 88
 89	path->leave_spinning = 1;
 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	path->leave_spinning = 1;
139
140	ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
141	if (ret) {
142		test_err("didn't find our key %d", ret);
143		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
161	btrfs_init_dummy_trans(&trans, NULL);
162
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) {
169		test_std_err(TEST_ALLOC_ROOT);
170		return -ENOMEM;
171	}
172
173	path->leave_spinning = 1;
174	ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
175	if (ret) {
176		test_err("couldn't find extent ref");
177		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) {
198		test_err("couldn't find backref %d", ret);
199		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
207static int test_no_shared_qgroup(struct btrfs_root *root,
208		u32 sectorsize, u32 nodesize)
209{
210	struct btrfs_trans_handle trans;
211	struct btrfs_fs_info *fs_info = root->fs_info;
212	struct ulist *old_roots = NULL;
213	struct ulist *new_roots = NULL;
214	int ret;
215
216	btrfs_init_dummy_trans(&trans, fs_info);
217
218	test_msg("running qgroup add/remove tests");
219	ret = btrfs_create_qgroup(&trans, BTRFS_FS_TREE_OBJECTID);
220	if (ret) {
221		test_err("couldn't create a qgroup %d", ret);
222		return ret;
223	}
224
225	/*
226	 * Since the test trans doesn't have the complicated delayed refs,
227	 * we can only call btrfs_qgroup_account_extent() directly to test
228	 * quota.
229	 */
230	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
231			false);
232	if (ret) {
233		ulist_free(old_roots);
234		test_err("couldn't find old roots: %d", ret);
235		return ret;
236	}
237
238	ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
239				BTRFS_FS_TREE_OBJECTID);
240	if (ret)
241		return ret;
242
243	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
244			false);
245	if (ret) {
246		ulist_free(old_roots);
247		ulist_free(new_roots);
248		test_err("couldn't find old roots: %d", ret);
249		return ret;
250	}
251
252	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
253					  new_roots);
254	if (ret) {
255		test_err("couldn't account space for a qgroup %d", ret);
256		return ret;
257	}
258
259	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
260				nodesize, nodesize)) {
261		test_err("qgroup counts didn't match expected values");
262		return -EINVAL;
263	}
264	old_roots = NULL;
265	new_roots = NULL;
266
267	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
268			false);
269	if (ret) {
270		ulist_free(old_roots);
271		test_err("couldn't find old roots: %d", ret);
272		return ret;
273	}
274
275	ret = remove_extent_item(root, nodesize, nodesize);
276	if (ret)
277		return -EINVAL;
278
279	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
280			false);
281	if (ret) {
282		ulist_free(old_roots);
283		ulist_free(new_roots);
284		test_err("couldn't find old roots: %d", ret);
285		return ret;
286	}
287
288	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
289					  new_roots);
290	if (ret) {
291		test_err("couldn't account space for a qgroup %d", ret);
292		return -EINVAL;
293	}
294
295	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
296		test_err("qgroup counts didn't match expected values");
297		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 */
308static int test_multiple_refs(struct btrfs_root *root,
309		u32 sectorsize, u32 nodesize)
310{
311	struct btrfs_trans_handle trans;
312	struct btrfs_fs_info *fs_info = root->fs_info;
313	struct ulist *old_roots = NULL;
314	struct ulist *new_roots = NULL;
315	int ret;
316
317	btrfs_init_dummy_trans(&trans, fs_info);
318
319	test_msg("running qgroup multiple refs test");
320
321	/*
322	 * We have BTRFS_FS_TREE_OBJECTID created already from the
323	 * previous test.
324	 */
325	ret = btrfs_create_qgroup(&trans, BTRFS_FIRST_FREE_OBJECTID);
326	if (ret) {
327		test_err("couldn't create a qgroup %d", ret);
328		return ret;
329	}
330
331	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
332			false);
333	if (ret) {
334		ulist_free(old_roots);
335		test_err("couldn't find old roots: %d", ret);
336		return ret;
337	}
338
339	ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
340				BTRFS_FS_TREE_OBJECTID);
341	if (ret)
342		return ret;
343
344	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
345			false);
346	if (ret) {
347		ulist_free(old_roots);
348		ulist_free(new_roots);
349		test_err("couldn't find old roots: %d", ret);
350		return ret;
351	}
352
353	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
354					  new_roots);
355	if (ret) {
356		test_err("couldn't account space for a qgroup %d", ret);
357		return ret;
358	}
359
360	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
361				       nodesize, nodesize)) {
362		test_err("qgroup counts didn't match expected values");
363		return -EINVAL;
364	}
365
366	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
367			false);
368	if (ret) {
369		ulist_free(old_roots);
370		test_err("couldn't find old roots: %d", ret);
371		return ret;
372	}
373
374	ret = add_tree_ref(root, nodesize, nodesize, 0,
375			BTRFS_FIRST_FREE_OBJECTID);
376	if (ret)
377		return ret;
378
379	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
380			false);
381	if (ret) {
382		ulist_free(old_roots);
383		ulist_free(new_roots);
384		test_err("couldn't find old roots: %d", ret);
385		return ret;
386	}
387
388	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
389					  new_roots);
390	if (ret) {
391		test_err("couldn't account space for a qgroup %d", ret);
392		return ret;
393	}
394
395	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
396					nodesize, 0)) {
397		test_err("qgroup counts didn't match expected values");
398		return -EINVAL;
399	}
400
401	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
402					nodesize, 0)) {
403		test_err("qgroup counts didn't match expected values");
404		return -EINVAL;
405	}
406
407	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
408			false);
409	if (ret) {
410		ulist_free(old_roots);
411		test_err("couldn't find old roots: %d", ret);
412		return ret;
413	}
414
415	ret = remove_extent_ref(root, nodesize, nodesize, 0,
416				BTRFS_FIRST_FREE_OBJECTID);
417	if (ret)
418		return ret;
419
420	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
421			false);
422	if (ret) {
423		ulist_free(old_roots);
424		ulist_free(new_roots);
425		test_err("couldn't find old roots: %d", ret);
426		return ret;
427	}
428
429	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
430					  new_roots);
431	if (ret) {
432		test_err("couldn't account space for a qgroup %d", ret);
433		return ret;
434	}
435
436	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
437					0, 0)) {
438		test_err("qgroup counts didn't match expected values");
439		return -EINVAL;
440	}
441
442	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
443					nodesize, nodesize)) {
444		test_err("qgroup counts didn't match expected values");
445		return -EINVAL;
446	}
447
448	return 0;
449}
450
451int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
452{
453	struct btrfs_fs_info *fs_info = NULL;
454	struct btrfs_root *root;
455	struct btrfs_root *tmp_root;
456	int ret = 0;
457
458	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
459	if (!fs_info) {
460		test_std_err(TEST_ALLOC_FS_INFO);
461		return -ENOMEM;
462	}
463
464	root = btrfs_alloc_dummy_root(fs_info);
465	if (IS_ERR(root)) {
466		test_std_err(TEST_ALLOC_ROOT);
467		ret = PTR_ERR(root);
468		goto out;
469	}
470
471	/* 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;
480	set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
481
482	/*
483	 * Can't use bytenr 0, some things freak out
484	 * *cough*backref walking code*cough*
485	 */
486	root->node = alloc_test_extent_buffer(root->fs_info, nodesize);
487	if (!root->node) {
488		test_err("couldn't allocate dummy buffer");
489		ret = -ENOMEM;
490		goto out;
491	}
492	btrfs_set_header_level(root->node, 0);
493	btrfs_set_header_nritems(root->node, 0);
494	root->alloc_bytenr += 2 * nodesize;
495
496	tmp_root = btrfs_alloc_dummy_root(fs_info);
497	if (IS_ERR(tmp_root)) {
498		test_std_err(TEST_ALLOC_ROOT);
499		ret = PTR_ERR(tmp_root);
500		goto out;
501	}
502
503	tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID;
504	root->fs_info->fs_root = tmp_root;
505	ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
506	if (ret) {
507		test_err("couldn't insert fs root %d", ret);
508		goto out;
509	}
510
511	tmp_root = btrfs_alloc_dummy_root(fs_info);
512	if (IS_ERR(tmp_root)) {
513		test_std_err(TEST_ALLOC_ROOT);
514		ret = PTR_ERR(tmp_root);
515		goto out;
516	}
517
518	tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID;
519	ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
520	if (ret) {
521		test_err("couldn't insert fs root %d", ret);
522		goto out;
523	}
524
525	test_msg("running qgroup tests");
526	ret = test_no_shared_qgroup(root, sectorsize, nodesize);
527	if (ret)
528		goto out;
529	ret = test_multiple_refs(root, sectorsize, nodesize);
530out:
531	btrfs_free_dummy_root(root);
532	btrfs_free_dummy_fs_info(fs_info);
533	return ret;
534}
v4.10.11
 
  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 <linux/types.h>
 20#include "btrfs-tests.h"
 21#include "../ctree.h"
 22#include "../transaction.h"
 23#include "../disk-io.h"
 24#include "../qgroup.h"
 25#include "../backref.h"
 26
 27static 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
 40	btrfs_init_dummy_trans(&trans);
 41
 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
 90	btrfs_init_dummy_trans(&trans);
 91
 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
140	btrfs_init_dummy_trans(&trans);
141
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
174	btrfs_init_dummy_trans(&trans);
175
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
220static int test_no_shared_qgroup(struct btrfs_root *root,
221		u32 sectorsize, u32 nodesize)
222{
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;
227	int ret;
228
229	btrfs_init_dummy_trans(&trans);
230
231	test_msg("Qgroup basic add\n");
232	ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FS_TREE_OBJECTID);
233	if (ret) {
234		test_msg("Couldn't create a qgroup %d\n", ret);
235		return ret;
236	}
237
238	/*
239	 * Since the test trans doesn't have the complicated delayed refs,
240	 * we can only call btrfs_qgroup_account_extent() directly to test
241	 * quota.
242	 */
243	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
 
244	if (ret) {
245		ulist_free(old_roots);
246		test_msg("Couldn't find old roots: %d\n", ret);
247		return ret;
248	}
249
250	ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
251				BTRFS_FS_TREE_OBJECTID);
252	if (ret)
253		return ret;
254
255	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
 
256	if (ret) {
257		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
263	ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
264					  nodesize, old_roots, new_roots);
265	if (ret) {
266		test_msg("Couldn't account space for a qgroup %d\n", ret);
267		return ret;
268	}
269
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");
273		return -EINVAL;
274	}
275	old_roots = NULL;
276	new_roots = NULL;
277
278	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
 
279	if (ret) {
280		ulist_free(old_roots);
281		test_msg("Couldn't find old roots: %d\n", ret);
282		return ret;
283	}
284
285	ret = remove_extent_item(root, nodesize, nodesize);
286	if (ret)
287		return -EINVAL;
288
289	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
 
290	if (ret) {
291		ulist_free(old_roots);
292		ulist_free(new_roots);
293		test_msg("Couldn't find old roots: %d\n", ret);
294		return ret;
295	}
296
297	ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
298					  nodesize, old_roots, new_roots);
299	if (ret) {
300		test_msg("Couldn't account space for a qgroup %d\n", ret);
301		return -EINVAL;
302	}
303
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");
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_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;
324	int ret;
325
326	btrfs_init_dummy_trans(&trans);
327
328	test_msg("Qgroup multiple refs test\n");
329
330	/*
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);
335	if (ret) {
336		test_msg("Couldn't create a qgroup %d\n", ret);
337		return ret;
338	}
339
340	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
 
341	if (ret) {
342		ulist_free(old_roots);
343		test_msg("Couldn't find old roots: %d\n", ret);
344		return ret;
345	}
346
347	ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
348				BTRFS_FS_TREE_OBJECTID);
349	if (ret)
350		return ret;
351
352	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
 
353	if (ret) {
354		ulist_free(old_roots);
355		ulist_free(new_roots);
356		test_msg("Couldn't find old roots: %d\n", ret);
357		return ret;
358	}
359
360	ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
361					  nodesize, old_roots, new_roots);
362	if (ret) {
363		test_msg("Couldn't account space for a qgroup %d\n", ret);
364		return ret;
365	}
366
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");
370		return -EINVAL;
371	}
372
373	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
 
374	if (ret) {
375		ulist_free(old_roots);
376		test_msg("Couldn't find old roots: %d\n", ret);
377		return ret;
378	}
379
380	ret = add_tree_ref(root, nodesize, nodesize, 0,
381			BTRFS_FIRST_FREE_OBJECTID);
382	if (ret)
383		return ret;
384
385	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
 
386	if (ret) {
387		ulist_free(old_roots);
388		ulist_free(new_roots);
389		test_msg("Couldn't find old roots: %d\n", ret);
390		return ret;
391	}
392
393	ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
394					  nodesize, old_roots, new_roots);
395	if (ret) {
396		test_msg("Couldn't account space for a qgroup %d\n", ret);
397		return ret;
398	}
399
400	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
401					nodesize, 0)) {
402		test_msg("Qgroup counts didn't match expected values\n");
403		return -EINVAL;
404	}
405
406	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
407					nodesize, 0)) {
408		test_msg("Qgroup counts didn't match expected values\n");
409		return -EINVAL;
410	}
411
412	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
 
413	if (ret) {
414		ulist_free(old_roots);
415		test_msg("Couldn't find old roots: %d\n", ret);
416		return ret;
417	}
418
419	ret = remove_extent_ref(root, nodesize, nodesize, 0,
420				BTRFS_FIRST_FREE_OBJECTID);
421	if (ret)
422		return ret;
423
424	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
 
425	if (ret) {
426		ulist_free(old_roots);
427		ulist_free(new_roots);
428		test_msg("Couldn't find old roots: %d\n", ret);
429		return ret;
430	}
431
432	ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
433					  nodesize, old_roots, new_roots);
434	if (ret) {
435		test_msg("Couldn't account space for a qgroup %d\n", ret);
436		return ret;
437	}
438
439	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
440					0, 0)) {
441		test_msg("Qgroup counts didn't match expected values\n");
442		return -EINVAL;
443	}
444
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");
448		return -EINVAL;
449	}
450
451	return 0;
452}
453
454int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
455{
456	struct btrfs_fs_info *fs_info = NULL;
457	struct btrfs_root *root;
458	struct btrfs_root *tmp_root;
459	int ret = 0;
460
461	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
462	if (!fs_info) {
463		test_msg("Couldn't allocate dummy fs info\n");
464		return -ENOMEM;
465	}
466
467	root = btrfs_alloc_dummy_root(fs_info);
468	if (IS_ERR(root)) {
469		test_msg("Couldn't allocate root\n");
470		ret = PTR_ERR(root);
471		goto out;
472	}
473
474	/* We are using this root as our extent root */
475	root->fs_info->extent_root = root;
476
477	/*
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.
480	 */
481	root->fs_info->tree_root = root;
482	root->fs_info->quota_root = root;
483	set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
484
485	/*
486	 * Can't use bytenr 0, some things freak out
487	 * *cough*backref walking code*cough*
488	 */
489	root->node = alloc_test_extent_buffer(root->fs_info, nodesize);
490	if (!root->node) {
491		test_msg("Couldn't allocate dummy buffer\n");
492		ret = -ENOMEM;
493		goto out;
494	}
495	btrfs_set_header_level(root->node, 0);
496	btrfs_set_header_nritems(root->node, 0);
497	root->alloc_bytenr += 2 * nodesize;
498
499	tmp_root = btrfs_alloc_dummy_root(fs_info);
500	if (IS_ERR(tmp_root)) {
501		test_msg("Couldn't allocate a fs root\n");
502		ret = PTR_ERR(tmp_root);
503		goto out;
504	}
505
506	tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID;
507	root->fs_info->fs_root = tmp_root;
508	ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
509	if (ret) {
510		test_msg("Couldn't insert fs root %d\n", ret);
511		goto out;
512	}
513
514	tmp_root = btrfs_alloc_dummy_root(fs_info);
515	if (IS_ERR(tmp_root)) {
516		test_msg("Couldn't allocate a fs root\n");
517		ret = PTR_ERR(tmp_root);
518		goto out;
519	}
520
521	tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID;
522	ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
523	if (ret) {
524		test_msg("Couldn't insert fs root %d\n", ret);
525		goto out;
526	}
527
528	test_msg("Running qgroup tests\n");
529	ret = test_no_shared_qgroup(root, sectorsize, nodesize);
530	if (ret)
531		goto out;
532	ret = test_multiple_refs(root, sectorsize, nodesize);
533out:
534	btrfs_free_dummy_root(root);
535	btrfs_free_dummy_fs_info(fs_info);
536	return ret;
537}