David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Facebook. All rights reserved. |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 6 | #include <linux/types.h> |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 7 | #include "btrfs-tests.h" |
| 8 | #include "../ctree.h" |
| 9 | #include "../transaction.h" |
| 10 | #include "../disk-io.h" |
| 11 | #include "../qgroup.h" |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 12 | #include "../backref.h" |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 13 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 14 | static int insert_normal_tree_ref(struct btrfs_root *root, u64 bytenr, |
| 15 | u64 num_bytes, u64 parent, u64 root_objectid) |
| 16 | { |
| 17 | struct btrfs_trans_handle trans; |
| 18 | struct btrfs_extent_item *item; |
| 19 | struct btrfs_extent_inline_ref *iref; |
| 20 | struct btrfs_tree_block_info *block_info; |
| 21 | struct btrfs_path *path; |
| 22 | struct extent_buffer *leaf; |
| 23 | struct btrfs_key ins; |
| 24 | u32 size = sizeof(*item) + sizeof(*iref) + sizeof(*block_info); |
| 25 | int ret; |
| 26 | |
Nikolay Borisov | 483bce0 | 2018-05-10 15:44:40 +0300 | [diff] [blame] | 27 | btrfs_init_dummy_trans(&trans, NULL); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 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) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 35 | test_err("couldn't allocate path"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 36 | return -ENOMEM; |
| 37 | } |
| 38 | |
| 39 | path->leave_spinning = 1; |
| 40 | ret = btrfs_insert_empty_item(&trans, root, path, &ins, size); |
| 41 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 42 | test_err("couldn't insert ref %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 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); |
Qu Wenruo | 3c0efdf | 2018-03-27 20:44:18 +0800 | [diff] [blame] | 53 | btrfs_set_tree_block_level(leaf, block_info, 0); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 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 | |
| 67 | static int add_tree_ref(struct btrfs_root *root, u64 bytenr, u64 num_bytes, |
| 68 | u64 parent, u64 root_objectid) |
| 69 | { |
| 70 | struct btrfs_trans_handle trans; |
| 71 | struct btrfs_extent_item *item; |
| 72 | struct btrfs_path *path; |
| 73 | struct btrfs_key key; |
| 74 | u64 refs; |
| 75 | int ret; |
| 76 | |
Nikolay Borisov | 483bce0 | 2018-05-10 15:44:40 +0300 | [diff] [blame] | 77 | btrfs_init_dummy_trans(&trans, NULL); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 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) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 85 | test_err("couldn't allocate path"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 86 | return -ENOMEM; |
| 87 | } |
| 88 | |
| 89 | path->leave_spinning = 1; |
| 90 | ret = btrfs_search_slot(&trans, root, &key, path, 0, 1); |
| 91 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 92 | test_err("couldn't find extent ref"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 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) |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 114 | test_err("failed to insert backref"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 115 | btrfs_free_path(path); |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | static int remove_extent_item(struct btrfs_root *root, u64 bytenr, |
| 120 | u64 num_bytes) |
| 121 | { |
| 122 | struct btrfs_trans_handle trans; |
| 123 | struct btrfs_key key; |
| 124 | struct btrfs_path *path; |
| 125 | int ret; |
| 126 | |
Nikolay Borisov | 483bce0 | 2018-05-10 15:44:40 +0300 | [diff] [blame] | 127 | btrfs_init_dummy_trans(&trans, NULL); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 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) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 135 | test_err("couldn't allocate path"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 136 | return -ENOMEM; |
| 137 | } |
| 138 | path->leave_spinning = 1; |
| 139 | |
| 140 | ret = btrfs_search_slot(&trans, root, &key, path, -1, 1); |
| 141 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 142 | test_err("didn't find our key %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 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 | |
| 151 | static int remove_extent_ref(struct btrfs_root *root, u64 bytenr, |
| 152 | u64 num_bytes, u64 parent, u64 root_objectid) |
| 153 | { |
| 154 | struct btrfs_trans_handle trans; |
| 155 | struct btrfs_extent_item *item; |
| 156 | struct btrfs_path *path; |
| 157 | struct btrfs_key key; |
| 158 | u64 refs; |
| 159 | int ret; |
| 160 | |
Nikolay Borisov | 483bce0 | 2018-05-10 15:44:40 +0300 | [diff] [blame] | 161 | btrfs_init_dummy_trans(&trans, NULL); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 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) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 169 | test_err("couldn't allocate path"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 170 | return -ENOMEM; |
| 171 | } |
| 172 | |
| 173 | path->leave_spinning = 1; |
| 174 | ret = btrfs_search_slot(&trans, root, &key, path, 0, 1); |
| 175 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 176 | test_err("couldn't find extent ref"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 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) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 198 | test_err("couldn't find backref %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 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 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 207 | static int test_no_shared_qgroup(struct btrfs_root *root, |
| 208 | u32 sectorsize, u32 nodesize) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 209 | { |
| 210 | struct btrfs_trans_handle trans; |
| 211 | struct btrfs_fs_info *fs_info = root->fs_info; |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 212 | struct ulist *old_roots = NULL; |
| 213 | struct ulist *new_roots = NULL; |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 214 | int ret; |
| 215 | |
Nikolay Borisov | 483bce0 | 2018-05-10 15:44:40 +0300 | [diff] [blame] | 216 | btrfs_init_dummy_trans(&trans, fs_info); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 217 | |
David Sterba | 315b76b | 2018-05-17 00:00:44 +0200 | [diff] [blame] | 218 | test_msg("qgroup basic add"); |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 219 | ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FS_TREE_OBJECTID); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 220 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 221 | test_err("couldn't create a qgroup %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 222 | return ret; |
| 223 | } |
| 224 | |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 225 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 226 | * Since the test trans doesn't have the complicated delayed refs, |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 227 | * we can only call btrfs_qgroup_account_extent() directly to test |
| 228 | * quota. |
| 229 | */ |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 230 | ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, |
| 231 | false); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 232 | if (ret) { |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 233 | ulist_free(old_roots); |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 234 | test_err("couldn't find old roots: %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 235 | return ret; |
| 236 | } |
| 237 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 238 | ret = insert_normal_tree_ref(root, nodesize, nodesize, 0, |
| 239 | BTRFS_FS_TREE_OBJECTID); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 240 | if (ret) |
| 241 | return ret; |
| 242 | |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 243 | ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, |
| 244 | false); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 245 | if (ret) { |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 246 | ulist_free(old_roots); |
| 247 | ulist_free(new_roots); |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 248 | test_err("couldn't find old roots: %d", ret); |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 249 | return ret; |
| 250 | } |
| 251 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 252 | ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize, |
| 253 | nodesize, old_roots, new_roots); |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 254 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 255 | test_err("couldn't account space for a qgroup %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 256 | return ret; |
| 257 | } |
| 258 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 259 | if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, |
| 260 | nodesize, nodesize)) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 261 | test_err("qgroup counts didn't match expected values"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 262 | return -EINVAL; |
| 263 | } |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 264 | old_roots = NULL; |
| 265 | new_roots = NULL; |
| 266 | |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 267 | ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, |
| 268 | false); |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 269 | if (ret) { |
| 270 | ulist_free(old_roots); |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 271 | test_err("couldn't find old roots: %d", ret); |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 272 | return ret; |
| 273 | } |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 274 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 275 | ret = remove_extent_item(root, nodesize, nodesize); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 276 | if (ret) |
| 277 | return -EINVAL; |
| 278 | |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 279 | ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, |
| 280 | false); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 281 | if (ret) { |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 282 | ulist_free(old_roots); |
| 283 | ulist_free(new_roots); |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 284 | test_err("couldn't find old roots: %d", ret); |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 285 | return ret; |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 286 | } |
| 287 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 288 | ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize, |
| 289 | nodesize, old_roots, new_roots); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 290 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 291 | test_err("couldn't account space for a qgroup %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 292 | return -EINVAL; |
| 293 | } |
| 294 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 295 | if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 296 | test_err("qgroup counts didn't match expected values"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 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 | */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 308 | static int test_multiple_refs(struct btrfs_root *root, |
| 309 | u32 sectorsize, u32 nodesize) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 310 | { |
| 311 | struct btrfs_trans_handle trans; |
| 312 | struct btrfs_fs_info *fs_info = root->fs_info; |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 313 | struct ulist *old_roots = NULL; |
| 314 | struct ulist *new_roots = NULL; |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 315 | int ret; |
| 316 | |
Nikolay Borisov | 483bce0 | 2018-05-10 15:44:40 +0300 | [diff] [blame] | 317 | btrfs_init_dummy_trans(&trans, fs_info); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 318 | |
David Sterba | 315b76b | 2018-05-17 00:00:44 +0200 | [diff] [blame] | 319 | test_msg("qgroup multiple refs test"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 320 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 321 | /* |
| 322 | * We have BTRFS_FS_TREE_OBJECTID created already from the |
| 323 | * previous test. |
| 324 | */ |
| 325 | ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FIRST_FREE_OBJECTID); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 326 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 327 | test_err("couldn't create a qgroup %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 328 | return ret; |
| 329 | } |
| 330 | |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 331 | ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, |
| 332 | false); |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 333 | if (ret) { |
| 334 | ulist_free(old_roots); |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 335 | test_err("couldn't find old roots: %d", ret); |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 336 | return ret; |
| 337 | } |
| 338 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 339 | ret = insert_normal_tree_ref(root, nodesize, nodesize, 0, |
| 340 | BTRFS_FS_TREE_OBJECTID); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 341 | if (ret) |
| 342 | return ret; |
| 343 | |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 344 | ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, |
| 345 | false); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 346 | if (ret) { |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 347 | ulist_free(old_roots); |
| 348 | ulist_free(new_roots); |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 349 | test_err("couldn't find old roots: %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 350 | return ret; |
| 351 | } |
| 352 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 353 | ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize, |
| 354 | nodesize, old_roots, new_roots); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 355 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 356 | test_err("couldn't account space for a qgroup %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 357 | return ret; |
| 358 | } |
| 359 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 360 | if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 361 | nodesize, nodesize)) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 362 | test_err("qgroup counts didn't match expected values"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 363 | return -EINVAL; |
| 364 | } |
| 365 | |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 366 | ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, |
| 367 | false); |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 368 | if (ret) { |
| 369 | ulist_free(old_roots); |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 370 | test_err("couldn't find old roots: %d", ret); |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 371 | return ret; |
| 372 | } |
| 373 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 374 | ret = add_tree_ref(root, nodesize, nodesize, 0, |
| 375 | BTRFS_FIRST_FREE_OBJECTID); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 376 | if (ret) |
| 377 | return ret; |
| 378 | |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 379 | ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, |
| 380 | false); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 381 | if (ret) { |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 382 | ulist_free(old_roots); |
| 383 | ulist_free(new_roots); |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 384 | test_err("couldn't find old roots: %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 385 | return ret; |
| 386 | } |
| 387 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 388 | ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize, |
| 389 | nodesize, old_roots, new_roots); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 390 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 391 | test_err("couldn't account space for a qgroup %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 392 | return ret; |
| 393 | } |
| 394 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 395 | if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, |
| 396 | nodesize, 0)) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 397 | test_err("qgroup counts didn't match expected values"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 398 | return -EINVAL; |
| 399 | } |
| 400 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 401 | if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID, |
| 402 | nodesize, 0)) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 403 | test_err("qgroup counts didn't match expected values"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 404 | return -EINVAL; |
| 405 | } |
| 406 | |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 407 | ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots, |
| 408 | false); |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 409 | if (ret) { |
| 410 | ulist_free(old_roots); |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 411 | test_err("couldn't find old roots: %d", ret); |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 412 | return ret; |
| 413 | } |
| 414 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 415 | ret = remove_extent_ref(root, nodesize, nodesize, 0, |
| 416 | BTRFS_FIRST_FREE_OBJECTID); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 417 | if (ret) |
| 418 | return ret; |
| 419 | |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 420 | ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots, |
| 421 | false); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 422 | if (ret) { |
Qu Wenruo | 442244c | 2015-04-16 17:18:36 +0800 | [diff] [blame] | 423 | ulist_free(old_roots); |
| 424 | ulist_free(new_roots); |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 425 | test_err("couldn't find old roots: %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 426 | return ret; |
| 427 | } |
| 428 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 429 | ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize, |
| 430 | nodesize, old_roots, new_roots); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 431 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 432 | test_err("couldn't account space for a qgroup %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 433 | return ret; |
| 434 | } |
| 435 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 436 | if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID, |
| 437 | 0, 0)) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 438 | test_err("qgroup counts didn't match expected values"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 439 | return -EINVAL; |
| 440 | } |
| 441 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 442 | if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, |
| 443 | nodesize, nodesize)) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 444 | test_err("qgroup counts didn't match expected values"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 445 | return -EINVAL; |
| 446 | } |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 451 | int btrfs_test_qgroups(u32 sectorsize, u32 nodesize) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 452 | { |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 453 | struct btrfs_fs_info *fs_info = NULL; |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 454 | struct btrfs_root *root; |
| 455 | struct btrfs_root *tmp_root; |
| 456 | int ret = 0; |
| 457 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 458 | fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 459 | if (!fs_info) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 460 | test_err("couldn't allocate dummy fs info"); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 461 | return -ENOMEM; |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 462 | } |
| 463 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 464 | root = btrfs_alloc_dummy_root(fs_info); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 465 | if (IS_ERR(root)) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 466 | test_err("couldn't allocate root"); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 467 | ret = PTR_ERR(root); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 468 | goto out; |
| 469 | } |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 470 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 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; |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 480 | set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 481 | |
| 482 | /* |
| 483 | * Can't use bytenr 0, some things freak out |
| 484 | * *cough*backref walking code*cough* |
| 485 | */ |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 486 | root->node = alloc_test_extent_buffer(root->fs_info, nodesize); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 487 | if (!root->node) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 488 | test_err("couldn't allocate dummy buffer"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 489 | ret = -ENOMEM; |
| 490 | goto out; |
| 491 | } |
Filipe Manana | b050f9f | 2014-06-12 02:47:37 +0100 | [diff] [blame] | 492 | btrfs_set_header_level(root->node, 0); |
| 493 | btrfs_set_header_nritems(root->node, 0); |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 494 | root->alloc_bytenr += 2 * nodesize; |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 495 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 496 | tmp_root = btrfs_alloc_dummy_root(fs_info); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 497 | if (IS_ERR(tmp_root)) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 498 | test_err("couldn't allocate a fs root"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 499 | ret = PTR_ERR(tmp_root); |
| 500 | goto out; |
| 501 | } |
| 502 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 503 | tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID; |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 504 | root->fs_info->fs_root = tmp_root; |
| 505 | ret = btrfs_insert_fs_root(root->fs_info, tmp_root); |
| 506 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 507 | test_err("couldn't insert fs root %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 508 | goto out; |
| 509 | } |
| 510 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 511 | tmp_root = btrfs_alloc_dummy_root(fs_info); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 512 | if (IS_ERR(tmp_root)) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 513 | test_err("couldn't allocate a fs root"); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 514 | ret = PTR_ERR(tmp_root); |
| 515 | goto out; |
| 516 | } |
| 517 | |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 518 | tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID; |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 519 | ret = btrfs_insert_fs_root(root->fs_info, tmp_root); |
| 520 | if (ret) { |
David Sterba | 3c7251f | 2018-05-17 00:00:42 +0200 | [diff] [blame] | 521 | test_err("couldn't insert fs root %d", ret); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 522 | goto out; |
| 523 | } |
| 524 | |
David Sterba | 315b76b | 2018-05-17 00:00:44 +0200 | [diff] [blame] | 525 | test_msg("running qgroup tests"); |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 526 | ret = test_no_shared_qgroup(root, sectorsize, nodesize); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 527 | if (ret) |
| 528 | goto out; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 529 | ret = test_multiple_refs(root, sectorsize, nodesize); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 530 | out: |
| 531 | btrfs_free_dummy_root(root); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 532 | btrfs_free_dummy_fs_info(fs_info); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 533 | return ret; |
| 534 | } |