2 * Copyright (C) 2011 STRATO. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/pagemap.h>
21 #include <linux/writeback.h>
22 #include <linux/blkdev.h>
23 #include <linux/rbtree.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/btrfs.h>
29 #include "transaction.h"
34 #include "extent_io.h"
38 * - subvol delete -> delete when ref goes to 0? delete limits also?
42 * - copy also limits on subvol creation
44 * - caches fuer ulists
45 * - performance benchmarks
46 * - check all ioctl parameters
50 * one struct for each qgroup, organized in fs_info->qgroup_tree.
58 u64 rfer; /* referenced */
59 u64 rfer_cmpr; /* referenced compressed */
60 u64 excl; /* exclusive */
61 u64 excl_cmpr; /* exclusive compressed */
66 u64 lim_flags; /* which limits are set */
73 * reservation tracking
80 struct list_head groups; /* groups this group is member of */
81 struct list_head members; /* groups that are members of this group */
82 struct list_head dirty; /* dirty groups */
83 struct rb_node node; /* tree of qgroups */
86 * temp variables for accounting operations
87 * Refer to qgroup_shared_accouting() for details.
93 static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
96 if (qg->old_refcnt < seq)
98 qg->old_refcnt += mod;
101 static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
104 if (qg->new_refcnt < seq)
105 qg->new_refcnt = seq;
106 qg->new_refcnt += mod;
109 static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
111 if (qg->old_refcnt < seq)
113 return qg->old_refcnt - seq;
116 static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
118 if (qg->new_refcnt < seq)
120 return qg->new_refcnt - seq;
124 * glue structure to represent the relations between qgroups.
126 struct btrfs_qgroup_list {
127 struct list_head next_group;
128 struct list_head next_member;
129 struct btrfs_qgroup *group;
130 struct btrfs_qgroup *member;
133 #define ptr_to_u64(x) ((u64)(uintptr_t)x)
134 #define u64_to_ptr(x) ((struct btrfs_qgroup *)(uintptr_t)x)
137 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
139 static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
141 /* must be called with qgroup_ioctl_lock held */
142 static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
145 struct rb_node *n = fs_info->qgroup_tree.rb_node;
146 struct btrfs_qgroup *qgroup;
149 qgroup = rb_entry(n, struct btrfs_qgroup, node);
150 if (qgroup->qgroupid < qgroupid)
152 else if (qgroup->qgroupid > qgroupid)
160 /* must be called with qgroup_lock held */
161 static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
164 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
165 struct rb_node *parent = NULL;
166 struct btrfs_qgroup *qgroup;
170 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
172 if (qgroup->qgroupid < qgroupid)
174 else if (qgroup->qgroupid > qgroupid)
180 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
182 return ERR_PTR(-ENOMEM);
184 qgroup->qgroupid = qgroupid;
185 INIT_LIST_HEAD(&qgroup->groups);
186 INIT_LIST_HEAD(&qgroup->members);
187 INIT_LIST_HEAD(&qgroup->dirty);
189 rb_link_node(&qgroup->node, parent, p);
190 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
195 static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
197 struct btrfs_qgroup_list *list;
199 list_del(&qgroup->dirty);
200 while (!list_empty(&qgroup->groups)) {
201 list = list_first_entry(&qgroup->groups,
202 struct btrfs_qgroup_list, next_group);
203 list_del(&list->next_group);
204 list_del(&list->next_member);
208 while (!list_empty(&qgroup->members)) {
209 list = list_first_entry(&qgroup->members,
210 struct btrfs_qgroup_list, next_member);
211 list_del(&list->next_group);
212 list_del(&list->next_member);
218 /* must be called with qgroup_lock held */
219 static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
221 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
226 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
227 __del_qgroup_rb(qgroup);
231 /* must be called with qgroup_lock held */
232 static int add_relation_rb(struct btrfs_fs_info *fs_info,
233 u64 memberid, u64 parentid)
235 struct btrfs_qgroup *member;
236 struct btrfs_qgroup *parent;
237 struct btrfs_qgroup_list *list;
239 member = find_qgroup_rb(fs_info, memberid);
240 parent = find_qgroup_rb(fs_info, parentid);
241 if (!member || !parent)
244 list = kzalloc(sizeof(*list), GFP_ATOMIC);
248 list->group = parent;
249 list->member = member;
250 list_add_tail(&list->next_group, &member->groups);
251 list_add_tail(&list->next_member, &parent->members);
256 /* must be called with qgroup_lock held */
257 static int del_relation_rb(struct btrfs_fs_info *fs_info,
258 u64 memberid, u64 parentid)
260 struct btrfs_qgroup *member;
261 struct btrfs_qgroup *parent;
262 struct btrfs_qgroup_list *list;
264 member = find_qgroup_rb(fs_info, memberid);
265 parent = find_qgroup_rb(fs_info, parentid);
266 if (!member || !parent)
269 list_for_each_entry(list, &member->groups, next_group) {
270 if (list->group == parent) {
271 list_del(&list->next_group);
272 list_del(&list->next_member);
280 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
281 int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
284 struct btrfs_qgroup *qgroup;
286 qgroup = find_qgroup_rb(fs_info, qgroupid);
289 if (qgroup->rfer != rfer || qgroup->excl != excl)
296 * The full config is read in one go, only called from open_ctree()
297 * It doesn't use any locking, as at this point we're still single-threaded
299 int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
301 struct btrfs_key key;
302 struct btrfs_key found_key;
303 struct btrfs_root *quota_root = fs_info->quota_root;
304 struct btrfs_path *path = NULL;
305 struct extent_buffer *l;
309 u64 rescan_progress = 0;
311 if (!fs_info->quota_enabled)
314 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
315 if (!fs_info->qgroup_ulist) {
320 path = btrfs_alloc_path();
326 /* default this to quota off, in case no status key is found */
327 fs_info->qgroup_flags = 0;
330 * pass 1: read status, all qgroup infos and limits
335 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
340 struct btrfs_qgroup *qgroup;
342 slot = path->slots[0];
344 btrfs_item_key_to_cpu(l, &found_key, slot);
346 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
347 struct btrfs_qgroup_status_item *ptr;
349 ptr = btrfs_item_ptr(l, slot,
350 struct btrfs_qgroup_status_item);
352 if (btrfs_qgroup_status_version(l, ptr) !=
353 BTRFS_QGROUP_STATUS_VERSION) {
355 "old qgroup version, quota disabled");
358 if (btrfs_qgroup_status_generation(l, ptr) !=
359 fs_info->generation) {
360 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
362 "qgroup generation mismatch, "
363 "marked as inconsistent");
365 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
367 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
371 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
372 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
375 qgroup = find_qgroup_rb(fs_info, found_key.offset);
376 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
377 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
378 btrfs_err(fs_info, "inconsitent qgroup config");
379 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
382 qgroup = add_qgroup_rb(fs_info, found_key.offset);
383 if (IS_ERR(qgroup)) {
384 ret = PTR_ERR(qgroup);
388 switch (found_key.type) {
389 case BTRFS_QGROUP_INFO_KEY: {
390 struct btrfs_qgroup_info_item *ptr;
392 ptr = btrfs_item_ptr(l, slot,
393 struct btrfs_qgroup_info_item);
394 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
395 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
396 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
397 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
398 /* generation currently unused */
401 case BTRFS_QGROUP_LIMIT_KEY: {
402 struct btrfs_qgroup_limit_item *ptr;
404 ptr = btrfs_item_ptr(l, slot,
405 struct btrfs_qgroup_limit_item);
406 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
407 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
408 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
409 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
410 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
415 ret = btrfs_next_item(quota_root, path);
421 btrfs_release_path(path);
424 * pass 2: read all qgroup relations
427 key.type = BTRFS_QGROUP_RELATION_KEY;
429 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
433 slot = path->slots[0];
435 btrfs_item_key_to_cpu(l, &found_key, slot);
437 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
440 if (found_key.objectid > found_key.offset) {
441 /* parent <- member, not needed to build config */
442 /* FIXME should we omit the key completely? */
446 ret = add_relation_rb(fs_info, found_key.objectid,
448 if (ret == -ENOENT) {
450 "orphan qgroup relation 0x%llx->0x%llx",
451 found_key.objectid, found_key.offset);
452 ret = 0; /* ignore the error */
457 ret = btrfs_next_item(quota_root, path);
464 fs_info->qgroup_flags |= flags;
465 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
466 fs_info->quota_enabled = 0;
467 fs_info->pending_quota_state = 0;
468 } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
470 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
472 btrfs_free_path(path);
475 ulist_free(fs_info->qgroup_ulist);
476 fs_info->qgroup_ulist = NULL;
477 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
480 return ret < 0 ? ret : 0;
484 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
485 * first two are in single-threaded paths.And for the third one, we have set
486 * quota_root to be null with qgroup_lock held before, so it is safe to clean
487 * up the in-memory structures without qgroup_lock held.
489 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
492 struct btrfs_qgroup *qgroup;
494 while ((n = rb_first(&fs_info->qgroup_tree))) {
495 qgroup = rb_entry(n, struct btrfs_qgroup, node);
496 rb_erase(n, &fs_info->qgroup_tree);
497 __del_qgroup_rb(qgroup);
500 * we call btrfs_free_qgroup_config() when umounting
501 * filesystem and disabling quota, so we set qgroup_ulit
502 * to be null here to avoid double free.
504 ulist_free(fs_info->qgroup_ulist);
505 fs_info->qgroup_ulist = NULL;
508 static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
509 struct btrfs_root *quota_root,
513 struct btrfs_path *path;
514 struct btrfs_key key;
516 path = btrfs_alloc_path();
521 key.type = BTRFS_QGROUP_RELATION_KEY;
524 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
526 btrfs_mark_buffer_dirty(path->nodes[0]);
528 btrfs_free_path(path);
532 static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
533 struct btrfs_root *quota_root,
537 struct btrfs_path *path;
538 struct btrfs_key key;
540 path = btrfs_alloc_path();
545 key.type = BTRFS_QGROUP_RELATION_KEY;
548 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
557 ret = btrfs_del_item(trans, quota_root, path);
559 btrfs_free_path(path);
563 static int add_qgroup_item(struct btrfs_trans_handle *trans,
564 struct btrfs_root *quota_root, u64 qgroupid)
567 struct btrfs_path *path;
568 struct btrfs_qgroup_info_item *qgroup_info;
569 struct btrfs_qgroup_limit_item *qgroup_limit;
570 struct extent_buffer *leaf;
571 struct btrfs_key key;
573 if (btrfs_test_is_dummy_root(quota_root))
576 path = btrfs_alloc_path();
581 key.type = BTRFS_QGROUP_INFO_KEY;
582 key.offset = qgroupid;
585 * Avoid a transaction abort by catching -EEXIST here. In that
586 * case, we proceed by re-initializing the existing structure
590 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
591 sizeof(*qgroup_info));
592 if (ret && ret != -EEXIST)
595 leaf = path->nodes[0];
596 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
597 struct btrfs_qgroup_info_item);
598 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
599 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
600 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
601 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
602 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
604 btrfs_mark_buffer_dirty(leaf);
606 btrfs_release_path(path);
608 key.type = BTRFS_QGROUP_LIMIT_KEY;
609 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
610 sizeof(*qgroup_limit));
611 if (ret && ret != -EEXIST)
614 leaf = path->nodes[0];
615 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
616 struct btrfs_qgroup_limit_item);
617 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
618 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
619 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
620 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
621 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
623 btrfs_mark_buffer_dirty(leaf);
627 btrfs_free_path(path);
631 static int del_qgroup_item(struct btrfs_trans_handle *trans,
632 struct btrfs_root *quota_root, u64 qgroupid)
635 struct btrfs_path *path;
636 struct btrfs_key key;
638 path = btrfs_alloc_path();
643 key.type = BTRFS_QGROUP_INFO_KEY;
644 key.offset = qgroupid;
645 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
654 ret = btrfs_del_item(trans, quota_root, path);
658 btrfs_release_path(path);
660 key.type = BTRFS_QGROUP_LIMIT_KEY;
661 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
670 ret = btrfs_del_item(trans, quota_root, path);
673 btrfs_free_path(path);
677 static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
678 struct btrfs_root *root,
679 struct btrfs_qgroup *qgroup)
681 struct btrfs_path *path;
682 struct btrfs_key key;
683 struct extent_buffer *l;
684 struct btrfs_qgroup_limit_item *qgroup_limit;
689 key.type = BTRFS_QGROUP_LIMIT_KEY;
690 key.offset = qgroup->qgroupid;
692 path = btrfs_alloc_path();
696 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
704 slot = path->slots[0];
705 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
706 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
707 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
708 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
709 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
710 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
712 btrfs_mark_buffer_dirty(l);
715 btrfs_free_path(path);
719 static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
720 struct btrfs_root *root,
721 struct btrfs_qgroup *qgroup)
723 struct btrfs_path *path;
724 struct btrfs_key key;
725 struct extent_buffer *l;
726 struct btrfs_qgroup_info_item *qgroup_info;
730 if (btrfs_test_is_dummy_root(root))
734 key.type = BTRFS_QGROUP_INFO_KEY;
735 key.offset = qgroup->qgroupid;
737 path = btrfs_alloc_path();
741 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
749 slot = path->slots[0];
750 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
751 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
752 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
753 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
754 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
755 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
757 btrfs_mark_buffer_dirty(l);
760 btrfs_free_path(path);
764 static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
765 struct btrfs_fs_info *fs_info,
766 struct btrfs_root *root)
768 struct btrfs_path *path;
769 struct btrfs_key key;
770 struct extent_buffer *l;
771 struct btrfs_qgroup_status_item *ptr;
776 key.type = BTRFS_QGROUP_STATUS_KEY;
779 path = btrfs_alloc_path();
783 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
791 slot = path->slots[0];
792 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
793 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
794 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
795 btrfs_set_qgroup_status_rescan(l, ptr,
796 fs_info->qgroup_rescan_progress.objectid);
798 btrfs_mark_buffer_dirty(l);
801 btrfs_free_path(path);
806 * called with qgroup_lock held
808 static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
809 struct btrfs_root *root)
811 struct btrfs_path *path;
812 struct btrfs_key key;
813 struct extent_buffer *leaf = NULL;
817 path = btrfs_alloc_path();
821 path->leave_spinning = 1;
828 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
831 leaf = path->nodes[0];
832 nr = btrfs_header_nritems(leaf);
836 * delete the leaf one by one
837 * since the whole tree is going
841 ret = btrfs_del_items(trans, root, path, 0, nr);
845 btrfs_release_path(path);
849 root->fs_info->pending_quota_state = 0;
850 btrfs_free_path(path);
854 int btrfs_quota_enable(struct btrfs_trans_handle *trans,
855 struct btrfs_fs_info *fs_info)
857 struct btrfs_root *quota_root;
858 struct btrfs_root *tree_root = fs_info->tree_root;
859 struct btrfs_path *path = NULL;
860 struct btrfs_qgroup_status_item *ptr;
861 struct extent_buffer *leaf;
862 struct btrfs_key key;
863 struct btrfs_key found_key;
864 struct btrfs_qgroup *qgroup = NULL;
868 mutex_lock(&fs_info->qgroup_ioctl_lock);
869 if (fs_info->quota_root) {
870 fs_info->pending_quota_state = 1;
874 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
875 if (!fs_info->qgroup_ulist) {
881 * initially create the quota tree
883 quota_root = btrfs_create_tree(trans, fs_info,
884 BTRFS_QUOTA_TREE_OBJECTID);
885 if (IS_ERR(quota_root)) {
886 ret = PTR_ERR(quota_root);
890 path = btrfs_alloc_path();
897 key.type = BTRFS_QGROUP_STATUS_KEY;
900 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
905 leaf = path->nodes[0];
906 ptr = btrfs_item_ptr(leaf, path->slots[0],
907 struct btrfs_qgroup_status_item);
908 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
909 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
910 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
911 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
912 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
913 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
915 btrfs_mark_buffer_dirty(leaf);
918 key.type = BTRFS_ROOT_REF_KEY;
921 btrfs_release_path(path);
922 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
930 slot = path->slots[0];
931 leaf = path->nodes[0];
932 btrfs_item_key_to_cpu(leaf, &found_key, slot);
934 if (found_key.type == BTRFS_ROOT_REF_KEY) {
935 ret = add_qgroup_item(trans, quota_root,
940 qgroup = add_qgroup_rb(fs_info, found_key.offset);
941 if (IS_ERR(qgroup)) {
942 ret = PTR_ERR(qgroup);
946 ret = btrfs_next_item(tree_root, path);
954 btrfs_release_path(path);
955 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
959 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
960 if (IS_ERR(qgroup)) {
961 ret = PTR_ERR(qgroup);
964 spin_lock(&fs_info->qgroup_lock);
965 fs_info->quota_root = quota_root;
966 fs_info->pending_quota_state = 1;
967 spin_unlock(&fs_info->qgroup_lock);
969 btrfs_free_path(path);
972 free_extent_buffer(quota_root->node);
973 free_extent_buffer(quota_root->commit_root);
978 ulist_free(fs_info->qgroup_ulist);
979 fs_info->qgroup_ulist = NULL;
981 mutex_unlock(&fs_info->qgroup_ioctl_lock);
985 int btrfs_quota_disable(struct btrfs_trans_handle *trans,
986 struct btrfs_fs_info *fs_info)
988 struct btrfs_root *tree_root = fs_info->tree_root;
989 struct btrfs_root *quota_root;
992 mutex_lock(&fs_info->qgroup_ioctl_lock);
993 if (!fs_info->quota_root)
995 spin_lock(&fs_info->qgroup_lock);
996 fs_info->quota_enabled = 0;
997 fs_info->pending_quota_state = 0;
998 quota_root = fs_info->quota_root;
999 fs_info->quota_root = NULL;
1000 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1001 spin_unlock(&fs_info->qgroup_lock);
1003 btrfs_free_qgroup_config(fs_info);
1005 ret = btrfs_clean_quota_tree(trans, quota_root);
1009 ret = btrfs_del_root(trans, tree_root, "a_root->root_key);
1013 list_del("a_root->dirty_list);
1015 btrfs_tree_lock(quota_root->node);
1016 clean_tree_block(trans, tree_root->fs_info, quota_root->node);
1017 btrfs_tree_unlock(quota_root->node);
1018 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1020 free_extent_buffer(quota_root->node);
1021 free_extent_buffer(quota_root->commit_root);
1024 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1028 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1029 struct btrfs_qgroup *qgroup)
1031 if (list_empty(&qgroup->dirty))
1032 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1036 * The easy accounting, if we are adding/removing the only ref for an extent
1037 * then this qgroup and all of the parent qgroups get their refrence and
1038 * exclusive counts adjusted.
1040 * Caller should hold fs_info->qgroup_lock.
1042 static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1043 struct ulist *tmp, u64 ref_root,
1044 u64 num_bytes, int sign)
1046 struct btrfs_qgroup *qgroup;
1047 struct btrfs_qgroup_list *glist;
1048 struct ulist_node *unode;
1049 struct ulist_iterator uiter;
1052 qgroup = find_qgroup_rb(fs_info, ref_root);
1056 qgroup->rfer += sign * num_bytes;
1057 qgroup->rfer_cmpr += sign * num_bytes;
1059 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1060 qgroup->excl += sign * num_bytes;
1061 qgroup->excl_cmpr += sign * num_bytes;
1063 qgroup->reserved -= num_bytes;
1065 qgroup_dirty(fs_info, qgroup);
1067 /* Get all of the parent groups that contain this qgroup */
1068 list_for_each_entry(glist, &qgroup->groups, next_group) {
1069 ret = ulist_add(tmp, glist->group->qgroupid,
1070 ptr_to_u64(glist->group), GFP_ATOMIC);
1075 /* Iterate all of the parents and adjust their reference counts */
1076 ULIST_ITER_INIT(&uiter);
1077 while ((unode = ulist_next(tmp, &uiter))) {
1078 qgroup = u64_to_ptr(unode->aux);
1079 qgroup->rfer += sign * num_bytes;
1080 qgroup->rfer_cmpr += sign * num_bytes;
1081 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1082 qgroup->excl += sign * num_bytes;
1084 qgroup->reserved -= num_bytes;
1085 qgroup->excl_cmpr += sign * num_bytes;
1086 qgroup_dirty(fs_info, qgroup);
1088 /* Add any parents of the parents */
1089 list_for_each_entry(glist, &qgroup->groups, next_group) {
1090 ret = ulist_add(tmp, glist->group->qgroupid,
1091 ptr_to_u64(glist->group), GFP_ATOMIC);
1103 * Quick path for updating qgroup with only excl refs.
1105 * In that case, just update all parent will be enough.
1106 * Or we needs to do a full rescan.
1107 * Caller should also hold fs_info->qgroup_lock.
1109 * Return 0 for quick update, return >0 for need to full rescan
1110 * and mark INCONSISTENT flag.
1111 * Return < 0 for other error.
1113 static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1114 struct ulist *tmp, u64 src, u64 dst,
1117 struct btrfs_qgroup *qgroup;
1121 qgroup = find_qgroup_rb(fs_info, src);
1124 if (qgroup->excl == qgroup->rfer) {
1126 err = __qgroup_excl_accounting(fs_info, tmp, dst,
1127 qgroup->excl, sign);
1135 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1139 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1140 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1142 struct btrfs_root *quota_root;
1143 struct btrfs_qgroup *parent;
1144 struct btrfs_qgroup *member;
1145 struct btrfs_qgroup_list *list;
1149 /* Check the level of src and dst first */
1150 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1153 tmp = ulist_alloc(GFP_NOFS);
1157 mutex_lock(&fs_info->qgroup_ioctl_lock);
1158 quota_root = fs_info->quota_root;
1163 member = find_qgroup_rb(fs_info, src);
1164 parent = find_qgroup_rb(fs_info, dst);
1165 if (!member || !parent) {
1170 /* check if such qgroup relation exist firstly */
1171 list_for_each_entry(list, &member->groups, next_group) {
1172 if (list->group == parent) {
1178 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1182 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1184 del_qgroup_relation_item(trans, quota_root, src, dst);
1188 spin_lock(&fs_info->qgroup_lock);
1189 ret = add_relation_rb(quota_root->fs_info, src, dst);
1191 spin_unlock(&fs_info->qgroup_lock);
1194 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
1195 spin_unlock(&fs_info->qgroup_lock);
1197 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1202 int __del_qgroup_relation(struct btrfs_trans_handle *trans,
1203 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1205 struct btrfs_root *quota_root;
1206 struct btrfs_qgroup *parent;
1207 struct btrfs_qgroup *member;
1208 struct btrfs_qgroup_list *list;
1213 tmp = ulist_alloc(GFP_NOFS);
1217 quota_root = fs_info->quota_root;
1223 member = find_qgroup_rb(fs_info, src);
1224 parent = find_qgroup_rb(fs_info, dst);
1225 if (!member || !parent) {
1230 /* check if such qgroup relation exist firstly */
1231 list_for_each_entry(list, &member->groups, next_group) {
1232 if (list->group == parent)
1238 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1239 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1243 spin_lock(&fs_info->qgroup_lock);
1244 del_relation_rb(fs_info, src, dst);
1245 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
1246 spin_unlock(&fs_info->qgroup_lock);
1252 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1253 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1257 mutex_lock(&fs_info->qgroup_ioctl_lock);
1258 ret = __del_qgroup_relation(trans, fs_info, src, dst);
1259 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1264 int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
1265 struct btrfs_fs_info *fs_info, u64 qgroupid)
1267 struct btrfs_root *quota_root;
1268 struct btrfs_qgroup *qgroup;
1271 mutex_lock(&fs_info->qgroup_ioctl_lock);
1272 quota_root = fs_info->quota_root;
1277 qgroup = find_qgroup_rb(fs_info, qgroupid);
1283 ret = add_qgroup_item(trans, quota_root, qgroupid);
1287 spin_lock(&fs_info->qgroup_lock);
1288 qgroup = add_qgroup_rb(fs_info, qgroupid);
1289 spin_unlock(&fs_info->qgroup_lock);
1292 ret = PTR_ERR(qgroup);
1294 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1298 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1299 struct btrfs_fs_info *fs_info, u64 qgroupid)
1301 struct btrfs_root *quota_root;
1302 struct btrfs_qgroup *qgroup;
1303 struct btrfs_qgroup_list *list;
1306 mutex_lock(&fs_info->qgroup_ioctl_lock);
1307 quota_root = fs_info->quota_root;
1313 qgroup = find_qgroup_rb(fs_info, qgroupid);
1318 /* check if there are no children of this qgroup */
1319 if (!list_empty(&qgroup->members)) {
1324 ret = del_qgroup_item(trans, quota_root, qgroupid);
1326 while (!list_empty(&qgroup->groups)) {
1327 list = list_first_entry(&qgroup->groups,
1328 struct btrfs_qgroup_list, next_group);
1329 ret = __del_qgroup_relation(trans, fs_info,
1331 list->group->qgroupid);
1336 spin_lock(&fs_info->qgroup_lock);
1337 del_qgroup_rb(quota_root->fs_info, qgroupid);
1338 spin_unlock(&fs_info->qgroup_lock);
1340 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1344 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1345 struct btrfs_fs_info *fs_info, u64 qgroupid,
1346 struct btrfs_qgroup_limit *limit)
1348 struct btrfs_root *quota_root;
1349 struct btrfs_qgroup *qgroup;
1352 mutex_lock(&fs_info->qgroup_ioctl_lock);
1353 quota_root = fs_info->quota_root;
1359 qgroup = find_qgroup_rb(fs_info, qgroupid);
1365 spin_lock(&fs_info->qgroup_lock);
1366 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER)
1367 qgroup->max_rfer = limit->max_rfer;
1368 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL)
1369 qgroup->max_excl = limit->max_excl;
1370 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER)
1371 qgroup->rsv_rfer = limit->rsv_rfer;
1372 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL)
1373 qgroup->rsv_excl = limit->rsv_excl;
1374 qgroup->lim_flags |= limit->flags;
1376 spin_unlock(&fs_info->qgroup_lock);
1378 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1380 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1381 btrfs_info(fs_info, "unable to update quota limit for %llu",
1386 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1390 static int comp_oper_exist(struct btrfs_qgroup_operation *oper1,
1391 struct btrfs_qgroup_operation *oper2)
1394 * Ignore seq and type here, we're looking for any operation
1395 * at all related to this extent on that root.
1397 if (oper1->bytenr < oper2->bytenr)
1399 if (oper1->bytenr > oper2->bytenr)
1401 if (oper1->ref_root < oper2->ref_root)
1403 if (oper1->ref_root > oper2->ref_root)
1408 static int qgroup_oper_exists(struct btrfs_fs_info *fs_info,
1409 struct btrfs_qgroup_operation *oper)
1412 struct btrfs_qgroup_operation *cur;
1415 spin_lock(&fs_info->qgroup_op_lock);
1416 n = fs_info->qgroup_op_tree.rb_node;
1418 cur = rb_entry(n, struct btrfs_qgroup_operation, n);
1419 cmp = comp_oper_exist(cur, oper);
1425 spin_unlock(&fs_info->qgroup_op_lock);
1429 spin_unlock(&fs_info->qgroup_op_lock);
1433 static int comp_oper(struct btrfs_qgroup_operation *oper1,
1434 struct btrfs_qgroup_operation *oper2)
1436 if (oper1->bytenr < oper2->bytenr)
1438 if (oper1->bytenr > oper2->bytenr)
1440 if (oper1->ref_root < oper2->ref_root)
1442 if (oper1->ref_root > oper2->ref_root)
1444 if (oper1->seq < oper2->seq)
1446 if (oper1->seq > oper2->seq)
1448 if (oper1->type < oper2->type)
1450 if (oper1->type > oper2->type)
1455 static int insert_qgroup_oper(struct btrfs_fs_info *fs_info,
1456 struct btrfs_qgroup_operation *oper)
1459 struct rb_node *parent = NULL;
1460 struct btrfs_qgroup_operation *cur;
1463 spin_lock(&fs_info->qgroup_op_lock);
1464 p = &fs_info->qgroup_op_tree.rb_node;
1467 cur = rb_entry(parent, struct btrfs_qgroup_operation, n);
1468 cmp = comp_oper(cur, oper);
1470 p = &(*p)->rb_right;
1474 spin_unlock(&fs_info->qgroup_op_lock);
1478 rb_link_node(&oper->n, parent, p);
1479 rb_insert_color(&oper->n, &fs_info->qgroup_op_tree);
1480 spin_unlock(&fs_info->qgroup_op_lock);
1485 * Record a quota operation for processing later on.
1486 * @trans: the transaction we are adding the delayed op to.
1487 * @fs_info: the fs_info for this fs.
1488 * @ref_root: the root of the reference we are acting on,
1489 * @bytenr: the bytenr we are acting on.
1490 * @num_bytes: the number of bytes in the reference.
1491 * @type: the type of operation this is.
1492 * @mod_seq: do we need to get a sequence number for looking up roots.
1494 * We just add it to our trans qgroup_ref_list and carry on and process these
1495 * operations in order at some later point. If the reference root isn't a fs
1496 * root then we don't bother with doing anything.
1498 * MUST BE HOLDING THE REF LOCK.
1500 int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
1501 struct btrfs_fs_info *fs_info, u64 ref_root,
1502 u64 bytenr, u64 num_bytes,
1503 enum btrfs_qgroup_operation_type type, int mod_seq)
1505 struct btrfs_qgroup_operation *oper;
1508 if (!is_fstree(ref_root) || !fs_info->quota_enabled)
1511 oper = kmalloc(sizeof(*oper), GFP_NOFS);
1515 oper->ref_root = ref_root;
1516 oper->bytenr = bytenr;
1517 oper->num_bytes = num_bytes;
1519 oper->seq = atomic_inc_return(&fs_info->qgroup_op_seq);
1520 INIT_LIST_HEAD(&oper->elem.list);
1523 trace_btrfs_qgroup_record_ref(oper);
1525 if (type == BTRFS_QGROUP_OPER_SUB_SUBTREE) {
1527 * If any operation for this bytenr/ref_root combo
1528 * exists, then we know it's not exclusively owned and
1529 * shouldn't be queued up.
1531 * This also catches the case where we have a cloned
1532 * extent that gets queued up multiple times during
1535 if (qgroup_oper_exists(fs_info, oper)) {
1541 ret = insert_qgroup_oper(fs_info, oper);
1543 /* Shouldn't happen so have an assert for developers */
1548 list_add_tail(&oper->list, &trans->qgroup_ref_list);
1551 btrfs_get_tree_mod_seq(fs_info, &oper->elem);
1556 struct btrfs_qgroup_extent_record
1557 *btrfs_qgroup_insert_dirty_extent(struct btrfs_delayed_ref_root *delayed_refs,
1558 struct btrfs_qgroup_extent_record *record)
1560 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1561 struct rb_node *parent_node = NULL;
1562 struct btrfs_qgroup_extent_record *entry;
1563 u64 bytenr = record->bytenr;
1567 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1569 if (bytenr < entry->bytenr)
1571 else if (bytenr > entry->bytenr)
1572 p = &(*p)->rb_right;
1577 rb_link_node(&record->node, parent_node, p);
1578 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
1583 * The easy accounting, if we are adding/removing the only ref for an extent
1584 * then this qgroup and all of the parent qgroups get their refrence and
1585 * exclusive counts adjusted.
1587 static int qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1588 struct btrfs_qgroup_operation *oper)
1594 tmp = ulist_alloc(GFP_NOFS);
1598 spin_lock(&fs_info->qgroup_lock);
1599 if (!fs_info->quota_root)
1602 switch (oper->type) {
1603 case BTRFS_QGROUP_OPER_ADD_EXCL:
1606 case BTRFS_QGROUP_OPER_SUB_EXCL:
1612 ret = __qgroup_excl_accounting(fs_info, tmp, oper->ref_root,
1613 oper->num_bytes, sign);
1615 spin_unlock(&fs_info->qgroup_lock);
1621 * Walk all of the roots that pointed to our bytenr and adjust their refcnts as
1624 static int qgroup_calc_old_refcnt(struct btrfs_fs_info *fs_info,
1625 u64 root_to_skip, struct ulist *tmp,
1626 struct ulist *roots, struct ulist *qgroups,
1627 u64 seq, int *old_roots, int rescan)
1629 struct ulist_node *unode;
1630 struct ulist_iterator uiter;
1631 struct ulist_node *tmp_unode;
1632 struct ulist_iterator tmp_uiter;
1633 struct btrfs_qgroup *qg;
1636 ULIST_ITER_INIT(&uiter);
1637 while ((unode = ulist_next(roots, &uiter))) {
1638 /* We don't count our current root here */
1639 if (unode->val == root_to_skip)
1641 qg = find_qgroup_rb(fs_info, unode->val);
1645 * We could have a pending removal of this same ref so we may
1646 * not have actually found our ref root when doing
1647 * btrfs_find_all_roots, so we need to keep track of how many
1648 * old roots we find in case we removed ours and added a
1649 * different one at the same time. I don't think this could
1650 * happen in practice but that sort of thinking leads to pain
1651 * and suffering and to the dark side.
1656 ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
1660 ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC);
1663 ULIST_ITER_INIT(&tmp_uiter);
1664 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1665 struct btrfs_qgroup_list *glist;
1668 qg = u64_to_ptr(tmp_unode->aux);
1670 * We use this sequence number to keep from having to
1671 * run the whole list and 0 out the refcnt every time.
1672 * We basically use sequnce as the known 0 count and
1673 * then add 1 everytime we see a qgroup. This is how we
1674 * get how many of the roots actually point up to the
1675 * upper level qgroups in order to determine exclusive
1678 * For rescan none of the extent is recorded before so
1679 * we just don't add old_refcnt.
1685 btrfs_qgroup_update_old_refcnt(qg, seq, mod);
1686 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1687 list_for_each_entry(glist, &qg->groups, next_group) {
1688 ret = ulist_add(qgroups, glist->group->qgroupid,
1689 ptr_to_u64(glist->group),
1693 ret = ulist_add(tmp, glist->group->qgroupid,
1694 ptr_to_u64(glist->group),
1705 * We need to walk forward in our operation tree and account for any roots that
1706 * were deleted after we made this operation.
1708 static int qgroup_account_deleted_refs(struct btrfs_fs_info *fs_info,
1709 struct btrfs_qgroup_operation *oper,
1711 struct ulist *qgroups, u64 seq,
1714 struct ulist_node *unode;
1715 struct ulist_iterator uiter;
1716 struct btrfs_qgroup *qg;
1717 struct btrfs_qgroup_operation *tmp_oper;
1724 * We only walk forward in the tree since we're only interested in
1725 * removals that happened _after_ our operation.
1727 spin_lock(&fs_info->qgroup_op_lock);
1728 n = rb_next(&oper->n);
1729 spin_unlock(&fs_info->qgroup_op_lock);
1732 tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
1733 while (tmp_oper->bytenr == oper->bytenr) {
1735 * If it's not a removal we don't care, additions work out
1736 * properly with our refcnt tracking.
1738 if (tmp_oper->type != BTRFS_QGROUP_OPER_SUB_SHARED &&
1739 tmp_oper->type != BTRFS_QGROUP_OPER_SUB_EXCL)
1741 qg = find_qgroup_rb(fs_info, tmp_oper->ref_root);
1744 ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
1750 * We only want to increase old_roots if this qgroup is
1751 * not already in the list of qgroups. If it is already
1752 * there then that means it must have been re-added or
1753 * the delete will be discarded because we had an
1754 * existing ref that we haven't looked up yet. In this
1755 * case we don't want to increase old_roots. So if ret
1756 * == 1 then we know that this is the first time we've
1757 * seen this qgroup and we can bump the old_roots.
1760 ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg),
1766 spin_lock(&fs_info->qgroup_op_lock);
1767 n = rb_next(&tmp_oper->n);
1768 spin_unlock(&fs_info->qgroup_op_lock);
1771 tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
1774 /* Ok now process the qgroups we found */
1775 ULIST_ITER_INIT(&uiter);
1776 while ((unode = ulist_next(tmp, &uiter))) {
1777 struct btrfs_qgroup_list *glist;
1779 qg = u64_to_ptr(unode->aux);
1780 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1781 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1782 list_for_each_entry(glist, &qg->groups, next_group) {
1783 ret = ulist_add(qgroups, glist->group->qgroupid,
1784 ptr_to_u64(glist->group), GFP_ATOMIC);
1787 ret = ulist_add(tmp, glist->group->qgroupid,
1788 ptr_to_u64(glist->group), GFP_ATOMIC);
1796 /* Add refcnt for the newly added reference. */
1797 static int qgroup_calc_new_refcnt(struct btrfs_fs_info *fs_info,
1798 struct btrfs_qgroup_operation *oper,
1799 struct btrfs_qgroup *qgroup,
1800 struct ulist *tmp, struct ulist *qgroups,
1803 struct ulist_node *unode;
1804 struct ulist_iterator uiter;
1805 struct btrfs_qgroup *qg;
1809 ret = ulist_add(qgroups, qgroup->qgroupid, ptr_to_u64(qgroup),
1813 ret = ulist_add(tmp, qgroup->qgroupid, ptr_to_u64(qgroup),
1817 ULIST_ITER_INIT(&uiter);
1818 while ((unode = ulist_next(tmp, &uiter))) {
1819 struct btrfs_qgroup_list *glist;
1821 qg = u64_to_ptr(unode->aux);
1822 if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED)
1823 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1825 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1826 list_for_each_entry(glist, &qg->groups, next_group) {
1827 ret = ulist_add(tmp, glist->group->qgroupid,
1828 ptr_to_u64(glist->group), GFP_ATOMIC);
1831 ret = ulist_add(qgroups, glist->group->qgroupid,
1832 ptr_to_u64(glist->group), GFP_ATOMIC);
1840 #define UPDATE_NEW 0
1841 #define UPDATE_OLD 1
1843 * Walk all of the roots that points to the bytenr and adjust their refcnts.
1845 static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
1846 struct ulist *roots, struct ulist *tmp,
1847 struct ulist *qgroups, u64 seq, int update_old)
1849 struct ulist_node *unode;
1850 struct ulist_iterator uiter;
1851 struct ulist_node *tmp_unode;
1852 struct ulist_iterator tmp_uiter;
1853 struct btrfs_qgroup *qg;
1858 ULIST_ITER_INIT(&uiter);
1859 while ((unode = ulist_next(roots, &uiter))) {
1860 qg = find_qgroup_rb(fs_info, unode->val);
1865 ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
1869 ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC);
1872 ULIST_ITER_INIT(&tmp_uiter);
1873 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1874 struct btrfs_qgroup_list *glist;
1876 qg = u64_to_ptr(tmp_unode->aux);
1878 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1880 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1881 list_for_each_entry(glist, &qg->groups, next_group) {
1882 ret = ulist_add(qgroups, glist->group->qgroupid,
1883 ptr_to_u64(glist->group),
1887 ret = ulist_add(tmp, glist->group->qgroupid,
1888 ptr_to_u64(glist->group),
1899 * Update qgroup rfer/excl counters.
1900 * Rfer update is easy, codes can explain themselves.
1901 * Excl update is tricky, the update is split into 2 part.
1902 * Part 1: Possible exclusive <-> sharing detect:
1904 * -------------------------------------
1906 * -------------------------------------
1908 * -------------------------------------
1911 * A: cur_old_roots < nr_old_roots (not exclusive before)
1912 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
1913 * B: cur_new_roots < nr_new_roots (not exclusive now)
1914 * !B: cur_new_roots == nr_new_roots (possible exclsuive now)
1917 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
1918 * *: Definitely not changed. **: Possible unchanged.
1920 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
1922 * To make the logic clear, we first use condition A and B to split
1923 * combination into 4 results.
1925 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
1926 * only on variant maybe 0.
1928 * Lastly, check result **, since there are 2 variants maybe 0, split them
1930 * But this time we don't need to consider other things, the codes and logic
1931 * is easy to understand now.
1933 static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
1934 struct ulist *qgroups,
1937 u64 num_bytes, u64 seq)
1939 struct ulist_node *unode;
1940 struct ulist_iterator uiter;
1941 struct btrfs_qgroup *qg;
1942 u64 cur_new_count, cur_old_count;
1944 ULIST_ITER_INIT(&uiter);
1945 while ((unode = ulist_next(qgroups, &uiter))) {
1948 qg = u64_to_ptr(unode->aux);
1949 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
1950 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
1952 /* Rfer update part */
1953 if (cur_old_count == 0 && cur_new_count > 0) {
1954 qg->rfer += num_bytes;
1955 qg->rfer_cmpr += num_bytes;
1958 if (cur_old_count > 0 && cur_new_count == 0) {
1959 qg->rfer -= num_bytes;
1960 qg->rfer_cmpr -= num_bytes;
1964 /* Excl update part */
1965 /* Exclusive/none -> shared case */
1966 if (cur_old_count == nr_old_roots &&
1967 cur_new_count < nr_new_roots) {
1968 /* Exclusive -> shared */
1969 if (cur_old_count != 0) {
1970 qg->excl -= num_bytes;
1971 qg->excl_cmpr -= num_bytes;
1976 /* Shared -> exclusive/none case */
1977 if (cur_old_count < nr_old_roots &&
1978 cur_new_count == nr_new_roots) {
1979 /* Shared->exclusive */
1980 if (cur_new_count != 0) {
1981 qg->excl += num_bytes;
1982 qg->excl_cmpr += num_bytes;
1987 /* Exclusive/none -> exclusive/none case */
1988 if (cur_old_count == nr_old_roots &&
1989 cur_new_count == nr_new_roots) {
1990 if (cur_old_count == 0) {
1991 /* None -> exclusive/none */
1993 if (cur_new_count != 0) {
1994 /* None -> exclusive */
1995 qg->excl += num_bytes;
1996 qg->excl_cmpr += num_bytes;
1999 /* None -> none, nothing changed */
2001 /* Exclusive -> exclusive/none */
2003 if (cur_new_count == 0) {
2004 /* Exclusive -> none */
2005 qg->excl -= num_bytes;
2006 qg->excl_cmpr -= num_bytes;
2009 /* Exclusive -> exclusive, nothing changed */
2013 qgroup_dirty(fs_info, qg);
2019 * This adjusts the counters for all referenced qgroups if need be.
2021 static int qgroup_adjust_counters(struct btrfs_fs_info *fs_info,
2022 u64 root_to_skip, u64 num_bytes,
2023 struct ulist *qgroups, u64 seq,
2024 int old_roots, int new_roots, int rescan)
2026 struct ulist_node *unode;
2027 struct ulist_iterator uiter;
2028 struct btrfs_qgroup *qg;
2029 u64 cur_new_count, cur_old_count;
2031 ULIST_ITER_INIT(&uiter);
2032 while ((unode = ulist_next(qgroups, &uiter))) {
2035 qg = u64_to_ptr(unode->aux);
2036 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
2037 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
2040 * Wasn't referenced before but is now, add to the reference
2043 if (cur_old_count == 0 && cur_new_count > 0) {
2044 qg->rfer += num_bytes;
2045 qg->rfer_cmpr += num_bytes;
2050 * Was referenced before but isn't now, subtract from the
2051 * reference counters.
2053 if (cur_old_count > 0 && cur_new_count == 0) {
2054 qg->rfer -= num_bytes;
2055 qg->rfer_cmpr -= num_bytes;
2060 * If our refcount was the same as the roots previously but our
2061 * new count isn't the same as the number of roots now then we
2062 * went from having a exclusive reference on this range to not.
2064 if (old_roots && cur_old_count == old_roots &&
2065 (cur_new_count != new_roots || new_roots == 0)) {
2066 WARN_ON(cur_new_count != new_roots && new_roots == 0);
2067 qg->excl -= num_bytes;
2068 qg->excl_cmpr -= num_bytes;
2073 * If we didn't reference all the roots before but now we do we
2074 * have an exclusive reference to this range.
2076 if ((!old_roots || (old_roots && cur_old_count != old_roots))
2077 && cur_new_count == new_roots) {
2078 qg->excl += num_bytes;
2079 qg->excl_cmpr += num_bytes;
2084 qgroup_dirty(fs_info, qg);
2090 * If we removed a data extent and there were other references for that bytenr
2091 * then we need to lookup all referenced roots to make sure we still don't
2092 * reference this bytenr. If we do then we can just discard this operation.
2094 static int check_existing_refs(struct btrfs_trans_handle *trans,
2095 struct btrfs_fs_info *fs_info,
2096 struct btrfs_qgroup_operation *oper)
2098 struct ulist *roots = NULL;
2099 struct ulist_node *unode;
2100 struct ulist_iterator uiter;
2103 ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
2104 oper->elem.seq, &roots);
2109 ULIST_ITER_INIT(&uiter);
2110 while ((unode = ulist_next(roots, &uiter))) {
2111 if (unode->val == oper->ref_root) {
2117 btrfs_put_tree_mod_seq(fs_info, &oper->elem);
2123 * If we share a reference across multiple roots then we may need to adjust
2124 * various qgroups referenced and exclusive counters. The basic premise is this
2126 * 1) We have seq to represent a 0 count. Instead of looping through all of the
2127 * qgroups and resetting their refcount to 0 we just constantly bump this
2128 * sequence number to act as the base reference count. This means that if
2129 * anybody is equal to or below this sequence they were never referenced. We
2130 * jack this sequence up by the number of roots we found each time in order to
2131 * make sure we don't have any overlap.
2133 * 2) We first search all the roots that reference the area _except_ the root
2134 * we're acting on currently. This makes up the old_refcnt of all the qgroups
2137 * 3) We walk all of the qgroups referenced by the root we are currently acting
2138 * on, and will either adjust old_refcnt in the case of a removal or the
2139 * new_refcnt in the case of an addition.
2141 * 4) Finally we walk all the qgroups that are referenced by this range
2142 * including the root we are acting on currently. We will adjust the counters
2143 * based on the number of roots we had and will have after this operation.
2145 * Take this example as an illustration
2149 * [qg 0/0] [qg 0/1] [qg 0/2]
2153 * Say we are adding a reference that is covered by qg 0/0. The first step
2154 * would give a refcnt of 1 to qg 0/1 and 0/2 and a refcnt of 2 to qg 1/0 with
2155 * old_roots being 2. Because it is adding new_roots will be 1. We then go
2156 * through qg 0/0 which will get the new_refcnt set to 1 and add 1 to qg 1/0's
2157 * new_refcnt, bringing it to 3. We then walk through all of the qgroups, we
2158 * notice that the old refcnt for qg 0/0 < the new refcnt, so we added a
2159 * reference and thus must add the size to the referenced bytes. Everything
2160 * else is the same so nothing else changes.
2162 static int qgroup_shared_accounting(struct btrfs_trans_handle *trans,
2163 struct btrfs_fs_info *fs_info,
2164 struct btrfs_qgroup_operation *oper)
2166 struct ulist *roots = NULL;
2167 struct ulist *qgroups, *tmp;
2168 struct btrfs_qgroup *qgroup;
2169 struct seq_list elem = SEQ_LIST_INIT(elem);
2175 if (oper->elem.seq) {
2176 ret = check_existing_refs(trans, fs_info, oper);
2183 qgroups = ulist_alloc(GFP_NOFS);
2187 tmp = ulist_alloc(GFP_NOFS);
2189 ulist_free(qgroups);
2193 btrfs_get_tree_mod_seq(fs_info, &elem);
2194 ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, elem.seq,
2196 btrfs_put_tree_mod_seq(fs_info, &elem);
2198 ulist_free(qgroups);
2202 spin_lock(&fs_info->qgroup_lock);
2203 qgroup = find_qgroup_rb(fs_info, oper->ref_root);
2206 seq = fs_info->qgroup_seq;
2209 * So roots is the list of all the roots currently pointing at the
2210 * bytenr, including the ref we are adding if we are adding, or not if
2211 * we are removing a ref. So we pass in the ref_root to skip that root
2212 * in our calculations. We set old_refnct and new_refcnt cause who the
2213 * hell knows what everything looked like before, and it doesn't matter
2216 ret = qgroup_calc_old_refcnt(fs_info, oper->ref_root, tmp, roots, qgroups,
2217 seq, &old_roots, 0);
2222 * Now adjust the refcounts of the qgroups that care about this
2223 * reference, either the old_count in the case of removal or new_count
2224 * in the case of an addition.
2226 ret = qgroup_calc_new_refcnt(fs_info, oper, qgroup, tmp, qgroups,
2232 * ...in the case of removals. If we had a removal before we got around
2233 * to processing this operation then we need to find that guy and count
2234 * his references as if they really existed so we don't end up screwing
2235 * up the exclusive counts. Then whenever we go to process the delete
2236 * everything will be grand and we can account for whatever exclusive
2237 * changes need to be made there. We also have to pass in old_roots so
2238 * we have an accurate count of the roots as it pertains to this
2239 * operations view of the world.
2241 ret = qgroup_account_deleted_refs(fs_info, oper, tmp, qgroups, seq,
2247 * We are adding our root, need to adjust up the number of roots,
2248 * otherwise old_roots is the number of roots we want.
2250 if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
2251 new_roots = old_roots + 1;
2253 new_roots = old_roots;
2258 * Bump qgroup_seq to avoid seq overlap
2259 * XXX: This makes qgroup_seq mismatch with oper->seq.
2261 fs_info->qgroup_seq += old_roots + 1;
2265 * And now the magic happens, bless Arne for having a pretty elegant
2266 * solution for this.
2268 qgroup_adjust_counters(fs_info, oper->ref_root, oper->num_bytes,
2269 qgroups, seq, old_roots, new_roots, 0);
2271 spin_unlock(&fs_info->qgroup_lock);
2272 ulist_free(qgroups);
2279 * Process a reference to a shared subtree. This type of operation is
2280 * queued during snapshot removal when we encounter extents which are
2281 * shared between more than one root.
2283 static int qgroup_subtree_accounting(struct btrfs_trans_handle *trans,
2284 struct btrfs_fs_info *fs_info,
2285 struct btrfs_qgroup_operation *oper)
2287 struct ulist *roots = NULL;
2288 struct ulist_node *unode;
2289 struct ulist_iterator uiter;
2290 struct btrfs_qgroup_list *glist;
2291 struct ulist *parents;
2294 struct btrfs_qgroup *qg;
2296 struct seq_list elem = SEQ_LIST_INIT(elem);
2298 parents = ulist_alloc(GFP_NOFS);
2302 btrfs_get_tree_mod_seq(fs_info, &elem);
2303 ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
2305 btrfs_put_tree_mod_seq(fs_info, &elem);
2309 if (roots->nnodes != 1)
2312 ULIST_ITER_INIT(&uiter);
2313 unode = ulist_next(roots, &uiter); /* Only want 1 so no need to loop */
2315 * If we find our ref root then that means all refs
2316 * this extent has to the root have not yet been
2317 * deleted. In that case, we do nothing and let the
2318 * last ref for this bytenr drive our update.
2320 * This can happen for example if an extent is
2321 * referenced multiple times in a snapshot (clone,
2322 * etc). If we are in the middle of snapshot removal,
2323 * queued updates for such an extent will find the
2324 * root if we have not yet finished removing the
2327 if (unode->val == oper->ref_root)
2330 root_obj = unode->val;
2333 spin_lock(&fs_info->qgroup_lock);
2334 qg = find_qgroup_rb(fs_info, root_obj);
2338 qg->excl += oper->num_bytes;
2339 qg->excl_cmpr += oper->num_bytes;
2340 qgroup_dirty(fs_info, qg);
2343 * Adjust counts for parent groups. First we find all
2344 * parents, then in the 2nd loop we do the adjustment
2345 * while adding parents of the parents to our ulist.
2347 list_for_each_entry(glist, &qg->groups, next_group) {
2348 err = ulist_add(parents, glist->group->qgroupid,
2349 ptr_to_u64(glist->group), GFP_ATOMIC);
2356 ULIST_ITER_INIT(&uiter);
2357 while ((unode = ulist_next(parents, &uiter))) {
2358 qg = u64_to_ptr(unode->aux);
2359 qg->excl += oper->num_bytes;
2360 qg->excl_cmpr += oper->num_bytes;
2361 qgroup_dirty(fs_info, qg);
2363 /* Add any parents of the parents */
2364 list_for_each_entry(glist, &qg->groups, next_group) {
2365 err = ulist_add(parents, glist->group->qgroupid,
2366 ptr_to_u64(glist->group), GFP_ATOMIC);
2375 spin_unlock(&fs_info->qgroup_lock);
2379 ulist_free(parents);
2384 * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
2385 * from the fs. First, all roots referencing the extent are searched, and
2386 * then the space is accounted accordingly to the different roots. The
2387 * accounting algorithm works in 3 steps documented inline.
2389 static int btrfs_qgroup_account(struct btrfs_trans_handle *trans,
2390 struct btrfs_fs_info *fs_info,
2391 struct btrfs_qgroup_operation *oper)
2395 if (!fs_info->quota_enabled)
2398 BUG_ON(!fs_info->quota_root);
2400 mutex_lock(&fs_info->qgroup_rescan_lock);
2401 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2402 if (fs_info->qgroup_rescan_progress.objectid <= oper->bytenr) {
2403 mutex_unlock(&fs_info->qgroup_rescan_lock);
2407 mutex_unlock(&fs_info->qgroup_rescan_lock);
2409 ASSERT(is_fstree(oper->ref_root));
2411 trace_btrfs_qgroup_account(oper);
2413 switch (oper->type) {
2414 case BTRFS_QGROUP_OPER_ADD_EXCL:
2415 case BTRFS_QGROUP_OPER_SUB_EXCL:
2416 ret = qgroup_excl_accounting(fs_info, oper);
2418 case BTRFS_QGROUP_OPER_ADD_SHARED:
2419 case BTRFS_QGROUP_OPER_SUB_SHARED:
2420 ret = qgroup_shared_accounting(trans, fs_info, oper);
2422 case BTRFS_QGROUP_OPER_SUB_SUBTREE:
2423 ret = qgroup_subtree_accounting(trans, fs_info, oper);
2432 * Needs to be called everytime we run delayed refs, even if there is an error
2433 * in order to cleanup outstanding operations.
2435 int btrfs_delayed_qgroup_accounting(struct btrfs_trans_handle *trans,
2436 struct btrfs_fs_info *fs_info)
2438 struct btrfs_qgroup_operation *oper;
2441 while (!list_empty(&trans->qgroup_ref_list)) {
2442 oper = list_first_entry(&trans->qgroup_ref_list,
2443 struct btrfs_qgroup_operation, list);
2444 list_del_init(&oper->list);
2445 if (!ret || !trans->aborted)
2446 ret = btrfs_qgroup_account(trans, fs_info, oper);
2447 spin_lock(&fs_info->qgroup_op_lock);
2448 rb_erase(&oper->n, &fs_info->qgroup_op_tree);
2449 spin_unlock(&fs_info->qgroup_op_lock);
2450 btrfs_put_tree_mod_seq(fs_info, &oper->elem);
2457 * called from commit_transaction. Writes all changed qgroups to disk.
2459 int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2460 struct btrfs_fs_info *fs_info)
2462 struct btrfs_root *quota_root = fs_info->quota_root;
2464 int start_rescan_worker = 0;
2469 if (!fs_info->quota_enabled && fs_info->pending_quota_state)
2470 start_rescan_worker = 1;
2472 fs_info->quota_enabled = fs_info->pending_quota_state;
2474 spin_lock(&fs_info->qgroup_lock);
2475 while (!list_empty(&fs_info->dirty_qgroups)) {
2476 struct btrfs_qgroup *qgroup;
2477 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2478 struct btrfs_qgroup, dirty);
2479 list_del_init(&qgroup->dirty);
2480 spin_unlock(&fs_info->qgroup_lock);
2481 ret = update_qgroup_info_item(trans, quota_root, qgroup);
2483 fs_info->qgroup_flags |=
2484 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2485 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
2487 fs_info->qgroup_flags |=
2488 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2489 spin_lock(&fs_info->qgroup_lock);
2491 if (fs_info->quota_enabled)
2492 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2494 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2495 spin_unlock(&fs_info->qgroup_lock);
2497 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2499 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2501 if (!ret && start_rescan_worker) {
2502 ret = qgroup_rescan_init(fs_info, 0, 1);
2504 qgroup_rescan_zero_tracking(fs_info);
2505 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2506 &fs_info->qgroup_rescan_work);
2517 * copy the acounting information between qgroups. This is necessary when a
2518 * snapshot or a subvolume is created
2520 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2521 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2522 struct btrfs_qgroup_inherit *inherit)
2527 struct btrfs_root *quota_root = fs_info->quota_root;
2528 struct btrfs_qgroup *srcgroup;
2529 struct btrfs_qgroup *dstgroup;
2533 mutex_lock(&fs_info->qgroup_ioctl_lock);
2534 if (!fs_info->quota_enabled)
2543 i_qgroups = (u64 *)(inherit + 1);
2544 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2545 2 * inherit->num_excl_copies;
2546 for (i = 0; i < nums; ++i) {
2547 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
2553 if ((srcgroup->qgroupid >> 48) <= (objectid >> 48)) {
2562 * create a tracking group for the subvol itself
2564 ret = add_qgroup_item(trans, quota_root, objectid);
2569 struct btrfs_root *srcroot;
2570 struct btrfs_key srckey;
2572 srckey.objectid = srcid;
2573 srckey.type = BTRFS_ROOT_ITEM_KEY;
2574 srckey.offset = (u64)-1;
2575 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2576 if (IS_ERR(srcroot)) {
2577 ret = PTR_ERR(srcroot);
2582 level_size = srcroot->nodesize;
2587 * add qgroup to all inherited groups
2590 i_qgroups = (u64 *)(inherit + 1);
2591 for (i = 0; i < inherit->num_qgroups; ++i) {
2592 ret = add_qgroup_relation_item(trans, quota_root,
2593 objectid, *i_qgroups);
2596 ret = add_qgroup_relation_item(trans, quota_root,
2597 *i_qgroups, objectid);
2605 spin_lock(&fs_info->qgroup_lock);
2607 dstgroup = add_qgroup_rb(fs_info, objectid);
2608 if (IS_ERR(dstgroup)) {
2609 ret = PTR_ERR(dstgroup);
2613 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
2614 dstgroup->lim_flags = inherit->lim.flags;
2615 dstgroup->max_rfer = inherit->lim.max_rfer;
2616 dstgroup->max_excl = inherit->lim.max_excl;
2617 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2618 dstgroup->rsv_excl = inherit->lim.rsv_excl;
2620 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
2622 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2623 btrfs_info(fs_info, "unable to update quota limit for %llu",
2624 dstgroup->qgroupid);
2630 srcgroup = find_qgroup_rb(fs_info, srcid);
2635 * We call inherit after we clone the root in order to make sure
2636 * our counts don't go crazy, so at this point the only
2637 * difference between the two roots should be the root node.
2639 dstgroup->rfer = srcgroup->rfer;
2640 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2641 dstgroup->excl = level_size;
2642 dstgroup->excl_cmpr = level_size;
2643 srcgroup->excl = level_size;
2644 srcgroup->excl_cmpr = level_size;
2646 /* inherit the limit info */
2647 dstgroup->lim_flags = srcgroup->lim_flags;
2648 dstgroup->max_rfer = srcgroup->max_rfer;
2649 dstgroup->max_excl = srcgroup->max_excl;
2650 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2651 dstgroup->rsv_excl = srcgroup->rsv_excl;
2653 qgroup_dirty(fs_info, dstgroup);
2654 qgroup_dirty(fs_info, srcgroup);
2660 i_qgroups = (u64 *)(inherit + 1);
2661 for (i = 0; i < inherit->num_qgroups; ++i) {
2662 ret = add_relation_rb(quota_root->fs_info, objectid,
2669 for (i = 0; i < inherit->num_ref_copies; ++i) {
2670 struct btrfs_qgroup *src;
2671 struct btrfs_qgroup *dst;
2673 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2674 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2681 dst->rfer = src->rfer - level_size;
2682 dst->rfer_cmpr = src->rfer_cmpr - level_size;
2685 for (i = 0; i < inherit->num_excl_copies; ++i) {
2686 struct btrfs_qgroup *src;
2687 struct btrfs_qgroup *dst;
2689 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2690 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2697 dst->excl = src->excl + level_size;
2698 dst->excl_cmpr = src->excl_cmpr + level_size;
2703 spin_unlock(&fs_info->qgroup_lock);
2705 mutex_unlock(&fs_info->qgroup_ioctl_lock);
2709 int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
2711 struct btrfs_root *quota_root;
2712 struct btrfs_qgroup *qgroup;
2713 struct btrfs_fs_info *fs_info = root->fs_info;
2714 u64 ref_root = root->root_key.objectid;
2716 struct ulist_node *unode;
2717 struct ulist_iterator uiter;
2719 if (!is_fstree(ref_root))
2725 spin_lock(&fs_info->qgroup_lock);
2726 quota_root = fs_info->quota_root;
2730 qgroup = find_qgroup_rb(fs_info, ref_root);
2735 * in a first step, we check all affected qgroups if any limits would
2738 ulist_reinit(fs_info->qgroup_ulist);
2739 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
2740 (uintptr_t)qgroup, GFP_ATOMIC);
2743 ULIST_ITER_INIT(&uiter);
2744 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2745 struct btrfs_qgroup *qg;
2746 struct btrfs_qgroup_list *glist;
2748 qg = u64_to_ptr(unode->aux);
2750 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
2751 qg->reserved + (s64)qg->rfer + num_bytes >
2757 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
2758 qg->reserved + (s64)qg->excl + num_bytes >
2764 list_for_each_entry(glist, &qg->groups, next_group) {
2765 ret = ulist_add(fs_info->qgroup_ulist,
2766 glist->group->qgroupid,
2767 (uintptr_t)glist->group, GFP_ATOMIC);
2774 * no limits exceeded, now record the reservation into all qgroups
2776 ULIST_ITER_INIT(&uiter);
2777 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2778 struct btrfs_qgroup *qg;
2780 qg = u64_to_ptr(unode->aux);
2782 qg->reserved += num_bytes;
2786 spin_unlock(&fs_info->qgroup_lock);
2790 void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
2792 struct btrfs_root *quota_root;
2793 struct btrfs_qgroup *qgroup;
2794 struct btrfs_fs_info *fs_info = root->fs_info;
2795 struct ulist_node *unode;
2796 struct ulist_iterator uiter;
2797 u64 ref_root = root->root_key.objectid;
2800 if (!is_fstree(ref_root))
2806 spin_lock(&fs_info->qgroup_lock);
2808 quota_root = fs_info->quota_root;
2812 qgroup = find_qgroup_rb(fs_info, ref_root);
2816 ulist_reinit(fs_info->qgroup_ulist);
2817 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
2818 (uintptr_t)qgroup, GFP_ATOMIC);
2821 ULIST_ITER_INIT(&uiter);
2822 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2823 struct btrfs_qgroup *qg;
2824 struct btrfs_qgroup_list *glist;
2826 qg = u64_to_ptr(unode->aux);
2828 qg->reserved -= num_bytes;
2830 list_for_each_entry(glist, &qg->groups, next_group) {
2831 ret = ulist_add(fs_info->qgroup_ulist,
2832 glist->group->qgroupid,
2833 (uintptr_t)glist->group, GFP_ATOMIC);
2840 spin_unlock(&fs_info->qgroup_lock);
2843 void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
2845 if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
2847 btrfs_err(trans->root->fs_info,
2848 "qgroups not uptodate in trans handle %p: list is%s empty, "
2850 trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
2851 (u32)(trans->delayed_ref_elem.seq >> 32),
2852 (u32)trans->delayed_ref_elem.seq);
2857 * returns < 0 on error, 0 when more leafs are to be scanned.
2858 * returns 1 when done.
2861 qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
2862 struct btrfs_trans_handle *trans, struct ulist *qgroups,
2863 struct ulist *tmp, struct extent_buffer *scratch_leaf)
2865 struct btrfs_key found;
2866 struct ulist *roots = NULL;
2867 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
2874 path->leave_spinning = 1;
2875 mutex_lock(&fs_info->qgroup_rescan_lock);
2876 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2877 &fs_info->qgroup_rescan_progress,
2880 pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
2881 fs_info->qgroup_rescan_progress.objectid,
2882 fs_info->qgroup_rescan_progress.type,
2883 fs_info->qgroup_rescan_progress.offset, ret);
2887 * The rescan is about to end, we will not be scanning any
2888 * further blocks. We cannot unset the RESCAN flag here, because
2889 * we want to commit the transaction if everything went well.
2890 * To make the live accounting work in this phase, we set our
2891 * scan progress pointer such that every real extent objectid
2894 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2895 btrfs_release_path(path);
2896 mutex_unlock(&fs_info->qgroup_rescan_lock);
2900 btrfs_item_key_to_cpu(path->nodes[0], &found,
2901 btrfs_header_nritems(path->nodes[0]) - 1);
2902 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2904 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2905 memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
2906 slot = path->slots[0];
2907 btrfs_release_path(path);
2908 mutex_unlock(&fs_info->qgroup_rescan_lock);
2910 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2911 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
2912 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2913 found.type != BTRFS_METADATA_ITEM_KEY)
2915 if (found.type == BTRFS_METADATA_ITEM_KEY)
2916 num_bytes = fs_info->extent_root->nodesize;
2918 num_bytes = found.offset;
2920 ulist_reinit(qgroups);
2921 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
2925 spin_lock(&fs_info->qgroup_lock);
2926 seq = fs_info->qgroup_seq;
2927 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
2930 ret = qgroup_calc_old_refcnt(fs_info, 0, tmp, roots, qgroups,
2931 seq, &new_roots, 1);
2933 spin_unlock(&fs_info->qgroup_lock);
2938 ret = qgroup_adjust_counters(fs_info, 0, num_bytes, qgroups,
2939 seq, 0, new_roots, 1);
2941 spin_unlock(&fs_info->qgroup_lock);
2945 spin_unlock(&fs_info->qgroup_lock);
2949 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2954 static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
2956 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2957 qgroup_rescan_work);
2958 struct btrfs_path *path;
2959 struct btrfs_trans_handle *trans = NULL;
2960 struct ulist *tmp = NULL, *qgroups = NULL;
2961 struct extent_buffer *scratch_leaf = NULL;
2965 path = btrfs_alloc_path();
2968 qgroups = ulist_alloc(GFP_NOFS);
2971 tmp = ulist_alloc(GFP_NOFS);
2974 scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
2980 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2981 if (IS_ERR(trans)) {
2982 err = PTR_ERR(trans);
2985 if (!fs_info->quota_enabled) {
2988 err = qgroup_rescan_leaf(fs_info, path, trans,
2989 qgroups, tmp, scratch_leaf);
2992 btrfs_commit_transaction(trans, fs_info->fs_root);
2994 btrfs_end_transaction(trans, fs_info->fs_root);
2998 kfree(scratch_leaf);
2999 ulist_free(qgroups);
3001 btrfs_free_path(path);
3003 mutex_lock(&fs_info->qgroup_rescan_lock);
3004 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3007 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
3008 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3009 } else if (err < 0) {
3010 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3012 mutex_unlock(&fs_info->qgroup_rescan_lock);
3015 * only update status, since the previous part has alreay updated the
3018 trans = btrfs_start_transaction(fs_info->quota_root, 1);
3019 if (IS_ERR(trans)) {
3020 err = PTR_ERR(trans);
3022 "fail to start transaction for status update: %d\n",
3026 ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
3029 btrfs_err(fs_info, "fail to update qgroup status: %d\n", err);
3031 btrfs_end_transaction(trans, fs_info->quota_root);
3034 btrfs_info(fs_info, "qgroup scan completed%s",
3035 err > 0 ? " (inconsistency flag cleared)" : "");
3037 btrfs_err(fs_info, "qgroup scan failed with %d", err);
3041 complete_all(&fs_info->qgroup_rescan_completion);
3045 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
3046 * memory required for the rescan context.
3049 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
3055 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
3056 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
3061 mutex_lock(&fs_info->qgroup_rescan_lock);
3062 spin_lock(&fs_info->qgroup_lock);
3065 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
3067 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
3071 spin_unlock(&fs_info->qgroup_lock);
3072 mutex_unlock(&fs_info->qgroup_rescan_lock);
3075 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3078 memset(&fs_info->qgroup_rescan_progress, 0,
3079 sizeof(fs_info->qgroup_rescan_progress));
3080 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
3082 spin_unlock(&fs_info->qgroup_lock);
3083 mutex_unlock(&fs_info->qgroup_rescan_lock);
3085 init_completion(&fs_info->qgroup_rescan_completion);
3087 memset(&fs_info->qgroup_rescan_work, 0,
3088 sizeof(fs_info->qgroup_rescan_work));
3089 btrfs_init_work(&fs_info->qgroup_rescan_work,
3090 btrfs_qgroup_rescan_helper,
3091 btrfs_qgroup_rescan_worker, NULL, NULL);
3095 btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
3103 qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
3106 struct btrfs_qgroup *qgroup;
3108 spin_lock(&fs_info->qgroup_lock);
3109 /* clear all current qgroup tracking information */
3110 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
3111 qgroup = rb_entry(n, struct btrfs_qgroup, node);
3113 qgroup->rfer_cmpr = 0;
3115 qgroup->excl_cmpr = 0;
3117 spin_unlock(&fs_info->qgroup_lock);
3121 btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
3124 struct btrfs_trans_handle *trans;
3126 ret = qgroup_rescan_init(fs_info, 0, 1);
3131 * We have set the rescan_progress to 0, which means no more
3132 * delayed refs will be accounted by btrfs_qgroup_account_ref.
3133 * However, btrfs_qgroup_account_ref may be right after its call
3134 * to btrfs_find_all_roots, in which case it would still do the
3136 * To solve this, we're committing the transaction, which will
3137 * ensure we run all delayed refs and only after that, we are
3138 * going to clear all tracking information for a clean start.
3141 trans = btrfs_join_transaction(fs_info->fs_root);
3142 if (IS_ERR(trans)) {
3143 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3144 return PTR_ERR(trans);
3146 ret = btrfs_commit_transaction(trans, fs_info->fs_root);
3148 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3152 qgroup_rescan_zero_tracking(fs_info);
3154 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3155 &fs_info->qgroup_rescan_work);
3160 int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
3165 mutex_lock(&fs_info->qgroup_rescan_lock);
3166 spin_lock(&fs_info->qgroup_lock);
3167 running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3168 spin_unlock(&fs_info->qgroup_lock);
3169 mutex_unlock(&fs_info->qgroup_rescan_lock);
3172 ret = wait_for_completion_interruptible(
3173 &fs_info->qgroup_rescan_completion);
3179 * this is only called from open_ctree where we're still single threaded, thus
3180 * locking is omitted here.
3183 btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
3185 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
3186 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3187 &fs_info->qgroup_rescan_work);