1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011 STRATO. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/pagemap.h>
8 #include <linux/writeback.h>
9 #include <linux/blkdev.h>
10 #include <linux/rbtree.h>
11 #include <linux/slab.h>
12 #include <linux/workqueue.h>
13 #include <linux/btrfs.h>
14 #include <linux/sched/mm.h>
17 #include "transaction.h"
22 #include "extent_io.h"
24 #include "block-group.h"
26 #include "tree-mod-log.h"
28 #include "accessors.h"
29 #include "extent-tree.h"
30 #include "root-tree.h"
31 #include "tree-checker.h"
33 enum btrfs_qgroup_mode btrfs_qgroup_mode(struct btrfs_fs_info *fs_info)
35 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
36 return BTRFS_QGROUP_MODE_DISABLED;
37 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE)
38 return BTRFS_QGROUP_MODE_SIMPLE;
39 return BTRFS_QGROUP_MODE_FULL;
42 bool btrfs_qgroup_enabled(struct btrfs_fs_info *fs_info)
44 return btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_DISABLED;
47 bool btrfs_qgroup_full_accounting(struct btrfs_fs_info *fs_info)
49 return btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL;
53 * Helpers to access qgroup reservation
55 * Callers should ensure the lock context and type are valid
58 static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
63 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
64 ret += qgroup->rsv.values[i];
69 #ifdef CONFIG_BTRFS_DEBUG
70 static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
72 if (type == BTRFS_QGROUP_RSV_DATA)
74 if (type == BTRFS_QGROUP_RSV_META_PERTRANS)
75 return "meta_pertrans";
76 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
77 return "meta_prealloc";
82 static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
83 struct btrfs_qgroup *qgroup, u64 num_bytes,
84 enum btrfs_qgroup_rsv_type type)
86 trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
87 qgroup->rsv.values[type] += num_bytes;
90 static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
91 struct btrfs_qgroup *qgroup, u64 num_bytes,
92 enum btrfs_qgroup_rsv_type type)
94 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
95 if (qgroup->rsv.values[type] >= num_bytes) {
96 qgroup->rsv.values[type] -= num_bytes;
99 #ifdef CONFIG_BTRFS_DEBUG
101 "qgroup %llu %s reserved space underflow, have %llu to free %llu",
102 qgroup->qgroupid, qgroup_rsv_type_str(type),
103 qgroup->rsv.values[type], num_bytes);
105 qgroup->rsv.values[type] = 0;
108 static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
109 struct btrfs_qgroup *dest,
110 struct btrfs_qgroup *src)
114 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
115 qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
118 static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
119 struct btrfs_qgroup *dest,
120 struct btrfs_qgroup *src)
124 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
125 qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
128 static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
131 if (qg->old_refcnt < seq)
132 qg->old_refcnt = seq;
133 qg->old_refcnt += mod;
136 static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
139 if (qg->new_refcnt < seq)
140 qg->new_refcnt = seq;
141 qg->new_refcnt += mod;
144 static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
146 if (qg->old_refcnt < seq)
148 return qg->old_refcnt - seq;
151 static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
153 if (qg->new_refcnt < seq)
155 return qg->new_refcnt - seq;
159 * glue structure to represent the relations between qgroups.
161 struct btrfs_qgroup_list {
162 struct list_head next_group;
163 struct list_head next_member;
164 struct btrfs_qgroup *group;
165 struct btrfs_qgroup *member;
169 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
171 static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
173 /* must be called with qgroup_ioctl_lock held */
174 static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
177 struct rb_node *n = fs_info->qgroup_tree.rb_node;
178 struct btrfs_qgroup *qgroup;
181 qgroup = rb_entry(n, struct btrfs_qgroup, node);
182 if (qgroup->qgroupid < qgroupid)
184 else if (qgroup->qgroupid > qgroupid)
193 * Add qgroup to the filesystem's qgroup tree.
195 * Must be called with qgroup_lock held and @prealloc preallocated.
197 * The control on the lifespan of @prealloc would be transferred to this
198 * function, thus caller should no longer touch @prealloc.
200 static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
201 struct btrfs_qgroup *prealloc,
204 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
205 struct rb_node *parent = NULL;
206 struct btrfs_qgroup *qgroup;
208 /* Caller must have pre-allocated @prealloc. */
213 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
215 if (qgroup->qgroupid < qgroupid) {
217 } else if (qgroup->qgroupid > qgroupid) {
226 qgroup->qgroupid = qgroupid;
227 INIT_LIST_HEAD(&qgroup->groups);
228 INIT_LIST_HEAD(&qgroup->members);
229 INIT_LIST_HEAD(&qgroup->dirty);
230 INIT_LIST_HEAD(&qgroup->iterator);
231 INIT_LIST_HEAD(&qgroup->nested_iterator);
233 rb_link_node(&qgroup->node, parent, p);
234 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
239 static void __del_qgroup_rb(struct btrfs_fs_info *fs_info,
240 struct btrfs_qgroup *qgroup)
242 struct btrfs_qgroup_list *list;
244 list_del(&qgroup->dirty);
245 while (!list_empty(&qgroup->groups)) {
246 list = list_first_entry(&qgroup->groups,
247 struct btrfs_qgroup_list, next_group);
248 list_del(&list->next_group);
249 list_del(&list->next_member);
253 while (!list_empty(&qgroup->members)) {
254 list = list_first_entry(&qgroup->members,
255 struct btrfs_qgroup_list, next_member);
256 list_del(&list->next_group);
257 list_del(&list->next_member);
262 /* must be called with qgroup_lock held */
263 static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
265 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
270 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
271 __del_qgroup_rb(fs_info, qgroup);
276 * Add relation specified by two qgroups.
278 * Must be called with qgroup_lock held, the ownership of @prealloc is
279 * transferred to this function and caller should not touch it anymore.
281 * Return: 0 on success
282 * -ENOENT if one of the qgroups is NULL
285 static int __add_relation_rb(struct btrfs_qgroup_list *prealloc,
286 struct btrfs_qgroup *member,
287 struct btrfs_qgroup *parent)
289 if (!member || !parent) {
294 prealloc->group = parent;
295 prealloc->member = member;
296 list_add_tail(&prealloc->next_group, &member->groups);
297 list_add_tail(&prealloc->next_member, &parent->members);
303 * Add relation specified by two qgroup ids.
305 * Must be called with qgroup_lock held.
307 * Return: 0 on success
308 * -ENOENT if one of the ids does not exist
311 static int add_relation_rb(struct btrfs_fs_info *fs_info,
312 struct btrfs_qgroup_list *prealloc,
313 u64 memberid, u64 parentid)
315 struct btrfs_qgroup *member;
316 struct btrfs_qgroup *parent;
318 member = find_qgroup_rb(fs_info, memberid);
319 parent = find_qgroup_rb(fs_info, parentid);
321 return __add_relation_rb(prealloc, member, parent);
324 /* Must be called with qgroup_lock held */
325 static int del_relation_rb(struct btrfs_fs_info *fs_info,
326 u64 memberid, u64 parentid)
328 struct btrfs_qgroup *member;
329 struct btrfs_qgroup *parent;
330 struct btrfs_qgroup_list *list;
332 member = find_qgroup_rb(fs_info, memberid);
333 parent = find_qgroup_rb(fs_info, parentid);
334 if (!member || !parent)
337 list_for_each_entry(list, &member->groups, next_group) {
338 if (list->group == parent) {
339 list_del(&list->next_group);
340 list_del(&list->next_member);
348 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
349 int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
352 struct btrfs_qgroup *qgroup;
354 qgroup = find_qgroup_rb(fs_info, qgroupid);
357 if (qgroup->rfer != rfer || qgroup->excl != excl)
363 static void qgroup_mark_inconsistent(struct btrfs_fs_info *fs_info)
365 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
367 fs_info->qgroup_flags |= (BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT |
368 BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN |
369 BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING);
372 static void qgroup_read_enable_gen(struct btrfs_fs_info *fs_info,
373 struct extent_buffer *leaf, int slot,
374 struct btrfs_qgroup_status_item *ptr)
376 ASSERT(btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
377 ASSERT(btrfs_item_size(leaf, slot) >= sizeof(*ptr));
378 fs_info->qgroup_enable_gen = btrfs_qgroup_status_enable_gen(leaf, ptr);
382 * The full config is read in one go, only called from open_ctree()
383 * It doesn't use any locking, as at this point we're still single-threaded
385 int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
387 struct btrfs_key key;
388 struct btrfs_key found_key;
389 struct btrfs_root *quota_root = fs_info->quota_root;
390 struct btrfs_path *path = NULL;
391 struct extent_buffer *l;
395 u64 rescan_progress = 0;
397 if (!fs_info->quota_root)
400 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
401 if (!fs_info->qgroup_ulist) {
406 path = btrfs_alloc_path();
412 ret = btrfs_sysfs_add_qgroups(fs_info);
415 /* default this to quota off, in case no status key is found */
416 fs_info->qgroup_flags = 0;
419 * pass 1: read status, all qgroup infos and limits
424 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
429 struct btrfs_qgroup *qgroup;
431 slot = path->slots[0];
433 btrfs_item_key_to_cpu(l, &found_key, slot);
435 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
436 struct btrfs_qgroup_status_item *ptr;
438 ptr = btrfs_item_ptr(l, slot,
439 struct btrfs_qgroup_status_item);
441 if (btrfs_qgroup_status_version(l, ptr) !=
442 BTRFS_QGROUP_STATUS_VERSION) {
444 "old qgroup version, quota disabled");
447 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l, ptr);
448 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE) {
449 qgroup_read_enable_gen(fs_info, l, slot, ptr);
450 } else if (btrfs_qgroup_status_generation(l, ptr) != fs_info->generation) {
451 qgroup_mark_inconsistent(fs_info);
453 "qgroup generation mismatch, marked as inconsistent");
455 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
459 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
460 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
463 qgroup = find_qgroup_rb(fs_info, found_key.offset);
464 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
465 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
466 btrfs_err(fs_info, "inconsistent qgroup config");
467 qgroup_mark_inconsistent(fs_info);
470 struct btrfs_qgroup *prealloc;
471 struct btrfs_root *tree_root = fs_info->tree_root;
473 prealloc = kzalloc(sizeof(*prealloc), GFP_KERNEL);
478 qgroup = add_qgroup_rb(fs_info, prealloc, found_key.offset);
480 * If a qgroup exists for a subvolume ID, it is possible
481 * that subvolume has been deleted, in which case
482 * re-using that ID would lead to incorrect accounting.
484 * Ensure that we skip any such subvol ids.
486 * We don't need to lock because this is only called
487 * during mount before we start doing things like creating
490 if (is_fstree(qgroup->qgroupid) &&
491 qgroup->qgroupid > tree_root->free_objectid)
493 * Don't need to check against BTRFS_LAST_FREE_OBJECTID,
494 * as it will get checked on the next call to
495 * btrfs_get_free_objectid.
497 tree_root->free_objectid = qgroup->qgroupid + 1;
499 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
503 switch (found_key.type) {
504 case BTRFS_QGROUP_INFO_KEY: {
505 struct btrfs_qgroup_info_item *ptr;
507 ptr = btrfs_item_ptr(l, slot,
508 struct btrfs_qgroup_info_item);
509 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
510 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
511 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
512 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
513 /* generation currently unused */
516 case BTRFS_QGROUP_LIMIT_KEY: {
517 struct btrfs_qgroup_limit_item *ptr;
519 ptr = btrfs_item_ptr(l, slot,
520 struct btrfs_qgroup_limit_item);
521 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
522 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
523 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
524 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
525 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
530 ret = btrfs_next_item(quota_root, path);
536 btrfs_release_path(path);
539 * pass 2: read all qgroup relations
542 key.type = BTRFS_QGROUP_RELATION_KEY;
544 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
548 struct btrfs_qgroup_list *list = NULL;
550 slot = path->slots[0];
552 btrfs_item_key_to_cpu(l, &found_key, slot);
554 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
557 if (found_key.objectid > found_key.offset) {
558 /* parent <- member, not needed to build config */
559 /* FIXME should we omit the key completely? */
563 list = kzalloc(sizeof(*list), GFP_KERNEL);
568 ret = add_relation_rb(fs_info, list, found_key.objectid,
571 if (ret == -ENOENT) {
573 "orphan qgroup relation 0x%llx->0x%llx",
574 found_key.objectid, found_key.offset);
575 ret = 0; /* ignore the error */
580 ret = btrfs_next_item(quota_root, path);
587 btrfs_free_path(path);
588 fs_info->qgroup_flags |= flags;
590 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)
591 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
592 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
593 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
595 ulist_free(fs_info->qgroup_ulist);
596 fs_info->qgroup_ulist = NULL;
597 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
598 btrfs_sysfs_del_qgroups(fs_info);
601 return ret < 0 ? ret : 0;
605 * Called in close_ctree() when quota is still enabled. This verifies we don't
606 * leak some reserved space.
608 * Return false if no reserved space is left.
609 * Return true if some reserved space is leaked.
611 bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info)
613 struct rb_node *node;
616 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED)
619 * Since we're unmounting, there is no race and no need to grab qgroup
620 * lock. And here we don't go post-order to provide a more user
621 * friendly sorted result.
623 for (node = rb_first(&fs_info->qgroup_tree); node; node = rb_next(node)) {
624 struct btrfs_qgroup *qgroup;
627 qgroup = rb_entry(node, struct btrfs_qgroup, node);
628 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++) {
629 if (qgroup->rsv.values[i]) {
632 "qgroup %hu/%llu has unreleased space, type %d rsv %llu",
633 btrfs_qgroup_level(qgroup->qgroupid),
634 btrfs_qgroup_subvolid(qgroup->qgroupid),
635 i, qgroup->rsv.values[i]);
643 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
644 * first two are in single-threaded paths.And for the third one, we have set
645 * quota_root to be null with qgroup_lock held before, so it is safe to clean
646 * up the in-memory structures without qgroup_lock held.
648 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
651 struct btrfs_qgroup *qgroup;
653 while ((n = rb_first(&fs_info->qgroup_tree))) {
654 qgroup = rb_entry(n, struct btrfs_qgroup, node);
655 rb_erase(n, &fs_info->qgroup_tree);
656 __del_qgroup_rb(fs_info, qgroup);
657 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
661 * We call btrfs_free_qgroup_config() when unmounting
662 * filesystem and disabling quota, so we set qgroup_ulist
663 * to be null here to avoid double free.
665 ulist_free(fs_info->qgroup_ulist);
666 fs_info->qgroup_ulist = NULL;
667 btrfs_sysfs_del_qgroups(fs_info);
670 static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
674 struct btrfs_root *quota_root = trans->fs_info->quota_root;
675 struct btrfs_path *path;
676 struct btrfs_key key;
678 path = btrfs_alloc_path();
683 key.type = BTRFS_QGROUP_RELATION_KEY;
686 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
688 btrfs_mark_buffer_dirty(trans, path->nodes[0]);
690 btrfs_free_path(path);
694 static int del_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
698 struct btrfs_root *quota_root = trans->fs_info->quota_root;
699 struct btrfs_path *path;
700 struct btrfs_key key;
702 path = btrfs_alloc_path();
707 key.type = BTRFS_QGROUP_RELATION_KEY;
710 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
719 ret = btrfs_del_item(trans, quota_root, path);
721 btrfs_free_path(path);
725 static int add_qgroup_item(struct btrfs_trans_handle *trans,
726 struct btrfs_root *quota_root, u64 qgroupid)
729 struct btrfs_path *path;
730 struct btrfs_qgroup_info_item *qgroup_info;
731 struct btrfs_qgroup_limit_item *qgroup_limit;
732 struct extent_buffer *leaf;
733 struct btrfs_key key;
735 if (btrfs_is_testing(quota_root->fs_info))
738 path = btrfs_alloc_path();
743 key.type = BTRFS_QGROUP_INFO_KEY;
744 key.offset = qgroupid;
747 * Avoid a transaction abort by catching -EEXIST here. In that
748 * case, we proceed by re-initializing the existing structure
752 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
753 sizeof(*qgroup_info));
754 if (ret && ret != -EEXIST)
757 leaf = path->nodes[0];
758 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
759 struct btrfs_qgroup_info_item);
760 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
761 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
762 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
763 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
764 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
766 btrfs_mark_buffer_dirty(trans, leaf);
768 btrfs_release_path(path);
770 key.type = BTRFS_QGROUP_LIMIT_KEY;
771 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
772 sizeof(*qgroup_limit));
773 if (ret && ret != -EEXIST)
776 leaf = path->nodes[0];
777 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
778 struct btrfs_qgroup_limit_item);
779 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
780 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
781 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
782 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
783 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
785 btrfs_mark_buffer_dirty(trans, leaf);
789 btrfs_free_path(path);
793 static int del_qgroup_item(struct btrfs_trans_handle *trans, u64 qgroupid)
796 struct btrfs_root *quota_root = trans->fs_info->quota_root;
797 struct btrfs_path *path;
798 struct btrfs_key key;
800 path = btrfs_alloc_path();
805 key.type = BTRFS_QGROUP_INFO_KEY;
806 key.offset = qgroupid;
807 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
816 ret = btrfs_del_item(trans, quota_root, path);
820 btrfs_release_path(path);
822 key.type = BTRFS_QGROUP_LIMIT_KEY;
823 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
832 ret = btrfs_del_item(trans, quota_root, path);
835 btrfs_free_path(path);
839 static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
840 struct btrfs_qgroup *qgroup)
842 struct btrfs_root *quota_root = trans->fs_info->quota_root;
843 struct btrfs_path *path;
844 struct btrfs_key key;
845 struct extent_buffer *l;
846 struct btrfs_qgroup_limit_item *qgroup_limit;
851 key.type = BTRFS_QGROUP_LIMIT_KEY;
852 key.offset = qgroup->qgroupid;
854 path = btrfs_alloc_path();
858 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
866 slot = path->slots[0];
867 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
868 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
869 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
870 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
871 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
872 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
874 btrfs_mark_buffer_dirty(trans, l);
877 btrfs_free_path(path);
881 static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
882 struct btrfs_qgroup *qgroup)
884 struct btrfs_fs_info *fs_info = trans->fs_info;
885 struct btrfs_root *quota_root = fs_info->quota_root;
886 struct btrfs_path *path;
887 struct btrfs_key key;
888 struct extent_buffer *l;
889 struct btrfs_qgroup_info_item *qgroup_info;
893 if (btrfs_is_testing(fs_info))
897 key.type = BTRFS_QGROUP_INFO_KEY;
898 key.offset = qgroup->qgroupid;
900 path = btrfs_alloc_path();
904 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
912 slot = path->slots[0];
913 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
914 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
915 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
916 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
917 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
918 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
920 btrfs_mark_buffer_dirty(trans, l);
923 btrfs_free_path(path);
927 static int update_qgroup_status_item(struct btrfs_trans_handle *trans)
929 struct btrfs_fs_info *fs_info = trans->fs_info;
930 struct btrfs_root *quota_root = fs_info->quota_root;
931 struct btrfs_path *path;
932 struct btrfs_key key;
933 struct extent_buffer *l;
934 struct btrfs_qgroup_status_item *ptr;
939 key.type = BTRFS_QGROUP_STATUS_KEY;
942 path = btrfs_alloc_path();
946 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
954 slot = path->slots[0];
955 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
956 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags &
957 BTRFS_QGROUP_STATUS_FLAGS_MASK);
958 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
959 btrfs_set_qgroup_status_rescan(l, ptr,
960 fs_info->qgroup_rescan_progress.objectid);
962 btrfs_mark_buffer_dirty(trans, l);
965 btrfs_free_path(path);
970 * called with qgroup_lock held
972 static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
973 struct btrfs_root *root)
975 struct btrfs_path *path;
976 struct btrfs_key key;
977 struct extent_buffer *leaf = NULL;
981 path = btrfs_alloc_path();
990 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
993 leaf = path->nodes[0];
994 nr = btrfs_header_nritems(leaf);
998 * delete the leaf one by one
999 * since the whole tree is going
1003 ret = btrfs_del_items(trans, root, path, 0, nr);
1007 btrfs_release_path(path);
1011 btrfs_free_path(path);
1015 int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
1016 struct btrfs_ioctl_quota_ctl_args *quota_ctl_args)
1018 struct btrfs_root *quota_root;
1019 struct btrfs_root *tree_root = fs_info->tree_root;
1020 struct btrfs_path *path = NULL;
1021 struct btrfs_qgroup_status_item *ptr;
1022 struct extent_buffer *leaf;
1023 struct btrfs_key key;
1024 struct btrfs_key found_key;
1025 struct btrfs_qgroup *qgroup = NULL;
1026 struct btrfs_qgroup *prealloc = NULL;
1027 struct btrfs_trans_handle *trans = NULL;
1028 struct ulist *ulist = NULL;
1029 const bool simple = (quota_ctl_args->cmd == BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA);
1034 * We need to have subvol_sem write locked, to prevent races between
1035 * concurrent tasks trying to enable quotas, because we will unlock
1036 * and relock qgroup_ioctl_lock before setting fs_info->quota_root
1037 * and before setting BTRFS_FS_QUOTA_ENABLED.
1039 lockdep_assert_held_write(&fs_info->subvol_sem);
1041 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
1043 "qgroups are currently unsupported in extent tree v2");
1047 mutex_lock(&fs_info->qgroup_ioctl_lock);
1048 if (fs_info->quota_root)
1051 ulist = ulist_alloc(GFP_KERNEL);
1057 ret = btrfs_sysfs_add_qgroups(fs_info);
1062 * Unlock qgroup_ioctl_lock before starting the transaction. This is to
1063 * avoid lock acquisition inversion problems (reported by lockdep) between
1064 * qgroup_ioctl_lock and the vfs freeze semaphores, acquired when we
1065 * start a transaction.
1066 * After we started the transaction lock qgroup_ioctl_lock again and
1067 * check if someone else created the quota root in the meanwhile. If so,
1068 * just return success and release the transaction handle.
1070 * Also we don't need to worry about someone else calling
1071 * btrfs_sysfs_add_qgroups() after we unlock and getting an error because
1072 * that function returns 0 (success) when the sysfs entries already exist.
1074 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1077 * 1 for quota root item
1078 * 1 for BTRFS_QGROUP_STATUS item
1080 * Yet we also need 2*n items for a QGROUP_INFO/QGROUP_LIMIT items
1081 * per subvolume. However those are not currently reserved since it
1082 * would be a lot of overkill.
1084 trans = btrfs_start_transaction(tree_root, 2);
1086 mutex_lock(&fs_info->qgroup_ioctl_lock);
1087 if (IS_ERR(trans)) {
1088 ret = PTR_ERR(trans);
1093 if (fs_info->quota_root)
1096 fs_info->qgroup_ulist = ulist;
1100 * initially create the quota tree
1102 quota_root = btrfs_create_tree(trans, BTRFS_QUOTA_TREE_OBJECTID);
1103 if (IS_ERR(quota_root)) {
1104 ret = PTR_ERR(quota_root);
1105 btrfs_abort_transaction(trans, ret);
1109 path = btrfs_alloc_path();
1112 btrfs_abort_transaction(trans, ret);
1117 key.type = BTRFS_QGROUP_STATUS_KEY;
1120 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
1123 btrfs_abort_transaction(trans, ret);
1127 leaf = path->nodes[0];
1128 ptr = btrfs_item_ptr(leaf, path->slots[0],
1129 struct btrfs_qgroup_status_item);
1130 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
1131 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
1132 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON;
1134 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE;
1135 btrfs_set_qgroup_status_enable_gen(leaf, ptr, trans->transid);
1137 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1139 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags &
1140 BTRFS_QGROUP_STATUS_FLAGS_MASK);
1141 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
1143 btrfs_mark_buffer_dirty(trans, leaf);
1146 key.type = BTRFS_ROOT_REF_KEY;
1149 btrfs_release_path(path);
1150 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
1154 btrfs_abort_transaction(trans, ret);
1159 slot = path->slots[0];
1160 leaf = path->nodes[0];
1161 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1163 if (found_key.type == BTRFS_ROOT_REF_KEY) {
1165 /* Release locks on tree_root before we access quota_root */
1166 btrfs_release_path(path);
1168 /* We should not have a stray @prealloc pointer. */
1169 ASSERT(prealloc == NULL);
1170 prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
1173 btrfs_abort_transaction(trans, ret);
1177 ret = add_qgroup_item(trans, quota_root,
1180 btrfs_abort_transaction(trans, ret);
1184 qgroup = add_qgroup_rb(fs_info, prealloc, found_key.offset);
1186 if (IS_ERR(qgroup)) {
1187 ret = PTR_ERR(qgroup);
1188 btrfs_abort_transaction(trans, ret);
1191 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1193 btrfs_abort_transaction(trans, ret);
1196 ret = btrfs_search_slot_for_read(tree_root, &found_key,
1199 btrfs_abort_transaction(trans, ret);
1204 * Shouldn't happen, but in case it does we
1205 * don't need to do the btrfs_next_item, just
1211 ret = btrfs_next_item(tree_root, path);
1213 btrfs_abort_transaction(trans, ret);
1221 btrfs_release_path(path);
1222 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
1224 btrfs_abort_transaction(trans, ret);
1228 ASSERT(prealloc == NULL);
1229 prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
1234 qgroup = add_qgroup_rb(fs_info, prealloc, BTRFS_FS_TREE_OBJECTID);
1236 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1238 btrfs_abort_transaction(trans, ret);
1242 fs_info->qgroup_enable_gen = trans->transid;
1244 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1246 * Commit the transaction while not holding qgroup_ioctl_lock, to avoid
1247 * a deadlock with tasks concurrently doing other qgroup operations, such
1248 * adding/removing qgroups or adding/deleting qgroup relations for example,
1249 * because all qgroup operations first start or join a transaction and then
1250 * lock the qgroup_ioctl_lock mutex.
1251 * We are safe from a concurrent task trying to enable quotas, by calling
1252 * this function, since we are serialized by fs_info->subvol_sem.
1254 ret = btrfs_commit_transaction(trans);
1256 mutex_lock(&fs_info->qgroup_ioctl_lock);
1261 * Set quota enabled flag after committing the transaction, to avoid
1262 * deadlocks on fs_info->qgroup_ioctl_lock with concurrent snapshot
1265 spin_lock(&fs_info->qgroup_lock);
1266 fs_info->quota_root = quota_root;
1267 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1269 btrfs_set_fs_incompat(fs_info, SIMPLE_QUOTA);
1270 spin_unlock(&fs_info->qgroup_lock);
1272 /* Skip rescan for simple qgroups. */
1273 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
1276 ret = qgroup_rescan_init(fs_info, 0, 1);
1278 qgroup_rescan_zero_tracking(fs_info);
1279 fs_info->qgroup_rescan_running = true;
1280 btrfs_queue_work(fs_info->qgroup_rescan_workers,
1281 &fs_info->qgroup_rescan_work);
1284 * We have set both BTRFS_FS_QUOTA_ENABLED and
1285 * BTRFS_QGROUP_STATUS_FLAG_ON, so we can only fail with
1286 * -EINPROGRESS. That can happen because someone started the
1287 * rescan worker by calling quota rescan ioctl before we
1288 * attempted to initialize the rescan worker. Failure due to
1289 * quotas disabled in the meanwhile is not possible, because
1290 * we are holding a write lock on fs_info->subvol_sem, which
1291 * is also acquired when disabling quotas.
1292 * Ignore such error, and any other error would need to undo
1293 * everything we did in the transaction we just committed.
1295 ASSERT(ret == -EINPROGRESS);
1300 btrfs_free_path(path);
1303 btrfs_put_root(quota_root);
1306 ulist_free(fs_info->qgroup_ulist);
1307 fs_info->qgroup_ulist = NULL;
1308 btrfs_sysfs_del_qgroups(fs_info);
1310 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1312 btrfs_end_transaction(trans);
1314 ret = btrfs_end_transaction(trans);
1321 * It is possible to have outstanding ordered extents which reserved bytes
1322 * before we disabled. We need to fully flush delalloc, ordered extents, and a
1323 * commit to ensure that we don't leak such reservations, only to have them
1324 * come back if we re-enable.
1326 * - enable simple quotas
1328 * - release it, store rsv_bytes in OE
1330 * - enable simple quotas (qgroup rsv are all 0)
1332 * - run delayed refs
1333 * - free rsv_bytes, resulting in miscounting or even underflow
1335 static int flush_reservations(struct btrfs_fs_info *fs_info)
1337 struct btrfs_trans_handle *trans;
1340 ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
1343 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1344 trans = btrfs_join_transaction(fs_info->tree_root);
1346 return PTR_ERR(trans);
1347 ret = btrfs_commit_transaction(trans);
1352 int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
1354 struct btrfs_root *quota_root = NULL;
1355 struct btrfs_trans_handle *trans = NULL;
1359 * We need to have subvol_sem write locked to prevent races with
1360 * snapshot creation.
1362 lockdep_assert_held_write(&fs_info->subvol_sem);
1365 * Relocation will mess with backrefs, so make sure we have the
1366 * cleaner_mutex held to protect us from relocate.
1368 lockdep_assert_held(&fs_info->cleaner_mutex);
1370 mutex_lock(&fs_info->qgroup_ioctl_lock);
1371 if (!fs_info->quota_root)
1375 * Unlock the qgroup_ioctl_lock mutex before waiting for the rescan worker to
1376 * complete. Otherwise we can deadlock because btrfs_remove_qgroup() needs
1377 * to lock that mutex while holding a transaction handle and the rescan
1378 * worker needs to commit a transaction.
1380 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1383 * Request qgroup rescan worker to complete and wait for it. This wait
1384 * must be done before transaction start for quota disable since it may
1385 * deadlock with transaction by the qgroup rescan worker.
1387 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1388 btrfs_qgroup_wait_for_completion(fs_info, false);
1391 * We have nothing held here and no trans handle, just return the error
1394 ret = flush_reservations(fs_info);
1399 * 1 For the root item
1401 * We should also reserve enough items for the quota tree deletion in
1402 * btrfs_clean_quota_tree but this is not done.
1404 * Also, we must always start a transaction without holding the mutex
1405 * qgroup_ioctl_lock, see btrfs_quota_enable().
1407 trans = btrfs_start_transaction(fs_info->tree_root, 1);
1409 mutex_lock(&fs_info->qgroup_ioctl_lock);
1410 if (IS_ERR(trans)) {
1411 ret = PTR_ERR(trans);
1413 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1417 if (!fs_info->quota_root)
1420 spin_lock(&fs_info->qgroup_lock);
1421 quota_root = fs_info->quota_root;
1422 fs_info->quota_root = NULL;
1423 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1424 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE;
1425 fs_info->qgroup_drop_subtree_thres = BTRFS_MAX_LEVEL;
1426 spin_unlock(&fs_info->qgroup_lock);
1428 btrfs_free_qgroup_config(fs_info);
1430 ret = btrfs_clean_quota_tree(trans, quota_root);
1432 btrfs_abort_transaction(trans, ret);
1436 ret = btrfs_del_root(trans, "a_root->root_key);
1438 btrfs_abort_transaction(trans, ret);
1442 spin_lock(&fs_info->trans_lock);
1443 list_del("a_root->dirty_list);
1444 spin_unlock(&fs_info->trans_lock);
1446 btrfs_tree_lock(quota_root->node);
1447 btrfs_clear_buffer_dirty(trans, quota_root->node);
1448 btrfs_tree_unlock(quota_root->node);
1449 btrfs_free_tree_block(trans, btrfs_root_id(quota_root),
1450 quota_root->node, 0, 1);
1454 btrfs_put_root(quota_root);
1455 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1457 btrfs_end_transaction(trans);
1459 ret = btrfs_commit_transaction(trans);
1463 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1464 struct btrfs_qgroup *qgroup)
1466 if (list_empty(&qgroup->dirty))
1467 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1470 static void qgroup_iterator_add(struct list_head *head, struct btrfs_qgroup *qgroup)
1472 if (!list_empty(&qgroup->iterator))
1475 list_add_tail(&qgroup->iterator, head);
1478 static void qgroup_iterator_clean(struct list_head *head)
1480 while (!list_empty(head)) {
1481 struct btrfs_qgroup *qgroup;
1483 qgroup = list_first_entry(head, struct btrfs_qgroup, iterator);
1484 list_del_init(&qgroup->iterator);
1489 * The easy accounting, we're updating qgroup relationship whose child qgroup
1490 * only has exclusive extents.
1492 * In this case, all exclusive extents will also be exclusive for parent, so
1493 * excl/rfer just get added/removed.
1495 * So is qgroup reservation space, which should also be added/removed to
1497 * Or when child tries to release reservation space, parent will underflow its
1498 * reservation (for relationship adding case).
1500 * Caller should hold fs_info->qgroup_lock.
1502 static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info, u64 ref_root,
1503 struct btrfs_qgroup *src, int sign)
1505 struct btrfs_qgroup *qgroup;
1506 struct btrfs_qgroup *cur;
1507 LIST_HEAD(qgroup_list);
1508 u64 num_bytes = src->excl;
1511 qgroup = find_qgroup_rb(fs_info, ref_root);
1515 qgroup_iterator_add(&qgroup_list, qgroup);
1516 list_for_each_entry(cur, &qgroup_list, iterator) {
1517 struct btrfs_qgroup_list *glist;
1519 qgroup->rfer += sign * num_bytes;
1520 qgroup->rfer_cmpr += sign * num_bytes;
1522 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1523 qgroup->excl += sign * num_bytes;
1524 qgroup->excl_cmpr += sign * num_bytes;
1527 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1529 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1530 qgroup_dirty(fs_info, qgroup);
1532 /* Append parent qgroups to @qgroup_list. */
1533 list_for_each_entry(glist, &qgroup->groups, next_group)
1534 qgroup_iterator_add(&qgroup_list, glist->group);
1538 qgroup_iterator_clean(&qgroup_list);
1544 * Quick path for updating qgroup with only excl refs.
1546 * In that case, just update all parent will be enough.
1547 * Or we needs to do a full rescan.
1548 * Caller should also hold fs_info->qgroup_lock.
1550 * Return 0 for quick update, return >0 for need to full rescan
1551 * and mark INCONSISTENT flag.
1552 * Return < 0 for other error.
1554 static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1555 u64 src, u64 dst, int sign)
1557 struct btrfs_qgroup *qgroup;
1560 qgroup = find_qgroup_rb(fs_info, src);
1563 if (qgroup->excl == qgroup->rfer) {
1564 ret = __qgroup_excl_accounting(fs_info, dst, qgroup, sign);
1571 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1575 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, u64 dst)
1577 struct btrfs_fs_info *fs_info = trans->fs_info;
1578 struct btrfs_qgroup *parent;
1579 struct btrfs_qgroup *member;
1580 struct btrfs_qgroup_list *list;
1581 struct btrfs_qgroup_list *prealloc = NULL;
1584 /* Check the level of src and dst first */
1585 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1588 mutex_lock(&fs_info->qgroup_ioctl_lock);
1589 if (!fs_info->quota_root) {
1593 member = find_qgroup_rb(fs_info, src);
1594 parent = find_qgroup_rb(fs_info, dst);
1595 if (!member || !parent) {
1600 /* check if such qgroup relation exist firstly */
1601 list_for_each_entry(list, &member->groups, next_group) {
1602 if (list->group == parent) {
1608 prealloc = kzalloc(sizeof(*list), GFP_NOFS);
1613 ret = add_qgroup_relation_item(trans, src, dst);
1617 ret = add_qgroup_relation_item(trans, dst, src);
1619 del_qgroup_relation_item(trans, src, dst);
1623 spin_lock(&fs_info->qgroup_lock);
1624 ret = __add_relation_rb(prealloc, member, parent);
1627 spin_unlock(&fs_info->qgroup_lock);
1630 ret = quick_update_accounting(fs_info, src, dst, 1);
1631 spin_unlock(&fs_info->qgroup_lock);
1634 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1638 static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1641 struct btrfs_fs_info *fs_info = trans->fs_info;
1642 struct btrfs_qgroup *parent;
1643 struct btrfs_qgroup *member;
1644 struct btrfs_qgroup_list *list;
1649 if (!fs_info->quota_root) {
1654 member = find_qgroup_rb(fs_info, src);
1655 parent = find_qgroup_rb(fs_info, dst);
1657 * The parent/member pair doesn't exist, then try to delete the dead
1658 * relation items only.
1660 if (!member || !parent)
1663 /* check if such qgroup relation exist firstly */
1664 list_for_each_entry(list, &member->groups, next_group) {
1665 if (list->group == parent) {
1672 ret = del_qgroup_relation_item(trans, src, dst);
1673 if (ret < 0 && ret != -ENOENT)
1675 ret2 = del_qgroup_relation_item(trans, dst, src);
1676 if (ret2 < 0 && ret2 != -ENOENT)
1679 /* At least one deletion succeeded, return 0 */
1684 spin_lock(&fs_info->qgroup_lock);
1685 del_relation_rb(fs_info, src, dst);
1686 ret = quick_update_accounting(fs_info, src, dst, -1);
1687 spin_unlock(&fs_info->qgroup_lock);
1693 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1696 struct btrfs_fs_info *fs_info = trans->fs_info;
1699 mutex_lock(&fs_info->qgroup_ioctl_lock);
1700 ret = __del_qgroup_relation(trans, src, dst);
1701 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1706 int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
1708 struct btrfs_fs_info *fs_info = trans->fs_info;
1709 struct btrfs_root *quota_root;
1710 struct btrfs_qgroup *qgroup;
1711 struct btrfs_qgroup *prealloc = NULL;
1714 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED)
1717 mutex_lock(&fs_info->qgroup_ioctl_lock);
1718 if (!fs_info->quota_root) {
1722 quota_root = fs_info->quota_root;
1723 qgroup = find_qgroup_rb(fs_info, qgroupid);
1729 prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
1735 ret = add_qgroup_item(trans, quota_root, qgroupid);
1739 spin_lock(&fs_info->qgroup_lock);
1740 qgroup = add_qgroup_rb(fs_info, prealloc, qgroupid);
1741 spin_unlock(&fs_info->qgroup_lock);
1744 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1746 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1751 static bool qgroup_has_usage(struct btrfs_qgroup *qgroup)
1753 return (qgroup->rfer > 0 || qgroup->rfer_cmpr > 0 ||
1754 qgroup->excl > 0 || qgroup->excl_cmpr > 0 ||
1755 qgroup->rsv.values[BTRFS_QGROUP_RSV_DATA] > 0 ||
1756 qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC] > 0 ||
1757 qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS] > 0);
1760 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
1762 struct btrfs_fs_info *fs_info = trans->fs_info;
1763 struct btrfs_qgroup *qgroup;
1764 struct btrfs_qgroup_list *list;
1767 mutex_lock(&fs_info->qgroup_ioctl_lock);
1768 if (!fs_info->quota_root) {
1773 qgroup = find_qgroup_rb(fs_info, qgroupid);
1779 if (is_fstree(qgroupid) && qgroup_has_usage(qgroup)) {
1784 /* Check if there are no children of this qgroup */
1785 if (!list_empty(&qgroup->members)) {
1790 ret = del_qgroup_item(trans, qgroupid);
1791 if (ret && ret != -ENOENT)
1794 while (!list_empty(&qgroup->groups)) {
1795 list = list_first_entry(&qgroup->groups,
1796 struct btrfs_qgroup_list, next_group);
1797 ret = __del_qgroup_relation(trans, qgroupid,
1798 list->group->qgroupid);
1803 spin_lock(&fs_info->qgroup_lock);
1804 del_qgroup_rb(fs_info, qgroupid);
1805 spin_unlock(&fs_info->qgroup_lock);
1808 * Remove the qgroup from sysfs now without holding the qgroup_lock
1809 * spinlock, since the sysfs_remove_group() function needs to take
1810 * the mutex kernfs_mutex through kernfs_remove_by_name_ns().
1812 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
1815 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1819 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,
1820 struct btrfs_qgroup_limit *limit)
1822 struct btrfs_fs_info *fs_info = trans->fs_info;
1823 struct btrfs_qgroup *qgroup;
1825 /* Sometimes we would want to clear the limit on this qgroup.
1826 * To meet this requirement, we treat the -1 as a special value
1827 * which tell kernel to clear the limit on this qgroup.
1829 const u64 CLEAR_VALUE = -1;
1831 mutex_lock(&fs_info->qgroup_ioctl_lock);
1832 if (!fs_info->quota_root) {
1837 qgroup = find_qgroup_rb(fs_info, qgroupid);
1843 spin_lock(&fs_info->qgroup_lock);
1844 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1845 if (limit->max_rfer == CLEAR_VALUE) {
1846 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1847 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1848 qgroup->max_rfer = 0;
1850 qgroup->max_rfer = limit->max_rfer;
1853 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1854 if (limit->max_excl == CLEAR_VALUE) {
1855 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1856 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1857 qgroup->max_excl = 0;
1859 qgroup->max_excl = limit->max_excl;
1862 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1863 if (limit->rsv_rfer == CLEAR_VALUE) {
1864 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1865 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1866 qgroup->rsv_rfer = 0;
1868 qgroup->rsv_rfer = limit->rsv_rfer;
1871 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1872 if (limit->rsv_excl == CLEAR_VALUE) {
1873 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1874 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1875 qgroup->rsv_excl = 0;
1877 qgroup->rsv_excl = limit->rsv_excl;
1880 qgroup->lim_flags |= limit->flags;
1882 spin_unlock(&fs_info->qgroup_lock);
1884 ret = update_qgroup_limit_item(trans, qgroup);
1886 qgroup_mark_inconsistent(fs_info);
1887 btrfs_info(fs_info, "unable to update quota limit for %llu",
1892 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1897 * Inform qgroup to trace one dirty extent, its info is recorded in @record.
1898 * So qgroup can account it at transaction committing time.
1900 * No lock version, caller must acquire delayed ref lock and allocated memory,
1901 * then call btrfs_qgroup_trace_extent_post() after exiting lock context.
1903 * Return 0 for success insert
1904 * Return >0 for existing record, caller can free @record safely.
1905 * Error is not possible
1907 int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
1908 struct btrfs_delayed_ref_root *delayed_refs,
1909 struct btrfs_qgroup_extent_record *record)
1911 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1912 struct rb_node *parent_node = NULL;
1913 struct btrfs_qgroup_extent_record *entry;
1914 u64 bytenr = record->bytenr;
1916 if (!btrfs_qgroup_full_accounting(fs_info))
1919 lockdep_assert_held(&delayed_refs->lock);
1920 trace_btrfs_qgroup_trace_extent(fs_info, record);
1924 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1926 if (bytenr < entry->bytenr) {
1928 } else if (bytenr > entry->bytenr) {
1929 p = &(*p)->rb_right;
1931 if (record->data_rsv && !entry->data_rsv) {
1932 entry->data_rsv = record->data_rsv;
1933 entry->data_rsv_refroot =
1934 record->data_rsv_refroot;
1940 rb_link_node(&record->node, parent_node, p);
1941 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
1946 * Post handler after qgroup_trace_extent_nolock().
1948 * NOTE: Current qgroup does the expensive backref walk at transaction
1949 * committing time with TRANS_STATE_COMMIT_DOING, this blocks incoming
1951 * This is designed to allow btrfs_find_all_roots() to get correct new_roots
1954 * However for old_roots there is no need to do backref walk at that time,
1955 * since we search commit roots to walk backref and result will always be
1958 * Due to the nature of no lock version, we can't do backref there.
1959 * So we must call btrfs_qgroup_trace_extent_post() after exiting
1962 * TODO: If we can fix and prove btrfs_find_all_roots() can get correct result
1963 * using current root, then we can move all expensive backref walk out of
1964 * transaction committing, but not now as qgroup accounting will be wrong again.
1966 int btrfs_qgroup_trace_extent_post(struct btrfs_trans_handle *trans,
1967 struct btrfs_qgroup_extent_record *qrecord)
1969 struct btrfs_backref_walk_ctx ctx = { 0 };
1972 if (!btrfs_qgroup_full_accounting(trans->fs_info))
1975 * We are always called in a context where we are already holding a
1976 * transaction handle. Often we are called when adding a data delayed
1977 * reference from btrfs_truncate_inode_items() (truncating or unlinking),
1978 * in which case we will be holding a write lock on extent buffer from a
1979 * subvolume tree. In this case we can't allow btrfs_find_all_roots() to
1980 * acquire fs_info->commit_root_sem, because that is a higher level lock
1981 * that must be acquired before locking any extent buffers.
1983 * So we want btrfs_find_all_roots() to not acquire the commit_root_sem
1984 * but we can't pass it a non-NULL transaction handle, because otherwise
1985 * it would not use commit roots and would lock extent buffers, causing
1986 * a deadlock if it ends up trying to read lock the same extent buffer
1987 * that was previously write locked at btrfs_truncate_inode_items().
1989 * So pass a NULL transaction handle to btrfs_find_all_roots() and
1990 * explicitly tell it to not acquire the commit_root_sem - if we are
1991 * holding a transaction handle we don't need its protection.
1993 ASSERT(trans != NULL);
1995 if (trans->fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING)
1998 ctx.bytenr = qrecord->bytenr;
1999 ctx.fs_info = trans->fs_info;
2001 ret = btrfs_find_all_roots(&ctx, true);
2003 qgroup_mark_inconsistent(trans->fs_info);
2004 btrfs_warn(trans->fs_info,
2005 "error accounting new delayed refs extent (err code: %d), quota inconsistent",
2011 * Here we don't need to get the lock of
2012 * trans->transaction->delayed_refs, since inserted qrecord won't
2013 * be deleted, only qrecord->node may be modified (new qrecord insert)
2015 * So modifying qrecord->old_roots is safe here
2017 qrecord->old_roots = ctx.roots;
2022 * Inform qgroup to trace one dirty extent, specified by @bytenr and
2024 * So qgroup can account it at commit trans time.
2026 * Better encapsulated version, with memory allocation and backref walk for
2028 * So this can sleep.
2030 * Return 0 if the operation is done.
2031 * Return <0 for error, like memory allocation failure or invalid parameter
2034 int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
2037 struct btrfs_fs_info *fs_info = trans->fs_info;
2038 struct btrfs_qgroup_extent_record *record;
2039 struct btrfs_delayed_ref_root *delayed_refs;
2042 if (!btrfs_qgroup_full_accounting(fs_info) || bytenr == 0 || num_bytes == 0)
2044 record = kzalloc(sizeof(*record), GFP_NOFS);
2048 delayed_refs = &trans->transaction->delayed_refs;
2049 record->bytenr = bytenr;
2050 record->num_bytes = num_bytes;
2051 record->old_roots = NULL;
2053 spin_lock(&delayed_refs->lock);
2054 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
2055 spin_unlock(&delayed_refs->lock);
2060 return btrfs_qgroup_trace_extent_post(trans, record);
2064 * Inform qgroup to trace all leaf items of data
2066 * Return 0 for success
2067 * Return <0 for error(ENOMEM)
2069 int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
2070 struct extent_buffer *eb)
2072 struct btrfs_fs_info *fs_info = trans->fs_info;
2073 int nr = btrfs_header_nritems(eb);
2074 int i, extent_type, ret;
2075 struct btrfs_key key;
2076 struct btrfs_file_extent_item *fi;
2077 u64 bytenr, num_bytes;
2079 /* We can be called directly from walk_up_proc() */
2080 if (!btrfs_qgroup_full_accounting(fs_info))
2083 for (i = 0; i < nr; i++) {
2084 btrfs_item_key_to_cpu(eb, &key, i);
2086 if (key.type != BTRFS_EXTENT_DATA_KEY)
2089 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
2090 /* filter out non qgroup-accountable extents */
2091 extent_type = btrfs_file_extent_type(eb, fi);
2093 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
2096 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
2100 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
2102 ret = btrfs_qgroup_trace_extent(trans, bytenr, num_bytes);
2111 * Walk up the tree from the bottom, freeing leaves and any interior
2112 * nodes which have had all slots visited. If a node (leaf or
2113 * interior) is freed, the node above it will have it's slot
2114 * incremented. The root node will never be freed.
2116 * At the end of this function, we should have a path which has all
2117 * slots incremented to the next position for a search. If we need to
2118 * read a new node it will be NULL and the node above it will have the
2119 * correct slot selected for a later read.
2121 * If we increment the root nodes slot counter past the number of
2122 * elements, 1 is returned to signal completion of the search.
2124 static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
2128 struct extent_buffer *eb;
2130 if (root_level == 0)
2133 while (level <= root_level) {
2134 eb = path->nodes[level];
2135 nr = btrfs_header_nritems(eb);
2136 path->slots[level]++;
2137 slot = path->slots[level];
2138 if (slot >= nr || level == 0) {
2140 * Don't free the root - we will detect this
2141 * condition after our loop and return a
2142 * positive value for caller to stop walking the tree.
2144 if (level != root_level) {
2145 btrfs_tree_unlock_rw(eb, path->locks[level]);
2146 path->locks[level] = 0;
2148 free_extent_buffer(eb);
2149 path->nodes[level] = NULL;
2150 path->slots[level] = 0;
2154 * We have a valid slot to walk back down
2155 * from. Stop here so caller can process these
2164 eb = path->nodes[root_level];
2165 if (path->slots[root_level] >= btrfs_header_nritems(eb))
2172 * Helper function to trace a subtree tree block swap.
2174 * The swap will happen in highest tree block, but there may be a lot of
2175 * tree blocks involved.
2178 * OO = Old tree blocks
2179 * NN = New tree blocks allocated during balance
2181 * File tree (257) Reloc tree for 257
2184 * L1 OO OO (a) OO NN (a)
2186 * L0 OO OO OO OO OO OO NN NN
2189 * When calling qgroup_trace_extent_swap(), we will pass:
2191 * @dst_path = [ nodes[1] = NN(a), nodes[0] = NN(c) ]
2195 * In that case, qgroup_trace_extent_swap() will search from OO(a) to
2196 * reach OO(c), then mark both OO(c) and NN(c) as qgroup dirty.
2198 * The main work of qgroup_trace_extent_swap() can be split into 3 parts:
2200 * 1) Tree search from @src_eb
2201 * It should acts as a simplified btrfs_search_slot().
2202 * The key for search can be extracted from @dst_path->nodes[dst_level]
2205 * 2) Mark the final tree blocks in @src_path and @dst_path qgroup dirty
2206 * NOTE: In above case, OO(a) and NN(a) won't be marked qgroup dirty.
2207 * They should be marked during previous (@dst_level = 1) iteration.
2209 * 3) Mark file extents in leaves dirty
2210 * We don't have good way to pick out new file extents only.
2211 * So we still follow the old method by scanning all file extents in
2214 * This function can free us from keeping two paths, thus later we only need
2215 * to care about how to iterate all new tree blocks in reloc tree.
2217 static int qgroup_trace_extent_swap(struct btrfs_trans_handle* trans,
2218 struct extent_buffer *src_eb,
2219 struct btrfs_path *dst_path,
2220 int dst_level, int root_level,
2223 struct btrfs_key key;
2224 struct btrfs_path *src_path;
2225 struct btrfs_fs_info *fs_info = trans->fs_info;
2226 u32 nodesize = fs_info->nodesize;
2227 int cur_level = root_level;
2230 BUG_ON(dst_level > root_level);
2231 /* Level mismatch */
2232 if (btrfs_header_level(src_eb) != root_level)
2235 src_path = btrfs_alloc_path();
2242 btrfs_node_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
2244 btrfs_item_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
2247 atomic_inc(&src_eb->refs);
2248 src_path->nodes[root_level] = src_eb;
2249 src_path->slots[root_level] = dst_path->slots[root_level];
2250 src_path->locks[root_level] = 0;
2252 /* A simplified version of btrfs_search_slot() */
2253 while (cur_level >= dst_level) {
2254 struct btrfs_key src_key;
2255 struct btrfs_key dst_key;
2257 if (src_path->nodes[cur_level] == NULL) {
2258 struct extent_buffer *eb;
2261 eb = src_path->nodes[cur_level + 1];
2262 parent_slot = src_path->slots[cur_level + 1];
2264 eb = btrfs_read_node_slot(eb, parent_slot);
2270 src_path->nodes[cur_level] = eb;
2272 btrfs_tree_read_lock(eb);
2273 src_path->locks[cur_level] = BTRFS_READ_LOCK;
2276 src_path->slots[cur_level] = dst_path->slots[cur_level];
2278 btrfs_node_key_to_cpu(dst_path->nodes[cur_level],
2279 &dst_key, dst_path->slots[cur_level]);
2280 btrfs_node_key_to_cpu(src_path->nodes[cur_level],
2281 &src_key, src_path->slots[cur_level]);
2283 btrfs_item_key_to_cpu(dst_path->nodes[cur_level],
2284 &dst_key, dst_path->slots[cur_level]);
2285 btrfs_item_key_to_cpu(src_path->nodes[cur_level],
2286 &src_key, src_path->slots[cur_level]);
2288 /* Content mismatch, something went wrong */
2289 if (btrfs_comp_cpu_keys(&dst_key, &src_key)) {
2297 * Now both @dst_path and @src_path have been populated, record the tree
2298 * blocks for qgroup accounting.
2300 ret = btrfs_qgroup_trace_extent(trans, src_path->nodes[dst_level]->start,
2304 ret = btrfs_qgroup_trace_extent(trans, dst_path->nodes[dst_level]->start,
2309 /* Record leaf file extents */
2310 if (dst_level == 0 && trace_leaf) {
2311 ret = btrfs_qgroup_trace_leaf_items(trans, src_path->nodes[0]);
2314 ret = btrfs_qgroup_trace_leaf_items(trans, dst_path->nodes[0]);
2317 btrfs_free_path(src_path);
2322 * Helper function to do recursive generation-aware depth-first search, to
2323 * locate all new tree blocks in a subtree of reloc tree.
2325 * E.g. (OO = Old tree blocks, NN = New tree blocks, whose gen == last_snapshot)
2334 * @dst_path = [ nodes[1] = NN(b), nodes[0] = NULL ],
2338 * We will iterate through tree blocks NN(b), NN(d) and info qgroup to trace
2339 * above tree blocks along with their counter parts in file tree.
2340 * While during search, old tree blocks OO(c) will be skipped as tree block swap
2341 * won't affect OO(c).
2343 static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans,
2344 struct extent_buffer *src_eb,
2345 struct btrfs_path *dst_path,
2346 int cur_level, int root_level,
2347 u64 last_snapshot, bool trace_leaf)
2349 struct btrfs_fs_info *fs_info = trans->fs_info;
2350 struct extent_buffer *eb;
2351 bool need_cleanup = false;
2355 /* Level sanity check */
2356 if (cur_level < 0 || cur_level >= BTRFS_MAX_LEVEL - 1 ||
2357 root_level < 0 || root_level >= BTRFS_MAX_LEVEL - 1 ||
2358 root_level < cur_level) {
2359 btrfs_err_rl(fs_info,
2360 "%s: bad levels, cur_level=%d root_level=%d",
2361 __func__, cur_level, root_level);
2365 /* Read the tree block if needed */
2366 if (dst_path->nodes[cur_level] == NULL) {
2371 * dst_path->nodes[root_level] must be initialized before
2372 * calling this function.
2374 if (cur_level == root_level) {
2375 btrfs_err_rl(fs_info,
2376 "%s: dst_path->nodes[%d] not initialized, root_level=%d cur_level=%d",
2377 __func__, root_level, root_level, cur_level);
2382 * We need to get child blockptr/gen from parent before we can
2385 eb = dst_path->nodes[cur_level + 1];
2386 parent_slot = dst_path->slots[cur_level + 1];
2387 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
2389 /* This node is old, no need to trace */
2390 if (child_gen < last_snapshot)
2393 eb = btrfs_read_node_slot(eb, parent_slot);
2399 dst_path->nodes[cur_level] = eb;
2400 dst_path->slots[cur_level] = 0;
2402 btrfs_tree_read_lock(eb);
2403 dst_path->locks[cur_level] = BTRFS_READ_LOCK;
2404 need_cleanup = true;
2407 /* Now record this tree block and its counter part for qgroups */
2408 ret = qgroup_trace_extent_swap(trans, src_eb, dst_path, cur_level,
2409 root_level, trace_leaf);
2413 eb = dst_path->nodes[cur_level];
2415 if (cur_level > 0) {
2416 /* Iterate all child tree blocks */
2417 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2418 /* Skip old tree blocks as they won't be swapped */
2419 if (btrfs_node_ptr_generation(eb, i) < last_snapshot)
2421 dst_path->slots[cur_level] = i;
2423 /* Recursive call (at most 7 times) */
2424 ret = qgroup_trace_new_subtree_blocks(trans, src_eb,
2425 dst_path, cur_level - 1, root_level,
2426 last_snapshot, trace_leaf);
2435 btrfs_tree_unlock_rw(dst_path->nodes[cur_level],
2436 dst_path->locks[cur_level]);
2437 free_extent_buffer(dst_path->nodes[cur_level]);
2438 dst_path->nodes[cur_level] = NULL;
2439 dst_path->slots[cur_level] = 0;
2440 dst_path->locks[cur_level] = 0;
2446 static int qgroup_trace_subtree_swap(struct btrfs_trans_handle *trans,
2447 struct extent_buffer *src_eb,
2448 struct extent_buffer *dst_eb,
2449 u64 last_snapshot, bool trace_leaf)
2451 struct btrfs_fs_info *fs_info = trans->fs_info;
2452 struct btrfs_path *dst_path = NULL;
2456 if (!btrfs_qgroup_full_accounting(fs_info))
2459 /* Wrong parameter order */
2460 if (btrfs_header_generation(src_eb) > btrfs_header_generation(dst_eb)) {
2461 btrfs_err_rl(fs_info,
2462 "%s: bad parameter order, src_gen=%llu dst_gen=%llu", __func__,
2463 btrfs_header_generation(src_eb),
2464 btrfs_header_generation(dst_eb));
2468 if (!extent_buffer_uptodate(src_eb) || !extent_buffer_uptodate(dst_eb)) {
2473 level = btrfs_header_level(dst_eb);
2474 dst_path = btrfs_alloc_path();
2480 atomic_inc(&dst_eb->refs);
2481 dst_path->nodes[level] = dst_eb;
2482 dst_path->slots[level] = 0;
2483 dst_path->locks[level] = 0;
2485 /* Do the generation aware breadth-first search */
2486 ret = qgroup_trace_new_subtree_blocks(trans, src_eb, dst_path, level,
2487 level, last_snapshot, trace_leaf);
2493 btrfs_free_path(dst_path);
2495 qgroup_mark_inconsistent(fs_info);
2500 * Inform qgroup to trace a whole subtree, including all its child tree
2502 * The root tree block is specified by @root_eb.
2504 * Normally used by relocation(tree block swap) and subvolume deletion.
2506 * Return 0 for success
2507 * Return <0 for error(ENOMEM or tree search error)
2509 int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
2510 struct extent_buffer *root_eb,
2511 u64 root_gen, int root_level)
2513 struct btrfs_fs_info *fs_info = trans->fs_info;
2516 u8 drop_subptree_thres;
2517 struct extent_buffer *eb = root_eb;
2518 struct btrfs_path *path = NULL;
2520 ASSERT(0 <= root_level && root_level < BTRFS_MAX_LEVEL);
2521 ASSERT(root_eb != NULL);
2523 if (!btrfs_qgroup_full_accounting(fs_info))
2526 spin_lock(&fs_info->qgroup_lock);
2527 drop_subptree_thres = fs_info->qgroup_drop_subtree_thres;
2528 spin_unlock(&fs_info->qgroup_lock);
2531 * This function only gets called for snapshot drop, if we hit a high
2532 * node here, it means we are going to change ownership for quite a lot
2533 * of extents, which will greatly slow down btrfs_commit_transaction().
2535 * So here if we find a high tree here, we just skip the accounting and
2536 * mark qgroup inconsistent.
2538 if (root_level >= drop_subptree_thres) {
2539 qgroup_mark_inconsistent(fs_info);
2543 if (!extent_buffer_uptodate(root_eb)) {
2544 struct btrfs_tree_parent_check check = {
2545 .has_first_key = false,
2546 .transid = root_gen,
2550 ret = btrfs_read_extent_buffer(root_eb, &check);
2555 if (root_level == 0) {
2556 ret = btrfs_qgroup_trace_leaf_items(trans, root_eb);
2560 path = btrfs_alloc_path();
2565 * Walk down the tree. Missing extent blocks are filled in as
2566 * we go. Metadata is accounted every time we read a new
2569 * When we reach a leaf, we account for file extent items in it,
2570 * walk back up the tree (adjusting slot pointers as we go)
2571 * and restart the search process.
2573 atomic_inc(&root_eb->refs); /* For path */
2574 path->nodes[root_level] = root_eb;
2575 path->slots[root_level] = 0;
2576 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
2579 while (level >= 0) {
2580 if (path->nodes[level] == NULL) {
2585 * We need to get child blockptr from parent before we
2588 eb = path->nodes[level + 1];
2589 parent_slot = path->slots[level + 1];
2590 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
2592 eb = btrfs_read_node_slot(eb, parent_slot);
2598 path->nodes[level] = eb;
2599 path->slots[level] = 0;
2601 btrfs_tree_read_lock(eb);
2602 path->locks[level] = BTRFS_READ_LOCK;
2604 ret = btrfs_qgroup_trace_extent(trans, child_bytenr,
2611 ret = btrfs_qgroup_trace_leaf_items(trans,
2612 path->nodes[level]);
2616 /* Nonzero return here means we completed our search */
2617 ret = adjust_slots_upwards(path, root_level);
2621 /* Restart search with new slots */
2630 btrfs_free_path(path);
2635 static void qgroup_iterator_nested_add(struct list_head *head, struct btrfs_qgroup *qgroup)
2637 if (!list_empty(&qgroup->nested_iterator))
2640 list_add_tail(&qgroup->nested_iterator, head);
2643 static void qgroup_iterator_nested_clean(struct list_head *head)
2645 while (!list_empty(head)) {
2646 struct btrfs_qgroup *qgroup;
2648 qgroup = list_first_entry(head, struct btrfs_qgroup, nested_iterator);
2649 list_del_init(&qgroup->nested_iterator);
2653 #define UPDATE_NEW 0
2654 #define UPDATE_OLD 1
2656 * Walk all of the roots that points to the bytenr and adjust their refcnts.
2658 static void qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
2659 struct ulist *roots, struct list_head *qgroups,
2660 u64 seq, int update_old)
2662 struct ulist_node *unode;
2663 struct ulist_iterator uiter;
2664 struct btrfs_qgroup *qg;
2668 ULIST_ITER_INIT(&uiter);
2669 while ((unode = ulist_next(roots, &uiter))) {
2672 qg = find_qgroup_rb(fs_info, unode->val);
2676 qgroup_iterator_nested_add(qgroups, qg);
2677 qgroup_iterator_add(&tmp, qg);
2678 list_for_each_entry(qg, &tmp, iterator) {
2679 struct btrfs_qgroup_list *glist;
2682 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
2684 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
2686 list_for_each_entry(glist, &qg->groups, next_group) {
2687 qgroup_iterator_nested_add(qgroups, glist->group);
2688 qgroup_iterator_add(&tmp, glist->group);
2691 qgroup_iterator_clean(&tmp);
2696 * Update qgroup rfer/excl counters.
2697 * Rfer update is easy, codes can explain themselves.
2699 * Excl update is tricky, the update is split into 2 parts.
2700 * Part 1: Possible exclusive <-> sharing detect:
2702 * -------------------------------------
2704 * -------------------------------------
2706 * -------------------------------------
2709 * A: cur_old_roots < nr_old_roots (not exclusive before)
2710 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
2711 * B: cur_new_roots < nr_new_roots (not exclusive now)
2712 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
2715 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
2716 * *: Definitely not changed. **: Possible unchanged.
2718 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
2720 * To make the logic clear, we first use condition A and B to split
2721 * combination into 4 results.
2723 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
2724 * only on variant maybe 0.
2726 * Lastly, check result **, since there are 2 variants maybe 0, split them
2728 * But this time we don't need to consider other things, the codes and logic
2729 * is easy to understand now.
2731 static void qgroup_update_counters(struct btrfs_fs_info *fs_info,
2732 struct list_head *qgroups, u64 nr_old_roots,
2733 u64 nr_new_roots, u64 num_bytes, u64 seq)
2735 struct btrfs_qgroup *qg;
2737 list_for_each_entry(qg, qgroups, nested_iterator) {
2738 u64 cur_new_count, cur_old_count;
2741 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
2742 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
2744 trace_qgroup_update_counters(fs_info, qg, cur_old_count,
2747 /* Rfer update part */
2748 if (cur_old_count == 0 && cur_new_count > 0) {
2749 qg->rfer += num_bytes;
2750 qg->rfer_cmpr += num_bytes;
2753 if (cur_old_count > 0 && cur_new_count == 0) {
2754 qg->rfer -= num_bytes;
2755 qg->rfer_cmpr -= num_bytes;
2759 /* Excl update part */
2760 /* Exclusive/none -> shared case */
2761 if (cur_old_count == nr_old_roots &&
2762 cur_new_count < nr_new_roots) {
2763 /* Exclusive -> shared */
2764 if (cur_old_count != 0) {
2765 qg->excl -= num_bytes;
2766 qg->excl_cmpr -= num_bytes;
2771 /* Shared -> exclusive/none case */
2772 if (cur_old_count < nr_old_roots &&
2773 cur_new_count == nr_new_roots) {
2774 /* Shared->exclusive */
2775 if (cur_new_count != 0) {
2776 qg->excl += num_bytes;
2777 qg->excl_cmpr += num_bytes;
2782 /* Exclusive/none -> exclusive/none case */
2783 if (cur_old_count == nr_old_roots &&
2784 cur_new_count == nr_new_roots) {
2785 if (cur_old_count == 0) {
2786 /* None -> exclusive/none */
2788 if (cur_new_count != 0) {
2789 /* None -> exclusive */
2790 qg->excl += num_bytes;
2791 qg->excl_cmpr += num_bytes;
2794 /* None -> none, nothing changed */
2796 /* Exclusive -> exclusive/none */
2798 if (cur_new_count == 0) {
2799 /* Exclusive -> none */
2800 qg->excl -= num_bytes;
2801 qg->excl_cmpr -= num_bytes;
2804 /* Exclusive -> exclusive, nothing changed */
2809 qgroup_dirty(fs_info, qg);
2814 * Check if the @roots potentially is a list of fs tree roots
2816 * Return 0 for definitely not a fs/subvol tree roots ulist
2817 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
2820 static int maybe_fs_roots(struct ulist *roots)
2822 struct ulist_node *unode;
2823 struct ulist_iterator uiter;
2825 /* Empty one, still possible for fs roots */
2826 if (!roots || roots->nnodes == 0)
2829 ULIST_ITER_INIT(&uiter);
2830 unode = ulist_next(roots, &uiter);
2835 * If it contains fs tree roots, then it must belong to fs/subvol
2837 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
2839 return is_fstree(unode->val);
2842 int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr,
2843 u64 num_bytes, struct ulist *old_roots,
2844 struct ulist *new_roots)
2846 struct btrfs_fs_info *fs_info = trans->fs_info;
2849 u64 nr_new_roots = 0;
2850 u64 nr_old_roots = 0;
2854 * If quotas get disabled meanwhile, the resources need to be freed and
2855 * we can't just exit here.
2857 if (!btrfs_qgroup_full_accounting(fs_info) ||
2858 fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING)
2862 if (!maybe_fs_roots(new_roots))
2864 nr_new_roots = new_roots->nnodes;
2867 if (!maybe_fs_roots(old_roots))
2869 nr_old_roots = old_roots->nnodes;
2872 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
2873 if (nr_old_roots == 0 && nr_new_roots == 0)
2876 trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
2877 num_bytes, nr_old_roots, nr_new_roots);
2879 mutex_lock(&fs_info->qgroup_rescan_lock);
2880 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2881 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
2882 mutex_unlock(&fs_info->qgroup_rescan_lock);
2887 mutex_unlock(&fs_info->qgroup_rescan_lock);
2889 spin_lock(&fs_info->qgroup_lock);
2890 seq = fs_info->qgroup_seq;
2892 /* Update old refcnts using old_roots */
2893 qgroup_update_refcnt(fs_info, old_roots, &qgroups, seq, UPDATE_OLD);
2895 /* Update new refcnts using new_roots */
2896 qgroup_update_refcnt(fs_info, new_roots, &qgroups, seq, UPDATE_NEW);
2898 qgroup_update_counters(fs_info, &qgroups, nr_old_roots, nr_new_roots,
2902 * We're done using the iterator, release all its qgroups while holding
2903 * fs_info->qgroup_lock so that we don't race with btrfs_remove_qgroup()
2904 * and trigger use-after-free accesses to qgroups.
2906 qgroup_iterator_nested_clean(&qgroups);
2909 * Bump qgroup_seq to avoid seq overlap
2911 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2912 spin_unlock(&fs_info->qgroup_lock);
2914 ulist_free(old_roots);
2915 ulist_free(new_roots);
2919 int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
2921 struct btrfs_fs_info *fs_info = trans->fs_info;
2922 struct btrfs_qgroup_extent_record *record;
2923 struct btrfs_delayed_ref_root *delayed_refs;
2924 struct ulist *new_roots = NULL;
2925 struct rb_node *node;
2926 u64 num_dirty_extents = 0;
2930 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
2933 delayed_refs = &trans->transaction->delayed_refs;
2934 qgroup_to_skip = delayed_refs->qgroup_to_skip;
2935 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2936 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2939 num_dirty_extents++;
2940 trace_btrfs_qgroup_account_extents(fs_info, record);
2942 if (!ret && !(fs_info->qgroup_flags &
2943 BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING)) {
2944 struct btrfs_backref_walk_ctx ctx = { 0 };
2946 ctx.bytenr = record->bytenr;
2947 ctx.fs_info = fs_info;
2950 * Old roots should be searched when inserting qgroup
2953 * But for INCONSISTENT (NO_ACCOUNTING) -> rescan case,
2954 * we may have some record inserted during
2955 * NO_ACCOUNTING (thus no old_roots populated), but
2956 * later we start rescan, which clears NO_ACCOUNTING,
2957 * leaving some inserted records without old_roots
2960 * Those cases are rare and should not cause too much
2961 * time spent during commit_transaction().
2963 if (!record->old_roots) {
2964 /* Search commit root to find old_roots */
2965 ret = btrfs_find_all_roots(&ctx, false);
2968 record->old_roots = ctx.roots;
2973 * Use BTRFS_SEQ_LAST as time_seq to do special search,
2974 * which doesn't lock tree or delayed_refs and search
2975 * current root. It's safe inside commit_transaction().
2978 ctx.time_seq = BTRFS_SEQ_LAST;
2979 ret = btrfs_find_all_roots(&ctx, false);
2982 new_roots = ctx.roots;
2983 if (qgroup_to_skip) {
2984 ulist_del(new_roots, qgroup_to_skip, 0);
2985 ulist_del(record->old_roots, qgroup_to_skip,
2988 ret = btrfs_qgroup_account_extent(trans, record->bytenr,
2992 record->old_roots = NULL;
2995 /* Free the reserved data space */
2996 btrfs_qgroup_free_refroot(fs_info,
2997 record->data_rsv_refroot,
2999 BTRFS_QGROUP_RSV_DATA);
3001 ulist_free(record->old_roots);
3002 ulist_free(new_roots);
3004 rb_erase(node, &delayed_refs->dirty_extent_root);
3008 trace_qgroup_num_dirty_extents(fs_info, trans->transid,
3014 * Writes all changed qgroups to disk.
3015 * Called by the transaction commit path and the qgroup assign ioctl.
3017 int btrfs_run_qgroups(struct btrfs_trans_handle *trans)
3019 struct btrfs_fs_info *fs_info = trans->fs_info;
3023 * In case we are called from the qgroup assign ioctl, assert that we
3024 * are holding the qgroup_ioctl_lock, otherwise we can race with a quota
3025 * disable operation (ioctl) and access a freed quota root.
3027 if (trans->transaction->state != TRANS_STATE_COMMIT_DOING)
3028 lockdep_assert_held(&fs_info->qgroup_ioctl_lock);
3030 if (!fs_info->quota_root)
3033 spin_lock(&fs_info->qgroup_lock);
3034 while (!list_empty(&fs_info->dirty_qgroups)) {
3035 struct btrfs_qgroup *qgroup;
3036 qgroup = list_first_entry(&fs_info->dirty_qgroups,
3037 struct btrfs_qgroup, dirty);
3038 list_del_init(&qgroup->dirty);
3039 spin_unlock(&fs_info->qgroup_lock);
3040 ret = update_qgroup_info_item(trans, qgroup);
3042 qgroup_mark_inconsistent(fs_info);
3043 ret = update_qgroup_limit_item(trans, qgroup);
3045 qgroup_mark_inconsistent(fs_info);
3046 spin_lock(&fs_info->qgroup_lock);
3048 if (btrfs_qgroup_enabled(fs_info))
3049 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
3051 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
3052 spin_unlock(&fs_info->qgroup_lock);
3054 ret = update_qgroup_status_item(trans);
3056 qgroup_mark_inconsistent(fs_info);
3061 int btrfs_qgroup_check_inherit(struct btrfs_fs_info *fs_info,
3062 struct btrfs_qgroup_inherit *inherit,
3065 if (inherit->flags & ~BTRFS_QGROUP_INHERIT_FLAGS_SUPP)
3067 if (size < sizeof(*inherit) || size > PAGE_SIZE)
3071 * In the past we allowed btrfs_qgroup_inherit to specify to copy
3072 * rfer/excl numbers directly from other qgroups. This behavior has
3073 * been disabled in userspace for a very long time, but here we should
3074 * also disable it in kernel, as this behavior is known to mark qgroup
3075 * inconsistent, and a rescan would wipe out the changes anyway.
3077 * Reject any btrfs_qgroup_inherit with num_ref_copies or num_excl_copies.
3079 if (inherit->num_ref_copies > 0 || inherit->num_excl_copies > 0)
3082 if (size != struct_size(inherit, qgroups, inherit->num_qgroups))
3086 * Skip the inherit source qgroups check if qgroup is not enabled.
3087 * Qgroup can still be later enabled causing problems, but in that case
3088 * btrfs_qgroup_inherit() would just ignore those invalid ones.
3090 if (!btrfs_qgroup_enabled(fs_info))
3094 * Now check all the remaining qgroups, they should all:
3097 * - Be higher level qgroups.
3099 for (int i = 0; i < inherit->num_qgroups; i++) {
3100 struct btrfs_qgroup *qgroup;
3101 u64 qgroupid = inherit->qgroups[i];
3103 if (btrfs_qgroup_level(qgroupid) == 0)
3106 spin_lock(&fs_info->qgroup_lock);
3107 qgroup = find_qgroup_rb(fs_info, qgroupid);
3109 spin_unlock(&fs_info->qgroup_lock);
3112 spin_unlock(&fs_info->qgroup_lock);
3117 static int qgroup_auto_inherit(struct btrfs_fs_info *fs_info,
3119 struct btrfs_qgroup_inherit **inherit)
3122 u64 num_qgroups = 0;
3123 struct btrfs_qgroup *inode_qg;
3124 struct btrfs_qgroup_list *qg_list;
3125 struct btrfs_qgroup_inherit *res;
3132 inode_qg = find_qgroup_rb(fs_info, inode_rootid);
3136 num_qgroups = list_count_nodes(&inode_qg->groups);
3141 struct_sz = struct_size(res, qgroups, num_qgroups);
3142 if (struct_sz == SIZE_MAX)
3145 res = kzalloc(struct_sz, GFP_NOFS);
3148 res->num_qgroups = num_qgroups;
3149 qgids = res->qgroups;
3151 list_for_each_entry(qg_list, &inode_qg->groups, next_group)
3152 qgids[i++] = qg_list->group->qgroupid;
3159 * Check if we can skip rescan when inheriting qgroups. If @src has a single
3160 * @parent, and that @parent is owning all its bytes exclusively, we can skip
3161 * the full rescan, by just adding nodesize to the @parent's excl/rfer.
3163 * Return <0 for fatal errors (like srcid/parentid has no qgroup).
3164 * Return 0 if a quick inherit is done.
3165 * Return >0 if a quick inherit is not possible, and a full rescan is needed.
3167 static int qgroup_snapshot_quick_inherit(struct btrfs_fs_info *fs_info,
3168 u64 srcid, u64 parentid)
3170 struct btrfs_qgroup *src;
3171 struct btrfs_qgroup *parent;
3172 struct btrfs_qgroup_list *list;
3175 src = find_qgroup_rb(fs_info, srcid);
3178 parent = find_qgroup_rb(fs_info, parentid);
3183 * Source has no parent qgroup, but our new qgroup would have one.
3184 * Qgroup numbers would become inconsistent.
3186 if (list_empty(&src->groups))
3189 list_for_each_entry(list, &src->groups, next_group) {
3190 /* The parent is not the same, quick update is not possible. */
3191 if (list->group->qgroupid != parentid)
3195 * More than one parent qgroup, we can't be sure about accounting
3203 * The parent is not exclusively owning all its bytes. We're not sure
3204 * if the source has any bytes not fully owned by the parent.
3206 if (parent->excl != parent->rfer)
3209 parent->excl += fs_info->nodesize;
3210 parent->rfer += fs_info->nodesize;
3215 * Copy the accounting information between qgroups. This is necessary
3216 * when a snapshot or a subvolume is created. Throwing an error will
3217 * cause a transaction abort so we take extra care here to only error
3218 * when a readonly fs is a reasonable outcome.
3220 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
3221 u64 objectid, u64 inode_rootid,
3222 struct btrfs_qgroup_inherit *inherit)
3227 bool committing = false;
3228 struct btrfs_fs_info *fs_info = trans->fs_info;
3229 struct btrfs_root *quota_root;
3230 struct btrfs_qgroup *srcgroup;
3231 struct btrfs_qgroup *dstgroup;
3232 struct btrfs_qgroup *prealloc;
3233 struct btrfs_qgroup_list **qlist_prealloc = NULL;
3234 bool free_inherit = false;
3235 bool need_rescan = false;
3239 prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
3244 * There are only two callers of this function.
3246 * One in create_subvol() in the ioctl context, which needs to hold
3247 * the qgroup_ioctl_lock.
3249 * The other one in create_pending_snapshot() where no other qgroup
3250 * code can modify the fs as they all need to either start a new trans
3251 * or hold a trans handler, thus we don't need to hold
3252 * qgroup_ioctl_lock.
3253 * This would avoid long and complex lock chain and make lockdep happy.
3255 spin_lock(&fs_info->trans_lock);
3256 if (trans->transaction->state == TRANS_STATE_COMMIT_DOING)
3258 spin_unlock(&fs_info->trans_lock);
3261 mutex_lock(&fs_info->qgroup_ioctl_lock);
3262 if (!btrfs_qgroup_enabled(fs_info))
3265 quota_root = fs_info->quota_root;
3271 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE && !inherit) {
3272 ret = qgroup_auto_inherit(fs_info, inode_rootid, &inherit);
3275 free_inherit = true;
3279 i_qgroups = (u64 *)(inherit + 1);
3280 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
3281 2 * inherit->num_excl_copies;
3282 for (i = 0; i < nums; ++i) {
3283 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
3286 * Zero out invalid groups so we can ignore
3290 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
3298 * create a tracking group for the subvol itself
3300 ret = add_qgroup_item(trans, quota_root, objectid);
3305 * add qgroup to all inherited groups
3308 i_qgroups = (u64 *)(inherit + 1);
3309 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
3310 if (*i_qgroups == 0)
3312 ret = add_qgroup_relation_item(trans, objectid,
3314 if (ret && ret != -EEXIST)
3316 ret = add_qgroup_relation_item(trans, *i_qgroups,
3318 if (ret && ret != -EEXIST)
3323 qlist_prealloc = kcalloc(inherit->num_qgroups,
3324 sizeof(struct btrfs_qgroup_list *),
3326 if (!qlist_prealloc) {
3330 for (int i = 0; i < inherit->num_qgroups; i++) {
3331 qlist_prealloc[i] = kzalloc(sizeof(struct btrfs_qgroup_list),
3333 if (!qlist_prealloc[i]) {
3340 spin_lock(&fs_info->qgroup_lock);
3342 dstgroup = add_qgroup_rb(fs_info, prealloc, objectid);
3345 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
3346 dstgroup->lim_flags = inherit->lim.flags;
3347 dstgroup->max_rfer = inherit->lim.max_rfer;
3348 dstgroup->max_excl = inherit->lim.max_excl;
3349 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
3350 dstgroup->rsv_excl = inherit->lim.rsv_excl;
3352 qgroup_dirty(fs_info, dstgroup);
3355 if (srcid && btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL) {
3356 srcgroup = find_qgroup_rb(fs_info, srcid);
3361 * We call inherit after we clone the root in order to make sure
3362 * our counts don't go crazy, so at this point the only
3363 * difference between the two roots should be the root node.
3365 level_size = fs_info->nodesize;
3366 dstgroup->rfer = srcgroup->rfer;
3367 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
3368 dstgroup->excl = level_size;
3369 dstgroup->excl_cmpr = level_size;
3370 srcgroup->excl = level_size;
3371 srcgroup->excl_cmpr = level_size;
3373 /* inherit the limit info */
3374 dstgroup->lim_flags = srcgroup->lim_flags;
3375 dstgroup->max_rfer = srcgroup->max_rfer;
3376 dstgroup->max_excl = srcgroup->max_excl;
3377 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
3378 dstgroup->rsv_excl = srcgroup->rsv_excl;
3380 qgroup_dirty(fs_info, dstgroup);
3381 qgroup_dirty(fs_info, srcgroup);
3384 * If the source qgroup has parent but the new one doesn't,
3385 * we need a full rescan.
3387 if (!inherit && !list_empty(&srcgroup->groups))
3394 i_qgroups = (u64 *)(inherit + 1);
3395 for (i = 0; i < inherit->num_qgroups; ++i) {
3397 ret = add_relation_rb(fs_info, qlist_prealloc[i], objectid,
3399 qlist_prealloc[i] = NULL;
3404 /* Check if we can do a quick inherit. */
3405 ret = qgroup_snapshot_quick_inherit(fs_info, srcid, *i_qgroups);
3415 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
3416 struct btrfs_qgroup *src;
3417 struct btrfs_qgroup *dst;
3419 if (!i_qgroups[0] || !i_qgroups[1])
3422 src = find_qgroup_rb(fs_info, i_qgroups[0]);
3423 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
3430 dst->rfer = src->rfer - level_size;
3431 dst->rfer_cmpr = src->rfer_cmpr - level_size;
3433 /* Manually tweaking numbers certainly needs a rescan */
3436 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
3437 struct btrfs_qgroup *src;
3438 struct btrfs_qgroup *dst;
3440 if (!i_qgroups[0] || !i_qgroups[1])
3443 src = find_qgroup_rb(fs_info, i_qgroups[0]);
3444 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
3451 dst->excl = src->excl + level_size;
3452 dst->excl_cmpr = src->excl_cmpr + level_size;
3457 spin_unlock(&fs_info->qgroup_lock);
3459 ret = btrfs_sysfs_add_one_qgroup(fs_info, dstgroup);
3462 mutex_unlock(&fs_info->qgroup_ioctl_lock);
3464 qgroup_mark_inconsistent(fs_info);
3465 if (qlist_prealloc) {
3466 for (int i = 0; i < inherit->num_qgroups; i++)
3467 kfree(qlist_prealloc[i]);
3468 kfree(qlist_prealloc);
3476 static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
3478 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
3479 qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
3482 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
3483 qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
3489 static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
3490 enum btrfs_qgroup_rsv_type type)
3492 struct btrfs_qgroup *qgroup;
3493 struct btrfs_fs_info *fs_info = root->fs_info;
3494 u64 ref_root = btrfs_root_id(root);
3496 LIST_HEAD(qgroup_list);
3498 if (!is_fstree(ref_root))
3504 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
3505 capable(CAP_SYS_RESOURCE))
3508 spin_lock(&fs_info->qgroup_lock);
3509 if (!fs_info->quota_root)
3512 qgroup = find_qgroup_rb(fs_info, ref_root);
3516 qgroup_iterator_add(&qgroup_list, qgroup);
3517 list_for_each_entry(qgroup, &qgroup_list, iterator) {
3518 struct btrfs_qgroup_list *glist;
3520 if (enforce && !qgroup_check_limits(qgroup, num_bytes)) {
3525 list_for_each_entry(glist, &qgroup->groups, next_group)
3526 qgroup_iterator_add(&qgroup_list, glist->group);
3531 * no limits exceeded, now record the reservation into all qgroups
3533 list_for_each_entry(qgroup, &qgroup_list, iterator)
3534 qgroup_rsv_add(fs_info, qgroup, num_bytes, type);
3537 qgroup_iterator_clean(&qgroup_list);
3538 spin_unlock(&fs_info->qgroup_lock);
3543 * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0
3546 * Will handle all higher level qgroup too.
3548 * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
3549 * This special case is only used for META_PERTRANS type.
3551 void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
3552 u64 ref_root, u64 num_bytes,
3553 enum btrfs_qgroup_rsv_type type)
3555 struct btrfs_qgroup *qgroup;
3556 LIST_HEAD(qgroup_list);
3558 if (!is_fstree(ref_root))
3564 if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
3565 WARN(1, "%s: Invalid type to free", __func__);
3568 spin_lock(&fs_info->qgroup_lock);
3570 if (!fs_info->quota_root)
3573 qgroup = find_qgroup_rb(fs_info, ref_root);
3577 if (num_bytes == (u64)-1)
3579 * We're freeing all pertrans rsv, get reserved value from
3580 * level 0 qgroup as real num_bytes to free.
3582 num_bytes = qgroup->rsv.values[type];
3584 qgroup_iterator_add(&qgroup_list, qgroup);
3585 list_for_each_entry(qgroup, &qgroup_list, iterator) {
3586 struct btrfs_qgroup_list *glist;
3588 qgroup_rsv_release(fs_info, qgroup, num_bytes, type);
3589 list_for_each_entry(glist, &qgroup->groups, next_group) {
3590 qgroup_iterator_add(&qgroup_list, glist->group);
3594 qgroup_iterator_clean(&qgroup_list);
3595 spin_unlock(&fs_info->qgroup_lock);
3599 * Check if the leaf is the last leaf. Which means all node pointers
3600 * are at their last position.
3602 static bool is_last_leaf(struct btrfs_path *path)
3606 for (i = 1; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
3607 if (path->slots[i] != btrfs_header_nritems(path->nodes[i]) - 1)
3614 * returns < 0 on error, 0 when more leafs are to be scanned.
3615 * returns 1 when done.
3617 static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans,
3618 struct btrfs_path *path)
3620 struct btrfs_fs_info *fs_info = trans->fs_info;
3621 struct btrfs_root *extent_root;
3622 struct btrfs_key found;
3623 struct extent_buffer *scratch_leaf = NULL;
3629 if (!btrfs_qgroup_full_accounting(fs_info))
3632 mutex_lock(&fs_info->qgroup_rescan_lock);
3633 extent_root = btrfs_extent_root(fs_info,
3634 fs_info->qgroup_rescan_progress.objectid);
3635 ret = btrfs_search_slot_for_read(extent_root,
3636 &fs_info->qgroup_rescan_progress,
3639 btrfs_debug(fs_info,
3640 "current progress key (%llu %u %llu), search_slot ret %d",
3641 fs_info->qgroup_rescan_progress.objectid,
3642 fs_info->qgroup_rescan_progress.type,
3643 fs_info->qgroup_rescan_progress.offset, ret);
3647 * The rescan is about to end, we will not be scanning any
3648 * further blocks. We cannot unset the RESCAN flag here, because
3649 * we want to commit the transaction if everything went well.
3650 * To make the live accounting work in this phase, we set our
3651 * scan progress pointer such that every real extent objectid
3654 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
3655 btrfs_release_path(path);
3656 mutex_unlock(&fs_info->qgroup_rescan_lock);
3659 done = is_last_leaf(path);
3661 btrfs_item_key_to_cpu(path->nodes[0], &found,
3662 btrfs_header_nritems(path->nodes[0]) - 1);
3663 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
3665 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
3666 if (!scratch_leaf) {
3668 mutex_unlock(&fs_info->qgroup_rescan_lock);
3671 slot = path->slots[0];
3672 btrfs_release_path(path);
3673 mutex_unlock(&fs_info->qgroup_rescan_lock);
3675 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
3676 struct btrfs_backref_walk_ctx ctx = { 0 };
3678 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
3679 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
3680 found.type != BTRFS_METADATA_ITEM_KEY)
3682 if (found.type == BTRFS_METADATA_ITEM_KEY)
3683 num_bytes = fs_info->nodesize;
3685 num_bytes = found.offset;
3687 ctx.bytenr = found.objectid;
3688 ctx.fs_info = fs_info;
3690 ret = btrfs_find_all_roots(&ctx, false);
3693 /* For rescan, just pass old_roots as NULL */
3694 ret = btrfs_qgroup_account_extent(trans, found.objectid,
3695 num_bytes, NULL, ctx.roots);
3701 free_extent_buffer(scratch_leaf);
3705 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
3710 static bool rescan_should_stop(struct btrfs_fs_info *fs_info)
3712 if (btrfs_fs_closing(fs_info))
3714 if (test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state))
3716 if (!btrfs_qgroup_enabled(fs_info))
3718 if (fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN)
3723 static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
3725 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
3726 qgroup_rescan_work);
3727 struct btrfs_path *path;
3728 struct btrfs_trans_handle *trans = NULL;
3730 bool stopped = false;
3731 bool did_leaf_rescans = false;
3733 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
3736 path = btrfs_alloc_path();
3742 * Rescan should only search for commit root, and any later difference
3743 * should be recorded by qgroup
3745 path->search_commit_root = 1;
3746 path->skip_locking = 1;
3748 while (!ret && !(stopped = rescan_should_stop(fs_info))) {
3749 trans = btrfs_start_transaction(fs_info->fs_root, 0);
3750 if (IS_ERR(trans)) {
3751 ret = PTR_ERR(trans);
3755 ret = qgroup_rescan_leaf(trans, path);
3756 did_leaf_rescans = true;
3759 btrfs_commit_transaction(trans);
3761 btrfs_end_transaction(trans);
3765 btrfs_free_path(path);
3767 mutex_lock(&fs_info->qgroup_rescan_lock);
3769 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
3770 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3771 } else if (ret < 0 || stopped) {
3772 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3774 mutex_unlock(&fs_info->qgroup_rescan_lock);
3777 * Only update status, since the previous part has already updated the
3778 * qgroup info, and only if we did any actual work. This also prevents
3779 * race with a concurrent quota disable, which has already set
3780 * fs_info->quota_root to NULL and cleared BTRFS_FS_QUOTA_ENABLED at
3781 * btrfs_quota_disable().
3783 if (did_leaf_rescans) {
3784 trans = btrfs_start_transaction(fs_info->quota_root, 1);
3785 if (IS_ERR(trans)) {
3786 ret = PTR_ERR(trans);
3789 "fail to start transaction for status update: %d",
3796 mutex_lock(&fs_info->qgroup_rescan_lock);
3798 fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN)
3799 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3801 int ret2 = update_qgroup_status_item(trans);
3805 btrfs_err(fs_info, "fail to update qgroup status: %d", ret);
3808 fs_info->qgroup_rescan_running = false;
3809 fs_info->qgroup_flags &= ~BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN;
3810 complete_all(&fs_info->qgroup_rescan_completion);
3811 mutex_unlock(&fs_info->qgroup_rescan_lock);
3816 btrfs_end_transaction(trans);
3819 btrfs_info(fs_info, "qgroup scan paused");
3820 } else if (fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN) {
3821 btrfs_info(fs_info, "qgroup scan cancelled");
3822 } else if (ret >= 0) {
3823 btrfs_info(fs_info, "qgroup scan completed%s",
3824 ret > 0 ? " (inconsistency flag cleared)" : "");
3826 btrfs_err(fs_info, "qgroup scan failed with %d", ret);
3831 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
3832 * memory required for the rescan context.
3835 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
3840 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE) {
3841 btrfs_warn(fs_info, "qgroup rescan init failed, running in simple mode");
3846 /* we're resuming qgroup rescan at mount time */
3847 if (!(fs_info->qgroup_flags &
3848 BTRFS_QGROUP_STATUS_FLAG_RESCAN)) {
3849 btrfs_debug(fs_info,
3850 "qgroup rescan init failed, qgroup rescan is not queued");
3852 } else if (!(fs_info->qgroup_flags &
3853 BTRFS_QGROUP_STATUS_FLAG_ON)) {
3854 btrfs_debug(fs_info,
3855 "qgroup rescan init failed, qgroup is not enabled");
3863 mutex_lock(&fs_info->qgroup_rescan_lock);
3866 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3868 } else if (!(fs_info->qgroup_flags &
3869 BTRFS_QGROUP_STATUS_FLAG_ON)) {
3870 btrfs_debug(fs_info,
3871 "qgroup rescan init failed, qgroup is not enabled");
3873 } else if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED) {
3874 /* Quota disable is in progress */
3879 mutex_unlock(&fs_info->qgroup_rescan_lock);
3882 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3885 memset(&fs_info->qgroup_rescan_progress, 0,
3886 sizeof(fs_info->qgroup_rescan_progress));
3887 fs_info->qgroup_flags &= ~(BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN |
3888 BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING);
3889 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
3890 init_completion(&fs_info->qgroup_rescan_completion);
3891 mutex_unlock(&fs_info->qgroup_rescan_lock);
3893 btrfs_init_work(&fs_info->qgroup_rescan_work,
3894 btrfs_qgroup_rescan_worker, NULL);
3899 qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
3902 struct btrfs_qgroup *qgroup;
3904 spin_lock(&fs_info->qgroup_lock);
3905 /* clear all current qgroup tracking information */
3906 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
3907 qgroup = rb_entry(n, struct btrfs_qgroup, node);
3909 qgroup->rfer_cmpr = 0;
3911 qgroup->excl_cmpr = 0;
3912 qgroup_dirty(fs_info, qgroup);
3914 spin_unlock(&fs_info->qgroup_lock);
3918 btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
3921 struct btrfs_trans_handle *trans;
3923 ret = qgroup_rescan_init(fs_info, 0, 1);
3928 * We have set the rescan_progress to 0, which means no more
3929 * delayed refs will be accounted by btrfs_qgroup_account_ref.
3930 * However, btrfs_qgroup_account_ref may be right after its call
3931 * to btrfs_find_all_roots, in which case it would still do the
3933 * To solve this, we're committing the transaction, which will
3934 * ensure we run all delayed refs and only after that, we are
3935 * going to clear all tracking information for a clean start.
3938 trans = btrfs_attach_transaction_barrier(fs_info->fs_root);
3939 if (IS_ERR(trans) && trans != ERR_PTR(-ENOENT)) {
3940 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3941 return PTR_ERR(trans);
3942 } else if (trans != ERR_PTR(-ENOENT)) {
3943 ret = btrfs_commit_transaction(trans);
3945 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3950 qgroup_rescan_zero_tracking(fs_info);
3952 mutex_lock(&fs_info->qgroup_rescan_lock);
3953 fs_info->qgroup_rescan_running = true;
3954 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3955 &fs_info->qgroup_rescan_work);
3956 mutex_unlock(&fs_info->qgroup_rescan_lock);
3961 int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
3967 mutex_lock(&fs_info->qgroup_rescan_lock);
3968 running = fs_info->qgroup_rescan_running;
3969 mutex_unlock(&fs_info->qgroup_rescan_lock);
3975 ret = wait_for_completion_interruptible(
3976 &fs_info->qgroup_rescan_completion);
3978 wait_for_completion(&fs_info->qgroup_rescan_completion);
3984 * this is only called from open_ctree where we're still single threaded, thus
3985 * locking is omitted here.
3988 btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
3990 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3991 mutex_lock(&fs_info->qgroup_rescan_lock);
3992 fs_info->qgroup_rescan_running = true;
3993 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3994 &fs_info->qgroup_rescan_work);
3995 mutex_unlock(&fs_info->qgroup_rescan_lock);
3999 #define rbtree_iterate_from_safe(node, next, start) \
4000 for (node = start; node && ({ next = rb_next(node); 1;}); node = next)
4002 static int qgroup_unreserve_range(struct btrfs_inode *inode,
4003 struct extent_changeset *reserved, u64 start,
4006 struct rb_node *node;
4007 struct rb_node *next;
4008 struct ulist_node *entry;
4011 node = reserved->range_changed.root.rb_node;
4015 entry = rb_entry(node, struct ulist_node, rb_node);
4016 if (entry->val < start)
4017 node = node->rb_right;
4019 node = node->rb_left;
4022 if (entry->val > start && rb_prev(&entry->rb_node))
4023 entry = rb_entry(rb_prev(&entry->rb_node), struct ulist_node,
4026 rbtree_iterate_from_safe(node, next, &entry->rb_node) {
4032 entry = rb_entry(node, struct ulist_node, rb_node);
4033 entry_start = entry->val;
4034 entry_end = entry->aux;
4035 entry_len = entry_end - entry_start + 1;
4037 if (entry_start >= start + len)
4039 if (entry_start + entry_len <= start)
4042 * Now the entry is in [start, start + len), revert the
4043 * EXTENT_QGROUP_RESERVED bit.
4045 clear_ret = clear_extent_bits(&inode->io_tree, entry_start,
4046 entry_end, EXTENT_QGROUP_RESERVED);
4047 if (!ret && clear_ret < 0)
4050 ulist_del(&reserved->range_changed, entry->val, entry->aux);
4051 if (likely(reserved->bytes_changed >= entry_len)) {
4052 reserved->bytes_changed -= entry_len;
4055 reserved->bytes_changed = 0;
4063 * Try to free some space for qgroup.
4065 * For qgroup, there are only 3 ways to free qgroup space:
4066 * - Flush nodatacow write
4067 * Any nodatacow write will free its reserved data space at run_delalloc_range().
4068 * In theory, we should only flush nodatacow inodes, but it's not yet
4069 * possible, so we need to flush the whole root.
4071 * - Wait for ordered extents
4072 * When ordered extents are finished, their reserved metadata is finally
4073 * converted to per_trans status, which can be freed by later commit
4076 * - Commit transaction
4077 * This would free the meta_per_trans space.
4078 * In theory this shouldn't provide much space, but any more qgroup space
4081 static int try_flush_qgroup(struct btrfs_root *root)
4083 struct btrfs_trans_handle *trans;
4086 /* Can't hold an open transaction or we run the risk of deadlocking. */
4087 ASSERT(current->journal_info == NULL);
4088 if (WARN_ON(current->journal_info))
4092 * We don't want to run flush again and again, so if there is a running
4093 * one, we won't try to start a new flush, but exit directly.
4095 if (test_and_set_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state)) {
4096 wait_event(root->qgroup_flush_wait,
4097 !test_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state));
4101 ret = btrfs_start_delalloc_snapshot(root, true);
4104 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
4106 trans = btrfs_attach_transaction_barrier(root);
4107 if (IS_ERR(trans)) {
4108 ret = PTR_ERR(trans);
4114 ret = btrfs_commit_transaction(trans);
4116 clear_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state);
4117 wake_up(&root->qgroup_flush_wait);
4121 static int qgroup_reserve_data(struct btrfs_inode *inode,
4122 struct extent_changeset **reserved_ret, u64 start,
4125 struct btrfs_root *root = inode->root;
4126 struct extent_changeset *reserved;
4127 bool new_reserved = false;
4132 if (btrfs_qgroup_mode(root->fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
4133 !is_fstree(btrfs_root_id(root)) || len == 0)
4136 /* @reserved parameter is mandatory for qgroup */
4137 if (WARN_ON(!reserved_ret))
4139 if (!*reserved_ret) {
4140 new_reserved = true;
4141 *reserved_ret = extent_changeset_alloc();
4145 reserved = *reserved_ret;
4146 /* Record already reserved space */
4147 orig_reserved = reserved->bytes_changed;
4148 ret = set_record_extent_bits(&inode->io_tree, start,
4149 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
4151 /* Newly reserved space */
4152 to_reserve = reserved->bytes_changed - orig_reserved;
4153 trace_btrfs_qgroup_reserve_data(&inode->vfs_inode, start, len,
4154 to_reserve, QGROUP_RESERVE);
4157 ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
4164 qgroup_unreserve_range(inode, reserved, start, len);
4167 extent_changeset_free(reserved);
4168 *reserved_ret = NULL;
4174 * Reserve qgroup space for range [start, start + len).
4176 * This function will either reserve space from related qgroups or do nothing
4177 * if the range is already reserved.
4179 * Return 0 for successful reservation
4180 * Return <0 for error (including -EQUOT)
4182 * NOTE: This function may sleep for memory allocation, dirty page flushing and
4183 * commit transaction. So caller should not hold any dirty page locked.
4185 int btrfs_qgroup_reserve_data(struct btrfs_inode *inode,
4186 struct extent_changeset **reserved_ret, u64 start,
4191 ret = qgroup_reserve_data(inode, reserved_ret, start, len);
4192 if (ret <= 0 && ret != -EDQUOT)
4195 ret = try_flush_qgroup(inode->root);
4198 return qgroup_reserve_data(inode, reserved_ret, start, len);
4201 /* Free ranges specified by @reserved, normally in error path */
4202 static int qgroup_free_reserved_data(struct btrfs_inode *inode,
4203 struct extent_changeset *reserved,
4204 u64 start, u64 len, u64 *freed_ret)
4206 struct btrfs_root *root = inode->root;
4207 struct ulist_node *unode;
4208 struct ulist_iterator uiter;
4209 struct extent_changeset changeset;
4213 extent_changeset_init(&changeset);
4214 len = round_up(start + len, root->fs_info->sectorsize);
4215 start = round_down(start, root->fs_info->sectorsize);
4217 ULIST_ITER_INIT(&uiter);
4218 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
4219 u64 range_start = unode->val;
4220 /* unode->aux is the inclusive end */
4221 u64 range_len = unode->aux - range_start + 1;
4225 extent_changeset_release(&changeset);
4227 /* Only free range in range [start, start + len) */
4228 if (range_start >= start + len ||
4229 range_start + range_len <= start)
4231 free_start = max(range_start, start);
4232 free_len = min(start + len, range_start + range_len) -
4235 * TODO: To also modify reserved->ranges_reserved to reflect
4238 * However as long as we free qgroup reserved according to
4239 * EXTENT_QGROUP_RESERVED, we won't double free.
4240 * So not need to rush.
4242 ret = clear_record_extent_bits(&inode->io_tree, free_start,
4243 free_start + free_len - 1,
4244 EXTENT_QGROUP_RESERVED, &changeset);
4247 freed += changeset.bytes_changed;
4249 btrfs_qgroup_free_refroot(root->fs_info, btrfs_root_id(root), freed,
4250 BTRFS_QGROUP_RSV_DATA);
4255 extent_changeset_release(&changeset);
4259 static int __btrfs_qgroup_release_data(struct btrfs_inode *inode,
4260 struct extent_changeset *reserved, u64 start, u64 len,
4261 u64 *released, int free)
4263 struct extent_changeset changeset;
4264 int trace_op = QGROUP_RELEASE;
4267 if (btrfs_qgroup_mode(inode->root->fs_info) == BTRFS_QGROUP_MODE_DISABLED) {
4268 extent_changeset_init(&changeset);
4269 return clear_record_extent_bits(&inode->io_tree, start,
4271 EXTENT_QGROUP_RESERVED, &changeset);
4274 /* In release case, we shouldn't have @reserved */
4275 WARN_ON(!free && reserved);
4276 if (free && reserved)
4277 return qgroup_free_reserved_data(inode, reserved, start, len, released);
4278 extent_changeset_init(&changeset);
4279 ret = clear_record_extent_bits(&inode->io_tree, start, start + len -1,
4280 EXTENT_QGROUP_RESERVED, &changeset);
4285 trace_op = QGROUP_FREE;
4286 trace_btrfs_qgroup_release_data(&inode->vfs_inode, start, len,
4287 changeset.bytes_changed, trace_op);
4289 btrfs_qgroup_free_refroot(inode->root->fs_info,
4290 btrfs_root_id(inode->root),
4291 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
4293 *released = changeset.bytes_changed;
4295 extent_changeset_release(&changeset);
4300 * Free a reserved space range from io_tree and related qgroups
4302 * Should be called when a range of pages get invalidated before reaching disk.
4303 * Or for error cleanup case.
4304 * if @reserved is given, only reserved range in [@start, @start + @len) will
4307 * For data written to disk, use btrfs_qgroup_release_data().
4309 * NOTE: This function may sleep for memory allocation.
4311 int btrfs_qgroup_free_data(struct btrfs_inode *inode,
4312 struct extent_changeset *reserved,
4313 u64 start, u64 len, u64 *freed)
4315 return __btrfs_qgroup_release_data(inode, reserved, start, len, freed, 1);
4319 * Release a reserved space range from io_tree only.
4321 * Should be called when a range of pages get written to disk and corresponding
4322 * FILE_EXTENT is inserted into corresponding root.
4324 * Since new qgroup accounting framework will only update qgroup numbers at
4325 * commit_transaction() time, its reserved space shouldn't be freed from
4328 * But we should release the range from io_tree, to allow further write to be
4331 * NOTE: This function may sleep for memory allocation.
4333 int btrfs_qgroup_release_data(struct btrfs_inode *inode, u64 start, u64 len, u64 *released)
4335 return __btrfs_qgroup_release_data(inode, NULL, start, len, released, 0);
4338 static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
4339 enum btrfs_qgroup_rsv_type type)
4341 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
4342 type != BTRFS_QGROUP_RSV_META_PERTRANS)
4347 spin_lock(&root->qgroup_meta_rsv_lock);
4348 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
4349 root->qgroup_meta_rsv_prealloc += num_bytes;
4351 root->qgroup_meta_rsv_pertrans += num_bytes;
4352 spin_unlock(&root->qgroup_meta_rsv_lock);
4355 static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
4356 enum btrfs_qgroup_rsv_type type)
4358 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
4359 type != BTRFS_QGROUP_RSV_META_PERTRANS)
4364 spin_lock(&root->qgroup_meta_rsv_lock);
4365 if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
4366 num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
4368 root->qgroup_meta_rsv_prealloc -= num_bytes;
4370 num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
4372 root->qgroup_meta_rsv_pertrans -= num_bytes;
4374 spin_unlock(&root->qgroup_meta_rsv_lock);
4378 int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
4379 enum btrfs_qgroup_rsv_type type, bool enforce)
4381 struct btrfs_fs_info *fs_info = root->fs_info;
4384 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
4385 !is_fstree(btrfs_root_id(root)) || num_bytes == 0)
4388 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
4389 trace_qgroup_meta_reserve(root, (s64)num_bytes, type);
4390 ret = qgroup_reserve(root, num_bytes, enforce, type);
4394 * Record what we have reserved into root.
4396 * To avoid quota disabled->enabled underflow.
4397 * In that case, we may try to free space we haven't reserved
4398 * (since quota was disabled), so record what we reserved into root.
4399 * And ensure later release won't underflow this number.
4401 add_root_meta_rsv(root, num_bytes, type);
4405 int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
4406 enum btrfs_qgroup_rsv_type type, bool enforce,
4411 ret = btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
4412 if ((ret <= 0 && ret != -EDQUOT) || noflush)
4415 ret = try_flush_qgroup(root);
4418 return btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
4422 * Per-transaction meta reservation should be all freed at transaction commit
4425 void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
4427 struct btrfs_fs_info *fs_info = root->fs_info;
4429 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
4430 !is_fstree(btrfs_root_id(root)))
4433 /* TODO: Update trace point to handle such free */
4434 trace_qgroup_meta_free_all_pertrans(root);
4435 /* Special value -1 means to free all reserved space */
4436 btrfs_qgroup_free_refroot(fs_info, btrfs_root_id(root), (u64)-1,
4437 BTRFS_QGROUP_RSV_META_PERTRANS);
4440 void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
4441 enum btrfs_qgroup_rsv_type type)
4443 struct btrfs_fs_info *fs_info = root->fs_info;
4445 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
4446 !is_fstree(btrfs_root_id(root)))
4450 * reservation for META_PREALLOC can happen before quota is enabled,
4451 * which can lead to underflow.
4452 * Here ensure we will only free what we really have reserved.
4454 num_bytes = sub_root_meta_rsv(root, num_bytes, type);
4455 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
4456 trace_qgroup_meta_reserve(root, -(s64)num_bytes, type);
4457 btrfs_qgroup_free_refroot(fs_info, btrfs_root_id(root), num_bytes, type);
4460 static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
4463 struct btrfs_qgroup *qgroup;
4464 LIST_HEAD(qgroup_list);
4468 if (!fs_info->quota_root)
4471 spin_lock(&fs_info->qgroup_lock);
4472 qgroup = find_qgroup_rb(fs_info, ref_root);
4476 qgroup_iterator_add(&qgroup_list, qgroup);
4477 list_for_each_entry(qgroup, &qgroup_list, iterator) {
4478 struct btrfs_qgroup_list *glist;
4480 qgroup_rsv_release(fs_info, qgroup, num_bytes,
4481 BTRFS_QGROUP_RSV_META_PREALLOC);
4482 if (!sb_rdonly(fs_info->sb))
4483 qgroup_rsv_add(fs_info, qgroup, num_bytes,
4484 BTRFS_QGROUP_RSV_META_PERTRANS);
4486 list_for_each_entry(glist, &qgroup->groups, next_group)
4487 qgroup_iterator_add(&qgroup_list, glist->group);
4490 qgroup_iterator_clean(&qgroup_list);
4491 spin_unlock(&fs_info->qgroup_lock);
4495 * Convert @num_bytes of META_PREALLOCATED reservation to META_PERTRANS.
4497 * This is called when preallocated meta reservation needs to be used.
4498 * Normally after btrfs_join_transaction() call.
4500 void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
4502 struct btrfs_fs_info *fs_info = root->fs_info;
4504 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
4505 !is_fstree(btrfs_root_id(root)))
4507 /* Same as btrfs_qgroup_free_meta_prealloc() */
4508 num_bytes = sub_root_meta_rsv(root, num_bytes,
4509 BTRFS_QGROUP_RSV_META_PREALLOC);
4510 trace_qgroup_meta_convert(root, num_bytes);
4511 qgroup_convert_meta(fs_info, btrfs_root_id(root), num_bytes);
4512 if (!sb_rdonly(fs_info->sb))
4513 add_root_meta_rsv(root, num_bytes, BTRFS_QGROUP_RSV_META_PERTRANS);
4517 * Check qgroup reserved space leaking, normally at destroy inode
4520 void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode)
4522 struct extent_changeset changeset;
4523 struct ulist_node *unode;
4524 struct ulist_iterator iter;
4527 extent_changeset_init(&changeset);
4528 ret = clear_record_extent_bits(&inode->io_tree, 0, (u64)-1,
4529 EXTENT_QGROUP_RESERVED, &changeset);
4532 if (WARN_ON(changeset.bytes_changed)) {
4533 ULIST_ITER_INIT(&iter);
4534 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
4535 btrfs_warn(inode->root->fs_info,
4536 "leaking qgroup reserved space, ino: %llu, start: %llu, end: %llu",
4537 btrfs_ino(inode), unode->val, unode->aux);
4539 btrfs_qgroup_free_refroot(inode->root->fs_info,
4540 btrfs_root_id(inode->root),
4541 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
4544 extent_changeset_release(&changeset);
4547 void btrfs_qgroup_init_swapped_blocks(
4548 struct btrfs_qgroup_swapped_blocks *swapped_blocks)
4552 spin_lock_init(&swapped_blocks->lock);
4553 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
4554 swapped_blocks->blocks[i] = RB_ROOT;
4555 swapped_blocks->swapped = false;
4559 * Delete all swapped blocks record of @root.
4560 * Every record here means we skipped a full subtree scan for qgroup.
4562 * Gets called when committing one transaction.
4564 void btrfs_qgroup_clean_swapped_blocks(struct btrfs_root *root)
4566 struct btrfs_qgroup_swapped_blocks *swapped_blocks;
4569 swapped_blocks = &root->swapped_blocks;
4571 spin_lock(&swapped_blocks->lock);
4572 if (!swapped_blocks->swapped)
4574 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4575 struct rb_root *cur_root = &swapped_blocks->blocks[i];
4576 struct btrfs_qgroup_swapped_block *entry;
4577 struct btrfs_qgroup_swapped_block *next;
4579 rbtree_postorder_for_each_entry_safe(entry, next, cur_root,
4582 swapped_blocks->blocks[i] = RB_ROOT;
4584 swapped_blocks->swapped = false;
4586 spin_unlock(&swapped_blocks->lock);
4590 * Add subtree roots record into @subvol_root.
4592 * @subvol_root: tree root of the subvolume tree get swapped
4593 * @bg: block group under balance
4594 * @subvol_parent/slot: pointer to the subtree root in subvolume tree
4595 * @reloc_parent/slot: pointer to the subtree root in reloc tree
4596 * BOTH POINTERS ARE BEFORE TREE SWAP
4597 * @last_snapshot: last snapshot generation of the subvolume tree
4599 int btrfs_qgroup_add_swapped_blocks(struct btrfs_trans_handle *trans,
4600 struct btrfs_root *subvol_root,
4601 struct btrfs_block_group *bg,
4602 struct extent_buffer *subvol_parent, int subvol_slot,
4603 struct extent_buffer *reloc_parent, int reloc_slot,
4606 struct btrfs_fs_info *fs_info = subvol_root->fs_info;
4607 struct btrfs_qgroup_swapped_blocks *blocks = &subvol_root->swapped_blocks;
4608 struct btrfs_qgroup_swapped_block *block;
4609 struct rb_node **cur;
4610 struct rb_node *parent = NULL;
4611 int level = btrfs_header_level(subvol_parent) - 1;
4614 if (!btrfs_qgroup_full_accounting(fs_info))
4617 if (btrfs_node_ptr_generation(subvol_parent, subvol_slot) >
4618 btrfs_node_ptr_generation(reloc_parent, reloc_slot)) {
4619 btrfs_err_rl(fs_info,
4620 "%s: bad parameter order, subvol_gen=%llu reloc_gen=%llu",
4622 btrfs_node_ptr_generation(subvol_parent, subvol_slot),
4623 btrfs_node_ptr_generation(reloc_parent, reloc_slot));
4627 block = kmalloc(sizeof(*block), GFP_NOFS);
4634 * @reloc_parent/slot is still before swap, while @block is going to
4635 * record the bytenr after swap, so we do the swap here.
4637 block->subvol_bytenr = btrfs_node_blockptr(reloc_parent, reloc_slot);
4638 block->subvol_generation = btrfs_node_ptr_generation(reloc_parent,
4640 block->reloc_bytenr = btrfs_node_blockptr(subvol_parent, subvol_slot);
4641 block->reloc_generation = btrfs_node_ptr_generation(subvol_parent,
4643 block->last_snapshot = last_snapshot;
4644 block->level = level;
4647 * If we have bg == NULL, we're called from btrfs_recover_relocation(),
4648 * no one else can modify tree blocks thus we qgroup will not change
4649 * no matter the value of trace_leaf.
4651 if (bg && bg->flags & BTRFS_BLOCK_GROUP_DATA)
4652 block->trace_leaf = true;
4654 block->trace_leaf = false;
4655 btrfs_node_key_to_cpu(reloc_parent, &block->first_key, reloc_slot);
4657 /* Insert @block into @blocks */
4658 spin_lock(&blocks->lock);
4659 cur = &blocks->blocks[level].rb_node;
4661 struct btrfs_qgroup_swapped_block *entry;
4664 entry = rb_entry(parent, struct btrfs_qgroup_swapped_block,
4667 if (entry->subvol_bytenr < block->subvol_bytenr) {
4668 cur = &(*cur)->rb_left;
4669 } else if (entry->subvol_bytenr > block->subvol_bytenr) {
4670 cur = &(*cur)->rb_right;
4672 if (entry->subvol_generation !=
4673 block->subvol_generation ||
4674 entry->reloc_bytenr != block->reloc_bytenr ||
4675 entry->reloc_generation !=
4676 block->reloc_generation) {
4678 * Duplicated but mismatch entry found.
4681 * Marking qgroup inconsistent should be enough
4684 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
4691 rb_link_node(&block->node, parent, cur);
4692 rb_insert_color(&block->node, &blocks->blocks[level]);
4693 blocks->swapped = true;
4695 spin_unlock(&blocks->lock);
4698 qgroup_mark_inconsistent(fs_info);
4703 * Check if the tree block is a subtree root, and if so do the needed
4704 * delayed subtree trace for qgroup.
4706 * This is called during btrfs_cow_block().
4708 int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans,
4709 struct btrfs_root *root,
4710 struct extent_buffer *subvol_eb)
4712 struct btrfs_fs_info *fs_info = root->fs_info;
4713 struct btrfs_tree_parent_check check = { 0 };
4714 struct btrfs_qgroup_swapped_blocks *blocks = &root->swapped_blocks;
4715 struct btrfs_qgroup_swapped_block *block;
4716 struct extent_buffer *reloc_eb = NULL;
4717 struct rb_node *node;
4719 bool swapped = false;
4720 int level = btrfs_header_level(subvol_eb);
4724 if (!btrfs_qgroup_full_accounting(fs_info))
4726 if (!is_fstree(btrfs_root_id(root)) || !root->reloc_root)
4729 spin_lock(&blocks->lock);
4730 if (!blocks->swapped) {
4731 spin_unlock(&blocks->lock);
4734 node = blocks->blocks[level].rb_node;
4737 block = rb_entry(node, struct btrfs_qgroup_swapped_block, node);
4738 if (block->subvol_bytenr < subvol_eb->start) {
4739 node = node->rb_left;
4740 } else if (block->subvol_bytenr > subvol_eb->start) {
4741 node = node->rb_right;
4748 spin_unlock(&blocks->lock);
4751 /* Found one, remove it from @blocks first and update blocks->swapped */
4752 rb_erase(&block->node, &blocks->blocks[level]);
4753 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4754 if (RB_EMPTY_ROOT(&blocks->blocks[i])) {
4759 blocks->swapped = swapped;
4760 spin_unlock(&blocks->lock);
4762 check.level = block->level;
4763 check.transid = block->reloc_generation;
4764 check.has_first_key = true;
4765 memcpy(&check.first_key, &block->first_key, sizeof(check.first_key));
4767 /* Read out reloc subtree root */
4768 reloc_eb = read_tree_block(fs_info, block->reloc_bytenr, &check);
4769 if (IS_ERR(reloc_eb)) {
4770 ret = PTR_ERR(reloc_eb);
4774 if (!extent_buffer_uptodate(reloc_eb)) {
4779 ret = qgroup_trace_subtree_swap(trans, reloc_eb, subvol_eb,
4780 block->last_snapshot, block->trace_leaf);
4783 free_extent_buffer(reloc_eb);
4786 btrfs_err_rl(fs_info,
4787 "failed to account subtree at bytenr %llu: %d",
4788 subvol_eb->start, ret);
4789 qgroup_mark_inconsistent(fs_info);
4794 void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans)
4796 struct btrfs_qgroup_extent_record *entry;
4797 struct btrfs_qgroup_extent_record *next;
4798 struct rb_root *root;
4800 root = &trans->delayed_refs.dirty_extent_root;
4801 rbtree_postorder_for_each_entry_safe(entry, next, root, node) {
4802 ulist_free(entry->old_roots);
4808 void btrfs_free_squota_rsv(struct btrfs_fs_info *fs_info, u64 root, u64 rsv_bytes)
4810 if (btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_SIMPLE)
4813 if (!is_fstree(root))
4816 btrfs_qgroup_free_refroot(fs_info, root, rsv_bytes, BTRFS_QGROUP_RSV_DATA);
4819 int btrfs_record_squota_delta(struct btrfs_fs_info *fs_info,
4820 struct btrfs_squota_delta *delta)
4823 struct btrfs_qgroup *qgroup;
4824 struct btrfs_qgroup *qg;
4825 LIST_HEAD(qgroup_list);
4826 u64 root = delta->root;
4827 u64 num_bytes = delta->num_bytes;
4828 const int sign = (delta->is_inc ? 1 : -1);
4830 if (btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_SIMPLE)
4833 if (!is_fstree(root))
4836 /* If the extent predates enabling quotas, don't count it. */
4837 if (delta->generation < fs_info->qgroup_enable_gen)
4840 spin_lock(&fs_info->qgroup_lock);
4841 qgroup = find_qgroup_rb(fs_info, root);
4848 qgroup_iterator_add(&qgroup_list, qgroup);
4849 list_for_each_entry(qg, &qgroup_list, iterator) {
4850 struct btrfs_qgroup_list *glist;
4852 qg->excl += num_bytes * sign;
4853 qg->rfer += num_bytes * sign;
4854 qgroup_dirty(fs_info, qg);
4856 list_for_each_entry(glist, &qg->groups, next_group)
4857 qgroup_iterator_add(&qgroup_list, glist->group);
4859 qgroup_iterator_clean(&qgroup_list);
4862 spin_unlock(&fs_info->qgroup_lock);