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"
29 * Helpers to access qgroup reservation
31 * Callers should ensure the lock context and type are valid
34 static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
39 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
40 ret += qgroup->rsv.values[i];
45 #ifdef CONFIG_BTRFS_DEBUG
46 static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
48 if (type == BTRFS_QGROUP_RSV_DATA)
50 if (type == BTRFS_QGROUP_RSV_META_PERTRANS)
51 return "meta_pertrans";
52 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
53 return "meta_prealloc";
58 static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
59 struct btrfs_qgroup *qgroup, u64 num_bytes,
60 enum btrfs_qgroup_rsv_type type)
62 trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
63 qgroup->rsv.values[type] += num_bytes;
66 static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
67 struct btrfs_qgroup *qgroup, u64 num_bytes,
68 enum btrfs_qgroup_rsv_type type)
70 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
71 if (qgroup->rsv.values[type] >= num_bytes) {
72 qgroup->rsv.values[type] -= num_bytes;
75 #ifdef CONFIG_BTRFS_DEBUG
77 "qgroup %llu %s reserved space underflow, have %llu to free %llu",
78 qgroup->qgroupid, qgroup_rsv_type_str(type),
79 qgroup->rsv.values[type], num_bytes);
81 qgroup->rsv.values[type] = 0;
84 static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
85 struct btrfs_qgroup *dest,
86 struct btrfs_qgroup *src)
90 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
91 qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
94 static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
95 struct btrfs_qgroup *dest,
96 struct btrfs_qgroup *src)
100 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
101 qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
104 static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
107 if (qg->old_refcnt < seq)
108 qg->old_refcnt = seq;
109 qg->old_refcnt += mod;
112 static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
115 if (qg->new_refcnt < seq)
116 qg->new_refcnt = seq;
117 qg->new_refcnt += mod;
120 static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
122 if (qg->old_refcnt < seq)
124 return qg->old_refcnt - seq;
127 static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
129 if (qg->new_refcnt < seq)
131 return qg->new_refcnt - seq;
135 * glue structure to represent the relations between qgroups.
137 struct btrfs_qgroup_list {
138 struct list_head next_group;
139 struct list_head next_member;
140 struct btrfs_qgroup *group;
141 struct btrfs_qgroup *member;
144 static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
146 return (u64)(uintptr_t)qg;
149 static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
151 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
155 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
157 static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
159 /* must be called with qgroup_ioctl_lock held */
160 static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
163 struct rb_node *n = fs_info->qgroup_tree.rb_node;
164 struct btrfs_qgroup *qgroup;
167 qgroup = rb_entry(n, struct btrfs_qgroup, node);
168 if (qgroup->qgroupid < qgroupid)
170 else if (qgroup->qgroupid > qgroupid)
178 /* must be called with qgroup_lock held */
179 static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
182 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
183 struct rb_node *parent = NULL;
184 struct btrfs_qgroup *qgroup;
188 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
190 if (qgroup->qgroupid < qgroupid)
192 else if (qgroup->qgroupid > qgroupid)
198 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
200 return ERR_PTR(-ENOMEM);
202 qgroup->qgroupid = qgroupid;
203 INIT_LIST_HEAD(&qgroup->groups);
204 INIT_LIST_HEAD(&qgroup->members);
205 INIT_LIST_HEAD(&qgroup->dirty);
207 rb_link_node(&qgroup->node, parent, p);
208 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
213 static void __del_qgroup_rb(struct btrfs_fs_info *fs_info,
214 struct btrfs_qgroup *qgroup)
216 struct btrfs_qgroup_list *list;
218 list_del(&qgroup->dirty);
219 while (!list_empty(&qgroup->groups)) {
220 list = list_first_entry(&qgroup->groups,
221 struct btrfs_qgroup_list, next_group);
222 list_del(&list->next_group);
223 list_del(&list->next_member);
227 while (!list_empty(&qgroup->members)) {
228 list = list_first_entry(&qgroup->members,
229 struct btrfs_qgroup_list, next_member);
230 list_del(&list->next_group);
231 list_del(&list->next_member);
236 /* must be called with qgroup_lock held */
237 static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
239 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
244 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
245 __del_qgroup_rb(fs_info, qgroup);
250 * Add relation specified by two qgroups.
252 * Must be called with qgroup_lock held.
254 * Return: 0 on success
255 * -ENOENT if one of the qgroups is NULL
258 static int __add_relation_rb(struct btrfs_qgroup *member, struct btrfs_qgroup *parent)
260 struct btrfs_qgroup_list *list;
262 if (!member || !parent)
265 list = kzalloc(sizeof(*list), GFP_ATOMIC);
269 list->group = parent;
270 list->member = member;
271 list_add_tail(&list->next_group, &member->groups);
272 list_add_tail(&list->next_member, &parent->members);
278 * Add relation specified by two qgoup ids.
280 * Must be called with qgroup_lock held.
282 * Return: 0 on success
283 * -ENOENT if one of the ids does not exist
286 static int add_relation_rb(struct btrfs_fs_info *fs_info, u64 memberid, u64 parentid)
288 struct btrfs_qgroup *member;
289 struct btrfs_qgroup *parent;
291 member = find_qgroup_rb(fs_info, memberid);
292 parent = find_qgroup_rb(fs_info, parentid);
294 return __add_relation_rb(member, parent);
297 /* Must be called with qgroup_lock held */
298 static int del_relation_rb(struct btrfs_fs_info *fs_info,
299 u64 memberid, u64 parentid)
301 struct btrfs_qgroup *member;
302 struct btrfs_qgroup *parent;
303 struct btrfs_qgroup_list *list;
305 member = find_qgroup_rb(fs_info, memberid);
306 parent = find_qgroup_rb(fs_info, parentid);
307 if (!member || !parent)
310 list_for_each_entry(list, &member->groups, next_group) {
311 if (list->group == parent) {
312 list_del(&list->next_group);
313 list_del(&list->next_member);
321 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
322 int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
325 struct btrfs_qgroup *qgroup;
327 qgroup = find_qgroup_rb(fs_info, qgroupid);
330 if (qgroup->rfer != rfer || qgroup->excl != excl)
337 * The full config is read in one go, only called from open_ctree()
338 * It doesn't use any locking, as at this point we're still single-threaded
340 int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
342 struct btrfs_key key;
343 struct btrfs_key found_key;
344 struct btrfs_root *quota_root = fs_info->quota_root;
345 struct btrfs_path *path = NULL;
346 struct extent_buffer *l;
350 u64 rescan_progress = 0;
352 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
355 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
356 if (!fs_info->qgroup_ulist) {
361 path = btrfs_alloc_path();
367 ret = btrfs_sysfs_add_qgroups(fs_info);
370 /* default this to quota off, in case no status key is found */
371 fs_info->qgroup_flags = 0;
374 * pass 1: read status, all qgroup infos and limits
379 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
384 struct btrfs_qgroup *qgroup;
386 slot = path->slots[0];
388 btrfs_item_key_to_cpu(l, &found_key, slot);
390 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
391 struct btrfs_qgroup_status_item *ptr;
393 ptr = btrfs_item_ptr(l, slot,
394 struct btrfs_qgroup_status_item);
396 if (btrfs_qgroup_status_version(l, ptr) !=
397 BTRFS_QGROUP_STATUS_VERSION) {
399 "old qgroup version, quota disabled");
402 if (btrfs_qgroup_status_generation(l, ptr) !=
403 fs_info->generation) {
404 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
406 "qgroup generation mismatch, marked as inconsistent");
408 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
410 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
414 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
415 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
418 qgroup = find_qgroup_rb(fs_info, found_key.offset);
419 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
420 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
421 btrfs_err(fs_info, "inconsistent qgroup config");
422 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
425 qgroup = add_qgroup_rb(fs_info, found_key.offset);
426 if (IS_ERR(qgroup)) {
427 ret = PTR_ERR(qgroup);
431 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
435 switch (found_key.type) {
436 case BTRFS_QGROUP_INFO_KEY: {
437 struct btrfs_qgroup_info_item *ptr;
439 ptr = btrfs_item_ptr(l, slot,
440 struct btrfs_qgroup_info_item);
441 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
442 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
443 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
444 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
445 /* generation currently unused */
448 case BTRFS_QGROUP_LIMIT_KEY: {
449 struct btrfs_qgroup_limit_item *ptr;
451 ptr = btrfs_item_ptr(l, slot,
452 struct btrfs_qgroup_limit_item);
453 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
454 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
455 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
456 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
457 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
462 ret = btrfs_next_item(quota_root, path);
468 btrfs_release_path(path);
471 * pass 2: read all qgroup relations
474 key.type = BTRFS_QGROUP_RELATION_KEY;
476 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
480 slot = path->slots[0];
482 btrfs_item_key_to_cpu(l, &found_key, slot);
484 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
487 if (found_key.objectid > found_key.offset) {
488 /* parent <- member, not needed to build config */
489 /* FIXME should we omit the key completely? */
493 ret = add_relation_rb(fs_info, found_key.objectid,
495 if (ret == -ENOENT) {
497 "orphan qgroup relation 0x%llx->0x%llx",
498 found_key.objectid, found_key.offset);
499 ret = 0; /* ignore the error */
504 ret = btrfs_next_item(quota_root, path);
511 btrfs_free_path(path);
512 fs_info->qgroup_flags |= flags;
513 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
514 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
515 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
517 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
520 ulist_free(fs_info->qgroup_ulist);
521 fs_info->qgroup_ulist = NULL;
522 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
523 btrfs_sysfs_del_qgroups(fs_info);
526 return ret < 0 ? ret : 0;
530 * Called in close_ctree() when quota is still enabled. This verifies we don't
531 * leak some reserved space.
533 * Return false if no reserved space is left.
534 * Return true if some reserved space is leaked.
536 bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info)
538 struct rb_node *node;
541 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
544 * Since we're unmounting, there is no race and no need to grab qgroup
545 * lock. And here we don't go post-order to provide a more user
546 * friendly sorted result.
548 for (node = rb_first(&fs_info->qgroup_tree); node; node = rb_next(node)) {
549 struct btrfs_qgroup *qgroup;
552 qgroup = rb_entry(node, struct btrfs_qgroup, node);
553 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++) {
554 if (qgroup->rsv.values[i]) {
557 "qgroup %hu/%llu has unreleased space, type %d rsv %llu",
558 btrfs_qgroup_level(qgroup->qgroupid),
559 btrfs_qgroup_subvolid(qgroup->qgroupid),
560 i, qgroup->rsv.values[i]);
568 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
569 * first two are in single-threaded paths.And for the third one, we have set
570 * quota_root to be null with qgroup_lock held before, so it is safe to clean
571 * up the in-memory structures without qgroup_lock held.
573 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
576 struct btrfs_qgroup *qgroup;
578 while ((n = rb_first(&fs_info->qgroup_tree))) {
579 qgroup = rb_entry(n, struct btrfs_qgroup, node);
580 rb_erase(n, &fs_info->qgroup_tree);
581 __del_qgroup_rb(fs_info, qgroup);
582 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
586 * We call btrfs_free_qgroup_config() when unmounting
587 * filesystem and disabling quota, so we set qgroup_ulist
588 * to be null here to avoid double free.
590 ulist_free(fs_info->qgroup_ulist);
591 fs_info->qgroup_ulist = NULL;
592 btrfs_sysfs_del_qgroups(fs_info);
595 static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
599 struct btrfs_root *quota_root = trans->fs_info->quota_root;
600 struct btrfs_path *path;
601 struct btrfs_key key;
603 path = btrfs_alloc_path();
608 key.type = BTRFS_QGROUP_RELATION_KEY;
611 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
613 btrfs_mark_buffer_dirty(path->nodes[0]);
615 btrfs_free_path(path);
619 static int del_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
623 struct btrfs_root *quota_root = trans->fs_info->quota_root;
624 struct btrfs_path *path;
625 struct btrfs_key key;
627 path = btrfs_alloc_path();
632 key.type = BTRFS_QGROUP_RELATION_KEY;
635 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
644 ret = btrfs_del_item(trans, quota_root, path);
646 btrfs_free_path(path);
650 static int add_qgroup_item(struct btrfs_trans_handle *trans,
651 struct btrfs_root *quota_root, u64 qgroupid)
654 struct btrfs_path *path;
655 struct btrfs_qgroup_info_item *qgroup_info;
656 struct btrfs_qgroup_limit_item *qgroup_limit;
657 struct extent_buffer *leaf;
658 struct btrfs_key key;
660 if (btrfs_is_testing(quota_root->fs_info))
663 path = btrfs_alloc_path();
668 key.type = BTRFS_QGROUP_INFO_KEY;
669 key.offset = qgroupid;
672 * Avoid a transaction abort by catching -EEXIST here. In that
673 * case, we proceed by re-initializing the existing structure
677 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
678 sizeof(*qgroup_info));
679 if (ret && ret != -EEXIST)
682 leaf = path->nodes[0];
683 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
684 struct btrfs_qgroup_info_item);
685 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
686 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
687 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
688 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
689 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
691 btrfs_mark_buffer_dirty(leaf);
693 btrfs_release_path(path);
695 key.type = BTRFS_QGROUP_LIMIT_KEY;
696 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
697 sizeof(*qgroup_limit));
698 if (ret && ret != -EEXIST)
701 leaf = path->nodes[0];
702 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
703 struct btrfs_qgroup_limit_item);
704 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
705 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
706 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
707 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
708 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
710 btrfs_mark_buffer_dirty(leaf);
714 btrfs_free_path(path);
718 static int del_qgroup_item(struct btrfs_trans_handle *trans, u64 qgroupid)
721 struct btrfs_root *quota_root = trans->fs_info->quota_root;
722 struct btrfs_path *path;
723 struct btrfs_key key;
725 path = btrfs_alloc_path();
730 key.type = BTRFS_QGROUP_INFO_KEY;
731 key.offset = qgroupid;
732 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
741 ret = btrfs_del_item(trans, quota_root, path);
745 btrfs_release_path(path);
747 key.type = BTRFS_QGROUP_LIMIT_KEY;
748 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
757 ret = btrfs_del_item(trans, quota_root, path);
760 btrfs_free_path(path);
764 static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
765 struct btrfs_qgroup *qgroup)
767 struct btrfs_root *quota_root = trans->fs_info->quota_root;
768 struct btrfs_path *path;
769 struct btrfs_key key;
770 struct extent_buffer *l;
771 struct btrfs_qgroup_limit_item *qgroup_limit;
776 key.type = BTRFS_QGROUP_LIMIT_KEY;
777 key.offset = qgroup->qgroupid;
779 path = btrfs_alloc_path();
783 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
791 slot = path->slots[0];
792 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
793 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
794 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
795 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
796 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
797 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
799 btrfs_mark_buffer_dirty(l);
802 btrfs_free_path(path);
806 static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
807 struct btrfs_qgroup *qgroup)
809 struct btrfs_fs_info *fs_info = trans->fs_info;
810 struct btrfs_root *quota_root = fs_info->quota_root;
811 struct btrfs_path *path;
812 struct btrfs_key key;
813 struct extent_buffer *l;
814 struct btrfs_qgroup_info_item *qgroup_info;
818 if (btrfs_is_testing(fs_info))
822 key.type = BTRFS_QGROUP_INFO_KEY;
823 key.offset = qgroup->qgroupid;
825 path = btrfs_alloc_path();
829 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
837 slot = path->slots[0];
838 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
839 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
840 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
841 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
842 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
843 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
845 btrfs_mark_buffer_dirty(l);
848 btrfs_free_path(path);
852 static int update_qgroup_status_item(struct btrfs_trans_handle *trans)
854 struct btrfs_fs_info *fs_info = trans->fs_info;
855 struct btrfs_root *quota_root = fs_info->quota_root;
856 struct btrfs_path *path;
857 struct btrfs_key key;
858 struct extent_buffer *l;
859 struct btrfs_qgroup_status_item *ptr;
864 key.type = BTRFS_QGROUP_STATUS_KEY;
867 path = btrfs_alloc_path();
871 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
879 slot = path->slots[0];
880 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
881 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
882 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
883 btrfs_set_qgroup_status_rescan(l, ptr,
884 fs_info->qgroup_rescan_progress.objectid);
886 btrfs_mark_buffer_dirty(l);
889 btrfs_free_path(path);
894 * called with qgroup_lock held
896 static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
897 struct btrfs_root *root)
899 struct btrfs_path *path;
900 struct btrfs_key key;
901 struct extent_buffer *leaf = NULL;
905 path = btrfs_alloc_path();
914 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
917 leaf = path->nodes[0];
918 nr = btrfs_header_nritems(leaf);
922 * delete the leaf one by one
923 * since the whole tree is going
927 ret = btrfs_del_items(trans, root, path, 0, nr);
931 btrfs_release_path(path);
935 btrfs_free_path(path);
939 int btrfs_quota_enable(struct btrfs_fs_info *fs_info)
941 struct btrfs_root *quota_root;
942 struct btrfs_root *tree_root = fs_info->tree_root;
943 struct btrfs_path *path = NULL;
944 struct btrfs_qgroup_status_item *ptr;
945 struct extent_buffer *leaf;
946 struct btrfs_key key;
947 struct btrfs_key found_key;
948 struct btrfs_qgroup *qgroup = NULL;
949 struct btrfs_trans_handle *trans = NULL;
950 struct ulist *ulist = NULL;
955 * We need to have subvol_sem write locked, to prevent races between
956 * concurrent tasks trying to enable quotas, because we will unlock
957 * and relock qgroup_ioctl_lock before setting fs_info->quota_root
958 * and before setting BTRFS_FS_QUOTA_ENABLED.
960 lockdep_assert_held_write(&fs_info->subvol_sem);
962 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
964 "qgroups are currently unsupported in extent tree v2");
968 mutex_lock(&fs_info->qgroup_ioctl_lock);
969 if (fs_info->quota_root)
972 ulist = ulist_alloc(GFP_KERNEL);
978 ret = btrfs_sysfs_add_qgroups(fs_info);
983 * Unlock qgroup_ioctl_lock before starting the transaction. This is to
984 * avoid lock acquisition inversion problems (reported by lockdep) between
985 * qgroup_ioctl_lock and the vfs freeze semaphores, acquired when we
986 * start a transaction.
987 * After we started the transaction lock qgroup_ioctl_lock again and
988 * check if someone else created the quota root in the meanwhile. If so,
989 * just return success and release the transaction handle.
991 * Also we don't need to worry about someone else calling
992 * btrfs_sysfs_add_qgroups() after we unlock and getting an error because
993 * that function returns 0 (success) when the sysfs entries already exist.
995 mutex_unlock(&fs_info->qgroup_ioctl_lock);
998 * 1 for quota root item
999 * 1 for BTRFS_QGROUP_STATUS item
1001 * Yet we also need 2*n items for a QGROUP_INFO/QGROUP_LIMIT items
1002 * per subvolume. However those are not currently reserved since it
1003 * would be a lot of overkill.
1005 trans = btrfs_start_transaction(tree_root, 2);
1007 mutex_lock(&fs_info->qgroup_ioctl_lock);
1008 if (IS_ERR(trans)) {
1009 ret = PTR_ERR(trans);
1014 if (fs_info->quota_root)
1017 fs_info->qgroup_ulist = ulist;
1021 * initially create the quota tree
1023 quota_root = btrfs_create_tree(trans, BTRFS_QUOTA_TREE_OBJECTID);
1024 if (IS_ERR(quota_root)) {
1025 ret = PTR_ERR(quota_root);
1026 btrfs_abort_transaction(trans, ret);
1030 path = btrfs_alloc_path();
1033 btrfs_abort_transaction(trans, ret);
1038 key.type = BTRFS_QGROUP_STATUS_KEY;
1041 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
1044 btrfs_abort_transaction(trans, ret);
1048 leaf = path->nodes[0];
1049 ptr = btrfs_item_ptr(leaf, path->slots[0],
1050 struct btrfs_qgroup_status_item);
1051 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
1052 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
1053 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
1054 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1055 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
1056 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
1058 btrfs_mark_buffer_dirty(leaf);
1061 key.type = BTRFS_ROOT_REF_KEY;
1064 btrfs_release_path(path);
1065 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
1069 btrfs_abort_transaction(trans, ret);
1074 slot = path->slots[0];
1075 leaf = path->nodes[0];
1076 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1078 if (found_key.type == BTRFS_ROOT_REF_KEY) {
1080 /* Release locks on tree_root before we access quota_root */
1081 btrfs_release_path(path);
1083 ret = add_qgroup_item(trans, quota_root,
1086 btrfs_abort_transaction(trans, ret);
1090 qgroup = add_qgroup_rb(fs_info, found_key.offset);
1091 if (IS_ERR(qgroup)) {
1092 ret = PTR_ERR(qgroup);
1093 btrfs_abort_transaction(trans, ret);
1096 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1098 btrfs_abort_transaction(trans, ret);
1101 ret = btrfs_search_slot_for_read(tree_root, &found_key,
1104 btrfs_abort_transaction(trans, ret);
1109 * Shouldn't happen, but in case it does we
1110 * don't need to do the btrfs_next_item, just
1116 ret = btrfs_next_item(tree_root, path);
1118 btrfs_abort_transaction(trans, ret);
1126 btrfs_release_path(path);
1127 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
1129 btrfs_abort_transaction(trans, ret);
1133 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
1134 if (IS_ERR(qgroup)) {
1135 ret = PTR_ERR(qgroup);
1136 btrfs_abort_transaction(trans, ret);
1139 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1141 btrfs_abort_transaction(trans, ret);
1145 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1147 * Commit the transaction while not holding qgroup_ioctl_lock, to avoid
1148 * a deadlock with tasks concurrently doing other qgroup operations, such
1149 * adding/removing qgroups or adding/deleting qgroup relations for example,
1150 * because all qgroup operations first start or join a transaction and then
1151 * lock the qgroup_ioctl_lock mutex.
1152 * We are safe from a concurrent task trying to enable quotas, by calling
1153 * this function, since we are serialized by fs_info->subvol_sem.
1155 ret = btrfs_commit_transaction(trans);
1157 mutex_lock(&fs_info->qgroup_ioctl_lock);
1162 * Set quota enabled flag after committing the transaction, to avoid
1163 * deadlocks on fs_info->qgroup_ioctl_lock with concurrent snapshot
1166 spin_lock(&fs_info->qgroup_lock);
1167 fs_info->quota_root = quota_root;
1168 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1169 spin_unlock(&fs_info->qgroup_lock);
1171 ret = qgroup_rescan_init(fs_info, 0, 1);
1173 qgroup_rescan_zero_tracking(fs_info);
1174 fs_info->qgroup_rescan_running = true;
1175 btrfs_queue_work(fs_info->qgroup_rescan_workers,
1176 &fs_info->qgroup_rescan_work);
1180 btrfs_free_path(path);
1183 btrfs_put_root(quota_root);
1186 ulist_free(fs_info->qgroup_ulist);
1187 fs_info->qgroup_ulist = NULL;
1188 btrfs_sysfs_del_qgroups(fs_info);
1190 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1192 btrfs_end_transaction(trans);
1194 ret = btrfs_end_transaction(trans);
1199 int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
1201 struct btrfs_root *quota_root;
1202 struct btrfs_trans_handle *trans = NULL;
1206 * We need to have subvol_sem write locked, to prevent races between
1207 * concurrent tasks trying to disable quotas, because we will unlock
1208 * and relock qgroup_ioctl_lock across BTRFS_FS_QUOTA_ENABLED changes.
1210 lockdep_assert_held_write(&fs_info->subvol_sem);
1212 mutex_lock(&fs_info->qgroup_ioctl_lock);
1213 if (!fs_info->quota_root)
1217 * Unlock the qgroup_ioctl_lock mutex before waiting for the rescan worker to
1218 * complete. Otherwise we can deadlock because btrfs_remove_qgroup() needs
1219 * to lock that mutex while holding a transaction handle and the rescan
1220 * worker needs to commit a transaction.
1222 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1225 * Request qgroup rescan worker to complete and wait for it. This wait
1226 * must be done before transaction start for quota disable since it may
1227 * deadlock with transaction by the qgroup rescan worker.
1229 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1230 btrfs_qgroup_wait_for_completion(fs_info, false);
1233 * 1 For the root item
1235 * We should also reserve enough items for the quota tree deletion in
1236 * btrfs_clean_quota_tree but this is not done.
1238 * Also, we must always start a transaction without holding the mutex
1239 * qgroup_ioctl_lock, see btrfs_quota_enable().
1241 trans = btrfs_start_transaction(fs_info->tree_root, 1);
1243 mutex_lock(&fs_info->qgroup_ioctl_lock);
1244 if (IS_ERR(trans)) {
1245 ret = PTR_ERR(trans);
1247 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1251 if (!fs_info->quota_root)
1254 spin_lock(&fs_info->qgroup_lock);
1255 quota_root = fs_info->quota_root;
1256 fs_info->quota_root = NULL;
1257 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1258 spin_unlock(&fs_info->qgroup_lock);
1260 btrfs_free_qgroup_config(fs_info);
1262 ret = btrfs_clean_quota_tree(trans, quota_root);
1264 btrfs_abort_transaction(trans, ret);
1268 ret = btrfs_del_root(trans, "a_root->root_key);
1270 btrfs_abort_transaction(trans, ret);
1274 list_del("a_root->dirty_list);
1276 btrfs_tree_lock(quota_root->node);
1277 btrfs_clean_tree_block(quota_root->node);
1278 btrfs_tree_unlock(quota_root->node);
1279 btrfs_free_tree_block(trans, btrfs_root_id(quota_root),
1280 quota_root->node, 0, 1);
1282 btrfs_put_root(quota_root);
1285 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1287 btrfs_end_transaction(trans);
1289 ret = btrfs_end_transaction(trans);
1294 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1295 struct btrfs_qgroup *qgroup)
1297 if (list_empty(&qgroup->dirty))
1298 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1302 * The easy accounting, we're updating qgroup relationship whose child qgroup
1303 * only has exclusive extents.
1305 * In this case, all exclusive extents will also be exclusive for parent, so
1306 * excl/rfer just get added/removed.
1308 * So is qgroup reservation space, which should also be added/removed to
1310 * Or when child tries to release reservation space, parent will underflow its
1311 * reservation (for relationship adding case).
1313 * Caller should hold fs_info->qgroup_lock.
1315 static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1316 struct ulist *tmp, u64 ref_root,
1317 struct btrfs_qgroup *src, int sign)
1319 struct btrfs_qgroup *qgroup;
1320 struct btrfs_qgroup_list *glist;
1321 struct ulist_node *unode;
1322 struct ulist_iterator uiter;
1323 u64 num_bytes = src->excl;
1326 qgroup = find_qgroup_rb(fs_info, ref_root);
1330 qgroup->rfer += sign * num_bytes;
1331 qgroup->rfer_cmpr += sign * num_bytes;
1333 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1334 qgroup->excl += sign * num_bytes;
1335 qgroup->excl_cmpr += sign * num_bytes;
1338 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1340 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1342 qgroup_dirty(fs_info, qgroup);
1344 /* Get all of the parent groups that contain this qgroup */
1345 list_for_each_entry(glist, &qgroup->groups, next_group) {
1346 ret = ulist_add(tmp, glist->group->qgroupid,
1347 qgroup_to_aux(glist->group), GFP_ATOMIC);
1352 /* Iterate all of the parents and adjust their reference counts */
1353 ULIST_ITER_INIT(&uiter);
1354 while ((unode = ulist_next(tmp, &uiter))) {
1355 qgroup = unode_aux_to_qgroup(unode);
1356 qgroup->rfer += sign * num_bytes;
1357 qgroup->rfer_cmpr += sign * num_bytes;
1358 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1359 qgroup->excl += sign * num_bytes;
1361 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1363 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1364 qgroup->excl_cmpr += sign * num_bytes;
1365 qgroup_dirty(fs_info, qgroup);
1367 /* Add any parents of the parents */
1368 list_for_each_entry(glist, &qgroup->groups, next_group) {
1369 ret = ulist_add(tmp, glist->group->qgroupid,
1370 qgroup_to_aux(glist->group), GFP_ATOMIC);
1382 * Quick path for updating qgroup with only excl refs.
1384 * In that case, just update all parent will be enough.
1385 * Or we needs to do a full rescan.
1386 * Caller should also hold fs_info->qgroup_lock.
1388 * Return 0 for quick update, return >0 for need to full rescan
1389 * and mark INCONSISTENT flag.
1390 * Return < 0 for other error.
1392 static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1393 struct ulist *tmp, u64 src, u64 dst,
1396 struct btrfs_qgroup *qgroup;
1400 qgroup = find_qgroup_rb(fs_info, src);
1403 if (qgroup->excl == qgroup->rfer) {
1405 err = __qgroup_excl_accounting(fs_info, tmp, dst,
1414 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1418 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1421 struct btrfs_fs_info *fs_info = trans->fs_info;
1422 struct btrfs_qgroup *parent;
1423 struct btrfs_qgroup *member;
1424 struct btrfs_qgroup_list *list;
1426 unsigned int nofs_flag;
1429 /* Check the level of src and dst first */
1430 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1433 /* We hold a transaction handle open, must do a NOFS allocation. */
1434 nofs_flag = memalloc_nofs_save();
1435 tmp = ulist_alloc(GFP_KERNEL);
1436 memalloc_nofs_restore(nofs_flag);
1440 mutex_lock(&fs_info->qgroup_ioctl_lock);
1441 if (!fs_info->quota_root) {
1445 member = find_qgroup_rb(fs_info, src);
1446 parent = find_qgroup_rb(fs_info, dst);
1447 if (!member || !parent) {
1452 /* check if such qgroup relation exist firstly */
1453 list_for_each_entry(list, &member->groups, next_group) {
1454 if (list->group == parent) {
1460 ret = add_qgroup_relation_item(trans, src, dst);
1464 ret = add_qgroup_relation_item(trans, dst, src);
1466 del_qgroup_relation_item(trans, src, dst);
1470 spin_lock(&fs_info->qgroup_lock);
1471 ret = __add_relation_rb(member, parent);
1473 spin_unlock(&fs_info->qgroup_lock);
1476 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
1477 spin_unlock(&fs_info->qgroup_lock);
1479 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1484 static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1487 struct btrfs_fs_info *fs_info = trans->fs_info;
1488 struct btrfs_qgroup *parent;
1489 struct btrfs_qgroup *member;
1490 struct btrfs_qgroup_list *list;
1493 unsigned int nofs_flag;
1497 /* We hold a transaction handle open, must do a NOFS allocation. */
1498 nofs_flag = memalloc_nofs_save();
1499 tmp = ulist_alloc(GFP_KERNEL);
1500 memalloc_nofs_restore(nofs_flag);
1504 if (!fs_info->quota_root) {
1509 member = find_qgroup_rb(fs_info, src);
1510 parent = find_qgroup_rb(fs_info, dst);
1512 * The parent/member pair doesn't exist, then try to delete the dead
1513 * relation items only.
1515 if (!member || !parent)
1518 /* check if such qgroup relation exist firstly */
1519 list_for_each_entry(list, &member->groups, next_group) {
1520 if (list->group == parent) {
1527 ret = del_qgroup_relation_item(trans, src, dst);
1528 if (ret < 0 && ret != -ENOENT)
1530 ret2 = del_qgroup_relation_item(trans, dst, src);
1531 if (ret2 < 0 && ret2 != -ENOENT)
1534 /* At least one deletion succeeded, return 0 */
1539 spin_lock(&fs_info->qgroup_lock);
1540 del_relation_rb(fs_info, src, dst);
1541 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
1542 spin_unlock(&fs_info->qgroup_lock);
1549 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1552 struct btrfs_fs_info *fs_info = trans->fs_info;
1555 mutex_lock(&fs_info->qgroup_ioctl_lock);
1556 ret = __del_qgroup_relation(trans, src, dst);
1557 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1562 int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
1564 struct btrfs_fs_info *fs_info = trans->fs_info;
1565 struct btrfs_root *quota_root;
1566 struct btrfs_qgroup *qgroup;
1569 mutex_lock(&fs_info->qgroup_ioctl_lock);
1570 if (!fs_info->quota_root) {
1574 quota_root = fs_info->quota_root;
1575 qgroup = find_qgroup_rb(fs_info, qgroupid);
1581 ret = add_qgroup_item(trans, quota_root, qgroupid);
1585 spin_lock(&fs_info->qgroup_lock);
1586 qgroup = add_qgroup_rb(fs_info, qgroupid);
1587 spin_unlock(&fs_info->qgroup_lock);
1589 if (IS_ERR(qgroup)) {
1590 ret = PTR_ERR(qgroup);
1593 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1595 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1599 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
1601 struct btrfs_fs_info *fs_info = trans->fs_info;
1602 struct btrfs_qgroup *qgroup;
1603 struct btrfs_qgroup_list *list;
1606 mutex_lock(&fs_info->qgroup_ioctl_lock);
1607 if (!fs_info->quota_root) {
1612 qgroup = find_qgroup_rb(fs_info, qgroupid);
1618 /* Check if there are no children of this qgroup */
1619 if (!list_empty(&qgroup->members)) {
1624 ret = del_qgroup_item(trans, qgroupid);
1625 if (ret && ret != -ENOENT)
1628 while (!list_empty(&qgroup->groups)) {
1629 list = list_first_entry(&qgroup->groups,
1630 struct btrfs_qgroup_list, next_group);
1631 ret = __del_qgroup_relation(trans, qgroupid,
1632 list->group->qgroupid);
1637 spin_lock(&fs_info->qgroup_lock);
1638 del_qgroup_rb(fs_info, qgroupid);
1639 spin_unlock(&fs_info->qgroup_lock);
1642 * Remove the qgroup from sysfs now without holding the qgroup_lock
1643 * spinlock, since the sysfs_remove_group() function needs to take
1644 * the mutex kernfs_mutex through kernfs_remove_by_name_ns().
1646 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
1649 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1653 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,
1654 struct btrfs_qgroup_limit *limit)
1656 struct btrfs_fs_info *fs_info = trans->fs_info;
1657 struct btrfs_qgroup *qgroup;
1659 /* Sometimes we would want to clear the limit on this qgroup.
1660 * To meet this requirement, we treat the -1 as a special value
1661 * which tell kernel to clear the limit on this qgroup.
1663 const u64 CLEAR_VALUE = -1;
1665 mutex_lock(&fs_info->qgroup_ioctl_lock);
1666 if (!fs_info->quota_root) {
1671 qgroup = find_qgroup_rb(fs_info, qgroupid);
1677 spin_lock(&fs_info->qgroup_lock);
1678 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1679 if (limit->max_rfer == CLEAR_VALUE) {
1680 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1681 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1682 qgroup->max_rfer = 0;
1684 qgroup->max_rfer = limit->max_rfer;
1687 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1688 if (limit->max_excl == CLEAR_VALUE) {
1689 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1690 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1691 qgroup->max_excl = 0;
1693 qgroup->max_excl = limit->max_excl;
1696 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1697 if (limit->rsv_rfer == CLEAR_VALUE) {
1698 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1699 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1700 qgroup->rsv_rfer = 0;
1702 qgroup->rsv_rfer = limit->rsv_rfer;
1705 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1706 if (limit->rsv_excl == CLEAR_VALUE) {
1707 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1708 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1709 qgroup->rsv_excl = 0;
1711 qgroup->rsv_excl = limit->rsv_excl;
1714 qgroup->lim_flags |= limit->flags;
1716 spin_unlock(&fs_info->qgroup_lock);
1718 ret = update_qgroup_limit_item(trans, qgroup);
1720 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1721 btrfs_info(fs_info, "unable to update quota limit for %llu",
1726 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1730 int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
1731 struct btrfs_delayed_ref_root *delayed_refs,
1732 struct btrfs_qgroup_extent_record *record)
1734 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1735 struct rb_node *parent_node = NULL;
1736 struct btrfs_qgroup_extent_record *entry;
1737 u64 bytenr = record->bytenr;
1739 lockdep_assert_held(&delayed_refs->lock);
1740 trace_btrfs_qgroup_trace_extent(fs_info, record);
1744 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1746 if (bytenr < entry->bytenr) {
1748 } else if (bytenr > entry->bytenr) {
1749 p = &(*p)->rb_right;
1751 if (record->data_rsv && !entry->data_rsv) {
1752 entry->data_rsv = record->data_rsv;
1753 entry->data_rsv_refroot =
1754 record->data_rsv_refroot;
1760 rb_link_node(&record->node, parent_node, p);
1761 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
1765 int btrfs_qgroup_trace_extent_post(struct btrfs_trans_handle *trans,
1766 struct btrfs_qgroup_extent_record *qrecord)
1768 struct ulist *old_root;
1769 u64 bytenr = qrecord->bytenr;
1773 * We are always called in a context where we are already holding a
1774 * transaction handle. Often we are called when adding a data delayed
1775 * reference from btrfs_truncate_inode_items() (truncating or unlinking),
1776 * in which case we will be holding a write lock on extent buffer from a
1777 * subvolume tree. In this case we can't allow btrfs_find_all_roots() to
1778 * acquire fs_info->commit_root_sem, because that is a higher level lock
1779 * that must be acquired before locking any extent buffers.
1781 * So we want btrfs_find_all_roots() to not acquire the commit_root_sem
1782 * but we can't pass it a non-NULL transaction handle, because otherwise
1783 * it would not use commit roots and would lock extent buffers, causing
1784 * a deadlock if it ends up trying to read lock the same extent buffer
1785 * that was previously write locked at btrfs_truncate_inode_items().
1787 * So pass a NULL transaction handle to btrfs_find_all_roots() and
1788 * explicitly tell it to not acquire the commit_root_sem - if we are
1789 * holding a transaction handle we don't need its protection.
1791 ASSERT(trans != NULL);
1793 ret = btrfs_find_all_roots(NULL, trans->fs_info, bytenr, 0, &old_root,
1796 trans->fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1797 btrfs_warn(trans->fs_info,
1798 "error accounting new delayed refs extent (err code: %d), quota inconsistent",
1804 * Here we don't need to get the lock of
1805 * trans->transaction->delayed_refs, since inserted qrecord won't
1806 * be deleted, only qrecord->node may be modified (new qrecord insert)
1808 * So modifying qrecord->old_roots is safe here
1810 qrecord->old_roots = old_root;
1814 int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
1815 u64 num_bytes, gfp_t gfp_flag)
1817 struct btrfs_fs_info *fs_info = trans->fs_info;
1818 struct btrfs_qgroup_extent_record *record;
1819 struct btrfs_delayed_ref_root *delayed_refs;
1822 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1823 || bytenr == 0 || num_bytes == 0)
1825 record = kzalloc(sizeof(*record), gfp_flag);
1829 delayed_refs = &trans->transaction->delayed_refs;
1830 record->bytenr = bytenr;
1831 record->num_bytes = num_bytes;
1832 record->old_roots = NULL;
1834 spin_lock(&delayed_refs->lock);
1835 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
1836 spin_unlock(&delayed_refs->lock);
1841 return btrfs_qgroup_trace_extent_post(trans, record);
1844 int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
1845 struct extent_buffer *eb)
1847 struct btrfs_fs_info *fs_info = trans->fs_info;
1848 int nr = btrfs_header_nritems(eb);
1849 int i, extent_type, ret;
1850 struct btrfs_key key;
1851 struct btrfs_file_extent_item *fi;
1852 u64 bytenr, num_bytes;
1854 /* We can be called directly from walk_up_proc() */
1855 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1858 for (i = 0; i < nr; i++) {
1859 btrfs_item_key_to_cpu(eb, &key, i);
1861 if (key.type != BTRFS_EXTENT_DATA_KEY)
1864 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1865 /* filter out non qgroup-accountable extents */
1866 extent_type = btrfs_file_extent_type(eb, fi);
1868 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1871 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1875 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1877 ret = btrfs_qgroup_trace_extent(trans, bytenr, num_bytes,
1887 * Walk up the tree from the bottom, freeing leaves and any interior
1888 * nodes which have had all slots visited. If a node (leaf or
1889 * interior) is freed, the node above it will have it's slot
1890 * incremented. The root node will never be freed.
1892 * At the end of this function, we should have a path which has all
1893 * slots incremented to the next position for a search. If we need to
1894 * read a new node it will be NULL and the node above it will have the
1895 * correct slot selected for a later read.
1897 * If we increment the root nodes slot counter past the number of
1898 * elements, 1 is returned to signal completion of the search.
1900 static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
1904 struct extent_buffer *eb;
1906 if (root_level == 0)
1909 while (level <= root_level) {
1910 eb = path->nodes[level];
1911 nr = btrfs_header_nritems(eb);
1912 path->slots[level]++;
1913 slot = path->slots[level];
1914 if (slot >= nr || level == 0) {
1916 * Don't free the root - we will detect this
1917 * condition after our loop and return a
1918 * positive value for caller to stop walking the tree.
1920 if (level != root_level) {
1921 btrfs_tree_unlock_rw(eb, path->locks[level]);
1922 path->locks[level] = 0;
1924 free_extent_buffer(eb);
1925 path->nodes[level] = NULL;
1926 path->slots[level] = 0;
1930 * We have a valid slot to walk back down
1931 * from. Stop here so caller can process these
1940 eb = path->nodes[root_level];
1941 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1948 * Helper function to trace a subtree tree block swap.
1950 * The swap will happen in highest tree block, but there may be a lot of
1951 * tree blocks involved.
1954 * OO = Old tree blocks
1955 * NN = New tree blocks allocated during balance
1957 * File tree (257) Reloc tree for 257
1960 * L1 OO OO (a) OO NN (a)
1962 * L0 OO OO OO OO OO OO NN NN
1965 * When calling qgroup_trace_extent_swap(), we will pass:
1967 * @dst_path = [ nodes[1] = NN(a), nodes[0] = NN(c) ]
1971 * In that case, qgroup_trace_extent_swap() will search from OO(a) to
1972 * reach OO(c), then mark both OO(c) and NN(c) as qgroup dirty.
1974 * The main work of qgroup_trace_extent_swap() can be split into 3 parts:
1976 * 1) Tree search from @src_eb
1977 * It should acts as a simplified btrfs_search_slot().
1978 * The key for search can be extracted from @dst_path->nodes[dst_level]
1981 * 2) Mark the final tree blocks in @src_path and @dst_path qgroup dirty
1982 * NOTE: In above case, OO(a) and NN(a) won't be marked qgroup dirty.
1983 * They should be marked during previous (@dst_level = 1) iteration.
1985 * 3) Mark file extents in leaves dirty
1986 * We don't have good way to pick out new file extents only.
1987 * So we still follow the old method by scanning all file extents in
1990 * This function can free us from keeping two paths, thus later we only need
1991 * to care about how to iterate all new tree blocks in reloc tree.
1993 static int qgroup_trace_extent_swap(struct btrfs_trans_handle* trans,
1994 struct extent_buffer *src_eb,
1995 struct btrfs_path *dst_path,
1996 int dst_level, int root_level,
1999 struct btrfs_key key;
2000 struct btrfs_path *src_path;
2001 struct btrfs_fs_info *fs_info = trans->fs_info;
2002 u32 nodesize = fs_info->nodesize;
2003 int cur_level = root_level;
2006 BUG_ON(dst_level > root_level);
2007 /* Level mismatch */
2008 if (btrfs_header_level(src_eb) != root_level)
2011 src_path = btrfs_alloc_path();
2018 btrfs_node_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
2020 btrfs_item_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
2023 atomic_inc(&src_eb->refs);
2024 src_path->nodes[root_level] = src_eb;
2025 src_path->slots[root_level] = dst_path->slots[root_level];
2026 src_path->locks[root_level] = 0;
2028 /* A simplified version of btrfs_search_slot() */
2029 while (cur_level >= dst_level) {
2030 struct btrfs_key src_key;
2031 struct btrfs_key dst_key;
2033 if (src_path->nodes[cur_level] == NULL) {
2034 struct extent_buffer *eb;
2037 eb = src_path->nodes[cur_level + 1];
2038 parent_slot = src_path->slots[cur_level + 1];
2040 eb = btrfs_read_node_slot(eb, parent_slot);
2046 src_path->nodes[cur_level] = eb;
2048 btrfs_tree_read_lock(eb);
2049 src_path->locks[cur_level] = BTRFS_READ_LOCK;
2052 src_path->slots[cur_level] = dst_path->slots[cur_level];
2054 btrfs_node_key_to_cpu(dst_path->nodes[cur_level],
2055 &dst_key, dst_path->slots[cur_level]);
2056 btrfs_node_key_to_cpu(src_path->nodes[cur_level],
2057 &src_key, src_path->slots[cur_level]);
2059 btrfs_item_key_to_cpu(dst_path->nodes[cur_level],
2060 &dst_key, dst_path->slots[cur_level]);
2061 btrfs_item_key_to_cpu(src_path->nodes[cur_level],
2062 &src_key, src_path->slots[cur_level]);
2064 /* Content mismatch, something went wrong */
2065 if (btrfs_comp_cpu_keys(&dst_key, &src_key)) {
2073 * Now both @dst_path and @src_path have been populated, record the tree
2074 * blocks for qgroup accounting.
2076 ret = btrfs_qgroup_trace_extent(trans, src_path->nodes[dst_level]->start,
2077 nodesize, GFP_NOFS);
2080 ret = btrfs_qgroup_trace_extent(trans,
2081 dst_path->nodes[dst_level]->start,
2082 nodesize, GFP_NOFS);
2086 /* Record leaf file extents */
2087 if (dst_level == 0 && trace_leaf) {
2088 ret = btrfs_qgroup_trace_leaf_items(trans, src_path->nodes[0]);
2091 ret = btrfs_qgroup_trace_leaf_items(trans, dst_path->nodes[0]);
2094 btrfs_free_path(src_path);
2099 * Helper function to do recursive generation-aware depth-first search, to
2100 * locate all new tree blocks in a subtree of reloc tree.
2102 * E.g. (OO = Old tree blocks, NN = New tree blocks, whose gen == last_snapshot)
2111 * @dst_path = [ nodes[1] = NN(b), nodes[0] = NULL ],
2115 * We will iterate through tree blocks NN(b), NN(d) and info qgroup to trace
2116 * above tree blocks along with their counter parts in file tree.
2117 * While during search, old tree blocks OO(c) will be skipped as tree block swap
2118 * won't affect OO(c).
2120 static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans,
2121 struct extent_buffer *src_eb,
2122 struct btrfs_path *dst_path,
2123 int cur_level, int root_level,
2124 u64 last_snapshot, bool trace_leaf)
2126 struct btrfs_fs_info *fs_info = trans->fs_info;
2127 struct extent_buffer *eb;
2128 bool need_cleanup = false;
2132 /* Level sanity check */
2133 if (cur_level < 0 || cur_level >= BTRFS_MAX_LEVEL - 1 ||
2134 root_level < 0 || root_level >= BTRFS_MAX_LEVEL - 1 ||
2135 root_level < cur_level) {
2136 btrfs_err_rl(fs_info,
2137 "%s: bad levels, cur_level=%d root_level=%d",
2138 __func__, cur_level, root_level);
2142 /* Read the tree block if needed */
2143 if (dst_path->nodes[cur_level] == NULL) {
2148 * dst_path->nodes[root_level] must be initialized before
2149 * calling this function.
2151 if (cur_level == root_level) {
2152 btrfs_err_rl(fs_info,
2153 "%s: dst_path->nodes[%d] not initialized, root_level=%d cur_level=%d",
2154 __func__, root_level, root_level, cur_level);
2159 * We need to get child blockptr/gen from parent before we can
2162 eb = dst_path->nodes[cur_level + 1];
2163 parent_slot = dst_path->slots[cur_level + 1];
2164 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
2166 /* This node is old, no need to trace */
2167 if (child_gen < last_snapshot)
2170 eb = btrfs_read_node_slot(eb, parent_slot);
2176 dst_path->nodes[cur_level] = eb;
2177 dst_path->slots[cur_level] = 0;
2179 btrfs_tree_read_lock(eb);
2180 dst_path->locks[cur_level] = BTRFS_READ_LOCK;
2181 need_cleanup = true;
2184 /* Now record this tree block and its counter part for qgroups */
2185 ret = qgroup_trace_extent_swap(trans, src_eb, dst_path, cur_level,
2186 root_level, trace_leaf);
2190 eb = dst_path->nodes[cur_level];
2192 if (cur_level > 0) {
2193 /* Iterate all child tree blocks */
2194 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2195 /* Skip old tree blocks as they won't be swapped */
2196 if (btrfs_node_ptr_generation(eb, i) < last_snapshot)
2198 dst_path->slots[cur_level] = i;
2200 /* Recursive call (at most 7 times) */
2201 ret = qgroup_trace_new_subtree_blocks(trans, src_eb,
2202 dst_path, cur_level - 1, root_level,
2203 last_snapshot, trace_leaf);
2212 btrfs_tree_unlock_rw(dst_path->nodes[cur_level],
2213 dst_path->locks[cur_level]);
2214 free_extent_buffer(dst_path->nodes[cur_level]);
2215 dst_path->nodes[cur_level] = NULL;
2216 dst_path->slots[cur_level] = 0;
2217 dst_path->locks[cur_level] = 0;
2223 static int qgroup_trace_subtree_swap(struct btrfs_trans_handle *trans,
2224 struct extent_buffer *src_eb,
2225 struct extent_buffer *dst_eb,
2226 u64 last_snapshot, bool trace_leaf)
2228 struct btrfs_fs_info *fs_info = trans->fs_info;
2229 struct btrfs_path *dst_path = NULL;
2233 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2236 /* Wrong parameter order */
2237 if (btrfs_header_generation(src_eb) > btrfs_header_generation(dst_eb)) {
2238 btrfs_err_rl(fs_info,
2239 "%s: bad parameter order, src_gen=%llu dst_gen=%llu", __func__,
2240 btrfs_header_generation(src_eb),
2241 btrfs_header_generation(dst_eb));
2245 if (!extent_buffer_uptodate(src_eb) || !extent_buffer_uptodate(dst_eb)) {
2250 level = btrfs_header_level(dst_eb);
2251 dst_path = btrfs_alloc_path();
2257 atomic_inc(&dst_eb->refs);
2258 dst_path->nodes[level] = dst_eb;
2259 dst_path->slots[level] = 0;
2260 dst_path->locks[level] = 0;
2262 /* Do the generation aware breadth-first search */
2263 ret = qgroup_trace_new_subtree_blocks(trans, src_eb, dst_path, level,
2264 level, last_snapshot, trace_leaf);
2270 btrfs_free_path(dst_path);
2272 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2276 int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
2277 struct extent_buffer *root_eb,
2278 u64 root_gen, int root_level)
2280 struct btrfs_fs_info *fs_info = trans->fs_info;
2283 struct extent_buffer *eb = root_eb;
2284 struct btrfs_path *path = NULL;
2286 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
2287 BUG_ON(root_eb == NULL);
2289 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2292 if (!extent_buffer_uptodate(root_eb)) {
2293 ret = btrfs_read_extent_buffer(root_eb, root_gen, root_level, NULL);
2298 if (root_level == 0) {
2299 ret = btrfs_qgroup_trace_leaf_items(trans, root_eb);
2303 path = btrfs_alloc_path();
2308 * Walk down the tree. Missing extent blocks are filled in as
2309 * we go. Metadata is accounted every time we read a new
2312 * When we reach a leaf, we account for file extent items in it,
2313 * walk back up the tree (adjusting slot pointers as we go)
2314 * and restart the search process.
2316 atomic_inc(&root_eb->refs); /* For path */
2317 path->nodes[root_level] = root_eb;
2318 path->slots[root_level] = 0;
2319 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
2322 while (level >= 0) {
2323 if (path->nodes[level] == NULL) {
2328 * We need to get child blockptr from parent before we
2331 eb = path->nodes[level + 1];
2332 parent_slot = path->slots[level + 1];
2333 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
2335 eb = btrfs_read_node_slot(eb, parent_slot);
2341 path->nodes[level] = eb;
2342 path->slots[level] = 0;
2344 btrfs_tree_read_lock(eb);
2345 path->locks[level] = BTRFS_READ_LOCK;
2347 ret = btrfs_qgroup_trace_extent(trans, child_bytenr,
2355 ret = btrfs_qgroup_trace_leaf_items(trans,
2356 path->nodes[level]);
2360 /* Nonzero return here means we completed our search */
2361 ret = adjust_slots_upwards(path, root_level);
2365 /* Restart search with new slots */
2374 btrfs_free_path(path);
2379 #define UPDATE_NEW 0
2380 #define UPDATE_OLD 1
2382 * Walk all of the roots that points to the bytenr and adjust their refcnts.
2384 static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
2385 struct ulist *roots, struct ulist *tmp,
2386 struct ulist *qgroups, u64 seq, int update_old)
2388 struct ulist_node *unode;
2389 struct ulist_iterator uiter;
2390 struct ulist_node *tmp_unode;
2391 struct ulist_iterator tmp_uiter;
2392 struct btrfs_qgroup *qg;
2397 ULIST_ITER_INIT(&uiter);
2398 while ((unode = ulist_next(roots, &uiter))) {
2399 qg = find_qgroup_rb(fs_info, unode->val);
2404 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
2408 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
2411 ULIST_ITER_INIT(&tmp_uiter);
2412 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
2413 struct btrfs_qgroup_list *glist;
2415 qg = unode_aux_to_qgroup(tmp_unode);
2417 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
2419 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
2420 list_for_each_entry(glist, &qg->groups, next_group) {
2421 ret = ulist_add(qgroups, glist->group->qgroupid,
2422 qgroup_to_aux(glist->group),
2426 ret = ulist_add(tmp, glist->group->qgroupid,
2427 qgroup_to_aux(glist->group),
2438 * Update qgroup rfer/excl counters.
2439 * Rfer update is easy, codes can explain themselves.
2441 * Excl update is tricky, the update is split into 2 parts.
2442 * Part 1: Possible exclusive <-> sharing detect:
2444 * -------------------------------------
2446 * -------------------------------------
2448 * -------------------------------------
2451 * A: cur_old_roots < nr_old_roots (not exclusive before)
2452 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
2453 * B: cur_new_roots < nr_new_roots (not exclusive now)
2454 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
2457 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
2458 * *: Definitely not changed. **: Possible unchanged.
2460 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
2462 * To make the logic clear, we first use condition A and B to split
2463 * combination into 4 results.
2465 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
2466 * only on variant maybe 0.
2468 * Lastly, check result **, since there are 2 variants maybe 0, split them
2470 * But this time we don't need to consider other things, the codes and logic
2471 * is easy to understand now.
2473 static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
2474 struct ulist *qgroups,
2477 u64 num_bytes, u64 seq)
2479 struct ulist_node *unode;
2480 struct ulist_iterator uiter;
2481 struct btrfs_qgroup *qg;
2482 u64 cur_new_count, cur_old_count;
2484 ULIST_ITER_INIT(&uiter);
2485 while ((unode = ulist_next(qgroups, &uiter))) {
2488 qg = unode_aux_to_qgroup(unode);
2489 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
2490 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
2492 trace_qgroup_update_counters(fs_info, qg, cur_old_count,
2495 /* Rfer update part */
2496 if (cur_old_count == 0 && cur_new_count > 0) {
2497 qg->rfer += num_bytes;
2498 qg->rfer_cmpr += num_bytes;
2501 if (cur_old_count > 0 && cur_new_count == 0) {
2502 qg->rfer -= num_bytes;
2503 qg->rfer_cmpr -= num_bytes;
2507 /* Excl update part */
2508 /* Exclusive/none -> shared case */
2509 if (cur_old_count == nr_old_roots &&
2510 cur_new_count < nr_new_roots) {
2511 /* Exclusive -> shared */
2512 if (cur_old_count != 0) {
2513 qg->excl -= num_bytes;
2514 qg->excl_cmpr -= num_bytes;
2519 /* Shared -> exclusive/none case */
2520 if (cur_old_count < nr_old_roots &&
2521 cur_new_count == nr_new_roots) {
2522 /* Shared->exclusive */
2523 if (cur_new_count != 0) {
2524 qg->excl += num_bytes;
2525 qg->excl_cmpr += num_bytes;
2530 /* Exclusive/none -> exclusive/none case */
2531 if (cur_old_count == nr_old_roots &&
2532 cur_new_count == nr_new_roots) {
2533 if (cur_old_count == 0) {
2534 /* None -> exclusive/none */
2536 if (cur_new_count != 0) {
2537 /* None -> exclusive */
2538 qg->excl += num_bytes;
2539 qg->excl_cmpr += num_bytes;
2542 /* None -> none, nothing changed */
2544 /* Exclusive -> exclusive/none */
2546 if (cur_new_count == 0) {
2547 /* Exclusive -> none */
2548 qg->excl -= num_bytes;
2549 qg->excl_cmpr -= num_bytes;
2552 /* Exclusive -> exclusive, nothing changed */
2557 qgroup_dirty(fs_info, qg);
2563 * Check if the @roots potentially is a list of fs tree roots
2565 * Return 0 for definitely not a fs/subvol tree roots ulist
2566 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
2569 static int maybe_fs_roots(struct ulist *roots)
2571 struct ulist_node *unode;
2572 struct ulist_iterator uiter;
2574 /* Empty one, still possible for fs roots */
2575 if (!roots || roots->nnodes == 0)
2578 ULIST_ITER_INIT(&uiter);
2579 unode = ulist_next(roots, &uiter);
2584 * If it contains fs tree roots, then it must belong to fs/subvol
2586 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
2588 return is_fstree(unode->val);
2591 int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr,
2592 u64 num_bytes, struct ulist *old_roots,
2593 struct ulist *new_roots)
2595 struct btrfs_fs_info *fs_info = trans->fs_info;
2596 struct ulist *qgroups = NULL;
2597 struct ulist *tmp = NULL;
2599 u64 nr_new_roots = 0;
2600 u64 nr_old_roots = 0;
2604 * If quotas get disabled meanwhile, the resources need to be freed and
2605 * we can't just exit here.
2607 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2611 if (!maybe_fs_roots(new_roots))
2613 nr_new_roots = new_roots->nnodes;
2616 if (!maybe_fs_roots(old_roots))
2618 nr_old_roots = old_roots->nnodes;
2621 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
2622 if (nr_old_roots == 0 && nr_new_roots == 0)
2625 BUG_ON(!fs_info->quota_root);
2627 trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
2628 num_bytes, nr_old_roots, nr_new_roots);
2630 qgroups = ulist_alloc(GFP_NOFS);
2635 tmp = ulist_alloc(GFP_NOFS);
2641 mutex_lock(&fs_info->qgroup_rescan_lock);
2642 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2643 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
2644 mutex_unlock(&fs_info->qgroup_rescan_lock);
2649 mutex_unlock(&fs_info->qgroup_rescan_lock);
2651 spin_lock(&fs_info->qgroup_lock);
2652 seq = fs_info->qgroup_seq;
2654 /* Update old refcnts using old_roots */
2655 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
2660 /* Update new refcnts using new_roots */
2661 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
2666 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
2670 * Bump qgroup_seq to avoid seq overlap
2672 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2674 spin_unlock(&fs_info->qgroup_lock);
2677 ulist_free(qgroups);
2678 ulist_free(old_roots);
2679 ulist_free(new_roots);
2683 int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
2685 struct btrfs_fs_info *fs_info = trans->fs_info;
2686 struct btrfs_qgroup_extent_record *record;
2687 struct btrfs_delayed_ref_root *delayed_refs;
2688 struct ulist *new_roots = NULL;
2689 struct rb_node *node;
2690 u64 num_dirty_extents = 0;
2694 delayed_refs = &trans->transaction->delayed_refs;
2695 qgroup_to_skip = delayed_refs->qgroup_to_skip;
2696 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2697 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2700 num_dirty_extents++;
2701 trace_btrfs_qgroup_account_extents(fs_info, record);
2705 * Old roots should be searched when inserting qgroup
2708 if (WARN_ON(!record->old_roots)) {
2709 /* Search commit root to find old_roots */
2710 ret = btrfs_find_all_roots(NULL, fs_info,
2712 &record->old_roots, false);
2717 /* Free the reserved data space */
2718 btrfs_qgroup_free_refroot(fs_info,
2719 record->data_rsv_refroot,
2721 BTRFS_QGROUP_RSV_DATA);
2723 * Use BTRFS_SEQ_LAST as time_seq to do special search,
2724 * which doesn't lock tree or delayed_refs and search
2725 * current root. It's safe inside commit_transaction().
2727 ret = btrfs_find_all_roots(trans, fs_info,
2728 record->bytenr, BTRFS_SEQ_LAST, &new_roots, false);
2731 if (qgroup_to_skip) {
2732 ulist_del(new_roots, qgroup_to_skip, 0);
2733 ulist_del(record->old_roots, qgroup_to_skip,
2736 ret = btrfs_qgroup_account_extent(trans, record->bytenr,
2740 record->old_roots = NULL;
2744 ulist_free(record->old_roots);
2745 ulist_free(new_roots);
2747 rb_erase(node, &delayed_refs->dirty_extent_root);
2751 trace_qgroup_num_dirty_extents(fs_info, trans->transid,
2757 * called from commit_transaction. Writes all changed qgroups to disk.
2759 int btrfs_run_qgroups(struct btrfs_trans_handle *trans)
2761 struct btrfs_fs_info *fs_info = trans->fs_info;
2764 if (!fs_info->quota_root)
2767 spin_lock(&fs_info->qgroup_lock);
2768 while (!list_empty(&fs_info->dirty_qgroups)) {
2769 struct btrfs_qgroup *qgroup;
2770 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2771 struct btrfs_qgroup, dirty);
2772 list_del_init(&qgroup->dirty);
2773 spin_unlock(&fs_info->qgroup_lock);
2774 ret = update_qgroup_info_item(trans, qgroup);
2776 fs_info->qgroup_flags |=
2777 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2778 ret = update_qgroup_limit_item(trans, qgroup);
2780 fs_info->qgroup_flags |=
2781 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2782 spin_lock(&fs_info->qgroup_lock);
2784 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2785 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2787 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2788 spin_unlock(&fs_info->qgroup_lock);
2790 ret = update_qgroup_status_item(trans);
2792 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2798 * Copy the accounting information between qgroups. This is necessary
2799 * when a snapshot or a subvolume is created. Throwing an error will
2800 * cause a transaction abort so we take extra care here to only error
2801 * when a readonly fs is a reasonable outcome.
2803 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
2804 u64 objectid, struct btrfs_qgroup_inherit *inherit)
2809 bool committing = false;
2810 struct btrfs_fs_info *fs_info = trans->fs_info;
2811 struct btrfs_root *quota_root;
2812 struct btrfs_qgroup *srcgroup;
2813 struct btrfs_qgroup *dstgroup;
2814 bool need_rescan = false;
2819 * There are only two callers of this function.
2821 * One in create_subvol() in the ioctl context, which needs to hold
2822 * the qgroup_ioctl_lock.
2824 * The other one in create_pending_snapshot() where no other qgroup
2825 * code can modify the fs as they all need to either start a new trans
2826 * or hold a trans handler, thus we don't need to hold
2827 * qgroup_ioctl_lock.
2828 * This would avoid long and complex lock chain and make lockdep happy.
2830 spin_lock(&fs_info->trans_lock);
2831 if (trans->transaction->state == TRANS_STATE_COMMIT_DOING)
2833 spin_unlock(&fs_info->trans_lock);
2836 mutex_lock(&fs_info->qgroup_ioctl_lock);
2837 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2840 quota_root = fs_info->quota_root;
2847 i_qgroups = (u64 *)(inherit + 1);
2848 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2849 2 * inherit->num_excl_copies;
2850 for (i = 0; i < nums; ++i) {
2851 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
2854 * Zero out invalid groups so we can ignore
2858 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2866 * create a tracking group for the subvol itself
2868 ret = add_qgroup_item(trans, quota_root, objectid);
2873 * add qgroup to all inherited groups
2876 i_qgroups = (u64 *)(inherit + 1);
2877 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2878 if (*i_qgroups == 0)
2880 ret = add_qgroup_relation_item(trans, objectid,
2882 if (ret && ret != -EEXIST)
2884 ret = add_qgroup_relation_item(trans, *i_qgroups,
2886 if (ret && ret != -EEXIST)
2893 spin_lock(&fs_info->qgroup_lock);
2895 dstgroup = add_qgroup_rb(fs_info, objectid);
2896 if (IS_ERR(dstgroup)) {
2897 ret = PTR_ERR(dstgroup);
2901 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
2902 dstgroup->lim_flags = inherit->lim.flags;
2903 dstgroup->max_rfer = inherit->lim.max_rfer;
2904 dstgroup->max_excl = inherit->lim.max_excl;
2905 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2906 dstgroup->rsv_excl = inherit->lim.rsv_excl;
2908 ret = update_qgroup_limit_item(trans, dstgroup);
2910 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2912 "unable to update quota limit for %llu",
2913 dstgroup->qgroupid);
2919 srcgroup = find_qgroup_rb(fs_info, srcid);
2924 * We call inherit after we clone the root in order to make sure
2925 * our counts don't go crazy, so at this point the only
2926 * difference between the two roots should be the root node.
2928 level_size = fs_info->nodesize;
2929 dstgroup->rfer = srcgroup->rfer;
2930 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2931 dstgroup->excl = level_size;
2932 dstgroup->excl_cmpr = level_size;
2933 srcgroup->excl = level_size;
2934 srcgroup->excl_cmpr = level_size;
2936 /* inherit the limit info */
2937 dstgroup->lim_flags = srcgroup->lim_flags;
2938 dstgroup->max_rfer = srcgroup->max_rfer;
2939 dstgroup->max_excl = srcgroup->max_excl;
2940 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2941 dstgroup->rsv_excl = srcgroup->rsv_excl;
2943 qgroup_dirty(fs_info, dstgroup);
2944 qgroup_dirty(fs_info, srcgroup);
2950 i_qgroups = (u64 *)(inherit + 1);
2951 for (i = 0; i < inherit->num_qgroups; ++i) {
2953 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
2960 * If we're doing a snapshot, and adding the snapshot to a new
2961 * qgroup, the numbers are guaranteed to be incorrect.
2967 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
2968 struct btrfs_qgroup *src;
2969 struct btrfs_qgroup *dst;
2971 if (!i_qgroups[0] || !i_qgroups[1])
2974 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2975 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2982 dst->rfer = src->rfer - level_size;
2983 dst->rfer_cmpr = src->rfer_cmpr - level_size;
2985 /* Manually tweaking numbers certainly needs a rescan */
2988 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
2989 struct btrfs_qgroup *src;
2990 struct btrfs_qgroup *dst;
2992 if (!i_qgroups[0] || !i_qgroups[1])
2995 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2996 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
3003 dst->excl = src->excl + level_size;
3004 dst->excl_cmpr = src->excl_cmpr + level_size;
3009 spin_unlock(&fs_info->qgroup_lock);
3011 ret = btrfs_sysfs_add_one_qgroup(fs_info, dstgroup);
3014 mutex_unlock(&fs_info->qgroup_ioctl_lock);
3016 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3020 static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
3022 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
3023 qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
3026 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
3027 qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
3033 static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
3034 enum btrfs_qgroup_rsv_type type)
3036 struct btrfs_qgroup *qgroup;
3037 struct btrfs_fs_info *fs_info = root->fs_info;
3038 u64 ref_root = root->root_key.objectid;
3040 struct ulist_node *unode;
3041 struct ulist_iterator uiter;
3043 if (!is_fstree(ref_root))
3049 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
3050 capable(CAP_SYS_RESOURCE))
3053 spin_lock(&fs_info->qgroup_lock);
3054 if (!fs_info->quota_root)
3057 qgroup = find_qgroup_rb(fs_info, ref_root);
3062 * in a first step, we check all affected qgroups if any limits would
3065 ulist_reinit(fs_info->qgroup_ulist);
3066 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3067 qgroup_to_aux(qgroup), GFP_ATOMIC);
3070 ULIST_ITER_INIT(&uiter);
3071 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3072 struct btrfs_qgroup *qg;
3073 struct btrfs_qgroup_list *glist;
3075 qg = unode_aux_to_qgroup(unode);
3077 if (enforce && !qgroup_check_limits(qg, num_bytes)) {
3082 list_for_each_entry(glist, &qg->groups, next_group) {
3083 ret = ulist_add(fs_info->qgroup_ulist,
3084 glist->group->qgroupid,
3085 qgroup_to_aux(glist->group), GFP_ATOMIC);
3092 * no limits exceeded, now record the reservation into all qgroups
3094 ULIST_ITER_INIT(&uiter);
3095 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3096 struct btrfs_qgroup *qg;
3098 qg = unode_aux_to_qgroup(unode);
3100 qgroup_rsv_add(fs_info, qg, num_bytes, type);
3104 spin_unlock(&fs_info->qgroup_lock);
3109 * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0
3112 * Will handle all higher level qgroup too.
3114 * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
3115 * This special case is only used for META_PERTRANS type.
3117 void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
3118 u64 ref_root, u64 num_bytes,
3119 enum btrfs_qgroup_rsv_type type)
3121 struct btrfs_qgroup *qgroup;
3122 struct ulist_node *unode;
3123 struct ulist_iterator uiter;
3126 if (!is_fstree(ref_root))
3132 if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
3133 WARN(1, "%s: Invalid type to free", __func__);
3136 spin_lock(&fs_info->qgroup_lock);
3138 if (!fs_info->quota_root)
3141 qgroup = find_qgroup_rb(fs_info, ref_root);
3145 if (num_bytes == (u64)-1)
3147 * We're freeing all pertrans rsv, get reserved value from
3148 * level 0 qgroup as real num_bytes to free.
3150 num_bytes = qgroup->rsv.values[type];
3152 ulist_reinit(fs_info->qgroup_ulist);
3153 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3154 qgroup_to_aux(qgroup), GFP_ATOMIC);
3157 ULIST_ITER_INIT(&uiter);
3158 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3159 struct btrfs_qgroup *qg;
3160 struct btrfs_qgroup_list *glist;
3162 qg = unode_aux_to_qgroup(unode);
3164 qgroup_rsv_release(fs_info, qg, num_bytes, type);
3166 list_for_each_entry(glist, &qg->groups, next_group) {
3167 ret = ulist_add(fs_info->qgroup_ulist,
3168 glist->group->qgroupid,
3169 qgroup_to_aux(glist->group), GFP_ATOMIC);
3176 spin_unlock(&fs_info->qgroup_lock);
3180 * Check if the leaf is the last leaf. Which means all node pointers
3181 * are at their last position.
3183 static bool is_last_leaf(struct btrfs_path *path)
3187 for (i = 1; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
3188 if (path->slots[i] != btrfs_header_nritems(path->nodes[i]) - 1)
3195 * returns < 0 on error, 0 when more leafs are to be scanned.
3196 * returns 1 when done.
3198 static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans,
3199 struct btrfs_path *path)
3201 struct btrfs_fs_info *fs_info = trans->fs_info;
3202 struct btrfs_root *extent_root;
3203 struct btrfs_key found;
3204 struct extent_buffer *scratch_leaf = NULL;
3205 struct ulist *roots = NULL;
3211 mutex_lock(&fs_info->qgroup_rescan_lock);
3212 extent_root = btrfs_extent_root(fs_info,
3213 fs_info->qgroup_rescan_progress.objectid);
3214 ret = btrfs_search_slot_for_read(extent_root,
3215 &fs_info->qgroup_rescan_progress,
3218 btrfs_debug(fs_info,
3219 "current progress key (%llu %u %llu), search_slot ret %d",
3220 fs_info->qgroup_rescan_progress.objectid,
3221 fs_info->qgroup_rescan_progress.type,
3222 fs_info->qgroup_rescan_progress.offset, ret);
3226 * The rescan is about to end, we will not be scanning any
3227 * further blocks. We cannot unset the RESCAN flag here, because
3228 * we want to commit the transaction if everything went well.
3229 * To make the live accounting work in this phase, we set our
3230 * scan progress pointer such that every real extent objectid
3233 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
3234 btrfs_release_path(path);
3235 mutex_unlock(&fs_info->qgroup_rescan_lock);
3238 done = is_last_leaf(path);
3240 btrfs_item_key_to_cpu(path->nodes[0], &found,
3241 btrfs_header_nritems(path->nodes[0]) - 1);
3242 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
3244 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
3245 if (!scratch_leaf) {
3247 mutex_unlock(&fs_info->qgroup_rescan_lock);
3250 slot = path->slots[0];
3251 btrfs_release_path(path);
3252 mutex_unlock(&fs_info->qgroup_rescan_lock);
3254 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
3255 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
3256 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
3257 found.type != BTRFS_METADATA_ITEM_KEY)
3259 if (found.type == BTRFS_METADATA_ITEM_KEY)
3260 num_bytes = fs_info->nodesize;
3262 num_bytes = found.offset;
3264 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
3268 /* For rescan, just pass old_roots as NULL */
3269 ret = btrfs_qgroup_account_extent(trans, found.objectid,
3270 num_bytes, NULL, roots);
3276 free_extent_buffer(scratch_leaf);
3280 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
3285 static bool rescan_should_stop(struct btrfs_fs_info *fs_info)
3287 return btrfs_fs_closing(fs_info) ||
3288 test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state) ||
3289 !test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
3292 static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
3294 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
3295 qgroup_rescan_work);
3296 struct btrfs_path *path;
3297 struct btrfs_trans_handle *trans = NULL;
3300 bool stopped = false;
3302 path = btrfs_alloc_path();
3306 * Rescan should only search for commit root, and any later difference
3307 * should be recorded by qgroup
3309 path->search_commit_root = 1;
3310 path->skip_locking = 1;
3313 while (!err && !(stopped = rescan_should_stop(fs_info))) {
3314 trans = btrfs_start_transaction(fs_info->fs_root, 0);
3315 if (IS_ERR(trans)) {
3316 err = PTR_ERR(trans);
3320 err = qgroup_rescan_leaf(trans, path);
3323 btrfs_commit_transaction(trans);
3325 btrfs_end_transaction(trans);
3329 btrfs_free_path(path);
3331 mutex_lock(&fs_info->qgroup_rescan_lock);
3333 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
3334 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3335 } else if (err < 0 || stopped) {
3336 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3338 mutex_unlock(&fs_info->qgroup_rescan_lock);
3341 * only update status, since the previous part has already updated the
3344 trans = btrfs_start_transaction(fs_info->quota_root, 1);
3345 if (IS_ERR(trans)) {
3346 err = PTR_ERR(trans);
3349 "fail to start transaction for status update: %d",
3353 mutex_lock(&fs_info->qgroup_rescan_lock);
3355 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3357 ret = update_qgroup_status_item(trans);
3360 btrfs_err(fs_info, "fail to update qgroup status: %d",
3364 fs_info->qgroup_rescan_running = false;
3365 complete_all(&fs_info->qgroup_rescan_completion);
3366 mutex_unlock(&fs_info->qgroup_rescan_lock);
3371 btrfs_end_transaction(trans);
3374 btrfs_info(fs_info, "qgroup scan paused");
3375 } else if (err >= 0) {
3376 btrfs_info(fs_info, "qgroup scan completed%s",
3377 err > 0 ? " (inconsistency flag cleared)" : "");
3379 btrfs_err(fs_info, "qgroup scan failed with %d", err);
3384 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
3385 * memory required for the rescan context.
3388 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
3394 /* we're resuming qgroup rescan at mount time */
3395 if (!(fs_info->qgroup_flags &
3396 BTRFS_QGROUP_STATUS_FLAG_RESCAN)) {
3398 "qgroup rescan init failed, qgroup rescan is not queued");
3400 } else if (!(fs_info->qgroup_flags &
3401 BTRFS_QGROUP_STATUS_FLAG_ON)) {
3403 "qgroup rescan init failed, qgroup is not enabled");
3411 mutex_lock(&fs_info->qgroup_rescan_lock);
3414 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3416 "qgroup rescan is already in progress");
3418 } else if (!(fs_info->qgroup_flags &
3419 BTRFS_QGROUP_STATUS_FLAG_ON)) {
3421 "qgroup rescan init failed, qgroup is not enabled");
3423 } else if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
3424 /* Quota disable is in progress */
3429 mutex_unlock(&fs_info->qgroup_rescan_lock);
3432 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3435 memset(&fs_info->qgroup_rescan_progress, 0,
3436 sizeof(fs_info->qgroup_rescan_progress));
3437 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
3438 init_completion(&fs_info->qgroup_rescan_completion);
3439 mutex_unlock(&fs_info->qgroup_rescan_lock);
3441 btrfs_init_work(&fs_info->qgroup_rescan_work,
3442 btrfs_qgroup_rescan_worker, NULL, NULL);
3447 qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
3450 struct btrfs_qgroup *qgroup;
3452 spin_lock(&fs_info->qgroup_lock);
3453 /* clear all current qgroup tracking information */
3454 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
3455 qgroup = rb_entry(n, struct btrfs_qgroup, node);
3457 qgroup->rfer_cmpr = 0;
3459 qgroup->excl_cmpr = 0;
3460 qgroup_dirty(fs_info, qgroup);
3462 spin_unlock(&fs_info->qgroup_lock);
3466 btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
3469 struct btrfs_trans_handle *trans;
3471 ret = qgroup_rescan_init(fs_info, 0, 1);
3476 * We have set the rescan_progress to 0, which means no more
3477 * delayed refs will be accounted by btrfs_qgroup_account_ref.
3478 * However, btrfs_qgroup_account_ref may be right after its call
3479 * to btrfs_find_all_roots, in which case it would still do the
3481 * To solve this, we're committing the transaction, which will
3482 * ensure we run all delayed refs and only after that, we are
3483 * going to clear all tracking information for a clean start.
3486 trans = btrfs_join_transaction(fs_info->fs_root);
3487 if (IS_ERR(trans)) {
3488 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3489 return PTR_ERR(trans);
3491 ret = btrfs_commit_transaction(trans);
3493 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3497 qgroup_rescan_zero_tracking(fs_info);
3499 mutex_lock(&fs_info->qgroup_rescan_lock);
3500 fs_info->qgroup_rescan_running = true;
3501 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3502 &fs_info->qgroup_rescan_work);
3503 mutex_unlock(&fs_info->qgroup_rescan_lock);
3508 int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
3514 mutex_lock(&fs_info->qgroup_rescan_lock);
3515 running = fs_info->qgroup_rescan_running;
3516 mutex_unlock(&fs_info->qgroup_rescan_lock);
3522 ret = wait_for_completion_interruptible(
3523 &fs_info->qgroup_rescan_completion);
3525 wait_for_completion(&fs_info->qgroup_rescan_completion);
3531 * this is only called from open_ctree where we're still single threaded, thus
3532 * locking is omitted here.
3535 btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
3537 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3538 mutex_lock(&fs_info->qgroup_rescan_lock);
3539 fs_info->qgroup_rescan_running = true;
3540 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3541 &fs_info->qgroup_rescan_work);
3542 mutex_unlock(&fs_info->qgroup_rescan_lock);
3546 #define rbtree_iterate_from_safe(node, next, start) \
3547 for (node = start; node && ({ next = rb_next(node); 1;}); node = next)
3549 static int qgroup_unreserve_range(struct btrfs_inode *inode,
3550 struct extent_changeset *reserved, u64 start,
3553 struct rb_node *node;
3554 struct rb_node *next;
3555 struct ulist_node *entry;
3558 node = reserved->range_changed.root.rb_node;
3562 entry = rb_entry(node, struct ulist_node, rb_node);
3563 if (entry->val < start)
3564 node = node->rb_right;
3566 node = node->rb_left;
3569 if (entry->val > start && rb_prev(&entry->rb_node))
3570 entry = rb_entry(rb_prev(&entry->rb_node), struct ulist_node,
3573 rbtree_iterate_from_safe(node, next, &entry->rb_node) {
3579 entry = rb_entry(node, struct ulist_node, rb_node);
3580 entry_start = entry->val;
3581 entry_end = entry->aux;
3582 entry_len = entry_end - entry_start + 1;
3584 if (entry_start >= start + len)
3586 if (entry_start + entry_len <= start)
3589 * Now the entry is in [start, start + len), revert the
3590 * EXTENT_QGROUP_RESERVED bit.
3592 clear_ret = clear_extent_bits(&inode->io_tree, entry_start,
3593 entry_end, EXTENT_QGROUP_RESERVED);
3594 if (!ret && clear_ret < 0)
3597 ulist_del(&reserved->range_changed, entry->val, entry->aux);
3598 if (likely(reserved->bytes_changed >= entry_len)) {
3599 reserved->bytes_changed -= entry_len;
3602 reserved->bytes_changed = 0;
3610 * Try to free some space for qgroup.
3612 * For qgroup, there are only 3 ways to free qgroup space:
3613 * - Flush nodatacow write
3614 * Any nodatacow write will free its reserved data space at run_delalloc_range().
3615 * In theory, we should only flush nodatacow inodes, but it's not yet
3616 * possible, so we need to flush the whole root.
3618 * - Wait for ordered extents
3619 * When ordered extents are finished, their reserved metadata is finally
3620 * converted to per_trans status, which can be freed by later commit
3623 * - Commit transaction
3624 * This would free the meta_per_trans space.
3625 * In theory this shouldn't provide much space, but any more qgroup space
3628 static int try_flush_qgroup(struct btrfs_root *root)
3630 struct btrfs_trans_handle *trans;
3633 /* Can't hold an open transaction or we run the risk of deadlocking. */
3634 ASSERT(current->journal_info == NULL);
3635 if (WARN_ON(current->journal_info))
3639 * We don't want to run flush again and again, so if there is a running
3640 * one, we won't try to start a new flush, but exit directly.
3642 if (test_and_set_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state)) {
3643 wait_event(root->qgroup_flush_wait,
3644 !test_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state));
3648 ret = btrfs_start_delalloc_snapshot(root, true);
3651 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
3653 trans = btrfs_join_transaction(root);
3654 if (IS_ERR(trans)) {
3655 ret = PTR_ERR(trans);
3659 ret = btrfs_commit_transaction(trans);
3661 clear_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state);
3662 wake_up(&root->qgroup_flush_wait);
3666 static int qgroup_reserve_data(struct btrfs_inode *inode,
3667 struct extent_changeset **reserved_ret, u64 start,
3670 struct btrfs_root *root = inode->root;
3671 struct extent_changeset *reserved;
3672 bool new_reserved = false;
3677 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
3678 !is_fstree(root->root_key.objectid) || len == 0)
3681 /* @reserved parameter is mandatory for qgroup */
3682 if (WARN_ON(!reserved_ret))
3684 if (!*reserved_ret) {
3685 new_reserved = true;
3686 *reserved_ret = extent_changeset_alloc();
3690 reserved = *reserved_ret;
3691 /* Record already reserved space */
3692 orig_reserved = reserved->bytes_changed;
3693 ret = set_record_extent_bits(&inode->io_tree, start,
3694 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
3696 /* Newly reserved space */
3697 to_reserve = reserved->bytes_changed - orig_reserved;
3698 trace_btrfs_qgroup_reserve_data(&inode->vfs_inode, start, len,
3699 to_reserve, QGROUP_RESERVE);
3702 ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
3709 qgroup_unreserve_range(inode, reserved, start, len);
3712 extent_changeset_free(reserved);
3713 *reserved_ret = NULL;
3719 * Reserve qgroup space for range [start, start + len).
3721 * This function will either reserve space from related qgroups or do nothing
3722 * if the range is already reserved.
3724 * Return 0 for successful reservation
3725 * Return <0 for error (including -EQUOT)
3727 * NOTE: This function may sleep for memory allocation, dirty page flushing and
3728 * commit transaction. So caller should not hold any dirty page locked.
3730 int btrfs_qgroup_reserve_data(struct btrfs_inode *inode,
3731 struct extent_changeset **reserved_ret, u64 start,
3736 ret = qgroup_reserve_data(inode, reserved_ret, start, len);
3737 if (ret <= 0 && ret != -EDQUOT)
3740 ret = try_flush_qgroup(inode->root);
3743 return qgroup_reserve_data(inode, reserved_ret, start, len);
3746 /* Free ranges specified by @reserved, normally in error path */
3747 static int qgroup_free_reserved_data(struct btrfs_inode *inode,
3748 struct extent_changeset *reserved, u64 start, u64 len)
3750 struct btrfs_root *root = inode->root;
3751 struct ulist_node *unode;
3752 struct ulist_iterator uiter;
3753 struct extent_changeset changeset;
3757 extent_changeset_init(&changeset);
3758 len = round_up(start + len, root->fs_info->sectorsize);
3759 start = round_down(start, root->fs_info->sectorsize);
3761 ULIST_ITER_INIT(&uiter);
3762 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
3763 u64 range_start = unode->val;
3764 /* unode->aux is the inclusive end */
3765 u64 range_len = unode->aux - range_start + 1;
3769 extent_changeset_release(&changeset);
3771 /* Only free range in range [start, start + len) */
3772 if (range_start >= start + len ||
3773 range_start + range_len <= start)
3775 free_start = max(range_start, start);
3776 free_len = min(start + len, range_start + range_len) -
3779 * TODO: To also modify reserved->ranges_reserved to reflect
3782 * However as long as we free qgroup reserved according to
3783 * EXTENT_QGROUP_RESERVED, we won't double free.
3784 * So not need to rush.
3786 ret = clear_record_extent_bits(&inode->io_tree, free_start,
3787 free_start + free_len - 1,
3788 EXTENT_QGROUP_RESERVED, &changeset);
3791 freed += changeset.bytes_changed;
3793 btrfs_qgroup_free_refroot(root->fs_info, root->root_key.objectid, freed,
3794 BTRFS_QGROUP_RSV_DATA);
3797 extent_changeset_release(&changeset);
3801 static int __btrfs_qgroup_release_data(struct btrfs_inode *inode,
3802 struct extent_changeset *reserved, u64 start, u64 len,
3805 struct extent_changeset changeset;
3806 int trace_op = QGROUP_RELEASE;
3809 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &inode->root->fs_info->flags))
3812 /* In release case, we shouldn't have @reserved */
3813 WARN_ON(!free && reserved);
3814 if (free && reserved)
3815 return qgroup_free_reserved_data(inode, reserved, start, len);
3816 extent_changeset_init(&changeset);
3817 ret = clear_record_extent_bits(&inode->io_tree, start, start + len -1,
3818 EXTENT_QGROUP_RESERVED, &changeset);
3823 trace_op = QGROUP_FREE;
3824 trace_btrfs_qgroup_release_data(&inode->vfs_inode, start, len,
3825 changeset.bytes_changed, trace_op);
3827 btrfs_qgroup_free_refroot(inode->root->fs_info,
3828 inode->root->root_key.objectid,
3829 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
3830 ret = changeset.bytes_changed;
3832 extent_changeset_release(&changeset);
3837 * Free a reserved space range from io_tree and related qgroups
3839 * Should be called when a range of pages get invalidated before reaching disk.
3840 * Or for error cleanup case.
3841 * if @reserved is given, only reserved range in [@start, @start + @len) will
3844 * For data written to disk, use btrfs_qgroup_release_data().
3846 * NOTE: This function may sleep for memory allocation.
3848 int btrfs_qgroup_free_data(struct btrfs_inode *inode,
3849 struct extent_changeset *reserved, u64 start, u64 len)
3851 return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
3855 * Release a reserved space range from io_tree only.
3857 * Should be called when a range of pages get written to disk and corresponding
3858 * FILE_EXTENT is inserted into corresponding root.
3860 * Since new qgroup accounting framework will only update qgroup numbers at
3861 * commit_transaction() time, its reserved space shouldn't be freed from
3864 * But we should release the range from io_tree, to allow further write to be
3867 * NOTE: This function may sleep for memory allocation.
3869 int btrfs_qgroup_release_data(struct btrfs_inode *inode, u64 start, u64 len)
3871 return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
3874 static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3875 enum btrfs_qgroup_rsv_type type)
3877 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3878 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3883 spin_lock(&root->qgroup_meta_rsv_lock);
3884 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
3885 root->qgroup_meta_rsv_prealloc += num_bytes;
3887 root->qgroup_meta_rsv_pertrans += num_bytes;
3888 spin_unlock(&root->qgroup_meta_rsv_lock);
3891 static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3892 enum btrfs_qgroup_rsv_type type)
3894 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3895 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3900 spin_lock(&root->qgroup_meta_rsv_lock);
3901 if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
3902 num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
3904 root->qgroup_meta_rsv_prealloc -= num_bytes;
3906 num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
3908 root->qgroup_meta_rsv_pertrans -= num_bytes;
3910 spin_unlock(&root->qgroup_meta_rsv_lock);
3914 int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3915 enum btrfs_qgroup_rsv_type type, bool enforce)
3917 struct btrfs_fs_info *fs_info = root->fs_info;
3920 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3921 !is_fstree(root->root_key.objectid) || num_bytes == 0)
3924 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
3925 trace_qgroup_meta_reserve(root, (s64)num_bytes, type);
3926 ret = qgroup_reserve(root, num_bytes, enforce, type);
3930 * Record what we have reserved into root.
3932 * To avoid quota disabled->enabled underflow.
3933 * In that case, we may try to free space we haven't reserved
3934 * (since quota was disabled), so record what we reserved into root.
3935 * And ensure later release won't underflow this number.
3937 add_root_meta_rsv(root, num_bytes, type);
3941 int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3942 enum btrfs_qgroup_rsv_type type, bool enforce,
3947 ret = btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
3948 if ((ret <= 0 && ret != -EDQUOT) || noflush)
3951 ret = try_flush_qgroup(root);
3954 return btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
3957 void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
3959 struct btrfs_fs_info *fs_info = root->fs_info;
3961 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3962 !is_fstree(root->root_key.objectid))
3965 /* TODO: Update trace point to handle such free */
3966 trace_qgroup_meta_free_all_pertrans(root);
3967 /* Special value -1 means to free all reserved space */
3968 btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid, (u64)-1,
3969 BTRFS_QGROUP_RSV_META_PERTRANS);
3972 void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
3973 enum btrfs_qgroup_rsv_type type)
3975 struct btrfs_fs_info *fs_info = root->fs_info;
3977 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3978 !is_fstree(root->root_key.objectid))
3982 * reservation for META_PREALLOC can happen before quota is enabled,
3983 * which can lead to underflow.
3984 * Here ensure we will only free what we really have reserved.
3986 num_bytes = sub_root_meta_rsv(root, num_bytes, type);
3987 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
3988 trace_qgroup_meta_reserve(root, -(s64)num_bytes, type);
3989 btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid,
3993 static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
3996 struct btrfs_qgroup *qgroup;
3997 struct ulist_node *unode;
3998 struct ulist_iterator uiter;
4003 if (!fs_info->quota_root)
4006 spin_lock(&fs_info->qgroup_lock);
4007 qgroup = find_qgroup_rb(fs_info, ref_root);
4010 ulist_reinit(fs_info->qgroup_ulist);
4011 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
4012 qgroup_to_aux(qgroup), GFP_ATOMIC);
4015 ULIST_ITER_INIT(&uiter);
4016 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
4017 struct btrfs_qgroup *qg;
4018 struct btrfs_qgroup_list *glist;
4020 qg = unode_aux_to_qgroup(unode);
4022 qgroup_rsv_release(fs_info, qg, num_bytes,
4023 BTRFS_QGROUP_RSV_META_PREALLOC);
4024 qgroup_rsv_add(fs_info, qg, num_bytes,
4025 BTRFS_QGROUP_RSV_META_PERTRANS);
4026 list_for_each_entry(glist, &qg->groups, next_group) {
4027 ret = ulist_add(fs_info->qgroup_ulist,
4028 glist->group->qgroupid,
4029 qgroup_to_aux(glist->group), GFP_ATOMIC);
4035 spin_unlock(&fs_info->qgroup_lock);
4038 void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
4040 struct btrfs_fs_info *fs_info = root->fs_info;
4042 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
4043 !is_fstree(root->root_key.objectid))
4045 /* Same as btrfs_qgroup_free_meta_prealloc() */
4046 num_bytes = sub_root_meta_rsv(root, num_bytes,
4047 BTRFS_QGROUP_RSV_META_PREALLOC);
4048 trace_qgroup_meta_convert(root, num_bytes);
4049 qgroup_convert_meta(fs_info, root->root_key.objectid, num_bytes);
4053 * Check qgroup reserved space leaking, normally at destroy inode
4056 void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode)
4058 struct extent_changeset changeset;
4059 struct ulist_node *unode;
4060 struct ulist_iterator iter;
4063 extent_changeset_init(&changeset);
4064 ret = clear_record_extent_bits(&inode->io_tree, 0, (u64)-1,
4065 EXTENT_QGROUP_RESERVED, &changeset);
4068 if (WARN_ON(changeset.bytes_changed)) {
4069 ULIST_ITER_INIT(&iter);
4070 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
4071 btrfs_warn(inode->root->fs_info,
4072 "leaking qgroup reserved space, ino: %llu, start: %llu, end: %llu",
4073 btrfs_ino(inode), unode->val, unode->aux);
4075 btrfs_qgroup_free_refroot(inode->root->fs_info,
4076 inode->root->root_key.objectid,
4077 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
4080 extent_changeset_release(&changeset);
4083 void btrfs_qgroup_init_swapped_blocks(
4084 struct btrfs_qgroup_swapped_blocks *swapped_blocks)
4088 spin_lock_init(&swapped_blocks->lock);
4089 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
4090 swapped_blocks->blocks[i] = RB_ROOT;
4091 swapped_blocks->swapped = false;
4095 * Delete all swapped blocks record of @root.
4096 * Every record here means we skipped a full subtree scan for qgroup.
4098 * Gets called when committing one transaction.
4100 void btrfs_qgroup_clean_swapped_blocks(struct btrfs_root *root)
4102 struct btrfs_qgroup_swapped_blocks *swapped_blocks;
4105 swapped_blocks = &root->swapped_blocks;
4107 spin_lock(&swapped_blocks->lock);
4108 if (!swapped_blocks->swapped)
4110 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4111 struct rb_root *cur_root = &swapped_blocks->blocks[i];
4112 struct btrfs_qgroup_swapped_block *entry;
4113 struct btrfs_qgroup_swapped_block *next;
4115 rbtree_postorder_for_each_entry_safe(entry, next, cur_root,
4118 swapped_blocks->blocks[i] = RB_ROOT;
4120 swapped_blocks->swapped = false;
4122 spin_unlock(&swapped_blocks->lock);
4126 * Add subtree roots record into @subvol_root.
4128 * @subvol_root: tree root of the subvolume tree get swapped
4129 * @bg: block group under balance
4130 * @subvol_parent/slot: pointer to the subtree root in subvolume tree
4131 * @reloc_parent/slot: pointer to the subtree root in reloc tree
4132 * BOTH POINTERS ARE BEFORE TREE SWAP
4133 * @last_snapshot: last snapshot generation of the subvolume tree
4135 int btrfs_qgroup_add_swapped_blocks(struct btrfs_trans_handle *trans,
4136 struct btrfs_root *subvol_root,
4137 struct btrfs_block_group *bg,
4138 struct extent_buffer *subvol_parent, int subvol_slot,
4139 struct extent_buffer *reloc_parent, int reloc_slot,
4142 struct btrfs_fs_info *fs_info = subvol_root->fs_info;
4143 struct btrfs_qgroup_swapped_blocks *blocks = &subvol_root->swapped_blocks;
4144 struct btrfs_qgroup_swapped_block *block;
4145 struct rb_node **cur;
4146 struct rb_node *parent = NULL;
4147 int level = btrfs_header_level(subvol_parent) - 1;
4150 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
4153 if (btrfs_node_ptr_generation(subvol_parent, subvol_slot) >
4154 btrfs_node_ptr_generation(reloc_parent, reloc_slot)) {
4155 btrfs_err_rl(fs_info,
4156 "%s: bad parameter order, subvol_gen=%llu reloc_gen=%llu",
4158 btrfs_node_ptr_generation(subvol_parent, subvol_slot),
4159 btrfs_node_ptr_generation(reloc_parent, reloc_slot));
4163 block = kmalloc(sizeof(*block), GFP_NOFS);
4170 * @reloc_parent/slot is still before swap, while @block is going to
4171 * record the bytenr after swap, so we do the swap here.
4173 block->subvol_bytenr = btrfs_node_blockptr(reloc_parent, reloc_slot);
4174 block->subvol_generation = btrfs_node_ptr_generation(reloc_parent,
4176 block->reloc_bytenr = btrfs_node_blockptr(subvol_parent, subvol_slot);
4177 block->reloc_generation = btrfs_node_ptr_generation(subvol_parent,
4179 block->last_snapshot = last_snapshot;
4180 block->level = level;
4183 * If we have bg == NULL, we're called from btrfs_recover_relocation(),
4184 * no one else can modify tree blocks thus we qgroup will not change
4185 * no matter the value of trace_leaf.
4187 if (bg && bg->flags & BTRFS_BLOCK_GROUP_DATA)
4188 block->trace_leaf = true;
4190 block->trace_leaf = false;
4191 btrfs_node_key_to_cpu(reloc_parent, &block->first_key, reloc_slot);
4193 /* Insert @block into @blocks */
4194 spin_lock(&blocks->lock);
4195 cur = &blocks->blocks[level].rb_node;
4197 struct btrfs_qgroup_swapped_block *entry;
4200 entry = rb_entry(parent, struct btrfs_qgroup_swapped_block,
4203 if (entry->subvol_bytenr < block->subvol_bytenr) {
4204 cur = &(*cur)->rb_left;
4205 } else if (entry->subvol_bytenr > block->subvol_bytenr) {
4206 cur = &(*cur)->rb_right;
4208 if (entry->subvol_generation !=
4209 block->subvol_generation ||
4210 entry->reloc_bytenr != block->reloc_bytenr ||
4211 entry->reloc_generation !=
4212 block->reloc_generation) {
4214 * Duplicated but mismatch entry found.
4217 * Marking qgroup inconsistent should be enough
4220 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
4227 rb_link_node(&block->node, parent, cur);
4228 rb_insert_color(&block->node, &blocks->blocks[level]);
4229 blocks->swapped = true;
4231 spin_unlock(&blocks->lock);
4234 fs_info->qgroup_flags |=
4235 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
4240 * Check if the tree block is a subtree root, and if so do the needed
4241 * delayed subtree trace for qgroup.
4243 * This is called during btrfs_cow_block().
4245 int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans,
4246 struct btrfs_root *root,
4247 struct extent_buffer *subvol_eb)
4249 struct btrfs_fs_info *fs_info = root->fs_info;
4250 struct btrfs_qgroup_swapped_blocks *blocks = &root->swapped_blocks;
4251 struct btrfs_qgroup_swapped_block *block;
4252 struct extent_buffer *reloc_eb = NULL;
4253 struct rb_node *node;
4255 bool swapped = false;
4256 int level = btrfs_header_level(subvol_eb);
4260 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
4262 if (!is_fstree(root->root_key.objectid) || !root->reloc_root)
4265 spin_lock(&blocks->lock);
4266 if (!blocks->swapped) {
4267 spin_unlock(&blocks->lock);
4270 node = blocks->blocks[level].rb_node;
4273 block = rb_entry(node, struct btrfs_qgroup_swapped_block, node);
4274 if (block->subvol_bytenr < subvol_eb->start) {
4275 node = node->rb_left;
4276 } else if (block->subvol_bytenr > subvol_eb->start) {
4277 node = node->rb_right;
4284 spin_unlock(&blocks->lock);
4287 /* Found one, remove it from @blocks first and update blocks->swapped */
4288 rb_erase(&block->node, &blocks->blocks[level]);
4289 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4290 if (RB_EMPTY_ROOT(&blocks->blocks[i])) {
4295 blocks->swapped = swapped;
4296 spin_unlock(&blocks->lock);
4298 /* Read out reloc subtree root */
4299 reloc_eb = read_tree_block(fs_info, block->reloc_bytenr, 0,
4300 block->reloc_generation, block->level,
4302 if (IS_ERR(reloc_eb)) {
4303 ret = PTR_ERR(reloc_eb);
4307 if (!extent_buffer_uptodate(reloc_eb)) {
4312 ret = qgroup_trace_subtree_swap(trans, reloc_eb, subvol_eb,
4313 block->last_snapshot, block->trace_leaf);
4316 free_extent_buffer(reloc_eb);
4319 btrfs_err_rl(fs_info,
4320 "failed to account subtree at bytenr %llu: %d",
4321 subvol_eb->start, ret);
4322 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
4327 void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans)
4329 struct btrfs_qgroup_extent_record *entry;
4330 struct btrfs_qgroup_extent_record *next;
4331 struct rb_root *root;
4333 root = &trans->delayed_refs.dirty_extent_root;
4334 rbtree_postorder_for_each_entry_safe(entry, next, root, node) {
4335 ulist_free(entry->old_roots);