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		test_err("couldn't find old roots: %d", ret);
234		return ret;
235	}
236
237	ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
238				BTRFS_FS_TREE_OBJECTID);
239	if (ret) {
240		ulist_free(old_roots);
241		return ret;
242	}
243
244	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
245			false);
246	if (ret) {
247		ulist_free(old_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	/* btrfs_qgroup_account_extent() always frees the ulists passed to it. */
260	old_roots = NULL;
261	new_roots = NULL;
262
263	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
264				nodesize, nodesize)) {
265		test_err("qgroup counts didn't match expected values");
266		return -EINVAL;
267	}
268
269	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
270			false);
271	if (ret) {
272		test_err("couldn't find old roots: %d", ret);
273		return ret;
274	}
275
276	ret = remove_extent_item(root, nodesize, nodesize);
277	if (ret) {
278		ulist_free(old_roots);
279		return -EINVAL;
280	}
281
282	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
283			false);
284	if (ret) {
285		ulist_free(old_roots);
286		test_err("couldn't find old roots: %d", ret);
287		return ret;
288	}
289
290	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
291					  new_roots);
292	if (ret) {
293		test_err("couldn't account space for a qgroup %d", ret);
294		return -EINVAL;
295	}
296
297	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
298		test_err("qgroup counts didn't match expected values");
299		return -EINVAL;
300	}
301
302	return 0;
303}
304
305/*
306 * Add a ref for two different roots to make sure the shared value comes out
307 * right, also remove one of the roots and make sure the exclusive count is
308 * adjusted properly.
309 */
310static int test_multiple_refs(struct btrfs_root *root,
311		u32 sectorsize, u32 nodesize)
312{
313	struct btrfs_trans_handle trans;
314	struct btrfs_fs_info *fs_info = root->fs_info;
315	struct ulist *old_roots = NULL;
316	struct ulist *new_roots = NULL;
317	int ret;
318
319	btrfs_init_dummy_trans(&trans, fs_info);
320
321	test_msg("running qgroup multiple refs test");
322
323	/*
324	 * We have BTRFS_FS_TREE_OBJECTID created already from the
325	 * previous test.
326	 */
327	ret = btrfs_create_qgroup(&trans, BTRFS_FIRST_FREE_OBJECTID);
328	if (ret) {
329		test_err("couldn't create a qgroup %d", ret);
330		return ret;
331	}
332
333	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
334			false);
335	if (ret) {
336		test_err("couldn't find old roots: %d", ret);
337		return ret;
338	}
339
340	ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
341				BTRFS_FS_TREE_OBJECTID);
342	if (ret) {
343		ulist_free(old_roots);
344		return ret;
345	}
346
347	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
348			false);
349	if (ret) {
350		ulist_free(old_roots);
351		test_err("couldn't find old roots: %d", ret);
352		return ret;
353	}
354
355	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
356					  new_roots);
357	if (ret) {
358		test_err("couldn't account space for a qgroup %d", ret);
359		return ret;
360	}
361
362	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
363				       nodesize, nodesize)) {
364		test_err("qgroup counts didn't match expected values");
365		return -EINVAL;
366	}
367
368	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
369			false);
370	if (ret) {
371		test_err("couldn't find old roots: %d", ret);
372		return ret;
373	}
374
375	ret = add_tree_ref(root, nodesize, nodesize, 0,
376			BTRFS_FIRST_FREE_OBJECTID);
377	if (ret) {
378		ulist_free(old_roots);
379		return ret;
380	}
381
382	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
383			false);
384	if (ret) {
385		ulist_free(old_roots);
386		test_err("couldn't find old roots: %d", ret);
387		return ret;
388	}
389
390	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
391					  new_roots);
392	if (ret) {
393		test_err("couldn't account space for a qgroup %d", ret);
394		return ret;
395	}
396
397	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
398					nodesize, 0)) {
399		test_err("qgroup counts didn't match expected values");
400		return -EINVAL;
401	}
402
403	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
404					nodesize, 0)) {
405		test_err("qgroup counts didn't match expected values");
406		return -EINVAL;
407	}
408
409	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
410			false);
411	if (ret) {
412		test_err("couldn't find old roots: %d", ret);
413		return ret;
414	}
415
416	ret = remove_extent_ref(root, nodesize, nodesize, 0,
417				BTRFS_FIRST_FREE_OBJECTID);
418	if (ret) {
419		ulist_free(old_roots);
420		return ret;
421	}
422
423	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
424			false);
425	if (ret) {
426		ulist_free(old_roots);
427		test_err("couldn't find old roots: %d", ret);
428		return ret;
429	}
430
431	ret = btrfs_qgroup_account_extent(&trans, nodesize, nodesize, old_roots,
432					  new_roots);
433	if (ret) {
434		test_err("couldn't account space for a qgroup %d", ret);
435		return ret;
436	}
437
438	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
439					0, 0)) {
440		test_err("qgroup counts didn't match expected values");
441		return -EINVAL;
442	}
443
444	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
445					nodesize, nodesize)) {
446		test_err("qgroup counts didn't match expected values");
447		return -EINVAL;
448	}
449
450	return 0;
451}
452
453int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
454{
455	struct btrfs_fs_info *fs_info = NULL;
456	struct btrfs_root *root;
457	struct btrfs_root *tmp_root;
458	int ret = 0;
459
460	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
461	if (!fs_info) {
462		test_std_err(TEST_ALLOC_FS_INFO);
463		return -ENOMEM;
464	}
465
466	root = btrfs_alloc_dummy_root(fs_info);
467	if (IS_ERR(root)) {
468		test_std_err(TEST_ALLOC_ROOT);
469		ret = PTR_ERR(root);
470		goto out;
471	}
472
473	/* We are using this root as our extent root */
474	root->fs_info->extent_root = root;
475
476	/*
477	 * Some of the paths we test assume we have a filled out fs_info, so we
478	 * just need to add the root in there so we don't panic.
479	 */
480	root->fs_info->tree_root = root;
481	root->fs_info->quota_root = root;
482	set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
483
484	/*
485	 * Can't use bytenr 0, some things freak out
486	 * *cough*backref walking code*cough*
487	 */
488	root->node = alloc_test_extent_buffer(root->fs_info, nodesize);
489	if (IS_ERR(root->node)) {
490		test_err("couldn't allocate dummy buffer");
491		ret = PTR_ERR(root->node);
492		goto out;
493	}
494	btrfs_set_header_level(root->node, 0);
495	btrfs_set_header_nritems(root->node, 0);
496	root->alloc_bytenr += 2 * nodesize;
497
498	tmp_root = btrfs_alloc_dummy_root(fs_info);
499	if (IS_ERR(tmp_root)) {
500		test_std_err(TEST_ALLOC_ROOT);
501		ret = PTR_ERR(tmp_root);
502		goto out;
503	}
504
505	tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID;
506	root->fs_info->fs_root = tmp_root;
507	ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
508	if (ret) {
509		test_err("couldn't insert fs root %d", ret);
510		goto out;
511	}
512	btrfs_put_root(tmp_root);
513
514	tmp_root = btrfs_alloc_dummy_root(fs_info);
515	if (IS_ERR(tmp_root)) {
516		test_std_err(TEST_ALLOC_ROOT);
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_err("couldn't insert fs root %d", ret);
525		goto out;
526	}
527	btrfs_put_root(tmp_root);
528
529	test_msg("running qgroup tests");
530	ret = test_no_shared_qgroup(root, sectorsize, nodesize);
531	if (ret)
532		goto out;
533	ret = test_multiple_refs(root, sectorsize, nodesize);
534out:
535	btrfs_free_dummy_root(root);
536	btrfs_free_dummy_fs_info(fs_info);
537	return ret;
538}
539