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/sizes.h>
17 #include "transaction.h"
22 #include "extent_io.h"
24 #include "block-group.h"
27 * - subvol delete -> delete when ref goes to 0? delete limits also?
31 * - copy also limits on subvol creation
34 * - performance benchmarks
35 * - check all ioctl parameters
39 * Helpers to access qgroup reservation
41 * Callers should ensure the lock context and type are valid
44 static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
49 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
50 ret += qgroup->rsv.values[i];
55 #ifdef CONFIG_BTRFS_DEBUG
56 static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
58 if (type == BTRFS_QGROUP_RSV_DATA)
60 if (type == BTRFS_QGROUP_RSV_META_PERTRANS)
61 return "meta_pertrans";
62 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
63 return "meta_prealloc";
68 static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
69 struct btrfs_qgroup *qgroup, u64 num_bytes,
70 enum btrfs_qgroup_rsv_type type)
72 trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
73 qgroup->rsv.values[type] += num_bytes;
76 static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
77 struct btrfs_qgroup *qgroup, u64 num_bytes,
78 enum btrfs_qgroup_rsv_type type)
80 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
81 if (qgroup->rsv.values[type] >= num_bytes) {
82 qgroup->rsv.values[type] -= num_bytes;
85 #ifdef CONFIG_BTRFS_DEBUG
87 "qgroup %llu %s reserved space underflow, have %llu to free %llu",
88 qgroup->qgroupid, qgroup_rsv_type_str(type),
89 qgroup->rsv.values[type], num_bytes);
91 qgroup->rsv.values[type] = 0;
94 static void qgroup_rsv_add_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_add(fs_info, dest, src->rsv.values[i], i);
104 static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
105 struct btrfs_qgroup *dest,
106 struct btrfs_qgroup *src)
110 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
111 qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
114 static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
117 if (qg->old_refcnt < seq)
118 qg->old_refcnt = seq;
119 qg->old_refcnt += mod;
122 static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
125 if (qg->new_refcnt < seq)
126 qg->new_refcnt = seq;
127 qg->new_refcnt += mod;
130 static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
132 if (qg->old_refcnt < seq)
134 return qg->old_refcnt - seq;
137 static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
139 if (qg->new_refcnt < seq)
141 return qg->new_refcnt - seq;
145 * glue structure to represent the relations between qgroups.
147 struct btrfs_qgroup_list {
148 struct list_head next_group;
149 struct list_head next_member;
150 struct btrfs_qgroup *group;
151 struct btrfs_qgroup *member;
154 static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
156 return (u64)(uintptr_t)qg;
159 static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
161 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
165 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
167 static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
169 /* must be called with qgroup_ioctl_lock held */
170 static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
173 struct rb_node *n = fs_info->qgroup_tree.rb_node;
174 struct btrfs_qgroup *qgroup;
177 qgroup = rb_entry(n, struct btrfs_qgroup, node);
178 if (qgroup->qgroupid < qgroupid)
180 else if (qgroup->qgroupid > qgroupid)
188 /* must be called with qgroup_lock held */
189 static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
192 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
193 struct rb_node *parent = NULL;
194 struct btrfs_qgroup *qgroup;
198 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
200 if (qgroup->qgroupid < qgroupid)
202 else if (qgroup->qgroupid > qgroupid)
208 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
210 return ERR_PTR(-ENOMEM);
212 qgroup->qgroupid = qgroupid;
213 INIT_LIST_HEAD(&qgroup->groups);
214 INIT_LIST_HEAD(&qgroup->members);
215 INIT_LIST_HEAD(&qgroup->dirty);
217 rb_link_node(&qgroup->node, parent, p);
218 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
223 static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
225 struct btrfs_qgroup_list *list;
227 list_del(&qgroup->dirty);
228 while (!list_empty(&qgroup->groups)) {
229 list = list_first_entry(&qgroup->groups,
230 struct btrfs_qgroup_list, next_group);
231 list_del(&list->next_group);
232 list_del(&list->next_member);
236 while (!list_empty(&qgroup->members)) {
237 list = list_first_entry(&qgroup->members,
238 struct btrfs_qgroup_list, next_member);
239 list_del(&list->next_group);
240 list_del(&list->next_member);
246 /* must be called with qgroup_lock held */
247 static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
249 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
254 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
255 __del_qgroup_rb(qgroup);
259 /* must be called with qgroup_lock held */
260 static int add_relation_rb(struct btrfs_fs_info *fs_info,
261 u64 memberid, u64 parentid)
263 struct btrfs_qgroup *member;
264 struct btrfs_qgroup *parent;
265 struct btrfs_qgroup_list *list;
267 member = find_qgroup_rb(fs_info, memberid);
268 parent = find_qgroup_rb(fs_info, parentid);
269 if (!member || !parent)
272 list = kzalloc(sizeof(*list), GFP_ATOMIC);
276 list->group = parent;
277 list->member = member;
278 list_add_tail(&list->next_group, &member->groups);
279 list_add_tail(&list->next_member, &parent->members);
284 /* must be called with qgroup_lock held */
285 static int del_relation_rb(struct btrfs_fs_info *fs_info,
286 u64 memberid, u64 parentid)
288 struct btrfs_qgroup *member;
289 struct btrfs_qgroup *parent;
290 struct btrfs_qgroup_list *list;
292 member = find_qgroup_rb(fs_info, memberid);
293 parent = find_qgroup_rb(fs_info, parentid);
294 if (!member || !parent)
297 list_for_each_entry(list, &member->groups, next_group) {
298 if (list->group == parent) {
299 list_del(&list->next_group);
300 list_del(&list->next_member);
308 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
309 int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
312 struct btrfs_qgroup *qgroup;
314 qgroup = find_qgroup_rb(fs_info, qgroupid);
317 if (qgroup->rfer != rfer || qgroup->excl != excl)
324 * The full config is read in one go, only called from open_ctree()
325 * It doesn't use any locking, as at this point we're still single-threaded
327 int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
329 struct btrfs_key key;
330 struct btrfs_key found_key;
331 struct btrfs_root *quota_root = fs_info->quota_root;
332 struct btrfs_path *path = NULL;
333 struct extent_buffer *l;
337 u64 rescan_progress = 0;
339 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
342 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
343 if (!fs_info->qgroup_ulist) {
348 path = btrfs_alloc_path();
354 /* default this to quota off, in case no status key is found */
355 fs_info->qgroup_flags = 0;
358 * pass 1: read status, all qgroup infos and limits
363 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
368 struct btrfs_qgroup *qgroup;
370 slot = path->slots[0];
372 btrfs_item_key_to_cpu(l, &found_key, slot);
374 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
375 struct btrfs_qgroup_status_item *ptr;
377 ptr = btrfs_item_ptr(l, slot,
378 struct btrfs_qgroup_status_item);
380 if (btrfs_qgroup_status_version(l, ptr) !=
381 BTRFS_QGROUP_STATUS_VERSION) {
383 "old qgroup version, quota disabled");
386 if (btrfs_qgroup_status_generation(l, ptr) !=
387 fs_info->generation) {
388 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
390 "qgroup generation mismatch, marked as inconsistent");
392 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
394 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
398 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
399 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
402 qgroup = find_qgroup_rb(fs_info, found_key.offset);
403 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
404 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
405 btrfs_err(fs_info, "inconsistent qgroup config");
406 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
409 qgroup = add_qgroup_rb(fs_info, found_key.offset);
410 if (IS_ERR(qgroup)) {
411 ret = PTR_ERR(qgroup);
415 switch (found_key.type) {
416 case BTRFS_QGROUP_INFO_KEY: {
417 struct btrfs_qgroup_info_item *ptr;
419 ptr = btrfs_item_ptr(l, slot,
420 struct btrfs_qgroup_info_item);
421 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
422 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
423 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
424 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
425 /* generation currently unused */
428 case BTRFS_QGROUP_LIMIT_KEY: {
429 struct btrfs_qgroup_limit_item *ptr;
431 ptr = btrfs_item_ptr(l, slot,
432 struct btrfs_qgroup_limit_item);
433 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
434 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
435 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
436 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
437 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
442 ret = btrfs_next_item(quota_root, path);
448 btrfs_release_path(path);
451 * pass 2: read all qgroup relations
454 key.type = BTRFS_QGROUP_RELATION_KEY;
456 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
460 slot = path->slots[0];
462 btrfs_item_key_to_cpu(l, &found_key, slot);
464 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
467 if (found_key.objectid > found_key.offset) {
468 /* parent <- member, not needed to build config */
469 /* FIXME should we omit the key completely? */
473 ret = add_relation_rb(fs_info, found_key.objectid,
475 if (ret == -ENOENT) {
477 "orphan qgroup relation 0x%llx->0x%llx",
478 found_key.objectid, found_key.offset);
479 ret = 0; /* ignore the error */
484 ret = btrfs_next_item(quota_root, path);
491 fs_info->qgroup_flags |= flags;
492 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
493 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
494 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
496 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
497 btrfs_free_path(path);
500 ulist_free(fs_info->qgroup_ulist);
501 fs_info->qgroup_ulist = NULL;
502 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
505 return ret < 0 ? ret : 0;
508 static u64 btrfs_qgroup_subvolid(u64 qgroupid)
510 return (qgroupid & ((1ULL << BTRFS_QGROUP_LEVEL_SHIFT) - 1));
514 * Called in close_ctree() when quota is still enabled. This verifies we don't
515 * leak some reserved space.
517 * Return false if no reserved space is left.
518 * Return true if some reserved space is leaked.
520 bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info)
522 struct rb_node *node;
525 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
528 * Since we're unmounting, there is no race and no need to grab qgroup
529 * lock. And here we don't go post-order to provide a more user
530 * friendly sorted result.
532 for (node = rb_first(&fs_info->qgroup_tree); node; node = rb_next(node)) {
533 struct btrfs_qgroup *qgroup;
536 qgroup = rb_entry(node, struct btrfs_qgroup, node);
537 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++) {
538 if (qgroup->rsv.values[i]) {
541 "qgroup %llu/%llu has unreleased space, type %d rsv %llu",
542 btrfs_qgroup_level(qgroup->qgroupid),
543 btrfs_qgroup_subvolid(qgroup->qgroupid),
544 i, qgroup->rsv.values[i]);
552 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
553 * first two are in single-threaded paths.And for the third one, we have set
554 * quota_root to be null with qgroup_lock held before, so it is safe to clean
555 * up the in-memory structures without qgroup_lock held.
557 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
560 struct btrfs_qgroup *qgroup;
562 while ((n = rb_first(&fs_info->qgroup_tree))) {
563 qgroup = rb_entry(n, struct btrfs_qgroup, node);
564 rb_erase(n, &fs_info->qgroup_tree);
565 __del_qgroup_rb(qgroup);
568 * We call btrfs_free_qgroup_config() when unmounting
569 * filesystem and disabling quota, so we set qgroup_ulist
570 * to be null here to avoid double free.
572 ulist_free(fs_info->qgroup_ulist);
573 fs_info->qgroup_ulist = NULL;
576 static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
580 struct btrfs_root *quota_root = trans->fs_info->quota_root;
581 struct btrfs_path *path;
582 struct btrfs_key key;
584 path = btrfs_alloc_path();
589 key.type = BTRFS_QGROUP_RELATION_KEY;
592 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
594 btrfs_mark_buffer_dirty(path->nodes[0]);
596 btrfs_free_path(path);
600 static int del_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
604 struct btrfs_root *quota_root = trans->fs_info->quota_root;
605 struct btrfs_path *path;
606 struct btrfs_key key;
608 path = btrfs_alloc_path();
613 key.type = BTRFS_QGROUP_RELATION_KEY;
616 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
625 ret = btrfs_del_item(trans, quota_root, path);
627 btrfs_free_path(path);
631 static int add_qgroup_item(struct btrfs_trans_handle *trans,
632 struct btrfs_root *quota_root, u64 qgroupid)
635 struct btrfs_path *path;
636 struct btrfs_qgroup_info_item *qgroup_info;
637 struct btrfs_qgroup_limit_item *qgroup_limit;
638 struct extent_buffer *leaf;
639 struct btrfs_key key;
641 if (btrfs_is_testing(quota_root->fs_info))
644 path = btrfs_alloc_path();
649 key.type = BTRFS_QGROUP_INFO_KEY;
650 key.offset = qgroupid;
653 * Avoid a transaction abort by catching -EEXIST here. In that
654 * case, we proceed by re-initializing the existing structure
658 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
659 sizeof(*qgroup_info));
660 if (ret && ret != -EEXIST)
663 leaf = path->nodes[0];
664 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
665 struct btrfs_qgroup_info_item);
666 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
667 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
668 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
669 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
670 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
672 btrfs_mark_buffer_dirty(leaf);
674 btrfs_release_path(path);
676 key.type = BTRFS_QGROUP_LIMIT_KEY;
677 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
678 sizeof(*qgroup_limit));
679 if (ret && ret != -EEXIST)
682 leaf = path->nodes[0];
683 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
684 struct btrfs_qgroup_limit_item);
685 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
686 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
687 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
688 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
689 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
691 btrfs_mark_buffer_dirty(leaf);
695 btrfs_free_path(path);
699 static int del_qgroup_item(struct btrfs_trans_handle *trans, u64 qgroupid)
702 struct btrfs_root *quota_root = trans->fs_info->quota_root;
703 struct btrfs_path *path;
704 struct btrfs_key key;
706 path = btrfs_alloc_path();
711 key.type = BTRFS_QGROUP_INFO_KEY;
712 key.offset = qgroupid;
713 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
722 ret = btrfs_del_item(trans, quota_root, path);
726 btrfs_release_path(path);
728 key.type = BTRFS_QGROUP_LIMIT_KEY;
729 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
738 ret = btrfs_del_item(trans, quota_root, path);
741 btrfs_free_path(path);
745 static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
746 struct btrfs_qgroup *qgroup)
748 struct btrfs_root *quota_root = trans->fs_info->quota_root;
749 struct btrfs_path *path;
750 struct btrfs_key key;
751 struct extent_buffer *l;
752 struct btrfs_qgroup_limit_item *qgroup_limit;
757 key.type = BTRFS_QGROUP_LIMIT_KEY;
758 key.offset = qgroup->qgroupid;
760 path = btrfs_alloc_path();
764 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
772 slot = path->slots[0];
773 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
774 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
775 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
776 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
777 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
778 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
780 btrfs_mark_buffer_dirty(l);
783 btrfs_free_path(path);
787 static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
788 struct btrfs_qgroup *qgroup)
790 struct btrfs_fs_info *fs_info = trans->fs_info;
791 struct btrfs_root *quota_root = fs_info->quota_root;
792 struct btrfs_path *path;
793 struct btrfs_key key;
794 struct extent_buffer *l;
795 struct btrfs_qgroup_info_item *qgroup_info;
799 if (btrfs_is_testing(fs_info))
803 key.type = BTRFS_QGROUP_INFO_KEY;
804 key.offset = qgroup->qgroupid;
806 path = btrfs_alloc_path();
810 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
818 slot = path->slots[0];
819 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
820 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
821 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
822 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
823 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
824 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
826 btrfs_mark_buffer_dirty(l);
829 btrfs_free_path(path);
833 static int update_qgroup_status_item(struct btrfs_trans_handle *trans)
835 struct btrfs_fs_info *fs_info = trans->fs_info;
836 struct btrfs_root *quota_root = fs_info->quota_root;
837 struct btrfs_path *path;
838 struct btrfs_key key;
839 struct extent_buffer *l;
840 struct btrfs_qgroup_status_item *ptr;
845 key.type = BTRFS_QGROUP_STATUS_KEY;
848 path = btrfs_alloc_path();
852 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
860 slot = path->slots[0];
861 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
862 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
863 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
864 btrfs_set_qgroup_status_rescan(l, ptr,
865 fs_info->qgroup_rescan_progress.objectid);
867 btrfs_mark_buffer_dirty(l);
870 btrfs_free_path(path);
875 * called with qgroup_lock held
877 static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
878 struct btrfs_root *root)
880 struct btrfs_path *path;
881 struct btrfs_key key;
882 struct extent_buffer *leaf = NULL;
886 path = btrfs_alloc_path();
890 path->leave_spinning = 1;
897 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
900 leaf = path->nodes[0];
901 nr = btrfs_header_nritems(leaf);
905 * delete the leaf one by one
906 * since the whole tree is going
910 ret = btrfs_del_items(trans, root, path, 0, nr);
914 btrfs_release_path(path);
918 btrfs_free_path(path);
922 int btrfs_quota_enable(struct btrfs_fs_info *fs_info)
924 struct btrfs_root *quota_root;
925 struct btrfs_root *tree_root = fs_info->tree_root;
926 struct btrfs_path *path = NULL;
927 struct btrfs_qgroup_status_item *ptr;
928 struct extent_buffer *leaf;
929 struct btrfs_key key;
930 struct btrfs_key found_key;
931 struct btrfs_qgroup *qgroup = NULL;
932 struct btrfs_trans_handle *trans = NULL;
936 mutex_lock(&fs_info->qgroup_ioctl_lock);
937 if (fs_info->quota_root)
940 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
941 if (!fs_info->qgroup_ulist) {
947 * 1 for quota root item
948 * 1 for BTRFS_QGROUP_STATUS item
950 * Yet we also need 2*n items for a QGROUP_INFO/QGROUP_LIMIT items
951 * per subvolume. However those are not currently reserved since it
952 * would be a lot of overkill.
954 trans = btrfs_start_transaction(tree_root, 2);
956 ret = PTR_ERR(trans);
962 * initially create the quota tree
964 quota_root = btrfs_create_tree(trans, BTRFS_QUOTA_TREE_OBJECTID);
965 if (IS_ERR(quota_root)) {
966 ret = PTR_ERR(quota_root);
967 btrfs_abort_transaction(trans, ret);
971 path = btrfs_alloc_path();
974 btrfs_abort_transaction(trans, ret);
979 key.type = BTRFS_QGROUP_STATUS_KEY;
982 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
985 btrfs_abort_transaction(trans, ret);
989 leaf = path->nodes[0];
990 ptr = btrfs_item_ptr(leaf, path->slots[0],
991 struct btrfs_qgroup_status_item);
992 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
993 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
994 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
995 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
996 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
997 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
999 btrfs_mark_buffer_dirty(leaf);
1002 key.type = BTRFS_ROOT_REF_KEY;
1005 btrfs_release_path(path);
1006 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
1010 btrfs_abort_transaction(trans, ret);
1015 slot = path->slots[0];
1016 leaf = path->nodes[0];
1017 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1019 if (found_key.type == BTRFS_ROOT_REF_KEY) {
1020 ret = add_qgroup_item(trans, quota_root,
1023 btrfs_abort_transaction(trans, ret);
1027 qgroup = add_qgroup_rb(fs_info, found_key.offset);
1028 if (IS_ERR(qgroup)) {
1029 ret = PTR_ERR(qgroup);
1030 btrfs_abort_transaction(trans, ret);
1034 ret = btrfs_next_item(tree_root, path);
1036 btrfs_abort_transaction(trans, ret);
1044 btrfs_release_path(path);
1045 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
1047 btrfs_abort_transaction(trans, ret);
1051 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
1052 if (IS_ERR(qgroup)) {
1053 ret = PTR_ERR(qgroup);
1054 btrfs_abort_transaction(trans, ret);
1058 ret = btrfs_commit_transaction(trans);
1064 * Set quota enabled flag after committing the transaction, to avoid
1065 * deadlocks on fs_info->qgroup_ioctl_lock with concurrent snapshot
1068 spin_lock(&fs_info->qgroup_lock);
1069 fs_info->quota_root = quota_root;
1070 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1071 spin_unlock(&fs_info->qgroup_lock);
1073 ret = qgroup_rescan_init(fs_info, 0, 1);
1075 qgroup_rescan_zero_tracking(fs_info);
1076 fs_info->qgroup_rescan_running = true;
1077 btrfs_queue_work(fs_info->qgroup_rescan_workers,
1078 &fs_info->qgroup_rescan_work);
1082 btrfs_free_path(path);
1085 btrfs_put_root(quota_root);
1088 ulist_free(fs_info->qgroup_ulist);
1089 fs_info->qgroup_ulist = NULL;
1091 btrfs_end_transaction(trans);
1093 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1097 int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
1099 struct btrfs_root *quota_root;
1100 struct btrfs_trans_handle *trans = NULL;
1103 mutex_lock(&fs_info->qgroup_ioctl_lock);
1104 if (!fs_info->quota_root)
1108 * 1 For the root item
1110 * We should also reserve enough items for the quota tree deletion in
1111 * btrfs_clean_quota_tree but this is not done.
1113 trans = btrfs_start_transaction(fs_info->tree_root, 1);
1114 if (IS_ERR(trans)) {
1115 ret = PTR_ERR(trans);
1119 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1120 btrfs_qgroup_wait_for_completion(fs_info, false);
1121 spin_lock(&fs_info->qgroup_lock);
1122 quota_root = fs_info->quota_root;
1123 fs_info->quota_root = NULL;
1124 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1125 spin_unlock(&fs_info->qgroup_lock);
1127 btrfs_free_qgroup_config(fs_info);
1129 ret = btrfs_clean_quota_tree(trans, quota_root);
1131 btrfs_abort_transaction(trans, ret);
1135 ret = btrfs_del_root(trans, "a_root->root_key);
1137 btrfs_abort_transaction(trans, ret);
1141 list_del("a_root->dirty_list);
1143 btrfs_tree_lock(quota_root->node);
1144 btrfs_clean_tree_block(quota_root->node);
1145 btrfs_tree_unlock(quota_root->node);
1146 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1148 btrfs_put_root(quota_root);
1151 ret = btrfs_end_transaction(trans);
1153 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1157 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1158 struct btrfs_qgroup *qgroup)
1160 if (list_empty(&qgroup->dirty))
1161 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1165 * The easy accounting, we're updating qgroup relationship whose child qgroup
1166 * only has exclusive extents.
1168 * In this case, all exclusive extents will also be exclusive for parent, so
1169 * excl/rfer just get added/removed.
1171 * So is qgroup reservation space, which should also be added/removed to
1173 * Or when child tries to release reservation space, parent will underflow its
1174 * reservation (for relationship adding case).
1176 * Caller should hold fs_info->qgroup_lock.
1178 static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1179 struct ulist *tmp, u64 ref_root,
1180 struct btrfs_qgroup *src, int sign)
1182 struct btrfs_qgroup *qgroup;
1183 struct btrfs_qgroup_list *glist;
1184 struct ulist_node *unode;
1185 struct ulist_iterator uiter;
1186 u64 num_bytes = src->excl;
1189 qgroup = find_qgroup_rb(fs_info, ref_root);
1193 qgroup->rfer += sign * num_bytes;
1194 qgroup->rfer_cmpr += sign * num_bytes;
1196 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1197 qgroup->excl += sign * num_bytes;
1198 qgroup->excl_cmpr += sign * num_bytes;
1201 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1203 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1205 qgroup_dirty(fs_info, qgroup);
1207 /* Get all of the parent groups that contain this qgroup */
1208 list_for_each_entry(glist, &qgroup->groups, next_group) {
1209 ret = ulist_add(tmp, glist->group->qgroupid,
1210 qgroup_to_aux(glist->group), GFP_ATOMIC);
1215 /* Iterate all of the parents and adjust their reference counts */
1216 ULIST_ITER_INIT(&uiter);
1217 while ((unode = ulist_next(tmp, &uiter))) {
1218 qgroup = unode_aux_to_qgroup(unode);
1219 qgroup->rfer += sign * num_bytes;
1220 qgroup->rfer_cmpr += sign * num_bytes;
1221 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1222 qgroup->excl += sign * num_bytes;
1224 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1226 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1227 qgroup->excl_cmpr += sign * num_bytes;
1228 qgroup_dirty(fs_info, qgroup);
1230 /* Add any parents of the parents */
1231 list_for_each_entry(glist, &qgroup->groups, next_group) {
1232 ret = ulist_add(tmp, glist->group->qgroupid,
1233 qgroup_to_aux(glist->group), GFP_ATOMIC);
1245 * Quick path for updating qgroup with only excl refs.
1247 * In that case, just update all parent will be enough.
1248 * Or we needs to do a full rescan.
1249 * Caller should also hold fs_info->qgroup_lock.
1251 * Return 0 for quick update, return >0 for need to full rescan
1252 * and mark INCONSISTENT flag.
1253 * Return < 0 for other error.
1255 static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1256 struct ulist *tmp, u64 src, u64 dst,
1259 struct btrfs_qgroup *qgroup;
1263 qgroup = find_qgroup_rb(fs_info, src);
1266 if (qgroup->excl == qgroup->rfer) {
1268 err = __qgroup_excl_accounting(fs_info, tmp, dst,
1277 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1281 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1284 struct btrfs_fs_info *fs_info = trans->fs_info;
1285 struct btrfs_qgroup *parent;
1286 struct btrfs_qgroup *member;
1287 struct btrfs_qgroup_list *list;
1291 /* Check the level of src and dst first */
1292 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1295 tmp = ulist_alloc(GFP_KERNEL);
1299 mutex_lock(&fs_info->qgroup_ioctl_lock);
1300 if (!fs_info->quota_root) {
1304 member = find_qgroup_rb(fs_info, src);
1305 parent = find_qgroup_rb(fs_info, dst);
1306 if (!member || !parent) {
1311 /* check if such qgroup relation exist firstly */
1312 list_for_each_entry(list, &member->groups, next_group) {
1313 if (list->group == parent) {
1319 ret = add_qgroup_relation_item(trans, src, dst);
1323 ret = add_qgroup_relation_item(trans, dst, src);
1325 del_qgroup_relation_item(trans, src, dst);
1329 spin_lock(&fs_info->qgroup_lock);
1330 ret = add_relation_rb(fs_info, src, dst);
1332 spin_unlock(&fs_info->qgroup_lock);
1335 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
1336 spin_unlock(&fs_info->qgroup_lock);
1338 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1343 static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1346 struct btrfs_fs_info *fs_info = trans->fs_info;
1347 struct btrfs_qgroup *parent;
1348 struct btrfs_qgroup *member;
1349 struct btrfs_qgroup_list *list;
1355 tmp = ulist_alloc(GFP_KERNEL);
1359 if (!fs_info->quota_root) {
1364 member = find_qgroup_rb(fs_info, src);
1365 parent = find_qgroup_rb(fs_info, dst);
1367 * The parent/member pair doesn't exist, then try to delete the dead
1368 * relation items only.
1370 if (!member || !parent)
1373 /* check if such qgroup relation exist firstly */
1374 list_for_each_entry(list, &member->groups, next_group) {
1375 if (list->group == parent) {
1382 ret = del_qgroup_relation_item(trans, src, dst);
1383 if (ret < 0 && ret != -ENOENT)
1385 ret2 = del_qgroup_relation_item(trans, dst, src);
1386 if (ret2 < 0 && ret2 != -ENOENT)
1389 /* At least one deletion succeeded, return 0 */
1394 spin_lock(&fs_info->qgroup_lock);
1395 del_relation_rb(fs_info, src, dst);
1396 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
1397 spin_unlock(&fs_info->qgroup_lock);
1404 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1407 struct btrfs_fs_info *fs_info = trans->fs_info;
1410 mutex_lock(&fs_info->qgroup_ioctl_lock);
1411 ret = __del_qgroup_relation(trans, src, dst);
1412 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1417 int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
1419 struct btrfs_fs_info *fs_info = trans->fs_info;
1420 struct btrfs_root *quota_root;
1421 struct btrfs_qgroup *qgroup;
1424 mutex_lock(&fs_info->qgroup_ioctl_lock);
1425 if (!fs_info->quota_root) {
1429 quota_root = fs_info->quota_root;
1430 qgroup = find_qgroup_rb(fs_info, qgroupid);
1436 ret = add_qgroup_item(trans, quota_root, qgroupid);
1440 spin_lock(&fs_info->qgroup_lock);
1441 qgroup = add_qgroup_rb(fs_info, qgroupid);
1442 spin_unlock(&fs_info->qgroup_lock);
1445 ret = PTR_ERR(qgroup);
1447 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1451 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
1453 struct btrfs_fs_info *fs_info = trans->fs_info;
1454 struct btrfs_qgroup *qgroup;
1455 struct btrfs_qgroup_list *list;
1458 mutex_lock(&fs_info->qgroup_ioctl_lock);
1459 if (!fs_info->quota_root) {
1464 qgroup = find_qgroup_rb(fs_info, qgroupid);
1470 /* Check if there are no children of this qgroup */
1471 if (!list_empty(&qgroup->members)) {
1476 ret = del_qgroup_item(trans, qgroupid);
1477 if (ret && ret != -ENOENT)
1480 while (!list_empty(&qgroup->groups)) {
1481 list = list_first_entry(&qgroup->groups,
1482 struct btrfs_qgroup_list, next_group);
1483 ret = __del_qgroup_relation(trans, qgroupid,
1484 list->group->qgroupid);
1489 spin_lock(&fs_info->qgroup_lock);
1490 del_qgroup_rb(fs_info, qgroupid);
1491 spin_unlock(&fs_info->qgroup_lock);
1493 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1497 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,
1498 struct btrfs_qgroup_limit *limit)
1500 struct btrfs_fs_info *fs_info = trans->fs_info;
1501 struct btrfs_qgroup *qgroup;
1503 /* Sometimes we would want to clear the limit on this qgroup.
1504 * To meet this requirement, we treat the -1 as a special value
1505 * which tell kernel to clear the limit on this qgroup.
1507 const u64 CLEAR_VALUE = -1;
1509 mutex_lock(&fs_info->qgroup_ioctl_lock);
1510 if (!fs_info->quota_root) {
1515 qgroup = find_qgroup_rb(fs_info, qgroupid);
1521 spin_lock(&fs_info->qgroup_lock);
1522 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1523 if (limit->max_rfer == CLEAR_VALUE) {
1524 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1525 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1526 qgroup->max_rfer = 0;
1528 qgroup->max_rfer = limit->max_rfer;
1531 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1532 if (limit->max_excl == CLEAR_VALUE) {
1533 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1534 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1535 qgroup->max_excl = 0;
1537 qgroup->max_excl = limit->max_excl;
1540 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1541 if (limit->rsv_rfer == CLEAR_VALUE) {
1542 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1543 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1544 qgroup->rsv_rfer = 0;
1546 qgroup->rsv_rfer = limit->rsv_rfer;
1549 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1550 if (limit->rsv_excl == CLEAR_VALUE) {
1551 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1552 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1553 qgroup->rsv_excl = 0;
1555 qgroup->rsv_excl = limit->rsv_excl;
1558 qgroup->lim_flags |= limit->flags;
1560 spin_unlock(&fs_info->qgroup_lock);
1562 ret = update_qgroup_limit_item(trans, qgroup);
1564 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1565 btrfs_info(fs_info, "unable to update quota limit for %llu",
1570 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1574 int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
1575 struct btrfs_delayed_ref_root *delayed_refs,
1576 struct btrfs_qgroup_extent_record *record)
1578 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1579 struct rb_node *parent_node = NULL;
1580 struct btrfs_qgroup_extent_record *entry;
1581 u64 bytenr = record->bytenr;
1583 lockdep_assert_held(&delayed_refs->lock);
1584 trace_btrfs_qgroup_trace_extent(fs_info, record);
1588 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1590 if (bytenr < entry->bytenr) {
1592 } else if (bytenr > entry->bytenr) {
1593 p = &(*p)->rb_right;
1595 if (record->data_rsv && !entry->data_rsv) {
1596 entry->data_rsv = record->data_rsv;
1597 entry->data_rsv_refroot =
1598 record->data_rsv_refroot;
1604 rb_link_node(&record->node, parent_node, p);
1605 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
1609 int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1610 struct btrfs_qgroup_extent_record *qrecord)
1612 struct ulist *old_root;
1613 u64 bytenr = qrecord->bytenr;
1616 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
1618 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1620 "error accounting new delayed refs extent (err code: %d), quota inconsistent",
1626 * Here we don't need to get the lock of
1627 * trans->transaction->delayed_refs, since inserted qrecord won't
1628 * be deleted, only qrecord->node may be modified (new qrecord insert)
1630 * So modifying qrecord->old_roots is safe here
1632 qrecord->old_roots = old_root;
1636 int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
1637 u64 num_bytes, gfp_t gfp_flag)
1639 struct btrfs_fs_info *fs_info = trans->fs_info;
1640 struct btrfs_qgroup_extent_record *record;
1641 struct btrfs_delayed_ref_root *delayed_refs;
1644 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1645 || bytenr == 0 || num_bytes == 0)
1647 record = kzalloc(sizeof(*record), gfp_flag);
1651 delayed_refs = &trans->transaction->delayed_refs;
1652 record->bytenr = bytenr;
1653 record->num_bytes = num_bytes;
1654 record->old_roots = NULL;
1656 spin_lock(&delayed_refs->lock);
1657 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
1658 spin_unlock(&delayed_refs->lock);
1663 return btrfs_qgroup_trace_extent_post(fs_info, record);
1666 int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
1667 struct extent_buffer *eb)
1669 struct btrfs_fs_info *fs_info = trans->fs_info;
1670 int nr = btrfs_header_nritems(eb);
1671 int i, extent_type, ret;
1672 struct btrfs_key key;
1673 struct btrfs_file_extent_item *fi;
1674 u64 bytenr, num_bytes;
1676 /* We can be called directly from walk_up_proc() */
1677 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1680 for (i = 0; i < nr; i++) {
1681 btrfs_item_key_to_cpu(eb, &key, i);
1683 if (key.type != BTRFS_EXTENT_DATA_KEY)
1686 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1687 /* filter out non qgroup-accountable extents */
1688 extent_type = btrfs_file_extent_type(eb, fi);
1690 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1693 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1697 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1699 ret = btrfs_qgroup_trace_extent(trans, bytenr, num_bytes,
1709 * Walk up the tree from the bottom, freeing leaves and any interior
1710 * nodes which have had all slots visited. If a node (leaf or
1711 * interior) is freed, the node above it will have it's slot
1712 * incremented. The root node will never be freed.
1714 * At the end of this function, we should have a path which has all
1715 * slots incremented to the next position for a search. If we need to
1716 * read a new node it will be NULL and the node above it will have the
1717 * correct slot selected for a later read.
1719 * If we increment the root nodes slot counter past the number of
1720 * elements, 1 is returned to signal completion of the search.
1722 static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
1726 struct extent_buffer *eb;
1728 if (root_level == 0)
1731 while (level <= root_level) {
1732 eb = path->nodes[level];
1733 nr = btrfs_header_nritems(eb);
1734 path->slots[level]++;
1735 slot = path->slots[level];
1736 if (slot >= nr || level == 0) {
1738 * Don't free the root - we will detect this
1739 * condition after our loop and return a
1740 * positive value for caller to stop walking the tree.
1742 if (level != root_level) {
1743 btrfs_tree_unlock_rw(eb, path->locks[level]);
1744 path->locks[level] = 0;
1746 free_extent_buffer(eb);
1747 path->nodes[level] = NULL;
1748 path->slots[level] = 0;
1752 * We have a valid slot to walk back down
1753 * from. Stop here so caller can process these
1762 eb = path->nodes[root_level];
1763 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1770 * Helper function to trace a subtree tree block swap.
1772 * The swap will happen in highest tree block, but there may be a lot of
1773 * tree blocks involved.
1776 * OO = Old tree blocks
1777 * NN = New tree blocks allocated during balance
1779 * File tree (257) Reloc tree for 257
1782 * L1 OO OO (a) OO NN (a)
1784 * L0 OO OO OO OO OO OO NN NN
1787 * When calling qgroup_trace_extent_swap(), we will pass:
1789 * @dst_path = [ nodes[1] = NN(a), nodes[0] = NN(c) ]
1793 * In that case, qgroup_trace_extent_swap() will search from OO(a) to
1794 * reach OO(c), then mark both OO(c) and NN(c) as qgroup dirty.
1796 * The main work of qgroup_trace_extent_swap() can be split into 3 parts:
1798 * 1) Tree search from @src_eb
1799 * It should acts as a simplified btrfs_search_slot().
1800 * The key for search can be extracted from @dst_path->nodes[dst_level]
1803 * 2) Mark the final tree blocks in @src_path and @dst_path qgroup dirty
1804 * NOTE: In above case, OO(a) and NN(a) won't be marked qgroup dirty.
1805 * They should be marked during previous (@dst_level = 1) iteration.
1807 * 3) Mark file extents in leaves dirty
1808 * We don't have good way to pick out new file extents only.
1809 * So we still follow the old method by scanning all file extents in
1812 * This function can free us from keeping two paths, thus later we only need
1813 * to care about how to iterate all new tree blocks in reloc tree.
1815 static int qgroup_trace_extent_swap(struct btrfs_trans_handle* trans,
1816 struct extent_buffer *src_eb,
1817 struct btrfs_path *dst_path,
1818 int dst_level, int root_level,
1821 struct btrfs_key key;
1822 struct btrfs_path *src_path;
1823 struct btrfs_fs_info *fs_info = trans->fs_info;
1824 u32 nodesize = fs_info->nodesize;
1825 int cur_level = root_level;
1828 BUG_ON(dst_level > root_level);
1829 /* Level mismatch */
1830 if (btrfs_header_level(src_eb) != root_level)
1833 src_path = btrfs_alloc_path();
1840 btrfs_node_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
1842 btrfs_item_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
1845 atomic_inc(&src_eb->refs);
1846 src_path->nodes[root_level] = src_eb;
1847 src_path->slots[root_level] = dst_path->slots[root_level];
1848 src_path->locks[root_level] = 0;
1850 /* A simplified version of btrfs_search_slot() */
1851 while (cur_level >= dst_level) {
1852 struct btrfs_key src_key;
1853 struct btrfs_key dst_key;
1855 if (src_path->nodes[cur_level] == NULL) {
1856 struct btrfs_key first_key;
1857 struct extent_buffer *eb;
1862 eb = src_path->nodes[cur_level + 1];
1863 parent_slot = src_path->slots[cur_level + 1];
1864 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1865 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
1866 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
1868 eb = read_tree_block(fs_info, child_bytenr, child_gen,
1869 cur_level, &first_key);
1873 } else if (!extent_buffer_uptodate(eb)) {
1874 free_extent_buffer(eb);
1879 src_path->nodes[cur_level] = eb;
1881 btrfs_tree_read_lock(eb);
1882 btrfs_set_lock_blocking_read(eb);
1883 src_path->locks[cur_level] = BTRFS_READ_LOCK_BLOCKING;
1886 src_path->slots[cur_level] = dst_path->slots[cur_level];
1888 btrfs_node_key_to_cpu(dst_path->nodes[cur_level],
1889 &dst_key, dst_path->slots[cur_level]);
1890 btrfs_node_key_to_cpu(src_path->nodes[cur_level],
1891 &src_key, src_path->slots[cur_level]);
1893 btrfs_item_key_to_cpu(dst_path->nodes[cur_level],
1894 &dst_key, dst_path->slots[cur_level]);
1895 btrfs_item_key_to_cpu(src_path->nodes[cur_level],
1896 &src_key, src_path->slots[cur_level]);
1898 /* Content mismatch, something went wrong */
1899 if (btrfs_comp_cpu_keys(&dst_key, &src_key)) {
1907 * Now both @dst_path and @src_path have been populated, record the tree
1908 * blocks for qgroup accounting.
1910 ret = btrfs_qgroup_trace_extent(trans, src_path->nodes[dst_level]->start,
1911 nodesize, GFP_NOFS);
1914 ret = btrfs_qgroup_trace_extent(trans,
1915 dst_path->nodes[dst_level]->start,
1916 nodesize, GFP_NOFS);
1920 /* Record leaf file extents */
1921 if (dst_level == 0 && trace_leaf) {
1922 ret = btrfs_qgroup_trace_leaf_items(trans, src_path->nodes[0]);
1925 ret = btrfs_qgroup_trace_leaf_items(trans, dst_path->nodes[0]);
1928 btrfs_free_path(src_path);
1933 * Helper function to do recursive generation-aware depth-first search, to
1934 * locate all new tree blocks in a subtree of reloc tree.
1936 * E.g. (OO = Old tree blocks, NN = New tree blocks, whose gen == last_snapshot)
1945 * @dst_path = [ nodes[1] = NN(b), nodes[0] = NULL ],
1949 * We will iterate through tree blocks NN(b), NN(d) and info qgroup to trace
1950 * above tree blocks along with their counter parts in file tree.
1951 * While during search, old tree blocks OO(c) will be skipped as tree block swap
1952 * won't affect OO(c).
1954 static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans,
1955 struct extent_buffer *src_eb,
1956 struct btrfs_path *dst_path,
1957 int cur_level, int root_level,
1958 u64 last_snapshot, bool trace_leaf)
1960 struct btrfs_fs_info *fs_info = trans->fs_info;
1961 struct extent_buffer *eb;
1962 bool need_cleanup = false;
1966 /* Level sanity check */
1967 if (cur_level < 0 || cur_level >= BTRFS_MAX_LEVEL - 1 ||
1968 root_level < 0 || root_level >= BTRFS_MAX_LEVEL - 1 ||
1969 root_level < cur_level) {
1970 btrfs_err_rl(fs_info,
1971 "%s: bad levels, cur_level=%d root_level=%d",
1972 __func__, cur_level, root_level);
1976 /* Read the tree block if needed */
1977 if (dst_path->nodes[cur_level] == NULL) {
1978 struct btrfs_key first_key;
1984 * dst_path->nodes[root_level] must be initialized before
1985 * calling this function.
1987 if (cur_level == root_level) {
1988 btrfs_err_rl(fs_info,
1989 "%s: dst_path->nodes[%d] not initialized, root_level=%d cur_level=%d",
1990 __func__, root_level, root_level, cur_level);
1995 * We need to get child blockptr/gen from parent before we can
1998 eb = dst_path->nodes[cur_level + 1];
1999 parent_slot = dst_path->slots[cur_level + 1];
2000 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
2001 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
2002 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
2004 /* This node is old, no need to trace */
2005 if (child_gen < last_snapshot)
2008 eb = read_tree_block(fs_info, child_bytenr, child_gen,
2009 cur_level, &first_key);
2013 } else if (!extent_buffer_uptodate(eb)) {
2014 free_extent_buffer(eb);
2019 dst_path->nodes[cur_level] = eb;
2020 dst_path->slots[cur_level] = 0;
2022 btrfs_tree_read_lock(eb);
2023 btrfs_set_lock_blocking_read(eb);
2024 dst_path->locks[cur_level] = BTRFS_READ_LOCK_BLOCKING;
2025 need_cleanup = true;
2028 /* Now record this tree block and its counter part for qgroups */
2029 ret = qgroup_trace_extent_swap(trans, src_eb, dst_path, cur_level,
2030 root_level, trace_leaf);
2034 eb = dst_path->nodes[cur_level];
2036 if (cur_level > 0) {
2037 /* Iterate all child tree blocks */
2038 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2039 /* Skip old tree blocks as they won't be swapped */
2040 if (btrfs_node_ptr_generation(eb, i) < last_snapshot)
2042 dst_path->slots[cur_level] = i;
2044 /* Recursive call (at most 7 times) */
2045 ret = qgroup_trace_new_subtree_blocks(trans, src_eb,
2046 dst_path, cur_level - 1, root_level,
2047 last_snapshot, trace_leaf);
2056 btrfs_tree_unlock_rw(dst_path->nodes[cur_level],
2057 dst_path->locks[cur_level]);
2058 free_extent_buffer(dst_path->nodes[cur_level]);
2059 dst_path->nodes[cur_level] = NULL;
2060 dst_path->slots[cur_level] = 0;
2061 dst_path->locks[cur_level] = 0;
2067 static int qgroup_trace_subtree_swap(struct btrfs_trans_handle *trans,
2068 struct extent_buffer *src_eb,
2069 struct extent_buffer *dst_eb,
2070 u64 last_snapshot, bool trace_leaf)
2072 struct btrfs_fs_info *fs_info = trans->fs_info;
2073 struct btrfs_path *dst_path = NULL;
2077 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2080 /* Wrong parameter order */
2081 if (btrfs_header_generation(src_eb) > btrfs_header_generation(dst_eb)) {
2082 btrfs_err_rl(fs_info,
2083 "%s: bad parameter order, src_gen=%llu dst_gen=%llu", __func__,
2084 btrfs_header_generation(src_eb),
2085 btrfs_header_generation(dst_eb));
2089 if (!extent_buffer_uptodate(src_eb) || !extent_buffer_uptodate(dst_eb)) {
2094 level = btrfs_header_level(dst_eb);
2095 dst_path = btrfs_alloc_path();
2101 atomic_inc(&dst_eb->refs);
2102 dst_path->nodes[level] = dst_eb;
2103 dst_path->slots[level] = 0;
2104 dst_path->locks[level] = 0;
2106 /* Do the generation aware breadth-first search */
2107 ret = qgroup_trace_new_subtree_blocks(trans, src_eb, dst_path, level,
2108 level, last_snapshot, trace_leaf);
2114 btrfs_free_path(dst_path);
2116 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2120 int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
2121 struct extent_buffer *root_eb,
2122 u64 root_gen, int root_level)
2124 struct btrfs_fs_info *fs_info = trans->fs_info;
2127 struct extent_buffer *eb = root_eb;
2128 struct btrfs_path *path = NULL;
2130 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
2131 BUG_ON(root_eb == NULL);
2133 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2136 if (!extent_buffer_uptodate(root_eb)) {
2137 ret = btrfs_read_buffer(root_eb, root_gen, root_level, NULL);
2142 if (root_level == 0) {
2143 ret = btrfs_qgroup_trace_leaf_items(trans, root_eb);
2147 path = btrfs_alloc_path();
2152 * Walk down the tree. Missing extent blocks are filled in as
2153 * we go. Metadata is accounted every time we read a new
2156 * When we reach a leaf, we account for file extent items in it,
2157 * walk back up the tree (adjusting slot pointers as we go)
2158 * and restart the search process.
2160 atomic_inc(&root_eb->refs); /* For path */
2161 path->nodes[root_level] = root_eb;
2162 path->slots[root_level] = 0;
2163 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
2166 while (level >= 0) {
2167 if (path->nodes[level] == NULL) {
2168 struct btrfs_key first_key;
2174 * We need to get child blockptr/gen from parent before
2177 eb = path->nodes[level + 1];
2178 parent_slot = path->slots[level + 1];
2179 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
2180 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
2181 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
2183 eb = read_tree_block(fs_info, child_bytenr, child_gen,
2188 } else if (!extent_buffer_uptodate(eb)) {
2189 free_extent_buffer(eb);
2194 path->nodes[level] = eb;
2195 path->slots[level] = 0;
2197 btrfs_tree_read_lock(eb);
2198 btrfs_set_lock_blocking_read(eb);
2199 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
2201 ret = btrfs_qgroup_trace_extent(trans, child_bytenr,
2209 ret = btrfs_qgroup_trace_leaf_items(trans,
2210 path->nodes[level]);
2214 /* Nonzero return here means we completed our search */
2215 ret = adjust_slots_upwards(path, root_level);
2219 /* Restart search with new slots */
2228 btrfs_free_path(path);
2233 #define UPDATE_NEW 0
2234 #define UPDATE_OLD 1
2236 * Walk all of the roots that points to the bytenr and adjust their refcnts.
2238 static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
2239 struct ulist *roots, struct ulist *tmp,
2240 struct ulist *qgroups, u64 seq, int update_old)
2242 struct ulist_node *unode;
2243 struct ulist_iterator uiter;
2244 struct ulist_node *tmp_unode;
2245 struct ulist_iterator tmp_uiter;
2246 struct btrfs_qgroup *qg;
2251 ULIST_ITER_INIT(&uiter);
2252 while ((unode = ulist_next(roots, &uiter))) {
2253 qg = find_qgroup_rb(fs_info, unode->val);
2258 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
2262 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
2265 ULIST_ITER_INIT(&tmp_uiter);
2266 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
2267 struct btrfs_qgroup_list *glist;
2269 qg = unode_aux_to_qgroup(tmp_unode);
2271 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
2273 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
2274 list_for_each_entry(glist, &qg->groups, next_group) {
2275 ret = ulist_add(qgroups, glist->group->qgroupid,
2276 qgroup_to_aux(glist->group),
2280 ret = ulist_add(tmp, glist->group->qgroupid,
2281 qgroup_to_aux(glist->group),
2292 * Update qgroup rfer/excl counters.
2293 * Rfer update is easy, codes can explain themselves.
2295 * Excl update is tricky, the update is split into 2 part.
2296 * Part 1: Possible exclusive <-> sharing detect:
2298 * -------------------------------------
2300 * -------------------------------------
2302 * -------------------------------------
2305 * A: cur_old_roots < nr_old_roots (not exclusive before)
2306 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
2307 * B: cur_new_roots < nr_new_roots (not exclusive now)
2308 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
2311 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
2312 * *: Definitely not changed. **: Possible unchanged.
2314 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
2316 * To make the logic clear, we first use condition A and B to split
2317 * combination into 4 results.
2319 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
2320 * only on variant maybe 0.
2322 * Lastly, check result **, since there are 2 variants maybe 0, split them
2324 * But this time we don't need to consider other things, the codes and logic
2325 * is easy to understand now.
2327 static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
2328 struct ulist *qgroups,
2331 u64 num_bytes, u64 seq)
2333 struct ulist_node *unode;
2334 struct ulist_iterator uiter;
2335 struct btrfs_qgroup *qg;
2336 u64 cur_new_count, cur_old_count;
2338 ULIST_ITER_INIT(&uiter);
2339 while ((unode = ulist_next(qgroups, &uiter))) {
2342 qg = unode_aux_to_qgroup(unode);
2343 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
2344 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
2346 trace_qgroup_update_counters(fs_info, qg, cur_old_count,
2349 /* Rfer update part */
2350 if (cur_old_count == 0 && cur_new_count > 0) {
2351 qg->rfer += num_bytes;
2352 qg->rfer_cmpr += num_bytes;
2355 if (cur_old_count > 0 && cur_new_count == 0) {
2356 qg->rfer -= num_bytes;
2357 qg->rfer_cmpr -= num_bytes;
2361 /* Excl update part */
2362 /* Exclusive/none -> shared case */
2363 if (cur_old_count == nr_old_roots &&
2364 cur_new_count < nr_new_roots) {
2365 /* Exclusive -> shared */
2366 if (cur_old_count != 0) {
2367 qg->excl -= num_bytes;
2368 qg->excl_cmpr -= num_bytes;
2373 /* Shared -> exclusive/none case */
2374 if (cur_old_count < nr_old_roots &&
2375 cur_new_count == nr_new_roots) {
2376 /* Shared->exclusive */
2377 if (cur_new_count != 0) {
2378 qg->excl += num_bytes;
2379 qg->excl_cmpr += num_bytes;
2384 /* Exclusive/none -> exclusive/none case */
2385 if (cur_old_count == nr_old_roots &&
2386 cur_new_count == nr_new_roots) {
2387 if (cur_old_count == 0) {
2388 /* None -> exclusive/none */
2390 if (cur_new_count != 0) {
2391 /* None -> exclusive */
2392 qg->excl += num_bytes;
2393 qg->excl_cmpr += num_bytes;
2396 /* None -> none, nothing changed */
2398 /* Exclusive -> exclusive/none */
2400 if (cur_new_count == 0) {
2401 /* Exclusive -> none */
2402 qg->excl -= num_bytes;
2403 qg->excl_cmpr -= num_bytes;
2406 /* Exclusive -> exclusive, nothing changed */
2411 qgroup_dirty(fs_info, qg);
2417 * Check if the @roots potentially is a list of fs tree roots
2419 * Return 0 for definitely not a fs/subvol tree roots ulist
2420 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
2423 static int maybe_fs_roots(struct ulist *roots)
2425 struct ulist_node *unode;
2426 struct ulist_iterator uiter;
2428 /* Empty one, still possible for fs roots */
2429 if (!roots || roots->nnodes == 0)
2432 ULIST_ITER_INIT(&uiter);
2433 unode = ulist_next(roots, &uiter);
2438 * If it contains fs tree roots, then it must belong to fs/subvol
2440 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
2442 return is_fstree(unode->val);
2445 int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr,
2446 u64 num_bytes, struct ulist *old_roots,
2447 struct ulist *new_roots)
2449 struct btrfs_fs_info *fs_info = trans->fs_info;
2450 struct ulist *qgroups = NULL;
2451 struct ulist *tmp = NULL;
2453 u64 nr_new_roots = 0;
2454 u64 nr_old_roots = 0;
2458 * If quotas get disabled meanwhile, the resouces need to be freed and
2459 * we can't just exit here.
2461 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2465 if (!maybe_fs_roots(new_roots))
2467 nr_new_roots = new_roots->nnodes;
2470 if (!maybe_fs_roots(old_roots))
2472 nr_old_roots = old_roots->nnodes;
2475 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
2476 if (nr_old_roots == 0 && nr_new_roots == 0)
2479 BUG_ON(!fs_info->quota_root);
2481 trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
2482 num_bytes, nr_old_roots, nr_new_roots);
2484 qgroups = ulist_alloc(GFP_NOFS);
2489 tmp = ulist_alloc(GFP_NOFS);
2495 mutex_lock(&fs_info->qgroup_rescan_lock);
2496 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2497 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
2498 mutex_unlock(&fs_info->qgroup_rescan_lock);
2503 mutex_unlock(&fs_info->qgroup_rescan_lock);
2505 spin_lock(&fs_info->qgroup_lock);
2506 seq = fs_info->qgroup_seq;
2508 /* Update old refcnts using old_roots */
2509 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
2514 /* Update new refcnts using new_roots */
2515 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
2520 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
2524 * Bump qgroup_seq to avoid seq overlap
2526 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2528 spin_unlock(&fs_info->qgroup_lock);
2531 ulist_free(qgroups);
2532 ulist_free(old_roots);
2533 ulist_free(new_roots);
2537 int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
2539 struct btrfs_fs_info *fs_info = trans->fs_info;
2540 struct btrfs_qgroup_extent_record *record;
2541 struct btrfs_delayed_ref_root *delayed_refs;
2542 struct ulist *new_roots = NULL;
2543 struct rb_node *node;
2544 u64 num_dirty_extents = 0;
2548 delayed_refs = &trans->transaction->delayed_refs;
2549 qgroup_to_skip = delayed_refs->qgroup_to_skip;
2550 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2551 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2554 num_dirty_extents++;
2555 trace_btrfs_qgroup_account_extents(fs_info, record);
2559 * Old roots should be searched when inserting qgroup
2562 if (WARN_ON(!record->old_roots)) {
2563 /* Search commit root to find old_roots */
2564 ret = btrfs_find_all_roots(NULL, fs_info,
2566 &record->old_roots, false);
2571 /* Free the reserved data space */
2572 btrfs_qgroup_free_refroot(fs_info,
2573 record->data_rsv_refroot,
2575 BTRFS_QGROUP_RSV_DATA);
2577 * Use SEQ_LAST as time_seq to do special search, which
2578 * doesn't lock tree or delayed_refs and search current
2579 * root. It's safe inside commit_transaction().
2581 ret = btrfs_find_all_roots(trans, fs_info,
2582 record->bytenr, SEQ_LAST, &new_roots, false);
2585 if (qgroup_to_skip) {
2586 ulist_del(new_roots, qgroup_to_skip, 0);
2587 ulist_del(record->old_roots, qgroup_to_skip,
2590 ret = btrfs_qgroup_account_extent(trans, record->bytenr,
2594 record->old_roots = NULL;
2598 ulist_free(record->old_roots);
2599 ulist_free(new_roots);
2601 rb_erase(node, &delayed_refs->dirty_extent_root);
2605 trace_qgroup_num_dirty_extents(fs_info, trans->transid,
2611 * called from commit_transaction. Writes all changed qgroups to disk.
2613 int btrfs_run_qgroups(struct btrfs_trans_handle *trans)
2615 struct btrfs_fs_info *fs_info = trans->fs_info;
2618 if (!fs_info->quota_root)
2621 spin_lock(&fs_info->qgroup_lock);
2622 while (!list_empty(&fs_info->dirty_qgroups)) {
2623 struct btrfs_qgroup *qgroup;
2624 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2625 struct btrfs_qgroup, dirty);
2626 list_del_init(&qgroup->dirty);
2627 spin_unlock(&fs_info->qgroup_lock);
2628 ret = update_qgroup_info_item(trans, qgroup);
2630 fs_info->qgroup_flags |=
2631 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2632 ret = update_qgroup_limit_item(trans, qgroup);
2634 fs_info->qgroup_flags |=
2635 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2636 spin_lock(&fs_info->qgroup_lock);
2638 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2639 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2641 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2642 spin_unlock(&fs_info->qgroup_lock);
2644 ret = update_qgroup_status_item(trans);
2646 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2652 * Copy the accounting information between qgroups. This is necessary
2653 * when a snapshot or a subvolume is created. Throwing an error will
2654 * cause a transaction abort so we take extra care here to only error
2655 * when a readonly fs is a reasonable outcome.
2657 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
2658 u64 objectid, struct btrfs_qgroup_inherit *inherit)
2663 bool committing = false;
2664 struct btrfs_fs_info *fs_info = trans->fs_info;
2665 struct btrfs_root *quota_root;
2666 struct btrfs_qgroup *srcgroup;
2667 struct btrfs_qgroup *dstgroup;
2668 bool need_rescan = false;
2673 * There are only two callers of this function.
2675 * One in create_subvol() in the ioctl context, which needs to hold
2676 * the qgroup_ioctl_lock.
2678 * The other one in create_pending_snapshot() where no other qgroup
2679 * code can modify the fs as they all need to either start a new trans
2680 * or hold a trans handler, thus we don't need to hold
2681 * qgroup_ioctl_lock.
2682 * This would avoid long and complex lock chain and make lockdep happy.
2684 spin_lock(&fs_info->trans_lock);
2685 if (trans->transaction->state == TRANS_STATE_COMMIT_DOING)
2687 spin_unlock(&fs_info->trans_lock);
2690 mutex_lock(&fs_info->qgroup_ioctl_lock);
2691 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2694 quota_root = fs_info->quota_root;
2701 i_qgroups = (u64 *)(inherit + 1);
2702 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2703 2 * inherit->num_excl_copies;
2704 for (i = 0; i < nums; ++i) {
2705 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
2708 * Zero out invalid groups so we can ignore
2712 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2720 * create a tracking group for the subvol itself
2722 ret = add_qgroup_item(trans, quota_root, objectid);
2727 * add qgroup to all inherited groups
2730 i_qgroups = (u64 *)(inherit + 1);
2731 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2732 if (*i_qgroups == 0)
2734 ret = add_qgroup_relation_item(trans, objectid,
2736 if (ret && ret != -EEXIST)
2738 ret = add_qgroup_relation_item(trans, *i_qgroups,
2740 if (ret && ret != -EEXIST)
2747 spin_lock(&fs_info->qgroup_lock);
2749 dstgroup = add_qgroup_rb(fs_info, objectid);
2750 if (IS_ERR(dstgroup)) {
2751 ret = PTR_ERR(dstgroup);
2755 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
2756 dstgroup->lim_flags = inherit->lim.flags;
2757 dstgroup->max_rfer = inherit->lim.max_rfer;
2758 dstgroup->max_excl = inherit->lim.max_excl;
2759 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2760 dstgroup->rsv_excl = inherit->lim.rsv_excl;
2762 ret = update_qgroup_limit_item(trans, dstgroup);
2764 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2766 "unable to update quota limit for %llu",
2767 dstgroup->qgroupid);
2773 srcgroup = find_qgroup_rb(fs_info, srcid);
2778 * We call inherit after we clone the root in order to make sure
2779 * our counts don't go crazy, so at this point the only
2780 * difference between the two roots should be the root node.
2782 level_size = fs_info->nodesize;
2783 dstgroup->rfer = srcgroup->rfer;
2784 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2785 dstgroup->excl = level_size;
2786 dstgroup->excl_cmpr = level_size;
2787 srcgroup->excl = level_size;
2788 srcgroup->excl_cmpr = level_size;
2790 /* inherit the limit info */
2791 dstgroup->lim_flags = srcgroup->lim_flags;
2792 dstgroup->max_rfer = srcgroup->max_rfer;
2793 dstgroup->max_excl = srcgroup->max_excl;
2794 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2795 dstgroup->rsv_excl = srcgroup->rsv_excl;
2797 qgroup_dirty(fs_info, dstgroup);
2798 qgroup_dirty(fs_info, srcgroup);
2804 i_qgroups = (u64 *)(inherit + 1);
2805 for (i = 0; i < inherit->num_qgroups; ++i) {
2807 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
2814 * If we're doing a snapshot, and adding the snapshot to a new
2815 * qgroup, the numbers are guaranteed to be incorrect.
2821 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
2822 struct btrfs_qgroup *src;
2823 struct btrfs_qgroup *dst;
2825 if (!i_qgroups[0] || !i_qgroups[1])
2828 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2829 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2836 dst->rfer = src->rfer - level_size;
2837 dst->rfer_cmpr = src->rfer_cmpr - level_size;
2839 /* Manually tweaking numbers certainly needs a rescan */
2842 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
2843 struct btrfs_qgroup *src;
2844 struct btrfs_qgroup *dst;
2846 if (!i_qgroups[0] || !i_qgroups[1])
2849 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2850 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2857 dst->excl = src->excl + level_size;
2858 dst->excl_cmpr = src->excl_cmpr + level_size;
2863 spin_unlock(&fs_info->qgroup_lock);
2866 mutex_unlock(&fs_info->qgroup_ioctl_lock);
2868 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2873 * Two limits to commit transaction in advance.
2875 * For RATIO, it will be 1/RATIO of the remaining limit as threshold.
2876 * For SIZE, it will be in byte unit as threshold.
2878 #define QGROUP_FREE_RATIO 32
2879 #define QGROUP_FREE_SIZE SZ_32M
2880 static bool qgroup_check_limits(struct btrfs_fs_info *fs_info,
2881 const struct btrfs_qgroup *qg, u64 num_bytes)
2886 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
2887 qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
2890 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
2891 qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
2895 * Even if we passed the check, it's better to check if reservation
2896 * for meta_pertrans is pushing us near limit.
2897 * If there is too much pertrans reservation or it's near the limit,
2898 * let's try commit transaction to free some, using transaction_kthread
2900 if ((qg->lim_flags & (BTRFS_QGROUP_LIMIT_MAX_RFER |
2901 BTRFS_QGROUP_LIMIT_MAX_EXCL))) {
2902 if (qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
2903 free = qg->max_excl - qgroup_rsv_total(qg) - qg->excl;
2904 threshold = min_t(u64, qg->max_excl / QGROUP_FREE_RATIO,
2907 free = qg->max_rfer - qgroup_rsv_total(qg) - qg->rfer;
2908 threshold = min_t(u64, qg->max_rfer / QGROUP_FREE_RATIO,
2913 * Use transaction_kthread to commit transaction, so we no
2914 * longer need to bother nested transaction nor lock context.
2916 if (free < threshold)
2917 btrfs_commit_transaction_locksafe(fs_info);
2923 static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
2924 enum btrfs_qgroup_rsv_type type)
2926 struct btrfs_qgroup *qgroup;
2927 struct btrfs_fs_info *fs_info = root->fs_info;
2928 u64 ref_root = root->root_key.objectid;
2930 struct ulist_node *unode;
2931 struct ulist_iterator uiter;
2933 if (!is_fstree(ref_root))
2939 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
2940 capable(CAP_SYS_RESOURCE))
2943 spin_lock(&fs_info->qgroup_lock);
2944 if (!fs_info->quota_root)
2947 qgroup = find_qgroup_rb(fs_info, ref_root);
2952 * in a first step, we check all affected qgroups if any limits would
2955 ulist_reinit(fs_info->qgroup_ulist);
2956 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
2957 qgroup_to_aux(qgroup), GFP_ATOMIC);
2960 ULIST_ITER_INIT(&uiter);
2961 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2962 struct btrfs_qgroup *qg;
2963 struct btrfs_qgroup_list *glist;
2965 qg = unode_aux_to_qgroup(unode);
2967 if (enforce && !qgroup_check_limits(fs_info, qg, num_bytes)) {
2972 list_for_each_entry(glist, &qg->groups, next_group) {
2973 ret = ulist_add(fs_info->qgroup_ulist,
2974 glist->group->qgroupid,
2975 qgroup_to_aux(glist->group), GFP_ATOMIC);
2982 * no limits exceeded, now record the reservation into all qgroups
2984 ULIST_ITER_INIT(&uiter);
2985 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2986 struct btrfs_qgroup *qg;
2988 qg = unode_aux_to_qgroup(unode);
2990 qgroup_rsv_add(fs_info, qg, num_bytes, type);
2994 spin_unlock(&fs_info->qgroup_lock);
2999 * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0
3002 * Will handle all higher level qgroup too.
3004 * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
3005 * This special case is only used for META_PERTRANS type.
3007 void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
3008 u64 ref_root, u64 num_bytes,
3009 enum btrfs_qgroup_rsv_type type)
3011 struct btrfs_qgroup *qgroup;
3012 struct ulist_node *unode;
3013 struct ulist_iterator uiter;
3016 if (!is_fstree(ref_root))
3022 if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
3023 WARN(1, "%s: Invalid type to free", __func__);
3026 spin_lock(&fs_info->qgroup_lock);
3028 if (!fs_info->quota_root)
3031 qgroup = find_qgroup_rb(fs_info, ref_root);
3035 if (num_bytes == (u64)-1)
3037 * We're freeing all pertrans rsv, get reserved value from
3038 * level 0 qgroup as real num_bytes to free.
3040 num_bytes = qgroup->rsv.values[type];
3042 ulist_reinit(fs_info->qgroup_ulist);
3043 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3044 qgroup_to_aux(qgroup), GFP_ATOMIC);
3047 ULIST_ITER_INIT(&uiter);
3048 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3049 struct btrfs_qgroup *qg;
3050 struct btrfs_qgroup_list *glist;
3052 qg = unode_aux_to_qgroup(unode);
3054 qgroup_rsv_release(fs_info, qg, num_bytes, type);
3056 list_for_each_entry(glist, &qg->groups, next_group) {
3057 ret = ulist_add(fs_info->qgroup_ulist,
3058 glist->group->qgroupid,
3059 qgroup_to_aux(glist->group), GFP_ATOMIC);
3066 spin_unlock(&fs_info->qgroup_lock);
3070 * Check if the leaf is the last leaf. Which means all node pointers
3071 * are at their last position.
3073 static bool is_last_leaf(struct btrfs_path *path)
3077 for (i = 1; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
3078 if (path->slots[i] != btrfs_header_nritems(path->nodes[i]) - 1)
3085 * returns < 0 on error, 0 when more leafs are to be scanned.
3086 * returns 1 when done.
3088 static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans,
3089 struct btrfs_path *path)
3091 struct btrfs_fs_info *fs_info = trans->fs_info;
3092 struct btrfs_key found;
3093 struct extent_buffer *scratch_leaf = NULL;
3094 struct ulist *roots = NULL;
3100 mutex_lock(&fs_info->qgroup_rescan_lock);
3101 ret = btrfs_search_slot_for_read(fs_info->extent_root,
3102 &fs_info->qgroup_rescan_progress,
3105 btrfs_debug(fs_info,
3106 "current progress key (%llu %u %llu), search_slot ret %d",
3107 fs_info->qgroup_rescan_progress.objectid,
3108 fs_info->qgroup_rescan_progress.type,
3109 fs_info->qgroup_rescan_progress.offset, ret);
3113 * The rescan is about to end, we will not be scanning any
3114 * further blocks. We cannot unset the RESCAN flag here, because
3115 * we want to commit the transaction if everything went well.
3116 * To make the live accounting work in this phase, we set our
3117 * scan progress pointer such that every real extent objectid
3120 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
3121 btrfs_release_path(path);
3122 mutex_unlock(&fs_info->qgroup_rescan_lock);
3125 done = is_last_leaf(path);
3127 btrfs_item_key_to_cpu(path->nodes[0], &found,
3128 btrfs_header_nritems(path->nodes[0]) - 1);
3129 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
3131 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
3132 if (!scratch_leaf) {
3134 mutex_unlock(&fs_info->qgroup_rescan_lock);
3137 slot = path->slots[0];
3138 btrfs_release_path(path);
3139 mutex_unlock(&fs_info->qgroup_rescan_lock);
3141 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
3142 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
3143 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
3144 found.type != BTRFS_METADATA_ITEM_KEY)
3146 if (found.type == BTRFS_METADATA_ITEM_KEY)
3147 num_bytes = fs_info->nodesize;
3149 num_bytes = found.offset;
3151 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
3155 /* For rescan, just pass old_roots as NULL */
3156 ret = btrfs_qgroup_account_extent(trans, found.objectid,
3157 num_bytes, NULL, roots);
3163 free_extent_buffer(scratch_leaf);
3167 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
3172 static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
3174 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
3175 qgroup_rescan_work);
3176 struct btrfs_path *path;
3177 struct btrfs_trans_handle *trans = NULL;
3181 path = btrfs_alloc_path();
3185 * Rescan should only search for commit root, and any later difference
3186 * should be recorded by qgroup
3188 path->search_commit_root = 1;
3189 path->skip_locking = 1;
3192 while (!err && !btrfs_fs_closing(fs_info)) {
3193 trans = btrfs_start_transaction(fs_info->fs_root, 0);
3194 if (IS_ERR(trans)) {
3195 err = PTR_ERR(trans);
3198 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
3201 err = qgroup_rescan_leaf(trans, path);
3204 btrfs_commit_transaction(trans);
3206 btrfs_end_transaction(trans);
3210 btrfs_free_path(path);
3212 mutex_lock(&fs_info->qgroup_rescan_lock);
3214 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
3215 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3216 } else if (err < 0) {
3217 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3219 mutex_unlock(&fs_info->qgroup_rescan_lock);
3222 * only update status, since the previous part has already updated the
3225 trans = btrfs_start_transaction(fs_info->quota_root, 1);
3226 if (IS_ERR(trans)) {
3227 err = PTR_ERR(trans);
3230 "fail to start transaction for status update: %d",
3234 mutex_lock(&fs_info->qgroup_rescan_lock);
3235 if (!btrfs_fs_closing(fs_info))
3236 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3238 ret = update_qgroup_status_item(trans);
3241 btrfs_err(fs_info, "fail to update qgroup status: %d",
3245 fs_info->qgroup_rescan_running = false;
3246 complete_all(&fs_info->qgroup_rescan_completion);
3247 mutex_unlock(&fs_info->qgroup_rescan_lock);
3252 btrfs_end_transaction(trans);
3254 if (btrfs_fs_closing(fs_info)) {
3255 btrfs_info(fs_info, "qgroup scan paused");
3256 } else if (err >= 0) {
3257 btrfs_info(fs_info, "qgroup scan completed%s",
3258 err > 0 ? " (inconsistency flag cleared)" : "");
3260 btrfs_err(fs_info, "qgroup scan failed with %d", err);
3265 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
3266 * memory required for the rescan context.
3269 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
3275 /* we're resuming qgroup rescan at mount time */
3276 if (!(fs_info->qgroup_flags &
3277 BTRFS_QGROUP_STATUS_FLAG_RESCAN)) {
3279 "qgroup rescan init failed, qgroup rescan is not queued");
3281 } else if (!(fs_info->qgroup_flags &
3282 BTRFS_QGROUP_STATUS_FLAG_ON)) {
3284 "qgroup rescan init failed, qgroup is not enabled");
3292 mutex_lock(&fs_info->qgroup_rescan_lock);
3295 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3297 "qgroup rescan is already in progress");
3299 } else if (!(fs_info->qgroup_flags &
3300 BTRFS_QGROUP_STATUS_FLAG_ON)) {
3302 "qgroup rescan init failed, qgroup is not enabled");
3307 mutex_unlock(&fs_info->qgroup_rescan_lock);
3310 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3313 memset(&fs_info->qgroup_rescan_progress, 0,
3314 sizeof(fs_info->qgroup_rescan_progress));
3315 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
3316 init_completion(&fs_info->qgroup_rescan_completion);
3317 mutex_unlock(&fs_info->qgroup_rescan_lock);
3319 btrfs_init_work(&fs_info->qgroup_rescan_work,
3320 btrfs_qgroup_rescan_worker, NULL, NULL);
3325 qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
3328 struct btrfs_qgroup *qgroup;
3330 spin_lock(&fs_info->qgroup_lock);
3331 /* clear all current qgroup tracking information */
3332 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
3333 qgroup = rb_entry(n, struct btrfs_qgroup, node);
3335 qgroup->rfer_cmpr = 0;
3337 qgroup->excl_cmpr = 0;
3338 qgroup_dirty(fs_info, qgroup);
3340 spin_unlock(&fs_info->qgroup_lock);
3344 btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
3347 struct btrfs_trans_handle *trans;
3349 ret = qgroup_rescan_init(fs_info, 0, 1);
3354 * We have set the rescan_progress to 0, which means no more
3355 * delayed refs will be accounted by btrfs_qgroup_account_ref.
3356 * However, btrfs_qgroup_account_ref may be right after its call
3357 * to btrfs_find_all_roots, in which case it would still do the
3359 * To solve this, we're committing the transaction, which will
3360 * ensure we run all delayed refs and only after that, we are
3361 * going to clear all tracking information for a clean start.
3364 trans = btrfs_join_transaction(fs_info->fs_root);
3365 if (IS_ERR(trans)) {
3366 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3367 return PTR_ERR(trans);
3369 ret = btrfs_commit_transaction(trans);
3371 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3375 qgroup_rescan_zero_tracking(fs_info);
3377 mutex_lock(&fs_info->qgroup_rescan_lock);
3378 fs_info->qgroup_rescan_running = true;
3379 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3380 &fs_info->qgroup_rescan_work);
3381 mutex_unlock(&fs_info->qgroup_rescan_lock);
3386 int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
3392 mutex_lock(&fs_info->qgroup_rescan_lock);
3393 running = fs_info->qgroup_rescan_running;
3394 mutex_unlock(&fs_info->qgroup_rescan_lock);
3400 ret = wait_for_completion_interruptible(
3401 &fs_info->qgroup_rescan_completion);
3403 wait_for_completion(&fs_info->qgroup_rescan_completion);
3409 * this is only called from open_ctree where we're still single threaded, thus
3410 * locking is omitted here.
3413 btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
3415 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3416 mutex_lock(&fs_info->qgroup_rescan_lock);
3417 fs_info->qgroup_rescan_running = true;
3418 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3419 &fs_info->qgroup_rescan_work);
3420 mutex_unlock(&fs_info->qgroup_rescan_lock);
3425 * Reserve qgroup space for range [start, start + len).
3427 * This function will either reserve space from related qgroups or doing
3428 * nothing if the range is already reserved.
3430 * Return 0 for successful reserve
3431 * Return <0 for error (including -EQUOT)
3433 * NOTE: this function may sleep for memory allocation.
3434 * if btrfs_qgroup_reserve_data() is called multiple times with
3435 * same @reserved, caller must ensure when error happens it's OK
3436 * to free *ALL* reserved space.
3438 int btrfs_qgroup_reserve_data(struct btrfs_inode *inode,
3439 struct extent_changeset **reserved_ret, u64 start,
3442 struct btrfs_root *root = inode->root;
3443 struct ulist_node *unode;
3444 struct ulist_iterator uiter;
3445 struct extent_changeset *reserved;
3450 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
3451 !is_fstree(root->root_key.objectid) || len == 0)
3454 /* @reserved parameter is mandatory for qgroup */
3455 if (WARN_ON(!reserved_ret))
3457 if (!*reserved_ret) {
3458 *reserved_ret = extent_changeset_alloc();
3462 reserved = *reserved_ret;
3463 /* Record already reserved space */
3464 orig_reserved = reserved->bytes_changed;
3465 ret = set_record_extent_bits(&inode->io_tree, start,
3466 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
3468 /* Newly reserved space */
3469 to_reserve = reserved->bytes_changed - orig_reserved;
3470 trace_btrfs_qgroup_reserve_data(&inode->vfs_inode, start, len,
3471 to_reserve, QGROUP_RESERVE);
3474 ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
3481 /* cleanup *ALL* already reserved ranges */
3482 ULIST_ITER_INIT(&uiter);
3483 while ((unode = ulist_next(&reserved->range_changed, &uiter)))
3484 clear_extent_bit(&inode->io_tree, unode->val,
3485 unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL);
3486 /* Also free data bytes of already reserved one */
3487 btrfs_qgroup_free_refroot(root->fs_info, root->root_key.objectid,
3488 orig_reserved, BTRFS_QGROUP_RSV_DATA);
3489 extent_changeset_release(reserved);
3493 /* Free ranges specified by @reserved, normally in error path */
3494 static int qgroup_free_reserved_data(struct btrfs_inode *inode,
3495 struct extent_changeset *reserved, u64 start, u64 len)
3497 struct btrfs_root *root = inode->root;
3498 struct ulist_node *unode;
3499 struct ulist_iterator uiter;
3500 struct extent_changeset changeset;
3504 extent_changeset_init(&changeset);
3505 len = round_up(start + len, root->fs_info->sectorsize);
3506 start = round_down(start, root->fs_info->sectorsize);
3508 ULIST_ITER_INIT(&uiter);
3509 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
3510 u64 range_start = unode->val;
3511 /* unode->aux is the inclusive end */
3512 u64 range_len = unode->aux - range_start + 1;
3516 extent_changeset_release(&changeset);
3518 /* Only free range in range [start, start + len) */
3519 if (range_start >= start + len ||
3520 range_start + range_len <= start)
3522 free_start = max(range_start, start);
3523 free_len = min(start + len, range_start + range_len) -
3526 * TODO: To also modify reserved->ranges_reserved to reflect
3529 * However as long as we free qgroup reserved according to
3530 * EXTENT_QGROUP_RESERVED, we won't double free.
3531 * So not need to rush.
3533 ret = clear_record_extent_bits(&inode->io_tree, free_start,
3534 free_start + free_len - 1,
3535 EXTENT_QGROUP_RESERVED, &changeset);
3538 freed += changeset.bytes_changed;
3540 btrfs_qgroup_free_refroot(root->fs_info, root->root_key.objectid, freed,
3541 BTRFS_QGROUP_RSV_DATA);
3544 extent_changeset_release(&changeset);
3548 static int __btrfs_qgroup_release_data(struct btrfs_inode *inode,
3549 struct extent_changeset *reserved, u64 start, u64 len,
3552 struct extent_changeset changeset;
3553 int trace_op = QGROUP_RELEASE;
3556 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &inode->root->fs_info->flags))
3559 /* In release case, we shouldn't have @reserved */
3560 WARN_ON(!free && reserved);
3561 if (free && reserved)
3562 return qgroup_free_reserved_data(inode, reserved, start, len);
3563 extent_changeset_init(&changeset);
3564 ret = clear_record_extent_bits(&inode->io_tree, start, start + len -1,
3565 EXTENT_QGROUP_RESERVED, &changeset);
3570 trace_op = QGROUP_FREE;
3571 trace_btrfs_qgroup_release_data(&inode->vfs_inode, start, len,
3572 changeset.bytes_changed, trace_op);
3574 btrfs_qgroup_free_refroot(inode->root->fs_info,
3575 inode->root->root_key.objectid,
3576 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
3577 ret = changeset.bytes_changed;
3579 extent_changeset_release(&changeset);
3584 * Free a reserved space range from io_tree and related qgroups
3586 * Should be called when a range of pages get invalidated before reaching disk.
3587 * Or for error cleanup case.
3588 * if @reserved is given, only reserved range in [@start, @start + @len) will
3591 * For data written to disk, use btrfs_qgroup_release_data().
3593 * NOTE: This function may sleep for memory allocation.
3595 int btrfs_qgroup_free_data(struct btrfs_inode *inode,
3596 struct extent_changeset *reserved, u64 start, u64 len)
3598 return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
3602 * Release a reserved space range from io_tree only.
3604 * Should be called when a range of pages get written to disk and corresponding
3605 * FILE_EXTENT is inserted into corresponding root.
3607 * Since new qgroup accounting framework will only update qgroup numbers at
3608 * commit_transaction() time, its reserved space shouldn't be freed from
3611 * But we should release the range from io_tree, to allow further write to be
3614 * NOTE: This function may sleep for memory allocation.
3616 int btrfs_qgroup_release_data(struct btrfs_inode *inode, u64 start, u64 len)
3618 return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
3621 static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3622 enum btrfs_qgroup_rsv_type type)
3624 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3625 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3630 spin_lock(&root->qgroup_meta_rsv_lock);
3631 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
3632 root->qgroup_meta_rsv_prealloc += num_bytes;
3634 root->qgroup_meta_rsv_pertrans += num_bytes;
3635 spin_unlock(&root->qgroup_meta_rsv_lock);
3638 static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3639 enum btrfs_qgroup_rsv_type type)
3641 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3642 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3647 spin_lock(&root->qgroup_meta_rsv_lock);
3648 if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
3649 num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
3651 root->qgroup_meta_rsv_prealloc -= num_bytes;
3653 num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
3655 root->qgroup_meta_rsv_pertrans -= num_bytes;
3657 spin_unlock(&root->qgroup_meta_rsv_lock);
3661 int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3662 enum btrfs_qgroup_rsv_type type, bool enforce)
3664 struct btrfs_fs_info *fs_info = root->fs_info;
3667 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3668 !is_fstree(root->root_key.objectid) || num_bytes == 0)
3671 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
3672 trace_qgroup_meta_reserve(root, (s64)num_bytes, type);
3673 ret = qgroup_reserve(root, num_bytes, enforce, type);
3677 * Record what we have reserved into root.
3679 * To avoid quota disabled->enabled underflow.
3680 * In that case, we may try to free space we haven't reserved
3681 * (since quota was disabled), so record what we reserved into root.
3682 * And ensure later release won't underflow this number.
3684 add_root_meta_rsv(root, num_bytes, type);
3688 void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
3690 struct btrfs_fs_info *fs_info = root->fs_info;
3692 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3693 !is_fstree(root->root_key.objectid))
3696 /* TODO: Update trace point to handle such free */
3697 trace_qgroup_meta_free_all_pertrans(root);
3698 /* Special value -1 means to free all reserved space */
3699 btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid, (u64)-1,
3700 BTRFS_QGROUP_RSV_META_PERTRANS);
3703 void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
3704 enum btrfs_qgroup_rsv_type type)
3706 struct btrfs_fs_info *fs_info = root->fs_info;
3708 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3709 !is_fstree(root->root_key.objectid))
3713 * reservation for META_PREALLOC can happen before quota is enabled,
3714 * which can lead to underflow.
3715 * Here ensure we will only free what we really have reserved.
3717 num_bytes = sub_root_meta_rsv(root, num_bytes, type);
3718 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
3719 trace_qgroup_meta_reserve(root, -(s64)num_bytes, type);
3720 btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid,
3724 static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
3727 struct btrfs_qgroup *qgroup;
3728 struct ulist_node *unode;
3729 struct ulist_iterator uiter;
3734 if (!fs_info->quota_root)
3737 spin_lock(&fs_info->qgroup_lock);
3738 qgroup = find_qgroup_rb(fs_info, ref_root);
3741 ulist_reinit(fs_info->qgroup_ulist);
3742 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3743 qgroup_to_aux(qgroup), GFP_ATOMIC);
3746 ULIST_ITER_INIT(&uiter);
3747 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3748 struct btrfs_qgroup *qg;
3749 struct btrfs_qgroup_list *glist;
3751 qg = unode_aux_to_qgroup(unode);
3753 qgroup_rsv_release(fs_info, qg, num_bytes,
3754 BTRFS_QGROUP_RSV_META_PREALLOC);
3755 qgroup_rsv_add(fs_info, qg, num_bytes,
3756 BTRFS_QGROUP_RSV_META_PERTRANS);
3757 list_for_each_entry(glist, &qg->groups, next_group) {
3758 ret = ulist_add(fs_info->qgroup_ulist,
3759 glist->group->qgroupid,
3760 qgroup_to_aux(glist->group), GFP_ATOMIC);
3766 spin_unlock(&fs_info->qgroup_lock);
3769 void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
3771 struct btrfs_fs_info *fs_info = root->fs_info;
3773 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3774 !is_fstree(root->root_key.objectid))
3776 /* Same as btrfs_qgroup_free_meta_prealloc() */
3777 num_bytes = sub_root_meta_rsv(root, num_bytes,
3778 BTRFS_QGROUP_RSV_META_PREALLOC);
3779 trace_qgroup_meta_convert(root, num_bytes);
3780 qgroup_convert_meta(fs_info, root->root_key.objectid, num_bytes);
3784 * Check qgroup reserved space leaking, normally at destroy inode
3787 void btrfs_qgroup_check_reserved_leak(struct inode *inode)
3789 struct extent_changeset changeset;
3790 struct ulist_node *unode;
3791 struct ulist_iterator iter;
3794 extent_changeset_init(&changeset);
3795 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
3796 EXTENT_QGROUP_RESERVED, &changeset);
3799 if (WARN_ON(changeset.bytes_changed)) {
3800 ULIST_ITER_INIT(&iter);
3801 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
3802 btrfs_warn(BTRFS_I(inode)->root->fs_info,
3803 "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
3804 inode->i_ino, unode->val, unode->aux);
3806 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3807 BTRFS_I(inode)->root->root_key.objectid,
3808 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
3811 extent_changeset_release(&changeset);
3814 void btrfs_qgroup_init_swapped_blocks(
3815 struct btrfs_qgroup_swapped_blocks *swapped_blocks)
3819 spin_lock_init(&swapped_blocks->lock);
3820 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
3821 swapped_blocks->blocks[i] = RB_ROOT;
3822 swapped_blocks->swapped = false;
3826 * Delete all swapped blocks record of @root.
3827 * Every record here means we skipped a full subtree scan for qgroup.
3829 * Gets called when committing one transaction.
3831 void btrfs_qgroup_clean_swapped_blocks(struct btrfs_root *root)
3833 struct btrfs_qgroup_swapped_blocks *swapped_blocks;
3836 swapped_blocks = &root->swapped_blocks;
3838 spin_lock(&swapped_blocks->lock);
3839 if (!swapped_blocks->swapped)
3841 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3842 struct rb_root *cur_root = &swapped_blocks->blocks[i];
3843 struct btrfs_qgroup_swapped_block *entry;
3844 struct btrfs_qgroup_swapped_block *next;
3846 rbtree_postorder_for_each_entry_safe(entry, next, cur_root,
3849 swapped_blocks->blocks[i] = RB_ROOT;
3851 swapped_blocks->swapped = false;
3853 spin_unlock(&swapped_blocks->lock);
3857 * Add subtree roots record into @subvol_root.
3859 * @subvol_root: tree root of the subvolume tree get swapped
3860 * @bg: block group under balance
3861 * @subvol_parent/slot: pointer to the subtree root in subvolume tree
3862 * @reloc_parent/slot: pointer to the subtree root in reloc tree
3863 * BOTH POINTERS ARE BEFORE TREE SWAP
3864 * @last_snapshot: last snapshot generation of the subvolume tree
3866 int btrfs_qgroup_add_swapped_blocks(struct btrfs_trans_handle *trans,
3867 struct btrfs_root *subvol_root,
3868 struct btrfs_block_group *bg,
3869 struct extent_buffer *subvol_parent, int subvol_slot,
3870 struct extent_buffer *reloc_parent, int reloc_slot,
3873 struct btrfs_fs_info *fs_info = subvol_root->fs_info;
3874 struct btrfs_qgroup_swapped_blocks *blocks = &subvol_root->swapped_blocks;
3875 struct btrfs_qgroup_swapped_block *block;
3876 struct rb_node **cur;
3877 struct rb_node *parent = NULL;
3878 int level = btrfs_header_level(subvol_parent) - 1;
3881 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
3884 if (btrfs_node_ptr_generation(subvol_parent, subvol_slot) >
3885 btrfs_node_ptr_generation(reloc_parent, reloc_slot)) {
3886 btrfs_err_rl(fs_info,
3887 "%s: bad parameter order, subvol_gen=%llu reloc_gen=%llu",
3889 btrfs_node_ptr_generation(subvol_parent, subvol_slot),
3890 btrfs_node_ptr_generation(reloc_parent, reloc_slot));
3894 block = kmalloc(sizeof(*block), GFP_NOFS);
3901 * @reloc_parent/slot is still before swap, while @block is going to
3902 * record the bytenr after swap, so we do the swap here.
3904 block->subvol_bytenr = btrfs_node_blockptr(reloc_parent, reloc_slot);
3905 block->subvol_generation = btrfs_node_ptr_generation(reloc_parent,
3907 block->reloc_bytenr = btrfs_node_blockptr(subvol_parent, subvol_slot);
3908 block->reloc_generation = btrfs_node_ptr_generation(subvol_parent,
3910 block->last_snapshot = last_snapshot;
3911 block->level = level;
3914 * If we have bg == NULL, we're called from btrfs_recover_relocation(),
3915 * no one else can modify tree blocks thus we qgroup will not change
3916 * no matter the value of trace_leaf.
3918 if (bg && bg->flags & BTRFS_BLOCK_GROUP_DATA)
3919 block->trace_leaf = true;
3921 block->trace_leaf = false;
3922 btrfs_node_key_to_cpu(reloc_parent, &block->first_key, reloc_slot);
3924 /* Insert @block into @blocks */
3925 spin_lock(&blocks->lock);
3926 cur = &blocks->blocks[level].rb_node;
3928 struct btrfs_qgroup_swapped_block *entry;
3931 entry = rb_entry(parent, struct btrfs_qgroup_swapped_block,
3934 if (entry->subvol_bytenr < block->subvol_bytenr) {
3935 cur = &(*cur)->rb_left;
3936 } else if (entry->subvol_bytenr > block->subvol_bytenr) {
3937 cur = &(*cur)->rb_right;
3939 if (entry->subvol_generation !=
3940 block->subvol_generation ||
3941 entry->reloc_bytenr != block->reloc_bytenr ||
3942 entry->reloc_generation !=
3943 block->reloc_generation) {
3945 * Duplicated but mismatch entry found.
3948 * Marking qgroup inconsistent should be enough
3951 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
3958 rb_link_node(&block->node, parent, cur);
3959 rb_insert_color(&block->node, &blocks->blocks[level]);
3960 blocks->swapped = true;
3962 spin_unlock(&blocks->lock);
3965 fs_info->qgroup_flags |=
3966 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3971 * Check if the tree block is a subtree root, and if so do the needed
3972 * delayed subtree trace for qgroup.
3974 * This is called during btrfs_cow_block().
3976 int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans,
3977 struct btrfs_root *root,
3978 struct extent_buffer *subvol_eb)
3980 struct btrfs_fs_info *fs_info = root->fs_info;
3981 struct btrfs_qgroup_swapped_blocks *blocks = &root->swapped_blocks;
3982 struct btrfs_qgroup_swapped_block *block;
3983 struct extent_buffer *reloc_eb = NULL;
3984 struct rb_node *node;
3986 bool swapped = false;
3987 int level = btrfs_header_level(subvol_eb);
3991 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
3993 if (!is_fstree(root->root_key.objectid) || !root->reloc_root)
3996 spin_lock(&blocks->lock);
3997 if (!blocks->swapped) {
3998 spin_unlock(&blocks->lock);
4001 node = blocks->blocks[level].rb_node;
4004 block = rb_entry(node, struct btrfs_qgroup_swapped_block, node);
4005 if (block->subvol_bytenr < subvol_eb->start) {
4006 node = node->rb_left;
4007 } else if (block->subvol_bytenr > subvol_eb->start) {
4008 node = node->rb_right;
4015 spin_unlock(&blocks->lock);
4018 /* Found one, remove it from @blocks first and update blocks->swapped */
4019 rb_erase(&block->node, &blocks->blocks[level]);
4020 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4021 if (RB_EMPTY_ROOT(&blocks->blocks[i])) {
4026 blocks->swapped = swapped;
4027 spin_unlock(&blocks->lock);
4029 /* Read out reloc subtree root */
4030 reloc_eb = read_tree_block(fs_info, block->reloc_bytenr,
4031 block->reloc_generation, block->level,
4033 if (IS_ERR(reloc_eb)) {
4034 ret = PTR_ERR(reloc_eb);
4038 if (!extent_buffer_uptodate(reloc_eb)) {
4043 ret = qgroup_trace_subtree_swap(trans, reloc_eb, subvol_eb,
4044 block->last_snapshot, block->trace_leaf);
4047 free_extent_buffer(reloc_eb);
4050 btrfs_err_rl(fs_info,
4051 "failed to account subtree at bytenr %llu: %d",
4052 subvol_eb->start, ret);
4053 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
4058 void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans)
4060 struct btrfs_qgroup_extent_record *entry;
4061 struct btrfs_qgroup_extent_record *next;
4062 struct rb_root *root;
4064 root = &trans->delayed_refs.dirty_extent_root;
4065 rbtree_postorder_for_each_entry_safe(entry, next, root, node) {
4066 ulist_free(entry->old_roots);