Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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