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 static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
162 struct btrfs_qgroup_list *list;
164 list_del(&qgroup->dirty);
165 while (!list_empty(&qgroup->groups)) {
166 list = list_first_entry(&qgroup->groups,
167 struct btrfs_qgroup_list, next_group);
168 list_del(&list->next_group);
169 list_del(&list->next_member);
173 while (!list_empty(&qgroup->members)) {
174 list = list_first_entry(&qgroup->members,
175 struct btrfs_qgroup_list, next_member);
176 list_del(&list->next_group);
177 list_del(&list->next_member);
183 /* must be called with qgroup_lock held */
184 static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
186 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
191 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
192 __del_qgroup_rb(qgroup);
196 /* must be called with qgroup_lock held */
197 static int add_relation_rb(struct btrfs_fs_info *fs_info,
198 u64 memberid, u64 parentid)
200 struct btrfs_qgroup *member;
201 struct btrfs_qgroup *parent;
202 struct btrfs_qgroup_list *list;
204 member = find_qgroup_rb(fs_info, memberid);
205 parent = find_qgroup_rb(fs_info, parentid);
206 if (!member || !parent)
209 list = kzalloc(sizeof(*list), GFP_ATOMIC);
213 list->group = parent;
214 list->member = member;
215 list_add_tail(&list->next_group, &member->groups);
216 list_add_tail(&list->next_member, &parent->members);
221 /* must be called with qgroup_lock held */
222 static int del_relation_rb(struct btrfs_fs_info *fs_info,
223 u64 memberid, u64 parentid)
225 struct btrfs_qgroup *member;
226 struct btrfs_qgroup *parent;
227 struct btrfs_qgroup_list *list;
229 member = find_qgroup_rb(fs_info, memberid);
230 parent = find_qgroup_rb(fs_info, parentid);
231 if (!member || !parent)
234 list_for_each_entry(list, &member->groups, next_group) {
235 if (list->group == parent) {
236 list_del(&list->next_group);
237 list_del(&list->next_member);
246 * The full config is read in one go, only called from open_ctree()
247 * It doesn't use any locking, as at this point we're still single-threaded
249 int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
251 struct btrfs_key key;
252 struct btrfs_key found_key;
253 struct btrfs_root *quota_root = fs_info->quota_root;
254 struct btrfs_path *path = NULL;
255 struct extent_buffer *l;
259 u64 rescan_progress = 0;
261 if (!fs_info->quota_enabled)
264 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
265 if (!fs_info->qgroup_ulist) {
270 path = btrfs_alloc_path();
276 /* default this to quota off, in case no status key is found */
277 fs_info->qgroup_flags = 0;
280 * pass 1: read status, all qgroup infos and limits
285 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
290 struct btrfs_qgroup *qgroup;
292 slot = path->slots[0];
294 btrfs_item_key_to_cpu(l, &found_key, slot);
296 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
297 struct btrfs_qgroup_status_item *ptr;
299 ptr = btrfs_item_ptr(l, slot,
300 struct btrfs_qgroup_status_item);
302 if (btrfs_qgroup_status_version(l, ptr) !=
303 BTRFS_QGROUP_STATUS_VERSION) {
305 "btrfs: old qgroup version, quota disabled\n");
308 if (btrfs_qgroup_status_generation(l, ptr) !=
309 fs_info->generation) {
310 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
312 "btrfs: qgroup generation mismatch, "
313 "marked as inconsistent\n");
315 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
317 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
321 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
322 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
325 qgroup = find_qgroup_rb(fs_info, found_key.offset);
326 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
327 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
328 printk(KERN_ERR "btrfs: inconsitent qgroup config\n");
329 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
332 qgroup = add_qgroup_rb(fs_info, found_key.offset);
333 if (IS_ERR(qgroup)) {
334 ret = PTR_ERR(qgroup);
338 switch (found_key.type) {
339 case BTRFS_QGROUP_INFO_KEY: {
340 struct btrfs_qgroup_info_item *ptr;
342 ptr = btrfs_item_ptr(l, slot,
343 struct btrfs_qgroup_info_item);
344 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
345 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
346 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
347 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
348 /* generation currently unused */
351 case BTRFS_QGROUP_LIMIT_KEY: {
352 struct btrfs_qgroup_limit_item *ptr;
354 ptr = btrfs_item_ptr(l, slot,
355 struct btrfs_qgroup_limit_item);
356 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
357 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
358 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
359 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
360 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
365 ret = btrfs_next_item(quota_root, path);
371 btrfs_release_path(path);
374 * pass 2: read all qgroup relations
377 key.type = BTRFS_QGROUP_RELATION_KEY;
379 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
383 slot = path->slots[0];
385 btrfs_item_key_to_cpu(l, &found_key, slot);
387 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
390 if (found_key.objectid > found_key.offset) {
391 /* parent <- member, not needed to build config */
392 /* FIXME should we omit the key completely? */
396 ret = add_relation_rb(fs_info, found_key.objectid,
398 if (ret == -ENOENT) {
400 "btrfs: orphan qgroup relation 0x%llx->0x%llx\n",
401 (unsigned long long)found_key.objectid,
402 (unsigned long long)found_key.offset);
403 ret = 0; /* ignore the error */
408 ret = btrfs_next_item(quota_root, path);
415 fs_info->qgroup_flags |= flags;
416 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
417 fs_info->quota_enabled = 0;
418 fs_info->pending_quota_state = 0;
419 } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
421 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
423 btrfs_free_path(path);
426 ulist_free(fs_info->qgroup_ulist);
427 fs_info->qgroup_ulist = NULL;
428 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
431 return ret < 0 ? ret : 0;
435 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
436 * first two are in single-threaded paths.And for the third one, we have set
437 * quota_root to be null with qgroup_lock held before, so it is safe to clean
438 * up the in-memory structures without qgroup_lock held.
440 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
443 struct btrfs_qgroup *qgroup;
445 while ((n = rb_first(&fs_info->qgroup_tree))) {
446 qgroup = rb_entry(n, struct btrfs_qgroup, node);
447 rb_erase(n, &fs_info->qgroup_tree);
448 __del_qgroup_rb(qgroup);
451 * we call btrfs_free_qgroup_config() when umounting
452 * filesystem and disabling quota, so we set qgroup_ulit
453 * to be null here to avoid double free.
455 ulist_free(fs_info->qgroup_ulist);
456 fs_info->qgroup_ulist = NULL;
459 static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
460 struct btrfs_root *quota_root,
464 struct btrfs_path *path;
465 struct btrfs_key key;
467 path = btrfs_alloc_path();
472 key.type = BTRFS_QGROUP_RELATION_KEY;
475 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
477 btrfs_mark_buffer_dirty(path->nodes[0]);
479 btrfs_free_path(path);
483 static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
484 struct btrfs_root *quota_root,
488 struct btrfs_path *path;
489 struct btrfs_key key;
491 path = btrfs_alloc_path();
496 key.type = BTRFS_QGROUP_RELATION_KEY;
499 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
508 ret = btrfs_del_item(trans, quota_root, path);
510 btrfs_free_path(path);
514 static int add_qgroup_item(struct btrfs_trans_handle *trans,
515 struct btrfs_root *quota_root, u64 qgroupid)
518 struct btrfs_path *path;
519 struct btrfs_qgroup_info_item *qgroup_info;
520 struct btrfs_qgroup_limit_item *qgroup_limit;
521 struct extent_buffer *leaf;
522 struct btrfs_key key;
524 path = btrfs_alloc_path();
529 key.type = BTRFS_QGROUP_INFO_KEY;
530 key.offset = qgroupid;
532 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
533 sizeof(*qgroup_info));
537 leaf = path->nodes[0];
538 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
539 struct btrfs_qgroup_info_item);
540 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
541 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
542 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
543 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
544 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
546 btrfs_mark_buffer_dirty(leaf);
548 btrfs_release_path(path);
550 key.type = BTRFS_QGROUP_LIMIT_KEY;
551 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
552 sizeof(*qgroup_limit));
556 leaf = path->nodes[0];
557 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
558 struct btrfs_qgroup_limit_item);
559 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
560 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
561 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
562 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
563 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
565 btrfs_mark_buffer_dirty(leaf);
569 btrfs_free_path(path);
573 static int del_qgroup_item(struct btrfs_trans_handle *trans,
574 struct btrfs_root *quota_root, u64 qgroupid)
577 struct btrfs_path *path;
578 struct btrfs_key key;
580 path = btrfs_alloc_path();
585 key.type = BTRFS_QGROUP_INFO_KEY;
586 key.offset = qgroupid;
587 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
596 ret = btrfs_del_item(trans, quota_root, path);
600 btrfs_release_path(path);
602 key.type = BTRFS_QGROUP_LIMIT_KEY;
603 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
612 ret = btrfs_del_item(trans, quota_root, path);
615 btrfs_free_path(path);
619 static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
620 struct btrfs_root *root, u64 qgroupid,
621 u64 flags, u64 max_rfer, u64 max_excl,
622 u64 rsv_rfer, u64 rsv_excl)
624 struct btrfs_path *path;
625 struct btrfs_key key;
626 struct extent_buffer *l;
627 struct btrfs_qgroup_limit_item *qgroup_limit;
632 key.type = BTRFS_QGROUP_LIMIT_KEY;
633 key.offset = qgroupid;
635 path = btrfs_alloc_path();
639 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
647 slot = path->slots[0];
648 qgroup_limit = btrfs_item_ptr(l, path->slots[0],
649 struct btrfs_qgroup_limit_item);
650 btrfs_set_qgroup_limit_flags(l, qgroup_limit, flags);
651 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, max_rfer);
652 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, max_excl);
653 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, rsv_rfer);
654 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, rsv_excl);
656 btrfs_mark_buffer_dirty(l);
659 btrfs_free_path(path);
663 static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
664 struct btrfs_root *root,
665 struct btrfs_qgroup *qgroup)
667 struct btrfs_path *path;
668 struct btrfs_key key;
669 struct extent_buffer *l;
670 struct btrfs_qgroup_info_item *qgroup_info;
675 key.type = BTRFS_QGROUP_INFO_KEY;
676 key.offset = qgroup->qgroupid;
678 path = btrfs_alloc_path();
682 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
690 slot = path->slots[0];
691 qgroup_info = btrfs_item_ptr(l, path->slots[0],
692 struct btrfs_qgroup_info_item);
693 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
694 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
695 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
696 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
697 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
699 btrfs_mark_buffer_dirty(l);
702 btrfs_free_path(path);
706 static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
707 struct btrfs_fs_info *fs_info,
708 struct btrfs_root *root)
710 struct btrfs_path *path;
711 struct btrfs_key key;
712 struct extent_buffer *l;
713 struct btrfs_qgroup_status_item *ptr;
718 key.type = BTRFS_QGROUP_STATUS_KEY;
721 path = btrfs_alloc_path();
725 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
733 slot = path->slots[0];
734 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
735 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
736 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
737 btrfs_set_qgroup_status_rescan(l, ptr,
738 fs_info->qgroup_rescan_progress.objectid);
740 btrfs_mark_buffer_dirty(l);
743 btrfs_free_path(path);
748 * called with qgroup_lock held
750 static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
751 struct btrfs_root *root)
753 struct btrfs_path *path;
754 struct btrfs_key key;
755 struct extent_buffer *leaf = NULL;
759 path = btrfs_alloc_path();
763 path->leave_spinning = 1;
770 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
773 leaf = path->nodes[0];
774 nr = btrfs_header_nritems(leaf);
778 * delete the leaf one by one
779 * since the whole tree is going
783 ret = btrfs_del_items(trans, root, path, 0, nr);
787 btrfs_release_path(path);
791 root->fs_info->pending_quota_state = 0;
792 btrfs_free_path(path);
796 int btrfs_quota_enable(struct btrfs_trans_handle *trans,
797 struct btrfs_fs_info *fs_info)
799 struct btrfs_root *quota_root;
800 struct btrfs_root *tree_root = fs_info->tree_root;
801 struct btrfs_path *path = NULL;
802 struct btrfs_qgroup_status_item *ptr;
803 struct extent_buffer *leaf;
804 struct btrfs_key key;
805 struct btrfs_key found_key;
806 struct btrfs_qgroup *qgroup = NULL;
810 mutex_lock(&fs_info->qgroup_ioctl_lock);
811 if (fs_info->quota_root) {
812 fs_info->pending_quota_state = 1;
816 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
817 if (!fs_info->qgroup_ulist) {
823 * initially create the quota tree
825 quota_root = btrfs_create_tree(trans, fs_info,
826 BTRFS_QUOTA_TREE_OBJECTID);
827 if (IS_ERR(quota_root)) {
828 ret = PTR_ERR(quota_root);
832 path = btrfs_alloc_path();
839 key.type = BTRFS_QGROUP_STATUS_KEY;
842 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
847 leaf = path->nodes[0];
848 ptr = btrfs_item_ptr(leaf, path->slots[0],
849 struct btrfs_qgroup_status_item);
850 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
851 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
852 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
853 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
854 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
855 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
857 btrfs_mark_buffer_dirty(leaf);
860 key.type = BTRFS_ROOT_REF_KEY;
863 btrfs_release_path(path);
864 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
872 slot = path->slots[0];
873 leaf = path->nodes[0];
874 btrfs_item_key_to_cpu(leaf, &found_key, slot);
876 if (found_key.type == BTRFS_ROOT_REF_KEY) {
877 ret = add_qgroup_item(trans, quota_root,
882 qgroup = add_qgroup_rb(fs_info, found_key.offset);
883 if (IS_ERR(qgroup)) {
884 ret = PTR_ERR(qgroup);
888 ret = btrfs_next_item(tree_root, path);
896 btrfs_release_path(path);
897 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
901 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
902 if (IS_ERR(qgroup)) {
903 ret = PTR_ERR(qgroup);
906 spin_lock(&fs_info->qgroup_lock);
907 fs_info->quota_root = quota_root;
908 fs_info->pending_quota_state = 1;
909 spin_unlock(&fs_info->qgroup_lock);
911 btrfs_free_path(path);
914 free_extent_buffer(quota_root->node);
915 free_extent_buffer(quota_root->commit_root);
920 ulist_free(fs_info->qgroup_ulist);
921 fs_info->qgroup_ulist = NULL;
923 mutex_unlock(&fs_info->qgroup_ioctl_lock);
927 int btrfs_quota_disable(struct btrfs_trans_handle *trans,
928 struct btrfs_fs_info *fs_info)
930 struct btrfs_root *tree_root = fs_info->tree_root;
931 struct btrfs_root *quota_root;
934 mutex_lock(&fs_info->qgroup_ioctl_lock);
935 if (!fs_info->quota_root)
937 spin_lock(&fs_info->qgroup_lock);
938 fs_info->quota_enabled = 0;
939 fs_info->pending_quota_state = 0;
940 quota_root = fs_info->quota_root;
941 fs_info->quota_root = NULL;
942 spin_unlock(&fs_info->qgroup_lock);
944 btrfs_free_qgroup_config(fs_info);
951 ret = btrfs_clean_quota_tree(trans, quota_root);
955 ret = btrfs_del_root(trans, tree_root, "a_root->root_key);
959 list_del("a_root->dirty_list);
961 btrfs_tree_lock(quota_root->node);
962 clean_tree_block(trans, tree_root, quota_root->node);
963 btrfs_tree_unlock(quota_root->node);
964 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
966 free_extent_buffer(quota_root->node);
967 free_extent_buffer(quota_root->commit_root);
970 mutex_unlock(&fs_info->qgroup_ioctl_lock);
974 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
975 struct btrfs_qgroup *qgroup)
977 if (list_empty(&qgroup->dirty))
978 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
981 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
982 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
984 struct btrfs_root *quota_root;
985 struct btrfs_qgroup *parent;
986 struct btrfs_qgroup *member;
987 struct btrfs_qgroup_list *list;
990 mutex_lock(&fs_info->qgroup_ioctl_lock);
991 quota_root = fs_info->quota_root;
996 member = find_qgroup_rb(fs_info, src);
997 parent = find_qgroup_rb(fs_info, dst);
998 if (!member || !parent) {
1003 /* check if such qgroup relation exist firstly */
1004 list_for_each_entry(list, &member->groups, next_group) {
1005 if (list->group == parent) {
1011 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1015 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1017 del_qgroup_relation_item(trans, quota_root, src, dst);
1021 spin_lock(&fs_info->qgroup_lock);
1022 ret = add_relation_rb(quota_root->fs_info, src, dst);
1023 spin_unlock(&fs_info->qgroup_lock);
1025 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1029 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1030 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1032 struct btrfs_root *quota_root;
1033 struct btrfs_qgroup *parent;
1034 struct btrfs_qgroup *member;
1035 struct btrfs_qgroup_list *list;
1039 mutex_lock(&fs_info->qgroup_ioctl_lock);
1040 quota_root = fs_info->quota_root;
1046 member = find_qgroup_rb(fs_info, src);
1047 parent = find_qgroup_rb(fs_info, dst);
1048 if (!member || !parent) {
1053 /* check if such qgroup relation exist firstly */
1054 list_for_each_entry(list, &member->groups, next_group) {
1055 if (list->group == parent)
1061 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1062 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1066 spin_lock(&fs_info->qgroup_lock);
1067 del_relation_rb(fs_info, src, dst);
1068 spin_unlock(&fs_info->qgroup_lock);
1070 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1074 int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
1075 struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
1077 struct btrfs_root *quota_root;
1078 struct btrfs_qgroup *qgroup;
1081 mutex_lock(&fs_info->qgroup_ioctl_lock);
1082 quota_root = fs_info->quota_root;
1087 qgroup = find_qgroup_rb(fs_info, qgroupid);
1093 ret = add_qgroup_item(trans, quota_root, qgroupid);
1097 spin_lock(&fs_info->qgroup_lock);
1098 qgroup = add_qgroup_rb(fs_info, qgroupid);
1099 spin_unlock(&fs_info->qgroup_lock);
1102 ret = PTR_ERR(qgroup);
1104 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1108 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1109 struct btrfs_fs_info *fs_info, u64 qgroupid)
1111 struct btrfs_root *quota_root;
1112 struct btrfs_qgroup *qgroup;
1115 mutex_lock(&fs_info->qgroup_ioctl_lock);
1116 quota_root = fs_info->quota_root;
1122 qgroup = find_qgroup_rb(fs_info, qgroupid);
1127 /* check if there are no relations to this qgroup */
1128 if (!list_empty(&qgroup->groups) ||
1129 !list_empty(&qgroup->members)) {
1134 ret = del_qgroup_item(trans, quota_root, qgroupid);
1136 spin_lock(&fs_info->qgroup_lock);
1137 del_qgroup_rb(quota_root->fs_info, qgroupid);
1138 spin_unlock(&fs_info->qgroup_lock);
1140 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1144 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1145 struct btrfs_fs_info *fs_info, u64 qgroupid,
1146 struct btrfs_qgroup_limit *limit)
1148 struct btrfs_root *quota_root;
1149 struct btrfs_qgroup *qgroup;
1152 mutex_lock(&fs_info->qgroup_ioctl_lock);
1153 quota_root = fs_info->quota_root;
1159 qgroup = find_qgroup_rb(fs_info, qgroupid);
1164 ret = update_qgroup_limit_item(trans, quota_root, qgroupid,
1165 limit->flags, limit->max_rfer,
1166 limit->max_excl, limit->rsv_rfer,
1169 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1170 printk(KERN_INFO "unable to update quota limit for %llu\n",
1171 (unsigned long long)qgroupid);
1174 spin_lock(&fs_info->qgroup_lock);
1175 qgroup->lim_flags = limit->flags;
1176 qgroup->max_rfer = limit->max_rfer;
1177 qgroup->max_excl = limit->max_excl;
1178 qgroup->rsv_rfer = limit->rsv_rfer;
1179 qgroup->rsv_excl = limit->rsv_excl;
1180 spin_unlock(&fs_info->qgroup_lock);
1182 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1187 * btrfs_qgroup_record_ref is called when the ref is added or deleted. it puts
1188 * the modification into a list that's later used by btrfs_end_transaction to
1189 * pass the recorded modifications on to btrfs_qgroup_account_ref.
1191 int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
1192 struct btrfs_delayed_ref_node *node,
1193 struct btrfs_delayed_extent_op *extent_op)
1195 struct qgroup_update *u;
1197 BUG_ON(!trans->delayed_ref_elem.seq);
1198 u = kmalloc(sizeof(*u), GFP_NOFS);
1203 u->extent_op = extent_op;
1204 list_add_tail(&u->list, &trans->qgroup_ref_list);
1209 static int qgroup_account_ref_step1(struct btrfs_fs_info *fs_info,
1210 struct ulist *roots, struct ulist *tmp,
1213 struct ulist_node *unode;
1214 struct ulist_iterator uiter;
1215 struct ulist_node *tmp_unode;
1216 struct ulist_iterator tmp_uiter;
1217 struct btrfs_qgroup *qg;
1220 ULIST_ITER_INIT(&uiter);
1221 while ((unode = ulist_next(roots, &uiter))) {
1222 qg = find_qgroup_rb(fs_info, unode->val);
1227 /* XXX id not needed */
1228 ret = ulist_add(tmp, qg->qgroupid,
1229 (u64)(uintptr_t)qg, GFP_ATOMIC);
1232 ULIST_ITER_INIT(&tmp_uiter);
1233 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1234 struct btrfs_qgroup_list *glist;
1236 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
1237 if (qg->refcnt < seq)
1238 qg->refcnt = seq + 1;
1242 list_for_each_entry(glist, &qg->groups, next_group) {
1243 ret = ulist_add(tmp, glist->group->qgroupid,
1244 (u64)(uintptr_t)glist->group,
1255 static int qgroup_account_ref_step2(struct btrfs_fs_info *fs_info,
1256 struct ulist *roots, struct ulist *tmp,
1257 u64 seq, int sgn, u64 num_bytes,
1258 struct btrfs_qgroup *qgroup)
1260 struct ulist_node *unode;
1261 struct ulist_iterator uiter;
1262 struct btrfs_qgroup *qg;
1263 struct btrfs_qgroup_list *glist;
1267 ret = ulist_add(tmp, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
1271 ULIST_ITER_INIT(&uiter);
1272 while ((unode = ulist_next(tmp, &uiter))) {
1273 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1274 if (qg->refcnt < seq) {
1275 /* not visited by step 1 */
1276 qg->rfer += sgn * num_bytes;
1277 qg->rfer_cmpr += sgn * num_bytes;
1278 if (roots->nnodes == 0) {
1279 qg->excl += sgn * num_bytes;
1280 qg->excl_cmpr += sgn * num_bytes;
1282 qgroup_dirty(fs_info, qg);
1284 WARN_ON(qg->tag >= seq);
1287 list_for_each_entry(glist, &qg->groups, next_group) {
1288 ret = ulist_add(tmp, glist->group->qgroupid,
1289 (uintptr_t)glist->group, GFP_ATOMIC);
1298 static int qgroup_account_ref_step3(struct btrfs_fs_info *fs_info,
1299 struct ulist *roots, struct ulist *tmp,
1300 u64 seq, int sgn, u64 num_bytes)
1302 struct ulist_node *unode;
1303 struct ulist_iterator uiter;
1304 struct btrfs_qgroup *qg;
1305 struct ulist_node *tmp_unode;
1306 struct ulist_iterator tmp_uiter;
1309 ULIST_ITER_INIT(&uiter);
1310 while ((unode = ulist_next(roots, &uiter))) {
1311 qg = find_qgroup_rb(fs_info, unode->val);
1316 ret = ulist_add(tmp, qg->qgroupid, (uintptr_t)qg, GFP_ATOMIC);
1320 ULIST_ITER_INIT(&tmp_uiter);
1321 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1322 struct btrfs_qgroup_list *glist;
1324 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
1328 if (qg->refcnt - seq == roots->nnodes) {
1329 qg->excl -= sgn * num_bytes;
1330 qg->excl_cmpr -= sgn * num_bytes;
1331 qgroup_dirty(fs_info, qg);
1334 list_for_each_entry(glist, &qg->groups, next_group) {
1335 ret = ulist_add(tmp, glist->group->qgroupid,
1336 (uintptr_t)glist->group,
1348 * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
1349 * from the fs. First, all roots referencing the extent are searched, and
1350 * then the space is accounted accordingly to the different roots. The
1351 * accounting algorithm works in 3 steps documented inline.
1353 int btrfs_qgroup_account_ref(struct btrfs_trans_handle *trans,
1354 struct btrfs_fs_info *fs_info,
1355 struct btrfs_delayed_ref_node *node,
1356 struct btrfs_delayed_extent_op *extent_op)
1358 struct btrfs_key ins;
1359 struct btrfs_root *quota_root;
1361 struct btrfs_qgroup *qgroup;
1362 struct ulist *roots = NULL;
1367 if (!fs_info->quota_enabled)
1370 BUG_ON(!fs_info->quota_root);
1372 ins.objectid = node->bytenr;
1373 ins.offset = node->num_bytes;
1374 ins.type = BTRFS_EXTENT_ITEM_KEY;
1376 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1377 node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
1378 struct btrfs_delayed_tree_ref *ref;
1379 ref = btrfs_delayed_node_to_tree_ref(node);
1380 ref_root = ref->root;
1381 } else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1382 node->type == BTRFS_SHARED_DATA_REF_KEY) {
1383 struct btrfs_delayed_data_ref *ref;
1384 ref = btrfs_delayed_node_to_data_ref(node);
1385 ref_root = ref->root;
1390 if (!is_fstree(ref_root)) {
1392 * non-fs-trees are not being accounted
1397 switch (node->action) {
1398 case BTRFS_ADD_DELAYED_REF:
1399 case BTRFS_ADD_DELAYED_EXTENT:
1401 seq = btrfs_tree_mod_seq_prev(node->seq);
1403 case BTRFS_DROP_DELAYED_REF:
1407 case BTRFS_UPDATE_DELAYED_HEAD:
1413 mutex_lock(&fs_info->qgroup_rescan_lock);
1414 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
1415 if (fs_info->qgroup_rescan_progress.objectid <= node->bytenr) {
1416 mutex_unlock(&fs_info->qgroup_rescan_lock);
1420 mutex_unlock(&fs_info->qgroup_rescan_lock);
1423 * the delayed ref sequence number we pass depends on the direction of
1424 * the operation. for add operations, we pass
1425 * tree_mod_log_prev_seq(node->seq) to skip
1426 * the delayed ref's current sequence number, because we need the state
1427 * of the tree before the add operation. for delete operations, we pass
1428 * (node->seq) to include the delayed ref's current sequence number,
1429 * because we need the state of the tree after the delete operation.
1431 ret = btrfs_find_all_roots(trans, fs_info, node->bytenr, seq, &roots);
1435 spin_lock(&fs_info->qgroup_lock);
1437 quota_root = fs_info->quota_root;
1441 qgroup = find_qgroup_rb(fs_info, ref_root);
1446 * step 1: for each old ref, visit all nodes once and inc refcnt
1448 ulist_reinit(fs_info->qgroup_ulist);
1449 seq = fs_info->qgroup_seq;
1450 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
1452 ret = qgroup_account_ref_step1(fs_info, roots, fs_info->qgroup_ulist,
1458 * step 2: walk from the new root
1460 ret = qgroup_account_ref_step2(fs_info, roots, fs_info->qgroup_ulist,
1461 seq, sgn, node->num_bytes, qgroup);
1466 * step 3: walk again from old refs
1468 ret = qgroup_account_ref_step3(fs_info, roots, fs_info->qgroup_ulist,
1469 seq, sgn, node->num_bytes);
1474 spin_unlock(&fs_info->qgroup_lock);
1481 * called from commit_transaction. Writes all changed qgroups to disk.
1483 int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
1484 struct btrfs_fs_info *fs_info)
1486 struct btrfs_root *quota_root = fs_info->quota_root;
1488 int start_rescan_worker = 0;
1493 if (!fs_info->quota_enabled && fs_info->pending_quota_state)
1494 start_rescan_worker = 1;
1496 fs_info->quota_enabled = fs_info->pending_quota_state;
1498 spin_lock(&fs_info->qgroup_lock);
1499 while (!list_empty(&fs_info->dirty_qgroups)) {
1500 struct btrfs_qgroup *qgroup;
1501 qgroup = list_first_entry(&fs_info->dirty_qgroups,
1502 struct btrfs_qgroup, dirty);
1503 list_del_init(&qgroup->dirty);
1504 spin_unlock(&fs_info->qgroup_lock);
1505 ret = update_qgroup_info_item(trans, quota_root, qgroup);
1507 fs_info->qgroup_flags |=
1508 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1509 spin_lock(&fs_info->qgroup_lock);
1511 if (fs_info->quota_enabled)
1512 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
1514 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1515 spin_unlock(&fs_info->qgroup_lock);
1517 ret = update_qgroup_status_item(trans, fs_info, quota_root);
1519 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1521 if (!ret && start_rescan_worker) {
1522 ret = qgroup_rescan_init(fs_info, 0, 1);
1524 qgroup_rescan_zero_tracking(fs_info);
1525 btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
1526 &fs_info->qgroup_rescan_work);
1537 * copy the acounting information between qgroups. This is necessary when a
1538 * snapshot or a subvolume is created
1540 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
1541 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
1542 struct btrfs_qgroup_inherit *inherit)
1547 struct btrfs_root *quota_root = fs_info->quota_root;
1548 struct btrfs_qgroup *srcgroup;
1549 struct btrfs_qgroup *dstgroup;
1553 mutex_lock(&fs_info->qgroup_ioctl_lock);
1554 if (!fs_info->quota_enabled)
1563 i_qgroups = (u64 *)(inherit + 1);
1564 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
1565 2 * inherit->num_excl_copies;
1566 for (i = 0; i < nums; ++i) {
1567 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
1577 * create a tracking group for the subvol itself
1579 ret = add_qgroup_item(trans, quota_root, objectid);
1583 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
1584 ret = update_qgroup_limit_item(trans, quota_root, objectid,
1586 inherit->lim.max_rfer,
1587 inherit->lim.max_excl,
1588 inherit->lim.rsv_rfer,
1589 inherit->lim.rsv_excl);
1595 struct btrfs_root *srcroot;
1596 struct btrfs_key srckey;
1599 srckey.objectid = srcid;
1600 srckey.type = BTRFS_ROOT_ITEM_KEY;
1601 srckey.offset = (u64)-1;
1602 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
1603 if (IS_ERR(srcroot)) {
1604 ret = PTR_ERR(srcroot);
1609 srcroot_level = btrfs_header_level(srcroot->node);
1610 level_size = btrfs_level_size(srcroot, srcroot_level);
1615 * add qgroup to all inherited groups
1618 i_qgroups = (u64 *)(inherit + 1);
1619 for (i = 0; i < inherit->num_qgroups; ++i) {
1620 ret = add_qgroup_relation_item(trans, quota_root,
1621 objectid, *i_qgroups);
1624 ret = add_qgroup_relation_item(trans, quota_root,
1625 *i_qgroups, objectid);
1633 spin_lock(&fs_info->qgroup_lock);
1635 dstgroup = add_qgroup_rb(fs_info, objectid);
1636 if (IS_ERR(dstgroup)) {
1637 ret = PTR_ERR(dstgroup);
1642 srcgroup = find_qgroup_rb(fs_info, srcid);
1645 dstgroup->rfer = srcgroup->rfer - level_size;
1646 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr - level_size;
1647 srcgroup->excl = level_size;
1648 srcgroup->excl_cmpr = level_size;
1649 qgroup_dirty(fs_info, dstgroup);
1650 qgroup_dirty(fs_info, srcgroup);
1656 i_qgroups = (u64 *)(inherit + 1);
1657 for (i = 0; i < inherit->num_qgroups; ++i) {
1658 ret = add_relation_rb(quota_root->fs_info, objectid,
1665 for (i = 0; i < inherit->num_ref_copies; ++i) {
1666 struct btrfs_qgroup *src;
1667 struct btrfs_qgroup *dst;
1669 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1670 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1677 dst->rfer = src->rfer - level_size;
1678 dst->rfer_cmpr = src->rfer_cmpr - level_size;
1681 for (i = 0; i < inherit->num_excl_copies; ++i) {
1682 struct btrfs_qgroup *src;
1683 struct btrfs_qgroup *dst;
1685 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1686 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1693 dst->excl = src->excl + level_size;
1694 dst->excl_cmpr = src->excl_cmpr + level_size;
1699 spin_unlock(&fs_info->qgroup_lock);
1701 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1706 * reserve some space for a qgroup and all its parents. The reservation takes
1707 * place with start_transaction or dealloc_reserve, similar to ENOSPC
1708 * accounting. If not enough space is available, EDQUOT is returned.
1709 * We assume that the requested space is new for all qgroups.
1711 int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
1713 struct btrfs_root *quota_root;
1714 struct btrfs_qgroup *qgroup;
1715 struct btrfs_fs_info *fs_info = root->fs_info;
1716 u64 ref_root = root->root_key.objectid;
1718 struct ulist_node *unode;
1719 struct ulist_iterator uiter;
1721 if (!is_fstree(ref_root))
1727 spin_lock(&fs_info->qgroup_lock);
1728 quota_root = fs_info->quota_root;
1732 qgroup = find_qgroup_rb(fs_info, ref_root);
1737 * in a first step, we check all affected qgroups if any limits would
1740 ulist_reinit(fs_info->qgroup_ulist);
1741 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
1742 (uintptr_t)qgroup, GFP_ATOMIC);
1745 ULIST_ITER_INIT(&uiter);
1746 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
1747 struct btrfs_qgroup *qg;
1748 struct btrfs_qgroup_list *glist;
1750 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1752 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
1753 qg->reserved + (s64)qg->rfer + num_bytes >
1759 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
1760 qg->reserved + (s64)qg->excl + num_bytes >
1766 list_for_each_entry(glist, &qg->groups, next_group) {
1767 ret = ulist_add(fs_info->qgroup_ulist,
1768 glist->group->qgroupid,
1769 (uintptr_t)glist->group, GFP_ATOMIC);
1776 * no limits exceeded, now record the reservation into all qgroups
1778 ULIST_ITER_INIT(&uiter);
1779 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
1780 struct btrfs_qgroup *qg;
1782 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1784 qg->reserved += num_bytes;
1788 spin_unlock(&fs_info->qgroup_lock);
1792 void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
1794 struct btrfs_root *quota_root;
1795 struct btrfs_qgroup *qgroup;
1796 struct btrfs_fs_info *fs_info = root->fs_info;
1797 struct ulist_node *unode;
1798 struct ulist_iterator uiter;
1799 u64 ref_root = root->root_key.objectid;
1802 if (!is_fstree(ref_root))
1808 spin_lock(&fs_info->qgroup_lock);
1810 quota_root = fs_info->quota_root;
1814 qgroup = find_qgroup_rb(fs_info, ref_root);
1818 ulist_reinit(fs_info->qgroup_ulist);
1819 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
1820 (uintptr_t)qgroup, GFP_ATOMIC);
1823 ULIST_ITER_INIT(&uiter);
1824 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
1825 struct btrfs_qgroup *qg;
1826 struct btrfs_qgroup_list *glist;
1828 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1830 qg->reserved -= num_bytes;
1832 list_for_each_entry(glist, &qg->groups, next_group) {
1833 ret = ulist_add(fs_info->qgroup_ulist,
1834 glist->group->qgroupid,
1835 (uintptr_t)glist->group, GFP_ATOMIC);
1842 spin_unlock(&fs_info->qgroup_lock);
1845 void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
1847 if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
1849 pr_err("btrfs: qgroups not uptodate in trans handle %p: list is%s empty, seq is %#x.%x\n",
1850 trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
1851 (u32)(trans->delayed_ref_elem.seq >> 32),
1852 (u32)trans->delayed_ref_elem.seq);
1857 * returns < 0 on error, 0 when more leafs are to be scanned.
1858 * returns 1 when done, 2 when done and FLAG_INCONSISTENT was cleared.
1861 qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1862 struct btrfs_trans_handle *trans, struct ulist *tmp,
1863 struct extent_buffer *scratch_leaf)
1865 struct btrfs_key found;
1866 struct ulist *roots = NULL;
1867 struct ulist_node *unode;
1868 struct ulist_iterator uiter;
1869 struct seq_list tree_mod_seq_elem = {};
1874 path->leave_spinning = 1;
1875 mutex_lock(&fs_info->qgroup_rescan_lock);
1876 ret = btrfs_search_slot_for_read(fs_info->extent_root,
1877 &fs_info->qgroup_rescan_progress,
1880 pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
1881 (unsigned long long)fs_info->qgroup_rescan_progress.objectid,
1882 fs_info->qgroup_rescan_progress.type,
1883 (unsigned long long)fs_info->qgroup_rescan_progress.offset,
1888 * The rescan is about to end, we will not be scanning any
1889 * further blocks. We cannot unset the RESCAN flag here, because
1890 * we want to commit the transaction if everything went well.
1891 * To make the live accounting work in this phase, we set our
1892 * scan progress pointer such that every real extent objectid
1895 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
1896 btrfs_release_path(path);
1897 mutex_unlock(&fs_info->qgroup_rescan_lock);
1901 btrfs_item_key_to_cpu(path->nodes[0], &found,
1902 btrfs_header_nritems(path->nodes[0]) - 1);
1903 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
1905 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
1906 memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
1907 slot = path->slots[0];
1908 btrfs_release_path(path);
1909 mutex_unlock(&fs_info->qgroup_rescan_lock);
1911 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
1912 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
1913 if (found.type != BTRFS_EXTENT_ITEM_KEY)
1915 ret = btrfs_find_all_roots(trans, fs_info, found.objectid,
1916 tree_mod_seq_elem.seq, &roots);
1919 spin_lock(&fs_info->qgroup_lock);
1920 seq = fs_info->qgroup_seq;
1921 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
1923 ret = qgroup_account_ref_step1(fs_info, roots, tmp, seq);
1925 spin_unlock(&fs_info->qgroup_lock);
1931 * step2 of btrfs_qgroup_account_ref works from a single root,
1932 * we're doing all at once here.
1935 ULIST_ITER_INIT(&uiter);
1936 while ((unode = ulist_next(roots, &uiter))) {
1937 struct btrfs_qgroup *qg;
1939 qg = find_qgroup_rb(fs_info, unode->val);
1943 ret = ulist_add(tmp, qg->qgroupid, (uintptr_t)qg,
1946 spin_unlock(&fs_info->qgroup_lock);
1952 /* this loop is similar to step 2 of btrfs_qgroup_account_ref */
1953 ULIST_ITER_INIT(&uiter);
1954 while ((unode = ulist_next(tmp, &uiter))) {
1955 struct btrfs_qgroup *qg;
1956 struct btrfs_qgroup_list *glist;
1958 qg = (struct btrfs_qgroup *)(uintptr_t) unode->aux;
1959 qg->rfer += found.offset;
1960 qg->rfer_cmpr += found.offset;
1961 WARN_ON(qg->tag >= seq);
1962 if (qg->refcnt - seq == roots->nnodes) {
1963 qg->excl += found.offset;
1964 qg->excl_cmpr += found.offset;
1966 qgroup_dirty(fs_info, qg);
1968 list_for_each_entry(glist, &qg->groups, next_group) {
1969 ret = ulist_add(tmp, glist->group->qgroupid,
1970 (uintptr_t)glist->group,
1973 spin_unlock(&fs_info->qgroup_lock);
1980 spin_unlock(&fs_info->qgroup_lock);
1986 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
1991 static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
1993 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
1994 qgroup_rescan_work);
1995 struct btrfs_path *path;
1996 struct btrfs_trans_handle *trans = NULL;
1997 struct ulist *tmp = NULL;
1998 struct extent_buffer *scratch_leaf = NULL;
2001 path = btrfs_alloc_path();
2004 tmp = ulist_alloc(GFP_NOFS);
2007 scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
2013 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2014 if (IS_ERR(trans)) {
2015 err = PTR_ERR(trans);
2018 if (!fs_info->quota_enabled) {
2021 err = qgroup_rescan_leaf(fs_info, path, trans,
2025 btrfs_commit_transaction(trans, fs_info->fs_root);
2027 btrfs_end_transaction(trans, fs_info->fs_root);
2031 kfree(scratch_leaf);
2033 btrfs_free_path(path);
2035 mutex_lock(&fs_info->qgroup_rescan_lock);
2036 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2039 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2040 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2041 } else if (err < 0) {
2042 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2044 mutex_unlock(&fs_info->qgroup_rescan_lock);
2047 pr_info("btrfs: qgroup scan completed%s\n",
2048 err == 2 ? " (inconsistency flag cleared)" : "");
2050 pr_err("btrfs: qgroup scan failed with %d\n", err);
2053 complete_all(&fs_info->qgroup_rescan_completion);
2057 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2058 * memory required for the rescan context.
2061 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2067 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2068 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2073 mutex_lock(&fs_info->qgroup_rescan_lock);
2074 spin_lock(&fs_info->qgroup_lock);
2077 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2079 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2083 spin_unlock(&fs_info->qgroup_lock);
2084 mutex_unlock(&fs_info->qgroup_rescan_lock);
2088 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2091 memset(&fs_info->qgroup_rescan_progress, 0,
2092 sizeof(fs_info->qgroup_rescan_progress));
2093 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
2095 spin_unlock(&fs_info->qgroup_lock);
2096 mutex_unlock(&fs_info->qgroup_rescan_lock);
2098 init_completion(&fs_info->qgroup_rescan_completion);
2100 memset(&fs_info->qgroup_rescan_work, 0,
2101 sizeof(fs_info->qgroup_rescan_work));
2102 fs_info->qgroup_rescan_work.func = btrfs_qgroup_rescan_worker;
2106 pr_info("btrfs: qgroup_rescan_init failed with %d\n", ret);
2114 qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2117 struct btrfs_qgroup *qgroup;
2119 spin_lock(&fs_info->qgroup_lock);
2120 /* clear all current qgroup tracking information */
2121 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2122 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2124 qgroup->rfer_cmpr = 0;
2126 qgroup->excl_cmpr = 0;
2128 spin_unlock(&fs_info->qgroup_lock);
2132 btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2135 struct btrfs_trans_handle *trans;
2137 ret = qgroup_rescan_init(fs_info, 0, 1);
2142 * We have set the rescan_progress to 0, which means no more
2143 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2144 * However, btrfs_qgroup_account_ref may be right after its call
2145 * to btrfs_find_all_roots, in which case it would still do the
2147 * To solve this, we're committing the transaction, which will
2148 * ensure we run all delayed refs and only after that, we are
2149 * going to clear all tracking information for a clean start.
2152 trans = btrfs_join_transaction(fs_info->fs_root);
2153 if (IS_ERR(trans)) {
2154 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2155 return PTR_ERR(trans);
2157 ret = btrfs_commit_transaction(trans, fs_info->fs_root);
2159 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2163 qgroup_rescan_zero_tracking(fs_info);
2165 btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
2166 &fs_info->qgroup_rescan_work);
2171 int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
2176 mutex_lock(&fs_info->qgroup_rescan_lock);
2177 spin_lock(&fs_info->qgroup_lock);
2178 running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2179 spin_unlock(&fs_info->qgroup_lock);
2180 mutex_unlock(&fs_info->qgroup_rescan_lock);
2183 ret = wait_for_completion_interruptible(
2184 &fs_info->qgroup_rescan_completion);
2190 * this is only called from open_ctree where we're still single threaded, thus
2191 * locking is omitted here.
2194 btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2196 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2197 btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
2198 &fs_info->qgroup_rescan_work);