2 * Copyright (C) 2011 STRATO. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/pagemap.h>
21 #include <linux/writeback.h>
22 #include <linux/blkdev.h>
23 #include <linux/rbtree.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/btrfs.h>
29 #include "transaction.h"
34 #include "extent_io.h"
38 * - subvol delete -> delete when ref goes to 0? delete limits also?
42 * - copy also limits on subvol creation
44 * - caches fuer ulists
45 * - performance benchmarks
46 * - check all ioctl parameters
50 * one struct for each qgroup, organized in fs_info->qgroup_tree.
58 u64 rfer; /* referenced */
59 u64 rfer_cmpr; /* referenced compressed */
60 u64 excl; /* exclusive */
61 u64 excl_cmpr; /* exclusive compressed */
66 u64 lim_flags; /* which limits are set */
73 * reservation tracking
80 struct list_head groups; /* groups this group is member of */
81 struct list_head members; /* groups that are members of this group */
82 struct list_head dirty; /* dirty groups */
83 struct rb_node node; /* tree of qgroups */
86 * temp variables for accounting operations
93 * glue structure to represent the relations between qgroups.
95 struct btrfs_qgroup_list {
96 struct list_head next_group;
97 struct list_head next_member;
98 struct btrfs_qgroup *group;
99 struct btrfs_qgroup *member;
102 #define ptr_to_u64(x) ((u64)(uintptr_t)x)
103 #define u64_to_ptr(x) ((struct btrfs_qgroup *)(uintptr_t)x)
106 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
108 static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
110 /* must be called with qgroup_ioctl_lock held */
111 static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
114 struct rb_node *n = fs_info->qgroup_tree.rb_node;
115 struct btrfs_qgroup *qgroup;
118 qgroup = rb_entry(n, struct btrfs_qgroup, node);
119 if (qgroup->qgroupid < qgroupid)
121 else if (qgroup->qgroupid > qgroupid)
129 /* must be called with qgroup_lock held */
130 static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
133 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
134 struct rb_node *parent = NULL;
135 struct btrfs_qgroup *qgroup;
139 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
141 if (qgroup->qgroupid < qgroupid)
143 else if (qgroup->qgroupid > qgroupid)
149 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
151 return ERR_PTR(-ENOMEM);
153 qgroup->qgroupid = qgroupid;
154 INIT_LIST_HEAD(&qgroup->groups);
155 INIT_LIST_HEAD(&qgroup->members);
156 INIT_LIST_HEAD(&qgroup->dirty);
158 rb_link_node(&qgroup->node, parent, p);
159 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
164 static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
166 struct btrfs_qgroup_list *list;
168 list_del(&qgroup->dirty);
169 while (!list_empty(&qgroup->groups)) {
170 list = list_first_entry(&qgroup->groups,
171 struct btrfs_qgroup_list, next_group);
172 list_del(&list->next_group);
173 list_del(&list->next_member);
177 while (!list_empty(&qgroup->members)) {
178 list = list_first_entry(&qgroup->members,
179 struct btrfs_qgroup_list, next_member);
180 list_del(&list->next_group);
181 list_del(&list->next_member);
187 /* must be called with qgroup_lock held */
188 static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
190 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
195 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
196 __del_qgroup_rb(qgroup);
200 /* must be called with qgroup_lock held */
201 static int add_relation_rb(struct btrfs_fs_info *fs_info,
202 u64 memberid, u64 parentid)
204 struct btrfs_qgroup *member;
205 struct btrfs_qgroup *parent;
206 struct btrfs_qgroup_list *list;
208 member = find_qgroup_rb(fs_info, memberid);
209 parent = find_qgroup_rb(fs_info, parentid);
210 if (!member || !parent)
213 list = kzalloc(sizeof(*list), GFP_ATOMIC);
217 list->group = parent;
218 list->member = member;
219 list_add_tail(&list->next_group, &member->groups);
220 list_add_tail(&list->next_member, &parent->members);
225 /* must be called with qgroup_lock held */
226 static int del_relation_rb(struct btrfs_fs_info *fs_info,
227 u64 memberid, u64 parentid)
229 struct btrfs_qgroup *member;
230 struct btrfs_qgroup *parent;
231 struct btrfs_qgroup_list *list;
233 member = find_qgroup_rb(fs_info, memberid);
234 parent = find_qgroup_rb(fs_info, parentid);
235 if (!member || !parent)
238 list_for_each_entry(list, &member->groups, next_group) {
239 if (list->group == parent) {
240 list_del(&list->next_group);
241 list_del(&list->next_member);
249 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
250 int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
253 struct btrfs_qgroup *qgroup;
255 qgroup = find_qgroup_rb(fs_info, qgroupid);
258 if (qgroup->rfer != rfer || qgroup->excl != excl)
265 * The full config is read in one go, only called from open_ctree()
266 * It doesn't use any locking, as at this point we're still single-threaded
268 int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
270 struct btrfs_key key;
271 struct btrfs_key found_key;
272 struct btrfs_root *quota_root = fs_info->quota_root;
273 struct btrfs_path *path = NULL;
274 struct extent_buffer *l;
278 u64 rescan_progress = 0;
280 if (!fs_info->quota_enabled)
283 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
284 if (!fs_info->qgroup_ulist) {
289 path = btrfs_alloc_path();
295 /* default this to quota off, in case no status key is found */
296 fs_info->qgroup_flags = 0;
299 * pass 1: read status, all qgroup infos and limits
304 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
309 struct btrfs_qgroup *qgroup;
311 slot = path->slots[0];
313 btrfs_item_key_to_cpu(l, &found_key, slot);
315 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
316 struct btrfs_qgroup_status_item *ptr;
318 ptr = btrfs_item_ptr(l, slot,
319 struct btrfs_qgroup_status_item);
321 if (btrfs_qgroup_status_version(l, ptr) !=
322 BTRFS_QGROUP_STATUS_VERSION) {
324 "old qgroup version, quota disabled");
327 if (btrfs_qgroup_status_generation(l, ptr) !=
328 fs_info->generation) {
329 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
331 "qgroup generation mismatch, "
332 "marked as inconsistent");
334 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
336 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
340 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
341 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
344 qgroup = find_qgroup_rb(fs_info, found_key.offset);
345 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
346 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
347 btrfs_err(fs_info, "inconsitent qgroup config");
348 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
351 qgroup = add_qgroup_rb(fs_info, found_key.offset);
352 if (IS_ERR(qgroup)) {
353 ret = PTR_ERR(qgroup);
357 switch (found_key.type) {
358 case BTRFS_QGROUP_INFO_KEY: {
359 struct btrfs_qgroup_info_item *ptr;
361 ptr = btrfs_item_ptr(l, slot,
362 struct btrfs_qgroup_info_item);
363 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
364 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
365 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
366 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
367 /* generation currently unused */
370 case BTRFS_QGROUP_LIMIT_KEY: {
371 struct btrfs_qgroup_limit_item *ptr;
373 ptr = btrfs_item_ptr(l, slot,
374 struct btrfs_qgroup_limit_item);
375 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
376 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
377 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
378 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
379 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
384 ret = btrfs_next_item(quota_root, path);
390 btrfs_release_path(path);
393 * pass 2: read all qgroup relations
396 key.type = BTRFS_QGROUP_RELATION_KEY;
398 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
402 slot = path->slots[0];
404 btrfs_item_key_to_cpu(l, &found_key, slot);
406 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
409 if (found_key.objectid > found_key.offset) {
410 /* parent <- member, not needed to build config */
411 /* FIXME should we omit the key completely? */
415 ret = add_relation_rb(fs_info, found_key.objectid,
417 if (ret == -ENOENT) {
419 "orphan qgroup relation 0x%llx->0x%llx",
420 found_key.objectid, found_key.offset);
421 ret = 0; /* ignore the error */
426 ret = btrfs_next_item(quota_root, path);
433 fs_info->qgroup_flags |= flags;
434 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
435 fs_info->quota_enabled = 0;
436 fs_info->pending_quota_state = 0;
437 } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
439 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
441 btrfs_free_path(path);
444 ulist_free(fs_info->qgroup_ulist);
445 fs_info->qgroup_ulist = NULL;
446 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
449 return ret < 0 ? ret : 0;
453 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
454 * first two are in single-threaded paths.And for the third one, we have set
455 * quota_root to be null with qgroup_lock held before, so it is safe to clean
456 * up the in-memory structures without qgroup_lock held.
458 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
461 struct btrfs_qgroup *qgroup;
463 while ((n = rb_first(&fs_info->qgroup_tree))) {
464 qgroup = rb_entry(n, struct btrfs_qgroup, node);
465 rb_erase(n, &fs_info->qgroup_tree);
466 __del_qgroup_rb(qgroup);
469 * we call btrfs_free_qgroup_config() when umounting
470 * filesystem and disabling quota, so we set qgroup_ulit
471 * to be null here to avoid double free.
473 ulist_free(fs_info->qgroup_ulist);
474 fs_info->qgroup_ulist = NULL;
477 static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
478 struct btrfs_root *quota_root,
482 struct btrfs_path *path;
483 struct btrfs_key key;
485 path = btrfs_alloc_path();
490 key.type = BTRFS_QGROUP_RELATION_KEY;
493 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
495 btrfs_mark_buffer_dirty(path->nodes[0]);
497 btrfs_free_path(path);
501 static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
502 struct btrfs_root *quota_root,
506 struct btrfs_path *path;
507 struct btrfs_key key;
509 path = btrfs_alloc_path();
514 key.type = BTRFS_QGROUP_RELATION_KEY;
517 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
526 ret = btrfs_del_item(trans, quota_root, path);
528 btrfs_free_path(path);
532 static int add_qgroup_item(struct btrfs_trans_handle *trans,
533 struct btrfs_root *quota_root, u64 qgroupid)
536 struct btrfs_path *path;
537 struct btrfs_qgroup_info_item *qgroup_info;
538 struct btrfs_qgroup_limit_item *qgroup_limit;
539 struct extent_buffer *leaf;
540 struct btrfs_key key;
542 if (btrfs_test_is_dummy_root(quota_root))
545 path = btrfs_alloc_path();
550 key.type = BTRFS_QGROUP_INFO_KEY;
551 key.offset = qgroupid;
554 * Avoid a transaction abort by catching -EEXIST here. In that
555 * case, we proceed by re-initializing the existing structure
559 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
560 sizeof(*qgroup_info));
561 if (ret && ret != -EEXIST)
564 leaf = path->nodes[0];
565 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
566 struct btrfs_qgroup_info_item);
567 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
568 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
569 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
570 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
571 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
573 btrfs_mark_buffer_dirty(leaf);
575 btrfs_release_path(path);
577 key.type = BTRFS_QGROUP_LIMIT_KEY;
578 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
579 sizeof(*qgroup_limit));
580 if (ret && ret != -EEXIST)
583 leaf = path->nodes[0];
584 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
585 struct btrfs_qgroup_limit_item);
586 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
587 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
588 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
589 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
590 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
592 btrfs_mark_buffer_dirty(leaf);
596 btrfs_free_path(path);
600 static int del_qgroup_item(struct btrfs_trans_handle *trans,
601 struct btrfs_root *quota_root, u64 qgroupid)
604 struct btrfs_path *path;
605 struct btrfs_key key;
607 path = btrfs_alloc_path();
612 key.type = BTRFS_QGROUP_INFO_KEY;
613 key.offset = qgroupid;
614 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
623 ret = btrfs_del_item(trans, quota_root, path);
627 btrfs_release_path(path);
629 key.type = BTRFS_QGROUP_LIMIT_KEY;
630 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
639 ret = btrfs_del_item(trans, quota_root, path);
642 btrfs_free_path(path);
646 static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
647 struct btrfs_root *root,
648 struct btrfs_qgroup *qgroup)
650 struct btrfs_path *path;
651 struct btrfs_key key;
652 struct extent_buffer *l;
653 struct btrfs_qgroup_limit_item *qgroup_limit;
658 key.type = BTRFS_QGROUP_LIMIT_KEY;
659 key.offset = qgroup->qgroupid;
661 path = btrfs_alloc_path();
665 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
673 slot = path->slots[0];
674 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
675 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
676 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
677 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
678 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
679 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
681 btrfs_mark_buffer_dirty(l);
684 btrfs_free_path(path);
688 static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
689 struct btrfs_root *root,
690 struct btrfs_qgroup *qgroup)
692 struct btrfs_path *path;
693 struct btrfs_key key;
694 struct extent_buffer *l;
695 struct btrfs_qgroup_info_item *qgroup_info;
699 if (btrfs_test_is_dummy_root(root))
703 key.type = BTRFS_QGROUP_INFO_KEY;
704 key.offset = qgroup->qgroupid;
706 path = btrfs_alloc_path();
710 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
718 slot = path->slots[0];
719 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
720 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
721 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
722 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
723 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
724 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
726 btrfs_mark_buffer_dirty(l);
729 btrfs_free_path(path);
733 static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
734 struct btrfs_fs_info *fs_info,
735 struct btrfs_root *root)
737 struct btrfs_path *path;
738 struct btrfs_key key;
739 struct extent_buffer *l;
740 struct btrfs_qgroup_status_item *ptr;
745 key.type = BTRFS_QGROUP_STATUS_KEY;
748 path = btrfs_alloc_path();
752 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
760 slot = path->slots[0];
761 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
762 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
763 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
764 btrfs_set_qgroup_status_rescan(l, ptr,
765 fs_info->qgroup_rescan_progress.objectid);
767 btrfs_mark_buffer_dirty(l);
770 btrfs_free_path(path);
775 * called with qgroup_lock held
777 static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
778 struct btrfs_root *root)
780 struct btrfs_path *path;
781 struct btrfs_key key;
782 struct extent_buffer *leaf = NULL;
786 path = btrfs_alloc_path();
790 path->leave_spinning = 1;
797 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
800 leaf = path->nodes[0];
801 nr = btrfs_header_nritems(leaf);
805 * delete the leaf one by one
806 * since the whole tree is going
810 ret = btrfs_del_items(trans, root, path, 0, nr);
814 btrfs_release_path(path);
818 root->fs_info->pending_quota_state = 0;
819 btrfs_free_path(path);
823 int btrfs_quota_enable(struct btrfs_trans_handle *trans,
824 struct btrfs_fs_info *fs_info)
826 struct btrfs_root *quota_root;
827 struct btrfs_root *tree_root = fs_info->tree_root;
828 struct btrfs_path *path = NULL;
829 struct btrfs_qgroup_status_item *ptr;
830 struct extent_buffer *leaf;
831 struct btrfs_key key;
832 struct btrfs_key found_key;
833 struct btrfs_qgroup *qgroup = NULL;
837 mutex_lock(&fs_info->qgroup_ioctl_lock);
838 if (fs_info->quota_root) {
839 fs_info->pending_quota_state = 1;
843 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
844 if (!fs_info->qgroup_ulist) {
850 * initially create the quota tree
852 quota_root = btrfs_create_tree(trans, fs_info,
853 BTRFS_QUOTA_TREE_OBJECTID);
854 if (IS_ERR(quota_root)) {
855 ret = PTR_ERR(quota_root);
859 path = btrfs_alloc_path();
866 key.type = BTRFS_QGROUP_STATUS_KEY;
869 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
874 leaf = path->nodes[0];
875 ptr = btrfs_item_ptr(leaf, path->slots[0],
876 struct btrfs_qgroup_status_item);
877 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
878 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
879 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
880 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
881 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
882 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
884 btrfs_mark_buffer_dirty(leaf);
887 key.type = BTRFS_ROOT_REF_KEY;
890 btrfs_release_path(path);
891 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
899 slot = path->slots[0];
900 leaf = path->nodes[0];
901 btrfs_item_key_to_cpu(leaf, &found_key, slot);
903 if (found_key.type == BTRFS_ROOT_REF_KEY) {
904 ret = add_qgroup_item(trans, quota_root,
909 qgroup = add_qgroup_rb(fs_info, found_key.offset);
910 if (IS_ERR(qgroup)) {
911 ret = PTR_ERR(qgroup);
915 ret = btrfs_next_item(tree_root, path);
923 btrfs_release_path(path);
924 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
928 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
929 if (IS_ERR(qgroup)) {
930 ret = PTR_ERR(qgroup);
933 spin_lock(&fs_info->qgroup_lock);
934 fs_info->quota_root = quota_root;
935 fs_info->pending_quota_state = 1;
936 spin_unlock(&fs_info->qgroup_lock);
938 btrfs_free_path(path);
941 free_extent_buffer(quota_root->node);
942 free_extent_buffer(quota_root->commit_root);
947 ulist_free(fs_info->qgroup_ulist);
948 fs_info->qgroup_ulist = NULL;
950 mutex_unlock(&fs_info->qgroup_ioctl_lock);
954 int btrfs_quota_disable(struct btrfs_trans_handle *trans,
955 struct btrfs_fs_info *fs_info)
957 struct btrfs_root *tree_root = fs_info->tree_root;
958 struct btrfs_root *quota_root;
961 mutex_lock(&fs_info->qgroup_ioctl_lock);
962 if (!fs_info->quota_root)
964 spin_lock(&fs_info->qgroup_lock);
965 fs_info->quota_enabled = 0;
966 fs_info->pending_quota_state = 0;
967 quota_root = fs_info->quota_root;
968 fs_info->quota_root = NULL;
969 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
970 spin_unlock(&fs_info->qgroup_lock);
972 btrfs_free_qgroup_config(fs_info);
974 ret = btrfs_clean_quota_tree(trans, quota_root);
978 ret = btrfs_del_root(trans, tree_root, "a_root->root_key);
982 list_del("a_root->dirty_list);
984 btrfs_tree_lock(quota_root->node);
985 clean_tree_block(trans, tree_root->fs_info, quota_root->node);
986 btrfs_tree_unlock(quota_root->node);
987 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
989 free_extent_buffer(quota_root->node);
990 free_extent_buffer(quota_root->commit_root);
993 mutex_unlock(&fs_info->qgroup_ioctl_lock);
997 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
998 struct btrfs_qgroup *qgroup)
1000 if (list_empty(&qgroup->dirty))
1001 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1004 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1005 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1007 struct btrfs_root *quota_root;
1008 struct btrfs_qgroup *parent;
1009 struct btrfs_qgroup *member;
1010 struct btrfs_qgroup_list *list;
1013 /* Check the level of src and dst first */
1014 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1017 mutex_lock(&fs_info->qgroup_ioctl_lock);
1018 quota_root = fs_info->quota_root;
1023 member = find_qgroup_rb(fs_info, src);
1024 parent = find_qgroup_rb(fs_info, dst);
1025 if (!member || !parent) {
1030 /* check if such qgroup relation exist firstly */
1031 list_for_each_entry(list, &member->groups, next_group) {
1032 if (list->group == parent) {
1038 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1042 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1044 del_qgroup_relation_item(trans, quota_root, src, dst);
1048 spin_lock(&fs_info->qgroup_lock);
1049 ret = add_relation_rb(quota_root->fs_info, src, dst);
1050 spin_unlock(&fs_info->qgroup_lock);
1052 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1056 int __del_qgroup_relation(struct btrfs_trans_handle *trans,
1057 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1059 struct btrfs_root *quota_root;
1060 struct btrfs_qgroup *parent;
1061 struct btrfs_qgroup *member;
1062 struct btrfs_qgroup_list *list;
1066 quota_root = fs_info->quota_root;
1072 member = find_qgroup_rb(fs_info, src);
1073 parent = find_qgroup_rb(fs_info, dst);
1074 if (!member || !parent) {
1079 /* check if such qgroup relation exist firstly */
1080 list_for_each_entry(list, &member->groups, next_group) {
1081 if (list->group == parent)
1087 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1088 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1092 spin_lock(&fs_info->qgroup_lock);
1093 del_relation_rb(fs_info, src, dst);
1094 spin_unlock(&fs_info->qgroup_lock);
1099 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1100 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1104 mutex_lock(&fs_info->qgroup_ioctl_lock);
1105 ret = __del_qgroup_relation(trans, fs_info, src, dst);
1106 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1111 int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
1112 struct btrfs_fs_info *fs_info, u64 qgroupid)
1114 struct btrfs_root *quota_root;
1115 struct btrfs_qgroup *qgroup;
1118 mutex_lock(&fs_info->qgroup_ioctl_lock);
1119 quota_root = fs_info->quota_root;
1124 qgroup = find_qgroup_rb(fs_info, qgroupid);
1130 ret = add_qgroup_item(trans, quota_root, qgroupid);
1134 spin_lock(&fs_info->qgroup_lock);
1135 qgroup = add_qgroup_rb(fs_info, qgroupid);
1136 spin_unlock(&fs_info->qgroup_lock);
1139 ret = PTR_ERR(qgroup);
1141 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1145 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1146 struct btrfs_fs_info *fs_info, u64 qgroupid)
1148 struct btrfs_root *quota_root;
1149 struct btrfs_qgroup *qgroup;
1150 struct btrfs_qgroup_list *list;
1153 mutex_lock(&fs_info->qgroup_ioctl_lock);
1154 quota_root = fs_info->quota_root;
1160 qgroup = find_qgroup_rb(fs_info, qgroupid);
1165 /* check if there are no children of this qgroup */
1166 if (!list_empty(&qgroup->members)) {
1171 ret = del_qgroup_item(trans, quota_root, qgroupid);
1173 while (!list_empty(&qgroup->groups)) {
1174 list = list_first_entry(&qgroup->groups,
1175 struct btrfs_qgroup_list, next_group);
1176 ret = __del_qgroup_relation(trans, fs_info,
1178 list->group->qgroupid);
1183 spin_lock(&fs_info->qgroup_lock);
1184 del_qgroup_rb(quota_root->fs_info, qgroupid);
1185 spin_unlock(&fs_info->qgroup_lock);
1187 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1191 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1192 struct btrfs_fs_info *fs_info, u64 qgroupid,
1193 struct btrfs_qgroup_limit *limit)
1195 struct btrfs_root *quota_root;
1196 struct btrfs_qgroup *qgroup;
1199 mutex_lock(&fs_info->qgroup_ioctl_lock);
1200 quota_root = fs_info->quota_root;
1206 qgroup = find_qgroup_rb(fs_info, qgroupid);
1212 spin_lock(&fs_info->qgroup_lock);
1213 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER)
1214 qgroup->max_rfer = limit->max_rfer;
1215 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL)
1216 qgroup->max_excl = limit->max_excl;
1217 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER)
1218 qgroup->rsv_rfer = limit->rsv_rfer;
1219 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL)
1220 qgroup->rsv_excl = limit->rsv_excl;
1221 qgroup->lim_flags |= limit->flags;
1223 spin_unlock(&fs_info->qgroup_lock);
1225 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1227 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1228 btrfs_info(fs_info, "unable to update quota limit for %llu",
1233 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1237 static int comp_oper_exist(struct btrfs_qgroup_operation *oper1,
1238 struct btrfs_qgroup_operation *oper2)
1241 * Ignore seq and type here, we're looking for any operation
1242 * at all related to this extent on that root.
1244 if (oper1->bytenr < oper2->bytenr)
1246 if (oper1->bytenr > oper2->bytenr)
1248 if (oper1->ref_root < oper2->ref_root)
1250 if (oper1->ref_root > oper2->ref_root)
1255 static int qgroup_oper_exists(struct btrfs_fs_info *fs_info,
1256 struct btrfs_qgroup_operation *oper)
1259 struct btrfs_qgroup_operation *cur;
1262 spin_lock(&fs_info->qgroup_op_lock);
1263 n = fs_info->qgroup_op_tree.rb_node;
1265 cur = rb_entry(n, struct btrfs_qgroup_operation, n);
1266 cmp = comp_oper_exist(cur, oper);
1272 spin_unlock(&fs_info->qgroup_op_lock);
1276 spin_unlock(&fs_info->qgroup_op_lock);
1280 static int comp_oper(struct btrfs_qgroup_operation *oper1,
1281 struct btrfs_qgroup_operation *oper2)
1283 if (oper1->bytenr < oper2->bytenr)
1285 if (oper1->bytenr > oper2->bytenr)
1287 if (oper1->ref_root < oper2->ref_root)
1289 if (oper1->ref_root > oper2->ref_root)
1291 if (oper1->seq < oper2->seq)
1293 if (oper1->seq > oper2->seq)
1295 if (oper1->type < oper2->type)
1297 if (oper1->type > oper2->type)
1302 static int insert_qgroup_oper(struct btrfs_fs_info *fs_info,
1303 struct btrfs_qgroup_operation *oper)
1306 struct rb_node *parent = NULL;
1307 struct btrfs_qgroup_operation *cur;
1310 spin_lock(&fs_info->qgroup_op_lock);
1311 p = &fs_info->qgroup_op_tree.rb_node;
1314 cur = rb_entry(parent, struct btrfs_qgroup_operation, n);
1315 cmp = comp_oper(cur, oper);
1317 p = &(*p)->rb_right;
1321 spin_unlock(&fs_info->qgroup_op_lock);
1325 rb_link_node(&oper->n, parent, p);
1326 rb_insert_color(&oper->n, &fs_info->qgroup_op_tree);
1327 spin_unlock(&fs_info->qgroup_op_lock);
1332 * Record a quota operation for processing later on.
1333 * @trans: the transaction we are adding the delayed op to.
1334 * @fs_info: the fs_info for this fs.
1335 * @ref_root: the root of the reference we are acting on,
1336 * @bytenr: the bytenr we are acting on.
1337 * @num_bytes: the number of bytes in the reference.
1338 * @type: the type of operation this is.
1339 * @mod_seq: do we need to get a sequence number for looking up roots.
1341 * We just add it to our trans qgroup_ref_list and carry on and process these
1342 * operations in order at some later point. If the reference root isn't a fs
1343 * root then we don't bother with doing anything.
1345 * MUST BE HOLDING THE REF LOCK.
1347 int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
1348 struct btrfs_fs_info *fs_info, u64 ref_root,
1349 u64 bytenr, u64 num_bytes,
1350 enum btrfs_qgroup_operation_type type, int mod_seq)
1352 struct btrfs_qgroup_operation *oper;
1355 if (!is_fstree(ref_root) || !fs_info->quota_enabled)
1358 oper = kmalloc(sizeof(*oper), GFP_NOFS);
1362 oper->ref_root = ref_root;
1363 oper->bytenr = bytenr;
1364 oper->num_bytes = num_bytes;
1366 oper->seq = atomic_inc_return(&fs_info->qgroup_op_seq);
1367 INIT_LIST_HEAD(&oper->elem.list);
1370 trace_btrfs_qgroup_record_ref(oper);
1372 if (type == BTRFS_QGROUP_OPER_SUB_SUBTREE) {
1374 * If any operation for this bytenr/ref_root combo
1375 * exists, then we know it's not exclusively owned and
1376 * shouldn't be queued up.
1378 * This also catches the case where we have a cloned
1379 * extent that gets queued up multiple times during
1382 if (qgroup_oper_exists(fs_info, oper)) {
1388 ret = insert_qgroup_oper(fs_info, oper);
1390 /* Shouldn't happen so have an assert for developers */
1395 list_add_tail(&oper->list, &trans->qgroup_ref_list);
1398 btrfs_get_tree_mod_seq(fs_info, &oper->elem);
1404 * The easy accounting, if we are adding/removing the only ref for an extent
1405 * then this qgroup and all of the parent qgroups get their refrence and
1406 * exclusive counts adjusted.
1408 static int qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1409 struct btrfs_qgroup_operation *oper)
1411 struct btrfs_qgroup *qgroup;
1413 struct btrfs_qgroup_list *glist;
1414 struct ulist_node *unode;
1415 struct ulist_iterator uiter;
1419 tmp = ulist_alloc(GFP_NOFS);
1423 spin_lock(&fs_info->qgroup_lock);
1424 if (!fs_info->quota_root)
1426 qgroup = find_qgroup_rb(fs_info, oper->ref_root);
1429 switch (oper->type) {
1430 case BTRFS_QGROUP_OPER_ADD_EXCL:
1433 case BTRFS_QGROUP_OPER_SUB_EXCL:
1439 qgroup->rfer += sign * oper->num_bytes;
1440 qgroup->rfer_cmpr += sign * oper->num_bytes;
1442 WARN_ON(sign < 0 && qgroup->excl < oper->num_bytes);
1443 qgroup->excl += sign * oper->num_bytes;
1444 qgroup->excl_cmpr += sign * oper->num_bytes;
1446 qgroup->reserved -= oper->num_bytes;
1448 qgroup_dirty(fs_info, qgroup);
1450 /* Get all of the parent groups that contain this qgroup */
1451 list_for_each_entry(glist, &qgroup->groups, next_group) {
1452 ret = ulist_add(tmp, glist->group->qgroupid,
1453 ptr_to_u64(glist->group), GFP_ATOMIC);
1458 /* Iterate all of the parents and adjust their reference counts */
1459 ULIST_ITER_INIT(&uiter);
1460 while ((unode = ulist_next(tmp, &uiter))) {
1461 qgroup = u64_to_ptr(unode->aux);
1462 qgroup->rfer += sign * oper->num_bytes;
1463 qgroup->rfer_cmpr += sign * oper->num_bytes;
1464 WARN_ON(sign < 0 && qgroup->excl < oper->num_bytes);
1465 qgroup->excl += sign * oper->num_bytes;
1467 qgroup->reserved -= oper->num_bytes;
1468 qgroup->excl_cmpr += sign * oper->num_bytes;
1469 qgroup_dirty(fs_info, qgroup);
1471 /* Add any parents of the parents */
1472 list_for_each_entry(glist, &qgroup->groups, next_group) {
1473 ret = ulist_add(tmp, glist->group->qgroupid,
1474 ptr_to_u64(glist->group), GFP_ATOMIC);
1481 spin_unlock(&fs_info->qgroup_lock);
1487 * Walk all of the roots that pointed to our bytenr and adjust their refcnts as
1490 static int qgroup_calc_old_refcnt(struct btrfs_fs_info *fs_info,
1491 u64 root_to_skip, struct ulist *tmp,
1492 struct ulist *roots, struct ulist *qgroups,
1493 u64 seq, int *old_roots, int rescan)
1495 struct ulist_node *unode;
1496 struct ulist_iterator uiter;
1497 struct ulist_node *tmp_unode;
1498 struct ulist_iterator tmp_uiter;
1499 struct btrfs_qgroup *qg;
1502 ULIST_ITER_INIT(&uiter);
1503 while ((unode = ulist_next(roots, &uiter))) {
1504 /* We don't count our current root here */
1505 if (unode->val == root_to_skip)
1507 qg = find_qgroup_rb(fs_info, unode->val);
1511 * We could have a pending removal of this same ref so we may
1512 * not have actually found our ref root when doing
1513 * btrfs_find_all_roots, so we need to keep track of how many
1514 * old roots we find in case we removed ours and added a
1515 * different one at the same time. I don't think this could
1516 * happen in practice but that sort of thinking leads to pain
1517 * and suffering and to the dark side.
1522 ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
1526 ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC);
1529 ULIST_ITER_INIT(&tmp_uiter);
1530 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1531 struct btrfs_qgroup_list *glist;
1533 qg = u64_to_ptr(tmp_unode->aux);
1535 * We use this sequence number to keep from having to
1536 * run the whole list and 0 out the refcnt every time.
1537 * We basically use sequnce as the known 0 count and
1538 * then add 1 everytime we see a qgroup. This is how we
1539 * get how many of the roots actually point up to the
1540 * upper level qgroups in order to determine exclusive
1543 * For rescan we want to set old_refcnt to seq so our
1544 * exclusive calculations end up correct.
1547 qg->old_refcnt = seq;
1548 else if (qg->old_refcnt < seq)
1549 qg->old_refcnt = seq + 1;
1553 if (qg->new_refcnt < seq)
1554 qg->new_refcnt = seq + 1;
1557 list_for_each_entry(glist, &qg->groups, next_group) {
1558 ret = ulist_add(qgroups, glist->group->qgroupid,
1559 ptr_to_u64(glist->group),
1563 ret = ulist_add(tmp, glist->group->qgroupid,
1564 ptr_to_u64(glist->group),
1575 * We need to walk forward in our operation tree and account for any roots that
1576 * were deleted after we made this operation.
1578 static int qgroup_account_deleted_refs(struct btrfs_fs_info *fs_info,
1579 struct btrfs_qgroup_operation *oper,
1581 struct ulist *qgroups, u64 seq,
1584 struct ulist_node *unode;
1585 struct ulist_iterator uiter;
1586 struct btrfs_qgroup *qg;
1587 struct btrfs_qgroup_operation *tmp_oper;
1594 * We only walk forward in the tree since we're only interested in
1595 * removals that happened _after_ our operation.
1597 spin_lock(&fs_info->qgroup_op_lock);
1598 n = rb_next(&oper->n);
1599 spin_unlock(&fs_info->qgroup_op_lock);
1602 tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
1603 while (tmp_oper->bytenr == oper->bytenr) {
1605 * If it's not a removal we don't care, additions work out
1606 * properly with our refcnt tracking.
1608 if (tmp_oper->type != BTRFS_QGROUP_OPER_SUB_SHARED &&
1609 tmp_oper->type != BTRFS_QGROUP_OPER_SUB_EXCL)
1611 qg = find_qgroup_rb(fs_info, tmp_oper->ref_root);
1614 ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
1620 * We only want to increase old_roots if this qgroup is
1621 * not already in the list of qgroups. If it is already
1622 * there then that means it must have been re-added or
1623 * the delete will be discarded because we had an
1624 * existing ref that we haven't looked up yet. In this
1625 * case we don't want to increase old_roots. So if ret
1626 * == 1 then we know that this is the first time we've
1627 * seen this qgroup and we can bump the old_roots.
1630 ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg),
1636 spin_lock(&fs_info->qgroup_op_lock);
1637 n = rb_next(&tmp_oper->n);
1638 spin_unlock(&fs_info->qgroup_op_lock);
1641 tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
1644 /* Ok now process the qgroups we found */
1645 ULIST_ITER_INIT(&uiter);
1646 while ((unode = ulist_next(tmp, &uiter))) {
1647 struct btrfs_qgroup_list *glist;
1649 qg = u64_to_ptr(unode->aux);
1650 if (qg->old_refcnt < seq)
1651 qg->old_refcnt = seq + 1;
1654 if (qg->new_refcnt < seq)
1655 qg->new_refcnt = seq + 1;
1658 list_for_each_entry(glist, &qg->groups, next_group) {
1659 ret = ulist_add(qgroups, glist->group->qgroupid,
1660 ptr_to_u64(glist->group), GFP_ATOMIC);
1663 ret = ulist_add(tmp, glist->group->qgroupid,
1664 ptr_to_u64(glist->group), GFP_ATOMIC);
1672 /* Add refcnt for the newly added reference. */
1673 static int qgroup_calc_new_refcnt(struct btrfs_fs_info *fs_info,
1674 struct btrfs_qgroup_operation *oper,
1675 struct btrfs_qgroup *qgroup,
1676 struct ulist *tmp, struct ulist *qgroups,
1679 struct ulist_node *unode;
1680 struct ulist_iterator uiter;
1681 struct btrfs_qgroup *qg;
1685 ret = ulist_add(qgroups, qgroup->qgroupid, ptr_to_u64(qgroup),
1689 ret = ulist_add(tmp, qgroup->qgroupid, ptr_to_u64(qgroup),
1693 ULIST_ITER_INIT(&uiter);
1694 while ((unode = ulist_next(tmp, &uiter))) {
1695 struct btrfs_qgroup_list *glist;
1697 qg = u64_to_ptr(unode->aux);
1698 if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
1699 if (qg->new_refcnt < seq)
1700 qg->new_refcnt = seq + 1;
1704 if (qg->old_refcnt < seq)
1705 qg->old_refcnt = seq + 1;
1709 list_for_each_entry(glist, &qg->groups, next_group) {
1710 ret = ulist_add(tmp, glist->group->qgroupid,
1711 ptr_to_u64(glist->group), GFP_ATOMIC);
1714 ret = ulist_add(qgroups, glist->group->qgroupid,
1715 ptr_to_u64(glist->group), GFP_ATOMIC);
1724 * This adjusts the counters for all referenced qgroups if need be.
1726 static int qgroup_adjust_counters(struct btrfs_fs_info *fs_info,
1727 u64 root_to_skip, u64 num_bytes,
1728 struct ulist *qgroups, u64 seq,
1729 int old_roots, int new_roots, int rescan)
1731 struct ulist_node *unode;
1732 struct ulist_iterator uiter;
1733 struct btrfs_qgroup *qg;
1734 u64 cur_new_count, cur_old_count;
1736 ULIST_ITER_INIT(&uiter);
1737 while ((unode = ulist_next(qgroups, &uiter))) {
1740 qg = u64_to_ptr(unode->aux);
1742 * Wasn't referenced before but is now, add to the reference
1745 if (qg->old_refcnt <= seq && qg->new_refcnt > seq) {
1746 qg->rfer += num_bytes;
1747 qg->rfer_cmpr += num_bytes;
1752 * Was referenced before but isn't now, subtract from the
1753 * reference counters.
1755 if (qg->old_refcnt > seq && qg->new_refcnt <= seq) {
1756 qg->rfer -= num_bytes;
1757 qg->rfer_cmpr -= num_bytes;
1761 if (qg->old_refcnt < seq)
1764 cur_old_count = qg->old_refcnt - seq;
1765 if (qg->new_refcnt < seq)
1768 cur_new_count = qg->new_refcnt - seq;
1771 * If our refcount was the same as the roots previously but our
1772 * new count isn't the same as the number of roots now then we
1773 * went from having a exclusive reference on this range to not.
1775 if (old_roots && cur_old_count == old_roots &&
1776 (cur_new_count != new_roots || new_roots == 0)) {
1777 WARN_ON(cur_new_count != new_roots && new_roots == 0);
1778 qg->excl -= num_bytes;
1779 qg->excl_cmpr -= num_bytes;
1784 * If we didn't reference all the roots before but now we do we
1785 * have an exclusive reference to this range.
1787 if ((!old_roots || (old_roots && cur_old_count != old_roots))
1788 && cur_new_count == new_roots) {
1789 qg->excl += num_bytes;
1790 qg->excl_cmpr += num_bytes;
1795 qgroup_dirty(fs_info, qg);
1801 * If we removed a data extent and there were other references for that bytenr
1802 * then we need to lookup all referenced roots to make sure we still don't
1803 * reference this bytenr. If we do then we can just discard this operation.
1805 static int check_existing_refs(struct btrfs_trans_handle *trans,
1806 struct btrfs_fs_info *fs_info,
1807 struct btrfs_qgroup_operation *oper)
1809 struct ulist *roots = NULL;
1810 struct ulist_node *unode;
1811 struct ulist_iterator uiter;
1814 ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
1815 oper->elem.seq, &roots);
1820 ULIST_ITER_INIT(&uiter);
1821 while ((unode = ulist_next(roots, &uiter))) {
1822 if (unode->val == oper->ref_root) {
1828 btrfs_put_tree_mod_seq(fs_info, &oper->elem);
1834 * If we share a reference across multiple roots then we may need to adjust
1835 * various qgroups referenced and exclusive counters. The basic premise is this
1837 * 1) We have seq to represent a 0 count. Instead of looping through all of the
1838 * qgroups and resetting their refcount to 0 we just constantly bump this
1839 * sequence number to act as the base reference count. This means that if
1840 * anybody is equal to or below this sequence they were never referenced. We
1841 * jack this sequence up by the number of roots we found each time in order to
1842 * make sure we don't have any overlap.
1844 * 2) We first search all the roots that reference the area _except_ the root
1845 * we're acting on currently. This makes up the old_refcnt of all the qgroups
1848 * 3) We walk all of the qgroups referenced by the root we are currently acting
1849 * on, and will either adjust old_refcnt in the case of a removal or the
1850 * new_refcnt in the case of an addition.
1852 * 4) Finally we walk all the qgroups that are referenced by this range
1853 * including the root we are acting on currently. We will adjust the counters
1854 * based on the number of roots we had and will have after this operation.
1856 * Take this example as an illustration
1860 * [qg 0/0] [qg 0/1] [qg 0/2]
1864 * Say we are adding a reference that is covered by qg 0/0. The first step
1865 * would give a refcnt of 1 to qg 0/1 and 0/2 and a refcnt of 2 to qg 1/0 with
1866 * old_roots being 2. Because it is adding new_roots will be 1. We then go
1867 * through qg 0/0 which will get the new_refcnt set to 1 and add 1 to qg 1/0's
1868 * new_refcnt, bringing it to 3. We then walk through all of the qgroups, we
1869 * notice that the old refcnt for qg 0/0 < the new refcnt, so we added a
1870 * reference and thus must add the size to the referenced bytes. Everything
1871 * else is the same so nothing else changes.
1873 static int qgroup_shared_accounting(struct btrfs_trans_handle *trans,
1874 struct btrfs_fs_info *fs_info,
1875 struct btrfs_qgroup_operation *oper)
1877 struct ulist *roots = NULL;
1878 struct ulist *qgroups, *tmp;
1879 struct btrfs_qgroup *qgroup;
1880 struct seq_list elem = SEQ_LIST_INIT(elem);
1886 if (oper->elem.seq) {
1887 ret = check_existing_refs(trans, fs_info, oper);
1894 qgroups = ulist_alloc(GFP_NOFS);
1898 tmp = ulist_alloc(GFP_NOFS);
1900 ulist_free(qgroups);
1904 btrfs_get_tree_mod_seq(fs_info, &elem);
1905 ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, elem.seq,
1907 btrfs_put_tree_mod_seq(fs_info, &elem);
1909 ulist_free(qgroups);
1913 spin_lock(&fs_info->qgroup_lock);
1914 qgroup = find_qgroup_rb(fs_info, oper->ref_root);
1917 seq = fs_info->qgroup_seq;
1920 * So roots is the list of all the roots currently pointing at the
1921 * bytenr, including the ref we are adding if we are adding, or not if
1922 * we are removing a ref. So we pass in the ref_root to skip that root
1923 * in our calculations. We set old_refnct and new_refcnt cause who the
1924 * hell knows what everything looked like before, and it doesn't matter
1927 ret = qgroup_calc_old_refcnt(fs_info, oper->ref_root, tmp, roots, qgroups,
1928 seq, &old_roots, 0);
1933 * Now adjust the refcounts of the qgroups that care about this
1934 * reference, either the old_count in the case of removal or new_count
1935 * in the case of an addition.
1937 ret = qgroup_calc_new_refcnt(fs_info, oper, qgroup, tmp, qgroups,
1943 * ...in the case of removals. If we had a removal before we got around
1944 * to processing this operation then we need to find that guy and count
1945 * his references as if they really existed so we don't end up screwing
1946 * up the exclusive counts. Then whenever we go to process the delete
1947 * everything will be grand and we can account for whatever exclusive
1948 * changes need to be made there. We also have to pass in old_roots so
1949 * we have an accurate count of the roots as it pertains to this
1950 * operations view of the world.
1952 ret = qgroup_account_deleted_refs(fs_info, oper, tmp, qgroups, seq,
1958 * We are adding our root, need to adjust up the number of roots,
1959 * otherwise old_roots is the number of roots we want.
1961 if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
1962 new_roots = old_roots + 1;
1964 new_roots = old_roots;
1967 fs_info->qgroup_seq += old_roots + 1;
1971 * And now the magic happens, bless Arne for having a pretty elegant
1972 * solution for this.
1974 qgroup_adjust_counters(fs_info, oper->ref_root, oper->num_bytes,
1975 qgroups, seq, old_roots, new_roots, 0);
1977 spin_unlock(&fs_info->qgroup_lock);
1978 ulist_free(qgroups);
1985 * Process a reference to a shared subtree. This type of operation is
1986 * queued during snapshot removal when we encounter extents which are
1987 * shared between more than one root.
1989 static int qgroup_subtree_accounting(struct btrfs_trans_handle *trans,
1990 struct btrfs_fs_info *fs_info,
1991 struct btrfs_qgroup_operation *oper)
1993 struct ulist *roots = NULL;
1994 struct ulist_node *unode;
1995 struct ulist_iterator uiter;
1996 struct btrfs_qgroup_list *glist;
1997 struct ulist *parents;
2000 struct btrfs_qgroup *qg;
2002 struct seq_list elem = SEQ_LIST_INIT(elem);
2004 parents = ulist_alloc(GFP_NOFS);
2008 btrfs_get_tree_mod_seq(fs_info, &elem);
2009 ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
2011 btrfs_put_tree_mod_seq(fs_info, &elem);
2015 if (roots->nnodes != 1)
2018 ULIST_ITER_INIT(&uiter);
2019 unode = ulist_next(roots, &uiter); /* Only want 1 so no need to loop */
2021 * If we find our ref root then that means all refs
2022 * this extent has to the root have not yet been
2023 * deleted. In that case, we do nothing and let the
2024 * last ref for this bytenr drive our update.
2026 * This can happen for example if an extent is
2027 * referenced multiple times in a snapshot (clone,
2028 * etc). If we are in the middle of snapshot removal,
2029 * queued updates for such an extent will find the
2030 * root if we have not yet finished removing the
2033 if (unode->val == oper->ref_root)
2036 root_obj = unode->val;
2039 spin_lock(&fs_info->qgroup_lock);
2040 qg = find_qgroup_rb(fs_info, root_obj);
2044 qg->excl += oper->num_bytes;
2045 qg->excl_cmpr += oper->num_bytes;
2046 qgroup_dirty(fs_info, qg);
2049 * Adjust counts for parent groups. First we find all
2050 * parents, then in the 2nd loop we do the adjustment
2051 * while adding parents of the parents to our ulist.
2053 list_for_each_entry(glist, &qg->groups, next_group) {
2054 err = ulist_add(parents, glist->group->qgroupid,
2055 ptr_to_u64(glist->group), GFP_ATOMIC);
2062 ULIST_ITER_INIT(&uiter);
2063 while ((unode = ulist_next(parents, &uiter))) {
2064 qg = u64_to_ptr(unode->aux);
2065 qg->excl += oper->num_bytes;
2066 qg->excl_cmpr += oper->num_bytes;
2067 qgroup_dirty(fs_info, qg);
2069 /* Add any parents of the parents */
2070 list_for_each_entry(glist, &qg->groups, next_group) {
2071 err = ulist_add(parents, glist->group->qgroupid,
2072 ptr_to_u64(glist->group), GFP_ATOMIC);
2081 spin_unlock(&fs_info->qgroup_lock);
2085 ulist_free(parents);
2090 * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
2091 * from the fs. First, all roots referencing the extent are searched, and
2092 * then the space is accounted accordingly to the different roots. The
2093 * accounting algorithm works in 3 steps documented inline.
2095 static int btrfs_qgroup_account(struct btrfs_trans_handle *trans,
2096 struct btrfs_fs_info *fs_info,
2097 struct btrfs_qgroup_operation *oper)
2101 if (!fs_info->quota_enabled)
2104 BUG_ON(!fs_info->quota_root);
2106 mutex_lock(&fs_info->qgroup_rescan_lock);
2107 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2108 if (fs_info->qgroup_rescan_progress.objectid <= oper->bytenr) {
2109 mutex_unlock(&fs_info->qgroup_rescan_lock);
2113 mutex_unlock(&fs_info->qgroup_rescan_lock);
2115 ASSERT(is_fstree(oper->ref_root));
2117 trace_btrfs_qgroup_account(oper);
2119 switch (oper->type) {
2120 case BTRFS_QGROUP_OPER_ADD_EXCL:
2121 case BTRFS_QGROUP_OPER_SUB_EXCL:
2122 ret = qgroup_excl_accounting(fs_info, oper);
2124 case BTRFS_QGROUP_OPER_ADD_SHARED:
2125 case BTRFS_QGROUP_OPER_SUB_SHARED:
2126 ret = qgroup_shared_accounting(trans, fs_info, oper);
2128 case BTRFS_QGROUP_OPER_SUB_SUBTREE:
2129 ret = qgroup_subtree_accounting(trans, fs_info, oper);
2138 * Needs to be called everytime we run delayed refs, even if there is an error
2139 * in order to cleanup outstanding operations.
2141 int btrfs_delayed_qgroup_accounting(struct btrfs_trans_handle *trans,
2142 struct btrfs_fs_info *fs_info)
2144 struct btrfs_qgroup_operation *oper;
2147 while (!list_empty(&trans->qgroup_ref_list)) {
2148 oper = list_first_entry(&trans->qgroup_ref_list,
2149 struct btrfs_qgroup_operation, list);
2150 list_del_init(&oper->list);
2151 if (!ret || !trans->aborted)
2152 ret = btrfs_qgroup_account(trans, fs_info, oper);
2153 spin_lock(&fs_info->qgroup_op_lock);
2154 rb_erase(&oper->n, &fs_info->qgroup_op_tree);
2155 spin_unlock(&fs_info->qgroup_op_lock);
2156 btrfs_put_tree_mod_seq(fs_info, &oper->elem);
2163 * called from commit_transaction. Writes all changed qgroups to disk.
2165 int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2166 struct btrfs_fs_info *fs_info)
2168 struct btrfs_root *quota_root = fs_info->quota_root;
2170 int start_rescan_worker = 0;
2175 if (!fs_info->quota_enabled && fs_info->pending_quota_state)
2176 start_rescan_worker = 1;
2178 fs_info->quota_enabled = fs_info->pending_quota_state;
2180 spin_lock(&fs_info->qgroup_lock);
2181 while (!list_empty(&fs_info->dirty_qgroups)) {
2182 struct btrfs_qgroup *qgroup;
2183 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2184 struct btrfs_qgroup, dirty);
2185 list_del_init(&qgroup->dirty);
2186 spin_unlock(&fs_info->qgroup_lock);
2187 ret = update_qgroup_info_item(trans, quota_root, qgroup);
2189 fs_info->qgroup_flags |=
2190 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2191 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
2193 fs_info->qgroup_flags |=
2194 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2195 spin_lock(&fs_info->qgroup_lock);
2197 if (fs_info->quota_enabled)
2198 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2200 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2201 spin_unlock(&fs_info->qgroup_lock);
2203 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2205 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2207 if (!ret && start_rescan_worker) {
2208 ret = qgroup_rescan_init(fs_info, 0, 1);
2210 qgroup_rescan_zero_tracking(fs_info);
2211 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2212 &fs_info->qgroup_rescan_work);
2223 * copy the acounting information between qgroups. This is necessary when a
2224 * snapshot or a subvolume is created
2226 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2227 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2228 struct btrfs_qgroup_inherit *inherit)
2233 struct btrfs_root *quota_root = fs_info->quota_root;
2234 struct btrfs_qgroup *srcgroup;
2235 struct btrfs_qgroup *dstgroup;
2239 mutex_lock(&fs_info->qgroup_ioctl_lock);
2240 if (!fs_info->quota_enabled)
2249 i_qgroups = (u64 *)(inherit + 1);
2250 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2251 2 * inherit->num_excl_copies;
2252 for (i = 0; i < nums; ++i) {
2253 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
2259 if ((srcgroup->qgroupid >> 48) <= (objectid >> 48)) {
2268 * create a tracking group for the subvol itself
2270 ret = add_qgroup_item(trans, quota_root, objectid);
2275 struct btrfs_root *srcroot;
2276 struct btrfs_key srckey;
2278 srckey.objectid = srcid;
2279 srckey.type = BTRFS_ROOT_ITEM_KEY;
2280 srckey.offset = (u64)-1;
2281 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2282 if (IS_ERR(srcroot)) {
2283 ret = PTR_ERR(srcroot);
2288 level_size = srcroot->nodesize;
2293 * add qgroup to all inherited groups
2296 i_qgroups = (u64 *)(inherit + 1);
2297 for (i = 0; i < inherit->num_qgroups; ++i) {
2298 ret = add_qgroup_relation_item(trans, quota_root,
2299 objectid, *i_qgroups);
2302 ret = add_qgroup_relation_item(trans, quota_root,
2303 *i_qgroups, objectid);
2311 spin_lock(&fs_info->qgroup_lock);
2313 dstgroup = add_qgroup_rb(fs_info, objectid);
2314 if (IS_ERR(dstgroup)) {
2315 ret = PTR_ERR(dstgroup);
2319 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
2320 dstgroup->lim_flags = inherit->lim.flags;
2321 dstgroup->max_rfer = inherit->lim.max_rfer;
2322 dstgroup->max_excl = inherit->lim.max_excl;
2323 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2324 dstgroup->rsv_excl = inherit->lim.rsv_excl;
2326 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
2328 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2329 btrfs_info(fs_info, "unable to update quota limit for %llu",
2330 dstgroup->qgroupid);
2336 srcgroup = find_qgroup_rb(fs_info, srcid);
2341 * We call inherit after we clone the root in order to make sure
2342 * our counts don't go crazy, so at this point the only
2343 * difference between the two roots should be the root node.
2345 dstgroup->rfer = srcgroup->rfer;
2346 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2347 dstgroup->excl = level_size;
2348 dstgroup->excl_cmpr = level_size;
2349 srcgroup->excl = level_size;
2350 srcgroup->excl_cmpr = level_size;
2352 /* inherit the limit info */
2353 dstgroup->lim_flags = srcgroup->lim_flags;
2354 dstgroup->max_rfer = srcgroup->max_rfer;
2355 dstgroup->max_excl = srcgroup->max_excl;
2356 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2357 dstgroup->rsv_excl = srcgroup->rsv_excl;
2359 qgroup_dirty(fs_info, dstgroup);
2360 qgroup_dirty(fs_info, srcgroup);
2366 i_qgroups = (u64 *)(inherit + 1);
2367 for (i = 0; i < inherit->num_qgroups; ++i) {
2368 ret = add_relation_rb(quota_root->fs_info, objectid,
2375 for (i = 0; i < inherit->num_ref_copies; ++i) {
2376 struct btrfs_qgroup *src;
2377 struct btrfs_qgroup *dst;
2379 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2380 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2387 dst->rfer = src->rfer - level_size;
2388 dst->rfer_cmpr = src->rfer_cmpr - level_size;
2391 for (i = 0; i < inherit->num_excl_copies; ++i) {
2392 struct btrfs_qgroup *src;
2393 struct btrfs_qgroup *dst;
2395 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2396 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2403 dst->excl = src->excl + level_size;
2404 dst->excl_cmpr = src->excl_cmpr + level_size;
2409 spin_unlock(&fs_info->qgroup_lock);
2411 mutex_unlock(&fs_info->qgroup_ioctl_lock);
2415 int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
2417 struct btrfs_root *quota_root;
2418 struct btrfs_qgroup *qgroup;
2419 struct btrfs_fs_info *fs_info = root->fs_info;
2420 u64 ref_root = root->root_key.objectid;
2422 struct ulist_node *unode;
2423 struct ulist_iterator uiter;
2425 if (!is_fstree(ref_root))
2431 spin_lock(&fs_info->qgroup_lock);
2432 quota_root = fs_info->quota_root;
2436 qgroup = find_qgroup_rb(fs_info, ref_root);
2441 * in a first step, we check all affected qgroups if any limits would
2444 ulist_reinit(fs_info->qgroup_ulist);
2445 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
2446 (uintptr_t)qgroup, GFP_ATOMIC);
2449 ULIST_ITER_INIT(&uiter);
2450 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2451 struct btrfs_qgroup *qg;
2452 struct btrfs_qgroup_list *glist;
2454 qg = u64_to_ptr(unode->aux);
2456 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
2457 qg->reserved + (s64)qg->rfer + num_bytes >
2463 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
2464 qg->reserved + (s64)qg->excl + num_bytes >
2470 list_for_each_entry(glist, &qg->groups, next_group) {
2471 ret = ulist_add(fs_info->qgroup_ulist,
2472 glist->group->qgroupid,
2473 (uintptr_t)glist->group, GFP_ATOMIC);
2480 * no limits exceeded, now record the reservation into all qgroups
2482 ULIST_ITER_INIT(&uiter);
2483 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2484 struct btrfs_qgroup *qg;
2486 qg = u64_to_ptr(unode->aux);
2488 qg->reserved += num_bytes;
2492 spin_unlock(&fs_info->qgroup_lock);
2496 void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
2498 struct btrfs_root *quota_root;
2499 struct btrfs_qgroup *qgroup;
2500 struct btrfs_fs_info *fs_info = root->fs_info;
2501 struct ulist_node *unode;
2502 struct ulist_iterator uiter;
2503 u64 ref_root = root->root_key.objectid;
2506 if (!is_fstree(ref_root))
2512 spin_lock(&fs_info->qgroup_lock);
2514 quota_root = fs_info->quota_root;
2518 qgroup = find_qgroup_rb(fs_info, ref_root);
2522 ulist_reinit(fs_info->qgroup_ulist);
2523 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
2524 (uintptr_t)qgroup, GFP_ATOMIC);
2527 ULIST_ITER_INIT(&uiter);
2528 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2529 struct btrfs_qgroup *qg;
2530 struct btrfs_qgroup_list *glist;
2532 qg = u64_to_ptr(unode->aux);
2534 qg->reserved -= num_bytes;
2536 list_for_each_entry(glist, &qg->groups, next_group) {
2537 ret = ulist_add(fs_info->qgroup_ulist,
2538 glist->group->qgroupid,
2539 (uintptr_t)glist->group, GFP_ATOMIC);
2546 spin_unlock(&fs_info->qgroup_lock);
2549 void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
2551 if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
2553 btrfs_err(trans->root->fs_info,
2554 "qgroups not uptodate in trans handle %p: list is%s empty, "
2556 trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
2557 (u32)(trans->delayed_ref_elem.seq >> 32),
2558 (u32)trans->delayed_ref_elem.seq);
2563 * returns < 0 on error, 0 when more leafs are to be scanned.
2564 * returns 1 when done.
2567 qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
2568 struct btrfs_trans_handle *trans, struct ulist *qgroups,
2569 struct ulist *tmp, struct extent_buffer *scratch_leaf)
2571 struct btrfs_key found;
2572 struct ulist *roots = NULL;
2573 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
2580 path->leave_spinning = 1;
2581 mutex_lock(&fs_info->qgroup_rescan_lock);
2582 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2583 &fs_info->qgroup_rescan_progress,
2586 pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
2587 fs_info->qgroup_rescan_progress.objectid,
2588 fs_info->qgroup_rescan_progress.type,
2589 fs_info->qgroup_rescan_progress.offset, ret);
2593 * The rescan is about to end, we will not be scanning any
2594 * further blocks. We cannot unset the RESCAN flag here, because
2595 * we want to commit the transaction if everything went well.
2596 * To make the live accounting work in this phase, we set our
2597 * scan progress pointer such that every real extent objectid
2600 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2601 btrfs_release_path(path);
2602 mutex_unlock(&fs_info->qgroup_rescan_lock);
2606 btrfs_item_key_to_cpu(path->nodes[0], &found,
2607 btrfs_header_nritems(path->nodes[0]) - 1);
2608 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2610 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2611 memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
2612 slot = path->slots[0];
2613 btrfs_release_path(path);
2614 mutex_unlock(&fs_info->qgroup_rescan_lock);
2616 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2617 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
2618 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2619 found.type != BTRFS_METADATA_ITEM_KEY)
2621 if (found.type == BTRFS_METADATA_ITEM_KEY)
2622 num_bytes = fs_info->extent_root->nodesize;
2624 num_bytes = found.offset;
2626 ulist_reinit(qgroups);
2627 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
2631 spin_lock(&fs_info->qgroup_lock);
2632 seq = fs_info->qgroup_seq;
2633 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
2636 ret = qgroup_calc_old_refcnt(fs_info, 0, tmp, roots, qgroups,
2637 seq, &new_roots, 1);
2639 spin_unlock(&fs_info->qgroup_lock);
2644 ret = qgroup_adjust_counters(fs_info, 0, num_bytes, qgroups,
2645 seq, 0, new_roots, 1);
2647 spin_unlock(&fs_info->qgroup_lock);
2651 spin_unlock(&fs_info->qgroup_lock);
2655 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2660 static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
2662 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2663 qgroup_rescan_work);
2664 struct btrfs_path *path;
2665 struct btrfs_trans_handle *trans = NULL;
2666 struct ulist *tmp = NULL, *qgroups = NULL;
2667 struct extent_buffer *scratch_leaf = NULL;
2671 path = btrfs_alloc_path();
2674 qgroups = ulist_alloc(GFP_NOFS);
2677 tmp = ulist_alloc(GFP_NOFS);
2680 scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
2686 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2687 if (IS_ERR(trans)) {
2688 err = PTR_ERR(trans);
2691 if (!fs_info->quota_enabled) {
2694 err = qgroup_rescan_leaf(fs_info, path, trans,
2695 qgroups, tmp, scratch_leaf);
2698 btrfs_commit_transaction(trans, fs_info->fs_root);
2700 btrfs_end_transaction(trans, fs_info->fs_root);
2704 kfree(scratch_leaf);
2705 ulist_free(qgroups);
2707 btrfs_free_path(path);
2709 mutex_lock(&fs_info->qgroup_rescan_lock);
2710 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2713 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2714 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2715 } else if (err < 0) {
2716 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2718 mutex_unlock(&fs_info->qgroup_rescan_lock);
2721 * only update status, since the previous part has alreay updated the
2724 trans = btrfs_start_transaction(fs_info->quota_root, 1);
2725 if (IS_ERR(trans)) {
2726 err = PTR_ERR(trans);
2728 "fail to start transaction for status update: %d\n",
2732 ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
2735 btrfs_err(fs_info, "fail to update qgroup status: %d\n",
2737 btrfs_abort_transaction(trans, fs_info->quota_root, err);
2740 btrfs_end_transaction(trans, fs_info->quota_root);
2743 btrfs_info(fs_info, "qgroup scan completed%s",
2744 err > 0 ? " (inconsistency flag cleared)" : "");
2746 btrfs_err(fs_info, "qgroup scan failed with %d", err);
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;
2791 spin_unlock(&fs_info->qgroup_lock);
2792 mutex_unlock(&fs_info->qgroup_rescan_lock);
2794 init_completion(&fs_info->qgroup_rescan_completion);
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, fs_info->fs_root);
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)
2874 mutex_lock(&fs_info->qgroup_rescan_lock);
2875 spin_lock(&fs_info->qgroup_lock);
2876 running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2877 spin_unlock(&fs_info->qgroup_lock);
2878 mutex_unlock(&fs_info->qgroup_rescan_lock);
2881 ret = wait_for_completion_interruptible(
2882 &fs_info->qgroup_rescan_completion);
2888 * this is only called from open_ctree where we're still single threaded, thus
2889 * locking is omitted here.
2892 btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2894 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2895 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2896 &fs_info->qgroup_rescan_work);