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