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);
946 ret = btrfs_clean_quota_tree(trans, quota_root);
950 ret = btrfs_del_root(trans, tree_root, "a_root->root_key);
954 list_del("a_root->dirty_list);
956 btrfs_tree_lock(quota_root->node);
957 clean_tree_block(trans, tree_root, quota_root->node);
958 btrfs_tree_unlock(quota_root->node);
959 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
961 free_extent_buffer(quota_root->node);
962 free_extent_buffer(quota_root->commit_root);
965 mutex_unlock(&fs_info->qgroup_ioctl_lock);
969 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
970 struct btrfs_qgroup *qgroup)
972 if (list_empty(&qgroup->dirty))
973 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
976 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
977 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
979 struct btrfs_root *quota_root;
980 struct btrfs_qgroup *parent;
981 struct btrfs_qgroup *member;
982 struct btrfs_qgroup_list *list;
985 mutex_lock(&fs_info->qgroup_ioctl_lock);
986 quota_root = fs_info->quota_root;
991 member = find_qgroup_rb(fs_info, src);
992 parent = find_qgroup_rb(fs_info, dst);
993 if (!member || !parent) {
998 /* check if such qgroup relation exist firstly */
999 list_for_each_entry(list, &member->groups, next_group) {
1000 if (list->group == parent) {
1006 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1010 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1012 del_qgroup_relation_item(trans, quota_root, src, dst);
1016 spin_lock(&fs_info->qgroup_lock);
1017 ret = add_relation_rb(quota_root->fs_info, src, dst);
1018 spin_unlock(&fs_info->qgroup_lock);
1020 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1024 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1025 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1027 struct btrfs_root *quota_root;
1028 struct btrfs_qgroup *parent;
1029 struct btrfs_qgroup *member;
1030 struct btrfs_qgroup_list *list;
1034 mutex_lock(&fs_info->qgroup_ioctl_lock);
1035 quota_root = fs_info->quota_root;
1041 member = find_qgroup_rb(fs_info, src);
1042 parent = find_qgroup_rb(fs_info, dst);
1043 if (!member || !parent) {
1048 /* check if such qgroup relation exist firstly */
1049 list_for_each_entry(list, &member->groups, next_group) {
1050 if (list->group == parent)
1056 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1057 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1061 spin_lock(&fs_info->qgroup_lock);
1062 del_relation_rb(fs_info, src, dst);
1063 spin_unlock(&fs_info->qgroup_lock);
1065 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1069 int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
1070 struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
1072 struct btrfs_root *quota_root;
1073 struct btrfs_qgroup *qgroup;
1076 mutex_lock(&fs_info->qgroup_ioctl_lock);
1077 quota_root = fs_info->quota_root;
1082 qgroup = find_qgroup_rb(fs_info, qgroupid);
1088 ret = add_qgroup_item(trans, quota_root, qgroupid);
1092 spin_lock(&fs_info->qgroup_lock);
1093 qgroup = add_qgroup_rb(fs_info, qgroupid);
1094 spin_unlock(&fs_info->qgroup_lock);
1097 ret = PTR_ERR(qgroup);
1099 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1103 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1104 struct btrfs_fs_info *fs_info, u64 qgroupid)
1106 struct btrfs_root *quota_root;
1107 struct btrfs_qgroup *qgroup;
1110 mutex_lock(&fs_info->qgroup_ioctl_lock);
1111 quota_root = fs_info->quota_root;
1117 qgroup = find_qgroup_rb(fs_info, qgroupid);
1122 /* check if there are no relations to this qgroup */
1123 if (!list_empty(&qgroup->groups) ||
1124 !list_empty(&qgroup->members)) {
1129 ret = del_qgroup_item(trans, quota_root, qgroupid);
1131 spin_lock(&fs_info->qgroup_lock);
1132 del_qgroup_rb(quota_root->fs_info, qgroupid);
1133 spin_unlock(&fs_info->qgroup_lock);
1135 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1139 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1140 struct btrfs_fs_info *fs_info, u64 qgroupid,
1141 struct btrfs_qgroup_limit *limit)
1143 struct btrfs_root *quota_root;
1144 struct btrfs_qgroup *qgroup;
1147 mutex_lock(&fs_info->qgroup_ioctl_lock);
1148 quota_root = fs_info->quota_root;
1154 qgroup = find_qgroup_rb(fs_info, qgroupid);
1159 ret = update_qgroup_limit_item(trans, quota_root, qgroupid,
1160 limit->flags, limit->max_rfer,
1161 limit->max_excl, limit->rsv_rfer,
1164 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1165 printk(KERN_INFO "unable to update quota limit for %llu\n",
1166 (unsigned long long)qgroupid);
1169 spin_lock(&fs_info->qgroup_lock);
1170 qgroup->lim_flags = limit->flags;
1171 qgroup->max_rfer = limit->max_rfer;
1172 qgroup->max_excl = limit->max_excl;
1173 qgroup->rsv_rfer = limit->rsv_rfer;
1174 qgroup->rsv_excl = limit->rsv_excl;
1175 spin_unlock(&fs_info->qgroup_lock);
1177 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1182 * btrfs_qgroup_record_ref is called when the ref is added or deleted. it puts
1183 * the modification into a list that's later used by btrfs_end_transaction to
1184 * pass the recorded modifications on to btrfs_qgroup_account_ref.
1186 int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
1187 struct btrfs_delayed_ref_node *node,
1188 struct btrfs_delayed_extent_op *extent_op)
1190 struct qgroup_update *u;
1192 BUG_ON(!trans->delayed_ref_elem.seq);
1193 u = kmalloc(sizeof(*u), GFP_NOFS);
1198 u->extent_op = extent_op;
1199 list_add_tail(&u->list, &trans->qgroup_ref_list);
1204 static int qgroup_account_ref_step1(struct btrfs_fs_info *fs_info,
1205 struct ulist *roots, struct ulist *tmp,
1208 struct ulist_node *unode;
1209 struct ulist_iterator uiter;
1210 struct ulist_node *tmp_unode;
1211 struct ulist_iterator tmp_uiter;
1212 struct btrfs_qgroup *qg;
1215 ULIST_ITER_INIT(&uiter);
1216 while ((unode = ulist_next(roots, &uiter))) {
1217 qg = find_qgroup_rb(fs_info, unode->val);
1222 /* XXX id not needed */
1223 ret = ulist_add(tmp, qg->qgroupid,
1224 (u64)(uintptr_t)qg, GFP_ATOMIC);
1227 ULIST_ITER_INIT(&tmp_uiter);
1228 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1229 struct btrfs_qgroup_list *glist;
1231 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
1232 if (qg->refcnt < seq)
1233 qg->refcnt = seq + 1;
1237 list_for_each_entry(glist, &qg->groups, next_group) {
1238 ret = ulist_add(tmp, glist->group->qgroupid,
1239 (u64)(uintptr_t)glist->group,
1250 static int qgroup_account_ref_step2(struct btrfs_fs_info *fs_info,
1251 struct ulist *roots, struct ulist *tmp,
1252 u64 seq, int sgn, u64 num_bytes,
1253 struct btrfs_qgroup *qgroup)
1255 struct ulist_node *unode;
1256 struct ulist_iterator uiter;
1257 struct btrfs_qgroup *qg;
1258 struct btrfs_qgroup_list *glist;
1262 ret = ulist_add(tmp, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
1266 ULIST_ITER_INIT(&uiter);
1267 while ((unode = ulist_next(tmp, &uiter))) {
1268 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1269 if (qg->refcnt < seq) {
1270 /* not visited by step 1 */
1271 qg->rfer += sgn * num_bytes;
1272 qg->rfer_cmpr += sgn * num_bytes;
1273 if (roots->nnodes == 0) {
1274 qg->excl += sgn * num_bytes;
1275 qg->excl_cmpr += sgn * num_bytes;
1277 qgroup_dirty(fs_info, qg);
1279 WARN_ON(qg->tag >= seq);
1282 list_for_each_entry(glist, &qg->groups, next_group) {
1283 ret = ulist_add(tmp, glist->group->qgroupid,
1284 (uintptr_t)glist->group, GFP_ATOMIC);
1293 static int qgroup_account_ref_step3(struct btrfs_fs_info *fs_info,
1294 struct ulist *roots, struct ulist *tmp,
1295 u64 seq, int sgn, u64 num_bytes)
1297 struct ulist_node *unode;
1298 struct ulist_iterator uiter;
1299 struct btrfs_qgroup *qg;
1300 struct ulist_node *tmp_unode;
1301 struct ulist_iterator tmp_uiter;
1304 ULIST_ITER_INIT(&uiter);
1305 while ((unode = ulist_next(roots, &uiter))) {
1306 qg = find_qgroup_rb(fs_info, unode->val);
1311 ret = ulist_add(tmp, qg->qgroupid, (uintptr_t)qg, GFP_ATOMIC);
1315 ULIST_ITER_INIT(&tmp_uiter);
1316 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1317 struct btrfs_qgroup_list *glist;
1319 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
1323 if (qg->refcnt - seq == roots->nnodes) {
1324 qg->excl -= sgn * num_bytes;
1325 qg->excl_cmpr -= sgn * num_bytes;
1326 qgroup_dirty(fs_info, qg);
1329 list_for_each_entry(glist, &qg->groups, next_group) {
1330 ret = ulist_add(tmp, glist->group->qgroupid,
1331 (uintptr_t)glist->group,
1343 * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
1344 * from the fs. First, all roots referencing the extent are searched, and
1345 * then the space is accounted accordingly to the different roots. The
1346 * accounting algorithm works in 3 steps documented inline.
1348 int btrfs_qgroup_account_ref(struct btrfs_trans_handle *trans,
1349 struct btrfs_fs_info *fs_info,
1350 struct btrfs_delayed_ref_node *node,
1351 struct btrfs_delayed_extent_op *extent_op)
1353 struct btrfs_key ins;
1354 struct btrfs_root *quota_root;
1356 struct btrfs_qgroup *qgroup;
1357 struct ulist *roots = NULL;
1362 if (!fs_info->quota_enabled)
1365 BUG_ON(!fs_info->quota_root);
1367 ins.objectid = node->bytenr;
1368 ins.offset = node->num_bytes;
1369 ins.type = BTRFS_EXTENT_ITEM_KEY;
1371 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1372 node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
1373 struct btrfs_delayed_tree_ref *ref;
1374 ref = btrfs_delayed_node_to_tree_ref(node);
1375 ref_root = ref->root;
1376 } else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1377 node->type == BTRFS_SHARED_DATA_REF_KEY) {
1378 struct btrfs_delayed_data_ref *ref;
1379 ref = btrfs_delayed_node_to_data_ref(node);
1380 ref_root = ref->root;
1385 if (!is_fstree(ref_root)) {
1387 * non-fs-trees are not being accounted
1392 switch (node->action) {
1393 case BTRFS_ADD_DELAYED_REF:
1394 case BTRFS_ADD_DELAYED_EXTENT:
1396 seq = btrfs_tree_mod_seq_prev(node->seq);
1398 case BTRFS_DROP_DELAYED_REF:
1402 case BTRFS_UPDATE_DELAYED_HEAD:
1408 mutex_lock(&fs_info->qgroup_rescan_lock);
1409 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
1410 if (fs_info->qgroup_rescan_progress.objectid <= node->bytenr) {
1411 mutex_unlock(&fs_info->qgroup_rescan_lock);
1415 mutex_unlock(&fs_info->qgroup_rescan_lock);
1418 * the delayed ref sequence number we pass depends on the direction of
1419 * the operation. for add operations, we pass
1420 * tree_mod_log_prev_seq(node->seq) to skip
1421 * the delayed ref's current sequence number, because we need the state
1422 * of the tree before the add operation. for delete operations, we pass
1423 * (node->seq) to include the delayed ref's current sequence number,
1424 * because we need the state of the tree after the delete operation.
1426 ret = btrfs_find_all_roots(trans, fs_info, node->bytenr, seq, &roots);
1430 spin_lock(&fs_info->qgroup_lock);
1432 quota_root = fs_info->quota_root;
1436 qgroup = find_qgroup_rb(fs_info, ref_root);
1441 * step 1: for each old ref, visit all nodes once and inc refcnt
1443 ulist_reinit(fs_info->qgroup_ulist);
1444 seq = fs_info->qgroup_seq;
1445 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
1447 ret = qgroup_account_ref_step1(fs_info, roots, fs_info->qgroup_ulist,
1453 * step 2: walk from the new root
1455 ret = qgroup_account_ref_step2(fs_info, roots, fs_info->qgroup_ulist,
1456 seq, sgn, node->num_bytes, qgroup);
1461 * step 3: walk again from old refs
1463 ret = qgroup_account_ref_step3(fs_info, roots, fs_info->qgroup_ulist,
1464 seq, sgn, node->num_bytes);
1469 spin_unlock(&fs_info->qgroup_lock);
1476 * called from commit_transaction. Writes all changed qgroups to disk.
1478 int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
1479 struct btrfs_fs_info *fs_info)
1481 struct btrfs_root *quota_root = fs_info->quota_root;
1483 int start_rescan_worker = 0;
1488 if (!fs_info->quota_enabled && fs_info->pending_quota_state)
1489 start_rescan_worker = 1;
1491 fs_info->quota_enabled = fs_info->pending_quota_state;
1493 spin_lock(&fs_info->qgroup_lock);
1494 while (!list_empty(&fs_info->dirty_qgroups)) {
1495 struct btrfs_qgroup *qgroup;
1496 qgroup = list_first_entry(&fs_info->dirty_qgroups,
1497 struct btrfs_qgroup, dirty);
1498 list_del_init(&qgroup->dirty);
1499 spin_unlock(&fs_info->qgroup_lock);
1500 ret = update_qgroup_info_item(trans, quota_root, qgroup);
1502 fs_info->qgroup_flags |=
1503 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1504 spin_lock(&fs_info->qgroup_lock);
1506 if (fs_info->quota_enabled)
1507 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
1509 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1510 spin_unlock(&fs_info->qgroup_lock);
1512 ret = update_qgroup_status_item(trans, fs_info, quota_root);
1514 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1516 if (!ret && start_rescan_worker) {
1517 ret = qgroup_rescan_init(fs_info, 0, 1);
1519 qgroup_rescan_zero_tracking(fs_info);
1520 btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
1521 &fs_info->qgroup_rescan_work);
1532 * copy the acounting information between qgroups. This is necessary when a
1533 * snapshot or a subvolume is created
1535 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
1536 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
1537 struct btrfs_qgroup_inherit *inherit)
1542 struct btrfs_root *quota_root = fs_info->quota_root;
1543 struct btrfs_qgroup *srcgroup;
1544 struct btrfs_qgroup *dstgroup;
1548 mutex_lock(&fs_info->qgroup_ioctl_lock);
1549 if (!fs_info->quota_enabled)
1558 i_qgroups = (u64 *)(inherit + 1);
1559 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
1560 2 * inherit->num_excl_copies;
1561 for (i = 0; i < nums; ++i) {
1562 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
1572 * create a tracking group for the subvol itself
1574 ret = add_qgroup_item(trans, quota_root, objectid);
1578 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
1579 ret = update_qgroup_limit_item(trans, quota_root, objectid,
1581 inherit->lim.max_rfer,
1582 inherit->lim.max_excl,
1583 inherit->lim.rsv_rfer,
1584 inherit->lim.rsv_excl);
1590 struct btrfs_root *srcroot;
1591 struct btrfs_key srckey;
1594 srckey.objectid = srcid;
1595 srckey.type = BTRFS_ROOT_ITEM_KEY;
1596 srckey.offset = (u64)-1;
1597 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
1598 if (IS_ERR(srcroot)) {
1599 ret = PTR_ERR(srcroot);
1604 srcroot_level = btrfs_header_level(srcroot->node);
1605 level_size = btrfs_level_size(srcroot, srcroot_level);
1610 * add qgroup to all inherited groups
1613 i_qgroups = (u64 *)(inherit + 1);
1614 for (i = 0; i < inherit->num_qgroups; ++i) {
1615 ret = add_qgroup_relation_item(trans, quota_root,
1616 objectid, *i_qgroups);
1619 ret = add_qgroup_relation_item(trans, quota_root,
1620 *i_qgroups, objectid);
1628 spin_lock(&fs_info->qgroup_lock);
1630 dstgroup = add_qgroup_rb(fs_info, objectid);
1631 if (IS_ERR(dstgroup)) {
1632 ret = PTR_ERR(dstgroup);
1637 srcgroup = find_qgroup_rb(fs_info, srcid);
1640 dstgroup->rfer = srcgroup->rfer - level_size;
1641 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr - level_size;
1642 srcgroup->excl = level_size;
1643 srcgroup->excl_cmpr = level_size;
1644 qgroup_dirty(fs_info, dstgroup);
1645 qgroup_dirty(fs_info, srcgroup);
1651 i_qgroups = (u64 *)(inherit + 1);
1652 for (i = 0; i < inherit->num_qgroups; ++i) {
1653 ret = add_relation_rb(quota_root->fs_info, objectid,
1660 for (i = 0; i < inherit->num_ref_copies; ++i) {
1661 struct btrfs_qgroup *src;
1662 struct btrfs_qgroup *dst;
1664 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1665 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1672 dst->rfer = src->rfer - level_size;
1673 dst->rfer_cmpr = src->rfer_cmpr - level_size;
1676 for (i = 0; i < inherit->num_excl_copies; ++i) {
1677 struct btrfs_qgroup *src;
1678 struct btrfs_qgroup *dst;
1680 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1681 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1688 dst->excl = src->excl + level_size;
1689 dst->excl_cmpr = src->excl_cmpr + level_size;
1694 spin_unlock(&fs_info->qgroup_lock);
1696 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1701 * reserve some space for a qgroup and all its parents. The reservation takes
1702 * place with start_transaction or dealloc_reserve, similar to ENOSPC
1703 * accounting. If not enough space is available, EDQUOT is returned.
1704 * We assume that the requested space is new for all qgroups.
1706 int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
1708 struct btrfs_root *quota_root;
1709 struct btrfs_qgroup *qgroup;
1710 struct btrfs_fs_info *fs_info = root->fs_info;
1711 u64 ref_root = root->root_key.objectid;
1713 struct ulist_node *unode;
1714 struct ulist_iterator uiter;
1716 if (!is_fstree(ref_root))
1722 spin_lock(&fs_info->qgroup_lock);
1723 quota_root = fs_info->quota_root;
1727 qgroup = find_qgroup_rb(fs_info, ref_root);
1732 * in a first step, we check all affected qgroups if any limits would
1735 ulist_reinit(fs_info->qgroup_ulist);
1736 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
1737 (uintptr_t)qgroup, GFP_ATOMIC);
1740 ULIST_ITER_INIT(&uiter);
1741 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
1742 struct btrfs_qgroup *qg;
1743 struct btrfs_qgroup_list *glist;
1745 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1747 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
1748 qg->reserved + (s64)qg->rfer + num_bytes >
1754 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
1755 qg->reserved + (s64)qg->excl + num_bytes >
1761 list_for_each_entry(glist, &qg->groups, next_group) {
1762 ret = ulist_add(fs_info->qgroup_ulist,
1763 glist->group->qgroupid,
1764 (uintptr_t)glist->group, GFP_ATOMIC);
1771 * no limits exceeded, now record the reservation into all qgroups
1773 ULIST_ITER_INIT(&uiter);
1774 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
1775 struct btrfs_qgroup *qg;
1777 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1779 qg->reserved += num_bytes;
1783 spin_unlock(&fs_info->qgroup_lock);
1787 void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
1789 struct btrfs_root *quota_root;
1790 struct btrfs_qgroup *qgroup;
1791 struct btrfs_fs_info *fs_info = root->fs_info;
1792 struct ulist_node *unode;
1793 struct ulist_iterator uiter;
1794 u64 ref_root = root->root_key.objectid;
1797 if (!is_fstree(ref_root))
1803 spin_lock(&fs_info->qgroup_lock);
1805 quota_root = fs_info->quota_root;
1809 qgroup = find_qgroup_rb(fs_info, ref_root);
1813 ulist_reinit(fs_info->qgroup_ulist);
1814 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
1815 (uintptr_t)qgroup, GFP_ATOMIC);
1818 ULIST_ITER_INIT(&uiter);
1819 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
1820 struct btrfs_qgroup *qg;
1821 struct btrfs_qgroup_list *glist;
1823 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1825 qg->reserved -= num_bytes;
1827 list_for_each_entry(glist, &qg->groups, next_group) {
1828 ret = ulist_add(fs_info->qgroup_ulist,
1829 glist->group->qgroupid,
1830 (uintptr_t)glist->group, GFP_ATOMIC);
1837 spin_unlock(&fs_info->qgroup_lock);
1840 void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
1842 if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
1844 pr_err("btrfs: qgroups not uptodate in trans handle %p: list is%s empty, seq is %#x.%x\n",
1845 trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
1846 (u32)(trans->delayed_ref_elem.seq >> 32),
1847 (u32)trans->delayed_ref_elem.seq);
1852 * returns < 0 on error, 0 when more leafs are to be scanned.
1853 * returns 1 when done, 2 when done and FLAG_INCONSISTENT was cleared.
1856 qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1857 struct btrfs_trans_handle *trans, struct ulist *tmp,
1858 struct extent_buffer *scratch_leaf)
1860 struct btrfs_key found;
1861 struct ulist *roots = NULL;
1862 struct ulist_node *unode;
1863 struct ulist_iterator uiter;
1864 struct seq_list tree_mod_seq_elem = {};
1869 path->leave_spinning = 1;
1870 mutex_lock(&fs_info->qgroup_rescan_lock);
1871 ret = btrfs_search_slot_for_read(fs_info->extent_root,
1872 &fs_info->qgroup_rescan_progress,
1875 pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
1876 (unsigned long long)fs_info->qgroup_rescan_progress.objectid,
1877 fs_info->qgroup_rescan_progress.type,
1878 (unsigned long long)fs_info->qgroup_rescan_progress.offset,
1883 * The rescan is about to end, we will not be scanning any
1884 * further blocks. We cannot unset the RESCAN flag here, because
1885 * we want to commit the transaction if everything went well.
1886 * To make the live accounting work in this phase, we set our
1887 * scan progress pointer such that every real extent objectid
1890 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
1891 btrfs_release_path(path);
1892 mutex_unlock(&fs_info->qgroup_rescan_lock);
1896 btrfs_item_key_to_cpu(path->nodes[0], &found,
1897 btrfs_header_nritems(path->nodes[0]) - 1);
1898 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
1900 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
1901 memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
1902 slot = path->slots[0];
1903 btrfs_release_path(path);
1904 mutex_unlock(&fs_info->qgroup_rescan_lock);
1906 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
1907 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
1908 if (found.type != BTRFS_EXTENT_ITEM_KEY)
1910 ret = btrfs_find_all_roots(trans, fs_info, found.objectid,
1911 tree_mod_seq_elem.seq, &roots);
1914 spin_lock(&fs_info->qgroup_lock);
1915 seq = fs_info->qgroup_seq;
1916 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
1918 ret = qgroup_account_ref_step1(fs_info, roots, tmp, seq);
1920 spin_unlock(&fs_info->qgroup_lock);
1926 * step2 of btrfs_qgroup_account_ref works from a single root,
1927 * we're doing all at once here.
1930 ULIST_ITER_INIT(&uiter);
1931 while ((unode = ulist_next(roots, &uiter))) {
1932 struct btrfs_qgroup *qg;
1934 qg = find_qgroup_rb(fs_info, unode->val);
1938 ret = ulist_add(tmp, qg->qgroupid, (uintptr_t)qg,
1941 spin_unlock(&fs_info->qgroup_lock);
1947 /* this loop is similar to step 2 of btrfs_qgroup_account_ref */
1948 ULIST_ITER_INIT(&uiter);
1949 while ((unode = ulist_next(tmp, &uiter))) {
1950 struct btrfs_qgroup *qg;
1951 struct btrfs_qgroup_list *glist;
1953 qg = (struct btrfs_qgroup *)(uintptr_t) unode->aux;
1954 qg->rfer += found.offset;
1955 qg->rfer_cmpr += found.offset;
1956 WARN_ON(qg->tag >= seq);
1957 if (qg->refcnt - seq == roots->nnodes) {
1958 qg->excl += found.offset;
1959 qg->excl_cmpr += found.offset;
1961 qgroup_dirty(fs_info, qg);
1963 list_for_each_entry(glist, &qg->groups, next_group) {
1964 ret = ulist_add(tmp, glist->group->qgroupid,
1965 (uintptr_t)glist->group,
1968 spin_unlock(&fs_info->qgroup_lock);
1975 spin_unlock(&fs_info->qgroup_lock);
1981 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
1986 static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
1988 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
1989 qgroup_rescan_work);
1990 struct btrfs_path *path;
1991 struct btrfs_trans_handle *trans = NULL;
1992 struct ulist *tmp = NULL;
1993 struct extent_buffer *scratch_leaf = NULL;
1996 path = btrfs_alloc_path();
1999 tmp = ulist_alloc(GFP_NOFS);
2002 scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
2008 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2009 if (IS_ERR(trans)) {
2010 err = PTR_ERR(trans);
2013 if (!fs_info->quota_enabled) {
2016 err = qgroup_rescan_leaf(fs_info, path, trans,
2020 btrfs_commit_transaction(trans, fs_info->fs_root);
2022 btrfs_end_transaction(trans, fs_info->fs_root);
2026 kfree(scratch_leaf);
2028 btrfs_free_path(path);
2030 mutex_lock(&fs_info->qgroup_rescan_lock);
2031 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2034 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2035 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2036 } else if (err < 0) {
2037 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2039 mutex_unlock(&fs_info->qgroup_rescan_lock);
2042 pr_info("btrfs: qgroup scan completed%s\n",
2043 err == 2 ? " (inconsistency flag cleared)" : "");
2045 pr_err("btrfs: qgroup scan failed with %d\n", err);
2048 complete_all(&fs_info->qgroup_rescan_completion);
2052 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2053 * memory required for the rescan context.
2056 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2062 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2063 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2068 mutex_lock(&fs_info->qgroup_rescan_lock);
2069 spin_lock(&fs_info->qgroup_lock);
2072 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2074 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2078 spin_unlock(&fs_info->qgroup_lock);
2079 mutex_unlock(&fs_info->qgroup_rescan_lock);
2083 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2086 memset(&fs_info->qgroup_rescan_progress, 0,
2087 sizeof(fs_info->qgroup_rescan_progress));
2088 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
2090 spin_unlock(&fs_info->qgroup_lock);
2091 mutex_unlock(&fs_info->qgroup_rescan_lock);
2093 init_completion(&fs_info->qgroup_rescan_completion);
2095 memset(&fs_info->qgroup_rescan_work, 0,
2096 sizeof(fs_info->qgroup_rescan_work));
2097 fs_info->qgroup_rescan_work.func = btrfs_qgroup_rescan_worker;
2101 pr_info("btrfs: qgroup_rescan_init failed with %d\n", ret);
2109 qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2112 struct btrfs_qgroup *qgroup;
2114 spin_lock(&fs_info->qgroup_lock);
2115 /* clear all current qgroup tracking information */
2116 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2117 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2119 qgroup->rfer_cmpr = 0;
2121 qgroup->excl_cmpr = 0;
2123 spin_unlock(&fs_info->qgroup_lock);
2127 btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2130 struct btrfs_trans_handle *trans;
2132 ret = qgroup_rescan_init(fs_info, 0, 1);
2137 * We have set the rescan_progress to 0, which means no more
2138 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2139 * However, btrfs_qgroup_account_ref may be right after its call
2140 * to btrfs_find_all_roots, in which case it would still do the
2142 * To solve this, we're committing the transaction, which will
2143 * ensure we run all delayed refs and only after that, we are
2144 * going to clear all tracking information for a clean start.
2147 trans = btrfs_join_transaction(fs_info->fs_root);
2148 if (IS_ERR(trans)) {
2149 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2150 return PTR_ERR(trans);
2152 ret = btrfs_commit_transaction(trans, fs_info->fs_root);
2154 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2158 qgroup_rescan_zero_tracking(fs_info);
2160 btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
2161 &fs_info->qgroup_rescan_work);
2166 int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
2171 mutex_lock(&fs_info->qgroup_rescan_lock);
2172 spin_lock(&fs_info->qgroup_lock);
2173 running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2174 spin_unlock(&fs_info->qgroup_lock);
2175 mutex_unlock(&fs_info->qgroup_rescan_lock);
2178 ret = wait_for_completion_interruptible(
2179 &fs_info->qgroup_rescan_completion);
2185 * this is only called from open_ctree where we're still single threaded, thus
2186 * locking is omitted here.
2189 btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2191 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2192 btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
2193 &fs_info->qgroup_rescan_work);