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