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"
27 * - subvol delete -> delete when ref goes to 0? delete limits also?
31 * - copy also limits on subvol creation
33 * - caches fuer ulists
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;
509 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
510 * first two are in single-threaded paths.And for the third one, we have set
511 * quota_root to be null with qgroup_lock held before, so it is safe to clean
512 * up the in-memory structures without qgroup_lock held.
514 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
517 struct btrfs_qgroup *qgroup;
519 while ((n = rb_first(&fs_info->qgroup_tree))) {
520 qgroup = rb_entry(n, struct btrfs_qgroup, node);
521 rb_erase(n, &fs_info->qgroup_tree);
522 __del_qgroup_rb(qgroup);
525 * we call btrfs_free_qgroup_config() when umounting
526 * filesystem and disabling quota, so we set qgroup_ulist
527 * to be null here to avoid double free.
529 ulist_free(fs_info->qgroup_ulist);
530 fs_info->qgroup_ulist = NULL;
533 static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
534 struct btrfs_root *quota_root,
538 struct btrfs_path *path;
539 struct btrfs_key key;
541 path = btrfs_alloc_path();
546 key.type = BTRFS_QGROUP_RELATION_KEY;
549 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
551 btrfs_mark_buffer_dirty(path->nodes[0]);
553 btrfs_free_path(path);
557 static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
558 struct btrfs_root *quota_root,
562 struct btrfs_path *path;
563 struct btrfs_key key;
565 path = btrfs_alloc_path();
570 key.type = BTRFS_QGROUP_RELATION_KEY;
573 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
582 ret = btrfs_del_item(trans, quota_root, path);
584 btrfs_free_path(path);
588 static int add_qgroup_item(struct btrfs_trans_handle *trans,
589 struct btrfs_root *quota_root, u64 qgroupid)
592 struct btrfs_path *path;
593 struct btrfs_qgroup_info_item *qgroup_info;
594 struct btrfs_qgroup_limit_item *qgroup_limit;
595 struct extent_buffer *leaf;
596 struct btrfs_key key;
598 if (btrfs_is_testing(quota_root->fs_info))
601 path = btrfs_alloc_path();
606 key.type = BTRFS_QGROUP_INFO_KEY;
607 key.offset = qgroupid;
610 * Avoid a transaction abort by catching -EEXIST here. In that
611 * case, we proceed by re-initializing the existing structure
615 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
616 sizeof(*qgroup_info));
617 if (ret && ret != -EEXIST)
620 leaf = path->nodes[0];
621 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
622 struct btrfs_qgroup_info_item);
623 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
624 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
625 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
626 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
627 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
629 btrfs_mark_buffer_dirty(leaf);
631 btrfs_release_path(path);
633 key.type = BTRFS_QGROUP_LIMIT_KEY;
634 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
635 sizeof(*qgroup_limit));
636 if (ret && ret != -EEXIST)
639 leaf = path->nodes[0];
640 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
641 struct btrfs_qgroup_limit_item);
642 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
643 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
644 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
645 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
646 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
648 btrfs_mark_buffer_dirty(leaf);
652 btrfs_free_path(path);
656 static int del_qgroup_item(struct btrfs_trans_handle *trans,
657 struct btrfs_root *quota_root, u64 qgroupid)
660 struct btrfs_path *path;
661 struct btrfs_key key;
663 path = btrfs_alloc_path();
668 key.type = BTRFS_QGROUP_INFO_KEY;
669 key.offset = qgroupid;
670 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
679 ret = btrfs_del_item(trans, quota_root, path);
683 btrfs_release_path(path);
685 key.type = BTRFS_QGROUP_LIMIT_KEY;
686 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
695 ret = btrfs_del_item(trans, quota_root, path);
698 btrfs_free_path(path);
702 static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
703 struct btrfs_root *root,
704 struct btrfs_qgroup *qgroup)
706 struct btrfs_path *path;
707 struct btrfs_key key;
708 struct extent_buffer *l;
709 struct btrfs_qgroup_limit_item *qgroup_limit;
714 key.type = BTRFS_QGROUP_LIMIT_KEY;
715 key.offset = qgroup->qgroupid;
717 path = btrfs_alloc_path();
721 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
729 slot = path->slots[0];
730 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
731 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
732 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
733 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
734 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
735 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
737 btrfs_mark_buffer_dirty(l);
740 btrfs_free_path(path);
744 static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
745 struct btrfs_root *root,
746 struct btrfs_qgroup *qgroup)
748 struct btrfs_path *path;
749 struct btrfs_key key;
750 struct extent_buffer *l;
751 struct btrfs_qgroup_info_item *qgroup_info;
755 if (btrfs_is_testing(root->fs_info))
759 key.type = BTRFS_QGROUP_INFO_KEY;
760 key.offset = qgroup->qgroupid;
762 path = btrfs_alloc_path();
766 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
774 slot = path->slots[0];
775 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
776 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
777 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
778 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
779 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
780 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
782 btrfs_mark_buffer_dirty(l);
785 btrfs_free_path(path);
789 static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
790 struct btrfs_fs_info *fs_info,
791 struct btrfs_root *root)
793 struct btrfs_path *path;
794 struct btrfs_key key;
795 struct extent_buffer *l;
796 struct btrfs_qgroup_status_item *ptr;
801 key.type = BTRFS_QGROUP_STATUS_KEY;
804 path = btrfs_alloc_path();
808 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
816 slot = path->slots[0];
817 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
818 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
819 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
820 btrfs_set_qgroup_status_rescan(l, ptr,
821 fs_info->qgroup_rescan_progress.objectid);
823 btrfs_mark_buffer_dirty(l);
826 btrfs_free_path(path);
831 * called with qgroup_lock held
833 static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
834 struct btrfs_root *root)
836 struct btrfs_path *path;
837 struct btrfs_key key;
838 struct extent_buffer *leaf = NULL;
842 path = btrfs_alloc_path();
846 path->leave_spinning = 1;
853 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
856 leaf = path->nodes[0];
857 nr = btrfs_header_nritems(leaf);
861 * delete the leaf one by one
862 * since the whole tree is going
866 ret = btrfs_del_items(trans, root, path, 0, nr);
870 btrfs_release_path(path);
874 btrfs_free_path(path);
878 int btrfs_quota_enable(struct btrfs_trans_handle *trans,
879 struct btrfs_fs_info *fs_info)
881 struct btrfs_root *quota_root;
882 struct btrfs_root *tree_root = fs_info->tree_root;
883 struct btrfs_path *path = NULL;
884 struct btrfs_qgroup_status_item *ptr;
885 struct extent_buffer *leaf;
886 struct btrfs_key key;
887 struct btrfs_key found_key;
888 struct btrfs_qgroup *qgroup = NULL;
892 mutex_lock(&fs_info->qgroup_ioctl_lock);
893 if (fs_info->quota_root)
896 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
897 if (!fs_info->qgroup_ulist) {
903 * initially create the quota tree
905 quota_root = btrfs_create_tree(trans, fs_info,
906 BTRFS_QUOTA_TREE_OBJECTID);
907 if (IS_ERR(quota_root)) {
908 ret = PTR_ERR(quota_root);
912 path = btrfs_alloc_path();
919 key.type = BTRFS_QGROUP_STATUS_KEY;
922 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
927 leaf = path->nodes[0];
928 ptr = btrfs_item_ptr(leaf, path->slots[0],
929 struct btrfs_qgroup_status_item);
930 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
931 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
932 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
933 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
934 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
935 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
937 btrfs_mark_buffer_dirty(leaf);
940 key.type = BTRFS_ROOT_REF_KEY;
943 btrfs_release_path(path);
944 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
952 slot = path->slots[0];
953 leaf = path->nodes[0];
954 btrfs_item_key_to_cpu(leaf, &found_key, slot);
956 if (found_key.type == BTRFS_ROOT_REF_KEY) {
957 ret = add_qgroup_item(trans, quota_root,
962 qgroup = add_qgroup_rb(fs_info, found_key.offset);
963 if (IS_ERR(qgroup)) {
964 ret = PTR_ERR(qgroup);
968 ret = btrfs_next_item(tree_root, path);
976 btrfs_release_path(path);
977 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
981 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
982 if (IS_ERR(qgroup)) {
983 ret = PTR_ERR(qgroup);
986 spin_lock(&fs_info->qgroup_lock);
987 fs_info->quota_root = quota_root;
988 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
989 spin_unlock(&fs_info->qgroup_lock);
990 ret = qgroup_rescan_init(fs_info, 0, 1);
992 qgroup_rescan_zero_tracking(fs_info);
993 btrfs_queue_work(fs_info->qgroup_rescan_workers,
994 &fs_info->qgroup_rescan_work);
998 btrfs_free_path(path);
1001 free_extent_buffer(quota_root->node);
1002 free_extent_buffer(quota_root->commit_root);
1007 ulist_free(fs_info->qgroup_ulist);
1008 fs_info->qgroup_ulist = NULL;
1010 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1014 int btrfs_quota_disable(struct btrfs_trans_handle *trans,
1015 struct btrfs_fs_info *fs_info)
1017 struct btrfs_root *quota_root;
1020 mutex_lock(&fs_info->qgroup_ioctl_lock);
1021 if (!fs_info->quota_root)
1023 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1024 btrfs_qgroup_wait_for_completion(fs_info, false);
1025 spin_lock(&fs_info->qgroup_lock);
1026 quota_root = fs_info->quota_root;
1027 fs_info->quota_root = NULL;
1028 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1029 spin_unlock(&fs_info->qgroup_lock);
1031 btrfs_free_qgroup_config(fs_info);
1033 ret = btrfs_clean_quota_tree(trans, quota_root);
1037 ret = btrfs_del_root(trans, fs_info, "a_root->root_key);
1041 list_del("a_root->dirty_list);
1043 btrfs_tree_lock(quota_root->node);
1044 clean_tree_block(fs_info, quota_root->node);
1045 btrfs_tree_unlock(quota_root->node);
1046 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1048 free_extent_buffer(quota_root->node);
1049 free_extent_buffer(quota_root->commit_root);
1052 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1056 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1057 struct btrfs_qgroup *qgroup)
1059 if (list_empty(&qgroup->dirty))
1060 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1064 * The easy accounting, we're updating qgroup relationship whose child qgroup
1065 * only has exclusive extents.
1067 * In this case, all exclsuive extents will also be exlusive for parent, so
1068 * excl/rfer just get added/removed.
1070 * So is qgroup reservation space, which should also be added/removed to
1072 * Or when child tries to release reservation space, parent will underflow its
1073 * reservation (for relationship adding case).
1075 * Caller should hold fs_info->qgroup_lock.
1077 static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1078 struct ulist *tmp, u64 ref_root,
1079 struct btrfs_qgroup *src, int sign)
1081 struct btrfs_qgroup *qgroup;
1082 struct btrfs_qgroup_list *glist;
1083 struct ulist_node *unode;
1084 struct ulist_iterator uiter;
1085 u64 num_bytes = src->excl;
1088 qgroup = find_qgroup_rb(fs_info, ref_root);
1092 qgroup->rfer += sign * num_bytes;
1093 qgroup->rfer_cmpr += sign * num_bytes;
1095 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1096 qgroup->excl += sign * num_bytes;
1097 qgroup->excl_cmpr += sign * num_bytes;
1100 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1102 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1104 qgroup_dirty(fs_info, qgroup);
1106 /* Get all of the parent groups that contain this qgroup */
1107 list_for_each_entry(glist, &qgroup->groups, next_group) {
1108 ret = ulist_add(tmp, glist->group->qgroupid,
1109 qgroup_to_aux(glist->group), GFP_ATOMIC);
1114 /* Iterate all of the parents and adjust their reference counts */
1115 ULIST_ITER_INIT(&uiter);
1116 while ((unode = ulist_next(tmp, &uiter))) {
1117 qgroup = unode_aux_to_qgroup(unode);
1118 qgroup->rfer += sign * num_bytes;
1119 qgroup->rfer_cmpr += sign * num_bytes;
1120 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1121 qgroup->excl += sign * num_bytes;
1123 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1125 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1126 qgroup->excl_cmpr += sign * num_bytes;
1127 qgroup_dirty(fs_info, qgroup);
1129 /* Add any parents of the parents */
1130 list_for_each_entry(glist, &qgroup->groups, next_group) {
1131 ret = ulist_add(tmp, glist->group->qgroupid,
1132 qgroup_to_aux(glist->group), GFP_ATOMIC);
1144 * Quick path for updating qgroup with only excl refs.
1146 * In that case, just update all parent will be enough.
1147 * Or we needs to do a full rescan.
1148 * Caller should also hold fs_info->qgroup_lock.
1150 * Return 0 for quick update, return >0 for need to full rescan
1151 * and mark INCONSISTENT flag.
1152 * Return < 0 for other error.
1154 static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1155 struct ulist *tmp, u64 src, u64 dst,
1158 struct btrfs_qgroup *qgroup;
1162 qgroup = find_qgroup_rb(fs_info, src);
1165 if (qgroup->excl == qgroup->rfer) {
1167 err = __qgroup_excl_accounting(fs_info, tmp, dst,
1176 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1180 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1181 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1183 struct btrfs_root *quota_root;
1184 struct btrfs_qgroup *parent;
1185 struct btrfs_qgroup *member;
1186 struct btrfs_qgroup_list *list;
1190 /* Check the level of src and dst first */
1191 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1194 tmp = ulist_alloc(GFP_KERNEL);
1198 mutex_lock(&fs_info->qgroup_ioctl_lock);
1199 quota_root = fs_info->quota_root;
1204 member = find_qgroup_rb(fs_info, src);
1205 parent = find_qgroup_rb(fs_info, dst);
1206 if (!member || !parent) {
1211 /* check if such qgroup relation exist firstly */
1212 list_for_each_entry(list, &member->groups, next_group) {
1213 if (list->group == parent) {
1219 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1223 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1225 del_qgroup_relation_item(trans, quota_root, src, dst);
1229 spin_lock(&fs_info->qgroup_lock);
1230 ret = add_relation_rb(fs_info, src, dst);
1232 spin_unlock(&fs_info->qgroup_lock);
1235 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
1236 spin_unlock(&fs_info->qgroup_lock);
1238 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1243 static int __del_qgroup_relation(struct btrfs_trans_handle *trans,
1244 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1246 struct btrfs_root *quota_root;
1247 struct btrfs_qgroup *parent;
1248 struct btrfs_qgroup *member;
1249 struct btrfs_qgroup_list *list;
1254 tmp = ulist_alloc(GFP_KERNEL);
1258 quota_root = fs_info->quota_root;
1264 member = find_qgroup_rb(fs_info, src);
1265 parent = find_qgroup_rb(fs_info, dst);
1266 if (!member || !parent) {
1271 /* check if such qgroup relation exist firstly */
1272 list_for_each_entry(list, &member->groups, next_group) {
1273 if (list->group == parent)
1279 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1280 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1284 spin_lock(&fs_info->qgroup_lock);
1285 del_relation_rb(fs_info, src, dst);
1286 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
1287 spin_unlock(&fs_info->qgroup_lock);
1293 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1294 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1298 mutex_lock(&fs_info->qgroup_ioctl_lock);
1299 ret = __del_qgroup_relation(trans, fs_info, src, dst);
1300 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1305 int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
1306 struct btrfs_fs_info *fs_info, u64 qgroupid)
1308 struct btrfs_root *quota_root;
1309 struct btrfs_qgroup *qgroup;
1312 mutex_lock(&fs_info->qgroup_ioctl_lock);
1313 quota_root = fs_info->quota_root;
1318 qgroup = find_qgroup_rb(fs_info, qgroupid);
1324 ret = add_qgroup_item(trans, quota_root, qgroupid);
1328 spin_lock(&fs_info->qgroup_lock);
1329 qgroup = add_qgroup_rb(fs_info, qgroupid);
1330 spin_unlock(&fs_info->qgroup_lock);
1333 ret = PTR_ERR(qgroup);
1335 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1339 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1340 struct btrfs_fs_info *fs_info, u64 qgroupid)
1342 struct btrfs_root *quota_root;
1343 struct btrfs_qgroup *qgroup;
1344 struct btrfs_qgroup_list *list;
1347 mutex_lock(&fs_info->qgroup_ioctl_lock);
1348 quota_root = fs_info->quota_root;
1354 qgroup = find_qgroup_rb(fs_info, qgroupid);
1359 /* check if there are no children of this qgroup */
1360 if (!list_empty(&qgroup->members)) {
1365 ret = del_qgroup_item(trans, quota_root, qgroupid);
1366 if (ret && ret != -ENOENT)
1369 while (!list_empty(&qgroup->groups)) {
1370 list = list_first_entry(&qgroup->groups,
1371 struct btrfs_qgroup_list, next_group);
1372 ret = __del_qgroup_relation(trans, fs_info,
1374 list->group->qgroupid);
1379 spin_lock(&fs_info->qgroup_lock);
1380 del_qgroup_rb(fs_info, qgroupid);
1381 spin_unlock(&fs_info->qgroup_lock);
1383 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1387 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1388 struct btrfs_fs_info *fs_info, u64 qgroupid,
1389 struct btrfs_qgroup_limit *limit)
1391 struct btrfs_root *quota_root;
1392 struct btrfs_qgroup *qgroup;
1394 /* Sometimes we would want to clear the limit on this qgroup.
1395 * To meet this requirement, we treat the -1 as a special value
1396 * which tell kernel to clear the limit on this qgroup.
1398 const u64 CLEAR_VALUE = -1;
1400 mutex_lock(&fs_info->qgroup_ioctl_lock);
1401 quota_root = fs_info->quota_root;
1407 qgroup = find_qgroup_rb(fs_info, qgroupid);
1413 spin_lock(&fs_info->qgroup_lock);
1414 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1415 if (limit->max_rfer == CLEAR_VALUE) {
1416 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1417 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1418 qgroup->max_rfer = 0;
1420 qgroup->max_rfer = limit->max_rfer;
1423 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1424 if (limit->max_excl == CLEAR_VALUE) {
1425 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1426 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1427 qgroup->max_excl = 0;
1429 qgroup->max_excl = limit->max_excl;
1432 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1433 if (limit->rsv_rfer == CLEAR_VALUE) {
1434 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1435 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1436 qgroup->rsv_rfer = 0;
1438 qgroup->rsv_rfer = limit->rsv_rfer;
1441 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1442 if (limit->rsv_excl == CLEAR_VALUE) {
1443 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1444 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1445 qgroup->rsv_excl = 0;
1447 qgroup->rsv_excl = limit->rsv_excl;
1450 qgroup->lim_flags |= limit->flags;
1452 spin_unlock(&fs_info->qgroup_lock);
1454 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1456 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1457 btrfs_info(fs_info, "unable to update quota limit for %llu",
1462 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1466 int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
1467 struct btrfs_delayed_ref_root *delayed_refs,
1468 struct btrfs_qgroup_extent_record *record)
1470 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1471 struct rb_node *parent_node = NULL;
1472 struct btrfs_qgroup_extent_record *entry;
1473 u64 bytenr = record->bytenr;
1475 lockdep_assert_held(&delayed_refs->lock);
1476 trace_btrfs_qgroup_trace_extent(fs_info, record);
1480 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1482 if (bytenr < entry->bytenr)
1484 else if (bytenr > entry->bytenr)
1485 p = &(*p)->rb_right;
1490 rb_link_node(&record->node, parent_node, p);
1491 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
1495 int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1496 struct btrfs_qgroup_extent_record *qrecord)
1498 struct ulist *old_root;
1499 u64 bytenr = qrecord->bytenr;
1502 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
1504 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1506 "error accounting new delayed refs extent (err code: %d), quota inconsistent",
1512 * Here we don't need to get the lock of
1513 * trans->transaction->delayed_refs, since inserted qrecord won't
1514 * be deleted, only qrecord->node may be modified (new qrecord insert)
1516 * So modifying qrecord->old_roots is safe here
1518 qrecord->old_roots = old_root;
1522 int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
1523 struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
1526 struct btrfs_qgroup_extent_record *record;
1527 struct btrfs_delayed_ref_root *delayed_refs;
1530 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1531 || bytenr == 0 || num_bytes == 0)
1533 if (WARN_ON(trans == NULL))
1535 record = kmalloc(sizeof(*record), gfp_flag);
1539 delayed_refs = &trans->transaction->delayed_refs;
1540 record->bytenr = bytenr;
1541 record->num_bytes = num_bytes;
1542 record->old_roots = NULL;
1544 spin_lock(&delayed_refs->lock);
1545 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
1546 spin_unlock(&delayed_refs->lock);
1551 return btrfs_qgroup_trace_extent_post(fs_info, record);
1554 int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
1555 struct btrfs_fs_info *fs_info,
1556 struct extent_buffer *eb)
1558 int nr = btrfs_header_nritems(eb);
1559 int i, extent_type, ret;
1560 struct btrfs_key key;
1561 struct btrfs_file_extent_item *fi;
1562 u64 bytenr, num_bytes;
1564 /* We can be called directly from walk_up_proc() */
1565 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1568 for (i = 0; i < nr; i++) {
1569 btrfs_item_key_to_cpu(eb, &key, i);
1571 if (key.type != BTRFS_EXTENT_DATA_KEY)
1574 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1575 /* filter out non qgroup-accountable extents */
1576 extent_type = btrfs_file_extent_type(eb, fi);
1578 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1581 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1585 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1587 ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
1588 num_bytes, GFP_NOFS);
1597 * Walk up the tree from the bottom, freeing leaves and any interior
1598 * nodes which have had all slots visited. If a node (leaf or
1599 * interior) is freed, the node above it will have it's slot
1600 * incremented. The root node will never be freed.
1602 * At the end of this function, we should have a path which has all
1603 * slots incremented to the next position for a search. If we need to
1604 * read a new node it will be NULL and the node above it will have the
1605 * correct slot selected for a later read.
1607 * If we increment the root nodes slot counter past the number of
1608 * elements, 1 is returned to signal completion of the search.
1610 static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
1614 struct extent_buffer *eb;
1616 if (root_level == 0)
1619 while (level <= root_level) {
1620 eb = path->nodes[level];
1621 nr = btrfs_header_nritems(eb);
1622 path->slots[level]++;
1623 slot = path->slots[level];
1624 if (slot >= nr || level == 0) {
1626 * Don't free the root - we will detect this
1627 * condition after our loop and return a
1628 * positive value for caller to stop walking the tree.
1630 if (level != root_level) {
1631 btrfs_tree_unlock_rw(eb, path->locks[level]);
1632 path->locks[level] = 0;
1634 free_extent_buffer(eb);
1635 path->nodes[level] = NULL;
1636 path->slots[level] = 0;
1640 * We have a valid slot to walk back down
1641 * from. Stop here so caller can process these
1650 eb = path->nodes[root_level];
1651 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1657 int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
1658 struct btrfs_root *root,
1659 struct extent_buffer *root_eb,
1660 u64 root_gen, int root_level)
1662 struct btrfs_fs_info *fs_info = root->fs_info;
1665 struct extent_buffer *eb = root_eb;
1666 struct btrfs_path *path = NULL;
1668 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
1669 BUG_ON(root_eb == NULL);
1671 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1674 if (!extent_buffer_uptodate(root_eb)) {
1675 ret = btrfs_read_buffer(root_eb, root_gen, root_level, NULL);
1680 if (root_level == 0) {
1681 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
1685 path = btrfs_alloc_path();
1690 * Walk down the tree. Missing extent blocks are filled in as
1691 * we go. Metadata is accounted every time we read a new
1694 * When we reach a leaf, we account for file extent items in it,
1695 * walk back up the tree (adjusting slot pointers as we go)
1696 * and restart the search process.
1698 extent_buffer_get(root_eb); /* For path */
1699 path->nodes[root_level] = root_eb;
1700 path->slots[root_level] = 0;
1701 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
1704 while (level >= 0) {
1705 if (path->nodes[level] == NULL) {
1706 struct btrfs_key first_key;
1712 * We need to get child blockptr/gen from parent before
1715 eb = path->nodes[level + 1];
1716 parent_slot = path->slots[level + 1];
1717 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1718 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
1719 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
1721 eb = read_tree_block(fs_info, child_bytenr, child_gen,
1726 } else if (!extent_buffer_uptodate(eb)) {
1727 free_extent_buffer(eb);
1732 path->nodes[level] = eb;
1733 path->slots[level] = 0;
1735 btrfs_tree_read_lock(eb);
1736 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1737 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
1739 ret = btrfs_qgroup_trace_extent(trans, fs_info,
1748 ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
1749 path->nodes[level]);
1753 /* Nonzero return here means we completed our search */
1754 ret = adjust_slots_upwards(path, root_level);
1758 /* Restart search with new slots */
1767 btrfs_free_path(path);
1772 #define UPDATE_NEW 0
1773 #define UPDATE_OLD 1
1775 * Walk all of the roots that points to the bytenr and adjust their refcnts.
1777 static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
1778 struct ulist *roots, struct ulist *tmp,
1779 struct ulist *qgroups, u64 seq, int update_old)
1781 struct ulist_node *unode;
1782 struct ulist_iterator uiter;
1783 struct ulist_node *tmp_unode;
1784 struct ulist_iterator tmp_uiter;
1785 struct btrfs_qgroup *qg;
1790 ULIST_ITER_INIT(&uiter);
1791 while ((unode = ulist_next(roots, &uiter))) {
1792 qg = find_qgroup_rb(fs_info, unode->val);
1797 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
1801 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
1804 ULIST_ITER_INIT(&tmp_uiter);
1805 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1806 struct btrfs_qgroup_list *glist;
1808 qg = unode_aux_to_qgroup(tmp_unode);
1810 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1812 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1813 list_for_each_entry(glist, &qg->groups, next_group) {
1814 ret = ulist_add(qgroups, glist->group->qgroupid,
1815 qgroup_to_aux(glist->group),
1819 ret = ulist_add(tmp, glist->group->qgroupid,
1820 qgroup_to_aux(glist->group),
1831 * Update qgroup rfer/excl counters.
1832 * Rfer update is easy, codes can explain themselves.
1834 * Excl update is tricky, the update is split into 2 part.
1835 * Part 1: Possible exclusive <-> sharing detect:
1837 * -------------------------------------
1839 * -------------------------------------
1841 * -------------------------------------
1844 * A: cur_old_roots < nr_old_roots (not exclusive before)
1845 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
1846 * B: cur_new_roots < nr_new_roots (not exclusive now)
1847 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
1850 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
1851 * *: Definitely not changed. **: Possible unchanged.
1853 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
1855 * To make the logic clear, we first use condition A and B to split
1856 * combination into 4 results.
1858 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
1859 * only on variant maybe 0.
1861 * Lastly, check result **, since there are 2 variants maybe 0, split them
1863 * But this time we don't need to consider other things, the codes and logic
1864 * is easy to understand now.
1866 static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
1867 struct ulist *qgroups,
1870 u64 num_bytes, u64 seq)
1872 struct ulist_node *unode;
1873 struct ulist_iterator uiter;
1874 struct btrfs_qgroup *qg;
1875 u64 cur_new_count, cur_old_count;
1877 ULIST_ITER_INIT(&uiter);
1878 while ((unode = ulist_next(qgroups, &uiter))) {
1881 qg = unode_aux_to_qgroup(unode);
1882 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
1883 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
1885 trace_qgroup_update_counters(fs_info, qg->qgroupid,
1886 cur_old_count, cur_new_count);
1888 /* Rfer update part */
1889 if (cur_old_count == 0 && cur_new_count > 0) {
1890 qg->rfer += num_bytes;
1891 qg->rfer_cmpr += num_bytes;
1894 if (cur_old_count > 0 && cur_new_count == 0) {
1895 qg->rfer -= num_bytes;
1896 qg->rfer_cmpr -= num_bytes;
1900 /* Excl update part */
1901 /* Exclusive/none -> shared case */
1902 if (cur_old_count == nr_old_roots &&
1903 cur_new_count < nr_new_roots) {
1904 /* Exclusive -> shared */
1905 if (cur_old_count != 0) {
1906 qg->excl -= num_bytes;
1907 qg->excl_cmpr -= num_bytes;
1912 /* Shared -> exclusive/none case */
1913 if (cur_old_count < nr_old_roots &&
1914 cur_new_count == nr_new_roots) {
1915 /* Shared->exclusive */
1916 if (cur_new_count != 0) {
1917 qg->excl += num_bytes;
1918 qg->excl_cmpr += num_bytes;
1923 /* Exclusive/none -> exclusive/none case */
1924 if (cur_old_count == nr_old_roots &&
1925 cur_new_count == nr_new_roots) {
1926 if (cur_old_count == 0) {
1927 /* None -> exclusive/none */
1929 if (cur_new_count != 0) {
1930 /* None -> exclusive */
1931 qg->excl += num_bytes;
1932 qg->excl_cmpr += num_bytes;
1935 /* None -> none, nothing changed */
1937 /* Exclusive -> exclusive/none */
1939 if (cur_new_count == 0) {
1940 /* Exclusive -> none */
1941 qg->excl -= num_bytes;
1942 qg->excl_cmpr -= num_bytes;
1945 /* Exclusive -> exclusive, nothing changed */
1950 qgroup_dirty(fs_info, qg);
1956 * Check if the @roots potentially is a list of fs tree roots
1958 * Return 0 for definitely not a fs/subvol tree roots ulist
1959 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
1962 static int maybe_fs_roots(struct ulist *roots)
1964 struct ulist_node *unode;
1965 struct ulist_iterator uiter;
1967 /* Empty one, still possible for fs roots */
1968 if (!roots || roots->nnodes == 0)
1971 ULIST_ITER_INIT(&uiter);
1972 unode = ulist_next(roots, &uiter);
1977 * If it contains fs tree roots, then it must belong to fs/subvol
1979 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
1981 return is_fstree(unode->val);
1985 btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
1986 struct btrfs_fs_info *fs_info,
1987 u64 bytenr, u64 num_bytes,
1988 struct ulist *old_roots, struct ulist *new_roots)
1990 struct ulist *qgroups = NULL;
1991 struct ulist *tmp = NULL;
1993 u64 nr_new_roots = 0;
1994 u64 nr_old_roots = 0;
1997 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2001 if (!maybe_fs_roots(new_roots))
2003 nr_new_roots = new_roots->nnodes;
2006 if (!maybe_fs_roots(old_roots))
2008 nr_old_roots = old_roots->nnodes;
2011 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
2012 if (nr_old_roots == 0 && nr_new_roots == 0)
2015 BUG_ON(!fs_info->quota_root);
2017 trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
2018 nr_old_roots, nr_new_roots);
2020 qgroups = ulist_alloc(GFP_NOFS);
2025 tmp = ulist_alloc(GFP_NOFS);
2031 mutex_lock(&fs_info->qgroup_rescan_lock);
2032 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2033 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
2034 mutex_unlock(&fs_info->qgroup_rescan_lock);
2039 mutex_unlock(&fs_info->qgroup_rescan_lock);
2041 spin_lock(&fs_info->qgroup_lock);
2042 seq = fs_info->qgroup_seq;
2044 /* Update old refcnts using old_roots */
2045 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
2050 /* Update new refcnts using new_roots */
2051 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
2056 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
2060 * Bump qgroup_seq to avoid seq overlap
2062 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2064 spin_unlock(&fs_info->qgroup_lock);
2067 ulist_free(qgroups);
2068 ulist_free(old_roots);
2069 ulist_free(new_roots);
2073 int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
2075 struct btrfs_fs_info *fs_info = trans->fs_info;
2076 struct btrfs_qgroup_extent_record *record;
2077 struct btrfs_delayed_ref_root *delayed_refs;
2078 struct ulist *new_roots = NULL;
2079 struct rb_node *node;
2083 delayed_refs = &trans->transaction->delayed_refs;
2084 qgroup_to_skip = delayed_refs->qgroup_to_skip;
2085 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2086 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2089 trace_btrfs_qgroup_account_extents(fs_info, record);
2093 * Old roots should be searched when inserting qgroup
2096 if (WARN_ON(!record->old_roots)) {
2097 /* Search commit root to find old_roots */
2098 ret = btrfs_find_all_roots(NULL, fs_info,
2100 &record->old_roots, false);
2106 * Use SEQ_LAST as time_seq to do special search, which
2107 * doesn't lock tree or delayed_refs and search current
2108 * root. It's safe inside commit_transaction().
2110 ret = btrfs_find_all_roots(trans, fs_info,
2111 record->bytenr, SEQ_LAST, &new_roots, false);
2114 if (qgroup_to_skip) {
2115 ulist_del(new_roots, qgroup_to_skip, 0);
2116 ulist_del(record->old_roots, qgroup_to_skip,
2119 ret = btrfs_qgroup_account_extent(trans, fs_info,
2120 record->bytenr, record->num_bytes,
2121 record->old_roots, new_roots);
2122 record->old_roots = NULL;
2126 ulist_free(record->old_roots);
2127 ulist_free(new_roots);
2129 rb_erase(node, &delayed_refs->dirty_extent_root);
2137 * called from commit_transaction. Writes all changed qgroups to disk.
2139 int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2140 struct btrfs_fs_info *fs_info)
2142 struct btrfs_root *quota_root = fs_info->quota_root;
2148 spin_lock(&fs_info->qgroup_lock);
2149 while (!list_empty(&fs_info->dirty_qgroups)) {
2150 struct btrfs_qgroup *qgroup;
2151 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2152 struct btrfs_qgroup, dirty);
2153 list_del_init(&qgroup->dirty);
2154 spin_unlock(&fs_info->qgroup_lock);
2155 ret = update_qgroup_info_item(trans, quota_root, qgroup);
2157 fs_info->qgroup_flags |=
2158 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2159 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
2161 fs_info->qgroup_flags |=
2162 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2163 spin_lock(&fs_info->qgroup_lock);
2165 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2166 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2168 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2169 spin_unlock(&fs_info->qgroup_lock);
2171 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2173 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2179 * Copy the accounting information between qgroups. This is necessary
2180 * when a snapshot or a subvolume is created. Throwing an error will
2181 * cause a transaction abort so we take extra care here to only error
2182 * when a readonly fs is a reasonable outcome.
2184 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2185 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2186 struct btrfs_qgroup_inherit *inherit)
2191 struct btrfs_root *quota_root = fs_info->quota_root;
2192 struct btrfs_qgroup *srcgroup;
2193 struct btrfs_qgroup *dstgroup;
2197 mutex_lock(&fs_info->qgroup_ioctl_lock);
2198 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2207 i_qgroups = (u64 *)(inherit + 1);
2208 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2209 2 * inherit->num_excl_copies;
2210 for (i = 0; i < nums; ++i) {
2211 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
2214 * Zero out invalid groups so we can ignore
2218 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2226 * create a tracking group for the subvol itself
2228 ret = add_qgroup_item(trans, quota_root, objectid);
2233 struct btrfs_root *srcroot;
2234 struct btrfs_key srckey;
2236 srckey.objectid = srcid;
2237 srckey.type = BTRFS_ROOT_ITEM_KEY;
2238 srckey.offset = (u64)-1;
2239 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2240 if (IS_ERR(srcroot)) {
2241 ret = PTR_ERR(srcroot);
2245 level_size = fs_info->nodesize;
2249 * add qgroup to all inherited groups
2252 i_qgroups = (u64 *)(inherit + 1);
2253 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2254 if (*i_qgroups == 0)
2256 ret = add_qgroup_relation_item(trans, quota_root,
2257 objectid, *i_qgroups);
2258 if (ret && ret != -EEXIST)
2260 ret = add_qgroup_relation_item(trans, quota_root,
2261 *i_qgroups, objectid);
2262 if (ret && ret != -EEXIST)
2269 spin_lock(&fs_info->qgroup_lock);
2271 dstgroup = add_qgroup_rb(fs_info, objectid);
2272 if (IS_ERR(dstgroup)) {
2273 ret = PTR_ERR(dstgroup);
2277 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
2278 dstgroup->lim_flags = inherit->lim.flags;
2279 dstgroup->max_rfer = inherit->lim.max_rfer;
2280 dstgroup->max_excl = inherit->lim.max_excl;
2281 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2282 dstgroup->rsv_excl = inherit->lim.rsv_excl;
2284 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
2286 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2288 "unable to update quota limit for %llu",
2289 dstgroup->qgroupid);
2295 srcgroup = find_qgroup_rb(fs_info, srcid);
2300 * We call inherit after we clone the root in order to make sure
2301 * our counts don't go crazy, so at this point the only
2302 * difference between the two roots should be the root node.
2304 dstgroup->rfer = srcgroup->rfer;
2305 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2306 dstgroup->excl = level_size;
2307 dstgroup->excl_cmpr = level_size;
2308 srcgroup->excl = level_size;
2309 srcgroup->excl_cmpr = level_size;
2311 /* inherit the limit info */
2312 dstgroup->lim_flags = srcgroup->lim_flags;
2313 dstgroup->max_rfer = srcgroup->max_rfer;
2314 dstgroup->max_excl = srcgroup->max_excl;
2315 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2316 dstgroup->rsv_excl = srcgroup->rsv_excl;
2318 qgroup_dirty(fs_info, dstgroup);
2319 qgroup_dirty(fs_info, srcgroup);
2325 i_qgroups = (u64 *)(inherit + 1);
2326 for (i = 0; i < inherit->num_qgroups; ++i) {
2328 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
2335 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
2336 struct btrfs_qgroup *src;
2337 struct btrfs_qgroup *dst;
2339 if (!i_qgroups[0] || !i_qgroups[1])
2342 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2343 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2350 dst->rfer = src->rfer - level_size;
2351 dst->rfer_cmpr = src->rfer_cmpr - level_size;
2353 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
2354 struct btrfs_qgroup *src;
2355 struct btrfs_qgroup *dst;
2357 if (!i_qgroups[0] || !i_qgroups[1])
2360 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2361 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2368 dst->excl = src->excl + level_size;
2369 dst->excl_cmpr = src->excl_cmpr + level_size;
2373 spin_unlock(&fs_info->qgroup_lock);
2375 mutex_unlock(&fs_info->qgroup_ioctl_lock);
2380 * Two limits to commit transaction in advance.
2382 * For RATIO, it will be 1/RATIO of the remaining limit
2383 * (excluding data and prealloc meta) as threshold.
2384 * For SIZE, it will be in byte unit as threshold.
2386 #define QGROUP_PERTRANS_RATIO 32
2387 #define QGROUP_PERTRANS_SIZE SZ_32M
2388 static bool qgroup_check_limits(struct btrfs_fs_info *fs_info,
2389 const struct btrfs_qgroup *qg, u64 num_bytes)
2394 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
2395 qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
2398 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
2399 qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
2403 * Even if we passed the check, it's better to check if reservation
2404 * for meta_pertrans is pushing us near limit.
2405 * If there is too much pertrans reservation or it's near the limit,
2406 * let's try commit transaction to free some, using transaction_kthread
2408 if ((qg->lim_flags & (BTRFS_QGROUP_LIMIT_MAX_RFER |
2409 BTRFS_QGROUP_LIMIT_MAX_EXCL))) {
2410 if (qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL)
2411 limit = qg->max_excl;
2413 limit = qg->max_rfer;
2414 threshold = (limit - qg->rsv.values[BTRFS_QGROUP_RSV_DATA] -
2415 qg->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC]) /
2416 QGROUP_PERTRANS_RATIO;
2417 threshold = min_t(u64, threshold, QGROUP_PERTRANS_SIZE);
2420 * Use transaction_kthread to commit transaction, so we no
2421 * longer need to bother nested transaction nor lock context.
2423 if (qg->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS] > threshold)
2424 btrfs_commit_transaction_locksafe(fs_info);
2430 static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
2431 enum btrfs_qgroup_rsv_type type)
2433 struct btrfs_root *quota_root;
2434 struct btrfs_qgroup *qgroup;
2435 struct btrfs_fs_info *fs_info = root->fs_info;
2436 u64 ref_root = root->root_key.objectid;
2438 struct ulist_node *unode;
2439 struct ulist_iterator uiter;
2441 if (!is_fstree(ref_root))
2447 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
2448 capable(CAP_SYS_RESOURCE))
2451 spin_lock(&fs_info->qgroup_lock);
2452 quota_root = fs_info->quota_root;
2456 qgroup = find_qgroup_rb(fs_info, ref_root);
2461 * in a first step, we check all affected qgroups if any limits would
2464 ulist_reinit(fs_info->qgroup_ulist);
2465 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
2466 qgroup_to_aux(qgroup), GFP_ATOMIC);
2469 ULIST_ITER_INIT(&uiter);
2470 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2471 struct btrfs_qgroup *qg;
2472 struct btrfs_qgroup_list *glist;
2474 qg = unode_aux_to_qgroup(unode);
2476 if (enforce && !qgroup_check_limits(fs_info, qg, num_bytes)) {
2481 list_for_each_entry(glist, &qg->groups, next_group) {
2482 ret = ulist_add(fs_info->qgroup_ulist,
2483 glist->group->qgroupid,
2484 qgroup_to_aux(glist->group), GFP_ATOMIC);
2491 * no limits exceeded, now record the reservation into all qgroups
2493 ULIST_ITER_INIT(&uiter);
2494 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2495 struct btrfs_qgroup *qg;
2497 qg = unode_aux_to_qgroup(unode);
2499 trace_qgroup_update_reserve(fs_info, qg, num_bytes, type);
2500 qgroup_rsv_add(fs_info, qg, num_bytes, type);
2504 spin_unlock(&fs_info->qgroup_lock);
2509 * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0
2512 * Will handle all higher level qgroup too.
2514 * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
2515 * This special case is only used for META_PERTRANS type.
2517 void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
2518 u64 ref_root, u64 num_bytes,
2519 enum btrfs_qgroup_rsv_type type)
2521 struct btrfs_root *quota_root;
2522 struct btrfs_qgroup *qgroup;
2523 struct ulist_node *unode;
2524 struct ulist_iterator uiter;
2527 if (!is_fstree(ref_root))
2533 if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
2534 WARN(1, "%s: Invalid type to free", __func__);
2537 spin_lock(&fs_info->qgroup_lock);
2539 quota_root = fs_info->quota_root;
2543 qgroup = find_qgroup_rb(fs_info, ref_root);
2547 if (num_bytes == (u64)-1)
2549 * We're freeing all pertrans rsv, get reserved value from
2550 * level 0 qgroup as real num_bytes to free.
2552 num_bytes = qgroup->rsv.values[type];
2554 ulist_reinit(fs_info->qgroup_ulist);
2555 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
2556 qgroup_to_aux(qgroup), GFP_ATOMIC);
2559 ULIST_ITER_INIT(&uiter);
2560 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2561 struct btrfs_qgroup *qg;
2562 struct btrfs_qgroup_list *glist;
2564 qg = unode_aux_to_qgroup(unode);
2566 trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes, type);
2567 qgroup_rsv_release(fs_info, qg, num_bytes, type);
2569 list_for_each_entry(glist, &qg->groups, next_group) {
2570 ret = ulist_add(fs_info->qgroup_ulist,
2571 glist->group->qgroupid,
2572 qgroup_to_aux(glist->group), GFP_ATOMIC);
2579 spin_unlock(&fs_info->qgroup_lock);
2583 * returns < 0 on error, 0 when more leafs are to be scanned.
2584 * returns 1 when done.
2587 qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
2588 struct btrfs_trans_handle *trans)
2590 struct btrfs_key found;
2591 struct extent_buffer *scratch_leaf = NULL;
2592 struct ulist *roots = NULL;
2593 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
2598 mutex_lock(&fs_info->qgroup_rescan_lock);
2599 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2600 &fs_info->qgroup_rescan_progress,
2603 btrfs_debug(fs_info,
2604 "current progress key (%llu %u %llu), search_slot ret %d",
2605 fs_info->qgroup_rescan_progress.objectid,
2606 fs_info->qgroup_rescan_progress.type,
2607 fs_info->qgroup_rescan_progress.offset, ret);
2611 * The rescan is about to end, we will not be scanning any
2612 * further blocks. We cannot unset the RESCAN flag here, because
2613 * we want to commit the transaction if everything went well.
2614 * To make the live accounting work in this phase, we set our
2615 * scan progress pointer such that every real extent objectid
2618 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2619 btrfs_release_path(path);
2620 mutex_unlock(&fs_info->qgroup_rescan_lock);
2624 btrfs_item_key_to_cpu(path->nodes[0], &found,
2625 btrfs_header_nritems(path->nodes[0]) - 1);
2626 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2628 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2629 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
2630 if (!scratch_leaf) {
2632 mutex_unlock(&fs_info->qgroup_rescan_lock);
2635 extent_buffer_get(scratch_leaf);
2636 btrfs_tree_read_lock(scratch_leaf);
2637 btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
2638 slot = path->slots[0];
2639 btrfs_release_path(path);
2640 mutex_unlock(&fs_info->qgroup_rescan_lock);
2642 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2643 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
2644 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2645 found.type != BTRFS_METADATA_ITEM_KEY)
2647 if (found.type == BTRFS_METADATA_ITEM_KEY)
2648 num_bytes = fs_info->nodesize;
2650 num_bytes = found.offset;
2652 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
2656 /* For rescan, just pass old_roots as NULL */
2657 ret = btrfs_qgroup_account_extent(trans, fs_info,
2658 found.objectid, num_bytes, NULL, roots);
2664 btrfs_tree_read_unlock_blocking(scratch_leaf);
2665 free_extent_buffer(scratch_leaf);
2667 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2672 static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
2674 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2675 qgroup_rescan_work);
2676 struct btrfs_path *path;
2677 struct btrfs_trans_handle *trans = NULL;
2681 path = btrfs_alloc_path();
2686 while (!err && !btrfs_fs_closing(fs_info)) {
2687 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2688 if (IS_ERR(trans)) {
2689 err = PTR_ERR(trans);
2692 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
2695 err = qgroup_rescan_leaf(fs_info, path, trans);
2698 btrfs_commit_transaction(trans);
2700 btrfs_end_transaction(trans);
2704 btrfs_free_path(path);
2706 mutex_lock(&fs_info->qgroup_rescan_lock);
2707 if (!btrfs_fs_closing(fs_info))
2708 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2711 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2712 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2713 } else if (err < 0) {
2714 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2716 mutex_unlock(&fs_info->qgroup_rescan_lock);
2719 * only update status, since the previous part has already updated the
2722 trans = btrfs_start_transaction(fs_info->quota_root, 1);
2723 if (IS_ERR(trans)) {
2724 err = PTR_ERR(trans);
2726 "fail to start transaction for status update: %d",
2730 ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
2733 btrfs_err(fs_info, "fail to update qgroup status: %d", err);
2735 btrfs_end_transaction(trans);
2737 if (btrfs_fs_closing(fs_info)) {
2738 btrfs_info(fs_info, "qgroup scan paused");
2739 } else if (err >= 0) {
2740 btrfs_info(fs_info, "qgroup scan completed%s",
2741 err > 0 ? " (inconsistency flag cleared)" : "");
2743 btrfs_err(fs_info, "qgroup scan failed with %d", err);
2747 mutex_lock(&fs_info->qgroup_rescan_lock);
2748 fs_info->qgroup_rescan_running = false;
2749 mutex_unlock(&fs_info->qgroup_rescan_lock);
2750 complete_all(&fs_info->qgroup_rescan_completion);
2754 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2755 * memory required for the rescan context.
2758 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2764 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2765 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2770 mutex_lock(&fs_info->qgroup_rescan_lock);
2771 spin_lock(&fs_info->qgroup_lock);
2774 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2776 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2780 spin_unlock(&fs_info->qgroup_lock);
2781 mutex_unlock(&fs_info->qgroup_rescan_lock);
2784 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2787 memset(&fs_info->qgroup_rescan_progress, 0,
2788 sizeof(fs_info->qgroup_rescan_progress));
2789 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
2790 init_completion(&fs_info->qgroup_rescan_completion);
2791 fs_info->qgroup_rescan_running = true;
2793 spin_unlock(&fs_info->qgroup_lock);
2794 mutex_unlock(&fs_info->qgroup_rescan_lock);
2796 memset(&fs_info->qgroup_rescan_work, 0,
2797 sizeof(fs_info->qgroup_rescan_work));
2798 btrfs_init_work(&fs_info->qgroup_rescan_work,
2799 btrfs_qgroup_rescan_helper,
2800 btrfs_qgroup_rescan_worker, NULL, NULL);
2804 btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
2812 qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2815 struct btrfs_qgroup *qgroup;
2817 spin_lock(&fs_info->qgroup_lock);
2818 /* clear all current qgroup tracking information */
2819 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2820 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2822 qgroup->rfer_cmpr = 0;
2824 qgroup->excl_cmpr = 0;
2826 spin_unlock(&fs_info->qgroup_lock);
2830 btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2833 struct btrfs_trans_handle *trans;
2835 ret = qgroup_rescan_init(fs_info, 0, 1);
2840 * We have set the rescan_progress to 0, which means no more
2841 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2842 * However, btrfs_qgroup_account_ref may be right after its call
2843 * to btrfs_find_all_roots, in which case it would still do the
2845 * To solve this, we're committing the transaction, which will
2846 * ensure we run all delayed refs and only after that, we are
2847 * going to clear all tracking information for a clean start.
2850 trans = btrfs_join_transaction(fs_info->fs_root);
2851 if (IS_ERR(trans)) {
2852 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2853 return PTR_ERR(trans);
2855 ret = btrfs_commit_transaction(trans);
2857 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2861 qgroup_rescan_zero_tracking(fs_info);
2863 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2864 &fs_info->qgroup_rescan_work);
2869 int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
2875 mutex_lock(&fs_info->qgroup_rescan_lock);
2876 spin_lock(&fs_info->qgroup_lock);
2877 running = fs_info->qgroup_rescan_running;
2878 spin_unlock(&fs_info->qgroup_lock);
2879 mutex_unlock(&fs_info->qgroup_rescan_lock);
2885 ret = wait_for_completion_interruptible(
2886 &fs_info->qgroup_rescan_completion);
2888 wait_for_completion(&fs_info->qgroup_rescan_completion);
2894 * this is only called from open_ctree where we're still single threaded, thus
2895 * locking is omitted here.
2898 btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2900 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2901 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2902 &fs_info->qgroup_rescan_work);
2906 * Reserve qgroup space for range [start, start + len).
2908 * This function will either reserve space from related qgroups or doing
2909 * nothing if the range is already reserved.
2911 * Return 0 for successful reserve
2912 * Return <0 for error (including -EQUOT)
2914 * NOTE: this function may sleep for memory allocation.
2915 * if btrfs_qgroup_reserve_data() is called multiple times with
2916 * same @reserved, caller must ensure when error happens it's OK
2917 * to free *ALL* reserved space.
2919 int btrfs_qgroup_reserve_data(struct inode *inode,
2920 struct extent_changeset **reserved_ret, u64 start,
2923 struct btrfs_root *root = BTRFS_I(inode)->root;
2924 struct ulist_node *unode;
2925 struct ulist_iterator uiter;
2926 struct extent_changeset *reserved;
2931 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2932 !is_fstree(root->objectid) || len == 0)
2935 /* @reserved parameter is mandatory for qgroup */
2936 if (WARN_ON(!reserved_ret))
2938 if (!*reserved_ret) {
2939 *reserved_ret = extent_changeset_alloc();
2943 reserved = *reserved_ret;
2944 /* Record already reserved space */
2945 orig_reserved = reserved->bytes_changed;
2946 ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
2947 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
2949 /* Newly reserved space */
2950 to_reserve = reserved->bytes_changed - orig_reserved;
2951 trace_btrfs_qgroup_reserve_data(inode, start, len,
2952 to_reserve, QGROUP_RESERVE);
2955 ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
2962 /* cleanup *ALL* already reserved ranges */
2963 ULIST_ITER_INIT(&uiter);
2964 while ((unode = ulist_next(&reserved->range_changed, &uiter)))
2965 clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
2966 unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL);
2967 extent_changeset_release(reserved);
2971 /* Free ranges specified by @reserved, normally in error path */
2972 static int qgroup_free_reserved_data(struct inode *inode,
2973 struct extent_changeset *reserved, u64 start, u64 len)
2975 struct btrfs_root *root = BTRFS_I(inode)->root;
2976 struct ulist_node *unode;
2977 struct ulist_iterator uiter;
2978 struct extent_changeset changeset;
2982 extent_changeset_init(&changeset);
2983 len = round_up(start + len, root->fs_info->sectorsize);
2984 start = round_down(start, root->fs_info->sectorsize);
2986 ULIST_ITER_INIT(&uiter);
2987 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
2988 u64 range_start = unode->val;
2989 /* unode->aux is the inclusive end */
2990 u64 range_len = unode->aux - range_start + 1;
2994 extent_changeset_release(&changeset);
2996 /* Only free range in range [start, start + len) */
2997 if (range_start >= start + len ||
2998 range_start + range_len <= start)
3000 free_start = max(range_start, start);
3001 free_len = min(start + len, range_start + range_len) -
3004 * TODO: To also modify reserved->ranges_reserved to reflect
3007 * However as long as we free qgroup reserved according to
3008 * EXTENT_QGROUP_RESERVED, we won't double free.
3009 * So not need to rush.
3011 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_failure_tree,
3012 free_start, free_start + free_len - 1,
3013 EXTENT_QGROUP_RESERVED, &changeset);
3016 freed += changeset.bytes_changed;
3018 btrfs_qgroup_free_refroot(root->fs_info, root->objectid, freed,
3019 BTRFS_QGROUP_RSV_DATA);
3022 extent_changeset_release(&changeset);
3026 static int __btrfs_qgroup_release_data(struct inode *inode,
3027 struct extent_changeset *reserved, u64 start, u64 len,
3030 struct extent_changeset changeset;
3031 int trace_op = QGROUP_RELEASE;
3034 /* In release case, we shouldn't have @reserved */
3035 WARN_ON(!free && reserved);
3036 if (free && reserved)
3037 return qgroup_free_reserved_data(inode, reserved, start, len);
3038 extent_changeset_init(&changeset);
3039 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
3040 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
3045 trace_op = QGROUP_FREE;
3046 trace_btrfs_qgroup_release_data(inode, start, len,
3047 changeset.bytes_changed, trace_op);
3049 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3050 BTRFS_I(inode)->root->objectid,
3051 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
3052 ret = changeset.bytes_changed;
3054 extent_changeset_release(&changeset);
3059 * Free a reserved space range from io_tree and related qgroups
3061 * Should be called when a range of pages get invalidated before reaching disk.
3062 * Or for error cleanup case.
3063 * if @reserved is given, only reserved range in [@start, @start + @len) will
3066 * For data written to disk, use btrfs_qgroup_release_data().
3068 * NOTE: This function may sleep for memory allocation.
3070 int btrfs_qgroup_free_data(struct inode *inode,
3071 struct extent_changeset *reserved, u64 start, u64 len)
3073 return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
3077 * Release a reserved space range from io_tree only.
3079 * Should be called when a range of pages get written to disk and corresponding
3080 * FILE_EXTENT is inserted into corresponding root.
3082 * Since new qgroup accounting framework will only update qgroup numbers at
3083 * commit_transaction() time, its reserved space shouldn't be freed from
3086 * But we should release the range from io_tree, to allow further write to be
3089 * NOTE: This function may sleep for memory allocation.
3091 int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
3093 return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
3096 static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3097 enum btrfs_qgroup_rsv_type type)
3099 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3100 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3105 spin_lock(&root->qgroup_meta_rsv_lock);
3106 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
3107 root->qgroup_meta_rsv_prealloc += num_bytes;
3109 root->qgroup_meta_rsv_pertrans += num_bytes;
3110 spin_unlock(&root->qgroup_meta_rsv_lock);
3113 static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3114 enum btrfs_qgroup_rsv_type type)
3116 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3117 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3122 spin_lock(&root->qgroup_meta_rsv_lock);
3123 if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
3124 num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
3126 root->qgroup_meta_rsv_prealloc -= num_bytes;
3128 num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
3130 root->qgroup_meta_rsv_pertrans -= num_bytes;
3132 spin_unlock(&root->qgroup_meta_rsv_lock);
3136 int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3137 enum btrfs_qgroup_rsv_type type, bool enforce)
3139 struct btrfs_fs_info *fs_info = root->fs_info;
3142 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3143 !is_fstree(root->objectid) || num_bytes == 0)
3146 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
3147 trace_qgroup_meta_reserve(root, type, (s64)num_bytes);
3148 ret = qgroup_reserve(root, num_bytes, enforce, type);
3152 * Record what we have reserved into root.
3154 * To avoid quota disabled->enabled underflow.
3155 * In that case, we may try to free space we haven't reserved
3156 * (since quota was disabled), so record what we reserved into root.
3157 * And ensure later release won't underflow this number.
3159 add_root_meta_rsv(root, num_bytes, type);
3163 void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
3165 struct btrfs_fs_info *fs_info = root->fs_info;
3167 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3168 !is_fstree(root->objectid))
3171 /* TODO: Update trace point to handle such free */
3172 trace_qgroup_meta_free_all_pertrans(root);
3173 /* Special value -1 means to free all reserved space */
3174 btrfs_qgroup_free_refroot(fs_info, root->objectid, (u64)-1,
3175 BTRFS_QGROUP_RSV_META_PERTRANS);
3178 void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
3179 enum btrfs_qgroup_rsv_type type)
3181 struct btrfs_fs_info *fs_info = root->fs_info;
3183 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3184 !is_fstree(root->objectid))
3188 * reservation for META_PREALLOC can happen before quota is enabled,
3189 * which can lead to underflow.
3190 * Here ensure we will only free what we really have reserved.
3192 num_bytes = sub_root_meta_rsv(root, num_bytes, type);
3193 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
3194 trace_qgroup_meta_reserve(root, type, -(s64)num_bytes);
3195 btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes, type);
3198 static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
3201 struct btrfs_root *quota_root = fs_info->quota_root;
3202 struct btrfs_qgroup *qgroup;
3203 struct ulist_node *unode;
3204 struct ulist_iterator uiter;
3212 spin_lock(&fs_info->qgroup_lock);
3213 qgroup = find_qgroup_rb(fs_info, ref_root);
3216 ulist_reinit(fs_info->qgroup_ulist);
3217 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3218 qgroup_to_aux(qgroup), GFP_ATOMIC);
3221 ULIST_ITER_INIT(&uiter);
3222 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3223 struct btrfs_qgroup *qg;
3224 struct btrfs_qgroup_list *glist;
3226 qg = unode_aux_to_qgroup(unode);
3228 qgroup_rsv_release(fs_info, qg, num_bytes,
3229 BTRFS_QGROUP_RSV_META_PREALLOC);
3230 qgroup_rsv_add(fs_info, qg, num_bytes,
3231 BTRFS_QGROUP_RSV_META_PERTRANS);
3232 list_for_each_entry(glist, &qg->groups, next_group) {
3233 ret = ulist_add(fs_info->qgroup_ulist,
3234 glist->group->qgroupid,
3235 qgroup_to_aux(glist->group), GFP_ATOMIC);
3241 spin_unlock(&fs_info->qgroup_lock);
3244 void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
3246 struct btrfs_fs_info *fs_info = root->fs_info;
3248 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3249 !is_fstree(root->objectid))
3251 /* Same as btrfs_qgroup_free_meta_prealloc() */
3252 num_bytes = sub_root_meta_rsv(root, num_bytes,
3253 BTRFS_QGROUP_RSV_META_PREALLOC);
3254 trace_qgroup_meta_convert(root, num_bytes);
3255 qgroup_convert_meta(fs_info, root->objectid, num_bytes);
3259 * Check qgroup reserved space leaking, normally at destroy inode
3262 void btrfs_qgroup_check_reserved_leak(struct inode *inode)
3264 struct extent_changeset changeset;
3265 struct ulist_node *unode;
3266 struct ulist_iterator iter;
3269 extent_changeset_init(&changeset);
3270 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
3271 EXTENT_QGROUP_RESERVED, &changeset);
3274 if (WARN_ON(changeset.bytes_changed)) {
3275 ULIST_ITER_INIT(&iter);
3276 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
3277 btrfs_warn(BTRFS_I(inode)->root->fs_info,
3278 "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
3279 inode->i_ino, unode->val, unode->aux);
3281 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3282 BTRFS_I(inode)->root->objectid,
3283 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
3286 extent_changeset_release(&changeset);