Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 STRATO. 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/sched.h> |
| 20 | #include <linux/pagemap.h> |
| 21 | #include <linux/writeback.h> |
| 22 | #include <linux/blkdev.h> |
| 23 | #include <linux/rbtree.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/workqueue.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 26 | #include <linux/btrfs.h> |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 27 | |
| 28 | #include "ctree.h" |
| 29 | #include "transaction.h" |
| 30 | #include "disk-io.h" |
| 31 | #include "locking.h" |
| 32 | #include "ulist.h" |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 33 | #include "backref.h" |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 34 | #include "extent_io.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 35 | #include "qgroup.h" |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 36 | |
| 37 | /* TODO XXX FIXME |
| 38 | * - subvol delete -> delete when ref goes to 0? delete limits also? |
| 39 | * - reorganize keys |
| 40 | * - compressed |
| 41 | * - sync |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 42 | * - copy also limits on subvol creation |
| 43 | * - limit |
| 44 | * - caches fuer ulists |
| 45 | * - performance benchmarks |
| 46 | * - check all ioctl parameters |
| 47 | */ |
| 48 | |
| 49 | /* |
| 50 | * one struct for each qgroup, organized in fs_info->qgroup_tree. |
| 51 | */ |
| 52 | struct btrfs_qgroup { |
| 53 | u64 qgroupid; |
| 54 | |
| 55 | /* |
| 56 | * state |
| 57 | */ |
| 58 | u64 rfer; /* referenced */ |
| 59 | u64 rfer_cmpr; /* referenced compressed */ |
| 60 | u64 excl; /* exclusive */ |
| 61 | u64 excl_cmpr; /* exclusive compressed */ |
| 62 | |
| 63 | /* |
| 64 | * limits |
| 65 | */ |
| 66 | u64 lim_flags; /* which limits are set */ |
| 67 | u64 max_rfer; |
| 68 | u64 max_excl; |
| 69 | u64 rsv_rfer; |
| 70 | u64 rsv_excl; |
| 71 | |
| 72 | /* |
| 73 | * reservation tracking |
| 74 | */ |
| 75 | u64 reserved; |
| 76 | |
| 77 | /* |
| 78 | * lists |
| 79 | */ |
| 80 | struct list_head groups; /* groups this group is member of */ |
| 81 | struct list_head members; /* groups that are members of this group */ |
| 82 | struct list_head dirty; /* dirty groups */ |
| 83 | struct rb_node node; /* tree of qgroups */ |
| 84 | |
| 85 | /* |
| 86 | * temp variables for accounting operations |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 87 | * Refer to qgroup_shared_accouting() for details. |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 88 | */ |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 89 | u64 old_refcnt; |
| 90 | u64 new_refcnt; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 93 | static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq, |
| 94 | int mod) |
| 95 | { |
| 96 | if (qg->old_refcnt < seq) |
| 97 | qg->old_refcnt = seq; |
| 98 | qg->old_refcnt += mod; |
| 99 | } |
| 100 | |
| 101 | static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq, |
| 102 | int mod) |
| 103 | { |
| 104 | if (qg->new_refcnt < seq) |
| 105 | qg->new_refcnt = seq; |
| 106 | qg->new_refcnt += mod; |
| 107 | } |
| 108 | |
| 109 | static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq) |
| 110 | { |
| 111 | if (qg->old_refcnt < seq) |
| 112 | return 0; |
| 113 | return qg->old_refcnt - seq; |
| 114 | } |
| 115 | |
| 116 | static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq) |
| 117 | { |
| 118 | if (qg->new_refcnt < seq) |
| 119 | return 0; |
| 120 | return qg->new_refcnt - seq; |
| 121 | } |
| 122 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 123 | /* |
| 124 | * glue structure to represent the relations between qgroups. |
| 125 | */ |
| 126 | struct btrfs_qgroup_list { |
| 127 | struct list_head next_group; |
| 128 | struct list_head next_member; |
| 129 | struct btrfs_qgroup *group; |
| 130 | struct btrfs_qgroup *member; |
| 131 | }; |
| 132 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 133 | #define ptr_to_u64(x) ((u64)(uintptr_t)x) |
| 134 | #define u64_to_ptr(x) ((struct btrfs_qgroup *)(uintptr_t)x) |
| 135 | |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 136 | static int |
| 137 | qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, |
| 138 | int init_flags); |
| 139 | static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 140 | |
Wang Shilong | 58400fc | 2013-04-07 10:50:17 +0000 | [diff] [blame] | 141 | /* must be called with qgroup_ioctl_lock held */ |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 142 | static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info, |
| 143 | u64 qgroupid) |
| 144 | { |
| 145 | struct rb_node *n = fs_info->qgroup_tree.rb_node; |
| 146 | struct btrfs_qgroup *qgroup; |
| 147 | |
| 148 | while (n) { |
| 149 | qgroup = rb_entry(n, struct btrfs_qgroup, node); |
| 150 | if (qgroup->qgroupid < qgroupid) |
| 151 | n = n->rb_left; |
| 152 | else if (qgroup->qgroupid > qgroupid) |
| 153 | n = n->rb_right; |
| 154 | else |
| 155 | return qgroup; |
| 156 | } |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | /* must be called with qgroup_lock held */ |
| 161 | static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info, |
| 162 | u64 qgroupid) |
| 163 | { |
| 164 | struct rb_node **p = &fs_info->qgroup_tree.rb_node; |
| 165 | struct rb_node *parent = NULL; |
| 166 | struct btrfs_qgroup *qgroup; |
| 167 | |
| 168 | while (*p) { |
| 169 | parent = *p; |
| 170 | qgroup = rb_entry(parent, struct btrfs_qgroup, node); |
| 171 | |
| 172 | if (qgroup->qgroupid < qgroupid) |
| 173 | p = &(*p)->rb_left; |
| 174 | else if (qgroup->qgroupid > qgroupid) |
| 175 | p = &(*p)->rb_right; |
| 176 | else |
| 177 | return qgroup; |
| 178 | } |
| 179 | |
| 180 | qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC); |
| 181 | if (!qgroup) |
| 182 | return ERR_PTR(-ENOMEM); |
| 183 | |
| 184 | qgroup->qgroupid = qgroupid; |
| 185 | INIT_LIST_HEAD(&qgroup->groups); |
| 186 | INIT_LIST_HEAD(&qgroup->members); |
| 187 | INIT_LIST_HEAD(&qgroup->dirty); |
| 188 | |
| 189 | rb_link_node(&qgroup->node, parent, p); |
| 190 | rb_insert_color(&qgroup->node, &fs_info->qgroup_tree); |
| 191 | |
| 192 | return qgroup; |
| 193 | } |
| 194 | |
Wang Shilong | 4082bd3 | 2013-08-14 09:13:36 +0800 | [diff] [blame] | 195 | static void __del_qgroup_rb(struct btrfs_qgroup *qgroup) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 196 | { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 197 | struct btrfs_qgroup_list *list; |
| 198 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 199 | list_del(&qgroup->dirty); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 200 | while (!list_empty(&qgroup->groups)) { |
| 201 | list = list_first_entry(&qgroup->groups, |
| 202 | struct btrfs_qgroup_list, next_group); |
| 203 | list_del(&list->next_group); |
| 204 | list_del(&list->next_member); |
| 205 | kfree(list); |
| 206 | } |
| 207 | |
| 208 | while (!list_empty(&qgroup->members)) { |
| 209 | list = list_first_entry(&qgroup->members, |
| 210 | struct btrfs_qgroup_list, next_member); |
| 211 | list_del(&list->next_group); |
| 212 | list_del(&list->next_member); |
| 213 | kfree(list); |
| 214 | } |
| 215 | kfree(qgroup); |
Wang Shilong | 4082bd3 | 2013-08-14 09:13:36 +0800 | [diff] [blame] | 216 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 217 | |
Wang Shilong | 4082bd3 | 2013-08-14 09:13:36 +0800 | [diff] [blame] | 218 | /* must be called with qgroup_lock held */ |
| 219 | static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid) |
| 220 | { |
| 221 | struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid); |
| 222 | |
| 223 | if (!qgroup) |
| 224 | return -ENOENT; |
| 225 | |
| 226 | rb_erase(&qgroup->node, &fs_info->qgroup_tree); |
| 227 | __del_qgroup_rb(qgroup); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /* must be called with qgroup_lock held */ |
| 232 | static int add_relation_rb(struct btrfs_fs_info *fs_info, |
| 233 | u64 memberid, u64 parentid) |
| 234 | { |
| 235 | struct btrfs_qgroup *member; |
| 236 | struct btrfs_qgroup *parent; |
| 237 | struct btrfs_qgroup_list *list; |
| 238 | |
| 239 | member = find_qgroup_rb(fs_info, memberid); |
| 240 | parent = find_qgroup_rb(fs_info, parentid); |
| 241 | if (!member || !parent) |
| 242 | return -ENOENT; |
| 243 | |
| 244 | list = kzalloc(sizeof(*list), GFP_ATOMIC); |
| 245 | if (!list) |
| 246 | return -ENOMEM; |
| 247 | |
| 248 | list->group = parent; |
| 249 | list->member = member; |
| 250 | list_add_tail(&list->next_group, &member->groups); |
| 251 | list_add_tail(&list->next_member, &parent->members); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* must be called with qgroup_lock held */ |
| 257 | static int del_relation_rb(struct btrfs_fs_info *fs_info, |
| 258 | u64 memberid, u64 parentid) |
| 259 | { |
| 260 | struct btrfs_qgroup *member; |
| 261 | struct btrfs_qgroup *parent; |
| 262 | struct btrfs_qgroup_list *list; |
| 263 | |
| 264 | member = find_qgroup_rb(fs_info, memberid); |
| 265 | parent = find_qgroup_rb(fs_info, parentid); |
| 266 | if (!member || !parent) |
| 267 | return -ENOENT; |
| 268 | |
| 269 | list_for_each_entry(list, &member->groups, next_group) { |
| 270 | if (list->group == parent) { |
| 271 | list_del(&list->next_group); |
| 272 | list_del(&list->next_member); |
| 273 | kfree(list); |
| 274 | return 0; |
| 275 | } |
| 276 | } |
| 277 | return -ENOENT; |
| 278 | } |
| 279 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 280 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 281 | int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid, |
| 282 | u64 rfer, u64 excl) |
| 283 | { |
| 284 | struct btrfs_qgroup *qgroup; |
| 285 | |
| 286 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
| 287 | if (!qgroup) |
| 288 | return -EINVAL; |
| 289 | if (qgroup->rfer != rfer || qgroup->excl != excl) |
| 290 | return -EINVAL; |
| 291 | return 0; |
| 292 | } |
| 293 | #endif |
| 294 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 295 | /* |
| 296 | * The full config is read in one go, only called from open_ctree() |
| 297 | * It doesn't use any locking, as at this point we're still single-threaded |
| 298 | */ |
| 299 | int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info) |
| 300 | { |
| 301 | struct btrfs_key key; |
| 302 | struct btrfs_key found_key; |
| 303 | struct btrfs_root *quota_root = fs_info->quota_root; |
| 304 | struct btrfs_path *path = NULL; |
| 305 | struct extent_buffer *l; |
| 306 | int slot; |
| 307 | int ret = 0; |
| 308 | u64 flags = 0; |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 309 | u64 rescan_progress = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 310 | |
| 311 | if (!fs_info->quota_enabled) |
| 312 | return 0; |
| 313 | |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 314 | fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS); |
| 315 | if (!fs_info->qgroup_ulist) { |
| 316 | ret = -ENOMEM; |
| 317 | goto out; |
| 318 | } |
| 319 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 320 | path = btrfs_alloc_path(); |
| 321 | if (!path) { |
| 322 | ret = -ENOMEM; |
| 323 | goto out; |
| 324 | } |
| 325 | |
| 326 | /* default this to quota off, in case no status key is found */ |
| 327 | fs_info->qgroup_flags = 0; |
| 328 | |
| 329 | /* |
| 330 | * pass 1: read status, all qgroup infos and limits |
| 331 | */ |
| 332 | key.objectid = 0; |
| 333 | key.type = 0; |
| 334 | key.offset = 0; |
| 335 | ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1); |
| 336 | if (ret) |
| 337 | goto out; |
| 338 | |
| 339 | while (1) { |
| 340 | struct btrfs_qgroup *qgroup; |
| 341 | |
| 342 | slot = path->slots[0]; |
| 343 | l = path->nodes[0]; |
| 344 | btrfs_item_key_to_cpu(l, &found_key, slot); |
| 345 | |
| 346 | if (found_key.type == BTRFS_QGROUP_STATUS_KEY) { |
| 347 | struct btrfs_qgroup_status_item *ptr; |
| 348 | |
| 349 | ptr = btrfs_item_ptr(l, slot, |
| 350 | struct btrfs_qgroup_status_item); |
| 351 | |
| 352 | if (btrfs_qgroup_status_version(l, ptr) != |
| 353 | BTRFS_QGROUP_STATUS_VERSION) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 354 | btrfs_err(fs_info, |
| 355 | "old qgroup version, quota disabled"); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 356 | goto out; |
| 357 | } |
| 358 | if (btrfs_qgroup_status_generation(l, ptr) != |
| 359 | fs_info->generation) { |
| 360 | flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 361 | btrfs_err(fs_info, |
| 362 | "qgroup generation mismatch, " |
| 363 | "marked as inconsistent"); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 364 | } |
| 365 | fs_info->qgroup_flags = btrfs_qgroup_status_flags(l, |
| 366 | ptr); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 367 | rescan_progress = btrfs_qgroup_status_rescan(l, ptr); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 368 | goto next1; |
| 369 | } |
| 370 | |
| 371 | if (found_key.type != BTRFS_QGROUP_INFO_KEY && |
| 372 | found_key.type != BTRFS_QGROUP_LIMIT_KEY) |
| 373 | goto next1; |
| 374 | |
| 375 | qgroup = find_qgroup_rb(fs_info, found_key.offset); |
| 376 | if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) || |
| 377 | (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 378 | btrfs_err(fs_info, "inconsitent qgroup config"); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 379 | flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 380 | } |
| 381 | if (!qgroup) { |
| 382 | qgroup = add_qgroup_rb(fs_info, found_key.offset); |
| 383 | if (IS_ERR(qgroup)) { |
| 384 | ret = PTR_ERR(qgroup); |
| 385 | goto out; |
| 386 | } |
| 387 | } |
| 388 | switch (found_key.type) { |
| 389 | case BTRFS_QGROUP_INFO_KEY: { |
| 390 | struct btrfs_qgroup_info_item *ptr; |
| 391 | |
| 392 | ptr = btrfs_item_ptr(l, slot, |
| 393 | struct btrfs_qgroup_info_item); |
| 394 | qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr); |
| 395 | qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr); |
| 396 | qgroup->excl = btrfs_qgroup_info_excl(l, ptr); |
| 397 | qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr); |
| 398 | /* generation currently unused */ |
| 399 | break; |
| 400 | } |
| 401 | case BTRFS_QGROUP_LIMIT_KEY: { |
| 402 | struct btrfs_qgroup_limit_item *ptr; |
| 403 | |
| 404 | ptr = btrfs_item_ptr(l, slot, |
| 405 | struct btrfs_qgroup_limit_item); |
| 406 | qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr); |
| 407 | qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr); |
| 408 | qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr); |
| 409 | qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr); |
| 410 | qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr); |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | next1: |
| 415 | ret = btrfs_next_item(quota_root, path); |
| 416 | if (ret < 0) |
| 417 | goto out; |
| 418 | if (ret) |
| 419 | break; |
| 420 | } |
| 421 | btrfs_release_path(path); |
| 422 | |
| 423 | /* |
| 424 | * pass 2: read all qgroup relations |
| 425 | */ |
| 426 | key.objectid = 0; |
| 427 | key.type = BTRFS_QGROUP_RELATION_KEY; |
| 428 | key.offset = 0; |
| 429 | ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0); |
| 430 | if (ret) |
| 431 | goto out; |
| 432 | while (1) { |
| 433 | slot = path->slots[0]; |
| 434 | l = path->nodes[0]; |
| 435 | btrfs_item_key_to_cpu(l, &found_key, slot); |
| 436 | |
| 437 | if (found_key.type != BTRFS_QGROUP_RELATION_KEY) |
| 438 | goto next2; |
| 439 | |
| 440 | if (found_key.objectid > found_key.offset) { |
| 441 | /* parent <- member, not needed to build config */ |
| 442 | /* FIXME should we omit the key completely? */ |
| 443 | goto next2; |
| 444 | } |
| 445 | |
| 446 | ret = add_relation_rb(fs_info, found_key.objectid, |
| 447 | found_key.offset); |
Arne Jansen | ff24858 | 2013-01-17 01:22:08 -0700 | [diff] [blame] | 448 | if (ret == -ENOENT) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 449 | btrfs_warn(fs_info, |
| 450 | "orphan qgroup relation 0x%llx->0x%llx", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 451 | found_key.objectid, found_key.offset); |
Arne Jansen | ff24858 | 2013-01-17 01:22:08 -0700 | [diff] [blame] | 452 | ret = 0; /* ignore the error */ |
| 453 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 454 | if (ret) |
| 455 | goto out; |
| 456 | next2: |
| 457 | ret = btrfs_next_item(quota_root, path); |
| 458 | if (ret < 0) |
| 459 | goto out; |
| 460 | if (ret) |
| 461 | break; |
| 462 | } |
| 463 | out: |
| 464 | fs_info->qgroup_flags |= flags; |
| 465 | if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) { |
| 466 | fs_info->quota_enabled = 0; |
| 467 | fs_info->pending_quota_state = 0; |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 468 | } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN && |
| 469 | ret >= 0) { |
| 470 | ret = qgroup_rescan_init(fs_info, rescan_progress, 0); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 471 | } |
| 472 | btrfs_free_path(path); |
| 473 | |
Jan Schmidt | eb1716a | 2013-05-28 15:47:23 +0000 | [diff] [blame] | 474 | if (ret < 0) { |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 475 | ulist_free(fs_info->qgroup_ulist); |
Jan Schmidt | eb1716a | 2013-05-28 15:47:23 +0000 | [diff] [blame] | 476 | fs_info->qgroup_ulist = NULL; |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 477 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
Jan Schmidt | eb1716a | 2013-05-28 15:47:23 +0000 | [diff] [blame] | 478 | } |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 479 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 480 | return ret < 0 ? ret : 0; |
| 481 | } |
| 482 | |
| 483 | /* |
Wang Shilong | e685da1 | 2013-08-14 09:13:37 +0800 | [diff] [blame] | 484 | * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(), |
| 485 | * first two are in single-threaded paths.And for the third one, we have set |
| 486 | * quota_root to be null with qgroup_lock held before, so it is safe to clean |
| 487 | * up the in-memory structures without qgroup_lock held. |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 488 | */ |
| 489 | void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info) |
| 490 | { |
| 491 | struct rb_node *n; |
| 492 | struct btrfs_qgroup *qgroup; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 493 | |
| 494 | while ((n = rb_first(&fs_info->qgroup_tree))) { |
| 495 | qgroup = rb_entry(n, struct btrfs_qgroup, node); |
| 496 | rb_erase(n, &fs_info->qgroup_tree); |
Wang Shilong | 4082bd3 | 2013-08-14 09:13:36 +0800 | [diff] [blame] | 497 | __del_qgroup_rb(qgroup); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 498 | } |
Wang Shilong | 1e7bac1 | 2013-07-13 21:02:54 +0800 | [diff] [blame] | 499 | /* |
| 500 | * we call btrfs_free_qgroup_config() when umounting |
| 501 | * filesystem and disabling quota, so we set qgroup_ulit |
| 502 | * to be null here to avoid double free. |
| 503 | */ |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 504 | ulist_free(fs_info->qgroup_ulist); |
Wang Shilong | 1e7bac1 | 2013-07-13 21:02:54 +0800 | [diff] [blame] | 505 | fs_info->qgroup_ulist = NULL; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, |
| 509 | struct btrfs_root *quota_root, |
| 510 | u64 src, u64 dst) |
| 511 | { |
| 512 | int ret; |
| 513 | struct btrfs_path *path; |
| 514 | struct btrfs_key key; |
| 515 | |
| 516 | path = btrfs_alloc_path(); |
| 517 | if (!path) |
| 518 | return -ENOMEM; |
| 519 | |
| 520 | key.objectid = src; |
| 521 | key.type = BTRFS_QGROUP_RELATION_KEY; |
| 522 | key.offset = dst; |
| 523 | |
| 524 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0); |
| 525 | |
| 526 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 527 | |
| 528 | btrfs_free_path(path); |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | static int del_qgroup_relation_item(struct btrfs_trans_handle *trans, |
| 533 | struct btrfs_root *quota_root, |
| 534 | u64 src, u64 dst) |
| 535 | { |
| 536 | int ret; |
| 537 | struct btrfs_path *path; |
| 538 | struct btrfs_key key; |
| 539 | |
| 540 | path = btrfs_alloc_path(); |
| 541 | if (!path) |
| 542 | return -ENOMEM; |
| 543 | |
| 544 | key.objectid = src; |
| 545 | key.type = BTRFS_QGROUP_RELATION_KEY; |
| 546 | key.offset = dst; |
| 547 | |
| 548 | ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1); |
| 549 | if (ret < 0) |
| 550 | goto out; |
| 551 | |
| 552 | if (ret > 0) { |
| 553 | ret = -ENOENT; |
| 554 | goto out; |
| 555 | } |
| 556 | |
| 557 | ret = btrfs_del_item(trans, quota_root, path); |
| 558 | out: |
| 559 | btrfs_free_path(path); |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | static int add_qgroup_item(struct btrfs_trans_handle *trans, |
| 564 | struct btrfs_root *quota_root, u64 qgroupid) |
| 565 | { |
| 566 | int ret; |
| 567 | struct btrfs_path *path; |
| 568 | struct btrfs_qgroup_info_item *qgroup_info; |
| 569 | struct btrfs_qgroup_limit_item *qgroup_limit; |
| 570 | struct extent_buffer *leaf; |
| 571 | struct btrfs_key key; |
| 572 | |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 573 | if (btrfs_test_is_dummy_root(quota_root)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 574 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 575 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 576 | path = btrfs_alloc_path(); |
| 577 | if (!path) |
| 578 | return -ENOMEM; |
| 579 | |
| 580 | key.objectid = 0; |
| 581 | key.type = BTRFS_QGROUP_INFO_KEY; |
| 582 | key.offset = qgroupid; |
| 583 | |
Mark Fasheh | 0b4699d | 2014-08-18 14:01:17 -0700 | [diff] [blame] | 584 | /* |
| 585 | * Avoid a transaction abort by catching -EEXIST here. In that |
| 586 | * case, we proceed by re-initializing the existing structure |
| 587 | * on disk. |
| 588 | */ |
| 589 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 590 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, |
| 591 | sizeof(*qgroup_info)); |
Mark Fasheh | 0b4699d | 2014-08-18 14:01:17 -0700 | [diff] [blame] | 592 | if (ret && ret != -EEXIST) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 593 | goto out; |
| 594 | |
| 595 | leaf = path->nodes[0]; |
| 596 | qgroup_info = btrfs_item_ptr(leaf, path->slots[0], |
| 597 | struct btrfs_qgroup_info_item); |
| 598 | btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid); |
| 599 | btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0); |
| 600 | btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0); |
| 601 | btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0); |
| 602 | btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0); |
| 603 | |
| 604 | btrfs_mark_buffer_dirty(leaf); |
| 605 | |
| 606 | btrfs_release_path(path); |
| 607 | |
| 608 | key.type = BTRFS_QGROUP_LIMIT_KEY; |
| 609 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, |
| 610 | sizeof(*qgroup_limit)); |
Mark Fasheh | 0b4699d | 2014-08-18 14:01:17 -0700 | [diff] [blame] | 611 | if (ret && ret != -EEXIST) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 612 | goto out; |
| 613 | |
| 614 | leaf = path->nodes[0]; |
| 615 | qgroup_limit = btrfs_item_ptr(leaf, path->slots[0], |
| 616 | struct btrfs_qgroup_limit_item); |
| 617 | btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0); |
| 618 | btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0); |
| 619 | btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0); |
| 620 | btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0); |
| 621 | btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0); |
| 622 | |
| 623 | btrfs_mark_buffer_dirty(leaf); |
| 624 | |
| 625 | ret = 0; |
| 626 | out: |
| 627 | btrfs_free_path(path); |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | static int del_qgroup_item(struct btrfs_trans_handle *trans, |
| 632 | struct btrfs_root *quota_root, u64 qgroupid) |
| 633 | { |
| 634 | int ret; |
| 635 | struct btrfs_path *path; |
| 636 | struct btrfs_key key; |
| 637 | |
| 638 | path = btrfs_alloc_path(); |
| 639 | if (!path) |
| 640 | return -ENOMEM; |
| 641 | |
| 642 | key.objectid = 0; |
| 643 | key.type = BTRFS_QGROUP_INFO_KEY; |
| 644 | key.offset = qgroupid; |
| 645 | ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1); |
| 646 | if (ret < 0) |
| 647 | goto out; |
| 648 | |
| 649 | if (ret > 0) { |
| 650 | ret = -ENOENT; |
| 651 | goto out; |
| 652 | } |
| 653 | |
| 654 | ret = btrfs_del_item(trans, quota_root, path); |
| 655 | if (ret) |
| 656 | goto out; |
| 657 | |
| 658 | btrfs_release_path(path); |
| 659 | |
| 660 | key.type = BTRFS_QGROUP_LIMIT_KEY; |
| 661 | ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1); |
| 662 | if (ret < 0) |
| 663 | goto out; |
| 664 | |
| 665 | if (ret > 0) { |
| 666 | ret = -ENOENT; |
| 667 | goto out; |
| 668 | } |
| 669 | |
| 670 | ret = btrfs_del_item(trans, quota_root, path); |
| 671 | |
| 672 | out: |
| 673 | btrfs_free_path(path); |
| 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | static int update_qgroup_limit_item(struct btrfs_trans_handle *trans, |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 678 | struct btrfs_root *root, |
| 679 | struct btrfs_qgroup *qgroup) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 680 | { |
| 681 | struct btrfs_path *path; |
| 682 | struct btrfs_key key; |
| 683 | struct extent_buffer *l; |
| 684 | struct btrfs_qgroup_limit_item *qgroup_limit; |
| 685 | int ret; |
| 686 | int slot; |
| 687 | |
| 688 | key.objectid = 0; |
| 689 | key.type = BTRFS_QGROUP_LIMIT_KEY; |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 690 | key.offset = qgroup->qgroupid; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 691 | |
| 692 | path = btrfs_alloc_path(); |
Wang Shilong | 84cbe2f | 2013-02-27 11:20:56 +0000 | [diff] [blame] | 693 | if (!path) |
| 694 | return -ENOMEM; |
| 695 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 696 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 697 | if (ret > 0) |
| 698 | ret = -ENOENT; |
| 699 | |
| 700 | if (ret) |
| 701 | goto out; |
| 702 | |
| 703 | l = path->nodes[0]; |
| 704 | slot = path->slots[0]; |
Valentina Giusti | a3df41e | 2013-11-04 22:34:29 +0100 | [diff] [blame] | 705 | qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item); |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 706 | btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags); |
| 707 | btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer); |
| 708 | btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl); |
| 709 | btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer); |
| 710 | btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 711 | |
| 712 | btrfs_mark_buffer_dirty(l); |
| 713 | |
| 714 | out: |
| 715 | btrfs_free_path(path); |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | static int update_qgroup_info_item(struct btrfs_trans_handle *trans, |
| 720 | struct btrfs_root *root, |
| 721 | struct btrfs_qgroup *qgroup) |
| 722 | { |
| 723 | struct btrfs_path *path; |
| 724 | struct btrfs_key key; |
| 725 | struct extent_buffer *l; |
| 726 | struct btrfs_qgroup_info_item *qgroup_info; |
| 727 | int ret; |
| 728 | int slot; |
| 729 | |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 730 | if (btrfs_test_is_dummy_root(root)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 731 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 732 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 733 | key.objectid = 0; |
| 734 | key.type = BTRFS_QGROUP_INFO_KEY; |
| 735 | key.offset = qgroup->qgroupid; |
| 736 | |
| 737 | path = btrfs_alloc_path(); |
Wang Shilong | 84cbe2f | 2013-02-27 11:20:56 +0000 | [diff] [blame] | 738 | if (!path) |
| 739 | return -ENOMEM; |
| 740 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 741 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 742 | if (ret > 0) |
| 743 | ret = -ENOENT; |
| 744 | |
| 745 | if (ret) |
| 746 | goto out; |
| 747 | |
| 748 | l = path->nodes[0]; |
| 749 | slot = path->slots[0]; |
Valentina Giusti | a3df41e | 2013-11-04 22:34:29 +0100 | [diff] [blame] | 750 | qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 751 | btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid); |
| 752 | btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer); |
| 753 | btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr); |
| 754 | btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl); |
| 755 | btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr); |
| 756 | |
| 757 | btrfs_mark_buffer_dirty(l); |
| 758 | |
| 759 | out: |
| 760 | btrfs_free_path(path); |
| 761 | return ret; |
| 762 | } |
| 763 | |
| 764 | static int update_qgroup_status_item(struct btrfs_trans_handle *trans, |
| 765 | struct btrfs_fs_info *fs_info, |
| 766 | struct btrfs_root *root) |
| 767 | { |
| 768 | struct btrfs_path *path; |
| 769 | struct btrfs_key key; |
| 770 | struct extent_buffer *l; |
| 771 | struct btrfs_qgroup_status_item *ptr; |
| 772 | int ret; |
| 773 | int slot; |
| 774 | |
| 775 | key.objectid = 0; |
| 776 | key.type = BTRFS_QGROUP_STATUS_KEY; |
| 777 | key.offset = 0; |
| 778 | |
| 779 | path = btrfs_alloc_path(); |
Wang Shilong | 84cbe2f | 2013-02-27 11:20:56 +0000 | [diff] [blame] | 780 | if (!path) |
| 781 | return -ENOMEM; |
| 782 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 783 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 784 | if (ret > 0) |
| 785 | ret = -ENOENT; |
| 786 | |
| 787 | if (ret) |
| 788 | goto out; |
| 789 | |
| 790 | l = path->nodes[0]; |
| 791 | slot = path->slots[0]; |
| 792 | ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item); |
| 793 | btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags); |
| 794 | btrfs_set_qgroup_status_generation(l, ptr, trans->transid); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 795 | btrfs_set_qgroup_status_rescan(l, ptr, |
| 796 | fs_info->qgroup_rescan_progress.objectid); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 797 | |
| 798 | btrfs_mark_buffer_dirty(l); |
| 799 | |
| 800 | out: |
| 801 | btrfs_free_path(path); |
| 802 | return ret; |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * called with qgroup_lock held |
| 807 | */ |
| 808 | static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans, |
| 809 | struct btrfs_root *root) |
| 810 | { |
| 811 | struct btrfs_path *path; |
| 812 | struct btrfs_key key; |
Wang Shilong | 06b3a86 | 2013-02-27 11:16:57 +0000 | [diff] [blame] | 813 | struct extent_buffer *leaf = NULL; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 814 | int ret; |
Wang Shilong | 06b3a86 | 2013-02-27 11:16:57 +0000 | [diff] [blame] | 815 | int nr = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 816 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 817 | path = btrfs_alloc_path(); |
| 818 | if (!path) |
| 819 | return -ENOMEM; |
| 820 | |
Wang Shilong | 06b3a86 | 2013-02-27 11:16:57 +0000 | [diff] [blame] | 821 | path->leave_spinning = 1; |
| 822 | |
| 823 | key.objectid = 0; |
| 824 | key.offset = 0; |
| 825 | key.type = 0; |
| 826 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 827 | while (1) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 828 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Wang Shilong | 06b3a86 | 2013-02-27 11:16:57 +0000 | [diff] [blame] | 829 | if (ret < 0) |
| 830 | goto out; |
| 831 | leaf = path->nodes[0]; |
| 832 | nr = btrfs_header_nritems(leaf); |
| 833 | if (!nr) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 834 | break; |
Wang Shilong | 06b3a86 | 2013-02-27 11:16:57 +0000 | [diff] [blame] | 835 | /* |
| 836 | * delete the leaf one by one |
| 837 | * since the whole tree is going |
| 838 | * to be deleted. |
| 839 | */ |
| 840 | path->slots[0] = 0; |
| 841 | ret = btrfs_del_items(trans, root, path, 0, nr); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 842 | if (ret) |
| 843 | goto out; |
Wang Shilong | 06b3a86 | 2013-02-27 11:16:57 +0000 | [diff] [blame] | 844 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 845 | btrfs_release_path(path); |
| 846 | } |
| 847 | ret = 0; |
| 848 | out: |
| 849 | root->fs_info->pending_quota_state = 0; |
| 850 | btrfs_free_path(path); |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | int btrfs_quota_enable(struct btrfs_trans_handle *trans, |
| 855 | struct btrfs_fs_info *fs_info) |
| 856 | { |
| 857 | struct btrfs_root *quota_root; |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 858 | struct btrfs_root *tree_root = fs_info->tree_root; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 859 | struct btrfs_path *path = NULL; |
| 860 | struct btrfs_qgroup_status_item *ptr; |
| 861 | struct extent_buffer *leaf; |
| 862 | struct btrfs_key key; |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 863 | struct btrfs_key found_key; |
| 864 | struct btrfs_qgroup *qgroup = NULL; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 865 | int ret = 0; |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 866 | int slot; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 867 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 868 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 869 | if (fs_info->quota_root) { |
| 870 | fs_info->pending_quota_state = 1; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 871 | goto out; |
| 872 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 873 | |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 874 | fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS); |
| 875 | if (!fs_info->qgroup_ulist) { |
| 876 | ret = -ENOMEM; |
| 877 | goto out; |
| 878 | } |
| 879 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 880 | /* |
| 881 | * initially create the quota tree |
| 882 | */ |
| 883 | quota_root = btrfs_create_tree(trans, fs_info, |
| 884 | BTRFS_QUOTA_TREE_OBJECTID); |
| 885 | if (IS_ERR(quota_root)) { |
| 886 | ret = PTR_ERR(quota_root); |
| 887 | goto out; |
| 888 | } |
| 889 | |
| 890 | path = btrfs_alloc_path(); |
Tsutomu Itoh | 5b7ff5b | 2012-10-16 05:44:21 +0000 | [diff] [blame] | 891 | if (!path) { |
| 892 | ret = -ENOMEM; |
| 893 | goto out_free_root; |
| 894 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 895 | |
| 896 | key.objectid = 0; |
| 897 | key.type = BTRFS_QGROUP_STATUS_KEY; |
| 898 | key.offset = 0; |
| 899 | |
| 900 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, |
| 901 | sizeof(*ptr)); |
| 902 | if (ret) |
Tsutomu Itoh | 5b7ff5b | 2012-10-16 05:44:21 +0000 | [diff] [blame] | 903 | goto out_free_path; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 904 | |
| 905 | leaf = path->nodes[0]; |
| 906 | ptr = btrfs_item_ptr(leaf, path->slots[0], |
| 907 | struct btrfs_qgroup_status_item); |
| 908 | btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid); |
| 909 | btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION); |
| 910 | fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON | |
| 911 | BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 912 | btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 913 | btrfs_set_qgroup_status_rescan(leaf, ptr, 0); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 914 | |
| 915 | btrfs_mark_buffer_dirty(leaf); |
| 916 | |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 917 | key.objectid = 0; |
| 918 | key.type = BTRFS_ROOT_REF_KEY; |
| 919 | key.offset = 0; |
| 920 | |
| 921 | btrfs_release_path(path); |
| 922 | ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0); |
| 923 | if (ret > 0) |
| 924 | goto out_add_root; |
| 925 | if (ret < 0) |
| 926 | goto out_free_path; |
| 927 | |
| 928 | |
| 929 | while (1) { |
| 930 | slot = path->slots[0]; |
| 931 | leaf = path->nodes[0]; |
| 932 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 933 | |
| 934 | if (found_key.type == BTRFS_ROOT_REF_KEY) { |
| 935 | ret = add_qgroup_item(trans, quota_root, |
| 936 | found_key.offset); |
| 937 | if (ret) |
| 938 | goto out_free_path; |
| 939 | |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 940 | qgroup = add_qgroup_rb(fs_info, found_key.offset); |
| 941 | if (IS_ERR(qgroup)) { |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 942 | ret = PTR_ERR(qgroup); |
| 943 | goto out_free_path; |
| 944 | } |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 945 | } |
| 946 | ret = btrfs_next_item(tree_root, path); |
| 947 | if (ret < 0) |
| 948 | goto out_free_path; |
| 949 | if (ret) |
| 950 | break; |
| 951 | } |
| 952 | |
| 953 | out_add_root: |
| 954 | btrfs_release_path(path); |
| 955 | ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID); |
| 956 | if (ret) |
| 957 | goto out_free_path; |
| 958 | |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 959 | qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID); |
| 960 | if (IS_ERR(qgroup)) { |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 961 | ret = PTR_ERR(qgroup); |
| 962 | goto out_free_path; |
| 963 | } |
Wang Shilong | 58400fc | 2013-04-07 10:50:17 +0000 | [diff] [blame] | 964 | spin_lock(&fs_info->qgroup_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 965 | fs_info->quota_root = quota_root; |
| 966 | fs_info->pending_quota_state = 1; |
| 967 | spin_unlock(&fs_info->qgroup_lock); |
Tsutomu Itoh | 5b7ff5b | 2012-10-16 05:44:21 +0000 | [diff] [blame] | 968 | out_free_path: |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 969 | btrfs_free_path(path); |
Tsutomu Itoh | 5b7ff5b | 2012-10-16 05:44:21 +0000 | [diff] [blame] | 970 | out_free_root: |
| 971 | if (ret) { |
| 972 | free_extent_buffer(quota_root->node); |
| 973 | free_extent_buffer(quota_root->commit_root); |
| 974 | kfree(quota_root); |
| 975 | } |
| 976 | out: |
Jan Schmidt | eb1716a | 2013-05-28 15:47:23 +0000 | [diff] [blame] | 977 | if (ret) { |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 978 | ulist_free(fs_info->qgroup_ulist); |
Jan Schmidt | eb1716a | 2013-05-28 15:47:23 +0000 | [diff] [blame] | 979 | fs_info->qgroup_ulist = NULL; |
| 980 | } |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 981 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 982 | return ret; |
| 983 | } |
| 984 | |
| 985 | int btrfs_quota_disable(struct btrfs_trans_handle *trans, |
| 986 | struct btrfs_fs_info *fs_info) |
| 987 | { |
| 988 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 989 | struct btrfs_root *quota_root; |
| 990 | int ret = 0; |
| 991 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 992 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Wang Shilong | 58400fc | 2013-04-07 10:50:17 +0000 | [diff] [blame] | 993 | if (!fs_info->quota_root) |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 994 | goto out; |
Wang Shilong | 58400fc | 2013-04-07 10:50:17 +0000 | [diff] [blame] | 995 | spin_lock(&fs_info->qgroup_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 996 | fs_info->quota_enabled = 0; |
| 997 | fs_info->pending_quota_state = 0; |
| 998 | quota_root = fs_info->quota_root; |
| 999 | fs_info->quota_root = NULL; |
Dongsheng Yang | 8ea0ec9 | 2015-02-27 16:24:26 +0800 | [diff] [blame] | 1000 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1001 | spin_unlock(&fs_info->qgroup_lock); |
| 1002 | |
Wang Shilong | e685da1 | 2013-08-14 09:13:37 +0800 | [diff] [blame] | 1003 | btrfs_free_qgroup_config(fs_info); |
| 1004 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1005 | ret = btrfs_clean_quota_tree(trans, quota_root); |
| 1006 | if (ret) |
| 1007 | goto out; |
| 1008 | |
| 1009 | ret = btrfs_del_root(trans, tree_root, "a_root->root_key); |
| 1010 | if (ret) |
| 1011 | goto out; |
| 1012 | |
| 1013 | list_del("a_root->dirty_list); |
| 1014 | |
| 1015 | btrfs_tree_lock(quota_root->node); |
Daniel Dressler | 01d5847 | 2014-11-21 17:15:07 +0900 | [diff] [blame] | 1016 | clean_tree_block(trans, tree_root->fs_info, quota_root->node); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1017 | btrfs_tree_unlock(quota_root->node); |
| 1018 | btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1); |
| 1019 | |
| 1020 | free_extent_buffer(quota_root->node); |
| 1021 | free_extent_buffer(quota_root->commit_root); |
| 1022 | kfree(quota_root); |
| 1023 | out: |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1024 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1025 | return ret; |
| 1026 | } |
| 1027 | |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 1028 | static void qgroup_dirty(struct btrfs_fs_info *fs_info, |
| 1029 | struct btrfs_qgroup *qgroup) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1030 | { |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 1031 | if (list_empty(&qgroup->dirty)) |
| 1032 | list_add(&qgroup->dirty, &fs_info->dirty_qgroups); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1035 | /* |
| 1036 | * The easy accounting, if we are adding/removing the only ref for an extent |
| 1037 | * then this qgroup and all of the parent qgroups get their refrence and |
| 1038 | * exclusive counts adjusted. |
| 1039 | * |
| 1040 | * Caller should hold fs_info->qgroup_lock. |
| 1041 | */ |
| 1042 | static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info, |
| 1043 | struct ulist *tmp, u64 ref_root, |
| 1044 | u64 num_bytes, int sign) |
| 1045 | { |
| 1046 | struct btrfs_qgroup *qgroup; |
| 1047 | struct btrfs_qgroup_list *glist; |
| 1048 | struct ulist_node *unode; |
| 1049 | struct ulist_iterator uiter; |
| 1050 | int ret = 0; |
| 1051 | |
| 1052 | qgroup = find_qgroup_rb(fs_info, ref_root); |
| 1053 | if (!qgroup) |
| 1054 | goto out; |
| 1055 | |
| 1056 | qgroup->rfer += sign * num_bytes; |
| 1057 | qgroup->rfer_cmpr += sign * num_bytes; |
| 1058 | |
| 1059 | WARN_ON(sign < 0 && qgroup->excl < num_bytes); |
| 1060 | qgroup->excl += sign * num_bytes; |
| 1061 | qgroup->excl_cmpr += sign * num_bytes; |
| 1062 | if (sign > 0) |
| 1063 | qgroup->reserved -= num_bytes; |
| 1064 | |
| 1065 | qgroup_dirty(fs_info, qgroup); |
| 1066 | |
| 1067 | /* Get all of the parent groups that contain this qgroup */ |
| 1068 | list_for_each_entry(glist, &qgroup->groups, next_group) { |
| 1069 | ret = ulist_add(tmp, glist->group->qgroupid, |
| 1070 | ptr_to_u64(glist->group), GFP_ATOMIC); |
| 1071 | if (ret < 0) |
| 1072 | goto out; |
| 1073 | } |
| 1074 | |
| 1075 | /* Iterate all of the parents and adjust their reference counts */ |
| 1076 | ULIST_ITER_INIT(&uiter); |
| 1077 | while ((unode = ulist_next(tmp, &uiter))) { |
| 1078 | qgroup = u64_to_ptr(unode->aux); |
| 1079 | qgroup->rfer += sign * num_bytes; |
| 1080 | qgroup->rfer_cmpr += sign * num_bytes; |
| 1081 | WARN_ON(sign < 0 && qgroup->excl < num_bytes); |
| 1082 | qgroup->excl += sign * num_bytes; |
| 1083 | if (sign > 0) |
| 1084 | qgroup->reserved -= num_bytes; |
| 1085 | qgroup->excl_cmpr += sign * num_bytes; |
| 1086 | qgroup_dirty(fs_info, qgroup); |
| 1087 | |
| 1088 | /* Add any parents of the parents */ |
| 1089 | list_for_each_entry(glist, &qgroup->groups, next_group) { |
| 1090 | ret = ulist_add(tmp, glist->group->qgroupid, |
| 1091 | ptr_to_u64(glist->group), GFP_ATOMIC); |
| 1092 | if (ret < 0) |
| 1093 | goto out; |
| 1094 | } |
| 1095 | } |
| 1096 | ret = 0; |
| 1097 | out: |
| 1098 | return ret; |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | /* |
| 1103 | * Quick path for updating qgroup with only excl refs. |
| 1104 | * |
| 1105 | * In that case, just update all parent will be enough. |
| 1106 | * Or we needs to do a full rescan. |
| 1107 | * Caller should also hold fs_info->qgroup_lock. |
| 1108 | * |
| 1109 | * Return 0 for quick update, return >0 for need to full rescan |
| 1110 | * and mark INCONSISTENT flag. |
| 1111 | * Return < 0 for other error. |
| 1112 | */ |
| 1113 | static int quick_update_accounting(struct btrfs_fs_info *fs_info, |
| 1114 | struct ulist *tmp, u64 src, u64 dst, |
| 1115 | int sign) |
| 1116 | { |
| 1117 | struct btrfs_qgroup *qgroup; |
| 1118 | int ret = 1; |
| 1119 | int err = 0; |
| 1120 | |
| 1121 | qgroup = find_qgroup_rb(fs_info, src); |
| 1122 | if (!qgroup) |
| 1123 | goto out; |
| 1124 | if (qgroup->excl == qgroup->rfer) { |
| 1125 | ret = 0; |
| 1126 | err = __qgroup_excl_accounting(fs_info, tmp, dst, |
| 1127 | qgroup->excl, sign); |
| 1128 | if (err < 0) { |
| 1129 | ret = err; |
| 1130 | goto out; |
| 1131 | } |
| 1132 | } |
| 1133 | out: |
| 1134 | if (ret) |
| 1135 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 1136 | return ret; |
| 1137 | } |
| 1138 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1139 | int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, |
| 1140 | struct btrfs_fs_info *fs_info, u64 src, u64 dst) |
| 1141 | { |
| 1142 | struct btrfs_root *quota_root; |
Wang Shilong | b7fef4f | 2013-04-07 10:50:18 +0000 | [diff] [blame] | 1143 | struct btrfs_qgroup *parent; |
| 1144 | struct btrfs_qgroup *member; |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1145 | struct btrfs_qgroup_list *list; |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1146 | struct ulist *tmp; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1147 | int ret = 0; |
| 1148 | |
Qu Wenruo | 8465ece | 2015-02-27 16:24:22 +0800 | [diff] [blame] | 1149 | /* Check the level of src and dst first */ |
| 1150 | if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst)) |
| 1151 | return -EINVAL; |
| 1152 | |
Christian Engelmayer | ab3680d | 2015-05-02 17:19:55 +0200 | [diff] [blame] | 1153 | tmp = ulist_alloc(GFP_NOFS); |
| 1154 | if (!tmp) |
| 1155 | return -ENOMEM; |
| 1156 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1157 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1158 | quota_root = fs_info->quota_root; |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1159 | if (!quota_root) { |
| 1160 | ret = -EINVAL; |
| 1161 | goto out; |
| 1162 | } |
Wang Shilong | b7fef4f | 2013-04-07 10:50:18 +0000 | [diff] [blame] | 1163 | member = find_qgroup_rb(fs_info, src); |
| 1164 | parent = find_qgroup_rb(fs_info, dst); |
| 1165 | if (!member || !parent) { |
| 1166 | ret = -EINVAL; |
| 1167 | goto out; |
| 1168 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1169 | |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1170 | /* check if such qgroup relation exist firstly */ |
| 1171 | list_for_each_entry(list, &member->groups, next_group) { |
| 1172 | if (list->group == parent) { |
| 1173 | ret = -EEXIST; |
| 1174 | goto out; |
| 1175 | } |
| 1176 | } |
| 1177 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1178 | ret = add_qgroup_relation_item(trans, quota_root, src, dst); |
| 1179 | if (ret) |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1180 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1181 | |
| 1182 | ret = add_qgroup_relation_item(trans, quota_root, dst, src); |
| 1183 | if (ret) { |
| 1184 | del_qgroup_relation_item(trans, quota_root, src, dst); |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1185 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | spin_lock(&fs_info->qgroup_lock); |
| 1189 | ret = add_relation_rb(quota_root->fs_info, src, dst); |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1190 | if (ret < 0) { |
| 1191 | spin_unlock(&fs_info->qgroup_lock); |
| 1192 | goto out; |
| 1193 | } |
| 1194 | ret = quick_update_accounting(fs_info, tmp, src, dst, 1); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1195 | spin_unlock(&fs_info->qgroup_lock); |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1196 | out: |
| 1197 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1198 | ulist_free(tmp); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1199 | return ret; |
| 1200 | } |
| 1201 | |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1202 | int __del_qgroup_relation(struct btrfs_trans_handle *trans, |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1203 | struct btrfs_fs_info *fs_info, u64 src, u64 dst) |
| 1204 | { |
| 1205 | struct btrfs_root *quota_root; |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1206 | struct btrfs_qgroup *parent; |
| 1207 | struct btrfs_qgroup *member; |
| 1208 | struct btrfs_qgroup_list *list; |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1209 | struct ulist *tmp; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1210 | int ret = 0; |
| 1211 | int err; |
| 1212 | |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1213 | tmp = ulist_alloc(GFP_NOFS); |
| 1214 | if (!tmp) |
| 1215 | return -ENOMEM; |
| 1216 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1217 | quota_root = fs_info->quota_root; |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1218 | if (!quota_root) { |
| 1219 | ret = -EINVAL; |
| 1220 | goto out; |
| 1221 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1222 | |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1223 | member = find_qgroup_rb(fs_info, src); |
| 1224 | parent = find_qgroup_rb(fs_info, dst); |
| 1225 | if (!member || !parent) { |
| 1226 | ret = -EINVAL; |
| 1227 | goto out; |
| 1228 | } |
| 1229 | |
| 1230 | /* check if such qgroup relation exist firstly */ |
| 1231 | list_for_each_entry(list, &member->groups, next_group) { |
| 1232 | if (list->group == parent) |
| 1233 | goto exist; |
| 1234 | } |
| 1235 | ret = -ENOENT; |
| 1236 | goto out; |
| 1237 | exist: |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1238 | ret = del_qgroup_relation_item(trans, quota_root, src, dst); |
| 1239 | err = del_qgroup_relation_item(trans, quota_root, dst, src); |
| 1240 | if (err && !ret) |
| 1241 | ret = err; |
| 1242 | |
| 1243 | spin_lock(&fs_info->qgroup_lock); |
| 1244 | del_relation_rb(fs_info, src, dst); |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1245 | ret = quick_update_accounting(fs_info, tmp, src, dst, -1); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1246 | spin_unlock(&fs_info->qgroup_lock); |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1247 | out: |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1248 | ulist_free(tmp); |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1249 | return ret; |
| 1250 | } |
| 1251 | |
| 1252 | int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, |
| 1253 | struct btrfs_fs_info *fs_info, u64 src, u64 dst) |
| 1254 | { |
| 1255 | int ret = 0; |
| 1256 | |
| 1257 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
| 1258 | ret = __del_qgroup_relation(trans, fs_info, src, dst); |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1259 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1260 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1261 | return ret; |
| 1262 | } |
| 1263 | |
| 1264 | int btrfs_create_qgroup(struct btrfs_trans_handle *trans, |
Dongsheng Yang | 4087cf2 | 2015-01-18 10:59:23 -0500 | [diff] [blame] | 1265 | struct btrfs_fs_info *fs_info, u64 qgroupid) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1266 | { |
| 1267 | struct btrfs_root *quota_root; |
| 1268 | struct btrfs_qgroup *qgroup; |
| 1269 | int ret = 0; |
| 1270 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1271 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1272 | quota_root = fs_info->quota_root; |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1273 | if (!quota_root) { |
| 1274 | ret = -EINVAL; |
| 1275 | goto out; |
| 1276 | } |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1277 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
| 1278 | if (qgroup) { |
| 1279 | ret = -EEXIST; |
| 1280 | goto out; |
| 1281 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1282 | |
| 1283 | ret = add_qgroup_item(trans, quota_root, qgroupid); |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1284 | if (ret) |
| 1285 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1286 | |
| 1287 | spin_lock(&fs_info->qgroup_lock); |
| 1288 | qgroup = add_qgroup_rb(fs_info, qgroupid); |
| 1289 | spin_unlock(&fs_info->qgroup_lock); |
| 1290 | |
| 1291 | if (IS_ERR(qgroup)) |
| 1292 | ret = PTR_ERR(qgroup); |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1293 | out: |
| 1294 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1295 | return ret; |
| 1296 | } |
| 1297 | |
| 1298 | int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, |
| 1299 | struct btrfs_fs_info *fs_info, u64 qgroupid) |
| 1300 | { |
| 1301 | struct btrfs_root *quota_root; |
Arne Jansen | 2cf6870 | 2013-01-17 01:22:09 -0700 | [diff] [blame] | 1302 | struct btrfs_qgroup *qgroup; |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1303 | struct btrfs_qgroup_list *list; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1304 | int ret = 0; |
| 1305 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1306 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1307 | quota_root = fs_info->quota_root; |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1308 | if (!quota_root) { |
| 1309 | ret = -EINVAL; |
| 1310 | goto out; |
| 1311 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1312 | |
Arne Jansen | 2cf6870 | 2013-01-17 01:22:09 -0700 | [diff] [blame] | 1313 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1314 | if (!qgroup) { |
| 1315 | ret = -ENOENT; |
| 1316 | goto out; |
| 1317 | } else { |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1318 | /* check if there are no children of this qgroup */ |
| 1319 | if (!list_empty(&qgroup->members)) { |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1320 | ret = -EBUSY; |
| 1321 | goto out; |
Arne Jansen | 2cf6870 | 2013-01-17 01:22:09 -0700 | [diff] [blame] | 1322 | } |
| 1323 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1324 | ret = del_qgroup_item(trans, quota_root, qgroupid); |
| 1325 | |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1326 | while (!list_empty(&qgroup->groups)) { |
| 1327 | list = list_first_entry(&qgroup->groups, |
| 1328 | struct btrfs_qgroup_list, next_group); |
| 1329 | ret = __del_qgroup_relation(trans, fs_info, |
| 1330 | qgroupid, |
| 1331 | list->group->qgroupid); |
| 1332 | if (ret) |
| 1333 | goto out; |
| 1334 | } |
| 1335 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1336 | spin_lock(&fs_info->qgroup_lock); |
| 1337 | del_qgroup_rb(quota_root->fs_info, qgroupid); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1338 | spin_unlock(&fs_info->qgroup_lock); |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1339 | out: |
| 1340 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1341 | return ret; |
| 1342 | } |
| 1343 | |
| 1344 | int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, |
| 1345 | struct btrfs_fs_info *fs_info, u64 qgroupid, |
| 1346 | struct btrfs_qgroup_limit *limit) |
| 1347 | { |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1348 | struct btrfs_root *quota_root; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1349 | struct btrfs_qgroup *qgroup; |
| 1350 | int ret = 0; |
| 1351 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1352 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
| 1353 | quota_root = fs_info->quota_root; |
| 1354 | if (!quota_root) { |
| 1355 | ret = -EINVAL; |
| 1356 | goto out; |
| 1357 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1358 | |
Wang Shilong | ddb47af | 2013-04-07 10:50:20 +0000 | [diff] [blame] | 1359 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
| 1360 | if (!qgroup) { |
| 1361 | ret = -ENOENT; |
| 1362 | goto out; |
| 1363 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1364 | |
Wang Shilong | 58400fc | 2013-04-07 10:50:17 +0000 | [diff] [blame] | 1365 | spin_lock(&fs_info->qgroup_lock); |
Dongsheng Yang | 03477d9 | 2015-02-06 11:06:25 -0500 | [diff] [blame] | 1366 | if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) |
| 1367 | qgroup->max_rfer = limit->max_rfer; |
| 1368 | if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) |
| 1369 | qgroup->max_excl = limit->max_excl; |
| 1370 | if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) |
| 1371 | qgroup->rsv_rfer = limit->rsv_rfer; |
| 1372 | if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) |
| 1373 | qgroup->rsv_excl = limit->rsv_excl; |
| 1374 | qgroup->lim_flags |= limit->flags; |
| 1375 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1376 | spin_unlock(&fs_info->qgroup_lock); |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 1377 | |
| 1378 | ret = update_qgroup_limit_item(trans, quota_root, qgroup); |
| 1379 | if (ret) { |
| 1380 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 1381 | btrfs_info(fs_info, "unable to update quota limit for %llu", |
| 1382 | qgroupid); |
| 1383 | } |
| 1384 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1385 | out: |
| 1386 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1387 | return ret; |
| 1388 | } |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 1389 | |
| 1390 | static int comp_oper_exist(struct btrfs_qgroup_operation *oper1, |
| 1391 | struct btrfs_qgroup_operation *oper2) |
| 1392 | { |
| 1393 | /* |
| 1394 | * Ignore seq and type here, we're looking for any operation |
| 1395 | * at all related to this extent on that root. |
| 1396 | */ |
| 1397 | if (oper1->bytenr < oper2->bytenr) |
| 1398 | return -1; |
| 1399 | if (oper1->bytenr > oper2->bytenr) |
| 1400 | return 1; |
| 1401 | if (oper1->ref_root < oper2->ref_root) |
| 1402 | return -1; |
| 1403 | if (oper1->ref_root > oper2->ref_root) |
| 1404 | return 1; |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
| 1408 | static int qgroup_oper_exists(struct btrfs_fs_info *fs_info, |
| 1409 | struct btrfs_qgroup_operation *oper) |
| 1410 | { |
| 1411 | struct rb_node *n; |
| 1412 | struct btrfs_qgroup_operation *cur; |
| 1413 | int cmp; |
| 1414 | |
| 1415 | spin_lock(&fs_info->qgroup_op_lock); |
| 1416 | n = fs_info->qgroup_op_tree.rb_node; |
| 1417 | while (n) { |
| 1418 | cur = rb_entry(n, struct btrfs_qgroup_operation, n); |
| 1419 | cmp = comp_oper_exist(cur, oper); |
| 1420 | if (cmp < 0) { |
| 1421 | n = n->rb_right; |
| 1422 | } else if (cmp) { |
| 1423 | n = n->rb_left; |
| 1424 | } else { |
| 1425 | spin_unlock(&fs_info->qgroup_op_lock); |
| 1426 | return -EEXIST; |
| 1427 | } |
| 1428 | } |
| 1429 | spin_unlock(&fs_info->qgroup_op_lock); |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1433 | static int comp_oper(struct btrfs_qgroup_operation *oper1, |
| 1434 | struct btrfs_qgroup_operation *oper2) |
| 1435 | { |
| 1436 | if (oper1->bytenr < oper2->bytenr) |
| 1437 | return -1; |
| 1438 | if (oper1->bytenr > oper2->bytenr) |
| 1439 | return 1; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1440 | if (oper1->ref_root < oper2->ref_root) |
| 1441 | return -1; |
| 1442 | if (oper1->ref_root > oper2->ref_root) |
| 1443 | return 1; |
Filipe Manana | bf69196 | 2015-03-14 07:03:27 +0000 | [diff] [blame] | 1444 | if (oper1->seq < oper2->seq) |
| 1445 | return -1; |
| 1446 | if (oper1->seq > oper2->seq) |
| 1447 | return 1; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1448 | if (oper1->type < oper2->type) |
| 1449 | return -1; |
| 1450 | if (oper1->type > oper2->type) |
| 1451 | return 1; |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
| 1455 | static int insert_qgroup_oper(struct btrfs_fs_info *fs_info, |
| 1456 | struct btrfs_qgroup_operation *oper) |
| 1457 | { |
| 1458 | struct rb_node **p; |
| 1459 | struct rb_node *parent = NULL; |
| 1460 | struct btrfs_qgroup_operation *cur; |
| 1461 | int cmp; |
| 1462 | |
| 1463 | spin_lock(&fs_info->qgroup_op_lock); |
| 1464 | p = &fs_info->qgroup_op_tree.rb_node; |
| 1465 | while (*p) { |
| 1466 | parent = *p; |
| 1467 | cur = rb_entry(parent, struct btrfs_qgroup_operation, n); |
| 1468 | cmp = comp_oper(cur, oper); |
| 1469 | if (cmp < 0) { |
| 1470 | p = &(*p)->rb_right; |
| 1471 | } else if (cmp) { |
| 1472 | p = &(*p)->rb_left; |
| 1473 | } else { |
| 1474 | spin_unlock(&fs_info->qgroup_op_lock); |
| 1475 | return -EEXIST; |
| 1476 | } |
| 1477 | } |
| 1478 | rb_link_node(&oper->n, parent, p); |
| 1479 | rb_insert_color(&oper->n, &fs_info->qgroup_op_tree); |
| 1480 | spin_unlock(&fs_info->qgroup_op_lock); |
| 1481 | return 0; |
| 1482 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1483 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1484 | /* |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1485 | * Record a quota operation for processing later on. |
| 1486 | * @trans: the transaction we are adding the delayed op to. |
| 1487 | * @fs_info: the fs_info for this fs. |
| 1488 | * @ref_root: the root of the reference we are acting on, |
| 1489 | * @bytenr: the bytenr we are acting on. |
| 1490 | * @num_bytes: the number of bytes in the reference. |
| 1491 | * @type: the type of operation this is. |
| 1492 | * @mod_seq: do we need to get a sequence number for looking up roots. |
| 1493 | * |
| 1494 | * We just add it to our trans qgroup_ref_list and carry on and process these |
| 1495 | * operations in order at some later point. If the reference root isn't a fs |
| 1496 | * root then we don't bother with doing anything. |
| 1497 | * |
| 1498 | * MUST BE HOLDING THE REF LOCK. |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1499 | */ |
| 1500 | int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1501 | struct btrfs_fs_info *fs_info, u64 ref_root, |
| 1502 | u64 bytenr, u64 num_bytes, |
| 1503 | enum btrfs_qgroup_operation_type type, int mod_seq) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1504 | { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1505 | struct btrfs_qgroup_operation *oper; |
| 1506 | int ret; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1507 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1508 | if (!is_fstree(ref_root) || !fs_info->quota_enabled) |
| 1509 | return 0; |
| 1510 | |
| 1511 | oper = kmalloc(sizeof(*oper), GFP_NOFS); |
| 1512 | if (!oper) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1513 | return -ENOMEM; |
| 1514 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1515 | oper->ref_root = ref_root; |
| 1516 | oper->bytenr = bytenr; |
| 1517 | oper->num_bytes = num_bytes; |
| 1518 | oper->type = type; |
| 1519 | oper->seq = atomic_inc_return(&fs_info->qgroup_op_seq); |
| 1520 | INIT_LIST_HEAD(&oper->elem.list); |
| 1521 | oper->elem.seq = 0; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 1522 | |
Mark Fasheh | d398210 | 2014-07-17 12:39:00 -0700 | [diff] [blame] | 1523 | trace_btrfs_qgroup_record_ref(oper); |
| 1524 | |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 1525 | if (type == BTRFS_QGROUP_OPER_SUB_SUBTREE) { |
| 1526 | /* |
| 1527 | * If any operation for this bytenr/ref_root combo |
| 1528 | * exists, then we know it's not exclusively owned and |
| 1529 | * shouldn't be queued up. |
| 1530 | * |
| 1531 | * This also catches the case where we have a cloned |
| 1532 | * extent that gets queued up multiple times during |
| 1533 | * drop snapshot. |
| 1534 | */ |
| 1535 | if (qgroup_oper_exists(fs_info, oper)) { |
| 1536 | kfree(oper); |
| 1537 | return 0; |
| 1538 | } |
| 1539 | } |
| 1540 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1541 | ret = insert_qgroup_oper(fs_info, oper); |
| 1542 | if (ret) { |
| 1543 | /* Shouldn't happen so have an assert for developers */ |
| 1544 | ASSERT(0); |
| 1545 | kfree(oper); |
| 1546 | return ret; |
| 1547 | } |
| 1548 | list_add_tail(&oper->list, &trans->qgroup_ref_list); |
| 1549 | |
| 1550 | if (mod_seq) |
| 1551 | btrfs_get_tree_mod_seq(fs_info, &oper->elem); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1552 | |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
Qu Wenruo | 3b7d00f | 2015-04-16 16:40:39 +0800 | [diff] [blame] | 1556 | int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans, |
| 1557 | struct btrfs_fs_info *fs_info) |
| 1558 | { |
| 1559 | struct btrfs_qgroup_extent_record *record; |
| 1560 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1561 | struct rb_node *node; |
| 1562 | int ret = 0; |
| 1563 | |
| 1564 | delayed_refs = &trans->transaction->delayed_refs; |
| 1565 | |
| 1566 | /* |
| 1567 | * No need to do lock, since this function will only be called in |
| 1568 | * btrfs_commmit_transaction(). |
| 1569 | */ |
| 1570 | node = rb_first(&delayed_refs->dirty_extent_root); |
| 1571 | while (node) { |
| 1572 | record = rb_entry(node, struct btrfs_qgroup_extent_record, |
| 1573 | node); |
| 1574 | ret = btrfs_find_all_roots(NULL, fs_info, record->bytenr, 0, |
| 1575 | &record->old_roots); |
| 1576 | if (ret < 0) |
| 1577 | break; |
| 1578 | node = rb_next(node); |
| 1579 | } |
| 1580 | return ret; |
| 1581 | } |
| 1582 | |
Qu Wenruo | 3368d00 | 2015-04-16 14:34:17 +0800 | [diff] [blame] | 1583 | struct btrfs_qgroup_extent_record |
| 1584 | *btrfs_qgroup_insert_dirty_extent(struct btrfs_delayed_ref_root *delayed_refs, |
| 1585 | struct btrfs_qgroup_extent_record *record) |
| 1586 | { |
| 1587 | struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node; |
| 1588 | struct rb_node *parent_node = NULL; |
| 1589 | struct btrfs_qgroup_extent_record *entry; |
| 1590 | u64 bytenr = record->bytenr; |
| 1591 | |
| 1592 | while (*p) { |
| 1593 | parent_node = *p; |
| 1594 | entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record, |
| 1595 | node); |
| 1596 | if (bytenr < entry->bytenr) |
| 1597 | p = &(*p)->rb_left; |
| 1598 | else if (bytenr > entry->bytenr) |
| 1599 | p = &(*p)->rb_right; |
| 1600 | else |
| 1601 | return entry; |
| 1602 | } |
| 1603 | |
| 1604 | rb_link_node(&record->node, parent_node, p); |
| 1605 | rb_insert_color(&record->node, &delayed_refs->dirty_extent_root); |
| 1606 | return NULL; |
| 1607 | } |
| 1608 | |
| 1609 | /* |
| 1610 | * The easy accounting, if we are adding/removing the only ref for an extent |
| 1611 | * then this qgroup and all of the parent qgroups get their refrence and |
| 1612 | * exclusive counts adjusted. |
| 1613 | */ |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1614 | static int qgroup_excl_accounting(struct btrfs_fs_info *fs_info, |
| 1615 | struct btrfs_qgroup_operation *oper) |
| 1616 | { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1617 | struct ulist *tmp; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1618 | int sign = 0; |
| 1619 | int ret = 0; |
| 1620 | |
| 1621 | tmp = ulist_alloc(GFP_NOFS); |
| 1622 | if (!tmp) |
| 1623 | return -ENOMEM; |
| 1624 | |
| 1625 | spin_lock(&fs_info->qgroup_lock); |
| 1626 | if (!fs_info->quota_root) |
| 1627 | goto out; |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1628 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1629 | switch (oper->type) { |
| 1630 | case BTRFS_QGROUP_OPER_ADD_EXCL: |
| 1631 | sign = 1; |
| 1632 | break; |
| 1633 | case BTRFS_QGROUP_OPER_SUB_EXCL: |
| 1634 | sign = -1; |
| 1635 | break; |
| 1636 | default: |
| 1637 | ASSERT(0); |
| 1638 | } |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1639 | ret = __qgroup_excl_accounting(fs_info, tmp, oper->ref_root, |
| 1640 | oper->num_bytes, sign); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1641 | out: |
| 1642 | spin_unlock(&fs_info->qgroup_lock); |
| 1643 | ulist_free(tmp); |
| 1644 | return ret; |
| 1645 | } |
| 1646 | |
| 1647 | /* |
| 1648 | * Walk all of the roots that pointed to our bytenr and adjust their refcnts as |
| 1649 | * properly. |
| 1650 | */ |
| 1651 | static int qgroup_calc_old_refcnt(struct btrfs_fs_info *fs_info, |
| 1652 | u64 root_to_skip, struct ulist *tmp, |
| 1653 | struct ulist *roots, struct ulist *qgroups, |
| 1654 | u64 seq, int *old_roots, int rescan) |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1655 | { |
| 1656 | struct ulist_node *unode; |
| 1657 | struct ulist_iterator uiter; |
| 1658 | struct ulist_node *tmp_unode; |
| 1659 | struct ulist_iterator tmp_uiter; |
| 1660 | struct btrfs_qgroup *qg; |
| 1661 | int ret; |
| 1662 | |
| 1663 | ULIST_ITER_INIT(&uiter); |
| 1664 | while ((unode = ulist_next(roots, &uiter))) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1665 | /* We don't count our current root here */ |
| 1666 | if (unode->val == root_to_skip) |
| 1667 | continue; |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1668 | qg = find_qgroup_rb(fs_info, unode->val); |
| 1669 | if (!qg) |
| 1670 | continue; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1671 | /* |
| 1672 | * We could have a pending removal of this same ref so we may |
| 1673 | * not have actually found our ref root when doing |
| 1674 | * btrfs_find_all_roots, so we need to keep track of how many |
| 1675 | * old roots we find in case we removed ours and added a |
| 1676 | * different one at the same time. I don't think this could |
| 1677 | * happen in practice but that sort of thinking leads to pain |
| 1678 | * and suffering and to the dark side. |
| 1679 | */ |
| 1680 | (*old_roots)++; |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1681 | |
| 1682 | ulist_reinit(tmp); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1683 | ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg), |
| 1684 | GFP_ATOMIC); |
| 1685 | if (ret < 0) |
| 1686 | return ret; |
| 1687 | ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC); |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1688 | if (ret < 0) |
| 1689 | return ret; |
| 1690 | ULIST_ITER_INIT(&tmp_uiter); |
| 1691 | while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) { |
| 1692 | struct btrfs_qgroup_list *glist; |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 1693 | int mod; |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1694 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1695 | qg = u64_to_ptr(tmp_unode->aux); |
| 1696 | /* |
| 1697 | * We use this sequence number to keep from having to |
| 1698 | * run the whole list and 0 out the refcnt every time. |
| 1699 | * We basically use sequnce as the known 0 count and |
| 1700 | * then add 1 everytime we see a qgroup. This is how we |
| 1701 | * get how many of the roots actually point up to the |
| 1702 | * upper level qgroups in order to determine exclusive |
| 1703 | * counts. |
| 1704 | * |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 1705 | * For rescan none of the extent is recorded before so |
| 1706 | * we just don't add old_refcnt. |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1707 | */ |
| 1708 | if (rescan) |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 1709 | mod = 0; |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1710 | else |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 1711 | mod = 1; |
| 1712 | btrfs_qgroup_update_old_refcnt(qg, seq, mod); |
| 1713 | btrfs_qgroup_update_new_refcnt(qg, seq, 1); |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1714 | list_for_each_entry(glist, &qg->groups, next_group) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1715 | ret = ulist_add(qgroups, glist->group->qgroupid, |
| 1716 | ptr_to_u64(glist->group), |
| 1717 | GFP_ATOMIC); |
| 1718 | if (ret < 0) |
| 1719 | return ret; |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1720 | ret = ulist_add(tmp, glist->group->qgroupid, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1721 | ptr_to_u64(glist->group), |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1722 | GFP_ATOMIC); |
| 1723 | if (ret < 0) |
| 1724 | return ret; |
| 1725 | } |
| 1726 | } |
| 1727 | } |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1728 | return 0; |
| 1729 | } |
| 1730 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1731 | /* |
| 1732 | * We need to walk forward in our operation tree and account for any roots that |
| 1733 | * were deleted after we made this operation. |
| 1734 | */ |
| 1735 | static int qgroup_account_deleted_refs(struct btrfs_fs_info *fs_info, |
| 1736 | struct btrfs_qgroup_operation *oper, |
| 1737 | struct ulist *tmp, |
| 1738 | struct ulist *qgroups, u64 seq, |
| 1739 | int *old_roots) |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1740 | { |
| 1741 | struct ulist_node *unode; |
| 1742 | struct ulist_iterator uiter; |
| 1743 | struct btrfs_qgroup *qg; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1744 | struct btrfs_qgroup_operation *tmp_oper; |
| 1745 | struct rb_node *n; |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1746 | int ret; |
| 1747 | |
| 1748 | ulist_reinit(tmp); |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1749 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1750 | /* |
| 1751 | * We only walk forward in the tree since we're only interested in |
| 1752 | * removals that happened _after_ our operation. |
| 1753 | */ |
| 1754 | spin_lock(&fs_info->qgroup_op_lock); |
| 1755 | n = rb_next(&oper->n); |
| 1756 | spin_unlock(&fs_info->qgroup_op_lock); |
| 1757 | if (!n) |
| 1758 | return 0; |
| 1759 | tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n); |
| 1760 | while (tmp_oper->bytenr == oper->bytenr) { |
| 1761 | /* |
| 1762 | * If it's not a removal we don't care, additions work out |
| 1763 | * properly with our refcnt tracking. |
| 1764 | */ |
| 1765 | if (tmp_oper->type != BTRFS_QGROUP_OPER_SUB_SHARED && |
| 1766 | tmp_oper->type != BTRFS_QGROUP_OPER_SUB_EXCL) |
| 1767 | goto next; |
| 1768 | qg = find_qgroup_rb(fs_info, tmp_oper->ref_root); |
| 1769 | if (!qg) |
| 1770 | goto next; |
| 1771 | ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg), |
| 1772 | GFP_ATOMIC); |
| 1773 | if (ret) { |
| 1774 | if (ret < 0) |
| 1775 | return ret; |
| 1776 | /* |
| 1777 | * We only want to increase old_roots if this qgroup is |
| 1778 | * not already in the list of qgroups. If it is already |
| 1779 | * there then that means it must have been re-added or |
| 1780 | * the delete will be discarded because we had an |
| 1781 | * existing ref that we haven't looked up yet. In this |
| 1782 | * case we don't want to increase old_roots. So if ret |
| 1783 | * == 1 then we know that this is the first time we've |
| 1784 | * seen this qgroup and we can bump the old_roots. |
| 1785 | */ |
| 1786 | (*old_roots)++; |
| 1787 | ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), |
| 1788 | GFP_ATOMIC); |
| 1789 | if (ret < 0) |
| 1790 | return ret; |
| 1791 | } |
| 1792 | next: |
| 1793 | spin_lock(&fs_info->qgroup_op_lock); |
| 1794 | n = rb_next(&tmp_oper->n); |
| 1795 | spin_unlock(&fs_info->qgroup_op_lock); |
| 1796 | if (!n) |
| 1797 | break; |
| 1798 | tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n); |
| 1799 | } |
| 1800 | |
| 1801 | /* Ok now process the qgroups we found */ |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1802 | ULIST_ITER_INIT(&uiter); |
| 1803 | while ((unode = ulist_next(tmp, &uiter))) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1804 | struct btrfs_qgroup_list *glist; |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1805 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1806 | qg = u64_to_ptr(unode->aux); |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 1807 | btrfs_qgroup_update_old_refcnt(qg, seq, 1); |
| 1808 | btrfs_qgroup_update_new_refcnt(qg, seq, 1); |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1809 | list_for_each_entry(glist, &qg->groups, next_group) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1810 | ret = ulist_add(qgroups, glist->group->qgroupid, |
| 1811 | ptr_to_u64(glist->group), GFP_ATOMIC); |
| 1812 | if (ret < 0) |
| 1813 | return ret; |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1814 | ret = ulist_add(tmp, glist->group->qgroupid, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1815 | ptr_to_u64(glist->group), GFP_ATOMIC); |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1816 | if (ret < 0) |
| 1817 | return ret; |
| 1818 | } |
| 1819 | } |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1820 | return 0; |
| 1821 | } |
| 1822 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1823 | /* Add refcnt for the newly added reference. */ |
| 1824 | static int qgroup_calc_new_refcnt(struct btrfs_fs_info *fs_info, |
| 1825 | struct btrfs_qgroup_operation *oper, |
| 1826 | struct btrfs_qgroup *qgroup, |
| 1827 | struct ulist *tmp, struct ulist *qgroups, |
| 1828 | u64 seq) |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1829 | { |
| 1830 | struct ulist_node *unode; |
| 1831 | struct ulist_iterator uiter; |
| 1832 | struct btrfs_qgroup *qg; |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 1833 | int ret; |
| 1834 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1835 | ulist_reinit(tmp); |
| 1836 | ret = ulist_add(qgroups, qgroup->qgroupid, ptr_to_u64(qgroup), |
| 1837 | GFP_ATOMIC); |
| 1838 | if (ret < 0) |
| 1839 | return ret; |
| 1840 | ret = ulist_add(tmp, qgroup->qgroupid, ptr_to_u64(qgroup), |
| 1841 | GFP_ATOMIC); |
| 1842 | if (ret < 0) |
| 1843 | return ret; |
| 1844 | ULIST_ITER_INIT(&uiter); |
| 1845 | while ((unode = ulist_next(tmp, &uiter))) { |
| 1846 | struct btrfs_qgroup_list *glist; |
| 1847 | |
| 1848 | qg = u64_to_ptr(unode->aux); |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 1849 | if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) |
| 1850 | btrfs_qgroup_update_new_refcnt(qg, seq, 1); |
| 1851 | else |
| 1852 | btrfs_qgroup_update_old_refcnt(qg, seq, 1); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1853 | list_for_each_entry(glist, &qg->groups, next_group) { |
| 1854 | ret = ulist_add(tmp, glist->group->qgroupid, |
| 1855 | ptr_to_u64(glist->group), GFP_ATOMIC); |
| 1856 | if (ret < 0) |
| 1857 | return ret; |
| 1858 | ret = ulist_add(qgroups, glist->group->qgroupid, |
| 1859 | ptr_to_u64(glist->group), GFP_ATOMIC); |
| 1860 | if (ret < 0) |
| 1861 | return ret; |
| 1862 | } |
| 1863 | } |
| 1864 | return 0; |
| 1865 | } |
| 1866 | |
Qu Wenruo | d810ef2 | 2015-04-12 16:52:34 +0800 | [diff] [blame] | 1867 | #define UPDATE_NEW 0 |
| 1868 | #define UPDATE_OLD 1 |
| 1869 | /* |
| 1870 | * Walk all of the roots that points to the bytenr and adjust their refcnts. |
| 1871 | */ |
| 1872 | static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info, |
| 1873 | struct ulist *roots, struct ulist *tmp, |
| 1874 | struct ulist *qgroups, u64 seq, int update_old) |
| 1875 | { |
| 1876 | struct ulist_node *unode; |
| 1877 | struct ulist_iterator uiter; |
| 1878 | struct ulist_node *tmp_unode; |
| 1879 | struct ulist_iterator tmp_uiter; |
| 1880 | struct btrfs_qgroup *qg; |
| 1881 | int ret = 0; |
| 1882 | |
| 1883 | if (!roots) |
| 1884 | return 0; |
| 1885 | ULIST_ITER_INIT(&uiter); |
| 1886 | while ((unode = ulist_next(roots, &uiter))) { |
| 1887 | qg = find_qgroup_rb(fs_info, unode->val); |
| 1888 | if (!qg) |
| 1889 | continue; |
| 1890 | |
| 1891 | ulist_reinit(tmp); |
| 1892 | ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg), |
| 1893 | GFP_ATOMIC); |
| 1894 | if (ret < 0) |
| 1895 | return ret; |
| 1896 | ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC); |
| 1897 | if (ret < 0) |
| 1898 | return ret; |
| 1899 | ULIST_ITER_INIT(&tmp_uiter); |
| 1900 | while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) { |
| 1901 | struct btrfs_qgroup_list *glist; |
| 1902 | |
| 1903 | qg = u64_to_ptr(tmp_unode->aux); |
| 1904 | if (update_old) |
| 1905 | btrfs_qgroup_update_old_refcnt(qg, seq, 1); |
| 1906 | else |
| 1907 | btrfs_qgroup_update_new_refcnt(qg, seq, 1); |
| 1908 | list_for_each_entry(glist, &qg->groups, next_group) { |
| 1909 | ret = ulist_add(qgroups, glist->group->qgroupid, |
| 1910 | ptr_to_u64(glist->group), |
| 1911 | GFP_ATOMIC); |
| 1912 | if (ret < 0) |
| 1913 | return ret; |
| 1914 | ret = ulist_add(tmp, glist->group->qgroupid, |
| 1915 | ptr_to_u64(glist->group), |
| 1916 | GFP_ATOMIC); |
| 1917 | if (ret < 0) |
| 1918 | return ret; |
| 1919 | } |
| 1920 | } |
| 1921 | } |
| 1922 | return 0; |
| 1923 | } |
| 1924 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1925 | /* |
Qu Wenruo | 823ae5b | 2015-04-12 16:59:57 +0800 | [diff] [blame] | 1926 | * Update qgroup rfer/excl counters. |
| 1927 | * Rfer update is easy, codes can explain themselves. |
| 1928 | * Excl update is tricky, the update is split into 2 part. |
| 1929 | * Part 1: Possible exclusive <-> sharing detect: |
| 1930 | * | A | !A | |
| 1931 | * ------------------------------------- |
| 1932 | * B | * | - | |
| 1933 | * ------------------------------------- |
| 1934 | * !B | + | ** | |
| 1935 | * ------------------------------------- |
| 1936 | * |
| 1937 | * Conditions: |
| 1938 | * A: cur_old_roots < nr_old_roots (not exclusive before) |
| 1939 | * !A: cur_old_roots == nr_old_roots (possible exclusive before) |
| 1940 | * B: cur_new_roots < nr_new_roots (not exclusive now) |
| 1941 | * !B: cur_new_roots == nr_new_roots (possible exclsuive now) |
| 1942 | * |
| 1943 | * Results: |
| 1944 | * +: Possible sharing -> exclusive -: Possible exclusive -> sharing |
| 1945 | * *: Definitely not changed. **: Possible unchanged. |
| 1946 | * |
| 1947 | * For !A and !B condition, the exception is cur_old/new_roots == 0 case. |
| 1948 | * |
| 1949 | * To make the logic clear, we first use condition A and B to split |
| 1950 | * combination into 4 results. |
| 1951 | * |
| 1952 | * Then, for result "+" and "-", check old/new_roots == 0 case, as in them |
| 1953 | * only on variant maybe 0. |
| 1954 | * |
| 1955 | * Lastly, check result **, since there are 2 variants maybe 0, split them |
| 1956 | * again(2x2). |
| 1957 | * But this time we don't need to consider other things, the codes and logic |
| 1958 | * is easy to understand now. |
| 1959 | */ |
| 1960 | static int qgroup_update_counters(struct btrfs_fs_info *fs_info, |
| 1961 | struct ulist *qgroups, |
| 1962 | u64 nr_old_roots, |
| 1963 | u64 nr_new_roots, |
| 1964 | u64 num_bytes, u64 seq) |
| 1965 | { |
| 1966 | struct ulist_node *unode; |
| 1967 | struct ulist_iterator uiter; |
| 1968 | struct btrfs_qgroup *qg; |
| 1969 | u64 cur_new_count, cur_old_count; |
| 1970 | |
| 1971 | ULIST_ITER_INIT(&uiter); |
| 1972 | while ((unode = ulist_next(qgroups, &uiter))) { |
| 1973 | bool dirty = false; |
| 1974 | |
| 1975 | qg = u64_to_ptr(unode->aux); |
| 1976 | cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq); |
| 1977 | cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq); |
| 1978 | |
| 1979 | /* Rfer update part */ |
| 1980 | if (cur_old_count == 0 && cur_new_count > 0) { |
| 1981 | qg->rfer += num_bytes; |
| 1982 | qg->rfer_cmpr += num_bytes; |
| 1983 | dirty = true; |
| 1984 | } |
| 1985 | if (cur_old_count > 0 && cur_new_count == 0) { |
| 1986 | qg->rfer -= num_bytes; |
| 1987 | qg->rfer_cmpr -= num_bytes; |
| 1988 | dirty = true; |
| 1989 | } |
| 1990 | |
| 1991 | /* Excl update part */ |
| 1992 | /* Exclusive/none -> shared case */ |
| 1993 | if (cur_old_count == nr_old_roots && |
| 1994 | cur_new_count < nr_new_roots) { |
| 1995 | /* Exclusive -> shared */ |
| 1996 | if (cur_old_count != 0) { |
| 1997 | qg->excl -= num_bytes; |
| 1998 | qg->excl_cmpr -= num_bytes; |
| 1999 | dirty = true; |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | /* Shared -> exclusive/none case */ |
| 2004 | if (cur_old_count < nr_old_roots && |
| 2005 | cur_new_count == nr_new_roots) { |
| 2006 | /* Shared->exclusive */ |
| 2007 | if (cur_new_count != 0) { |
| 2008 | qg->excl += num_bytes; |
| 2009 | qg->excl_cmpr += num_bytes; |
| 2010 | dirty = true; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | /* Exclusive/none -> exclusive/none case */ |
| 2015 | if (cur_old_count == nr_old_roots && |
| 2016 | cur_new_count == nr_new_roots) { |
| 2017 | if (cur_old_count == 0) { |
| 2018 | /* None -> exclusive/none */ |
| 2019 | |
| 2020 | if (cur_new_count != 0) { |
| 2021 | /* None -> exclusive */ |
| 2022 | qg->excl += num_bytes; |
| 2023 | qg->excl_cmpr += num_bytes; |
| 2024 | dirty = true; |
| 2025 | } |
| 2026 | /* None -> none, nothing changed */ |
| 2027 | } else { |
| 2028 | /* Exclusive -> exclusive/none */ |
| 2029 | |
| 2030 | if (cur_new_count == 0) { |
| 2031 | /* Exclusive -> none */ |
| 2032 | qg->excl -= num_bytes; |
| 2033 | qg->excl_cmpr -= num_bytes; |
| 2034 | dirty = true; |
| 2035 | } |
| 2036 | /* Exclusive -> exclusive, nothing changed */ |
| 2037 | } |
| 2038 | } |
| 2039 | if (dirty) |
| 2040 | qgroup_dirty(fs_info, qg); |
| 2041 | } |
| 2042 | return 0; |
| 2043 | } |
| 2044 | |
| 2045 | /* |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2046 | * This adjusts the counters for all referenced qgroups if need be. |
| 2047 | */ |
| 2048 | static int qgroup_adjust_counters(struct btrfs_fs_info *fs_info, |
| 2049 | u64 root_to_skip, u64 num_bytes, |
| 2050 | struct ulist *qgroups, u64 seq, |
| 2051 | int old_roots, int new_roots, int rescan) |
| 2052 | { |
| 2053 | struct ulist_node *unode; |
| 2054 | struct ulist_iterator uiter; |
| 2055 | struct btrfs_qgroup *qg; |
| 2056 | u64 cur_new_count, cur_old_count; |
| 2057 | |
| 2058 | ULIST_ITER_INIT(&uiter); |
| 2059 | while ((unode = ulist_next(qgroups, &uiter))) { |
| 2060 | bool dirty = false; |
| 2061 | |
| 2062 | qg = u64_to_ptr(unode->aux); |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 2063 | cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq); |
| 2064 | cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq); |
| 2065 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2066 | /* |
| 2067 | * Wasn't referenced before but is now, add to the reference |
| 2068 | * counters. |
| 2069 | */ |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 2070 | if (cur_old_count == 0 && cur_new_count > 0) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2071 | qg->rfer += num_bytes; |
| 2072 | qg->rfer_cmpr += num_bytes; |
| 2073 | dirty = true; |
| 2074 | } |
| 2075 | |
| 2076 | /* |
| 2077 | * Was referenced before but isn't now, subtract from the |
| 2078 | * reference counters. |
| 2079 | */ |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 2080 | if (cur_old_count > 0 && cur_new_count == 0) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2081 | qg->rfer -= num_bytes; |
| 2082 | qg->rfer_cmpr -= num_bytes; |
| 2083 | dirty = true; |
| 2084 | } |
| 2085 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2086 | /* |
| 2087 | * If our refcount was the same as the roots previously but our |
| 2088 | * new count isn't the same as the number of roots now then we |
| 2089 | * went from having a exclusive reference on this range to not. |
| 2090 | */ |
| 2091 | if (old_roots && cur_old_count == old_roots && |
| 2092 | (cur_new_count != new_roots || new_roots == 0)) { |
| 2093 | WARN_ON(cur_new_count != new_roots && new_roots == 0); |
| 2094 | qg->excl -= num_bytes; |
| 2095 | qg->excl_cmpr -= num_bytes; |
| 2096 | dirty = true; |
| 2097 | } |
| 2098 | |
| 2099 | /* |
| 2100 | * If we didn't reference all the roots before but now we do we |
| 2101 | * have an exclusive reference to this range. |
| 2102 | */ |
| 2103 | if ((!old_roots || (old_roots && cur_old_count != old_roots)) |
| 2104 | && cur_new_count == new_roots) { |
| 2105 | qg->excl += num_bytes; |
| 2106 | qg->excl_cmpr += num_bytes; |
| 2107 | dirty = true; |
| 2108 | } |
| 2109 | |
| 2110 | if (dirty) |
| 2111 | qgroup_dirty(fs_info, qg); |
| 2112 | } |
| 2113 | return 0; |
| 2114 | } |
| 2115 | |
| 2116 | /* |
| 2117 | * If we removed a data extent and there were other references for that bytenr |
| 2118 | * then we need to lookup all referenced roots to make sure we still don't |
| 2119 | * reference this bytenr. If we do then we can just discard this operation. |
| 2120 | */ |
| 2121 | static int check_existing_refs(struct btrfs_trans_handle *trans, |
| 2122 | struct btrfs_fs_info *fs_info, |
| 2123 | struct btrfs_qgroup_operation *oper) |
| 2124 | { |
| 2125 | struct ulist *roots = NULL; |
| 2126 | struct ulist_node *unode; |
| 2127 | struct ulist_iterator uiter; |
| 2128 | int ret = 0; |
| 2129 | |
| 2130 | ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, |
| 2131 | oper->elem.seq, &roots); |
| 2132 | if (ret < 0) |
| 2133 | return ret; |
| 2134 | ret = 0; |
| 2135 | |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 2136 | ULIST_ITER_INIT(&uiter); |
| 2137 | while ((unode = ulist_next(roots, &uiter))) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2138 | if (unode->val == oper->ref_root) { |
| 2139 | ret = 1; |
| 2140 | break; |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 2141 | } |
| 2142 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2143 | ulist_free(roots); |
| 2144 | btrfs_put_tree_mod_seq(fs_info, &oper->elem); |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 2145 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2146 | return ret; |
| 2147 | } |
| 2148 | |
| 2149 | /* |
| 2150 | * If we share a reference across multiple roots then we may need to adjust |
| 2151 | * various qgroups referenced and exclusive counters. The basic premise is this |
| 2152 | * |
| 2153 | * 1) We have seq to represent a 0 count. Instead of looping through all of the |
| 2154 | * qgroups and resetting their refcount to 0 we just constantly bump this |
| 2155 | * sequence number to act as the base reference count. This means that if |
| 2156 | * anybody is equal to or below this sequence they were never referenced. We |
| 2157 | * jack this sequence up by the number of roots we found each time in order to |
| 2158 | * make sure we don't have any overlap. |
| 2159 | * |
| 2160 | * 2) We first search all the roots that reference the area _except_ the root |
| 2161 | * we're acting on currently. This makes up the old_refcnt of all the qgroups |
| 2162 | * before. |
| 2163 | * |
| 2164 | * 3) We walk all of the qgroups referenced by the root we are currently acting |
| 2165 | * on, and will either adjust old_refcnt in the case of a removal or the |
| 2166 | * new_refcnt in the case of an addition. |
| 2167 | * |
| 2168 | * 4) Finally we walk all the qgroups that are referenced by this range |
| 2169 | * including the root we are acting on currently. We will adjust the counters |
| 2170 | * based on the number of roots we had and will have after this operation. |
| 2171 | * |
| 2172 | * Take this example as an illustration |
| 2173 | * |
| 2174 | * [qgroup 1/0] |
| 2175 | * / | \ |
| 2176 | * [qg 0/0] [qg 0/1] [qg 0/2] |
| 2177 | * \ | / |
| 2178 | * [ extent ] |
| 2179 | * |
| 2180 | * Say we are adding a reference that is covered by qg 0/0. The first step |
| 2181 | * would give a refcnt of 1 to qg 0/1 and 0/2 and a refcnt of 2 to qg 1/0 with |
| 2182 | * old_roots being 2. Because it is adding new_roots will be 1. We then go |
| 2183 | * through qg 0/0 which will get the new_refcnt set to 1 and add 1 to qg 1/0's |
| 2184 | * new_refcnt, bringing it to 3. We then walk through all of the qgroups, we |
| 2185 | * notice that the old refcnt for qg 0/0 < the new refcnt, so we added a |
| 2186 | * reference and thus must add the size to the referenced bytes. Everything |
| 2187 | * else is the same so nothing else changes. |
| 2188 | */ |
| 2189 | static int qgroup_shared_accounting(struct btrfs_trans_handle *trans, |
| 2190 | struct btrfs_fs_info *fs_info, |
| 2191 | struct btrfs_qgroup_operation *oper) |
| 2192 | { |
| 2193 | struct ulist *roots = NULL; |
| 2194 | struct ulist *qgroups, *tmp; |
| 2195 | struct btrfs_qgroup *qgroup; |
David Sterba | 3284da7 | 2015-02-25 15:47:32 +0100 | [diff] [blame] | 2196 | struct seq_list elem = SEQ_LIST_INIT(elem); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2197 | u64 seq; |
| 2198 | int old_roots = 0; |
| 2199 | int new_roots = 0; |
| 2200 | int ret = 0; |
| 2201 | |
| 2202 | if (oper->elem.seq) { |
| 2203 | ret = check_existing_refs(trans, fs_info, oper); |
| 2204 | if (ret < 0) |
| 2205 | return ret; |
| 2206 | if (ret) |
| 2207 | return 0; |
| 2208 | } |
| 2209 | |
| 2210 | qgroups = ulist_alloc(GFP_NOFS); |
| 2211 | if (!qgroups) |
| 2212 | return -ENOMEM; |
| 2213 | |
| 2214 | tmp = ulist_alloc(GFP_NOFS); |
Eric Sandeen | d737278 | 2014-06-12 00:14:59 -0500 | [diff] [blame] | 2215 | if (!tmp) { |
| 2216 | ulist_free(qgroups); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2217 | return -ENOMEM; |
Eric Sandeen | d737278 | 2014-06-12 00:14:59 -0500 | [diff] [blame] | 2218 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2219 | |
| 2220 | btrfs_get_tree_mod_seq(fs_info, &elem); |
| 2221 | ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, elem.seq, |
| 2222 | &roots); |
| 2223 | btrfs_put_tree_mod_seq(fs_info, &elem); |
| 2224 | if (ret < 0) { |
| 2225 | ulist_free(qgroups); |
| 2226 | ulist_free(tmp); |
| 2227 | return ret; |
| 2228 | } |
| 2229 | spin_lock(&fs_info->qgroup_lock); |
| 2230 | qgroup = find_qgroup_rb(fs_info, oper->ref_root); |
| 2231 | if (!qgroup) |
| 2232 | goto out; |
| 2233 | seq = fs_info->qgroup_seq; |
| 2234 | |
| 2235 | /* |
| 2236 | * So roots is the list of all the roots currently pointing at the |
| 2237 | * bytenr, including the ref we are adding if we are adding, or not if |
| 2238 | * we are removing a ref. So we pass in the ref_root to skip that root |
| 2239 | * in our calculations. We set old_refnct and new_refcnt cause who the |
| 2240 | * hell knows what everything looked like before, and it doesn't matter |
| 2241 | * except... |
| 2242 | */ |
| 2243 | ret = qgroup_calc_old_refcnt(fs_info, oper->ref_root, tmp, roots, qgroups, |
| 2244 | seq, &old_roots, 0); |
| 2245 | if (ret < 0) |
| 2246 | goto out; |
| 2247 | |
| 2248 | /* |
| 2249 | * Now adjust the refcounts of the qgroups that care about this |
| 2250 | * reference, either the old_count in the case of removal or new_count |
| 2251 | * in the case of an addition. |
| 2252 | */ |
| 2253 | ret = qgroup_calc_new_refcnt(fs_info, oper, qgroup, tmp, qgroups, |
| 2254 | seq); |
| 2255 | if (ret < 0) |
| 2256 | goto out; |
| 2257 | |
| 2258 | /* |
| 2259 | * ...in the case of removals. If we had a removal before we got around |
| 2260 | * to processing this operation then we need to find that guy and count |
| 2261 | * his references as if they really existed so we don't end up screwing |
| 2262 | * up the exclusive counts. Then whenever we go to process the delete |
| 2263 | * everything will be grand and we can account for whatever exclusive |
| 2264 | * changes need to be made there. We also have to pass in old_roots so |
| 2265 | * we have an accurate count of the roots as it pertains to this |
| 2266 | * operations view of the world. |
| 2267 | */ |
| 2268 | ret = qgroup_account_deleted_refs(fs_info, oper, tmp, qgroups, seq, |
| 2269 | &old_roots); |
| 2270 | if (ret < 0) |
| 2271 | goto out; |
| 2272 | |
| 2273 | /* |
| 2274 | * We are adding our root, need to adjust up the number of roots, |
| 2275 | * otherwise old_roots is the number of roots we want. |
| 2276 | */ |
| 2277 | if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) { |
| 2278 | new_roots = old_roots + 1; |
| 2279 | } else { |
| 2280 | new_roots = old_roots; |
| 2281 | old_roots++; |
| 2282 | } |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 2283 | |
| 2284 | /* |
| 2285 | * Bump qgroup_seq to avoid seq overlap |
| 2286 | * XXX: This makes qgroup_seq mismatch with oper->seq. |
| 2287 | */ |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2288 | fs_info->qgroup_seq += old_roots + 1; |
| 2289 | |
| 2290 | |
| 2291 | /* |
| 2292 | * And now the magic happens, bless Arne for having a pretty elegant |
| 2293 | * solution for this. |
| 2294 | */ |
| 2295 | qgroup_adjust_counters(fs_info, oper->ref_root, oper->num_bytes, |
| 2296 | qgroups, seq, old_roots, new_roots, 0); |
| 2297 | out: |
| 2298 | spin_unlock(&fs_info->qgroup_lock); |
| 2299 | ulist_free(qgroups); |
| 2300 | ulist_free(roots); |
| 2301 | ulist_free(tmp); |
| 2302 | return ret; |
Jan Schmidt | 46b665c | 2013-04-25 16:04:50 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2305 | /* |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 2306 | * Process a reference to a shared subtree. This type of operation is |
| 2307 | * queued during snapshot removal when we encounter extents which are |
| 2308 | * shared between more than one root. |
| 2309 | */ |
| 2310 | static int qgroup_subtree_accounting(struct btrfs_trans_handle *trans, |
| 2311 | struct btrfs_fs_info *fs_info, |
| 2312 | struct btrfs_qgroup_operation *oper) |
| 2313 | { |
| 2314 | struct ulist *roots = NULL; |
| 2315 | struct ulist_node *unode; |
| 2316 | struct ulist_iterator uiter; |
| 2317 | struct btrfs_qgroup_list *glist; |
| 2318 | struct ulist *parents; |
| 2319 | int ret = 0; |
Mark Fasheh | f90e579 | 2014-07-17 12:39:04 -0700 | [diff] [blame] | 2320 | int err; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 2321 | struct btrfs_qgroup *qg; |
| 2322 | u64 root_obj = 0; |
David Sterba | 3284da7 | 2015-02-25 15:47:32 +0100 | [diff] [blame] | 2323 | struct seq_list elem = SEQ_LIST_INIT(elem); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 2324 | |
| 2325 | parents = ulist_alloc(GFP_NOFS); |
| 2326 | if (!parents) |
| 2327 | return -ENOMEM; |
| 2328 | |
| 2329 | btrfs_get_tree_mod_seq(fs_info, &elem); |
| 2330 | ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, |
| 2331 | elem.seq, &roots); |
| 2332 | btrfs_put_tree_mod_seq(fs_info, &elem); |
| 2333 | if (ret < 0) |
Eric Sandeen | a3c1089 | 2014-08-17 15:09:21 -0500 | [diff] [blame] | 2334 | goto out; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 2335 | |
| 2336 | if (roots->nnodes != 1) |
| 2337 | goto out; |
| 2338 | |
| 2339 | ULIST_ITER_INIT(&uiter); |
| 2340 | unode = ulist_next(roots, &uiter); /* Only want 1 so no need to loop */ |
| 2341 | /* |
| 2342 | * If we find our ref root then that means all refs |
| 2343 | * this extent has to the root have not yet been |
| 2344 | * deleted. In that case, we do nothing and let the |
| 2345 | * last ref for this bytenr drive our update. |
| 2346 | * |
| 2347 | * This can happen for example if an extent is |
| 2348 | * referenced multiple times in a snapshot (clone, |
| 2349 | * etc). If we are in the middle of snapshot removal, |
| 2350 | * queued updates for such an extent will find the |
| 2351 | * root if we have not yet finished removing the |
| 2352 | * snapshot. |
| 2353 | */ |
| 2354 | if (unode->val == oper->ref_root) |
| 2355 | goto out; |
| 2356 | |
| 2357 | root_obj = unode->val; |
| 2358 | BUG_ON(!root_obj); |
| 2359 | |
| 2360 | spin_lock(&fs_info->qgroup_lock); |
| 2361 | qg = find_qgroup_rb(fs_info, root_obj); |
| 2362 | if (!qg) |
| 2363 | goto out_unlock; |
| 2364 | |
| 2365 | qg->excl += oper->num_bytes; |
| 2366 | qg->excl_cmpr += oper->num_bytes; |
| 2367 | qgroup_dirty(fs_info, qg); |
| 2368 | |
| 2369 | /* |
| 2370 | * Adjust counts for parent groups. First we find all |
| 2371 | * parents, then in the 2nd loop we do the adjustment |
| 2372 | * while adding parents of the parents to our ulist. |
| 2373 | */ |
| 2374 | list_for_each_entry(glist, &qg->groups, next_group) { |
Mark Fasheh | f90e579 | 2014-07-17 12:39:04 -0700 | [diff] [blame] | 2375 | err = ulist_add(parents, glist->group->qgroupid, |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 2376 | ptr_to_u64(glist->group), GFP_ATOMIC); |
Mark Fasheh | f90e579 | 2014-07-17 12:39:04 -0700 | [diff] [blame] | 2377 | if (err < 0) { |
| 2378 | ret = err; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 2379 | goto out_unlock; |
Mark Fasheh | f90e579 | 2014-07-17 12:39:04 -0700 | [diff] [blame] | 2380 | } |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | ULIST_ITER_INIT(&uiter); |
| 2384 | while ((unode = ulist_next(parents, &uiter))) { |
| 2385 | qg = u64_to_ptr(unode->aux); |
| 2386 | qg->excl += oper->num_bytes; |
| 2387 | qg->excl_cmpr += oper->num_bytes; |
| 2388 | qgroup_dirty(fs_info, qg); |
| 2389 | |
| 2390 | /* Add any parents of the parents */ |
| 2391 | list_for_each_entry(glist, &qg->groups, next_group) { |
Mark Fasheh | f90e579 | 2014-07-17 12:39:04 -0700 | [diff] [blame] | 2392 | err = ulist_add(parents, glist->group->qgroupid, |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 2393 | ptr_to_u64(glist->group), GFP_ATOMIC); |
Mark Fasheh | f90e579 | 2014-07-17 12:39:04 -0700 | [diff] [blame] | 2394 | if (err < 0) { |
| 2395 | ret = err; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 2396 | goto out_unlock; |
Mark Fasheh | f90e579 | 2014-07-17 12:39:04 -0700 | [diff] [blame] | 2397 | } |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | out_unlock: |
| 2402 | spin_unlock(&fs_info->qgroup_lock); |
| 2403 | |
| 2404 | out: |
| 2405 | ulist_free(roots); |
| 2406 | ulist_free(parents); |
| 2407 | return ret; |
| 2408 | } |
| 2409 | |
| 2410 | /* |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2411 | * btrfs_qgroup_account_ref is called for every ref that is added to or deleted |
| 2412 | * from the fs. First, all roots referencing the extent are searched, and |
| 2413 | * then the space is accounted accordingly to the different roots. The |
| 2414 | * accounting algorithm works in 3 steps documented inline. |
| 2415 | */ |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2416 | static int btrfs_qgroup_account(struct btrfs_trans_handle *trans, |
| 2417 | struct btrfs_fs_info *fs_info, |
| 2418 | struct btrfs_qgroup_operation *oper) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2419 | { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2420 | int ret = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2421 | |
| 2422 | if (!fs_info->quota_enabled) |
| 2423 | return 0; |
| 2424 | |
| 2425 | BUG_ON(!fs_info->quota_root); |
| 2426 | |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 2427 | mutex_lock(&fs_info->qgroup_rescan_lock); |
| 2428 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2429 | if (fs_info->qgroup_rescan_progress.objectid <= oper->bytenr) { |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 2430 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 2431 | return 0; |
| 2432 | } |
| 2433 | } |
| 2434 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 2435 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2436 | ASSERT(is_fstree(oper->ref_root)); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2437 | |
Mark Fasheh | d398210 | 2014-07-17 12:39:00 -0700 | [diff] [blame] | 2438 | trace_btrfs_qgroup_account(oper); |
| 2439 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2440 | switch (oper->type) { |
| 2441 | case BTRFS_QGROUP_OPER_ADD_EXCL: |
| 2442 | case BTRFS_QGROUP_OPER_SUB_EXCL: |
| 2443 | ret = qgroup_excl_accounting(fs_info, oper); |
| 2444 | break; |
| 2445 | case BTRFS_QGROUP_OPER_ADD_SHARED: |
| 2446 | case BTRFS_QGROUP_OPER_SUB_SHARED: |
| 2447 | ret = qgroup_shared_accounting(trans, fs_info, oper); |
| 2448 | break; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 2449 | case BTRFS_QGROUP_OPER_SUB_SUBTREE: |
| 2450 | ret = qgroup_subtree_accounting(trans, fs_info, oper); |
| 2451 | break; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2452 | default: |
| 2453 | ASSERT(0); |
| 2454 | } |
| 2455 | return ret; |
| 2456 | } |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 2457 | |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2458 | static int |
| 2459 | btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, |
| 2460 | struct btrfs_fs_info *fs_info, |
| 2461 | u64 bytenr, u64 num_bytes, |
| 2462 | struct ulist *old_roots, struct ulist *new_roots) |
| 2463 | { |
| 2464 | struct ulist *qgroups = NULL; |
| 2465 | struct ulist *tmp = NULL; |
| 2466 | u64 seq; |
| 2467 | u64 nr_new_roots = 0; |
| 2468 | u64 nr_old_roots = 0; |
| 2469 | int ret = 0; |
| 2470 | |
| 2471 | if (new_roots) |
| 2472 | nr_new_roots = new_roots->nnodes; |
| 2473 | if (old_roots) |
| 2474 | nr_old_roots = old_roots->nnodes; |
| 2475 | |
| 2476 | if (!fs_info->quota_enabled) |
| 2477 | goto out_free; |
| 2478 | BUG_ON(!fs_info->quota_root); |
| 2479 | |
| 2480 | qgroups = ulist_alloc(GFP_NOFS); |
| 2481 | if (!qgroups) { |
| 2482 | ret = -ENOMEM; |
| 2483 | goto out_free; |
| 2484 | } |
| 2485 | tmp = ulist_alloc(GFP_NOFS); |
| 2486 | if (!tmp) { |
| 2487 | ret = -ENOMEM; |
| 2488 | goto out_free; |
| 2489 | } |
| 2490 | |
| 2491 | mutex_lock(&fs_info->qgroup_rescan_lock); |
| 2492 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
| 2493 | if (fs_info->qgroup_rescan_progress.objectid <= bytenr) { |
| 2494 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 2495 | ret = 0; |
| 2496 | goto out_free; |
| 2497 | } |
| 2498 | } |
| 2499 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 2500 | |
| 2501 | spin_lock(&fs_info->qgroup_lock); |
| 2502 | seq = fs_info->qgroup_seq; |
| 2503 | |
| 2504 | /* Update old refcnts using old_roots */ |
| 2505 | ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq, |
| 2506 | UPDATE_OLD); |
| 2507 | if (ret < 0) |
| 2508 | goto out; |
| 2509 | |
| 2510 | /* Update new refcnts using new_roots */ |
| 2511 | ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq, |
| 2512 | UPDATE_NEW); |
| 2513 | if (ret < 0) |
| 2514 | goto out; |
| 2515 | |
| 2516 | qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots, |
| 2517 | num_bytes, seq); |
| 2518 | |
| 2519 | /* |
| 2520 | * Bump qgroup_seq to avoid seq overlap |
| 2521 | */ |
| 2522 | fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1; |
| 2523 | out: |
| 2524 | spin_unlock(&fs_info->qgroup_lock); |
| 2525 | out_free: |
| 2526 | ulist_free(tmp); |
| 2527 | ulist_free(qgroups); |
| 2528 | ulist_free(old_roots); |
| 2529 | ulist_free(new_roots); |
| 2530 | return ret; |
| 2531 | } |
| 2532 | |
| 2533 | int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans, |
| 2534 | struct btrfs_fs_info *fs_info) |
| 2535 | { |
| 2536 | struct btrfs_qgroup_extent_record *record; |
| 2537 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2538 | struct ulist *new_roots = NULL; |
| 2539 | struct rb_node *node; |
| 2540 | int ret = 0; |
| 2541 | |
| 2542 | delayed_refs = &trans->transaction->delayed_refs; |
| 2543 | while ((node = rb_first(&delayed_refs->dirty_extent_root))) { |
| 2544 | record = rb_entry(node, struct btrfs_qgroup_extent_record, |
| 2545 | node); |
| 2546 | |
| 2547 | if (!ret) { |
| 2548 | /* |
| 2549 | * Use (u64)-1 as time_seq to do special search, which |
| 2550 | * doesn't lock tree or delayed_refs and search current |
| 2551 | * root. It's safe inside commit_transaction(). |
| 2552 | */ |
| 2553 | ret = btrfs_find_all_roots(trans, fs_info, |
| 2554 | record->bytenr, (u64)-1, &new_roots); |
| 2555 | if (ret < 0) |
| 2556 | goto cleanup; |
| 2557 | ret = btrfs_qgroup_account_extent(trans, fs_info, |
| 2558 | record->bytenr, record->num_bytes, |
| 2559 | record->old_roots, new_roots); |
| 2560 | record->old_roots = NULL; |
| 2561 | new_roots = NULL; |
| 2562 | } |
| 2563 | cleanup: |
| 2564 | ulist_free(record->old_roots); |
| 2565 | ulist_free(new_roots); |
| 2566 | new_roots = NULL; |
| 2567 | rb_erase(node, &delayed_refs->dirty_extent_root); |
| 2568 | kfree(record); |
| 2569 | |
| 2570 | } |
| 2571 | return ret; |
| 2572 | } |
| 2573 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2574 | /* |
| 2575 | * Needs to be called everytime we run delayed refs, even if there is an error |
| 2576 | * in order to cleanup outstanding operations. |
| 2577 | */ |
| 2578 | int btrfs_delayed_qgroup_accounting(struct btrfs_trans_handle *trans, |
| 2579 | struct btrfs_fs_info *fs_info) |
| 2580 | { |
| 2581 | struct btrfs_qgroup_operation *oper; |
| 2582 | int ret = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2583 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2584 | while (!list_empty(&trans->qgroup_ref_list)) { |
| 2585 | oper = list_first_entry(&trans->qgroup_ref_list, |
| 2586 | struct btrfs_qgroup_operation, list); |
| 2587 | list_del_init(&oper->list); |
| 2588 | if (!ret || !trans->aborted) |
| 2589 | ret = btrfs_qgroup_account(trans, fs_info, oper); |
| 2590 | spin_lock(&fs_info->qgroup_op_lock); |
| 2591 | rb_erase(&oper->n, &fs_info->qgroup_op_tree); |
| 2592 | spin_unlock(&fs_info->qgroup_op_lock); |
| 2593 | btrfs_put_tree_mod_seq(fs_info, &oper->elem); |
| 2594 | kfree(oper); |
| 2595 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2596 | return ret; |
| 2597 | } |
| 2598 | |
| 2599 | /* |
| 2600 | * called from commit_transaction. Writes all changed qgroups to disk. |
| 2601 | */ |
| 2602 | int btrfs_run_qgroups(struct btrfs_trans_handle *trans, |
| 2603 | struct btrfs_fs_info *fs_info) |
| 2604 | { |
| 2605 | struct btrfs_root *quota_root = fs_info->quota_root; |
| 2606 | int ret = 0; |
Jan Schmidt | 3d7b5a2 | 2013-04-25 16:04:52 +0000 | [diff] [blame] | 2607 | int start_rescan_worker = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2608 | |
| 2609 | if (!quota_root) |
| 2610 | goto out; |
| 2611 | |
Jan Schmidt | 3d7b5a2 | 2013-04-25 16:04:52 +0000 | [diff] [blame] | 2612 | if (!fs_info->quota_enabled && fs_info->pending_quota_state) |
| 2613 | start_rescan_worker = 1; |
| 2614 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2615 | fs_info->quota_enabled = fs_info->pending_quota_state; |
| 2616 | |
| 2617 | spin_lock(&fs_info->qgroup_lock); |
| 2618 | while (!list_empty(&fs_info->dirty_qgroups)) { |
| 2619 | struct btrfs_qgroup *qgroup; |
| 2620 | qgroup = list_first_entry(&fs_info->dirty_qgroups, |
| 2621 | struct btrfs_qgroup, dirty); |
| 2622 | list_del_init(&qgroup->dirty); |
| 2623 | spin_unlock(&fs_info->qgroup_lock); |
| 2624 | ret = update_qgroup_info_item(trans, quota_root, qgroup); |
| 2625 | if (ret) |
| 2626 | fs_info->qgroup_flags |= |
| 2627 | BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
Dongsheng Yang | d3001ed | 2014-11-20 21:04:56 -0500 | [diff] [blame] | 2628 | ret = update_qgroup_limit_item(trans, quota_root, qgroup); |
| 2629 | if (ret) |
| 2630 | fs_info->qgroup_flags |= |
| 2631 | BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2632 | spin_lock(&fs_info->qgroup_lock); |
| 2633 | } |
| 2634 | if (fs_info->quota_enabled) |
| 2635 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON; |
| 2636 | else |
| 2637 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON; |
| 2638 | spin_unlock(&fs_info->qgroup_lock); |
| 2639 | |
| 2640 | ret = update_qgroup_status_item(trans, fs_info, quota_root); |
| 2641 | if (ret) |
| 2642 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 2643 | |
Jan Schmidt | 3d7b5a2 | 2013-04-25 16:04:52 +0000 | [diff] [blame] | 2644 | if (!ret && start_rescan_worker) { |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 2645 | ret = qgroup_rescan_init(fs_info, 0, 1); |
| 2646 | if (!ret) { |
| 2647 | qgroup_rescan_zero_tracking(fs_info); |
Qu Wenruo | fc97fab | 2014-02-28 10:46:16 +0800 | [diff] [blame] | 2648 | btrfs_queue_work(fs_info->qgroup_rescan_workers, |
| 2649 | &fs_info->qgroup_rescan_work); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 2650 | } |
Jan Schmidt | 3d7b5a2 | 2013-04-25 16:04:52 +0000 | [diff] [blame] | 2651 | ret = 0; |
| 2652 | } |
| 2653 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2654 | out: |
| 2655 | |
| 2656 | return ret; |
| 2657 | } |
| 2658 | |
| 2659 | /* |
| 2660 | * copy the acounting information between qgroups. This is necessary when a |
| 2661 | * snapshot or a subvolume is created |
| 2662 | */ |
| 2663 | int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, |
| 2664 | struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid, |
| 2665 | struct btrfs_qgroup_inherit *inherit) |
| 2666 | { |
| 2667 | int ret = 0; |
| 2668 | int i; |
| 2669 | u64 *i_qgroups; |
| 2670 | struct btrfs_root *quota_root = fs_info->quota_root; |
| 2671 | struct btrfs_qgroup *srcgroup; |
| 2672 | struct btrfs_qgroup *dstgroup; |
| 2673 | u32 level_size = 0; |
Wang Shilong | 3f5e2d3 | 2013-04-07 10:50:19 +0000 | [diff] [blame] | 2674 | u64 nums; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2675 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 2676 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2677 | if (!fs_info->quota_enabled) |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 2678 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2679 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 2680 | if (!quota_root) { |
| 2681 | ret = -EINVAL; |
| 2682 | goto out; |
| 2683 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2684 | |
Wang Shilong | 3f5e2d3 | 2013-04-07 10:50:19 +0000 | [diff] [blame] | 2685 | if (inherit) { |
| 2686 | i_qgroups = (u64 *)(inherit + 1); |
| 2687 | nums = inherit->num_qgroups + 2 * inherit->num_ref_copies + |
| 2688 | 2 * inherit->num_excl_copies; |
| 2689 | for (i = 0; i < nums; ++i) { |
| 2690 | srcgroup = find_qgroup_rb(fs_info, *i_qgroups); |
| 2691 | if (!srcgroup) { |
| 2692 | ret = -EINVAL; |
| 2693 | goto out; |
| 2694 | } |
Dongsheng Yang | 09870d2 | 2014-11-11 07:18:22 -0500 | [diff] [blame] | 2695 | |
| 2696 | if ((srcgroup->qgroupid >> 48) <= (objectid >> 48)) { |
| 2697 | ret = -EINVAL; |
| 2698 | goto out; |
| 2699 | } |
Wang Shilong | 3f5e2d3 | 2013-04-07 10:50:19 +0000 | [diff] [blame] | 2700 | ++i_qgroups; |
| 2701 | } |
| 2702 | } |
| 2703 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2704 | /* |
| 2705 | * create a tracking group for the subvol itself |
| 2706 | */ |
| 2707 | ret = add_qgroup_item(trans, quota_root, objectid); |
| 2708 | if (ret) |
| 2709 | goto out; |
| 2710 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2711 | if (srcid) { |
| 2712 | struct btrfs_root *srcroot; |
| 2713 | struct btrfs_key srckey; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2714 | |
| 2715 | srckey.objectid = srcid; |
| 2716 | srckey.type = BTRFS_ROOT_ITEM_KEY; |
| 2717 | srckey.offset = (u64)-1; |
| 2718 | srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey); |
| 2719 | if (IS_ERR(srcroot)) { |
| 2720 | ret = PTR_ERR(srcroot); |
| 2721 | goto out; |
| 2722 | } |
| 2723 | |
| 2724 | rcu_read_lock(); |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 2725 | level_size = srcroot->nodesize; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2726 | rcu_read_unlock(); |
| 2727 | } |
| 2728 | |
| 2729 | /* |
| 2730 | * add qgroup to all inherited groups |
| 2731 | */ |
| 2732 | if (inherit) { |
| 2733 | i_qgroups = (u64 *)(inherit + 1); |
| 2734 | for (i = 0; i < inherit->num_qgroups; ++i) { |
| 2735 | ret = add_qgroup_relation_item(trans, quota_root, |
| 2736 | objectid, *i_qgroups); |
| 2737 | if (ret) |
| 2738 | goto out; |
| 2739 | ret = add_qgroup_relation_item(trans, quota_root, |
| 2740 | *i_qgroups, objectid); |
| 2741 | if (ret) |
| 2742 | goto out; |
| 2743 | ++i_qgroups; |
| 2744 | } |
| 2745 | } |
| 2746 | |
| 2747 | |
| 2748 | spin_lock(&fs_info->qgroup_lock); |
| 2749 | |
| 2750 | dstgroup = add_qgroup_rb(fs_info, objectid); |
Dan Carpenter | 57a5a88 | 2012-07-30 02:15:43 -0600 | [diff] [blame] | 2751 | if (IS_ERR(dstgroup)) { |
| 2752 | ret = PTR_ERR(dstgroup); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2753 | goto unlock; |
Dan Carpenter | 57a5a88 | 2012-07-30 02:15:43 -0600 | [diff] [blame] | 2754 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2755 | |
Dongsheng Yang | e8c8541 | 2014-11-20 20:58:34 -0500 | [diff] [blame] | 2756 | if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) { |
Dongsheng Yang | e8c8541 | 2014-11-20 20:58:34 -0500 | [diff] [blame] | 2757 | dstgroup->lim_flags = inherit->lim.flags; |
| 2758 | dstgroup->max_rfer = inherit->lim.max_rfer; |
| 2759 | dstgroup->max_excl = inherit->lim.max_excl; |
| 2760 | dstgroup->rsv_rfer = inherit->lim.rsv_rfer; |
| 2761 | dstgroup->rsv_excl = inherit->lim.rsv_excl; |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 2762 | |
| 2763 | ret = update_qgroup_limit_item(trans, quota_root, dstgroup); |
| 2764 | if (ret) { |
| 2765 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 2766 | btrfs_info(fs_info, "unable to update quota limit for %llu", |
| 2767 | dstgroup->qgroupid); |
| 2768 | goto unlock; |
| 2769 | } |
Dongsheng Yang | e8c8541 | 2014-11-20 20:58:34 -0500 | [diff] [blame] | 2770 | } |
| 2771 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2772 | if (srcid) { |
| 2773 | srcgroup = find_qgroup_rb(fs_info, srcid); |
Chris Mason | f3a87f1 | 2012-09-14 20:06:30 -0400 | [diff] [blame] | 2774 | if (!srcgroup) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2775 | goto unlock; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2776 | |
| 2777 | /* |
| 2778 | * We call inherit after we clone the root in order to make sure |
| 2779 | * our counts don't go crazy, so at this point the only |
| 2780 | * difference between the two roots should be the root node. |
| 2781 | */ |
| 2782 | dstgroup->rfer = srcgroup->rfer; |
| 2783 | dstgroup->rfer_cmpr = srcgroup->rfer_cmpr; |
| 2784 | dstgroup->excl = level_size; |
| 2785 | dstgroup->excl_cmpr = level_size; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2786 | srcgroup->excl = level_size; |
| 2787 | srcgroup->excl_cmpr = level_size; |
Dongsheng Yang | 3eeb4d5 | 2014-11-20 20:14:38 -0500 | [diff] [blame] | 2788 | |
| 2789 | /* inherit the limit info */ |
| 2790 | dstgroup->lim_flags = srcgroup->lim_flags; |
| 2791 | dstgroup->max_rfer = srcgroup->max_rfer; |
| 2792 | dstgroup->max_excl = srcgroup->max_excl; |
| 2793 | dstgroup->rsv_rfer = srcgroup->rsv_rfer; |
| 2794 | dstgroup->rsv_excl = srcgroup->rsv_excl; |
| 2795 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2796 | qgroup_dirty(fs_info, dstgroup); |
| 2797 | qgroup_dirty(fs_info, srcgroup); |
| 2798 | } |
| 2799 | |
Chris Mason | f3a87f1 | 2012-09-14 20:06:30 -0400 | [diff] [blame] | 2800 | if (!inherit) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2801 | goto unlock; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2802 | |
| 2803 | i_qgroups = (u64 *)(inherit + 1); |
| 2804 | for (i = 0; i < inherit->num_qgroups; ++i) { |
| 2805 | ret = add_relation_rb(quota_root->fs_info, objectid, |
| 2806 | *i_qgroups); |
| 2807 | if (ret) |
| 2808 | goto unlock; |
| 2809 | ++i_qgroups; |
| 2810 | } |
| 2811 | |
| 2812 | for (i = 0; i < inherit->num_ref_copies; ++i) { |
| 2813 | struct btrfs_qgroup *src; |
| 2814 | struct btrfs_qgroup *dst; |
| 2815 | |
| 2816 | src = find_qgroup_rb(fs_info, i_qgroups[0]); |
| 2817 | dst = find_qgroup_rb(fs_info, i_qgroups[1]); |
| 2818 | |
| 2819 | if (!src || !dst) { |
| 2820 | ret = -EINVAL; |
| 2821 | goto unlock; |
| 2822 | } |
| 2823 | |
| 2824 | dst->rfer = src->rfer - level_size; |
| 2825 | dst->rfer_cmpr = src->rfer_cmpr - level_size; |
| 2826 | i_qgroups += 2; |
| 2827 | } |
| 2828 | for (i = 0; i < inherit->num_excl_copies; ++i) { |
| 2829 | struct btrfs_qgroup *src; |
| 2830 | struct btrfs_qgroup *dst; |
| 2831 | |
| 2832 | src = find_qgroup_rb(fs_info, i_qgroups[0]); |
| 2833 | dst = find_qgroup_rb(fs_info, i_qgroups[1]); |
| 2834 | |
| 2835 | if (!src || !dst) { |
| 2836 | ret = -EINVAL; |
| 2837 | goto unlock; |
| 2838 | } |
| 2839 | |
| 2840 | dst->excl = src->excl + level_size; |
| 2841 | dst->excl_cmpr = src->excl_cmpr + level_size; |
| 2842 | i_qgroups += 2; |
| 2843 | } |
| 2844 | |
| 2845 | unlock: |
| 2846 | spin_unlock(&fs_info->qgroup_lock); |
| 2847 | out: |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 2848 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2849 | return ret; |
| 2850 | } |
| 2851 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2852 | int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes) |
| 2853 | { |
| 2854 | struct btrfs_root *quota_root; |
| 2855 | struct btrfs_qgroup *qgroup; |
| 2856 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2857 | u64 ref_root = root->root_key.objectid; |
| 2858 | int ret = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2859 | struct ulist_node *unode; |
| 2860 | struct ulist_iterator uiter; |
| 2861 | |
| 2862 | if (!is_fstree(ref_root)) |
| 2863 | return 0; |
| 2864 | |
| 2865 | if (num_bytes == 0) |
| 2866 | return 0; |
| 2867 | |
| 2868 | spin_lock(&fs_info->qgroup_lock); |
| 2869 | quota_root = fs_info->quota_root; |
| 2870 | if (!quota_root) |
| 2871 | goto out; |
| 2872 | |
| 2873 | qgroup = find_qgroup_rb(fs_info, ref_root); |
| 2874 | if (!qgroup) |
| 2875 | goto out; |
| 2876 | |
| 2877 | /* |
| 2878 | * in a first step, we check all affected qgroups if any limits would |
| 2879 | * be exceeded |
| 2880 | */ |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 2881 | ulist_reinit(fs_info->qgroup_ulist); |
| 2882 | ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid, |
Wang Shilong | 3c97185 | 2013-04-17 14:00:36 +0000 | [diff] [blame] | 2883 | (uintptr_t)qgroup, GFP_ATOMIC); |
| 2884 | if (ret < 0) |
| 2885 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2886 | ULIST_ITER_INIT(&uiter); |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 2887 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2888 | struct btrfs_qgroup *qg; |
| 2889 | struct btrfs_qgroup_list *glist; |
| 2890 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2891 | qg = u64_to_ptr(unode->aux); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2892 | |
| 2893 | if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) && |
Dongsheng Yang | e2d1f92 | 2015-02-06 10:26:52 -0500 | [diff] [blame] | 2894 | qg->reserved + (s64)qg->rfer + num_bytes > |
Wang Shilong | 720f1e2 | 2013-03-06 11:51:47 +0000 | [diff] [blame] | 2895 | qg->max_rfer) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2896 | ret = -EDQUOT; |
Wang Shilong | 720f1e2 | 2013-03-06 11:51:47 +0000 | [diff] [blame] | 2897 | goto out; |
| 2898 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2899 | |
| 2900 | if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) && |
Dongsheng Yang | e2d1f92 | 2015-02-06 10:26:52 -0500 | [diff] [blame] | 2901 | qg->reserved + (s64)qg->excl + num_bytes > |
Wang Shilong | 720f1e2 | 2013-03-06 11:51:47 +0000 | [diff] [blame] | 2902 | qg->max_excl) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2903 | ret = -EDQUOT; |
Wang Shilong | 720f1e2 | 2013-03-06 11:51:47 +0000 | [diff] [blame] | 2904 | goto out; |
| 2905 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2906 | |
| 2907 | list_for_each_entry(glist, &qg->groups, next_group) { |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 2908 | ret = ulist_add(fs_info->qgroup_ulist, |
| 2909 | glist->group->qgroupid, |
Wang Shilong | 3c97185 | 2013-04-17 14:00:36 +0000 | [diff] [blame] | 2910 | (uintptr_t)glist->group, GFP_ATOMIC); |
| 2911 | if (ret < 0) |
| 2912 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2913 | } |
| 2914 | } |
Wang Shilong | 3c97185 | 2013-04-17 14:00:36 +0000 | [diff] [blame] | 2915 | ret = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2916 | /* |
| 2917 | * no limits exceeded, now record the reservation into all qgroups |
| 2918 | */ |
| 2919 | ULIST_ITER_INIT(&uiter); |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 2920 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2921 | struct btrfs_qgroup *qg; |
| 2922 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2923 | qg = u64_to_ptr(unode->aux); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2924 | |
Dongsheng Yang | e2d1f92 | 2015-02-06 10:26:52 -0500 | [diff] [blame] | 2925 | qg->reserved += num_bytes; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | out: |
| 2929 | spin_unlock(&fs_info->qgroup_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2930 | return ret; |
| 2931 | } |
| 2932 | |
| 2933 | void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes) |
| 2934 | { |
| 2935 | struct btrfs_root *quota_root; |
| 2936 | struct btrfs_qgroup *qgroup; |
| 2937 | struct btrfs_fs_info *fs_info = root->fs_info; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2938 | struct ulist_node *unode; |
| 2939 | struct ulist_iterator uiter; |
| 2940 | u64 ref_root = root->root_key.objectid; |
Wang Shilong | 3c97185 | 2013-04-17 14:00:36 +0000 | [diff] [blame] | 2941 | int ret = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2942 | |
| 2943 | if (!is_fstree(ref_root)) |
| 2944 | return; |
| 2945 | |
| 2946 | if (num_bytes == 0) |
| 2947 | return; |
| 2948 | |
| 2949 | spin_lock(&fs_info->qgroup_lock); |
| 2950 | |
| 2951 | quota_root = fs_info->quota_root; |
| 2952 | if (!quota_root) |
| 2953 | goto out; |
| 2954 | |
| 2955 | qgroup = find_qgroup_rb(fs_info, ref_root); |
| 2956 | if (!qgroup) |
| 2957 | goto out; |
| 2958 | |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 2959 | ulist_reinit(fs_info->qgroup_ulist); |
| 2960 | ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid, |
Wang Shilong | 3c97185 | 2013-04-17 14:00:36 +0000 | [diff] [blame] | 2961 | (uintptr_t)qgroup, GFP_ATOMIC); |
| 2962 | if (ret < 0) |
| 2963 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2964 | ULIST_ITER_INIT(&uiter); |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 2965 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2966 | struct btrfs_qgroup *qg; |
| 2967 | struct btrfs_qgroup_list *glist; |
| 2968 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2969 | qg = u64_to_ptr(unode->aux); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2970 | |
Dongsheng Yang | e2d1f92 | 2015-02-06 10:26:52 -0500 | [diff] [blame] | 2971 | qg->reserved -= num_bytes; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2972 | |
| 2973 | list_for_each_entry(glist, &qg->groups, next_group) { |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 2974 | ret = ulist_add(fs_info->qgroup_ulist, |
| 2975 | glist->group->qgroupid, |
Wang Shilong | 3c97185 | 2013-04-17 14:00:36 +0000 | [diff] [blame] | 2976 | (uintptr_t)glist->group, GFP_ATOMIC); |
| 2977 | if (ret < 0) |
| 2978 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2979 | } |
| 2980 | } |
| 2981 | |
| 2982 | out: |
| 2983 | spin_unlock(&fs_info->qgroup_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2984 | } |
| 2985 | |
| 2986 | void assert_qgroups_uptodate(struct btrfs_trans_handle *trans) |
| 2987 | { |
| 2988 | if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq) |
| 2989 | return; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2990 | btrfs_err(trans->root->fs_info, |
| 2991 | "qgroups not uptodate in trans handle %p: list is%s empty, " |
| 2992 | "seq is %#x.%x", |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2993 | trans, list_empty(&trans->qgroup_ref_list) ? "" : " not", |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 2994 | (u32)(trans->delayed_ref_elem.seq >> 32), |
| 2995 | (u32)trans->delayed_ref_elem.seq); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2996 | BUG(); |
| 2997 | } |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 2998 | |
| 2999 | /* |
| 3000 | * returns < 0 on error, 0 when more leafs are to be scanned. |
Qu Wenruo | 3393168 | 2015-02-27 16:24:24 +0800 | [diff] [blame] | 3001 | * returns 1 when done. |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3002 | */ |
| 3003 | static int |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3004 | qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
Qu Wenruo | 9d220c9 | 2015-04-13 11:02:16 +0800 | [diff] [blame] | 3005 | struct btrfs_trans_handle *trans, |
| 3006 | struct extent_buffer *scratch_leaf) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3007 | { |
| 3008 | struct btrfs_key found; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3009 | struct ulist *roots = NULL; |
David Sterba | 3284da7 | 2015-02-25 15:47:32 +0100 | [diff] [blame] | 3010 | struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3011 | u64 num_bytes; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3012 | int slot; |
| 3013 | int ret; |
| 3014 | |
| 3015 | path->leave_spinning = 1; |
| 3016 | mutex_lock(&fs_info->qgroup_rescan_lock); |
| 3017 | ret = btrfs_search_slot_for_read(fs_info->extent_root, |
| 3018 | &fs_info->qgroup_rescan_progress, |
| 3019 | path, 1, 0); |
| 3020 | |
| 3021 | pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3022 | fs_info->qgroup_rescan_progress.objectid, |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3023 | fs_info->qgroup_rescan_progress.type, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3024 | fs_info->qgroup_rescan_progress.offset, ret); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3025 | |
| 3026 | if (ret) { |
| 3027 | /* |
| 3028 | * The rescan is about to end, we will not be scanning any |
| 3029 | * further blocks. We cannot unset the RESCAN flag here, because |
| 3030 | * we want to commit the transaction if everything went well. |
| 3031 | * To make the live accounting work in this phase, we set our |
| 3032 | * scan progress pointer such that every real extent objectid |
| 3033 | * will be smaller. |
| 3034 | */ |
| 3035 | fs_info->qgroup_rescan_progress.objectid = (u64)-1; |
| 3036 | btrfs_release_path(path); |
| 3037 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3038 | return ret; |
| 3039 | } |
| 3040 | |
| 3041 | btrfs_item_key_to_cpu(path->nodes[0], &found, |
| 3042 | btrfs_header_nritems(path->nodes[0]) - 1); |
| 3043 | fs_info->qgroup_rescan_progress.objectid = found.objectid + 1; |
| 3044 | |
| 3045 | btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); |
| 3046 | memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf)); |
| 3047 | slot = path->slots[0]; |
| 3048 | btrfs_release_path(path); |
| 3049 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3050 | |
| 3051 | for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) { |
| 3052 | btrfs_item_key_to_cpu(scratch_leaf, &found, slot); |
Josef Bacik | 3a6d75e | 2014-01-23 16:45:10 -0500 | [diff] [blame] | 3053 | if (found.type != BTRFS_EXTENT_ITEM_KEY && |
| 3054 | found.type != BTRFS_METADATA_ITEM_KEY) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3055 | continue; |
Josef Bacik | 3a6d75e | 2014-01-23 16:45:10 -0500 | [diff] [blame] | 3056 | if (found.type == BTRFS_METADATA_ITEM_KEY) |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 3057 | num_bytes = fs_info->extent_root->nodesize; |
Josef Bacik | 3a6d75e | 2014-01-23 16:45:10 -0500 | [diff] [blame] | 3058 | else |
| 3059 | num_bytes = found.offset; |
| 3060 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3061 | ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0, |
| 3062 | &roots); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3063 | if (ret < 0) |
| 3064 | goto out; |
Qu Wenruo | 9d220c9 | 2015-04-13 11:02:16 +0800 | [diff] [blame] | 3065 | /* For rescan, just pass old_roots as NULL */ |
| 3066 | ret = btrfs_qgroup_account_extent(trans, fs_info, |
| 3067 | found.objectid, num_bytes, NULL, roots); |
| 3068 | if (ret < 0) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3069 | goto out; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3070 | } |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3071 | out: |
| 3072 | btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); |
| 3073 | |
| 3074 | return ret; |
| 3075 | } |
| 3076 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 3077 | static void btrfs_qgroup_rescan_worker(struct btrfs_work *work) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3078 | { |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3079 | struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info, |
| 3080 | qgroup_rescan_work); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3081 | struct btrfs_path *path; |
| 3082 | struct btrfs_trans_handle *trans = NULL; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3083 | struct extent_buffer *scratch_leaf = NULL; |
| 3084 | int err = -ENOMEM; |
Qu Wenruo | 53b7cde | 2015-02-27 16:24:25 +0800 | [diff] [blame] | 3085 | int ret = 0; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3086 | |
| 3087 | path = btrfs_alloc_path(); |
| 3088 | if (!path) |
| 3089 | goto out; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3090 | scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS); |
| 3091 | if (!scratch_leaf) |
| 3092 | goto out; |
| 3093 | |
| 3094 | err = 0; |
| 3095 | while (!err) { |
| 3096 | trans = btrfs_start_transaction(fs_info->fs_root, 0); |
| 3097 | if (IS_ERR(trans)) { |
| 3098 | err = PTR_ERR(trans); |
| 3099 | break; |
| 3100 | } |
| 3101 | if (!fs_info->quota_enabled) { |
| 3102 | err = -EINTR; |
| 3103 | } else { |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3104 | err = qgroup_rescan_leaf(fs_info, path, trans, |
Qu Wenruo | 9d220c9 | 2015-04-13 11:02:16 +0800 | [diff] [blame] | 3105 | scratch_leaf); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3106 | } |
| 3107 | if (err > 0) |
| 3108 | btrfs_commit_transaction(trans, fs_info->fs_root); |
| 3109 | else |
| 3110 | btrfs_end_transaction(trans, fs_info->fs_root); |
| 3111 | } |
| 3112 | |
| 3113 | out: |
| 3114 | kfree(scratch_leaf); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3115 | btrfs_free_path(path); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3116 | |
| 3117 | mutex_lock(&fs_info->qgroup_rescan_lock); |
| 3118 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
| 3119 | |
Qu Wenruo | 3393168 | 2015-02-27 16:24:24 +0800 | [diff] [blame] | 3120 | if (err > 0 && |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3121 | fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) { |
| 3122 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 3123 | } else if (err < 0) { |
| 3124 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 3125 | } |
| 3126 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3127 | |
Qu Wenruo | 53b7cde | 2015-02-27 16:24:25 +0800 | [diff] [blame] | 3128 | /* |
| 3129 | * only update status, since the previous part has alreay updated the |
| 3130 | * qgroup info. |
| 3131 | */ |
| 3132 | trans = btrfs_start_transaction(fs_info->quota_root, 1); |
| 3133 | if (IS_ERR(trans)) { |
| 3134 | err = PTR_ERR(trans); |
| 3135 | btrfs_err(fs_info, |
| 3136 | "fail to start transaction for status update: %d\n", |
| 3137 | err); |
| 3138 | goto done; |
| 3139 | } |
| 3140 | ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root); |
| 3141 | if (ret < 0) { |
| 3142 | err = ret; |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 3143 | btrfs_err(fs_info, "fail to update qgroup status: %d\n", err); |
Qu Wenruo | 53b7cde | 2015-02-27 16:24:25 +0800 | [diff] [blame] | 3144 | } |
| 3145 | btrfs_end_transaction(trans, fs_info->quota_root); |
| 3146 | |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3147 | if (err >= 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3148 | btrfs_info(fs_info, "qgroup scan completed%s", |
Qu Wenruo | 3393168 | 2015-02-27 16:24:24 +0800 | [diff] [blame] | 3149 | err > 0 ? " (inconsistency flag cleared)" : ""); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3150 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3151 | btrfs_err(fs_info, "qgroup scan failed with %d", err); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3152 | } |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 3153 | |
Qu Wenruo | 53b7cde | 2015-02-27 16:24:25 +0800 | [diff] [blame] | 3154 | done: |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 3155 | complete_all(&fs_info->qgroup_rescan_completion); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3158 | /* |
| 3159 | * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all |
| 3160 | * memory required for the rescan context. |
| 3161 | */ |
| 3162 | static int |
| 3163 | qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, |
| 3164 | int init_flags) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3165 | { |
| 3166 | int ret = 0; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3167 | |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3168 | if (!init_flags && |
| 3169 | (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) || |
| 3170 | !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) { |
| 3171 | ret = -EINVAL; |
| 3172 | goto err; |
| 3173 | } |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3174 | |
| 3175 | mutex_lock(&fs_info->qgroup_rescan_lock); |
| 3176 | spin_lock(&fs_info->qgroup_lock); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3177 | |
| 3178 | if (init_flags) { |
| 3179 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) |
| 3180 | ret = -EINPROGRESS; |
| 3181 | else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) |
| 3182 | ret = -EINVAL; |
| 3183 | |
| 3184 | if (ret) { |
| 3185 | spin_unlock(&fs_info->qgroup_lock); |
| 3186 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3187 | goto err; |
| 3188 | } |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3189 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
| 3190 | } |
| 3191 | |
| 3192 | memset(&fs_info->qgroup_rescan_progress, 0, |
| 3193 | sizeof(fs_info->qgroup_rescan_progress)); |
| 3194 | fs_info->qgroup_rescan_progress.objectid = progress_objectid; |
| 3195 | |
| 3196 | spin_unlock(&fs_info->qgroup_lock); |
| 3197 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3198 | |
| 3199 | init_completion(&fs_info->qgroup_rescan_completion); |
| 3200 | |
| 3201 | memset(&fs_info->qgroup_rescan_work, 0, |
| 3202 | sizeof(fs_info->qgroup_rescan_work)); |
Qu Wenruo | fc97fab | 2014-02-28 10:46:16 +0800 | [diff] [blame] | 3203 | btrfs_init_work(&fs_info->qgroup_rescan_work, |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3204 | btrfs_qgroup_rescan_helper, |
Qu Wenruo | fc97fab | 2014-02-28 10:46:16 +0800 | [diff] [blame] | 3205 | btrfs_qgroup_rescan_worker, NULL, NULL); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3206 | |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3207 | if (ret) { |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3208 | err: |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3209 | btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3210 | return ret; |
| 3211 | } |
| 3212 | |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3213 | return 0; |
| 3214 | } |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3215 | |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3216 | static void |
| 3217 | qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info) |
| 3218 | { |
| 3219 | struct rb_node *n; |
| 3220 | struct btrfs_qgroup *qgroup; |
| 3221 | |
| 3222 | spin_lock(&fs_info->qgroup_lock); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3223 | /* clear all current qgroup tracking information */ |
| 3224 | for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) { |
| 3225 | qgroup = rb_entry(n, struct btrfs_qgroup, node); |
| 3226 | qgroup->rfer = 0; |
| 3227 | qgroup->rfer_cmpr = 0; |
| 3228 | qgroup->excl = 0; |
| 3229 | qgroup->excl_cmpr = 0; |
| 3230 | } |
| 3231 | spin_unlock(&fs_info->qgroup_lock); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3232 | } |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3233 | |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3234 | int |
| 3235 | btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info) |
| 3236 | { |
| 3237 | int ret = 0; |
| 3238 | struct btrfs_trans_handle *trans; |
| 3239 | |
| 3240 | ret = qgroup_rescan_init(fs_info, 0, 1); |
| 3241 | if (ret) |
| 3242 | return ret; |
| 3243 | |
| 3244 | /* |
| 3245 | * We have set the rescan_progress to 0, which means no more |
| 3246 | * delayed refs will be accounted by btrfs_qgroup_account_ref. |
| 3247 | * However, btrfs_qgroup_account_ref may be right after its call |
| 3248 | * to btrfs_find_all_roots, in which case it would still do the |
| 3249 | * accounting. |
| 3250 | * To solve this, we're committing the transaction, which will |
| 3251 | * ensure we run all delayed refs and only after that, we are |
| 3252 | * going to clear all tracking information for a clean start. |
| 3253 | */ |
| 3254 | |
| 3255 | trans = btrfs_join_transaction(fs_info->fs_root); |
| 3256 | if (IS_ERR(trans)) { |
| 3257 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
| 3258 | return PTR_ERR(trans); |
| 3259 | } |
| 3260 | ret = btrfs_commit_transaction(trans, fs_info->fs_root); |
| 3261 | if (ret) { |
| 3262 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
| 3263 | return ret; |
| 3264 | } |
| 3265 | |
| 3266 | qgroup_rescan_zero_tracking(fs_info); |
| 3267 | |
Qu Wenruo | fc97fab | 2014-02-28 10:46:16 +0800 | [diff] [blame] | 3268 | btrfs_queue_work(fs_info->qgroup_rescan_workers, |
| 3269 | &fs_info->qgroup_rescan_work); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3270 | |
| 3271 | return 0; |
| 3272 | } |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 3273 | |
| 3274 | int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info) |
| 3275 | { |
| 3276 | int running; |
| 3277 | int ret = 0; |
| 3278 | |
| 3279 | mutex_lock(&fs_info->qgroup_rescan_lock); |
| 3280 | spin_lock(&fs_info->qgroup_lock); |
| 3281 | running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
| 3282 | spin_unlock(&fs_info->qgroup_lock); |
| 3283 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3284 | |
| 3285 | if (running) |
| 3286 | ret = wait_for_completion_interruptible( |
| 3287 | &fs_info->qgroup_rescan_completion); |
| 3288 | |
| 3289 | return ret; |
| 3290 | } |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3291 | |
| 3292 | /* |
| 3293 | * this is only called from open_ctree where we're still single threaded, thus |
| 3294 | * locking is omitted here. |
| 3295 | */ |
| 3296 | void |
| 3297 | btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info) |
| 3298 | { |
| 3299 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) |
Qu Wenruo | fc97fab | 2014-02-28 10:46:16 +0800 | [diff] [blame] | 3300 | btrfs_queue_work(fs_info->qgroup_rescan_workers, |
| 3301 | &fs_info->qgroup_rescan_work); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3302 | } |