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"
37 * - subvol delete -> delete when ref goes to 0? delete limits also?
41 * - copy also limits on subvol creation
43 * - caches fuer ulists
44 * - performance benchmarks
45 * - check all ioctl parameters
49 * one struct for each qgroup, organized in fs_info->qgroup_tree.
57 u64 rfer; /* referenced */
58 u64 rfer_cmpr; /* referenced compressed */
59 u64 excl; /* exclusive */
60 u64 excl_cmpr; /* exclusive compressed */
65 u64 lim_flags; /* which limits are set */
72 * reservation tracking
79 struct list_head groups; /* groups this group is member of */
80 struct list_head members; /* groups that are members of this group */
81 struct list_head dirty; /* dirty groups */
82 struct rb_node node; /* tree of qgroups */
85 * temp variables for accounting operations
92 * glue structure to represent the relations between qgroups.
94 struct btrfs_qgroup_list {
95 struct list_head next_group;
96 struct list_head next_member;
97 struct btrfs_qgroup *group;
98 struct btrfs_qgroup *member;
102 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
104 static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
106 /* must be called with qgroup_ioctl_lock held */
107 static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
110 struct rb_node *n = fs_info->qgroup_tree.rb_node;
111 struct btrfs_qgroup *qgroup;
114 qgroup = rb_entry(n, struct btrfs_qgroup, node);
115 if (qgroup->qgroupid < qgroupid)
117 else if (qgroup->qgroupid > qgroupid)
125 /* must be called with qgroup_lock held */
126 static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
129 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
130 struct rb_node *parent = NULL;
131 struct btrfs_qgroup *qgroup;
135 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
137 if (qgroup->qgroupid < qgroupid)
139 else if (qgroup->qgroupid > qgroupid)
145 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
147 return ERR_PTR(-ENOMEM);
149 qgroup->qgroupid = qgroupid;
150 INIT_LIST_HEAD(&qgroup->groups);
151 INIT_LIST_HEAD(&qgroup->members);
152 INIT_LIST_HEAD(&qgroup->dirty);
154 rb_link_node(&qgroup->node, parent, p);
155 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
160 /* must be called with qgroup_lock held */
161 static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
163 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
164 struct btrfs_qgroup_list *list;
169 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
170 list_del(&qgroup->dirty);
172 while (!list_empty(&qgroup->groups)) {
173 list = list_first_entry(&qgroup->groups,
174 struct btrfs_qgroup_list, next_group);
175 list_del(&list->next_group);
176 list_del(&list->next_member);
180 while (!list_empty(&qgroup->members)) {
181 list = list_first_entry(&qgroup->members,
182 struct btrfs_qgroup_list, next_member);
183 list_del(&list->next_group);
184 list_del(&list->next_member);
192 /* must be called with qgroup_lock held */
193 static int add_relation_rb(struct btrfs_fs_info *fs_info,
194 u64 memberid, u64 parentid)
196 struct btrfs_qgroup *member;
197 struct btrfs_qgroup *parent;
198 struct btrfs_qgroup_list *list;
200 member = find_qgroup_rb(fs_info, memberid);
201 parent = find_qgroup_rb(fs_info, parentid);
202 if (!member || !parent)
205 list = kzalloc(sizeof(*list), GFP_ATOMIC);
209 list->group = parent;
210 list->member = member;
211 list_add_tail(&list->next_group, &member->groups);
212 list_add_tail(&list->next_member, &parent->members);
217 /* must be called with qgroup_lock held */
218 static int del_relation_rb(struct btrfs_fs_info *fs_info,
219 u64 memberid, u64 parentid)
221 struct btrfs_qgroup *member;
222 struct btrfs_qgroup *parent;
223 struct btrfs_qgroup_list *list;
225 member = find_qgroup_rb(fs_info, memberid);
226 parent = find_qgroup_rb(fs_info, parentid);
227 if (!member || !parent)
230 list_for_each_entry(list, &member->groups, next_group) {
231 if (list->group == parent) {
232 list_del(&list->next_group);
233 list_del(&list->next_member);
242 * The full config is read in one go, only called from open_ctree()
243 * It doesn't use any locking, as at this point we're still single-threaded
245 int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
247 struct btrfs_key key;
248 struct btrfs_key found_key;
249 struct btrfs_root *quota_root = fs_info->quota_root;
250 struct btrfs_path *path = NULL;
251 struct extent_buffer *l;
255 u64 rescan_progress = 0;
257 if (!fs_info->quota_enabled)
260 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
261 if (!fs_info->qgroup_ulist) {
266 path = btrfs_alloc_path();
272 /* default this to quota off, in case no status key is found */
273 fs_info->qgroup_flags = 0;
276 * pass 1: read status, all qgroup infos and limits
281 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
286 struct btrfs_qgroup *qgroup;
288 slot = path->slots[0];
290 btrfs_item_key_to_cpu(l, &found_key, slot);
292 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
293 struct btrfs_qgroup_status_item *ptr;
295 ptr = btrfs_item_ptr(l, slot,
296 struct btrfs_qgroup_status_item);
298 if (btrfs_qgroup_status_version(l, ptr) !=
299 BTRFS_QGROUP_STATUS_VERSION) {
301 "btrfs: old qgroup version, quota disabled\n");
304 if (btrfs_qgroup_status_generation(l, ptr) !=
305 fs_info->generation) {
306 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
308 "btrfs: qgroup generation mismatch, "
309 "marked as inconsistent\n");
311 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
313 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
317 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
318 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
321 qgroup = find_qgroup_rb(fs_info, found_key.offset);
322 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
323 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
324 printk(KERN_ERR "btrfs: inconsitent qgroup config\n");
325 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
328 qgroup = add_qgroup_rb(fs_info, found_key.offset);
329 if (IS_ERR(qgroup)) {
330 ret = PTR_ERR(qgroup);
334 switch (found_key.type) {
335 case BTRFS_QGROUP_INFO_KEY: {
336 struct btrfs_qgroup_info_item *ptr;
338 ptr = btrfs_item_ptr(l, slot,
339 struct btrfs_qgroup_info_item);
340 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
341 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
342 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
343 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
344 /* generation currently unused */
347 case BTRFS_QGROUP_LIMIT_KEY: {
348 struct btrfs_qgroup_limit_item *ptr;
350 ptr = btrfs_item_ptr(l, slot,
351 struct btrfs_qgroup_limit_item);
352 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
353 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
354 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
355 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
356 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
361 ret = btrfs_next_item(quota_root, path);
367 btrfs_release_path(path);
370 * pass 2: read all qgroup relations
373 key.type = BTRFS_QGROUP_RELATION_KEY;
375 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
379 slot = path->slots[0];
381 btrfs_item_key_to_cpu(l, &found_key, slot);
383 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
386 if (found_key.objectid > found_key.offset) {
387 /* parent <- member, not needed to build config */
388 /* FIXME should we omit the key completely? */
392 ret = add_relation_rb(fs_info, found_key.objectid,
394 if (ret == -ENOENT) {
396 "btrfs: orphan qgroup relation 0x%llx->0x%llx\n",
397 (unsigned long long)found_key.objectid,
398 (unsigned long long)found_key.offset);
399 ret = 0; /* ignore the error */
404 ret = btrfs_next_item(quota_root, path);
411 fs_info->qgroup_flags |= flags;
412 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
413 fs_info->quota_enabled = 0;
414 fs_info->pending_quota_state = 0;
415 } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
417 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
419 btrfs_free_path(path);
422 ulist_free(fs_info->qgroup_ulist);
423 fs_info->qgroup_ulist = NULL;
424 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
427 return ret < 0 ? ret : 0;
431 * This is only called from close_ctree() or open_ctree(), both in single-
432 * treaded paths. Clean up the in-memory structures. No locking needed.
434 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
437 struct btrfs_qgroup *qgroup;
438 struct btrfs_qgroup_list *list;
440 while ((n = rb_first(&fs_info->qgroup_tree))) {
441 qgroup = rb_entry(n, struct btrfs_qgroup, node);
442 rb_erase(n, &fs_info->qgroup_tree);
444 while (!list_empty(&qgroup->groups)) {
445 list = list_first_entry(&qgroup->groups,
446 struct btrfs_qgroup_list,
448 list_del(&list->next_group);
449 list_del(&list->next_member);
453 while (!list_empty(&qgroup->members)) {
454 list = list_first_entry(&qgroup->members,
455 struct btrfs_qgroup_list,
457 list_del(&list->next_group);
458 list_del(&list->next_member);
463 ulist_free(fs_info->qgroup_ulist);
466 static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
467 struct btrfs_root *quota_root,
471 struct btrfs_path *path;
472 struct btrfs_key key;
474 path = btrfs_alloc_path();
479 key.type = BTRFS_QGROUP_RELATION_KEY;
482 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
484 btrfs_mark_buffer_dirty(path->nodes[0]);
486 btrfs_free_path(path);
490 static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
491 struct btrfs_root *quota_root,
495 struct btrfs_path *path;
496 struct btrfs_key key;
498 path = btrfs_alloc_path();
503 key.type = BTRFS_QGROUP_RELATION_KEY;
506 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
515 ret = btrfs_del_item(trans, quota_root, path);
517 btrfs_free_path(path);
521 static int add_qgroup_item(struct btrfs_trans_handle *trans,
522 struct btrfs_root *quota_root, u64 qgroupid)
525 struct btrfs_path *path;
526 struct btrfs_qgroup_info_item *qgroup_info;
527 struct btrfs_qgroup_limit_item *qgroup_limit;
528 struct extent_buffer *leaf;
529 struct btrfs_key key;
531 path = btrfs_alloc_path();
536 key.type = BTRFS_QGROUP_INFO_KEY;
537 key.offset = qgroupid;
539 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
540 sizeof(*qgroup_info));
544 leaf = path->nodes[0];
545 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
546 struct btrfs_qgroup_info_item);
547 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
548 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
549 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
550 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
551 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
553 btrfs_mark_buffer_dirty(leaf);
555 btrfs_release_path(path);
557 key.type = BTRFS_QGROUP_LIMIT_KEY;
558 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
559 sizeof(*qgroup_limit));
563 leaf = path->nodes[0];
564 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
565 struct btrfs_qgroup_limit_item);
566 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
567 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
568 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
569 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
570 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
572 btrfs_mark_buffer_dirty(leaf);
576 btrfs_free_path(path);
580 static int del_qgroup_item(struct btrfs_trans_handle *trans,
581 struct btrfs_root *quota_root, u64 qgroupid)
584 struct btrfs_path *path;
585 struct btrfs_key key;
587 path = btrfs_alloc_path();
592 key.type = BTRFS_QGROUP_INFO_KEY;
593 key.offset = qgroupid;
594 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
603 ret = btrfs_del_item(trans, quota_root, path);
607 btrfs_release_path(path);
609 key.type = BTRFS_QGROUP_LIMIT_KEY;
610 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
619 ret = btrfs_del_item(trans, quota_root, path);
622 btrfs_free_path(path);
626 static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
627 struct btrfs_root *root, u64 qgroupid,
628 u64 flags, u64 max_rfer, u64 max_excl,
629 u64 rsv_rfer, u64 rsv_excl)
631 struct btrfs_path *path;
632 struct btrfs_key key;
633 struct extent_buffer *l;
634 struct btrfs_qgroup_limit_item *qgroup_limit;
639 key.type = BTRFS_QGROUP_LIMIT_KEY;
640 key.offset = qgroupid;
642 path = btrfs_alloc_path();
646 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
654 slot = path->slots[0];
655 qgroup_limit = btrfs_item_ptr(l, path->slots[0],
656 struct btrfs_qgroup_limit_item);
657 btrfs_set_qgroup_limit_flags(l, qgroup_limit, flags);
658 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, max_rfer);
659 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, max_excl);
660 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, rsv_rfer);
661 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, rsv_excl);
663 btrfs_mark_buffer_dirty(l);
666 btrfs_free_path(path);
670 static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
671 struct btrfs_root *root,
672 struct btrfs_qgroup *qgroup)
674 struct btrfs_path *path;
675 struct btrfs_key key;
676 struct extent_buffer *l;
677 struct btrfs_qgroup_info_item *qgroup_info;
682 key.type = BTRFS_QGROUP_INFO_KEY;
683 key.offset = qgroup->qgroupid;
685 path = btrfs_alloc_path();
689 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
697 slot = path->slots[0];
698 qgroup_info = btrfs_item_ptr(l, path->slots[0],
699 struct btrfs_qgroup_info_item);
700 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
701 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
702 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
703 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
704 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
706 btrfs_mark_buffer_dirty(l);
709 btrfs_free_path(path);
713 static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
714 struct btrfs_fs_info *fs_info,
715 struct btrfs_root *root)
717 struct btrfs_path *path;
718 struct btrfs_key key;
719 struct extent_buffer *l;
720 struct btrfs_qgroup_status_item *ptr;
725 key.type = BTRFS_QGROUP_STATUS_KEY;
728 path = btrfs_alloc_path();
732 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
740 slot = path->slots[0];
741 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
742 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
743 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
744 btrfs_set_qgroup_status_rescan(l, ptr,
745 fs_info->qgroup_rescan_progress.objectid);
747 btrfs_mark_buffer_dirty(l);
750 btrfs_free_path(path);
755 * called with qgroup_lock held
757 static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
758 struct btrfs_root *root)
760 struct btrfs_path *path;
761 struct btrfs_key key;
762 struct extent_buffer *leaf = NULL;
766 path = btrfs_alloc_path();
770 path->leave_spinning = 1;
777 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
780 leaf = path->nodes[0];
781 nr = btrfs_header_nritems(leaf);
785 * delete the leaf one by one
786 * since the whole tree is going
790 ret = btrfs_del_items(trans, root, path, 0, nr);
794 btrfs_release_path(path);
798 root->fs_info->pending_quota_state = 0;
799 btrfs_free_path(path);
803 int btrfs_quota_enable(struct btrfs_trans_handle *trans,
804 struct btrfs_fs_info *fs_info)
806 struct btrfs_root *quota_root;
807 struct btrfs_root *tree_root = fs_info->tree_root;
808 struct btrfs_path *path = NULL;
809 struct btrfs_qgroup_status_item *ptr;
810 struct extent_buffer *leaf;
811 struct btrfs_key key;
812 struct btrfs_key found_key;
813 struct btrfs_qgroup *qgroup = NULL;
817 mutex_lock(&fs_info->qgroup_ioctl_lock);
818 if (fs_info->quota_root) {
819 fs_info->pending_quota_state = 1;
823 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
824 if (!fs_info->qgroup_ulist) {
830 * initially create the quota tree
832 quota_root = btrfs_create_tree(trans, fs_info,
833 BTRFS_QUOTA_TREE_OBJECTID);
834 if (IS_ERR(quota_root)) {
835 ret = PTR_ERR(quota_root);
839 path = btrfs_alloc_path();
846 key.type = BTRFS_QGROUP_STATUS_KEY;
849 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
854 leaf = path->nodes[0];
855 ptr = btrfs_item_ptr(leaf, path->slots[0],
856 struct btrfs_qgroup_status_item);
857 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
858 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
859 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
860 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
861 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
862 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
864 btrfs_mark_buffer_dirty(leaf);
867 key.type = BTRFS_ROOT_REF_KEY;
870 btrfs_release_path(path);
871 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
879 slot = path->slots[0];
880 leaf = path->nodes[0];
881 btrfs_item_key_to_cpu(leaf, &found_key, slot);
883 if (found_key.type == BTRFS_ROOT_REF_KEY) {
884 ret = add_qgroup_item(trans, quota_root,
889 qgroup = add_qgroup_rb(fs_info, found_key.offset);
890 if (IS_ERR(qgroup)) {
891 ret = PTR_ERR(qgroup);
895 ret = btrfs_next_item(tree_root, path);
903 btrfs_release_path(path);
904 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
908 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
909 if (IS_ERR(qgroup)) {
910 ret = PTR_ERR(qgroup);
913 spin_lock(&fs_info->qgroup_lock);
914 fs_info->quota_root = quota_root;
915 fs_info->pending_quota_state = 1;
916 spin_unlock(&fs_info->qgroup_lock);
918 btrfs_free_path(path);
921 free_extent_buffer(quota_root->node);
922 free_extent_buffer(quota_root->commit_root);
927 ulist_free(fs_info->qgroup_ulist);
928 fs_info->qgroup_ulist = NULL;
930 mutex_unlock(&fs_info->qgroup_ioctl_lock);
934 int btrfs_quota_disable(struct btrfs_trans_handle *trans,
935 struct btrfs_fs_info *fs_info)
937 struct btrfs_root *tree_root = fs_info->tree_root;
938 struct btrfs_root *quota_root;
941 mutex_lock(&fs_info->qgroup_ioctl_lock);
942 if (!fs_info->quota_root)
944 spin_lock(&fs_info->qgroup_lock);
945 fs_info->quota_enabled = 0;
946 fs_info->pending_quota_state = 0;
947 quota_root = fs_info->quota_root;
948 fs_info->quota_root = NULL;
949 btrfs_free_qgroup_config(fs_info);
950 spin_unlock(&fs_info->qgroup_lock);
957 ret = btrfs_clean_quota_tree(trans, quota_root);
961 ret = btrfs_del_root(trans, tree_root, "a_root->root_key);
965 list_del("a_root->dirty_list);
967 btrfs_tree_lock(quota_root->node);
968 clean_tree_block(trans, tree_root, quota_root->node);
969 btrfs_tree_unlock(quota_root->node);
970 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
972 free_extent_buffer(quota_root->node);
973 free_extent_buffer(quota_root->commit_root);
976 mutex_unlock(&fs_info->qgroup_ioctl_lock);
980 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
981 struct btrfs_qgroup *qgroup)
983 if (list_empty(&qgroup->dirty))
984 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
987 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
988 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
990 struct btrfs_root *quota_root;
991 struct btrfs_qgroup *parent;
992 struct btrfs_qgroup *member;
993 struct btrfs_qgroup_list *list;
996 mutex_lock(&fs_info->qgroup_ioctl_lock);
997 quota_root = fs_info->quota_root;
1002 member = find_qgroup_rb(fs_info, src);
1003 parent = find_qgroup_rb(fs_info, dst);
1004 if (!member || !parent) {
1009 /* check if such qgroup relation exist firstly */
1010 list_for_each_entry(list, &member->groups, next_group) {
1011 if (list->group == parent) {
1017 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1021 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1023 del_qgroup_relation_item(trans, quota_root, src, dst);
1027 spin_lock(&fs_info->qgroup_lock);
1028 ret = add_relation_rb(quota_root->fs_info, src, dst);
1029 spin_unlock(&fs_info->qgroup_lock);
1031 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1035 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1036 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1038 struct btrfs_root *quota_root;
1039 struct btrfs_qgroup *parent;
1040 struct btrfs_qgroup *member;
1041 struct btrfs_qgroup_list *list;
1045 mutex_lock(&fs_info->qgroup_ioctl_lock);
1046 quota_root = fs_info->quota_root;
1052 member = find_qgroup_rb(fs_info, src);
1053 parent = find_qgroup_rb(fs_info, dst);
1054 if (!member || !parent) {
1059 /* check if such qgroup relation exist firstly */
1060 list_for_each_entry(list, &member->groups, next_group) {
1061 if (list->group == parent)
1067 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1068 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1072 spin_lock(&fs_info->qgroup_lock);
1073 del_relation_rb(fs_info, src, dst);
1074 spin_unlock(&fs_info->qgroup_lock);
1076 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1080 int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
1081 struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
1083 struct btrfs_root *quota_root;
1084 struct btrfs_qgroup *qgroup;
1087 mutex_lock(&fs_info->qgroup_ioctl_lock);
1088 quota_root = fs_info->quota_root;
1093 qgroup = find_qgroup_rb(fs_info, qgroupid);
1099 ret = add_qgroup_item(trans, quota_root, qgroupid);
1103 spin_lock(&fs_info->qgroup_lock);
1104 qgroup = add_qgroup_rb(fs_info, qgroupid);
1105 spin_unlock(&fs_info->qgroup_lock);
1108 ret = PTR_ERR(qgroup);
1110 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1114 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1115 struct btrfs_fs_info *fs_info, u64 qgroupid)
1117 struct btrfs_root *quota_root;
1118 struct btrfs_qgroup *qgroup;
1121 mutex_lock(&fs_info->qgroup_ioctl_lock);
1122 quota_root = fs_info->quota_root;
1128 qgroup = find_qgroup_rb(fs_info, qgroupid);
1133 /* check if there are no relations to this qgroup */
1134 if (!list_empty(&qgroup->groups) ||
1135 !list_empty(&qgroup->members)) {
1140 ret = del_qgroup_item(trans, quota_root, qgroupid);
1142 spin_lock(&fs_info->qgroup_lock);
1143 del_qgroup_rb(quota_root->fs_info, qgroupid);
1144 spin_unlock(&fs_info->qgroup_lock);
1146 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1150 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1151 struct btrfs_fs_info *fs_info, u64 qgroupid,
1152 struct btrfs_qgroup_limit *limit)
1154 struct btrfs_root *quota_root;
1155 struct btrfs_qgroup *qgroup;
1158 mutex_lock(&fs_info->qgroup_ioctl_lock);
1159 quota_root = fs_info->quota_root;
1165 qgroup = find_qgroup_rb(fs_info, qgroupid);
1170 ret = update_qgroup_limit_item(trans, quota_root, qgroupid,
1171 limit->flags, limit->max_rfer,
1172 limit->max_excl, limit->rsv_rfer,
1175 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1176 printk(KERN_INFO "unable to update quota limit for %llu\n",
1177 (unsigned long long)qgroupid);
1180 spin_lock(&fs_info->qgroup_lock);
1181 qgroup->lim_flags = limit->flags;
1182 qgroup->max_rfer = limit->max_rfer;
1183 qgroup->max_excl = limit->max_excl;
1184 qgroup->rsv_rfer = limit->rsv_rfer;
1185 qgroup->rsv_excl = limit->rsv_excl;
1186 spin_unlock(&fs_info->qgroup_lock);
1188 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1193 * btrfs_qgroup_record_ref is called when the ref is added or deleted. it puts
1194 * the modification into a list that's later used by btrfs_end_transaction to
1195 * pass the recorded modifications on to btrfs_qgroup_account_ref.
1197 int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
1198 struct btrfs_delayed_ref_node *node,
1199 struct btrfs_delayed_extent_op *extent_op)
1201 struct qgroup_update *u;
1203 BUG_ON(!trans->delayed_ref_elem.seq);
1204 u = kmalloc(sizeof(*u), GFP_NOFS);
1209 u->extent_op = extent_op;
1210 list_add_tail(&u->list, &trans->qgroup_ref_list);
1215 static int qgroup_account_ref_step1(struct btrfs_fs_info *fs_info,
1216 struct ulist *roots, struct ulist *tmp,
1219 struct ulist_node *unode;
1220 struct ulist_iterator uiter;
1221 struct ulist_node *tmp_unode;
1222 struct ulist_iterator tmp_uiter;
1223 struct btrfs_qgroup *qg;
1226 ULIST_ITER_INIT(&uiter);
1227 while ((unode = ulist_next(roots, &uiter))) {
1228 qg = find_qgroup_rb(fs_info, unode->val);
1233 /* XXX id not needed */
1234 ret = ulist_add(tmp, qg->qgroupid,
1235 (u64)(uintptr_t)qg, GFP_ATOMIC);
1238 ULIST_ITER_INIT(&tmp_uiter);
1239 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1240 struct btrfs_qgroup_list *glist;
1242 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
1243 if (qg->refcnt < seq)
1244 qg->refcnt = seq + 1;
1248 list_for_each_entry(glist, &qg->groups, next_group) {
1249 ret = ulist_add(tmp, glist->group->qgroupid,
1250 (u64)(uintptr_t)glist->group,
1261 static int qgroup_account_ref_step2(struct btrfs_fs_info *fs_info,
1262 struct ulist *roots, struct ulist *tmp,
1263 u64 seq, int sgn, u64 num_bytes,
1264 struct btrfs_qgroup *qgroup)
1266 struct ulist_node *unode;
1267 struct ulist_iterator uiter;
1268 struct btrfs_qgroup *qg;
1269 struct btrfs_qgroup_list *glist;
1273 ret = ulist_add(tmp, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
1277 ULIST_ITER_INIT(&uiter);
1278 while ((unode = ulist_next(tmp, &uiter))) {
1279 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1280 if (qg->refcnt < seq) {
1281 /* not visited by step 1 */
1282 qg->rfer += sgn * num_bytes;
1283 qg->rfer_cmpr += sgn * num_bytes;
1284 if (roots->nnodes == 0) {
1285 qg->excl += sgn * num_bytes;
1286 qg->excl_cmpr += sgn * num_bytes;
1288 qgroup_dirty(fs_info, qg);
1290 WARN_ON(qg->tag >= seq);
1293 list_for_each_entry(glist, &qg->groups, next_group) {
1294 ret = ulist_add(tmp, glist->group->qgroupid,
1295 (uintptr_t)glist->group, GFP_ATOMIC);
1304 static int qgroup_account_ref_step3(struct btrfs_fs_info *fs_info,
1305 struct ulist *roots, struct ulist *tmp,
1306 u64 seq, int sgn, u64 num_bytes)
1308 struct ulist_node *unode;
1309 struct ulist_iterator uiter;
1310 struct btrfs_qgroup *qg;
1311 struct ulist_node *tmp_unode;
1312 struct ulist_iterator tmp_uiter;
1315 ULIST_ITER_INIT(&uiter);
1316 while ((unode = ulist_next(roots, &uiter))) {
1317 qg = find_qgroup_rb(fs_info, unode->val);
1322 ret = ulist_add(tmp, qg->qgroupid, (uintptr_t)qg, GFP_ATOMIC);
1326 ULIST_ITER_INIT(&tmp_uiter);
1327 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1328 struct btrfs_qgroup_list *glist;
1330 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
1334 if (qg->refcnt - seq == roots->nnodes) {
1335 qg->excl -= sgn * num_bytes;
1336 qg->excl_cmpr -= sgn * num_bytes;
1337 qgroup_dirty(fs_info, qg);
1340 list_for_each_entry(glist, &qg->groups, next_group) {
1341 ret = ulist_add(tmp, glist->group->qgroupid,
1342 (uintptr_t)glist->group,
1354 * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
1355 * from the fs. First, all roots referencing the extent are searched, and
1356 * then the space is accounted accordingly to the different roots. The
1357 * accounting algorithm works in 3 steps documented inline.
1359 int btrfs_qgroup_account_ref(struct btrfs_trans_handle *trans,
1360 struct btrfs_fs_info *fs_info,
1361 struct btrfs_delayed_ref_node *node,
1362 struct btrfs_delayed_extent_op *extent_op)
1364 struct btrfs_key ins;
1365 struct btrfs_root *quota_root;
1367 struct btrfs_qgroup *qgroup;
1368 struct ulist *roots = NULL;
1373 if (!fs_info->quota_enabled)
1376 BUG_ON(!fs_info->quota_root);
1378 ins.objectid = node->bytenr;
1379 ins.offset = node->num_bytes;
1380 ins.type = BTRFS_EXTENT_ITEM_KEY;
1382 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1383 node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
1384 struct btrfs_delayed_tree_ref *ref;
1385 ref = btrfs_delayed_node_to_tree_ref(node);
1386 ref_root = ref->root;
1387 } else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1388 node->type == BTRFS_SHARED_DATA_REF_KEY) {
1389 struct btrfs_delayed_data_ref *ref;
1390 ref = btrfs_delayed_node_to_data_ref(node);
1391 ref_root = ref->root;
1396 if (!is_fstree(ref_root)) {
1398 * non-fs-trees are not being accounted
1403 switch (node->action) {
1404 case BTRFS_ADD_DELAYED_REF:
1405 case BTRFS_ADD_DELAYED_EXTENT:
1407 seq = btrfs_tree_mod_seq_prev(node->seq);
1409 case BTRFS_DROP_DELAYED_REF:
1413 case BTRFS_UPDATE_DELAYED_HEAD:
1419 mutex_lock(&fs_info->qgroup_rescan_lock);
1420 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
1421 if (fs_info->qgroup_rescan_progress.objectid <= node->bytenr) {
1422 mutex_unlock(&fs_info->qgroup_rescan_lock);
1426 mutex_unlock(&fs_info->qgroup_rescan_lock);
1429 * the delayed ref sequence number we pass depends on the direction of
1430 * the operation. for add operations, we pass
1431 * tree_mod_log_prev_seq(node->seq) to skip
1432 * the delayed ref's current sequence number, because we need the state
1433 * of the tree before the add operation. for delete operations, we pass
1434 * (node->seq) to include the delayed ref's current sequence number,
1435 * because we need the state of the tree after the delete operation.
1437 ret = btrfs_find_all_roots(trans, fs_info, node->bytenr, seq, &roots);
1441 spin_lock(&fs_info->qgroup_lock);
1443 quota_root = fs_info->quota_root;
1447 qgroup = find_qgroup_rb(fs_info, ref_root);
1452 * step 1: for each old ref, visit all nodes once and inc refcnt
1454 ulist_reinit(fs_info->qgroup_ulist);
1455 seq = fs_info->qgroup_seq;
1456 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
1458 ret = qgroup_account_ref_step1(fs_info, roots, fs_info->qgroup_ulist,
1464 * step 2: walk from the new root
1466 ret = qgroup_account_ref_step2(fs_info, roots, fs_info->qgroup_ulist,
1467 seq, sgn, node->num_bytes, qgroup);
1472 * step 3: walk again from old refs
1474 ret = qgroup_account_ref_step3(fs_info, roots, fs_info->qgroup_ulist,
1475 seq, sgn, node->num_bytes);
1480 spin_unlock(&fs_info->qgroup_lock);
1487 * called from commit_transaction. Writes all changed qgroups to disk.
1489 int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
1490 struct btrfs_fs_info *fs_info)
1492 struct btrfs_root *quota_root = fs_info->quota_root;
1494 int start_rescan_worker = 0;
1499 if (!fs_info->quota_enabled && fs_info->pending_quota_state)
1500 start_rescan_worker = 1;
1502 fs_info->quota_enabled = fs_info->pending_quota_state;
1504 spin_lock(&fs_info->qgroup_lock);
1505 while (!list_empty(&fs_info->dirty_qgroups)) {
1506 struct btrfs_qgroup *qgroup;
1507 qgroup = list_first_entry(&fs_info->dirty_qgroups,
1508 struct btrfs_qgroup, dirty);
1509 list_del_init(&qgroup->dirty);
1510 spin_unlock(&fs_info->qgroup_lock);
1511 ret = update_qgroup_info_item(trans, quota_root, qgroup);
1513 fs_info->qgroup_flags |=
1514 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1515 spin_lock(&fs_info->qgroup_lock);
1517 if (fs_info->quota_enabled)
1518 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
1520 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1521 spin_unlock(&fs_info->qgroup_lock);
1523 ret = update_qgroup_status_item(trans, fs_info, quota_root);
1525 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1527 if (!ret && start_rescan_worker) {
1528 ret = qgroup_rescan_init(fs_info, 0, 1);
1530 qgroup_rescan_zero_tracking(fs_info);
1531 btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
1532 &fs_info->qgroup_rescan_work);
1543 * copy the acounting information between qgroups. This is necessary when a
1544 * snapshot or a subvolume is created
1546 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
1547 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
1548 struct btrfs_qgroup_inherit *inherit)
1553 struct btrfs_root *quota_root = fs_info->quota_root;
1554 struct btrfs_qgroup *srcgroup;
1555 struct btrfs_qgroup *dstgroup;
1559 mutex_lock(&fs_info->qgroup_ioctl_lock);
1560 if (!fs_info->quota_enabled)
1569 i_qgroups = (u64 *)(inherit + 1);
1570 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
1571 2 * inherit->num_excl_copies;
1572 for (i = 0; i < nums; ++i) {
1573 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
1583 * create a tracking group for the subvol itself
1585 ret = add_qgroup_item(trans, quota_root, objectid);
1589 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
1590 ret = update_qgroup_limit_item(trans, quota_root, objectid,
1592 inherit->lim.max_rfer,
1593 inherit->lim.max_excl,
1594 inherit->lim.rsv_rfer,
1595 inherit->lim.rsv_excl);
1601 struct btrfs_root *srcroot;
1602 struct btrfs_key srckey;
1605 srckey.objectid = srcid;
1606 srckey.type = BTRFS_ROOT_ITEM_KEY;
1607 srckey.offset = (u64)-1;
1608 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
1609 if (IS_ERR(srcroot)) {
1610 ret = PTR_ERR(srcroot);
1615 srcroot_level = btrfs_header_level(srcroot->node);
1616 level_size = btrfs_level_size(srcroot, srcroot_level);
1621 * add qgroup to all inherited groups
1624 i_qgroups = (u64 *)(inherit + 1);
1625 for (i = 0; i < inherit->num_qgroups; ++i) {
1626 ret = add_qgroup_relation_item(trans, quota_root,
1627 objectid, *i_qgroups);
1630 ret = add_qgroup_relation_item(trans, quota_root,
1631 *i_qgroups, objectid);
1639 spin_lock(&fs_info->qgroup_lock);
1641 dstgroup = add_qgroup_rb(fs_info, objectid);
1642 if (IS_ERR(dstgroup)) {
1643 ret = PTR_ERR(dstgroup);
1648 srcgroup = find_qgroup_rb(fs_info, srcid);
1651 dstgroup->rfer = srcgroup->rfer - level_size;
1652 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr - level_size;
1653 srcgroup->excl = level_size;
1654 srcgroup->excl_cmpr = level_size;
1655 qgroup_dirty(fs_info, dstgroup);
1656 qgroup_dirty(fs_info, srcgroup);
1662 i_qgroups = (u64 *)(inherit + 1);
1663 for (i = 0; i < inherit->num_qgroups; ++i) {
1664 ret = add_relation_rb(quota_root->fs_info, objectid,
1671 for (i = 0; i < inherit->num_ref_copies; ++i) {
1672 struct btrfs_qgroup *src;
1673 struct btrfs_qgroup *dst;
1675 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1676 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1683 dst->rfer = src->rfer - level_size;
1684 dst->rfer_cmpr = src->rfer_cmpr - level_size;
1687 for (i = 0; i < inherit->num_excl_copies; ++i) {
1688 struct btrfs_qgroup *src;
1689 struct btrfs_qgroup *dst;
1691 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1692 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1699 dst->excl = src->excl + level_size;
1700 dst->excl_cmpr = src->excl_cmpr + level_size;
1705 spin_unlock(&fs_info->qgroup_lock);
1707 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1712 * reserve some space for a qgroup and all its parents. The reservation takes
1713 * place with start_transaction or dealloc_reserve, similar to ENOSPC
1714 * accounting. If not enough space is available, EDQUOT is returned.
1715 * We assume that the requested space is new for all qgroups.
1717 int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
1719 struct btrfs_root *quota_root;
1720 struct btrfs_qgroup *qgroup;
1721 struct btrfs_fs_info *fs_info = root->fs_info;
1722 u64 ref_root = root->root_key.objectid;
1724 struct ulist_node *unode;
1725 struct ulist_iterator uiter;
1727 if (!is_fstree(ref_root))
1733 spin_lock(&fs_info->qgroup_lock);
1734 quota_root = fs_info->quota_root;
1738 qgroup = find_qgroup_rb(fs_info, ref_root);
1743 * in a first step, we check all affected qgroups if any limits would
1746 ulist_reinit(fs_info->qgroup_ulist);
1747 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
1748 (uintptr_t)qgroup, GFP_ATOMIC);
1751 ULIST_ITER_INIT(&uiter);
1752 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
1753 struct btrfs_qgroup *qg;
1754 struct btrfs_qgroup_list *glist;
1756 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1758 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
1759 qg->reserved + (s64)qg->rfer + num_bytes >
1765 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
1766 qg->reserved + (s64)qg->excl + num_bytes >
1772 list_for_each_entry(glist, &qg->groups, next_group) {
1773 ret = ulist_add(fs_info->qgroup_ulist,
1774 glist->group->qgroupid,
1775 (uintptr_t)glist->group, GFP_ATOMIC);
1782 * no limits exceeded, now record the reservation into all qgroups
1784 ULIST_ITER_INIT(&uiter);
1785 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
1786 struct btrfs_qgroup *qg;
1788 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1790 qg->reserved += num_bytes;
1794 spin_unlock(&fs_info->qgroup_lock);
1798 void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
1800 struct btrfs_root *quota_root;
1801 struct btrfs_qgroup *qgroup;
1802 struct btrfs_fs_info *fs_info = root->fs_info;
1803 struct ulist_node *unode;
1804 struct ulist_iterator uiter;
1805 u64 ref_root = root->root_key.objectid;
1808 if (!is_fstree(ref_root))
1814 spin_lock(&fs_info->qgroup_lock);
1816 quota_root = fs_info->quota_root;
1820 qgroup = find_qgroup_rb(fs_info, ref_root);
1824 ulist_reinit(fs_info->qgroup_ulist);
1825 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
1826 (uintptr_t)qgroup, GFP_ATOMIC);
1829 ULIST_ITER_INIT(&uiter);
1830 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
1831 struct btrfs_qgroup *qg;
1832 struct btrfs_qgroup_list *glist;
1834 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1836 qg->reserved -= num_bytes;
1838 list_for_each_entry(glist, &qg->groups, next_group) {
1839 ret = ulist_add(fs_info->qgroup_ulist,
1840 glist->group->qgroupid,
1841 (uintptr_t)glist->group, GFP_ATOMIC);
1848 spin_unlock(&fs_info->qgroup_lock);
1851 void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
1853 if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
1855 pr_err("btrfs: qgroups not uptodate in trans handle %p: list is%s empty, seq is %#x.%x\n",
1856 trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
1857 (u32)(trans->delayed_ref_elem.seq >> 32),
1858 (u32)trans->delayed_ref_elem.seq);
1863 * returns < 0 on error, 0 when more leafs are to be scanned.
1864 * returns 1 when done, 2 when done and FLAG_INCONSISTENT was cleared.
1867 qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1868 struct btrfs_trans_handle *trans, struct ulist *tmp,
1869 struct extent_buffer *scratch_leaf)
1871 struct btrfs_key found;
1872 struct ulist *roots = NULL;
1873 struct ulist_node *unode;
1874 struct ulist_iterator uiter;
1875 struct seq_list tree_mod_seq_elem = {};
1880 path->leave_spinning = 1;
1881 mutex_lock(&fs_info->qgroup_rescan_lock);
1882 ret = btrfs_search_slot_for_read(fs_info->extent_root,
1883 &fs_info->qgroup_rescan_progress,
1886 pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
1887 (unsigned long long)fs_info->qgroup_rescan_progress.objectid,
1888 fs_info->qgroup_rescan_progress.type,
1889 (unsigned long long)fs_info->qgroup_rescan_progress.offset,
1894 * The rescan is about to end, we will not be scanning any
1895 * further blocks. We cannot unset the RESCAN flag here, because
1896 * we want to commit the transaction if everything went well.
1897 * To make the live accounting work in this phase, we set our
1898 * scan progress pointer such that every real extent objectid
1901 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
1902 btrfs_release_path(path);
1903 mutex_unlock(&fs_info->qgroup_rescan_lock);
1907 btrfs_item_key_to_cpu(path->nodes[0], &found,
1908 btrfs_header_nritems(path->nodes[0]) - 1);
1909 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
1911 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
1912 memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
1913 slot = path->slots[0];
1914 btrfs_release_path(path);
1915 mutex_unlock(&fs_info->qgroup_rescan_lock);
1917 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
1918 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
1919 if (found.type != BTRFS_EXTENT_ITEM_KEY)
1921 ret = btrfs_find_all_roots(trans, fs_info, found.objectid,
1922 tree_mod_seq_elem.seq, &roots);
1925 spin_lock(&fs_info->qgroup_lock);
1926 seq = fs_info->qgroup_seq;
1927 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
1929 ret = qgroup_account_ref_step1(fs_info, roots, tmp, seq);
1931 spin_unlock(&fs_info->qgroup_lock);
1937 * step2 of btrfs_qgroup_account_ref works from a single root,
1938 * we're doing all at once here.
1941 ULIST_ITER_INIT(&uiter);
1942 while ((unode = ulist_next(roots, &uiter))) {
1943 struct btrfs_qgroup *qg;
1945 qg = find_qgroup_rb(fs_info, unode->val);
1949 ret = ulist_add(tmp, qg->qgroupid, (uintptr_t)qg,
1952 spin_unlock(&fs_info->qgroup_lock);
1958 /* this loop is similar to step 2 of btrfs_qgroup_account_ref */
1959 ULIST_ITER_INIT(&uiter);
1960 while ((unode = ulist_next(tmp, &uiter))) {
1961 struct btrfs_qgroup *qg;
1962 struct btrfs_qgroup_list *glist;
1964 qg = (struct btrfs_qgroup *)(uintptr_t) unode->aux;
1965 qg->rfer += found.offset;
1966 qg->rfer_cmpr += found.offset;
1967 WARN_ON(qg->tag >= seq);
1968 if (qg->refcnt - seq == roots->nnodes) {
1969 qg->excl += found.offset;
1970 qg->excl_cmpr += found.offset;
1972 qgroup_dirty(fs_info, qg);
1974 list_for_each_entry(glist, &qg->groups, next_group) {
1975 ret = ulist_add(tmp, glist->group->qgroupid,
1976 (uintptr_t)glist->group,
1979 spin_unlock(&fs_info->qgroup_lock);
1986 spin_unlock(&fs_info->qgroup_lock);
1992 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
1997 static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
1999 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2000 qgroup_rescan_work);
2001 struct btrfs_path *path;
2002 struct btrfs_trans_handle *trans = NULL;
2003 struct ulist *tmp = NULL;
2004 struct extent_buffer *scratch_leaf = NULL;
2007 path = btrfs_alloc_path();
2010 tmp = ulist_alloc(GFP_NOFS);
2013 scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
2019 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2020 if (IS_ERR(trans)) {
2021 err = PTR_ERR(trans);
2024 if (!fs_info->quota_enabled) {
2027 err = qgroup_rescan_leaf(fs_info, path, trans,
2031 btrfs_commit_transaction(trans, fs_info->fs_root);
2033 btrfs_end_transaction(trans, fs_info->fs_root);
2037 kfree(scratch_leaf);
2039 btrfs_free_path(path);
2041 mutex_lock(&fs_info->qgroup_rescan_lock);
2042 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2045 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2046 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2047 } else if (err < 0) {
2048 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2050 mutex_unlock(&fs_info->qgroup_rescan_lock);
2053 pr_info("btrfs: qgroup scan completed%s\n",
2054 err == 2 ? " (inconsistency flag cleared)" : "");
2056 pr_err("btrfs: qgroup scan failed with %d\n", err);
2059 complete_all(&fs_info->qgroup_rescan_completion);
2063 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2064 * memory required for the rescan context.
2067 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2073 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2074 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2079 mutex_lock(&fs_info->qgroup_rescan_lock);
2080 spin_lock(&fs_info->qgroup_lock);
2083 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2085 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2089 spin_unlock(&fs_info->qgroup_lock);
2090 mutex_unlock(&fs_info->qgroup_rescan_lock);
2094 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2097 memset(&fs_info->qgroup_rescan_progress, 0,
2098 sizeof(fs_info->qgroup_rescan_progress));
2099 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
2101 spin_unlock(&fs_info->qgroup_lock);
2102 mutex_unlock(&fs_info->qgroup_rescan_lock);
2104 init_completion(&fs_info->qgroup_rescan_completion);
2106 memset(&fs_info->qgroup_rescan_work, 0,
2107 sizeof(fs_info->qgroup_rescan_work));
2108 fs_info->qgroup_rescan_work.func = btrfs_qgroup_rescan_worker;
2112 pr_info("btrfs: qgroup_rescan_init failed with %d\n", ret);
2120 qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2123 struct btrfs_qgroup *qgroup;
2125 spin_lock(&fs_info->qgroup_lock);
2126 /* clear all current qgroup tracking information */
2127 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2128 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2130 qgroup->rfer_cmpr = 0;
2132 qgroup->excl_cmpr = 0;
2134 spin_unlock(&fs_info->qgroup_lock);
2138 btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2141 struct btrfs_trans_handle *trans;
2143 ret = qgroup_rescan_init(fs_info, 0, 1);
2148 * We have set the rescan_progress to 0, which means no more
2149 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2150 * However, btrfs_qgroup_account_ref may be right after its call
2151 * to btrfs_find_all_roots, in which case it would still do the
2153 * To solve this, we're committing the transaction, which will
2154 * ensure we run all delayed refs and only after that, we are
2155 * going to clear all tracking information for a clean start.
2158 trans = btrfs_join_transaction(fs_info->fs_root);
2159 if (IS_ERR(trans)) {
2160 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2161 return PTR_ERR(trans);
2163 ret = btrfs_commit_transaction(trans, fs_info->fs_root);
2165 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2169 qgroup_rescan_zero_tracking(fs_info);
2171 btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
2172 &fs_info->qgroup_rescan_work);
2177 int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
2182 mutex_lock(&fs_info->qgroup_rescan_lock);
2183 spin_lock(&fs_info->qgroup_lock);
2184 running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2185 spin_unlock(&fs_info->qgroup_lock);
2186 mutex_unlock(&fs_info->qgroup_rescan_lock);
2189 ret = wait_for_completion_interruptible(
2190 &fs_info->qgroup_rescan_completion);
2196 * this is only called from open_ctree where we're still single threaded, thus
2197 * locking is omitted here.
2200 btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2202 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2203 btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
2204 &fs_info->qgroup_rescan_work);